agx86att.pas 11 KB

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