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