Skip to content

Commit

Permalink
Tweaked formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Hirevo committed Aug 5, 2020
1 parent 306aaab commit bf7a0ff
Showing 1 changed file with 94 additions and 53 deletions.
147 changes: 94 additions & 53 deletions som-core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,124 @@ use std::fmt;

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[rustfmt::skip]
pub enum Bytecode {
Halt = 0,
Dup = 1,
PushLocal = 2,
PushArgument = 3,
PushField = 4,
PushBlock = 5,
PushConstant = 6,
PushGlobal = 7,
Pop = 8,
PopLocal = 9,
PopArgument = 10,
PopField = 11,
Send = 12,
SuperSend = 13,
ReturnLocal = 14,
Halt = 0,
Dup = 1,
PushLocal = 2,
PushArgument = 3,
PushField = 4,
PushBlock = 5,
PushConstant = 6,
PushGlobal = 7,
Pop = 8,
PopLocal = 9,
PopArgument = 10,
PopField = 11,
Send = 12,
SuperSend = 13,
ReturnLocal = 14,
ReturnNonLocal = 15,
}

impl Bytecode {
/// Get the instruction's name.
#[rustfmt::skip]
pub fn name(self) -> &'static str {
NAMES[self as usize]
// NAMES[self as usize]
match self {
Self::Halt => "HALT",
Self::Dup => "DUP",
Self::PushLocal => "PUSH_LOCAL",
Self::PushArgument => "PUSH_ARGUMENT",
Self::PushField => "PUSH_FIELD",
Self::PushBlock => "PUSH_BLOCK",
Self::PushConstant => "PUSH_CONSTANT",
Self::PushGlobal => "PUSH_GLOBAL",
Self::Pop => "POP",
Self::PopLocal => "POP_LOCAL",
Self::PopArgument => "POP_ARGUMENT",
Self::PopField => "POP_FIELD",
Self::Send => "SEND",
Self::SuperSend => "SUPER_SEND",
Self::ReturnLocal => "RETURN_LOCAL",
Self::ReturnNonLocal => "RETURN_NON_LOCAL",
}
}

/// Get the instruction's name padded so that every padded names are of the same length.
#[rustfmt::skip]
pub fn padded_name(self) -> &'static str {
PADDED_NAMES[self as usize]
// PADDED_NAMES[self as usize]
match self {
Self::Halt => "HALT ",
Self::Dup => "DUP ",
Self::PushLocal => "PUSH_LOCAL ",
Self::PushArgument => "PUSH_ARGUMENT ",
Self::PushField => "PUSH_FIELD ",
Self::PushBlock => "PUSH_BLOCK ",
Self::PushConstant => "PUSH_CONSTANT ",
Self::PushGlobal => "PUSH_GLOBAL ",
Self::Pop => "POP ",
Self::PopLocal => "POP_LOCAL ",
Self::PopArgument => "POP_ARGUMENT ",
Self::PopField => "POP_FIELD ",
Self::Send => "SEND ",
Self::SuperSend => "SUPER_SEND ",
Self::ReturnLocal => "RETURN_LOCAL ",
Self::ReturnNonLocal => "RETURN_NON_LOCAL",
}
}

/// Get the number of bytes to read to process the instruction.
#[rustfmt::skip]
pub fn bytecode_len(self) -> usize {
match self {
Bytecode::Halt => 1,
Bytecode::Dup => 1,
Bytecode::PushLocal => 3,
Bytecode::PushArgument => 3,
Bytecode::PushField => 2,
Bytecode::PushBlock => 2,
Bytecode::PushConstant => 2,
Bytecode::PushGlobal => 2,
Bytecode::Pop => 1,
Bytecode::PopLocal => 3,
Bytecode::PopArgument => 3,
Bytecode::PopField => 2,
Bytecode::Send => 2,
Bytecode::SuperSend => 2,
Bytecode::ReturnLocal => 1,
Bytecode::ReturnNonLocal => 1,
Self::Halt => 1,
Self::Dup => 1,
Self::PushLocal => 3,
Self::PushArgument => 3,
Self::PushField => 2,
Self::PushBlock => 2,
Self::PushConstant => 2,
Self::PushGlobal => 2,
Self::Pop => 1,
Self::PopLocal => 3,
Self::PopArgument => 3,
Self::PopField => 2,
Self::Send => 2,
Self::SuperSend => 2,
Self::ReturnLocal => 1,
Self::ReturnNonLocal => 1,
}
}

/// Attempt to convert a raw byte to an instruction.
pub fn from_byte(byte: u8) -> Option<Bytecode> {
#[rustfmt::skip]
pub fn from_byte(byte: u8) -> Option<Self> {
match byte {
0 => Some(Bytecode::Halt),
1 => Some(Bytecode::Dup),
2 => Some(Bytecode::PushLocal),
3 => Some(Bytecode::PushArgument),
4 => Some(Bytecode::PushField),
5 => Some(Bytecode::PushBlock),
6 => Some(Bytecode::PushConstant),
7 => Some(Bytecode::PushGlobal),
8 => Some(Bytecode::Pop),
9 => Some(Bytecode::PopLocal),
10 => Some(Bytecode::PopArgument),
11 => Some(Bytecode::PopField),
12 => Some(Bytecode::Send),
13 => Some(Bytecode::SuperSend),
14 => Some(Bytecode::ReturnLocal),
15 => Some(Bytecode::ReturnNonLocal),
_ => None,
0 => Some(Self::Halt),
1 => Some(Self::Dup),
2 => Some(Self::PushLocal),
3 => Some(Self::PushArgument),
4 => Some(Self::PushField),
5 => Some(Self::PushBlock),
6 => Some(Self::PushConstant),
7 => Some(Self::PushGlobal),
8 => Some(Self::Pop),
9 => Some(Self::PopLocal),
10 => Some(Self::PopArgument),
11 => Some(Self::PopField),
12 => Some(Self::Send),
13 => Some(Self::SuperSend),
14 => Some(Self::ReturnLocal),
15 => Some(Self::ReturnNonLocal),
_ => None,
}
}
}

static NAMES: [&str; 16] = [
pub static NAMES: [&str; 16] = [
"HALT",
"DUP",
"PUSH_LOCAL",
Expand All @@ -97,7 +138,7 @@ static NAMES: [&str; 16] = [
"RETURN_NON_LOCAL",
];

static PADDED_NAMES: [&str; 16] = [
pub static PADDED_NAMES: [&str; 16] = [
"HALT ",
"DUP ",
"PUSH_LOCAL ",
Expand Down

0 comments on commit bf7a0ff

Please sign in to comment.