agx86int.pas 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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,constexp,
  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. function single2str(d : single) : string; override;
  34. function double2str(d : double) : string; override;
  35. function extended2str(e : extended) : string; override;
  36. function comp2str(d : bestreal) : string;
  37. procedure WriteTree(p:TAsmList);override;
  38. procedure WriteAsmList;override;
  39. Function DoAssemble:boolean;override;
  40. procedure WriteExternals;
  41. end;
  42. implementation
  43. uses
  44. SysUtils,math,
  45. cutils,globtype,globals,systems,cclasses,
  46. verbose,cscript,cpuinfo,
  47. itx86int,
  48. cgbase
  49. {$ifdef EXTDEBUG}
  50. ,fmodule
  51. {$endif EXTDEBUG}
  52. ;
  53. const
  54. line_length = 70;
  55. max_tokens : longint = 25;
  56. (*
  57. wasm_cpu_name : array[tcputype] of string = (
  58. {$if defined(x86_64)}
  59. 'IA64', // cpu_none,
  60. '686', // cpu_athlon64,
  61. '686', // cpu_core_i,
  62. '686', // cpu_core_avx,
  63. '686' // cpu_core_avx2
  64. {$elseif defined(i386)}
  65. 'IA64', // cpu_none,
  66. '386', // cpu_386,
  67. '486', // cpu_486,
  68. '586', // cpu_Pentium,
  69. '686', // cpu_Pentium2,
  70. '686', // cpu_Pentium3,
  71. '686', // cpu_Pentium4,
  72. '686', // cpu_PentiumM,
  73. '686', // cpu_core_i,
  74. '686', // cpu_core_avx,
  75. '686' // cpu_core_avx2
  76. {$elseif defined(i8086)}
  77. 'IA64', // cpu_none
  78. '8086', // cpu_8086
  79. '186', // cpu_186
  80. '286', // cpu_286
  81. '386', // cpu_386
  82. '486', // cpu_486
  83. '586', // cpu_Pentium
  84. '686', // cpu_Pentium2
  85. '686', // cpu_Pentium3
  86. '686', // cpu_Pentium4
  87. '686' // cpu_PentiumM
  88. {$endif}
  89. );
  90. *)
  91. secnames : array[TAsmSectiontype] of string[4] = ('','',
  92. 'CODE','DATA','DATA','DATA','BSS','TLS',
  93. '','','','','','',
  94. '','','','',
  95. '',
  96. '',
  97. '',
  98. '',
  99. '',
  100. '','','','','','',
  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. secnamesml64 : array[TAsmSectiontype] of string[7] = ('','',
  145. '_TEXT','_DATA','_DATA','_DATA','_BSS','_TLS',
  146. '','','','',
  147. 'idata$2','idata$4','idata$5','idata$6','idata$7','edata',
  148. '',
  149. '',
  150. '',
  151. '',
  152. '',
  153. '','','','','','',
  154. '',
  155. '',
  156. '',
  157. '',
  158. '',
  159. '',
  160. '',
  161. '',
  162. '',
  163. '',
  164. '',
  165. '',
  166. '',
  167. '',
  168. '',
  169. '',
  170. '',
  171. '',
  172. '',
  173. '',
  174. '',
  175. '',
  176. '',
  177. '',
  178. '',
  179. '',
  180. '',
  181. '',
  182. '',
  183. '',
  184. '',
  185. '',
  186. '',
  187. '',
  188. '',
  189. '',
  190. '',
  191. '',
  192. '',
  193. '',
  194. '',
  195. ''
  196. );
  197. function TX86IntelAssembler.single2str(d : single) : string;
  198. var
  199. hs : string;
  200. p : byte;
  201. begin
  202. str(d,hs);
  203. { nasm expects a lowercase e }
  204. p:=pos('E',hs);
  205. if p>0 then
  206. hs[p]:='e';
  207. p:=pos('+',hs);
  208. if p>0 then
  209. delete(hs,p,1);
  210. single2str:=lower(hs);
  211. end;
  212. function TX86IntelAssembler.double2str(d : double) : string;
  213. var
  214. hs : string;
  215. p : byte;
  216. begin
  217. str(d,hs);
  218. { nasm expects a lowercase e }
  219. p:=pos('E',hs);
  220. if p>0 then
  221. hs[p]:='e';
  222. p:=pos('+',hs);
  223. if p>0 then
  224. delete(hs,p,1);
  225. double2str:=lower(hs);
  226. end;
  227. function TX86IntelAssembler.extended2str(e : extended) : string;
  228. var
  229. hs : string;
  230. p : byte;
  231. begin
  232. str(e,hs);
  233. { nasm expects a lowercase e }
  234. p:=pos('E',hs);
  235. if p>0 then
  236. hs[p]:='e';
  237. p:=pos('+',hs);
  238. if p>0 then
  239. delete(hs,p,1);
  240. extended2str:=lower(hs);
  241. end;
  242. function TX86IntelAssembler.comp2str(d : bestreal) : string;
  243. type
  244. pdouble = ^double;
  245. var
  246. c : comp;
  247. dd : pdouble;
  248. begin
  249. c:=comp(d);
  250. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  251. comp2str:=double2str(dd^);
  252. end;
  253. { MASM supports aligns up to 8192 }
  254. function alignstr(b : longint) : string;
  255. begin
  256. case b of
  257. 1: result:='BYTE';
  258. 2: result:='WORD';
  259. 4: result:='DWORD';
  260. 0,
  261. 16: result:='PARA';
  262. 256: result:='PAGE';
  263. else
  264. result:='ALIGN('+tostr(b)+')';
  265. end;
  266. end;
  267. {****************************************************************************
  268. tx86IntelAssembler
  269. ****************************************************************************}
  270. procedure tx86IntelAssembler.WriteReference(var ref : treference);
  271. var
  272. first : boolean;
  273. begin
  274. with ref do
  275. begin
  276. first:=true;
  277. if segment<>NR_NO then
  278. writer.AsmWrite(masm_regname(segment)+':[')
  279. else
  280. writer.AsmWrite('[');
  281. if assigned(symbol) then
  282. begin
  283. if (asminfo^.id = as_i386_tasm) then
  284. writer.AsmWrite('dword ptr ');
  285. writer.AsmWrite(symbol.name);
  286. first:=false;
  287. end;
  288. if (base<>NR_NO) then
  289. begin
  290. if not(first) then
  291. writer.AsmWrite('+')
  292. else
  293. first:=false;
  294. {$ifdef x86_64}
  295. { ml64 needs [$+foo] instead of [rip+foo] }
  296. if (base=NR_RIP) and (asminfo^.id=as_x86_64_masm) then
  297. writer.AsmWrite('$')
  298. else
  299. {$endif x86_64}
  300. writer.AsmWrite(masm_regname(base));
  301. end;
  302. if (index<>NR_NO) then
  303. begin
  304. if not(first) then
  305. writer.AsmWrite('+')
  306. else
  307. first:=false;
  308. writer.AsmWrite(masm_regname(index));
  309. if scalefactor<>0 then
  310. writer.AsmWrite('*'+tostr(scalefactor));
  311. end;
  312. if offset<0 then
  313. begin
  314. writer.AsmWrite(tostr(offset));
  315. first:=false;
  316. end
  317. else if (offset>0) then
  318. begin
  319. writer.AsmWrite('+'+tostr(offset));
  320. first:=false;
  321. end;
  322. if first then
  323. writer.AsmWrite('0');
  324. writer.AsmWrite(']');
  325. end;
  326. end;
  327. procedure tx86IntelAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;dest : boolean);
  328. begin
  329. case o.typ of
  330. top_reg :
  331. writer.AsmWrite(masm_regname(o.reg));
  332. top_const :
  333. writer.AsmWrite(tostr(o.val));
  334. top_ref :
  335. begin
  336. if o.ref^.refaddr in [addr_no,addr_pic,addr_pic_no_got] then
  337. begin
  338. if ((opcode <> A_LGS) and (opcode <> A_LSS) and
  339. (opcode <> A_LFS)
  340. {$ifndef x86_64}
  341. and (opcode <> A_LDS) and (opcode <> A_LES)
  342. {$endif x86_64}
  343. ) then
  344. Begin
  345. case s of
  346. S_B : writer.AsmWrite('byte ptr ');
  347. S_W : writer.AsmWrite('word ptr ');
  348. S_L : writer.AsmWrite('dword ptr ');
  349. S_Q : writer.AsmWrite('qword ptr ');
  350. S_IS : writer.AsmWrite('word ptr ');
  351. S_IL : writer.AsmWrite('dword ptr ');
  352. S_IQ : writer.AsmWrite('qword ptr ');
  353. S_FS : writer.AsmWrite('dword ptr ');
  354. S_FL : writer.AsmWrite('qword ptr ');
  355. S_T,
  356. S_FX : writer.AsmWrite('tbyte ptr ');
  357. S_BW : if dest then
  358. writer.AsmWrite('word ptr ')
  359. else
  360. writer.AsmWrite('byte ptr ');
  361. S_BL : if dest then
  362. writer.AsmWrite('dword ptr ')
  363. else
  364. writer.AsmWrite('byte ptr ');
  365. S_WL : if dest then
  366. writer.AsmWrite('dword ptr ')
  367. else
  368. writer.AsmWrite('word ptr ');
  369. S_XMM: writer.AsmWrite('xmmword ptr ');
  370. S_YMM: writer.AsmWrite('ymmword ptr ');
  371. S_ZMM: writer.AsmWrite('zmmword ptr ');
  372. {$ifdef x86_64}
  373. S_BQ : if dest then
  374. writer.AsmWrite('qword ptr ')
  375. else
  376. writer.AsmWrite('byte ptr ');
  377. S_WQ : if dest then
  378. writer.AsmWrite('qword ptr ')
  379. else
  380. writer.AsmWrite('word ptr ');
  381. S_LQ : if dest then
  382. writer.AsmWrite('qword ptr ')
  383. else
  384. writer.AsmWrite('dword ptr ');
  385. {$endif x86_64}
  386. else
  387. ;
  388. end;
  389. end;
  390. WriteReference(o.ref^);
  391. end
  392. else
  393. begin
  394. writer.AsmWrite('offset ');
  395. if assigned(o.ref^.symbol) then
  396. writer.AsmWrite(o.ref^.symbol.name);
  397. if o.ref^.offset>0 then
  398. writer.AsmWrite('+'+tostr(o.ref^.offset))
  399. else
  400. if o.ref^.offset<0 then
  401. writer.AsmWrite(tostr(o.ref^.offset))
  402. else
  403. if not(assigned(o.ref^.symbol)) then
  404. writer.AsmWrite('0');
  405. end;
  406. end;
  407. else
  408. internalerror(2005060510);
  409. end;
  410. end;
  411. procedure tx86IntelAssembler.WriteOper_jmp(const o:toper;s : topsize);
  412. begin
  413. case o.typ of
  414. top_reg :
  415. writer.AsmWrite(masm_regname(o.reg));
  416. top_const :
  417. writer.AsmWrite(tostr(o.val));
  418. top_ref :
  419. { what about lcall or ljmp ??? }
  420. begin
  421. if o.ref^.refaddr=addr_no then
  422. begin
  423. if (asminfo^.id <> as_i386_tasm) then
  424. begin
  425. if s=S_FAR then
  426. writer.AsmWrite('far ptr ')
  427. else
  428. {$ifdef x86_64}
  429. writer.AsmWrite('qword ptr ');
  430. {$else x86_64}
  431. writer.AsmWrite('dword ptr ');
  432. {$endif x86_64}
  433. end;
  434. WriteReference(o.ref^);
  435. end
  436. else
  437. begin
  438. writer.AsmWrite(o.ref^.symbol.name);
  439. if o.ref^.offset>0 then
  440. writer.AsmWrite('+'+tostr(o.ref^.offset))
  441. else
  442. if o.ref^.offset<0 then
  443. writer.AsmWrite(tostr(o.ref^.offset));
  444. end;
  445. end;
  446. else
  447. internalerror(2005060511);
  448. end;
  449. end;
  450. const
  451. ait_const2str : array[aitconst_128bit..aitconst_64bit_unaligned] of string[20]=(
  452. #9''#9,#9'DQ'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  453. #9'FIXMESLEB',#9'FIXEMEULEB',
  454. #9'DD RVA'#9,#9'DD SECREL32'#9,
  455. #9'FIXME',#9'FIXME',#9'FIXME',#9'FIXME',
  456. #9'DW'#9,#9'DD'#9,#9'DQ'#9
  457. );
  458. Function PadTabs(const p:string;addch:char):string;
  459. var
  460. s : string;
  461. i : longint;
  462. begin
  463. i:=length(p);
  464. if addch<>#0 then
  465. begin
  466. inc(i);
  467. s:=p+addch;
  468. end
  469. else
  470. s:=p;
  471. if i<8 then
  472. PadTabs:=s+#9#9
  473. else
  474. PadTabs:=s+#9;
  475. end;
  476. procedure tx86IntelAssembler.WriteTree(p:TAsmList);
  477. var
  478. s,
  479. prefix,
  480. suffix : string;
  481. hp,nhp : tai;
  482. cpu: tcputype;
  483. counter,
  484. lines, tokens,
  485. InlineLevel : longint;
  486. i,j,l : longint;
  487. consttype : taiconst_type;
  488. do_line,DoNotSplitLine,
  489. quoted : boolean;
  490. fixed_opcode: TAsmOp;
  491. begin
  492. if not assigned(p) then
  493. exit;
  494. { lineinfo is only needed for al_procedures (PFV) }
  495. do_line:=((cs_asm_source in current_settings.globalswitches) or
  496. (cs_lineinfo in current_settings.moduleswitches))
  497. and (p=current_asmdata.asmlists[al_procedures]);
  498. InlineLevel:=0;
  499. DoNotSplitLine:=false;
  500. hp:=tai(p.first);
  501. while assigned(hp) do
  502. begin
  503. prefetch(pointer(hp.next)^);
  504. if not(hp.typ in SkipLineInfo) then
  505. begin
  506. current_filepos:=tailineinfo(hp).fileinfo;
  507. { no line info for inlined code }
  508. if do_line and (inlinelevel=0) and not DoNotSplitLine then
  509. WriteSourceLine(hp as tailineinfo);
  510. end;
  511. DoNotSplitLine:=false;
  512. case hp.typ of
  513. ait_comment :
  514. Begin
  515. writer.AsmWrite(asminfo^.comment);
  516. writer.AsmWritePChar(tai_comment(hp).str);
  517. writer.AsmLn;
  518. End;
  519. ait_regalloc :
  520. begin
  521. if (cs_asm_regalloc in current_settings.globalswitches) then
  522. writer.AsmWriteLn(asminfo^.comment+'Register '+masm_regname(tai_regalloc(hp).reg)+
  523. regallocstr[tai_regalloc(hp).ratype]);
  524. end;
  525. ait_tempalloc :
  526. begin
  527. if (cs_asm_tempalloc in current_settings.globalswitches) then
  528. WriteTempalloc(tai_tempalloc(hp));
  529. end;
  530. ait_section :
  531. begin
  532. if tai_section(hp).sectype<>sec_none then
  533. begin
  534. if asminfo^.id=as_x86_64_masm then
  535. begin
  536. if LasTSecType<>sec_none then
  537. writer.AsmWriteLn(secnamesml64[LasTSecType]+#9#9'ENDS');
  538. writer.AsmLn;
  539. writer.AsmWriteLn(secnamesml64[tai_section(hp).sectype]+#9+'SEGMENT')
  540. end
  541. else
  542. begin
  543. if LasTSecType<>sec_none then
  544. writer.AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  545. writer.AsmLn;
  546. if (asminfo^.id=as_i386_wasm) then
  547. s:='DWORD'
  548. else
  549. s:=alignstr(tai_section(hp).secalign);
  550. writer.AsmWriteLn('_'+secnames[tai_section(hp).sectype]+#9#9+
  551. 'SEGMENT'#9+s+' PUBLIC USE32 '''+
  552. secnames[tai_section(hp).sectype]+'''');
  553. end;
  554. end;
  555. LasTSecType:=tai_section(hp).sectype;
  556. end;
  557. ait_align :
  558. begin
  559. { CAUSES PROBLEMS WITH THE SEGMENT DEFINITION }
  560. { SEGMENT DEFINITION SHOULD MATCH TYPE OF ALIGN }
  561. { HERE UNDER TASM! }
  562. if tai_align_abstract(hp).aligntype>1 then
  563. writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align_abstract(hp).aligntype));
  564. end;
  565. ait_datablock :
  566. begin
  567. if tai_datablock(hp).is_global then
  568. writer.AsmWriteLn(#9'PUBLIC'#9+tai_datablock(hp).sym.name);
  569. writer.AsmWriteLn(PadTabs(tai_datablock(hp).sym.name,#0)+'DB'#9+tostr(tai_datablock(hp).size)+' DUP(?)');
  570. end;
  571. ait_const:
  572. begin
  573. consttype:=tai_const(hp).consttype;
  574. case consttype of
  575. aitconst_uleb128bit,
  576. aitconst_sleb128bit,
  577. aitconst_128bit,
  578. aitconst_64bit,
  579. aitconst_32bit,
  580. aitconst_16bit,
  581. aitconst_8bit,
  582. aitconst_16bit_unaligned,
  583. aitconst_32bit_unaligned,
  584. aitconst_64bit_unaligned,
  585. aitconst_rva_symbol,
  586. aitconst_secrel32_symbol :
  587. begin
  588. writer.AsmWrite(ait_const2str[consttype]);
  589. l:=0;
  590. tokens:=1;
  591. repeat
  592. if assigned(tai_const(hp).sym) then
  593. begin
  594. if assigned(tai_const(hp).endsym) then
  595. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  596. else
  597. s:=tai_const(hp).sym.name;
  598. if tai_const(hp).value<>0 then
  599. s:=s+tostr_with_plus(tai_const(hp).value);
  600. end
  601. else
  602. s:=tostr(tai_const(hp).value);
  603. writer.AsmWrite(s);
  604. inc(l,length(s));
  605. inc(tokens);
  606. if (l>line_length) or
  607. (tokens>max_tokens) or
  608. (hp.next=nil) or
  609. (tai(hp.next).typ<>ait_const) or
  610. (tai_const(hp.next).consttype<>consttype) then
  611. break;
  612. hp:=tai(hp.next);
  613. writer.AsmWrite(',');
  614. until false;
  615. { Substract section start for secrel32 type }
  616. if consttype=aitconst_secrel32_symbol then
  617. writer.AsmWrite(' - $$');
  618. writer.AsmLn;
  619. end;
  620. else
  621. internalerror(200704253);
  622. end;
  623. end;
  624. ait_realconst:
  625. begin
  626. case tai_realconst(hp).realtyp of
  627. aitrealconst_s32bit:
  628. begin
  629. if (asminfo^.id = as_i386_wasm) and (IsInfinite(tai_realconst(hp).value.s32val)) then
  630. begin
  631. { Watcom Wasm does not handle Infinity }
  632. if Sign(tai_realconst(hp).value.s32val)=PositiveValue then
  633. writer.AsmWriteln(#9#9'DB'#9'0,0,80h,7Fh')
  634. else
  635. writer.AsmWriteln(#9#9'DW'#9'0,0,80h,FFh');
  636. end
  637. else if (asminfo^.id = as_i386_wasm) and (IsNan(tai_realconst(hp).value.s32val)) then
  638. writer.AsmWriteln(#9#9'DB'#9'1,0,80h,7Fh')
  639. else
  640. writer.AsmWriteLn(#9#9'DD'#9+single2str(tai_realconst(hp).value.s32val));
  641. end;
  642. aitrealconst_s64bit:
  643. begin
  644. if (asminfo^.id = as_i386_wasm) and (IsInfinite(tai_realconst(hp).value.s64val)) then
  645. begin
  646. { Watcom Wasm does not handle Infinity }
  647. if Sign(tai_realconst(hp).value.s64val)=PositiveValue then
  648. writer.AsmWriteln(#9#9'DW'#9'0,0,0,7FF0h')
  649. else
  650. writer.AsmWriteln(#9#9'DW'#9'0,0,0,FFF0h');
  651. end
  652. else if (asminfo^.id = as_i386_wasm) and (IsNan(tai_realconst(hp).value.s64val)) then
  653. writer.AsmWriteln(#9#9'DW'#9'0,0,0,0,7FF8h')
  654. else
  655. writer.AsmWriteLn(#9#9'DQ'#9+double2str(tai_realconst(hp).value.s64val));
  656. end;
  657. aitrealconst_s80bit:
  658. if (asminfo^.id = as_i386_wasm) and (IsInfinite(tai_realconst(hp).value.s80val)) then
  659. begin
  660. { Watcom Wasm does not handle Infinity }
  661. if Sign(tai_realconst(hp).value.s80val)=PositiveValue then
  662. writer.AsmWriteln(#9#9'DW'#9'0,0,0,8000h,7FFFh')
  663. else
  664. writer.AsmWriteln(#9#9'DW'#9'0,0,0,8000h,FFFFh');
  665. end
  666. else if (asminfo^.id = as_i386_wasm) and (IsNan(tai_realconst(hp).value.s80val)) then
  667. writer.AsmWriteln(#9#9'DW'#9'0,0,0,C000h,7FFFh')
  668. else
  669. writer.AsmWriteLn(#9#9'DT'#9+extended2str(tai_realconst(hp).value.s80val));
  670. aitrealconst_s64comp:
  671. writer.AsmWriteLn(#9#9'DQ'#9+extended2str(tai_realconst(hp).value.s64compval));
  672. else
  673. internalerror(2014050604);
  674. end;
  675. end;
  676. ait_string :
  677. begin
  678. counter := 0;
  679. lines := tai_string(hp).len div line_length;
  680. { separate lines in different parts }
  681. if tai_string(hp).len > 0 then
  682. Begin
  683. for j := 0 to lines-1 do
  684. begin
  685. writer.AsmWrite(#9#9'DB'#9);
  686. quoted:=false;
  687. for i:=counter to counter+line_length-1 do
  688. begin
  689. { it is an ascii character. }
  690. if (ord(tai_string(hp).str[i])>31) and
  691. (ord(tai_string(hp).str[i])<128) and
  692. (tai_string(hp).str[i]<>'"') then
  693. begin
  694. if not(quoted) then
  695. begin
  696. if i>counter then
  697. writer.AsmWrite(',');
  698. writer.AsmWrite('"');
  699. end;
  700. writer.AsmWrite(tai_string(hp).str[i]);
  701. quoted:=true;
  702. end { if > 31 and < 128 and ord('"') }
  703. else
  704. begin
  705. if quoted then
  706. writer.AsmWrite('"');
  707. if i>counter then
  708. writer.AsmWrite(',');
  709. quoted:=false;
  710. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  711. end;
  712. end; { end for i:=0 to... }
  713. if quoted then writer.AsmWrite('"');
  714. writer.AsmWrite(target_info.newline);
  715. counter := counter+line_length;
  716. end; { end for j:=0 ... }
  717. { do last line of lines }
  718. if counter<tai_string(hp).len then
  719. writer.AsmWrite(#9#9'DB'#9);
  720. quoted:=false;
  721. for i:=counter to tai_string(hp).len-1 do
  722. begin
  723. { it is an ascii character. }
  724. if (ord(tai_string(hp).str[i])>31) and
  725. (ord(tai_string(hp).str[i])<128) and
  726. (tai_string(hp).str[i]<>'"') then
  727. begin
  728. if not(quoted) then
  729. begin
  730. if i>counter then
  731. writer.AsmWrite(',');
  732. writer.AsmWrite('"');
  733. end;
  734. writer.AsmWrite(tai_string(hp).str[i]);
  735. quoted:=true;
  736. end { if > 31 and < 128 and " }
  737. else
  738. begin
  739. if quoted then
  740. writer.AsmWrite('"');
  741. if i>counter then
  742. writer.AsmWrite(',');
  743. quoted:=false;
  744. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  745. end;
  746. end; { end for i:=0 to... }
  747. if quoted then
  748. writer.AsmWrite('"');
  749. end;
  750. writer.AsmLn;
  751. end;
  752. ait_label :
  753. begin
  754. if tai_label(hp).labsym.is_used then
  755. begin
  756. writer.AsmWrite(tai_label(hp).labsym.name);
  757. if assigned(hp.next) and not(tai(hp.next).typ in
  758. [ait_const,ait_realconst,ait_string]) then
  759. writer.AsmWriteLn(':')
  760. else
  761. DoNotSplitLine:=true;
  762. end;
  763. end;
  764. ait_symbol :
  765. begin
  766. if tai_symbol(hp).has_value then
  767. internalerror(2009090802);
  768. { wasm is case insensitive, we nned to use only uppercase version
  769. if both a lowercase and an uppercase version are provided }
  770. if (asminfo^.id = as_i386_wasm) then
  771. begin
  772. nhp:=tai(hp.next);
  773. while assigned(nhp) and (nhp.typ in [ait_function_name,ait_force_line]) do
  774. nhp:=tai(nhp.next);
  775. if assigned(nhp) and (tai(nhp).typ=ait_symbol) and
  776. (lower(tai_symbol(nhp).sym.name)=tai_symbol(hp).sym.name) then
  777. begin
  778. writer.AsmWriteln(asminfo^.comment+' '+tai_symbol(hp).sym.name+' removed');
  779. hp:=tai(nhp);
  780. end;
  781. end;
  782. if tai_symbol(hp).is_global then
  783. writer.AsmWriteLn(#9'PUBLIC'#9+tai_symbol(hp).sym.name);
  784. writer.AsmWrite(tai_symbol(hp).sym.name);
  785. if assigned(hp.next) and not(tai(hp.next).typ in
  786. [ait_const,ait_realconst,ait_string]) then
  787. writer.AsmWriteLn(':');
  788. end;
  789. ait_symbol_end :
  790. begin
  791. end;
  792. ait_instruction :
  793. begin
  794. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  795. taicpu(hp).SetOperandOrder(op_intel);
  796. { Reset }
  797. suffix:='';
  798. prefix:= '';
  799. { We need to explicitely set
  800. word prefix to get selectors
  801. to be pushed in 2 bytes PM }
  802. if (taicpu(hp).opsize=S_W) and
  803. (
  804. (
  805. (fixed_opcode=A_PUSH) or
  806. (fixed_opcode=A_POP)
  807. ) and
  808. (taicpu(hp).oper[0]^.typ=top_reg) and
  809. is_segment_reg(taicpu(hp).oper[0]^.reg)
  810. ) then
  811. writer.AsmWriteln(#9#9'DB'#9'066h');
  812. { added prefix instructions, must be on same line as opcode }
  813. if (taicpu(hp).ops = 0) and
  814. ((fixed_opcode = A_REP) or
  815. (fixed_opcode = A_LOCK) or
  816. (fixed_opcode = A_REPE) or
  817. (fixed_opcode = A_REPNZ) or
  818. (fixed_opcode = A_REPZ) or
  819. (fixed_opcode = A_REPNE)) then
  820. Begin
  821. prefix:=std_op2str[fixed_opcode]+#9;
  822. { there can be a stab inbetween when the opcode was on
  823. a different line in the source code }
  824. repeat
  825. hp:=tai(hp.next);
  826. until (hp=nil) or (hp.typ=ait_instruction);
  827. { next instruction ... }
  828. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  829. taicpu(hp).SetOperandOrder(op_intel);
  830. { this is theorically impossible... }
  831. if hp=nil then
  832. begin
  833. writer.AsmWriteLn(#9#9+prefix);
  834. break;
  835. end;
  836. { nasm prefers prefix on a line alone
  837. writer.AsmWriteln(#9#9+prefix); but not masm PM
  838. prefix:=''; }
  839. if asminfo^.id in [as_i386_nasmcoff,as_i386_nasmwin32,as_i386_nasmwdosx,
  840. as_i386_nasmelf,as_i386_nasmobj,as_i386_nasmbeos,as_i386_nasmhaiku] then
  841. begin
  842. writer.AsmWriteln(prefix);
  843. prefix:='';
  844. end;
  845. end
  846. else
  847. prefix:= '';
  848. if (asminfo^.id = as_i386_wasm) and
  849. (taicpu(hp).opsize=S_W) and
  850. (fixed_opcode=A_PUSH) and
  851. (taicpu(hp).oper[0]^.typ=top_const) then
  852. begin
  853. writer.AsmWriteln(#9#9'DB 66h,68h ; pushw imm16');
  854. writer.AsmWrite(#9#9'DW');
  855. end
  856. else if (asminfo^.id=as_x86_64_masm) and
  857. (fixed_opcode=A_MOVQ) then
  858. writer.AsmWrite(#9#9'mov')
  859. {$ifdef I386}
  860. else if (asminfo^.id = as_i386_wasm) and ((fixed_opcode=A_RETD)
  861. or (fixed_opcode=A_RETND) or (fixed_opcode=A_RETFD)) then
  862. begin
  863. { no 'd' suffix for Watcom assembler }
  864. case fixed_opcode of
  865. A_RETD:
  866. writer.AsmWrite(#9#9'ret');
  867. A_RETND:
  868. writer.AsmWrite(#9#9'retn');
  869. A_RETFD:
  870. writer.AsmWrite(#9#9'retf');
  871. else
  872. internalerror(2019050907);
  873. end
  874. end
  875. {$endif I386}
  876. else
  877. writer.AsmWrite(#9#9+prefix+std_op2str[fixed_opcode]+cond2str[taicpu(hp).condition]+suffix);
  878. if taicpu(hp).ops<>0 then
  879. begin
  880. if is_calljmp(fixed_opcode) then
  881. begin
  882. writer.AsmWrite(#9);
  883. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opsize);
  884. end
  885. else
  886. begin
  887. for i:=0to taicpu(hp).ops-1 do
  888. begin
  889. if i=0 then
  890. writer.AsmWrite(#9)
  891. else
  892. writer.AsmWrite(',');
  893. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,fixed_opcode,(i=2));
  894. end;
  895. end;
  896. end;
  897. writer.AsmLn;
  898. end;
  899. ait_stab,
  900. ait_force_line,
  901. ait_function_name : ;
  902. ait_cutobject :
  903. begin
  904. { only reset buffer if nothing has changed }
  905. if not writer.ClearIfEmpty then
  906. begin
  907. if LasTSecType<>sec_none then
  908. writer.AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  909. writer.AsmLn;
  910. writer.AsmWriteLn(#9'END');
  911. writer.AsmClose;
  912. DoAssemble;
  913. writer.AsmCreate(tai_cutobject(hp).place);
  914. end;
  915. { avoid empty files }
  916. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  917. begin
  918. if tai(hp.next).typ=ait_section then
  919. lasTSecType:=tai_section(hp.next).sectype;
  920. hp:=tai(hp.next);
  921. end;
  922. if (asminfo^.id = as_i386_wasm) then
  923. begin
  924. writer.AsmWriteLn(#9'.686p');
  925. writer.AsmWriteLn(#9'.xmm');
  926. end
  927. else
  928. writer.AsmWriteLn(#9'.386p');
  929. {$ifdef i8086}
  930. writer.AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  931. writer.AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  932. {$endif i8086}
  933. { I was told that this isn't necesarry because }
  934. { the labels generated by FPC are unique (FK) }
  935. { writer.AsmWriteLn(#9'LOCALS '+asminfo^.labelprefix); }
  936. { TODO: PARA is incorrect, must use actual section align }
  937. if lasTSectype<>sec_none then
  938. writer.AsmWriteLn('_'+secnames[lasTSectype]+#9#9+
  939. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  940. secnames[lasTSectype]+'''');
  941. writer.MarkEmpty;
  942. end;
  943. ait_marker :
  944. begin
  945. if tai_marker(hp).kind=mark_NoLineInfoStart then
  946. inc(InlineLevel)
  947. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  948. dec(InlineLevel);
  949. end;
  950. ait_directive :
  951. begin
  952. case tai_directive(hp).directive of
  953. asd_nasm_import :
  954. begin
  955. writer.AsmWrite('import ');
  956. writer.AsmWrite(tai_directive(hp).name);
  957. writer.AsmLn;
  958. end;
  959. asd_extern :
  960. begin
  961. writer.AsmWrite('EXTRN ');
  962. writer.AsmWrite(tai_directive(hp).name);
  963. writer.AsmLn;
  964. end;
  965. asd_cpu :
  966. begin
  967. if (asminfo^.id = as_i386_wasm) then
  968. begin
  969. {writer.AsmWrite('.');}
  970. for cpu:=low(tcputype) to high(tcputype) do
  971. begin
  972. if tai_directive(hp).name=CPUTypeStr[CPU] then
  973. begin
  974. { writer.AsmWriteLn(wasm_cpu_name[cpu]); }
  975. break;
  976. end;
  977. end;
  978. end
  979. else
  980. begin
  981. { TODO: implement this properly for TASM/MASM/WASM (.686p, etc.) }
  982. writer.AsmWrite(asminfo^.comment+' CPU ');
  983. writer.AsmWrite(tai_directive(hp).name);
  984. writer.AsmLn;
  985. end;
  986. end
  987. else
  988. internalerror(200509192);
  989. end;
  990. end;
  991. ait_seh_directive :
  992. { Ignore for now };
  993. else
  994. internalerror(10000);
  995. end;
  996. hp:=tai(hp.next);
  997. end;
  998. end;
  999. procedure tx86intelassembler.WriteExternals;
  1000. var
  1001. sym : TAsmSymbol;
  1002. i : longint;
  1003. begin
  1004. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  1005. begin
  1006. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  1007. if sym.bind in [AB_EXTERNAL,AB_EXTERNAL_INDIRECT] then
  1008. begin
  1009. case asminfo^.id of
  1010. as_i386_masm,
  1011. as_i386_wasm :
  1012. writer.AsmWriteln(#9'EXTRN'#9+sym.name+': NEAR');
  1013. as_x86_64_masm :
  1014. writer.AsmWriteln(#9'EXTRN'#9+sym.name+': PROC');
  1015. else
  1016. writer.AsmWriteln(#9'EXTRN'#9+sym.name);
  1017. end;
  1018. end;
  1019. end;
  1020. end;
  1021. function tx86intelassembler.DoAssemble : boolean;
  1022. var
  1023. masmobjfn : string;
  1024. begin
  1025. DoAssemble:=Inherited DoAssemble;
  1026. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  1027. if (asminfo^.id in [as_i386_masm,as_i386_wasm]) then
  1028. begin
  1029. masmobjfn:=ChangeFileExt(objfilename,'.obj');
  1030. if not(cs_asm_extern in current_settings.globalswitches) then
  1031. begin
  1032. if Not FileExists(objfilename) and
  1033. FileExists(masmobjfn) then
  1034. RenameFile(masmobjfn,objfilename);
  1035. end
  1036. else
  1037. AsmRes.AddAsmCommand('mv',masmobjfn+' '+objfilename,objfilename);
  1038. end;
  1039. end;
  1040. procedure tx86IntelAssembler.WriteAsmList;
  1041. var
  1042. hal : tasmlisttype;
  1043. begin
  1044. {$ifdef EXTDEBUG}
  1045. if current_module.mainsource<>'' then
  1046. comment(v_info,'Start writing intel-styled assembler output for '+current_module.mainsource);
  1047. {$endif}
  1048. if asminfo^.id<>as_x86_64_masm then
  1049. begin
  1050. if (asminfo^.id = as_i386_wasm) then
  1051. begin
  1052. writer.AsmWriteLn(#9'.686p');
  1053. writer.AsmWriteLn(#9'.xmm');
  1054. end
  1055. else
  1056. writer.AsmWriteLn(#9'.386p');
  1057. { masm 6.11 does not seem to like LOCALS PM }
  1058. if (asminfo^.id = as_i386_tasm) then
  1059. begin
  1060. writer.AsmWriteLn(#9'LOCALS '+asminfo^.labelprefix);
  1061. end;
  1062. {$ifdef i8086}
  1063. writer.AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  1064. writer.AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  1065. {$endif i8086}
  1066. writer.AsmLn;
  1067. end;
  1068. WriteExternals;
  1069. for hal:=low(TasmlistType) to high(TasmlistType) do
  1070. begin
  1071. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  1072. writetree(current_asmdata.asmlists[hal]);
  1073. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  1074. end;
  1075. { better do this at end of WriteTree, but then there comes a trouble with
  1076. al_const which does not have leading ait_section and thus goes out of segment }
  1077. if LastSecType <> sec_none then
  1078. begin
  1079. if asminfo^.id=as_x86_64_masm then
  1080. writer.AsmWriteLn(secnamesml64[LasTSecType]+#9#9'ENDS')
  1081. else
  1082. writer.AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  1083. end;
  1084. LastSecType := sec_none;
  1085. writer.AsmWriteLn(#9'END');
  1086. writer.AsmLn;
  1087. {$ifdef EXTDEBUG}
  1088. if current_module.mainsource<>'' then
  1089. comment(v_info,'Done writing intel-styled assembler output for '+current_module.mainsource);
  1090. {$endif EXTDEBUG}
  1091. end;
  1092. {*****************************************************************************
  1093. Initialize
  1094. *****************************************************************************}
  1095. const
  1096. {$ifdef i386}
  1097. as_i386_tasm_info : tasminfo =
  1098. (
  1099. id : as_i386_tasm;
  1100. idtxt : 'TASM';
  1101. asmbin : 'tasm';
  1102. asmcmd : '/m2 /ml $EXTRAOPT $ASM $OBJ';
  1103. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  1104. flags : [af_needar,af_labelprefix_only_inside_procedure];
  1105. labelprefix : '@@';
  1106. comment : '; ';
  1107. dollarsign: '$';
  1108. );
  1109. as_i386_masm_info : tasminfo =
  1110. (
  1111. id : as_i386_masm;
  1112. idtxt : 'MASM';
  1113. asmbin : 'masm';
  1114. asmcmd : '/c /Cp $EXTRAOPT $ASM /Fo$OBJ';
  1115. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  1116. flags : [af_needar];
  1117. labelprefix : '@@';
  1118. comment : '; ';
  1119. dollarsign: '$';
  1120. );
  1121. as_i386_wasm_info : tasminfo =
  1122. (
  1123. id : as_i386_wasm;
  1124. idtxt : 'WASM';
  1125. asmbin : 'wasm';
  1126. asmcmd : '$ASM $EXTRAOPT -6s -fp6 -ms -zq -Fo=$OBJ';
  1127. supported_targets : [system_i386_watcom];
  1128. flags : [af_needar];
  1129. labelprefix : '@@';
  1130. comment : '; ';
  1131. dollarsign: '$';
  1132. );
  1133. {$endif i386}
  1134. {$ifdef x86_64}
  1135. as_x86_64_masm_info : tasminfo =
  1136. (
  1137. id : as_x86_64_masm;
  1138. idtxt : 'MASM';
  1139. asmbin : 'ml64';
  1140. asmcmd : '/c /Cp $EXTRAOPT $ASM /Fo$OBJ';
  1141. supported_targets : [system_x86_64_win64];
  1142. flags : [af_needar];
  1143. labelprefix : '@@';
  1144. comment : '; ';
  1145. dollarsign: '$';
  1146. );
  1147. {$endif x86_64}
  1148. initialization
  1149. {$ifdef x86_64}
  1150. RegisterAssembler(as_x86_64_masm_info,tx86IntelAssembler);
  1151. {$endif x86_64}
  1152. {$ifdef i386}
  1153. RegisterAssembler(as_i386_tasm_info,tx86IntelAssembler);
  1154. RegisterAssembler(as_i386_masm_info,tx86IntelAssembler);
  1155. RegisterAssembler(as_i386_wasm_info,tx86IntelAssembler);
  1156. {$endif i386}
  1157. end.