agx86int.pas 42 KB

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