agx86int.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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=asmlist[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 :
  412. Begin
  413. AsmWrite(target_asm.comment);
  414. AsmWritePChar(tai_comment(hp).str);
  415. AsmLn;
  416. End;
  417. ait_regalloc :
  418. begin
  419. if (cs_asm_regalloc in aktglobalswitches) then
  420. AsmWriteLn(target_asm.comment+'Register '+masm_regname(tai_regalloc(hp).reg)+
  421. regallocstr[tai_regalloc(hp).ratype]);
  422. end;
  423. ait_tempalloc :
  424. begin
  425. if (cs_asm_tempalloc in aktglobalswitches) then
  426. begin
  427. {$ifdef EXTDEBUG}
  428. if assigned(tai_tempalloc(hp).problem) then
  429. AsmWriteLn(target_asm.comment+tai_tempalloc(hp).problem^+' ('+tostr(tai_tempalloc(hp).temppos)+','+
  430. tostr(tai_tempalloc(hp).tempsize)+')')
  431. else
  432. {$endif EXTDEBUG}
  433. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  434. tostr(tai_tempalloc(hp).tempsize)+tempallocstr[tai_tempalloc(hp).allocation]);
  435. end;
  436. end;
  437. ait_section :
  438. begin
  439. if tai_section(hp).sectype<>sec_none then
  440. begin
  441. if aktoutputformat=as_x86_64_masm then
  442. begin
  443. if LasTSecType<>sec_none then
  444. AsmWriteLn(secnamesml64[LasTSecType]+#9#9'ENDS');
  445. AsmLn;
  446. AsmWriteLn(secnamesml64[tai_section(hp).sectype]+#9+'SEGMENT')
  447. end
  448. else
  449. begin
  450. if LasTSecType<>sec_none then
  451. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  452. AsmLn;
  453. AsmWriteLn('_'+secnames[tai_section(hp).sectype]+#9#9+
  454. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  455. secnames[tai_section(hp).sectype]+'''');
  456. end;
  457. end;
  458. LasTSecType:=tai_section(hp).sectype;
  459. end;
  460. ait_align :
  461. begin
  462. { CAUSES PROBLEMS WITH THE SEGMENT DEFINITION }
  463. { SEGMENT DEFINITION SHOULD MATCH TYPE OF ALIGN }
  464. { HERE UNDER TASM! }
  465. if tai_align(hp).aligntype>1 then
  466. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  467. end;
  468. ait_datablock :
  469. begin
  470. if tai_datablock(hp).is_global then
  471. AsmWriteLn(#9'PUBLIC'#9+tai_datablock(hp).sym.name);
  472. AsmWriteLn(PadTabs(tai_datablock(hp).sym.name,#0)+'DB'#9+tostr(tai_datablock(hp).size)+' DUP(?)');
  473. end;
  474. ait_const_uleb128bit,
  475. ait_const_sleb128bit,
  476. ait_const_128bit,
  477. ait_const_64bit,
  478. ait_const_32bit,
  479. ait_const_16bit,
  480. ait_const_8bit,
  481. ait_const_rva_symbol,
  482. ait_const_indirect_symbol :
  483. begin
  484. AsmWrite(ait_const2str[hp.typ]);
  485. consttyp:=hp.typ;
  486. l:=0;
  487. repeat
  488. if assigned(tai_const(hp).sym) then
  489. begin
  490. if assigned(tai_const(hp).endsym) then
  491. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  492. else
  493. s:=tai_const(hp).sym.name;
  494. if tai_const(hp).value<>0 then
  495. s:=s+tostr_with_plus(tai_const(hp).value);
  496. end
  497. else
  498. s:=tostr(tai_const(hp).value);
  499. AsmWrite(s);
  500. if (l>line_length) or
  501. (hp.next=nil) or
  502. (tai(hp.next).typ<>consttyp) then
  503. break;
  504. hp:=tai(hp.next);
  505. AsmWrite(',');
  506. until false;
  507. AsmLn;
  508. end;
  509. ait_real_32bit :
  510. AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  511. ait_real_64bit :
  512. AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  513. ait_real_80bit :
  514. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  515. ait_comp_64bit :
  516. AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  517. ait_string :
  518. begin
  519. counter := 0;
  520. lines := tai_string(hp).len div line_length;
  521. { separate lines in different parts }
  522. if tai_string(hp).len > 0 then
  523. Begin
  524. for j := 0 to lines-1 do
  525. begin
  526. AsmWrite(#9#9'DB'#9);
  527. quoted:=false;
  528. for i:=counter to counter+line_length-1 do
  529. begin
  530. { it is an ascii character. }
  531. if (ord(tai_string(hp).str[i])>31) and
  532. (ord(tai_string(hp).str[i])<128) and
  533. (tai_string(hp).str[i]<>'"') then
  534. begin
  535. if not(quoted) then
  536. begin
  537. if i>counter then
  538. AsmWrite(',');
  539. AsmWrite('"');
  540. end;
  541. AsmWrite(tai_string(hp).str[i]);
  542. quoted:=true;
  543. end { if > 31 and < 128 and ord('"') }
  544. else
  545. begin
  546. if quoted then
  547. AsmWrite('"');
  548. if i>counter then
  549. AsmWrite(',');
  550. quoted:=false;
  551. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  552. end;
  553. end; { end for i:=0 to... }
  554. if quoted then AsmWrite('"');
  555. AsmWrite(target_info.newline);
  556. counter := counter+line_length;
  557. end; { end for j:=0 ... }
  558. { do last line of lines }
  559. if counter<tai_string(hp).len then
  560. AsmWrite(#9#9'DB'#9);
  561. quoted:=false;
  562. for i:=counter to tai_string(hp).len-1 do
  563. begin
  564. { it is an ascii character. }
  565. if (ord(tai_string(hp).str[i])>31) and
  566. (ord(tai_string(hp).str[i])<128) and
  567. (tai_string(hp).str[i]<>'"') then
  568. begin
  569. if not(quoted) then
  570. begin
  571. if i>counter then
  572. AsmWrite(',');
  573. AsmWrite('"');
  574. end;
  575. AsmWrite(tai_string(hp).str[i]);
  576. quoted:=true;
  577. end { if > 31 and < 128 and " }
  578. else
  579. begin
  580. if quoted then
  581. AsmWrite('"');
  582. if i>counter then
  583. AsmWrite(',');
  584. quoted:=false;
  585. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  586. end;
  587. end; { end for i:=0 to... }
  588. if quoted then
  589. AsmWrite('"');
  590. end;
  591. AsmLn;
  592. end;
  593. ait_label :
  594. begin
  595. if tai_label(hp).l.is_used then
  596. begin
  597. AsmWrite(tai_label(hp).l.name);
  598. if assigned(hp.next) and not(tai(hp.next).typ in
  599. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  600. ait_const_rva_symbol,
  601. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  602. AsmWriteLn(':')
  603. else
  604. DoNotSplitLine:=true;
  605. end;
  606. end;
  607. ait_direct :
  608. begin
  609. AsmWritePChar(tai_direct(hp).str);
  610. AsmLn;
  611. end;
  612. ait_symbol :
  613. begin
  614. if tai_symbol(hp).is_global then
  615. AsmWriteLn(#9'PUBLIC'#9+tai_symbol(hp).sym.name);
  616. AsmWrite(tai_symbol(hp).sym.name);
  617. if assigned(hp.next) and not(tai(hp.next).typ in
  618. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  619. ait_const_rva_symbol,
  620. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  621. AsmWriteLn(':')
  622. end;
  623. ait_symbol_end :
  624. begin
  625. end;
  626. ait_instruction :
  627. begin
  628. taicpu(hp).CheckNonCommutativeOpcodes;
  629. taicpu(hp).SetOperandOrder(op_intel);
  630. { Reset }
  631. suffix:='';
  632. prefix:= '';
  633. { We need to explicitely set
  634. word prefix to get selectors
  635. to be pushed in 2 bytes PM }
  636. if (taicpu(hp).opsize=S_W) and
  637. (
  638. (
  639. (taicpu(hp).opcode=A_PUSH) or
  640. (taicpu(hp).opcode=A_POP)
  641. ) and
  642. (taicpu(hp).oper[0]^.typ=top_reg) and
  643. is_segment_reg(taicpu(hp).oper[0]^.reg)
  644. ) then
  645. AsmWriteln(#9#9'DB'#9'066h');
  646. { added prefix instructions, must be on same line as opcode }
  647. if (taicpu(hp).ops = 0) and
  648. ((taicpu(hp).opcode = A_REP) or
  649. (taicpu(hp).opcode = A_LOCK) or
  650. (taicpu(hp).opcode = A_REPE) or
  651. (taicpu(hp).opcode = A_REPNZ) or
  652. (taicpu(hp).opcode = A_REPZ) or
  653. (taicpu(hp).opcode = A_REPNE)) then
  654. Begin
  655. prefix:=std_op2str[taicpu(hp).opcode]+#9;
  656. hp:=tai(hp.next);
  657. { this is theorically impossible... }
  658. if hp=nil then
  659. begin
  660. AsmWriteLn(#9#9+prefix);
  661. break;
  662. end;
  663. { nasm prefers prefix on a line alone
  664. AsmWriteln(#9#9+prefix); but not masm PM
  665. prefix:=''; }
  666. if aktoutputformat in [as_i386_nasmcoff,as_i386_nasmwin32,as_i386_nasmwdosx,
  667. as_i386_nasmelf,as_i386_nasmobj,as_i386_nasmbeos] then
  668. begin
  669. AsmWriteln(prefix);
  670. prefix:='';
  671. end;
  672. end
  673. else
  674. prefix:= '';
  675. if (aktoutputformat = as_i386_wasm) and
  676. (taicpu(hp).opsize=S_W) and
  677. (taicpu(hp).opcode=A_PUSH) and
  678. (taicpu(hp).oper[0]^.typ=top_const) then
  679. begin
  680. AsmWriteln(#9#9'DB 66h,68h ; pushw imm16');
  681. AsmWrite(#9#9'DW');
  682. end
  683. else if (aktoutputformat=as_x86_64_masm) and
  684. (taicpu(hp).opcode=A_MOVQ) then
  685. AsmWrite(#9#9'mov')
  686. else
  687. AsmWrite(#9#9+prefix+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]+suffix);
  688. if taicpu(hp).ops<>0 then
  689. begin
  690. if is_calljmp(taicpu(hp).opcode) then
  691. begin
  692. AsmWrite(#9);
  693. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opsize);
  694. end
  695. else
  696. begin
  697. for i:=0to taicpu(hp).ops-1 do
  698. begin
  699. if i=0 then
  700. AsmWrite(#9)
  701. else
  702. AsmWrite(',');
  703. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,(i=2));
  704. end;
  705. end;
  706. end;
  707. AsmLn;
  708. end;
  709. {$ifdef GDB}
  710. ait_stabn,
  711. ait_stabs,
  712. ait_force_line,
  713. ait_stab_function_name :
  714. ;
  715. {$endif GDB}
  716. ait_cutobject :
  717. begin
  718. { only reset buffer if nothing has changed }
  719. if AsmSize=AsmStartSize then
  720. AsmClear
  721. else
  722. begin
  723. if LasTSecType<>sec_none then
  724. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  725. AsmLn;
  726. AsmWriteLn(#9'END');
  727. AsmClose;
  728. DoAssemble;
  729. AsmCreate(tai_cutobject(hp).place);
  730. end;
  731. { avoid empty files }
  732. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  733. begin
  734. if tai(hp.next).typ=ait_section then
  735. lasTSecType:=tai_section(hp.next).sectype;
  736. hp:=tai(hp.next);
  737. end;
  738. AsmWriteLn(#9'.386p');
  739. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  740. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  741. { I was told that this isn't necesarry because }
  742. { the labels generated by FPC are unique (FK) }
  743. { AsmWriteLn(#9'LOCALS '+target_asm.labelprefix); }
  744. if lasTSectype<>sec_none then
  745. AsmWriteLn('_'+secnames[lasTSectype]+#9#9+
  746. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  747. secnames[lasTSectype]+'''');
  748. AsmStartSize:=AsmSize;
  749. end;
  750. ait_marker :
  751. begin
  752. if tai_marker(hp).kind=InlineStart then
  753. inc(InlineLevel)
  754. else if tai_marker(hp).kind=InlineEnd then
  755. dec(InlineLevel);
  756. end;
  757. else
  758. internalerror(10000);
  759. end;
  760. hp:=tai(hp.next);
  761. end;
  762. end;
  763. var
  764. currentasmlist : TExternalAssembler;
  765. procedure writeexternal(p:tnamedindexitem;arg:pointer);
  766. begin
  767. if tasmsymbol(p).defbind=AB_EXTERNAL then
  768. begin
  769. case aktoutputformat of
  770. as_i386_masm,as_i386_wasm:
  771. currentasmlist.AsmWriteln(#9'EXTRN'#9+p.name
  772. +': NEAR');
  773. as_x86_64_masm:
  774. currentasmlist.AsmWriteln(#9'EXTRN'#9+p.name
  775. +': PROC');
  776. else
  777. currentasmlist.AsmWriteln(#9'EXTRN'#9+p.name);
  778. end;
  779. end;
  780. end;
  781. procedure tx86IntelAssembler.WriteExternals;
  782. begin
  783. currentasmlist:=self;
  784. objectlibrary.symbolsearch.foreach_static(@writeexternal,nil);
  785. end;
  786. function tx86intelassembler.DoAssemble : boolean;
  787. var f : file;
  788. begin
  789. DoAssemble:=Inherited DoAssemble;
  790. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  791. if (aktoutputformat in [as_i386_masm,as_i386_wasm]) then
  792. begin
  793. if not(cs_asm_extern in aktglobalswitches) then
  794. begin
  795. if Not FileExists(objfile) and
  796. FileExists(ForceExtension(objfile,'.obj')) then
  797. begin
  798. Assign(F,ForceExtension(objfile,'.obj'));
  799. Rename(F,objfile);
  800. end;
  801. end
  802. else
  803. AsmRes.AddAsmCommand('mv',ForceExtension(objfile,'.obj')+' '+objfile,objfile);
  804. end;
  805. end;
  806. procedure tx86IntelAssembler.WriteAsmList;
  807. begin
  808. {$ifdef EXTDEBUG}
  809. if assigned(current_module.mainsource) then
  810. comment(v_info,'Start writing intel-styled assembler output for '+current_module.mainsource^);
  811. {$endif}
  812. LasTSecType:=sec_none;
  813. if aktoutputformat<>as_x86_64_masm then
  814. begin
  815. AsmWriteLn(#9'.386p');
  816. { masm 6.11 does not seem to like LOCALS PM }
  817. if (aktoutputformat = as_i386_tasm) then
  818. begin
  819. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  820. end;
  821. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  822. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  823. AsmLn;
  824. end;
  825. WriteExternals;
  826. { INTEL ASM doesn't support stabs
  827. WriteTree(debuglist);}
  828. WriteTree(asmlist[codesegment]);
  829. WriteTree(asmlist[datasegment]);
  830. WriteTree(asmlist[consts]);
  831. WriteTree(asmlist[rttilist]);
  832. WriteTree(asmlist[resourcestrings]);
  833. WriteTree(asmlist[bsssegment]);
  834. WriteTree(asmlist[threadvarsegment]);
  835. Writetree(asmlist[importsection]);
  836. { exports are written by DLLTOOL
  837. if we use it so don't insert it twice (PM) }
  838. if not UseDeffileForExports and assigned(asmlist[exportsection]) then
  839. Writetree(asmlist[exportsection]);
  840. AsmWriteLn(#9'END');
  841. AsmLn;
  842. {$ifdef EXTDEBUG}
  843. if assigned(current_module.mainsource) then
  844. comment(v_info,'Done writing intel-styled assembler output for '+current_module.mainsource^);
  845. {$endif EXTDEBUG}
  846. end;
  847. {*****************************************************************************
  848. Initialize
  849. *****************************************************************************}
  850. const
  851. as_i386_tasm_info : tasminfo =
  852. (
  853. id : as_i386_tasm;
  854. idtxt : 'TASM';
  855. asmbin : 'tasm';
  856. asmcmd : '/m2 /ml $ASM $OBJ';
  857. supported_target : system_any; { what should I write here ?? }
  858. flags : [af_allowdirect,af_needar,af_labelprefix_only_inside_procedure];
  859. labelprefix : '@@';
  860. comment : '; ';
  861. );
  862. as_i386_masm_info : tasminfo =
  863. (
  864. id : as_i386_masm;
  865. idtxt : 'MASM';
  866. asmbin : 'masm';
  867. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  868. supported_target : system_any; { what should I write here ?? }
  869. flags : [af_allowdirect,af_needar];
  870. labelprefix : '@@';
  871. comment : '; ';
  872. );
  873. as_i386_wasm_info : tasminfo =
  874. (
  875. id : as_i386_wasm;
  876. idtxt : 'WASM';
  877. asmbin : 'wasm';
  878. asmcmd : '$ASM -6s -fp6 -ms -zq -Fo=$OBJ';
  879. supported_target : system_any; { what should I write here ?? }
  880. flags : [af_allowdirect,af_needar];
  881. labelprefix : '@@';
  882. comment : '; ';
  883. );
  884. as_x86_64_masm_info : tasminfo =
  885. (
  886. id : as_x86_64_masm;
  887. idtxt : 'MASM';
  888. asmbin : 'ml64';
  889. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  890. supported_target : system_any; { what should I write here ?? }
  891. flags : [af_allowdirect,af_needar];
  892. labelprefix : '@@';
  893. comment : '; ';
  894. );
  895. initialization
  896. {$ifdef x86_64}
  897. RegisterAssembler(as_x86_64_masm_info,tx86IntelAssembler);
  898. {$endif x86_64}
  899. {$ifdef i386}
  900. RegisterAssembler(as_i386_tasm_info,tx86IntelAssembler);
  901. RegisterAssembler(as_i386_masm_info,tx86IntelAssembler);
  902. RegisterAssembler(as_i386_wasm_info,tx86IntelAssembler);
  903. {$endif i386}
  904. end.