Merged with tip.
[cacao.git] / src / vm / jit / codegen-common.h
1 /* src/vm/jit/codegen-common.h - 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_H
27 #define _CODEGEN_COMMON_H
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 typedef struct exceptionref           exceptionref;
37 typedef struct linenumberref          linenumberref;
38
39
40 #include "config.h"
41 #include "vm/types.h"
42
43 #include "vm/builtin.h"
44 #include "vm/global.h"
45
46 #include "vm/jit/dseg.h"
47 #include "vm/jit/jit.h"
48 #include "vm/jit/reg.h"
49 #include "vm/jit/code.h"
50 #include "vm/jit/replace.h"
51
52 #include "vmcore/descriptor.h"
53 #include "vmcore/method.h"
54 #include "vmcore/references.h"
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         list_t         *brancheslabel;
133         list_t         *linenumbers;    /* list of line numbers                   */
134
135         methodinfo     *method;
136
137         s4              stackframesize;    /* stackframe size of this method      */
138
139 #if defined(ENABLE_REPLACEMENT)
140         rplpoint       *replacementpoint;  /* current replacement point           */
141 #endif
142 };
143
144
145 #define CODEGENDATA_FLAG_ERROR           0x00000001
146 #define CODEGENDATA_FLAG_LONGBRANCHES    0x00000002
147
148
149 #define CODEGENDATA_HAS_FLAG_ERROR(cd) \
150     ((cd)->flags & CODEGENDATA_FLAG_ERROR)
151
152 #define CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd) \
153     ((cd)->flags & CODEGENDATA_FLAG_LONGBRANCHES)
154
155
156 /* branchref *****************************************************************/
157
158 struct branchref {
159         s4         branchmpc;       /* patching position in code segment          */
160         s4         condition;       /* conditional branch condition               */
161         s4         reg;             /* register number to check                   */
162         u4         options;         /* branch options                             */
163         branchref *next;            /* next element in branchref list             */
164 };
165
166
167 /* branch_label_ref_t *********************************************************/
168
169 struct branch_label_ref_t {
170         s4         mpc;             /* position in code segment                   */
171         s4         label;           /* label number                               */
172         s4         condition;       /* conditional branch condition               */
173         s4         reg;             /* register number to check                   */
174         u4         options;         /* branch options                             */
175         listnode_t linkage;
176 };
177
178
179 /* jumpref ********************************************************************/
180
181 struct jumpref {
182         s4          tablepos;       /* patching position in data segment          */
183         basicblock *target;         /* target basic block                         */
184         jumpref    *next;           /* next element in jumpref list               */
185 };
186
187
188 /* dataref ********************************************************************/
189
190 struct dataref {
191         s4       datapos;           /* patching position in generated code        */
192         dataref *next;              /* next element in dataref list               */
193 };
194
195
196 /* linenumberref **************************************************************/
197
198 struct linenumberref {
199         s4             tablepos;    /* patching position in data segment          */
200         s4             linenumber;  /* line number, used for inserting into the   */
201                                     /* table and for validity checking            */
202                                     /* -1......start of inlined body              */
203                                     /* -2......end of inlined body                */
204                                     /* <= -3...special entry with methodinfo *    */
205                                                                 /* (see doc/inlining_stacktrace.txt)          */
206         ptrint         targetmpc;   /* machine code program counter of first      */
207                                     /* instruction for given line                 */
208                                                                 /* NOTE: for linenumber <= -3 this is a the   */
209                                     /* (methodinfo *) of the inlined method       */
210         linenumberref *next;        /* next element in linenumberref list         */
211 };
212
213
214 /* function prototypes ********************************************************/
215
216 void codegen_init(void);
217 void codegen_setup(jitdata *jd);
218
219 bool codegen_generate(jitdata *jd);
220 bool codegen_emit(jitdata *jd);
221
222 #if defined(ENABLE_INTRP)
223 bool intrp_codegen(jitdata *jd);
224 #endif
225
226 void codegen_close(void);
227
228 void codegen_increase(codegendata *cd);
229
230 #if defined(ENABLE_INTRP)
231 u1 *codegen_ncode_increase(codegendata *cd, u1 *ncodeptr);
232 #endif
233
234 void codegen_add_branch_ref(codegendata *cd, basicblock *target, s4 condition, s4 reg, u4 options);
235 void codegen_resolve_branchrefs(codegendata *cd, basicblock *bptr);
236
237 void codegen_branch_label_add(codegendata *cd, s4 label, s4 condition, s4 reg, u4 options);
238
239 #if defined(ENABLE_REPLACEMENT)
240 #if !defined(NDEBUG)
241 void codegen_set_replacement_point_notrap(codegendata *cd, s4 type);
242 void codegen_set_replacement_point(codegendata *cd, s4 type);
243 #else
244 void codegen_set_replacement_point_notrap(codegendata *cd);
245 void codegen_set_replacement_point(codegendata *cd);
246 #endif
247 #endif /* defined(ENABLE_REPLACEMENT) */
248
249 void codegen_finish(jitdata *jd);
250
251 #if defined(ENABLE_DISASSEMBLER)
252 void codegen_disassemble_stub(methodinfo *m, u1 *start, u1 *end);
253 #endif
254
255 /* stub functions */
256
257 u1       *codegen_generate_stub_compiler(methodinfo *m);
258 void      codegen_generate_stub_builtin(methodinfo *m, builtintable_entry *bte);
259 codeinfo *codegen_generate_stub_native(methodinfo *m, functionptr f);
260
261 #if !defined(JIT_COMPILER_VIA_SIGNAL)
262 void      codegen_emit_stub_compiler(jitdata *jd);
263 #endif
264 void      codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f, int skipparams);
265
266 #if defined(ENABLE_INTRP)
267 u1 *intrp_createcompilerstub(methodinfo *m);
268 u1 *intrp_createnativestub(functionptr f, jitdata *jd, methoddesc *md);
269 #endif
270
271 void removecompilerstub(u1 *stub);
272 void removenativestub(u1 *stub);
273
274 java_handle_t *codegen_start_native_call(u1 *currentsp, u1 *pv);
275 java_object_t *codegen_finish_native_call(u1 *currentsp, u1 *pv);
276
277 s4 codegen_reg_of_var(u2 opcode, varinfo *v, s4 tempregnum);
278 s4 codegen_reg_of_dst(jitdata *jd, instruction *iptr, s4 tempregnum);
279
280 #if defined(ENABLE_SSA)
281 void codegen_emit_phi_moves(jitdata *jd, basicblock *bptr);
282 #endif
283
284 #endif /* _CODEGEN_COMMON_H */
285
286
287 /*
288  * These are local overrides for various environment variables in Emacs.
289  * Please do not remove this and leave it at the end of the file, where
290  * Emacs will automagically detect them.
291  * ---------------------------------------------------------------------
292  * Local variables:
293  * mode: c
294  * indent-tabs-mode: t
295  * c-basic-offset: 4
296  * tab-width: 4
297  * End:
298  * vim:noexpandtab:sw=4:ts=4:
299  */