agx86int.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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. AsmLn;
  545. end;
  546. else
  547. internalerror(200704253);
  548. end;
  549. end;
  550. ait_real_32bit :
  551. AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  552. ait_real_64bit :
  553. AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  554. ait_real_80bit :
  555. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  556. ait_comp_64bit :
  557. AsmWriteLn(#9#9'DQ'#9+extended2str(tai_comp_64bit(hp).value));
  558. ait_string :
  559. begin
  560. counter := 0;
  561. lines := tai_string(hp).len div line_length;
  562. { separate lines in different parts }
  563. if tai_string(hp).len > 0 then
  564. Begin
  565. for j := 0 to lines-1 do
  566. begin
  567. AsmWrite(#9#9'DB'#9);
  568. quoted:=false;
  569. for i:=counter to counter+line_length-1 do
  570. begin
  571. { it is an ascii character. }
  572. if (ord(tai_string(hp).str[i])>31) and
  573. (ord(tai_string(hp).str[i])<128) and
  574. (tai_string(hp).str[i]<>'"') then
  575. begin
  576. if not(quoted) then
  577. begin
  578. if i>counter then
  579. AsmWrite(',');
  580. AsmWrite('"');
  581. end;
  582. AsmWrite(tai_string(hp).str[i]);
  583. quoted:=true;
  584. end { if > 31 and < 128 and ord('"') }
  585. else
  586. begin
  587. if quoted then
  588. AsmWrite('"');
  589. if i>counter then
  590. AsmWrite(',');
  591. quoted:=false;
  592. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  593. end;
  594. end; { end for i:=0 to... }
  595. if quoted then AsmWrite('"');
  596. AsmWrite(target_info.newline);
  597. counter := counter+line_length;
  598. end; { end for j:=0 ... }
  599. { do last line of lines }
  600. if counter<tai_string(hp).len then
  601. AsmWrite(#9#9'DB'#9);
  602. quoted:=false;
  603. for i:=counter to tai_string(hp).len-1 do
  604. begin
  605. { it is an ascii character. }
  606. if (ord(tai_string(hp).str[i])>31) and
  607. (ord(tai_string(hp).str[i])<128) and
  608. (tai_string(hp).str[i]<>'"') then
  609. begin
  610. if not(quoted) then
  611. begin
  612. if i>counter then
  613. AsmWrite(',');
  614. AsmWrite('"');
  615. end;
  616. AsmWrite(tai_string(hp).str[i]);
  617. quoted:=true;
  618. end { if > 31 and < 128 and " }
  619. else
  620. begin
  621. if quoted then
  622. AsmWrite('"');
  623. if i>counter then
  624. AsmWrite(',');
  625. quoted:=false;
  626. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  627. end;
  628. end; { end for i:=0 to... }
  629. if quoted then
  630. AsmWrite('"');
  631. end;
  632. AsmLn;
  633. end;
  634. ait_label :
  635. begin
  636. if tai_label(hp).labsym.is_used then
  637. begin
  638. AsmWrite(tai_label(hp).labsym.name);
  639. if assigned(hp.next) and not(tai(hp.next).typ in
  640. [ait_const,
  641. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  642. AsmWriteLn(':')
  643. else
  644. DoNotSplitLine:=true;
  645. end;
  646. end;
  647. ait_symbol :
  648. begin
  649. if tai_symbol(hp).has_value then
  650. internalerror(2009090802);
  651. if tai_symbol(hp).is_global then
  652. AsmWriteLn(#9'PUBLIC'#9+tai_symbol(hp).sym.name);
  653. AsmWrite(tai_symbol(hp).sym.name);
  654. if assigned(hp.next) and not(tai(hp.next).typ in
  655. [ait_const,
  656. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  657. AsmWriteLn(':')
  658. end;
  659. ait_symbol_end :
  660. begin
  661. end;
  662. ait_instruction :
  663. begin
  664. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  665. taicpu(hp).SetOperandOrder(op_intel);
  666. { Reset }
  667. suffix:='';
  668. prefix:= '';
  669. { We need to explicitely set
  670. word prefix to get selectors
  671. to be pushed in 2 bytes PM }
  672. if (taicpu(hp).opsize=S_W) and
  673. (
  674. (
  675. (fixed_opcode=A_PUSH) or
  676. (fixed_opcode=A_POP)
  677. ) and
  678. (taicpu(hp).oper[0]^.typ=top_reg) and
  679. is_segment_reg(taicpu(hp).oper[0]^.reg)
  680. ) then
  681. AsmWriteln(#9#9'DB'#9'066h');
  682. { added prefix instructions, must be on same line as opcode }
  683. if (taicpu(hp).ops = 0) and
  684. ((fixed_opcode = A_REP) or
  685. (fixed_opcode = A_LOCK) or
  686. (fixed_opcode = A_REPE) or
  687. (fixed_opcode = A_REPNZ) or
  688. (fixed_opcode = A_REPZ) or
  689. (fixed_opcode = A_REPNE)) then
  690. Begin
  691. prefix:=std_op2str[fixed_opcode]+#9;
  692. { there can be a stab inbetween when the opcode was on
  693. a different line in the source code }
  694. repeat
  695. hp:=tai(hp.next);
  696. until (hp=nil) or (hp.typ=ait_instruction);
  697. { this is theorically impossible... }
  698. if hp=nil then
  699. begin
  700. AsmWriteLn(#9#9+prefix);
  701. break;
  702. end;
  703. { nasm prefers prefix on a line alone
  704. AsmWriteln(#9#9+prefix); but not masm PM
  705. prefix:=''; }
  706. if target_asm.id in [as_i386_nasmcoff,as_i386_nasmwin32,as_i386_nasmwdosx,
  707. as_i386_nasmelf,as_i386_nasmobj,as_i386_nasmbeos,as_i386_nasmhaiku] then
  708. begin
  709. AsmWriteln(prefix);
  710. prefix:='';
  711. end;
  712. end
  713. else
  714. prefix:= '';
  715. if (target_asm.id = as_i386_wasm) and
  716. (taicpu(hp).opsize=S_W) and
  717. (fixed_opcode=A_PUSH) and
  718. (taicpu(hp).oper[0]^.typ=top_const) then
  719. begin
  720. AsmWriteln(#9#9'DB 66h,68h ; pushw imm16');
  721. AsmWrite(#9#9'DW');
  722. end
  723. else if (target_asm.id=as_x86_64_masm) and
  724. (fixed_opcode=A_MOVQ) then
  725. AsmWrite(#9#9'mov')
  726. else
  727. AsmWrite(#9#9+prefix+std_op2str[fixed_opcode]+cond2str[taicpu(hp).condition]+suffix);
  728. if taicpu(hp).ops<>0 then
  729. begin
  730. if is_calljmp(fixed_opcode) then
  731. begin
  732. AsmWrite(#9);
  733. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opsize);
  734. end
  735. else
  736. begin
  737. for i:=0to taicpu(hp).ops-1 do
  738. begin
  739. if i=0 then
  740. AsmWrite(#9)
  741. else
  742. AsmWrite(',');
  743. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,fixed_opcode,(i=2));
  744. end;
  745. end;
  746. end;
  747. AsmLn;
  748. end;
  749. ait_stab,
  750. ait_force_line,
  751. ait_function_name : ;
  752. ait_cutobject :
  753. begin
  754. { only reset buffer if nothing has changed }
  755. if AsmSize=AsmStartSize then
  756. AsmClear
  757. else
  758. begin
  759. if LasTSecType<>sec_none then
  760. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  761. AsmLn;
  762. AsmWriteLn(#9'END');
  763. AsmClose;
  764. DoAssemble;
  765. AsmCreate(tai_cutobject(hp).place);
  766. end;
  767. { avoid empty files }
  768. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  769. begin
  770. if tai(hp.next).typ=ait_section then
  771. lasTSecType:=tai_section(hp.next).sectype;
  772. hp:=tai(hp.next);
  773. end;
  774. AsmWriteLn(#9'.386p');
  775. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  776. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  777. { I was told that this isn't necesarry because }
  778. { the labels generated by FPC are unique (FK) }
  779. { AsmWriteLn(#9'LOCALS '+target_asm.labelprefix); }
  780. { TODO: PARA is incorrect, must use actual section align }
  781. if lasTSectype<>sec_none then
  782. AsmWriteLn('_'+secnames[lasTSectype]+#9#9+
  783. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  784. secnames[lasTSectype]+'''');
  785. AsmStartSize:=AsmSize;
  786. end;
  787. ait_marker :
  788. begin
  789. if tai_marker(hp).kind=mark_NoLineInfoStart then
  790. inc(InlineLevel)
  791. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  792. dec(InlineLevel);
  793. end;
  794. ait_directive :
  795. begin
  796. case tai_directive(hp).directive of
  797. asd_nasm_import :
  798. AsmWrite('import ');
  799. asd_extern :
  800. AsmWrite('EXTRN ');
  801. else
  802. internalerror(200509192);
  803. end;
  804. AsmWrite(tai_directive(hp).name);
  805. AsmLn;
  806. end;
  807. ait_seh_directive :
  808. { Ignore for now };
  809. else
  810. internalerror(10000);
  811. end;
  812. hp:=tai(hp.next);
  813. end;
  814. end;
  815. procedure tx86intelassembler.WriteExternals;
  816. var
  817. sym : TAsmSymbol;
  818. i : longint;
  819. begin
  820. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  821. begin
  822. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  823. if sym.bind=AB_EXTERNAL then
  824. begin
  825. case target_asm.id of
  826. as_i386_masm,
  827. as_i386_wasm :
  828. AsmWriteln(#9'EXTRN'#9+sym.name+': NEAR');
  829. as_x86_64_masm :
  830. AsmWriteln(#9'EXTRN'#9+sym.name+': PROC');
  831. else
  832. AsmWriteln(#9'EXTRN'#9+sym.name);
  833. end;
  834. end;
  835. end;
  836. end;
  837. function tx86intelassembler.DoAssemble : boolean;
  838. var
  839. masmobjfn : string;
  840. begin
  841. DoAssemble:=Inherited DoAssemble;
  842. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  843. if (target_asm.id in [as_i386_masm,as_i386_wasm]) then
  844. begin
  845. masmobjfn:=ChangeFileExt(objfilename,'.obj');
  846. if not(cs_asm_extern in current_settings.globalswitches) then
  847. begin
  848. if Not FileExists(objfilename) and
  849. FileExists(masmobjfn) then
  850. RenameFile(masmobjfn,objfilename);
  851. end
  852. else
  853. AsmRes.AddAsmCommand('mv',masmobjfn+' '+objfilename,objfilename);
  854. end;
  855. end;
  856. procedure tx86IntelAssembler.WriteAsmList;
  857. var
  858. hal : tasmlisttype;
  859. begin
  860. {$ifdef EXTDEBUG}
  861. if current_module.mainsource<>'' then
  862. comment(v_info,'Start writing intel-styled assembler output for '+current_module.mainsource);
  863. {$endif}
  864. if target_asm.id<>as_x86_64_masm then
  865. begin
  866. AsmWriteLn(#9'.386p');
  867. { masm 6.11 does not seem to like LOCALS PM }
  868. if (target_asm.id = as_i386_tasm) then
  869. begin
  870. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  871. end;
  872. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  873. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  874. AsmLn;
  875. end;
  876. WriteExternals;
  877. for hal:=low(TasmlistType) to high(TasmlistType) do
  878. begin
  879. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  880. writetree(current_asmdata.asmlists[hal]);
  881. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  882. end;
  883. { better do this at end of WriteTree, but then there comes a trouble with
  884. al_const which does not have leading ait_section and thus goes out of segment }
  885. if LastSecType <> sec_none then
  886. begin
  887. if target_asm.id=as_x86_64_masm then
  888. AsmWriteLn(secnamesml64[LasTSecType]+#9#9'ENDS')
  889. else
  890. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  891. end;
  892. LastSecType := sec_none;
  893. AsmWriteLn(#9'END');
  894. AsmLn;
  895. {$ifdef EXTDEBUG}
  896. if current_module.mainsource<>'' then
  897. comment(v_info,'Done writing intel-styled assembler output for '+current_module.mainsource);
  898. {$endif EXTDEBUG}
  899. end;
  900. {*****************************************************************************
  901. Initialize
  902. *****************************************************************************}
  903. const
  904. {$ifdef i386}
  905. as_i386_tasm_info : tasminfo =
  906. (
  907. id : as_i386_tasm;
  908. idtxt : 'TASM';
  909. asmbin : 'tasm';
  910. asmcmd : '/m2 /ml $ASM $OBJ';
  911. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  912. flags : [af_needar,af_labelprefix_only_inside_procedure];
  913. labelprefix : '@@';
  914. comment : '; ';
  915. dollarsign: '$';
  916. );
  917. as_i386_masm_info : tasminfo =
  918. (
  919. id : as_i386_masm;
  920. idtxt : 'MASM';
  921. asmbin : 'masm';
  922. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  923. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  924. flags : [af_needar];
  925. labelprefix : '@@';
  926. comment : '; ';
  927. dollarsign: '$';
  928. );
  929. as_i386_wasm_info : tasminfo =
  930. (
  931. id : as_i386_wasm;
  932. idtxt : 'WASM';
  933. asmbin : 'wasm';
  934. asmcmd : '$ASM -6s -fp6 -ms -zq -Fo=$OBJ';
  935. supported_targets : [system_i386_watcom];
  936. flags : [af_needar];
  937. labelprefix : '@@';
  938. comment : '; ';
  939. dollarsign: '$';
  940. );
  941. {$endif i386}
  942. {$ifdef x86_64}
  943. as_x86_64_masm_info : tasminfo =
  944. (
  945. id : as_x86_64_masm;
  946. idtxt : 'MASM';
  947. asmbin : 'ml64';
  948. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  949. supported_targets : [system_x86_64_win64];
  950. flags : [af_needar];
  951. labelprefix : '@@';
  952. comment : '; ';
  953. dollarsign: '$';
  954. );
  955. {$endif x86_64}
  956. initialization
  957. {$ifdef x86_64}
  958. RegisterAssembler(as_x86_64_masm_info,tx86IntelAssembler);
  959. {$endif x86_64}
  960. {$ifdef i386}
  961. RegisterAssembler(as_i386_tasm_info,tx86IntelAssembler);
  962. RegisterAssembler(as_i386_masm_info,tx86IntelAssembler);
  963. RegisterAssembler(as_i386_wasm_info,tx86IntelAssembler);
  964. {$endif i386}
  965. end.