agx86att.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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,
  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. itx86att,
  41. cginfo,
  42. aasmcpu;
  43. {****************************************************************************
  44. TX86ATTASMOUTPUT
  45. ****************************************************************************}
  46. procedure Tx86AttAssembler.WriteReference(var ref : treference);
  47. begin
  48. with ref do
  49. begin
  50. inc(offset,offsetfixup);
  51. offsetfixup:=0;
  52. { have we a segment prefix ? }
  53. { These are probably not correctly handled under GAS }
  54. { should be replaced by coding the segment override }
  55. { directly! - DJGPP FAQ }
  56. if segment<>NR_NO then
  57. AsmWrite(gas_regname(segment)+':');
  58. if assigned(symbol) then
  59. AsmWrite(symbol.name);
  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. WriteReference(o.ref^);
  100. top_const :
  101. AsmWrite('$'+tostr(longint(o.val)));
  102. top_symbol :
  103. begin
  104. AsmWrite('$');
  105. if assigned(o.sym) then
  106. AsmWrite(o.sym.name);
  107. if o.symofs>0 then
  108. AsmWrite('+'+tostr(o.symofs))
  109. else
  110. if o.symofs<0 then
  111. AsmWrite(tostr(o.symofs))
  112. else
  113. if not(assigned(o.sym)) then
  114. AsmWrite('0');
  115. end;
  116. else
  117. internalerror(10001);
  118. end;
  119. end;
  120. procedure Tx86AttAssembler.WriteOper_jmp(const o:toper);
  121. begin
  122. case o.typ of
  123. top_reg :
  124. AsmWrite('*'+gas_regname(o.reg));
  125. top_ref :
  126. begin
  127. AsmWrite('*');
  128. WriteReference(o.ref^);
  129. end;
  130. top_const :
  131. AsmWrite(tostr(longint(o.val)));
  132. top_symbol :
  133. begin
  134. AsmWrite(o.sym.name);
  135. if o.symofs>0 then
  136. AsmWrite('+'+tostr(o.symofs))
  137. else
  138. if o.symofs<0 then
  139. AsmWrite(tostr(o.symofs));
  140. end;
  141. else
  142. internalerror(10001);
  143. end;
  144. end;
  145. procedure Tx86AttAssembler.WriteInstruction(hp: tai);
  146. var
  147. op : tasmop;
  148. calljmp : boolean;
  149. i : integer;
  150. begin
  151. if hp.typ <> ait_instruction then
  152. exit;
  153. taicpu(hp).SetOperandOrder(op_att);
  154. op:=taicpu(hp).opcode;
  155. calljmp:=is_calljmp(op);
  156. { call maybe not translated to call }
  157. AsmWrite(#9+gas_op2str[op]+cond2str[taicpu(hp).condition]);
  158. { suffix needed ? fnstsw,fldcw don't support suffixes
  159. with binutils 2.9.5 under linux }
  160. { if (Taicpu(hp).oper[0].typ=top_reg) and
  161. (Taicpu(hp).oper[0].reg.enum>lastreg) then
  162. internalerror(200301081);}
  163. if (not calljmp) and
  164. (gas_needsuffix[op]<>AttSufNONE) and
  165. (op<>A_FNSTSW) and (op<>A_FSTSW) and
  166. (op<>A_FNSTCW) and (op<>A_FSTCW) and
  167. (op<>A_FLDCW) and not(
  168. (taicpu(hp).oper[0].typ=top_reg) and
  169. (getregtype(taicpu(hp).oper[0].reg)=R_FPUREGISTER)
  170. ) then
  171. AsmWrite(gas_opsize2str[taicpu(hp).opsize]);
  172. { process operands }
  173. if taicpu(hp).ops<>0 then
  174. begin
  175. if calljmp then
  176. begin
  177. AsmWrite(#9);
  178. WriteOper_jmp(taicpu(hp).oper[0]);
  179. end
  180. else
  181. begin
  182. for i:=0 to taicpu(hp).ops-1 do
  183. begin
  184. if i=0 then
  185. AsmWrite(#9)
  186. else
  187. AsmWrite(',');
  188. WriteOper(taicpu(hp).oper[i]);
  189. end;
  190. end;
  191. end;
  192. AsmLn;
  193. end;
  194. {*****************************************************************************
  195. Initialize
  196. *****************************************************************************}
  197. const
  198. {$ifdef x86_64}
  199. as_x86_64_as_info : tasminfo =
  200. (
  201. id : as_x86_64_as;
  202. idtxt : 'AS';
  203. asmbin : 'as';
  204. asmcmd : '-o $OBJ $ASM';
  205. supported_target : system_any;
  206. outputbinary: false;
  207. allowdirect : true;
  208. needar : true;
  209. labelprefix_only_inside_procedure : false;
  210. labelprefix : '.L';
  211. comment : '# ';
  212. secnames : ('',
  213. '.text','.data','.bss',
  214. '','','','','','',
  215. '.stab','.stabstr','COMMON')
  216. );
  217. {$else x86_64}
  218. as_i386_as_info : tasminfo =
  219. (
  220. id : as_gas;
  221. idtxt : 'AS';
  222. asmbin : 'as';
  223. asmcmd : '-o $OBJ $ASM';
  224. supported_target : system_any;
  225. outputbinary: false;
  226. allowdirect : true;
  227. needar : true;
  228. labelprefix_only_inside_procedure : false;
  229. labelprefix : '.L';
  230. comment : '# ';
  231. secnames : ('',
  232. '.text','.data','.bss',
  233. '','','','','','',
  234. '.stab','.stabstr','COMMON')
  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. { supported_target : system_i386_emx;}
  244. outputbinary: false;
  245. allowdirect : true;
  246. needar : true;
  247. labelprefix_only_inside_procedure : false;
  248. labelprefix : 'L';
  249. comment : '# ';
  250. secnames : ('',
  251. '.text','.data','.bss',
  252. '','','','','','',
  253. '.stab','.stabstr','COMMON')
  254. );
  255. as_i386_asw_info : tasminfo =
  256. (
  257. id : as_i386_asw;
  258. idtxt : 'ASW';
  259. asmbin : 'asw';
  260. asmcmd : '-o $OBJ $ASM';
  261. supported_target : system_i386_win32;
  262. outputbinary: false;
  263. allowdirect : true;
  264. needar : true;
  265. labelprefix_only_inside_procedure : false;
  266. labelprefix : '.L';
  267. comment : '# ';
  268. secnames : ('',
  269. '.text','.data','.section .bss',
  270. '.section .idata$2','.section .idata$4','.section .idata$5',
  271. '.section .idata$6','.section .idata$7','.section .edata',
  272. '.stab','.stabstr','COMMON')
  273. );
  274. {$endif x86_64}
  275. initialization
  276. {$ifdef x86_64}
  277. RegisterAssembler(as_x86_64_as_info,Tx86ATTAssembler);
  278. {$else x86_64}
  279. RegisterAssembler(as_i386_as_info,Tx86ATTAssembler);
  280. RegisterAssembler(as_i386_as_aout_info,Tx86ATTAssembler);
  281. RegisterAssembler(as_i386_asw_info,Tx86ATTAssembler);
  282. {$endif x86_64}
  283. end.
  284. {
  285. $Log$
  286. Revision 1.5 2003-09-03 15:55:02 peter
  287. * NEWRA branch merged
  288. Revision 1.4.2.1 2003/08/31 15:46:26 peter
  289. * more updates for tregister
  290. Revision 1.4 2003/08/18 11:49:47 daniel
  291. * Made ATT asm writer work with -sr
  292. Revision 1.3 2003/05/28 23:18:31 florian
  293. * started to fix and clean up the sparc port
  294. Revision 1.2 2003/05/22 21:33:31 peter
  295. * removed some unit dependencies
  296. Revision 1.1 2003/04/25 12:04:31 florian
  297. * merged agx64att and ag386att to x86/agx86att
  298. Revision 1.31 2003/03/23 23:33:10 hajny
  299. + emx target added
  300. Revision 1.30 2003/02/19 22:00:15 daniel
  301. * Code generator converted to new register notation
  302. - Horribily outdated todo.txt removed
  303. Revision 1.29 2003/01/08 18:43:57 daniel
  304. * Tregister changed into a record
  305. Revision 1.28 2003/01/05 13:36:53 florian
  306. * x86-64 compiles
  307. + very basic support for float128 type (x86-64 only)
  308. Revision 1.27 2002/12/24 18:10:34 peter
  309. * Long symbol names support
  310. Revision 1.26 2002/08/12 15:08:40 carl
  311. + stab register indexes for powerpc (moved from gdb to cpubase)
  312. + tprocessor enumeration moved to cpuinfo
  313. + linker in target_info is now a class
  314. * many many updates for m68k (will soon start to compile)
  315. - removed some ifdef or correct them for correct cpu
  316. Revision 1.25 2002/07/26 21:15:42 florian
  317. * rewrote the system handling
  318. Revision 1.24 2002/07/07 09:52:33 florian
  319. * powerpc target fixed, very simple units can be compiled
  320. * some basic stuff for better callparanode handling, far from being finished
  321. Revision 1.23 2002/07/01 18:46:29 peter
  322. * internal linker
  323. * reorganized aasm layer
  324. Revision 1.22 2002/05/18 13:34:21 peter
  325. * readded missing revisions
  326. Revision 1.21 2002/05/16 19:46:49 carl
  327. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  328. + try to fix temp allocation (still in ifdef)
  329. + generic constructor calls
  330. + start of tassembler / tmodulebase class cleanup
  331. Revision 1.19 2002/05/12 16:53:16 peter
  332. * moved entry and exitcode to ncgutil and cgobj
  333. * foreach gets extra argument for passing local data to the
  334. iterator function
  335. * -CR checks also class typecasts at runtime by changing them
  336. into as
  337. * fixed compiler to cycle with the -CR option
  338. * fixed stabs with elf writer, finally the global variables can
  339. be watched
  340. * removed a lot of routines from cga unit and replaced them by
  341. calls to cgobj
  342. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  343. u32bit then the other is typecasted also to u32bit without giving
  344. a rangecheck warning/error.
  345. * fixed pascal calling method with reversing also the high tree in
  346. the parast, detected by tcalcst3 test
  347. Revision 1.18 2002/04/15 19:12:10 carl
  348. + target_info.size_of_pointer -> pointer_size
  349. + some cleanup of unused types/variables
  350. * move several constants from cpubase to their specific units
  351. (where they are used)
  352. + att_Reg2str -> gas_reg2str
  353. + int_reg2str -> std_reg2str
  354. Revision 1.17 2002/04/14 16:58:04 carl
  355. + move into aggas most of the stuff non-processor specific
  356. Revision 1.16 2002/04/10 08:07:55 jonas
  357. * fix for the ie9999 under Linux (patch from Peter)
  358. Revision 1.15 2002/04/04 19:06:06 peter
  359. * removed unused units
  360. * use tlocation.size in cg.a_*loc*() routines
  361. Revision 1.14 2002/04/04 18:26:55 carl
  362. + added wdosx patch from Pavel
  363. Revision 1.13 2002/04/02 17:11:33 peter
  364. * tlocation,treference update
  365. * LOC_CONSTANT added for better constant handling
  366. * secondadd splitted in multiple routines
  367. * location_force_reg added for loading a location to a register
  368. of a specified size
  369. * secondassignment parses now first the right and then the left node
  370. (this is compatible with Kylix). This saves a lot of push/pop especially
  371. with string operations
  372. * adapted some routines to use the new cg methods
  373. }