lib/std/lang/il/binary/writer.rad 28.4 KiB raw
1
//! Bounded binary RIL encoding into caller-owned storage.
2
//! After an error, discard bytes written by the failed operation.
3
4
use std::collections::dict;
5
use std::lang::il;
6
use std::lang::il::binary;
7
use std::lang::il::binary::collect;
8
use std::mem;
9
use std::mem::heap;
10
11
/// Names used by a binary encoder.
12
export trait SymbolTable {
13
    /// Return the stable wire index of a name, adding it when collection is enabled.
14
    fn (&mut SymbolTable) index(name: *[u8]) -> u32 throws (binary::Error);
15
}
16
17
/// Complete wire-order symbol table resolved by direct comparison.
18
export record Listed {
19
    /// Names in wire-index order.
20
    names: *[*[u8]],
21
}
22
23
/// Complete indexed symbol table owned during an encoding call.
24
export record Indexed {
25
    /// Name-to-index table returned to the caller after encoding.
26
    table: dict::Dict,
27
}
28
29
/// Growing symbol table owned during a streaming encoding call.
30
export record Collected {
31
    /// Names in first-use order with their stable index.
32
    names: collect::Names,
33
    /// Storage of copied names and their growing table.
34
    arena: *heap::Heap,
35
}
36
37
instance SymbolTable for Listed {
38
    /// Resolve a name in the supplied table.
39
    fn (symbols: &mut Listed) index(name: *[u8]) -> u32 throws (binary::Error) {
40
        for candidate, i in symbols.names {
41
            if mem::eq(candidate, name) {
42
                return i;
43
            }
44
        }
45
        throw binary::Error::Symbol;
46
    }
47
}
48
49
instance SymbolTable for Indexed {
50
    /// Resolve a name through the index.
51
    fn (symbols: &mut Indexed) index(name: *[u8]) -> u32 throws (binary::Error) {
52
        let found = dict::get(&symbols.table, name) else throw binary::Error::Symbol;
53
        return found as u32;
54
    }
55
}
56
57
instance SymbolTable for Collected {
58
    /// Intern a qualified name and return its first-use index.
59
    fn (symbols: &mut Collected) index(name: *[u8]) -> u32 throws (binary::Error) {
60
        return try collect::reference(&mut symbols.names, name, symbols.arena);
61
    }
62
}
63
64
/// Output buffer and cursor. The caller moves the buffer back after encoding.
65
export record Writer {
66
    /// Destination bytes.
67
    bytes: *mut [u8],
68
    /// Number of bytes written.
69
    offset: u32,
70
}
71
72
/// Create an output cursor over `bytes`.
73
export fn new(bytes: *mut [u8]) -> Writer {
74
    return Writer { bytes, offset: 0 };
75
}
76
77
/// Write the low byte of an integer.
78
export fn byte(out: &mut Writer, value: u64) throws (binary::Error) {
79
    if out.offset >= out.bytes.len {
80
        throw binary::Error::Capacity;
81
    }
82
    set out.bytes[out.offset] = value as u8;
83
    set out.offset += 1;
84
}
85
86
/// Write the low 32 bits of an integer in little-endian order.
87
export fn word(out: &mut Writer, value: u64) throws (binary::Error) {
88
    if out.bytes.len - out.offset < 4 {
89
        throw binary::Error::Capacity;
90
    }
91
    mem::store32Le(&mut out.bytes[..], out.offset, value as u32);
92
    set out.offset += 4;
93
}
94
95
/// Write the low bytes of an integer with width 1, 2, 4, or 8.
96
export fn integer(out: &mut Writer, value: u64, width: u32) throws (binary::Error) {
97
    if width <> 1 and width <> 2 and width <> 4 and width <> 8 {
98
        throw binary::Error::Invalid;
99
    }
100
    if width > out.bytes.len - out.offset {
101
        throw binary::Error::Capacity;
102
    }
103
    let offset = out.offset;
104
    match width {
105
        case 1 => set out.bytes[offset] = value as u8,
106
        case 2 => mem::store16Le(&mut out.bytes[..], offset, value as u16),
107
        case 4 => mem::store32Le(&mut out.bytes[..], offset, value as u32),
108
        case 8 => mem::store64Le(&mut out.bytes[..], offset, value),
109
        else   => throw binary::Error::Invalid,
110
    }
111
    set out.offset += width;
112
}
113
114
/// Write a byte sequence with its u32 length.
115
export fn bytes(out: &mut Writer, value: &[u8]) throws (binary::Error) {
116
    try word(out, value.len as u64);
117
    if value.len > out.bytes.len - out.offset {
118
        throw binary::Error::Capacity;
119
    }
120
    try mem::copy(&mut out.bytes[out.offset .. out.offset + value.len], value) catch {
121
        throw binary::Error::Capacity;
122
    };
123
    set out.offset += value.len;
124
}
125
126
/// Write the index of a name in the package symbol table.
127
export fn symbol(
128
    out: &mut Writer, name: *[u8], symbols: &mut opaque SymbolTable
129
) throws (binary::Error) {
130
    try word(out, try symbols.index(name) as u64);
131
}
132
133
/// Write an IL type as its byte width.
134
export fn typ(out: &mut Writer, value: il::Type) throws (binary::Error) {
135
    try byte(out, il::typeSize(value) as u64);
136
}
137
138
/// Write a pointer type: the pointee layout index and a flag byte.
139
export fn ptrType(out: &mut Writer, value: il::PtrType) throws (binary::Error) {
140
    try word(out, value.layout as u64);
141
    let mut flags: u8 = 0;
142
    if value.own {
143
        set flags |= super::PTR_OWN;
144
    }
145
    if value.mutable {
146
        set flags |= super::PTR_MUTABLE;
147
    }
148
    if value.nullable {
149
        set flags |= super::PTR_NULLABLE;
150
    }
151
    try byte(out, flags as u64);
152
}
153
154
/// Write a value type as a tag followed by its payload.
155
export fn valType(out: &mut Writer, value: il::ValType) throws (binary::Error) {
156
    match value {
157
        case il::ValType::Void => try byte(out, super::VALTYPE_VOID as u64),
158
        case il::ValType::Data(t) => {
159
            try byte(out, super::VALTYPE_DATA as u64);
160
            try typ(out, t);
161
        }
162
        case il::ValType::Ptr(ptr) => {
163
            try byte(out, super::VALTYPE_PTR as u64);
164
            try ptrType(out, ptr);
165
        }
166
        case il::ValType::Fn(signature) => {
167
            try byte(out, super::VALTYPE_FN as u64);
168
            try word(out, signature as u64);
169
        }
170
        case il::ValType::Heap(heap) => {
171
            try byte(out, super::VALTYPE_HEAP as u64);
172
            try heapType(out, heap);
173
        }
174
    }
175
}
176
177
/// Get the flags of the rights of a heap.
178
export fn heapFlags(heap: il::HeapType) -> u8 {
179
    match heap {
180
        case il::HeapType::Plain => return 0,
181
        case il::HeapType::Typed => return super::HEAP_TYPED,
182
        case il::HeapType::Reset => return super::HEAP_RESET,
183
    }
184
}
185
186
/// Write the rights of a heap handle.
187
export fn heapType(out: &mut Writer, heap: il::HeapType) throws (binary::Error) {
188
    try byte(out, heapFlags(heap) as u64);
189
}
190
191
/// Write a list of region indices.
192
fn regions(out: &mut Writer, items: *[u32]) throws (binary::Error) {
193
    try word(out, items.len as u64);
194
    for item in items {
195
        try word(out, item as u64);
196
    }
197
}
198
199
/// Write one layout: size, alignment, regions, and its slots.
200
export fn layout(out: &mut Writer, value: il::Layout) throws (binary::Error) {
201
    try word(out, value.size as u64);
202
    try word(out, value.alignment as u64);
203
    try word(out, value.regions as u64);
204
    try word(out, value.slots.len as u64);
205
    for slot in value.slots {
206
        try word(out, slot.offset as u64);
207
        match slot.kind {
208
            case il::SlotKind::Ptr(ptr) => {
209
                try byte(out, super::SLOT_PTR as u64);
210
                try ptrType(out, ptr);
211
            }
212
            case il::SlotKind::Slice(ptr) => {
213
                try byte(out, super::SLOT_SLICE as u64);
214
                try ptrType(out, ptr);
215
            }
216
            case il::SlotKind::Fn(signature) => {
217
                try byte(out, super::SLOT_FN as u64);
218
                try word(out, signature as u64);
219
            }
220
            case il::SlotKind::Heap(heap) => {
221
                try byte(out, super::SLOT_HEAP as u64);
222
                try heapType(out, heap);
223
            }
224
            case il::SlotKind::Trait(object) => {
225
                try byte(out, super::SLOT_TRAIT as u64);
226
                try word(out, object.iface as u64);
227
                try rights(out, object.own, object.mutable);
228
            }
229
            case il::SlotKind::Union { offset, size, variants } => {
230
                try byte(out, super::SLOT_UNION as u64);
231
                try word(out, offset as u64);
232
                try word(out, size as u64);
233
                try word(out, variants.len as u64);
234
                for variant in variants {
235
                    try word(out, variant.tag as u64);
236
                    try optionalIndex(out, variant.layout);
237
                    try regions(out, variant.regions);
238
                }
239
            }
240
            case il::SlotKind::Sub(sub) => {
241
                try byte(out, super::SLOT_SUB as u64);
242
                try word(out, sub as u64);
243
            }
244
            case il::SlotKind::Array { layout: element, count } => {
245
                try byte(out, super::SLOT_ARRAY as u64);
246
                try word(out, element as u64);
247
                try word(out, count as u64);
248
            }
249
        }
250
        try regions(out, slot.regions);
251
    }
252
}
253
254
/// Write an optional table index: a presence byte, then the index when present.
255
export fn optionalIndex(out: &mut Writer, index: ?u32) throws (binary::Error) {
256
    if let present = index {
257
        try byte(out, 1);
258
        try word(out, present as u64);
259
    } else {
260
        try byte(out, 0);
261
    }
262
}
263
264
/// Write the rights of a receiver as pointer type flags.
265
fn rights(out: &mut Writer, own: bool, mutable: bool) throws (binary::Error) {
266
    let mut flags: u8 = 0;
267
    if own {
268
        set flags |= super::PTR_OWN;
269
    }
270
    if mutable {
271
        set flags |= super::PTR_MUTABLE;
272
    }
273
    try byte(out, flags as u64);
274
}
275
276
/// Write one trait: its methods, each the signature without the receiver, the
277
/// receiver position and the rights the receiver needs.
278
export fn traitRecord(out: &mut Writer, value: il::Trait) throws (binary::Error) {
279
    try word(out, value.methods.len as u64);
280
    for method in value.methods {
281
        try word(out, method.sig as u64);
282
        try word(out, method.receiver as u64);
283
        try rights(out, method.own, method.mutable);
284
    }
285
}
286
287
/// Write one signature: the parameter types, the result type, and the
288
/// parameter that a returned reference borrows from.
289
export fn signature(out: &mut Writer, value: il::Signature) throws (binary::Error) {
290
    try word(out, value.params.len as u64);
291
    for param in value.params {
292
        try valType(out, param);
293
    }
294
    try valType(out, value.ret);
295
    try borrows(out, value.borrows);
296
}
297
298
/// Write the sources of borrowed result slots.
299
export fn borrows(out: &mut Writer, items: *[il::Borrow]) throws (binary::Error) {
300
    try word(out, items.len as u64);
301
    for item in items {
302
        try optionalIndex(out, item.target);
303
        try word(out, item.result as u64);
304
        try word(out, item.param as u64);
305
        try optionalIndex(out, item.source);
306
    }
307
}
308
309
/// Write a tagged value. Symbol addresses use table indices.
310
export fn val(out: &mut Writer, value: il::Val, symbols: &mut opaque SymbolTable)
311
    throws (binary::Error)
