| ... | ... | @@ -383,10 +383,6 @@ static void destroy_instruction_src(IrInstSrc *inst) { |
| 383 | 383 | return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcFloatCast *>(inst)); |
| 384 | 384 | case IrInstSrcIdErrSetCast: |
| 385 | 385 | return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcErrSetCast *>(inst)); |
| 386 | | case IrInstSrcIdFromBytes: |
| 387 | | return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcFromBytes *>(inst)); |
| 388 | | case IrInstSrcIdToBytes: |
| 389 | | return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcToBytes *>(inst)); |
| 390 | 386 | case IrInstSrcIdIntToFloat: |
| 391 | 387 | return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcIntToFloat *>(inst)); |
| 392 | 388 | case IrInstSrcIdFloatToInt: |
| ... | ... | @@ -707,8 +703,6 @@ void destroy_instruction_gen(IrInstGen *inst) { |
| 707 | 703 | return heap::c_allocator.destroy(reinterpret_cast<IrInstGenAssertZero *>(inst)); |
| 708 | 704 | case IrInstGenIdAssertNonNull: |
| 709 | 705 | return heap::c_allocator.destroy(reinterpret_cast<IrInstGenAssertNonNull *>(inst)); |
| 710 | | case IrInstGenIdResizeSlice: |
| 711 | | return heap::c_allocator.destroy(reinterpret_cast<IrInstGenResizeSlice *>(inst)); |
| 712 | 706 | case IrInstGenIdAlloca: |
| 713 | 707 | return heap::c_allocator.destroy(reinterpret_cast<IrInstGenAlloca *>(inst)); |
| 714 | 708 | case IrInstGenIdSuspendBegin: |
| ... | ... | @@ -1563,14 +1557,6 @@ static constexpr IrInstSrcId ir_inst_id(IrInstSrcErrSetCast *) { |
| 1563 | 1557 | return IrInstSrcIdErrSetCast; |
| 1564 | 1558 | } |
| 1565 | 1559 | |
| 1566 | | static constexpr IrInstSrcId ir_inst_id(IrInstSrcToBytes *) { |
| 1567 | | return IrInstSrcIdToBytes; |
| 1568 | | } |
| 1569 | | |
| 1570 | | static constexpr IrInstSrcId ir_inst_id(IrInstSrcFromBytes *) { |
| 1571 | | return IrInstSrcIdFromBytes; |
| 1572 | | } |
| 1573 | | |
| 1574 | 1560 | static constexpr IrInstSrcId ir_inst_id(IrInstSrcCheckRuntimeScope *) { |
| 1575 | 1561 | return IrInstSrcIdCheckRuntimeScope; |
| 1576 | 1562 | } |
| ... | ... | @@ -1700,10 +1686,6 @@ static constexpr IrInstGenId ir_inst_id(IrInstGenCast *) { |
| 1700 | 1686 | return IrInstGenIdCast; |
| 1701 | 1687 | } |
| 1702 | 1688 | |
| 1703 | | static constexpr IrInstGenId ir_inst_id(IrInstGenResizeSlice *) { |
| 1704 | | return IrInstGenIdResizeSlice; |
| 1705 | | } |
| 1706 | | |
| 1707 | 1689 | static constexpr IrInstGenId ir_inst_id(IrInstGenUnreachable *) { |
| 1708 | 1690 | return IrInstGenIdUnreachable; |
| 1709 | 1691 | } |
| ... | ... | @@ -2759,21 +2741,6 @@ static IrInstGen *ir_build_var_decl_gen(IrAnalyze *ira, IrInst *source_instructi |
| 2759 | 2741 | return &inst->base; |
| 2760 | 2742 | } |
| 2761 | 2743 | |
| 2762 | | static IrInstGen *ir_build_resize_slice(IrAnalyze *ira, IrInst *source_instruction, |
| 2763 | | IrInstGen *operand, ZigType *ty, IrInstGen *result_loc) |
| 2764 | | { |
| 2765 | | IrInstGenResizeSlice *instruction = ir_build_inst_gen<IrInstGenResizeSlice>(&ira->new_irb, |
| 2766 | | source_instruction->scope, source_instruction->source_node); |
| 2767 | | instruction->base.value->type = ty; |
| 2768 | | instruction->operand = operand; |
| 2769 | | instruction->result_loc = result_loc; |
| 2770 | | |
| 2771 | | ir_ref_inst_gen(operand, ira->new_irb.current_basic_block); |
| 2772 | | if (result_loc != nullptr) ir_ref_inst_gen(result_loc, ira->new_irb.current_basic_block); |
| 2773 | | |
| 2774 | | return &instruction->base; |
| 2775 | | } |
| 2776 | | |
| 2777 | 2744 | static IrInstSrc *ir_build_export(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, |
| 2778 | 2745 | IrInstSrc *target, IrInstSrc *options) |
| 2779 | 2746 | { |
| ... | ... | @@ -3540,32 +3507,6 @@ static IrInstSrc *ir_build_err_set_cast(IrBuilderSrc *irb, Scope *scope, AstNode |
| 3540 | 3507 | return &instruction->base; |
| 3541 | 3508 | } |
| 3542 | 3509 | |
| 3543 | | static IrInstSrc *ir_build_to_bytes(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, IrInstSrc *target, |
| 3544 | | ResultLoc *result_loc) |
| 3545 | | { |
| 3546 | | IrInstSrcToBytes *instruction = ir_build_instruction<IrInstSrcToBytes>(irb, scope, source_node); |
| 3547 | | instruction->target = target; |
| 3548 | | instruction->result_loc = result_loc; |
| 3549 | | |
| 3550 | | ir_ref_instruction(target, irb->current_basic_block); |
| 3551 | | |
| 3552 | | return &instruction->base; |
| 3553 | | } |
| 3554 | | |
| 3555 | | static IrInstSrc *ir_build_from_bytes(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, |
| 3556 | | IrInstSrc *dest_child_type, IrInstSrc *target, ResultLoc *result_loc) |
| 3557 | | { |
| 3558 | | IrInstSrcFromBytes *instruction = ir_build_instruction<IrInstSrcFromBytes>(irb, scope, source_node); |
| 3559 | | instruction->dest_child_type = dest_child_type; |
| 3560 | | instruction->target = target; |
| 3561 | | instruction->result_loc = result_loc; |
| 3562 | | |
| 3563 | | ir_ref_instruction(dest_child_type, irb->current_basic_block); |
| 3564 | | ir_ref_instruction(target, irb->current_basic_block); |
| 3565 | | |
| 3566 | | return &instruction->base; |
| 3567 | | } |
| 3568 | | |
| 3569 | 3510 | static IrInstSrc *ir_build_int_to_float(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, |
| 3570 | 3511 | IrInstSrc *dest_type, IrInstSrc *target) |
| 3571 | 3512 | { |
| ... | ... | @@ -6597,31 +6538,6 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod |
| 6597 | 6538 | IrInstSrc *result = ir_build_err_set_cast(irb, scope, node, arg0_value, arg1_value); |
| 6598 | 6539 | return ir_lval_wrap(irb, scope, result, lval, result_loc); |
| 6599 | 6540 | } |
| 6600 | | case BuiltinFnIdFromBytes: |
| 6601 | | { |
| 6602 | | AstNode *arg0_node = node->data.fn_call_expr.params.at(0); |
| 6603 | | IrInstSrc *arg0_value = ir_gen_node(irb, arg0_node, scope); |
| 6604 | | if (arg0_value == irb->codegen->invalid_inst_src) |
| 6605 | | return arg0_value; |
| 6606 | | |
| 6607 | | AstNode *arg1_node = node->data.fn_call_expr.params.at(1); |
| 6608 | | IrInstSrc *arg1_value = ir_gen_node(irb, arg1_node, scope); |
| 6609 | | if (arg1_value == irb->codegen->invalid_inst_src) |
| 6610 | | return arg1_value; |
| 6611 | | |
| 6612 | | IrInstSrc *result = ir_build_from_bytes(irb, scope, node, arg0_value, arg1_value, result_loc); |
| 6613 | | return ir_lval_wrap(irb, scope, result, lval, result_loc); |
| 6614 | | } |
| 6615 | | case BuiltinFnIdToBytes: |
| 6616 | | { |
| 6617 | | AstNode *arg0_node = node->data.fn_call_expr.params.at(0); |
| 6618 | | IrInstSrc *arg0_value = ir_gen_node(irb, arg0_node, scope); |
| 6619 | | if (arg0_value == irb->codegen->invalid_inst_src) |
| 6620 | | return arg0_value; |
| 6621 | | |
| 6622 | | IrInstSrc *result = ir_build_to_bytes(irb, scope, node, arg0_value, result_loc); |
| 6623 | | return ir_lval_wrap(irb, scope, result, lval, result_loc); |
| 6624 | | } |
| 6625 | 6541 | case BuiltinFnIdIntToFloat: |
| 6626 | 6542 | { |
| 6627 | 6543 | AstNode *arg0_node = node->data.fn_call_expr.params.at(0); |
| ... | ... | @@ -25489,171 +25405,6 @@ static IrInstGen *ir_analyze_instruction_err_set_cast(IrAnalyze *ira, IrInstSrcE |
| 25489 | 25405 | return ir_analyze_err_set_cast(ira, &instruction->base.base, target, dest_type); |
| 25490 | 25406 | } |
| 25491 | 25407 | |
| 25492 | | static IrInstGen *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstSrcFromBytes *instruction) { |
| 25493 | | Error err; |
| 25494 | | |
| 25495 | | ZigType *dest_child_type = ir_resolve_type(ira, instruction->dest_child_type->child); |
| 25496 | | if (type_is_invalid(dest_child_type)) |
| 25497 | | return ira->codegen->invalid_inst_gen; |
| 25498 | | |
| 25499 | | IrInstGen *target = instruction->target->child; |
| 25500 | | if (type_is_invalid(target->value->type)) |
| 25501 | | return ira->codegen->invalid_inst_gen; |
| 25502 | | |
| 25503 | | bool src_ptr_const; |
| 25504 | | bool src_ptr_volatile; |
| 25505 | | uint32_t src_ptr_align; |
| 25506 | | if (target->value->type->id == ZigTypeIdPointer) { |
| 25507 | | src_ptr_const = target->value->type->data.pointer.is_const; |
| 25508 | | src_ptr_volatile = target->value->type->data.pointer.is_volatile; |
| 25509 | | |
| 25510 | | if ((err = resolve_ptr_align(ira, target->value->type, &src_ptr_align))) |
| 25511 | | return ira->codegen->invalid_inst_gen; |
| 25512 | | } else if (is_slice(target->value->type)) { |
| 25513 | | ZigType *src_ptr_type = target->value->type->data.structure.fields[slice_ptr_index]->type_entry; |
| 25514 | | src_ptr_const = src_ptr_type->data.pointer.is_const; |
| 25515 | | src_ptr_volatile = src_ptr_type->data.pointer.is_volatile; |
| 25516 | | |
| 25517 | | if ((err = resolve_ptr_align(ira, src_ptr_type, &src_ptr_align))) |
| 25518 | | return ira->codegen->invalid_inst_gen; |
| 25519 | | } else { |
| 25520 | | src_ptr_const = true; |
| 25521 | | src_ptr_volatile = false; |
| 25522 | | |
| 25523 | | if ((err = type_resolve(ira->codegen, target->value->type, ResolveStatusAlignmentKnown))) |
| 25524 | | return ira->codegen->invalid_inst_gen; |
| 25525 | | |
| 25526 | | src_ptr_align = get_abi_alignment(ira->codegen, target->value->type); |
| 25527 | | } |
| 25528 | | |
| 25529 | | if (src_ptr_align != 0) { |
| 25530 | | if ((err = type_resolve(ira->codegen, dest_child_type, ResolveStatusAlignmentKnown))) |
| 25531 | | return ira->codegen->invalid_inst_gen; |
| 25532 | | } |
| 25533 | | |
| 25534 | | ZigType *dest_ptr_type = get_pointer_to_type_extra(ira->codegen, dest_child_type, |
| 25535 | | src_ptr_const, src_ptr_volatile, PtrLenUnknown, |
| 25536 | | src_ptr_align, 0, 0, false); |
| 25537 | | ZigType *dest_slice_type = get_slice_type(ira->codegen, dest_ptr_type); |
| 25538 | | |
| 25539 | | ZigType *u8_ptr = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8, |
| 25540 | | src_ptr_const, src_ptr_volatile, PtrLenUnknown, |
| 25541 | | src_ptr_align, 0, 0, false); |
| 25542 | | ZigType *u8_slice = get_slice_type(ira->codegen, u8_ptr); |
| 25543 | | |
| 25544 | | IrInstGen *casted_value = ir_implicit_cast2(ira, &instruction->target->base, target, u8_slice); |
| 25545 | | if (type_is_invalid(casted_value->value->type)) |
| 25546 | | return ira->codegen->invalid_inst_gen; |
| 25547 | | |
| 25548 | | bool have_known_len = false; |
| 25549 | | uint64_t known_len; |
| 25550 | | |
| 25551 | | if (instr_is_comptime(casted_value)) { |
| 25552 | | ZigValue *val = ir_resolve_const(ira, casted_value, UndefBad); |
| 25553 | | if (!val) |
| 25554 | | return ira->codegen->invalid_inst_gen; |
| 25555 | | |
| 25556 | | ZigValue *len_val = val->data.x_struct.fields[slice_len_index]; |
| 25557 | | if (value_is_comptime(len_val)) { |
| 25558 | | known_len = bigint_as_u64(&len_val->data.x_bigint); |
| 25559 | | have_known_len = true; |
| 25560 | | } |
| 25561 | | } |
| 25562 | | |
| 25563 | | IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc, |
| 25564 | | dest_slice_type, nullptr, true, true); |
| 25565 | | if (result_loc != nullptr && (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable)) { |
| 25566 | | return result_loc; |
| 25567 | | } |
| 25568 | | |
| 25569 | | if (target->value->type->id == ZigTypeIdPointer && |
| 25570 | | target->value->type->data.pointer.ptr_len == PtrLenSingle && |
| 25571 | | target->value->type->data.pointer.child_type->id == ZigTypeIdArray) |
| 25572 | | { |
| 25573 | | known_len = target->value->type->data.pointer.child_type->data.array.len; |
| 25574 | | have_known_len = true; |
| 25575 | | } else if (casted_value->value->data.rh_slice.id == RuntimeHintSliceIdLen) { |
| 25576 | | known_len = casted_value->value->data.rh_slice.len; |
| 25577 | | have_known_len = true; |
| 25578 | | } |
| 25579 | | |
| 25580 | | if (have_known_len) { |
| 25581 | | if ((err = type_resolve(ira->codegen, dest_child_type, ResolveStatusSizeKnown))) |
| 25582 | | return ira->codegen->invalid_inst_gen; |
| 25583 | | uint64_t child_type_size = type_size(ira->codegen, dest_child_type); |
| 25584 | | uint64_t remainder = known_len % child_type_size; |
| 25585 | | if (remainder != 0) { |
| 25586 | | ErrorMsg *msg = ir_add_error(ira, &instruction->base.base, |
| 25587 | | buf_sprintf("unable to convert [%" ZIG_PRI_u64 "]u8 to %s: size mismatch", |
| 25588 | | known_len, buf_ptr(&dest_slice_type->name))); |
| 25589 | | add_error_note(ira->codegen, msg, instruction->dest_child_type->base.source_node, |
| 25590 | | buf_sprintf("%s has size %" ZIG_PRI_u64 "; remaining bytes: %" ZIG_PRI_u64, |
| 25591 | | buf_ptr(&dest_child_type->name), child_type_size, remainder)); |
| 25592 | | return ira->codegen->invalid_inst_gen; |
| 25593 | | } |
| 25594 | | } |
| 25595 | | |
| 25596 | | return ir_build_resize_slice(ira, &instruction->base.base, casted_value, dest_slice_type, result_loc); |
| 25597 | | } |
| 25598 | | |
| 25599 | | static IrInstGen *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstSrcToBytes *instruction) { |
| 25600 | | Error err; |
| 25601 | | |
| 25602 | | IrInstGen *target = instruction->target->child; |
| 25603 | | if (type_is_invalid(target->value->type)) |
| 25604 | | return ira->codegen->invalid_inst_gen; |
| 25605 | | |
| 25606 | | if (!is_slice(target->value->type)) { |
| 25607 | | ir_add_error(ira, &instruction->target->base, |
| 25608 | | buf_sprintf("expected slice, found '%s'", buf_ptr(&target->value->type->name))); |
| 25609 | | return ira->codegen->invalid_inst_gen; |
| 25610 | | } |
| 25611 | | |
| 25612 | | ZigType *src_ptr_type = target->value->type->data.structure.fields[slice_ptr_index]->type_entry; |
| 25613 | | |
| 25614 | | uint32_t alignment; |
| 25615 | | if ((err = resolve_ptr_align(ira, src_ptr_type, &alignment))) |
| 25616 | | return ira->codegen->invalid_inst_gen; |
| 25617 | | |
| 25618 | | ZigType *dest_ptr_type = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8, |
| 25619 | | src_ptr_type->data.pointer.is_const, src_ptr_type->data.pointer.is_volatile, PtrLenUnknown, |
| 25620 | | alignment, 0, 0, false); |
| 25621 | | ZigType *dest_slice_type = get_slice_type(ira->codegen, dest_ptr_type); |
| 25622 | | |
| 25623 | | if (instr_is_comptime(target)) { |
| 25624 | | ZigValue *target_val = ir_resolve_const(ira, target, UndefBad); |
| 25625 | | if (target_val == nullptr) |
| 25626 | | return ira->codegen->invalid_inst_gen; |
| 25627 | | |
| 25628 | | IrInstGen *result = ir_const(ira, &instruction->base.base, dest_slice_type); |
| 25629 | | result->value->data.x_struct.fields = alloc_const_vals_ptrs(ira->codegen, 2); |
| 25630 | | |
| 25631 | | ZigValue *ptr_val = result->value->data.x_struct.fields[slice_ptr_index]; |
| 25632 | | ZigValue *target_ptr_val = target_val->data.x_struct.fields[slice_ptr_index]; |
| 25633 | | copy_const_val(ira->codegen, ptr_val, target_ptr_val); |
| 25634 | | ptr_val->type = dest_ptr_type; |
| 25635 | | |
| 25636 | | ZigValue *len_val = result->value->data.x_struct.fields[slice_len_index]; |
| 25637 | | len_val->special = ConstValSpecialStatic; |
| 25638 | | len_val->type = ira->codegen->builtin_types.entry_usize; |
| 25639 | | ZigValue *target_len_val = target_val->data.x_struct.fields[slice_len_index]; |
| 25640 | | ZigType *elem_type = src_ptr_type->data.pointer.child_type; |
| 25641 | | BigInt elem_size_bigint; |
| 25642 | | bigint_init_unsigned(&elem_size_bigint, type_size(ira->codegen, elem_type)); |
| 25643 | | bigint_mul(&len_val->data.x_bigint, &target_len_val->data.x_bigint, &elem_size_bigint); |
| 25644 | | |
| 25645 | | return result; |
| 25646 | | } |
| 25647 | | |
| 25648 | | IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc, |
| 25649 | | dest_slice_type, nullptr, true, true); |
| 25650 | | if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) { |
| 25651 | | return result_loc; |
| 25652 | | } |
| 25653 | | |
| 25654 | | return ir_build_resize_slice(ira, &instruction->base.base, target, dest_slice_type, result_loc); |
| 25655 | | } |
| 25656 | | |
| 25657 | 25408 | static Error resolve_ptr_align(IrAnalyze *ira, ZigType *ty, uint32_t *result_align) { |
| 25658 | 25409 | Error err; |
| 25659 | 25410 | |
| ... | ... | @@ -29783,10 +29534,6 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc |
| 29783 | 29534 | return ir_analyze_instruction_float_cast(ira, (IrInstSrcFloatCast *)instruction); |
| 29784 | 29535 | case IrInstSrcIdErrSetCast: |
| 29785 | 29536 | return ir_analyze_instruction_err_set_cast(ira, (IrInstSrcErrSetCast *)instruction); |
| 29786 | | case IrInstSrcIdFromBytes: |
| 29787 | | return ir_analyze_instruction_from_bytes(ira, (IrInstSrcFromBytes *)instruction); |
| 29788 | | case IrInstSrcIdToBytes: |
| 29789 | | return ir_analyze_instruction_to_bytes(ira, (IrInstSrcToBytes *)instruction); |
| 29790 | 29537 | case IrInstSrcIdIntToFloat: |
| 29791 | 29538 | return ir_analyze_instruction_int_to_float(ira, (IrInstSrcIntToFloat *)instruction); |
| 29792 | 29539 | case IrInstSrcIdFloatToInt: |
| ... | ... | @@ -30113,7 +29860,6 @@ bool ir_inst_gen_has_side_effects(IrInstGen *instruction) { |
| 30113 | 29860 | case IrInstGenIdCmpxchg: |
| 30114 | 29861 | case IrInstGenIdAssertZero: |
| 30115 | 29862 | case IrInstGenIdAssertNonNull: |
| 30116 | | case IrInstGenIdResizeSlice: |
| 30117 | 29863 | case IrInstGenIdPtrOfArrayToSlice: |
| 30118 | 29864 | case IrInstGenIdSlice: |
| 30119 | 29865 | case IrInstGenIdOptionalWrap: |
| ... | ... | @@ -30339,8 +30085,6 @@ bool ir_inst_src_has_side_effects(IrInstSrc *instruction) { |
| 30339 | 30085 | case IrInstSrcIdIntToFloat: |
| 30340 | 30086 | case IrInstSrcIdFloatToInt: |
| 30341 | 30087 | case IrInstSrcIdBoolToInt: |
| 30342 | | case IrInstSrcIdFromBytes: |
| 30343 | | case IrInstSrcIdToBytes: |
| 30344 | 30088 | case IrInstSrcIdEnumToInt: |
| 30345 | 30089 | case IrInstSrcIdHasDecl: |
| 30346 | 30090 | case IrInstSrcIdAlloca: |