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