Skip to content

Commit b661c16

Browse files
committed
Implement more bytecode instructions
1 parent 7d24d3a commit b661c16

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

classes/IntBox.class

296 Bytes
Binary file not shown.

classes/IntBox.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class IntBox {
2+
private int value;
3+
4+
public IntBox(int value) {
5+
this.value = value;
6+
}
7+
8+
public int getValue() {
9+
return this.value;
10+
}
11+
}

src/bytecode.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
const ILOAD_1: u8 = 27;
12
const ALOAD_0: u8 = 42;
3+
const IRETURN: u8 = 172;
24
const RETURN: u8 = 177;
5+
const GETFIELD: u8 = 180;
6+
const PUTFIELD: u8 = 181;
37
const INVOKESPECIAL: u8 = 183;
48

59
#[derive(Debug)]
610
pub enum Bytecode {
11+
Iload_1,
712
Aload_0,
13+
Ireturn,
814
Return,
15+
Getfield(u16),
16+
Putfield(u16),
917
Invokespecial(u16),
1018
}
1119

@@ -20,16 +28,44 @@ impl Bytecode {
2028
let instruction = bytes[i];
2129

2230
match instruction {
31+
ILOAD_1 => {
32+
bytecode.push((i, Iload_1));
33+
34+
i += 1;
35+
},
2336
ALOAD_0 => {
2437
bytecode.push((i, Aload_0));
2538

2639
i += 1;
2740
},
41+
IRETURN => {
42+
bytecode.push((i, Ireturn));
43+
44+
i += 1;
45+
},
2846
RETURN => {
2947
bytecode.push((i, Return));
3048

3149
i += 1;
3250
},
51+
GETFIELD => {
52+
let field = u16::from_be_bytes(
53+
[bytes[i + 1], bytes[i + 2]]
54+
);
55+
56+
bytecode.push((i, Getfield(field)));
57+
58+
i += 3;
59+
},
60+
PUTFIELD => {
61+
let field = u16::from_be_bytes(
62+
[bytes[i + 1], bytes[i + 2]]
63+
);
64+
65+
bytecode.push((i, Putfield(field)));
66+
67+
i += 3;
68+
},
3369
INVOKESPECIAL => {
3470
let method = u16::from_be_bytes(
3571
[bytes[i + 1], bytes[i + 2]]
@@ -52,9 +88,13 @@ impl ToString for Bytecode {
5288
use Bytecode::*;
5389

5490
match self {
91+
Iload_1 => "iload_1".to_string(),
5592
Aload_0 => "aload_0".to_string(),
93+
Ireturn => "ireturn".to_string(),
5694
Return => "return".to_string(),
57-
Invokespecial(method) => format!("invokespecial #{}", method),
95+
Getfield(field) => format!("{:13} #{}", "getfield", field),
96+
Putfield(field) => format!("{:13} #{}", "putfield", field),
97+
Invokespecial(method) => format!("{:13} #{}", "invokespecial", method),
5898
_ => panic!(),
5999
}
60100
}

0 commit comments

Comments
 (0)