From 5a65ee2fc705177ac540eb499e14e671d302d54e Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Wed, 26 Jan 2022 09:13:25 +0700 Subject: [PATCH] feat: math intrinsic --- example/math.blsp | 1 + math.bbb | 7 +++++ src/compiler/compile.rs | 70 +++++++++++++++++++++++++++++++++++++++-- src/compiler/lib.rs | 6 ---- src/compiler/mod.rs | 2 ++ 5 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 example/math.blsp create mode 100644 math.bbb diff --git a/example/math.blsp b/example/math.blsp new file mode 100644 index 0000000..1cd1035 --- /dev/null +++ b/example/math.blsp @@ -0,0 +1 @@ +(print (/ (+ 345 345) 10)) \ No newline at end of file diff --git a/math.bbb b/math.bbb new file mode 100644 index 0000000..47408d6 --- /dev/null +++ b/math.bbb @@ -0,0 +1,7 @@ +STORE r1 $345 +STORE r2 $345 +IADD r1 r2 r3 +STORE r4 $10 +IDIV r3 r4 r5 +STORE r6 $1 +CALL r6 r5 diff --git a/src/compiler/compile.rs b/src/compiler/compile.rs index 89844da..00fcc1e 100644 --- a/src/compiler/compile.rs +++ b/src/compiler/compile.rs @@ -85,7 +85,9 @@ impl Compiler { value: Type::String(s.to_string()), }); } - _ => return Err(format!("Expected atom, got {:?}", atom)), + _ => { + result.append(&mut self.compile(atom.clone())?); + } } Ok(result) @@ -110,7 +112,71 @@ impl Compiler { address: call_register, args: arg_pointer, }); - } + }, + "add" | "+" => { + let mut lhs = self.compile_atom(&args[0])?; + let lhs_pointer = self.current_pointer(); + result.append(&mut lhs); + + let mut rhs = self.compile_atom(&args[1])?; + let rhs_pointer = self.current_pointer(); + result.append(&mut rhs); + + let result_register = self.next_register(); + result.push(Instr::IAdd { + lhs: lhs_pointer, + rhs: rhs_pointer, + to: result_register, + }); + }, + "sub" | "-" => { + let mut lhs = self.compile_atom(&args[0])?; + let lhs_pointer = self.current_pointer(); + result.append(&mut lhs); + + let mut rhs = self.compile_atom(&args[1])?; + let rhs_pointer = self.current_pointer(); + result.append(&mut rhs); + + let result_register = self.next_register(); + result.push(Instr::ISub { + lhs: lhs_pointer, + rhs: rhs_pointer, + to: result_register, + }); + }, + "mul" | "*" => { + let mut lhs = self.compile_atom(&args[0])?; + let lhs_pointer = self.current_pointer(); + result.append(&mut lhs); + + let mut rhs = self.compile_atom(&args[1])?; + let rhs_pointer = self.current_pointer(); + result.append(&mut rhs); + + let result_register = self.next_register(); + result.push(Instr::IMul { + lhs: lhs_pointer, + rhs: rhs_pointer, + to: result_register, + }); + }, + "div" | "/" => { + let mut lhs = self.compile_atom(&args[0])?; + let lhs_pointer = self.current_pointer(); + result.append(&mut lhs); + + let mut rhs = self.compile_atom(&args[1])?; + let rhs_pointer = self.current_pointer(); + result.append(&mut rhs); + + let result_register = self.next_register(); + result.push(Instr::IDiv { + lhs: lhs_pointer, + rhs: rhs_pointer, + to: result_register, + }); + }, _ => return Err(format!("Unknown intrinsic: {}", intrinsic)), } diff --git a/src/compiler/lib.rs b/src/compiler/lib.rs index a77daf5..74e7c6e 100644 --- a/src/compiler/lib.rs +++ b/src/compiler/lib.rs @@ -38,8 +38,6 @@ pub enum Instr { Store { address: Register, value: Type, }, // Call intrinsic function. Call { address: Register, args: Register }, - // Stack arithmetic. - Add, Sub, Mul, Div, // Immediate arithmetic. IAdd { lhs: Register, rhs: Register, to: Register, }, ISub { lhs: Register, rhs: Register, to: Register, }, @@ -53,10 +51,6 @@ impl Display for Instr { Instr::Load { address } => write!(f, "LOAD {}", address), Instr::Store { address, value } => write!(f, "STORE {} {}", address, value), Instr::Call { address, args } => write!(f, "CALL {} {}", address, args), - Instr::Add => write!(f, "ADD"), - Instr::Sub => write!(f, "SUB"), - Instr::Mul => write!(f, "MUL"), - Instr::Div => write!(f, "DIV"), Instr::IAdd { lhs, rhs, to } => write!(f, "IADD {} {} {}", lhs, rhs, to), Instr::ISub { lhs, rhs, to } => write!(f, "ISUB {} {} {}", lhs, rhs, to), Instr::IMul { lhs, rhs, to } => write!(f, "IMUL {} {} {}", lhs, rhs, to), diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index c566f60..00ab781 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -1,2 +1,4 @@ +/// Contains the definition of instructions and register. pub mod lib; +/// Contains the compiler. pub mod compile; \ No newline at end of file