From 7188a7d33e0bfd318da52784a188828555d96ad9 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 14 Jan 2017 15:02:34 -0800 Subject: [PATCH] Clarified that we hardcode a "main" function in compiler data structure --- src/compilation.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/compilation.rs b/src/compilation.rs index e32f1c5..dce7ab4 100644 --- a/src/compilation.rs +++ b/src/compilation.rs @@ -48,7 +48,7 @@ struct CompilationData { module: LLVMModuleRef, builder: LLVMBuilderRef, variables: VariableMap, - func: Option, + main_function: LLVMValueRef, } fn compile_ast(ast: AST, filename: &str) { @@ -59,21 +59,19 @@ fn compile_ast(ast: AST, filename: &str) { let module = LLVMWrap::module_create_with_name("example module"); let builder = LLVMWrap::CreateBuilderInContext(context); + let program_return_type = LLVMWrap::Int64TypeInContext(context); + let main_function_type = LLVMWrap::FunctionType(program_return_type, &Vec::new(), false); + let main_function: LLVMValueRef = LLVMWrap::AddFunction(module, "main", main_function_type); + let mut data = CompilationData { context: context, builder: builder, module: module, variables: names, - func: None, + main_function: main_function, }; - let int_type = LLVMWrap::Int64TypeInContext(data.context); - let function_type = LLVMWrap::FunctionType(int_type, &Vec::new(), false); - let function = LLVMWrap::AddFunction(data.module, "main", function_type); - - data.func = Some(function); - - let bb = LLVMWrap::AppendBasicBlockInContext(data.context, function, "entry"); + let bb = LLVMWrap::AppendBasicBlockInContext(data.context, data.main_function, "entry"); LLVMWrap::PositionBuilderAtEnd(builder, bb); let value = ast.codegen(&mut data); @@ -189,7 +187,7 @@ impl CodeGen for Expression { zero, "is_nonzero"); - let func: LLVMValueRef = data.func.expect("lol no function here"); + let func: LLVMValueRef = data.main_function; let then_block = LLVMWrap::AppendBasicBlockInContext(data.context, func, "entry"); let else_block =