aboutsummaryrefslogtreecommitdiff
path: root/src/compiler.c
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-04-22 02:34:30 +0200
committerdec05eba <dec05eba@protonmail.com>2020-07-25 14:36:46 +0200
commita76ba1b33e397638c4209dd77e6073e423ac07a8 (patch)
tree0ef62209546ba91d53a2fabb54f3b8ac9dcfafdf /src/compiler.c
parent6a9466da5377d0bc73c7e5aa48deca3740d3de6f (diff)
Start on bytecode. Commit before os switch
Diffstat (limited to 'src/compiler.c')
-rw-r--r--src/compiler.c38
1 files changed, 23 insertions, 15 deletions
diff --git a/src/compiler.c b/src/compiler.c
index d10d9fb..3a921c3 100644
--- a/src/compiler.c
+++ b/src/compiler.c
@@ -1,6 +1,7 @@
#include "../include/compiler.h"
#include "../include/parser.h"
#include "../include/ssa/ssa.h"
+#include "../include/bytecode/bytecode.h"
#include "../include/std/log.h"
#include "../include/std/mem.h"
#include "../include/std/hash.h"
@@ -236,28 +237,35 @@ 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;
- 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;
+ return_if_error(scoped_allocator_alloc(parser->allocator, sizeof(Ssa), (void**)&compiler_context.ssa));
+ return_if_error(ssa_init(compiler_context.ssa, parser->allocator));
+ parser->ssa = compiler_context.ssa;
amal_log_debug("Generating SSA for file: %.*s", parser->tokenizer.code_name.size, parser->tokenizer.code_name.data);
- result = setjmp(compiler_context->env);
+ result = setjmp(compiler_context.env);
if(result == 0)
- scope_generate_ssa(&parser->struct_decl.body, compiler_context);
+ 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;
+static CHECK_RESULT int thread_generate_bytecode(Parser *parser) {
+ BytecodeCompilerContext compiler_context;
+ int result;
+
+ return_if_error(scoped_allocator_alloc(parser->allocator, sizeof(Bytecode), (void**)&compiler_context.bytecode));
+ return_if_error(bytecode_init(compiler_context.bytecode, parser->allocator));
+ compiler_context.parser = parser;
+ amal_log_debug("Generating bytecode for file: %.*s", parser->tokenizer.code_name.size, parser->tokenizer.code_name.data);
+ result = setjmp(compiler_context.env);
+
+ if(result == 0)
+ generate_bytecode_from_ssa(&compiler_context);
+
+ return result;
}
/* TODO: Handle errors (stop work in all other threads and report errors/warnings) */
@@ -285,7 +293,7 @@ static void* thread_callback_generic(void *userdata) {
cleanup_if_error(thread_generate_ssa(parser));
break;
case THREAD_WORK_GENERATE_BYTECODE:
- cleanup_if_error(thread_generate_bytecode(compiler_userdata.compiler, parser));
+ cleanup_if_error(thread_generate_bytecode(parser));
break;
}
cleanup_if_error(amal_mutex_lock(&compiler_userdata.compiler->mutex, "thread_callback_generic"));
@@ -430,7 +438,7 @@ static CHECK_RESULT int amal_compiler_load_file_join_threads(amal_compiler *self
static CHECK_RESULT int amal_compiler_dispatch_generic(amal_compiler *self, ThreadWorkType work_type) {
Parser **parser;
Parser **parser_end;
- parser = buffer_start(&self->parsers);
+ parser = buffer_begin(&self->parsers);
parser_end = buffer_end(&self->parsers);
self->generic_work_object_index = 0;
for(; parser != parser_end; ++parser) {