From 2d2c31cc18aa9af2cdf26fa462edf7a164d45328 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 12 Mar 2019 22:18:43 +0100 Subject: Refactor ssa --- include/ast.h | 3 --- include/defs.h | 1 + include/ssa/ssa.h | 13 +++++++++++++ src/ast.c | 17 ----------------- src/compiler.c | 57 ++++++++++++++++++++++++++++++++++++------------------- src/ssa/ssa.c | 17 +++++++++++++++++ 6 files changed, 68 insertions(+), 40 deletions(-) diff --git a/include/ast.h b/include/ast.h index 497488f..b63dab5 100644 --- a/include/ast.h +++ b/include/ast.h @@ -23,7 +23,6 @@ typedef struct String String; typedef struct Variable Variable; typedef struct Number Number; typedef struct Binop Binop; -typedef struct Scope Scope; typedef union { void *data; @@ -133,7 +132,5 @@ CHECK_RESULT int scope_init(Scope *self, ScopedAllocator *allocator); CHECK_RESULT int scope_add_child(Scope *self, Ast *child); /* longjump to compiler env on failure */ void scope_resolve(Scope *self, AstCompilerContext *context); -/* longjump to compiler env on failure */ -void scope_generate_ssa(Scope *self, AstCompilerContext *context); #endif diff --git a/include/defs.h b/include/defs.h index 6a9bca4..74f2411 100644 --- a/include/defs.h +++ b/include/defs.h @@ -4,5 +4,6 @@ typedef struct ParserThreadData ParserThreadData; typedef struct amal_compiler amal_compiler; typedef struct Parser Parser; +typedef struct Scope Scope; #endif diff --git a/include/ssa/ssa.h b/include/ssa/ssa.h index e5bd86b..a21b45a 100644 --- a/include/ssa/ssa.h +++ b/include/ssa/ssa.h @@ -4,6 +4,9 @@ #include "../std/buffer.h" #include "../std/hash_map.h" #include "../std/defs.h" +#include "../defs.h" + +#include typedef enum { SSA_ASSIGN_INTER, @@ -53,4 +56,14 @@ CHECK_RESULT int ssa_ins_func_end(Ssa *self); CHECK_RESULT int ssa_ins_push(Ssa *self, SsaRegister reg); CHECK_RESULT int ssa_ins_call(Ssa *self, SsaFuncIndex func, SsaRegister *result); + +typedef struct { + jmp_buf env; + Parser *parser; + Ssa ssa; +} SsaCompilerContext; + +/* longjump to compiler env on failure */ +void scope_generate_ssa(Scope *self, SsaCompilerContext *context); + #endif diff --git a/src/ast.c b/src/ast.c index b0852b5..079d45a 100644 --- a/src/ast.c +++ b/src/ast.c @@ -14,7 +14,6 @@ do { \ } while(0) static void ast_resolve(Ast *self, AstCompilerContext *context); -static void ast_generate_ssa(Ast *self, AstCompilerContext *context); Ast ast_none() { Ast ast; @@ -226,19 +225,3 @@ void ast_resolve(Ast *self, AstCompilerContext *context) { } /*self->resolve_status = AST_RESOLVED;*/ } - -void scope_generate_ssa(Scope *self, AstCompilerContext *context) { - Ast *ast; - Ast *ast_end; - ast = buffer_start(&self->ast_objects); - ast_end = buffer_end(&self->ast_objects); - for(; ast != ast_end; ++ast) { - ast_generate_ssa(ast, context); - } -} - -void ast_generate_ssa(Ast *self, AstCompilerContext *context) { - /* TODO: Implement */ - (void)self; - (void)context; -} \ No newline at end of file diff --git a/src/compiler.c b/src/compiler.c index fbdeb94..c50ffac 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -1,5 +1,6 @@ #include "../include/compiler.h" #include "../include/parser.h" +#include "../include/ssa/ssa.h" #include "../include/std/log.h" #include "../include/std/mem.h" #include "../include/std/alloc.h" @@ -150,39 +151,55 @@ static void* thread_callback_parse_file(void *userdata) { return result; } +static CHECK_RESULT int thread_resolve_ast(Parser *parser) { + AstCompilerContext compiler_context; + int result; + compiler_context.parser = parser; + 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); + } + return result; +} + +static CHECK_RESULT int thread_generate_ssa(Parser *parser) { + SsaCompilerContext compiler_context; + int result; + compiler_context.parser = parser; + 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->scope, &compiler_context); + } + return result; +} + /* TODO: Handle errors (stop work in all other threads and report errors/warnings) */ static void* thread_callback_generic(void *userdata) { CompilerGenericThreadUserData compiler_userdata; Parser *parser; - AstCompilerContext compiler_context; void *result; assert(!amal_thread_is_main()); am_memcpy(&compiler_userdata, userdata, sizeof(compiler_userdata)); am_free(userdata); parser = compiler_userdata.parser; - compiler_context.parser = parser; result = (void*)AMAL_COMPILER_ERR; for(;;) { - int result; - result = setjmp(compiler_context.env); - if(result == 0) { - switch(compiler_userdata.work_type) { - case THREAD_WORK_PARSE: - assert(bool_false && "Thread work type can't ge 'parse' for generic work"); - break; - case THREAD_WORK_RESOLVE_AST: - amal_log_debug("Resolving AST for file: %.*s", parser->tokenizer.code_name.size, parser->tokenizer.code_name.data); - scope_resolve(&parser->scope, &compiler_context); - break; - case THREAD_WORK_GENERATE_SSA: - 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); - break; + /* TODO: stop work in all other threads on failure */ + switch(compiler_userdata.work_type) { + case THREAD_WORK_PARSE: { + assert(bool_false && "Thread work type can't ge 'parse' for generic work"); + break; } - } else { - /* TODO: stop work in all other threads */ - break; + case THREAD_WORK_RESOLVE_AST: + cleanup_if_error(thread_resolve_ast(parser)); + break; + case THREAD_WORK_GENERATE_SSA: + cleanup_if_error(thread_generate_ssa(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)) diff --git a/src/ssa/ssa.c b/src/ssa/ssa.c index 3433e8d..f5187af 100644 --- a/src/ssa/ssa.c +++ b/src/ssa/ssa.c @@ -1,6 +1,7 @@ #include "../../include/ssa/ssa.h" #include "../../include/std/mem.h" #include "../../include/std/log.h" +#include "../../include/ast.h" #include static int compare_number(const void *a, const void *b) { @@ -163,3 +164,19 @@ int ssa_ins_call(Ssa *self, SsaFuncIndex func, SsaRegister *result) { amal_log_debug("r%u = CALL f%u", *result, func); return 0; } + +static void ast_generate_ssa(Ast *self, SsaCompilerContext *context) { + /* TODO: Implement */ + (void)self; + (void)context; +} + +void scope_generate_ssa(Scope *self, SsaCompilerContext *context) { + Ast *ast; + Ast *ast_end; + ast = buffer_start(&self->ast_objects); + ast_end = buffer_end(&self->ast_objects); + for(; ast != ast_end; ++ast) { + ast_generate_ssa(ast, context); + } +} -- cgit v1.2.3