agx86int.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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[aitconst_128bit..aitconst_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. consttype : taiconst_type;
  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:
  482. begin
  483. consttype:=tai_const(hp).consttype;
  484. case consttype of
  485. aitconst_uleb128bit,
  486. aitconst_sleb128bit,
  487. aitconst_128bit,
  488. aitconst_64bit,
  489. aitconst_32bit,
  490. aitconst_16bit,
  491. aitconst_8bit,
  492. aitconst_rva_symbol,
  493. aitconst_indirect_symbol :
  494. begin
  495. AsmWrite(ait_const2str[consttype]);
  496. l:=0;
  497. repeat
  498. if assigned(tai_const(hp).sym) then
  499. begin
  500. if assigned(tai_const(hp).endsym) then
  501. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  502. else
  503. s:=tai_const(hp).sym.name;
  504. if tai_const(hp).value<>0 then
  505. s:=s+tostr_with_plus(tai_const(hp).value);
  506. end
  507. else
  508. s:=tostr(tai_const(hp).value);
  509. AsmWrite(s);
  510. if (l>line_length) or
  511. (hp.next=nil) or
  512. (tai(hp.next).typ<>ait_const) or
  513. (tai_const(hp.next).consttype<>consttype) then
  514. break;
  515. hp:=tai(hp.next);
  516. AsmWrite(',');
  517. until false;
  518. AsmLn;
  519. end;
  520. 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+comp2str(tai_real_80bit(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).l.is_used then
  609. begin
  610. AsmWrite(tai_label(hp).l.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. hp:=tai(hp.next);
  663. { this is theorically impossible... }
  664. if hp=nil then
  665. begin
  666. AsmWriteLn(#9#9+prefix);
  667. break;
  668. end;
  669. { nasm prefers prefix on a line alone
  670. AsmWriteln(#9#9+prefix); but not masm PM
  671. prefix:=''; }
  672. if target_asm.id in [as_i386_nasmcoff,as_i386_nasmwin32,as_i386_nasmwdosx,
  673. as_i386_nasmelf,as_i386_nasmobj,as_i386_nasmbeos] then
  674. begin
  675. AsmWriteln(prefix);
  676. prefix:='';
  677. end;
  678. end
  679. else
  680. prefix:= '';
  681. if (target_asm.id = as_i386_wasm) and
  682. (taicpu(hp).opsize=S_W) and
  683. (taicpu(hp).opcode=A_PUSH) and
  684. (taicpu(hp).oper[0]^.typ=top_const) then
  685. begin
  686. AsmWriteln(#9#9'DB 66h,68h ; pushw imm16');
  687. AsmWrite(#9#9'DW');
  688. end
  689. else if (target_asm.id=as_x86_64_masm) and
  690. (taicpu(hp).opcode=A_MOVQ) then
  691. AsmWrite(#9#9'mov')
  692. else
  693. AsmWrite(#9#9+prefix+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]+suffix);
  694. if taicpu(hp).ops<>0 then
  695. begin
  696. if is_calljmp(taicpu(hp).opcode) then
  697. begin
  698. AsmWrite(#9);
  699. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opsize);
  700. end
  701. else
  702. begin
  703. for i:=0to taicpu(hp).ops-1 do
  704. begin
  705. if i=0 then
  706. AsmWrite(#9)
  707. else
  708. AsmWrite(',');
  709. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,(i=2));
  710. end;
  711. end;
  712. end;
  713. AsmLn;
  714. end;
  715. ait_stab,
  716. ait_force_line,
  717. ait_function_name : ;
  718. ait_cutobject :
  719. begin
  720. { only reset buffer if nothing has changed }
  721. if AsmSize=AsmStartSize then
  722. AsmClear
  723. else
  724. begin
  725. if LasTSecType<>sec_none then
  726. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  727. AsmLn;
  728. AsmWriteLn(#9'END');
  729. AsmClose;
  730. DoAssemble;
  731. AsmCreate(tai_cutobject(hp).place);
  732. end;
  733. { avoid empty files }
  734. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  735. begin
  736. if tai(hp.next).typ=ait_section then
  737. lasTSecType:=tai_section(hp.next).sectype;
  738. hp:=tai(hp.next);
  739. end;
  740. AsmWriteLn(#9'.386p');
  741. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  742. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  743. { I was told that this isn't necesarry because }
  744. { the labels generated by FPC are unique (FK) }
  745. { AsmWriteLn(#9'LOCALS '+target_asm.labelprefix); }
  746. if lasTSectype<>sec_none then
  747. AsmWriteLn('_'+secnames[lasTSectype]+#9#9+
  748. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  749. secnames[lasTSectype]+'''');
  750. AsmStartSize:=AsmSize;
  751. end;
  752. ait_marker :
  753. begin
  754. if tai_marker(hp).kind=InlineStart then
  755. inc(InlineLevel)
  756. else if tai_marker(hp).kind=InlineEnd then
  757. dec(InlineLevel);
  758. end;
  759. ait_directive :
  760. begin
  761. case tai_directive(hp).directive of
  762. asd_nasm_import :
  763. AsmWrite('import ');
  764. asd_extern :
  765. AsmWrite('EXTRN ');
  766. else
  767. internalerror(200509192);
  768. end;
  769. if assigned(tai_directive(hp).name) then
  770. AsmWrite(tai_directive(hp).name^);
  771. AsmLn;
  772. end;
  773. else
  774. internalerror(10000);
  775. end;
  776. hp:=tai(hp.next);
  777. end;
  778. end;
  779. var
  780. currentasmlist : TExternalAssembler;
  781. procedure writeexternal(p:tnamedindexitem;arg:pointer);
  782. begin
  783. if tasmsymbol(p).defbind=AB_EXTERNAL then
  784. begin
  785. case target_asm.id of
  786. as_i386_masm,as_i386_wasm:
  787. currentasmlist.AsmWriteln(#9'EXTRN'#9+p.name
  788. +': NEAR');
  789. as_x86_64_masm:
  790. currentasmlist.AsmWriteln(#9'EXTRN'#9+p.name
  791. +': PROC');
  792. else
  793. currentasmlist.AsmWriteln(#9'EXTRN'#9+p.name);
  794. end;
  795. end;
  796. end;
  797. procedure tx86IntelAssembler.WriteExternals;
  798. begin
  799. currentasmlist:=self;
  800. objectlibrary.symbolsearch.foreach_static(@writeexternal,nil);
  801. end;
  802. function tx86intelassembler.DoAssemble : boolean;
  803. var f : file;
  804. begin
  805. DoAssemble:=Inherited DoAssemble;
  806. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  807. if (target_asm.id in [as_i386_masm,as_i386_wasm]) then
  808. begin
  809. if not(cs_asm_extern in aktglobalswitches) then
  810. begin
  811. if Not FileExists(objfile) and
  812. FileExists(ForceExtension(objfile,'.obj')) then
  813. begin
  814. Assign(F,ForceExtension(objfile,'.obj'));
  815. Rename(F,objfile);
  816. end;
  817. end
  818. else
  819. AsmRes.AddAsmCommand('mv',ForceExtension(objfile,'.obj')+' '+objfile,objfile);
  820. end;
  821. end;
  822. procedure tx86IntelAssembler.WriteAsmList;
  823. var
  824. hal : tasmlist;
  825. begin
  826. {$ifdef EXTDEBUG}
  827. if assigned(current_module.mainsource) then
  828. comment(v_info,'Start writing intel-styled assembler output for '+current_module.mainsource^);
  829. {$endif}
  830. LasTSecType:=sec_none;
  831. if target_asm.id<>as_x86_64_masm then
  832. begin
  833. AsmWriteLn(#9'.386p');
  834. { masm 6.11 does not seem to like LOCALS PM }
  835. if (target_asm.id = as_i386_tasm) then
  836. begin
  837. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  838. end;
  839. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  840. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  841. AsmLn;
  842. end;
  843. WriteExternals;
  844. for hal:=low(Tasmlist) to high(Tasmlist) do
  845. begin
  846. AsmWriteLn(target_asm.comment+'Begin asmlist '+TasmlistStr[hal]);
  847. writetree(asmlist[hal]);
  848. AsmWriteLn(target_asm.comment+'End asmlist '+TasmlistStr[hal]);
  849. end;
  850. AsmWriteLn(#9'END');
  851. AsmLn;
  852. {$ifdef EXTDEBUG}
  853. if assigned(current_module.mainsource) then
  854. comment(v_info,'Done writing intel-styled assembler output for '+current_module.mainsource^);
  855. {$endif EXTDEBUG}
  856. end;
  857. {*****************************************************************************
  858. Initialize
  859. *****************************************************************************}
  860. const
  861. as_i386_tasm_info : tasminfo =
  862. (
  863. id : as_i386_tasm;
  864. idtxt : 'TASM';
  865. asmbin : 'tasm';
  866. asmcmd : '/m2 /ml $ASM $OBJ';
  867. supported_target : system_any; { what should I write here ?? }
  868. flags : [af_allowdirect,af_needar,af_labelprefix_only_inside_procedure];
  869. labelprefix : '@@';
  870. comment : '; ';
  871. );
  872. as_i386_masm_info : tasminfo =
  873. (
  874. id : as_i386_masm;
  875. idtxt : 'MASM';
  876. asmbin : 'masm';
  877. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  878. supported_target : system_any; { what should I write here ?? }
  879. flags : [af_allowdirect,af_needar];
  880. labelprefix : '@@';
  881. comment : '; ';
  882. );
  883. as_i386_wasm_info : tasminfo =
  884. (
  885. id : as_i386_wasm;
  886. idtxt : 'WASM';
  887. asmbin : 'wasm';
  888. asmcmd : '$ASM -6s -fp6 -ms -zq -Fo=$OBJ';
  889. supported_target : system_any; { what should I write here ?? }
  890. flags : [af_allowdirect,af_needar];
  891. labelprefix : '@@';
  892. comment : '; ';
  893. );
  894. as_x86_64_masm_info : tasminfo =
  895. (
  896. id : as_x86_64_masm;
  897. idtxt : 'MASM';
  898. asmbin : 'ml64';
  899. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  900. supported_target : system_any; { what should I write here ?? }
  901. flags : [af_allowdirect,af_needar];
  902. labelprefix : '@@';
  903. comment : '; ';
  904. );
  905. initialization
  906. {$ifdef x86_64}
  907. RegisterAssembler(as_x86_64_masm_info,tx86IntelAssembler);
  908. {$endif x86_64}
  909. {$ifdef i386}
  910. RegisterAssembler(as_i386_tasm_info,tx86IntelAssembler);
  911. RegisterAssembler(as_i386_masm_info,tx86IntelAssembler);
  912. RegisterAssembler(as_i386_wasm_info,tx86IntelAssembler);
  913. {$endif i386}
  914. end.