compiler: Simplify single-package RIL output

213af9c4338a74e7ecdb1d76dc8c46c1d950fc3d00bd2bbeac5db281acfa7369
Keep the active package's symbols, exports, entry and function bytes in
one spool. Pass completed provider descriptors to import selection and
record exact pins with their dependency labels.

Use the lowering pass's descriptor tables directly for package output.
Keep whole-program tables for direct source compilation.

Assisted-by: Codex:gpt-6
Change-Id: Ic67e070573c6c369c10204c5481abee9c0b7beec
Alexis Sellier committed ago 1 parent 1c9a5376
compiler/radiance/build.rad +27 -36
937 937
        }
938 938
    }
939 939
    unsafe {
940 940
        heap::reset(work.functions);
941 941
    }
942 -
    let mut initial: codegen::RilSpool = undefined;
943 -
    {
944 -
        let mut names: [*[u8]; package::MAX_PACKAGES] = [""; package::MAX_PACKAGES];
945 -
        for i in 0 .. ctx.packageCount {
946 -
            set names[i] = ctx.inputs[i].name;
947 -
        }
948 -
        set initial = try codegen::ril(
949 -
            work.emitter, work.spool.take(), &names[.. ctx.packageCount], index
950 -
        ) catch {
951 -
            throw super::error(&["package encoding storage exhausted", pkg.name]);
952 -
        };
953 -
    }
942 +
    let initial = try codegen::ril(work.emitter, work.spool.take(), pkg.name) catch {
943 +
        throw super::error(&["package encoding storage exhausted", pkg.name]);
944 +
    };
945 +
    let mut published: *mut [link::Provider] = &mut [];
954 946
    let mut low = lower::lowerer(res, modules, pkg.name, work.functions, lower::LowerOptions {
955 947
        debug: ctx.debug, buildTest: ctx.config.buildTest, tableScope: lower::TableScope::Package
956 948
    });
957 -
    for provider, i in providers {
949 +
    for provider in providers {
958 950
        let checkpoint = heap::mark(work.functions);
959 951
        let mut input = reader::new(provider.image);
960 952
        let head = try program::header(&mut input, work.functions) catch {
961 953
            throw super::error(&["package encoding storage exhausted", pkg.name]);
962 954
        };
955 +
        let mut exports: *mut [binary::Export] = &mut [];
963 956
        for item in head.exports {
964 957
            let name = try keep(initial.arena, item.name) catch {
965 958
                throw super::Error::Other;
966 959
            };
967 960
            let mut item = item;
968 961
            set item.name = name;
969 -
            try initial.packages[i].exports.append(item, initial.arena) catch {
962 +
            try exports.append(item, initial.arena) catch {
970 963
                throw super::error(&["package encoding storage exhausted", pkg.name]);
971 964
            };
972 965
        }
966 +
        try published.append(
967 +
            link::Provider {
968 +
                name: provider.name, image: provider.identity, exports: &exports[..]
969 +
            },
970 +
            initial.arena
971 +
        ) catch {
972 +
            throw super::Error::Other;
973 +
        };
973 974
        // Only the copied export descriptors survive header decoding.
974 975
        unsafe {
975 976
            heap::rewind(work.functions, checkpoint);
976 977
        }
977 978
    }
994 995
        firstModule: root,
995 996
        native: target,
996 997
        entry: selection.entry,
997 998
    };
998 999
    try super::lowerPackage(ctx, modules, &mut low, &pkg, true, &mut sink);
999 -
    return try finishPackage(ctx, low, sink, &declarations[..], providers, work, selection);
1000 +
    return try finishPackage(
1001 +
        ctx, low, sink, &declarations[..], providers, &published[..], work, selection
1002 +
    );
1000 1003
}
1001 1004
1002 1005
/// Encode the tables, exact pins, and metadata after a package's functions are consumed.
1003 1006
fn finishPackage(
1004 1007
    ctx: &super::CompileContext,
1005 1008
    low: lower::Lowerer,
1006 1009
    sink: locations::Sink,
1007 1010
    declarations: *[artifact::Module],
1008 1011
    providers: &[artifact::Package],
1012 +
    published: &[link::Provider],
1009 1013
    work: &mut Workspace,
1010 1014
    selection: Request
1011 1015
) -> Compilation throws (super::Error) {
1012 1016
    let mut lowering = low;
1013 1017
    let index = ctx.entryPkgIdx else panic "finishPackage: missing package";
1035 1039
    }
1036 1040
    let case locations::Sink { ril: encoded, native, locations: locationOutput, .. } = sink
1037 1041
        else panic "compile: expected function sink";
1038 1042
    let mut spool = encoded;
1039 1043
    let dataOrigins = lowering.dataOrigins.take();
1040 -
    let starts = lowering.tables.tableStarts.take();
