lib/std/lang/il/printer/tests.rad 23.5 KiB raw
1
//! Tests for the IL printer.
2
3
use std::mem;
4
use std::testing;
5
use std::mem::heap;
6
use std::lang::il;
7
use std::lang::sexpr;
8
9
/// Storage for the programs of the tests.
10
static PROGRAM_STORAGE: [u8; 16384] = [0; 16384];
11
/// Output buffer of the test that prints a whole program.
12
static WHOLE: [u8; 512] = [0; 512];
13
/// Output buffer of the test that prints the data and the function text apart.
14
static SPLIT: [u8; 512] = [0; 512];
15
/// Text buffer for functions printed one at a time.
16
static FN_TEXT: [u8; 512] = [0; 512];
17
/// Layout of every data declaration of the tests.
18
constant LAYOUTS: [il::Layout; 1] = [il::Layout { size: 4, alignment: 4, slots: &[], regions: 0 }];
19
/// Initializer of every data declaration of the tests.
20
constant VALUES: [il::DataValue; 1] = [
21
    il::DataValue {
22
        item: il::DataItem::Val { typ: il::Type::W32, val: 7 },
23
        count: 1,
24
    },
25
];
26
27
/// Build a list of data declarations that have the given names.
28
fn dataList(a: &heap::Heap, names: &[*[u8]]) -> *[il::Data] {
29
    let mut list: *mut [il::Data] = &mut [];
30
    for name in names {
31
        try! list.append(
32
            il::Data {
33
                name,
34
                size: 4,
35
                alignment: 4,
36
                layout: 0,
37
                readOnly: true,
38
                heap: nil,
39
                isZeroInit: false,
40
                values: &VALUES[..],
41
            },
42
            a
43
        );
44
    }
45
    return &list[..];
46
}
47
48
/// Build a list of functions that have the given names. Odd positions are
49
/// extern functions, which print without a body.
50
fn fnList(a: &heap::Heap, names: &[*[u8]]) -> *[*il::Fn] {
51
    let mut list: *mut [*il::Fn] = &mut [];
52
    for name, i in names {
53
        let mut funcs: *mut [il::Fn] = &mut [];
54
        try! funcs.append(
55
            il::Fn {
56
                name,
57
                params: &[],
58
                returnType: il::ValType::Data(il::Type::W32),
59
                isExtern: i % 2 == 1,
60
                isLeaf: true,
61
                blocks: &[],
62
                vals: &[],
63
                cases: &[],
64
                borrows: &[],
65
            },
66
            a
67
        );
68
        try! list.append(&mut funcs[0], a);
69
    }
70
    return &list[..];
71
}
72
73
/// Function and signature text identifies a contents borrow with a star.
74
@test unsafe fn contentsBorrow() throws (testing::TestError) {
75
    let a = heap::over(&mut PROGRAM_STORAGE[..]);
76
    let functions = fnList(a, &["view"]);
77
    let mut func = *functions[0];
78
    set func.borrows = &[il::Borrow { target: nil, result: 0, param: 1, source: 0 }];
79
    let mut stored: *mut [il::Fn] = &mut [];
80
    try! stored.append(func, a);
81
    let mut out = sexpr::Buffer { buf: &mut FN_TEXT[..], pos: 0 };
82
    super::printFn(&mut out, &stored[0]);
83
    assert mem::find(&FN_TEXT[.. out.pos], " borrows 0<-*1.0") <> nil;
84
    let signature = il::Signature {
85
        params: &[],
86
        ret: il::ValType::Void,
87
        borrows: &[il::Borrow { target: nil, result: 0, param: 1, source: 0 }],
88
    };
89
    set out.pos = 0;
90
    super::printSpooled(&mut out, &[], &[signature], &[], &[], "");
91
    assert mem::find(&FN_TEXT[.. out.pos], " borrows 0<-*1.0") <> nil;
92
}
93
94
/// Print `program` as a whole, and again as the data followed by the text of
95
/// its functions, printed one at a time. Expect the same text.
96
unsafe fn expectSplitMatchesWhole(program: il::Program) throws (testing::TestError) {
97
    let mut whole = sexpr::Buffer { buf: &mut WHOLE[..], pos: 0 };
98
    super::printProgram(&mut whole, &program);
99
100
    let mut fnText = sexpr::Buffer { buf: &mut FN_TEXT[..], pos: 0 };
101
    for func in program.fns {
102
        if fnText.pos > 0 {
103
            let sink: &mut opaque sexpr::Output = &mut fnText;
104
            sink.write("\n");
105
        }
106
        super::printFn(&mut fnText, func);
107
    }
108
    let mut split = sexpr::Buffer { buf: &mut SPLIT[..], pos: 0 };
109
    super::printSpooled(
110
        &mut split,
111
        program.layouts,
112
        program.signatures,
113
        program.traits,
114
        program.data,
115
        &FN_TEXT[.. fnText.pos]
116
    );
117
118
    try testing::expect(whole.pos < WHOLE.len);
119
    try testing::expect(mem::eq(&WHOLE[.. whole.pos], &SPLIT[.. split.pos]));
120
}
121
122
/// Test that data and functions printed apart match the whole program.
123
@test unsafe fn testSpooledProgramMatchesWhole() throws (testing::TestError) {
124
    let arena = heap::over(&mut PROGRAM_STORAGE[..]);
125
    let a: &heap::Heap = arena;
126
    try expectSplitMatchesWhole(il::Program {
127
        data: dataList(a, &["a", "b"]),
128
        fns: fnList(a, &["f", "g", "h"]),
129
        layouts: &LAYOUTS[..],
130
        signatures: &[],
131
        traits: &[],
132
    });
133
}
134
135
/// Test a program that has data and no functions.
136
@test unsafe fn testSpooledProgramDataOnly() throws (testing::TestError) {
137
    let arena = heap::over(&mut PROGRAM_STORAGE[..]);
138
    let a: &heap::Heap = arena;
139
    try expectSplitMatchesWhole(il::Program {
140
        data: dataList(a, &["a"]), fns: &[], layouts: &LAYOUTS[..], signatures: &[], traits: &[]
141
    });
142
}
143
144
/// Test a program that has functions and no data.
145
@test unsafe fn testSpooledProgramFnsOnly() throws (testing::TestError) {
146
    let arena = heap::over(&mut PROGRAM_STORAGE[..]);
147
    try expectSplitMatchesWhole(il::Program {
148
        data: &[], fns: fnList(arena, &["f", "g"]), layouts: &[], signatures: &[], traits: &[]
149
    });
150
}
151
152
/// Test an empty program.
153
@test unsafe fn testSpooledProgramEmpty() throws (testing::TestError) {
154
    try expectSplitMatchesWhole(il::Program {
155
        data: &[], fns: &[], layouts: &[], signatures: &[], traits: &[]
156
    });
157
}
158
159
/// Test the separators of a program with data and two functions.
160
@test unsafe fn testProgramSeparators() throws (testing::TestError) {
161
    let arena = heap::over(&mut PROGRAM_STORAGE[..]);
162
    let a: &heap::Heap = arena;
163
    let program = il::Program {
164
        data: dataList(a, &["a"]),
165
        fns: fnList(a, &["f", "g"]),
166
        layouts: &LAYOUTS[..],
167
        signatures: &[],
168
        traits: &[],
169
    };
170
    let mut whole = sexpr::Buffer { buf: &mut WHOLE[..], pos: 0 };
171
    super::printProgram(&mut whole, &program);
172
173
    try expectSegments(
174
        &WHOLE[.. whole.pos],
175
        &[
176
            "layout $L0 size 4 align 4 {}\n\n",
177
            "data $a align 4 $L0 {\n    w32 7;\n}\n\n",
178
            "fn w32 $f() {\n}\n\nextern fn w32 $g();\n",
179
        ]
180
    );
181
}
182
183
/// Output buffer of the label tests.
184
static LABEL_TEXT: [u8; 64] = [0; 64];
185
/// Output buffer of the test that prints a function with labelled blocks.
186
static LABEL_FN: [u8; 256] = [0; 256];
187
188
/// Expect that `label` prints as `expected`.
189
unsafe fn expectLabel(label: il::BlockLabel, expected: *[u8]) throws (testing::TestError) {
190
    let mut out = sexpr::Buffer { buf: &mut LABEL_TEXT[..], pos: 0 };
191
    super::writeLabel(&mut out, label);
192
    try testing::expect(mem::eq(&LABEL_TEXT[.. out.pos], expected));
193
}
194
195
/// Test that a label prints its base followed by the decimal suffix.
196
@test unsafe fn testLabelWithSuffix() throws (testing::TestError) {
197
    try expectLabel(il::BlockLabel { base: "then", suffix: 0 }, "then0");
198
    try expectLabel(il::BlockLabel { base: "then", suffix: 7 }, "then7");
199
    try expectLabel(il::BlockLabel { base: "resize.fill", suffix: 10 }, "resize.fill10");
200
    try expectLabel(il::BlockLabel { base: "else", suffix: 1234567890 }, "else1234567890");
201
    try expectLabel(il::BlockLabel { base: "x", suffix: 4294967295 }, "x4294967295");
202
}
203
204
/// Test that a label without a suffix prints its base alone, and that a base
205
/// that ends in a digit keeps it.
206
@test unsafe fn testLabelWithoutSuffix() throws (testing::TestError) {
207
    try expectLabel(il::BlockLabel { base: "entry", suffix: nil }, "entry");
208
    try expectLabel(il::BlockLabel { base: "w32", suffix: nil }, "w32");
209
    try expectLabel(il::BlockLabel { base: "w32", suffix: 0 }, "w320");
210
}
211
212
/// Test that block headers and branch targets print the same label text.
213
@test unsafe fn testBlockLabelsInFunction() throws (testing::TestError) {
214
    let arena = heap::over(&mut PROGRAM_STORAGE[..]);
215
    let a: &heap::Heap = arena;
216
    let none = il::ArgRange { start: 0, len: 0 };
217
    let mut blocks: *mut [il::Block] = &mut [];
218
    let mut entryInstrs: *mut [il::Instr] = &mut [];
219
    try! entryInstrs.append(
220
        il::Instr::Br {
221
            op: il::CmpOp::Eq,
222
            typ: il::Type::W32,
223
            a: il::Val::Imm(1),
224
            b: il::Val::Imm(2),
225
            thenTarget: 1,
226
            thenArgs: none,
227
            elseTarget: 2,
228
            elseArgs: none,
229
        },
230
        a
231
    );
232
    let mut thenInstrs: *mut [il::Instr] = &mut [];
233
    try! thenInstrs.append(il::Instr::Jmp { target: 2, args: none }, a);
234
    let mut doneInstrs: *mut [il::Instr] = &mut [];
235
    try! doneInstrs.append(il::Instr::Ret { val: nil }, a);
236
    try! blocks.append(
237
        il::Block {
238
            label: il::BlockLabel { base: "entry", suffix: 0 },
239
            params: &[],
240
            instrs: &entryInstrs[..],
241
            locs: &[],
242
            preds: &[],
243
            loopDepth: 0,
244
        },
245
        a
246
    );
247
    try! blocks.append(
248
        il::Block {
249
            label: il::BlockLabel { base: "then", suffix: 12 },
250
            params: &[],
251
            instrs: &thenInstrs[..],
252
            locs: &[],
253
            preds: &[],
254
            loopDepth: 0,
255
        },
256
        a
257
    );
258
    try! blocks.append(
259
        il::Block {
260
            label: il::BlockLabel { base: "done", suffix: nil },
261
            params: &[],
262
            instrs: &doneInstrs[..],
263
            locs: &[],
264
            preds: &[],
265
            loopDepth: 0,
266
        },
267
        a
268
    );
269
    let mut funcs: *mut [il::Fn] = &mut [];
270
    try! funcs.append(
271
        il::Fn {
272
            name: "f",
273
            params: &[],
274
            returnType: il::ValType::Data(il::Type::W32),
275
            isExtern: false,
276
            isLeaf: true,
277
            blocks: &blocks[..],
278
            vals: &[],
279
            cases: &[],
280
            borrows: &[],
281
        },
282
        a
283
    );
284
285
    let mut out = sexpr::Buffer { buf: &mut LABEL_FN[..], pos: 0 };
286
    super::printFn(&mut out, &mut funcs[0]);
287
    let expected =
288
        "fn w32 $f() {\n  @entry0\n    br.eq w32 1 2 @then12 @done;\n  @then12\n    jmp @done;\n  @done\n    ret;\n}\n";
289
    try testing::expect(mem::eq(&LABEL_FN[.. out.pos], expected));
290
}
291
292
/// Output buffer of the tests that print tables.
293
static TABLES: [u8; 1024] = [0; 1024];
294
295
/// Expect that `actual` is the concatenation of `segments`.
296
fn expectSegments(actual: *[u8], segments: &[*[u8]]) throws (testing::TestError) {
297
    let mut at: u32 = 0;
298
    for segment in segments {
299
        try testing::expect(at + segment.len <= actual.len);
300
        try testing::expect(mem::eq(&actual[at .. at + segment.len], segment));
301
        set at += segment.len;
302
    }
303
    try testing::expect(at == actual.len);
304
}
305
306
/// Test that layouts print with every kind of slot, and that signatures print their types.
307
@test unsafe fn testLayoutsAndSignatures() throws (testing::TestError) {
308
    let layouts = [
309
        il::Layout { size: 1, alignment: 1, slots: &[], regions: 0 },
310
        il::Layout {
311
            size: 16,
312
            alignment: 8,
313
            slots: &[
314
                il::Slot {
315
                    offset: 0,
316
                    kind: il::SlotKind::Ptr(il::PtrType {
317
                        layout: 0, own: true, mutable: true, nullable: true
318
                    }),
319
                    regions: &[],
320
                },
321
                il::Slot { offset: 8, kind: il::SlotKind::Fn(1), regions: &[] },
322
            ],
323
            regions: 0,
324
        },
325
        il::Layout {
326
            size: 64,
327
            alignment: 8,
328
            slots: &[
329
                il::Slot {
330
                    offset: 0,
331
                    kind: il::SlotKind::Slice(il::PtrType {
332
                        layout: 0, own: false, mutable: false, nullable: true
333
                    }),
334
                    regions: &[],
335
                },
336
                il::Slot { offset: 16, kind: il::SlotKind::Sub(1), regions: &[] },
337
                il::Slot {
338
                    offset: 32, kind: il::SlotKind::Array { layout: 1, count: 2 }, regions: &[]
339
                },
340
            ],
341
            regions: 0,
342
        },
343
        il::Layout {
344
            size: 16,
345
            alignment: 8,
346
            slots: &[
347
                il::Slot {
348
                    offset: 0,
349
                    kind: il::SlotKind::Union {
350
                        offset: 8,
351
                        size: 16,
352
                        variants: &[
353
                            il::UnionVariant { tag: 0, layout: 1, regions: &[] },
354
                            il::UnionVariant { tag: 2, layout: nil, regions: &[] },
355
                        ],
356
                    },
357
                    regions: &[],
358
                },
359
            ],
360
            regions: 0,
361
        },
362
    ];
363
    let signatures = [
364
        il::Signature { params: &[], ret: il::ValType::Void, borrows: &[] },
365
        il::Signature {
366
            params: &[
367
                il::ValType::Data(il::Type::W8),
368
                il::ValType::Ptr(il::PtrType {
369
                    layout: 2, own: false, mutable: true, nullable: false
370
                }),
371
                il::ValType::Ptr(il::PtrType {
372
                    layout: 1, own: true, mutable: false, nullable: true
373
                }),
374
                il::ValType::Fn(0),
375
            ],
376
            ret: il::ValType::Ptr(il::PtrType {
377
                layout: 0, own: true, mutable: false, nullable: true
378
            }),
379
            borrows: &[],
380
        },
381
    ];
382
    let mut out = sexpr::Buffer { buf: &mut TABLES[..], pos: 0 };
383
    super::printSpooled(&mut out, &layouts[..], &signatures[..], &[], &[], "");
384
    try expectSegments(
385
        &TABLES[.. out.pos],
386
        &[
387
            "layout $L0 size 1 align 1 {}\n",
388
            "layout $L1 size 16 align 8 {\n    slot 0 ptr own mut $L0;\n    slot 8 fn $S1;\n}\n",
389
            "layout $L2 size 64 align 8 {\n",
390
            "    slot 0 slice ref const $L0;\n    slot 16 sub $L1;\n    slot 32 array $L1 2;\n}\n",
391
            "layout $L3 size 16 align 8 {\n    slot 0 union offset 8 size 16 0:$L1 2:void;\n}\n",
392
            "\nsignature $S0 () -> void;\n",
393
            "signature $S1 (w8, ptr ref mut $L2, ptr own const nullable $L1, fn $S0)",
394
            " -> ptr own const nullable $L0;\n",
395
        ]
396
    );
397
}
398
399
/// Test that parameters, results, reserves and calls print their value types and layouts.
400
@test unsafe fn testTypedFunction() throws (testing::TestError) {
401
    let arena = heap::over(&mut PROGRAM_STORAGE[..]);
402
    let a: &heap::Heap = arena;
403
    let ptr = il::ValType::Ptr(il::PtrType {
404
        layout: 3, own: false, mutable: true, nullable: false
405
    });
406
    let mut params: *mut [il::Param] = &mut [];
407
    try! params.append(il::Param { value: il::Reg { n: 0 }, type: ptr }, a);
408
    try! params.append(il::Param { value: il::Reg { n: 1 }, type: il::ValType::Fn(2) }, a);
409
    let mut vals: *mut [il::Val] = &mut [];
410
    try! vals.append(il::Val::Reg(il::Reg { n: 2 }), a);
411
    let mut instrs: *mut [il::Instr] = &mut [];
412
    try! instrs.append(il::Instr::Reserve { dst: il::Reg { n: 2 }, layout: 3 }, a);
413
    try! instrs.append(
414
        il::Instr::Blit { dst: il::Reg { n: 2 }, src: il::Reg { n: 0 }, layout: 3 }, a
415
    );
416
    try! instrs.append(
417
        il::Instr::Call {
418
            retTy: ptr,
419
            dst: il::Reg { n: 3 },
420
            func: il::Val::Reg(il::Reg { n: 1 }),
421
            args: il::ArgRange { start: 0, len: 1 },
422
        },
423
        a
424
    );
425
    try! instrs.append(il::Instr::Ret { val: il::Val::Reg(il::Reg { n: 3 }) }, a);
426
    let mut blocks: *mut [il::Block] = &mut [];
427
    try! blocks.append(
428
        il::Block {
429
            label: il::BlockLabel { base: "entry", suffix: 0 },
430
            params: &[],
431
            instrs: &instrs[..],
432
            locs: &[],
433
            preds: &[],
434
            loopDepth: 0,
435
        },
436
        a
437
    );
438
    let mut funcs: *mut [il::Fn] = &mut [];
439
    try! funcs.append(
440
        il::Fn {
441
            name: "f",
442
            params: &params[..],
443
            returnType: ptr,
444
            isExtern: false,
445
            isLeaf: false,
446
            blocks: &blocks[..],
447
            vals: &vals[..],
448
            cases: &[],
449
            borrows: &[],
450
        },
451
        a
452
    );
453
454
    let mut out = sexpr::Buffer { buf: &mut TABLES[..], pos: 0 };
455
    super::printFn(&mut out, &mut funcs[0]);
456
    try expectSegments(
457
        &TABLES[.. out.pos],
458
        &[
459
            "fn ptr ref mut $L3 $f(ptr ref mut $L3 %0, fn $S2 %1) {\n  @entry0\n",
460
            "    reserve %2 $L3;\n    blit %2 %0 $L3;\n",
461
            "    call ptr ref mut $L3 %3 %1(%2);\n    ret %3;\n}\n",
462
        ]
463
    );
464
}
465
466
/// Test that the typed memory instructions print with their operands and layouts.
467
@test unsafe fn testTypedMemoryInstructions() throws (testing::TestError) {
468
    let arena = heap::over(&mut PROGRAM_STORAGE[..]);
469
    let a: &heap::Heap = arena;
470
    let mut instrs: *mut [il::Instr] = &mut [];
471
    try! instrs.append(
472
        il::Instr::Field {
473
            dst: il::Reg { n: 1 }, src: il::Reg { n: 0 }, offset: 8
474
        },
475
        a
476
    );
477
    try! instrs.append(
478
        il::Instr::Elem {
479
            dst: il::Reg { n: 2 },
480
            base: il::Reg { n: 1 },
481
            index: il::Val::Reg(il::Reg { n: 9 }),
482
            layout: 4,
483
            count: 3,
484
        },
485
        a
486
    );
487
    try! instrs.append(
488
        il::Instr::SliceElem {
489
            dst: il::Reg { n: 3 }, slice: il::Reg { n: 1 }, index: il::Val::Imm(2), layout: 4
490
        },
491
        a
492
    );
493
    try! instrs.append(
494
        il::Instr::SliceNew {
495
            dst: il::Reg { n: 4 },
496
            base: il::Val::Reg(il::Reg { n: 2 }),
497
            layout: 4,
498
            count: 3,
499
            start: il::Val::Imm(1),
500
            end: il::Val::Reg(il::Reg { n: 9 }),
501
        },
502
        a
503
    );
504
    try! instrs.append(
505
        il::Instr::SliceSub {
506
            dst: il::Reg { n: 5 },
507
            src: il::Reg { n: 4 },
508
            layout: 4,
509
            start: il::Val::Imm(0),
510
            end: il::Val::Imm(1),
511
        },
512
        a
513
    );
514
    try! instrs.append(
515
        il::Instr::SliceLen {
516
            dst: il::Reg { n: 5 }, layout: 4, length: il::Val::Reg(il::Reg { n: 9 })
517
        },
518
        a
519
    );
520
    try! instrs.append(
521
        il::Instr::SliceCopy { dst: il::Reg { n: 5 }, src: il::Reg { n: 4 }, layout: 4 },
522
        a
523
    );
524
    try! instrs.append(
525
        il::Instr::SliceGrow {
526
            ok: il::Reg { n: 8 },
527
            slice: il::Reg { n: 5 },
528
            heap: il::Val::Reg(il::Reg { n: 9 }),
529
            layout: 4,
530
            capacity: il::Val::Imm(7),
531
        },
532
        a
533
    );
534
    try! instrs.append(
535
        il::Instr::LoadPtr {
536
            dst: il::Reg { n: 6 }, src: il::Reg { n: 0 }, offset: 16, nullable: false
537
        },
538
        a
539
    );
540
    try! instrs.append(
541
        il::Instr::LoadPtr {
542
            dst: il::Reg { n: 7 }, src: il::Reg { n: 0 }, offset: 16, nullable: true
543
        },
544
        a
545
    );
546
    try! instrs.append(
547
        il::Instr::StorePtr {
548
            src: il::Val::Reg(il::Reg { n: 7 }),
549
            dst: il::Reg {
550
                n: 0
551
            },
552
            offset: 16,
553
        },
554
        a
555
    );
556
    try! instrs.append(il::Instr::Ret { val: nil }, a);
557
    let mut blocks: *mut [il::Block] = &mut [];
558
    try! blocks.append(
559
        il::Block {
560
            label: il::BlockLabel { base: "entry", suffix: 0 },
561
            params: &[],
562
            instrs: &instrs[..],
563
            locs: &[],
564
            preds: &[],
565
            loopDepth: 0,
566
        },
567
        a
568
    );
569
    let mut funcs: *mut [il::Fn] = &mut [];
570
    try! funcs.append(
571
        il::Fn {
572
            name: "f",
573
            params: &[],
574
            returnType: il::ValType::Void,
575
            isExtern: false,
576
            isLeaf: true,
577
            blocks: &blocks[..],
578
            vals: &[],
579
            cases: &[],
580
            borrows: &[],
581
        },
582
        a
583
    );
584
    let mut out = sexpr::Buffer { buf: &mut TABLES[..], pos: 0 };
585
    super::printFn(&mut out, &mut funcs[0]);
586
    try expectSegments(
587
        &TABLES[.. out.pos],
588
        &[
589
            "fn void $f() {\n  @entry0\n",
590
            "    field %1 %0 8;\n    elem %2 %1 %9 $L4 3;\n    slice.elem %3 %1 2 $L4;\n",
591
            "    slice.new %4 %2 $L4 3 1 %9;\n    slice.sub %5 %4 $L4 0 1;\n",
592
            "    slice.len %5 $L4 %9;\n    slice.copy %5 %4 $L4;\n",
593
            "    slice.grow %8 %5 %9 $L4 7;\n",
594
            "    load.ptr %6 %0 16;\n    load.ptr nullable %7 %0 16;\n    store.ptr %7 %0 16;\n    ret;\n}\n",
595
        ]
596
    );
597
}
598
599
/// An environment call prints its buffer, and a page slice its access.
600
@test unsafe fn testEcallBufferAndPageSlice() throws (testing::TestError) {
601
    let arena = heap::over(&mut PROGRAM_STORAGE[..]);
602
    let a: &heap::Heap = arena;
603
    let mut instrs: *mut [il::Instr] = &mut [];
604
    try! instrs.append(
605
        il::Instr::Ecall {
606
            dst: il::Reg { n: 1 },
607
            num: il::Val::Imm(44),
608
            a0: il::Val::Reg(il::Reg { n: 0 }),
609
            a1: il::Val::Imm(0),
610
            a2: il::Val::Imm(0),
611
            a3: il::Val::Imm(0),
612
            buf: il::EcallBuf { ptr: il::Reg { n: 2 }, size: 16 },
613
        },
614
        a
615
    );
616
    try! instrs.append(
617
        il::Instr::Ecall {
618
            dst: il::Reg { n: 3 },
619
            num: il::Val::Imm(1),
620
            a0: il::Val::Imm(0),
621
            a1: il::Val::Imm(0),
622
            a2: il::Val::Imm(0),
623
            a3: il::Val::Imm(0),
624
            buf: nil,
625
        },
626
        a
627
    );
628
    try! instrs.append(
629
        il::Instr::PageSlice {
630
            err: il::Reg { n: 4 },
631
            slot: il::Reg { n: 5 },
632
            handle: il::Val::Reg(il::Reg { n: 0 }),
633
            write: true,
634
            layout: 2,
635
        },
636
        a
637
    );
638
    try! instrs.append(
639
        il::Instr::PageSlice {
640
            err: il::Reg { n: 6 },
641
            slot: il::Reg { n: 5 },
642
            handle: il::Val::Imm(0),
643
            write: false,
644
            layout: 2,
645
        },
646
        a
647
    );
648
    try! instrs.append(
649
        il::Instr::PageHeap {
650
            err: il::Reg { n: 7 }, slot: il::Reg { n: 8 }, handle: il::Val::Reg(il::Reg { n: 0 })
651
        },
652
        a
653
    );
654
    try! instrs.append(
655
        il::Instr::UnionGet {
656
            dst: il::Reg { n: 9 }, src: il::Reg { n: 0 }, tag: 3, offset: 8, layout: 2
657
        },
658
        a
659
    );
660
    try! instrs.append(
661
        il::Instr::UnionSet {
662
            dst: il::Reg { n: 0 },
663
            tag: 3,
664
            offset: 8,
665
            src: il::Val::Reg(il::Reg { n: 9 }),
666
            layout: 2,
667
        },
668
        a
669
    );
670
    try! instrs.append(
671
        il::Instr::UnionSet {
672
            dst: il::Reg { n: 0 }, tag: 4, offset: 8, src: il::Val::Undef, layout: nil
673
        },
674
        a
675
    );
676
    try! instrs.append(il::Instr::HeapReset { heap: il::Val::Reg(il::Reg { n: 0 }) }, a);
677
    try! instrs.append(il::Instr::Ret { val: nil }, a);
678
    let mut blocks: *mut [il::Block] = &mut [];
679
    try! blocks.append(
680
        il::Block {
681
            label: il::BlockLabel { base: "entry", suffix: 0 },
682
            params: &[],
683
            instrs: &instrs[..],
684
            locs: &[],
685
            preds: &[],
686
            loopDepth: 0,
687
        },
688
        a
689
    );
690
    let mut funcs: *mut [il::Fn] = &mut [];
691
    try! funcs.append(
692
        il::Fn {
693
            name: "f",
694
            params: &[],
695
            returnType: il::ValType::Void,
696
            isExtern: false,
697
            isLeaf: false,
698
            blocks: &blocks[..],
699
            vals: &[],
700
            cases: &[],
701
            borrows: &[],
702
        },
703
        a
704
    );
705
    let mut out = sexpr::Buffer { buf: &mut TABLES[..], pos: 0 };
706
    super::printFn(&mut out, &mut funcs[0]);
707
    try expectSegments(
708
        &TABLES[.. out.pos],
709
        &[
710
            "fn void $f() {\n  @entry0\n",
711
            "    ecall %1 44 %0 0 0 0 buf %2 16;\n    ecall %3 1 0 0 0 0;\n",
712
            "    page.slice %4 %5 %0 write $L2;\n    page.slice %6 %5 0 read $L2;\n",
713
            "    page.heap %7 %8 %0;\n    union.get %9 %0 3 8 $L2;\n",
714
            "    union.set %0 3 8 %9 $L2;\n    union.set %0 4 8 undefined;\n",
715
            "    heap.reset %0;\n    ret;\n}\n",
716
        ]
717
    );
718
}