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