std: Add checked integer primitives
b3a785058cee5ef6da0105aaf3f73d49f95fa2ee2bdad3102c3bd243a91c1c05
Add little-endian byte-slice loads and stores with one range check. Support rotations, zero counts, population counts and byte swaps. Select Zbb instructions and support every byte alignment on RV64. Check byte-slice permissions in RIL and cover integer widths, bounds, alignment and bit semantics in executable and verifier tests. Assisted-by: Codex:gpt-6 Change-Id: Ia2a1da628fc3cf1616605d97e454268dac7f59af
1 parent
1f0c6397
lib/std.rad
+1 -0
| 8 | 8 | export mod fs; |
|
| 9 | 9 | export mod process; |
|
| 10 | 10 | export mod arch; |
|
| 11 | 11 | export mod fmt; |
|
| 12 | 12 | export mod mem; |
|
| 13 | + | export mod bits; |
|
| 13 | 14 | export mod vec; |
|
| 14 | 15 | export mod intrinsics; |
|
| 15 | 16 | ||
| 16 | 17 | // Test modules. |
|
| 17 | 18 | @test export mod testing; |
lib/std/arch/rv64/isel.rad
+138 -7
| 25 | 25 | //! |
|
| 26 | 26 | //! loadVal(rd, val) -> Reg |
|
| 27 | 27 | //! Force an [`il::Val`] into a specific register `rd`. Built on [`resolveVal`] + [`emitMv`]. |
|
| 28 | 28 | //! Used when the instruction requires the value in `rd` (e.g. `sub rd, rd, rs2`). |
|
| 29 | 29 | ||
| 30 | - | use std::mem; |
|
| 31 | 30 | use std::lang::alloc; |
|
| 32 | - | use std::mem::heap; |
|
| 33 | - | use std::lang::il; |
|
| 34 | - | use std::lang::types; |
|
| 35 | 31 | use std::lang::gen; |
|
| 36 | - | use std::lang::gen::regalloc; |
|
| 37 | 32 | use std::lang::gen::labels; |
|
| 33 | + | use std::lang::gen::regalloc; |
|
| 34 | + | use std::lang::il; |
|
| 35 | + | use std::lang::types; |
|
| 36 | + | use std::mem; |
|
| 37 | + | use std::mem::heap; |
|
| 38 | 38 | ||
| 39 | 39 | use super::atomics; |
|
| 40 | - | use super::encode; |
|
| 41 | 40 | use super::emit; |
|
| 41 | + | use super::encode; |
|
| 42 | 42 | ||
| 43 | 43 | /// Largest object size representable as a signed address offset. |
|
| 44 | 44 | constant MAX_OBJECT_SIZE: u32 = 0x7fffffff; |
|
| 45 | 45 | ||
| 46 | 46 | /////////////// |
| 509 | 509 | case il::Instr::UnOp { op, typ, dst, a } => { |
|
| 510 | 510 | if op == il::UnOp::Not { |
|
| 511 | 511 | update(x, dst, Extension { sign: valueExtension(&x.exts[..], a).sign, zero: 64 }); |
|
| 512 | 512 | return true; |
|
| 513 | 513 | } |
|
| 514 | - | update(x, dst, WORD if typ == il::Type::W32 else UNKNOWN); |
|
| 514 | + | if op == il::UnOp::ByteSwap { |
|
| 515 | + | update(x, dst, narrowExtension(typ, false)); |
|
| 516 | + | } else if op == il::UnOp::LeadingZeros |
|
| 517 | + | or op == il::UnOp::TrailingZeros |
|
| 518 | + | or op == il::UnOp::PopCount |
|
| 519 | + | { |
|
| 520 | + | update(x, dst, Extension { sign: 8, zero: 8 }); |
|
| 521 | + | } else { |
|
| 522 | + | update(x, dst, WORD if typ == il::Type::W32 else UNKNOWN); |
|
| 523 | + | } |
|
| 515 | 524 | } |
|
| 525 | + | case il::Instr::BytesLoad { typ, dst, .. } => |
|
| 526 | + | update(x, dst, narrowExtension(typ, false)), |
|
| 516 | 527 | case il::Instr::Copy { dst, val } => { |
|
| 517 | 528 | update(x, dst, valueExtension(&x.exts[..], val)); |
|
| 518 | 529 | return true; |
|
| 519 | 530 | } |
|
| 520 | 531 | case il::Instr::Load { typ, dst, .. } => |
| 1985 | 1996 | let ri = try resolveIndex(s, e, index); |
|
| 1986 | 1997 | try emit::loadImm(e, super::ADDR_SCRATCH, count as i64); |
|
| 1987 | 1998 | try emitTrapUnlessLtu(e, ri, super::ADDR_SCRATCH); |
|
| 1988 | 1999 | try emitAddress(e, rd, rbase, ri, size, super::ADDR_SCRATCH, super::SCRATCH3); |
|
| 1989 | 2000 | } |
|
| 2001 | + | case il::Instr::BytesLoad { typ, dst, slice, offset } => { |
|
| 2002 | + | try byteAddress(s, e, slice, offset, typ); |
|
| 2003 | + | let rd = getDstReg(s, dst, super::SCRATCH1); |
|
| 2004 | + | try loadBytes(e, rd, typ); |
|
| 2005 | + | } |
|
| 2006 | + | case il::Instr::BytesStore { typ, slice, offset, value } => { |
|
| 2007 | + | // T3 keeps the value through address checks and byte stores. |
|
| 2008 | + | let rs = try resolveVal(s, e, super::SCRATCH3, value); |
|
| 2009 | + | try emitMv(s, e, super::SCRATCH3, rs); |
|
| 2010 | + | try byteAddress(s, e, slice, offset, typ); |
|
| 2011 | + | try storeBytes(e, typ); |
|
| 2012 | + | } |
|
| 1990 | 2013 | case il::Instr::SliceElem { dst, slice, index, layout } => { |
|
| 1991 | 2014 | let size = layouts[layout].size; |
|
| 1992 | 2015 | let rd = getDstReg(s, dst, super::SCRATCH1); |
|
| 1993 | 2016 | let rslice = try getSrcReg(s, e, slice, super::SCRATCH2); |
|
| 1994 | 2017 | // Slices are shorter than 2^31 elements, so a sign-extended index that |
| 2524 | 2547 | rs1: gen::Reg, |
|
| 2525 | 2548 | a: il::Val, |
|
| 2526 | 2549 | b: il::Val |
|
| 2527 | 2550 | ) throws (super::Error) { |
|
| 2528 | 2551 | match op { |
|
| 2552 | + | case il::BinOp::RotL, il::BinOp::RotR => { |
|
| 2553 | + | if let case il::Val::Imm(count) = b { |
|
| 2554 | + | let bits = il::typeSize(typ) * 8; |
|
| 2555 | + | let shift = count as u64 & (bits - 1) as u64; |
|
| 2556 | + | let right = (bits as u64 - shift) & (bits - 1) as u64 |
|
| 2557 | + | if op == il::BinOp::RotL |
|
| 2558 | + | else shift; |
|
| 2559 | + | if typ == il::Type::W32 { |
|
| 2560 | + | try emit::emit(e, encode::roriw(rd, rs1, right as i32)); |
|
| 2561 | + | } else { |
|
| 2562 | + | try emit::emit(e, encode::rori(rd, rs1, right as i32)); |
|
| 2563 | + | } |
|
| 2564 | + | return; |
|
| 2565 | + | } |
|
| 2566 | + | let rs2 = try resolveVal(s, e, super::SCRATCH2, b); |
|
| 2567 | + | if typ == il::Type::W32 { |
|
| 2568 | + | let word = encode::rolw(rd, rs1, rs2) |
|
| 2569 | + | if op == il::BinOp::RotL |
|
| 2570 | + | else encode::rorw(rd, rs1, rs2); |
|
| 2571 | + | try emit::emit(e, word); |
|
| 2572 | + | } else { |
|
| 2573 | + | let word = encode::rol(rd, rs1, rs2) |
|
| 2574 | + | if op == il::BinOp::RotL |
|
| 2575 | + | else encode::ror(rd, rs1, rs2); |
|
| 2576 | + | try emit::emit(e, word); |
|
| 2577 | + | } |
|
| 2578 | + | } |
|
| 2529 | 2579 | case il::BinOp::Add => { |
|
| 2530 | 2580 | if typ == il::Type::W32 { |
|
| 2531 | 2581 | // Inline W32 ADD with immediate optimization. |
|
| 2532 | 2582 | if let case il::Val::Imm(imm) = b { |
|
| 2533 | 2583 | if encode::isSmallImm64(imm) { |
| 2690 | 2740 | /// Select a unary ALU operation. |
|
| 2691 | 2741 | fn selectAluUnOp( |
|
| 2692 | 2742 | s: &mut Selector, e: &mut emit::Emitter, op: il::UnOp, typ: il::Type, rd: gen::Reg, rs: gen::Reg |
|
| 2693 | 2743 | ) throws (super::Error) { |
|
| 2694 | 2744 | match op { |
|
| 2745 | + | case il::UnOp::LeadingZeros => { |
|
| 2746 | + | let word = encode::clzw(rd, rs) |
|
| 2747 | + | if typ == il::Type::W32 |
|
| 2748 | + | else encode::clz(rd, rs); |
|
| 2749 | + | try emit::emit(e, word); |
|
| 2750 | + | } |
|
| 2751 | + | case il::UnOp::TrailingZeros => { |
|
| 2752 | + | let word = encode::ctzw(rd, rs) |
|
| 2753 | + | if typ == il::Type::W32 |
|
| 2754 | + | else encode::ctz(rd, rs); |
|
| 2755 | + | try emit::emit(e, word); |
|
| 2756 | + | } |
|
| 2757 | + | case il::UnOp::PopCount => { |
|
| 2758 | + | let word = encode::cpopw(rd, rs) |
|
| 2759 | + | if typ == il::Type::W32 |
|
| 2760 | + | else encode::cpop(rd, rs); |
|
| 2761 | + | try emit::emit(e, word); |
|
| 2762 | + | } |
|
| 2763 | + | case il::UnOp::ByteSwap => { |
|
| 2764 | + | try emit::emit(e, encode::rev8(rd, rs)); |
|
| 2765 | + | if typ <> il::Type::W64 { |
|
| 2766 | + | try emit::emit(e, encode::srli(rd, rd, ((8 - il::typeSize(typ)) * 8) as i32)); |
|
| 2767 | + | } |
|
| 2768 | + | } |
|
| 2695 | 2769 | case il::UnOp::Neg => { |
|
| 2696 | 2770 | if typ == il::Type::W32 { |
|
| 2697 | 2771 | try emit::emit(e, encode::subw(rd, super::ZERO, rs)); |
|
| 2698 | 2772 | } else { |
|
| 2699 | 2773 | try emit::emit(e, encode::neg(rd, rs)); |
| 3243 | 3317 | emit::patch(e, branch, encode::blt(super::A0, super::ZERO, skip)); |
|
| 3244 | 3318 | ||
| 3245 | 3319 | let rd = getDstReg(s, err, super::SCRATCH1); |
|
| 3246 | 3320 | try emitMv(s, e, rd, super::SCRATCH1); |
|
| 3247 | 3321 | } |
|
| 3322 | + | ||
| 3323 | + | /// Compute a checked byte address in ADDR_SCRATCH. T3 is preserved. |
|
| 3324 | + | fn byteAddress( |
|
| 3325 | + | s: &mut Selector, e: &mut emit::Emitter, slice: il::Reg, offset: il::Val, typ: il::Type |
|
| 3326 | + | ) throws (super::Error) { |
|
| 3327 | + | let rslot = try getSrcReg(s, e, slice, super::SCRATCH2); |
|
| 3328 | + | try emitMv(s, e, super::SCRATCH2, rslot); |
|
| 3329 | + | try loadU32(s, e, super::SCRATCH1, super::SCRATCH1, offset); |
|
| 3330 | + | // The sum fits in 33 bits, so an offset near U32_MAX cannot wrap. |
|
| 3331 | + | try emit::emit(e, encode::addi( |
|
| 3332 | + | super::SCRATCH1, super::SCRATCH1, il::typeSize(typ) as i32 |
|
| 3333 | + | )); |
|
| 3334 | + | try emit::emitLoad(e, super::ADDR_SCRATCH, super::SCRATCH2, SLICE_LEN_OFFSET, il::Type::W32); |
|
| 3335 | + | try emitTrapUnlessLeu(e, super::SCRATCH1, super::ADDR_SCRATCH); |
|
| 3336 | + | try emit::emitLd(e, super::ADDR_SCRATCH, super::SCRATCH2, SLICE_PTR_OFFSET); |
|
| 3337 | + | try emit::emit(e, encode::add(super::ADDR_SCRATCH, super::ADDR_SCRATCH, super::SCRATCH1)); |
|
| 3338 | + | try emit::emit(e, encode::addi( |
|
| 3339 | + | super::ADDR_SCRATCH, super::ADDR_SCRATCH, -(il::typeSize(typ) as i32) |
|
| 3340 | + | )); |
|
| 3341 | + | } |
|
| 3342 | + | ||
| 3343 | + | /// Load little-endian bytes at ADDR_SCRATCH without an alignment fault. |
|
| 3344 | + | fn loadBytes(e: &mut emit::Emitter, rd: gen::Reg, typ: il::Type) throws (super::Error) { |
|
| 3345 | + | let size = il::typeSize(typ) as i32; |
|
| 3346 | + | if size == 1 { |
|
| 3347 | + | try emit::emitLoad(e, rd, super::ADDR_SCRATCH, 0, typ); |
|
| 3348 | + | return; |
|
| 3349 | + | } |
|
| 3350 | + | try emit::emit(e, encode::andi(super::SCRATCH2, super::ADDR_SCRATCH, size - 1)); |
|
| 3351 | + | try emit::emit(e, encode::bne(super::SCRATCH2, super::ZERO, 3 * super::INSTR_SIZE)); |
|
| 3352 | + | try emit::emitLoad(e, rd, super::ADDR_SCRATCH, 0, typ); |
|
| 3353 | + | try emit::emit(e, encode::jal(super::ZERO, (3 * size - 1) * super::INSTR_SIZE)); |
|
| 3354 | + | try emit::emit(e, encode::lbu(rd, super::ADDR_SCRATCH, 0)); |
|
| 3355 | + | for i in 1 .. size { |
|
| 3356 | + | try emit::emit(e, encode::lbu(super::SCRATCH2, super::ADDR_SCRATCH, i)); |
|
| 3357 | + | try emit::emit(e, encode::slli(super::SCRATCH2, super::SCRATCH2, i * 8)); |
|
| 3358 | + | try emit::emit(e, encode::or_(rd, rd, super::SCRATCH2)); |
|
| 3359 | + | } |
|
| 3360 | + | } |
|
| 3361 | + | ||
| 3362 | + | /// Store the low bytes of T3 at ADDR_SCRATCH without an alignment fault. |
|
| 3363 | + | fn storeBytes(e: &mut emit::Emitter, typ: il::Type) throws (super::Error) { |
|
| 3364 | + | let size = il::typeSize(typ) as i32; |
|
| 3365 | + | if size == 1 { |
|
| 3366 | + | try emit::emitStore(e, super::SCRATCH3, super::ADDR_SCRATCH, 0, typ); |
|
| 3367 | + | return; |
|
| 3368 | + | } |
|
| 3369 | + | try emit::emit(e, encode::andi(super::SCRATCH2, super::ADDR_SCRATCH, size - 1)); |
|
| 3370 | + | try emit::emit(e, encode::bne(super::SCRATCH2, super::ZERO, 3 * super::INSTR_SIZE)); |
|
| 3371 | + | try emit::emitStore(e, super::SCRATCH3, super::ADDR_SCRATCH, 0, typ); |
|
| 3372 | + | try emit::emit(e, encode::jal(super::ZERO, 2 * size * super::INSTR_SIZE)); |
|
| 3373 | + | try emit::emit(e, encode::sb(super::SCRATCH3, super::ADDR_SCRATCH, 0)); |
|
| 3374 | + | for i in 1 .. size { |
|
| 3375 | + | try emit::emit(e, encode::srli(super::SCRATCH2, super::SCRATCH3, i * 8)); |
|
| 3376 | + | try emit::emit(e, encode::sb(super::SCRATCH2, super::ADDR_SCRATCH, i)); |
|
| 3377 | + | } |
|
| 3378 | + | } |
lib/std/arch/rv64/tests.rad
+65 -7
| 4 | 4 | //! by comparing against known-good values. |
|
| 5 | 5 | ||
| 6 | 6 | @test mod bounds; |
|
| 7 | 7 | @test mod atomics; |
|
| 8 | 8 | ||
| 9 | - | use std::testing; |
|
| 9 | + | use std::arch::rv64; |
|
| 10 | + | use std::collections::dict; |
|
| 10 | 11 | use std::lang::alloc; |
|
| 11 | - | use std::mem::heap; |
|
| 12 | - | use std::lang::il; |
|
| 12 | + | use std::lang::gen::bitset; |
|
| 13 | 13 | use std::lang::gen::data; |
|
| 14 | 14 | use std::lang::gen::labels; |
|
| 15 | - | use std::lang::gen::bitset; |
|
| 16 | 15 | use std::lang::gen::regalloc; |
|
| 17 | 16 | use std::lang::gen::regalloc::liveness; |
|
| 18 | - | use std::arch::rv64; |
|
| 19 | - | use std::collections::dict; |
|
| 17 | + | use std::lang::il; |
|
| 18 | + | use std::mem::heap; |
|
| 19 | + | use std::testing; |
|
| 20 | 20 | ||
| 21 | + | use super::asm; |
|
| 21 | 22 | use super::encode; |
|
| 22 | 23 | use super::isel; |
|
| 23 | - | use super::asm; |
|
| 24 | 24 | ||
| 25 | 25 | /// Reusable emitter storage. |
|
| 26 | 26 | static ASSEMBLY_ARENA_STORAGE: [u8; 16777216] = [0; 16777216]; |
|
| 27 | 27 | static ASSEMBLY_TEXT_STORAGE: [u32; 2] = [0; 2]; |
|
| 28 | 28 |
| 1873 | 1873 | let extended = il::Instr::Zext { typ: il::Type::W32, dst: il::Reg { n: 2 }, val: reg(0) }; |
|
| 1874 | 1874 | assert containsInstruction( |
|
| 1875 | 1875 | try generateBody(&[], nil, &[extended, sum]), 0x0800003b, 0xfe00707f |
|
| 1876 | 1876 | ); |
|
| 1877 | 1877 | } |
|
| 1878 | + | ||
| 1879 | + | /// Integer primitives select the word and doubleword bit instructions. |
|
| 1880 | + | @test unsafe fn testIntegerPrimitiveSelection() throws (testing::TestError) { |
|
| 1881 | + | for typ in &[il::Type::W32, il::Type::W64] { |
|
| 1882 | + | let word = typ == il::Type::W32; |
|
| 1883 | + | for op in &[il::BinOp::RotL, il::BinOp::RotR] { |
|
| 1884 | + | let left = op == il::BinOp::RotL; |
|
| 1885 | + | let bits: u32 = (0x6000103b if left else 0x6000503b) |
|
| 1886 | + | if word |
|
| 1887 | + | else 0x60001033 if left else 0x60005033; |
|
| 1888 | + | let rotate = il::Instr::BinOp { |
|
| 1889 | + | op, typ, dst: il::Reg { n: 2 }, a: reg(0), b: reg(1) |
|
| 1890 | + | }; |
|
| 1891 | + | assert containsInstruction(try generateBody(&[], nil, &[rotate]), bits, 0xfe00707f); |
|
| 1892 | + | let immediate = il::Instr::BinOp { |
|
| 1893 | + | op, typ, dst: il::Reg { n: 2 }, a: reg(0), b: il::Val::Imm(4) |
|
| 1894 | + | }; |
|
| 1895 | + | let count: u32 = (28 if word else 60) if left else 4; |
|
| 1896 | + | let encoded = (0x6000501b if word else 0x60005013) | (count << 20); |
|
| 1897 | + | assert containsInstruction( |
|
| 1898 | + | try generateBody(&[], nil, &[immediate]), encoded, 0xfff0707f |
|
| 1899 | + | ); |
|
| 1900 | + | } |
|
| 1901 | + | for op, index in &[ |
|
| 1902 | + | il::UnOp::LeadingZeros, il::UnOp::TrailingZeros, il::UnOp::PopCount, |
|
| 1903 | + | il::UnOp::ByteSwap, |
|
| 1904 | + | ] { |
|
| 1905 | + | let bits: u32 = 0x6b805013 |
|
| 1906 | + | if op == il::UnOp::ByteSwap |
|
| 1907 | + | else (0x6000101b if word else 0x60001013) | (index << 20); |
|
| 1908 | + | let unary = il::Instr::UnOp { op, typ, dst: il::Reg { n: 2 }, a: reg(0) }; |
|
| 1909 | + | assert containsInstruction(try generateBody(&[], nil, &[unary]), bits, 0xfff0707f); |
|
| 1910 | + | } |
|
| 1911 | + | } |
|
| 1912 | + | } |
|
| 1913 | + | ||
| 1914 | + | /// Byte offsets use Zba zero extension and one range check at every width. |
|
| 1915 | + | @test unsafe fn testBytePrimitiveSelection() throws (testing::TestError) { |
|
| 1916 | + | for typ in &[il::Type::W16, il::Type::W32, il::Type::W64] { |
|
| 1917 | + | let load = il::Instr::BytesLoad { |
|
| 1918 | + | typ, dst: il::Reg { n: 2 }, slice: il::Reg { n: 0 }, offset: reg(1) |
|
| 1919 | + | }; |
|
| 1920 | + | let store = il::Instr::BytesStore { |
|
| 1921 | + | typ, slice: il::Reg { n: 0 }, offset: reg(1), value: il::Val::Imm(0) |
|
| 1922 | + | }; |
|
| 1923 | + | for access in &[load, store] { |
|
| 1924 | + | let code = try generateBody(&[], nil, &[access]); |
|
| 1925 | + | assert containsInstruction(code, 0x0800003b, 0xfff0707f); |
|
| 1926 | + | let mut checks: u32 = 0; |
|
| 1927 | + | for instruction in code { |
|
| 1928 | + | if (instruction & 0x707f) == 0x7063 { |
|
| 1929 | + | set checks += 1; |
|
| 1930 | + | } |
|
| 1931 | + | } |
|
| 1932 | + | assert checks == 1; |
|
| 1933 | + | } |
|
| 1934 | + | } |
|
| 1935 | + | } |
lib/std/bits.rad
added
+42 -0
| 1 | + | //! Fixed-width bit operations. Rotation counts wrap modulo the value width. |
|
| 2 | + | ||
| 3 | + | @test mod tests; |
|
| 4 | + | ||
| 5 | + | /// Rotate a 32-bit value to the left. The count is reduced modulo 32. |
|
| 6 | + | @intrinsic export fn rotateLeft32(value: u32, count: u32) -> u32; |
|
| 7 | + | ||
| 8 | + | /// Rotate a 32-bit value to the right. The count is reduced modulo 32. |
|
| 9 | + | @intrinsic export fn rotateRight32(value: u32, count: u32) -> u32; |
|
| 10 | + | ||
| 11 | + | /// Count high zero bits in a 32-bit value. Return 32 for zero. |
|
| 12 | + | @intrinsic export fn leadingZeros32(value: u32) -> u32; |
|
| 13 | + | ||
| 14 | + | /// Count low zero bits in a 32-bit value. Return 32 for zero. |
|
| 15 | + | @intrinsic export fn trailingZeros32(value: u32) -> u32; |
|
| 16 | + | ||
| 17 | + | /// Count the set bits in a 32-bit value. |
|
| 18 | + | @intrinsic export fn popCount32(value: u32) -> u32; |
|
| 19 | + | ||
| 20 | + | /// Reverse the byte order of a 32-bit value. |
|
| 21 | + | @intrinsic export fn byteSwap32(value: u32) -> u32; |
|
| 22 | + | ||
| 23 | + | /// Rotate a 64-bit value to the left. The count is reduced modulo 64. |
|
| 24 | + | @intrinsic export fn rotateLeft64(value: u64, count: u32) -> u64; |
|
| 25 | + | ||
| 26 | + | /// Rotate a 64-bit value to the right. The count is reduced modulo 64. |
|
| 27 | + | @intrinsic export fn rotateRight64(value: u64, count: u32) -> u64; |
|
| 28 | + | ||
| 29 | + | /// Count high zero bits in a 64-bit value. Return 64 for zero. |
|
| 30 | + | @intrinsic export fn leadingZeros64(value: u64) -> u32; |
|
| 31 | + | ||
| 32 | + | /// Count low zero bits in a 64-bit value. Return 64 for zero. |
|
| 33 | + | @intrinsic export fn trailingZeros64(value: u64) -> u32; |
|
| 34 | + | ||
| 35 | + | /// Count the set bits in a 64-bit value. |
|
| 36 | + | @intrinsic export fn popCount64(value: u64) -> u32; |
|
| 37 | + | ||
| 38 | + | /// Reverse the byte order of a 64-bit value. |
|
| 39 | + | @intrinsic export fn byteSwap64(value: u64) -> u64; |
|
| 40 | + | ||
| 41 | + | /// Reverse the byte order of a 16-bit value. |
|
| 42 | + | @intrinsic export fn byteSwap16(value: u16) -> u16; |
lib/std/bits/tests.rad
added
+80 -0
| 1 | + | //! Tests for fixed-width bit operations. |
|
| 2 | + | ||
| 3 | + | use std::bits; |
|
| 4 | + | use std::mem; |
|
| 5 | + | use std::testing; |
|
| 6 | + | ||
| 7 | + | /// Check both word widths against bit-by-bit counts and wide rotations. |
|
| 8 | + | @test fn operations() throws (testing::TestError) { |
|
| 9 | + | for i in 0 .. 130 { |
|
| 10 | + | let value: u64 = 1 as u64 << (i % 64) as u64 |
|
| 11 | + | if i < 64 |
|
| 12 | + | else ~(1 as u64 << (i % 64) as u64); |
|
| 13 | + | let x = value as u32; |
|
| 14 | + | let mut ones32: u32 = 0; |
|
| 15 | + | let mut ones64: u32 = 0; |
|
| 16 | + | let mut leading32: u32 = 32; |
|
| 17 | + | let mut leading64: u32 = 64; |
|
| 18 | + | let mut trailing32: u32 = 32; |
|
| 19 | + | let mut trailing64: u32 = 64; |
|
| 20 | + | let mut swap32: u32 = 0; |
|
| 21 | + | let mut swap64: u64 = 0; |
|
| 22 | + | for bit in 0 .. 64 { |
|
| 23 | + | if (value & (1 as u64 << bit as u64)) <> 0 { |
|
| 24 | + | set ones64 += 1; |
|
| 25 | + | set leading64 = 63 - bit; |
|
| 26 | + | if trailing64 == 64 { |
|
| 27 | + | set trailing64 = bit; |
|
| 28 | + | } |
|
| 29 | + | } |
|
| 30 | + | if bit < 32 and (x & (1 as u32 << bit)) <> 0 { |
|
| 31 | + | set ones32 += 1; |
|
| 32 | + | set leading32 = 31 - bit; |
|
| 33 | + | if trailing32 == 32 { |
|
| 34 | + | set trailing32 = bit; |
|
| 35 | + | } |
|
| 36 | + | } |
|
| 37 | + | } |
|
| 38 | + | for byte in 0 .. 8 { |
|
| 39 | + | set swap64 = (swap64 << 8) | ((value >> (byte * 8) as u64) & 0xff); |
|
| 40 | + | if byte < 4 { |
|
| 41 | + | set swap32 = (swap32 << 8) | ((x >> (byte * 8)) & 0xff); |
|
| 42 | + | } |
|
| 43 | + | } |
|
| 44 | + | assert bits::leadingZeros32(x) == leading32; |
|
| 45 | + | assert bits::leadingZeros64(value) == leading64; |
|
| 46 | + | assert bits::trailingZeros32(x) == trailing32; |
|
| 47 | + | assert bits::trailingZeros64(value) == trailing64; |
|
| 48 | + | assert bits::popCount32(x) == ones32; |
|
| 49 | + | assert bits::popCount64(value) == ones64; |
|
| 50 | + | assert bits::byteSwap16(x as u16) as u32 == (swap32 >> 16); |
|
| 51 | + | assert bits::byteSwap32(x) as u64 == swap32 as u64; |
|
| 52 | + | assert bits::byteSwap64(value) == swap64; |
|
| 53 | + | for count in 0 .. 130 { |
|
| 54 | + | let k32: u32 = count % 32; |
|
| 55 | + | let k64 = (count % 64) as u64; |
|
| 56 | + | let left32 = |
|
| 57 | + | ((x as u64 << k32 as u64) |
|
| 58 | + | | (x as u64 >> (32 - k32) as u64)) as u32; |
|
| 59 | + | let right32 = |
|
| 60 | + | ((x as u64 >> k32 as u64) |
|
| 61 | + | | (x as u64 << (32 - k32) as u64)) as u32; |
|
| 62 | + | let left64 = value if k64 == 0 else (value << k64) | (value >> (64 - k64)); |
|
| 63 | + | let right64 = value if k64 == 0 else (value >> k64) | (value << (64 - k64)); |
|
| 64 | + | assert bits::rotateLeft32(x, count) == left32; |
|
| 65 | + | assert bits::rotateRight32(x, count) == right32; |
|
| 66 | + | assert bits::rotateLeft64(value, count) == left64; |
|
| 67 | + | assert bits::rotateRight64(value, count) == right64; |
|
| 68 | + | } |
|
| 69 | + | assert bits::rotateLeft32(x, mem::U32_MAX) == bits::rotateRight32(x, 1); |
|
| 70 | + | assert bits::rotateLeft64(value, mem::U32_MAX) == bits::rotateRight64(value, 1); |
|
| 71 | + | } |
|
| 72 | + | assert bits::leadingZeros32(0) == 32; |
|
| 73 | + | assert bits::leadingZeros64(0) == 64; |
|
| 74 | + | assert bits::trailingZeros32(0) == 32; |
|
| 75 | + | assert bits::trailingZeros64(0) == 64; |
|
| 76 | + | assert bits::popCount32(0) == 0; |
|
| 77 | + | assert bits::popCount64(0) == 0; |
|
| 78 | + | assert bits::popCount32(mem::U32_MAX) == 32; |
|
| 79 | + | assert bits::popCount64(mem::U64_MAX) == 64; |
|
| 80 | + | } |
lib/std/lang/il.rad
+44 -0
| 243 | 243 | Add, Sub, Mul, Sdiv, Udiv, Srem, Urem, |
|
| 244 | 244 | // Comparison. |
|
| 245 | 245 | Eq, Ne, Slt, Sge, Ult, Uge, |
|
| 246 | 246 | // Bitwise. |
|
| 247 | 247 | And, Or, Xor, Shl, Sshr, Ushr, |
|
| 248 | + | /// Rotate the low bits by a count modulo the width. |
|
| 249 | + | RotL, RotR, |
|
| 248 | 250 | } |
|
| 249 | 251 | ||
| 250 | 252 | /// Unary ALU operation kind. |
|
| 251 | 253 | export union UnOp: Copy { |
|
| 252 | 254 | Neg, Not, |
|
| 255 | + | /// Count zero bits from the high end. Zero returns the width. |
|
| 256 | + | LeadingZeros, |
|
| 257 | + | /// Count zero bits from the low end. Zero returns the width. |
|
| 258 | + | TrailingZeros, |
|
| 259 | + | /// Count set bits. |
|
| 260 | + | PopCount, |
|
| 261 | + | /// Reverse the order of bytes in the value. |
|
| 262 | + | ByteSwap, |
|
| 253 | 263 | } |
|
| 254 | 264 | ||
| 255 | 265 | ////////////////// |
|
| 256 | 266 | // Instructions // |
|
| 257 | 267 | ////////////////// |
| 381 | 391 | /// Element index. |
|
| 382 | 392 | index: Val, |
|
| 383 | 393 | /// Index of the element layout in `Program.layouts`. |
|
| 384 | 394 | layout: u32, |
|
| 385 | 395 | }, |
|
| 396 | + | /// Read little-endian bytes: `bytes.load <type> %dst %slice <offset>;`. |
|
| 397 | + | /// Traps unless the byte range fits in the slice. The offset is unsigned W32. |
|
| 398 | + | BytesLoad { |
|
| 399 | + | /// Width of the unsigned result. |
|
| 400 | + | typ: Type, |
|
| 401 | + | /// Register that receives the value. |
|
| 402 | + | dst: Reg, |
|
| 403 | + | /// Address of a plain byte slice slot. |
|
| 404 | + | slice: Reg, |
|
| 405 | + | /// Byte offset in the slice. |
|
| 406 | + | offset: Val, |
|
| 407 | + | }, |
|
| 408 | + | /// Write little-endian bytes: `bytes.store <type> %slice <offset> <value>;`. |
|
| 409 | + | /// Traps before writing unless the byte range fits in the writable byte slice. |
|
| 410 | + | BytesStore { |
|
| 411 | + | /// Width of the stored value. |
|
| 412 | + | typ: Type, |
|
| 413 | + | /// Address of a writable plain byte slice slot. |
|
| 414 | + | slice: Reg, |
|
| 415 | + | /// Unsigned W32 byte offset in the slice. |
|
| 416 | + | offset: Val, |
|
| 417 | + | /// Value whose low bits are stored. |
|
| 418 | + | value: Val, |
|
| 419 | + | }, |
|
| 386 | 420 | /// Build a slice of an array: `slice.new %dst <base> $layout <count> <start> <end>;`. |
|
| 387 | 421 | /// |
|
| 388 | 422 | /// `dst` addresses a slice object. `base` addresses `count` objects of `layout`. |
|
| 389 | 423 | /// The instruction traps unless `start <= end <= count`, and then writes the data |
|
| 390 | 424 | /// pointer `base + start * size`, the length `end - start`, and the capacity |
| 1023 | 1057 | case Instr::Sload { dst, .. } => return dst, |
|
| 1024 | 1058 | case Instr::Copy { dst, .. } => return dst, |
|
| 1025 | 1059 | case Instr::Field { dst, .. } => return dst, |
|
| 1026 | 1060 | case Instr::Elem { dst, .. } => return dst, |
|
| 1027 | 1061 | case Instr::SliceElem { dst, .. } => return dst, |
|
| 1062 | + | case Instr::BytesLoad { dst, .. } => return dst, |
|
| 1028 | 1063 | case Instr::SliceGrow { ok, .. } => return ok, |
|
| 1029 | 1064 | case Instr::LoadPtr { dst, .. } => return dst, |
|
| 1030 | 1065 | case Instr::BinOp { dst, .. } => return dst, |
|
| 1031 | 1066 | case Instr::UnOp { dst, .. } => return dst, |
|
| 1032 | 1067 | case Instr::Zext { dst, .. } => return dst, |
| 1146 | 1181 | } |
|
| 1147 | 1182 | case Instr::SliceElem { slice, index, .. } => { |
|
| 1148 | 1183 | visitor.visit(slice); |
|
| 1149 | 1184 | withReg(index, visitor); |
|
| 1150 | 1185 | } |
|
| 1186 | + | case Instr::BytesLoad { slice, offset, .. } => { |
|
| 1187 | + | visitor.visit(slice); |
|
| 1188 | + | withReg(offset, visitor); |
|
| 1189 | + | } |
|
| 1190 | + | case Instr::BytesStore { slice, offset, value, .. } => { |
|
| 1191 | + | visitor.visit(slice); |
|
| 1192 | + | withReg(offset, visitor); |
|
| 1193 | + | withReg(value, visitor); |
|
| 1194 | + | } |
|
| 1151 | 1195 | case Instr::SliceNew { dst, base, start, end, .. } => { |
|
| 1152 | 1196 | visitor.visit(dst); |
|
| 1153 | 1197 | withReg(base, visitor); |
|
| 1154 | 1198 | withReg(start, visitor); |
|
| 1155 | 1199 | withReg(end, visitor); |
lib/std/lang/il/binary.rad
+19 -0
| 188 | 188 | /// Wire tag for instruction UnionGet. |
|
| 189 | 189 | export constant INSTR_UNION_GET: u8 = 38; |
|
| 190 | 190 | /// Wire tag for instruction UnionSet. |
|
| 191 | 191 | export constant INSTR_UNION_SET: u8 = 39; |
|
| 192 | 192 | ||
| 193 | + | /// Wire tag for checked byte loads. |
|
| 194 | + | export constant INSTR_BYTES_LOAD: u8 = 40; |
|
| 195 | + | /// Wire tag for checked byte stores. |
|
| 196 | + | export constant INSTR_BYTES_STORE: u8 = 41; |
|
| 197 | + | ||
| 193 | 198 | /// Wire tag for data Val. |
|
| 194 | 199 | export constant DATA_VAL: u8 = 0; |
|
| 195 | 200 | /// Wire tag for data Sym. |
|
| 196 | 201 | export constant DATA_SYM: u8 = 1; |
|
| 197 | 202 | /// Wire tag for data Fn. |
| 238 | 243 | /// Wire tag for binary operation Sshr. |
|
| 239 | 244 | export constant BIN_SSHR: u8 = 17; |
|
| 240 | 245 | /// Wire tag for binary operation Ushr. |
|
| 241 | 246 | export constant BIN_USHR: u8 = 18; |
|
| 242 | 247 | ||
| 248 | + | /// Wire tag for RotL. |
|
| 249 | + | export constant BIN_ROTL: u8 = 19; |
|
| 250 | + | /// Wire tag for RotR. |
|
| 251 | + | export constant BIN_ROTR: u8 = 20; |
|
| 252 | + | ||
| 243 | 253 | /// Wire tag for unary operation Neg. |
|
| 244 | 254 | export constant UN_NEG: u8 = 0; |
|
| 245 | 255 | /// Wire tag for unary operation Not. |
|
| 246 | 256 | export constant UN_NOT: u8 = 1; |
|
| 247 | 257 | ||
| 258 | + | /// Wire tag for LeadingZeros. |
|
| 259 | + | export constant UN_LEADING_ZEROS: u8 = 2; |
|
| 260 | + | /// Wire tag for TrailingZeros. |
|
| 261 | + | export constant UN_TRAILING_ZEROS: u8 = 3; |
|
| 262 | + | /// Wire tag for PopCount. |
|
| 263 | + | export constant UN_POP_COUNT: u8 = 4; |
|
| 264 | + | /// Wire tag for ByteSwap. |
|
| 265 | + | export constant UN_BYTE_SWAP: u8 = 5; |
|
| 266 | + | ||
| 248 | 267 | /// Wire tag for comparison Eq. |
|
| 249 | 268 | export constant CMP_EQ: u8 = 0; |
|
| 250 | 269 | /// Wire tag for comparison Ne. |
|
| 251 | 270 | export constant CMP_NE: u8 = 1; |
|
| 252 | 271 | /// Wire tag for comparison Slt. |
lib/std/lang/il/binary/collect.rad
+7 -2
| 180 | 180 | } |
|
| 181 | 181 | case il::Instr::SliceGrow { heap, capacity, .. } => { |
|
| 182 | 182 | try value(names, owner, heap, a); |
|
| 183 | 183 | try value(names, owner, capacity, a); |
|
| 184 | 184 | } |
|
| 185 | - | case il::Instr::SliceLen { length, .. } => try value(names, owner, length, a), |
|
| 186 | - | case il::Instr::Elem { index, .. } => try value(names, owner, index, a), |
|
| 185 | + | case il::Instr::SliceLen { length, .. } => try value(names, owner, length, a), |
|
| 186 | + | case il::Instr::Elem { index, .. } => try value(names, owner, index, a), |
|
| 187 | + | case il::Instr::BytesLoad { offset, .. } => try value(names, owner, offset, a), |
|
| 188 | + | case il::Instr::BytesStore { offset, value: val, .. } => { |
|
| 189 | + | try value(names, owner, offset, a); |
|
| 190 | + | try value(names, owner, val, a); |
|
| 191 | + | } |
|
| 187 | 192 | case il::Instr::SliceElem { index, .. } => try value(names, owner, index, a), |
|
| 188 | 193 | case il::Instr::StorePtr { src, .. } => try value(names, owner, src, a), |
|
| 189 | 194 | case il::Instr::TraitNew { table, .. } => try value(names, owner, table, a), |
|
| 190 | 195 | case il::Instr::UnionSet { src, .. } => try value(names, owner, src, a), |
|
| 191 | 196 | case il::Instr::TraitCall { args, .. } => |
lib/std/lang/il/binary/reader.rad
+23 -3
| 338 | 338 | case super::BIN_OR => return il::BinOp::Or, |
|
| 339 | 339 | case super::BIN_XOR => return il::BinOp::Xor, |
|
| 340 | 340 | case super::BIN_SHL => return il::BinOp::Shl, |
|
| 341 | 341 | case super::BIN_SSHR => return il::BinOp::Sshr, |
|
| 342 | 342 | case super::BIN_USHR => return il::BinOp::Ushr, |
|
| 343 | + | case super::BIN_ROTR => return il::BinOp::RotR, |
|
| 344 | + | case super::BIN_ROTL => return il::BinOp::RotL, |
|
| 343 | 345 | else => throw binary::Error::Invalid, |
|
| 344 | 346 | } |
|
| 345 | 347 | } |
|
| 346 | 348 | ||
| 347 | 349 | /// Read a checked un operation tag. |
|
| 348 | 350 | fn (input: &mut Reader['wire]) unOp() -> il::UnOp throws (binary::Error) { |
|
| 349 | 351 | let tag = try input.integer(1) as u8; |
|
| 350 | 352 | match tag { |
|
| 351 | - | case super::UN_NEG => return il::UnOp::Neg, |
|
| 352 | - | case super::UN_NOT => return il::UnOp::Not, |
|
| 353 | - | else => throw binary::Error::Invalid, |
|
| 353 | + | case super::UN_NEG => return il::UnOp::Neg, |
|
| 354 | + | case super::UN_NOT => return il::UnOp::Not, |
|
| 355 | + | case super::UN_BYTE_SWAP => return il::UnOp::ByteSwap, |
|
| 356 | + | case super::UN_POP_COUNT => return il::UnOp::PopCount, |
|
| 357 | + | case super::UN_TRAILING_ZEROS => return il::UnOp::TrailingZeros, |
|
| 358 | + | case super::UN_LEADING_ZEROS => return il::UnOp::LeadingZeros, |
|
| 359 | + | else => throw binary::Error::Invalid, |
|
| 354 | 360 | } |
|
| 355 | 361 | } |
|
| 356 | 362 | ||
| 357 | 363 | /// Read a checked cmp operation tag. |
|
| 358 | 364 | fn (input: &mut Reader['wire]) cmpOp() -> il::CmpOp throws (binary::Error) { |
| 584 | 590 | let vcount = try input.integer(4) as u32; |
|
| 585 | 591 | return il::Instr::Elem { |
|
| 586 | 592 | dst: vdst, base: vbase, index: vindex, layout: vlayout, count: vcount |
|
| 587 | 593 | }; |
|
| 588 | 594 | } |
|
| 595 | + | case super::INSTR_BYTES_LOAD => { |
|
| 596 | + | let typ = try input.typ(); |
|
| 597 | + | let dst = try input.reg(); |
|
| 598 | + | let slice = try input.reg(); |
|
| 599 | + | let offset = try input.val(); |
|
| 600 | + | return il::Instr::BytesLoad { typ, dst, slice, offset }; |
|
| 601 | + | } |
|
| 602 | + | case super::INSTR_BYTES_STORE => { |
|
| 603 | + | let typ = try input.typ(); |
|
| 604 | + | let slice = try input.reg(); |
|
| 605 | + | let offset = try input.val(); |
|
| 606 | + | let value = try input.val(); |
|
| 607 | + | return il::Instr::BytesStore { typ, slice, offset, value }; |
|
| 608 | + | } |
|
| 589 | 609 | case super::INSTR_SLICE_ELEM => { |
|
| 590 | 610 | let vdst = try input.reg(); |
|
| 591 | 611 | let vslice = try input.reg(); |
|
| 592 | 612 | let vindex = try input.val(); |
|
| 593 | 613 | let vlayout = try input.layoutIndex(); |
lib/std/lang/il/binary/tests.rad
+13 -4
| 1 | 1 | //! Binary RIL encoding fixtures. |
|
| 2 | 2 | ||
| 3 | 3 | @test export mod decode; |
|
| 4 | 4 | ||
| 5 | - | use std::testing; |
|
| 6 | 5 | use std::lang::il; |
|
| 7 | - | use std::lang::il::binary::writer; |
|
| 8 | 6 | use std::lang::il::binary; |
|
| 9 | 7 | use std::lang::il::binary::reader; |
|
| 8 | + | use std::lang::il::binary::writer; |
|
| 10 | 9 | use std::mem::heap; |
|
| 10 | + | use std::testing; |
|
| 11 | 11 | ||
| 12 | 12 | /// Decode arena backing storage. Tests reset it before each use. |
|
| 13 | 13 | static MEMORY: [u8; 512] = [0; 512]; |
|
| 14 | 14 | ||
| 15 | 15 | /// Check little-endian encoding for every integer width. |
| 151 | 151 | dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, offset: 8 |
|
| 152 | 152 | }, &[21, 1, 0, 0, 0, 2, 0, 0, 0, 8, 0, 0, 0]); |
|
| 153 | 153 | try instruction(il::Instr::Elem { |
|
| 154 | 154 | dst: il::Reg { n: 1 }, base: il::Reg { n: 2 }, index: il::Val::Undef, layout: 3, count: 5 |
|
| 155 | 155 | }, &[22, 1, 0, 0, 0, 2, 0, 0, 0, 4, 3, 0, 0, 0, 5, 0, 0, 0]); |
|
| 156 | + | try instruction(il::Instr::BytesLoad { |
|
| 157 | + | typ: il::Type::W64, dst: il::Reg { n: 1 }, slice: il::Reg { n: 2 }, offset: il::Val::Undef |
|
| 158 | + | }, &[40, 8, 1, 0, 0, 0, 2, 0, 0, 0, 4]); |
|
| 159 | + | try instruction(il::Instr::BytesStore { |
|
| 160 | + | typ: il::Type::W32, slice: il::Reg { n: 2 }, offset: il::Val::Undef, value: il::Val::Undef |
|
| 161 | + | }, &[41, 4, 2, 0, 0, 0, 4, 4]); |
|
| 156 | 162 | try instruction(il::Instr::SliceElem { |
|
| 157 | 163 | dst: il::Reg { n: 1 }, slice: il::Reg { n: 2 }, index: il::Val::Undef, layout: 3 |
|
| 158 | 164 | }, &[23, 1, 0, 0, 0, 2, 0, 0, 0, 4, 3, 0, 0, 0]); |
|
| 159 | 165 | try instruction(il::Instr::SliceNew { |
|
| 160 | 166 | dst: il::Reg { n: 1 }, |
| 562 | 568 | il::BinOp::Sdiv, il::BinOp::Udiv, il::BinOp::Srem, |
|
| 563 | 569 | il::BinOp::Urem, il::BinOp::Eq, il::BinOp::Ne, |
|
| 564 | 570 | il::BinOp::Slt, il::BinOp::Sge, il::BinOp::Ult, |
|
| 565 | 571 | il::BinOp::Uge, il::BinOp::And, il::BinOp::Or, |
|
| 566 | 572 | il::BinOp::Xor, il::BinOp::Shl, il::BinOp::Sshr, |
|
| 567 | - | il::BinOp::Ushr, |
|
| 573 | + | il::BinOp::Ushr, il::BinOp::RotL, il::BinOp::RotR, |
|
| 568 | 574 | ]; |
|
| 569 | 575 | for op, tag in binaryOps { |
|
| 570 | 576 | try instruction(il::Instr::BinOp { |
|
| 571 | 577 | op, typ: il::Type::W64, dst: il::Reg { n: 1 }, a: il::Val::Undef, b: il::Val::Undef |
|
| 572 | 578 | }, &[6, tag as u8, 8, 1, 0, 0, 0, 4, 4]); |
|
| 573 | 579 | } |
|
| 574 | - | for op, tag in &[il::UnOp::Neg, il::UnOp::Not] { |
|
| 580 | + | for op, tag in &[ |
|
| 581 | + | il::UnOp::Neg, il::UnOp::Not, il::UnOp::LeadingZeros, |
|
| 582 | + | il::UnOp::TrailingZeros, il::UnOp::PopCount, il::UnOp::ByteSwap, |
|
| 583 | + | ] { |
|
| 575 | 584 | try instruction(il::Instr::UnOp { |
|
| 576 | 585 | op, typ: il::Type::W64, dst: il::Reg { n: 1 }, a: il::Val::Undef |
|
| 577 | 586 | }, &[7, tag as u8, 8, 1, 0, 0, 0, 4]); |
|
| 578 | 587 | } |
|
| 579 | 588 | let empty = il::ArgRange { start: 0, len: 0 }; |
lib/std/lang/il/binary/writer.rad
+22 -2
| 310 | 310 | case il::BinOp::Or => try integer(out, super::BIN_OR as u64, 1), |
|
| 311 | 311 | case il::BinOp::Xor => try integer(out, super::BIN_XOR as u64, 1), |
|
| 312 | 312 | case il::BinOp::Shl => try integer(out, super::BIN_SHL as u64, 1), |
|
| 313 | 313 | case il::BinOp::Sshr => try integer(out, super::BIN_SSHR as u64, 1), |
|
| 314 | 314 | case il::BinOp::Ushr => try integer(out, super::BIN_USHR as u64, 1), |
|
| 315 | + | case il::BinOp::RotR => try integer(out, super::BIN_ROTR as u64, 1), |
|
| 316 | + | case il::BinOp::RotL => try integer(out, super::BIN_ROTL as u64, 1), |
|
| 315 | 317 | } |
|
| 316 | 318 | } |
|
| 317 | 319 | ||
| 318 | 320 | /// Write an explicit un operation tag. |
|
| 319 | 321 | fn unOp(out: &mut Writer, value: il::UnOp) throws (binary::Error) { |
|
| 320 | 322 | match value { |
|
| 321 | - | case il::UnOp::Neg => try integer(out, super::UN_NEG as u64, 1), |
|
| 322 | - | case il::UnOp::Not => try integer(out, super::UN_NOT as u64, 1), |
|
| 323 | + | case il::UnOp::Neg => try integer(out, super::UN_NEG as u64, 1), |
|
| 324 | + | case il::UnOp::Not => try integer(out, super::UN_NOT as u64, 1), |
|
| 325 | + | case il::UnOp::ByteSwap => try integer(out, super::UN_BYTE_SWAP as u64, 1), |
|
| 326 | + | case il::UnOp::PopCount => try integer(out, super::UN_POP_COUNT as u64, 1), |
|
| 327 | + | case il::UnOp::TrailingZeros => try integer(out, super::UN_TRAILING_ZEROS as u64, 1), |
|
| 328 | + | case il::UnOp::LeadingZeros => try integer(out, super::UN_LEADING_ZEROS as u64, 1), |
|
| 323 | 329 | } |
|
| 324 | 330 | } |
|
| 325 | 331 | ||
| 326 | 332 | /// Write an explicit cmp operation tag. |
|
| 327 | 333 | fn cmpOp(out: &mut Writer, value: il::CmpOp) throws (binary::Error) { |
| 531 | 537 | try integer(out, vbase.n as u64, 4); |
|
| 532 | 538 | try val(out, vindex); |
|
| 533 | 539 | try integer(out, vlayout as u64, 4); |
|
| 534 | 540 | try integer(out, vcount as u64, 4); |
|
| 535 | 541 | } |
|
| 542 | + | case il::Instr::BytesLoad { typ: width, dst, slice, offset } => { |
|
| 543 | + | try integer(out, super::INSTR_BYTES_LOAD as u64, 1); |
|
| 544 | + | try typ(out, width); |
|
| 545 | + | try integer(out, dst.n as u64, 4); |
|
| 546 | + | try integer(out, slice.n as u64, 4); |
|
| 547 | + | try val(out, offset); |
|
| 548 | + | } |
|
| 549 | + | case il::Instr::BytesStore { typ: width, slice, offset, value } => { |
|
| 550 | + | try integer(out, super::INSTR_BYTES_STORE as u64, 1); |
|
| 551 | + | try typ(out, width); |
|
| 552 | + | try integer(out, slice.n as u64, 4); |
|
| 553 | + | try val(out, offset); |
|
| 554 | + | try val(out, value); |
|
| 555 | + | } |
|
| 536 | 556 | case il::Instr::SliceElem { dst: vdst, slice: vslice, index: vindex, layout: vlayout } => { |
|
| 537 | 557 | try integer(out, super::INSTR_SLICE_ELEM as u64, 1); |
|
| 538 | 558 | try integer(out, vdst.n as u64, 4); |
|
| 539 | 559 | try integer(out, vslice.n as u64, 4); |
|
| 540 | 560 | try val(out, vindex); |
lib/std/lang/il/printer.rad
+28 -2
| 128 | 128 | case super::BinOp::Or => return "or", |
|
| 129 | 129 | case super::BinOp::Xor => return "xor", |
|
| 130 | 130 | case super::BinOp::Shl => return "shl", |
|
| 131 | 131 | case super::BinOp::Sshr => return "sshr", |
|
| 132 | 132 | case super::BinOp::Ushr => return "ushr", |
|
| 133 | + | case super::BinOp::RotR => return "rotate.right", |
|
| 134 | + | case super::BinOp::RotL => return "rotate.left", |
|
| 133 | 135 | } |
|
| 134 | 136 | } |
|
| 135 | 137 | ||
| 136 | 138 | /// Get the string representation of a unary ALU operation. |
|
| 137 | 139 | fn unOpStr(op: super::UnOp) -> *[u8] { |
|
| 138 | 140 | match op { |
|
| 139 | - | case super::UnOp::Neg => return "neg", |
|
| 140 | - | case super::UnOp::Not => return "not", |
|
| 141 | + | case super::UnOp::Neg => return "neg", |
|
| 142 | + | case super::UnOp::Not => return "not", |
|
| 143 | + | case super::UnOp::ByteSwap => return "byte.swap", |
|
| 144 | + | case super::UnOp::PopCount => return "pop.count", |
|
| 145 | + | case super::UnOp::TrailingZeros => return "trailing.zeros", |
|
| 146 | + | case super::UnOp::LeadingZeros => return "leading.zeros", |
|
| 141 | 147 | } |
|
| 142 | 148 | } |
|
| 143 | 149 | ||
| 144 | 150 | //////////////////// |
|
| 145 | 151 | // Value printing // |
| 306 | 312 | out.write(" "); |
|
| 307 | 313 | writeIndex(out, "L", layout); |
|
| 308 | 314 | out.write(" "); |
|
| 309 | 315 | sexpr::writeI64(out, count as i64); |
|
| 310 | 316 | } |
|
| 317 | + | case super::Instr::BytesLoad { typ, dst, slice, offset } => { |
|
| 318 | + | out.write("bytes.load "); |
|
| 319 | + | out.write(typeStr(typ)); |
|
| 320 | + | out.write(" "); |
|
| 321 | + | writeReg(out, dst); |
|
| 322 | + | out.write(" "); |
|
| 323 | + | writeReg(out, slice); |
|
| 324 | + | out.write(" "); |
|
| 325 | + | writeVal(out, offset); |
|
| 326 | + | } |
|
| 327 | + | case super::Instr::BytesStore { typ, slice, offset, value } => { |
|
| 328 | + | out.write("bytes.store "); |
|
| 329 | + | out.write(typeStr(typ)); |
|
| 330 | + | out.write(" "); |
|
| 331 | + | writeReg(out, slice); |
|
| 332 | + | out.write(" "); |
|
| 333 | + | writeVal(out, offset); |
|
| 334 | + | out.write(" "); |
|
| 335 | + | writeVal(out, value); |
|
| 336 | + | } |
|
| 311 | 337 | case super::Instr::SliceElem { dst, slice, index, layout } => { |
|
| 312 | 338 | out.write("slice.elem "); |
|
| 313 | 339 | writeReg(out, dst); |
|
| 314 | 340 | out.write(" "); |
|
| 315 | 341 | writeReg(out, slice); |
lib/std/lang/il/verify.rad
+38 -2
| 845 | 845 | throw fail(v, Rule::View); |
|
| 846 | 846 | } |
|
| 847 | 847 | return slot; |
|
| 848 | 848 | } |
|
| 849 | 849 | ||
| 850 | + | /// Require plain one-byte elements, with write permission for a store. |
|
| 851 | + | fn byteSlice(v: &Verifier, slice: il::Reg, writing: bool) throws (Error) { |
|
| 852 | + | let slot = try sliceAt(v, try accessible(v, slice)); |
|
| 853 | + | let t = &v.ctx.table; |
|
| 854 | + | if not layout::isPlain(t, slot.layout) or layout::size(t, slot.layout) <> 1 { |
|
| 855 | + | throw fail(v, Rule::View); |
|
| 856 | + | } |
|
| 857 | + | if writing and not slot.mutable { |
|
| 858 | + | throw fail(v, Rule::Mutability); |
|
| 859 | + | } |
|
| 860 | + | } |
|
| 861 | + | ||
| 850 | 862 | /// Whether a pointer addresses an object that can hold non-owning pointers: an |
|
| 851 | 863 | /// object of the current frame, or a target object of a parameter. |
|
| 852 | 864 | fn holds(p: PtrState) -> bool { |
|
| 853 | 865 | return p.local or p.target; |
|
| 854 | 866 | } |
| 1354 | 1366 | nullable: false, |
|
| 1355 | 1367 | local: p.local, |
|
| 1356 | 1368 | target: p.target, |
|
| 1357 | 1369 | })); |
|
| 1358 | 1370 | } |
|
| 1371 | + | case il::Instr::BytesLoad { dst, slice, offset, .. } => { |
|
| 1372 | + | try byteSlice(v, slice, false); |
|
| 1373 | + | try operand(v, offset); |
|
| 1374 | + | try define(v, dst, RegState::Data); |
|
| 1375 | + | } |
|
| 1376 | + | case il::Instr::BytesStore { slice, offset, value, .. } => { |
|
| 1377 | + | try byteSlice(v, slice, true); |
|
| 1378 | + | try operand(v, offset); |
|
| 1379 | + | try operand(v, value); |
|
| 1380 | + | } |
|
| 1359 | 1381 | case il::Instr::SliceElem { dst, slice, index, layout: e } => { |
|
| 1360 | 1382 | try operand(v, index); |
|
| 1361 | 1383 | let p = try accessible(v, slice); |
|
| 1362 | 1384 | let slot = try sliceSource(v, p, e); |
|
| 1363 | 1385 | try define(v, dst, RegState::Ptr(PtrState { |
| 1469 | 1491 | try checkUnionSet(v, dst, tag, offset, src, layout); |
|
| 1470 | 1492 | } |
|
| 1471 | 1493 | case il::Instr::TraitCall { retTy, dst, obj, method, receiver, args } => { |
|
| 1472 | 1494 | try checkTraitCall(v, retTy, dst, obj, method, receiver, args); |
|
| 1473 | 1495 | } |
|
| 1474 | - | case il::Instr::BinOp { dst, a, b, .. } => { |
|
| 1496 | + | case il::Instr::BinOp { op, typ, dst, a, b } => { |
|
| 1497 | + | if (op == il::BinOp::RotL or op == il::BinOp::RotR) |
|
| 1498 | + | and typ <> il::Type::W32 |
|
| 1499 | + | and typ <> il::Type::W64 |
|
| 1500 | + | { |
|
| 1501 | + | throw fail(v, Rule::Type); |
|
| 1502 | + | } |
|
| 1475 | 1503 | try operand(v, a); |
|
| 1476 | 1504 | try operand(v, b); |
|
| 1477 | 1505 | try define(v, dst, RegState::Data); |
|
| 1478 | 1506 | } |
|
| 1479 | - | case il::Instr::UnOp { dst, a, .. } => { |
|
| 1507 | + | case il::Instr::UnOp { op, typ, dst, a } => { |
|
| 1508 | + | if (op == il::UnOp::LeadingZeros |
|
| 1509 | + | or op == il::UnOp::TrailingZeros |
|
| 1510 | + | or op == il::UnOp::PopCount) |
|
| 1511 | + | and typ <> il::Type::W32 |
|
| 1512 | + | and typ <> il::Type::W64 |
|
| 1513 | + | { |
|
| 1514 | + | throw fail(v, Rule::Type); |
|
| 1515 | + | } |
|
| 1480 | 1516 | try operand(v, a); |
|
| 1481 | 1517 | try define(v, dst, RegState::Data); |
|
| 1482 | 1518 | } |
|
| 1483 | 1519 | case il::Instr::Zext { dst, val, .. } => { |
|
| 1484 | 1520 | try operand(v, val); |
lib/std/lang/il/verify/tests.rad
+26 -0
| 5455 | 5455 | il::Borrow { target: nil, result: 1, param: 1, source: 1 }, |
|
| 5456 | 5456 | ] |
|
| 5457 | 5457 | ); |
|
| 5458 | 5458 | try accepted(&mut b); |
|
| 5459 | 5459 | } |
|
| 5460 | + | ||
| 5461 | + | /// Byte operations accept plain bytes and enforce element write permission. |
|
| 5462 | + | @test unsafe fn byteAccess() throws (testing::TestError) { |
|
| 5463 | + | for width in &[il::Type::W8, il::Type::W16, il::Type::W32, il::Type::W64] { |
|
| 5464 | + | let read = il::Instr::BytesLoad { |
|
| 5465 | + | typ: width, dst: il::Reg { n: 1 }, slice: il::Reg { n: 0 }, offset: imm(0) |
|
| 5466 | + | }; |
|
| 5467 | + | let write = il::Instr::BytesStore { |
|
| 5468 | + | typ: width, slice: il::Reg { n: 0 }, offset: imm(0), value: imm(0) |
|
| 5469 | + | }; |
|
| 5470 | + | for slot in &[SLICE, SLICEC, BORROW, SLICE4, SLICECELL, P16] { |
|
| 5471 | + | let input = param(0, ptrType(slot, false, false, false)); |
|
| 5472 | + | let invalid = slot == SLICE4 or slot == SLICECELL; |
|
| 5473 | + | let expected = verify::Rule::View if invalid else verify::Rule::Slot; |
|
| 5474 | + | try check(&[input], &[read], expected if invalid or slot == P16 else nil); |
|
| 5475 | + | if slot == SLICEC { |
|
| 5476 | + | try check(&[input], &[write], verify::Rule::Mutability); |
|
| 5477 | + | } else { |
|
| 5478 | + | try check(&[input], &[write], expected if invalid or slot == P16 else nil); |
|
| 5479 | + | } |
|
| 5480 | + | } |
|
| 5481 | + | let nullable = param(0, ptrType(SLICE, false, false, true)); |
|
| 5482 | + | try check(&[nullable], &[read], verify::Rule::Null); |
|
| 5483 | + | try check(&[nullable], &[write], verify::Rule::Null); |
|
| 5484 | + | } |
|
| 5485 | + | } |
lib/std/lang/lower.rad
+155 -0
| 10687 | 10687 | if not ast::hasAttribute(self.resolver.symbols[*sym].attrs, ast::Attribute::Intrinsic) { |
|
| 10688 | 10688 | return nil; |
|
| 10689 | 10689 | } |
|
| 10690 | 10690 | // Check for known intrinsic names. |
|
| 10691 | 10691 | let name = self.resolver.symbols[*sym].name; |
|
| 10692 | + | if mem::eq(name, "load16Le") { |
|
| 10693 | + | return try lowerBytes(self, call, il::Type::W16, false); |
|
| 10694 | + | } |
|
| 10695 | + | if mem::eq(name, "store16Le") { |
|
| 10696 | + | return try lowerBytes(self, call, il::Type::W16, true); |
|
| 10697 | + | } |
|
| 10698 | + | if mem::eq(name, "load32Le") { |
|
| 10699 | + | return try lowerBytes(self, call, il::Type::W32, false); |
|
| 10700 | + | } |
|
| 10701 | + | if mem::eq(name, "store32Le") { |
|
| 10702 | + | return try lowerBytes(self, call, il::Type::W32, true); |
|
| 10703 | + | } |
|
| 10704 | + | if mem::eq(name, "load64Le") { |
|
| 10705 | + | return try lowerBytes(self, call, il::Type::W64, false); |
|
| 10706 | + | } |
|
| 10707 | + | if mem::eq(name, "store64Le") { |
|
| 10708 | + | return try lowerBytes(self, call, il::Type::W64, true); |
|
| 10709 | + | } |
|
| 10710 | + | if mem::eq(name, "rotateLeft32") { |
|
| 10711 | + | return try lowerRotate(self, call, il::Type::W32, il::BinOp::RotL); |
|
| 10712 | + | } |
|
| 10713 | + | if mem::eq(name, "rotateRight32") { |
|
| 10714 | + | return try lowerRotate(self, call, il::Type::W32, il::BinOp::RotR); |
|
| 10715 | + | } |
|
| 10716 | + | if mem::eq(name, "rotateLeft64") { |
|
| 10717 | + | return try lowerRotate(self, call, il::Type::W64, il::BinOp::RotL); |
|
| 10718 | + | } |
|
| 10719 | + | if mem::eq(name, "rotateRight64") { |
|
| 10720 | + | return try lowerRotate(self, call, il::Type::W64, il::BinOp::RotR); |
|
| 10721 | + | } |
|
| 10722 | + | if mem::eq(name, "byteSwap16") { |
|
| 10723 | + | return try lowerBits(self, call, il::Type::W16, il::UnOp::ByteSwap); |
|
| 10724 | + | } |
|
| 10725 | + | if mem::eq(name, "leadingZeros32") { |
|
| 10726 | + | return try lowerBits(self, call, il::Type::W32, il::UnOp::LeadingZeros); |
|
| 10727 | + | } |
|
| 10728 | + | if mem::eq(name, "trailingZeros32") { |
|
| 10729 | + | return try lowerBits(self, call, il::Type::W32, il::UnOp::TrailingZeros); |
|
| 10730 | + | } |
|
| 10731 | + | if mem::eq(name, "popCount32") { |
|
| 10732 | + | return try lowerBits(self, call, il::Type::W32, il::UnOp::PopCount); |
|
| 10733 | + | } |
|
| 10734 | + | if mem::eq(name, "byteSwap32") { |
|
| 10735 | + | return try lowerBits(self, call, il::Type::W32, il::UnOp::ByteSwap); |
|
| 10736 | + | } |
|
| 10737 | + | if mem::eq(name, "leadingZeros64") { |
|
| 10738 | + | return try lowerBits(self, call, il::Type::W64, il::UnOp::LeadingZeros); |
|
| 10739 | + | } |
|
| 10740 | + | if mem::eq(name, "trailingZeros64") { |
|
| 10741 | + | return try lowerBits(self, call, il::Type::W64, il::UnOp::TrailingZeros); |
|
| 10742 | + | } |
|
| 10743 | + | if mem::eq(name, "popCount64") { |
|
| 10744 | + | return try lowerBits(self, call, il::Type::W64, il::UnOp::PopCount); |
|
| 10745 | + | } |
|
| 10746 | + | if mem::eq(name, "byteSwap64") { |
|
| 10747 | + | return try lowerBits(self, call, il::Type::W64, il::UnOp::ByteSwap); |
|
| 10748 | + | } |
|
| 10692 | 10749 | if mem::stripPrefix("ecall", name) <> nil { |
|
| 10693 | 10750 | return try lowerEcall(self, call); |
|
| 10694 | 10751 | } else if mem::stripPrefix("pageSlice", name) <> nil { |
|
| 10695 | 10752 | return try lowerPageSlice(self, call); |
|
| 10696 | 10753 | } else if mem::eq(name, "pageHeap") { |
| 12231 | 12288 | } |
|
| 12232 | 12289 | let dst = nextReg(self); |
|
| 12233 | 12290 | try emit(self, il::Instr::DeviceRead { typ, dst, handle, offset }); |
|
| 12234 | 12291 | return il::Val::Reg(dst); |
|
| 12235 | 12292 | } |
|
| 12293 | + | ||
| 12294 | + | /// Require a non-throwing intrinsic with the given parameter and result types. |
|
| 12295 | + | fn integerIntrinsic( |
|
| 12296 | + | self: &mut Lowerer, call: &ast::Call, params: &[resolver::Type], result: resolver::Type |
|
| 12297 | + | ) throws (LowerError) { |
|
| 12298 | + | let case resolver::Type::Fn(info) = try typeOf(self, call.callee) |
|
| 12299 | + | else throw LowerError::ExpectedFunction; |
|
| 12300 | + | if info.throwList.len <> 0 |
|
| 12301 | + | or info.paramTypes.len <> params.len |
|
| 12302 | + | or call.args.len <> params.len |
|
| 12303 | + | or *info.returnType <> result |
|
| 12304 | + | { |
|
| 12305 | + | throw LowerError::Unsupported; |
|
| 12306 | + | } |
|
| 12307 | + | for typ, i in params { |
|
| 12308 | + | if *info.paramTypes[i] <> typ { |
|
| 12309 | + | throw LowerError::Unsupported; |
|
| 12310 | + | } |
|
| 12311 | + | } |
|
| 12312 | + | } |
|
| 12313 | + | ||
| 12314 | + | /// Unsigned source type for an integer primitive. |
|
| 12315 | + | fn unsignedType(typ: il::Type) -> resolver::Type { |
|
| 12316 | + | match typ { |
|
| 12317 | + | case il::Type::W8 => return resolver::Type::U8, |
|
| 12318 | + | case il::Type::W16 => return resolver::Type::U16, |
|
| 12319 | + | case il::Type::W32 => return resolver::Type::U32, |
|
| 12320 | + | case il::Type::W64 => return resolver::Type::U64, |
|
| 12321 | + | } |
|
| 12322 | + | } |
|
| 12323 | + | ||
| 12324 | + | /// Lower a checked load or store on a borrowed byte slice. |
|
| 12325 | + | fn lowerBytes( |
|
| 12326 | + | self: &mut Lowerer, call: &ast::Call, typ: il::Type, writing: bool |
|
| 12327 | + | ) -> il::Val throws (LowerError) { |
|
| 12328 | + | let case resolver::Type::Fn(info) = try typeOf(self, call.callee) |
|
| 12329 | + | else throw LowerError::ExpectedFunction; |
|
| 12330 | + | let count: u32 = 3 if writing else 2; |
|
| 12331 | + | if info.throwList.len <> 0 |
|
| 12332 | + | or info.paramTypes.len <> count |
|
| 12333 | + | or call.args.len <> count |
|
| 12334 | + | { |
|
| 12335 | + | throw LowerError::Unsupported; |
|
| 12336 | + | } |
|
| 12337 | + | let case resolver::Type::Slice { class, item, mutable } = *info.paramTypes[0] |
|
| 12338 | + | else throw LowerError::Unsupported; |
|
| 12339 | + | if class <> types::PointerClass::Ref |
|
| 12340 | + | or *item <> resolver::Type::U8 |
|
| 12341 | + | or mutable <> writing |
|
| 12342 | + | or *info.paramTypes[1] <> resolver::Type::U32 |
|
| 12343 | + | { |
|
| 12344 | + | throw LowerError::Unsupported; |
|
| 12345 | + | } |
|
| 12346 | + | let valueType = unsignedType(typ); |
|
| 12347 | + | if writing { |
|
| 12348 | + | if *info.returnType <> resolver::Type::Void or *info.paramTypes[2] <> valueType { |
|
| 12349 | + | throw LowerError::Unsupported; |
|
| 12350 | + | } |
|
| 12351 | + | } else if *info.returnType <> valueType { |
|
| 12352 | + | throw LowerError::Unsupported; |
|
| 12353 | + | } |
|
| 12354 | + | let slice = try emitValToReg(self, try lowerExpr(self, call.args[0])); |
|
| 12355 | + | let offset = try lowerExpr(self, call.args[1]); |
|
| 12356 | + | if writing { |
|
| 12357 | + | let value = try lowerExpr(self, call.args[2]); |
|
| 12358 | + | try emit(self, il::Instr::BytesStore { typ, slice, offset, value }); |
|
| 12359 | + | return il::Val::Undef; |
|
| 12360 | + | } |
|
| 12361 | + | let dst = nextReg(self); |
|
| 12362 | + | try emit(self, il::Instr::BytesLoad { typ, dst, slice, offset }); |
|
| 12363 | + | return il::Val::Reg(dst); |
|
| 12364 | + | } |
|
| 12365 | + | ||
| 12366 | + | /// Lower a rotation with a count modulo the value width. |
|
| 12367 | + | fn lowerRotate( |
|
| 12368 | + | self: &mut Lowerer, call: &ast::Call, typ: il::Type, op: il::BinOp |
|
| 12369 | + | ) -> il::Val throws (LowerError) { |
|
| 12370 | + | let valueType = unsignedType(typ); |
|
| 12371 | + | try integerIntrinsic(self, call, &[valueType, resolver::Type::U32], valueType); |
|
| 12372 | + | let a = try lowerExpr(self, call.args[0]); |
|
| 12373 | + | let b = try lowerExpr(self, call.args[1]); |
|
| 12374 | + | let dst = nextReg(self); |
|
| 12375 | + | try emit(self, il::Instr::BinOp { op, typ, dst, a, b }); |
|
| 12376 | + | return il::Val::Reg(dst); |
|
| 12377 | + | } |
|
| 12378 | + | ||
| 12379 | + | /// Lower a bit count or byte reversal. |
|
| 12380 | + | fn lowerBits( |
|
| 12381 | + | self: &mut Lowerer, call: &ast::Call, typ: il::Type, op: il::UnOp |
|
| 12382 | + | ) -> il::Val throws (LowerError) { |
|
| 12383 | + | let valueType = unsignedType(typ); |
|
| 12384 | + | let result = valueType if op == il::UnOp::ByteSwap else resolver::Type::U32; |
|
| 12385 | + | try integerIntrinsic(self, call, &[valueType], result); |
|
| 12386 | + | let a = try lowerExpr(self, call.args[0]); |
|
| 12387 | + | let dst = nextReg(self); |
|
| 12388 | + | try emit(self, il::Instr::UnOp { op, typ, dst, a }); |
|
| 12389 | + | return il::Val::Reg(dst); |
|
| 12390 | + | } |
lib/std/lang/lower/tests.rad
+21 -0
| 260 | 260 | assert borrow.param == 0; |
|
| 261 | 261 | assert borrow.source == nil; |
|
| 262 | 262 | assert program.fns[1].borrows.len == 1; |
|
| 263 | 263 | assert program.fns[1].borrows[0].param == 1; |
|
| 264 | 264 | } |
|
| 265 | + | ||
| 266 | + | /// Integer primitives require their exact source signatures. |
|
| 267 | + | @test unsafe fn testIntegerIntrinsicSignatures() throws (testing::TestError) { |
|
| 268 | + | for source in &[ |
|
| 269 | + | "@intrinsic fn rotateLeft32(x: u64, n: u32) -> u64; fn f() -> u64 { return rotateLeft32(0, 0); }", |
|
| 270 | + | "@intrinsic fn leadingZeros64(x: u64) -> u64; fn f() -> u64 { return leadingZeros64(0); }", |
|
| 271 | + | "@intrinsic fn load64Le(b: &[u8], i: u32) -> u32; fn f(b: &[u8]) -> u32 { return load64Le(b, 0); }", |
|
| 272 | + | "@intrinsic fn store32Le(b: &[u8], i: u32, x: u32); fn f(b: &[u8]) { store32Le(b, 0, 0); }", |
|
| 273 | + | ] { |
|
| 274 | + | let mut res = tests::testResolver(); |
|
| 275 | + | let result = try tests::resolveProgramStr(&mut res, source); |
|
| 276 | + | try tests::expectNoErrors(&result); |
|
| 277 | + | let arena = heap::over(&mut FN_STORAGE[..]); |
|
| 278 | + | let mut failed = false; |
|
| 279 | + | try super::lower(res, result.root, "test", arena) catch err { |
|
| 280 | + | assert err == super::LowerError::Unsupported; |
|
| 281 | + | set failed = true; |
|
| 282 | + | }; |
|
| 283 | + | assert failed; |
|
| 284 | + | } |
|
| 285 | + | } |
lib/std/mem.rad
+27 -0
| 1 | 1 | //! Memory operations and alignment helpers. |
|
| 2 | 2 | export mod heap; |
|
| 3 | 3 | ||
| 4 | + | @test mod tests; |
|
| 5 | + | ||
| 6 | + | use std::bits; |
|
| 4 | 7 | use std::char; |
|
| 5 | 8 | ||
| 6 | 9 | /// Largest unsigned 64-bit integer. |
|
| 7 | 10 | export constant U64_MAX: u64 = 0xffffffffffffffff; |
|
| 8 | 11 | /// Largest unsigned 32-bit integer. |
| 15 | 18 | union MemoryError: Copy { |
|
| 16 | 19 | /// Buffer is too small. |
|
| 17 | 20 | BufferTooSmall, |
|
| 18 | 21 | } |
|
| 19 | 22 | ||
| 23 | + | /// Load an unsigned 16-bit value from `bytes` at `offset` in little-endian order. |
|
| 24 | + | /// The address can be unaligned. Trap unless all 2 bytes fit in the slice. |
|
| 25 | + | @intrinsic export fn load16Le(bytes: &[u8], offset: u32) -> u16; |
|
| 26 | + | ||
| 27 | + | /// Store an unsigned 16-bit value at `offset` in little-endian order. |
|
| 28 | + | /// The address can be unaligned. Trap before writing unless all 2 bytes fit. |
|
| 29 | + | @intrinsic export fn store16Le(bytes: &mut [u8], offset: u32, value: u16); |
|
| 30 | + | ||
| 31 | + | /// Load an unsigned 32-bit value from `bytes` at `offset` in little-endian order. |
|
| 32 | + | /// The address can be unaligned. Trap unless all 4 bytes fit in the slice. |
|
| 33 | + | @intrinsic export fn load32Le(bytes: &[u8], offset: u32) -> u32; |
|
| 34 | + | ||
| 35 | + | /// Store an unsigned 32-bit value at `offset` in little-endian order. |
|
| 36 | + | /// The address can be unaligned. Trap before writing unless all 4 bytes fit. |
|
| 37 | + | @intrinsic export fn store32Le(bytes: &mut [u8], offset: u32, value: u32); |
|
| 38 | + | ||
| 39 | + | /// Load an unsigned 64-bit value from `bytes` at `offset` in little-endian order. |
|
| 40 | + | /// The address can be unaligned. Trap unless all 8 bytes fit in the slice. |
|
| 41 | + | @intrinsic export fn load64Le(bytes: &[u8], offset: u32) -> u64; |
|
| 42 | + | ||
| 43 | + | /// Store an unsigned 64-bit value at `offset` in little-endian order. |
|
| 44 | + | /// The address can be unaligned. Trap before writing unless all 8 bytes fit. |
|
| 45 | + | @intrinsic export fn store64Le(bytes: &mut [u8], offset: u32, value: u64); |
|
| 46 | + | ||
| 20 | 47 | /// Copy bytes between two slices. Returns the number of bytes copied. |
|
| 21 | 48 | export fn copy(into: &mut [u8], from: &[u8]) -> u32 throws (MemoryError) { |
|
| 22 | 49 | if into.len < from.len { |
|
| 23 | 50 | throw MemoryError::BufferTooSmall; |
|
| 24 | 51 | } |
lib/std/mem/tests.rad
added
+101 -0
| 1 | + | //! Tests for byte access and memory operations. |
|
| 2 | + | ||
| 3 | + | use std::mem; |
|
| 4 | + | use std::testing; |
|
| 5 | + | ||
| 6 | + | /// Fixed-width accesses use little-endian byte order at every alignment. |
|
| 7 | + | @test fn integerBytes() throws (testing::TestError) { |
|
| 8 | + | for offset in 0 .. 16 { |
|
| 9 | + | let mut bytes: [u8; 24] = [0xa5; 24]; |
|
| 10 | + | mem::store16Le(&mut bytes[.. offset + 2], offset, 0x80ff); |
|
| 11 | + | assert mem::load16Le(&bytes[.. offset + 2], offset) as u64 == 0x80ff; |
|
| 12 | + | for j in 0 .. bytes.len { |
|
| 13 | + | let expected: u8 = 0xa5 |
|
| 14 | + | if j < offset or j >= offset + 2 |
|
| 15 | + | else (0x80ff as u64 >> ((j - offset) * 8) as u64) as u8; |
|
| 16 | + | assert bytes[j] == expected; |
|
| 17 | + | } |
|
| 18 | + | mem::store32Le(&mut bytes[.. offset + 4], offset, 0x89abcdef); |
|
| 19 | + | assert mem::load32Le(&bytes[.. offset + 4], offset) as u64 == 0x89abcdef; |
|
| 20 | + | for j in 0 .. bytes.len { |
|
| 21 | + | let expected: u8 = 0xa5 |
|
| 22 | + | if j < offset or j >= offset + 4 |
|
| 23 | + | else (0x89abcdef as u64 >> ((j - offset) * 8) as u64) as u8; |
|
| 24 | + | assert bytes[j] == expected; |
|
| 25 | + | } |
|
| 26 | + | mem::store64Le(&mut bytes[.. offset + 8], offset, 0x8877665544332211); |
|
| 27 | + | assert mem::load64Le(&bytes[.. offset + 8], offset) == 0x8877665544332211; |
|
| 28 | + | for j in 0 .. bytes.len { |
|
| 29 | + | let expected: u8 = 0xa5 |
|
| 30 | + | if j < offset or j >= offset + 8 |
|
| 31 | + | else (0x8877665544332211 as u64 >> ((j - offset) * 8) as u64) as u8; |
|
| 32 | + | assert bytes[j] == expected; |
|
| 33 | + | } |
|
| 34 | + | mem::store64Le(&mut bytes[..], offset, 0); |
|
| 35 | + | assert mem::load64Le(&bytes[..], offset) == 0; |
|
| 36 | + | mem::store64Le(&mut bytes[..], offset, mem::U64_MAX); |
|
| 37 | + | assert mem::load64Le(&bytes[..], offset) == mem::U64_MAX; |
|
| 38 | + | } |
|
| 39 | + | } |
|
| 40 | + | ||
| 41 | + | /// Copy, equality and comparison handle word boundaries and every mismatch. |
|
| 42 | + | @test fn byteSequences() throws (testing::TestError) { |
|
| 43 | + | let mut source: [u8; 80] = [0; 80]; |
|
| 44 | + | for i in 0 .. source.len { |
|
| 45 | + | set source[i] = (i * 7) as u8; |
|
| 46 | + | } |
|
| 47 | + | for start in 0 .. 8 { |
|
| 48 | + | for length in 0 .. 65 { |
|
| 49 | + | let mut target: [u8; 80] = [0xff; 80]; |
|
| 50 | + | let input = &source[start .. start + length]; |
|
| 51 | + | assert try! mem::copy(&mut target[1 ..], input) == length; |
|
| 52 | + | assert target[0] == 0xff; |
|
| 53 | + | for i in 0 .. length { |
|
| 54 | + | assert target[1 + i] == source[start + i]; |
|
| 55 | + | } |
|
| 56 | + | assert target[length + 1] == 0xff; |
|
| 57 | + | assert mem::eq(input, &target[1 .. length + 1]); |
|
| 58 | + | assert mem::cmp(input, &target[1 .. length + 1]) == 0; |
|
| 59 | + | for i in 0 .. length { |
|
| 60 | + | let saved = target[1 + i]; |
|
| 61 | + | set target[1 + i] = saved ^ 0x80; |
|
| 62 | + | let order: i32 = -1 if saved < (saved ^ 0x80) else 1; |
|
| 63 | + | assert not mem::eq(input, &target[1 .. length + 1]); |
|
| 64 | + | assert mem::cmp(input, &target[1 .. length + 1]) == order; |
|
| 65 | + | assert mem::cmp(&target[1 .. length + 1], input) == -order; |
|
| 66 | + | set target[1 + i] = saved; |
|
| 67 | + | } |
|
| 68 | + | } |
|
| 69 | + | } |
|
| 70 | + | let mut tooSmall: [u8; 7] = [0xa5; 7]; |
|
| 71 | + | let mut failed = false; |
|
| 72 | + | try mem::copy(&mut tooSmall[..], &source[.. 8]) catch { |
|
| 73 | + | set failed = true; |
|
| 74 | + | }; |
|
| 75 | + | assert failed; |
|
| 76 | + | for byte in tooSmall { |
|
| 77 | + | assert byte == 0xa5; |
|
| 78 | + | } |
|
| 79 | + | } |
|
| 80 | + | ||
| 81 | + | /// Variable-width encoders preserve byte order and the surrounding bytes. |
|
| 82 | + | @test fn variableIntegerBytes() throws (testing::TestError) { |
|
| 83 | + | let value: u64 = 0x8877665544332211; |
|
| 84 | + | for offset in 0 .. 16 { |
|
| 85 | + | for size in 0 .. 9 { |
|
| 86 | + | let mut bytes: [u8; 24] = [0xa5; 24]; |
|
| 87 | + | mem::writeLe(&mut bytes[..], offset, value, size); |
|
| 88 | + | let mut expected: u64 = 0; |
|
| 89 | + | for i in 0 .. bytes.len { |
|
| 90 | + | if i < offset or i >= offset + size { |
|
| 91 | + | assert bytes[i] == 0xa5; |
|
| 92 | + | } else { |
|
| 93 | + | let shift = ((i - offset) * 8) as u64; |
|
| 94 | + | assert bytes[i] == (value >> shift) as u8; |
|
| 95 | + | set expected |= bytes[i] as u64 << shift; |
|
| 96 | + | } |
|
| 97 | + | } |
|
| 98 | + | assert mem::readLe(&bytes[..], offset, size) == expected; |
|
| 99 | + | } |
|
| 100 | + | } |
|
| 101 | + | } |
std.lib
+1 -0
| 1 | 1 | lib/std.rad |
|
| 2 | 2 | lib/std/char.rad |
|
| 3 | 3 | lib/std/fmt.rad |
|
| 4 | 4 | lib/std/mem.rad |
|
| 5 | + | lib/std/bits.rad |
|
| 5 | 6 | lib/std/mem/heap.rad |
|
| 6 | 7 | lib/std/vec.rad |
|
| 7 | 8 | lib/std/io.rad |
|
| 8 | 9 | lib/std/intrinsics.rad |
|
| 9 | 10 | lib/std/sys.rad |
std.lib.test
+2 -0
| 27 | 27 | lib/std/arch/rv64/image/tests.rad |
|
| 28 | 28 | lib/std/arch/rv64/shared/tests.rad |
|
| 29 | 29 | lib/std/arch/rv64/shared/tests/run.rad |
|
| 30 | 30 | lib/std/arch/rv64/tests/bounds.rad |
|
| 31 | 31 | lib/std/arch/rv64/tests/atomics.rad |
|
| 32 | + | lib/std/bits/tests.rad |
|
| 33 | + | lib/std/mem/tests.rad |
test/native/build.rad
+85 -8
| 1 | 1 | //! Build a native machine fixture with the reusable RV64 backend. |
|
| 2 | 2 | ||
| 3 | - | use std::sys; |
|
| 3 | + | use std::arch::rv64; |
|
| 4 | + | use std::arch::rv64::image; |
|
| 5 | + | use std::collections::dict; |
|
| 4 | 6 | use std::fs; |
|
| 5 | - | use std::lang::il; |
|
| 6 | - | use std::mem::heap; |
|
| 7 | 7 | use std::lang::gen::data; |
|
| 8 | 8 | use std::lang::gen::labels; |
|
| 9 | - | use std::collections::dict; |
|
| 10 | - | use std::arch::rv64; |
|
| 11 | - | use std::arch::rv64::image; |
|
| 9 | + | use std::lang::il; |
|
| 10 | + | use std::mem::heap; |
|
| 11 | + | use std::sys; |
|
| 12 | 12 | ||
| 13 | 13 | /// Native fixture code base and entry address. |
|
| 14 | 14 | constant CODE_BASE: u64 = 0x80010000; |
|
| 15 | 15 | /// Native fixture read-only data base. |
|
| 16 | 16 | constant RO_BASE: u64 = 0x80018000; |
| 98 | 98 | il::Instr::Br { |
|
| 99 | 99 | op: il::CmpOp::Eq, |
|
| 100 | 100 | typ: il::Type::W64, |
|
| 101 | 101 | a: il::Val::Reg(il::Reg { n: 3 }), |
|
| 102 | 102 | b: il::Val::Imm(0), |
|
| 103 | - | thenTarget: 4, |
|
| 103 | + | thenTarget: 5, |
|
| 104 | 104 | thenArgs: NO_ARGS, |
|
| 105 | 105 | elseTarget: 3, |
|
| 106 | 106 | elseArgs: NO_ARGS, |
|
| 107 | 107 | }, |
|
| 108 | 108 | ], |
| 182 | 182 | locs: &[], |
|
| 183 | 183 | preds: &[1], |
|
| 184 | 184 | loopDepth: 0, |
|
| 185 | 185 | }, |
|
| 186 | 186 | ]; |
|
| 187 | + | let mut allBlocks: *mut [il::Block] = &mut []; |
|
| 188 | + | try! allBlocks.extend(blocks, scratch); |
|
| 189 | + | let setup: *[il::Instr] = &[ |
|
| 190 | + | il::Instr::Copy { |
|
| 191 | + | dst: il::Reg { n: 12 }, val: il::Val::DataSym("fixture::zero") |
|
| 192 | + | }, |
|
| 193 | + | il::Instr::Field { |
|
| 194 | + | dst: il::Reg { n: 13 }, src: il::Reg { n: 12 }, offset: 16 |
|
| 195 | + | }, |
|
| 196 | + | il::Instr::Store { |
|
| 197 | + | typ: il::Type::W64, |
|
| 198 | + | src: il::Val::Reg(il::Reg { n: 13 }), |
|
| 199 | + | dst: il::Reg { n: 12 }, |
|
| 200 | + | offset: 0, |
|
| 201 | + | }, |
|
| 202 | + | il::Instr::Store { |
|
| 203 | + | typ: il::Type::W32, src: il::Val::Imm(16), dst: il::Reg { n: 12 }, offset: 8 |
|
| 204 | + | }, |
|
| 205 | + | il::Instr::Jmp { target: 6, args: NO_ARGS }, |
|
| 206 | + | ]; |
|
| 207 | + | try! allBlocks.append( |
|
| 208 | + | il::Block { |
|
| 209 | + | label: il::BlockLabel { base: "byte.setup", suffix: nil }, |
|
| 210 | + | params: &[], |
|
| 211 | + | instrs: setup, |
|
| 212 | + | locs: &[], |
|
| 213 | + | preds: &[1], |
|
| 214 | + | loopDepth: 0, |
|
| 215 | + | }, |
|
| 216 | + | scratch |
|
| 217 | + | ); |
|
| 218 | + | for typ, width in &[il::Type::W16, il::Type::W32, il::Type::W64] { |
|
| 219 | + | let value: i64 = |
|
| 220 | + | 0x80ff if width == 0 else 0x89abcdef if width == 1 else 0x8877665544332211 as i64; |
|
| 221 | + | for offset in 0 .. 8 { |
|
| 222 | + | let block = 6 + width * 8 + offset; |
|
| 223 | + | let dst = il::Reg { n: 14 + width * 8 + offset }; |
|
| 224 | + | let mut instrs: *mut [il::Instr] = &mut []; |
|
| 225 | + | try! instrs.extend( |
|
| 226 | + | &[ |
|
| 227 | + | il::Instr::BytesStore { |
|
| 228 | + | typ, |
|
| 229 | + | slice: il::Reg { n: 12 }, |
|
| 230 | + | offset: il::Val::Imm(offset as i64), |
|
| 231 | + | value: il::Val::Imm(value), |
|
| 232 | + | }, |
|
| 233 | + | il::Instr::BytesLoad { |
|
| 234 | + | typ, dst, slice: il::Reg { n: 12 }, offset: il::Val::Imm(offset as i64) |
|
| 235 | + | }, |
|
| 236 | + | il::Instr::Br { |
|
| 237 | + | op: il::CmpOp::Eq, |
|
| 238 | + | typ: il::Type::W64, |
|
| 239 | + | a: il::Val::Reg(dst), |
|
| 240 | + | b: il::Val::Imm(value), |
|
| 241 | + | thenTarget: 4 if block == 29 else block + 1, |
|
| 242 | + | thenArgs: NO_ARGS, |
|
| 243 | + | elseTarget: 3, |
|
| 244 | + | elseArgs: NO_ARGS, |
|
| 245 | + | }, |
|
| 246 | + | ], |
|
| 247 | + | scratch |
|
| 248 | + | ); |
|
| 249 | + | let mut preds: *mut [u32] = &mut []; |
|
| 250 | + | try! preds.append(block - 1, scratch); |
|
| 251 | + | try! allBlocks.append( |
|
| 252 | + | il::Block { |
|
| 253 | + | label: il::BlockLabel { base: "byte.access", suffix: block }, |
|
| 254 | + | params: &[], |
|
| 255 | + | instrs, |
|
| 256 | + | locs: &[], |
|
| 257 | + | preds, |
|
| 258 | + | loopDepth: 0, |
|
| 259 | + | }, |
|
| 260 | + | scratch |
|
| 261 | + | ); |
|
| 262 | + | } |
|
| 263 | + | } |
|
| 187 | 264 | let mut funcs: *mut [il::Fn] = &mut []; |
|
| 188 | 265 | try! funcs.append( |
|
| 189 | 266 | il::Fn { |
|
| 190 | 267 | name: "fixture::entry", |
|
| 191 | 268 | params: &[], |
|
| 192 | 269 | returnType: il::ValType::Data(il::Type::W64), |
|
| 193 | 270 | isExtern: false, |
|
| 194 | 271 | isLeaf: true, |
|
| 195 | - | blocks, |
|
| 272 | + | blocks: allBlocks, |
|
| 196 | 273 | vals: &[], |
|
| 197 | 274 | cases: &[], |
|
| 198 | 275 | borrows: &[], |
|
| 199 | 276 | }, |
|
| 200 | 277 | scratch |
test/tests/intrinsic.bits.rad
added
+11 -0
| 1 | + | //! returns: 0 |
|
| 2 | + | //! uses-std |
|
| 3 | + | ||
| 4 | + | use std::bits; |
|
| 5 | + | ||
| 6 | + | /// Check rotations and the zero-input count. |
|
| 7 | + | @default fn main() -> i32 { |
|
| 8 | + | assert bits::rotateLeft32(0x12345678, 36) == 0x23456781; |
|
| 9 | + | assert bits::trailingZeros32(0) == 32; |
|
| 10 | + | return 0; |
|
| 11 | + | } |
test/tests/intrinsic.bytes.load16.end.rad
added
+18 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &[u8], offset: u32) { |
|
| 9 | + | let value = mem::load16Le(&bytes[..], offset); |
|
| 10 | + | assert value == 0; |
|
| 11 | + | } |
|
| 12 | + | ||
| 13 | + | /// Pass an invalid byte range to the checked access. |
|
| 14 | + | @default fn main() -> i32 { |
|
| 15 | + | let bytes: [u8; 2] = [0; 2]; |
|
| 16 | + | access(&bytes[..], 1); |
|
| 17 | + | return 0; |
|
| 18 | + | } |
test/tests/intrinsic.bytes.load16.overflow.rad
added
+18 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &[u8], offset: u32) { |
|
| 9 | + | let value = mem::load16Le(&bytes[..], offset); |
|
| 10 | + | assert value == 0; |
|
| 11 | + | } |
|
| 12 | + | ||
| 13 | + | /// Pass an invalid byte range to the checked access. |
|
| 14 | + | @default fn main() -> i32 { |
|
| 15 | + | let bytes: [u8; 2] = [0; 2]; |
|
| 16 | + | access(&bytes[..], 0xffffffff); |
|
| 17 | + | return 0; |
|
| 18 | + | } |
test/tests/intrinsic.bytes.load16.short.rad
added
+18 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &[u8], offset: u32) { |
|
| 9 | + | let value = mem::load16Le(&bytes[..], offset); |
|
| 10 | + | assert value == 0; |
|
| 11 | + | } |
|
| 12 | + | ||
| 13 | + | /// Pass an invalid byte range to the checked access. |
|
| 14 | + | @default fn main() -> i32 { |
|
| 15 | + | let bytes: [u8; 1] = [0; 1]; |
|
| 16 | + | access(&bytes[..], 0); |
|
| 17 | + | return 0; |
|
| 18 | + | } |
test/tests/intrinsic.bytes.load32.end.rad
added
+18 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &[u8], offset: u32) { |
|
| 9 | + | let value = mem::load32Le(&bytes[..], offset); |
|
| 10 | + | assert value == 0; |
|
| 11 | + | } |
|
| 12 | + | ||
| 13 | + | /// Pass an invalid byte range to the checked access. |
|
| 14 | + | @default fn main() -> i32 { |
|
| 15 | + | let bytes: [u8; 4] = [0; 4]; |
|
| 16 | + | access(&bytes[..], 1); |
|
| 17 | + | return 0; |
|
| 18 | + | } |
test/tests/intrinsic.bytes.load32.overflow.rad
added
+18 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &[u8], offset: u32) { |
|
| 9 | + | let value = mem::load32Le(&bytes[..], offset); |
|
| 10 | + | assert value == 0; |
|
| 11 | + | } |
|
| 12 | + | ||
| 13 | + | /// Pass an invalid byte range to the checked access. |
|
| 14 | + | @default fn main() -> i32 { |
|
| 15 | + | let bytes: [u8; 4] = [0; 4]; |
|
| 16 | + | access(&bytes[..], 0xffffffff); |
|
| 17 | + | return 0; |
|
| 18 | + | } |
test/tests/intrinsic.bytes.load32.short.rad
added
+18 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &[u8], offset: u32) { |
|
| 9 | + | let value = mem::load32Le(&bytes[..], offset); |
|
| 10 | + | assert value == 0; |
|
| 11 | + | } |
|
| 12 | + | ||
| 13 | + | /// Pass an invalid byte range to the checked access. |
|
| 14 | + | @default fn main() -> i32 { |
|
| 15 | + | let bytes: [u8; 3] = [0; 3]; |
|
| 16 | + | access(&bytes[..], 0); |
|
| 17 | + | return 0; |
|
| 18 | + | } |
test/tests/intrinsic.bytes.load64.end.rad
added
+18 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &[u8], offset: u32) { |
|
| 9 | + | let value = mem::load64Le(&bytes[..], offset); |
|
| 10 | + | assert value == 0; |
|
| 11 | + | } |
|
| 12 | + | ||
| 13 | + | /// Pass an invalid byte range to the checked access. |
|
| 14 | + | @default fn main() -> i32 { |
|
| 15 | + | let bytes: [u8; 8] = [0; 8]; |
|
| 16 | + | access(&bytes[..], 1); |
|
| 17 | + | return 0; |
|
| 18 | + | } |
test/tests/intrinsic.bytes.load64.overflow.rad
added
+18 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &[u8], offset: u32) { |
|
| 9 | + | let value = mem::load64Le(&bytes[..], offset); |
|
| 10 | + | assert value == 0; |
|
| 11 | + | } |
|
| 12 | + | ||
| 13 | + | /// Pass an invalid byte range to the checked access. |
|
| 14 | + | @default fn main() -> i32 { |
|
| 15 | + | let bytes: [u8; 8] = [0; 8]; |
|
| 16 | + | access(&bytes[..], 0xffffffff); |
|
| 17 | + | return 0; |
|
| 18 | + | } |
test/tests/intrinsic.bytes.load64.short.rad
added
+18 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &[u8], offset: u32) { |
|
| 9 | + | let value = mem::load64Le(&bytes[..], offset); |
|
| 10 | + | assert value == 0; |
|
| 11 | + | } |
|
| 12 | + | ||
| 13 | + | /// Pass an invalid byte range to the checked access. |
|
| 14 | + | @default fn main() -> i32 { |
|
| 15 | + | let bytes: [u8; 7] = [0; 7]; |
|
| 16 | + | access(&bytes[..], 0); |
|
| 17 | + | return 0; |
|
| 18 | + | } |
test/tests/intrinsic.bytes.rad
added
+15 -0
| 1 | + | //! returns: 0 |
|
| 2 | + | //! uses-std |
|
| 3 | + | ||
| 4 | + | use std::mem; |
|
| 5 | + | ||
| 6 | + | /// Check unaligned access and the last valid byte range. |
|
| 7 | + | @default fn main() -> i32 { |
|
| 8 | + | let mut bytes: [u8; 9] = [0; 9]; |
|
| 9 | + | mem::store64Le(&mut bytes[..], 1, 0x8877665544332211); |
|
| 10 | + | assert bytes[0] == 0; |
|
| 11 | + | assert bytes[1] == 0x11; |
|
| 12 | + | assert bytes[8] == 0x88; |
|
| 13 | + | assert mem::load64Le(&bytes[..], 1) == 0x8877665544332211; |
|
| 14 | + | return 0; |
|
| 15 | + | } |
test/tests/intrinsic.bytes.store16.end.rad
added
+17 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &mut [u8], offset: u32) { |
|
| 9 | + | mem::store16Le(&mut bytes[..], offset, 1); |
|
| 10 | + | } |
|
| 11 | + | ||
| 12 | + | /// Pass an invalid byte range to the checked access. |
|
| 13 | + | @default fn main() -> i32 { |
|
| 14 | + | let mut bytes: [u8; 2] = [0; 2]; |
|
| 15 | + | access(&mut bytes[..], 1); |
|
| 16 | + | return 0; |
|
| 17 | + | } |
test/tests/intrinsic.bytes.store16.overflow.rad
added
+17 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &mut [u8], offset: u32) { |
|
| 9 | + | mem::store16Le(&mut bytes[..], offset, 1); |
|
| 10 | + | } |
|
| 11 | + | ||
| 12 | + | /// Pass an invalid byte range to the checked access. |
|
| 13 | + | @default fn main() -> i32 { |
|
| 14 | + | let mut bytes: [u8; 2] = [0; 2]; |
|
| 15 | + | access(&mut bytes[..], 0xffffffff); |
|
| 16 | + | return 0; |
|
| 17 | + | } |
test/tests/intrinsic.bytes.store16.short.rad
added
+17 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &mut [u8], offset: u32) { |
|
| 9 | + | mem::store16Le(&mut bytes[..], offset, 1); |
|
| 10 | + | } |
|
| 11 | + | ||
| 12 | + | /// Pass an invalid byte range to the checked access. |
|
| 13 | + | @default fn main() -> i32 { |
|
| 14 | + | let mut bytes: [u8; 1] = [0; 1]; |
|
| 15 | + | access(&mut bytes[..], 0); |
|
| 16 | + | return 0; |
|
| 17 | + | } |
test/tests/intrinsic.bytes.store32.end.rad
added
+17 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &mut [u8], offset: u32) { |
|
| 9 | + | mem::store32Le(&mut bytes[..], offset, 1); |
|
| 10 | + | } |
|
| 11 | + | ||
| 12 | + | /// Pass an invalid byte range to the checked access. |
|
| 13 | + | @default fn main() -> i32 { |
|
| 14 | + | let mut bytes: [u8; 4] = [0; 4]; |
|
| 15 | + | access(&mut bytes[..], 1); |
|
| 16 | + | return 0; |
|
| 17 | + | } |
test/tests/intrinsic.bytes.store32.overflow.rad
added
+17 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &mut [u8], offset: u32) { |
|
| 9 | + | mem::store32Le(&mut bytes[..], offset, 1); |
|
| 10 | + | } |
|
| 11 | + | ||
| 12 | + | /// Pass an invalid byte range to the checked access. |
|
| 13 | + | @default fn main() -> i32 { |
|
| 14 | + | let mut bytes: [u8; 4] = [0; 4]; |
|
| 15 | + | access(&mut bytes[..], 0xffffffff); |
|
| 16 | + | return 0; |
|
| 17 | + | } |
test/tests/intrinsic.bytes.store32.short.rad
added
+17 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &mut [u8], offset: u32) { |
|
| 9 | + | mem::store32Le(&mut bytes[..], offset, 1); |
|
| 10 | + | } |
|
| 11 | + | ||
| 12 | + | /// Pass an invalid byte range to the checked access. |
|
| 13 | + | @default fn main() -> i32 { |
|
| 14 | + | let mut bytes: [u8; 3] = [0; 3]; |
|
| 15 | + | access(&mut bytes[..], 0); |
|
| 16 | + | return 0; |
|
| 17 | + | } |
test/tests/intrinsic.bytes.store64.end.rad
added
+17 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &mut [u8], offset: u32) { |
|
| 9 | + | mem::store64Le(&mut bytes[..], offset, 1); |
|
| 10 | + | } |
|
| 11 | + | ||
| 12 | + | /// Pass an invalid byte range to the checked access. |
|
| 13 | + | @default fn main() -> i32 { |
|
| 14 | + | let mut bytes: [u8; 8] = [0; 8]; |
|
| 15 | + | access(&mut bytes[..], 1); |
|
| 16 | + | return 0; |
|
| 17 | + | } |
test/tests/intrinsic.bytes.store64.overflow.rad
added
+17 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &mut [u8], offset: u32) { |
|
| 9 | + | mem::store64Le(&mut bytes[..], offset, 1); |
|
| 10 | + | } |
|
| 11 | + | ||
| 12 | + | /// Pass an invalid byte range to the checked access. |
|
| 13 | + | @default fn main() -> i32 { |
|
| 14 | + | let mut bytes: [u8; 8] = [0; 8]; |
|
| 15 | + | access(&mut bytes[..], 0xffffffff); |
|
| 16 | + | return 0; |
|
| 17 | + | } |
test/tests/intrinsic.bytes.store64.short.rad
added
+17 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A byte range outside the slice traps. |
|
| 4 | + | ||
| 5 | + | use std::mem; |
|
| 6 | + | ||
| 7 | + | /// Check a runtime offset on a byte slice. |
|
| 8 | + | fn access(bytes: &mut [u8], offset: u32) { |
|
| 9 | + | mem::store64Le(&mut bytes[..], offset, 1); |
|
| 10 | + | } |
|
| 11 | + | ||
| 12 | + | /// Pass an invalid byte range to the checked access. |
|
| 13 | + | @default fn main() -> i32 { |
|
| 14 | + | let mut bytes: [u8; 7] = [0; 7]; |
|
| 15 | + | access(&mut bytes[..], 0); |
|
| 16 | + | return 0; |
|
| 17 | + | } |