From 4d052d5119653c80f4c7b7c6695256e0540a55a5 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 20 May 2018 17:31:46 +0100 Subject: [PATCH] Remove code relating to default struct values --- src/check_type.cpp | 96 +------------------------------------ src/gb/gb.h | 26 +++++----- src/ir.cpp | 94 ------------------------------------ src/ir_print.cpp | 117 +++++++-------------------------------------- src/parser.cpp | 3 +- src/types.cpp | 1 - 6 files changed, 32 insertions(+), 305 deletions(-) diff --git a/src/check_type.cpp b/src/check_type.cpp index 7d6582ece..ca961c133 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -65,92 +65,11 @@ void check_struct_fields(Checker *c, AstNode *node, Array *fields, Arr ast_node(p, Field, param); AstNode *type_expr = p->type; Type *type = nullptr; - AstNode *default_value = unparen_expr(p->default_value); - ExactValue value = {}; - bool default_is_nil = false; bool detemine_type_from_operand = false; - if (type_expr == nullptr) { - Operand o = {}; - check_expr_or_type(c, &o, default_value); - if (is_operand_nil(o)) { - default_is_nil = true; - } else if (o.mode != Addressing_Constant) { - if (default_value->kind == AstNode_ProcLit) { - if (named_type != nullptr) { - value = exact_value_procedure(default_value); - } else { - error(default_value, "A procedure literal cannot be a default value in an anonymous structure"); - } - } else { - Entity *e = nullptr; - if (o.mode == Addressing_Value && is_type_proc(o.type)) { - Operand x = {}; - if (default_value->kind == AstNode_Ident) { - e = check_ident(c, &x, default_value, nullptr, nullptr, false); - } else if (default_value->kind == AstNode_SelectorExpr) { - e = check_selector(c, &x, default_value, nullptr); - } - } - - if (e != nullptr && e->kind == Entity_Procedure) { - value = exact_value_procedure(e->identifier); - add_entity_use(c, e->identifier, e); - } else { - error(default_value, "Default parameter must be a constant"); - } - } - } else { - value = o.value; - } - - type = default_type(o.type); - } else { - if (type_expr->kind == AstNode_Ident && type_expr->Ident.token.string == "Element") { - gb_printf_err("Element\n"); - } - + if (type_expr != nullptr) { type = check_type_expr(c, type_expr, nullptr); - - if (default_value != nullptr) { - Operand o = {}; - check_expr_with_type_hint(c, &o, default_value, type); - - if (is_operand_nil(o)) { - default_is_nil = true; - } else if (o.mode != Addressing_Constant) { - if (default_value->kind == AstNode_ProcLit) { - if (named_type != nullptr) { - value = exact_value_procedure(default_value); - } else { - error(default_value, "A procedure literal cannot be a default value in an anonymous structure"); - } - } else { - Entity *e = nullptr; - if (o.mode == Addressing_Value && is_type_proc(o.type)) { - Operand x = {}; - if (default_value->kind == AstNode_Ident) { - e = check_ident(c, &x, default_value, nullptr, nullptr, false); - } else if (default_value->kind == AstNode_SelectorExpr) { - e = check_selector(c, &x, default_value, nullptr); - } - } - - if (e != nullptr && e->kind == Entity_Procedure) { - value = exact_value_procedure(e->identifier); - add_entity_use(c, e->identifier, e); - } else { - error(default_value, "Default parameter must be a constant"); - } - } - } else { - value = o.value; - } - - check_is_assignable_to(c, &o, type); - } - if (is_type_polymorphic(type)) { type = nullptr; } @@ -179,9 +98,6 @@ void check_struct_fields(Checker *c, AstNode *node, Array *fields, Arr Token name_token = name->Ident.token; Entity *field = alloc_entity_field(c->context.scope, name_token, type, is_using, field_src_index); - field->Variable.default_value = value; - field->Variable.default_is_nil = default_is_nil; - add_entity(c, c->context.scope, name, field); array_add(fields, field); @@ -511,16 +427,6 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node, ArrayStruct.fields, st->fields, min_field_count, named_type, context); } - for_array(i, struct_type->Struct.fields) { - Entity *f = struct_type->Struct.fields[i]; - if (f->kind == Entity_Variable) { - if (f->Variable.default_value.kind == ExactValue_Procedure) { - struct_type->Struct.has_proc_default_values = true; - break; - } - } - } - if (st->align != nullptr) { if (st->is_packed) { syntax_error(st->align, "'#align' cannot be applied with '#packed'"); diff --git a/src/gb/gb.h b/src/gb/gb.h index eb838702b..b2997eccd 100644 --- a/src/gb/gb.h +++ b/src/gb/gb.h @@ -3673,7 +3673,8 @@ gb_inline void *gb_memcopy(void *dest, void const *source, isize n) { return NULL; } - __asm__ __volatile__("rep movsb" : "+D"(dest), "+S"(source), "+c"(n) : : "memory"); + void *dest_copy = dest; + __asm__ __volatile__("rep movsb" : "+D"(dest_copy), "+S"(source), "+c"(n) : : "memory"); #else u8 *d = cast(u8 *)dest; u8 const *s = cast(u8 const *)source; @@ -3798,12 +3799,15 @@ gb_inline void *gb_memcopy(void *dest, void const *source, isize n) { *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; } - if (n & 4) + if (n & 4) { *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; - if (n & 2) + } + if (n & 2) { *d++ = *s++; *d++ = *s++; - if (n & 1) + } + if (n & 1) { *d = *s; + } } #endif @@ -4027,10 +4031,10 @@ gb_inline void *gb_resize (gbAllocator a, void *ptr, isize old_size, isize gb_inline void *gb_resize_align(gbAllocator a, void *ptr, isize old_size, isize new_size, isize alignment) { return a.proc(a.data, gbAllocation_Resize, new_size, alignment, ptr, old_size, GB_DEFAULT_ALLOCATOR_FLAGS); } gb_inline void *gb_alloc_copy (gbAllocator a, void const *src, isize size) { - return gb_memmove(gb_alloc(a, size), src, size); + return gb_memcopy(gb_alloc(a, size), src, size); } gb_inline void *gb_alloc_copy_align(gbAllocator a, void const *src, isize size, isize alignment) { - return gb_memmove(gb_alloc_align(a, size, alignment), src, size); + return gb_memcopy(gb_alloc_align(a, size, alignment), src, size); } gb_inline char *gb_alloc_str(gbAllocator a, char const *str) { @@ -4039,8 +4043,7 @@ gb_inline char *gb_alloc_str(gbAllocator a, char const *str) { gb_inline char *gb_alloc_str_len(gbAllocator a, char const *str, isize len) { char *result; - result = cast(char *)gb_alloc(a, len+1); - gb_memmove(result, str, len); + result = cast(char *)gb_alloc_copy(a, str, len+1); result[len] = '\0'; return result; } @@ -6548,7 +6551,7 @@ gbString gb_string_make_length(gbAllocator a, void const *init_str, isize num_by header->allocator = a; header->length = num_bytes; header->capacity = num_bytes; - if (num_bytes > 0 && init_str) { + if (num_bytes && init_str) { gb_memcopy(str, init_str, num_bytes); } str[num_bytes] = '\0'; @@ -8480,9 +8483,9 @@ gb_no_inline isize gb_snprintf_va(char *text, isize max_len, char const *fmt, va int width = va_arg(va, int); if (width < 0) { info.flags |= gbFmt_Minus; - info.width = -info.width; + info.width = -width; } else { - info.width = -info.width; + info.width = width; } fmt++; } else { @@ -10813,4 +10816,3 @@ GB_COMPARE_PROC(gb_video_mode_dsc_cmp) { #endif #endif // GB_IMPLEMENTATION - diff --git a/src/ir.cpp b/src/ir.cpp index 81f88e8c9..ab5c38838 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -683,37 +683,6 @@ Type *ir_type(irValue *value) { } -bool ir_type_has_default_values(Type *t) { -#if !defined(NO_DEFAULT_STRUCT_VALUES) - switch (t->kind) { - case Type_Named: - return ir_type_has_default_values(t->Named.base); - - case Type_Array: - return ir_type_has_default_values(t->Array.elem); - - case Type_Struct: - if (t->Struct.is_raw_union) return false; - for_array(i, t->Struct.fields) { - Entity *f = t->Struct.fields[i]; - if (f->kind != Entity_Variable) continue; - if (f->Variable.default_is_nil) { - // NOTE(bill): This is technically zero - continue; - } else if (f->Variable.default_value.kind != ExactValue_Invalid) { - return true; - } else if (f->Variable.default_is_undef) { - return true; - } - bool sub = ir_type_has_default_values(f->type); - if (sub) return true; - } - break; - } -#endif - return false; -} - irInstr *ir_get_last_instr(irBlock *block) { if (block != nullptr) { @@ -3942,39 +3911,6 @@ irValue *ir_gen_anonymous_proc_lit(irModule *m, String prefix_name, AstNode *exp return value; } -void ir_check_type_and_gen_for_proc_lit(irProcedure *proc, Type *t) { - if (t == nullptr) return; - if (t->kind == Type_Struct && t->Struct.has_proc_default_values) { - for_array(i, t->Struct.fields) { - Entity *f = t->Struct.fields[i]; - if (f->kind == Entity_Variable && f->Variable.default_value.kind == ExactValue_Procedure) { - AstNode *expr = f->Variable.default_value.value_procedure; - GB_ASSERT(expr != nullptr); - if (expr->kind == AstNode_ProcLit) { - ir_gen_anonymous_proc_lit(proc->module, proc->name, expr, proc); - } - } - } - } -} - -void ir_check_type_and_gen_for_proc_lit(irModule *m, String prefix_name, Type *t) { - if (t == nullptr) return; - if (t->kind == Type_Struct && t->Struct.has_proc_default_values) { - for_array(i, t->Struct.fields) { - Entity *f = t->Struct.fields[i]; - if (f->kind == Entity_Variable && f->Variable.default_value.kind == ExactValue_Procedure) { - AstNode *expr = f->Variable.default_value.value_procedure; - GB_ASSERT(expr != nullptr); - if (expr->kind == AstNode_ProcLit) { - ir_gen_anonymous_proc_lit(m, prefix_name, expr); - } - } - } - } -} - - void ir_gen_global_type_name(irModule *m, Entity *e, String name) { if (e->type == nullptr) return; @@ -4037,8 +3973,6 @@ void ir_gen_global_type_name(irModule *m, Entity *e, String name) { // } // } // } - - ir_check_type_and_gen_for_proc_lit(m, e->token.string, bt); } @@ -4376,11 +4310,6 @@ irValue *ir_build_builtin_proc(irProcedure *proc, AstNode *expr, TypeAndValue tv irValue *call = ir_emit_global_call(proc, "alloc", args); irValue *ptr = ir_emit_conv(proc, call, elem_ptr_type); - - if (ir_type_has_default_values(elem_type)) { - ir_init_data_with_defaults(proc, ptr, len, expr); - } - irValue *slice = ir_add_local_generated(proc, type); ir_fill_slice(proc, slice, ptr, len); return ir_emit_load(proc, slice); @@ -4427,11 +4356,6 @@ irValue *ir_build_builtin_proc(irProcedure *proc, AstNode *expr, TypeAndValue tv args[4] = cap; args[5] = ir_emit_source_code_location(proc, ce->args[0]); ir_emit_global_call(proc, "__dynamic_array_make", args); - - if (ir_type_has_default_values(elem_type)) { - ir_init_data_with_defaults(proc, ir_dynamic_array_elem(proc, ir_emit_load(proc, array)), len, expr); - } - return ir_emit_load(proc, array); } break; @@ -6660,8 +6584,6 @@ void ir_build_stmt_internal(irProcedure *proc, AstNode *node) { lval = ir_build_addr(proc, name); } array_add(&lvals, lval); - - ir_check_type_and_gen_for_proc_lit(proc, ir_addr_type(lval)); } for_array(i, vd->values) { @@ -7627,8 +7549,6 @@ void ir_build_proc(irValue *value, irProcedure *parent) { if (expr->kind == AstNode_ProcLit) { ir_gen_anonymous_proc_lit(proc->module, proc->name, expr, proc); } - } else { - ir_check_type_and_gen_for_proc_lit(proc, f->type); } } } @@ -8406,20 +8326,6 @@ void ir_gen_tree(irGen *s) { var.var = g; var.decl = decl; - - if (e->type->kind == Type_Struct && e->type->Struct.has_proc_default_values) { - for_array(i, e->type->Struct.fields) { - Entity *f = e->type->Struct.fields[i]; - if (f->kind == Entity_Variable && f->Variable.default_value.kind == ExactValue_Procedure) { - AstNode *expr = f->Variable.default_value.value_procedure; - GB_ASSERT(expr != nullptr); - if (expr->kind == AstNode_ProcLit) { - ir_gen_anonymous_proc_lit(m, e->token.string, expr); - } - } - } - } - if (decl->init_expr != nullptr && !is_type_any(e->type)) { TypeAndValue tav = type_and_value_of_expr(info, decl->init_expr); if (tav.mode != Addressing_Invalid) { diff --git a/src/ir_print.cpp b/src/ir_print.cpp index 1d98557d4..4375cd7e1 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -308,16 +308,16 @@ void ir_print_type(irFileBuffer *f, irModule *m, Type *t, bool in_struct) { case Basic_b32: ir_write_str_lit(f, "i32"); return; case Basic_b64: ir_write_str_lit(f, "i64"); return; - case Basic_i8: ir_write_str_lit(f, "i8"); return; - case Basic_u8: ir_write_str_lit(f, "i8"); return; - case Basic_i16: ir_write_str_lit(f, "i16"); return; - case Basic_u16: ir_write_str_lit(f, "i16"); return; - case Basic_i32: ir_write_str_lit(f, "i32"); return; - case Basic_u32: ir_write_str_lit(f, "i32"); return; - case Basic_i64: ir_write_str_lit(f, "i64"); return; - case Basic_u64: ir_write_str_lit(f, "i64"); return; + case Basic_i8: ir_write_str_lit(f, "i8"); return; + case Basic_u8: ir_write_str_lit(f, "i8"); return; + case Basic_i16: ir_write_str_lit(f, "i16"); return; + case Basic_u16: ir_write_str_lit(f, "i16"); return; + case Basic_i32: ir_write_str_lit(f, "i32"); return; + case Basic_u32: ir_write_str_lit(f, "i32"); return; + case Basic_i64: ir_write_str_lit(f, "i64"); return; + case Basic_u64: ir_write_str_lit(f, "i64"); return; - case Basic_rune: ir_write_str_lit(f, "i32"); return; + case Basic_rune: ir_write_str_lit(f, "i32"); return; case Basic_int: case Basic_uint: @@ -503,11 +503,7 @@ void ir_print_compound_element(irFileBuffer *f, irModule *m, ExactValue v, Type ir_write_byte(f, ' '); if (v.kind == ExactValue_Invalid || !elem_type_can_be_constant(elem_type)) { - if (ir_type_has_default_values(elem_type)) { - ir_print_exact_value(f, m, v, elem_type); - } else { - ir_fprintf(f, "zeroinitializer"); - } + ir_fprintf(f, "zeroinitializer"); } else { ir_print_exact_value(f, m, v, elem_type); } @@ -675,13 +671,8 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type * Type *elem_type = type->Array.elem; isize elem_count = cl->elems.count; - bool has_defaults = ir_type_has_default_values(type); if (elem_count == 0) { - if (!has_defaults) { - ir_write_str_lit(f, "zeroinitializer"); - } else { - ir_print_exact_value(f, m, empty_exact_value, type); - } + ir_write_str_lit(f, "zeroinitializer"); break; } GB_ASSERT_MSG(elem_count == type->Array.count, "%td != %td", elem_count, type->Array.count); @@ -706,8 +697,7 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type * ast_node(cl, CompoundLit, value.value_compound); - bool has_defaults = ir_type_has_default_values(type); - if (cl->elems.count == 0 && !has_defaults) { + if (cl->elems.count == 0) { ir_write_str_lit(f, "zeroinitializer"); break; } @@ -748,18 +738,6 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type * } } - for (isize i = 0; i < value_count; i++) { - if (visited[i]) continue; - Entity *f = type->Struct.fields[i]; - ExactValue v = {}; - if (!f->Variable.default_is_nil) { - v = f->Variable.default_value; - } - values[i] = v; - } - - - if (type->Struct.is_packed) ir_write_byte(f, '<'); ir_write_byte(f, '{'); if (type->Struct.custom_align > 0) { @@ -773,13 +751,7 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type * for (isize i = 0; i < value_count; i++) { if (i > 0) ir_write_string(f, str_lit(", ")); Entity *e = type->Struct.fields[i]; - - if (!visited[i] && e->Variable.default_is_undef) { - ir_print_type(f, m, e->type); - ir_write_str_lit(f, " undef"); - } else { - ir_print_compound_element(f, m, values[i], e->type); - } + ir_print_compound_element(f, m, values[i], e->type); } @@ -809,63 +781,10 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type * ir_print_value(f, m, val, type); break; } - default: { - bool has_defaults = ir_type_has_default_values(type); - if (!has_defaults) { - ir_write_str_lit(f, "zeroinitializer"); - } else { - if (is_type_struct(type)) { - i32 value_count = cast(i32)type->Struct.fields.count; - if (type->Struct.is_packed) ir_write_byte(f, '<'); - ir_write_byte(f, '{'); - if (type->Struct.custom_align > 0) { - ir_fprintf(f, "[0 x <%lld x i8>] zeroinitializer", cast(i64)type->Struct.custom_align); - if (value_count > 0) { - ir_write_string(f, str_lit(", ")); - } - } - - for (isize i = 0; i < value_count; i++) { - if (i > 0) ir_write_string(f, str_lit(", ")); - Entity *e = type->Struct.fields[i]; - if (e->Variable.default_is_undef) { - ir_print_type(f, m, e->type); - ir_write_str_lit(f, " undef"); - } else { - ExactValue value = {}; - if (!e->Variable.default_is_nil) { - value = e->Variable.default_value; - } - ir_print_compound_element(f, m, value, e->type); - } - } - - ir_write_byte(f, '}'); - if (type->Struct.is_packed) ir_write_byte(f, '>'); - - } else if (is_type_array(type)) { - i64 count = type->Array.count; - if (count == 0) { - ir_write_str_lit(f, "zeroinitializer"); - } else { - Type *elem = type->Array.elem; - ir_write_byte(f, '['); - for (i64 i = 0; i < count; i++) { - if (i > 0) ir_write_string(f, str_lit(", ")); - ir_print_type(f, m, elem); - ir_write_byte(f, ' '); - ir_print_exact_value(f, m, empty_exact_value, elem); - } - ir_write_byte(f, ']'); - } - } else { - GB_PANIC("Unknown type for default values"); - } - } - // GB_PANIC("Invalid ExactValue: %d", value.kind); + default: + ir_write_str_lit(f, "zeroinitializer"); break; } - } } void ir_print_block_name(irFileBuffer *f, irBlock *b) { @@ -1873,11 +1792,7 @@ void print_llvm_ir(irGen *ir) { if (g->value != nullptr) { ir_print_value(f, m, g->value, g->entity->type); } else { - if (ir_type_has_default_values(g->entity->type)) { - ir_print_exact_value(f, m, empty_exact_value, g->entity->type); - } else { - ir_write_string(f, str_lit("zeroinitializer")); - } + ir_write_string(f, str_lit("zeroinitializer")); } } ir_write_byte(f, '\n'); diff --git a/src/parser.cpp b/src/parser.cpp index c30ca4779..c32af2427 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -2952,6 +2952,7 @@ AstNode *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_flags, Tok default_value = parse_expr(f, false); if (!allow_default_parameters) { syntax_error(f->curr_token, "Default parameters are only allowed for procedures"); + default_value = nullptr; } } @@ -2959,12 +2960,10 @@ AstNode *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_flags, Tok syntax_error(f->curr_token, "Default parameters can only be applied to single values"); } - #if defined(NO_DEFAULT_STRUCT_VALUES) if (allowed_flags == FieldFlag_Struct && default_value != nullptr) { syntax_error(default_value, "Default parameters are not allowed for structs"); default_value = nullptr; } - #endif if (type != nullptr && type->kind == AstNode_Ellipsis) { if (seen_ellipsis) syntax_error(type, "Extra variadic parameter after ellipsis"); diff --git a/src/types.cpp b/src/types.cpp index efba72e57..7a40f9bff 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -91,7 +91,6 @@ struct TypeStruct { bool is_raw_union; bool is_polymorphic; bool is_poly_specialized; - bool has_proc_default_values; Type * polymorphic_params; // Type_Tuple Type * polymorphic_parent;