From 763ad9f5d2a3d391869af3ac5263bc05ba8fc755 Mon Sep 17 00:00:00 2001 From: Daniel Dominguez Date: Fri, 15 Dec 2023 15:29:21 -0600 Subject: [PATCH] Add main function to generated code --- compiler/src/circuit_design/circuit.rs | 39 +++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/compiler/src/circuit_design/circuit.rs b/compiler/src/circuit_design/circuit.rs index 8101a4b0d..6026f299f 100644 --- a/compiler/src/circuit_design/circuit.rs +++ b/compiler/src/circuit_design/circuit.rs @@ -10,11 +10,11 @@ use code_producers::c_elements::*; use code_producers::llvm_elements::array_switch::{load_array_stores_fns, load_array_load_fns}; use code_producers::llvm_elements::*; use code_producers::llvm_elements::fr::load_fr; -use code_producers::llvm_elements::functions::{ - create_function, FunctionLLVMIRProducer, ExtractedFunctionLLVMIRProducer, -}; +use code_producers::llvm_elements::functions::{create_function, FunctionLLVMIRProducer, ExtractedFunctionLLVMIRProducer, create_bb}; +use code_producers::llvm_elements::instructions::{create_alloca, create_call, create_gep, create_load, create_return_void}; use code_producers::llvm_elements::stdlib::{load_stdlib, GENERATED_FN_PREFIX}; -use code_producers::llvm_elements::types::{bigint_type, void_type}; +use code_producers::llvm_elements::types::{bigint_type, i32_type, void_type}; +use code_producers::llvm_elements::values::zero; use code_producers::wasm_elements::*; use program_structure::program_archive::ProgramArchive; @@ -156,6 +156,37 @@ impl WriteLLVMIR for Circuit { // Code for prologue + // Main function + { + let main_fn = create_function(producer, + &None, + 0, + "", + "main", + void_type(producer).fn_type(&[], false)); + main_fn.set_linkage(Linkage::External); + let main = create_bb(producer, main_fn, "main"); + producer.set_current_bb(main); + + let bigint_ptr = bigint_type(producer).array_type(0); + // Set the type of the component memory: signals array + signals counter + let component_memory = producer.context().struct_type(&[ + to_basic_type_enum(bigint_ptr.ptr_type(Default::default())), + to_basic_type_enum(i32_type(producer)) + ], false); + let alloca = create_alloca(producer, + component_memory.as_any_type_enum(), + "").into_pointer_value(); + let _ = create_call(producer, + build_fn_name(producer.get_main_template_header().clone()).as_str(), + &[alloca.into()]); + let signals_ptr = create_gep(producer, alloca, &[zero(producer), zero(producer)]).into_pointer_value(); + let signals = create_load(producer, signals_ptr).into_pointer_value(); + let _ = create_call(producer, + run_fn_name(producer.get_main_template_header().clone()).as_str(), + &[signals.into()]); + let _ = create_return_void(producer); + } None // No need to return at this level } }