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