test/sync/build.rad 2.3 KiB raw
1
//! Build a native image from the synchronization machine fixture.
2
3
use std::sys;
4
use std::io;
5
use std::fs;
6
use std::mem::heap;
7
use std::lang::strings;
8
use std::arch::rv64::asm;
9
use std::arch::rv64::image;
10
use std::arch::rv64;
11
12
/// Synchronization fixture code base and entry address.
13
constant CODE_BASE: u64 = 0x80010000;
14
/// Synchronization fixture writable data base.
15
constant RW_BASE: u64 = 0x80020000;
16
/// Bytes reserved for synchronization fixture data.
17
constant RW_SIZE: u32 = 4096;
18
19
/// Number of elements reserved for source storage.
20
constant SOURCE_CAPACITY: u32 = 65536;
21
/// Number of elements reserved for text storage.
22
constant TEXT_CAPACITY: u32 = 4096;
23
/// Number of elements reserved for memory storage.
24
constant MEMORY_CAPACITY: u32 = 16777216;
25
26
/// Assembly input storage.
27
static SOURCE: [u8; SOURCE_CAPACITY] = [0; SOURCE_CAPACITY];
28
/// Encoded text words.
29
static TEXT: [u32; TEXT_CAPACITY] = [0; TEXT_CAPACITY];
30
/// Assembly name and fixup storage.
31
static MEMORY: [u8; MEMORY_CAPACITY] = [0; MEMORY_CAPACITY];
32
/// Slots of the interned assembly identifiers.
33
unsafe static TABLE: [*[u8]; strings::TABLE_SIZE] = undefined;
34
35
/// Assemble startup and synchronization routines into one native code segment.
36
@default unsafe fn main(env: *sys::Env) -> i32 {
37
    assert env.args.len == 2;
38
    let length = try fs::readFile(env.args[0], &mut SOURCE[..]) catch {
39
        panic "main: missing assembly";
40
    };
41
    let arena = heap::over(&mut MEMORY[..]);
42
    let mut pool = strings::pool(&mut TABLE[..]);
43
    let data: *mut [u8] = &mut [];
44
    let program = try asm::assemble(
45
        asm::scanner::SourceKind::File { path: env.args[0] },
46
        &SOURCE[.. length],
47
        &mut TEXT[..],
48
        data,
49
        arena,
50
        &mut pool,
51
        0
52
    ) catch err {
53
        asm::printError(err, &env.args[0][..], &SOURCE[.. length]);
54
        return 1;
55
    };
56
    assert program.externalFixups.len == 0;
57
    let size = program.text.len * rv64::INSTR_SIZE as u32;
58
    let header = try! image::header(image::Layout {
59
        code: image::Segment { address: CODE_BASE, size },
60
        roData: image::Segment { address: 0, size: 0 },
61
        rwData: image::Segment { address: RW_BASE, size: 0 },
62
        rwTail: RW_SIZE,
63
    });
64
    let mut out = try! fs::create(env.args[1]);
65
    out.write(&header[..]);
66
    out.write(@sliceOf(program.text.ptr as *u8, size));
67
    try! out.close();
68
    return 0;
69
}