aboutsummaryrefslogtreecommitdiff
path: root/src/compiler.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/compiler.c')
-rw-r--r--src/compiler.c90
1 files changed, 65 insertions, 25 deletions
diff --git a/src/compiler.c b/src/compiler.c
index 05fae78..8c3266c 100644
--- a/src/compiler.c
+++ b/src/compiler.c
@@ -29,10 +29,10 @@ static CHECK_RESULT int create_default_type(amal_compiler *compiler, const char
StructDecl *struct_decl;
Ast *expr;
- return_if_error(scoped_allocator_alloc(&compiler->allocator, sizeof(StructDecl), (void**)&struct_decl));
+ return_if_error(arena_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(arena_allocator_alloc(&compiler->allocator, sizeof(LhsExpr), (void**)lhs_expr));
lhsexpr_init(*lhs_expr, bool_true, bool_true, bool_true, create_buffer_view(name, strnlen(name, PATH_MAX)));
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));
@@ -42,22 +42,59 @@ static CHECK_RESULT int create_default_type(amal_compiler *compiler, const char
}
static CHECK_RESULT int init_default_types(amal_compiler *compiler) {
- 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_if_error(create_default_type(compiler, "i8", (LhsExpr**)&compiler->default_types.i8));
+ return_if_error(create_default_type(compiler, "i16", (LhsExpr**)&compiler->default_types.i16));
+ return_if_error(create_default_type(compiler, "i32", (LhsExpr**)&compiler->default_types.i32));
+ return_if_error(create_default_type(compiler, "i64", (LhsExpr**)&compiler->default_types.i64));
+ return_if_error(create_default_type(compiler, "u8", (LhsExpr**)&compiler->default_types.u8));
+ return_if_error(create_default_type(compiler, "u16", (LhsExpr**)&compiler->default_types.u16));
+ return_if_error(create_default_type(compiler, "u32", (LhsExpr**)&compiler->default_types.u32));
+ return_if_error(create_default_type(compiler, "u64", (LhsExpr**)&compiler->default_types.u64));
+ return_if_error(create_default_type(compiler, "isize", (LhsExpr**)&compiler->default_types.isize));
+ return_if_error(create_default_type(compiler, "usize", (LhsExpr**)&compiler->default_types.usize));
+ return_if_error(create_default_type(compiler, "f32", (LhsExpr**)&compiler->default_types.f32));
+ return_if_error(create_default_type(compiler, "f64", (LhsExpr**)&compiler->default_types.f64));
+ return_if_error(create_default_type(compiler, "str", (LhsExpr**)&compiler->default_types.str));
+
+ compiler->default_types.arithmetic_types[0] = compiler->default_types.i8;
+ compiler->default_types.arithmetic_types[1] = compiler->default_types.u8;
+ compiler->default_types.arithmetic_types[2] = compiler->default_types.i16;
+ compiler->default_types.arithmetic_types[3] = compiler->default_types.u16;
+ compiler->default_types.arithmetic_types[4] = compiler->default_types.i32;
+ compiler->default_types.arithmetic_types[5] = compiler->default_types.u32;
+ compiler->default_types.arithmetic_types[6] = compiler->default_types.i64;
+ compiler->default_types.arithmetic_types[7] = compiler->default_types.u64;
+ compiler->default_types.arithmetic_types[8] = compiler->default_types.isize;
+ compiler->default_types.arithmetic_types[9] = compiler->default_types.usize;
+
+ compiler->default_types.i8->is_signed = bool_true;
+ compiler->default_types.u8->is_signed = bool_false;
+ compiler->default_types.i16->is_signed = bool_true;
+ compiler->default_types.u16->is_signed = bool_false;
+ compiler->default_types.i32->is_signed = bool_true;
+ compiler->default_types.u32->is_signed = bool_false;
+ compiler->default_types.i64->is_signed = bool_true;
+ compiler->default_types.u64->is_signed = bool_false;
+ compiler->default_types.isize->is_signed = bool_true;
+ compiler->default_types.usize->is_signed = bool_false;
+ compiler->default_types.f32->is_signed = bool_true;
+ compiler->default_types.f64->is_signed = bool_true;
+ compiler->default_types.str->is_signed = bool_false;
return 0;
}
+bool is_arithmetic_type(LhsExpr *expr, amal_compiler *compiler) {
+ usize i;
+ const amal_default_types *default_types;
+ default_types = &compiler->default_types;
+
+ for(i = 0; i < NUM_ARITHMETIC_TYPES; ++i) {
+ if(expr == (LhsExpr*)default_types->arithmetic_types[i])
+ return bool_true;
+ }
+ return bool_false;
+}
+
void amal_compiler_options_init(amal_compiler_options *self) {
self->error_callback = NULL;
self->error_callback_userdata = NULL;
@@ -89,12 +126,12 @@ static CHECK_RESULT int amal_compiler_init(amal_compiler *self, const amal_compi
self->generic_work_object_index = 0;
amal_mutex_init(&self->mutex);
- return_if_error(scoped_allocator_init(&self->allocator));
+ return_if_error(arena_allocator_init(&self->allocator));
cleanup_if_error(scope_init(&self->root_scope, NULL, &self->allocator));
cleanup_if_error(buffer_init(&self->parsers, &self->allocator));
cleanup_if_error(buffer_init(&self->queued_files, &self->allocator));
cleanup_if_error(hash_map_init(&self->file_scopes, &self->allocator, sizeof(FileScopeReference*), hash_map_compare_string, amal_hash_string));
- cleanup_if_error(scoped_allocator_alloc(&self->allocator,
+ cleanup_if_error(arena_allocator_alloc(&self->allocator,
self->usable_thread_count * sizeof(ParserThreadData),
(void**)&self->threads));
for(i = 0; i < self->usable_thread_count; ++i)
@@ -114,7 +151,7 @@ void amal_compiler_deinit(amal_compiler *self) {
}
amal_mutex_deinit(&self->mutex);
- scoped_allocator_deinit(&self->allocator);
+ arena_allocator_deinit(&self->allocator);
}
typedef enum {
@@ -145,7 +182,7 @@ typedef struct {
ThreadWorkType type;
} ThreadWorkData;
-static CHECK_RESULT int amal_compiler_load_in_this_thread(amal_compiler *compiler, FileScopeReference *file_scope, ScopedAllocator *allocator) {
+static CHECK_RESULT int amal_compiler_load_in_this_thread(amal_compiler *compiler, FileScopeReference *file_scope, ArenaAllocator *allocator) {
Parser *parser;
int result;
BufferView filepath;
@@ -153,7 +190,7 @@ static CHECK_RESULT int amal_compiler_load_in_this_thread(amal_compiler *compile
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(arena_allocator_alloc(allocator, sizeof(Parser), (void**)&parser));
return_if_error(parser_init(parser, compiler, allocator));
file_scope->parser = parser;
return_if_error(parser_parse_file(parser, filepath));
@@ -229,7 +266,7 @@ static CHECK_RESULT int thread_generate_ssa(Parser *parser) {
SsaCompilerContext compiler_context;
int result;
- return_if_error(scoped_allocator_alloc(parser->allocator, sizeof(Ssa), (void**)&compiler_context.ssa));
+ return_if_error(arena_allocator_alloc(parser->allocator, sizeof(Ssa), (void**)&compiler_context.ssa));
return_if_error(ssa_init(compiler_context.ssa, parser->allocator));
compiler_context.compiler = parser->compiler;
parser->ssa = compiler_context.ssa;
@@ -446,7 +483,10 @@ static CHECK_RESULT int amal_compiler_dispatch_generic(amal_compiler *self, Thre
}
static CHECK_RESULT int amal_compiler_generate_program(amal_compiler *self) {
- /* TODO: Copying the bytecode to the program can be done using multiple threads */
+ /*
+ TODO: Copying the bytecode to the program can be done using multiple threads.
+ Use self->threads for that.
+ */
Parser **parser;
Parser **parser_end;
parser = buffer_begin(&self->parsers);
@@ -472,7 +512,7 @@ static CHECK_RESULT int try_create_file_scope(amal_compiler *compiler, const cha
path_view = create_buffer_view(result_path, result_path_size);
cleanup_if_error(amal_mutex_lock(&compiler->mutex, "try_create_file_scope"));
if(!hash_map_get(&compiler->file_scopes, path_view, file_scope)) {
- cleanup_if_error(scoped_allocator_alloc(&compiler->allocator, sizeof(FileScopeReference), (void**)file_scope));
+ cleanup_if_error(arena_allocator_alloc(&compiler->allocator, sizeof(FileScopeReference), (void**)file_scope));
/* @(*file_scope)->canonical_path won't change after this, so it's fine if allocator belongs to non-thread safe compiler instance */
cleanup_if_error(file_scope_reference_init(*file_scope, path_view, &compiler->allocator));
cleanup_if_error(hash_map_insert(&compiler->file_scopes, path_view, file_scope));
@@ -487,11 +527,11 @@ static CHECK_RESULT int try_create_file_scope(amal_compiler *compiler, const cha
}
int amal_compiler_load_file(amal_compiler_options *options, amal_program *program, const char *filepath) {
- assert(program);
- assert(filepath);
amal_compiler compiler;
FileScopeReference *file_scope;
int result;
+ assert(program);
+ assert(filepath);
return_if_error(amal_compiler_init(&compiler, options, program));
result = amal_compiler_internal_load_file(&compiler, filepath, &file_scope);
amal_compiler_deinit(&compiler);