checkpoint;

Remaining work is on function hoisting. Functions have been defined and monomorphized
This commit is contained in:
microproofs 2023-07-21 01:14:22 -04:00 committed by Kasey
parent ae9de11e77
commit 62660e04b5
3 changed files with 129 additions and 97 deletions

View File

@ -2214,17 +2214,18 @@ impl<'a> CodeGenerator<'a> {
&mut air_tree,
&mut functions_to_hoist,
&mut used_functions,
&mut TreePath::new(),
0,
0,
);
println!("FUNCTIONS TO HOIST {:#?}", functions_to_hoist);
while let Some((key, variant_name)) = used_functions.pop() {
defined_functions.push((key.clone(), variant_name.clone()));
let function_variants = functions_to_hoist
.get(&key)
.unwrap_or_else(|| panic!("Missing Function Definition"));
let (_, function) = function_variants
let (tree_path, function) = function_variants
.get(&variant_name)
.unwrap_or_else(|| panic!("Missing Function Variant Definition"));
@ -2232,12 +2233,15 @@ impl<'a> CodeGenerator<'a> {
let mut hoist_body = body.clone();
let mut hoist_deps = deps.clone();
self.hoist_functions(
let mut tree_path = tree_path.clone();
self.define_dependent_functions(
&mut hoist_body,
&mut functions_to_hoist,
&mut used_functions,
&defined_functions,
&mut hoist_deps,
&mut tree_path,
);
let function_variants = functions_to_hoist
@ -2258,10 +2262,14 @@ impl<'a> CodeGenerator<'a> {
println!("FUNCTIONS DEFINED {:#?}", defined_functions);
println!("FUNCTIONS USED {:#?}", used_functions);
// First we need to sort functions by dependencies
// Now we need to hoist the functions to the top of the validator
air_tree
}
fn hoist_functions(
fn define_dependent_functions(
&mut self,
air_tree: &mut AirTree,
function_usage: &mut IndexMap<
@ -2271,8 +2279,21 @@ impl<'a> CodeGenerator<'a> {
used_functions: &mut Vec<(FunctionAccessKey, String)>,
defined_functions: &[(FunctionAccessKey, String)],
current_function_deps: &mut Vec<(FunctionAccessKey, String)>,
validator_tree_path: &mut TreePath,
) {
self.find_function_vars_and_depth(air_tree, function_usage, current_function_deps);
let Some((depth, index)) = validator_tree_path.pop()
else { return };
validator_tree_path.push(depth, index);
self.find_function_vars_and_depth(
air_tree,
function_usage,
current_function_deps,
validator_tree_path,
depth + 1,
0,
);
for (generic_function_key, variant_name) in current_function_deps.iter() {
if !used_functions
@ -2295,8 +2316,15 @@ impl<'a> CodeGenerator<'a> {
IndexMap<String, (TreePath, UserFunction)>,
>,
dependency_functions: &mut Vec<(FunctionAccessKey, String)>,
path: &mut TreePath,
current_depth: usize,
depth_index: usize,
) {
air_tree.traverse_tree_with(&mut |air_tree, tree_path| {
air_tree.traverse_tree_with_path(
path,
current_depth,
depth_index,
&mut |air_tree, tree_path| {
if let AirTree::Expression(AirExpression::Var { constructor, .. }) = air_tree {
let ValueConstructorVariant::ModuleFn {
name: func_name,
@ -2307,7 +2335,6 @@ impl<'a> CodeGenerator<'a> {
else { return };
let function_var_tipo = &constructor.tipo;
println!("FUNCTION VAR TYPE {:#?}", function_var_tipo);
let generic_function_key = FunctionAccessKey {
module_name: module.clone(),
@ -2319,8 +2346,6 @@ impl<'a> CodeGenerator<'a> {
.get(&generic_function_key)
.unwrap_or_else(|| panic!("Missing Function Definition"));
println!("Function Def {:#?}", function_def);
let mut function_var_types = function_var_tipo
.arg_types()
.unwrap_or_else(|| panic!("Expected a function tipo with arg types"));
@ -2351,8 +2376,6 @@ impl<'a> CodeGenerator<'a> {
IndexMap::new()
};
println!("MONO TYPES {:#?}", mono_types);
let variant_name = mono_types
.iter()
.sorted_by(|(id, _), (id2, _)| id.cmp(id2))
@ -2363,7 +2386,8 @@ impl<'a> CodeGenerator<'a> {
.iter()
.any(|(key, name)| key == &generic_function_key && name == &variant_name)
{
dependency_functions.push((generic_function_key.clone(), variant_name.clone()));
dependency_functions
.push((generic_function_key.clone(), variant_name.clone()));
}
if let Some(func_variants) = function_usage.get_mut(&generic_function_key) {
@ -2374,7 +2398,10 @@ impl<'a> CodeGenerator<'a> {
monomorphize(&mut function_air_tree_body, &mono_types);
erase_opaque_type_operations(&mut function_air_tree_body, &self.data_types);
erase_opaque_type_operations(
&mut function_air_tree_body,
&self.data_types,
);
func_variants.insert(
variant_name,
@ -2404,7 +2431,8 @@ impl<'a> CodeGenerator<'a> {
function_usage.insert(generic_function_key, function_variant_path);
}
}
});
},
);
}
fn uplc_code_gen(&mut self, ir_stack: &mut Vec<Air>) -> Term<Name> {

View File

@ -175,8 +175,6 @@ pub fn find_and_replace_generics(
new_args.push(arg);
}
println!("SO ARGS ARE {:#?}", new_args);
let ret = find_and_replace_generics(ret, mono_types);
let t = Type::Fn {
@ -199,10 +197,7 @@ pub fn find_and_replace_generics(
let var_type = var_tipo.as_ref().borrow().clone();
match var_type {
TypeVar::Link { tipo } => {
find_and_replace_generics(&tipo, mono_types);
tipo
}
TypeVar::Link { tipo } => find_and_replace_generics(&tipo, mono_types),
TypeVar::Generic { .. } | TypeVar::Unbound { .. } => unreachable!(),
}
}
@ -328,7 +323,6 @@ pub fn get_variant_name(t: &Arc<Type>) -> String {
pub fn monomorphize(air_tree: &mut AirTree, mono_types: &IndexMap<u64, Arc<Type>>) {
air_tree.traverse_tree_with(&mut |air_tree: &mut AirTree, _| {
let mut held_types = air_tree.mut_held_types();
println!("Held types: {:#?}", held_types);
while let Some(tipo) = held_types.pop() {
*tipo = find_and_replace_generics(tipo, mono_types)

View File

@ -1,5 +1,5 @@
use indexmap::IndexSet;
use std::{borrow::BorrowMut, slice::Iter, sync::Arc};
use std::{borrow::BorrowMut, path, slice::Iter, sync::Arc};
use uplc::{builder::EXPECT_ON_LIST, builtins::DefaultFunction};
use crate::{
@ -24,8 +24,8 @@ impl TreePath {
self.path.push((depth, index));
}
pub fn pop(&mut self) {
self.path.pop();
pub fn pop(&mut self) -> Option<(usize, usize)> {
self.path.pop()
}
pub fn current_path(&self) -> Self {
@ -1333,6 +1333,16 @@ impl AirTree {
self.do_traverse_tree_with(&mut tree_path, 0, 0, with);
}
pub fn traverse_tree_with_path(
&mut self,
path: &mut TreePath,
current_depth: usize,
depth_index: usize,
with: &mut impl FnMut(&mut AirTree, &TreePath),
) {
self.do_traverse_tree_with(path, current_depth, depth_index, with);
}
fn do_traverse_tree_with(
&mut self,
tree_path: &mut TreePath,