agx86int.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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 (target_asm.id = 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 (target_asm.id <> 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 al_procedures (PFV) }
  356. do_line:=((cs_asm_source in aktglobalswitches) or
  357. (cs_lineinfo in aktmoduleswitches))
  358. and (p=asmlist[al_procedures]);
  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 target_asm.id=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_symbol :
  608. begin
  609. if tai_symbol(hp).is_global then
  610. AsmWriteLn(#9'PUBLIC'#9+tai_symbol(hp).sym.name);
  611. AsmWrite(tai_symbol(hp).sym.name);
  612. if assigned(hp.next) and not(tai(hp.next).typ in
  613. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  614. ait_const_rva_symbol,
  615. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  616. AsmWriteLn(':')
  617. end;
  618. ait_symbol_end :
  619. begin
  620. end;
  621. ait_instruction :
  622. begin
  623. taicpu(hp).CheckNonCommutativeOpcodes;
  624. taicpu(hp).SetOperandOrder(op_intel);
  625. { Reset }
  626. suffix:='';
  627. prefix:= '';
  628. { We need to explicitely set
  629. word prefix to get selectors
  630. to be pushed in 2 bytes PM }
  631. if (taicpu(hp).opsize=S_W) and
  632. (
  633. (
  634. (taicpu(hp).opcode=A_PUSH) or
  635. (taicpu(hp).opcode=A_POP)
  636. ) and
  637. (taicpu(hp).oper[0]^.typ=top_reg) and
  638. is_segment_reg(taicpu(hp).oper[0]^.reg)
  639. ) then
  640. AsmWriteln(#9#9'DB'#9'066h');
  641. { added prefix instructions, must be on same line as opcode }
  642. if (taicpu(hp).ops = 0) and
  643. ((taicpu(hp).opcode = A_REP) or
  644. (taicpu(hp).opcode = A_LOCK) or
  645. (taicpu(hp).opcode = A_REPE) or
  646. (taicpu(hp).opcode = A_REPNZ) or
  647. (taicpu(hp).opcode = A_REPZ) or
  648. (taicpu(hp).opcode = A_REPNE)) then
  649. Begin
  650. prefix:=std_op2str[taicpu(hp).opcode]+#9;
  651. hp:=tai(hp.next);
  652. { this is theorically impossible... }
  653. if hp=nil then
  654. begin
  655. AsmWriteLn(#9#9+prefix);
  656. break;
  657. end;
  658. { nasm prefers prefix on a line alone
  659. AsmWriteln(#9#9+prefix); but not masm PM
  660. prefix:=''; }
  661. if target_asm.id in [as_i386_nasmcoff,as_i386_nasmwin32,as_i386_nasmwdosx,
  662. as_i386_nasmelf,as_i386_nasmobj,as_i386_nasmbeos] then
  663. begin
  664. AsmWriteln(prefix);
  665. prefix:='';
  666. end;
  667. end
  668. else
  669. prefix:= '';
  670. if (target_asm.id = as_i386_wasm) and
  671. (taicpu(hp).opsize=S_W) and
  672. (taicpu(hp).opcode=A_PUSH) and
  673. (taicpu(hp).oper[0]^.typ=top_const) then
  674. begin
  675. AsmWriteln(#9#9'DB 66h,68h ; pushw imm16');
  676. AsmWrite(#9#9'DW');
  677. end
  678. else if (target_asm.id=as_x86_64_masm) and
  679. (taicpu(hp).opcode=A_MOVQ) then
  680. AsmWrite(#9#9'mov')
  681. else
  682. AsmWrite(#9#9+prefix+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]+suffix);
  683. if taicpu(hp).ops<>0 then
  684. begin
  685. if is_calljmp(taicpu(hp).opcode) then
  686. begin
  687. AsmWrite(#9);
  688. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opsize);
  689. end
  690. else
  691. begin
  692. for i:=0to taicpu(hp).ops-1 do
  693. begin
  694. if i=0 then
  695. AsmWrite(#9)
  696. else
  697. AsmWrite(',');
  698. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,(i=2));
  699. end;
  700. end;
  701. end;
  702. AsmLn;
  703. end;
  704. ait_stab,
  705. ait_force_line,
  706. ait_function_name : ;
  707. ait_cutobject :
  708. begin
  709. { only reset buffer if nothing has changed }
  710. if AsmSize=AsmStartSize then
  711. AsmClear
  712. else
  713. begin
  714. if LasTSecType<>sec_none then
  715. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  716. AsmLn;
  717. AsmWriteLn(#9'END');
  718. AsmClose;
  719. DoAssemble;
  720. AsmCreate(tai_cutobject(hp).place);
  721. end;
  722. { avoid empty files }
  723. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  724. begin
  725. if tai(hp.next).typ=ait_section then
  726. lasTSecType:=tai_section(hp.next).sectype;
  727. hp:=tai(hp.next);
  728. end;
  729. AsmWriteLn(#9'.386p');
  730. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  731. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  732. { I was told that this isn't necesarry because }
  733. { the labels generated by FPC are unique (FK) }
  734. { AsmWriteLn(#9'LOCALS '+target_asm.labelprefix); }
  735. if lasTSectype<>sec_none then
  736. AsmWriteLn('_'+secnames[lasTSectype]+#9#9+
  737. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  738. secnames[lasTSectype]+'''');
  739. AsmStartSize:=AsmSize;
  740. end;
  741. ait_marker :
  742. begin
  743. if tai_marker(hp).kind=InlineStart then
  744. inc(InlineLevel)
  745. else if tai_marker(hp).kind=InlineEnd then
  746. dec(InlineLevel);
  747. end;
  748. ait_directive :
  749. begin
  750. case tai_directive(hp).directive of
  751. asd_nasm_import :
  752. AsmWrite('import ');
  753. asd_extern :
  754. AsmWrite('EXTRN ');
  755. else
  756. internalerror(200509192);
  757. end;
  758. if assigned(tai_directive(hp).name) then
  759. AsmWrite(tai_directive(hp).name^);
  760. AsmLn;
  761. end;
  762. else
  763. internalerror(10000);
  764. end;
  765. hp:=tai(hp.next);
  766. end;
  767. end;
  768. var
  769. currentasmlist : TExternalAssembler;
  770. procedure writeexternal(p:tnamedindexitem;arg:pointer);
  771. begin
  772. if tasmsymbol(p).defbind=AB_EXTERNAL then
  773. begin
  774. case target_asm.id of
  775. as_i386_masm,as_i386_wasm:
  776. currentasmlist.AsmWriteln(#9'EXTRN'#9+p.name
  777. +': NEAR');
  778. as_x86_64_masm:
  779. currentasmlist.AsmWriteln(#9'EXTRN'#9+p.name
  780. +': PROC');
  781. else
  782. currentasmlist.AsmWriteln(#9'EXTRN'#9+p.name);
  783. end;
  784. end;
  785. end;
  786. procedure tx86IntelAssembler.WriteExternals;
  787. begin
  788. currentasmlist:=self;
  789. objectlibrary.symbolsearch.foreach_static(@writeexternal,nil);
  790. end;
  791. function tx86intelassembler.DoAssemble : boolean;
  792. var f : file;
  793. begin
  794. DoAssemble:=Inherited DoAssemble;
  795. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  796. if (target_asm.id in [as_i386_masm,as_i386_wasm]) then
  797. begin
  798. if not(cs_asm_extern in aktglobalswitches) then
  799. begin
  800. if Not FileExists(objfile) and
  801. FileExists(ForceExtension(objfile,'.obj')) then
  802. begin
  803. Assign(F,ForceExtension(objfile,'.obj'));
  804. Rename(F,objfile);
  805. end;
  806. end
  807. else
  808. AsmRes.AddAsmCommand('mv',ForceExtension(objfile,'.obj')+' '+objfile,objfile);
  809. end;
  810. end;
  811. procedure tx86IntelAssembler.WriteAsmList;
  812. var
  813. hal : tasmlist;
  814. begin
  815. {$ifdef EXTDEBUG}
  816. if assigned(current_module.mainsource) then
  817. comment(v_info,'Start writing intel-styled assembler output for '+current_module.mainsource^);
  818. {$endif}
  819. LasTSecType:=sec_none;
  820. if target_asm.id<>as_x86_64_masm then
  821. begin
  822. AsmWriteLn(#9'.386p');
  823. { masm 6.11 does not seem to like LOCALS PM }
  824. if (target_asm.id = as_i386_tasm) then
  825. begin
  826. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  827. end;
  828. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  829. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  830. AsmLn;
  831. end;
  832. WriteExternals;
  833. for hal:=low(Tasmlist) to high(Tasmlist) do
  834. begin
  835. AsmWriteLn(target_asm.comment+'Begin asmlist '+TasmlistStr[hal]);
  836. writetree(asmlist[hal]);
  837. AsmWriteLn(target_asm.comment+'End asmlist '+TasmlistStr[hal]);
  838. end;
  839. AsmWriteLn(#9'END');
  840. AsmLn;
  841. {$ifdef EXTDEBUG}
  842. if assigned(current_module.mainsource) then
  843. comment(v_info,'Done writing intel-styled assembler output for '+current_module.mainsource^);
  844. {$endif EXTDEBUG}
  845. end;
  846. {*****************************************************************************
  847. Initialize
  848. *****************************************************************************}
  849. const
  850. as_i386_tasm_info : tasminfo =
  851. (
  852. id : as_i386_tasm;
  853. idtxt : 'TASM';
  854. asmbin : 'tasm';
  855. asmcmd : '/m2 /ml $ASM $OBJ';
  856. supported_target : system_any; { what should I write here ?? }
  857. flags : [af_allowdirect,af_needar,af_labelprefix_only_inside_procedure];
  858. labelprefix : '@@';
  859. comment : '; ';
  860. );
  861. as_i386_masm_info : tasminfo =
  862. (
  863. id : as_i386_masm;
  864. idtxt : 'MASM';
  865. asmbin : 'masm';
  866. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  867. supported_target : system_any; { what should I write here ?? }
  868. flags : [af_allowdirect,af_needar];
  869. labelprefix : '@@';
  870. comment : '; ';
  871. );
  872. as_i386_wasm_info : tasminfo =
  873. (
  874. id : as_i386_wasm;
  875. idtxt : 'WASM';
  876. asmbin : 'wasm';
  877. asmcmd : '$ASM -6s -fp6 -ms -zq -Fo=$OBJ';
  878. supported_target : system_any; { what should I write here ?? }
  879. flags : [af_allowdirect,af_needar];
  880. labelprefix : '@@';
  881. comment : '; ';
  882. );
  883. as_x86_64_masm_info : tasminfo =
  884. (
  885. id : as_x86_64_masm;
  886. idtxt : 'MASM';
  887. asmbin : 'ml64';
  888. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  889. supported_target : system_any; { what should I write here ?? }
  890. flags : [af_allowdirect,af_needar];
  891. labelprefix : '@@';
  892. comment : '; ';
  893. );
  894. initialization
  895. {$ifdef x86_64}
  896. RegisterAssembler(as_x86_64_masm_info,tx86IntelAssembler);
  897. {$endif x86_64}
  898. {$ifdef i386}
  899. RegisterAssembler(as_i386_tasm_info,tx86IntelAssembler);
  900. RegisterAssembler(as_i386_masm_info,tx86IntelAssembler);
  901. RegisterAssembler(as_i386_wasm_info,tx86IntelAssembler);
  902. {$endif i386}
  903. end.