agx86int.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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'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_indirect_symbol :
  493. begin
  494. AsmWrite(ait_const2str[consttype]);
  495. l:=0;
  496. repeat
  497. if assigned(tai_const(hp).sym) then
  498. begin
  499. if assigned(tai_const(hp).endsym) then
  500. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  501. else
  502. s:=tai_const(hp).sym.name;
  503. if tai_const(hp).value<>0 then
  504. s:=s+tostr_with_plus(tai_const(hp).value);
  505. end
  506. else
  507. s:=tostr(tai_const(hp).value);
  508. AsmWrite(s);
  509. if (l>line_length) or
  510. (hp.next=nil) or
  511. (tai(hp.next).typ<>ait_const) or
  512. (tai_const(hp.next).consttype<>consttype) then
  513. break;
  514. hp:=tai(hp.next);
  515. AsmWrite(',');
  516. until false;
  517. AsmLn;
  518. end;
  519. end;
  520. end;
  521. ait_real_32bit :
  522. AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  523. ait_real_64bit :
  524. AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  525. ait_real_80bit :
  526. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  527. ait_comp_64bit :
  528. AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  529. ait_string :
  530. begin
  531. counter := 0;
  532. lines := tai_string(hp).len div line_length;
  533. { separate lines in different parts }
  534. if tai_string(hp).len > 0 then
  535. Begin
  536. for j := 0 to lines-1 do
  537. begin
  538. AsmWrite(#9#9'DB'#9);
  539. quoted:=false;
  540. for i:=counter to counter+line_length-1 do
  541. begin
  542. { it is an ascii character. }
  543. if (ord(tai_string(hp).str[i])>31) and
  544. (ord(tai_string(hp).str[i])<128) and
  545. (tai_string(hp).str[i]<>'"') then
  546. begin
  547. if not(quoted) then
  548. begin
  549. if i>counter then
  550. AsmWrite(',');
  551. AsmWrite('"');
  552. end;
  553. AsmWrite(tai_string(hp).str[i]);
  554. quoted:=true;
  555. end { if > 31 and < 128 and ord('"') }
  556. else
  557. begin
  558. if quoted then
  559. AsmWrite('"');
  560. if i>counter then
  561. AsmWrite(',');
  562. quoted:=false;
  563. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  564. end;
  565. end; { end for i:=0 to... }
  566. if quoted then AsmWrite('"');
  567. AsmWrite(target_info.newline);
  568. counter := counter+line_length;
  569. end; { end for j:=0 ... }
  570. { do last line of lines }
  571. if counter<tai_string(hp).len then
  572. AsmWrite(#9#9'DB'#9);
  573. quoted:=false;
  574. for i:=counter to tai_string(hp).len-1 do
  575. begin
  576. { it is an ascii character. }
  577. if (ord(tai_string(hp).str[i])>31) and
  578. (ord(tai_string(hp).str[i])<128) and
  579. (tai_string(hp).str[i]<>'"') then
  580. begin
  581. if not(quoted) then
  582. begin
  583. if i>counter then
  584. AsmWrite(',');
  585. AsmWrite('"');
  586. end;
  587. AsmWrite(tai_string(hp).str[i]);
  588. quoted:=true;
  589. end { if > 31 and < 128 and " }
  590. else
  591. begin
  592. if quoted then
  593. AsmWrite('"');
  594. if i>counter then
  595. AsmWrite(',');
  596. quoted:=false;
  597. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  598. end;
  599. end; { end for i:=0 to... }
  600. if quoted then
  601. AsmWrite('"');
  602. end;
  603. AsmLn;
  604. end;
  605. ait_label :
  606. begin
  607. if tai_label(hp).labsym.is_used then
  608. begin
  609. AsmWrite(tai_label(hp).labsym.name);
  610. if assigned(hp.next) and not(tai(hp.next).typ in
  611. [ait_const,
  612. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  613. AsmWriteLn(':')
  614. else
  615. DoNotSplitLine:=true;
  616. end;
  617. end;
  618. ait_symbol :
  619. begin
  620. if tai_symbol(hp).is_global then
  621. AsmWriteLn(#9'PUBLIC'#9+tai_symbol(hp).sym.name);
  622. AsmWrite(tai_symbol(hp).sym.name);
  623. if assigned(hp.next) and not(tai(hp.next).typ in
  624. [ait_const,
  625. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  626. AsmWriteLn(':')
  627. end;
  628. ait_symbol_end :
  629. begin
  630. end;
  631. ait_instruction :
  632. begin
  633. taicpu(hp).CheckNonCommutativeOpcodes;
  634. taicpu(hp).SetOperandOrder(op_intel);
  635. { Reset }
  636. suffix:='';
  637. prefix:= '';
  638. { We need to explicitely set
  639. word prefix to get selectors
  640. to be pushed in 2 bytes PM }
  641. if (taicpu(hp).opsize=S_W) and
  642. (
  643. (
  644. (taicpu(hp).opcode=A_PUSH) or
  645. (taicpu(hp).opcode=A_POP)
  646. ) and
  647. (taicpu(hp).oper[0]^.typ=top_reg) and
  648. is_segment_reg(taicpu(hp).oper[0]^.reg)
  649. ) then
  650. AsmWriteln(#9#9'DB'#9'066h');
  651. { added prefix instructions, must be on same line as opcode }
  652. if (taicpu(hp).ops = 0) and
  653. ((taicpu(hp).opcode = A_REP) or
  654. (taicpu(hp).opcode = A_LOCK) or
  655. (taicpu(hp).opcode = A_REPE) or
  656. (taicpu(hp).opcode = A_REPNZ) or
  657. (taicpu(hp).opcode = A_REPZ) or
  658. (taicpu(hp).opcode = A_REPNE)) then
  659. Begin
  660. prefix:=std_op2str[taicpu(hp).opcode]+#9;
  661. hp:=tai(hp.next);
  662. { this is theorically impossible... }
  663. if hp=nil then
  664. begin
  665. AsmWriteLn(#9#9+prefix);
  666. break;
  667. end;
  668. { nasm prefers prefix on a line alone
  669. AsmWriteln(#9#9+prefix); but not masm PM
  670. prefix:=''; }
  671. if target_asm.id in [as_i386_nasmcoff,as_i386_nasmwin32,as_i386_nasmwdosx,
  672. as_i386_nasmelf,as_i386_nasmobj,as_i386_nasmbeos] then
  673. begin
  674. AsmWriteln(prefix);
  675. prefix:='';
  676. end;
  677. end
  678. else
  679. prefix:= '';
  680. if (target_asm.id = as_i386_wasm) and
  681. (taicpu(hp).opsize=S_W) and
  682. (taicpu(hp).opcode=A_PUSH) and
  683. (taicpu(hp).oper[0]^.typ=top_const) then
  684. begin
  685. AsmWriteln(#9#9'DB 66h,68h ; pushw imm16');
  686. AsmWrite(#9#9'DW');
  687. end
  688. else if (target_asm.id=as_x86_64_masm) and
  689. (taicpu(hp).opcode=A_MOVQ) then
  690. AsmWrite(#9#9'mov')
  691. else
  692. AsmWrite(#9#9+prefix+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]+suffix);
  693. if taicpu(hp).ops<>0 then
  694. begin
  695. if is_calljmp(taicpu(hp).opcode) then
  696. begin
  697. AsmWrite(#9);
  698. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opsize);
  699. end
  700. else
  701. begin
  702. for i:=0to taicpu(hp).ops-1 do
  703. begin
  704. if i=0 then
  705. AsmWrite(#9)
  706. else
  707. AsmWrite(',');
  708. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,(i=2));
  709. end;
  710. end;
  711. end;
  712. AsmLn;
  713. end;
  714. ait_stab,
  715. ait_force_line,
  716. ait_function_name : ;
  717. ait_cutobject :
  718. begin
  719. { only reset buffer if nothing has changed }
  720. if AsmSize=AsmStartSize then
  721. AsmClear
  722. else
  723. begin
  724. if LasTSecType<>sec_none then
  725. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  726. AsmLn;
  727. AsmWriteLn(#9'END');
  728. AsmClose;
  729. DoAssemble;
  730. AsmCreate(tai_cutobject(hp).place);
  731. end;
  732. { avoid empty files }
  733. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  734. begin
  735. if tai(hp.next).typ=ait_section then
  736. lasTSecType:=tai_section(hp.next).sectype;
  737. hp:=tai(hp.next);
  738. end;
  739. AsmWriteLn(#9'.386p');
  740. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  741. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  742. { I was told that this isn't necesarry because }
  743. { the labels generated by FPC are unique (FK) }
  744. { AsmWriteLn(#9'LOCALS '+target_asm.labelprefix); }
  745. if lasTSectype<>sec_none then
  746. AsmWriteLn('_'+secnames[lasTSectype]+#9#9+
  747. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  748. secnames[lasTSectype]+'''');
  749. AsmStartSize:=AsmSize;
  750. end;
  751. ait_marker :
  752. begin
  753. if tai_marker(hp).kind=mark_InlineStart then
  754. inc(InlineLevel)
  755. else if tai_marker(hp).kind=mark_InlineEnd then
  756. dec(InlineLevel);
  757. end;
  758. ait_directive :
  759. begin
  760. case tai_directive(hp).directive of
  761. asd_nasm_import :
  762. AsmWrite('import ');
  763. asd_extern :
  764. AsmWrite('EXTRN ');
  765. else
  766. internalerror(200509192);
  767. end;
  768. if assigned(tai_directive(hp).name) then
  769. AsmWrite(tai_directive(hp).name^);
  770. AsmLn;
  771. end;
  772. else
  773. internalerror(10000);
  774. end;
  775. hp:=tai(hp.next);
  776. end;
  777. end;
  778. procedure tx86intelassembler.WriteExternals;
  779. var
  780. sym : TAsmSymbol;
  781. i : longint;
  782. begin
  783. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  784. begin
  785. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  786. if sym.bind=AB_EXTERNAL then
  787. begin
  788. case target_asm.id of
  789. as_i386_masm,
  790. as_i386_wasm :
  791. AsmWriteln(#9'EXTRN'#9+sym.name+': NEAR');
  792. as_x86_64_masm :
  793. AsmWriteln(#9'EXTRN'#9+sym.name+': PROC');
  794. else
  795. AsmWriteln(#9'EXTRN'#9+sym.name);
  796. end;
  797. end;
  798. end;
  799. end;
  800. function tx86intelassembler.DoAssemble : boolean;
  801. var
  802. masmobjfn : string;
  803. begin
  804. DoAssemble:=Inherited DoAssemble;
  805. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  806. if (target_asm.id in [as_i386_masm,as_i386_wasm]) then
  807. begin
  808. masmobjfn:=ChangeFileExt(objfilename,'.obj');
  809. if not(cs_asm_extern in current_settings.globalswitches) then
  810. begin
  811. if Not FileExists(objfilename) and
  812. FileExists(masmobjfn) then
  813. RenameFile(masmobjfn,objfilename);
  814. end
  815. else
  816. AsmRes.AddAsmCommand('mv',masmobjfn+' '+objfilename,objfilename);
  817. end;
  818. end;
  819. procedure tx86IntelAssembler.WriteAsmList;
  820. var
  821. hal : tasmlisttype;
  822. begin
  823. {$ifdef EXTDEBUG}
  824. if assigned(current_module.mainsource) then
  825. comment(v_info,'Start writing intel-styled assembler output for '+current_module.mainsource^);
  826. {$endif}
  827. LasTSecType:=sec_none;
  828. if target_asm.id<>as_x86_64_masm then
  829. begin
  830. AsmWriteLn(#9'.386p');
  831. { masm 6.11 does not seem to like LOCALS PM }
  832. if (target_asm.id = as_i386_tasm) then
  833. begin
  834. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  835. end;
  836. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  837. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  838. AsmLn;
  839. end;
  840. WriteExternals;
  841. for hal:=low(TasmlistType) to high(TasmlistType) do
  842. begin
  843. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  844. writetree(current_asmdata.asmlists[hal]);
  845. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  846. end;
  847. AsmWriteLn(#9'END');
  848. AsmLn;
  849. {$ifdef EXTDEBUG}
  850. if assigned(current_module.mainsource) then
  851. comment(v_info,'Done writing intel-styled assembler output for '+current_module.mainsource^);
  852. {$endif EXTDEBUG}
  853. end;
  854. {*****************************************************************************
  855. Initialize
  856. *****************************************************************************}
  857. const
  858. as_i386_tasm_info : tasminfo =
  859. (
  860. id : as_i386_tasm;
  861. idtxt : 'TASM';
  862. asmbin : 'tasm';
  863. asmcmd : '/m2 /ml $ASM $OBJ';
  864. supported_target : system_any; { what should I write here ?? }
  865. flags : [af_allowdirect,af_needar,af_labelprefix_only_inside_procedure];
  866. labelprefix : '@@';
  867. comment : '; ';
  868. );
  869. as_i386_masm_info : tasminfo =
  870. (
  871. id : as_i386_masm;
  872. idtxt : 'MASM';
  873. asmbin : 'masm';
  874. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  875. supported_target : system_any; { what should I write here ?? }
  876. flags : [af_allowdirect,af_needar];
  877. labelprefix : '@@';
  878. comment : '; ';
  879. );
  880. as_i386_wasm_info : tasminfo =
  881. (
  882. id : as_i386_wasm;
  883. idtxt : 'WASM';
  884. asmbin : 'wasm';
  885. asmcmd : '$ASM -6s -fp6 -ms -zq -Fo=$OBJ';
  886. supported_target : system_any; { what should I write here ?? }
  887. flags : [af_allowdirect,af_needar];
  888. labelprefix : '@@';
  889. comment : '; ';
  890. );
  891. as_x86_64_masm_info : tasminfo =
  892. (
  893. id : as_x86_64_masm;
  894. idtxt : 'MASM';
  895. asmbin : 'ml64';
  896. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  897. supported_target : system_any; { what should I write here ?? }
  898. flags : [af_allowdirect,af_needar];
  899. labelprefix : '@@';
  900. comment : '; ';
  901. );
  902. initialization
  903. {$ifdef x86_64}
  904. RegisterAssembler(as_x86_64_masm_info,tx86IntelAssembler);
  905. {$endif x86_64}
  906. {$ifdef i386}
  907. RegisterAssembler(as_i386_tasm_info,tx86IntelAssembler);
  908. RegisterAssembler(as_i386_masm_info,tx86IntelAssembler);
  909. RegisterAssembler(as_i386_wasm_info,tx86IntelAssembler);
  910. {$endif i386}
  911. end.