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