agx86int.pas 33 KB

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