lang: Verify slice growth with plain elements
f61c1cb152bc30b44c3f07631c9ddec791f70915acfd84cf28d4deac960452db
Safe programs that grow a slice with `append`, `reserve`, `resize` or `extend` were rejected by the RIL verifier: the allocator returned `*mut opaque`, and the growth code stored the new pointer into a slice header, which the verifier cannot allow. An allocator may now return an owned `*mut [u8]`. Growth retypes those bytes with three new instructions: `slice.cast` makes a slice of plain elements from a byte slice and traps if it is too short or misaligned, `slice.len` sets a length up to the capacity, and `slice.copy` copies elements between slices. The verifier accepts them for plain element types only, because spare capacity of elements with owned pointers may hold stale pointers and copying them would duplicate those. The RV64 backend, the binary format and the printer support them. The old pointer-returning allocator keeps its lowering, so the seed still builds the compiler; the next commits move std and the compiler over. The allocator fixtures return byte slices now, and slice.append, slice.append.trait, slice.append.errors, slice.extend.bytes, slice.reserve.result and slice.resize.extend pass the verifier. Assisted-by: Claude:opus-5.5
1 parent
7855c231
lib/std/arch/rv64/isel.rad
+100 -0
| 968 | 968 | case il::Instr::SliceSub { dst, .. } => { |
|
| 969 | 969 | if dst.n == reg.n { |
|
| 970 | 970 | set written = SLICE_SIZE as u64; |
|
| 971 | 971 | } |
|
| 972 | 972 | } |
|
| 973 | + | case il::Instr::SliceCast { dst, .. } => { |
|
| 974 | + | if dst.n == reg.n { |
|
| 975 | + | set written = SLICE_SIZE as u64; |
|
| 976 | + | } |
|
| 977 | + | } |
|
| 973 | 978 | else => {} |
|
| 974 | 979 | } |
|
| 975 | 980 | return uses.count == 1 and written >= slotsEnd(layouts, l); |
|
| 976 | 981 | } |
|
| 977 | 982 | set at += 1; |
|
| 978 | 983 | } |
|
| 979 | 984 | return false; |
|
| 980 | 985 | } |
|
| 981 | 986 | ||
| 987 | + | /// Select the slice instructions that retype, resize and copy slices. |
|
| 988 | + | fn emitSliceOp( |
|
| 989 | + | s: &mut Selector, e: &mut emit::Emitter, layouts: &[il::Layout], instr: il::Instr |
|
| 990 | + | ) throws (super::Error) { |
|
| 991 | + | match instr { |
|
| 992 | + | case il::Instr::SliceCast { dst, src, layout, count } => |
|
| 993 | + | try emitSliceCast(s, e, dst, src, layouts[layout], count), |
|
| 994 | + | case il::Instr::SliceLen { dst, length, .. } => |
|
| 995 | + | try emitSliceLen(s, e, dst, length), |
|
| 996 | + | case il::Instr::SliceCopy { dst, src, layout } => |
|
| 997 | + | try emitSliceCopy(s, e, dst, src, layouts[layout]), |
|
| 998 | + | else => panic "not a slice instruction", |
|
| 999 | + | } |
|
| 1000 | + | } |
|
| 1001 | + | ||
| 1002 | + | /// Select `slice.cast`: bytes of the slice at `src` become `count` elements of `object`. |
|
| 1003 | + | fn emitSliceCast( |
|
| 1004 | + | s: &mut Selector, |
|
| 1005 | + | e: &mut emit::Emitter, |
|
| 1006 | + | dst: il::Reg, |
|
| 1007 | + | src: il::Reg, |
|
| 1008 | + | object: il::Layout, |
|
| 1009 | + | count: il::Val |
|
| 1010 | + | ) throws (super::Error) { |
|
| 1011 | + | // T1 holds the count, T3 the byte count and then the data pointer. The ranges |
|
| 1012 | + | // are checked before the destination is touched. |
|
| 1013 | + | try loadU32(s, e, super::SCRATCH1, super::SCRATCH1, count); |
|
| 1014 | + | try emitScale(e, super::SCRATCH3, super::SCRATCH1, object.size, super::ADDR_SCRATCH); |
|
| 1015 | + | let rsrc = try getSrcReg(s, e, src, super::SCRATCH2); |
|
| 1016 | + | try emit::emitLoad(e, super::ADDR_SCRATCH, rsrc, SLICE_LEN_OFFSET, il::Type::W32); |
|
| 1017 | + | try emitTrapUnlessLeu(e, super::SCRATCH3, super::ADDR_SCRATCH); |
|
| 1018 | + | try emit::emitLd(e, super::SCRATCH3, rsrc, SLICE_PTR_OFFSET); |
|
| 1019 | + | if object.alignment > 1 { |
|
| 1020 | + | try emit::emit(e, encode::andi( |
|
| 1021 | + | super::ADDR_SCRATCH, super::SCRATCH3, object.alignment as i32 - 1 |
|
| 1022 | + | )); |
|
| 1023 | + | try emit::emit(e, encode::beqz(super::ADDR_SCRATCH, super::INSTR_SIZE * 2)); |
|
| 1024 | + | try emit::emit(e, encode::ebreak()); |
|
| 1025 | + | } |
|
| 1026 | + | let rslot = try getSrcReg(s, e, dst, super::SCRATCH2); |
|
| 1027 | + | try emit::emitSd(e, super::SCRATCH3, rslot, SLICE_PTR_OFFSET); |
|
| 1028 | + | try emit::emitSw(e, super::SCRATCH1, rslot, SLICE_LEN_OFFSET); |
|
| 1029 | + | try emit::emitSw(e, super::SCRATCH1, rslot, SLICE_CAP_OFFSET); |
|
| 1030 | + | } |
|
| 1031 | + | ||
| 1032 | + | /// Select `slice.len`: set the length of the slice at `dst` after checking its capacity. |
|
| 1033 | + | fn emitSliceLen( |
|
| 1034 | + | s: &mut Selector, e: &mut emit::Emitter, dst: il::Reg, length: il::Val |
|
| 1035 | + | ) throws (super::Error) { |
|
| 1036 | + | try loadU32(s, e, super::SCRATCH1, super::SCRATCH1, length); |
|
| 1037 | + | let rslot = try getSrcReg(s, e, dst, super::SCRATCH2); |
|
| 1038 | + | try emit::emitLoad(e, super::ADDR_SCRATCH, rslot, SLICE_CAP_OFFSET, il::Type::W32); |
|
| 1039 | + | try emitTrapUnlessLeu(e, super::SCRATCH1, super::ADDR_SCRATCH); |
|
| 1040 | + | try emit::emitSw(e, super::SCRATCH1, rslot, SLICE_LEN_OFFSET); |
|
| 1041 | + | } |
|
| 1042 | + | ||
| 1043 | + | /// Select `slice.copy`: copy the elements of the slice at `src` to the start of `dst`. |
|
| 1044 | + | fn emitSliceCopy( |
|
| 1045 | + | s: &mut Selector, e: &mut emit::Emitter, dst: il::Reg, src: il::Reg, object: il::Layout |
|
| 1046 | + | ) throws (super::Error) { |
|
| 1047 | + | let mut unit = object.alignment; |
|
| 1048 | + | if unit > super::DWORD_SIZE as u32 { |
|
| 1049 | + | set unit = super::DWORD_SIZE as u32; |
|
| 1050 | + | } |
|
| 1051 | + | // The source length must fit the destination. Both pointers are then loaded in |
|
| 1052 | + | // place, since a spilled slice address sits in the register that receives its |
|
| 1053 | + | // pointer. T1 is the source cursor, T2 the destination cursor and T3 the end of |
|
| 1054 | + | // the source. |
|
| 1055 | + | let rdst = try getSrcReg(s, e, dst, super::SCRATCH2); |
|
| 1056 | + | try emit::emitLoad(e, super::ADDR_SCRATCH, rdst, SLICE_LEN_OFFSET, il::Type::W32); |
|
| 1057 | + | let rsrc = try getSrcReg(s, e, src, super::SCRATCH1); |
|
| 1058 | + | try emit::emitLoad(e, super::SCRATCH3, rsrc, SLICE_LEN_OFFSET, il::Type::W32); |
|
| 1059 | + | try emitTrapUnlessLeu(e, super::SCRATCH3, super::ADDR_SCRATCH); |
|
| 1060 | + | try emitScale(e, super::SCRATCH3, super::SCRATCH3, object.size, super::ADDR_SCRATCH); |
|
| 1061 | + | try emit::emitLd(e, super::SCRATCH1, rsrc, SLICE_PTR_OFFSET); |
|
| 1062 | + | try emit::emitLd(e, super::SCRATCH2, rdst, SLICE_PTR_OFFSET); |
|
| 1063 | + | try emit::emit(e, encode::add(super::SCRATCH3, super::SCRATCH3, super::SCRATCH1)); |
|
| 1064 | + | // Skip the loop when there is nothing to copy. |
|
| 1065 | + | try emit::emit(e, encode::beq(super::SCRATCH1, super::SCRATCH3, super::INSTR_SIZE * 6)); |
|
| 1066 | + | let start = e.code.len; |
|
| 1067 | + | try emit::emitSload(e, super::ADDR_SCRATCH, super::SCRATCH1, 0, accessType(unit)); |
|
| 1068 | + | try emit::emitStore(e, super::ADDR_SCRATCH, super::SCRATCH2, 0, accessType(unit)); |
|
| 1069 | + | try emit::emit(e, encode::addi(super::SCRATCH1, super::SCRATCH1, unit as i32)); |
|
| 1070 | + | try emit::emit(e, encode::addi(super::SCRATCH2, super::SCRATCH2, unit as i32)); |
|
| 1071 | + | let offset = (start as i32 - e.code.len as i32) * super::INSTR_SIZE; |
|
| 1072 | + | try emit::emit(e, encode::bne(super::SCRATCH1, super::SCRATCH3, offset)); |
|
| 1073 | + | } |
|
| 1074 | + | ||
| 982 | 1075 | /// Zero `size` bytes of the frame at SP plus `offset`. The region is 8-byte aligned. |
|
| 983 | 1076 | fn emitZeroFrame(e: &mut emit::Emitter, offset: i32, size: u32) throws (super::Error) { |
|
| 984 | 1077 | let words = (size / 8) as i32; |
|
| 985 | 1078 | if words > ZERO_UNROLL_WORDS { |
|
| 986 | 1079 | // Clear with a loop over a cursor. |
| 1391 | 1484 | super::ADDR_SCRATCH, super::ADDR_SCRATCH, super::SCRATCH3 |
|
| 1392 | 1485 | )); |
|
| 1393 | 1486 | } |
|
| 1394 | 1487 | try emit::emitSd(e, super::ADDR_SCRATCH, rslot, SLICE_PTR_OFFSET); |
|
| 1395 | 1488 | } |
|
| 1489 | + | case |
|
| 1490 | + | il::Instr::SliceCast { .. }, |
|
| 1491 | + | il::Instr::SliceLen { .. }, |
|
| 1492 | + | il::Instr::SliceCopy { .. } => |
|
| 1493 | + | { |
|
| 1494 | + | try emitSliceOp(s, e, layouts, instr); |
|
| 1495 | + | } |
|
| 1396 | 1496 | case il::Instr::LoadPtr { dst, src, offset, nullable } => { |
|
| 1397 | 1497 | let rd = getDstReg(s, dst, super::SCRATCH1); |
|
| 1398 | 1498 | let base = try getSrcReg(s, e, src, super::SCRATCH2); |
|
| 1399 | 1499 | try emit::emitLoad(e, rd, base, offset, il::Type::W64); |
|
| 1400 | 1500 | if not nullable { |
lib/std/arch/rv64/shared/tests/run.rad
+129 -0
| 168 | 168 | write(m, rd, read(m, rs1) + imm as i64 as u64); |
|
| 169 | 169 | } |
|
| 170 | 170 | case decode::Instr::Addiw { rd, rs1, imm } => { |
|
| 171 | 171 | write(m, rd, sext32(read(m, rs1) + imm as i64 as u64)); |
|
| 172 | 172 | } |
|
| 173 | + | case decode::Instr::Andi { rd, rs1, imm } => { |
|
| 174 | + | write(m, rd, read(m, rs1) & imm as i64 as u64); |
|
| 175 | + | } |
|
| 173 | 176 | case decode::Instr::Add { rd, rs1, rs2 } => { |
|
| 174 | 177 | write(m, rd, read(m, rs1) + read(m, rs2)); |
|
| 175 | 178 | } |
|
| 176 | 179 | case decode::Instr::Sub { rd, rs1, rs2 } => { |
|
| 177 | 180 | write(m, rd, read(m, rs1) - read(m, rs2)); |
| 591 | 594 | try expectTrap(try sliceSub(one, two, 20)); |
|
| 592 | 595 | try expectTrap(try sliceSub(two, one, 40)); |
|
| 593 | 596 | try expectTrap(try sliceSub(one, il::Val::Imm(8), 40)); |
|
| 594 | 597 | } |
|
| 595 | 598 | ||
| 599 | + | /// Write a slice object at `address`. |
|
| 600 | + | unsafe fn putSlice(address: u64, ptr: u64, len: u64, cap: u64) { |
|
| 601 | + | assert store(address, 8, ptr); |
|
| 602 | + | assert store(address + 8, 4, len); |
|
| 603 | + | assert store(address + 12, 4, cap); |
|
| 604 | + | } |
|
| 605 | + | ||
| 606 | + | /// Run `slice.cast` into the object at [`RESULT`] from bytes at `ptr`, `len` long, with a |
|
| 607 | + | /// count in an immediate or in a register. |
|
| 608 | + | unsafe fn sliceCast(ptr: u64, len: u64, count: u64, inRegister: bool) -> Built |
|
| 609 | + | throws (testing::TestError) |
|
| 610 | + | { |
|
| 611 | + | let mut operand = il::Val::Imm(count as i64); |
|
| 612 | + | if inRegister { |
|
| 613 | + | set operand = il::Val::Reg(il::Reg { n: 2 }); |
|
| 614 | + | } |
|
| 615 | + | let package = try compile( |
|
| 616 | + | il::Instr::SliceCast { |
|
| 617 | + | dst: il::Reg { n: 0 }, src: il::Reg { n: 1 }, layout: 0, count: operand |
|
| 618 | + | }, |
|
| 619 | + | 3, |
|
| 620 | + | nil |
|
| 621 | + | ) catch { |
|
| 622 | + | throw testing::TestError::Failed; |
|
| 623 | + | }; |
|
| 624 | + | let mut m = boot(&[RESULT, SOURCE, count]); |
|
| 625 | + | putSlice(SOURCE, ptr, len, len + 10); |
|
| 626 | + | putSlice(RESULT, 0xbad, 0xbad, 0xbad); |
|
| 627 | + | return built(run(package.code, &mut m)); |
|
| 628 | + | } |
|
| 629 | + | ||
| 630 | + | /// Run `slice.len` on the object at [`RESULT`], which holds three elements and has room for ten. |
|
| 631 | + | unsafe fn sliceLen(length: i64, inRegister: bool) -> Built throws (testing::TestError) { |
|
| 632 | + | let mut operand = il::Val::Imm(length); |
|
| 633 | + | if inRegister { |
|
| 634 | + | set operand = il::Val::Reg(il::Reg { n: 1 }); |
|
| 635 | + | } |
|
| 636 | + | let package = try compile( |
|
| 637 | + | il::Instr::SliceLen { dst: il::Reg { n: 0 }, layout: 0, length: operand }, 2, nil |
|
| 638 | + | ) catch { |
|
| 639 | + | throw testing::TestError::Failed; |
|
| 640 | + | }; |
|
| 641 | + | let mut m = boot(&[RESULT, length as u64]); |
|
| 642 | + | putSlice(RESULT, POINTER, 3, 10); |
|
| 643 | + | return built(run(package.code, &mut m)); |
|
| 644 | + | } |
|
| 645 | + | ||
| 646 | + | /// Run `slice.copy` of `from` elements into a slice of `to` elements. The elements of the |
|
| 647 | + | /// source are `1 ..= from`, and the bytes around the target area stay at [`UNTOUCHED`]. |
|
| 648 | + | unsafe fn sliceCopy(from: u64, to: u64) -> Outcome throws (testing::TestError) { |
|
| 649 | + | let package = try compile( |
|
| 650 | + | il::Instr::SliceCopy { dst: il::Reg { n: 0 }, src: il::Reg { n: 1 }, layout: 0 }, 2, nil |
|
| 651 | + | ) catch { |
|
| 652 | + | throw testing::TestError::Failed; |
|
| 653 | + | }; |
|
| 654 | + | let mut m = boot(&[RESULT, SOURCE]); |
|
| 655 | + | putSlice(SOURCE, SOURCE_AREA, from, from); |
|
| 656 | + | putSlice(RESULT, TARGET_AREA, to, to); |
|
| 657 | + | for i in 0 .. from { |
|
| 658 | + | assert store(SOURCE_AREA + i * ELEMENT, 8, i + 1); |
|
| 659 | + | } |
|
| 660 | + | for i in 0 .. 8 { |
|
| 661 | + | assert store(TARGET_AREA + i * ELEMENT, 8, 0xaaaaaaaaaaaaaaaa); |
|
| 662 | + | } |
|
| 663 | + | return run(package.code, &mut m); |
|
| 664 | + | } |
|
| 665 | + | ||
| 666 | + | /// The count of a cast is a 32-bit value, the bytes must hold that many elements, and the |
|
| 667 | + | /// pointer must be aligned to the element. The result has the count as length and capacity. |
|
| 668 | + | @test unsafe fn sliceCastBounds() throws (testing::TestError) { |
|
| 669 | + | for form in 0 .. 2 { |
|
| 670 | + | let reg = form == 1; |
|
| 671 | + | let b = try sliceCast(POINTER, 32, 4, reg); |
|
| 672 | + | try testing::expect(finished(b.outcome)); |
|
| 673 | + | try testing::expect(b.ptr == POINTER and b.len == 4 and b.cap == 4); |
|
| 674 | + | let shorter = try sliceCast(POINTER, 36, 2, reg); |
|
| 675 | + | try testing::expect(finished(shorter.outcome)); |
|
| 676 | + | try testing::expect(shorter.ptr == POINTER and shorter.len == 2 and shorter.cap == 2); |
|
| 677 | + | let empty = try sliceCast(POINTER, 0, 0, reg); |
|
| 678 | + | try testing::expect(finished(empty.outcome)); |
|
| 679 | + | try testing::expect(empty.len == 0 and empty.cap == 0); |
|
| 680 | + | // The result is untouched when a check fails. |
|
| 681 | + | let long = try sliceCast(POINTER, 32, 5, reg); |
|
| 682 | + | try testing::expect(trapped(long.outcome) and long.ptr == 0xbad and long.len == 0xbad); |
|
| 683 | + | try expectTrap(try sliceCast(POINTER, 39, 5, reg)); |
|
| 684 | + | try expectTrap(try sliceCast(POINTER + 4, 32, 4, reg)); |
|
| 685 | + | try expectTrap(try sliceCast(POINTER + 1, 32, 0, reg)); |
|
| 686 | + | // Only the low 32 bits of a count in a register or an immediate select the count. |
|
| 687 | + | try expectTrap(try sliceCast(POINTER, 32, 0x100000004 + 1, reg)); |
|
| 688 | + | } |
|
| 689 | + | } |
|
| 690 | + | ||
| 691 | + | /// A length up to the capacity is accepted, and a larger one traps without a change. |
|
| 692 | + | @test unsafe fn sliceLenBounds() throws (testing::TestError) { |
|
| 693 | + | for form in 0 .. 2 { |
|
| 694 | + | let reg = form == 1; |
|
| 695 | + | let lengths: [u32; 3] = [0, 3, 10]; |
|
| 696 | + | for length in lengths { |
|
| 697 | + | let b = try sliceLen(length as i64, reg); |
|
| 698 | + | try testing::expect(finished(b.outcome)); |
|
| 699 | + | try testing::expect(b.ptr == POINTER and b.len == length as u64 and b.cap == 10); |
|
| 700 | + | } |
|
| 701 | + | let over = try sliceLen(11, reg); |
|
| 702 | + | try testing::expect(trapped(over.outcome) and over.len == 3); |
|
| 703 | + | try expectTrap(try sliceLen(0x100000000 + 11, reg)); |
|
| 704 | + | try expectTrap(try sliceLen(-1, reg)); |
|
| 705 | + | } |
|
| 706 | + | } |
|
| 707 | + | ||
| 708 | + | /// A copy moves exactly the elements of the source, and traps when they do not fit. |
|
| 709 | + | @test unsafe fn sliceCopyBounds() throws (testing::TestError) { |
|
| 710 | + | let counts: [u64; 4] = [0, 1, 2, 5]; |
|
| 711 | + | for n in counts { |
|
| 712 | + | let outcome = try sliceCopy(n, 6); |
|
| 713 | + | try testing::expect(finished(outcome)); |
|
| 714 | + | for i in 0 .. 8 { |
|
| 715 | + | let value = load(TARGET_AREA + i * ELEMENT, 8) else panic "copy: outside memory"; |
|
| 716 | + | let want: u64 = i + 1 if i < n else 0xaaaaaaaaaaaaaaaa; |
|
| 717 | + | try testing::expect(value == want); |
|
| 718 | + | } |
|
| 719 | + | } |
|
| 720 | + | try testing::expect(trapped(try sliceCopy(7, 6))); |
|
| 721 | + | let tight = try sliceCopy(6, 6); |
|
| 722 | + | try testing::expect(finished(tight)); |
|
| 723 | + | } |
|
| 724 | + | ||
| 596 | 725 | /// An immediate index is an unsigned 32-bit value, like the same value in a register: only its |
|
| 597 | 726 | /// low 32 bits select the element, and an index at or above the count traps. |
|
| 598 | 727 | @test unsafe fn elemImmediateIndex() throws (testing::TestError) { |
|
| 599 | 728 | let indexes: [i64; 10] = [ |
|
| 600 | 729 | 0, 7, 8, 0xffffffff, 0x100000000, 0x100000001, 0x100000008, -1, -8, 0x7fffffff00000003 |
lib/std/lang/il.rad
+57 -0
| 404 | 404 | /// Index of the first element in the new slice. |
|
| 405 | 405 | start: Val, |
|
| 406 | 406 | /// Index past the last element in the new slice. |
|
| 407 | 407 | end: Val, |
|
| 408 | 408 | }, |
|
| 409 | + | /// Retype bytes as elements: `slice.cast %dst %src $layout <count>;`. |
|
| 410 | + | /// |
|
| 411 | + | /// `src` addresses a slice object of plain one-byte elements, and `dst` a slice |
|
| 412 | + | /// object whose element layout is the plain, nonzero-sized `layout`. The |
|
| 413 | + | /// instruction traps unless the data pointer is aligned for `layout` and holds at |
|
| 414 | + | /// least `count` elements, and then writes the same data pointer, a length of |
|
| 415 | + | /// `count`, and a capacity of `count`. `count` is an unsigned 32-bit value. |
|
| 416 | + | SliceCast { |
|
| 417 | + | /// Address of the slice object that receives the result. |
|
| 418 | + | dst: Reg, |
|
| 419 | + | /// Address of the byte slice object. |
|
| 420 | + | src: Reg, |
|
| 421 | + | /// Index of the element layout in `Program.layouts`. |
|
| 422 | + | layout: u32, |
|
| 423 | + | /// Number of elements in the result. |
|
| 424 | + | count: Val, |
|
| 425 | + | }, |
|
| 426 | + | /// Set the length of a slice: `slice.len %dst $layout <length>;`. |
|
| 427 | + | /// |
|
| 428 | + | /// `dst` addresses a slice object of element layout `layout`. The instruction |
|
| 429 | + | /// traps unless `length` is at most the capacity, and then writes the length. |
|
| 430 | + | /// `length` is an unsigned 32-bit value. |
|
| 431 | + | SliceLen { |
|
| 432 | + | /// Address of the slice object. |
|
| 433 | + | dst: Reg, |
|
| 434 | + | /// Index of the element layout in `Program.layouts`. |
|
| 435 | + | layout: u32, |
|
| 436 | + | /// New length. |
|
| 437 | + | length: Val, |
|
| 438 | + | }, |
|
| 439 | + | /// Copy the elements of a slice: `slice.copy %dst %src $layout;`. |
|
| 440 | + | /// |
|
| 441 | + | /// `dst` and `src` address slice objects of the same plain element layout. The |
|
| 442 | + | /// instruction traps unless the length of `src` is at most the length of `dst`, and |
|
| 443 | + | /// then copies the elements of `src` to the start of `dst`. The two ranges must not |
|
| 444 | + | /// overlap. |
|
| 445 | + | SliceCopy { |
|
| 446 | + | /// Address of the destination slice object. |
|
| 447 | + | dst: Reg, |
|
| 448 | + | /// Address of the source slice object. |
|
| 449 | + | src: Reg, |
|
| 450 | + | /// Index of the element layout in `Program.layouts`. |
|
| 451 | + | layout: u32, |
|
| 452 | + | }, |
|
| 409 | 453 | /// Load a pointer or function value from a slot: `load.ptr %dst %src <offset>;`. |
|
| 410 | 454 | /// The load traps on zero unless it has the `nullable` qualifier. |
|
| 411 | 455 | LoadPtr { |
|
| 412 | 456 | /// Register that receives the value. |
|
| 413 | 457 | dst: Reg, |
| 970 | 1014 | visitor.visit(dst); |
|
| 971 | 1015 | visitor.visit(src); |
|
| 972 | 1016 | withReg(start, visitor); |
|
| 973 | 1017 | withReg(end, visitor); |
|
| 974 | 1018 | } |
|
| 1019 | + | case Instr::SliceCast { dst, src, count, .. } => { |
|
| 1020 | + | visitor.visit(dst); |
|
| 1021 | + | visitor.visit(src); |
|
| 1022 | + | withReg(count, visitor); |
|
| 1023 | + | } |
|
| 1024 | + | case Instr::SliceLen { dst, length, .. } => { |
|
| 1025 | + | visitor.visit(dst); |
|
| 1026 | + | withReg(length, visitor); |
|
| 1027 | + | } |
|
| 1028 | + | case Instr::SliceCopy { dst, src, .. } => { |
|
| 1029 | + | visitor.visit(dst); |
|
| 1030 | + | visitor.visit(src); |
|
| 1031 | + | } |
|
| 975 | 1032 | case Instr::LoadPtr { src, .. } => visitor.visit(src), |
|
| 976 | 1033 | case Instr::StorePtr { src, dst, .. } => { |
|
| 977 | 1034 | withReg(src, visitor); |
|
| 978 | 1035 | visitor.visit(dst); |
|
| 979 | 1036 | } |
lib/std/lang/il/binary.rad
+6 -0
| 156 | 156 | export constant INSTR_TRAIT_NEW: u8 = 29; |
|
| 157 | 157 | /// Wire tag for instruction TraitCall. |
|
| 158 | 158 | export constant INSTR_TRAIT_CALL: u8 = 30; |
|
| 159 | 159 | /// Wire tag for instruction TraitCopy. |
|
| 160 | 160 | export constant INSTR_TRAIT_COPY: u8 = 31; |
|
| 161 | + | /// Wire tag for instruction SliceCast. |
|
| 162 | + | export constant INSTR_SLICE_CAST: u8 = 32; |
|
| 163 | + | /// Wire tag for instruction SliceLen. |
|
| 164 | + | export constant INSTR_SLICE_LEN: u8 = 33; |
|
| 165 | + | /// Wire tag for instruction SliceCopy. |
|
| 166 | + | export constant INSTR_SLICE_COPY: u8 = 34; |
|
| 161 | 167 | ||
| 162 | 168 | /// Wire tag for data Val. |
|
| 163 | 169 | export constant DATA_VAL: u8 = 0; |
|
| 164 | 170 | /// Wire tag for data Sym. |
|
| 165 | 171 | export constant DATA_SYM: u8 = 1; |
lib/std/lang/il/binary/collect.rad
+3 -0
| 173 | 173 | } |
|
| 174 | 174 | case il::Instr::SliceSub { start, end, .. } => { |
|
| 175 | 175 | try value(names, owner, start, a); |
|
| 176 | 176 | try value(names, owner, end, a); |
|
| 177 | 177 | } |
|
| 178 | + | case il::Instr::SliceCast { count, .. } => try value(names, owner, count, a), |
|
| 179 | + | case il::Instr::SliceLen { length, .. } => try value(names, owner, length, a), |
|
| 178 | 180 | case il::Instr::Elem { index, .. } => try value(names, owner, index, a), |
|
| 179 | 181 | case il::Instr::SliceElem { index, .. } => try value(names, owner, index, a), |
|
| 180 | 182 | case il::Instr::StorePtr { src, .. } => try value(names, owner, src, a), |
|
| 181 | 183 | case il::Instr::TraitNew { table, .. } => try value(names, owner, table, a), |
|
| 182 | 184 | case il::Instr::TraitCall { args, .. } => |
|
| 183 | 185 | try values(names, owner, il::argVals(func, args), a), |
|
| 184 | 186 | case |
|
| 185 | 187 | il::Instr::Field { .. }, |
|
| 186 | 188 | il::Instr::TraitCopy { .. }, |
|
| 189 | + | il::Instr::SliceCopy { .. }, |
|
| 187 | 190 | il::Instr::LoadPtr { .. }, |
|
| 188 | 191 | il::Instr::Reserve { .. }, |
|
| 189 | 192 | il::Instr::Blit { .. }, |
|
| 190 | 193 | il::Instr::Load { .. }, |
|
| 191 | 194 | il::Instr::Sload { .. }, |
lib/std/lang/il/binary/reader.rad
+18 -0
| 288 | 288 | case super::CMP_ULT => return il::CmpOp::Ult, |
|
| 289 | 289 | else => throw binary::Error::Invalid, |
|
| 290 | 290 | } |
|
| 291 | 291 | } |
|
| 292 | 292 | ||
| 293 | + | /// Read the operands of a slice instruction that changes the type, length or contents of |
|
| 294 | + | /// a slice, which has wire tag `tag`. |
|
| 295 | + | fn sliceInstr(input: &mut Reader, wire: &[u8], tag: u8) -> il::Instr throws (binary::Error) { |
|
| 296 | + | let dst = try reg(input, wire); |
|
| 297 | + | if tag == super::INSTR_SLICE_LEN { |
|
| 298 | + | let layout = try layoutIndex(input, wire); |
|
| 299 | + | return il::Instr::SliceLen { dst, layout, length: try val(input, wire) }; |
|
| 300 | + | } |
|
| 301 | + | let src = try reg(input, wire); |
|
| 302 | + | let layout = try layoutIndex(input, wire); |
|
| 303 | + | if tag == super::INSTR_SLICE_CAST { |
|
| 304 | + | return il::Instr::SliceCast { dst, src, layout, count: try val(input, wire) }; |
|
| 305 | + | } |
|
| 306 | + | return il::Instr::SliceCopy { dst, src, layout }; |
|
| 307 | + | } |
|
| 308 | + | ||
| 293 | 309 | /// Read one instruction and reconstruct its operand sequences. |
|
| 294 | 310 | export fn instr(input: &mut Reader, wire: &[u8], a: &mut opaque alloc::Allocator) -> il::Instr |
|
| 295 | 311 | throws (binary::Error) |
|
| 296 | 312 | { |
|
| 297 | 313 | let tag = try integer(input, wire, 1) as u8; |
| 507 | 523 | let vend = try val(input, wire); |
|
| 508 | 524 | return il::Instr::SliceSub { |
|
| 509 | 525 | dst: vdst, src: vsrc, layout: vlayout, start: vstart, end: vend |
|
| 510 | 526 | }; |
|
| 511 | 527 | } |
|
| 528 | + | case super::INSTR_SLICE_CAST, super::INSTR_SLICE_LEN, super::INSTR_SLICE_COPY => |
|
| 529 | + | return try sliceInstr(input, wire, tag), |
|
| 512 | 530 | case super::INSTR_LOAD_PTR => { |
|
| 513 | 531 | let vdst = try reg(input, wire); |
|
| 514 | 532 | let vsrc = try reg(input, wire); |
|
| 515 | 533 | let voffset = try integer(input, wire, 4) as i32; |
|
| 516 | 534 | let nullable = try flag(input, wire); |
lib/std/lang/il/binary/tests.rad
+9 -0
| 168 | 168 | src: il::Reg { n: 2 }, |
|
| 169 | 169 | layout: 3, |
|
| 170 | 170 | start: il::Val::Undef, |
|
| 171 | 171 | end: il::Val::Undef, |
|
| 172 | 172 | }, &[25, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 4]); |
|
| 173 | + | try instruction(il::Instr::SliceCast { |
|
| 174 | + | dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, layout: 3, count: il::Val::Undef |
|
| 175 | + | }, &[32, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4]); |
|
| 176 | + | try instruction(il::Instr::SliceLen { |
|
| 177 | + | dst: il::Reg { n: 1 }, layout: 3, length: il::Val::Undef |
|
| 178 | + | }, &[33, 1, 0, 0, 0, 3, 0, 0, 0, 4]); |
|
| 179 | + | try instruction(il::Instr::SliceCopy { |
|
| 180 | + | dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, layout: 3 |
|
| 181 | + | }, &[34, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]); |
|
| 173 | 182 | try instruction(il::Instr::LoadPtr { |
|
| 174 | 183 | dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, offset: -1, nullable: false |
|
| 175 | 184 | }, &[26, 1, 0, 0, 0, 2, 0, 0, 0, 255, 255, 255, 255, 0]); |
|
| 176 | 185 | try instruction(il::Instr::LoadPtr { |
|
| 177 | 186 | dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, offset: 8, nullable: true |
lib/std/lang/il/binary/writer.rad
+19 -0
| 483 | 483 | try integer(out, vsrc.n as u64, 4); |
|
| 484 | 484 | try integer(out, vlayout as u64, 4); |
|
| 485 | 485 | try val(out, vstart); |
|
| 486 | 486 | try val(out, vend); |
|
| 487 | 487 | } |
|
| 488 | + | case il::Instr::SliceCast { dst: vdst, src: vsrc, layout: vlayout, count: vcount } => { |
|
| 489 | + | try integer(out, super::INSTR_SLICE_CAST as u64, 1); |
|
| 490 | + | try integer(out, vdst.n as u64, 4); |
|
| 491 | + | try integer(out, vsrc.n as u64, 4); |
|
| 492 | + | try integer(out, vlayout as u64, 4); |
|
| 493 | + | try val(out, vcount); |
|
| 494 | + | } |
|
| 495 | + | case il::Instr::SliceLen { dst: vdst, layout: vlayout, length: vlength } => { |
|
| 496 | + | try integer(out, super::INSTR_SLICE_LEN as u64, 1); |
|
| 497 | + | try integer(out, vdst.n as u64, 4); |
|
| 498 | + | try integer(out, vlayout as u64, 4); |
|
| 499 | + | try val(out, vlength); |
|
| 500 | + | } |
|
| 501 | + | case il::Instr::SliceCopy { dst: vdst, src: vsrc, layout: vlayout } => { |
|
| 502 | + | try integer(out, super::INSTR_SLICE_COPY as u64, 1); |
|
| 503 | + | try integer(out, vdst.n as u64, 4); |
|
| 504 | + | try integer(out, vsrc.n as u64, 4); |
|
| 505 | + | try integer(out, vlayout as u64, 4); |
|
| 506 | + | } |
|
| 488 | 507 | case il::Instr::LoadPtr { dst: vdst, src: vsrc, offset: voffset, nullable } => { |
|
| 489 | 508 | try integer(out, super::INSTR_LOAD_PTR as u64, 1); |
|
| 490 | 509 | try integer(out, vdst.n as u64, 4); |
|
| 491 | 510 | try integer(out, vsrc.n as u64, 4); |
|
| 492 | 511 | try integer(out, voffset as u64, 4); |
lib/std/lang/il/printer.rad
+26 -0
| 303 | 303 | out.write(" "); |
|
| 304 | 304 | writeVal(out, start); |
|
| 305 | 305 | out.write(" "); |
|
| 306 | 306 | writeVal(out, end); |
|
| 307 | 307 | } |
|
| 308 | + | case super::Instr::SliceCast { dst, src, layout, count } => { |
|
| 309 | + | out.write("slice.cast "); |
|
| 310 | + | writeReg(out, dst); |
|
| 311 | + | out.write(" "); |
|
| 312 | + | writeReg(out, src); |
|
| 313 | + | out.write(" "); |
|
| 314 | + | writeIndex(out, "L", layout); |
|
| 315 | + | out.write(" "); |
|
| 316 | + | writeVal(out, count); |
|
| 317 | + | } |
|
| 318 | + | case super::Instr::SliceLen { dst, layout, length } => { |
|
| 319 | + | out.write("slice.len "); |
|
| 320 | + | writeReg(out, dst); |
|
| 321 | + | out.write(" "); |
|
| 322 | + | writeIndex(out, "L", layout); |
|
| 323 | + | out.write(" "); |
|
| 324 | + | writeVal(out, length); |
|
| 325 | + | } |
|
| 326 | + | case super::Instr::SliceCopy { dst, src, layout } => { |
|
| 327 | + | out.write("slice.copy "); |
|
| 328 | + | writeReg(out, dst); |
|
| 329 | + | out.write(" "); |
|
| 330 | + | writeReg(out, src); |
|
| 331 | + | out.write(" "); |
|
| 332 | + | writeIndex(out, "L", layout); |
|
| 333 | + | } |
|
| 308 | 334 | case super::Instr::LoadPtr { dst, src, offset, nullable } => { |
|
| 309 | 335 | out.write("load.ptr "); |
|
| 310 | 336 | if nullable { |
|
| 311 | 337 | out.write("nullable "); |
|
| 312 | 338 | } |
lib/std/lang/il/printer/tests.rad
+17 -0
| 457 | 457 | start: il::Val::Imm(0), |
|
| 458 | 458 | end: il::Val::Imm(1), |
|
| 459 | 459 | }, |
|
| 460 | 460 | a |
|
| 461 | 461 | ); |
|
| 462 | + | try! instrs.append( |
|
| 463 | + | il::Instr::SliceCast { |
|
| 464 | + | dst: il::Reg { n: 5 }, src: il::Reg { n: 4 }, layout: 4, count: il::Val::Imm(3) |
|
| 465 | + | }, |
|
| 466 | + | a |
|
| 467 | + | ); |
|
| 468 | + | try! instrs.append( |
|
| 469 | + | il::Instr::SliceLen { |
|
| 470 | + | dst: il::Reg { n: 5 }, layout: 4, length: il::Val::Reg(il::Reg { n: 9 }) |
|
| 471 | + | }, |
|
| 472 | + | a |
|
| 473 | + | ); |
|
| 474 | + | try! instrs.append( |
|
| 475 | + | il::Instr::SliceCopy { dst: il::Reg { n: 5 }, src: il::Reg { n: 4 }, layout: 4 }, |
|
| 476 | + | a |
|
| 477 | + | ); |
|
| 462 | 478 | try! instrs.append( |
|
| 463 | 479 | il::Instr::LoadPtr { |
|
| 464 | 480 | dst: il::Reg { n: 6 }, src: il::Reg { n: 0 }, offset: 16, nullable: false |
|
| 465 | 481 | }, |
|
| 466 | 482 | a |
| 514 | 530 | &TABLES[.. out.pos], |
|
| 515 | 531 | &[ |
|
| 516 | 532 | "fn void $f() {\n @entry0\n", |
|
| 517 | 533 | " field %1 %0 8;\n elem %2 %1 %9 $L4 3;\n slice.elem %3 %1 2 $L4;\n", |
|
| 518 | 534 | " slice.new %4 %2 $L4 3 1 %9;\n slice.sub %5 %4 $L4 0 1;\n", |
|
| 535 | + | " slice.cast %5 %4 $L4 3;\n slice.len %5 $L4 %9;\n slice.copy %5 %4 $L4;\n", |
|
| 519 | 536 | " load.ptr %6 %0 16;\n load.ptr nullable %7 %0 16;\n store.ptr %7 %0 16;\n ret;\n}\n", |
|
| 520 | 537 | ] |
|
| 521 | 538 | ); |
|
| 522 | 539 | } |
|
| 523 | 540 |
lib/std/lang/il/verify.rad
+46 -4
| 668 | 668 | if not layout::arrayView(t, p.layout, o, e, count) { |
|
| 669 | 669 | throw fail(v, Rule::View); |
|
| 670 | 670 | } |
|
| 671 | 671 | } |
|
| 672 | 672 | ||
| 673 | + | /// The slice slot that a pointer addresses. |
|
| 674 | + | fn sliceAt(v: &Verifier, p: PtrState) -> il::PtrType throws (Error) { |
|
| 675 | + | let o = try extent(v, p, 0, 0); |
|
| 676 | + | let case il::SlotKind::Slice(slot) = layout::slotAt(&v.ctx.table, p.layout, o) else { |
|
| 677 | + | throw fail(v, Rule::Slot); |
|
| 678 | + | }; |
|
| 679 | + | return slot; |
|
| 680 | + | } |
|
| 681 | + | ||
| 673 | 682 | /// The slice slot that a pointer addresses. A plain view must have the same |
|
| 674 | 683 | /// element size as the slot. |
|
| 675 | 684 | fn sliceSlot(v: &Verifier, p: PtrState, e: u32) -> il::PtrType throws (Error) { |
|
| 676 | 685 | let t = &v.ctx.table; |
|
| 677 | - | let o = try extent(v, p, 0, 0); |
|
| 678 | - | let case il::SlotKind::Slice(slot) = layout::slotAt(t, p.layout, o) else { |
|
| 679 | - | throw fail(v, Rule::Slot); |
|
| 680 | - | }; |
|
| 686 | + | let slot = try sliceAt(v, p); |
|
| 681 | 687 | let same = slot.layout == e or (layout::isPlain(t, slot.layout) |
|
| 682 | 688 | and layout::isPlain(t, e) |
|
| 683 | 689 | and layout::size(t, slot.layout) == layout::size(t, e)); |
|
| 684 | 690 | if not same { |
|
| 685 | 691 | throw fail(v, Rule::View); |
| 694 | 700 | throw fail(v, Rule::View); |
|
| 695 | 701 | } |
|
| 696 | 702 | return slot; |
|
| 697 | 703 | } |
|
| 698 | 704 | ||
| 705 | + | /// Check that slice elements of layout `e` are plain and have a size. A slice of such |
|
| 706 | + | /// elements can be resized and copied without forming or duplicating a slot. |
|
| 707 | + | fn plainElements(v: &Verifier, e: u32) throws (Error) { |
|
| 708 | + | let t = &v.ctx.table; |
|
| 709 | + | if not layout::isPlain(t, e) or layout::size(t, e) == 0 { |
|
| 710 | + | throw fail(v, Rule::View); |
|
| 711 | + | } |
|
| 712 | + | } |
|
| 713 | + | ||
| 699 | 714 | /// A slice slot that the slice instructions write. |
|
| 700 | 715 | record SliceDest: Copy { |
|
| 701 | 716 | /// Type of the slot. |
|
| 702 | 717 | slot: il::PtrType, |
|
| 703 | 718 | /// Whether the slot is in an object of the current frame. |
| 1104 | 1119 | try operand(v, start); |
|
| 1105 | 1120 | try operand(v, end); |
|
| 1106 | 1121 | let from = try sliceSource(v, try accessible(v, src), e); |
|
| 1107 | 1122 | try sliceFrom(v, d, from.own, from.mutable); |
|
| 1108 | 1123 | } |
|
| 1124 | + | case il::Instr::SliceCast { dst, src, layout: e, count } => { |
|
| 1125 | + | // The new slice has the plain element layout of the destination slot, so no |
|
| 1126 | + | // slot can be formed from bytes. |
|
| 1127 | + | let d = try sliceDest(v, dst, e); |
|
| 1128 | + | try operand(v, count); |
|
| 1129 | + | try plainElements(v, e); |
|
| 1130 | + | let from = try sliceAt(v, try accessible(v, src)); |
|
| 1131 | + | let t = &v.ctx.table; |
|
| 1132 | + | if not layout::isPlain(t, from.layout) or layout::size(t, from.layout) <> 1 { |
|
| 1133 | + | throw fail(v, Rule::View); |
|
| 1134 | + | } |
|
| 1135 | + | try sliceFrom(v, d, from.own, from.mutable); |
|
| 1136 | + | } |
|
| 1137 | + | case il::Instr::SliceLen { dst, layout: e, length } => { |
|
| 1138 | + | try sliceDest(v, dst, e); |
|
| 1139 | + | try operand(v, length); |
|
| 1140 | + | try plainElements(v, e); |
|
| 1141 | + | } |
|
| 1142 | + | case il::Instr::SliceCopy { dst, src, layout: e } => { |
|
| 1143 | + | // Owned slots would be duplicated by a copy. |
|
| 1144 | + | try plainElements(v, e); |
|
| 1145 | + | let to = try sliceSource(v, try accessible(v, dst), e); |
|
| 1146 | + | try sliceSource(v, try accessible(v, src), e); |
|
| 1147 | + | if not to.mutable { |
|
| 1148 | + | throw fail(v, Rule::Mutability); |
|
| 1149 | + | } |
|
| 1150 | + | } |
|
| 1109 | 1151 | case il::Instr::LoadPtr { dst, src, offset, nullable } => { |
|
| 1110 | 1152 | let p = try accessible(v, src); |
|
| 1111 | 1153 | let o = try extent(v, p, offset as i64, 0); |
|
| 1112 | 1154 | let slot = layout::slotAt(&v.ctx.table, p.layout, o) else { |
|
| 1113 | 1155 | throw fail(v, Rule::Slot); |
lib/std/lang/il/verify/tests.rad
+112 -1
| 45 | 45 | constant FNCELL1: u32 = 13; |
|
| 46 | 46 | /// Plain layout of three bytes. |
|
| 47 | 47 | constant P3: u32 = 14; |
|
| 48 | 48 | /// Plain layout of eight bytes with the alignment of a byte. |
|
| 49 | 49 | constant B8: u32 = 15; |
|
| 50 | + | /// Sixteen bytes: a slice of owned writable `P4` at offset 0. |
|
| 51 | + | constant SLICE4: u32 = 16; |
|
| 52 | + | /// Sixteen bytes: a slice of owned read-only `P4` at offset 0. |
|
| 53 | + | constant SLICE4C: u32 = 17; |
|
| 54 | + | /// Sixteen bytes: a slice of owned writable `CELL` at offset 0. |
|
| 55 | + | constant SLICECELL: u32 = 18; |
|
| 56 | + | /// Plain layout without bytes. |
|
| 57 | + | constant P0: u32 = 19; |
|
| 50 | 58 | ||
| 51 | 59 | /// Signature of a function from data to data. |
|
| 52 | 60 | constant SIG_DATA: u32 = 0; |
|
| 53 | 61 | /// Signature of a function without parameters or result. |
|
| 54 | 62 | constant SIG_VOID: u32 = 1; |
| 204 | 212 | ]); |
|
| 205 | 213 | addLayout(&mut b, 16, 8, &[sliceSlot(0, P1, false, true)]); |
|
| 206 | 214 | addLayout(&mut b, 8, 8, &[il::Slot { offset: 0, kind: il::SlotKind::Fn(SIG_VOID) }]); |
|
| 207 | 215 | addLayout(&mut b, 3, 1, &[]); |
|
| 208 | 216 | addLayout(&mut b, 8, 1, &[]); |
|
| 217 | + | addLayout(&mut b, 16, 8, &[sliceSlot(0, P4, true, true)]); |
|
| 218 | + | addLayout(&mut b, 16, 8, &[sliceSlot(0, P4, true, false)]); |
|
| 219 | + | addLayout(&mut b, 16, 8, &[sliceSlot(0, CELL, true, true)]); |
|
| 220 | + | addLayout(&mut b, 0, 1, &[]); |
|
| 209 | 221 | addSignature(&mut b, &[dataType()], dataType()); |
|
| 210 | 222 | addSignature(&mut b, &[], il::ValType::Void); |
|
| 211 | 223 | return b; |
|
| 212 | 224 | } |
|
| 213 | 225 |
| 399 | 411 | return il::Instr::SliceSub { |
|
| 400 | 412 | dst: il::Reg { n: dst }, src: il::Reg { n: src }, layout, start: imm(0), end: imm(0) |
|
| 401 | 413 | }; |
|
| 402 | 414 | } |
|
| 403 | 415 | ||
| 416 | + | fn sliceCast(dst: u32, src: u32, layout: u32, count: il::Val) -> il::Instr { |
|
| 417 | + | return il::Instr::SliceCast { |
|
| 418 | + | dst: il::Reg { n: dst }, src: il::Reg { n: src }, layout, count |
|
| 419 | + | }; |
|
| 420 | + | } |
|
| 421 | + | ||
| 422 | + | fn sliceLen(dst: u32, layout: u32, length: il::Val) -> il::Instr { |
|
| 423 | + | return il::Instr::SliceLen { dst: il::Reg { n: dst }, layout, length }; |
|
| 424 | + | } |
|
| 425 | + | ||
| 426 | + | fn sliceCopy(dst: u32, src: u32, layout: u32) -> il::Instr { |
|
| 427 | + | return il::Instr::SliceCopy { dst: il::Reg { n: dst }, src: il::Reg { n: src }, layout }; |
|
| 428 | + | } |
|
| 429 | + | ||
| 404 | 430 | fn blit(dst: u32, src: u32, layout: u32) -> il::Instr { |
|
| 405 | 431 | return il::Instr::Blit { dst: il::Reg { n: dst }, src: il::Reg { n: src }, layout }; |
|
| 406 | 432 | } |
|
| 407 | 433 | ||
| 408 | 434 | fn copy(dst: u32, val: il::Val) -> il::Instr { |
| 1171 | 1197 | verify::Rule::Class |
|
| 1172 | 1198 | ); |
|
| 1173 | 1199 | try check(&[], &[reserve(0, P16), reserve(1, SLICE), sliceSub(1, 0, P1)], verify::Rule::Slot); |
|
| 1174 | 1200 | } |
|
| 1175 | 1201 | ||
| 1202 | + | /// Bytes become elements of a plain layout, with the rights of the byte slice. |
|
| 1203 | + | @test unsafe fn castBytes() throws (testing::TestError) { |
|
| 1204 | + | try check( |
|
| 1205 | + | &[oneData()], &[reserve(1, SLICE), reserve(2, SLICE4), sliceCast(2, 1, P4, reg(0))], nil |
|
| 1206 | + | ); |
|
| 1207 | + | try check( |
|
| 1208 | + | &[], &[reserve(0, SLICE), reserve(1, SLICE4), sliceCast(1, 0, P4, imm(2))], nil |
|
| 1209 | + | ); |
|
| 1210 | + | try check( |
|
| 1211 | + | &[], |
|
| 1212 | + | &[reserve(0, SLICEC), reserve(1, SLICE4C), sliceCast(1, 0, P4, imm(2))], |
|
| 1213 | + | nil |
|
| 1214 | + | ); |
|
| 1215 | + | try check( |
|
| 1216 | + | &[], |
|
| 1217 | + | &[reserve(0, SLICEC), reserve(1, SLICE4), sliceCast(1, 0, P4, imm(2))], |
|
| 1218 | + | verify::Rule::Mutability |
|
| 1219 | + | ); |
|
| 1220 | + | try check( |
|
| 1221 | + | &[], |
|
| 1222 | + | &[reserve(0, BORROW), reserve(1, SLICE4), sliceCast(1, 0, P4, imm(2))], |
|
| 1223 | + | verify::Rule::Class |
|
| 1224 | + | ); |
|
| 1225 | + | // The source holds bytes. |
|
| 1226 | + | try check( |
|
| 1227 | + | &[], |
|
| 1228 | + | &[reserve(0, SLICE4), reserve(1, SLICE4), sliceCast(1, 0, P4, imm(2))], |
|
| 1229 | + | verify::Rule::View |
|
| 1230 | + | ); |
|
| 1231 | + | // The destination slot has the element layout. |
|
| 1232 | + | try check( |
|
| 1233 | + | &[], |
|
| 1234 | + | &[reserve(0, SLICE), reserve(1, SLICE), sliceCast(1, 0, P4, imm(2))], |
|
| 1235 | + | verify::Rule::View |
|
| 1236 | + | ); |
|
| 1237 | + | // Elements are plain and have a size. |
|
| 1238 | + | try check( |
|
| 1239 | + | &[], |
|
| 1240 | + | &[reserve(0, SLICE), reserve(1, SLICECELL), sliceCast(1, 0, CELL, imm(1))], |
|
| 1241 | + | verify::Rule::View |
|
| 1242 | + | ); |
|
| 1243 | + | try check( |
|
| 1244 | + | &[], |
|
| 1245 | + | &[reserve(0, SLICE), reserve(1, SLICE), sliceCast(1, 0, P0, imm(1))], |
|
| 1246 | + | verify::Rule::View |
|
| 1247 | + | ); |
|
| 1248 | + | try check( |
|
| 1249 | + | &[], &[reserve(0, P16), reserve(1, SLICE4), sliceCast(1, 0, P4, imm(2))], verify::Rule::Slot |
|
| 1250 | + | ); |
|
| 1251 | + | try check( |
|
| 1252 | + | &[], &[reserve(0, SLICE), reserve(1, P16), sliceCast(1, 0, P4, imm(2))], verify::Rule::Slot |
|
| 1253 | + | ); |
|
| 1254 | + | } |
|
| 1255 | + | ||
| 1256 | + | /// The length of a slice is set through a slice slot of the element layout. |
|
| 1257 | + | @test unsafe fn setLength() throws (testing::TestError) { |
|
| 1258 | + | try check(&[oneData()], &[reserve(1, SLICE4), sliceLen(1, P4, reg(0))], nil); |
|
| 1259 | + | try check(&[], &[reserve(0, SLICE4C), sliceLen(0, P4, imm(1))], nil); |
|
| 1260 | + | try check(&[], &[reserve(0, SLICE4), sliceLen(0, P1, imm(1))], verify::Rule::View); |
|
| 1261 | + | try check(&[], &[reserve(0, P16), sliceLen(0, P4, imm(1))], verify::Rule::Slot); |
|
| 1262 | + | // Spare capacity of slot-bearing elements may hold stale pointers. |
|
| 1263 | + | try check(&[], &[reserve(0, SLICECELL), sliceLen(0, CELL, imm(1))], verify::Rule::View); |
|
| 1264 | + | } |
|
| 1265 | + | ||
| 1266 | + | /// Elements are copied between slices of one plain layout. |
|
| 1267 | + | @test unsafe fn copyElements() throws (testing::TestError) { |
|
| 1268 | + | try check(&[], &[reserve(0, SLICE4), reserve(1, SLICE4), sliceCopy(1, 0, P4)], nil); |
|
| 1269 | + | try check(&[], &[reserve(0, SLICE4C), reserve(1, SLICE4), sliceCopy(1, 0, P4)], nil); |
|
| 1270 | + | try check( |
|
| 1271 | + | &[], |
|
| 1272 | + | &[reserve(0, SLICE4), reserve(1, SLICE4C), sliceCopy(1, 0, P4)], |
|
| 1273 | + | verify::Rule::Mutability |
|
| 1274 | + | ); |
|
| 1275 | + | try check( |
|
| 1276 | + | &[], &[reserve(0, SLICE), reserve(1, SLICE4), sliceCopy(1, 0, P4)], verify::Rule::View |
|
| 1277 | + | ); |
|
| 1278 | + | // A copy of slots would duplicate the pointers that they own. |
|
| 1279 | + | try check( |
|
| 1280 | + | &[], |
|
| 1281 | + | &[reserve(0, SLICECELL), reserve(1, SLICECELL), sliceCopy(1, 0, CELL)], |
|
| 1282 | + | verify::Rule::View |
|
| 1283 | + | ); |
|
| 1284 | + | try check(&[], &[reserve(0, P16), reserve(1, SLICE4), sliceCopy(1, 0, P4)], verify::Rule::Slot); |
|
| 1285 | + | } |
|
| 1286 | + | ||
| 1176 | 1287 | //////////// |
|
| 1177 | 1288 | // Blits // |
|
| 1178 | 1289 | //////////// |
|
| 1179 | 1290 | ||
| 1180 | 1291 | /// The source holds the copied object. The destination holds it too, or is a plain region. |
| 2545 | 2656 | try checkIn(&mut b, &[], il::ValType::Void, &body[..], rule); |
|
| 2546 | 2657 | } |
|
| 2547 | 2658 | ||
| 2548 | 2659 | /// Layout of the trait objects that [`objectCase`] builds, which [`addObjects`] adds after |
|
| 2549 | 2660 | /// the tables. The first is the object, then the read-only and the owning object. |
|
| 2550 | - | constant OBJECT: u32 = 19; |
|
| 2661 | + | constant OBJECT: u32 = 23; |
|
| 2551 | 2662 | ||
| 2552 | 2663 | /// Check that a trait object built from a receiver and its method table can call the |
|
| 2553 | 2664 | /// methods that its rights allow, and that the methods get the receiver. |
|
| 2554 | 2665 | @test unsafe fn traitObjectCalls() throws (testing::TestError) { |
|
| 2555 | 2666 | let mut b = newBuilder(); |
lib/std/lang/lower.rad
+264 -54
| 9446 | 9446 | self, |
|
| 9447 | 9447 | try resultLayoutOf( |
|
| 9448 | 9448 | self, *info.returnType, info.throwList |
|
| 9449 | 9449 | ) |
|
| 9450 | 9450 | ); |
|
| 9451 | - | // The allocator has a result of its own, because its payload is a pointer and the |
|
| 9452 | - | // result of the append holds a slice. |
|
| 9453 | - | let allocResult = try emitAllocResult(self, info.throwList); |
|
| 9451 | + | // The allocator has a result of its own, because its payload is a pointer or a byte |
|
| 9452 | + | // slice, and the result of the append holds a slice. |
|
| 9453 | + | let allocType = try effectiveType(self, call.args[1]); |
|
| 9454 | + | let typed = isTypedAllocator(self, allocType); |
|
| 9455 | + | let payload = allocPayload(self, allocType); |
|
| 9456 | + | let allocResult = try emitAllocResult(self, payload, info.throwList); |
|
| 9454 | 9457 | ||
| 9455 | 9458 | // Get the address of the slice header. |
|
| 9456 | 9459 | let sliceVal = try lowerExpr(self, access.parent); |
|
| 9457 | 9460 | let sliceReg = try emitValToReg(self, sliceVal); |
|
| 9458 | 9461 | ||
| 9459 | 9462 | // Lower the value to append and the allocator. |
|
| 9460 | 9463 | let elemVal = try lowerCallArg(self, call.args[0], true, nil); |
|
| 9461 | 9464 | let allocVal = try lowerExpr(self, call.args[1]); |
|
| 9462 | 9465 | let allocReg = try emitValToReg(self, allocVal); |
|
| 9463 | - | let allocType = try effectiveType(self, call.args[1]); |
|
| 9464 | 9466 | ||
| 9465 | 9467 | let elemLayout = resolver::getTypeLayout(&self.resolver, *elemType); |
|
| 9466 | 9468 | let stride = elemLayout.size; |
|
| 9467 | 9469 | let alignment = elemLayout.alignment; |
|
| 9468 | 9470 |
| 9529 | 9531 | il::Val::Imm( |
|
| 9530 | 9532 | stride as i64 |
|
| 9531 | 9533 | ) |
|
| 9532 | 9534 | ); |
|
| 9533 | 9535 | try emitSliceAlloc(self, allocResult, allocReg, allocType, byteSize, alignment); |
|
| 9534 | - | try emitAllocForward(self, allocResult, result, info, doneBlock, "append.copy"); |
|
| 9535 | - | try emitSliceGrowCopy(self, allocResult, sliceReg, lenVal, elemType, newCapVal, "append"); |
|
| 9536 | + | try emitAllocForward(self, allocResult, result, info, payload, doneBlock, "append.copy"); |
|
| 9537 | + | try emitSliceGrow( |
|
| 9538 | + | self, allocType, allocResult, sliceReg, lenVal, elemType, newCapVal, "append" |
|
| 9539 | + | ); |
|
| 9536 | 9540 | } |
|
| 9537 | 9541 | try emitJmp(self, storeBlock); |
|
| 9538 | 9542 | try switchToAndSeal(self, storeBlock); |
|
| 9539 | 9543 | ||
| 9540 | 9544 | // -- @store block --------------------------------------------------------- |
|
| 9541 | 9545 | ||
| 9542 | - | // Store element at `ptr + len * stride`. |
|
| 9543 | - | if stride <> 0 { |
|
| 9544 | - | let ptrReg = try loadSlicePtr(self, sliceReg); |
|
| 9545 | - | let elemDst = try emitRawElem(self, stride, ptrReg, lenVal); |
|
| 9546 | - | try emitStore(self, elemDst, 0, *elemType, elemVal); |
|
| 9547 | - | } |
|
| 9548 | - | // Increment len. |
|
| 9546 | + | // Store element at index `len`. With a typed allocator the length grows first, because |
|
| 9547 | + | // the element is addressed by a checked index. |
|
| 9549 | 9548 | let newLen = try emitTypedBinOp(self, il::BinOp::Add, il::Type::W32, lenVal, il::Val::Imm(1)); |
|
| 9550 | - | try emitStoreW32At(self, newLen, sliceReg, SLICE_LEN_OFFSET); |
|
| 9551 | - | ||
| 9549 | + | if typed { |
|
| 9550 | + | try emitSetLen(self, typed, sliceReg, elemType, newLen); |
|
| 9551 | + | if stride <> 0 { |
|
| 9552 | + | let elemDst = try emitSliceElemAt(self, sliceReg, lenVal, *elemType); |
|
| 9553 | + | try emitStore(self, elemDst, 0, *elemType, elemVal); |
|
| 9554 | + | } |
|
| 9555 | + | } else { |
|
| 9556 | + | if stride <> 0 { |
|
| 9557 | + | let ptrReg = try loadSlicePtr(self, sliceReg); |
|
| 9558 | + | let elemDst = try emitRawElem(self, stride, ptrReg, lenVal); |
|
| 9559 | + | try emitStore(self, elemDst, 0, *elemType, elemVal); |
|
| 9560 | + | } |
|
| 9561 | + | try emitStoreW32At(self, newLen, sliceReg, SLICE_LEN_OFFSET); |
|
| 9562 | + | } |
|
| 9552 | 9563 | // Return the append result. |
|
| 9553 | 9564 | try emitStoreW64At(self, il::Val::Imm(0), result, 0); |
|
| 9554 | 9565 | try emitStore(self, result, RESULT_VAL_OFFSET, *info.returnType, il::Val::Reg(sliceReg)); |
|
| 9555 | 9566 | try emitJmp(self, doneBlock); |
|
| 9556 | 9567 | ||
| 9557 | 9568 | try switchToAndSeal(self, doneBlock); |
|
| 9558 | 9569 | return il::Val::Reg(result); |
|
| 9559 | 9570 | } |
|
| 9560 | 9571 | ||
| 9572 | + | /// Whether the allocator of type `allocType` returns an owned byte slice. Growth then |
|
| 9573 | + | /// retypes the storage with `slice.cast`. Otherwise the allocator returns a pointer, which |
|
| 9574 | + | /// growth reads as plain data. |
|
| 9575 | + | fn isTypedAllocator(self: &mut Lowerer, allocType: resolver::Type) -> bool { |
|
| 9576 | + | let case resolver::Type::TraitObject { traitInfo, .. } = allocType else return false; |
|
| 9577 | + | let method = self.resolver.traits[*traitInfo].methods[0]; |
|
| 9578 | + | if let case resolver::Type::Slice { .. } = *method.fnType.returnType { |
|
| 9579 | + | return true; |
|
| 9580 | + | } |
|
| 9581 | + | return false; |
|
| 9582 | + | } |
|
| 9583 | + | ||
| 9584 | + | /// Get the type of the value that the allocator of type `allocType` returns: the byte slice |
|
| 9585 | + | /// of a typed allocator, and otherwise a pointer-sized integer, which has the size of the |
|
| 9586 | + | /// pointer that the allocator returns. |
|
| 9587 | + | fn allocPayload(self: &mut Lowerer, allocType: resolver::Type) -> resolver::Type { |
|
| 9588 | + | if isTypedAllocator(self, allocType) { |
|
| 9589 | + | let case resolver::Type::TraitObject { traitInfo, .. } = allocType |
|
| 9590 | + | else panic "allocPayload"; |
|
| 9591 | + | return *self.resolver.traits[*traitInfo].methods[0].fnType.returnType; |
|
| 9592 | + | } |
|
| 9593 | + | return resolver::Type::U64; |
|
| 9594 | + | } |
|
| 9595 | + | ||
| 9561 | 9596 | /// Reserve the result of a slice call that runs a fallible allocator. |
|
| 9562 | 9597 | /// |
|
| 9563 | - | /// The allocator writes its pointer at RESULT_VAL_OFFSET. The call's own payload |
|
| 9564 | - | /// is `Void` for `reserve`, `resize`, and `extend`, which has no size, so the |
|
| 9565 | - | /// layout uses a pointer-sized payload instead. The result is large enough for |
|
| 9566 | - | /// the pointer and the allocator's error types. |
|
| 9598 | + | /// The allocator writes its storage at RESULT_VAL_OFFSET, as a value of type `payload`. |
|
| 9599 | + | /// The call's own payload is `Void` for `reserve`, `resize`, and `extend`, which has no |
|
| 9600 | + | /// size, so the layout uses the payload of the allocator instead. The result is large |
|
| 9601 | + | /// enough for that value and the allocator's error types. |
|
| 9567 | 9602 | fn emitAllocResult( |
|
| 9568 | 9603 | self: &mut Lowerer, |
|
| 9604 | + | payload: resolver::Type, |
|
| 9569 | 9605 | throwList: *[*resolver::Type] |
|
| 9570 | 9606 | ) -> il::Reg throws (LowerError) { |
|
| 9571 | - | return try emitReserveIndex(self, try resultLayoutOf(self, resolver::Type::U64, throwList)); |
|
| 9607 | + | return try emitReserveIndex(self, try resultLayoutOf(self, payload, throwList)); |
|
| 9608 | + | } |
|
| 9609 | + | ||
| 9610 | + | /// Reserve the result of a slice builtin that runs the allocator of type `allocType`, and the |
|
| 9611 | + | /// result of the allocator, which the first register of the pair holds. Both are one object |
|
| 9612 | + | /// when the allocator returns a pointer, because the pointer and the error then fit in the |
|
| 9613 | + | /// result of the builtin. A byte slice does not, so the builtin has its own result. |
|
| 9614 | + | fn emitAllocResults( |
|
| 9615 | + | self: &mut Lowerer, allocType: resolver::Type, info: *resolver::FnType |
|
| 9616 | + | ) -> AllocResults throws (LowerError) { |
|
| 9617 | + | let allocResult = try emitAllocResult(self, allocPayload(self, allocType), info.throwList); |
|
| 9618 | + | if not isTypedAllocator(self, allocType) { |
|
| 9619 | + | return AllocResults { allocResult, result: allocResult }; |
|
| 9620 | + | } |
|
| 9621 | + | let result = try emitReserveIndex( |
|
| 9622 | + | self, try resultLayoutOf(self, *info.returnType, info.throwList) |
|
| 9623 | + | ); |
|
| 9624 | + | return AllocResults { allocResult, result }; |
|
| 9625 | + | } |
|
| 9626 | + | ||
| 9627 | + | /// The results of a slice builtin and of its allocator. |
|
| 9628 | + | record AllocResults: Copy { |
|
| 9629 | + | /// Result of the allocator. |
|
| 9630 | + | allocResult: il::Reg, |
|
| 9631 | + | /// Result of the builtin. |
|
| 9632 | + | result: il::Reg, |
|
| 9572 | 9633 | } |
|
| 9573 | 9634 | ||
| 9574 | 9635 | /// Lower `slice.reserve(count, allocator)` and return its result aggregate. |
|
| 9575 | 9636 | /// |
|
| 9576 | 9637 | /// Spare capacity that holds `count` elements leaves the slice unchanged. |
| 9586 | 9647 | let case ast::NodeValue::FieldAccess(access) = call.callee.value |
|
| 9587 | 9648 | else throw LowerError::MissingMetadata; |
|
| 9588 | 9649 | let case resolver::Type::Fn(info) = try typeOf(self, call.callee) |
|
| 9589 | 9650 | else throw LowerError::ExpectedFunction; |
|
| 9590 | 9651 | ||
| 9591 | - | let result = try emitAllocResult(self, info.throwList); |
|
| 9652 | + | let allocType = try effectiveType(self, call.args[1]); |
|
| 9653 | + | let results = try emitAllocResults(self, allocType, info); |
|
| 9654 | + | let result = results.result; |
|
| 9592 | 9655 | ||
| 9593 | 9656 | // Lower the receiver, count, and allocator. |
|
| 9594 | 9657 | let sliceVal = try lowerExpr(self, access.parent); |
|
| 9595 | 9658 | let sliceReg = try emitValToReg(self, sliceVal); |
|
| 9596 | 9659 | let countVal = try lowerExpr(self, call.args[0]); |
|
| 9597 | 9660 | let allocVal = try lowerExpr(self, call.args[1]); |
|
| 9598 | 9661 | let allocReg = try emitValToReg(self, allocVal); |
|
| 9599 | - | let allocType = try effectiveType(self, call.args[1]); |
|
| 9600 | 9662 | ||
| 9601 | 9663 | let elemLayout = resolver::getTypeLayout(&self.resolver, *elemType); |
|
| 9602 | 9664 | let stride = elemLayout.size; |
|
| 9603 | 9665 | let alignment = elemLayout.alignment; |
|
| 9604 | 9666 |
| 9637 | 9699 | newLenVal, |
|
| 9638 | 9700 | il::Val::Imm( |
|
| 9639 | 9701 | stride as i64 |
|
| 9640 | 9702 | ) |
|
| 9641 | 9703 | ); |
|
| 9642 | - | try emitSliceAlloc(self, result, allocReg, allocType, byteSize, alignment); |
|
| 9704 | + | try emitSliceAlloc(self, results.allocResult, allocReg, allocType, byteSize, alignment); |
|
| 9643 | 9705 | ||
| 9644 | - | try emitAllocBranch(self, result, doneBlock, "reserve.copy"); |
|
| 9645 | - | try emitSliceGrowCopy(self, result, sliceReg, lenVal, elemType, newLenVal, "reserve"); |
|
| 9706 | + | try emitAllocCheck(self, results, allocType, info, doneBlock, "reserve.copy"); |
|
| 9707 | + | try emitSliceGrow( |
|
| 9708 | + | self, allocType, results.allocResult, sliceReg, lenVal, elemType, newLenVal, "reserve" |
|
| 9709 | + | ); |
|
| 9646 | 9710 | } |
|
| 9647 | 9711 | try emitJmp(self, successBlock); |
|
| 9648 | 9712 | ||
| 9649 | 9713 | // -- @success block ------------------------------------------------------- |
|
| 9650 | 9714 |
| 9726 | 9790 | fn emitAllocForward( |
|
| 9727 | 9791 | self: &mut Lowerer, |
|
| 9728 | 9792 | allocResult: il::Reg, |
|
| 9729 | 9793 | result: il::Reg, |
|
| 9730 | 9794 | info: *resolver::FnType, |
|
| 9795 | + | payload: resolver::Type, |
|
| 9731 | 9796 | doneBlock: BlockId, |
|
| 9732 | 9797 | label: *[u8] |
|
| 9733 | 9798 | ) throws (LowerError) { |
|
| 9734 | 9799 | let tag = try resultTagReg(self, allocResult); |
|
| 9735 | 9800 | let failBlock = try createBlock(self, label); |
| 9738 | 9803 | self, il::CmpOp::Ne, il::Type::W64, il::Val::Reg(tag), il::Val::Imm(0), failBlock, copyBlock |
|
| 9739 | 9804 | ); |
|
| 9740 | 9805 | ||
| 9741 | 9806 | try switchToAndSeal(self, failBlock); |
|
| 9742 | 9807 | try emitStoreW64At(self, il::Val::Reg(tag), result, TVAL_TAG_OFFSET); |
|
| 9743 | - | let src = try emitPtrOffset(self, allocResult, RESULT_VAL_OFFSET); |
|
| 9808 | + | let src = try emitPtrOffset( |
|
| 9809 | + | self, allocResult, try resultErrOffset(self, payload, info.throwList) |
|
| 9810 | + | ); |
|
| 9744 | 9811 | let dst = try emitPtrOffset( |
|
| 9745 | 9812 | self, |
|
| 9746 | 9813 | result, |
|
| 9747 | 9814 | try resultErrOffset( |
|
| 9748 | 9815 | self, *info.returnType, info.throwList |
| 9753 | 9820 | try emitJmp(self, doneBlock); |
|
| 9754 | 9821 | ||
| 9755 | 9822 | try switchToAndSeal(self, copyBlock); |
|
| 9756 | 9823 | } |
|
| 9757 | 9824 | ||
| 9825 | + | /// Branch to `doneBlock` when the allocation of `results` failed, then switch to a new |
|
| 9826 | + | /// block named `label`, which publishes the new storage. The error goes to the result of |
|
| 9827 | + | /// the builtin `info`. The allocator has the type `allocType`. |
|
| 9828 | + | fn emitAllocCheck( |
|
| 9829 | + | self: &mut Lowerer, |
|
| 9830 | + | results: AllocResults, |
|
| 9831 | + | allocType: resolver::Type, |
|
| 9832 | + | info: *resolver::FnType, |
|
| 9833 | + | doneBlock: BlockId, |
|
| 9834 | + | label: *[u8] |
|
| 9835 | + | ) throws (LowerError) { |
|
| 9836 | + | if isTypedAllocator(self, allocType) { |
|
| 9837 | + | let payload = allocPayload(self, allocType); |
|
| 9838 | + | try emitAllocForward( |
|
| 9839 | + | self, results.allocResult, results.result, info, payload, doneBlock, label |
|
| 9840 | + | ); |
|
| 9841 | + | } else { |
|
| 9842 | + | try emitAllocBranch(self, results.result, doneBlock, label); |
|
| 9843 | + | } |
|
| 9844 | + | } |
|
| 9845 | + | ||
| 9846 | + | /// Publish the storage that the allocator returned in `allocResult` as the buffer of the |
|
| 9847 | + | /// slice at `sliceReg`, which has `lenVal` elements, with room for `newCap` elements. |
|
| 9848 | + | /// |
|
| 9849 | + | /// A byte slice is retyped to elements, which the old elements are copied into, and the |
|
| 9850 | + | /// result replaces the slice. A pointer is read as plain data. The caller sets the length. |
|
| 9851 | + | fn emitSliceGrow( |
|
| 9852 | + | self: &mut Lowerer, |
|
| 9853 | + | allocType: resolver::Type, |
|
| 9854 | + | allocResult: il::Reg, |
|
| 9855 | + | sliceReg: il::Reg, |
|
| 9856 | + | lenVal: il::Val, |
|
| 9857 | + | elemType: *resolver::Type, |
|
| 9858 | + | newCap: il::Val, |
|
| 9859 | + | label: *[u8] |
|
| 9860 | + | ) throws (LowerError) { |
|
| 9861 | + | if not isTypedAllocator(self, allocType) { |
|
| 9862 | + | try emitSliceGrowCopy(self, allocResult, sliceReg, lenVal, elemType, newCap, label); |
|
| 9863 | + | return; |
|
| 9864 | + | } |
|
| 9865 | + | let layout = try layoutOf(self, *elemType); |
|
| 9866 | + | let sliceType = resolver::Type::Slice { |
|
| 9867 | + | class: types::PointerClass::Owned, item: elemType, mutable: true |
|
| 9868 | + | }; |
|
| 9869 | + | let bytes = try emitPtrOffset(self, allocResult, RESULT_VAL_OFFSET); |
|
| 9870 | + | let grown = try emitReserve(self, sliceType); |
|
| 9871 | + | ||
| 9872 | + | try emit(self, il::Instr::SliceCast { dst: grown, src: bytes, layout, count: newCap }); |
|
| 9873 | + | try emit(self, il::Instr::SliceCopy { dst: grown, src: sliceReg, layout }); |
|
| 9874 | + | try emit(self, il::Instr::SliceLen { dst: grown, layout, length: lenVal }); |
|
| 9875 | + | try emitStore(self, sliceReg, 0, sliceType, il::Val::Reg(grown)); |
|
| 9876 | + | } |
|
| 9877 | + | ||
| 9878 | + | /// Set the length of the slice at `sliceReg`. A slice that a typed allocator grows changes |
|
| 9879 | + | /// its length with `slice.len`, which checks the capacity, since its elements are |
|
| 9880 | + | /// addressed by checked indexes. |
|
| 9881 | + | fn emitSetLen( |
|
| 9882 | + | self: &mut Lowerer, |
|
| 9883 | + | typed: bool, |
|
| 9884 | + | sliceReg: il::Reg, |
|
| 9885 | + | elemType: *resolver::Type, |
|
| 9886 | + | length: il::Val |
|
| 9887 | + | ) throws (LowerError) { |
|
| 9888 | + | if typed { |
|
| 9889 | + | let layout = try layoutOf(self, *elemType); |
|
| 9890 | + | try emit(self, il::Instr::SliceLen { dst: sliceReg, layout, length }); |
|
| 9891 | + | } else { |
|
| 9892 | + | try emitStoreW32At(self, length, sliceReg, SLICE_LEN_OFFSET); |
|
| 9893 | + | } |
|
| 9894 | + | } |
|
| 9895 | + | ||
| 9896 | + | /// Emit a typed fill loop over a slice: `for i in start..count { slice[i] = value; }`. The |
|
| 9897 | + | /// length of the slice covers `count`. |
|
| 9898 | + | fn emitSliceFillLoop( |
|
| 9899 | + | self: &mut Lowerer, |
|
| 9900 | + | label: *[u8], |
|
| 9901 | + | sliceReg: il::Reg, |
|
| 9902 | + | start: il::Val, |
|
| 9903 | + | value: il::Val, |
|
| 9904 | + | count: il::Val, |
|
| 9905 | + | elemType: resolver::Type |
|
| 9906 | + | ) throws (LowerError) { |
|
| 9907 | + | let iReg = nextReg(self); |
|
| 9908 | + | let header = try createBlockWithParam(self, label, il::Param { |
|
| 9909 | + | value: iReg, type: il::ValType::Data(il::Type::W32) |
|
| 9910 | + | }); |
|
| 9911 | + | let body = try createBlock(self, label); |
|
| 9912 | + | let done = try createBlock(self, label); |
|
| 9913 | + | ||
| 9914 | + | try emitJmpWithArg(self, header, start); |
|
| 9915 | + | switchToBlock(self, header); |
|
| 9916 | + | try emitBrCmp(self, il::CmpOp::Ult, il::Type::W32, il::Val::Reg(iReg), count, body, done); |
|
| 9917 | + | ||
| 9918 | + | try switchToAndSeal(self, body); |
|
| 9919 | + | let dstElem = try emitSliceElemAt(self, sliceReg, il::Val::Reg(iReg), elemType); |
|
| 9920 | + | try emitStore(self, dstElem, 0, elemType, value); |
|
| 9921 | + | ||
| 9922 | + | let nextI = try emitTypedBinOp( |
|
| 9923 | + | self, il::BinOp::Add, il::Type::W32, il::Val::Reg(iReg), il::Val::Imm(1) |
|
| 9924 | + | ); |
|
| 9925 | + | try emitJmpWithArg(self, header, nextI); |
|
| 9926 | + | try sealBlock(self, header); |
|
| 9927 | + | try switchToAndSeal(self, done); |
|
| 9928 | + | } |
|
| 9929 | + | ||
| 9758 | 9930 | /// Copy the slice's current elements into the buffer that `result` points to, |
|
| 9759 | 9931 | /// then publish that buffer and `newCap` as the slice pointer and capacity. |
|
| 9760 | 9932 | /// |
|
| 9761 | 9933 | /// The caller passes the old length `lenVal`. The caller stores the new length |
|
| 9762 | 9934 | /// after the fill, so this function leaves the length unchanged. Terminates the |
| 9826 | 9998 | let case ast::NodeValue::FieldAccess(access) = call.callee.value |
|
| 9827 | 9999 | else throw LowerError::MissingMetadata; |
|
| 9828 | 10000 | let case resolver::Type::Fn(info) = try typeOf(self, call.callee) |
|
| 9829 | 10001 | else throw LowerError::ExpectedFunction; |
|
| 9830 | 10002 | ||
| 9831 | - | let result = try emitAllocResult(self, info.throwList); |
|
| 10003 | + | let allocType = try effectiveType(self, call.args[2]); |
|
| 10004 | + | let typed = isTypedAllocator(self, allocType); |
|
| 10005 | + | let results = try emitAllocResults(self, allocType, info); |
|
| 10006 | + | let result = results.result; |
|
| 9832 | 10007 | ||
| 9833 | 10008 | // Lower the receiver, length, value, and allocator. |
|
| 9834 | 10009 | let sliceVal = try lowerExpr(self, access.parent); |
|
| 9835 | 10010 | let sliceReg = try emitValToReg(self, sliceVal); |
|
| 9836 | 10011 | let newLenVal = try lowerExpr(self, call.args[0]); |
|
| 9837 | 10012 | let valueVal = try lowerCallArg(self, call.args[1], true, nil); |
|
| 9838 | 10013 | let allocVal = try lowerExpr(self, call.args[2]); |
|
| 9839 | 10014 | let allocReg = try emitValToReg(self, allocVal); |
|
| 9840 | - | let allocType = try effectiveType(self, call.args[2]); |
|
| 9841 | 10015 | ||
| 9842 | 10016 | let elemLayout = resolver::getTypeLayout(&self.resolver, *elemType); |
|
| 9843 | 10017 | let stride = elemLayout.size; |
|
| 9844 | 10018 | let alignment = elemLayout.alignment; |
|
| 9845 | 10019 |
| 9854 | 10028 | let successBlock = try createBlock(self, "resize.success"); |
|
| 9855 | 10029 | let doneBlock = try createBlock(self, "resize.done"); |
|
| 9856 | 10030 | try emitBrCmp(self, il::CmpOp::Ult, il::Type::W32, newLenVal, lenVal, shrinkBlock, checkBlock); |
|
| 9857 | 10031 | ||
| 9858 | 10032 | try switchToAndSeal(self, shrinkBlock); |
|
| 9859 | - | try emitStoreW32At(self, newLenVal, sliceReg, SLICE_LEN_OFFSET); |
|
| 10033 | + | try emitSetLen(self, typed, sliceReg, elemType, newLenVal); |
|
| 9860 | 10034 | try emitJmp(self, successBlock); |
|
| 9861 | 10035 | ||
| 9862 | 10036 | try switchToAndSeal(self, checkBlock); |
|
| 9863 | 10037 | try emitBrCmp(self, il::CmpOp::Ult, il::Type::W32, capVal, newLenVal, growBlock, fillBlock); |
|
| 9864 | 10038 |
| 9878 | 10052 | try emitStoreW32At(self, newLenVal, sliceReg, SLICE_CAP_OFFSET); |
|
| 9879 | 10053 | } else { |
|
| 9880 | 10054 | let byteSize = try emitTypedBinOp( |
|
| 9881 | 10055 | self, il::BinOp::Mul, il::Type::W32, newLenVal, il::Val::Imm(stride as i64) |
|
| 9882 | 10056 | ); |
|
| 9883 | - | try emitSliceAlloc(self, result, allocReg, allocType, byteSize, alignment); |
|
| 10057 | + | try emitSliceAlloc(self, results.allocResult, allocReg, allocType, byteSize, alignment); |
|
| 9884 | 10058 | ||
| 9885 | - | try emitAllocBranch(self, result, doneBlock, "resize.copy"); |
|
| 9886 | - | try emitSliceGrowCopy( |
|
| 10059 | + | try emitAllocCheck(self, results, allocType, info, doneBlock, "resize.copy"); |
|
| 10060 | + | try emitSliceGrow( |
|
| 9887 | 10061 | self, |
|
| 9888 | - | result, |
|
| 10062 | + | allocType, |
|
| 10063 | + | results.allocResult, |
|
| 9889 | 10064 | sliceReg, |
|
| 9890 | 10065 | try loadSliceLen(self, sliceReg), |
|
| 9891 | 10066 | elemType, |
|
| 9892 | 10067 | newLenVal, |
|
| 9893 | 10068 | "resize" |
| 9898 | 10073 | // -- @fill block ---------------------------------------------------------- |
|
| 9899 | 10074 | ||
| 9900 | 10075 | // Store `value` into each slot in [len, newLen). The pointer is reloaded |
|
| 9901 | 10076 | // because growth may have replaced it. |
|
| 9902 | 10077 | try switchToAndSeal(self, fillBlock); |
|
| 9903 | - | if stride <> 0 { |
|
| 9904 | - | let fillPtrReg = try loadSlicePtr(self, sliceReg); |
|
| 9905 | - | try emitRawFillLoop( |
|
| 9906 | - | self, "resize.fill", fillPtrReg, lenVal, valueVal, newLenVal, *elemType |
|
| 9907 | - | ); |
|
| 10078 | + | if typed { |
|
| 10079 | + | // The length covers the new slots first, since they are addressed by checked indexes. |
|
| 10080 | + | try emitSetLen(self, typed, sliceReg, elemType, newLenVal); |
|
| 10081 | + | if stride <> 0 { |
|
| 10082 | + | try emitSliceFillLoop( |
|
| 10083 | + | self, "resize.fill", sliceReg, lenVal, valueVal, newLenVal, *elemType |
|
| 10084 | + | ); |
|
| 10085 | + | } |
|
| 10086 | + | } else { |
|
| 10087 | + | if stride <> 0 { |
|
| 10088 | + | let fillPtrReg = try loadSlicePtr(self, sliceReg); |
|
| 10089 | + | try emitRawFillLoop( |
|
| 10090 | + | self, "resize.fill", fillPtrReg, lenVal, valueVal, newLenVal, *elemType |
|
| 10091 | + | ); |
|
| 10092 | + | } |
|
| 10093 | + | try emitStoreW32At(self, newLenVal, sliceReg, SLICE_LEN_OFFSET); |
|
| 9908 | 10094 | } |
|
| 9909 | - | try emitStoreW32At(self, newLenVal, sliceReg, SLICE_LEN_OFFSET); |
|
| 9910 | 10095 | try emitJmp(self, successBlock); |
|
| 9911 | 10096 | ||
| 9912 | 10097 | // -- @success block ------------------------------------------------------- |
|
| 9913 | 10098 | ||
| 9914 | 10099 | try switchToAndSeal(self, successBlock); |
| 9951 | 10136 | let case ast::NodeValue::FieldAccess(access) = call.callee.value |
|
| 9952 | 10137 | else throw LowerError::MissingMetadata; |
|
| 9953 | 10138 | let case resolver::Type::Fn(info) = try typeOf(self, call.callee) |
|
| 9954 | 10139 | else throw LowerError::ExpectedFunction; |
|
| 9955 | 10140 | ||
| 9956 | - | let result = try emitAllocResult(self, info.throwList); |
|
| 10141 | + | let allocType = try effectiveType(self, call.args[1]); |
|
| 10142 | + | let typed = isTypedAllocator(self, allocType); |
|
| 10143 | + | let results = try emitAllocResults(self, allocType, info); |
|
| 10144 | + | let result = results.result; |
|
| 9957 | 10145 | ||
| 9958 | 10146 | // Lower the receiver, source, and allocator. |
|
| 9959 | 10147 | let sliceVal = try lowerExpr(self, access.parent); |
|
| 9960 | 10148 | let sliceReg = try emitValToReg(self, sliceVal); |
|
| 9961 | 10149 | let srcVal = try lowerExpr(self, call.args[0]); |
|
| 9962 | 10150 | let srcReg = try emitValToReg(self, srcVal); |
|
| 9963 | 10151 | let allocVal = try lowerExpr(self, call.args[1]); |
|
| 9964 | 10152 | let allocReg = try emitValToReg(self, allocVal); |
|
| 9965 | - | let allocType = try effectiveType(self, call.args[1]); |
|
| 9966 | 10153 | ||
| 9967 | 10154 | let elemLayout = resolver::getTypeLayout(&self.resolver, *elemType); |
|
| 9968 | 10155 | let stride = elemLayout.size; |
|
| 9969 | 10156 | let alignment = elemLayout.alignment; |
|
| 9970 | 10157 | ||
| 9971 | 10158 | let lenVal = try loadSliceLen(self, sliceReg); |
|
| 9972 | 10159 | let capVal = try loadSliceCap(self, sliceReg); |
|
| 9973 | 10160 | let srcLenVal = try loadSliceLen(self, srcReg); |
|
| 9974 | 10161 | let srcPtrReg = try loadSlicePtr(self, srcReg); |
|
| 9975 | 10162 | ||
| 10163 | + | // A typed allocator copies from a snapshot of the source slice, which stays valid when |
|
| 10164 | + | // the source names the receiver and growth replaces its header. |
|
| 10165 | + | let srcType = try effectiveType(self, call.args[0]); |
|
| 10166 | + | let mut snapshot = srcReg; |
|
| 10167 | + | if typed { |
|
| 10168 | + | set snapshot = try emitReserve(self, srcType); |
|
| 10169 | + | try emitStore(self, snapshot, 0, srcType, il::Val::Reg(srcReg)); |
|
| 10170 | + | } |
|
| 9976 | 10171 | // The sum of two u32 lengths cannot wrap in 64 bits. A sum that fits in the capacity |
|
| 9977 | 10172 | // fits in u32 bytes, because the capacity does. |
|
| 9978 | 10173 | let limit = maxSliceLength(stride); |
|
| 9979 | 10174 | let newLenVal = try emitTypedBinOp(self, il::BinOp::Add, il::Type::W64, lenVal, srcLenVal); |
|
| 9980 | 10175 |
| 10035 | 10230 | il::BinOp::Mul, |
|
| 10036 | 10231 | il::Type::W32, |
|
| 10037 | 10232 | il::Val::Reg(newCapReg), |
|
| 10038 | 10233 | il::Val::Imm(stride as i64) |
|
| 10039 | 10234 | ); |
|
| 10040 | - | try emitSliceAlloc(self, result, allocReg, allocType, byteSize, alignment); |
|
| 10235 | + | try emitSliceAlloc(self, results.allocResult, allocReg, allocType, byteSize, alignment); |
|
| 10041 | 10236 | ||
| 10042 | - | try emitAllocBranch(self, result, doneBlock, "extend.copy"); |
|
| 10043 | - | try emitSliceGrowCopy( |
|
| 10237 | + | try emitAllocCheck(self, results, allocType, info, doneBlock, "extend.copy"); |
|
| 10238 | + | try emitSliceGrow( |
|
| 10044 | 10239 | self, |
|
| 10045 | - | result, |
|
| 10240 | + | allocType, |
|
| 10241 | + | results.allocResult, |
|
| 10046 | 10242 | sliceReg, |
|
| 10047 | 10243 | try loadSliceLen(self, sliceReg), |
|
| 10048 | 10244 | elemType, |
|
| 10049 | 10245 | il::Val::Reg(newCapReg), |
|
| 10050 | 10246 | "extend" |
| 10054 | 10250 | } |
|
| 10055 | 10251 | // -- @place block --------------------------------------------------------- |
|
| 10056 | 10252 | ||
| 10057 | 10253 | // Copy the source elements after the old ones, then publish the new length. |
|
| 10058 | 10254 | try switchToAndSeal(self, placeBlock); |
|
| 10059 | - | if stride <> 0 { |
|
| 10060 | - | let ptrReg = try loadSlicePtr(self, sliceReg); |
|
| 10061 | - | let dstReg = try emitRawElem(self, stride, ptrReg, lenVal); |
|
| 10062 | - | let srcBytes = try emitByteCount(self, srcLenVal, stride); |
|
| 10063 | - | try emitCopyBytes( |
|
| 10064 | - | self, dstReg, srcPtrReg, srcBytes, copyWordSize(stride, alignment), "extend" |
|
| 10065 | - | ); |
|
| 10255 | + | if typed { |
|
| 10256 | + | // The length covers the new slots first, and the copy goes to the slice of them. |
|
| 10257 | + | try emitSetLen(self, typed, sliceReg, elemType, newLenVal); |
|
| 10258 | + | if stride <> 0 { |
|
| 10259 | + | let layout = try layoutOf(self, *elemType); |
|
| 10260 | + | let part = try emitReserve(self, resolver::Type::Slice { |
|
| 10261 | + | class: types::PointerClass::Owned, item: elemType, mutable: true |
|
| 10262 | + | }); |
|
| 10263 | + | try emit(self, il::Instr::SliceSub { |
|
| 10264 | + | dst: part, src: sliceReg, layout, start: lenVal, end: newLenVal |
|
| 10265 | + | }); |
|
| 10266 | + | try emit(self, il::Instr::SliceCopy { dst: part, src: snapshot, layout }); |
|
| 10267 | + | } |
|
| 10268 | + | } else { |
|
| 10269 | + | if stride <> 0 { |
|
| 10270 | + | let ptrReg = try loadSlicePtr(self, sliceReg); |
|
| 10271 | + | let dstReg = try emitRawElem(self, stride, ptrReg, lenVal); |
|
| 10272 | + | let srcBytes = try emitByteCount(self, srcLenVal, stride); |
|
| 10273 | + | try emitCopyBytes( |
|
| 10274 | + | self, dstReg, srcPtrReg, srcBytes, copyWordSize(stride, alignment), "extend" |
|
| 10275 | + | ); |
|
| 10276 | + | } |
|
| 10277 | + | try emitStoreW32At(self, newLenVal, sliceReg, SLICE_LEN_OFFSET); |
|
| 10066 | 10278 | } |
|
| 10067 | - | try emitStoreW32At(self, newLenVal, sliceReg, SLICE_LEN_OFFSET); |
|
| 10068 | - | ||
| 10069 | 10279 | try emitStoreW64At(self, il::Val::Imm(0), result, 0); |
|
| 10070 | 10280 | try emitJmp(self, doneBlock); |
|
| 10071 | 10281 | ||
| 10072 | 10282 | try switchToAndSeal(self, doneBlock); |
|
| 10073 | 10283 | return il::Val::Reg(result); |
lib/std/lang/resolver.rad
+8 -4
| 6802 | 6802 | } |
|
| 6803 | 6803 | ||
| 6804 | 6804 | /// Check that a trait has the shape of an allocator. |
|
| 6805 | 6805 | /// |
|
| 6806 | 6806 | /// The trait has one safe method with a mutable reference receiver, taking a |
|
| 6807 | - | /// byte size and an alignment, returning an owned mutable opaque pointer, and |
|
| 6808 | - | /// throwing at least one error. The v-table then has a single slot, whose |
|
| 6809 | - | /// function takes the result address, the data pointer, the size and the |
|
| 6810 | - | /// alignment. |
|
| 6807 | + | /// byte size and an alignment, returning an owned mutable opaque pointer or an owned |
|
| 6808 | + | /// mutable byte slice, and throwing at least one error. The v-table then has a single |
|
| 6809 | + | /// slot, whose function takes the result address, the data pointer, the size and the |
|
| 6810 | + | /// alignment. A byte slice result lets verified code retype the storage; see |
|
| 6811 | + | /// `slice.cast`. |
|
| 6811 | 6812 | fn isAllocatorTrait(self: &Resolver, traitInfo: TraitId) -> bool { |
|
| 6812 | 6813 | let info = &self.traits[*traitInfo]; |
|
| 6813 | 6814 | if info.methods.len <> 1 or info.supertraits.len <> 0 { |
|
| 6814 | 6815 | return false; |
|
| 6815 | 6816 | } |
| 6824 | 6825 | return false; |
|
| 6825 | 6826 | } |
|
| 6826 | 6827 | if *callback.paramTypes[0] <> Type::U32 or *callback.paramTypes[1] <> Type::U32 { |
|
| 6827 | 6828 | return false; |
|
| 6828 | 6829 | } |
|
| 6830 | + | if let case Type::Slice { class, item, mutable } = *callback.returnType { |
|
| 6831 | + | return class == types::PointerClass::Owned and mutable and *item == Type::U8; |
|
| 6832 | + | } |
|
| 6829 | 6833 | let case Type::Pointer { class, target, mutable } = *callback.returnType else return false; |
|
| 6830 | 6834 | return class == types::PointerClass::Owned and mutable and *target == Type::Opaque; |
|
| 6831 | 6835 | } |
|
| 6832 | 6836 | ||
| 6833 | 6837 | /// Return the allocator trait that a slice allocator argument of type `ty` stands for. |
lib/std/lang/resolver/tests/structure.rad
+37 -0
| 2503 | 2503 | let program = "fn f(s: *[u8]) -> u32 { return s.cap; }"; |
|
| 2504 | 2504 | let result = try resolveProgramStr(&mut a, program); |
|
| 2505 | 2505 | try expectNoErrors(&result); |
|
| 2506 | 2506 | } |
|
| 2507 | 2507 | ||
| 2508 | + | /// A slice growth builtin takes an allocator that returns an owned mutable byte slice, as it |
|
| 2509 | + | /// does one that returns an owned mutable opaque pointer, and no other result. |
|
| 2510 | + | @test unsafe fn testResolveSliceAllocatorResults() throws (testing::TestError) { |
|
| 2511 | + | let results: [*[u8]; 2] = ["*mut [u8]", "*mut opaque"]; |
|
| 2512 | + | for result in results { |
|
| 2513 | + | let mut a = testResolver(); |
|
| 2514 | + | let mut program: *mut [u8] = &mut []; |
|
| 2515 | + | try! program.extend( |
|
| 2516 | + | "union E: Copy { Full } trait A { fn (&mut A) allocate(n: u32, m: u32) -> ", |
|
| 2517 | + | &mut a.arena |
|
| 2518 | + | ); |
|
| 2519 | + | try! program.extend(result, &mut a.arena); |
|
| 2520 | + | try! program.extend( |
|
| 2521 | + | " throws (E); } fn f(owned: *mut [i32], a: &mut opaque A) { let mut s = owned; try! s.append(1, a); }", |
|
| 2522 | + | &mut a.arena |
|
| 2523 | + | ); |
|
| 2524 | + | let resolved = try resolveProgramStr(&mut a, &program[..]); |
|
| 2525 | + | try expectNoErrors(&resolved); |
|
| 2526 | + | } |
|
| 2527 | + | let rejected: [*[u8]; 3] = ["*[u8]", "*mut [i32]", "*mut [u8; 4]"]; |
|
| 2528 | + | for result in rejected { |
|
| 2529 | + | let mut a = testResolver(); |
|
| 2530 | + | let mut program: *mut [u8] = &mut []; |
|
| 2531 | + | try! program.extend( |
|
| 2532 | + | "union E: Copy { Full } trait A { fn (&mut A) allocate(n: u32, m: u32) -> ", |
|
| 2533 | + | &mut a.arena |
|
| 2534 | + | ); |
|
| 2535 | + | try! program.extend(result, &mut a.arena); |
|
| 2536 | + | try! program.extend( |
|
| 2537 | + | " throws (E); } fn f(owned: *mut [i32], a: &mut opaque A) { let mut s = owned; try! s.append(1, a); }", |
|
| 2538 | + | &mut a.arena |
|
| 2539 | + | ); |
|
| 2540 | + | let resolved = try resolveProgramStr(&mut a, &program[..]); |
|
| 2541 | + | try expectError(&resolved); |
|
| 2542 | + | } |
|
| 2543 | + | } |
|
| 2544 | + | ||
| 2508 | 2545 | /// Test `.append()` on immutable slice produces an error. |
|
| 2509 | 2546 | @test unsafe fn testResolveSliceAppendImmutable() throws (testing::TestError) { |
|
| 2510 | 2547 | let mut a = testResolver(); |
|
| 2511 | 2548 | let program = |
|
| 2512 | 2549 | "union E: Copy { Exhausted } trait A { fn (&mut A) allocate(n: u32, m: u32) -> *mut opaque throws (E); } fn f(s: *[i32], a: &mut opaque A) { try! s.append(1, a); }"; |
test/tests/record.scalar.rad
+12 -3
| 22 | 22 | union AllocError: Copy { |
|
| 23 | 23 | OutOfMemory, |
|
| 24 | 24 | } |
|
| 25 | 25 | ||
| 26 | 26 | trait Allocator { |
|
| 27 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError); |
|
| 27 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError); |
|
| 28 | 28 | } |
|
| 29 | 29 | ||
| 30 | 30 | constant DEFAULT_ID: NodeId = NodeId(7); |
|
| 31 | 31 | static GLOBAL_ID: NodeId = NodeId(1); |
|
| 32 | 32 | static GLOBAL_WRAP: Wrap = Wrap { value: 1 }; |
| 116 | 116 | } |
|
| 117 | 117 | ||
| 118 | 118 | /// Allocator that always fails. Appends within capacity never call it. |
|
| 119 | 119 | record NoAlloc {} |
|
| 120 | 120 | ||
| 121 | + | /// Allocate raw storage for `NoAlloc`. |
|
| 122 | + | fn rawAllocate0(n: &mut NoAlloc, size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 123 | + | throw AllocError::OutOfMemory; |
|
| 124 | + | } |
|
| 125 | + | ||
| 121 | 126 | instance Allocator for NoAlloc { |
|
| 122 | - | fn (n: &mut NoAlloc) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 123 | - | throw AllocError::OutOfMemory; |
|
| 127 | + | fn (n: &mut NoAlloc) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError) { |
|
| 128 | + | let ptr = try rawAllocate0(n, size, alignment); |
|
| 129 | + | // The storage was just allocated, so no other slice names it. |
|
| 130 | + | unsafe { |
|
| 131 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 132 | + | } |
|
| 124 | 133 | } |
|
| 125 | 134 | } |
|
| 126 | 135 | ||
| 127 | 136 | /// Method with a mutable pointer receiver on a scalar record. |
|
| 128 | 137 | fn (w: *mut Wrap) bumpBy(n: u32) { |
test/tests/slice.alloc.short.rad
added
+40 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! Growth traps when the allocator returns fewer bytes than the new capacity needs. |
|
| 3 | + | ||
| 4 | + | /// Allocation failures. |
|
| 5 | + | union AllocError: Copy { |
|
| 6 | + | /// The allocator cannot provide storage. |
|
| 7 | + | OutOfMemory, |
|
| 8 | + | } |
|
| 9 | + | ||
| 10 | + | /// Allocator that returns a single byte whatever the size. |
|
| 11 | + | record Short { |
|
| 12 | + | /// Backing storage. |
|
| 13 | + | data: *mut [u8], |
|
| 14 | + | } |
|
| 15 | + | ||
| 16 | + | /// Allocation interface of the slice growth builtins. |
|
| 17 | + | trait Allocator { |
|
| 18 | + | /// Allocate `size` bytes aligned to `alignment`. |
|
| 19 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError); |
|
| 20 | + | } |
|
| 21 | + | ||
| 22 | + | instance Allocator for Short { |
|
| 23 | + | /// Return one byte of storage. |
|
| 24 | + | fn (short: &mut Short) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError) { |
|
| 25 | + | // The byte is returned once, because the test grows one slice once. |
|
| 26 | + | unsafe { |
|
| 27 | + | return @sliceOf(&mut short.data[0], 1); |
|
| 28 | + | } |
|
| 29 | + | } |
|
| 30 | + | } |
|
| 31 | + | ||
| 32 | + | /// Backing storage of the allocator. |
|
| 33 | + | unsafe static BUF: [u8; 16] = undefined; |
|
| 34 | + | ||
| 35 | + | @default unsafe fn main() -> i32 { |
|
| 36 | + | let mut short = Short { data: &mut BUF[..] }; |
|
| 37 | + | let mut items: *mut [u32] = &mut []; |
|
| 38 | + | try! items.append(1, &mut short); |
|
| 39 | + | return 0; |
|
| 40 | + | } |
test/tests/slice.alloc.trait.rad
+19 -10
| 10 | 10 | } |
|
| 11 | 11 | ||
| 12 | 12 | /// Allocation interface. |
|
| 13 | 13 | trait Source { |
|
| 14 | 14 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 15 | - | fn (&mut Source) allocate(size: u32, alignment: u32) -> *mut opaque throws (Full); |
|
| 15 | + | fn (&mut Source) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Full); |
|
| 16 | 16 | } |
|
| 17 | 17 | ||
| 18 | 18 | /// Allocator state: bytes handed out and allocation calls made. |
|
| 19 | 19 | record Bump { |
|
| 20 | 20 | /// Bytes handed out so far. |
| 24 | 24 | } |
|
| 25 | 25 | ||
| 26 | 26 | /// Backing storage for the bump allocator. |
|
| 27 | 27 | static HEAP: [u8; 256] = [0; 256]; |
|
| 28 | 28 | ||
| 29 | + | /// Allocate raw storage for `Bump`. |
|
| 30 | + | fn rawAllocate0(b: &mut Bump, size: u32, alignment: u32) -> *mut opaque throws (Full) { |
|
| 31 | + | set b.calls += 1; |
|
| 32 | + | let start = b.used; |
|
| 33 | + | if size > 256 - start { |
|
| 34 | + | throw Full::Exhausted; |
|
| 35 | + | } |
|
| 36 | + | set b.used = start + size; |
|
| 37 | + | unsafe { |
|
| 38 | + | let base: *mut u8 = &mut HEAP[start]; |
|
| 39 | + | return base as *mut opaque; |
|
| 40 | + | } |
|
| 41 | + | } |
|
| 42 | + | ||
| 29 | 43 | instance Source for Bump { |
|
| 30 | 44 | /// Bump allocation from `HEAP`, failing when the request does not fit. |
|
| 31 | - | fn (b: &mut Bump) allocate(size: u32, alignment: u32) -> *mut opaque throws (Full) { |
|
| 32 | - | set b.calls += 1; |
|
| 33 | - | let start = b.used; |
|
| 34 | - | if size > 256 - start { |
|
| 35 | - | throw Full::Exhausted; |
|
| 36 | - | } |
|
| 37 | - | set b.used = start + size; |
|
| 45 | + | fn (b: &mut Bump) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Full) { |
|
| 46 | + | let ptr = try rawAllocate0(b, size, alignment); |
|
| 47 | + | // The storage was just allocated, so no other slice names it. |
|
| 38 | 48 | unsafe { |
|
| 39 | - | let base: *mut u8 = &mut HEAP[start]; |
|
| 40 | - | return base as *mut opaque; |
|
| 49 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 41 | 50 | } |
|
| 42 | 51 | } |
|
| 43 | 52 | } |
|
| 44 | 53 | ||
| 45 | 54 | /// Append through a trait object parameter. |
test/tests/slice.append.errors.rad
+2 -2
| 18 | 18 | } |
|
| 19 | 19 | ||
| 20 | 20 | /// Allocation interface with two independent error types. |
|
| 21 | 21 | trait Allocator { |
|
| 22 | 22 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 23 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws ( |
|
| 23 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws ( |
|
| 24 | 24 | Denied, Unavailable |
|
| 25 | 25 | ); |
|
| 26 | 26 | } |
|
| 27 | 27 | ||
| 28 | 28 | /// Allocator state: selects which error to report. |
| 33 | 33 | calls: u32, |
|
| 34 | 34 | } |
|
| 35 | 35 | ||
| 36 | 36 | instance Allocator for State { |
|
| 37 | 37 | /// Return an error with its payload. |
|
| 38 | - | fn (state: &mut State) allocate(size: u32, alignment: u32) -> *mut opaque throws ( |
|
| 38 | + | fn (state: &mut State) allocate(size: u32, alignment: u32) -> *mut [u8] throws ( |
|
| 39 | 39 | Denied, Unavailable |
|
| 40 | 40 | ) { |
|
| 41 | 41 | set state.calls += 1; |
|
| 42 | 42 | if state.unavailable { |
|
| 43 | 43 | throw Unavailable { first: 11, second: 22, third: 33 }; |
test/tests/slice.append.failure.rad
+12 -3
| 8 | 8 | } |
|
| 9 | 9 | ||
| 10 | 10 | /// Allocation interface of the slice growth builtins. |
|
| 11 | 11 | trait Allocator { |
|
| 12 | 12 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 13 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError); |
|
| 13 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError); |
|
| 14 | 14 | } |
|
| 15 | 15 | ||
| 16 | 16 | /// Allocator that rejects every request. |
|
| 17 | 17 | record Reject {} |
|
| 18 | 18 | ||
| 19 | + | /// Allocate raw storage for `Reject`. |
|
| 20 | + | fn rawAllocate0(r: &mut Reject, size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 21 | + | throw AllocError::OutOfMemory; |
|
| 22 | + | } |
|
| 23 | + | ||
| 19 | 24 | instance Allocator for Reject { |
|
| 20 | 25 | /// Reject the request. |
|
| 21 | - | fn (r: &mut Reject) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 22 | - | throw AllocError::OutOfMemory; |
|
| 26 | + | fn (r: &mut Reject) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError) { |
|
| 27 | + | let ptr = try rawAllocate0(r, size, alignment); |
|
| 28 | + | // The storage was just allocated, so no other slice names it. |
|
| 29 | + | unsafe { |
|
| 30 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 31 | + | } |
|
| 23 | 32 | } |
|
| 24 | 33 | } |
|
| 25 | 34 | ||
| 26 | 35 | @default unsafe fn main() -> i32 { |
|
| 27 | 36 | static storage: [i32; 2] = [11, 22]; |
test/tests/slice.append.limit.rad
+13 -4
| 8 | 8 | } |
|
| 9 | 9 | ||
| 10 | 10 | /// Slice allocation interface. |
|
| 11 | 11 | trait Allocator { |
|
| 12 | 12 | /// Allocate storage. |
|
| 13 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (Error); |
|
| 13 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Error); |
|
| 14 | 14 | } |
|
| 15 | 15 | ||
| 16 | 16 | /// Allocator that records the requested size. |
|
| 17 | 17 | record Reject: Copy { |
|
| 18 | 18 | /// Most recently requested size. |
|
| 19 | 19 | seen: u32, |
|
| 20 | 20 | } |
|
| 21 | 21 | ||
| 22 | + | /// Allocate raw storage for `Reject`. |
|
| 23 | + | fn rawAllocate0(r: &mut Reject, size: u32, alignment: u32) -> *mut opaque throws (Error) { |
|
| 24 | + | set r.seen = size; |
|
| 25 | + | throw Error::Full; |
|
| 26 | + | } |
|
| 27 | + | ||
| 22 | 28 | instance Allocator for Reject { |
|
| 23 | 29 | /// Record and reject the request. |
|
| 24 | - | fn (r: &mut Reject) allocate(size: u32, alignment: u32) -> *mut opaque throws (Error) { |
|
| 25 | - | set r.seen = size; |
|
| 26 | - | throw Error::Full; |
|
| 30 | + | fn (r: &mut Reject) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Error) { |
|
| 31 | + | let ptr = try rawAllocate0(r, size, alignment); |
|
| 32 | + | // The storage was just allocated, so no other slice names it. |
|
| 33 | + | unsafe { |
|
| 34 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 35 | + | } |
|
| 27 | 36 | } |
|
| 28 | 37 | } |
|
| 29 | 38 | ||
| 30 | 39 | @default unsafe fn main() -> i32 { |
|
| 31 | 40 | static data: [u8; 1] = [0]; |
test/tests/slice.append.owned.rad
added
+26 -0
| 1 | + | //! returns: 0 |
|
| 2 | + | //! Growth of a slice of owned pointers is not verifiable: its storage cannot be retyped. |
|
| 3 | + | ||
| 4 | + | /// Allocation failures. |
|
| 5 | + | union Error: Copy { |
|
| 6 | + | /// Diagnostic payload from the allocator. |
|
| 7 | + | Failed(u64), |
|
| 8 | + | } |
|
| 9 | + | ||
| 10 | + | /// Allocation interface of the slice growth builtins. |
|
| 11 | + | trait Allocator { |
|
| 12 | + | /// Allocate `size` bytes aligned to `alignment`. |
|
| 13 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Error); |
|
| 14 | + | } |
|
| 15 | + | ||
| 16 | + | /// Append an owned pointer and propagate the allocator error. |
|
| 17 | + | fn append(owned: *mut [*mut i32], value: *mut i32, a: &mut opaque Allocator) |
|
| 18 | + | -> *mut [*mut i32] throws (Error) |
|
| 19 | + | { |
|
| 20 | + | let mut items = owned; |
|
| 21 | + | return try items.append(value, a); |
|
| 22 | + | } |
|
| 23 | + | ||
| 24 | + | @default fn main() -> i32 { |
|
| 25 | + | return 0; |
|
| 26 | + | } |
test/tests/slice.append.rad
+1 -1
| 7 | 7 | } |
|
| 8 | 8 | ||
| 9 | 9 | /// Allocation interface of the slice growth builtins. |
|
| 10 | 10 | trait Allocator { |
|
| 11 | 11 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 12 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (Error); |
|
| 12 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Error); |
|
| 13 | 13 | } |
|
| 14 | 14 | ||
| 15 | 15 | /// Append and propagate the allocator error. |
|
| 16 | 16 | fn append(owned: *mut [i32], value: i32, a: &mut opaque Allocator) -> *mut [i32] throws (Error) { |
|
| 17 | 17 | let mut items = owned; |
test/tests/slice.append.ril
+44 -50
| 6 | 6 | slot 8 sub $L1; |
|
| 7 | 7 | } |
|
| 8 | 8 | layout $L3 size 16 align 8 { |
|
| 9 | 9 | slot 0 trait ref mut $T0; |
|
| 10 | 10 | } |
|
| 11 | - | layout $L4 size 24 align 8 {} |
|
| 12 | - | layout $L5 size 16 align 8 {} |
|
| 11 | + | layout $L4 size 1 align 1 {} |
|
| 12 | + | layout $L5 size 16 align 8 { |
|
| 13 | + | slot 0 slice own mut $L4; |
|
| 14 | + | } |
|
| 15 | + | layout $L6 size 40 align 8 { |
|
| 16 | + | slot 8 sub $L5; |
|
| 17 | + | } |
|
| 18 | + | layout $L7 size 16 align 8 {} |
|
| 13 | 19 | ||
| 14 | - | signature $S0 (ptr ref mut $L4, w32, w32) -> void; |
|
| 20 | + | signature $S0 (ptr ref mut $L6, w32, w32) -> void; |
|
| 15 | 21 | ||
| 16 | 22 | trait $T0 { |
|
| 17 | 23 | method $S0 at 1 ref mut; |
|
| 18 | 24 | } |
|
| 19 | 25 | ||
| 20 | 26 | fn void $append(ptr ref mut $L2 %0, ptr ref const $L1 %1, w32 %2, ptr ref const $L3 %3) { |
|
| 21 | 27 | @entry0 |
|
| 22 | 28 | reserve %4 $L1; |
|
| 23 | 29 | blit %4 %1 $L1; |
|
| 24 | 30 | reserve %5 $L2; |
|
| 25 | - | reserve %6 $L4; |
|
| 31 | + | reserve %6 $L6; |
|
| 26 | 32 | load w32 %7 %4 8; |
|
| 27 | 33 | load w32 %8 %4 12; |
|
| 28 | 34 | br.ult w32 %7 %8 @append.store1 @append.grow3; |
|
| 29 | 35 | @append.store1 |
|
| 30 | - | load w64 %25 %4 0; |
|
| 31 | - | mul w64 %26 %7 4; |
|
| 32 | - | add w64 %27 %25 %26; |
|
| 33 | - | store w32 %2 %27 0; |
|
| 34 | - | add w32 %28 %7 1; |
|
| 35 | - | store w32 %28 %4 8; |
|
| 36 | + | add w32 %19 %7 1; |
|
| 37 | + | slice.len %4 $L0 %19; |
|
| 38 | + | slice.elem %20 %4 %7 $L0; |
|
| 39 | + | store w32 %2 %20 0; |
|
| 36 | 40 | store w64 0 %5 0; |
|
| 37 | - | field %29 %5 8; |
|
| 38 | - | blit %29 %4 $L1; |
|
| 41 | + | field %21 %5 8; |
|
| 42 | + | blit %21 %4 $L1; |
|
| 39 | 43 | jmp @append.done2; |
|
| 40 | 44 | @append.done2 |
|
| 41 | - | load w64 %30 %5 0; |
|
| 42 | - | reserve %31 $L1; |
|
| 43 | - | br.ne w64 %30 0 @err15 @ok14; |
|
| 45 | + | load w64 %22 %5 0; |
|
| 46 | + | reserve %23 $L1; |
|
| 47 | + | br.ne w64 %22 0 @err12 @ok11; |
|
| 44 | 48 | @append.grow3 |
|
| 45 | 49 | br.ult w32 %8 1073741823 @guard#pass4 @guard#trap5; |
|
| 46 | 50 | @guard#pass4 |
|
| 47 | 51 | br.ult w32 %8 536870912 @append.double7 @append.exact8; |
|
| 48 | 52 | @guard#trap5 |
| 60 | 64 | @append.exact8 |
|
| 61 | 65 | add w32 %12 %8 1; |
|
| 62 | 66 | jmp @append.alloc6(%12); |
|
| 63 | 67 | @append.copy9 |
|
| 64 | 68 | store w64 %14 %5 0; |
|
| 65 | - | field %15 %6 8; |
|
| 69 | + | field %15 %6 24; |
|
| 66 | 70 | field %16 %5 24; |
|
| 67 | - | blit %16 %15 $L5; |
|
| 71 | + | blit %16 %15 $L7; |
|
| 68 | 72 | jmp @append.done2; |
|
| 69 | 73 | @append.copy10 |
|
| 70 | - | load w64 %17 %6 8; |
|
| 71 | - | load w64 %18 %4 0; |
|
| 72 | - | mul w64 %19 %7 4; |
|
| 73 | - | jmp @append11(0); |
|
| 74 | - | @append11(w64 %20) |
|
| 75 | - | br.ult w64 %20 %19 @append12 @append13; |
|
| 76 | - | @append12 |
|
| 77 | - | add w64 %21 %18 %20; |
|
| 78 | - | load w32 %22 %21 0; |
|
| 79 | - | add w64 %23 %17 %20; |
|
| 80 | - | store w32 %22 %23 0; |
|
| 81 | - | add w64 %24 %20 4; |
|
| 82 | - | jmp @append11(%24); |
|
| 83 | - | @append13 |
|
| 84 | - | store w64 %17 %4 0; |
|
| 85 | - | store w32 %9 %4 12; |
|
| 74 | + | field %17 %6 8; |
|
| 75 | + | reserve %18 $L1; |
|
| 76 | + | slice.cast %18 %17 $L0 %9; |
|
| 77 | + | slice.copy %18 %4 $L0; |
|
| 78 | + | slice.len %18 $L0 %7; |
|
| 79 | + | blit %4 %18 $L1; |
|
| 86 | 80 | jmp @append.store1; |
|
| 87 | - | @ok14 |
|
| 88 | - | field %32 %5 8; |
|
| 89 | - | blit %31 %32 $L1; |
|
| 90 | - | jmp @merge16; |
|
| 91 | - | @err15 |
|
| 92 | - | reserve %33 $L2; |
|
| 93 | - | store w64 %30 %33 0; |
|
| 94 | - | field %34 %5 24; |
|
| 95 | - | field %35 %33 24; |
|
| 96 | - | blit %35 %34 $L5; |
|
| 97 | - | blit %0 %33 $L2; |
|
| 81 | + | @ok11 |
|
| 82 | + | field %24 %5 8; |
|
| 83 | + | blit %23 %24 $L1; |
|
| 84 | + | jmp @merge13; |
|
| 85 | + | @err12 |
|
| 86 | + | reserve %25 $L2; |
|
| 87 | + | store w64 %22 %25 0; |
|
| 88 | + | field %26 %5 24; |
|
| 89 | + | field %27 %25 24; |
|
| 90 | + | blit %27 %26 $L7; |
|
| 91 | + | blit %0 %25 $L2; |
|
| 98 | 92 | ret; |
|
| 99 | - | @merge16 |
|
| 100 | - | reserve %36 $L2; |
|
| 101 | - | store w64 0 %36 0; |
|
| 102 | - | field %37 %36 8; |
|
| 103 | - | blit %37 %31 $L1; |
|
| 104 | - | blit %0 %36 $L2; |
|
| 93 | + | @merge13 |
|
| 94 | + | reserve %28 $L2; |
|
| 95 | + | store w64 0 %28 0; |
|
| 96 | + | field %29 %28 8; |
|
| 97 | + | blit %29 %23 $L1; |
|
| 98 | + | blit %0 %28 $L2; |
|
| 105 | 99 | ret; |
|
| 106 | 100 | } |
test/tests/slice.append.trait.rad
+1 -1
| 8 | 8 | } |
|
| 9 | 9 | ||
| 10 | 10 | /// Allocation interface. |
|
| 11 | 11 | trait Source { |
|
| 12 | 12 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 13 | - | fn (&mut Source) allocate(size: u32, alignment: u32) -> *mut opaque throws (Error); |
|
| 13 | + | fn (&mut Source) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Error); |
|
| 14 | 14 | } |
|
| 15 | 15 | ||
| 16 | 16 | /// Append and propagate the allocator error. |
|
| 17 | 17 | fn append(owned: *mut [i32], value: i32, a: &mut opaque Source) -> *mut [i32] throws (Error) { |
|
| 18 | 18 | let mut items = owned; |
test/tests/slice.append.trait.ril
+44 -50
| 6 | 6 | slot 8 sub $L1; |
|
| 7 | 7 | } |
|
| 8 | 8 | layout $L3 size 16 align 8 { |
|
| 9 | 9 | slot 0 trait ref mut $T0; |
|
| 10 | 10 | } |
|
| 11 | - | layout $L4 size 24 align 8 {} |
|
| 12 | - | layout $L5 size 16 align 8 {} |
|
| 11 | + | layout $L4 size 1 align 1 {} |
|
| 12 | + | layout $L5 size 16 align 8 { |
|
| 13 | + | slot 0 slice own mut $L4; |
|
| 14 | + | } |
|
| 15 | + | layout $L6 size 40 align 8 { |
|
| 16 | + | slot 8 sub $L5; |
|
| 17 | + | } |
|
| 18 | + | layout $L7 size 16 align 8 {} |
|
| 13 | 19 | ||
| 14 | - | signature $S0 (ptr ref mut $L4, w32, w32) -> void; |
|
| 20 | + | signature $S0 (ptr ref mut $L6, w32, w32) -> void; |
|
| 15 | 21 | ||
| 16 | 22 | trait $T0 { |
|
| 17 | 23 | method $S0 at 1 ref mut; |
|
| 18 | 24 | } |
|
| 19 | 25 | ||
| 20 | 26 | fn void $append(ptr ref mut $L2 %0, ptr ref const $L1 %1, w32 %2, ptr ref const $L3 %3) { |
|
| 21 | 27 | @entry0 |
|
| 22 | 28 | reserve %4 $L1; |
|
| 23 | 29 | blit %4 %1 $L1; |
|
| 24 | 30 | reserve %5 $L2; |
|
| 25 | - | reserve %6 $L4; |
|
| 31 | + | reserve %6 $L6; |
|
| 26 | 32 | load w32 %7 %4 8; |
|
| 27 | 33 | load w32 %8 %4 12; |
|
| 28 | 34 | br.ult w32 %7 %8 @append.store1 @append.grow3; |
|
| 29 | 35 | @append.store1 |
|
| 30 | - | load w64 %25 %4 0; |
|
| 31 | - | mul w64 %26 %7 4; |
|
| 32 | - | add w64 %27 %25 %26; |
|
| 33 | - | store w32 %2 %27 0; |
|
| 34 | - | add w32 %28 %7 1; |
|
| 35 | - | store w32 %28 %4 8; |
|
| 36 | + | add w32 %19 %7 1; |
|
| 37 | + | slice.len %4 $L0 %19; |
|
| 38 | + | slice.elem %20 %4 %7 $L0; |
|
| 39 | + | store w32 %2 %20 0; |
|
| 36 | 40 | store w64 0 %5 0; |
|
| 37 | - | field %29 %5 8; |
|
| 38 | - | blit %29 %4 $L1; |
|
| 41 | + | field %21 %5 8; |
|
| 42 | + | blit %21 %4 $L1; |
|
| 39 | 43 | jmp @append.done2; |
|
| 40 | 44 | @append.done2 |
|
| 41 | - | load w64 %30 %5 0; |
|
| 42 | - | reserve %31 $L1; |
|
| 43 | - | br.ne w64 %30 0 @err15 @ok14; |
|
| 45 | + | load w64 %22 %5 0; |
|
| 46 | + | reserve %23 $L1; |
|
| 47 | + | br.ne w64 %22 0 @err12 @ok11; |
|
| 44 | 48 | @append.grow3 |
|
| 45 | 49 | br.ult w32 %8 1073741823 @guard#pass4 @guard#trap5; |
|
| 46 | 50 | @guard#pass4 |
|
| 47 | 51 | br.ult w32 %8 536870912 @append.double7 @append.exact8; |
|
| 48 | 52 | @guard#trap5 |
| 60 | 64 | @append.exact8 |
|
| 61 | 65 | add w32 %12 %8 1; |
|
| 62 | 66 | jmp @append.alloc6(%12); |
|
| 63 | 67 | @append.copy9 |
|
| 64 | 68 | store w64 %14 %5 0; |
|
| 65 | - | field %15 %6 8; |
|
| 69 | + | field %15 %6 24; |
|
| 66 | 70 | field %16 %5 24; |
|
| 67 | - | blit %16 %15 $L5; |
|
| 71 | + | blit %16 %15 $L7; |
|
| 68 | 72 | jmp @append.done2; |
|
| 69 | 73 | @append.copy10 |
|
| 70 | - | load w64 %17 %6 8; |
|
| 71 | - | load w64 %18 %4 0; |
|
| 72 | - | mul w64 %19 %7 4; |
|
| 73 | - | jmp @append11(0); |
|
| 74 | - | @append11(w64 %20) |
|
| 75 | - | br.ult w64 %20 %19 @append12 @append13; |
|
| 76 | - | @append12 |
|
| 77 | - | add w64 %21 %18 %20; |
|
| 78 | - | load w32 %22 %21 0; |
|
| 79 | - | add w64 %23 %17 %20; |
|
| 80 | - | store w32 %22 %23 0; |
|
| 81 | - | add w64 %24 %20 4; |
|
| 82 | - | jmp @append11(%24); |
|
| 83 | - | @append13 |
|
| 84 | - | store w64 %17 %4 0; |
|
| 85 | - | store w32 %9 %4 12; |
|
| 74 | + | field %17 %6 8; |
|
| 75 | + | reserve %18 $L1; |
|
| 76 | + | slice.cast %18 %17 $L0 %9; |
|
| 77 | + | slice.copy %18 %4 $L0; |
|
| 78 | + | slice.len %18 $L0 %7; |
|
| 79 | + | blit %4 %18 $L1; |
|
| 86 | 80 | jmp @append.store1; |
|
| 87 | - | @ok14 |
|
| 88 | - | field %32 %5 8; |
|
| 89 | - | blit %31 %32 $L1; |
|
| 90 | - | jmp @merge16; |
|
| 91 | - | @err15 |
|
| 92 | - | reserve %33 $L2; |
|
| 93 | - | store w64 %30 %33 0; |
|
| 94 | - | field %34 %5 24; |
|
| 95 | - | field %35 %33 24; |
|
| 96 | - | blit %35 %34 $L5; |
|
| 97 | - | blit %0 %33 $L2; |
|
| 81 | + | @ok11 |
|
| 82 | + | field %24 %5 8; |
|
| 83 | + | blit %23 %24 $L1; |
|
| 84 | + | jmp @merge13; |
|
| 85 | + | @err12 |
|
| 86 | + | reserve %25 $L2; |
|
| 87 | + | store w64 %22 %25 0; |
|
| 88 | + | field %26 %5 24; |
|
| 89 | + | field %27 %25 24; |
|
| 90 | + | blit %27 %26 $L7; |
|
| 91 | + | blit %0 %25 $L2; |
|
| 98 | 92 | ret; |
|
| 99 | - | @merge16 |
|
| 100 | - | reserve %36 $L2; |
|
| 101 | - | store w64 0 %36 0; |
|
| 102 | - | field %37 %36 8; |
|
| 103 | - | blit %37 %31 $L1; |
|
| 104 | - | blit %0 %36 $L2; |
|
| 93 | + | @merge13 |
|
| 94 | + | reserve %28 $L2; |
|
| 95 | + | store w64 0 %28 0; |
|
| 96 | + | field %29 %28 8; |
|
| 97 | + | blit %29 %23 $L1; |
|
| 98 | + | blit %0 %28 $L2; |
|
| 105 | 99 | ret; |
|
| 106 | 100 | } |
test/tests/slice.append.values.rad
+12 -3
| 33 | 33 | } |
|
| 34 | 34 | ||
| 35 | 35 | /// Allocation interface of the slice growth builtins. |
|
| 36 | 36 | trait Allocator { |
|
| 37 | 37 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 38 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError); |
|
| 38 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError); |
|
| 39 | + | } |
|
| 40 | + | ||
| 41 | + | /// Allocate raw storage for `Arena`. |
|
| 42 | + | fn rawAllocate0(arena: &mut Arena, size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 43 | + | return arenaAlloc(arena, size, alignment); |
|
| 39 | 44 | } |
|
| 40 | 45 | ||
| 41 | 46 | instance Allocator for Arena { |
|
| 42 | 47 | /// Allocate from the arena. |
|
| 43 | - | fn (arena: &mut Arena) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 44 | - | return arenaAlloc(arena, size, alignment); |
|
| 48 | + | fn (arena: &mut Arena) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError) { |
|
| 49 | + | let ptr = try rawAllocate0(arena, size, alignment); |
|
| 50 | + | // The storage was just allocated, so no other slice names it. |
|
| 51 | + | unsafe { |
|
| 52 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 53 | + | } |
|
| 45 | 54 | } |
|
| 46 | 55 | } |
|
| 47 | 56 | ||
| 48 | 57 | unsafe static BUF: [u8; 4096] = undefined; |
|
| 49 | 58 |
test/tests/slice.extend.borrowed.rad
+12 -3
| 33 | 33 | } |
|
| 34 | 34 | ||
| 35 | 35 | /// Allocation interface of the slice growth builtins. |
|
| 36 | 36 | trait Allocator { |
|
| 37 | 37 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 38 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError); |
|
| 38 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError); |
|
| 39 | + | } |
|
| 40 | + | ||
| 41 | + | /// Allocate raw storage for `Arena`. |
|
| 42 | + | fn rawAllocate0(arena: &mut Arena, size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 43 | + | return arenaAlloc(arena, size, alignment); |
|
| 39 | 44 | } |
|
| 40 | 45 | ||
| 41 | 46 | instance Allocator for Arena { |
|
| 42 | 47 | /// Allocate from the arena. |
|
| 43 | - | fn (arena: &mut Arena) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 44 | - | return arenaAlloc(arena, size, alignment); |
|
| 48 | + | fn (arena: &mut Arena) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError) { |
|
| 49 | + | let ptr = try rawAllocate0(arena, size, alignment); |
|
| 50 | + | // The storage was just allocated, so no other slice names it. |
|
| 51 | + | unsafe { |
|
| 52 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 53 | + | } |
|
| 45 | 54 | } |
|
| 46 | 55 | } |
|
| 47 | 56 | ||
| 48 | 57 | /// Backing storage of the arena. |
|
| 49 | 58 | unsafe static ARENA_BUF: [u8; 8192] = undefined; |
test/tests/slice.extend.bytes.rad
+1 -1
| 9 | 9 | } |
|
| 10 | 10 | ||
| 11 | 11 | /// Allocation interface of the slice growth builtins. |
|
| 12 | 12 | trait Allocator { |
|
| 13 | 13 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 14 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (Full); |
|
| 14 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Full); |
|
| 15 | 15 | } |
|
| 16 | 16 | ||
| 17 | 17 | /// Append every byte of `src` to `s`. |
|
| 18 | 18 | unsafe fn joinBytes(owned: *mut [u8], src: *[u8], a: &mut opaque Allocator) { |
|
| 19 | 19 | let mut s = owned; |
test/tests/slice.extend.bytes.ril
+104 -140
| 6 | 6 | slot 0 slice own const $L1; |
|
| 7 | 7 | } |
|
| 8 | 8 | layout $L3 size 16 align 8 { |
|
| 9 | 9 | slot 0 trait ref mut $T0; |
|
| 10 | 10 | } |
|
| 11 | - | layout $L4 size 16 align 8 {} |
|
| 12 | - | layout $L5 size 16 align 8 { |
|
| 13 | - | slot 0 slice own mut $L6; |
|
| 11 | + | layout $L4 size 32 align 8 { |
|
| 12 | + | slot 8 sub $L0; |
|
| 14 | 13 | } |
|
| 15 | - | layout $L6 size 8 align 8 {} |
|
| 16 | - | layout $L7 size 16 align 8 { |
|
| 17 | - | slot 0 slice own const $L6; |
|
| 14 | + | layout $L5 size 16 align 8 {} |
|
| 15 | + | layout $L6 size 16 align 8 { |
|
| 16 | + | slot 0 slice own mut $L7; |
|
| 17 | + | } |
|
| 18 | + | layout $L7 size 8 align 8 {} |
|
| 19 | + | layout $L8 size 16 align 8 { |
|
| 20 | + | slot 0 slice own const $L7; |
|
| 18 | 21 | } |
|
| 19 | 22 | ||
| 20 | 23 | signature $S0 (ptr ref mut $L4, w32, w32) -> void; |
|
| 21 | 24 | ||
| 22 | 25 | trait $T0 { |
| 26 | 29 | fn void $joinBytes(ptr ref const $L0 %0, ptr ref const $L2 %1, ptr ref const $L3 %2) { |
|
| 27 | 30 | @entry0 |
|
| 28 | 31 | reserve %3 $L0; |
|
| 29 | 32 | blit %3 %0 $L0; |
|
| 30 | 33 | reserve %4 $L4; |
|
| 31 | - | load w32 %5 %3 8; |
|
| 32 | - | load w32 %6 %3 12; |
|
| 33 | - | load w32 %7 %1 8; |
|
| 34 | - | load w64 %8 %1 0; |
|
| 35 | - | add w64 %9 %5 %7; |
|
| 36 | - | br.ult w64 %6 %9 @extend.grow1 @extend.place2; |
|
| 34 | + | reserve %5 $L5; |
|
| 35 | + | load w32 %6 %3 8; |
|
| 36 | + | load w32 %7 %3 12; |
|
| 37 | + | load w32 %8 %1 8; |
|
| 38 | + | load w64 %9 %1 0; |
|
| 39 | + | reserve %10 $L2; |
|
| 40 | + | blit %10 %1 $L2; |
|
| 41 | + | add w64 %11 %6 %8; |
|
| 42 | + | br.ult w64 %7 %11 @extend.grow1 @extend.place2; |
|
| 37 | 43 | @extend.grow1 |
|
| 38 | - | br.ult w64 %9 2147483648 @guard#pass4 @guard#trap5; |
|
| 44 | + | br.ult w64 %11 2147483648 @guard#pass4 @guard#trap5; |
|
| 39 | 45 | @extend.place2 |
|
| 40 | - | load w64 %23 %3 0; |
|
| 41 | - | add w64 %24 %23 %5; |
|
| 42 | - | or w64 %25 %24 %8; |
|
| 43 | - | and w64 %26 %25 7; |
|
| 44 | - | sub w64 %27 %26 1; |
|
| 45 | - | and w64 %28 %27 -8; |
|
| 46 | - | and w64 %29 %7 %28; |
|
| 47 | - | jmp @extend14(0); |
|
| 46 | + | slice.len %3 $L1 %11; |
|
| 47 | + | reserve %22 $L0; |
|
| 48 | + | slice.sub %22 %3 $L1 %6 %11; |
|
| 49 | + | slice.copy %22 %10 $L1; |
|
| 50 | + | store w64 0 %5 0; |
|
| 51 | + | jmp @extend.done3; |
|
| 48 | 52 | @extend.done3 |
|
| 49 | - | load w64 %40 %4 0; |
|
| 50 | - | br.ne w64 %40 0 @err21 @ok20; |
|
| 53 | + | load w64 %23 %5 0; |
|
| 54 | + | br.ne w64 %23 0 @err13 @ok12; |
|
| 51 | 55 | @guard#pass4 |
|
| 52 | - | br.ult w32 %6 1073741824 @extend.double7 @extend.exact8; |
|
| 56 | + | br.ult w32 %7 1073741824 @extend.double7 @extend.exact8; |
|
| 53 | 57 | @guard#trap5 |
|
| 54 | 58 | ebreak; |
|
| 55 | 59 | unreachable; |
|
| 56 | - | @extend.alloc6(w32 %10) |
|
| 57 | - | mul w32 %13 %10 1; |
|
| 58 | - | trait.call void %2 0 at 1(%4, undefined, %13, 1); |
|
| 59 | - | load w64 %14 %4 0; |
|
| 60 | - | br.ne w64 %14 0 @extend.done3 @extend.copy10; |
|
| 60 | + | @extend.alloc6(w32 %12) |
|
| 61 | + | mul w32 %15 %12 1; |
|
| 62 | + | trait.call void %2 0 at 1(%4, undefined, %15, 1); |
|
| 63 | + | load w64 %16 %4 0; |
|
| 64 | + | br.ne w64 %16 0 @extend.copy10 @extend.copy11; |
|
| 61 | 65 | @extend.double7 |
|
| 62 | - | shl w32 %11 %6 1; |
|
| 63 | - | or w32 %12 %11 1; |
|
| 64 | - | br.ult w32 %12 %9 @extend.exact8 @extend.twice9; |
|
| 66 | + | shl w32 %13 %7 1; |
|
| 67 | + | or w32 %14 %13 1; |
|
| 68 | + | br.ult w32 %14 %11 @extend.exact8 @extend.twice9; |
|
| 65 | 69 | @extend.exact8 |
|
| 66 | - | jmp @extend.alloc6(%9); |
|
| 70 | + | jmp @extend.alloc6(%11); |
|
| 67 | 71 | @extend.twice9 |
|
| 68 | - | jmp @extend.alloc6(%12); |
|
| 72 | + | jmp @extend.alloc6(%14); |
|
| 69 | 73 | @extend.copy10 |
|
| 70 | - | load w32 %15 %3 8; |
|
| 71 | - | load w64 %16 %4 8; |
|
| 72 | - | load w64 %17 %3 0; |
|
| 73 | - | jmp @extend11(0); |
|
| 74 | - | @extend11(w64 %18) |
|
| 75 | - | br.ult w64 %18 %15 @extend12 @extend13; |
|
| 76 | - | @extend12 |
|
| 77 | - | add w64 %19 %17 %18; |
|
| 78 | - | load w8 %20 %19 0; |
|
| 79 | - | add w64 %21 %16 %18; |
|
| 80 | - | store w8 %20 %21 0; |
|
| 81 | - | add w64 %22 %18 1; |
|
| 82 | - | jmp @extend11(%22); |
|
| 83 | - | @extend13 |
|
| 84 | - | store w64 %16 %3 0; |
|
| 85 | - | store w32 %10 %3 12; |
|
| 86 | - | jmp @extend.place2; |
|
| 87 | - | @extend14(w64 %30) |
|
| 88 | - | br.ult w64 %30 %29 @extend15 @extend16; |
|
| 89 | - | @extend15 |
|
| 90 | - | add w64 %31 %8 %30; |
|
| 91 | - | load w64 %32 %31 0; |
|
| 92 | - | add w64 %33 %24 %30; |
|
| 93 | - | store w64 %32 %33 0; |
|
| 94 | - | add w64 %34 %30 8; |
|
| 95 | - | jmp @extend14(%34); |
|
| 96 | - | @extend16 |
|
| 97 | - | jmp @extend17(%30); |
|
| 98 | - | @extend17(w64 %35) |
|
| 99 | - | br.ult w64 %35 %7 @extend18 @extend19; |
|
| 100 | - | @extend18 |
|
| 101 | - | add w64 %36 %8 %35; |
|
| 102 | - | load w8 %37 %36 0; |
|
| 103 | - | add w64 %38 %24 %35; |
|
| 104 | - | store w8 %37 %38 0; |
|
| 105 | - | add w64 %39 %35 1; |
|
| 106 | - | jmp @extend17(%39); |
|
| 107 | - | @extend19 |
|
| 108 | - | store w32 %9 %3 8; |
|
| 109 | - | store w64 0 %4 0; |
|
| 74 | + | store w64 %16 %5 0; |
|
| 75 | + | field %17 %4 24; |
|
| 76 | + | field %18 %5 8; |
|
| 77 | + | blit %18 %17 $L1; |
|
| 110 | 78 | jmp @extend.done3; |
|
| 111 | - | @ok20 |
|
| 112 | - | jmp @merge22; |
|
| 113 | - | @err21 |
|
| 79 | + | @extend.copy11 |
|
| 80 | + | load w32 %19 %3 8; |
|
| 81 | + | field %20 %4 8; |
|
| 82 | + | reserve %21 $L0; |
|
| 83 | + | slice.cast %21 %20 $L1 %12; |
|
| 84 | + | slice.copy %21 %3 $L1; |
|
| 85 | + | slice.len %21 $L1 %19; |
|
| 86 | + | blit %3 %21 $L0; |
|
| 87 | + | jmp @extend.place2; |
|
| 88 | + | @ok12 |
|
| 89 | + | jmp @merge14; |
|
| 90 | + | @err13 |
|
| 114 | 91 | unreachable; |
|
| 115 | - | @merge22 |
|
| 92 | + | @merge14 |
|
| 116 | 93 | ret; |
|
| 117 | 94 | } |
|
| 118 | 95 | ||
| 119 | - | fn void $joinWords(ptr ref const $L5 %0, ptr ref const $L7 %1, ptr ref const $L3 %2) { |
|
| 96 | + | fn void $joinWords(ptr ref const $L6 %0, ptr ref const $L8 %1, ptr ref const $L3 %2) { |
|
| 120 | 97 | @entry0 |
|
| 121 | - | reserve %3 $L5; |
|
| 122 | - | blit %3 %0 $L5; |
|
| 98 | + | reserve %3 $L6; |
|
| 99 | + | blit %3 %0 $L6; |
|
| 123 | 100 | reserve %4 $L4; |
|
| 124 | - | load w32 %5 %3 8; |
|
| 125 | - | load w32 %6 %3 12; |
|
| 126 | - | load w32 %7 %1 8; |
|
| 127 | - | load w64 %8 %1 0; |
|
| 128 | - | add w64 %9 %5 %7; |
|
| 129 | - | br.ult w64 %6 %9 @extend.grow1 @extend.place2; |
|
| 101 | + | reserve %5 $L5; |
|
| 102 | + | load w32 %6 %3 8; |
|
| 103 | + | load w32 %7 %3 12; |
|
| 104 | + | load w32 %8 %1 8; |
|
| 105 | + | load w64 %9 %1 0; |
|
| 106 | + | reserve %10 $L8; |
|
| 107 | + | blit %10 %1 $L8; |
|
| 108 | + | add w64 %11 %6 %8; |
|
| 109 | + | br.ult w64 %7 %11 @extend.grow1 @extend.place2; |
|
| 130 | 110 | @extend.grow1 |
|
| 131 | - | br.ult w64 %9 536870912 @guard#pass4 @guard#trap5; |
|
| 111 | + | br.ult w64 %11 536870912 @guard#pass4 @guard#trap5; |
|
| 132 | 112 | @extend.place2 |
|
| 133 | - | load w64 %24 %3 0; |
|
| 134 | - | mul w64 %25 %5 8; |
|
| 135 | - | add w64 %26 %24 %25; |
|
| 136 | - | mul w64 %27 %7 8; |
|
| 137 | - | jmp @extend14(0); |
|
| 113 | + | slice.len %3 $L7 %11; |
|
| 114 | + | reserve %22 $L6; |
|
| 115 | + | slice.sub %22 %3 $L7 %6 %11; |
|
| 116 | + | slice.copy %22 %10 $L7; |
|
| 117 | + | store w64 0 %5 0; |
|
| 118 | + | jmp @extend.done3; |
|
| 138 | 119 | @extend.done3 |
|
| 139 | - | load w64 %33 %4 0; |
|
| 140 | - | br.ne w64 %33 0 @err18 @ok17; |
|
| 120 | + | load w64 %23 %5 0; |
|
| 121 | + | br.ne w64 %23 0 @err13 @ok12; |
|
| 141 | 122 | @guard#pass4 |
|
| 142 | - | br.ult w32 %6 268435456 @extend.double7 @extend.exact8; |
|
| 123 | + | br.ult w32 %7 268435456 @extend.double7 @extend.exact8; |
|
| 143 | 124 | @guard#trap5 |
|
| 144 | 125 | ebreak; |
|
| 145 | 126 | unreachable; |
|
| 146 | - | @extend.alloc6(w32 %10) |
|
| 147 | - | mul w32 %13 %10 8; |
|
| 148 | - | trait.call void %2 0 at 1(%4, undefined, %13, 8); |
|
| 149 | - | load w64 %14 %4 0; |
|
| 150 | - | br.ne w64 %14 0 @extend.done3 @extend.copy10; |
|
| 127 | + | @extend.alloc6(w32 %12) |
|
| 128 | + | mul w32 %15 %12 8; |
|
| 129 | + | trait.call void %2 0 at 1(%4, undefined, %15, 8); |
|
| 130 | + | load w64 %16 %4 0; |
|
| 131 | + | br.ne w64 %16 0 @extend.copy10 @extend.copy11; |
|
| 151 | 132 | @extend.double7 |
|
| 152 | - | shl w32 %11 %6 1; |
|
| 153 | - | or w32 %12 %11 1; |
|
| 154 | - | br.ult w32 %12 %9 @extend.exact8 @extend.twice9; |
|
| 133 | + | shl w32 %13 %7 1; |
|
| 134 | + | or w32 %14 %13 1; |
|
| 135 | + | br.ult w32 %14 %11 @extend.exact8 @extend.twice9; |
|
| 155 | 136 | @extend.exact8 |
|
| 156 | - | jmp @extend.alloc6(%9); |
|
| 137 | + | jmp @extend.alloc6(%11); |
|
| 157 | 138 | @extend.twice9 |
|
| 158 | - | jmp @extend.alloc6(%12); |
|
| 139 | + | jmp @extend.alloc6(%14); |
|
| 159 | 140 | @extend.copy10 |
|
| 160 | - | load w32 %15 %3 8; |
|
| 161 | - | load w64 %16 %4 8; |
|
| 162 | - | load w64 %17 %3 0; |
|
| 163 | - | mul w64 %18 %15 8; |
|
| 164 | - | jmp @extend11(0); |
|
| 165 | - | @extend11(w64 %19) |
|
| 166 | - | br.ult w64 %19 %18 @extend12 @extend13; |
|
| 167 | - | @extend12 |
|
| 168 | - | add w64 %20 %17 %19; |
|
| 169 | - | load w64 %21 %20 0; |
|
| 170 | - | add w64 %22 %16 %19; |
|
| 171 | - | store w64 %21 %22 0; |
|
| 172 | - | add w64 %23 %19 8; |
|
| 173 | - | jmp @extend11(%23); |
|
| 174 | - | @extend13 |
|
| 175 | - | store w64 %16 %3 0; |
|
| 176 | - | store w32 %10 %3 12; |
|
| 177 | - | jmp @extend.place2; |
|
| 178 | - | @extend14(w64 %28) |
|
| 179 | - | br.ult w64 %28 %27 @extend15 @extend16; |
|
| 180 | - | @extend15 |
|
| 181 | - | add w64 %29 %8 %28; |
|
| 182 | - | load w64 %30 %29 0; |
|
| 183 | - | add w64 %31 %26 %28; |
|
| 184 | - | store w64 %30 %31 0; |
|
| 185 | - | add w64 %32 %28 8; |
|
| 186 | - | jmp @extend14(%32); |
|
| 187 | - | @extend16 |
|
| 188 | - | store w32 %9 %3 8; |
|
| 189 | - | store w64 0 %4 0; |
|
| 141 | + | store w64 %16 %5 0; |
|
| 142 | + | field %17 %4 24; |
|
| 143 | + | field %18 %5 8; |
|
| 144 | + | blit %18 %17 $L1; |
|
| 190 | 145 | jmp @extend.done3; |
|
| 191 | - | @ok17 |
|
| 192 | - | jmp @merge19; |
|
| 193 | - | @err18 |
|
| 146 | + | @extend.copy11 |
|
| 147 | + | load w32 %19 %3 8; |
|
| 148 | + | field %20 %4 8; |
|
| 149 | + | reserve %21 $L6; |
|
| 150 | + | slice.cast %21 %20 $L7 %12; |
|
| 151 | + | slice.copy %21 %3 $L7; |
|
| 152 | + | slice.len %21 $L7 %19; |
|
| 153 | + | blit %3 %21 $L6; |
|
| 154 | + | jmp @extend.place2; |
|
| 155 | + | @ok12 |
|
| 156 | + | jmp @merge14; |
|
| 157 | + | @err13 |
|
| 194 | 158 | unreachable; |
|
| 195 | - | @merge19 |
|
| 159 | + | @merge14 |
|
| 196 | 160 | ret; |
|
| 197 | 161 | } |
|
| 198 | 162 | ||
| 199 | 163 | fn w32 $main() { |
|
| 200 | 164 | @entry0 |
test/tests/slice.extend.copy.rad
+23 -5
| 34 | 34 | } |
|
| 35 | 35 | ||
| 36 | 36 | /// Allocation interface of the slice growth builtins. |
|
| 37 | 37 | trait Allocator { |
|
| 38 | 38 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 39 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError); |
|
| 39 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError); |
|
| 40 | + | } |
|
| 41 | + | ||
| 42 | + | /// Allocate raw storage for `Arena`. |
|
| 43 | + | fn rawAllocate0(arena: &mut Arena, size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 44 | + | return arenaAlloc(arena, size, alignment); |
|
| 40 | 45 | } |
|
| 41 | 46 | ||
| 42 | 47 | instance Allocator for Arena { |
|
| 43 | 48 | /// Allocate from the arena. |
|
| 44 | - | fn (arena: &mut Arena) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 45 | - | return arenaAlloc(arena, size, alignment); |
|
| 49 | + | fn (arena: &mut Arena) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError) { |
|
| 50 | + | let ptr = try rawAllocate0(arena, size, alignment); |
|
| 51 | + | // The storage was just allocated, so no other slice names it. |
|
| 52 | + | unsafe { |
|
| 53 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 54 | + | } |
|
| 46 | 55 | } |
|
| 47 | 56 | } |
|
| 48 | 57 | ||
| 49 | 58 | /// Backing storage of the arena. |
|
| 50 | 59 | unsafe static ARENA_BUF: [u8; 16384] = undefined; |
|
| 51 | 60 | ||
| 52 | 61 | /// Allocator that rejects every request, for copies that fit in capacity. |
|
| 53 | 62 | record Reject {} |
|
| 54 | 63 | ||
| 64 | + | /// Allocate raw storage for `Reject`. |
|
| 65 | + | fn rawAllocate1(r: &mut Reject, size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 66 | + | panic "allocate: unexpected allocation"; |
|
| 67 | + | } |
|
| 68 | + | ||
| 55 | 69 | instance Allocator for Reject { |
|
| 56 | 70 | /// Reject the request. |
|
| 57 | - | fn (r: &mut Reject) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 58 | - | panic "allocate: unexpected allocation"; |
|
| 71 | + | fn (r: &mut Reject) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError) { |
|
| 72 | + | let ptr = try rawAllocate1(r, size, alignment); |
|
| 73 | + | // The storage was just allocated, so no other slice names it. |
|
| 74 | + | unsafe { |
|
| 75 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 76 | + | } |
|
| 59 | 77 | } |
|
| 60 | 78 | } |
|
| 61 | 79 | ||
| 62 | 80 | /// Source lengths of the checks. They straddle the 8-byte word size. |
|
| 63 | 81 | static COUNTS: [u32; 16] = [0, 1, 2, 3, 4, 5, 7, 8, 9, 12, 15, 16, 17, 24, 31, 33]; |
test/tests/slice.extend.overflow.rad
+12 -3
| 9 | 9 | } |
|
| 10 | 10 | ||
| 11 | 11 | /// Allocation interface of the slice growth builtins. |
|
| 12 | 12 | trait Allocator { |
|
| 13 | 13 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 14 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (Full); |
|
| 14 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Full); |
|
| 15 | 15 | } |
|
| 16 | 16 | ||
| 17 | 17 | /// Allocator that must not run. |
|
| 18 | 18 | record Never {} |
|
| 19 | 19 | ||
| 20 | + | /// Allocate raw storage for `Never`. |
|
| 21 | + | fn rawAllocate0(n: &mut Never, size: u32, alignment: u32) -> *mut opaque throws (Full) { |
|
| 22 | + | panic "allocate: unexpected allocation"; |
|
| 23 | + | } |
|
| 24 | + | ||
| 20 | 25 | instance Allocator for Never { |
|
| 21 | 26 | /// Fail the test if the builtin allocates. |
|
| 22 | - | fn (n: &mut Never) allocate(size: u32, alignment: u32) -> *mut opaque throws (Full) { |
|
| 23 | - | panic "allocate: unexpected allocation"; |
|
| 27 | + | fn (n: &mut Never) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Full) { |
|
| 28 | + | let ptr = try rawAllocate0(n, size, alignment); |
|
| 29 | + | // The storage was just allocated, so no other slice names it. |
|
| 30 | + | unsafe { |
|
| 31 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 32 | + | } |
|
| 24 | 33 | } |
|
| 25 | 34 | } |
|
| 26 | 35 | ||
| 27 | 36 | /// One element of storage, which the forged slices name but never access. |
|
| 28 | 37 | static DATA: [u8; 1] = [1]; |
test/tests/slice.extend.overflow.words.rad
+12 -3
| 9 | 9 | } |
|
| 10 | 10 | ||
| 11 | 11 | /// Allocation interface of the slice growth builtins. |
|
| 12 | 12 | trait Allocator { |
|
| 13 | 13 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 14 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (Full); |
|
| 14 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Full); |
|
| 15 | 15 | } |
|
| 16 | 16 | ||
| 17 | 17 | /// Allocator that must not run. |
|
| 18 | 18 | record Never {} |
|
| 19 | 19 | ||
| 20 | + | /// Allocate raw storage for `Never`. |
|
| 21 | + | fn rawAllocate0(n: &mut Never, size: u32, alignment: u32) -> *mut opaque throws (Full) { |
|
| 22 | + | panic "allocate: unexpected allocation"; |
|
| 23 | + | } |
|
| 24 | + | ||
| 20 | 25 | instance Allocator for Never { |
|
| 21 | 26 | /// Fail the test if the builtin allocates. |
|
| 22 | - | fn (n: &mut Never) allocate(size: u32, alignment: u32) -> *mut opaque throws (Full) { |
|
| 23 | - | panic "allocate: unexpected allocation"; |
|
| 27 | + | fn (n: &mut Never) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Full) { |
|
| 28 | + | let ptr = try rawAllocate0(n, size, alignment); |
|
| 29 | + | // The storage was just allocated, so no other slice names it. |
|
| 30 | + | unsafe { |
|
| 31 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 32 | + | } |
|
| 24 | 33 | } |
|
| 25 | 34 | } |
|
| 26 | 35 | ||
| 27 | 36 | /// One element of storage, which the forged slices name but never access. |
|
| 28 | 37 | static DATA: [u32; 1] = [1]; |
test/tests/slice.failure.rad
+13 -4
| 9 | 9 | } |
|
| 10 | 10 | ||
| 11 | 11 | /// Allocation interface of the slice growth builtins. |
|
| 12 | 12 | trait Allocator { |
|
| 13 | 13 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 14 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError); |
|
| 14 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError); |
|
| 15 | 15 | } |
|
| 16 | 16 | ||
| 17 | 17 | /// Allocator that rejects every request and counts the attempts. |
|
| 18 | 18 | record Rejecter { |
|
| 19 | 19 | /// Number of rejected requests. |
|
| 20 | 20 | attempts: u32, |
|
| 21 | 21 | } |
|
| 22 | 22 | ||
| 23 | + | /// Allocate raw storage for `Rejecter`. |
|
| 24 | + | fn rawAllocate0(r: &mut Rejecter, size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 25 | + | set r.attempts += 1; |
|
| 26 | + | throw AllocError::OutOfMemory; |
|
| 27 | + | } |
|
| 28 | + | ||
| 23 | 29 | instance Allocator for Rejecter { |
|
| 24 | 30 | /// Reject the request. |
|
| 25 | - | fn (r: &mut Rejecter) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 26 | - | set r.attempts += 1; |
|
| 27 | - | throw AllocError::OutOfMemory; |
|
| 31 | + | fn (r: &mut Rejecter) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError) { |
|
| 32 | + | let ptr = try rawAllocate0(r, size, alignment); |
|
| 33 | + | // The storage was just allocated, so no other slice names it. |
|
| 34 | + | unsafe { |
|
| 35 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 36 | + | } |
|
| 28 | 37 | } |
|
| 29 | 38 | } |
|
| 30 | 39 | ||
| 31 | 40 | /// Run the failed `extend` scenario and return the first failing code. |
|
| 32 | 41 | unsafe fn extendFailure() -> i32 { |
test/tests/slice.grow.rad
+12 -3
| 41 | 41 | } |
|
| 42 | 42 | ||
| 43 | 43 | /// Allocation interface of the slice growth builtins. |
|
| 44 | 44 | trait Allocator { |
|
| 45 | 45 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 46 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError); |
|
| 46 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError); |
|
| 47 | + | } |
|
| 48 | + | ||
| 49 | + | /// Allocate raw storage for `Arena`. |
|
| 50 | + | fn rawAllocate0(arena: &mut Arena, size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 51 | + | return arenaAlloc(arena, size, alignment); |
|
| 47 | 52 | } |
|
| 48 | 53 | ||
| 49 | 54 | instance Allocator for Arena { |
|
| 50 | 55 | /// Allocate from the arena. |
|
| 51 | - | fn (arena: &mut Arena) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 52 | - | return arenaAlloc(arena, size, alignment); |
|
| 56 | + | fn (arena: &mut Arena) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError) { |
|
| 57 | + | let ptr = try rawAllocate0(arena, size, alignment); |
|
| 58 | + | // The storage was just allocated, so no other slice names it. |
|
| 59 | + | unsafe { |
|
| 60 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 61 | + | } |
|
| 53 | 62 | } |
|
| 54 | 63 | } |
|
| 55 | 64 | ||
| 56 | 65 | /// Backing storage for every slice in this test. |
|
| 57 | 66 |
test/tests/slice.range.capacity.rad
+12 -3
| 8 | 8 | } |
|
| 9 | 9 | ||
| 10 | 10 | /// Allocation interface used by append. |
|
| 11 | 11 | trait Allocator { |
|
| 12 | 12 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 13 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError); |
|
| 13 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError); |
|
| 14 | 14 | } |
|
| 15 | 15 | ||
| 16 | 16 | /// Allocator that rejects allocation when append must use existing storage. |
|
| 17 | 17 | record Reject {} |
|
| 18 | 18 | ||
| 19 | + | /// Allocate raw storage for `Reject`. |
|
| 20 | + | fn rawAllocate0(r: &mut Reject, size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 21 | + | panic "allocate: unexpected allocation"; |
|
| 22 | + | } |
|
| 23 | + | ||
| 19 | 24 | instance Allocator for Reject { |
|
| 20 | 25 | /// Reject the request. |
|
| 21 | - | fn (r: &mut Reject) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 22 | - | panic "allocate: unexpected allocation"; |
|
| 26 | + | fn (r: &mut Reject) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError) { |
|
| 27 | + | let ptr = try rawAllocate0(r, size, alignment); |
|
| 28 | + | // The storage was just allocated, so no other slice names it. |
|
| 29 | + | unsafe { |
|
| 30 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 31 | + | } |
|
| 23 | 32 | } |
|
| 24 | 33 | } |
|
| 25 | 34 | ||
| 26 | 35 | /// Check dynamic range bounds and nested slice capacity. |
|
| 27 | 36 | fn checkRange(storage: *mut [i32], start: u32, end: u32) { |
test/tests/slice.reserve.rad
+19 -10
| 9 | 9 | } |
|
| 10 | 10 | ||
| 11 | 11 | /// Allocation interface with a single error type. |
|
| 12 | 12 | trait Allocator { |
|
| 13 | 13 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 14 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (Full); |
|
| 14 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Full); |
|
| 15 | 15 | } |
|
| 16 | 16 | ||
| 17 | 17 | /// Allocator state: bytes handed out and allocation calls made. |
|
| 18 | 18 | record Bump: Copy { |
|
| 19 | 19 | /// Bytes handed out so far. |
| 23 | 23 | } |
|
| 24 | 24 | ||
| 25 | 25 | /// Backing storage for the bump allocator. |
|
| 26 | 26 | static HEAP: [u8; 256] = [0; 256]; |
|
| 27 | 27 | ||
| 28 | + | /// Allocate raw storage for `Bump`. |
|
| 29 | + | fn rawAllocate0(state: &mut Bump, size: u32, alignment: u32) -> *mut opaque throws (Full) { |
|
| 30 | + | set state.calls += 1; |
|
| 31 | + | let start = state.used; |
|
| 32 | + | if size > 256 - start { |
|
| 33 | + | throw Full::Exhausted; |
|
| 34 | + | } |
|
| 35 | + | set state.used = start + size; |
|
| 36 | + | unsafe { |
|
| 37 | + | let base: *mut u8 = &mut HEAP[start]; |
|
| 38 | + | return base as *mut opaque; |
|
| 39 | + | } |
|
| 40 | + | } |
|
| 41 | + | ||
| 28 | 42 | instance Allocator for Bump { |
|
| 29 | 43 | /// Bump allocation from `HEAP`, failing when the request does not fit. |
|
| 30 | - | fn (state: &mut Bump) allocate(size: u32, alignment: u32) -> *mut opaque throws (Full) { |
|
| 31 | - | set state.calls += 1; |
|
| 32 | - | let start = state.used; |
|
| 33 | - | if size > 256 - start { |
|
| 34 | - | throw Full::Exhausted; |
|
| 35 | - | } |
|
| 36 | - | set state.used = start + size; |
|
| 44 | + | fn (state: &mut Bump) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Full) { |
|
| 45 | + | let ptr = try rawAllocate0(state, size, alignment); |
|
| 46 | + | // The storage was just allocated, so no other slice names it. |
|
| 37 | 47 | unsafe { |
|
| 38 | - | let base: *mut u8 = &mut HEAP[start]; |
|
| 39 | - | return base as *mut opaque; |
|
| 48 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 40 | 49 | } |
|
| 41 | 50 | } |
|
| 42 | 51 | } |
|
| 43 | 52 | ||
| 44 | 53 | @default unsafe fn main() -> i32 { |
test/tests/slice.reserve.result.rad
+1 -1
| 8 | 8 | } |
|
| 9 | 9 | ||
| 10 | 10 | /// Allocation interface of the slice growth builtins. |
|
| 11 | 11 | trait Allocator { |
|
| 12 | 12 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 13 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (Full); |
|
| 13 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Full); |
|
| 14 | 14 | } |
|
| 15 | 15 | ||
| 16 | 16 | /// Reserve room for four elements in `s`. |
|
| 17 | 17 | unsafe fn reserveSmall(owned: *mut [u32], a: &mut opaque Allocator) { |
|
| 18 | 18 | let mut s = owned; |
test/tests/slice.reserve.result.ril
+42 -38
| 3 | 3 | } |
|
| 4 | 4 | layout $L1 size 4 align 4 {} |
|
| 5 | 5 | layout $L2 size 16 align 8 { |
|
| 6 | 6 | slot 0 trait ref mut $T0; |
|
| 7 | 7 | } |
|
| 8 | - | layout $L3 size 16 align 8 {} |
|
| 8 | + | layout $L3 size 1 align 1 {} |
|
| 9 | + | layout $L4 size 16 align 8 { |
|
| 10 | + | slot 0 slice own mut $L3; |
|
| 11 | + | } |
|
| 12 | + | layout $L5 size 32 align 8 { |
|
| 13 | + | slot 8 sub $L4; |
|
| 14 | + | } |
|
| 15 | + | layout $L6 size 16 align 8 {} |
|
| 9 | 16 | ||
| 10 | - | signature $S0 (ptr ref mut $L3, w32, w32) -> void; |
|
| 17 | + | signature $S0 (ptr ref mut $L5, w32, w32) -> void; |
|
| 11 | 18 | ||
| 12 | 19 | trait $T0 { |
|
| 13 | 20 | method $S0 at 1 ref mut; |
|
| 14 | 21 | } |
|
| 15 | 22 | ||
| 16 | 23 | fn void $reserveSmall(ptr ref const $L0 %0, ptr ref const $L2 %1) { |
|
| 17 | 24 | @entry0 |
|
| 18 | 25 | reserve %2 $L0; |
|
| 19 | 26 | blit %2 %0 $L0; |
|
| 20 | - | reserve %3 $L3; |
|
| 21 | - | load w32 %4 %2 8; |
|
| 22 | - | load w32 %5 %2 12; |
|
| 23 | - | sub w32 %6 %5 %4; |
|
| 24 | - | br.ult w32 %6 4 @reserve.grow1 @reserve.success2; |
|
| 27 | + | reserve %3 $L5; |
|
| 28 | + | reserve %4 $L6; |
|
| 29 | + | load w32 %5 %2 8; |
|
| 30 | + | load w32 %6 %2 12; |
|
| 31 | + | sub w32 %7 %6 %5; |
|
| 32 | + | br.ult w32 %7 4 @reserve.grow1 @reserve.success2; |
|
| 25 | 33 | @reserve.grow1 |
|
| 26 | - | sub w32 %7 1073741823 %4; |
|
| 27 | - | uge w32 %8 %7 4; |
|
| 28 | - | br.ne w32 %8 0 @guard#pass4 @guard#trap5; |
|
| 34 | + | sub w32 %8 1073741823 %5; |
|
| 35 | + | uge w32 %9 %8 4; |
|
| 36 | + | br.ne w32 %9 0 @guard#pass4 @guard#trap5; |
|
| 29 | 37 | @reserve.success2 |
|
| 30 | - | store w64 0 %3 0; |
|
| 38 | + | store w64 0 %4 0; |
|
| 31 | 39 | jmp @reserve.done3; |
|
| 32 | 40 | @reserve.done3 |
|
| 33 | - | load w64 %20 %3 0; |
|
| 34 | - | br.ne w64 %20 0 @err11 @ok10; |
|
| 41 | + | load w64 %17 %4 0; |
|
| 42 | + | br.ne w64 %17 0 @err9 @ok8; |
|
| 35 | 43 | @guard#pass4 |
|
| 36 | - | add w32 %9 %4 4; |
|
| 37 | - | mul w32 %10 %9 4; |
|
| 38 | - | trait.call void %1 0 at 1(%3, undefined, %10, 4); |
|
| 39 | - | load w64 %11 %3 0; |
|
| 40 | - | br.ne w64 %11 0 @reserve.done3 @reserve.copy6; |
|
| 44 | + | add w32 %10 %5 4; |
|
| 45 | + | mul w32 %11 %10 4; |
|
| 46 | + | trait.call void %1 0 at 1(%3, undefined, %11, 4); |
|
| 47 | + | load w64 %12 %3 0; |
|
| 48 | + | br.ne w64 %12 0 @reserve.copy6 @reserve.copy7; |
|
| 41 | 49 | @guard#trap5 |
|
| 42 | 50 | ebreak; |
|
| 43 | 51 | unreachable; |
|
| 44 | 52 | @reserve.copy6 |
|
| 45 | - | load w64 %12 %3 8; |
|
| 46 | - | load w64 %13 %2 0; |
|
| 47 | - | mul w64 %14 %4 4; |
|
| 48 | - | jmp @reserve7(0); |
|
| 49 | - | @reserve7(w64 %15) |
|
| 50 | - | br.ult w64 %15 %14 @reserve8 @reserve9; |
|
| 51 | - | @reserve8 |
|
| 52 | - | add w64 %16 %13 %15; |
|
| 53 | - | load w32 %17 %16 0; |
|
| 54 | - | add w64 %18 %12 %15; |
|
| 55 | - | store w32 %17 %18 0; |
|
| 56 | - | add w64 %19 %15 4; |
|
| 57 | - | jmp @reserve7(%19); |
|
| 58 | - | @reserve9 |
|
| 59 | - | store w64 %12 %2 0; |
|
| 60 | - | store w32 %9 %2 12; |
|
| 53 | + | store w64 %12 %4 0; |
|
| 54 | + | field %13 %3 24; |
|
| 55 | + | field %14 %4 8; |
|
| 56 | + | blit %14 %13 $L3; |
|
| 57 | + | jmp @reserve.done3; |
|
| 58 | + | @reserve.copy7 |
|
| 59 | + | field %15 %3 8; |
|
| 60 | + | reserve %16 $L0; |
|
| 61 | + | slice.cast %16 %15 $L1 %10; |
|
| 62 | + | slice.copy %16 %2 $L1; |
|
| 63 | + | slice.len %16 $L1 %5; |
|
| 64 | + | blit %2 %16 $L0; |
|
| 61 | 65 | jmp @reserve.success2; |
|
| 62 | - | @ok10 |
|
| 63 | - | jmp @merge12; |
|
| 64 | - | @err11 |
|
| 66 | + | @ok8 |
|
| 67 | + | jmp @merge10; |
|
| 68 | + | @err9 |
|
| 65 | 69 | unreachable; |
|
| 66 | - | @merge12 |
|
| 70 | + | @merge10 |
|
| 67 | 71 | ret; |
|
| 68 | 72 | } |
|
| 69 | 73 | ||
| 70 | 74 | fn w32 $main() { |
|
| 71 | 75 | @entry0 |
test/tests/slice.resize.extend.rad
+1 -1
| 8 | 8 | } |
|
| 9 | 9 | ||
| 10 | 10 | /// Allocation interface of the slice growth builtins. |
|
| 11 | 11 | trait Allocator { |
|
| 12 | 12 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 13 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (Full); |
|
| 13 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Full); |
|
| 14 | 14 | } |
|
| 15 | 15 | ||
| 16 | 16 | /// Resize `s` to four elements filled with seven. |
|
| 17 | 17 | unsafe fn fillSmall(owned: *mut [u32], a: &mut opaque Allocator) { |
|
| 18 | 18 | let mut s = owned; |
test/tests/slice.resize.extend.ril
+100 -127
| 3 | 3 | } |
|
| 4 | 4 | layout $L1 size 4 align 4 {} |
|
| 5 | 5 | layout $L2 size 16 align 8 { |
|
| 6 | 6 | slot 0 trait ref mut $T0; |
|
| 7 | 7 | } |
|
| 8 | - | layout $L3 size 16 align 8 {} |
|
| 8 | + | layout $L3 size 1 align 1 {} |
|
| 9 | 9 | layout $L4 size 16 align 8 { |
|
| 10 | + | slot 0 slice own mut $L3; |
|
| 11 | + | } |
|
| 12 | + | layout $L5 size 32 align 8 { |
|
| 13 | + | slot 8 sub $L4; |
|
| 14 | + | } |
|
| 15 | + | layout $L6 size 16 align 8 {} |
|
| 16 | + | layout $L7 size 16 align 8 { |
|
| 10 | 17 | slot 0 slice own const $L1; |
|
| 11 | 18 | } |
|
| 12 | 19 | ||
| 13 | - | signature $S0 (ptr ref mut $L3, w32, w32) -> void; |
|
| 20 | + | signature $S0 (ptr ref mut $L5, w32, w32) -> void; |
|
| 14 | 21 | ||
| 15 | 22 | trait $T0 { |
|
| 16 | 23 | method $S0 at 1 ref mut; |
|
| 17 | 24 | } |
|
| 18 | 25 | ||
| 19 | 26 | fn void $fillSmall(ptr ref const $L0 %0, ptr ref const $L2 %1) { |
|
| 20 | 27 | @entry0 |
|
| 21 | 28 | reserve %2 $L0; |
|
| 22 | 29 | blit %2 %0 $L0; |
|
| 23 | - | reserve %3 $L3; |
|
| 24 | - | load w32 %4 %2 8; |
|
| 25 | - | load w32 %5 %2 12; |
|
| 26 | - | br.ult w32 4 %4 @resize.shrink1 @resize.check2; |
|
| 30 | + | reserve %3 $L5; |
|
| 31 | + | reserve %4 $L6; |
|
| 32 | + | load w32 %5 %2 8; |
|
| 33 | + | load w32 %6 %2 12; |
|
| 34 | + | br.ult w32 4 %5 @resize.shrink1 @resize.check2; |
|
| 27 | 35 | @resize.shrink1 |
|
| 28 | - | store w32 4 %2 8; |
|
| 36 | + | slice.len %2 $L1 4; |
|
| 29 | 37 | jmp @resize.success5; |
|
| 30 | 38 | @resize.check2 |
|
| 31 | - | br.ult w32 %5 4 @resize.grow3 @resize.fill4; |
|
| 39 | + | br.ult w32 %6 4 @resize.grow3 @resize.fill4; |
|
| 32 | 40 | @resize.grow3 |
|
| 33 | - | uge w32 %6 1073741823 4; |
|
| 34 | - | br.ne w32 %6 0 @guard#pass7 @guard#trap8; |
|
| 41 | + | uge w32 %7 1073741823 4; |
|
| 42 | + | br.ne w32 %7 0 @guard#pass7 @guard#trap8; |
|
| 35 | 43 | @resize.fill4 |
|
| 36 | - | load w64 %18 %2 0; |
|
| 37 | - | jmp @resize.fill13(%4); |
|
| 44 | + | slice.len %2 $L1 4; |
|
| 45 | + | jmp @resize.fill11(%5); |
|
| 38 | 46 | @resize.success5 |
|
| 39 | - | store w64 0 %3 0; |
|
| 47 | + | store w64 0 %4 0; |
|
| 40 | 48 | jmp @resize.done6; |
|
| 41 | 49 | @resize.done6 |
|
| 42 | - | load w64 %23 %3 0; |
|
| 43 | - | br.ne w64 %23 0 @err17 @ok16; |
|
| 50 | + | load w64 %18 %4 0; |
|
| 51 | + | br.ne w64 %18 0 @err15 @ok14; |
|
| 44 | 52 | @guard#pass7 |
|
| 45 | - | mul w32 %7 4 4; |
|
| 46 | - | trait.call void %1 0 at 1(%3, undefined, %7, 4); |
|
| 47 | - | load w64 %8 %3 0; |
|
| 48 | - | br.ne w64 %8 0 @resize.done6 @resize.copy9; |
|
| 53 | + | mul w32 %8 4 4; |
|
| 54 | + | trait.call void %1 0 at 1(%3, undefined, %8, 4); |
|
| 55 | + | load w64 %9 %3 0; |
|
| 56 | + | br.ne w64 %9 0 @resize.copy9 @resize.copy10; |
|
| 49 | 57 | @guard#trap8 |
|
| 50 | 58 | ebreak; |
|
| 51 | 59 | unreachable; |
|
| 52 | 60 | @resize.copy9 |
|
| 53 | - | load w32 %9 %2 8; |
|
| 54 | - | load w64 %10 %3 8; |
|
| 55 | - | load w64 %11 %2 0; |
|
| 56 | - | mul w64 %12 %9 4; |
|
| 57 | - | jmp @resize10(0); |
|
| 58 | - | @resize10(w64 %13) |
|
| 59 | - | br.ult w64 %13 %12 @resize11 @resize12; |
|
| 60 | - | @resize11 |
|
| 61 | - | add w64 %14 %11 %13; |
|
| 62 | - | load w32 %15 %14 0; |
|
| 63 | - | add w64 %16 %10 %13; |
|
| 64 | - | store w32 %15 %16 0; |
|
| 65 | - | add w64 %17 %13 4; |
|
| 66 | - | jmp @resize10(%17); |
|
| 67 | - | @resize12 |
|
| 68 | - | store w64 %10 %2 0; |
|
| 69 | - | store w32 4 %2 12; |
|
| 61 | + | store w64 %9 %4 0; |
|
| 62 | + | field %10 %3 24; |
|
| 63 | + | field %11 %4 8; |
|
| 64 | + | blit %11 %10 $L3; |
|
| 65 | + | jmp @resize.done6; |
|
| 66 | + | @resize.copy10 |
|
| 67 | + | load w32 %12 %2 8; |
|
| 68 | + | field %13 %3 8; |
|
| 69 | + | reserve %14 $L0; |
|
| 70 | + | slice.cast %14 %13 $L1 4; |
|
| 71 | + | slice.copy %14 %2 $L1; |
|
| 72 | + | slice.len %14 $L1 %12; |
|
| 73 | + | blit %2 %14 $L0; |
|
| 70 | 74 | jmp @resize.fill4; |
|
| 71 | - | @resize.fill13(w32 %19) |
|
| 72 | - | br.ult w32 %19 4 @resize.fill14 @resize.fill15; |
|
| 73 | - | @resize.fill14 |
|
| 74 | - | mul w64 %20 %19 4; |
|
| 75 | - | add w64 %21 %18 %20; |
|
| 76 | - | store w32 7 %21 0; |
|
| 77 | - | add w32 %22 %19 1; |
|
| 78 | - | jmp @resize.fill13(%22); |
|
| 79 | - | @resize.fill15 |
|
| 80 | - | store w32 4 %2 8; |
|
| 75 | + | @resize.fill11(w32 %15) |
|
| 76 | + | br.ult w32 %15 4 @resize.fill12 @resize.fill13; |
|
| 77 | + | @resize.fill12 |
|
| 78 | + | slice.elem %16 %2 %15 $L1; |
|
| 79 | + | store w32 7 %16 0; |
|
| 80 | + | add w32 %17 %15 1; |
|
| 81 | + | jmp @resize.fill11(%17); |
|
| 82 | + | @resize.fill13 |
|
| 81 | 83 | jmp @resize.success5; |
|
| 82 | - | @ok16 |
|
| 83 | - | jmp @merge18; |
|
| 84 | - | @err17 |
|
| 84 | + | @ok14 |
|
| 85 | + | jmp @merge16; |
|
| 86 | + | @err15 |
|
| 85 | 87 | unreachable; |
|
| 86 | - | @merge18 |
|
| 88 | + | @merge16 |
|
| 87 | 89 | ret; |
|
| 88 | 90 | } |
|
| 89 | 91 | ||
| 90 | - | fn void $joinSmall(ptr ref const $L0 %0, ptr ref const $L4 %1, ptr ref const $L2 %2) { |
|
| 92 | + | fn void $joinSmall(ptr ref const $L0 %0, ptr ref const $L7 %1, ptr ref const $L2 %2) { |
|
| 91 | 93 | @entry0 |
|
| 92 | 94 | reserve %3 $L0; |
|
| 93 | 95 | blit %3 %0 $L0; |
|
| 94 | - | reserve %4 $L3; |
|
| 95 | - | load w32 %5 %3 8; |
|
| 96 | - | load w32 %6 %3 12; |
|
| 97 | - | load w32 %7 %1 8; |
|
| 98 | - | load w64 %8 %1 0; |
|
| 99 | - | add w64 %9 %5 %7; |
|
| 100 | - | br.ult w64 %6 %9 @extend.grow1 @extend.place2; |
|
| 96 | + | reserve %4 $L5; |
|
| 97 | + | reserve %5 $L6; |
|
| 98 | + | load w32 %6 %3 8; |
|
| 99 | + | load w32 %7 %3 12; |
|
| 100 | + | load w32 %8 %1 8; |
|
| 101 | + | load w64 %9 %1 0; |
|
| 102 | + | reserve %10 $L7; |
|
| 103 | + | blit %10 %1 $L7; |
|
| 104 | + | add w64 %11 %6 %8; |
|
| 105 | + | br.ult w64 %7 %11 @extend.grow1 @extend.place2; |
|
| 101 | 106 | @extend.grow1 |
|
| 102 | - | br.ult w64 %9 1073741824 @guard#pass4 @guard#trap5; |
|
| 107 | + | br.ult w64 %11 1073741824 @guard#pass4 @guard#trap5; |
|
| 103 | 108 | @extend.place2 |
|
| 104 | - | load w64 %24 %3 0; |
|
| 105 | - | mul w64 %25 %5 4; |
|
| 106 | - | add w64 %26 %24 %25; |
|
| 107 | - | mul w64 %27 %7 4; |
|
| 108 | - | or w64 %28 %26 %8; |
|
| 109 | - | and w64 %29 %28 7; |
|
| 110 | - | sub w64 %30 %29 1; |
|
| 111 | - | and w64 %31 %30 -8; |
|
| 112 | - | and w64 %32 %27 %31; |
|
| 113 | - | jmp @extend14(0); |
|
| 109 | + | slice.len %3 $L1 %11; |
|
| 110 | + | reserve %22 $L0; |
|
| 111 | + | slice.sub %22 %3 $L1 %6 %11; |
|
| 112 | + | slice.copy %22 %10 $L1; |
|
| 113 | + | store w64 0 %5 0; |
|
| 114 | + | jmp @extend.done3; |
|
| 114 | 115 | @extend.done3 |
|
| 115 | - | load w64 %43 %4 0; |
|
| 116 | - | br.ne w64 %43 0 @err21 @ok20; |
|
| 116 | + | load w64 %23 %5 0; |
|
| 117 | + | br.ne w64 %23 0 @err13 @ok12; |
|
| 117 | 118 | @guard#pass4 |
|
| 118 | - | br.ult w32 %6 536870912 @extend.double7 @extend.exact8; |
|
| 119 | + | br.ult w32 %7 536870912 @extend.double7 @extend.exact8; |
|
| 119 | 120 | @guard#trap5 |
|
| 120 | 121 | ebreak; |
|
| 121 | 122 | unreachable; |
|
| 122 | - | @extend.alloc6(w32 %10) |
|
| 123 | - | mul w32 %13 %10 4; |
|
| 124 | - | trait.call void %2 0 at 1(%4, undefined, %13, 4); |
|
| 125 | - | load w64 %14 %4 0; |
|
| 126 | - | br.ne w64 %14 0 @extend.done3 @extend.copy10; |
|
| 123 | + | @extend.alloc6(w32 %12) |
|
| 124 | + | mul w32 %15 %12 4; |
|
| 125 | + | trait.call void %2 0 at 1(%4, undefined, %15, 4); |
|
| 126 | + | load w64 %16 %4 0; |
|
| 127 | + | br.ne w64 %16 0 @extend.copy10 @extend.copy11; |
|
| 127 | 128 | @extend.double7 |
|
| 128 | - | shl w32 %11 %6 1; |
|
| 129 | - | or w32 %12 %11 1; |
|
| 130 | - | br.ult w32 %12 %9 @extend.exact8 @extend.twice9; |
|
| 129 | + | shl w32 %13 %7 1; |
|
| 130 | + | or w32 %14 %13 1; |
|
| 131 | + | br.ult w32 %14 %11 @extend.exact8 @extend.twice9; |
|
| 131 | 132 | @extend.exact8 |
|
| 132 | - | jmp @extend.alloc6(%9); |
|
| 133 | + | jmp @extend.alloc6(%11); |
|
| 133 | 134 | @extend.twice9 |
|
| 134 | - | jmp @extend.alloc6(%12); |
|
| 135 | + | jmp @extend.alloc6(%14); |
|
| 135 | 136 | @extend.copy10 |
|
| 136 | - | load w32 %15 %3 8; |
|
| 137 | - | load w64 %16 %4 8; |
|
| 138 | - | load w64 %17 %3 0; |
|
| 139 | - | mul w64 %18 %15 4; |
|
| 140 | - | jmp @extend11(0); |
|
| 141 | - | @extend11(w64 %19) |
|
| 142 | - | br.ult w64 %19 %18 @extend12 @extend13; |
|
| 143 | - | @extend12 |
|
| 144 | - | add w64 %20 %17 %19; |
|
| 145 | - | load w32 %21 %20 0; |
|
| 146 | - | add w64 %22 %16 %19; |
|
| 147 | - | store w32 %21 %22 0; |
|
| 148 | - | add w64 %23 %19 4; |
|
| 149 | - | jmp @extend11(%23); |
|
| 150 | - | @extend13 |
|
| 151 | - | store w64 %16 %3 0; |
|
| 152 | - | store w32 %10 %3 12; |
|
| 153 | - | jmp @extend.place2; |
|
| 154 | - | @extend14(w64 %33) |
|
| 155 | - | br.ult w64 %33 %32 @extend15 @extend16; |
|
| 156 | - | @extend15 |
|
| 157 | - | add w64 %34 %8 %33; |
|
| 158 | - | load w64 %35 %34 0; |
|
| 159 | - | add w64 %36 %26 %33; |
|
| 160 | - | store w64 %35 %36 0; |
|
| 161 | - | add w64 %37 %33 8; |
|
| 162 | - | jmp @extend14(%37); |
|
| 163 | - | @extend16 |
|
| 164 | - | jmp @extend17(%33); |
|
| 165 | - | @extend17(w64 %38) |
|
| 166 | - | br.ult w64 %38 %27 @extend18 @extend19; |
|
| 167 | - | @extend18 |
|
| 168 | - | add w64 %39 %8 %38; |
|
| 169 | - | load w32 %40 %39 0; |
|
| 170 | - | add w64 %41 %26 %38; |
|
| 171 | - | store w32 %40 %41 0; |
|
| 172 | - | add w64 %42 %38 4; |
|
| 173 | - | jmp @extend17(%42); |
|
| 174 | - | @extend19 |
|
| 175 | - | store w32 %9 %3 8; |
|
| 176 | - | store w64 0 %4 0; |
|
| 137 | + | store w64 %16 %5 0; |
|
| 138 | + | field %17 %4 24; |
|
| 139 | + | field %18 %5 8; |
|
| 140 | + | blit %18 %17 $L3; |
|
| 177 | 141 | jmp @extend.done3; |
|
| 178 | - | @ok20 |
|
| 179 | - | jmp @merge22; |
|
| 180 | - | @err21 |
|
| 142 | + | @extend.copy11 |
|
| 143 | + | load w32 %19 %3 8; |
|
| 144 | + | field %20 %4 8; |
|
| 145 | + | reserve %21 $L0; |
|
| 146 | + | slice.cast %21 %20 $L1 %12; |
|
| 147 | + | slice.copy %21 %3 $L1; |
|
| 148 | + | slice.len %21 $L1 %19; |
|
| 149 | + | blit %3 %21 $L0; |
|
| 150 | + | jmp @extend.place2; |
|
| 151 | + | @ok12 |
|
| 152 | + | jmp @merge14; |
|
| 153 | + | @err13 |
|
| 181 | 154 | unreachable; |
|
| 182 | - | @merge22 |
|
| 155 | + | @merge14 |
|
| 183 | 156 | ret; |
|
| 184 | 157 | } |
|
| 185 | 158 | ||
| 186 | 159 | fn w32 $main() { |
|
| 187 | 160 | @entry0 |
test/tests/slice.resize.fill.snapshot.rad
+17 -8
| 8 | 8 | record Value: Copy { x: u64, y: u64, z: u64 } |
|
| 9 | 9 | record Arena { data: *mut [u8] } |
|
| 10 | 10 | unsafe static SOURCE: Value = Value { x: 7, y: 8, z: 9 }; |
|
| 11 | 11 | ||
| 12 | 12 | trait Allocator { |
|
| 13 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError); |
|
| 13 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError); |
|
| 14 | + | } |
|
| 15 | + | ||
| 16 | + | /// Allocate raw storage for `Arena`. |
|
| 17 | + | fn rawAllocate0(self: &mut Arena, size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 18 | + | assert size <= self.data.len; |
|
| 19 | + | unsafe { |
|
| 20 | + | set SOURCE.x = 99; |
|
| 21 | + | } |
|
| 22 | + | unsafe { |
|
| 23 | + | let base: *mut u8 = &mut self.data[0]; |
|
| 24 | + | return base as *mut opaque; |
|
| 25 | + | } |
|
| 14 | 26 | } |
|
| 15 | 27 | ||
| 16 | 28 | instance Allocator for Arena { |
|
| 17 | - | fn (self: &mut Arena) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 18 | - | assert size <= self.data.len; |
|
| 19 | - | unsafe { |
|
| 20 | - | set SOURCE.x = 99; |
|
| 21 | - | } |
|
| 29 | + | fn (self: &mut Arena) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError) { |
|
| 30 | + | let ptr = try rawAllocate0(self, size, alignment); |
|
| 31 | + | // The storage was just allocated, so no other slice names it. |
|
| 22 | 32 | unsafe { |
|
| 23 | - | let base: *mut u8 = &mut self.data[0]; |
|
| 24 | - | return base as *mut opaque; |
|
| 33 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 25 | 34 | } |
|
| 26 | 35 | } |
|
| 27 | 36 | } |
|
| 28 | 37 | ||
| 29 | 38 | unsafe fn resizeFill(arena: &mut Arena) { |
test/tests/slice.sizes.rad
+12 -3
| 38 | 38 | } |
|
| 39 | 39 | ||
| 40 | 40 | /// Allocation interface of the slice growth builtins. |
|
| 41 | 41 | trait Allocator { |
|
| 42 | 42 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 43 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError); |
|
| 43 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError); |
|
| 44 | + | } |
|
| 45 | + | ||
| 46 | + | /// Allocate raw storage for `Arena`. |
|
| 47 | + | fn rawAllocate0(arena: &mut Arena, size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 48 | + | return arenaAlloc(arena, size, alignment); |
|
| 44 | 49 | } |
|
| 45 | 50 | ||
| 46 | 51 | instance Allocator for Arena { |
|
| 47 | 52 | /// Allocate from the arena. |
|
| 48 | - | fn (arena: &mut Arena) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 49 | - | return arenaAlloc(arena, size, alignment); |
|
| 53 | + | fn (arena: &mut Arena) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError) { |
|
| 54 | + | let ptr = try rawAllocate0(arena, size, alignment); |
|
| 55 | + | // The storage was just allocated, so no other slice names it. |
|
| 56 | + | unsafe { |
|
| 57 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 58 | + | } |
|
| 50 | 59 | } |
|
| 51 | 60 | } |
|
| 52 | 61 | ||
| 53 | 62 | /// Element of 12 bytes and alignment 4. |
|
| 54 | 63 | record Triple: Copy { |
test/tests/slice.take.rad
+12 -3
| 32 | 32 | } |
|
| 33 | 33 | ||
| 34 | 34 | /// Allocation interface of the slice growth builtins. |
|
| 35 | 35 | trait Allocator { |
|
| 36 | 36 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 37 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError); |
|
| 37 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError); |
|
| 38 | + | } |
|
| 39 | + | ||
| 40 | + | /// Allocate raw storage for `Arena`. |
|
| 41 | + | fn rawAllocate0(arena: &mut Arena, size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 42 | + | return arenaAlloc(arena, size, alignment); |
|
| 38 | 43 | } |
|
| 39 | 44 | ||
| 40 | 45 | instance Allocator for Arena { |
|
| 41 | 46 | /// Allocate from the arena. |
|
| 42 | - | fn (arena: &mut Arena) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 43 | - | return arenaAlloc(arena, size, alignment); |
|
| 47 | + | fn (arena: &mut Arena) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError) { |
|
| 48 | + | let ptr = try rawAllocate0(arena, size, alignment); |
|
| 49 | + | // The storage was just allocated, so no other slice names it. |
|
| 50 | + | unsafe { |
|
| 51 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 52 | + | } |
|
| 44 | 53 | } |
|
| 45 | 54 | } |
|
| 46 | 55 | ||
| 47 | 56 | /// Holder of a slice header that is reached through a mutable reference. |
|
| 48 | 57 | record Holder { |
test/tests/slice.take.sizes.rad
+12 -3
| 64 | 64 | } |
|
| 65 | 65 | ||
| 66 | 66 | /// Allocation interface of the slice growth builtins. |
|
| 67 | 67 | trait Allocator { |
|
| 68 | 68 | /// Allocate `size` bytes aligned to `alignment`. |
|
| 69 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError); |
|
| 69 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError); |
|
| 70 | + | } |
|
| 71 | + | ||
| 72 | + | /// Allocate raw storage for `Arena`. |
|
| 73 | + | fn rawAllocate0(arena: &mut Arena, size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 74 | + | return arenaAlloc(arena, size, alignment); |
|
| 70 | 75 | } |
|
| 71 | 76 | ||
| 72 | 77 | instance Allocator for Arena { |
|
| 73 | 78 | /// Allocate from the arena. |
|
| 74 | - | fn (arena: &mut Arena) allocate(size: u32, alignment: u32) -> *mut opaque throws (AllocError) { |
|
| 75 | - | return arenaAlloc(arena, size, alignment); |
|
| 79 | + | fn (arena: &mut Arena) allocate(size: u32, alignment: u32) -> *mut [u8] throws (AllocError) { |
|
| 80 | + | let ptr = try rawAllocate0(arena, size, alignment); |
|
| 81 | + | // The storage was just allocated, so no other slice names it. |
|
| 82 | + | unsafe { |
|
| 83 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 84 | + | } |
|
| 76 | 85 | } |
|
| 77 | 86 | } |
|
| 78 | 87 | ||
| 79 | 88 | /// Build a `Wide` element whose fields all depend on `v`. |
|
| 80 | 89 | fn wide(v: u64) -> Wide { |
test/tests/slice.zero-sized.rad
+13 -4
| 8 | 8 | } |
|
| 9 | 9 | ||
| 10 | 10 | /// Slice allocation interface. |
|
| 11 | 11 | trait Allocator { |
|
| 12 | 12 | /// Allocate storage. |
|
| 13 | - | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (Error); |
|
| 13 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Error); |
|
| 14 | 14 | } |
|
| 15 | 15 | ||
| 16 | 16 | /// Allocator that records calls. |
|
| 17 | 17 | record Reject: Copy { |
|
| 18 | 18 | /// Number of allocation calls. |
|
| 19 | 19 | calls: u32, |
|
| 20 | 20 | } |
|
| 21 | 21 | ||
| 22 | + | /// Allocate raw storage for `Reject`. |
|
| 23 | + | fn rawAllocate0(r: &mut Reject, size: u32, alignment: u32) -> *mut opaque throws (Error) { |
|
| 24 | + | set r.calls += 1; |
|
| 25 | + | throw Error::Full; |
|
| 26 | + | } |
|
| 27 | + | ||
| 22 | 28 | instance Allocator for Reject { |
|
| 23 | 29 | /// Count and reject allocation requests. |
|
| 24 | - | fn (r: &mut Reject) allocate(size: u32, alignment: u32) -> *mut opaque throws (Error) { |
|
| 25 | - | set r.calls += 1; |
|
| 26 | - | throw Error::Full; |
|
| 30 | + | fn (r: &mut Reject) allocate(size: u32, alignment: u32) -> *mut [u8] throws (Error) { |
|
| 31 | + | let ptr = try rawAllocate0(r, size, alignment); |
|
| 32 | + | // The storage was just allocated, so no other slice names it. |
|
| 33 | + | unsafe { |
|
| 34 | + | return @sliceOf(ptr as *mut u8, size); |
|
| 35 | + | } |
|
| 27 | 36 | } |
|
| 28 | 37 | } |
|
| 29 | 38 | ||
| 30 | 39 | /// Record evaluation of a zero-sized value. |
|
| 31 | 40 | fn nextValue(count: &mut u32) -> [u8; 0] { |
test/verify/expected
+20 -24
| 60 | 60 | prog.sieve Type |
|
| 61 | 61 | prog.symtab Type |
|
| 62 | 62 | prog.tokenizer Type |
|
| 63 | 63 | prog.vm Type |
|
| 64 | 64 | range.arithmetic Type |
|
| 65 | - | record.scalar Class |
|
| 65 | + | record.scalar View |
|
| 66 | 66 | ref.mut.ptr Type |
|
| 67 | 67 | slice.alloc.loop Type |
|
| 68 | - | slice.alloc.trait Slot |
|
| 69 | - | slice.append.errors Slot |
|
| 70 | - | slice.append.failure Slot |
|
| 71 | - | slice.append.limit Type |
|
| 68 | + | slice.alloc.short View |
|
| 69 | + | slice.alloc.trait View |
|
| 70 | + | slice.append.failure View |
|
| 71 | + | slice.append.limit View |
|
| 72 | 72 | slice.append.limit.trap Symbol |
|
| 73 | - | slice.append.trait Slot |
|
| 74 | - | slice.append Slot |
|
| 75 | - | slice.append.values Type |
|
| 73 | + | slice.append.owned View |
|
| 74 | + | slice.append.values View |
|
| 76 | 75 | slice.assign Type |
|
| 77 | 76 | slice.basic Slot |
|
| 78 | 77 | slice.cap Type |
|
| 79 | 78 | slice.construct.unsafe Slot |
|
| 80 | 79 | slice.delete Slot |
|
| 81 | 80 | slice.delete.bounds Slot |
|
| 82 | 81 | slice.delete.slots Slot |
|
| 83 | 82 | slice.empty.nonnull Data |
|
| 84 | - | slice.extend.borrowed Slot |
|
| 85 | - | slice.extend.bytes Slot |
|
| 86 | - | slice.extend.copy Slot |
|
| 87 | - | slice.extend.overflow Type |
|
| 88 | - | slice.extend.overflow.words Type |
|
| 89 | - | slice.failure Slot |
|
| 90 | - | slice.grow Type |
|
| 83 | + | slice.extend.borrowed View |
|
| 84 | + | slice.extend.copy View |
|
| 85 | + | slice.extend.overflow View |
|
| 86 | + | slice.extend.overflow.words View |
|
| 87 | + | slice.failure View |
|
| 88 | + | slice.grow View |
|
| 91 | 89 | slice.header.unsafe Slot |
|
| 92 | 90 | slice.of Type |
|
| 93 | 91 | slice.ptr.unsafe Slot |
|
| 94 | - | slice.range.capacity Slot |
|
| 92 | + | slice.range.capacity View |
|
| 95 | 93 | slice.range.dynamic Type |
|
| 96 | 94 | slice.reserve.limit Symbol |
|
| 97 | - | slice.reserve.result Slot |
|
| 98 | - | slice.reserve Type |
|
| 99 | - | slice.resize.extend Slot |
|
| 100 | - | slice.resize.fill.snapshot Slot |
|
| 101 | - | slice.sizes Slot |
|
| 95 | + | slice.reserve View |
|
| 96 | + | slice.resize.fill.snapshot View |
|
| 97 | + | slice.sizes View |
|
| 102 | 98 | slice.subslice Type |
|
| 103 | - | slice.take.sizes Slot |
|
| 104 | - | slice.take Slot |
|
| 105 | - | slice.zero-sized Slot |
|
| 99 | + | slice.take.sizes View |
|
| 100 | + | slice.take View |
|
| 101 | + | slice.zero-sized View |
|
| 106 | 102 | slice.zero-sized.limit Symbol |
|
| 107 | 103 | static.empty.slice.optional Data |
|
| 108 | 104 | static.slice.empty Data |
|
| 109 | 105 | static.slice.empty.record.table Data |
|
| 110 | 106 | static.slice.empty.table Data |