agx86att.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. owner.AsmWrite('@GOTPCREL');
  95. {$else x86_64}
  96. owner.AsmWrite('@GOT');
  97. {$endif x86_64}
  98. if offset<0 then
  99. owner.AsmWrite(tostr(offset))
  100. else
  101. if (offset>0) then
  102. begin
  103. if assigned(symbol) then
  104. owner.AsmWrite('+'+tostr(offset))
  105. else
  106. owner.AsmWrite(tostr(offset));
  107. end
  108. else if (index=NR_NO) and (base=NR_NO) and (not assigned(symbol)) then
  109. owner.AsmWrite('0');
  110. if (index<>NR_NO) and (base=NR_NO) then
  111. begin
  112. owner.AsmWrite('(,'+gas_regname(index));
  113. if scalefactor<>0 then
  114. owner.AsmWrite(','+tostr(scalefactor)+')')
  115. else
  116. owner.AsmWrite(')');
  117. end
  118. else
  119. if (index=NR_NO) and (base<>NR_NO) then
  120. owner.AsmWrite('('+gas_regname(base)+')')
  121. else
  122. if (index<>NR_NO) and (base<>NR_NO) then
  123. begin
  124. owner.AsmWrite('('+gas_regname(base)+','+gas_regname(index));
  125. if scalefactor<>0 then
  126. owner.AsmWrite(','+tostr(scalefactor));
  127. owner.AsmWrite(')');
  128. end;
  129. end;
  130. end;
  131. procedure Tx86InstrWriter.WriteOper(const o:toper);
  132. begin
  133. case o.typ of
  134. top_reg :
  135. owner.AsmWrite(gas_regname(o.reg));
  136. top_ref :
  137. if o.ref^.refaddr in [addr_no,addr_pic,addr_pic_no_got] then
  138. WriteReference(o.ref^)
  139. else
  140. begin
  141. owner.AsmWrite('$');
  142. if assigned(o.ref^.symbol) then
  143. owner.AsmWrite(o.ref^.symbol.name);
  144. if o.ref^.offset>0 then
  145. owner.AsmWrite('+'+tostr(o.ref^.offset))
  146. else
  147. if o.ref^.offset<0 then
  148. owner.AsmWrite(tostr(o.ref^.offset))
  149. else
  150. if not(assigned(o.ref^.symbol)) then
  151. owner.AsmWrite('0');
  152. end;
  153. top_const :
  154. owner.AsmWrite('$'+tostr(o.val));
  155. else
  156. internalerror(10001);
  157. end;
  158. end;
  159. procedure Tx86InstrWriter.WriteOper_jmp(const o:toper);
  160. begin
  161. case o.typ of
  162. top_reg :
  163. owner.AsmWrite('*'+gas_regname(o.reg));
  164. top_ref :
  165. begin
  166. if o.ref^.refaddr=addr_no then
  167. begin
  168. owner.AsmWrite('*');
  169. WriteReference(o.ref^);
  170. end
  171. else
  172. begin
  173. owner.AsmWrite(o.ref^.symbol.name);
  174. if o.ref^.refaddr=addr_pic then
  175. owner.AsmWrite('@PLT');
  176. if o.ref^.offset>0 then
  177. owner.AsmWrite('+'+tostr(o.ref^.offset))
  178. else
  179. if o.ref^.offset<0 then
  180. owner.AsmWrite(tostr(o.ref^.offset));
  181. end;
  182. end;
  183. top_const :
  184. owner.AsmWrite(tostr(o.val));
  185. else
  186. internalerror(10001);
  187. end;
  188. end;
  189. procedure Tx86InstrWriter.WriteInstruction(hp: tai);
  190. var
  191. op : tasmop;
  192. calljmp : boolean;
  193. i : integer;
  194. begin
  195. if hp.typ <> ait_instruction then
  196. exit;
  197. taicpu(hp).SetOperandOrder(op_att);
  198. op:=taicpu(hp).opcode;
  199. calljmp:=is_calljmp(op);
  200. owner.AsmWrite(#9);
  201. { movsd should not be translated to movsl when there
  202. are (xmm) arguments }
  203. if (op=A_MOVSD) and (taicpu(hp).ops>0) then
  204. owner.AsmWrite('movsd')
  205. else
  206. owner.AsmWrite(gas_op2str[op]);
  207. owner.AsmWrite(cond2str[taicpu(hp).condition]);
  208. { suffix needed ? fnstsw,fldcw don't support suffixes
  209. with binutils 2.9.5 under linux }
  210. { if (Taicpu(hp).oper[0]^.typ=top_reg) and
  211. (Taicpu(hp).oper[0]^.reg.enum>lastreg) then
  212. internalerror(200301081);}
  213. if (not calljmp) and
  214. (gas_needsuffix[op]<>AttSufNONE) and
  215. (op<>A_FNSTSW) and
  216. (op<>A_FSTSW) and
  217. (op<>A_FNSTCW) and
  218. (op<>A_FSTCW) and
  219. (op<>A_FLDCW) and
  220. not(
  221. (taicpu(hp).ops<>0) and
  222. (taicpu(hp).oper[0]^.typ=top_reg) and
  223. (getregtype(taicpu(hp).oper[0]^.reg)=R_FPUREGISTER)
  224. ) then
  225. owner.AsmWrite(gas_opsize2str[taicpu(hp).opsize]);
  226. { process operands }
  227. if taicpu(hp).ops<>0 then
  228. begin
  229. if calljmp then
  230. begin
  231. owner.AsmWrite(#9);
  232. WriteOper_jmp(taicpu(hp).oper[0]^);
  233. end
  234. else
  235. begin
  236. for i:=0 to taicpu(hp).ops-1 do
  237. begin
  238. if i=0 then
  239. owner.AsmWrite(#9)
  240. else
  241. owner.AsmWrite(',');
  242. WriteOper(taicpu(hp).oper[i]^);
  243. end;
  244. end;
  245. end;
  246. owner.AsmLn;
  247. end;
  248. {*****************************************************************************
  249. Initialize
  250. *****************************************************************************}
  251. const
  252. {$ifdef x86_64}
  253. as_x86_64_as_info : tasminfo =
  254. (
  255. id : as_gas;
  256. idtxt : 'AS';
  257. asmbin : 'as';
  258. asmcmd : '--64 -o $OBJ $ASM';
  259. supported_targets : [system_x86_64_linux,system_x86_64_freebsd,system_x86_64_win64,system_x86_64_embedded];
  260. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  261. labelprefix : '.L';
  262. comment : '# ';
  263. );
  264. as_x86_64_gas_darwin_info : tasminfo =
  265. (
  266. id : as_darwin;
  267. idtxt : 'AS-Darwin';
  268. asmbin : 'as';
  269. asmcmd : '-o $OBJ $ASM -arch x86_64';
  270. supported_targets : [system_x86_64_darwin];
  271. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  272. labelprefix : 'L';
  273. comment : '# ';
  274. );
  275. {$else x86_64}
  276. as_i386_as_info : tasminfo =
  277. (
  278. id : as_gas;
  279. idtxt : 'AS';
  280. asmbin : 'as';
  281. asmcmd : '--32 -o $OBJ $ASM';
  282. supported_targets : [system_i386_GO32V2,system_i386_linux,system_i386_Win32,system_i386_freebsd,system_i386_solaris,system_i386_beos,
  283. system_i386_netbsd,system_i386_Netware,system_i386_qnx,system_i386_wdosx,system_i386_openbsd,
  284. system_i386_netwlibc,system_i386_wince,system_i386_embedded,system_i386_symbian,system_i386_haiku,system_x86_6432_linux];
  285. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  286. labelprefix : '.L';
  287. comment : '# ';
  288. );
  289. as_i386_as_aout_info : tasminfo =
  290. (
  291. id : as_i386_as_aout;
  292. idtxt : 'AS_AOUT';
  293. asmbin : 'as';
  294. asmcmd : '-o $OBJ $ASM';
  295. supported_targets : [system_i386_linux,system_i386_OS2,system_i386_freebsd,system_i386_netbsd,system_i386_openbsd,system_i386_EMX,system_i386_embedded];
  296. flags : [af_allowdirect,af_needar];
  297. labelprefix : 'L';
  298. comment : '# ';
  299. );
  300. as_i386_gas_darwin_info : tasminfo =
  301. (
  302. id : as_darwin;
  303. idtxt : 'AS-Darwin';
  304. asmbin : 'as';
  305. asmcmd : '-o $OBJ $ASM -arch i386';
  306. supported_targets : [system_i386_darwin];
  307. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  308. labelprefix : 'L';
  309. comment : '# ';
  310. );
  311. as_i386_gas_info : tasminfo =
  312. (
  313. id : as_ggas;
  314. idtxt : 'GAS';
  315. asmbin : 'gas';
  316. asmcmd : '--32 -o $OBJ $ASM';
  317. supported_targets : [system_i386_GO32V2,system_i386_linux,system_i386_Win32,system_i386_freebsd,system_i386_solaris,system_i386_beos,
  318. system_i386_netbsd,system_i386_Netware,system_i386_qnx,system_i386_wdosx,system_i386_openbsd,
  319. system_i386_netwlibc,system_i386_wince,system_i386_embedded,system_i386_symbian,system_i386_haiku,system_x86_6432_linux];
  320. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  321. labelprefix : '.L';
  322. comment : '# ';
  323. );
  324. {$endif x86_64}
  325. initialization
  326. {$ifdef x86_64}
  327. RegisterAssembler(as_x86_64_as_info,Tx86ATTAssembler);
  328. RegisterAssembler(as_x86_64_gas_darwin_info,Tx86AppleGNUAssembler);
  329. {$else x86_64}
  330. RegisterAssembler(as_i386_as_info,Tx86ATTAssembler);
  331. RegisterAssembler(as_i386_gas_info,Tx86ATTAssembler);
  332. RegisterAssembler(as_i386_gas_darwin_info,Tx86AppleGNUAssembler);
  333. RegisterAssembler(as_i386_as_aout_info,Tx86AoutGNUAssembler);
  334. {$endif x86_64}
  335. end.