use std::fmt::Display;
pub use ux::*;
#[repr(u8)]
pub enum Opcode {
Halt = 0x00,
Loadi = 0x01,
LoadCi = 0x02,
LoadCf = 0x03,
Pushi = 0x10,
Addi = 0x20,
Subi = 0x21,
Muli = 0x22,
Divi = 0x23,
Modi = 0x24,
Inci = 0x25,
Addf = 0x30,
Subf = 0x31,
Mulf = 0x32,
Divf = 0x33,
Modf = 0x34,
Call = 0x40,
Jmp = 0x50,
JmpEq = 0x51,
JmpNe = 0x52,
Alloc = 0x60,
}
#[derive(Debug, Clone)]
pub enum PrettyOp {
Halt,
Loadi { i: u8, val: i48 },
LoadCf { i: u8, c: u48 },
LoadCi { i: u8, c: u48 },
Pushi { val: u56 },
Addi { r: u8, i: u8, k: u8 },
Subi { r: u8, i: u8, k: u8 },
Muli { r: u8, i: u8, k: u8 },
Divi { r: u8, i: u8, k: u8 },
Modi { r: u8, i: u8, k: u8 },
Addf { r: u8, i: u8, k: u8 },
Subf { r: u8, i: u8, k: u8 },
Mulf { r: u8, i: u8, k: u8 },
Divf { r: u8, i: u8, k: u8 },
Modf { r: u8, i: u8, k: u8 },
Inci { i: u8 },
Call { func: u32, args: u16, rets: u8 },
Jmp { offset: i56 },
JmpEq { i: u8, k: u8, offset: i40 },
JmpNe { i: u8, k: u8, offset: i40 },
Comment(String),
Alloc { n: u56 },
}
impl Display for PrettyOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_string())
}
}
impl From<u64> for PrettyOp {
fn from(value: u64) -> Self {
let bytes = value.to_le_bytes();
let opcode = unsafe { std::mem::transmute::<u8, Opcode>(bytes[0]) };
match opcode {
Opcode::Halt => PrettyOp::Halt,
Opcode::Loadi => {
let i = bytes[1];
let val = i48::new(bytes_to_i64(&bytes[2..8]));
PrettyOp::Loadi { i, val }
}
Opcode::LoadCi => {
let i = bytes[1];
let c = u48::new(bytes_to_u64(&bytes[2..8]));
PrettyOp::LoadCi { i, c }
}
Opcode::LoadCf => {
let i = bytes[1];
let c = u48::new(bytes_to_u64(&bytes[2..8]));
PrettyOp::LoadCf { i, c }
}
Opcode::Pushi => {
let val = u56::new(bytes_to_u64(&bytes[1..8]));
PrettyOp::Pushi { val }
}
Opcode::Addi => {
let r = bytes[1];
let i = bytes[2];
let k = bytes[3];
PrettyOp::Addi { r, i, k }
}
Opcode::Subi => {
let r = bytes[1];
let i = bytes[2];
let k = bytes[3];
PrettyOp::Subi { r, i, k }
}
Opcode::Muli => {
let r = bytes[1];
let i = bytes[2];
let k = bytes[3];
PrettyOp::Muli { r, i, k }
}
Opcode::Divi => {
let r = bytes[1];
let i = bytes[2];
let k = bytes[3];
PrettyOp::Divi { r, i, k }
}
Opcode::Modi => {
let r = bytes[1];
let i = bytes[2];
let k = bytes[3];
PrettyOp::Modi { r, i, k }
}
Opcode::Inci => {
let i = bytes[1];
PrettyOp::Inci { i }
}
Opcode::Addf => {
let r = bytes[1];
let i = bytes[2];
let k = bytes[3];
PrettyOp::Addf { r, i, k }
}
Opcode::Subf => {
let r = bytes[1];
let i = bytes[2];
let k = bytes[3];
PrettyOp::Subf { r, i, k }
}
Opcode::Mulf => {
let r = bytes[1];
let i = bytes[2];
let k = bytes[3];
PrettyOp::Mulf { r, i, k }
}
Opcode::Divf => {
let r = bytes[1];
let i = bytes[2];
let k = bytes[3];
PrettyOp::Divf { r, i, k }
}
Opcode::Modf => {
let r = bytes[1];
let i = bytes[2];
let k = bytes[3];
PrettyOp::Modf { r, i, k }
}
Opcode::Call => {
let func = u32::from_le_bytes([bytes[1], bytes[2], bytes[3], bytes[4]]);
let args = u16::from_le_bytes([bytes[5], bytes[6]]);
let rets = bytes[7];
PrettyOp::Call { func, args, rets }
}
Opcode::Jmp => {
let offset = i56::new(bytes_to_i64(&bytes[1..8]));
PrettyOp::Jmp { offset }
}
Opcode::JmpEq => {
let i = bytes[1];
let k = bytes[2];
let offset = i40::new(bytes_to_i64(&bytes[3..8]));
PrettyOp::JmpEq { i, k, offset }
}
Opcode::JmpNe => {
let i = bytes[1];
let k = bytes[2];
let offset = i40::new(bytes_to_i64(&bytes[3..8]));
PrettyOp::JmpNe { i, k, offset }
}
Opcode::Alloc => {
let n = u56::new(bytes_to_u64(&bytes[1..8]));
PrettyOp::Alloc { n }
}
}
}
}
impl PrettyOp {
pub fn to_string(&self) -> String {
match self {
PrettyOp::Halt => "HALT".to_string(),
PrettyOp::Loadi { i, val } => format!("LOADI S[{}], {}", i, val),
PrettyOp::LoadCf { i, c } => format!("LOADCF S[{}], CF[{}]", i, c),
PrettyOp::LoadCi { i, c } => format!("LOADCI S[{}], CI[{}]", i, c),
PrettyOp::Pushi { val } => format!("PUSHI {}", val),
PrettyOp::Addi { r, i, k } => format!("ADDI S[{}], S[{}], S[{}]", r, i, k),
PrettyOp::Subi { r, i, k } => format!("SUBI S[{}], S[{}], S[{}]", r, i, k),
PrettyOp::Muli { r, i, k } => format!("MULI S[{}], S[{}], S[{}]", r, i, k),
PrettyOp::Divi { r, i, k } => format!("DIVI S[{}], S[{}], S[{}]", r, i, k),
PrettyOp::Modi { r, i, k } => format!("MODI S[{}], S[{}], S[{}]", r, i, k),
PrettyOp::Inci { i } => format!("INCI S[{}]", i),
PrettyOp::Addf { r, i, k } => format!("ADDF S[{}], S[{}], S[{}]", r, i, k),
PrettyOp::Subf { r, i, k } => format!("SUBF S[{}], S[{}], S[{}]", r, i, k),
PrettyOp::Mulf { r, i, k } => format!("MULF S[{}], S[{}], S[{}]", r, i, k),
PrettyOp::Divf { r, i, k } => format!("DIVF S[{}], S[{}], S[{}]", r, i, k),
PrettyOp::Modf { r, i, k } => format!("MODF S[{}], S[{}], S[{}]", r, i, k),
PrettyOp::Call { func, args, rets } => {
format!("CALL func_{:x}, {} args, {} rets", func, args, rets)
}
PrettyOp::Jmp { offset } => format!("JMPRAW {}", offset),
PrettyOp::JmpEq { i, k, offset } => format!("JMPEQ S[{}], S[{}], {}", i, k, offset),
PrettyOp::JmpNe { i, k, offset } => format!("JMPNE S[{}], S[{}], {}", i, k, offset),
PrettyOp::Comment(s) => format!("; {}", s),
PrettyOp::Alloc { n } => format!("ALLOC {}", n),
}
}
pub fn into_slice(self) -> [u8; 8] {
match self {
PrettyOp::Halt => [Opcode::Halt as u8, 0, 0, 0, 0, 0, 0, 0],
PrettyOp::Loadi { i, val } => {
let val = i64::from(val).to_le_bytes();
[
Opcode::Loadi as u8,
i,
val[0],
val[1],
val[2],
val[3],
val[4],
val[5],
]
}
PrettyOp::LoadCi { i, c } => {
let c = u64::from(c).to_le_bytes();
[Opcode::LoadCi as u8, i, c[0], c[1], c[2], c[3], c[4], c[5]]
}
PrettyOp::LoadCf { i, c } => {
let c = u64::from(c).to_le_bytes();
[Opcode::LoadCf as u8, i, c[0], c[1], c[2], c[3], c[4], c[5]]
}
PrettyOp::Pushi { val } => {
let v = u64::from(val).to_le_bytes();
[
Opcode::Pushi as u8,
v[0],
v[1],
v[2],
v[3],
v[4],
v[5],
v[6],
]
}
PrettyOp::Addi { r, i, k } => [Opcode::Addi as u8, r, i, k, 0, 0, 0, 0],
PrettyOp::Subi { r, i, k } => [Opcode::Subi as u8, r, i, k, 0, 0, 0, 0],
PrettyOp::Muli { r, i, k } => [Opcode::Muli as u8, r, i, k, 0, 0, 0, 0],
PrettyOp::Divi { r, i, k } => [Opcode::Divi as u8, r, i, k, 0, 0, 0, 0],
PrettyOp::Modi { r, i, k } => [Opcode::Modi as u8, r, i, k, 0, 0, 0, 0],
PrettyOp::Inci { i } => [Opcode::Inci as u8, i, 0, 0, 0, 0, 0, 0],
PrettyOp::Addf { r, i, k } => [Opcode::Addf as u8, r, i, k, 0, 0, 0, 0],
PrettyOp::Subf { r, i, k } => [Opcode::Subf as u8, r, i, k, 0, 0, 0, 0],
PrettyOp::Mulf { r, i, k } => [Opcode::Mulf as u8, r, i, k, 0, 0, 0, 0],
PrettyOp::Divf { r, i, k } => [Opcode::Divf as u8, r, i, k, 0, 0, 0, 0],
PrettyOp::Modf { r, i, k } => [Opcode::Modf as u8, r, i, k, 0, 0, 0, 0],
PrettyOp::Call { func, args, rets } => {
let func = func.to_le_bytes();
let args = args.to_le_bytes();
[
Opcode::Call as u8,
func[0],
func[1],
func[2],
func[3],
args[0],
args[1],
rets,
]
}
PrettyOp::Jmp { offset } => {
let off = i64::from(offset).to_le_bytes();
[
Opcode::Jmp as u8,
off[0],
off[1],
off[2],
off[3],
off[4],
off[5],
off[6],
]
}
PrettyOp::JmpEq { i, k, offset } => {
let off = i64::from(offset).to_le_bytes();
[
Opcode::JmpEq as u8,
i,
k,
off[0],
off[1],
off[2],
off[3],
off[4],
]
}
PrettyOp::JmpNe { i, k, offset } => {
let off = i64::from(offset).to_le_bytes();
[
Opcode::JmpNe as u8,
i,
k,
off[0],
off[1],
off[2],
off[3],
off[4],
]
}
PrettyOp::Comment(_) => [0; 8],
PrettyOp::Alloc { n } => {
let off = u64::from(n).to_le_bytes();
[
Opcode::Alloc as u8,
off[0],
off[1],
off[2],
off[3],
off[4],
off[5],
off[6],
]
}
}
}
}
pub struct Bytecode {
data: Vec<u8>,
}
#[inline]
pub fn bytes_to_u64(bytes: &[u8]) -> u64 {
bytes
.iter()
.take(8)
.enumerate()
.fold(0u64, |acc, (i, &b)| acc | ((b as u64) << (8 * i)))
}
#[inline]
pub fn bytes_to_i64(bytes: &[u8]) -> i64 {
let val: i64 = bytes
.iter()
.take(8)
.enumerate()
.fold(0i64, |acc, (i, &b)| acc | ((b as i64) << (8 * i)));
let sign_bit = 1i64 << (bytes.len() * 8 - 1);
if (val & sign_bit) != 0 {
if bytes.len() * 8 >= 64 {
val
} else {
val | (!0i64 << (bytes.len() * 8))
}
} else {
val
}
}
#[macro_export]
macro_rules! bytes_to_ux {
($ux:ty, $bytes:expr) => {{
const SIZE: usize = <$ux>::BITS as usize / 8;
let val = $bytes
.iter()
.take(SIZE)
.enumerate()
.fold(0u64, |acc, (i, &b)| acc | ((b as u64) << (8 * i)));
<$ux>::new(val)
}};
}
#[macro_export]
macro_rules! bytes_to_ix {
($ix:ty, $bytes:expr) => {{
const SIZE: usize = <$ix>::BITS as usize / 8;
let val: i64 = $bytes
.iter()
.take(SIZE)
.enumerate()
.fold(0i64, |acc, (i, &b)| acc | ((b as i64) << (8 * i)));
let sign_bit = 1i64 << (SIZE * 8 - 1);
if (val & sign_bit) != 0 {
<$ix>::new(val | (!0i64 << (SIZE * 8)))
} else {
<$ix>::new(val)
}
}};
}
impl Bytecode {
pub fn new() -> Self {
Self { data: Vec::new() }
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
data: Vec::with_capacity(capacity),
}
}
pub fn emit(&mut self, pretty_op: PrettyOp) {
let bytes = pretty_op.into_slice();
self.data.extend_from_slice(&bytes);
}
pub fn emit_raw(&mut self, bytes: [u8; 8]) {
self.data.extend_from_slice(&bytes);
}
pub fn into_bytes(self) -> Vec<u8> {
self.data
}
pub fn as_bytes(&self) -> &[u8] {
&self.data
}
pub fn handle(&self) -> usize {
self.data.len() - 1
}
pub fn get_at(&self, index: usize) -> Option<PrettyOp> {
if index + 8 <= self.data.len() {
let slice = &self.data[index..index + 8];
let mut arr = [0u8; 8];
arr.copy_from_slice(slice);
Some(PrettyOp::from(u64::from_le_bytes(arr)))
} else {
None
}
}
pub fn set_at(&mut self, index: usize, pretty_op: PrettyOp) -> Result<(), String> {
if index + 8 <= self.data.len() {
let bytes = pretty_op.into_slice();
self.data[index..index + 8].copy_from_slice(&bytes);
Ok(())
} else {
Err("Index out of bounds".to_string())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_prettyop_into_slice_halt() {
let op = PrettyOp::Halt;
assert_eq!(op.into_slice(), [0x00, 0, 0, 0, 0, 0, 0, 0]);
}
#[test]
fn test_prettyop_into_slice_loadi_max() {
let op = PrettyOp::Loadi {
i: u8::MAX,
val: i48::MAX,
};
let val = i64::from(i48::MAX).to_le_bytes();
assert_eq!(
op.into_slice(),
[
Opcode::Loadi as u8,
u8::MAX,
val[0],
val[1],
val[2],
val[3],
val[4],
val[5]
]
);
}
#[test]
fn test_prettyop_into_slice_loadci_max() {
let op = PrettyOp::LoadCi {
i: u8::MAX,
c: u48::MAX,
};
let c = u64::from(u48::MAX).to_le_bytes();
assert_eq!(
op.into_slice(),
[
Opcode::LoadCi as u8,
u8::MAX,
c[0],
c[1],
c[2],
c[3],
c[4],
c[5]
]
);
}
#[test]
fn test_prettyop_into_slice_pushi_max() {
let op = PrettyOp::Pushi { val: u56::MAX };
let v = u64::from(u56::MAX).to_le_bytes();
assert_eq!(
op.into_slice(),
[
Opcode::Pushi as u8,
v[0],
v[1],
v[2],
v[3],
v[4],
v[5],
v[6]
]
);
}
#[test]
fn test_prettyop_into_slice_addi_max() {
let op = PrettyOp::Addi {
r: u8::MAX,
i: u8::MAX,
k: u8::MAX,
};
assert_eq!(
op.into_slice(),
[Opcode::Addi as u8, u8::MAX, u8::MAX, u8::MAX, 0, 0, 0, 0]
);
}
#[test]
fn test_prettyop_into_slice_call_max() {
let op = PrettyOp::Call {
func: u32::MAX,
args: u16::MAX,
rets: u8::MAX,
};
let func = u32::MAX.to_le_bytes();
let args = u16::MAX.to_le_bytes();
assert_eq!(
op.into_slice(),
[
Opcode::Call as u8,
func[0],
func[1],
func[2],
func[3],
args[0],
args[1],
u8::MAX
]
);
}
#[test]
fn test_bytes_to_u56_zero() {
let bytes = [0u8; 7];
let val = bytes_to_ux!(u56, &bytes);
assert_eq!(val, u56::new(0));
}
#[test]
fn test_bytes_to_u56_max() {
let bytes = [0xFFu8; 7];
let val = bytes_to_ux!(u56, &bytes);
assert_eq!(val, u56::new(0x00FFFFFFFFFFFFFF));
}
#[test]
fn test_bytes_to_u56_middle() {
let bytes = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
let val = bytes_to_ux!(u56, &bytes);
assert_eq!(val, u56::new(0x07060504030201));
}
#[test]
fn test_bytes_to_i56_zero() {
let bytes = [0u8; 7];
let val = bytes_to_ix!(i56, &bytes);
assert_eq!(val, i56::new(0));
}
#[test]
fn test_bytes_to_i56_positive_middle() {
let bytes = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
let val = bytes_to_ix!(i56, &bytes);
assert_eq!(val, i56::new(0x07060504030201));
}
#[test]
fn test_bytes_to_i56_negative_one() {
let bytes = [0xFFu8; 7];
let val = bytes_to_ix!(i56, &bytes);
assert_eq!(val, i56::new(-1));
}
#[test]
fn test_bytes_to_i56_max_positive() {
let bytes = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F];
let val = bytes_to_ix!(i56, &bytes);
assert_eq!(val, i56::new(0x007FFFFFFFFFFFFF));
}
#[test]
fn test_bytes_to_i56_min_negative() {
let bytes = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80];
let val = bytes_to_ix!(i56, &bytes);
assert_eq!(val, i56::new(-36028797018963968)); }
#[test]
fn test_bytes_to_i56_sign_extension() {
let bytes = [0xFFu8; 7];
let val = bytes_to_ix!(i56, &bytes);
assert_eq!(val, i56::new(-1));
let bytes = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80];
let val = bytes_to_ix!(i56, &bytes);
assert_eq!(val, i56::new(-36028797018963968));
}
#[test]
fn test_bytes_to_i56_truncates_extra_bytes() {
let bytes = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xFF, 0xFF];
let val = bytes_to_ix!(i56, &bytes);
assert_eq!(val, i56::new(0x07060504030201));
}
#[test]
fn test_bytes_to_i56_from_i64_positive() {
let n: i64 = 12345678901234;
let bytes = n.to_le_bytes();
let val = bytes_to_ix!(i56, &bytes[..7]);
assert_eq!(val, i56::new(n & 0x00FFFFFFFFFFFFFF));
}
#[test]
fn test_bytes_to_i56_from_i64_negative() {
let n: i64 = -12345678901234;
let bytes = n.to_le_bytes();
let val = bytes_to_ix!(i56, &bytes[..7]);
let expected = i56::new(n | (!0i64 << 56));
assert_eq!(val, expected);
}
}