agx86int.pas 41 KB

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