agx86int.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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. { next instruction ... }
  708. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  709. taicpu(hp).SetOperandOrder(op_intel);
  710. { this is theorically impossible... }
  711. if hp=nil then
  712. begin
  713. AsmWriteLn(#9#9+prefix);
  714. break;
  715. end;
  716. { nasm prefers prefix on a line alone
  717. AsmWriteln(#9#9+prefix); but not masm PM
  718. prefix:=''; }
  719. if target_asm.id in [as_i386_nasmcoff,as_i386_nasmwin32,as_i386_nasmwdosx,
  720. as_i386_nasmelf,as_i386_nasmobj,as_i386_nasmbeos,as_i386_nasmhaiku] then
  721. begin
  722. AsmWriteln(prefix);
  723. prefix:='';
  724. end;
  725. end
  726. else
  727. prefix:= '';
  728. if (target_asm.id = as_i386_wasm) and
  729. (taicpu(hp).opsize=S_W) and
  730. (fixed_opcode=A_PUSH) and
  731. (taicpu(hp).oper[0]^.typ=top_const) then
  732. begin
  733. AsmWriteln(#9#9'DB 66h,68h ; pushw imm16');
  734. AsmWrite(#9#9'DW');
  735. end
  736. else if (target_asm.id=as_x86_64_masm) and
  737. (fixed_opcode=A_MOVQ) then
  738. AsmWrite(#9#9'mov')
  739. else
  740. AsmWrite(#9#9+prefix+std_op2str[fixed_opcode]+cond2str[taicpu(hp).condition]+suffix);
  741. if taicpu(hp).ops<>0 then
  742. begin
  743. if is_calljmp(fixed_opcode) then
  744. begin
  745. AsmWrite(#9);
  746. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opsize);
  747. end
  748. else
  749. begin
  750. for i:=0to taicpu(hp).ops-1 do
  751. begin
  752. if i=0 then
  753. AsmWrite(#9)
  754. else
  755. AsmWrite(',');
  756. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,fixed_opcode,(i=2));
  757. end;
  758. end;
  759. end;
  760. AsmLn;
  761. end;
  762. ait_stab,
  763. ait_force_line,
  764. ait_function_name : ;
  765. ait_cutobject :
  766. begin
  767. { only reset buffer if nothing has changed }
  768. if AsmSize=AsmStartSize then
  769. AsmClear
  770. else
  771. begin
  772. if LasTSecType<>sec_none then
  773. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  774. AsmLn;
  775. AsmWriteLn(#9'END');
  776. AsmClose;
  777. DoAssemble;
  778. AsmCreate(tai_cutobject(hp).place);
  779. end;
  780. { avoid empty files }
  781. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  782. begin
  783. if tai(hp.next).typ=ait_section then
  784. lasTSecType:=tai_section(hp.next).sectype;
  785. hp:=tai(hp.next);
  786. end;
  787. AsmWriteLn(#9'.386p');
  788. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  789. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  790. { I was told that this isn't necesarry because }
  791. { the labels generated by FPC are unique (FK) }
  792. { AsmWriteLn(#9'LOCALS '+target_asm.labelprefix); }
  793. { TODO: PARA is incorrect, must use actual section align }
  794. if lasTSectype<>sec_none then
  795. AsmWriteLn('_'+secnames[lasTSectype]+#9#9+
  796. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  797. secnames[lasTSectype]+'''');
  798. AsmStartSize:=AsmSize;
  799. end;
  800. ait_marker :
  801. begin
  802. if tai_marker(hp).kind=mark_NoLineInfoStart then
  803. inc(InlineLevel)
  804. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  805. dec(InlineLevel);
  806. end;
  807. ait_directive :
  808. begin
  809. case tai_directive(hp).directive of
  810. asd_nasm_import :
  811. AsmWrite('import ');
  812. asd_extern :
  813. AsmWrite('EXTRN ');
  814. else
  815. internalerror(200509192);
  816. end;
  817. AsmWrite(tai_directive(hp).name);
  818. AsmLn;
  819. end;
  820. ait_seh_directive :
  821. { Ignore for now };
  822. else
  823. internalerror(10000);
  824. end;
  825. hp:=tai(hp.next);
  826. end;
  827. end;
  828. procedure tx86intelassembler.WriteExternals;
  829. var
  830. sym : TAsmSymbol;
  831. i : longint;
  832. begin
  833. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  834. begin
  835. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  836. if sym.bind=AB_EXTERNAL then
  837. begin
  838. case target_asm.id of
  839. as_i386_masm,
  840. as_i386_wasm :
  841. AsmWriteln(#9'EXTRN'#9+sym.name+': NEAR');
  842. as_x86_64_masm :
  843. AsmWriteln(#9'EXTRN'#9+sym.name+': PROC');
  844. else
  845. AsmWriteln(#9'EXTRN'#9+sym.name);
  846. end;
  847. end;
  848. end;
  849. end;
  850. function tx86intelassembler.DoAssemble : boolean;
  851. var
  852. masmobjfn : string;
  853. begin
  854. DoAssemble:=Inherited DoAssemble;
  855. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  856. if (target_asm.id in [as_i386_masm,as_i386_wasm]) then
  857. begin
  858. masmobjfn:=ChangeFileExt(objfilename,'.obj');
  859. if not(cs_asm_extern in current_settings.globalswitches) then
  860. begin
  861. if Not FileExists(objfilename) and
  862. FileExists(masmobjfn) then
  863. RenameFile(masmobjfn,objfilename);
  864. end
  865. else
  866. AsmRes.AddAsmCommand('mv',masmobjfn+' '+objfilename,objfilename);
  867. end;
  868. end;
  869. procedure tx86IntelAssembler.WriteAsmList;
  870. var
  871. hal : tasmlisttype;
  872. begin
  873. {$ifdef EXTDEBUG}
  874. if current_module.mainsource<>'' then
  875. comment(v_info,'Start writing intel-styled assembler output for '+current_module.mainsource);
  876. {$endif}
  877. if target_asm.id<>as_x86_64_masm then
  878. begin
  879. AsmWriteLn(#9'.386p');
  880. { masm 6.11 does not seem to like LOCALS PM }
  881. if (target_asm.id = as_i386_tasm) then
  882. begin
  883. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  884. end;
  885. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  886. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  887. AsmLn;
  888. end;
  889. WriteExternals;
  890. for hal:=low(TasmlistType) to high(TasmlistType) do
  891. begin
  892. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  893. writetree(current_asmdata.asmlists[hal]);
  894. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  895. end;
  896. { better do this at end of WriteTree, but then there comes a trouble with
  897. al_const which does not have leading ait_section and thus goes out of segment }
  898. if LastSecType <> sec_none then
  899. begin
  900. if target_asm.id=as_x86_64_masm then
  901. AsmWriteLn(secnamesml64[LasTSecType]+#9#9'ENDS')
  902. else
  903. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  904. end;
  905. LastSecType := sec_none;
  906. AsmWriteLn(#9'END');
  907. AsmLn;
  908. {$ifdef EXTDEBUG}
  909. if current_module.mainsource<>'' then
  910. comment(v_info,'Done writing intel-styled assembler output for '+current_module.mainsource);
  911. {$endif EXTDEBUG}
  912. end;
  913. {*****************************************************************************
  914. Initialize
  915. *****************************************************************************}
  916. const
  917. {$ifdef i386}
  918. as_i386_tasm_info : tasminfo =
  919. (
  920. id : as_i386_tasm;
  921. idtxt : 'TASM';
  922. asmbin : 'tasm';
  923. asmcmd : '/m2 /ml $EXTRAOPT $ASM $OBJ';
  924. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  925. flags : [af_needar,af_labelprefix_only_inside_procedure];
  926. labelprefix : '@@';
  927. comment : '; ';
  928. dollarsign: '$';
  929. );
  930. as_i386_masm_info : tasminfo =
  931. (
  932. id : as_i386_masm;
  933. idtxt : 'MASM';
  934. asmbin : 'masm';
  935. asmcmd : '/c /Cp $EXTRAOPT $ASM /Fo$OBJ';
  936. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  937. flags : [af_needar];
  938. labelprefix : '@@';
  939. comment : '; ';
  940. dollarsign: '$';
  941. );
  942. as_i386_wasm_info : tasminfo =
  943. (
  944. id : as_i386_wasm;
  945. idtxt : 'WASM';
  946. asmbin : 'wasm';
  947. asmcmd : '$ASM $EXTRAOPT -6s -fp6 -ms -zq -Fo=$OBJ';
  948. supported_targets : [system_i386_watcom];
  949. flags : [af_needar];
  950. labelprefix : '@@';
  951. comment : '; ';
  952. dollarsign: '$';
  953. );
  954. {$endif i386}
  955. {$ifdef x86_64}
  956. as_x86_64_masm_info : tasminfo =
  957. (
  958. id : as_x86_64_masm;
  959. idtxt : 'MASM';
  960. asmbin : 'ml64';
  961. asmcmd : '/c /Cp $EXTRAOPT $ASM /Fo$OBJ';
  962. supported_targets : [system_x86_64_win64];
  963. flags : [af_needar];
  964. labelprefix : '@@';
  965. comment : '; ';
  966. dollarsign: '$';
  967. );
  968. {$endif x86_64}
  969. initialization
  970. {$ifdef x86_64}
  971. RegisterAssembler(as_x86_64_masm_info,tx86IntelAssembler);
  972. {$endif x86_64}
  973. {$ifdef i386}
  974. RegisterAssembler(as_i386_tasm_info,tx86IntelAssembler);
  975. RegisterAssembler(as_i386_masm_info,tx86IntelAssembler);
  976. RegisterAssembler(as_i386_wasm_info,tx86IntelAssembler);
  977. {$endif i386}
  978. end.