ag386att.pas 13 KB

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