* Changed types.h include to vm/types.h.
[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:
30
31    $Id: codegen.inc.h 3217 2005-09-19 13:11:24Z 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(__INTRP__)
98         u1             *mcodeptr;       /* code generation pointer                */
99 #endif
100
101 #if defined(ENABLE_INTRP)
102         u1             *last_compiled;
103 #endif
104
105         u1             *dsegtop;        /* pointer to top (end) of data area      */
106         s4              dsegsize;       /* complete size of data area (bytes)     */
107         s4              dseglen;        /* used size of data area (bytes)         */
108                                     /* data area grows from top to bottom     */
109
110         jumpref        *jumpreferences; /* list of jumptable target addresses     */
111         dataref        *datareferences; /* list of data segment references        */
112         branchref      *xboundrefs;     /* list of bound check branches           */
113         branchref      *xcheckarefs;    /* list of array size check branches      */
114         branchref      *xnullrefs;      /* list of null check branches            */
115         branchref      *xcastrefs;      /* list of cast check branches            */
116         branchref      *xstorerefs;     /* list of array store check branches     */
117         branchref      *xdivrefs;       /* list of divide by zero branches        */
118         branchref      *xexceptionrefs; /* list of exception branches             */
119         patchref       *patchrefs;
120
121         linenumberref  *linenumberreferences; /* list of line numbers and the     */
122                                         /* program counters of their first        */
123                                         /* instruction                            */
124         s4              linenumbertablesizepos;
125         s4              linenumbertablestartpos;
126         s4              linenumbertab;
127
128         methodinfo     *method;
129         s4              exceptiontablelength; /* exceptiontable length            */
130         exceptiontable *exceptiontable; /* the exceptiontable                     */
131
132         threadcritnodetemp *threadcrit; /* List of critical code regions          */
133         threadcritnodetemp threadcritcurrent;
134         s4                 threadcritcount; /* Number of critical regions         */
135
136         s4              maxstack;
137         s4              maxlocals;
138 };
139
140
141 /***************** forward references in branch instructions ******************/
142
143 struct branchref {
144         s4         branchpos;       /* patching position in code segment          */
145         s4         reg;             /* used for ArrayIndexOutOfBounds index reg   */
146         branchref *next;            /* next element in branchref list             */
147 };
148
149
150 /******************** forward references in tables  ***************************/
151
152 struct jumpref {
153         s4          tablepos;       /* patching position in data segment          */
154         basicblock *target;         /* target basic block                         */
155         jumpref    *next;           /* next element in jumpref list               */
156 };
157
158
159 struct dataref {
160         u1      *pos;               /* patching position in generated code        */
161         dataref *next;              /* next element in dataref list               */
162 };
163
164
165 struct patchref {
166         s4           branchpos;
167         functionptr  patcher;
168         voidptr      ref;
169         patchref    *next;
170         s4           disp;
171 };
172
173
174 struct linenumberref {
175         s4             tablepos;    /* patching position in data segment          */
176         s4             targetmpc;   /* machine code program counter of first      */
177                                     /* instruction for given line                 */
178         u2             linenumber;  /* line number, used for inserting into the   */
179                                     /* table and for validty checking             */
180         linenumberref *next;        /* next element in linenumberref list         */
181 };
182
183
184 #if defined(__I386__) || defined(__X86_64__) || defined(__INTRP__)
185 typedef struct _methodtree_element methodtree_element;
186
187 struct _methodtree_element {
188         functionptr startpc;
189         functionptr endpc;
190 };
191 #endif
192
193
194 /* function prototypes ********************************************************/
195
196 void codegen_init(void);
197 void codegen_setup(methodinfo *m, codegendata *cd, t_inlining_globals *e);
198 void codegen(methodinfo *m, codegendata *cd, registerdata *rd);
199 void codegen_free(methodinfo *m, codegendata *cd);
200 void codegen_close(void);
201 void codegen_insertmethod(functionptr startpc, functionptr endpc);
202
203 functionptr codegen_findmethod(functionptr pc);
204
205 #if defined(__I386__) || defined(__X86_64__)
206 void codegen_addreference(codegendata *cd, struct basicblock *target, void *branchptr);
207 #endif
208
209 void dseg_display(methodinfo *m, codegendata *cd);
210
211 functionptr codegen_createnativestub(functionptr f, methodinfo *m);
212 void codegen_disassemble_nativestub(methodinfo *m, u1 *start, u1 *end);
213
214 functionptr createcompilerstub(methodinfo *m);
215 functionptr createnativestub(functionptr f, methodinfo *m, codegendata *cd,
216                                                          registerdata *rd, methoddesc *md);
217
218 void removecompilerstub(functionptr stub);
219 void removenativestub(functionptr stub);
220
221 #endif /* _CODEGEN_INC_H */
222
223
224 /*
225  * These are local overrides for various environment variables in Emacs.
226  * Please do not remove this and leave it at the end of the file, where
227  * Emacs will automagically detect them.
228  * ---------------------------------------------------------------------
229  * Local variables:
230  * mode: c
231  * indent-tabs-mode: t
232  * c-basic-offset: 4
233  * tab-width: 4
234  * End:
235  */