-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(builder): add stuff needed for fib
- Loading branch information
Showing
11 changed files
with
468 additions
and
90 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#[no_mangle] | ||
pub fn fib(a: u32, b: u32, n: u32) -> u32 { | ||
if n == 0 { a } else { fib(b, a + b, n - 1) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use inkwell::values::InstructionValue; | ||
|
||
use super::CairoFunctionBuilder; | ||
|
||
impl<'ctx> CairoFunctionBuilder<'ctx> { | ||
pub fn process_branch(&mut self, instruction: &InstructionValue<'ctx>, is_loop: &bool) -> String { | ||
let cond = instruction.get_operand(0).unwrap().left().unwrap(); | ||
// If we're in a loop this is the exit condition so we break. | ||
if *is_loop { | ||
format!("if {}\n{{break;}}", self.variables.get(&cond).unwrap()) | ||
} else { | ||
// else it means that we're in a if/else case and the first block is the if the 2nd is the else. | ||
self.if_blocks.insert(instruction.get_operand(1).unwrap().right().unwrap(), cond); | ||
self.else_blocks.insert(instruction.get_operand(2).unwrap().right().unwrap()); | ||
|
||
"".to_owned() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use inkwell::values::{AsValueRef, InstructionValue, PhiValue}; | ||
|
||
use super::CairoFunctionBuilder; | ||
use crate::builder::get_name; | ||
|
||
impl<'ctx> CairoFunctionBuilder<'ctx> { | ||
pub fn process_phi(&mut self, instruction: &InstructionValue<'ctx>, is_loop: &bool) -> String { | ||
let phi = unsafe { PhiValue::new(instruction.as_value_ref()) }; | ||
// name of the result variable | ||
let phi_name = get_name(phi.get_name()).unwrap(); // variable to store the result in | ||
// Incomming values (basic block + variable to set the value to) | ||
let first = phi.get_incoming(0).unwrap(); | ||
// Name of the variable we should set the value to. | ||
let left_var = get_name(first.0.get_name()).unwrap(); // phi left variable | ||
|
||
// Incomming values (basic block + variable to set the value to) | ||
let second = phi.get_incoming(1).unwrap(); | ||
// Name of the variable we should set the value to. | ||
let right_var = get_name(second.0.get_name()).unwrap(); // phi right variable | ||
|
||
self.variables.insert(first.0, left_var.clone()); | ||
self.variables.insert(second.0, right_var.clone()); | ||
self.variables.insert(phi.as_basic_value(), right_var.clone()); | ||
// If we're in a subscope we don't need the `let` because we declared the variable above the scope. | ||
format!( | ||
"{}{} = if is_from_{} {{ {} }} else if is_from_{} {{ {} }} else {{ panic!(\"There is a bug in the \ | ||
compiler please report it\")}};", | ||
if *is_loop { "" } else { "let " }, | ||
phi_name, | ||
get_name(first.1.get_name()).unwrap(), // phi left basic block | ||
left_var, | ||
get_name(second.1.get_name()).unwrap(), // phi right basic block | ||
right_var | ||
) | ||
} | ||
} |
Oops, something went wrong.