agx86int.pas 41 KB

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