cgcpu.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the code generator for the i386
  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 the code generator for the i386.
  19. }
  20. unit cgcpu;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cgbase,cgobj,cg64f32,cgx86,
  25. aasmbase,aasmtai,aasmcpu,
  26. cpubase,cpuinfo,
  27. node,symconst
  28. {$ifdef delphi}
  29. ,dmisc
  30. {$endif}
  31. ;
  32. type
  33. tcg386 = class(tcgx86)
  34. procedure init_register_allocators;override;
  35. class function reg_cgsize(const reg: tregister): tcgsize; override;
  36. end;
  37. tcg64f386 = class(tcg64f32)
  38. procedure a_op64_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);override;
  39. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  40. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);override;
  41. procedure a_op64_const_ref(list : taasmoutput;op:TOpCG;value : qword;const ref : treference);override;
  42. private
  43. procedure get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp);
  44. end;
  45. implementation
  46. uses
  47. globtype,globals,verbose,systems,cutils,
  48. symdef,symsym,defutil,paramgr,
  49. rgcpu,rgx86,tgobj;
  50. procedure Tcg386.init_register_allocators;
  51. begin
  52. inherited init_register_allocators;
  53. if cs_create_pic in aktmoduleswitches then
  54. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_EAX,RS_EDX,RS_ECX,RS_ESI,RS_EDI],first_int_imreg,[RS_EBP,RS_EBX])
  55. else
  56. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_EAX,RS_EDX,RS_ECX,RS_EBX,RS_ESI,RS_EDI],first_int_imreg,[RS_EBP]);
  57. rg[R_MMXREGISTER]:=trgcpu.create(R_MMXREGISTER,R_SUBNONE,[RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3,RS_XMM4,RS_XMM5,RS_XMM6,RS_XMM7],first_sse_imreg,[]);
  58. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,[RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3,RS_XMM4,RS_XMM5,RS_XMM6,RS_XMM7],first_sse_imreg,[]);
  59. rgfpu:=Trgx86fpu.create;
  60. end;
  61. class function tcg386.reg_cgsize(const reg: tregister): tcgsize;
  62. const subreg2cgsize:array[Tsubregister] of Tcgsize =
  63. (OS_NO,OS_8,OS_8,OS_16,OS_32,OS_64,OS_NO,OS_NO);
  64. begin
  65. case getregtype(reg) of
  66. R_INTREGISTER :
  67. reg_cgsize:=subreg2cgsize[getsubreg(reg)];
  68. R_FPUREGISTER :
  69. reg_cgsize:=OS_F80;
  70. R_MMXREGISTER,
  71. R_MMREGISTER :
  72. reg_cgsize:=OS_M64;
  73. R_SPECIALREGISTER :
  74. case reg of
  75. NR_CS,NR_DS,NR_ES,NR_SS,NR_FS,NR_GS:
  76. reg_cgsize:=OS_16
  77. else
  78. reg_cgsize:=OS_32
  79. end
  80. else
  81. internalerror(200303181);
  82. end;
  83. end;
  84. { const
  85. opsize_2_cgsize: array[topsize] of tcgsize = (OS_NO,
  86. OS_8,OS_16,OS_32,OS_NO,OS_NO,OS_NO,
  87. OS_32,OS_64,OS_64,
  88. OS_F32,OS_F64,OS_F80,OS_F32,OS_F64,OS_M64,OS_NO,
  89. OS_NO,OS_NO,OS_NO
  90. );
  91. begin
  92. result := opsize_2_cgsize[reg2opsize(reg)];
  93. end;}
  94. { ************* 64bit operations ************ }
  95. procedure tcg64f386.get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp);
  96. begin
  97. case op of
  98. OP_ADD :
  99. begin
  100. op1:=A_ADD;
  101. op2:=A_ADC;
  102. end;
  103. OP_SUB :
  104. begin
  105. op1:=A_SUB;
  106. op2:=A_SBB;
  107. end;
  108. OP_XOR :
  109. begin
  110. op1:=A_XOR;
  111. op2:=A_XOR;
  112. end;
  113. OP_OR :
  114. begin
  115. op1:=A_OR;
  116. op2:=A_OR;
  117. end;
  118. OP_AND :
  119. begin
  120. op1:=A_AND;
  121. op2:=A_AND;
  122. end;
  123. else
  124. internalerror(200203241);
  125. end;
  126. end;
  127. procedure tcg64f386.a_op64_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);
  128. var
  129. op1,op2 : TAsmOp;
  130. tempref : treference;
  131. begin
  132. get_64bit_ops(op,op1,op2);
  133. list.concat(taicpu.op_ref_reg(op1,S_L,ref,reg.reglo));
  134. tempref:=ref;
  135. inc(tempref.offset,4);
  136. list.concat(taicpu.op_ref_reg(op2,S_L,tempref,reg.reghi));
  137. end;
  138. procedure tcg64f386.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  139. var
  140. op1,op2 : TAsmOp;
  141. begin
  142. case op of
  143. OP_NEG :
  144. begin
  145. if (regsrc.reglo<>regdst.reglo) then
  146. a_load64_reg_reg(list,regsrc,regdst);
  147. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reghi));
  148. list.concat(taicpu.op_reg(A_NEG,S_L,regdst.reglo));
  149. list.concat(taicpu.op_const_reg(A_SBB,S_L,aword(-1),regdst.reghi));
  150. exit;
  151. end;
  152. OP_NOT :
  153. begin
  154. if (regsrc.reglo<>regdst.reglo) then
  155. a_load64_reg_reg(list,regsrc,regdst);
  156. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reghi));
  157. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reglo));
  158. exit;
  159. end;
  160. end;
  161. get_64bit_ops(op,op1,op2);
  162. list.concat(taicpu.op_reg_reg(op1,S_L,regsrc.reglo,regdst.reglo));
  163. list.concat(taicpu.op_reg_reg(op2,S_L,regsrc.reghi,regdst.reghi));
  164. end;
  165. procedure tcg64f386.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  166. var
  167. op1,op2 : TAsmOp;
  168. begin
  169. case op of
  170. OP_AND,OP_OR,OP_XOR:
  171. begin
  172. cg.a_op_const_reg(list,op,OS_32,lo(value),reg.reglo);
  173. cg.a_op_const_reg(list,op,OS_32,hi(value),reg.reghi);
  174. end;
  175. OP_ADD, OP_SUB:
  176. begin
  177. // can't use a_op_const_ref because this may use dec/inc
  178. get_64bit_ops(op,op1,op2);
  179. list.concat(taicpu.op_const_reg(op1,S_L,lo(value),reg.reglo));
  180. list.concat(taicpu.op_const_reg(op2,S_L,hi(value),reg.reghi));
  181. end;
  182. else
  183. internalerror(200204021);
  184. end;
  185. end;
  186. procedure tcg64f386.a_op64_const_ref(list : taasmoutput;op:TOpCG;value : qword;const ref : treference);
  187. var
  188. op1,op2 : TAsmOp;
  189. tempref : treference;
  190. begin
  191. case op of
  192. OP_AND,OP_OR,OP_XOR:
  193. begin
  194. cg.a_op_const_ref(list,op,OS_32,lo(value),ref);
  195. tempref:=ref;
  196. inc(tempref.offset,4);
  197. cg.a_op_const_ref(list,op,OS_32,hi(value),tempref);
  198. end;
  199. OP_ADD, OP_SUB:
  200. begin
  201. get_64bit_ops(op,op1,op2);
  202. // can't use a_op_const_ref because this may use dec/inc
  203. list.concat(taicpu.op_const_ref(op1,S_L,lo(value),ref));
  204. tempref:=ref;
  205. inc(tempref.offset,4);
  206. list.concat(taicpu.op_const_ref(op2,S_L,hi(value),tempref));
  207. end;
  208. else
  209. internalerror(200204022);
  210. end;
  211. end;
  212. begin
  213. cg := tcg386.create;
  214. cg64 := tcg64f386.create;
  215. end.
  216. {
  217. $Log$
  218. Revision 1.44 2004-01-14 23:39:05 florian
  219. * another bunch of x86-64 fixes mainly calling convention and
  220. assembler reader related
  221. Revision 1.43 2004/01/12 16:39:40 peter
  222. * sparc updates, mostly float related
  223. Revision 1.42 2003/12/24 00:10:02 florian
  224. - delete parameter in cg64 methods removed
  225. Revision 1.41 2003/12/19 22:08:44 daniel
  226. * Some work to restore the MMX capabilities
  227. Revision 1.40 2003/10/10 17:48:14 peter
  228. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  229. * tregisteralloctor renamed to trgobj
  230. * removed rgobj from a lot of units
  231. * moved location_* and reference_* to cgobj
  232. * first things for mmx register allocation
  233. Revision 1.39 2003/10/01 20:34:49 peter
  234. * procinfo unit contains tprocinfo
  235. * cginfo renamed to cgbase
  236. * moved cgmessage to verbose
  237. * fixed ppc and sparc compiles
  238. Revision 1.38 2003/09/25 13:13:32 florian
  239. * more x86-64 fixes
  240. Revision 1.37 2003/09/03 15:55:01 peter
  241. * NEWRA branch merged
  242. Revision 1.36.2.1 2003/08/29 17:28:59 peter
  243. * next batch of updates
  244. Revision 1.36 2003/06/12 18:31:18 peter
  245. * fix newra cycle for i386
  246. Revision 1.35 2003/06/03 21:11:09 peter
  247. * cg.a_load_* get a from and to size specifier
  248. * makeregsize only accepts newregister
  249. * i386 uses generic tcgnotnode,tcgunaryminus
  250. Revision 1.34 2003/06/01 21:38:06 peter
  251. * getregisterfpu size parameter added
  252. * op_const_reg size parameter added
  253. * sparc updates
  254. Revision 1.33 2003/05/22 21:32:28 peter
  255. * removed some unit dependencies
  256. Revision 1.32 2002/11/25 17:43:26 peter
  257. * splitted defbase in defutil,symutil,defcmp
  258. * merged isconvertable and is_equal into compare_defs(_ext)
  259. * made operator search faster by walking the list only once
  260. Revision 1.31 2002/10/05 12:43:29 carl
  261. * fixes for Delphi 6 compilation
  262. (warning : Some features do not work under Delphi)
  263. Revision 1.30 2002/09/07 15:25:10 peter
  264. * old logs removed and tabs fixed
  265. Revision 1.29 2002/07/20 19:28:47 florian
  266. * splitting of i386\cgcpu.pas into x86\cgx86.pas and i386\cgcpu.pas
  267. cgx86.pas will contain the common code for i386 and x86_64
  268. Revision 1.28 2002/07/20 11:58:00 florian
  269. * types.pas renamed to defbase.pas because D6 contains a types
  270. unit so this would conflicts if D6 programms are compiled
  271. + Willamette/SSE2 instructions to assembler added
  272. Revision 1.27 2002/07/11 14:41:32 florian
  273. * start of the new generic parameter handling
  274. Revision 1.26 2002/07/07 09:52:33 florian
  275. * powerpc target fixed, very simple units can be compiled
  276. * some basic stuff for better callparanode handling, far from being finished
  277. Revision 1.25 2002/07/01 18:46:30 peter
  278. * internal linker
  279. * reorganized aasm layer
  280. Revision 1.24 2002/07/01 16:23:55 peter
  281. * cg64 patch
  282. * basics for currency
  283. * asnode updates for class and interface (not finished)
  284. Revision 1.23 2002/06/16 08:16:59 carl
  285. * bugfix of missing popecx for shift operations
  286. Revision 1.22 2002/05/22 19:02:16 carl
  287. + generic FPC_HELP_FAIL
  288. + generic FPC_HELP_DESTRUCTOR instated (original from Pierre)
  289. + generic FPC_DISPOSE_CLASS
  290. + TEST_GENERIC define
  291. Revision 1.21 2002/05/20 13:30:40 carl
  292. * bugfix of hdisponen (base must be set, not index)
  293. * more portability fixes
  294. Revision 1.20 2002/05/18 13:34:22 peter
  295. * readded missing revisions
  296. Revision 1.19 2002/05/16 19:46:50 carl
  297. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  298. + try to fix temp allocation (still in ifdef)
  299. + generic constructor calls
  300. + start of tassembler / tmodulebase class cleanup
  301. Revision 1.17 2002/05/13 19:54:37 peter
  302. * removed n386ld and n386util units
  303. * maybe_save/maybe_restore added instead of the old maybe_push
  304. Revision 1.16 2002/05/12 19:59:05 carl
  305. * some small portability fixes
  306. Revision 1.15 2002/05/12 16:53:16 peter
  307. * moved entry and exitcode to ncgutil and cgobj
  308. * foreach gets extra argument for passing local data to the
  309. iterator function
  310. * -CR checks also class typecasts at runtime by changing them
  311. into as
  312. * fixed compiler to cycle with the -CR option
  313. * fixed stabs with elf writer, finally the global variables can
  314. be watched
  315. * removed a lot of routines from cga unit and replaced them by
  316. calls to cgobj
  317. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  318. u32bit then the other is typecasted also to u32bit without giving
  319. a rangecheck warning/error.
  320. * fixed pascal calling method with reversing also the high tree in
  321. the parast, detected by tcalcst3 test
  322. Revision 1.14 2002/04/25 20:16:40 peter
  323. * moved more routines from cga/n386util
  324. Revision 1.13 2002/04/21 15:31:05 carl
  325. * changeregsize -> rg.makeregsize
  326. + a_jmp_always added
  327. Revision 1.12 2002/04/15 19:44:20 peter
  328. * fixed stackcheck that would be called recursively when a stack
  329. error was found
  330. * generic changeregsize(reg,size) for i386 register resizing
  331. * removed some more routines from cga unit
  332. * fixed returnvalue handling
  333. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  334. Revision 1.11 2002/04/04 19:06:10 peter
  335. * removed unused units
  336. * use tlocation.size in a_*loc*() routines
  337. Revision 1.10 2002/04/02 20:29:02 jonas
  338. * optimized the code generated by the a_op_const_* and a_op64_const
  339. methods
  340. Revision 1.9 2002/04/02 17:11:33 peter
  341. * tlocation,treference update
  342. * LOC_CONSTANT added for better constant handling
  343. * secondadd splitted in multiple routines
  344. * location_force_reg added for loading a location to a register
  345. of a specified size
  346. * secondassignment parses now first the right and then the left node
  347. (this is compatible with Kylix). This saves a lot of push/pop especially
  348. with string operations
  349. * adapted some routines to use the new cg methods
  350. }