Fix LDC, LDC_W, and INSTANCEOF opcodes, more debugging
[jamvm-avr32-jem.git] / src / cast.c
blob4ab50650b048d76a44cbfc8199bc8051d5a16d61
1 /*
2 * Copyright (C) 2003, 2004, 2005, 2006, 2007
3 * Robert Lougher <rob@lougher.org.uk>.
5 * This file is part of JamVM.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "jam.h"
24 char implements(Class *class, Class *test) {
25 ClassBlock *test_cb = CLASS_CB(test);
26 int i;
28 for(i = 0; i < test_cb->interfaces_count; i++)
29 if((class == test_cb->interfaces[i]) ||
30 implements(class, test_cb->interfaces[i]))
31 return TRUE;
33 if(test_cb->super)
34 return implements(class, test_cb->super);
36 return FALSE;
39 char isSubClassOf(Class *class, Class *test) {
40 for(; test != NULL && test != class; test = CLASS_CB(test)->super);
41 return test != NULL;
44 char isInstOfArray0(Class *array_class, Class *test_elem, int test_dim) {
45 ClassBlock *array_cb = CLASS_CB(array_class);
46 Class *array_elem = array_cb->element_class;
48 if(test_dim == array_cb->dim)
49 return isInstanceOf(array_elem, test_elem);
51 if(test_dim > array_cb->dim)
52 return IS_INTERFACE(CLASS_CB(array_elem)) ?
53 implements(array_elem, array_class) :
54 (array_elem == array_cb->super);
56 return FALSE;
59 char isInstOfArray(Class *class, Class *test) {
60 ClassBlock *test_cb = CLASS_CB(test);
62 if(!IS_ARRAY(CLASS_CB(class)))
63 return class == test_cb->super;
65 return isInstOfArray0(class, test_cb->element_class, test_cb->dim);
68 char isInstanceOf(Class *class, Class *test) {
69 if(class == test)
70 return TRUE;
72 if(IS_INTERFACE(CLASS_CB(class)))
73 return implements(class, test);
74 else
75 if(IS_ARRAY(CLASS_CB(test)))
76 return isInstOfArray(class, test);
77 else
78 return isSubClassOf(class, test);
81 char arrayStoreCheck(Class *array_class, Class *test) {
82 ClassBlock *test_cb = CLASS_CB(test);
84 if(!IS_ARRAY(test_cb))
85 return isInstOfArray0(array_class, test, 1);
87 return isInstOfArray0(array_class, test_cb->element_class, test_cb->dim + 1);