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