Skip to content

Commit

Permalink
Add main function to generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
0xddom committed Dec 15, 2023
1 parent 4a080f9 commit 763ad9f
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions compiler/src/circuit_design/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit 763ad9f

Please sign in to comment.