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