* Updated to jitcache-arm-x86 branch d4f6023b26c5+d1b5b1c106ac
[cacao.git] / src / vm / jit / code.hpp
1 /* src/vm/jit/code.hpp - codeinfo struct for representing compiled code
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 _CODE_HPP
27 #define _CODE_HPP
28
29 #include "config.h"
30
31 #include <assert.h>
32 #include <stdint.h>
33
34 #include "vm/types.h"
35
36 #include "toolbox/list.hpp"
37
38 #include "vm/global.h"
39 #include "vm/method.h"
40
41 #include "vm/jit/exceptiontable.h"
42 #if defined (ENABLE_JITCACHE)
43 #include "vm/jit/jitcache.hpp"
44 #endif
45 #include "vm/jit/linenumbertable.hpp"
46 #include "vm/jit/methodheader.h"
47 #include "vm/jit/patcher-common.hpp"
48 #include "vm/jit/replace.hpp"
49
50
51 /* constants ******************************************************************/
52
53 #define CODE_FLAG_INVALID         0x0001
54 #define CODE_FLAG_LEAFMETHOD      0x0002
55 #define CODE_FLAG_SYNCHRONIZED    0x0004
56 #define CODE_FLAG_TLH             0x0008
57
58
59 /* codeinfo *******************************************************************
60
61    A codeinfo represents a particular realization of a method in
62    machine code.
63
64    ATTENTION: The methodinfo entry in the code-structure MUST have the
65    offset 0, otherwise we have a problem in our compiler stub. This is
66    checked with an assert in code_init().
67
68 *******************************************************************************/
69
70 struct codeinfo {
71         methodinfo   *m;                    /* method this is a realization of    */
72         codeinfo     *prev;                 /* previous codeinfo of this method   */
73
74         uint32_t      flags;                /* OR of CODE_FLAG_ constants         */
75
76         u1            optlevel;             /* optimization level of this code    */
77         s4            basicblockcount;      /* number of basic blocks             */
78
79         int32_t       synchronizedoffset;   /* stack offset of synchronized obj.  */
80
81         /* machine code */
82         u1           *mcode;                /* pointer to machine code            */
83         u1           *entrypoint;           /* machine code entry point           */
84         s4            mcodelength;          /* length of generated machine code   */
85
86         exceptiontable_t  *exceptiontable;
87         LinenumberTable* linenumbertable;
88
89         /* patcher list */
90 #ifdef __cplusplus
91         List<patchref_t>* patchers;
92 #else
93         List*         patchers;
94 #endif
95
96 #if defined (ENABLE_JITCACHE)
97 #ifdef __cplusplus
98         List<cachedref_t>* cachedrefs;
99 #else
100         List*         cachedrefs;
101 #endif
102 #endif
103
104         /* replacement */                                   
105         s4            stackframesize;       /* size of the stackframe in slots    */
106
107 #if defined(ENABLE_REPLACEMENT)
108         rplpoint     *rplpoints;            /* replacement points                 */
109         rplalloc     *regalloc;             /* register allocation info           */
110         s4            rplpointcount;        /* number of replacement points       */
111         s4            globalcount;          /* number of global allocations       */
112         s4            regalloccount;        /* number of total allocations        */
113         s4            memuse;               /* number of arg + local slots        */
114         u1            savedintcount;        /* number of callee saved int regs    */
115         u1            savedfltcount;        /* number of callee saved flt regs    */
116 # if defined(HAS_ADDRESS_REGISTER_FILE)
117         u1            savedadrcount;        /* number of callee saved adr regs    */
118 # endif
119         u1           *savedmcode;           /* saved code under patches           */
120 #endif
121
122 #if defined(ENABLE_PROFILING)
123         u4            frequency;            /* number of method invocations       */
124         u4           *bbfrequency;                  
125         s8            cycles;               /* number of cpu cycles               */
126 #endif
127 };
128
129
130 #ifdef __cplusplus
131 extern "C" {
132 #endif
133
134 /* inline functions ***********************************************************/
135
136 /* code_xxx_invalid ************************************************************
137
138    Functions for CODE_FLAG_INVALID.
139
140 *******************************************************************************/
141
142 inline static int code_is_invalid(codeinfo *code)
143 {
144         return (code->flags & CODE_FLAG_INVALID);
145 }
146
147 inline static void code_flag_invalid(codeinfo *code)
148 {
149         code->flags |= CODE_FLAG_INVALID;
150 }
151
152 inline static void code_unflag_invalid(codeinfo *code)
153 {
154         code->flags &= ~CODE_FLAG_INVALID;
155 }
156
157
158 /* code_xxx_leafmethod *********************************************************
159
160    Functions for CODE_FLAG_LEAFMETHOD.
161
162 *******************************************************************************/
163
164 inline static int code_is_leafmethod(codeinfo *code)
165 {
166         return (code->flags & CODE_FLAG_LEAFMETHOD);
167 }
168
169 inline static void code_flag_leafmethod(codeinfo *code)
170 {
171         code->flags |= CODE_FLAG_LEAFMETHOD;
172 }
173
174 inline static void code_unflag_leafmethod(codeinfo *code)
175 {
176         code->flags &= ~CODE_FLAG_LEAFMETHOD;
177 }
178
179
180 /* code_xxx_synchronized *******************************************************
181
182    Functions for CODE_FLAG_SYNCHRONIZED.
183
184 *******************************************************************************/
185
186 inline static int code_is_synchronized(codeinfo *code)
187 {
188         return (code->flags & CODE_FLAG_SYNCHRONIZED);
189 }
190
191 inline static void code_flag_synchronized(codeinfo *code)
192 {
193         code->flags |= CODE_FLAG_SYNCHRONIZED;
194 }
195
196 inline static void code_unflag_synchronized(codeinfo *code)
197 {
198         code->flags &= ~CODE_FLAG_SYNCHRONIZED;
199 }
200
201
202 /* code_get_codeinfo_for_pv ****************************************************
203
204    Return the codeinfo for the given PV.
205
206    IN:
207        pv...............PV
208
209    RETURN VALUE:
210        the codeinfo *
211
212 *******************************************************************************/
213
214 inline static codeinfo *code_get_codeinfo_for_pv(void *pv)
215 {
216         codeinfo *code;
217
218         assert(pv != NULL);
219
220         code = *((codeinfo **) (((uintptr_t) pv) + CodeinfoPointer));
221
222         return code;
223 }
224
225
226 /* function prototypes ********************************************************/
227
228 void code_init(void);
229
230 codeinfo *code_codeinfo_new(methodinfo *m);
231 void code_codeinfo_free(codeinfo *code);
232
233 codeinfo *code_find_codeinfo_for_pc(void *pc);
234 codeinfo *code_find_codeinfo_for_pc_nocheck(void *pc);
235
236 methodinfo *code_get_methodinfo_for_pv(void *pv);
237
238 #if defined(ENABLE_REPLACEMENT)
239 int code_get_sync_slot_count(codeinfo *code);
240 #endif /* defined(ENABLE_REPLACEMENT) */
241
242 void code_free_code_of_method(methodinfo *m);
243
244 #ifdef __cplusplus
245 }
246 #endif
247
248 #endif // _CODE_HPP
249
250
251 /*
252  * These are local overrides for various environment variables in Emacs.
253  * Please do not remove this and leave it at the end of the file, where
254  * Emacs will automagically detect them.
255  * ---------------------------------------------------------------------
256  * Local variables:
257  * mode: c++
258  * indent-tabs-mode: t
259  * c-basic-offset: 4
260  * tab-width: 4
261  * End:
262  * vim:noexpandtab:sw=4:ts=4:
263  */