312
{
313
    match value {
314
        case il::Val::Reg(reg) => {
315
            try byte(out, super::VALUE_REG as u64);
316
            try word(out, reg.n as u64);
317
        }
318
        case il::Val::Imm(n) => {
319
            try byte(out, super::VALUE_IMM as u64);
320
            try integer(out, n as u64, 8);
321
        }
322
        case il::Val::DataSym(name) => {
323
            try byte(out, super::VALUE_DATASYM as u64);
324
            try symbol(out, name, symbols);
325
        }
326
        case il::Val::FnAddr(name) => {
327
            try byte(out, super::VALUE_FNADDR as u64);
328
            try symbol(out, name, symbols);
329
        }
330
        case il::Val::Undef => try byte(out, super::VALUE_UNDEF as u64),
331
    }
332
}
333
334
/// Write a sequence of values.
335
export fn values(out: &mut Writer, items: &[il::Val], symbols: &mut opaque SymbolTable)
336
    throws (binary::Error)
337
{
338
    try word(out, items.len as u64);
339
    for item in items {
340
        try val(out, item, symbols);
341
    }
342
}
343
344
/// Write an explicit bin operation tag.
345
fn binOp(out: &mut Writer, value: il::BinOp) throws (binary::Error) {
346
    match value {
347
        case il::BinOp::Add  => try byte(out, super::BIN_ADD as u64),
348
        case il::BinOp::Sub  => try byte(out, super::BIN_SUB as u64),
349
        case il::BinOp::Mul  => try byte(out, super::BIN_MUL as u64),
350
        case il::BinOp::Sdiv => try byte(out, super::BIN_SDIV as u64),
351
        case il::BinOp::Udiv => try byte(out, super::BIN_UDIV as u64),
352
        case il::BinOp::Srem => try byte(out, super::BIN_SREM as u64),
353
        case il::BinOp::Urem => try byte(out, super::BIN_UREM as u64),
354
        case il::BinOp::Eq   => try byte(out, super::BIN_EQ as u64),
355
        case il::BinOp::Ne   => try byte(out, super::BIN_NE as u64),
356
        case il::BinOp::Slt  => try byte(out, super::BIN_SLT as u64),
357
        case il::BinOp::Sge  => try byte(out, super::BIN_SGE as u64),
358
        case il::BinOp::Ult  => try byte(out, super::BIN_ULT as u64),
359
        case il::BinOp::Uge  => try byte(out, super::BIN_UGE as u64),
360
        case il::BinOp::And  => try byte(out, super::BIN_AND as u64),
361
        case il::BinOp::Or   => try byte(out, super::BIN_OR as u64),
362
        case il::BinOp::Xor  => try byte(out, super::BIN_XOR as u64),
363
        case il::BinOp::Shl  => try byte(out, super::BIN_SHL as u64),
364
        case il::BinOp::Sshr => try byte(out, super::BIN_SSHR as u64),
365
        case il::BinOp::Ushr => try byte(out, super::BIN_USHR as u64),
366
        case il::BinOp::RotR => try byte(out, super::BIN_ROTR as u64),
367
        case il::BinOp::RotL => try byte(out, super::BIN_ROTL as u64),
368
    }
369
}
370
371
/// Write an explicit un operation tag.
372
fn unOp(out: &mut Writer, value: il::UnOp) throws (binary::Error) {
373
    match value {
374
        case il::UnOp::Neg           => try byte(out, super::UN_NEG as u64),
375
        case il::UnOp::Not           => try byte(out, super::UN_NOT as u64),
376
        case il::UnOp::ByteSwap      => try byte(out, super::UN_BYTE_SWAP as u64),
377
        case il::UnOp::PopCount      => try byte(out, super::UN_POP_COUNT as u64),
378
        case il::UnOp::TrailingZeros => try byte(out, super::UN_TRAILING_ZEROS as u64),
379
        case il::UnOp::LeadingZeros  => try byte(out, super::UN_LEADING_ZEROS as u64),
380
    }
381
}
382
383
/// Write an explicit cmp operation tag.
384
fn cmpOp(out: &mut Writer, value: il::CmpOp) throws (binary::Error) {
385
    match value {
386
        case il::CmpOp::Eq  => try byte(out, super::CMP_EQ as u64),
387
        case il::CmpOp::Ne  => try byte(out, super::CMP_NE as u64),
388
        case il::CmpOp::Slt => try byte(out, super::CMP_SLT as u64),
389
        case il::CmpOp::Ult => try byte(out, super::CMP_ULT as u64),
390
    }
391
}
392
393
/// Write one instruction of `func`. Fields follow the IL record declaration order.
394
/// Argument and case ranges are read from the pools of `func`.
395
export fn instr(out: &mut Writer, func: &il::Fn, item: il::Instr, symbols: &mut opaque SymbolTable)
396
    throws (binary::Error)
