diff options
Diffstat (limited to 'src/compiler.c')
-rw-r--r-- | src/compiler.c | 59 |
1 files changed, 34 insertions, 25 deletions
diff --git a/src/compiler.c b/src/compiler.c index 228cb74..803515b 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -273,7 +273,7 @@ static CHECK_RESULT int thread_generate_ssa(Parser *parser) { int result; return_if_error(arena_allocator_alloc(parser->allocator, sizeof(Ssa), (void**)&compiler_context.ssa)); - return_if_error(ssa_init(compiler_context.ssa, parser->allocator)); + return_if_error(ssa_init(compiler_context.ssa, parser)); compiler_context.compiler = parser->compiler; parser->ssa = compiler_context.ssa; amal_log_debug("Generating SSA for file: %.*s", parser->tokenizer.code_name.size, parser->tokenizer.code_name.data); @@ -549,6 +549,36 @@ int amal_compiler_load_file(amal_compiler_options *options, amal_program *progra return result; } +/* TODO: Verify main func has correct signature */ +static CHECK_RESULT int validate_main_func(FileScopeReference *main_file_scope, LhsExpr **main_func) { + const BufferView main_func_name = { "main", 4 }; + LhsExpr *main_func_expr; + + main_func_expr = structdecl_get_field_by_name(&main_file_scope->parser->struct_decl, main_func_name); + if(!main_func_expr) { + amal_log_error("main function missing from start file \"%.*s\"", main_file_scope->canonical_path.size, main_file_scope->canonical_path.data); + return AMAL_COMPILER_ERR; + } + *main_func = main_func_expr; + + if(!main_func_expr->rhs_expr || main_func_expr->rhs_expr->type != AST_FUNCTION_DECL) { + amal_log_error("main exists in start file \"%.*s\" but it's not an non-extern function", main_file_scope->canonical_path.size, main_file_scope->canonical_path.data); + return AMAL_COMPILER_ERR; + } + + if(!LHS_EXPR_IS_CONST(main_func_expr)) { + amal_log_error("main function in start file \"%.*s\" has to be const", main_file_scope->canonical_path.size, main_file_scope->canonical_path.data); + return AMAL_COMPILER_ERR; + } + + if(LHS_EXPR_IS_EXTERN(main_func_expr)) { + amal_log_error("main function in start file \"%.*s\" can't be declared as extern", main_file_scope->canonical_path.size, main_file_scope->canonical_path.data); + return AMAL_COMPILER_ERR; + } + + return 0; +} + int amal_compiler_internal_load_file(amal_compiler *self, const char *filepath, FileScopeReference **file_scope) { int result; ParserThreadData *parser_thread_data; @@ -587,39 +617,18 @@ int amal_compiler_internal_load_file(amal_compiler *self, const char *filepath, and writing it to a file, which is an IO bottlenecked operation and it won't benefit from multithreading and may even lose performance because of it. */ - const BufferView main_func_name = { "main", 4 }; - LhsExpr *main_func_expr; + LhsExpr *main_func; return_if_error(amal_compiler_load_file_join_threads(self)); assert(amal_compiler_check_all_threads_done(self)); amal_log_info("Finished parsing all files, resolving AST"); - main_func_expr = structdecl_get_field_by_name(&(*file_scope)->parser->struct_decl, main_func_name); - if(!main_func_expr) { - amal_log_error("main function missing from start file \"%.*s\"", (*file_scope)->canonical_path.size, (*file_scope)->canonical_path.data); - return AMAL_COMPILER_ERR; - } - - if(!main_func_expr->rhs_expr || main_func_expr->rhs_expr->type != AST_FUNCTION_DECL) { - amal_log_error("main exists in start file \"%.*s\" but it's not an non-extern function", (*file_scope)->canonical_path.size, (*file_scope)->canonical_path.data); - return AMAL_COMPILER_ERR; - } - - if(!LHS_EXPR_IS_CONST(main_func_expr)) { - amal_log_error("main function in start file \"%.*s\" has to be const", (*file_scope)->canonical_path.size, (*file_scope)->canonical_path.data); - return AMAL_COMPILER_ERR; - } - - if(LHS_EXPR_IS_EXTERN(main_func_expr)) { - amal_log_error("main function in start file \"%.*s\" can't be declared as extern", (*file_scope)->canonical_path.size, (*file_scope)->canonical_path.data); - return AMAL_COMPILER_ERR; - } - + return_if_error(validate_main_func(*file_scope, &main_func)); /* The main function is the start file needs to be exported, so it's accessible in the program execution to find the entry (main) function. */ - main_func_expr->decl_flags |= DECL_FLAG_EXPORT; + main_func->decl_flags |= DECL_FLAG_EXPORT; return_if_error(amal_compiler_dispatch_generic(self, THREAD_WORK_RESOLVE_AST)); assert(amal_compiler_check_all_threads_done(self)); |