b3b20faa9d153c63fd3a66b8ae58c29c574dbe14
[cacao.git] / src / vm / resolve.h
1 /* vm/resolve.h - resolving classes/interfaces/fields/methods
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: Edwin Steiner
28
29    Changes:
30
31    $Id: resolve.h 2112 2005-03-29 21:29:08Z twisti $
32
33 */
34
35
36 #ifndef _RESOLVE_H
37 #define _RESOLVE_H
38
39 /* forward declarations *******************************************************/
40
41 typedef struct unresolved_field unresolved_field;
42 typedef struct unresolved_method unresolved_method;
43 typedef struct unresolved_subtype_set unresolved_subtype_set;
44
45
46 #include "vm/global.h"
47 #include "vm/loader.h"
48 #include "vm/jit/jit.h"
49
50
51 /* constants ******************************************************************/
52
53 #define RESOLVE_STATIC    0x0001
54 #define RESOLVE_PUTFIELD  0x0002
55
56
57 /* enums **********************************************************************/
58
59 typedef enum {
60         resolveLazy,
61         resolveEager
62 } resolve_mode_t;
63
64
65 typedef enum {
66         resolveLinkageError,
67         resolveIllegalAccessError
68 } resolve_err_t;
69
70
71 /* classref_or_classinfo ******************************************************/
72
73 typedef union {
74         constant_classref *ref;       /* a symbolic class reference               */
75         classinfo         *cls;       /* an already loaded class                  */
76         void              *any;       /* used for general access (x != NULL,...)  */
77 } classref_or_classinfo;
78
79
80 /* structs ********************************************************************/
81
82 struct unresolved_subtype_set {
83         classref_or_classinfo *subtyperefs;     /* NULL terminated list */
84 };
85
86
87 /* XXX unify heads of unresolved_field and unresolved_method? */
88
89 struct unresolved_field {
90         constant_FMIref *fieldref;
91         methodinfo      *referermethod;
92         s4               flags;
93         
94         unresolved_subtype_set  instancetypes;
95         unresolved_subtype_set  valueconstraints;
96 };
97
98 struct unresolved_method {
99         constant_FMIref *methodref;
100         methodinfo      *referermethod;
101         s4               flags;
102         
103         unresolved_subtype_set  instancetypes;
104         unresolved_subtype_set *paramconstraints;
105 };
106
107 #define SUBTYPESET_IS_EMPTY(stset) \
108         ((stset).subtyperefs == NULL)
109
110 #define UNRESOLVED_SUBTYPE_SET_EMTPY(stset) \
111         do { (stset).subtyperefs = NULL; } while(0)
112
113 /* a value that never occurrs in classinfo.header.vftbl                       */
114 #define CLASSREF_PSEUDO_VFTBL ((vftbl_t *) 1)
115
116 /* macro for testing if a classref_or_classinfo is a classref                 */
117 /* `reforinfo` is only evaluated once                                         */
118 #define IS_CLASSREF(reforinfo)  \
119         ((reforinfo).ref->pseudo_vftbl == CLASSREF_PSEUDO_VFTBL)
120
121 /* macro for casting a classref/classinfo * to a classref_or_classinfo        */
122 #define CLASSREF_OR_CLASSINFO(value) \
123         (*((classref_or_classinfo *)(&(value))))
124
125
126 /* function prototypes ********************************************************/
127
128 /* resolve_class ***************************************************************
129  
130    Resolve a symbolic class reference
131   
132    IN:
133        referer..........the class containing the reference
134        refmethod........the method from which resolution was triggered
135                         (may be NULL if not applicable)
136        classname........class name to resolve
137        mode.............mode of resolution:
138                             resolveLazy...only resolve if it does not
139                                           require loading classes
140                             resolveEager..load classes if necessary
141   
142    OUT:
143        *result..........set to result of resolution, or to NULL if
144                         the reference has not been resolved
145                         In the case of an exception, *result is
146                         guaranteed to be set to NULL.
147   
148    RETURN VALUE:
149        true.............everything ok 
150                         (*result may still be NULL for resolveLazy)
151        false............an exception has been thrown
152
153    NOTE:
154        The returned class is *not* guaranteed to be linked!
155            (It is guaranteed to be loaded, though.)
156    
157 *******************************************************************************/
158
159 bool
160 resolve_class(classinfo *referer,methodinfo *refmethod,
161                           utf *classname,
162                           resolve_mode_t mode,
163                           classinfo **result);
164
165 /* resolve_classref_or_classinfo ***********************************************
166  
167    Resolve a symbolic class reference if necessary
168   
169    IN:
170        refmethod........the method from which resolution was triggered
171                         (may be NULL if not applicable)
172        cls..............class reference or classinfo
173        mode.............mode of resolution:
174                             resolveLazy...only resolve if it does not
175                                           require loading classes
176                             resolveEager..load classes if necessary
177            link.............if true, guarantee that the returned class, if any,
178                             has been linked
179   
180    OUT:
181        *result..........set to result of resolution, or to NULL if
182                         the reference has not been resolved
183                         In the case of an exception, *result is
184                         guaranteed to be set to NULL.
185   
186    RETURN VALUE:
187        true.............everything ok 
188                         (*result may still be NULL for resolveLazy)
189        false............an exception has been thrown
190    
191 *******************************************************************************/
192
193 bool
194 resolve_classref_or_classinfo(methodinfo *refmethod,
195                                                           classref_or_classinfo cls,
196                                                           resolve_mode_t mode,
197                                                           bool link,
198                                                           classinfo **result);
199
200 /* resolve_field ***************************************************************
201  
202    Resolve an unresolved field reference
203   
204    IN:
205        ref..............struct containing the reference
206        mode.............mode of resolution:
207                             resolveLazy...only resolve if it does not
208                                           require loading classes
209                             resolveEager..load classes if necessary
210   
211    OUT:
212        *result..........set to the result of resolution, or to NULL if
213                         the reference has not been resolved
214                         In the case of an exception, *result is
215                         guaranteed to be set to NULL.
216   
217    RETURN VALUE:
218        true.............everything ok 
219                         (*result may still be NULL for resolveLazy) XXX implement
220        false............an exception has been thrown
221    
222 *******************************************************************************/
223
224 bool
225 resolve_field(unresolved_field *ref,
226                           resolve_mode_t mode,
227                           fieldinfo **result);
228
229 /* resolve_method **************************************************************
230  
231    Resolve an unresolved method reference
232   
233    IN:
234        ref..............struct containing the reference
235        mode.............mode of resolution:
236                             resolveLazy...only resolve if it does not
237                                           require loading classes
238                             resolveEager..load classes if necessary
239   
240    OUT:
241        *result..........set to the result of resolution, or to NULL if
242                         the reference has not been resolved
243                         In the case of an exception, *result is
244                         guaranteed to be set to NULL.
245   
246    RETURN VALUE:
247        true.............everything ok 
248                         (*result may still be NULL for resolveLazy) XXX implement
249        false............an exception has been thrown
250    
251 *******************************************************************************/
252
253 bool
254 resolve_method(unresolved_method *ref,
255                           resolve_mode_t mode,
256                            methodinfo **result);
257
258 /* resolve_and_check_subtype_set ***********************************************
259  
260    Resolve the references in the given set and test subtype relationships
261   
262    IN:
263        referer..........the class containing the references
264        refmethod........the method triggering the resolution
265        ref..............a set of class/interface references
266                         (may be empty)
267        type.............the type to test against the set
268        reversed.........if true, test if type is a subtype of
269                         the set members, instead of the other
270                         way round
271        mode.............mode of resolution:
272                             resolveLazy...only resolve if it does not
273                                           require loading classes
274                             resolveEager..load classes if necessary
275        error............which type of exception to throw if
276                         the test fails. May be:
277                             resolveLinkageError, or
278                             resolveIllegalAccessError
279
280    OUT:
281        *checked.........set to true if all checks were performed,
282                             otherwise set to false
283                             (This is guaranteed to be true if mode was
284                                                 resolveEager and no exception occured.)
285                                                 If checked == NULL, this parameter is not used.
286   
287    RETURN VALUE:
288        true.............the check succeeded
289        false............the check failed. An exception has been
290                         thrown.
291    
292    NOTE:
293        The references in the set are resolved first, so any
294        exception which may occurr during resolution may
295        be thrown by this function.
296    
297 *******************************************************************************/
298
299 bool
300 resolve_and_check_subtype_set(classinfo *referer,methodinfo *refmethod,
301                                                           unresolved_subtype_set *ref,
302                                                           classref_or_classinfo type,
303                                                           bool reversed,
304                                                           resolve_mode_t mode,
305                                                           resolve_err_t error,
306                                                           bool *checked);
307
308 /* create_unresolved_field *****************************************************
309  
310    Create an unresolved_field struct for the given field access instruction
311   
312    IN:
313        referer..........the class containing the reference
314            refmethod........the method triggering the resolution (if any)
315            iptr.............the {GET,PUT}{FIELD,STATIC}{,CONST} instruction
316            stack............the input stack of the instruction
317
318    RETURN VALUE:
319        a pointer to a new unresolved_field struct
320
321 *******************************************************************************/
322
323 unresolved_field *
324 create_unresolved_field(classinfo *referer,methodinfo *refmethod,
325                                                 instruction *iptr,
326                                                 stackelement *stack);
327
328 /* create_unresolved_method ****************************************************
329  
330    Create an unresolved_method struct for the given method invocation
331   
332    IN:
333        referer..........the class containing the reference
334            refmethod........the method triggering the resolution (if any)
335            iptr.............the INVOKE* instruction
336            stack............the input stack of the instruction
337
338    RETURN VALUE:
339        a pointer to a new unresolved_method struct
340
341 *******************************************************************************/
342
343 unresolved_method *
344 create_unresolved_method(classinfo *referer,methodinfo *refmethod,
345                                                  instruction *iptr,
346                                                  stackelement *stack);
347
348 /* unresolved_field_free *******************************************************
349  
350    Free the memory used by an unresolved_field
351   
352    IN:
353        ref..............the unresolved_field
354
355 *******************************************************************************/
356
357 void unresolved_field_free(unresolved_field *ref);
358
359 /* unresolved_method_free ******************************************************
360  
361    Free the memory used by an unresolved_method
362   
363    IN:
364        ref..............the unresolved_method
365
366 *******************************************************************************/
367
368 void unresolved_method_free(unresolved_method *ref);
369
370 /* unresolved_field_debug_dump *************************************************
371  
372    Print debug info for unresolved_field to stream
373   
374    IN:
375        ref..............the unresolved_field
376            file.............the stream
377
378 *******************************************************************************/
379
380 void unresolved_field_debug_dump(unresolved_field *ref,FILE *file);
381
382 /* unresolved_method_debug_dump ************************************************
383  
384    Print debug info for unresolved_method to stream
385   
386    IN:
387        ref..............the unresolved_method
388            file.............the stream
389
390 *******************************************************************************/
391
392 void unresolved_method_debug_dump(unresolved_method *ref,FILE *file);
393
394 /* unresolved_subtype_set_debug_dump *******************************************
395  
396    Print debug info for unresolved_subtype_set to stream
397   
398    IN:
399        stset............the unresolved_subtype_set
400            file.............the stream
401
402 *******************************************************************************/
403
404 void unresolved_subtype_set_debug_dump(unresolved_subtype_set *stset,FILE *file);
405         
406 #endif /* _RESOLVE_H */
407
408 /*
409  * These are local overrides for various environment variables in Emacs.
410  * Please do not remove this and leave it at the end of the file, where
411  * Emacs will automagically detect them.
412  * ---------------------------------------------------------------------
413  * Local variables:
414  * mode: c
415  * indent-tabs-mode: t
416  * c-basic-offset: 4
417  * tab-width: 4
418  * End:
419  * vim:noexpandtab:sw=4:ts=4:
420  */
421