agx86att.pas 9.1 KB

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