agx86att.pas 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements an asmoutput class for i386 AT&T syntax
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. { This unit implements an asmoutput class for i386 AT&T syntax
  19. }
  20. unit agx86att;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cclasses,cpubase,
  25. globals,
  26. aasmbase,aasmtai,assemble,aggas;
  27. type
  28. Tx86ATTAssembler=class(TGNUassembler)
  29. private
  30. procedure WriteReference(var ref : treference);
  31. procedure WriteOper(const o:toper);
  32. procedure WriteOper_jmp(const o:toper);
  33. public
  34. procedure WriteInstruction(hp: tai);override;
  35. end;
  36. implementation
  37. uses
  38. cutils,systems,
  39. verbose,
  40. itcpugas,
  41. cpuinfo,
  42. cgbase,
  43. aasmcpu;
  44. {****************************************************************************
  45. TX86ATTASMOUTPUT
  46. ****************************************************************************}
  47. procedure Tx86AttAssembler.WriteReference(var ref : treference);
  48. begin
  49. with ref do
  50. begin
  51. { have we a segment prefix ? }
  52. { These are probably not correctly handled under GAS }
  53. { should be replaced by coding the segment override }
  54. { directly! - DJGPP FAQ }
  55. if segment<>NR_NO then
  56. AsmWrite(gas_regname(segment)+':');
  57. if assigned(symbol) then
  58. AsmWrite(symbol.name);
  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=addr_no 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^.offset>0 then
  136. AsmWrite('+'+tostr(o.ref^.offset))
  137. else
  138. if o.ref^.offset<0 then
  139. AsmWrite(tostr(o.ref^.offset));
  140. end;
  141. end;
  142. top_const :
  143. AsmWrite(tostr(o.val));
  144. else
  145. internalerror(10001);
  146. end;
  147. end;
  148. procedure Tx86AttAssembler.WriteInstruction(hp: tai);
  149. var
  150. op : tasmop;
  151. calljmp : boolean;
  152. i : integer;
  153. begin
  154. if hp.typ <> ait_instruction then
  155. exit;
  156. taicpu(hp).SetOperandOrder(op_att);
  157. op:=taicpu(hp).opcode;
  158. calljmp:=is_calljmp(op);
  159. AsmWrite(#9);
  160. { movsd should not be translated to movsl when there
  161. are (xmm) arguments }
  162. if (op=A_MOVSD) and (taicpu(hp).ops>0) then
  163. AsmWrite('movsd')
  164. else
  165. AsmWrite(gas_op2str[op]);
  166. AsmWrite(cond2str[taicpu(hp).condition]);
  167. { suffix needed ? fnstsw,fldcw don't support suffixes
  168. with binutils 2.9.5 under linux }
  169. { if (Taicpu(hp).oper[0]^.typ=top_reg) and
  170. (Taicpu(hp).oper[0]^.reg.enum>lastreg) then
  171. internalerror(200301081);}
  172. if (not calljmp) and
  173. (gas_needsuffix[op]<>AttSufNONE) and
  174. (op<>A_FNSTSW) and
  175. (op<>A_FSTSW) and
  176. (op<>A_FNSTCW) and
  177. (op<>A_FSTCW) and
  178. (op<>A_FLDCW) and
  179. not(
  180. (taicpu(hp).ops<>0) and
  181. (taicpu(hp).oper[0]^.typ=top_reg) and
  182. (getregtype(taicpu(hp).oper[0]^.reg)=R_FPUREGISTER)
  183. ) then
  184. AsmWrite(gas_opsize2str[taicpu(hp).opsize]);
  185. { process operands }
  186. if taicpu(hp).ops<>0 then
  187. begin
  188. if calljmp then
  189. begin
  190. AsmWrite(#9);
  191. WriteOper_jmp(taicpu(hp).oper[0]^);
  192. end
  193. else
  194. begin
  195. for i:=0 to taicpu(hp).ops-1 do
  196. begin
  197. if i=0 then
  198. AsmWrite(#9)
  199. else
  200. AsmWrite(',');
  201. WriteOper(taicpu(hp).oper[i]^);
  202. end;
  203. end;
  204. end;
  205. AsmLn;
  206. end;
  207. {*****************************************************************************
  208. Initialize
  209. *****************************************************************************}
  210. const
  211. {$ifdef x86_64}
  212. as_x86_64_as_info : tasminfo =
  213. (
  214. id : as_gas;
  215. idtxt : 'AS';
  216. asmbin : 'as';
  217. asmcmd : '-o $OBJ $ASM';
  218. supported_target : system_any;
  219. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  220. labelprefix : '.L';
  221. comment : '# ';
  222. );
  223. {$else x86_64}
  224. as_i386_as_info : tasminfo =
  225. (
  226. id : as_gas;
  227. idtxt : 'AS';
  228. asmbin : 'as';
  229. asmcmd : '-o $OBJ $ASM';
  230. supported_target : system_any;
  231. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  232. labelprefix : '.L';
  233. comment : '# ';
  234. );
  235. as_i386_as_aout_info : tasminfo =
  236. (
  237. id : as_i386_as_aout;
  238. idtxt : 'AS_AOUT';
  239. asmbin : 'as';
  240. asmcmd : '-o $OBJ $ASM';
  241. supported_target : system_any;
  242. flags : [af_allowdirect,af_needar];
  243. labelprefix : 'L';
  244. comment : '# ';
  245. );
  246. {$endif x86_64}
  247. initialization
  248. {$ifdef x86_64}
  249. RegisterAssembler(as_x86_64_as_info,Tx86ATTAssembler);
  250. {$else x86_64}
  251. RegisterAssembler(as_i386_as_info,Tx86ATTAssembler);
  252. RegisterAssembler(as_i386_as_aout_info,Tx86ATTAssembler);
  253. {$endif x86_64}
  254. end.
  255. {
  256. $Log$
  257. Revision 1.16 2004-06-29 21:00:08 peter
  258. * only enable dwarf for supported platforms
  259. Revision 1.15 2004/06/20 08:55:32 florian
  260. * logs truncated
  261. Revision 1.14 2004/06/16 20:07:11 florian
  262. * dwarf branch merged
  263. Revision 1.13.2.6 2004/05/10 21:28:35 peter
  264. * section_smartlink enabled for gas under linux
  265. Revision 1.13.2.5 2004/05/01 16:02:10 peter
  266. * POINTER_SIZE replaced with sizeof(aint)
  267. * aint,aword,tconst*int moved to globtype
  268. Revision 1.13.2.4 2004/04/27 18:18:26 peter
  269. * aword -> aint
  270. Revision 1.13.2.3 2004/04/26 21:04:04 peter
  271. * write aint
  272. }