-
Notifications
You must be signed in to change notification settings - Fork 0
/
opcode.c
4546 lines (3865 loc) · 144 KB
/
opcode.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#include "frame_stack.h"
#include "structs.h"
#include "jvm.h"
#include "loader.h"
#include "linker.h"
#include "initializer.h"
extern frame_stack_t *jvm_stack;
extern pc_t jvm_pc;
#define MAX_DIMENSION 65000
/** \addtogroup Opcodes
* @{
*/
/**
* @brief Retorna uma cópia do operando passado
*
* @param any_type_t operando
* @return Cópia do operando
*/
any_type_t* copyAnyType(any_type_t *operando){
any_type_t *copy = (any_type_t *) malloc(sizeof(any_type_t));
copy->tag = operando->tag;
if(copy->tag == PRIMITIVE){
copy->val.primitive_val.tag = operando->val.primitive_val.tag;
switch(copy->val.primitive_val.tag){
case BYTE:
copy->val.primitive_val.val.val8 = operando->val.primitive_val.val.val8;
break;
case SHORT:
copy->val.primitive_val.val.val16 = operando->val.primitive_val.val.val16;
break;
case INT:
copy->val.primitive_val.val.val32 = operando->val.primitive_val.val.val32;
break;
case LONG:
copy->val.primitive_val.val.val64 = operando->val.primitive_val.val.val64;
break;
case CHAR:
copy->val.primitive_val.val.val_char = operando->val.primitive_val.val.val_char;
break;
case BOOLEAN:
copy->val.primitive_val.val.val_boolean = operando->val.primitive_val.val.val_boolean;
break;
case FLOAT:
copy->val.primitive_val.val.val_float = operando->val.primitive_val.val.val_float;
break;
case DOUBLE:
copy->val.primitive_val.val.val_double = operando->val.primitive_val.val.val_double;
break;
case RETURN_ADDRESS:
copy->val.primitive_val.val.val_return_addr = operando->val.primitive_val.val.val_return_addr;
break;
}
}else{
copy->val.reference_val.tag = operando->val.reference_val.tag;
switch(copy->val.reference_val.tag){
case ARRAY:
copy->val.reference_val.val.array = operando->val.reference_val.val.array;
break;
case OBJECT:
copy->val.reference_val.val.object = operando->val.reference_val.val.object;
break;
case NULL_REFERENCE:
copy->val.reference_val.val.val_null = operando->val.reference_val.val.val_null;
break;
}
}
return operando;
}
/**
* @brief Retorna o número de bytes com operandos do opcode que começa em code[index]
*
* @param Array dos opcode
* @param Index do opcode
* @return Número de bytes com operandos
*/
int getNumberOfOpcodeOperandsInBytes(u1* code, u2 index) {
int counter = 0;
int padding = 0;
u4 low = 0;
u4 high = 0;
u4 npairs = 0;
u1 byte1 = 0;
u1 byte2 = 0;
u1 byte3 = 0;
u1 byte4 = 0;
switch(code[index]) {
case 0xb9:
case 0xba:
case 0xc8:
case 0xc9:
return 4;
case 0xaa: //tableswitch is special
counter = 1;
padding = 4 - (index % 4) - 1;
counter += padding; // 1 byte do opcode + allignment bytes
counter += 4; // jump default
byte1 = code[index + counter];
byte2 = code[index + counter + 1];
byte3 = code[index + counter + 2];
byte4 = code[index + counter + 3];
low = (byte1 << 24) | (byte2 << 16) | (byte3 << 8) | byte4;
counter += 4; // count low
byte1 = code[index + counter];
byte2 = code[index + counter + 1];
byte3 = code[index + counter + 2];
byte4 = code[index + counter + 3];
high = (byte1 << 24) | (byte2 << 16) | (byte3 << 8) | byte4;
counter += 4; // count high
counter += (4 * (high - low + 1)); // count the x offsets
return counter;
case 0xab: //lookupswitch is special
counter = 1;
padding = 4 - (index % 4) - 1;
counter += padding; // 1 byte do opcode + allignment bytes
counter += 4; //ignore default_offset
byte1 = code[index + counter];
byte2 = code[index + counter + 1];
byte3 = code[index + counter + 2];
byte4 = code[index + counter + 3];
npairs = (byte1 << 24) | (byte2 << 16) | (byte3 << 8) | byte4;
counter += 4; // count the npairs
counter += (8 * npairs); // count all the key:offset pairs
return counter;
case 0xc5:
return 3;
case 0xc4: //wide is special
if (code[index + 1] == 0x84) {
// forma com iinc
return 5;
} else {
return 3;
}
case 0xbd:
case 0xc0:
case 0xc1:
case 0xb7:
case 0xb8:
case 0xb6:
case 0x13:
case 0x14:
case 0xbb:
case 0xb5:
case 0xb3:
case 0xb4:
case 0xb2:
case 0x84:
case 0x11:
case 0xa7:
case 0xa5:
case 0xa6:
case 0x9f:
case 0xa2:
case 0xa3:
case 0xa4:
case 0xa1:
case 0xa0:
case 0x99:
case 0x9c:
case 0x9d:
case 0x9e:
case 0x9b:
case 0x9a:
case 0xc7:
case 0xc6:
case 0xa8:
return 2;
case 0x19:
case 0x3a:
case 0x18:
case 0x39:
case 0x17:
case 0x38:
case 0x15:
case 0x36:
case 0x12:
case 0x16:
case 0x37:
case 0xa9:
case 0x10:
case 0xbc:
return 1;
default:
return 0;
}
return 0;
}
/**
* @brief Avança jvm_pc.code_pc para o próximo opcode
*
* @see getCodeAttribute, getNumberOfOpcodeOperandsInBytes
*/
void goToNextOpcode() {
if (jvm_pc.jumped) {
jvm_pc.jumped = 0;
return;
}
jvm_pc.jumped = 0;
code_attribute_t* code_attribute = getCodeAttribute(jvm_pc.currentClass, jvm_pc.method);
jvm_pc.code_pc += getNumberOfOpcodeOperandsInBytes(code_attribute->code, jvm_pc.code_pc) + 1;
}
/**
* @brief push a null reference onto the stack
*
*/
void aconst_null(){
DEBUG_PRINT("got into aconst_null\n");
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = REFERENCE;
operand->val.reference_val.tag = NULL_REFERENCE;
operand->val.reference_val.val.val_null = NULL;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load the int value -1 onto the stack
*
*/
void iconst_m1(){
DEBUG_PRINT("got into iconst_m1\n");
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = INT;
operand->val.primitive_val.val.val32 = -1;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load the int value 0 onto the stack
*
*/
void iconst_0(){
DEBUG_PRINT("got into iconst_0\n");
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = INT;
operand->val.primitive_val.val.val32 = 0;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load the int value 1 onto the stack
*
*/
void iconst_1(){
DEBUG_PRINT("got into iconst_1\n");
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = INT;
operand->val.primitive_val.val.val32 = 1;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load the int value 2 onto the stack
*
*/
void iconst_2(){
DEBUG_PRINT("got into iconst_2\n");
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = INT;
operand->val.primitive_val.val.val32 = 2;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load the int value 3 onto the stack
*
*/
void iconst_3(){
DEBUG_PRINT("got into iconst_3\n");
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = INT;
operand->val.primitive_val.val.val32 = 3;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load the int value 4 onto the stack
*
*/
void iconst_4(){
DEBUG_PRINT("got into iconst_4\n");
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = INT;
operand->val.primitive_val.val.val32 = 4;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load the int value 5 onto the stack
*
*/
void iconst_5(){
DEBUG_PRINT("got into iconst_5\n");
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = INT;
operand->val.primitive_val.val.val32 = 5;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load the long value 0 onto the stack
*
*/
void lconst_0(){
DEBUG_PRINT("got into lconst_0\n");
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = LONG;
operand->val.primitive_val.val.val64 = 0;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load the long value 1 onto the stack
*
*/
void lconst_1(){
DEBUG_PRINT("got into lconst_1\n");
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = LONG;
operand->val.primitive_val.val.val64 = 1;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load the float value 0 onto the stack
*
*/
void fconst_0(){
DEBUG_PRINT("got into fconst_0\n");
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = FLOAT;
operand->val.primitive_val.val.val_float = 0;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load the long value 1 onto the stack
*
*/
void fconst_1(){
DEBUG_PRINT("got into fconst_1\n");
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = FLOAT;
operand->val.primitive_val.val.val_float = 1;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load the long value 2 onto the stack
*
*/
void fconst_2(){
DEBUG_PRINT("got into fconst_2\n");
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = FLOAT;
operand->val.primitive_val.val.val_float = 2;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load the double value 0 onto the stack
*
*/
void dconst_0(){
DEBUG_PRINT("got into dconst_0\n");
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = DOUBLE;
operand->val.primitive_val.val.val_double = 0;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load the long value 1 onto the stack
*
*/
void dconst_1(){
DEBUG_PRINT("got into dconst_1\n");
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = DOUBLE;
operand->val.primitive_val.val.val_double = 1;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief push a byte onto the stack as an integer value
*
*/
void bipush(){
DEBUG_PRINT("got into bipush\n");
code_attribute_t *code_attribute = getCodeAttribute(jvm_pc.currentClass, jvm_pc.method);
u1 b = code_attribute->code[jvm_pc.code_pc+1];
int8_t value = b;
int32_t value2 = value;
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = INT;
operand->val.primitive_val.val.val32 = value2;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief push a short onto the stack
*
*/
void sipush(){
DEBUG_PRINT("got into sipush\n");
code_attribute_t *code_attribute = getCodeAttribute(jvm_pc.currentClass, jvm_pc.method);
u1 b1 = code_attribute->code[jvm_pc.code_pc+1];
u1 b2 = code_attribute->code[jvm_pc.code_pc+2];
int16_t aux = (b1 << 8) | b2;
int32_t value = (int32_t) aux;
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = INT;
operand->val.primitive_val.val.val32 = value;
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief push a constant #index from a constant pool (String, int or float) onto the stack
*
*/
void ldc(){
DEBUG_PRINT("got into ldc\n");
code_attribute_t *code_attribute = getCodeAttribute(jvm_pc.currentClass, jvm_pc.method);
u1 b = code_attribute->code[jvm_pc.code_pc+1];
u4 bytes;
u2 bytes1;
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
switch(jvm_pc.currentClass->class_file.constant_pool[b].tag){
case CONSTANT_Integer:
bytes = jvm_pc.currentClass->class_file.constant_pool[b].info.Integer.bytes;
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = INT;
operand->val.primitive_val.val.val32 = bytes;
break;
case CONSTANT_Float:
bytes = jvm_pc.currentClass->class_file.constant_pool[b].info.Float.bytes;
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = FLOAT;
memmove(&(operand->val.primitive_val.val.val_float), &(bytes), sizeof(float));
break;
case CONSTANT_String:
bytes1 = jvm_pc.currentClass->class_file.constant_pool[b].info.String.string_index;
Utf8_info_t *Utf8_cod = &(jvm_pc.currentClass->class_file.constant_pool[bytes1].info.Utf8);
operand = utf8_to_array_reference(Utf8_cod);
break;
default:
printf("Erro \n");
break;
}
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief push a constant #index from a constant pool (String, int or float) onto the stack (wide index is constructed as indexbyte1 << 8 + indexbyte2)
*
*/
void ldc_w(){
DEBUG_PRINT("got into ldc_w\n");
code_attribute_t *code_attribute = getCodeAttribute(jvm_pc.currentClass, jvm_pc.method);
u1 b1 = code_attribute->code[jvm_pc.code_pc+1];
u1 b2 = code_attribute->code[jvm_pc.code_pc+2];
u2 b = (b1<<8)|b2;
u4 bytes;
u2 bytes1;
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
switch(jvm_pc.currentClass->class_file.constant_pool[b].tag){
case CONSTANT_Integer:
bytes = jvm_pc.currentClass->class_file.constant_pool[b].info.Integer.bytes;
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = INT;
operand->val.primitive_val.val.val32 = bytes;
break;
case CONSTANT_Float:
bytes = jvm_pc.currentClass->class_file.constant_pool[b].info.Float.bytes;
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = FLOAT;
operand->val.primitive_val.val.val_float = bytes;
break;
case CONSTANT_String:
bytes1 = jvm_pc.currentClass->class_file.constant_pool[b].info.String.string_index;
Utf8_info_t *Utf8_cod = &(jvm_pc.currentClass->class_file.constant_pool[bytes1].info.Utf8);
operand = utf8_to_array_reference(Utf8_cod);
break;
default:
printf("Erro \n");
break;
}
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief push a constant #index from a constant pool (double or long) onto the stack (wide index is constructed as indexbyte1 << 8 + indexbyte2)
*
*/
void ldc2_w(){
DEBUG_PRINT("got into ldc2_w\n");
code_attribute_t *code_attribute = getCodeAttribute(jvm_pc.currentClass, jvm_pc.method);
u1 b1 = code_attribute->code[jvm_pc.code_pc+1];
u1 b2 = code_attribute->code[jvm_pc.code_pc+2];
u2 b = (b1<<8)|b2;
uint64_t high_bytes;
uint64_t low_bytes;
any_type_t *operand = (any_type_t*) malloc(sizeof(any_type_t));
switch(jvm_pc.currentClass->class_file.constant_pool[b].tag){
case CONSTANT_Long:
high_bytes = jvm_pc.currentClass->class_file.constant_pool[b].info.Long.high_bytes;
low_bytes = jvm_pc.currentClass->class_file.constant_pool[b].info.Long.low_bytes;
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = LONG;
operand->val.primitive_val.val.val64 = (high_bytes<<32)|low_bytes;
break;
case CONSTANT_Double:
high_bytes = jvm_pc.currentClass->class_file.constant_pool[b].info.Long.high_bytes;
low_bytes = jvm_pc.currentClass->class_file.constant_pool[b].info.Long.low_bytes;
uint64_t double_bytes = (high_bytes<<32) | low_bytes;
operand->tag = PRIMITIVE;
operand->val.primitive_val.tag = DOUBLE;
memmove(&(operand->val.primitive_val.val.val_double), &(double_bytes), sizeof(double));
break;
default:
printf("Erro \n");
break;
}
frame_t* frame = peek_frame_stack(jvm_stack);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load an (int, long, float, double, reference) value from a local variable #index
*
*/
void tload(){
DEBUG_PRINT("got into tload\n");
any_type_t* operand = (any_type_t*) malloc(sizeof(any_type_t));
code_attribute_t *code_attribute = getCodeAttribute(jvm_pc.currentClass, jvm_pc.method);
u1 index = code_attribute->code[jvm_pc.code_pc+1];
frame_t *frame = peek_frame_stack(jvm_stack);
memmove(operand, &(frame->local_var.var[index]), sizeof(any_type_t));
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load an (int, long, float, double, reference) value from a local 0
*
*/
void tload_0(){
DEBUG_PRINT("got into tload_0\n");
any_type_t* operand = (any_type_t*) malloc(sizeof(any_type_t));
frame_t *frame = peek_frame_stack(jvm_stack);
memmove(operand, &(frame->local_var.var[0]), sizeof(any_type_t));
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load an (int, long, float, double, reference) value from a local 1
*
*/
void tload_1(){
DEBUG_PRINT("got into tload_1\n");
any_type_t* operand = (any_type_t*) malloc(sizeof(any_type_t));
frame_t *frame = peek_frame_stack(jvm_stack);
memmove(operand, &(frame->local_var.var[1]), sizeof(any_type_t));
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load an (int, long, float, double, reference) value from a local 2
*
*/
void tload_2(){
DEBUG_PRINT("got into tload_2\n");
any_type_t* operand = (any_type_t*) malloc(sizeof(any_type_t));
frame_t *frame = peek_frame_stack(jvm_stack);
memmove(operand, &(frame->local_var.var[2]), sizeof(any_type_t));
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load an (int, long, float, double, reference) value from a local 3
*
*/
void tload_3(){
DEBUG_PRINT("got into tload_3\n");
any_type_t* operand = (any_type_t*) malloc(sizeof(any_type_t));
frame_t *frame = peek_frame_stack(jvm_stack);
memmove(operand, &(frame->local_var.var[3]), sizeof(any_type_t));
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief load an int from an array (operand_stack: arrayref, index -> value)
*
*/
void taload(){
DEBUG_PRINT("got into taload\n");
any_type_t *index, *arrayref;
uint32_t int_index;
frame_t *frame = peek_frame_stack(jvm_stack);
any_type_t* operand = (any_type_t*) malloc(sizeof(any_type_t));
index = pop_operand_stack(&(frame->operand_stack));
arrayref = pop_operand_stack(&(frame->operand_stack));
int_index = index->val.primitive_val.val.val32;
memmove(operand, &(arrayref->val.reference_val.val.array.components[int_index]), sizeof(any_type_t));
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief store (int, long, float, double, reference) value into variable #index
*
*/
void tstore(){
DEBUG_PRINT("got into tstore\n");
any_type_t *value;
code_attribute_t *code_attribute = getCodeAttribute(jvm_pc.currentClass, jvm_pc.method);
u1 index = code_attribute->code[jvm_pc.code_pc+1];
frame_t *frame = peek_frame_stack(jvm_stack);
value = pop_operand_stack(&(frame->operand_stack));
memmove(&(frame->local_var.var[index]), value, sizeof(any_type_t));
if(value->val.primitive_val.tag == LONG|| value->val.primitive_val.tag == DOUBLE)
memmove(&(frame->local_var.var[index + 1]), value, sizeof(any_type_t));
}
/**
* @brief store (int, long, float, double, reference) value into variable 0
*
*/
void tstore_0(){
DEBUG_PRINT("got into tstore_0\n");
any_type_t *value;
frame_t *frame = peek_frame_stack(jvm_stack);
value = pop_operand_stack(&(frame->operand_stack));
memmove(&(frame->local_var.var[0]), value, sizeof(any_type_t));
if(value->val.primitive_val.tag == LONG|| value->val.primitive_val.tag == DOUBLE)
memmove(&(frame->local_var.var[1]), value, sizeof(any_type_t));
}
/**
* @brief store (int, long, float, double, reference) value into variable 1
*
*/
void tstore_1(){
DEBUG_PRINT("got into tstore_1\n");
any_type_t *value;
frame_t *frame = peek_frame_stack(jvm_stack);
value = pop_operand_stack(&(frame->operand_stack));
memmove(&(frame->local_var.var[1]), value, sizeof(any_type_t));
if(value->val.primitive_val.tag == LONG|| value->val.primitive_val.tag == DOUBLE)
memmove(&(frame->local_var.var[2]), value, sizeof(any_type_t));
}
/**
* @brief store (int, long, float, double, reference) value into variable 2
*
*/
void tstore_2(){
DEBUG_PRINT("got into tstore_2\n");
any_type_t *value;
frame_t *frame = peek_frame_stack(jvm_stack);
value = pop_operand_stack(&(frame->operand_stack));
memmove(&(frame->local_var.var[2]), value, sizeof(any_type_t));
if(value->val.primitive_val.tag == LONG|| value->val.primitive_val.tag == DOUBLE)
memmove(&(frame->local_var.var[3]), value, sizeof(any_type_t));
}
/**
* @brief store (int, long, float, double, reference) value into variable 3
*
*/
void tstore_3(){
DEBUG_PRINT("got into tstore_3\n");
any_type_t *value;
frame_t *frame = peek_frame_stack(jvm_stack);
value = pop_operand_stack(&(frame->operand_stack));
memmove(&(frame->local_var.var[3]), value, sizeof(any_type_t));
if(value->val.primitive_val.tag == LONG|| value->val.primitive_val.tag == DOUBLE)
memmove(&(frame->local_var.var[4]), value, sizeof(any_type_t));
}
/**
* @brief store an (int, long, float, double, reference, byte, boolean, char short) into an array (operand_stack: arrayref, index, value ->)
*
*/
void tastore(){
DEBUG_PRINT("got into tastore\n");
any_type_t *index, *arrayref, *value;
uint32_t int_index;
frame_t *frame = peek_frame_stack(jvm_stack);
value = pop_operand_stack(&(frame->operand_stack));
index = pop_operand_stack(&(frame->operand_stack));
arrayref = pop_operand_stack(&(frame->operand_stack));
int_index = index->val.primitive_val.val.val32;
memmove(&(arrayref->val.reference_val.val.array.components[int_index]), value, sizeof(any_type_t));
}
/**
* @brief discard the top value on the stack (operand_stack: value ->)
*
*/
void pop(){
DEBUG_PRINT("got into pop\n");
frame_t *frame = peek_frame_stack(jvm_stack);
pop_operand_stack(&(frame->operand_stack));
}
/**
* @brief discard the top two values on the stack (or one value, if it is a double or long) (operand_stack: {value2, value1} ->)
*
*/
void pop2(){
DEBUG_PRINT("got into pop2\n");
frame_t *frame = peek_frame_stack(jvm_stack);
any_type_t *value = NULL;
value = pop_operand_stack(&(frame->operand_stack));
if(value->val.primitive_val.tag != LONG && value->val.primitive_val.tag != DOUBLE)
pop_operand_stack(&(frame->operand_stack));
}
/**
* @brief duplicate the value on top of the stack (operand_stack: value -> value, value)
*
*/
void dup(){
DEBUG_PRINT("got into dup\n");
any_type_t *operand = NULL;
frame_t *frame = peek_frame_stack(jvm_stack);
operand = pop_operand_stack(&(frame->operand_stack));
push_operand_stack(&(frame->operand_stack), operand);
push_operand_stack(&(frame->operand_stack), operand);
}
/**
* @brief insert a copy of the top value into the stack two values from the top. value1 and value2 must not be of the type double or long. (operand_stack: value2, value1 -> value1, value2, value1)
*
*/
void dup_x1(){
DEBUG_PRINT("got into dup_x1\n");
any_type_t *operand1 = NULL;
any_type_t *operand2 = NULL;
frame_t *frame = peek_frame_stack(jvm_stack);
operand1 = pop_operand_stack(&(frame->operand_stack));
operand2 = pop_operand_stack(&(frame->operand_stack));
if(isLongOrDouble(operand1) || isLongOrDouble(operand2)) {
printf("ERROR: dup_x1 com operando LONG / DOUBLE\n");
exit(1);
}
push_operand_stack(&(frame->operand_stack), operand1);
push_operand_stack(&(frame->operand_stack), operand2);
push_operand_stack(&(frame->operand_stack), operand1);
}
/**
* @brief insert a copy of the top value into the stack two (if value2 is double or long it takes up the entry of value3, too) or three values (if value2 is neither double nor long) from the top (operand_stack: value3, value2, value1 -> value1, value3, value2, value1)
*
*/
void dup_x2(){
DEBUG_PRINT("got into dup_x2\n");
any_type_t *operand1, *operand2, *operand3;
frame_t *frame = peek_frame_stack(jvm_stack);
operand1 = pop_operand_stack(&(frame->operand_stack));
operand2 = pop_operand_stack(&(frame->operand_stack));
if(isLongOrDouble(operand2))
operand3 = pop_operand_stack(&(frame->operand_stack));
push_operand_stack(&(frame->operand_stack), operand1);
if(isLongOrDouble(operand2))
push_operand_stack(&(frame->operand_stack), operand3);
push_operand_stack(&(frame->operand_stack), operand2);
push_operand_stack(&(frame->operand_stack), operand1);
}
/**
* @brief duplicate top two stack words (two values, if value1 is not double nor long; a single value, if value1 is double or long) (operand_stack: {value2, value1} -> {value2, value1}, {value2, value1})
*
*/
void dup2(){
DEBUG_PRINT("got into dup2\n");
any_type_t *operand1, *operand2;
frame_t *frame = peek_frame_stack(jvm_stack);
operand1 = pop_operand_stack(&(frame->operand_stack));
operand2 = pop_operand_stack(&(frame->operand_stack));
if(!isLongOrDouble(operand1))
push_operand_stack(&(frame->operand_stack), operand2);
push_operand_stack(&(frame->operand_stack), operand1);
if(!isLongOrDouble(operand1))
push_operand_stack(&(frame->operand_stack), operand2);
push_operand_stack(&(frame->operand_stack), operand1);
}
/**
* @brief duplicate two words and insert beneath third word (operand_stack: value3, {value2, value1} -> {value2, value1}, value3, {value2, value1})
*
*/
void dup2_x1(){
DEBUG_PRINT("got into dup2_x1\n");
any_type_t *operand1, *operand2, *operand3;
frame_t *frame = peek_frame_stack(jvm_stack);
operand1 = pop_operand_stack(&(frame->operand_stack));
operand2 = pop_operand_stack(&(frame->operand_stack));
if(!isLongOrDouble(operand1))
operand3 = pop_operand_stack(&(frame->operand_stack));
if(!isLongOrDouble(operand1))
push_operand_stack(&(frame->operand_stack), operand2);
push_operand_stack(&(frame->operand_stack), operand1);
if(!isLongOrDouble(operand1))
push_operand_stack(&(frame->operand_stack), operand3);
push_operand_stack(&(frame->operand_stack), operand2);
push_operand_stack(&(frame->operand_stack), operand1);
}
/**
* @brief duplicate two words and insert beneath fourth word (operand_stack: {value4, value3}, {value2, value1} -> {value2, value1}, {value4, value3}, {value2, value1})
*
*/
void dup2_x2(){
DEBUG_PRINT("got into dup2_x2\n");
any_type_t *operand1 = NULL;
any_type_t *operand2 = NULL;
any_type_t *operand3 = NULL;
any_type_t *operand4 = NULL;
frame_t *frame = peek_frame_stack(jvm_stack);
operand1 = pop_operand_stack(&(frame->operand_stack));
operand2 = pop_operand_stack(&(frame->operand_stack));
if(!isLongOrDouble(operand1) && isLongOrDouble(operand2))
operand3 = pop_operand_stack(&(frame->operand_stack));
if(!isLongOrDouble(operand1) && !isLongOrDouble(operand2))
operand4 = pop_operand_stack(&(frame->operand_stack));
if(!isLongOrDouble(operand1))
push_operand_stack(&(frame->operand_stack), operand2);
push_operand_stack(&(frame->operand_stack), operand1);
if(!isLongOrDouble(operand1) && !isLongOrDouble(operand2))
push_operand_stack(&(frame->operand_stack), operand4);
if(isLongOrDouble(operand1) && isLongOrDouble(operand2))
push_operand_stack(&(frame->operand_stack), operand3);
push_operand_stack(&(frame->operand_stack), operand2);
push_operand_stack(&(frame->operand_stack), operand1);
}
/**
* @brief swaps two top words on the stack (note that value1 and value2 must not be double or long)
*
*/
void swap(){
DEBUG_PRINT("got into swap\n");
any_type_t *operand1 = NULL;
any_type_t *operand2 = NULL;
frame_t *frame = peek_frame_stack(jvm_stack);
operand1 = pop_operand_stack(&(frame->operand_stack));
operand2 = pop_operand_stack(&(frame->operand_stack));
if(isLongOrDouble(operand1) || isLongOrDouble(operand2)) {
printf("ERROR: swap com operando LONG / DOUBLE\n");