1041 1044
    let references = lowering.tables.references.take();
1042 1045
    let case lower::Lowerer { resolver: resolved, data, tables, .. } = lowering
1043 1046
        else panic "compile: expected lowerer";
1044 1047
    let whole = il::Program {
1045 1048
        data,
1046 1049
        fns: &[],
1047 1050
        layouts: &tables.layouts[..],
1048 1051
        signatures: &tables.signatures[..],
1049 1052
        traits: &tables.traits[..],
1050 1053
    };
1051 -
    let mut plan = try link::prepare(
1052 -
        &mut spool, &whole, &dataOrigins[..], &starts[..], &references[..], index as u16
1054 +
    let plan = try link::prepare(
1055 +
        &mut spool, &whole, &dataOrigins[..], published, &references[..], index as u16
1053 1056
    ) catch {
1054 1057
        throw super::error(&["cannot resolve package imports"]);
1055 1058
    };
1056 -
    {
1057 -
        let mut identities: [?binary::Id; package::MAX_PACKAGES] = [nil; package::MAX_PACKAGES];
1058 -
        for provider, i in providers {
1059 -
            set identities[i] = provider.identity;
1060 -
        }
1061 -
        super::pkgLog(&pkg, &["encoding package tables"]);
1062 -
        let pins = try link::pins(plan.dependencies, &identities[..], work.functions) catch {
1063 -
            throw super::error(&["package encoding storage exhausted", pkg.name]);
1064 -
        };
1065 -
        let selectedPins = pins else throw super::Error::Other;
1066 -
        set plan.image.links.pins = selectedPins;
1067 -
    }
1059 +
    super::pkgLog(&pkg, &["encoding package tables"]);
1068 1060
    let mut head = try buffer(work.functions, HEADER_BYTES);
1069 -
    let functionCount = spool.packages[index].count;
1061 +
    let functionCount = spool.count;
1070 1062
    let length = try program::encodeHead(
1071 -
        &mut head, &plan.image, &mut spool.packages[index].names.index, functionCount
1063 +
        &mut head, &plan.image, &mut spool.names.index, functionCount
1072 1064
    ) catch {
1073 1065
        throw super::error(&["cannot encode package image"]);
1074 1066
    };
1075 1067
    try artifact::alignBuffer(work.artifacts) catch {
1076 1068
        throw super::Error::Other;
1077 1069
    };
1078 1070
    let mut image: *mut [u8] = &mut [];
1079 -
    let start = spool.packages[index].start;
1080 -
    let end = spool.packages[index].end;
1081 -
    try image.reserve(length + end - start, work.artifacts) catch {
1071 +
    let end = spool.used;
1072 +
    try image.reserve(length + end, work.artifacts) catch {
1082 1073
        throw super::error(&["package image storage exhausted", pkg.name]);
1083 1074
    };
1084 1075
    try image.extend(&head[.. length], work.artifacts) catch {
1085 1076
        throw super::error(&["package encoding storage exhausted", pkg.name]);
1086 1077
    };
1087 -
    try image.extend(&spool.bytes[start .. end], work.artifacts) catch {
1078 +
    try image.extend(&spool.bytes[.. end], work.artifacts) catch {
1088 1079
        throw super::error(&["package encoding storage exhausted", pkg.name]);
1089 1080
    };
1090 1081
    let bytes: *[u8] = &image[..];
1091 1082
    set work.spool = spool.bytes.take();
1092 1083
    let mut dependencies: *mut [artifact::Dependency] = &mut [];
compiler/radiance/codegen.rad +25 -68
99 99
            throw lower::LowerError::OutputFailed;
100 100
        }
101 101
    }
102 102
}
103 103
104 -
/// Binary RIL state of one package while its functions stream in.
105 -
export record RilPackage {
104 +
/// Sink that encodes one package's functions and reclaims their IL arena.
105 +
/// Only encoded bytes and name tables remain after each function.
106 +
export record RilSpool {
107 +
    /// Storage of the name tables. It lives until the package is written.
108 +
    arena: *heap::Heap,
106 109
    /// Package name.
107 110
    name: *[u8],
108 -
    /// Interned names, shared by the functions and the tables of the package.
111 +
    /// Interned names shared by functions and package tables.
109 112
    names: collect::Names,
110 -
    /// Offset of the first encoded function in the spool.
111 -
    start: u32,
112 -
    /// Offset past the last encoded function in the spool.
113 -
    end: u32,
113 +
    /// Encoded functions in lowering order.
114 +
    bytes: *mut [u8],
115 +
    /// Number of initialized function bytes.
116 +
    used: u32,
114 117
    /// Number of encoded functions.
115 118
    count: u32,
116 -
    /// Definitions that other packages can link against: the exported functions as they
117 -
    /// arrive, and the exported data once the driver has lowered the package.
119 +
    /// Exported functions, followed by exported data after lowering.
118 120
    exports: *mut [binary::Export],
119 -
    /// Stored name of the function lowered with the default role, if any. It is the
120 -
    /// generated storage wrapper when the default function takes storage parameters.
121 +
    /// Default function's stored name, including its storage wrapper if present.
121 122
    entry: ?*[u8],
122 123
}
123 124
124 -
/// Sink that encodes each function of a package into binary RIL and reclaims
125 -
/// its IL arena. Only the encoded bytes and the name tables stay, so the memory
126 -
/// does not grow with the IL of the program. Each package owns one contiguous
127 -
/// extent of `bytes`, because lowering finishes a package before the next.
128 -
export record RilSpool {
129 -
    /// Storage of the name tables. It lives until the packages are written.
130 -
    arena: *heap::Heap,
131 -
    /// Encoded functions of all packages, in lowering order.
132 -
    bytes: *mut [u8],
133 -
    /// Number of bytes of `bytes` in use.
134 -
    used: u32,
135 -
    /// One entry per package.
136 -
    packages: *mut [RilPackage],
137 -
}
138 -
139 -
/// Create a spool for the packages named `names`. Only `active` encodes functions;
140 -
/// other packages supply export and identity descriptors.
141 -
export fn ril(arena: *heap::Heap, bytes: *mut [u8], names: &[*[u8]], active: u32) -> RilSpool
125 +
/// Create a function spool for one package.
126 +
export fn ril(arena: *heap::Heap, bytes: *mut [u8], name: *[u8]) -> RilSpool
142 127
    throws (binary::Error)
143 128
{
144 -
    let mut packages: *mut [RilPackage] = &mut [];
145 -
    for name, i in names {
146 -
        let slots = collect::INDEX_SLOTS if active == i else 2;
147 -
        let mut tables = try collect::new(slots, arena);
148 -
        try collect::start(&mut tables, name, arena);
149 -
        try packages.append(
150 -
            RilPackage {
151 -
                name,
152 -
                names: tables,
153 -
                start: 0,
154 -
                end: 0,
155 -
                count: 0,
156 -
                exports: &mut [],
157 -
                entry: nil,
158 -
            },
159 -
            arena
160 -
        ) catch {
161 -
            throw binary::Error::Storage;
162 -
        };
163 -
    }
164 -
    return RilSpool { arena, bytes, used: 0, packages };
129 +
    let mut names = try collect::new(collect::INDEX_SLOTS, arena);
130 +
    try collect::start(&mut names, name, arena);
131 +
    return RilSpool {
132 +
        arena, name, names, bytes, used: 0, count: 0, exports: &mut [], entry: nil
133 +
    };
165 134
}
166 135
167 136
instance lower::FnSink for RilSpool {
168 137
    /// Encode one function into its package and reclaim its IL arena.
169 138
    fn (spool: &mut RilSpool) emit(
190 159
        };
191 160
        resetFnArena(fnArena);
192 161
    }
193 162
}
194 163
195 -
/// Collect the names of `func` and append its encoding to the package that `origin` names.
164 +
/// Collect the names of `func` and append its encoding to the package spool.
196 165
/// A function with the default role becomes the entry of its package. An exported function
197 166
/// is also an export.
198 167
fn encodeFunction(spool: &mut RilSpool, func: *il::Fn, role: lower::FnRole, origin: lower::Origin)
199 168
    throws (binary::Error)
