2024-09-12 04:39:31 +00:00
|
|
|
use std::fmt::{self, Display, Formatter};
|
|
|
|
|
2024-09-10 22:19:59 +00:00
|
|
|
use crate::{Chunk, Span};
|
2024-09-10 03:24:22 +00:00
|
|
|
|
2024-09-12 03:07:20 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
pub struct Instruction {
|
2024-09-12 04:39:31 +00:00
|
|
|
pub operation: Operation,
|
2024-09-12 13:11:49 +00:00
|
|
|
pub destination: u8,
|
2024-09-12 04:39:31 +00:00
|
|
|
pub arguments: [u8; 2],
|
2024-09-10 03:24:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Instruction {
|
2024-09-12 09:08:55 +00:00
|
|
|
pub fn decode(bits: u32) -> Instruction {
|
|
|
|
let operation = Operation::from((bits >> 24) as u8);
|
|
|
|
let to_register = ((bits >> 16) & 0xff) as u8;
|
|
|
|
let arguments = [((bits >> 8) & 0xff) as u8, (bits & 0xff) as u8];
|
|
|
|
|
|
|
|
Instruction {
|
|
|
|
operation,
|
2024-09-12 13:11:49 +00:00
|
|
|
destination: to_register,
|
2024-09-12 09:08:55 +00:00
|
|
|
arguments,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn encode(&self) -> u32 {
|
2024-09-12 13:11:49 +00:00
|
|
|
let operation = self.operation as u8 as u32;
|
|
|
|
let to_register = self.destination as u32;
|
|
|
|
let arguments = (self.arguments[0] as u32) << 8 | (self.arguments[1] as u32);
|
2024-09-12 09:08:55 +00:00
|
|
|
|
|
|
|
operation << 24 | to_register << 16 | arguments
|
|
|
|
}
|
|
|
|
|
2024-09-12 03:07:20 +00:00
|
|
|
pub fn r#move(to_register: u8, from_register: u8) -> Instruction {
|
|
|
|
Instruction {
|
2024-09-12 04:39:31 +00:00
|
|
|
operation: Operation::Move,
|
2024-09-12 13:11:49 +00:00
|
|
|
destination: to_register,
|
2024-09-12 03:07:20 +00:00
|
|
|
arguments: [from_register, 0],
|
2024-09-10 03:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 03:07:20 +00:00
|
|
|
pub fn close(to_register: u8) -> Instruction {
|
|
|
|
Instruction {
|
2024-09-12 04:39:31 +00:00
|
|
|
operation: Operation::Close,
|
2024-09-12 13:11:49 +00:00
|
|
|
destination: to_register,
|
2024-09-12 03:07:20 +00:00
|
|
|
arguments: [0, 0],
|
|
|
|
}
|
|
|
|
}
|
2024-09-10 22:19:59 +00:00
|
|
|
|
2024-09-12 03:07:20 +00:00
|
|
|
pub fn load_constant(to_register: u8, constant_index: u16) -> Instruction {
|
|
|
|
Instruction {
|
2024-09-12 04:39:31 +00:00
|
|
|
operation: Operation::LoadConstant,
|
2024-09-12 13:11:49 +00:00
|
|
|
destination: to_register,
|
2024-09-12 03:07:20 +00:00
|
|
|
arguments: constant_index.to_le_bytes(),
|
|
|
|
}
|
|
|
|
}
|
2024-09-10 13:26:05 +00:00
|
|
|
|
2024-09-12 17:03:24 +00:00
|
|
|
pub fn declare_local(to_register: u8, variable_index: u16) -> Instruction {
|
2024-09-12 03:07:20 +00:00
|
|
|
Instruction {
|
2024-09-12 17:03:24 +00:00
|
|
|
operation: Operation::DeclareLocal,
|
2024-09-12 13:11:49 +00:00
|
|
|
destination: to_register,
|
2024-09-12 03:07:20 +00:00
|
|
|
arguments: variable_index.to_le_bytes(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 17:03:24 +00:00
|
|
|
pub fn get_local(to_register: u8, variable_index: u16) -> Instruction {
|
2024-09-12 03:07:20 +00:00
|
|
|
Instruction {
|
2024-09-12 17:03:24 +00:00
|
|
|
operation: Operation::GetLocal,
|
2024-09-12 13:11:49 +00:00
|
|
|
destination: to_register,
|
2024-09-12 03:07:20 +00:00
|
|
|
arguments: variable_index.to_le_bytes(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 13:11:49 +00:00
|
|
|
pub fn set_local(from_register: u8, variable_index: u16) -> Instruction {
|
2024-09-12 03:07:20 +00:00
|
|
|
Instruction {
|
2024-09-12 17:03:24 +00:00
|
|
|
operation: Operation::SetLocal,
|
2024-09-12 13:11:49 +00:00
|
|
|
destination: from_register,
|
2024-09-12 03:07:20 +00:00
|
|
|
arguments: variable_index.to_le_bytes(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add(to_register: u8, left_register: u8, right_register: u8) -> Instruction {
|
|
|
|
Instruction {
|
2024-09-12 04:39:31 +00:00
|
|
|
operation: Operation::Add,
|
2024-09-12 13:11:49 +00:00
|
|
|
destination: to_register,
|
2024-09-12 03:07:20 +00:00
|
|
|
arguments: [left_register, right_register],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn subtract(to_register: u8, left_register: u8, right_register: u8) -> Instruction {
|
|
|
|
Instruction {
|
2024-09-12 04:39:31 +00:00
|
|
|
operation: Operation::Subtract,
|
2024-09-12 13:11:49 +00:00
|
|
|
destination: to_register,
|
2024-09-12 03:07:20 +00:00
|
|
|
arguments: [left_register, right_register],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn multiply(to_register: u8, left_register: u8, right_register: u8) -> Instruction {
|
|
|
|
Instruction {
|
2024-09-12 04:39:31 +00:00
|
|
|
operation: Operation::Multiply,
|
2024-09-12 13:11:49 +00:00
|
|
|
destination: to_register,
|
2024-09-12 03:07:20 +00:00
|
|
|
arguments: [left_register, right_register],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn divide(to_register: u8, left_register: u8, right_register: u8) -> Instruction {
|
|
|
|
Instruction {
|
2024-09-12 04:39:31 +00:00
|
|
|
operation: Operation::Divide,
|
2024-09-12 13:11:49 +00:00
|
|
|
destination: to_register,
|
2024-09-12 03:07:20 +00:00
|
|
|
arguments: [left_register, right_register],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn negate(to_register: u8, from_register: u8) -> Instruction {
|
|
|
|
Instruction {
|
2024-09-12 04:39:31 +00:00
|
|
|
operation: Operation::Negate,
|
2024-09-12 13:11:49 +00:00
|
|
|
destination: to_register,
|
2024-09-12 03:07:20 +00:00
|
|
|
arguments: [from_register, 0],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn r#return() -> Instruction {
|
|
|
|
Instruction {
|
2024-09-12 04:39:31 +00:00
|
|
|
operation: Operation::Return,
|
2024-09-12 13:11:49 +00:00
|
|
|
destination: 0,
|
2024-09-12 03:07:20 +00:00
|
|
|
arguments: [0, 0],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 04:39:31 +00:00
|
|
|
pub fn disassemble(&self, chunk: &Chunk) -> String {
|
2024-09-12 17:03:24 +00:00
|
|
|
let mut disassembled = format!("{:16} ", self.operation.to_string());
|
|
|
|
|
|
|
|
if let Some(info) = self.disassembly_info(Some(chunk)) {
|
|
|
|
disassembled.push_str(&info);
|
|
|
|
}
|
|
|
|
|
|
|
|
disassembled
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn disassembly_info(&self, chunk: Option<&Chunk>) -> Option<String> {
|
|
|
|
let info = match self.operation {
|
2024-09-12 09:08:55 +00:00
|
|
|
Operation::Move => {
|
2024-09-12 17:03:24 +00:00
|
|
|
format!("R({}) R({})", self.destination, self.arguments[0])
|
2024-09-12 09:08:55 +00:00
|
|
|
}
|
2024-09-12 17:03:24 +00:00
|
|
|
Operation::Close => format!("R({})", self.destination),
|
2024-09-12 04:39:31 +00:00
|
|
|
Operation::LoadConstant => {
|
2024-09-12 13:11:49 +00:00
|
|
|
let constant_index = u16::from_le_bytes(self.arguments) as usize;
|
2024-09-12 03:07:20 +00:00
|
|
|
|
2024-09-12 17:03:24 +00:00
|
|
|
if let Some(chunk) = chunk {
|
|
|
|
match chunk.get_constant(constant_index, Span(0, 0)) {
|
|
|
|
Ok(value) => {
|
|
|
|
format!("R({}) = C({}) {}", self.destination, constant_index, value)
|
|
|
|
}
|
|
|
|
Err(error) => format!(
|
|
|
|
"R({}) = C({}) {:?}",
|
|
|
|
self.destination, constant_index, error
|
|
|
|
),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
format!("R({}) = C({})", self.destination, constant_index)
|
|
|
|
}
|
2024-09-12 09:08:55 +00:00
|
|
|
}
|
2024-09-12 17:03:24 +00:00
|
|
|
Operation::DeclareLocal => {
|
2024-09-12 13:11:49 +00:00
|
|
|
let local_index = u16::from_le_bytes([self.arguments[0], self.arguments[1]]);
|
2024-09-12 09:08:55 +00:00
|
|
|
|
2024-09-12 17:03:24 +00:00
|
|
|
format!("L({}) = R({})", local_index, self.destination)
|
2024-09-12 09:08:55 +00:00
|
|
|
}
|
2024-09-12 17:03:24 +00:00
|
|
|
Operation::GetLocal => {
|
|
|
|
let local_index = u16::from_le_bytes([self.arguments[0], self.arguments[1]]);
|
2024-09-12 09:08:55 +00:00
|
|
|
|
2024-09-12 17:03:24 +00:00
|
|
|
format!("R({}) = L({})", self.destination, local_index)
|
2024-09-12 09:08:55 +00:00
|
|
|
}
|
2024-09-12 17:03:24 +00:00
|
|
|
Operation::SetLocal => {
|
2024-09-12 09:08:55 +00:00
|
|
|
let identifier_index = u16::from_le_bytes([self.arguments[0], self.arguments[1]]);
|
|
|
|
|
2024-09-12 17:03:24 +00:00
|
|
|
format!("L({}) = R({})", identifier_index, self.destination)
|
2024-09-12 09:08:55 +00:00
|
|
|
}
|
|
|
|
Operation::Add => {
|
|
|
|
format!(
|
2024-09-12 17:03:24 +00:00
|
|
|
"R({}) = RC({}) + RC({})",
|
|
|
|
self.destination, self.arguments[0], self.arguments[1]
|
2024-09-12 09:08:55 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
Operation::Subtract => {
|
|
|
|
format!(
|
2024-09-12 17:03:24 +00:00
|
|
|
"R({}) = RC({}) - RC({})",
|
|
|
|
self.destination, self.arguments[0], self.arguments[1]
|
2024-09-12 09:08:55 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
Operation::Multiply => {
|
|
|
|
format!(
|
2024-09-12 17:03:24 +00:00
|
|
|
"R({}) = RC({}) * RC({})",
|
|
|
|
self.destination, self.arguments[0], self.arguments[1]
|
2024-09-12 09:08:55 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
Operation::Divide => {
|
|
|
|
format!(
|
2024-09-12 17:03:24 +00:00
|
|
|
"R({}) = RC({}) / RC({})",
|
|
|
|
self.destination, self.arguments[0], self.arguments[1]
|
2024-09-12 09:08:55 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
Operation::Negate => {
|
2024-09-12 17:03:24 +00:00
|
|
|
format!("R({}) = -RC({})", self.destination, self.arguments[0])
|
2024-09-12 09:08:55 +00:00
|
|
|
}
|
2024-09-12 17:03:24 +00:00
|
|
|
Operation::Return => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(info)
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Instruction {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2024-09-12 17:03:24 +00:00
|
|
|
if let Some(info) = self.disassembly_info(None) {
|
|
|
|
write!(f, "{} {}", self.operation, info)
|
|
|
|
} else {
|
|
|
|
write!(f, "{}", self.operation)
|
2024-09-10 03:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 03:07:20 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
2024-09-12 04:39:31 +00:00
|
|
|
pub enum Operation {
|
2024-09-12 03:07:20 +00:00
|
|
|
// Stack manipulation
|
2024-09-12 09:08:55 +00:00
|
|
|
Move = 0,
|
|
|
|
Close = 1,
|
2024-09-12 03:07:20 +00:00
|
|
|
|
|
|
|
// Constants
|
2024-09-12 09:08:55 +00:00
|
|
|
LoadConstant = 2,
|
2024-09-12 03:07:20 +00:00
|
|
|
|
|
|
|
// Variables
|
2024-09-12 17:03:24 +00:00
|
|
|
DeclareLocal = 3,
|
|
|
|
GetLocal = 4,
|
|
|
|
SetLocal = 5,
|
2024-09-12 03:07:20 +00:00
|
|
|
|
|
|
|
// Binary operations
|
2024-09-12 09:08:55 +00:00
|
|
|
Add = 6,
|
|
|
|
Subtract = 7,
|
|
|
|
Multiply = 8,
|
|
|
|
Divide = 9,
|
2024-09-12 03:07:20 +00:00
|
|
|
|
|
|
|
// Unary operations
|
2024-09-12 09:08:55 +00:00
|
|
|
Negate = 10,
|
2024-09-12 03:07:20 +00:00
|
|
|
|
|
|
|
// Control flow
|
2024-09-12 09:08:55 +00:00
|
|
|
Return = 11,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u8> for Operation {
|
|
|
|
fn from(byte: u8) -> Self {
|
|
|
|
match byte {
|
|
|
|
0 => Operation::Move,
|
|
|
|
1 => Operation::Close,
|
|
|
|
2 => Operation::LoadConstant,
|
2024-09-12 17:03:24 +00:00
|
|
|
3 => Operation::DeclareLocal,
|
|
|
|
4 => Operation::GetLocal,
|
|
|
|
5 => Operation::SetLocal,
|
2024-09-12 09:08:55 +00:00
|
|
|
6 => Operation::Add,
|
|
|
|
7 => Operation::Subtract,
|
|
|
|
8 => Operation::Multiply,
|
|
|
|
9 => Operation::Divide,
|
|
|
|
10 => Operation::Negate,
|
|
|
|
_ => Operation::Return,
|
|
|
|
}
|
|
|
|
}
|
2024-09-10 03:24:22 +00:00
|
|
|
}
|
2024-09-10 14:44:15 +00:00
|
|
|
|
2024-09-12 04:39:31 +00:00
|
|
|
impl Display for Operation {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Operation::Move => write!(f, "MOVE"),
|
|
|
|
Operation::Close => write!(f, "CLOSE"),
|
|
|
|
Operation::LoadConstant => write!(f, "LOAD_CONSTANT"),
|
2024-09-12 17:03:24 +00:00
|
|
|
Operation::DeclareLocal => write!(f, "DECLARE_LOCAL"),
|
|
|
|
Operation::GetLocal => write!(f, "GET_LOCAL"),
|
|
|
|
Operation::SetLocal => write!(f, "SET_LOCAL"),
|
2024-09-12 04:39:31 +00:00
|
|
|
Operation::Add => write!(f, "ADD"),
|
|
|
|
Operation::Subtract => write!(f, "SUBTRACT"),
|
|
|
|
Operation::Multiply => write!(f, "MULTIPLY"),
|
|
|
|
Operation::Divide => write!(f, "DIVIDE"),
|
|
|
|
Operation::Negate => write!(f, "NEGATE"),
|
|
|
|
Operation::Return => write!(f, "RETURN"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 03:07:20 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::mem::size_of;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn instruction_is_32_bits() {
|
|
|
|
assert_eq!(size_of::<Instruction>(), 4);
|
2024-09-10 14:44:15 +00:00
|
|
|
}
|
|
|
|
}
|