@@ -11,80 +11,97 @@ public class Evaluator {
11
11
private int instructionPointer ;
12
12
13
13
public WasmValue evaluate (byte [] opcodes , Type [] parameterTypes , Object [] args , Type returnType ) {
14
+ System .out .println ("New invocation" );
14
15
instructionPointer = 0 ;
16
+ var depth = 0 ;
15
17
WasmValue [] locals = toLocals (parameterTypes , args );
16
18
Stack <WasmValue > stack = new Stack <>();
17
19
execution :
18
20
while (instructionPointer < opcodes .length ) {
19
21
var opcode = opcodes [instructionPointer ++];
22
+ System .out .printf ("Opcode 0x%02X " , opcode );
20
23
21
24
22
25
switch (opcode ) {
23
26
// block
24
27
case 0x02 : {
28
+ System .out .println ("block" );
29
+ depth ++;
25
30
var blockType = opcodes [instructionPointer ++];
26
31
}
27
32
break ;
28
33
29
34
// end
30
35
case 0x0B :
36
+ System .out .println ("end" );
31
37
break ;
32
38
// br_if
33
39
case 0x0D : {
40
+ System .out .println ("br_if" );
34
41
var val = stack .pop ();
35
42
if (!val .type ().equals (Type .I32 ))
36
43
throw new WasmRuntimeException ("0x0D:br_if expects I32 in the top of the stack but got " + val .type ());
37
44
var label = readUnsignedLeb128 (opcodes );
38
45
if (val .value ().equals (1 )) {
39
- instructionPointer = label ;
46
+ while (depth != label ){
47
+ while (0x0B != opcodes [instructionPointer ++]);
48
+ depth --;
49
+ }
40
50
}
41
51
break ;
42
52
}
43
53
// return
44
54
case 0x0F : {
55
+ System .out .println ("return" );
45
56
break execution ;
46
57
}
47
58
48
59
// local.get
49
60
case 0x20 :
61
+ System .out .println ("local.get" );
50
62
var local = readUnsignedLeb128 (opcodes );
51
63
stack .push (locals [local ]);
52
64
break ;
53
65
// i32.const
54
66
case 0x41 : {
67
+ System .out .println ("i32.const" );
55
68
var val = readUnsignedLeb128 (opcodes );
56
69
var currentValue = new WasmValue <>(Type .I32 , val );
57
70
stack .push (currentValue );
58
71
}
59
72
break ;
60
73
// i32.lt_s
61
74
case 0x48 : {
62
- WasmValue < Integer > a = stack . pop ( );
75
+ System . out . println ( "i32.lt_s" );
63
76
WasmValue <Integer > b = stack .pop ();
77
+ WasmValue <Integer > a = stack .pop ();
64
78
Integer d = a .value () < b .value () ? 1 : 0 ;
65
79
stack .push (new WasmValue (Type .I32 , d ));
66
80
}
67
81
break ;
68
82
// i32.add
69
83
case 0x6A : {
70
- WasmValue < Integer > a = stack . pop ( );
84
+ System . out . println ( "i32.add" );
71
85
WasmValue <Integer > b = stack .pop ();
86
+ WasmValue <Integer > a = stack .pop ();
72
87
Integer d = a .value () + b .value ();
73
88
stack .push (new WasmValue (Type .I32 , d ));
74
89
break ;
75
90
}
76
91
// i32.mul
77
92
case 0x6C : {
78
- WasmValue < Integer > a = stack . pop ( );
93
+ System . out . println ( "i32.mul" );
79
94
WasmValue <Integer > b = stack .pop ();
95
+ WasmValue <Integer > a = stack .pop ();
80
96
Integer d = a .value () * b .value ();
81
97
stack .push (new WasmValue (Type .I32 , d ));
82
98
break ;
83
99
}
84
100
// i32.div_u
85
101
case 0x6E : {
86
- WasmValue < Integer > a = stack . pop ( );
102
+ System . out . println ( "i32.div_u" );
87
103
WasmValue <Integer > b = stack .pop ();
104
+ WasmValue <Integer > a = stack .pop ();
88
105
Integer d = Integer .divideUnsigned (a .value (), b .value ());
89
106
stack .push (new WasmValue (Type .I32 , d ));
90
107
break ;
0 commit comments