Skip to content

Commit

Permalink
Consistently make 1 empty line between output items in codegen.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebenfield committed Oct 22, 2024
1 parent b4f28bf commit da8ef9b
Show file tree
Hide file tree
Showing 686 changed files with 190 additions and 1,638 deletions.
129 changes: 52 additions & 77 deletions compiler/passes/src/code_generation/visit_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ use leo_ast::{Composite, Function, Location, Mapping, Member, Mode, Program, Pro
use leo_span::{Symbol, sym};

use indexmap::IndexMap;
use itertools::Itertools;
use std::fmt::Write as _;

const EXPECT_STR: &str = "Failed to write code";

impl<'a> CodeGenerator<'a> {
pub(crate) fn visit_program(&mut self, input: &'a Program) -> String {
// Accumulate instructions into a program string.
let mut program_string = String::new();

// Print out the dependencies of the program. Already arranged in post order by Retriever module.
input.stubs.iter().for_each(|(program_name, _)| {
program_string.push_str(&format!("import {}.aleo;\n", program_name));
writeln!(program_string, "import {}.aleo;", program_name).expect(EXPECT_STR);
});

// Retrieve the program scope.
Expand All @@ -40,11 +41,7 @@ impl<'a> CodeGenerator<'a> {
self.program_id = Some(program_scope.program_id);

// Print the program id.
writeln!(program_string, "program {};", program_scope.program_id)
.expect("Failed to write program id to string.");

// Newline separator.
program_string.push('\n');
writeln!(program_string, "program {};", program_scope.program_id).expect(EXPECT_STR);

// Get the post-order ordering of the composite data types.
// Note that the unwrap is safe since type checking guarantees that the struct dependency graph is acyclic.
Expand All @@ -68,70 +65,50 @@ impl<'a> CodeGenerator<'a> {
.collect();

// Visit each `Struct` or `Record` in the post-ordering and produce an Aleo struct or record.
program_string.push_str(
&order
.into_iter()
.map(|name| {
match structs_map.get(&name) {
// If the struct is found, it is a struct or external record.
Some(struct_) => self.visit_struct_or_record(struct_),
// If the struct is not found, it is an imported record.
None => String::new(),
}
})
.join("\n"),
);

// Newline separator.
program_string.push('\n');
for name in order.into_iter() {
if let Some(struct_) = structs_map.get(&name) {
program_string.push_str(&self.visit_struct_or_record(struct_));
}
}

// Visit each mapping in the Leo AST and produce an Aleo mapping declaration.
program_string
.push_str(&program_scope.mappings.iter().map(|(_, mapping)| self.visit_mapping(mapping)).join("\n"));
for (_symbol, mapping) in program_scope.mappings.iter() {
program_string.push_str(&self.visit_mapping(mapping));
}

// Visit each function in the program scope and produce an Aleo function.
// Note that in the function inlining pass, we reorder the functions such that they are in post-order.
// In other words, a callee function precedes its caller function in the program scope.
program_string.push_str(
&program_scope
.functions
.iter()
.map(|(_, function)| {
if function.variant != Variant::AsyncFunction {
let mut function_string = self.visit_function(function);

// Attach the associated finalize to async transitions.
if function.variant == Variant::AsyncTransition {
// Set state variables.
self.finalize_caller = Some(function.identifier.name);
// Generate code for the associated finalize function.
let finalize = &self
.symbol_table
.lookup_fn_symbol(Location::new(
Some(self.program_id.unwrap().name.name),
function.identifier.name,
))
.unwrap()
.clone()
.finalize
.unwrap()
.name;
// Write the finalize string.
function_string.push_str(&format!(
"{}\n",
&self.visit_function(
&program_scope.functions.iter().find(|(name, _f)| name == finalize).unwrap().1
)
));
}

function_string
} else {
String::new()
}
})
.join("\n"),
);
for (_symbol, function) in program_scope.functions.iter() {
// program_string.push_str(&program_scope.functions.iter().map(|(_, function)| {
if function.variant != Variant::AsyncFunction {
let mut function_string = self.visit_function(function);

// Attach the associated finalize to async transitions.
if function.variant == Variant::AsyncTransition {
// Set state variables.
self.finalize_caller = Some(function.identifier.name);
// Generate code for the associated finalize function.
let finalize = &self
.symbol_table
.lookup_fn_symbol(Location::new(
Some(self.program_id.unwrap().name.name),
function.identifier.name,
))
.unwrap()
.clone()
.finalize
.unwrap()
.name;
// Write the finalize string.
function_string.push_str(&self.visit_function(
&program_scope.functions.iter().find(|(name, _f)| name == finalize).unwrap().1,
));
}

program_string.push_str(&function_string);
}
}

program_string
}
Expand All @@ -144,22 +121,21 @@ impl<'a> CodeGenerator<'a> {
// Add private symbol to composite types.
self.composite_mapping.insert(&struct_.identifier.name, (false, String::from("private"))); // todo: private by default here.

let mut output_string = format!("struct {}:\n", struct_.identifier); // todo: check if this is safe from name conflicts.
let mut output_string = format!("\nstruct {}:\n", struct_.identifier); // todo: check if this is safe from name conflicts.

// Construct and append the record variables.
for var in struct_.members.iter() {
writeln!(output_string, " {} as {};", var.identifier, Self::visit_type(&var.type_),)
.expect("failed to write to string");
writeln!(output_string, " {} as {};", var.identifier, Self::visit_type(&var.type_),).expect(EXPECT_STR);
}

output_string
}

fn visit_record(&mut self, record: &'a Composite) -> String {
// Add record symbol to composite types.
let mut output_string = String::from("record");
self.composite_mapping.insert(&record.identifier.name, (true, output_string.clone()));
writeln!(output_string, " {}:", record.identifier).expect("failed to write to string"); // todo: check if this is safe from name conflicts.
self.composite_mapping.insert(&record.identifier.name, (true, "record".into()));

let mut output_string = format!("\nrecord {}:\n", record.identifier); // todo: check if this is safe from name conflicts.

let mut members = Vec::with_capacity(record.members.len());
let mut member_map: IndexMap<Symbol, Member> =
Expand All @@ -185,7 +161,7 @@ impl<'a> CodeGenerator<'a> {
var.identifier,
Self::visit_type(&var.type_)
)
.expect("failed to write to string");
.expect(EXPECT_STR);
}

output_string
Expand All @@ -210,7 +186,7 @@ impl<'a> CodeGenerator<'a> {
Variant::Transition | Variant::AsyncTransition => format!("\nfunction {}:\n", function.identifier),
Variant::Function => format!("\nclosure {}:\n", function.identifier),
Variant::AsyncFunction => format!("\nfinalize {}:\n", self.finalize_caller.unwrap()),
Variant::Inline => return String::from("\n"),
Variant::Inline => return String::new(),
};

// Construct and append the input declarations of the function.
Expand Down Expand Up @@ -241,8 +217,7 @@ impl<'a> CodeGenerator<'a> {
}
};

writeln!(function_string, " input {register_string} as {type_string};",)
.expect("failed to write to string");
writeln!(function_string, " input {register_string} as {type_string};",).expect(EXPECT_STR);
}

// Construct and append the function body.
Expand Down Expand Up @@ -275,10 +250,10 @@ impl<'a> CodeGenerator<'a> {
};

// Create the key string, e.g. ` key as address.public`.
mapping_string.push_str(&format!("\tkey as {};\n", create_type(&mapping.key_type)));
writeln!(mapping_string, " key as {};", create_type(&mapping.key_type)).expect(EXPECT_STR);

// Create the value string, e.g. ` value as address.public`.
mapping_string.push_str(&format!("\tvalue as {};\n", create_type(&mapping.value_type)));
writeln!(mapping_string, " value as {};", create_type(&mapping.value_type)).expect(EXPECT_STR);

// Add the mapping to the variable mapping.
self.global_mapping.insert(&mapping.identifier.name, mapping.identifier.to_string());
Expand Down
2 changes: 0 additions & 2 deletions tests/expectations/compiler/address/binary.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ expectation = "Pass"
outputs = [[{ compile = [{ initial_symbol_table = "756e2b87734bb537caa46fae8a2b650aab26e96062df959e32e4828535c6affd", type_checked_symbol_table = "d53bb8960397c6ee70314bcd5a30dbb59d655bda52b4937c16a94af0417fe793", unrolled_symbol_table = "d53bb8960397c6ee70314bcd5a30dbb59d655bda52b4937c16a94af0417fe793", initial_ast = "3e0dce3c7ac38e237c811a557ddf5422d92024cd3a2f9a050f5089fb49e1c0d2", unrolled_ast = "3e0dce3c7ac38e237c811a557ddf5422d92024cd3a2f9a050f5089fb49e1c0d2", ssa_ast = "e4e399f95f533afdcd018463d8a27bc573fcc02dfd11b0e32990e690b98584da", flattened_ast = "2b95ef75e175131a082bc796f2b57942c2fb395373c91ef5fbbf1ed70d80a2c3", destructured_ast = "6b932fa3264ea209145cb10679089bb14e6f5e667c8cff3b9adef16424e70646", inlined_ast = "6b932fa3264ea209145cb10679089bb14e6f5e667c8cff3b9adef16424e70646", dce_ast = "23001f440ab4c99e89ac05facdfe45b10206fcc86a80bb11f8108c9b3785151b", bytecode = """
program test.aleo;



function main:
input r0 as address.private;
is.neq r0 aleo1fj982yqchhy973kz7e9jk6er7t6qd6jm9anplnlprem507w6lv9spwvfxx into r1;
Expand Down
2 changes: 0 additions & 2 deletions tests/expectations/compiler/address/branch.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ expectation = "Pass"
outputs = [[{ compile = [{ initial_symbol_table = "aa5665d4b9e05e78e9181f1bb7e07f98f0009c1f82b29b781173679d0392f75c", type_checked_symbol_table = "c9a355adf84dd491014c8150453db6edb824f841877c1e912bc659ca15416723", unrolled_symbol_table = "c9a355adf84dd491014c8150453db6edb824f841877c1e912bc659ca15416723", initial_ast = "55ccd130c0fb8317c3ccd6eeafdd9630fdab2447c4368c0f6870ce0f81a60e82", unrolled_ast = "55ccd130c0fb8317c3ccd6eeafdd9630fdab2447c4368c0f6870ce0f81a60e82", ssa_ast = "ec3c124600b30e1bbe9c2745037f4a841ad4d12e9b4ce41f446faa1d7b34be6c", flattened_ast = "a47513dc4d7f5aadd738b38b152d16e73e4d41b4d3329b9b2ccab72dfc7c8528", destructured_ast = "67b5a3b7d55cf41ff4af1069e3a6a2d4b2e5b4adeccfc145e24db5018111499b", inlined_ast = "67b5a3b7d55cf41ff4af1069e3a6a2d4b2e5b4adeccfc145e24db5018111499b", dce_ast = "3c317e1a5194206c1a6a41ffb463da578330bf0040ecae4e9cdccbc1c16c9d22", bytecode = """
program test.aleo;



function main:
input r0 as address.private;
input r1 as boolean.private;
Expand Down
2 changes: 0 additions & 2 deletions tests/expectations/compiler/address/equal.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ expectation = "Pass"
outputs = [[{ compile = [{ initial_symbol_table = "af1b9dd4e2e7f82e1a536bfc53948de3c814fc9ccf61e5115c2109e09cfb2b67", type_checked_symbol_table = "a87370905b296fa38a2921ce60df21da4670958bcf91d6c236acf70d02343087", unrolled_symbol_table = "a87370905b296fa38a2921ce60df21da4670958bcf91d6c236acf70d02343087", initial_ast = "a1b18ca13abd5d553005007013851ea090ce27a325f360f36a087fd7125b1c9b", unrolled_ast = "a1b18ca13abd5d553005007013851ea090ce27a325f360f36a087fd7125b1c9b", ssa_ast = "048c531ce2a9cbecfa2e1ea0479ff3e245adcac3641843092083354074a3eeab", flattened_ast = "01c4814b6404c3801fedfd3e54056cb765af01f26209407347826bc3651f9adc", destructured_ast = "6ec15e189f4ff47f7b8b18aad652dfb6d440415341b6e8df1f18706f80d9c8b4", inlined_ast = "6ec15e189f4ff47f7b8b18aad652dfb6d440415341b6e8df1f18706f80d9c8b4", dce_ast = "6ec15e189f4ff47f7b8b18aad652dfb6d440415341b6e8df1f18706f80d9c8b4", bytecode = """
program test.aleo;



function main:
input r0 as address.private;
is.eq r0 aleo10qerras5799u6k7rjtc9y3hcwxuykr45qra7x7dp6jgnc0923czqm0lgta into r1;
Expand Down
6 changes: 2 additions & 4 deletions tests/expectations/compiler/address/special_address.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ expectation = "Pass"
outputs = [[{ compile = [{ initial_symbol_table = "9093a20a62879f03d6a7a4dc24b051778d2c543d1df6a59af8c423d047ec2e13", type_checked_symbol_table = "639ed2aefe557daf6c3433d645bec25030a82e3296f26ac46961f451d2bb28ae", unrolled_symbol_table = "639ed2aefe557daf6c3433d645bec25030a82e3296f26ac46961f451d2bb28ae", initial_ast = "da0a545e9de3b8cdec100132e7d9886d08b7e69217d129229e8a86489199ec21", unrolled_ast = "da0a545e9de3b8cdec100132e7d9886d08b7e69217d129229e8a86489199ec21", ssa_ast = "f76c8e4b70096ec05a1583648b295127d0a60d8b9973a1d5c7dff53782282c1d", flattened_ast = "d743a19b1faa6b66b90bd8496249cc5b94e9ba8260ad7ce95da721b68e59c3a9", destructured_ast = "d36731bec3ce6fd1c46b5660340e8e396fb874b0c3d3f6ae407e5f55aeb07aa4", inlined_ast = "d36731bec3ce6fd1c46b5660340e8e396fb874b0c3d3f6ae407e5f55aeb07aa4", dce_ast = "d36731bec3ce6fd1c46b5660340e8e396fb874b0c3d3f6ae407e5f55aeb07aa4", bytecode = """
program test.aleo;



mapping Yo:
\tkey as address.public;
\tvalue as u32.public;
key as address.public;
value as u32.public;

function main:
input r0 as address.private;
Expand Down
2 changes: 0 additions & 2 deletions tests/expectations/compiler/address/ternary.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ expectation = "Pass"
outputs = [[{ compile = [{ initial_symbol_table = "af1b9dd4e2e7f82e1a536bfc53948de3c814fc9ccf61e5115c2109e09cfb2b67", type_checked_symbol_table = "a7e0e6793233e6dec4b48137418a894ee322ac0635bccf5376979766adf2bbfa", unrolled_symbol_table = "a7e0e6793233e6dec4b48137418a894ee322ac0635bccf5376979766adf2bbfa", initial_ast = "fb145169ad1e437da9a503342918f5793085d4f5355837ac261984c33042831c", unrolled_ast = "fb145169ad1e437da9a503342918f5793085d4f5355837ac261984c33042831c", ssa_ast = "315ce58a6c118e5955412d56ab3d52990475bf2452b8d8e21aba7c7628f59e84", flattened_ast = "f90ae68ee0bcbd72c41672353efc856b34f166a20a6de512fb70f8217ec5051e", destructured_ast = "afb641b9398c8b97833609f67e51483893d56505c6c2e64ed4173e79ad226e91", inlined_ast = "afb641b9398c8b97833609f67e51483893d56505c6c2e64ed4173e79ad226e91", dce_ast = "afb641b9398c8b97833609f67e51483893d56505c6c2e64ed4173e79ad226e91", bytecode = """
program test.aleo;



function main:
input r0 as address.private;
is.eq r0 aleo1l7ytv5jqjzpxtjqttl5z9mle8ujcpac9t6tkge5f4haah4pxas8sagzecd into r1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ expectation = "Pass"
outputs = [[{ compile = [{ initial_symbol_table = "86cc47c4ae9edafe0811fe203d260ee357a3f7853a2ffbf0a9afc8e911369f51", type_checked_symbol_table = "d27200bee59ed91be0d42e9c04b8b89a8afb8a8518db3372f0ff65d097967144", unrolled_symbol_table = "168951779efa956128e2892c5c3d538b3f34cabec54dd8efb70c4105a22f8edb", initial_ast = "e638024190743e42d22971bf4a5d2ae071661a5c05a9774f97763362531c3f99", unrolled_ast = "9e599c1b6e6ab0b8f32f83b2cb2e327848eb02bca60c782ae40a0bd051bc9662", ssa_ast = "a43e3ef81113c1d9c70cfbfef5052b4fa26eb2feb8094ce60dd68aff58d6c2b7", flattened_ast = "8b353b3efece3f3c00a6742a9706db425d1c93cb2f69be28071917304212d43f", destructured_ast = "a673e29723f018691dd0f9585836e9b8361b043b1d514961837f0c6099f54eef", inlined_ast = "a673e29723f018691dd0f9585836e9b8361b043b1d514961837f0c6099f54eef", dce_ast = "a673e29723f018691dd0f9585836e9b8361b043b1d514961837f0c6099f54eef", bytecode = """
program test.aleo;



function foo:
input r0 as [boolean; 4u32].private;
assert.eq r0[0u32] true;
Expand Down
2 changes: 0 additions & 2 deletions tests/expectations/compiler/array/array_access.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ expectation = "Pass"
outputs = [[{ compile = [{ initial_symbol_table = "bbff18546983d05503a84e82edfc8ba43a819761a4966180f364b658bee7c71c", type_checked_symbol_table = "68601235ddc6c200098d7374a78da4acbb29d3d5d1a6d67bdb7b5baa7ddfce4c", unrolled_symbol_table = "68601235ddc6c200098d7374a78da4acbb29d3d5d1a6d67bdb7b5baa7ddfce4c", initial_ast = "994bad0eab3a9fd89b319668c2e45551fc8ca2d0722e751ef8ece6eddd7d11bf", unrolled_ast = "994bad0eab3a9fd89b319668c2e45551fc8ca2d0722e751ef8ece6eddd7d11bf", ssa_ast = "cf69c0c5a757a9387c0100f3cac9ed3292a1b4189a872f7d70cefec4b687f2ec", flattened_ast = "f83a1302e81bb2ad8737ee001d3e577106774175b87186f4237500db6a1e4e4d", destructured_ast = "c5f3b9d50a5b6bab1b5a992608653b865f619ec19e95f42fef43ac709f4b3134", inlined_ast = "c5f3b9d50a5b6bab1b5a992608653b865f619ec19e95f42fef43ac709f4b3134", dce_ast = "c5f3b9d50a5b6bab1b5a992608653b865f619ec19e95f42fef43ac709f4b3134", bytecode = """
program test.aleo;



function foo:
input r0 as [boolean; 8u32].private;
output r0[0u32] as boolean.private;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ record floo:
owner as address.private;
data as [u8; 8u32].private;


function foo:
input r0 as [[boolean; 8u32]; 8u32].private;
cast 1u8 1u8 1u8 1u8 1u8 1u8 1u8 1u8 into r1 as [u8; 8u32];
Expand Down
4 changes: 0 additions & 4 deletions tests/expectations/compiler/array/array_in_finalize.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ expectation = "Pass"
outputs = [[{ compile = [{ initial_symbol_table = "ba3fc19ac12ac6857103131150087f2008db2ed17e29f81333e71448b7460bdc", type_checked_symbol_table = "e9456a96681c99f57e277529196368b924eb914dcc1e747246eceaa6fe3cda30", unrolled_symbol_table = "e9456a96681c99f57e277529196368b924eb914dcc1e747246eceaa6fe3cda30", initial_ast = "d7838abbe0ad26b7a8bc6eae9ab9c0f7be4d180426d060f86e4bb158b7f177a0", unrolled_ast = "d7838abbe0ad26b7a8bc6eae9ab9c0f7be4d180426d060f86e4bb158b7f177a0", ssa_ast = "d7d54b8076d9dfe9cf0d87015531ce37dcf3af5bc2849ba783ccbedef88dbfcb", flattened_ast = "579cc01e705c2b6d9d1e7622c34a034e3328b8e625fbe0bf541b7bd54ae49d17", destructured_ast = "db35f913e14ba042c5c5010eecc9e0bbdf46c3e73ca91b302ea8578dc4f60b3e", inlined_ast = "ebb9563fb18a8a38343a1e264ba17b7b4285f36016a52bde85777fb66b8308e1", dce_ast = "ebb9563fb18a8a38343a1e264ba17b7b4285f36016a52bde85777fb66b8308e1", bytecode = """
program test.aleo;




function foo:
input r0 as [boolean; 8u32].private;
async foo r0 into r1;
Expand All @@ -14,5 +11,4 @@ function foo:
finalize foo:
input r0 as [boolean; 8u32].public;
assert.eq true true;

""", errors = "", warnings = "" }] }]]
Loading

0 comments on commit da8ef9b

Please sign in to comment.