From a056efce04ae6d73749eb027eb09d3f744fdac2a Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 30 Jan 2025 19:32:20 +0100 Subject: [PATCH] Additional cleanup. --- src/compiler/context.c | 12 +- src/compiler/diagnostics.c | 5 +- src/compiler/enums.h | 2 +- src/compiler/expr.c | 1 + src/compiler/json_output.c | 1 - src/compiler/lexer.c | 31 ++--- src/compiler/linker.c | 7 +- src/compiler/llvm_codegen.c | 41 +++---- src/compiler/llvm_codegen_builtins.c | 35 +++--- src/compiler/llvm_codegen_expr.c | 137 +++------------------- src/compiler/llvm_codegen_function.c | 16 +-- src/compiler/llvm_codegen_internal.h | 18 ++- src/compiler/llvm_codegen_internal_impl.h | 7 +- src/compiler/llvm_codegen_stmt.c | 2 +- src/compiler/llvm_codegen_storeload.c | 4 +- src/compiler/llvm_codegen_type.c | 9 +- src/compiler/parse_expr.c | 8 +- src/compiler/parse_global.c | 34 +++--- src/compiler/parser_internal.h | 6 +- src/compiler/sema_passes.c | 10 +- 20 files changed, 125 insertions(+), 261 deletions(-) diff --git a/src/compiler/context.c b/src/compiler/context.c index ca8add2e0..7ffe0f705 100644 --- a/src/compiler/context.c +++ b/src/compiler/context.c @@ -137,7 +137,7 @@ bool context_set_module(ParseContext *context, Path *path, const char **generic_ bool context_is_macro(SemaContext *context) { if (context->current_macro != NULL) return true; - return context->call_env.current_function && context->call_env.current_function->func_decl.in_macro; + return context->call_env.current_function && context->call_env.current_function->func_decl.in_macro; // NOLINT } void unit_register_external_symbol(SemaContext *context, Decl *decl) @@ -211,10 +211,7 @@ void unit_register_global_decl(CompilationUnit *unit, Decl *decl) vec_add(unit->methods_to_register, decl); return; } - else - { - vec_add(unit->macros, decl); - } + vec_add(unit->macros, decl); decl_register(decl); break; case DECL_FUNC: @@ -229,10 +226,7 @@ void unit_register_global_decl(CompilationUnit *unit, Decl *decl) vec_add(unit->methods_to_register, decl); return; } - else - { - vec_add(unit->functions, decl); - } + vec_add(unit->functions, decl); decl_register(decl); break; case DECL_VAR: diff --git a/src/compiler/diagnostics.c b/src/compiler/diagnostics.c index 464ff70b6..89df5b859 100644 --- a/src/compiler/diagnostics.c +++ b/src/compiler/diagnostics.c @@ -1,13 +1,10 @@ -// Copyright (c) 2019 Christoffer Lerno. All rights reserved. +// Copyright (c) 2019-2025 Christoffer Lerno. All rights reserved. // Use of this source code is governed by the GNU LGPLv3.0 license // a copy of which can be found in the LICENSE file. #include "compiler_internal.h" #include - - - #define LINES_SHOWN 4 #define MAX_WIDTH 120 diff --git a/src/compiler/enums.h b/src/compiler/enums.h index c201ffa6b..1a6748f25 100644 --- a/src/compiler/enums.h +++ b/src/compiler/enums.h @@ -1,4 +1,4 @@ -// Copyright (c) 2019-2023 Christoffer Lerno. All rights reserved. +// Copyright (c) 2019-2025 Christoffer Lerno. All rights reserved. // Use of this source code is governed by the GNU LGPLv3.0 license // a copy of which can be found in the LICENSE file. diff --git a/src/compiler/expr.c b/src/compiler/expr.c index 53cce89d8..21b984b15 100644 --- a/src/compiler/expr.c +++ b/src/compiler/expr.c @@ -716,6 +716,7 @@ bool expr_rewrite_to_const_initializer_index(Type *list_type, ConstInitializer * case CONST_INIT_ARRAY_VALUE: return false; case CONST_INIT_VALUE: + assert(initializer); expr_replace(result, initializer->init_value); return true; } diff --git a/src/compiler/json_output.c b/src/compiler/json_output.c index b2922930f..b0141a104 100644 --- a/src/compiler/json_output.c +++ b/src/compiler/json_output.c @@ -273,7 +273,6 @@ static inline void emit_members(FILE *file, Decl **members, int indent) PRINT("\t\t\t\t\t]\n"); print_indent(file, indent); PRINTF("\t\t\t\t}"); - continue; } } diff --git a/src/compiler/lexer.c b/src/compiler/lexer.c index d75c93aa4..92eb237ce 100644 --- a/src/compiler/lexer.c +++ b/src/compiler/lexer.c @@ -331,13 +331,7 @@ static inline bool scan_ident(Lexer *lexer, TokenType normal, TokenType const_to hash = FNV1a(c, hash); next(lexer); } - // Allow bang! - if (peek(lexer) == '!' && type == normal) - { - hash = FNV1a('!', hash); - next(lexer); - } - EXIT:; +EXIT:; uint32_t len = (uint32_t)(lexer->current - lexer->lexing_start); if (!type) { @@ -712,12 +706,11 @@ static inline bool scan_char(Lexer *lexer) } int width = 0; - char c; Int128 b = { 0, 0 }; while (!match(lexer, '\'')) { - c = peek(lexer); + char c = peek(lexer); next(lexer); // End of file may occur: if (c == '\0') @@ -937,7 +930,6 @@ static inline bool scan_string(Lexer *lexer) { c = *current; if (c != '\n' && c != '\0') current++; - continue; } } const char *end = current - 1; @@ -1028,11 +1020,10 @@ static inline bool scan_hex_array(Lexer *lexer) { char start_char = peek(lexer); next(lexer); // Step past ' or " ` - char c; uint64_t len = 0; while (1) { - c = peek(lexer); + char c = peek(lexer); if (c == 0) { return add_error_token_at_current(lexer, "The hex string seems to be missing a terminating '%c'", start_char); @@ -1063,7 +1054,7 @@ static inline bool scan_hex_array(Lexer *lexer) { return add_error_token(lexer, "The hexadecimal string is not an even length, did you miss a digit somewhere?"); } - if (!new_token(lexer, TOKEN_BYTES, lexer->lexing_start)) return false; + new_token(lexer, TOKEN_BYTES, lexer->lexing_start); lexer->data.is_base64 = false; lexer->data.bytes_len = (uint64_t)len / 2; return true; @@ -1076,12 +1067,11 @@ static inline bool scan_base64(Lexer *lexer) next(lexer); // Step past 4 char start_char = peek(lexer); next(lexer); // Step past ' or " or ` - char c; unsigned end_len = 0; uint64_t len = 0; while (1) { - c = peek(lexer); + char c = peek(lexer); if (c == 0) { return add_error_token_at_start(lexer, "The base64 string seems to be missing a terminating '%c'", start_char); @@ -1143,7 +1133,7 @@ static inline bool scan_base64(Lexer *lexer) "- only need 1 or 2 bytes of extra padding."); } uint64_t decoded_len = (3 * len - end_len) / 4; - if (!new_token(lexer, TOKEN_BYTES, lexer->lexing_start)) return false; + new_token(lexer, TOKEN_BYTES, lexer->lexing_start); lexer->data.is_base64 = true; lexer->data.bytes_len = decoded_len; return true; @@ -1156,7 +1146,7 @@ static inline bool scan_base64(Lexer *lexer) /** * Parse the <* *> directives comments **/ -static bool parse_doc_start(Lexer *lexer) +static bool parse_doc_start(Lexer *lexer) // NOLINT { const char *comment_start = NULL; bool may_have_contract = true; @@ -1194,7 +1184,7 @@ static bool parse_doc_start(Lexer *lexer) comment_start = lexer->current; } next(lexer); - continue; + break; } } EXIT:; @@ -1222,7 +1212,7 @@ static bool lexer_scan_token_inner(Lexer *lexer) // Point start to the first non-whitespace character. begin_new_token(lexer); - if (reached_end(lexer)) return new_token(lexer, TOKEN_EOF, "\n") && false; + if (reached_end(lexer)) return new_token(lexer, TOKEN_EOF, "\n"), false; char c = peek(lexer); next(lexer); @@ -1363,7 +1353,7 @@ static bool lexer_scan_token_inner(Lexer *lexer) { return scan_base64(lexer); } - goto IDENT; + goto IDENT; // NOLINT case '_': IDENT: backtrack(lexer); @@ -1432,7 +1422,6 @@ INLINE void check_bidirectional_markers(Lexer *lexer) if (balance != 0) { add_error_token_at_start(lexer, "Invalid encoding - Unbalanced bidirectional markers."); - return; } } diff --git a/src/compiler/linker.c b/src/compiler/linker.c index 45164eb43..4f3a57af9 100644 --- a/src/compiler/linker.c +++ b/src/compiler/linker.c @@ -370,10 +370,7 @@ static void linker_setup_linux(const char ***args_ref, Linker linker_type, bool add_plain_arg("-nostdlib"); return; } - else - { - linking_add_link(&compiler.linking, "m"); - } + linking_add_link(&compiler.linking, "m"); if (compiler.build.debug_info == DEBUG_INFO_FULL) { add_plain_arg("-rdynamic"); @@ -917,7 +914,7 @@ const char *cc_compiler(const char *cc, const char *file, const char *flags, con } const char *out_name = dir ? str_printf("%s/%s%s", dir, filename, get_object_extension()) - : str_printf("%s%s", filename, get_object_extension());; + : str_printf("%s%s", filename, get_object_extension()); const char **parts = NULL; const char ***args_ref = &parts; add_quote_arg(cc); diff --git a/src/compiler/llvm_codegen.c b/src/compiler/llvm_codegen.c index 5c53825aa..3e7d49528 100644 --- a/src/compiler/llvm_codegen.c +++ b/src/compiler/llvm_codegen.c @@ -1,11 +1,10 @@ -// Copyright (c) 2019 Christoffer Lerno. All rights reserved. +// Copyright (c) 2019-2025 Christoffer Lerno. All rights reserved. // Use of this source code is governed by the GNU LGPLv3.0 license // a copy of which can be found in the LICENSE file. #include "llvm_codegen_internal.h" #include "compiler_tests/benchmark.h" #include "c3_llvm.h" -#include #include #include #include @@ -474,7 +473,7 @@ void llvm_set_weak(GenContext *c, LLVMValueRef global) llvm_set_comdat(c, global); } -static void llvm_set_external_reference(GenContext *c, LLVMValueRef ref, bool is_weak) +static void llvm_set_external_reference(LLVMValueRef ref, bool is_weak) { if (compiler.platform.os == OS_TYPE_WIN32) is_weak = false; LLVMSetLinkage(ref, is_weak ? LLVMExternalWeakLinkage : LLVMExternalLinkage); @@ -493,8 +492,8 @@ void llvm_set_decl_linkage(GenContext *c, Decl *decl) bool same_module = is_static || decl->unit->module == c->code_module; if (decl->is_extern || !same_module) { - llvm_set_external_reference(c, ref, should_weaken); - if (opt_ref) llvm_set_external_reference(c, opt_ref, should_weaken); + llvm_set_external_reference(ref, should_weaken); + if (opt_ref) llvm_set_external_reference(opt_ref, should_weaken); return; } if (decl_is_externally_visible(decl) && !is_static) @@ -959,9 +958,9 @@ LLVMBuilderRef llvm_create_function_entry(GenContext *c, LLVMValueRef func, LLVM return builder; } -LLVMBasicBlockRef llvm_append_basic_block(GenContext *c, LLVMValueRef function, const char *name) +LLVMBasicBlockRef llvm_append_basic_block(GenContext *c, LLVMValueRef func, const char *name) { - return LLVMAppendBasicBlockInContext(c->context, function, name); + return LLVMAppendBasicBlockInContext(c->context, func, name); } LLVMBasicBlockRef llvm_basic_block_new(GenContext *c, const char *name) @@ -1298,7 +1297,7 @@ LLVMValueRef llvm_get_ref(GenContext *c, Decl *decl) else { const char *name = scratch_buffer_to_string(); - if (decl->is_extern && (backend_ref = LLVMGetNamedFunction(c->module, name))) + if (decl->is_extern && (backend_ref = LLVMGetNamedFunction(c->module, name))) // NOLINT { return decl->backend_ref = backend_ref; } @@ -1364,10 +1363,9 @@ INLINE GenContext *llvm_gen_tests(Module** modules, unsigned module_count, LLVMC Module *module = modules[i]; FOREACH(Decl *, test, module->tests) { - LLVMValueRef ref; LLVMTypeRef type = opt_test; scratch_buffer_set_extern_decl_name(test, true); - ref = LLVMAddFunction(c->module, scratch_buffer_to_string(), type); + LLVMValueRef ref = LLVMAddFunction(c->module, scratch_buffer_to_string(), type); scratch_buffer_clear(); scratch_buffer_printf("%s::%s", module->name->module, test->name); LLVMValueRef name = llvm_emit_string_const(c, scratch_buffer_to_string(), ".test.name"); @@ -1433,10 +1431,9 @@ INLINE GenContext *llvm_gen_benchmarks(Module** modules, unsigned module_count, Module *module = modules[i]; FOREACH(Decl *, benchmark, module->benchmarks) { - LLVMValueRef ref; LLVMTypeRef type = opt_benchmark; scratch_buffer_set_extern_decl_name(benchmark, true); - ref = LLVMAddFunction(c->module, scratch_buffer_to_string(), type); + LLVMValueRef ref = LLVMAddFunction(c->module, scratch_buffer_to_string(), type); scratch_buffer_clear(); scratch_buffer_printf("%s::%s", module->name->module, benchmark->name); LLVMValueRef name = llvm_emit_string_const(c, scratch_buffer_to_string(), ".benchmark.name"); @@ -1488,8 +1485,6 @@ void **llvm_gen(Module** modules, unsigned module_count) llvm_codegen_setup(); if (compiler.build.single_module == SINGLE_MODULE_ON) { - GenContext *first_context; - unsigned first_element; LLVMContextRef context = LLVMGetGlobalContext(); for (int i = 0; i < module_count; i++) { @@ -1538,7 +1533,7 @@ LLVMMetadataRef llvm_get_debug_file(GenContext *c, FileId file_id) { FOREACH(DebugFile, file, c->debug.debug_files) { - if (file.file_id == file_id) return file.debug_file; + if (file.file_id == file_id) return file.debug_file; // NOLINT } File *f = source_file_by_id(file_id); LLVMMetadataRef file = LLVMDIBuilderCreateFile(c->debug.builder, @@ -1721,9 +1716,9 @@ void llvm_attribute_add_type(GenContext *c, LLVMValueRef value_to_add_attribute_ LLVMAddAttributeAtIndex(value_to_add_attribute_to, (LLVMAttributeIndex)index, llvm_attr); } -void llvm_attribute_add(GenContext *context, LLVMValueRef value_to_add_attribute_to, unsigned attribute, int index) +void llvm_attribute_add(GenContext *c, LLVMValueRef value_to_add_attribute_to, unsigned attribute, int index) { - llvm_attribute_add_int(context, value_to_add_attribute_to, attribute, 0, index); + llvm_attribute_add_int(c, value_to_add_attribute_to, attribute, 0, index); } void llvm_attribute_add_call_type(GenContext *c, LLVMValueRef call, unsigned attribute, int index, LLVMTypeRef type) @@ -1732,23 +1727,23 @@ void llvm_attribute_add_call_type(GenContext *c, LLVMValueRef call, unsigned att LLVMAddCallSiteAttribute(call, (LLVMAttributeIndex)index, llvm_attr); } -void llvm_attribute_add_call(GenContext *context, LLVMValueRef call, unsigned attribute, int index, int64_t value) +void llvm_attribute_add_call(GenContext *c, LLVMValueRef call, unsigned attribute, int index, int64_t value) { - LLVMAttributeRef llvm_attr = LLVMCreateEnumAttribute(context->context, attribute, (uint64_t)value); + LLVMAttributeRef llvm_attr = LLVMCreateEnumAttribute(c->context, attribute, (uint64_t)value); LLVMAddCallSiteAttribute(call, (LLVMAttributeIndex)index, llvm_attr); } -void llvm_attribute_add_range(GenContext *context, LLVMValueRef value_to_add_attribute_to, unsigned attribute, int index_start, int index_end) +void llvm_attribute_add_range(GenContext *c, LLVMValueRef value_to_add_attribute_to, unsigned attribute, int index_start, int index_end) { for (int i = index_start; i <= index_end; i++) { - llvm_attribute_add_int(context, value_to_add_attribute_to, attribute, 0, i); + llvm_attribute_add_int(c, value_to_add_attribute_to, attribute, 0, i); } } -void llvm_attribute_add_string(GenContext *context, LLVMValueRef value_to_add_attribute_to, const char *attribute, const char *value, int index) +void llvm_attribute_add_string(GenContext *c, LLVMValueRef value_to_add_attribute_to, const char *attribute, const char *value, int index) { - LLVMAttributeRef llvm_attr = LLVMCreateStringAttribute(context->context, attribute, (unsigned)strlen(attribute), value, (unsigned)strlen(value)); + LLVMAttributeRef llvm_attr = LLVMCreateStringAttribute(c->context, attribute, (unsigned)strlen(attribute), value, (unsigned)strlen(value)); LLVMAddAttributeAtIndex(value_to_add_attribute_to, (LLVMAttributeIndex)index, llvm_attr); } diff --git a/src/compiler/llvm_codegen_builtins.c b/src/compiler/llvm_codegen_builtins.c index 17c5ff30c..71b87feda 100644 --- a/src/compiler/llvm_codegen_builtins.c +++ b/src/compiler/llvm_codegen_builtins.c @@ -39,12 +39,10 @@ INLINE void llvm_emit_swizzle(GenContext *c, BEValue *result_value, Expr *expr, { Expr **args = expr->call_expr.arguments; unsigned count = vec_size(args); - LLVMValueRef arg1; LLVMValueRef arg2; - LLVMValueRef mask; llvm_emit_expr(c, result_value, args[0]); llvm_value_rvalue(c, result_value); - arg1 = result_value->value; + LLVMValueRef arg1 = result_value->value; unsigned mask_start = 1; if (swizzle_two) { @@ -73,7 +71,6 @@ INLINE void llvm_emit_swizzle(GenContext *c, BEValue *result_value, Expr *expr, } LLVMValueRef val = LLVMBuildShuffleVector(c->builder, arg1, arg2, LLVMConstVector(mask_val, mask_len), "shuffle"); llvm_value_set(result_value, val, expr->type); - return; } static LLVMAtomicOrdering ordering_to_llvm(int value) @@ -121,7 +118,7 @@ INLINE void llvm_emit_compare_exchange(GenContext *c, BEValue *result_value, Exp llvm_value_set(result_value, llvm_emit_extract_value(c, result, 0), type); } -INLINE void llvm_emit_unreachable_stmt(GenContext *c, BEValue *result_value, Expr *expr) +INLINE void llvm_emit_unreachable_stmt(GenContext *c, BEValue *result_value) { llvm_value_set(result_value, LLVMBuildUnreachable(c->builder), type_void); c->current_block = NULL; @@ -180,8 +177,8 @@ INLINE void llvm_emit_atomic_fetch(GenContext *c, BuiltinFunction func, BEValue bool is_float = type_is_float(result_value->type); bool is_unsigned = !is_float && type_is_unsigned(result_value->type); LLVMAtomicRMWBinOp op; - static LLVMAtomicRMWBinOp LLVMAtomicRMWBinOpUIncWrap = LLVMAtomicRMWBinOpFMin + 1; - static LLVMAtomicRMWBinOp LLVMAtomicRMWBinOpUDecWrap = LLVMAtomicRMWBinOpFMin + 2; + static LLVMAtomicRMWBinOp LLVMAtomicRMWBinOpUIncWrap = LLVMAtomicRMWBinOpFMin + 1; // NOLINT + static LLVMAtomicRMWBinOp LLVMAtomicRMWBinOpUDecWrap = LLVMAtomicRMWBinOpFMin + 2; // NOLINT switch (func) { case BUILTIN_ATOMIC_FETCH_EXCHANGE: @@ -250,7 +247,7 @@ INLINE void llvm_emit_unaligned_load(GenContext *c, BEValue *result_value, Expr llvm_value_rvalue(c, result_value); } -static inline LLVMValueRef llvm_syscall_asm(GenContext *c, LLVMTypeRef func_type, char *call) +static inline LLVMValueRef llvm_syscall_asm(LLVMTypeRef func_type, char *call) { return LLVMGetInlineAsm(func_type, call, strlen(call), scratch_buffer_to_string(), scratch_buffer.len, @@ -301,7 +298,7 @@ static inline void llvm_emit_syscall(GenContext *c, BEValue *be_value, Expr *exp static char const *regs[] = { "x8", "x0", "x1", "x2", "x3", "x4", "x5" }; llvm_syscall_write_regs_to_scratch(regs, arguments); } - inline_asm = llvm_syscall_asm(c, func_type, "svc #0x80"); + inline_asm = llvm_syscall_asm(func_type, "svc #0x80"); break; case ARCH_TYPE_X86: { @@ -313,10 +310,10 @@ static inline void llvm_emit_syscall(GenContext *c, BEValue *be_value, Expr *exp { scratch_buffer_append(",rm"); char *asm_str = "push %[arg6]\npush %%ebp\nmov 4(%%esp), %%ebp\nint $0x80\npop %%ebp\nadd $4, %%esp"; - inline_asm = llvm_syscall_asm(c, func_type, asm_str); + inline_asm = llvm_syscall_asm(func_type, asm_str); break; } - inline_asm = llvm_syscall_asm(c, func_type, "int $0x80"); + inline_asm = llvm_syscall_asm(func_type, "int $0x80"); break; } case ARCH_TYPE_X86_64: @@ -328,7 +325,7 @@ static inline void llvm_emit_syscall(GenContext *c, BEValue *be_value, Expr *exp } // Check clobbers on different OSes scratch_buffer_append(",~{rcx},~{r11},~{memory}"); - inline_asm = llvm_syscall_asm(c, func_type, "syscall"); + inline_asm = llvm_syscall_asm(func_type, "syscall"); break; case ARCH_UNSUPPORTED: default: @@ -455,7 +452,7 @@ void llvm_emit_int_with_bool_builtin(GenContext *c, unsigned intrinsic, BEValue Expr **args = expr->call_expr.arguments; LLVMValueRef arg_slots[2]; llvm_emit_intrinsic_args(c, args, arg_slots, 1); - arg_slots[1] = llvm_get_zero_raw(c->bool_type); + arg_slots[1] = LLVMConstInt(c->bool_type, bool_val ? 1 : 0, false); LLVMTypeRef call_type[1] = { LLVMTypeOf(arg_slots[0]) }; LLVMValueRef result = llvm_emit_call_intrinsic(c, intrinsic, call_type, 1, arg_slots, 2); llvm_value_set(be_value, result, expr->type); @@ -479,7 +476,7 @@ void llvm_emit_3_variant_builtin(GenContext *c, BEValue *be_value, Expr *expr, u LLVMValueRef arg_slots[3]; unsigned intrinsic = llvm_intrinsic_by_type(args[0]->type, sid, uid, fid); llvm_emit_intrinsic_args(c, args, arg_slots, count); - LLVMTypeRef call_type[1] = { LLVMTypeOf(arg_slots[0]) }; + LLVMTypeRef call_type[1] = { LLVMTypeOf(arg_slots[0]) }; // NOLINT LLVMValueRef result = llvm_emit_call_intrinsic(c, intrinsic, call_type, 1, arg_slots, count); llvm_value_set(be_value, result, expr->type); } @@ -639,7 +636,6 @@ static void llvm_emit_wrap_builtin(GenContext *c, BEValue *result_value, Expr *e Type *base_type = type_lowering(args[0]->type); if (base_type->type_kind == TYPE_VECTOR) base_type = base_type->array.base; ASSERT(type_is_integer(base_type)); - bool is_signed = type_is_signed(base_type); LLVMValueRef res; switch (func) { @@ -685,15 +681,13 @@ static void llvm_emit_wrap_builtin(GenContext *c, BEValue *result_value, Expr *e void llvm_emit_builtin_call(GenContext *c, BEValue *result_value, Expr *expr) { BuiltinFunction func = exprptr(expr->call_expr.function)->builtin_expr.builtin; - unsigned intrinsic; - LLVMValueRef val = NULL; switch (func) { case BUILTIN_ANY_MAKE: // Folded in the frontend. UNREACHABLE case BUILTIN_UNREACHABLE: - llvm_emit_unreachable_stmt(c, result_value, expr); + llvm_emit_unreachable_stmt(c, result_value); return; case BUILTIN_SWIZZLE: llvm_emit_swizzle(c, result_value, expr, false); @@ -708,7 +702,7 @@ void llvm_emit_builtin_call(GenContext *c, BEValue *result_value, Expr *expr) { llvm_emit_expr(c, result_value, expr->call_expr.arguments[0]); llvm_value_rvalue(c, result_value); - LLVMValueRef res = llvm_emit_call_intrinsic(c, func == BUILTIN_FRAMEADDRESS ? intrinsic_id.frameaddress : intrinsic_id.returnaddress, &c->ptr_type, 1, &result_value->value, 1); + LLVMValueRef res = llvm_emit_call_intrinsic(c, intrinsic_id.frameaddress, &c->ptr_type, 1, &result_value->value, 1); llvm_value_set(result_value, res, expr->type); return; } @@ -716,8 +710,7 @@ void llvm_emit_builtin_call(GenContext *c, BEValue *result_value, Expr *expr) { llvm_emit_expr(c, result_value, expr->call_expr.arguments[0]); llvm_value_rvalue(c, result_value); - LLVMTypeRef type = LLVMTypeOf(result_value->value); - LLVMValueRef res = llvm_emit_call_intrinsic(c, func == BUILTIN_FRAMEADDRESS ? intrinsic_id.frameaddress : intrinsic_id.returnaddress, NULL, 0, &result_value->value, 1); + LLVMValueRef res = llvm_emit_call_intrinsic(c, intrinsic_id.returnaddress, NULL, 0, &result_value->value, 1); llvm_value_set(result_value, res, expr->type); return; } diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 6eb52a573..a0ecbb9c6 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -6,7 +6,7 @@ #include -static LLVMValueRef llvm_emit_coerce_alignment(GenContext *c, BEValue *be_value, LLVMTypeRef coerce_type, AlignSize target_alignment, AlignSize *resulting_alignment); +static LLVMValueRef llvm_emit_coerce_alignment(GenContext *c, BEValue *be_value, AlignSize target_alignment, AlignSize *resulting_alignment); static bool bitstruct_requires_bitswap(Decl *decl); static inline LLVMValueRef llvm_const_high_bitmask(GenContext *c, LLVMTypeRef type, int type_bits, int high_bits); static inline LLVMValueRef llvm_const_low_bitmask(GenContext *c, LLVMTypeRef type, int type_bits, int low_bits); @@ -239,7 +239,7 @@ static void llvm_convert_vector_comparison(GenContext *c, BEValue *be_value, LLV llvm_value_set(be_value, result, type_bool); } -static LLVMValueRef llvm_emit_coerce_alignment(GenContext *c, BEValue *be_value, LLVMTypeRef coerce_type, AlignSize target_alignment, AlignSize *resulting_alignment) +static LLVMValueRef llvm_emit_coerce_alignment(GenContext *c, BEValue *be_value, AlignSize target_alignment, AlignSize *resulting_alignment) { // If we are loading something with greater alignment than what we have, we cannot directly memcpy. if (!llvm_value_is_addr(be_value) || be_value->alignment < target_alignment) @@ -697,7 +697,6 @@ static inline void llvm_emit_subscript_addr(GenContext *c, BEValue *value, Expr Expr *parent_expr = exprptr(expr->subscript_expr.expr); Expr *index_expr = exprptr(expr->subscript_expr.index.expr); Type *parent_type = type_lowering(parent_expr->type); - BEValue ref; // First, get thing being subscripted. llvm_emit_expr(c, value, parent_expr); BEValue len = { .value = NULL }; @@ -924,7 +923,6 @@ static inline void llvm_extract_bitvalue_from_array(GenContext *c, BEValue *be_v unsigned end = member->var.end_bit; LLVMValueRef array_ptr = be_value->value; LLVMTypeRef array_type = llvm_get_type(c, type_char); - LLVMValueRef result = NULL; int start_byte = start / 8; int end_byte = end / 8; Type *member_type = type_lowering(member->type); @@ -1346,85 +1344,8 @@ void llvm_emit_ignored_expr(GenContext *c, Expr *expr) } -static LLVMValueRef llvm_emit_char_array_zero(GenContext *c, BEValue *value, bool find_zero) -{ - llvm_value_addr(c, value); - unsigned len = type_size(value->type); - ASSERT(len > 0); - LLVMValueRef total = NULL; - for (int i = 0; i < len; i++) - { - LLVMValueRef ref = llvm_emit_const_ptradd_inbounds_raw(c, value->value, i); - LLVMValueRef val = llvm_zext_trunc(c, llvm_load(c, c->byte_type, ref, 1, ""), llvm_get_type(c, type_cint)); - total = total ? LLVMBuildAdd(c->builder, total, val, "") : val; - } - return LLVMBuildICmp(c->builder, find_zero ? LLVMIntEQ : LLVMIntNE, total, llvm_get_zero(c, type_cint), ""); -} -static void llvm_emit_bitstruct_to_bool(GenContext *c, BEValue *value, Type *to_type, Type *from_type) -{ - Type *base_type = type_flatten(from_type->decl->strukt.container_type->type); - if (base_type->type_kind != TYPE_ARRAY) - { - llvm_emit_int_comp_zero(c, value, value, BINARYOP_NE); - return; - } - llvm_value_set(value, llvm_emit_char_array_zero(c, value, false), to_type); -} - -static LLVMValueRef llvm_recursive_set_value(GenContext *c, DesignatorElement **current_element_ptr, LLVMValueRef parent, DesignatorElement **last_element_ptr, Expr *value) -{ - DesignatorElement *current_element = current_element_ptr[0]; - if (current_element_ptr == last_element_ptr) - { - BEValue res; - llvm_emit_expr(c, &res, value); - ArraySize index = (ArraySize)current_element->index; - LLVMValueRef val = llvm_load_value_store(c, &res); - switch (current_element->kind) - { - case DESIGNATOR_FIELD: - case DESIGNATOR_ARRAY: - return llvm_emit_insert_value(c, parent, val, index); - case DESIGNATOR_RANGE: - for (ArrayIndex i = current_element->index; i <= current_element->index_end; i++) - { - parent = llvm_emit_insert_value(c, parent, val, i); - } - return parent; - } - UNREACHABLE - } - LLVMValueRef current_val; - switch (current_element->kind) - { - case DESIGNATOR_FIELD: - { - unsigned index = (unsigned)current_element->index; - current_val = llvm_emit_extract_value(c, parent, index); - current_val = llvm_recursive_set_value(c, current_element_ptr + 1, current_val, last_element_ptr, value); - return llvm_emit_insert_value(c, parent, current_val, index); - } - case DESIGNATOR_ARRAY: - current_val = llvm_emit_extract_value(c, parent, current_element->index); - current_val = llvm_recursive_set_value(c, current_element_ptr + 1, current_val, last_element_ptr, value); - return llvm_emit_insert_value(c, parent, current_val, current_element->index); - case DESIGNATOR_RANGE: - for (ArrayIndex i = current_element->index; i <= current_element->index_end; i++) - { - current_val = llvm_emit_extract_value(c, parent, i); - current_val = llvm_recursive_set_value(c, current_element_ptr + 1, current_val, last_element_ptr, value); - parent = llvm_emit_insert_value(c, parent, current_val, i); - } - return parent; - default: - UNREACHABLE - } -} - - void llvm_emit_initialize_reference_temporary_const(GenContext *c, BEValue *ref, ConstInitializer *initializer) { - bool modified = false; // First create the constant value. LLVMValueRef value = llvm_emit_const_initializer(c, initializer); @@ -1527,8 +1448,6 @@ static void llvm_emit_const_init_ref(GenContext *c, BEValue *ref, ConstInitializ 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; - LLVMValueRef *parts = NULL; FOREACH(ConstInitializer *, element, elements) { ASSERT(element->kind == CONST_INIT_ARRAY_VALUE); @@ -1666,7 +1585,6 @@ static inline void llvm_emit_initialize_reference_list(GenContext *c, BEValue *r bool is_struct = type_is_union_or_strukt(real_type); bool is_array = real_type->type_kind == TYPE_ARRAY; - bool is_vector = real_type->type_kind == TYPE_VECTOR; // Now walk through the elements. FOREACH_IDX(i, Expr *, element, elements) { @@ -2416,7 +2334,6 @@ static void llvm_emit_dynamic_method_addr(GenContext *c, BEValue *value, Expr *e llvm_value_rvalue(c, value); LLVMValueRef introspect = LLVMBuildIntToPtr(c->builder, value->value, c->ptr_type, ""); - AlignSize align; Decl *dyn_fn = expr->access_resolved_expr.ref; LLVMValueRef func = llvm_emit_dynamic_search(c, introspect, llvm_get_ref(c, dyn_fn)); @@ -2427,7 +2344,7 @@ static void llvm_emit_unary_expr(GenContext *c, BEValue *value, Expr *expr) { Type *type = type_reduced_from_expr(expr->unary_expr.expr); Expr *inner = expr->unary_expr.expr; - LLVMValueRef llvm_value; + switch (expr->unary_expr.operator) { case UNARYOP_ERROR: @@ -2441,6 +2358,7 @@ static void llvm_emit_unary_expr(GenContext *c, BEValue *value, Expr *expr) { llvm_value_rvalue(c, value); Type *vec_type = type_vector_type(type); + LLVMValueRef llvm_value; if (type_is_float(vec_type)) { llvm_value = LLVMBuildFCmp(c->builder, LLVMRealUEQ, value->value, llvm_get_zero(c, type), "not"); @@ -2625,7 +2543,6 @@ static void llvm_emit_slice_values(GenContext *c, Expr *slice, BEValue *parent_r LLVMValueRef parent_addr = parent_addr_x.value; LLVMValueRef parent_load_value = NULL; LLVMValueRef parent_base; - bool is_optional = type_is_optional(parent_type); parent_type = type_no_optional(parent_type); switch (parent_type->type_kind) { @@ -3000,13 +2917,11 @@ static void llvm_emit_slice_assign(GenContext *c, BEValue *be_value, Expr *expr) static void llvm_emit_logical_and_or(GenContext *c, BEValue *be_value, Expr *expr, BinaryOp op) { - LLVMBasicBlockRef lhs_end_block; - // Generate left-hand condition and conditional branch llvm_emit_expr(c, be_value, exprptr(expr->binary_expr.left)); llvm_value_rvalue(c, be_value); - lhs_end_block = llvm_get_current_block_if_in_use(c); + LLVMBasicBlockRef lhs_end_block = llvm_get_current_block_if_in_use(c); LLVMValueRef result_on_skip = LLVMConstInt(c->bool_type, op == BINARYOP_AND ? 0 : 1, 0); @@ -3312,8 +3227,6 @@ static void llvm_emit_any_comparison(GenContext *c, BEValue *result, BEValue *lh LLVMValueRef val; LLVMValueRef val2; LLVMValueRef res; - LLVMIntPredicate comparison; - const char *desc; switch (binary_op) { case BINARYOP_EQ: @@ -3377,9 +3290,6 @@ static void llvm_emit_slice_comp(GenContext *c, BEValue *be_value, BEValue *lhs, LLVMBasicBlockRef value_cmp = llvm_basic_block_new(c, "slice_cmp_values"); LLVMBasicBlockRef loop_begin = llvm_basic_block_new(c, "slice_loop_start"); LLVMBasicBlockRef comparison = llvm_basic_block_new(c, "slice_loop_comparison"); - LLVMBasicBlockRef no_match_block; - LLVMBasicBlockRef all_match_block; - LLVMBasicBlockRef match_fail_block; llvm_value_rvalue(c, lhs); llvm_value_rvalue(c, rhs); @@ -3394,7 +3304,7 @@ static void llvm_emit_slice_comp(GenContext *c, BEValue *be_value, BEValue *lhs, BEValue len_match; llvm_emit_comp(c, &len_match, &lhs_len, &rhs_len, BINARYOP_EQ); - no_match_block = c->current_block; + LLVMBasicBlockRef no_match_block = c->current_block; llvm_emit_cond_br(c, &len_match, value_cmp, exit); llvm_emit_block(c, value_cmp); @@ -3409,7 +3319,7 @@ static void llvm_emit_slice_comp(GenContext *c, BEValue *be_value, BEValue *lhs, llvm_value_rvalue(c, ¤t_index); BEValue cmp; llvm_emit_comp(c, &cmp, ¤t_index, &lhs_len, BINARYOP_LT); - all_match_block = c->current_block; + LLVMBasicBlockRef all_match_block = c->current_block; llvm_emit_cond_br(c, &cmp, comparison, exit); llvm_emit_block(c, comparison); @@ -3428,7 +3338,7 @@ static void llvm_emit_slice_comp(GenContext *c, BEValue *be_value, BEValue *lhs, current_index.value), array_base_type); llvm_emit_comp(c, &cmp, &lhs_to_compare, &rhs_to_compare, BINARYOP_EQ); - match_fail_block = c->current_block; + LLVMBasicBlockRef match_fail_block = c->current_block; llvm_store_raw(c, &index_var, LLVMBuildAdd(c->builder, current_index.value, one, "")); llvm_emit_cond_br(c, &cmp, loop_begin, exit); llvm_emit_block(c, exit); @@ -3625,8 +3535,7 @@ MEMCMP: LLVMBasicBlockRef exit = llvm_basic_block_new(c, "array_cmp_exit"); LLVMBasicBlockRef loop_begin = llvm_basic_block_new(c, "array_loop_start"); LLVMBasicBlockRef comparison = llvm_basic_block_new(c, "array_loop_comparison"); - LLVMBasicBlockRef comparison_phi; - LLVMBasicBlockRef loop_begin_phi; + LLVMValueRef len_val = llvm_const_int(c, type_isz, len); LLVMValueRef one = llvm_const_int(c, type_isz, 1); BEValue index_var; @@ -3648,14 +3557,14 @@ MEMCMP: llvm_value_set_address(&rhs_v, rhs_ptr, array_base_type, align_rhs); BEValue comp; llvm_emit_comp(c, &comp, &lhs_v, &rhs_v, BINARYOP_EQ); - loop_begin_phi = c->current_block; + LLVMBasicBlockRef loop_begin_phi = c->current_block; llvm_emit_cond_br(c, &comp, comparison, exit); llvm_emit_block(c, comparison); LLVMValueRef new_index = LLVMBuildAdd(c->builder, index_copy.value, one, "inc"); llvm_store_raw(c, &index_var, new_index); llvm_emit_int_comp_raw(c, &comp, type_isz, type_isz, new_index, len_val, BINARYOP_LT); - comparison_phi = c->current_block; + LLVMBasicBlockRef comparison_phi = c->current_block; llvm_emit_cond_br(c, &comp, loop_begin, exit); llvm_emit_block(c, exit); LLVMValueRef success = LLVMConstInt(c->bool_type, want_match ? 1 : 0, false); @@ -3800,7 +3709,7 @@ static void llvm_emit_else(GenContext *c, BEValue *be_value, Expr *expr) llvm_emit_exprid(c, &real_value, expr->binary_expr.left); bool was_address = llvm_value_is_addr(&real_value); llvm_value_fold_optional(c, &real_value); - if (was_address && !llvm_temp_as_address(c, real_value.type)) + if (was_address && !llvm_temp_as_address(real_value.type)) { was_address = false; llvm_value_rvalue(c, &real_value); @@ -4244,9 +4153,7 @@ static void llvm_emit_post_unary_expr(GenContext *context, BEValue *be_value, Ex void llvm_emit_typeid(GenContext *c, BEValue *be_value, Type *type) { - LLVMValueRef value; - type = type->canonical; - llvm_value_set(be_value, llvm_get_typeid(c, type), type_typeid); + llvm_value_set(be_value, llvm_get_typeid(c, type->canonical), type_typeid); } void llvm_emit_try_assign_try_catch(GenContext *c, bool is_try, BEValue *be_value, BEValue *var_addr, BEValue *catch_addr, Expr *rhs) @@ -4519,7 +4426,6 @@ void gencontext_emit_ternary_expr(GenContext *c, BEValue *value, Expr *expr) LLVMBasicBlockRef phi_block = llvm_basic_block_new(c, "cond.phi"); LLVMBasicBlockRef rhs_block = llvm_basic_block_new(c, "cond.rhs"); - LLVMBasicBlockRef lhs_exit; LLVMBasicBlockRef lhs_block = llvm_basic_block_new(c, "cond.lhs"); llvm_emit_cond_br(c, value, lhs_block, rhs_block); llvm_emit_block(c, lhs_block); @@ -4527,7 +4433,7 @@ void gencontext_emit_ternary_expr(GenContext *c, BEValue *value, Expr *expr) llvm_emit_expr(c, &lhs, then_expr); llvm_value_rvalue(c, &lhs); LLVMValueRef lhs_value = lhs.value; - lhs_exit = llvm_get_current_block_if_in_use(c); + LLVMBasicBlockRef lhs_exit = llvm_get_current_block_if_in_use(c); if (lhs.type == type_bool && LLVMTypeOf(lhs_value) != c->bool_type) { llvm_emit_trunc_bool(c, lhs_value); @@ -5065,7 +4971,7 @@ void llvm_emit_parameter(GenContext *c, LLVMValueRef *args, unsigned *arg_count_ AlignSize target_alignment = llvm_abi_alignment(c, coerce_type); AlignSize alignment; - LLVMValueRef cast = llvm_emit_coerce_alignment(c, be_value, coerce_type, target_alignment, &alignment); + LLVMValueRef cast = llvm_emit_coerce_alignment(c, be_value, target_alignment, &alignment); LLVMTypeRef element = llvm_get_type(c, type_uint); for (unsigned idx = 0; idx < info->direct_struct_expand; idx++) { @@ -5151,7 +5057,7 @@ void llvm_emit_parameter(GenContext *c, LLVMValueRef *args, unsigned *arg_count_ LLVMTypeRef hi = llvm_abi_type(c, info->direct_pair.hi); AlignSize struct_align; - LLVMValueRef cast = llvm_emit_coerce_alignment(c, be_value, struct_type, llvm_abi_alignment(c, struct_type), &struct_align); + LLVMValueRef cast = llvm_emit_coerce_alignment(c, be_value, llvm_abi_alignment(c, struct_type), &struct_align); // Get the lo value. LLVMValueRef lo_ptr = llvm_emit_struct_gep_raw(c, cast, struct_type, 0, struct_align, &alignment); @@ -5363,8 +5269,6 @@ void llvm_emit_raw_call(GenContext *c, BEValue *result_value, FunctionPrototype } if (prototype->is_optional) { - BEValue no_err; - // 17a. If we used the error var as the indirect recipient, then that will hold the error. // otherwise it's whatever value in be_value. BEValue error_holder = *result_value; @@ -5420,7 +5324,6 @@ static LLVMValueRef llvm_emit_dynamic_search(GenContext *c, LLVMValueRef type_id LLVMBasicBlockRef entry; LLVMBuilderRef builder = llvm_create_function_entry(c, func, &entry); - AlignSize align; LLVMValueRef dtable_ptr_in = LLVMGetParam(func, 0); LLVMValueRef func_ref = LLVMGetParam(func, 1); @@ -5635,8 +5538,6 @@ INLINE void llvm_emit_call_invocation(GenContext *c, BEValue *result_value, llvm_value_set_address_abi_aligned(&synthetic_return_param, synthetic_return_param.value, actual_return_type); } - BEValue temp_value; - // 8. Add all other arguments. for (unsigned i = 0; i < param_count; i++) { @@ -5879,7 +5780,6 @@ static void llvm_emit_call_expr(GenContext *c, BEValue *result_value, Expr *expr LLVMBasicBlockRef missing_function = llvm_basic_block_new(c, "missing_function"); LLVMBasicBlockRef match = llvm_basic_block_new(c, "match"); - AlignSize align; Decl *dyn_fn = declptr(expr->call_expr.func_ref); prototype = type_get_resolved_prototype(dyn_fn->type); func_type = llvm_get_type(c, dyn_fn->type); @@ -5978,7 +5878,6 @@ static inline void llvm_emit_return_block(GenContext *c, BEValue *be_value, Type LLVMValueRef old_ret_out = c->return_out; LLVMValueRef error_out = c->catch.fault; LLVMBasicBlockRef error_block = c->catch.block; - LLVMValueRef return_out = NULL; LLVMBasicBlockRef expr_block = llvm_basic_block_new(c, "expr_block.exit"); LLVMBasicBlockRef cleanup_error_block = error_block; BlockExit exit = { @@ -6079,7 +5978,7 @@ DONE: c->return_out = old_ret_out; c->catch.block = error_block; - c->catch.fault = error_out; + c->catch.fault = error_out; // NOLINT } @@ -6614,7 +6513,6 @@ static inline void llvm_emit_builtin_access(GenContext *c, BEValue *be_value, Ex } ASSERT(type_flatten(inner->type)->type_kind == TYPE_FAULTTYPE); llvm_value_rvalue(c, be_value); - BEValue zero; LLVMBasicBlockRef exit_block = llvm_basic_block_new(c, "faultordinal_exit"); LLVMBasicBlockRef ok_block = llvm_basic_block_new(c, "faultordinal_found"); BEValue check; @@ -6920,7 +6818,6 @@ void llvm_emit_enum_from_ord(GenContext *c, BEValue *value, Expr *expr) scratch_buffer_clear(); scratch_buffer_printf("Attempt to convert a negative value (%%d) to enum '%s' failed.", decl->name); llvm_emit_int_comp_zero(c, &check, value, BINARYOP_LT); - BEValue val; llvm_emit_panic_on_true(c, check.value, "Attempt to convert negative value to enum failed.", expr->span, scratch_buffer_copy(), value, NULL); } scratch_buffer_clear(); diff --git a/src/compiler/llvm_codegen_function.c b/src/compiler/llvm_codegen_function.c index bed422107..8ba48e253 100644 --- a/src/compiler/llvm_codegen_function.c +++ b/src/compiler/llvm_codegen_function.c @@ -53,7 +53,7 @@ bool llvm_emit_check_block_branch(GenContext *c) return false; } return true; -}; +} void llvm_emit_br(GenContext *c, LLVMBasicBlockRef next_block) { @@ -84,7 +84,7 @@ static void llvm_expand_from_args(GenContext *c, Type *type, LLVMValueRef ref, u LLVMValueRef target = llvm_emit_array_gep_raw(c, ref, array_type, i, alignment, &element_align); llvm_expand_from_args(c, type->array.base, target, index, element_align); } - return; + break; } case TYPE_STRUCT: { @@ -95,18 +95,18 @@ static void llvm_expand_from_args(GenContext *c, Type *type, LLVMValueRef ref, u LLVMValueRef target = llvm_emit_struct_gep_raw(c, ref, struct_type, i, alignment, &element_align); llvm_expand_from_args(c, member->type, target, index, element_align); } - return; + break; } case TYPE_UNION: { Type *largest_type = type_find_largest_union_element(type); // COERCE UPDATE bitcast removed, check for ways to optimize llvm_expand_from_args(c, largest_type, ref, index, alignment); - return; + break; } default: llvm_store_to_ptr_raw_aligned(c, ref, llvm_get_next_param(c, index), alignment); - return; + break; } } @@ -338,6 +338,7 @@ void llvm_emit_return_abi(GenContext *c, BEValue *return_value, BEValue *optiona case ABI_ARG_EXPAND_COERCE: { // Pick the return as an address. + assert(return_value); llvm_value_addr(c, return_value); LLVMValueRef addr = return_value->value; AlignSize align = return_value->alignment; @@ -352,7 +353,7 @@ void llvm_emit_return_abi(GenContext *c, BEValue *return_value, BEValue *optiona // And return that unpadded result llvm_emit_return_value(c, composite); - break; + return; } case ABI_ARG_DIRECT: DIRECT_RETURN: @@ -381,6 +382,7 @@ DIRECT_RETURN: return; } } + UNREACHABLE } void llvm_emit_return_implicit(GenContext *c) @@ -468,7 +470,7 @@ void llvm_emit_body(GenContext *c, LLVMValueRef function, FunctionPrototype *pro if (emit_debug) { scope = (DebugScope) { .lexical_block = c->debug.function, NULL, NULL }; - c->debug.block_stack = &scope; + c->debug.block_stack = &scope; // NOLINT EMIT_LOC(c, body); } diff --git a/src/compiler/llvm_codegen_internal.h b/src/compiler/llvm_codegen_internal.h index 76a89fa87..febedf2f0 100644 --- a/src/compiler/llvm_codegen_internal.h +++ b/src/compiler/llvm_codegen_internal.h @@ -7,10 +7,8 @@ #include "codegen_internal.h" #include #include -#include #include #include -#include #include #include "dwarf.h" #include "c3_llvm.h" @@ -325,7 +323,7 @@ INLINE bool llvm_is_local_eval(GenContext *c); // -- BE value -- void llvm_value_addr(GenContext *c, BEValue *value); bool llvm_value_is_const(BEValue *value); -void llvm_value_rvalue(GenContext *context, BEValue *value); +void llvm_value_rvalue(GenContext *c, BEValue *value); void llvm_value_deref(GenContext *c, BEValue *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); @@ -341,7 +339,7 @@ INLINE void llvm_value_bitcast(GenContext *c, BEValue *value, Type *type); void llvm_value_aggregate_two(GenContext *c, BEValue *value, Type *type, LLVMValueRef value1, LLVMValueRef value2); // -- Types -- -LLVMValueRef llvm_get_typeid(GenContext *context, Type *type); +LLVMValueRef llvm_get_typeid(GenContext *c, Type *type); LLVMTypeRef llvm_abi_type(GenContext *c, AbiType type); TypeSize llvm_abi_size(GenContext *c, LLVMTypeRef type); BitSize llvm_bitsize(GenContext *c, LLVMTypeRef type); @@ -429,9 +427,9 @@ LLVMBasicBlockRef llvm_basic_block_new(GenContext *c, const char *name); LLVMBuilderRef llvm_create_function_entry(GenContext *c, LLVMValueRef func, LLVMBasicBlockRef *entry_block_ref); LLVMBasicBlockRef llvm_append_basic_block(GenContext *c, LLVMValueRef func, const char *name); void llvm_emit_block(GenContext *c, LLVMBasicBlockRef next_block); -static inline LLVMBasicBlockRef llvm_get_current_block_if_in_use(GenContext *context); +static inline LLVMBasicBlockRef llvm_get_current_block_if_in_use(GenContext *c); INLINE bool llvm_basic_block_is_unused(LLVMBasicBlockRef block); -bool llvm_emit_check_block_branch(GenContext *context); +bool llvm_emit_check_block_branch(GenContext *c); // -- Comparisons --- void llvm_emit_lhs_is_subtype(GenContext *c, BEValue *result, BEValue *lhs, BEValue *rhs); @@ -457,13 +455,13 @@ INLINE LLVMValueRef llvm_store_raw(GenContext *c, BEValue *destination, LLVMValu INLINE LLVMValueRef llvm_store_decl(GenContext *c, Decl *decl, BEValue *value); INLINE LLVMValueRef llvm_store_decl_raw(GenContext *context, Decl *decl, LLVMValueRef value); INLINE LLVMValueRef llvm_store_to_ptr(GenContext *c, LLVMValueRef destination, BEValue *value); -INLINE LLVMValueRef llvm_store_to_ptr_raw(GenContext *context, LLVMValueRef pointer, LLVMValueRef value, Type *type); +INLINE LLVMValueRef llvm_store_to_ptr_raw(GenContext *c, LLVMValueRef pointer, LLVMValueRef value, Type *type); LLVMValueRef llvm_store_to_ptr_aligned(GenContext *c, LLVMValueRef destination, BEValue *value, AlignSize alignment); LLVMValueRef llvm_store_to_ptr_raw_aligned(GenContext *context, LLVMValueRef pointer, LLVMValueRef value, AlignSize alignment); void llvm_store_to_ptr_zero(GenContext *context, LLVMValueRef pointer, Type *type); TypeSize llvm_store_size(GenContext *c, LLVMTypeRef type); TypeSize llvm_alloc_size(GenContext *c, LLVMTypeRef type); -bool llvm_temp_as_address(GenContext *c, Type *type); +bool llvm_temp_as_address(Type *type); /// -- Aggregates -- INLINE LLVMValueRef llvm_emit_insert_value(GenContext *c, LLVMValueRef agg, LLVMValueRef new_value, ArraySize index); @@ -535,8 +533,8 @@ void llvm_emit_slice_len(GenContext *c, BEValue *slice, BEValue *len); void llvm_emit_slice_pointer(GenContext *c, BEValue *slice, BEValue *pointer); void llvm_emit_compound_stmt(GenContext *c, Ast *ast); LLVMValueRef llvm_emit_const_bitstruct(GenContext *c, ConstInitializer *initializer); -void llvm_emit_function_body(GenContext *context, Decl *decl); -void llvm_emit_dynamic_functions(GenContext *context, Decl **funcs); +void llvm_emit_function_body(GenContext *c, Decl *decl); +void llvm_emit_dynamic_functions(GenContext *c, Decl **funcs); BEValue llvm_emit_assign_expr(GenContext *c, BEValue *ref, Expr *expr, LLVMValueRef optional, bool is_init); INLINE void llvm_emit_exprid(GenContext *c, BEValue *value, ExprId expr); INLINE void llvm_emit_statement_chain(GenContext *c, AstId current); diff --git a/src/compiler/llvm_codegen_internal_impl.h b/src/compiler/llvm_codegen_internal_impl.h index 9f71067c0..958b381dd 100644 --- a/src/compiler/llvm_codegen_internal_impl.h +++ b/src/compiler/llvm_codegen_internal_impl.h @@ -1,3 +1,4 @@ + INLINE LLVMValueRef llvm_emit_insert_value(GenContext *c, LLVMValueRef agg, LLVMValueRef new_value, ArraySize index) { if (LLVMGetTypeKind(LLVMTypeOf(agg)) == LLVMVectorTypeKind) @@ -184,13 +185,13 @@ static inline bool llvm_delete_current_if_unused(GenContext *c) return true; } -static inline LLVMBasicBlockRef llvm_get_current_block_if_in_use(GenContext *context) +static inline LLVMBasicBlockRef llvm_get_current_block_if_in_use(GenContext *c) { - LLVMBasicBlockRef block = context->current_block; + LLVMBasicBlockRef block = c->current_block; if (block && llvm_basic_block_is_unused(block)) { LLVMDeleteBasicBlock(block); - return context->current_block = NULL; + return c->current_block = NULL; } return block; } diff --git a/src/compiler/llvm_codegen_stmt.c b/src/compiler/llvm_codegen_stmt.c index 4728809d1..906396ea3 100644 --- a/src/compiler/llvm_codegen_stmt.c +++ b/src/compiler/llvm_codegen_stmt.c @@ -264,7 +264,7 @@ static inline void llvm_emit_return(GenContext *c, Ast *ast) { if (has_return_value) { - if (llvm_temp_as_address(c, return_value.type)) + if (llvm_temp_as_address(return_value.type)) { LLVMValueRef temp = llvm_emit_alloca_aligned(c, return_value.type, "ret$temp"); llvm_store_to_ptr(c, temp, &return_value); diff --git a/src/compiler/llvm_codegen_storeload.c b/src/compiler/llvm_codegen_storeload.c index 40c9ddbc2..9de895101 100644 --- a/src/compiler/llvm_codegen_storeload.c +++ b/src/compiler/llvm_codegen_storeload.c @@ -18,7 +18,7 @@ void llvm_store_to_ptr_zero(GenContext *context, LLVMValueRef pointer, Type *typ llvm_store_to_ptr_raw_aligned(context, pointer, llvm_get_zero(context, type), type_abi_alignment(type)); } -bool llvm_temp_as_address(GenContext *c, Type *type) +bool llvm_temp_as_address(Type *type) { if (type_size(type) <= 2) return false; switch (type_lowering(type)->type_kind) @@ -77,7 +77,7 @@ LLVMValueRef llvm_load(GenContext *c, LLVMTypeRef type, LLVMValueRef pointer, Al ASSERT(!llvm_is_global_eval(c)); ASSERT(LLVMGetTypeContext(type) == c->context); LLVMValueRef value = LLVMBuildLoad2(c->builder, type, pointer, name); - llvm_set_alignment(value, alignment ? alignment : llvm_abi_alignment(c, type)); + llvm_set_alignment(value, alignment); return value; } diff --git a/src/compiler/llvm_codegen_type.c b/src/compiler/llvm_codegen_type.c index 7c154b8eb..8950815d9 100644 --- a/src/compiler/llvm_codegen_type.c +++ b/src/compiler/llvm_codegen_type.c @@ -145,7 +145,7 @@ static void param_expand(GenContext *context, LLVMTypeRef** params_ref, Type *ty vec_add(*params_ref, llvm_get_type(context, type)); return; } - + UNREACHABLE } static inline void add_func_type_param(GenContext *c, Type *param_type, ABIArgInfo *arg_info, LLVMTypeRef **params) @@ -430,7 +430,6 @@ static inline LLVMValueRef llvm_generate_introspection_global(GenContext *c, LLV } ASSERT(type == type->canonical); Type *parent_type = type_find_parent_type(type); - LLVMValueRef parent_typeid; LLVMValueRef global_name = NULL; if (!additional) { @@ -516,6 +515,7 @@ static LLVMValueRef llvm_get_introspection_for_enum(GenContext *c, Type *type) bool obfuscate = decl->obfuscate; for (unsigned i = 0; i < elements; i++) { + assert(values); const char *name = enum_vals[i]->name; scratch_buffer_clear(); scratch_buffer_append(".enum."); @@ -537,15 +537,15 @@ static LLVMValueRef llvm_get_introspection_for_enum(GenContext *c, Type *type) LLVMValueRef names = llvm_get_array(c->chars_type, values, elements); LLVMValueRef val = llvm_generate_introspection_global(c, NULL, type, INTROSPECT_TYPE_ENUM, type_base(type), elements, names, is_external); - LLVMTypeRef val_type; unsigned count = vec_size(associated_values); for (unsigned ai = 0; ai < count; ai++) { - val_type = NULL; + LLVMTypeRef val_type = NULL; bool mixed = false; for (unsigned i = 0; i < elements; i++) { + assert(values); BEValue value; llvm_emit_expr_global_value(c, &value, enum_vals[i]->enum_constant.args[ai]); LLVMValueRef llvm_value = llvm_load_value_store(c, &value); @@ -628,6 +628,7 @@ static LLVMValueRef llvm_get_introspection_for_fault(GenContext *c, Type *type) LLVMValueRef* values = elements ? MALLOC(sizeof(LLVMValueRef) * elements) : NULL; for (unsigned i = 0; i < elements; i++) { + assert(values); values[i] = fault_vals[i]->backend_ref; } c->builder = builder; diff --git a/src/compiler/parse_expr.c b/src/compiler/parse_expr.c index cb422e384..6c593c0cf 100644 --- a/src/compiler/parse_expr.c +++ b/src/compiler/parse_expr.c @@ -427,7 +427,7 @@ static Expr *parse_lambda(ParseContext *c, Expr *left) Decl **decls = NULL; Variadic variadic = VARIADIC_NONE; int vararg_index = -1; - if (!parse_parameters(c, &decls, NULL, &variadic, &vararg_index, PARAM_PARSE_LAMBDA)) return false; + if (!parse_parameters(c, &decls, &variadic, &vararg_index, PARAM_PARSE_LAMBDA)) return false; CONSUME_OR_RET(TOKEN_RPAREN, poisoned_expr); Signature *sig = &func->func_decl.signature; sig->vararg_index = vararg_index < 0 ? vec_size(decls) : vararg_index; @@ -606,7 +606,7 @@ bool parse_init_list(ParseContext *c, Expr ***result, TokenType param_end, bool /** * expression_list ::= decl_or_expr+ */ -Expr *parse_expression_list(ParseContext *c, bool allow_decl) +Expr *parse_expression_list(ParseContext *c, bool allow_decls) { Expr *expr_list = EXPR_NEW_TOKEN(EXPR_EXPRESSION_LIST); while (1) @@ -615,7 +615,7 @@ Expr *parse_expression_list(ParseContext *c, bool allow_decl) ASSIGN_EXPR_OR_RET(Expr *expr, parse_decl_or_expr(c, &decl), poisoned_expr); if (!expr) { - if (!allow_decl) + if (!allow_decls) { PRINT_ERROR_HERE("This looks like a declaration, which isn't allowed here."); return poisoned_expr; @@ -996,7 +996,7 @@ static Expr *parse_call_expr(ParseContext *c, Expr *left) PRINT_ERROR_LAST("Expected an ending ')'. Did you forget a ')' before this ';'?"); return poisoned_expr; } - if (!parse_parameters(c, &body_args, NULL, NULL, NULL, PARAM_PARSE_CALL)) return poisoned_expr; + if (!parse_parameters(c, &body_args, NULL, NULL, PARAM_PARSE_CALL)) return poisoned_expr; } if (!tok_is(c, TOKEN_RPAREN)) { diff --git a/src/compiler/parse_global.c b/src/compiler/parse_global.c index 386fea376..aa83fe515 100644 --- a/src/compiler/parse_global.c +++ b/src/compiler/parse_global.c @@ -13,7 +13,7 @@ typedef enum FunctionParse_ FUNC_PARSE_INTERFACE, } FunctionParse; -static inline Decl *parse_func_definition(ParseContext *c, AstId contracts, FunctionParse parse_type); +static inline Decl *parse_func_definition(ParseContext *c, AstId contracts, FunctionParse parse_kind); static inline bool parse_bitstruct_body(ParseContext *c, Decl *decl); static inline bool parse_enum_param_list(ParseContext *c, Decl*** parameters_ref); static Decl *parse_include(ParseContext *c); @@ -514,6 +514,7 @@ static inline TypeInfo *parse_generic_type(ParseContext *c, TypeInfo *type, bool /** * array_type_index ::= '[' (constant_expression | '*')? ']' * + * @param c the parse context * @param type the type to wrap, may not be poisoned. * @return type (poisoned if fails) */ @@ -572,6 +573,7 @@ DIRECT_SLICE:; /** * vector_type_index ::= '[<' (constant_expression | '*') '>]' * + * @param c the parse context. * @param type the type to wrap, may not be poisoned. * @return type (poisoned if fails) */ @@ -1076,13 +1078,13 @@ bool parse_attributes(ParseContext *c, Attr ***attributes_ref, Visibility *visib Attr *attr; if (!parse_attribute(c, &attr, false)) return false; if (!attr) return true; - bool parsed_builtin = false; Visibility parsed_visibility = -1; // NOLINT if (!attr->is_custom) { // This is important: if we would allow user defined attributes, // ordering between visibility of attributes would be complex. // since there is little + bool parsed_builtin = false; switch (attr->attr_kind) { case ATTRIBUTE_PUBLIC: @@ -1198,7 +1200,7 @@ static inline Decl *parse_global_declaration(ParseContext *c) print_error_at(type->span, "This looks like the beginning of a function declaration but it's missing the initial `fn`. Did you forget it?"); return poisoned_decl; } - else if (tok_is(c, TOKEN_LBRACKET)) + if (tok_is(c, TOKEN_LBRACKET)) { // Maybe we were doing int foo[4] = ... PRINT_ERROR_HERE("This looks like a declaration of the format 'int foo[4]' " @@ -1293,8 +1295,7 @@ INLINE bool is_end_of_param_list(ParseContext *c) * parameter ::= type ELLIPSIS? (non_type_ident ('=' expr))? * | ELLIPSIS (CT_TYPE_IDENT | non_type_ident ('=' expr)?)? */ -bool parse_parameters(ParseContext *c, Decl ***params_ref, Decl **body_params, - Variadic *variadic, int *vararg_index_ref, ParameterParseKind parse_kind) +bool parse_parameters(ParseContext *c, Decl ***params_ref, Variadic *variadic, int *vararg_index_ref, ParameterParseKind parse_kind) { Decl** params = NULL; bool var_arg_found = false; @@ -1509,7 +1510,7 @@ static inline bool parse_fn_parameter_list(ParseContext *c, Signature *signature CONSUME_OR_RET(TOKEN_LPAREN, false); Variadic variadic = VARIADIC_NONE; int vararg_index = -1; - if (!parse_parameters(c, &decls, NULL, &variadic, &vararg_index, PARAM_PARSE_FUNC)) return false; + if (!parse_parameters(c, &decls, &variadic, &vararg_index, PARAM_PARSE_FUNC)) return false; CONSUME_OR_RET(TOKEN_RPAREN, false); signature->vararg_index = vararg_index < 0 ? vec_size(decls) : vararg_index; signature->params = decls; @@ -1535,9 +1536,10 @@ static inline bool parse_fn_parameter_list(ParseContext *c, Signature *signature * | BITSTRUCT IDENT ':' type opt_attributes struct_body * | BITSTRUCT ':' type opt_attributes struct_body * + * @param c the parse context * @param parent the parent of the struct */ -bool parse_struct_body(ParseContext *c, Decl *parent) +static bool parse_struct_body(ParseContext *c, Decl *parent) { CONSUME_OR_RET(TOKEN_LBRACE, false); @@ -1866,7 +1868,7 @@ static bool parse_macro_params(ParseContext *c, Decl *macro) Variadic variadic = VARIADIC_NONE; int vararg_index = -1; Decl **params = NULL; - if (!parse_parameters(c, ¶ms, NULL, &variadic, &vararg_index, PARAM_PARSE_MACRO)) return false; + if (!parse_parameters(c, ¶ms, &variadic, &vararg_index, PARAM_PARSE_MACRO)) return false; macro->func_decl.signature.params = params; macro->func_decl.signature.vararg_index = vararg_index < 0 ? vec_size(params) : vararg_index; macro->func_decl.signature.variadic = variadic; @@ -1879,7 +1881,7 @@ static bool parse_macro_params(ParseContext *c, Decl *macro) TRY_CONSUME_OR_RET(TOKEN_AT_IDENT, "Expected an ending ')' or a block parameter on the format '@block(...).", false); if (try_consume(c, TOKEN_LPAREN)) { - if (!parse_parameters(c, &body_param->body_params, NULL, NULL, NULL, PARAM_PARSE_BODY)) return false; + if (!parse_parameters(c, &body_param->body_params, NULL, NULL, PARAM_PARSE_BODY)) return false; CONSUME_OR_RET(TOKEN_RPAREN, false); } macro->func_decl.body_param = declid(body_param); @@ -2066,7 +2068,7 @@ static inline Decl *parse_def_attribute(ParseContext *c) print_error_at(c->prev_span, "At least one parameter was expected after '(' - try removing the '()'."); return poisoned_decl; } - if (!parse_parameters(c, &decl->attr_decl.params, NULL, NULL, NULL, PARAM_PARSE_ATTR)) return poisoned_decl; + if (!parse_parameters(c, &decl->attr_decl.params, NULL, NULL, PARAM_PARSE_ATTR)) return poisoned_decl; CONSUME_OR_RET(TOKEN_RPAREN, poisoned_decl); } @@ -2170,7 +2172,7 @@ static inline bool parse_func_macro_header(ParseContext *c, Decl *decl) print_error_at(c->span, "Expected a macro name here, e.g. '@someName' or 'someName'."); return false; } - else if (!is_macro && c->tok != TOKEN_IDENT) + if (!is_macro && c->tok != TOKEN_IDENT) { print_error_at(c->span, "Expected a function name here, e.g. 'someName'."); return false; @@ -2363,7 +2365,7 @@ static inline Decl *parse_enum_declaration(ParseContext *c) "is not supported for declaring enum associated values."); return poisoned_decl; } - if (try_consume(c, TOKEN_COMMA)) continue; + if (try_consume(c, TOKEN_COMMA)) continue; // NOLINT } } enum_const->enum_constant.args = args; @@ -2828,7 +2830,7 @@ END: * * @return Decl* or a poison value if parsing failed */ -Decl *parse_top_level_statement(ParseContext *c, ParseContext **c_ref) +Decl *parse_top_level_statement(ParseContext *c, ParseContext **context_out) { AstId contracts = 0; if (!parse_contracts(c, &contracts)) return poisoned_decl; @@ -2879,7 +2881,7 @@ Decl *parse_top_level_statement(ParseContext *c, ParseContext **c_ref) decl->is_extern = true; break; case TOKEN_MODULE: - if (!c_ref) + if (!context_out) { PRINT_ERROR_HERE("'module' is not valid inside an include."); return poisoned_decl; @@ -2891,7 +2893,7 @@ Decl *parse_top_level_statement(ParseContext *c, ParseContext **c_ref) ParseContext *new_context = CALLOCS(ParseContext); *new_context = *c; new_context->unit = unit_create(c->unit->file); - *c_ref = c = new_context; + *context_out = c = new_context; } if (!parse_module(c, contracts)) return poisoned_decl; return NULL; @@ -2931,7 +2933,7 @@ Decl *parse_top_level_statement(ParseContext *c, ParseContext **c_ref) } case TOKEN_IMPORT: if (contracts) goto CONTRACT_NOT_ALLOWED; - if (!c_ref) + if (!context_out) { PRINT_ERROR_HERE("'import' may not appear inside a compile time statement."); return poisoned_decl; diff --git a/src/compiler/parser_internal.h b/src/compiler/parser_internal.h index 26fdf8962..5206b028c 100644 --- a/src/compiler/parser_internal.h +++ b/src/compiler/parser_internal.h @@ -22,7 +22,7 @@ typedef enum #define TRY_CONSUME_OR_RET(_tok, _message, _type) do { if (!consume(c, _tok, _message)) return _type; } while(0) #define CHECK_EXPR_OR_RET(_expr) do { if (!expr_ok(_expr)) return _expr; } while(0) -Decl *parse_top_level_statement(ParseContext *c, ParseContext **new_context); +Decl *parse_top_level_statement(ParseContext *c, ParseContext **context_out); Ast *parse_ct_assert_stmt(ParseContext *c); Ast *parse_ct_error_stmt(ParseContext *c); Ast *parse_ct_echo_stmt(ParseContext *c); @@ -57,8 +57,8 @@ bool parse_current_is_expr(ParseContext *c); bool parse_generic_parameters(ParseContext *c, Expr ***exprs_ref, bool is_new_syntax); -bool parse_parameters(ParseContext *c, Decl ***params_ref, Decl **body_params, - Variadic *variadic, int *vararg_index_ref, ParameterParseKind parse_kind); +bool parse_parameters(ParseContext *c, Decl ***params_ref, + Variadic *variadic, int *vararg_index_ref, ParameterParseKind parse_kind); bool parse_arg_list(ParseContext *c, Expr ***result, TokenType param_end, bool vasplat); Expr *parse_type_compound_literal_expr_after_type(ParseContext *c, TypeInfo *type_info); diff --git a/src/compiler/sema_passes.c b/src/compiler/sema_passes.c index 2f0eee2ef..fbb3028d2 100644 --- a/src/compiler/sema_passes.c +++ b/src/compiler/sema_passes.c @@ -131,7 +131,7 @@ INLINE void register_global_decls(CompilationUnit *unit, Decl **decls) vec_resize(decls, 0); } -INLINE File *sema_load_file(CompilationUnit *unit, SourceSpan span, Expr *filename, const char *type, File *no_file) +INLINE File *sema_load_file(CompilationUnit *unit, Expr *filename) { if (!expr_is_const_string(filename)) { @@ -149,7 +149,6 @@ INLINE File *sema_load_file(CompilationUnit *unit, SourceSpan span, Expr *filena File *file = source_file_load(string, &loaded, &error); if (!file) { - if (no_file) return no_file; print_error_at(filename->span, "Failed to load file '%s': %s.", filename->const_expr.bytes.ptr, error); return NULL; } @@ -175,7 +174,7 @@ static Decl **sema_load_include(CompilationUnit *unit, Decl *decl) bool success = sema_analyse_ct_expr(&context, decl->include.filename); sema_context_destroy(&context); if (!success) return NULL; - File *file = sema_load_file(unit, decl->span, decl->include.filename, "$include", NULL); + File *file = sema_load_file(unit, decl->include.filename); if (!file) return NULL; if (compiler.context.includes_used++ > MAX_INCLUDE_DIRECTIVES) { @@ -279,12 +278,11 @@ static Decl **sema_run_exec(CompilationUnit *unit, Decl *decl) } } File *file; - // TODO fix Win32 - char old_path_buffer[PATH_MAX]; + char old_path_buffer[PATH_MAX]; // NOLINT char *old_path = NULL; if (compiler.build.script_dir) { - old_path = getcwd(old_path, PATH_MAX); + old_path = getcwd(old_path_buffer, PATH_MAX); if (!dir_change(compiler.build.script_dir)) { RETURN_PRINT_ERROR_AT(NULL, decl, "Failed to open script dir '%s'", compiler.build.script_dir);