feat: change define_ir_processor to handle code_gen_functions

Also flattened out that function by using let else
This commit is contained in:
Kasey White 2023-04-06 13:30:45 -04:00 committed by Kasey
parent bc7b07c1d9
commit 4ff0504d58
2 changed files with 227 additions and 215 deletions

View File

@ -125,10 +125,6 @@ impl<'a> CodeGenerator<'a> {
let other_term = self.uplc_code_gen(&mut other_ir_stack); let other_term = self.uplc_code_gen(&mut other_ir_stack);
let other_term = other_term.final_wrapper();
let other_term = self.wrap_validator_args(other_term, &other.arguments, true);
let (spend, mint) = if other.arguments.len() > fun.arguments.len() { let (spend, mint) = if other.arguments.len() > fun.arguments.len() {
(other_term, term) (other_term, term)
} else { } else {
@ -2972,27 +2968,33 @@ impl<'a> CodeGenerator<'a> {
) { ) {
let mut to_be_defined_map: IndexMap<FunctionAccessKey, Scope> = IndexMap::new(); let mut to_be_defined_map: IndexMap<FunctionAccessKey, Scope> = IndexMap::new();
for (index, ir) in ir_stack.to_vec().iter().enumerate().rev() { for (index, ir) in ir_stack.to_vec().iter().enumerate().rev() {
match ir { // I tried putting the 2 let else together, but then formatting stopped working
Air::Var { #[rustfmt::skip]
let Air::Var {
scope, constructor, .. scope, constructor, ..
} => { } = ir else {
if let ValueConstructorVariant::ModuleFn { let scope = ir.scope();
name,
module, process_scope_updates(&mut to_be_defined_map, scope, func_index_map);
builtin: None, continue;
.. };
} = &constructor.variant
{ #[rustfmt::skip]
let ValueConstructorVariant::ModuleFn {name, module, builtin: None, ..} = &constructor.variant else {
let scope = ir.scope();
process_scope_updates(&mut to_be_defined_map, scope, func_index_map);
continue;
};
let non_variant_function_key = FunctionAccessKey { let non_variant_function_key = FunctionAccessKey {
module_name: module.clone(), module_name: module.clone(),
function_name: name.clone(), function_name: name.clone(),
variant_name: String::new(), variant_name: String::new(),
}; };
let function = *self.functions.get(&non_variant_function_key).unwrap(); if let Some(function) = self.functions.get(&non_variant_function_key).cloned() {
let mut func_stack = AirStack::with_scope(self.id_gen.clone(), scope.clone());
let mut func_stack =
AirStack::with_scope(self.id_gen.clone(), scope.clone());
self.build(&function.body, &mut func_stack); self.build(&function.body, &mut func_stack);
@ -3007,9 +3009,7 @@ impl<'a> CodeGenerator<'a> {
if arg.tipo.is_generic() { if arg.tipo.is_generic() {
let param_type = &param_types[index]; let param_type = &param_types[index];
map.append(&mut builder::get_generic_id_and_type( map.append(&mut builder::get_generic_id_and_type(&arg.tipo, param_type));
&arg.tipo, param_type,
));
} }
} }
@ -3051,21 +3051,17 @@ impl<'a> CodeGenerator<'a> {
let mut func_calls = IndexMap::new(); let mut func_calls = IndexMap::new();
for ir in func_ir.clone().into_iter() { for ir in func_ir.clone().into_iter() {
if let Air::Var { let Air::Var { constructor, ..} = ir else {
constructor: continue;
ValueConstructor { };
variant:
ValueConstructorVariant::ModuleFn { let ValueConstructorVariant::ModuleFn {
name: func_name, name : func_name, module, builtin: None, ..
module, } = &constructor.variant
.. else {
}, continue;
tipo, };
..
},
..
} = ir
{
let current_func = FunctionAccessKey { let current_func = FunctionAccessKey {
module_name: module.clone(), module_name: module.clone(),
function_name: func_name.clone(), function_name: func_name.clone(),
@ -3082,12 +3078,11 @@ impl<'a> CodeGenerator<'a> {
if function_key.clone() == current_func_as_variant { if function_key.clone() == current_func_as_variant {
func_calls.insert(current_func_as_variant, ()); func_calls.insert(current_func_as_variant, ());
} else if let (Some(function), Type::Fn { .. }) = } else if let (Some(function), Type::Fn { .. }) =
(function, &*tipo) (function, &*constructor.tipo)
{ {
let param_types = tipo.arg_types().unwrap(); let param_types = constructor.tipo.arg_types().unwrap();
let mut mono_types: IndexMap<u64, Arc<Type>> = let mut mono_types: IndexMap<u64, Arc<Type>> = IndexMap::new();
IndexMap::new();
let mut map = mono_types.into_iter().collect_vec(); let mut map = mono_types.into_iter().collect_vec();
for (index, arg) in function.arguments.iter().enumerate() { for (index, arg) in function.arguments.iter().enumerate() {
@ -3110,17 +3105,15 @@ impl<'a> CodeGenerator<'a> {
} }
mono_types = map.into_iter().collect(); mono_types = map.into_iter().collect();
let mut func_stack = AirStack::with_scope( let mut func_stack =
self.id_gen.clone(), AirStack::with_scope(self.id_gen.clone(), scope.clone());
scope.clone(),
);
self.build(&function.body, &mut func_stack); self.build(&function.body, &mut func_stack);
let temp_ir = func_stack.complete(); let temp_ir = func_stack.complete();
let (variant_name, _) = let (variant_name, _) =
builder::monomorphize(temp_ir, mono_types, &tipo); builder::monomorphize(temp_ir, mono_types, &constructor.tipo);
func_calls.insert( func_calls.insert(
FunctionAccessKey { FunctionAccessKey {
@ -3134,7 +3127,6 @@ impl<'a> CodeGenerator<'a> {
func_calls.insert(current_func, ()); func_calls.insert(current_func, ());
} }
} }
}
let mut args = vec![]; let mut args = vec![];
@ -3164,52 +3156,49 @@ impl<'a> CodeGenerator<'a> {
recursive, recursive,
args, args,
defined_by_zero_arg: in_zero_arg_func, defined_by_zero_arg: in_zero_arg_func,
is_code_gen_func: false,
}, },
); );
} }
} else if let Some(code_gen_func) = self.code_gen_functions.get(name).cloned() {
// Get actual code gen func if link
let (func_ir, dependencies) = match code_gen_func {
CodeGenFunction::Function(func_ir, dependencies) => (func_ir, dependencies),
CodeGenFunction::Link(func) => {
if let Some(CodeGenFunction::Function(func_ir, dependencies)) =
self.code_gen_functions.get(&func).cloned()
{
(func_ir, dependencies)
} else { } else {
for func in to_be_defined_map.clone().iter() { unreachable!("Link must resolve to a code gen function.");
if scope.common_ancestor(func.1) == scope.clone() {
if let Some(index_scope) = func_index_map.get(func.0) {
if index_scope.common_ancestor(func.1) == scope.clone() {
func_index_map.insert(func.0.clone(), scope.clone());
to_be_defined_map.shift_remove(func.0);
} else {
to_be_defined_map.insert(
func.0.clone(),
index_scope.common_ancestor(func.1),
);
}
} else {
func_index_map.insert(func.0.clone(), scope.clone());
to_be_defined_map.shift_remove(func.0);
} }
} }
} };
}
}
a => {
let scope = a.scope();
for func in to_be_defined_map.clone().iter() { func_components.insert(
if scope.common_ancestor(func.1) == scope.clone() { FunctionAccessKey {
if let Some(index_scope) = func_index_map.get(func.0) { module_name: "".to_string(),
if index_scope.common_ancestor(func.1) == scope.clone() { function_name: name.to_string(),
func_index_map.insert(func.0.clone(), scope.clone()); variant_name: "".to_string(),
to_be_defined_map.shift_remove(func.0); },
} else { FuncComponents {
to_be_defined_map.insert( ir: func_ir,
func.0.clone(), dependencies: dependencies
index_scope.common_ancestor(func.1), .into_iter()
.map(|item| FunctionAccessKey {
module_name: "".to_string(),
function_name: item,
variant_name: "".to_string(),
})
.collect_vec(),
recursive: false,
args: vec![],
defined_by_zero_arg: in_zero_arg_func,
is_code_gen_func: true,
},
); );
}
} else { } else {
func_index_map.insert(func.0.clone(), scope.clone()); unreachable!("We found a function with no definitions");
to_be_defined_map.shift_remove(func.0);
}
}
}
}
} }
} }
@ -4978,3 +4967,25 @@ impl<'a> CodeGenerator<'a> {
term term
} }
} }
fn process_scope_updates(
to_be_defined_map: &mut IndexMap<FunctionAccessKey, Scope>,
scope: Scope,
func_index_map: &mut IndexMap<FunctionAccessKey, Scope>,
) {
for func in to_be_defined_map.clone().iter() {
if scope.common_ancestor(func.1) == scope.clone() {
if let Some(index_scope) = func_index_map.get(func.0) {
if index_scope.common_ancestor(func.1) == scope.clone() {
func_index_map.insert(func.0.clone(), scope.clone());
to_be_defined_map.shift_remove(func.0);
} else {
to_be_defined_map.insert(func.0.clone(), index_scope.common_ancestor(func.1));
}
} else {
func_index_map.insert(func.0.clone(), scope.clone());
to_be_defined_map.shift_remove(func.0);
}
}
}
}

View File

@ -32,6 +32,7 @@ pub struct FuncComponents {
pub args: Vec<String>, pub args: Vec<String>,
pub recursive: bool, pub recursive: bool,
pub defined_by_zero_arg: bool, pub defined_by_zero_arg: bool,
pub is_code_gen_func: bool,
} }
#[derive(Clone, Eq, Debug, PartialEq, Hash)] #[derive(Clone, Eq, Debug, PartialEq, Hash)]