| ... | ... | @@ -159,7 +159,7 @@ static ZigType *ir_resolve_atomic_operand_type(IrAnalyze *ira, IrInstruction *op |
| 159 | 159 | static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *value, LVal lval); |
| 160 | 160 | static ZigType *adjust_ptr_align(CodeGen *g, ZigType *ptr_type, uint32_t new_align); |
| 161 | 161 | static ZigType *adjust_slice_align(CodeGen *g, ZigType *slice_type, uint32_t new_align); |
| 162 | | static void buf_read_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue *val); |
| 162 | static Error buf_read_value_bytes(IrAnalyze *ira, AstNode *source_node, uint8_t *buf, ConstExprValue *val); |
| 163 | 163 | static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue *val); |
| 164 | 164 | static Error ir_read_const_ptr(IrAnalyze *ira, AstNode *source_node, |
| 165 | 165 | ConstExprValue *out_val, ConstExprValue *ptr_val); |
| ... | ... | @@ -13725,7 +13725,8 @@ static Error ir_read_const_ptr(IrAnalyze *ira, AstNode *source_node, |
| 13725 | 13725 | Buf buf = BUF_INIT; |
| 13726 | 13726 | buf_resize(&buf, src_size); |
| 13727 | 13727 | buf_write_value_bytes(ira->codegen, (uint8_t*)buf_ptr(&buf), pointee); |
| 13728 | | buf_read_value_bytes(ira->codegen, (uint8_t*)buf_ptr(&buf), out_val); |
| 13728 | if ((err = buf_read_value_bytes(ira, source_node, (uint8_t*)buf_ptr(&buf), out_val))) |
| 13729 | return err; |
| 13729 | 13730 | return ErrorNone; |
| 13730 | 13731 | } |
| 13731 | 13732 | |
| ... | ... | @@ -13759,7 +13760,8 @@ static Error ir_read_const_ptr(IrAnalyze *ira, AstNode *source_node, |
| 13759 | 13760 | ConstExprValue *elem_val = &array_val->data.x_array.data.s_none.elements[elem_index + i]; |
| 13760 | 13761 | buf_write_value_bytes(ira->codegen, (uint8_t*)buf_ptr(&buf) + (i * elem_size), elem_val); |
| 13761 | 13762 | } |
| 13762 | | buf_read_value_bytes(ira->codegen, (uint8_t*)buf_ptr(&buf), out_val); |
| 13763 | if ((err = buf_read_value_bytes(ira, source_node, (uint8_t*)buf_ptr(&buf), out_val))) |
| 13764 | return err; |
| 13763 | 13765 | return ErrorNone; |
| 13764 | 13766 | } |
| 13765 | 13767 | case ConstPtrSpecialBaseStruct: |
| ... | ... | @@ -20077,7 +20079,8 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue |
| 20077 | 20079 | zig_unreachable(); |
| 20078 | 20080 | } |
| 20079 | 20081 | |
| 20080 | | static void buf_read_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue *val) { |
| 20082 | static Error buf_read_value_bytes(IrAnalyze *ira, AstNode *source_node, uint8_t *buf, ConstExprValue *val) { |
| 20083 | Error err; |
| 20081 | 20084 | assert(val->special == ConstValSpecialStatic); |
| 20082 | 20085 | switch (val->type->id) { |
| 20083 | 20086 | case ZigTypeIdInvalid: |
| ... | ... | @@ -20094,30 +20097,60 @@ static void buf_read_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue |
| 20094 | 20097 | case ZigTypeIdPromise: |
| 20095 | 20098 | zig_unreachable(); |
| 20096 | 20099 | case ZigTypeIdVoid: |
| 20097 | | return; |
| 20100 | return ErrorNone; |
| 20098 | 20101 | case ZigTypeIdBool: |
| 20099 | 20102 | val->data.x_bool = (buf[0] != 0); |
| 20100 | | return; |
| 20103 | return ErrorNone; |
| 20101 | 20104 | case ZigTypeIdInt: |
| 20102 | 20105 | bigint_read_twos_complement(&val->data.x_bigint, buf, val->type->data.integral.bit_count, |
| 20103 | | codegen->is_big_endian, val->type->data.integral.is_signed); |
| 20104 | | return; |
| 20106 | ira->codegen->is_big_endian, val->type->data.integral.is_signed); |
| 20107 | return ErrorNone; |
| 20105 | 20108 | case ZigTypeIdFloat: |
| 20106 | | float_read_ieee597(val, buf, codegen->is_big_endian); |
| 20107 | | return; |
| 20109 | float_read_ieee597(val, buf, ira->codegen->is_big_endian); |
| 20110 | return ErrorNone; |
| 20108 | 20111 | case ZigTypeIdPointer: |
| 20109 | 20112 | { |
| 20110 | 20113 | val->data.x_ptr.special = ConstPtrSpecialHardCodedAddr; |
| 20111 | 20114 | BigInt bn; |
| 20112 | | bigint_read_twos_complement(&bn, buf, codegen->builtin_types.entry_usize->data.integral.bit_count, |
| 20113 | | codegen->is_big_endian, false); |
| 20115 | bigint_read_twos_complement(&bn, buf, ira->codegen->builtin_types.entry_usize->data.integral.bit_count, |
| 20116 | ira->codegen->is_big_endian, false); |
| 20114 | 20117 | val->data.x_ptr.data.hard_coded_addr.addr = bigint_as_unsigned(&bn); |
| 20115 | | return; |
| 20118 | return ErrorNone; |
| 20116 | 20119 | } |
| 20117 | 20120 | case ZigTypeIdArray: |
| 20118 | 20121 | zig_panic("TODO buf_read_value_bytes array type"); |
| 20119 | 20122 | case ZigTypeIdStruct: |
| 20120 | | zig_panic("TODO buf_read_value_bytes struct type"); |
| 20123 | switch (val->type->data.structure.layout) { |
| 20124 | case ContainerLayoutAuto: { |
| 20125 | ErrorMsg *msg = ir_add_error_node(ira, source_node, |
| 20126 | buf_sprintf("non-extern, non-packed struct '%s' cannot have its bytes reinterpreted", |
| 20127 | buf_ptr(&val->type->name))); |
| 20128 | add_error_note(ira->codegen, msg, val->type->data.structure.decl_node, |
| 20129 | buf_sprintf("declared here")); |
| 20130 | return ErrorSemanticAnalyzeFail; |
| 20131 | } |
| 20132 | case ContainerLayoutExtern: { |
| 20133 | size_t src_field_count = val->type->data.structure.src_field_count; |
| 20134 | val->data.x_struct.fields = create_const_vals(src_field_count); |
| 20135 | for (size_t field_i = 0; field_i < src_field_count; field_i += 1) { |
| 20136 | ConstExprValue *field_val = &val->data.x_struct.fields[field_i]; |
| 20137 | field_val->special = ConstValSpecialStatic; |
| 20138 | TypeStructField *type_field = &val->type->data.structure.fields[field_i]; |
| 20139 | field_val->type = type_field->type_entry; |
| 20140 | if (type_field->gen_index == SIZE_MAX) |
| 20141 | continue; |
| 20142 | size_t offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, val->type->type_ref, |
| 20143 | type_field->gen_index); |
| 20144 | uint8_t *new_buf = buf + offset; |
| 20145 | if ((err = buf_read_value_bytes(ira, source_node, new_buf, field_val))) |
| 20146 | return err; |
| 20147 | } |
| 20148 | return ErrorNone; |
| 20149 | } |
| 20150 | case ContainerLayoutPacked: |
| 20151 | zig_panic("TODO buf_read_value_bytes packed struct"); |
| 20152 | } |
| 20153 | zig_unreachable(); |
| 20121 | 20154 | case ZigTypeIdOptional: |
| 20122 | 20155 | zig_panic("TODO buf_read_value_bytes maybe type"); |
| 20123 | 20156 | case ZigTypeIdErrorUnion: |
| ... | ... | @@ -20220,7 +20253,8 @@ static IrInstruction *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstruct |
| 20220 | 20253 | IrInstruction *result = ir_const(ira, &instruction->base, dest_type); |
| 20221 | 20254 | uint8_t *buf = allocate_nonzero<uint8_t>(src_size_bytes); |
| 20222 | 20255 | buf_write_value_bytes(ira->codegen, buf, val); |
| 20223 | | buf_read_value_bytes(ira->codegen, buf, &result->value); |
| 20256 | if ((err = buf_read_value_bytes(ira, instruction->base.source_node, buf, &result->value))) |
| 20257 | return ira->codegen->invalid_instruction; |
| 20224 | 20258 | return result; |
| 20225 | 20259 | } |
| 20226 | 20260 | |