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