agx86int.pas 42 KB

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