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