* Updated to jitcache-arm-x86 branch d4f6023b26c5+d1b5b1c106ac
[cacao.git] / src / vm / jit / codegen-common.hpp
1 /* src/vm/jit/codegen-common.hpp - architecture independent code generator stuff
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _CODEGEN_COMMON_HPP
27 #define _CODEGEN_COMMON_HPP
28
29 /* forward typedefs ***********************************************************/
30
31 typedef struct codegendata            codegendata;
32 typedef struct branchref              branchref;
33 typedef struct branch_label_ref_t     branch_label_ref_t;
34 typedef struct jumpref                jumpref;
35 typedef struct dataref                dataref;
36
37
38 #include "config.h"
39 #include "vm/types.h"
40
41 #include "toolbox/list.hpp"
42
43 #include "vm/jit/builtin.hpp"
44 #include "vm/descriptor.h"
45 #include "vm/global.h"
46 #include "vm/method.h"
47 #include "vm/references.h"
48
49 #include "vm/jit/dseg.h"
50 #include "vm/jit/jit.hpp"
51 #include "vm/jit/reg.h"
52 #include "vm/jit/code.hpp"
53 #include "vm/jit/linenumbertable.hpp"
54 #include "vm/jit/replace.hpp"
55
56
57 #define MCODEINITSIZE (1<<15)       /* 32 Kbyte code area initialization size */
58 #define DSEGINITSIZE  (1<<12)       /*  4 Kbyte data area initialization size */
59
60 #define NCODEINITSIZE (1<<15)       /* 32 Kbyte code area initialization size */
61
62
63 /* Register Pack/Unpack Macros ************************************************/
64
65 /* ATTENTION: Don't change the order where low and high bits are
66    stored! At least mips32 relies in one case on that order. */
67
68 #define PACK_REGS(low,high) \
69     ( (((high) & 0x0000ffff) << 16) | ((low) & 0x0000ffff) )
70
71 #define GET_LOW_REG(a)      ((a) & 0x0000ffff)
72 #define GET_HIGH_REG(a)    (((a) & 0xffff0000) >> 16)
73
74
75 /* branch conditions **********************************************************/
76
77 #define BRANCH_UNCONDITIONAL    -1
78
79 #define BRANCH_EQ               (ICMD_IFEQ - ICMD_IFEQ)
80 #define BRANCH_NE               (ICMD_IFNE - ICMD_IFEQ)
81 #define BRANCH_LT               (ICMD_IFLT - ICMD_IFEQ)
82 #define BRANCH_GE               (ICMD_IFGE - ICMD_IFEQ)
83 #define BRANCH_GT               (ICMD_IFGT - ICMD_IFEQ)
84 #define BRANCH_LE               (ICMD_IFLE - ICMD_IFEQ)
85
86 #define BRANCH_ULT              256
87 #define BRANCH_ULE              257
88 #define BRANCH_UGE              258
89 #define BRANCH_UGT              259
90
91 #define BRANCH_NAN              260
92
93
94 /* common branch options ******************************************************/
95
96 #define BRANCH_OPT_NONE         0
97
98
99 /* codegendata ****************************************************************/
100
101 struct codegendata {
102         u4              flags;          /* code generator flags                   */
103         u1             *mcodebase;      /* base pointer of code area              */
104         u1             *mcodeend;       /* pointer to end of code area            */
105         s4              mcodesize;      /* complete size of code area (bytes)     */
106         u1             *mcodeptr;       /* code generation pointer                */
107         u1             *lastmcodeptr;   /* last patcher position of basic block   */
108
109 #if defined(ENABLE_INTRP)
110         u1             *ncodebase;      /* base pointer of native code area       */
111         s4              ncodesize;      /* complete size of native code area      */
112         u1             *ncodeptr;       /* native code generation pointer         */
113
114         u4              lastinstwithoutdispatch; /* ~0 if there was a dispatch    */
115
116         s4              lastpatcheroffset; /* -1 if current super has no patcher  */
117         s4              dynsuperm;      /* offsets of start of current dynamic ...*/
118         s4              dynsupern;      /* ... superinstruction starts            */
119         struct superstart *superstarts; /* list of supers without patchers        */
120 #endif
121
122         dsegentry      *dseg;           /* chain of data segment entries          */
123         s4              dseglen;        /* used size of data area (bytes)         */
124                                     /* data area grows from top to bottom     */
125
126         jumpref        *jumpreferences; /* list of jumptable target addresses     */
127
128 #if defined(__I386__) || defined(__X86_64__) || defined(__XDSPCORE__) || defined(__M68K__) || defined(ENABLE_INTRP) || defined(__S390__)
129         dataref        *datareferences; /* list of data segment references        */
130 #endif
131
132 #ifdef __cplusplus
133         DumpList<branch_label_ref_t*>* brancheslabel;
134         DumpList<Linenumber>* linenumbers; ///< List of line numbers.
135 #else
136         // REMOVEME
137         DumpList* brancheslabel;
138         DumpList* linenumbers;
139 #endif
140
141         methodinfo     *method;
142
143         s4              stackframesize;    /* stackframe size of this method      */
144
145 #if defined(ENABLE_REPLACEMENT)
146         rplpoint       *replacementpoint;  /* current replacement point           */
147 #endif
148 };
149
150
151 #define CODEGENDATA_FLAG_ERROR           0x00000001
152 #define CODEGENDATA_FLAG_LONGBRANCHES    0x00000002
153
154
155 #define CODEGENDATA_HAS_FLAG_ERROR(cd) \
156     ((cd)->flags & CODEGENDATA_FLAG_ERROR)
157
158 #define CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd) \
159     ((cd)->flags & CODEGENDATA_FLAG_LONGBRANCHES)
160
161
162 /* branchref *****************************************************************/
163
164 struct branchref {
165         s4         branchmpc;       /* patching position in code segment          */
166         s4         condition;       /* conditional branch condition               */
167         s4         reg;             /* register number to check                   */
168         u4         options;         /* branch options                             */
169         branchref *next;            /* next element in branchref list             */
170 };
171
172
173 /* branch_label_ref_t *********************************************************/
174
175 struct branch_label_ref_t {
176         s4         mpc;             /* position in code segment                   */
177         s4         label;           /* label number                               */
178         s4         condition;       /* conditional branch condition               */
179         s4         reg;             /* register number to check                   */
180         u4         options;         /* branch options                             */
181 /*      listnode_t linkage; */
182 };
183
184
185 /* jumpref ********************************************************************/
186
187 struct jumpref {
188         s4          tablepos;       /* patching position in data segment          */
189         basicblock *target;         /* target basic block                         */
190         jumpref    *next;           /* next element in jumpref list               */
191 };
192
193
194 /* dataref ********************************************************************/
195
196 struct dataref {
197         s4       datapos;           /* patching position in generated code        */
198         dataref *next;              /* next element in dataref list               */
199 };
200
201
202 /* function prototypes ********************************************************/
203
204 #ifdef __cplusplus
205 extern "C" {
206 #endif
207
208 void codegen_init(void);
209 void codegen_setup(jitdata *jd);
210
211 bool codegen_generate(jitdata *jd);
212 bool codegen_emit(jitdata *jd);
213
214 #if defined(ENABLE_INTRP)
215 bool intrp_codegen(jitdata *jd);
216 #endif
217
218 void codegen_close(void);
219
220 void codegen_increase(codegendata *cd);
221
222 #if defined(ENABLE_INTRP)
223 u1 *codegen_ncode_increase(codegendata *cd, u1 *ncodeptr);
224 #endif
225
226 void codegen_add_branch_ref(codegendata *cd, basicblock *target, s4 condition, s4 reg, u4 options);
227 void codegen_resolve_branchrefs(codegendata *cd, basicblock *bptr);
228
229 void codegen_branch_label_add(codegendata *cd, s4 label, s4 condition, s4 reg, u4 options);
230
231 #if defined(ENABLE_REPLACEMENT)
232 #if !defined(NDEBUG)
233 void codegen_set_replacement_point_notrap(codegendata *cd, s4 type);
234 void codegen_set_replacement_point(codegendata *cd, s4 type);
235 #else
236 void codegen_set_replacement_point_notrap(codegendata *cd);
237 void codegen_set_replacement_point(codegendata *cd);
238 #endif
239 #endif /* defined(ENABLE_REPLACEMENT) */
240
241 void codegen_finish(jitdata *jd);
242
243 java_handle_t *codegen_start_native_call(u1 *currentsp, u1 *pv);
244 java_object_t *codegen_finish_native_call(u1 *currentsp, u1 *pv);
245
246 s4 codegen_reg_of_var(u2 opcode, varinfo *v, s4 tempregnum);
247 s4 codegen_reg_of_dst(jitdata *jd, instruction *iptr, s4 tempregnum);
248
249 #if defined(ENABLE_SSA)
250 void codegen_emit_phi_moves(jitdata *jd, basicblock *bptr);
251 #endif
252
253 // REMOVEME
254 void codegen_emit_stub_compiler(jitdata *jd);
255 void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f, int skipparams);
256
257 #ifdef __cplusplus
258 }
259 #endif
260
261 #endif // _CODEGEN_COMMON_HPP
262
263
264 /*
265  * These are local overrides for various environment variables in Emacs.
266  * Please do not remove this and leave it at the end of the file, where
267  * Emacs will automagically detect them.
268  * ---------------------------------------------------------------------
269  * Local variables:
270  * mode: c++
271  * indent-tabs-mode: t
272  * c-basic-offset: 4
273  * tab-width: 4
274  * End:
275  * vim:noexpandtab:sw=4:ts=4:
276  */