| ... | @@ -172,7 +172,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue | ... | @@ -172,7 +172,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue |
| 172 | static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node, | 172 | static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node, |
| 173 | ConstExprValue *out_val, ConstExprValue *ptr_val); | 173 | ConstExprValue *out_val, ConstExprValue *ptr_val); |
| 174 | static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *ptr, | 174 | static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *ptr, |
| 175 | ZigType *dest_type, IrInstruction *dest_type_src); | 175 | ZigType *dest_type, IrInstruction *dest_type_src, bool safety_check_on); |
| 176 | static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed); | 176 | static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed); |
| 177 | static void copy_const_val(ConstExprValue *dest, ConstExprValue *src, bool same_global_refs); | 177 | static void copy_const_val(ConstExprValue *dest, ConstExprValue *src, bool same_global_refs); |
| 178 | static Error resolve_ptr_align(IrAnalyze *ira, ZigType *ty, uint32_t *result_align); | 178 | static Error resolve_ptr_align(IrAnalyze *ira, ZigType *ty, uint32_t *result_align); |
| ... | @@ -2202,12 +2202,13 @@ static IrInstruction *ir_build_test_comptime(IrBuilder *irb, Scope *scope, AstNo | ... | @@ -2202,12 +2202,13 @@ static IrInstruction *ir_build_test_comptime(IrBuilder *irb, Scope *scope, AstNo |
| 2202 | } | 2202 | } |
| 2203 | | 2203 | |
| 2204 | static IrInstruction *ir_build_ptr_cast_src(IrBuilder *irb, Scope *scope, AstNode *source_node, | 2204 | static IrInstruction *ir_build_ptr_cast_src(IrBuilder *irb, Scope *scope, AstNode *source_node, |
| 2205 | IrInstruction *dest_type, IrInstruction *ptr) | 2205 | IrInstruction *dest_type, IrInstruction *ptr, bool safety_check_on) |
| 2206 | { | 2206 | { |
| 2207 | IrInstructionPtrCastSrc *instruction = ir_build_instruction<IrInstructionPtrCastSrc>( | 2207 | IrInstructionPtrCastSrc *instruction = ir_build_instruction<IrInstructionPtrCastSrc>( |
| 2208 | irb, scope, source_node); | 2208 | irb, scope, source_node); |
| 2209 | instruction->dest_type = dest_type; | 2209 | instruction->dest_type = dest_type; |
| 2210 | instruction->ptr = ptr; | 2210 | instruction->ptr = ptr; |
| | 2211 | instruction->safety_check_on = safety_check_on; |
| 2211 | | 2212 | |
| 2212 | ir_ref_instruction(dest_type, irb->current_basic_block); | 2213 | ir_ref_instruction(dest_type, irb->current_basic_block); |
| 2213 | ir_ref_instruction(ptr, irb->current_basic_block); | 2214 | ir_ref_instruction(ptr, irb->current_basic_block); |
| ... | @@ -2216,12 +2217,13 @@ static IrInstruction *ir_build_ptr_cast_src(IrBuilder *irb, Scope *scope, AstNod | ... | @@ -2216,12 +2217,13 @@ static IrInstruction *ir_build_ptr_cast_src(IrBuilder *irb, Scope *scope, AstNod |
| 2216 | } | 2217 | } |
| 2217 | | 2218 | |
| 2218 | static IrInstruction *ir_build_ptr_cast_gen(IrAnalyze *ira, IrInstruction *source_instruction, | 2219 | static IrInstruction *ir_build_ptr_cast_gen(IrAnalyze *ira, IrInstruction *source_instruction, |
| 2219 | ZigType *ptr_type, IrInstruction *ptr) | 2220 | ZigType *ptr_type, IrInstruction *ptr, bool safety_check_on) |
| 2220 | { | 2221 | { |
| 2221 | IrInstructionPtrCastGen *instruction = ir_build_instruction<IrInstructionPtrCastGen>( | 2222 | IrInstructionPtrCastGen *instruction = ir_build_instruction<IrInstructionPtrCastGen>( |
| 2222 | &ira->new_irb, source_instruction->scope, source_instruction->source_node); | 2223 | &ira->new_irb, source_instruction->scope, source_instruction->source_node); |
| 2223 | instruction->base.value.type = ptr_type; | 2224 | instruction->base.value.type = ptr_type; |
| 2224 | instruction->ptr = ptr; | 2225 | instruction->ptr = ptr; |
| | 2226 | instruction->safety_check_on = safety_check_on; |
| 2225 | | 2227 | |
| 2226 | ir_ref_instruction(ptr, ira->new_irb.current_basic_block); | 2228 | ir_ref_instruction(ptr, ira->new_irb.current_basic_block); |
| 2227 | | 2229 | |
| ... | @@ -4505,7 +4507,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo | ... | @@ -4505,7 +4507,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo |
| 4505 | if (arg1_value == irb->codegen->invalid_instruction) | 4507 | if (arg1_value == irb->codegen->invalid_instruction) |
| 4506 | return arg1_value; | 4508 | return arg1_value; |
| 4507 | | 4509 | |
| 4508 | IrInstruction *ptr_cast = ir_build_ptr_cast_src(irb, scope, node, arg0_value, arg1_value); | 4510 | IrInstruction *ptr_cast = ir_build_ptr_cast_src(irb, scope, node, arg0_value, arg1_value, true); |
| 4509 | return ir_lval_wrap(irb, scope, ptr_cast, lval); | 4511 | return ir_lval_wrap(irb, scope, ptr_cast, lval); |
| 4510 | } | 4512 | } |
| 4511 | case BuiltinFnIdBitCast: | 4513 | case BuiltinFnIdBitCast: |
| ... | @@ -6740,7 +6742,8 @@ static IrInstruction *ir_gen_cancel_target(IrBuilder *irb, Scope *scope, AstNode | ... | @@ -6740,7 +6742,8 @@ static IrInstruction *ir_gen_cancel_target(IrBuilder *irb, Scope *scope, AstNode |
| 6740 | IrInstruction *is_suspended_mask = ir_build_const_usize(irb, scope, node, 0x2); // 0b010 | 6742 | IrInstruction *is_suspended_mask = ir_build_const_usize(irb, scope, node, 0x2); // 0b010 |
| 6741 | | 6743 | |
| 6742 | // TODO relies on Zig not re-ordering fields | 6744 | // TODO relies on Zig not re-ordering fields |
| 6743 | IrInstruction *casted_target_inst = ir_build_ptr_cast_src(irb, scope, node, promise_T_type_val, target_inst); | 6745 | IrInstruction *casted_target_inst = ir_build_ptr_cast_src(irb, scope, node, promise_T_type_val, target_inst, |
| | 6746 | false); |
| 6744 | IrInstruction *coro_promise_ptr = ir_build_coro_promise(irb, scope, node, casted_target_inst); | 6747 | IrInstruction *coro_promise_ptr = ir_build_coro_promise(irb, scope, node, casted_target_inst); |
| 6745 | Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME); | 6748 | Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME); |
| 6746 | IrInstruction *atomic_state_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, | 6749 | IrInstruction *atomic_state_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, |
| ... | @@ -6818,7 +6821,8 @@ static IrInstruction *ir_gen_resume_target(IrBuilder *irb, Scope *scope, AstNode | ... | @@ -6818,7 +6821,8 @@ static IrInstruction *ir_gen_resume_target(IrBuilder *irb, Scope *scope, AstNode |
| 6818 | get_promise_type(irb->codegen, irb->codegen->builtin_types.entry_void)); | 6821 | get_promise_type(irb->codegen, irb->codegen->builtin_types.entry_void)); |
| 6819 | | 6822 | |
| 6820 | // TODO relies on Zig not re-ordering fields | 6823 | // TODO relies on Zig not re-ordering fields |
| 6821 | IrInstruction *casted_target_inst = ir_build_ptr_cast_src(irb, scope, node, promise_T_type_val, target_inst); | 6824 | IrInstruction *casted_target_inst = ir_build_ptr_cast_src(irb, scope, node, promise_T_type_val, target_inst, |
| | 6825 | false); |
| 6822 | IrInstruction *coro_promise_ptr = ir_build_coro_promise(irb, scope, node, casted_target_inst); | 6826 | IrInstruction *coro_promise_ptr = ir_build_coro_promise(irb, scope, node, casted_target_inst); |
| 6823 | Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME); | 6827 | Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME); |
| 6824 | IrInstruction *atomic_state_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, | 6828 | IrInstruction *atomic_state_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, |
| ... | @@ -7363,7 +7367,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec | ... | @@ -7363,7 +7367,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec |
| 7363 | | 7367 | |
| 7364 | u8_ptr_type = ir_build_const_type(irb, coro_scope, node, | 7368 | u8_ptr_type = ir_build_const_type(irb, coro_scope, node, |
| 7365 | get_pointer_to_type(irb->codegen, irb->codegen->builtin_types.entry_u8, false)); | 7369 | get_pointer_to_type(irb->codegen, irb->codegen->builtin_types.entry_u8, false)); |
| 7366 | IrInstruction *promise_as_u8_ptr = ir_build_ptr_cast_src(irb, coro_scope, node, u8_ptr_type, coro_promise_ptr); | 7370 | IrInstruction *promise_as_u8_ptr = ir_build_ptr_cast_src(irb, coro_scope, node, u8_ptr_type, |
| | 7371 | coro_promise_ptr, false); |
| 7367 | coro_id = ir_build_coro_id(irb, coro_scope, node, promise_as_u8_ptr); | 7372 | coro_id = ir_build_coro_id(irb, coro_scope, node, promise_as_u8_ptr); |
| 7368 | coro_size_var = ir_create_var(irb, node, coro_scope, nullptr, false, false, true, const_bool_false); | 7373 | coro_size_var = ir_create_var(irb, node, coro_scope, nullptr, false, false, true, const_bool_false); |
| 7369 | IrInstruction *coro_size = ir_build_coro_size(irb, coro_scope, node); | 7374 | IrInstruction *coro_size = ir_build_coro_size(irb, coro_scope, node); |
| ... | @@ -7387,7 +7392,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec | ... | @@ -7387,7 +7392,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec |
| 7387 | ir_build_return(irb, coro_scope, node, undef); | 7392 | ir_build_return(irb, coro_scope, node, undef); |
| 7388 | | 7393 | |
| 7389 | ir_set_cursor_at_end_and_append_block(irb, alloc_ok_block); | 7394 | ir_set_cursor_at_end_and_append_block(irb, alloc_ok_block); |
| 7390 | IrInstruction *coro_mem_ptr = ir_build_ptr_cast_src(irb, coro_scope, node, u8_ptr_type, maybe_coro_mem_ptr); | 7395 | IrInstruction *coro_mem_ptr = ir_build_ptr_cast_src(irb, coro_scope, node, u8_ptr_type, maybe_coro_mem_ptr, |
| | 7396 | false); |
| 7391 | irb->exec->coro_handle = ir_build_coro_begin(irb, coro_scope, node, coro_id, coro_mem_ptr); | 7397 | irb->exec->coro_handle = ir_build_coro_begin(irb, coro_scope, node, coro_id, coro_mem_ptr); |
| 7392 | | 7398 | |
| 7393 | Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME); | 7399 | Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME); |
| ... | @@ -7465,9 +7471,10 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec | ... | @@ -7465,9 +7471,10 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec |
| 7465 | get_pointer_to_type_extra(irb->codegen, irb->codegen->builtin_types.entry_u8, | 7471 | get_pointer_to_type_extra(irb->codegen, irb->codegen->builtin_types.entry_u8, |
| 7466 | false, false, PtrLenUnknown, 0, 0, 0)); | 7472 | false, false, PtrLenUnknown, 0, 0, 0)); |
| 7467 | IrInstruction *result_ptr = ir_build_load_ptr(irb, scope, node, irb->exec->coro_result_ptr_field_ptr); | 7473 | IrInstruction *result_ptr = ir_build_load_ptr(irb, scope, node, irb->exec->coro_result_ptr_field_ptr); |
| 7468 | IrInstruction *result_ptr_as_u8_ptr = ir_build_ptr_cast_src(irb, scope, node, u8_ptr_type_unknown_len, result_ptr); | 7474 | IrInstruction *result_ptr_as_u8_ptr = ir_build_ptr_cast_src(irb, scope, node, u8_ptr_type_unknown_len, |
| 7469 | IrInstruction *return_value_ptr_as_u8_ptr = ir_build_ptr_cast_src(irb, scope, node, u8_ptr_type_unknown_len, | 7475 | result_ptr, false); |
| 7470 | irb->exec->coro_result_field_ptr); | 7476 | IrInstruction *return_value_ptr_as_u8_ptr = ir_build_ptr_cast_src(irb, scope, node, |
| | 7477 | u8_ptr_type_unknown_len, irb->exec->coro_result_field_ptr, false); |
| 7471 | IrInstruction *return_type_inst = ir_build_const_type(irb, scope, node, | 7478 | IrInstruction *return_type_inst = ir_build_const_type(irb, scope, node, |
| 7472 | fn_entry->type_entry->data.fn.fn_type_id.return_type); | 7479 | fn_entry->type_entry->data.fn.fn_type_id.return_type); |
| 7473 | IrInstruction *size_of_ret_val = ir_build_size_of(irb, scope, node, return_type_inst); | 7480 | IrInstruction *size_of_ret_val = ir_build_size_of(irb, scope, node, return_type_inst); |
| ... | @@ -7517,7 +7524,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec | ... | @@ -7517,7 +7524,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec |
| 7517 | IrInstruction *u8_ptr_type_unknown_len = ir_build_const_type(irb, scope, node, | 7524 | IrInstruction *u8_ptr_type_unknown_len = ir_build_const_type(irb, scope, node, |
| 7518 | get_pointer_to_type_extra(irb->codegen, irb->codegen->builtin_types.entry_u8, | 7525 | get_pointer_to_type_extra(irb->codegen, irb->codegen->builtin_types.entry_u8, |
| 7519 | false, false, PtrLenUnknown, 0, 0, 0)); | 7526 | false, false, PtrLenUnknown, 0, 0, 0)); |
| 7520 | IrInstruction *coro_mem_ptr = ir_build_ptr_cast_src(irb, scope, node, u8_ptr_type_unknown_len, coro_mem_ptr_maybe); | 7527 | IrInstruction *coro_mem_ptr = ir_build_ptr_cast_src(irb, scope, node, u8_ptr_type_unknown_len, |
| | 7528 | coro_mem_ptr_maybe, false); |
| 7521 | IrInstruction *coro_mem_ptr_ref = ir_build_ref(irb, scope, node, coro_mem_ptr, true, false); | 7529 | IrInstruction *coro_mem_ptr_ref = ir_build_ref(irb, scope, node, coro_mem_ptr, true, false); |
| 7522 | IrInstruction *coro_size_ptr = ir_build_var_ptr(irb, scope, node, coro_size_var); | 7530 | IrInstruction *coro_size_ptr = ir_build_var_ptr(irb, scope, node, coro_size_var); |
| 7523 | IrInstruction *coro_size = ir_build_load_ptr(irb, scope, node, coro_size_ptr); | 7531 | IrInstruction *coro_size = ir_build_load_ptr(irb, scope, node, coro_size_ptr); |
| ... | @@ -8644,15 +8652,6 @@ static ZigType *get_error_set_intersection(IrAnalyze *ira, ZigType *set1, ZigTyp | ... | @@ -8644,15 +8652,6 @@ static ZigType *get_error_set_intersection(IrAnalyze *ira, ZigType *set1, ZigTyp |
| 8644 | return err_set_type; | 8652 | return err_set_type; |
| 8645 | } | 8653 | } |
| 8646 | | 8654 | |
| 8647 | static bool ptr_allows_addr_zero(ZigType *ptr_type) { | | |
| 8648 | if (ptr_type->id == ZigTypeIdPointer) { | | |
| 8649 | return ptr_type->data.pointer.allow_zero; | | |
| 8650 | } else if (ptr_type->id == ZigTypeIdOptional) { | | |
| 8651 | return true; | | |
| 8652 | } | | |
| 8653 | return false; | | |
| 8654 | } | | |
| 8655 | | | |
| 8656 | static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted_type, | 8655 | static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted_type, |
| 8657 | ZigType *actual_type, AstNode *source_node, bool wanted_is_mutable) | 8656 | ZigType *actual_type, AstNode *source_node, bool wanted_is_mutable) |
| 8658 | { | 8657 | { |
| ... | @@ -11310,7 +11309,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst | ... | @@ -11310,7 +11309,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst |
| 11310 | actual_type->data.pointer.host_int_bytes == dest_ptr_type->data.pointer.host_int_bytes && | 11309 | actual_type->data.pointer.host_int_bytes == dest_ptr_type->data.pointer.host_int_bytes && |
| 11311 | get_ptr_align(ira->codegen, actual_type) >= get_ptr_align(ira->codegen, dest_ptr_type)) | 11310 | get_ptr_align(ira->codegen, actual_type) >= get_ptr_align(ira->codegen, dest_ptr_type)) |
| 11312 | { | 11311 | { |
| 11313 | return ir_analyze_ptr_cast(ira, source_instr, value, wanted_type, source_instr); | 11312 | return ir_analyze_ptr_cast(ira, source_instr, value, wanted_type, source_instr, true); |
| 11314 | } | 11313 | } |
| 11315 | } | 11314 | } |
| 11316 | | 11315 | |
| ... | @@ -11352,7 +11351,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst | ... | @@ -11352,7 +11351,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst |
| 11352 | actual_type->data.pointer.child_type, source_node, | 11351 | actual_type->data.pointer.child_type, source_node, |
| 11353 | !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk) | 11352 | !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk) |
| 11354 | { | 11353 | { |
| 11355 | return ir_analyze_ptr_cast(ira, source_instr, value, wanted_type, source_instr); | 11354 | return ir_analyze_ptr_cast(ira, source_instr, value, wanted_type, source_instr, true); |
| 11356 | } | 11355 | } |
| 11357 | | 11356 | |
| 11358 | // cast from integer to C pointer | 11357 | // cast from integer to C pointer |
| ... | @@ -20616,7 +20615,7 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3 | ... | @@ -20616,7 +20615,7 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3 |
| 20616 | } | 20615 | } |
| 20617 | | 20616 | |
| 20618 | static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *ptr, | 20617 | static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *ptr, |
| 20619 | ZigType *dest_type, IrInstruction *dest_type_src) | 20618 | ZigType *dest_type, IrInstruction *dest_type_src, bool safety_check_on) |
| 20620 | { | 20619 | { |
| 20621 | Error err; | 20620 | Error err; |
| 20622 | | 20621 | |
| ... | @@ -20685,7 +20684,7 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_ | ... | @@ -20685,7 +20684,7 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_ |
| 20685 | return ira->codegen->invalid_instruction; | 20684 | return ira->codegen->invalid_instruction; |
| 20686 | } | 20685 | } |
| 20687 | | 20686 | |
| 20688 | IrInstruction *casted_ptr = ir_build_ptr_cast_gen(ira, source_instr, dest_type, ptr); | 20687 | IrInstruction *casted_ptr = ir_build_ptr_cast_gen(ira, source_instr, dest_type, ptr, safety_check_on); |
| 20689 | | 20688 | |
| 20690 | if (type_has_bits(dest_type) && !type_has_bits(src_type)) { | 20689 | if (type_has_bits(dest_type) && !type_has_bits(src_type)) { |
| 20691 | ErrorMsg *msg = ir_add_error(ira, source_instr, | 20690 | ErrorMsg *msg = ir_add_error(ira, source_instr, |
| ... | @@ -20722,7 +20721,8 @@ static IrInstruction *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstruct | ... | @@ -20722,7 +20721,8 @@ static IrInstruction *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstruct |
| 20722 | if (type_is_invalid(src_type)) | 20721 | if (type_is_invalid(src_type)) |
| 20723 | return ira->codegen->invalid_instruction; | 20722 | return ira->codegen->invalid_instruction; |
| 20724 | | 20723 | |
| 20725 | return ir_analyze_ptr_cast(ira, &instruction->base, ptr, dest_type, dest_type_value); | 20724 | return ir_analyze_ptr_cast(ira, &instruction->base, ptr, dest_type, dest_type_value, |
| | 20725 | instruction->safety_check_on); |
| 20726 | } | 20726 | } |
| 20727 | | 20727 | |
| 20728 | static void buf_write_value_bytes_array(CodeGen *codegen, uint8_t *buf, ConstExprValue *val, size_t len) { | 20728 | static void buf_write_value_bytes_array(CodeGen *codegen, uint8_t *buf, ConstExprValue *val, size_t len) { |