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