Linker related stuff (mostly from loader.c/global.h).
[cacao.git] / src / vm / linker.h
1 /* src/vm/linker.h - class linker 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: linker.h 2101 2005-03-28 21:59:45Z twisti $
32 */
33
34
35 #ifndef _LINKER_H
36 #define _LINKER_H
37
38 /* forward typedefs ***********************************************************/
39
40 typedef struct _vftbl vftbl_t;
41
42
43 #include "vm/class.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 /* function prototypes ********************************************************/
119
120 void linker_init(void);
121 classinfo *class_link(classinfo *c);
122
123 #endif /* _LINKER_H */
124
125
126 /*
127  * These are local overrides for various environment variables in Emacs.
128  * Please do not remove this and leave it at the end of the file, where
129  * Emacs will automagically detect them.
130  * ---------------------------------------------------------------------
131  * Local variables:
132  * mode: c
133  * indent-tabs-mode: t
134  * c-basic-offset: 4
135  * tab-width: 4
136  * End:
137  */