agx86int.pas 33 KB

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