ag386att.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 ag386att;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cclasses,cpubase,
  25. globals,
  26. aasmbase,aasmtai,aasmcpu,assemble,aggas;
  27. type
  28. T386ATTAssembler=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. TAttSuffix = (AttSufNONE,AttSufINT,AttSufFPU,AttSufFPUint);
  37. const
  38. gas_op2str:op2strtable={$i i386att.inc}
  39. gas_needsuffix:array[tasmop] of TAttSuffix={$i i386atts.inc}
  40. gas_reg2str : reg2strtable = ('',
  41. '%eax','%ecx','%edx','%ebx','%esp','%ebp','%esi','%edi',
  42. '%ax','%cx','%dx','%bx','%sp','%bp','%si','%di',
  43. '%al','%cl','%dl','%bl','%ah','%ch','%bh','%dh',
  44. '%cs','%ds','%es','%ss','%fs','%gs',
  45. '%st','%st(0)','%st(1)','%st(2)','%st(3)','%st(4)','%st(5)','%st(6)','%st(7)',
  46. '%dr0','%dr1','%dr2','%dr3','%dr6','%dr7',
  47. '%cr0','%cr2','%cr3','%cr4',
  48. '%tr3','%tr4','%tr5','%tr6','%tr7',
  49. '%mm0','%mm1','%mm2','%mm3','%mm4','%mm5','%mm6','%mm7',
  50. '%xmm0','%xmm1','%xmm2','%xmm3','%xmm4','%xmm5','%xmm6','%xmm7'
  51. );
  52. gas_opsize2str : array[topsize] of string[2] = ('',
  53. 'b','w','l','bw','bl','wl',
  54. 's','l','q',
  55. 's','l','t','d','q','v','',
  56. '','',''
  57. );
  58. implementation
  59. uses
  60. cutils,systems,
  61. verbose;
  62. {****************************************************************************
  63. TI386ATTASMOUTPUT
  64. ****************************************************************************}
  65. procedure T386AttAssembler.WriteReference(var ref : treference);
  66. begin
  67. with ref do
  68. begin
  69. inc(offset,offsetfixup);
  70. offsetfixup:=0;
  71. { have we 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<>R_NO then
  76. AsmWrite(gas_reg2str[segment]+':');
  77. if assigned(symbol) then
  78. AsmWrite(symbol.name);
  79. if offset<0 then
  80. AsmWrite(tostr(offset))
  81. else
  82. if (offset>0) then
  83. begin
  84. if assigned(symbol) then
  85. AsmWrite('+'+tostr(offset))
  86. else
  87. AsmWrite(tostr(offset));
  88. end
  89. else if (index=R_NO) and (base=R_NO) and not assigned(symbol) then
  90. AsmWrite('0');
  91. if (index<>R_NO) and (base=R_NO) then
  92. begin
  93. AsmWrite('(,'+gas_reg2str[index]);
  94. if scalefactor<>0 then
  95. AsmWrite(','+tostr(scalefactor)+')')
  96. else
  97. AsmWrite(')');
  98. end
  99. else
  100. if (index=R_NO) and (base<>R_NO) then
  101. AsmWrite('('+gas_reg2str[base]+')')
  102. else
  103. if (index<>R_NO) and (base<>R_NO) then
  104. begin
  105. AsmWrite('('+gas_reg2str[base]+','+gas_reg2str[index]);
  106. if scalefactor<>0 then
  107. AsmWrite(','+tostr(scalefactor));
  108. AsmWrite(')');
  109. end;
  110. end;
  111. end;
  112. procedure T386AttAssembler.WriteOper(const o:toper);
  113. begin
  114. case o.typ of
  115. top_reg :
  116. AsmWrite(gas_reg2str[o.reg]);
  117. top_ref :
  118. WriteReference(o.ref^);
  119. top_const :
  120. AsmWrite('$'+tostr(longint(o.val)));
  121. top_symbol :
  122. begin
  123. AsmWrite('$');
  124. if assigned(o.sym) then
  125. AsmWrite(o.sym.name);
  126. if o.symofs>0 then
  127. AsmWrite('+'+tostr(o.symofs))
  128. else
  129. if o.symofs<0 then
  130. AsmWrite(tostr(o.symofs))
  131. else
  132. if not(assigned(o.sym)) then
  133. AsmWrite('0');
  134. end;
  135. else
  136. internalerror(10001);
  137. end;
  138. end;
  139. procedure T386AttAssembler.WriteOper_jmp(const o:toper);
  140. begin
  141. case o.typ of
  142. top_reg :
  143. AsmWrite('*'+gas_reg2str[o.reg]);
  144. top_ref :
  145. begin
  146. AsmWrite('*');
  147. WriteReference(o.ref^);
  148. end;
  149. top_const :
  150. AsmWrite(tostr(longint(o.val)));
  151. top_symbol :
  152. begin
  153. AsmWrite(o.sym.name);
  154. if o.symofs>0 then
  155. AsmWrite('+'+tostr(o.symofs))
  156. else
  157. if o.symofs<0 then
  158. AsmWrite(tostr(o.symofs));
  159. end;
  160. else
  161. internalerror(10001);
  162. end;
  163. end;
  164. procedure T386AttAssembler.WriteInstruction(hp: tai);
  165. var
  166. op : tasmop;
  167. calljmp : boolean;
  168. i : integer;
  169. begin
  170. if hp.typ <> ait_instruction then
  171. exit;
  172. taicpu(hp).SetOperandOrder(op_att);
  173. op:=taicpu(hp).opcode;
  174. calljmp:=is_calljmp(op);
  175. { call maybe not translated to call }
  176. AsmWrite(#9+gas_op2str[op]+cond2str[taicpu(hp).condition]);
  177. { suffix needed ? fnstsw,fldcw don't support suffixes
  178. with binutils 2.9.5 under linux }
  179. if (not calljmp) and
  180. (gas_needsuffix[op]<>AttSufNONE) and
  181. (op<>A_FNSTSW) and (op<>A_FSTSW) and
  182. (op<>A_FNSTCW) and (op<>A_FSTCW) and
  183. (op<>A_FLDCW) and not(
  184. (taicpu(hp).oper[0].typ=top_reg) and
  185. (taicpu(hp).oper[0].reg in [R_ST..R_ST7])
  186. ) then
  187. AsmWrite(gas_opsize2str[taicpu(hp).opsize]);
  188. { process operands }
  189. if taicpu(hp).ops<>0 then
  190. begin
  191. if calljmp then
  192. begin
  193. AsmWrite(#9);
  194. WriteOper_jmp(taicpu(hp).oper[0]);
  195. end
  196. else
  197. begin
  198. for i:=0 to taicpu(hp).ops-1 do
  199. begin
  200. if i=0 then
  201. AsmWrite(#9)
  202. else
  203. AsmWrite(',');
  204. WriteOper(taicpu(hp).oper[i]);
  205. end;
  206. end;
  207. end;
  208. AsmLn;
  209. end;
  210. {*****************************************************************************
  211. Initialize
  212. *****************************************************************************}
  213. const
  214. as_i386_as_info : tasminfo =
  215. (
  216. id : as_gas;
  217. idtxt : 'AS';
  218. asmbin : 'as';
  219. asmcmd : '-o $OBJ $ASM';
  220. supported_target : system_any;
  221. outputbinary: false;
  222. allowdirect : true;
  223. needar : true;
  224. labelprefix_only_inside_procedure : false;
  225. labelprefix : '.L';
  226. comment : '# ';
  227. secnames : ('',
  228. '.text','.data','.bss',
  229. '','','','','','',
  230. '.stab','.stabstr','COMMON')
  231. );
  232. as_i386_as_aout_info : tasminfo =
  233. (
  234. id : as_i386_as_aout;
  235. idtxt : 'AS_AOUT';
  236. asmbin : 'as';
  237. asmcmd : '-o $OBJ $ASM';
  238. supported_target : system_i386_os2;
  239. outputbinary: false;
  240. allowdirect : true;
  241. needar : true;
  242. labelprefix_only_inside_procedure : false;
  243. labelprefix : 'L';
  244. comment : '# ';
  245. secnames : ('',
  246. '.text','.data','.bss',
  247. '','','','','','',
  248. '.stab','.stabstr','COMMON')
  249. );
  250. as_i386_asw_info : tasminfo =
  251. (
  252. id : as_i386_asw;
  253. idtxt : 'ASW';
  254. asmbin : 'asw';
  255. asmcmd : '-o $OBJ $ASM';
  256. supported_target : system_i386_win32;
  257. outputbinary: false;
  258. allowdirect : true;
  259. needar : true;
  260. labelprefix_only_inside_procedure : false;
  261. labelprefix : '.L';
  262. comment : '# ';
  263. secnames : ('',
  264. '.text','.data','.section .bss',
  265. '.section .idata$2','.section .idata$4','.section .idata$5',
  266. '.section .idata$6','.section .idata$7','.section .edata',
  267. '.stab','.stabstr','COMMON')
  268. );
  269. initialization
  270. RegisterAssembler(as_i386_as_info,T386ATTAssembler);
  271. RegisterAssembler(as_i386_as_aout_info,T386ATTAssembler);
  272. RegisterAssembler(as_i386_asw_info,T386ATTAssembler);
  273. end.
  274. {
  275. $Log$
  276. Revision 1.28 2003-01-05 13:36:53 florian
  277. * x86-64 compiles
  278. + very basic support for float128 type (x86-64 only)
  279. Revision 1.27 2002/12/24 18:10:34 peter
  280. * Long symbol names support
  281. Revision 1.26 2002/08/12 15:08:40 carl
  282. + stab register indexes for powerpc (moved from gdb to cpubase)
  283. + tprocessor enumeration moved to cpuinfo
  284. + linker in target_info is now a class
  285. * many many updates for m68k (will soon start to compile)
  286. - removed some ifdef or correct them for correct cpu
  287. Revision 1.25 2002/07/26 21:15:42 florian
  288. * rewrote the system handling
  289. Revision 1.24 2002/07/07 09:52:33 florian
  290. * powerpc target fixed, very simple units can be compiled
  291. * some basic stuff for better callparanode handling, far from being finished
  292. Revision 1.23 2002/07/01 18:46:29 peter
  293. * internal linker
  294. * reorganized aasm layer
  295. Revision 1.22 2002/05/18 13:34:21 peter
  296. * readded missing revisions
  297. Revision 1.21 2002/05/16 19:46:49 carl
  298. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  299. + try to fix temp allocation (still in ifdef)
  300. + generic constructor calls
  301. + start of tassembler / tmodulebase class cleanup
  302. Revision 1.19 2002/05/12 16:53:16 peter
  303. * moved entry and exitcode to ncgutil and cgobj
  304. * foreach gets extra argument for passing local data to the
  305. iterator function
  306. * -CR checks also class typecasts at runtime by changing them
  307. into as
  308. * fixed compiler to cycle with the -CR option
  309. * fixed stabs with elf writer, finally the global variables can
  310. be watched
  311. * removed a lot of routines from cga unit and replaced them by
  312. calls to cgobj
  313. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  314. u32bit then the other is typecasted also to u32bit without giving
  315. a rangecheck warning/error.
  316. * fixed pascal calling method with reversing also the high tree in
  317. the parast, detected by tcalcst3 test
  318. Revision 1.18 2002/04/15 19:12:10 carl
  319. + target_info.size_of_pointer -> pointer_size
  320. + some cleanup of unused types/variables
  321. * move several constants from cpubase to their specific units
  322. (where they are used)
  323. + att_Reg2str -> gas_reg2str
  324. + int_reg2str -> std_reg2str
  325. Revision 1.17 2002/04/14 16:58:04 carl
  326. + move into aggas most of the stuff non-processor specific
  327. Revision 1.16 2002/04/10 08:07:55 jonas
  328. * fix for the ie9999 under Linux (patch from Peter)
  329. Revision 1.15 2002/04/04 19:06:06 peter
  330. * removed unused units
  331. * use tlocation.size in cg.a_*loc*() routines
  332. Revision 1.14 2002/04/04 18:26:55 carl
  333. + added wdosx patch from Pavel
  334. Revision 1.13 2002/04/02 17:11:33 peter
  335. * tlocation,treference update
  336. * LOC_CONSTANT added for better constant handling
  337. * secondadd splitted in multiple routines
  338. * location_force_reg added for loading a location to a register
  339. of a specified size
  340. * secondassignment parses now first the right and then the left node
  341. (this is compatible with Kylix). This saves a lot of push/pop especially
  342. with string operations
  343. * adapted some routines to use the new cg methods
  344. }