From 6c89f96df1484fc5017eb484f0f8270ab4be341f Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Mon, 25 Nov 2019 17:18:56 -0500 Subject: [PATCH] stage1: consolodate interning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - merge const_void_val → intern.x_void - move const_zero_byte → intern.zero_byte - wrap intern access --- src/all_types.hpp | 13 ++++++--- src/analyze.cpp | 2 +- src/codegen.cpp | 59 +++++++++++++++++++++++++++++++--------- src/ir.cpp | 14 +++++----- src/memory_profiling.cpp | 1 + src/memory_profiling.hpp | 1 + 6 files changed, 65 insertions(+), 25 deletions(-) diff --git a/src/all_types.hpp b/src/all_types.hpp index cbe875113f..5b062efc9a 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -2021,12 +2021,19 @@ struct CodeGen { ZigType *entry_any_frame; } builtin_types; - struct { + struct Intern { ZigValue x_undefined; ZigValue x_void; ZigValue x_null; ZigValue x_unreachable; - } intern_values; + ZigValue zero_byte; + + ZigValue *for_undefined(); + ZigValue *for_void(); + ZigValue *for_null(); + ZigValue *for_unreachable(); + ZigValue *for_zero_byte(); + } intern; ZigType *align_amt_type; ZigType *stack_trace_type; @@ -2050,8 +2057,6 @@ struct CodeGen { IrInstruction *invalid_instruction; IrInstruction *unreach_instruction; - ZigValue const_zero_byte; - ZigValue const_void_val; ZigValue panic_msg_vals[PanicMsgIdCount]; // The function definitions this module includes. diff --git a/src/analyze.cpp b/src/analyze.cpp index e47b67a495..01a65ec0bf 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -5647,7 +5647,7 @@ void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str) { // first we build the underlying array ZigValue *array_val = create_const_vals(1); array_val->special = ConstValSpecialStatic; - array_val->type = get_array_type(g, g->builtin_types.entry_u8, buf_len(str), &g->const_zero_byte); + array_val->type = get_array_type(g, g->builtin_types.entry_u8, buf_len(str), g->intern.for_zero_byte()); array_val->data.x_array.special = ConstArraySpecialBuf; array_val->data.x_array.data.s_buf = str; diff --git a/src/codegen.cpp b/src/codegen.cpp index 30ac5af4a1..327ab76425 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -8010,29 +8010,36 @@ static void define_builtin_types(CodeGen *g) { static void define_intern_values(CodeGen *g) { { - auto& value = g->intern_values.x_undefined; + auto& value = g->intern.x_undefined; value.type = g->builtin_types.entry_undef; value.global_refs = allocate(1, "ConstGlobalRefs.undefined"); value.special = ConstValSpecialStatic; } { - auto& value = g->intern_values.x_void; + auto& value = g->intern.x_void; value.type = g->builtin_types.entry_void; value.global_refs = allocate(1, "ConstGlobalRefs.void"); value.special = ConstValSpecialStatic; } { - auto& value = g->intern_values.x_null; + auto& value = g->intern.x_null; value.type = g->builtin_types.entry_null; value.global_refs = allocate(1, "ConstGlobalRefs.null"); value.special = ConstValSpecialStatic; } { - auto& value = g->intern_values.x_unreachable; + auto& value = g->intern.x_unreachable; value.type = g->builtin_types.entry_unreachable; value.global_refs = allocate(1, "ConstGlobalRefs.unreachable"); value.special = ConstValSpecialStatic; } + { + auto& value = g->intern.zero_byte; + value.type = g->builtin_types.entry_u8; + value.global_refs = allocate(1, "ConstGlobalRefs.zero_byte"); + value.special = ConstValSpecialStatic; + bigint_init_unsigned(&value.data.x_bigint, 0); + } } static BuiltinFnEntry *create_builtin_fn(CodeGen *g, BuiltinFnId id, const char *name, size_t count) { @@ -8669,15 +8676,6 @@ static void init(CodeGen *g) { g->unreach_instruction->value->type = g->builtin_types.entry_unreachable; g->unreach_instruction->value->global_refs = allocate(1); - g->const_void_val.special = ConstValSpecialStatic; - g->const_void_val.type = g->builtin_types.entry_void; - g->const_void_val.global_refs = allocate(1); - - g->const_zero_byte.special = ConstValSpecialStatic; - g->const_zero_byte.type = g->builtin_types.entry_u8; - g->const_zero_byte.global_refs = allocate(1); - bigint_init_unsigned(&g->const_zero_byte.data.x_bigint, 0); - { ConstGlobalRefs *global_refs = allocate(PanicMsgIdCount); for (size_t i = 0; i < PanicMsgIdCount; i += 1) { @@ -10564,3 +10562,38 @@ void codegen_switch_sub_prog_node(CodeGen *g, Stage2ProgressNode *node) { } g->sub_progress_node = node; } + +ZigValue *CodeGen::Intern::for_undefined() { +#ifdef ZIG_ENABLE_MEM_PROFILE + memprof_intern_count.x_undefined += 1; +#endif + return &this->x_undefined; +} + +ZigValue *CodeGen::Intern::for_void() { +#ifdef ZIG_ENABLE_MEM_PROFILE + memprof_intern_count.x_void += 1; +#endif + return &this->x_void; +} + +ZigValue *CodeGen::Intern::for_null() { +#ifdef ZIG_ENABLE_MEM_PROFILE + memprof_intern_count.x_null += 1; +#endif + return &this->x_null; +} + +ZigValue *CodeGen::Intern::for_unreachable() { +#ifdef ZIG_ENABLE_MEM_PROFILE + memprof_intern_count.x_unreachable += 1; +#endif + return &this->x_unreachable; +} + +ZigValue *CodeGen::Intern::for_zero_byte() { +#ifdef ZIG_ENABLE_MEM_PROFILE + memprof_intern_count.zero_byte += 1; +#endif + return &this->zero_byte; +} diff --git a/src/ir.cpp b/src/ir.cpp index f4bcfb6330..bb614982e1 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -1235,7 +1235,7 @@ static IrInstruction *ir_build_const_void(IrBuilder *irb, Scope *scope, AstNode #endif IrInstructionConst *const_instruction = ir_create_instruction_noval(irb, scope, source_node); ir_instruction_append(irb->current_basic_block, &const_instruction->base); - const_instruction->base.value = &irb->codegen->intern_values.x_void; + const_instruction->base.value = irb->codegen->intern.for_void(); return &const_instruction->base; } @@ -1245,7 +1245,7 @@ static IrInstruction *ir_build_const_undefined(IrBuilder *irb, Scope *scope, Ast #endif IrInstructionConst *const_instruction = ir_create_instruction_noval(irb, scope, source_node); ir_instruction_append(irb->current_basic_block, &const_instruction->base); - const_instruction->base.value = &irb->codegen->intern_values.x_undefined; + const_instruction->base.value = irb->codegen->intern.for_undefined(); return &const_instruction->base; } @@ -1279,7 +1279,7 @@ static IrInstruction *ir_build_const_null(IrBuilder *irb, Scope *scope, AstNode #endif IrInstructionConst *const_instruction = ir_create_instruction_noval(irb, scope, source_node); ir_instruction_append(irb->current_basic_block, &const_instruction->base); - const_instruction->base.value = &irb->codegen->intern_values.x_null; + const_instruction->base.value = irb->codegen->intern.for_null(); return &const_instruction->base; } @@ -11464,7 +11464,7 @@ static IrInstruction *ir_const_unreachable(IrAnalyze *ira, IrInstruction *source memprof_intern_count.x_unreachable += 1; #endif IrInstruction *result = ir_const_noval(ira, source_instruction); - result->value = &ira->codegen->intern_values.x_unreachable; + result->value = ira->codegen->intern.for_unreachable(); return result; } @@ -11473,7 +11473,7 @@ static IrInstruction *ir_const_void(IrAnalyze *ira, IrInstruction *source_instru memprof_intern_count.x_void += 1; #endif IrInstruction *result = ir_const_noval(ira, source_instruction); - result->value = &ira->codegen->intern_values.x_void; + result->value = ira->codegen->intern.for_void(); return result; } @@ -18493,7 +18493,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct if (var) { return ir_get_var_ptr(ira, &elem_ptr_instruction->base, var); } else { - return ir_get_const_ptr(ira, &elem_ptr_instruction->base, &ira->codegen->const_void_val, + return ir_get_const_ptr(ira, &elem_ptr_instruction->base, ira->codegen->intern.for_void(), ira->codegen->builtin_types.entry_void, ConstPtrMutComptimeConst, is_const, is_volatile, 0); } } else if (array_type->id == ZigTypeIdVector) { @@ -21856,7 +21856,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr case ZigTypeIdNull: case ZigTypeIdArgTuple: case ZigTypeIdOpaque: - result = &ira->codegen->const_void_val; + result = ira->codegen->intern.for_void(); break; case ZigTypeIdInt: { diff --git a/src/memory_profiling.cpp b/src/memory_profiling.cpp index 7457ba9f3f..a44dfc4450 100644 --- a/src/memory_profiling.cpp +++ b/src/memory_profiling.cpp @@ -141,6 +141,7 @@ void memprof_dump_stats(FILE *file) { fprintf(stderr, "void: interned %zu times\n", memprof_intern_count.x_void); fprintf(stderr, "null: interned %zu times\n", memprof_intern_count.x_null); fprintf(stderr, "unreachable: interned %zu times\n", memprof_intern_count.x_unreachable); + fprintf(stderr, "zero_byte: interned %zu times\n", memprof_intern_count.zero_byte); } #endif diff --git a/src/memory_profiling.hpp b/src/memory_profiling.hpp index 56321564d0..78e77a89f1 100644 --- a/src/memory_profiling.hpp +++ b/src/memory_profiling.hpp @@ -18,6 +18,7 @@ struct MemprofInternCount { size_t x_void; size_t x_null; size_t x_unreachable; + size_t zero_byte; }; extern MemprofInternCount memprof_intern_count;