agx86int.pas 33 KB

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