lang: Verify calls through trait objects
2c3824576a5fff1edc168d3da6a7c391e057135c38c3572e52aedbc5374684a0
A trait object was a plain pair of pointers in RIL, and a method call loaded its function from the table and called it indirectly. The verifier could not tie the table to the receiver, so it rejected every safe program that calls through a trait object, among them the allocator calls of slice growth. Add a trait slot to layouts: 16 bytes holding the receiver and its method table, written only by the new `trait.new`, `trait.copy` and `trait.call` instructions. A program carries a table of traits, with the signature and receiver rights of each method. `trait.new` requires a read-only method table whose entries match the method signatures with the receiver's type. `trait.call` passes the stored receiver, so a call cannot pair one object's receiver with another object's methods, and traps on an object that was never built. `trait.copy` gives a call argument an object with fewer rights. Mutable data cannot hold trait objects, and the binary format carries the trait table and the new instructions. Trait objects of `*unsafe opaque` type keep the plain layout. Assisted-by: Claude:opus-5.5
1 parent
802eb248
compiler/radiance.rad
+2 -0
| 1341 | 1341 | il::Program { |
|
| 1342 | 1342 | data: &dataItems[..], |
|
| 1343 | 1343 | fns: &[], |
|
| 1344 | 1344 | layouts: tables.layouts, |
|
| 1345 | 1345 | signatures: tables.signatures, |
|
| 1346 | + | traits: tables.traits, |
|
| 1346 | 1347 | }, |
|
| 1347 | 1348 | &exports[..], |
|
| 1348 | 1349 | entry, |
|
| 1349 | 1350 | &mut spool.arena |
|
| 1350 | 1351 | ) catch { |
| 1405 | 1406 | try lowerAllPackagesInto(ctx, modules, &mut low, &mut spool); |
|
| 1406 | 1407 | il::printer::printSpooled( |
|
| 1407 | 1408 | &mut out, |
|
| 1408 | 1409 | &low.tables.layouts[..], |
|
| 1409 | 1410 | &low.tables.signatures[..], |
|
| 1411 | + | &low.tables.traits[..], |
|
| 1410 | 1412 | &low.data[..], |
|
| 1411 | 1413 | &spool.text.buf[.. spool.text.pos] |
|
| 1412 | 1414 | ); |
|
| 1413 | 1415 | io::print("\n"); |
|
| 1414 | 1416 | return; |
kernel/kernel/tests/loader.rad
+1 -1
| 117 | 117 | symbols: &["loaded", "loaded::main", "entry", "missing"], |
|
| 118 | 118 | name: "loaded", |
|
| 119 | 119 | dependencies, |
|
| 120 | 120 | exports, |
|
| 121 | 121 | entry: "loaded::main", |
|
| 122 | - | program: il::Program { data: &[], fns: list, layouts: &[], signatures: &[] }, |
|
| 122 | + | program: il::Program { data: &[], fns: list, layouts: &[], signatures: &[], traits: &[] }, |
|
| 123 | 123 | }; |
|
| 124 | 124 | let bytes = @sliceOf(&mut RAM[0] as *mut u8, @sizeOf([u64; 18874368])); |
|
| 125 | 125 | let mut output = &mut bytes[OFFSET .. OFFSET + 4096] as *mut [u8]; |
|
| 126 | 126 | let length = try! program::encode(&mut output, &package); |
|
| 127 | 127 | return loader::Request { |
lib/std/arch/rv64/isel.rad
+63 -17
| 759 | 759 | try emit::emit(e, encode::ebreak()); |
|
| 760 | 760 | } |
|
| 761 | 761 | return rs2; |
|
| 762 | 762 | } |
|
| 763 | 763 | ||
| 764 | + | /// Byte offset of the receiver in a trait object. |
|
| 765 | + | constant TRAIT_RECV_OFFSET: i32 = 0; |
|
| 766 | + | /// Byte offset of the method table address in a trait object. |
|
| 767 | + | constant TRAIT_TABLE_OFFSET: i32 = 8; |
|
| 764 | 768 | /// Byte offset of the data pointer in a slice object. |
|
| 765 | 769 | constant SLICE_PTR_OFFSET: i32 = 0; |
|
| 766 | 770 | /// Byte offset of the 32-bit length in a slice object. |
|
| 767 | 771 | constant SLICE_LEN_OFFSET: i32 = 8; |
|
| 768 | 772 | /// Byte offset of the 32-bit capacity in a slice object. |
| 1596 | 1600 | // setup can clobber it. |
|
| 1597 | 1601 | if let case il::Val::Reg(r) = callee { |
|
| 1598 | 1602 | let target = try getSrcReg(s, e, r, super::SCRATCH2); |
|
| 1599 | 1603 | try emitMv(s, e, super::SCRATCH2, target); |
|
| 1600 | 1604 | } |
|
| 1601 | - | // Store the arguments beyond A7 on the stack first, because the |
|
| 1602 | - | // register moves below overwrite registers that may hold them. |
|
| 1603 | - | let vals = il::argVals(func, args); |
|
| 1604 | - | try emitStackArgs(s, e, vals); |
|
| 1605 | - | ||
| 1606 | - | // Move arguments to A0-A7 using parallel move resolution. |
|
| 1607 | - | let regCount = super::ARG_REGS.len if vals.len > super::ARG_REGS.len else vals.len; |
|
| 1608 | - | try emitParallelMoves(s, e, &super::ARG_REGS[..], &vals[.. regCount]); |
|
| 1605 | + | try emitCallArgs(s, e, il::argVals(func, args)); |
|
| 1609 | 1606 | ||
| 1610 | 1607 | // Emit call. |
|
| 1611 | 1608 | match callee { |
|
| 1612 | 1609 | case il::Val::FnAddr(name) => { |
|
| 1613 | 1610 | try emit::recordCall(e, name); |
| 1617 | 1614 | } |
|
| 1618 | 1615 | else => { |
|
| 1619 | 1616 | throw super::Error::Invalid; |
|
| 1620 | 1617 | } |
|
| 1621 | 1618 | } |
|
| 1622 | - | // Release the outgoing stack arguments. |
|
| 1623 | - | if s.argAdjust <> 0 { |
|
| 1624 | - | try emit::emitAddImm(e, super::SP, super::SP, s.argAdjust); |
|
| 1625 | - | set s.argAdjust = 0; |
|
| 1626 | - | } |
|
| 1627 | - | // Move result from A0. |
|
| 1628 | - | if let d = dst { |
|
| 1629 | - | let rd = getDstReg(s, d, super::SCRATCH1); |
|
| 1630 | - | try emitMv(s, e, rd, super::A0); |
|
| 1619 | + | try finishCall(s, e, dst); |
|
| 1620 | + | } |
|
| 1621 | + | case il::Instr::TraitCall { dst, obj, method, receiver, args, .. } => { |
|
| 1622 | + | // The receiver and the method stay in scratch registers that the argument |
|
| 1623 | + | // setup does not use, and the receiver goes to its register last. |
|
| 1624 | + | let vals = il::argVals(func, args); |
|
| 1625 | + | if receiver >= super::ARG_REGS.len or receiver >= vals.len { |
|
| 1626 | + | throw super::Error::Invalid; |
|
| 1631 | 1627 | } |
|
| 1628 | + | let robj = try getSrcReg(s, e, obj, super::SCRATCH2); |
|
| 1629 | + | try emit::emitLd(e, super::SCRATCH3, robj, TRAIT_RECV_OFFSET); |
|
| 1630 | + | try emit::emitLd(e, super::SCRATCH2, robj, TRAIT_TABLE_OFFSET); |
|
| 1631 | + | // An object that was never built holds a zero table. A built table |
|
| 1632 | + | // holds every method. |
|
| 1633 | + | try emit::emit(e, encode::bne(super::SCRATCH2, super::ZERO, super::INSTR_SIZE * 2)); |
|
| 1634 | + | try emit::emit(e, encode::ebreak()); |
|
| 1635 | + | try emit::emitLd( |
|
| 1636 | + | e, super::SCRATCH2, super::SCRATCH2, method as i32 * super::DWORD_SIZE |
|
| 1637 | + | ); |
|
| 1638 | + | try emitCallArgs(s, e, vals); |
|
| 1639 | + | try emitMv(s, e, super::ARG_REGS[receiver], super::SCRATCH3); |
|
| 1640 | + | try emit::emit(e, encode::jalr(super::RA, super::SCRATCH2, 0)); |
|
| 1641 | + | try finishCall(s, e, dst); |
|
| 1642 | + | } |
|
| 1643 | + | case il::Instr::TraitCopy { dst, src } => { |
|
| 1644 | + | let rdst = try getSrcReg(s, e, dst, super::SCRATCH2); |
|
| 1645 | + | let rsrc = try getSrcReg(s, e, src, super::SCRATCH1); |
|
| 1646 | + | try emit::emitLd(e, super::SCRATCH3, rsrc, TRAIT_RECV_OFFSET); |
|
| 1647 | + | try emit::emitSd(e, super::SCRATCH3, rdst, TRAIT_RECV_OFFSET); |
|
| 1648 | + | try emit::emitLd(e, super::SCRATCH3, rsrc, TRAIT_TABLE_OFFSET); |
|
| 1649 | + | try emit::emitSd(e, super::SCRATCH3, rdst, TRAIT_TABLE_OFFSET); |
|
| 1650 | + | } |
|
| 1651 | + | case il::Instr::TraitNew { dst, recv, table } => { |
|
| 1652 | + | let base = try getSrcReg(s, e, dst, super::SCRATCH2); |
|
| 1653 | + | let rr = try getSrcReg(s, e, recv, super::SCRATCH1); |
|
| 1654 | + | try emit::emitStore(e, rr, base, TRAIT_RECV_OFFSET, il::Type::W64); |
|
| 1655 | + | let rt = try resolveVal(s, e, super::SCRATCH1, table); |
|
| 1656 | + | try emit::emitStore(e, rt, base, TRAIT_TABLE_OFFSET, il::Type::W64); |
|
| 1632 | 1657 | } |
|
| 1633 | 1658 | case il::Instr::Ecall { dst, num, a0, a1, a2, a3, buf } => { |
|
| 1634 | 1659 | // The kernel reads and writes caller memory only through the buffer registers, |
|
| 1635 | 1660 | // which are zero when the call has no buffer. |
|
| 1636 | 1661 | let mut bufPtr = il::Val::Imm(0); |
| 2086 | 2111 | try emit::emitSd(e, rs, super::SP, i as i32 * super::DWORD_SIZE); |
|
| 2087 | 2112 | } |
|
| 2088 | 2113 | } |
|
| 2089 | 2114 | } |
|
| 2090 | 2115 | ||
| 2116 | + | /// Move the arguments of a call to the stack and to A0-A7. The stack arguments |
|
| 2117 | + | /// come first, because the register moves overwrite registers that may hold them. |
|
| 2118 | + | /// Neither step uses SCRATCH2 or SCRATCH3. |
|
| 2119 | + | fn emitCallArgs(s: &mut Selector, e: &mut emit::Emitter, vals: &[il::Val]) throws (super::Error) { |
|
| 2120 | + | try emitStackArgs(s, e, vals); |
|
| 2121 | + | let regCount = super::ARG_REGS.len if vals.len > super::ARG_REGS.len else vals.len; |
|
| 2122 | + | try emitParallelMoves(s, e, &super::ARG_REGS[..], &vals[.. regCount]); |
|
| 2123 | + | } |
|
| 2124 | + | ||
| 2125 | + | /// Release the outgoing stack arguments of a call, and move its result from A0. |
|
| 2126 | + | fn finishCall(s: &mut Selector, e: &mut emit::Emitter, dst: ?il::Reg) throws (super::Error) { |
|
| 2127 | + | if s.argAdjust <> 0 { |
|
| 2128 | + | try emit::emitAddImm(e, super::SP, super::SP, s.argAdjust); |
|
| 2129 | + | set s.argAdjust = 0; |
|
| 2130 | + | } |
|
| 2131 | + | if let d = dst { |
|
| 2132 | + | let rd = getDstReg(s, d, super::SCRATCH1); |
|
| 2133 | + | try emitMv(s, e, rd, super::A0); |
|
| 2134 | + | } |
|
| 2135 | + | } |
|
| 2136 | + | ||
| 2091 | 2137 | /// Resolve parallel moves from IL values to physical destination registers. |
|
| 2092 | 2138 | /// |
|
| 2093 | 2139 | /// The parallel move problem arises when moving values between registers where |
|
| 2094 | 2140 | /// there may be dependencies (e.g. moving A0 to A1 and A1 to A0 simultaneously). |
|
| 2095 | 2141 | /// |
lib/std/arch/rv64/shared/tests.rad
+2 -1
| 36 | 36 | name: "p", |
|
| 37 | 37 | dependencies: &[], |
|
| 38 | 38 | exports: &[binary::Export { name: "p::entry", kind: binary::ExportKind::Function }], |
|
| 39 | 39 | entry: "p::entry", |
|
| 40 | 40 | program: il::Program { |
|
| 41 | - | data: &[], fns: &[], layouts: &[], signatures: &[] |
|
| 41 | + | data: &[], fns: &[], layouts: &[], signatures: &[], traits: &[] |
|
| 42 | 42 | }, |
|
| 43 | 43 | }; |
|
| 44 | 44 | let assembly = asm::Program { |
|
| 45 | 45 | text: &[0x02a00513 as u32, 0x00008067], |
|
| 46 | 46 | data: &[], |
| 372 | 372 | layouts: &[ |
|
| 373 | 373 | il::Layout { size: 32, alignment: 8, slots: &[] }, |
|
| 374 | 374 | il::Layout { size: 16, alignment: 16, slots: &[] }, |
|
| 375 | 375 | ], |
|
| 376 | 376 | signatures: &[], |
|
| 377 | + | traits: &[], |
|
| 377 | 378 | }, |
|
| 378 | 379 | }; |
|
| 379 | 380 | } |
|
| 380 | 381 | ||
| 381 | 382 | /// Compile into small persistent tables. |
lib/std/arch/rv64/shared/tests/run.rad
+48 -5
| 290 | 290 | unsafe fn compile(instr: il::Instr, paramCount: u32, result: ?il::Val) -> shared::Package |
|
| 291 | 291 | throws (shared::Error) |
|
| 292 | 292 | { |
|
| 293 | 293 | return try compileBody(&[instr, il::Instr::Ret { val: result }], paramCount, &[ |
|
| 294 | 294 | il::Layout { size: ELEMENT as u32, alignment: 8, slots: &[] } |
|
| 295 | - | ]); |
|
| 295 | + | ], &[]); |
|
| 296 | 296 | } |
|
| 297 | 297 | ||
| 298 | 298 | /// Compile a function of `paramCount` data parameters with the instructions `body`, which end |
|
| 299 | - | /// with a return, into a package with two data symbols and the layout table `layouts`. |
|
| 299 | + | /// with a return, into a package with two data symbols and the layout table `layouts`. The |
|
| 300 | + | /// argument ranges of the instructions select from `vals`. |
|
| 300 | 301 | unsafe fn compileBody( |
|
| 301 | - | body: &[il::Instr], paramCount: u32, layouts: *[il::Layout] |
|
| 302 | + | body: &[il::Instr], paramCount: u32, layouts: *[il::Layout], vals: &[il::Val] |
|
| 302 | 303 | ) -> shared::Package throws (shared::Error) { |
|
| 303 | 304 | let mut ilArena = alloc::new(&mut IL[..]); |
|
| 304 | 305 | let mut params: *mut [il::Param] = &mut []; |
|
| 305 | 306 | for n in 0 .. paramCount { |
|
| 306 | 307 | try! params.append(dataParam(n), &mut ilArena); |
|
| 307 | 308 | } |
|
| 308 | 309 | let mut instrs: *mut [il::Instr] = &mut []; |
|
| 309 | 310 | try! instrs.extend(body, &mut ilArena); |
|
| 311 | + | let mut pool: *mut [il::Val] = &mut []; |
|
| 312 | + | try! pool.extend(vals, &mut ilArena); |
|
| 310 | 313 | let mut blocks: *mut [il::Block] = &mut []; |
|
| 311 | 314 | try! blocks.append( |
|
| 312 | 315 | il::Block { |
|
| 313 | 316 | label: il::BlockLabel { base: "b", suffix: nil }, |
|
| 314 | 317 | params: &[], |
| 326 | 329 | params: ¶ms[..], |
|
| 327 | 330 | returnType: il::ValType::Data(il::Type::W64), |
|
| 328 | 331 | isExtern: false, |
|
| 329 | 332 | isLeaf: false, |
|
| 330 | 333 | blocks: &blocks[..], |
|
| 331 | - | vals: &[], |
|
| 334 | + | vals: &pool[..], |
|
| 332 | 335 | cases: &[], |
|
| 333 | 336 | }, |
|
| 334 | 337 | &mut ilArena |
|
| 335 | 338 | ); |
|
| 336 | 339 | let mut fns: *mut [*il::Fn] = &mut []; |
| 367 | 370 | }, |
|
| 368 | 371 | ], |
|
| 369 | 372 | fns: &fns[..], |
|
| 370 | 373 | layouts, |
|
| 371 | 374 | signatures: &[], |
|
| 375 | + | traits: &[], |
|
| 372 | 376 | }, |
|
| 373 | 377 | }; |
|
| 374 | 378 | let arena = alloc::new(&mut tests::ARENA[..]); |
|
| 375 | 379 | let mut scratch = alloc::new(&mut tests::SCRATCH[..]); |
|
| 376 | 380 | return try shared::compile( |
| 701 | 705 | }, |
|
| 702 | 706 | &mut arena |
|
| 703 | 707 | ); |
|
| 704 | 708 | try! body.append(il::Instr::Ret { val: il::Val::Reg(il::Reg { n: next }) }, &mut arena); |
|
| 705 | 709 | set COPIED[0] = il::Layout { size, alignment: boundary, slots: &[] }; |
|
| 706 | - | return try compileBody(&body[..], 2, &COPIED[..] as *[il::Layout]); |
|
| 710 | + | return try compileBody(&body[..], 2, &COPIED[..] as *[il::Layout], &[]); |
|
| 707 | 711 | } |
|
| 708 | 712 | ||
| 709 | 713 | /// The byte at `address` of the modeled memory. |
|
| 710 | 714 | unsafe fn byteAt(address: u64) -> u8 { |
|
| 711 | 715 | let value = load(address, 1) else panic "byteAt: outside memory"; |
| 821 | 825 | try expectBlit(large, boundary, mode); |
|
| 822 | 826 | } |
|
| 823 | 827 | } |
|
| 824 | 828 | } |
|
| 825 | 829 | } |
|
| 830 | + | ||
| 831 | + | /// Address of the trait object of a trait call check. |
|
| 832 | + | constant OBJECT_AT: u64 = 0x1100; |
|
| 833 | + | /// Address of the method table of a trait call check. |
|
| 834 | + | constant TABLE_AT: u64 = 0x1200; |
|
| 835 | + | /// Receiver of the trait object of a trait call check. |
|
| 836 | + | constant RECEIVER: u64 = 0x77; |
|
| 837 | + | ||
| 838 | + | /// A trait call passes the receiver of the object at its position among the arguments and |
|
| 839 | + | /// calls the method from the table. An object that was never built has a zero table, and |
|
| 840 | + | /// the call traps instead of jumping through it. |
|
| 841 | + | @test unsafe fn traitCallReceiver() throws (testing::TestError) { |
|
| 842 | + | let call = il::Instr::TraitCall { |
|
| 843 | + | retTy: il::ValType::Data(il::Type::W64), |
|
| 844 | + | dst: il::Reg { n: 1 }, |
|
| 845 | + | obj: il::Reg { n: 0 }, |
|
| 846 | + | method: 1, |
|
| 847 | + | receiver: 1, |
|
| 848 | + | args: il::ArgRange { start: 0, len: 2 }, |
|
| 849 | + | }; |
|
| 850 | + | let body = [call, il::Instr::Ret { val: il::Val::Reg(il::Reg { n: 1 }) }]; |
|
| 851 | + | let package = try compileBody( |
|
| 852 | + | &body[..], |
|
| 853 | + | 1, |
|
| 854 | + | &[il::Layout { size: ELEMENT as u32, alignment: 8, slots: &[] }], |
|
| 855 | + | &[il::Val::Imm(5), il::Val::Undef] |
|
| 856 | + | ) catch { |
|
| 857 | + | throw testing::TestError::Failed; |
|
| 858 | + | }; |
|
| 859 | + | let mut m = boot(&[OBJECT_AT]); |
|
| 860 | + | try testing::expect(store(OBJECT_AT, 8, RECEIVER)); |
|
| 861 | + | try testing::expect(store(OBJECT_AT + 8, 8, TABLE_AT)); |
|
| 862 | + | try testing::expect(store(TABLE_AT + 8, 8, RETURN)); |
|
| 863 | + | try testing::expect(returned(run(package.code, &mut m), 5)); |
|
| 864 | + | try testing::expect(read(&m, rv64::A1) == RECEIVER); |
|
| 865 | + | ||
| 866 | + | let mut zero = boot(&[OBJECT_AT]); |
|
| 867 | + | try testing::expect(trapped(run(package.code, &mut zero))); |
|
| 868 | + | } |
lib/std/lang/il.rad
+95 -0
| 171 | 171 | mutable: bool, |
|
| 172 | 172 | /// Whether the pointer can be zero. A slot of a layout is always nullable. |
|
| 173 | 173 | nullable: bool, |
|
| 174 | 174 | } |
|
| 175 | 175 | ||
| 176 | + | /// Type of a trait object slot: a receiver and the method table of its trait. |
|
| 177 | + | export record ObjectType: Copy { |
|
| 178 | + | /// Index of the trait in `Program.traits`. |
|
| 179 | + | iface: u32, |
|
| 180 | + | /// Whether the receiver lives as long as the domain. Otherwise it is a |
|
| 181 | + | /// reference, which can point into a stack frame. |
|
| 182 | + | own: bool, |
|
| 183 | + | /// Whether the receiver can be written through the object. |
|
| 184 | + | mutable: bool, |
|
| 185 | + | } |
|
| 186 | + | ||
| 176 | 187 | /// Type of a value that crosses a function or block boundary. |
|
| 177 | 188 | export union ValType: Copy { |
|
| 178 | 189 | /// No value. Only the result of a function that returns nothing. |
|
| 179 | 190 | Void, |
|
| 180 | 191 | /// Plain data of a given width. |
| 414 | 425 | /// Address of the object that holds the slot. |
|
| 415 | 426 | dst: Reg, |
|
| 416 | 427 | /// Byte offset of the slot. |
|
| 417 | 428 | offset: i32, |
|
| 418 | 429 | }, |
|
| 430 | + | /// Build a trait object: `trait.new %dst %recv <table>;`. |
|
| 431 | + | /// |
|
| 432 | + | /// `dst` addresses a trait object slot. The instruction writes the receiver |
|
| 433 | + | /// `recv` and the address of the method table `table`, a read-only data |
|
| 434 | + | /// symbol that holds one function of the receiver's type for each method. |
|
| 435 | + | TraitNew { |
|
| 436 | + | /// Address of the trait object slot. |
|
| 437 | + | dst: Reg, |
|
| 438 | + | /// Receiver. |
|
| 439 | + | recv: Reg, |
|
| 440 | + | /// Method table. |
|
| 441 | + | table: Val, |
|
| 442 | + | }, |
|
| 443 | + | /// Copy a trait object: `trait.copy %dst %src;`. |
|
| 444 | + | /// |
|
| 445 | + | /// `dst` and `src` address trait object slots of the same trait. The slot at |
|
| 446 | + | /// `dst` has the rights of the slot at `src` or fewer. |
|
| 447 | + | TraitCopy { |
|
| 448 | + | /// Address of the slot that receives the object. |
|
| 449 | + | dst: Reg, |
|
| 450 | + | /// Address of the slot that holds the object. |
|
| 451 | + | src: Reg, |
|
| 452 | + | }, |
|
| 453 | + | /// Call a method of a trait object: |
|
| 454 | + | /// `trait.call <type> %dst %obj <method> at <receiver>(<arg>...);`. |
|
| 455 | + | /// |
|
| 456 | + | /// `obj` addresses a trait object slot. The argument at position `receiver` |
|
| 457 | + | /// is `undef`; the call passes the receiver of the object there. The |
|
| 458 | + | /// instruction traps when the object holds no method table. |
|
| 459 | + | TraitCall { |
|
| 460 | + | /// Return type. |
|
| 461 | + | retTy: ValType, |
|
| 462 | + | /// Holds return value, for non-void methods. |
|
| 463 | + | dst: ?Reg, |
|
| 464 | + | /// Address of the trait object slot. |
|
| 465 | + | obj: Reg, |
|
| 466 | + | /// Index of the method in the trait. |
|
| 467 | + | method: u32, |
|
| 468 | + | /// Position of the receiver among the arguments. |
|
| 469 | + | receiver: u32, |
|
| 470 | + | /// Arguments, with `undef` at the receiver position. |
|
| 471 | + | args: ArgRange, |
|
| 472 | + | }, |
|
| 419 | 473 | ||
| 420 | 474 | ///////////////////// |
|
| 421 | 475 | // ALU operations // |
|
| 422 | 476 | ///////////////////// |
|
| 423 | 477 |
| 680 | 734 | /// and a 32-bit capacity at offset 12. The layout of the pointer type |
|
| 681 | 735 | /// describes one element. |
|
| 682 | 736 | Slice(PtrType), |
|
| 683 | 737 | /// Function value of 8 bytes. Holds the index of its signature. |
|
| 684 | 738 | Fn(u32), |
|
| 739 | + | /// Trait object of 16 bytes: the receiver at offset 0 and the address of |
|
| 740 | + | /// a method table at offset 8. Only `trait.new` writes it, and only a copy |
|
| 741 | + | /// of the whole slot moves it. |
|
| 742 | + | Trait(ObjectType), |
|
| 685 | 743 | /// Nested object that contains slots, at the offset of the slot. Holds the |
|
| 686 | 744 | /// index of its layout, which is lower than the index of the enclosing layout. |
|
| 687 | 745 | Sub(u32), |
|
| 688 | 746 | /// Run of `count` consecutive objects of a layout that contains slots, |
|
| 689 | 747 | /// starting at the offset of the slot. The layout index is lower than the |
| 723 | 781 | params: *[ValType], |
|
| 724 | 782 | /// Result type. |
|
| 725 | 783 | ret: ValType, |
|
| 726 | 784 | } |
|
| 727 | 785 | ||
| 786 | + | /// A method of a trait: the type of its functions without the receiver. |
|
| 787 | + | export record Method: Copy { |
|
| 788 | + | /// Index in `Program.signatures` of the function type without the receiver. |
|
| 789 | + | sig: u32, |
|
| 790 | + | /// Position of the receiver among the parameters of the functions. |
|
| 791 | + | receiver: u32, |
|
| 792 | + | /// Whether the receiver must own its object. |
|
| 793 | + | own: bool, |
|
| 794 | + | /// Whether the receiver must be writable. |
|
| 795 | + | mutable: bool, |
|
| 796 | + | } |
|
| 797 | + | ||
| 798 | + | /// The methods of a trait, in method table order. |
|
| 799 | + | export record Trait: Copy { |
|
| 800 | + | /// Methods. |
|
| 801 | + | methods: *[Method], |
|
| 802 | + | } |
|
| 803 | + | ||
| 728 | 804 | /// An IL program (compilation unit). |
|
| 729 | 805 | export record Program: Copy { |
|
| 730 | 806 | /// Immutable global data. |
|
| 731 | 807 | data: *[Data], |
|
| 732 | 808 | /// Functions. |
|
| 733 | 809 | fns: *[*Fn], |
|
| 734 | 810 | /// Layout table. Indices in functions and data refer to it. |
|
| 735 | 811 | layouts: *[Layout], |
|
| 736 | 812 | /// Signature table. Function value types refer to it. |
|
| 737 | 813 | signatures: *[Signature], |
|
| 814 | + | /// Trait table. Trait object slots refer to it. |
|
| 815 | + | traits: *[Trait], |
|
| 738 | 816 | } |
|
| 739 | 817 | ||
| 740 | 818 | /////////////////////// |
|
| 741 | 819 | // Utility Functions // |
|
| 742 | 820 | /////////////////////// |
| 766 | 844 | case Instr::BinOp { dst, .. } => return dst, |
|
| 767 | 845 | case Instr::UnOp { dst, .. } => return dst, |
|
| 768 | 846 | case Instr::Zext { dst, .. } => return dst, |
|
| 769 | 847 | case Instr::Sext { dst, .. } => return dst, |
|
| 770 | 848 | case Instr::Call { dst, .. } => return dst, |
|
| 849 | + | case Instr::TraitCall { dst, .. } => return dst, |
|
| 771 | 850 | case Instr::Ecall { dst, .. } => return dst, |
|
| 772 | 851 | case Instr::PageSlice { err, .. } => return err, |
|
| 773 | 852 | case Instr::DeviceRead { dst, .. } => return dst, |
|
| 774 | 853 | else => return nil, |
|
| 775 | 854 | } |
| 778 | 857 | /// Check if an instruction is a function call. |
|
| 779 | 858 | export fn isCall(instr: Instr) -> bool { |
|
| 780 | 859 | match instr { |
|
| 781 | 860 | case |
|
| 782 | 861 | Instr::Call { .. }, |
|
| 862 | + | Instr::TraitCall { .. }, |
|
| 783 | 863 | Instr::Ecall { .. }, |
|
| 784 | 864 | Instr::PageSlice { .. }, |
|
| 785 | 865 | Instr::DeviceRead { .. }, |
|
| 786 | 866 | Instr::DeviceWrite { .. } => return true, |
|
| 787 | 867 | else => return false, |
| 895 | 975 | case Instr::LoadPtr { src, .. } => visitor.visit(src), |
|
| 896 | 976 | case Instr::StorePtr { src, dst, .. } => { |
|
| 897 | 977 | withReg(src, visitor); |
|
| 898 | 978 | visitor.visit(dst); |
|
| 899 | 979 | } |
|
| 980 | + | case Instr::TraitNew { dst, recv, table } => { |
|
| 981 | + | visitor.visit(dst); |
|
| 982 | + | visitor.visit(recv); |
|
| 983 | + | withReg(table, visitor); |
|
| 984 | + | } |
|
| 985 | + | case Instr::TraitCopy { dst, src } => { |
|
| 986 | + | visitor.visit(dst); |
|
| 987 | + | visitor.visit(src); |
|
| 988 | + | } |
|
| 989 | + | case Instr::TraitCall { obj, args, .. } => { |
|
| 990 | + | visitor.visit(obj); |
|
| 991 | + | for arg in argVals(func, args) { |
|
| 992 | + | withReg(arg, visitor); |
|
| 993 | + | } |
|
| 994 | + | } |
|
| 900 | 995 | case Instr::BinOp { a, b, .. } => { |
|
| 901 | 996 | withReg(a, visitor); |
|
| 902 | 997 | withReg(b, visitor); |
|
| 903 | 998 | } |
|
| 904 | 999 | case Instr::UnOp { a, .. } => |
lib/std/lang/il/binary.rad
+12 -0
| 27 | 27 | export constant MIN_EXPORT_BYTES: u32 = 5; |
|
| 28 | 28 | /// Minimum wire bytes in a layout record. |
|
| 29 | 29 | export constant MIN_LAYOUT_BYTES: u32 = 12; |
|
| 30 | 30 | /// Minimum wire bytes in a function signature. |
|
| 31 | 31 | export constant MIN_SIGNATURE_BYTES: u32 = 5; |
|
| 32 | + | /// Minimum wire bytes in a trait record. |
|
| 33 | + | export constant MIN_TRAIT_BYTES: u32 = 4; |
|
| 34 | + | /// Minimum wire bytes in a trait method. |
|
| 35 | + | export constant MIN_METHOD_BYTES: u32 = 9; |
|
| 32 | 36 | /// Minimum wire bytes in a switch case. |
|
| 33 | 37 | export constant MIN_SWITCH_CASE_BYTES: u32 = 16; |
|
| 34 | 38 | ||
| 35 | 39 | /// Binary RIL signature, encoded as the bytes RIL followed by zero. |
|
| 36 | 40 | export constant MAGIC: u32 = 0x004c4952; |
| 120 | 124 | export constant SLOT_FN: u8 = 2; |
|
| 121 | 125 | /// Wire tag for slot kind Sub. |
|
| 122 | 126 | export constant SLOT_SUB: u8 = 3; |
|
| 123 | 127 | /// Wire tag for slot kind Array. |
|
| 124 | 128 | export constant SLOT_ARRAY: u8 = 4; |
|
| 129 | + | /// Wire tag for slot kind Trait. |
|
| 130 | + | export constant SLOT_TRAIT: u8 = 5; |
|
| 125 | 131 | ||
| 126 | 132 | /// Pointer type flag: the pointee lives as long as the domain. |
|
| 127 | 133 | export constant PTR_OWN: u8 = 1; |
|
| 128 | 134 | /// Pointer type flag: the pointee can be written. |
|
| 129 | 135 | export constant PTR_MUTABLE: u8 = 2; |
| 144 | 150 | export constant INSTR_LOAD_PTR: u8 = 26; |
|
| 145 | 151 | /// Wire tag for instruction StorePtr. |
|
| 146 | 152 | export constant INSTR_STORE_PTR: u8 = 27; |
|
| 147 | 153 | /// Wire tag for instruction PageSlice. |
|
| 148 | 154 | export constant INSTR_PAGE_SLICE: u8 = 28; |
|
| 155 | + | /// Wire tag for instruction TraitNew. |
|
| 156 | + | export constant INSTR_TRAIT_NEW: u8 = 29; |
|
| 157 | + | /// Wire tag for instruction TraitCall. |
|
| 158 | + | export constant INSTR_TRAIT_CALL: u8 = 30; |
|
| 159 | + | /// Wire tag for instruction TraitCopy. |
|
| 160 | + | export constant INSTR_TRAIT_COPY: u8 = 31; |
|
| 149 | 161 | ||
| 150 | 162 | /// Wire tag for data Val. |
|
| 151 | 163 | export constant DATA_VAL: u8 = 0; |
|
| 152 | 164 | /// Wire tag for data Sym. |
|
| 153 | 165 | export constant DATA_SYM: u8 = 1; |
lib/std/lang/il/binary/collect.rad
+4 -0
| 176 | 176 | try value(names, owner, end, a); |
|
| 177 | 177 | } |
|
| 178 | 178 | case il::Instr::Elem { index, .. } => try value(names, owner, index, a), |
|
| 179 | 179 | case il::Instr::SliceElem { index, .. } => try value(names, owner, index, a), |
|
| 180 | 180 | case il::Instr::StorePtr { src, .. } => try value(names, owner, src, a), |
|
| 181 | + | case il::Instr::TraitNew { table, .. } => try value(names, owner, table, a), |
|
| 182 | + | case il::Instr::TraitCall { args, .. } => |
|
| 183 | + | try values(names, owner, il::argVals(func, args), a), |
|
| 181 | 184 | case |
|
| 182 | 185 | il::Instr::Field { .. }, |
|
| 186 | + | il::Instr::TraitCopy { .. }, |
|
| 183 | 187 | il::Instr::LoadPtr { .. }, |
|
| 184 | 188 | il::Instr::Reserve { .. }, |
|
| 185 | 189 | il::Instr::Blit { .. }, |
|
| 186 | 190 | il::Instr::Load { .. }, |
|
| 187 | 191 | il::Instr::Sload { .. }, |
lib/std/lang/il/binary/program.rad
+47 -2
| 1 | 1 | //! Binary package envelopes and IL program reconstruction. |
|
| 2 | 2 | //! Tables occur in this order: symbols, dependencies, exports, layouts, signatures, |
|
| 3 | - | //! data, functions. Block records carry labels, parameters, and instructions. The |
|
| 3 | + | //! traits, data, functions. Block records carry labels, parameters, and instructions. The |
|
| 4 | 4 | //! decoder derives predecessors and loop depth from the terminators. |
|
| 5 | 5 | //! A label is its base text followed by an optional decimal suffix. |
|
| 6 | 6 | ||
| 7 | 7 | use std::lang::il; |
|
| 8 | 8 | use std::lang::il::binary; |
| 50 | 50 | } else { |
|
| 51 | 51 | try writer::integer(out, 0, 1); |
|
| 52 | 52 | } |
|
| 53 | 53 | try writer::integer(out, package.program.layouts.len as u64, 4); |
|
| 54 | 54 | try writer::integer(out, package.program.signatures.len as u64, 4); |
|
| 55 | + | try writer::integer(out, package.program.traits.len as u64, 4); |
|
| 55 | 56 | for item in package.program.layouts { |
|
| 56 | 57 | try writer::layout(out, item); |
|
| 57 | 58 | } |
|
| 58 | 59 | for item in package.program.signatures { |
|
| 59 | 60 | try writer::signature(out, item); |
|
| 60 | 61 | } |
|
| 62 | + | for item in package.program.traits { |
|
| 63 | + | try writer::traitRecord(out, item); |
|
| 64 | + | } |
|
| 61 | 65 | try writer::integer(out, package.program.data.len as u64, 4); |
|
| 62 | 66 | for item in package.program.data { |
|
| 63 | 67 | try writer::symbol(out, item.name); |
|
| 64 | 68 | try writer::integer(out, item.layout as u64, 4); |
|
| 65 | 69 | try writer::integer(out, 1 if item.readOnly else 0, 1); |
| 223 | 227 | set width = 16; |
|
| 224 | 228 | } |
|
| 225 | 229 | case binary::SLOT_FN => { |
|
| 226 | 230 | set kind = il::SlotKind::Fn(try reader::signatureIndex(input, wire)); |
|
| 227 | 231 | } |
|
| 232 | + | case binary::SLOT_TRAIT => { |
|
| 233 | + | let iface = try reader::traitIndex(input, wire); |
|
| 234 | + | let rights = try reader::rights(input, wire); |
|
| 235 | + | set kind = il::SlotKind::Trait(il::ObjectType { |
|
| 236 | + | iface, own: rights.own, mutable: rights.mutable |
|
| 237 | + | }); |
|
| 238 | + | set width = 16; |
|
| 239 | + | } |
|
| 228 | 240 | case binary::SLOT_SUB => { |
|
| 229 | 241 | let sub = try reader::integer(input, wire, 4) as u32; |
|
| 230 | 242 | if sub >= i { |
|
| 231 | 243 | throw binary::Error::Invalid; |
|
| 232 | 244 | } |
| 291 | 303 | }; |
|
| 292 | 304 | } |
|
| 293 | 305 | return &items[..]; |
|
| 294 | 306 | } |
|
| 295 | 307 | ||
| 308 | + | /// Read the trait table. Signature indices are checked against the table size. |
|
| 309 | + | fn readTraits( |
|
| 310 | + | input: &mut reader::Reader, wire: &[u8], a: &mut opaque alloc::Allocator |
|
| 311 | + | ) -> *[il::Trait] throws (binary::Error) { |
|
| 312 | + | let mut items: *mut [il::Trait] = &mut []; |
|
| 313 | + | try items.reserve(input.traits, a) catch { |
|
| 314 | + | throw binary::Error::Storage; |
|
| 315 | + | }; |
|
| 316 | + | for _ in 0 .. input.traits { |
|
| 317 | + | let methodCount = try reader::count(input, wire, binary::MIN_METHOD_BYTES); |
|
| 318 | + | let mut methods: *mut [il::Method] = &mut []; |
|
| 319 | + | try methods.reserve(methodCount, a) catch { |
|
| 320 | + | throw binary::Error::Storage; |
|
| 321 | + | }; |
|
| 322 | + | for _ in 0 .. methodCount { |
|
| 323 | + | let sig = try reader::signatureIndex(input, wire); |
|
| 324 | + | let receiver = try reader::integer(input, wire, 4) as u32; |
|
| 325 | + | let rights = try reader::rights(input, wire); |
|
| 326 | + | try methods.append( |
|
| 327 | + | il::Method { sig, receiver, own: rights.own, mutable: rights.mutable }, a |
|
| 328 | + | ) catch { |
|
| 329 | + | throw binary::Error::Storage; |
|
| 330 | + | }; |
|
| 331 | + | } |
|
| 332 | + | try items.append(il::Trait { methods: &methods[..] }, a) catch { |
|
| 333 | + | throw binary::Error::Storage; |
|
| 334 | + | }; |
|
| 335 | + | } |
|
| 336 | + | return &items[..]; |
|
| 337 | + | } |
|
| 338 | + | ||
| 296 | 339 | /// Read global data and check initializer extents against the layout storage. |
|
| 297 | 340 | fn readData( |
|
| 298 | 341 | input: &mut reader::Reader, wire: &[u8], a: &mut opaque alloc::Allocator, layouts: &[il::Layout] |
|
| 299 | 342 | ) -> *[il::Data] throws (binary::Error) { |
|
| 300 | 343 | let n = try reader::count(input, wire, binary::MIN_DATA_BYTES); |
| 518 | 561 | input, wire, binary::MIN_LAYOUT_BYTES |
|
| 519 | 562 | ); |
|
| 520 | 563 | set input.signatures = try reader::count( |
|
| 521 | 564 | input, wire, binary::MIN_SIGNATURE_BYTES |
|
| 522 | 565 | ); |
|
| 566 | + | set input.traits = try reader::count(input, wire, binary::MIN_TRAIT_BYTES); |
|
| 523 | 567 | let layouts = try readLayouts(input, wire, a); |
|
| 524 | 568 | let signatures = try readSignatures(input, wire, a); |
|
| 569 | + | let traits = try readTraits(input, wire, a); |
|
| 525 | 570 | let data = try readData(input, wire, a, layouts); |
|
| 526 | 571 | let fns = try readFunctions(input, wire, a, limits); |
|
| 527 | 572 | if not reader::atEnd(input, wire) { |
|
| 528 | 573 | throw binary::Error::Invalid; |
|
| 529 | 574 | } |
| 532 | 577 | symbols: &symbols[..], |
|
| 533 | 578 | name, |
|
| 534 | 579 | dependencies: &dependencies[..], |
|
| 535 | 580 | exports: &exports[..], |
|
| 536 | 581 | entry, |
|
| 537 | - | program: il::Program { data, fns, layouts, signatures }, |
|
| 582 | + | program: il::Program { data, fns, layouts, signatures, traits }, |
|
| 538 | 583 | }; |
|
| 539 | 584 | } |
|
| 540 | 585 | ||
| 541 | 586 | /// Decode one package into storage from `arena`. Restore the arena offset on |
|
| 542 | 587 | /// every failure. |
lib/std/lang/il/binary/reader.rad
+64 -0
| 15 | 15 | symbols: *mut [*[u8]], |
|
| 16 | 16 | /// Number of entries in the layout table of the package. |
|
| 17 | 17 | layouts: u32, |
|
| 18 | 18 | /// Number of entries in the signature table of the package. |
|
| 19 | 19 | signatures: u32, |
|
| 20 | + | /// Number of entries in the trait table of the package. |
|
| 21 | + | traits: u32, |
|
| 20 | 22 | /// Exclusive bound for SSA register numbers. |
|
| 21 | 23 | registers: u32, |
|
| 22 | 24 | /// Number of blocks in the current function. |
|
| 23 | 25 | blocks: u32, |
|
| 24 | 26 | /// Argument pool of the current function. |
| 32 | 34 | return Reader { |
|
| 33 | 35 | offset: 0, |
|
| 34 | 36 | symbols: &mut [], |
|
| 35 | 37 | layouts: 0, |
|
| 36 | 38 | signatures: 0, |
|
| 39 | + | traits: 0, |
|
| 37 | 40 | registers: 0, |
|
| 38 | 41 | blocks: 0, |
|
| 39 | 42 | vals: &mut [], |
|
| 40 | 43 | cases: &mut [], |
|
| 41 | 44 | }; |
| 132 | 135 | throw binary::Error::Invalid; |
|
| 133 | 136 | } |
|
| 134 | 137 | return n; |
|
| 135 | 138 | } |
|
| 136 | 139 | ||
| 140 | + | /// Read a trait table index within the package. |
|
| 141 | + | export fn traitIndex(input: &mut Reader, wire: &[u8]) -> u32 throws (binary::Error) { |
|
| 142 | + | let n = try integer(input, wire, 4) as u32; |
|
| 143 | + | if n >= input.traits { |
|
| 144 | + | throw binary::Error::Invalid; |
|
| 145 | + | } |
|
| 146 | + | return n; |
|
| 147 | + | } |
|
| 148 | + | ||
| 149 | + | /// Rights of a receiver. |
|
| 150 | + | export record Rights: Copy { |
|
| 151 | + | /// Whether the receiver owns its object. |
|
| 152 | + | own: bool, |
|
| 153 | + | /// Whether the receiver can be written. |
|
| 154 | + | mutable: bool, |
|
| 155 | + | } |
|
| 156 | + | ||
| 157 | + | /// Read the rights of a receiver, encoded as pointer type flags without nullability. |
|
| 158 | + | export fn rights(input: &mut Reader, wire: &[u8]) -> Rights throws (binary::Error) { |
|
| 159 | + | let flags = try integer(input, wire, 1) as u8; |
|
| 160 | + | if flags > (super::PTR_OWN | super::PTR_MUTABLE) { |
|
| 161 | + | throw binary::Error::Invalid; |
|
| 162 | + | } |
|
| 163 | + | return Rights { |
|
| 164 | + | own: (flags & super::PTR_OWN) <> 0, mutable: (flags & super::PTR_MUTABLE) <> 0 |
|
| 165 | + | }; |
|
| 166 | + | } |
|
| 167 | + | ||
| 137 | 168 | /// Read a pointer type with checked layout index and flags. |
|
| 138 | 169 | export fn ptrType(input: &mut Reader, wire: &[u8]) -> il::PtrType throws (binary::Error) { |
|
| 139 | 170 | let layout = try layoutIndex(input, wire); |
|
| 140 | 171 | let flags = try integer(input, wire, 1) as u8; |
|
| 141 | 172 | if flags > (super::PTR_OWN | super::PTR_MUTABLE | super::PTR_NULLABLE) { |
| 489 | 520 | let vsrc = try val(input, wire); |
|
| 490 | 521 | let vdst = try reg(input, wire); |
|
| 491 | 522 | let voffset = try integer(input, wire, 4) as i32; |
|
| 492 | 523 | return il::Instr::StorePtr { src: vsrc, dst: vdst, offset: voffset }; |
|
| 493 | 524 | } |
|
| 525 | + | case super::INSTR_TRAIT_NEW => { |
|
| 526 | + | let vdst = try reg(input, wire); |
|
| 527 | + | let vrecv = try reg(input, wire); |
|
| 528 | + | let vtable = try val(input, wire); |
|
| 529 | + | return il::Instr::TraitNew { dst: vdst, recv: vrecv, table: vtable }; |
|
| 530 | + | } |
|
| 531 | + | case super::INSTR_TRAIT_COPY => { |
|
| 532 | + | let vdst = try reg(input, wire); |
|
| 533 | + | let vsrc = try reg(input, wire); |
|
| 534 | + | return il::Instr::TraitCopy { dst: vdst, src: vsrc }; |
|
| 535 | + | } |
|
| 536 | + | case super::INSTR_TRAIT_CALL => { |
|
| 537 | + | let vretTy = try valType(input, wire); |
|
| 538 | + | let mut vdst: ?il::Reg = nil; |
|
| 539 | + | if try flag(input, wire) { |
|
| 540 | + | set vdst = try reg(input, wire); |
|
| 541 | + | } |
|
| 542 | + | let vobj = try reg(input, wire); |
|
| 543 | + | let vmethod = try integer(input, wire, 4) as u32; |
|
| 544 | + | let vreceiver = try integer(input, wire, 4) as u32; |
|
| 545 | + | let vargs = try values(input, wire, a); |
|
| 546 | + | if vreceiver >= vargs.len { |
|
| 547 | + | throw binary::Error::Invalid; |
|
| 548 | + | } |
|
| 549 | + | return il::Instr::TraitCall { |
|
| 550 | + | retTy: vretTy, |
|
| 551 | + | dst: vdst, |
|
| 552 | + | obj: vobj, |
|
| 553 | + | method: vmethod, |
|
| 554 | + | receiver: vreceiver, |
|
| 555 | + | args: vargs, |
|
| 556 | + | }; |
|
| 557 | + | } |
|
| 494 | 558 | case super::INSTR_EBREAK => { |
|
| 495 | 559 | return il::Instr::Ebreak; |
|
| 496 | 560 | } |
|
| 497 | 561 | case super::INSTR_MEMORYFENCE => { |
|
| 498 | 562 | return il::Instr::MemoryFence; |
lib/std/lang/il/binary/tests.rad
+22 -0
| 286 | 286 | slot: il::Reg { n: 2 }, |
|
| 287 | 287 | handle: il::Val::Undef, |
|
| 288 | 288 | write: true, |
|
| 289 | 289 | layout: 3, |
|
| 290 | 290 | }, &[28, 1, 0, 0, 0, 2, 0, 0, 0, 4, 1, 3, 0, 0, 0]); |
|
| 291 | + | try instruction(il::Instr::TraitNew { |
|
| 292 | + | dst: il::Reg { n: 1 }, recv: il::Reg { n: 2 }, table: il::Val::Undef |
|
| 293 | + | }, &[29, 1, 0, 0, 0, 2, 0, 0, 0, 4]); |
|
| 294 | + | try instruction(il::Instr::TraitCall { |
|
| 295 | + | retTy: il::ValType::Void, |
|
| 296 | + | dst: nil, |
|
| 297 | + | obj: il::Reg { n: 2 }, |
|
| 298 | + | method: 1, |
|
| 299 | + | receiver: 0, |
|
| 300 | + | args: ONE_ARG, |
|
| 301 | + | }, &[30, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 4]); |
|
| 302 | + | try instruction(il::Instr::TraitCall { |
|
| 303 | + | retTy: il::ValType::Data(il::Type::W64), |
|
| 304 | + | dst: il::Reg { n: 3 }, |
|
| 305 | + | obj: il::Reg { n: 2 }, |
|
| 306 | + | method: 0, |
|
| 307 | + | receiver: 0, |
|
| 308 | + | args: ONE_ARG, |
|
| 309 | + | }, &[30, 1, 8, 1, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 4]); |
|
| 310 | + | try instruction(il::Instr::TraitCopy { |
|
| 311 | + | dst: il::Reg { n: 1 }, src: il::Reg { n: 2 } |
|
| 312 | + | }, &[31, 1, 0, 0, 0, 2, 0, 0, 0]); |
|
| 291 | 313 | try instruction(il::Instr::Ebreak, &[17]); |
|
| 292 | 314 | try instruction(il::Instr::MemoryFence, &[18]); |
|
| 293 | 315 | for typ in [il::Type::W8, il::Type::W16, il::Type::W32, il::Type::W64] { |
|
| 294 | 316 | let mut read: [u8; 8] = [19, 0, 1, 0, 0, 0, 4, 4]; |
|
| 295 | 317 | let mut write: [u8; 5] = [20, 0, 4, 4, 4]; |
lib/std/lang/il/binary/tests/decode.rad
+95 -7
| 11 | 11 | ||
| 12 | 12 | /// Decode arena backing storage. Tests reset it before each use. |
|
| 13 | 13 | static MEMORY: [u8; 2048] = [0; 2048]; |
|
| 14 | 14 | ||
| 15 | 15 | /// Program without declarations. |
|
| 16 | - | constant EMPTY: il::Program = il::Program { data: &[], fns: &[], layouts: &[], signatures: &[] }; |
|
| 16 | + | constant EMPTY: il::Program = il::Program { |
|
| 17 | + | data: &[], fns: &[], layouts: &[], signatures: &[], traits: &[] |
|
| 18 | + | }; |
|
| 17 | 19 | ||
| 18 | 20 | /// Structural limits for small test programs. |
|
| 19 | 21 | constant LIMITS: binary::Limits = binary::Limits { registers: 16, blocks: 4 }; |
|
| 20 | 22 | ||
| 21 | 23 | /// Encode `package` into test storage that the writer borrows for the call. |
| 95 | 97 | &buffer[.. length], |
|
| 96 | 98 | &[ |
|
| 97 | 99 | 82, 73, 76, 0, 1, 0, 0, 0, |
|
| 98 | 100 | 1, 0, 0, 0, 112, 0, 0, 0, 0, |
|
| 99 | 101 | 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 100 | - | 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 102 | + | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 101 | 103 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 102 | 104 | ] |
|
| 103 | 105 | ); |
|
| 104 | 106 | } |
|
| 105 | 107 |
| 200 | 202 | layouts: &[ |
|
| 201 | 203 | il::Layout { size: 64, alignment: 8, slots: &[] }, |
|
| 202 | 204 | il::Layout { size: 4096, alignment: 4096, slots: &[] }, |
|
| 203 | 205 | ], |
|
| 204 | 206 | signatures: &[], |
|
| 207 | + | traits: &[], |
|
| 205 | 208 | }, |
|
| 206 | 209 | }; |
|
| 207 | 210 | try roundTrip(&package); |
|
| 208 | 211 | } |
|
| 209 | 212 |
| 245 | 248 | try badInstruction(&[5, 2, 0, 0, 0], binary::Error::Invalid); |
|
| 246 | 249 | try badInstruction(&[5, 0, 0, 0, 0, 0, 2, 0, 0, 0], binary::Error::Invalid); |
|
| 247 | 250 | try badInstruction(&[12, 1, 0, 0, 0], binary::Error::Invalid); |
|
| 248 | 251 | try badInstruction(&[5, 0, 0, 0, 0, 2, 1, 0, 0, 0], binary::Error::Symbol); |
|
| 249 | 252 | try badInstruction(&[12, 0, 0, 0, 0, 255, 255, 255, 255], binary::Error::Truncated); |
|
| 253 | + | // The receiver of a trait call is one of its arguments. |
|
| 254 | + | try badInstruction( |
|
| 255 | + | &[30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], binary::Error::Invalid |
|
| 256 | + | ); |
|
| 250 | 257 | // No layout table, so no layout index is valid. |
|
| 251 | 258 | try badInstruction(&[0, 0, 0, 0, 0, 0, 0, 0, 0], binary::Error::Invalid); |
|
| 252 | 259 | try badInstruction( |
|
| 253 | 260 | &[22, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 1, 0, 0, 0], binary::Error::Invalid |
|
| 254 | 261 | ); |
| 338 | 345 | program: il::Program { |
|
| 339 | 346 | data: retainData(&[item]), |
|
| 340 | 347 | fns: &[], |
|
| 341 | 348 | layouts: &[il::Layout { size: 0xffffffff, alignment: 8, slots: &[] }], |
|
| 342 | 349 | signatures: &[], |
|
| 350 | + | traits: &[], |
|
| 343 | 351 | }, |
|
| 344 | 352 | }; |
|
| 345 | 353 | try rejected(&package, LIMITS); |
|
| 346 | 354 | set item.values = &[ |
|
| 347 | 355 | il::DataValue { item: il::DataItem::Undef, count: 1 }, |
| 350 | 358 | set package.program = il::Program { |
|
| 351 | 359 | data: retainData(&[item]), |
|
| 352 | 360 | fns: &[], |
|
| 353 | 361 | layouts: &[il::Layout { size: 1, alignment: 1, slots: &[] }], |
|
| 354 | 362 | signatures: &[], |
|
| 363 | + | traits: &[], |
|
| 355 | 364 | }; |
|
| 356 | 365 | try rejected(&package, LIMITS); |
|
| 357 | 366 | // The layout index must name a layout of the package. |
|
| 358 | 367 | set item.values = &[]; |
|
| 359 | 368 | set item.layout = 1; |
|
| 360 | 369 | set package.program = il::Program { |
|
| 361 | 370 | data: retainData(&[item]), |
|
| 362 | 371 | fns: &[], |
|
| 363 | 372 | layouts: &[il::Layout { size: 1, alignment: 1, slots: &[] }], |
|
| 364 | 373 | signatures: &[], |
|
| 374 | + | traits: &[], |
|
| 365 | 375 | }; |
|
| 366 | 376 | try rejected(&package, LIMITS); |
|
| 367 | 377 | set package.program = EMPTY; |
|
| 368 | 378 | set package.symbols = &["p", "p"]; |
|
| 369 | 379 | try rejected(&package, LIMITS); |
| 388 | 398 | symbols: &["p"], |
|
| 389 | 399 | name: "p", |
|
| 390 | 400 | dependencies: &[], |
|
| 391 | 401 | exports: &[], |
|
| 392 | 402 | entry: nil, |
|
| 393 | - | program: il::Program { data: &[], fns: &list[..], layouts: &[], signatures: &[] }, |
|
| 403 | + | program: il::Program { |
|
| 404 | + | data: &[], fns: &list[..], layouts: &[], signatures: &[], traits: &[] |
|
| 405 | + | }, |
|
| 394 | 406 | }; |
|
| 395 | 407 | try rejected(&package, limits); |
|
| 396 | 408 | } |
|
| 397 | 409 | ||
| 398 | 410 | /// Check function block limits, parameter indices, and branch targets. |
| 512 | 524 | symbols: &["p", "p::f"], |
|
| 513 | 525 | name: "p", |
|
| 514 | 526 | dependencies: &[], |
|
| 515 | 527 | exports: &[], |
|
| 516 | 528 | entry: nil, |
|
| 517 | - | program: il::Program { data: &[], fns: &list[..], layouts: &[], signatures: &[] }, |
|
| 529 | + | program: il::Program { |
|
| 530 | + | data: &[], fns: &list[..], layouts: &[], signatures: &[], traits: &[] |
|
| 531 | + | }, |
|
| 518 | 532 | }; |
|
| 519 | 533 | let length = try encodeInto(&mut whole[..] as *mut [u8], &package) catch { |
|
| 520 | 534 | throw testing::TestError::Failed; |
|
| 521 | 535 | }; |
|
| 522 | 536 | let mut index = dict::init(&mut slots[..] as *mut [dict::Entry]); |
| 596 | 610 | }, |
|
| 597 | 611 | ], |
|
| 598 | 612 | fns: &[], |
|
| 599 | 613 | layouts: &[], |
|
| 600 | 614 | signatures: &[], |
|
| 615 | + | traits: &[], |
|
| 601 | 616 | }; |
|
| 602 | 617 | let mut arena = alloc::new(&mut MEMORY[..]); |
|
| 603 | 618 | let mut failed = false; |
|
| 604 | 619 | try collected("other", local, &mut arena) catch err { |
|
| 605 | 620 | try testing::expect(err == binary::Error::Invalid); |
| 737 | 752 | symbols: &["p", "p::f"], |
|
| 738 | 753 | name: "p", |
|
| 739 | 754 | dependencies: &[], |
|
| 740 | 755 | exports: &[], |
|
| 741 | 756 | entry: nil, |
|
| 742 | - | program: il::Program { data: &[], fns: &list[..], layouts, signatures }, |
|
| 757 | + | program: il::Program { data: &[], fns: &list[..], layouts, signatures, traits: &[] }, |
|
| 743 | 758 | }; |
|
| 744 | 759 | try roundTrip(&package); |
|
| 745 | 760 | ||
| 746 | 761 | let mut arena = alloc::new(&mut MEMORY[..]); |
|
| 747 | 762 | let decoded = try reencoded(&package, &mut arena); |
| 778 | 793 | layout: 2, |
|
| 779 | 794 | } |
|
| 780 | 795 | ); |
|
| 781 | 796 | } |
|
| 782 | 797 | ||
| 798 | + | /// A trait object slot of trait 0 that borrows a writable receiver. |
|
| 799 | + | constant OBJECT_SLOT: il::Slot = il::Slot { |
|
| 800 | + | offset: 0, |
|
| 801 | + | kind: il::SlotKind::Trait(il::ObjectType { |
|
| 802 | + | iface: 0, own: false, mutable: true |
|
| 803 | + | }), |
|
| 804 | + | }; |
|
| 805 | + | ||
| 806 | + | /// Check that a trait table and trait object slots survive encoding, and that their |
|
| 807 | + | /// indices and rights are checked. |
|
| 808 | + | @test unsafe fn traitPackage() throws (testing::TestError) { |
|
| 809 | + | let object = OBJECT_SLOT; |
|
| 810 | + | let layouts: *[il::Layout] = &[ |
|
| 811 | + | il::Layout { size: 4, alignment: 4, slots: &[] }, |
|
| 812 | + | il::Layout { size: 16, alignment: 8, slots: &[OBJECT_SLOT] }, |
|
| 813 | + | ]; |
|
| 814 | + | let signatures: *[il::Signature] = &[ |
|
| 815 | + | il::Signature { params: &[], ret: il::ValType::Data(il::Type::W64) } |
|
| 816 | + | ]; |
|
| 817 | + | let traits: *[il::Trait] = &[ |
|
| 818 | + | il::Trait { |
|
| 819 | + | methods: &[ |
|
| 820 | + | il::Method { sig: 0, receiver: 0, own: false, mutable: true }, |
|
| 821 | + | il::Method { sig: 0, receiver: 0, own: true, mutable: false }, |
|
| 822 | + | ], |
|
| 823 | + | }, |
|
| 824 | + | ]; |
|
| 825 | + | let methods = traits[0].methods; |
|
| 826 | + | let package = binary::Package { |
|
| 827 | + | symbols: &["p"], |
|
| 828 | + | name: "p", |
|
| 829 | + | dependencies: &[], |
|
| 830 | + | exports: &[], |
|
| 831 | + | entry: nil, |
|
| 832 | + | program: il::Program { data: &[], fns: &[], layouts, signatures, traits }, |
|
| 833 | + | }; |
|
| 834 | + | try roundTrip(&package); |
|
| 835 | + | ||
| 836 | + | let mut arena = alloc::new(&mut MEMORY[..]); |
|
| 837 | + | let decoded = try reencoded(&package, &mut arena); |
|
| 838 | + | try testing::expect(decoded.program.traits.len == 1); |
|
| 839 | + | try testing::expect(decoded.program.traits[0].methods.len == 2); |
|
| 840 | + | for method, i in methods { |
|
| 841 | + | try testing::expect(decoded.program.traits[0].methods[i] == method); |
|
| 842 | + | } |
|
| 843 | + | try testing::expect(decoded.program.layouts[1].slots[0] == object); |
|
| 844 | + | ||
| 845 | + | // A trait slot needs a trait, and a method needs a signature. |
|
| 846 | + | try rejectedLayouts(&[il::Layout { size: 16, alignment: 8, slots: &[OBJECT_SLOT] }], &[]); |
|
| 847 | + | let unsigned = binary::Package { |
|
| 848 | + | symbols: &["p"], |
|
| 849 | + | name: "p", |
|
| 850 | + | dependencies: &[], |
|
| 851 | + | exports: &[], |
|
| 852 | + | entry: nil, |
|
| 853 | + | program: il::Program { |
|
| 854 | + | data: &[], |
|
| 855 | + | fns: &[], |
|
| 856 | + | layouts, |
|
| 857 | + | signatures, |
|
| 858 | + | traits: &[ |
|
| 859 | + | il::Trait { |
|
| 860 | + | methods: &[il::Method { sig: 1, receiver: 0, own: false, mutable: false }] |
|
| 861 | + | }, |
|
| 862 | + | ], |
|
| 863 | + | }, |
|
| 864 | + | }; |
|
| 865 | + | try rejected(&unsigned, LIMITS); |
|
| 866 | + | } |
|
| 867 | + | ||
| 783 | 868 | /// Encode a package with the given tables and check that decoding rejects it. |
|
| 784 | 869 | unsafe fn rejectedLayouts(layouts: *[il::Layout], signatures: *[il::Signature]) throws ( |
|
| 785 | 870 | testing::TestError |
|
| 786 | 871 | ) { |
|
| 787 | 872 | let package = binary::Package { |
|
| 788 | 873 | symbols: &["p"], |
|
| 789 | 874 | name: "p", |
|
| 790 | 875 | dependencies: &[], |
|
| 791 | 876 | exports: &[], |
|
| 792 | 877 | entry: nil, |
|
| 793 | - | program: il::Program { data: &[], fns: &[], layouts, signatures }, |
|
| 878 | + | program: il::Program { data: &[], fns: &[], layouts, signatures, traits: &[] }, |
|
| 794 | 879 | }; |
|
| 795 | 880 | try rejected(&package, LIMITS); |
|
| 796 | 881 | } |
|
| 797 | 882 | ||
| 798 | 883 | /// Owned nullable pointer to layout 1, as stored in a slot. |
| 1036 | 1121 | fns: &[], |
|
| 1037 | 1122 | layouts: &[PLAIN, PTR_LAYOUT], |
|
| 1038 | 1123 | signatures: &[ |
|
| 1039 | 1124 | il::Signature { params: &[], ret: il::ValType::Fn(0) } |
|
| 1040 | 1125 | ], |
|
| 1126 | + | traits: &[], |
|
| 1041 | 1127 | }, |
|
| 1042 | 1128 | }; |
|
| 1043 | 1129 | try roundTrip(&ok); |
|
| 1044 | 1130 | } |
|
| 1045 | 1131 |
| 1125 | 1211 | symbols: &["p", "p::f"], |
|
| 1126 | 1212 | name: "p", |
|
| 1127 | 1213 | dependencies: &[], |
|
| 1128 | 1214 | exports: &[], |
|
| 1129 | 1215 | entry: nil, |
|
| 1130 | - | program: il::Program { data: &[], fns: &fns[..], layouts: &[], signatures: &[] }, |
|
| 1216 | + | program: il::Program { |
|
| 1217 | + | data: &[], fns: &fns[..], layouts: &[], signatures: &[], traits: &[] |
|
| 1218 | + | }, |
|
| 1131 | 1219 | }; |
|
| 1132 | 1220 | static buffer: [u8; 512] = [0; 512]; |
|
| 1133 | 1221 | let length = try encodeInto(&mut buffer[..] as *mut [u8], &package) catch { |
|
| 1134 | 1222 | throw testing::TestError::Failed; |
|
| 1135 | 1223 | }; |
lib/std/lang/il/binary/writer.rad
+55 -0
| 141 | 141 | } |
|
| 142 | 142 | case il::SlotKind::Fn(signature) => { |
|
| 143 | 143 | try integer(out, super::SLOT_FN as u64, 1); |
|
| 144 | 144 | try integer(out, signature as u64, 4); |
|
| 145 | 145 | } |
|
| 146 | + | case il::SlotKind::Trait(object) => { |
|
| 147 | + | try integer(out, super::SLOT_TRAIT as u64, 1); |
|
| 148 | + | try integer(out, object.iface as u64, 4); |
|
| 149 | + | try rights(out, object.own, object.mutable); |
|
| 150 | + | } |
|
| 146 | 151 | case il::SlotKind::Sub(sub) => { |
|
| 147 | 152 | try integer(out, super::SLOT_SUB as u64, 1); |
|
| 148 | 153 | try integer(out, sub as u64, 4); |
|
| 149 | 154 | } |
|
| 150 | 155 | case il::SlotKind::Array { layout: element, count } => { |
| 154 | 159 | } |
|
| 155 | 160 | } |
|
| 156 | 161 | } |
|
| 157 | 162 | } |
|
| 158 | 163 | ||
| 164 | + | /// Write the rights of a receiver as pointer type flags. |
|
| 165 | + | fn rights(out: &mut Writer, own: bool, mutable: bool) throws (binary::Error) { |
|
| 166 | + | let mut flags: u8 = 0; |
|
| 167 | + | if own { |
|
| 168 | + | set flags |= super::PTR_OWN; |
|
| 169 | + | } |
|
| 170 | + | if mutable { |
|
| 171 | + | set flags |= super::PTR_MUTABLE; |
|
| 172 | + | } |
|
| 173 | + | try integer(out, flags as u64, 1); |
|
| 174 | + | } |
|
| 175 | + | ||
| 176 | + | /// Write one trait: its methods, each the signature without the receiver, the |
|
| 177 | + | /// receiver position and the rights the receiver needs. |
|
| 178 | + | export fn traitRecord(out: &mut Writer, value: il::Trait) throws (binary::Error) { |
|
| 179 | + | try integer(out, value.methods.len as u64, 4); |
|
| 180 | + | for method in value.methods { |
|
| 181 | + | try integer(out, method.sig as u64, 4); |
|
| 182 | + | try integer(out, method.receiver as u64, 4); |
|
| 183 | + | try rights(out, method.own, method.mutable); |
|
| 184 | + | } |
|
| 185 | + | } |
|
| 186 | + | ||
| 159 | 187 | /// Write one signature: the parameter types and the result type. |
|
| 160 | 188 | export fn signature(out: &mut Writer, value: il::Signature) throws (binary::Error) { |
|
| 161 | 189 | try integer(out, value.params.len as u64, 4); |
|
| 162 | 190 | for param in value.params { |
|
| 163 | 191 | try valType(out, param); |
| 468 | 496 | try integer(out, super::INSTR_STORE_PTR as u64, 1); |
|
| 469 | 497 | try val(out, vsrc); |
|
| 470 | 498 | try integer(out, vdst.n as u64, 4); |
|
| 471 | 499 | try integer(out, voffset as u64, 4); |
|
| 472 | 500 | } |
|
| 501 | + | case il::Instr::TraitNew { dst: vdst, recv: vrecv, table: vtable } => { |
|
| 502 | + | try integer(out, super::INSTR_TRAIT_NEW as u64, 1); |
|
| 503 | + | try integer(out, vdst.n as u64, 4); |
|
| 504 | + | try integer(out, vrecv.n as u64, 4); |
|
| 505 | + | try val(out, vtable); |
|
| 506 | + | } |
|
| 507 | + | case il::Instr::TraitCopy { dst: vdst, src: vsrc } => { |
|
| 508 | + | try integer(out, super::INSTR_TRAIT_COPY as u64, 1); |
|
| 509 | + | try integer(out, vdst.n as u64, 4); |
|
| 510 | + | try integer(out, vsrc.n as u64, 4); |
|
| 511 | + | } |
|
| 512 | + | case il::Instr::TraitCall { |
|
| 513 | + | retTy: vretTy, dst: vdst, obj: vobj, method: vmethod, receiver: vreceiver, args: vargs |
|
| 514 | + | } => { |
|
| 515 | + | try integer(out, super::INSTR_TRAIT_CALL as u64, 1); |
|
| 516 | + | try valType(out, vretTy); |
|
| 517 | + | if let present = vdst { |
|
| 518 | + | try integer(out, 1, 1); |
|
| 519 | + | try integer(out, present.n as u64, 4); |
|
| 520 | + | } else { |
|
| 521 | + | try integer(out, 0, 1); |
|
| 522 | + | } |
|
| 523 | + | try integer(out, vobj.n as u64, 4); |
|
| 524 | + | try integer(out, vmethod as u64, 4); |
|
| 525 | + | try integer(out, vreceiver as u64, 4); |
|
| 526 | + | try values(out, il::argVals(func, vargs)); |
|
| 527 | + | } |
|
| 473 | 528 | case il::Instr::Ebreak => { |
|
| 474 | 529 | try integer(out, super::INSTR_EBREAK as u64, 1); |
|
| 475 | 530 | } |
|
| 476 | 531 | case il::Instr::MemoryFence => { |
|
| 477 | 532 | try integer(out, super::INSTR_MEMORYFENCE as u64, 1); |
lib/std/lang/il/printer.rad
+79 -4
| 322 | 322 | out.write(" "); |
|
| 323 | 323 | writeReg(out, dst); |
|
| 324 | 324 | out.write(" "); |
|
| 325 | 325 | sexpr::writeI64(out, offset as i64); |
|
| 326 | 326 | } |
|
| 327 | + | case super::Instr::TraitNew { dst, recv, table } => { |
|
| 328 | + | out.write("trait.new "); |
|
| 329 | + | writeReg(out, dst); |
|
| 330 | + | out.write(" "); |
|
| 331 | + | writeReg(out, recv); |
|
| 332 | + | out.write(" "); |
|
| 333 | + | writeVal(out, table); |
|
| 334 | + | } |
|
| 335 | + | case super::Instr::TraitCopy { dst, src } => { |
|
| 336 | + | out.write("trait.copy "); |
|
| 337 | + | writeReg(out, dst); |
|
| 338 | + | out.write(" "); |
|
| 339 | + | writeReg(out, src); |
|
| 340 | + | } |
|
| 341 | + | case super::Instr::TraitCall { retTy, dst, obj, method, receiver, args } => { |
|
| 342 | + | out.write("trait.call "); |
|
| 343 | + | writeValType(out, retTy); |
|
| 344 | + | out.write(" "); |
|
| 345 | + | if let d = dst { |
|
| 346 | + | writeReg(out, d); |
|
| 347 | + | out.write(" "); |
|
| 348 | + | } |
|
| 349 | + | writeReg(out, obj); |
|
| 350 | + | out.write(" "); |
|
| 351 | + | sexpr::writeI64(out, method as i64); |
|
| 352 | + | out.write(" at "); |
|
| 353 | + | sexpr::writeI64(out, receiver as i64); |
|
| 354 | + | writeArgs(out, func, args); |
|
| 355 | + | } |
|
| 327 | 356 | ||
| 328 | 357 | // ALU operations. |
|
| 329 | 358 | case super::Instr::BinOp { op, dst, typ, a: va, b } => |
|
| 330 | 359 | writeTypedBinOp(out, binOpStr(op), typ, dst, va, b), |
|
| 331 | 360 | case super::Instr::UnOp { op, dst, typ, a: va } => |
| 649 | 678 | } |
|
| 650 | 679 | case super::SlotKind::Fn(sig) => { |
|
| 651 | 680 | out.write("fn "); |
|
| 652 | 681 | writeIndex(out, "S", sig); |
|
| 653 | 682 | } |
|
| 683 | + | case super::SlotKind::Trait(object) => { |
|
| 684 | + | out.write("trait "); |
|
| 685 | + | out.write("own " if object.own else "ref "); |
|
| 686 | + | out.write("mut " if object.mutable else "const "); |
|
| 687 | + | writeIndex(out, "T", object.iface); |
|
| 688 | + | } |
|
| 654 | 689 | case super::SlotKind::Sub(layout) => { |
|
| 655 | 690 | out.write("sub "); |
|
| 656 | 691 | writeIndex(out, "L", layout); |
|
| 657 | 692 | } |
|
| 658 | 693 | case super::SlotKind::Array { layout, count } => { |
| 707 | 742 | writeValType(out, signature.ret); |
|
| 708 | 743 | out.write(";\n"); |
|
| 709 | 744 | } |
|
| 710 | 745 | } |
|
| 711 | 746 | ||
| 747 | + | /// Write the trait table, one declaration per trait. A trait lists one |
|
| 748 | + | /// `method $S at <receiver> <class> <mut>;` declaration per line, in method order. |
|
| 749 | + | fn writeTraits(out: &mut opaque sexpr::Output, traits: &[super::Trait]) { |
|
| 750 | + | for item, i in traits { |
|
| 751 | + | out.write("trait "); |
|
| 752 | + | writeIndex(out, "T", i); |
|
| 753 | + | if item.methods.len == 0 { |
|
| 754 | + | out.write(" {}\n"); |
|
| 755 | + | continue; |
|
| 756 | + | } |
|
| 757 | + | out.write(" {\n"); |
|
| 758 | + | for method in item.methods { |
|
| 759 | + | indent(out, 1); |
|
| 760 | + | out.write("method "); |
|
| 761 | + | writeIndex(out, "S", method.sig); |
|
| 762 | + | out.write(" at "); |
|
| 763 | + | sexpr::writeI64(out, method.receiver as i64); |
|
| 764 | + | out.write(" own " if method.own else " ref "); |
|
| 765 | + | out.write("mut;\n" if method.mutable else "const;\n"); |
|
| 766 | + | } |
|
| 767 | + | out.write("}\n"); |
|
| 768 | + | } |
|
| 769 | + | } |
|
| 770 | + | ||
| 712 | 771 | ////////////////////// |
|
| 713 | 772 | // Program printing // |
|
| 714 | 773 | ////////////////////// |
|
| 715 | 774 | ||
| 716 | 775 | /// Write the data declarations, with a blank line between neighbors. |
| 721 | 780 | out.write("\n"); |
|
| 722 | 781 | } |
|
| 723 | 782 | } |
|
| 724 | 783 | } |
|
| 725 | 784 | ||
| 726 | - | /// Write the layouts, signatures and data of a program, with a blank line between |
|
| 727 | - | /// sections and after them when `hasFns` is set. |
|
| 785 | + | /// Write the layouts, signatures, traits and data of a program, with a blank line |
|
| 786 | + | /// between sections and after them when `hasFns` is set. |
|
| 728 | 787 | fn writeDecls( |
|
| 729 | 788 | out: &mut opaque sexpr::Output, |
|
| 730 | 789 | layouts: &[super::Layout], |
|
| 731 | 790 | signatures: &[super::Signature], |
|
| 791 | + | traits: &[super::Trait], |
|
| 732 | 792 | data: &[super::Data], |
|
| 733 | 793 | hasFns: bool |
|
| 734 | 794 | ) { |
|
| 735 | 795 | let mut written = false; |
|
| 736 | 796 | if layouts.len > 0 { |
| 742 | 802 | out.write("\n"); |
|
| 743 | 803 | } |
|
| 744 | 804 | writeSignatures(out, signatures); |
|
| 745 | 805 | set written = true; |
|
| 746 | 806 | } |
|
| 807 | + | if traits.len > 0 { |
|
| 808 | + | if written { |
|
| 809 | + | out.write("\n"); |
|
| 810 | + | } |
|
| 811 | + | writeTraits(out, traits); |
|
| 812 | + | set written = true; |
|
| 813 | + | } |
|
| 747 | 814 | if data.len > 0 { |
|
| 748 | 815 | if written { |
|
| 749 | 816 | out.write("\n"); |
|
| 750 | 817 | } |
|
| 751 | 818 | writeDataDecls(out, data); |
| 756 | 823 | } |
|
| 757 | 824 | } |
|
| 758 | 825 | ||
| 759 | 826 | /// Print a program. |
|
| 760 | 827 | export fn printProgram(out: &mut opaque sexpr::Output, program: &super::Program) { |
|
| 761 | - | writeDecls(out, program.layouts, program.signatures, program.data, program.fns.len > 0); |
|
| 828 | + | writeDecls( |
|
| 829 | + | out, |
|
| 830 | + | program.layouts, |
|
| 831 | + | program.signatures, |
|
| 832 | + | program.traits, |
|
| 833 | + | program.data, |
|
| 834 | + | program.fns.len > 0 |
|
| 835 | + | ); |
|
| 762 | 836 | for func, i in program.fns { |
|
| 763 | 837 | printFn(out, func); |
|
| 764 | 838 | if i < program.fns.len - 1 { |
|
| 765 | 839 | out.write("\n"); |
|
| 766 | 840 | } |
| 776 | 850 | /// before the tables and data are complete, because they are printed first. |
|
| 777 | 851 | export fn printSpooled( |
|
| 778 | 852 | out: &mut opaque sexpr::Output, |
|
| 779 | 853 | layouts: &[super::Layout], |
|
| 780 | 854 | signatures: &[super::Signature], |
|
| 855 | + | traits: &[super::Trait], |
|
| 781 | 856 | data: &[super::Data], |
|
| 782 | 857 | fnText: &[u8] |
|
| 783 | 858 | ) { |
|
| 784 | - | writeDecls(out, layouts, signatures, data, fnText.len > 0); |
|
| 859 | + | writeDecls(out, layouts, signatures, traits, data, fnText.len > 0); |
|
| 785 | 860 | out.write(fnText); |
|
| 786 | 861 | } |
|
| 787 | 862 | ||
| 788 | 863 | /// Print a program to a buffer, returning the written slice. |
|
| 789 | 864 | /// |
lib/std/lang/il/printer/tests.rad
+14 -5
| 82 | 82 | } |
|
| 83 | 83 | super::printFn(&mut fnText, func); |
|
| 84 | 84 | } |
|
| 85 | 85 | let mut split = sexpr::Buffer { buf: &mut SPLIT[..], pos: 0 }; |
|
| 86 | 86 | super::printSpooled( |
|
| 87 | - | &mut split, program.layouts, program.signatures, program.data, &FN_TEXT[.. fnText.pos] |
|
| 87 | + | &mut split, |
|
| 88 | + | program.layouts, |
|
| 89 | + | program.signatures, |
|
| 90 | + | program.traits, |
|
| 91 | + | program.data, |
|
| 92 | + | &FN_TEXT[.. fnText.pos] |
|
| 88 | 93 | ); |
|
| 89 | 94 | ||
| 90 | 95 | try testing::expect(whole.pos < WHOLE.len); |
|
| 91 | 96 | try testing::expect(mem::eq(&WHOLE[.. whole.pos], &SPLIT[.. split.pos])); |
|
| 92 | 97 | } |
| 98 | 103 | try expectSplitMatchesWhole(il::Program { |
|
| 99 | 104 | data: dataList(a, &["a", "b"]), |
|
| 100 | 105 | fns: fnList(a, &["f", "g", "h"]), |
|
| 101 | 106 | layouts: &LAYOUTS[..], |
|
| 102 | 107 | signatures: &[], |
|
| 108 | + | traits: &[], |
|
| 103 | 109 | }); |
|
| 104 | 110 | } |
|
| 105 | 111 | ||
| 106 | 112 | /// Test a program that has data and no functions. |
|
| 107 | 113 | @test unsafe fn testSpooledProgramDataOnly() throws (testing::TestError) { |
|
| 108 | 114 | let mut arena = alloc::new(&mut PROGRAM_STORAGE[..]); |
|
| 109 | 115 | let a: &mut opaque alloc::Allocator = &mut arena; |
|
| 110 | 116 | try expectSplitMatchesWhole(il::Program { |
|
| 111 | - | data: dataList(a, &["a"]), fns: &[], layouts: &LAYOUTS[..], signatures: &[] |
|
| 117 | + | data: dataList(a, &["a"]), fns: &[], layouts: &LAYOUTS[..], signatures: &[], traits: &[] |
|
| 112 | 118 | }); |
|
| 113 | 119 | } |
|
| 114 | 120 | ||
| 115 | 121 | /// Test a program that has functions and no data. |
|
| 116 | 122 | @test unsafe fn testSpooledProgramFnsOnly() throws (testing::TestError) { |
|
| 117 | 123 | let mut arena = alloc::new(&mut PROGRAM_STORAGE[..]); |
|
| 118 | 124 | try expectSplitMatchesWhole(il::Program { |
|
| 119 | - | data: &[], fns: fnList(&mut arena, &["f", "g"]), layouts: &[], signatures: &[] |
|
| 125 | + | data: &[], fns: fnList(&mut arena, &["f", "g"]), layouts: &[], signatures: &[], traits: &[] |
|
| 120 | 126 | }); |
|
| 121 | 127 | } |
|
| 122 | 128 | ||
| 123 | 129 | /// Test an empty program. |
|
| 124 | 130 | @test unsafe fn testSpooledProgramEmpty() throws (testing::TestError) { |
|
| 125 | - | try expectSplitMatchesWhole(il::Program { data: &[], fns: &[], layouts: &[], signatures: &[] }); |
|
| 131 | + | try expectSplitMatchesWhole(il::Program { |
|
| 132 | + | data: &[], fns: &[], layouts: &[], signatures: &[], traits: &[] |
|
| 133 | + | }); |
|
| 126 | 134 | } |
|
| 127 | 135 | ||
| 128 | 136 | /// Test the separators of a program with data and two functions. |
|
| 129 | 137 | @test unsafe fn testProgramSeparators() throws (testing::TestError) { |
|
| 130 | 138 | let mut arena = alloc::new(&mut PROGRAM_STORAGE[..]); |
| 132 | 140 | let program = il::Program { |
|
| 133 | 141 | data: dataList(a, &["a"]), |
|
| 134 | 142 | fns: fnList(a, &["f", "g"]), |
|
| 135 | 143 | layouts: &LAYOUTS[..], |
|
| 136 | 144 | signatures: &[], |
|
| 145 | + | traits: &[], |
|
| 137 | 146 | }; |
|
| 138 | 147 | let mut whole = sexpr::Buffer { buf: &mut WHOLE[..], pos: 0 }; |
|
| 139 | 148 | super::printProgram(&mut whole, &program); |
|
| 140 | 149 | ||
| 141 | 150 | try expectSegments( |
| 319 | 328 | layout: 0, own: true, mutable: false, nullable: true |
|
| 320 | 329 | }), |
|
| 321 | 330 | }, |
|
| 322 | 331 | ]; |
|
| 323 | 332 | let mut out = sexpr::Buffer { buf: &mut TABLES[..], pos: 0 }; |
|
| 324 | - | super::printSpooled(&mut out, &layouts[..], &signatures[..], &[], ""); |
|
| 333 | + | super::printSpooled(&mut out, &layouts[..], &signatures[..], &[], &[], ""); |
|
| 325 | 334 | try expectSegments( |
|
| 326 | 335 | &TABLES[.. out.pos], |
|
| 327 | 336 | &[ |
|
| 328 | 337 | "layout $L0 size 1 align 1 {}\n", |
|
| 329 | 338 | "layout $L1 size 16 align 8 {\n slot 0 ptr own mut $L0;\n slot 8 fn $S1;\n}\n", |
lib/std/lang/il/verify.rad
+194 -0
| 109 | 109 | Package, |
|
| 110 | 110 | /// A layout, by index. |
|
| 111 | 111 | Layout(u32), |
|
| 112 | 112 | /// A signature, by index. |
|
| 113 | 113 | Signature(u32), |
|
| 114 | + | /// A trait, by index. |
|
| 115 | + | Trait(u32), |
|
| 114 | 116 | /// A data definition, by index. |
|
| 115 | 117 | Data(u32), |
|
| 116 | 118 | /// A function, by index. |
|
| 117 | 119 | Fn(u32), |
|
| 118 | 120 | /// An instruction of a function. |
| 386 | 388 | } |
|
| 387 | 389 | try checkType(c, sig.ret, Role::Return, Place::Signature(i)); |
|
| 388 | 390 | } |
|
| 389 | 391 | } |
|
| 390 | 392 | ||
| 393 | + | /// Check the trait table. A method adds its receiver to the parameters of its signature, |
|
| 394 | + | /// at a position within them. |
|
| 395 | + | fn checkTraits(c: &Context) throws (Error) { |
|
| 396 | + | for i in 0 .. c.prog.traits.len { |
|
| 397 | + | for method in c.prog.traits[i].methods { |
|
| 398 | + | let params = c.prog.signatures[method.sig].params.len; |
|
| 399 | + | if params >= c.config.params { |
|
| 400 | + | throw Error { rule: Rule::Limit, place: Place::Trait(i) }; |
|
| 401 | + | } |
|
| 402 | + | if method.receiver > params { |
|
| 403 | + | throw Error { rule: Rule::Layout, place: Place::Trait(i) }; |
|
| 404 | + | } |
|
| 405 | + | } |
|
| 406 | + | } |
|
| 407 | + | } |
|
| 408 | + | ||
| 391 | 409 | /// Check the declarations and the shape of every function, and measure the largest. |
|
| 392 | 410 | /// Registers are below the limit, every block ends with exactly one terminator, and |
|
| 393 | 411 | /// branch targets are blocks of the function. |
|
| 394 | 412 | fn measure(c: &Context) -> Sizes throws (Error) { |
|
| 395 | 413 | let mut sizes = Sizes { registers: 0, blocks: 0, edges: 0 }; |
| 704 | 722 | throw fail(v, Rule::Class); |
|
| 705 | 723 | } |
|
| 706 | 724 | try rights(v, d.slot, own, mutable); |
|
| 707 | 725 | } |
|
| 708 | 726 | ||
| 727 | + | /////////////////// |
|
| 728 | + | // Trait objects // |
|
| 729 | + | /////////////////// |
|
| 730 | + | ||
| 731 | + | /// The trait object slot that a pointer addresses. |
|
| 732 | + | fn objectSlot(v: &Verifier, p: PtrState) -> il::ObjectType throws (Error) { |
|
| 733 | + | let o = try extent(v, p, 0, 0); |
|
| 734 | + | let case il::SlotKind::Trait(object) = layout::slotAt(&v.ctx.table, p.layout, o) else { |
|
| 735 | + | throw fail(v, Rule::Slot); |
|
| 736 | + | }; |
|
| 737 | + | return object; |
|
| 738 | + | } |
|
| 739 | + | ||
| 740 | + | /// Whether the function type `concrete` is the type of method `m` for a receiver `r`: the |
|
| 741 | + | /// signature of the method with a receiver inserted that `r` views, with the rights that |
|
| 742 | + | /// the method needs. |
|
| 743 | + | fn implements(v: &Verifier, concrete: u32, m: il::Method, r: PtrState) -> bool { |
|
| 744 | + | let full = v.ctx.prog.signatures[concrete]; |
|
| 745 | + | let erased = v.ctx.prog.signatures[m.sig]; |
|
| 746 | + | if full.params.len <> erased.params.len + 1 or not typeEq(full.ret, erased.ret) { |
|
| 747 | + | return false; |
|
| 748 | + | } |
|
| 749 | + | for k in 0 .. full.params.len { |
|
| 750 | + | if k == m.receiver { |
|
| 751 | + | let case il::ValType::Ptr(p) = full.params[k] else { |
|
| 752 | + | return false; |
|
| 753 | + | }; |
|
| 754 | + | if p.own <> m.own or p.mutable <> m.mutable or p.nullable { |
|
| 755 | + | return false; |
|
| 756 | + | } |
|
| 757 | + | if not layout::view(&v.ctx.table, r.layout, r.offset as u64, p.layout) { |
|
| 758 | + | return false; |
|
| 759 | + | } |
|
| 760 | + | } else { |
|
| 761 | + | let j = k if k < m.receiver else k - 1; |
|
| 762 | + | if not typeEq(full.params[k], erased.params[j]) { |
|
| 763 | + | return false; |
|
| 764 | + | } |
|
| 765 | + | } |
|
| 766 | + | } |
|
| 767 | + | return true; |
|
| 768 | + | } |
|
| 769 | + | ||
| 770 | + | /// Check that `table` is a method table of trait `iface` for the receiver `r`: read-only |
|
| 771 | + | /// data that holds one function per method, each of the method's type for the receiver. |
|
| 772 | + | fn checkTable(v: &Verifier, table: il::Val, iface: u32, r: PtrState) throws (Error) { |
|
| 773 | + | let case il::Val::DataSym(name) = table else { |
|
| 774 | + | throw fail(v, Rule::Type); |
|
| 775 | + | }; |
|
| 776 | + | let index = dict::get(&v.ctx.dataNames, name) else { |
|
| 777 | + | throw fail(v, Rule::Symbol); |
|
| 778 | + | }; |
|
| 779 | + | let item = v.ctx.prog.data[index as u32]; |
|
| 780 | + | if not item.readOnly { |
|
| 781 | + | throw fail(v, Rule::Mutability); |
|
| 782 | + | } |
|
| 783 | + | let methods = v.ctx.prog.traits[iface].methods; |
|
| 784 | + | let slots = v.ctx.prog.layouts[item.layout].slots; |
|
| 785 | + | // The data checks match each function of the initializer with its slot, so a table |
|
| 786 | + | // with one function per slot holds no zero. |
|
| 787 | + | if slots.len <> methods.len or item.values.len <> methods.len { |
|
| 788 | + | throw fail(v, Rule::Type); |
|
| 789 | + | } |
|
| 790 | + | for k in 0 .. methods.len { |
|
| 791 | + | let case il::SlotKind::Fn(sig) = slots[k].kind else { |
|
| 792 | + | throw fail(v, Rule::Type); |
|
| 793 | + | }; |
|
| 794 | + | let case il::DataItem::Fn(_) = item.values[k].item else { |
|
| 795 | + | throw fail(v, Rule::Type); |
|
| 796 | + | }; |
|
| 797 | + | if slots[k].offset <> k * layout::WORD as u32 or item.values[k].count <> 1 { |
|
| 798 | + | throw fail(v, Rule::Type); |
|
| 799 | + | } |
|
| 800 | + | if not implements(v, sig, methods[k], r) { |
|
| 801 | + | throw fail(v, Rule::Type); |
|
| 802 | + | } |
|
| 803 | + | } |
|
| 804 | + | } |
|
| 805 | + | ||
| 806 | + | /// Check a trait object construction. The slot keeps the receiver, so the receiver has the |
|
| 807 | + | /// class and rights of the slot, as for `store.ptr`. |
|
| 808 | + | fn checkTraitNew(v: &Verifier, dst: il::Reg, recv: il::Reg, table: il::Val) throws (Error) { |
|
| 809 | + | let d = try writable(v, dst); |
|
| 810 | + | let object = try objectSlot(v, d); |
|
| 811 | + | let r = try accessible(v, recv); |
|
| 812 | + | if object.own and not r.own { |
|
| 813 | + | throw fail(v, Rule::Class); |
|
| 814 | + | } |
|
| 815 | + | if not object.own and not r.own and not d.local { |
|
| 816 | + | throw fail(v, Rule::Class); |
|
| 817 | + | } |
|
| 818 | + | if object.mutable and not r.mutable { |
|
| 819 | + | throw fail(v, Rule::Mutability); |
|
| 820 | + | } |
|
| 821 | + | try checkTable(v, table, object.iface, r); |
|
| 822 | + | } |
|
| 823 | + | ||
| 824 | + | /// Check a copy of a trait object into a slot with the same or fewer rights. A slot that |
|
| 825 | + | /// does not own its receiver keeps a borrowed one only in the frame. |
|
| 826 | + | fn checkTraitCopy(v: &Verifier, dst: il::Reg, src: il::Reg) throws (Error) { |
|
| 827 | + | let d = try writable(v, dst); |
|
| 828 | + | let to = try objectSlot(v, d); |
|
| 829 | + | let from = try objectSlot(v, try accessible(v, src)); |
|
| 830 | + | if to.iface <> from.iface { |
|
| 831 | + | throw fail(v, Rule::View); |
|
| 832 | + | } |
|
| 833 | + | if to.own and not from.own { |
|
| 834 | + | throw fail(v, Rule::Class); |
|
| 835 | + | } |
|
| 836 | + | if not to.own and not from.own and not d.local { |
|
| 837 | + | throw fail(v, Rule::Class); |
|
| 838 | + | } |
|
| 839 | + | if to.mutable and not from.mutable { |
|
| 840 | + | throw fail(v, Rule::Mutability); |
|
| 841 | + | } |
|
| 842 | + | } |
|
| 843 | + | ||
| 844 | + | /// Check a method call through a trait object, and define its result. The method needs |
|
| 845 | + | /// no more rights than the object has, and the receiver position holds no argument. |
|
| 846 | + | fn checkTraitCall( |
|
| 847 | + | v: &mut Verifier, |
|
| 848 | + | retTy: il::ValType, |
|
| 849 | + | dst: ?il::Reg, |
|
| 850 | + | obj: il::Reg, |
|
| 851 | + | method: u32, |
|
| 852 | + | receiver: u32, |
|
| 853 | + | args: il::ArgRange |
|
| 854 | + | ) throws (Error) { |
|
| 855 | + | let object = try objectSlot(v, try accessible(v, obj)); |
|
| 856 | + | let methods = v.ctx.prog.traits[object.iface].methods; |
|
| 857 | + | if method >= methods.len { |
|
| 858 | + | throw fail(v, Rule::Call); |
|
| 859 | + | } |
|
| 860 | + | let m = methods[method]; |
|
| 861 | + | if m.own and not object.own { |
|
| 862 | + | throw fail(v, Rule::Class); |
|
| 863 | + | } |
|
| 864 | + | if m.mutable and not object.mutable { |
|
| 865 | + | throw fail(v, Rule::Mutability); |
|
| 866 | + | } |
|
| 867 | + | let erased = SigRef::Table(m.sig); |
|
| 868 | + | if receiver <> m.receiver or not typeEq(retTy, sigRet(&v.ctx, erased)) { |
|
| 869 | + | throw fail(v, Rule::Call); |
|
| 870 | + | } |
|
| 871 | + | let values = il::argVals(v.func, args); |
|
| 872 | + | if values.len <> sigLen(&v.ctx, erased) + 1 { |
|
| 873 | + | throw fail(v, Rule::Arity); |
|
| 874 | + | } |
|
| 875 | + | for k in 0 .. values.len { |
|
| 876 | + | if k == receiver { |
|
| 877 | + | let case il::Val::Undef = values[k] else { |
|
| 878 | + | throw fail(v, Rule::Call); |
|
| 879 | + | }; |
|
| 880 | + | } else { |
|
| 881 | + | let j = k if k < receiver else k - 1; |
|
| 882 | + | try assign(v, values[k], sigParam(&v.ctx, erased, j)); |
|
| 883 | + | } |
|
| 884 | + | } |
|
| 885 | + | if let reg = dst { |
|
| 886 | + | if let case il::ValType::Void = retTy { |
|
| 887 | + | throw fail(v, Rule::Call); |
|
| 888 | + | } |
|
| 889 | + | try define(v, reg, declared(retTy)); |
|
| 890 | + | } |
|
| 891 | + | } |
|
| 892 | + | ||
| 709 | 893 | /////////////////// |
|
| 710 | 894 | // Instructions // |
|
| 711 | 895 | /////////////////// |
|
| 712 | 896 | ||
| 713 | 897 | /// Check a store of a pointer or function value into a slot. |
| 946 | 1130 | } |
|
| 947 | 1131 | } |
|
| 948 | 1132 | case il::Instr::StorePtr { src, dst, offset } => { |
|
| 949 | 1133 | try checkStorePtr(v, src, dst, offset); |
|
| 950 | 1134 | } |
|
| 1135 | + | case il::Instr::TraitNew { dst, recv, table } => { |
|
| 1136 | + | try checkTraitNew(v, dst, recv, table); |
|
| 1137 | + | } |
|
| 1138 | + | case il::Instr::TraitCopy { dst, src } => { |
|
| 1139 | + | try checkTraitCopy(v, dst, src); |
|
| 1140 | + | } |
|
| 1141 | + | case il::Instr::TraitCall { retTy, dst, obj, method, receiver, args } => { |
|
| 1142 | + | try checkTraitCall(v, retTy, dst, obj, method, receiver, args); |
|
| 1143 | + | } |
|
| 951 | 1144 | case il::Instr::BinOp { dst, a, b, .. } => { |
|
| 952 | 1145 | try operand(v, a); |
|
| 953 | 1146 | try operand(v, b); |
|
| 954 | 1147 | try define(v, dst, RegState::Data); |
|
| 955 | 1148 | } |
| 1128 | 1321 | throw STORAGE; |
|
| 1129 | 1322 | }; |
|
| 1130 | 1323 | } |
|
| 1131 | 1324 | let c = Context { prog: *program, config, table, fnNames, dataNames }; |
|
| 1132 | 1325 | try checkSignatures(&c); |
|
| 1326 | + | try checkTraits(&c); |
|
| 1133 | 1327 | return c; |
|
| 1134 | 1328 | } |
|
| 1135 | 1329 | ||
| 1136 | 1330 | /// Check data, function declarations and function bodies. |
|
| 1137 | 1331 | fn finish(c: Context, a: &mut alloc::Arena) throws (Error) { |
lib/std/lang/il/verify/data.rad
+7 -3
| 1 | 1 | //! Data initializer checks of the RIL verifier. |
|
| 2 | 2 | //! |
|
| 3 | 3 | //! Static memory starts as the initializer says, and every byte that the initializer |
|
| 4 | 4 | //! leaves out or marks undefined is zero. Pointer and function slots may start as zero |
|
| 5 | - | //! or hold the address of a symbol that fits the slot. Bytes outside slots are free. |
|
| 5 | + | //! or hold the address of a symbol that fits the slot. Trait object slots start as zero. |
|
| 6 | + | //! Bytes outside slots are free. |
|
| 6 | 7 | ||
| 7 | 8 | use std::collections::dict; |
|
| 8 | 9 | use std::lang::il; |
|
| 9 | 10 | use std::lang::il::verify; |
|
| 10 | 11 | use std::lang::il::verify::layout; |
| 131 | 132 | let data = c.prog.data[live.target]; |
|
| 132 | 133 | if not layout::arrayView(t, data.layout, 0, p.layout, len as u32) { |
|
| 133 | 134 | throw fail(index, verify::Rule::Data); |
|
| 134 | 135 | } |
|
| 135 | 136 | } |
|
| 136 | - | else => panic "atom: leaf slot is a nested object", |
|
| 137 | + | case il::SlotKind::Trait(_) => throw fail(index, verify::Rule::Data), |
|
| 138 | + | else => panic "atom: leaf slot is a nested object", |
|
| 137 | 139 | } |
|
| 138 | 140 | } |
|
| 139 | 141 | ||
| 140 | 142 | /// Check the initializer of data `index` against its layout. |
|
| 141 | 143 | export fn check(c: &verify::Context, index: u32) throws (verify::Error) { |
| 143 | 145 | let data = c.prog.data[index]; |
|
| 144 | 146 | let l = c.prog.layouts[data.layout]; |
|
| 145 | 147 | if data.size <> l.size or data.alignment <> l.alignment { |
|
| 146 | 148 | throw fail(index, verify::Rule::Data); |
|
| 147 | 149 | } |
|
| 148 | - | if t.infos[data.layout].refs or (not data.readOnly and t.infos[data.layout].slices) { |
|
| 150 | + | // Several contexts can write mutable data, and a racing writer could combine the |
|
| 151 | + | // two words of one slice or trait object with those of another. |
|
| 152 | + | if t.infos[data.layout].refs or (not data.readOnly and t.infos[data.layout].pairs) { |
|
| 149 | 153 | throw fail(index, verify::Rule::Data); |
|
| 150 | 154 | } |
|
| 151 | 155 | let mut pos: u64 = 0; |
|
| 152 | 156 | let mut pending: ?Pending = nil; |
|
| 153 | 157 | for value in data.values { |
lib/std/lang/il/verify/layout.rad
+24 -11
| 1 | 1 | //! Layout table checks and queries of the RIL verifier. |
|
| 2 | 2 | //! |
|
| 3 | 3 | //! A layout lists the slots of an object, the byte ranges that hold pointers, |
|
| 4 | - | //! slices and function values. All other bytes are plain data. The decoder |
|
| 4 | + | //! slices, function values and trait objects. All other bytes are plain data. The decoder |
|
| 5 | 5 | //! establishes that slots are sorted, aligned, disjoint and inside the size, and |
|
| 6 | 6 | //! that nested layouts have lower indices than the layouts that contain them. |
|
| 7 | 7 | //! The queries here rely on that, and on table indices that are in range. |
|
| 8 | 8 | //! |
|
| 9 | 9 | //! Descent through nested layouts is bounded by [`verify::Config::layoutDepth`], |
| 15 | 15 | ||
| 16 | 16 | /// Size in bytes of a pointer or function slot. |
|
| 17 | 17 | export constant WORD: u64 = 8; |
|
| 18 | 18 | /// Size in bytes of a slice slot: pointer, length and capacity. |
|
| 19 | 19 | constant SLICE: u64 = 16; |
|
| 20 | + | /// Size in bytes of a trait object slot: receiver and method table. |
|
| 21 | + | constant TRAIT: u64 = 16; |
|
| 20 | 22 | /// Bytes of a slice slot that only the slice instructions write: pointer and length. |
|
| 21 | 23 | /// The capacity that follows is plain data. |
|
| 22 | 24 | export constant SLICE_PROTECTED: u64 = 12; |
|
| 23 | 25 | ||
| 24 | 26 | /// Facts about a layout, computed once for the whole table. |
|
| 25 | 27 | export record Info: Copy { |
|
| 26 | 28 | /// Nesting depth of sub and array nodes. A layout without such nodes has depth zero. |
|
| 27 | 29 | depth: u32, |
|
| 28 | - | /// Offset of the first byte that a pointer, slice or function slot protects, also in |
|
| 29 | - | /// nested nodes, or nil when the layout is plain data. |
|
| 30 | + | /// Offset of the first byte that a pointer, slice, function or trait object slot |
|
| 31 | + | /// protects, also in nested nodes, or nil when the layout is plain data. |
|
| 30 | 32 | first: ?u64, |
|
| 31 | - | /// Whether the layout has a non-owning pointer or slice slot, also in nested nodes. |
|
| 33 | + | /// Whether the layout has a non-owning pointer, slice or trait object slot, also in |
|
| 34 | + | /// nested nodes. |
|
| 32 | 35 | refs: bool, |
|
| 33 | - | /// Whether the layout has a slice slot, also in nested nodes. |
|
| 34 | - | slices: bool, |
|
| 36 | + | /// Whether the layout has a slot of two words that are written together, a slice or |
|
| 37 | + | /// a trait object, also in nested nodes. |
|
| 38 | + | pairs: bool, |
|
| 35 | 39 | } |
|
| 36 | 40 | ||
| 37 | 41 | /// A checked layout table. |
|
| 38 | 42 | export record Table: Copy { |
|
| 39 | 43 | /// The layouts of the program. |
| 67 | 71 | fn absorb(info: &mut Info, child: Info) { |
|
| 68 | 72 | if child.depth + 1 > info.depth { |
|
| 69 | 73 | set info.depth = child.depth + 1; |
|
| 70 | 74 | } |
|
| 71 | 75 | set info.refs = info.refs or child.refs; |
|
| 72 | - | set info.slices = info.slices or child.slices; |
|
| 76 | + | set info.pairs = info.pairs or child.pairs; |
|
| 73 | 77 | } |
|
| 74 | 78 | ||
| 75 | 79 | /// Check the layout table against the limits of `config` and compute the facts about each |
|
| 76 | 80 | /// layout. Owning pointers and slices must not point to layouts with non-owning slots. |
|
| 77 | 81 | export fn build(layouts: *[il::Layout], config: verify::Config, a: &mut alloc::Arena) -> Table |
| 84 | 88 | for i in 0 .. layouts.len { |
|
| 85 | 89 | let layout = layouts[i]; |
|
| 86 | 90 | if layout.size > config.layoutSize { |
|
| 87 | 91 | throw fail(i, verify::Rule::Limit); |
|
| 88 | 92 | } |
|
| 89 | - | let mut info = Info { depth: 0, first: nil, refs: false, slices: false }; |
|
| 93 | + | let mut info = Info { depth: 0, first: nil, refs: false, pairs: false }; |
|
| 90 | 94 | for slot in layout.slots { |
|
| 91 | 95 | match slot.kind { |
|
| 92 | 96 | case il::SlotKind::Ptr(p) => { |
|
| 93 | 97 | set info.refs = info.refs or not p.own; |
|
| 94 | 98 | } |
|
| 95 | 99 | case il::SlotKind::Slice(p) => { |
|
| 96 | - | set info.slices = true; |
|
| 100 | + | set info.pairs = true; |
|
| 97 | 101 | set info.refs = info.refs or not p.own; |
|
| 98 | 102 | } |
|
| 103 | + | case il::SlotKind::Trait(object) => { |
|
| 104 | + | set info.pairs = true; |
|
| 105 | + | set info.refs = info.refs or not object.own; |
|
| 106 | + | } |
|
| 99 | 107 | case il::SlotKind::Fn(_) => {} |
|
| 100 | 108 | case il::SlotKind::Sub(child) => { |
|
| 101 | 109 | if child >= i { |
|
| 102 | 110 | throw fail(i, verify::Rule::Layout); |
|
| 103 | 111 | } |
| 156 | 164 | /// Number of bytes that a slot of kind `kind` covers. |
|
| 157 | 165 | export fn slotWidth(t: &Table, kind: il::SlotKind) -> u64 { |
|
| 158 | 166 | match kind { |
|
| 159 | 167 | case il::SlotKind::Ptr(_), il::SlotKind::Fn(_) => return WORD, |
|
| 160 | 168 | case il::SlotKind::Slice(_) => return SLICE, |
|
| 169 | + | case il::SlotKind::Trait(_) => return TRAIT, |
|
| 161 | 170 | case il::SlotKind::Sub(child) => return size(t, child), |
|
| 162 | 171 | case il::SlotKind::Array { layout, count } => return size(t, layout) * count as u64, |
|
| 163 | 172 | } |
|
| 164 | 173 | } |
|
| 165 | 174 |
| 237 | 246 | ||
| 238 | 247 | /// Offset of the first protected byte of a slot of kind `kind`, from the start of the slot, |
|
| 239 | 248 | /// or nil when the slot protects none. `infos` has the facts about the nested layouts. |
|
| 240 | 249 | fn protectedStart(infos: &[Info], kind: il::SlotKind) -> ?u64 { |
|
| 241 | 250 | match kind { |
|
| 242 | - | case il::SlotKind::Ptr(_), il::SlotKind::Fn(_), il::SlotKind::Slice(_) => return 0, |
|
| 251 | + | case |
|
| 252 | + | il::SlotKind::Ptr(_), |
|
| 253 | + | il::SlotKind::Fn(_), |
|
| 254 | + | il::SlotKind::Slice(_), |
|
| 255 | + | il::SlotKind::Trait(_) => return 0, |
|
| 243 | 256 | case il::SlotKind::Sub(child) => return infos[child].first, |
|
| 244 | 257 | case il::SlotKind::Array { layout, count } => { |
|
| 245 | 258 | if count == 0 { |
|
| 246 | 259 | return nil; |
|
| 247 | 260 | } |
| 263 | 276 | ||
| 264 | 277 | /// Offset of the first protected byte of `slot` at or after byte `rel` of the slot, from the |
|
| 265 | 278 | /// start of the slot, or nil when there is none. The byte `rel` must lie inside the slot. |
|
| 266 | 279 | fn protectedFrom(t: &Table, slot: il::Slot, rel: u64) -> ?u64 { |
|
| 267 | 280 | match slot.kind { |
|
| 268 | - | case il::SlotKind::Ptr(_), il::SlotKind::Fn(_) => return rel, |
|
| 281 | + | case il::SlotKind::Ptr(_), il::SlotKind::Fn(_), il::SlotKind::Trait(_) => return rel, |
|
| 269 | 282 | case il::SlotKind::Slice(_) => { |
|
| 270 | 283 | // The capacity of a slice is plain. |
|
| 271 | 284 | if rel < SLICE_PROTECTED { |
|
| 272 | 285 | return rel; |
|
| 273 | 286 | } |
lib/std/lang/il/verify/tests.rad
+462 -1
| 75 | 75 | arena: alloc::Arena, |
|
| 76 | 76 | /// Layout table. |
|
| 77 | 77 | layouts: *mut [il::Layout], |
|
| 78 | 78 | /// Signature table. |
|
| 79 | 79 | signatures: *mut [il::Signature], |
|
| 80 | + | /// Trait table. |
|
| 81 | + | traits: *mut [il::Trait], |
|
| 80 | 82 | /// Data definitions. |
|
| 81 | 83 | data: *mut [il::Data], |
|
| 82 | 84 | /// Functions. |
|
| 83 | 85 | fns: *mut [*il::Fn], |
|
| 84 | 86 | /// Argument pool shared by the functions. |
| 173 | 175 | unsafe fn newBuilder() -> Builder { |
|
| 174 | 176 | let mut b = Builder { |
|
| 175 | 177 | arena: alloc::new(&mut BUILD[..]), |
|
| 176 | 178 | layouts: &mut [], |
|
| 177 | 179 | signatures: &mut [], |
|
| 180 | + | traits: &mut [], |
|
| 178 | 181 | data: &mut [], |
|
| 179 | 182 | fns: &mut [], |
|
| 180 | 183 | vals: &mut [], |
|
| 181 | 184 | cases: &mut [], |
|
| 182 | 185 | }; |
| 313 | 316 | fn program(b: &mut Builder) -> il::Program { |
|
| 314 | 317 | let data = b.data.take(); |
|
| 315 | 318 | let fns = b.fns.take(); |
|
| 316 | 319 | let layouts = b.layouts.take(); |
|
| 317 | 320 | let signatures = b.signatures.take(); |
|
| 321 | + | let traits = b.traits.take(); |
|
| 318 | 322 | return il::Program { |
|
| 319 | - | data: &data[..], fns: &fns[..], layouts: &layouts[..], signatures: &signatures[..] |
|
| 323 | + | data: &data[..], |
|
| 324 | + | fns: &fns[..], |
|
| 325 | + | layouts: &layouts[..], |
|
| 326 | + | signatures: &signatures[..], |
|
| 327 | + | traits: &traits[..], |
|
| 320 | 328 | }; |
|
| 321 | 329 | } |
|
| 322 | 330 | ||
| 323 | 331 | ///////////////// |
|
| 324 | 332 | // Instruction // |
| 2404 | 2412 | copy(0, imm(5)), reserve(1, P8), add(2, reg(0), reg(0)), jmp(1, none()) |
|
| 2405 | 2413 | ]); |
|
| 2406 | 2414 | addBlocks(&mut b, "main", &[], dataType(), &[entry, reader, writer]); |
|
| 2407 | 2415 | try accepted(&mut b); |
|
| 2408 | 2416 | } |
|
| 2417 | + | ||
| 2418 | + | /////////////////// |
|
| 2419 | + | // Trait objects // |
|
| 2420 | + | /////////////////// |
|
| 2421 | + | ||
| 2422 | + | /// Layouts of the trait objects of trait 0. |
|
| 2423 | + | record Objects: Copy { |
|
| 2424 | + | /// A trait object that borrows a writable receiver. |
|
| 2425 | + | object: u32, |
|
| 2426 | + | /// A trait object that borrows a read-only receiver. |
|
| 2427 | + | constObject: u32, |
|
| 2428 | + | /// A trait object that owns a writable receiver. |
|
| 2429 | + | ownObject: u32, |
|
| 2430 | + | } |
|
| 2431 | + | ||
| 2432 | + | /// A function value slot of signature `sig` at `offset`. |
|
| 2433 | + | fn fnSlot(offset: u32, sig: u32) -> il::Slot { |
|
| 2434 | + | return il::Slot { offset, kind: il::SlotKind::Fn(sig) }; |
|
| 2435 | + | } |
|
| 2436 | + | ||
| 2437 | + | /// A trait object slot of trait 0 at offset 0. |
|
| 2438 | + | fn objectSlot(own: bool, mutable: bool) -> il::Slot { |
|
| 2439 | + | return il::Slot { |
|
| 2440 | + | offset: 0, |
|
| 2441 | + | kind: il::SlotKind::Trait(il::ObjectType { |
|
| 2442 | + | iface: 0, own, mutable |
|
| 2443 | + | }), |
|
| 2444 | + | }; |
|
| 2445 | + | } |
|
| 2446 | + | ||
| 2447 | + | /// Add a trait, and return its index. |
|
| 2448 | + | fn addTrait(b: &mut Builder, methods: &[il::Method]) -> u32 { |
|
| 2449 | + | let index = b.traits.len; |
|
| 2450 | + | let mut copy: *mut [il::Method] = &mut []; |
|
| 2451 | + | try! copy.extend(methods, &mut b.arena); |
|
| 2452 | + | try! b.traits.append(il::Trait { methods: ©[..] }, &mut b.arena); |
|
| 2453 | + | return index; |
|
| 2454 | + | } |
|
| 2455 | + | ||
| 2456 | + | /// Add trait 0, whose method 0 reads its receiver and returns data, and whose method 1 |
|
| 2457 | + | /// writes it and takes data. Add `get` and `put`, which implement them for a `P4` receiver, |
|
| 2458 | + | /// and their read-only method table `table`. Add the read-only table `short`, which lacks |
|
| 2459 | + | /// method 1, the writable table `open`, and the table `cells` of the methods for a `CELL` |
|
| 2460 | + | /// receiver. |
|
| 2461 | + | fn addObjects(b: &mut Builder) -> Objects { |
|
| 2462 | + | let getSig = addSignature(b, &[], dataType()); |
|
| 2463 | + | let putSig = addSignature(b, &[dataType()], il::ValType::Void); |
|
| 2464 | + | let getP4 = addSignature(b, &[ptrType(P4, false, false, false)], dataType()); |
|
| 2465 | + | let putP4 = addSignature( |
|
| 2466 | + | b, &[ptrType(P4, false, true, false), dataType()], il::ValType::Void |
|
| 2467 | + | ); |
|
| 2468 | + | let getCell = addSignature(b, &[ptrType(CELL, false, false, false)], dataType()); |
|
| 2469 | + | let putCell = addSignature( |
|
| 2470 | + | b, &[ptrType(CELL, false, true, false), dataType()], il::ValType::Void |
|
| 2471 | + | ); |
|
| 2472 | + | addTrait( |
|
| 2473 | + | b, |
|
| 2474 | + | &[ |
|
| 2475 | + | il::Method { sig: getSig, receiver: 0, own: false, mutable: false }, |
|
| 2476 | + | il::Method { sig: putSig, receiver: 0, own: false, mutable: true }, |
|
| 2477 | + | ] |
|
| 2478 | + | ); |
|
| 2479 | + | let table = addLayout(b, 16, 8, &[fnSlot(0, getP4), fnSlot(8, putP4)]); |
|
| 2480 | + | let short = addLayout(b, 8, 8, &[fnSlot(0, getP4)]); |
|
| 2481 | + | let cells = addLayout(b, 16, 8, &[fnSlot(0, getCell), fnSlot(8, putCell)]); |
|
| 2482 | + | addFn(b, "get", &[param(0, ptrType(P4, false, false, false))], dataType(), &[ |
|
| 2483 | + | load(1, 0, 0), ret(reg(1)) |
|
| 2484 | + | ]); |
|
| 2485 | + | addFn( |
|
| 2486 | + | b, |
|
| 2487 | + | "put", |
|
| 2488 | + | &[param(0, ptrType(P4, false, true, false)), param(1, dataType())], |
|
| 2489 | + | il::ValType::Void, |
|
| 2490 | + | &[store(reg(1), 0, 0), retVoid()] |
|
| 2491 | + | ); |
|
| 2492 | + | addFn(b, "getCell", &[param(0, ptrType(CELL, false, false, false))], dataType(), &[ |
|
| 2493 | + | load64(1, 0, 0), ret(reg(1)) |
|
| 2494 | + | ]); |
|
| 2495 | + | addFn( |
|
| 2496 | + | b, |
|
| 2497 | + | "putCell", |
|
| 2498 | + | &[param(0, ptrType(CELL, false, true, false)), param(1, dataType())], |
|
| 2499 | + | il::ValType::Void, |
|
| 2500 | + | &[retVoid()] |
|
| 2501 | + | ); |
|
| 2502 | + | addData(b, "table", table, true, &[function("get"), function("put")]); |
|
| 2503 | + | addData(b, "open", table, false, &[function("get"), function("put")]); |
|
| 2504 | + | addData(b, "short", short, true, &[function("get")]); |
|
| 2505 | + | addData(b, "cells", cells, true, &[function("getCell"), function("putCell")]); |
|
| 2506 | + | return Objects { |
|
| 2507 | + | object: addLayout(b, 16, 8, &[objectSlot(false, true)]), |
|
| 2508 | + | constObject: addLayout(b, 16, 8, &[objectSlot(false, false)]), |
|
| 2509 | + | ownObject: addLayout(b, 16, 8, &[objectSlot(true, true)]), |
|
| 2510 | + | }; |
|
| 2511 | + | } |
|
| 2512 | + | ||
| 2513 | + | /// Build a trait object in the slot at `dst` from the receiver `recv` and the table `table`. |
|
| 2514 | + | fn traitNew(dst: u32, recv: u32, table: il::Val) -> il::Instr { |
|
| 2515 | + | return il::Instr::TraitNew { dst: il::Reg { n: dst }, recv: il::Reg { n: recv }, table }; |
|
| 2516 | + | } |
|
| 2517 | + | ||
| 2518 | + | /// Call method `method` of the trait object at `obj`, with the receiver at `receiver`. |
|
| 2519 | + | fn traitCall( |
|
| 2520 | + | dst: ?u32, retTy: il::ValType, obj: u32, method: u32, receiver: u32, values: il::ArgRange |
|
| 2521 | + | ) -> il::Instr { |
|
| 2522 | + | let mut result: ?il::Reg = nil; |
|
| 2523 | + | if let n = dst { |
|
| 2524 | + | set result = il::Reg { n }; |
|
| 2525 | + | } |
|
| 2526 | + | return il::Instr::TraitCall { |
|
| 2527 | + | retTy, dst: result, obj: il::Reg { n: obj }, method, receiver, args: values |
|
| 2528 | + | }; |
|
| 2529 | + | } |
|
| 2530 | + | ||
| 2531 | + | /// Check `main`, which builds a trait object of layout `object` in its frame from a |
|
| 2532 | + | /// receiver of layout `recv` in its frame and the table `table`, and then runs `instrs`. |
|
| 2533 | + | /// The receiver is register 0 and the object register 1. |
|
| 2534 | + | unsafe fn objectCase( |
|
| 2535 | + | recv: u32, object: u32, table: *[u8], instrs: &[il::Instr], rule: ?verify::Rule |
|
| 2536 | + | ) throws (testing::TestError) { |
|
| 2537 | + | let mut b = newBuilder(); |
|
| 2538 | + | addObjects(&mut b); |
|
| 2539 | + | let mut body: *mut [il::Instr] = &mut []; |
|
| 2540 | + | try! body.append(reserve(0, recv), &mut b.arena); |
|
| 2541 | + | try! body.append(reserve(1, object), &mut b.arena); |
|
| 2542 | + | try! body.append(traitNew(1, 0, il::Val::DataSym(table)), &mut b.arena); |
|
| 2543 | + | try! body.extend(instrs, &mut b.arena); |
|
| 2544 | + | try! body.append(retVoid(), &mut b.arena); |
|
| 2545 | + | try checkIn(&mut b, &[], il::ValType::Void, &body[..], rule); |
|
| 2546 | + | } |
|
| 2547 | + | ||
| 2548 | + | /// Layout of the trait objects that [`objectCase`] builds, which [`addObjects`] adds after |
|
| 2549 | + | /// the tables. The first is the object, then the read-only and the owning object. |
|
| 2550 | + | constant OBJECT: u32 = 19; |
|
| 2551 | + | ||
| 2552 | + | /// Check that a trait object built from a receiver and its method table can call the |
|
| 2553 | + | /// methods that its rights allow, and that the methods get the receiver. |
|
| 2554 | + | @test unsafe fn traitObjectCalls() throws (testing::TestError) { |
|
| 2555 | + | let mut b = newBuilder(); |
|
| 2556 | + | let objects = addObjects(&mut b); |
|
| 2557 | + | try testing::expect(objects.object == OBJECT); |
|
| 2558 | + | let get = args(&mut b, &[il::Val::Undef]); |
|
| 2559 | + | let put = args(&mut b, &[il::Val::Undef, imm(5)]); |
|
| 2560 | + | try checkIn( |
|
| 2561 | + | &mut b, |
|
| 2562 | + | &[], |
|
| 2563 | + | il::ValType::Void, |
|
| 2564 | + | &[ |
|
| 2565 | + | reserve(0, P4), |
|
| 2566 | + | reserve(1, objects.object), |
|
| 2567 | + | traitNew(1, 0, il::Val::DataSym("table")), |
|
| 2568 | + | traitCall(2, dataType(), 1, 0, 0, get), |
|
| 2569 | + | traitCall(nil, il::ValType::Void, 1, 1, 0, put), |
|
| 2570 | + | retVoid(), |
|
| 2571 | + | ], |
|
| 2572 | + | nil |
|
| 2573 | + | ); |
|
| 2574 | + | ||
| 2575 | + | // The rights to the receiver are those of the slot, also through a read-only pointer |
|
| 2576 | + | // to the object. |
|
| 2577 | + | let mut c = newBuilder(); |
|
| 2578 | + | let held = addObjects(&mut c); |
|
| 2579 | + | let putHeld = args(&mut c, &[il::Val::Undef, imm(5)]); |
|
| 2580 | + | try checkIn( |
|
| 2581 | + | &mut c, |
|
| 2582 | + | &[param(0, ptrType(held.object, false, false, false))], |
|
| 2583 | + | il::ValType::Void, |
|
| 2584 | + | &[ |
|
| 2585 | + | traitCall(nil, il::ValType::Void, 0, 1, 0, putHeld), retVoid() |
|
| 2586 | + | ], |
|
| 2587 | + | nil |
|
| 2588 | + | ); |
|
| 2589 | + | } |
|
| 2590 | + | ||
| 2591 | + | /// Check that a trait object is built only from a receiver that its method table fits: |
|
| 2592 | + | /// read-only data with a function of the right type for each method. |
|
| 2593 | + | @test unsafe fn traitObjectTables() throws (testing::TestError) { |
|
| 2594 | + | try objectCase(P4, OBJECT, "table", &[], nil); |
|
| 2595 | + | // The table is for another receiver layout, or the receiver views another layout. |
|
| 2596 | + | try objectCase(P4, OBJECT, "cells", &[], verify::Rule::Type); |
|
| 2597 | + | try objectCase(CELL, OBJECT, "table", &[], verify::Rule::Type); |
|
| 2598 | + | try objectCase(CELL, OBJECT, "cells", &[], nil); |
|
| 2599 | + | // A table that lacks a method, or that can be written. |
|
| 2600 | + | try objectCase(P4, OBJECT, "short", &[], verify::Rule::Type); |
|
| 2601 | + | try objectCase(P4, OBJECT, "open", &[], verify::Rule::Mutability); |
|
| 2602 | + | try objectCase(P4, OBJECT, "missing", &[], verify::Rule::Symbol); |
|
| 2603 | + | ||
| 2604 | + | // A table must be a symbol, not a value. |
|
| 2605 | + | let mut b = newBuilder(); |
|
| 2606 | + | addObjects(&mut b); |
|
| 2607 | + | try checkIn( |
|
| 2608 | + | &mut b, |
|
| 2609 | + | &[], |
|
| 2610 | + | il::ValType::Void, |
|
| 2611 | + | &[ |
|
| 2612 | + | reserve(0, P4), reserve(1, OBJECT), traitNew(1, 0, imm(0)), retVoid() |
|
| 2613 | + | ], |
|
| 2614 | + | verify::Rule::Type |
|
| 2615 | + | ); |
|
| 2616 | + | ||
| 2617 | + | // The destination must be a trait object slot. |
|
| 2618 | + | let mut c = newBuilder(); |
|
| 2619 | + | addObjects(&mut c); |
|
| 2620 | + | try checkIn( |
|
| 2621 | + | &mut c, |
|
| 2622 | + | &[], |
|
| 2623 | + | il::ValType::Void, |
|
| 2624 | + | &[ |
|
| 2625 | + | reserve(0, P4), reserve(1, P16), traitNew(1, 0, il::Val::DataSym("table")), retVoid() |
|
| 2626 | + | ], |
|
| 2627 | + | verify::Rule::Slot |
|
| 2628 | + | ); |
|
| 2629 | + | } |
|
| 2630 | + | ||
| 2631 | + | /// Check that the receiver of a trait object has the class and rights of its slot. |
|
| 2632 | + | @test unsafe fn traitObjectReceivers() throws (testing::TestError) { |
|
| 2633 | + | // An owning slot needs an owning receiver. |
|
| 2634 | + | try objectCase(P4, OBJECT + 2, "table", &[], verify::Rule::Class); |
|
| 2635 | + | ||
| 2636 | + | // A borrowed receiver stays in the frame. |
|
| 2637 | + | let mut b = newBuilder(); |
|
| 2638 | + | addObjects(&mut b); |
|
| 2639 | + | try checkIn( |
|
| 2640 | + | &mut b, |
|
| 2641 | + | &[param(0, ptrType(OBJECT, false, true, false))], |
|
| 2642 | + | il::ValType::Void, |
|
| 2643 | + | &[ |
|
| 2644 | + | reserve(1, P4), traitNew(0, 1, il::Val::DataSym("table")), retVoid() |
|
| 2645 | + | ], |
|
| 2646 | + | verify::Rule::Class |
|
| 2647 | + | ); |
|
| 2648 | + | ||
| 2649 | + | // A writable slot needs a writable receiver. |
|
| 2650 | + | let mut c = newBuilder(); |
|
| 2651 | + | addObjects(&mut c); |
|
| 2652 | + | try checkIn( |
|
| 2653 | + | &mut c, |
|
| 2654 | + | &[param(0, ptrType(P4, false, false, false))], |
|
| 2655 | + | il::ValType::Void, |
|
| 2656 | + | &[ |
|
| 2657 | + | reserve(1, OBJECT), traitNew(1, 0, il::Val::DataSym("table")), retVoid() |
|
| 2658 | + | ], |
|
| 2659 | + | verify::Rule::Mutability |
|
| 2660 | + | ); |
|
| 2661 | + | let mut d = newBuilder(); |
|
| 2662 | + | addObjects(&mut d); |
|
| 2663 | + | try checkIn( |
|
| 2664 | + | &mut d, |
|
| 2665 | + | &[param(0, ptrType(P4, false, false, false))], |
|
| 2666 | + | il::ValType::Void, |
|
| 2667 | + | &[ |
|
| 2668 | + | reserve(1, OBJECT + 1), traitNew(1, 0, il::Val::DataSym("table")), retVoid() |
|
| 2669 | + | ], |
|
| 2670 | + | nil |
|
| 2671 | + | ); |
|
| 2672 | + | } |
|
| 2673 | + | ||
| 2674 | + | /// Check one call through the object that [`objectCase`] builds from a `P4` receiver. |
|
| 2675 | + | unsafe fn methodCase( |
|
| 2676 | + | object: u32, |
|
| 2677 | + | dst: ?u32, |
|
| 2678 | + | retTy: il::ValType, |
|
| 2679 | + | method: u32, |
|
| 2680 | + | receiver: u32, |
|
| 2681 | + | values: &[il::Val], |
|
| 2682 | + | rule: ?verify::Rule |
|
| 2683 | + | ) throws (testing::TestError) { |
|
| 2684 | + | let mut b = newBuilder(); |
|
| 2685 | + | addObjects(&mut b); |
|
| 2686 | + | let range = args(&mut b, values); |
|
| 2687 | + | try checkIn( |
|
| 2688 | + | &mut b, |
|
| 2689 | + | &[], |
|
| 2690 | + | il::ValType::Void, |
|
| 2691 | + | &[ |
|
| 2692 | + | reserve(0, P4), |
|
| 2693 | + | reserve(1, object), |
|
| 2694 | + | traitNew(1, 0, il::Val::DataSym("table")), |
|
| 2695 | + | traitCall(dst, retTy, 1, method, receiver, range), |
|
| 2696 | + | retVoid(), |
|
| 2697 | + | ], |
|
| 2698 | + | rule |
|
| 2699 | + | ); |
|
| 2700 | + | } |
|
| 2701 | + | ||
| 2702 | + | /// Check the method, receiver position, arguments and result of a trait call. |
|
| 2703 | + | @test unsafe fn traitObjectMethods() throws (testing::TestError) { |
|
| 2704 | + | let none = il::Val::Undef; |
|
| 2705 | + | try methodCase(OBJECT, 2, dataType(), 0, 0, &[none], nil); |
|
| 2706 | + | try methodCase(OBJECT, nil, il::ValType::Void, 1, 0, &[none, imm(1)], nil); |
|
| 2707 | + | // A read-only object reads, and does not write. |
|
| 2708 | + | try methodCase(OBJECT + 1, 2, dataType(), 0, 0, &[none], nil); |
|
| 2709 | + | try methodCase( |
|
| 2710 | + | OBJECT + 1, nil, il::ValType::Void, 1, 0, &[none, imm(1)], verify::Rule::Mutability |
|
| 2711 | + | ); |
|
| 2712 | + | // The method exists, and the receiver is at its position, with no argument there. |
|
| 2713 | + | try methodCase(OBJECT, 2, dataType(), 2, 0, &[none], verify::Rule::Call); |
|
| 2714 | + | try methodCase(OBJECT, nil, il::ValType::Void, 1, 1, &[imm(1), none], verify::Rule::Call); |
|
| 2715 | + | try methodCase(OBJECT, 2, dataType(), 0, 0, &[imm(1)], verify::Rule::Call); |
|
| 2716 | + | try methodCase(OBJECT, 2, dataType(), 0, 0, &[reg(0)], verify::Rule::Call); |
|
| 2717 | + | // The arguments and the result fit the method. |
|
| 2718 | + | try methodCase(OBJECT, 2, dataType(), 0, 0, &[none, imm(1)], verify::Rule::Arity); |
|
| 2719 | + | try methodCase(OBJECT, nil, il::ValType::Void, 1, 0, &[none], verify::Rule::Arity); |
|
| 2720 | + | try methodCase(OBJECT, nil, il::ValType::Void, 0, 0, &[none], verify::Rule::Call); |
|
| 2721 | + | try methodCase(OBJECT, 2, il::ValType::Void, 0, 0, &[none], verify::Rule::Call); |
|
| 2722 | + | } |
|
| 2723 | + | ||
| 2724 | + | /// Check that only the trait instructions write or read a trait object slot as such. |
|
| 2725 | + | @test unsafe fn traitObjectSlots() throws (testing::TestError) { |
|
| 2726 | + | try objectCase(P4, OBJECT, "table", &[store64(imm(0), 1, 8)], verify::Rule::Slot); |
|
| 2727 | + | try objectCase(P4, OBJECT, "table", &[store(imm(0), 1, 4)], verify::Rule::Slot); |
|
| 2728 | + | try objectCase(P4, OBJECT, "table", &[storePtr(imm(0), 1, 0)], verify::Rule::Slot); |
|
| 2729 | + | try objectCase(P4, OBJECT, "table", &[loadPtr(2, 1, 8)], verify::Rule::Slot); |
|
| 2730 | + | // Reading the bytes as data is allowed, and so is copying the whole object. |
|
| 2731 | + | try objectCase(P4, OBJECT, "table", &[load64(2, 1, 8)], nil); |
|
| 2732 | + | try objectCase(P4, OBJECT, "table", &[reserve(2, OBJECT), blit(2, 1, OBJECT)], nil); |
|
| 2733 | + | // The copy keeps a borrowed receiver in the frame. |
|
| 2734 | + | let mut b = newBuilder(); |
|
| 2735 | + | addObjects(&mut b); |
|
| 2736 | + | try checkIn( |
|
| 2737 | + | &mut b, |
|
| 2738 | + | &[param(0, ptrType(OBJECT, false, true, false))], |
|
| 2739 | + | il::ValType::Void, |
|
| 2740 | + | &[ |
|
| 2741 | + | reserve(1, P4), |
|
| 2742 | + | reserve(2, OBJECT), |
|
| 2743 | + | traitNew(2, 1, il::Val::DataSym("table")), |
|
| 2744 | + | blit(0, 2, OBJECT), |
|
| 2745 | + | retVoid(), |
|
| 2746 | + | ], |
|
| 2747 | + | verify::Rule::Class |
|
| 2748 | + | ); |
|
| 2749 | + | // A call needs a trait object slot. |
|
| 2750 | + | let mut c = newBuilder(); |
|
| 2751 | + | addObjects(&mut c); |
|
| 2752 | + | let get = args(&mut c, &[il::Val::Undef]); |
|
| 2753 | + | try checkIn( |
|
| 2754 | + | &mut c, |
|
| 2755 | + | &[], |
|
| 2756 | + | il::ValType::Void, |
|
| 2757 | + | &[ |
|
| 2758 | + | reserve(0, P16), traitCall(1, dataType(), 0, 0, 0, get), retVoid() |
|
| 2759 | + | ], |
|
| 2760 | + | verify::Rule::Slot |
|
| 2761 | + | ); |
|
| 2762 | + | } |
|
| 2763 | + | ||
| 2764 | + | /// Check that static trait objects are owning, read-only and zero, and that the trait table |
|
| 2765 | + | /// places receivers within the parameters. |
|
| 2766 | + | @test unsafe fn traitObjectTablesAndData() throws (testing::TestError) { |
|
| 2767 | + | let mut b = newBuilder(); |
|
| 2768 | + | addObjects(&mut b); |
|
| 2769 | + | addData(&mut b, "d", OBJECT + 2, true, &[repeat(il::DataItem::Undef, 16)]); |
|
| 2770 | + | try accepted(&mut b); |
|
| 2771 | + | // Several contexts can write mutable data, and a borrowed receiver would outlive its |
|
| 2772 | + | // frame. |
|
| 2773 | + | let mut c = newBuilder(); |
|
| 2774 | + | addObjects(&mut c); |
|
| 2775 | + | addData(&mut c, "d", OBJECT + 2, false, &[repeat(il::DataItem::Undef, 16)]); |
|
| 2776 | + | try rejected(&mut c, verify::Rule::Data); |
|
| 2777 | + | let mut d = newBuilder(); |
|
| 2778 | + | addObjects(&mut d); |
|
| 2779 | + | addData(&mut d, "d", OBJECT, true, &[repeat(il::DataItem::Undef, 16)]); |
|
| 2780 | + | try rejected(&mut d, verify::Rule::Data); |
|
| 2781 | + | // An initializer cannot build an object. |
|
| 2782 | + | let mut e = newBuilder(); |
|
| 2783 | + | addObjects(&mut e); |
|
| 2784 | + | addData(&mut e, "d", OBJECT + 2, true, &[symbol("table"), symbol("table")]); |
|
| 2785 | + | try rejected(&mut e, verify::Rule::Data); |
|
| 2786 | + | // A receiver goes among the parameters of the method. |
|
| 2787 | + | let mut f = newBuilder(); |
|
| 2788 | + | addTrait(&mut f, &[il::Method { sig: SIG_VOID, receiver: 1, own: false, mutable: false }]); |
|
| 2789 | + | try rejected(&mut f, verify::Rule::Layout); |
|
| 2790 | + | // An owning pointer cannot point to an object with a borrowed receiver. |
|
| 2791 | + | let mut g = newBuilder(); |
|
| 2792 | + | addObjects(&mut g); |
|
| 2793 | + | addLayout(&mut g, 8, 8, &[ptrSlot(0, OBJECT, true, false)]); |
|
| 2794 | + | try rejected(&mut g, verify::Rule::Layout); |
|
| 2795 | + | } |
|
| 2796 | + | ||
| 2797 | + | /// Copy the trait object at `src` to the slot at `dst`. |
|
| 2798 | + | fn traitCopy(dst: u32, src: u32) -> il::Instr { |
|
| 2799 | + | return il::Instr::TraitCopy { dst: il::Reg { n: dst }, src: il::Reg { n: src } }; |
|
| 2800 | + | } |
|
| 2801 | + | ||
| 2802 | + | /// Check that a trait object is copied only to a slot of the same trait with the same or |
|
| 2803 | + | /// fewer rights, and that a borrowed receiver stays in the frame. |
|
| 2804 | + | @test unsafe fn traitObjectCopies() throws (testing::TestError) { |
|
| 2805 | + | try objectCase(P4, OBJECT, "table", &[reserve(2, OBJECT + 1), traitCopy(2, 1)], nil); |
|
| 2806 | + | try objectCase(P4, OBJECT, "table", &[reserve(2, OBJECT), traitCopy(2, 1)], nil); |
|
| 2807 | + | // A copy cannot gain the right to write, or ownership. |
|
| 2808 | + | let mut b = newBuilder(); |
|
| 2809 | + | addObjects(&mut b); |
|
| 2810 | + | try checkIn( |
|
| 2811 | + | &mut b, |
|
| 2812 | + | &[param(0, ptrType(P4, false, false, false))], |
|
| 2813 | + | il::ValType::Void, |
|
| 2814 | + | &[ |
|
| 2815 | + | reserve(1, OBJECT + 1), |
|
| 2816 | + | traitNew(1, 0, il::Val::DataSym("table")), |
|
| 2817 | + | reserve(2, OBJECT), |
|
| 2818 | + | traitCopy(2, 1), |
|
| 2819 | + | retVoid(), |
|
| 2820 | + | ], |
|
| 2821 | + | verify::Rule::Mutability |
|
| 2822 | + | ); |
|
| 2823 | + | try objectCase( |
|
| 2824 | + | P4, OBJECT, "table", &[reserve(2, OBJECT + 2), traitCopy(2, 1)], verify::Rule::Class |
|
| 2825 | + | ); |
|
| 2826 | + | // A borrowed receiver is not copied out of the frame. |
|
| 2827 | + | let mut c = newBuilder(); |
|
| 2828 | + | addObjects(&mut c); |
|
| 2829 | + | try checkIn( |
|
| 2830 | + | &mut c, |
|
| 2831 | + | &[param(0, ptrType(OBJECT, false, true, false))], |
|
| 2832 | + | il::ValType::Void, |
|
| 2833 | + | &[ |
|
| 2834 | + | reserve(1, P4), |
|
| 2835 | + | reserve(2, OBJECT), |
|
| 2836 | + | traitNew(2, 1, il::Val::DataSym("table")), |
|
| 2837 | + | traitCopy(0, 2), |
|
| 2838 | + | retVoid(), |
|
| 2839 | + | ], |
|
| 2840 | + | verify::Rule::Class |
|
| 2841 | + | ); |
|
| 2842 | + | // Both slots are trait objects of the same trait. |
|
| 2843 | + | try objectCase(P4, OBJECT, "table", &[reserve(2, P16), traitCopy(2, 1)], verify::Rule::Slot); |
|
| 2844 | + | let mut d = newBuilder(); |
|
| 2845 | + | addObjects(&mut d); |
|
| 2846 | + | addTrait(&mut d, &[]); |
|
| 2847 | + | let other = addLayout(&mut d, 16, 8, &[ |
|
| 2848 | + | il::Slot { |
|
| 2849 | + | offset: 0, |
|
| 2850 | + | kind: il::SlotKind::Trait(il::ObjectType { |
|
| 2851 | + | iface: 1, own: false, mutable: false |
|
| 2852 | + | }), |
|
| 2853 | + | }, |
|
| 2854 | + | ]); |
|
| 2855 | + | try checkIn( |
|
| 2856 | + | &mut d, |
|
| 2857 | + | &[], |
|
| 2858 | + | il::ValType::Void, |
|
| 2859 | + | &[ |
|
| 2860 | + | reserve(0, P4), |
|
| 2861 | + | reserve(1, OBJECT), |
|
| 2862 | + | traitNew(1, 0, il::Val::DataSym("table")), |
|
| 2863 | + | reserve(2, other), |
|
| 2864 | + | traitCopy(2, 1), |
|
| 2865 | + | retVoid(), |
|
| 2866 | + | ], |
|
| 2867 | + | verify::Rule::View |
|
| 2868 | + | ); |
|
| 2869 | + | } |
lib/std/lang/lower.rad
+243 -52
| 352 | 352 | package: u16, |
|
| 353 | 353 | /// Index of the first layout of the package. |
|
| 354 | 354 | layouts: u32, |
|
| 355 | 355 | /// Index of the first signature of the package. |
|
| 356 | 356 | signatures: u32, |
|
| 357 | + | /// Index of the first trait of the package. |
|
| 358 | + | traits: u32, |
|
| 357 | 359 | } |
|
| 358 | 360 | ||
| 359 | - | /// The layout and signature tables of a lowering pass. |
|
| 361 | + | /// Index of a trait in the trait table of the current package. |
|
| 362 | + | record TraitIndex: Copy { |
|
| 363 | + | /// The trait. |
|
| 364 | + | id: resolver::TraitId, |
|
| 365 | + | /// Package-relative index in the trait table. |
|
| 366 | + | index: u32, |
|
| 367 | + | } |
|
| 368 | + | ||
| 369 | + | /// The layout, signature and trait tables of a lowering pass. |
|
| 360 | 370 | /// |
|
| 361 | 371 | /// Indices in IL are relative to the tables of the current package. The tables of all |
|
| 362 | 372 | /// packages are stored one after the other, and `layoutBase` and `signatureBase` locate |
|
| 363 | 373 | /// the current ones. With a program-wide scope the bases stay zero. |
|
| 364 | 374 | export record Tables { |
| 368 | 378 | signatures: *mut [il::Signature], |
|
| 369 | 379 | /// Index in `layouts` of the first layout of the current package. |
|
| 370 | 380 | layoutBase: u32, |
|
| 371 | 381 | /// Index in `signatures` of the first signature of the current package. |
|
| 372 | 382 | signatureBase: u32, |
|
| 383 | + | /// Traits of all packages. |
|
| 384 | + | traits: *mut [il::Trait], |
|
| 385 | + | /// Index in `traits` of the first trait of the current package. |
|
| 386 | + | traitBase: u32, |
|
| 387 | + | /// Traits of the current package that have an index. |
|
| 388 | + | traitIndices: *mut [TraitIndex], |
|
| 373 | 389 | /// Where the tables of each lowered package start, in lowering order. |
|
| 374 | 390 | tableStarts: *mut [TableStart], |
|
| 375 | 391 | /// Package-relative indices of layouts that are found by content: all layouts |
|
| 376 | 392 | /// without slots, and the layouts of results. |
|
| 377 | 393 | sharedLayouts: *mut [u32], |
| 381 | 397 | nominalLayouts: *mut [NominalLayout], |
|
| 382 | 398 | /// Layouts that were numbered and whose slots are not computed yet. |
|
| 383 | 399 | fillQueue: *mut [PendingLayout], |
|
| 384 | 400 | } |
|
| 385 | 401 | ||
| 386 | - | /// Layout and signature tables of one package. |
|
| 402 | + | /// Layout, signature and trait tables of one package. |
|
| 387 | 403 | export record PackageTables: Copy { |
|
| 388 | 404 | /// Layouts that the functions and data of the package refer to. |
|
| 389 | 405 | layouts: *[il::Layout], |
|
| 390 | 406 | /// Signatures that the function types of the package refer to. |
|
| 391 | 407 | signatures: *[il::Signature], |
|
| 408 | + | /// Traits that the trait objects of the package refer to. |
|
| 409 | + | traits: *[il::Trait], |
|
| 392 | 410 | } |
|
| 393 | 411 | ||
| 394 | 412 | /// Consumer of the functions produced by the lowerer. |
|
| 395 | 413 | /// |
|
| 396 | 414 | /// The lowerer calls `emit` once for each finished function, after [`endFn`] |
| 1196 | 1214 | tables: Tables { |
|
| 1197 | 1215 | layouts: &mut [], |
|
| 1198 | 1216 | signatures: &mut [], |
|
| 1199 | 1217 | layoutBase: 0, |
|
| 1200 | 1218 | signatureBase: 0, |
|
| 1219 | + | traits: &mut [], |
|
| 1220 | + | traitBase: 0, |
|
| 1221 | + | traitIndices: &mut [], |
|
| 1201 | 1222 | tableStarts: &mut [], |
|
| 1202 | 1223 | sharedLayouts: &mut [], |
|
| 1203 | 1224 | typeLayouts: &mut [], |
|
| 1204 | 1225 | nominalLayouts: &mut [], |
|
| 1205 | 1226 | fillQueue: &mut [], |
| 1307 | 1328 | return il::Program { |
|
| 1308 | 1329 | data, |
|
| 1309 | 1330 | fns: &fns.fns[..], |
|
| 1310 | 1331 | layouts: &tables.layouts[..], |
|
| 1311 | 1332 | signatures: &tables.signatures[..], |
|
| 1333 | + | traits: &tables.traits[..], |
|
| 1312 | 1334 | }; |
|
| 1313 | 1335 | } |
|
| 1314 | 1336 | ||
| 1315 | 1337 | ///////////////////////////////// |
|
| 1316 | 1338 | // Qualified Name Construction // |
| 1377 | 1399 | set self.currentMod = nil; |
|
| 1378 | 1400 | set self.packageDataStart = self.data.len; |
|
| 1379 | 1401 | if self.options.tableScope == TableScope::Package { |
|
| 1380 | 1402 | try self.tables.tableStarts.append( |
|
| 1381 | 1403 | TableStart { |
|
| 1382 | - | package, layouts: self.tables.layouts.len, signatures: self.tables.signatures.len |
|
| 1404 | + | package, |
|
| 1405 | + | layouts: self.tables.layouts.len, |
|
| 1406 | + | signatures: self.tables.signatures.len, |
|
| 1407 | + | traits: self.tables.traits.len, |
|
| 1383 | 1408 | }, |
|
| 1384 | 1409 | &mut self.resolver.arena |
|
| 1385 | 1410 | ) catch { |
|
| 1386 | 1411 | throw LowerError::AllocationFailed; |
|
| 1387 | 1412 | }; |
|
| 1388 | 1413 | set self.tables.layoutBase = self.tables.layouts.len; |
|
| 1389 | 1414 | set self.tables.signatureBase = self.tables.signatures.len; |
|
| 1415 | + | set self.tables.traitBase = self.tables.traits.len; |
|
| 1416 | + | set self.tables.traitIndices = &mut []; |
|
| 1390 | 1417 | set self.tables.sharedLayouts = &mut []; |
|
| 1391 | 1418 | set self.tables.typeLayouts = &mut []; |
|
| 1392 | 1419 | set self.tables.nominalLayouts = &mut []; |
|
| 1393 | 1420 | } |
|
| 1394 | 1421 | } |
|
| 1395 | 1422 | ||
| 1396 | - | /// Get the layouts and signatures of one package of a program that was lowered with the |
|
| 1423 | + | /// Get the layouts, signatures and traits of one package of a program that was lowered with the |
|
| 1397 | 1424 | /// table scope [`TableScope::Package`]. `starts` are the table starts that the lowerer |
|
| 1398 | 1425 | /// recorded. Indices in the functions and data of the package refer to the result. |
|
| 1399 | 1426 | export fn packageTables( |
|
| 1400 | 1427 | program: &il::Program, starts: &[TableStart], package: u16 |
|
| 1401 | 1428 | ) -> PackageTables { |
|
| 1402 | - | let mut tables = PackageTables { layouts: &[], signatures: &[] }; |
|
| 1429 | + | let mut tables = PackageTables { layouts: &[], signatures: &[], traits: &[] }; |
|
| 1403 | 1430 | for start, i in starts { |
|
| 1404 | 1431 | if start.package == package { |
|
| 1405 | 1432 | let mut layoutEnd = program.layouts.len; |
|
| 1406 | 1433 | let mut signatureEnd = program.signatures.len; |
|
| 1434 | + | let mut traitEnd = program.traits.len; |
|
| 1407 | 1435 | if i + 1 < starts.len { |
|
| 1408 | 1436 | set layoutEnd = starts[i + 1].layouts; |
|
| 1409 | 1437 | set signatureEnd = starts[i + 1].signatures; |
|
| 1438 | + | set traitEnd = starts[i + 1].traits; |
|
| 1410 | 1439 | } |
|
| 1411 | 1440 | set tables = PackageTables { |
|
| 1412 | 1441 | layouts: &program.layouts[start.layouts .. layoutEnd], |
|
| 1413 | 1442 | signatures: &program.signatures[start.signatures .. signatureEnd], |
|
| 1443 | + | traits: &program.traits[start.traits .. traitEnd], |
|
| 1414 | 1444 | }; |
|
| 1415 | 1445 | } |
|
| 1416 | 1446 | } |
|
| 1417 | 1447 | return tables; |
|
| 1418 | 1448 | } |
| 2008 | 2038 | throw LowerError::AllocationFailed; |
|
| 2009 | 2039 | }; |
|
| 2010 | 2040 | } |
|
| 2011 | 2041 | // An instance is usable wherever its trait and type are visible, so its v-table is |
|
| 2012 | 2042 | // available to other packages. |
|
| 2013 | - | let vtableLayout = resolver::Layout { |
|
| 2014 | - | size: methodCount * resolver::PTR_SIZE, |
|
| 2015 | - | alignment: resolver::PTR_SIZE, |
|
| 2016 | - | }; |
|
| 2043 | + | let size = methodCount * resolver::PTR_SIZE; |
|
| 2017 | 2044 | try addData( |
|
| 2018 | 2045 | self, |
|
| 2019 | 2046 | il::Data { |
|
| 2020 | 2047 | name: vName, |
|
| 2021 | - | size: vtableLayout.size, |
|
| 2022 | - | alignment: vtableLayout.alignment, |
|
| 2023 | - | layout: try plainLayoutOf(self, vtableLayout), |
|
| 2048 | + | size, |
|
| 2049 | + | alignment: resolver::PTR_SIZE, |
|
| 2050 | + | layout: try vtableLayout(self, traitInfo, typeSym), |
|
| 2024 | 2051 | readOnly: true, |
|
| 2025 | 2052 | isZeroInit: false, |
|
| 2026 | 2053 | values: &values[..], |
|
| 2027 | 2054 | }, |
|
| 2028 | 2055 | Linkage::Exported |
|
| 2029 | 2056 | ); |
|
| 2030 | 2057 | } |
|
| 2031 | 2058 | ||
| 2059 | + | /// Get the layout of the v-table of the instance of `traitInfo` for the type of |
|
| 2060 | + | /// `typeSym`: one function slot per method, of the type of the method of the instance. |
|
| 2061 | + | fn vtableLayout( |
|
| 2062 | + | self: &mut Lowerer, traitInfo: resolver::TraitId, typeSym: resolver::SymbolId |
|
| 2063 | + | ) -> u32 throws (LowerError) { |
|
| 2064 | + | let case resolver::SymbolData::Type(nominal) = self.resolver.symbols[*typeSym].data |
|
| 2065 | + | else throw LowerError::MissingMetadata; |
|
| 2066 | + | let concrete = resolver::Type::Nominal(nominal); |
|
| 2067 | + | let mut found: ?resolver::InstanceEntry = nil; |
|
| 2068 | + | for entry in &self.resolver.instances[..] { |
|
| 2069 | + | if entry.traitType == traitInfo and resolver::typesEqual(entry.concreteType, concrete) { |
|
| 2070 | + | set found = entry; |
|
| 2071 | + | } |
|
| 2072 | + | } |
|
| 2073 | + | let inst = found else throw LowerError::MissingMetadata; |
|
| 2074 | + | let methodCount = self.resolver.traits[*traitInfo].methods.len; |
|
| 2075 | + | let mut slots: *mut [il::Slot] = &mut []; |
|
| 2076 | + | try slots.reserve(methodCount, &mut self.resolver.arena) catch { |
|
| 2077 | + | throw LowerError::AllocationFailed; |
|
| 2078 | + | }; |
|
| 2079 | + | for i in 0 .. methodCount { |
|
| 2080 | + | let node = self.resolver.symbols[*inst.methods[i]].node; |
|
| 2081 | + | let case resolver::Type::Fn(fnType) = resolver::nodeData(&self.resolver, node).ty |
|
| 2082 | + | else throw LowerError::ExpectedFunction; |
|
| 2083 | + | let sig = try signatureIndex(self, fnType); |
|
| 2084 | + | try slots.append( |
|
| 2085 | + | il::Slot { offset: i * resolver::PTR_SIZE, kind: il::SlotKind::Fn(sig) }, |
|
| 2086 | + | &mut self.resolver.arena |
|
| 2087 | + | ) catch { |
|
| 2088 | + | throw LowerError::AllocationFailed; |
|
| 2089 | + | }; |
|
| 2090 | + | } |
|
| 2091 | + | try fillLayouts(self); |
|
| 2092 | + | return try internLayout( |
|
| 2093 | + | self, methodCount * resolver::PTR_SIZE, resolver::PTR_SIZE, &slots[..] |
|
| 2094 | + | ); |
|
| 2095 | + | } |
|
| 2096 | + | ||
| 2097 | + | /// Get the index of trait `id` in the trait table of the package. A method of the |
|
| 2098 | + | /// trait has the signature of its type, which excludes the receiver. The receiver |
|
| 2099 | + | /// follows the hidden result parameter, if the method has one. |
|
| 2100 | + | fn traitIndex(self: &mut Lowerer, id: resolver::TraitId) -> u32 throws (LowerError) { |
|
| 2101 | + | for entry in &self.tables.traitIndices[..] { |
|
| 2102 | + | if entry.id == id { |
|
| 2103 | + | return entry.index; |
|
| 2104 | + | } |
|
| 2105 | + | } |
|
| 2106 | + | // Number the trait before its methods, whose types can refer to it. |
|
| 2107 | + | let index = self.tables.traits.len - self.tables.traitBase; |
|
| 2108 | + | try self.tables.traits.append(il::Trait { methods: &[] }, &mut self.resolver.arena) catch { |
|
| 2109 | + | throw LowerError::AllocationFailed; |
|
| 2110 | + | }; |
|
| 2111 | + | try self.tables.traitIndices.append(TraitIndex { id, index }, &mut self.resolver.arena) catch { |
|
| 2112 | + | throw LowerError::AllocationFailed; |
|
| 2113 | + | }; |
|
| 2114 | + | let mut methods: *mut [il::Method] = &mut []; |
|
| 2115 | + | for method in &self.resolver.traits[*id].methods[..] { |
|
| 2116 | + | let receiver: u32 = 1 if requiresReturnParam(&self.resolver, method.fnType) else 0; |
|
| 2117 | + | let item = il::Method { |
|
| 2118 | + | sig: try signatureIndex(self, method.fnType), |
|
| 2119 | + | receiver, |
|
| 2120 | + | own: method.receiverClass == types::PointerClass::Owned, |
|
| 2121 | + | mutable: method.mutable, |
|
| 2122 | + | }; |
|
| 2123 | + | try methods.append(item, &mut self.resolver.arena) catch { |
|
| 2124 | + | throw LowerError::AllocationFailed; |
|
| 2125 | + | }; |
|
| 2126 | + | } |
|
| 2127 | + | set self.tables.traits[self.tables.traitBase + index].methods = &methods[..]; |
|
| 2128 | + | return index; |
|
| 2129 | + | } |
|
| 2130 | + | ||
| 2131 | + | /// Whether a trait object type is a trait object slot. An unsafe trait object is |
|
| 2132 | + | /// plain data, since its receiver is. |
|
| 2133 | + | fn isTraitSlot(typ: resolver::Type) -> bool { |
|
| 2134 | + | if let case resolver::Type::TraitObject { class, .. } = typ { |
|
| 2135 | + | return class <> types::PointerClass::Unsafe; |
|
| 2136 | + | } |
|
| 2137 | + | return false; |
|
| 2138 | + | } |
|
| 2139 | + | ||
| 2032 | 2140 | /// Lower a method node into an IL function with the given qualified name. |
|
| 2033 | 2141 | /// Shared by both instance methods and standalone methods. |
|
| 2034 | 2142 | fn lowerMethod( |
|
| 2035 | 2143 | self: &mut Lowerer, |
|
| 2036 | 2144 | node: *ast::Node, |
| 5877 | 5985 | /// IL value, so the lowerer must use the post-coercion type when |
|
| 5878 | 5986 | /// choosing how to compare or store that value. |
|
| 5879 | 5987 | fn effectiveType(self: &mut Lowerer, node: *ast::Node) -> resolver::Type throws (LowerError) { |
|
| 5880 | 5988 | let ty = try typeOf(self, node); |
|
| 5881 | 5989 | if let coerce = resolver::coercionFor(&self.resolver, node) { |
|
| 5882 | - | if let case resolver::Coercion::OptionalLift(optTy) = coerce { |
|
| 5883 | - | return optTy; |
|
| 5990 | + | match coerce { |
|
| 5991 | + | case resolver::Coercion::OptionalLift(optTy) => return optTy, |
|
| 5992 | + | case resolver::Coercion::TraitObject { traitInfo, class, mutable, .. } => |
|
| 5993 | + | return resolver::Type::TraitObject { class, traitInfo, mutable }, |
|
| 5994 | + | else => {} |
|
| 5884 | 5995 | } |
|
| 5885 | 5996 | } |
|
| 5886 | 5997 | return ty; |
|
| 5887 | 5998 | } |
|
| 5888 | 5999 |
| 6222 | 6333 | try emitStore(self, dst, SLICE_CAP_OFFSET, resolver::Type::U32, capVal); |
|
| 6223 | 6334 | ||
| 6224 | 6335 | return il::Val::Reg(dst); |
|
| 6225 | 6336 | } |
|
| 6226 | 6337 | ||
| 6227 | - | /// Build a trait object fat pointer from a data pointer and a v-table. |
|
| 6338 | + | /// Build a trait object of type `objType` from a data pointer and a v-table. |
|
| 6228 | 6339 | fn buildTraitObject( |
|
| 6229 | 6340 | self: &mut Lowerer, |
|
| 6230 | 6341 | dataVal: il::Val, |
|
| 6342 | + | objType: resolver::Type, |
|
| 6231 | 6343 | traitInfo: resolver::TraitId, |
|
| 6232 | 6344 | instId: resolver::InstanceId |
|
| 6233 | 6345 | ) -> il::Val throws (LowerError) { |
|
| 6234 | 6346 | let inst = self.resolver.instances[*instId]; |
|
| 6235 | 6347 | let traitName = self.resolver.traits[*traitInfo].name; |
|
| 6236 | 6348 | let vName = try vtableName(self, inst.moduleId, inst.concreteTypeName, traitName); |
|
| 6237 | - | ||
| 6349 | + | if isTraitSlot(objType) { |
|
| 6350 | + | let slot = try emitReserve(self, objType); |
|
| 6351 | + | let recv = try emitValToReg(self, dataVal); |
|
| 6352 | + | try emit(self, il::Instr::TraitNew { dst: slot, recv, table: il::Val::DataSym(vName) }); |
|
| 6353 | + | return il::Val::Reg(slot); |
|
| 6354 | + | } |
|
| 6238 | 6355 | // Reserve space for the trait object on the stack. |
|
| 6239 | 6356 | let slot = try emitReservePlain(self, resolver::Layout { |
|
| 6240 | 6357 | size: resolver::PTR_SIZE * 2, |
|
| 6241 | 6358 | alignment: resolver::PTR_SIZE, |
|
| 6242 | 6359 | }); |
| 9341 | 9458 | ||
| 9342 | 9459 | // Lower the value to append and the allocator. |
|
| 9343 | 9460 | let elemVal = try lowerCallArg(self, call.args[0], true, nil); |
|
| 9344 | 9461 | let allocVal = try lowerExpr(self, call.args[1]); |
|
| 9345 | 9462 | let allocReg = try emitValToReg(self, allocVal); |
|
| 9463 | + | let allocType = try effectiveType(self, call.args[1]); |
|
| 9346 | 9464 | ||
| 9347 | 9465 | let elemLayout = resolver::getTypeLayout(&self.resolver, *elemType); |
|
| 9348 | 9466 | let stride = elemLayout.size; |
|
| 9349 | 9467 | let alignment = elemLayout.alignment; |
|
| 9350 | 9468 |
| 9410 | 9528 | newCapVal, |
|
| 9411 | 9529 | il::Val::Imm( |
|
| 9412 | 9530 | stride as i64 |
|
| 9413 | 9531 | ) |
|
| 9414 | 9532 | ); |
|
| 9415 | - | try emitSliceAlloc(self, allocResult, allocReg, byteSize, alignment); |
|
| 9533 | + | try emitSliceAlloc(self, allocResult, allocReg, allocType, byteSize, alignment); |
|
| 9416 | 9534 | try emitAllocForward(self, allocResult, result, info, doneBlock, "append.copy"); |
|
| 9417 | 9535 | try emitSliceGrowCopy(self, allocResult, sliceReg, lenVal, elemType, newCapVal, "append"); |
|
| 9418 | 9536 | } |
|
| 9419 | 9537 | try emitJmp(self, storeBlock); |
|
| 9420 | 9538 | try switchToAndSeal(self, storeBlock); |
| 9476 | 9594 | let sliceVal = try lowerExpr(self, access.parent); |
|
| 9477 | 9595 | let sliceReg = try emitValToReg(self, sliceVal); |
|
| 9478 | 9596 | let countVal = try lowerExpr(self, call.args[0]); |
|
| 9479 | 9597 | let allocVal = try lowerExpr(self, call.args[1]); |
|
| 9480 | 9598 | let allocReg = try emitValToReg(self, allocVal); |
|
| 9599 | + | let allocType = try effectiveType(self, call.args[1]); |
|
| 9481 | 9600 | ||
| 9482 | 9601 | let elemLayout = resolver::getTypeLayout(&self.resolver, *elemType); |
|
| 9483 | 9602 | let stride = elemLayout.size; |
|
| 9484 | 9603 | let alignment = elemLayout.alignment; |
|
| 9485 | 9604 |
| 9518 | 9637 | newLenVal, |
|
| 9519 | 9638 | il::Val::Imm( |
|
| 9520 | 9639 | stride as i64 |
|
| 9521 | 9640 | ) |
|
| 9522 | 9641 | ); |
|
| 9523 | - | try emitSliceAlloc(self, result, allocReg, byteSize, alignment); |
|
| 9642 | + | try emitSliceAlloc(self, result, allocReg, allocType, byteSize, alignment); |
|
| 9524 | 9643 | ||
| 9525 | 9644 | try emitAllocBranch(self, result, doneBlock, "reserve.copy"); |
|
| 9526 | 9645 | try emitSliceGrowCopy(self, result, sliceReg, lenVal, elemType, newLenVal, "reserve"); |
|
| 9527 | 9646 | } |
|
| 9528 | 9647 | try emitJmp(self, successBlock); |
| 9535 | 9654 | ||
| 9536 | 9655 | try switchToAndSeal(self, doneBlock); |
|
| 9537 | 9656 | return il::Val::Reg(result); |
|
| 9538 | 9657 | } |
|
| 9539 | 9658 | ||
| 9540 | - | /// Call the allocator in `allocReg` for `byteSize` bytes and store its result in `result`. |
|
| 9659 | + | /// Call the allocator in `allocReg`, of type `allocType`, for `byteSize` bytes and store |
|
| 9660 | + | /// its result in `result`. |
|
| 9541 | 9661 | /// |
|
| 9542 | 9662 | /// A trait object holds the data pointer and the v-table. The resolver accepts |
|
| 9543 | 9663 | /// only traits with one method, so the function is the first v-table slot. The |
|
| 9544 | 9664 | /// call is `func(result, data, byteSize, alignment)`, and the result holds the |
|
| 9545 | 9665 | /// error tag at offset 0 and the new pointer at `RESULT_VAL_OFFSET`. |
|
| 9546 | 9666 | fn emitSliceAlloc( |
|
| 9547 | 9667 | self: &mut Lowerer, |
|
| 9548 | 9668 | result: il::Reg, |
|
| 9549 | 9669 | allocReg: il::Reg, |
|
| 9670 | + | allocType: resolver::Type, |
|
| 9550 | 9671 | byteSize: il::Val, |
|
| 9551 | 9672 | alignment: u32 |
|
| 9552 | 9673 | ) throws (LowerError) { |
|
| 9674 | + | if isTraitSlot(allocType) { |
|
| 9675 | + | let args = try allocVals(self, 4); |
|
| 9676 | + | set self.argPool[args.start] = il::Val::Reg(result); |
|
| 9677 | + | set self.argPool[args.start + 1] = il::Val::Undef; |
|
| 9678 | + | set self.argPool[args.start + 2] = byteSize; |
|
| 9679 | + | set self.argPool[args.start + 3] = il::Val::Imm(alignment as i64); |
|
| 9680 | + | try emit(self, il::Instr::TraitCall { |
|
| 9681 | + | retTy: il::ValType::Void, dst: nil, obj: allocReg, method: 0, receiver: 1, args |
|
| 9682 | + | }); |
|
| 9683 | + | return; |
|
| 9684 | + | } |
|
| 9553 | 9685 | let funcReg = nextReg(self); |
|
| 9554 | 9686 | let dataReg = nextReg(self); |
|
| 9555 | 9687 | try emitLoadW64At(self, dataReg, allocReg, TRAIT_OBJ_DATA_OFFSET); |
|
| 9556 | 9688 | let vtableReg = nextReg(self); |
|
| 9557 | 9689 | try emitLoadW64At(self, vtableReg, allocReg, TRAIT_OBJ_VTABLE_OFFSET); |
| 9703 | 9835 | let sliceReg = try emitValToReg(self, sliceVal); |
|
| 9704 | 9836 | let newLenVal = try lowerExpr(self, call.args[0]); |
|
| 9705 | 9837 | let valueVal = try lowerCallArg(self, call.args[1], true, nil); |
|
| 9706 | 9838 | let allocVal = try lowerExpr(self, call.args[2]); |
|
| 9707 | 9839 | let allocReg = try emitValToReg(self, allocVal); |
|
| 9840 | + | let allocType = try effectiveType(self, call.args[2]); |
|
| 9708 | 9841 | ||
| 9709 | 9842 | let elemLayout = resolver::getTypeLayout(&self.resolver, *elemType); |
|
| 9710 | 9843 | let stride = elemLayout.size; |
|
| 9711 | 9844 | let alignment = elemLayout.alignment; |
|
| 9712 | 9845 |
| 9745 | 9878 | try emitStoreW32At(self, newLenVal, sliceReg, SLICE_CAP_OFFSET); |
|
| 9746 | 9879 | } else { |
|
| 9747 | 9880 | let byteSize = try emitTypedBinOp( |
|
| 9748 | 9881 | self, il::BinOp::Mul, il::Type::W32, newLenVal, il::Val::Imm(stride as i64) |
|
| 9749 | 9882 | ); |
|
| 9750 | - | try emitSliceAlloc(self, result, allocReg, byteSize, alignment); |
|
| 9883 | + | try emitSliceAlloc(self, result, allocReg, allocType, byteSize, alignment); |
|
| 9751 | 9884 | ||
| 9752 | 9885 | try emitAllocBranch(self, result, doneBlock, "resize.copy"); |
|
| 9753 | 9886 | try emitSliceGrowCopy( |
|
| 9754 | 9887 | self, |
|
| 9755 | 9888 | result, |
| 9827 | 9960 | let sliceReg = try emitValToReg(self, sliceVal); |
|
| 9828 | 9961 | let srcVal = try lowerExpr(self, call.args[0]); |
|
| 9829 | 9962 | let srcReg = try emitValToReg(self, srcVal); |
|
| 9830 | 9963 | let allocVal = try lowerExpr(self, call.args[1]); |
|
| 9831 | 9964 | let allocReg = try emitValToReg(self, allocVal); |
|
| 9965 | + | let allocType = try effectiveType(self, call.args[1]); |
|
| 9832 | 9966 | ||
| 9833 | 9967 | let elemLayout = resolver::getTypeLayout(&self.resolver, *elemType); |
|
| 9834 | 9968 | let stride = elemLayout.size; |
|
| 9835 | 9969 | let alignment = elemLayout.alignment; |
|
| 9836 | 9970 |
| 9901 | 10035 | il::BinOp::Mul, |
|
| 9902 | 10036 | il::Type::W32, |
|
| 9903 | 10037 | il::Val::Reg(newCapReg), |
|
| 9904 | 10038 | il::Val::Imm(stride as i64) |
|
| 9905 | 10039 | ); |
|
| 9906 | - | try emitSliceAlloc(self, result, allocReg, byteSize, alignment); |
|
| 10040 | + | try emitSliceAlloc(self, result, allocReg, allocType, byteSize, alignment); |
|
| 9907 | 10041 | ||
| 9908 | 10042 | try emitAllocBranch(self, result, doneBlock, "extend.copy"); |
|
| 9909 | 10043 | try emitSliceGrowCopy( |
|
| 9910 | 10044 | self, |
|
| 9911 | 10045 | result, |
| 10101 | 10235 | else throw LowerError::MissingMetadata; |
|
| 10102 | 10236 | ||
| 10103 | 10237 | // Lower the trait object expression. |
|
| 10104 | 10238 | let traitObjVal = try lowerExpr(self, access.parent); |
|
| 10105 | 10239 | let traitObjReg = try emitValToReg(self, traitObjVal); |
|
| 10240 | + | let methodFnType = self.resolver.traits[*traitInfo].methods[methodIndex].fnType; |
|
| 10241 | + | let argOffset: u32 = 1 if requiresReturnParam(&self.resolver, methodFnType) else 0; |
|
| 10106 | 10242 | ||
| 10243 | + | if isTraitSlot(resolver::autoDeref(try typeOf(self, access.parent))) { |
|
| 10244 | + | // The call passes the receiver of the object in its place among the arguments. |
|
| 10245 | + | let args = try allocVals(self, call.args.len + 1 + argOffset); |
|
| 10246 | + | set self.argPool[args.start + argOffset] = il::Val::Undef; |
|
| 10247 | + | for arg, i in call.args { |
|
| 10248 | + | let val = try lowerCallArg( |
|
| 10249 | + | self, arg, i + 1 < call.args.len, *methodFnType.paramTypes[i] |
|
| 10250 | + | ); |
|
| 10251 | + | set self.argPool[args.start + i + 1 + argOffset] = val; |
|
| 10252 | + | } |
|
| 10253 | + | let target = CallTarget::Trait { |
|
| 10254 | + | obj: traitObjReg, method: methodIndex, receiver: argOffset |
|
| 10255 | + | }; |
|
| 10256 | + | return try emitCallValue(self, target, methodFnType, args); |
|
| 10257 | + | } |
|
| 10107 | 10258 | // Load data pointer from trait object. |
|
| 10108 | 10259 | let dataReg = nextReg(self); |
|
| 10109 | 10260 | try emit(self, il::Instr::Load { |
|
| 10110 | 10261 | typ: il::Type::W64, |
|
| 10111 | 10262 | dst: dataReg, |
| 10130 | 10281 | typ: il::Type::W64, |
|
| 10131 | 10282 | dst: fnPtrReg, |
|
| 10132 | 10283 | src: vtableReg, |
|
| 10133 | 10284 | offset: slotOffset, |
|
| 10134 | 10285 | }); |
|
| 10135 | - | let methodFnType = self.resolver.traits[*traitInfo].methods[methodIndex].fnType; |
|
| 10136 | 10286 | ||
| 10137 | 10287 | // Build args: optional return param slot + data pointer (receiver) + user args. |
|
| 10138 | - | let argOffset: u32 = 1 if requiresReturnParam(&self.resolver, methodFnType) else 0; |
|
| 10139 | 10288 | let args = try allocVals(self, call.args.len + 1 + argOffset); |
|
| 10140 | 10289 | set self.argPool[args.start + argOffset] = il::Val::Reg(dataReg); |
|
| 10141 | 10290 | ||
| 10142 | 10291 | for arg, i in call.args { |
|
| 10143 | 10292 | let val = try lowerCallArg(self, arg, i + 1 < call.args.len, nil); |
|
| 10144 | 10293 | set self.argPool[args.start + i + 1 + argOffset] = val; |
|
| 10145 | 10294 | } |
|
| 10146 | - | return try emitCallValue(self, il::Val::Reg(fnPtrReg), methodFnType, args); |
|
| 10295 | + | return try emitCallValue(self, CallTarget::Fn(il::Val::Reg(fnPtrReg)), methodFnType, args); |
|
| 10147 | 10296 | } |
|
| 10148 | 10297 | ||
| 10149 | 10298 | /// Lower a call argument, snapshotting aggregate place expressions before |
|
| 10150 | 10299 | /// evaluating later arguments. Aggregates are represented by addresses in the |
|
| 10151 | 10300 | /// IL, so retaining the original address would let a later argument mutation |
| 10159 | 10308 | if hasLater and ast::isPlaceExpr(arg) and isAggregateType(&self.resolver, argType) { |
|
| 10160 | 10309 | set val = try emitStackVal(self, argType, val); |
|
| 10161 | 10310 | } |
|
| 10162 | 10311 | if let want = paramTy { |
|
| 10163 | 10312 | set val = try borrowSlice(self, val, argType, want); |
|
| 10313 | + | set val = try narrowObject(self, val, argType, want); |
|
| 10164 | 10314 | } |
|
| 10165 | 10315 | return val; |
|
| 10166 | 10316 | } |
|
| 10167 | 10317 | ||
| 10318 | + | /// Copy a trait object to the object that a parameter with fewer rights takes. A pointer |
|
| 10319 | + | /// to an object views only its own layout, and the rights are part of the layout. Any |
|
| 10320 | + | /// other argument is unchanged. |
|
| 10321 | + | fn narrowObject( |
|
| 10322 | + | self: &mut Lowerer, val: il::Val, from: resolver::Type, to: resolver::Type |
|
| 10323 | + | ) -> il::Val throws (LowerError) { |
|
| 10324 | + | if not isTraitSlot(from) or not isTraitSlot(to) or resolver::typesEqual(from, to) { |
|
| 10325 | + | return val; |
|
| 10326 | + | } |
|
| 10327 | + | let src = try emitValToReg(self, val); |
|
| 10328 | + | let dst = try emitReserve(self, to); |
|
| 10329 | + | ||
| 10330 | + | try emit(self, il::Instr::TraitCopy { dst, src }); |
|
| 10331 | + | return il::Val::Reg(dst); |
|
| 10332 | + | } |
|
| 10333 | + | ||
| 10168 | 10334 | /// Convert an owned slice to the borrowed slice that a parameter takes. The slice of |
|
| 10169 | 10335 | /// the parameter lives in a new object, since a pointer to an object views only its own |
|
| 10170 | 10336 | /// layout. Any other argument is unchanged. |
|
| 10171 | 10337 | fn borrowSlice( |
|
| 10172 | 10338 | self: &mut Lowerer, val: il::Val, from: resolver::Type, to: resolver::Type |
| 10184 | 10350 | ||
| 10185 | 10351 | try emit(self, il::Instr::SliceSub { dst, src, layout, start: il::Val::Imm(0), end: len }); |
|
| 10186 | 10352 | return il::Val::Reg(dst); |
|
| 10187 | 10353 | } |
|
| 10188 | 10354 | ||
| 10355 | + | /// What a call calls. |
|
| 10356 | + | union CallTarget: Copy { |
|
| 10357 | + | /// A function value. |
|
| 10358 | + | Fn(il::Val), |
|
| 10359 | + | /// A method of the trait object at `obj`. The receiver goes to position `receiver` |
|
| 10360 | + | /// of the arguments. |
|
| 10361 | + | Trait { obj: il::Reg, method: u32, receiver: u32 }, |
|
| 10362 | + | } |
|
| 10363 | + | ||
| 10364 | + | /// Emit the call instruction for `target`. |
|
| 10365 | + | fn emitCallTo( |
|
| 10366 | + | self: &mut Lowerer, target: CallTarget, retTy: il::ValType, dst: ?il::Reg, args: il::ArgRange |
|
| 10367 | + | ) throws (LowerError) { |
|
| 10368 | + | match target { |
|
| 10369 | + | case CallTarget::Fn(func) => try emit(self, il::Instr::Call { retTy, dst, func, args }), |
|
| 10370 | + | case CallTarget::Trait { obj, method, receiver } => |
|
| 10371 | + | try emit(self, il::Instr::TraitCall { retTy, dst, obj, method, receiver, args }), |
|
| 10372 | + | } |
|
| 10373 | + | } |
|
| 10374 | + | ||
| 10189 | 10375 | /// Emit a function call with return-parameter and small-aggregate handling. |
|
| 10190 | 10376 | /// |
|
| 10191 | 10377 | /// All call lowering paths (regular, trait method, standalone method) converge |
|
| 10192 | 10378 | /// here after preparing the callee value, function type, and argument array. |
|
| 10193 | 10379 | /// The `args` slice must already include a slot at index zero for the hidden |
|
| 10194 | 10380 | /// return parameter; that slot is filled by this function. |
|
| 10195 | 10381 | fn emitCallValue( |
|
| 10196 | 10382 | self: &mut Lowerer, |
|
| 10197 | - | callee: il::Val, |
|
| 10383 | + | target: CallTarget, |
|
| 10198 | 10384 | fnInfo: *resolver::FnType, |
|
| 10199 | 10385 | args: il::ArgRange |
|
| 10200 | 10386 | ) -> il::Val throws (LowerError) { |
|
| 10201 | 10387 | let retTy = *fnInfo.returnType; |
|
| 10202 | 10388 |
| 10213 | 10399 | let slot = try emitReserve(self, retTy); |
|
| 10214 | 10400 | set self.argPool[args.start] = il::Val::Reg(slot); |
|
| 10215 | 10401 | } |
|
| 10216 | 10402 | let slotVal = self.argPool[args.start]; |
|
| 10217 | 10403 | ||
| 10218 | - | try emit(self, il::Instr::Call { |
|
| 10219 | - | retTy: try returnTypeOf(self, fnInfo), |
|
| 10220 | - | dst: nil, |
|
| 10221 | - | func: callee, |
|
| 10222 | - | args, |
|
| 10223 | - | }); |
|
| 10404 | + | try emitCallTo(self, target, try returnTypeOf(self, fnInfo), nil, args); |
|
| 10224 | 10405 | return slotVal; |
|
| 10225 | 10406 | } |
|
| 10226 | 10407 | let mut dst: ?il::Reg = nil; |
|
| 10227 | 10408 | if retTy <> resolver::Type::Void and retTy <> resolver::Type::Never { |
|
| 10228 | 10409 | set dst = nextReg(self); |
|
| 10229 | 10410 | } |
|
| 10230 | - | try emit(self, il::Instr::Call { |
|
| 10231 | - | retTy: try returnTypeOf(self, fnInfo), |
|
| 10232 | - | dst, |
|
| 10233 | - | func: callee, |
|
| 10234 | - | args, |
|
| 10235 | - | }); |
|
| 10411 | + | try emitCallTo(self, target, try returnTypeOf(self, fnInfo), dst, args); |
|
| 10236 | 10412 | ||
| 10237 | 10413 | if retTy == resolver::Type::Never { |
|
| 10238 | 10414 | try emit(self, il::Instr::Unreachable); |
|
| 10239 | 10415 | } |
|
| 10240 | 10416 | if let d = dst { |
| 10321 | 10497 | set self.argPool[args.start + argOffset] = receiverVal; |
|
| 10322 | 10498 | for arg, i in call.args { |
|
| 10323 | 10499 | let val = try lowerCallArg(self, arg, i + 1 < call.args.len, *fnInfo.paramTypes[i + 1]); |
|
| 10324 | 10500 | set self.argPool[args.start + i + 1 + argOffset] = val; |
|
| 10325 | 10501 | } |
|
| 10326 | - | return try emitCallValue(self, il::Val::FnAddr(qualName), fnInfo, args); |
|
| 10502 | + | return try emitCallValue(self, CallTarget::Fn(il::Val::FnAddr(qualName)), fnInfo, args); |
|
| 10327 | 10503 | } |
|
| 10328 | 10504 | ||
| 10329 | 10505 | /// Check if a call is to a compiler intrinsic and lower it directly. |
|
| 10330 | 10506 | fn lowerIntrinsicCall(self: &mut Lowerer, call: ast::Call) -> ?il::Val throws (LowerError) { |
|
| 10331 | 10507 | // Get the callee symbol and check if it's marked as an intrinsic. |
| 10474 | 10650 | let args = try allocVals(self, call.args.len + offset); |
|
| 10475 | 10651 | for arg, i in call.args { |
|
| 10476 | 10652 | let val = try lowerCallArg(self, arg, i + 1 < call.args.len, *fnInfo.paramTypes[i]); |
|
| 10477 | 10653 | set self.argPool[args.start + i + offset] = val; |
|
| 10478 | 10654 | } |
|
| 10479 | - | return try emitCallValue(self, callee, fnInfo, args); |
|
| 10655 | + | return try emitCallValue(self, CallTarget::Fn(callee), fnInfo, args); |
|
| 10480 | 10656 | } |
|
| 10481 | 10657 | ||
| 10482 | 10658 | /// Apply coercions requested by the resolver. |
|
| 10483 | 10659 | fn applyCoercion( |
|
| 10484 | 10660 | self: &mut Lowerer, |
| 10500 | 10676 | } |
|
| 10501 | 10677 | case resolver::Coercion::ResultWrap => { |
|
| 10502 | 10678 | let payloadType = *currentFnType(self).returnType; |
|
| 10503 | 10679 | return try buildResult(self, 0, val, payloadType); |
|
| 10504 | 10680 | } |
|
| 10505 | - | case resolver::Coercion::TraitObject { traitInfo, inst } => { |
|
| 10506 | - | return try buildTraitObject(self, val, traitInfo, inst); |
|
| 10681 | + | case resolver::Coercion::TraitObject { traitInfo, inst, class, mutable } => { |
|
| 10682 | + | let objType = resolver::Type::TraitObject { class, traitInfo, mutable }; |
|
| 10683 | + | return try buildTraitObject(self, val, objType, traitInfo, inst); |
|
| 10507 | 10684 | } |
|
| 10508 | 10685 | case resolver::Coercion::Identity => return val, |
|
| 10509 | 10686 | } |
|
| 10510 | 10687 | } |
|
| 10511 | 10688 |
| 10925 | 11102 | match typ { |
|
| 10926 | 11103 | case resolver::Type::Pointer { class, target, .. } => |
|
| 10927 | 11104 | return class <> types::PointerClass::Unsafe and *target <> resolver::Type::Opaque, |
|
| 10928 | 11105 | case resolver::Type::Slice { class, item, .. } => |
|
| 10929 | 11106 | return class <> types::PointerClass::Unsafe and *item <> resolver::Type::Opaque, |
|
| 10930 | - | case resolver::Type::Fn(_) => return true, |
|
| 11107 | + | case resolver::Type::Fn(_) => return true, |
|
| 11108 | + | case resolver::Type::TraitObject { .. } => return isTraitSlot(typ), |
|
| 10931 | 11109 | case resolver::Type::Optional(inner) => |
|
| 10932 | 11110 | return resolver::isNullableType(*inner) and isSlotType(*inner), |
|
| 10933 | 11111 | else => return false, |
|
| 10934 | 11112 | } |
|
| 10935 | 11113 | } |
| 11161 | 11339 | if mode == SlotMode::Nested { |
|
| 11162 | 11340 | return il::SlotKind::Fn(0); |
|
| 11163 | 11341 | } |
|
| 11164 | 11342 | return il::SlotKind::Fn(try signatureIndex(self, fnType)); |
|
| 11165 | 11343 | } |
|
| 11344 | + | case resolver::Type::TraitObject { class, traitInfo, mutable } => { |
|
| 11345 | + | let mut iface: u32 = 0; |
|
| 11346 | + | if mode == SlotMode::Full { |
|
| 11347 | + | set iface = try traitIndex(self, traitInfo); |
|
| 11348 | + | } |
|
| 11349 | + | return il::SlotKind::Trait(il::ObjectType { |
|
| 11350 | + | iface, own: class == types::PointerClass::Owned, mutable |
|
| 11351 | + | }); |
|
| 11352 | + | } |
|
| 11166 | 11353 | case resolver::Type::Optional(inner) => return try slotKind(self, *inner, mode), |
|
| 11167 | 11354 | else => return nil, |
|
| 11168 | 11355 | } |
|
| 11169 | 11356 | } |
|
| 11170 | 11357 |
| 11194 | 11381 | throw LowerError::AllocationFailed; |
|
| 11195 | 11382 | }; |
|
| 11196 | 11383 | } |
|
| 11197 | 11384 | ||
| 11198 | 11385 | /// Append the slot that a part of type `typ` at `offset` contributes to the layout that |
|
| 11199 | - | /// contains it. A slice is a nested object, so that a pointer to the part is a pointer to |
|
| 11200 | - | /// the slice, and a copy of the part is a copy of a slice. Other parts are as in [`nested`]. |
|
| 11386 | + | /// contains it. A slice or trait object is a nested object, so that a pointer to the part |
|
| 11387 | + | /// is a pointer to the slice or object, and a copy of the part is a copy of one. Other |
|
| 11388 | + | /// parts are as in [`nested`]. |
|
| 11201 | 11389 | fn part( |
|
| 11202 | 11390 | self: &mut Lowerer, |
|
| 11203 | 11391 | typ: resolver::Type, |
|
| 11204 | 11392 | offset: u32, |
|
| 11205 | 11393 | mode: SlotMode, |
|
| 11206 | 11394 | out: &mut *mut [il::Slot] |
|
| 11207 | 11395 | ) throws (LowerError) { |
|
| 11208 | 11396 | if let kind = try slotKind(self, typ, mode) { |
|
| 11209 | - | if let case il::SlotKind::Slice(_) = kind { |
|
| 11210 | - | try out.append( |
|
| 11211 | - | il::Slot { offset, kind: il::SlotKind::Sub(try layoutIndex(self, typ)) }, |
|
| 11212 | - | &mut self.resolver.arena |
|
| 11213 | - | ) catch { |
|
| 11214 | - | throw LowerError::AllocationFailed; |
|
| 11215 | - | }; |
|
| 11216 | - | return; |
|
| 11397 | + | match kind { |
|
| 11398 | + | case il::SlotKind::Slice(_), il::SlotKind::Trait(_) => { |
|
| 11399 | + | try out.append( |
|
| 11400 | + | il::Slot { offset, kind: il::SlotKind::Sub(try layoutIndex(self, typ)) }, |
|
| 11401 | + | &mut self.resolver.arena |
|
| 11402 | + | ) catch { |
|
| 11403 | + | throw LowerError::AllocationFailed; |
|
| 11404 | + | }; |
|
| 11405 | + | return; |
|
| 11406 | + | } |
|
| 11407 | + | else => {} |
|
| 11217 | 11408 | } |
|
| 11218 | 11409 | } |
|
| 11219 | 11410 | try nested(self, typ, offset, mode, out); |
|
| 11220 | 11411 | } |
|
| 11221 | 11412 |
lib/std/lang/resolver.rad
+7 -1
| 255 | 255 | TraitObject { |
|
| 256 | 256 | /// Trait type information. |
|
| 257 | 257 | traitInfo: TraitId, |
|
| 258 | 258 | /// Instance entry for v-table lookup. |
|
| 259 | 259 | inst: InstanceId, |
|
| 260 | + | /// Class of the trait object. |
|
| 261 | + | class: types::PointerClass, |
|
| 262 | + | /// Whether the trait object can write its receiver. |
|
| 263 | + | mutable: bool, |
|
| 260 | 264 | }, |
|
| 261 | 265 | } |
|
| 262 | 266 | ||
| 263 | 267 | /// Type layout. |
|
| 264 | 268 | export record Layout: Copy { |
| 2135 | 2139 | or (lhsMutable and not rhsMutable) |
|
| 2136 | 2140 | { |
|
| 2137 | 2141 | return nil; |
|
| 2138 | 2142 | } |
|
| 2139 | 2143 | if let inst = findInstance(self, lhsTraitInfo, *rhsTarget) { |
|
| 2140 | - | return Coercion::TraitObject { traitInfo: lhsTraitInfo, inst }; |
|
| 2144 | + | return Coercion::TraitObject { |
|
| 2145 | + | traitInfo: lhsTraitInfo, inst, class: lhsClass, mutable: lhsMutable |
|
| 2146 | + | }; |
|
| 2141 | 2147 | } |
|
| 2142 | 2148 | } |
|
| 2143 | 2149 | if let case Type::TraitObject { |
|
| 2144 | 2150 | class: rhsClass, traitInfo: rhsTraitInfo, mutable: rhsMutable |
|
| 2145 | 2151 | } = |
test/loader/hostile.rad
+1 -1
| 137 | 137 | name: "hostile", |
|
| 138 | 138 | dependencies, |
|
| 139 | 139 | exports: &[], |
|
| 140 | 140 | entry: ENTRY, |
|
| 141 | 141 | program: il::Program { |
|
| 142 | - | data: &tables[..], fns: &list[..], layouts: &table[..], signatures: &[] |
|
| 142 | + | data: &tables[..], fns: &list[..], layouts: &table[..], signatures: &[], traits: &[] |
|
| 143 | 143 | }, |
|
| 144 | 144 | }; |
|
| 145 | 145 | let mut output: *mut [u8] = &mut OUT[..]; |
|
| 146 | 146 | let length = try! program::encode(&mut output, &package); |
|
| 147 | 147 | let mut path: *mut [u8] = &mut []; |
test/tests/slice.append.ril
+53 -47
| 3 | 3 | slot 0 slice own mut $L0; |
|
| 4 | 4 | } |
|
| 5 | 5 | layout $L2 size 40 align 8 { |
|
| 6 | 6 | slot 8 sub $L1; |
|
| 7 | 7 | } |
|
| 8 | - | layout $L3 size 16 align 8 {} |
|
| 8 | + | layout $L3 size 16 align 8 { |
|
| 9 | + | slot 0 trait ref mut $T0; |
|
| 10 | + | } |
|
| 9 | 11 | layout $L4 size 24 align 8 {} |
|
| 12 | + | layout $L5 size 16 align 8 {} |
|
| 13 | + | ||
| 14 | + | signature $S0 (ptr ref mut $L4, w32, w32) -> void; |
|
| 15 | + | ||
| 16 | + | trait $T0 { |
|
| 17 | + | method $S0 at 1 ref mut; |
|
| 18 | + | } |
|
| 10 | 19 | ||
| 11 | 20 | fn void $append(ptr ref mut $L2 %0, ptr ref const $L1 %1, w32 %2, ptr ref const $L3 %3) { |
|
| 12 | 21 | @entry0 |
|
| 13 | 22 | reserve %4 $L1; |
|
| 14 | 23 | blit %4 %1 $L1; |
| 16 | 25 | reserve %6 $L4; |
|
| 17 | 26 | load w32 %7 %4 8; |
|
| 18 | 27 | load w32 %8 %4 12; |
|
| 19 | 28 | br.ult w32 %7 %8 @append.store1 @append.grow3; |
|
| 20 | 29 | @append.store1 |
|
| 21 | - | load w64 %28 %4 0; |
|
| 22 | - | mul w64 %29 %7 4; |
|
| 23 | - | add w64 %30 %28 %29; |
|
| 24 | - | store w32 %2 %30 0; |
|
| 25 | - | add w32 %31 %7 1; |
|
| 26 | - | store w32 %31 %4 8; |
|
| 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; |
|
| 27 | 36 | store w64 0 %5 0; |
|
| 28 | - | field %32 %5 8; |
|
| 29 | - | blit %32 %4 $L1; |
|
| 37 | + | field %29 %5 8; |
|
| 38 | + | blit %29 %4 $L1; |
|
| 30 | 39 | jmp @append.done2; |
|
| 31 | 40 | @append.done2 |
|
| 32 | - | load w64 %33 %5 0; |
|
| 33 | - | reserve %34 $L1; |
|
| 34 | - | br.ne w64 %33 0 @err15 @ok14; |
|
| 41 | + | load w64 %30 %5 0; |
|
| 42 | + | reserve %31 $L1; |
|
| 43 | + | br.ne w64 %30 0 @err15 @ok14; |
|
| 35 | 44 | @append.grow3 |
|
| 36 | 45 | br.ult w32 %8 1073741823 @guard#pass4 @guard#trap5; |
|
| 37 | 46 | @guard#pass4 |
|
| 38 | 47 | br.ult w32 %8 536870912 @append.double7 @append.exact8; |
|
| 39 | 48 | @guard#trap5 |
|
| 40 | 49 | ebreak; |
|
| 41 | 50 | unreachable; |
|
| 42 | 51 | @append.alloc6(w32 %9) |
|
| 43 | 52 | mul w32 %13 %9 4; |
|
| 44 | - | load w64 %15 %3 0; |
|
| 45 | - | load w64 %16 %3 8; |
|
| 46 | - | load w64 %14 %16 0; |
|
| 47 | - | call void %14(%6, %15, %13, 4); |
|
| 48 | - | load w64 %17 %6 0; |
|
| 49 | - | br.ne w64 %17 0 @append.copy9 @append.copy10; |
|
| 53 | + | trait.call void %3 0 at 1(%6, undefined, %13, 4); |
|
| 54 | + | load w64 %14 %6 0; |
|
| 55 | + | br.ne w64 %14 0 @append.copy9 @append.copy10; |
|
| 50 | 56 | @append.double7 |
|
| 51 | 57 | shl w32 %10 %8 1; |
|
| 52 | 58 | or w32 %11 %10 1; |
|
| 53 | 59 | jmp @append.alloc6(%11); |
|
| 54 | 60 | @append.exact8 |
|
| 55 | 61 | add w32 %12 %8 1; |
|
| 56 | 62 | jmp @append.alloc6(%12); |
|
| 57 | 63 | @append.copy9 |
|
| 58 | - | store w64 %17 %5 0; |
|
| 59 | - | field %18 %6 8; |
|
| 60 | - | field %19 %5 24; |
|
| 61 | - | blit %19 %18 $L3; |
|
| 64 | + | store w64 %14 %5 0; |
|
| 65 | + | field %15 %6 8; |
|
| 66 | + | field %16 %5 24; |
|
| 67 | + | blit %16 %15 $L5; |
|
| 62 | 68 | jmp @append.done2; |
|
| 63 | 69 | @append.copy10 |
|
| 64 | - | load w64 %20 %6 8; |
|
| 65 | - | load w64 %21 %4 0; |
|
| 66 | - | mul w64 %22 %7 4; |
|
| 70 | + | load w64 %17 %6 8; |
|
| 71 | + | load w64 %18 %4 0; |
|
| 72 | + | mul w64 %19 %7 4; |
|
| 67 | 73 | jmp @append11(0); |
|
| 68 | - | @append11(w64 %23) |
|
| 69 | - | br.ult w64 %23 %22 @append12 @append13; |
|
| 74 | + | @append11(w64 %20) |
|
| 75 | + | br.ult w64 %20 %19 @append12 @append13; |
|
| 70 | 76 | @append12 |
|
| 71 | - | add w64 %24 %21 %23; |
|
| 72 | - | load w32 %25 %24 0; |
|
| 73 | - | add w64 %26 %20 %23; |
|
| 74 | - | store w32 %25 %26 0; |
|
| 75 | - | add w64 %27 %23 4; |
|
| 76 | - | jmp @append11(%27); |
|
| 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); |
|
| 77 | 83 | @append13 |
|
| 78 | - | store w64 %20 %4 0; |
|
| 84 | + | store w64 %17 %4 0; |
|
| 79 | 85 | store w32 %9 %4 12; |
|
| 80 | 86 | jmp @append.store1; |
|
| 81 | 87 | @ok14 |
|
| 82 | - | field %35 %5 8; |
|
| 83 | - | blit %34 %35 $L1; |
|
| 88 | + | field %32 %5 8; |
|
| 89 | + | blit %31 %32 $L1; |
|
| 84 | 90 | jmp @merge16; |
|
| 85 | 91 | @err15 |
|
| 86 | - | reserve %36 $L2; |
|
| 87 | - | store w64 %33 %36 0; |
|
| 88 | - | field %37 %5 24; |
|
| 89 | - | field %38 %36 24; |
|
| 90 | - | blit %38 %37 $L3; |
|
| 91 | - | blit %0 %36 $L2; |
|
| 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; |
|
| 92 | 98 | ret; |
|
| 93 | 99 | @merge16 |
|
| 94 | - | reserve %39 $L2; |
|
| 95 | - | store w64 0 %39 0; |
|
| 96 | - | field %40 %39 8; |
|
| 97 | - | blit %40 %34 $L1; |
|
| 98 | - | blit %0 %39 $L2; |
|
| 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; |
|
| 99 | 105 | ret; |
|
| 100 | 106 | } |
test/tests/slice.append.trait.ril
+53 -47
| 3 | 3 | slot 0 slice own mut $L0; |
|
| 4 | 4 | } |
|
| 5 | 5 | layout $L2 size 40 align 8 { |
|
| 6 | 6 | slot 8 sub $L1; |
|
| 7 | 7 | } |
|
| 8 | - | layout $L3 size 16 align 8 {} |
|
| 8 | + | layout $L3 size 16 align 8 { |
|
| 9 | + | slot 0 trait ref mut $T0; |
|
| 10 | + | } |
|
| 9 | 11 | layout $L4 size 24 align 8 {} |
|
| 12 | + | layout $L5 size 16 align 8 {} |
|
| 13 | + | ||
| 14 | + | signature $S0 (ptr ref mut $L4, w32, w32) -> void; |
|
| 15 | + | ||
| 16 | + | trait $T0 { |
|
| 17 | + | method $S0 at 1 ref mut; |
|
| 18 | + | } |
|
| 10 | 19 | ||
| 11 | 20 | fn void $append(ptr ref mut $L2 %0, ptr ref const $L1 %1, w32 %2, ptr ref const $L3 %3) { |
|
| 12 | 21 | @entry0 |
|
| 13 | 22 | reserve %4 $L1; |
|
| 14 | 23 | blit %4 %1 $L1; |
| 16 | 25 | reserve %6 $L4; |
|
| 17 | 26 | load w32 %7 %4 8; |
|
| 18 | 27 | load w32 %8 %4 12; |
|
| 19 | 28 | br.ult w32 %7 %8 @append.store1 @append.grow3; |
|
| 20 | 29 | @append.store1 |
|
| 21 | - | load w64 %28 %4 0; |
|
| 22 | - | mul w64 %29 %7 4; |
|
| 23 | - | add w64 %30 %28 %29; |
|
| 24 | - | store w32 %2 %30 0; |
|
| 25 | - | add w32 %31 %7 1; |
|
| 26 | - | store w32 %31 %4 8; |
|
| 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; |
|
| 27 | 36 | store w64 0 %5 0; |
|
| 28 | - | field %32 %5 8; |
|
| 29 | - | blit %32 %4 $L1; |
|
| 37 | + | field %29 %5 8; |
|
| 38 | + | blit %29 %4 $L1; |
|
| 30 | 39 | jmp @append.done2; |
|
| 31 | 40 | @append.done2 |
|
| 32 | - | load w64 %33 %5 0; |
|
| 33 | - | reserve %34 $L1; |
|
| 34 | - | br.ne w64 %33 0 @err15 @ok14; |
|
| 41 | + | load w64 %30 %5 0; |
|
| 42 | + | reserve %31 $L1; |
|
| 43 | + | br.ne w64 %30 0 @err15 @ok14; |
|
| 35 | 44 | @append.grow3 |
|
| 36 | 45 | br.ult w32 %8 1073741823 @guard#pass4 @guard#trap5; |
|
| 37 | 46 | @guard#pass4 |
|
| 38 | 47 | br.ult w32 %8 536870912 @append.double7 @append.exact8; |
|
| 39 | 48 | @guard#trap5 |
|
| 40 | 49 | ebreak; |
|
| 41 | 50 | unreachable; |
|
| 42 | 51 | @append.alloc6(w32 %9) |
|
| 43 | 52 | mul w32 %13 %9 4; |
|
| 44 | - | load w64 %15 %3 0; |
|
| 45 | - | load w64 %16 %3 8; |
|
| 46 | - | load w64 %14 %16 0; |
|
| 47 | - | call void %14(%6, %15, %13, 4); |
|
| 48 | - | load w64 %17 %6 0; |
|
| 49 | - | br.ne w64 %17 0 @append.copy9 @append.copy10; |
|
| 53 | + | trait.call void %3 0 at 1(%6, undefined, %13, 4); |
|
| 54 | + | load w64 %14 %6 0; |
|
| 55 | + | br.ne w64 %14 0 @append.copy9 @append.copy10; |
|
| 50 | 56 | @append.double7 |
|
| 51 | 57 | shl w32 %10 %8 1; |
|
| 52 | 58 | or w32 %11 %10 1; |
|
| 53 | 59 | jmp @append.alloc6(%11); |
|
| 54 | 60 | @append.exact8 |
|
| 55 | 61 | add w32 %12 %8 1; |
|
| 56 | 62 | jmp @append.alloc6(%12); |
|
| 57 | 63 | @append.copy9 |
|
| 58 | - | store w64 %17 %5 0; |
|
| 59 | - | field %18 %6 8; |
|
| 60 | - | field %19 %5 24; |
|
| 61 | - | blit %19 %18 $L3; |
|
| 64 | + | store w64 %14 %5 0; |
|
| 65 | + | field %15 %6 8; |
|
| 66 | + | field %16 %5 24; |
|
| 67 | + | blit %16 %15 $L5; |
|
| 62 | 68 | jmp @append.done2; |
|
| 63 | 69 | @append.copy10 |
|
| 64 | - | load w64 %20 %6 8; |
|
| 65 | - | load w64 %21 %4 0; |
|
| 66 | - | mul w64 %22 %7 4; |
|
| 70 | + | load w64 %17 %6 8; |
|
| 71 | + | load w64 %18 %4 0; |
|
| 72 | + | mul w64 %19 %7 4; |
|
| 67 | 73 | jmp @append11(0); |
|
| 68 | - | @append11(w64 %23) |
|
| 69 | - | br.ult w64 %23 %22 @append12 @append13; |
|
| 74 | + | @append11(w64 %20) |
|
| 75 | + | br.ult w64 %20 %19 @append12 @append13; |
|
| 70 | 76 | @append12 |
|
| 71 | - | add w64 %24 %21 %23; |
|
| 72 | - | load w32 %25 %24 0; |
|
| 73 | - | add w64 %26 %20 %23; |
|
| 74 | - | store w32 %25 %26 0; |
|
| 75 | - | add w64 %27 %23 4; |
|
| 76 | - | jmp @append11(%27); |
|
| 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); |
|
| 77 | 83 | @append13 |
|
| 78 | - | store w64 %20 %4 0; |
|
| 84 | + | store w64 %17 %4 0; |
|
| 79 | 85 | store w32 %9 %4 12; |
|
| 80 | 86 | jmp @append.store1; |
|
| 81 | 87 | @ok14 |
|
| 82 | - | field %35 %5 8; |
|
| 83 | - | blit %34 %35 $L1; |
|
| 88 | + | field %32 %5 8; |
|
| 89 | + | blit %31 %32 $L1; |
|
| 84 | 90 | jmp @merge16; |
|
| 85 | 91 | @err15 |
|
| 86 | - | reserve %36 $L2; |
|
| 87 | - | store w64 %33 %36 0; |
|
| 88 | - | field %37 %5 24; |
|
| 89 | - | field %38 %36 24; |
|
| 90 | - | blit %38 %37 $L3; |
|
| 91 | - | blit %0 %36 $L2; |
|
| 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; |
|
| 92 | 98 | ret; |
|
| 93 | 99 | @merge16 |
|
| 94 | - | reserve %39 $L2; |
|
| 95 | - | store w64 0 %39 0; |
|
| 96 | - | field %40 %39 8; |
|
| 97 | - | blit %40 %34 $L1; |
|
| 98 | - | blit %0 %39 $L2; |
|
| 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; |
|
| 99 | 105 | ret; |
|
| 100 | 106 | } |
test/tests/slice.extend.bytes.ril
+91 -88
| 3 | 3 | } |
|
| 4 | 4 | layout $L1 size 1 align 1 {} |
|
| 5 | 5 | layout $L2 size 16 align 8 { |
|
| 6 | 6 | slot 0 slice own const $L1; |
|
| 7 | 7 | } |
|
| 8 | - | layout $L3 size 16 align 8 {} |
|
| 9 | - | layout $L4 size 16 align 8 { |
|
| 10 | - | slot 0 slice own mut $L5; |
|
| 8 | + | layout $L3 size 16 align 8 { |
|
| 9 | + | slot 0 trait ref mut $T0; |
|
| 11 | 10 | } |
|
| 12 | - | layout $L5 size 8 align 8 {} |
|
| 13 | - | layout $L6 size 16 align 8 { |
|
| 14 | - | slot 0 slice own const $L5; |
|
| 11 | + | layout $L4 size 16 align 8 {} |
|
| 12 | + | layout $L5 size 16 align 8 { |
|
| 13 | + | slot 0 slice own mut $L6; |
|
| 14 | + | } |
|
| 15 | + | layout $L6 size 8 align 8 {} |
|
| 16 | + | layout $L7 size 16 align 8 { |
|
| 17 | + | slot 0 slice own const $L6; |
|
| 18 | + | } |
|
| 19 | + | ||
| 20 | + | signature $S0 (ptr ref mut $L4, w32, w32) -> void; |
|
| 21 | + | ||
| 22 | + | trait $T0 { |
|
| 23 | + | method $S0 at 1 ref mut; |
|
| 15 | 24 | } |
|
| 16 | 25 | ||
| 17 | 26 | fn void $joinBytes(ptr ref const $L0 %0, ptr ref const $L2 %1, ptr ref const $L3 %2) { |
|
| 18 | 27 | @entry0 |
|
| 19 | 28 | reserve %3 $L0; |
|
| 20 | 29 | blit %3 %0 $L0; |
|
| 21 | - | reserve %4 $L3; |
|
| 30 | + | reserve %4 $L4; |
|
| 22 | 31 | load w32 %5 %3 8; |
|
| 23 | 32 | load w32 %6 %3 12; |
|
| 24 | 33 | load w32 %7 %1 8; |
|
| 25 | 34 | load w64 %8 %1 0; |
|
| 26 | 35 | add w64 %9 %5 %7; |
|
| 27 | 36 | br.ult w64 %6 %9 @extend.grow1 @extend.place2; |
|
| 28 | 37 | @extend.grow1 |
|
| 29 | 38 | br.ult w64 %9 2147483648 @guard#pass4 @guard#trap5; |
|
| 30 | 39 | @extend.place2 |
|
| 31 | - | load w64 %26 %3 0; |
|
| 32 | - | add w64 %27 %26 %5; |
|
| 33 | - | or w64 %28 %27 %8; |
|
| 34 | - | and w64 %29 %28 7; |
|
| 35 | - | sub w64 %30 %29 1; |
|
| 36 | - | and w64 %31 %30 -8; |
|
| 37 | - | and w64 %32 %7 %31; |
|
| 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; |
|
| 38 | 47 | jmp @extend14(0); |
|
| 39 | 48 | @extend.done3 |
|
| 40 | - | load w64 %43 %4 0; |
|
| 41 | - | br.ne w64 %43 0 @err21 @ok20; |
|
| 49 | + | load w64 %40 %4 0; |
|
| 50 | + | br.ne w64 %40 0 @err21 @ok20; |
|
| 42 | 51 | @guard#pass4 |
|
| 43 | 52 | br.ult w32 %6 1073741824 @extend.double7 @extend.exact8; |
|
| 44 | 53 | @guard#trap5 |
|
| 45 | 54 | ebreak; |
|
| 46 | 55 | unreachable; |
|
| 47 | 56 | @extend.alloc6(w32 %10) |
|
| 48 | 57 | mul w32 %13 %10 1; |
|
| 49 | - | load w64 %15 %2 0; |
|
| 50 | - | load w64 %16 %2 8; |
|
| 51 | - | load w64 %14 %16 0; |
|
| 52 | - | call void %14(%4, %15, %13, 1); |
|
| 53 | - | load w64 %17 %4 0; |
|
| 54 | - | br.ne w64 %17 0 @extend.done3 @extend.copy10; |
|
| 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; |
|
| 55 | 61 | @extend.double7 |
|
| 56 | 62 | shl w32 %11 %6 1; |
|
| 57 | 63 | or w32 %12 %11 1; |
|
| 58 | 64 | br.ult w32 %12 %9 @extend.exact8 @extend.twice9; |
|
| 59 | 65 | @extend.exact8 |
|
| 60 | 66 | jmp @extend.alloc6(%9); |
|
| 61 | 67 | @extend.twice9 |
|
| 62 | 68 | jmp @extend.alloc6(%12); |
|
| 63 | 69 | @extend.copy10 |
|
| 64 | - | load w32 %18 %3 8; |
|
| 65 | - | load w64 %19 %4 8; |
|
| 66 | - | load w64 %20 %3 0; |
|
| 70 | + | load w32 %15 %3 8; |
|
| 71 | + | load w64 %16 %4 8; |
|
| 72 | + | load w64 %17 %3 0; |
|
| 67 | 73 | jmp @extend11(0); |
|
| 68 | - | @extend11(w64 %21) |
|
| 69 | - | br.ult w64 %21 %18 @extend12 @extend13; |
|
| 74 | + | @extend11(w64 %18) |
|
| 75 | + | br.ult w64 %18 %15 @extend12 @extend13; |
|
| 70 | 76 | @extend12 |
|
| 71 | - | add w64 %22 %20 %21; |
|
| 72 | - | load w8 %23 %22 0; |
|
| 73 | - | add w64 %24 %19 %21; |
|
| 74 | - | store w8 %23 %24 0; |
|
| 75 | - | add w64 %25 %21 1; |
|
| 76 | - | jmp @extend11(%25); |
|
| 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); |
|
| 77 | 83 | @extend13 |
|
| 78 | - | store w64 %19 %3 0; |
|
| 84 | + | store w64 %16 %3 0; |
|
| 79 | 85 | store w32 %10 %3 12; |
|
| 80 | 86 | jmp @extend.place2; |
|
| 81 | - | @extend14(w64 %33) |
|
| 82 | - | br.ult w64 %33 %32 @extend15 @extend16; |
|
| 87 | + | @extend14(w64 %30) |
|
| 88 | + | br.ult w64 %30 %29 @extend15 @extend16; |
|
| 83 | 89 | @extend15 |
|
| 84 | - | add w64 %34 %8 %33; |
|
| 85 | - | load w64 %35 %34 0; |
|
| 86 | - | add w64 %36 %27 %33; |
|
| 87 | - | store w64 %35 %36 0; |
|
| 88 | - | add w64 %37 %33 8; |
|
| 89 | - | jmp @extend14(%37); |
|
| 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); |
|
| 90 | 96 | @extend16 |
|
| 91 | - | jmp @extend17(%33); |
|
| 92 | - | @extend17(w64 %38) |
|
| 93 | - | br.ult w64 %38 %7 @extend18 @extend19; |
|
| 97 | + | jmp @extend17(%30); |
|
| 98 | + | @extend17(w64 %35) |
|
| 99 | + | br.ult w64 %35 %7 @extend18 @extend19; |
|
| 94 | 100 | @extend18 |
|
| 95 | - | add w64 %39 %8 %38; |
|
| 96 | - | load w8 %40 %39 0; |
|
| 97 | - | add w64 %41 %27 %38; |
|
| 98 | - | store w8 %40 %41 0; |
|
| 99 | - | add w64 %42 %38 1; |
|
| 100 | - | jmp @extend17(%42); |
|
| 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); |
|
| 101 | 107 | @extend19 |
|
| 102 | 108 | store w32 %9 %3 8; |
|
| 103 | 109 | store w64 0 %4 0; |
|
| 104 | 110 | jmp @extend.done3; |
|
| 105 | 111 | @ok20 |
| 108 | 114 | unreachable; |
|
| 109 | 115 | @merge22 |
|
| 110 | 116 | ret; |
|
| 111 | 117 | } |
|
| 112 | 118 | ||
| 113 | - | fn void $joinWords(ptr ref const $L4 %0, ptr ref const $L6 %1, ptr ref const $L3 %2) { |
|
| 119 | + | fn void $joinWords(ptr ref const $L5 %0, ptr ref const $L7 %1, ptr ref const $L3 %2) { |
|
| 114 | 120 | @entry0 |
|
| 115 | - | reserve %3 $L4; |
|
| 116 | - | blit %3 %0 $L4; |
|
| 117 | - | reserve %4 $L3; |
|
| 121 | + | reserve %3 $L5; |
|
| 122 | + | blit %3 %0 $L5; |
|
| 123 | + | reserve %4 $L4; |
|
| 118 | 124 | load w32 %5 %3 8; |
|
| 119 | 125 | load w32 %6 %3 12; |
|
| 120 | 126 | load w32 %7 %1 8; |
|
| 121 | 127 | load w64 %8 %1 0; |
|
| 122 | 128 | add w64 %9 %5 %7; |
|
| 123 | 129 | br.ult w64 %6 %9 @extend.grow1 @extend.place2; |
|
| 124 | 130 | @extend.grow1 |
|
| 125 | 131 | br.ult w64 %9 536870912 @guard#pass4 @guard#trap5; |
|
| 126 | 132 | @extend.place2 |
|
| 127 | - | load w64 %27 %3 0; |
|
| 128 | - | mul w64 %28 %5 8; |
|
| 129 | - | add w64 %29 %27 %28; |
|
| 130 | - | mul w64 %30 %7 8; |
|
| 133 | + | load w64 %24 %3 0; |
|
| 134 | + | mul w64 %25 %5 8; |
|
| 135 | + | add w64 %26 %24 %25; |
|
| 136 | + | mul w64 %27 %7 8; |
|
| 131 | 137 | jmp @extend14(0); |
|
| 132 | 138 | @extend.done3 |
|
| 133 | - | load w64 %36 %4 0; |
|
| 134 | - | br.ne w64 %36 0 @err18 @ok17; |
|
| 139 | + | load w64 %33 %4 0; |
|
| 140 | + | br.ne w64 %33 0 @err18 @ok17; |
|
| 135 | 141 | @guard#pass4 |
|
| 136 | 142 | br.ult w32 %6 268435456 @extend.double7 @extend.exact8; |
|
| 137 | 143 | @guard#trap5 |
|
| 138 | 144 | ebreak; |
|
| 139 | 145 | unreachable; |
|
| 140 | 146 | @extend.alloc6(w32 %10) |
|
| 141 | 147 | mul w32 %13 %10 8; |
|
| 142 | - | load w64 %15 %2 0; |
|
| 143 | - | load w64 %16 %2 8; |
|
| 144 | - | load w64 %14 %16 0; |
|
| 145 | - | call void %14(%4, %15, %13, 8); |
|
| 146 | - | load w64 %17 %4 0; |
|
| 147 | - | br.ne w64 %17 0 @extend.done3 @extend.copy10; |
|
| 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; |
|
| 148 | 151 | @extend.double7 |
|
| 149 | 152 | shl w32 %11 %6 1; |
|
| 150 | 153 | or w32 %12 %11 1; |
|
| 151 | 154 | br.ult w32 %12 %9 @extend.exact8 @extend.twice9; |
|
| 152 | 155 | @extend.exact8 |
|
| 153 | 156 | jmp @extend.alloc6(%9); |
|
| 154 | 157 | @extend.twice9 |
|
| 155 | 158 | jmp @extend.alloc6(%12); |
|
| 156 | 159 | @extend.copy10 |
|
| 157 | - | load w32 %18 %3 8; |
|
| 158 | - | load w64 %19 %4 8; |
|
| 159 | - | load w64 %20 %3 0; |
|
| 160 | - | mul w64 %21 %18 8; |
|
| 160 | + | load w32 %15 %3 8; |
|
| 161 | + | load w64 %16 %4 8; |
|
| 162 | + | load w64 %17 %3 0; |
|
| 163 | + | mul w64 %18 %15 8; |
|
| 161 | 164 | jmp @extend11(0); |
|
| 162 | - | @extend11(w64 %22) |
|
| 163 | - | br.ult w64 %22 %21 @extend12 @extend13; |
|
| 165 | + | @extend11(w64 %19) |
|
| 166 | + | br.ult w64 %19 %18 @extend12 @extend13; |
|
| 164 | 167 | @extend12 |
|
| 165 | - | add w64 %23 %20 %22; |
|
| 166 | - | load w64 %24 %23 0; |
|
| 167 | - | add w64 %25 %19 %22; |
|
| 168 | - | store w64 %24 %25 0; |
|
| 169 | - | add w64 %26 %22 8; |
|
| 170 | - | jmp @extend11(%26); |
|
| 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); |
|
| 171 | 174 | @extend13 |
|
| 172 | - | store w64 %19 %3 0; |
|
| 175 | + | store w64 %16 %3 0; |
|
| 173 | 176 | store w32 %10 %3 12; |
|
| 174 | 177 | jmp @extend.place2; |
|
| 175 | - | @extend14(w64 %31) |
|
| 176 | - | br.ult w64 %31 %30 @extend15 @extend16; |
|
| 178 | + | @extend14(w64 %28) |
|
| 179 | + | br.ult w64 %28 %27 @extend15 @extend16; |
|
| 177 | 180 | @extend15 |
|
| 178 | - | add w64 %32 %8 %31; |
|
| 179 | - | load w64 %33 %32 0; |
|
| 180 | - | add w64 %34 %29 %31; |
|
| 181 | - | store w64 %33 %34 0; |
|
| 182 | - | add w64 %35 %31 8; |
|
| 183 | - | jmp @extend14(%35); |
|
| 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); |
|
| 184 | 187 | @extend16 |
|
| 185 | 188 | store w32 %9 %3 8; |
|
| 186 | 189 | store w64 0 %4 0; |
|
| 187 | 190 | jmp @extend.done3; |
|
| 188 | 191 | @ok17 |
test/tests/slice.reserve.result.ril
+28 -22
| 1 | 1 | layout $L0 size 16 align 8 { |
|
| 2 | 2 | slot 0 slice own mut $L1; |
|
| 3 | 3 | } |
|
| 4 | 4 | layout $L1 size 4 align 4 {} |
|
| 5 | - | layout $L2 size 16 align 8 {} |
|
| 5 | + | layout $L2 size 16 align 8 { |
|
| 6 | + | slot 0 trait ref mut $T0; |
|
| 7 | + | } |
|
| 8 | + | layout $L3 size 16 align 8 {} |
|
| 9 | + | ||
| 10 | + | signature $S0 (ptr ref mut $L3, w32, w32) -> void; |
|
| 11 | + | ||
| 12 | + | trait $T0 { |
|
| 13 | + | method $S0 at 1 ref mut; |
|
| 14 | + | } |
|
| 6 | 15 | ||
| 7 | 16 | fn void $reserveSmall(ptr ref const $L0 %0, ptr ref const $L2 %1) { |
|
| 8 | 17 | @entry0 |
|
| 9 | 18 | reserve %2 $L0; |
|
| 10 | 19 | blit %2 %0 $L0; |
|
| 11 | - | reserve %3 $L2; |
|
| 20 | + | reserve %3 $L3; |
|
| 12 | 21 | load w32 %4 %2 8; |
|
| 13 | 22 | load w32 %5 %2 12; |
|
| 14 | 23 | sub w32 %6 %5 %4; |
|
| 15 | 24 | br.ult w32 %6 4 @reserve.grow1 @reserve.success2; |
|
| 16 | 25 | @reserve.grow1 |
| 19 | 28 | br.ne w32 %8 0 @guard#pass4 @guard#trap5; |
|
| 20 | 29 | @reserve.success2 |
|
| 21 | 30 | store w64 0 %3 0; |
|
| 22 | 31 | jmp @reserve.done3; |
|
| 23 | 32 | @reserve.done3 |
|
| 24 | - | load w64 %23 %3 0; |
|
| 25 | - | br.ne w64 %23 0 @err11 @ok10; |
|
| 33 | + | load w64 %20 %3 0; |
|
| 34 | + | br.ne w64 %20 0 @err11 @ok10; |
|
| 26 | 35 | @guard#pass4 |
|
| 27 | 36 | add w32 %9 %4 4; |
|
| 28 | 37 | mul w32 %10 %9 4; |
|
| 29 | - | load w64 %12 %1 0; |
|
| 30 | - | load w64 %13 %1 8; |
|
| 31 | - | load w64 %11 %13 0; |
|
| 32 | - | call void %11(%3, %12, %10, 4); |
|
| 33 | - | load w64 %14 %3 0; |
|
| 34 | - | br.ne w64 %14 0 @reserve.done3 @reserve.copy6; |
|
| 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; |
|
| 35 | 41 | @guard#trap5 |
|
| 36 | 42 | ebreak; |
|
| 37 | 43 | unreachable; |
|
| 38 | 44 | @reserve.copy6 |
|
| 39 | - | load w64 %15 %3 8; |
|
| 40 | - | load w64 %16 %2 0; |
|
| 41 | - | mul w64 %17 %4 4; |
|
| 45 | + | load w64 %12 %3 8; |
|
| 46 | + | load w64 %13 %2 0; |
|
| 47 | + | mul w64 %14 %4 4; |
|
| 42 | 48 | jmp @reserve7(0); |
|
| 43 | - | @reserve7(w64 %18) |
|
| 44 | - | br.ult w64 %18 %17 @reserve8 @reserve9; |
|
| 49 | + | @reserve7(w64 %15) |
|
| 50 | + | br.ult w64 %15 %14 @reserve8 @reserve9; |
|
| 45 | 51 | @reserve8 |
|
| 46 | - | add w64 %19 %16 %18; |
|
| 47 | - | load w32 %20 %19 0; |
|
| 48 | - | add w64 %21 %15 %18; |
|
| 49 | - | store w32 %20 %21 0; |
|
| 50 | - | add w64 %22 %18 4; |
|
| 51 | - | jmp @reserve7(%22); |
|
| 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); |
|
| 52 | 58 | @reserve9 |
|
| 53 | - | store w64 %15 %2 0; |
|
| 59 | + | store w64 %12 %2 0; |
|
| 54 | 60 | store w32 %9 %2 12; |
|
| 55 | 61 | jmp @reserve.success2; |
|
| 56 | 62 | @ok10 |
|
| 57 | 63 | jmp @merge12; |
|
| 58 | 64 | @err11 |
test/tests/slice.resize.extend.ril
+84 -81
| 1 | 1 | layout $L0 size 16 align 8 { |
|
| 2 | 2 | slot 0 slice own mut $L1; |
|
| 3 | 3 | } |
|
| 4 | 4 | layout $L1 size 4 align 4 {} |
|
| 5 | - | layout $L2 size 16 align 8 {} |
|
| 6 | - | layout $L3 size 16 align 8 { |
|
| 5 | + | layout $L2 size 16 align 8 { |
|
| 6 | + | slot 0 trait ref mut $T0; |
|
| 7 | + | } |
|
| 8 | + | layout $L3 size 16 align 8 {} |
|
| 9 | + | layout $L4 size 16 align 8 { |
|
| 7 | 10 | slot 0 slice own const $L1; |
|
| 8 | 11 | } |
|
| 9 | 12 | ||
| 13 | + | signature $S0 (ptr ref mut $L3, w32, w32) -> void; |
|
| 14 | + | ||
| 15 | + | trait $T0 { |
|
| 16 | + | method $S0 at 1 ref mut; |
|
| 17 | + | } |
|
| 18 | + | ||
| 10 | 19 | fn void $fillSmall(ptr ref const $L0 %0, ptr ref const $L2 %1) { |
|
| 11 | 20 | @entry0 |
|
| 12 | 21 | reserve %2 $L0; |
|
| 13 | 22 | blit %2 %0 $L0; |
|
| 14 | - | reserve %3 $L2; |
|
| 23 | + | reserve %3 $L3; |
|
| 15 | 24 | load w32 %4 %2 8; |
|
| 16 | 25 | load w32 %5 %2 12; |
|
| 17 | 26 | br.ult w32 4 %4 @resize.shrink1 @resize.check2; |
|
| 18 | 27 | @resize.shrink1 |
|
| 19 | 28 | store w32 4 %2 8; |
| 22 | 31 | br.ult w32 %5 4 @resize.grow3 @resize.fill4; |
|
| 23 | 32 | @resize.grow3 |
|
| 24 | 33 | uge w32 %6 1073741823 4; |
|
| 25 | 34 | br.ne w32 %6 0 @guard#pass7 @guard#trap8; |
|
| 26 | 35 | @resize.fill4 |
|
| 27 | - | load w64 %21 %2 0; |
|
| 36 | + | load w64 %18 %2 0; |
|
| 28 | 37 | jmp @resize.fill13(%4); |
|
| 29 | 38 | @resize.success5 |
|
| 30 | 39 | store w64 0 %3 0; |
|
| 31 | 40 | jmp @resize.done6; |
|
| 32 | 41 | @resize.done6 |
|
| 33 | - | load w64 %26 %3 0; |
|
| 34 | - | br.ne w64 %26 0 @err17 @ok16; |
|
| 42 | + | load w64 %23 %3 0; |
|
| 43 | + | br.ne w64 %23 0 @err17 @ok16; |
|
| 35 | 44 | @guard#pass7 |
|
| 36 | 45 | mul w32 %7 4 4; |
|
| 37 | - | load w64 %9 %1 0; |
|
| 38 | - | load w64 %10 %1 8; |
|
| 39 | - | load w64 %8 %10 0; |
|
| 40 | - | call void %8(%3, %9, %7, 4); |
|
| 41 | - | load w64 %11 %3 0; |
|
| 42 | - | br.ne w64 %11 0 @resize.done6 @resize.copy9; |
|
| 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; |
|
| 43 | 49 | @guard#trap8 |
|
| 44 | 50 | ebreak; |
|
| 45 | 51 | unreachable; |
|
| 46 | 52 | @resize.copy9 |
|
| 47 | - | load w32 %12 %2 8; |
|
| 48 | - | load w64 %13 %3 8; |
|
| 49 | - | load w64 %14 %2 0; |
|
| 50 | - | mul w64 %15 %12 4; |
|
| 53 | + | load w32 %9 %2 8; |
|
| 54 | + | load w64 %10 %3 8; |
|
| 55 | + | load w64 %11 %2 0; |
|
| 56 | + | mul w64 %12 %9 4; |
|
| 51 | 57 | jmp @resize10(0); |
|
| 52 | - | @resize10(w64 %16) |
|
| 53 | - | br.ult w64 %16 %15 @resize11 @resize12; |
|
| 58 | + | @resize10(w64 %13) |
|
| 59 | + | br.ult w64 %13 %12 @resize11 @resize12; |
|
| 54 | 60 | @resize11 |
|
| 55 | - | add w64 %17 %14 %16; |
|
| 56 | - | load w32 %18 %17 0; |
|
| 57 | - | add w64 %19 %13 %16; |
|
| 58 | - | store w32 %18 %19 0; |
|
| 59 | - | add w64 %20 %16 4; |
|
| 60 | - | jmp @resize10(%20); |
|
| 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); |
|
| 61 | 67 | @resize12 |
|
| 62 | - | store w64 %13 %2 0; |
|
| 68 | + | store w64 %10 %2 0; |
|
| 63 | 69 | store w32 4 %2 12; |
|
| 64 | 70 | jmp @resize.fill4; |
|
| 65 | - | @resize.fill13(w32 %22) |
|
| 66 | - | br.ult w32 %22 4 @resize.fill14 @resize.fill15; |
|
| 71 | + | @resize.fill13(w32 %19) |
|
| 72 | + | br.ult w32 %19 4 @resize.fill14 @resize.fill15; |
|
| 67 | 73 | @resize.fill14 |
|
| 68 | - | mul w64 %23 %22 4; |
|
| 69 | - | add w64 %24 %21 %23; |
|
| 70 | - | store w32 7 %24 0; |
|
| 71 | - | add w32 %25 %22 1; |
|
| 72 | - | jmp @resize.fill13(%25); |
|
| 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); |
|
| 73 | 79 | @resize.fill15 |
|
| 74 | 80 | store w32 4 %2 8; |
|
| 75 | 81 | jmp @resize.success5; |
|
| 76 | 82 | @ok16 |
|
| 77 | 83 | jmp @merge18; |
| 79 | 85 | unreachable; |
|
| 80 | 86 | @merge18 |
|
| 81 | 87 | ret; |
|
| 82 | 88 | } |
|
| 83 | 89 | ||
| 84 | - | fn void $joinSmall(ptr ref const $L0 %0, ptr ref const $L3 %1, ptr ref const $L2 %2) { |
|
| 90 | + | fn void $joinSmall(ptr ref const $L0 %0, ptr ref const $L4 %1, ptr ref const $L2 %2) { |
|
| 85 | 91 | @entry0 |
|
| 86 | 92 | reserve %3 $L0; |
|
| 87 | 93 | blit %3 %0 $L0; |
|
| 88 | - | reserve %4 $L2; |
|
| 94 | + | reserve %4 $L3; |
|
| 89 | 95 | load w32 %5 %3 8; |
|
| 90 | 96 | load w32 %6 %3 12; |
|
| 91 | 97 | load w32 %7 %1 8; |
|
| 92 | 98 | load w64 %8 %1 0; |
|
| 93 | 99 | add w64 %9 %5 %7; |
|
| 94 | 100 | br.ult w64 %6 %9 @extend.grow1 @extend.place2; |
|
| 95 | 101 | @extend.grow1 |
|
| 96 | 102 | br.ult w64 %9 1073741824 @guard#pass4 @guard#trap5; |
|
| 97 | 103 | @extend.place2 |
|
| 98 | - | load w64 %27 %3 0; |
|
| 99 | - | mul w64 %28 %5 4; |
|
| 100 | - | add w64 %29 %27 %28; |
|
| 101 | - | mul w64 %30 %7 4; |
|
| 102 | - | or w64 %31 %29 %8; |
|
| 103 | - | and w64 %32 %31 7; |
|
| 104 | - | sub w64 %33 %32 1; |
|
| 105 | - | and w64 %34 %33 -8; |
|
| 106 | - | and w64 %35 %30 %34; |
|
| 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; |
|
| 107 | 113 | jmp @extend14(0); |
|
| 108 | 114 | @extend.done3 |
|
| 109 | - | load w64 %46 %4 0; |
|
| 110 | - | br.ne w64 %46 0 @err21 @ok20; |
|
| 115 | + | load w64 %43 %4 0; |
|
| 116 | + | br.ne w64 %43 0 @err21 @ok20; |
|
| 111 | 117 | @guard#pass4 |
|
| 112 | 118 | br.ult w32 %6 536870912 @extend.double7 @extend.exact8; |
|
| 113 | 119 | @guard#trap5 |
|
| 114 | 120 | ebreak; |
|
| 115 | 121 | unreachable; |
|
| 116 | 122 | @extend.alloc6(w32 %10) |
|
| 117 | 123 | mul w32 %13 %10 4; |
|
| 118 | - | load w64 %15 %2 0; |
|
| 119 | - | load w64 %16 %2 8; |
|
| 120 | - | load w64 %14 %16 0; |
|
| 121 | - | call void %14(%4, %15, %13, 4); |
|
| 122 | - | load w64 %17 %4 0; |
|
| 123 | - | br.ne w64 %17 0 @extend.done3 @extend.copy10; |
|
| 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; |
|
| 124 | 127 | @extend.double7 |
|
| 125 | 128 | shl w32 %11 %6 1; |
|
| 126 | 129 | or w32 %12 %11 1; |
|
| 127 | 130 | br.ult w32 %12 %9 @extend.exact8 @extend.twice9; |
|
| 128 | 131 | @extend.exact8 |
|
| 129 | 132 | jmp @extend.alloc6(%9); |
|
| 130 | 133 | @extend.twice9 |
|
| 131 | 134 | jmp @extend.alloc6(%12); |
|
| 132 | 135 | @extend.copy10 |
|
| 133 | - | load w32 %18 %3 8; |
|
| 134 | - | load w64 %19 %4 8; |
|
| 135 | - | load w64 %20 %3 0; |
|
| 136 | - | mul w64 %21 %18 4; |
|
| 136 | + | load w32 %15 %3 8; |
|
| 137 | + | load w64 %16 %4 8; |
|
| 138 | + | load w64 %17 %3 0; |
|
| 139 | + | mul w64 %18 %15 4; |
|
| 137 | 140 | jmp @extend11(0); |
|
| 138 | - | @extend11(w64 %22) |
|
| 139 | - | br.ult w64 %22 %21 @extend12 @extend13; |
|
| 141 | + | @extend11(w64 %19) |
|
| 142 | + | br.ult w64 %19 %18 @extend12 @extend13; |
|
| 140 | 143 | @extend12 |
|
| 141 | - | add w64 %23 %20 %22; |
|
| 142 | - | load w32 %24 %23 0; |
|
| 143 | - | add w64 %25 %19 %22; |
|
| 144 | - | store w32 %24 %25 0; |
|
| 145 | - | add w64 %26 %22 4; |
|
| 146 | - | jmp @extend11(%26); |
|
| 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); |
|
| 147 | 150 | @extend13 |
|
| 148 | - | store w64 %19 %3 0; |
|
| 151 | + | store w64 %16 %3 0; |
|
| 149 | 152 | store w32 %10 %3 12; |
|
| 150 | 153 | jmp @extend.place2; |
|
| 151 | - | @extend14(w64 %36) |
|
| 152 | - | br.ult w64 %36 %35 @extend15 @extend16; |
|
| 154 | + | @extend14(w64 %33) |
|
| 155 | + | br.ult w64 %33 %32 @extend15 @extend16; |
|
| 153 | 156 | @extend15 |
|
| 154 | - | add w64 %37 %8 %36; |
|
| 155 | - | load w64 %38 %37 0; |
|
| 156 | - | add w64 %39 %29 %36; |
|
| 157 | - | store w64 %38 %39 0; |
|
| 158 | - | add w64 %40 %36 8; |
|
| 159 | - | jmp @extend14(%40); |
|
| 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); |
|
| 160 | 163 | @extend16 |
|
| 161 | - | jmp @extend17(%36); |
|
| 162 | - | @extend17(w64 %41) |
|
| 163 | - | br.ult w64 %41 %30 @extend18 @extend19; |
|
| 164 | + | jmp @extend17(%33); |
|
| 165 | + | @extend17(w64 %38) |
|
| 166 | + | br.ult w64 %38 %27 @extend18 @extend19; |
|
| 164 | 167 | @extend18 |
|
| 165 | - | add w64 %42 %8 %41; |
|
| 166 | - | load w32 %43 %42 0; |
|
| 167 | - | add w64 %44 %29 %41; |
|
| 168 | - | store w32 %43 %44 0; |
|
| 169 | - | add w64 %45 %41 4; |
|
| 170 | - | jmp @extend17(%45); |
|
| 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); |
|
| 171 | 174 | @extend19 |
|
| 172 | 175 | store w32 %9 %3 8; |
|
| 173 | 176 | store w64 0 %4 0; |
|
| 174 | 177 | jmp @extend.done3; |
|
| 175 | 178 | @ok20 |
test/tests/trait.dispatch.ril
+21 -11
| 1 | 1 | layout $L0 size 4 align 4 {} |
|
| 2 | - | layout $L1 size 16 align 8 {} |
|
| 2 | + | layout $L1 size 16 align 8 { |
|
| 3 | + | slot 0 fn $S0; |
|
| 4 | + | slot 8 fn $S1; |
|
| 5 | + | } |
|
| 6 | + | layout $L2 size 16 align 8 { |
|
| 7 | + | slot 0 trait own mut $T0; |
|
| 8 | + | } |
|
| 9 | + | ||
| 10 | + | signature $S0 (ptr ref const $L0) -> w32; |
|
| 11 | + | signature $S1 (ptr ref mut $L0, w32) -> void; |
|
| 12 | + | signature $S2 () -> w32; |
|
| 13 | + | signature $S3 (w32) -> void; |
|
| 14 | + | ||
| 15 | + | trait $T0 { |
|
| 16 | + | method $S2 at 0 ref const; |
|
| 17 | + | method $S3 at 0 ref mut; |
|
| 18 | + | } |
|
| 3 | 19 | ||
| 4 | 20 | data $"vtable::Acc::Ops" align 8 $L1 { |
|
| 5 | 21 | fn $"Acc::get"; |
|
| 6 | 22 | fn $"Acc::put"; |
|
| 7 | 23 | } |
| 16 | 32 | @entry0 |
|
| 17 | 33 | store w32 %1 %0 0; |
|
| 18 | 34 | ret; |
|
| 19 | 35 | } |
|
| 20 | 36 | ||
| 21 | - | fn w32 $dispatch(ptr ref const $L1 %0) { |
|
| 37 | + | fn w32 $dispatch(ptr ref const $L2 %0) { |
|
| 22 | 38 | @entry0 |
|
| 23 | - | load w64 %1 %0 0; |
|
| 24 | - | load w64 %2 %0 8; |
|
| 25 | - | load w64 %3 %2 8; |
|
| 26 | - | call void %3(%1, 42); |
|
| 27 | - | load w64 %4 %0 0; |
|
| 28 | - | load w64 %5 %0 8; |
|
| 29 | - | load w64 %6 %5 0; |
|
| 30 | - | call w32 %7 %6(%4); |
|
| 31 | - | ret %7; |
|
| 39 | + | trait.call void %0 1 at 0(undefined, 42); |
|
| 40 | + | trait.call w32 %1 %0 0 at 0(undefined); |
|
| 41 | + | ret %1; |
|
| 32 | 42 | } |
test/tests/trait.object.narrow.rad
added
+45 -0
| 1 | + | //! returns: 0 |
|
| 2 | + | //! A trait object passes to a parameter that has fewer rights to its receiver. |
|
| 3 | + | ||
| 4 | + | /// A counter. |
|
| 5 | + | record Counter: Copy { |
|
| 6 | + | /// Current count. |
|
| 7 | + | n: i32, |
|
| 8 | + | } |
|
| 9 | + | ||
| 10 | + | /// Operations on a counter. |
|
| 11 | + | trait Count { |
|
| 12 | + | /// Read the count. |
|
| 13 | + | fn (&Count) get() -> i32; |
|
| 14 | + | /// Add to the count. |
|
| 15 | + | fn (&mut Count) add(n: i32); |
|
| 16 | + | } |
|
| 17 | + | ||
| 18 | + | instance Count for Counter { |
|
| 19 | + | fn (c: &Counter) get() -> i32 { |
|
| 20 | + | return c.n; |
|
| 21 | + | } |
|
| 22 | + | ||
| 23 | + | fn (c: &mut Counter) add(n: i32) { |
|
| 24 | + | set c.n += n; |
|
| 25 | + | } |
|
| 26 | + | } |
|
| 27 | + | ||
| 28 | + | /// Read the count through a read-only object. |
|
| 29 | + | fn read(c: &opaque Count) -> i32 { |
|
| 30 | + | return c.get(); |
|
| 31 | + | } |
|
| 32 | + | ||
| 33 | + | /// Add to the count, then read it through an object with fewer rights. |
|
| 34 | + | fn bump(c: &mut opaque Count, n: i32) -> i32 { |
|
| 35 | + | c.add(n); |
|
| 36 | + | return read(c); |
|
| 37 | + | } |
|
| 38 | + | ||
| 39 | + | @default fn main() -> i32 { |
|
| 40 | + | let mut counter = Counter { n: 1 }; |
|
| 41 | + | assert bump(&mut counter, 2) == 3; |
|
| 42 | + | assert bump(&mut counter, 4) == 7; |
|
| 43 | + | assert counter.n == 7; |
|
| 44 | + | return 0; |
|
| 45 | + | } |
test/tests/trait.object.ril
+5 -1
| 1 | - | layout $L0 size 8 align 8 {} |
|
| 1 | + | layout $L0 size 8 align 8 { |
|
| 2 | + | slot 0 fn $S0; |
|
| 3 | + | } |
|
| 2 | 4 | layout $L1 size 4 align 4 {} |
|
| 3 | 5 | layout $L2 size 16 align 8 {} |
|
| 4 | 6 | ||
| 7 | + | signature $S0 (w64, w32) -> w32; |
|
| 8 | + | ||
| 5 | 9 | data $"vtable::Counter::Adder" align 8 $L0 { |
|
| 6 | 10 | fn $"Counter::add"; |
|
| 7 | 11 | } |
|
| 8 | 12 | ||
| 9 | 13 | fn w32 $"Counter::add"(w64 %0, w32 %1) { |
test/tests/trait.supertrait.ril
+23 -10
| 1 | 1 | layout $L0 size 16 align 8 {} |
|
| 2 | 2 | layout $L1 size 1 align 1 {} |
|
| 3 | - | layout $L2 size 8 align 8 {} |
|
| 3 | + | layout $L2 size 8 align 8 { |
|
| 4 | + | slot 0 fn $S0; |
|
| 5 | + | } |
|
| 4 | 6 | layout $L3 size 16 align 8 { |
|
| 5 | 7 | slot 0 slice own const $L1; |
|
| 6 | 8 | } |
|
| 7 | - | layout $L4 size 24 align 8 {} |
|
| 8 | - | layout $L5 size 76 align 4 {} |
|
| 9 | - | layout $L6 size 3 align 1 {} |
|
| 10 | - | layout $L7 size 8 align 1 {} |
|
| 9 | + | layout $L4 size 8 align 8 { |
|
| 10 | + | slot 0 fn $S1; |
|
| 11 | + | } |
|
| 12 | + | layout $L5 size 24 align 8 { |
|
| 13 | + | slot 0 fn $S0; |
|
| 14 | + | slot 8 fn $S1; |
|
| 15 | + | slot 16 fn $S2; |
|
| 16 | + | } |
|
| 17 | + | layout $L6 size 76 align 4 {} |
|
| 18 | + | layout $L7 size 3 align 1 {} |
|
| 19 | + | layout $L8 size 8 align 1 {} |
|
| 20 | + | ||
| 21 | + | signature $S0 (w64, ptr ref const $L0) -> w32; |
|
| 22 | + | signature $S1 (w64, ptr ref const $L3) -> w32; |
|
| 23 | + | signature $S2 (w64) -> w32; |
|
| 11 | 24 | ||
| 12 | 25 | data $"vtable::Socket::Reader" align 8 $L2 { |
|
| 13 | 26 | fn $"Socket::read"; |
|
| 14 | 27 | } |
|
| 15 | 28 | ||
| 16 | - | data $"vtable::Socket::Writer" align 8 $L2 { |
|
| 29 | + | data $"vtable::Socket::Writer" align 8 $L4 { |
|
| 17 | 30 | fn $"Socket::write"; |
|
| 18 | 31 | } |
|
| 19 | 32 | ||
| 20 | - | data $"vtable::Socket::ReadWriter" align 8 $L4 { |
|
| 33 | + | data $"vtable::Socket::ReadWriter" align 8 $L5 { |
|
| 21 | 34 | fn $"Socket::read"; |
|
| 22 | 35 | fn $"Socket::write"; |
|
| 23 | 36 | fn $"Socket::flush"; |
|
| 24 | 37 | } |
|
| 25 | 38 | ||
| 26 | - | data $main$literal$0 align 1 $L6 { |
|
| 39 | + | data $main$literal$0 align 1 $L7 { |
|
| 27 | 40 | str "abc"; |
|
| 28 | 41 | } |
|
| 29 | 42 | ||
| 30 | 43 | fn w32 $"Socket::read"(w64 %0, ptr ref const $L0 %1) { |
|
| 31 | 44 | @entry0 |
| 95 | 108 | ret %1; |
|
| 96 | 109 | } |
|
| 97 | 110 | ||
| 98 | 111 | fn w32 $main() { |
|
| 99 | 112 | @entry0 |
|
| 100 | - | reserve %0 $L5; |
|
| 113 | + | reserve %0 $L6; |
|
| 101 | 114 | store w32 0 %0 32; |
|
| 102 | 115 | store w32 5 %0 36; |
|
| 103 | 116 | store w32 0 %0 72; |
|
| 104 | 117 | elem %1 %0 0 $L1 32; |
|
| 105 | 118 | store w8 104 %1 0; |
| 128 | 141 | sload w32 %13 %0 72; |
|
| 129 | 142 | br.eq w32 %13 3 @assert.ok4 @assert.fail3; |
|
| 130 | 143 | @assert.fail3 |
|
| 131 | 144 | unreachable; |
|
| 132 | 145 | @assert.ok4 |
|
| 133 | - | reserve %14 $L7; |
|
| 146 | + | reserve %14 $L8; |
|
| 134 | 147 | load w64 %15 %6 0; |
|
| 135 | 148 | load w64 %16 %6 8; |
|
| 136 | 149 | load w64 %17 %16 0; |
|
| 137 | 150 | reserve %18 $L0; |
|
| 138 | 151 | store w64 %14 %18 0; |
test/verify/check.rad
+4 -0
| 51 | 51 | } |
|
| 52 | 52 | case verify::Place::Signature(i) => { |
|
| 53 | 53 | io::print("signature "); |
|
| 54 | 54 | io::printU32(i); |
|
| 55 | 55 | } |
|
| 56 | + | case verify::Place::Trait(i) => { |
|
| 57 | + | io::print("trait "); |
|
| 58 | + | io::printU32(i); |
|
| 59 | + | } |
|
| 56 | 60 | case verify::Place::Data(i) => { |
|
| 57 | 61 | io::print("data "); |
|
| 58 | 62 | io::print(program.data[i].name); |
|
| 59 | 63 | } |
|
| 60 | 64 | case verify::Place::Fn(i) => { |
test/verify/expected
+18 -22
| 1 | - | alloc.arena.trait Type |
|
| 1 | + | alloc.arena.trait Slot |
|
| 2 | 2 | array.pointer Type |
|
| 3 | 3 | array.slice.gen.index Type |
|
| 4 | 4 | array.slice Type |
|
| 5 | 5 | bool.comparison.slice.record.gen Slot |
|
| 6 | 6 | bool.comparison.slice Slot |
| 36 | 36 | match.nested.deref Type |
|
| 37 | 37 | match.nested.enum.byref Type |
|
| 38 | 38 | match.nested.guard.field Type |
|
| 39 | 39 | match.variant.fields.bind Slot |
|
| 40 | 40 | match.variant.fields.deref Type |
|
| 41 | - | method.eight.params.throws Type |
|
| 42 | - | method.eight.params Type |
|
| 43 | 41 | method.multiple Type |
|
| 44 | 42 | method.with.trait Type |
|
| 45 | 43 | mutref.call.result Type |
|
| 46 | 44 | mutref.loop Type |
|
| 47 | 45 | opt.slice.npo Type |
|
| 48 | 46 | pointer.arithmetic.unsafe Type |
|
| 49 | - | pointer.borrow.unsafe Type |
|
| 47 | + | pointer.borrow.unsafe View |
|
| 50 | 48 | pointer.cast.unsafe View |
|
| 51 | 49 | pointer.copy.edge.case Type |
|
| 52 | 50 | pointer.slice.index Type |
|
| 53 | 51 | pointer.slice.store Data |
|
| 54 | 52 | pointer.stack.unsafe Type |
| 66 | 64 | range.arithmetic Type |
|
| 67 | 65 | record.scalar Class |
|
| 68 | 66 | ref.local.slice.release Type |
|
| 69 | 67 | ref.mut.ptr Type |
|
| 70 | 68 | slice.alloc.loop Type |
|
| 71 | - | slice.alloc.trait Type |
|
| 72 | - | slice.append.errors Type |
|
| 73 | - | slice.append.failure Type |
|
| 69 | + | slice.alloc.trait Slot |
|
| 70 | + | slice.append.errors Slot |
|
| 71 | + | slice.append.failure Slot |
|
| 74 | 72 | slice.append.limit Type |
|
| 75 | 73 | slice.append.limit.trap Symbol |
|
| 76 | - | slice.append.trait Type |
|
| 77 | - | slice.append Type |
|
| 74 | + | slice.append.trait Slot |
|
| 75 | + | slice.append Slot |
|
| 78 | 76 | slice.append.values Type |
|
| 79 | 77 | slice.assign Type |
|
| 80 | 78 | slice.basic Slot |
|
| 81 | 79 | slice.cap Type |
|
| 82 | 80 | slice.construct.unsafe Slot |
|
| 83 | 81 | slice.delete Type |
|
| 84 | 82 | slice.empty.nonnull Data |
|
| 85 | - | slice.extend.borrowed Type |
|
| 86 | - | slice.extend.bytes Type |
|
| 87 | - | slice.extend.copy Type |
|
| 83 | + | slice.extend.borrowed Slot |
|
| 84 | + | slice.extend.bytes Slot |
|
| 85 | + | slice.extend.copy Slot |
|
| 88 | 86 | slice.extend.overflow Type |
|
| 89 | 87 | slice.extend.overflow.words Type |
|
| 90 | - | slice.failure Type |
|
| 88 | + | slice.failure Slot |
|
| 91 | 89 | slice.grow Type |
|
| 92 | 90 | slice.header.unsafe Slot |
|
| 93 | 91 | slice.of Type |
|
| 94 | 92 | slice.ptr.unsafe Slot |
|
| 95 | - | slice.range.capacity Type |
|
| 93 | + | slice.range.capacity Slot |
|
| 96 | 94 | slice.range.dynamic Type |
|
| 97 | 95 | slice.reserve.limit Symbol |
|
| 98 | - | slice.reserve.result Type |
|
| 96 | + | slice.reserve.result Slot |
|
| 99 | 97 | slice.reserve Type |
|
| 100 | - | slice.resize.extend Type |
|
| 101 | - | slice.resize.fill.snapshot Type |
|
| 102 | - | slice.sizes Type |
|
| 98 | + | slice.resize.extend Slot |
|
| 99 | + | slice.resize.fill.snapshot Slot |
|
| 100 | + | slice.sizes Slot |
|
| 103 | 101 | slice.subslice Type |
|
| 104 | - | slice.take.sizes Type |
|
| 105 | - | slice.take Type |
|
| 102 | + | slice.take.sizes Slot |
|
| 103 | + | slice.take Slot |
|
| 106 | 104 | slice.zero-sized Slot |
|
| 107 | 105 | slice.zero-sized.limit Symbol |
|
| 108 | 106 | static.empty.slice.optional Data |
|
| 109 | 107 | static.slice.empty Data |
|
| 110 | 108 | static.slice.empty.record.table Data |
| 114 | 112 | static.zero.record.repeat Data |
|
| 115 | 113 | trait.aggregate.ret Type |
|
| 116 | 114 | trait.array.optional Type |
|
| 117 | 115 | trait.basic Type |
|
| 118 | 116 | trait.control.flow Type |
|
| 119 | - | trait.dispatch Type |
|
| 120 | 117 | trait.fn.param Type |
|
| 121 | 118 | trait.multiple.methods Type |
|
| 122 | 119 | trait.multiple.traits Type |
|
| 123 | 120 | trait.multiple.types Type |
|
| 124 | 121 | trait.object Type |
|
| 125 | - | trait.param.names.shared Type |
|
| 126 | 122 | trait.supertrait.forward Type |
|
| 127 | 123 | trait.supertrait Type |
|
| 128 | 124 | trait.throws Type |
|
| 129 | 125 | trait.writer Type |
|
| 130 | 126 | undefined.merge.pointer Type |