agx86int.pas 33 KB

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