* Updated header: Added 2006. Changed address of FSF. Changed email
[cacao.git] / src / vm / descriptor.h
1 /* vm/descriptor.h - checking and parsing of field / method descriptors
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: Edwin Steiner
28
29    Changes:
30
31    $Id: descriptor.h 4357 2006-01-22 23:33:38Z twisti $
32
33 */
34
35
36 #ifndef _DESCRIPTOR_H
37 #define _DESCRIPTOR_H
38
39 /* forward typedefs ***********************************************************/
40
41 typedef struct descriptor_pool descriptor_pool;
42 typedef struct paramdesc paramdesc;
43
44 #include "vm/class.h"
45 #include "vm/global.h"
46 #include "vm/hashtable.h"
47 #include "vm/method.h"
48 #include "vm/references.h"
49
50
51 /* data structures ************************************************************/ 
52
53 /*----------------------------------------------------------------------------*/
54 /* Descriptor Pools                                                           */
55 /*                                                                            */
56 /* A descriptor_pool is a temporary data structure used during loading of     */
57 /* a class. The descriptor_pool is used to allocate the table of              */
58 /* constant_classrefs the class uses, and for parsing the field and method    */
59 /* descriptors which occurr within the class. The inner workings of           */
60 /* descriptor_pool are not important for outside code.                        */
61 /*                                                                            */
62 /* You use a descriptor_pool as follows:                                      */
63 /*                                                                            */
64 /* 1. create one with descriptor_pool_new                                     */
65 /* 2. add all explicit class references with descriptor_pool_add_class        */
66 /* 3. add all field/method descriptors with descriptor_pool_add               */
67 /* 4. call descriptor_pool_create_classrefs                                   */
68 /*    You can now lookup classrefs with descriptor_pool_lookup_classref       */
69 /* 5. call descriptor_pool_alloc_parsed_descriptors                           */
70 /* 6. for each field descriptor call descriptor_pool_parse_field_descriptor   */
71 /*    for each method descriptor call descriptor_pool_parse_method_descriptor */
72 /* 7. call descriptor_pool_get_parsed_descriptors                             */
73 /*                                                                            */
74 /* IMPORTANT: The descriptor_pool functions use DNEW and DMNEW for allocating */
75 /*            memory which can be thrown away when the steps above have been  */
76 /*            done.                                                           */
77 /*----------------------------------------------------------------------------*/
78
79 struct descriptor_pool {
80         classinfo         *referer;
81         u4                 fieldcount;
82         u4                 methodcount;
83         u4                 paramcount;
84         u4                 descriptorsize;
85         u1                *descriptors;
86         u1                *descriptors_next;
87         hashtable          descriptorhash;
88         constant_classref *classrefs;
89         hashtable          classrefhash;
90         u1                *descriptor_kind;       /* useful for debugging */
91         u1                *descriptor_kind_next;  /* useful for debugging */
92 };
93
94
95 /* data structures for parsed field/method descriptors ************************/
96
97 struct typedesc {
98         constant_classref *classref;   /* class reference for TYPE_ADR types      */
99         u1                 type;       /* TYPE_??? constant [1]                   */
100         u1                 decltype;   /* (PRIMITIVE)TYPE_??? constant [2]        */
101         u1                 arraydim;   /* array dimension (0 if no array)         */
102 };
103
104 /* [1]...the type field contains the basic type used within the VM. So ints,  */
105 /*       shorts, chars, bytes, booleans all have TYPE_INT.                    */
106 /* [2]...the decltype field contains the declared type.                       */
107 /*       So short is PRIMITIVETYPE_SHORT, char is PRIMITIVETYPE_CHAR.         */
108 /*       For non-primitive types decltype is TYPE_ADR.                        */
109
110 struct paramdesc {
111         bool inmemory;              /* argument in register or on stack           */
112         s4   regoff;                /* register index or stack offset             */
113 };
114
115 struct methoddesc {
116         s2         paramcount;      /* number of parameters                       */
117         s2         paramslots;      /* like above but LONG,DOUBLE count twice     */
118         s4         argintreguse;    /* number of used integer argument registers  */
119         s4         argfltreguse;    /* number of used float argument registers    */
120         s4         memuse;          /* number of stack slots used                 */
121         paramdesc *params;          /* allocated parameter descriptions [3]       */
122         typedesc   returntype;      /* parsed descriptor of the return type       */
123         typedesc   paramtypes[1];   /* parameter types, variable length!          */
124 };
125
126 /* [3]...If params is NULL, the parameter descriptions have not yet been      */
127 /*       allocated. In this case ___the possible 'this' pointer of the method */
128 /*       is NOT counted in paramcount/paramslots and it is NOT included in    */
129 /*       the paramtypes array___.                                             */
130 /*       If params != NULL, the parameter descriptions have been              */
131 /*       allocated, and the 'this' pointer of the method, if any, IS included.*/
132 /*       In case the method has no parameters at all, the special value       */
133 /*       METHODDESC_NO_PARAMS is used (see below).                            */
134
135 /* METHODDESC_NO_PARAMS is a special value for the methoddesc.params field    */
136 /* indicating that the method is a static method without any parameters.      */
137 /* This special value must be != NULL and it may only be set if               */
138 /* md->paramcount == 0.                                                       */
139
140 #define METHODDESC_NOPARAMS  ((paramdesc*)1)
141
142 /* function prototypes ********************************************************/
143
144 descriptor_pool * descriptor_pool_new(classinfo *referer);
145
146 bool descriptor_pool_add_class(descriptor_pool *pool,utf *name);
147 bool descriptor_pool_add(descriptor_pool *pool,utf *desc,int *paramslots);
148
149 u2 descriptor_to_basic_type(utf *desc);
150 u2 descriptor_typesize(typedesc *td);
151
152 constant_classref * descriptor_pool_create_classrefs(descriptor_pool *pool,
153                                                                                                          s4 *count);
154 constant_classref * descriptor_pool_lookup_classref(descriptor_pool *pool,utf *classname);
155
156 void descriptor_pool_alloc_parsed_descriptors(descriptor_pool *pool);
157
158 typedesc *descriptor_pool_parse_field_descriptor(descriptor_pool *pool, utf *desc);
159 methoddesc *descriptor_pool_parse_method_descriptor(descriptor_pool *pool, utf *desc, s4 mflags,
160                                                                                                         constant_classref *thisclass);
161
162 bool descriptor_params_from_paramtypes(methoddesc *md, s4 mflags);
163
164 void *descriptor_pool_get_parsed_descriptors(descriptor_pool *pool, s4 *size);
165 void descriptor_pool_get_sizes(descriptor_pool *pool, u4 *classrefsize,
166                                                            u4 *descsize);
167
168 #ifndef NDEBUG
169 void descriptor_debug_print_typedesc(FILE *file,typedesc *d);
170 void descriptor_debug_print_methoddesc(FILE *file,methoddesc *d);
171 void descriptor_debug_print_paramdesc(FILE *file,paramdesc *d);
172 void descriptor_pool_debug_dump(descriptor_pool *pool, FILE *file);
173 #endif /* !defined(NDEBUG) */
174
175 /* machine dependent descriptor function */
176 void md_param_alloc(methoddesc *md);
177
178 #endif /* _DESCRIPTOR_H */
179
180
181 /*
182  * These are local overrides for various environment variables in Emacs.
183  * Please do not remove this and leave it at the end of the file, where
184  * Emacs will automagically detect them.
185  * ---------------------------------------------------------------------
186  * Local variables:
187  * mode: c
188  * indent-tabs-mode: t
189  * c-basic-offset: 4
190  * tab-width: 4
191  * End:
192  * vim:noexpandtab:sw=4:ts=4:
193  */