agx86att.pas 15 KB

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