9183e4b17dfcc7222119c9098f4778d31df725a7
[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, 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$
26
27 */
28
29
30 #define ISBUILTIN(v)   (bte->fp == (functionptr) (v))
31
32 {
33         builtintable_entry *bte;
34
35         bte = state->iptr->sx.s23.s3.bte;
36
37         /* XXX this is an ugly if-chain but twisti did not want a function */
38         /* pointer in builtintable_entry for this, so here you go.. ;)     */
39
40         if (ISBUILTIN(BUILTIN_new)) {
41                 dv->type = TYPE_ADR;
42 #if defined(TYPECHECK_TYPEINFERER)
43                 assert(state->iptr[-1].opc == ICMD_ACONST);
44                 typeinfo_init_class(&(dv->typeinfo), state->iptr[-1].sx.val.c);
45 #else
46                 if (state->iptr[-1].opc != ICMD_ACONST)
47                         TYPECHECK_VERIFYERROR_bool("illegal instruction: builtin_new without class");
48                 TYPEINFO_INIT_NEWOBJECT(dv->typeinfo,state->iptr);
49 #endif
50         }
51         else if (ISBUILTIN(BUILTIN_newarray_boolean)) {
52                 TYPECHECK_INT(OP1);
53                 dv->type = TYPE_ADR;
54                 TYPEINFO_INIT_PRIMITIVE_ARRAY(dv->typeinfo,ARRAYTYPE_BOOLEAN);
55         }
56         else if (ISBUILTIN(BUILTIN_newarray_char)) {
57                 TYPECHECK_INT(OP1);
58                 dv->type = TYPE_ADR;
59                 TYPEINFO_INIT_PRIMITIVE_ARRAY(dv->typeinfo,ARRAYTYPE_CHAR);
60         }
61         else if (ISBUILTIN(BUILTIN_newarray_float)) {
62                 TYPECHECK_INT(OP1);
63                 dv->type = TYPE_ADR;
64                 TYPEINFO_INIT_PRIMITIVE_ARRAY(dv->typeinfo,ARRAYTYPE_FLOAT);
65         }
66         else if (ISBUILTIN(BUILTIN_newarray_double)) {
67                 TYPECHECK_INT(OP1);
68                 dv->type = TYPE_ADR;
69                 TYPEINFO_INIT_PRIMITIVE_ARRAY(dv->typeinfo,ARRAYTYPE_DOUBLE);
70         }
71         else if (ISBUILTIN(BUILTIN_newarray_byte)) {
72                 TYPECHECK_INT(OP1);
73                 dv->type = TYPE_ADR;
74                 TYPEINFO_INIT_PRIMITIVE_ARRAY(dv->typeinfo,ARRAYTYPE_BYTE);
75         }
76         else if (ISBUILTIN(BUILTIN_newarray_short)) {
77                 TYPECHECK_INT(OP1);
78                 dv->type = TYPE_ADR;
79                 TYPEINFO_INIT_PRIMITIVE_ARRAY(dv->typeinfo,ARRAYTYPE_SHORT);
80         }
81         else if (ISBUILTIN(BUILTIN_newarray_int)) {
82                 TYPECHECK_INT(OP1);
83                 dv->type = TYPE_ADR;
84                 TYPEINFO_INIT_PRIMITIVE_ARRAY(dv->typeinfo,ARRAYTYPE_INT);
85         }
86         else if (ISBUILTIN(BUILTIN_newarray_long)) {
87                 TYPECHECK_INT(OP1);
88                 dv->type = TYPE_ADR;
89                 TYPEINFO_INIT_PRIMITIVE_ARRAY(dv->typeinfo,ARRAYTYPE_LONG);
90         }
91         else if (ISBUILTIN(BUILTIN_newarray))
92         {
93                 TYPECHECK_INT(OP1);
94                 if (state->iptr[-1].opc != ICMD_ACONST)
95                         TYPECHECK_VERIFYERROR_bool("illegal instruction: builtin_newarray without class");
96                 /* XXX check that it is an array class(ref) */
97                 dv->type = TYPE_ADR;
98                 typeinfo_init_class(&(dv->typeinfo),state->iptr[-1].sx.val.c);
99         }
100         else if (ISBUILTIN(BUILTIN_arrayinstanceof))
101         {
102                 TYPECHECK_ADR(OP1);
103                 if (state->iptr[-1].opc != ICMD_ACONST)
104                         TYPECHECK_VERIFYERROR_bool("illegal instruction: builtin_arrayinstanceof without class");
105                 dv->type = TYPE_INT;
106                 /* XXX check that it is an array class(ref) */
107         }
108         else {
109                 methoddesc *md;
110                 u1 rtype;
111 #if !defined(TYPECHECK_TYPEINFERER)
112                 s4 i;
113 #endif
114 #if defined(TYPECHECK_STACKBASED)
115                 typedescriptor *av;
116 #endif
117
118                 /* verify a generic builtin call */
119
120                 TYPECHECK_COUNT(stat_ins_builtin_gen);
121
122                 md = bte->md;
123 #if !defined(TYPECHECK_TYPEINFERER)
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                                 exceptions_throw_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 #endif /* !defined(TYPECHECK_TYPEINFERER) */
154
155                 /* set the return type */
156
157                 rtype = md->returntype.type;
158                 if (rtype != TYPE_VOID) {
159                         dv->type = rtype;
160                         if (!typeinfo_init_from_typedesc(&(md->returntype),NULL,&(dv->typeinfo)))
161                                 return false;
162                 }
163         }
164 }
165
166 #undef ISBUILTIN
167
168 /*
169  * These are local overrides for various environment variables in Emacs.
170  * Please do not remove this and leave it at the end of the file, where
171  * Emacs will automagically detect them.
172  * ---------------------------------------------------------------------
173  * Local variables:
174  * mode: c
175  * indent-tabs-mode: t
176  * c-basic-offset: 4
177  * tab-width: 4
178  * End:
179  * vim:noexpandtab:sw=4:ts=4:filetype=c:
180  */