agx86att.pas 9.3 KB

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