* Merged with default branch at rev 16f3633aaa5a.
[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 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #ifndef _CODEGEN_COMMON_H
29 #define _CODEGEN_COMMON_H
30
31 /* forward typedefs ***********************************************************/
32
33 typedef struct codegendata            codegendata;
34 typedef struct branchref              branchref;
35 typedef struct branch_label_ref_t     branch_label_ref_t;
36 typedef struct critical_section_ref_t critical_section_ref_t;
37 typedef struct jumpref                jumpref;
38 typedef struct dataref                dataref;
39 typedef struct exceptionref           exceptionref;
40 typedef struct patchref               patchref;
41 typedef struct linenumberref          linenumberref;
42
43
44 #include "config.h"
45 #include "vm/types.h"
46
47 #include "vm/builtin.h"
48 #include "vm/global.h"
49
50 #include "vm/jit/dseg.h"
51 #include "vm/jit/jit.h"
52 #include "vm/jit/reg.h"
53 #include "vm/jit/code.h"
54 #include "vm/jit/replace.h"
55
56 #include "vmcore/descriptor.h"
57 #include "vmcore/method.h"
58 #include "vmcore/references.h"
59
60
61 #define MCODEINITSIZE (1<<15)       /* 32 Kbyte code area initialization size */
62 #define DSEGINITSIZE  (1<<12)       /*  4 Kbyte data area initialization size */
63
64 #define NCODEINITSIZE (1<<15)       /* 32 Kbyte code area initialization size */
65
66
67 /* Register Pack/Unpack Macros ************************************************/
68
69 /* ATTENTION: Don't change the order where low and high bits are
70    stored! At least mips32 relies in one case on that order. */
71
72 #define PACK_REGS(low,high) \
73     ( (((high) & 0x0000ffff) << 16) | ((low) & 0x0000ffff) )
74
75 #define GET_LOW_REG(a)      ((a) & 0x0000ffff)
76 #define GET_HIGH_REG(a)    (((a) & 0xffff0000) >> 16)
77
78
79 /* branch conditions **********************************************************/
80
81 #define BRANCH_UNCONDITIONAL    -1
82
83 #define BRANCH_EQ               (ICMD_IFEQ - ICMD_IFEQ)
84 #define BRANCH_NE               (ICMD_IFNE - ICMD_IFEQ)
85 #define BRANCH_LT               (ICMD_IFLT - ICMD_IFEQ)
86 #define BRANCH_GE               (ICMD_IFGE - ICMD_IFEQ)
87 #define BRANCH_GT               (ICMD_IFGT - ICMD_IFEQ)
88 #define BRANCH_LE               (ICMD_IFLE - ICMD_IFEQ)
89
90 #define BRANCH_ULT              256
91 #define BRANCH_ULE              257
92 #define BRANCH_UGE              258
93 #define BRANCH_UGT              259
94
95 #define BRANCH_NAN              260
96
97
98 /* common branch options ******************************************************/
99
100 #define BRANCH_OPT_NONE         0
101
102
103 /* codegendata ****************************************************************/
104
105 struct codegendata {
106         u4              flags;          /* code generator flags                   */
107         u1             *mcodebase;      /* base pointer of code area              */
108         u1             *mcodeend;       /* pointer to end of code area            */
109         s4              mcodesize;      /* complete size of code area (bytes)     */
110         u1             *mcodeptr;       /* code generation pointer                */
111         u1             *lastmcodeptr;   /* last patcher position of basic block   */
112
113 #if defined(ENABLE_INTRP)
114         u1             *ncodebase;      /* base pointer of native code area       */
115         s4              ncodesize;      /* complete size of native code area      */
116         u1             *ncodeptr;       /* native code generation pointer         */
117
118         u4              lastinstwithoutdispatch; /* ~0 if there was a dispatch    */
119
120         s4              lastpatcheroffset; /* -1 if current super has no patcher  */
121         s4              dynsuperm;      /* offsets of start of current dynamic ...*/
122         s4              dynsupern;      /* ... superinstruction starts            */
123         struct superstart *superstarts; /* list of supers without patchers        */
124 #endif
125
126         dsegentry      *dseg;           /* chain of data segment entries          */
127         s4              dseglen;        /* used size of data area (bytes)         */
128                                     /* data area grows from top to bottom     */
129
130         jumpref        *jumpreferences; /* list of jumptable target addresses     */
131
132 #if defined(__I386__) || defined(__X86_64__) || defined(__XDSPCORE__) || defined(__M68K__) || defined(ENABLE_INTRP) || defined(__S390__)
133         dataref        *datareferences; /* list of data segment references        */
134 #endif
135
136 /*      list_t         *patchrefs; */
137         patchref       *patchrefs;
138         list_t         *brancheslabel;
139         list_t         *listcritical;   /* list of critical sections              */
140
141         linenumberref  *linenumberreferences; /* list of line numbers and the     */
142                                         /* program counters of their first        */
143                                         /* instruction                            */
144         s4              linenumbertablesizepos;
145         s4              linenumbertablestartpos;
146         s4              linenumbertab;
147
148         methodinfo     *method;
149
150         s4              stackframesize;    /* stackframe size of this method      */
151
152 #if defined(ENABLE_REPLACEMENT)
153         rplpoint       *replacementpoint;  /* current replacement point           */
154 #endif
155 };
156
157
158 #define CODEGENDATA_FLAG_ERROR           0x00000001
159 #define CODEGENDATA_FLAG_LONGBRANCHES    0x00000002
160
161
162 #define CODEGENDATA_HAS_FLAG_ERROR(cd) \
163     ((cd)->flags & CODEGENDATA_FLAG_ERROR)
164
165 #define CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd) \
166     ((cd)->flags & CODEGENDATA_FLAG_LONGBRANCHES)
167
168
169 /* branchref *****************************************************************/
170
171 struct branchref {
172         s4         branchmpc;       /* patching position in code segment          */
173         s4         condition;       /* conditional branch condition               */
174         s4         reg;             /* register number to check                   */
175         u4         options;         /* branch options                             */
176         branchref *next;            /* next element in branchref list             */
177 };
178
179
180 /* branch_label_ref_t *********************************************************/
181
182 struct branch_label_ref_t {
183         s4         mpc;             /* position in code segment                   */
184         s4         label;           /* label number                               */
185         s4         condition;       /* conditional branch condition               */
186         s4         reg;             /* register number to check                   */
187         u4         options;         /* branch options                             */
188         listnode_t linkage;
189 };
190
191
192 /* critical_section_ref_t *****************************************************/
193
194 struct critical_section_ref_t {
195         s4         start;           /* relative offset to method entry-point      */
196         s4         end;
197         s4         restart;
198         listnode_t linkage;
199 };
200
201
202 /* jumpref ********************************************************************/
203
204 struct jumpref {
205         s4          tablepos;       /* patching position in data segment          */
206         basicblock *target;         /* target basic block                         */
207         jumpref    *next;           /* next element in jumpref list               */
208 };
209
210
211 /* dataref ********************************************************************/
212
213 struct dataref {
214         s4       datapos;           /* patching position in generated code        */
215         dataref *next;              /* next element in dataref list               */
216 };
217
218
219 /* patchref *******************************************************************/
220
221 struct patchref {
222         s4           branchpos;     /* relative offset to method entrypoint       */
223         s4           disp;          /* displacement of ref in the data segment    */
224         functionptr  patcher;       /* patcher function to call                   */
225         voidptr      ref;           /* reference passed                           */
226 /*      listnode     linkage; */
227         patchref    *next;
228 };
229
230
231 /* linenumberref **************************************************************/
232
233 struct linenumberref {
234         s4             tablepos;    /* patching position in data segment          */
235         s4             linenumber;  /* line number, used for inserting into the   */
236                                     /* table and for validity checking            */
237                                     /* -1......start of inlined body              */
238                                     /* -2......end of inlined body                */
239                                     /* <= -3...special entry with methodinfo *    */
240                                                                 /* (see doc/inlining_stacktrace.txt)          */
241         ptrint         targetmpc;   /* machine code program counter of first      */
242                                     /* instruction for given line                 */
243                                                                 /* NOTE: for linenumber <= -3 this is a the   */
244                                     /* (methodinfo *) of the inlined method       */
245         linenumberref *next;        /* next element in linenumberref list         */
246 };
247
248
249 /* methodtree_element *********************************************************/
250
251 typedef struct methodtree_element methodtree_element;
252
253 struct methodtree_element {
254         u1 *startpc;
255         u1 *endpc;
256 };
257
258
259 /* function prototypes ********************************************************/
260
261 void codegen_init(void);
262 void codegen_setup(jitdata *jd);
263
264 bool codegen_generate(jitdata *jd);
265 bool codegen_emit(jitdata *jd);
266
267 #if defined(ENABLE_INTRP)
268 bool intrp_codegen(jitdata *jd);
269 #endif
270
271 void codegen_close(void);
272
273 void codegen_increase(codegendata *cd);
274
275 #if defined(ENABLE_INTRP)
276 u1 *codegen_ncode_increase(codegendata *cd, u1 *ncodeptr);
277 #endif
278
279 void codegen_add_branch_ref(codegendata *cd, basicblock *target, s4 condition, s4 reg, u4 options);
280 void codegen_resolve_branchrefs(codegendata *cd, basicblock *bptr);
281
282 void codegen_branch_label_add(codegendata *cd, s4 label, s4 condition, s4 reg, u4 options);
283
284
285 void codegen_add_patch_ref(codegendata *cd, functionptr patcher, voidptr ref,
286                                                    s4 disp);
287 /* XXX REMOVE ME: don't-break-trunk macro */
288 #define codegen_addpatchref codegen_add_patch_ref
289
290 void codegen_insertmethod(u1 *startpc, u1 *endpc);
291 u1 *codegen_get_pv_from_pc(u1 *pc);
292 u1 *codegen_get_pv_from_pc_nocheck(u1 *pc);
293
294 #if defined(ENABLE_REPLACEMENT)
295 #if !defined(NDEBUG)
296 void codegen_set_replacement_point_notrap(codegendata *cd, s4 type);
297 void codegen_set_replacement_point(codegendata *cd, s4 type);
298 #else
299 void codegen_set_replacement_point_notrap(codegendata *cd);
300 void codegen_set_replacement_point(codegendata *cd);
301 #endif
302 #endif /* defined(ENABLE_REPLACEMENT) */
303
304 void codegen_finish(jitdata *jd);
305
306 #if defined(ENABLE_DISASSEMBLER)
307 void codegen_disassemble_nativestub(methodinfo *m, u1 *start, u1 *end);
308 #endif
309
310 /* stub functions */
311
312 u1       *codegen_generate_stub_compiler(methodinfo *m);
313 void      codegen_generate_stub_builtin(builtintable_entry *bte);
314 codeinfo *codegen_generate_stub_native(methodinfo *m, functionptr f);
315
316 void      codegen_emit_stub_compiler(jitdata *jd);
317 void      codegen_emit_stub_builtin(jitdata *jd, builtintable_entry *bte);
318 void      codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f);
319
320 #if defined(ENABLE_INTRP)
321 u1 *intrp_createcompilerstub(methodinfo *m);
322 u1 *intrp_createnativestub(functionptr f, jitdata *jd, methoddesc *md);
323 #endif
324
325 void removecompilerstub(u1 *stub);
326 void removenativestub(u1 *stub);
327
328 void codegen_stub_builtin_enter(u1 *datasp, u1 *pv, u1 *sp, u1 *ra);
329 void codegen_stub_builtin_exit(u1 *datasp);
330
331 void codegen_start_native_call(u1 *datasp, u1 *pv, u1 *sp, u1 *ra);
332 java_object_t *codegen_finish_native_call(u1 *datasp);
333
334 s4 codegen_reg_of_var(u2 opcode, varinfo *v, s4 tempregnum);
335 s4 codegen_reg_of_dst(jitdata *jd, instruction *iptr, s4 tempregnum);
336
337 #if defined(ENABLE_THREADS)
338 void codegen_critical_section_new(codegendata *cd);
339 void codegen_critical_section_start(codegendata *cd);
340 void codegen_critical_section_end(codegendata *cd);
341
342 # define CODEGEN_CRITICAL_SECTION_NEW      codegen_critical_section_new(cd)
343 # define CODEGEN_CRITICAL_SECTION_START    codegen_critical_section_start(cd)
344 # define CODEGEN_CRITICAL_SECTION_END      codegen_critical_section_end(cd)
345 #else
346 # define CODEGEN_CRITICAL_SECTION_NEW      /* no-op */
347 # define CODEGEN_CRITICAL_SECTION_START    /* no-op */
348 # define CODEGEN_CRITICAL_SECTION_END      /* no-op */
349 #endif
350
351 /* machine dependent functions */
352 u1 *md_codegen_get_pv_from_pc(u1 *ra);
353
354
355 #if defined(ENABLE_SSA)
356 void codegen_emit_phi_moves(jitdata *jd, basicblock *bptr);
357 #endif
358
359 #endif /* _CODEGEN_COMMON_H */
360
361
362 /*
363  * These are local overrides for various environment variables in Emacs.
364  * Please do not remove this and leave it at the end of the file, where
365  * Emacs will automagically detect them.
366  * ---------------------------------------------------------------------
367  * Local variables:
368  * mode: c
369  * indent-tabs-mode: t
370  * c-basic-offset: 4
371  * tab-width: 4
372  * End:
373  * vim:noexpandtab:sw=4:ts=4:
374  */