From 9f5889c1263d63b953b3a324da07321deef5ca58 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Sat, 27 Apr 2024 08:28:40 -0700 Subject: Add integer arithmetic. --- bytecode/src/encoding.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) (limited to 'bytecode/src/encoding.rs') diff --git a/bytecode/src/encoding.rs b/bytecode/src/encoding.rs index eb6633d..8bec753 100644 --- a/bytecode/src/encoding.rs +++ b/bytecode/src/encoding.rs @@ -39,6 +39,34 @@ pub struct Exit { pub val: Arg, } +#[derive(Debug, Clone, Copy)] +pub struct Add { + pub out: u8, + pub val1: Arg, + pub val2: Arg, +} + +#[derive(Debug, Clone, Copy)] +pub struct Sub { + pub out: u8, + pub val1: Arg, + pub val2: Arg, +} + +#[derive(Debug, Clone, Copy)] +pub struct Mul { + pub out: u8, + pub val1: Arg, + pub val2: Arg, +} + +#[derive(Debug, Clone, Copy)] +pub struct Div { + pub out: u8, + pub val1: Arg, + pub val2: Arg, +} + #[derive(Debug, Clone, Copy)] pub enum Op { Alloc(Alloc), @@ -47,6 +75,10 @@ pub enum Op { Peek(Peek), Shuf(Shuf), Exit(Exit), + Add(Add), + Sub(Sub), + Mul(Mul), + Div(Div), } struct Reader<'a> { @@ -106,7 +138,7 @@ impl Op { let code_byte = r.parse_byte()?; let code = code_byte >> 2; let arg1_const = code_byte & 2 != 0; - // let arg2_const = code_byte & 1 != 0; + let arg2_const = code_byte & 1 != 0; let op = match code { 1 => { @@ -142,6 +174,30 @@ impl Op { let val = r.parse_arg(arg1_const)?; Op::Exit(Exit { val }) } + 7 => { + let out = r.parse_local()?; + let val1 = r.parse_arg(arg1_const)?; + let val2 = r.parse_arg(arg2_const)?; + Op::Add(Add { out, val1, val2 }) + } + 8 => { + let out = r.parse_local()?; + let val1 = r.parse_arg(arg1_const)?; + let val2 = r.parse_arg(arg2_const)?; + Op::Sub(Sub { out, val1, val2 }) + } + 9 => { + let out = r.parse_local()?; + let val1 = r.parse_arg(arg1_const)?; + let val2 = r.parse_arg(arg2_const)?; + Op::Mul(Mul { out, val1, val2 }) + } + 10 => { + let out = r.parse_local()?; + let val1 = r.parse_arg(arg1_const)?; + let val2 = r.parse_arg(arg2_const)?; + Op::Div(Div { out, val1, val2 }) + } _ => { return Err(Box::from(format!("invalid code {}", code))); } -- cgit v1.3.1