Line data Source code
1 : /* src/vm/references.hpp - references to classes/fields/methods
2 :
3 : Copyright (C) 1996-2013
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 : #ifndef REFERENCES_HPP_
26 : #define REFERENCES_HPP_ 1
27 :
28 : #include "config.h"
29 :
30 : #include "vm/global.hpp"
31 : #include "vm/types.hpp"
32 : #include "vm/utf8.hpp"
33 :
34 : struct classinfo;
35 : struct typedesc;
36 : struct fieldinfo;
37 : struct methoddesc;
38 : struct methodinfo;
39 :
40 : /* constant_classref **********************************************************/
41 :
42 : /// a value that never occurrs in classinfo.header.vftbl
43 : #define CLASSREF_PSEUDO_VFTBL (reinterpret_cast<void *>(1))
44 :
45 : struct constant_classref {
46 : void* const pseudo_vftbl; /* for distinguishing it from classinfo */
47 : classinfo* const referer; /* class containing the reference */
48 : const Utf8String name; /* name of the class refered to */
49 :
50 434805 : constant_classref(classinfo *referer, Utf8String name)
51 434805 : : pseudo_vftbl(CLASSREF_PSEUDO_VFTBL), referer(referer), name(name) {}
52 : };
53 :
54 :
55 : /* classref_or_classinfo ******************************************************/
56 :
57 : /**
58 : * @Cpp11 Replace static constructors with regular constructors.
59 : * With C++98 this breaks other unions that use classref_or_classinfo.
60 : */
61 : union classref_or_classinfo {
62 : constant_classref *ref; /* a symbolic class reference */
63 : classinfo *cls; /* an already loaded class */
64 : void *any; /* used for general access (x != NULL,...) */
65 :
66 2936988 : bool is_classref() const { return ref->pseudo_vftbl == CLASSREF_PSEUDO_VFTBL; }
67 168720 : bool is_classinfo() const { return !is_classref(); }
68 : };
69 :
70 :
71 : /* parseddesc_t ***************************************************************/
72 :
73 : union parseddesc_t {
74 : typedesc *fd; /* parsed field descriptor */
75 : methoddesc *md; /* parsed method descriptor */
76 :
77 : // test against NULL;
78 668728 : operator bool() const { return fd; }
79 : };
80 :
81 : /* structs ********************************************************************/
82 :
83 : /**
84 : * Fieldref, Methodref and InterfaceMethodref
85 : */
86 : struct constant_FMIref {
87 668728 : constant_FMIref(constant_classref *ref,
88 : Utf8String name,
89 : Utf8String descriptor,
90 : parseddesc_t parseddesc)
91 668728 : : name(name), descriptor(descriptor), parseddesc(parseddesc) {
92 668728 : p.classref = ref;
93 668728 : }
94 :
95 : union {
96 : // set when FMIref is unresolved
97 : constant_classref *classref; /* class having this field/meth./intfm. */
98 :
99 : // set when FMIref is resolved
100 : fieldinfo *field; /* resolved field */
101 : methodinfo *method; /* resolved method */
102 : } p;
103 : const Utf8String name; /* field/method/interfacemethod name */
104 : const Utf8String descriptor; /* field/method/intfmeth. type descriptor string */
105 : const parseddesc_t parseddesc; /* parsed descriptor */
106 :
107 1322903 : bool is_resolved() const { return p.classref->pseudo_vftbl != CLASSREF_PSEUDO_VFTBL; }
108 : };
109 :
110 : /// The kinds of methodhandles that can appear in a class file
111 : enum MethodHandleKind {
112 : REF_getField = 1,
113 : REF_getStatic = 2,
114 : REF_putField = 3,
115 : REF_putStatic = 4,
116 : REF_invokeVirtual = 5,
117 : REF_invokeStatic = 6,
118 : REF_invokeSpecial = 7,
119 : REF_newInvokeSpecial = 8,
120 : REF_invokeInterface = 9
121 : };
122 :
123 : /***
124 : * A MethodHandle constant stored in the constant pool
125 : */
126 : struct constant_MethodHandle {
127 0 : constant_MethodHandle(MethodHandleKind kind, constant_FMIref *fmi)
128 0 : : kind(kind), fmi(fmi), handle(NULL) {}
129 :
130 : const MethodHandleKind kind;
131 : constant_FMIref *const fmi;
132 :
133 : /// resolved java.lang.invoke.MethodHandle object
134 : java_object_t *handle;
135 : };
136 :
137 : /**
138 : * A MethodType constant stored in the constant pool
139 : */
140 : struct constant_MethodType {
141 0 : constant_MethodType(Utf8String desc, methoddesc *parseddesc)
142 0 : : descriptor(desc), parseddesc(parseddesc), type(NULL) {}
143 :
144 : const Utf8String descriptor;
145 : const methoddesc *const parseddesc;
146 :
147 : /// resolved java.lang.invoke.MethodType object
148 : java_object_t *type;
149 : };
150 :
151 : /**
152 : * An invokedynamic call site.
153 : */
154 : struct constant_InvokeDynamic {
155 0 : constant_InvokeDynamic(uint16_t bsm, Utf8String name, Utf8String desc, methoddesc *parseddesc)
156 0 : : bootstrap_method_index(bsm), name(name), descriptor(desc), parseddesc(parseddesc), handle(NULL) {}
157 :
158 : const uint16_t bootstrap_method_index;
159 : const Utf8String name;
160 : const Utf8String descriptor;
161 : const methoddesc *const parseddesc;
162 :
163 : /// resolved java.lang.invoke.MethodHandle object for this CallSite
164 : java_object_t *handle;
165 : };
166 :
167 :
168 : /* inline functions ***********************************************************/
169 :
170 : /**
171 : * Functions for casting a classref/classinfo to a classref_or_classinfo
172 : * @Cpp11 Replace with constructors in classref_or_classinfo.
173 : * With C++98 this breaks other unions that use classref_or_classinfo.
174 : * @{
175 : */
176 388889 : static inline classref_or_classinfo to_classref_or_classinfo(classinfo *c) {
177 : classref_or_classinfo out;
178 388889 : out.cls = c;
179 388889 : return out;
180 : }
181 703700 : static inline classref_or_classinfo to_classref_or_classinfo(constant_classref *c) {
182 : classref_or_classinfo out;
183 703700 : out.ref = c;
184 703700 : return out;
185 : }
186 : /**
187 : * @}
188 : */
189 :
190 :
191 : /* macros *********************************************************************/
192 :
193 : /* macro for accessing the name of a classref/classinfo */
194 : #define CLASSREF_OR_CLASSINFO_NAME(value) \
195 : ((value).is_classref() ? (value).ref->name : (value).cls->name)
196 :
197 : /* macro for accessing the class name of a method reference */
198 : #define METHODREF_CLASSNAME(fmiref) \
199 : ((fmiref)->is_resolved() ? (fmiref)->p.method->clazz->name \
200 : : (fmiref)->p.classref->name)
201 :
202 : /* macro for accessing the class name of a method reference */
203 : #define FIELDREF_CLASSNAME(fmiref) \
204 : ((fmiref)->is_resolved() ? (fmiref)->p.field->clazz->name \
205 : : (fmiref)->p.classref->name)
206 :
207 : #endif // REFERENCES_HPP_
208 :
209 : /*
210 : * These are local overrides for various environment variables in Emacs.
211 : * Please do not remove this and leave it at the end of the file, where
212 : * Emacs will automagically detect them.
213 : * ---------------------------------------------------------------------
214 : * Local variables:
215 : * mode: c++
216 : * indent-tabs-mode: t
217 : * c-basic-offset: 4
218 : * tab-width: 4
219 : * End:
220 : * vim:noexpandtab:sw=4:ts=4:
221 : */
222 :
|