agx86att.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. 0: begin
  314. owner.AsmWrite(gas_opsize2str[taicpu(hp).opsize]);
  315. break;
  316. end;
  317. end;
  318. end;
  319. end;
  320. end
  321. else owner.AsmWrite(gas_opsize2str[taicpu(hp).opsize]);
  322. end;
  323. { process operands }
  324. if taicpu(hp).ops<>0 then
  325. begin
  326. if calljmp then
  327. begin
  328. owner.AsmWrite(#9);
  329. WriteOper_jmp(taicpu(hp).oper[0]^);
  330. end
  331. else
  332. begin
  333. for i:=0 to taicpu(hp).ops-1 do
  334. begin
  335. if i=0 then
  336. owner.AsmWrite(#9)
  337. else
  338. owner.AsmWrite(',');
  339. WriteOper(taicpu(hp).oper[i]^);
  340. end;
  341. end;
  342. end;
  343. owner.AsmLn;
  344. if need_second_mov then
  345. begin
  346. taicpu(hp).oper[0]^.val:=0;
  347. inc(taicpu(hp).oper[1]^.ref^.offset,4);
  348. WriteInstruction(hp);
  349. end;
  350. end;
  351. {*****************************************************************************
  352. Initialize
  353. *****************************************************************************}
  354. const
  355. {$ifdef x86_64}
  356. as_x86_64_as_info : tasminfo =
  357. (
  358. id : as_gas;
  359. idtxt : 'AS';
  360. asmbin : 'as';
  361. asmcmd : '--64 -o $OBJ $ASM';
  362. supported_targets : [system_x86_64_linux,system_x86_64_freebsd,
  363. system_x86_64_win64,system_x86_64_embedded,
  364. system_x86_64_openbsd,system_x86_64_netbsd];
  365. flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
  366. labelprefix : '.L';
  367. comment : '# ';
  368. dollarsign: '$';
  369. );
  370. as_x86_64_gas_info : tasminfo =
  371. (
  372. id : as_ggas;
  373. idtxt : 'GAS';
  374. asmbin : 'gas';
  375. asmcmd : '--64 -o $OBJ $ASM';
  376. supported_targets : [system_x86_64_solaris];
  377. flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
  378. labelprefix : '.L';
  379. comment : '# ';
  380. dollarsign: '$';
  381. );
  382. as_x86_64_gas_darwin_info : tasminfo =
  383. (
  384. id : as_darwin;
  385. idtxt : 'AS-Darwin';
  386. asmbin : 'as';
  387. asmcmd : '-o $OBJ $ASM -arch x86_64';
  388. supported_targets : [system_x86_64_darwin];
  389. flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
  390. labelprefix : 'L';
  391. comment : '# ';
  392. dollarsign: '$';
  393. );
  394. {$else x86_64}
  395. as_i386_as_info : tasminfo =
  396. (
  397. id : as_gas;
  398. idtxt : 'AS';
  399. asmbin : 'as';
  400. asmcmd : '--32 -o $OBJ $ASM';
  401. supported_targets : [system_i386_GO32V2,system_i386_linux,system_i386_Win32,system_i386_freebsd,system_i386_solaris,system_i386_beos,
  402. system_i386_netbsd,system_i386_Netware,system_i386_qnx,system_i386_wdosx,system_i386_openbsd,
  403. system_i386_netwlibc,system_i386_wince,system_i386_embedded,system_i386_symbian,system_i386_haiku,system_x86_6432_linux,
  404. system_i386_nativent,system_i386_android];
  405. flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
  406. labelprefix : '.L';
  407. comment : '# ';
  408. dollarsign: '$';
  409. );
  410. as_i386_as_aout_info : tasminfo =
  411. (
  412. id : as_i386_as_aout;
  413. idtxt : 'AS_AOUT';
  414. asmbin : 'as';
  415. asmcmd : '-o $OBJ $ASM';
  416. supported_targets : [system_i386_linux,system_i386_OS2,system_i386_freebsd,system_i386_netbsd,system_i386_openbsd,system_i386_EMX,system_i386_embedded];
  417. flags : [af_needar,af_stabs_use_function_absolute_addresses];
  418. labelprefix : 'L';
  419. comment : '# ';
  420. dollarsign: '$';
  421. );
  422. as_i386_gas_darwin_info : tasminfo =
  423. (
  424. id : as_darwin;
  425. idtxt : 'AS-Darwin';
  426. asmbin : 'as';
  427. asmcmd : '-o $OBJ $ASM -arch i386';
  428. supported_targets : [system_i386_darwin,system_i386_iphonesim];
  429. flags : [af_needar,af_smartlink_sections,af_supports_dwarf,af_stabs_use_function_absolute_addresses];
  430. labelprefix : 'L';
  431. comment : '# ';
  432. dollarsign: '$';
  433. );
  434. as_i386_gas_info : tasminfo =
  435. (
  436. id : as_ggas;
  437. idtxt : 'GAS';
  438. asmbin : 'gas';
  439. asmcmd : '--32 -o $OBJ $ASM';
  440. supported_targets : [system_i386_GO32V2,system_i386_linux,system_i386_Win32,system_i386_freebsd,system_i386_solaris,system_i386_beos,
  441. system_i386_netbsd,system_i386_Netware,system_i386_qnx,system_i386_wdosx,system_i386_openbsd,
  442. system_i386_netwlibc,system_i386_wince,system_i386_embedded,system_i386_symbian,system_i386_haiku,
  443. system_x86_6432_linux,system_i386_android];
  444. flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
  445. labelprefix : '.L';
  446. comment : '# ';
  447. dollarsign: '$';
  448. );
  449. {$endif x86_64}
  450. initialization
  451. {$ifdef x86_64}
  452. RegisterAssembler(as_x86_64_as_info,Tx86ATTAssembler);
  453. RegisterAssembler(as_x86_64_gas_info,Tx86ATTAssembler);
  454. RegisterAssembler(as_x86_64_gas_darwin_info,Tx86AppleGNUAssembler);
  455. {$else x86_64}
  456. RegisterAssembler(as_i386_as_info,Tx86ATTAssembler);
  457. RegisterAssembler(as_i386_gas_info,Tx86ATTAssembler);
  458. RegisterAssembler(as_i386_gas_darwin_info,Tx86AppleGNUAssembler);
  459. RegisterAssembler(as_i386_as_aout_info,Tx86AoutGNUAssembler);
  460. {$endif x86_64}
  461. end.