diff options
-rwxr-xr-x | build.sh | 25 | ||||
-rw-r--r-- | executor/x86_64/asm.c | 18 | ||||
-rw-r--r-- | include/std/misc.h | 8 | ||||
-rw-r--r-- | src/ast.c | 7 | ||||
-rw-r--r-- | src/compiler.c | 4 | ||||
-rw-r--r-- | src/ir/ir.c | 24 | ||||
-rw-r--r-- | src/std/file.c | 47 | ||||
-rw-r--r-- | src/std/misc.c | 5 |
8 files changed, 45 insertions, 93 deletions
@@ -39,27 +39,6 @@ if [ -n "$PEDANTIC" ]; then CFLAGS="$CFLAGS -DAMAL_PEDANTIC -pedantic " fi -build_compile_commands() { - set +x - compile_commands=$( - first=0 - echo "[" - for source_file in $@; do - if [ $first = 1 ]; then - echo " ," - fi - first=1 - o_file="${source_file}.o" - echo " {" - echo " \"file\": \"$source_file\"," - echo " \"directory\": \"$this_script_dir\"," - echo " \"command\": \"$CC -o $o_file $CFLAGS $LIBS -c $source_file\"" - echo " }" - done - echo "]") - echo "$compile_commands" > "compile_commands.json" -} - build_test() { CFLAGS="$CFLAGS -g -O0 -DDEBUG" @@ -75,8 +54,6 @@ build_test() { set -x time $CC $source_files_test $CFLAGS $LIBS -o test "./libamalgam.so" $(pkg-config --cflags --libs glfw3 glew) set +x - - build_compile_commands $source_files $source_files_test } build_release() { @@ -92,8 +69,6 @@ build_release() { scan-build $CC $BUILD_ARGS fi set +x - - build_compile_commands $source_files } case "$1" in diff --git a/executor/x86_64/asm.c b/executor/x86_64/asm.c index cf56b69..f57f117 100644 --- a/executor/x86_64/asm.c +++ b/executor/x86_64/asm.c @@ -34,6 +34,10 @@ static u8 rex_rm(AsmPtr *dst, Reg64 src) { return rex_sib(dst->base, src); } +static i32 abs_i32(i32 num) { + return num >= 0 ? num : -num; +} + #ifdef DEBUG #include <stdarg.h> #include <string.h> @@ -122,10 +126,6 @@ static const char* reg64_to_str(Reg64 reg) { return NULL; } -static i32 abs(i32 num) { - return num >= 0 ? num : -num; -} - static const char* asm_ptr_to_string(AsmPtr *self) { const char *buf = (const char*)(asm_debug_str_buffer + asm_debug_str_buffer_index); asm_debug_str_append("QWORD PTR ["); @@ -140,7 +140,7 @@ static const char* asm_ptr_to_string(AsmPtr *self) { asm_debug_str_append(" - "); else asm_debug_str_append(" + "); - asm_debug_str_append_num(abs(self->disp)); + asm_debug_str_append_num(abs_i32(self->disp)); } asm_debug_str_append("]"); return buf; @@ -293,10 +293,6 @@ void asm_nop(Asm *self) { ins_end(self, "nop"); } -static i32 abs_i32(i32 value) { - return value >= 0 ? value : -value; -} - /* TODO: Implement 1 and 2 byte displacement? There has to be at least 6 bytes left in the asm buffer before calling this function. @@ -626,7 +622,7 @@ void asm_jz(Asm *self, i32 relative) { in a maximum instruction pointer size of 16 bits */ ins_start(self); - if(abs(relative - 2) <= INT8_MAX) { + if(abs_i32(relative - 2) <= INT8_MAX) { relative -= 2; *self->code_it++ = 0x74; *self->code_it++ = (i8)relative; @@ -659,7 +655,7 @@ void asm_jmp(Asm *self, i32 relative) { in a maximum instruction pointer size of 16 bits */ ins_start(self); - if(abs(relative - 2) <= INT8_MAX) { + if(abs_i32(relative - 2) <= INT8_MAX) { relative -= 2; *self->code_it++ = 0xEB; *self->code_it++ = (i8)relative; diff --git a/include/std/misc.h b/include/std/misc.h index 03d7972..ecbdac5 100644 --- a/include/std/misc.h +++ b/include/std/misc.h @@ -3,6 +3,7 @@ #include "types.h" #include <sys/types.h> +#include "log.h" #if defined(__BYTE_ORDER) #if __BYTE_ORDER == __LITTLE_ENDIAN @@ -26,10 +27,6 @@ u16 byteswap16(u16 value); u32 byteswap32(u32 value); u64 byteswap64(u64 value); -#ifndef AMAL_PEDANTIC -#include "log.h" -#endif - #ifdef AMAL_PEDANTIC #define throw_debug_msg do {} while(0) #else @@ -70,4 +67,7 @@ typedef struct { #define ignore_result_int(expr) (void)((expr)+1) +void check_abort() __THROW __attribute__ ((__noreturn__)); +#define check(cond) do { if(!(cond)) { amal_log_error("compiler-bug: condition failed: " # cond); check_abort(); } } while(0) + #endif @@ -661,8 +661,8 @@ static void assignmentexpr_resolve(Ast *ast, AstCompilerContext *context) { if(resolved_var->type == NAMED_OBJECT_FUNC_PARAM || LHS_EXPR_IS_CONST(resolved_var->value.lhs_expr)) is_lhs_const = bool_true; } else if(self->lhs_expr->type == AST_BINOP) { - LhsExpr *lhs_expr; - lhs_expr = binop_get_lhs_expr(self->lhs_expr->value.binop); + LhsExpr *lhs_expr = binop_get_lhs_expr(self->lhs_expr->value.binop); + check(lhs_expr); is_lhs_const = LHS_EXPR_IS_CONST(lhs_expr); } @@ -764,7 +764,8 @@ static bool resolve_data_type_equals(AstResolvedType *self, AstResolvedType *oth case RESOLVED_TYPE_FUNC_SIG: return function_signature_equals(self->value.func_sig, other->value.func_sig); } - assert(bool_false); + check(bool_false); + return bool_false; } static bool function_parameter_is_c_vararg(FunctionParameter *self, AstCompilerContext *context) { diff --git a/src/compiler.c b/src/compiler.c index f9d9355..eac51ba 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -294,11 +294,11 @@ static CHECK_RESULT int thread_generate_bytecode(Parser *parser) { } static int thread_callback_generic(void *userdata) { - int result; + int result = -1; CompilerGenericThreadUserData *compiler_userdata = userdata; switch(compiler_userdata->work_type) { case THREAD_WORK_PARSE: { - assert(bool_false && "Thread work type can't be 'parse' for generic work"); + check(bool_false && "Thread work type can't be 'parse' for generic work"); break; } case THREAD_WORK_RESOLVE_AST: diff --git a/src/ir/ir.c b/src/ir/ir.c index a6ed931..f0c1ee2 100644 --- a/src/ir/ir.c +++ b/src/ir/ir.c @@ -559,8 +559,6 @@ static CHECK_RESULT IrRegister lhsexpr_export_generate_ir(LhsExpr *self, IrCompi #endif static CHECK_RESULT IrRegister lhsexpr_generate_ir(LhsExpr *self, AstResolveData *resolve_data, IrCompilerContext *context) { - IrRegister reg; - if(LHS_EXPR_IS_EXTERN(self)) return lhsexpr_extern_generate_ir(self, context); @@ -582,6 +580,7 @@ static CHECK_RESULT IrRegister lhsexpr_generate_ir(LhsExpr *self, AstResolveData } return rhs_reg; } else { + IrRegister reg = 0; /* TODO: Do not assign if we dont want default value */ if(resolve_data->type.type == RESOLVED_TYPE_LHS_EXPR) { IrNumber number; @@ -590,16 +589,16 @@ static CHECK_RESULT IrRegister lhsexpr_generate_ir(LhsExpr *self, AstResolveData else if(resolve_data->type.value.lhs_expr == (LhsExpr*)context->compiler->default_types.f64) number = create_ir_float(0.0); else - assert(bool_false && "TODO: assign default value to reg depending on LhsExpr type"); + check(bool_false && "TODO: assign default value to reg depending on LhsExpr type"); throw_if_error(ir_get_unique_reg(context->ir, ®)); throw_if_error(ir_ins_assign_inter(context->ir, reg, number)); } else if(resolve_data->type.type == RESOLVED_TYPE_FUNC_SIG) { - assert(bool_false && "TODO: Implement this when variable can be null. Then the function pointer should be null"); + check(bool_false && "TODO: Implement this when variable can be null. Then the function pointer should be null"); } else { - assert(bool_false); + check(bool_false); } + return reg; } - return reg; } static CHECK_RESULT IrRegister assignmentexpr_generate_ir(AssignmentExpr *self, IrCompilerContext *context) { @@ -682,6 +681,7 @@ static CHECK_RESULT IrRegister funccall_generate_ir(FunctionCall *self, IrCompil FunctionParameter *func_param_expr; int import_index = context->import_index; + func_sig = NULL; func_lhs_expr = NULL; func_param_expr = NULL; context->import_index = 0; @@ -689,7 +689,8 @@ static CHECK_RESULT IrRegister funccall_generate_ir(FunctionCall *self, IrCompil switch(self->func.resolved_var.type) { case NAMED_OBJECT_NONE: - assert(bool_false); + check(bool_false); + func_sig = NULL; break; case NAMED_OBJECT_LHS_EXPR: { func_lhs_expr = self->func.resolved_var.value.lhs_expr; @@ -697,22 +698,23 @@ static CHECK_RESULT IrRegister funccall_generate_ir(FunctionCall *self, IrCompil func_sig = func_lhs_expr->type.value.signature; } else if(func_lhs_expr->type.type == VARIABLE_TYPE_VARIABLE) { AstResolveData *resolve_data = func_lhs_expr->type.value.variable->resolved_var.resolve_data; - assert(resolve_data->type.type == RESOLVED_TYPE_FUNC_SIG); + check(resolve_data->type.type == RESOLVED_TYPE_FUNC_SIG); func_sig = resolve_data->type.value.func_sig; } else { - assert(func_lhs_expr->rhs_expr && func_lhs_expr->rhs_expr->resolve_data.type.type == RESOLVED_TYPE_FUNC_SIG); + check(func_lhs_expr->rhs_expr && func_lhs_expr->rhs_expr->resolve_data.type.type == RESOLVED_TYPE_FUNC_SIG); func_sig = func_lhs_expr->rhs_expr->resolve_data.type.value.func_sig; } break; } case NAMED_OBJECT_FUNC_PARAM: { func_param_expr = self->func.resolved_var.value.func_param; - assert(func_param_expr->type.type == VARIABLE_TYPE_SIGNATURE); + check(func_param_expr->type.type == VARIABLE_TYPE_SIGNATURE); func_sig = func_param_expr->type.value.signature; break; } } + check(func_sig); func_decl = func_sig->func_decl; /* Push return arguments */ @@ -952,7 +954,7 @@ static CHECK_RESULT IrRegister ast_generate_ir_resolve_data(void *ast_data, AstT resolve_data->ir_reg = 0;/*structdecl_generate_ir(ast_data, context);*/ break; case AST_STRUCT_FIELD: - assert(bool_false); + check(bool_false); resolve_data->ir_reg = 0;/*structfield_generate_ir(ast_data, context);*/ break; case AST_LHS: diff --git a/src/std/file.c b/src/std/file.c index d61e871..f0d769e 100644 --- a/src/std/file.c +++ b/src/std/file.c @@ -170,49 +170,15 @@ int mapped_file_deinit(MappedFile *self) { } #endif -typedef enum { - REGULAR, - DIRECTORY, - OTHER -} FileType; - -/* - TODO: Remove this and instead use fstat, and then read the file. - Otherwise this is not atomic and the file type can change between the calls -*/ -static CHECK_RESULT int file_get_type(const char *filepath, FileType *type) { - struct stat file_stats; - if(stat(filepath, &file_stats) == -1) { - amal_log_error("file_get_type: %s failed, error: %s", filepath, strerror(errno)); - return -1; - } - - if(file_stats.st_mode & S_IFDIR) - *type = DIRECTORY; - else if(file_stats.st_mode & S_IFREG) - *type = REGULAR; - else - *type = OTHER; - return 0; -} - int read_whole_file(const char *filepath, Buffer *data_result) { - FileType file_type; FILE *file; int result; usize bytes_read; - usize size; - - return_if_error(file_get_type(filepath, &file_type)); - if(file_type != REGULAR) { - amal_log_error("Expected file %s to be a regular file", filepath); - return -2; - } + long size; file = fopen(filepath, "rb"); if(!file) { - int error; - error = errno; + int error = errno; amal_log_error("read_whole_file: %s failed, error: %s", filepath, strerror(error)); return error; } @@ -224,6 +190,13 @@ int read_whole_file(const char *filepath, Buffer *data_result) { } size = ftell(file); + if(size == -1) { + int error = errno; + result = error; + amal_log_error("read_whole_file: %s failed, error: %s", filepath, strerror(error)); + goto cleanup; + } + result = fseek(file, 0, SEEK_SET); if(result != 0) { result = ferror(file); @@ -239,7 +212,7 @@ int read_whole_file(const char *filepath, Buffer *data_result) { assert(data_result->capacity == 0 && data_result->size == 0); cleanup_if_error(buffer_append_empty(data_result, size)); bytes_read = fread(data_result->data, 1, data_result->size, file); - if(bytes_read != size) + if(bytes_read != (unsigned long)size) result = ferror(file); cleanup: diff --git a/src/std/misc.c b/src/std/misc.c index f53797d..33ae2be 100644 --- a/src/std/misc.c +++ b/src/std/misc.c @@ -1,4 +1,5 @@ #include "../../include/std/misc.h" +#include <stdlib.h> #if defined(_MSC_VER) u16 byteswap16(u16 value) { @@ -52,3 +53,7 @@ u64 byteswap64(u64 value) { return result; } #endif + +void check_abort() { + abort(); +} |