agx86att.pas 8.6 KB

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