Fix LDC, LDC_W, and INSTANCEOF opcodes, more debugging
[jamvm-avr32-jem.git] / src / dll_ffi.c
blob20bd64b005c4facf776935fa5d0cb343ba75f1bb
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 /* Must be included first to get configure options */
23 #include "jam.h"
25 #ifdef USE_FFI
26 #include <ffi.h>
27 #include <stdio.h>
28 #include "sig.h"
30 ffi_type *sig2ffi(char sig) {
31 ffi_type *type;
33 switch(sig) {
34 case 'V':
35 type = &ffi_type_void;
36 break;
37 case 'D':
38 type = &ffi_type_double;
39 break;
40 case 'J':
41 type = &ffi_type_sint64;
42 break;
43 case 'F':
44 type = &ffi_type_float;
45 break;
46 default:
47 type = &ffi_type_pointer;
48 break;
51 return type;
54 int nativeExtraArg(MethodBlock *mb) {
55 char *sig = mb->type;
56 int count = 2;
58 SCAN_SIG(sig, count++, count++);
60 return count;
63 #define DOUBLE \
64 *types_pntr++ = sig2ffi(*sig);\
65 *values_pntr++ = opntr; \
66 opntr += 2
68 #define SINGLE \
69 *types_pntr++ = sig2ffi(*sig);\
70 *values_pntr++ = opntr++
72 uintptr_t *callJNIMethod(void *env, Class *class, char *sig, int num_args, uintptr_t *ostack,
73 unsigned char *func) {
74 ffi_cif cif;
75 void *values[num_args];
76 ffi_type *types[num_args];
77 uintptr_t *opntr = ostack;
78 void **values_pntr = &values[2];
79 ffi_type **types_pntr = &types[2];
81 types[0] = &ffi_type_pointer;
82 values[0] = &env;
84 types[1] = &ffi_type_pointer;
85 values[1] = class ? &class : (void*)opntr++;
87 SCAN_SIG(sig, DOUBLE, SINGLE);
89 if(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, num_args, sig2ffi(*sig), types) == FFI_OK) {
90 ffi_call(&cif, FFI_FN(func), ostack, values);
92 if(*sig == 'J' || *sig == 'D')
93 ostack += 2;
94 else if(*sig != 'V')
95 ostack++;
98 return ostack;
100 #endif