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