agx86att.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. begin
  211. if hp.typ <> ait_instruction then
  212. exit;
  213. taicpu(hp).SetOperandOrder(op_att);
  214. op:=taicpu(hp).opcode;
  215. calljmp:=is_calljmp(op);
  216. { constant values in the 32 bit range are sign-extended to
  217. 64 bits, but this is not what we want. PM 2010-09-02
  218. the fix consists of simply setting only the 4-byte register
  219. as the upper 4-bytes will be zeroed at the same time. }
  220. need_second_mov:=false;
  221. {$ifdef x86_64}
  222. if (op=A_MOV) and (taicpu(hp).opsize=S_Q) and
  223. (taicpu(hp).oper[0]^.typ = top_const) then
  224. begin
  225. val := taicpu(hp).oper[0]^.val;
  226. if (val > int64($7fffffff)) and (val < int64($100000000)) then
  227. begin
  228. owner.AsmWrite(target_asm.comment);
  229. owner.AsmWritePChar('Fix for Win64-GAS bug');
  230. owner.AsmLn;
  231. taicpu(hp).opsize:=S_L;
  232. if taicpu(hp).oper[1]^.typ = top_reg then
  233. setsubreg(taicpu(hp).oper[1]^.reg,R_SUBD)
  234. else if taicpu(hp).oper[1]^.typ = top_ref then
  235. need_second_mov:=true
  236. else
  237. internalerror(20100902);
  238. end;
  239. end;
  240. {$endif x86_64}
  241. owner.AsmWrite(#9);
  242. { movsd should not be translated to movsl when there
  243. are (xmm) arguments }
  244. if (op=A_MOVSD) and (taicpu(hp).ops>0) then
  245. owner.AsmWrite('movsd')
  246. else
  247. owner.AsmWrite(gas_op2str[op]);
  248. owner.AsmWrite(cond2str[taicpu(hp).condition]);
  249. { suffix needed ? fnstsw,fldcw don't support suffixes
  250. with binutils 2.9.5 under linux }
  251. { if (Taicpu(hp).oper[0]^.typ=top_reg) and
  252. (Taicpu(hp).oper[0]^.reg.enum>lastreg) then
  253. internalerror(200301081);}
  254. if (not calljmp) and
  255. (gas_needsuffix[op]<>AttSufNONE) and
  256. (op<>A_FNSTSW) and
  257. (op<>A_FSTSW) and
  258. (op<>A_FNSTCW) and
  259. (op<>A_FSTCW) and
  260. (op<>A_FLDCW) and
  261. (not fskipPopcountSuffix or
  262. (op<>A_POPCNT)) and
  263. not(
  264. (taicpu(hp).ops<>0) and
  265. (taicpu(hp).oper[0]^.typ=top_reg) and
  266. (getregtype(taicpu(hp).oper[0]^.reg)=R_FPUREGISTER)
  267. ) then
  268. owner.AsmWrite(gas_opsize2str[taicpu(hp).opsize]);
  269. { process operands }
  270. if taicpu(hp).ops<>0 then
  271. begin
  272. if calljmp then
  273. begin
  274. owner.AsmWrite(#9);
  275. WriteOper_jmp(taicpu(hp).oper[0]^);
  276. end
  277. else
  278. begin
  279. for i:=0 to taicpu(hp).ops-1 do
  280. begin
  281. if i=0 then
  282. owner.AsmWrite(#9)
  283. else
  284. owner.AsmWrite(',');
  285. WriteOper(taicpu(hp).oper[i]^);
  286. end;
  287. end;
  288. end;
  289. owner.AsmLn;
  290. if need_second_mov then
  291. begin
  292. taicpu(hp).oper[0]^.val:=0;
  293. inc(taicpu(hp).oper[1]^.ref^.offset,4);
  294. WriteInstruction(hp);
  295. end;
  296. end;
  297. {*****************************************************************************
  298. Initialize
  299. *****************************************************************************}
  300. const
  301. {$ifdef x86_64}
  302. as_x86_64_as_info : tasminfo =
  303. (
  304. id : as_gas;
  305. idtxt : 'AS';
  306. asmbin : 'as';
  307. asmcmd : '--64 -o $OBJ $ASM';
  308. supported_targets : [system_x86_64_linux,system_x86_64_freebsd,
  309. system_x86_64_win64,system_x86_64_embedded,
  310. system_x86_64_openbsd,system_x86_64_netbsd];
  311. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  312. labelprefix : '.L';
  313. comment : '# ';
  314. dollarsign: '$';
  315. );
  316. as_x86_64_gas_info : tasminfo =
  317. (
  318. id : as_ggas;
  319. idtxt : 'GAS';
  320. asmbin : 'gas';
  321. asmcmd : '--64 -o $OBJ $ASM';
  322. supported_targets : [system_x86_64_solaris];
  323. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  324. labelprefix : '.L';
  325. comment : '# ';
  326. dollarsign: '$';
  327. );
  328. as_x86_64_gas_darwin_info : tasminfo =
  329. (
  330. id : as_darwin;
  331. idtxt : 'AS-Darwin';
  332. asmbin : 'as';
  333. asmcmd : '-o $OBJ $ASM -arch x86_64';
  334. supported_targets : [system_x86_64_darwin];
  335. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  336. labelprefix : 'L';
  337. comment : '# ';
  338. dollarsign: '$';
  339. );
  340. {$else x86_64}
  341. as_i386_as_info : tasminfo =
  342. (
  343. id : as_gas;
  344. idtxt : 'AS';
  345. asmbin : 'as';
  346. asmcmd : '--32 -o $OBJ $ASM';
  347. supported_targets : [system_i386_GO32V2,system_i386_linux,system_i386_Win32,system_i386_freebsd,system_i386_solaris,system_i386_beos,
  348. system_i386_netbsd,system_i386_Netware,system_i386_qnx,system_i386_wdosx,system_i386_openbsd,
  349. system_i386_netwlibc,system_i386_wince,system_i386_embedded,system_i386_symbian,system_i386_haiku,system_x86_6432_linux,
  350. system_i386_nativent];
  351. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  352. labelprefix : '.L';
  353. comment : '# ';
  354. dollarsign: '$';
  355. );
  356. as_i386_as_aout_info : tasminfo =
  357. (
  358. id : as_i386_as_aout;
  359. idtxt : 'AS_AOUT';
  360. asmbin : 'as';
  361. asmcmd : '-o $OBJ $ASM';
  362. supported_targets : [system_i386_linux,system_i386_OS2,system_i386_freebsd,system_i386_netbsd,system_i386_openbsd,system_i386_EMX,system_i386_embedded];
  363. flags : [af_allowdirect,af_needar,af_stabs_use_function_absolute_addresses];
  364. labelprefix : 'L';
  365. comment : '# ';
  366. dollarsign: '$';
  367. );
  368. as_i386_gas_darwin_info : tasminfo =
  369. (
  370. id : as_darwin;
  371. idtxt : 'AS-Darwin';
  372. asmbin : 'as';
  373. asmcmd : '-o $OBJ $ASM -arch i386';
  374. supported_targets : [system_i386_darwin,system_i386_iphonesim];
  375. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf,af_stabs_use_function_absolute_addresses];
  376. labelprefix : 'L';
  377. comment : '# ';
  378. dollarsign: '$';
  379. );
  380. as_i386_gas_info : tasminfo =
  381. (
  382. id : as_ggas;
  383. idtxt : 'GAS';
  384. asmbin : 'gas';
  385. asmcmd : '--32 -o $OBJ $ASM';
  386. supported_targets : [system_i386_GO32V2,system_i386_linux,system_i386_Win32,system_i386_freebsd,system_i386_solaris,system_i386_beos,
  387. system_i386_netbsd,system_i386_Netware,system_i386_qnx,system_i386_wdosx,system_i386_openbsd,
  388. system_i386_netwlibc,system_i386_wince,system_i386_embedded,system_i386_symbian,system_i386_haiku,system_x86_6432_linux];
  389. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  390. labelprefix : '.L';
  391. comment : '# ';
  392. dollarsign: '$';
  393. );
  394. {$endif x86_64}
  395. initialization
  396. {$ifdef x86_64}
  397. RegisterAssembler(as_x86_64_as_info,Tx86ATTAssembler);
  398. RegisterAssembler(as_x86_64_gas_info,Tx86ATTAssembler);
  399. RegisterAssembler(as_x86_64_gas_darwin_info,Tx86AppleGNUAssembler);
  400. {$else x86_64}
  401. RegisterAssembler(as_i386_as_info,Tx86ATTAssembler);
  402. RegisterAssembler(as_i386_gas_info,Tx86ATTAssembler);
  403. RegisterAssembler(as_i386_gas_darwin_info,Tx86AppleGNUAssembler);
  404. RegisterAssembler(as_i386_as_aout_info,Tx86AoutGNUAssembler);
  405. {$endif x86_64}
  406. end.