| ... | @@ -780,10 +780,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionPtrCastGen *) { | ... | @@ -780,10 +780,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionPtrCastGen *) { |
| 780 | return IrInstructionIdPtrCastGen; | 780 | return IrInstructionIdPtrCastGen; |
| 781 | } | 781 | } |
| 782 | | 782 | |
| 783 | static constexpr IrInstructionId ir_instruction_id(IrInstructionBitCast *) { | | |
| 784 | return IrInstructionIdBitCast; | | |
| 785 | } | | |
| 786 | | | |
| 787 | static constexpr IrInstructionId ir_instruction_id(IrInstructionBitCastGen *) { | 783 | static constexpr IrInstructionId ir_instruction_id(IrInstructionBitCastGen *) { |
| 788 | return IrInstructionIdBitCastGen; | 784 | return IrInstructionIdBitCastGen; |
| 789 | } | 785 | } |
| ... | @@ -2429,20 +2425,6 @@ static IrInstruction *ir_build_load_ptr_gen(IrAnalyze *ira, IrInstruction *sourc | ... | @@ -2429,20 +2425,6 @@ static IrInstruction *ir_build_load_ptr_gen(IrAnalyze *ira, IrInstruction *sourc |
| 2429 | return &instruction->base; | 2425 | return &instruction->base; |
| 2430 | } | 2426 | } |
| 2431 | | 2427 | |
| 2432 | static IrInstruction *ir_build_bit_cast(IrBuilder *irb, Scope *scope, AstNode *source_node, | | |
| 2433 | IrInstruction *dest_type, IrInstruction *value) | | |
| 2434 | { | | |
| 2435 | IrInstructionBitCast *instruction = ir_build_instruction<IrInstructionBitCast>( | | |
| 2436 | irb, scope, source_node); | | |
| 2437 | instruction->dest_type = dest_type; | | |
| 2438 | instruction->value = value; | | |
| 2439 | | | |
| 2440 | ir_ref_instruction(dest_type, irb->current_basic_block); | | |
| 2441 | ir_ref_instruction(value, irb->current_basic_block); | | |
| 2442 | | | |
| 2443 | return &instruction->base; | | |
| 2444 | } | | |
| 2445 | | | |
| 2446 | static IrInstruction *ir_build_bit_cast_gen(IrAnalyze *ira, IrInstruction *source_instruction, | 2428 | static IrInstruction *ir_build_bit_cast_gen(IrAnalyze *ira, IrInstruction *source_instruction, |
| 2447 | IrInstruction *operand, ZigType *ty) | 2429 | IrInstruction *operand, ZigType *ty) |
| 2448 | { | 2430 | { |
| ... | @@ -4836,18 +4818,23 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo | ... | @@ -4836,18 +4818,23 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo |
| 4836 | } | 4818 | } |
| 4837 | case BuiltinFnIdBitCast: | 4819 | case BuiltinFnIdBitCast: |
| 4838 | { | 4820 | { |
| 4839 | AstNode *arg0_node = node->data.fn_call_expr.params.at(0); | 4821 | AstNode *dest_type_node = node->data.fn_call_expr.params.at(0); |
| 4840 | IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope); | 4822 | IrInstruction *dest_type = ir_gen_node(irb, dest_type_node, scope); |
| 4841 | if (arg0_value == irb->codegen->invalid_instruction) | 4823 | if (dest_type == irb->codegen->invalid_instruction) |
| 4842 | return arg0_value; | 4824 | return dest_type; |
| | 4825 | |
| | 4826 | ResultLocBitCast *result_loc_bit_cast = allocate<ResultLocBitCast>(1); |
| | 4827 | result_loc_bit_cast->base.id = ResultLocIdBitCast; |
| | 4828 | result_loc_bit_cast->base.source_instruction = dest_type; |
| | 4829 | result_loc_bit_cast->parent = result_loc; |
| 4843 | | 4830 | |
| 4844 | AstNode *arg1_node = node->data.fn_call_expr.params.at(1); | 4831 | AstNode *arg1_node = node->data.fn_call_expr.params.at(1); |
| 4845 | IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope); | 4832 | IrInstruction *arg1_value = ir_gen_node_extra(irb, arg1_node, scope, LValNone, |
| | 4833 | &result_loc_bit_cast->base); |
| 4846 | if (arg1_value == irb->codegen->invalid_instruction) | 4834 | if (arg1_value == irb->codegen->invalid_instruction) |
| 4847 | return arg1_value; | 4835 | return arg1_value; |
| 4848 | | 4836 | |
| 4849 | IrInstruction *bit_cast = ir_build_bit_cast(irb, scope, node, arg0_value, arg1_value); | 4837 | return ir_lval_wrap(irb, scope, arg1_value, lval, result_loc); |
| 4850 | return ir_lval_wrap(irb, scope, bit_cast, lval, result_loc); | | |
| 4851 | } | 4838 | } |
| 4852 | case BuiltinFnIdIntToPtr: | 4839 | case BuiltinFnIdIntToPtr: |
| 4853 | { | 4840 | { |
| ... | @@ -14059,9 +14046,10 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira, | ... | @@ -14059,9 +14046,10 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira, |
| 14059 | bool var_class_requires_const = false; | 14046 | bool var_class_requires_const = false; |
| 14060 | | 14047 | |
| 14061 | IrInstruction *var_ptr = decl_var_instruction->ptr->child; | 14048 | IrInstruction *var_ptr = decl_var_instruction->ptr->child; |
| 14062 | // if this assertion trips there may be a missing ir_expr_wrap in pass1 IR generation. | 14049 | // if this is null, a compiler error happened and did not initialize the variable. |
| 14063 | ir_assert(var_ptr != nullptr, &decl_var_instruction->base); | 14050 | // if there are no compile errors there may be a missing ir_expr_wrap in pass1 IR generation. |
| 14064 | if (type_is_invalid(var_ptr->value.type)) { | 14051 | if (var_ptr == nullptr || type_is_invalid(var_ptr->value.type)) { |
| | 14052 | ir_assert(var_ptr != nullptr || ira->codegen->errors.length != 0, &decl_var_instruction->base); |
| 14065 | var->var_type = ira->codegen->builtin_types.entry_invalid; | 14053 | var->var_type = ira->codegen->builtin_types.entry_invalid; |
| 14066 | return ira->codegen->invalid_instruction; | 14054 | return ira->codegen->invalid_instruction; |
| 14067 | } | 14055 | } |
| ... | @@ -14528,6 +14516,7 @@ static ZigType *ir_result_loc_expected_type(IrAnalyze *ira, IrInstruction *suspe | ... | @@ -14528,6 +14516,7 @@ static ZigType *ir_result_loc_expected_type(IrAnalyze *ira, IrInstruction *suspe |
| 14528 | zig_unreachable(); | 14516 | zig_unreachable(); |
| 14529 | case ResultLocIdNone: | 14517 | case ResultLocIdNone: |
| 14530 | case ResultLocIdVar: | 14518 | case ResultLocIdVar: |
| | 14519 | case ResultLocIdBitCast: |
| 14531 | return nullptr; | 14520 | return nullptr; |
| 14532 | case ResultLocIdInstruction: | 14521 | case ResultLocIdInstruction: |
| 14533 | return result_loc->source_instruction->child->value.type; | 14522 | return result_loc->source_instruction->child->value.type; |
| ... | @@ -14539,6 +14528,28 @@ static ZigType *ir_result_loc_expected_type(IrAnalyze *ira, IrInstruction *suspe | ... | @@ -14539,6 +14528,28 @@ static ZigType *ir_result_loc_expected_type(IrAnalyze *ira, IrInstruction *suspe |
| 14539 | zig_unreachable(); | 14528 | zig_unreachable(); |
| 14540 | } | 14529 | } |
| 14541 | | 14530 | |
| | 14531 | static bool type_can_bit_cast(ZigType *t) { |
| | 14532 | switch (t->id) { |
| | 14533 | case ZigTypeIdInvalid: |
| | 14534 | zig_unreachable(); |
| | 14535 | case ZigTypeIdMetaType: |
| | 14536 | case ZigTypeIdOpaque: |
| | 14537 | case ZigTypeIdBoundFn: |
| | 14538 | case ZigTypeIdArgTuple: |
| | 14539 | case ZigTypeIdUnreachable: |
| | 14540 | case ZigTypeIdComptimeFloat: |
| | 14541 | case ZigTypeIdComptimeInt: |
| | 14542 | case ZigTypeIdEnumLiteral: |
| | 14543 | case ZigTypeIdUndefined: |
| | 14544 | case ZigTypeIdNull: |
| | 14545 | case ZigTypeIdPointer: |
| | 14546 | return false; |
| | 14547 | default: |
| | 14548 | // TODO list these types out explicitly, there are probably some other invalid ones here |
| | 14549 | return true; |
| | 14550 | } |
| | 14551 | } |
| | 14552 | |
| 14542 | // give nullptr for value to resolve it at runtime | 14553 | // give nullptr for value to resolve it at runtime |
| 14543 | // returns a result location, or nullptr if the result location was already taken care of | 14554 | // returns a result location, or nullptr if the result location was already taken care of |
| 14544 | // when calling this function, at the callsite must check for result type noreturn and propagate it up | 14555 | // when calling this function, at the callsite must check for result type noreturn and propagate it up |
| ... | @@ -14676,6 +14687,50 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s | ... | @@ -14676,6 +14687,50 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s |
| 14676 | result_loc->resolved_loc = parent_result_loc; | 14687 | result_loc->resolved_loc = parent_result_loc; |
| 14677 | return result_loc->resolved_loc; | 14688 | return result_loc->resolved_loc; |
| 14678 | } | 14689 | } |
| | 14690 | case ResultLocIdBitCast: { |
| | 14691 | ResultLocBitCast *result_bit_cast = reinterpret_cast<ResultLocBitCast *>(result_loc); |
| | 14692 | ZigType *dest_type = ir_resolve_type(ira, result_bit_cast->base.source_instruction->child); |
| | 14693 | if (type_is_invalid(dest_type)) |
| | 14694 | return ira->codegen->invalid_instruction; |
| | 14695 | |
| | 14696 | if (get_codegen_ptr_type(dest_type) != nullptr) { |
| | 14697 | ir_add_error(ira, result_loc->source_instruction, |
| | 14698 | buf_sprintf("unable to @bitCast to pointer type '%s'", buf_ptr(&dest_type->name))); |
| | 14699 | return ira->codegen->invalid_instruction; |
| | 14700 | } |
| | 14701 | |
| | 14702 | if (!type_can_bit_cast(dest_type)) { |
| | 14703 | ir_add_error(ira, result_loc->source_instruction, |
| | 14704 | buf_sprintf("unable to @bitCast to type '%s'", buf_ptr(&dest_type->name))); |
| | 14705 | return ira->codegen->invalid_instruction; |
| | 14706 | } |
| | 14707 | |
| | 14708 | if (get_codegen_ptr_type(value_type) != nullptr) { |
| | 14709 | ir_add_error(ira, suspend_source_instr, |
| | 14710 | buf_sprintf("unable to @bitCast from pointer type '%s'", buf_ptr(&value_type->name))); |
| | 14711 | return ira->codegen->invalid_instruction; |
| | 14712 | } |
| | 14713 | |
| | 14714 | if (!type_can_bit_cast(value_type)) { |
| | 14715 | ir_add_error(ira, suspend_source_instr, |
| | 14716 | buf_sprintf("unable to @bitCast from type '%s'", buf_ptr(&value_type->name))); |
| | 14717 | return ira->codegen->invalid_instruction; |
| | 14718 | } |
| | 14719 | |
| | 14720 | |
| | 14721 | IrInstruction *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, result_bit_cast->parent, |
| | 14722 | dest_type, nullptr); |
| | 14723 | if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value.type) || |
| | 14724 | parent_result_loc->value.type->id == ZigTypeIdUnreachable) |
| | 14725 | { |
| | 14726 | return parent_result_loc; |
| | 14727 | } |
| | 14728 | ZigType *ptr_type = get_pointer_to_type(ira->codegen, value_type, false); |
| | 14729 | result_loc->written = true; |
| | 14730 | result_loc->resolved_loc = ir_analyze_ptr_cast(ira, suspend_source_instr, parent_result_loc, |
| | 14731 | ptr_type, result_bit_cast->base.source_instruction, false); |
| | 14732 | return result_loc->resolved_loc; |
| | 14733 | } |
| 14679 | } | 14734 | } |
| 14680 | zig_unreachable(); | 14735 | zig_unreachable(); |
| 14681 | } | 14736 | } |
| ... | @@ -22755,28 +22810,6 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou | ... | @@ -22755,28 +22810,6 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou |
| 22755 | zig_unreachable(); | 22810 | zig_unreachable(); |
| 22756 | } | 22811 | } |
| 22757 | | 22812 | |
| 22758 | static bool type_can_bit_cast(ZigType *t) { | | |
| 22759 | switch (t->id) { | | |
| 22760 | case ZigTypeIdInvalid: | | |
| 22761 | zig_unreachable(); | | |
| 22762 | case ZigTypeIdMetaType: | | |
| 22763 | case ZigTypeIdOpaque: | | |
| 22764 | case ZigTypeIdBoundFn: | | |
| 22765 | case ZigTypeIdArgTuple: | | |
| 22766 | case ZigTypeIdUnreachable: | | |
| 22767 | case ZigTypeIdComptimeFloat: | | |
| 22768 | case ZigTypeIdComptimeInt: | | |
| 22769 | case ZigTypeIdEnumLiteral: | | |
| 22770 | case ZigTypeIdUndefined: | | |
| 22771 | case ZigTypeIdNull: | | |
| 22772 | case ZigTypeIdPointer: | | |
| 22773 | return false; | | |
| 22774 | default: | | |
| 22775 | // TODO list these types out explicitly, there are probably some other invalid ones here | | |
| 22776 | return true; | | |
| 22777 | } | | |
| 22778 | } | | |
| 22779 | | | |
| 22780 | static IrInstruction *ir_analyze_bit_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value, | 22813 | static IrInstruction *ir_analyze_bit_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value, |
| 22781 | ZigType *dest_type) | 22814 | ZigType *dest_type) |
| 22782 | { | 22815 | { |
| ... | @@ -22829,50 +22862,10 @@ static IrInstruction *ir_analyze_bit_cast(IrAnalyze *ira, IrInstruction *source_ | ... | @@ -22829,50 +22862,10 @@ static IrInstruction *ir_analyze_bit_cast(IrAnalyze *ira, IrInstruction *source_ |
| 22829 | } | 22862 | } |
| 22830 | | 22863 | |
| 22831 | IrInstruction *result = ir_build_bit_cast_gen(ira, source_instr, value, dest_type); | 22864 | IrInstruction *result = ir_build_bit_cast_gen(ira, source_instr, value, dest_type); |
| 22832 | if (handle_is_ptr(dest_type) && !handle_is_ptr(src_type)) { | 22865 | assert(!(handle_is_ptr(dest_type) && !handle_is_ptr(src_type))); |
| 22833 | ir_add_alloca(ira, result, dest_type); | | |
| 22834 | } | | |
| 22835 | return result; | 22866 | return result; |
| 22836 | } | 22867 | } |
| 22837 | | 22868 | |
| 22838 | static IrInstruction *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstructionBitCast *instruction) { | | |
| 22839 | IrInstruction *dest_type_value = instruction->dest_type->child; | | |
| 22840 | ZigType *dest_type = ir_resolve_type(ira, dest_type_value); | | |
| 22841 | if (type_is_invalid(dest_type)) | | |
| 22842 | return ira->codegen->invalid_instruction; | | |
| 22843 | | | |
| 22844 | IrInstruction *value = instruction->value->child; | | |
| 22845 | ZigType *src_type = value->value.type; | | |
| 22846 | if (type_is_invalid(src_type)) | | |
| 22847 | return ira->codegen->invalid_instruction; | | |
| 22848 | | | |
| 22849 | if (get_codegen_ptr_type(src_type) != nullptr) { | | |
| 22850 | ir_add_error(ira, value, | | |
| 22851 | buf_sprintf("unable to @bitCast from pointer type '%s'", buf_ptr(&src_type->name))); | | |
| 22852 | return ira->codegen->invalid_instruction; | | |
| 22853 | } | | |
| 22854 | | | |
| 22855 | if (!type_can_bit_cast(src_type)) { | | |
| 22856 | ir_add_error(ira, dest_type_value, | | |
| 22857 | buf_sprintf("unable to @bitCast from type '%s'", buf_ptr(&src_type->name))); | | |
| 22858 | return ira->codegen->invalid_instruction; | | |
| 22859 | } | | |
| 22860 | | | |
| 22861 | if (get_codegen_ptr_type(dest_type) != nullptr) { | | |
| 22862 | ir_add_error(ira, dest_type_value, | | |
| 22863 | buf_sprintf("unable to @bitCast to pointer type '%s'", buf_ptr(&dest_type->name))); | | |
| 22864 | return ira->codegen->invalid_instruction; | | |
| 22865 | } | | |
| 22866 | | | |
| 22867 | if (!type_can_bit_cast(dest_type)) { | | |
| 22868 | ir_add_error(ira, dest_type_value, | | |
| 22869 | buf_sprintf("unable to @bitCast to type '%s'", buf_ptr(&dest_type->name))); | | |
| 22870 | return ira->codegen->invalid_instruction; | | |
| 22871 | } | | |
| 22872 | | | |
| 22873 | return ir_analyze_bit_cast(ira, &instruction->base, value, dest_type); | | |
| 22874 | } | | |
| 22875 | | | |
| 22876 | static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *target, | 22869 | static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *target, |
| 22877 | ZigType *ptr_type) | 22870 | ZigType *ptr_type) |
| 22878 | { | 22871 | { |
| ... | @@ -24089,8 +24082,6 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction | ... | @@ -24089,8 +24082,6 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction |
| 24089 | return ir_analyze_instruction_panic(ira, (IrInstructionPanic *)instruction); | 24082 | return ir_analyze_instruction_panic(ira, (IrInstructionPanic *)instruction); |
| 24090 | case IrInstructionIdPtrCastSrc: | 24083 | case IrInstructionIdPtrCastSrc: |
| 24091 | return ir_analyze_instruction_ptr_cast(ira, (IrInstructionPtrCastSrc *)instruction); | 24084 | return ir_analyze_instruction_ptr_cast(ira, (IrInstructionPtrCastSrc *)instruction); |
| 24092 | case IrInstructionIdBitCast: | | |
| 24093 | return ir_analyze_instruction_bit_cast(ira, (IrInstructionBitCast *)instruction); | | |
| 24094 | case IrInstructionIdIntToPtr: | 24085 | case IrInstructionIdIntToPtr: |
| 24095 | return ir_analyze_instruction_int_to_ptr(ira, (IrInstructionIntToPtr *)instruction); | 24086 | return ir_analyze_instruction_int_to_ptr(ira, (IrInstructionIntToPtr *)instruction); |
| 24096 | case IrInstructionIdPtrToInt: | 24087 | case IrInstructionIdPtrToInt: |
| ... | @@ -24386,7 +24377,6 @@ bool ir_has_side_effects(IrInstruction *instruction) { | ... | @@ -24386,7 +24377,6 @@ bool ir_has_side_effects(IrInstruction *instruction) { |
| 24386 | case IrInstructionIdTestComptime: | 24377 | case IrInstructionIdTestComptime: |
| 24387 | case IrInstructionIdPtrCastSrc: | 24378 | case IrInstructionIdPtrCastSrc: |
| 24388 | case IrInstructionIdPtrCastGen: | 24379 | case IrInstructionIdPtrCastGen: |
| 24389 | case IrInstructionIdBitCast: | | |
| 24390 | case IrInstructionIdBitCastGen: | 24380 | case IrInstructionIdBitCastGen: |
| 24391 | case IrInstructionIdWidenOrShorten: | 24381 | case IrInstructionIdWidenOrShorten: |
| 24392 | case IrInstructionIdPtrToInt: | 24382 | case IrInstructionIdPtrToInt: |