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,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:TAAsmoutput);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. {$ifdef FPC}
  118. c:=comp(d);
  119. {$else}
  120. c:=d;
  121. {$endif}
  122. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  123. comp2str:=double2str(dd^);
  124. end;
  125. function fixline(s:string):string;
  126. {
  127. return s with all leading and ending spaces and tabs removed
  128. }
  129. var
  130. i,j,k : longint;
  131. begin
  132. i:=length(s);
  133. while (i>0) and (s[i] in [#9,' ']) do
  134. dec(i);
  135. j:=1;
  136. while (j<i) and (s[j] in [#9,' ']) do
  137. inc(j);
  138. for k:=j to i do
  139. if s[k] in [#0..#31,#127..#255] then
  140. s[k]:='.';
  141. fixline:=Copy(s,j,i-j+1);
  142. end;
  143. {****************************************************************************
  144. tx86IntelAssembler
  145. ****************************************************************************}
  146. procedure tx86IntelAssembler.WriteReference(var ref : treference);
  147. var
  148. first : boolean;
  149. begin
  150. with ref do
  151. begin
  152. first:=true;
  153. if segment<>NR_NO then
  154. AsmWrite(masm_regname(segment)+':[')
  155. else
  156. AsmWrite('[');
  157. if assigned(symbol) then
  158. begin
  159. if (target_asm.id = as_i386_tasm) then
  160. AsmWrite('dword ptr ');
  161. AsmWrite(symbol.name);
  162. first:=false;
  163. end;
  164. if (base<>NR_NO) then
  165. begin
  166. if not(first) then
  167. AsmWrite('+')
  168. else
  169. first:=false;
  170. AsmWrite(masm_regname(base));
  171. end;
  172. if (index<>NR_NO) then
  173. begin
  174. if not(first) then
  175. AsmWrite('+')
  176. else
  177. first:=false;
  178. AsmWrite(masm_regname(index));
  179. if scalefactor<>0 then
  180. AsmWrite('*'+tostr(scalefactor));
  181. end;
  182. if offset<0 then
  183. begin
  184. AsmWrite(tostr(offset));
  185. first:=false;
  186. end
  187. else if (offset>0) then
  188. begin
  189. AsmWrite('+'+tostr(offset));
  190. first:=false;
  191. end;
  192. if first then
  193. AsmWrite('0');
  194. AsmWrite(']');
  195. end;
  196. end;
  197. procedure tx86IntelAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;dest : boolean);
  198. begin
  199. case o.typ of
  200. top_reg :
  201. AsmWrite(masm_regname(o.reg));
  202. top_const :
  203. AsmWrite(tostr(longint(o.val)));
  204. top_ref :
  205. begin
  206. if o.ref^.refaddr=addr_no then
  207. begin
  208. if ((opcode <> A_LGS) and (opcode <> A_LSS) and
  209. (opcode <> A_LFS) and (opcode <> A_LDS) and
  210. (opcode <> A_LES)) then
  211. Begin
  212. case s of
  213. S_B : AsmWrite('byte ptr ');
  214. S_W : AsmWrite('word ptr ');
  215. S_L : AsmWrite('dword ptr ');
  216. S_Q : AsmWrite('qword ptr ');
  217. S_IS : AsmWrite('word ptr ');
  218. S_IL : AsmWrite('dword ptr ');
  219. S_IQ : AsmWrite('qword ptr ');
  220. S_FS : AsmWrite('dword ptr ');
  221. S_FL : AsmWrite('qword ptr ');
  222. S_T,
  223. S_FX : AsmWrite('tbyte ptr ');
  224. S_BW : if dest then
  225. AsmWrite('word ptr ')
  226. else
  227. AsmWrite('byte ptr ');
  228. S_BL : if dest then
  229. AsmWrite('dword ptr ')
  230. else
  231. AsmWrite('byte ptr ');
  232. S_WL : if dest then
  233. AsmWrite('dword ptr ')
  234. else
  235. AsmWrite('word ptr ');
  236. {$ifdef x86_64}
  237. S_BQ : if dest then
  238. AsmWrite('qword ptr ')
  239. else
  240. AsmWrite('byte ptr ');
  241. S_WQ : if dest then
  242. AsmWrite('qword ptr ')
  243. else
  244. AsmWrite('word ptr ');
  245. S_LQ : if dest then
  246. AsmWrite('qword ptr ')
  247. else
  248. AsmWrite('dword ptr ');
  249. S_XMM: AsmWrite('xmmword ptr ');
  250. {$endif x86_64}
  251. end;
  252. end;
  253. WriteReference(o.ref^);
  254. end
  255. else
  256. begin
  257. AsmWrite('offset ');
  258. if assigned(o.ref^.symbol) then
  259. AsmWrite(o.ref^.symbol.name);
  260. if o.ref^.offset>0 then
  261. AsmWrite('+'+tostr(o.ref^.offset))
  262. else
  263. if o.ref^.offset<0 then
  264. AsmWrite(tostr(o.ref^.offset))
  265. else
  266. if not(assigned(o.ref^.symbol)) then
  267. AsmWrite('0');
  268. end;
  269. end;
  270. else
  271. internalerror(2005060510);
  272. end;
  273. end;
  274. procedure tx86IntelAssembler.WriteOper_jmp(const o:toper;s : topsize);
  275. begin
  276. case o.typ of
  277. top_reg :
  278. AsmWrite(masm_regname(o.reg));
  279. top_const :
  280. AsmWrite(tostr(longint(o.val)));
  281. top_ref :
  282. { what about lcall or ljmp ??? }
  283. begin
  284. if o.ref^.refaddr=addr_no then
  285. begin
  286. if (target_asm.id <> as_i386_tasm) then
  287. begin
  288. if s=S_FAR then
  289. AsmWrite('far ptr ')
  290. else
  291. {$ifdef x86_64}
  292. AsmWrite('qword ptr ');
  293. {$else x86_64}
  294. AsmWrite('dword ptr ');
  295. {$endif x86_64}
  296. end;
  297. WriteReference(o.ref^);
  298. end
  299. else
  300. begin
  301. AsmWrite(o.ref^.symbol.name);
  302. if o.ref^.offset>0 then
  303. AsmWrite('+'+tostr(o.ref^.offset))
  304. else
  305. if o.ref^.offset<0 then
  306. AsmWrite(tostr(o.ref^.offset));
  307. end;
  308. end;
  309. else
  310. internalerror(2005060511);
  311. end;
  312. end;
  313. var
  314. LasTSectype : TAsmSectionType;
  315. lastfileinfo : tfileposinfo;
  316. infile,
  317. lastinfile : tinputfile;
  318. const
  319. ait_const2str : array[ait_const_128bit..ait_const_indirect_symbol] of string[20]=(
  320. #9''#9,#9'DQ'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  321. #9'FIXMESLEB',#9'FIXEMEULEB',
  322. #9'DD RVA'#9,#9'FIXMEINDIRECT'#9
  323. );
  324. Function PadTabs(const p:string;addch:char):string;
  325. var
  326. s : string;
  327. i : longint;
  328. begin
  329. i:=length(p);
  330. if addch<>#0 then
  331. begin
  332. inc(i);
  333. s:=p+addch;
  334. end
  335. else
  336. s:=p;
  337. if i<8 then
  338. PadTabs:=s+#9#9
  339. else
  340. PadTabs:=s+#9;
  341. end;
  342. procedure tx86IntelAssembler.WriteTree(p:TAAsmoutput);
  343. const
  344. regallocstr : array[tregalloctype] of string[10]=(' allocated',' released',' sync',' resized');
  345. tempallocstr : array[boolean] of string[10]=(' released',' allocated');
  346. var
  347. s,
  348. prefix,
  349. suffix : string;
  350. hp : tai;
  351. hp1 : tailineinfo;
  352. counter,
  353. lines,
  354. InlineLevel : longint;
  355. i,j,l : longint;
  356. consttyp : taitype;
  357. do_line,DoNotSplitLine,
  358. quoted : boolean;
  359. begin
  360. if not assigned(p) then
  361. exit;
  362. { lineinfo is only needed for al_procedures (PFV) }
  363. do_line:=((cs_asm_source in aktglobalswitches) or
  364. (cs_lineinfo in aktmoduleswitches))
  365. and (p=asmlist[al_procedures]);
  366. InlineLevel:=0;
  367. DoNotSplitLine:=false;
  368. hp:=tai(p.first);
  369. while assigned(hp) do
  370. begin
  371. if do_line and not(hp.typ in SkipLineInfo) and
  372. not DoNotSplitLine then
  373. begin
  374. hp1:=hp as tailineinfo;
  375. { load infile }
  376. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  377. begin
  378. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  379. if assigned(infile) then
  380. begin
  381. { open only if needed !! }
  382. if (cs_asm_source in aktglobalswitches) then
  383. infile.open;
  384. end;
  385. { avoid unnecessary reopens of the same file !! }
  386. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  387. { be sure to change line !! }
  388. lastfileinfo.line:=-1;
  389. end;
  390. { write source }
  391. if (cs_asm_source in aktglobalswitches) and
  392. assigned(infile) then
  393. begin
  394. if (infile<>lastinfile) then
  395. begin
  396. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  397. if assigned(lastinfile) then
  398. lastinfile.close;
  399. end;
  400. if (hp1.fileinfo.line<>lastfileinfo.line) and
  401. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  402. begin
  403. if (hp1.fileinfo.line<>0) and
  404. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  405. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  406. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  407. { set it to a negative value !
  408. to make that is has been read already !! PM }
  409. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  410. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  411. end;
  412. end;
  413. lastfileinfo:=hp1.fileinfo;
  414. lastinfile:=infile;
  415. end;
  416. DoNotSplitLine:=false;
  417. case hp.typ of
  418. ait_comment :
  419. Begin
  420. AsmWrite(target_asm.comment);
  421. AsmWritePChar(tai_comment(hp).str);
  422. AsmLn;
  423. End;
  424. ait_regalloc :
  425. begin
  426. if (cs_asm_regalloc in aktglobalswitches) then
  427. AsmWriteLn(target_asm.comment+'Register '+masm_regname(tai_regalloc(hp).reg)+
  428. regallocstr[tai_regalloc(hp).ratype]);
  429. end;
  430. ait_tempalloc :
  431. begin
  432. if (cs_asm_tempalloc in aktglobalswitches) then
  433. begin
  434. {$ifdef EXTDEBUG}
  435. if assigned(tai_tempalloc(hp).problem) then
  436. AsmWriteLn(target_asm.comment+tai_tempalloc(hp).problem^+' ('+tostr(tai_tempalloc(hp).temppos)+','+
  437. tostr(tai_tempalloc(hp).tempsize)+')')
  438. else
  439. {$endif EXTDEBUG}
  440. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  441. tostr(tai_tempalloc(hp).tempsize)+tempallocstr[tai_tempalloc(hp).allocation]);
  442. end;
  443. end;
  444. ait_section :
  445. begin
  446. if tai_section(hp).sectype<>sec_none then
  447. begin
  448. if target_asm.id=as_x86_64_masm then
  449. begin
  450. if LasTSecType<>sec_none then
  451. AsmWriteLn(secnamesml64[LasTSecType]+#9#9'ENDS');
  452. AsmLn;
  453. AsmWriteLn(secnamesml64[tai_section(hp).sectype]+#9+'SEGMENT')
  454. end
  455. else
  456. begin
  457. if LasTSecType<>sec_none then
  458. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  459. AsmLn;
  460. AsmWriteLn('_'+secnames[tai_section(hp).sectype]+#9#9+
  461. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  462. secnames[tai_section(hp).sectype]+'''');
  463. end;
  464. end;
  465. LasTSecType:=tai_section(hp).sectype;
  466. end;
  467. ait_align :
  468. begin
  469. { CAUSES PROBLEMS WITH THE SEGMENT DEFINITION }
  470. { SEGMENT DEFINITION SHOULD MATCH TYPE OF ALIGN }
  471. { HERE UNDER TASM! }
  472. if tai_align(hp).aligntype>1 then
  473. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  474. end;
  475. ait_datablock :
  476. begin
  477. if tai_datablock(hp).is_global then
  478. AsmWriteLn(#9'PUBLIC'#9+tai_datablock(hp).sym.name);
  479. AsmWriteLn(PadTabs(tai_datablock(hp).sym.name,#0)+'DB'#9+tostr(tai_datablock(hp).size)+' DUP(?)');
  480. end;
  481. ait_const_uleb128bit,
  482. ait_const_sleb128bit,
  483. ait_const_128bit,
  484. ait_const_64bit,
  485. ait_const_32bit,
  486. ait_const_16bit,
  487. ait_const_8bit,
  488. ait_const_rva_symbol,
  489. ait_const_indirect_symbol :
  490. begin
  491. AsmWrite(ait_const2str[hp.typ]);
  492. consttyp:=hp.typ;
  493. l:=0;
  494. repeat
  495. if assigned(tai_const(hp).sym) then
  496. begin
  497. if assigned(tai_const(hp).endsym) then
  498. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  499. else
  500. s:=tai_const(hp).sym.name;
  501. if tai_const(hp).value<>0 then
  502. s:=s+tostr_with_plus(tai_const(hp).value);
  503. end
  504. else
  505. s:=tostr(tai_const(hp).value);
  506. AsmWrite(s);
  507. if (l>line_length) or
  508. (hp.next=nil) or
  509. (tai(hp.next).typ<>consttyp) then
  510. break;
  511. hp:=tai(hp.next);
  512. AsmWrite(',');
  513. until false;
  514. AsmLn;
  515. end;
  516. ait_real_32bit :
  517. AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  518. ait_real_64bit :
  519. AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  520. ait_real_80bit :
  521. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  522. ait_comp_64bit :
  523. AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  524. ait_string :
  525. begin
  526. counter := 0;
  527. lines := tai_string(hp).len div line_length;
  528. { separate lines in different parts }
  529. if tai_string(hp).len > 0 then
  530. Begin
  531. for j := 0 to lines-1 do
  532. begin
  533. AsmWrite(#9#9'DB'#9);
  534. quoted:=false;
  535. for i:=counter to counter+line_length-1 do
  536. begin
  537. { it is an ascii character. }
  538. if (ord(tai_string(hp).str[i])>31) and
  539. (ord(tai_string(hp).str[i])<128) and
  540. (tai_string(hp).str[i]<>'"') then
  541. begin
  542. if not(quoted) then
  543. begin
  544. if i>counter then
  545. AsmWrite(',');
  546. AsmWrite('"');
  547. end;
  548. AsmWrite(tai_string(hp).str[i]);
  549. quoted:=true;
  550. end { if > 31 and < 128 and ord('"') }
  551. else
  552. begin
  553. if quoted then
  554. AsmWrite('"');
  555. if i>counter then
  556. AsmWrite(',');
  557. quoted:=false;
  558. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  559. end;
  560. end; { end for i:=0 to... }
  561. if quoted then AsmWrite('"');
  562. AsmWrite(target_info.newline);
  563. counter := counter+line_length;
  564. end; { end for j:=0 ... }
  565. { do last line of lines }
  566. if counter<tai_string(hp).len then
  567. AsmWrite(#9#9'DB'#9);
  568. quoted:=false;
  569. for i:=counter to tai_string(hp).len-1 do
  570. begin
  571. { it is an ascii character. }
  572. if (ord(tai_string(hp).str[i])>31) and
  573. (ord(tai_string(hp).str[i])<128) and
  574. (tai_string(hp).str[i]<>'"') then
  575. begin
  576. if not(quoted) then
  577. begin
  578. if i>counter then
  579. AsmWrite(',');
  580. AsmWrite('"');
  581. end;
  582. AsmWrite(tai_string(hp).str[i]);
  583. quoted:=true;
  584. end { if > 31 and < 128 and " }
  585. else
  586. begin
  587. if quoted then
  588. AsmWrite('"');
  589. if i>counter then
  590. AsmWrite(',');
  591. quoted:=false;
  592. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  593. end;
  594. end; { end for i:=0 to... }
  595. if quoted then
  596. AsmWrite('"');
  597. end;
  598. AsmLn;
  599. end;
  600. ait_label :
  601. begin
  602. if tai_label(hp).l.is_used then
  603. begin
  604. AsmWrite(tai_label(hp).l.name);
  605. if assigned(hp.next) and not(tai(hp.next).typ in
  606. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  607. ait_const_rva_symbol,
  608. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  609. AsmWriteLn(':')
  610. else
  611. DoNotSplitLine:=true;
  612. end;
  613. end;
  614. ait_symbol :
  615. begin
  616. if tai_symbol(hp).is_global then
  617. AsmWriteLn(#9'PUBLIC'#9+tai_symbol(hp).sym.name);
  618. AsmWrite(tai_symbol(hp).sym.name);
  619. if assigned(hp.next) and not(tai(hp.next).typ in
  620. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  621. ait_const_rva_symbol,
  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=InlineStart then
  751. inc(InlineLevel)
  752. else if tai_marker(hp).kind=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).defbind=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. objectlibrary.symbolsearch.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(objfile) and
  808. FileExists(ForceExtension(objfile,'.obj')) then
  809. begin
  810. Assign(F,ForceExtension(objfile,'.obj'));
  811. Rename(F,objfile);
  812. end;
  813. end
  814. else
  815. AsmRes.AddAsmCommand('mv',ForceExtension(objfile,'.obj')+' '+objfile,objfile);
  816. end;
  817. end;
  818. procedure tx86IntelAssembler.WriteAsmList;
  819. var
  820. hal : tasmlist;
  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(Tasmlist) to high(Tasmlist) do
  841. begin
  842. AsmWriteLn(target_asm.comment+'Begin asmlist '+TasmlistStr[hal]);
  843. writetree(asmlist[hal]);
  844. AsmWriteLn(target_asm.comment+'End asmlist '+TasmlistStr[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.