From db928915878c64f9e5c479739bd768bd4ac0b912 Mon Sep 17 00:00:00 2001 From: Jimmi HC Date: Sun, 6 Jan 2019 17:48:31 +0100 Subject: [PATCH 1/5] Respect the type system instead of ConstExprValue when getting field --- src/ir.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 400b07368..d74bef242 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -14577,8 +14577,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_ return ira->codegen->invalid_instruction; if (type_is_invalid(struct_val->type)) return ira->codegen->invalid_instruction; - ConstExprValue *field_val = &struct_val->data.x_struct.fields[field->src_index]; - ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field_val->type, + ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field->type_entry, is_const, is_volatile, PtrLenSingle, align_bytes, (uint32_t)(ptr_bit_offset + field->bit_offset_in_host), (uint32_t)host_int_bytes_for_result_type); From e410b1f915974fe3daeebae324e8c4e4b42090dd Mon Sep 17 00:00:00 2001 From: Jimmi HC Date: Sun, 6 Jan 2019 17:49:24 +0100 Subject: [PATCH 2/5] Implemented buf_read_value_bytes for ZigTypeIdArray * Only for x_array.special == ConstArraySpecialNone --- src/ir.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index d74bef242..2353bb108 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -20179,8 +20179,29 @@ static Error buf_read_value_bytes(IrAnalyze *ira, AstNode *source_node, uint8_t val->data.x_ptr.data.hard_coded_addr.addr = bigint_as_unsigned(&bn); return ErrorNone; } - case ZigTypeIdArray: - zig_panic("TODO buf_read_value_bytes array type"); + case ZigTypeIdArray: { + uint64_t elem_size = type_size(ira->codegen, val->type->data.array.child_type); + size_t len = val->type->data.array.len; + + switch (val->data.x_array.special) { + case ConstArraySpecialNone: + val->data.x_array.data.s_none.elements = create_const_vals(len); + for (size_t i = 0; i < len; i++) { + ConstExprValue *elem = &val->data.x_array.data.s_none.elements[i]; + elem->special = ConstValSpecialStatic; + elem->type = val->type->data.array.child_type; + if ((err = buf_read_value_bytes(ira, source_node, buf + (elem_size * i), elem))) + return err; + } + break; + case ConstArraySpecialUndef: + zig_panic("TODO buf_read_value_bytes ConstArraySpecialUndef array type"); + case ConstArraySpecialBuf: + zig_panic("TODO buf_read_value_bytes ConstArraySpecialBuf array type"); + } + + return ErrorNone; + } case ZigTypeIdStruct: switch (val->type->data.structure.layout) { case ContainerLayoutAuto: { From 55e95daf543a27961fe9ca7a998d3cbd25d2973b Mon Sep 17 00:00:00 2001 From: Jimmi HC Date: Sun, 6 Jan 2019 17:53:34 +0100 Subject: [PATCH 3/5] Fixed issue where TypeInfo would use types from a prev CodeGen instance When doing multible codegen passes (such as building compiler_rt and then something else) the TypeInfo cache code would point to types from the prev code gen (such as the prev 'bool' type), giving us errors like "expected type 'bool', but found type 'bool'" This disabling of caching might have a performance hit, but correctness is better than speed, so let's have this for now, until someone optimizes this correctly (probably in stage2) --- src/ir.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 2353bb108..4e401b724 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -16882,16 +16882,12 @@ static void ensure_field_index(ZigType *type, const char *field_name, size_t ind static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, ZigType *root) { Error err; - static ConstExprValue *type_info_var = nullptr; // TODO oops this global variable made it past code review - static ZigType *type_info_type = nullptr; // TODO oops this global variable made it past code review - if (type_info_var == nullptr) { - type_info_var = get_builtin_value(ira->codegen, "TypeInfo"); - assert(type_info_var->type->id == ZigTypeIdMetaType); + ConstExprValue *type_info_var = get_builtin_value(ira->codegen, "TypeInfo"); // TODO oops this global variable made it past code review + assert(type_info_var->type->id == ZigTypeIdMetaType); + assertNoError(ensure_complete_type(ira->codegen, type_info_var->data.x_type)); - assertNoError(ensure_complete_type(ira->codegen, type_info_var->data.x_type)); - type_info_type = type_info_var->data.x_type; - assert(type_info_type->id == ZigTypeIdUnion); - } + ZigType *type_info_type = type_info_var->data.x_type; // TODO oops this global variable made it past code review + assert(type_info_type->id == ZigTypeIdUnion); if (type_name == nullptr && root == nullptr) return type_info_type; From 97702988d1783db6840c642809a41d8f176bb500 Mon Sep 17 00:00:00 2001 From: Jimmi HC Date: Sun, 6 Jan 2019 17:58:00 +0100 Subject: [PATCH 4/5] Added test case --- test/cases/ptrcast.zig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/cases/ptrcast.zig b/test/cases/ptrcast.zig index 6f0e6e594..54c3dda84 100644 --- a/test/cases/ptrcast.zig +++ b/test/cases/ptrcast.zig @@ -34,3 +34,19 @@ fn testReinterpretBytesAsExternStruct() void { var val = ptr.c; assertOrPanic(val == 5); } + +test "reinterpret struct field at comptime" { + const numLittle = comptime Bytes.init(0x12345678); + assertOrPanic(std.mem.eql(u8, []u8{ 0x78, 0x56, 0x34, 0x12 }, numLittle.bytes)); +} + +const Bytes = struct { + bytes: [4]u8, + + pub fn init(v: u32) Bytes { + var res: Bytes = undefined; + @ptrCast(*align(1) u32, &res.bytes).* = v; + + return res; + } +}; From da08525bb31c3608667254132b65e409f5869a03 Mon Sep 17 00:00:00 2001 From: Jimmi HC Date: Sun, 6 Jan 2019 18:02:00 +0100 Subject: [PATCH 5/5] Removed oops comments --- src/ir.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 4e401b724..531b70553 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -16882,11 +16882,11 @@ static void ensure_field_index(ZigType *type, const char *field_name, size_t ind static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, ZigType *root) { Error err; - ConstExprValue *type_info_var = get_builtin_value(ira->codegen, "TypeInfo"); // TODO oops this global variable made it past code review + ConstExprValue *type_info_var = get_builtin_value(ira->codegen, "TypeInfo"); assert(type_info_var->type->id == ZigTypeIdMetaType); assertNoError(ensure_complete_type(ira->codegen, type_info_var->data.x_type)); - ZigType *type_info_type = type_info_var->data.x_type; // TODO oops this global variable made it past code review + ZigType *type_info_type = type_info_var->data.x_type; assert(type_info_type->id == ZigTypeIdUnion); if (type_name == nullptr && root == nullptr)