68fc49bdff27c263109524fe155e1f75d8680ccb
[cacao.git] / src / vm / jit / codegen-common.hpp
1 /* src/vm/jit/codegen-common.hpp - 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
37
38 #include "config.h"
39 #include "vm/types.h"
40
41 #include "toolbox/list.hpp"
42
43 #include "vm/jit/builtin.hpp"
44 #include "vm/descriptor.hpp"
45 #include "vm/global.h"
46 #include "vm/method.hpp"
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.hpp"
53 #include "vm/jit/linenumbertable.hpp"
54 #include "vm/jit/replace.hpp"
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 /* All 32-bit machines we support use packed registers to store
75    return values and temporary values. */
76
77 #if SIZEOF_VOID_P == 8
78 # define REG_LRESULT         REG_RESULT
79 # define REG_LTMP12          REG_ITMP1
80 # define REG_LTMP23          REG_ITMP2
81 #else
82 # define REG_LRESULT         REG_RESULT_PACKED
83 # define REG_LTMP12          REG_ITMP12_PACKED
84 # define REG_LTMP23          REG_ITMP23_PACKED
85 #endif
86
87
88 /* branch conditions **********************************************************/
89
90 #define BRANCH_UNCONDITIONAL    -1
91
92 #define BRANCH_EQ               (ICMD_IFEQ - ICMD_IFEQ)
93 #define BRANCH_NE               (ICMD_IFNE - ICMD_IFEQ)
94 #define BRANCH_LT               (ICMD_IFLT - ICMD_IFEQ)
95 #define BRANCH_GE               (ICMD_IFGE - ICMD_IFEQ)
96 #define BRANCH_GT               (ICMD_IFGT - ICMD_IFEQ)
97 #define BRANCH_LE               (ICMD_IFLE - ICMD_IFEQ)
98
99 #define BRANCH_ULT              256
100 #define BRANCH_ULE              257
101 #define BRANCH_UGE              258
102 #define BRANCH_UGT              259
103
104 #define BRANCH_NAN              260
105
106
107 /* common branch options ******************************************************/
108
109 #define BRANCH_OPT_NONE         0
110
111
112 /* codegendata ****************************************************************/
113
114 struct codegendata {
115         u4              flags;          /* code generator flags                   */
116         u1             *mcodebase;      /* base pointer of code area              */
117         u1             *mcodeend;       /* pointer to end of code area            */
118         s4              mcodesize;      /* complete size of code area (bytes)     */
119         u1             *mcodeptr;       /* code generation pointer                */
120         u1             *lastmcodeptr;   /* last patcher position of basic block   */
121
122 #if defined(ENABLE_INTRP)
123         u1             *ncodebase;      /* base pointer of native code area       */
124         s4              ncodesize;      /* complete size of native code area      */
125         u1             *ncodeptr;       /* native code generation pointer         */
126
127         u4              lastinstwithoutdispatch; /* ~0 if there was a dispatch    */
128
129         s4              lastpatcheroffset; /* -1 if current super has no patcher  */
130         s4              dynsuperm;      /* offsets of start of current dynamic ...*/
131         s4              dynsupern;      /* ... superinstruction starts            */
132         struct superstart *superstarts; /* list of supers without patchers        */
133 #endif
134
135         dsegentry      *dseg;           /* chain of data segment entries          */
136         s4              dseglen;        /* used size of data area (bytes)         */
137                                     /* data area grows from top to bottom     */
138
139         jumpref        *jumpreferences; /* list of jumptable target addresses     */
140
141 #if defined(__I386__) || defined(__X86_64__) || defined(__XDSPCORE__) || defined(__M68K__) || defined(ENABLE_INTRP) || defined(__S390__)
142         dataref        *datareferences; /* list of data segment references        */
143 #endif
144
145 #ifdef __cplusplus
146         DumpList<branch_label_ref_t*>* brancheslabel;
147         DumpList<Linenumber>* linenumbers; ///< List of line numbers.
148 #else
149         // REMOVEME
150         DumpList* brancheslabel;
151         DumpList* linenumbers;
152 #endif
153
154         methodinfo     *method;
155
156         s4              stackframesize;    /* stackframe size of this method      */
157
158 #if defined(ENABLE_REPLACEMENT)
159         rplpoint       *replacementpoint;  /* current replacement point           */
160 #endif
161 };
162
163
164 #define CODEGENDATA_FLAG_ERROR           0x00000001
165 #define CODEGENDATA_FLAG_LONGBRANCHES    0x00000002
166
167
168 #define CODEGENDATA_HAS_FLAG_ERROR(cd) \
169     ((cd)->flags & CODEGENDATA_FLAG_ERROR)
170
171 #define CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd) \
172     ((cd)->flags & CODEGENDATA_FLAG_LONGBRANCHES)
173
174
175 /* branchref *****************************************************************/
176
177 struct branchref {
178         s4         branchmpc;       /* patching position in code segment          */
179         s4         condition;       /* conditional branch condition               */
180         s4         reg;             /* register number to check                   */
181         u4         options;         /* branch options                             */
182         branchref *next;            /* next element in branchref list             */
183 };
184
185
186 /* branch_label_ref_t *********************************************************/
187
188 struct branch_label_ref_t {
189         s4         mpc;             /* position in code segment                   */
190         s4         label;           /* label number                               */
191         s4         condition;       /* conditional branch condition               */
192         s4         reg;             /* register number to check                   */
193         u4         options;         /* branch options                             */
194 /*      listnode_t linkage; */
195 };
196
197
198 /* jumpref ********************************************************************/
199
200 struct jumpref {
201         s4          tablepos;       /* patching position in data segment          */
202         basicblock *target;         /* target basic block                         */
203         jumpref    *next;           /* next element in jumpref list               */
204 };
205
206
207 /* dataref ********************************************************************/
208
209 struct dataref {
210         s4       datapos;           /* patching position in generated code        */
211         dataref *next;              /* next element in dataref list               */
212 };
213
214
215 /* function prototypes ********************************************************/
216
217 #ifdef __cplusplus
218 extern "C" {
219 #endif
220
221 void codegen_init(void);
222 void codegen_setup(jitdata *jd);
223
224 bool codegen_generate(jitdata *jd);
225 bool codegen_emit(jitdata *jd);
226
227 void codegen_emit_prolog(jitdata* jd);
228 void codegen_emit_epilog(jitdata* jd);
229 void codegen_emit_instruction(jitdata* jd, instruction* iptr);
230
231 #if defined(ENABLE_INTRP)
232 bool intrp_codegen(jitdata *jd);
233 #endif
234
235 void codegen_close(void);
236
237 void codegen_increase(codegendata *cd);
238
239 #if defined(ENABLE_INTRP)
240 u1 *codegen_ncode_increase(codegendata *cd, u1 *ncodeptr);
241 #endif
242
243 void codegen_add_branch_ref(codegendata *cd, basicblock *target, s4 condition, s4 reg, u4 options);
244 void codegen_resolve_branchrefs(codegendata *cd, basicblock *bptr);
245
246 void codegen_branch_label_add(codegendata *cd, s4 label, s4 condition, s4 reg, u4 options);
247
248 #if defined(ENABLE_REPLACEMENT)
249 #if !defined(NDEBUG)
250 void codegen_set_replacement_point_notrap(codegendata *cd, s4 type);
251 void codegen_set_replacement_point(codegendata *cd, s4 type);
252 #else
253 void codegen_set_replacement_point_notrap(codegendata *cd);
254 void codegen_set_replacement_point(codegendata *cd);
255 #endif
256 #endif /* defined(ENABLE_REPLACEMENT) */
257
258 void codegen_finish(jitdata *jd);
259
260 java_handle_t *codegen_start_native_call(u1 *currentsp, u1 *pv);
261 java_object_t *codegen_finish_native_call(u1 *currentsp, u1 *pv);
262
263 s4 codegen_reg_of_var(u2 opcode, varinfo *v, s4 tempregnum);
264 s4 codegen_reg_of_dst(jitdata *jd, instruction *iptr, s4 tempregnum);
265
266 #if defined(ENABLE_SSA)
267 void codegen_emit_phi_moves(jitdata *jd, basicblock *bptr);
268 #endif
269
270 // REMOVEME
271 void codegen_emit_stub_compiler(jitdata *jd);
272 void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f, int skipparams);
273
274 #ifdef __cplusplus
275 }
276 #endif
277
278 #endif // _CODEGEN_COMMON_HPP
279
280
281 /*
282  * These are local overrides for various environment variables in Emacs.
283  * Please do not remove this and leave it at the end of the file, where
284  * Emacs will automagically detect them.
285  * ---------------------------------------------------------------------
286  * Local variables:
287  * mode: c++
288  * indent-tabs-mode: t
289  * c-basic-offset: 4
290  * tab-width: 4
291  * End:
292  * vim:noexpandtab:sw=4:ts=4:
293  */