-
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.
fix(builder): fib example now works as expected
- Loading branch information
Showing
8 changed files
with
236 additions
and
154 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#[no_mangle] | ||
pub fn fib(a: u32, b: u32, n: u32) -> u32 { | ||
pub fn fib(a: u128, b: u128, n: u128) -> u128 { | ||
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 |
---|---|---|
@@ -1,19 +1,47 @@ | ||
use inkwell::basic_block::BasicBlock; | ||
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()) | ||
/// Process a branch instruction. If there is only 1 operand without condition it'll translate | ||
/// the basic block it jumps to and will move on to the next basic block. | ||
/// If there is an if/else it will process all the basic blocks that are involved. | ||
pub fn process_branch( | ||
&mut self, | ||
instruction: &InstructionValue<'ctx>, | ||
bb: &BasicBlock<'ctx>, | ||
is_loop: &bool, | ||
is_else: &bool, | ||
) -> String { | ||
// Get all the annoying variables that require to be declared in a bigger scope and will update | ||
// their value. | ||
self.bblock_variables.get(bb).cloned().unwrap_or_default().into_values().for_each(|val| { | ||
self.push_body_line(format!("{} = {};", val.trim_end_matches("_temp"), val)); | ||
}); | ||
self.set_basic_block_booleans(bb); | ||
// Case were there is an inconditionnal jump. | ||
if instruction.get_num_operands() == 1 { | ||
self.process_basic_block(&instruction.get_operand(0).unwrap().right().unwrap()); | ||
"".to_owned() | ||
} 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()); | ||
// There is a condition could either be a loop break or if/else | ||
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 { | ||
let res = format!("if {}\n{{break;}}", self.variables.get(&cond).unwrap()); | ||
|
||
"".to_owned() | ||
res | ||
} else { | ||
self.close_scopes(bb, is_else, is_loop); | ||
// 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.process_basic_block(&instruction.get_operand(1).unwrap().right().unwrap()); | ||
self.else_blocks.insert(instruction.get_operand(2).unwrap().right().unwrap()); | ||
self.process_basic_block(&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 |
---|---|---|
@@ -1,36 +1,52 @@ | ||
use inkwell::values::{AsValueRef, InstructionValue, PhiValue}; | ||
use inkwell::basic_block::BasicBlock; | ||
use inkwell::values::{AsValueRef, BasicValueEnum, 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 { | ||
pub fn process_phi(&mut self, instruction: &InstructionValue<'ctx>, bb: &BasicBlock<'ctx>) -> String { | ||
let annoying_phis = self.bblock_variables.get(bb).cloned().unwrap_or_default(); | ||
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 | ||
let phi_name = annoying_phis | ||
.get(unsafe { &BasicValueEnum::new(instruction.as_value_ref()) }) | ||
.cloned() | ||
.unwrap_or_else(|| { | ||
let name = self.get_name(phi.get_name()); | ||
// if it was not in the mapping insert it. In theory we could insert it in any case but we don't | ||
// want to do that it would poisin the regular variable mapping with the annoying phis and would | ||
// mess everything up | ||
self.variables.insert(phi.as_basic_value(), name.clone()); | ||
name | ||
}); // 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 | ||
let left_var = self.variables.get(&first.0).cloned().unwrap_or_else(|| { | ||
let name = self.get_name(first.0.get_name()); | ||
self.variables.insert(first.0, name.clone()); | ||
name | ||
}); // phi right 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()); | ||
let right_var = self.variables.get(&second.0).cloned().unwrap_or_else(|| { | ||
let name = self.get_name(second.0.get_name()); | ||
self.variables.insert(second.0, name.clone()); | ||
name | ||
}); // phi right variable | ||
// 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 " }, | ||
"let {} = if is_from_{} {{ {} }} else if is_from_{} {{ {} }} else {{ panic!(\"There is a bug in the \ | ||
compiler at var {} please report it\")}};", | ||
phi_name, | ||
get_name(first.1.get_name()).unwrap(), // phi left basic block | ||
self.get_name(first.1.get_name()), // phi left basic block | ||
left_var, | ||
get_name(second.1.get_name()).unwrap(), // phi right basic block | ||
right_var | ||
self.get_name(second.1.get_name()), // phi right basic block | ||
right_var, | ||
phi_name | ||
) | ||
} | ||
} |
Oops, something went wrong.