diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index af697b201..e1b766e47 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -365,6 +365,44 @@ void compile_file_list(BuildOptions *options) compile(); } +static void setup_int_define(const char *id, uint64_t i, Type *type) +{ + TokenType token_type = TOKEN_CONST_IDENT; + id = symtab_add(id, (uint32_t) strlen(id), fnv1a(id, (uint32_t) strlen(id)), &token_type); + Expr *expr = expr_new(EXPR_CONST, INVALID_RANGE); + assert(type_is_integer(type)); + expr_const_set_int(&expr->const_expr, i, type->type_kind); + if (expr_const_will_overflow(&expr->const_expr, type->type_kind)) + { + error_exit("Integer define %s overflow.", id); + } + expr->type = type; + expr->const_expr.narrowable = true; + expr->span = INVALID_RANGE; + expr->resolve_status = RESOLVE_NOT_DONE; + void *previous = stable_set(&global_context.compiler_defines, id, expr); + if (previous) + { + error_exit("Redefined ident %s", id); + } +} + +static void setup_bool_define(const char *id, bool value) +{ + TokenType token_type = TOKEN_CONST_IDENT; + id = symtab_add(id, (uint32_t) strlen(id), fnv1a(id, (uint32_t) strlen(id)), &token_type); + Expr *expr = expr_new(EXPR_CONST, INVALID_RANGE); + expr_const_set_bool(&expr->const_expr, value); + expr->type = type_bool; + expr->span = INVALID_RANGE; + expr->resolve_status = RESOLVE_NOT_DONE; + void *previous = stable_set(&global_context.compiler_defines, id, expr); + if (previous) + { + error_exit("Redefined ident %s", id); + } +} + void compile() { active_target.sources = target_expand_source_names(active_target.source_dirs, ".c3", ".c3t", true); @@ -376,6 +414,19 @@ void compile() symtab_init(active_target.symtab_size ? active_target.symtab_size : 64 * 1024); target_setup(&active_target); + setup_int_define("C_SHORT_SIZE", platform_target.width_c_short, type_long); + setup_int_define("C_INT_SIZE", platform_target.width_c_int, type_long); + setup_int_define("C_LONG_SIZE", platform_target.width_c_long, type_long); + setup_int_define("C_LONG_LONG_SIZE", platform_target.width_c_long_long, type_long); + setup_bool_define("C_CHAR_IS_SIGNED", platform_target.signed_c_char); + setup_bool_define("PLATFORM_BIG_ENDIAN", platform_target.big_endian); + setup_bool_define("PLATFORM_I128_SUPPORTED", platform_target.int128); + setup_int_define("COMPILER_OPT_LEVEL", (uint64_t)active_target.optimization_level, type_int); + setup_int_define("COMPILER_SIZE_OPT_LEVEL", (uint64_t)active_target.size_optimization_level, type_int); + setup_bool_define("COMPILER_SAFE_MODE", active_target.feature.safe_mode); + + type_init_cint(); + if (!vec_size(active_target.sources)) error_exit("No files to compile."); if (active_target.lex_only) { diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 1c5ca5834..c28f07489 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -790,7 +790,7 @@ static inline void llvm_extract_bitvalue_from_array(GenContext *c, BEValue *be_v continue; } - res = LLVMBuildOr(c->builder, element, res, ""); + if (!LLVMIsNull(element)) res = LLVMBuildOr(c->builder, element, res, ""); } if (big_endian) { @@ -893,7 +893,7 @@ static inline void llvm_emit_bitassign_array(GenContext *c, BEValue *result, BEV LLVMValueRef current = llvm_load(c, c->byte_type, byte_ptr, alignment, ""); LLVMValueRef bit = llvm_emit_shl_fixed(c, LLVMConstInt(c->byte_type, 1, 0), start_bit % 8); current = LLVMBuildAnd(c->builder, current, LLVMConstNot(bit), ""); - current = LLVMBuildOr(c->builder, current, value, ""); + if (!LLVMIsNull(value)) current = LLVMBuildOr(c->builder, current, value, ""); llvm_store(c, byte_ptr, current, alignment); return; } @@ -942,7 +942,7 @@ static inline void llvm_emit_bitassign_array(GenContext *c, BEValue *result, BEV // Empty the top bits. current = LLVMBuildAnd(c->builder, current, mask, ""); // Use *or* with the top bits from "res": - current = LLVMBuildOr(c->builder, current, res, ""); + if (!LLVMIsNull(res)) current = LLVMBuildOr(c->builder, current, res, ""); // And store it back. llvm_store(c, byte_ptr, current, alignment); // We now shift the value by the number of bits we used. @@ -962,7 +962,7 @@ static inline void llvm_emit_bitassign_array(GenContext *c, BEValue *result, BEV // Clear the lower bits. current = LLVMBuildAnd(c->builder, current, LLVMConstNot(mask), ""); // Use *or* with the bottom bits from "value": - current = LLVMBuildOr(c->builder, current, value, ""); + if (!LLVMIsNull(value)) current = LLVMBuildOr(c->builder, current, value, ""); // And store it back. llvm_store(c, byte_ptr, current, alignment); continue; diff --git a/src/compiler/semantic_analyser.c b/src/compiler/semantic_analyser.c index 79874db13..b7d1a3764 100644 --- a/src/compiler/semantic_analyser.c +++ b/src/compiler/semantic_analyser.c @@ -137,27 +137,6 @@ static inline void halt_on_error(void) } -static void setup_int_define(const char *id, uint64_t i, Type *type) -{ - TokenType token_type = TOKEN_CONST_IDENT; - id = symtab_add(id, (uint32_t) strlen(id), fnv1a(id, (uint32_t) strlen(id)), &token_type); - Expr *expr = expr_new(EXPR_CONST, INVALID_RANGE); - assert(type_is_integer(type)); - expr_const_set_int(&expr->const_expr, i, type->type_kind); - if (expr_const_will_overflow(&expr->const_expr, type->type_kind)) - { - error_exit("Integer define %s overflow.", id); - } - expr->type = type; - expr->const_expr.narrowable = true; - expr->span = INVALID_RANGE; - expr->resolve_status = RESOLVE_NOT_DONE; - void *previous = stable_set(&global_context.compiler_defines, id, expr); - if (previous) - { - error_exit("Redefined ident %s", id); - } -} void sema_analyze_stage(Module *module, AnalysisStage stage) { @@ -242,21 +221,6 @@ static void register_generic_decls(Module *module, Decl **decls) } -static void setup_bool_define(const char *id, bool value) -{ - TokenType token_type = TOKEN_CONST_IDENT; - id = symtab_add(id, (uint32_t) strlen(id), fnv1a(id, (uint32_t) strlen(id)), &token_type); - Expr *expr = expr_new(EXPR_CONST, INVALID_RANGE); - expr_const_set_bool(&expr->const_expr, value); - expr->type = type_bool; - expr->span = INVALID_RANGE; - expr->resolve_status = RESOLVE_NOT_DONE; - void *previous = stable_set(&global_context.compiler_defines, id, expr); - if (previous) - { - error_exit("Redefined ident %s", id); - } -} static void analyze_generic_module(Module *module) { @@ -279,18 +243,6 @@ static void analyze_to_stage(AnalysisStage stage) void sema_analysis_run(void) { - setup_int_define("C_SHORT_SIZE", platform_target.width_c_short, type_long); - setup_int_define("C_INT_SIZE", platform_target.width_c_int, type_long); - setup_int_define("C_LONG_SIZE", platform_target.width_c_long, type_long); - setup_int_define("C_LONG_LONG_SIZE", platform_target.width_c_long_long, type_long); - setup_bool_define("C_CHAR_IS_SIGNED", platform_target.signed_c_char); - setup_bool_define("PLATFORM_BIG_ENDIAN", platform_target.big_endian); - setup_bool_define("PLATFORM_I128_SUPPORTED", platform_target.int128); - setup_int_define("COMPILER_OPT_LEVEL", (uint64_t)active_target.optimization_level, type_int); - setup_int_define("COMPILER_SIZE_OPT_LEVEL", (uint64_t)active_target.size_optimization_level, type_int); - setup_bool_define("COMPILER_SAFE_MODE", active_target.feature.safe_mode); - - type_init_cint(); global_context_clear_errors(); diff --git a/src/compiler/target.c b/src/compiler/target.c index fae4d92fd..104126f78 100644 --- a/src/compiler/target.c +++ b/src/compiler/target.c @@ -1388,5 +1388,7 @@ void target_setup(BuildTarget *target) // TODO remove type_setup(&platform_target); + + }