From c207014f81fe742675c6e869b59d09e916c69fc6 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 24 Mar 2019 01:19:18 +0100 Subject: Resolve cross-file references (with mutex). Not done --- src/compiler.c | 57 ++++++++++++++++++++++++++------------------------------- 1 file changed, 26 insertions(+), 31 deletions(-) (limited to 'src/compiler.c') diff --git a/src/compiler.c b/src/compiler.c index cc35d1e..1feaf86 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -13,11 +13,6 @@ #define MIN(a, b) ((a) < (b) ? (a) : (b)) -typedef struct { - BufferView path; - Scope *file_scope; -} FileQueueItem; - static CHECK_RESULT int get_thread_count_env_var(int *thread_count) { char *threads; threads = getenv("THREADS"); @@ -38,39 +33,38 @@ static usize strnlen(const char *str, usize max_length) { } /* TODO: Allow to specify size and members? */ -static CHECK_RESULT int create_default_type(amal_compiler *compiler, const char *name) { +static CHECK_RESULT int create_default_type(amal_compiler *compiler, const char *name, LhsExpr **lhs_expr) { StructDecl *struct_decl; - LhsExpr *lhs_expr; Ast *expr; return_if_error(scoped_allocator_alloc(&compiler->allocator, sizeof(StructDecl), (void**)&struct_decl)); return_if_error(structdecl_init(struct_decl, &compiler->root_scope, &compiler->allocator)); - return_if_error(scoped_allocator_alloc(&compiler->allocator, sizeof(LhsExpr), (void**)&lhs_expr)); - return_if_error(lhsexpr_init(lhs_expr, bool_true, bool_true, + return_if_error(scoped_allocator_alloc(&compiler->allocator, sizeof(LhsExpr), (void**)lhs_expr)); + return_if_error(lhsexpr_init(*lhs_expr, bool_true, bool_true, create_buffer_view(name, strnlen(name, PATH_MAX)), - NULL)); - return_if_error(ast_create(&compiler->allocator, struct_decl, AST_STRUCT_DECL, &lhs_expr->rhs_expr)); - return_if_error(ast_create(&compiler->allocator, lhs_expr, AST_LHS, &expr)); - expr->resolve_data.type = lhs_expr; + &compiler->allocator)); + return_if_error(ast_create(&compiler->allocator, struct_decl, AST_STRUCT_DECL, &(*lhs_expr)->rhs_expr)); + return_if_error(ast_create(&compiler->allocator, *lhs_expr, AST_LHS, &expr)); + expr->resolve_data.type = *lhs_expr; expr->resolve_data.status = AST_RESOLVED; return scope_add_child(&compiler->root_scope, expr); } static CHECK_RESULT int init_default_types(amal_compiler *compiler) { - return_if_error(create_default_type(compiler, "i8")); - return_if_error(create_default_type(compiler, "i16")); - return_if_error(create_default_type(compiler, "i32")); - return_if_error(create_default_type(compiler, "i64")); - return_if_error(create_default_type(compiler, "u8")); - return_if_error(create_default_type(compiler, "u16")); - return_if_error(create_default_type(compiler, "u32")); - return_if_error(create_default_type(compiler, "u64")); - return_if_error(create_default_type(compiler, "isize")); - return_if_error(create_default_type(compiler, "usize")); - return_if_error(create_default_type(compiler, "f32")); - return_if_error(create_default_type(compiler, "f64")); - return_if_error(create_default_type(compiler, "str")); + return_if_error(create_default_type(compiler, "i8", &compiler->default_types.i8)); + return_if_error(create_default_type(compiler, "i16", &compiler->default_types.i16)); + return_if_error(create_default_type(compiler, "i32", &compiler->default_types.i32)); + return_if_error(create_default_type(compiler, "i64", &compiler->default_types.i64)); + return_if_error(create_default_type(compiler, "u8", &compiler->default_types.u8)); + return_if_error(create_default_type(compiler, "u16", &compiler->default_types.u16)); + return_if_error(create_default_type(compiler, "u32", &compiler->default_types.u32)); + return_if_error(create_default_type(compiler, "u64", &compiler->default_types.u64)); + return_if_error(create_default_type(compiler, "isize", &compiler->default_types.isize)); + return_if_error(create_default_type(compiler, "usize", &compiler->default_types.usize)); + return_if_error(create_default_type(compiler, "f32", &compiler->default_types.f32)); + return_if_error(create_default_type(compiler, "f64", &compiler->default_types.f64)); + return_if_error(create_default_type(compiler, "str", &compiler->default_types.str)); return 0; } @@ -169,7 +163,7 @@ static CHECK_RESULT int amal_compiler_load_in_this_thread(amal_compiler *self, F amal_log_info("Started parsing %.*s", filepath.size, filepath.data); return_if_error(scoped_allocator_alloc(allocator, sizeof(Parser), (void**)&parser)); return_if_error(parser_init(parser, self, allocator)); - file_scope->scope = &parser->scope; + file_scope->parser = parser; return_if_error(parser_parse_file(parser, filepath)); cleanup_if_error(amal_mutex_lock(&self->mutex, "amal_compiler_load_in_this_thread")); cleanup_if_error(buffer_append(&self->parsers, &parser, sizeof(parser))); @@ -211,15 +205,16 @@ static void* thread_callback_parse_file(void *userdata) { return result; } -static CHECK_RESULT int thread_resolve_ast(Parser *parser) { +static CHECK_RESULT int thread_resolve_ast(amal_compiler *compiler, Parser *parser) { AstCompilerContext compiler_context; int result; compiler_context.parser = parser; + compiler_context.compiler = compiler; compiler_context.scope = NULL; result = setjmp(compiler_context.env); if(result == 0) { amal_log_debug("Resolving AST for file: %.*s", parser->tokenizer.code_name.size, parser->tokenizer.code_name.data); - scope_resolve(&parser->scope, &compiler_context); + scope_resolve(&parser->struct_decl.body, &compiler_context); } return result; } @@ -232,7 +227,7 @@ static CHECK_RESULT int thread_generate_ssa(Parser *parser) { if(result == 0) { return_if_error(ssa_init(&compiler_context.ssa, parser->allocator)); amal_log_debug("Generating SSA for file: %.*s", parser->tokenizer.code_name.size, parser->tokenizer.code_name.data); - scope_generate_ssa(&parser->scope, &compiler_context); + scope_generate_ssa(&parser->struct_decl.body, &compiler_context); } return result; } @@ -256,7 +251,7 @@ static void* thread_callback_generic(void *userdata) { break; } case THREAD_WORK_RESOLVE_AST: - cleanup_if_error(thread_resolve_ast(parser)); + cleanup_if_error(thread_resolve_ast(compiler_userdata.compiler, parser)); break; case THREAD_WORK_GENERATE_SSA: cleanup_if_error(thread_generate_ssa(parser)); -- cgit v1.2.3