lang: Encode RIL as binary packages

fb2c0458ae5439c1916687fbece38dff63a0574129b5d8944a5294fb56055b6a
Programs had no stored form of RIL between lowering and code
generation, so a package could not be compiled apart from the program
that uses it.

Add the `std::lang::il::binary` modules, which encode and decode typed
RIL packages: name tables, layouts, signatures, data, and functions,
with bounds checks on every length in the input. The compiler gains
`-ril <directory>`, which lowers each package to one `.ril` file. It
encodes each function as the lowerer produces it, so memory does not
grow with the IL of the program. Add encode and decode tests, and let
`test/compile` build a fixture to a RIL directory.

Assisted-by: Codex:gpt-6
Assisted-by: Claude:opus-5.5
Alexis Sellier committed ago 1 parent 1befb074
compiler/radiance.rad +119 -2
20 20
use std::arch::rv64::image;
21 21
use std::arch::rv64::printer;
22 22
use std::lang::sexpr;
23 23
use std::lang::gen::data;
24 24
use std::lang::gen::labels;
25 +
use std::lang::il::binary;
26 +
use std::lang::il::binary::collect;
27 +
use std::lang::il::binary::program;
25 28
use std::lang::gen::types;
26 29
use std::sys;
27 30
use std::sys::unix;
28 31
use std::collections::dict;
29 32
83 86
/// Symbol name exported for startup code to call the semantic entry function.
84 87
export constant DEFAULT_ENTRY_SYMBOL: *[u8] = "::default";
85 88
86 89
/// Usage string.
87 90
constant USAGE: *[u8] =
88 -
    "usage: radiance -pkg <name> [-start <input.ras>] -mod <input>.. [-pkg <name> -mod <input>..] -entry <pkg> [-o <output>]\n-mod accepts a source path or logical/path=source.rad relative to the package root.\n";
91 +
    "usage: radiance -pkg <name> [-start <input.ras>] -mod <input>.. [-pkg <name> -mod <input>..] -entry <pkg> [-o <output> | -ril <directory>]\n-mod accepts a source path or logical/path=source.rad relative to the package root.\n";
89 92
90 93
/// Compiler error.
91 94
union Error: Copy {
92 95
    Other,
93 96
}
138 141
    config: resolver::Config,
139 142
    /// What to dump during compilation.
140 143
    dump: Dump,
141 144
    /// Output path for binary.
142 145
    outputPath: ?*[u8],
146 +
    /// Output directory for separate binary RIL packages.
147 +
    rilDirectory: ?*[u8],
143 148
    /// Whether to emit debug info (.debug file).
144 149
    debug: bool,
145 150
    /// Whether the generated code checks every frame against the stack limit in `tp`.
146 151
    stackCheck: bool,
147 152
}
428 433
) -> CompileContext throws (Error) {
429 434
    let mut buildTest = false;
430 435
    let mut debugEnabled = false;
431 436
    let mut stackChecked = false;
432 437
    let mut outputPath: ?*[u8] = nil;
438 +
    let mut rilDirectory: ?*[u8] = nil;
433 439
    let mut dump = Dump::None;
434 440
    let mut entryPkgName: ?*[u8] = nil;
435 441
436 442
    // Per-package source path tracking.
437 443
    let mut inputs: [PackageInput; MAX_PACKAGES] = [packageInput(""); MAX_PACKAGES];
503 509
        } else if mem::eq(arg, "-stack-check") {
504 510
            set stackChecked = true;
505 511
        } else if mem::eq(arg, "-o") {
506 512
            try nextArg(args, &mut idx, &["`-o` requires an output path"]);
507 513
            set outputPath = args[idx];
514 +
        } else if mem::eq(arg, "-ril") {
515 +
            try nextArg(args, &mut idx, &["`-ril` requires an output directory"]);
516 +
            set rilDirectory = args[idx];
508 517
        } else if mem::eq(arg, "-dump") {
509 518
            try nextArg(args, &mut idx, &["`-dump` requires a mode (eg. ast)"]);
510 519
            let mode = args[idx];
511 520
            if mem::eq(mode, "ast") {
512 521
                set dump = Dump::Ast;
559 568
    for i in 0..pkgCount {
560 569
        if i <> entryIdx and inputs[i].startupPath <> nil {
561 570
            throw error(&["`-start` is only supported on the entry package"]);
562 571
        }
563 572
    }
573 +
    if rilDirectory <> nil and (outputPath <> nil or dump <> Dump::None) {
574 +
        throw error(&["`-ril` requires a separate invocation from `-o` or `-dump`"]);
575 +
    }
564 576
    let mut ctx = CompileContext {
565 577
        packages: [package::Package { id: 0, name: "", rootModuleId: nil }; MAX_PACKAGES],
566 578
        inputs,
567 579
        packageCount: pkgCount,
568 580
        entryPkgIdx,
569 581
        parser: parser::newParser(nodes, pool),
570 582
        config: resolver::Config { buildTest },
571 583
        dump,
572 584
        outputPath,
585 +
        rilDirectory,
573 586
        debug: debugEnabled,
574 587
        stackCheck: stackChecked,
575 588
    };
576 589
    // Initialize and parse all packages.
577 590
    for i in 0..pkgCount {
642 655
) -> lower::Lowerer {
643 656
    let entryIdx = ctx.entryPkgIdx else {
644 657
        panic "newLowerer: no entry package";
645 658
    };
646 659
    let entryPkg = &ctx.packages[entryIdx];
660 +
    let tableScope = lower::TableScope::Package if ctx.rilDirectory <> nil
661 +
        else lower::TableScope::Program;
647 662
    let options = lower::LowerOptions { debug: ctx.debug, buildTest: ctx.config.buildTest,
648 -
        tableScope: lower::TableScope::Program };
663 +
        tableScope };
649 664
650 665
    return lower::lowerer(res, modules, entryPkg.name, fnArena, options);
651 666
}
652 667
653 668
/// Lower all packages into an existing lowerer. Each function goes to `sink`.
1228 1243
        }
1229 1244
        throw Error::Other;
1230 1245
    };
