agx86att.pas 19 KB

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