From 071bdd4d6facb8786f089882d53c127e6163e3ce Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 19 Mar 2019 21:31:57 +0100 Subject: Set file scope when parser is created. No need to resolve for every @import instance --- include/compiler.h | 2 +- src/ast.c | 2 +- src/compiler.c | 27 +++++++++++++++------------ 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/include/compiler.h b/include/compiler.h index 47d4471..a7fbb34 100644 --- a/include/compiler.h +++ b/include/compiler.h @@ -17,7 +17,7 @@ struct amal_compiler { ScopedAllocator allocator; Scope root_scope; Buffer/**/ parsers; - Buffer/**/ queued_files; + Buffer/**/ queued_files; HashMap/**/ file_scopes; ParserThreadData *threads; int usable_thread_count; diff --git a/src/ast.c b/src/ast.c index 8e19819..a51ae71 100644 --- a/src/ast.c +++ b/src/ast.c @@ -241,7 +241,7 @@ static void import_resolve(Ast *self, AstCompilerContext *context) { (void)import; (void)self; (void)context; - /* TODO: Implement parser scope for import */ + /* TODO: Convert all scopes to structs and set import->resolved_type to import->file_scope->scope; */ } static void funcdecl_resolve(Ast *self, AstCompilerContext *context) { diff --git a/src/compiler.c b/src/compiler.c index f0e5928..8510a2c 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -137,7 +137,7 @@ typedef enum { typedef struct { amal_compiler *compiler; ParserThreadData *parser_thread_data; - BufferView filepath; + FileScopeReference *file_scope; } CompilerParserThreadUserData; typedef struct { @@ -149,24 +149,27 @@ typedef struct { typedef struct { union { - BufferView filepath; + FileScopeReference *file_scope; Parser *parser; } value; ThreadWorkType type; } ThreadWorkData; -static CHECK_RESULT int amal_compiler_load_in_this_thread(amal_compiler *self, BufferView filepath, ScopedAllocator *allocator) { +static CHECK_RESULT int amal_compiler_load_in_this_thread(amal_compiler *self, FileScopeReference *file_scope, ScopedAllocator *allocator) { Parser *parser; int result; + BufferView filepath; result = AMAL_COMPILER_ERR; - amal_log_info("Started parsing %.*s", (int)filepath.size, filepath.data); + filepath = create_buffer_view(file_scope->canonical_path.data, file_scope->canonical_path.size); + 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; 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))); - amal_log_info("Finished parsing %.*s", (int)filepath.size, filepath.data); + amal_log_info("Finished parsing %.*s", filepath.size, filepath.data); result = AMAL_COMPILER_OK; cleanup: @@ -176,22 +179,22 @@ static CHECK_RESULT int amal_compiler_load_in_this_thread(amal_compiler *self, B /* TODO: Handle errors (stop parsing in all other threads and report errors/warnings) */ static void* thread_callback_parse_file(void *userdata) { - BufferView next_file; + FileScopeReference *file_scope; CompilerParserThreadUserData compiler_parser_userdata; void *result; assert(!amal_thread_is_main()); am_memcpy(&compiler_parser_userdata, userdata, sizeof(compiler_parser_userdata)); am_free(userdata); - next_file = compiler_parser_userdata.filepath; + file_scope = compiler_parser_userdata.file_scope; result = (void*)AMAL_COMPILER_ERR; for(;;) { int has_next; cleanup_if_error(amal_compiler_load_in_this_thread(compiler_parser_userdata.compiler, - next_file, + file_scope, &compiler_parser_userdata.parser_thread_data->allocator)); cleanup_if_error(amal_mutex_lock(&compiler_parser_userdata.compiler->mutex, "thread_callback_parse_file")); - has_next = buffer_pop(&compiler_parser_userdata.compiler->queued_files, &next_file, sizeof(next_file)); + has_next = buffer_pop(&compiler_parser_userdata.compiler->queued_files, &file_scope, sizeof(FileScopeReference*)); amal_mutex_tryunlock(&compiler_parser_userdata.compiler->mutex); if(has_next != 0) break; @@ -292,7 +295,7 @@ static CHECK_RESULT int amal_compiler_select_thread_for_work(amal_compiler *self thread_user_data = userdata; userdata->compiler = self; userdata->parser_thread_data = parser_thread_data; - userdata->filepath = work_data.value.filepath; + userdata->file_scope = work_data.value.file_scope; result = parser_thread_data_start(parser_thread_data, thread_callback_parse_file, userdata); break; } @@ -454,7 +457,7 @@ int amal_compiler_load_file(amal_compiler *self, const char *filepath, FileScope result = AMAL_COMPILER_ERR; thread_work_data.type = THREAD_WORK_PARSE; - thread_work_data.value.filepath = filepath_view; + thread_work_data.value.file_scope = *file_scope; main_job = bool_false; /* The first time we get here, this will run single-threaded so this part doesn't need mutex */ @@ -485,7 +488,7 @@ int amal_compiler_load_file(amal_compiler *self, const char *filepath, FileScope return AMAL_COMPILER_OK; cleanup_if_error(amal_mutex_lock(&self->mutex, "amal_compiler_load_file")); - cleanup_if_error(buffer_append(&self->queued_files, &filepath_view, sizeof(filepath_view))); + cleanup_if_error(buffer_append(&self->queued_files, file_scope, sizeof(FileScopeReference*))); result = AMAL_COMPILER_OK; cleanup: -- cgit v1.2.3