1231 1246
}
1232 1247
1248 +
/// Size of the buffer that holds the tables of one binary RIL package (4 MB).
1249 +
/// It holds the symbol table, the exports, and the data section.
1250 +
constant RIL_HEAD_SIZE: u32 = 4194304;
1251 +
1252 +
/// Emit one binary RIL file per package into an existing directory.
1253 +
///
1254 +
/// Each function is encoded as the lowerer produces it, so memory does not grow
1255 +
/// with the IL of the program. The tables of a package precede its functions in
1256 +
/// the file, and they are complete only after the last function, so the file is
1257 +
/// written once lowering ends.
1258 +
fn emitPackages(
1259 +
    ctx: &mut CompileContext,
1260 +
    modules: module::ModuleTable,
1261 +
    res: resolver::Resolver,
1262 +
    fnArena: alloc::Arena,
1263 +
    backend: &mut BackendStorage,
1264 +
    directory: *[u8]
1265 +
) throws (Error) {
1266 +
    let mut names: [*[u8]; MAX_PACKAGES] = [""; MAX_PACKAGES];
1267 +
    for i in 0..ctx.packageCount {
1268 +
        if ctx.inputs[i].asmPathCount > 0 or ctx.inputs[i].startupPath <> nil {
1269 +
            throw error(&["binary RIL output requires Radiance source modules"]);
1270 +
        }
1271 +
        set names[i] = ctx.packages[i].name;
1272 +
    }
1273 +
    let mut spool = try codegen::ril(alloc::new(backend.emitter.take()), backend.ilText.take(),
1274 +
        &names[..ctx.packageCount]) catch {
1275 +
        throw error(&["out of memory for binary RIL tables"]);
1276 +
    };
1277 +
    let mut low = newLowerer(ctx, modules, res, fnArena);
1278 +
    try lowerAllPackagesInto(ctx, modules, &mut low, &mut spool);
1279 +
    let dataOrigins = low.dataOrigins.take();
1280 +
    let tableStarts = low.tables.tableStarts.take();
1281 +
    let whole = lower::finalize(low, lower::FnCollector { fns: &mut [] });
1282 +
1283 +
    let mut head: *mut [u8] = &mut [];
1284 +
    try head.resize(RIL_HEAD_SIZE, 0, &mut spool.arena) catch {
1285 +
        throw error(&["out of memory for binary RIL tables"]);
1286 +
    };
1287 +
    for i in 0..ctx.packageCount {
1288 +
        let pkg = &ctx.packages[i];
1289 +
        let tables = lower::packageTables(&whole, &tableStarts[..], pkg.id);
1290 +
        let mut dataItems: *mut [il::Data] = &mut [];
1291 +
        for item, j in whole.data {
1292 +
            let origin = dataOrigins[j];
1293 +
            if origin.package <> pkg.id {
1294 +
                continue;
1295 +
            }
1296 +
            try dataItems.append(item, &mut spool.arena) catch {
1297 +
                throw error(&["out of memory selecting package data", pkg.name]);
1298 +
            };
1299 +
            if let case lower::Linkage::Exported = origin.linkage {
1300 +
                let definition = binary::Export { name: item.name, kind: binary::ExportKind::Data };
1301 +
                try spool.packages[i].exports.append(definition, &mut spool.arena) catch {
1302 +
                    throw error(&["out of memory collecting package exports", pkg.name]);
1303 +
                };
1304 +
            }
1305 +
        }
1306 +
        let exports = spool.packages[i].exports.take();
1307 +
        let entry = spool.packages[i].entry;
1308 +
        let image = try collect::finish(
1309 +
            &mut spool.packages[i].names, pkg.name,
1310 +
            il::Program {
1311 +
                data: &dataItems[..], fns: &[], layouts: tables.layouts,
1312 +
                    signatures: tables.signatures,
1313 +
            }, &exports[..], entry, &mut spool.arena
1314 +
        ) catch {
1315 +
            throw error(&["cannot collect binary RIL package", pkg.name]);
1316 +
        };
1317 +
        let fnCount = spool.packages[i].count;
1318 +
        let length = try program::encodeHead(
1319 +
            &mut head, &image, &mut spool.packages[i].names.index, fnCount
1320 +
        ) catch {
1321 +
            throw error(&["binary RIL tables exceed their buffer", pkg.name]);
1322 +
        };
1323 +
        let mut path: [u8; MAX_PATH_LEN] = [0; MAX_PATH_LEN];
1324 +
        let mut pos: u32 = 0;
1325 +
        for part in &[directory, "/", pkg.name, ".ril"] {
1326 +
            set pos += try mem::copy(&mut path[pos..MAX_PATH_LEN - 1], part) catch {
1327 +
                throw error(&["binary RIL output path is too long"]);
1328 +
            };
1329 +
        }
1330 +
        let fd = unix::openOpts(&path[..pos],
1331 +
            unix::OpenFlags(*unix::O_WRONLY | *unix::O_CREAT | *unix::O_TRUNC),
1332 +
                unix::FILE_MODE);
1333 +
        if fd < 0 {
1334 +
            throw error(&["cannot write binary RIL package", pkg.name]);
1335 +
        }
1336 +
        let start = spool.packages[i].start;
1337 +
        let end = spool.packages[i].end;
1338 +
        let written = unix::writeAll(fd, &head[..length]) and unix::writeAll(fd,
1339 +
            &spool.bytes[start..end]);
1340 +
        if not (unix::close(fd) == 0 and written) {
1341 +
            throw error(&["cannot write binary RIL package", pkg.name]);
1342 +
        }
1343 +
    }
1344 +
}
1345 +
1233 1346
/// Lower, optionally dump, and optionally generate binary output.
1234 1347
/// The lowerer takes ownership of `res` and of `fnArena`, and the passes take
1235 1348
/// their buffers from `backend`.
1236 1349
fn compile(
1237 1350
    ctx: &mut CompileContext,
1239 1352
    res: resolver::Resolver,
1240 1353
    fnArena: alloc::Arena,
1241 1354
    backend: &mut BackendStorage
1242 1355
) throws (Error) {
1243 1356
    let entryPkg = try getEntryPackage(ctx);
1357 +
    if let directory = ctx.rilDirectory {
1358 +
        try emitPackages(ctx, modules, res, fnArena, backend, directory);
1359 +
        return;
1360 +
    }
1244 1361
    let mut out = sexpr::Stdout {};
1245 1362
1246 1363
    if ctx.dump == Dump::Il {
1247 1364
        // Print each function as it is lowered. The data section is complete
1248 1365
        // after the last function, and it comes first in the dump.
compiler/radiance/codegen.rad +109 -0
7 7
use std::lang::alloc;
8 8
use std::lang::il;
9 9
use std::lang::lower;
10 10
use std::lang::sexpr;
11 11
use std::lang::gen::data;
12 +
use std::lang::il::binary;
13 +
use std::lang::il::binary::collect;
14 +
use std::lang::il::binary::program;
15 +
use std::mem;
12 16
13 17
/// Code generator that receives the functions of a streaming lowering pass.
14 18
export record Context {
15 19
    /// Generator owned by the context for the whole lowering pass.
16 20
    generator: rv64::Generator,
78 82
        layouts: &[il::Layout], fnArena: &mut alloc::Arena) throws (lower::LowerError) {
79 83
        resetFnArena(fnArena);
80 84
    }
81 85
}
82 86
87 +
/// Binary RIL state of one package while its functions stream in.
88 +
export record RilPackage {
89 +
    /// Package name.
90 +
    name: *[u8],
91 +
    /// Interned names, shared by the functions and the tables of the package.
92 +
    names: collect::Names,
93 +
    /// Offset of the first encoded function in the spool.
94 +
    start: u32,
95 +
    /// Offset past the last encoded function in the spool.
96 +
    end: u32,
97 +
    /// Number of encoded functions.
98 +
    count: u32,
99 +
    /// Definitions that other packages can link against: the exported functions as they
100 +
    /// arrive, and the exported data once the driver has lowered the package.
101 +
    exports: *mut [binary::Export],
102 +
    /// Stored name of the function lowered with the default role, if any. It is the
103 +
    /// generated storage wrapper when the default function takes storage parameters.
104 +
    entry: ?*[u8],
105 +
}
106 +
107 +
/// Sink that encodes each function of a package into binary RIL and reclaims
108 +
/// its IL arena. Only the encoded bytes and the name tables stay, so the memory
109 +
/// does not grow with the IL of the program. Each package owns one contiguous
110 +
/// extent of `bytes`, because lowering finishes a package before the next.
111 +
export record RilSpool {
112 +
    /// Storage of the name tables. It lives until the packages are written.
113 +
    arena: alloc::Arena,
114 +
    /// Encoded functions of all packages, in lowering order.
115 +
    bytes: *mut [u8],
116 +
    /// Number of bytes of `bytes` in use.
117 +
    used: u32,
118 +
    /// One entry per package.
119 +
    packages: *mut [RilPackage],
120 +
}
121 +
122 +
/// Create a spool for the packages named `names`.
123 +
export fn ril(arena: alloc::Arena, bytes: *mut [u8], names: &[*[u8]]) -> RilSpool
124 +
    throws (binary::Error) {
125 +
    let mut owned = arena;
126 +
    let mut packages: *mut [RilPackage] = &mut [];
127 +
    for name in names {
128 +
        let mut tables = try collect::new(collect::INDEX_SLOTS, &mut owned);
129 +
        try collect::start(&mut tables, name, &mut owned);
130 +
        try packages.append(RilPackage {
131 +
            name, names: tables, start: 0, end: 0, count: 0,
132 +
            exports: &mut [], entry: nil,
133 +
        }, &mut owned) catch {
134 +
            throw binary::Error::Storage;
135 +
        };
136 +
    }
137 +
    return RilSpool { arena: owned, bytes, used: 0, packages };
138 +
}
139 +
140 +
instance lower::FnSink for RilSpool {
141 +
    /// Encode one function into its package and reclaim its IL arena.
142 +
    fn (spool: &mut RilSpool) emit(func: *il::Fn, role: lower::FnRole, origin: lower::Origin,
143 +
        layouts: &[il::Layout], fnArena: &mut alloc::Arena) throws (lower::LowerError) {
144 +
        try encodeFunction(spool, func, role, origin) catch error {
145 +
            resetFnArena(fnArena);
146 +
            match error {
147 +
                case binary::Error::Capacity => io::printError(
148 +
                    "radiance: binary RIL output capacity exceeded\n"),
149 +
                case binary::Error::Storage => io::printError(
150 +
                    "radiance: binary RIL name storage exhausted\n"),
151 +
                else => io::printError("radiance: invalid binary RIL function\n"),
152 +
            }
153 +
            throw lower::LowerError::OutputFailed;
154 +
        };
155 +
        resetFnArena(fnArena);
156 +
    }
157 +
}
158 +
159 +
/// Collect the names of `func` and append its encoding to the package that `origin` names.
160 +
/// A function with the default role becomes the entry of its package. An exported function
161 +
/// is also an export.
162 +
fn encodeFunction(spool: &mut RilSpool, func: *il::Fn, role: lower::FnRole, origin: lower::Origin)
163 +
    throws (binary::Error) {
164 +
    let i = origin.package as u32;
165 +
    if i >= spool.packages.len {
166 +
        throw binary::Error::Invalid;
167 +
    }
168 +
    if spool.packages[i].count > 0 and spool.packages[i].end <> spool.used {
169 +
        throw binary::Error::Invalid;
170 +
    }
171 +
    let name = spool.packages[i].name;
172 +
    let stored = try collect::function(&mut spool.packages[i].names, name, func, &mut spool.arena);
173 +
    if let case lower::Linkage::Exported = origin.linkage {
174 +
        let definition = binary::Export { name: stored, kind: binary::ExportKind::Function };
175 +
        try spool.packages[i].exports.append(definition, &mut spool.arena) catch {
176 +
            throw binary::Error::Storage;
177 +
        };
178 +
    }
179 +
    if let case lower::FnRole::Default = role {
180 +
        set spool.packages[i].entry = stored;
181 +
    }
182 +
    let end = try program::appendFunction(&mut spool.bytes, spool.used,
183 +
        &mut spool.packages[i].names.index, func);
184 +
    if spool.packages[i].count == 0 {
185 +
        set spool.packages[i].start = spool.used;
186 +
    }
187 +
    set spool.packages[i].end = end;
188 +
    set spool.packages[i].count += 1;
189 +
    set spool.used = end;
190 +
}
191 +
83 192
/// Reset the function arena after a function has been generated.
84 193
///
85 194
/// This is the audited unsafe site of the streaming pass. Invariant: the
86 195
/// lowerer cleared every per-function slice with `endFn` before it called the
87 196
/// sink, and no generated structure keeps a pointer into the arena. No memory
lib/std/lang/il.rad +1 -0
72 72
73 73
// TODO: Labels should have their own type.
74 74
// TODO: Blocks should have an instruction in `Instr`.
75 75
76 76
export mod printer;
77 +
export mod binary;
77 78
78 79
use std::lang::alloc;
79 80
80 81
/// DeviceAccess flag bits that contain the byte width.
81 82
export constant DEVICE_WIDTH_MASK: u64 = 0xff;
lib/std/lang/il/binary.rad added +254 -0
1 +
//! Binary RIL wire definitions. Integers use little-endian byte order.
2 +
//! Sequences have a u32 element count. Symbols, layouts and signatures use u32
3 +
//! table indices. Optional fields use a byte: zero for absent, one for present.
4 +
5 +
export mod writer;
6 +
export mod reader;
7 +
export mod program;
8 +
export mod collect;
9 +
export mod cfg;
10 +
11 +
use std::lang::il;
12 +
@test export mod tests;
13 +
14 +
/// Minimum wire bytes in a register and value-type parameter.
15 +
export constant MIN_PARAM_BYTES: u32 = 5;
16 +
/// Minimum wire bytes in a layout slot.
17 +
export constant MIN_SLOT_BYTES: u32 = 9;
18 +
/// Minimum wire bytes in a global data record.
19 +
export constant MIN_DATA_BYTES: u32 = 14;
20 +
/// Minimum wire bytes in a repeated data value.
21 +
export constant MIN_DATA_VALUE_BYTES: u32 = 5;
22 +
/// Minimum wire bytes in a function record.
23 +
export constant MIN_FUNCTION_BYTES: u32 = 14;
24 +
/// Minimum wire bytes in a block record.
25 +
export constant MIN_BLOCK_BYTES: u32 = 13;
26 +
/// Minimum wire bytes in an exported symbol.
27 +
export constant MIN_EXPORT_BYTES: u32 = 5;
28 +
/// Minimum wire bytes in a layout record.
29 +
export constant MIN_LAYOUT_BYTES: u32 = 12;
30 +
/// Minimum wire bytes in a function signature.
31 +
export constant MIN_SIGNATURE_BYTES: u32 = 5;
32 +
/// Minimum wire bytes in a switch case.
33 +
export constant MIN_SWITCH_CASE_BYTES: u32 = 16;
34 +
35 +
/// Binary RIL signature, encoded as the bytes RIL followed by zero.
36 +
export constant MAGIC: u32 = 0x004c4952;
37 +
38 +
/// A binary RIL encoding or decoding failure.
39 +
export union Error: Copy {
40 +
    /// The output buffer has insufficient space.
41 +
    Capacity,
42 +
    /// A referenced name is absent from the symbol table.
43 +
    Symbol,
44 +
    /// An integer width, tag, or field is invalid.
45 +
    Invalid,
46 +
    /// The input ends inside a field.
47 +
    Truncated,
48 +
    /// The decode arena has insufficient space.
49 +
    Storage,
50 +
}
51 +
52 +
/// Wire tag for value Reg.
53 +
export constant VALUE_REG: u8 = 0;
54 +
/// Wire tag for value Imm.
55 +
export constant VALUE_IMM: u8 = 1;
56 +
/// Wire tag for value DataSym.
57 +
export constant VALUE_DATASYM: u8 = 2;
58 +
/// Wire tag for value FnAddr.
59 +
export constant VALUE_FNADDR: u8 = 3;
60 +
/// Wire tag for value Undef.
61 +
export constant VALUE_UNDEF: u8 = 4;
62 +
63 +
/// Wire tag for instruction Reserve.
64 +
export constant INSTR_RESERVE: u8 = 0;
65 +
/// Wire tag for instruction Load.
66 +
export constant INSTR_LOAD: u8 = 1;
67 +
/// Wire tag for instruction Sload.
68 +
export constant INSTR_SLOAD: u8 = 2;
69 +
/// Wire tag for instruction Store.
70 +
export constant INSTR_STORE: u8 = 3;
71 +
/// Wire tag for instruction Blit.
72 +
export constant INSTR_BLIT: u8 = 4;
73 +
/// Wire tag for instruction Copy.
74 +
export constant INSTR_COPY: u8 = 5;
75 +
/// Wire tag for instruction BinOp.
76 +
export constant INSTR_BINOP: u8 = 6;
77 +
/// Wire tag for instruction UnOp.
78 +
export constant INSTR_UNOP: u8 = 7;
79 +
/// Wire tag for instruction Zext.
80 +
export constant INSTR_ZEXT: u8 = 8;
81 +
/// Wire tag for instruction Sext.
82 +
export constant INSTR_SEXT: u8 = 9;
83 +
/// Wire tag for instruction Call.
84 +
export constant INSTR_CALL: u8 = 10;
85 +
/// Wire tag for instruction Ret.
86 +
export constant INSTR_RET: u8 = 11;
87 +
/// Wire tag for instruction Jmp.
88 +
export constant INSTR_JMP: u8 = 12;
89 +
/// Wire tag for instruction Br.
90 +
export constant INSTR_BR: u8 = 13;
91 +
/// Wire tag for instruction Switch.
92 +
export constant INSTR_SWITCH: u8 = 14;
93 +
/// Wire tag for instruction Unreachable.
94 +
export constant INSTR_UNREACHABLE: u8 = 15;
95 +
/// Wire tag for instruction Ecall.
96 +
export constant INSTR_ECALL: u8 = 16;
97 +
/// Wire tag for instruction Ebreak.
98 +
export constant INSTR_EBREAK: u8 = 17;
99 +
/// Wire tag for instruction MemoryFence.
100 +
export constant INSTR_MEMORYFENCE: u8 = 18;
101 +
/// Wire tag for a checked device read.
102 +
export constant INSTR_DEVICE_READ: u8 = 19;
103 +
/// Wire tag for a checked device write.
104 +
export constant INSTR_DEVICE_WRITE: u8 = 20;
105 +
106 +
/// Wire tag for value type Void.
107 +
export constant VALTYPE_VOID: u8 = 0;
108 +
/// Wire tag for value type Data.
109 +
export constant VALTYPE_DATA: u8 = 1;
110 +
/// Wire tag for value type Ptr.
111 +
export constant VALTYPE_PTR: u8 = 2;
112 +
/// Wire tag for value type Fn.
113 +
export constant VALTYPE_FN: u8 = 3;
114 +
115 +
/// Wire tag for slot kind Ptr.
116 +
export constant SLOT_PTR: u8 = 0;
117 +
/// Wire tag for slot kind Slice.
118 +
export constant SLOT_SLICE: u8 = 1;
119 +
/// Wire tag for slot kind Fn.
120 +
export constant SLOT_FN: u8 = 2;
121 +
/// Wire tag for slot kind Sub.
122 +
export constant SLOT_SUB: u8 = 3;
123 +
/// Wire tag for slot kind Array.
124 +
export constant SLOT_ARRAY: u8 = 4;
125 +
126 +
/// Pointer type flag: the pointee lives as long as the domain.
127 +
export constant PTR_OWN: u8 = 1;
128 +
/// Pointer type flag: the pointee can be written.
129 +
export constant PTR_MUTABLE: u8 = 2;
130 +
/// Pointer type flag: the pointer can be zero.
131 +
export constant PTR_NULLABLE: u8 = 4;
132 +
133 +
/// Wire tag for instruction Field.
134 +
export constant INSTR_FIELD: u8 = 21;
135 +
/// Wire tag for instruction Elem.
136 +
export constant INSTR_ELEM: u8 = 22;
137 +
/// Wire tag for instruction SliceElem.
138 +
export constant INSTR_SLICE_ELEM: u8 = 23;
139 +
/// Wire tag for instruction SliceNew.
140 +
export constant INSTR_SLICE_NEW: u8 = 24;
141 +
/// Wire tag for instruction SliceSub.
142 +
export constant INSTR_SLICE_SUB: u8 = 25;
143 +
/// Wire tag for instruction LoadPtr.
144 +
export constant INSTR_LOAD_PTR: u8 = 26;
145 +
/// Wire tag for instruction StorePtr.
146 +
export constant INSTR_STORE_PTR: u8 = 27;
147 +
/// Wire tag for instruction PageSlice.
148 +
export constant INSTR_PAGE_SLICE: u8 = 28;
149 +
150 +
/// Wire tag for data Val.
151 +
export constant DATA_VAL: u8 = 0;
152 +
/// Wire tag for data Sym.
153 +
export constant DATA_SYM: u8 = 1;
154 +
/// Wire tag for data Fn.
155 +
export constant DATA_FN: u8 = 2;
156 +
/// Wire tag for data Str.
157 +
export constant DATA_STR: u8 = 3;
158 +
/// Wire tag for data Undef.
159 +
export constant DATA_UNDEF: u8 = 4;
160 +
161 +
/// Wire tag for binary operation Add.
162 +
export constant BIN_ADD: u8 = 0;
163 +
/// Wire tag for binary operation Sub.
164 +
export constant BIN_SUB: u8 = 1;
165 +
/// Wire tag for binary operation Mul.
166 +
export constant BIN_MUL: u8 = 2;
167 +
/// Wire tag for binary operation Sdiv.
168 +
export constant BIN_SDIV: u8 = 3;
169 +
/// Wire tag for binary operation Udiv.
170 +
export constant BIN_UDIV: u8 = 4;
171 +
/// Wire tag for binary operation Srem.
172 +
export constant BIN_SREM: u8 = 5;
173 +
/// Wire tag for binary operation Urem.
174 +
export constant BIN_UREM: u8 = 6;
175 +
/// Wire tag for binary operation Eq.
176 +
export constant BIN_EQ: u8 = 7;
177 +
/// Wire tag for binary operation Ne.
178 +
export constant BIN_NE: u8 = 8;
179 +
/// Wire tag for binary operation Slt.
180 +
export constant BIN_SLT: u8 = 9;
181 +
/// Wire tag for binary operation Sge.
182 +
export constant BIN_SGE: u8 = 10;
183 +
/// Wire tag for binary operation Ult.
184 +
export constant BIN_ULT: u8 = 11;
185 +
/// Wire tag for binary operation Uge.
186 +
export constant BIN_UGE: u8 = 12;
187 +
/// Wire tag for binary operation And.
188 +
export constant BIN_AND: u8 = 13;
189 +
/// Wire tag for binary operation Or.
190 +
export constant BIN_OR: u8 = 14;
191 +
/// Wire tag for binary operation Xor.
192 +
export constant BIN_XOR: u8 = 15;
193 +
/// Wire tag for binary operation Shl.
194 +
export constant BIN_SHL: u8 = 16;
195 +
/// Wire tag for binary operation Sshr.
196 +
export constant BIN_SSHR: u8 = 17;
197 +
/// Wire tag for binary operation Ushr.
198 +
export constant BIN_USHR: u8 = 18;
199 +
200 +
/// Wire tag for unary operation Neg.
201 +
export constant UN_NEG: u8 = 0;
202 +
/// Wire tag for unary operation Not.
203 +
export constant UN_NOT: u8 = 1;
204 +
205 +
/// Wire tag for comparison Eq.
206 +
export constant CMP_EQ: u8 = 0;
207 +
/// Wire tag for comparison Ne.
208 +
export constant CMP_NE: u8 = 1;
209 +
/// Wire tag for comparison Slt.
210 +
export constant CMP_SLT: u8 = 2;
211 +
/// Wire tag for comparison Ult.
212 +
export constant CMP_ULT: u8 = 3;
213 +
214 +
215 +
/// The kind of an exported package symbol.
216 +
export union ExportKind: Copy {
217 +
    /// A callable function entry.
218 +
    Function,
219 +
    /// A global data definition.
220 +
    Data,
221 +
}
222 +
223 +
/// A public symbol supplied by a package.
224 +
export record Export: Copy {
225 +
    /// Qualified symbol name.
226 +
    name: *[u8],
227 +
    /// Definition kind.
228 +
    kind: ExportKind,
229 +
}
230 +
231 +
/// A binary package and its IL definitions.
232 +
/// Referenced storage must remain valid and immutable while the package is read.
233 +
export record Package: Copy {
234 +
    /// Unique symbol names in wire-index order.
235 +
    symbols: *[*[u8]],
236 +
    /// Immutable package name.
237 +
    name: *[u8],
238 +
    /// Names of required packages.
239 +
    dependencies: *[*[u8]],
240 +
    /// Public definitions.
241 +
    exports: *[Export],
242 +
    /// Default exported entry, if the package has one.
243 +
    entry: ?*[u8],
244 +
    /// Package-local functions and global data.
245 +
    program: il::Program,
246 +
}
247 +
248 +
/// Structural limits supplied by the consumer of decoded IL.
249 +
export record Limits: Copy {
250 +
    /// Exclusive upper bound for SSA register numbers.
251 +
    registers: u32,
252 +
    /// Maximum number of blocks in one function.
253 +
    blocks: u32,
254 +
}
lib/std/lang/il/binary/cfg.rad added +255 -0
1 +
//! Control flow of decoded functions: predecessors and loop depth.
2 +
//!
3 +
//! The wire format carries neither. The decoder derives them from the
4 +
//! terminator of each block, so they always agree with the instructions.
5 +
6 +
use std::lang::alloc;
7 +
use std::lang::il;
8 +
use std::lang::il::binary;
9 +
10 +
/// Search state of a block in the depth-first walk.
11 +
union Visit: Copy {
12 +
    /// Not reached yet.
13 +
    Fresh,
14 +
    /// On the current path from the entry block.
15 +
    Open,
16 +
    /// Fully explored.
17 +
    Done,
18 +
}
19 +
20 +
/// Edges of a function in compressed form. The successors of block `b` are
21 +
/// `succs[succStart[b]..succStart[b + 1]]`, and its predecessors are
22 +
/// `preds[predStart[b]..predStart[b + 1]]`.
23 +
record Edges {
24 +
    /// Index in `succs` of the first successor of each block, and the edge count.
25 +
    succStart: *[u32],
26 +
    /// Targets of the edges, grouped by source block, without repeats.
27 +
    succs: *[u32],
28 +
    /// Index in `preds` of the first predecessor of each block, and the edge count.
29 +
    predStart: *[u32],
30 +
    /// Sources of the edges, grouped by target block in increasing order.
31 +
    preds: *[u32],
32 +
}
33 +
34 +
/// Append the targets of the terminator of `block` to `targets`. Repeated targets of
35 +
/// one terminator are kept once.
36 +
fn terminatorTargets(
37 +
    block: il::Block,
38 +
    cases: &[il::SwitchCase],
39 +
    first: u32,
40 +
    targets: &mut *mut [u32],
41 +
    a: &mut opaque alloc::Allocator
42 +
) throws (binary::Error) {
43 +
    if block.instrs.len == 0 {
44 +
        return;
45 +
    }
46 +
    match block.instrs[block.instrs.len - 1] {
47 +
        case il::Instr::Jmp { target, .. } => {
48 +
            try addTarget(targets, first, target, a);
49 +
        },
50 +
        case il::Instr::Br { thenTarget, elseTarget, .. } => {
51 +
            try addTarget(targets, first, thenTarget, a);
52 +
            try addTarget(targets, first, elseTarget, a);
53 +
        },
54 +
        case il::Instr::Switch { defaultTarget, cases: range, .. } => {
55 +
            try addTarget(targets, first, defaultTarget, a);
56 +
            for branch in &cases[range.start..range.start + range.len] {
57 +
                try addTarget(targets, first, branch.target, a);
58 +
            }
59 +
        },
60 +
        else => {},
61 +
    }
62 +
}
63 +
64 +
/// Append `target` to `targets` unless the terminator that starts at `first` has it already.
65 +
fn addTarget(targets: &mut *mut [u32], first: u32, target: u32, a: &mut opaque alloc::Allocator)
66 +
    throws (binary::Error)
67 +
{
68 +
    for i in first..targets.len {
69 +
        if targets[i] == target {
70 +
            return;
71 +
        }
72 +
    }
73 +
    try targets.append(target, a) catch {
74 +
        throw binary::Error::Storage;
75 +
    };
76 +
}
77 +
78 +
/// Build the edge lists of the blocks from their terminators. Targets must be in range.
79 +
fn collect(blocks: &[il::Block], cases: &[il::SwitchCase], a: &mut opaque alloc::Allocator)
80 +
    -> Edges throws (binary::Error)
81 +
{
82 +
    let n = blocks.len;
83 +
    let mut succStart: *mut [u32] = &mut [];
84 +
    let mut succs: *mut [u32] = &mut [];
85 +
    for b in 0..n {
86 +
        let first = succs.len;
87 +
        try succStart.append(first, a) catch {
88 +
            throw binary::Error::Storage;
89 +
        };
90 +
        try terminatorTargets(blocks[b], cases, first, &mut succs, a);
91 +
    }
92 +
    let total = succs.len;
93 +
    try succStart.append(total, a) catch {
94 +
        throw binary::Error::Storage;
95 +
    };
96 +
    // Count the predecessors of each block, then place the sources in increasing order.
97 +
    let mut predStart: *mut [u32] = &mut [];
98 +
    try predStart.resize(n + 1, 0, a) catch {
99 +
        throw binary::Error::Storage;
100 +
    };
101 +
    for i in 0..total {
102 +
        set predStart[succs[i] + 1] += 1;
103 +
    }
104 +
    for b in 0..n {
105 +
        set predStart[b + 1] += predStart[b];
106 +
    }
107 +
    let mut next: *mut [u32] = &mut [];
108 +
    try next.resize(n, 0, a) catch {
109 +
        throw binary::Error::Storage;
110 +
    };
111 +
    let mut preds: *mut [u32] = &mut [];
112 +
    try preds.resize(total, 0, a) catch {
113 +
        throw binary::Error::Storage;
114 +
    };
115 +
    for b in 0..n {
116 +
        for i in succStart[b]..succStart[b + 1] {
117 +
            let target = succs[i];
118 +
            set preds[predStart[target] + next[target]] = b;
119 +
            set next[target] += 1;
120 +
        }
121 +
    }
122 +
    return Edges {
123 +
        succStart: &succStart[..], succs: &succs[..], predStart: &predStart[..], preds: &preds[..],
124 +
    };
125 +
}
126 +
127 +
/// Find the back edges of the graph reachable from the entry block. An edge is a back
128 +
/// edge when its target is on the current path. Return the sources in `from` and
129 +
/// the targets, the loop headers, in `to`.
130 +
fn backEdges(
131 +
    edges: &Edges,
132 +
    from: &mut *mut [u32],
133 +
    to: &mut *mut [u32],
134 +
    a: &mut opaque alloc::Allocator
135 +
) throws (binary::Error) {
136 +
    let n = edges.predStart.len - 1;
137 +
    if n == 0 {
138 +
        return;
139 +
    }
140 +
    let mut visit: *mut [Visit] = &mut [];
141 +
    let mut next: *mut [u32] = &mut [];
142 +
    let mut path: *mut [u32] = &mut [];
143 +
    try visit.resize(n, Visit::Fresh, a) catch {
144 +
        throw binary::Error::Storage;
145 +
    };
146 +
    try next.resize(n, 0, a) catch {
147 +
        throw binary::Error::Storage;
148 +
    };
149 +
    try path.append(0, a) catch {
150 +
        throw binary::Error::Storage;
151 +
    };
152 +
    set visit[0] = Visit::Open;
153 +
    while path.len > 0 {
154 +
        let b = path[path.len - 1];
155 +
        let at = edges.succStart[b] + next[b];
156 +
        if at == edges.succStart[b + 1] {
157 +
            set visit[b] = Visit::Done;
158 +
            path.delete(path.len - 1);
159 +
            continue;
160 +
        }
161 +
        let target = edges.succs[at];
162 +
        set next[b] += 1;
163 +
        match visit[target] {
164 +
            case Visit::Fresh => {
165 +
                set visit[target] = Visit::Open;
166 +
                try path.append(target, a) catch {
167 +
                    throw binary::Error::Storage;
168 +
                };
169 +
            },
170 +
            case Visit::Open => {
171 +
                try from.append(b, a) catch {
172 +
                    throw binary::Error::Storage;
173 +
                };
174 +
                try to.append(target, a) catch {
175 +
                    throw binary::Error::Storage;
176 +
                };
177 +
            },
178 +
            case Visit::Done => {},
179 +
        }
180 +
    }
181 +
}
182 +
183 +
/// Count, for each block, the natural loops that contain it. The loop of a header
184 +
/// is the header and every block that reaches a back edge source without passing
185 +
/// the header. Loops that share a header count once.
186 +
fn loopDepths(edges: &Edges, a: &mut opaque alloc::Allocator) -> *mut [u32] throws (binary::Error) {
187 +
    let n = edges.predStart.len - 1;
188 +
    let mut from: *mut [u32] = &mut [];
189 +
    let mut to: *mut [u32] = &mut [];
190 +
    try backEdges(edges, &mut from, &mut to, a);
191 +
192 +
    let mut depth: *mut [u32] = &mut [];
193 +
    let mut mark: *mut [u32] = &mut [];
194 +
    let mut counted: *mut [bool] = &mut [];
195 +
    let mut work: *mut [u32] = &mut [];
196 +
    try depth.resize(n, 0, a) catch {
197 +
        throw binary::Error::Storage;
198 +
    };
199 +
    try mark.resize(n, 0, a) catch {
200 +
        throw binary::Error::Storage;
201 +
    };
202 +
    try counted.resize(n, false, a) catch {
203 +
        throw binary::Error::Storage;
204 +
    };
205 +
    for i in 0..to.len {
206 +
        let header = to[i];
207 +
        if counted[header] {
208 +
            continue;
209 +
        }
210 +
        set counted[header] = true;
211 +
        // Blocks of this loop carry the stamp, which differs for every header.
212 +
        let stamp = header + 1;
213 +
        set mark[header] = stamp;
214 +
        for j in i..to.len {
215 +
            if to[j] == header {
216 +
                try work.append(from[j], a) catch {
217 +
                    throw binary::Error::Storage;
218 +
                };
219 +
            }
220 +
        }
221 +
        while work.len > 0 {
222 +
            let b = work[work.len - 1];
223 +
            work.delete(work.len - 1);
224 +
            if mark[b] == stamp {
225 +
                continue;
226 +
            }
227 +
            set mark[b] = stamp;
228 +
            for k in edges.predStart[b]..edges.predStart[b + 1] {
229 +
                try work.append(edges.preds[k], a) catch {
230 +
                    throw binary::Error::Storage;
231 +
                };
232 +
            }
233 +
        }
234 +
        for b in 0..n {
235 +
            if mark[b] == stamp {
236 +
                set depth[b] += 1;
237 +
            }
238 +
        }
239 +
    }
240 +
    return depth;
241 +
}
242 +
243 +
/// Set the predecessors and the loop depth of every block of a function from
244 +
/// its terminators. `cases` is the case pool the switch terminators index.
245 +
/// Every branch target must be a block of the function.
246 +
export fn link(blocks: &mut [il::Block], cases: &[il::SwitchCase], a: &mut opaque alloc::Allocator)
247 +
    throws (binary::Error)
248 +
{
249 +
    let edges = try collect(blocks, cases, a);
250 +
    let depth = try loopDepths(&edges, a);
251 +
    for b in 0..blocks.len {
252 +
        set blocks[b].preds = &edges.preds[edges.predStart[b]..edges.predStart[b + 1]];
253 +
        set blocks[b].loopDepth = depth[b];
254 +
    }
255 +
}
lib/std/lang/il/binary/collect.rad added +270 -0
1 +
//! Deterministic symbol and dependency collection for package-local IL.
2 +
3 +
use std::mem;
4 +
use std::lang::il;
5 +
use std::lang::il::binary;
6 +
use std::lang::alloc;
7 +
use std::collections::dict;
8 +
9 +
/// Number of slots of the name index. It must be a power of two. The index
10 +
/// takes at most half of its slots, which bounds the names of one package.
11 +
export constant INDEX_SLOTS: u32 = 32768;
12 +
13 +
/// Storage for a package's indexed names. Tables grow in the allocator that
14 +
/// each operation receives, so one allocator must serve a whole collection.
15 +
/// Names are copied into that allocator, so callers may release their own
16 +
/// storage after each call.
17 +
export record Names {
18 +
    /// Unique names in first-use order.
19 +
    symbols: *mut [*[u8]],
20 +
    /// Unique dependency names in first-use order.
21 +
    dependencies: *mut [*[u8]],
22 +
    /// Maps each name to its position in `symbols`.
23 +
    index: dict::Dict,
24 +
}
25 +
26 +
/// Start collection with empty tables.
27 +
/// `slots` is the number of index slots, a power of two of at least two.
28 +
export fn new(slots: u32, a: &mut opaque alloc::Allocator) -> Names throws (binary::Error) {
29 +
    let mut table: *mut [dict::Entry] = &mut [];
30 +
    try table.resize(slots, dict::Entry { key: &[], value: 0 }, a) catch {
31 +
        throw binary::Error::Storage;
32 +
    };
33 +
    return Names { symbols: &mut [], dependencies: &mut [], index: dict::fresh(table) };
34 +
}
35 +
36 +
/// Add a unique name and return the stored copy.
37 +
fn add(names: &mut Names, name: *[u8], a: &mut opaque alloc::Allocator) -> *[u8]
38 +
    throws (binary::Error) {
39 +
    if name.len == 0 {
40 +
        throw binary::Error::Invalid;
41 +
    }
42 +
    if let i = dict::get(&names.index, name) {
43 +
        return names.symbols[i as u32];
44 +
    }
45 +
    let copy = try alloc::concatBytes(a, &[name]) catch {
46 +
        throw binary::Error::Storage;
47 +
    };
48 +
    let stored: *[u8] = &copy[..];
49 +
    try dict::insert(&mut names.index, stored, names.symbols.len as i32) catch {
50 +
        throw binary::Error::Storage;
51 +
    };
52 +
    try names.symbols.append(stored, a) catch {
53 +
        throw binary::Error::Storage;
54 +
    };
55 +
    return stored;
56 +
}
57 +
58 +
/// Add a qualified reference and its owning package dependency.
59 +
fn reference(
60 +
    names: &mut Names,
61 +
    owner: *[u8],
62 +
    name: *[u8],
63 +
    a: &mut opaque alloc::Allocator
64 +
) throws (binary::Error) {
65 +
    let stored = try add(names, name, a);
66 +
    for i in 0..stored.len {
67 +
        if stored[i] == ':' and i + 1 < stored.len and stored[i + 1] == ':' {
68 +
            if i == 0 or i + 2 == stored.len {
69 +
                throw binary::Error::Invalid;
70 +
            }
71 +
            let dependency = &stored[..i];
72 +
            if mem::eq(dependency, owner) {
73 +
                return;
74 +
            }
75 +
            let known = try add(names, dependency, a);
76 +
            for other in &names.dependencies[..] {
77 +
                if mem::eq(other, known) {
78 +
                    return;
79 +
                }
80 +
            }
81 +
            try names.dependencies.append(known, a) catch {
82 +
                throw binary::Error::Storage;
83 +
            };
84 +
            return;
85 +
        }
86 +
    }
87 +
    throw binary::Error::Invalid;
88 +
}
89 +
90 +
/// Collect names referenced by one IL value.
91 +
fn value(names: &mut Names, owner: *[u8], item: il::Val, a: &mut opaque alloc::Allocator)
92 +
    throws (binary::Error) {
93 +
    match item {
94 +
        case il::Val::DataSym(name) => try reference(names, owner, name, a),
95 +
        case il::Val::FnAddr(name) => try reference(names, owner, name, a),
96 +
        else => {},
97 +
    }
98 +
}
99 +
100 +
/// Collect names from an instruction's variable-length operand sequence.
101 +
fn values(names: &mut Names, owner: *[u8], items: *[il::Val], a: &mut opaque alloc::Allocator)
102 +
    throws (binary::Error) {
103 +
    for item in items {
104 +
        try value(names, owner, item, a);
105 +
    }
106 +
}
107 +
108 +
/// Collect all symbolic instruction operands of `func`.
109 +
fn instruction(
110 +
    names: &mut Names,
111 +
    owner: *[u8],
112 +
    func: &il::Fn,
113 +
    instr: il::Instr,
114 +
    a: &mut opaque alloc::Allocator
115 +
) throws (binary::Error) {
116 +
    match instr {
117 +
        case il::Instr::Store { src, .. } => try value(names, owner, src, a),
118 +
        case il::Instr::Copy { val, .. } => try value(names, owner, val, a),
119 +
        case il::Instr::Zext { val, .. } => try value(names, owner, val, a),
120 +
        case il::Instr::Sext { val, .. } => try value(names, owner, val, a),
121 +
        case il::Instr::BinOp { a: left, b: right, .. } => {
122 +
            try value(names, owner, left, a);
123 +
            try value(names, owner, right, a);
124 +
        },
125 +
        case il::Instr::UnOp { a: operand, .. } => try value(names, owner, operand, a),
126 +
        case il::Instr::Call { func: callee, args, .. } => {
127 +
            try value(names, owner, callee, a);
128 +
            try values(names, owner, il::argVals(func, args), a);
129 +
        },
130 +
        case il::Instr::Ret { val } => {
131 +
            if let item = val {
132 +
                try value(names, owner, item, a);
133 +
            }
134 +
        },
135 +
        case il::Instr::Jmp { args, .. } => try values(names, owner, il::argVals(func, args), a),
136 +
        case il::Instr::Br { a: left, b: right, thenArgs, elseArgs, .. } => {
137 +
            try value(names, owner, left, a);
138 +
            try value(names, owner, right, a);
139 +
            try values(names, owner, il::argVals(func, thenArgs), a);
140 +
            try values(names, owner, il::argVals(func, elseArgs), a);
141 +
        },
142 +
        case il::Instr::Switch { val, defaultArgs, cases, .. } => {
143 +
            try value(names, owner, val, a);
144 +
            try values(names, owner, il::argVals(func, defaultArgs), a);
145 +
            for branch in il::switchCases(func, cases) {
146 +
                try values(names, owner, il::argVals(func, branch.args), a);
147 +
            }
148 +
        },
149 +
        case il::Instr::Ecall { num, a0, a1, a2, a3, .. } => {
150 +
            try value(names, owner, num, a);
151 +
            try value(names, owner, a0, a);
152 +
            try value(names, owner, a1, a);
153 +
            try value(names, owner, a2, a);
154 +
            try value(names, owner, a3, a);
155 +
        },
156 +
        case il::Instr::PageSlice { handle, .. } => try value(names, owner, handle, a),
157 +
        case il::Instr::DeviceRead { handle, offset, .. } => {
158 +
            try value(names, owner, handle, a);
159 +
            try value(names, owner, offset, a);
160 +
        },
161 +
        case il::Instr::DeviceWrite { handle, offset, value: source, .. } => {
162 +
            try value(names, owner, handle, a);
163 +
            try value(names, owner, offset, a);
164 +
            try value(names, owner, source, a);
165 +
        },
166 +
        case il::Instr::SliceNew { base, start, end, .. } => {
167 +
            try value(names, owner, base, a);
168 +
            try value(names, owner, start, a);
169 +
            try value(names, owner, end, a);
170 +
        },
171 +
        case il::Instr::SliceSub { start, end, .. } => {
172 +
            try value(names, owner, start, a);
173 +
            try value(names, owner, end, a);
174 +
        },
175 +
        case il::Instr::Elem { index, .. } => try value(names, owner, index, a),
176 +
        case il::Instr::SliceElem { index, .. } => try value(names, owner, index, a),
177 +
        case il::Instr::StorePtr { src, .. } => try value(names, owner, src, a),
178 +
        case il::Instr::Field { .. }, il::Instr::LoadPtr { .. },
179 +
             il::Instr::Reserve { .. }, il::Instr::Blit { .. },
180 +
             il::Instr::Load { .. }, il::Instr::Sload { .. }, il::Instr::Unreachable,
181 +
             il::Instr::Ebreak, il::Instr::MemoryFence => {},
182 +
    }
183 +
}
184 +
185 +
/// Check that a definition belongs to the selected package.
186 +
fn definition(names: &mut Names, owner: *[u8], name: *[u8], a: &mut opaque alloc::Allocator)
187 +
    -> *[u8] throws (binary::Error) {
188 +
    let rest = mem::stripPrefix(owner, name) else {
189 +
        throw binary::Error::Invalid;
190 +
    };
191 +
    if rest.len < 3 or rest[0] <> ':' or rest[1] <> ':' {
192 +
        throw binary::Error::Invalid;
193 +
    }
194 +
    return try add(names, name, a);
195 +
}
196 +
197 +
/// Add the package name to the tables. Call it before the first [`function`].
198 +
export fn start(names: &mut Names, owner: *[u8], a: &mut opaque alloc::Allocator)
199 +
    throws (binary::Error) {
200 +
    try add(names, owner, a);
201 +
}
202 +
203 +
/// Collect the definition and the references of one function. Return the
204 +
/// stored copy of its name. Nothing in `func` is retained, so the caller may
205 +
/// release the function afterward.
206 +
export fn function(
207 +
    names: &mut Names,
208 +
    owner: *[u8],
209 +
    func: *il::Fn,
210 +
    a: &mut opaque alloc::Allocator
211 +
) -> *[u8] throws (binary::Error) {
212 +
    let stored = try definition(names, owner, func.name, a);
213 +
    for block in func.blocks {
214 +
        for instr in block.instrs {
215 +
            try instruction(names, owner, func, instr, a);
216 +
        }
217 +
    }
218 +
    return stored;
219 +
}
220 +
221 +
/// Build package tables from local definitions and their qualified references.
222 +
/// The tables move out of `names`, but its name index stays valid.
223 +
/// The functions of `program` must have been collected with [`function`].
224 +
export fn finish(
225 +
    names: &mut Names,
226 +
    owner: *[u8],
227 +
    program: il::Program,
228 +
    exports: *[binary::Export],
229 +
    entry: ?*[u8],
230 +
    a: &mut opaque alloc::Allocator
231 +
) -> binary::Package throws (binary::Error) {
232 +
    for item in exports {
233 +
        try definition(names, owner, item.name, a);
234 +
    }
235 +
    if let name = entry {
236 +
        try definition(names, owner, name, a);
237 +
    }
238 +
    for item in program.data {
239 +
        try definition(names, owner, item.name, a);
240 +
        for data in item.values {
241 +
            match data.item {
242 +
                case il::DataItem::Sym(name) => try reference(names, owner, name, a),
243 +
                case il::DataItem::Fn(name) => try reference(names, owner, name, a),
244 +
                else => {},
245 +
            }
246 +
        }
247 +
    }
248 +
    let symbols = names.symbols.take();
249 +
    let dependencies = names.dependencies.take();
250 +
    return binary::Package {
251 +
        symbols: &symbols[..], name: owner, dependencies: &dependencies[..], exports, entry,
252 +
            program,
253 +
    };
254 +
}
255 +
256 +
/// Build package tables for a complete program.
257 +
export fn package(
258 +
    names: &mut Names,
259 +
    owner: *[u8],
260 +
    program: il::Program,
261 +
    exports: *[binary::Export],
262 +
    entry: ?*[u8],
263 +
    a: &mut opaque alloc::Allocator
264 +
) -> binary::Package throws (binary::Error) {
265 +
    try start(names, owner, a);
266 +
    for func in program.fns {
267 +
        try function(names, owner, func, a);
268 +
    }
269 +
    return try finish(names, owner, program, exports, entry, a);
270 +
}
lib/std/lang/il/binary/program.rad added +526 -0
1 +
//! Binary package envelopes and IL program reconstruction.
2 +
//! Tables occur in this order: symbols, dependencies, exports, layouts, signatures,
3 +
//! data, functions. Block records carry labels, parameters, and instructions. The
4 +
//! decoder derives predecessors and loop depth from the terminators.
5 +
//! A label is its base text followed by an optional decimal suffix.
6 +
7 +
use std::lang::il;
8 +
use std::lang::il::binary;
9 +
use std::lang::il::binary::reader;
10 +
use std::lang::il::binary::writer;
11 +
use std::lang::il::binary::cfg;
12 +
use std::lang::alloc;
13 +
use std::mem;
14 +
use std::collections::dict;
15 +
16 +
/// Write a counted sequence of typed SSA parameters.
17 +
fn writeParams(out: &mut writer::Writer, params: &[il::Param]) throws (binary::Error) {
18 +
    try writer::integer(out, params.len as u64, 4);
19 +
    for param in params {
20 +
        try writer::integer(out, param.value.n as u64, 4);
21 +
        try writer::valType(out, param.type);
22 +
    }
23 +
}
24 +
25 +
/// Write the package tables through `out`, up to and including the function count.
26 +
fn writeHead(out: &mut writer::Writer, package: &binary::Package, fnCount: u32)
27 +
    throws (binary::Error) {
28 +
    try writer::integer(out, binary::MAGIC as u64, 4);
29 +
    try writer::integer(out, package.symbols.len as u64, 4);
30 +
    for name in package.symbols {
31 +
        try writer::bytes(out, name);
32 +
    }
33 +
    try writer::symbol(out, package.name);
34 +
    try writer::integer(out, package.dependencies.len as u64, 4);
35 +
    for name in package.dependencies {
36 +
        try writer::symbol(out, name);
37 +
    }
38 +
    try writer::integer(out, package.exports.len as u64, 4);
39 +
    for item in package.exports {
40 +
        try writer::symbol(out, item.name);
41 +
        match item.kind {
42 +
            case binary::ExportKind::Function => try writer::integer(out, 0, 1),
43 +
            case binary::ExportKind::Data => try writer::integer(out, 1, 1),
44 +
        }
45 +
    }
46 +
    if let entry = package.entry {
47 +
        try writer::integer(out, 1, 1);
48 +
        try writer::symbol(out, entry);
49 +
    } else {
50 +
        try writer::integer(out, 0, 1);
51 +
    }
52 +
    try writer::integer(out, package.program.layouts.len as u64, 4);
53 +
    try writer::integer(out, package.program.signatures.len as u64, 4);
54 +
    for item in package.program.layouts {
55 +
        try writer::layout(out, item);
56 +
    }
57 +
    for item in package.program.signatures {
58 +
        try writer::signature(out, item);
59 +
    }
60 +
    try writer::integer(out, package.program.data.len as u64, 4);
61 +
    for item in package.program.data {
62 +
        try writer::symbol(out, item.name);
63 +
        try writer::integer(out, item.layout as u64, 4);
64 +
        try writer::integer(out, 1 if item.readOnly else 0, 1);
65 +
        try writer::integer(out, 1 if item.isZeroInit else 0, 1);
66 +
        try writer::integer(out, item.values.len as u64, 4);
67 +
        for value in item.values {
68 +
            try writer::dataValue(out, value);
69 +
        }
70 +
    }
71 +
    try writer::integer(out, fnCount as u64, 4);
72 +
}
73 +
74 +
/// Write one function: name, signature, and blocks.
75 +
fn writeFunction(out: &mut writer::Writer, func: *il::Fn) throws (binary::Error) {
76 +
    try writer::symbol(out, func.name);
77 +
    try writer::valType(out, func.returnType);
78 +
    try writer::integer(out, 1 if func.isExtern else 0, 1);
79 +
    try writeParams(out, func.params);
80 +
    try writer::integer(out, func.blocks.len as u64, 4);
81 +
    for block in func.blocks {
82 +
        try writer::bytes(out, block.label.base);
83 +
        if let suffix = block.label.suffix {
84 +
            try writer::integer(out, 1, 1);
85 +
            try writer::integer(out, suffix as u64, 4);
86 +
        } else {
87 +
            try writer::integer(out, 0, 1);
88 +
        }
89 +
        try writeParams(out, block.params);
90 +
        try writer::integer(out, block.instrs.len as u64, 4);
91 +
        for instr in block.instrs {
92 +
            try writer::instr(out, func, instr);
93 +
        }
94 +
    }
95 +
}
96 +
97 +
/// Write the package tables and functions through `out`. Return the number of encoded bytes.
98 +
fn writePackage(out: &mut writer::Writer, package: &binary::Package) -> u32 throws (binary::Error) {
99 +
    try writeHead(out, package, package.program.fns.len);
100 +
    for func in package.program.fns {
101 +
        try writeFunction(out, func);
102 +
    }
103 +
    return out.offset;
104 +
}
105 +
106 +
/// Move the buffer and index of `out` back to their owners.
107 +
fn release(out: &mut writer::Writer, bytes: &mut *mut [u8], index: &mut dict::Dict) {
108 +
    set *bytes = out.bytes.take();
109 +
    set *index = dict::take(&mut out.index);
110 +
}
111 +
112 +
/// Write a package into `bytes`. Return the number of encoded bytes. The
113 +
/// buffer is lent to the writer for the call and is back in `bytes` after it,
114 +
/// also on failure.
115 +
export fn encode(bytes: &mut *mut [u8], package: &binary::Package) -> u32 throws (binary::Error) {
116 +
    let mut out = writer::new(bytes.take(), package.symbols);
117 +
    let length = try writePackage(&mut out, package) catch err {
118 +
        set *bytes = out.bytes.take();
119 +
        throw err;
120 +
    };
121 +
    set *bytes = out.bytes.take();
122 +
    return length;
123 +
}
124 +
125 +
/// Write the package tables and the function count into `bytes`. The functions
126 +
/// follow in the file, encoded with [`appendFunction`]. `index` maps each name
127 +
/// of the package symbol table to its wire index. Return the number of bytes.
128 +
/// The buffer and the index are lent to the writer for the call and are back
129 +
/// after it, also on failure.
130 +
export fn encodeHead(bytes: &mut *mut [u8], package: &binary::Package, index: &mut dict::Dict,
131 +
    fnCount: u32)
132 +
    -> u32 throws (binary::Error)
133 +
{
134 +
    let mut out = writer::withIndex(bytes.take(), 0, dict::take(index));
135 +
    try writeHead(&mut out, package, fnCount) catch err {
136 +
        release(&mut out, bytes, index);
137 +
        throw err;
138 +
    };
139 +
    let length = out.offset;
140 +
    release(&mut out, bytes, index);
141 +
    return length;
142 +
}
143 +
144 +
/// Encode `func` into `bytes` at `used`. Return the new used length. `index`
145 +
/// must map every name that `func` references to its wire index. The buffer and
146 +
/// the index are lent to the writer for the call and are back after it, also on
147 +
/// failure.
148 +
export fn appendFunction(bytes: &mut *mut [u8], used: u32, index: &mut dict::Dict, func: *il::Fn)
149 +
    -> u32 throws (binary::Error)
150 +
{
151 +
    let mut out = writer::withIndex(bytes.take(), used, dict::take(index));
152 +
    try writeFunction(&mut out, func) catch err {
153 +
        release(&mut out, bytes, index);
154 +
        throw err;
155 +
    };
156 +
    let end = out.offset;
157 +
    release(&mut out, bytes, index);
158 +
    return end;
159 +
}
160 +
161 +
/// Read typed SSA parameters with checked register indices.
162 +
fn readParams(input: &mut reader::Reader, wire: &[u8], a: &mut opaque alloc::Allocator)
163 +
    -> *[il::Param] throws (binary::Error)
164 +
{
165 +
    let n = try reader::count(input, wire, binary::MIN_PARAM_BYTES);
166 +
    let mut params: *mut [il::Param] = &mut [];
167 +
    try params.reserve(n, a) catch {
168 +
        throw binary::Error::Storage;
169 +
    };
170 +
    for _ in 0..n {
171 +
        let value = try reader::reg(input, wire);
172 +
        let t = try reader::valType(input, wire);
173 +
        try params.append(il::Param { value, type: t }, a) catch {
174 +
            throw binary::Error::Storage;
175 +
        };
176 +
    }
177 +
    return &params[..];
178 +
}
179 +
180 +
/// Read the layout table. Each layout is checked for structure: a power-of-two
181 +
/// alignment, a size that is a multiple of it, slots that are sorted, aligned, disjoint and
182 +
/// inside the size, and nested layouts that precede the layout that contains them.
183 +
fn readLayouts(input: &mut reader::Reader, wire: &[u8], a: &mut opaque alloc::Allocator)
184 +
    -> *[il::Layout] throws (binary::Error)
185 +
{
186 +
    let mut items: *mut [il::Layout] = &mut [];
187 +
    try items.reserve(input.layouts, a) catch {
188 +
        throw binary::Error::Storage;
189 +
    };
190 +
    for i in 0..input.layouts {
191 +
        let size = try reader::integer(input, wire, 4) as u32;
192 +
        let alignment = try reader::integer(input, wire, 4) as u32;
193 +
        if alignment == 0 or (alignment & (alignment - 1)) <> 0 or size % alignment <> 0 {
194 +
            throw binary::Error::Invalid;
195 +
        }
196 +
        let slotCount = try reader::count(input, wire, binary::MIN_SLOT_BYTES);
197 +
        let mut slots: *mut [il::Slot] = &mut [];
198 +
        try slots.reserve(slotCount, a) catch {
199 +
            throw binary::Error::Storage;
200 +
        };
201 +
        // End of the previous slot.
202 +
        let mut end: u64 = 0;
203 +
        for _ in 0..slotCount {
204 +
            let offset = try reader::integer(input, wire, 4) as u32;
205 +
            let tag = try reader::integer(input, wire, 1) as u8;
206 +
            let mut kind = il::SlotKind::Fn(0);
207 +
            let mut width: u64 = 8;
208 +
            let mut slotAlignment: u32 = 8;
209 +
            match tag {
210 +
                case binary::SLOT_PTR => {
211 +
                    let ptr = try reader::ptrType(input, wire);
212 +
                    if not ptr.nullable {
213 +
                        throw binary::Error::Invalid;
214 +
                    }
215 +
                    set kind = il::SlotKind::Ptr(ptr);
216 +
                },
217 +
                case binary::SLOT_SLICE => {
218 +
                    let ptr = try reader::ptrType(input, wire);
219 +
                    if not ptr.nullable {
220 +
                        throw binary::Error::Invalid;
221 +
                    }
222 +
                    set kind = il::SlotKind::Slice(ptr);
223 +
                    set width = 16;
224 +
                },
225 +
                case binary::SLOT_FN => {
226 +
                    set kind = il::SlotKind::Fn(try reader::signatureIndex(input, wire));
227 +
                },
228 +
                case binary::SLOT_SUB => {
229 +
                    let sub = try reader::integer(input, wire, 4) as u32;
230 +
                    if sub >= i {
231 +
                        throw binary::Error::Invalid;
232 +
                    }
233 +
                    set kind = il::SlotKind::Sub(sub);
234 +
                    set width = items[sub].size as u64;
235 +
                    set slotAlignment = items[sub].alignment;
236 +
                },
237 +
                case binary::SLOT_ARRAY => {
238 +
                    let element = try reader::integer(input, wire, 4) as u32;
239 +
                    let count = try reader::integer(input, wire, 4) as u32;
240 +
                    if element >= i {
241 +
                        throw binary::Error::Invalid;
242 +
                    }
243 +
                    set kind = il::SlotKind::Array { layout: element, count };
244 +
                    set width = items[element].size as u64 * count as u64;
245 +
                    set slotAlignment = items[element].alignment;
246 +
                },
247 +
                else => throw binary::Error::Invalid,
248 +
            }
249 +
            if slotAlignment > alignment or offset % slotAlignment <> 0
250 +
                or (offset as u64) < end or offset as u64 + width > size as u64 {
251 +
                throw binary::Error::Invalid;
252 +
            }
253 +
            set end = offset as u64 + width;
254 +
            try slots.append(il::Slot { offset, kind }, a) catch {
255 +
                throw binary::Error::Storage;
256 +
            };
257 +
        }
258 +
        try items.append(il::Layout { size, alignment, slots: &slots[..] }, a) catch {
259 +
            throw binary::Error::Storage;
260 +
        };
261 +
    }
262 +
    return &items[..];
263 +
}
264 +
265 +
/// Read the signature table. Value types are checked against the table sizes.
266 +
fn readSignatures(input: &mut reader::Reader, wire: &[u8], a: &mut opaque alloc::Allocator)
267 +
    -> *[il::Signature] throws (binary::Error)
268 +
{
269 +
    let mut items: *mut [il::Signature] = &mut [];
270 +
    try items.reserve(input.signatures, a) catch {
271 +
        throw binary::Error::Storage;
272 +
    };
273 +
    for _ in 0..input.signatures {
274 +
        let paramCount = try reader::count(input, wire, 1);
275 +
        let mut params: *mut [il::ValType] = &mut [];
276 +
        try params.reserve(paramCount, a) catch {
277 +
            throw binary::Error::Storage;
278 +
        };
279 +
        for _ in 0..paramCount {
280 +
            let param = try reader::valType(input, wire);
281 +
            try params.append(param, a) catch {
282 +
                throw binary::Error::Storage;
283 +
            };
284 +
        }
285 +
        let ret = try reader::valType(input, wire);
286 +
        try items.append(il::Signature { params: &params[..], ret }, a) catch {
287 +
            throw binary::Error::Storage;
288 +
        };
289 +
    }
290 +
    return &items[..];
291 +
}
292 +
293 +
/// Read global data and check initializer extents against the layout storage.
294 +
fn readData(input: &mut reader::Reader, wire: &[u8], a: &mut opaque alloc::Allocator,
295 +
    layouts: &[il::Layout])
296 +
    -> *[il::Data] throws (binary::Error)
297 +
{
298 +
    let n = try reader::count(input, wire, binary::MIN_DATA_BYTES);
299 +
    let mut items: *mut [il::Data] = &mut [];
300 +
    try items.reserve(n, a) catch {
301 +
        throw binary::Error::Storage;
302 +
    };
303 +
    for _ in 0..n {
304 +
        let name = try reader::symbol(input, wire);
305 +
        let layout = try reader::layoutIndex(input, wire);
306 +
        let size = layouts[layout].size;
307 +
        let alignment = layouts[layout].alignment;
308 +
        let readOnly = try reader::flag(input, wire);
309 +
        let isZeroInit = try reader::flag(input, wire);
310 +
        let count = try reader::count(input, wire,
311 +
            binary::MIN_DATA_VALUE_BYTES);
312 +
        let mut values: *mut [il::DataValue] = &mut [];
313 +
        try values.reserve(count, a) catch {
314 +
            throw binary::Error::Storage;
315 +
        };
316 +
        let mut extent: u64 = 0;
317 +
        for _ in 0..count {
318 +
            let value = try reader::dataValue(input, wire, a);
319 +
            let mut width: u32 = 0;
320 +
            match value.item {
321 +
                case il::DataItem::Val { typ, .. } => {
322 +
                    set width = il::typeSize(typ);
323 +
                },
324 +
                case il::DataItem::Sym(_), il::DataItem::Fn(_) => {
325 +
                    set width = 8;
326 +
                },
327 +
                case il::DataItem::Str(text) => {
328 +
                    set width = text.len;
329 +
                },
330 +
                case il::DataItem::Undef => {
331 +
                    set width = 1;
332 +
                },
333 +
            }
334 +
            set extent += width as u64 * value.count as u64;
335 +
            if extent > size as u64 {
336 +
                throw binary::Error::Invalid;
337 +
            }
338 +
            try values.append(value, a) catch {
339 +
                throw binary::Error::Storage;
340 +
            };
341 +
        }
342 +
        try items.append(il::Data {
343 +
            name, size, alignment, layout, readOnly, isZeroInit, values: &values[..],
344 +
        }, a) catch {
345 +
            throw binary::Error::Storage;
346 +
        };
347 +
    }
348 +
    return &items[..];
349 +
}
350 +
351 +
/// Read one block with its label, parameters, and instructions.
352 +
/// Return whether the block contains a call.
353 +
fn readBlock(
354 +
    input: &mut reader::Reader, wire: &[u8],
355 +
    a: &mut opaque alloc::Allocator,
356 +
    blocks: &mut *mut [il::Block]
357 +
) -> bool throws (binary::Error) {
358 +
    let base = try reader::bytes(input, wire, a);
359 +
    let mut suffix: ?u32 = nil;
360 +
    if try reader::flag(input, wire) {
361 +
        set suffix = try reader::integer(input, wire, 4) as u32;
362 +
    }
363 +
    let params = try readParams(input, wire, a);
364 +
    let instrCount = try reader::count(input, wire, 1);
365 +
    let mut instrs: *mut [il::Instr] = &mut [];
366 +
    try instrs.reserve(instrCount, a) catch {
367 +
        throw binary::Error::Storage;
368 +
    };
369 +
    let mut hasCall = false;
370 +
    for _ in 0..instrCount {
371 +
        let instr = try reader::instr(input, wire, a);
372 +
        if il::isCall(instr) {
373 +
            set hasCall = true;
374 +
        }
375 +
        try instrs.append(instr, a) catch {
376 +
            throw binary::Error::Storage;
377 +
        };
378 +
    }
379 +
    try blocks.append(il::Block {
380 +
        label: il::BlockLabel { base, suffix }, params, instrs: &instrs[..], locs: &[],
381 +
        preds: &[], loopDepth: 0,
382 +
    }, a) catch {
383 +
        throw binary::Error::Storage;
384 +
    };
385 +
    return hasCall;
386 +
}
387 +
388 +
/// Read functions with checked block and register indices.
389 +
fn readFunctions(input: &mut reader::Reader, wire: &[u8], a: &mut opaque alloc::Allocator,
390 +
    limits: binary::Limits)
391 +
    -> *[*il::Fn] throws (binary::Error)
392 +
{
393 +
    let n = try reader::count(input, wire, binary::MIN_FUNCTION_BYTES);
394 +
    let mut fns: *mut [*il::Fn] = &mut [];
395 +
    try fns.reserve(n, a) catch {
396 +
        throw binary::Error::Storage;
397 +
    };
398 +
    set input.registers = limits.registers;
399 +
    for _ in 0..n {
400 +
        let name = try reader::symbol(input, wire);
401 +
        let returnType = try reader::valType(input, wire);
402 +
        let isExtern = try reader::flag(input, wire);
403 +
        let params = try readParams(input, wire, a);
404 +
        let count = try reader::count(input, wire, binary::MIN_BLOCK_BYTES);
405 +
        if count > limits.blocks or (isExtern and count <> 0) {
406 +
            throw binary::Error::Invalid;
407 +
        }
408 +
        set input.blocks = count;
409 +
        let mut blocks: *mut [il::Block] = &mut [];
410 +
        try blocks.reserve(count, a) catch {
411 +
            throw binary::Error::Storage;
412 +
        };
413 +
        let mut isLeaf = true;
414 +
        for _ in 0..count {
415 +
            if try readBlock(input, wire, a, &mut blocks) {
416 +
                set isLeaf = false;
417 +
            }
418 +
        }
419 +
        try cfg::link(&mut blocks[..], &input.cases[..], a);
420 +
        // The function takes the pools. The next function starts with empty ones.
421 +
        let vals = input.vals.take();
422 +
        let cases = input.cases.take();
423 +
        let mut funcs: *mut [il::Fn] = &mut [];
424 +
        try funcs.append(il::Fn {
425 +
            name, params, returnType, isExtern, isLeaf, blocks: &blocks[..],
426 +
            vals: &vals[..], cases: &cases[..],
427 +
        }, a) catch {
428 +
            throw binary::Error::Storage;
429 +
        };
430 +
        try fns.append(&funcs[0], a) catch {
431 +
            throw binary::Error::Storage;
432 +
        };
433 +
    }
434 +
    return &fns[..];
435 +
}
436 +
437 +
/// Read the package tables. The input must contain exactly one package.
438 +
fn readPackage(input: &mut reader::Reader, wire: &[u8], a: &mut opaque alloc::Allocator,
439 +
    limits: binary::Limits)
440 +
    -> binary::Package throws (binary::Error)
441 +
{
442 +
    let magic = try reader::integer(input, wire, 4) as u32;
443 +
    if magic <> binary::MAGIC {
444 +
        throw binary::Error::Invalid;
445 +
    }
446 +
    let symbolCount = try reader::count(input, wire, 4);
447 +
    try input.symbols.reserve(symbolCount, a) catch {
448 +
        throw binary::Error::Storage;
449 +
    };
450 +
    for i in 0..symbolCount {
451 +
        let name = try reader::bytes(input, wire, a);
452 +
        if name.len == 0 {
453 +
            throw binary::Error::Invalid;
454 +
        }
455 +
        for j in 0..i {
456 +
            if mem::eq(name, input.symbols[j]) {
457 +
                throw binary::Error::Invalid;
458 +
            }
459 +
        }
460 +
        try input.symbols.append(name, a) catch {
461 +
            throw binary::Error::Storage;
462 +
        };
463 +
    }
464 +
    let name = try reader::symbol(input, wire);
465 +
    let depCount = try reader::count(input, wire, 4);
466 +
    let mut dependencies: *mut [*[u8]] = &mut [];
467 +
    try dependencies.reserve(depCount, a) catch {
468 +
        throw binary::Error::Storage;
469 +
    };
470 +
    for _ in 0..depCount {
471 +
        let dependency = try reader::symbol(input, wire);
472 +
        try dependencies.append(dependency, a) catch {
473 +
            throw binary::Error::Storage;
474 +
        };
475 +
    }
476 +
    let exportCount = try reader::count(input, wire, binary::MIN_EXPORT_BYTES);
477 +
    let mut exports: *mut [binary::Export] = &mut [];
478 +
    try exports.reserve(exportCount, a) catch {
479 +
        throw binary::Error::Storage;
480 +
    };
481 +
    for _ in 0..exportCount {
482 +
        let symbol = try reader::symbol(input, wire);
483 +
        let kind = binary::ExportKind::Data if try reader::flag(input,
484 +
            wire) else binary::ExportKind::Function;
485 +
        try exports.append(binary::Export { name: symbol, kind }, a) catch {
486 +
            throw binary::Error::Storage;
487 +
        };
488 +
    }
489 +
    let mut entry: ?*[u8] = nil;
490 +
    if try reader::flag(input, wire) {
491 +
        set entry = try reader::symbol(input, wire);
492 +
    }
493 +
    set input.layouts = try reader::count(input, wire,
494 +
        binary::MIN_LAYOUT_BYTES);
495 +
    set input.signatures = try reader::count(input, wire,
496 +
        binary::MIN_SIGNATURE_BYTES);
497 +
    let layouts = try readLayouts(input, wire, a);
498 +
    let signatures = try readSignatures(input, wire, a);
499 +
    let data = try readData(input, wire, a, layouts);
500 +
    let fns = try readFunctions(input, wire, a, limits);
501 +
    if not reader::atEnd(input, wire) {
502 +
        throw binary::Error::Invalid;
503 +
    }
504 +
    let symbols = input.symbols.take();
505 +
    return binary::Package {
506 +
        symbols: &symbols[..], name, dependencies: &dependencies[..], exports: &exports[..], entry,
507 +
        program: il::Program { data, fns, layouts, signatures },
508 +
    };
509 +
}
510 +
511 +
/// Decode one package into storage from `arena`. Restore the arena offset on
512 +
/// every failure.
513 +
export fn decode(bytes: &[u8], arena: &mut alloc::Arena, limits: binary::Limits)
514 +
    -> binary::Package throws (binary::Error)
515 +
{
516 +
    let saved = alloc::save(arena);
517 +
    let mut input = reader::new();
518 +
    return try readPackage(&mut input, bytes, arena, limits) catch err {
519 +
        // A failed decode returns no pointer into the storage that it allocated, and the
520 +
        // exclusive borrow of `arena` leaves no other user of that range.
521 +
        unsafe {
522 +
            alloc::restore(arena, saved);
523 +
        }
524 +
        throw err;
525 +
    };
526 +
}
lib/std/lang/il/binary/reader.rad added +508 -0
1 +
//! Checked binary RIL decoding into caller-owned allocator storage.
2 +
//! Callers discard partial results and restore the allocator after a failed decode.
3 +
//! A cursor holds only offsets and pools. Every read takes the encoded bytes `wire`
4 +
//! of the package, which must be the same bytes for every read through one cursor.
5 +
6 +
use std::lang::il;
7 +
use std::lang::il::binary;
8 +
use std::lang::alloc;
9 +
10 +
/// Input cursor and the argument and case pools of the function being read.
11 +
export record Reader {
12 +
    /// Number of bytes consumed.
13 +
    offset: u32,
14 +
    /// Symbol names in wire-index order.
15 +
    symbols: *mut [*[u8]],
16 +
    /// Number of entries in the layout table of the package.
17 +
    layouts: u32,
18 +
    /// Number of entries in the signature table of the package.
19 +
    signatures: u32,
20 +
    /// Exclusive bound for SSA register numbers.
21 +
    registers: u32,
22 +
    /// Number of blocks in the current function.
23 +
    blocks: u32,
24 +
    /// Argument pool of the current function.
25 +
    vals: *mut [il::Val],
26 +
    /// Case pool of the current function.
27 +
    cases: *mut [il::SwitchCase],
28 +
}
29 +
30 +
/// Create a cursor at the start of the input. Set function bounds before reading instructions.
31 +
export fn new() -> Reader {
32 +
    return Reader {
33 +
        offset: 0, symbols: &mut [], layouts: 0, signatures: 0, registers: 0, blocks: 0,
34 +
        vals: &mut [], cases: &mut [],
35 +
    };
36 +
}
37 +
38 +
/// Whether the input is fully consumed.
39 +
export fn atEnd(input: &Reader, wire: &[u8]) -> bool {
40 +
    return input.offset == wire.len;
41 +
}
42 +
43 +
/// Read an unsigned integer with width 1, 2, 4, or 8.
44 +
export fn integer(input: &mut Reader, wire: &[u8], width: u32) -> u64 throws (binary::Error) {
45 +
    if width <> 1 and width <> 2 and width <> 4 and width <> 8 {
46 +
        throw binary::Error::Invalid;
47 +
    }
48 +
    if width > wire.len - input.offset {
49 +
        throw binary::Error::Truncated;
50 +
    }
51 +
    let at = input.offset;
52 +
    let mut result: u64 = 0;
53 +
    for i in 0..width {
54 +
        set result |= (wire[at + i] as u64) << (i * 8) as u64;
55 +
    }
56 +
    set input.offset += width;
57 +
    return result;
58 +
}
59 +
60 +
/// Read a zero-or-one optional-field or boolean marker.
61 +
export fn flag(input: &mut Reader, wire: &[u8]) -> bool throws (binary::Error) {
62 +
    let n = try integer(input, wire, 1);
63 +
    if n > 1 {
64 +
        throw binary::Error::Invalid;
65 +
    }
66 +
    return n == 1;
67 +
}
68 +
69 +
/// Read a count and check the minimum required input bytes before allocation.
70 +
export fn count(input: &mut Reader, wire: &[u8], minimum: u32) -> u32 throws (binary::Error) {
71 +
    assert minimum > 0;
72 +
    let n = try integer(input, wire, 4) as u32;
73 +
    if n > (wire.len - input.offset) / minimum {
74 +
        throw binary::Error::Truncated;
75 +
    }
76 +
    return n;
77 +
}
78 +
79 +
/// Read a length-prefixed byte string and copy it into storage from `a`.
80 +
export fn bytes(input: &mut Reader, wire: &[u8], a: &mut opaque alloc::Allocator) -> *[u8]
81 +
    throws (binary::Error) {
82 +
    let n = try count(input, wire, 1);
83 +
    let mut result: *mut [u8] = &mut [];
84 +
    try result.extend(&wire[input.offset..input.offset + n], a) catch {
85 +
        throw binary::Error::Storage;
86 +
    };
87 +
    set input.offset += n;
88 +
    return &result[..];
89 +
}
90 +
91 +
/// Resolve a checked symbol-table index.
92 +
export fn symbol(input: &mut Reader, wire: &[u8]) -> *[u8] throws (binary::Error) {
93 +
    let index = try integer(input, wire, 4) as u32;
94 +
    if index >= input.symbols.len {
95 +
        throw binary::Error::Symbol;
96 +
    }
97 +
    return input.symbols[index];
98 +
}
99 +
100 +
/// Read an IL type from its byte width.
101 +
export fn typ(input: &mut Reader, wire: &[u8]) -> il::Type throws (binary::Error) {
102 +
    let width = try integer(input, wire, 1);
103 +
    match width {
104 +
        case 1 => return il::Type::W8,
105 +
        case 2 => return il::Type::W16,
106 +
        case 4 => return il::Type::W32,
107 +
        case 8 => return il::Type::W64,
108 +
        else => throw binary::Error::Invalid,
109 +
    }
110 +
}
111 +
112 +
/// Read a layout table index within the package.
113 +
export fn layoutIndex(input: &mut Reader, wire: &[u8]) -> u32 throws (binary::Error) {
114 +
    let n = try integer(input, wire, 4) as u32;
115 +
    if n >= input.layouts {
116 +
        throw binary::Error::Invalid;
117 +
    }
118 +
    return n;
119 +
}
120 +
121 +
/// Read a signature table index within the package.
122 +
export fn signatureIndex(input: &mut Reader, wire: &[u8]) -> u32 throws (binary::Error) {
123 +
    let n = try integer(input, wire, 4) as u32;
124 +
    if n >= input.signatures {
125 +
        throw binary::Error::Invalid;
126 +
    }
127 +
    return n;
128 +
}
129 +
130 +
/// Read a pointer type with checked layout index and flags.
131 +
export fn ptrType(input: &mut Reader, wire: &[u8]) -> il::PtrType throws (binary::Error) {
132 +
    let layout = try layoutIndex(input, wire);
133 +
    let flags = try integer(input, wire, 1) as u8;
134 +
    if flags > (super::PTR_OWN | super::PTR_MUTABLE | super::PTR_NULLABLE) {
135 +
        throw binary::Error::Invalid;
136 +
    }
137 +
    return il::PtrType {
138 +
        layout,
139 +
        own: (flags & super::PTR_OWN) <> 0,
140 +
        mutable: (flags & super::PTR_MUTABLE) <> 0,
141 +
        nullable: (flags & super::PTR_NULLABLE) <> 0,
142 +
    };
143 +
}
144 +
145 +
/// Read a value type with checked layout and signature indices.
146 +
export fn valType(input: &mut Reader, wire: &[u8]) -> il::ValType throws (binary::Error) {
147 +
    let tag = try integer(input, wire, 1) as u8;
148 +
    match tag {
149 +
        case super::VALTYPE_VOID => return il::ValType::Void,
150 +
        case super::VALTYPE_DATA => return il::ValType::Data(try typ(input, wire)),
151 +
        case super::VALTYPE_PTR => return il::ValType::Ptr(try ptrType(input, wire)),
152 +
        case super::VALTYPE_FN => return il::ValType::Fn(try signatureIndex(input, wire)),
153 +
        else => throw binary::Error::Invalid,
154 +
    }
155 +
}
156 +
157 +
/// Read a register index within the current function's bound.
158 +
export fn reg(input: &mut Reader, wire: &[u8]) -> il::Reg throws (binary::Error) {
159 +
    let n = try integer(input, wire, 4) as u32;
160 +
    if n >= input.registers {
161 +
        throw binary::Error::Invalid;
162 +
    }
163 +
    return il::Reg { n };
164 +
}
165 +
166 +
/// Read a target block index within the current function.
167 +
export fn target(input: &mut Reader, wire: &[u8]) -> u32 throws (binary::Error) {
168 +
    let n = try integer(input, wire, 4) as u32;
169 +
    if n >= input.blocks {
170 +
        throw binary::Error::Invalid;
171 +
    }
172 +
    return n;
173 +
}
174 +
175 +
/// Read a tagged value with checked register and symbol indices.
176 +
export fn val(input: &mut Reader, wire: &[u8]) -> il::Val throws (binary::Error) {
177 +
    let tag = try integer(input, wire, 1) as u8;
178 +
    match tag {
179 +
        case super::VALUE_REG => return il::Val::Reg(try reg(input, wire)),
180 +
        case super::VALUE_IMM => return il::Val::Imm(try integer(input, wire, 8) as i64),
181 +
        case super::VALUE_DATASYM => return il::Val::DataSym(try symbol(input, wire)),
182 +
        case super::VALUE_FNADDR => return il::Val::FnAddr(try symbol(input, wire)),
183 +
        case super::VALUE_UNDEF => return il::Val::Undef,
184 +
        else => throw binary::Error::Invalid,
185 +
    }
186 +
}
187 +
188 +
/// Read a counted sequence of values into the argument pool of the current function.
189 +
export fn values(input: &mut Reader, wire: &[u8], a: &mut opaque alloc::Allocator) -> il::ArgRange
190 +
    throws (binary::Error) {
191 +
    let n = try count(input, wire, 1);
192 +
    let start = input.vals.len;
193 +
    try input.vals.reserve(n, a) catch {
194 +
        throw binary::Error::Storage;
195 +
    };
196 +
    for _ in 0..n {
197 +
        let item = try val(input, wire);
198 +
        try input.vals.append(item, a) catch {
199 +
            throw binary::Error::Storage;
200 +
        };
201 +
    }
202 +
    return il::ArgRange { start, len: n };
203 +
}
204 +
205 +
/// Read a checked bin operation tag.
206 +
fn binOp(input: &mut Reader, wire: &[u8]) -> il::BinOp throws (binary::Error) {
207 +
    let tag = try integer(input, wire, 1) as u8;
208 +
    match tag {
209 +
        case super::BIN_ADD => return il::BinOp::Add,
210 +
        case super::BIN_SUB => return il::BinOp::Sub,
211 +
        case super::BIN_MUL => return il::BinOp::Mul,
212 +
        case super::BIN_SDIV => return il::BinOp::Sdiv,
213 +
        case super::BIN_UDIV => return il::BinOp::Udiv,
214 +
        case super::BIN_SREM => return il::BinOp::Srem,
215 +
        case super::BIN_UREM => return il::BinOp::Urem,
216 +
        case super::BIN_EQ => return il::BinOp::Eq,
217 +
        case super::BIN_NE => return il::BinOp::Ne,
218 +
        case super::BIN_SLT => return il::BinOp::Slt,
219 +
        case super::BIN_SGE => return il::BinOp::Sge,
220 +
        case super::BIN_ULT => return il::BinOp::Ult,
221 +
        case super::BIN_UGE => return il::BinOp::Uge,
222 +
        case super::BIN_AND => return il::BinOp::And,
223 +
        case super::BIN_OR => return il::BinOp::Or,
224 +
        case super::BIN_XOR => return il::BinOp::Xor,
225 +
        case super::BIN_SHL => return il::BinOp::Shl,
226 +
        case super::BIN_SSHR => return il::BinOp::Sshr,
227 +
        case super::BIN_USHR => return il::BinOp::Ushr,
228 +
        else => throw binary::Error::Invalid,
229 +
    }
230 +
}
231 +
232 +
/// Read a checked un operation tag.
233 +
fn unOp(input: &mut Reader, wire: &[u8]) -> il::UnOp throws (binary::Error) {
234 +
    let tag = try integer(input, wire, 1) as u8;
235 +
    match tag {
236 +
        case super::UN_NEG => return il::UnOp::Neg,
237 +
        case super::UN_NOT => return il::UnOp::Not,
238 +
        else => throw binary::Error::Invalid,
239 +
    }
240 +
}
241 +
242 +
/// Read a checked cmp operation tag.
243 +
fn cmpOp(input: &mut Reader, wire: &[u8]) -> il::CmpOp throws (binary::Error) {
244 +
    let tag = try integer(input, wire, 1) as u8;
245 +
    match tag {
246 +
        case super::CMP_EQ => return il::CmpOp::Eq,
247 +
        case super::CMP_NE => return il::CmpOp::Ne,
248 +
        case super::CMP_SLT => return il::CmpOp::Slt,
249 +
        case super::CMP_ULT => return il::CmpOp::Ult,
250 +
        else => throw binary::Error::Invalid,
251 +
    }
252 +
}
253 +
254 +
/// Read one instruction and reconstruct its operand sequences.
255 +
export fn instr(input: &mut Reader, wire: &[u8], a: &mut opaque alloc::Allocator) -> il::Instr
256 +
    throws (binary::Error) {
257 +
    let tag = try integer(input, wire, 1) as u8;
258 +
    match tag {
259 +
        case super::INSTR_RESERVE => {
260 +
            let vdst = try reg(input, wire);
261 +
            let vlayout = try layoutIndex(input, wire);
262 +
            return il::Instr::Reserve { dst: vdst, layout: vlayout };
263 +
        },
264 +
        case super::INSTR_LOAD, super::INSTR_SLOAD => {
265 +
            let vtyp = try typ(input, wire);
266 +
            let vdst = try reg(input, wire);
267 +
            let vsrc = try reg(input, wire);
268 +
            let voffset = try integer(input, wire, 4) as i32;
269 +
            if tag == super::INSTR_SLOAD {
270 +
                return il::Instr::Sload { typ: vtyp, dst: vdst, src: vsrc, offset: voffset };
271 +
            }
272 +
            return il::Instr::Load { typ: vtyp, dst: vdst, src: vsrc, offset: voffset };
273 +
        },
274 +
        case super::INSTR_STORE => {
275 +
            let vtyp = try typ(input, wire);
276 +
            let vsrc = try val(input, wire);
277 +
            let vdst = try reg(input, wire);
278 +
            let voffset = try integer(input, wire, 4) as i32;
279 +
            return il::Instr::Store { typ: vtyp, src: vsrc, dst: vdst, offset: voffset };
280 +
        },
281 +
        case super::INSTR_BLIT => {
282 +
            let vdst = try reg(input, wire);
283 +
            let vsrc = try reg(input, wire);
284 +
            let vlayout = try layoutIndex(input, wire);
285 +
            return il::Instr::Blit { dst: vdst, src: vsrc, layout: vlayout };
286 +
        },
287 +
        case super::INSTR_COPY => {
288 +
            let vdst = try reg(input, wire);
289 +
            let vval = try val(input, wire);
290 +
            return il::Instr::Copy { dst: vdst, val: vval };
291 +
        },
292 +
        case super::INSTR_BINOP => {
293 +
            let vop = try binOp(input, wire);
294 +
            let vtyp = try typ(input, wire);
295 +
            let vdst = try reg(input, wire);
296 +
            let va = try val(input, wire);
297 +
            let vb = try val(input, wire);
298 +
            return il::Instr::BinOp { op: vop, typ: vtyp, dst: vdst, a: va, b: vb };
299 +
        },
300 +
        case super::INSTR_UNOP => {
301 +
            let vop = try unOp(input, wire);
302 +
            let vtyp = try typ(input, wire);
303 +
            let vdst = try reg(input, wire);
304 +
            let va = try val(input, wire);
305 +
            return il::Instr::UnOp { op: vop, typ: vtyp, dst: vdst, a: va };
306 +
        },
307 +
        case super::INSTR_ZEXT, super::INSTR_SEXT => {
308 +
            let vtyp = try typ(input, wire);
309 +
            let vdst = try reg(input, wire);
310 +
            let vval = try val(input, wire);
311 +
            if tag == super::INSTR_SEXT {
312 +
                return il::Instr::Sext { typ: vtyp, dst: vdst, val: vval };
313 +
            }
314 +
            return il::Instr::Zext { typ: vtyp, dst: vdst, val: vval };
315 +
        },
316 +
        case super::INSTR_CALL => {
317 +
            let vretTy = try valType(input, wire);
318 +
            let mut vdst: ?il::Reg = nil;
319 +
            if try flag(input, wire) {
320 +
                set vdst = try reg(input, wire);
321 +
            }
322 +
            let vfunc = try val(input, wire);
323 +
            let vargs = try values(input, wire, a);
324 +
            return il::Instr::Call { retTy: vretTy, dst: vdst, func: vfunc, args: vargs };
325 +
        },
326 +
        case super::INSTR_RET => {
327 +
            let mut vval: ?il::Val = nil;
328 +
            if try flag(input, wire) {
329 +
                set vval = try val(input, wire);
330 +
            }
331 +
            return il::Instr::Ret { val: vval };
332 +
        },
333 +
        case super::INSTR_JMP => {
334 +
            let vtarget = try target(input, wire);
335 +
            let vargs = try values(input, wire, a);
336 +
            return il::Instr::Jmp { target: vtarget, args: vargs };
337 +
        },
338 +
        case super::INSTR_BR => {
339 +
            let vop = try cmpOp(input, wire);
340 +
            let vtyp = try typ(input, wire);
341 +
            let va = try val(input, wire);
342 +
            let vb = try val(input, wire);
343 +
            let vthenTarget = try target(input, wire);
344 +
            let vthenArgs = try values(input, wire, a);
345 +
            let velseTarget = try target(input, wire);
346 +
            let velseArgs = try values(input, wire, a);
347 +
            return il::Instr::Br { op: vop, typ: vtyp, a: va, b: vb, thenTarget: vthenTarget,
348 +
                thenArgs: vthenArgs, elseTarget: velseTarget, elseArgs: velseArgs };
349 +
        },
350 +
        case super::INSTR_SWITCH => {
351 +
            let vval = try val(input, wire);
352 +
            let vdefaultTarget = try target(input, wire);
353 +
            let vdefaultArgs = try values(input, wire, a);
354 +
            let n = try count(input, wire, binary::MIN_SWITCH_CASE_BYTES);
355 +
            let start = input.cases.len;
356 +
            try input.cases.reserve(n, a) catch {
357 +
                throw binary::Error::Storage;
358 +
            };
359 +
            for _ in 0..n {
360 +
                let value = try integer(input, wire, 8) as i64;
361 +
                let block = try target(input, wire);
362 +
                let args = try values(input, wire, a);
363 +
                try input.cases.append(il::SwitchCase { value, target: block, args }, a) catch {
364 +
                    throw binary::Error::Storage;
365 +
                };
366 +
            }
367 +
            return il::Instr::Switch {
368 +
                val: vval, defaultTarget: vdefaultTarget, defaultArgs: vdefaultArgs,
369 +
                cases: il::CaseRange { start, len: n },
370 +
            };
371 +
        },
372 +
        case super::INSTR_UNREACHABLE => {
373 +
            return il::Instr::Unreachable;
374 +
        },
375 +
        case super::INSTR_ECALL => {
376 +
            let vdst = try reg(input, wire);
377 +
            let vnum = try val(input, wire);
378 +
            let va0 = try val(input, wire);
379 +
            let va1 = try val(input, wire);
380 +
            let va2 = try val(input, wire);
381 +
            let va3 = try val(input, wire);
382 +
            let mut vbuf: ?il::EcallBuf = nil;
383 +
            if try flag(input, wire) {
384 +
                let ptr = try reg(input, wire);
385 +
                let size = try integer(input, wire, 4) as u32;
386 +
                set vbuf = il::EcallBuf { ptr, size };
387 +
            }
388 +
            return il::Instr::Ecall { dst: vdst, num: vnum, a0: va0, a1: va1, a2: va2, a3: va3,
389 +
                buf: vbuf };
390 +
        },
391 +
        case super::INSTR_PAGE_SLICE => {
392 +
            let verr = try reg(input, wire);
393 +
            let vslot = try reg(input, wire);
394 +
            let vhandle = try val(input, wire);
395 +
            let vwrite = try flag(input, wire);
396 +
            let vlayout = try layoutIndex(input, wire);
397 +
            return il::Instr::PageSlice {
398 +
                err: verr, slot: vslot, handle: vhandle, write: vwrite, layout: vlayout,
399 +
            };
400 +
        },
401 +
        case super::INSTR_DEVICE_READ => {
402 +
            let t = try typ(input, wire);
403 +
            let dst = try reg(input, wire);
404 +
            let handle = try val(input, wire);
405 +
            let offset = try val(input, wire);
406 +
            return il::Instr::DeviceRead { typ: t, dst, handle, offset };
407 +
        },
408 +
        case super::INSTR_DEVICE_WRITE => {
409 +
            let t = try typ(input, wire);
410 +
            let handle = try val(input, wire);
411 +
            let offset = try val(input, wire);
412 +
            let value = try val(input, wire);
413 +
            return il::Instr::DeviceWrite { typ: t, handle, offset, value };
414 +
        },
415 +
        case super::INSTR_FIELD => {
416 +
            let vdst = try reg(input, wire);
417 +
            let vsrc = try reg(input, wire);
418 +
            let voffset = try integer(input, wire, 4) as u32;
419 +
            return il::Instr::Field { dst: vdst, src: vsrc, offset: voffset };
420 +
        },
421 +
        case super::INSTR_ELEM => {
422 +
            let vdst = try reg(input, wire);
423 +
            let vbase = try reg(input, wire);
424 +
            let vindex = try val(input, wire);
425 +
            let vlayout = try layoutIndex(input, wire);
426 +
            let vcount = try integer(input, wire, 4) as u32;
427 +
            return il::Instr::Elem { dst: vdst, base: vbase, index: vindex, layout: vlayout,
428 +
                count: vcount };
429 +
        },
430 +
        case super::INSTR_SLICE_ELEM => {
431 +
            let vdst = try reg(input, wire);
432 +
            let vslice = try reg(input, wire);
433 +
            let vindex = try val(input, wire);
434 +
            let vlayout = try layoutIndex(input, wire);
435 +
            return il::Instr::SliceElem { dst: vdst, slice: vslice, index: vindex,
436 +
                layout: vlayout };
437 +
        },
438 +
        case super::INSTR_SLICE_NEW => {
439 +
            let vdst = try reg(input, wire);
440 +
            let vbase = try val(input, wire);
441 +
            let vlayout = try layoutIndex(input, wire);
442 +
            let vcount = try integer(input, wire, 4) as u32;
443 +
            let vstart = try val(input, wire);
444 +
            let vend = try val(input, wire);
445 +
            return il::Instr::SliceNew {
446 +
                dst: vdst, base: vbase, layout: vlayout, count: vcount, start: vstart, end: vend,
447 +
            };
448 +
        },
449 +
        case super::INSTR_SLICE_SUB => {
450 +
            let vdst = try reg(input, wire);
451 +
            let vsrc = try reg(input, wire);
452 +
            let vlayout = try layoutIndex(input, wire);
453 +
            let vstart = try val(input, wire);
454 +
            let vend = try val(input, wire);
455 +
            return il::Instr::SliceSub { dst: vdst, src: vsrc, layout: vlayout, start: vstart,
456 +
                end: vend };
457 +
        },
458 +
        case super::INSTR_LOAD_PTR => {
459 +
            let vdst = try reg(input, wire);
460 +
            let vsrc = try reg(input, wire);
461 +
            let voffset = try integer(input, wire, 4) as i32;
462 +
            let nullable = try flag(input, wire);
463 +
            return il::Instr::LoadPtr { dst: vdst, src: vsrc, offset: voffset, nullable };
464 +
        },
465 +
        case super::INSTR_STORE_PTR => {
466 +
            let vsrc = try val(input, wire);
467 +
            let vdst = try reg(input, wire);
468 +
            let voffset = try integer(input, wire, 4) as i32;
469 +
            return il::Instr::StorePtr { src: vsrc, dst: vdst, offset: voffset };
470 +
        },
471 +
        case super::INSTR_EBREAK => {
472 +
            return il::Instr::Ebreak;
473 +
        },
474 +
        case super::INSTR_MEMORYFENCE => {
475 +
            return il::Instr::MemoryFence;
476 +
        },
477 +
        else => throw binary::Error::Invalid,
478 +
    }
479 +
}
480 +
481 +
/// Read an initializer with a repetition count.
482 +
export fn dataValue(input: &mut Reader, wire: &[u8], a: &mut opaque alloc::Allocator)
483 +
    -> il::DataValue throws (binary::Error) {
484 +
    let tag = try integer(input, wire, 1) as u8;
485 +
    let mut item: il::DataItem = il::DataItem::Undef;
486 +
    match tag {
487 +
        case super::DATA_VAL => {
488 +
            let t = try typ(input, wire);
489 +
            let n = try integer(input, wire, il::typeSize(t));
490 +
            set item = il::DataItem::Val { typ: t, val: n as i64 };
491 +
        },
492 +
        case super::DATA_SYM => {
493 +
            set item = il::DataItem::Sym(try symbol(input, wire));
494 +
        },
495 +
        case super::DATA_FN => {
496 +
            set item = il::DataItem::Fn(try symbol(input, wire));
497 +
        },
498 +
        case super::DATA_STR => {
499 +
            set item = il::DataItem::Str(try bytes(input, wire, a));
500 +
        },
501 +
        case super::DATA_UNDEF => {
502 +
            set item = il::DataItem::Undef;
503 +
        },
504 +
        else => throw binary::Error::Invalid,
505 +
    }
506 +
    let n = try integer(input, wire, 4) as u32;
507 +
    return il::DataValue { item, count: n };
508 +
}
lib/std/lang/il/binary/tests.rad added +489 -0
1 +
//! Binary RIL encoding fixtures.
2 +
3 +
@test export mod decode;
4 +
5 +
use std::testing;
6 +
use std::lang::il;
7 +
use std::lang::il::binary::writer;
8 +
use std::lang::il::binary;
9 +
use std::lang::il::binary::reader;
10 +
use std::lang::alloc;
11 +
12 +
/// Decode arena backing storage. Tests reset it before each use.
13 +
static MEMORY: [u8; 512] = [0; 512];
14 +
15 +
/// Check little-endian encoding for every integer width.
16 +
@test unsafe fn integers() throws (testing::TestError) {
17 +
    static buffer: [u8; 15] = [0; 15];
18 +
    let mut out = writer::new((&mut buffer[..]) as *mut [u8], &[]);
19 +
    try writer::integer(&mut out, 0x12, 1) catch {
20 +
        throw testing::TestError::Failed;
21 +
    };
22 +
    try writer::integer(&mut out, 0x3456, 2) catch {
23 +
        throw testing::TestError::Failed;
24 +
    };
25 +
    try writer::integer(&mut out, 0x789abcde, 4) catch {
26 +
        throw testing::TestError::Failed;
27 +
    };
28 +
    try writer::integer(&mut out, 0x0123456789abcdef, 8) catch {
29 +
        throw testing::TestError::Failed;
30 +
    };
31 +
    try testing::expectBytesEq(&buffer[..], &[0x12, 0x56, 0x34, 0xde, 0xbc, 0x9a, 0x78, 0xef, 0xcd,
32 +
        0xab, 0x89, 0x67, 0x45, 0x23, 0x01]);
33 +
}
34 +
35 +
/// Argument pool of the function that owns the test instructions.
36 +
static ARGS: [il::Val; 1] = [il::Val::Undef];
37 +
38 +
/// Case pool of the function that owns the test instructions.
39 +
static CASES: [il::SwitchCase; 1] = [il::SwitchCase {
40 +
    value: -1, target: 2, args: il::ArgRange { start: 0, len: 1 },
41 +
}];
42 +
43 +
/// Argument range selecting the single value of `ARGS`.
44 +
constant ONE_ARG: il::ArgRange = il::ArgRange { start: 0, len: 1 };
45 +
46 +
/// Create a cursor with the symbols and bounds of the instruction fixtures.
47 +
fn cursor(arena: &mut alloc::Arena) -> reader::Reader {
48 +
    let mut input = reader::new();
49 +
    try! input.symbols.append("data", arena);
50 +
    try! input.symbols.append("fn", arena);
51 +
    set input.registers = 16;
52 +
    set input.blocks = 4;
53 +
    set input.layouts = 4;
54 +
    set input.signatures = 2;
55 +
    return input;
56 +
}
57 +
58 +
/// Compare one instruction with its fixed wire representation.
59 +
unsafe fn instruction(item: il::Instr, expected: &[u8]) throws (testing::TestError) {
60 +
    let func = il::Fn {
61 +
        name: "f", params: &[], returnType: il::ValType::Data(il::Type::W64), isExtern: false,
62 +
            isLeaf: true,
63 +
        blocks: &[], vals: &ARGS[..], cases: &CASES[..],
64 +
    };
65 +
    static buffer: [u8; 256] = [0; 256];
66 +
    let mut out = writer::new((&mut buffer[..]) as *mut [u8], &["data", "fn"]);
67 +
    try writer::instr(&mut out, &func, item) catch {
68 +
        throw testing::TestError::Failed;
69 +
    };
70 +
    try testing::expectBytesEq(&buffer[..out.offset], expected);
71 +
    for capacity in 0..expected.len {
72 +
        let mut short = writer::new((&mut buffer[..capacity]) as *mut [u8], &["data", "fn"]);
73 +
        let mut failed = false;
74 +
        try writer::instr(&mut short, &func, item) catch err {
75 +
            try testing::expect(err == binary::Error::Capacity);
76 +
            set failed = true;
77 +
        };
78 +
        try testing::expect(failed);
79 +
        try testing::expect(short.offset <= capacity);
80 +
    }
81 +
    let memory = &mut MEMORY[..512];
82 +
    let mut arena = alloc::new(&mut memory[..]);
83 +
    let mut input = cursor(&mut arena);
84 +
    let decoded = try reader::instr(&mut input, expected, &mut arena) catch {
85 +
        throw testing::TestError::Failed;
86 +
    };
87 +
    try testing::expect(input.offset == expected.len);
88 +
    let vals = input.vals.take();
89 +
    let cases = input.cases.take();
90 +
    let decodedFn = il::Fn {
91 +
        name: "f", params: &[], returnType: il::ValType::Data(il::Type::W64), isExtern: false,
92 +
            isLeaf: true,
93 +
        blocks: &[], vals: &vals[..], cases: &cases[..],
94 +
    };
95 +
    set out.offset = 0;
96 +
    try writer::instr(&mut out, &decodedFn, decoded) catch {
97 +
        throw testing::TestError::Failed;
98 +
    };
99 +
    try testing::expectBytesEq(&buffer[..out.offset], expected);
100 +
    for length in 0..expected.len {
101 +
        alloc::reset(&mut arena);
102 +
        set input = cursor(&mut arena);
103 +
        let mut failed = false;
104 +
        try reader::instr(&mut input, &expected[..length], &mut arena) catch err {
105 +
            try testing::expect(err == binary::Error::Truncated);
106 +
            set failed = true;
107 +
        };
108 +
        try testing::expect(failed);
109 +
    }
110 +
111 +
}
112 +
113 +
/// Check all instruction tags and their field order.
114 +
@test unsafe fn instructions() throws (testing::TestError) {
115 +
    try instruction(il::Instr::Reserve {
116 +
        dst: il::Reg { n: 1 }, layout: 3,
117 +
    }, &[0, 1, 0, 0, 0, 3, 0, 0, 0]);
118 +
    try instruction(il::Instr::Load {
119 +
        typ: il::Type::W8, dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, offset: -1,
120 +
    }, &[1, 1, 1, 0, 0, 0, 2, 0, 0, 0, 255, 255, 255, 255]);
121 +
    try instruction(il::Instr::Sload {
122 +
        typ: il::Type::W16, dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, offset: 3,
123 +
    }, &[2, 2, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]);
124 +
    try instruction(il::Instr::Store {
125 +
        typ: il::Type::W32, src: il::Val::Undef, dst: il::Reg { n: 2 }, offset: 3,
126 +
    }, &[3, 4, 4, 2, 0, 0, 0, 3, 0, 0, 0]);
127 +
    try instruction(il::Instr::Blit {
128 +
        dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, layout: 3,
129 +
    }, &[4, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]);
130 +
    try instruction(il::Instr::Copy {
131 +
        dst: il::Reg { n: 1 }, val: il::Val::Undef,
132 +
    }, &[5, 1, 0, 0, 0, 4]);
133 +
    try instruction(il::Instr::Field {
134 +
        dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, offset: 8,
135 +
    }, &[21, 1, 0, 0, 0, 2, 0, 0, 0, 8, 0, 0, 0]);
136 +
    try instruction(il::Instr::Elem {
137 +
        dst: il::Reg { n: 1 }, base: il::Reg { n: 2 }, index: il::Val::Undef, layout: 3, count: 5,
138 +
    }, &[22, 1, 0, 0, 0, 2, 0, 0, 0, 4, 3, 0, 0, 0, 5, 0, 0, 0]);
139 +
    try instruction(il::Instr::SliceElem {
140 +
        dst: il::Reg { n: 1 }, slice: il::Reg { n: 2 }, index: il::Val::Undef, layout: 3,
141 +
    }, &[23, 1, 0, 0, 0, 2, 0, 0, 0, 4, 3, 0, 0, 0]);
142 +
    try instruction(il::Instr::SliceNew {
143 +
        dst: il::Reg { n: 1 }, base: il::Val::Undef, layout: 3, count: 5, start: il::Val::Undef,
144 +
        end: il::Val::Undef,
145 +
    }, &[24, 1, 0, 0, 0, 4, 3, 0, 0, 0, 5, 0, 0, 0, 4, 4]);
146 +
    try instruction(il::Instr::SliceSub {
147 +
        dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, layout: 3, start: il::Val::Undef,
148 +
        end: il::Val::Undef,
149 +
    }, &[25, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 4]);
150 +
    try instruction(il::Instr::LoadPtr {
151 +
        dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, offset: -1, nullable: false,
152 +
    }, &[26, 1, 0, 0, 0, 2, 0, 0, 0, 255, 255, 255, 255, 0]);
153 +
    try instruction(il::Instr::LoadPtr {
154 +
        dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, offset: 8, nullable: true,
155 +
    }, &[26, 1, 0, 0, 0, 2, 0, 0, 0, 8, 0, 0, 0, 1]);
156 +
    try instruction(il::Instr::StorePtr {
157 +
        src: il::Val::Undef, dst: il::Reg { n: 2 }, offset: 8,
158 +
    }, &[27, 4, 2, 0, 0, 0, 8, 0, 0, 0]);
159 +
    try instruction(il::Instr::BinOp {
160 +
        op: il::BinOp::Add, typ: il::Type::W64, dst: il::Reg { n: 1 }, a: il::Val::Undef,
161 +
        b: il::Val::Undef,
162 +
    }, &[6, 0, 8, 1, 0, 0, 0, 4, 4]);
163 +
    try instruction(il::Instr::UnOp {
164 +
        op: il::UnOp::Neg, typ: il::Type::W64, dst: il::Reg { n: 1 }, a: il::Val::Undef,
165 +
    }, &[7, 0, 8, 1, 0, 0, 0, 4]);
166 +
    try instruction(il::Instr::Zext {
167 +
        typ: il::Type::W8, dst: il::Reg { n: 1 }, val: il::Val::Undef,
168 +
    }, &[8, 1, 1, 0, 0, 0, 4]);
169 +
    try instruction(il::Instr::Sext {
170 +
        typ: il::Type::W16, dst: il::Reg { n: 1 }, val: il::Val::Undef,
171 +
    }, &[9, 2, 1, 0, 0, 0, 4]);
172 +
    try instruction(il::Instr::Call {
173 +
        retTy: il::ValType::Data(il::Type::W64), dst: nil, func: il::Val::FnAddr("fn"),
174 +
            args: il::ArgRange { start: 0, len: 0 },
175 +
    }, &[10, 1, 8, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0]);
176 +
    try instruction(il::Instr::Call {
177 +
        retTy: il::ValType::Data(il::Type::W64), dst: il::Reg { n: 2 }, func: il::Val::FnAddr(
178 +
            "fn"), args: ONE_ARG,
179 +
    }, &[10, 1, 8, 1, 2, 0, 0, 0, 3, 1, 0, 0, 0, 1, 0, 0, 0, 4]);
180 +
    try instruction(il::Instr::Call {
181 +
        retTy: il::ValType::Void, dst: nil, func: il::Val::FnAddr("fn"), args: il::ArgRange {
182 +
            start: 0, len: 0 },
183 +
    }, &[10, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0]);
184 +
    try instruction(il::Instr::Call {
185 +
        retTy: il::ValType::Ptr(il::PtrType { layout: 2, own: false, mutable: true,
186 +
            nullable: false }),
187 +
        dst: il::Reg { n: 2 }, func: il::Val::FnAddr("fn"), args: ONE_ARG,
188 +
    }, &[10, 2, 2, 0, 0, 0, 2, 1, 2, 0, 0, 0, 3, 1, 0, 0, 0, 1, 0, 0, 0, 4]);
189 +
    try instruction(il::Instr::Call {
190 +
        retTy: il::ValType::Fn(1), dst: il::Reg { n: 2 }, func: il::Val::FnAddr("fn"),
191 +
            args: ONE_ARG,
192 +
    }, &[10, 3, 1, 0, 0, 0, 1, 2, 0, 0, 0, 3, 1, 0, 0, 0, 1, 0, 0, 0, 4]);
193 +
    try instruction(il::Instr::Ret {
194 +
        val: nil,
195 +
    }, &[11, 0]);
196 +
    try instruction(il::Instr::Ret {
197 +
        val: il::Val::Undef,
198 +
    }, &[11, 1, 4]);
199 +
    try instruction(il::Instr::Jmp {
200 +
        target: 2, args: ONE_ARG,
201 +
    }, &[12, 2, 0, 0, 0, 1, 0, 0, 0, 4]);
202 +
    try instruction(il::Instr::Br {
203 +
        op: il::CmpOp::Eq, typ: il::Type::W32, a: il::Val::Undef, b: il::Val::Undef, thenTarget: 1,
204 +
        thenArgs: ONE_ARG, elseTarget: 2, elseArgs: ONE_ARG,
205 +
    }, &[13, 0, 4, 4, 4, 1, 0, 0, 0, 1, 0, 0, 0, 4, 2, 0, 0, 0, 1, 0, 0, 0, 4]);
206 +
    try instruction(il::Instr::Switch {
207 +
        val: il::Val::Undef, defaultTarget: 1, defaultArgs: ONE_ARG,
208 +
            cases: il::CaseRange { start: 0, len: 1 },
209 +
    }, &[
210 +
        14, 4, 1, 0, 0, 0, 1, 0, 0, 0, 4, 1,
211 +
        0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 2,
212 +
        0, 0, 0, 1, 0, 0, 0, 4,
213 +
    ]);
214 +
    try instruction(il::Instr::Unreachable, &[15]);
215 +
    try instruction(il::Instr::Ecall {
216 +
        dst: il::Reg { n: 1 }, num: il::Val::Undef, a0: il::Val::Undef, a1: il::Val::Undef,
217 +
        a2: il::Val::Undef, a3: il::Val::Undef, buf: nil,
218 +
    }, &[16, 1, 0, 0, 0, 4, 4, 4, 4, 4, 0]);
219 +
    try instruction(il::Instr::Ecall {
220 +
        dst: il::Reg { n: 1 }, num: il::Val::Undef, a0: il::Val::Undef, a1: il::Val::Undef,
221 +
        a2: il::Val::Undef, a3: il::Val::Undef, buf: il::EcallBuf { ptr: il::Reg { n: 2 },
222 +
            size: 24 },
223 +
    }, &[16, 1, 0, 0, 0, 4, 4, 4, 4, 4, 1, 2, 0, 0, 0, 24, 0, 0, 0]);
224 +
    try instruction(il::Instr::PageSlice {
225 +
        err: il::Reg { n: 1 }, slot: il::Reg { n: 2 }, handle: il::Val::Undef, write: true,
226 +
            layout: 3,
227 +
    }, &[28, 1, 0, 0, 0, 2, 0, 0, 0, 4, 1, 3, 0, 0, 0]);
228 +
    try instruction(il::Instr::Ebreak, &[17]);
229 +
    try instruction(il::Instr::MemoryFence, &[18]);
230 +
    for typ in [il::Type::W8, il::Type::W16, il::Type::W32, il::Type::W64] {
231 +
        let mut read: [u8; 8] = [19, 0, 1, 0, 0, 0, 4, 4];
232 +
        let mut write: [u8; 5] = [20, 0, 4, 4, 4];
233 +
        set read[1] = il::typeSize(typ) as u8;
234 +
        set write[1] = il::typeSize(typ) as u8;
235 +
        try instruction(il::Instr::DeviceRead {
236 +
            typ, dst: il::Reg { n: 1 }, handle: il::Val::Undef, offset: il::Val::Undef,
237 +
        }, &read[..]);
238 +
        try instruction(il::Instr::DeviceWrite {
239 +
            typ, handle: il::Val::Undef, offset: il::Val::Undef, value: il::Val::Undef,
240 +
        }, &write[..]);
241 +
    }
242 +
}
243 +
244 +
/// Check every value tag and empty and nonempty sequences.
245 +
@test unsafe fn values() throws (testing::TestError) {
246 +
    static buffer: [u8; 64] = [0; 64];
247 +
    let mut out = writer::new((&mut buffer[..]) as *mut [u8], &["data", "fn"]);
248 +
    try writer::values(&mut out, &[]) catch {
249 +
        throw testing::TestError::Failed;
250 +
    };
251 +
    try writer::values(&mut out, &[
252 +
        il::Val::Reg(il::Reg { n: 0x12345678 }),
253 +
        il::Val::Imm(-2), il::Val::DataSym("data"),
254 +
        il::Val::FnAddr("fn"), il::Val::Undef,
255 +
    ]) catch {
256 +
        throw testing::TestError::Failed;
257 +
    };
258 +
    try testing::expectBytesEq(&buffer[..out.offset], &[
259 +
        0, 0, 0, 0, 5, 0, 0, 0,
260 +
        0, 0x78, 0x56, 0x34, 0x12,
261 +
        1, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
262 +
        2, 0, 0, 0, 0, 3, 1, 0, 0, 0, 4,
263 +
    ]);
264 +
    let memory = &mut MEMORY[..256];
265 +
    let mut arena = alloc::new(&mut memory[..]);
266 +
    let mut input = cursor(&mut arena);
267 +
    set input.registers = 0x12345679;
268 +
    let empty = try reader::values(&mut input, &buffer[..out.offset], &mut arena) catch {
269 +
        throw testing::TestError::Failed;
270 +
    };
271 +
    let decoded = try reader::values(&mut input, &buffer[..out.offset], &mut arena) catch {
272 +
        throw testing::TestError::Failed;
273 +
    };
274 +
    try testing::expect(empty.len == 0);
275 +
    try testing::expect(decoded.len == 5);
276 +
    try testing::expect(input.offset == out.offset);
277 +
    let pool = input.vals.take();
278 +
    static repeated: [u8; 64] = [0; 64];
279 +
    let mut copy = writer::new((&mut repeated[..]) as *mut [u8], &["data", "fn"]);
280 +
    try writer::values(&mut copy, &pool[empty.start..empty.start + empty.len]) catch {
281 +
        throw testing::TestError::Failed;
282 +
    };
283 +
    try writer::values(&mut copy, &pool[decoded.start..decoded.start + decoded.len]) catch {
284 +
        throw testing::TestError::Failed;
285 +
    };
286 +
    try testing::expectBytesEq(&buffer[..out.offset], &repeated[..copy.offset]);
287 +
}
288 +
289 +
/// Check initializer bytes and repetition counts.
290 +
@test unsafe fn initializers() throws (testing::TestError) {
291 +
    static buffer: [u8; 128] = [0; 128];
292 +
    let mut out = writer::new((&mut buffer[..]) as *mut [u8], &["data", "fn"]);
293 +
    try writer::dataValue(&mut out, il::DataValue { item: il::DataItem::Val { typ: il::Type::W8,
294 +
        val: -1 }, count: 0 }) catch {
295 +
            throw testing::TestError::Failed;
296 +
        };
297 +
    try writer::dataValue(&mut out, il::DataValue { item: il::DataItem::Val { typ: il::Type::W16,
298 +
        val: -2 }, count: 1 }) catch {
299 +
            throw testing::TestError::Failed;
300 +
        };
301 +
    try writer::dataValue(&mut out, il::DataValue { item: il::DataItem::Val { typ: il::Type::W32,
302 +
        val: -3 }, count: 2 }) catch {
303 +
            throw testing::TestError::Failed;
304 +
        };
305 +
    try writer::dataValue(&mut out, il::DataValue { item: il::DataItem::Val { typ: il::Type::W64,
306 +
        val: -4 }, count: 3 }) catch {
307 +
            throw testing::TestError::Failed;
308 +
        };
309 +
    try writer::dataValue(&mut out, il::DataValue { item: il::DataItem::Sym("data"), count: 4 })
310 +
        catch {
311 +
            throw testing::TestError::Failed;
312 +
        };
313 +
    try writer::dataValue(&mut out, il::DataValue { item: il::DataItem::Fn("fn"), count: 5 }) catch
314 +
        {
315 +
            throw testing::TestError::Failed;
316 +
        };
317 +
    try writer::dataValue(&mut out, il::DataValue { item: il::DataItem::Str("ab"), count: 6 }) catch
318 +
        {
319 +
            throw testing::TestError::Failed;
320 +
        };
321 +
    try writer::dataValue(&mut out, il::DataValue { item: il::DataItem::Str(""), count: 7 }) catch {
322 +
        throw testing::TestError::Failed;
323 +
    };
324 +
    try writer::dataValue(&mut out, il::DataValue { item: il::DataItem::Undef, count: 8 }) catch {
325 +
        throw testing::TestError::Failed;
326 +
    };
327 +
    try testing::expectBytesEq(&buffer[..out.offset], &[0, 1, 255, 0, 0, 0, 0, 0, 2, 254, 255, 1, 0,
328 +
        0, 0, 0, 4, 253, 255, 255, 255, 2, 0, 0, 0, 0, 8, 252, 255, 255, 255, 255, 255, 255, 255, 3,
329 +
        0, 0, 0, 1, 0, 0, 0, 0, 4, 0, 0, 0, 2, 1, 0, 0, 0, 5, 0, 0, 0, 3, 2, 0, 0, 0, 97, 98, 6, 0,
330 +
        0, 0, 3, 0, 0, 0, 0, 7, 0, 0, 0, 4, 8, 0, 0, 0]);
331 +
}
332 +
333 +
/// Reject invalid widths, insufficient storage, and absent symbols.
334 +
@test unsafe fn errors() throws (testing::TestError) {
335 +
    static buffer: [u8; 8] = [0; 8];
336 +
    let mut out = writer::new((&mut buffer[..]) as *mut [u8], &[]);
337 +
    let mut failures: u32 = 0;
338 +
    try writer::integer(&mut out, 1, 3) catch err {
339 +
        try testing::expect(err == binary::Error::Invalid);
340 +
        set failures += 1;
341 +
    };
342 +
    try writer::symbol(&mut out, "absent") catch err {
343 +
        try testing::expect(err == binary::Error::Symbol);
344 +
        set failures += 1;
345 +
    };
346 +
    try testing::expect(out.offset == 0);
347 +
    try writer::integer(&mut out, 0, 8) catch {
348 +
        throw testing::TestError::Failed;
349 +
    };
350 +
    try writer::integer(&mut out, 1, 1) catch err {
351 +
        try testing::expect(err == binary::Error::Capacity);
352 +
        set failures += 1;
353 +
    };
354 +
    try testing::expect(failures == 3);
355 +
    try testing::expect(out.offset == 8);
356 +
}
357 +
358 +
/// Check operation tags independently of the native union representation.
359 +
@test unsafe fn operations() throws (testing::TestError) {
360 +
    let binaryOps = &[
361 +
        il::BinOp::Add, il::BinOp::Sub, il::BinOp::Mul,
362 +
        il::BinOp::Sdiv, il::BinOp::Udiv, il::BinOp::Srem,
363 +
        il::BinOp::Urem, il::BinOp::Eq, il::BinOp::Ne,
364 +
        il::BinOp::Slt, il::BinOp::Sge, il::BinOp::Ult,
365 +
        il::BinOp::Uge, il::BinOp::And, il::BinOp::Or,
366 +
        il::BinOp::Xor, il::BinOp::Shl, il::BinOp::Sshr,
367 +
        il::BinOp::Ushr,
368 +
    ];
369 +
    for op, tag in binaryOps {
370 +
        try instruction(il::Instr::BinOp {
371 +
            op, typ: il::Type::W64, dst: il::Reg { n: 1 },
372 +
            a: il::Val::Undef, b: il::Val::Undef,
373 +
        }, &[6, tag as u8, 8, 1, 0, 0, 0, 4, 4]);
374 +
    }
375 +
    for op, tag in &[il::UnOp::Neg, il::UnOp::Not] {
376 +
        try instruction(il::Instr::UnOp {
377 +
            op, typ: il::Type::W64, dst: il::Reg { n: 1 },
378 +
            a: il::Val::Undef,
379 +
        }, &[7, tag as u8, 8, 1, 0, 0, 0, 4]);
380 +
    }
381 +
    let empty = il::ArgRange { start: 0, len: 0 };
382 +
    for op, tag in &[il::CmpOp::Eq, il::CmpOp::Ne, il::CmpOp::Slt, il::CmpOp::Ult] {
383 +
        try instruction(il::Instr::Br {
384 +
            op, typ: il::Type::W8, a: il::Val::Undef, b: il::Val::Undef,
385 +
            thenTarget: 1, thenArgs: empty,
386 +
            elseTarget: 2, elseArgs: empty,
387 +
        }, &[13, tag as u8, 1, 4, 4, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]);
388 +
    }
389 +
}
390 +
391 +
/// Check byte-sequence bounds at every output size.
392 +
@test unsafe fn byteCapacity() throws (testing::TestError) {
393 +
    static buffer: [u8; 7] = [0; 7];
394 +
    for capacity in 0..7 {
395 +
        let mut out = writer::new((&mut buffer[..capacity]) as *mut [u8], &[]);
396 +
        let mut failed = false;
397 +
        try writer::bytes(&mut out, "abc") catch err {
398 +
            try testing::expect(err == binary::Error::Capacity);
399 +
            set failed = true;
400 +
        };
401 +
        try testing::expect(failed);
402 +
        try testing::expect(out.offset <= capacity);
403 +
    }
404 +
    let mut out = writer::new((&mut buffer[..]) as *mut [u8], &[]);
405 +
    try writer::bytes(&mut out, "abc") catch {
406 +
        throw testing::TestError::Failed;
407 +
    };
408 +
    try testing::expectBytesEq(&buffer[..], &[3, 0, 0, 0, 97, 98, 99]);
409 +
}
410 +
411 +
/// Check the wire form of value types, and that the reader checks table indices.
412 +
@test unsafe fn valTypes() throws (testing::TestError) {
413 +
    let ptr = il::PtrType { layout: 3, own: true, mutable: false, nullable: true };
414 +
    let fixtures: [il::ValType; 6] = [
415 +
        il::ValType::Void, il::ValType::Data(il::Type::W16), il::ValType::Ptr(ptr),
416 +
        il::ValType::Ptr(il::PtrType { layout: 0, own: false, mutable: true, nullable: false }),
417 +
        il::ValType::Fn(1), il::ValType::Fn(0),
418 +
    ];
419 +
    let expected: [*[u8]; 6] = [
420 +
        &[0], &[1, 2], &[2, 3, 0, 0, 0, 5], &[2, 0, 0, 0, 0, 2], &[3, 1, 0, 0, 0], &[3, 0, 0, 0, 0],
421 +
    ];
422 +
    for item, i in &fixtures[..] {
423 +
        static buffer: [u8; 8] = [0; 8];
424 +
        let mut out = writer::new((&mut buffer[..]) as *mut [u8], &[]);
425 +
        try writer::valType(&mut out, item) catch {
426 +
            throw testing::TestError::Failed;
427 +
        };
428 +
        try testing::expectBytesEq(&buffer[..out.offset], expected[i]);
429 +
430 +
        let memory = &mut MEMORY[..64];
431 +
        let mut arena = alloc::new(&mut memory[..]);
432 +
        let mut input = cursor(&mut arena);
433 +
        let decoded = try reader::valType(&mut input, expected[i]) catch {
434 +
            throw testing::TestError::Failed;
435 +
        };
436 +
        try testing::expect(decoded == item);
437 +
    }
438 +
    // Indices outside the tables, unknown tags, and unknown flags are invalid.
439 +
    let invalid: [*[u8]; 5] = [
440 +
        &[2, 4, 0, 0, 0, 0], &[3, 2, 0, 0, 0], &[4], &[2, 0, 0, 0, 0, 8], &[1, 3],
441 +
    ];
442 +
    for bytes in &invalid[..] {
443 +
        let memory = &mut MEMORY[..64];
444 +
        let mut arena = alloc::new(&mut memory[..]);
445 +
        let mut input = cursor(&mut arena);
446 +
        let mut failed = false;
447 +
        try reader::valType(&mut input, bytes) catch err {
448 +
            try testing::expect(err == binary::Error::Invalid);
449 +
            set failed = true;
450 +
        };
451 +
        try testing::expect(failed);
452 +
    }
453 +
}
454 +
455 +
/// Check the wire form of a layout with every kind of slot, and of a signature.
456 +
@test unsafe fn layoutsAndSignatures() throws (testing::TestError) {
457 +
    static buffer: [u8; 128] = [0; 128];
458 +
    let mut out = writer::new((&mut buffer[..]) as *mut [u8], &[]);
459 +
    let layout = il::Layout {
460 +
        size: 64, alignment: 8,
461 +
        slots: &[
462 +
            il::Slot { offset: 0, kind: il::SlotKind::Ptr(
463 +
                il::PtrType { layout: 1, own: true, mutable: false, nullable: true }) },
464 +
            il::Slot { offset: 8, kind: il::SlotKind::Slice(
465 +
                il::PtrType { layout: 2, own: false, mutable: true, nullable: true }) },
466 +
            il::Slot { offset: 24, kind: il::SlotKind::Fn(1) },
467 +
            il::Slot { offset: 32, kind: il::SlotKind::Sub(0) },
468 +
            il::Slot { offset: 40, kind: il::SlotKind::Array { layout: 3, count: 2 } },
469 +
        ],
470 +
    };
471 +
    try writer::layout(&mut out, layout) catch {
472 +
        throw testing::TestError::Failed;
473 +
    };
474 +
    try testing::expectBytesEq(&buffer[..out.offset], &[
475 +
        64, 0, 0, 0, 8, 0, 0, 0, 5, 0, 0, 0,
476 +
        0, 0, 0, 0, 0, 1, 0, 0, 0, 5,
477 +
        8, 0, 0, 0, 1, 2, 0, 0, 0, 6,
478 +
        24, 0, 0, 0, 2, 1, 0, 0, 0,
479 +
        32, 0, 0, 0, 3, 0, 0, 0, 0,
480 +
        40, 0, 0, 0, 4, 3, 0, 0, 0, 2, 0, 0, 0,
481 +
    ]);
482 +
    set out.offset = 0;
483 +
    try writer::signature(&mut out, il::Signature {
484 +
        params: &[il::ValType::Data(il::Type::W32), il::ValType::Fn(1)], ret: il::ValType::Void,
485 +
    }) catch {
486 +
        throw testing::TestError::Failed;
487 +
    };
488 +
    try testing::expectBytesEq(&buffer[..out.offset], &[2, 0, 0, 0, 1, 4, 3, 1, 0, 0, 0, 0]);
489 +
}
lib/std/lang/il/binary/tests/decode.rad added +780 -0
1 +
//! Package reconstruction, malformed input, and arena rollback tests.
2 +
3 +
use std::testing;
4 +
use std::lang::alloc;
5 +
use std::lang::il;
6 +
use std::lang::il::binary;
7 +
use std::lang::il::binary::reader;
8 +
use std::lang::il::binary::program;
9 +
use std::lang::il::binary::collect;
10 +
use std::collections::dict;
11 +
12 +
/// Decode arena backing storage. Tests reset it before each use.
13 +
static MEMORY: [u8; 2048] = [0; 2048];
14 +
15 +
/// Program without declarations.
16 +
constant EMPTY: il::Program = il::Program { data: &[], fns: &[], layouts: &[], signatures: &[] };
17 +
18 +
/// Structural limits for small test programs.
19 +
constant LIMITS: binary::Limits = binary::Limits { registers: 16, blocks: 4 };
20 +
21 +
/// Encode `package` into test storage that the writer borrows for the call.
22 +
fn encodeInto(buffer: *mut [u8], package: &binary::Package) -> u32 throws (binary::Error) {
23 +
    let mut out = buffer;
24 +
    return try program::encode(&mut out, package);
25 +
}
26 +
27 +
/// Check a package round trip and failure at every truncated byte length.
28 +
unsafe fn roundTrip(package: &binary::Package) throws (testing::TestError) {
29 +
    static buffer: [u8; 2048] = [0; 2048];
30 +
    let length = try encodeInto((&mut buffer[..]) as *mut [u8], package) catch {
31 +
        throw testing::TestError::Failed;
32 +
    };
33 +
    let memory = &mut MEMORY[..2048];
34 +
    let mut arena = alloc::new(&mut memory[..]);
35 +
    alloc::restore(&mut arena, 8);
36 +
    let decoded = try program::decode(&buffer[..length], &mut arena, LIMITS) catch {
37 +
        throw testing::TestError::Failed;
38 +
    };
39 +
    try testing::expectBytesEq(decoded.name, package.name);
40 +
    try testing::expect(decoded.dependencies.len == package.dependencies.len);
41 +
    try testing::expect(decoded.exports.len == package.exports.len);
42 +
    try testing::expect(decoded.program.fns.len == package.program.fns.len);
43 +
    static encoded: [u8; 2048] = [0; 2048];
44 +
    let repeated = try encodeInto((&mut encoded[..]) as *mut [u8], &decoded) catch {
45 +
        throw testing::TestError::Failed;
46 +
    };
47 +
    try testing::expectBytesEq(&buffer[..length], &encoded[..repeated]);
48 +
    // Decoded names and string initializers retain arena ownership.
49 +
    for i in 0..length {
50 +
        set buffer[i] = 0;
51 +
    }
52 +
    let copied = try encodeInto((&mut buffer[..]) as *mut [u8], &decoded) catch {
53 +
        throw testing::TestError::Failed;
54 +
    };
55 +
    try testing::expectBytesEq(&buffer[..copied], &encoded[..repeated]);
56 +
    for end in 0..length {
57 +
        alloc::restore(&mut arena, 8);
58 +
        let mut failed = false;
59 +
        try program::decode(&buffer[..end], &mut arena, LIMITS) catch err {
60 +
            try testing::expect(err == binary::Error::Truncated);
61 +
            set failed = true;
62 +
        };
63 +
        try testing::expect(failed);
64 +
        try testing::expect(alloc::used(&arena) == 8);
65 +
    }
66 +
    // Exercise every smaller arena extent, including alignment boundaries.
67 +
    let mut usedArena = alloc::new(&mut memory[..]);
68 +
    try program::decode(&buffer[..length], &mut usedArena, LIMITS) catch {
69 +
        throw testing::TestError::Failed;
70 +
    };
71 +
    let required = alloc::used(&usedArena);
72 +
    for capacity in 0..required {
73 +
        let mut short = alloc::new(&mut MEMORY[..capacity]);
74 +
        let mut failed = false;
75 +
        try program::decode(&buffer[..length], &mut short, LIMITS) catch err {
76 +
            try testing::expect(err == binary::Error::Storage);
77 +
            set failed = true;
78 +
        };
79 +
        try testing::expect(failed);
80 +
        try testing::expect(alloc::used(&short) == 0);
81 +
    }
82 +
}
83 +
84 +
/// Check an empty program with a package identity.
85 +
@test unsafe fn emptyPackage() throws (testing::TestError) {
86 +
    let package = binary::Package {
87 +
        symbols: &["p"], name: "p", dependencies: &[], exports: &[], entry: nil,
88 +
        program: EMPTY,
89 +
    };
90 +
    try roundTrip(&package);
91 +
    static buffer: [u8; 64] = [0; 64];
92 +
    let length = try encodeInto((&mut buffer[..]) as *mut [u8], &package) catch {
93 +
        throw testing::TestError::Failed;
94 +
    };
95 +
    try testing::expectBytesEq(&buffer[..length], &[
96 +
        82, 73, 76, 0, 1, 0, 0, 0,
97 +
        1, 0, 0, 0, 112, 0, 0, 0, 0,
98 +
        0, 0, 0, 0, 0, 0, 0, 0, 0,
99 +
        0, 0, 0, 0, 0, 0, 0, 0,
100 +
        0, 0, 0, 0, 0, 0, 0, 0,
101 +
    ]);
102 +
}
103 +
104 +
/// Check functions, dependencies, exports, initializers, and block metadata.
105 +
@test unsafe fn fullPackage() throws (testing::TestError) {
106 +
    let params: *[il::Param] = &[il::Param { value: il::Reg { n: 1 }, type: il::ValType::Data(
107 +
        il::Type::W64) }];
108 +
    let blocks: *[il::Block] = &[il::Block {
109 +
        label: il::BlockLabel { base: "entry", suffix: 2 }, params: &[], instrs: &[
110 +
            il::Instr::Call {
111 +
                retTy: il::ValType::Data(il::Type::W64), dst: il::Reg { n: 2 },
112 +
                    func: il::Val::FnAddr("dep::fn"),
113 +
                args: il::ArgRange { start: 0, len: 1 },
114 +
            },
115 +
            il::Instr::Ret { val: il::Val::Reg(il::Reg { n: 2 }) },
116 +
        ],
117 +
        locs: &[], preds: &[], loopDepth: 0,
118 +
    }];
119 +
    let func = il::Fn {
120 +
        name: "p::main", params, returnType: il::ValType::Data(il::Type::W64), isExtern: false,
121 +
            isLeaf: false,
122 +
        blocks, vals: &[il::Val::Reg(il::Reg { n: 1 })], cases: &[],
123 +
    };
124 +
    let external = il::Fn {
125 +
        name: "dep::fn", params, returnType: il::ValType::Data(il::Type::W64), isExtern: true,
126 +
            isLeaf: true, blocks: &[],
127 +
        vals: &[], cases: &[],
128 +
    };
129 +
    let data = il::Data {
130 +
        name: "p::data", size: 64, alignment: 8, layout: 0, readOnly: false, isZeroInit: false,
131 +
        values: &[
132 +
            il::DataValue { item: il::DataItem::Val { typ: il::Type::W8, val: -1 }, count: 2 },
133 +
            il::DataValue { item: il::DataItem::Val { typ: il::Type::W16, val: -2 }, count: 1 },
134 +
            il::DataValue { item: il::DataItem::Val { typ: il::Type::W32, val: -3 }, count: 1 },
135 +
            il::DataValue { item: il::DataItem::Val { typ: il::Type::W64, val: -4 }, count: 1 },
136 +
            il::DataValue { item: il::DataItem::Sym("p::data"), count: 1 },
137 +
            il::DataValue { item: il::DataItem::Fn("dep::fn"), count: 1 },
138 +
            il::DataValue { item: il::DataItem::Str("bytes"), count: 2 },
139 +
            il::DataValue { item: il::DataItem::Str(""), count: 0 },
140 +
            il::DataValue { item: il::DataItem::Undef, count: 1 },
141 +
        ],
142 +
    };
143 +
    let zero = il::Data {
144 +
        name: "p::zero", size: 4096, alignment: 4096, layout: 1, readOnly: false, isZeroInit: true,
145 +
            values: &[],
146 +
    };
147 +
    static FIXTURE: [u8; 256] = [0; 256];
148 +
    let mut arena = alloc::new(&mut FIXTURE[..]);
149 +
    let mut list: *mut [*il::Fn] = &mut [];
150 +
    try! list.append(retainFn(func, &mut arena), &mut arena);
151 +
    try! list.append(retainFn(external, &mut arena), &mut arena);
152 +
    let functions: *[*il::Fn] = &list[..];
153 +
    let package = binary::Package {
154 +
        symbols: &["p", "dep", "p::main", "dep::fn", "p::data", "p::zero"], name: "p",
155 +
        dependencies: &["dep"],
156 +
        exports: &[
157 +
            binary::Export { name: "p::main", kind: binary::ExportKind::Function },
158 +
            binary::Export { name: "p::data", kind: binary::ExportKind::Data },
159 +
        ],
160 +
        entry: "p::main", program: il::Program {
161 +
            data: retainData(&[data, zero]), fns: functions,
162 +
            layouts: &[
163 +
                il::Layout { size: 64, alignment: 8, slots: &[] },
164 +
                il::Layout { size: 4096, alignment: 4096, slots: &[] },
165 +
            ],
166 +
            signatures: &[],
167 +
        },
168 +
    };
169 +
    try roundTrip(&package);
170 +
}
171 +
172 +
/// Reject an invalid encoded instruction without a panic.
173 +
unsafe fn badInstruction(bytes: *[u8], expected: binary::Error) throws (testing::TestError) {
174 +
    try badInstructionIn(bytes, 0, expected);
175 +
}
176 +
177 +
/// Reject an invalid encoded instruction in a package whose layout table has `layouts` entries.
178 +
unsafe fn badInstructionIn(bytes: *[u8], layouts: u32, expected: binary::Error)
179 +
    throws (testing::TestError) {
180 +
    let memory = &mut MEMORY[..64];
181 +
    let mut arena = alloc::new(&mut memory[..]);
182 +
    let mut input = reader::new();
183 +
    try! input.symbols.append("p", &mut arena);
184 +
    set input.registers = 2;
185 +
    set input.blocks = 1;
186 +
    set input.layouts = layouts;
187 +
    let mut failed = false;
188 +
    try reader::instr(&mut input, bytes, &mut arena) catch err {
189 +
        try testing::expect(err == expected);
190 +
        set failed = true;
191 +
    };
192 +
    try testing::expect(failed);
193 +
}
194 +
195 +
/// Check instruction tags, operand tags, types, optional flags, and indices.
196 +
@test unsafe fn malformedInstructions() throws (testing::TestError) {
197 +
    try badInstruction(&[26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], binary::Error::Invalid);
198 +
    try badInstruction(&[255], binary::Error::Invalid);
199 +
    try badInstruction(&[5, 0, 0, 0, 0, 255], binary::Error::Invalid);
200 +
    try badInstruction(&[1, 3], binary::Error::Invalid);
201 +
    try badInstruction(&[6, 255], binary::Error::Invalid);
202 +
    try badInstruction(&[7, 255], binary::Error::Invalid);
203 +
    try badInstruction(&[13, 255], binary::Error::Invalid);
204 +
    try badInstruction(&[11, 2], binary::Error::Invalid);
205 +
    try badInstruction(&[10, 8, 2], binary::Error::Invalid);
206 +
    try badInstruction(&[5, 2, 0, 0, 0], binary::Error::Invalid);
207 +
    try badInstruction(&[5, 0, 0, 0, 0, 0, 2, 0, 0, 0], binary::Error::Invalid);
208 +
    try badInstruction(&[12, 1, 0, 0, 0], binary::Error::Invalid);
209 +
    try badInstruction(&[5, 0, 0, 0, 0, 2, 1, 0, 0, 0], binary::Error::Symbol);
210 +
    try badInstruction(&[12, 0, 0, 0, 0, 255, 255, 255, 255], binary::Error::Truncated);
211 +
    // No layout table, so no layout index is valid.
212 +
    try badInstruction(&[0, 0, 0, 0, 0, 0, 0, 0, 0], binary::Error::Invalid);
213 +
    try badInstruction(&[22, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 1, 0, 0, 0],
214 +
        binary::Error::Invalid);
215 +
    try badInstruction(&[23, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0], binary::Error::Invalid);
216 +
    try badInstruction(&[24, 0, 0, 0, 0, 4, 0, 0, 0, 0, 1, 0, 0, 0, 4, 4], binary::Error::Invalid);
217 +
    try badInstruction(&[25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4], binary::Error::Invalid);
218 +
    // Registers and the layout of a blit are range checked.
219 +
    try badInstruction(&[21, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], binary::Error::Invalid);
220 +
    try badInstruction(&[4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128], binary::Error::Invalid);
221 +
    try badInstructionIn(&[4, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0], 2, binary::Error::Invalid);
222 +
    try badInstructionIn(&[4, 0, 0, 0, 0, 1, 0, 0, 0, 255, 255, 255, 255], 2,
223 +
        binary::Error::Invalid);
224 +
}
225 +
226 +
/// Check header validation and package symbol indices with arena rollback.
227 +
@test unsafe fn malformedPackages() throws (testing::TestError) {
228 +
    let package = binary::Package {
229 +
        symbols: &["p"], name: "p", dependencies: &[], exports: &[], entry: nil,
230 +
        program: EMPTY,
231 +
    };
232 +
    static buffer: [u8; 64] = [0; 64];
233 +
    let length = try encodeInto((&mut buffer[..]) as *mut [u8], &package) catch {
234 +
        throw testing::TestError::Failed;
235 +
    };
236 +
    let memory = &mut MEMORY[..64];
237 +
    let mut arena = alloc::new(&mut memory[..]);
238 +
    for offset in &[0, 4, 17, 29] {
239 +
        let original = buffer[offset];
240 +
        set buffer[offset] = 255;
241 +
        alloc::restore(&mut arena, 8);
242 +
        let mut failed = false;
243 +
        try program::decode(&buffer[..length], &mut arena, LIMITS) catch {
244 +
            set failed = true;
245 +
        };
246 +
        try testing::expect(failed);
247 +
        try testing::expect(alloc::used(&arena) == 8);
248 +
        set buffer[offset] = original;
249 +
    }
250 +
    let mut trailing = false;
251 +
    try program::decode(&buffer[..length + 1], &mut arena, LIMITS) catch err {
252 +
        try testing::expect(err == binary::Error::Invalid);
253 +
        set trailing = true;
254 +
    };
255 +
    try testing::expect(trailing);
256 +
    try testing::expect(alloc::used(&arena) == 8);
257 +
}
258 +
259 +
/// Encode a malformed package and check rejection with arena rollback.
260 +
unsafe fn rejected(package: &binary::Package, limits: binary::Limits) throws (testing::TestError) {
261 +
    static buffer: [u8; 512] = [0; 512];
262 +
    let length = try encodeInto((&mut buffer[..]) as *mut [u8], package) catch {
263 +
        throw testing::TestError::Failed;
264 +
    };
265 +
    let memory = &mut MEMORY[..512];
266 +
    let mut arena = alloc::new(&mut memory[..]);
267 +
    alloc::restore(&mut arena, 8);
268 +
    let mut failed = false;
269 +
    try program::decode(&buffer[..length], &mut arena, limits) catch err {
270 +
        try testing::expect(err == binary::Error::Invalid);
271 +
        set failed = true;
272 +
    };
273 +
    try testing::expect(failed);
274 +
    try testing::expect(alloc::used(&arena) == 8);
275 +
}
276 +
277 +
/// Check initializer extent arithmetic, layout indices, and duplicate symbols.
278 +
@test unsafe fn invalidData() throws (testing::TestError) {
279 +
    let mut item = il::Data {
280 +
        name: "p", size: 0xffffffff, alignment: 8, layout: 0, readOnly: false, isZeroInit: false,
281 +
        values: &[
282 +
            il::DataValue { item: il::DataItem::Val { typ: il::Type::W64, val: 0 },
283 +
                count: 0xffffffff },
284 +
        ],
285 +
    };
286 +
    let mut package = binary::Package {
287 +
        symbols: &["p"], name: "p", dependencies: &[], exports: &[], entry: nil,
288 +
        program: il::Program {
289 +
            data: retainData(&[item]), fns: &[],
290 +
            layouts: &[il::Layout { size: 0xffffffff, alignment: 8, slots: &[] }], signatures: &[],
291 +
        },
292 +
    };
293 +
    try rejected(&package, LIMITS);
294 +
    set item.values = &[
295 +
        il::DataValue { item: il::DataItem::Undef, count: 1 },
296 +
        il::DataValue { item: il::DataItem::Undef, count: 1 },
297 +
    ];
298 +
    set package.program = il::Program {
299 +
        data: retainData(&[item]), fns: &[],
300 +
        layouts: &[il::Layout { size: 1, alignment: 1, slots: &[] }], signatures: &[],
301 +
    };
302 +
    try rejected(&package, LIMITS);
303 +
    // The layout index must name a layout of the package.
304 +
    set item.values = &[];
305 +
    set item.layout = 1;
306 +
    set package.program = il::Program {
307 +
        data: retainData(&[item]), fns: &[],
308 +
        layouts: &[il::Layout { size: 1, alignment: 1, slots: &[] }], signatures: &[],
309 +
    };
310 +
    try rejected(&package, LIMITS);
311 +
    set package.program = EMPTY;
312 +
    set package.symbols = &["p", "p"];
313 +
    try rejected(&package, LIMITS);
314 +
    set package.symbols = &["p", ""];
315 +
    try rejected(&package, LIMITS);
316 +
}
317 +
318 +
/// Store `func` in `arena` and return its address.
319 +
unsafe fn retainFn(func: il::Fn, arena: &mut alloc::Arena) -> *il::Fn {
320 +
    let mut funcs: *mut [il::Fn] = &mut [];
321 +
    try! funcs.append(func, arena);
322 +
    return &mut funcs[0];
323 +
}
324 +
325 +
/// Encode a package that holds only `func` and check its rejection.
326 +
unsafe fn rejectedFn(func: il::Fn, limits: binary::Limits) throws (testing::TestError) {
327 +
    static FIXTURE: [u8; 256] = [0; 256];
328 +
    let mut arena = alloc::new(&mut FIXTURE[..]);
329 +
    let mut list: *mut [*il::Fn] = &mut [];
330 +
    try! list.append(retainFn(func, &mut arena), &mut arena);
331 +
    let package = binary::Package {
332 +
        symbols: &["p"], name: "p", dependencies: &[], exports: &[], entry: nil,
333 +
        program: il::Program { data: &[], fns: &list[..], layouts: &[], signatures: &[] },
334 +
    };
335 +
    try rejected(&package, limits);
336 +
}
337 +
338 +
/// Check function block limits, parameter indices, and branch targets.
339 +
@test unsafe fn invalidFunctions() throws (testing::TestError) {
340 +
    let blocks: *[il::Block] = &[il::Block {
341 +
        label: il::BlockLabel { base: "entry", suffix: nil }, params: &[],
342 +
        instrs: &[il::Instr::Ret { val: nil }], locs: &[], preds: &[], loopDepth: 0,
343 +
    }];
344 +
    try rejectedFn(il::Fn {
345 +
        name: "p", params: &[], returnType: il::ValType::Data(il::Type::W64), isExtern: false,
346 +
            isLeaf: true,
347 +
        blocks, vals: &[], cases: &[],
348 +
    }, binary::Limits { registers: 16, blocks: 0 });
349 +
    try rejectedFn(il::Fn {
350 +
        name: "p", params: &[], returnType: il::ValType::Data(il::Type::W64), isExtern: true,
351 +
            isLeaf: true,
352 +
        blocks, vals: &[], cases: &[],
353 +
    }, LIMITS);
354 +
    try rejectedFn(il::Fn {
355 +
        name: "p", params: &[il::Param { value: il::Reg { n: 16 }, type: il::ValType::Data(
356 +
            il::Type::W64) }],
357 +
        returnType: il::ValType::Data(il::Type::W64), isExtern: false, isLeaf: true,
358 +
        blocks: &[], vals: &[], cases: &[],
359 +
    }, LIMITS);
360 +
    try rejectedFn(il::Fn {
361 +
        name: "p", params: &[], returnType: il::ValType::Data(il::Type::W64), isExtern: false,
362 +
            isLeaf: true,
363 +
        blocks: &[il::Block {
364 +
            label: il::BlockLabel { base: "entry", suffix: nil }, params: &[],
365 +
            instrs: &[il::Instr::Jmp { target: 1, args: il::ArgRange { start: 0, len: 0 } }],
366 +
            locs: &[], preds: &[], loopDepth: 0,
367 +
        }],
368 +
        vals: &[], cases: &[],
369 +
    }, LIMITS);
370 +
}
371 +
372 +
/// Check that the indexed writer yields the bytes of a whole-package encoding,
373 +
/// and that the buffer and the index return to their owners after each call.
374 +
@test unsafe fn indexedEncoding() throws (testing::TestError) {
375 +
    static whole: [u8; 128] = [0; 128];
376 +
    static split: [u8; 128] = [0; 128];
377 +
    static slots: [dict::Entry; 8] = [dict::Entry { key: "", value: 0 }; 8];
378 +
    static fixture: [u8; 256] = [0; 256];
379 +
    let mut arena = alloc::new(&mut fixture[..]);
380 +
    let func = retainFn(il::Fn {
381 +
        name: "p::f", params: &[], returnType: il::ValType::Data(il::Type::W64), isExtern: false,
382 +
            isLeaf: true,
383 +
        blocks: &[il::Block {
384 +
            label: il::BlockLabel { base: "entry", suffix: nil }, params: &[],
385 +
            instrs: &[il::Instr::Ret { val: nil }], locs: &[], preds: &[], loopDepth: 0,
386 +
        }],
387 +
        vals: &[], cases: &[],
388 +
    }, &mut arena);
389 +
    let mut list: *mut [*il::Fn] = &mut [];
390 +
    try! list.append(func, &mut arena);
391 +
    let package = binary::Package {
392 +
        symbols: &["p", "p::f"], name: "p", dependencies: &[], exports: &[], entry: nil,
393 +
        program: il::Program { data: &[], fns: &list[..], layouts: &[], signatures: &[] },
394 +
    };
395 +
    let length = try encodeInto((&mut whole[..]) as *mut [u8], &package) catch {
396 +
        throw testing::TestError::Failed;
397 +
    };
398 +
    let mut index = dict::init((&mut slots[..]) as *mut [dict::Entry]);
399 +
    try dict::insert(&mut index, "p", 0) catch {
400 +
        throw testing::TestError::Failed;
401 +
    };
402 +
    try dict::insert(&mut index, "p::f", 1) catch {
403 +
        throw testing::TestError::Failed;
404 +
    };
405 +
    let mut bytes = (&mut split[..]) as *mut [u8];
406 +
    let head = try program::encodeHead(&mut bytes, &package, &mut index, 1) catch {
407 +
        throw testing::TestError::Failed;
408 +
    };
409 +
    try testing::expect(bytes.len == 128 and index.count == 2 and dict::get(&index, "p::f") == 1);
410 +
    let end = try program::appendFunction(&mut bytes, head, &mut index, func) catch {
411 +
        throw testing::TestError::Failed;
412 +
    };
413 +
    try testing::expect(bytes.len == 128 and index.count == 2 and dict::get(&index, "p") == 0);
414 +
    try testing::expectBytesEq(&whole[..length], &bytes[..end]);
415 +
416 +
    // A short buffer and an unknown name fail, and both owners get their storage back.
417 +
    let mut short = (&mut split[..head]) as *mut [u8];
418 +
    let mut failed = false;
419 +
    try program::appendFunction(&mut short, head - 1, &mut index, func) catch err {
420 +
        try testing::expect(err == binary::Error::Capacity);
421 +
        set failed = true;
422 +
    };
423 +
    try testing::expect(failed and short.len == head and dict::get(&index, "p::f") == 1);
424 +
    let mut missing = dict::init((&mut slots[..]) as *mut [dict::Entry]);
425 +
    try dict::insert(&mut missing, "p", 0) catch {
426 +
        throw testing::TestError::Failed;
427 +
    };
428 +
    set failed = false;
429 +
    try program::appendFunction(&mut bytes, head, &mut missing, func) catch err {
430 +
        try testing::expect(err == binary::Error::Symbol);
431 +
        set failed = true;
432 +
    };
433 +
    try testing::expect(failed and bytes.len == 128 and dict::get(&missing, "p") == 0);
434 +
}
435 +
436 +
/// Reject unknown initializer tags and widths before reading their payloads.
437 +
@test unsafe fn malformedInitializers() throws (testing::TestError) {
438 +
    let memory = &mut MEMORY[..8];
439 +
    let mut arena = alloc::new(&mut memory[..]);
440 +
    let fixtures: [*[u8]; 2] = [&[255], &[0, 3]];
441 +
    for bytes in &fixtures[..] {
442 +
        let mut input = reader::new();
443 +
        let mut failed = false;
444 +
        try reader::dataValue(&mut input, bytes, &mut arena) catch err {
445 +
            try testing::expect(err == binary::Error::Invalid);
446 +
            set failed = true;
447 +
        };
448 +
        try testing::expect(failed);
449 +
    }
450 +
}
451 +
452 +
/// Collect the names of `local` for package `owner` in `arena`.
453 +
unsafe fn collected(owner: *[u8], local: il::Program, arena: &mut alloc::Arena)
454 +
    -> binary::Package throws (binary::Error)
455 +
{
456 +
    let mut names = try collect::new(64, arena);
457 +
    return try collect::package(&mut names, owner, local, &[], nil, arena);
458 +
}
459 +
460 +
/// Check symbol collection storage exhaustion and package ownership failures.
461 +
@test unsafe fn collectionBounds() throws (testing::TestError) {
462 +
    let local = il::Program {
463 +
        data: &[il::Data {
464 +
            name: "p::data", size: 8, alignment: 8, layout: 0, readOnly: false, isZeroInit: false,
465 +
            values: &[il::DataValue { item: il::DataItem::Fn("dep::fn"), count: 1 }],
466 +
        }],
467 +
        fns: &[], layouts: &[], signatures: &[],
468 +
    };
469 +
    let mut arena = alloc::new(&mut MEMORY[..]);
470 +
    let mut failed = false;
471 +
    try collected("other", local, &mut arena) catch err {
472 +
        try testing::expect(err == binary::Error::Invalid);
473 +
        set failed = true;
474 +
    };
475 +
    try testing::expect(failed);
476 +
    alloc::restore(&mut arena, 0);
477 +
    let package = try collected("p", local, &mut arena) catch {
478 +
        throw testing::TestError::Failed;
479 +
    };
480 +
    try testing::expect(package.dependencies.len == 1);
481 +
    try testing::expectBytesEq(package.dependencies[0], "dep");
482 +
    try testing::expect(package.symbols.len == 4);
483 +
    let required = alloc::used(&arena);
484 +
    for capacity in 0..required {
485 +
        let mut short = alloc::new(&mut MEMORY[..capacity]);
486 +
        set failed = false;
487 +
        try collected("p", local, &mut short) catch err {
488 +
            try testing::expect(err == binary::Error::Storage);
489 +
            set failed = true;
490 +
        };
491 +
        try testing::expect(failed);
492 +
    }
493 +
}
494 +
495 +
/// Copy data fixtures into stable storage for the package descriptor.
496 +
unsafe fn retainData(items: &[il::Data]) -> *[il::Data] {
497 +
    unsafe static DATA: [il::Data; 2] = undefined;
498 +
    assert items.len <= DATA.len;
499 +
    for item, i in items {
500 +
        set DATA[i] = item;
501 +
    }
502 +
    return &DATA[..items.len];
503 +
}
504 +
505 +
/// Layout of eight bytes of plain data.
506 +
constant PLAIN: il::Layout = il::Layout { size: 8, alignment: 8, slots: &[] };
507 +
508 +
/// Mutable reference to layout 1, the type of the pointer parameters of the fixtures.
509 +
constant REF_L1: il::PtrType = il::PtrType { layout: 1, own: false, mutable: true,
510 +
    nullable: false };
511 +
512 +
/// Owned nullable pointer to layout 0 and to layout 1, as stored in a slot.
513 +
constant SLOT_L0: il::PtrType = il::PtrType { layout: 0, own: true, mutable: false,
514 +
    nullable: true };
515 +
516 +
/// Layout of a pointer-sized object that holds an owned pointer to layout 0.
517 +
constant PTR_LAYOUT: il::Layout = il::Layout {
518 +
    size: 8, alignment: 8,
519 +
    slots: &[il::Slot { offset: 0, kind: il::SlotKind::Ptr(
520 +
        il::PtrType { layout: 0, own: true, mutable: true, nullable: true }) }],
521 +
};
522 +
523 +
/// Decode `package` after encoding it, in a test arena that the caller resets.
524 +
unsafe fn reencoded(package: &binary::Package, arena: &mut alloc::Arena) -> binary::Package
525 +
    throws (testing::TestError)
526 +
{
527 +
    static buffer: [u8; 2048] = [0; 2048];
528 +
    let length = try encodeInto((&mut buffer[..]) as *mut [u8], package) catch {
529 +
        throw testing::TestError::Failed;
530 +
    };
531 +
    return try program::decode(&buffer[..length], arena, LIMITS) catch {
532 +
        throw testing::TestError::Failed;
533 +
    };
534 +
}
535 +
536 +
/// Check that layouts, signatures, and value types survive a round trip.
537 +
@test unsafe fn typedPackage() throws (testing::TestError) {
538 +
    let layouts: *[il::Layout] = &[
539 +
        il::Layout { size: 8, alignment: 8, slots: &[] },
540 +
        PTR_LAYOUT,
541 +
        il::Layout { size: 48, alignment: 8, slots: &[
542 +
            il::Slot { offset: 0, kind: il::SlotKind::Sub(1) },
543 +
            il::Slot { offset: 16, kind: il::SlotKind::Slice(SLOT_L0) },
544 +
            il::Slot { offset: 32, kind: il::SlotKind::Array { layout: 1, count: 2 } },
545 +
        ] },
546 +
    ];
547 +
    let signatures: *[il::Signature] = &[
548 +
        il::Signature { params: &[il::ValType::Ptr(REF_L1), il::ValType::Data(il::Type::W32)],
549 +
            ret: il::ValType::Void },
550 +
        il::Signature { params: &[], ret: il::ValType::Fn(0) },
551 +
    ];
552 +
    let blocks: *[il::Block] = &[il::Block {
553 +
        label: il::BlockLabel { base: "entry", suffix: nil },
554 +
        params: &[],
555 +
        instrs: &[
556 +
            il::Instr::Reserve { dst: il::Reg { n: 2 }, layout: 2 },
557 +
            il::Instr::Blit { dst: il::Reg { n: 2 }, src: il::Reg { n: 0 }, layout: 2 },
558 +
            il::Instr::Ret { val: il::Val::Reg(il::Reg { n: 2 }) },
559 +
        ],
560 +
        locs: &[], preds: &[], loopDepth: 0,
561 +
    }];
562 +
    static FIXTURE: [u8; 512] = [0; 512];
563 +
    let mut fixture = alloc::new(&mut FIXTURE[..]);
564 +
    let func = retainFn(il::Fn {
565 +
        name: "p::f",
566 +
        params: &[
567 +
            il::Param { value: il::Reg { n: 0 }, type: il::ValType::Ptr(REF_L1) },
568 +
            il::Param { value: il::Reg { n: 1 }, type: il::ValType::Fn(1) },
569 +
        ],
570 +
        returnType: il::ValType::Ptr(il::PtrType { layout: 2, own: true, mutable: false,
571 +
            nullable: true }),
572 +
        isExtern: false, isLeaf: true, blocks, vals: &[], cases: &[],
573 +
    }, &mut fixture);
574 +
    let mut list: *mut [*il::Fn] = &mut [];
575 +
    try! list.append(func, &mut fixture);
576 +
    let package = binary::Package {
577 +
        symbols: &["p", "p::f"], name: "p", dependencies: &[], exports: &[], entry: nil,
578 +
        program: il::Program { data: &[], fns: &list[..], layouts, signatures },
579 +
    };
580 +
    try roundTrip(&package);
581 +
582 +
    let mut arena = alloc::new(&mut MEMORY[..]);
583 +
    let decoded = try reencoded(&package, &mut arena);
584 +
    try testing::expect(decoded.program.layouts.len == 3 and decoded.program.signatures.len == 2);
585 +
    for expected, i in layouts {
586 +
        let actual = decoded.program.layouts[i];
587 +
        try testing::expect(actual.size == expected.size and
588 +
            actual.alignment == expected.alignment);
589 +
        try testing::expect(actual.slots.len == expected.slots.len);
590 +
        for slot, j in expected.slots {
591 +
            try testing::expect(actual.slots[j] == slot);
592 +
        }
593 +
    }
594 +
    for expected, i in signatures {
595 +
        let actual = decoded.program.signatures[i];
596 +
        try testing::expect(actual.ret == expected.ret and
597 +
            actual.params.len == expected.params.len);
598 +
        for param, j in expected.params {
599 +
            try testing::expect(actual.params[j] == param);
600 +
        }
601 +
    }
602 +
    let decodedFn = decoded.program.fns[0];
603 +
    try testing::expect(decodedFn.params[0].type == il::ValType::Ptr(REF_L1));
604 +
    try testing::expect(decodedFn.returnType == func.returnType);
605 +
    try testing::expect(decodedFn.blocks[0].instrs[0] == il::Instr::Reserve { dst: il::Reg {
606 +
        n: 2 }, layout: 2 });
607 +
}
608 +
609 +
/// Encode a package with the given tables and check that decoding rejects it.
610 +
unsafe fn rejectedLayouts(layouts: *[il::Layout], signatures: *[il::Signature]) throws (
611 +
    testing::TestError) {
612 +
    let package = binary::Package {
613 +
        symbols: &["p"], name: "p", dependencies: &[], exports: &[], entry: nil,
614 +
        program: il::Program { data: &[], fns: &[], layouts, signatures },
615 +
    };
616 +
    try rejected(&package, LIMITS);
617 +
}
618 +
619 +
/// Owned nullable pointer to layout 1, as stored in a slot.
620 +
constant SLOT_L1: il::PtrType = il::PtrType { layout: 1, own: true, mutable: false,
621 +
    nullable: true };
622 +
623 +
/// Check the structural rules of layouts: alignment, slot order, extent, and nesting.
624 +
@test unsafe fn invalidLayouts() throws (testing::TestError) {
625 +
    // Alignment is a power of two.
626 +
    try rejectedLayouts(&[il::Layout { size: 8, alignment: 0, slots: &[] }], &[]);
627 +
    try rejectedLayouts(&[il::Layout { size: 8, alignment: 3, slots: &[] }], &[]);
628 +
    // Size is a multiple of the alignment, so that array elements stay aligned.
629 +
    try rejectedLayouts(&[il::Layout { size: 12, alignment: 8, slots: &[] }], &[]);
630 +
    try rejectedLayouts(&[il::Layout { size: 1, alignment: 2, slots: &[] }], &[]);
631 +
    // Slots are aligned, sorted, disjoint, inside the size, and fit the alignment.
632 +
    try rejectedLayouts(&[il::Layout { size: 16, alignment: 8, slots: &[il::Slot { offset: 4,
633 +
        kind: il::SlotKind::Ptr(SLOT_L0) }] }], &[]);
634 +
    try rejectedLayouts(&[il::Layout { size: 16, alignment: 8, slots: &[il::Slot { offset: 8,
635 +
        kind: il::SlotKind::Ptr(SLOT_L0) }, il::Slot { offset: 0, kind: il::SlotKind::Ptr(SLOT_L0)
636 +
            }] }], &[]);
637 +
    try rejectedLayouts(&[il::Layout { size: 16, alignment: 8, slots: &[il::Slot { offset: 0,
638 +
        kind: il::SlotKind::Ptr(SLOT_L0) }, il::Slot { offset: 0, kind: il::SlotKind::Ptr(SLOT_L0)
639 +
            }] }], &[]);
640 +
    try rejectedLayouts(&[il::Layout { size: 24, alignment: 8, slots: &[
641 +
        il::Slot { offset: 0, kind: il::SlotKind::Slice(SLOT_L0) },
642 +
        il::Slot { offset: 8, kind: il::SlotKind::Ptr(SLOT_L0) },
643 +
    ] }], &[]);
644 +
    try rejectedLayouts(&[il::Layout { size: 8, alignment: 8, slots: &[il::Slot { offset: 8,
645 +
        kind: il::SlotKind::Ptr(SLOT_L0) }] }], &[]);
646 +
    try rejectedLayouts(&[il::Layout { size: 12, alignment: 8, slots: &[il::Slot { offset: 8,
647 +
        kind: il::SlotKind::Ptr(SLOT_L0) }] }], &[]);
648 +
    try rejectedLayouts(&[il::Layout { size: 8, alignment: 4, slots: &[il::Slot { offset: 0,
649 +
        kind: il::SlotKind::Ptr(SLOT_L0) }] }], &[]);
650 +
    // Nested layouts have lower indices.
651 +
    try rejectedLayouts(&[il::Layout { size: 8, alignment: 8, slots: &[
652 +
        il::Slot { offset: 0, kind: il::SlotKind::Sub(0) },
653 +
    ] }], &[]);
654 +
    try rejectedLayouts(&[il::Layout { size: 8, alignment: 8, slots: &[
655 +
        il::Slot { offset: 0, kind: il::SlotKind::Sub(1) },
656 +
    ] }, PLAIN], &[]);
657 +
    try rejectedLayouts(&[PLAIN, il::Layout { size: 16, alignment: 8, slots: &[
658 +
        il::Slot { offset: 0, kind: il::SlotKind::Array { layout: 1, count: 2 } },
659 +
    ] }], &[]);
660 +
    // Nested objects fit in the layout, also when their size overflows 32 bits.
661 +
    try rejectedLayouts(&[PLAIN, il::Layout { size: 8, alignment: 8, slots: &[
662 +
        il::Slot { offset: 0, kind: il::SlotKind::Array { layout: 0, count: 2 } },
663 +
    ] }], &[]);
664 +
    try rejectedLayouts(&[il::Layout { size: 0x10000, alignment: 8, slots: &[] },
665 +
        il::Layout { size: 0xffffffff, alignment: 8, slots: &[
666 +
            il::Slot { offset: 0, kind: il::SlotKind::Array { layout: 0, count: 0x10000 } },
667 +
        ] }], &[]);
668 +
    try rejectedLayouts(&[PLAIN, il::Layout { size: 8, alignment: 8, slots: &[
669 +
        il::Slot { offset: 4, kind: il::SlotKind::Sub(0) },
670 +
    ] }], &[]);
671 +
    // Indices of pointees and signatures name table entries.
672 +
    try rejectedLayouts(&[il::Layout { size: 8, alignment: 8, slots: &[il::Slot { offset: 0,
673 +
        kind: il::SlotKind::Ptr(SLOT_L1) }] }], &[]);
674 +
    try rejectedLayouts(&[il::Layout { size: 8, alignment: 8, slots: &[
675 +
        il::Slot { offset: 0, kind: il::SlotKind::Fn(0) },
676 +
    ] }], &[]);
677 +
    // A slot is nullable in memory.
678 +
    try rejectedLayouts(&[il::Layout { size: 8, alignment: 8, slots: &[
679 +
        il::Slot { offset: 0, kind: il::SlotKind::Ptr(
680 +
            il::PtrType { layout: 0, own: true, mutable: false, nullable: false }) },
681 +
    ] }], &[]);
682 +
    // Signatures refer to existing layouts and signatures.
683 +
    try rejectedLayouts(&[PLAIN], &[il::Signature {
684 +
        params: &[il::ValType::Ptr(il::PtrType { layout: 1, own: true, mutable: false,
685 +
            nullable: false })],
686 +
        ret: il::ValType::Void,
687 +
    }]);
688 +
    try rejectedLayouts(&[PLAIN], &[il::Signature { params: &[], ret: il::ValType::Fn(1) }]);
689 +
    // The wire forms of a valid table are accepted.
690 +
    let ok = binary::Package {
691 +
        symbols: &["p"], name: "p", dependencies: &[], exports: &[], entry: nil,
692 +
        program: il::Program { data: &[], fns: &[], layouts: &[PLAIN, PTR_LAYOUT], signatures: &[
693 +
            il::Signature { params: &[], ret: il::ValType::Fn(0) },
694 +
        ] },
695 +
    };
696 +
    try roundTrip(&ok);
697 +
}
698 +
699 +
/// Arguments of a branch that passes none.
700 +
constant NO_ARGS: il::ArgRange = il::ArgRange { start: 0, len: 0 };
701 +
702 +
/// Build a block that ends in `last`, with the instruction stored in `arena`.
703 +
unsafe fn endBlock(last: il::Instr, arena: &mut alloc::Arena) -> il::Block {
704 +
    let mut instrs: *mut [il::Instr] = &mut [];
705 +
    try! instrs.append(last, arena);
706 +
    return il::Block {
707 +
        label: il::BlockLabel { base: "b", suffix: nil }, params: &[], instrs: &instrs[..],
708 +
        locs: &[], preds: &[], loopDepth: 0,
709 +
    };
710 +
}
711 +
712 +
/// Instruction that jumps to `target` without arguments.
713 +
fn jump(target: u32) -> il::Instr {
714 +
    return il::Instr::Jmp { target, args: NO_ARGS };
715 +
}
716 +
717 +
/// Instruction that branches to `thenTarget` or `elseTarget` without arguments.
718 +
fn branch(thenTarget: u32, elseTarget: u32) -> il::Instr {
719 +
    return il::Instr::Br {
720 +
        op: il::CmpOp::Eq, typ: il::Type::W32, a: il::Val::Imm(0), b: il::Val::Imm(0),
721 +
        thenTarget, thenArgs: NO_ARGS, elseTarget, elseArgs: NO_ARGS,
722 +
    };
723 +
}
724 +
725 +
/// Check that the decoder derives predecessors and loop depths from the terminators.
726 +
@test unsafe fn controlFlow() throws (testing::TestError) {
727 +
    // Blocks 1 and 2 head an outer and an inner loop. Block 5 switches to the same
728 +
    // target twice, and blocks 6 and 7 return.
729 +
    static FIXTURE: [u8; 4096] = [0; 4096];
730 +
    let mut fixture = alloc::new(&mut FIXTURE[..]);
731 +
    let switchInstr = il::Instr::Switch {
732 +
        val: il::Val::Imm(0), defaultTarget: 6, defaultArgs: NO_ARGS,
733 +
        cases: il::CaseRange { start: 0, len: 2 },
734 +
    };
735 +
    let terminators = [
736 +
        jump(1), branch(2, 5), branch(3, 4), jump(2), jump(1), switchInstr,
737 +
        il::Instr::Ret { val: nil }, il::Instr::Ret { val: nil },
738 +
    ];
739 +
    let mut list: *mut [il::Block] = &mut [];
740 +
    for terminator in &terminators[..] {
741 +
        try! list.append(endBlock(terminator, &mut fixture), &mut fixture);
742 +
    }
743 +
    let blocks: *[il::Block] = &list[..];
744 +
    let cases: *[il::SwitchCase] = &[
745 +
        il::SwitchCase { value: 0, target: 6, args: NO_ARGS }, il::SwitchCase { value: 1,
746 +
            target: 7, args: NO_ARGS },
747 +
    ];
748 +
    let func = retainFn(il::Fn {
749 +
        name: "p::f", params: &[], returnType: il::ValType::Void, isExtern: false, isLeaf: true,
750 +
        blocks, vals: &[], cases,
751 +
    }, &mut fixture);
752 +
    let mut fns: *mut [*il::Fn] = &mut [];
753 +
    try! fns.append(func, &mut fixture);
754 +
    let package = binary::Package {
755 +
        symbols: &["p", "p::f"], name: "p", dependencies: &[], exports: &[], entry: nil,
756 +
        program: il::Program { data: &[], fns: &fns[..], layouts: &[], signatures: &[] },
757 +
    };
758 +
    static buffer: [u8; 512] = [0; 512];
759 +
    let length = try encodeInto((&mut buffer[..]) as *mut [u8], &package) catch {
760 +
        throw testing::TestError::Failed;
761 +
    };
762 +
    static DECODE: [u8; 4096] = [0; 4096];
763 +
    let mut arena = alloc::new(&mut DECODE[..]);
764 +
    let decoded = try program::decode(&buffer[..length], &mut arena, binary::Limits {
765 +
        registers: 16, blocks: 8 })
766 +
        catch {
767 +
            throw testing::TestError::Failed;
768 +
        };
769 +
    let expectedPreds: [*[u32]; 8] = [&[], &[0, 4], &[1, 3], &[2], &[2], &[1], &[5], &[5]];
770 +
    let expectedDepth: [u32; 8] = [0, 1, 2, 2, 1, 0, 0, 0];
771 +
    let decodedBlocks = decoded.program.fns[0].blocks;
772 +
    try testing::expect(decodedBlocks.len == 8);
773 +
    for block, i in decodedBlocks {
774 +
        try testing::expect(block.preds.len == expectedPreds[i].len);
775 +
        for pred, j in block.preds {
776 +
            try testing::expect(pred == expectedPreds[i][j]);
777 +
        }
778 +
        try testing::expect(block.loopDepth == expectedDepth[i]);
779 +
    }
780 +
}
lib/std/lang/il/binary/writer.rad added +492 -0
1 +
//! Bounded binary RIL encoding into caller-owned storage.
2 +
//! After an error, discard bytes written by the failed operation.
3 +
4 +
use std::lang::il;
5 +
use std::mem;
6 +
use std::lang::il::binary;
7 +
use std::collections::dict;
8 +
9 +
/// Output buffer, cursor and the symbol table used by this package.
10 +
/// The writer owns the buffer and the index while it is in use. The caller moves
11 +
/// them out with `take` before a write and back after it.
12 +
export record Writer {
13 +
    /// Destination bytes.
14 +
    bytes: *mut [u8],
15 +
    /// Number of bytes written.
16 +
    offset: u32,
17 +
    /// Names in wire-index order. The caller supplies unique names.
18 +
    symbols: *[*[u8]],
19 +
    /// Optional map from name to wire index. It replaces the scan of `symbols`
20 +
    /// and is in use when it has slots.
21 +
    index: dict::Dict,
22 +
}
23 +
24 +
/// Create an output cursor over `bytes` that scans `symbols` for names.
25 +
export fn new(bytes: *mut [u8], symbols: *[*[u8]]) -> Writer {
26 +
    return Writer { bytes, offset: 0, symbols, index: dict::Dict { entries: &mut [], count: 0 } };
27 +
}
28 +
29 +
/// Create an output cursor at `offset` that maps names to wire indices with `index`.
30 +
/// The index must have slots and must remain unchanged while the cursor is in use.
31 +
export fn withIndex(bytes: *mut [u8], offset: u32, index: dict::Dict) -> Writer {
32 +
    assert offset <= bytes.len;
33 +
    assert index.entries.len > 0;
34 +
    return Writer { bytes, offset, symbols: &[], index };
35 +
}
36 +
37 +
/// Number of bytes still free in the output buffer.
38 +
fn space(out: &Writer) -> u32 {
39 +
    return out.bytes.len - out.offset;
40 +
}
41 +
42 +
/// Write the low bytes of an integer with width 1, 2, 4, or 8.
43 +
export fn integer(out: &mut Writer, value: u64, width: u32) throws (binary::Error) {
44 +
    if width <> 1 and width <> 2 and width <> 4 and width <> 8 {
45 +
        throw binary::Error::Invalid;
46 +
    }
47 +
    if width > space(out) {
48 +
        throw binary::Error::Capacity;
49 +
    }
50 +
    for i in 0..width {
51 +
        set out.bytes[out.offset + i] = (value >> (i * 8) as u64) as u8;
52 +
    }
53 +
    set out.offset += width;
54 +
}
55 +
56 +
/// Write a byte sequence with its u32 length.
57 +
export fn bytes(out: &mut Writer, value: &[u8]) throws (binary::Error) {
58 +
    try integer(out, value.len as u64, 4);
59 +
    if value.len > space(out) {
60 +
        throw binary::Error::Capacity;
61 +
    }
62 +
    for b, i in value {
63 +
        set out.bytes[out.offset + i] = b;
64 +
    }
65 +
    set out.offset += value.len;
66 +
}
67 +
68 +
/// Write the index of a name in the package symbol table.
69 +
export fn symbol(out: &mut Writer, name: *[u8]) throws (binary::Error) {
70 +
    if out.index.entries.len > 0 {
71 +
        let i = dict::get(&out.index, name) else {
72 +
            throw binary::Error::Symbol;
73 +
        };
74 +
        try integer(out, i as u32 as u64, 4);
75 +
        return;
76 +
    }
77 +
    for candidate, i in out.symbols {
78 +
        if mem::eq(candidate, name) {
79 +
            try integer(out, i as u64, 4);
80 +
            return;
81 +
        }
82 +
    }
83 +
    throw binary::Error::Symbol;
84 +
}
85 +
86 +
/// Write an IL type as its byte width.
87 +
export fn typ(out: &mut Writer, value: il::Type) throws (binary::Error) {
88 +
    try integer(out, il::typeSize(value) as u64, 1);
89 +
}
90 +
91 +
/// Write a pointer type: the pointee layout index and a flag byte.
92 +
export fn ptrType(out: &mut Writer, value: il::PtrType) throws (binary::Error) {
93 +
    try integer(out, value.layout as u64, 4);
94 +
    let mut flags: u8 = 0;
95 +
    if value.own {
96 +
        set flags |= super::PTR_OWN;
97 +
    }
98 +
    if value.mutable {
99 +
        set flags |= super::PTR_MUTABLE;
100 +
    }
101 +
    if value.nullable {
102 +
        set flags |= super::PTR_NULLABLE;
103 +
    }
104 +
    try integer(out, flags as u64, 1);
105 +
}
106 +
107 +
/// Write a value type as a tag followed by its payload.
108 +
export fn valType(out: &mut Writer, value: il::ValType) throws (binary::Error) {
109 +
    match value {
110 +
        case il::ValType::Void => try integer(out, super::VALTYPE_VOID as u64, 1),
111 +
        case il::ValType::Data(t) => {
112 +
            try integer(out, super::VALTYPE_DATA as u64, 1);
113 +
            try typ(out, t);
114 +
        },
115 +
        case il::ValType::Ptr(ptr) => {
116 +
            try integer(out, super::VALTYPE_PTR as u64, 1);
117 +
            try ptrType(out, ptr);
118 +
        },
119 +
        case il::ValType::Fn(signature) => {
120 +
            try integer(out, super::VALTYPE_FN as u64, 1);
121 +
            try integer(out, signature as u64, 4);
122 +
        },
123 +
    }
124 +
}
125 +
126 +
/// Write one layout: size, alignment, and its slots.
127 +
export fn layout(out: &mut Writer, value: il::Layout) throws (binary::Error) {
128 +
    try integer(out, value.size as u64, 4);
129 +
    try integer(out, value.alignment as u64, 4);
130 +
    try integer(out, value.slots.len as u64, 4);
131 +
    for slot in value.slots {
132 +
        try integer(out, slot.offset as u64, 4);
133 +
        match slot.kind {
134 +
            case il::SlotKind::Ptr(ptr) => {
135 +
                try integer(out, super::SLOT_PTR as u64, 1);
136 +
                try ptrType(out, ptr);
137 +
            },
138 +
            case il::SlotKind::Slice(ptr) => {
139 +
                try integer(out, super::SLOT_SLICE as u64, 1);
140 +
                try ptrType(out, ptr);
141 +
            },
142 +
            case il::SlotKind::Fn(signature) => {
143 +
                try integer(out, super::SLOT_FN as u64, 1);
144 +
                try integer(out, signature as u64, 4);
145 +
            },
146 +
            case il::SlotKind::Sub(sub) => {
147 +
                try integer(out, super::SLOT_SUB as u64, 1);
148 +
                try integer(out, sub as u64, 4);
149 +
            },
150 +
            case il::SlotKind::Array { layout: element, count } => {
151 +
                try integer(out, super::SLOT_ARRAY as u64, 1);
152 +
                try integer(out, element as u64, 4);
153 +
                try integer(out, count as u64, 4);
154 +
            },
155 +
        }
156 +
    }
157 +
}
158 +
159 +
/// Write one signature: the parameter types and the result type.
160 +
export fn signature(out: &mut Writer, value: il::Signature) throws (binary::Error) {
161 +
    try integer(out, value.params.len as u64, 4);
162 +
    for param in value.params {
163 +
        try valType(out, param);
164 +
    }
165 +
    try valType(out, value.ret);
166 +
}
167 +
168 +
/// Write a tagged value. Symbol addresses use table indices.
169 +
export fn val(out: &mut Writer, value: il::Val) throws (binary::Error) {
170 +
    match value {
171 +
        case il::Val::Reg(reg) => {
172 +
            try integer(out, super::VALUE_REG as u64, 1);
173 +
            try integer(out, reg.n as u64, 4);
174 +
        },
175 +
        case il::Val::Imm(n) => {
176 +
            try integer(out, super::VALUE_IMM as u64, 1);
177 +
            try integer(out, n as u64, 8);
178 +
        },
179 +
        case il::Val::DataSym(name) => {
180 +
            try integer(out, super::VALUE_DATASYM as u64, 1);
181 +
            try symbol(out, name);
182 +
        },
183 +
        case il::Val::FnAddr(name) => {
184 +
            try integer(out, super::VALUE_FNADDR as u64, 1);
185 +
            try symbol(out, name);
186 +
        },
187 +
        case il::Val::Undef => try integer(out, super::VALUE_UNDEF as u64, 1),
188 +
    }
189 +
}
190 +
191 +
/// Write a sequence of values.
192 +
export fn values(out: &mut Writer, items: &[il::Val]) throws (binary::Error) {
193 +
    try integer(out, items.len as u64, 4);
194 +
    for item in items {
195 +
        try val(out, item);
196 +
    }
197 +
}
198 +
199 +
/// Write an explicit bin operation tag.
200 +
fn binOp(out: &mut Writer, value: il::BinOp) throws (binary::Error) {
201 +
    match value {
202 +
        case il::BinOp::Add => try integer(out, super::BIN_ADD as u64, 1),
203 +
        case il::BinOp::Sub => try integer(out, super::BIN_SUB as u64, 1),
204 +
        case il::BinOp::Mul => try integer(out, super::BIN_MUL as u64, 1),
205 +
        case il::BinOp::Sdiv => try integer(out, super::BIN_SDIV as u64, 1),
206 +
        case il::BinOp::Udiv => try integer(out, super::BIN_UDIV as u64, 1),
207 +
        case il::BinOp::Srem => try integer(out, super::BIN_SREM as u64, 1),
208 +
        case il::BinOp::Urem => try integer(out, super::BIN_UREM as u64, 1),
209 +
        case il::BinOp::Eq => try integer(out, super::BIN_EQ as u64, 1),
210 +
        case il::BinOp::Ne => try integer(out, super::BIN_NE as u64, 1),
211 +
        case il::BinOp::Slt => try integer(out, super::BIN_SLT as u64, 1),
212 +
        case il::BinOp::Sge => try integer(out, super::BIN_SGE as u64, 1),
213 +
        case il::BinOp::Ult => try integer(out, super::BIN_ULT as u64, 1),
214 +
        case il::BinOp::Uge => try integer(out, super::BIN_UGE as u64, 1),
215 +
        case il::BinOp::And => try integer(out, super::BIN_AND as u64, 1),
216 +
        case il::BinOp::Or => try integer(out, super::BIN_OR as u64, 1),
217 +
        case il::BinOp::Xor => try integer(out, super::BIN_XOR as u64, 1),
218 +
        case il::BinOp::Shl => try integer(out, super::BIN_SHL as u64, 1),
219 +
        case il::BinOp::Sshr => try integer(out, super::BIN_SSHR as u64, 1),
220 +
        case il::BinOp::Ushr => try integer(out, super::BIN_USHR as u64, 1),
221 +
    }
222 +
}
223 +
224 +
/// Write an explicit un operation tag.
225 +
fn unOp(out: &mut Writer, value: il::UnOp) throws (binary::Error) {
226 +
    match value {
227 +
        case il::UnOp::Neg => try integer(out, super::UN_NEG as u64, 1),
228 +
        case il::UnOp::Not => try integer(out, super::UN_NOT as u64, 1),
229 +
    }
230 +
}
231 +
232 +
/// Write an explicit cmp operation tag.
233 +
fn cmpOp(out: &mut Writer, value: il::CmpOp) throws (binary::Error) {
234 +
    match value {
235 +
        case il::CmpOp::Eq => try integer(out, super::CMP_EQ as u64, 1),
236 +
        case il::CmpOp::Ne => try integer(out, super::CMP_NE as u64, 1),
237 +
        case il::CmpOp::Slt => try integer(out, super::CMP_SLT as u64, 1),
238 +
        case il::CmpOp::Ult => try integer(out, super::CMP_ULT as u64, 1),
239 +
    }
240 +
}
241 +
242 +
/// Write one instruction of `func`. Fields follow the IL record declaration order.
243 +
/// Argument and case ranges are read from the pools of `func`.
244 +
export fn instr(out: &mut Writer, func: &il::Fn, item: il::Instr) throws (binary::Error) {
245 +
    match item {
246 +
        case il::Instr::Reserve { dst: vdst, layout: vlayout } => {
247 +
            try integer(out, super::INSTR_RESERVE as u64, 1);
248 +
            try integer(out, vdst.n as u64, 4);
249 +
            try integer(out, vlayout as u64, 4);
250 +
        },
251 +
        case il::Instr::Load { typ: vtyp, dst: vdst, src: vsrc, offset: voffset } => {
252 +
            try integer(out, super::INSTR_LOAD as u64, 1);
253 +
            try typ(out, vtyp);
254 +
            try integer(out, vdst.n as u64, 4);
255 +
            try integer(out, vsrc.n as u64, 4);
256 +
            try integer(out, voffset as u64, 4);
257 +
        },
258 +
        case il::Instr::Sload { typ: vtyp, dst: vdst, src: vsrc, offset: voffset } => {
259 +
            try integer(out, super::INSTR_SLOAD as u64, 1);
260 +
            try typ(out, vtyp);
261 +
            try integer(out, vdst.n as u64, 4);
262 +
            try integer(out, vsrc.n as u64, 4);
263 +
            try integer(out, voffset as u64, 4);
264 +
        },
265 +
        case il::Instr::Store { typ: vtyp, src: vsrc, dst: vdst, offset: voffset } => {
266 +
            try integer(out, super::INSTR_STORE as u64, 1);
267 +
            try typ(out, vtyp);
268 +
            try val(out, vsrc);
269 +
            try integer(out, vdst.n as u64, 4);
270 +
            try integer(out, voffset as u64, 4);
271 +
        },
272 +
        case il::Instr::Blit { dst: vdst, src: vsrc, layout: vlayout } => {
273 +
            try integer(out, super::INSTR_BLIT as u64, 1);
274 +
            try integer(out, vdst.n as u64, 4);
275 +
            try integer(out, vsrc.n as u64, 4);
276 +
            try integer(out, vlayout as u64, 4);
277 +
        },
278 +
        case il::Instr::Copy { dst: vdst, val: vval } => {
279 +
            try integer(out, super::INSTR_COPY as u64, 1);
280 +
            try integer(out, vdst.n as u64, 4);
281 +
            try val(out, vval);
282 +
        },
283 +
        case il::Instr::BinOp { op: vop, typ: vtyp, dst: vdst, a: va, b: vb } => {
284 +
            try integer(out, super::INSTR_BINOP as u64, 1);
285 +
            try binOp(out, vop);
286 +
            try typ(out, vtyp);
287 +
            try integer(out, vdst.n as u64, 4);
288 +
            try val(out, va);
289 +
            try val(out, vb);
290 +
        },
291 +
        case il::Instr::UnOp { op: vop, typ: vtyp, dst: vdst, a: va } => {
292 +
            try integer(out, super::INSTR_UNOP as u64, 1);
293 +
            try unOp(out, vop);
294 +
            try typ(out, vtyp);
295 +
            try integer(out, vdst.n as u64, 4);
296 +
            try val(out, va);
297 +
        },
298 +
        case il::Instr::Zext { typ: vtyp, dst: vdst, val: vval } => {
299 +
            try integer(out, super::INSTR_ZEXT as u64, 1);
300 +
            try typ(out, vtyp);
301 +
            try integer(out, vdst.n as u64, 4);
302 +
            try val(out, vval);
303 +
        },
304 +
        case il::Instr::Sext { typ: vtyp, dst: vdst, val: vval } => {
305 +
            try integer(out, super::INSTR_SEXT as u64, 1);
306 +
            try typ(out, vtyp);
307 +
            try integer(out, vdst.n as u64, 4);
308 +
            try val(out, vval);
309 +
        },
310 +
        case il::Instr::Call { retTy: vretTy, dst: vdst, func: vfunc, args: vargs } => {
311 +
            try integer(out, super::INSTR_CALL as u64, 1);
312 +
            try valType(out, vretTy);
313 +
            if let present = vdst {
314 +
                try integer(out, 1, 1);
315 +
                try integer(out, present.n as u64, 4);
316 +
            } else {
317 +
                try integer(out, 0, 1);
318 +
            }
319 +
            try val(out, vfunc);
320 +
            try values(out, il::argVals(func, vargs));
321 +
        },
322 +
        case il::Instr::Ret { val: vval } => {
323 +
            try integer(out, super::INSTR_RET as u64, 1);
324 +
            if let present = vval {
325 +
                try integer(out, 1, 1);
326 +
                try val(out, present);
327 +
            } else {
328 +
                try integer(out, 0, 1);
329 +
            }
330 +
        },
331 +
        case il::Instr::Jmp { target: vtarget, args: vargs } => {
332 +
            try integer(out, super::INSTR_JMP as u64, 1);
333 +
            try integer(out, vtarget as u64, 4);
334 +
            try values(out, il::argVals(func, vargs));
335 +
        },
336 +
        case il::Instr::Br { op: vop, typ: vtyp, a: va, b: vb, thenTarget: vthenTarget,
337 +
            thenArgs: vthenArgs, elseTarget: velseTarget, elseArgs: velseArgs } => {
338 +
            try integer(out, super::INSTR_BR as u64, 1);
339 +
            try cmpOp(out, vop);
340 +
            try typ(out, vtyp);
341 +
            try val(out, va);
342 +
            try val(out, vb);
343 +
            try integer(out, vthenTarget as u64, 4);
344 +
            try values(out, il::argVals(func, vthenArgs));
345 +
            try integer(out, velseTarget as u64, 4);
346 +
            try values(out, il::argVals(func, velseArgs));
347 +
        },
348 +
        case il::Instr::Switch { val: vval, defaultTarget: vdefaultTarget,
349 +
            defaultArgs: vdefaultArgs, cases: vcases } => {
350 +
            try integer(out, super::INSTR_SWITCH as u64, 1);
351 +
            try val(out, vval);
352 +
            try integer(out, vdefaultTarget as u64, 4);
353 +
            try values(out, il::argVals(func, vdefaultArgs));
354 +
            let branches = il::switchCases(func, vcases);
355 +
            try integer(out, branches.len as u64, 4);
356 +
            for branch in branches {
357 +
                try integer(out, branch.value as u64, 8);
358 +
                try integer(out, branch.target as u64, 4);
359 +
                try values(out, il::argVals(func, branch.args));
360 +
            }
361 +
        },
362 +
        case il::Instr::Unreachable => {
363 +
            try integer(out, super::INSTR_UNREACHABLE as u64, 1);
364 +
        },
365 +
        case il::Instr::Ecall { dst: vdst, num: vnum, a0: va0, a1: va1, a2: va2, a3: va3,
366 +
            buf: vbuf } => {
367 +
            try integer(out, super::INSTR_ECALL as u64, 1);
368 +
            try integer(out, vdst.n as u64, 4);
369 +
            try val(out, vnum);
370 +
            try val(out, va0);
371 +
            try val(out, va1);
372 +
            try val(out, va2);
373 +
            try val(out, va3);
374 +
            if let b = vbuf {
375 +
                try integer(out, 1, 1);
376 +
                try integer(out, b.ptr.n as u64, 4);
377 +
                try integer(out, b.size as u64, 4);
378 +
            } else {
379 +
                try integer(out, 0, 1);
380 +
            }
381 +
        },
382 +
        case il::Instr::PageSlice { err: verr, slot: vslot, handle: vhandle, write: vwrite,
383 +
            layout: vlayout } => {
384 +
            try integer(out, super::INSTR_PAGE_SLICE as u64, 1);
385 +
            try integer(out, verr.n as u64, 4);
386 +
            try integer(out, vslot.n as u64, 4);
387 +
            try val(out, vhandle);
388 +
            try integer(out, 1 if vwrite else 0, 1);
389 +
            try integer(out, vlayout as u64, 4);
390 +
        },
391 +
        case il::Instr::DeviceRead { typ: t, dst, handle, offset } => {
392 +
            try integer(out, super::INSTR_DEVICE_READ as u64, 1);
393 +
            try typ(out, t);
394 +
            try integer(out, dst.n as u64, 4);
395 +
            try val(out, handle);
396 +
            try val(out, offset);
397 +
        },
398 +
        case il::Instr::DeviceWrite { typ: t, handle, offset, value } => {
399 +
            try integer(out, super::INSTR_DEVICE_WRITE as u64, 1);
400 +
            try typ(out, t);
401 +
            try val(out, handle);
402 +
            try val(out, offset);
403 +
            try val(out, value);
404 +
        },
405 +
        case il::Instr::Field { dst: vdst, src: vsrc, offset: voffset } => {
406 +
            try integer(out, super::INSTR_FIELD as u64, 1);
407 +
            try integer(out, vdst.n as u64, 4);
408 +
            try integer(out, vsrc.n as u64, 4);
409 +
            try integer(out, voffset as u64, 4);
410 +
        },
411 +
        case il::Instr::Elem { dst: vdst, base: vbase, index: vindex, layout: vlayout,
412 +
            count: vcount } => {
413 +
            try integer(out, super::INSTR_ELEM as u64, 1);
414 +
            try integer(out, vdst.n as u64, 4);
415 +
            try integer(out, vbase.n as u64, 4);
416 +
            try val(out, vindex);
417 +
            try integer(out, vlayout as u64, 4);
418 +
            try integer(out, vcount as u64, 4);
419 +
        },
420 +
        case il::Instr::SliceElem { dst: vdst, slice: vslice, index: vindex, layout: vlayout } => {
421 +
            try integer(out, super::INSTR_SLICE_ELEM as u64, 1);
422 +
            try integer(out, vdst.n as u64, 4);
423 +
            try integer(out, vslice.n as u64, 4);
424 +
            try val(out, vindex);
425 +
            try integer(out, vlayout as u64, 4);
426 +
        },
427 +
        case il::Instr::SliceNew {
428 +
            dst: vdst, base: vbase, layout: vlayout, count: vcount, start: vstart, end: vend
429 +
        } => {
430 +
            try integer(out, super::INSTR_SLICE_NEW as u64, 1);
431 +
            try integer(out, vdst.n as u64, 4);
432 +
            try val(out, vbase);
433 +
            try integer(out, vlayout as u64, 4);
434 +
            try integer(out, vcount as u64, 4);
435 +
            try val(out, vstart);
436 +
            try val(out, vend);
437 +
        },
438 +
        case il::Instr::SliceSub { dst: vdst, src: vsrc, layout: vlayout, start: vstart,
439 +
            end: vend } => {
440 +
            try integer(out, super::INSTR_SLICE_SUB as u64, 1);
441 +
            try integer(out, vdst.n as u64, 4);
442 +
            try integer(out, vsrc.n as u64, 4);
443 +
            try integer(out, vlayout as u64, 4);
444 +
            try val(out, vstart);
445 +
            try val(out, vend);
446 +
        },
447 +
        case il::Instr::LoadPtr { dst: vdst, src: vsrc, offset: voffset, nullable } => {
448 +
            try integer(out, super::INSTR_LOAD_PTR as u64, 1);
449 +
            try integer(out, vdst.n as u64, 4);
450 +
            try integer(out, vsrc.n as u64, 4);
451 +
            try integer(out, voffset as u64, 4);
452 +
            try integer(out, 1 if nullable else 0, 1);
453 +
        },
454 +
        case il::Instr::StorePtr { src: vsrc, dst: vdst, offset: voffset } => {
455 +
            try integer(out, super::INSTR_STORE_PTR as u64, 1);
456 +
            try val(out, vsrc);
457 +
            try integer(out, vdst.n as u64, 4);
458 +
            try integer(out, voffset as u64, 4);
459 +
        },
460 +
        case il::Instr::Ebreak => {
461 +
            try integer(out, super::INSTR_EBREAK as u64, 1);
462 +
        },
463 +
        case il::Instr::MemoryFence => {
464 +
            try integer(out, super::INSTR_MEMORYFENCE as u64, 1);
465 +
        },
466 +
    }
467 +
}
468 +
469 +
/// Write one data initializer and its repetition count.
470 +
export fn dataValue(out: &mut Writer, value: il::DataValue) throws (binary::Error) {
471 +
    match value.item {
472 +
        case il::DataItem::Val { typ: t, val: n } => {
473 +
            try integer(out, super::DATA_VAL as u64, 1);
474 +
            try typ(out, t);
475 +
            try integer(out, n as u64, il::typeSize(t));
476 +
        },
477 +
        case il::DataItem::Sym(name) => {
478 +
            try integer(out, super::DATA_SYM as u64, 1);
479 +
            try symbol(out, name);
480 +
        },
481 +
        case il::DataItem::Fn(name) => {
482 +
            try integer(out, super::DATA_FN as u64, 1);
483 +
            try symbol(out, name);
484 +
        },
485 +
        case il::DataItem::Str(text) => {
486 +
            try integer(out, super::DATA_STR as u64, 1);
487 +
            try bytes(out, text);
488 +
        },
489 +
        case il::DataItem::Undef => try integer(out, super::DATA_UNDEF as u64, 1),
490 +
    }
491 +
    try integer(out, value.count as u64, 4);
492 +
}
std.lib +6 -0
30 30
lib/std/lang/ast/printer.rad
31 31
lib/std/lang/scanner.rad
32 32
lib/std/lang/parser.rad
33 33
lib/std/lang/il.rad
34 34
lib/std/lang/il/printer.rad
35 +
lib/std/lang/il/binary.rad
36 +
lib/std/lang/il/binary/writer.rad
37 +
lib/std/lang/il/binary/reader.rad
38 +
lib/std/lang/il/binary/program.rad
39 +
lib/std/lang/il/binary/collect.rad
40 +
lib/std/lang/il/binary/cfg.rad
35 41
lib/std/lang/resolver.rad
36 42
lib/std/lang/resolver/printer.rad
37 43
lib/std/lang/lower.rad
38 44
lib/std/lang/module.rad
39 45
lib/std/lang/module/printer.rad
std.lib.test +2 -0
15 15
lib/std/lang/resolver/tests/core.rad
16 16
lib/std/lang/resolver/tests/structure.rad
17 17
lib/std/lang/resolver/tests/safety.rad
18 18
lib/std/lang/resolver/tests/opacity.rad
19 19
lib/std/lang/gen/bitset/tests.rad
20 +
lib/std/lang/il/binary/tests.rad
21 +
lib/std/lang/il/binary/tests/decode.rad
20 22
lib/std/arch/rv64/image/tests.rad
21 23
lib/std/arch/rv64/tests/bounds.rad
22 24
lib/std/arch/rv64/tests/atomics.rad
test/compile +9 -3
1 1
#!/bin/sh
2 -
# Compile a compiler fixture to an executable.
2 +
# Compile a compiler fixture to an executable or a binary RIL directory.
3 3
# Usage: sh test/compile source output
4 4
set -eu
5 5
source=$1
6 6
output=$2
7 7
base=${source%.rad}
8 -
set -- -o "$output"
9 -
expected=$output
8 +
case "$output" in
9 +
    *.rv64) set -- -o "$output"; expected=$output ;;
10 +
    *)
11 +
        mkdir -p "$output"
12 +
        set -- -ril "$output"
13 +
        expected=$output/test.ril
14 +
        ;;
15 +
esac
10 16
11 17
entry=false
12 18
if grep -q '^//! uses-std' "$source"; then
13 19
    set -- "$@" -pkg std
14 20
    while IFS= read -r module; do