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