Clarified that we hardcode a "main" function

in compiler data structure
This commit is contained in:
greg 2017-01-14 15:02:34 -08:00
parent 0c7099771f
commit 7188a7d33e

View File

@ -48,7 +48,7 @@ struct CompilationData {
module: LLVMModuleRef, module: LLVMModuleRef,
builder: LLVMBuilderRef, builder: LLVMBuilderRef,
variables: VariableMap, variables: VariableMap,
func: Option<LLVMValueRef>, main_function: LLVMValueRef,
} }
fn compile_ast(ast: AST, filename: &str) { 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 module = LLVMWrap::module_create_with_name("example module");
let builder = LLVMWrap::CreateBuilderInContext(context); 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 { let mut data = CompilationData {
context: context, context: context,
builder: builder, builder: builder,
module: module, module: module,
variables: names, variables: names,
func: None, main_function: main_function,
}; };
let int_type = LLVMWrap::Int64TypeInContext(data.context); let bb = LLVMWrap::AppendBasicBlockInContext(data.context, data.main_function, "entry");
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");
LLVMWrap::PositionBuilderAtEnd(builder, bb); LLVMWrap::PositionBuilderAtEnd(builder, bb);
let value = ast.codegen(&mut data); let value = ast.codegen(&mut data);
@ -189,7 +187,7 @@ impl CodeGen for Expression {
zero, zero,
"is_nonzero"); "is_nonzero");
let func: LLVMValueRef = data.func.expect("lol no function here"); let func: LLVMValueRef = data.main_function;
let then_block = let then_block =
LLVMWrap::AppendBasicBlockInContext(data.context, func, "entry"); LLVMWrap::AppendBasicBlockInContext(data.context, func, "entry");
let else_block = let else_block =