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