agx86att.pas 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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,assemble,aggas;
  26. type
  27. Tx86ATTAssembler=class(TGNUassembler)
  28. private
  29. procedure WriteReference(var ref : treference);
  30. procedure WriteOper(const o:toper);
  31. procedure WriteOper_jmp(const o:toper);
  32. public
  33. procedure WriteInstruction(hp: tai);override;
  34. end;
  35. implementation
  36. uses
  37. cutils,systems,
  38. verbose,
  39. itcpugas,
  40. cgbase,
  41. aasmcpu;
  42. {****************************************************************************
  43. TX86ATTASMOUTPUT
  44. ****************************************************************************}
  45. procedure Tx86AttAssembler.WriteReference(var ref : treference);
  46. begin
  47. with ref do
  48. begin
  49. { have we a segment prefix ? }
  50. { These are probably not correctly handled under GAS }
  51. { should be replaced by coding the segment override }
  52. { directly! - DJGPP FAQ }
  53. if segment<>NR_NO then
  54. AsmWrite(gas_regname(segment)+':');
  55. if assigned(symbol) then
  56. AsmWrite(symbol.name);
  57. if ref.refaddr=addr_pic then
  58. {$ifdef x86_64}
  59. AsmWrite('@GOTPCREL');
  60. {$else x86_64}
  61. AsmWrite('@GOT');
  62. {$endif x86_64}
  63. if offset<0 then
  64. AsmWrite(tostr(offset))
  65. else
  66. if (offset>0) then
  67. begin
  68. if assigned(symbol) then
  69. AsmWrite('+'+tostr(offset))
  70. else
  71. AsmWrite(tostr(offset));
  72. end
  73. else if (index=NR_NO) and (base=NR_NO) and (not assigned(symbol)) then
  74. AsmWrite('0');
  75. if (index<>NR_NO) and (base=NR_NO) then
  76. begin
  77. AsmWrite('(,'+gas_regname(index));
  78. if scalefactor<>0 then
  79. AsmWrite(','+tostr(scalefactor)+')')
  80. else
  81. AsmWrite(')');
  82. end
  83. else
  84. if (index=NR_NO) and (base<>NR_NO) then
  85. AsmWrite('('+gas_regname(base)+')')
  86. else
  87. if (index<>NR_NO) and (base<>NR_NO) then
  88. begin
  89. AsmWrite('('+gas_regname(base)+','+gas_regname(index));
  90. if scalefactor<>0 then
  91. AsmWrite(','+tostr(scalefactor));
  92. AsmWrite(')');
  93. end;
  94. end;
  95. end;
  96. procedure Tx86AttAssembler.WriteOper(const o:toper);
  97. begin
  98. case o.typ of
  99. top_reg :
  100. AsmWrite(gas_regname(o.reg));
  101. top_ref :
  102. if o.ref^.refaddr in [addr_no,addr_pic] then
  103. WriteReference(o.ref^)
  104. else
  105. begin
  106. AsmWrite('$');
  107. if assigned(o.ref^.symbol) then
  108. AsmWrite(o.ref^.symbol.name);
  109. if o.ref^.offset>0 then
  110. AsmWrite('+'+tostr(o.ref^.offset))
  111. else
  112. if o.ref^.offset<0 then
  113. AsmWrite(tostr(o.ref^.offset))
  114. else
  115. if not(assigned(o.ref^.symbol)) then
  116. AsmWrite('0');
  117. end;
  118. top_const :
  119. AsmWrite('$'+tostr(o.val));
  120. else
  121. internalerror(10001);
  122. end;
  123. end;
  124. procedure Tx86AttAssembler.WriteOper_jmp(const o:toper);
  125. begin
  126. case o.typ of
  127. top_reg :
  128. AsmWrite('*'+gas_regname(o.reg));
  129. top_ref :
  130. begin
  131. if o.ref^.refaddr=addr_no then
  132. begin
  133. AsmWrite('*');
  134. WriteReference(o.ref^);
  135. end
  136. else
  137. begin
  138. AsmWrite(o.ref^.symbol.name);
  139. if o.ref^.offset>0 then
  140. AsmWrite('+'+tostr(o.ref^.offset))
  141. else
  142. if o.ref^.offset<0 then
  143. AsmWrite(tostr(o.ref^.offset));
  144. end;
  145. end;
  146. top_const :
  147. AsmWrite(tostr(o.val));
  148. else
  149. internalerror(10001);
  150. end;
  151. end;
  152. procedure Tx86AttAssembler.WriteInstruction(hp: tai);
  153. var
  154. op : tasmop;
  155. calljmp : boolean;
  156. i : integer;
  157. begin
  158. if hp.typ <> ait_instruction then
  159. exit;
  160. taicpu(hp).SetOperandOrder(op_att);
  161. op:=taicpu(hp).opcode;
  162. calljmp:=is_calljmp(op);
  163. AsmWrite(#9);
  164. { movsd should not be translated to movsl when there
  165. are (xmm) arguments }
  166. if (op=A_MOVSD) and (taicpu(hp).ops>0) then
  167. AsmWrite('movsd')
  168. else
  169. AsmWrite(gas_op2str[op]);
  170. AsmWrite(cond2str[taicpu(hp).condition]);
  171. { suffix needed ? fnstsw,fldcw don't support suffixes
  172. with binutils 2.9.5 under linux }
  173. { if (Taicpu(hp).oper[0]^.typ=top_reg) and
  174. (Taicpu(hp).oper[0]^.reg.enum>lastreg) then
  175. internalerror(200301081);}
  176. if (not calljmp) and
  177. (gas_needsuffix[op]<>AttSufNONE) and
  178. (op<>A_FNSTSW) and
  179. (op<>A_FSTSW) and
  180. (op<>A_FNSTCW) and
  181. (op<>A_FSTCW) and
  182. (op<>A_FLDCW) and
  183. not(
  184. (taicpu(hp).ops<>0) and
  185. (taicpu(hp).oper[0]^.typ=top_reg) and
  186. (getregtype(taicpu(hp).oper[0]^.reg)=R_FPUREGISTER)
  187. ) then
  188. AsmWrite(gas_opsize2str[taicpu(hp).opsize]);
  189. { process operands }
  190. if taicpu(hp).ops<>0 then
  191. begin
  192. if calljmp then
  193. begin
  194. AsmWrite(#9);
  195. WriteOper_jmp(taicpu(hp).oper[0]^);
  196. end
  197. else
  198. begin
  199. for i:=0 to taicpu(hp).ops-1 do
  200. begin
  201. if i=0 then
  202. AsmWrite(#9)
  203. else
  204. AsmWrite(',');
  205. WriteOper(taicpu(hp).oper[i]^);
  206. end;
  207. end;
  208. end;
  209. AsmLn;
  210. end;
  211. {*****************************************************************************
  212. Initialize
  213. *****************************************************************************}
  214. const
  215. {$ifdef x86_64}
  216. as_x86_64_as_info : tasminfo =
  217. (
  218. id : as_gas;
  219. idtxt : 'AS';
  220. asmbin : 'as';
  221. asmcmd : '-o $OBJ $ASM';
  222. supported_target : system_any;
  223. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  224. labelprefix : '.L';
  225. comment : '# ';
  226. );
  227. {$else x86_64}
  228. as_i386_as_info : tasminfo =
  229. (
  230. id : as_gas;
  231. idtxt : 'AS';
  232. asmbin : 'as';
  233. asmcmd : '-o $OBJ $ASM';
  234. supported_target : system_any;
  235. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  236. labelprefix : '.L';
  237. comment : '# ';
  238. );
  239. as_i386_as_aout_info : tasminfo =
  240. (
  241. id : as_i386_as_aout;
  242. idtxt : 'AS_AOUT';
  243. asmbin : 'as';
  244. asmcmd : '-o $OBJ $ASM';
  245. supported_target : system_any;
  246. flags : [af_allowdirect,af_needar];
  247. labelprefix : 'L';
  248. comment : '# ';
  249. );
  250. {$endif x86_64}
  251. initialization
  252. {$ifdef x86_64}
  253. RegisterAssembler(as_x86_64_as_info,Tx86ATTAssembler);
  254. {$else x86_64}
  255. RegisterAssembler(as_i386_as_info,Tx86ATTAssembler);
  256. RegisterAssembler(as_i386_as_aout_info,Tx86ATTAssembler);
  257. {$endif x86_64}
  258. end.