agx86att.pas 14 KB

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