From ba9b203c524975efce779a889d4338b9145ce992 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 15 Sep 2022 09:50:46 +0200 Subject: [PATCH] Cleanup. --- src/build/builder.c | 1 - src/compiler/asm/x86.h | 2 -- src/compiler/asm_target.c | 3 +-- src/compiler/codegen_asm.c | 9 ++------- src/compiler/codegen_internal.h | 2 +- src/compiler/compiler_internal.h | 21 +-------------------- src/compiler/linker.c | 2 +- src/compiler/llvm_codegen_expr.c | 12 ++---------- src/compiler/llvm_codegen_function.c | 1 + src/compiler/llvm_codegen_internal.h | 2 +- src/compiler/llvm_codegen_internal_impl.h | 6 +++--- src/compiler/llvm_codegen_stmt.c | 3 ++- src/compiler/number.c | 1 - src/compiler/sema_asm.c | 5 ++--- src/compiler/sema_decls.c | 3 --- src/compiler/sema_expr.c | 12 +++++------- src/compiler/sema_internal.h | 2 +- src/compiler/sema_stmts.c | 17 ++--------------- 18 files changed, 25 insertions(+), 79 deletions(-) diff --git a/src/build/builder.c b/src/build/builder.c index f26a4a5c1..71ee35847 100644 --- a/src/build/builder.c +++ b/src/build/builder.c @@ -5,7 +5,6 @@ #include "build_options.h" void load_library_files(void) {} -void load_files(void) {} #if defined(_M_X64) || defined(_M_AMD64) ArchOsTarget default_target = WINDOWS_X64; diff --git a/src/compiler/asm/x86.h b/src/compiler/asm/x86.h index cc7a7861a..a98201581 100644 --- a/src/compiler/asm/x86.h +++ b/src/compiler/asm/x86.h @@ -5,7 +5,6 @@ typedef enum X86_CC, X86_DF, X86_RAX, - X86_FIRST_RI_CLOBBER = X86_RAX, X86_RBX, X86_RCX, X86_RDX, @@ -225,4 +224,3 @@ static const char *x86_xmm_regs[] = { "$xmm0", "$xmm1", "$xmm2", "$xmm3", "$xmm4 static const char *x86_ymm_regs[] = { "$ymm0", "$ymm1", "$ymm2", "$ymm3", "$ymm4", "$ymm5", "$ymm6", "$ymm7", "$ymm8", "$ymm9", "$ymm10", "$ymm11", "$ymm12", "$ymm13", "$ymm14", "$ymm15" }; -static char x86_int_width_suffix[] = { 'b', 'w', 'l', 'q' }; diff --git a/src/compiler/asm_target.c b/src/compiler/asm_target.c index 7bb7151b8..765d28ed1 100644 --- a/src/compiler/asm_target.c +++ b/src/compiler/asm_target.c @@ -221,7 +221,6 @@ AsmRegister *asm_reg_by_name(const char *name) static void init_asm_aarch64(void) { // Clobbers cc_flag_mask = clobbers_make(AARCH64_CC, -1); - bool is_x64 = platform_target.arch == ARCH_TYPE_X86_64; asm_target.clobber_name_list = Aarch64ClobberNames; asm_target.extra_clobbers = NULL; reg_instr("ldr", "w:r32/r64, mem"); // Could be separated @@ -424,7 +423,7 @@ void init_asm(void) return; case ARCH_TYPE_UNKNOWN: error_exit("Unknown arch does not support asm."); - break; + UNREACHABLE case ARCH_TYPE_PPC: case ARCH_TYPE_PPC64: case ARCH_TYPE_PPC64LE: diff --git a/src/compiler/codegen_asm.c b/src/compiler/codegen_asm.c index d4b3e6b58..d9378f447 100644 --- a/src/compiler/codegen_asm.c +++ b/src/compiler/codegen_asm.c @@ -86,12 +86,8 @@ static inline void codegen_create_aarch64_arg(AsmInlineBlock *block, unsigned in scratch_buffer_append_unsigned_int(arg->value); return; case ASM_ARG_REG: - { - - AsmRegister *reg = arg->reg.ref; - scratch_buffer_append(®->name[1]); + scratch_buffer_append(&arg->reg.ref->name[1]); return; - } case ASM_ARG_VALUE: scratch_buffer_append_char('$'); scratch_buffer_append_unsigned_int(arg->index + input_offset); @@ -110,7 +106,6 @@ static inline void codegen_create_aarch64_arg(AsmInlineBlock *block, unsigned in return; case ASM_ARG_ADDR: TODO - return; case ASM_ARG_ADDROF: TODO } @@ -170,7 +165,7 @@ static inline char *codegen_create_aarch64_asm(AsmInlineBlock *block) return scratch_buffer_to_string(); } -const char *codegen_create_asm(Ast *ast, ClobberList *clobber_list) +const char *codegen_create_asm(Ast *ast) { assert(ast->ast_kind == AST_ASM_BLOCK_STMT); scratch_buffer_clear(); diff --git a/src/compiler/codegen_internal.h b/src/compiler/codegen_internal.h index 0f42ad456..42d18cb31 100644 --- a/src/compiler/codegen_internal.h +++ b/src/compiler/codegen_internal.h @@ -56,4 +56,4 @@ static inline bool abi_type_is_promotable_integer_or_bool(AbiType type) return false; } -const char *codegen_create_asm(Ast *ast, ClobberList *clobber_list); \ No newline at end of file +const char *codegen_create_asm(Ast *ast); \ No newline at end of file diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index c5926872a..f43666089 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -541,15 +541,6 @@ typedef struct }; } FuncDecl; -typedef struct -{ - TypeInfoId type_parent; // May be 0 - Signature signature; - AstId body; - DeclId body_param; - CompilationUnit *unit; - AstId docs; -} MacroDecl; typedef struct { @@ -621,10 +612,7 @@ typedef struct } LabelDecl; -typedef struct -{ - Decl **params; -} BodyParamDecl; + typedef struct Decl_ { @@ -1043,11 +1031,6 @@ typedef struct }; } ExprTryUnwrap; -typedef struct -{ - Expr *inner; -} ExprLen; - typedef struct { @@ -1119,7 +1102,6 @@ struct Expr_ ExprIdentifierRaw ct_ident_expr; // 24 ExprCtCall ct_call_expr; // 24 ExprCtArg ct_arg_expr; - ExprIdentifierRaw ct_macro_ident_expr; // 24 ExprIdentifierRaw hash_ident_expr; // 24 TypeInfo *typeid_expr; // 8 ExprBodyExpansion body_expansion_expr; // 24 @@ -1443,7 +1425,6 @@ typedef struct Module_ Decl** method_extensions; Decl** intvec_extensions; Decl** floatvec_extensions; - Decl** generic_cache; HTable symbols; struct CompilationUnit_ **units; Module *parent_module; diff --git a/src/compiler/linker.c b/src/compiler/linker.c index 9b25096ef..a524b322f 100644 --- a/src/compiler/linker.c +++ b/src/compiler/linker.c @@ -47,7 +47,7 @@ static inline bool is_pie(RelocModel reloc) static const char *ld_target(ArchType arch_type) { - switch (platform_target.arch) + switch (arch_type) { case ARCH_TYPE_X86_64: return "elf_x86_64"; diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 299a39913..b5c28bcd1 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -1062,7 +1062,6 @@ static void llvm_emit_arr_to_subarray_cast(GenContext *c, BEValue *value, Type * if (size) { llvm_value_rvalue(c, value); - LLVMTypeRef subarray_type = llvm_get_type(c, to_type); pointer = llvm_emit_bitcast_ptr(c, value->value, array_type); } else @@ -1112,7 +1111,6 @@ void llvm_emit_cast(GenContext *c, CastKind cast_kind, Expr *expr, BEValue *valu case CAST_PTRANY: { llvm_value_rvalue(c, value); - LLVMTypeRef any = llvm_get_type(c, to_type); LLVMValueRef pointer = llvm_emit_bitcast(c, value->value, type_voidptr); BEValue typeid; llvm_emit_typeid(c, &typeid, from_type->pointer); @@ -4789,7 +4787,6 @@ void llvm_emit_call_expr(GenContext *c, BEValue *result_value, Expr *expr) ABIArgInfo **abi_args = prototype->abi_args; unsigned param_count = vec_size(params); Expr **args = expr->call_expr.arguments; - unsigned arguments = vec_size(args); Expr **varargs = NULL; Expr *vararg_splat = NULL; if (prototype->variadic != VARIADIC_NONE) @@ -5276,8 +5273,7 @@ static inline void llvm_emit_expr_block(GenContext *context, BEValue *be_value, static inline void llvm_emit_macro_block(GenContext *context, BEValue *be_value, Expr *expr) { - foreach(Decl *, expr->macro_block.params) - { + FOREACH_BEGIN(Decl *val, expr->macro_block.params) // Skip vararg if (!val) continue; // In case we have a constant, we never do an emit. The value is already folded. @@ -5313,7 +5309,7 @@ static inline void llvm_emit_macro_block(GenContext *context, BEValue *be_value, llvm_emit_expr(context, &value, val->var.init_expr); llvm_store_decl(context, val, &value); - } + FOREACH_END(); llvm_emit_return_block(context, be_value, expr->type, expr->macro_block.first_stmt, expr->macro_block.block_exit); } @@ -5603,7 +5599,6 @@ static inline void llvm_emit_typeid_info(GenContext *c, BEValue *value, Expr *ex llvm_emit_block(c, exit); } { - LLVMTypeRef typeid = llvm_get_type(c, type_typeid); LLVMValueRef len = llvm_emit_struct_gep_raw(c, ref, c->introspect_type, INTROSPECT_INDEX_LEN, align, &alignment); len = llvm_load(c, c->size_type, len, alignment, "namelen"); LLVMValueRef val = llvm_emit_struct_gep_raw(c, ref, c->introspect_type, INTROSPECT_INDEX_ADDITIONAL, align, &alignment); @@ -5742,7 +5737,6 @@ static inline void llvm_emit_argv_to_subarray(GenContext *c, BEValue *value, Exp LLVMSetAlignment(arg_array, alignment); // Store the addresses. - LLVMTypeRef temp_type = llvm_get_type(c, expr->type); LLVMTypeRef loop_type = llvm_get_type(c, type_usize); LLVMTypeRef char_ptr_type = llvm_get_ptr_type(c, type_char); LLVMValueRef size = llvm_zext_trunc(c, count, loop_type); @@ -5846,7 +5840,6 @@ static inline void llvm_emit_builtin_access(GenContext *c, BEValue *be_value, Ex llvm_emit_block(c, ok_block); LLVMValueRef fault_data = LLVMBuildIntToPtr(c->builder, be_value->value, LLVMPointerType(c->fault_type, 0), ""); - LLVMTypeRef subarray = llvm_get_type(c, type_chars); LLVMValueRef ptr = LLVMBuildStructGEP2(c->builder, c->fault_type, fault_data, 1, ""); llvm_emit_br(c, exit_block); llvm_emit_block(c, exit_block); @@ -5864,7 +5857,6 @@ static inline void llvm_emit_builtin_access(GenContext *c, BEValue *be_value, Ex llvm_value_rvalue(c, be_value); LLVMTypeRef subarray = llvm_get_type(c, type_chars); - LLVMTypeRef backend = LLVMTypeOf(inner_type->backend_typeid); LLVMValueRef to_introspect = LLVMBuildIntToPtr(c->builder, inner_type->backend_typeid, LLVMPointerType(c->introspect_type, 0), ""); LLVMValueRef ptr = LLVMBuildStructGEP2(c->builder, c->introspect_type, to_introspect, INTROSPECT_INDEX_ADDITIONAL, ""); diff --git a/src/compiler/llvm_codegen_function.c b/src/compiler/llvm_codegen_function.c index 498504ce1..ecbdb9416 100644 --- a/src/compiler/llvm_codegen_function.c +++ b/src/compiler/llvm_codegen_function.c @@ -324,6 +324,7 @@ void llvm_emit_return_abi(GenContext *c, BEValue *return_value, BEValue *failabl // Get the coerce type. LLVMTypeRef coerce_type = llvm_get_coerce_type(c, info); // Create the new pointer + assert(return_value); LLVMValueRef coerce = LLVMBuildBitCast(c->builder, return_value->value, coerce_type, ""); // We might have only one value, in that case, build a GEP to that one. LLVMValueRef lo_val; diff --git a/src/compiler/llvm_codegen_internal.h b/src/compiler/llvm_codegen_internal.h index 0240cfc0c..18378612a 100644 --- a/src/compiler/llvm_codegen_internal.h +++ b/src/compiler/llvm_codegen_internal.h @@ -297,7 +297,7 @@ LLVMValueRef llvm_emit_alloca(GenContext *c, LLVMTypeRef type, unsigned alignmen LLVMValueRef llvm_emit_alloca_aligned(GenContext *c, Type *type, const char *name); void llvm_emit_and_set_decl_alloca(GenContext *c, Decl *decl); INLINE void llvm_set_alignment(LLVMValueRef alloca, AlignSize alignment); -INLINE AlignSize llvm_type_or_alloca_align(GenContext *c, LLVMValueRef dest, Type *type); +INLINE AlignSize llvm_type_or_alloca_align(LLVMValueRef dest, Type *type); // -- Bitcast -- static inline LLVMValueRef llvm_emit_bitcast(GenContext *c, LLVMValueRef value, Type *type); diff --git a/src/compiler/llvm_codegen_internal_impl.h b/src/compiler/llvm_codegen_internal_impl.h index b9b61eece..d2dba95c5 100644 --- a/src/compiler/llvm_codegen_internal_impl.h +++ b/src/compiler/llvm_codegen_internal_impl.h @@ -45,7 +45,7 @@ INLINE LLVMValueRef llvm_store_decl_raw(GenContext *context, Decl *decl, LLVMVal return llvm_store_to_ptr_raw_aligned(context, decl->backend_ref, value, decl->alignment); } -INLINE AlignSize llvm_type_or_alloca_align(GenContext *c, LLVMValueRef dest, Type *type) +INLINE AlignSize llvm_type_or_alloca_align(LLVMValueRef dest, Type *type) { if (LLVMIsAAllocaInst(dest) || LLVMIsAGlobalVariable(dest)) { @@ -56,12 +56,12 @@ INLINE AlignSize llvm_type_or_alloca_align(GenContext *c, LLVMValueRef dest, Typ INLINE LLVMValueRef llvm_store_to_ptr(GenContext *c, LLVMValueRef destination, BEValue *value) { - return llvm_store_to_ptr_aligned(c, destination, value, llvm_type_or_alloca_align(c, destination, value->type)); + return llvm_store_to_ptr_aligned(c, destination, value, llvm_type_or_alloca_align(destination, value->type)); } INLINE LLVMValueRef llvm_store_to_ptr_raw(GenContext *c, LLVMValueRef pointer, LLVMValueRef value, Type *type) { - return llvm_store_to_ptr_raw_aligned(c, pointer, value, llvm_type_or_alloca_align(c, pointer, type)); + return llvm_store_to_ptr_raw_aligned(c, pointer, value, llvm_type_or_alloca_align(pointer, type)); } static inline LLVMValueRef llvm_emit_bitcast(GenContext *c, LLVMValueRef value, Type *type) diff --git a/src/compiler/llvm_codegen_stmt.c b/src/compiler/llvm_codegen_stmt.c index f8e9662cc..1aaa76858 100644 --- a/src/compiler/llvm_codegen_stmt.c +++ b/src/compiler/llvm_codegen_stmt.c @@ -514,6 +514,7 @@ void llvm_emit_for_stmt(GenContext *c, Ast *ast) // Emit the block llvm_emit_block(c, cond_block); BEValue be_value; + assert(cond); if (cond->expr_kind == EXPR_COND) { llvm_emit_decl_expr_list(c, &be_value, cond, true); @@ -1066,7 +1067,7 @@ static inline void llvm_emit_asm_block_stmt(GenContext *c, Ast *ast) } else { - data = codegen_create_asm(ast, &clobber_list); + data = codegen_create_asm(ast); clobbers = clobber_list.string; FOREACH_BEGIN(ExprAsmArg * var, block->output_vars) codegen_new_constraint(&clobber_list); diff --git a/src/compiler/number.c b/src/compiler/number.c index 6e9732726..e55490909 100644 --- a/src/compiler/number.c +++ b/src/compiler/number.c @@ -189,7 +189,6 @@ bool expr_const_will_overflow(const ExprConst *expr, TypeKind kind) const char *expr_const_to_error_string(const ExprConst *expr) { - char *buff = NULL; switch (expr->const_kind) { case CONST_POINTER: diff --git a/src/compiler/sema_asm.c b/src/compiler/sema_asm.c index 224c26235..5b6828abe 100644 --- a/src/compiler/sema_asm.c +++ b/src/compiler/sema_asm.c @@ -204,10 +204,9 @@ static inline bool sema_check_asm_arg_reg(SemaContext *context, AsmInlineBlock * static inline ExprAsmArg *asm_reg_find_decl(ExprAsmArg **args, Decl *decl, AsmArgKind kind) { - foreach(ExprAsmArg *, args) - { + FOREACH_BEGIN(ExprAsmArg *val, args) if (val->kind == kind && val->ident.ident_decl == decl) return val; - } + FOREACH_END(); return NULL; } diff --git a/src/compiler/sema_decls.c b/src/compiler/sema_decls.c index 84d78f7a7..522f7c725 100644 --- a/src/compiler/sema_decls.c +++ b/src/compiler/sema_decls.c @@ -2009,7 +2009,6 @@ static inline bool sema_analyse_main_function(SemaContext *context, Decl *decl) Ast *stmt = new_ast(AST_EXPR_STMT, decl->span); stmt->expr_stmt = call; ast_append(&next, stmt); - Expr *c = expr_new(EXPR_CONST, decl->span); ret_stmt->expr_stmt = expr_new_const_int(decl->span, type_cint, 0, true); } ast_append(&next, ret_stmt); @@ -2145,7 +2144,6 @@ static inline bool sema_analyse_macro(SemaContext *context, Decl *decl) return decl_poison(decl); } - Decl **parameters = decl->func_decl.signature.params; DeclId body_param = decl->func_decl.body_param; Decl **body_parameters = body_param ? declptr(body_param)->body_params : NULL; @@ -2590,7 +2588,6 @@ static bool sema_analyse_parameterized_define(SemaContext *c, Decl *decl) static inline bool sema_analyse_attribute_decl(SemaContext *c, Decl *decl) { Decl **params = decl->attr_decl.params; - Attr **attrs = decl->attr_decl.attrs; unsigned param_count = vec_size(params); for (unsigned i = 0; i < param_count; i++) { diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index a7041b077..c83906d84 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -1722,6 +1722,7 @@ static inline bool sema_expr_analyse_call_invocation(SemaContext *context, Expr // 4a. Is this *not* a variadic function/macro? - Then that's an error. if (variadic == VARIADIC_NONE) { + assert(call->call_expr.arguments); SEMA_ERROR(call->call_expr.arguments[num_args - 1], "Using the splat operator is only allowed on vararg parameters."); return false; @@ -2009,6 +2010,7 @@ static inline Type *unify_returns(SemaContext *context) { SEMA_ERROR(return_stmt, "Cannot find a common parent type of %s and %s", rtype, common_type); + assert(context->returns); SEMA_NOTE(context->returns[i - 1], "The previous return was here."); return NULL; } @@ -3488,7 +3490,6 @@ static inline void expr_replace_with_enum_name_array(Expr *enum_array_expr, Decl Expr *initializer = expr_new(EXPR_INITIALIZER_LIST, span); ArraySize elements = vec_size(values); Expr **element_values = elements > 0 ? VECNEW(Expr*, elements) : NULL; - Type *kind = enum_decl->type; for (ArraySize i = 0; i < elements; i++) { Decl *decl = values[i]; @@ -8388,6 +8389,7 @@ bool sema_analyse_expr_lvalue_fold_const(SemaContext *context, Expr *expr) bool sema_analyse_expr_lvalue(SemaContext *context, Expr *expr) { + assert(expr); switch (expr->resolve_status) { case RESOLVE_NOT_DONE: @@ -8430,6 +8432,7 @@ MemberIndex sema_get_initializer_const_array_size(SemaContext *context, Expr *in return 0; case CONST_INIT_ARRAY: *may_be_array = true; + assert(init->init_array.elements); return VECLAST(init->init_array.elements)->init_array_value.index + 1; case CONST_INIT_ARRAY_FULL: *may_be_array = true; @@ -8669,14 +8672,9 @@ bool sema_insert_method_call(SemaContext *context, Expr *method_call, Decl *meth expr_insert_deref(parent); } } - assert(first == parent->type->canonical); + assert(parent && first == parent->type->canonical); if (!sema_expr_analyse_general_call(context, method_call, method_decl, parent, false)) return expr_poison(method_call); method_call->resolve_status = RESOLVE_DONE; return true; } -bool sema_expr_rvalue(SemaContext *context, Expr *expr) -{ - return sema_cast_rvalue(context, expr); -} - diff --git a/src/compiler/sema_internal.h b/src/compiler/sema_internal.h index c2fde42d4..6f44235db 100644 --- a/src/compiler/sema_internal.h +++ b/src/compiler/sema_internal.h @@ -85,7 +85,7 @@ const char *ct_eval_expr(SemaContext *c, const char *expr_type, Expr *inner, Tok extern const char *ct_eval_error; SemaContext *transform_context_for_eval(SemaContext *context, SemaContext *temp_context, CompilationUnit *eval_unit); bool sema_check_asm(SemaContext *context, AsmInlineBlock *block, Ast *asm_stmt); -bool sema_expr_rvalue(SemaContext *context, Expr *expr); + static inline bool expr_is_const(Expr *expr); static inline bool expr_is_const(Expr *expr) diff --git a/src/compiler/sema_stmts.c b/src/compiler/sema_stmts.c index a276fde47..58e1e76ea 100644 --- a/src/compiler/sema_stmts.c +++ b/src/compiler/sema_stmts.c @@ -421,7 +421,7 @@ static inline bool sema_analyse_catch_unwrap(SemaContext *context, Expr *expr) if (ident->type->canonical != type_anyerr) { SEMA_ERROR(ident, "Expected the variable to have the type %s, not %s.", type_quoted_error_string(type_anyerr), - type_quoted_error_string(type->type)); + type_quoted_error_string(ident->type)); return false; } @@ -709,20 +709,6 @@ static inline bool sema_analyse_declare_stmt(SemaContext *context, Ast *statemen return sema_analyse_var_decl(context, statement->declare_stmt, true); } -static inline bool expr_is_assign(Expr *expr) -{ - switch (expr->expr_kind) - { - case EXPR_DECL: - case EXPR_BITASSIGN: - case EXPR_SLICE_ASSIGN: - return true; - case EXPR_BINARY: - return expr->binary_expr.operator >= BINARYOP_ASSIGN; - default: - return false; - } -} static inline bool sema_analyse_expr_stmt(SemaContext *context, Ast *statement) { Expr *expr = statement->expr_stmt; @@ -983,6 +969,7 @@ static inline bool sema_analyse_foreach_stmt(SemaContext *context, Ast *statemen if (statement->foreach_stmt.index_by_ref) { + assert(index); SEMA_ERROR(index, "The index cannot be held by reference, did you accidentally add a '&'?"); return false; }