agx86int.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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','DATA','BSS','',
  50. '','','','','','',
  51. '','','','',
  52. '',
  53. '','','','',
  54. '',
  55. '',
  56. '',
  57. ''
  58. );
  59. secnamesml64 : array[TAsmSectiontype] of string[7] = ('',
  60. '_TEXT','_DATE','_DATA','_DATA','_BSS','',
  61. '','','','',
  62. 'idata$2','idata$4','idata$5','idata$6','idata$7','edata',
  63. '',
  64. '','','','',
  65. '',
  66. '',
  67. '',
  68. ''
  69. );
  70. function single2str(d : single) : string;
  71. var
  72. hs : string;
  73. p : byte;
  74. begin
  75. str(d,hs);
  76. { nasm expects a lowercase e }
  77. p:=pos('E',hs);
  78. if p>0 then
  79. hs[p]:='e';
  80. p:=pos('+',hs);
  81. if p>0 then
  82. delete(hs,p,1);
  83. single2str:=lower(hs);
  84. end;
  85. function double2str(d : double) : string;
  86. var
  87. hs : string;
  88. p : byte;
  89. begin
  90. str(d,hs);
  91. { nasm expects a lowercase e }
  92. p:=pos('E',hs);
  93. if p>0 then
  94. hs[p]:='e';
  95. p:=pos('+',hs);
  96. if p>0 then
  97. delete(hs,p,1);
  98. double2str:=lower(hs);
  99. end;
  100. function extended2str(e : extended) : string;
  101. var
  102. hs : string;
  103. p : byte;
  104. begin
  105. str(e,hs);
  106. { nasm expects a lowercase e }
  107. p:=pos('E',hs);
  108. if p>0 then
  109. hs[p]:='e';
  110. p:=pos('+',hs);
  111. if p>0 then
  112. delete(hs,p,1);
  113. extended2str:=lower(hs);
  114. end;
  115. function comp2str(d : bestreal) : string;
  116. type
  117. pdouble = ^double;
  118. var
  119. c : comp;
  120. dd : pdouble;
  121. begin
  122. c:=comp(d);
  123. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  124. comp2str:=double2str(dd^);
  125. end;
  126. function fixline(s:string):string;
  127. {
  128. return s with all leading and ending spaces and tabs removed
  129. }
  130. var
  131. i,j,k : longint;
  132. begin
  133. i:=length(s);
  134. while (i>0) and (s[i] in [#9,' ']) do
  135. dec(i);
  136. j:=1;
  137. while (j<i) and (s[j] in [#9,' ']) do
  138. inc(j);
  139. for k:=j to i do
  140. if s[k] in [#0..#31,#127..#255] then
  141. s[k]:='.';
  142. fixline:=Copy(s,j,i-j+1);
  143. end;
  144. {****************************************************************************
  145. tx86IntelAssembler
  146. ****************************************************************************}
  147. procedure tx86IntelAssembler.WriteReference(var ref : treference);
  148. var
  149. first : boolean;
  150. begin
  151. with ref do
  152. begin
  153. first:=true;
  154. if segment<>NR_NO then
  155. AsmWrite(masm_regname(segment)+':[')
  156. else
  157. AsmWrite('[');
  158. if assigned(symbol) then
  159. begin
  160. if (target_asm.id = as_i386_tasm) then
  161. AsmWrite('dword ptr ');
  162. AsmWrite(symbol.name);
  163. first:=false;
  164. end;
  165. if (base<>NR_NO) then
  166. begin
  167. if not(first) then
  168. AsmWrite('+')
  169. else
  170. first:=false;
  171. AsmWrite(masm_regname(base));
  172. end;
  173. if (index<>NR_NO) then
  174. begin
  175. if not(first) then
  176. AsmWrite('+')
  177. else
  178. first:=false;
  179. AsmWrite(masm_regname(index));
  180. if scalefactor<>0 then
  181. AsmWrite('*'+tostr(scalefactor));
  182. end;
  183. if offset<0 then
  184. begin
  185. AsmWrite(tostr(offset));
  186. first:=false;
  187. end
  188. else if (offset>0) then
  189. begin
  190. AsmWrite('+'+tostr(offset));
  191. first:=false;
  192. end;
  193. if first then
  194. AsmWrite('0');
  195. AsmWrite(']');
  196. end;
  197. end;
  198. procedure tx86IntelAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;dest : boolean);
  199. begin
  200. case o.typ of
  201. top_reg :
  202. AsmWrite(masm_regname(o.reg));
  203. top_const :
  204. AsmWrite(tostr(longint(o.val)));
  205. top_ref :
  206. begin
  207. if o.ref^.refaddr=addr_no then
  208. begin
  209. if ((opcode <> A_LGS) and (opcode <> A_LSS) and
  210. (opcode <> A_LFS) and (opcode <> A_LDS) and
  211. (opcode <> A_LES)) then
  212. Begin
  213. case s of
  214. S_B : AsmWrite('byte ptr ');
  215. S_W : AsmWrite('word ptr ');
  216. S_L : AsmWrite('dword ptr ');
  217. S_Q : AsmWrite('qword ptr ');
  218. S_IS : AsmWrite('word ptr ');
  219. S_IL : AsmWrite('dword ptr ');
  220. S_IQ : AsmWrite('qword ptr ');
  221. S_FS : AsmWrite('dword ptr ');
  222. S_FL : AsmWrite('qword ptr ');
  223. S_T,
  224. S_FX : AsmWrite('tbyte ptr ');
  225. S_BW : if dest then
  226. AsmWrite('word ptr ')
  227. else
  228. AsmWrite('byte ptr ');
  229. S_BL : if dest then
  230. AsmWrite('dword ptr ')
  231. else
  232. AsmWrite('byte ptr ');
  233. S_WL : if dest then
  234. AsmWrite('dword ptr ')
  235. else
  236. AsmWrite('word ptr ');
  237. {$ifdef x86_64}
  238. S_BQ : if dest then
  239. AsmWrite('qword ptr ')
  240. else
  241. AsmWrite('byte ptr ');
  242. S_WQ : if dest then
  243. AsmWrite('qword ptr ')
  244. else
  245. AsmWrite('word ptr ');
  246. S_LQ : if dest then
  247. AsmWrite('qword ptr ')
  248. else
  249. AsmWrite('dword ptr ');
  250. S_XMM: AsmWrite('xmmword ptr ');
  251. {$endif x86_64}
  252. end;
  253. end;
  254. WriteReference(o.ref^);
  255. end
  256. else
  257. begin
  258. AsmWrite('offset ');
  259. if assigned(o.ref^.symbol) then
  260. AsmWrite(o.ref^.symbol.name);
  261. if o.ref^.offset>0 then
  262. AsmWrite('+'+tostr(o.ref^.offset))
  263. else
  264. if o.ref^.offset<0 then
  265. AsmWrite(tostr(o.ref^.offset))
  266. else
  267. if not(assigned(o.ref^.symbol)) then
  268. AsmWrite('0');
  269. end;
  270. end;
  271. else
  272. internalerror(2005060510);
  273. end;
  274. end;
  275. procedure tx86IntelAssembler.WriteOper_jmp(const o:toper;s : topsize);
  276. begin
  277. case o.typ of
  278. top_reg :
  279. AsmWrite(masm_regname(o.reg));
  280. top_const :
  281. AsmWrite(tostr(longint(o.val)));
  282. top_ref :
  283. { what about lcall or ljmp ??? }
  284. begin
  285. if o.ref^.refaddr=addr_no then
  286. begin
  287. if (target_asm.id <> as_i386_tasm) then
  288. begin
  289. if s=S_FAR then
  290. AsmWrite('far ptr ')
  291. else
  292. {$ifdef x86_64}
  293. AsmWrite('qword ptr ');
  294. {$else x86_64}
  295. AsmWrite('dword ptr ');
  296. {$endif x86_64}
  297. end;
  298. WriteReference(o.ref^);
  299. end
  300. else
  301. begin
  302. AsmWrite(o.ref^.symbol.name);
  303. if o.ref^.offset>0 then
  304. AsmWrite('+'+tostr(o.ref^.offset))
  305. else
  306. if o.ref^.offset<0 then
  307. AsmWrite(tostr(o.ref^.offset));
  308. end;
  309. end;
  310. else
  311. internalerror(2005060511);
  312. end;
  313. end;
  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'DD SECREL32'#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 current_settings.globalswitches) or
  360. (cs_lineinfo in current_settings.moduleswitches))
  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 current_settings.globalswitches) 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 current_settings.globalswitches) 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 current_settings.globalswitches) 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 current_settings.globalswitches) 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_secrel32_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. inc(l,length(s));
  508. if (l>line_length) or
  509. (hp.next=nil) or
  510. (tai(hp.next).typ<>ait_const) or
  511. (tai_const(hp.next).consttype<>consttype) then
  512. break;
  513. hp:=tai(hp.next);
  514. AsmWrite(',');
  515. until false;
  516. AsmLn;
  517. end;
  518. else
  519. internalerror(200704253);
  520. end;
  521. end;
  522. ait_real_32bit :
  523. AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  524. ait_real_64bit :
  525. AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  526. ait_real_80bit :
  527. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  528. ait_comp_64bit :
  529. AsmWriteLn(#9#9'DQ'#9+extended2str(tai_comp_64bit(hp).value));
  530. ait_string :
  531. begin
  532. counter := 0;
  533. lines := tai_string(hp).len div line_length;
  534. { separate lines in different parts }
  535. if tai_string(hp).len > 0 then
  536. Begin
  537. for j := 0 to lines-1 do
  538. begin
  539. AsmWrite(#9#9'DB'#9);
  540. quoted:=false;
  541. for i:=counter to counter+line_length-1 do
  542. begin
  543. { it is an ascii character. }
  544. if (ord(tai_string(hp).str[i])>31) and
  545. (ord(tai_string(hp).str[i])<128) and
  546. (tai_string(hp).str[i]<>'"') then
  547. begin
  548. if not(quoted) then
  549. begin
  550. if i>counter then
  551. AsmWrite(',');
  552. AsmWrite('"');
  553. end;
  554. AsmWrite(tai_string(hp).str[i]);
  555. quoted:=true;
  556. end { if > 31 and < 128 and ord('"') }
  557. else
  558. begin
  559. if quoted then
  560. AsmWrite('"');
  561. if i>counter then
  562. AsmWrite(',');
  563. quoted:=false;
  564. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  565. end;
  566. end; { end for i:=0 to... }
  567. if quoted then AsmWrite('"');
  568. AsmWrite(target_info.newline);
  569. counter := counter+line_length;
  570. end; { end for j:=0 ... }
  571. { do last line of lines }
  572. if counter<tai_string(hp).len then
  573. AsmWrite(#9#9'DB'#9);
  574. quoted:=false;
  575. for i:=counter to tai_string(hp).len-1 do
  576. begin
  577. { it is an ascii character. }
  578. if (ord(tai_string(hp).str[i])>31) and
  579. (ord(tai_string(hp).str[i])<128) and
  580. (tai_string(hp).str[i]<>'"') then
  581. begin
  582. if not(quoted) then
  583. begin
  584. if i>counter then
  585. AsmWrite(',');
  586. AsmWrite('"');
  587. end;
  588. AsmWrite(tai_string(hp).str[i]);
  589. quoted:=true;
  590. end { if > 31 and < 128 and " }
  591. else
  592. begin
  593. if quoted then
  594. AsmWrite('"');
  595. if i>counter then
  596. AsmWrite(',');
  597. quoted:=false;
  598. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  599. end;
  600. end; { end for i:=0 to... }
  601. if quoted then
  602. AsmWrite('"');
  603. end;
  604. AsmLn;
  605. end;
  606. ait_label :
  607. begin
  608. if tai_label(hp).labsym.is_used then
  609. begin
  610. AsmWrite(tai_label(hp).labsym.name);
  611. if assigned(hp.next) and not(tai(hp.next).typ in
  612. [ait_const,
  613. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  614. AsmWriteLn(':')
  615. else
  616. DoNotSplitLine:=true;
  617. end;
  618. end;
  619. ait_symbol :
  620. begin
  621. if tai_symbol(hp).is_global then
  622. AsmWriteLn(#9'PUBLIC'#9+tai_symbol(hp).sym.name);
  623. AsmWrite(tai_symbol(hp).sym.name);
  624. if assigned(hp.next) and not(tai(hp.next).typ in
  625. [ait_const,
  626. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  627. AsmWriteLn(':')
  628. end;
  629. ait_symbol_end :
  630. begin
  631. end;
  632. ait_instruction :
  633. begin
  634. taicpu(hp).CheckNonCommutativeOpcodes;
  635. taicpu(hp).SetOperandOrder(op_intel);
  636. { Reset }
  637. suffix:='';
  638. prefix:= '';
  639. { We need to explicitely set
  640. word prefix to get selectors
  641. to be pushed in 2 bytes PM }
  642. if (taicpu(hp).opsize=S_W) and
  643. (
  644. (
  645. (taicpu(hp).opcode=A_PUSH) or
  646. (taicpu(hp).opcode=A_POP)
  647. ) and
  648. (taicpu(hp).oper[0]^.typ=top_reg) and
  649. is_segment_reg(taicpu(hp).oper[0]^.reg)
  650. ) then
  651. AsmWriteln(#9#9'DB'#9'066h');
  652. { added prefix instructions, must be on same line as opcode }
  653. if (taicpu(hp).ops = 0) and
  654. ((taicpu(hp).opcode = A_REP) or
  655. (taicpu(hp).opcode = A_LOCK) or
  656. (taicpu(hp).opcode = A_REPE) or
  657. (taicpu(hp).opcode = A_REPNZ) or
  658. (taicpu(hp).opcode = A_REPZ) or
  659. (taicpu(hp).opcode = A_REPNE)) then
  660. Begin
  661. prefix:=std_op2str[taicpu(hp).opcode]+#9;
  662. { there can be a stab inbetween when the opcode was on
  663. a different line in the source code }
  664. repeat
  665. hp:=tai(hp.next);
  666. until (hp=nil) or (hp.typ=ait_instruction);
  667. { this is theorically impossible... }
  668. if hp=nil then
  669. begin
  670. AsmWriteLn(#9#9+prefix);
  671. break;
  672. end;
  673. { nasm prefers prefix on a line alone
  674. AsmWriteln(#9#9+prefix); but not masm PM
  675. prefix:=''; }
  676. if target_asm.id in [as_i386_nasmcoff,as_i386_nasmwin32,as_i386_nasmwdosx,
  677. as_i386_nasmelf,as_i386_nasmobj,as_i386_nasmbeos,as_i386_nasmhaiku] then
  678. begin
  679. AsmWriteln(prefix);
  680. prefix:='';
  681. end;
  682. end
  683. else
  684. prefix:= '';
  685. if (target_asm.id = as_i386_wasm) and
  686. (taicpu(hp).opsize=S_W) and
  687. (taicpu(hp).opcode=A_PUSH) and
  688. (taicpu(hp).oper[0]^.typ=top_const) then
  689. begin
  690. AsmWriteln(#9#9'DB 66h,68h ; pushw imm16');
  691. AsmWrite(#9#9'DW');
  692. end
  693. else if (target_asm.id=as_x86_64_masm) and
  694. (taicpu(hp).opcode=A_MOVQ) then
  695. AsmWrite(#9#9'mov')
  696. else
  697. AsmWrite(#9#9+prefix+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]+suffix);
  698. if taicpu(hp).ops<>0 then
  699. begin
  700. if is_calljmp(taicpu(hp).opcode) then
  701. begin
  702. AsmWrite(#9);
  703. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opsize);
  704. end
  705. else
  706. begin
  707. for i:=0to taicpu(hp).ops-1 do
  708. begin
  709. if i=0 then
  710. AsmWrite(#9)
  711. else
  712. AsmWrite(',');
  713. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,(i=2));
  714. end;
  715. end;
  716. end;
  717. AsmLn;
  718. end;
  719. ait_stab,
  720. ait_force_line,
  721. ait_function_name : ;
  722. ait_cutobject :
  723. begin
  724. { only reset buffer if nothing has changed }
  725. if AsmSize=AsmStartSize then
  726. AsmClear
  727. else
  728. begin
  729. if LasTSecType<>sec_none then
  730. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  731. AsmLn;
  732. AsmWriteLn(#9'END');
  733. AsmClose;
  734. DoAssemble;
  735. AsmCreate(tai_cutobject(hp).place);
  736. end;
  737. { avoid empty files }
  738. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  739. begin
  740. if tai(hp.next).typ=ait_section then
  741. lasTSecType:=tai_section(hp.next).sectype;
  742. hp:=tai(hp.next);
  743. end;
  744. AsmWriteLn(#9'.386p');
  745. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  746. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  747. { I was told that this isn't necesarry because }
  748. { the labels generated by FPC are unique (FK) }
  749. { AsmWriteLn(#9'LOCALS '+target_asm.labelprefix); }
  750. if lasTSectype<>sec_none then
  751. AsmWriteLn('_'+secnames[lasTSectype]+#9#9+
  752. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  753. secnames[lasTSectype]+'''');
  754. AsmStartSize:=AsmSize;
  755. end;
  756. ait_marker :
  757. begin
  758. if tai_marker(hp).kind=mark_InlineStart then
  759. inc(InlineLevel)
  760. else if tai_marker(hp).kind=mark_InlineEnd then
  761. dec(InlineLevel);
  762. end;
  763. ait_directive :
  764. begin
  765. case tai_directive(hp).directive of
  766. asd_nasm_import :
  767. AsmWrite('import ');
  768. asd_extern :
  769. AsmWrite('EXTRN ');
  770. else
  771. internalerror(200509192);
  772. end;
  773. if assigned(tai_directive(hp).name) then
  774. AsmWrite(tai_directive(hp).name^);
  775. AsmLn;
  776. end;
  777. else
  778. internalerror(10000);
  779. end;
  780. hp:=tai(hp.next);
  781. end;
  782. end;
  783. procedure tx86intelassembler.WriteExternals;
  784. var
  785. sym : TAsmSymbol;
  786. i : longint;
  787. begin
  788. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  789. begin
  790. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  791. if sym.bind=AB_EXTERNAL then
  792. begin
  793. case target_asm.id of
  794. as_i386_masm,
  795. as_i386_wasm :
  796. AsmWriteln(#9'EXTRN'#9+sym.name+': NEAR');
  797. as_x86_64_masm :
  798. AsmWriteln(#9'EXTRN'#9+sym.name+': PROC');
  799. else
  800. AsmWriteln(#9'EXTRN'#9+sym.name);
  801. end;
  802. end;
  803. end;
  804. end;
  805. function tx86intelassembler.DoAssemble : boolean;
  806. var
  807. masmobjfn : string;
  808. begin
  809. DoAssemble:=Inherited DoAssemble;
  810. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  811. if (target_asm.id in [as_i386_masm,as_i386_wasm]) then
  812. begin
  813. masmobjfn:=ChangeFileExt(objfilename,'.obj');
  814. if not(cs_asm_extern in current_settings.globalswitches) then
  815. begin
  816. if Not FileExists(objfilename) and
  817. FileExists(masmobjfn) then
  818. RenameFile(masmobjfn,objfilename);
  819. end
  820. else
  821. AsmRes.AddAsmCommand('mv',masmobjfn+' '+objfilename,objfilename);
  822. end;
  823. end;
  824. procedure tx86IntelAssembler.WriteAsmList;
  825. var
  826. hal : tasmlisttype;
  827. begin
  828. {$ifdef EXTDEBUG}
  829. if assigned(current_module.mainsource) then
  830. comment(v_info,'Start writing intel-styled assembler output for '+current_module.mainsource^);
  831. {$endif}
  832. if target_asm.id<>as_x86_64_masm then
  833. begin
  834. AsmWriteLn(#9'.386p');
  835. { masm 6.11 does not seem to like LOCALS PM }
  836. if (target_asm.id = as_i386_tasm) then
  837. begin
  838. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  839. end;
  840. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  841. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  842. AsmLn;
  843. end;
  844. WriteExternals;
  845. for hal:=low(TasmlistType) to high(TasmlistType) do
  846. begin
  847. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  848. writetree(current_asmdata.asmlists[hal]);
  849. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  850. end;
  851. { better do this at end of WriteTree, but then there comes a trouble with
  852. al_const which does not have leading ait_section and thus goes out of segment }
  853. { TODO: probably ml64 needs 'closing' last section, too }
  854. if LastSecType <> sec_none then
  855. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  856. LastSecType := sec_none;
  857. AsmWriteLn(#9'END');
  858. AsmLn;
  859. {$ifdef EXTDEBUG}
  860. if assigned(current_module.mainsource) then
  861. comment(v_info,'Done writing intel-styled assembler output for '+current_module.mainsource^);
  862. {$endif EXTDEBUG}
  863. end;
  864. {*****************************************************************************
  865. Initialize
  866. *****************************************************************************}
  867. const
  868. {$ifdef i386}
  869. as_i386_tasm_info : tasminfo =
  870. (
  871. id : as_i386_tasm;
  872. idtxt : 'TASM';
  873. asmbin : 'tasm';
  874. asmcmd : '/m2 /ml $ASM $OBJ';
  875. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  876. flags : [af_allowdirect,af_needar,af_labelprefix_only_inside_procedure];
  877. labelprefix : '@@';
  878. comment : '; ';
  879. );
  880. as_i386_masm_info : tasminfo =
  881. (
  882. id : as_i386_masm;
  883. idtxt : 'MASM';
  884. asmbin : 'masm';
  885. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  886. supported_targets : [system_i386_GO32V2,system_i386_Win32,system_i386_wdosx,system_i386_watcom,system_i386_wince];
  887. flags : [af_allowdirect,af_needar];
  888. labelprefix : '@@';
  889. comment : '; ';
  890. );
  891. as_i386_wasm_info : tasminfo =
  892. (
  893. id : as_i386_wasm;
  894. idtxt : 'WASM';
  895. asmbin : 'wasm';
  896. asmcmd : '$ASM -6s -fp6 -ms -zq -Fo=$OBJ';
  897. supported_targets : [system_i386_watcom];
  898. flags : [af_allowdirect,af_needar];
  899. labelprefix : '@@';
  900. comment : '; ';
  901. );
  902. {$endif i386}
  903. {$ifdef x86_64}
  904. as_x86_64_masm_info : tasminfo =
  905. (
  906. id : as_x86_64_masm;
  907. idtxt : 'MASM';
  908. asmbin : 'ml64';
  909. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  910. supported_targets : [system_x86_64_win64];
  911. flags : [af_allowdirect,af_needar];
  912. labelprefix : '@@';
  913. comment : '; ';
  914. );
  915. {$endif x86_64}
  916. initialization
  917. {$ifdef x86_64}
  918. RegisterAssembler(as_x86_64_masm_info,tx86IntelAssembler);
  919. {$endif x86_64}
  920. {$ifdef i386}
  921. RegisterAssembler(as_i386_tasm_info,tx86IntelAssembler);
  922. RegisterAssembler(as_i386_masm_info,tx86IntelAssembler);
  923. RegisterAssembler(as_i386_wasm_info,tx86IntelAssembler);
  924. {$endif i386}
  925. end.