.githooks/
compiler/
kernel/
lib/
scripts/
seed/
sublime/
test/
boot/
bootstrap/
compiler/
cycles/
dispatch/
format/
host/
integration/
kernel/
loader/
manifests/
mmio/
native/
build.rad
11.4 KiB
packages/
runtime/
scheduling/
shared/
slots/
smp/
std/
sync/
termination/
tests/
trap/
verify/
Makefile
9.2 KiB
kernel.rad
776 B
runner.rad
11.6 KiB
vim/
.gitignore
333 B
.gitsigners
224 B
AGENTS.md
8.3 KiB
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
2.5 KiB
README
3.4 KiB
STYLE
3.2 KiB
std.lib
2.1 KiB
std.lib.test
1.1 KiB
test/native/build.rad
raw
| 1 | //! Build a native machine fixture with the reusable RV64 backend. |
| 2 | |
| 3 | use std::arch::rv64; |
| 4 | use std::arch::rv64::image; |
| 5 | use std::collections::dict; |
| 6 | use std::fs; |
| 7 | use std::lang::gen::data; |
| 8 | use std::lang::gen::labels; |
| 9 | use std::lang::il; |
| 10 | use std::mem::heap; |
| 11 | use std::sys; |
| 12 | |
| 13 | /// Native fixture code base and entry address. |
| 14 | constant CODE_BASE: u64 = 0x80010000; |
| 15 | /// Native fixture read-only data base. |
| 16 | constant RO_BASE: u64 = 0x80018000; |
| 17 | /// Native fixture writable data base. |
| 18 | constant RW_BASE: u64 = 0x80020000; |
| 19 | /// Machine completion device address. |
| 20 | constant FINISH_ADDRESS: i64 = 0x10001000; |
| 21 | /// Completion value for a successful machine test. |
| 22 | constant FINISH_PASS: i64 = 0x5555; |
| 23 | /// Completion value for a failed machine test. |
| 24 | constant FINISH_FAIL: i64 = 0x13333; |
| 25 | |
| 26 | /// Number of elements reserved for memory storage. |
| 27 | constant MEMORY_CAPACITY: u32 = 16777216; |
| 28 | /// Number of elements reserved for scratch storage. |
| 29 | constant SCRATCH_CAPACITY: u32 = 16777216; |
| 30 | |
| 31 | /// Code generation arena. It holds the tables and the emitted code. |
| 32 | static MEMORY: [u8; MEMORY_CAPACITY] = [0; MEMORY_CAPACITY]; |
| 33 | /// Function lowering and allocation workspace. |
| 34 | static SCRATCH: [u8; SCRATCH_CAPACITY] = [0; SCRATCH_CAPACITY]; |
| 35 | |
| 36 | /// Argument range of a branch that passes no block arguments. |
| 37 | constant NO_ARGS: il::ArgRange = il::ArgRange { start: 0, len: 0 }; |
| 38 | |
| 39 | /// Compile data accesses and conditional finish writes into a native image. |
| 40 | @default unsafe fn main(env: *sys::Env) -> i32 { |
| 41 | assert env.args.len == 1; |
| 42 | let arena = heap::over(&mut MEMORY[..]); |
| 43 | let scratch = heap::over(&mut SCRATCH[..]); |
| 44 | let mut blockOffsets: *mut [i32] = &mut []; |
| 45 | try! blockOffsets.resize(labels::MAX_BLOCKS_PER_FN, 0, arena); |
| 46 | let mut funcEntries: *mut [dict::Entry] = &mut []; |
| 47 | try! funcEntries.resize( |
| 48 | labels::FUNC_TABLE_SIZE, dict::Entry { key: &[], value: 0 }, arena |
| 49 | ); |
| 50 | let mut dataSyms: *mut [data::DataSym] = &mut []; |
| 51 | try! dataSyms.resize(2, data::DataSym { name: &[], addr: 0 }, arena); |
| 52 | let mut symEntries: *mut [dict::Entry] = &mut []; |
| 53 | try! symEntries.resize( |
| 54 | data::DATA_SYM_TABLE_SIZE, dict::Entry { key: &[], value: 0 }, arena |
| 55 | ); |
| 56 | let mut generator = try! rv64::beginProgram(rv64::ProgramOptions { |
| 57 | stack: rv64::StackCheck::Off, |
| 58 | entryPatch: rv64::EntryPatch::None, |
| 59 | debug: false, |
| 60 | placement: image::Placement::Physical { |
| 61 | code: CODE_BASE, roData: RO_BASE, rwData: RW_BASE |
| 62 | }, |
| 63 | }, arena, labels::init( |
| 64 | blockOffsets, funcEntries |
| 65 | )); |
| 66 | let blocks: *[il::Block] = &[ |
| 67 | il::Block { |
| 68 | label: il::BlockLabel { base: "entry", suffix: nil }, |
| 69 | params: &[], |
| 70 | instrs: &[ |
| 71 | il::Instr::Copy { dst: il::Reg { n: 0 }, val: il::Val::DataSym("fixture::value") }, |
| 72 | il::Instr::Load { |
| 73 | typ: il::Type::W64, dst: il::Reg { n: 1 }, src: il::Reg { n: 0 }, offset: 0 |
| 74 | }, |
| 75 | il::Instr::Br { |
| 76 | op: il::CmpOp::Eq, |
| 77 | typ: il::Type::W64, |
| 78 | a: il::Val::Reg(il::Reg { n: 1 }), |
| 79 | b: il::Val::Imm(42), |
| 80 | thenTarget: 1, |
| 81 | thenArgs: NO_ARGS, |
| 82 | elseTarget: 3, |
| 83 | elseArgs: NO_ARGS, |
| 84 | }, |
| 85 | ], |
| 86 | locs: &[], |
| 87 | preds: &[], |
| 88 | loopDepth: 0, |
| 89 | }, |
| 90 | il::Block { |
| 91 | label: il::BlockLabel { base: "zero", suffix: nil }, |
| 92 | params: &[], |
| 93 | instrs: &[ |
| 94 | il::Instr::Copy { dst: il::Reg { n: 2 }, val: il::Val::DataSym("fixture::zero") }, |
| 95 | il::Instr::Load { |
| 96 | typ: il::Type::W64, dst: il::Reg { n: 3 }, src: il::Reg { n: 2 }, offset: 0 |
| 97 | }, |
| 98 | il::Instr::Br { |
| 99 | op: il::CmpOp::Eq, |
| 100 | typ: il::Type::W64, |
| 101 | a: il::Val::Reg(il::Reg { n: 3 }), |
| 102 | b: il::Val::Imm(0), |
| 103 | thenTarget: 5, |
| 104 | thenArgs: NO_ARGS, |
| 105 | elseTarget: 3, |
| 106 | elseArgs: NO_ARGS, |
| 107 | }, |
| 108 | ], |
| 109 | locs: &[], |
| 110 | preds: &[0], |
| 111 | loopDepth: 0, |
| 112 | }, |
| 113 | il::Block { |
| 114 | label: il::BlockLabel { base: "success", suffix: nil }, |
| 115 | params: &[], |
| 116 | instrs: &[ |
| 117 | il::Instr::Copy { |
| 118 | dst: il::Reg { n: 4 }, val: il::Val::Imm(FINISH_ADDRESS) |
| 119 | }, |
| 120 | il::Instr::Copy { |
| 121 | dst: il::Reg { n: 9 }, val: il::Val::Imm(FINISH_PASS - 42) |
| 122 | }, |
| 123 | il::Instr::Load { |
| 124 | typ: il::Type::W64, dst: il::Reg { n: 10 }, src: il::Reg { n: 6 }, offset: 0 |
| 125 | }, |
| 126 | il::Instr::BinOp { |
| 127 | op: il::BinOp::Add, |
| 128 | typ: il::Type::W64, |
| 129 | dst: il::Reg { n: 11 }, |
| 130 | a: il::Val::Reg(il::Reg { n: 10 }), |
| 131 | b: il::Val::Reg(il::Reg { n: 9 }), |
| 132 | }, |
| 133 | il::Instr::Store { |
| 134 | typ: il::Type::W32, |
| 135 | src: il::Val::Reg(il::Reg { n: 11 }), |
| 136 | dst: il::Reg { n: 4 }, |
| 137 | offset: 0, |
| 138 | }, |
| 139 | il::Instr::Unreachable, |
| 140 | ], |
| 141 | locs: &[], |
| 142 | preds: &[4], |
| 143 | loopDepth: 0, |
| 144 | }, |
| 145 | il::Block { |
| 146 | label: il::BlockLabel { base: "failure", suffix: nil }, |
| 147 | params: &[], |
| 148 | instrs: &[ |
| 149 | il::Instr::Copy { |
| 150 | dst: il::Reg { n: 5 }, val: il::Val::Imm(FINISH_ADDRESS) |
| 151 | }, |
| 152 | il::Instr::Store { |
| 153 | typ: il::Type::W32, |
| 154 | src: il::Val::Imm(FINISH_FAIL), |
| 155 | dst: il::Reg { n: 5 }, |
| 156 | offset: 0, |
| 157 | }, |
| 158 | il::Instr::Unreachable, |
| 159 | ], |
| 160 | locs: &[], |
| 161 | preds: &[0, 1], |
| 162 | loopDepth: 0, |
| 163 | }, |
| 164 | // Dominates block 2, which comes first in the layout. Register 6 holds a pointer |
| 165 | // that block 2 uses beside the data of register 9, and the two must not share a |
| 166 | // register. |
| 167 | il::Block { |
| 168 | label: il::BlockLabel { base: "pointer", suffix: nil }, |
| 169 | params: &[], |
| 170 | instrs: &[ |
| 171 | il::Instr::Copy { dst: il::Reg { n: 7 }, val: il::Val::Imm(5) }, |
| 172 | il::Instr::Copy { dst: il::Reg { n: 6 }, val: il::Val::DataSym("fixture::value") }, |
| 173 | il::Instr::BinOp { |
| 174 | op: il::BinOp::Add, |
| 175 | typ: il::Type::W64, |
| 176 | dst: il::Reg { n: 8 }, |
| 177 | a: il::Val::Reg(il::Reg { n: 7 }), |
| 178 | b: il::Val::Reg(il::Reg { n: 7 }), |
| 179 | }, |
| 180 | il::Instr::Jmp { target: 2, args: NO_ARGS }, |
| 181 | ], |
| 182 | locs: &[], |
| 183 | preds: &[1], |
| 184 | loopDepth: 0, |
| 185 | }, |
| 186 | ]; |
| 187 | let mut allBlocks: *mut [il::Block] = &mut []; |
| 188 | try! allBlocks.extend(blocks, scratch); |
| 189 | let setup: *[il::Instr] = &[ |
| 190 | il::Instr::Copy { |
| 191 | dst: il::Reg { n: 12 }, val: il::Val::DataSym("fixture::zero") |
| 192 | }, |
| 193 | il::Instr::Field { |
| 194 | dst: il::Reg { n: 13 }, src: il::Reg { n: 12 }, offset: 16 |
| 195 | }, |
| 196 | il::Instr::Store { |
| 197 | typ: il::Type::W64, |
| 198 | src: il::Val::Reg(il::Reg { n: 13 }), |
| 199 | dst: il::Reg { n: 12 }, |
| 200 | offset: 0, |
| 201 | }, |
| 202 | il::Instr::Store { |
| 203 | typ: il::Type::W32, src: il::Val::Imm(16), dst: il::Reg { n: 12 }, offset: 8 |
| 204 | }, |
| 205 | il::Instr::Jmp { target: 6, args: NO_ARGS }, |
| 206 | ]; |
| 207 | try! allBlocks.append( |
| 208 | il::Block { |
| 209 | label: il::BlockLabel { base: "byte.setup", suffix: nil }, |
| 210 | params: &[], |
| 211 | instrs: setup, |
| 212 | locs: &[], |
| 213 | preds: &[1], |
| 214 | loopDepth: 0, |
| 215 | }, |
| 216 | scratch |
| 217 | ); |
| 218 | for typ, width in &[il::Type::W16, il::Type::W32, il::Type::W64] { |
| 219 | let value: i64 = |
| 220 | 0x80ff if width == 0 else 0x89abcdef if width == 1 else 0x8877665544332211 as i64; |
| 221 | for offset in 0 .. 8 { |
| 222 | let block = 6 + width * 8 + offset; |
| 223 | let dst = il::Reg { n: 14 + width * 8 + offset }; |
| 224 | let mut instrs: *mut [il::Instr] = &mut []; |
| 225 | try! instrs.extend( |
| 226 | &[ |
| 227 | il::Instr::BytesStore { |
| 228 | typ, |
| 229 | slice: il::Reg { n: 12 }, |
| 230 | offset: il::Val::Imm(offset as i64), |
| 231 | value: il::Val::Imm(value), |
| 232 | }, |
| 233 | il::Instr::BytesLoad { |
| 234 | typ, dst, slice: il::Reg { n: 12 }, offset: il::Val::Imm(offset as i64) |
| 235 | }, |
| 236 | il::Instr::Br { |
| 237 | op: il::CmpOp::Eq, |
| 238 | typ: il::Type::W64, |
| 239 | a: il::Val::Reg(dst), |
| 240 | b: il::Val::Imm(value), |
| 241 | thenTarget: 4 if block == 29 else block + 1, |
| 242 | thenArgs: NO_ARGS, |
| 243 | elseTarget: 3, |
| 244 | elseArgs: NO_ARGS, |
| 245 | }, |
| 246 | ], |
| 247 | scratch |
| 248 | ); |
| 249 | let mut preds: *mut [u32] = &mut []; |
| 250 | try! preds.append(block - 1, scratch); |
| 251 | try! allBlocks.append( |
| 252 | il::Block { |
| 253 | label: il::BlockLabel { base: "byte.access", suffix: block }, |
| 254 | params: &[], |
| 255 | instrs, |
| 256 | locs: &[], |
| 257 | preds, |
| 258 | loopDepth: 0, |
| 259 | }, |
| 260 | scratch |
| 261 | ); |
| 262 | } |
| 263 | } |
| 264 | let mut funcs: *mut [il::Fn] = &mut []; |
| 265 | try! funcs.append( |
| 266 | il::Fn { |
| 267 | name: "fixture::entry", |
| 268 | params: &[], |
| 269 | returnType: il::ValType::Data(il::Type::W64), |
| 270 | isExtern: false, |
| 271 | isLeaf: true, |
| 272 | blocks: allBlocks, |
| 273 | vals: &[], |
| 274 | cases: &[], |
| 275 | borrows: &[], |
| 276 | }, |
| 277 | scratch |
| 278 | ); |
| 279 | try! rv64::generateFunction(&mut generator, &funcs[0], &[], scratch); |
| 280 | let globals = &[ |
| 281 | il::Data { |
| 282 | name: "fixture::value", |
| 283 | size: 8, |
| 284 | alignment: 8, |
| 285 | layout: 0, |
| 286 | readOnly: false, |
| 287 | heap: nil, |
| 288 | isZeroInit: false, |
| 289 | values: &[ |
| 290 | il::DataValue { |
| 291 | item: il::DataItem::Val { typ: il::Type::W64, val: 42 }, count: 1 |
| 292 | }, |
| 293 | ], |
| 294 | }, |
| 295 | il::Data { |
| 296 | name: "fixture::zero", |
| 297 | size: 4096, |
| 298 | alignment: 8, |
| 299 | layout: 0, |
| 300 | readOnly: false, |
| 301 | heap: nil, |
| 302 | isZeroInit: true, |
| 303 | values: &[], |
| 304 | }, |
| 305 | ]; |
| 306 | let mut ro: [u8; 8] = [0; 8]; |
| 307 | let mut rw: [u8; 8] = [0; 8]; |
| 308 | let result = try! rv64::finishProgram( |
| 309 | &mut generator, |
| 310 | globals, |
| 311 | rv64::Storage { dataSyms, dataSymTable: dict::init(symEntries) }, |
| 312 | &[], |
| 313 | &mut ro[..], |
| 314 | &mut rw[..] |
| 315 | ); |
| 316 | let header = try! image::header(result.layout); |
| 317 | let code = @sliceOf(result.code.ptr as *u8, result.code.len * 4); |
| 318 | let mut out = try! fs::create(env.args[0]); |
| 319 | out.write(&header[..]); |
| 320 | out.write(code); |
| 321 | out.write(&ro[.. result.roDataSize]); |
| 322 | out.write(&rw[.. result.rwDataSize]); |
| 323 | try! out.close(); |
| 324 | return 0; |
| 325 | } |