agx86att.pas 18 KB

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