agx86int.pas 35 KB

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