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_realconst:
  561. begin
  562. case tai_realconst(hp).realtyp of
  563. aitrealconst_s32bit:
  564. AsmWriteLn(#9#9'DD'#9+single2str(tai_realconst(hp).value.s32val));
  565. aitrealconst_s64bit:
  566. AsmWriteLn(#9#9'DQ'#9+double2str(tai_realconst(hp).value.s64val));
  567. aitrealconst_s80bit:
  568. AsmWriteLn(#9#9'DT'#9+extended2str(tai_realconst(hp).value.s80val));
  569. aitrealconst_s64comp:
  570. AsmWriteLn(#9#9'DQ'#9+extended2str(tai_realconst(hp).value.s64compval));
  571. else
  572. internalerror(2014050604);
  573. end;
  574. end;
  575. ait_string :
  576. begin
  577. counter := 0;
  578. lines := tai_string(hp).len div line_length;
  579. { separate lines in different parts }
  580. if tai_string(hp).len > 0 then
  581. Begin
  582. for j := 0 to lines-1 do
  583. begin
  584. AsmWrite(#9#9'DB'#9);
  585. quoted:=false;
  586. for i:=counter to counter+line_length-1 do
  587. begin
  588. { it is an ascii character. }
  589. if (ord(tai_string(hp).str[i])>31) and
  590. (ord(tai_string(hp).str[i])<128) and
  591. (tai_string(hp).str[i]<>'"') then
  592. begin
  593. if not(quoted) then
  594. begin
  595. if i>counter then
  596. AsmWrite(',');
  597. AsmWrite('"');
  598. end;
  599. AsmWrite(tai_string(hp).str[i]);
  600. quoted:=true;
  601. end { if > 31 and < 128 and ord('"') }
  602. else
  603. begin
  604. if quoted then
  605. AsmWrite('"');
  606. if i>counter then
  607. AsmWrite(',');
  608. quoted:=false;
  609. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  610. end;
  611. end; { end for i:=0 to... }
  612. if quoted then AsmWrite('"');
  613. AsmWrite(target_info.newline);
  614. counter := counter+line_length;
  615. end; { end for j:=0 ... }
  616. { do last line of lines }
  617. if counter<tai_string(hp).len then
  618. AsmWrite(#9#9'DB'#9);
  619. quoted:=false;
  620. for i:=counter to tai_string(hp).len-1 do
  621. begin
  622. { it is an ascii character. }
  623. if (ord(tai_string(hp).str[i])>31) and
  624. (ord(tai_string(hp).str[i])<128) and
  625. (tai_string(hp).str[i]<>'"') then
  626. begin
  627. if not(quoted) then
  628. begin
  629. if i>counter then
  630. AsmWrite(',');
  631. AsmWrite('"');
  632. end;
  633. AsmWrite(tai_string(hp).str[i]);
  634. quoted:=true;
  635. end { if > 31 and < 128 and " }
  636. else
  637. begin
  638. if quoted then
  639. AsmWrite('"');
  640. if i>counter then
  641. AsmWrite(',');
  642. quoted:=false;
  643. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  644. end;
  645. end; { end for i:=0 to... }
  646. if quoted then
  647. AsmWrite('"');
  648. end;
  649. AsmLn;
  650. end;
  651. ait_label :
  652. begin
  653. if tai_label(hp).labsym.is_used then
  654. begin
  655. AsmWrite(tai_label(hp).labsym.name);
  656. if assigned(hp.next) and not(tai(hp.next).typ in
  657. [ait_const,ait_realconst,ait_string]) then
  658. AsmWriteLn(':')
  659. else
  660. DoNotSplitLine:=true;
  661. end;
  662. end;
  663. ait_symbol :
  664. begin
  665. if tai_symbol(hp).has_value then
  666. internalerror(2009090802);
  667. if tai_symbol(hp).is_global then
  668. AsmWriteLn(#9'PUBLIC'#9+tai_symbol(hp).sym.name);
  669. AsmWrite(tai_symbol(hp).sym.name);
  670. if assigned(hp.next) and not(tai(hp.next).typ in
  671. [ait_const,ait_realconst,ait_string]) then
  672. AsmWriteLn(':')
  673. end;
  674. ait_symbol_end :
  675. begin
  676. end;
  677. ait_instruction :
  678. begin
  679. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  680. taicpu(hp).SetOperandOrder(op_intel);
  681. { Reset }
  682. suffix:='';
  683. prefix:= '';
  684. { We need to explicitely set
  685. word prefix to get selectors
  686. to be pushed in 2 bytes PM }
  687. if (taicpu(hp).opsize=S_W) and
  688. (
  689. (
  690. (fixed_opcode=A_PUSH) or
  691. (fixed_opcode=A_POP)
  692. ) and
  693. (taicpu(hp).oper[0]^.typ=top_reg) and
  694. is_segment_reg(taicpu(hp).oper[0]^.reg)
  695. ) then
  696. AsmWriteln(#9#9'DB'#9'066h');
  697. { added prefix instructions, must be on same line as opcode }
  698. if (taicpu(hp).ops = 0) and
  699. ((fixed_opcode = A_REP) or
  700. (fixed_opcode = A_LOCK) or
  701. (fixed_opcode = A_REPE) or
  702. (fixed_opcode = A_REPNZ) or
  703. (fixed_opcode = A_REPZ) or
  704. (fixed_opcode = A_REPNE)) then
  705. Begin
  706. prefix:=std_op2str[fixed_opcode]+#9;
  707. { there can be a stab inbetween when the opcode was on
  708. a different line in the source code }
  709. repeat
  710. hp:=tai(hp.next);
  711. until (hp=nil) or (hp.typ=ait_instruction);
  712. { this is theorically impossible... }
  713. if hp=nil then
  714. begin
  715. AsmWriteLn(#9#9+prefix);
  716. break;
  717. end;
  718. { nasm prefers prefix on a line alone
  719. AsmWriteln(#9#9+prefix); but not masm PM
  720. prefix:=''; }
  721. if target_asm.id in [as_i386_nasmcoff,as_i386_nasmwin32,as_i386_nasmwdosx,
  722. as_i386_nasmelf,as_i386_nasmobj,as_i386_nasmbeos,as_i386_nasmhaiku] then
  723. begin
  724. AsmWriteln(prefix);
  725. prefix:='';
  726. end;
  727. end
  728. else
  729. prefix:= '';
  730. if (target_asm.id = as_i386_wasm) and
  731. (taicpu(hp).opsize=S_W) and
  732. (fixed_opcode=A_PUSH) and
  733. (taicpu(hp).oper[0]^.typ=top_const) then
  734. begin
  735. AsmWriteln(#9#9'DB 66h,68h ; pushw imm16');
  736. AsmWrite(#9#9'DW');
  737. end
  738. else if (target_asm.id=as_x86_64_masm) and
  739. (fixed_opcode=A_MOVQ) then
  740. AsmWrite(#9#9'mov')
  741. else
  742. AsmWrite(#9#9+prefix+std_op2str[fixed_opcode]+cond2str[taicpu(hp).condition]+suffix);
  743. if taicpu(hp).ops<>0 then
  744. begin
  745. if is_calljmp(fixed_opcode) then
  746. begin
  747. AsmWrite(#9);
  748. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opsize);
  749. end
  750. else
  751. begin
  752. for i:=0to taicpu(hp).ops-1 do
  753. begin
  754. if i=0 then
  755. AsmWrite(#9)
  756. else
  757. AsmWrite(',');
  758. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,fixed_opcode,(i=2));
  759. end;
  760. end;
  761. end;
  762. AsmLn;
  763. end;
  764. ait_stab,
  765. ait_force_line,
  766. ait_function_name : ;
  767. ait_cutobject :
  768. begin
  769. { only reset buffer if nothing has changed }
  770. if AsmSize=AsmStartSize then
  771. AsmClear
  772. else
  773. begin
  774. if LasTSecType<>sec_none then
  775. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  776. AsmLn;
  777. AsmWriteLn(#9'END');
  778. AsmClose;
  779. DoAssemble;
  780. AsmCreate(tai_cutobject(hp).place);
  781. end;
  782. { avoid empty files }
  783. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  784. begin
  785. if tai(hp.next).typ=ait_section then
  786. lasTSecType:=tai_section(hp.next).sectype;
  787. hp:=tai(hp.next);
  788. end;
  789. AsmWriteLn(#9'.386p');
  790. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  791. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  792. { I was told that this isn't necesarry because }
  793. { the labels generated by FPC are unique (FK) }
  794. { AsmWriteLn(#9'LOCALS '+target_asm.labelprefix); }
  795. { TODO: PARA is incorrect, must use actual section align }
  796. if lasTSectype<>sec_none then
  797. AsmWriteLn('_'+secnames[lasTSectype]+#9#9+
  798. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  799. secnames[lasTSectype]+'''');
  800. AsmStartSize:=AsmSize;
  801. end;
  802. ait_marker :
  803. begin
  804. if tai_marker(hp).kind=mark_NoLineInfoStart then
  805. inc(InlineLevel)
  806. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  807. dec(InlineLevel);
  808. end;
  809. ait_directive :
  810. begin
  811. case tai_directive(hp).directive of
  812. asd_nasm_import :
  813. AsmWrite('import ');
  814. asd_extern :
  815. AsmWrite('EXTRN ');
  816. else
  817. internalerror(200509192);
  818. end;
  819. AsmWrite(tai_directive(hp).name);
  820. AsmLn;
  821. end;
  822. ait_seh_directive :
  823. { Ignore for now };
  824. else
  825. internalerror(10000);
  826. end;
  827. hp:=tai(hp.next);
  828. end;
  829. end;
  830. procedure tx86intelassembler.WriteExternals;
  831. var
  832. sym : TAsmSymbol;
  833. i : longint;
  834. begin
  835. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  836. begin
  837. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  838. if sym.bind=AB_EXTERNAL then
  839. begin
  840. case target_asm.id of
  841. as_i386_masm,
  842. as_i386_wasm :
  843. AsmWriteln(#9'EXTRN'#9+sym.name+': NEAR');
  844. as_x86_64_masm :
  845. AsmWriteln(#9'EXTRN'#9+sym.name+': PROC');
  846. else
  847. AsmWriteln(#9'EXTRN'#9+sym.name);
  848. end;
  849. end;
  850. end;
  851. end;
  852. function tx86intelassembler.DoAssemble : boolean;
  853. var
  854. masmobjfn : string;
  855. begin
  856. DoAssemble:=Inherited DoAssemble;
  857. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  858. if (target_asm.id in [as_i386_masm,as_i386_wasm]) then
  859. begin
  860. masmobjfn:=ChangeFileExt(objfilename,'.obj');
  861. if not(cs_asm_extern in current_settings.globalswitches) then
  862. begin
  863. if Not FileExists(objfilename) and
  864. FileExists(masmobjfn) then
  865. RenameFile(masmobjfn,objfilename);
  866. end
  867. else
  868. AsmRes.AddAsmCommand('mv',masmobjfn+' '+objfilename,objfilename);
  869. end;
  870. end;
  871. procedure tx86IntelAssembler.WriteAsmList;
  872. var
  873. hal : tasmlisttype;
  874. begin
  875. {$ifdef EXTDEBUG}
  876. if current_module.mainsource<>'' then
  877. comment(v_info,'Start writing intel-styled assembler output for '+current_module.mainsource);
  878. {$endif}
  879. if target_asm.id<>as_x86_64_masm then
  880. begin
  881. AsmWriteLn(#9'.386p');
  882. { masm 6.11 does not seem to like LOCALS PM }
  883. if (target_asm.id = as_i386_tasm) then
  884. begin
  885. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  886. end;
  887. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  888. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  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.