agx86int.pas 33 KB

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