agx86int.pas 39 KB

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