* Changed functionptr to u1*.
[cacao.git] / src / vm / jit / codegen.inc.h
1 /* src/vm/jit/codegen.inc.h - code generation header
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Christian Thalinger
28
29    Changes: Christian Ullrich
30
31    $Id: codegen.inc.h 3745 2005-11-22 23:43:53Z twisti $
32
33 */
34
35
36 #ifndef _CODEGEN_INC_H
37 #define _CODEGEN_INC_H
38
39 /* We typedef these structures before #includes to resolve circular           */
40 /* dependencies.                                                              */
41
42 typedef struct codegendata codegendata;
43 typedef struct branchref branchref;
44 typedef struct jumpref jumpref;
45 typedef struct dataref dataref;
46 typedef struct patchref patchref;
47 typedef struct linenumberref linenumberref;
48 typedef struct threadcritnodetemp threadcritnodetemp;
49
50
51 #include "config.h"
52 #include "vm/types.h"
53
54 #include "vm/global.h"
55 #include "vm/references.h"
56 #include "vm/method.h"
57 #include "vm/jit/jit.h"
58 #include "vm/jit/reg.h"
59 #include "vm/jit/inline/inline.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
66 /* Register Pack/Unpack Macros ************************************************/
67
68 #define GET_LOW_REG(a)  (((a) & 0xffff0000) >> 16)
69 #define GET_HIGH_REG(a) ((a) &  0x0000ffff)
70
71 #define PACK_REGS(low,high) \
72         ( ((high) & 0x0000ffff) | (((low) & 0x0000ffff) << 16) )
73
74
75 #if SIZEOF_VOID_P == 8
76 #define dseg_addaddress(cd,value)    dseg_adds8((cd), (s8) (value))
77 #else
78 #define dseg_addaddress(cd,value)    dseg_adds4((cd), (s4) (value))
79 #endif
80
81
82 /************************* critical sections  *********************************/
83
84 struct threadcritnodetemp {
85         threadcritnodetemp *next;
86         s4                  mcodebegin;
87         s4                  mcodeend;
88         s4                  mcoderestart;
89 };
90
91
92 struct codegendata {
93         u1             *mcodebase;      /* base pointer of code area              */
94         s4             *mcodeend;       /* pointer to end of code area            */
95         s4              mcodesize;      /* complete size of code area (bytes)     */
96
97 #if defined(__I386__) || defined(__X86_64__) || defined(ENABLE_INTRP)
98         u1             *mcodeptr;       /* code generation pointer                */
99 #endif
100
101 #if defined(__I386__) || defined(__MIPS__) || defined(__X86_64__)
102         u1             *lastmcodeptr;   /* last patcher position of basic block   */
103 #endif
104
105 #if defined(ENABLE_INTRP)
106         u1             *last_compiled;
107 #endif
108
109         u1             *dsegtop;        /* pointer to top (end) of data area      */
110         s4              dsegsize;       /* complete size of data area (bytes)     */
111         s4              dseglen;        /* used size of data area (bytes)         */
112                                     /* data area grows from top to bottom     */
113
114         jumpref        *jumpreferences; /* list of jumptable target addresses     */
115         dataref        *datareferences; /* list of data segment references        */
116         branchref      *xboundrefs;     /* list of bound check branches           */
117         branchref      *xnullrefs;      /* list of null check branches            */
118         branchref      *xcastrefs;      /* list of cast check branches            */
119         branchref      *xstorerefs;     /* list of array store check branches     */
120         branchref      *xdivrefs;       /* list of divide by zero branches        */
121         branchref      *xexceptionrefs; /* list of exception branches             */
122         patchref       *patchrefs;
123
124         linenumberref  *linenumberreferences; /* list of line numbers and the     */
125                                         /* program counters of their first        */
126                                         /* instruction                            */
127         s4              linenumbertablesizepos;
128         s4              linenumbertablestartpos;
129         s4              linenumbertab;
130
131         methodinfo     *method;
132         s4              exceptiontablelength; /* exceptiontable length            */
133         exceptiontable *exceptiontable; /* the exceptiontable                     */
134
135         threadcritnodetemp *threadcrit; /* List of critical code regions          */
136         threadcritnodetemp threadcritcurrent;
137         s4                 threadcritcount; /* Number of critical regions         */
138
139         s4              maxstack;
140         s4              maxlocals;
141 };
142
143
144 /***************** forward references in branch instructions ******************/
145
146 struct branchref {
147         s4         branchpos;       /* patching position in code segment          */
148         s4         reg;             /* used for ArrayIndexOutOfBounds index reg   */
149         branchref *next;            /* next element in branchref list             */
150 };
151
152
153 /******************** forward references in tables  ***************************/
154
155 struct jumpref {
156         s4          tablepos;       /* patching position in data segment          */
157         basicblock *target;         /* target basic block                         */
158         jumpref    *next;           /* next element in jumpref list               */
159 };
160
161
162 struct dataref {
163         u1      *pos;               /* patching position in generated code        */
164         dataref *next;              /* next element in dataref list               */
165 };
166
167
168 struct patchref {
169         s4           branchpos;
170         functionptr  patcher;
171         voidptr      ref;
172         patchref    *next;
173         s4           disp;
174 };
175
176
177 struct linenumberref {
178         s4             tablepos;    /* patching position in data segment          */
179         s4             targetmpc;   /* machine code program counter of first      */
180                                     /* instruction for given line                 */
181         u2             linenumber;  /* line number, used for inserting into the   */
182                                     /* table and for validty checking             */
183         linenumberref *next;        /* next element in linenumberref list         */
184 };
185
186
187 #if defined(__I386__) || defined(__X86_64__) || defined(ENABLE_INTRP) || defined(DISABLE_GC)
188 typedef struct _methodtree_element methodtree_element;
189
190 struct _methodtree_element {
191         u1 *startpc;
192         u1 *endpc;
193 };
194 #endif
195
196
197 /* function prototypes ********************************************************/
198
199 void codegen_init(void);
200 void codegen_setup(methodinfo *m, codegendata *cd, t_inlining_globals *e);
201 bool codegen(methodinfo *m, codegendata *cd, registerdata *rd);
202 void codegen_free(methodinfo *m, codegendata *cd);
203 void codegen_close(void);
204 void codegen_insertmethod(u1 *startpc, u1 *endpc);
205
206 u1 *codegen_findmethod(u1 *pc);
207
208 #if defined(__I386__) || defined(__X86_64__)
209 void codegen_addreference(codegendata *cd, struct basicblock *target, void *branchptr);
210 #endif
211
212 void dseg_display(methodinfo *m, codegendata *cd);
213
214 u1 *codegen_createnativestub(functionptr f, methodinfo *m);
215 void codegen_disassemble_nativestub(methodinfo *m, u1 *start, u1 *end);
216
217 void codegen_start_native_call(u1 *datasp, u1 *pv, u1 *sp, u1 *ra);
218 void codegen_finish_native_call(u1 *datasp);
219
220 u1 *createcompilerstub(methodinfo *m);
221 u1 *createnativestub(functionptr f, methodinfo *m, codegendata *cd,
222                                          registerdata *rd, methoddesc *md);
223
224 void removecompilerstub(u1 *stub);
225 void removenativestub(u1 *stub);
226
227 /* machine dependent find method function */
228 u1 *md_codegen_findmethod(u1 *ra);
229
230 #endif /* _CODEGEN_INC_H */
231
232
233 /*
234  * These are local overrides for various environment variables in Emacs.
235  * Please do not remove this and leave it at the end of the file, where
236  * Emacs will automagically detect them.
237  * ---------------------------------------------------------------------
238  * Local variables:
239  * mode: c
240  * indent-tabs-mode: t
241  * c-basic-offset: 4
242  * tab-width: 4
243  * End:
244  */