agx86int.pas 32 KB

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