aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-03-31 13:44:27 +0200
committerdec05eba <dec05eba@protonmail.com>2020-07-25 14:36:46 +0200
commit6a9466da5377d0bc73c7e5aa48deca3740d3de6f (patch)
tree87f4630f72fe77d037fe19d17bdc00618929f678 /src
parent6927b6338b9655974db79c429e6ffc73037ab5e0 (diff)
Test errors, stop working on error
Diffstat (limited to 'src')
-rw-r--r--src/compiler.c94
-rw-r--r--src/parser.c5
-rw-r--r--src/std/buffer.c4
-rw-r--r--src/tokenizer.c81
4 files changed, 143 insertions, 41 deletions
diff --git a/src/compiler.c b/src/compiler.c
index c9ae09a..d10d9fb 100644
--- a/src/compiler.c
+++ b/src/compiler.c
@@ -11,8 +11,6 @@
#include <limits.h>
#include <assert.h>
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
-
static CHECK_RESULT int get_thread_count_env_var(int *thread_count) {
char *threads;
threads = getenv("THREADS");
@@ -68,7 +66,12 @@ static CHECK_RESULT int init_default_types(amal_compiler *compiler) {
return 0;
}
-int amal_compiler_init(amal_compiler *self) {
+void amal_compiler_options_init(amal_compiler_options *self) {
+ self->error_callback = NULL;
+ self->error_callback_userdata = NULL;
+}
+
+int amal_compiler_init(amal_compiler *self, const amal_compiler_options *options) {
int i;
int result;
@@ -87,7 +90,12 @@ int amal_compiler_init(amal_compiler *self) {
am_memset(&self->allocator, 0, sizeof(self->allocator));
am_memset(&self->root_scope, 0, sizeof(self->root_scope));
+ if(options)
+ am_memcpy(&self->options, options, sizeof(self->options));
+ else
+ am_memset(&self->options, 0, sizeof(self->options));
self->started = bool_false;
+ self->used = bool_false;
self->generic_work_object_index = 0;
amal_mutex_init(&self->mutex);
@@ -129,7 +137,8 @@ int amal_compiler_deinit(amal_compiler *self) {
typedef enum {
THREAD_WORK_PARSE,
THREAD_WORK_RESOLVE_AST,
- THREAD_WORK_GENERATE_SSA
+ THREAD_WORK_GENERATE_SSA,
+ THREAD_WORK_GENERATE_BYTECODE
} ThreadWorkType;
typedef struct {
@@ -153,7 +162,7 @@ typedef struct {
ThreadWorkType type;
} ThreadWorkData;
-static CHECK_RESULT int amal_compiler_load_in_this_thread(amal_compiler *self, FileScopeReference *file_scope, ScopedAllocator *allocator) {
+static CHECK_RESULT int amal_compiler_load_in_this_thread(amal_compiler *compiler, FileScopeReference *file_scope, ScopedAllocator *allocator) {
Parser *parser;
int result;
BufferView filepath;
@@ -162,16 +171,16 @@ static CHECK_RESULT int amal_compiler_load_in_this_thread(amal_compiler *self, F
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));
+ return_if_error(parser_init(parser, compiler, allocator));
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)));
+ cleanup_if_error(amal_mutex_lock(&compiler->mutex, "amal_compiler_load_in_this_thread"));
+ cleanup_if_error(buffer_append(&compiler->parsers, &parser, sizeof(parser)));
amal_log_info("Finished parsing %.*s", filepath.size, filepath.data);
result = AMAL_COMPILER_OK;
cleanup:
- amal_mutex_tryunlock(&self->mutex);
+ amal_mutex_tryunlock(&compiler->mutex);
return result;
}
@@ -200,6 +209,14 @@ static void* thread_callback_parse_file(void *userdata) {
result = NULL;
cleanup:
+ /*
+ To stop all other parsers from working cleanly, we simply clear the file queue,
+ and the other threads will stop when they are done with the file they are currently parsing.
+ */
+ if(result != NULL) {
+ ignore_result_int(amal_mutex_lock(&compiler_parser_userdata.compiler->mutex, "thread_callback_parse_file"));
+ buffer_clear(&compiler_parser_userdata.compiler->queued_files);
+ }
compiler_parser_userdata.parser_thread_data->status = PARSER_THREAD_STATUS_IDLE;
amal_mutex_tryunlock(&compiler_parser_userdata.compiler->mutex);
return result;
@@ -219,17 +236,30 @@ static CHECK_RESULT int thread_resolve_ast(amal_compiler *compiler, Parser *pars
}
static CHECK_RESULT int thread_generate_ssa(Parser *parser) {
- SsaCompilerContext compiler_context;
+ SsaCompilerContext *compiler_context;
int result;
- result = setjmp(compiler_context.env);
- 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->struct_decl.body, &compiler_context);
- }
+
+ return_if_error(scoped_allocator_alloc(parser->allocator, sizeof(SsaCompilerContext), (void**)&compiler_context));
+ return_if_error(ssa_init(&compiler_context->ssa, parser->allocator));
+ /* TODO: Maybe instead of creating a ssa context for every parser (every file), create one for every thread */
+ parser->ssa_context = compiler_context;
+ amal_log_debug("Generating SSA for file: %.*s", parser->tokenizer.code_name.size, parser->tokenizer.code_name.data);
+ result = setjmp(compiler_context->env);
+
+ if(result == 0)
+ scope_generate_ssa(&parser->struct_decl.body, compiler_context);
+
return result;
}
+static CHECK_RESULT int thread_generate_bytecode(amal_compiler *compiler, Parser *parser) {
+ /* TODO: Implement */
+ (void)compiler;
+ (void)parser;
+ /*assert(bool_false);*/
+ return 0;
+}
+
/* TODO: Handle errors (stop work in all other threads and report errors/warnings) */
static void* thread_callback_generic(void *userdata) {
CompilerGenericThreadUserData compiler_userdata;
@@ -254,6 +284,9 @@ static void* thread_callback_generic(void *userdata) {
case THREAD_WORK_GENERATE_SSA:
cleanup_if_error(thread_generate_ssa(parser));
break;
+ case THREAD_WORK_GENERATE_BYTECODE:
+ cleanup_if_error(thread_generate_bytecode(compiler_userdata.compiler, parser));
+ break;
}
cleanup_if_error(amal_mutex_lock(&compiler_userdata.compiler->mutex, "thread_callback_generic"));
if(compiler_userdata.compiler->generic_work_object_index + 1 >= (int)buffer_get_size(&compiler_userdata.compiler->parsers, Parser*))
@@ -265,6 +298,14 @@ static void* thread_callback_generic(void *userdata) {
result = NULL;
cleanup:
+ /*
+ To stop all other worker threads cleanly, we simply say we are done with all work in the queue,
+ and the other threads will stop when they are done with the work they are currently working on.
+ */
+ if(result != NULL) {
+ cleanup_if_error(amal_mutex_lock(&compiler_userdata.compiler->mutex, "thread_callback_generic"));
+ compiler_userdata.compiler->generic_work_object_index = (int)buffer_get_size(&compiler_userdata.compiler->parsers, Parser*);
+ }
compiler_userdata.parser_thread_data->status = PARSER_THREAD_STATUS_IDLE;
amal_mutex_tryunlock(&compiler_userdata.compiler->mutex);
return result;
@@ -297,7 +338,8 @@ static CHECK_RESULT int amal_compiler_select_thread_for_work(amal_compiler *self
break;
}
case THREAD_WORK_RESOLVE_AST:
- case THREAD_WORK_GENERATE_SSA: {
+ case THREAD_WORK_GENERATE_SSA:
+ case THREAD_WORK_GENERATE_BYTECODE: {
CompilerGenericThreadUserData *userdata;
cleanup_if_error(am_malloc(sizeof(CompilerGenericThreadUserData), (void**)&userdata));
thread_user_data = userdata;
@@ -432,11 +474,15 @@ static CHECK_RESULT int try_create_file_scope(amal_compiler *compiler, const cha
return ret;
}
-/*
-amal_compiler_load_file is called by the user for the first file to compile
-but also called by the parser when it sees @import
-*/
-int amal_compiler_load_file(amal_compiler *self, const char *filepath, FileScopeReference **file_scope) {
+int amal_compiler_load_file(amal_compiler *self, const char *filepath) {
+ FileScopeReference *file_scope;
+ if(self->used)
+ return AMAL_COMPILER_ERR;
+ self->used = bool_true;
+ return amal_compiler_internal_load_file(self, filepath, &file_scope);
+}
+
+int amal_compiler_internal_load_file(amal_compiler *self, const char *filepath, FileScopeReference **file_scope) {
int result;
BufferView filepath_view;
ParserThreadData *parser_thread_data;
@@ -478,6 +524,10 @@ int amal_compiler_load_file(amal_compiler *self, const char *filepath, FileScope
assert(amal_compiler_check_all_threads_done(self));
amal_log_info("Finished generating SSA");
+ return_if_error(amal_compiler_dispatch_generic(self, THREAD_WORK_GENERATE_BYTECODE));
+ assert(amal_compiler_check_all_threads_done(self));
+ amal_log_info("Finished generating bytecode");
+
return AMAL_COMPILER_OK;
}
diff --git a/src/parser.c b/src/parser.c
index bac7b3a..9aa8924 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -54,6 +54,7 @@ int parser_thread_data_join(ParserThreadData *self, void **result) {
int parser_init(Parser *self, amal_compiler *compiler, ScopedAllocator *allocator) {
self->allocator = allocator;
self->compiler = compiler;
+ self->ssa_context = NULL;
self->started = bool_false;
self->error.index = 0;
self->error.str = NULL;
@@ -571,7 +572,7 @@ ROOT = BODY_LOOP
int parser_parse_buffer(Parser *self, BufferView code_buffer, BufferView buffer_name) {
int result;
self->file_decl.var_name = buffer_name;
- throw_if_error(tokenizer_init(&self->tokenizer, self->allocator, code_buffer, buffer_name));
+ throw_if_error(tokenizer_init(&self->tokenizer, self->allocator, code_buffer, buffer_name, &self->compiler->options));
result = setjmp(self->parse_env);
if(result == 0)
parser_parse_body_loop(self, &self->struct_decl.body, TOK_END_OF_FILE);
@@ -633,7 +634,7 @@ void parser_queue_file(Parser *self, BufferView path, FileScopeReference **file_
file_directory = file_get_parent_directory(self->tokenizer.code_name);
throw_if_error(file_path_join(file_directory, path, &path_relative));
/* We want buffer to be null-terminated but null character should not be included for the size */
- result = amal_compiler_load_file(self->compiler, path_relative, file_scope);
+ result = amal_compiler_internal_load_file(self->compiler, path_relative, file_scope);
if(result != 0) {
self->error = tokenizer_create_error(&self->tokenizer,
tokenizer_get_code_reference_index(&self->tokenizer, path.data),
diff --git a/src/std/buffer.c b/src/std/buffer.c
index e631153..8e23a30 100644
--- a/src/std/buffer.c
+++ b/src/std/buffer.c
@@ -62,6 +62,10 @@ int buffer_pop(Buffer *self, void *data, usize size) {
return 0;
}
+void buffer_clear(Buffer *self) {
+ self->size = 0;
+}
+
void* buffer_start(Buffer *self) {
return self->data;
}
diff --git a/src/tokenizer.c b/src/tokenizer.c
index d873b0e..7f6d08e 100644
--- a/src/tokenizer.c
+++ b/src/tokenizer.c
@@ -25,8 +25,9 @@ static int tokenizer_get_end_of_line_from_index(Tokenizer *self, int index);
/* Returns -1 if end of multiline comment was not found */
static int tokenizer_get_end_of_multiline_comment(Tokenizer *self, int index);
-int tokenizer_init(Tokenizer *self, ScopedAllocator *allocator, BufferView code, BufferView code_name) {
+int tokenizer_init(Tokenizer *self, ScopedAllocator *allocator, BufferView code, BufferView code_name, const amal_compiler_options *compiler_options) {
assert(code.size <= INT_MAX);
+ assert(compiler_options);
self->code = code;
self->index = 0;
self->prev_index = 0;
@@ -35,6 +36,7 @@ int tokenizer_init(Tokenizer *self, ScopedAllocator *allocator, BufferView code,
self->code_name = code_name.data ? code_name : create_buffer_view("<buffer>", 8);
self->number_is_integer = bool_false;
self->allocator = allocator;
+ self->compiler_options = compiler_options;
return 0;
}
@@ -569,7 +571,18 @@ int tokenizer_get_end_of_line_from_index(Tokenizer *self, int index) {
}
return index;
}
-
+/*
+static int find_non_whitespace(const char *str, usize size) {
+ usize i;
+ for(i = 0; i < size; ++i) {
+ char c;
+ c = str[i];
+ if(c != ' ' && c != '\t')
+ return i;
+ }
+ return -1;
+}
+*/
int tokenizer_get_end_of_multiline_comment(Tokenizer *self, int index) {
char c;
int comment_count;
@@ -608,30 +621,66 @@ static int tokenizer_get_line_by_index(Tokenizer *self, int index) {
return line;
}
+static int max(int a, int b) {
+ return a > b ? a : b;
+}
+
void tokenizer_print_error(Tokenizer *self, int index, const char *fmt, ...) {
va_list args;
int line;
int line_start;
int line_end;
+ /*int code_start;*/
int prev_column;
int i;
- amal_mutex *mutex;
- mutex = amal_log_get_mutex();
- ignore_result_int(amal_mutex_lock(mutex, "tokenizer_print_error"));
- va_start(args, fmt);
line = tokenizer_get_line_by_index(self, index);
line_start = tokenizer_get_start_of_line_from_index(self, index);
line_end = tokenizer_get_end_of_line_from_index(self, index);
+ /*code_start = find_non_whitespace(&self->code.data[line_start], line_end - line_start);
+ if(code_start != -1)
+ line_start += code_start;*/
prev_column = index - line_start;
- fprintf(stderr, "\x1b[1;37m%.*s:%d:%d:\x1b[0m \x1b[1;31merror:\x1b[0m ", (int)self->code_name.size, self->code_name.data, line, 1 + prev_column);
- vfprintf(stderr, fmt, args);
- fprintf(stderr, "\n%.*s\n", line_end - line_start, self->code.data + line_start);
- for(i = 0; i < prev_column; ++i)
- fprintf(stderr, " ");
- fprintf(stderr, "\x1b[1;32m^\x1b[0m\n");
- va_end(args);
- ignore_result_int(amal_mutex_unlock(mutex));
+
+ if(self->compiler_options->error_callback) {
+ char buffer[2048];
+ int bytes_copied;
+
+ bytes_copied = 0;
+ bytes_copied += max(0, snprintf(buffer + bytes_copied, sizeof(buffer) - bytes_copied, "%.*s:%d:%d: error: ", (int)self->code_name.size, self->code_name.data, line, 1 + prev_column));
+
+ if(sizeof(buffer) - bytes_copied > 0) {
+ va_start(args, fmt);
+ bytes_copied += max(0, vsnprintf(buffer + bytes_copied, sizeof(buffer) - bytes_copied, fmt, args));
+ va_end(args);
+ }
+
+ if(sizeof(buffer) - bytes_copied > 0)
+ bytes_copied += max(0, snprintf(buffer + bytes_copied, sizeof(buffer) - bytes_copied, "\n%.*s\n", line_end - line_start, self->code.data + line_start));
+
+ if(sizeof(buffer) - bytes_copied > 0) {
+ for(i = 0; i < prev_column; ++i)
+ bytes_copied += max(0, snprintf(buffer + bytes_copied, sizeof(buffer) - bytes_copied, " "));
+ }
+
+ if(sizeof(buffer) - bytes_copied > 0)
+ bytes_copied += max(0, snprintf(buffer + bytes_copied, sizeof(buffer) - bytes_copied, "^\n"));
+
+ self->compiler_options->error_callback(buffer, bytes_copied, self->compiler_options->error_callback_userdata);
+ } else {
+ amal_mutex *mutex;
+ mutex = amal_log_get_mutex();
+ ignore_result_int(amal_mutex_lock(mutex, "tokenizer_print_error"));
+ va_start(args, fmt);
+ fprintf(stderr, "\x1b[1;37m%.*s:%d:%d:\x1b[0m \x1b[1;31merror:\x1b[0m ", (int)self->code_name.size, self->code_name.data, line, 1 + prev_column);
+ vfprintf(stderr, fmt, args);
+ fprintf(stderr, "\n%.*s\n", line_end - line_start, self->code.data + line_start);
+ for(i = 0; i < prev_column; ++i)
+ fprintf(stderr, " ");
+ fprintf(stderr, "\x1b[1;32m^\x1b[0m\n");
+ va_end(args);
+ ignore_result_int(amal_mutex_unlock(mutex));
+ }
}
void tokenizer_print_error_object(Tokenizer *self, TokenizerError *error) {
@@ -645,10 +694,8 @@ TokenizerError tokenizer_create_error(Tokenizer *self, int index, const char *fm
int bytes_copied;
va_start(args, fmt);
- bytes_copied = vsnprintf(buffer, sizeof(buffer), fmt, args);
+ bytes_copied = max(0, vsnprintf(buffer, sizeof(buffer), fmt, args));
va_end(args);
- if(bytes_copied < 0)
- bytes_copied = 0;
result.index = index;
result.str = NULL;