agx86int.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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,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:TAAsmoutput);override;
  34. procedure WriteAsmList;override;
  35. Function DoAssemble:boolean;override;
  36. procedure WriteExternals;
  37. end;
  38. implementation
  39. uses
  40. cutils,globtype,globals,systems,cclasses,
  41. verbose,finput,fmodule,script,cpuinfo,
  42. itx86int,
  43. cgbase
  44. ;
  45. const
  46. line_length = 70;
  47. secnames : array[TAsmSectionType] of string[4] = ('',
  48. 'CODE','DATA','DATA','BSS',
  49. '','','','','','',
  50. '','','','','',''
  51. );
  52. secnamesml64 : array[TAsmSectionType] of string[7] = ('',
  53. '_TEXT','_DATE','_DATA','_BSS',
  54. '','','','',
  55. 'idata$2','idata$4','idata$5','idata$6','idata$7','edata',
  56. '',''
  57. );
  58. function single2str(d : single) : string;
  59. var
  60. hs : string;
  61. p : byte;
  62. begin
  63. str(d,hs);
  64. { nasm expects a lowercase e }
  65. p:=pos('E',hs);
  66. if p>0 then
  67. hs[p]:='e';
  68. p:=pos('+',hs);
  69. if p>0 then
  70. delete(hs,p,1);
  71. single2str:=lower(hs);
  72. end;
  73. function double2str(d : double) : string;
  74. var
  75. hs : string;
  76. p : byte;
  77. begin
  78. str(d,hs);
  79. { nasm expects a lowercase e }
  80. p:=pos('E',hs);
  81. if p>0 then
  82. hs[p]:='e';
  83. p:=pos('+',hs);
  84. if p>0 then
  85. delete(hs,p,1);
  86. double2str:=lower(hs);
  87. end;
  88. function extended2str(e : extended) : string;
  89. var
  90. hs : string;
  91. p : byte;
  92. begin
  93. str(e,hs);
  94. { nasm expects a lowercase e }
  95. p:=pos('E',hs);
  96. if p>0 then
  97. hs[p]:='e';
  98. p:=pos('+',hs);
  99. if p>0 then
  100. delete(hs,p,1);
  101. extended2str:=lower(hs);
  102. end;
  103. function comp2str(d : bestreal) : string;
  104. type
  105. pdouble = ^double;
  106. var
  107. c : comp;
  108. dd : pdouble;
  109. begin
  110. {$ifdef FPC}
  111. c:=comp(d);
  112. {$else}
  113. c:=d;
  114. {$endif}
  115. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  116. comp2str:=double2str(dd^);
  117. end;
  118. function fixline(s:string):string;
  119. {
  120. return s with all leading and ending spaces and tabs removed
  121. }
  122. var
  123. i,j,k : longint;
  124. begin
  125. i:=length(s);
  126. while (i>0) and (s[i] in [#9,' ']) do
  127. dec(i);
  128. j:=1;
  129. while (j<i) and (s[j] in [#9,' ']) do
  130. inc(j);
  131. for k:=j to i do
  132. if s[k] in [#0..#31,#127..#255] then
  133. s[k]:='.';
  134. fixline:=Copy(s,j,i-j+1);
  135. end;
  136. {****************************************************************************
  137. tx86IntelAssembler
  138. ****************************************************************************}
  139. procedure tx86IntelAssembler.WriteReference(var ref : treference);
  140. var
  141. first : boolean;
  142. begin
  143. with ref do
  144. begin
  145. first:=true;
  146. if segment<>NR_NO then
  147. AsmWrite(masm_regname(segment)+':[')
  148. else
  149. AsmWrite('[');
  150. if assigned(symbol) then
  151. begin
  152. if (aktoutputformat = as_i386_tasm) then
  153. AsmWrite('dword ptr ');
  154. AsmWrite(symbol.name);
  155. first:=false;
  156. end;
  157. if (base<>NR_NO) then
  158. begin
  159. if not(first) then
  160. AsmWrite('+')
  161. else
  162. first:=false;
  163. AsmWrite(masm_regname(base));
  164. end;
  165. if (index<>NR_NO) then
  166. begin
  167. if not(first) then
  168. AsmWrite('+')
  169. else
  170. first:=false;
  171. AsmWrite(masm_regname(index));
  172. if scalefactor<>0 then
  173. AsmWrite('*'+tostr(scalefactor));
  174. end;
  175. if offset<0 then
  176. begin
  177. AsmWrite(tostr(offset));
  178. first:=false;
  179. end
  180. else if (offset>0) then
  181. begin
  182. AsmWrite('+'+tostr(offset));
  183. first:=false;
  184. end;
  185. if first then
  186. AsmWrite('0');
  187. AsmWrite(']');
  188. end;
  189. end;
  190. procedure tx86IntelAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;dest : boolean);
  191. begin
  192. case o.typ of
  193. top_reg :
  194. AsmWrite(masm_regname(o.reg));
  195. top_const :
  196. AsmWrite(tostr(longint(o.val)));
  197. top_ref :
  198. begin
  199. if o.ref^.refaddr=addr_no then
  200. begin
  201. if ((opcode <> A_LGS) and (opcode <> A_LSS) and
  202. (opcode <> A_LFS) and (opcode <> A_LDS) and
  203. (opcode <> A_LES)) then
  204. Begin
  205. case s of
  206. S_B : AsmWrite('byte ptr ');
  207. S_W : AsmWrite('word ptr ');
  208. S_L : AsmWrite('dword ptr ');
  209. S_Q : AsmWrite('qword ptr ');
  210. S_IS : AsmWrite('word ptr ');
  211. S_IL : AsmWrite('dword ptr ');
  212. S_IQ : AsmWrite('qword ptr ');
  213. S_FS : AsmWrite('dword ptr ');
  214. S_FL : AsmWrite('qword ptr ');
  215. S_T,
  216. S_FX : AsmWrite('tbyte ptr ');
  217. S_BW : if dest then
  218. AsmWrite('word ptr ')
  219. else
  220. AsmWrite('byte ptr ');
  221. S_BL : if dest then
  222. AsmWrite('dword ptr ')
  223. else
  224. AsmWrite('byte ptr ');
  225. S_WL : if dest then
  226. AsmWrite('dword ptr ')
  227. else
  228. AsmWrite('word ptr ');
  229. {$ifdef x86_64}
  230. S_BQ : if dest then
  231. AsmWrite('qword ptr ')
  232. else
  233. AsmWrite('byte ptr ');
  234. S_WQ : if dest then
  235. AsmWrite('qword ptr ')
  236. else
  237. AsmWrite('word ptr ');
  238. S_LQ : if dest then
  239. AsmWrite('qword ptr ')
  240. else
  241. AsmWrite('dword ptr ');
  242. S_XMM: AsmWrite('xmmword ptr ');
  243. {$endif x86_64}
  244. end;
  245. end;
  246. WriteReference(o.ref^);
  247. end
  248. else
  249. begin
  250. AsmWrite('offset ');
  251. if assigned(o.ref^.symbol) then
  252. AsmWrite(o.ref^.symbol.name);
  253. if o.ref^.offset>0 then
  254. AsmWrite('+'+tostr(o.ref^.offset))
  255. else
  256. if o.ref^.offset<0 then
  257. AsmWrite(tostr(o.ref^.offset))
  258. else
  259. if not(assigned(o.ref^.symbol)) then
  260. AsmWrite('0');
  261. end;
  262. end;
  263. else
  264. internalerror(2005060510);
  265. end;
  266. end;
  267. procedure tx86IntelAssembler.WriteOper_jmp(const o:toper;s : topsize);
  268. begin
  269. case o.typ of
  270. top_reg :
  271. AsmWrite(masm_regname(o.reg));
  272. top_const :
  273. AsmWrite(tostr(longint(o.val)));
  274. top_ref :
  275. { what about lcall or ljmp ??? }
  276. begin
  277. if o.ref^.refaddr=addr_no then
  278. begin
  279. if (aktoutputformat <> as_i386_tasm) then
  280. begin
  281. if s=S_FAR then
  282. AsmWrite('far ptr ')
  283. else
  284. {$ifdef x86_64}
  285. AsmWrite('qword ptr ');
  286. {$else x86_64}
  287. AsmWrite('dword ptr ');
  288. {$endif x86_64}
  289. end;
  290. WriteReference(o.ref^);
  291. end
  292. else
  293. begin
  294. AsmWrite(o.ref^.symbol.name);
  295. if o.ref^.offset>0 then
  296. AsmWrite('+'+tostr(o.ref^.offset))
  297. else
  298. if o.ref^.offset<0 then
  299. AsmWrite(tostr(o.ref^.offset));
  300. end;
  301. end;
  302. else
  303. internalerror(2005060511);
  304. end;
  305. end;
  306. var
  307. LasTSectype : TAsmSectionType;
  308. lastfileinfo : tfileposinfo;
  309. infile,
  310. lastinfile : tinputfile;
  311. const
  312. ait_const2str : array[ait_const_128bit..ait_const_indirect_symbol] of string[20]=(
  313. #9''#9,#9'DQ'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  314. #9'FIXMESLEB',#9'FIXEMEULEB',
  315. #9'DD RVA'#9,#9'FIXMEINDIRECT'#9
  316. );
  317. Function PadTabs(const p:string;addch:char):string;
  318. var
  319. s : string;
  320. i : longint;
  321. begin
  322. i:=length(p);
  323. if addch<>#0 then
  324. begin
  325. inc(i);
  326. s:=p+addch;
  327. end
  328. else
  329. s:=p;
  330. if i<8 then
  331. PadTabs:=s+#9#9
  332. else
  333. PadTabs:=s+#9;
  334. end;
  335. procedure tx86IntelAssembler.WriteTree(p:TAAsmoutput);
  336. const
  337. regallocstr : array[tregalloctype] of string[10]=(' allocated',' released',' sync',' resized');
  338. tempallocstr : array[boolean] of string[10]=(' released',' allocated');
  339. var
  340. s,
  341. prefix,
  342. suffix : string;
  343. hp : tai;
  344. hp1 : tailineinfo;
  345. counter,
  346. lines,
  347. InlineLevel : longint;
  348. i,j,l : longint;
  349. consttyp : taitype;
  350. do_line,DoNotSplitLine,
  351. quoted : boolean;
  352. begin
  353. if not assigned(p) then
  354. exit;
  355. { lineinfo is only needed for codesegment (PFV) }
  356. do_line:=((cs_asm_source in aktglobalswitches) or
  357. (cs_lineinfo in aktmoduleswitches))
  358. and (p=codesegment);
  359. InlineLevel:=0;
  360. DoNotSplitLine:=false;
  361. hp:=tai(p.first);
  362. while assigned(hp) do
  363. begin
  364. if do_line and not(hp.typ in SkipLineInfo) and
  365. not DoNotSplitLine then
  366. begin
  367. hp1:=hp as tailineinfo;
  368. { load infile }
  369. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  370. begin
  371. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  372. if assigned(infile) then
  373. begin
  374. { open only if needed !! }
  375. if (cs_asm_source in aktglobalswitches) then
  376. infile.open;
  377. end;
  378. { avoid unnecessary reopens of the same file !! }
  379. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  380. { be sure to change line !! }
  381. lastfileinfo.line:=-1;
  382. end;
  383. { write source }
  384. if (cs_asm_source in aktglobalswitches) and
  385. assigned(infile) then
  386. begin
  387. if (infile<>lastinfile) then
  388. begin
  389. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  390. if assigned(lastinfile) then
  391. lastinfile.close;
  392. end;
  393. if (hp1.fileinfo.line<>lastfileinfo.line) and
  394. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  395. begin
  396. if (hp1.fileinfo.line<>0) and
  397. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  398. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  399. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  400. { set it to a negative value !
  401. to make that is has been read already !! PM }
  402. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  403. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  404. end;
  405. end;
  406. lastfileinfo:=hp1.fileinfo;
  407. lastinfile:=infile;
  408. end;
  409. DoNotSplitLine:=false;
  410. case hp.typ of
  411. ait_comment : Begin
  412. AsmWrite(target_asm.comment);
  413. AsmWritePChar(tai_comment(hp).str);
  414. AsmLn;
  415. End;
  416. ait_regalloc :
  417. begin
  418. if (cs_asm_regalloc in aktglobalswitches) then
  419. AsmWriteLn(target_asm.comment+'Register '+masm_regname(tai_regalloc(hp).reg)+
  420. regallocstr[tai_regalloc(hp).ratype]);
  421. end;
  422. ait_tempalloc :
  423. begin
  424. if (cs_asm_tempalloc in aktglobalswitches) then
  425. begin
  426. {$ifdef EXTDEBUG}
  427. if assigned(tai_tempalloc(hp).problem) then
  428. AsmWriteLn(target_asm.comment+tai_tempalloc(hp).problem^+' ('+tostr(tai_tempalloc(hp).temppos)+','+
  429. tostr(tai_tempalloc(hp).tempsize)+')')
  430. else
  431. {$endif EXTDEBUG}
  432. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  433. tostr(tai_tempalloc(hp).tempsize)+tempallocstr[tai_tempalloc(hp).allocation]);
  434. end;
  435. end;
  436. ait_section : begin
  437. if tai_section(hp).sectype<>sec_none then
  438. begin
  439. if aktoutputformat=as_x86_64_masm then
  440. begin
  441. if LasTSecType<>sec_none then
  442. AsmWriteLn(secnamesml64[LasTSecType]+#9#9'ENDS');
  443. AsmLn;
  444. AsmWriteLn(secnamesml64[tai_section(hp).sectype]+#9+'SEGMENT')
  445. end
  446. else
  447. begin
  448. if LasTSecType<>sec_none then
  449. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  450. AsmLn;
  451. AsmWriteLn('_'+secnames[tai_section(hp).sectype]+#9#9+
  452. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  453. secnames[tai_section(hp).sectype]+'''');
  454. end;
  455. end;
  456. LasTSecType:=tai_section(hp).sectype;
  457. end;
  458. ait_align : begin
  459. { CAUSES PROBLEMS WITH THE SEGMENT DEFINITION }
  460. { SEGMENT DEFINITION SHOULD MATCH TYPE OF ALIGN }
  461. { HERE UNDER TASM! }
  462. if tai_align(hp).aligntype>1 then
  463. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  464. end;
  465. ait_datablock :
  466. begin
  467. if tai_datablock(hp).is_global then
  468. AsmWriteLn(#9'PUBLIC'#9+tai_datablock(hp).sym.name);
  469. AsmWriteLn(PadTabs(tai_datablock(hp).sym.name,#0)+'DB'#9+tostr(tai_datablock(hp).size)+' DUP(?)');
  470. end;
  471. ait_const_uleb128bit,
  472. ait_const_sleb128bit,
  473. ait_const_128bit,
  474. ait_const_64bit,
  475. ait_const_32bit,
  476. ait_const_16bit,
  477. ait_const_8bit,
  478. ait_const_rva_symbol,
  479. ait_const_indirect_symbol :
  480. begin
  481. AsmWrite(ait_const2str[hp.typ]);
  482. consttyp:=hp.typ;
  483. l:=0;
  484. repeat
  485. if assigned(tai_const(hp).sym) then
  486. begin
  487. if assigned(tai_const(hp).endsym) then
  488. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  489. else
  490. s:=tai_const(hp).sym.name;
  491. if tai_const(hp).value<>0 then
  492. s:=s+tostr_with_plus(tai_const(hp).value);
  493. end
  494. else
  495. s:=tostr(tai_const(hp).value);
  496. AsmWrite(s);
  497. if (l>line_length) or
  498. (hp.next=nil) or
  499. (tai(hp.next).typ<>consttyp) then
  500. break;
  501. hp:=tai(hp.next);
  502. AsmWrite(',');
  503. until false;
  504. AsmLn;
  505. end;
  506. ait_real_32bit : AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  507. ait_real_64bit : AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  508. ait_real_80bit : AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  509. ait_comp_64bit : AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  510. ait_string : begin
  511. counter := 0;
  512. lines := tai_string(hp).len div line_length;
  513. { separate lines in different parts }
  514. if tai_string(hp).len > 0 then
  515. Begin
  516. for j := 0 to lines-1 do
  517. begin
  518. AsmWrite(#9#9'DB'#9);
  519. quoted:=false;
  520. for i:=counter to counter+line_length-1 do
  521. begin
  522. { it is an ascii character. }
  523. if (ord(tai_string(hp).str[i])>31) and
  524. (ord(tai_string(hp).str[i])<128) and
  525. (tai_string(hp).str[i]<>'"') then
  526. begin
  527. if not(quoted) then
  528. begin
  529. if i>counter then
  530. AsmWrite(',');
  531. AsmWrite('"');
  532. end;
  533. AsmWrite(tai_string(hp).str[i]);
  534. quoted:=true;
  535. end { if > 31 and < 128 and ord('"') }
  536. else
  537. begin
  538. if quoted then
  539. AsmWrite('"');
  540. if i>counter then
  541. AsmWrite(',');
  542. quoted:=false;
  543. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  544. end;
  545. end; { end for i:=0 to... }
  546. if quoted then AsmWrite('"');
  547. AsmWrite(target_info.newline);
  548. counter := counter+line_length;
  549. end; { end for j:=0 ... }
  550. { do last line of lines }
  551. if counter<tai_string(hp).len then
  552. AsmWrite(#9#9'DB'#9);
  553. quoted:=false;
  554. for i:=counter to tai_string(hp).len-1 do
  555. begin
  556. { it is an ascii character. }
  557. if (ord(tai_string(hp).str[i])>31) and
  558. (ord(tai_string(hp).str[i])<128) and
  559. (tai_string(hp).str[i]<>'"') then
  560. begin
  561. if not(quoted) then
  562. begin
  563. if i>counter then
  564. AsmWrite(',');
  565. AsmWrite('"');
  566. end;
  567. AsmWrite(tai_string(hp).str[i]);
  568. quoted:=true;
  569. end { if > 31 and < 128 and " }
  570. else
  571. begin
  572. if quoted then
  573. AsmWrite('"');
  574. if i>counter then
  575. AsmWrite(',');
  576. quoted:=false;
  577. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  578. end;
  579. end; { end for i:=0 to... }
  580. if quoted then
  581. AsmWrite('"');
  582. end;
  583. AsmLn;
  584. end;
  585. ait_label : begin
  586. if tai_label(hp).l.is_used then
  587. begin
  588. AsmWrite(tai_label(hp).l.name);
  589. if assigned(hp.next) and not(tai(hp.next).typ in
  590. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  591. ait_const_rva_symbol,
  592. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  593. AsmWriteLn(':')
  594. else
  595. DoNotSplitLine:=true;
  596. end;
  597. end;
  598. ait_direct : begin
  599. AsmWritePChar(tai_direct(hp).str);
  600. AsmLn;
  601. end;
  602. ait_symbol : begin
  603. if tai_symbol(hp).is_global then
  604. AsmWriteLn(#9'PUBLIC'#9+tai_symbol(hp).sym.name);
  605. AsmWrite(tai_symbol(hp).sym.name);
  606. if assigned(hp.next) and not(tai(hp.next).typ in
  607. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  608. ait_const_rva_symbol,
  609. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  610. AsmWriteLn(':')
  611. end;
  612. ait_symbol_end : begin
  613. end;
  614. ait_instruction : begin
  615. taicpu(hp).CheckNonCommutativeOpcodes;
  616. taicpu(hp).SetOperandOrder(op_intel);
  617. { Reset }
  618. suffix:='';
  619. prefix:= '';
  620. { We need to explicitely set
  621. word prefix to get selectors
  622. to be pushed in 2 bytes PM }
  623. if (taicpu(hp).opsize=S_W) and
  624. (
  625. (
  626. (taicpu(hp).opcode=A_PUSH) or
  627. (taicpu(hp).opcode=A_POP)
  628. ) and
  629. (taicpu(hp).oper[0]^.typ=top_reg) and
  630. is_segment_reg(taicpu(hp).oper[0]^.reg)
  631. ) then
  632. AsmWriteln(#9#9'DB'#9'066h');
  633. { added prefix instructions, must be on same line as opcode }
  634. if (taicpu(hp).ops = 0) and
  635. ((taicpu(hp).opcode = A_REP) or
  636. (taicpu(hp).opcode = A_LOCK) or
  637. (taicpu(hp).opcode = A_REPE) or
  638. (taicpu(hp).opcode = A_REPNZ) or
  639. (taicpu(hp).opcode = A_REPZ) or
  640. (taicpu(hp).opcode = A_REPNE)) then
  641. Begin
  642. prefix:=std_op2str[taicpu(hp).opcode]+#9;
  643. hp:=tai(hp.next);
  644. { this is theorically impossible... }
  645. if hp=nil then
  646. begin
  647. AsmWriteLn(#9#9+prefix);
  648. break;
  649. end;
  650. { nasm prefers prefix on a line alone
  651. AsmWriteln(#9#9+prefix); but not masm PM
  652. prefix:=''; }
  653. if aktoutputformat in [as_i386_nasmcoff,as_i386_nasmwin32,as_i386_nasmwdosx,
  654. as_i386_nasmelf,as_i386_nasmobj,as_i386_nasmbeos] then
  655. begin
  656. AsmWriteln(prefix);
  657. prefix:='';
  658. end;
  659. end
  660. else
  661. prefix:= '';
  662. if (aktoutputformat = as_i386_wasm) and
  663. (taicpu(hp).opsize=S_W) and
  664. (taicpu(hp).opcode=A_PUSH) and
  665. (taicpu(hp).oper[0]^.typ=top_const) then
  666. begin
  667. AsmWriteln(#9#9'DB 66h,68h ; pushw imm16');
  668. AsmWrite(#9#9'DW');
  669. end
  670. else if (aktoutputformat=as_x86_64_masm) and
  671. (taicpu(hp).opcode=A_MOVQ) then
  672. AsmWrite(#9#9'mov')
  673. else
  674. AsmWrite(#9#9+prefix+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]+suffix);
  675. if taicpu(hp).ops<>0 then
  676. begin
  677. if is_calljmp(taicpu(hp).opcode) then
  678. begin
  679. AsmWrite(#9);
  680. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opsize);
  681. end
  682. else
  683. begin
  684. for i:=0to taicpu(hp).ops-1 do
  685. begin
  686. if i=0 then
  687. AsmWrite(#9)
  688. else
  689. AsmWrite(',');
  690. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,(i=2));
  691. end;
  692. end;
  693. end;
  694. AsmLn;
  695. end;
  696. {$ifdef GDB}
  697. ait_stabn,
  698. ait_stabs,
  699. ait_force_line,
  700. ait_stab_function_name : ;
  701. {$endif GDB}
  702. ait_cutobject : begin
  703. { only reset buffer if nothing has changed }
  704. if AsmSize=AsmStartSize then
  705. AsmClear
  706. else
  707. begin
  708. if LasTSecType<>sec_none then
  709. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  710. AsmLn;
  711. AsmWriteLn(#9'END');
  712. AsmClose;
  713. DoAssemble;
  714. AsmCreate(tai_cutobject(hp).place);
  715. end;
  716. { avoid empty files }
  717. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  718. begin
  719. if tai(hp.next).typ=ait_section then
  720. lasTSecType:=tai_section(hp.next).sectype;
  721. hp:=tai(hp.next);
  722. end;
  723. AsmWriteLn(#9'.386p');
  724. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  725. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  726. { I was told that this isn't necesarry because }
  727. { the labels generated by FPC are unique (FK) }
  728. { AsmWriteLn(#9'LOCALS '+target_asm.labelprefix); }
  729. if lasTSectype<>sec_none then
  730. AsmWriteLn('_'+secnames[lasTSectype]+#9#9+
  731. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  732. secnames[lasTSectype]+'''');
  733. AsmStartSize:=AsmSize;
  734. end;
  735. ait_marker :
  736. begin
  737. if tai_marker(hp).kind=InlineStart then
  738. inc(InlineLevel)
  739. else if tai_marker(hp).kind=InlineEnd then
  740. dec(InlineLevel);
  741. end;
  742. else
  743. internalerror(10000);
  744. end;
  745. hp:=tai(hp.next);
  746. end;
  747. end;
  748. var
  749. currentasmlist : TExternalAssembler;
  750. procedure writeexternal(p:tnamedindexitem;arg:pointer);
  751. begin
  752. if tasmsymbol(p).defbind=AB_EXTERNAL then
  753. begin
  754. case aktoutputformat of
  755. as_i386_masm,as_i386_wasm:
  756. currentasmlist.AsmWriteln(#9'EXTRN'#9+p.name
  757. +': NEAR');
  758. as_x86_64_masm:
  759. currentasmlist.AsmWriteln(#9'EXTRN'#9+p.name
  760. +': PROC');
  761. else
  762. currentasmlist.AsmWriteln(#9'EXTRN'#9+p.name);
  763. end;
  764. end;
  765. end;
  766. procedure tx86IntelAssembler.WriteExternals;
  767. begin
  768. currentasmlist:=self;
  769. objectlibrary.symbolsearch.foreach_static(@writeexternal,nil);
  770. end;
  771. function tx86intelassembler.DoAssemble : boolean;
  772. var f : file;
  773. begin
  774. DoAssemble:=Inherited DoAssemble;
  775. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  776. if (aktoutputformat in [as_i386_masm,as_i386_wasm]) then
  777. begin
  778. if not(cs_asm_extern in aktglobalswitches) then
  779. begin
  780. if Not FileExists(objfile) and
  781. FileExists(ForceExtension(objfile,'.obj')) then
  782. begin
  783. Assign(F,ForceExtension(objfile,'.obj'));
  784. Rename(F,objfile);
  785. end;
  786. end
  787. else
  788. AsmRes.AddAsmCommand('mv',ForceExtension(objfile,'.obj')+' '+objfile,objfile);
  789. end;
  790. end;
  791. procedure tx86IntelAssembler.WriteAsmList;
  792. begin
  793. {$ifdef EXTDEBUG}
  794. if assigned(current_module.mainsource) then
  795. comment(v_info,'Start writing intel-styled assembler output for '+current_module.mainsource^);
  796. {$endif}
  797. LasTSecType:=sec_none;
  798. if aktoutputformat<>as_x86_64_masm then
  799. begin
  800. AsmWriteLn(#9'.386p');
  801. { masm 6.11 does not seem to like LOCALS PM }
  802. if (aktoutputformat = as_i386_tasm) then
  803. begin
  804. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  805. end;
  806. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  807. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  808. AsmLn;
  809. end;
  810. WriteExternals;
  811. { INTEL ASM doesn't support stabs
  812. WriteTree(debuglist);}
  813. WriteTree(codesegment);
  814. WriteTree(datasegment);
  815. WriteTree(consts);
  816. WriteTree(rttilist);
  817. WriteTree(resourcestringlist);
  818. WriteTree(bsssegment);
  819. Writetree(importssection);
  820. { exports are written by DLLTOOL
  821. if we use it so don't insert it twice (PM) }
  822. if not UseDeffileForExports and assigned(exportssection) then
  823. Writetree(exportssection);
  824. AsmWriteLn(#9'END');
  825. AsmLn;
  826. {$ifdef EXTDEBUG}
  827. if assigned(current_module.mainsource) then
  828. comment(v_info,'Done writing intel-styled assembler output for '+current_module.mainsource^);
  829. {$endif EXTDEBUG}
  830. end;
  831. {*****************************************************************************
  832. Initialize
  833. *****************************************************************************}
  834. const
  835. as_i386_tasm_info : tasminfo =
  836. (
  837. id : as_i386_tasm;
  838. idtxt : 'TASM';
  839. asmbin : 'tasm';
  840. asmcmd : '/m2 /ml $ASM $OBJ';
  841. supported_target : system_any; { what should I write here ?? }
  842. flags : [af_allowdirect,af_needar,af_labelprefix_only_inside_procedure];
  843. labelprefix : '@@';
  844. comment : '; ';
  845. );
  846. as_i386_masm_info : tasminfo =
  847. (
  848. id : as_i386_masm;
  849. idtxt : 'MASM';
  850. asmbin : 'masm';
  851. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  852. supported_target : system_any; { what should I write here ?? }
  853. flags : [af_allowdirect,af_needar];
  854. labelprefix : '@@';
  855. comment : '; ';
  856. );
  857. as_i386_wasm_info : tasminfo =
  858. (
  859. id : as_i386_wasm;
  860. idtxt : 'WASM';
  861. asmbin : 'wasm';
  862. asmcmd : '$ASM -6s -fp6 -ms -zq -Fo=$OBJ';
  863. supported_target : system_any; { what should I write here ?? }
  864. flags : [af_allowdirect,af_needar];
  865. labelprefix : '@@';
  866. comment : '; ';
  867. );
  868. as_x86_64_masm_info : tasminfo =
  869. (
  870. id : as_x86_64_masm;
  871. idtxt : 'MASM';
  872. asmbin : 'ml64';
  873. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  874. supported_target : system_any; { what should I write here ?? }
  875. flags : [af_allowdirect,af_needar];
  876. labelprefix : '@@';
  877. comment : '; ';
  878. );
  879. initialization
  880. {$ifdef x86_64}
  881. RegisterAssembler(as_x86_64_masm_info,tx86IntelAssembler);
  882. {$endif x86_64}
  883. {$ifdef i386}
  884. RegisterAssembler(as_i386_tasm_info,tx86IntelAssembler);
  885. RegisterAssembler(as_i386_masm_info,tx86IntelAssembler);
  886. RegisterAssembler(as_i386_wasm_info,tx86IntelAssembler);
  887. {$endif i386}
  888. end.