397
{
398
    match item {
399
        case il::Instr::Reserve { dst: vdst, layout: vlayout } => {
400
            try byte(out, super::INSTR_RESERVE as u64);
401
            try word(out, vdst.n as u64);
402
            try word(out, vlayout as u64);
403
        }
404
        case il::Instr::Load { typ: vtyp, dst: vdst, src: vsrc, offset: voffset } => {
405
            try byte(out, super::INSTR_LOAD as u64);
406
            try typ(out, vtyp);
407
            try word(out, vdst.n as u64);
408
            try word(out, vsrc.n as u64);
409
            try word(out, voffset as u64);
410
        }
411
        case il::Instr::Sload { typ: vtyp, dst: vdst, src: vsrc, offset: voffset } => {
412
            try byte(out, super::INSTR_SLOAD as u64);
413
            try typ(out, vtyp);
414
            try word(out, vdst.n as u64);
415
            try word(out, vsrc.n as u64);
416
            try word(out, voffset as u64);
417
        }
418
        case il::Instr::Store { typ: vtyp, src: vsrc, dst: vdst, offset: voffset } => {
419
            try byte(out, super::INSTR_STORE as u64);
420
            try typ(out, vtyp);
421
            try val(out, vsrc, symbols);
422
            try word(out, vdst.n as u64);
423
            try word(out, voffset as u64);
424
        }
425
        case il::Instr::Blit { dst: vdst, src: vsrc, layout: vlayout } => {
426
            try byte(out, super::INSTR_BLIT as u64);
427
            try word(out, vdst.n as u64);
428
            try word(out, vsrc.n as u64);
429
            try word(out, vlayout as u64);
430
        }
431
        case il::Instr::Copy { dst: vdst, val: vval } => {
432
            try byte(out, super::INSTR_COPY as u64);
433
            try word(out, vdst.n as u64);
434
            try val(out, vval, symbols);
435
        }
436
        case il::Instr::BinOp { op: vop, typ: vtyp, dst: vdst, a: va, b: vb } => {
437
            try byte(out, super::INSTR_BINOP as u64);
438
            try binOp(out, vop);
439
            try typ(out, vtyp);
440
            try word(out, vdst.n as u64);
441
            try val(out, va, symbols);
442
            try val(out, vb, symbols);
443
        }
444
        case il::Instr::UnOp { op: vop, typ: vtyp, dst: vdst, a: va } => {
445
            try byte(out, super::INSTR_UNOP as u64);
446
            try unOp(out, vop);
447
            try typ(out, vtyp);
448
            try word(out, vdst.n as u64);
449
            try val(out, va, symbols);
450
        }
451
        case il::Instr::Zext { typ: vtyp, dst: vdst, val: vval } => {
452
            try byte(out, super::INSTR_ZEXT as u64);
453
            try typ(out, vtyp);
454
            try word(out, vdst.n as u64);
455
            try val(out, vval, symbols);
456
        }
457
        case il::Instr::Sext { typ: vtyp, dst: vdst, val: vval } => {
458
            try byte(out, super::INSTR_SEXT as u64);
459
            try typ(out, vtyp);
460
            try word(out, vdst.n as u64);
461
            try val(out, vval, symbols);
462
        }
463
        case il::Instr::Call { retTy: vretTy, dst: vdst, func: vfunc, args: vargs } => {
464
            try byte(out, super::INSTR_CALL as u64);
465
            try valType(out, vretTy);
466
            if let present = vdst {
467
                try byte(out, 1);
468
                try word(out, present.n as u64);
469
            } else {
470
                try byte(out, 0);
471
            }
472
            try val(out, vfunc, symbols);
473
            try values(out, il::argVals(func, vargs), symbols);
474
        }
475
        case il::Instr::Ret { val: vval } => {
476
            try byte(out, super::INSTR_RET as u64);
477
            if let present = vval {
478
                try byte(out, 1);
479
                try val(out, present, symbols);
480
            } else {
481
                try byte(out, 0);
482
            }
483
        }
484
        case il::Instr::Jmp { target: vtarget, args: vargs } => {
485
            try byte(out, super::INSTR_JMP as u64);
486
            try word(out, vtarget as u64);
487
            try values(out, il::argVals(func, vargs), symbols);
488
        }
489
        case il::Instr::Br {
490
            op: vop,
491
            typ: vtyp,
492
            a: va,
493
            b: vb,
494
            thenTarget: vthenTarget,
495
            thenArgs: vthenArgs,
496
            elseTarget: velseTarget,
497
            elseArgs: velseArgs,
498
        } => {
499
            try byte(out, super::INSTR_BR as u64);
500
            try cmpOp(out, vop);
501
            try typ(out, vtyp);
502
            try val(out, va, symbols);
503
            try val(out, vb, symbols);
504
            try word(out, vthenTarget as u64);
505
            try values(out, il::argVals(func, vthenArgs), symbols);
506
            try word(out, velseTarget as u64);
507
            try values(out, il::argVals(func, velseArgs), symbols);
508
        }
509
        case il::Instr::Switch {
510
            val: vval, defaultTarget: vdefaultTarget, defaultArgs: vdefaultArgs, cases: vcases
511
        } => {
512
            try byte(out, super::INSTR_SWITCH as u64);
513
            try val(out, vval, symbols);
514
            try word(out, vdefaultTarget as u64);
515
            try values(out, il::argVals(func, vdefaultArgs), symbols);
516
            let branches = il::switchCases(func, vcases);
517
            try word(out, branches.len as u64);
518
            for branch in branches {
519
                try integer(out, branch.value as u64, 8);
520
                try word(out, branch.target as u64);
521
                try values(out, il::argVals(func, branch.args), symbols);
522
            }
523
        }
524
        case il::Instr::Unreachable => {
525
            try byte(out, super::INSTR_UNREACHABLE as u64);
526
        }
527
        case il::Instr::Ecall {
528
            dst: vdst, num: vnum, a0: va0, a1: va1, a2: va2, a3: va3, buf: vbuf
529
        } => {
530
            try byte(out, super::INSTR_ECALL as u64);
531
            try word(out, vdst.n as u64);
532
            try val(out, vnum, symbols);
533
            try val(out, va0, symbols);
534
            try val(out, va1, symbols);
535
            try val(out, va2, symbols);
536
            try val(out, va3, symbols);
537
            if let b = vbuf {
538
                try byte(out, 1);
539
                try word(out, b.ptr.n as u64);
540
                try word(out, b.size as u64);
541
            } else {
542
                try byte(out, 0);
543
            }
544
        }
545
        case il::Instr::PageSlice {
546
            err: verr, slot: vslot, handle: vhandle, write: vwrite, layout: vlayout
547
        } => {
548
            try byte(out, super::INSTR_PAGE_SLICE as u64);
549
            try word(out, verr.n as u64);
550
            try word(out, vslot.n as u64);
551
            try val(out, vhandle, symbols);
552
            try byte(out, 1 if vwrite else 0);
553
            try word(out, vlayout as u64);
554
        }
555
        case il::Instr::PageHeap { err: verr, slot: vslot, handle: vhandle } => {
556
            try byte(out, super::INSTR_PAGE_HEAP as u64);
557
            try word(out, verr.n as u64);
558
            try word(out, vslot.n as u64);
559
            try val(out, vhandle, symbols);
560
        }
561
        case il::Instr::HeapReset { heap } => {
562
            try byte(out, super::INSTR_HEAP_RESET as u64);
563
            try val(out, heap, symbols);
564
        }
565
        case il::Instr::DeviceRead { typ: t, dst, handle, offset } => {
566
            try byte(out, super::INSTR_DEVICE_READ as u64);
567
            try typ(out, t);
568
            try word(out, dst.n as u64);
569
            try val(out, handle, symbols);
570
            try val(out, offset, symbols);
571
        }
572
        case il::Instr::DeviceWrite { typ: t, handle, offset, value } => {
573
            try byte(out, super::INSTR_DEVICE_WRITE as u64);
574
            try typ(out, t);
575
            try val(out, handle, symbols);
576
            try val(out, offset, symbols);
577
            try val(out, value, symbols);
578
        }
579
        case il::Instr::Field { dst: vdst, src: vsrc, offset: voffset } => {
580
            try byte(out, super::INSTR_FIELD as u64);
581
            try word(out, vdst.n as u64);
582
            try word(out, vsrc.n as u64);
583
            try word(out, voffset as u64);
584
        }
585
        case il::Instr::Elem {
586
            dst: vdst, base: vbase, index: vindex, layout: vlayout, count: vcount
587
        } => {
588
            try byte(out, super::INSTR_ELEM as u64);
589
            try word(out, vdst.n as u64);
590
            try word(out, vbase.n as u64);
591
            try val(out, vindex, symbols);
592
            try word(out, vlayout as u64);
593
            try word(out, vcount as u64);
594
        }
595
        case il::Instr::BytesLoad { typ: width, dst, slice, offset } => {
596
            try byte(out, super::INSTR_BYTES_LOAD as u64);
597
            try typ(out, width);
598
            try word(out, dst.n as u64);
599
            try word(out, slice.n as u64);
600
            try val(out, offset, symbols);
601
        }
602
        case il::Instr::BytesStore { typ: width, slice, offset, value } => {
603
            try byte(out, super::INSTR_BYTES_STORE as u64);
604
            try typ(out, width);
605
            try word(out, slice.n as u64);
606
            try val(out, offset, symbols);
607
            try val(out, value, symbols);
608
        }
609
        case il::Instr::SliceElem { dst: vdst, slice: vslice, index: vindex, layout: vlayout } => {
610
            try byte(out, super::INSTR_SLICE_ELEM as u64);
611
            try word(out, vdst.n as u64);
612
            try word(out, vslice.n as u64);
613
            try val(out, vindex, symbols);
614
            try word(out, vlayout as u64);
615
        }
616
        case il::Instr::SliceNew {
617
            dst: vdst, base: vbase, layout: vlayout, count: vcount, start: vstart, end: vend
618
        } => {
619
            try byte(out, super::INSTR_SLICE_NEW as u64);
620
            try word(out, vdst.n as u64);
621
            try val(out, vbase, symbols);
622
            try word(out, vlayout as u64);
623
            try word(out, vcount as u64);
624
            try val(out, vstart, symbols);
625
            try val(out, vend, symbols);
626
        }
627
        case il::Instr::SliceSub {
628
            dst: vdst, src: vsrc, layout: vlayout, start: vstart, end: vend
629
        } => {
630
            try byte(out, super::INSTR_SLICE_SUB as u64);
631
            try word(out, vdst.n as u64);
632
            try word(out, vsrc.n as u64);
633
            try word(out, vlayout as u64);
634
            try val(out, vstart, symbols);
635
            try val(out, vend, symbols);
636
        }
637
        case il::Instr::SliceLen { dst: vdst, layout: vlayout, length: vlength } => {
638
            try byte(out, super::INSTR_SLICE_LEN as u64);
639
            try word(out, vdst.n as u64);
640
            try word(out, vlayout as u64);
641
            try val(out, vlength, symbols);
642
        }
643
        case il::Instr::SliceCopy { dst: vdst, src: vsrc, layout: vlayout } => {
644
            try byte(out, super::INSTR_SLICE_COPY as u64);
645
            try word(out, vdst.n as u64);
646
            try word(out, vsrc.n as u64);
647
            try word(out, vlayout as u64);
648
        }
649
        case il::Instr::SliceGrow {
650
            ok: vok, slice: vslice, heap: vheap, layout: vlayout, capacity: vcapacity
651
        } => {
652
            try byte(out, super::INSTR_SLICE_GROW as u64);
653
            try word(out, vok.n as u64);
654
            try word(out, vslice.n as u64);
655
            try val(out, vheap, symbols);
656
            try word(out, vlayout as u64);
657
            try val(out, vcapacity, symbols);
658
        }
659
        case il::Instr::LoadPtr { dst: vdst, src: vsrc, offset: voffset, nullable } => {
660
            try byte(out, super::INSTR_LOAD_PTR as u64);
661
            try word(out, vdst.n as u64);
662
            try word(out, vsrc.n as u64);
663
            try word(out, voffset as u64);
664
            try byte(out, 1 if nullable else 0);
665
        }
666
        case il::Instr::StorePtr { src: vsrc, dst: vdst, offset: voffset } => {
667
            try byte(out, super::INSTR_STORE_PTR as u64);
668
            try val(out, vsrc, symbols);
669
            try word(out, vdst.n as u64);
670
            try word(out, voffset as u64);
671
        }
672
        case il::Instr::TraitNew { dst: vdst, recv: vrecv, table: vtable } => {
673
            try byte(out, super::INSTR_TRAIT_NEW as u64);
674
            try word(out, vdst.n as u64);
675
            try word(out, vrecv.n as u64);
676
            try val(out, vtable, symbols);
677
        }
678
        case il::Instr::TraitCopy { dst: vdst, src: vsrc } => {
679
            try byte(out, super::INSTR_TRAIT_COPY as u64);
680
            try word(out, vdst.n as u64);
681
            try word(out, vsrc.n as u64);
682
        }
683
        case il::Instr::UnionGet { dst: vdst, src: vsrc, tag, offset, layout: vlayout } => {
684
            try byte(out, super::INSTR_UNION_GET as u64);
685
            try word(out, vdst.n as u64);
686
            try word(out, vsrc.n as u64);
687
            try word(out, tag as u64);
688
            try word(out, offset as u64);
689
            try word(out, vlayout as u64);
690
        }
691
        case il::Instr::UnionSet { dst: vdst, tag, offset, src: vsrc, layout: vlayout } => {
692
            try byte(out, super::INSTR_UNION_SET as u64);
693
            try word(out, vdst.n as u64);
694
            try word(out, tag as u64);
695
            try word(out, offset as u64);
696
            try val(out, vsrc, symbols);
697
            try optionalIndex(out, vlayout);
698
        }
699
        case il::Instr::TraitCall {
700
            retTy: vretTy, dst: vdst, obj: vobj, method: vmethod, receiver: vreceiver, args: vargs
701
        } => {
702
            try byte(out, super::INSTR_TRAIT_CALL as u64);
703
            try valType(out, vretTy);
704
            if let present = vdst {
705
                try byte(out, 1);
706
                try word(out, present.n as u64);
707
            } else {
708
                try byte(out, 0);
709
            }
710
            try word(out, vobj.n as u64);
711
            try word(out, vmethod as u64);
712
            try word(out, vreceiver as u64);
713
            try values(out, il::argVals(func, vargs), symbols);
714
        }
715
        case il::Instr::Ebreak => {
716
            try byte(out, super::INSTR_EBREAK as u64);
717
        }
718
        case il::Instr::MemoryFence => {
719
            try byte(out, super::INSTR_MEMORYFENCE as u64);
720
        }
721
    }
722
}
723
724
/// Write one data initializer and its repetition count.
725
export fn dataValue(out: &mut Writer, value: il::DataValue, symbols: &mut opaque SymbolTable)
726
    throws (binary::Error)
727
{
728
    match value.item {
729
        case il::DataItem::Val { typ: t, val: n } => {
730
            try byte(out, super::DATA_VAL as u64);
731
            try typ(out, t);
732
            try integer(out, n as u64, il::typeSize(t));
733
        }
734
        case il::DataItem::Sym(name) => {
735
            try byte(out, super::DATA_SYM as u64);
736
            try symbol(out, name, symbols);
737
        }
738
        case il::DataItem::Fn(name) => {
739
            try byte(out, super::DATA_FN as u64);
740
            try symbol(out, name, symbols);
741
        }
742
        case il::DataItem::Str(text) => {
743
            try byte(out, super::DATA_STR as u64);
744
            try bytes(out, text);
745
        }
746
        case il::DataItem::Undef => try byte(out, super::DATA_UNDEF as u64),
747
    }
748
    try word(out, value.count as u64);
749
}