agx86int.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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. var
  315. LasTSectype : TAsmSectiontype;
  316. lastfileinfo : tfileposinfo;
  317. infile,
  318. lastinfile : tinputfile;
  319. const
  320. ait_const2str : array[aitconst_128bit..aitconst_indirect_symbol] of string[20]=(
  321. #9''#9,#9'DQ'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  322. #9'FIXMESLEB',#9'FIXEMEULEB',
  323. #9'DD RVA'#9,#9'DD SECREL32'#9,#9'FIXMEINDIRECT'#9
  324. );
  325. Function PadTabs(const p:string;addch:char):string;
  326. var
  327. s : string;
  328. i : longint;
  329. begin
  330. i:=length(p);
  331. if addch<>#0 then
  332. begin
  333. inc(i);
  334. s:=p+addch;
  335. end
  336. else
  337. s:=p;
  338. if i<8 then
  339. PadTabs:=s+#9#9
  340. else
  341. PadTabs:=s+#9;
  342. end;
  343. procedure tx86IntelAssembler.WriteTree(p:TAsmList);
  344. const
  345. regallocstr : array[tregalloctype] of string[10]=(' allocated',' released',' sync',' resized');
  346. tempallocstr : array[boolean] of string[10]=(' released',' allocated');
  347. var
  348. s,
  349. prefix,
  350. suffix : string;
  351. hp : tai;
  352. hp1 : tailineinfo;
  353. counter,
  354. lines,
  355. InlineLevel : longint;
  356. i,j,l : longint;
  357. consttype : taiconst_type;
  358. do_line,DoNotSplitLine,
  359. quoted : boolean;
  360. begin
  361. if not assigned(p) then
  362. exit;
  363. { lineinfo is only needed for al_procedures (PFV) }
  364. do_line:=((cs_asm_source in current_settings.globalswitches) or
  365. (cs_lineinfo in current_settings.moduleswitches))
  366. and (p=current_asmdata.asmlists[al_procedures]);
  367. InlineLevel:=0;
  368. DoNotSplitLine:=false;
  369. hp:=tai(p.first);
  370. while assigned(hp) do
  371. begin
  372. if do_line and not(hp.typ in SkipLineInfo) and
  373. not DoNotSplitLine then
  374. begin
  375. hp1:=hp as tailineinfo;
  376. { load infile }
  377. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  378. begin
  379. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  380. if assigned(infile) then
  381. begin
  382. { open only if needed !! }
  383. if (cs_asm_source in current_settings.globalswitches) then
  384. infile.open;
  385. end;
  386. { avoid unnecessary reopens of the same file !! }
  387. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  388. { be sure to change line !! }
  389. lastfileinfo.line:=-1;
  390. end;
  391. { write source }
  392. if (cs_asm_source in current_settings.globalswitches) and
  393. assigned(infile) then
  394. begin
  395. if (infile<>lastinfile) then
  396. begin
  397. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  398. if assigned(lastinfile) then
  399. lastinfile.close;
  400. end;
  401. if (hp1.fileinfo.line<>lastfileinfo.line) and
  402. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  403. begin
  404. if (hp1.fileinfo.line<>0) and
  405. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  406. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  407. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  408. { set it to a negative value !
  409. to make that is has been read already !! PM }
  410. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  411. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  412. end;
  413. end;
  414. lastfileinfo:=hp1.fileinfo;
  415. lastinfile:=infile;
  416. end;
  417. DoNotSplitLine:=false;
  418. case hp.typ of
  419. ait_comment :
  420. Begin
  421. AsmWrite(target_asm.comment);
  422. AsmWritePChar(tai_comment(hp).str);
  423. AsmLn;
  424. End;
  425. ait_regalloc :
  426. begin
  427. if (cs_asm_regalloc in current_settings.globalswitches) then
  428. AsmWriteLn(target_asm.comment+'Register '+masm_regname(tai_regalloc(hp).reg)+
  429. regallocstr[tai_regalloc(hp).ratype]);
  430. end;
  431. ait_tempalloc :
  432. begin
  433. if (cs_asm_tempalloc in current_settings.globalswitches) then
  434. begin
  435. {$ifdef EXTDEBUG}
  436. if assigned(tai_tempalloc(hp).problem) then
  437. AsmWriteLn(target_asm.comment+tai_tempalloc(hp).problem^+' ('+tostr(tai_tempalloc(hp).temppos)+','+
  438. tostr(tai_tempalloc(hp).tempsize)+')')
  439. else
  440. {$endif EXTDEBUG}
  441. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  442. tostr(tai_tempalloc(hp).tempsize)+tempallocstr[tai_tempalloc(hp).allocation]);
  443. end;
  444. end;
  445. ait_section :
  446. begin
  447. if tai_section(hp).sectype<>sec_none then
  448. begin
  449. if target_asm.id=as_x86_64_masm then
  450. begin
  451. if LasTSecType<>sec_none then
  452. AsmWriteLn(secnamesml64[LasTSecType]+#9#9'ENDS');
  453. AsmLn;
  454. AsmWriteLn(secnamesml64[tai_section(hp).sectype]+#9+'SEGMENT')
  455. end
  456. else
  457. begin
  458. if LasTSecType<>sec_none then
  459. AsmWriteLn('_'+secnames[LasTSecType]+#9#9'ENDS');
  460. AsmLn;
  461. AsmWriteLn('_'+secnames[tai_section(hp).sectype]+#9#9+
  462. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  463. secnames[tai_section(hp).sectype]+'''');
  464. end;
  465. end;
  466. LasTSecType:=tai_section(hp).sectype;
  467. end;
  468. ait_align :
  469. begin
  470. { CAUSES PROBLEMS WITH THE SEGMENT DEFINITION }
  471. { SEGMENT DEFINITION SHOULD MATCH TYPE OF ALIGN }
  472. { HERE UNDER TASM! }
  473. if tai_align_abstract(hp).aligntype>1 then
  474. AsmWriteLn(#9'ALIGN '+tostr(tai_align_abstract(hp).aligntype));
  475. end;
  476. ait_datablock :
  477. begin
  478. if tai_datablock(hp).is_global then
  479. AsmWriteLn(#9'PUBLIC'#9+tai_datablock(hp).sym.name);
  480. AsmWriteLn(PadTabs(tai_datablock(hp).sym.name,#0)+'DB'#9+tostr(tai_datablock(hp).size)+' DUP(?)');
  481. end;
  482. ait_const:
  483. begin
  484. consttype:=tai_const(hp).consttype;
  485. case consttype of
  486. aitconst_uleb128bit,
  487. aitconst_sleb128bit,
  488. aitconst_128bit,
  489. aitconst_64bit,
  490. aitconst_32bit,
  491. aitconst_16bit,
  492. aitconst_8bit,
  493. aitconst_rva_symbol,
  494. aitconst_secrel32_symbol,
  495. aitconst_indirect_symbol :
  496. begin
  497. AsmWrite(ait_const2str[consttype]);
  498. l:=0;
  499. repeat
  500. if assigned(tai_const(hp).sym) then
  501. begin
  502. if assigned(tai_const(hp).endsym) then
  503. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  504. else
  505. s:=tai_const(hp).sym.name;
  506. if tai_const(hp).value<>0 then
  507. s:=s+tostr_with_plus(tai_const(hp).value);
  508. end
  509. else
  510. s:=tostr(tai_const(hp).value);
  511. AsmWrite(s);
  512. if (l>line_length) or
  513. (hp.next=nil) or
  514. (tai(hp.next).typ<>ait_const) or
  515. (tai_const(hp.next).consttype<>consttype) then
  516. break;
  517. hp:=tai(hp.next);
  518. AsmWrite(',');
  519. until false;
  520. AsmLn;
  521. end;
  522. else
  523. internalerror(200704253);
  524. end;
  525. end;
  526. ait_real_32bit :
  527. AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  528. ait_real_64bit :
  529. AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  530. ait_real_80bit :
  531. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  532. ait_comp_64bit :
  533. AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  534. ait_string :
  535. begin
  536. counter := 0;
  537. lines := tai_string(hp).len div line_length;
  538. { separate lines in different parts }
  539. if tai_string(hp).len > 0 then
  540. Begin
  541. for j := 0 to lines-1 do
  542. begin
  543. AsmWrite(#9#9'DB'#9);
  544. quoted:=false;
  545. for i:=counter to counter+line_length-1 do
  546. begin
  547. { it is an ascii character. }
  548. if (ord(tai_string(hp).str[i])>31) and
  549. (ord(tai_string(hp).str[i])<128) and
  550. (tai_string(hp).str[i]<>'"') then
  551. begin
  552. if not(quoted) then
  553. begin
  554. if i>counter then
  555. AsmWrite(',');
  556. AsmWrite('"');
  557. end;
  558. AsmWrite(tai_string(hp).str[i]);
  559. quoted:=true;
  560. end { if > 31 and < 128 and ord('"') }
  561. else
  562. begin
  563. if quoted then
  564. AsmWrite('"');
  565. if i>counter then
  566. AsmWrite(',');
  567. quoted:=false;
  568. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  569. end;
  570. end; { end for i:=0 to... }
  571. if quoted then AsmWrite('"');
  572. AsmWrite(target_info.newline);
  573. counter := counter+line_length;
  574. end; { end for j:=0 ... }
  575. { do last line of lines }
  576. if counter<tai_string(hp).len then
  577. AsmWrite(#9#9'DB'#9);
  578. quoted:=false;
  579. for i:=counter to tai_string(hp).len-1 do
  580. begin
  581. { it is an ascii character. }
  582. if (ord(tai_string(hp).str[i])>31) and
  583. (ord(tai_string(hp).str[i])<128) and
  584. (tai_string(hp).str[i]<>'"') then
  585. begin
  586. if not(quoted) then
  587. begin
  588. if i>counter then
  589. AsmWrite(',');
  590. AsmWrite('"');
  591. end;
  592. AsmWrite(tai_string(hp).str[i]);
  593. quoted:=true;
  594. end { if > 31 and < 128 and " }
  595. else
  596. begin
  597. if quoted then
  598. AsmWrite('"');
  599. if i>counter then
  600. AsmWrite(',');
  601. quoted:=false;
  602. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  603. end;
  604. end; { end for i:=0 to... }
  605. if quoted then
  606. AsmWrite('"');
  607. end;
  608. AsmLn;
  609. end;
  610. ait_label :
  611. begin
  612. if tai_label(hp).labsym.is_used then
  613. begin
  614. AsmWrite(tai_label(hp).labsym.name);
  615. if assigned(hp.next) and not(tai(hp.next).typ in
  616. [ait_const,
  617. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  618. AsmWriteLn(':')
  619. else
  620. DoNotSplitLine:=true;
  621. end;
  622. end;
  623. ait_symbol :
  624. begin
  625. if tai_symbol(hp).is_global then
  626. AsmWriteLn(#9'PUBLIC'#9+tai_symbol(hp).sym.name);
  627. AsmWrite(tai_symbol(hp).sym.name);
  628. if assigned(hp.next) and not(tai(hp.next).typ in
  629. [ait_const,
  630. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  631. AsmWriteLn(':')
  632. end;
  633. ait_symbol_end :
  634. begin
  635. end;
  636. ait_instruction :
  637. begin
  638. taicpu(hp).CheckNonCommutativeOpcodes;
  639. taicpu(hp).SetOperandOrder(op_intel);
  640. { Reset }
  641. suffix:='';
  642. prefix:= '';
  643. { We need to explicitely set
  644. word prefix to get selectors
  645. to be pushed in 2 bytes PM }
  646. if (taicpu(hp).opsize=S_W) and
  647. (
  648. (
  649. (taicpu(hp).opcode=A_PUSH) or
  650. (taicpu(hp).opcode=A_POP)
  651. ) and
  652. (taicpu(hp).oper[0]^.typ=top_reg) and
  653. is_segment_reg(taicpu(hp).oper[0]^.reg)
  654. ) then
  655. AsmWriteln(#9#9'DB'#9'066h');
  656. { added prefix instructions, must be on same line as opcode }
  657. if (taicpu(hp).ops = 0) and
  658. ((taicpu(hp).opcode = A_REP) or
  659. (taicpu(hp).opcode = A_LOCK) or
  660. (taicpu(hp).opcode = A_REPE) or
  661. (taicpu(hp).opcode = A_REPNZ) or
  662. (taicpu(hp).opcode = A_REPZ) or
  663. (taicpu(hp).opcode = A_REPNE)) then
  664. Begin
  665. prefix:=std_op2str[taicpu(hp).opcode]+#9;
  666. hp:=tai(hp.next);
  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] 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. LasTSecType:=sec_none;
  833. if target_asm.id<>as_x86_64_masm then
  834. begin
  835. AsmWriteLn(#9'.386p');
  836. { masm 6.11 does not seem to like LOCALS PM }
  837. if (target_asm.id = as_i386_tasm) then
  838. begin
  839. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  840. end;
  841. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  842. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  843. AsmLn;
  844. end;
  845. WriteExternals;
  846. for hal:=low(TasmlistType) to high(TasmlistType) do
  847. begin
  848. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  849. writetree(current_asmdata.asmlists[hal]);
  850. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  851. end;
  852. AsmWriteLn(#9'END');
  853. AsmLn;
  854. {$ifdef EXTDEBUG}
  855. if assigned(current_module.mainsource) then
  856. comment(v_info,'Done writing intel-styled assembler output for '+current_module.mainsource^);
  857. {$endif EXTDEBUG}
  858. end;
  859. {*****************************************************************************
  860. Initialize
  861. *****************************************************************************}
  862. const
  863. as_i386_tasm_info : tasminfo =
  864. (
  865. id : as_i386_tasm;
  866. idtxt : 'TASM';
  867. asmbin : 'tasm';
  868. asmcmd : '/m2 /ml $ASM $OBJ';
  869. supported_target : system_any; { what should I write here ?? }
  870. flags : [af_allowdirect,af_needar,af_labelprefix_only_inside_procedure];
  871. labelprefix : '@@';
  872. comment : '; ';
  873. );
  874. as_i386_masm_info : tasminfo =
  875. (
  876. id : as_i386_masm;
  877. idtxt : 'MASM';
  878. asmbin : 'masm';
  879. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  880. supported_target : system_any; { what should I write here ?? }
  881. flags : [af_allowdirect,af_needar];
  882. labelprefix : '@@';
  883. comment : '; ';
  884. );
  885. as_i386_wasm_info : tasminfo =
  886. (
  887. id : as_i386_wasm;
  888. idtxt : 'WASM';
  889. asmbin : 'wasm';
  890. asmcmd : '$ASM -6s -fp6 -ms -zq -Fo=$OBJ';
  891. supported_target : system_any; { what should I write here ?? }
  892. flags : [af_allowdirect,af_needar];
  893. labelprefix : '@@';
  894. comment : '; ';
  895. );
  896. as_x86_64_masm_info : tasminfo =
  897. (
  898. id : as_x86_64_masm;
  899. idtxt : 'MASM';
  900. asmbin : 'ml64';
  901. asmcmd : '/c /Cp $ASM /Fo$OBJ';
  902. supported_target : system_any; { what should I write here ?? }
  903. flags : [af_allowdirect,af_needar];
  904. labelprefix : '@@';
  905. comment : '; ';
  906. );
  907. initialization
  908. {$ifdef x86_64}
  909. RegisterAssembler(as_x86_64_masm_info,tx86IntelAssembler);
  910. {$endif x86_64}
  911. {$ifdef i386}
  912. RegisterAssembler(as_i386_tasm_info,tx86IntelAssembler);
  913. RegisterAssembler(as_i386_masm_info,tx86IntelAssembler);
  914. RegisterAssembler(as_i386_wasm_info,tx86IntelAssembler);
  915. {$endif i386}
  916. end.