agx86int.pas 34 KB

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