agx86int.pas 33 KB

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