200 169
{
201 -
    let i = origin.package as u32;
202 -
    if i >= spool.packages.len {
203 -
        throw binary::Error::Invalid;
204 -
    }
205 -
    if spool.packages[i].count > 0 and spool.packages[i].end <> spool.used {
206 -
        throw binary::Error::Invalid;
207 -
    }
208 -
    let name = spool.packages[i].name;
209 170
    let stored = try collect::definition(
210 -
        &mut spool.packages[i].names, name, func.name, spool.arena
171 +
        &mut spool.names, spool.name, func.name, spool.arena
211 172
    );
212 173
    if let case lower::Linkage::Exported = origin.linkage {
213 174
        let definition = binary::Export {
214 175
            name: stored,
215 176
            kind: binary::ExportKind::Function,
216 177
            startable: false,
217 178
        };
218 -
        try spool.packages[i].exports.append(definition, spool.arena) catch {
179 +
        try spool.exports.append(definition, spool.arena) catch {
219 180
            throw binary::Error::Storage;
220 181
        };
221 182
    }
222 183
    if let case lower::FnRole::Default = role {
223 -
        set spool.packages[i].entry = stored;
184 +
        set spool.entry = stored;
224 185
    }
225 186
    let end = try program::appendCollected(
226 -
        &mut spool.bytes, spool.used, &mut spool.packages[i].names, func, spool.arena
187 +
        &mut spool.bytes, spool.used, &mut spool.names, func, spool.arena
227 188
    );
228 -
    if spool.packages[i].count == 0 {
229 -
        set spool.packages[i].start = spool.used;
230 -
    }
231 -
    set spool.packages[i].end = end;
232 -
    set spool.packages[i].count += 1;
189 +
    set spool.count += 1;
233 190
    set spool.used = end;
234 191
}
235 192
236 193
/// Reset the function arena after a function has been generated.
237 194
///
compiler/radiance/link.rad +29 -56
3 3
use std::lang::il;
4 4
use std::lang::il::binary;
5 5
use std::lang::il::binary::collect;
6 6
use std::lang::lower;
7 7
use std::mem;
8 -
use std::mem::heap;
9 8
use super::codegen;
10 9
11 10
/// Test whether a resolved foreign reference names a public provider definition.
12 -
fn exported(exports: &[*[binary::Export]], owner: u32, name: *[u8]) -> bool {
13 -
    for item in exports[owner] {
11 +
fn exported(provider: &Provider, name: *[u8]) -> bool {
12 +
    for item in provider.exports {
14 13
        if mem::eq(item.name, name) {
15 14
            return true;
16 15
        }
17 16
    }
18 17
    return false;
19 18
}
20 19
20 +
/// Completed provider used to select exact imports.
21 +
export record Provider: Copy {
22 +
    /// Package name.
23 +
    name: *[u8],
24 +
    /// Identity of the complete RIL image.
25 +
    image: binary::Id,
26 +
    /// Public function and data definitions.
27 +
    exports: *[binary::Export],
28 +
}
29 +
21 30
/// Package head and resolved provider indices for one compilation step.
22 31
export record Plan {
23 32
    /// Encoded definitions whose function bodies remain in the spool.
24 33
    image: binary::Package,
25 34
    /// Provider indices in the image's dependency order.
30 39
/// Function records remain in the spool.
31 40
export fn prepare(
32 41
    spool: &mut codegen::RilSpool,
33 42
    whole: &il::Program,
34 43
    origins: &[lower::Origin],
35 -
    starts: &[lower::TableStart],
44 +
    providers: &[Provider],
36 45
    references: &[lower::Reference],
37 46
    active: u16
38 47
) -> Plan throws (binary::Error) {
39 48
    let arena = spool.arena;
40 -
    let index = active as u32;
41 49
    for item, i in whole.data {
42 50
        if origins[i].package <> active {
43 51
            throw binary::Error::Invalid;
44 52
        }
45 53
        if let case lower::Linkage::Exported = origins[i].linkage {
46 -
            try spool.packages[index].exports.append(
54 +
            try spool.exports.append(
47 55
                binary::Export {
48 56
                    name: item.name, kind: binary::ExportKind::Data, startable: false
49 57
                },
50 58
                arena
51 59
            ) catch {
52 60
                throw binary::Error::Storage;
53 61
            };
54 62
        }
55 63
    }
56 -
    let mut exportTables: *mut [*[binary::Export]] = &mut [];
57 -
    for i in 0 .. spool.packages.len {
58 -
        let entries = spool.packages[i].exports.take();
59 -
        let frozen: *[binary::Export] = &entries[..];
60 -
        try exportTables.append(frozen, arena) catch {
61 -
            throw binary::Error::Storage;
62 -
        };
63 -
    }
64 -
    let published: *[*[binary::Export]] = &exportTables[..];
65 -
    let tables = lower::packageTables(whole, starts, active);
66 64
    let mut dependencies: *mut [*[u8]] = &mut [];
67 65
    let mut indices: *mut [u32] = &mut [];
66 +
    let mut pins: *mut [binary::Id] = &mut [];
68 67
    let mut imports: *mut [binary::Import] = &mut [];
69 68
    for reference in references {
70 69
        if reference.package <> active {
71 70
            throw binary::Error::Invalid;
72 71
        }
73 72
        let owner = reference.provider as u32;
74 -
        if owner >= spool.packages.len {
73 +
        if owner >= providers.len {
75 74
            throw binary::Error::Symbol;
76 75
        }
77 -
        if owner == index {
78 -
            continue;
79 -
        }
80 -
        if not exported(&published[..], owner, reference.name) {
76 +
        let provider = &providers[owner];
77 +
        if not exported(provider, reference.name) {
81 78
            throw binary::Error::Symbol;
82 79
        }
83 -
        let label = spool.packages[owner].name;
84 80
        let mut dependency: ?u32 = nil;
85 81
        for k in 0 .. indices.len {
86 82
            if indices[k] == owner {
87 83
                set dependency = k;
88 84
                break;
89 85
            }
90 86
        }
91 87
        if dependency == nil {
92 88
            set dependency = dependencies.len;
93 -
            try dependencies.append(label, arena) catch {
89 +
            try dependencies.append(provider.name, arena) catch {
90 +
                throw binary::Error::Storage;
91 +
            };
92 +
            try pins.append(provider.image, arena) catch {
94 93
                throw binary::Error::Storage;
95 94
            };
96 95
            try indices.append(owner, arena) catch {
97 96
                throw binary::Error::Storage;
98 97
            };
108 107
            arena
109 108
        ) catch {
110 109
            throw binary::Error::Storage;
111 110
        };
112 111
    }
113 -
    let name = spool.packages[index].name;
114 -
    let entry = spool.packages[index].entry;
112 +
    let exports = spool.exports.take();
115 113
    let mut image = try collect::finish(
116 -
        &mut spool.packages[index].names,
117 -
        name,
114 +
        &mut spool.names,
115 +
        spool.name,
118 116
        &dependencies[..],
119 -
        il::Program {
120 -
            data: whole.data,
121 -
            fns: &[],
122 -
            layouts: tables.layouts,
123 -
            signatures: tables.signatures,
124 -
            traits: tables.traits,
125 -
        },
126 -
        published[index],
127 -
        entry,
117 +
        *whole,
118 +
        &exports[..],
119 +
        spool.entry,
128 120
        arena
129 121
    );
122 +
    set image.links.pins = &pins[..];
130 123
    set image.links.imports = &imports[..];
131 124
    return Plan { image, dependencies: &indices[..] };
132 125
}
133 -
134 -
/// Resolve a package's exact dependency identities when all providers have been emitted.
135 -
/// An absent result means that a provider has not been emitted yet.
136 -
export fn pins(
137 -
    providers: &[u32], identities: &[?binary::Id], arena: &heap::Heap
138 -
) -> ?*[binary::Id] throws (binary::Error) {
139 -
    for index in providers {
140 -
        if identities[index] == nil {
141 -
            return nil;
142 -
        }
143 -
    }
144 -
    let mut result: *mut [binary::Id] = &mut [];
145 -
    for index in providers {
146 -
        let pin = identities[index] else panic "pins: provider identity";
147 -
        try result.append(pin, arena) catch {
148 -
            throw binary::Error::Storage;
149 -
        };
150 -
    }
151 -
    return &result[..];
152 -
}
lib/std/lang/lower.rad +21 -106
306 306
/// Extent of the layout and signature tables of a lowering pass.
307 307
export union TableScope: Copy {
308 308
    /// One pair of tables for the whole program. Indices are valid in the
309 309
    /// program that [`finalize`] returns.
310 310
    Program,
311 -
    /// Each package starts its own tables, so that its functions and data refer
312 -
    /// only to entries of the package. A package takes its tables from the
313 -
    /// lowerer with [`packageTables`] once it is lowered.
311 +
    /// One package with explicit foreign references and its own descriptor tables.
314 312
    Package,
315 313
}
316 314
317 315
/// Options controlling the lowering pass.
318 316
export record LowerOptions: Copy {
347 345
    package: u16,
348 346
    /// Visibility of the definition to other packages.
349 347
    linkage: Linkage,
350 348
}
351 349
352 -
/// Start of the layout and signature tables of one package, within the tables of
353 -
/// the lowerer.
354 -
export record TableStart: Copy {
355 -
    /// Identifier of the package, as in the module table.
356 -
    package: u16,
357 -
    /// Index of the first layout of the package.
358 -
    layouts: u32,
359 -
    /// Index of the first signature of the package.
360 -
    signatures: u32,
361 -
    /// Index of the first trait of the package.
362 -
    traits: u32,
363 -
}
364 -
365 350
/// Index of a trait in the trait table of the current package.
366 351
record TraitIndex: Copy {
367 352
    /// The trait.
368 353
    id: resolver::TraitId,
369 354
    /// Package-relative index in the trait table.
380 365
    name: *[u8],
381 366
    /// Expected signature or data description in that package's tables.
382 367
    kind: binary::ImportKind,
383 368
}
384 369
385 -
/// The layout, signature and trait tables of a lowering pass.
386 -
///
387 -
/// Indices in IL are relative to the tables of the current package. The tables of all
388 -
/// packages are stored one after the other, and `layoutBase` and `signatureBase` locate
389 -
/// the current ones. With a program-wide scope the bases stay zero.
370 +
/// The layout, signature and trait tables of one lowering pass.
371 +
/// All indices are relative to these tables. Package-local passes lower one package.
390 372
export record Tables {
391 -
    /// Layouts of all packages. Entries are added and never removed.
373 +
    /// Layouts of the pass. Entries are added and never removed.
392 374
    layouts: *mut [il::Layout],
393 -
    /// Signatures of all packages.
375 +
    /// Signatures of the pass.
394 376
    signatures: *mut [il::Signature],
395 -
    /// Index in `layouts` of the first layout of the current package.
396 -
    layoutBase: u32,
397 -
    /// Index in `signatures` of the first signature of the current package.
398 -
    signatureBase: u32,
399 -
    /// Traits of all packages.
377 +
    /// Traits of the pass.
400 378
    traits: *mut [il::Trait],
401 -
    /// Index in `traits` of the first trait of the current package.
402 -
    traitBase: u32,
403 379
    /// Traits of the current package that have an index.
404 380
    traitIndices: *mut [TraitIndex],
405 -
    /// Where the tables of each lowered package start, in lowering order.
406 -
    tableStarts: *mut [TableStart],
407 381
    /// Package-relative indices of layouts that are found by content: all layouts
408 382
    /// without slots, and the layouts of results. Sorted by size, alignment and slot count.
409 383
    sharedLayouts: *mut [u32],
410 384
    /// Layouts of the structural types that have slots, such as arrays and optionals.
411 385
    typeLayouts: *mut [TypeLayout],
417 391
    references: *mut [Reference],
418 392
    /// Names already registered in the current package's reference table.
419 393
    referenceIndex: dict::Dict,
420 394
}
421 395
422 -
/// Layout, signature and trait tables of one package.
423 -
export record PackageTables: Copy {
424 -
    /// Layouts that the functions and data of the package refer to.
425 -
    layouts: *[il::Layout],
426 -
    /// Signatures that the function types of the package refer to.
427 -
    signatures: *[il::Signature],
428 -
    /// Traits that the trait objects of the package refer to.
429 -
    traits: *[il::Trait],
430 -
}
431 -
432 396
/// Consumer of the functions produced by the lowerer.
433 397
///
434 398
/// The lowerer calls `emit` once for each finished function, after [`endFn`]
435 399
/// has cleared the per-function state. The sink receives the function arena
436 400
/// with the function. A sink that resets the arena must first stop using the
792 756
    func: *il::Fn,
793 757
    role: FnRole,
794 758
    linkage: Linkage,
795 759
    sink: &mut opaque FnSink
796 760
) throws (LowerError) {
797 -
    let layouts = &self.tables.layouts[self.tables.layoutBase ..];
761 +
    let layouts = &self.tables.layouts[..];
798 762
    try sink.emit(
799 763
        func, role, Origin { package: self.package, linkage }, layouts, self.fnArena
800 764
    );
801 765
}
802 766
1231 1195
        errTags: &mut [],
1232 1196
        options,
1233 1197
        tables: Tables {
1234 1198
            layouts: &mut [],
1235 1199
            signatures: &mut [],
1236 -
            layoutBase: 0,
1237 -
            signatureBase: 0,
1238 1200
            traits: &mut [],
1239 -
            traitBase: 0,
1240 1201
            traitIndices: &mut [],
1241 -
            tableStarts: &mut [],
1242 1202
            sharedLayouts: &mut [],
1243 1203
            typeLayouts: &mut [],
1244 1204
            nominalLayouts: &mut [],
1245 1205
            fillQueue: &mut [],
1246 1206
            references: &mut [],
1472 1432
            binary::collect::INDEX_SLOTS, dict::Entry { key: &[], value: 0 }, self.resolver.arena
1473 1433
        ) catch {
1474 1434
            throw LowerError::AllocationFailed;
1475 1435
        };
1476 1436
        set self.tables.referenceIndex = dict::fresh(entries);
1477 -
        try self.tables.tableStarts.append(
1478 -
            TableStart {
1479 -
                package,
1480 -
                layouts: self.tables.layouts.len,
1481 -
                signatures: self.tables.signatures.len,
1482 -
                traits: self.tables.traits.len,
1483 -
            },
1484 -
            self.resolver.arena
1485 -
        ) catch {
1486 -
            throw LowerError::AllocationFailed;
1487 -
        };
1488 -
        set self.tables.layoutBase = self.tables.layouts.len;
1489 -
        set self.tables.signatureBase = self.tables.signatures.len;
1490 -
        set self.tables.traitBase = self.tables.traits.len;
1491 -
        set self.tables.traitIndices = &mut [];
1492 -
        set self.tables.sharedLayouts = &mut [];
1493 -
        set self.tables.typeLayouts = &mut [];
1494 -
        set self.tables.nominalLayouts = &mut [];
1495 -
    }
1496 -
}
1497 -
1498 -
/// Get the layouts, signatures and traits of one package of a program that was lowered with the
1499 -
/// table scope [`TableScope::Package`]. `starts` are the table starts that the lowerer
1500 -
/// recorded. Indices in the functions and data of the package refer to the result.
1501 -
export fn packageTables(
1502 -
    program: &il::Program, starts: &[TableStart], package: u16
1503 -
) -> PackageTables {
1504 -
    let mut tables = PackageTables { layouts: &[], signatures: &[], traits: &[] };
1505 -
    for start, i in starts {
1506 -
        if start.package == package {
1507 -
            let mut layoutEnd = program.layouts.len;
1508 -
            let mut signatureEnd = program.signatures.len;
1509 -
            let mut traitEnd = program.traits.len;
1510 -
            if i + 1 < starts.len {
1511 -
                set layoutEnd = starts[i + 1].layouts;
1512 -
                set signatureEnd = starts[i + 1].signatures;
1513 -
                set traitEnd = starts[i + 1].traits;
1514 -
            }
1515 -
            set tables = PackageTables {
1516 -
                layouts: &program.layouts[start.layouts .. layoutEnd],
1517 -
                signatures: &program.signatures[start.signatures .. signatureEnd],
1518 -
                traits: &program.traits[start.traits .. traitEnd],
1519 -
            };
1520 -
        }
1521 1437
    }
1522 -
    return tables;
1523 1438
}
1524 1439
1525 1440
/// Return the signature of the function being lowered.
1526 1441
fn currentFnType(self: &Lowerer) -> *resolver::FnType {
1527 1442
    let fnType = self.fnType else {
2199 2114
        if entry.id == id {
2200 2115
            return entry.index;
2201 2116
        }
2202 2117
    }
2203 2118
    // Number the trait before its methods, whose types can refer to it.
2204 -
    let index = self.tables.traits.len - self.tables.traitBase;
2119 +
    let index = self.tables.traits.len;
2205 2120
    try self.tables.traits.append(il::Trait { methods: &[] }, self.resolver.arena) catch {
2206 2121
        throw LowerError::AllocationFailed;
2207 2122
    };
2208 2123
    try self.tables.traitIndices.append(TraitIndex { id, index }, self.resolver.arena) catch {
2209 2124
        throw LowerError::AllocationFailed;
2219 2134
        };
2220 2135
        try methods.append(item, self.resolver.arena) catch {
2221 2136
            throw LowerError::AllocationFailed;
2222 2137
        };
2223 2138
    }
2224 -
    set self.tables.traits[self.tables.traitBase + index].methods = &methods[..];
2139 +
    set self.tables.traits[index].methods = &methods[..];
2225 2140
    return index;
2226 2141
}
2227 2142
2228 2143
/// Lower a method node into an IL function with the given qualified name.
2229 2144
/// Shared by both instance methods and standalone methods.
2957 2872
    dataPrefix: *[u8]
2958 2873
) -> *[u8] throws (LowerError) {
2959 2874
    let counter = self.dataCounter;
2960 2875
    set self.dataCounter += 1;
2961 2876
    let name = try nextDeclDataName(self, dataPrefix, counter, "literal");
2962 -
    let info = self.tables.layouts[self.tables.layoutBase + layout];
2877 +
    let info = self.tables.layouts[layout];
2963 2878
    try addData(
2964 2879
        self,
2965 2880
        il::Data {
2966 2881
            name,
2967 2882
            size: info.size,
3063 2978
3064 2979
/// Find existing constant data entry with matching content.
3065 2980
/// Handles both string data and slice data.
3066 2981
fn findConstData(self: &Lowerer, values: *[il::DataValue], layout: u32) -> ?*[u8] {
3067 2982
    // Fast path for strings.
3068 -
    if values.len == 1 and self.tables.layouts[self.tables.layoutBase + layout].alignment == 1 {
2983 +
    if values.len == 1 and self.tables.layouts[layout].alignment == 1 {
3069 2984
        if let case il::DataItem::Str(s) = values[0].item {
3070 2985
            return findStringData(self, s);
3071 2986
        }
3072 2987
    }
3073 2988
    // General case: byte comparison of data values.
3086 3001
        if let name = findConstData(self, result.values, layout) {
3087 3002
            return name;
3088 3003
        }
3089 3004
    }
3090 3005
    let name = try nextDataName(self);
3091 -
    let info = self.tables.layouts[self.tables.layoutBase + layout];
3006 +
    let info = self.tables.layouts[layout];
3092 3007
    try addData(
3093 3008
        self,
3094 3009
        il::Data {
3095 3010
            name,
3096 3011
            size: info.size,
11693 11608
    let aligned = alignment if alignment > 0 else 1;
11694 11609
    let mut lo: u32 = 0;
11695 11610
    let mut hi = self.tables.sharedLayouts.len;
11696 11611
    while lo < hi {
11697 11612
        let mid = lo + (hi - lo) / 2;
11698 -
        let existing = self.tables.layouts[self.tables.layoutBase + self.tables.sharedLayouts[mid]];
11613 +
        let existing = self.tables.layouts[self.tables.sharedLayouts[mid]];
11699 11614
        if existing.size < size
11700 11615
            or (existing.size == size and existing.alignment < aligned)
11701 11616
            or (existing.size == size
11702 11617
                and existing.alignment == aligned
11703 11618
                and existing.slots.len < slots.len)
11707 11622
            set hi = mid;
11708 11623
        }
11709 11624
    }
11710 11625
    while lo < self.tables.sharedLayouts.len {
11711 11626
        let i = self.tables.sharedLayouts[lo];
11712 -
        let existing = self.tables.layouts[self.tables.layoutBase + i];
11627 +
        let existing = self.tables.layouts[i];
11713 11628
        if existing.size <> size
11714 11629
            or existing.alignment <> aligned
11715 11630
            or existing.slots.len <> slots.len
11716 11631
        {
11717 11632
            break;
11719 11634
        if existing.regions == regions and slotsEqual(existing.slots, slots) {
11720 11635
            return i;
11721 11636
        }
11722 11637
        set lo += 1;
11723 11638
    }
11724 -
    let index = self.tables.layouts.len - self.tables.layoutBase;
11639 +
    let index = self.tables.layouts.len;
11725 11640
    try self.tables.layouts.append(
11726 11641
        il::Layout { size, alignment: aligned, slots, regions }, self.resolver.arena
11727 11642
    ) catch {
11728 11643
        throw LowerError::AllocationFailed;
11729 11644
    };
11996 11911
11997 11912
        let mut slots: *mut [il::Slot] = &mut [];
11998 11913
        let mut next: u32 = 0;
11999 11914
        let regions = try regionRange(self, &mut next, item.typ);
12000 11915
        try expand(self, item.typ, 0, SlotMode::Full, regions, &mut slots);
12001 -
        set self.tables.layouts[self.tables.layoutBase + item.index].slots = &slots[..];
11916 +
        set self.tables.layouts[item.index].slots = &slots[..];
12002 11917
    }
12003 11918
}
12004 11919
12005 11920
/// Number the layout of a type that has slots, and queue it to get its slots.
12006 11921
fn numberSlotted(self: &mut Lowerer, typ: resolver::Type, layout: resolver::Layout) -> u32 throws (
12008 11923
) {
12009 11924
    // Number the nested layouts first, so that they have lower indices.
12010 11925
    let mut scratch: *mut [il::Slot] = &mut [];
12011 11926
    try expand(self, typ, 0, SlotMode::Nested, &[], &mut scratch);
12012 11927
12013 -
    let index = self.tables.layouts.len - self.tables.layoutBase;
11928 +
    let index = self.tables.layouts.len;
12014 11929
    try self.tables.layouts.append(
12015 11930
        il::Layout {
12016 11931
            size: layout.size,
12017 11932
            alignment: layout.alignment,
12018 11933
            slots: &[],
12328 12243
    }
12329 12244
    let ret = try returnValType(self, fnType);
12330 12245
    let borrows = try regions::contract(self, fnType, requiresReturnParam(
12331 12246
        &self.resolver, fnType
12332 12247
    ));
12333 -
    for i in self.tables.signatureBase .. self.tables.signatures.len {
12248 +
    for i in 0 .. self.tables.signatures.len {
12334 12249
        let existing = self.tables.signatures[i];
12335 12250
        if existing.ret == ret
12336 12251
            and existing.params.len == params.len
12337 12252
            and il::sameBorrows(existing.borrows, borrows)
12338 12253
        {
12342 12257
                    set same = false;
12343 12258
                    break;
12344 12259
                }
12345 12260
            }
12346 12261
            if same {
12347 -
                return i - self.tables.signatureBase;
12262 +
                return i;
12348 12263
            }
12349 12264
        }
12350 12265
    }
12351 -
    let index = self.tables.signatures.len - self.tables.signatureBase;
12266 +
    let index = self.tables.signatures.len;
12352 12267
    try self.tables.signatures.append(
12353 12268
        il::Signature { params: &params[..], ret, borrows }, self.resolver.arena
12354 12269
    ) catch {
12355 12270
        throw LowerError::AllocationFailed;
12356 12271
    };
12369 12284
/// a multiple of that alignment.
12370 12285
fn layoutWith(self: &mut Lowerer, typ: resolver::Type, layout: resolver::Layout) -> u32 throws (
12371 12286
    LowerError
12372 12287
) {
12373 12288
    let index = try layoutOf(self, typ);
12374 -
    let existing = self.tables.layouts[self.tables.layoutBase + index];
12289 +
    let existing = self.tables.layouts[index];
12375 12290
    if layout.alignment <= existing.alignment {
12376 12291
        return index;
12377 12292
    }
12378 12293
    // The size stays a multiple of the alignment, as the verifier requires of a layout.
12379 12294
    return try internLayout(