* src/vm/jit/verify/typecheck-typeinferer.c: New type inference pass.
[cacao.git] / src / vm / jit / verify / typecheck-builtins.inc
1 /* src/vm/jit/verify/typecheck-builtins.inc - type checking for ICMD_BUILTIN
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$
32
33 */
34
35
36 #define ISBUILTIN(v)   (bte->fp == (functionptr) (v))
37
38 {
39         builtintable_entry *bte;
40
41         bte = state->iptr->sx.s23.s3.bte;
42
43         /* XXX this is an ugly if-chain but twisti did not want a function */
44         /* pointer in builtintable_entry for this, so here you go.. ;)     */
45
46         if (ISBUILTIN(BUILTIN_new)) {
47                 dv->type = TYPE_ADR;
48 #if defined(TYPECHECK_TYPEINFERER)
49                 assert(state->iptr[-1].opc == ICMD_ACONST);
50                 typeinfo_init_class(&(dv->typeinfo), state->iptr[-1].sx.val.c);
51 #else
52                 if (state->iptr[-1].opc != ICMD_ACONST)
53                         TYPECHECK_VERIFYERROR_bool("illegal instruction: builtin_new without class");
54                 TYPEINFO_INIT_NEWOBJECT(dv->typeinfo,state->iptr);
55 #endif
56         }
57         else if (ISBUILTIN(BUILTIN_newarray_boolean)) {
58                 TYPECHECK_INT(OP1);
59                 dv->type = TYPE_ADR;
60                 TYPEINFO_INIT_PRIMITIVE_ARRAY(dv->typeinfo,ARRAYTYPE_BOOLEAN);
61         }
62         else if (ISBUILTIN(BUILTIN_newarray_char)) {
63                 TYPECHECK_INT(OP1);
64                 dv->type = TYPE_ADR;
65                 TYPEINFO_INIT_PRIMITIVE_ARRAY(dv->typeinfo,ARRAYTYPE_CHAR);
66         }
67         else if (ISBUILTIN(BUILTIN_newarray_float)) {
68                 TYPECHECK_INT(OP1);
69                 dv->type = TYPE_ADR;
70                 TYPEINFO_INIT_PRIMITIVE_ARRAY(dv->typeinfo,ARRAYTYPE_FLOAT);
71         }
72         else if (ISBUILTIN(BUILTIN_newarray_double)) {
73                 TYPECHECK_INT(OP1);
74                 dv->type = TYPE_ADR;
75                 TYPEINFO_INIT_PRIMITIVE_ARRAY(dv->typeinfo,ARRAYTYPE_DOUBLE);
76         }
77         else if (ISBUILTIN(BUILTIN_newarray_byte)) {
78                 TYPECHECK_INT(OP1);
79                 dv->type = TYPE_ADR;
80                 TYPEINFO_INIT_PRIMITIVE_ARRAY(dv->typeinfo,ARRAYTYPE_BYTE);
81         }
82         else if (ISBUILTIN(BUILTIN_newarray_short)) {
83                 TYPECHECK_INT(OP1);
84                 dv->type = TYPE_ADR;
85                 TYPEINFO_INIT_PRIMITIVE_ARRAY(dv->typeinfo,ARRAYTYPE_SHORT);
86         }
87         else if (ISBUILTIN(BUILTIN_newarray_int)) {
88                 TYPECHECK_INT(OP1);
89                 dv->type = TYPE_ADR;
90                 TYPEINFO_INIT_PRIMITIVE_ARRAY(dv->typeinfo,ARRAYTYPE_INT);
91         }
92         else if (ISBUILTIN(BUILTIN_newarray_long)) {
93                 TYPECHECK_INT(OP1);
94                 dv->type = TYPE_ADR;
95                 TYPEINFO_INIT_PRIMITIVE_ARRAY(dv->typeinfo,ARRAYTYPE_LONG);
96         }
97         else if (ISBUILTIN(BUILTIN_newarray))
98         {
99                 TYPECHECK_INT(OP1);
100                 if (state->iptr[-1].opc != ICMD_ACONST)
101                         TYPECHECK_VERIFYERROR_bool("illegal instruction: builtin_newarray without class");
102                 /* XXX check that it is an array class(ref) */
103                 dv->type = TYPE_ADR;
104                 typeinfo_init_class(&(dv->typeinfo),state->iptr[-1].sx.val.c);
105         }
106         else if (ISBUILTIN(BUILTIN_arrayinstanceof))
107         {
108                 TYPECHECK_ADR(OP1);
109                 if (state->iptr[-1].opc != ICMD_ACONST)
110                         TYPECHECK_VERIFYERROR_bool("illegal instruction: builtin_arrayinstanceof without class");
111                 dv->type = TYPE_INT;
112                 /* XXX check that it is an array class(ref) */
113         }
114         else {
115                 methoddesc *md;
116                 u1 rtype;
117 #if !defined(TYPECHECK_TYPEINFERER)
118                 s4 i;
119 #endif
120 #if defined(TYPECHECK_STACKBASED)
121                 typedescriptor *av;
122 #endif
123
124                 /* verify a generic builtin call */
125
126                 TYPECHECK_COUNT(stat_ins_builtin_gen);
127
128                 md = bte->md;
129 #if !defined(TYPECHECK_TYPEINFERER)
130                 i = md->paramcount;
131
132                 /* check the types of the arguments on the stack */
133
134 #if defined(TYPECHECK_STACKBASED)
135                 av = stack - (md->paramslots - 1);
136 #endif
137
138                 for (i--; i >= 0; i--) {
139 #if defined(TYPECHECK_VARIABLESBASED)
140                         varinfo *av = VAR(state->iptr->sx.s23.s2.args[i]);
141 #endif
142
143                         if (av->type != md->paramtypes[i].type) {
144                                 TYPECHECK_VERIFYERROR_bool("parameter type mismatch for builtin method");
145                         }
146
147 #ifdef TYPECHECK_DEBUG
148                         /* generic builtins may only take primitive types and java.lang.Object references */
149                         if (av->type == TYPE_ADR && md->paramtypes[i].classref->name != utf_java_lang_Object) {
150                                 *exceptionptr = new_internalerror("generic builtin method with non-generic reference parameter");
151                                 return false;
152                         }
153 #endif
154
155 #if defined(TYPECHECK_STACKBASED)
156                         av += (IS_2_WORD_TYPE(av->type)) ? 2 : 1;
157 #endif
158                 }
159 #endif /* !defined(TYPECHECK_TYPEINFERER) */
160
161                 /* set the return type */
162
163                 rtype = md->returntype.type;
164                 if (rtype != TYPE_VOID) {
165                         dv->type = rtype;
166                         if (!typeinfo_init_from_typedesc(&(md->returntype),NULL,&(dv->typeinfo)))
167                                 return false;
168                 }
169         }
170 }
171
172 #undef ISBUILTIN
173
174 /*
175  * These are local overrides for various environment variables in Emacs.
176  * Please do not remove this and leave it at the end of the file, where
177  * Emacs will automagically detect them.
178  * ---------------------------------------------------------------------
179  * Local variables:
180  * mode: c
181  * indent-tabs-mode: t
182  * c-basic-offset: 4
183  * tab-width: 4
184  * End:
185  * vim:noexpandtab:sw=4:ts=4:filetype=c:
186  */