* src/vm/jit/patcher-common.c: Moved to .cpp.
[cacao.git] / src / vm / linker.h
1 /* src/vm/linker.h - class linker header
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 _LINKER_H
27 #define _LINKER_H
28
29 /* forward typedefs ***********************************************************/
30
31 typedef struct _vftbl vftbl_t;
32 typedef struct arraydescriptor arraydescriptor;
33 typedef struct primitivetypeinfo primitivetypeinfo;
34
35
36 #include "config.h"
37 #include "vm/types.h"
38
39 #include "threads/mutex.hpp"
40
41 #include "vm/class.h"
42 #include "vm/references.h"
43
44
45 /* virtual function table ******************************************************
46
47    The vtbl has a bidirectional layout with open ends at both sides.
48    interfacetablelength gives the number of entries of the interface
49    table at the start of the vftbl. The vftbl pointer points to
50    &interfacetable[0].  vftbllength gives the number of entries of
51    table at the end of the vftbl.
52
53    runtime type check (checkcast):
54
55    Different methods are used for runtime type check depending on the
56    argument of checkcast/instanceof.
57         
58    A check against a class is implemented via relative numbering on
59    the class hierachy tree. The tree is numbered in a depth first
60    traversal setting the base field and the diff field. The diff field
61    gets the result of (high - base) so that a range check can be
62    implemented by an unsigned compare. A sub type test is done by
63    checking the inclusion of base of the sub class in the range of the
64    superclass.
65
66    A check against an interface is implemented via the
67    interfacevftbl. If the interfacevftbl contains a nonnull value a
68    class is a subclass of this interface.
69
70    interfacetable:
71
72    Like standard virtual methods interface methods are called using
73    virtual function tables. All interfaces are numbered sequentially
74    (starting with zero). For each class there exist an interface table
75    of virtual function tables for each implemented interface. The
76    length of the interface table is determined by the highest number
77    of an implemented interface.
78
79    The following example assumes a class which implements interface 0 and 3:
80
81    interfacetablelength = 4
82
83                   | ...       |            +----------+
84                   +-----------+            | method 2 |---> method z
85                   | class     |            | method 1 |---> method y
86                   +-----------+            | method 0 |---> method x
87                   | ivftbl  0 |----------> +----------+
88     vftblptr ---> +-----------+
89                   | ivftbl -1 |--> NULL    +----------+
90                   | ivftbl -2 |--> NULL    | method 1 |---> method x
91                   | ivftbl -3 |-----+      | method 0 |---> method a
92                   +-----------+     +----> +----------+
93      
94                               +---------------+
95                               | length 3 = 2  |
96                               | length 2 = 0  |
97                               | length 1 = 0  |
98                               | length 0 = 3  |
99     interfacevftbllength ---> +---------------+
100
101 *******************************************************************************/
102
103 struct _vftbl {
104         methodptr   *interfacetable[1];    /* interface table (access via macro)  */
105         classinfo   *clazz;                /* class, the vtbl belongs to          */
106         arraydescriptor *arraydesc;        /* for array classes, otherwise NULL   */
107         s4           vftbllength;          /* virtual function table length       */
108         s4           interfacetablelength; /* interface table length              */
109         s4           baseval;              /* base for runtime type check         */
110                                            /* (-index for interfaces)             */
111         s4           diffval;              /* high - base for runtime type check  */
112         s4          *interfacevftbllength; /* length of interface vftbls          */
113         methodptr    table[1];             /* class vftbl                         */
114 };
115
116
117 /* arraydescriptor *************************************************************
118
119    For every array class an arraydescriptor is allocated which
120    describes the array class. The arraydescriptor is referenced from
121    the vftbl of the array class.
122
123 *******************************************************************************/
124
125 struct arraydescriptor {
126         vftbl_t *componentvftbl; /* vftbl of the component type, NULL for primit. */
127         vftbl_t *elementvftbl;   /* vftbl of the element type, NULL for primitive */
128         s2       arraytype;      /* ARRAYTYPE_* constant                          */
129         s2       dimension;      /* dimension of the array (always >= 1)          */
130         s4       dataoffset;     /* offset of the array data from object pointer  */
131         s4       componentsize;  /* size of a component in bytes                  */
132         s2       elementtype;    /* ARRAYTYPE_* constant                          */
133 };
134
135
136 /* global variables ***********************************************************/
137
138 /* This lock must be taken while renumbering classes or while atomically      */
139 /* accessing classes.                                                         */
140
141 extern Mutex *linker_classrenumber_mutex;
142
143
144 /* function prototypes ********************************************************/
145
146 #ifdef __cplusplus
147 extern "C" {
148 #endif
149
150 void       linker_preinit(void);
151 void       linker_init(void);
152 classinfo *link_class(classinfo *c);
153
154 #ifdef __cplusplus
155 }
156 #endif
157
158 #endif /* _LINKER_H */
159
160
161 /*
162  * These are local overrides for various environment variables in Emacs.
163  * Please do not remove this and leave it at the end of the file, where
164  * Emacs will automagically detect them.
165  * ---------------------------------------------------------------------
166  * Local variables:
167  * mode: c
168  * indent-tabs-mode: t
169  * c-basic-offset: 4
170  * tab-width: 4
171  * End:
172  */