-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsjvm.cpp
2577 lines (2565 loc) · 117 KB
/
sjvm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2010 by Benjamin J. Land (a.k.a. BenLand100)
*
* This file is part of SJVM the Simple Java Virtual Machine.
*
* SJVM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SJVM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SJVM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "jni.h"
#include "GarbageCollector.h"
#include "Arrays.h"
#include "ClassFile.h"
#include "ClassLoader.h"
#include "CPInfo.h"
#include "SJVM.h"
#include "MethodInfo.h"
#include "FieldInfo.h"
#include "Exception.h"
#include "Native.h"
#include "jni.h"
#include <iostream>
#include <map>
#include <string>
#include <sstream>
#include <fstream>
#include <cstring>
#define WARNING_OUTPUT
//#define DEBUG_OUTPUT
#ifdef DEBUG_OUTPUT
#define debug(v) std::cout << v;
#else
#define debug(v)
#endif
#ifdef WARNING_OUTPUT
#define warn(v) std::cout << "WARNING: " << v;
#else
#define warn(v)
#endif
SJVM::SJVM(ClassPath *path) {
gc = new GlobalGC;
loader = new ClassLoader(path);
vm = new JavaVM;
JNIInvokeInterface_ *functions = new JNIInvokeInterface_;
memcpy(functions, &SJVM_JNIInvokeInterface, sizeof (JNIInvokeInterface_));
functions->reserved0 = this;
vm->functions = functions;
}
SJVM::~SJVM() {
delete gc;
delete loader;
delete vm->functions;
delete vm;
debug("SJVM Destroyed");
}
extern "C" void JNICALL Java_sjvm_SJVM_nativePrint(JNIEnv* env, jclass type, jstring string) {
const char* bytes = env->GetStringUTFChars(string, 0);
std::cout << bytes << '\n';
env->ReleaseStringUTFChars(string, bytes);
}
ClassFile* SJVM::loadClass(const char* name) {
return loader->load(name, this);
}
JavaVM* SJVM::getJavaVM() {
return vm;
}
#include <math.h>
#include "OPCodes.h"
#include "Thread.h"
#include "Native.h"
#include "GarbageCollector.h"
#define U2(var,data,index) var = data[index++] << 8; var |= data[index++];
#define U4(var,data,index) var = data[index++] << 24; var |= data[index++] << 16; var |= data[index++] << 8; var |= data[index++];
/**
* args are treated as a 32bit array, longs happen to be 32 bits
* exception sets the value to zero if no exception, otherwise a pointer to
* a valid exception object (must be freed by caller)
*/
Variable SJVM::execute(MethodInfo *meth, ClassFile *cur, Thread* thread, long* args, long argsc, Exception **thrown) {
debug("Invoked " << meth->full << " from " << cur->name << '\n');
*thrown = 0;
LocalGC localgc(gc, thread);
if (meth->native || meth->accessFlags & 0x0100) {
if (meth->accessFlags & 0x0008) {
argsc += 2;
long *newargs = new long[argsc];
newargs[0] = (long) thread->getEnv();
newargs[1] = (long) cur;
for (int i = 2; i < argsc; i++)
newargs[i] = args[i - 2];
args = newargs;
} else {
argsc++;
long *newargs = new long[argsc];
newargs[0] = (long) thread->getEnv();
for (int i = 1; i < argsc; i++)
newargs[i] = args[i - 1];
args = newargs;
}
if (meth->native == 0) {
debug("Locating Native Method\n");
std::stringstream fullname;
fullname << "Java_";
std::string classname(cur->name);
for (int i = 0; (i = classname.find('_', i)) != std::string::npos; i += 2)
classname.replace(i, 1, "_1", 0, 2);
for (int i = 0; (i = classname.find('/', i)) != std::string::npos; i += 1)
classname.replace(i, 1, "_", 0, 1);
fullname << classname;
std::string methodname(meth->name);
for (int i = 0; (i = methodname.find('_', i)) != std::string::npos; i += 2)
methodname.replace(i, 1, "_1", 0, 2);
for (int i = 0; (i = methodname.find('/', i)) != std::string::npos; i += 1)
methodname.replace(i, 1, "_", 0, 1);
fullname << "_";
fullname << methodname;
std::stringstream search;
search << fullname.str();
//search << '@' << argsc * 4;
debug("Searching for native... " << search.str() << '\n');
if (!(meth->native = libpool.address(search.str().c_str()))) {
std::string descriptor;
descriptor.append(meth->descriptor, 1, meth->descriptor.find(')') - 1);
for (int i = 0; (i = descriptor.find('_', i)) != std::string::npos; i += 2)
descriptor.replace(i, 1, "_1", 0, 2);
for (int i = 0; (i = descriptor.find(';', i)) != std::string::npos; i += 2)
descriptor.replace(i, 1, "_2", 0, 2);
for (int i = 0; (i = descriptor.find('[', i)) != std::string::npos; i += 2)
descriptor.replace(i, 1, "_3", 0, 2);
for (int i = 0; (i = descriptor.find('/', i)) != std::string::npos; i += 1)
descriptor.replace(i, 1, "_", 0, 1);
search.seekp(0);
fullname << "__";
fullname << descriptor;
search << fullname.str();
//search << '@' << argsc * 4;
debug("Searching for native... " << search.str() << '\n');
if (!(meth->native = libpool.address(search.str().c_str()))) {
warn("Failed to locate native method: " << search.str() << '\n');
Variable result;
result.j = 0;
/*JNIEnv* env = thread->getEnv();
jclass clazz = env->FindClass("java/lang/UnsatisfiedLinkError");
if (thread->nativeException) return result;
jmethodID id = env->GetMethodID(clazz, "<init>", "()V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id);
*thrown = new Exception(cur, meth, obj);*/
delete [] args;
return result;
}
}
}
debug("Native address: " << (unsigned int) meth->native << '\n');
thread->nativeException = 0;
long resl, resh;
asm ("1:\n"
"dec %%ecx\n"
"push (%%edx,%%ecx,4)\n"
"jnz 1b\n"
"call *%%eax"
: "=a" (resl), "=d" (resh)
: "c" (argsc), "d" (args), "a" (meth->native)
: "memory");
delete [] args;
*thrown = thread->nativeException;
if (meth->resobject) thread->nativeLocal->removeLocal((JOBJECT) resl);
Variable result;
if (meth->result == 2) {
result.j = ((long long) resh << 32) | (resl);
} else if (meth->result == 1) {
result.i = resl;
}
return result;
} else {
CPInfo **cp = cur->cp;
long locals[meth->code->maxLocals];
for (int i = 0; i < argsc; i++)
locals[i] = args[i];
long stackStart[meth->code->maxStack];
long *stack = stackStart - 1;
unsigned char *bc = meth->code->code;
unsigned int pc = 0;
Exception *exception = 0;
for (;;) {
debug("OP: " << (unsigned int) bc[pc] << '\n');
switch (bc[pc++]) {
case aaload:
{
int pos = *(stack--);
JOBJECTARRAY array = (JOBJECTARRAY) * stack;
if (!array) {
JNIEnv* env = thread->getEnv();
debug("aaload null\n");
jclass clazz = env->FindClass("java/lang/NullPointerException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "()V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
if (pos < 0 || pos >= array->size) {
JNIEnv* env = thread->getEnv();
jclass clazz = env->FindClass("java/lang/ArrayIndexOutOfBoundsException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "(I)V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id, pos);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
*(JOBJECT*) stack = array->array[pos];
localgc.addLocal(*(JOBJECT*) stack);
break;
}
case aastore:
{
JOBJECT obj = *(JOBJECT*) (stack--);
int pos = *(stack--);
JOBJECTARRAY array = (JOBJECTARRAY) *(stack--);
if (!array) {
JNIEnv* env = thread->getEnv();
debug("aastore null\n");
jclass clazz = env->FindClass("java/lang/NullPointerException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "()V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
if (pos < 0 || pos >= array->size) {
JNIEnv* env = thread->getEnv();
jclass clazz = env->FindClass("java/lang/ArrayIndexOutOfBoundsException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "(I)V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id, pos);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
JOBJECT old = array->array[pos];
if (old) gc->decrRef(old);
array->array[pos] = obj;
gc->incrRef(obj);
break;
}
case aconst_null:
stack++;
*stack = 0;
break;
case aload:
stack++;
*stack = locals[bc[pc++]];
break;
case aload_0:
stack++;
*stack = locals[0];
break;
case aload_1:
stack++;
*stack = locals[1];
break;
case aload_2:
stack++;
*stack = locals[2];
break;
case aload_3:
stack++;
*stack = locals[3];
break;
case anewarray:
{
int size = *(stack--);
unsigned short index;
U2(index, bc, pc);
ClassInfo *cinfo = (ClassInfo*) cp[index];
ClassFile* arrayType;
if (cinfo->classRef == 0) {
std::string name(((UTF8Info*) cp[cinfo->nameIndex])->bytes);
arrayType = loader->load((char*) name.c_str(), this);
if (thread->nativeException) goto EXCEPTION_HANDLER;
cinfo->classRef = arrayType;
cinfo->name = name;
} else {
arrayType = cinfo->classRef;
}
std::string name("[");
name.append(cinfo->name);
JOBJECTARRAY array = ClassFile::newArray<JOBJECT>(this,(char*)name.c_str(),size);
*(JOBJECT*) (++stack) = array;
localgc.addLocal(*(JOBJECT*) stack);
debug("anewarray\n");
break;
}
case areturn:
{
Variable result;
result.l = *(JOBJECT*) stack;
localgc.removeLocal(result.l);
return result;
}
case arraylength:
if (!(*(JOBJECTARRAY*) stack)) {
JNIEnv* env = thread->getEnv();
debug("arraylength null\n");
jclass clazz = env->FindClass("java/lang/NullPointerException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "()V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
*stack = ((JOBJECTARRAY) * stack)->size; //type irrelevant
break;
case astore:
locals[bc[pc++]] = *stack;
stack--;
break;
case astore_0:
locals[0] = *stack;
stack--;
break;
case astore_1:
locals[1] = *stack;
stack--;
break;
case astore_2:
locals[2] = *stack;
stack--;
break;
case astore_3:
locals[3] = *stack;
stack--;
break;
case baload:
{
int pos = *(stack--);
JBYTEARRAY array = (JBYTEARRAY) * stack;
if (!array) {
JNIEnv* env = thread->getEnv();
debug("baload null\n");
jclass clazz = env->FindClass("java/lang/NullPointerException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "()V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
if (pos < 0 || pos >= array->size) {
JNIEnv* env = thread->getEnv();
jclass clazz = env->FindClass("java/lang/ArrayIndexOutOfBoundsException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "(I)V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id, pos);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
*stack = array->array[pos];
break;
}
case bastore:
{
JBYTE b = *(stack--);
int pos = *(stack--);
JBYTEARRAY array = (JBYTEARRAY) *(stack--);
if (!array) {
JNIEnv* env = thread->getEnv();
debug("bastore null\n");
jclass clazz = env->FindClass("java/lang/NullPointerException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "()V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
if (pos < 0 || pos >= array->size) {
JNIEnv* env = thread->getEnv();
jclass clazz = env->FindClass("java/lang/ArrayIndexOutOfBoundsException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "(I)V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id, pos);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
array->array[pos] = b;
break;
}
case bipush:
*(++stack) = bc[pc++];
break;
case caload:
{
int pos = *(unsigned int*) (stack--);
JCHARARRAY array = (JCHARARRAY) * stack;
if (!array) {
JNIEnv* env = thread->getEnv();
debug("caload null\n");
jclass clazz = env->FindClass("java/lang/NullPointerException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "()V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
if (pos < 0 || pos >= array->size) {
JNIEnv* env = thread->getEnv();
jclass clazz = env->FindClass("java/lang/ArrayIndexOutOfBoundsException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "(I)V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id, pos);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
*stack = array->array[pos];
break;
}
case castore:
{
JCHAR c = *(unsigned int*) (stack--);
int pos = *(stack--);
JCHARARRAY array = (JCHARARRAY) *(stack--);
if (!array) {
JNIEnv* env = thread->getEnv();
debug("castore null\n");
jclass clazz = env->FindClass("java/lang/NullPointerException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "()V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
if (pos < 0 || pos >= array->size) {
JNIEnv* env = thread->getEnv();
jclass clazz = env->FindClass("java/lang/ArrayIndexOutOfBoundsException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "(I)V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id, pos);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
array->array[pos] = c;
break;
}
case checkcast:
{
unsigned short index;
U2(index, bc, pc);
ClassInfo *cinfo = (ClassInfo*) cp[index];
ClassFile *type;
if (cinfo->classRef == 0) {
std::string name(((UTF8Info*) cp[cinfo->nameIndex])->bytes);
type = loader->load((char*) name.c_str(), this);
if (thread->nativeException) goto EXCEPTION_HANDLER;
cinfo->classRef = type;
cinfo->name = name;
} else {
type = cinfo->classRef;
}
if (!(*(JOBJECT*) stack)) break;
if (!type->instanceOf(*(JOBJECT*) stack)) {
JNIEnv* env = thread->getEnv();
jclass clazz = env->FindClass("java/lang/ClassCastException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "()V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
break;
}
case d2f:
*(JFLOAT*) (--stack) = (JFLOAT) *((JDOUBLE*) (stack - 1));
break;
case d2i:
*(JINT*) (--stack) = (JINT) *((JDOUBLE*) (stack - 1));
break;
case d2l:
*(JLONG*) (stack - 1) = (JLONG) *((JDOUBLE*) (stack - 1));
break;
case dadd:
{
double a = *((JDOUBLE*) (stack - 1));
stack -= 2;
*(JDOUBLE*) (stack - 1) = *(JDOUBLE*) (stack - 1) + a;
debug("dadd: " << *(JDOUBLE*) (stack - 1) << '\n');
break;
}
case daload:
{
int pos = *(stack--);
JDOUBLEARRAY array = (JDOUBLEARRAY) * stack;
if (!array) {
JNIEnv* env = thread->getEnv();
debug("daload null\n");
jclass clazz = env->FindClass("java/lang/NullPointerException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "()V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
if (pos < 0 || pos >= array->size) {
JNIEnv* env = thread->getEnv();
jclass clazz = env->FindClass("java/lang/ArrayIndexOutOfBoundsException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "(I)V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id, pos);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
*(JDOUBLE*) (stack++) = array->array[pos];
break;
}
case dastore:
{
JDOUBLE d = *((JDOUBLE*) (stack - 1));
stack -= 2;
int pos = *(stack--);
JDOUBLEARRAY array = (JDOUBLEARRAY) *(stack--);
if (!array) {
JNIEnv* env = thread->getEnv();
debug("dastore null\n");
jclass clazz = env->FindClass("java/lang/NullPointerException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "()V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
if (pos < 0 || pos >= array->size) {
JNIEnv* env = thread->getEnv();
jclass clazz = env->FindClass("java/lang/ArrayIndexOutOfBoundsException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "(I)V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id, pos);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
array->array[pos] = d;
break;
}
case dcmpl:
case dcmpg:
{
JDOUBLE a = *((JDOUBLE*) (stack - 1));
stack -= 2;
JDOUBLE b = *((JDOUBLE*) (stack - 1));
stack -= 2;
if (b > a) *(++stack) = 1;
if (b < a) *(++stack) = -1;
if (b == a) *(++stack) = 0;
debug("dcmp[l/g]: " << a << " " << b << " " << *stack << '\n');
break;
}
case dconst_0:
*(JDOUBLE*) (++stack) = (JDOUBLE) 0;
stack++;
break;
case dconst_1:
*(JDOUBLE*) (++stack) = (JDOUBLE) 1;
stack++;
break;
case ddiv:
{
double a = *((JDOUBLE*) (stack - 1));
stack -= 2;
*(JDOUBLE*) (stack - 1) = *(JDOUBLE*) (stack - 1) / a;
break;
}
case dload:
*(++stack) = locals[bc[pc]];
*(++stack) = locals[bc[pc++]];
break;
case dload_0:
*(++stack) = locals[0];
*(++stack) = locals[1];
break;
case dload_1:
*(++stack) = locals[1];
*(++stack) = locals[2];
break;
case dload_2:
*(++stack) = locals[2];
*(++stack) = locals[3];
break;
case dload_3:
*(++stack) = locals[3];
*(++stack) = locals[4];
break;
case dmul:
{
double a = *((JDOUBLE*) (stack - 1));
stack -= 2;
*(JDOUBLE*) (stack - 1) = *(JDOUBLE*) (stack - 1) * a;
break;
}
case dneg:
*(JDOUBLE*) (stack - 1) = -(*(JDOUBLE*) (stack - 1));
break;
case drem:
{
double a = *((JDOUBLE*) (stack - 1));
stack -= 2;
*(JDOUBLE*) (stack - 1) = fmod(*(JDOUBLE*) (stack - 1), a);
break;
}
case dreturn:
{
Variable result;
result.d = *(JDOUBLE*) (stack - 1);
return result;
}
case dstore:
locals[bc[pc]] = *(stack - 1);
locals[bc[pc++]] = *(stack);
stack -= 2;
break;
case dstore_0:
locals[0] = *(stack - 1);
locals[1] = *(stack);
stack -= 2;
break;
case dstore_1:
locals[1] = *(stack - 1);
locals[2] = *(stack);
stack -= 2;
break;
;
case dstore_2:
locals[2] = *(stack - 1);
locals[3] = *(stack);
stack -= 2;
break;
case dstore_3:
locals[3] = *(stack - 1);
locals[4] = *(stack);
stack -= 2;
break;
case dsub:
{
double a = *((JDOUBLE*) (stack - 1));
stack -= 2;
*(JDOUBLE*) (stack - 1) = *(JDOUBLE*) (stack - 1) - a;
break;
}
case dup:
{
int temp = *stack;
*(++stack) = temp;
break;
}
case dup_x1:
{
int temp = *stack;
int last = *(--stack);
*stack = temp;
*(++stack) = last;
*(++stack) = temp;
break;
}
case dup_x2:
{
int temp = *stack;
int last1 = *(--stack);
int last2 = *(--stack);
*stack = temp;
*(++stack) = last2;
*(++stack) = last1;
*(++stack) = temp;
break;
}
case dup2: //assumes being used on cat2
{
int h = *(stack - 1);
int l = *(stack);
*(++stack) = h;
*(++stack) = l;
break;
}
case dup2_x1: //assumes being used on cat2
{
int h = *(stack - 1);
int l = *(stack);
int last = *(stack - 2);
stack -= 3;
*(++stack) = h;
*(++stack) = l;
*(++stack) = last;
*(++stack) = h;
*(++stack) = l;
break;
break;
}
case dup2_x2: //assumes being used on cat2
{
int h = *(stack - 1);
int l = *(stack);
int lasth = *(stack - 2);
int lastl = *(stack - 3);
stack -= 4;
*(++stack) = h;
*(++stack) = l;
*(++stack) = lastl;
*(++stack) = lasth;
*(++stack) = h;
*(++stack) = l;
break;
}
case f2d:
*(JDOUBLE*) stack = (JDOUBLE) *(JFLOAT*) stack;
stack++;
break;
case f2i:
*(JINT*) stack = (JINT) *(JFLOAT*) stack;
break;
case f2l:
*(JLONG*) stack = (JLONG) *(JFLOAT*) stack;
stack++;
break;
case fadd:
{
JFLOAT a = *(JFLOAT*) (stack--);
debug("fadd: " << a << '+');
debug(*(JFLOAT*) stack << '=');
*(JFLOAT*) stack = *(JFLOAT*) stack + a;
debug(*(JFLOAT*) stack << '\n');
break;
}
case faload:
{
int pos = *(stack--);
JFLOATARRAY array = (JFLOATARRAY) * stack;
if (!array) {
JNIEnv* env = thread->getEnv();
debug("faload null\n");
jclass clazz = env->FindClass("java/lang/NullPointerException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "()V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
if (pos < 0 || pos >= array->size) {
JNIEnv* env = thread->getEnv();
jclass clazz = env->FindClass("java/lang/ArrayIndexOutOfBoundsException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "(I)V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id, pos);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
*(JFLOAT*) stack = array->array[pos];
break;
}
case fastore:
{
JFLOAT f = *(stack--);
int pos = *(stack--);
JFLOATARRAY array = (JFLOATARRAY) *(stack--);
if (!array) {
JNIEnv* env = thread->getEnv();
debug("fastore null\n");
jclass clazz = env->FindClass("java/lang/NullPointerException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "()V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
if (pos < 0 || pos >= array->size) {
JNIEnv* env = thread->getEnv();
jclass clazz = env->FindClass("java/lang/ArrayIndexOutOfBoundsException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "(I)V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id, pos);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
array->array[pos] = f;
break;
}
case fcmpl:
case fcmpg:
{
JFLOAT a = *(JFLOAT*) (stack--);
JFLOAT b = *(JFLOAT*) (stack);
if (b > a) *(stack) = 1;
if (b < a) *(stack) = -1;
if (b == a) *(stack) = 0;
debug("fcmp[l/g]: " << a << " " << b << " " << *stack << '\n');
break;
}
case fconst_0:
*(++stack) = 0;
break;
case fconst_1:
*(++stack) = 1;
break;
case fconst_2:
*(++stack) = 2;
break;
case fdiv:
{
JFLOAT a = *(JFLOAT*) (stack--);
*(JFLOAT*) stack = *(JFLOAT*) stack / a;
break;
}
case fload:
*(++stack) = locals[bc[pc++]];
debug("fload: " << *(JFLOAT*) stack << '\n');
break;
case fload_0:
*(++stack) = locals[0];
break;
case fload_1:
*(++stack) = locals[1];
break;
case fload_2:
*(++stack) = locals[2];
break;
case fload_3:
*(++stack) = locals[3];
break;
case fmul:
{
float a = *(JFLOAT*) (stack--);
*(JFLOAT*) stack = *(JFLOAT*) stack * a;
break;
}
case fneg:
*stack = -(*stack);
break;
case frem:
{
JFLOAT a = *(stack--);
*(JFLOAT*) stack = fmod(*(JFLOAT*) stack, a);
break;
}
case freturn:
{
Variable result;
result.f = *(JFLOAT*) stack;
return result;
}
case fstore:
locals[bc[pc++]] = *stack;
debug("fstore: " << *(JFLOAT*) stack << '\n');
stack--;
break;
case fstore_0:
locals[0] = *stack;
stack--;
break;
case fstore_1:
locals[1] = *stack;
stack--;
break;
case fstore_2:
locals[2] = *stack;
stack--;
break;
case fstore_3:
locals[3] = *stack;
stack--;
break;
case fsub:
{
JFLOAT a = *(JFLOAT*) (stack--);
*(JFLOAT*) stack = *(JFLOAT*) stack - a;
break;
}
case getfield:
{
unsigned short int index;
U2(index, bc, pc);
FieldMethodInfo *info = (FieldMethodInfo*) cp[index];
ClassInfo *cinfo = (ClassInfo*) cp[info->classIndex];
JCLASS type;
if (cinfo->classRef == 0) {
std::string name(((UTF8Info*) cp[cinfo->nameIndex])->bytes);
type = loader->load((char*) name.c_str(), this);
if (thread->nativeException) goto EXCEPTION_HANDLER;
cinfo->classRef = type;
cinfo->name = name;
} else {
type = cinfo->classRef;
}
FieldInfo *field;
if (info->fieldRef == 0) {
if (info->name.size() < 1)
info->name.append(((UTF8Info*) cp[((NameAndTypeInfo*) cp[info->nameAndTypeIndex])->nameIndex])->bytes);
field = 0;
for (JCLASS find = type; find != 0 && field == 0; find = find->superClass)
if (find->fieldMap.find(info->name) != find->fieldMap.end())
field = find->fieldMap[info->name];
info->fieldRef = field;
} else {
field = info->fieldRef;
}
debug("getfield: " << field->descriptor << " " << field->name << '\n');
if (!(*(JOBJECT*) stack)) {
JNIEnv* env = thread->getEnv();
debug("getfield null\n");
jclass clazz = env->FindClass("java/lang/NullPointerException");
if (thread->nativeException) goto EXCEPTION_HANDLER;
jmethodID id = env->GetMethodID(clazz, "<init>", "()V");
JOBJECT obj = (JOBJECT) env->NewObject(clazz, id);
exception = new Exception(cur, meth, obj);
gc->incrPersist(exception->cause);
goto EXCEPTION_HANDLER;
}
switch (field->descriptor.c_str()[0]) {
case 'D':
case 'J':
*(JLONG*) stack = (*(JOBJECT*) stack)->fields[field->index].j;
stack++;
break;
case 'L':
case '[':
*stack = (*(JOBJECT*) stack)->fields[field->index].i;
localgc.addLocal(*(JOBJECT*) stack);
break;
default:
*stack = (*(JOBJECT*) stack)->fields[field->index].i;
}
break;
}
case getstatic:
{
unsigned short int index;
U2(index, bc, pc);
FieldMethodInfo *info = (FieldMethodInfo*) cp[index];
FieldInfo *field;
if (info->fieldRef == 0) {
ClassInfo *cinfo = (ClassInfo*) cp[info->classIndex];
JCLASS type;
if (cinfo->classRef == 0) {
std::string name(((UTF8Info*) cp[cinfo->nameIndex])->bytes);
type = loader->load((char*) name.c_str(), this);
if (thread->nativeException) goto EXCEPTION_HANDLER;
cinfo->classRef = type;
cinfo->name = name;
} else {
type = cinfo->classRef;
}
if (info->name.size() < 1)
info->name.append(((UTF8Info*) cp[((NameAndTypeInfo*) cp[info->nameAndTypeIndex])->nameIndex])->bytes);
field = 0;
for (JCLASS find = type; find != 0 && field == 0; find = find->superClass)
if (find->fieldMap.find(info->name) != find->fieldMap.end())
field = find->fieldMap[info->name];
info->fieldRef = field;
} else {
field = info->fieldRef;
}
debug("getstatic: " << field->descriptor << " " << field->name << '\n');
switch (field->descriptor.c_str()[0]) {
case 'D':
case 'J':
*(JLONG*) (stack + 1) = field->staticValue.j;