commit working changes so far

This commit is contained in:
Kasey White 2023-02-05 15:35:32 -05:00 committed by Lucas
parent 31cd19f198
commit c32a9d7b6f
3 changed files with 121 additions and 65 deletions

View File

@ -70,6 +70,14 @@ impl<'a> CodeGenerator<'a> {
} }
} }
pub fn reset(&mut self) {
self.needs_field_access = false;
self.used_data_assert_on_list = false;
self.zero_arg_functions = IndexMap::new();
self.id_gen = IdGenerator::new();
self.defined_functions = IndexMap::new();
}
pub fn generate( pub fn generate(
&mut self, &mut self,
body: &TypedExpr, body: &TypedExpr,
@ -83,8 +91,12 @@ impl<'a> CodeGenerator<'a> {
self.define_ir(&mut ir_stack); self.define_ir(&mut ir_stack);
println!("{ir_stack:#?}");
self.convert_opaque_type_to_inner_ir(&mut 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); let mut term = self.uplc_code_gen(&mut ir_stack);
if self.needs_field_access { if self.needs_field_access {
@ -113,6 +125,8 @@ impl<'a> CodeGenerator<'a> {
term, term,
}; };
println!("{}", program.to_pretty());
program = aiken_optimize_and_intern(program); program = aiken_optimize_and_intern(program);
program program
@ -302,13 +316,16 @@ impl<'a> CodeGenerator<'a> {
let mut value_scope = scope.clone(); let mut value_scope = scope.clone();
value_scope.push(self.id_gen.next()); value_scope.push(self.id_gen.next());
let mut replaced_type = tipo.clone();
replace_opaque_type(&mut replaced_type, self.data_types.clone());
self.build_ir(value, &mut value_vec, value_scope); self.build_ir(value, &mut value_vec, value_scope);
self.assignment_ir( self.assignment_ir(
pattern, pattern,
&mut pattern_vec, &mut pattern_vec,
&mut value_vec, &mut value_vec,
tipo, &replaced_type,
AssignmentProperties { AssignmentProperties {
value_type: value.tipo(), value_type: value.tipo(),
kind: *kind, kind: *kind,
@ -326,6 +343,8 @@ impl<'a> CodeGenerator<'a> {
// assuming one subject at the moment // assuming one subject at the moment
let subject = subjects[0].clone(); let subject = subjects[0].clone();
let mut replaced_type = subject.tipo();
replace_opaque_type(&mut replaced_type, self.data_types.clone());
if clauses.len() <= 1 { if clauses.len() <= 1 {
let mut value_vec: Vec<Air> = vec![]; let mut value_vec: Vec<Air> = vec![];
let mut pattern_vec: Vec<Air> = vec![]; let mut pattern_vec: Vec<Air> = vec![];
@ -339,7 +358,7 @@ impl<'a> CodeGenerator<'a> {
&clauses[0].pattern[0], &clauses[0].pattern[0],
&mut pattern_vec, &mut pattern_vec,
&mut subject_vec, &mut subject_vec,
&subject.tipo(), &replaced_type,
AssignmentProperties { AssignmentProperties {
value_type: clauses[0].then.tipo(), value_type: clauses[0].then.tipo(),
kind: AssignmentKind::Let, kind: AssignmentKind::Let,
@ -350,7 +369,8 @@ impl<'a> CodeGenerator<'a> {
ir_stack.append(&mut pattern_vec); ir_stack.append(&mut pattern_vec);
ir_stack.append(&mut value_vec); ir_stack.append(&mut value_vec);
} else { } else {
let clauses = if matches!(clauses[0].pattern[0], Pattern::List { .. }) { // HERE TODO
let clauses = if replaced_type.is_list() {
rearrange_clauses(clauses.clone()) rearrange_clauses(clauses.clone())
} else { } else {
clauses.clone() clauses.clone()
@ -360,7 +380,7 @@ impl<'a> CodeGenerator<'a> {
let mut pattern_vec = vec![]; let mut pattern_vec = vec![];
let mut clause_properties = ClauseProperties::init( let mut clause_properties = ClauseProperties::init(
&subject.tipo(), &replaced_type,
constr_var.clone(), constr_var.clone(),
subject_name.clone(), subject_name.clone(),
); );
@ -369,7 +389,7 @@ impl<'a> CodeGenerator<'a> {
&mut pattern_vec, &mut pattern_vec,
&mut clause_properties, &mut clause_properties,
clauses, clauses,
&subject.tipo(), &replaced_type,
scope.clone(), scope.clone(),
); );
@ -395,7 +415,7 @@ impl<'a> CodeGenerator<'a> {
last_pattern, last_pattern,
&mut pattern_vec, &mut pattern_vec,
&mut final_clause_vec, &mut final_clause_vec,
&subject.tipo(), &replaced_type,
&mut clause_properties, &mut clause_properties,
final_scope, final_scope,
); );
@ -411,7 +431,7 @@ impl<'a> CodeGenerator<'a> {
ir_stack.push(Air::When { ir_stack.push(Air::When {
scope: scope.clone(), scope: scope.clone(),
subject_name, subject_name,
tipo: subject.tipo(), tipo: replaced_type.clone(),
}); });
let mut scope = scope; let mut scope = scope;
@ -420,7 +440,7 @@ impl<'a> CodeGenerator<'a> {
ir_stack.push(Air::Var { ir_stack.push(Air::Var {
scope, scope,
constructor: ValueConstructor::public( constructor: ValueConstructor::public(
subject.tipo(), replaced_type,
ValueConstructorVariant::LocalVariable { ValueConstructorVariant::LocalVariable {
location: Span::empty(), location: Span::empty(),
}, },
@ -432,7 +452,7 @@ impl<'a> CodeGenerator<'a> {
ir_stack.push(Air::When { ir_stack.push(Air::When {
scope: scope.clone(), scope: scope.clone(),
subject_name, subject_name,
tipo: subject.tipo(), tipo: replaced_type,
}); });
let mut scope = scope; let mut scope = scope;
@ -892,7 +912,6 @@ impl<'a> CodeGenerator<'a> {
new_vec.append(values); new_vec.append(values);
// pattern_vec.push(value)
self.when_ir( self.when_ir(
pattern, pattern,
pattern_vec, pattern_vec,
@ -1724,8 +1743,7 @@ impl<'a> CodeGenerator<'a> {
} }
} }
if matches!(&assignment_properties.kind, AssignmentKind::Assert) { if !names.is_empty() {
} else {
pattern_vec.push(Air::ListAccessor { pattern_vec.push(Air::ListAccessor {
names, names,
tail: tail.is_some(), tail: tail.is_some(),
@ -1733,6 +1751,11 @@ impl<'a> CodeGenerator<'a> {
tipo: tipo.clone().into(), tipo: tipo.clone().into(),
check_last_item: false, check_last_item: false,
}); });
} else {
pattern_vec.push(Air::Let {
scope,
name: "_".to_string(),
})
} }
pattern_vec.append(values); pattern_vec.append(values);
@ -1747,7 +1770,7 @@ impl<'a> CodeGenerator<'a> {
let mut nested_pattern = vec![]; let mut nested_pattern = vec![];
let field_map = match constructor { let field_map = match constructor {
PatternConstructor::Record { field_map, .. } => field_map.clone().unwrap(), PatternConstructor::Record { field_map, .. } => field_map.clone(),
}; };
let mut type_map: IndexMap<usize, Arc<Type>> = IndexMap::new(); let mut type_map: IndexMap<usize, Arc<Type>> = IndexMap::new();
@ -1759,14 +1782,19 @@ impl<'a> CodeGenerator<'a> {
let arguments_index = arguments let arguments_index = arguments
.iter() .iter()
.filter_map(|item| { .enumerate()
.filter_map(|(index, item)| {
let label = item.label.clone().unwrap_or_default(); let label = item.label.clone().unwrap_or_default();
let field_index = field_map.fields.get(&label).map(|x| &x.0).unwrap_or(&0); let field_index = if let Some(field_map) = &field_map {
*field_map.fields.get(&label).map(|x| &x.0).unwrap()
} else {
index
};
self.extract_arg_and_index( self.extract_arg_and_index(
&item.value, &item.value,
*field_index, field_index,
&mut nested_pattern, &mut nested_pattern,
type_map.get(field_index).unwrap(), type_map.get(&field_index).unwrap(),
&assignment_properties, &assignment_properties,
&scope, &scope,
) )
@ -1786,6 +1814,11 @@ impl<'a> CodeGenerator<'a> {
scope, scope,
check_last_item: false, check_last_item: false,
}); });
} else {
pattern_vec.push(Air::Let {
scope,
name: "_".to_string(),
});
} }
pattern_vec.append(values); pattern_vec.append(values);
@ -1826,10 +1859,14 @@ impl<'a> CodeGenerator<'a> {
tipo: tipo.clone().into(), tipo: tipo.clone().into(),
check_last_item: true, check_last_item: true,
}); });
} else {
pattern_vec.push(Air::Let {
scope,
name: "_".to_string(),
});
} }
pattern_vec.append(values); pattern_vec.append(values);
pattern_vec.append(&mut nested_pattern); pattern_vec.append(&mut nested_pattern);
} }
} }
@ -1904,6 +1941,8 @@ impl<'a> CodeGenerator<'a> {
self.recursive_assert_tipo(tipo, &mut assert_list_vec, &name, scope.clone()); self.recursive_assert_tipo(tipo, &mut assert_list_vec, &name, scope.clone());
names.push(name);
pattern_vec.push(Air::ListAccessor { pattern_vec.push(Air::ListAccessor {
scope, scope,
tipo: tipo.clone().into(), tipo: tipo.clone().into(),
@ -2105,6 +2144,9 @@ impl<'a> CodeGenerator<'a> {
name: &str, name: &str,
scope: Vec<u64>, scope: Vec<u64>,
) { ) {
let mut tipo = tipo.clone().into();
replace_opaque_type(&mut tipo, self.data_types.clone());
if tipo.is_bool() if tipo.is_bool()
|| tipo.is_bytearray() || tipo.is_bytearray()
|| tipo.is_int() || tipo.is_int()
@ -2123,25 +2165,25 @@ impl<'a> CodeGenerator<'a> {
assert_vec.push(Air::Call { assert_vec.push(Air::Call {
scope: scope.clone(), scope: scope.clone(),
count: 2, count: 2,
tipo: tipo.clone().into(), tipo: tipo.clone(),
}); });
assert_vec.push(Air::Builtin { assert_vec.push(Air::Builtin {
scope: scope.clone(), scope: scope.clone(),
func: DefaultFunction::ChooseUnit, func: DefaultFunction::ChooseUnit,
tipo: tipo.clone().into(), tipo: tipo.clone(),
}); });
assert_vec.push(Air::Call { assert_vec.push(Air::Call {
scope: scope.clone(), scope: scope.clone(),
count: 2, count: 2,
tipo: tipo.clone().into(), tipo: tipo.clone(),
}); });
assert_vec.push(Air::Var { assert_vec.push(Air::Var {
scope: scope.clone(), scope: scope.clone(),
constructor: ValueConstructor::public( constructor: ValueConstructor::public(
tipo.clone().into(), tipo.clone(),
ValueConstructorVariant::LocalVariable { ValueConstructorVariant::LocalVariable {
location: Span::empty(), location: Span::empty(),
}, },
@ -2153,7 +2195,7 @@ impl<'a> CodeGenerator<'a> {
assert_vec.push(Air::Var { assert_vec.push(Air::Var {
scope: scope.clone(), scope: scope.clone(),
constructor: ValueConstructor::public( constructor: ValueConstructor::public(
tipo.clone().into(), tipo.clone(),
ValueConstructorVariant::LocalVariable { ValueConstructorVariant::LocalVariable {
location: Span::empty(), location: Span::empty(),
}, },
@ -2180,7 +2222,7 @@ impl<'a> CodeGenerator<'a> {
assert_vec.push(Air::Var { assert_vec.push(Air::Var {
scope: scope.clone(), scope: scope.clone(),
constructor: ValueConstructor::public( constructor: ValueConstructor::public(
tipo.clone().into(), tipo.clone(),
ValueConstructorVariant::LocalVariable { ValueConstructorVariant::LocalVariable {
location: Span::empty(), location: Span::empty(),
}, },
@ -2212,25 +2254,25 @@ impl<'a> CodeGenerator<'a> {
assert_vec.push(Air::Call { assert_vec.push(Air::Call {
scope: scope.clone(), scope: scope.clone(),
count: 2, count: 2,
tipo: tipo.clone().into(), tipo: tipo.clone(),
}); });
assert_vec.push(Air::Builtin { assert_vec.push(Air::Builtin {
scope: scope.clone(), scope: scope.clone(),
func: DefaultFunction::ChooseUnit, func: DefaultFunction::ChooseUnit,
tipo: tipo.clone().into(), tipo: tipo.clone(),
}); });
assert_vec.push(Air::Call { assert_vec.push(Air::Call {
scope: scope.clone(), scope: scope.clone(),
count: 2, count: 2,
tipo: tipo.clone().into(), tipo: tipo.clone(),
}); });
assert_vec.push(Air::Var { assert_vec.push(Air::Var {
scope: scope.clone(), scope: scope.clone(),
constructor: ValueConstructor::public( constructor: ValueConstructor::public(
tipo.clone().into(), tipo.clone(),
ValueConstructorVariant::LocalVariable { ValueConstructorVariant::LocalVariable {
location: Span::empty(), location: Span::empty(),
}, },
@ -2242,7 +2284,7 @@ impl<'a> CodeGenerator<'a> {
assert_vec.push(Air::Var { assert_vec.push(Air::Var {
scope: scope.clone(), scope: scope.clone(),
constructor: ValueConstructor::public( constructor: ValueConstructor::public(
tipo.clone().into(), tipo.clone(),
ValueConstructorVariant::LocalVariable { ValueConstructorVariant::LocalVariable {
location: Span::empty(), location: Span::empty(),
}, },
@ -2269,7 +2311,7 @@ impl<'a> CodeGenerator<'a> {
assert_vec.push(Air::Var { assert_vec.push(Air::Var {
scope: scope.clone(), scope: scope.clone(),
constructor: ValueConstructor::public( constructor: ValueConstructor::public(
tipo.clone().into(), tipo.clone(),
ValueConstructorVariant::LocalVariable { ValueConstructorVariant::LocalVariable {
location: Span::empty(), location: Span::empty(),
}, },
@ -2327,31 +2369,31 @@ impl<'a> CodeGenerator<'a> {
); );
} }
} else { } else {
let data_type = lookup_data_type_by_tipo(self.data_types.clone(), tipo).unwrap(); let data_type = lookup_data_type_by_tipo(self.data_types.clone(), &tipo).unwrap();
let new_id = self.id_gen.next(); let new_id = self.id_gen.next();
assert_vec.push(Air::Call { assert_vec.push(Air::Call {
scope: scope.clone(), scope: scope.clone(),
count: 2, count: 2,
tipo: tipo.clone().into(), tipo: tipo.clone(),
}); });
assert_vec.push(Air::Builtin { assert_vec.push(Air::Builtin {
scope: scope.clone(), scope: scope.clone(),
func: DefaultFunction::ChooseUnit, func: DefaultFunction::ChooseUnit,
tipo: tipo.clone().into(), tipo: tipo.clone(),
}); });
assert_vec.push(Air::When { assert_vec.push(Air::When {
scope: scope.clone(), scope: scope.clone(),
tipo: tipo.clone().into(), tipo: tipo.clone(),
subject_name: format!("__subject_{new_id}"), subject_name: format!("__subject_{new_id}"),
}); });
assert_vec.push(Air::Var { assert_vec.push(Air::Var {
scope: scope.clone(), scope: scope.clone(),
constructor: ValueConstructor::public( constructor: ValueConstructor::public(
tipo.clone().into(), tipo.clone(),
ValueConstructorVariant::LocalVariable { ValueConstructorVariant::LocalVariable {
location: Span::empty(), location: Span::empty(),
}, },
@ -2376,7 +2418,7 @@ impl<'a> CodeGenerator<'a> {
assert_vec.push(Air::Clause { assert_vec.push(Air::Clause {
scope: scope.clone(), scope: scope.clone(),
tipo: tipo.clone().into(), tipo: tipo.clone(),
subject_name: format!("__subject_{new_id}"), subject_name: format!("__subject_{new_id}"),
complex_clause: false, complex_clause: false,
}); });
@ -2386,23 +2428,25 @@ impl<'a> CodeGenerator<'a> {
value: index.to_string(), value: index.to_string(),
}); });
assert_vec.push(Air::FieldsExpose { if !arg_indices.is_empty() {
scope: scope.clone(), assert_vec.push(Air::FieldsExpose {
indices: arg_indices.clone(), scope: scope.clone(),
check_last_item: true, indices: arg_indices.clone(),
}); check_last_item: true,
});
assert_vec.push(Air::Var { assert_vec.push(Air::Var {
scope: scope.clone(), scope: scope.clone(),
constructor: ValueConstructor::public( constructor: ValueConstructor::public(
tipo.clone().into(), tipo.clone(),
ValueConstructorVariant::LocalVariable { ValueConstructorVariant::LocalVariable {
location: Span::empty(), location: Span::empty(),
}, },
), ),
name: name.to_owned(), name: name.to_owned(),
variant_name: String::new(), variant_name: String::new(),
}); });
}
for (_, name, tipo) in arg_indices { for (_, name, tipo) in arg_indices {
self.recursive_assert_tipo(&tipo, assert_vec, &name, scope.clone()); self.recursive_assert_tipo(&tipo, assert_vec, &name, scope.clone());
@ -3494,9 +3538,12 @@ impl<'a> CodeGenerator<'a> {
let mut arg_stack: Vec<Term<Name>> = vec![]; let mut arg_stack: Vec<Term<Name>> = vec![];
while let Some(ir_element) = ir_stack.pop() { while let Some(ir_element) = ir_stack.pop() {
println!("IR ELEMENT IS {:#?}", ir_element);
self.gen_uplc(ir_element, &mut arg_stack); self.gen_uplc(ir_element, &mut arg_stack);
}
println!("Arg STACK LEN IS {:#?} ", arg_stack.len());
}
println!("ARGSTACK FINALLY IS {:#?}", arg_stack);
arg_stack[0].clone() arg_stack[0].clone()
} }
@ -4595,7 +4642,7 @@ impl<'a> CodeGenerator<'a> {
), ),
) )
} else if tipo.is_list() || tipo.is_tuple() { } else if tipo.is_list() || tipo.is_tuple() {
unreachable!() unreachable!("{:#?}", tipo)
} else { } else {
apply_wrap( apply_wrap(
DefaultFunction::EqualsInteger.into(), DefaultFunction::EqualsInteger.into(),

View File

@ -36,6 +36,7 @@ impl Blueprint<Schema> {
let validators: Result<Vec<_>, Error> = modules let validators: Result<Vec<_>, Error> = modules
.validators() .validators()
.map(|(validator, def)| { .map(|(validator, def)| {
generator.reset();
Validator::from_checked_module(modules, generator, validator, def) Validator::from_checked_module(modules, generator, validator, def)
}) })
.collect(); .collect();

View File

@ -58,7 +58,7 @@ impl Program<Name> {
pub fn inline_reduce(self) -> Program<Name> { pub fn inline_reduce(self) -> Program<Name> {
let mut term = self.term.clone(); let mut term = self.term.clone();
inline_reduce(&mut term); inline_basic_reduce(&mut term);
Program { Program {
version: self.version, version: self.version,
term, term,
@ -120,38 +120,46 @@ fn builtin_force_reduce(term: &mut Term<Name>, builtin_map: &mut IndexMap<u8, ()
} }
} }
fn inline_reduce(term: &mut Term<Name>) { fn inline_basic_reduce(term: &mut Term<Name>) {
match term { match term {
Term::Delay(d) => { Term::Delay(d) => {
let d = Rc::make_mut(d); let d = Rc::make_mut(d);
inline_reduce(d); inline_basic_reduce(d);
} }
Term::Lambda { body, .. } => { Term::Lambda { body, .. } => {
let body = Rc::make_mut(body); let body = Rc::make_mut(body);
inline_reduce(body); inline_basic_reduce(body);
} }
Term::Apply { function, argument } => { Term::Apply { function, argument } => {
let arg = Rc::make_mut(argument); let arg = Rc::make_mut(argument);
inline_reduce(arg); inline_basic_reduce(arg);
let func = Rc::make_mut(function); let func = Rc::make_mut(function);
inline_reduce(func); inline_basic_reduce(func);
if let Term::Lambda { if let Term::Lambda {
parameter_name, parameter_name,
body, body,
} = func } = func
{ {
let mut occurrences = 0; if let replace_term @ (Term::Var(_)
var_occurrences(body, parameter_name.clone(), &mut occurrences); | Term::Constant(_)
if occurrences <= 1 { | Term::Error
*term = substitute_term(body.as_ref(), parameter_name.clone(), argument); | Term::Delay(_)
| Term::Lambda { .. }) = argument.as_ref()
{
let mut occurrences = 0;
var_occurrences(body, parameter_name.clone(), &mut occurrences);
if occurrences <= 1 {
*term =
substitute_term(body.as_ref(), parameter_name.clone(), replace_term);
}
} }
} }
} }
Term::Force(f) => { Term::Force(f) => {
let f = Rc::make_mut(f); let f = Rc::make_mut(f);
inline_reduce(f); inline_basic_reduce(f);
} }
_ => {} _ => {}
} }