Code cleanup

This commit is contained in:
VegOwOtenks 2024-08-30 15:33:54 +02:00
parent c1eca63f5a
commit 30fe5036d4
7 changed files with 691 additions and 158 deletions

View file

@ -1,5 +1,4 @@
use core::fmt::Debug;
use core::fmt;
pub struct Bytecode {
pub code: Box<[u8]>
@ -14,20 +13,57 @@ impl Bytecode {
let opcode = self.code[i];
let (instruction, offset) = match opcode {
0x00 => (Instruction::NoOperation(), 1),
0x01 => (Instruction::StoreIntoIntArray(), 1),
0x02 => (Instruction::PushConstIntM1(), 1),
0x03 => (Instruction::PushConstInt0(), 1),
0x04 => (Instruction::PushConstInt1(), 1),
0x05 => (Instruction::PushConstInt2(), 1),
0x06 => (Instruction::PushConstInt3(), 1),
0x07 => (Instruction::PushConstInt4(), 1),
0x08 => (Instruction::PushConstInt5(), 1),
0x0E => (Instruction::PushConstDouble0(), 1),
0x0F => (Instruction::PushConstDouble1(), 1),
0x11 => (Instruction::LoadShortImmediate((self.code[i+1] as u16) << 8 | self.code[i+2] as u16), 3),
0x12 => (Instruction::LoadConstant(self.code[i+1]), 2),
0x14 => (Instruction::LoadConstant64((self.code[i+1] as u16) << 8 | self.code[i+2] as u16), 3),
0x26 => (Instruction::LoadDouble0(), 1),
0x27 => (Instruction::LoadDouble1(), 1),
0x28 => (Instruction::LoadDouble2(), 1),
0x29 => (Instruction::LoadDouble3(), 1),
0x2A => (Instruction::LoadReference0(), 1),
0x2B => (Instruction::LoadReference1(), 1),
0x2C => (Instruction::LoadReference2(), 1),
0x2D => (Instruction::LoadReference3(), 1),
0x4B => (Instruction::StoreReference0(), 1),
0x4C => (Instruction::StoreReference1(), 1),
0x4D => (Instruction::StoreReference2(), 1),
0x4E => (Instruction::StoreReference3(), 1),
0x57 => (Instruction::Pop(), 1),
0x59 => (Instruction::Duplicate(), 1),
0x6D => (Instruction::DivideLong(), 1),
0x7A => (Instruction::ShiftIntRight(), 1),
0x80 => (Instruction::OrInt(), 1),
0xAC => (Instruction::ReturnInt(), 1),
0xB0 => (Instruction::ReturnReference(), 1),
0xB1 => (Instruction::ReturnVoid(), 1),
0xB2 => (Instruction::GetStatic(self.code[i+1], self.code[i+2]), 3),
0xB4 => (Instruction::GetField(self.code[i+1], self.code[i+2]), 3),
0xB5 => (Instruction::PutField(self.code[i+1], self.code[i+2]), 3),
0xB6 => (Instruction::InvokeVirtual(self.code[i+1], self.code[i+2]), 3),
0xB7 => (Instruction::InvokeSpecial(self.code[i+1], self.code[i+2]), 3),
0xBB => (Instruction::NewObject(self.code[i+1], self.code[i+2]), 3),
0xB2 => (Instruction::GetStatic((self.code[i+1] as u16) << 8 | self.code[i+2] as u16), 3),
0xB3 => (Instruction::PutStatic((self.code[i+1] as u16) << 8 | self.code[i+2] as u16), 3),
0xB4 => (Instruction::GetField((self.code[i+1] as u16) << 8 | self.code[i+2] as u16), 3),
0xB5 => (Instruction::PutField((self.code[i+1] as u16) << 8 | self.code[i+2] as u16), 3),
0xB6 => (Instruction::InvokeVirtual((self.code[i+1] as u16) << 8 | self.code[i+2] as u16), 3),
0xB7 => (Instruction::InvokeSpecial((self.code[i+1] as u16) << 8 | self.code[i+2] as u16), 3),
0xBA => (Instruction::InvokeDynamic((self.code[i+1] as u16) << 8 | self.code[i+2] as u16, (self.code[i+3] as u16) << 8 | self.code[i+4] as u16), 5),
0xBB => (Instruction::NewObject((self.code[i+1] as u16) << 8 | self.code[i+2] as u16), 3),
_ => (Instruction::Unknown(opcode), 1)
};
@ -50,19 +86,56 @@ impl Debug for Bytecode {
#[derive(Debug)]
#[repr(u8)]
pub enum Instruction {
LoadConstant(u8) = 0x12, // Push from constant pool
LoadReference0() = 0x2A, // Load local variable reference onto stack
LoadReference1() = 0x2B, // Load local variable reference onto stack
LoadReference2() = 0x2C, // Load local variable reference onto stack
LoadReference3() = 0x2D, // Load local variable reference onto stack
Duplicate() = 0x59, // duplicate top stack value
ReturnReference() = 0xB0, // return top-ref from current function
ReturnVoid() = 0xB1, // return void from function
GetStatic(u8, u8) = 0xB2, // get static field from class
GetField(u8, u8) = 0xB4, // get field from class
PutField(u8, u8) = 0xB5, // set field to a value
InvokeVirtual(u8, u8) = 0xB6, // invoke function on a class
InvokeSpecial(u8, u8) = 0xB7, // invoke instance method
NewObject(u8, u8) = 0xBB, // Create a new object from a constant-pool reference
NoOperation() = 0x00, // No-Operation
StoreIntoIntArray() = 0x01, // ..., arrayref, index, value
PushConstIntM1() = 0x02, // Push -1
PushConstInt0() = 0x03, // Push 0
PushConstInt1() = 0x04, // Push 1
PushConstInt2() = 0x05, // Push 2
PushConstInt3() = 0x06, // Push 3
PushConstInt4() = 0x07, // Push 4
PushConstInt5() = 0x08, // Push 5
PushConstDouble0() = 0x0E, // Push 0.0
PushConstDouble1() = 0x0F, // Push 1.0
LoadShortImmediate(u16) = 0x11, // push immediate short
LoadConstant(u8) = 0x12, // Push from constant pool
LoadConstant64(u16) = 0x14, // Push Long or Double from constant pool
LoadDouble0() = 0x26, // Load local double variable reference onto stack
LoadDouble1() = 0x27, // Load local double variable reference onto stack
LoadDouble2() = 0x28, // Load local double variable reference onto stack
LoadDouble3() = 0x29, // Load local double variable reference onto stack
LoadReference0() = 0x2A, // Load local reference variable reference onto stack
LoadReference1() = 0x2B, // Load local reference variable reference onto stack
LoadReference2() = 0x2C, // Load local reference variable reference onto stack
LoadReference3() = 0x2D, // Load local reference variable reference onto stack
StoreReference0() = 0x4B, // store reference into local variable
StoreReference1() = 0x4C, // store reference into local variable
StoreReference2() = 0x4D, // store reference into local variable
StoreReference3() = 0x4E, // store reference into local variable
Pop() = 0x57, // Pop top stack value
Duplicate() = 0x59, // duplicate top stack value
DivideLong() = 0x6D, // long division
ShiftIntRight() = 0x7a, // shift int
OrInt() = 0x80, // value, value => or
ReturnInt() = 0xAC, // return integer from function
ReturnReference() = 0xB0, // return top-ref from current function
ReturnVoid() = 0xB1, // return void from function
GetStatic(u16) = 0xB2, // get static field from class
PutStatic(u16) = 0xB3, // set static field on class
GetField(u16) = 0xB4, // get field from class
PutField(u16) = 0xB5, // set field to a value
InvokeVirtual(u16) = 0xB6, // invoke function on a class
InvokeSpecial(u16) = 0xB7, // invoke instance method
InvokeDynamic(u16, u16) = 0xBA, // invoke dynamic function
NewObject(u16) = 0xBB, // Create a new object from a constant-pool reference
Unknown(u8),
}