authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-01-09 10:43:48-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-01-09 10:43:48-05:00
log5864d92b4049e6d60a21a3f22220464808dd0518
tree0609155ee5e4f4fa589e61c1dee3b4ef5bc800e1
parent4d5d0d3adad09e9ce34d281327c424de6402fcdb
signaturelock-open Commit is signed but in an unrecognized format.

when rendering llvm const values, ensure the types align

the representation of the const expr val in zig, and the type that we tell LLVM it is.

5 files changed, 102 insertions(+), 60 deletions(-)

src/analyze.cpp+19-2
......@@ -5757,7 +5757,8 @@ void render_const_val_ptr(CodeGen *g, Buf *buf, ConstExprValue *const_val, ZigTy
57575757 case ConstPtrSpecialRef:
57585758 case ConstPtrSpecialBaseStruct:
57595759 buf_appendf(buf, "*");
5760 render_const_value(g, buf, const_ptr_pointee(g, const_val));
5760 // TODO we need a source node for const_ptr_pointee because it can generate compile errors
5761 render_const_value(g, buf, const_ptr_pointee(nullptr, g, const_val, nullptr));
57615762 return;
57625763 case ConstPtrSpecialBaseArray:
57635764 if (const_val->data.x_ptr.data.base_array.is_cstr) {
......@@ -5765,7 +5766,8 @@ void render_const_val_ptr(CodeGen *g, Buf *buf, ConstExprValue *const_val, ZigTy
57655766 return;
57665767 } else {
57675768 buf_appendf(buf, "*");
5768 render_const_value(g, buf, const_ptr_pointee(g, const_val));
5769 // TODO we need a source node for const_ptr_pointee because it can generate compile errors
5770 render_const_value(g, buf, const_ptr_pointee(nullptr, g, const_val, nullptr));
57695771 return;
57705772 }
57715773 case ConstPtrSpecialHardCodedAddr:
......@@ -6599,3 +6601,18 @@ uint32_t get_host_int_bytes(CodeGen *g, ZigType *struct_type, TypeStructField *f
65996601 LLVMTypeRef field_type = LLVMStructGetTypeAtIndex(struct_type->type_ref, field->gen_index);
66006602 return LLVMStoreSizeOfType(g->target_data_ref, field_type);
66016603}
6604
6605Error ensure_const_val_repr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,
6606 ConstExprValue *const_val, ZigType *wanted_type)
6607{
6608 ConstExprValue ptr_val = {};
6609 ptr_val.special = ConstValSpecialStatic;
6610 ptr_val.type = get_pointer_to_type(codegen, wanted_type, true);
6611 ptr_val.data.x_ptr.mut = ConstPtrMutComptimeConst;
6612 ptr_val.data.x_ptr.special = ConstPtrSpecialRef;
6613 ptr_val.data.x_ptr.data.ref.pointee = const_val;
6614 if (const_ptr_pointee(ira, codegen, &ptr_val, source_node) == nullptr)
6615 return ErrorSemanticAnalyzeFail;
6616
6617 return ErrorNone;
6618}
src/analyze.hpp+3
......@@ -222,4 +222,7 @@ enum ReqCompTime {
222222};
223223ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry);
224224
225Error ensure_const_val_repr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,
226 ConstExprValue *const_val, ZigType *wanted_type);
227
225228#endif
src/codegen.cpp+8
......@@ -5626,6 +5626,8 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
56265626}
56275627
56285628static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const char *name) {
5629 Error err;
5630
56295631 ZigType *type_entry = const_val->type;
56305632 assert(!type_entry->zero_bits);
56315633
......@@ -5769,6 +5771,12 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
57695771 }
57705772 ConstExprValue *field_val = &const_val->data.x_struct.fields[i];
57715773 assert(field_val->type != nullptr);
5774 if ((err = ensure_const_val_repr(nullptr, g, nullptr, field_val,
5775 type_struct_field->type_entry)))
5776 {
5777 zig_unreachable();
5778 }
5779
57725780 LLVMValueRef val = gen_const_val(g, field_val, "");
57735781 fields[type_struct_field->gen_index] = val;
57745782 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(field_val->type, val);
src/ir.cpp+68-57
......@@ -159,9 +159,9 @@ static ZigType *ir_resolve_atomic_operand_type(IrAnalyze *ira, IrInstruction *op
159159static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *value, LVal lval);
160160static ZigType *adjust_ptr_align(CodeGen *g, ZigType *ptr_type, uint32_t new_align);
161161static ZigType *adjust_slice_align(CodeGen *g, ZigType *slice_type, uint32_t new_align);
162static Error buf_read_value_bytes(IrAnalyze *ira, AstNode *source_node, uint8_t *buf, ConstExprValue *val);
162static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node, uint8_t *buf, ConstExprValue *val);
163163static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue *val);
164static Error ir_read_const_ptr(IrAnalyze *ira, AstNode *source_node,
164static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,
165165 ConstExprValue *out_val, ConstExprValue *ptr_val);
166166static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *ptr,
167167 ZigType *dest_type, IrInstruction *dest_type_src);
......@@ -7391,35 +7391,46 @@ static ErrorMsg *ir_add_error_node(IrAnalyze *ira, AstNode *source_node, Buf *ms
73917391 return exec_add_error_node(ira->codegen, ira->new_irb.exec, source_node, msg);
73927392}
73937393
7394static ErrorMsg *opt_ir_add_error_node(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node, Buf *msg) {
7395 if (ira != nullptr)
7396 return exec_add_error_node(codegen, ira->new_irb.exec, source_node, msg);
7397 else
7398 return add_node_error(codegen, source_node, msg);
7399}
7400
73947401static ErrorMsg *ir_add_error(IrAnalyze *ira, IrInstruction *source_instruction, Buf *msg) {
73957402 return ir_add_error_node(ira, source_instruction->source_node, msg);
73967403}
73977404
73987405// This function takes a comptime ptr and makes the child const value conform to the type
73997406// described by the pointer.
7400static Error eval_comptime_ptr_reinterpret(IrAnalyze *ira, AstNode *source_node, ConstExprValue *ptr_val) {
7407static Error eval_comptime_ptr_reinterpret(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,
7408 ConstExprValue *ptr_val)
7409{
74017410 Error err;
74027411 assert(ptr_val->type->id == ZigTypeIdPointer);
74037412 ConstExprValue tmp = {};
74047413 tmp.special = ConstValSpecialStatic;
74057414 tmp.type = ptr_val->type->data.pointer.child_type;
7406 if ((err = ir_read_const_ptr(ira, source_node, &tmp, ptr_val)))
7415 if ((err = ir_read_const_ptr(ira, codegen, source_node, &tmp, ptr_val)))
74077416 return err;
7408 ConstExprValue *child_val = const_ptr_pointee_unchecked(ira->codegen, ptr_val);
7417 ConstExprValue *child_val = const_ptr_pointee_unchecked(codegen, ptr_val);
74097418 copy_const_val(child_val, &tmp, false);
74107419 return ErrorNone;
74117420}
74127421
7413static ConstExprValue *ir_const_ptr_pointee(IrAnalyze *ira, ConstExprValue *const_val, AstNode *source_node) {
7422ConstExprValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ConstExprValue *const_val,
7423 AstNode *source_node)
7424{
74147425 Error err;
7415 ConstExprValue *val = const_ptr_pointee_unchecked(ira->codegen, const_val);
7426 ConstExprValue *val = const_ptr_pointee_unchecked(codegen, const_val);
74167427 assert(val != nullptr);
74177428 assert(const_val->type->id == ZigTypeIdPointer);
74187429 ZigType *expected_type = const_val->type->data.pointer.child_type;
74197430 if (!types_have_same_zig_comptime_repr(val->type, expected_type)) {
7420 if ((err = eval_comptime_ptr_reinterpret(ira, source_node, const_val)))
7431 if ((err = eval_comptime_ptr_reinterpret(ira, codegen, source_node, const_val)))
74217432 return nullptr;
7422 return const_ptr_pointee_unchecked(ira->codegen, const_val);
7433 return const_ptr_pointee_unchecked(codegen, const_val);
74237434 }
74247435 return val;
74257436}
......@@ -9461,7 +9472,7 @@ static IrInstruction *ir_resolve_ptr_of_array_to_unknown_len_ptr(IrAnalyze *ira,
94619472 wanted_type = adjust_ptr_align(ira->codegen, wanted_type, get_ptr_align(ira->codegen, value->value.type));
94629473
94639474 if (instr_is_comptime(value)) {
9464 ConstExprValue *pointee = ir_const_ptr_pointee(ira, &value->value, source_instr->source_node);
9475 ConstExprValue *pointee = const_ptr_pointee(ira, ira->codegen, &value->value, source_instr->source_node);
94659476 if (pointee == nullptr)
94669477 return ira->codegen->invalid_instruction;
94679478 if (pointee->special != ConstValSpecialRuntime) {
......@@ -9497,7 +9508,7 @@ static IrInstruction *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInstruc
94979508 wanted_type = adjust_slice_align(ira->codegen, wanted_type, get_ptr_align(ira->codegen, value->value.type));
94989509
94999510 if (instr_is_comptime(value)) {
9500 ConstExprValue *pointee = ir_const_ptr_pointee(ira, &value->value, source_instr->source_node);
9511 ConstExprValue *pointee = const_ptr_pointee(ira, ira->codegen, &value->value, source_instr->source_node);
95019512 if (pointee == nullptr)
95029513 return ira->codegen->invalid_instruction;
95039514 if (pointee->special != ConstValSpecialRuntime) {
......@@ -10456,7 +10467,7 @@ static IrInstruction *ir_analyze_ptr_to_array(IrAnalyze *ira, IrInstruction *sou
1045610467 return ira->codegen->invalid_instruction;
1045710468
1045810469 assert(val->type->id == ZigTypeIdPointer);
10459 ConstExprValue *pointee = ir_const_ptr_pointee(ira, val, source_instr->source_node);
10470 ConstExprValue *pointee = const_ptr_pointee(ira, ira->codegen, val, source_instr->source_node);
1046010471 if (pointee == nullptr)
1046110472 return ira->codegen->invalid_instruction;
1046210473 if (pointee->special != ConstValSpecialRuntime) {
......@@ -11025,7 +11036,7 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc
1102511036 IrInstruction *result = ir_create_const(&ira->new_irb, source_instruction->scope,
1102611037 source_instruction->source_node, child_type);
1102711038
11028 if ((err = ir_read_const_ptr(ira, source_instruction->source_node, &result->value,
11039 if ((err = ir_read_const_ptr(ira, ira->codegen, source_instruction->source_node, &result->value,
1102911040 &ptr->value)))
1103011041 {
1103111042 return ira->codegen->invalid_instruction;
......@@ -13730,21 +13741,21 @@ static IrInstruction *ir_analyze_instruction_call(IrAnalyze *ira, IrInstructionC
1373013741
1373113742// out_val->type must be the type to read the pointer as
1373213743// if the type is different than the actual type then it does a comptime byte reinterpretation
13733static Error ir_read_const_ptr(IrAnalyze *ira, AstNode *source_node,
13744static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,
1373413745 ConstExprValue *out_val, ConstExprValue *ptr_val)
1373513746{
1373613747 Error err;
1373713748 assert(out_val->type != nullptr);
1373813749
13739 ConstExprValue *pointee = const_ptr_pointee_unchecked(ira->codegen, ptr_val);
13750 ConstExprValue *pointee = const_ptr_pointee_unchecked(codegen, ptr_val);
1374013751
13741 if ((err = type_resolve(ira->codegen, pointee->type, ResolveStatusSizeKnown)))
13752 if ((err = type_resolve(codegen, pointee->type, ResolveStatusSizeKnown)))
1374213753 return ErrorSemanticAnalyzeFail;
13743 if ((err = type_resolve(ira->codegen, out_val->type, ResolveStatusSizeKnown)))
13754 if ((err = type_resolve(codegen, out_val->type, ResolveStatusSizeKnown)))
1374413755 return ErrorSemanticAnalyzeFail;
1374513756
13746 size_t src_size = type_size(ira->codegen, pointee->type);
13747 size_t dst_size = type_size(ira->codegen, out_val->type);
13757 size_t src_size = type_size(codegen, pointee->type);
13758 size_t dst_size = type_size(codegen, out_val->type);
1374813759
1374913760 if (src_size == dst_size && types_have_same_zig_comptime_repr(pointee->type, out_val->type)) {
1375013761 copy_const_val(out_val, pointee, ptr_val->data.x_ptr.mut == ConstPtrMutComptimeConst);
......@@ -13754,8 +13765,8 @@ static Error ir_read_const_ptr(IrAnalyze *ira, AstNode *source_node,
1375413765 if (dst_size <= src_size) {
1375513766 Buf buf = BUF_INIT;
1375613767 buf_resize(&buf, src_size);
13757 buf_write_value_bytes(ira->codegen, (uint8_t*)buf_ptr(&buf), pointee);
13758 if ((err = buf_read_value_bytes(ira, source_node, (uint8_t*)buf_ptr(&buf), out_val)))
13768 buf_write_value_bytes(codegen, (uint8_t*)buf_ptr(&buf), pointee);
13769 if ((err = buf_read_value_bytes(ira, codegen, source_node, (uint8_t*)buf_ptr(&buf), out_val)))
1375913770 return err;
1376013771 return ErrorNone;
1376113772 }
......@@ -13764,7 +13775,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, AstNode *source_node,
1376413775 case ConstPtrSpecialInvalid:
1376513776 zig_unreachable();
1376613777 case ConstPtrSpecialRef: {
13767 ir_add_error_node(ira, source_node,
13778 opt_ir_add_error_node(ira, codegen, source_node,
1376813779 buf_sprintf("attempt to read %zu bytes from pointer to %s which is %zu bytes",
1376913780 dst_size, buf_ptr(&pointee->type->name), src_size));
1377013781 return ErrorSemanticAnalyzeFail;
......@@ -13778,7 +13789,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, AstNode *source_node,
1377813789 size_t elem_index = ptr_val->data.x_ptr.data.base_array.elem_index;
1377913790 src_size = elem_size * (array_val->type->data.array.len - elem_index);
1378013791 if (dst_size > src_size) {
13781 ir_add_error_node(ira, source_node,
13792 opt_ir_add_error_node(ira, codegen, source_node,
1378213793 buf_sprintf("attempt to read %zu bytes from %s at index %" ZIG_PRI_usize " which is %zu bytes",
1378313794 dst_size, buf_ptr(&array_val->type->name), elem_index, src_size));
1378413795 return ErrorSemanticAnalyzeFail;
......@@ -13788,9 +13799,9 @@ static Error ir_read_const_ptr(IrAnalyze *ira, AstNode *source_node,
1378813799 buf_resize(&buf, elem_count * elem_size);
1378913800 for (size_t i = 0; i < elem_count; i += 1) {
1379013801 ConstExprValue *elem_val = &array_val->data.x_array.data.s_none.elements[elem_index + i];
13791 buf_write_value_bytes(ira->codegen, (uint8_t*)buf_ptr(&buf) + (i * elem_size), elem_val);
13802 buf_write_value_bytes(codegen, (uint8_t*)buf_ptr(&buf) + (i * elem_size), elem_val);
1379213803 }
13793 if ((err = buf_read_value_bytes(ira, source_node, (uint8_t*)buf_ptr(&buf), out_val)))
13804 if ((err = buf_read_value_bytes(ira, codegen, source_node, (uint8_t*)buf_ptr(&buf), out_val)))
1379413805 return err;
1379513806 return ErrorNone;
1379613807 }
......@@ -14227,7 +14238,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1422714238 array_type = array_type->data.pointer.child_type;
1422814239 ptr_type = ptr_type->data.pointer.child_type;
1422914240 if (orig_array_ptr_val->special != ConstValSpecialRuntime) {
14230 orig_array_ptr_val = ir_const_ptr_pointee(ira, orig_array_ptr_val,
14241 orig_array_ptr_val = const_ptr_pointee(ira, ira->codegen, orig_array_ptr_val,
1423114242 elem_ptr_instruction->base.source_node);
1423214243 if (orig_array_ptr_val == nullptr)
1423314244 return ira->codegen->invalid_instruction;
......@@ -14271,7 +14282,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1427114282 ConstExprValue *ptr_val = ir_resolve_const(ira, array_ptr, UndefBad);
1427214283 if (!ptr_val)
1427314284 return ira->codegen->invalid_instruction;
14274 ConstExprValue *args_val = ir_const_ptr_pointee(ira, ptr_val, elem_ptr_instruction->base.source_node);
14285 ConstExprValue *args_val = const_ptr_pointee(ira, ira->codegen, ptr_val, elem_ptr_instruction->base.source_node);
1427514286 if (args_val == nullptr)
1427614287 return ira->codegen->invalid_instruction;
1427714288 size_t start = args_val->data.x_arg_tuple.start_index;
......@@ -14353,7 +14364,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1435314364 (orig_array_ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar ||
1435414365 array_type->id == ZigTypeIdArray))
1435514366 {
14356 ConstExprValue *array_ptr_val = ir_const_ptr_pointee(ira, orig_array_ptr_val,
14367 ConstExprValue *array_ptr_val = const_ptr_pointee(ira, ira->codegen, orig_array_ptr_val,
1435714368 elem_ptr_instruction->base.source_node);
1435814369 if (array_ptr_val == nullptr)
1435914370 return ira->codegen->invalid_instruction;
......@@ -14572,7 +14583,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1457214583 return ira->codegen->invalid_instruction;
1457314584
1457414585 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
14575 ConstExprValue *struct_val = ir_const_ptr_pointee(ira, ptr_val, source_instr->source_node);
14586 ConstExprValue *struct_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);
1457614587 if (struct_val == nullptr)
1457714588 return ira->codegen->invalid_instruction;
1457814589 if (type_is_invalid(struct_val->type))
......@@ -14614,7 +14625,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1461414625 return ira->codegen->invalid_instruction;
1461514626
1461614627 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
14617 ConstExprValue *union_val = ir_const_ptr_pointee(ira, ptr_val, source_instr->source_node);
14628 ConstExprValue *union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);
1461814629 if (union_val == nullptr)
1461914630 return ira->codegen->invalid_instruction;
1462014631 if (type_is_invalid(union_val->type))
......@@ -14811,7 +14822,7 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
1481114822 return ira->codegen->invalid_instruction;
1481214823
1481314824 assert(container_ptr->value.type->id == ZigTypeIdPointer);
14814 ConstExprValue *child_val = ir_const_ptr_pointee(ira, container_ptr_val, source_node);
14825 ConstExprValue *child_val = const_ptr_pointee(ira, ira->codegen, container_ptr_val, source_node);
1481514826 if (child_val == nullptr)
1481614827 return ira->codegen->invalid_instruction;
1481714828
......@@ -14837,7 +14848,7 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
1483714848 return ira->codegen->invalid_instruction;
1483814849
1483914850 assert(container_ptr->value.type->id == ZigTypeIdPointer);
14840 ConstExprValue *child_val = ir_const_ptr_pointee(ira, container_ptr_val, source_node);
14851 ConstExprValue *child_val = const_ptr_pointee(ira, ira->codegen, container_ptr_val, source_node);
1484114852 if (child_val == nullptr)
1484214853 return ira->codegen->invalid_instruction;
1484314854 ZigType *child_type = child_val->data.x_type;
......@@ -15112,7 +15123,7 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
1511215123 if (!container_ptr_val)
1511315124 return ira->codegen->invalid_instruction;
1511415125
15115 ConstExprValue *namespace_val = ir_const_ptr_pointee(ira, container_ptr_val,
15126 ConstExprValue *namespace_val = const_ptr_pointee(ira, ira->codegen, container_ptr_val,
1511615127 field_ptr_instruction->base.source_node);
1511715128 if (namespace_val == nullptr)
1511815129 return ira->codegen->invalid_instruction;
......@@ -15187,7 +15198,7 @@ static IrInstruction *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstruc
1518715198 }
1518815199 if (ptr->value.data.x_ptr.mut == ConstPtrMutComptimeVar) {
1518915200 if (instr_is_comptime(casted_value)) {
15190 ConstExprValue *dest_val = ir_const_ptr_pointee(ira, &ptr->value, store_ptr_instruction->base.source_node);
15201 ConstExprValue *dest_val = const_ptr_pointee(ira, ira->codegen, &ptr->value, store_ptr_instruction->base.source_node);
1519115202 if (dest_val == nullptr)
1519215203 return ira->codegen->invalid_instruction;
1519315204 if (dest_val->special != ConstValSpecialRuntime) {
......@@ -15735,7 +15746,7 @@ static IrInstruction *ir_analyze_instruction_unwrap_maybe(IrAnalyze *ira,
1573515746 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
1573615747 if (!val)
1573715748 return ira->codegen->invalid_instruction;
15738 ConstExprValue *maybe_val = ir_const_ptr_pointee(ira, val, unwrap_maybe_instruction->base.source_node);
15749 ConstExprValue *maybe_val = const_ptr_pointee(ira, ira->codegen, val, unwrap_maybe_instruction->base.source_node);
1573915750 if (maybe_val == nullptr)
1574015751 return ira->codegen->invalid_instruction;
1574115752
......@@ -16032,7 +16043,7 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1603216043 ZigType *target_type = target_value_ptr->value.type->data.pointer.child_type;
1603316044 ConstExprValue *pointee_val = nullptr;
1603416045 if (instr_is_comptime(target_value_ptr)) {
16035 pointee_val = ir_const_ptr_pointee(ira, &target_value_ptr->value, target_value_ptr->source_node);
16046 pointee_val = const_ptr_pointee(ira, ira->codegen, &target_value_ptr->value, target_value_ptr->source_node);
1603616047 if (pointee_val == nullptr)
1603716048 return ira->codegen->invalid_instruction;
1603816049
......@@ -16167,7 +16178,7 @@ static IrInstruction *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstru
1616716178 if (!target_value_ptr)
1616816179 return ira->codegen->invalid_instruction;
1616916180
16170 ConstExprValue *pointee_val = ir_const_ptr_pointee(ira, target_val_ptr, instruction->base.source_node);
16181 ConstExprValue *pointee_val = const_ptr_pointee(ira, ira->codegen, target_val_ptr, instruction->base.source_node);
1617116182 if (pointee_val == nullptr)
1617216183 return ira->codegen->invalid_instruction;
1617316184
......@@ -18882,18 +18893,18 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
1888218893 if (array_type->id == ZigTypeIdPointer) {
1888318894 ZigType *child_array_type = array_type->data.pointer.child_type;
1888418895 assert(child_array_type->id == ZigTypeIdArray);
18885 parent_ptr = ir_const_ptr_pointee(ira, &ptr_ptr->value, instruction->base.source_node);
18896 parent_ptr = const_ptr_pointee(ira, ira->codegen, &ptr_ptr->value, instruction->base.source_node);
1888618897 if (parent_ptr == nullptr)
1888718898 return ira->codegen->invalid_instruction;
1888818899
18889 array_val = ir_const_ptr_pointee(ira, parent_ptr, instruction->base.source_node);
18900 array_val = const_ptr_pointee(ira, ira->codegen, parent_ptr, instruction->base.source_node);
1889018901 if (array_val == nullptr)
1889118902 return ira->codegen->invalid_instruction;
1889218903
1889318904 rel_end = child_array_type->data.array.len;
1889418905 abs_offset = 0;
1889518906 } else {
18896 array_val = ir_const_ptr_pointee(ira, &ptr_ptr->value, instruction->base.source_node);
18907 array_val = const_ptr_pointee(ira, ira->codegen, &ptr_ptr->value, instruction->base.source_node);
1889718908 if (array_val == nullptr)
1889818909 return ira->codegen->invalid_instruction;
1889918910 rel_end = array_type->data.array.len;
......@@ -18902,7 +18913,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
1890218913 }
1890318914 } else if (array_type->id == ZigTypeIdPointer) {
1890418915 assert(array_type->data.pointer.ptr_len == PtrLenUnknown);
18905 parent_ptr = ir_const_ptr_pointee(ira, &ptr_ptr->value, instruction->base.source_node);
18916 parent_ptr = const_ptr_pointee(ira, ira->codegen, &ptr_ptr->value, instruction->base.source_node);
1890618917 if (parent_ptr == nullptr)
1890718918 return ira->codegen->invalid_instruction;
1890818919
......@@ -18942,7 +18953,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
1894218953 zig_panic("TODO slice of ptr cast from function");
1894318954 }
1894418955 } else if (is_slice(array_type)) {
18945 ConstExprValue *slice_ptr = ir_const_ptr_pointee(ira, &ptr_ptr->value, instruction->base.source_node);
18956 ConstExprValue *slice_ptr = const_ptr_pointee(ira, ira->codegen, &ptr_ptr->value, instruction->base.source_node);
1894618957 if (slice_ptr == nullptr)
1894718958 return ira->codegen->invalid_instruction;
1894818959
......@@ -19351,7 +19362,7 @@ static IrInstruction *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstr
1935119362 {
1935219363 BigInt *op1_bigint = &casted_op1->value.data.x_bigint;
1935319364 BigInt *op2_bigint = &casted_op2->value.data.x_bigint;
19354 ConstExprValue *pointee_val = ir_const_ptr_pointee(ira, &casted_result_ptr->value, casted_result_ptr->source_node);
19365 ConstExprValue *pointee_val = const_ptr_pointee(ira, ira->codegen, &casted_result_ptr->value, casted_result_ptr->source_node);
1935519366 if (pointee_val == nullptr)
1935619367 return ira->codegen->invalid_instruction;
1935719368 BigInt *dest_bigint = &pointee_val->data.x_bigint;
......@@ -19450,7 +19461,7 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira,
1945019461 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
1945119462 if (!ptr_val)
1945219463 return ira->codegen->invalid_instruction;
19453 ConstExprValue *err_union_val = ir_const_ptr_pointee(ira, ptr_val, instruction->base.source_node);
19464 ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, instruction->base.source_node);
1945419465 if (err_union_val == nullptr)
1945519466 return ira->codegen->invalid_instruction;
1945619467 if (err_union_val->special != ConstValSpecialRuntime) {
......@@ -19502,7 +19513,7 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
1950219513 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
1950319514 if (!ptr_val)
1950419515 return ira->codegen->invalid_instruction;
19505 ConstExprValue *err_union_val = ir_const_ptr_pointee(ira, ptr_val, instruction->base.source_node);
19516 ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, instruction->base.source_node);
1950619517 if (err_union_val == nullptr)
1950719518 return ira->codegen->invalid_instruction;
1950819519 if (err_union_val->special != ConstValSpecialRuntime) {
......@@ -20132,7 +20143,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
2013220143 zig_unreachable();
2013320144}
2013420145
20135static Error buf_read_value_bytes(IrAnalyze *ira, AstNode *source_node, uint8_t *buf, ConstExprValue *val) {
20146static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node, uint8_t *buf, ConstExprValue *val) {
2013620147 Error err;
2013720148 assert(val->special == ConstValSpecialStatic);
2013820149 switch (val->type->id) {
......@@ -20156,22 +20167,22 @@ static Error buf_read_value_bytes(IrAnalyze *ira, AstNode *source_node, uint8_t
2015620167 return ErrorNone;
2015720168 case ZigTypeIdInt:
2015820169 bigint_read_twos_complement(&val->data.x_bigint, buf, val->type->data.integral.bit_count,
20159 ira->codegen->is_big_endian, val->type->data.integral.is_signed);
20170 codegen->is_big_endian, val->type->data.integral.is_signed);
2016020171 return ErrorNone;
2016120172 case ZigTypeIdFloat:
20162 float_read_ieee597(val, buf, ira->codegen->is_big_endian);
20173 float_read_ieee597(val, buf, codegen->is_big_endian);
2016320174 return ErrorNone;
2016420175 case ZigTypeIdPointer:
2016520176 {
2016620177 val->data.x_ptr.special = ConstPtrSpecialHardCodedAddr;
2016720178 BigInt bn;
20168 bigint_read_twos_complement(&bn, buf, ira->codegen->builtin_types.entry_usize->data.integral.bit_count,
20169 ira->codegen->is_big_endian, false);
20179 bigint_read_twos_complement(&bn, buf, codegen->builtin_types.entry_usize->data.integral.bit_count,
20180 codegen->is_big_endian, false);
2017020181 val->data.x_ptr.data.hard_coded_addr.addr = bigint_as_unsigned(&bn);
2017120182 return ErrorNone;
2017220183 }
2017320184 case ZigTypeIdArray: {
20174 uint64_t elem_size = type_size(ira->codegen, val->type->data.array.child_type);
20185 uint64_t elem_size = type_size(codegen, val->type->data.array.child_type);
2017520186 size_t len = val->type->data.array.len;
2017620187
2017720188 switch (val->data.x_array.special) {
......@@ -20181,7 +20192,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, AstNode *source_node, uint8_t
2018120192 ConstExprValue *elem = &val->data.x_array.data.s_none.elements[i];
2018220193 elem->special = ConstValSpecialStatic;
2018320194 elem->type = val->type->data.array.child_type;
20184 if ((err = buf_read_value_bytes(ira, source_node, buf + (elem_size * i), elem)))
20195 if ((err = buf_read_value_bytes(ira, codegen, source_node, buf + (elem_size * i), elem)))
2018520196 return err;
2018620197 }
2018720198 break;
......@@ -20196,10 +20207,10 @@ static Error buf_read_value_bytes(IrAnalyze *ira, AstNode *source_node, uint8_t
2019620207 case ZigTypeIdStruct:
2019720208 switch (val->type->data.structure.layout) {
2019820209 case ContainerLayoutAuto: {
20199 ErrorMsg *msg = ir_add_error_node(ira, source_node,
20210 ErrorMsg *msg = opt_ir_add_error_node(ira, codegen, source_node,
2020020211 buf_sprintf("non-extern, non-packed struct '%s' cannot have its bytes reinterpreted",
2020120212 buf_ptr(&val->type->name)));
20202 add_error_note(ira->codegen, msg, val->type->data.structure.decl_node,
20213 add_error_note(codegen, msg, val->type->data.structure.decl_node,
2020320214 buf_sprintf("declared here"));
2020420215 return ErrorSemanticAnalyzeFail;
2020520216 }
......@@ -20213,10 +20224,10 @@ static Error buf_read_value_bytes(IrAnalyze *ira, AstNode *source_node, uint8_t
2021320224 field_val->type = type_field->type_entry;
2021420225 if (type_field->gen_index == SIZE_MAX)
2021520226 continue;
20216 size_t offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, val->type->type_ref,
20227 size_t offset = LLVMOffsetOfElement(codegen->target_data_ref, val->type->type_ref,
2021720228 type_field->gen_index);
2021820229 uint8_t *new_buf = buf + offset;
20219 if ((err = buf_read_value_bytes(ira, source_node, new_buf, field_val)))
20230 if ((err = buf_read_value_bytes(ira, codegen, source_node, new_buf, field_val)))
2022020231 return err;
2022120232 }
2022220233 return ErrorNone;
......@@ -20327,7 +20338,7 @@ static IrInstruction *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstruct
2032720338 IrInstruction *result = ir_const(ira, &instruction->base, dest_type);
2032820339 uint8_t *buf = allocate_nonzero<uint8_t>(src_size_bytes);
2032920340 buf_write_value_bytes(ira->codegen, buf, val);
20330 if ((err = buf_read_value_bytes(ira, instruction->base.source_node, buf, &result->value)))
20341 if ((err = buf_read_value_bytes(ira, ira->codegen, instruction->base.source_node, buf, &result->value)))
2033120342 return ira->codegen->invalid_instruction;
2033220343 return result;
2033320344 }
src/ir.hpp+4-1
......@@ -22,6 +22,9 @@ ZigType *ir_analyze(CodeGen *g, IrExecutable *old_executable, IrExecutable *new_
2222 ZigType *expected_type, AstNode *expected_type_source_node);
2323
2424bool ir_has_side_effects(IrInstruction *instruction);
25ConstExprValue *const_ptr_pointee(CodeGen *codegen, ConstExprValue *const_val);
25
26struct IrAnalyze;
27ConstExprValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ConstExprValue *const_val,
28 AstNode *source_node);
2629
2730#endif