Codegen for function call; emits closures before functions

This commit is contained in:
Pranav Gaddamadugu 2022-08-04 21:03:42 -07:00
parent dfea5bbf15
commit 34be1fc39d
12 changed files with 42 additions and 26 deletions

View File

@ -273,7 +273,7 @@ impl<'a> CodeGenerator<'a> {
}
fn visit_call(&mut self, input: &'a CallExpression) -> (String, String) {
let mut call_instruction = format!(" {} ", input.function);
let mut call_instruction = format!("call {} ", input.function);
let mut instructions = String::new();
for argument in input.arguments.iter() {

View File

@ -61,14 +61,37 @@ impl<'a> CodeGenerator<'a> {
// Newline separator.
program_string.push('\n');
// Visit each `Function` in the Leo AST and produce a Aleo function instruction.
program_string.push_str(
&input
.functions
.values()
.map(|function| self.visit_function(function))
.join("\n"),
);
// Store closures and functions in separate strings.
let mut closures = String::new();
let mut functions = String::new();
// Visit each `Function` in the Leo AST and produce Aleo instructions.
input.functions.values().for_each(|function| {
// If the function is annotated with `@program`, then it is a program function.
for annotation in function.annotations.iter() {
if annotation.identifier.name == sym::program {
self.is_program_function = true;
}
}
let function_string = self.visit_function(function);
if self.is_program_function {
closures.push_str(&function_string);
closures.push('\n');
} else {
functions.push_str(&function_string);
functions.push('\n');
}
// Unset the `is_program_function` flag.
self.is_program_function = false;
});
// Closures must precede functions in the Aleo program.
program_string.push_str(&closures);
program_string.push('\n');
program_string.push_str(&functions);
program_string
}
@ -135,13 +158,6 @@ impl<'a> CodeGenerator<'a> {
}
fn visit_function(&mut self, function: &'a Function) -> String {
// If the function is annotated with `@program`, then it is a program function.
for annotation in function.annotations.iter() {
if annotation.identifier.name == sym::program {
self.is_program_function = true;
}
}
// Initialize the state of `self` with the appropriate values before visiting `function`.
self.next_register = 0;
self.variable_mapping = IndexMap::new();

View File

@ -2,4 +2,4 @@
namespace: Compile
expectation: Fail
outputs:
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 2i128 into r1;\\n output r1 as i128;\\n\", Nom(Tag)), (\"\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 2i128 into r1;\\n output r1 as i128;\\n\", Nom(Many1)), (\"function main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 2i128 into r1;\\n output r1 as i128;\\n\", Nom(Alt)), (\"function main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 2i128 into r1;\\n output r1 as i128;\\n\", Nom(Many1))] }"
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 2i128 into r1;\\n output r1 as i128;\\n\\n\", Nom(Tag)), (\"\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 2i128 into r1;\\n output r1 as i128;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 2i128 into r1;\\n output r1 as i128;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 2i128 into r1;\\n output r1 as i128;\\n\\n\", Nom(Many1))] }"

View File

@ -2,4 +2,4 @@
namespace: Compile
expectation: Fail
outputs:
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 1i128 into r1;\\n neg r1 into r2;\\n output r2 as i128;\\n\", Nom(Tag)), (\"\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 1i128 into r1;\\n neg r1 into r2;\\n output r2 as i128;\\n\", Nom(Many1)), (\"function main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 1i128 into r1;\\n neg r1 into r2;\\n output r2 as i128;\\n\", Nom(Alt)), (\"function main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 1i128 into r1;\\n neg r1 into r2;\\n output r2 as i128;\\n\", Nom(Many1))] }"
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 1i128 into r1;\\n neg r1 into r2;\\n output r2 as i128;\\n\\n\", Nom(Tag)), (\"\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 1i128 into r1;\\n neg r1 into r2;\\n output r2 as i128;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 1i128 into r1;\\n neg r1 into r2;\\n output r2 as i128;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 170141183460469231731687303715884105727i128 into r0;\\n sub r0 1i128 into r1;\\n neg r1 into r2;\\n output r2 as i128;\\n\\n\", Nom(Many1))] }"

View File

@ -2,4 +2,4 @@
namespace: Compile
expectation: Fail
outputs:
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 32767i16 into r0;\\n sub r0 2i16 into r1;\\n output r1 as i16;\\n\", Nom(Tag)), (\"\\n neg 32767i16 into r0;\\n sub r0 2i16 into r1;\\n output r1 as i16;\\n\", Nom(Many1)), (\"function main:\\n neg 32767i16 into r0;\\n sub r0 2i16 into r1;\\n output r1 as i16;\\n\", Nom(Alt)), (\"function main:\\n neg 32767i16 into r0;\\n sub r0 2i16 into r1;\\n output r1 as i16;\\n\", Nom(Many1))] }"
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 32767i16 into r0;\\n sub r0 2i16 into r1;\\n output r1 as i16;\\n\\n\", Nom(Tag)), (\"\\n neg 32767i16 into r0;\\n sub r0 2i16 into r1;\\n output r1 as i16;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 32767i16 into r0;\\n sub r0 2i16 into r1;\\n output r1 as i16;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 32767i16 into r0;\\n sub r0 2i16 into r1;\\n output r1 as i16;\\n\\n\", Nom(Many1))] }"

View File

@ -2,4 +2,4 @@
namespace: Compile
expectation: Fail
outputs:
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 32767i16 into r0;\\n sub r0 1i16 into r1;\\n neg r1 into r2;\\n output r2 as i16;\\n\", Nom(Tag)), (\"\\n neg 32767i16 into r0;\\n sub r0 1i16 into r1;\\n neg r1 into r2;\\n output r2 as i16;\\n\", Nom(Many1)), (\"function main:\\n neg 32767i16 into r0;\\n sub r0 1i16 into r1;\\n neg r1 into r2;\\n output r2 as i16;\\n\", Nom(Alt)), (\"function main:\\n neg 32767i16 into r0;\\n sub r0 1i16 into r1;\\n neg r1 into r2;\\n output r2 as i16;\\n\", Nom(Many1))] }"
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 32767i16 into r0;\\n sub r0 1i16 into r1;\\n neg r1 into r2;\\n output r2 as i16;\\n\\n\", Nom(Tag)), (\"\\n neg 32767i16 into r0;\\n sub r0 1i16 into r1;\\n neg r1 into r2;\\n output r2 as i16;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 32767i16 into r0;\\n sub r0 1i16 into r1;\\n neg r1 into r2;\\n output r2 as i16;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 32767i16 into r0;\\n sub r0 1i16 into r1;\\n neg r1 into r2;\\n output r2 as i16;\\n\\n\", Nom(Many1))] }"

View File

@ -2,4 +2,4 @@
namespace: Compile
expectation: Fail
outputs:
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 2147483647i32 into r0;\\n sub r0 2i32 into r1;\\n output r1 as i32;\\n\", Nom(Tag)), (\"\\n neg 2147483647i32 into r0;\\n sub r0 2i32 into r1;\\n output r1 as i32;\\n\", Nom(Many1)), (\"function main:\\n neg 2147483647i32 into r0;\\n sub r0 2i32 into r1;\\n output r1 as i32;\\n\", Nom(Alt)), (\"function main:\\n neg 2147483647i32 into r0;\\n sub r0 2i32 into r1;\\n output r1 as i32;\\n\", Nom(Many1))] }"
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 2147483647i32 into r0;\\n sub r0 2i32 into r1;\\n output r1 as i32;\\n\\n\", Nom(Tag)), (\"\\n neg 2147483647i32 into r0;\\n sub r0 2i32 into r1;\\n output r1 as i32;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 2147483647i32 into r0;\\n sub r0 2i32 into r1;\\n output r1 as i32;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 2147483647i32 into r0;\\n sub r0 2i32 into r1;\\n output r1 as i32;\\n\\n\", Nom(Many1))] }"

View File

@ -2,4 +2,4 @@
namespace: Compile
expectation: Fail
outputs:
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 2147483647i32 into r0;\\n sub r0 1i32 into r1;\\n neg r1 into r2;\\n output r2 as i32;\\n\", Nom(Tag)), (\"\\n neg 2147483647i32 into r0;\\n sub r0 1i32 into r1;\\n neg r1 into r2;\\n output r2 as i32;\\n\", Nom(Many1)), (\"function main:\\n neg 2147483647i32 into r0;\\n sub r0 1i32 into r1;\\n neg r1 into r2;\\n output r2 as i32;\\n\", Nom(Alt)), (\"function main:\\n neg 2147483647i32 into r0;\\n sub r0 1i32 into r1;\\n neg r1 into r2;\\n output r2 as i32;\\n\", Nom(Many1))] }"
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 2147483647i32 into r0;\\n sub r0 1i32 into r1;\\n neg r1 into r2;\\n output r2 as i32;\\n\\n\", Nom(Tag)), (\"\\n neg 2147483647i32 into r0;\\n sub r0 1i32 into r1;\\n neg r1 into r2;\\n output r2 as i32;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 2147483647i32 into r0;\\n sub r0 1i32 into r1;\\n neg r1 into r2;\\n output r2 as i32;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 2147483647i32 into r0;\\n sub r0 1i32 into r1;\\n neg r1 into r2;\\n output r2 as i32;\\n\\n\", Nom(Many1))] }"

View File

@ -2,4 +2,4 @@
namespace: Compile
expectation: Fail
outputs:
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 9223372036854775807i64 into r0;\\n sub r0 2i64 into r1;\\n output r1 as i64;\\n\", Nom(Tag)), (\"\\n neg 9223372036854775807i64 into r0;\\n sub r0 2i64 into r1;\\n output r1 as i64;\\n\", Nom(Many1)), (\"function main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 2i64 into r1;\\n output r1 as i64;\\n\", Nom(Alt)), (\"function main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 2i64 into r1;\\n output r1 as i64;\\n\", Nom(Many1))] }"
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 9223372036854775807i64 into r0;\\n sub r0 2i64 into r1;\\n output r1 as i64;\\n\\n\", Nom(Tag)), (\"\\n neg 9223372036854775807i64 into r0;\\n sub r0 2i64 into r1;\\n output r1 as i64;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 2i64 into r1;\\n output r1 as i64;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 2i64 into r1;\\n output r1 as i64;\\n\\n\", Nom(Many1))] }"

View File

@ -2,4 +2,4 @@
namespace: Compile
expectation: Fail
outputs:
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 9223372036854775807i64 into r0;\\n sub r0 1i64 into r1;\\n neg r1 into r2;\\n output r2 as i64;\\n\", Nom(Tag)), (\"\\n neg 9223372036854775807i64 into r0;\\n sub r0 1i64 into r1;\\n neg r1 into r2;\\n output r2 as i64;\\n\", Nom(Many1)), (\"function main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 1i64 into r1;\\n neg r1 into r2;\\n output r2 as i64;\\n\", Nom(Alt)), (\"function main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 1i64 into r1;\\n neg r1 into r2;\\n output r2 as i64;\\n\", Nom(Many1))] }"
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 9223372036854775807i64 into r0;\\n sub r0 1i64 into r1;\\n neg r1 into r2;\\n output r2 as i64;\\n\\n\", Nom(Tag)), (\"\\n neg 9223372036854775807i64 into r0;\\n sub r0 1i64 into r1;\\n neg r1 into r2;\\n output r2 as i64;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 1i64 into r1;\\n neg r1 into r2;\\n output r2 as i64;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 9223372036854775807i64 into r0;\\n sub r0 1i64 into r1;\\n neg r1 into r2;\\n output r2 as i64;\\n\\n\", Nom(Many1))] }"

View File

@ -2,4 +2,4 @@
namespace: Compile
expectation: Fail
outputs:
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 127i8 into r0;\\n sub r0 2i8 into r1;\\n output r1 as i8;\\n\", Nom(Tag)), (\"\\n neg 127i8 into r0;\\n sub r0 2i8 into r1;\\n output r1 as i8;\\n\", Nom(Many1)), (\"function main:\\n neg 127i8 into r0;\\n sub r0 2i8 into r1;\\n output r1 as i8;\\n\", Nom(Alt)), (\"function main:\\n neg 127i8 into r0;\\n sub r0 2i8 into r1;\\n output r1 as i8;\\n\", Nom(Many1))] }"
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 127i8 into r0;\\n sub r0 2i8 into r1;\\n output r1 as i8;\\n\\n\", Nom(Tag)), (\"\\n neg 127i8 into r0;\\n sub r0 2i8 into r1;\\n output r1 as i8;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 127i8 into r0;\\n sub r0 2i8 into r1;\\n output r1 as i8;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 127i8 into r0;\\n sub r0 2i8 into r1;\\n output r1 as i8;\\n\\n\", Nom(Many1))] }"

View File

@ -2,4 +2,4 @@
namespace: Compile
expectation: Fail
outputs:
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 127i8 into r0;\\n sub r0 1i8 into r1;\\n neg r1 into r2;\\n output r2 as i8;\\n\", Nom(Tag)), (\"\\n neg 127i8 into r0;\\n sub r0 1i8 into r1;\\n neg r1 into r2;\\n output r2 as i8;\\n\", Nom(Many1)), (\"function main:\\n neg 127i8 into r0;\\n sub r0 1i8 into r1;\\n neg r1 into r2;\\n output r2 as i8;\\n\", Nom(Alt)), (\"function main:\\n neg 127i8 into r0;\\n sub r0 1i8 into r1;\\n neg r1 into r2;\\n output r2 as i8;\\n\", Nom(Many1))] }"
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"neg 127i8 into r0;\\n sub r0 1i8 into r1;\\n neg r1 into r2;\\n output r2 as i8;\\n\\n\", Nom(Tag)), (\"\\n neg 127i8 into r0;\\n sub r0 1i8 into r1;\\n neg r1 into r2;\\n output r2 as i8;\\n\\n\", Nom(Many1)), (\"function main:\\n neg 127i8 into r0;\\n sub r0 1i8 into r1;\\n neg r1 into r2;\\n output r2 as i8;\\n\\n\", Nom(Alt)), (\"function main:\\n neg 127i8 into r0;\\n sub r0 1i8 into r1;\\n neg r1 into r2;\\n output r2 as i8;\\n\\n\", Nom(Many1))] }"