agx86int.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements an asmoutput class for Intel syntax with Intel i386+
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {
  18. This unit implements an asmoutput class for Intel syntax with Intel i386+
  19. }
  20. unit agx86int;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cpubase,
  25. aasmbase,aasmtai,aasmdata,aasmcpu,assemble,cgutils;
  26. type
  27. Tx86IntelAssembler = class(TExternalAssembler)
  28. private
  29. procedure WriteReference(var ref : treference);
  30. procedure WriteOper(const o:toper;s : topsize; opcode: tasmop;dest : boolean);
  31. procedure WriteOper_jmp(const o:toper;s : topsize);
  32. public
  33. procedure WriteTree(p:TAsmList);override;
  34. procedure WriteAsmList;override;
  35. Function DoAssemble:boolean;override;
  36. procedure WriteExternals;
  37. end;
  38. implementation
  39. uses
  40. SysUtils,
  41. cutils,globtype,globals,systems,cclasses,
  42. verbose,finput,fmodule,script,cpuinfo,
  43. itx86int,
  44. cgbase
  45. ;
  46. const
  47. line_length = 70;
  48. secnames : array[TAsmSectiontype] of string[4] = ('','',
  49. 'CODE','DATA','DATA','DATA','BSS','',
  50. '','','','','','',
  51. '','','','',
  52. '',
  53. '',
  54. '',
  55. '',
  56. '',
  57. '','','','',
  58. '',
  59. '',
  60. '',
  61. '',
  62. '',
  63. '',
  64. '',
  65. '',
  66. '',
  67. '',
  68. '',
  69. '',
  70. '',
  71. '',
  72. '',
  73. '',
  74. '',
  75. '',
  76. '',
  77. '',
  78. '',
  79. '',
  80. '',
  81. '',
  82. '',
  83. '',
  84. '',
  85. '',
  86. '',
  87. '',
  88. '',
  89. '',
  90. '',
  91. '',
  92. '',
  93. '',
  94. '',
  95. ''
  96. );
  97. secnamesml64 : array[TAsmSectiontype] of string[7] = ('','',
  98. '_TEXT','_DATE','_DATA','_DATA','_BSS','',
  99. '','','','',
  100. 'idata$2','idata$4','idata$5','idata$6','idata$7','edata',
  101. '',
  102. '',
  103. '',
  104. '',
  105. '',
  106. '','','','',
  107. '',
  108. '',
  109. '',
  110. '',
  111. '',
  112. '',
  113. '',
  114. '',
  115. '',
  116. '',
  117. '',
  118. '',
  119. '',
  120. '',
  121. '',
  122. '',
  123. '',
  124. '',
  125. '',
  126. '',
  127. '',
  128. '',
  129. '',
  130. '',
  131. '',
  132. '',
  133. '',
  134. '',
  135. '',
  136. '',
  137. '',
  138. '',
  139. '',
  140. '',
  141. '',
  142. '',
  143. '',
  144. ''
  145. );
  146. function single2str(d : single) : string;
  147. var
  148. hs : string;
  149. p : byte;
  150. begin
  151. str(d,hs);
  152. { nasm expects a lowercase e }
  153. p:=pos('E',hs);
  154. if p>0 then
  155. hs[p]:='e';
  156. p:=pos('+',hs);
  157. if p>0 then
  158. delete(hs,p,1);
  159. single2str:=lower(hs);
  160. end;
  161. function double2str(d : double) : string;
  162. var
  163. hs : string;
  164. p : byte;
  165. begin
  166. str(d,hs);
  167. { nasm expects a lowercase e }
  168. p:=pos('E',hs);
  169. if p>0 then
  170. hs[p]:='e';
  171. p:=pos('+',hs);
  172. if p>0 then
  173. delete(hs,p,1);
  174. double2str:=lower(hs);
  175. end;
  176. function extended2str(e : extended) : string;
  177. var
  178. hs : string;
  179. p : byte;
  180. begin
  181. str(e,hs);
  182. { nasm expects a lowercase e }
  183. p:=pos('E',hs);
  184. if p>0 then
  185. hs[p]:='e';
  186. p:=pos('+',hs);
  187. if p>0 then
  188. delete(hs,p,1);
  189. extended2str:=lower(hs);
  190. end;
  191. function comp2str(d : bestreal) : string;
  192. type
  193. pdouble = ^double;
  194. var
  195. c : comp;
  196. dd : pdouble;
  197. begin
  198. c:=comp(d);
  199. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  200. comp2str:=double2str(dd^);
  201. end;
  202. function fixline(s:string):string;
  203. {
  204. return s with all leading and ending spaces and tabs removed
  205. }
  206. var
  207. i,j,k : longint;
  208. begin
  209. i:=length(s);
  210. while (i>0) and (s[i] in [#9,' ']) do
  211. dec(i);
  212. j:=1;
  213. while (j<i) and (s[j] in [#9,' ']) do
  214. inc(j);
  215. for k:=j to i do
  216. if s[k] in [#0..#31,#127..#255] then
  217. s[k]:='.';
  218. fixline:=Copy(s,j,i-j+1);
  219. end;
  220. {****************************************************************************
  221. tx86IntelAssembler
  222. ****************************************************************************}
  223. procedure tx86IntelAssembler.WriteReference(var ref : treference);
  224. var
  225. first : boolean;
  226. begin
  227. with ref do
  228. begin
  229. first:=true;
  230. if segment<>NR_NO then
  231. AsmWrite(masm_regname(segment)+':[')
  232. else
  233. AsmWrite('[');
  234. if assigned(symbol) then
  235. begin
  236. if (target_asm.id = as_i386_tasm) then
  237. AsmWrite('dword ptr ');
  238. AsmWrite(symbol.name);
  239. first:=false;
  240. end;
  241. if (base<>NR_NO) then
  242. begin
  243. if not(first) then
  244. AsmWrite('+')
  245. else
  246. first:=false;
  247. AsmWrite(masm_regname(base));
  248. end;
  249. if (index<>NR_NO) then
  250. begin
  251. if not(first) then
  252. AsmWrite('+')
  253. else
  254. first:=false;
  255. AsmWrite(masm_regname(index));
  256. if scalefactor<>0 then
  257. AsmWrite('*'+tostr(scalefactor));
  258. end;
  259. if offset<0 then
  260. begin
  261. AsmWrite(tostr(offset));
  262. first:=false;
  263. end
  264. else if (offset>0) then
  265. begin
  266. AsmWrite('+'+tostr(offset));
  267. first:=false;
  268. end;
  269. if first then
  270. AsmWrite('0');
  271. AsmWrite(']');
  272. end;
  273. end;
  274. procedure tx86IntelAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;dest : boolean);
  275. begin
  276. case o.typ of
  277. top_reg :
  278. AsmWrite(masm_regname(o.reg));
  279. top_const :
  280. AsmWrite(tostr(o.val));
  281. top_ref :
  282. begin
  283. if o.ref^.refaddr=addr_no then
  284. begin
  285. if ((opcode <> A_LGS) and (opcode <> A_LSS) and
  286. (opcode <> A_LFS) and (opcode <> A_LDS) and
  287. (opcode <> A_LES)) then
  288. Begin
  289. case s of
  290. S_B : AsmWrite('byte ptr ');
  291. S_W : AsmWrite('word ptr ');
  292. S_L : AsmWrite('dword ptr ');
  293. S_Q : AsmWrite('qword ptr ');
  294. S_IS : AsmWrite('word ptr ');
  295. S_IL : AsmWrite('dword ptr ');
  296. S_IQ : AsmWrite('qword ptr ');
  297. S_FS : AsmWrite('dword ptr ');
  298. S_FL : AsmWrite('qword ptr ');
  299. S_T,
  300. S_FX : AsmWrite('tbyte ptr ');
  301. S_BW : if dest then
  302. AsmWrite('word ptr ')
  303. else
  304. AsmWrite('byte ptr ');
  305. S_BL : if dest then
  306. AsmWrite('dword ptr ')
  307. else
  308. AsmWrite('byte ptr ');
  309. S_WL : if dest then
  310. AsmWrite('dword ptr ')
  311. else
  312. AsmWrite('word ptr ');
  313. {$ifdef x86_64}
  314. S_BQ : if dest then
  315. AsmWrite('qword ptr ')
  316. else
  317. AsmWrite('byte ptr ');
  318. S_WQ : if dest then
  319. AsmWrite('qword ptr ')
  320. else
  321. AsmWrite('word ptr ');
  322. S_LQ : if dest then
  323. AsmWrite('qword ptr ')
  324. else
  325. AsmWrite('dword ptr ');
  326. S_XMM: AsmWrite('xmmword ptr ');
  327. {$endif x86_64}
  328. end;
  329. end;
  330. WriteReference(o.ref^);
  331. end
  332. else
  333. begin
  334. AsmWrite('offset ');
  335. if assigned(o.ref^.symbol) then
  336. AsmWrite(o.ref^.symbol.name);
  337. if o.ref^.offset>0 then
  338. AsmWrite('+'+tostr(o.ref^.offset))
  339. else
  340. if o.ref^.offset<0 then
  341. AsmWrite(tostr(o.ref^.offset))
  342. else
  343. if not(assigned(o.ref^.symbol)) then
  344. AsmWrite('0');
  345. end;
  346. end;
  347. else
  348. internalerror(2005060510);
  349. end;
  350. end;
  351. procedure tx86IntelAssembler.WriteOper_jmp(const o:toper;s : topsize);
  352. begin
  353. case o.typ of
  354. top_reg :
  355. AsmWrite(masm_regname(o.reg));
  356. top_const :
  357. AsmWrite(tostr(o.val));
  358. top_ref :
  359. { what about lcall or ljmp ??? }
  360. begin
  361. if o.ref^.refaddr=addr_no then
  362. begin
  363. if (target_asm.id <> as_i386_tasm) then
  364. begin
  365. if s=S_FAR then
  366. AsmWrite('far ptr ')
  367. else
  368. {$ifdef x86_64}
  369. AsmWrite('qword ptr ');
  370. {$else x86_64}
  371. AsmWrite('dword ptr ');
  372. {$endif x86_64}
  373. end;
  374. WriteReference(o.ref^);
  375. end
  376. else
  377. begin
  378. AsmWrite(o.ref^.symbol.name);
  379. if o.ref^.offset>0 then
  380. AsmWrite('+'+tostr(o.ref^.offset))
  381. else
  382. if o.ref^.offset<0 then
  383. AsmWrite(tostr(o.ref^.offset));
  384. end;
  385. end;
  386. else
  387. internalerror(2005060511);
  388. end;
  389. end;
  390. const
  391. ait_const2str : array[aitconst_128bit..aitconst_secrel32_symbol] of string[20]=(
  392. #9''#9,#9'DQ'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  393. #9'FIXMESLEB',#9'FIXEMEULEB',
  394. #9'DD RVA'#9,#9'DD SECREL32'#9
  395. );
  396. Function PadTabs(const p:string;addch:char):string;
  397. var
  398. s : string;
  399. i : longint;
  400. begin
  401. i:=length(p);
  402. if addch<>#0 then
  403. begin
  404. inc(i);
  405. s:=p+addch;
  406. end
  407. else
  408. s:=p;
  409. if i<8 then
  410. PadTabs:=s+#9#9
  411. else
  412. PadTabs:=s+#9;
  413. end;
  414. procedure tx86IntelAssembler.WriteTree(p:TAsmList);
  415. const
  416. regallocstr : array[tregalloctype] of string[10]=(' allocated',' released',' sync',' resized');
  417. tempallocstr : array[boolean] of string[10]=(' released',' allocated');
  418. var
  419. s,
  420. prefix,
  421. suffix : string;
  422. hp : tai;
  423. hp1 : tailineinfo;
  424. counter,
  425. lines,
  426. InlineLevel : longint;
  427. i,j,l : longint;
  428. consttype : taiconst_type;
  429. do_line,DoNotSplitLine,
  430. quoted : boolean;
  431. begin
  432. if not assigned(p) then
  433. exit;
  434. { lineinfo is only needed for al_procedures (PFV) }
  435. do_line:=((cs_asm_source in current_settings.globalswitches) or
  436. (cs_lineinfo in current_settings.moduleswitches))
  437. and (p=current_asmdata.asmlists[al_procedures]);
  438. InlineLevel:=0;
  439. DoNotSplitLine:=false;
  440. hp:=tai(p.first);
  441. while assigned(hp) do
  442. begin
  443. if do_line and not(hp.typ in SkipLineInfo) and
  444. not DoNotSplitLine and (InlineLevel=0) then
  445. begin
  446. hp1:=hp as tailineinfo;
  447. { load infile }
  448. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  449. begin
  450. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  451. if assigned(infile) then
  452. begin
  453. { open only if needed !! }
  454. if (cs_asm_source in current_settings.globalswitches) then
  455. infile.open;
  456. end;
  457. { avoid unnecessary reopens of the same file !! }
  458. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  459. { be sure to change line !! }
  460. lastfileinfo.line:=-1;
  461. end;
  462. { write source }
  463. if (cs_asm_source in current_settings.globalswitches) and
  464. assigned(infile) then
  465. begin
  466. if (infile<>lastinfile) then
  467. begin
  468. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  469. if assigned(lastinfile) then
  470. lastinfile.close;
  471. end;
  472. if (hp1.fileinfo.line<>lastfileinfo.line) and
  473. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  474. begin
  475. if (hp1.fileinfo.line<>0) and
  476. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  477. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  478. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  479. { set it to a negative value !
  480. to make that is has been read already !! PM }
  481. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  482. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  483. end;
  484. end;
  485. lastfileinfo:=hp1.fileinfo;
  486. lastinfile:=infile;
  487. end;
  488. DoNotSplitLine:=false;
  489. case hp.typ of
  490. ait_comment :
  491. Begin
  492. AsmWrite(target_asm.comment);
  493. AsmWritePChar(tai_comment(hp).str);
  494. AsmLn;
  495. End;
  496. ait_regalloc :
  497. begin
  498. if (cs_asm_regalloc in current_settings.globalswitches) then
  499. AsmWriteLn(target_asm.comment+'Register '+masm_regname(tai_regalloc(hp).reg)+
  500. regallocstr[tai_regalloc(hp).ratype]);
  501. end;
  502. ait_tempalloc :
  503. begin
  504. if (cs_asm_tempalloc in current_settings.globalswitches) then
  505. begin
  506. {$ifdef EXTDEBUG}
  507. if assigned(tai_tempalloc(hp).problem) then
  508. AsmWriteLn(target_asm.comment+tai_tempalloc(hp).problem^+' ('+tostr(tai_tempalloc(hp).temppos)+','+
  509. tostr(tai_tempalloc(hp).tempsize)+')')
  510. else
  511. {$endif EXTDEBUG}
  512. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  513. tostr(tai_tempalloc(hp).tempsize)+tempallocstr[tai_tempalloc(hp).allocation]);
  514. end;
  515. end;
  516. ait_section :
  517. begin
  518. if tai_section(hp).sectype<>sec_none then
  519. begin
  520. if target_asm.id=as_x86_64_masm then
  521. begin
  522. if LasTSecType<>sec_none then
  523. AsmWriteLn(secnamesml64[LasTSecType]+#9#9'ENDS');
  524. AsmLn;
  525. AsmWriteLn(secnamesml64[tai_section(hp).sectype]+#9+'SEGMENT')
  526. end
  527. else
  528. begin
  529. if LasTSecType<>sec_none then
  530. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  531. AsmLn;
  532. AsmWriteLn('_'+secnames[tai_section(hp).sectype]+#9#9+
  533. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  534. secnames[tai_section(hp).sectype]+'''');
  535. end;
  536. end;
  537. LasTSecType:=tai_section(hp).sectype;
  538. end;
  539. ait_align :
  540. begin
  541. { CAUSES PROBLEMS WITH THE SEGMENT DEFINITION }
  542. { SEGMENT DEFINITION SHOULD MATCH TYPE OF ALIGN }
  543. { HERE UNDER TASM! }
  544. if tai_align_abstract(hp).aligntype>1 then
  545. AsmWriteLn(#9'ALIGN '+tostr(tai_align_abstract(hp).aligntype));
  546. end;
  547. ait_datablock :
  548. begin
  549. if tai_datablock(hp).is_global then
  550. AsmWriteLn(#9'PUBLIC'#9+tai_datablock(hp).sym.name);
  551. AsmWriteLn(PadTabs(tai_datablock(hp).sym.name,#0)+'DB'#9+tostr(tai_datablock(hp).size)+' DUP(?)');
  552. end;
  553. ait_const:
  554. begin
  555. consttype:=tai_const(hp).consttype;
  556. case consttype of
  557. aitconst_uleb128bit,
  558. aitconst_sleb128bit,
  559. aitconst_128bit,
  560. aitconst_64bit,
  561. aitconst_32bit,
  562. aitconst_16bit,
  563. aitconst_8bit,
  564. aitconst_rva_symbol,
  565. aitconst_secrel32_symbol :
  566. begin
  567. AsmWrite(ait_const2str[consttype]);
  568. l:=0;
  569. repeat
  570. if assigned(tai_const(hp).sym) then
  571. begin
  572. if assigned(tai_const(hp).endsym) then
  573. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  574. else
  575. s:=tai_const(hp).sym.name;
  576. if tai_const(hp).value<>0 then
  577. s:=s+tostr_with_plus(tai_const(hp).value);
  578. end
  579. else
  580. s:=tostr(tai_const(hp).value);
  581. AsmWrite(s);
  582. inc(l,length(s));
  583. if (l>line_length) or
  584. (hp.next=nil) or
  585. (tai(hp.next).typ<>ait_const) or
  586. (tai_const(hp.next).consttype<>consttype) then
  587. break;
  588. hp:=tai(hp.next);
  589. AsmWrite(',');
  590. until false;
  591. AsmLn;
  592. end;
  593. else
  594. internalerror(200704253);
  595. end;
  596. end;
  597. ait_real_32bit :
  598. AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  599. ait_real_64bit :
  600. AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  601. ait_real_80bit :
  602. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  603. ait_comp_64bit :
  604. AsmWriteLn(#9#9'DQ'#9+extended2str(tai_comp_64bit(hp).value));
  605. ait_string :
  606. begin
  607. counter := 0;
  608. lines := tai_string(hp).len div line_length;
  609. { separate lines in different parts }
  610. if tai_string(hp).len > 0 then
  611. Begin
  612. for j := 0 to lines-1 do
  613. begin
  614. AsmWrite(#9#9'DB'#9);
  615. quoted:=false;
  616. for i:=counter to counter+line_length-1 do
  617. begin
  618. { it is an ascii character. }
  619. if (ord(tai_string(hp).str[i])>31) and
  620. (ord(tai_string(hp).str[i])<128) and
  621. (tai_string(hp).str[i]<>'"') then
  622. begin
  623. if not(quoted) then
  624. begin
  625. if i>counter then
  626. AsmWrite(',');
  627. AsmWrite('"');
  628. end;
  629. AsmWrite(tai_string(hp).str[i]);
  630. quoted:=true;
  631. end { if > 31 and < 128 and ord('"') }
  632. else
  633. begin
  634. if quoted then
  635. AsmWrite('"');
  636. if i>counter then
  637. AsmWrite(',');
  638. quoted:=false;
  639. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  640. end;
  641. end; { end for i:=0 to... }
  642. if quoted then AsmWrite('"');
  643. AsmWrite(target_info.newline);
  644. counter := counter+line_length;
  645. end; { end for j:=0 ... }
  646. { do last line of lines }
  647. if counter<tai_string(hp).len then
  648. AsmWrite(#9#9'DB'#9);
  649. quoted:=false;
  650. for i:=counter to tai_string(hp).len-1 do
  651. begin
  652. { it is an ascii character. }
  653. if (ord(tai_string(hp).str[i])>31) and
  654. (ord(tai_string(hp).str[i])<128) and
  655. (tai_string(hp).str[i]<>'"') then
  656. begin
  657. if not(quoted) then
  658. begin
  659. if i>counter then
  660. AsmWrite(',');
  661. AsmWrite('"');
  662. end;
  663. AsmWrite(tai_string(hp).str[i]);
  664. quoted:=true;
  665. end { if > 31 and < 128 and " }
  666. else
  667. begin
  668. if quoted then
  669. AsmWrite('"');
  670. if i>counter then
  671. AsmWrite(',');
  672. quoted:=false;
  673. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  674. end;
  675. end; { end for i:=0 to... }
  676. if quoted then
  677. AsmWrite('"');
  678. end;
  679. AsmLn;
  680. end;
  681. ait_label :
  682. begin
  683. if tai_label(hp).labsym.is_used then
  684. begin
  685. AsmWrite(tai_label(hp).labsym.name);
  686. if assigned(hp.next) and not(tai(hp.next).typ in
  687. [ait_const,
  688. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  689. AsmWriteLn(':')
  690. else
  691. DoNotSplitLine:=true;
  692. end;
  693. end;
  694. ait_symbol :
  695. begin
  696. if tai_symbol(hp).has_value then
  697. internalerror(2009090802);
  698. if tai_symbol(hp).is_global then
  699. AsmWriteLn(#9'PUBLIC'#9+tai_symbol(hp).sym.name);
  700. AsmWrite(tai_symbol(hp).sym.name);
  701. if assigned(hp.next) and not(tai(hp.next).typ in
  702. [ait_const,
  703. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  704. AsmWriteLn(':')
  705. end;
  706. ait_symbol_end :
  707. begin
  708. end;
  709. ait_instruction :
  710. begin
  711. taicpu(hp).CheckNonCommutativeOpcodes;
  712. taicpu(hp).SetOperandOrder(op_intel);
  713. { Reset }
  714. suffix:='';
  715. prefix:= '';
  716. { We need to explicitely set
  717. word prefix to get selectors
  718. to be pushed in 2 bytes PM }
  719. if (taicpu(hp).opsize=S_W) and
  720. (
  721. (
  722. (taicpu(hp).opcode=A_PUSH) or
  723. (taicpu(hp).opcode=A_POP)
  724. ) and
  725. (taicpu(hp).oper[0]^.typ=top_reg) and
  726. is_segment_reg(taicpu(hp).oper[0]^.reg)
  727. ) then
  728. AsmWriteln(#9#9'DB'#9'066h');
  729. { added prefix instructions, must be on same line as opcode }
  730. if (taicpu(hp).ops = 0) and
  731. ((taicpu(hp).opcode = A_REP) or
  732. (taicpu(hp).opcode = A_LOCK) or
  733. (taicpu(hp).opcode = A_REPE) or
  734. (taicpu(hp).opcode = A_REPNZ) or
  735. (taicpu(hp).opcode = A_REPZ) or
  736. (taicpu(hp).opcode = A_REPNE)) then
  737. Begin
  738. prefix:=std_op2str[taicpu(hp).opcode]+#9;
  739. { there can be a stab inbetween when the opcode was on
  740. a different line in the source code }
  741. repeat
  742. hp:=tai(hp.next);
  743. until (hp=nil) or (hp.typ=ait_instruction);
  744. { this is theorically impossible... }
  745. if hp=nil then
  746. begin
  747. AsmWriteLn(#9#9+prefix);
  748. break;
  749. end;
  750. { nasm prefers prefix on a line alone
  751. AsmWriteln(#9#9+prefix); but not masm PM
  752. prefix:=''; }
  753. if target_asm.id in [as_i386_nasmcoff,as_i386_nasmwin32,as_i386_nasmwdosx,
  754. as_i386_nasmelf,as_i386_nasmobj,as_i386_nasmbeos,as_i386_nasmhaiku] then
  755. begin
  756. AsmWriteln(prefix);
  757. prefix:='';
  758. end;
  759. end
  760. else
  761. prefix:= '';
  762. if (target_asm.id = as_i386_wasm) and
  763. (taicpu(hp).opsize=S_W) and
  764. (taicpu(hp).opcode=A_PUSH) and
  765. (taicpu(hp).oper[0]^.typ=top_const) then
  766. begin
  767. AsmWriteln(#9#9'DB 66h,68h ; pushw imm16');
  768. AsmWrite(#9#9'DW');
  769. end
  770. else if (target_asm.id=as_x86_64_masm) and
  771. (taicpu(hp).opcode=A_MOVQ) then
  772. AsmWrite(#9#9'mov')
  773. else
  774. AsmWrite(#9#9+prefix+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]+suffix);
  775. if taicpu(hp).ops<>0 then
  776. begin
  777. if is_calljmp(taicpu(hp).opcode) then
  778. begin
  779. AsmWrite(#9);
  780. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opsize);
  781. end
  782. else
  783. begin
  784. for i:=0to taicpu(hp).ops-1 do
  785. begin
  786. if i=0 then
  787. AsmWrite(#9)
  788. else
  789. AsmWrite(',');
  790. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,(i=2));
  791. end;
  792. end;
  793. end;
  794. AsmLn;
  795. end;
  796. ait_stab,
  797. ait_force_line,
  798. ait_function_name : ;
  799. ait_cutobject :
  800. begin
  801. { only reset buffer if nothing has changed }
  802. if AsmSize=AsmStartSize then
  803. AsmClear
  804. else
  805. begin
  806. if LasTSecType<>sec_none then
  807. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  808. AsmLn;
  809. AsmWriteLn(#9'END');
  810. AsmClose;
  811. DoAssemble;
  812. AsmCreate(tai_cutobject(hp).place);
  813. end;
  814. { avoid empty files }
  815. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  816. begin
  817. if tai(hp.next).typ=ait_section then
  818. lasTSecType:=tai_section(hp.next).sectype;
  819. hp:=tai(hp.next);
  820. end;
  821. AsmWriteLn(#9'.386p');
  822. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  823. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  824. { I was told that this isn't necesarry because }
  825. { the labels generated by FPC are unique (FK) }
  826. { AsmWriteLn(#9'LOCALS '+target_asm.labelprefix); }
  827. if lasTSectype<>sec_none then
  828. AsmWriteLn('_'+secnames[lasTSectype]+#9#9+
  829. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  830. secnames[lasTSectype]+'''');
  831. AsmStartSize:=AsmSize;
  832. end;
  833. ait_marker :
  834. begin
  835. if tai_marker(hp).kind=mark_NoLineInfoStart then
  836. inc(InlineLevel)
  837. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  838. dec(InlineLevel);
  839. end;
  840. ait_directive :
  841. begin
  842. case tai_directive(hp).directive of
  843. asd_nasm_import :
  844. AsmWrite('import ');
  845. asd_extern :
  846. AsmWrite('EXTRN ');
  847. else
  848. internalerror(200509192);
  849. end;
  850. if assigned(tai_directive(hp).name) then
  851. AsmWrite(tai_directive(hp).name^);
  852. AsmLn;
  853. end;
  854. else
  855. internalerror(10000);
  856. end;
  857. hp:=tai(hp.next);
  858. end;
  859. end;
  860. procedure tx86intelassembler.WriteExternals;
  861. var
  862. sym : TAsmSymbol;
  863. i : longint;
  864. begin
  865. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  866. begin
  867. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  868. if sym.bind=AB_EXTERNAL then
  869. begin
  870. case target_asm.id of
  871. as_i386_masm,
  872. as_i386_wasm :
  873. AsmWriteln(#9'EXTRN'#9+sym.name+': NEAR');
  874. as_x86_64_masm :
  875. AsmWriteln(#9'EXTRN'#9+sym.name+': PROC');
  876. else
  877. AsmWriteln(#9'EXTRN'#9+sym.name);
  878. end;
  879. end;
  880. end;
  881. end;
  882. function tx86intelassembler.DoAssemble : boolean;
  883. var
  884. masmobjfn : string;
  885. begin
  886. DoAssemble:=Inherited DoAssemble;
  887. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  888. if (target_asm.id in [as_i386_masm,as_i386_wasm]) then
  889. begin
  890. masmobjfn:=ChangeFileExt(objfilename,'.obj');
  891. if not(cs_asm_extern in current_settings.globalswitches) then
  892. begin
  893. if Not FileExists(objfilename) and
  894. FileExists(masmobjfn) then
  895. RenameFile(masmobjfn,objfilename);
  896. end
  897. else
  898. AsmRes.AddAsmCommand('mv',masmobjfn+' '+objfilename,objfilename);
  899. end;
  900. end;
  901. procedure tx86IntelAssembler.WriteAsmList;
  902. var
  903. hal : tasmlisttype;
  904. begin
  905. {$ifdef EXTDEBUG}
  906. if assigned(current_module.mainsource) then
  907. comment(v_info,'Start writing intel-styled assembler output for '+current_module.mainsource^);
  908. {$endif}
  909. if target_asm.id<>as_x86_64_masm then
  910. begin
  911. AsmWriteLn(#9'.386p');
  912. { masm 6.11 does not seem to like LOCALS PM }
  913. if (target_asm.id = as_i386_tasm) then
  914. begin
  915. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  916. end;
  917. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  918. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  919. AsmLn;
  920. end;
  921. WriteExternals;
  922. for hal:=low(TasmlistType) to high(TasmlistType) do
  923. begin
  924. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  925. writetree(current_asmdata.asmlists[hal]);
  926. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  927. end;
  928. { better do this at end of WriteTree, but then there comes a trouble with
  929. al_const which does not have leading ait_section and thus goes out of segment }
  930. { TODO: probably ml64 needs 'closing' last section, too }
  931. if LastSecType <> sec_none then
  932. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  933. LastSecType := sec_none;
  934. AsmWriteLn(#9'END');
  935. AsmLn;
  936. {$ifdef EXTDEBUG}
  937. if assigned(current_module.mainsource) then
  938. comment(v_info,'Done writing intel-styled assembler output for '+current_module.mainsource^);
  939. {$endif EXTDEBUG}
  940. end;
  941. {*****************************************************************************
  942. Initialize
  943. *****************************************************************************}
  944. const
  945. {$ifdef i386}
  946. as_i386_tasm_info : tasminfo =
  947. (
  948. id : as_i386_tasm;
  949. idtxt : 'TASM';
  950. asmbin : 'tasm';
  951. asmcmd : '/m2 /ml $ASM $OBJ';
  952. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  953. flags : [af_allowdirect,af_needar,af_labelprefix_only_inside_procedure];
  954. labelprefix : '@@';
  955. comment : '; ';
  956. );
  957. as_i386_masm_info : tasminfo =
  958. (
  959. id : as_i386_masm;
  960. idtxt : 'MASM';
  961. asmbin : 'masm';
  962. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  963. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  964. flags : [af_allowdirect,af_needar];
  965. labelprefix : '@@';
  966. comment : '; ';
  967. );
  968. as_i386_wasm_info : tasminfo =
  969. (
  970. id : as_i386_wasm;
  971. idtxt : 'WASM';
  972. asmbin : 'wasm';
  973. asmcmd : '$ASM -6s -fp6 -ms -zq -Fo=$OBJ';
  974. supported_targets : [system_i386_watcom];
  975. flags : [af_allowdirect,af_needar];
  976. labelprefix : '@@';
  977. comment : '; ';
  978. );
  979. {$endif i386}
  980. {$ifdef x86_64}
  981. as_x86_64_masm_info : tasminfo =
  982. (
  983. id : as_x86_64_masm;
  984. idtxt : 'MASM';
  985. asmbin : 'ml64';
  986. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  987. supported_targets : [system_x86_64_win64];
  988. flags : [af_allowdirect,af_needar];
  989. labelprefix : '@@';
  990. comment : '; ';
  991. );
  992. {$endif x86_64}
  993. initialization
  994. {$ifdef x86_64}
  995. RegisterAssembler(as_x86_64_masm_info,tx86IntelAssembler);
  996. {$endif x86_64}
  997. {$ifdef i386}
  998. RegisterAssembler(as_i386_tasm_info,tx86IntelAssembler);
  999. RegisterAssembler(as_i386_masm_info,tx86IntelAssembler);
  1000. RegisterAssembler(as_i386_wasm_info,tx86IntelAssembler);
  1001. {$endif i386}
  1002. end.