agx86int.pas 42 KB

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