agx86att.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. Tx86AoutGNUAssembler=class(TAoutGNUassembler)
  34. constructor create(smart: boolean); override;
  35. end;
  36. Tx86InstrWriter=class(TCPUInstrWriter)
  37. private
  38. procedure WriteReference(var ref : treference);
  39. procedure WriteOper(const o:toper);
  40. procedure WriteOper_jmp(const o:toper);
  41. public
  42. procedure WriteInstruction(hp: tai);override;
  43. end;
  44. implementation
  45. uses
  46. globtype,
  47. cutils,systems,
  48. verbose,
  49. itcpugas,
  50. cgbase,
  51. aasmcpu;
  52. {****************************************************************************
  53. Tx86ATTAssembler
  54. ****************************************************************************}
  55. constructor Tx86ATTAssembler.create(smart: boolean);
  56. begin
  57. inherited create(smart);
  58. InstrWriter := Tx86InstrWriter.create(self);
  59. end;
  60. {****************************************************************************
  61. Tx86AppleGNUAssembler
  62. ****************************************************************************}
  63. constructor Tx86AppleGNUAssembler.create(smart: boolean);
  64. begin
  65. inherited create(smart);
  66. InstrWriter := Tx86InstrWriter.create(self);
  67. end;
  68. {****************************************************************************
  69. Tx86AoutGNUAssembler
  70. ****************************************************************************}
  71. constructor Tx86AoutGNUAssembler.create(smart: boolean);
  72. begin
  73. inherited create(smart);
  74. InstrWriter := Tx86InstrWriter.create(self);
  75. end;
  76. {****************************************************************************
  77. Tx86InstrWriter
  78. ****************************************************************************}
  79. procedure Tx86InstrWriter.WriteReference(var ref : treference);
  80. begin
  81. with ref do
  82. begin
  83. { do we have a segment prefix ? }
  84. { These are probably not correctly handled under GAS }
  85. { should be replaced by coding the segment override }
  86. { directly! - DJGPP FAQ }
  87. if segment<>NR_NO then
  88. owner.AsmWrite(gas_regname(segment)+':');
  89. if assigned(symbol) then
  90. owner.AsmWrite(symbol.name);
  91. if assigned(relsymbol) then
  92. owner.AsmWrite('-'+relsymbol.name);
  93. if ref.refaddr=addr_pic then
  94. {$ifdef x86_64}
  95. owner.AsmWrite('@GOTPCREL');
  96. {$else x86_64}
  97. owner.AsmWrite('@GOT');
  98. {$endif x86_64}
  99. if offset<0 then
  100. owner.AsmWrite(tostr(offset))
  101. else
  102. if (offset>0) then
  103. begin
  104. if assigned(symbol) then
  105. owner.AsmWrite('+'+tostr(offset))
  106. else
  107. owner.AsmWrite(tostr(offset));
  108. end
  109. else if (index=NR_NO) and (base=NR_NO) and (not assigned(symbol)) then
  110. owner.AsmWrite('0');
  111. if (index<>NR_NO) and (base=NR_NO) then
  112. begin
  113. owner.AsmWrite('(,'+gas_regname(index));
  114. if scalefactor<>0 then
  115. owner.AsmWrite(','+tostr(scalefactor)+')')
  116. else
  117. owner.AsmWrite(')');
  118. end
  119. else
  120. if (index=NR_NO) and (base<>NR_NO) then
  121. owner.AsmWrite('('+gas_regname(base)+')')
  122. else
  123. if (index<>NR_NO) and (base<>NR_NO) then
  124. begin
  125. owner.AsmWrite('('+gas_regname(base)+','+gas_regname(index));
  126. if scalefactor<>0 then
  127. owner.AsmWrite(','+tostr(scalefactor));
  128. owner.AsmWrite(')');
  129. end;
  130. end;
  131. end;
  132. procedure Tx86InstrWriter.WriteOper(const o:toper);
  133. begin
  134. case o.typ of
  135. top_reg :
  136. owner.AsmWrite(gas_regname(o.reg));
  137. top_ref :
  138. if o.ref^.refaddr in [addr_no,addr_pic,addr_pic_no_got] then
  139. WriteReference(o.ref^)
  140. else
  141. begin
  142. owner.AsmWrite('$');
  143. if assigned(o.ref^.symbol) then
  144. owner.AsmWrite(o.ref^.symbol.name);
  145. if o.ref^.offset>0 then
  146. owner.AsmWrite('+'+tostr(o.ref^.offset))
  147. else
  148. if o.ref^.offset<0 then
  149. owner.AsmWrite(tostr(o.ref^.offset))
  150. else
  151. if not(assigned(o.ref^.symbol)) then
  152. owner.AsmWrite('0');
  153. end;
  154. top_const :
  155. owner.AsmWrite('$'+tostr(o.val));
  156. else
  157. internalerror(10001);
  158. end;
  159. end;
  160. procedure Tx86InstrWriter.WriteOper_jmp(const o:toper);
  161. begin
  162. case o.typ of
  163. top_reg :
  164. owner.AsmWrite('*'+gas_regname(o.reg));
  165. top_ref :
  166. begin
  167. if o.ref^.refaddr=addr_no then
  168. begin
  169. owner.AsmWrite('*');
  170. WriteReference(o.ref^);
  171. end
  172. else
  173. begin
  174. owner.AsmWrite(o.ref^.symbol.name);
  175. if o.ref^.refaddr=addr_pic then
  176. owner.AsmWrite('@PLT');
  177. if o.ref^.offset>0 then
  178. owner.AsmWrite('+'+tostr(o.ref^.offset))
  179. else
  180. if o.ref^.offset<0 then
  181. owner.AsmWrite(tostr(o.ref^.offset));
  182. end;
  183. end;
  184. top_const :
  185. owner.AsmWrite(tostr(o.val));
  186. else
  187. internalerror(10001);
  188. end;
  189. end;
  190. procedure Tx86InstrWriter.WriteInstruction(hp: tai);
  191. var
  192. op : tasmop;
  193. val : aint;
  194. calljmp : boolean;
  195. need_second_mov : boolean;
  196. i : integer;
  197. comment : tai_comment;
  198. begin
  199. if hp.typ <> ait_instruction then
  200. exit;
  201. taicpu(hp).SetOperandOrder(op_att);
  202. op:=taicpu(hp).opcode;
  203. calljmp:=is_calljmp(op);
  204. { constant values in the 32 bit range are sign-extended to
  205. 64 bits, but this is not what we want. PM 2010-09-02
  206. the fix consists of simply setting only the 4-byte register
  207. as the upper 4-bytes will be zeroed at the same time. }
  208. need_second_mov:=false;
  209. if (op=A_MOV) and (taicpu(hp).opsize=S_Q) and
  210. (taicpu(hp).oper[0]^.typ = top_const) then
  211. begin
  212. val := taicpu(hp).oper[0]^.val;
  213. if (val > int64($7fffffff)) and (val < int64($100000000)) then
  214. begin
  215. owner.AsmWrite(target_asm.comment);
  216. owner.AsmWritePChar('Fix for Win64-GAS bug');
  217. owner.AsmLn;
  218. taicpu(hp).opsize:=S_L;
  219. if taicpu(hp).oper[1]^.typ = top_reg then
  220. setsubreg(taicpu(hp).oper[1]^.reg,R_SUBD)
  221. else if taicpu(hp).oper[1]^.typ = top_ref then
  222. need_second_mov:=true
  223. else
  224. internalerror(20100902011);
  225. end;
  226. end;
  227. owner.AsmWrite(#9);
  228. { movsd should not be translated to movsl when there
  229. are (xmm) arguments }
  230. if (op=A_MOVSD) and (taicpu(hp).ops>0) then
  231. owner.AsmWrite('movsd')
  232. else
  233. owner.AsmWrite(gas_op2str[op]);
  234. owner.AsmWrite(cond2str[taicpu(hp).condition]);
  235. { suffix needed ? fnstsw,fldcw don't support suffixes
  236. with binutils 2.9.5 under linux }
  237. { if (Taicpu(hp).oper[0]^.typ=top_reg) and
  238. (Taicpu(hp).oper[0]^.reg.enum>lastreg) then
  239. internalerror(200301081);}
  240. if (not calljmp) and
  241. (gas_needsuffix[op]<>AttSufNONE) and
  242. (op<>A_FNSTSW) and
  243. (op<>A_FSTSW) and
  244. (op<>A_FNSTCW) and
  245. (op<>A_FSTCW) and
  246. (op<>A_FLDCW) and
  247. not(
  248. (taicpu(hp).ops<>0) and
  249. (taicpu(hp).oper[0]^.typ=top_reg) and
  250. (getregtype(taicpu(hp).oper[0]^.reg)=R_FPUREGISTER)
  251. ) then
  252. owner.AsmWrite(gas_opsize2str[taicpu(hp).opsize]);
  253. { process operands }
  254. if taicpu(hp).ops<>0 then
  255. begin
  256. if calljmp then
  257. begin
  258. owner.AsmWrite(#9);
  259. WriteOper_jmp(taicpu(hp).oper[0]^);
  260. end
  261. else
  262. begin
  263. for i:=0 to taicpu(hp).ops-1 do
  264. begin
  265. if i=0 then
  266. owner.AsmWrite(#9)
  267. else
  268. owner.AsmWrite(',');
  269. WriteOper(taicpu(hp).oper[i]^);
  270. end;
  271. end;
  272. end;
  273. owner.AsmLn;
  274. if need_second_mov then
  275. begin
  276. taicpu(hp).oper[0]^.val:=0;
  277. inc(taicpu(hp).oper[1]^.ref^.offset,4);
  278. WriteInstruction(hp);
  279. end;
  280. end;
  281. {*****************************************************************************
  282. Initialize
  283. *****************************************************************************}
  284. const
  285. {$ifdef x86_64}
  286. as_x86_64_as_info : tasminfo =
  287. (
  288. id : as_gas;
  289. idtxt : 'AS';
  290. asmbin : 'as';
  291. asmcmd : '--64 -o $OBJ $ASM';
  292. supported_targets : [system_x86_64_linux,system_x86_64_freebsd,system_x86_64_win64,system_x86_64_embedded];
  293. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  294. labelprefix : '.L';
  295. comment : '# ';
  296. );
  297. as_x86_64_gas_info : tasminfo =
  298. (
  299. id : as_ggas;
  300. idtxt : 'GAS';
  301. asmbin : 'gas';
  302. asmcmd : '--64 -o $OBJ $ASM';
  303. supported_targets : [system_x86_64_solaris];
  304. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  305. labelprefix : '.L';
  306. comment : '# ';
  307. );
  308. as_x86_64_gas_darwin_info : tasminfo =
  309. (
  310. id : as_darwin;
  311. idtxt : 'AS-Darwin';
  312. asmbin : 'as';
  313. asmcmd : '-o $OBJ $ASM -arch x86_64';
  314. supported_targets : [system_x86_64_darwin];
  315. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  316. labelprefix : 'L';
  317. comment : '# ';
  318. );
  319. {$else x86_64}
  320. as_i386_as_info : tasminfo =
  321. (
  322. id : as_gas;
  323. idtxt : 'AS';
  324. asmbin : 'as';
  325. asmcmd : '--32 -o $OBJ $ASM';
  326. supported_targets : [system_i386_GO32V2,system_i386_linux,system_i386_Win32,system_i386_freebsd,system_i386_solaris,system_i386_beos,
  327. system_i386_netbsd,system_i386_Netware,system_i386_qnx,system_i386_wdosx,system_i386_openbsd,
  328. system_i386_netwlibc,system_i386_wince,system_i386_embedded,system_i386_symbian,system_i386_haiku,system_x86_6432_linux];
  329. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  330. labelprefix : '.L';
  331. comment : '# ';
  332. );
  333. as_i386_as_aout_info : tasminfo =
  334. (
  335. id : as_i386_as_aout;
  336. idtxt : 'AS_AOUT';
  337. asmbin : 'as';
  338. asmcmd : '-o $OBJ $ASM';
  339. supported_targets : [system_i386_linux,system_i386_OS2,system_i386_freebsd,system_i386_netbsd,system_i386_openbsd,system_i386_EMX,system_i386_embedded];
  340. flags : [af_allowdirect,af_needar];
  341. labelprefix : 'L';
  342. comment : '# ';
  343. );
  344. as_i386_gas_darwin_info : tasminfo =
  345. (
  346. id : as_darwin;
  347. idtxt : 'AS-Darwin';
  348. asmbin : 'as';
  349. asmcmd : '-o $OBJ $ASM -arch i386';
  350. supported_targets : [system_i386_darwin];
  351. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  352. labelprefix : 'L';
  353. comment : '# ';
  354. );
  355. as_i386_gas_info : tasminfo =
  356. (
  357. id : as_ggas;
  358. idtxt : 'GAS';
  359. asmbin : 'gas';
  360. asmcmd : '--32 -o $OBJ $ASM';
  361. supported_targets : [system_i386_GO32V2,system_i386_linux,system_i386_Win32,system_i386_freebsd,system_i386_solaris,system_i386_beos,
  362. system_i386_netbsd,system_i386_Netware,system_i386_qnx,system_i386_wdosx,system_i386_openbsd,
  363. system_i386_netwlibc,system_i386_wince,system_i386_embedded,system_i386_symbian,system_i386_haiku,system_x86_6432_linux];
  364. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  365. labelprefix : '.L';
  366. comment : '# ';
  367. );
  368. {$endif x86_64}
  369. initialization
  370. {$ifdef x86_64}
  371. RegisterAssembler(as_x86_64_as_info,Tx86ATTAssembler);
  372. RegisterAssembler(as_x86_64_gas_info,Tx86ATTAssembler);
  373. RegisterAssembler(as_x86_64_gas_darwin_info,Tx86AppleGNUAssembler);
  374. {$else x86_64}
  375. RegisterAssembler(as_i386_as_info,Tx86ATTAssembler);
  376. RegisterAssembler(as_i386_gas_info,Tx86ATTAssembler);
  377. RegisterAssembler(as_i386_gas_darwin_info,Tx86AppleGNUAssembler);
  378. RegisterAssembler(as_i386_as_aout_info,Tx86AoutGNUAssembler);
  379. {$endif x86_64}
  380. end.