nativebinding.cpp 9.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/* ---------------------------------------------------------------------------
// Multi-Phasic Applications: SquirrelJME
//     Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
// ---------------------------------------------------------------------------
// SquirrelJME is under the GNU General Public License v3+, or later.
// See license.mkd for licensing and copyright information.
// --------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
11
#include <stdint.h>
12
13
14
15

#include "jni.h"
#include "cc_squirreljme_emulator_NativeBinding.h"
#include "cc_squirreljme_jvm_Assembly.h"
16
#include "squirreljme.h"
17

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
JNIEXPORT jlong JNICALL longPack(JNIEnv* env, jclass classy, jint al, jint ah)
{
	return (((jlong)al) & UINT64_C(0xFFFFFFFF)) |
		((((jlong)ah) & UINT64_C(0xFFFFFFFF)) << UINT64_C(32));
}

JNIEXPORT jint JNICALL longUnpackHigh(JNIEnv* env, jclass classy, jlong v)
{
	return (jint)(v >> UINT64_C(32));
}

JNIEXPORT jint JNICALL longUnpackLow(JNIEnv* env, jclass classy, jlong v)
{
	return (jint)(v);
}

34
35
// Method handler for special functions
JNIEXPORT void JNICALL restrictedFunction(JNIEnv* env, jclass classy);
36
37
38
39

// Assembly method mappings
static const JNINativeMethod assemblyMethods[] =
{
40
	{"arrayLength", "(I)I", (void*)restrictedFunction},
41
42
43
44
45
46
47
48
49
50
51
52
	{"arrayLength", "(Ljava/lang/Object;)I", (void*)restrictedFunction},
	{"atomicCompareGetAndSet", "(IIJ)I", (void*)restrictedFunction},
	{"atomicDecrementAndGet", "(J)I", (void*)restrictedFunction},
	{"atomicIncrement", "(J)V", (void*)restrictedFunction},
	{"breakpoint", "()V", (void*)restrictedFunction},
	{"doublePack", "(II)D", (void*)restrictedFunction},
	{"doubleToRawLongBits", "(D)J", (void*)restrictedFunction},
	{"doubleUnpackHigh", "(D)I", (void*)restrictedFunction},
	{"doubleUnpackLow", "(D)I", (void*)restrictedFunction},
	{"exceptionHandle", "()V", (void*)restrictedFunction},
	{"floatToRawIntBits", "(F)I", (void*)restrictedFunction},
	{"intBitsToFloat", "(I)F", (void*)restrictedFunction},
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
	{"invoke", "(II)V", (void*)restrictedFunction},
	{"invoke", "(III)V", (void*)restrictedFunction},
	{"invoke", "(IIII)V", (void*)restrictedFunction},
	{"invoke", "(IIIII)V", (void*)restrictedFunction},
	{"invoke", "(IIIIII)V", (void*)restrictedFunction},
	{"invoke", "(IIIIIII)V", (void*)restrictedFunction},
	{"invoke", "(IIIIIIII)V", (void*)restrictedFunction},
	{"invoke", "(IIIIIIIII)V", (void*)restrictedFunction},
	{"invoke", "(IIIIIIIIII)V", (void*)restrictedFunction},
	{"invokeV", "(II)I", (void*)restrictedFunction},
	{"invokeV", "(III)I", (void*)restrictedFunction},
	{"invokeV", "(IIII)I", (void*)restrictedFunction},
	{"invokeV", "(IIIII)I", (void*)restrictedFunction},
	{"invokeV", "(IIIIII)I", (void*)restrictedFunction},
	{"invokeV", "(IIIIIII)I", (void*)restrictedFunction},
	{"invokeV", "(IIIIIIII)I", (void*)restrictedFunction},
	{"invokeV", "(IIIIIIIII)I", (void*)restrictedFunction},
	{"invokeV", "(IIIIIIIIII)I", (void*)restrictedFunction},
	{"invokeVL", "(II)J", (void*)restrictedFunction},
	{"invokeVL", "(III)J", (void*)restrictedFunction},
	{"invokeVL", "(IIII)J", (void*)restrictedFunction},
	{"invokeVL", "(IIIII)J", (void*)restrictedFunction},
	{"invokeVL", "(IIIIII)J", (void*)restrictedFunction},
	{"invokeVL", "(IIIIIII)J", (void*)restrictedFunction},
	{"invokeVL", "(IIIIIIII)J", (void*)restrictedFunction},
	{"invokeVL", "(IIIIIIIII)J", (void*)restrictedFunction},
	{"invokeVL", "(IIIIIIIIII)J", (void*)restrictedFunction},
80
	{"longBitsToDouble", "(J)D", (void*)restrictedFunction},
81
82
83
	{"longPack", "(II)J", (void*)longPack},
	{"longUnpackHigh", "(J)I", (void*)longUnpackHigh},
	{"longUnpackLow", "(J)I", (void*)longUnpackLow},
84
85
	{"memReadByte", "(JI)I", (void*)restrictedFunction},
	{"memReadInt", "(JI)I", (void*)restrictedFunction},
86
	{"memReadLong", "(JI)J", (void*)restrictedFunction},
87
88
89
	{"memReadShort", "(JI)I", (void*)restrictedFunction},
	{"memWriteByte", "(JII)V", (void*)restrictedFunction},
	{"memWriteInt", "(JII)V", (void*)restrictedFunction},
90
	{"memWriteLong", "(JIJ)V", (void*)restrictedFunction},
91
	{"memWriteShort", "(JII)V", (void*)restrictedFunction},
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
	{"monitorCountDecrementAndGetAtomic", "(J)I", (void*)restrictedFunction},
	{"monitorCountDecrementAndGetAtomic", "(Ljava/lang/Object;)I", (void*)restrictedFunction},
	{"monitorCountIncrementAndGetAtomic", "(J)I", (void*)restrictedFunction},
	{"monitorCountIncrementAndGetAtomic", "(Ljava/lang/Object;)I", (void*)restrictedFunction},
	{"monitorCountGetAtomic", "(J)I", (void*)restrictedFunction},
	{"monitorCountGetAtomic", "(Ljava/lang/Object;)I", (void*)restrictedFunction},
	{"monitorCountSetAtomic", "(JI)V", (void*)restrictedFunction},
	{"monitorCountSetAtomic", "(Ljava/lang/Object;I)V", (void*)restrictedFunction},
	{"monitorOwnerCompareGetAndSetAtomic", "(JJJ)Ljava/lang/Thread;", (void*)restrictedFunction},
	{"monitorOwnerCompareGetAndSetAtomic", "(Ljava/lang/Object;JJ)Ljava/lang/Thread;", (void*)restrictedFunction},
	{"monitorOwnerCompareGetAndSetAtomic", "(JLjava/lang/Thread;Ljava/lang/Thread;)Ljava/lang/Thread;", (void*)restrictedFunction},
	{"monitorOwnerCompareGetAndSetAtomic", "(Ljava/lang/Object;Ljava/lang/Thread;Ljava/lang/Thread;)Ljava/lang/Thread;", (void*)restrictedFunction},
	{"monitorOwnerCompareGetAndSetAtomicPointer", "(JJJ)J", (void*)restrictedFunction},
	{"monitorOwnerCompareGetAndSetAtomicPointer", "(Ljava/lang/Object;JJ)J", (void*)restrictedFunction},
	{"monitorOwnerCompareGetAndSetAtomicPointer", "(JLjava/lang/Thread;Ljava/lang/Thread;)J", (void*)restrictedFunction},
	{"monitorOwnerCompareGetAndSetAtomicPointer", "(Ljava/lang/Object;Ljava/lang/Thread;Ljava/lang/Thread;)J", (void*)restrictedFunction},
	{"monitorOwnerGetAtomic", "(J)Ljava/lang/Thread;", (void*)restrictedFunction},
	{"monitorOwnerGetAtomic", "(Ljava/lang/Object;)Ljava/lang/Thread;", (void*)restrictedFunction},
	{"monitorOwnerGetPointerAtomic", "(J)J", (void*)restrictedFunction},
	{"monitorOwnerGetPointerAtomic", "(Ljava/lang/Object;)J", (void*)restrictedFunction},
	{"monitorOwnerSetAtomic", "(JJ)V", (void*)restrictedFunction},
	{"monitorOwnerSetAtomic", "(JLjava/lang/Thread;)V", (void*)restrictedFunction},
	{"monitorOwnerSetAtomic", "(Ljava/lang/Object;J)V", (void*)restrictedFunction},
	{"monitorOwnerSetAtomic", "(Ljava/lang/Object;Ljava/lang/Thread;)V", (void*)restrictedFunction},
116
117
118
119
120
121
	{"objectToPointer", "(Ljava/lang/Object;)I", (void*)restrictedFunction},
	{"objectToPointerRefQueue", "(Ljava/lang/Object;)I", (void*)restrictedFunction},
	{"pointerToObject", "(I)Ljava/lang/Object;", (void*)restrictedFunction},
	{"poolLoad", "(II)I", (void*)restrictedFunction},
	{"poolLoad", "(Ljava/lang/Object;I)I", (void*)restrictedFunction},
	{"refCount", "(I)V", (void*)restrictedFunction},
122
	{"refCount", "(Ljava/lang/Object;)V", (void*)restrictedFunction},
123
	{"refGetCount", "(I)I", (void*)restrictedFunction},
124
	{"refGetCount", "(Ljava/lang/Object;)I", (void*)restrictedFunction},
125
	{"refSetCount", "(II)V", (void*)restrictedFunction},
126
	{"refSetCount", "(Ljava/lang/Object;I)V", (void*)restrictedFunction},
127
	{"refUncount", "(I)V", (void*)restrictedFunction},
128
129
130
131
132
	{"refUncount", "(Ljava/lang/Object;)V", (void*)restrictedFunction},
	{"returnFrame", "()V", (void*)restrictedFunction},
	{"returnFrame", "(I)V", (void*)restrictedFunction},
	{"returnFrame", "(II)V", (void*)restrictedFunction},
	{"returnFrameLong", "(J)V", (void*)restrictedFunction},
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
	{"specialGetExceptionRegister", "()Ljava/lang/Object;", (void*)restrictedFunction},
	{"specialGetExceptionRegisterThrowable", "()Ljava/lang/Throwable;", (void*)restrictedFunction},
	{"specialGetExceptionRegisterPointer", "()J", (void*)restrictedFunction},
	{"specialGetPoolRegister", "()Ljava/lang/Object;", (void*)restrictedFunction},
	{"specialGetPoolRegisterPointer", "()J", (void*)restrictedFunction},
	{"specialGetReturnRegister", "()I", (void*)restrictedFunction},
	{"specialGetReturnRegisterLong", "()J", (void*)restrictedFunction},
	{"specialGetThreadRegister", "()Ljava/lang/Thread;", (void*)restrictedFunction},
	{"specialGetThreadRegisterPointer", "()J", (void*)restrictedFunction},
	{"specialSetExceptionRegister", "(J)V", (void*)restrictedFunction},
	{"specialSetExceptionRegister", "(Ljava/lang/Object;)V", (void*)restrictedFunction},
	{"specialSetPoolRegister", "(J)V", (void*)restrictedFunction},
	{"specialSetPoolRegister", "(Ljava/lang/Object;)V", (void*)restrictedFunction},
	{"specialSetThreadRegister", "(J)V", (void*)restrictedFunction},
	{"specialSetThreadRegister", "(Ljava/lang/Thread;)V", (void*)restrictedFunction},
148
149
150
151
152
153
154
};

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
{
	JNIEnv* env;

	// Used to indicate that something might be happened
155
	fprintf(stderr, "JNI Sub-Level: Loading Library...\n");
156
157
158
159
160

	// Support Java 7!
	return JNI_VERSION_1_6;
}

161
162
JNIEXPORT void JNICALL restrictedFunction(JNIEnv* env, jclass classy)
{
163
	// Call the oops method, so we can get a reasonable stack trace
164
	env->Throw((jthrowable)forwardCallStaticObject(env,
165
		"cc.squirreljme.runtime.cldc.debug.Debugging",
166
		"oops", "()java/lang/Error"));
167
168
}

169
170
171
JNIEXPORT jint JNICALL Java_cc_squirreljme_emulator_NativeBinding__1_1bindMethods
	(JNIEnv* env, jclass classy)
{
172
	jint rv = 0;
173
174
175
	
	// It is happening!
	fprintf(stderr, "JNI Sub-Level: Binding Methods...\n");
176
177

	rv |= env->RegisterNatives(env->FindClass("cc/squirreljme/jvm/Assembly"),
178
		assemblyMethods, sizeof(assemblyMethods) / sizeof(JNINativeMethod));
179
	
180
	rv |= mleDebugInit(env, classy);
181
	rv |= mleFormInit(env, classy);
182
	rv |= mleJarInit(env, classy);
183
	rv |= mleObjectInit(env, classy);
184
	rv |= mlePencilInit(env, classy);
185
	rv |= mleRuntimeInit(env, classy);
186
	rv |= mleTaskInit(env, classy);
187
	rv |= mleTerminalInit(env, classy);
188
	rv |= mleThreadInit(env, classy);
189
190
191
	
	// It happened!
	fprintf(stderr, "JNI Sub-Level: Methods are now bound!\n");
192
193

	return rv;
194
}