agx86att.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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,aasmdata,assemble,aggas;
  26. type
  27. Tx86ATTAssembler=class(TGNUassembler)
  28. constructor create(smart: boolean); override;
  29. end;
  30. Tx86AppleGNUAssembler=class(TAppleGNUassembler)
  31. constructor create(smart: boolean); override;
  32. end;
  33. Tx86InstrWriter=class(TCPUInstrWriter)
  34. private
  35. procedure WriteReference(var ref : treference);
  36. procedure WriteOper(const o:toper);
  37. procedure WriteOper_jmp(const o:toper);
  38. public
  39. procedure WriteInstruction(hp: tai);override;
  40. end;
  41. implementation
  42. uses
  43. cutils,systems,
  44. verbose,
  45. itcpugas,
  46. cgbase,
  47. aasmcpu;
  48. {****************************************************************************
  49. Tx86ATTAssembler
  50. ****************************************************************************}
  51. constructor Tx86ATTAssembler.create(smart: boolean);
  52. begin
  53. inherited create(smart);
  54. InstrWriter := Tx86InstrWriter.create(self);
  55. end;
  56. {****************************************************************************
  57. Tx86AppleGNUAssembler
  58. ****************************************************************************}
  59. constructor Tx86AppleGNUAssembler.create(smart: boolean);
  60. begin
  61. inherited create(smart);
  62. InstrWriter := Tx86InstrWriter.create(self);
  63. end;
  64. {****************************************************************************
  65. Tx86InstrWriter
  66. ****************************************************************************}
  67. procedure Tx86InstrWriter.WriteReference(var ref : treference);
  68. begin
  69. with ref do
  70. begin
  71. { do we have a segment prefix ? }
  72. { These are probably not correctly handled under GAS }
  73. { should be replaced by coding the segment override }
  74. { directly! - DJGPP FAQ }
  75. if segment<>NR_NO then
  76. owner.AsmWrite(gas_regname(segment)+':');
  77. if assigned(symbol) then
  78. owner.AsmWrite(symbol.name);
  79. if ref.refaddr=addr_pic then
  80. {$ifdef x86_64}
  81. owner.AsmWrite('@GOTPCREL');
  82. {$else x86_64}
  83. owner.AsmWrite('@GOT');
  84. {$endif x86_64}
  85. if offset<0 then
  86. owner.AsmWrite(tostr(offset))
  87. else
  88. if (offset>0) then
  89. begin
  90. if assigned(symbol) then
  91. owner.AsmWrite('+'+tostr(offset))
  92. else
  93. owner.AsmWrite(tostr(offset));
  94. end
  95. else if (index=NR_NO) and (base=NR_NO) and (not assigned(symbol)) then
  96. owner.AsmWrite('0');
  97. if (index<>NR_NO) and (base=NR_NO) then
  98. begin
  99. owner.AsmWrite('(,'+gas_regname(index));
  100. if scalefactor<>0 then
  101. owner.AsmWrite(','+tostr(scalefactor)+')')
  102. else
  103. owner.AsmWrite(')');
  104. end
  105. else
  106. if (index=NR_NO) and (base<>NR_NO) then
  107. owner.AsmWrite('('+gas_regname(base)+')')
  108. else
  109. if (index<>NR_NO) and (base<>NR_NO) then
  110. begin
  111. owner.AsmWrite('('+gas_regname(base)+','+gas_regname(index));
  112. if scalefactor<>0 then
  113. owner.AsmWrite(','+tostr(scalefactor));
  114. owner.AsmWrite(')');
  115. end;
  116. end;
  117. end;
  118. procedure Tx86InstrWriter.WriteOper(const o:toper);
  119. begin
  120. case o.typ of
  121. top_reg :
  122. owner.AsmWrite(gas_regname(o.reg));
  123. top_ref :
  124. if o.ref^.refaddr in [addr_no,addr_pic] then
  125. WriteReference(o.ref^)
  126. else
  127. begin
  128. owner.AsmWrite('$');
  129. if assigned(o.ref^.symbol) then
  130. owner.AsmWrite(o.ref^.symbol.name);
  131. if o.ref^.offset>0 then
  132. owner.AsmWrite('+'+tostr(o.ref^.offset))
  133. else
  134. if o.ref^.offset<0 then
  135. owner.AsmWrite(tostr(o.ref^.offset))
  136. else
  137. if not(assigned(o.ref^.symbol)) then
  138. owner.AsmWrite('0');
  139. end;
  140. top_const :
  141. owner.AsmWrite('$'+tostr(o.val));
  142. else
  143. internalerror(10001);
  144. end;
  145. end;
  146. procedure Tx86InstrWriter.WriteOper_jmp(const o:toper);
  147. begin
  148. case o.typ of
  149. top_reg :
  150. owner.AsmWrite('*'+gas_regname(o.reg));
  151. top_ref :
  152. begin
  153. if o.ref^.refaddr=addr_no then
  154. begin
  155. owner.AsmWrite('*');
  156. WriteReference(o.ref^);
  157. end
  158. else
  159. begin
  160. owner.AsmWrite(o.ref^.symbol.name);
  161. if o.ref^.refaddr=addr_pic then
  162. owner.AsmWrite('@PLT');
  163. if o.ref^.offset>0 then
  164. owner.AsmWrite('+'+tostr(o.ref^.offset))
  165. else
  166. if o.ref^.offset<0 then
  167. owner.AsmWrite(tostr(o.ref^.offset));
  168. end;
  169. end;
  170. top_const :
  171. owner.AsmWrite(tostr(o.val));
  172. else
  173. internalerror(10001);
  174. end;
  175. end;
  176. procedure Tx86InstrWriter.WriteInstruction(hp: tai);
  177. var
  178. op : tasmop;
  179. calljmp : boolean;
  180. i : integer;
  181. begin
  182. if hp.typ <> ait_instruction then
  183. exit;
  184. taicpu(hp).SetOperandOrder(op_att);
  185. op:=taicpu(hp).opcode;
  186. calljmp:=is_calljmp(op);
  187. owner.AsmWrite(#9);
  188. { movsd should not be translated to movsl when there
  189. are (xmm) arguments }
  190. if (op=A_MOVSD) and (taicpu(hp).ops>0) then
  191. owner.AsmWrite('movsd')
  192. else
  193. owner.AsmWrite(gas_op2str[op]);
  194. owner.AsmWrite(cond2str[taicpu(hp).condition]);
  195. { suffix needed ? fnstsw,fldcw don't support suffixes
  196. with binutils 2.9.5 under linux }
  197. { if (Taicpu(hp).oper[0]^.typ=top_reg) and
  198. (Taicpu(hp).oper[0]^.reg.enum>lastreg) then
  199. internalerror(200301081);}
  200. if (not calljmp) and
  201. (gas_needsuffix[op]<>AttSufNONE) and
  202. (op<>A_FNSTSW) and
  203. (op<>A_FSTSW) and
  204. (op<>A_FNSTCW) and
  205. (op<>A_FSTCW) and
  206. (op<>A_FLDCW) and
  207. not(
  208. (taicpu(hp).ops<>0) and
  209. (taicpu(hp).oper[0]^.typ=top_reg) and
  210. (getregtype(taicpu(hp).oper[0]^.reg)=R_FPUREGISTER)
  211. ) then
  212. owner.AsmWrite(gas_opsize2str[taicpu(hp).opsize]);
  213. { process operands }
  214. if taicpu(hp).ops<>0 then
  215. begin
  216. if calljmp then
  217. begin
  218. owner.AsmWrite(#9);
  219. WriteOper_jmp(taicpu(hp).oper[0]^);
  220. end
  221. else
  222. begin
  223. for i:=0 to taicpu(hp).ops-1 do
  224. begin
  225. if i=0 then
  226. owner.AsmWrite(#9)
  227. else
  228. owner.AsmWrite(',');
  229. WriteOper(taicpu(hp).oper[i]^);
  230. end;
  231. end;
  232. end;
  233. owner.AsmLn;
  234. end;
  235. {*****************************************************************************
  236. Initialize
  237. *****************************************************************************}
  238. const
  239. {$ifdef x86_64}
  240. as_x86_64_as_info : tasminfo =
  241. (
  242. id : as_gas;
  243. idtxt : 'AS';
  244. asmbin : 'as';
  245. asmcmd : '--64 -o $OBJ $ASM';
  246. supported_target : system_any;
  247. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  248. labelprefix : '.L';
  249. comment : '# ';
  250. );
  251. {$else x86_64}
  252. as_i386_as_info : tasminfo =
  253. (
  254. id : as_gas;
  255. idtxt : 'AS';
  256. asmbin : 'as';
  257. asmcmd : '--32 -o $OBJ $ASM';
  258. supported_target : system_any;
  259. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  260. labelprefix : '.L';
  261. comment : '# ';
  262. );
  263. as_i386_as_aout_info : tasminfo =
  264. (
  265. id : as_i386_as_aout;
  266. idtxt : 'AS_AOUT';
  267. asmbin : 'as';
  268. asmcmd : '-o $OBJ $ASM';
  269. supported_target : system_any;
  270. flags : [af_allowdirect,af_needar];
  271. labelprefix : 'L';
  272. comment : '# ';
  273. );
  274. as_i386_gas_darwin_info : tasminfo =
  275. (
  276. id : as_darwin;
  277. idtxt : 'AS-Darwin';
  278. asmbin : 'as';
  279. asmcmd : '-o $OBJ $ASM -arch i386';
  280. supported_target : system_any;
  281. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  282. labelprefix : 'L';
  283. comment : '# ';
  284. );
  285. as_i386_gas_info : tasminfo =
  286. (
  287. id : as_ggas;
  288. idtxt : 'GAS';
  289. asmbin : 'gas';
  290. asmcmd : '--32 -o $OBJ $ASM';
  291. supported_target : system_any;
  292. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  293. labelprefix : '.L';
  294. comment : '# ';
  295. );
  296. {$endif x86_64}
  297. initialization
  298. {$ifdef x86_64}
  299. RegisterAssembler(as_x86_64_as_info,Tx86ATTAssembler);
  300. {$else x86_64}
  301. RegisterAssembler(as_i386_as_info,Tx86ATTAssembler);
  302. RegisterAssembler(as_i386_gas_info,Tx86ATTAssembler);
  303. RegisterAssembler(as_i386_gas_darwin_info,Tx86AppleGNUAssembler);
  304. RegisterAssembler(as_i386_as_aout_info,Tx86ATTAssembler);
  305. {$endif x86_64}
  306. end.