From fe1f200e4dafb15bbeb8166ab3dd2e58e67cf90e Mon Sep 17 00:00:00 2001 From: Kasey White Date: Mon, 27 Feb 2023 00:55:57 -0500 Subject: [PATCH] rename find_generics_... to find_and_replace_generics Other renames as well --- crates/aiken-lang/src/builder.rs | 79 ++++---- crates/aiken-lang/src/uplc.rs | 330 +++++++++++++++++-------------- 2 files changed, 221 insertions(+), 188 deletions(-) diff --git a/crates/aiken-lang/src/builder.rs b/crates/aiken-lang/src/builder.rs index 7a483df4..561835c4 100644 --- a/crates/aiken-lang/src/builder.rs +++ b/crates/aiken-lang/src/builder.rs @@ -1129,10 +1129,11 @@ pub fn match_ir_for_recursion( } } -pub fn find_generics_to_replace(tipo: &mut Arc, generic_types: &IndexMap>) { +pub fn find_and_replace_generics(tipo: &mut Arc, mono_types: &IndexMap>) { if let Some(id) = tipo.get_generic() { - //If generic does not have a type we know of like a None in option then just use same type - *tipo = generic_types.get(&id).unwrap_or(tipo).clone(); + // If a generic does not have a type we know of + // like a None in option then just use same type + *tipo = mono_types.get(&id).unwrap_or(tipo).clone(); } else if tipo.is_generic() { match &**tipo { Type::App { @@ -1144,7 +1145,7 @@ pub fn find_generics_to_replace(tipo: &mut Arc, generic_types: &IndexMap, generic_types: &IndexMap, generic_types: &IndexMap, generic_types: &IndexMap todo!(), TypeVar::Link { tipo } => { let mut tipo = tipo; - find_generics_to_replace(&mut tipo, generic_types); + find_and_replace_generics(&mut tipo, mono_types); tipo } TypeVar::Generic { .. } => unreachable!(), @@ -1364,7 +1365,7 @@ pub fn wrap_validator_args(term: Term, arguments: &[TypedArg]) -> Term, - generic_types: IndexMap>, + mono_types: IndexMap>, full_type: &Arc, ) -> (String, Vec) { let mut new_air = ir.clone(); @@ -1381,7 +1382,7 @@ pub fn monomorphize( if constructor.tipo.is_generic() { let mut tipo = constructor.tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); let mut variant = String::new(); @@ -1415,7 +1416,7 @@ pub fn monomorphize( } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::List { scope, @@ -1435,7 +1436,7 @@ pub fn monomorphize( } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::ListAccessor { scope, @@ -1455,7 +1456,7 @@ pub fn monomorphize( } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::ListExpose { scope, @@ -1474,7 +1475,7 @@ pub fn monomorphize( } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::BinOp { scope, @@ -1493,7 +1494,7 @@ pub fn monomorphize( } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::Builtin { scope, @@ -1507,7 +1508,7 @@ pub fn monomorphize( Air::UnWrapData { scope, tipo } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::UnWrapData { scope, tipo }; needs_variant = true; @@ -1516,7 +1517,7 @@ pub fn monomorphize( Air::WrapData { scope, tipo } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::WrapData { scope, tipo }; needs_variant = true; @@ -1529,7 +1530,7 @@ pub fn monomorphize( } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::When { scope, @@ -1547,7 +1548,7 @@ pub fn monomorphize( } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::Clause { scope, @@ -1567,7 +1568,7 @@ pub fn monomorphize( } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::ListClause { scope, @@ -1590,7 +1591,7 @@ pub fn monomorphize( } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::TupleClause { scope, @@ -1611,7 +1612,7 @@ pub fn monomorphize( } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::ClauseGuard { scope, @@ -1630,7 +1631,7 @@ pub fn monomorphize( } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::ListClauseGuard { scope, @@ -1645,7 +1646,7 @@ pub fn monomorphize( Air::Tuple { scope, tipo, count } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::Tuple { scope, tipo, count }; needs_variant = true; @@ -1658,7 +1659,7 @@ pub fn monomorphize( } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::TupleIndex { scope, @@ -1671,7 +1672,7 @@ pub fn monomorphize( Air::ErrorTerm { scope, tipo } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::ErrorTerm { scope, tipo }; needs_variant = true; @@ -1680,7 +1681,7 @@ pub fn monomorphize( Air::Trace { scope, tipo } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::Trace { scope, tipo }; needs_variant = true; @@ -1694,7 +1695,7 @@ pub fn monomorphize( } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::Record { scope, @@ -1712,7 +1713,7 @@ pub fn monomorphize( } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::RecordAccess { scope, @@ -1731,7 +1732,7 @@ pub fn monomorphize( for (ind, name, tipo) in indices { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); needs_variant = true; new_indices.push((ind, name, tipo)); } else { @@ -1755,7 +1756,7 @@ pub fn monomorphize( for (ind, tipo) in indices { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); needs_variant = true; new_indices.push((ind, tipo)); } else { @@ -1763,7 +1764,7 @@ pub fn monomorphize( } } if tipo.is_generic() { - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); } new_air[index] = Air::RecordUpdate { scope, @@ -1780,7 +1781,7 @@ pub fn monomorphize( } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::TupleAccessor { scope, @@ -1795,7 +1796,7 @@ pub fn monomorphize( Air::Call { scope, count, tipo } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::Call { scope, count, tipo }; needs_variant = true; @@ -1804,7 +1805,7 @@ pub fn monomorphize( Air::If { scope, tipo } => { if tipo.is_generic() { let mut tipo = tipo.clone(); - find_generics_to_replace(&mut tipo, &generic_types); + find_and_replace_generics(&mut tipo, &mono_types); new_air[index] = Air::If { scope, tipo }; needs_variant = true; @@ -1984,17 +1985,17 @@ pub fn replace_opaque_type(t: &mut Arc, data_types: IndexMap> = IndexMap::new(); + let mut mono_types: IndexMap> = IndexMap::new(); for (tipo, param) in new_type_fields.iter().zip(t.arg_types().unwrap()) { - let mut map = generics_type_map.into_iter().collect_vec(); + let mut map = mono_types.into_iter().collect_vec(); map.append(&mut get_generics_and_type(tipo, ¶m)); - generics_type_map = map.into_iter().collect(); + mono_types = map.into_iter().collect(); } let mut generic_type = data_type.constructors[0].arguments[0].tipo.clone(); - find_generics_to_replace(&mut generic_type, &generics_type_map); + find_and_replace_generics(&mut generic_type, &mono_types); replace_opaque_type(&mut generic_type, data_types.clone()); *t = generic_type; diff --git a/crates/aiken-lang/src/uplc.rs b/crates/aiken-lang/src/uplc.rs index 7d7b3c4b..dd00365d 100644 --- a/crates/aiken-lang/src/uplc.rs +++ b/crates/aiken-lang/src/uplc.rs @@ -89,10 +89,16 @@ impl<'a> CodeGenerator<'a> { self.build_ir(body, &mut ir_stack, scope); + println!("{:#?}", ir_stack); + self.define_ir(&mut ir_stack); + println!("{:#?}", ir_stack); + self.convert_opaque_type_to_inner_ir(&mut ir_stack); + println!("{:#?}", ir_stack); + let mut term = self.uplc_code_gen(&mut ir_stack); if self.needs_field_access { @@ -3144,6 +3150,8 @@ impl<'a> CodeGenerator<'a> { let mut recursion_func_map_to_add = recursion_func_map.clone(); + println!("ARE WE GETTING HERE"); + for func_index in func_index_map.clone().iter() { let func = func_index.0; @@ -3252,181 +3260,203 @@ impl<'a> CodeGenerator<'a> { if let ValueConstructorVariant::ModuleFn { name, module, - builtin, + builtin: None, .. } = &constructor.variant { - if builtin.is_none() { - let non_variant_function_key = FunctionAccessKey { - module_name: module.clone(), - function_name: name.clone(), - variant_name: String::new(), - }; + let non_variant_function_key = FunctionAccessKey { + module_name: module.clone(), + function_name: name.clone(), + variant_name: String::new(), + }; - let function = *self.functions.get(&non_variant_function_key).unwrap(); + let function = *self.functions.get(&non_variant_function_key).unwrap(); - let mut func_ir = vec![]; + let mut func_ir = vec![]; - self.build_ir(&function.body, &mut func_ir, scope.to_vec()); + self.build_ir(&function.body, &mut func_ir, scope.to_vec()); - let param_types = constructor.tipo.arg_types().unwrap(); + println!( + "OUR FUNC IR FOR {:#?} IS: {:#?}", + non_variant_function_key, func_ir + ); - let mut generics_type_map: IndexMap> = IndexMap::new(); + let param_types = constructor.tipo.arg_types().unwrap(); - for (index, arg) in function.arguments.iter().enumerate() { - if arg.tipo.is_generic() { - let mut map = generics_type_map.into_iter().collect_vec(); - map.append(&mut get_generics_and_type( - &arg.tipo, - ¶m_types[index], - )); + let mut mono_types: IndexMap> = IndexMap::new(); - generics_type_map = map.into_iter().collect(); + for (index, arg) in function.arguments.iter().enumerate() { + if arg.tipo.is_generic() { + let mut map = mono_types.into_iter().collect_vec(); + map.append(&mut get_generics_and_type( + &arg.tipo, + ¶m_types[index], + )); + + mono_types = map.into_iter().collect(); + } + } + + println!("DID WE GET HERE"); + + let (variant_name, func_ir) = + monomorphize(func_ir, mono_types, &constructor.tipo); + + println!("AND HERE?"); + + let function_key = FunctionAccessKey { + module_name: module.clone(), + function_name: non_variant_function_key.function_name, + variant_name: variant_name.clone(), + }; + + ir_stack[index] = Air::Var { + scope: scope.clone(), + constructor: constructor.clone(), + name: name.clone(), + variant_name: variant_name.clone(), + }; + + if let Some(scope_prev) = to_be_defined_map.get(&function_key) { + let new_scope = get_common_ancestor(scope, scope_prev); + + to_be_defined_map.insert(function_key, new_scope); + } else if func_components.get(&function_key).is_some() { + to_be_defined_map.insert(function_key.clone(), scope.to_vec()); + } else { + to_be_defined_map.insert(function_key.clone(), scope.to_vec()); + let mut func_calls = IndexMap::new(); + + for ir in func_ir.clone().into_iter() { + if let Air::Var { + constructor: + ValueConstructor { + variant: + ValueConstructorVariant::ModuleFn { + name: func_name, + module, + .. + }, + tipo, + .. + }, + .. + } = ir + { + let current_func = FunctionAccessKey { + module_name: module.clone(), + function_name: func_name.clone(), + variant_name: String::new(), + }; + + let current_func_as_variant = FunctionAccessKey { + module_name: module.clone(), + function_name: func_name.clone(), + variant_name: variant_name.clone(), + }; + + let function = self.functions.get(¤t_func); + if function_key.clone() == current_func_as_variant { + func_calls.insert(current_func_as_variant, ()); + } else if let (Some(function), Type::Fn { .. }) = + (function, &*tipo) + { + let mut mono_types: IndexMap> = + IndexMap::new(); + + let param_types = tipo.arg_types().unwrap(); + + for (index, arg) in function.arguments.iter().enumerate() { + if arg.tipo.is_generic() { + let mut map = mono_types.into_iter().collect_vec(); + map.append(&mut get_generics_and_type( + &arg.tipo, + ¶m_types[index], + )); + + mono_types = map.into_iter().collect(); + } + } + + let mut func_ir = vec![]; + + self.build_ir(&function.body, &mut func_ir, scope.to_vec()); + + let (variant_name, _) = + monomorphize(func_ir, mono_types, &tipo); + + func_calls.insert( + FunctionAccessKey { + module_name: current_func.module_name, + function_name: current_func.function_name, + variant_name, + }, + (), + ); + } else { + func_calls.insert(current_func, ()); + } } } - let (variant_name, func_ir) = - monomorphize(func_ir, generics_type_map, &constructor.tipo); + let mut args = vec![]; - let function_key = FunctionAccessKey { - module_name: module.clone(), - function_name: non_variant_function_key.function_name, - variant_name: variant_name.clone(), - }; + for arg in function.arguments.iter() { + match &arg.arg_name { + ArgName::Named { name, .. } => { + args.push(name.clone()); + } + _ => { + args.push("_".to_string()); + } + } + } - ir_stack[index] = Air::Var { - scope: scope.clone(), - constructor: constructor.clone(), - name: name.clone(), - variant_name: variant_name.clone(), - }; + println!("FUNC CALLS IS {:#?}", func_calls); - if let Some(scope_prev) = to_be_defined_map.get(&function_key) { - let new_scope = get_common_ancestor(scope, scope_prev); - - to_be_defined_map.insert(function_key, new_scope); - } else if func_components.get(&function_key).is_some() { - to_be_defined_map.insert(function_key.clone(), scope.to_vec()); + let recursive = if func_calls.get(&function_key).is_some() { + func_calls.remove(&function_key); + true } else { - to_be_defined_map.insert(function_key.clone(), scope.to_vec()); - let mut func_calls = IndexMap::new(); + false + }; - for ir in func_ir.clone().into_iter() { - if let Air::Var { - constructor: - ValueConstructor { - variant: - ValueConstructorVariant::ModuleFn { - name: func_name, - module, - .. - }, - tipo, - .. - }, - .. - } = ir - { - let current_func = FunctionAccessKey { - module_name: module.clone(), - function_name: func_name.clone(), - variant_name: String::new(), - }; - - let current_func_as_variant = FunctionAccessKey { - module_name: module.clone(), - function_name: func_name.clone(), - variant_name: variant_name.clone(), - }; - - let function = self.functions.get(¤t_func); - if function_key.clone() == current_func_as_variant { - func_calls.insert(current_func_as_variant, ()); - } else if let (Some(function), Type::Fn { .. }) = - (function, &*tipo) - { - let mut generics_type_map: IndexMap> = - IndexMap::new(); - - let param_types = tipo.arg_types().unwrap(); - - for (index, arg) in - function.arguments.iter().enumerate() - { - if arg.tipo.is_generic() { - let mut map = - generics_type_map.into_iter().collect_vec(); - map.append(&mut get_generics_and_type( - &arg.tipo, - ¶m_types[index], - )); - - generics_type_map = map.into_iter().collect(); - } - } - - let mut func_ir = vec![]; - - self.build_ir( - &function.body, - &mut func_ir, - scope.to_vec(), - ); - - let (variant_name, _) = - monomorphize(func_ir, generics_type_map, &tipo); - - func_calls.insert( - FunctionAccessKey { - module_name: current_func.module_name, - function_name: current_func.function_name, - variant_name, - }, - (), - ); - } else { - func_calls.insert(current_func, ()); - } + func_components.insert( + function_key, + FuncComponents { + ir: func_ir, + dependencies: func_calls.keys().cloned().collect_vec(), + recursive, + args, + defined_by_zero_arg: in_zero_arg_func, + }, + ); + } + } else { + println!("ARE WE GETTING HERE323323"); + for func in to_be_defined_map.clone().iter() { + if get_common_ancestor(scope, func.1) == scope.to_vec() { + if let Some(index_scope) = func_index_map.get(func.0) { + if get_common_ancestor(index_scope, func.1) == scope.to_vec() { + 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(), + get_common_ancestor(index_scope, func.1), + ); } - } - - let mut args = vec![]; - - for arg in function.arguments.iter() { - match &arg.arg_name { - ArgName::Named { name, .. } => { - args.push(name.clone()); - } - _ => { - args.push("_".to_string()); - } - } - } - - let recursive = if func_calls.get(&function_key).is_some() { - func_calls.remove(&function_key); - true } else { - false - }; - - func_components.insert( - function_key, - FuncComponents { - ir: func_ir, - dependencies: func_calls.keys().cloned().collect_vec(), - recursive, - args, - defined_by_zero_arg: in_zero_arg_func, - }, - ); + func_index_map.insert(func.0.clone(), scope.clone()); + to_be_defined_map.shift_remove(func.0); + } } } } } a => { let scope = a.scope(); - + println!("ARE WE GETTING HERE2222"); for func in to_be_defined_map.clone().iter() { if get_common_ancestor(&scope, func.1) == scope.to_vec() { if let Some(index_scope) = func_index_map.get(func.0) { @@ -3449,6 +3479,8 @@ impl<'a> CodeGenerator<'a> { } } + println!("REACHED HERE"); + // Still to be defined for func in to_be_defined_map.clone().iter() { let index_scope = func_index_map.get(func.0).unwrap();