From 05c9b7cb33cf7dc240df8ab38721b01a1915a522 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 21 Nov 2021 23:37:54 +0100 Subject: [PATCH] More explicit conversion and width changes to some field. --- CMakeLists.txt | 2 +- src/build/project.c | 4 +- src/compiler/bigint.c | 44 ++--- src/compiler/c_abi_internal.h | 6 +- src/compiler/compiler.c | 18 +- src/compiler/compiler_internal.h | 63 +++---- src/compiler/context.c | 4 +- src/compiler/diagnostics.c | 2 +- src/compiler/lexer.c | 38 ++--- src/compiler/linker.c | 2 +- src/compiler/llvm_codegen.c | 59 ++++--- src/compiler/llvm_codegen_c_abi.c | 9 +- src/compiler/llvm_codegen_c_abi_aarch64.c | 4 +- src/compiler/llvm_codegen_c_abi_x64.c | 18 +- src/compiler/llvm_codegen_c_abi_x86.c | 10 +- src/compiler/llvm_codegen_debug_info.c | 8 +- src/compiler/llvm_codegen_expr.c | 193 +++++++++++----------- src/compiler/llvm_codegen_internal.h | 16 +- src/compiler/llvm_codegen_module.c | 4 +- src/compiler/llvm_codegen_stmt.c | 4 +- src/compiler/llvm_codegen_type.c | 8 +- src/compiler/module.c | 2 +- src/compiler/number.c | 4 +- src/compiler/parse_expr.c | 40 ++--- src/compiler/sema_casts.c | 6 +- src/compiler/sema_decls.c | 46 +++--- src/compiler/sema_expr.c | 106 ++++++------ src/compiler/sema_stmts.c | 12 +- src/compiler/sema_types.c | 10 +- src/compiler/source_file.c | 2 +- src/compiler/symtab.c | 2 +- src/compiler/target.c | 24 +-- src/compiler/types.c | 36 ++-- src/compiler_tests/benchmark.c | 2 +- src/utils/lib.h | 11 +- src/utils/stringutils.c | 8 +- src/utils/taskqueue.c | 2 +- src/utils/toml.c | 4 +- src/utils/whereami.c | 2 +- 39 files changed, 422 insertions(+), 413 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e1d402dbb..3ecb1f454 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -157,7 +157,7 @@ add_executable(c3c if(NOT CMAKE_C_COMPILER_ID STREQUAL "MSVC") message(STATUS "using gcc/clang warning switches") target_compile_options(c3c PRIVATE -Wall -Werror -Wno-unknown-pragmas -Wno-unused-result - -Wimplicit-int-conversion -Wno-unused-function -Wno-unused-variable -Wno-unused-parameter) + -Wconversion -Wno-unused-function -Wno-unused-variable -Wno-unused-parameter) endif() target_include_directories(c3c PRIVATE diff --git a/src/build/project.c b/src/build/project.c index ba45035e7..5eff9509d 100644 --- a/src/build/project.c +++ b/src/build/project.c @@ -67,7 +67,7 @@ static int get_valid_string_setting(TomlTable *table, const char *key, const cha { for (int i = 0; i < count; i++) { - unsigned str_len = strlen(values[i]); + unsigned str_len = (unsigned) strlen(values[i]); if (str_len != value->value.string->len) continue; if (memcmp(values[i], value->value.string->str, str_len) == 0) return i + first_result; } @@ -179,7 +179,7 @@ void project_add_target(Project *project, TomlValue *wrapped_table, const char * { error_exit("Error reading %s: %s symtab may not exceed %d.", PROJECT_TOML, type, MAX_SYMTAB_SIZE); } - target->symtab_size = symtab_size; + target->symtab_size = (uint32_t)symtab_size; } const char *cpu = get_valid_string(table, "cpu", type, false); target->cpu = cpu ? cpu : "generic"; diff --git a/src/compiler/bigint.c b/src/compiler/bigint.c index ba0f63f3b..72089a625 100644 --- a/src/compiler/bigint.c +++ b/src/compiler/bigint.c @@ -54,7 +54,7 @@ char *i128_to_string(Int128 op, uint64_t base, bool is_signed) *(loc++) = digits[rem.low]; op = i128_udiv(op, base_div); } while (!i128_is_zero(op)); - char *res = malloc(loc - buffer + 2); + char *res = malloc((size_t)(loc - buffer + 2)); char *c = res; if (add_minus) *(c++) = '-'; while (loc > buffer) @@ -67,7 +67,7 @@ char *i128_to_string(Int128 op, uint64_t base, bool is_signed) char *int_to_str(Int i, int radix) { - return i128_to_string(i.i, radix, type_kind_is_signed(i.type)); + return i128_to_string(i.i, (uint64_t)radix, type_kind_is_signed(i.type)); } @@ -88,7 +88,7 @@ Int128 i128_from_str(const char *str) Int128 x = { 0, 0 }; while ((c = *(str++)) != 0) { - x = i128_add64(i128_mult64(x, 10), c - '0'); + x = i128_add64(i128_mult64(x, 10), (uint64_t)(c - '0')); } return x; } @@ -100,7 +100,7 @@ Int128 i128_from_strl(const char *str, const char *end) while (str != end) { c = *(str++); - x = i128_add64(i128_mult64(x, 10), c - '0'); + x = i128_add64(i128_mult64(x, 10), (uint64_t) (c - '0')); } return x; } @@ -112,7 +112,7 @@ Int128 i128_from_hexstrl(const char *str, const char *end) while (str != end) { c = *(str++); - x = i128_add64(i128_shl64(x, 4), hex_nibble(c)); + x = i128_add64(i128_shl64(x, 4), (uint64_t)hex_nibble(c)); } return x; } @@ -150,7 +150,7 @@ Int128 i128_extend(Int128 op, TypeKind type) { int bits = type_kind_bitsize(type); if (bits == 128) return op; - int shift = 128 - bits; + uint64_t shift = 128 - (uint64_t)bits; op = i128_shl64(op, shift); bool is_signed = type_kind_is_signed(type); if (is_signed) @@ -291,7 +291,7 @@ Int128 i128_lshr64(Int128 op1, uint64_t amount) Int128 i128_from_float_signed(Real d) { - return (Int128){ 0, (int64_t)d }; + return (Int128){ 0, (uint64_t)((int64_t)d) }; } Int128 i128_from_float_unsigned(Real d) @@ -325,8 +325,8 @@ Int128 i128_ashr64(Int128 op1, uint64_t amount) if (!ISNEG(op1.high)) return i128_lshr64(op1, amount); if (amount > 127) return (Int128){ UINT64_MAX, UINT64_MAX }; if (amount == 64) return (Int128){ UINT64_MAX, op1.high }; - if (amount > 64) return (Int128){ UINT64_MAX, ((int64_t)op1.high) >> (amount - 64) }; - return (Int128){ ((int64_t)op1.high) >> amount, op1.low >> amount | (op1.high << (64 - amount)) }; + if (amount > 64) return (Int128){ UINT64_MAX, (uint64_t)(((int64_t)op1.high) >> (amount - 64)) }; + return (Int128){ (uint64_t)(((int64_t)op1.high) >> amount), op1.low >> amount | (op1.high << (64 - amount)) }; } Int128 i128_ashr(Int128 op1, Int128 op2) @@ -337,7 +337,7 @@ Int128 i128_ashr(Int128 op1, Int128 op2) Int128 i128_add_swrap64(Int128 op1, int64_t op2, bool *wrapped) { - Int128 res = i128_add64(op1, op2); + Int128 res = i128_add64(op1, (uint64_t) op2); bool is_less = i128_scomp(res, op1) == CMP_LT; *wrapped = op2 < 0 ? !is_less : is_less; return res; @@ -467,17 +467,17 @@ uint32_t i128_clz(const Int128 *op) int i128_lsb(const Int128 *op) { - return 127 - i128_ctz(op); + return (int)(127 - i128_ctz(op)); } int i128_msb(const Int128 *op) { - return 127 - i128_clz(op); + return (int)(127 - i128_clz(op)); } Real i128_to_float(Int128 op) { - return (Real)op.low + (Real)ldexp(op.high, 64); + return (Real)op.low + (Real)ldexp((double)op.high, 64); } Real i128_to_float_signed(Int128 op) @@ -486,19 +486,19 @@ Real i128_to_float_signed(Int128 op) { return -i128_to_float_signed(i128_neg(op)); } - return (Real)op.low + (Real)ldexp(op.high, 64); + return (Real)op.low + (Real)ldexp((double)op.high, 64); } void i128_udivrem(Int128 op1, Int128 op2, Int128 *div, Int128 *rem) { *div = (Int128){ 0, 0 }; - int32_t shift = i128_clz(&op2) - i128_clz(&op1); + int32_t shift = (int32_t)(i128_clz(&op2) - i128_clz(&op1)); if (shift < 0) { *rem = op1; return; } - op2 = i128_shl64(op2, shift); + op2 = i128_shl64(op2, (uint64_t)shift); do { *div = i128_shl64(*div, 1); @@ -544,7 +544,7 @@ Int128 i128_srem(Int128 op1, Int128 op2) Int128 i128_from_signed(int64_t i) { - return (Int128){ i < 0 ? UINT64_MAX : 0, i }; + return (Int128){ i < 0 ? UINT64_MAX : 0, (uint64_t)i }; } Int128 i128_from_unsigned(uint64_t i) @@ -713,7 +713,7 @@ uint64_t int_to_u64(Int op) int64_t int_to_i64(Int op) { - return op.i.low; + return (int64_t)op.i.low; } bool int_is_zero(Int op) @@ -766,13 +766,13 @@ Int int_conv(Int op, TypeKind to_type) return (Int){ op.i, to_type }; } // Extending from a signed to unsigned - int shift = 128 - to_bitsize; + uint64_t shift = (uint64_t)(128 - to_bitsize); // Cut off the top of the signed bits // 11101 -> 11010 -> 01101 return (Int){ i128_lshr64(i128_shl64(op.i, shift), shift), to_type }; } // The other case is cutting down bits. - int shift = 128 - to_bitsize; + uint64_t shift = (uint64_t)(128 - to_bitsize); Int128 without_top_bits = i128_lshr64(i128_shl64(op.i, shift), shift); if (to_signed) { @@ -892,7 +892,7 @@ Int128 i128_from_double(double x) if (x >= ldexp(1, 64)) { uint64_t hi = (uint64_t)ldexp(x, -64); - uint64_t lo = (uint64_t)(x - ldexp(hi, 64)); + uint64_t lo = (uint64_t)(x - ldexp((double)hi, 64)); return (Int128){ hi, lo }; } return i128_from_int((uint64_t)x); @@ -900,5 +900,5 @@ Int128 i128_from_double(double x) Int128 i128_from_double_signed(double x) { - return x < 0 ? i128_neg(i128_from_signed(-x)) : i128_from_int(x); + return x < 0 ? i128_neg(i128_from_signed((int64_t)-x)) : i128_from_int((uint64_t)x); } diff --git a/src/compiler/c_abi_internal.h b/src/compiler/c_abi_internal.h index 5ae8bdd28..f5f33742e 100644 --- a/src/compiler/c_abi_internal.h +++ b/src/compiler/c_abi_internal.h @@ -27,12 +27,12 @@ ABIArgInfo *abi_arg_new_indirect_realigned(AlignSize alignment, Type *by_val_typ ABIArgInfo *abi_arg_new_indirect_by_val(Type *by_val_type); ABIArgInfo *abi_arg_new_indirect_not_by_val(Type *type); -ByteSize abi_type_abi_alignment(AbiType *type); +AlignSize abi_type_abi_alignment(AbiType *type); bool abi_type_is_integer(AbiType *type); bool abi_type_is_float(AbiType *type); AbiType *abi_type_new_plain(Type *type); -AbiType *abi_type_new_int_bits(unsigned bits); -ByteSize abi_type_size(AbiType *type); +AbiType *abi_type_new_int_bits(ByteSize bits); +TypeSize abi_type_size(AbiType *type); typedef struct { diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 7f07f20ed..f445ddfb4 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -231,7 +231,7 @@ static void add_global_define(const char *name, Expr *value) { Decl *dec = decl_calloc(); TokenType type = TOKEN_CONST_IDENT; - const char *unique_name = symtab_add(name, strlen(name), fnv1a(name, strlen(name)), &type); + const char *unique_name = symtab_add(name, (uint32_t)strlen(name), fnv1a(name, (uint32_t)strlen(name)), &type); dec->name = unique_name; dec->module = &global_context.std_module; dec->visibility = VISIBLE_PUBLIC; @@ -251,7 +251,7 @@ static void add_global_define(const char *name, Expr *value) static void setup_int_define(const char *id, uint64_t i, Type *type) { TokenType token_type = TOKEN_CONST_IDENT; - id = symtab_add(id, strlen(id), fnv1a(id, strlen(id)), &token_type); + 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); @@ -273,7 +273,7 @@ static void setup_int_define(const char *id, uint64_t i, Type *type) static void setup_bool_define(const char *id, bool value) { TokenType token_type = TOKEN_CONST_IDENT; - id = symtab_add(id, strlen(id), fnv1a(id, strlen(id)), &token_type); + 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; @@ -295,8 +295,8 @@ void compiler_compile(void) 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", (int)active_target.optimization_level, type_int); - setup_int_define("COMPILER_SIZE_OPT_LEVEL", (int)active_target.size_optimization_level, type_int); + 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); global_context_clear_errors(); @@ -316,7 +316,7 @@ void compiler_compile(void) if (has_error) exit(EXIT_FAILURE); - global_context.std_module_path = (Path) { .module = kw_std, .span = INVALID_RANGE, .len = strlen(kw_std) }; + global_context.std_module_path = (Path) { .module = kw_std, .span = INVALID_RANGE, .len = (uint32_t) strlen(kw_std) }; global_context.std_module = (Module){ .name = &global_context.std_module_path }; global_context.std_module.stage = ANALYSIS_LAST; stable_init(&global_context.std_module.symbols, 0x10000); @@ -407,7 +407,7 @@ void compiler_compile(void) bool create_exe = !active_target.no_link && !active_target.test_output && (active_target.type == TARGET_TYPE_EXECUTABLE || active_target.type == TARGET_TYPE_TEST); - size_t output_file_count = vec_size(gen_contexts); + uint32_t output_file_count = vec_size(gen_contexts); if (output_file_count > MAX_OUTPUT_FILES) { error_exit("Too many output files."); @@ -623,7 +623,7 @@ void scratch_buffer_append(const char *string) void scratch_buffer_append_signed_int(int64_t i) { - int len_needed = sprintf(&global_context.scratch_buffer[global_context.scratch_buffer_len], "%lld", (long long)i); + uint32_t len_needed = (uint32_t)sprintf(&global_context.scratch_buffer[global_context.scratch_buffer_len], "%lld", (long long)i); if (global_context.scratch_buffer_len + len_needed > MAX_STRING_BUFFER - 1) { error_exit("Scratch buffer size (%d chars) exceeded", MAX_STRING_BUFFER - 1); @@ -633,7 +633,7 @@ void scratch_buffer_append_signed_int(int64_t i) void scratch_buffer_append_unsigned_int(uint64_t i) { - int len_needed = sprintf(&global_context.scratch_buffer[global_context.scratch_buffer_len], "%llu", (unsigned long long)i); + uint32_t len_needed = (uint32_t)sprintf(&global_context.scratch_buffer[global_context.scratch_buffer_len], "%llu", (unsigned long long)i); if (global_context.scratch_buffer_len + len_needed > MAX_STRING_BUFFER - 1) { error_exit("Scratch buffer size (%d chars) exceeded", MAX_STRING_BUFFER - 1); diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index ad1e12514..ea202524e 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -15,13 +15,15 @@ typedef double Real; -#define MAX_ARRAYINDEX INT64_MAX +#define MAX_ARRAYINDEX INT32_MAX typedef uint64_t ByteSize; -typedef int64_t ArrayIndex; -typedef int64_t IndexDiff; +typedef uint32_t TypeSize; +typedef int32_t IndexDiff; typedef int32_t MemberIndex; -typedef int32_t AlignSize; +typedef uint32_t AlignSize; typedef int32_t ScopeId; +typedef uint32_t ArraySize; +typedef uint64_t BitSize; #if PLATFORM_WINDOWS @@ -55,7 +57,8 @@ typedef struct #define MAX_FUNCTION_SIGNATURE_SIZE 2048 #define MAX_PARAMS 512 #define MAX_MEMBERS ((MemberIndex)(((uint64_t)2) << 28)) -#define MAX_ALIGNMENT ((ArrayIndex)(((uint64_t)2) << 28)) +#define MAX_ALIGNMENT ((MemberIndex)(((uint64_t)2) << 28)) +#define MAX_TYPE_SIZE UINT32_MAX #define MAX_OFFSET ((ArrayIndex)(((uint64_t)2) << 60)) typedef struct Ast_ Ast; @@ -123,7 +126,7 @@ typedef struct ConstInitializer_ struct { struct ConstInitializer_ *element; - ArrayIndex index; + MemberIndex index; } init_array_value; }; } ConstInitializer; @@ -153,7 +156,7 @@ typedef struct struct { const char *ptr; - uint64_t len; + TypeSize len; } bytes; Type *typeid; ConstInitializer *list; @@ -183,7 +186,7 @@ typedef struct SourceLoc end_id; SourceLoc *lines; SourceLoc current_line_start; - unsigned token_start_id; + uint32_t token_start_id; } File; typedef struct @@ -253,13 +256,13 @@ typedef struct typedef struct { Type *base; - ByteSize len; + TypeSize len; } TypeArray; typedef struct { Type *base; - ByteSize len; + TypeSize len; } TypeVector; typedef struct @@ -341,10 +344,10 @@ typedef struct typedef struct { - uint64_t size; + TypeSize size; Decl **members; MemberIndex union_rep; - int16_t padding; + AlignSize padding : 16; } StructDecl; @@ -377,14 +380,14 @@ typedef struct VarDecl_ void *backend_debug_ref; unsigned scope_depth; Expr *start; - int start_bit; + unsigned start_bit; }; union { void *failable_ref; struct ABIArgInfo_ *abi_info; Expr *end; - int end_bit; + unsigned end_bit; }; } VarDecl; @@ -584,8 +587,8 @@ typedef struct Decl_ const char *extname; AlignSize alignment; const char *section; - ArrayIndex offset : 32; - ArrayIndex padding : 32; + AlignSize offset : 32; + AlignSize padding : 32; /* bool is_exported : 1; bool is_used : 1; bool is_used_public : 1; @@ -746,8 +749,8 @@ typedef struct DesignatorElement_ Expr *index_end_expr; }; }; - ArrayIndex index; - ArrayIndex index_end; + MemberIndex index; + MemberIndex index_end; } DesignatorElement; typedef struct @@ -782,7 +785,7 @@ typedef struct bool array : 1; union { - ArrayIndex index; + MemberIndex index; const char *ident; }; } ExprFlatElement; @@ -1428,7 +1431,7 @@ typedef struct unsigned errors_found; unsigned warnings_found; char scratch_buffer[MAX_STRING_BUFFER]; - size_t scratch_buffer_len; + uint32_t scratch_buffer_len; STable scratch_table; STable compiler_defines; Module std_module; @@ -1849,7 +1852,7 @@ void expr_const_set_null(ExprConst *expr); bool expr_const_compare(const ExprConst *left, const ExprConst *right, BinaryOp op); bool expr_const_will_overflow(const ExprConst *expr, TypeKind kind); -ByteSize expr_const_list_size(const ConstInitializer *list); +ArraySize expr_const_list_size(const ConstInitializer *list); void expr_insert_addr(Expr *original); void expr_insert_deref(Expr *expr); @@ -1902,7 +1905,7 @@ static inline TokenType token_type(Token token) { return (TokenType)toktypeptr(t Decl *module_find_symbol(Module *module, const char *symbol); bool parse_file(File *file); -Path *path_create_from_string(const char *string, size_t len, SourceSpan span); +Path *path_create_from_string(const char *string, uint32_t len, SourceSpan span); Path *path_find_parent_path(Path *path); #define SEMA_TOKEN_ERROR(_tok, ...) sema_error_range(source_span_from_token_id(_tok.id), __VA_ARGS__) @@ -1929,7 +1932,7 @@ bool sema_erase_unwrapped(Context *context, Decl *decl); bool sema_analyse_cond_expr(Context *context, Expr *expr); bool sema_analyse_expr_rhs(Context *context, Type *to, Expr *expr, bool allow_failable); -ArrayIndex sema_get_initializer_const_array_size(Context *context, Expr *initializer, bool *may_be_array, bool *is_const_size); +MemberIndex sema_get_initializer_const_array_size(Context *context, Expr *initializer, bool *may_be_array, bool *is_const_size); bool sema_analyse_expr(Context *context, Expr *expr); bool sema_analyse_inferred_expr(Context *context, Type *to, Expr *expr); bool sema_analyse_decl(Context *context, Decl *decl); @@ -1948,11 +1951,11 @@ Decl *sema_resolve_normal_symbol(Context *context, TokenId symbol, Path *path, b Decl *sema_resolve_string_symbol(Context *context, const char *symbol, SourceSpan span, Path *path, bool report_error); bool sema_resolve_type(Context *context, Type *type); -bool sema_resolve_array_like_len(Context *context, TypeInfo *type_info, ArrayIndex *len_ref); +bool sema_resolve_array_like_len(Context *context, TypeInfo *type_info, ArraySize *len_ref); bool sema_resolve_type_info(Context *context, TypeInfo *type_info); bool sema_resolve_type_info_maybe_inferred(Context *context, TypeInfo *type_info, bool allow_inferred_type); bool sema_resolve_type_shallow(Context *context, TypeInfo *type_info, bool allow_inferred_type, bool in_shallow); -Type *sema_type_lower_by_size(Type *type, ByteSize element_size); +Type *sema_type_lower_by_size(Type *type, ArraySize element_size); void sema_error_at_prev_end(Token token, const char *message, ...); @@ -2027,7 +2030,7 @@ Type *type_find_max_type(Type *type, Type *other); Type *type_abi_find_single_struct_element(Type *type); const char *type_generate_qname(Type *type); bool type_is_valid_for_vector(Type *type); -Type *type_get_array(Type *arr_type, ByteSize len); +Type *type_get_array(Type *arr_type, ArraySize len); Type *type_get_indexed_type(Type *type); Type *type_get_ptr(Type *ptr_type); Type *type_get_ptr_recurse(Type *ptr_type); @@ -2056,7 +2059,7 @@ static inline bool type_is_promotable_float(Type *type); static inline bool type_is_promotable_integer(Type *type); static inline bool type_is_signed(Type *type); static inline bool type_is_structlike(Type *type); -static inline size_t type_min_alignment(size_t a, size_t b); +static inline AlignSize type_min_alignment(AlignSize a, AlignSize b); bool type_is_subtype(Type *type, Type *possible_subtype); Type *type_from_token(TokenType type); bool type_is_union_struct(Type *type); @@ -2067,7 +2070,7 @@ static inline bool type_is_vector(Type *type) { return type_flatten(type)->type_ bool type_is_float_or_float_vector(Type *type); bool type_may_have_sub_elements(Type *type); static inline bool type_ok(Type *type); -ByteSize type_size(Type *type); +TypeSize type_size(Type *type); const char *type_to_error_string(Type *type); const char *type_quoted_error_string(Type *type); @@ -2164,7 +2167,7 @@ static inline bool type_is_pointer(Type *type) return kind == TYPE_POINTER; } -static inline ByteSize aligned_offset(uint64_t offset, uint64_t alignment) +static inline AlignSize aligned_offset(AlignSize offset, AlignSize alignment) { return ((offset + alignment - 1) / alignment) * alignment; } @@ -2450,7 +2453,7 @@ TypeInfo *copy_type_info(TypeInfo *source); * Minimum alignment, values are either offsets or alignments. * @return */ -static inline size_t type_min_alignment(size_t a, size_t b) +static inline AlignSize type_min_alignment(AlignSize a, AlignSize b) { return (a | b) & (1 + ~(a | b)); } diff --git a/src/compiler/context.c b/src/compiler/context.c index fd51268c5..70ec3b2fa 100644 --- a/src/compiler/context.c +++ b/src/compiler/context.c @@ -90,8 +90,8 @@ bool context_set_module_from_filename(Context *context) TokenType type = TOKEN_IDENT; const char *module_name = symtab_add(global_context.scratch_buffer, - global_context.scratch_buffer_len, - fnv1a(global_context.scratch_buffer, global_context.scratch_buffer_len), + global_context.scratch_buffer_len, + fnv1a(global_context.scratch_buffer, (uint32_t) global_context.scratch_buffer_len), &type); if (type != TOKEN_IDENT) diff --git a/src/compiler/diagnostics.c b/src/compiler/diagnostics.c index 17a929258..2366ac9b9 100644 --- a/src/compiler/diagnostics.c +++ b/src/compiler/diagnostics.c @@ -34,7 +34,7 @@ static void print_error2(SourceLocation *location, const char *message, PrintTyp } static const int LINES_SHOWN = 4; - unsigned max_line_length = (int)round(log10(location->line)) + 1; + unsigned max_line_length = (unsigned)round(log10(location->line)) + 1; char number_buffer[20]; snprintf(number_buffer, 20, "%%%dd: %%.*s\n", max_line_length); diff --git a/src/compiler/lexer.c b/src/compiler/lexer.c index 1aee2d969..8929e6b19 100644 --- a/src/compiler/lexer.c +++ b/src/compiler/lexer.c @@ -45,7 +45,7 @@ void lexer_store_line_end(Lexer *lexer) { lexer->current_line++; lexer->line_start = lexer->current + 1; - source_file_append_line_end(lexer->current_file, lexer->current_file->start_id + lexer->current - lexer->file_begin); + source_file_append_line_end(lexer->current_file, (SourceLoc)(lexer->current_file->start_id + lexer->current - lexer->file_begin)); } // Peek one character ahead. @@ -104,7 +104,7 @@ static inline void add_generic_token(Lexer *lexer, TokenType type) // Set the location. location->file = lexer->current_file; - location->start = lexer->lexing_start - lexer->file_begin; + location->start = (uint32_t)(lexer->lexing_start - lexer->file_begin); // Calculate the column if (lexer->lexing_start < lexer->line_start) @@ -129,11 +129,11 @@ static inline void add_generic_token(Lexer *lexer, TokenType type) // The simple case, where the parsing started on the current line. location->line = lexer->current_line; // Col is simple difference. - location->col = (unsigned)(lexer->lexing_start - lexer->line_start) + 1; + location->col = (unsigned) (lexer->lexing_start - lexer->line_start) + 1; // Start is offset to file begin. - location->start = lexer->lexing_start - lexer->file_begin; + location->start = (SourceLoc) (lexer->lexing_start - lexer->file_begin); // Length is diff between current and start. - location->length = lexer->current - lexer->lexing_start; + location->length = (SourceLoc) (lexer->current - lexer->lexing_start); } // Return pointers to the data and the location, // these maybe be used to fill in data. @@ -158,10 +158,10 @@ static bool add_error_token_at(Lexer *lexer, const char *loc, uint32_t len, cons va_list list; va_start(list, message); SourceLocation location = { .file = lexer->current_file, - .start = loc - lexer->file_begin, + .start = (uint32_t) (loc - lexer->file_begin), .line = lexer->current_line, .length = len, - .col = (uint32_t)(loc - lexer->line_start) + 1, + .col = (uint32_t) (loc - lexer->line_start) + 1, }; sema_verror_range(&location, message, list); va_end(list); @@ -339,7 +339,7 @@ static inline bool scan_ident(Lexer *lexer, TokenType normal, TokenType const_to hash = FNV1a(next(lexer), hash); } EXIT:; - uint32_t len = lexer->current - lexer->lexing_start; + uint32_t len = (uint32_t)(lexer->current - lexer->lexing_start); if (!type) { if (!prefix && len == 1) return add_token(lexer, TOKEN_UNDERSCORE, "_"); @@ -664,7 +664,7 @@ static inline int64_t scan_utf8(Lexer *lexer, unsigned char c) } result += c & 0x3f; } - return result; + return (int64_t)result; ERROR: add_error_token(lexer, "Invalid UTF-8 sequence."); return -1; @@ -704,7 +704,7 @@ static inline bool scan_char(Lexer *lexer) { if (width != 0) goto UNICODE_IN_MULTI; const char *start = lexer->current; - int64_t utf8 = scan_utf8(lexer, c); + int64_t utf8 = scan_utf8(lexer, (unsigned char)c); if (utf8 < 0) return false; if (!match(lexer, '\'')) { @@ -713,7 +713,7 @@ static inline bool scan_char(Lexer *lexer) return add_error_token(lexer, "Unicode character literals may only contain one character, " "please remove the additional ones or use all ASCII."); } - b.low = utf8; + b.low = (uint64_t) utf8; width = utf8 > 0xffff ? 4 : 2; goto DONE; } @@ -773,7 +773,7 @@ static inline bool scan_char(Lexer *lexer) escape); } // Assign the value and go to DONE. - b.low = hex; + b.low = (uint64_t) hex; width = bytes; goto DONE; } @@ -781,7 +781,7 @@ static inline bool scan_char(Lexer *lexer) // No escape, a regular character. break; default: - c = (unsigned char)escape; + c = (signed char)escape; break; } // Default handling here: @@ -845,7 +845,7 @@ static int append_esc_string_token(char *restrict dest, const char *restrict src int h = char_to_nibble(src[1]); int l = char_to_nibble(src[2]); if (h < 0 || l < 0) return -1; - unicode_char = ((unsigned) h << 4U) + l; + unicode_char = ((unsigned) h << 4U) + (unsigned)l; scanned = 3; break; } @@ -856,7 +856,7 @@ static int append_esc_string_token(char *restrict dest, const char *restrict src int x3 = char_to_nibble(src[3]); int x4 = char_to_nibble(src[4]); if (x1 < 0 || x2 < 0 || x3 < 0 || x4 < 0) return -1; - unicode_char = ((unsigned) x1 << 12U) + ((unsigned) x2 << 8U) + ((unsigned) x3 << 4U) + x4; + unicode_char = ((unsigned) x1 << 12U) + ((unsigned) x2 << 8U) + ((unsigned) x3 << 4U) + (unsigned)x4; scanned = 5; break; } @@ -872,7 +872,7 @@ static int append_esc_string_token(char *restrict dest, const char *restrict src int x8 = char_to_nibble(src[8]); if (x1 < 0 || x2 < 0 || x3 < 0 || x4 < 0 || x5 < 0 || x6 < 0 || x7 < 0 || x8 < 0) return -1; unicode_char = ((unsigned) x1 << 28U) + ((unsigned) x2 << 24U) + ((unsigned) x3 << 20U) + ((unsigned) x4 << 16U) + - ((unsigned) x5 << 12U) + ((unsigned) x6 << 8U) + ((unsigned) x7 << 4U) + x8; + ((unsigned) x5 << 12U) + ((unsigned) x6 << 8U) + ((unsigned) x7 << 4U) + (unsigned)x8; scanned = 9; break; } @@ -1123,7 +1123,7 @@ static inline bool scan_string(Lexer *lexer) } } const char *end = current - 1; - char *destination = malloc_arena(end - lexer->current + 1); + char *destination = malloc_arena((size_t)(end - lexer->current + 1)); size_t len = 0; while (lexer->current < end) { @@ -1182,7 +1182,7 @@ static inline bool scan_raw_string(Lexer *lexer) } const char *current = lexer->lexing_start + 1; const char *end = lexer->current - 1; - size_t len = end - current; + size_t len = (size_t)(end - current); char *destination = malloc_arena(len + 1); len = 0; while (current < end) @@ -1752,7 +1752,7 @@ File* lexer_current_file(Lexer *lexer) void lexer_init_with_file(Lexer *lexer, File *file) { - file->token_start_id = toktype_arena.allocated; + file->token_start_id = (uint32_t) toktype_arena.allocated; lexer->current_file = file; lexer->file_begin = lexer->current_file->contents; lexer->lexing_start = lexer->file_begin; diff --git a/src/compiler/linker.c b/src/compiler/linker.c index b6dd59f1b..658525874 100644 --- a/src/compiler/linker.c +++ b/src/compiler/linker.c @@ -267,7 +267,7 @@ const char *concat_string_parts(const char **args) char *ptr = output; VECEACH(args, i) { - unsigned len = strlen(args[i]); + unsigned len = (unsigned)strlen(args[i]); memcpy(ptr, args[i], len); ptr += len; *(ptr++) = ' '; diff --git a/src/compiler/llvm_codegen.c b/src/compiler/llvm_codegen.c index b26039f91..03f857b6d 100644 --- a/src/compiler/llvm_codegen.c +++ b/src/compiler/llvm_codegen.c @@ -55,7 +55,7 @@ static void gencontext_destroy(GenContext *context) free(context); } -LLVMValueRef llvm_emit_memclear_size_align(GenContext *c, LLVMValueRef ref, uint64_t size, unsigned int align, bool bitcast) +LLVMValueRef llvm_emit_memclear_size_align(GenContext *c, LLVMValueRef ref, uint64_t size, AlignSize align, bool bitcast) { LLVMValueRef target = bitcast ? LLVMBuildBitCast(c->builder, ref, llvm_get_type(c, type_get_ptr(type_char)), "") : ref; @@ -78,7 +78,7 @@ void llvm_emit_memclear(GenContext *c, BEValue *ref) if (single_type && !type_is_abi_aggregate(single_type)) { BEValue element; - llvm_value_set_address_align(&element, llvm_emit_bitcast(c, ref->value, type_get_ptr(single_type)), single_type, ref->alignment); + llvm_value_set_address_align(&element, llvm_emit_bitcast(c, ref->value, type_get_ptr(single_type)), single_type, (unsigned)ref->alignment); llvm_emit_memclear(c, &element); return; } @@ -138,10 +138,10 @@ LLVMValueRef llvm_emit_const_initializer(GenContext *c, ConstInitializer *const_ LLVMTypeRef element_type_llvm = llvm_get_type(c, element_type); ConstInitializer **elements = const_init->init_array_full; assert(array_type->type_kind == TYPE_ARRAY || array_type->type_kind == TYPE_VECTOR); - ArrayIndex size = array_type->array.len; + ArraySize size = array_type->array.len; assert(size > 0); LLVMValueRef *parts = VECNEW(LLVMValueRef, size); - for (ArrayIndex i = 0; i < size; i++) + for (MemberIndex i = 0; i < size; i++) { LLVMValueRef element = llvm_emit_const_initializer(c, elements[i]); if (element_type_llvm != LLVMTypeOf(element)) was_modified = true; @@ -168,7 +168,7 @@ LLVMValueRef llvm_emit_const_initializer(GenContext *c, ConstInitializer *const_ ConstInitializer **elements = const_init->init_array.elements; unsigned element_count = vec_size(elements); assert(element_count > 0 && "Array should always have gotten at least one element."); - ArrayIndex current_index = 0; + MemberIndex current_index = 0; unsigned alignment = 0; LLVMValueRef *parts = NULL; bool pack = false; @@ -176,7 +176,7 @@ LLVMValueRef llvm_emit_const_initializer(GenContext *c, ConstInitializer *const_ { ConstInitializer *element = elements[i]; assert(element->kind == CONST_INIT_ARRAY_VALUE); - ArrayIndex element_index = element->init_array_value.index; + MemberIndex element_index = element->init_array_value.index; IndexDiff diff = element_index - current_index; if (alignment && expected_align != alignment) { @@ -194,7 +194,7 @@ LLVMValueRef llvm_emit_const_initializer(GenContext *c, ConstInitializer *const_ current_index = element_index + 1; } - IndexDiff end_diff = array_type->array.len - current_index; + IndexDiff end_diff = (MemberIndex)array_type->array.len - current_index; if (end_diff > 0) { vec_add(parts, llvm_emit_const_array_padding(element_type_llvm, end_diff, &was_modified)); @@ -220,18 +220,17 @@ LLVMValueRef llvm_emit_const_initializer(GenContext *c, ConstInitializer *const_ LLVMTypeRef first_type = LLVMStructGetTypeAtIndex(union_type_llvm, 0); // We need to calculate some possible padding. - ByteSize union_size = type_size(const_init->type); - ByteSize member_size = llvm_abi_size(c, result_type); - ByteSize padding = union_size - member_size; + TypeSize union_size = type_size(const_init->type); + TypeSize member_size = llvm_abi_size(c, result_type); // Create the resulting values: LLVMValueRef values[2] = { result, NULL }; unsigned value_count = 1; // Add possible padding as and i8 array. - if (padding > 0) + if (union_size > member_size) { - values[1] = llvm_emit_const_padding(c, padding); + values[1] = llvm_emit_const_padding(c, union_size - member_size); value_count = 2; } @@ -252,7 +251,7 @@ LLVMValueRef llvm_emit_const_initializer(GenContext *c, ConstInitializer *const_ } Decl *decl = const_init->type->decl; Decl **members = decl->strukt.members; - MemberIndex count = vec_size(members); + uint32_t count = vec_size(members); LLVMValueRef *entries = NULL; bool was_modified = false; for (MemberIndex i = 0; i < count; i++) @@ -356,7 +355,7 @@ void llvm_emit_global_variable_init(GenContext *c, Decl *decl) LLVMValueRef init_value; Type *var_type = type_lowering(decl->type); - ByteSize alignment = type_alloca_alignment(var_type); + AlignSize alignment = type_alloca_alignment(var_type); Expr *init_expr = decl->var.init_expr; if (init_expr) @@ -823,7 +822,7 @@ bool llvm_value_is_const(BEValue *value) return LLVMIsConstant(value->value); } -void llvm_value_set_address_align(BEValue *value, LLVMValueRef llvm_value, Type *type, unsigned alignment) +void llvm_value_set_address_align(BEValue *value, LLVMValueRef llvm_value, Type *type, AlignSize alignment) { value->value = llvm_value; value->alignment = alignment; @@ -994,12 +993,12 @@ const char *llvm_codegen(void *context) LLVMModuleRef module = c->module; // Starting from here we could potentially thread this: LLVMPassManagerBuilderRef pass_manager_builder = LLVMPassManagerBuilderCreate(); - LLVMPassManagerBuilderSetOptLevel(pass_manager_builder, active_target.optimization_level); - LLVMPassManagerBuilderSetSizeLevel(pass_manager_builder, active_target.size_optimization_level); + LLVMPassManagerBuilderSetOptLevel(pass_manager_builder, (unsigned)active_target.optimization_level); + LLVMPassManagerBuilderSetSizeLevel(pass_manager_builder, (unsigned)active_target.size_optimization_level); LLVMPassManagerBuilderSetDisableUnrollLoops(pass_manager_builder, active_target.optimization_level == OPTIMIZATION_NONE); if (active_target.optimization_level != OPTIMIZATION_NONE) { - LLVMPassManagerBuilderUseInlinerWithThreshold(pass_manager_builder, get_inlining_threshold()); + LLVMPassManagerBuilderUseInlinerWithThreshold(pass_manager_builder, (unsigned)get_inlining_threshold()); } LLVMPassManagerRef pass_manager = LLVMCreatePassManager(); LLVMPassManagerRef function_pass_manager = LLVMCreateFunctionPassManagerForModule(module); @@ -1128,13 +1127,13 @@ void *llvm_gen(Module *module) void llvm_attribute_add_int(GenContext *context, LLVMValueRef value_to_add_attribute_to, unsigned attribute_id, uint64_t val, int index) { LLVMAttributeRef llvm_attr = LLVMCreateEnumAttribute(context->context, attribute_id, val); - LLVMAddAttributeAtIndex(value_to_add_attribute_to, index, llvm_attr); + LLVMAddAttributeAtIndex(value_to_add_attribute_to, (LLVMAttributeIndex)index, llvm_attr); } void llvm_attribute_add_type(GenContext *c, LLVMValueRef value_to_add_attribute_to, unsigned attribute_id, LLVMTypeRef type, int index) { LLVMAttributeRef llvm_attr = LLVMCreateTypeAttribute(c->context, attribute_id, type); - LLVMAddAttributeAtIndex(value_to_add_attribute_to, index, llvm_attr); + LLVMAddAttributeAtIndex(value_to_add_attribute_to, (LLVMAttributeIndex)index, llvm_attr); } void llvm_attribute_add(GenContext *context, LLVMValueRef value_to_add_attribute_to, unsigned attribute_id, int index) @@ -1145,13 +1144,13 @@ void llvm_attribute_add(GenContext *context, LLVMValueRef value_to_add_attribute void llvm_attribute_add_call_type(GenContext *c, LLVMValueRef call, unsigned attribute_id, int index, LLVMTypeRef type) { LLVMAttributeRef llvm_attr = LLVMCreateTypeAttribute(c->context, attribute_id, type); - LLVMAddCallSiteAttribute(call, index, llvm_attr); + LLVMAddCallSiteAttribute(call, (LLVMAttributeIndex)index, llvm_attr); } void llvm_attribute_add_call(GenContext *context, LLVMValueRef call, unsigned attribute_id, int index, int64_t value) { - LLVMAttributeRef llvm_attr = LLVMCreateEnumAttribute(context->context, attribute_id, value); - LLVMAddCallSiteAttribute(call, index, llvm_attr); + LLVMAttributeRef llvm_attr = LLVMCreateEnumAttribute(context->context, attribute_id, (uint64_t)value); + LLVMAddCallSiteAttribute(call, (LLVMAttributeIndex)index, llvm_attr); } void llvm_attribute_add_range(GenContext *context, LLVMValueRef value_to_add_attribute_to, unsigned attribute_id, int index_start, int index_end) @@ -1164,17 +1163,17 @@ void llvm_attribute_add_range(GenContext *context, LLVMValueRef value_to_add_att void llvm_attribute_add_string(GenContext *context, LLVMValueRef value_to_add_attribute_to, const char *attribute, const char *value, int index) { - LLVMAttributeRef llvm_attr = LLVMCreateStringAttribute(context->context, attribute, strlen(attribute), value, strlen(value)); - LLVMAddAttributeAtIndex(value_to_add_attribute_to, index, llvm_attr); + LLVMAttributeRef llvm_attr = LLVMCreateStringAttribute(context->context, attribute, (unsigned)strlen(attribute), value, (unsigned)strlen(value)); + LLVMAddAttributeAtIndex(value_to_add_attribute_to, (LLVMAttributeIndex)index, llvm_attr); } -unsigned llvm_bitsize(GenContext *c, LLVMTypeRef type) +BitSize llvm_bitsize(GenContext *c, LLVMTypeRef type) { return LLVMSizeOfTypeInBits(c->target_data, type); } -unsigned llvm_abi_size(GenContext *c, LLVMTypeRef type) +TypeSize llvm_abi_size(GenContext *c, LLVMTypeRef type) { - return LLVMABISizeOfType(c->target_data, type); + return (TypeSize)LLVMABISizeOfType(c->target_data, type); } AlignSize llvm_abi_alignment(GenContext *c, LLVMTypeRef type) @@ -1281,9 +1280,9 @@ LLVMValueRef llvm_emit_load_aligned(GenContext *c, LLVMTypeRef type, LLVMValueRe return value; } -unsigned llvm_store_size(GenContext *c, LLVMTypeRef type) +TypeSize llvm_store_size(GenContext *c, LLVMTypeRef type) { - return LLVMStoreSizeOfType(c->target_data, type); + return (TypeSize)LLVMStoreSizeOfType(c->target_data, type); } void llvm_set_error_exit(GenContext *c, LLVMBasicBlockRef block) diff --git a/src/compiler/llvm_codegen_c_abi.c b/src/compiler/llvm_codegen_c_abi.c index e12c03785..e822e4f76 100644 --- a/src/compiler/llvm_codegen_c_abi.c +++ b/src/compiler/llvm_codegen_c_abi.c @@ -26,11 +26,12 @@ AbiType *abi_type_new_plain(Type *type) return abi_type; } -AbiType *abi_type_new_int_bits(unsigned bits) +AbiType *abi_type_new_int_bits(ByteSize bits) { + assert(bits < UINT32_MAX); AbiType *abi_type = CALLOCS(AbiType); abi_type->kind = ABI_TYPE_INT_BITS; - abi_type->int_bits = bits; + abi_type->int_bits = (uint32_t)bits; return abi_type; } @@ -44,7 +45,7 @@ bool abi_type_is_float(AbiType *type) return type->kind != ABI_TYPE_INT_BITS && type_is_float(type->type); } -ByteSize abi_type_size(AbiType *type) +TypeSize abi_type_size(AbiType *type) { switch (type->kind) { @@ -56,7 +57,7 @@ ByteSize abi_type_size(AbiType *type) UNREACHABLE; } -ByteSize abi_type_abi_alignment(AbiType *type) +AlignSize abi_type_abi_alignment(AbiType *type) { switch (type->kind) { diff --git a/src/compiler/llvm_codegen_c_abi_aarch64.c b/src/compiler/llvm_codegen_c_abi_aarch64.c index b8a4538a2..b709a49b1 100644 --- a/src/compiler/llvm_codegen_c_abi_aarch64.c +++ b/src/compiler/llvm_codegen_c_abi_aarch64.c @@ -26,7 +26,7 @@ ABIArgInfo *aarch64_classify_argument_type(Type *type) return aarch64_coerce_illegal_vector(type); } - ByteSize size = type_size(type); + TypeSize size = type_size(type); if (!type_is_abi_aggregate(type)) { @@ -92,7 +92,7 @@ ABIArgInfo *aarch64_classify_return_type(Type *type, bool variadic) return aarch64_coerce_illegal_vector(type); } - ByteSize size = type_size(type); + TypeSize size = type_size(type); // Large vectors by mem. if (type->type_kind == TYPE_VECTOR && size > 16) diff --git a/src/compiler/llvm_codegen_c_abi_x64.c b/src/compiler/llvm_codegen_c_abi_x64.c index bf11bfc4c..e53790a29 100644 --- a/src/compiler/llvm_codegen_c_abi_x64.c +++ b/src/compiler/llvm_codegen_c_abi_x64.c @@ -70,7 +70,7 @@ static bool x64_type_is_illegal_vector(Type *type) { // Only check vectors. if (type->type_kind != TYPE_VECTOR) return false; - unsigned size = type_size(type); + ByteSize size = type_size(type); // Less than 64 bits or larger than the avx native size => not allowed. if (size <= 8 || size > x64_native_vector_size_for_avx()) return true; // If we pass i128 in mem, then check for that. @@ -112,7 +112,7 @@ ABIArgInfo *x64_indirect_result(Type *type, unsigned free_int_regs) // (if 'onstack' appears, change this code) if (!free_int_regs) { - unsigned size = type_size(type); + ByteSize size = type_size(type); if (align <= 8 && size <= 8) { return abi_arg_new_direct_coerce(abi_type_new_int_bits(size * 8)); @@ -377,7 +377,7 @@ Decl *x64_get_member_at_offset(Decl *decl, unsigned offset) Decl *last_match = NULL; VECEACH(members, i) { - if (members[i]->offset > (ArrayIndex)offset) break; + if (members[i]->offset > (MemberIndex)offset) break; last_match = members[i]; } assert(last_match); @@ -457,16 +457,16 @@ bool x64_bits_contain_no_user_data(Type *type, unsigned start, unsigned end) // If the bytes being queried are off the end of the type, there is no user // data hiding here. This handles analysis of builtins, vectors and other // types that don't contain interesting padding. - ByteSize size = type_size(type); + TypeSize size = type_size(type); if (size <= start) return true; if (type->type_kind == TYPE_ARRAY) { // Check each field to see if the field overlaps with the queried range. - ByteSize element_size = type_size(type->array.base); + TypeSize element_size = type_size(type->array.base); for (unsigned i = 0; i < type->array.len; i++) { // If the field is after the span we care about, then we're done.. - ByteSize offset = i * element_size; + TypeSize offset = i * element_size; if (offset >= end) break; unsigned element_start = offset < start ? start - offset : 0; if (!x64_bits_contain_no_user_data(type->array.base, element_start, end - offset)) return false; @@ -578,8 +578,8 @@ AbiType *x64_get_int_type_at_offset(Type *type, unsigned offset, Type *source_ty case TYPE_ARRAY: { Type *element = type->array.base; - ByteSize element_size = type_size(element); - ByteSize element_offset = (offset / element_size) * element_size; + TypeSize element_size = type_size(element); + TypeSize element_offset = (offset / element_size) * element_size; return x64_get_int_type_at_offset(element, offset - element_offset, source_type, source_offset); } case TYPE_VOID: @@ -648,7 +648,7 @@ static AbiType *x64_get_byte_vector_type(Type *type) static ABIArgInfo *x64_get_argument_pair_return(AbiType *low_type, AbiType *high_type) { - unsigned low_size = abi_type_size(low_type); + TypeSize low_size = abi_type_size(low_type); unsigned hi_start = aligned_offset(low_size, abi_type_abi_alignment(high_type)); assert(hi_start == 8 && "Expected aligned with C-style structs."); return abi_arg_new_direct_pair(low_type, high_type); diff --git a/src/compiler/llvm_codegen_c_abi_x86.c b/src/compiler/llvm_codegen_c_abi_x86.c index 169a71719..dd0a08c36 100644 --- a/src/compiler/llvm_codegen_c_abi_x86.c +++ b/src/compiler/llvm_codegen_c_abi_x86.c @@ -93,7 +93,7 @@ static ABIArgInfo *create_indirect_return_x86(Type *type, Regs *regs) static bool x86_should_return_type_in_reg(Type *type) { assert(type->canonical == type); - unsigned size = type_size(type); + ByteSize size = type_size(type); if (size > 8) return false; // Require power of two for everything except mcu. @@ -179,7 +179,7 @@ ABIArgInfo *x86_classify_return(CallABI call, Regs *regs, Type *type) // On Darwin, vectors may be returned in registers. if (platform_target.x86.is_darwin_vector_abi) { - unsigned size = type_size(type); + ByteSize size = type_size(type); if (size == 16) { // Special case, convert 128 bit vector to two 64 bit elements. @@ -320,13 +320,13 @@ static bool x86_try_use_free_regs(Regs *regs, Type *type) // 1. Floats are not passed in regs on soft floats. if (!platform_target.x86.use_soft_float && type_is_float(type)) return false; - unsigned size = type_size(type); + ByteSize size = type_size(type); // 2. If the type is empty, don't use a register. if (!size) return false; // 3. Calculate the number of registers. - unsigned size_in_regs = (size + 3) / 4; + ByteSize size_in_regs = (size + 3) / 4; // 4. The MCU psABI allows passing parameters in-reg even if there are // earlier parameters that are passed on the stack. Also, @@ -431,7 +431,7 @@ static inline ABIArgInfo *x86_classify_homogenous_aggregate(Regs *regs, Type *ty static inline ABIArgInfo *x86_classify_vector(Regs *regs, Type *type) { - unsigned size = type_size(type); + ByteSize size = type_size(type); // On Windows, vectors are passed directly if registers are available, or // indirectly if not. This avoids the need to align argument memory. Pass diff --git a/src/compiler/llvm_codegen_debug_info.c b/src/compiler/llvm_codegen_debug_info.c index 7f5eefafc..c0dce5d23 100644 --- a/src/compiler/llvm_codegen_debug_info.c +++ b/src/compiler/llvm_codegen_debug_info.c @@ -23,7 +23,7 @@ static inline LLVMMetadataRef llvm_get_debug_struct(GenContext *c, Type *type, c loc ? c->debug.file : NULL, loc ? loc->line : 0, type_size(type) * 8, - type_abi_alignment(type) * 8, + (uint32_t)(type_abi_alignment(type) * 8), flags, NULL, elements, element_count, c->debug.runtime_version, @@ -45,7 +45,7 @@ static inline LLVMMetadataRef llvm_get_debug_member(GenContext *c, Type *type, c loc ? c->debug.file : NULL, loc ? loc->line : 0, type_size(type) * 8, - type_abi_alignment(type) * 8, + (uint32_t)(type_abi_alignment(type) * 8), offset * 8, flags, llvm_get_debug_type_internal(c, type, scope)); } @@ -250,7 +250,7 @@ static LLVMMetadataRef llvm_debug_simple_type(GenContext *context, Type *type, i type->name, strlen(type->name), type->builtin.bitsize, - dwarf_code, 0); + (LLVMDWARFTypeEncoding)dwarf_code, 0); } @@ -293,7 +293,7 @@ static LLVMMetadataRef llvm_debug_enum_type(GenContext *c, Type *type, LLVMMetad LLVMMetadataRef debug_info = LLVMDIBuilderCreateEnumerator( c->debug.builder, enum_constant->name, TOKLEN(enum_constant->name_token), - val, + (int64_t)val, is_unsigned); vec_add(elements, debug_info); } diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index c838d8216..446c444e2 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -14,7 +14,7 @@ static inline void llvm_emit_pre_inc_dec(GenContext *c, BEValue *value, Expr *ex static inline void llvm_emit_inc_dec_change(GenContext *c, bool use_mod, BEValue *addr, BEValue *after, BEValue *before, Expr *expr, int diff); static void llvm_emit_post_unary_expr(GenContext *context, BEValue *be_value, Expr *expr); static inline void llvm_emit_subscript_addr_with_base(GenContext *c, BEValue *result, BEValue *parent, BEValue *index, SourceLocation *loc); -static void llvm_emit_initialize_designated(GenContext *c, BEValue *ref, uint64_t offset, DesignatorElement** current, DesignatorElement **last, Expr *expr, BEValue *emitted_value); +static void llvm_emit_initialize_designated(GenContext *c, BEValue *ref, AlignSize offset, DesignatorElement** current, DesignatorElement **last, Expr *expr, BEValue *emitted_value); static inline void llvm_emit_const_initialize_reference(GenContext *c, BEValue *ref, Expr *expr); LLVMValueRef llvm_emit_is_no_error_value(GenContext *c, BEValue *value) { @@ -85,13 +85,13 @@ static inline LLVMValueRef llvm_emit_lshr(GenContext *c, LLVMValueRef data, int assert(shift >= 0); if (shift == 0) return data; LLVMTypeRef type = LLVMTypeOf(data); - int bit_width = llvm_bitsize(c, type); + BitSize bit_width = llvm_bitsize(c, type); if (shift >= bit_width) return LLVMConstNull(type); if (LLVMIsAConstant(data)) { - return LLVMConstLShr(data, LLVMConstInt(type, shift, false)); + return LLVMConstLShr(data, LLVMConstInt(type, (unsigned)shift, false)); } - return LLVMBuildLShr(c->builder, data, LLVMConstInt(type, shift, false), ""); + return LLVMBuildLShr(c->builder, data, LLVMConstInt(type, (unsigned)shift, false), ""); } static inline LLVMValueRef llvm_emit_shl(GenContext *c, LLVMValueRef data, int shift) @@ -99,13 +99,13 @@ static inline LLVMValueRef llvm_emit_shl(GenContext *c, LLVMValueRef data, int s assert(shift >= 0); if (shift == 0) return data; LLVMTypeRef type = LLVMTypeOf(data); - int bit_width = llvm_bitsize(c, type); + BitSize bit_width = llvm_bitsize(c, type); if (shift >= bit_width) return LLVMConstNull(type); if (LLVMIsAConstant(data)) { - return LLVMConstShl(data, LLVMConstInt(type, shift, false)); + return LLVMConstShl(data, LLVMConstInt(type, (unsigned)shift, false)); } - return LLVMBuildShl(c->builder, data, LLVMConstInt(type, shift, false), ""); + return LLVMBuildShl(c->builder, data, LLVMConstInt(type, (unsigned)shift, false), ""); } void llvm_convert_vector_comparison(GenContext *c, BEValue *be_value, LLVMValueRef val, Type *vector_type) @@ -169,21 +169,21 @@ LLVMValueRef llvm_const_low_bitmask(LLVMTypeRef type, int type_bits, int low_bit { if (low_bits < 1) return LLVMConstNull(type); if (type_bits <= low_bits) return LLVMConstAllOnes(type); - return LLVMConstLShr(LLVMConstAllOnes(type), LLVMConstInt(type, type_bits - low_bits, 0)); + return LLVMConstLShr(LLVMConstAllOnes(type), LLVMConstInt(type, (unsigned long long)(type_bits - low_bits), 0)); } LLVMValueRef llvm_const_high_bitmask(LLVMTypeRef type, int type_bits, int high_bits) { if (high_bits < 1) return LLVMConstNull(type); if (type_bits <= high_bits) return LLVMConstAllOnes(type); - return LLVMConstNot(LLVMConstLShr(LLVMConstAllOnes(type), LLVMConstInt(type, high_bits, 0))); + return LLVMConstNot(LLVMConstLShr(LLVMConstAllOnes(type), LLVMConstInt(type, (unsigned long long)high_bits, 0))); } -LLVMValueRef llvm_mask_low_bits(GenContext *c, LLVMValueRef value, int low_bits) +LLVMValueRef llvm_mask_low_bits(GenContext *c, LLVMValueRef value, unsigned low_bits) { LLVMTypeRef type = LLVMTypeOf(value); if (low_bits < 1) return LLVMConstNull(type); - int type_bits = llvm_bitsize(c, type); + BitSize type_bits = llvm_bitsize(c, type); if (type_bits <= low_bits) return value; LLVMValueRef mask = LLVMConstLShr(LLVMConstAllOnes(type), LLVMConstInt(type, type_bits - low_bits, 0)); if (LLVMIsConstant(value)) @@ -193,14 +193,14 @@ LLVMValueRef llvm_mask_low_bits(GenContext *c, LLVMValueRef value, int low_bits) return LLVMBuildAnd(c->builder, mask, value, ""); } -LLVMTypeRef llvm_const_padding_type(GenContext *c, ByteSize size) +LLVMTypeRef llvm_const_padding_type(GenContext *c, AlignSize size) { assert(size > 0); if (size == 1) return llvm_get_type(c, type_char); - return LLVMArrayType(llvm_get_type(c, type_char), size); + return LLVMArrayType(llvm_get_type(c, type_char), (unsigned)size); } -LLVMValueRef llvm_emit_const_padding(GenContext *c, ByteSize size) +LLVMValueRef llvm_emit_const_padding(GenContext *c, AlignSize size) { return LLVMGetUndef(llvm_const_padding_type(c, size)); } @@ -234,6 +234,7 @@ void llvm_enter_struct_for_coerce(GenContext *c, LLVMValueRef *struct_ptr, LLVMT { while (1) { + if (LLVMGetTypeKind(*type) != LLVMStructTypeKind) return; if (!LLVMCountStructElementTypes(*type)) return; LLVMTypeRef first_element = LLVMStructGetTypeAtIndex(*type, 0); ByteSize first_element_size = llvm_store_size(c, first_element); @@ -570,7 +571,7 @@ static inline void gencontext_emit_subscript(GenContext *c, BEValue *value, Expr } -static int find_member_index(Decl *parent, Decl *member) +static MemberIndex find_member_index(Decl *parent, Decl *member) { VECEACH(parent->strukt.members, i) { @@ -594,7 +595,7 @@ static void gencontext_emit_member_addr(GenContext *c, BEValue *value, Decl *par Decl *found = NULL; do { - int index = find_member_index(parent, member); + MemberIndex index = find_member_index(parent, member); assert(index > -1); found = parent->strukt.members[index]; switch (parent->type->canonical->type_kind) @@ -604,7 +605,7 @@ static void gencontext_emit_member_addr(GenContext *c, BEValue *value, Decl *par llvm_value_set_address_align(value, llvm_emit_bitcast(c, value->value, type_get_ptr(found->type)), found->type, value->alignment); break; case TYPE_STRUCT: - llvm_value_struct_gep(c, value, value, index); + llvm_value_struct_gep(c, value, value, (unsigned)index); break; default: UNREACHABLE @@ -619,7 +620,7 @@ static void llvm_emit_bitstruct_member(GenContext *c, BEValue *value, Decl *pare Decl *found = NULL; do { - int index = find_member_index(parent, member); + MemberIndex index = find_member_index(parent, member); assert(index > -1); found = parent->strukt.members[index]; switch (parent->type->canonical->type_kind) @@ -632,7 +633,7 @@ static void llvm_emit_bitstruct_member(GenContext *c, BEValue *value, Decl *pare value->alignment); break; case TYPE_STRUCT: - llvm_value_struct_gep(c, value, value, index); + llvm_value_struct_gep(c, value, value, (unsigned)index); break; case TYPE_BITSTRUCT: break; @@ -667,9 +668,10 @@ static inline void llvm_extract_bool_bit_from_array(GenContext *c, BEValue *be_v AlignSize alignment; LLVMValueRef array_ptr = be_value->value; LLVMTypeRef array_type = llvm_get_type(c, type_char); - int start_bit = member->var.start_bit; + unsigned start_bit = member->var.start_bit; // Grab the byte - LLVMValueRef byte_ptr = llvm_emit_array_gep_raw(c, array_ptr, llvm_get_type(c, be_value->type), start_bit / 8, be_value->alignment, &alignment); + LLVMValueRef byte_ptr = llvm_emit_array_gep_raw(c, array_ptr, llvm_get_type(c, be_value->type), + start_bit / 8, be_value->alignment, &alignment); LLVMValueRef element = llvm_emit_load_aligned(c, array_type, byte_ptr, alignment, ""); // Shift the bit to the zero position. element = llvm_emit_lshr(c, element, start_bit % 8); @@ -679,10 +681,10 @@ static inline void llvm_extract_bool_bit_from_array(GenContext *c, BEValue *be_v llvm_value_set_bool(be_value, element); } -static inline LLVMValueRef llvm_bswap_non_integral(GenContext *c, LLVMValueRef value, int bitsize) +static inline LLVMValueRef llvm_bswap_non_integral(GenContext *c, LLVMValueRef value, unsigned bitsize) { if (bitsize <= 8) return value; - LLVMValueRef shifted = llvm_emit_shl(c, value, llvm_bitsize(c, LLVMTypeOf(value)) - bitsize); + LLVMValueRef shifted = llvm_emit_shl(c, value, (int)llvm_bitsize(c, LLVMTypeOf(value)) - (int)bitsize); return llvm_emit_bswap(c, shifted); } @@ -697,8 +699,8 @@ static inline void llvm_extract_bitvalue_from_array(GenContext *c, BEValue *be_v bool big_endian = platform_target.big_endian; if (parent_decl->bitstruct.big_endian) big_endian = true; if (parent_decl->bitstruct.little_endian) big_endian = false; - int start = member->var.start_bit; - int end = member->var.end_bit; + unsigned start = member->var.start_bit; + unsigned end = member->var.end_bit; LLVMValueRef array_ptr = be_value->value; LLVMTypeRef array_type = llvm_get_type(c, type_char); LLVMValueRef result = NULL; @@ -706,13 +708,14 @@ static inline void llvm_extract_bitvalue_from_array(GenContext *c, BEValue *be_v int end_byte = end / 8; Type *member_type = type_lowering(member->type); LLVMTypeRef llvm_member_type = llvm_get_type(c, member_type); - int bitsize = type_size(member_type) * 8; + TypeSize bitsize = type_size(member_type) * 8; LLVMValueRef res = NULL; - unsigned offset = start % 8; + int offset = start % 8; for (int i = start_byte; i <= end_byte; i++) { AlignSize alignment; - LLVMValueRef byte_ptr = llvm_emit_array_gep_raw(c, array_ptr, llvm_get_type(c, be_value->type), i, be_value->alignment, &alignment); + LLVMValueRef byte_ptr = llvm_emit_array_gep_raw(c, array_ptr, llvm_get_type(c, be_value->type), + (unsigned)i, be_value->alignment, &alignment); LLVMValueRef element = llvm_emit_load_aligned(c, array_type, byte_ptr, alignment, ""); element = llvm_zext_trunc(c, element, llvm_member_type); int current_offset = 8 * (i - start_byte) - offset; @@ -737,7 +740,7 @@ static inline void llvm_extract_bitvalue_from_array(GenContext *c, BEValue *be_v } if (type_is_signed(member_type)) { - int top_bits_to_clear = bitsize - end + start - 1; + TypeSize top_bits_to_clear = bitsize - end + start - 1; if (top_bits_to_clear) { LLVMValueRef shift = LLVMConstInt(llvm_member_type, top_bits_to_clear, false); @@ -767,21 +770,21 @@ static inline void llvm_extract_bitvalue(GenContext *c, BEValue *be_value, Expr LLVMValueRef value = llvm_value_rvalue_store(c, be_value); if (bswap) value = llvm_emit_bswap(c, value); LLVMTypeRef container_type = LLVMTypeOf(value); - ByteSize container_size = type_size(be_value->type); - ByteSize container_bit_size = container_size * 8; - int start = member->var.start_bit; - int end = member->var.end_bit; + BitSize container_size = type_size(be_value->type); + BitSize container_bit_size = container_size * 8; + unsigned start = (unsigned)member->var.start_bit; + unsigned end = (unsigned)member->var.end_bit; Type *member_type = type_lowering(member->type); ByteSize member_type_size = type_size(member_type); if (type_is_signed(member_type)) { // Shift all the way left, so top bit is to the top. - int left_shift = container_bit_size - end - 1; + uint64_t left_shift = container_bit_size - end - 1; if (left_shift) { value = LLVMBuildShl(c->builder, value, LLVMConstInt(container_type, left_shift, 0), ""); } - int right_shift = left_shift + start; + uint64_t right_shift = left_shift + start; if (right_shift) { value = LLVMBuildAShr(c->builder, value, LLVMConstInt(container_type, right_shift, 0), ""); @@ -802,7 +805,7 @@ static inline void llvm_extract_bitvalue(GenContext *c, BEValue *be_value, Expr { value = LLVMBuildLShr(c->builder, value, LLVMConstInt(container_type, start, 0), ""); } - int bits_needed = end - start + 1; + TypeSize bits_needed = end - start + 1; value = llvm_mask_low_bits(c, value, bits_needed); value = llvm_zext_trunc(c, value, llvm_get_type(c, member_type)); } @@ -818,8 +821,8 @@ static inline void llvm_emit_bitassign_array(GenContext *c, BEValue *result, BEV assert(parent.type->type_kind == TYPE_ARRAY); LLVMTypeRef array_type = llvm_get_type(c, parent.type); - int start_bit = member->var.start_bit; - int end_bit = member->var.end_bit; + unsigned start_bit = member->var.start_bit; + unsigned end_bit = member->var.end_bit; Type *member_type = type_flatten(member->type); LLVMValueRef value = llvm_value_rvalue_store(c, result); @@ -855,7 +858,8 @@ static inline void llvm_emit_bitassign_array(GenContext *c, BEValue *result, BEV for (int i = start_byte; i <= end_byte; i++) { AlignSize alignment; - LLVMValueRef byte_ptr = llvm_emit_array_gep_raw(c, array_ptr, array_type, i, parent.alignment, &alignment); + LLVMValueRef byte_ptr = llvm_emit_array_gep_raw(c, array_ptr, array_type, + (unsigned)i, parent.alignment, &alignment); if (i == start_byte && start_mod != 0) { int skipped_bits = start_mod; @@ -873,7 +877,7 @@ static inline void llvm_emit_bitassign_array(GenContext *c, BEValue *result, BEV if (i == end_byte && end_mod != 7) { res = LLVMBuildAnd(c->builder, res, llvm_const_low_bitmask(c->byte_type, 8, end_mod + 1), ""); - mask = LLVMConstOr(mask, llvm_const_high_bitmask(c->byte_type, 8, 7 - end_bit)); + mask = LLVMConstOr(mask, llvm_const_high_bitmask(c->byte_type, 8, 7 - (int)end_bit)); } // Load the current value. LLVMValueRef current = llvm_emit_load_aligned(c, c->byte_type, byte_ptr, alignment, ""); @@ -951,11 +955,11 @@ static inline void llvm_emit_bitassign_expr(GenContext *c, BEValue *be_value, Ex // We now need to create a mask, a very naive algorithm: LLVMValueRef mask = LLVMConstAllOnes(struct_type); - int bits = type_size(parent.type) * 8; - int start_bit = member->var.start_bit; - int end_bit = member->var.end_bit; + TypeSize bits = type_size(parent.type) * 8; + int start_bit = (int)member->var.start_bit; + int end_bit = (int)member->var.end_bit; // Let's say we want to create 00111000 => start: 3 end: 5 - int left_shift = bits - end_bit - 1; + int left_shift = (int)bits - end_bit - 1; mask = llvm_emit_shl(c, mask, left_shift); // => shift 2: 11111100 mask = llvm_emit_lshr(c, mask, left_shift + start_bit); @@ -1329,18 +1333,18 @@ static LLVMValueRef llvm_recursive_set_value(GenContext *c, DesignatorElement ** { BEValue res; llvm_emit_expr(c, &res, value); - unsigned index = current_element->index; + unsigned index = (unsigned)current_element->index; LLVMValueRef val = llvm_value_rvalue_store(c, &res); switch (current_element->kind) { case DESIGNATOR_FIELD: return LLVMConstInsertValue(parent, val, &index, 1); case DESIGNATOR_ARRAY: - return LLVMConstInsertElement(parent, val, llvm_const_int(c, type_isize, current_element->index)); + return LLVMConstInsertElement(parent, val, llvm_const_int(c, type_isize, (unsigned)current_element->index)); case DESIGNATOR_RANGE: - for (int64_t i = current_element->index; i <= current_element->index_end; i++) + for (MemberIndex i = current_element->index; i <= current_element->index_end; i++) { - parent = LLVMConstInsertElement(parent, val, llvm_const_int(c, type_isize, i)); + parent = LLVMConstInsertElement(parent, val, llvm_const_int(c, type_isize, (uint64_t)i)); } return parent; } @@ -1351,22 +1355,22 @@ static LLVMValueRef llvm_recursive_set_value(GenContext *c, DesignatorElement ** { case DESIGNATOR_FIELD: { - unsigned index = current_element->index; + unsigned index = (unsigned)current_element->index; current_val = LLVMConstExtractValue(parent, &index, 1); current_val = llvm_recursive_set_value(c, current_element_ptr + 1, current_val, last_element_ptr, value); return LLVMConstInsertValue(parent, current_val, &index, 1); } case DESIGNATOR_ARRAY: { - LLVMValueRef index = llvm_const_int(c, type_isize, current_element->index); + LLVMValueRef index = llvm_const_int(c, type_isize, (uint64_t)current_element->index); current_val = LLVMConstExtractElement(parent, index); current_val = llvm_recursive_set_value(c, current_element_ptr + 1, current_val, last_element_ptr, value); return LLVMConstInsertElement(parent, current_val, index); } case DESIGNATOR_RANGE: - for (int64_t i = current_element->index; i <= current_element->index_end; i++) + for (MemberIndex i = current_element->index; i <= current_element->index_end; i++) { - LLVMValueRef index = llvm_const_int(c, type_isize, i); + LLVMValueRef index = llvm_const_int(c, type_isize, (uint64_t)i); current_val = LLVMConstExtractElement(parent, index); current_val = llvm_recursive_set_value(c, current_element_ptr + 1, current_val, last_element_ptr, value); parent = LLVMConstInsertElement(parent, current_val, index); @@ -1395,7 +1399,7 @@ void llvm_emit_initialize_reference_temporary_const(GenContext *c, BEValue *ref, LLVMSetLinkage(global_copy, LLVMPrivateLinkage); // Set a nice alignment - ByteSize alignment = type_alloca_alignment(expr->type); + AlignSize alignment = type_alloca_alignment(expr->type); llvm_set_alignment(global_copy, alignment); // Set the value and make it constant @@ -1433,13 +1437,13 @@ static void llvm_emit_inititialize_reference_const(GenContext *c, BEValue *ref, LLVMValueRef array_ref = ref->value; Type *array_type = const_init->type; Type *element_type = array_type->array.base; - ArrayIndex size = array_type->array.len; + MemberIndex size = (MemberIndex)array_type->array.len; LLVMTypeRef array_type_llvm = llvm_get_type(c, array_type); assert(size <= UINT32_MAX); - for (ArrayIndex i = 0; i < size; i++) + for (MemberIndex i = 0; i < size; i++) { AlignSize alignment; - LLVMValueRef array_pointer = llvm_emit_array_gep_raw(c, array_ref, array_type_llvm, i, ref->alignment, &alignment); + LLVMValueRef array_pointer = llvm_emit_array_gep_raw(c, array_ref, array_type_llvm, (unsigned)i, ref->alignment, &alignment); BEValue value; llvm_value_set_address_align(&value, array_pointer, element_type, alignment); llvm_emit_inititialize_reference_const(c, &value, const_init->init_array_full[i]); @@ -1454,15 +1458,15 @@ static void llvm_emit_inititialize_reference_const(GenContext *c, BEValue *ref, Type *element_type = array_type->array.base; LLVMTypeRef array_type_llvm = llvm_get_type(c, array_type); ConstInitializer **elements = const_init->init_array.elements; - ArrayIndex current_index = 0; + MemberIndex current_index = 0; LLVMValueRef *parts = NULL; VECEACH(elements, i) { ConstInitializer *element = elements[i]; assert(element->kind == CONST_INIT_ARRAY_VALUE); - ArrayIndex element_index = element->init_array_value.index; + MemberIndex element_index = element->init_array_value.index; AlignSize alignment; - LLVMValueRef array_pointer = llvm_emit_array_gep_raw(c, array_ref, array_type_llvm, element_index, ref->alignment, &alignment); + LLVMValueRef array_pointer = llvm_emit_array_gep_raw(c, array_ref, array_type_llvm, (unsigned)element_index, ref->alignment, &alignment); BEValue value; llvm_value_set_address_align(&value, array_pointer, element_type, alignment); llvm_emit_inititialize_reference_const(c, &value, element->init_array_value.element); @@ -1485,11 +1489,11 @@ static void llvm_emit_inititialize_reference_const(GenContext *c, BEValue *ref, { Decl *decl = const_init->type->decl; Decl **members = decl->strukt.members; - MemberIndex count = vec_size(members); + MemberIndex count = (MemberIndex)vec_size(members); for (MemberIndex i = 0; i < count; i++) { BEValue value; - llvm_value_struct_gep(c, &value, ref, i); + llvm_value_struct_gep(c, &value, ref, (unsigned)i); llvm_emit_inititialize_reference_const(c, &value, const_init->init_struct[i]); } return; @@ -1575,7 +1579,7 @@ static inline void llvm_emit_initialize_reference_list(GenContext *c, BEValue *r } } -static void llvm_emit_initialize_designated_const_range(GenContext *c, BEValue *ref, uint64_t offset, DesignatorElement** current, DesignatorElement **last, Expr *expr, BEValue *emitted_value) +static void llvm_emit_initialize_designated_const_range(GenContext *c, BEValue *ref, AlignSize offset, DesignatorElement** current, DesignatorElement **last, Expr *expr, BEValue *emitted_value) { DesignatorElement *curr = current[0]; llvm_value_addr(c, ref); @@ -1589,17 +1593,17 @@ static void llvm_emit_initialize_designated_const_range(GenContext *c, BEValue * emitted_value = &emitted_local; } LLVMTypeRef ref_type = llvm_get_type(c, ref->type); - for (unsigned i = curr->index; i <= curr->index_end; i++) + for (MemberIndex i = curr->index; i <= curr->index_end; i++) { BEValue new_ref; AlignSize alignment; - LLVMValueRef ptr = llvm_emit_array_gep_raw(c, ref->value, ref_type, i, ref->alignment, &alignment); + LLVMValueRef ptr = llvm_emit_array_gep_raw(c, ref->value, ref_type, (unsigned)i, ref->alignment, &alignment); llvm_value_set_address_align(&new_ref, ptr, type_get_indexed_type(ref->type), alignment); llvm_emit_initialize_designated(c, &new_ref, offset, current + 1, last, expr, emitted_value); } } -static void llvm_emit_initialize_designated(GenContext *c, BEValue *ref, uint64_t offset, DesignatorElement** current, +static void llvm_emit_initialize_designated(GenContext *c, BEValue *ref, AlignSize offset, DesignatorElement** current, DesignatorElement **last, Expr *expr, BEValue *emitted_value) { BEValue value; @@ -1640,7 +1644,7 @@ static void llvm_emit_initialize_designated(GenContext *c, BEValue *ref, uint64_ } else { - llvm_value_struct_gep(c, &value, ref, curr->index); + llvm_value_struct_gep(c, &value, ref, (unsigned)curr->index); } llvm_emit_initialize_designated(c, &value, offset, current + 1, last, expr, emitted_value); break; @@ -1648,9 +1652,9 @@ static void llvm_emit_initialize_designated(GenContext *c, BEValue *ref, uint64_ case DESIGNATOR_ARRAY: { Type *type = ref->type->array.base; - offset += curr->index * type_size(type); + offset += (unsigned)curr->index * type_size(type); AlignSize alignment; - LLVMValueRef ptr = llvm_emit_array_gep_raw(c, ref->value, llvm_get_type(c, ref->type), curr->index, ref->alignment, &alignment); + LLVMValueRef ptr = llvm_emit_array_gep_raw(c, ref->value, llvm_get_type(c, ref->type), (unsigned)curr->index, ref->alignment, &alignment); llvm_value_set_address_align(&value, ptr, type, alignment); llvm_emit_initialize_designated(c, &value, offset, current + 1, last, expr, emitted_value); break; @@ -1693,7 +1697,7 @@ LLVMValueRef llvm_emit_const_bitstruct_array(GenContext *c, ConstInitializer *in if (decl->bitstruct.little_endian) big_endian = false; LLVMValueRef data = LLVMConstNull(llvm_get_type(c, base_type)); Decl **members = decl->strukt.members; - MemberIndex count = vec_size(members); + MemberIndex count = (MemberIndex)vec_size(members); for (MemberIndex i = 0; i < count; i++) { Decl *member = members[i]; @@ -1713,7 +1717,7 @@ LLVMValueRef llvm_emit_const_bitstruct_array(GenContext *c, ConstInitializer *in if (!expr->const_expr.b) continue; LLVMValueRef bit = llvm_emit_shl(c, LLVMConstInt(c->byte_type, 1, 0), start_bit % 8); - int byte = start_bit / 8; + unsigned byte = start_bit / 8; LLVMValueRef current_value = llvm_emit_extract_value(c, data, byte); data = llvm_emit_insert_value(c, data, LLVMConstOr(current_value, bit), byte); continue; @@ -1742,15 +1746,15 @@ LLVMValueRef llvm_emit_const_bitstruct_array(GenContext *c, ConstInitializer *in } else { - to_or = llvm_emit_lshr(c, value, j * 8 - start_bit); + to_or = llvm_emit_lshr(c, value, j * 8 - (int)start_bit); } if (j == end_byte) { to_or = llvm_mask_low_bits(c, to_or, end_bit % 8 + 1); } if (member_type_bitsize > 8) to_or = LLVMConstTrunc(to_or, c->byte_type); - LLVMValueRef current_value = llvm_emit_extract_value(c, data, j); - data = llvm_emit_insert_value(c, data, LLVMConstOr(to_or, current_value), j); + LLVMValueRef current_value = llvm_emit_extract_value(c, data, (unsigned)j); + data = llvm_emit_insert_value(c, data, LLVMConstOr(to_or, current_value), (unsigned)j); } } return data; @@ -1769,13 +1773,12 @@ LLVMValueRef llvm_emit_const_bitstruct(GenContext *c, ConstInitializer *initiali LLVMTypeRef llvm_base_type = llvm_get_type(c, base_type); LLVMValueRef result = LLVMConstNull(llvm_base_type); Decl **members = decl->strukt.members; - MemberIndex count = vec_size(members); - ByteSize base_type_size = type_size(base_type); - ByteSize base_type_bitsize = base_type_size * 8; + MemberIndex count = (MemberIndex)vec_size(members); + TypeSize base_type_size = type_size(base_type); + TypeSize base_type_bitsize = base_type_size * 8; for (MemberIndex i = 0; i < count; i++) { Decl *member = members[i]; - ByteSize member_size = type_size(member->type); unsigned start_bit = member->var.start_bit; unsigned end_bit = member->var.end_bit; unsigned bit_size = end_bit - start_bit + 1; @@ -1891,7 +1894,7 @@ static inline void llvm_emit_inc_dec_change(GenContext *c, bool use_mod, BEValue case TYPE_POINTER: { // Use byte here, we don't need a big offset. - LLVMValueRef add = LLVMConstInt(diff < 0 ? llvm_get_type(c, type_ichar) : llvm_get_type(c, type_char), diff, diff < 0); + LLVMValueRef add = LLVMConstInt(diff < 0 ? llvm_get_type(c, type_ichar) : llvm_get_type(c, type_char), (unsigned long long)diff, diff < 0); after_value = llvm_emit_pointer_gep_raw(c, llvm_get_pointee_type(c, type), value.value, add); break; } @@ -1926,9 +1929,9 @@ static inline void llvm_emit_inc_dec_change(GenContext *c, bool use_mod, BEValue { diff_value = LLVMConstReal(llvm_get_type(c, element), diff); } - ArrayIndex width = type->vector.len; + ArraySize width = type->vector.len; LLVMValueRef val = LLVMGetUndef(llvm_get_type(c, type)); - for (ArrayIndex i = 0; i < width; i++) + for (ArraySize i = 0; i < width; i++) { val = LLVMConstInsertElement(val, diff_value, llvm_const_int(c, type_usize, i)); } @@ -2370,8 +2373,10 @@ static void llvm_emit_slice_assign(GenContext *c, BEValue *be_value, Expr *expr) assert(type_is_integer(start.type) && type_is_integer(end.type)); bool signed_start = type_is_signed(start.type); bool signed_end = type_is_signed(end.type); - uint64_t start_val = (uint64_t)(signed_start ? LLVMConstIntGetSExtValue(start.value) : LLVMConstIntGetZExtValue(start.value)); - uint64_t end_val = (uint64_t)(signed_end ? LLVMConstIntGetSExtValue(end.value) : LLVMConstIntGetZExtValue(end.value)); + uint64_t start_val = signed_start ? (uint64_t)LLVMConstIntGetSExtValue(start.value) + : (uint64_t)LLVMConstIntGetZExtValue(start.value); + uint64_t end_val = signed_end ? (uint64_t)LLVMConstIntGetSExtValue(end.value) + : (uint64_t)LLVMConstIntGetZExtValue(end.value); assert(start_val <= INT64_MAX); assert(end_val <= INT64_MAX); if (start_val > end_val) return; @@ -3622,7 +3627,7 @@ static LLVMValueRef llvm_emit_real(LLVMTypeRef type, Float f) #if LONG_DOUBLE global_context.scratch_buffer_len = sprintf(global_context.scratch_buffer, "%La", f.f); #else - global_context.scratch_buffer_len = sprintf(global_context.scratch_buffer, "%a", f.f); + global_context.scratch_buffer_len = (uint32_t)sprintf(global_context.scratch_buffer, "%a", f.f); #endif return LLVMConstRealOfStringAndSize(type, global_context.scratch_buffer, global_context.scratch_buffer_len); } @@ -3748,7 +3753,7 @@ static void llvm_expand_array_to_args(GenContext *c, Type *param_type, LLVMValue for (ByteSize i = 0; i < param_type->array.len; i++) { AlignSize load_align; - LLVMValueRef element_ptr = llvm_emit_array_gep_raw(c, expand_ptr, array_type, i, alignment, &load_align); + LLVMValueRef element_ptr = llvm_emit_array_gep_raw(c, expand_ptr, array_type, (unsigned)i, alignment, &load_align); llvm_expand_type_to_args(c, param_type->array.base, element_ptr, values, load_align); } } @@ -3827,7 +3832,7 @@ void llvm_emit_struct_member_ref(GenContext *c, BEValue *struct_ref, BEValue *me LLVMValueRef llvm_emit_struct_gep_raw(GenContext *context, LLVMValueRef ptr, LLVMTypeRef struct_type, unsigned index, unsigned struct_alignment, AlignSize *alignment) { - *alignment = type_min_alignment(LLVMOffsetOfElement(context->target_data, struct_type, index), struct_alignment); + *alignment = type_min_alignment((AlignSize)LLVMOffsetOfElement(context->target_data, struct_type, index), struct_alignment); if (LLVMIsConstant(ptr)) { LLVMValueRef idx[2] = { llvm_get_zero(context, type_int), llvm_const_int(context, type_int, index) }; @@ -3920,9 +3925,9 @@ static void llvm_emit_any_pointer(GenContext *c, BEValue *any, BEValue *pointer) void llvm_value_struct_gep(GenContext *c, BEValue *element, BEValue *struct_pointer, unsigned index) { llvm_value_fold_failable(c, struct_pointer); - ArrayIndex actual_index = -1; + MemberIndex actual_index = -1; Decl *member; - for (ArrayIndex i = 0; i <= index; i++) + for (MemberIndex i = 0; i <= index; i++) { member = struct_pointer->type->decl->strukt.members[i]; if (member->padding) @@ -3935,7 +3940,7 @@ void llvm_value_struct_gep(GenContext *c, BEValue *element, BEValue *struct_poin LLVMValueRef ref = llvm_emit_struct_gep_raw(c, struct_pointer->value, llvm_get_type(c, struct_pointer->type), - actual_index, + (unsigned)actual_index, struct_pointer->alignment, &alignment); llvm_value_set_address(element, ref, member->type); @@ -4380,9 +4385,9 @@ void llvm_emit_call_expr(GenContext *c, BEValue *result_value, Expr *expr) case ABI_ARG_INDIRECT: if (info->attributes.by_val) { - llvm_attribute_add_call_type(c, call_value, attribute_byval, i + 1, llvm_get_type(c, info->indirect.type)); + llvm_attribute_add_call_type(c, call_value, attribute_byval, (int)i + 1, llvm_get_type(c, info->indirect.type)); } - llvm_attribute_add_call(c, call_value, attribute_align, i + 1, info->indirect.alignment); + llvm_attribute_add_call(c, call_value, attribute_align, (int)i + 1, info->indirect.alignment); break; default: break; @@ -4827,9 +4832,9 @@ static inline void llvm_emit_failable(GenContext *c, BEValue *be_value, Expr *ex llvm_value_set(be_value, LLVMGetUndef(llvm_get_type(c, type)), type); } -static inline LLVMValueRef llvm_update_vector(GenContext *c, LLVMValueRef vector, LLVMValueRef value, ArrayIndex index, bool *is_const) +static inline LLVMValueRef llvm_update_vector(GenContext *c, LLVMValueRef vector, LLVMValueRef value, MemberIndex index, bool *is_const) { - LLVMValueRef index_value = llvm_const_int(c, type_usize, index); + LLVMValueRef index_value = llvm_const_int(c, type_usize, (uint64_t)index); if (*is_const && LLVMIsConstant(value)) { return LLVMConstInsertElement(vector, value, index_value); @@ -4863,7 +4868,7 @@ static inline void llvm_emit_vector_initializer_list(GenContext *c, BEValue *val Expr *element = elements[i]; llvm_emit_expr(c, &val, element); llvm_value_rvalue(c, &val); - vec_value = llvm_update_vector(c, vec_value, val.value, i, &is_const); + vec_value = llvm_update_vector(c, vec_value, val.value, (MemberIndex)i, &is_const); } } else @@ -4886,7 +4891,7 @@ static inline void llvm_emit_vector_initializer_list(GenContext *c, BEValue *val break; } case DESIGNATOR_RANGE: - for (ArrayIndex idx = element->index; idx <= element->index_end; idx++) + for (MemberIndex idx = element->index; idx <= element->index_end; idx++) { vec_value = llvm_update_vector(c, vec_value, val.value, idx, &is_const); } diff --git a/src/compiler/llvm_codegen_internal.h b/src/compiler/llvm_codegen_internal.h index 39c98cec2..3882d957f 100644 --- a/src/compiler/llvm_codegen_internal.h +++ b/src/compiler/llvm_codegen_internal.h @@ -201,7 +201,7 @@ void llvm_value_rvalue(GenContext *context, BEValue *value); void llvm_value_set_bool(BEValue *value, LLVMValueRef llvm_value); void llvm_value_set(BEValue *value, LLVMValueRef llvm_value, Type *type); void llvm_value_set_int(GenContext *c, BEValue *value, Type *type, uint64_t i); -void llvm_value_set_address_align(BEValue *value, LLVMValueRef llvm_value, Type *type, unsigned alignment); +void llvm_value_set_address_align(BEValue *value, LLVMValueRef llvm_value, Type *type, AlignSize alignment); void llvm_value_set_address(BEValue *value, LLVMValueRef llvm_value, Type *type); void llvm_value_set_decl_address(BEValue *value, Decl *decl); void llvm_value_fold_failable(GenContext *c, BEValue *value); @@ -210,8 +210,8 @@ void llvm_value_struct_gep(GenContext *c, BEValue *element, BEValue *struct_poin LLVMValueRef llvm_value_rvalue_store(GenContext *c, BEValue *value); LLVMTypeRef llvm_abi_type(GenContext *c, AbiType *type); -unsigned llvm_abi_size(GenContext *c, LLVMTypeRef type); -unsigned llvm_bitsize(GenContext *c, LLVMTypeRef type); +TypeSize llvm_abi_size(GenContext *c, LLVMTypeRef type); +BitSize llvm_bitsize(GenContext *c, LLVMTypeRef type); AlignSize llvm_abi_alignment(GenContext *c, LLVMTypeRef type); void llvm_attribute_add_range(GenContext *c, LLVMValueRef value_to_add_attribute_to, unsigned attribute_id, int index_start, int index_end); void llvm_attribute_add(GenContext *c, LLVMValueRef value_to_add_attribute_to, unsigned attribute_id, int index); @@ -222,8 +222,8 @@ void llvm_attribute_add_type(GenContext *c, LLVMValueRef value_to_add_attribute_ void llvm_attribute_add_int(GenContext *c, LLVMValueRef value_to_add_attribute_to, unsigned attribute_id, uint64_t val, int index); LLVMBasicBlockRef llvm_basic_block_new(GenContext *c, const char *name); static inline LLVMValueRef llvm_const_int(GenContext *c, Type *type, uint64_t val); -LLVMValueRef llvm_emit_const_padding(GenContext *c, ByteSize size); -LLVMTypeRef llvm_const_padding_type(GenContext *c, ByteSize size); +LLVMValueRef llvm_emit_const_padding(GenContext *c, AlignSize size); +LLVMTypeRef llvm_const_padding_type(GenContext *c, TypeSize size); LLVMValueRef llvm_emit_alloca(GenContext *c, LLVMTypeRef type, unsigned alignment, const char *name); LLVMValueRef llvm_emit_alloca_aligned(GenContext *c, Type *type, const char *name); void llvm_emit_and_set_decl_alloca(GenContext *c, Decl *decl); @@ -263,7 +263,7 @@ LLVMValueRef llvm_emit_load_aligned(GenContext *c, LLVMTypeRef type, LLVMValueRe void llvm_emit_local_var_alloca(GenContext *c, Decl *decl); LLVMValueRef llvm_emit_local_decl(GenContext *c, Decl *decl); LLVMValueRef llvm_emit_aggregate_value(GenContext *c, Type *type, ...); -LLVMValueRef llvm_emit_memclear_size_align(GenContext *c, LLVMValueRef ref, uint64_t size, unsigned align, bool bitcast); +LLVMValueRef llvm_emit_memclear_size_align(GenContext *c, LLVMValueRef ref, uint64_t size, AlignSize align, bool bitcast); void llvm_emit_memclear(GenContext *c, BEValue *ref); void llvm_emit_memcpy(GenContext *c, LLVMValueRef dest, unsigned dest_align, LLVMValueRef source, unsigned src_align, uint64_t len); void llvm_emit_memcpy_to_decl(GenContext *c, Decl *decl, LLVMValueRef source, unsigned source_alignment); @@ -305,7 +305,7 @@ LLVMMetadataRef llvm_debug_current_scope(GenContext *context); bool llvm_emit_check_block_branch(GenContext *context); -unsigned llvm_store_size(GenContext *c, LLVMTypeRef type); +TypeSize llvm_store_size(GenContext *c, LLVMTypeRef type); LLVMValueRef llvm_store_bevalue(GenContext *c, BEValue *destination, BEValue *value); void llvm_store_bevalue_raw(GenContext *c, BEValue *destination, LLVMValueRef raw_value); void llvm_store_bevalue_dest_aligned(GenContext *c, LLVMValueRef destination, BEValue *value); @@ -439,7 +439,7 @@ static inline LLVMValueRef llvm_const_int(GenContext *c, Type *type, uint64_t va static inline void llvm_set_alignment(LLVMValueRef alloca, AlignSize alignment) { assert(alignment > 0); - LLVMSetAlignment(alloca, alignment); + LLVMSetAlignment(alloca, (unsigned)alignment); } void llvm_set_error_exit(GenContext *c, LLVMBasicBlockRef block); void llvm_set_error_exit_and_value(GenContext *c, LLVMBasicBlockRef block, LLVMValueRef value); diff --git a/src/compiler/llvm_codegen_module.c b/src/compiler/llvm_codegen_module.c index 2a7c77d38..4176da2d9 100644 --- a/src/compiler/llvm_codegen_module.c +++ b/src/compiler/llvm_codegen_module.c @@ -34,13 +34,13 @@ void gencontext_begin_module(GenContext *c) if (active_target.pic == PIC_BIG || active_target.pic == PIC_SMALL) { static const char *pic_level = "PIC Level"; - LLVMMetadataRef setting = LLVMValueAsMetadata(LLVMConstInt(options_type, active_target.pic, false)); + LLVMMetadataRef setting = LLVMValueAsMetadata(LLVMConstInt(options_type, (unsigned)active_target.pic, false)); LLVMAddModuleFlag(c->module, LLVMModuleFlagBehaviorOverride, pic_level, strlen(pic_level), setting); } if (active_target.pie == PIE_BIG || active_target.pie == PIE_SMALL) { static const char *pie_level = "PIE Level"; - LLVMMetadataRef setting = LLVMValueAsMetadata(LLVMConstInt(options_type, active_target.pie, false)); + LLVMMetadataRef setting = LLVMValueAsMetadata(LLVMConstInt(options_type, (unsigned)active_target.pie, false)); LLVMAddModuleFlag(c->module, LLVMModuleFlagBehaviorOverride, pie_level, strlen(pie_level), setting); } diff --git a/src/compiler/llvm_codegen_stmt.c b/src/compiler/llvm_codegen_stmt.c index 69b8d174a..888eb2822 100644 --- a/src/compiler/llvm_codegen_stmt.c +++ b/src/compiler/llvm_codegen_stmt.c @@ -760,7 +760,7 @@ static void gencontext_emit_switch_body(GenContext *c, BEValue *switch_value, As { bool is_if_chain = switch_ast->switch_stmt.if_chain; Ast **cases = switch_ast->switch_stmt.cases; - ByteSize case_count = vec_size(cases); + ArraySize case_count = vec_size(cases); if (!case_count) { // No body or default is empty, just exit after the value. @@ -1119,7 +1119,7 @@ void gencontext_emit_expr_stmt(GenContext *c, Ast *ast) static LLVMValueRef llvm_emit_string(GenContext *c, const char *str) { LLVMTypeRef char_type = llvm_get_type(c, type_char); - unsigned len = strlen(str); + unsigned len = (unsigned)strlen(str); LLVMTypeRef char_array_type = LLVMArrayType(char_type, len + 1); LLVMValueRef global_string = LLVMAddGlobal(c->module, char_array_type, ""); LLVMSetLinkage(global_string, LLVMInternalLinkage); diff --git a/src/compiler/llvm_codegen_type.c b/src/compiler/llvm_codegen_type.c index 21bfe54fa..d5ce7962a 100644 --- a/src/compiler/llvm_codegen_type.c +++ b/src/compiler/llvm_codegen_type.c @@ -114,7 +114,7 @@ static inline LLVMTypeRef llvm_type_from_array(GenContext *context, Type *type) return type->backend_type = llvm_get_type(context, type->canonical); } - return type->backend_type = LLVMArrayType(llvm_get_type(context, type->array.base), type->array.len); + return type->backend_type = LLVMArrayType(llvm_get_type(context, type->array.base), (unsigned)type->array.len); } @@ -125,7 +125,7 @@ static void param_expand(GenContext *context, LLVMTypeRef** params_ref, Type *ty case TYPE_TYPEDEF: UNREACHABLE case TYPE_ARRAY: - for (ByteSize i = type->array.len; i > 0; i--) + for (ArraySize i = type->array.len; i > 0; i--) { param_expand(context, params_ref, type->array.base); } @@ -173,7 +173,7 @@ static void param_expand(GenContext *context, LLVMTypeRef** params_ref, Type *ty static inline void add_func_type_param(GenContext *context, Type *param_type, ABIArgInfo *arg_info, LLVMTypeRef **params) { - arg_info->param_index_start = vec_size(*params); + arg_info->param_index_start = (MemberIndex)vec_size(*params); switch (arg_info->kind) { case ABI_ARG_IGNORE: @@ -223,7 +223,7 @@ static inline void add_func_type_param(GenContext *context, Type *param_type, AB vec_add(*params, llvm_abi_type(context, arg_info->direct_pair.hi)); break; } - arg_info->param_index_end = vec_size(*params); + arg_info->param_index_end = (MemberIndex)vec_size(*params); } LLVMTypeRef llvm_func_type(GenContext *context, Type *type) diff --git a/src/compiler/module.c b/src/compiler/module.c index fc239fb24..85ed72798 100644 --- a/src/compiler/module.c +++ b/src/compiler/module.c @@ -9,7 +9,7 @@ Decl *module_find_symbol(Module *module, const char *symbol) return stable_get(&module->symbols, symbol); } -Path *path_create_from_string(const char *string, size_t len, SourceSpan span) +Path *path_create_from_string(const char *string, uint32_t len, SourceSpan span) { Path *path = CALLOCS(Path); path->span = span; diff --git a/src/compiler/number.c b/src/compiler/number.c index 7926fc4f3..94b4f4642 100644 --- a/src/compiler/number.c +++ b/src/compiler/number.c @@ -260,14 +260,14 @@ void expr_const_set_float(ExprConst *expr, Real d, TypeKind kind) expr->const_kind = CONST_FLOAT; } -ByteSize expr_const_list_size(const ConstInitializer *list) +ArraySize expr_const_list_size(const ConstInitializer *list) { switch (list->kind) { case CONST_INIT_ZERO: return 0; case CONST_INIT_ARRAY: - return VECLAST(list->init_array.elements)->init_array_value.index + 1; + return (ArraySize)VECLAST(list->init_array.elements)->init_array_value.index + 1; case CONST_INIT_ARRAY_FULL: return vec_size(list->init_array_full); default: diff --git a/src/compiler/parse_expr.c b/src/compiler/parse_expr.c index 654264113..d104a1bd0 100644 --- a/src/compiler/parse_expr.c +++ b/src/compiler/parse_expr.c @@ -787,7 +787,7 @@ static Expr *parse_ct_call(Context *context, Expr *left) } TRY_CONSUME_OR(TOKEN_RBRACKET, "Expected a ']' after the number.", poisoned_expr); flat_element.array = true; - flat_element.index = value.i.low; + flat_element.index = (MemberIndex) value.i.low; } else if (try_consume(context, TOKEN_DOT)) { @@ -972,7 +972,7 @@ static Expr *parse_integer(Context *context, Expr *left) Int128 i = { 0, 0 }; bool is_unsigned = false; - uint64_t type_bits = 0; + int type_bits = 0; int hex_characters = 0; int oct_characters = 0; int binary_characters = 0; @@ -1001,7 +1001,7 @@ static Expr *parse_integer(Context *context, Expr *left) if (c == '_') continue; if (i.high > max) wrapped = true; i = i128_shl64(i, 4); - i = i128_add64(i, hex_nibble(c)); + i = i128_add64(i, (uint64_t) hex_nibble(c)); hex_characters++; } break; @@ -1026,7 +1026,7 @@ static Expr *parse_integer(Context *context, Expr *left) if (c == '_') continue; if (i.high > max) wrapped = true; i = i128_shl64(i, 3); - i = i128_add64(i, c - '0'); + i = i128_add64(i, (uint64_t)(c - '0')); oct_characters++; } break; @@ -1040,7 +1040,7 @@ static Expr *parse_integer(Context *context, Expr *left) binary_characters++; if (i.high > max) wrapped = true; i = i128_shl64(i, 1); - i = i128_add64(i, c - '0'); + i = i128_add64(i, (uint64_t)(c - '0')); } break; default: @@ -1062,7 +1062,7 @@ static Expr *parse_integer(Context *context, Expr *left) if (c == '_') continue; uint64_t old_top = i.high; i = i128_mult64(i, 10); - i = i128_add64(i, c - '0'); + i = i128_add64(i, (uint64_t)(c - '0')); if (!wrapped && old_top > i.high) wrapped = true; } break; @@ -1078,7 +1078,7 @@ static Expr *parse_integer(Context *context, Expr *left) expr_int->const_expr.narrowable = !type_bits; if (type_bits) { - if (!is_power_of_two(type_bits) || type_bits > 128) + if (type_bits < 0 || !is_power_of_two((uint64_t)type_bits) || type_bits > 128) { SEMA_TOKEN_ERROR(context->tok, "Integer type suffix should be i8, i16, i32, i64 or i128."); return poisoned_expr; @@ -1114,28 +1114,28 @@ static Expr *parse_integer(Context *context, Expr *left) } } if (type_bits && type_bits < 8) type_bits = 8; - if (type_bits && !is_power_of_two(type_bits)) type_bits = next_highest_power_of_2(type_bits); + if (type_bits && !is_power_of_two((uint64_t)type_bits)) type_bits = (int)next_highest_power_of_2((uint32_t)type_bits); } if (type_bits) expr_int->const_expr.is_hex = false; if (!type_bits) { - type_bits = type_size(type) * 8; + type_bits = (int)type_size(type) * 8; } if (type_bits) { - type = is_unsigned ? type_int_unsigned_by_bitsize(type_bits) : type_int_signed_by_bitsize(type_bits); + type = is_unsigned ? type_int_unsigned_by_bitsize((unsigned)type_bits) : type_int_signed_by_bitsize((unsigned)type_bits); } expr_int->const_expr.ixx = (Int) { i, type->type_kind }; if (!int_fits(expr_int->const_expr.ixx, type->type_kind)) { - int radix = 10; + unsigned radix = 10; if (hex_characters) radix = 16; if (oct_characters) radix = 8; if (binary_characters) radix = 2; if (type_bits) { SEMA_TOKEN_ERROR(context->tok, "'%s' does not fit in a '%c%d' literal.", - i128_to_string(i, radix, true), is_unsigned ? 'u' : 'i', type_bits); + i128_to_string(i, radix, true), is_unsigned ? 'u' : 'i', type_bits); } else { @@ -1205,10 +1205,10 @@ static void parse_base64(char **result_pointer, char *result_pointer_end, const while ((val2 = base64_to_sextet(*(data++))) < 0); while ((val3 = base64_to_sextet(*(data++))) < 0); while ((val4 = base64_to_sextet(*(data++))) < 0); - uint32_t triplet = (val << 3 * 6) + (val2 << 2 * 6) + (val3 << 6) + val4; - if (data_current < result_pointer_end) *(data_current++) = (triplet >> 16) & 0xFF; - if (data_current < result_pointer_end) *(data_current++) = (triplet >> 8) & 0xFF; - if (data_current < result_pointer_end) *(data_current++) = triplet & 0xFF; + uint32_t triplet = (uint32_t)((val << 3 * 6) + (val2 << 2 * 6) + (val3 << 6) + val4); + if (data_current < result_pointer_end) *(data_current++) = (char)((triplet >> 16) & 0xFF); + if (data_current < result_pointer_end) *(data_current++) = (char)((triplet >> 8) & 0xFF); + if (data_current < result_pointer_end) *(data_current++) = (char)(triplet & 0xFF); } DONE: *result_pointer = data_current; @@ -1218,7 +1218,7 @@ static Expr *parse_bytes_expr(Context *context, Expr *left) { assert(!left && "Had left hand side"); TokenId tok = context->tok.id; - uint64_t len = 0; + ArraySize len = 0; while (TOKTYPE(tok) == TOKEN_BYTES) { len += TOKDATA(tok)->len; @@ -1337,7 +1337,7 @@ static int append_esc_string_token(char *restrict dest, const char *restrict src int h = char_to_nibble(src[1]); int l = char_to_nibble(src[2]); if (h < 0 || l < 0) return -1; - unicode_char = ((unsigned) h << 4U) + l; + unicode_char = ((unsigned) h << 4U) + (unsigned)l; scanned = 3; break; } @@ -1348,7 +1348,7 @@ static int append_esc_string_token(char *restrict dest, const char *restrict src int x3 = char_to_nibble(src[3]); int x4 = char_to_nibble(src[4]); if (x1 < 0 || x2 < 0 || x3 < 0 || x4 < 0) return -1; - unicode_char = ((unsigned) x1 << 12U) + ((unsigned) x2 << 8U) + ((unsigned) x3 << 4U) + x4; + unicode_char = ((unsigned) x1 << 12U) + ((unsigned) x2 << 8U) + ((unsigned) x3 << 4U) + (unsigned)x4; scanned = 5; break; } @@ -1364,7 +1364,7 @@ static int append_esc_string_token(char *restrict dest, const char *restrict src int x8 = char_to_nibble(src[8]); if (x1 < 0 || x2 < 0 || x3 < 0 || x4 < 0 || x5 < 0 || x6 < 0 || x7 < 0 || x8 < 0) return -1; unicode_char = ((unsigned) x1 << 28U) + ((unsigned) x2 << 24U) + ((unsigned) x3 << 20U) + ((unsigned) x4 << 16U) + - ((unsigned) x5 << 12U) + ((unsigned) x6 << 8U) + ((unsigned) x7 << 4U) + x8; + ((unsigned) x5 << 12U) + ((unsigned) x6 << 8U) + ((unsigned) x7 << 4U) + (unsigned)x8; scanned = 9; break; } diff --git a/src/compiler/sema_casts.c b/src/compiler/sema_casts.c index d1827cfe2..960243c1a 100644 --- a/src/compiler/sema_casts.c +++ b/src/compiler/sema_casts.c @@ -499,7 +499,7 @@ bool cast_may_explicit(Type *from_type, Type *to_type, bool ignore_failability, case TYPE_SUBARRAY: return to_kind == TYPE_POINTER; case TYPE_VECTOR: - return type_is_structurally_equivalent(type_get_array(from_type->vector.base, from_type->vector.len), to_type); + return type_is_structurally_equivalent(type_get_array(from_type->vector.base, (uint32_t)from_type->vector.len), to_type); case TYPE_UNTYPED_LIST: REMINDER("Look at untyped list explicit conversions"); return false; @@ -1136,11 +1136,11 @@ bool cast_implicit_bit_width(Expr *expr, Type *to_type) { if (type_is_unsigned(from_canonical)) { - to_type = type_int_unsigned_by_bitsize(type_size(to_canonical) * 8); + to_type = type_int_unsigned_by_bitsize((uint32_t)type_size(to_canonical) * 8); } else { - to_type = type_int_signed_by_bitsize(type_size(to_canonical) * 8); + to_type = type_int_signed_by_bitsize((uint32_t)type_size(to_canonical) * 8); } } } diff --git a/src/compiler/sema_decls.c b/src/compiler/sema_decls.c index dd594ea81..d84e99f7a 100644 --- a/src/compiler/sema_decls.c +++ b/src/compiler/sema_decls.c @@ -103,7 +103,7 @@ static inline bool sema_analyse_struct_member(Context *context, Decl *decl) static bool sema_analyse_union_members(Context *context, Decl *decl, Decl **members) { - ByteSize max_size = 0; + AlignSize max_size = 0; MemberIndex max_alignment_element = 0; AlignSize max_alignment = 0; @@ -127,18 +127,18 @@ static bool sema_analyse_union_members(Context *context, Decl *decl, Decl **memb AlignSize member_alignment = type_abi_alignment(member->type); ByteSize member_size = type_size(member->type); - + assert(member_size <= MAX_TYPE_SIZE); // Update max alignment if (member_alignment > max_alignment) { max_alignment = member_alignment; - max_alignment_element = i; + max_alignment_element = (MemberIndex)i; } // Update max size if (member_size > max_size) { //max_size_element = i; - max_size = member_size; + max_size = (AlignSize)member_size; // If this is bigger than the previous with max // alignment, pick this as the maximum size field. if (max_alignment_element != (MemberIndex)i && max_alignment == member_alignment) @@ -177,15 +177,15 @@ static bool sema_analyse_union_members(Context *context, Decl *decl, Decl **memb } // The actual size might be larger than the max size due to alignment. - unsigned size = aligned_offset(max_size, decl->alignment); + AlignSize size = aligned_offset(max_size, decl->alignment); - unsigned rep_size = type_size(members[max_alignment_element]->type); + ByteSize rep_size = type_size(members[max_alignment_element]->type); // If the actual size is bigger than the real size, add // padding. if (size > rep_size) { - decl->strukt.padding = (int16_t)(size - rep_size); + decl->strukt.padding = (AlignSize)(size - rep_size); } decl->strukt.size = size; @@ -198,8 +198,8 @@ static bool sema_analyse_struct_members(Context *context, Decl *decl, Decl **mem // Default alignment is 1 even if the it is empty. AlignSize natural_alignment = 1; bool is_unaligned = false; - ByteSize size = 0; - ByteSize offset = 0; + AlignSize size = 0; + AlignSize offset = 0; bool is_packed = decl->is_packed; VECEACH(members, i) { @@ -291,7 +291,7 @@ static bool sema_analyse_struct_members(Context *context, Decl *decl, Decl **mem // in this case we need an additional padding if (size > aligned_offset(offset, natural_alignment)) { - decl->strukt.padding = (int16_t)(size - offset); + decl->strukt.padding = (AlignSize)(size - offset); } // If the size is smaller the naturally aligned struct, then it is also unaligned @@ -302,7 +302,7 @@ static bool sema_analyse_struct_members(Context *context, Decl *decl, Decl **mem if (is_unaligned && size > offset) { assert(!decl->strukt.padding); - decl->strukt.padding = (int16_t)(size - offset); + decl->strukt.padding = (AlignSize)(size - offset); } decl->is_packed = is_unaligned; decl->strukt.size = size; @@ -409,13 +409,13 @@ static inline bool sema_analyse_bitstruct_member(Context *context, Decl *decl, u type_quoted_error_string(member->var.type_info->type)); return false; } - int bits = type_size(decl->bitstruct.base_type->type) * 8; + BitSize bits = type_size(decl->bitstruct.base_type->type) * 8; Int max_bits = (Int) { .type = TYPE_I64, .i = { .low = bits } }; Expr *start = member->var.start; Expr *end = member->var.end; if (!sema_analyse_expr(context, start)) return false; - int start_bit; - int end_bit; + unsigned start_bit; + unsigned end_bit; if (start->expr_kind != EXPR_CONST || !type_is_integer(start->type) || int_is_neg(start->const_expr.ixx)) { SEMA_ERROR(start, "This must be a constant non-negative integer value."); @@ -426,7 +426,7 @@ static inline bool sema_analyse_bitstruct_member(Context *context, Decl *decl, u SEMA_ERROR(start, "Expected at the most a bit index of %d\n", bits - 1); return false; } - end_bit = start_bit = start->const_expr.ixx.i.low; + end_bit = start_bit = (unsigned)start->const_expr.ixx.i.low; if (end) { if (!sema_analyse_expr(context, start)) return false; @@ -440,7 +440,7 @@ static inline bool sema_analyse_bitstruct_member(Context *context, Decl *decl, u SEMA_ERROR(end, "Expected at the most a bit index of %d.", bits - 1); return false; } - end_bit = end->const_expr.ixx.i.low; + end_bit = (unsigned)end->const_expr.ixx.i.low; } else { @@ -455,12 +455,12 @@ static inline bool sema_analyse_bitstruct_member(Context *context, Decl *decl, u SEMA_ERROR(start, "The start bit must be smaller than the end bit index."); return false; } - int bitsize_type = member_type == type_bool ? 1 : type_size(member_type) * 8; - int bits_available = end_bit + 1 - start_bit; + TypeSize bitsize_type = member_type == type_bool ? 1 : type_size(member_type) * 8; + TypeSize bits_available = end_bit + 1 - start_bit; if (bitsize_type < bits_available) { SEMA_ERROR(member, "The bit width of %s (%d) is less than the assigned bits (%d), try reducing the range.", - type_quoted_error_string(member->type), bitsize_type, bits_available); + type_quoted_error_string(member->type), (int)bitsize_type, (int)bits_available); return false; } member->var.start_bit = start_bit; @@ -961,7 +961,7 @@ AttributeType sema_analyse_attribute(Context *context, Attr *attr, AttributeDoma } static AttributeDomain attribute_domain[NUMBER_OF_ATTRIBUTES] = { [ATTRIBUTE_WEAK] = ATTR_FUNC | ATTR_CONST | ATTR_GLOBAL, - [ATTRIBUTE_EXTNAME] = ~ATTR_CALL, + [ATTRIBUTE_EXTNAME] = (AttributeDomain)~ATTR_CALL, [ATTRIBUTE_SECTION] = ATTR_FUNC | ATTR_CONST | ATTR_GLOBAL, [ATTRIBUTE_PACKED] = ATTR_STRUCT | ATTR_UNION, [ATTRIBUTE_NORETURN] = ATTR_FUNC, @@ -971,8 +971,8 @@ AttributeType sema_analyse_attribute(Context *context, Attr *attr, AttributeDoma [ATTRIBUTE_OPAQUE] = ATTR_STRUCT | ATTR_UNION | ATTR_BITSTRUCT, [ATTRIBUTE_BIGENDIAN] = ATTR_BITSTRUCT, [ATTRIBUTE_LITTLEENDIAN] = ATTR_BITSTRUCT, - [ATTRIBUTE_USED] = ~ATTR_CALL, - [ATTRIBUTE_UNUSED] = ~ATTR_CALL, + [ATTRIBUTE_USED] = (AttributeDomain)~ATTR_CALL, + [ATTRIBUTE_UNUSED] = (AttributeDomain)~ATTR_CALL, [ATTRIBUTE_NAKED] = ATTR_FUNC, [ATTRIBUTE_CDECL] = ATTR_FUNC, [ATTRIBUTE_STDCALL] = ATTR_FUNC, @@ -1025,7 +1025,7 @@ AttributeType sema_analyse_attribute(Context *context, Attr *attr, AttributeDoma return ATTRIBUTE_NONE; } - attr->alignment = align; + attr->alignment = (AlignSize)align; } return type; case ATTRIBUTE_SECTION: diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 8168060c7..391fc3600 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -242,7 +242,7 @@ static bool sema_bit_assignment_check(Expr *right, Decl *member) { if (right->expr_kind != EXPR_CONST || !type_is_integer(right->type)) return true; - int bits = member->var.end_bit - member->var.start_bit + 1; + unsigned bits = member->var.end_bit - member->var.start_bit + 1; if (bits >= type_size(right->type) * 8 || int_is_zero(right->const_expr.ixx)) return true; @@ -258,11 +258,11 @@ static bool sema_bit_assignment_check(Expr *right, Decl *member) i = i128_neg(i); i = i128_sub64(i, 1); } - bits_used = 1 + 128 - i128_clz(&i); + bits_used = (int) (1 + 128 - i128_clz(&i)); } else { - bits_used = 128 - i128_clz(&i); + bits_used = (int) (128 - i128_clz(&i)); } if (bits_used <= bits) return true; @@ -1779,7 +1779,7 @@ static inline bool sema_expr_analyse_generic_call(Context *context, Expr *call_e return false; } - int offset = total_args - explicit_args; + unsigned offset = total_args - explicit_args; for (unsigned i = 0; i < explicit_args; i++) { Expr *arg = args[i]; @@ -2198,7 +2198,7 @@ static bool expr_check_index_in_range(Context *context, Type *type, Expr *index_ SEMA_ERROR(index_expr, "Negative numbers are not allowed when indexing from the end."); return false; } - ArrayIndex idx = index.i.low; + MemberIndex idx = (MemberIndex)index.i.low; switch (type->type_kind) { case TYPE_POINTER: @@ -2207,7 +2207,7 @@ static bool expr_check_index_in_range(Context *context, Type *type, Expr *index_ case TYPE_ARRAY: case TYPE_VECTOR: { - int64_t len = (int64_t)type->array.len; + MemberIndex len = (MemberIndex)type->array.len; bool is_vector = type->type_kind == TYPE_VECTOR; if (from_end) { @@ -2483,7 +2483,7 @@ static inline void expr_rewrite_to_string(Expr *expr_to_rewrite, const char *str expr_to_rewrite->expr_kind = EXPR_CONST; expr_to_rewrite->const_expr.const_kind = CONST_STRING; expr_to_rewrite->const_expr.string.chars = (char *)string; - expr_to_rewrite->const_expr.string.len = (int)strlen(string); + expr_to_rewrite->const_expr.string.len = (uint32_t)strlen(string); expr_to_rewrite->resolve_status = RESOLVE_DONE; expr_to_rewrite->type = type_compstr; } @@ -2934,7 +2934,7 @@ static Decl *sema_resolve_element_for_name(Decl** decls, DesignatorElement **ele // The simple case, we have a match. if (decl->name == name) { - element->index = i; + element->index = (MemberIndex)i; return decl; } if (!decl->name) @@ -2957,7 +2957,7 @@ static Decl *sema_resolve_element_for_name(Decl** decls, DesignatorElement **ele // Create our anon field. DesignatorElement *anon_element = CALLOCS(DesignatorElement); anon_element->kind = DESIGNATOR_FIELD; - anon_element->index = i; + anon_element->index = (MemberIndex)i; elements[old_index] = anon_element; // Advance (*index)++; @@ -2967,7 +2967,7 @@ static Decl *sema_resolve_element_for_name(Decl** decls, DesignatorElement **ele return NULL; } -static int64_t sema_analyse_designator_index(Context *context, Expr *index) +static MemberIndex sema_analyse_designator_index(Context *context, Expr *index) { if (!sema_analyse_expr_lvalue(context, index)) { @@ -2984,9 +2984,9 @@ static int64_t sema_analyse_designator_index(Context *context, Expr *index) SEMA_ERROR(index, "The index must be a constant value."); return -1; } - if (!int_fits(index->const_expr.ixx, TYPE_I64)) + if (!int_fits(index->const_expr.ixx, TYPE_I32)) { - SEMA_ERROR(index, "The value of the index does not fit in a long."); + SEMA_ERROR(index, "The value of the index does not fit in an int."); return -1; } int64_t index_val = int_to_i64(index->const_expr.ixx); @@ -2995,10 +2995,10 @@ static int64_t sema_analyse_designator_index(Context *context, Expr *index) SEMA_ERROR(index, "Negative index values is not allowed."); return -1; } - return index_val; + return (MemberIndex)index_val; } -static Type *sema_find_type_of_element(Context *context, Type *type, DesignatorElement **elements, unsigned *curr_index, bool *is_constant, bool *did_report_error, ArrayIndex *max_index, Decl **member_ptr) +static Type *sema_find_type_of_element(Context *context, Type *type, DesignatorElement **elements, unsigned *curr_index, bool *is_constant, bool *did_report_error, MemberIndex *max_index, Decl **member_ptr) { Type *type_flattened = type_flatten(type); DesignatorElement *element = elements[*curr_index]; @@ -3024,13 +3024,13 @@ static Type *sema_find_type_of_element(Context *context, Type *type, DesignatorE default: return NULL; } - ArrayIndex index = sema_analyse_designator_index(context, element->index_expr); + MemberIndex index = sema_analyse_designator_index(context, element->index_expr); if (index < 0) { *did_report_error = true; return NULL; } - if (index >= (ArrayIndex)len) + if (index >= (MemberIndex)len) { SEMA_ERROR(element->index_expr, "The index may must be less than the array length (which was %llu).", (unsigned long long)len); *did_report_error = true; @@ -3041,7 +3041,7 @@ static Type *sema_find_type_of_element(Context *context, Type *type, DesignatorE if (max_index && *max_index < index) *max_index = index; if (element->kind == DESIGNATOR_RANGE) { - int64_t end_index = sema_analyse_designator_index(context, element->index_end_expr); + MemberIndex end_index = sema_analyse_designator_index(context, element->index_end_expr); if (end_index < 0) { *did_report_error = true; @@ -3053,7 +3053,7 @@ static Type *sema_find_type_of_element(Context *context, Type *type, DesignatorE *did_report_error = true; return NULL; } - if (end_index > (ArrayIndex)len) + if (end_index > (MemberIndex)len) { *did_report_error = true; SEMA_ERROR(element->index_expr, "The index may must be less than the array length (which was %llu).", (unsigned long long)len); @@ -3075,7 +3075,7 @@ static Type *sema_find_type_of_element(Context *context, Type *type, DesignatorE return member->type; } -static Type *sema_expr_analyse_designator(Context *context, Type *current, Expr *expr, ArrayIndex *max_index, Decl **member_ptr) +static Type *sema_expr_analyse_designator(Context *context, Type *current, Expr *expr, MemberIndex *max_index, Decl **member_ptr) { DesignatorElement **path = expr->designator_expr.path; @@ -3255,8 +3255,8 @@ static inline void sema_update_const_initializer_with_designator_array(ConstInit Expr *value) { DesignatorElement *element = curr[0]; - ArrayIndex low_index = element->index; - ArrayIndex high_index = element->kind == DESIGNATOR_RANGE ? element->index_end : element->index; + MemberIndex low_index = element->index; + MemberIndex high_index = element->kind == DESIGNATOR_RANGE ? element->index_end : element->index; assert(element->kind == DESIGNATOR_ARRAY || element->kind == DESIGNATOR_RANGE); // Expand zero into array. @@ -3275,9 +3275,9 @@ static inline void sema_update_const_initializer_with_designator_array(ConstInit unsigned array_count = vec_size(array_elements); - ArrayIndex insert_index = 0; + MemberIndex insert_index = 0; - for (ArrayIndex index = low_index; index <= high_index; index++) + for (MemberIndex index = low_index; index <= high_index; index++) { // Walk to the insert point or until we reached the end of the array. while (insert_index < array_count && array_elements[insert_index]->init_array_value.index < index) @@ -3398,7 +3398,7 @@ static bool sema_expr_analyse_designated_initializer(Context *context, Type *ass Type *original = assigned->canonical; bool is_bitstruct = original->type_kind == TYPE_BITSTRUCT; bool is_structlike = type_is_structlike(original) || is_bitstruct; - ArrayIndex max_index = -1; + MemberIndex max_index = -1; bool failable = false; VECEACH(init_expressions, i) { @@ -3419,7 +3419,7 @@ static bool sema_expr_analyse_designated_initializer(Context *context, Type *ass if (!is_structlike && initializer->type->type_kind == TYPE_INFERRED_ARRAY) { - initializer->type = sema_type_lower_by_size(initializer->type, max_index + 1); + initializer->type = sema_type_lower_by_size(initializer->type, (ArraySize)(max_index + 1)); } if (expr_is_constant_eval(initializer, CONSTANT_EVAL_ANY)) @@ -3466,8 +3466,8 @@ static inline bool sema_expr_analyse_struct_plain_initializer(Context *context, { Expr **elements = initializer->initializer_list; Decl **members = assigned->strukt.members; - unsigned size = vec_size(elements); - unsigned expected_members = vec_size(members); + MemberIndex size = (MemberIndex)vec_size(elements); + MemberIndex expected_members = (MemberIndex)vec_size(members); // 1. For struct number of members must be the same as the size of the struct. // Since we already handled the case with an empty initializer before going here @@ -3495,8 +3495,8 @@ static inline bool sema_expr_analyse_struct_plain_initializer(Context *context, } } // 3. Loop through all elements. - int max_loop = MAX(size, expected_members); - for (unsigned i = 0; i < max_loop; i++) + MemberIndex max_loop = MAX(size, expected_members); + for (MemberIndex i = 0; i < max_loop; i++) { // 4. Check if we exceeded the list of elements in the struct/union. // This way we can check the other elements which might help the @@ -3515,7 +3515,7 @@ static inline bool sema_expr_analyse_struct_plain_initializer(Context *context, if (!sub_element_count) { vec_add(initializer->initializer_list, NULL); - for (int j = size - 1; j > i; j--) + for (int j = (int)(size - 1); j > i; j--) { initializer->initializer_list[j] = initializer->initializer_list[j - 1]; } @@ -3530,7 +3530,7 @@ static inline bool sema_expr_analyse_struct_plain_initializer(Context *context, } if (i >= size) { - not_enough_elements(initializer, i); + not_enough_elements(initializer, (int)i); return false; } Expr *new_initializer = expr_new(EXPR_INITIALIZER_LIST, elements[i]->span); @@ -3543,7 +3543,7 @@ static inline bool sema_expr_analyse_struct_plain_initializer(Context *context, size -= reduce_by; max_loop = MAX(size, expected_members); assert(size <= vec_size(initializer->initializer_list)); - vec_resize(initializer->initializer_list, size); + vec_resize(initializer->initializer_list, (unsigned)size); elements = initializer->initializer_list; elements[i] = new_initializer; } @@ -4776,8 +4776,8 @@ static bool sema_expr_analyse_and_or(Context *context, Expr *expr, Expr *left, E static void cast_to_max_bit_size(Context *context, Expr *left, Expr *right, Type *left_type, Type *right_type) { - int bit_size_left = left_type->builtin.bitsize; - int bit_size_right = right_type->builtin.bitsize; + unsigned bit_size_left = left_type->builtin.bitsize; + unsigned bit_size_right = right_type->builtin.bitsize; assert(bit_size_left && bit_size_right); if (bit_size_left == bit_size_right) return; if (bit_size_left < bit_size_right) @@ -5772,7 +5772,7 @@ static inline char minilex_next(MiniLexer *lexer) static inline char minilex_peek(MiniLexer *lexer, int32_t offset) { - return lexer->chars[lexer->index + offset]; + return lexer->chars[lexer->index + (uint32_t)offset]; } static inline bool minilex_match(MiniLexer *lexer, char c) @@ -5796,11 +5796,11 @@ static inline void minilex_skip_whitespace(MiniLexer *lexer) uint64_t minilex_parse_number(MiniLexer *lexer, uint64_t max) { assert(max < UINT64_MAX); - ByteSize value = 0; + uint64_t value = 0; while (is_number(minilex_peek(lexer, 0))) { - ByteSize old_value = value; - value = value * 10 + minilex_next(lexer) - '0'; + uint64_t old_value = value; + value = value * 10 + (uint64_t)minilex_next(lexer) - '0'; if (old_value > value || value > max) { return UINT64_MAX; @@ -5827,7 +5827,7 @@ static inline bool sema_analyse_idents_string(Context *context, MiniLexer *lexer if (value == UINT64_MAX) return false; if (!minilex_match(lexer, ']')) return false; element.array = true; - element.index = (ArrayIndex)value; + element.index = (MemberIndex)value; vec_add(elements, element); continue; } @@ -5981,11 +5981,11 @@ static inline bool sema_analyse_identifier_path_string(Context *context, SourceS type = type_get_subarray(type); continue; } - ByteSize array_size = 0; + ArraySize array_size = 0; while (is_number(minilex_peek(&lexer, 0))) { ByteSize old_array_size = array_size; - array_size = array_size * 10 + minilex_next(&lexer) - '0'; + array_size = array_size * 10 + (ArraySize)minilex_next(&lexer) - '0'; if (old_array_size > array_size || array_size > MAX_ARRAYINDEX) { sema_error_range(span, "Array index out of bounds."); @@ -6103,8 +6103,8 @@ static inline bool sema_expr_analyse_ct_alignof(Context *context, Expr *expr) return false; } type = actual_type->array.base; - ByteSize size = type_size(type); - align = (AlignSize)type_min_alignment(size * element.index, align); + TypeSize size = type_size(type); + align = type_min_alignment(size * (AlignSize)element.index, align); continue; } if (!type_is_structlike(actual_type)) @@ -6272,7 +6272,7 @@ static Type *sema_expr_check_type_exists(Context *context, TypeInfo *type_info) return poisoned_type; case TYPE_INFO_VECTOR: { - ArrayIndex size; + ArraySize size; if (!sema_resolve_array_like_len(context, type_info, &size)) return poisoned_type; Type *type = sema_expr_check_type_exists(context, type_info->array.base); if (!type) return NULL; @@ -6288,7 +6288,7 @@ static Type *sema_expr_check_type_exists(Context *context, TypeInfo *type_info) } case TYPE_INFO_ARRAY: { - ArrayIndex size; + ArraySize size; if (!sema_resolve_array_like_len(context, type_info, &size)) return poisoned_type; Type *type = sema_expr_check_type_exists(context, type_info->array.base); if (!type) return NULL; @@ -6450,7 +6450,7 @@ static inline bool sema_expr_analyse_ct_offsetof(Context *context, Expr *expr) return false; } type = actual_type->array.base; - offset += type_size(type) * element.index; + offset += type_size(type) * (ArraySize)element.index; continue; } if (!type_is_structlike(actual_type)) @@ -6735,7 +6735,7 @@ bool sema_analyse_expr_lvalue(Context *context, Expr *expr) } } -ArrayIndex sema_get_initializer_const_array_size(Context *context, Expr *initializer, bool *may_be_array, bool *is_const_size) +MemberIndex sema_get_initializer_const_array_size(Context *context, Expr *initializer, bool *may_be_array, bool *is_const_size) { if (initializer->expr_kind == EXPR_CONST) { @@ -6749,7 +6749,7 @@ ArrayIndex sema_get_initializer_const_array_size(Context *context, Expr *initial if (type->type_kind == TYPE_ARRAY) { *may_be_array = true; - return type->array.len; + return (MemberIndex)type->array.len; } if (type->type_kind == TYPE_SUBARRAY) { @@ -6763,7 +6763,7 @@ ArrayIndex sema_get_initializer_const_array_size(Context *context, Expr *initial return VECLAST(init->init_array.elements)->init_array_value.index + 1; case CONST_INIT_ARRAY_FULL: *may_be_array = true; - return vec_size(init->init_array_full); + return (MemberIndex)vec_size(init->init_array_full); case CONST_INIT_ARRAY_VALUE: UNREACHABLE; case CONST_INIT_STRUCT: @@ -6779,14 +6779,14 @@ ArrayIndex sema_get_initializer_const_array_size(Context *context, Expr *initial case EXPR_INITIALIZER_LIST: *may_be_array = true; *is_const_size = true; - return vec_size(initializer->initializer_list); + return (MemberIndex)vec_size(initializer->initializer_list); case EXPR_DESIGNATED_INITIALIZER_LIST: break; default: UNREACHABLE } Expr **initializers = initializer->designated_init_list; - ArrayIndex size = 0; + MemberIndex size = 0; // Otherwise we assume everything's a designator. VECEACH(initializers, i) { @@ -6802,7 +6802,7 @@ ArrayIndex sema_get_initializer_const_array_size(Context *context, Expr *initial return -1; case DESIGNATOR_ARRAY: { - ArrayIndex index = sema_analyse_designator_index(context, element->index_expr); + MemberIndex index = sema_analyse_designator_index(context, element->index_expr); if (index < 0 || element->index_expr->expr_kind != EXPR_CONST) { *is_const_size = false; @@ -6813,7 +6813,7 @@ ArrayIndex sema_get_initializer_const_array_size(Context *context, Expr *initial } case DESIGNATOR_RANGE: { - ArrayIndex index = sema_analyse_designator_index(context, element->index_end_expr); + MemberIndex index = sema_analyse_designator_index(context, element->index_end_expr); if (index < 0 || element->index_end_expr->expr_kind != EXPR_CONST) { *is_const_size = false; diff --git a/src/compiler/sema_stmts.c b/src/compiler/sema_stmts.c index 473a94163..319ce64e8 100644 --- a/src/compiler/sema_stmts.c +++ b/src/compiler/sema_stmts.c @@ -1096,7 +1096,7 @@ static inline bool sema_analyse_foreach_stmt(Context *context, Ast *statement) { bool may_be_array; bool is_const_size; - ArrayIndex size = sema_get_initializer_const_array_size(context, enumerator, &may_be_array, &is_const_size); + MemberIndex size = sema_get_initializer_const_array_size(context, enumerator, &may_be_array, &is_const_size); if (!may_be_array) { SEMA_ERROR(enumerator, @@ -1125,7 +1125,7 @@ static inline bool sema_analyse_foreach_stmt(Context *context, Ast *statement) // First infer the type of the variable. if (!sema_resolve_type_info(context, variable_type_info)) return false; // And create the inferred type: - inferred_type = type_get_array(var->var.type_info->type, size); + inferred_type = type_get_array(var->var.type_info->type, (ArraySize)size); } // because we don't want the index + variable to move into the internal scope @@ -1815,8 +1815,8 @@ static bool sema_analyse_ct_switch_body(Context *context, Ast *statement) ExprConst *switch_expr_const = &cond->const_expr; Ast **cases = statement->ct_switch_stmt.body; unsigned case_count = vec_size(cases); - int matched_case = case_count; - int default_case = case_count; + int matched_case = (int)case_count; + int default_case = (int)case_count; for (unsigned i = 0; i < case_count; i++) { Ast *stmt = cases[i]; @@ -1858,7 +1858,7 @@ static bool sema_analyse_ct_switch_body(Context *context, Ast *statement) } if (expr_const_compare(switch_expr_const, const_expr, BINARYOP_EQ)) { - matched_case = i; + matched_case = (int) i; } break; } @@ -1869,7 +1869,7 @@ static bool sema_analyse_ct_switch_body(Context *context, Ast *statement) SEMA_PREV(cases[default_case], "The previous $default was here."); return false; } - default_case = i; + default_case = (int)i; continue; default: UNREACHABLE; diff --git a/src/compiler/sema_types.c b/src/compiler/sema_types.c index 1d1771eca..cfdce88da 100644 --- a/src/compiler/sema_types.c +++ b/src/compiler/sema_types.c @@ -15,7 +15,7 @@ static inline bool sema_resolve_ptr_type(Context *context, TypeInfo *type_info) return true; } -bool sema_resolve_array_like_len(Context *context, TypeInfo *type_info, ArrayIndex *len_ref) +bool sema_resolve_array_like_len(Context *context, TypeInfo *type_info, ArraySize *len_ref) { Expr *len_expr = type_info->array.len; if (!sema_analyse_expr(context, len_expr)) return type_info_poison(type_info); @@ -57,7 +57,7 @@ bool sema_resolve_array_like_len(Context *context, TypeInfo *type_info, ArrayInd } return type_info_poison(type_info); } - *len_ref = len.i.low; + *len_ref = (ArraySize)len.i.low; return true; } @@ -89,14 +89,14 @@ static inline bool sema_resolve_array_type(Context *context, TypeInfo *type, boo break; case TYPE_INFO_VECTOR: { - ArrayIndex width; + ArraySize width; if (!sema_resolve_array_like_len(context, type, &width)) return type_info_poison(type); type->type = type_get_vector(type->array.base->type, width); break; } case TYPE_INFO_ARRAY: { - ArrayIndex size; + ArraySize size; if (!sema_resolve_array_like_len(context, type, &size)) return type_info_poison(type); type->type = type_get_array(type->array.base->type, size); break; @@ -272,7 +272,7 @@ bool sema_resolve_type_shallow(Context *context, TypeInfo *type_info, bool allow return true; } -Type *sema_type_lower_by_size(Type *type, ByteSize element_size) +Type *sema_type_lower_by_size(Type *type, ArraySize element_size) { if (type->type_kind != TYPE_INFERRED_ARRAY) return type; diff --git a/src/compiler/source_file.c b/src/compiler/source_file.c index 18d408154..1daff0708 100644 --- a/src/compiler/source_file.c +++ b/src/compiler/source_file.c @@ -78,7 +78,7 @@ SourcePosition source_file_find_position_in_file(File *file, SourceLoc loc) { assert(file->start_id <= loc); - size_t lines = vec_size(file->lines); + unsigned lines = vec_size(file->lines); unsigned low = 0; unsigned high = lines; while (1) diff --git a/src/compiler/symtab.c b/src/compiler/symtab.c index 697ed90e0..6f80ce515 100644 --- a/src/compiler/symtab.c +++ b/src/compiler/symtab.c @@ -101,7 +101,7 @@ void symtab_init(uint32_t capacity) // Add keywords. for (int i = 0; i < TOKEN_LAST; i++) { - const char* name = token_type_to_string(i); + const char* name = token_type_to_string((TokenType)i); // Skip non-keywords if (!is_lower(name[0])) { diff --git a/src/compiler/target.c b/src/compiler/target.c index ac24ea283..13186bc07 100644 --- a/src/compiler/target.c +++ b/src/compiler/target.c @@ -9,8 +9,8 @@ static ArchType arch_from_llvm_string(StringSlice string); static EnvironmentType environment_type_from_llvm_string(StringSlice string); static bool arch_is_supported(ArchType arch); static unsigned os_target_c_type_bits(OsType os, ArchType arch, CType type); -static AlignData os_target_alignment_of_int(OsType os, ArchType arch, int bits); -static AlignData os_target_alignment_of_float(OsType os, ArchType arch, int bits); +static AlignData os_target_alignment_of_int(OsType os, ArchType arch, uint32_t bits); +static AlignData os_target_alignment_of_float(OsType os, ArchType arch, uint32_t bits); static OsType os_from_llvm_string(StringSlice string); static VendorType vendor_from_llvm_string(StringSlice string); static ObjectFormatType object_format_from_os(OsType os); @@ -30,11 +30,11 @@ int target_alloca_addr_space() static void type_dump(LLVMTargetDataRef llvm_target_data, LLVMTypeRef type) { - unsigned size = LLVMSizeOfTypeInBits(llvm_target_data, type); + unsigned long long size = LLVMSizeOfTypeInBits(llvm_target_data, type); unsigned abialign = LLVMABIAlignmentOfType(llvm_target_data, type) * 8; unsigned prefalign = LLVMPreferredAlignmentOfType(llvm_target_data, type) * 8; - printf(" | %-3d %-3d %-3d", size, abialign, prefalign); + printf(" | %-3llu %-3d %-3d", size, abialign, prefalign); } @@ -910,7 +910,7 @@ typedef enum { } Mangling; -static AlignData os_target_alignment_of_int(OsType os, ArchType arch, int bits) +static AlignData os_target_alignment_of_int(OsType os, ArchType arch, uint32_t bits) { switch (arch) { @@ -921,8 +921,8 @@ static AlignData os_target_alignment_of_int(OsType os, ArchType arch, int bits) case ARCH_TYPE_THUMB: case ARCH_TYPE_ARMB: case ARCH_TYPE_THUMBEB: - if ((os_is_apple(os) || os == OS_TYPE_NETBSD) && bits > 32) return (AlignData) { 32, MIN(64, bits) }; - return (AlignData) { MIN(64, bits), MIN(64, bits) }; + if ((os_is_apple(os) || os == OS_TYPE_NETBSD) && bits > 32) return (AlignData) { 32, MIN(64u, bits) }; + return (AlignData) { MIN(64u, bits), MIN(64u, bits) }; case ARCH_TYPE_PPC64: case ARCH_TYPE_PPC: case ARCH_TYPE_PPC64LE: @@ -930,7 +930,7 @@ static AlignData os_target_alignment_of_int(OsType os, ArchType arch, int bits) case ARCH_TYPE_WASM64: case ARCH_TYPE_RISCV32: case ARCH_TYPE_WASM32: - return (AlignData) { MIN(64, bits), MIN(64, bits) }; + return (AlignData) { MIN(64u, bits), MIN(64u, bits) }; case ARCH_TYPE_RISCV64: return (AlignData) { bits, bits }; case ARCH_TYPE_AARCH64: @@ -975,7 +975,7 @@ static unsigned arch_big_endian(ArchType arch) } -static AlignData os_target_alignment_of_float(OsType os, ArchType arch, int bits) +static AlignData os_target_alignment_of_float(OsType os, ArchType arch, uint32_t bits) { switch (arch) { @@ -988,7 +988,7 @@ static AlignData os_target_alignment_of_float(OsType os, ArchType arch, int bits { return (AlignData) { bits, bits }; } - return (AlignData) { MIN(32, bits), bits }; + return (AlignData) { MIN(32u, bits), bits }; case ARCH_TYPE_AARCH64: case ARCH_TYPE_AARCH64_BE: case ARCH_TYPE_PPC64: @@ -1096,7 +1096,7 @@ static PieGeneration arch_os_pie_default(ArchType arch, OsType os, EnvironmentTy UNREACHABLE } -static unsigned os_target_pref_alignment_of_float(OsType os, ArchType arch, int bits) +static AlignSize os_target_pref_alignment_of_float(OsType os, ArchType arch, uint32_t bits) { switch (arch) { @@ -1264,7 +1264,7 @@ void target_setup(BuildTarget *target) platform_target.float16 = os_target_supports_float16(platform_target.os, platform_target.arch); for (BitSizes i = BITS8; i < BITSIZES_LEN; i++) { - unsigned bits = 8 << (i - 1); + unsigned bits = (unsigned) (8 << (i - 1)); platform_target.integers[i] = os_target_alignment_of_int(platform_target.os, platform_target.arch, bits); platform_target.floats[i] = os_target_alignment_of_float(platform_target.os, platform_target.arch, bits); } diff --git a/src/compiler/types.c b/src/compiler/types.c index 1787306b2..d176a52b0 100644 --- a/src/compiler/types.c +++ b/src/compiler/types.c @@ -188,7 +188,7 @@ void type_append_signature_name(Type *type, char *dst, size_t *offset) -ByteSize type_size(Type *type) +TypeSize type_size(Type *type) { RETRY: switch (type->type_kind) @@ -201,11 +201,11 @@ RETRY: goto RETRY; case TYPE_VECTOR: { - ByteSize width = type_size(type->vector.base) * type->vector.len; + TypeSize width = type_size(type->vector.base) * type->vector.len; if (width & (width - 1)) { - ByteSize alignment = next_highest_power_of_2(width); - width = aligned_offset(width, alignment); + AlignSize alignment = next_highest_power_of_2((uint32_t) width); + width = aligned_offset((AlignSize)width, alignment); } return width; } @@ -412,11 +412,11 @@ AlignSize type_abi_alignment(Type *type) goto RETRY; case TYPE_VECTOR: { - ByteSize width = type_size(type->vector.base) * type->vector.len; - ByteSize alignment = width; + ByteSize width = type_size(type->vector.base) * (uint32_t)type->vector.len; + AlignSize alignment = (AlignSize)(int32_t)width; if (alignment & (alignment - 1)) { - alignment = next_highest_power_of_2(alignment); + alignment = (AlignSize)next_highest_power_of_2((uint32_t)alignment); } if (max_alignment_vector && alignment > max_alignment_vector) alignment = max_alignment_vector; return alignment; @@ -610,7 +610,7 @@ static inline bool array_structurally_equivalent_to_struct(Type *array, Type *ty { assert(array->type_kind == TYPE_ARRAY); - ByteSize len = array->array.len; + MemberIndex len = (MemberIndex)array->array.len; if (!len) return type_size(type) == 0; Type *base = array->array.base; @@ -624,7 +624,7 @@ static inline bool array_structurally_equivalent_to_struct(Type *array, Type *ty Decl **members = type->decl->strukt.members; // For structs / errors, all members must match. - ArrayIndex offset = 0; + MemberIndex offset = 0; AlignSize align_size = type_abi_alignment(array); Type *array_base = array->array.base; VECEACH(members, i) @@ -738,7 +738,7 @@ Type *type_get_indexed_type(Type *type) } } -static Type *type_create_array(Type *element_type, uint64_t len, bool vector, bool canonical) +static Type *type_create_array(Type *element_type, ArraySize len, bool vector, bool canonical) { if (canonical) element_type = element_type->canonical; if (!element_type->type_cache) @@ -763,13 +763,13 @@ static Type *type_create_array(Type *element_type, uint64_t len, bool vector, bo Type *vec_arr; if (vector) { - vec_arr = type_new(TYPE_VECTOR, strformat("%s[<%llu>]", element_type->name, len)); + vec_arr = type_new(TYPE_VECTOR, strformat("%s[<%u>]", element_type->name, len)); vec_arr->vector.base = element_type; vec_arr->vector.len = len; } else { - vec_arr = type_new(TYPE_ARRAY, strformat("%s[%llu]", element_type->name, len)); + vec_arr = type_new(TYPE_ARRAY, strformat("%s[%u]", element_type->name, len)); vec_arr->array.base = element_type; vec_arr->array.len = len; } @@ -785,7 +785,7 @@ static Type *type_create_array(Type *element_type, uint64_t len, bool vector, bo return vec_arr; } -Type *type_get_array(Type *arr_type, ByteSize len) +Type *type_get_array(Type *arr_type, ArraySize len) { return type_create_array(arr_type, len, false, false); } @@ -811,7 +811,7 @@ Type *type_get_vector_bool(Type *original_type) { Type *type = type_flatten(original_type); ByteSize size = type_size(type->vector.base); - return type_get_vector(type_int_signed_by_bitsize(size * 8), original_type->vector.len); + return type_get_vector(type_int_signed_by_bitsize((unsigned)size * 8), (unsigned)original_type->vector.len); } Type *type_get_vector(Type *vector_type, unsigned len) @@ -982,20 +982,20 @@ Type *type_find_function_type(FunctionSignature *signature) static inline void type_init_int(const char *name, Type *type, TypeKind kind, BitSizes bits) { - int actual_bits = bits ? 8 << (bits - 1) : 1; + unsigned actual_bits = bits ? (unsigned int)(8 << (bits - 1)) : 1; type_init(name, type, kind, actual_bits, platform_target.integers[bits]); } static inline void type_create_float(const char *name, Type *type, TypeKind kind, BitSizes bits) { - int actual_bits = bits ? 8 << (bits - 1) : 1; + unsigned actual_bits = bits ? (unsigned int)(8 << (bits - 1)) : 1; type_init(name, type, kind, actual_bits, platform_target.floats[bits]); } void type_setup(PlatformTarget *target) { stable_init(&function_types, 0x1000); - max_alignment_vector = target->align_max_vector; + max_alignment_vector = (AlignSize)target->align_max_vector; type_create_float("float16", &t.f16, TYPE_F16, BITS16); type_create_float("float", &t.f32, TYPE_F32, BITS32); @@ -1043,7 +1043,7 @@ void type_setup(PlatformTarget *target) type_create_alias("iptrdiff", &t.iptrdiff, type_int_signed_by_bitsize(target->width_pointer)); alignment_subarray = MAX(type_abi_alignment(&t.voidstar), type_abi_alignment(t.usz.canonical)); - size_subarray = alignment_subarray * 2; + size_subarray = (unsigned)(alignment_subarray * 2); type_init("anyerr", &t.anyerr, TYPE_ANYERR, target->width_pointer, target->align_pointer); } diff --git a/src/compiler_tests/benchmark.c b/src/compiler_tests/benchmark.c index 1d3a90643..fb29c8011 100644 --- a/src/compiler_tests/benchmark.c +++ b/src/compiler_tests/benchmark.c @@ -13,5 +13,5 @@ void bench_begin(void) } double bench_mark(void) { - return (double)(clock() - begin) / (double)CLOCKS_PER_SEC; + return (double)(clock() - (unsigned long)begin) / (double)CLOCKS_PER_SEC; } diff --git a/src/utils/lib.h b/src/utils/lib.h index 575867645..de88e50a5 100644 --- a/src/utils/lib.h +++ b/src/utils/lib.h @@ -351,28 +351,29 @@ static inline uint32_t fnv1a(const char *key, uint32_t len) typedef struct { - unsigned size; - unsigned capacity; + uint32_t size; + uint32_t capacity; char data[]; } VHeader_; static inline VHeader_* vec_new_(size_t element_size, size_t capacity) { assert(capacity < UINT32_MAX); + assert(element_size < UINT32_MAX / 100); VHeader_ *header = malloc_arena(element_size * capacity + sizeof(VHeader_)); header->size = 0; - header->capacity = (unsigned)capacity; + header->capacity = (uint32_t)capacity; return header; } -static inline unsigned vec_size(const void *vec) +static inline uint32_t vec_size(const void *vec) { if (!vec) return 0; const VHeader_ *header = vec; return header[-1].size; } -static inline void vec_resize(void *vec, unsigned new_size) +static inline void vec_resize(void *vec, uint32_t new_size) { if (!vec) return; VHeader_ *header = vec; diff --git a/src/utils/stringutils.c b/src/utils/stringutils.c index 6bef7dbe6..6ce9df902 100644 --- a/src/utils/stringutils.c +++ b/src/utils/stringutils.c @@ -14,9 +14,9 @@ char *strformat(const char *var, ...) va_start(list, var); int len = vsnprintf(NULL, 0, var, list); va_end(list); - if (len == 0) return ""; + if (len < 1) return ""; va_start(list, var); - char *buffer = malloc_arena(len + 1); + char *buffer = malloc_arena((uint32_t)len + 1); int new_len = vsnprintf(buffer, len + 1, var, list); va_end(list); assert(len == new_len); @@ -69,8 +69,8 @@ char *strcopy(const char *start, size_t len) char *strcat_arena(const char *a, const char *b) { - unsigned a_len = strlen(a); - unsigned b_len = strlen(b); + unsigned a_len = (unsigned)strlen(a); + unsigned b_len = (unsigned)strlen(b); char *buffer = malloc_arena(a_len + b_len + 1); memcpy(buffer, a, a_len); memcpy(buffer + a_len, b, b_len); diff --git a/src/utils/taskqueue.c b/src/utils/taskqueue.c index fd5c44a38..6a895c422 100644 --- a/src/utils/taskqueue.c +++ b/src/utils/taskqueue.c @@ -52,7 +52,7 @@ TaskQueueRef taskqueue_create(int threads) { assert(threads > 0); TaskQueue *queue = CALLOCS(TaskQueue); - queue->threads = malloc_arena(sizeof(pthread_t) * threads); + queue->threads = malloc_arena(sizeof(pthread_t) * (unsigned)threads); queue->thread_count = threads; if (pthread_mutex_init(&queue->lock, NULL)) error_exit("Failed to set up mutex"); if (pthread_cond_init(&queue->notify, NULL)) error_exit("Failed to set up cond"); diff --git a/src/utils/toml.c b/src/utils/toml.c index 72e2989f3..3798c79d3 100644 --- a/src/utils/toml.c +++ b/src/utils/toml.c @@ -48,7 +48,7 @@ int toml_vasprintf(char **str, TOML_CONST char *format, va_list args) return size; } - *str = malloc(size + 1); + *str = malloc((unsigned)size + 1); if (*str == NULL) { return -1; @@ -929,7 +929,7 @@ void toml_encode_unicode_scalar(TomlString *result, TomlParser *parser, int n, T char ch = *parser->ptr; if (isxdigit(ch)) { - scalar = scalar * 16 + toml_hex_char_to_int(ch); + scalar = scalar * 16 + (unsigned)toml_hex_char_to_int(ch); toml_move_next(parser); } else diff --git a/src/utils/whereami.c b/src/utils/whereami.c index 634e2fc99..517870f56 100644 --- a/src/utils/whereami.c +++ b/src/utils/whereami.c @@ -417,7 +417,7 @@ char *find_executable_path(void) { int len = get_executable_path_raw(NULL, 0, NULL); if (len < 0) return ""; - char *path = malloc(len + 1); + char *path = malloc((unsigned)len + 1); get_executable_path_raw(path, len, NULL); path[len] = '\0'; for (int i = 0; i < len; ++i)