agx86int.pas 33 KB

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