agx86att.pas 15 KB

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