agx86att.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements an asmoutput class for i386 AT&T syntax
  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. { This unit implements an asmoutput class for i386 AT&T syntax
  18. }
  19. unit agx86att;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. cclasses,cpubase,systems,
  24. globals,globtype,cgutils,
  25. aasmbase,aasmtai,aasmdata,assemble,aggas;
  26. type
  27. Tx86ATTAssembler=class(TGNUassembler)
  28. constructor create(info: pasminfo; smart: boolean); override;
  29. function MakeCmdLine: TCmdStr; override;
  30. end;
  31. Tx86AppleGNUAssembler=class(TAppleGNUassembler)
  32. constructor create(info: pasminfo; smart: boolean); override;
  33. end;
  34. Tx86AoutGNUAssembler=class(TAoutGNUassembler)
  35. constructor create(info: pasminfo; smart: boolean); override;
  36. end;
  37. Tx86InstrWriter=class(TCPUInstrWriter)
  38. private
  39. procedure WriteReference(var ref : treference);
  40. procedure WriteOper(const o:toper);
  41. procedure WriteOper_jmp(const o:toper);
  42. protected
  43. fskipPopcountSuffix: boolean;
  44. { http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56656 }
  45. fNoInterUnitMovQ: boolean;
  46. public
  47. procedure WriteInstruction(hp: tai);override;
  48. end;
  49. implementation
  50. uses
  51. cutils,
  52. verbose,
  53. itcpugas,
  54. cgbase,
  55. aasmcpu;
  56. {****************************************************************************
  57. Tx86ATTAssembler
  58. ****************************************************************************}
  59. constructor Tx86ATTAssembler.create(info: pasminfo; smart: boolean);
  60. begin
  61. inherited;
  62. InstrWriter := Tx86InstrWriter.create(self);
  63. end;
  64. function TX86ATTAssembler.MakeCmdLine: TCmdStr;
  65. var
  66. FormatName : string;
  67. begin
  68. result:=Inherited MakeCmdLine;
  69. {$ifdef i386}
  70. case target_info.system of
  71. system_i386_go32v2:
  72. FormatName:='coff';
  73. system_i386_wdosx,
  74. system_i386_win32:
  75. FormatName:='win32';
  76. system_i386_embedded:
  77. FormatName:='obj';
  78. system_i386_linux,
  79. system_i386_beos:
  80. FormatName:='elf';
  81. system_i386_darwin:
  82. FormatName:='macho32';
  83. else
  84. FormatName:='elf';
  85. end;
  86. {$endif i386}
  87. {$ifdef x86_64}
  88. case target_info.system of
  89. system_x86_64_win64:
  90. FormatName:='win64';
  91. system_x86_64_darwin:
  92. FormatName:='macho64';
  93. system_x86_64_linux:
  94. FormatName:='elf64';
  95. else
  96. FormatName:='elf64';
  97. end;
  98. {$endif x86_64}
  99. Replace(result,'$FORMAT',FormatName);
  100. end;
  101. {****************************************************************************
  102. Tx86AppleGNUAssembler
  103. ****************************************************************************}
  104. constructor Tx86AppleGNUAssembler.create(info: pasminfo; smart: boolean);
  105. begin
  106. inherited;
  107. InstrWriter := Tx86InstrWriter.create(self);
  108. { Apple's assembler does not support a size suffix for popcount }
  109. Tx86InstrWriter(InstrWriter).fskipPopcountSuffix := true;
  110. { Apple's assembler is broken regarding some movq suffix handling }
  111. Tx86InstrWriter(InstrWriter).fNoInterUnitMovQ := true;
  112. end;
  113. {****************************************************************************
  114. Tx86AoutGNUAssembler
  115. ****************************************************************************}
  116. constructor Tx86AoutGNUAssembler.create(info: pasminfo; smart: boolean);
  117. begin
  118. inherited;
  119. InstrWriter := Tx86InstrWriter.create(self);
  120. end;
  121. {****************************************************************************
  122. Tx86InstrWriter
  123. ****************************************************************************}
  124. procedure Tx86InstrWriter.WriteReference(var ref : treference);
  125. begin
  126. with ref do
  127. begin
  128. { do we have a segment prefix ? }
  129. { These are probably not correctly handled under GAS }
  130. { should be replaced by coding the segment override }
  131. { directly! - DJGPP FAQ }
  132. if segment<>NR_NO then
  133. owner.writer.AsmWrite(gas_regname(segment)+':');
  134. if assigned(symbol) then
  135. owner.writer.AsmWrite(symbol.name);
  136. if assigned(relsymbol) then
  137. owner.writer.AsmWrite('-'+relsymbol.name);
  138. if ref.refaddr=addr_pic then
  139. begin
  140. { @GOT and @GOTPCREL references are only allowed for symbol alone,
  141. indexing, relsymbol or offset cannot be present. }
  142. if assigned(relsymbol) or (offset<>0) or (index<>NR_NO) then
  143. InternalError(2015011801);
  144. {$ifdef x86_64}
  145. if (base<>NR_RIP) then
  146. InternalError(2015011802);
  147. owner.writer.AsmWrite('@GOTPCREL');
  148. {$else x86_64}
  149. owner.writer.AsmWrite('@GOT');
  150. {$endif x86_64}
  151. end;
  152. if offset<0 then
  153. owner.writer.AsmWrite(tostr(offset))
  154. else
  155. if (offset>0) then
  156. begin
  157. if assigned(symbol) then
  158. owner.writer.AsmWrite('+'+tostr(offset))
  159. else
  160. owner.writer.AsmWrite(tostr(offset));
  161. end
  162. else if (index=NR_NO) and (base=NR_NO) and (not assigned(symbol)) then
  163. owner.writer.AsmWrite('0');
  164. if (index<>NR_NO) and (base=NR_NO) then
  165. begin
  166. if scalefactor in [0,1] then
  167. { Switching index to base position gives shorter
  168. assembler instructions }
  169. begin
  170. owner.writer.AsmWrite('('+gas_regname(index)+')');
  171. end
  172. else
  173. begin
  174. owner.writer.AsmWrite('(,'+gas_regname(index));
  175. if scalefactor<>0 then
  176. owner.writer.AsmWrite(','+tostr(scalefactor)+')')
  177. else
  178. owner.writer.AsmWrite(')');
  179. end;
  180. end
  181. else
  182. if (index=NR_NO) and (base<>NR_NO) then
  183. owner.writer.AsmWrite('('+gas_regname(base)+')')
  184. else
  185. if (index<>NR_NO) and (base<>NR_NO) then
  186. begin
  187. owner.writer.AsmWrite('('+gas_regname(base)+','+gas_regname(index));
  188. if scalefactor<>0 then
  189. owner.writer.AsmWrite(','+tostr(scalefactor));
  190. owner.writer.AsmWrite(')');
  191. end;
  192. end;
  193. end;
  194. procedure Tx86InstrWriter.WriteOper(const o:toper);
  195. begin
  196. case o.typ of
  197. top_reg :
  198. owner.writer.AsmWrite(gas_regname(o.reg));
  199. top_ref :
  200. if o.ref^.refaddr in [addr_no,addr_pic,addr_pic_no_got] then
  201. WriteReference(o.ref^)
  202. else
  203. begin
  204. owner.writer.AsmWrite('$');
  205. if assigned(o.ref^.symbol) then
  206. owner.writer.AsmWrite(o.ref^.symbol.name);
  207. if o.ref^.offset>0 then
  208. owner.writer.AsmWrite('+'+tostr(o.ref^.offset))
  209. else
  210. if o.ref^.offset<0 then
  211. owner.writer.AsmWrite(tostr(o.ref^.offset))
  212. else
  213. if not(assigned(o.ref^.symbol)) then
  214. owner.writer.AsmWrite('0');
  215. end;
  216. top_const :
  217. owner.writer.AsmWrite('$'+tostr(o.val));
  218. else
  219. internalerror(10001);
  220. end;
  221. end;
  222. procedure Tx86InstrWriter.WriteOper_jmp(const o:toper);
  223. begin
  224. case o.typ of
  225. top_reg :
  226. owner.writer.AsmWrite('*'+gas_regname(o.reg));
  227. top_ref :
  228. begin
  229. if o.ref^.refaddr in [addr_no,addr_pic_no_got] then
  230. begin
  231. owner.writer.AsmWrite('*');
  232. WriteReference(o.ref^);
  233. end
  234. else
  235. begin
  236. owner.writer.AsmWrite(o.ref^.symbol.name);
  237. if o.ref^.refaddr=addr_pic then
  238. owner.writer.AsmWrite('@PLT');
  239. if o.ref^.offset>0 then
  240. owner.writer.AsmWrite('+'+tostr(o.ref^.offset))
  241. else
  242. if o.ref^.offset<0 then
  243. owner.writer.AsmWrite(tostr(o.ref^.offset));
  244. end;
  245. end;
  246. top_const :
  247. owner.writer.AsmWrite(tostr(o.val));
  248. else
  249. internalerror(10001);
  250. end;
  251. end;
  252. procedure Tx86InstrWriter.WriteInstruction(hp: tai);
  253. var
  254. op : tasmop;
  255. calljmp : boolean;
  256. i : integer;
  257. sreg : string;
  258. begin
  259. if hp.typ <> ait_instruction then
  260. exit;
  261. taicpu(hp).SetOperandOrder(op_att);
  262. op:=taicpu(hp).opcode;
  263. calljmp:=is_calljmp(op);
  264. // BUGFIX GAS-assembler
  265. // Intel "Intel 64 and IA-32 Architectures Software Developers manual 12/2011
  266. // Intel: VCVTDQ2PD YMMREG, YMMREG/mem128 ((intel syntax))
  267. // GAS: VCVTDQ2PD YMMREG, XMMREG/mem128 ((intel syntax))
  268. if (op = A_VCVTDQ2PD) and
  269. (taicpu(hp).ops = 2) and
  270. (taicpu(hp).oper[0]^.typ = top_reg) and
  271. (taicpu(hp).oper[1]^.typ = top_reg) then
  272. begin
  273. if ((taicpu(hp).oper[0]^.ot and OT_YMMREG) = OT_YMMREG) and
  274. ((taicpu(hp).oper[1]^.ot and OT_YMMREG) = OT_YMMREG) then
  275. begin
  276. // change registertype in oper[0] from OT_YMMREG to OT_XMMREG
  277. taicpu(hp).oper[0]^.ot := taicpu(hp).oper[0]^.ot and not(OT_YMMREG) or OT_XMMREG;
  278. sreg := gas_regname(taicpu(hp).oper[0]^.reg);
  279. if (copy(sreg, 1, 2) = '%y') or
  280. (copy(sreg, 1, 2) = '%Y') then
  281. taicpu(hp).oper[0]^.reg := gas_regnum_search('%x' + copy(sreg, 3, length(sreg) - 2));
  282. end;
  283. end;
  284. { see fNoInterUnitMovQ declaration comment }
  285. if fNoInterUnitMovQ then
  286. begin
  287. if ((op=A_MOVQ) or
  288. (op=A_VMOVQ)) and
  289. (((taicpu(hp).oper[0]^.typ=top_reg) and
  290. (getregtype(taicpu(hp).oper[0]^.reg)=R_INTREGISTER)) or
  291. ((taicpu(hp).oper[1]^.typ=top_reg) and
  292. (getregtype(taicpu(hp).oper[1]^.reg)=R_INTREGISTER))) then
  293. begin
  294. if op=A_MOVQ then
  295. op:=A_MOVD
  296. else
  297. op:=A_VMOVD;
  298. taicpu(hp).opcode:=op;
  299. end;
  300. end;
  301. owner.writer.AsmWrite(#9);
  302. { movsd should not be translated to movsl when there
  303. are (xmm) arguments }
  304. if (op=A_MOVSD) and (taicpu(hp).ops>0) then
  305. owner.writer.AsmWrite('movsd')
  306. else
  307. owner.writer.AsmWrite(gas_op2str[op]);
  308. owner.writer.AsmWrite(cond2str[taicpu(hp).condition]);
  309. { suffix needed ? fnstsw,fldcw don't support suffixes
  310. with binutils 2.9.5 under linux }
  311. { if (Taicpu(hp).oper[0]^.typ=top_reg) and
  312. (Taicpu(hp).oper[0]^.reg.enum>lastreg) then
  313. internalerror(200301081);}
  314. if (not calljmp) and
  315. (gas_needsuffix[op]<>AttSufNONE) and
  316. (op<>A_FNSTSW) and
  317. (op<>A_FSTSW) and
  318. (op<>A_FNSTCW) and
  319. (op<>A_FSTCW) and
  320. (op<>A_FLDCW) and
  321. (not fskipPopcountSuffix or
  322. (op<>A_POPCNT)) and
  323. not(
  324. (taicpu(hp).ops<>0) and
  325. (taicpu(hp).oper[0]^.typ=top_reg) and
  326. (getregtype(taicpu(hp).oper[0]^.reg)=R_FPUREGISTER)
  327. ) then
  328. begin
  329. if gas_needsuffix[op] = AttSufMM then
  330. begin
  331. for i:=0 to taicpu(hp).ops-1 do
  332. begin
  333. if (taicpu(hp).oper[i]^.typ = top_ref) then
  334. begin
  335. case taicpu(hp).oper[i]^.ot and OT_SIZE_MASK of
  336. OT_BITS32: begin
  337. owner.writer.AsmWrite(gas_opsize2str[S_L]);
  338. break;
  339. end;
  340. OT_BITS64: begin
  341. owner.writer.AsmWrite(gas_opsize2str[S_Q]);
  342. break;
  343. end;
  344. OT_BITS128: begin
  345. owner.writer.AsmWrite(gas_opsize2str[S_XMM]);
  346. break;
  347. end;
  348. OT_BITS256: begin
  349. owner.writer.AsmWrite(gas_opsize2str[S_YMM]);
  350. break;
  351. end;
  352. 0: begin
  353. owner.writer.AsmWrite(gas_opsize2str[taicpu(hp).opsize]);
  354. break;
  355. end;
  356. end;
  357. end;
  358. end;
  359. end
  360. else owner.writer.AsmWrite(gas_opsize2str[taicpu(hp).opsize]);
  361. end;
  362. { process operands }
  363. if taicpu(hp).ops<>0 then
  364. begin
  365. if calljmp then
  366. begin
  367. owner.writer.AsmWrite(#9);
  368. WriteOper_jmp(taicpu(hp).oper[0]^);
  369. end
  370. else
  371. begin
  372. for i:=0 to taicpu(hp).ops-1 do
  373. begin
  374. if i=0 then
  375. owner.writer.AsmWrite(#9)
  376. else
  377. owner.writer.AsmWrite(',');
  378. WriteOper(taicpu(hp).oper[i]^);
  379. end;
  380. end;
  381. end;
  382. owner.writer.AsmLn;
  383. end;
  384. {*****************************************************************************
  385. Initialize
  386. *****************************************************************************}
  387. const
  388. {$ifdef x86_64}
  389. as_x86_64_as_info : tasminfo =
  390. (
  391. id : as_gas;
  392. idtxt : 'AS';
  393. asmbin : 'as';
  394. asmcmd : '--64 -o $OBJ $EXTRAOPT $ASM';
  395. supported_targets : [system_x86_64_linux,system_x86_64_freebsd,
  396. system_x86_64_win64,system_x86_64_embedded,
  397. system_x86_64_openbsd,system_x86_64_netbsd,
  398. system_x86_64_dragonfly,system_x86_64_aros];
  399. flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
  400. labelprefix : '.L';
  401. comment : '# ';
  402. dollarsign: '$';
  403. );
  404. as_x86_64_yasm_info : tasminfo =
  405. (
  406. id : as_yasm;
  407. idtxt : 'YASM';
  408. asmbin : 'yasm';
  409. asmcmd : '-a x86 -p gas -f $FORMAT -o $OBJ $EXTRAOPT $ASM';
  410. supported_targets : [system_x86_64_linux,system_x86_64_freebsd,system_x86_64_win64,system_x86_64_embedded];
  411. flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
  412. labelprefix : '.L';
  413. comment : '# ';
  414. dollarsign: '$';
  415. );
  416. as_x86_64_gas_info : tasminfo =
  417. (
  418. id : as_ggas;
  419. idtxt : 'GAS';
  420. asmbin : 'gas';
  421. asmcmd : '--64 -o $OBJ $EXTRAOPT $ASM';
  422. supported_targets : [system_x86_64_solaris];
  423. flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
  424. labelprefix : '.L';
  425. comment : '# ';
  426. dollarsign: '$';
  427. );
  428. as_x86_64_gas_darwin_info : tasminfo =
  429. (
  430. id : as_darwin;
  431. idtxt : 'AS-Darwin';
  432. asmbin : 'as';
  433. asmcmd : '-o $OBJ $EXTRAOPT $ASM -arch x86_64';
  434. supported_targets : [system_x86_64_darwin,system_x86_64_iphonesim];
  435. flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
  436. labelprefix : 'L';
  437. comment : '# ';
  438. dollarsign: '$';
  439. );
  440. {$else x86_64}
  441. as_i386_as_info : tasminfo =
  442. (
  443. id : as_gas;
  444. idtxt : 'AS';
  445. asmbin : 'as';
  446. asmcmd : '--32 -o $OBJ $EXTRAOPT $ASM';
  447. supported_targets : [system_i386_GO32V2,system_i386_linux,system_i386_Win32,system_i386_freebsd,system_i386_solaris,system_i386_beos,
  448. system_i386_netbsd,system_i386_Netware,system_i386_qnx,system_i386_wdosx,system_i386_openbsd,
  449. system_i386_netwlibc,system_i386_wince,system_i386_embedded,system_i386_symbian,system_i386_haiku,system_x86_6432_linux,
  450. system_i386_nativent,system_i386_android,system_i386_aros];
  451. flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
  452. labelprefix : '.L';
  453. comment : '# ';
  454. dollarsign: '$';
  455. );
  456. as_i386_yasm_info : tasminfo =
  457. (
  458. id : as_yasm;
  459. idtxt : 'YASM';
  460. asmbin : 'yasm';
  461. asmcmd : '-a x86 -p gas -f $FORMAT -o $OBJ $EXTRAOPT $ASM';
  462. supported_targets : [system_i386_GO32V2,system_i386_linux,system_i386_Win32,system_i386_freebsd,system_i386_solaris,system_i386_beos,
  463. system_i386_netbsd,system_i386_Netware,system_i386_qnx,system_i386_wdosx,system_i386_openbsd,
  464. system_i386_netwlibc,system_i386_wince,system_i386_embedded,system_i386_symbian,system_i386_haiku,system_x86_6432_linux,
  465. system_i386_nativent];
  466. flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
  467. labelprefix : '.L';
  468. comment : '# ';
  469. dollarsign: '$';
  470. );
  471. as_i386_as_aout_info : tasminfo =
  472. (
  473. id : as_i386_as_aout;
  474. idtxt : 'AS_AOUT';
  475. asmbin : 'as';
  476. asmcmd : '-o $OBJ $EXTRAOPT $ASM';
  477. supported_targets : [system_i386_linux,system_i386_OS2,system_i386_freebsd,system_i386_netbsd,system_i386_openbsd,system_i386_EMX,system_i386_embedded];
  478. flags : [af_needar,af_stabs_use_function_absolute_addresses];
  479. labelprefix : 'L';
  480. comment : '# ';
  481. dollarsign: '$';
  482. );
  483. as_i386_gas_darwin_info : tasminfo =
  484. (
  485. id : as_darwin;
  486. idtxt : 'AS-Darwin';
  487. asmbin : 'as';
  488. asmcmd : '-o $OBJ $EXTRAOPT $ASM -arch i386';
  489. supported_targets : [system_i386_darwin,system_i386_iphonesim];
  490. flags : [af_needar,af_smartlink_sections,af_supports_dwarf,af_stabs_use_function_absolute_addresses];
  491. labelprefix : 'L';
  492. comment : '# ';
  493. dollarsign: '$';
  494. );
  495. as_i386_gas_info : tasminfo =
  496. (
  497. id : as_ggas;
  498. idtxt : 'GAS';
  499. asmbin : 'gas';
  500. asmcmd : '--32 -o $OBJ $EXTRAOPT $ASM';
  501. supported_targets : [system_i386_GO32V2,system_i386_linux,system_i386_Win32,system_i386_freebsd,system_i386_solaris,system_i386_beos,
  502. system_i386_netbsd,system_i386_Netware,system_i386_qnx,system_i386_wdosx,system_i386_openbsd,
  503. system_i386_netwlibc,system_i386_wince,system_i386_embedded,system_i386_symbian,system_i386_haiku,
  504. system_x86_6432_linux,system_i386_android];
  505. flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
  506. labelprefix : '.L';
  507. comment : '# ';
  508. dollarsign: '$';
  509. );
  510. {$endif x86_64}
  511. initialization
  512. {$ifdef x86_64}
  513. RegisterAssembler(as_x86_64_as_info,Tx86ATTAssembler);
  514. RegisterAssembler(as_x86_64_yasm_info,Tx86ATTAssembler);
  515. RegisterAssembler(as_x86_64_gas_info,Tx86ATTAssembler);
  516. RegisterAssembler(as_x86_64_gas_darwin_info,Tx86AppleGNUAssembler);
  517. {$else x86_64}
  518. RegisterAssembler(as_i386_as_info,Tx86ATTAssembler);
  519. RegisterAssembler(as_i386_gas_info,Tx86ATTAssembler);
  520. RegisterAssembler(as_i386_yasm_info,Tx86ATTAssembler);
  521. RegisterAssembler(as_i386_gas_darwin_info,Tx86AppleGNUAssembler);
  522. RegisterAssembler(as_i386_as_aout_info,Tx86AoutGNUAssembler);
  523. {$endif x86_64}
  524. end.