agx86att.pas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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^.refaddr=addr_pic then
  140. AsmWrite('@PLT');
  141. if o.ref^.offset>0 then
  142. AsmWrite('+'+tostr(o.ref^.offset))
  143. else
  144. if o.ref^.offset<0 then
  145. AsmWrite(tostr(o.ref^.offset));
  146. end;
  147. end;
  148. top_const :
  149. AsmWrite(tostr(o.val));
  150. else
  151. internalerror(10001);
  152. end;
  153. end;
  154. procedure Tx86AttAssembler.WriteInstruction(hp: tai);
  155. var
  156. op : tasmop;
  157. calljmp : boolean;
  158. i : integer;
  159. begin
  160. if hp.typ <> ait_instruction then
  161. exit;
  162. taicpu(hp).SetOperandOrder(op_att);
  163. op:=taicpu(hp).opcode;
  164. calljmp:=is_calljmp(op);
  165. AsmWrite(#9);
  166. { movsd should not be translated to movsl when there
  167. are (xmm) arguments }
  168. if (op=A_MOVSD) and (taicpu(hp).ops>0) then
  169. AsmWrite('movsd')
  170. else
  171. AsmWrite(gas_op2str[op]);
  172. AsmWrite(cond2str[taicpu(hp).condition]);
  173. { suffix needed ? fnstsw,fldcw don't support suffixes
  174. with binutils 2.9.5 under linux }
  175. { if (Taicpu(hp).oper[0]^.typ=top_reg) and
  176. (Taicpu(hp).oper[0]^.reg.enum>lastreg) then
  177. internalerror(200301081);}
  178. if (not calljmp) and
  179. (gas_needsuffix[op]<>AttSufNONE) and
  180. (op<>A_FNSTSW) and
  181. (op<>A_FSTSW) and
  182. (op<>A_FNSTCW) and
  183. (op<>A_FSTCW) and
  184. (op<>A_FLDCW) and
  185. not(
  186. (taicpu(hp).ops<>0) and
  187. (taicpu(hp).oper[0]^.typ=top_reg) and
  188. (getregtype(taicpu(hp).oper[0]^.reg)=R_FPUREGISTER)
  189. ) then
  190. AsmWrite(gas_opsize2str[taicpu(hp).opsize]);
  191. { process operands }
  192. if taicpu(hp).ops<>0 then
  193. begin
  194. if calljmp then
  195. begin
  196. AsmWrite(#9);
  197. WriteOper_jmp(taicpu(hp).oper[0]^);
  198. end
  199. else
  200. begin
  201. for i:=0 to taicpu(hp).ops-1 do
  202. begin
  203. if i=0 then
  204. AsmWrite(#9)
  205. else
  206. AsmWrite(',');
  207. WriteOper(taicpu(hp).oper[i]^);
  208. end;
  209. end;
  210. end;
  211. AsmLn;
  212. end;
  213. {*****************************************************************************
  214. Initialize
  215. *****************************************************************************}
  216. const
  217. {$ifdef x86_64}
  218. as_x86_64_as_info : tasminfo =
  219. (
  220. id : as_gas;
  221. idtxt : 'AS';
  222. asmbin : 'as';
  223. asmcmd : '--64 -o $OBJ $ASM';
  224. supported_target : system_any;
  225. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  226. labelprefix : '.L';
  227. comment : '# ';
  228. );
  229. {$else x86_64}
  230. as_i386_as_info : tasminfo =
  231. (
  232. id : as_gas;
  233. idtxt : 'AS';
  234. asmbin : 'as';
  235. asmcmd : '--32 -o $OBJ $ASM';
  236. supported_target : system_any;
  237. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  238. labelprefix : '.L';
  239. comment : '# ';
  240. );
  241. as_i386_as_aout_info : tasminfo =
  242. (
  243. id : as_i386_as_aout;
  244. idtxt : 'AS_AOUT';
  245. asmbin : 'as';
  246. asmcmd : '-o $OBJ $ASM';
  247. supported_target : system_any;
  248. flags : [af_allowdirect,af_needar];
  249. labelprefix : 'L';
  250. comment : '# ';
  251. );
  252. {$endif x86_64}
  253. initialization
  254. {$ifdef x86_64}
  255. RegisterAssembler(as_x86_64_as_info,Tx86ATTAssembler);
  256. {$else x86_64}
  257. RegisterAssembler(as_i386_as_info,Tx86ATTAssembler);
  258. RegisterAssembler(as_i386_as_aout_info,Tx86ATTAssembler);
  259. {$endif x86_64}
  260. end.