.githooks/
compiler/
kernel/
lib/
examples/
std/
arch/
bits/
char/
collections/
fs/
hash/
lang/
alloc/
ast/
gen/
il/
binary/
tests/
cfg.rad
8.2 KiB
collect.rad
4.3 KiB
linkage.rad
4.8 KiB
program.rad
30.5 KiB
reader.rad
28.5 KiB
tests.rad
30.4 KiB
writer.rad
28.4 KiB
link/
printer/
verify/
binary.rad
14.7 KiB
link.rad
11.8 KiB
printer.rad
31.7 KiB
verify.rad
99.5 KiB
layout/
lower/
manifest/
module/
parser/
resolver/
scanner/
sexpr/
alloc.rad
1.2 KiB
ast.rad
36.0 KiB
gen.rad
513 B
il.rad
48.5 KiB
layout.rad
6.9 KiB
lower.rad
487.6 KiB
manifest.rad
22.5 KiB
module.rad
17.8 KiB
package.rad
1.5 KiB
parser.rad
114.1 KiB
pretty.rad
103.6 KiB
resolver.rad
508.1 KiB
scanner.rad
19.3 KiB
sexpr.rad
7.1 KiB
strings.rad
3.3 KiB
types.rad
620 B
mem/
sys/
arch.rad
68 B
bits.rad
1.6 KiB
char.rad
1.5 KiB
collections.rad
39 B
fmt.rad
11.6 KiB
fs.rad
11.0 KiB
hash.rad
41 B
intrinsics.rad
467 B
io.rad
1.7 KiB
lang.rad
335 B
mem.rad
12.0 KiB
mem.ras
919 B
process.rad
6.2 KiB
std.manifest
4.9 KiB
std.rad
349 B
sys.rad
1.0 KiB
testing.rad
2.4 KiB
tests.rad
26.6 KiB
vec.rad
3.2 KiB
scripts/
seed/
sublime/
test/
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
lib/std/lang/il/binary/reader.rad
raw
| 1 | //! Checked binary RIL decoding into caller-owned allocator storage. |
| 2 | //! Callers discard partial results and restore the allocator after a failed decode. |
| 3 | //! A cursor borrows one package input and keeps its offsets and decoding pools. |
| 4 | |
| 5 | use std::lang::il; |
| 6 | use std::lang::il::binary; |
| 7 | use std::mem; |
| 8 | use std::mem::heap; |
| 9 | |
| 10 | /// Input cursor and the argument and case pools of the function being read. |
| 11 | export record Reader['wire] { |
| 12 | /// Encoded package bytes. |
| 13 | wire: &'wire [u8], |
| 14 | /// Number of bytes consumed. |
| 15 | offset: u32, |
| 16 | /// Symbol names in wire-index order. |
| 17 | symbols: *[*[u8]], |
| 18 | /// Number of entries in the layout table of the package. |
| 19 | layouts: u32, |
| 20 | /// Number of entries in the signature table of the package. |
| 21 | signatures: u32, |
| 22 | /// Number of entries in the trait table of the package. |
| 23 | traits: u32, |
| 24 | /// Exclusive bound for SSA register numbers. |
| 25 | registers: u32, |
| 26 | /// Number of blocks in the current function. |
| 27 | blocks: u32, |
| 28 | /// Argument pool of the current function. |
| 29 | vals: *mut [il::Val], |
| 30 | /// Case pool of the current function. |
| 31 | cases: *mut [il::SwitchCase], |
| 32 | } |
| 33 | |
| 34 | /// Create a cursor at the start of the input. Set function bounds before reading instructions. |
| 35 | export fn new(wire: &'wire [u8]) -> Reader['wire] { |
| 36 | return Reader { |
| 37 | wire, |
| 38 | offset: 0, |
| 39 | symbols: &[], |
| 40 | layouts: 0, |
| 41 | signatures: 0, |
| 42 | traits: 0, |
| 43 | registers: 0, |
| 44 | blocks: 0, |
| 45 | vals: &mut [], |
| 46 | cases: &mut [], |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | /// Whether the input is fully consumed. |
| 51 | export fn (input: &Reader['wire]) atEnd() -> bool { |
| 52 | return input.offset == input.wire.len; |
| 53 | } |
| 54 | |
| 55 | /// Read one unsigned byte. |
| 56 | export fn (input: &mut Reader['wire]) byte() -> u64 throws (binary::Error) { |
| 57 | if input.offset >= input.wire.len { |
| 58 | throw binary::Error::Truncated; |
| 59 | } |
| 60 | let value = input.wire[input.offset] as u64; |
| 61 | set input.offset += 1; |
| 62 | return value; |
| 63 | } |
| 64 | |
| 65 | /// Read a little-endian unsigned 32-bit integer. |
| 66 | export fn (input: &mut Reader['wire]) word() -> u64 throws (binary::Error) { |
| 67 | if input.wire.len - input.offset < 4 { |
| 68 | throw binary::Error::Truncated; |
| 69 | } |
| 70 | let value = mem::load32Le(input.wire, input.offset) as u64; |
| 71 | set input.offset += 4; |
| 72 | return value; |
| 73 | } |
| 74 | |
| 75 | /// Read a little-endian unsigned 64-bit integer. |
| 76 | fn (input: &mut Reader['wire]) doubleWord() -> u64 throws (binary::Error) { |
| 77 | if input.wire.len - input.offset < 8 { |
| 78 | throw binary::Error::Truncated; |
| 79 | } |
| 80 | let value = mem::load64Le(input.wire, input.offset); |
| 81 | set input.offset += 8; |
| 82 | return value; |
| 83 | } |
| 84 | |
| 85 | /// Read an unsigned integer with width 1, 2, 4, or 8. |
| 86 | export fn (input: &mut Reader['wire]) integer(width: u32) -> u64 throws (binary::Error) { |
| 87 | if width <> 1 and width <> 2 and width <> 4 and width <> 8 { |
| 88 | throw binary::Error::Invalid; |
| 89 | } |
| 90 | if width > input.wire.len - input.offset { |
| 91 | throw binary::Error::Truncated; |
| 92 | } |
| 93 | let offset = input.offset; |
| 94 | let mut result: u64 = 0; |
| 95 | match width { |
| 96 | case 1 => set result = input.wire[offset] as u64, |
| 97 | case 2 => set result = mem::load16Le(input.wire, offset) as u64, |
| 98 | case 4 => set result = mem::load32Le(input.wire, offset) as u64, |
| 99 | case 8 => set result = mem::load64Le(input.wire, offset), |
| 100 | else => throw binary::Error::Invalid, |
| 101 | } |
| 102 | set input.offset += width; |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | /// Read a zero-or-one optional-field or boolean marker. |
| 107 | export fn (input: &mut Reader['wire]) flag() -> bool throws (binary::Error) { |
| 108 | let n = try input.byte(); |
| 109 | if n > 1 { |
| 110 | throw binary::Error::Invalid; |
| 111 | } |
| 112 | return n == 1; |
| 113 | } |
| 114 | |
| 115 | /// Read a count and check the minimum required input bytes before allocation. |
| 116 | export fn (input: &mut Reader['wire]) count(minimum: u32) -> u32 throws (binary::Error) { |
| 117 | assert minimum > 0; |
| 118 | let n = try input.word() as u32; |
| 119 | if n > (input.wire.len - input.offset) / minimum { |
| 120 | throw binary::Error::Truncated; |
| 121 | } |
| 122 | return n; |
| 123 | } |
| 124 | |
| 125 | /// Read a list of region indices. The verifier checks them against the layouts. |
| 126 | export fn (input: &mut Reader['wire]) regions(a: &heap::Heap) -> *[u32] |
| 127 | throws (binary::Error) |
| 128 | { |
| 129 | let length = try input.count(binary::REGION_BYTES); |
| 130 | if length == 0 { |
| 131 | return &[]; |
| 132 | } |
| 133 | let mut items: *mut [u32] = &mut []; |
| 134 | try items.reserve(length, a) catch { |
| 135 | throw binary::Error::Storage; |
| 136 | }; |
| 137 | for _ in 0 .. length { |
| 138 | try items.append(try input.word() as u32, a) catch { |
| 139 | throw binary::Error::Storage; |
| 140 | }; |
| 141 | } |
| 142 | return &items[..]; |
| 143 | } |
| 144 | |
| 145 | /// Read a length-prefixed byte string and copy it into storage from `a`. |
| 146 | export fn (input: &mut Reader['wire]) bytes(a: &heap::Heap) -> *[u8] |
| 147 | throws (binary::Error) |
| 148 | { |
| 149 | let n = try input.count(1); |
| 150 | let mut result: *mut [u8] = &mut []; |
| 151 | try result.extend(&input.wire[input.offset .. input.offset + n], a) catch { |
| 152 | throw binary::Error::Storage; |
| 153 | }; |
| 154 | set input.offset += n; |
| 155 | return &result[..]; |
| 156 | } |
| 157 | |
| 158 | /// Resolve a checked symbol-table index. |
| 159 | export fn (input: &mut Reader['wire]) symbol() -> *[u8] throws (binary::Error) { |
| 160 | let index = try input.word() as u32; |
| 161 | if index >= input.symbols.len { |
| 162 | throw binary::Error::Symbol; |
| 163 | } |
| 164 | return input.symbols[index]; |
| 165 | } |
| 166 | |
| 167 | /// Read an IL type from its byte width. |
| 168 | export fn (input: &mut Reader['wire]) typ() -> il::Type throws (binary::Error) { |
| 169 | let width = try input.byte(); |
| 170 | match width { |
| 171 | case 1 => return il::Type::W8, |
| 172 | case 2 => return il::Type::W16, |
| 173 | case 4 => return il::Type::W32, |
| 174 | case 8 => return il::Type::W64, |
| 175 | else => throw binary::Error::Invalid, |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /// Read a layout table index within the package. |
| 180 | export fn (input: &mut Reader['wire]) layoutIndex() -> u32 throws (binary::Error) { |
| 181 | let n = try input.word() as u32; |
| 182 | if n >= input.layouts { |
| 183 | throw binary::Error::Invalid; |
| 184 | } |
| 185 | return n; |
| 186 | } |
| 187 | |
| 188 | /// Read an optional layout table index within the package. |
| 189 | export fn (input: &mut Reader['wire]) optionalLayoutIndex() -> ?u32 throws (binary::Error) { |
| 190 | if try input.flag() { |
| 191 | return try input.layoutIndex(); |
| 192 | } |
| 193 | return nil; |
| 194 | } |
| 195 | |
| 196 | /// Read an optional index: a presence byte, then the index when present. |
| 197 | /// The index is not checked against a table. |
| 198 | export fn (input: &mut Reader['wire]) optionalIndex() -> ?u32 throws (binary::Error) { |
| 199 | if try input.flag() { |
| 200 | return try input.word() as u32; |
| 201 | } |
| 202 | return nil; |
| 203 | } |
| 204 | |
| 205 | /// Read the bounded list of borrowed result sources. |
| 206 | export fn (input: &mut Reader['wire]) borrows(a: &heap::Heap) -> *[il::Borrow] |
| 207 | throws (binary::Error) |
| 208 | { |
| 209 | let length = try input.count(binary::MIN_BORROW_BYTES); |
| 210 | let mut items: *mut [il::Borrow] = &mut []; |
| 211 | try items.reserve(length, a) catch { |
| 212 | throw binary::Error::Storage; |
| 213 | }; |
| 214 | for _ in 0 .. length { |
| 215 | let target = try input.optionalIndex(); |
| 216 | let result = try input.word() as u32; |
| 217 | let param = try input.word() as u32; |
| 218 | let source = try input.optionalIndex(); |
| 219 | try items.append(il::Borrow { target, result, param, source }, a) catch { |
| 220 | throw binary::Error::Storage; |
| 221 | }; |
| 222 | } |
| 223 | return &items[..]; |
| 224 | } |
| 225 | |
| 226 | /// Read a signature table index within the package. |
| 227 | export fn (input: &mut Reader['wire]) signatureIndex() -> u32 throws (binary::Error) { |
| 228 | let n = try input.word() as u32; |
| 229 | if n >= input.signatures { |
| 230 | throw binary::Error::Invalid; |
| 231 | } |
| 232 | return n; |
| 233 | } |
| 234 | |
| 235 | /// Read a trait table index within the package. |
| 236 | export fn (input: &mut Reader['wire]) traitIndex() -> u32 throws (binary::Error) { |
| 237 | let n = try input.word() as u32; |
| 238 | if n >= input.traits { |
| 239 | throw binary::Error::Invalid; |
| 240 | } |
| 241 | return n; |
| 242 | } |
| 243 | |
| 244 | /// Rights of a receiver. |
| 245 | export record Rights: Copy { |
| 246 | /// Whether the receiver owns its object. |
| 247 | own: bool, |
| 248 | /// Whether the receiver can be written. |
| 249 | mutable: bool, |
| 250 | } |
| 251 | |
| 252 | /// Read the rights of a receiver, encoded as pointer type flags without nullability. |
| 253 | export fn (input: &mut Reader['wire]) rights() -> Rights throws (binary::Error) { |
| 254 | let flags = try input.byte() as u8; |
| 255 | if flags > (super::PTR_OWN | super::PTR_MUTABLE) { |
| 256 | throw binary::Error::Invalid; |
| 257 | } |
| 258 | return Rights { |
| 259 | own: (flags & super::PTR_OWN) <> 0, mutable: (flags & super::PTR_MUTABLE) <> 0 |
| 260 | }; |
| 261 | } |
| 262 | |
| 263 | /// Get the rights of a heap from their flags. A heap never has both flags. |
| 264 | export fn heapRights(flags: u8) -> il::HeapType throws (binary::Error) { |
| 265 | match flags { |
| 266 | case 0 => return il::HeapType::Plain, |
| 267 | case super::HEAP_TYPED => return il::HeapType::Typed, |
| 268 | case super::HEAP_RESET => return il::HeapType::Reset, |
| 269 | else => throw binary::Error::Invalid, |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /// Read the rights of a heap handle. |
| 274 | export fn (input: &mut Reader['wire]) heapType() -> il::HeapType throws (binary::Error) { |
| 275 | return try heapRights(try input.byte() as u8); |
| 276 | } |
| 277 | |
| 278 | /// Read a pointer type with checked layout index and flags. |
| 279 | export fn (input: &mut Reader['wire]) ptrType() -> il::PtrType throws (binary::Error) { |
| 280 | let layout = try input.layoutIndex(); |
| 281 | let flags = try input.byte() as u8; |
| 282 | if flags > (super::PTR_OWN | super::PTR_MUTABLE | super::PTR_NULLABLE) { |
| 283 | throw binary::Error::Invalid; |
| 284 | } |
| 285 | return il::PtrType { |
| 286 | layout, |
| 287 | own: (flags & super::PTR_OWN) <> 0, |
| 288 | mutable: (flags & super::PTR_MUTABLE) <> 0, |
| 289 | nullable: (flags & super::PTR_NULLABLE) <> 0, |
| 290 | }; |
| 291 | } |
| 292 | |
| 293 | /// Read a value type with checked layout and signature indices. |
| 294 | export fn (input: &mut Reader['wire]) valType() -> il::ValType throws (binary::Error) { |
| 295 | let tag = try input.byte() as u8; |
| 296 | match tag { |
| 297 | case super::VALTYPE_VOID => return il::ValType::Void, |
| 298 | case super::VALTYPE_DATA => return il::ValType::Data(try input.typ()), |
| 299 | case super::VALTYPE_PTR => return il::ValType::Ptr(try input.ptrType()), |
| 300 | case super::VALTYPE_FN => return il::ValType::Fn(try input.signatureIndex()), |
| 301 | case super::VALTYPE_HEAP => return il::ValType::Heap(try input.heapType()), |
| 302 | else => throw binary::Error::Invalid, |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /// Read a register index within the current function's bound. |
| 307 | export fn (input: &mut Reader['wire]) reg() -> il::Reg throws (binary::Error) { |
| 308 | let n = try input.word() as u32; |
| 309 | if n >= input.registers { |
| 310 | throw binary::Error::Invalid; |
| 311 | } |
| 312 | return il::Reg { n }; |
| 313 | } |
| 314 | |
| 315 | /// Read a target block index within the current function. |
| 316 | export fn (input: &mut Reader['wire]) target() -> u32 throws (binary::Error) { |
| 317 | let n = try input.word() as u32; |
| 318 | if n >= input.blocks { |
| 319 | throw binary::Error::Invalid; |
| 320 | } |
| 321 | return n; |
| 322 | } |
| 323 | |
| 324 | /// Read a tagged value with checked register and symbol indices. |
| 325 | export fn (input: &mut Reader['wire]) val() -> il::Val throws (binary::Error) { |
| 326 | let tag = try input.byte() as u8; |
| 327 | match tag { |
| 328 | case super::VALUE_REG => return il::Val::Reg(try input.reg()), |
| 329 | case super::VALUE_IMM => return il::Val::Imm(try input.doubleWord() as i64), |
| 330 | case super::VALUE_DATASYM => return il::Val::DataSym(try input.symbol()), |
| 331 | case super::VALUE_FNADDR => return il::Val::FnAddr(try input.symbol()), |
| 332 | case super::VALUE_UNDEF => return il::Val::Undef, |
| 333 | else => throw binary::Error::Invalid, |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /// Read a counted sequence of values into the argument pool of the current function. |
| 338 | export fn (input: &mut Reader['wire]) values(a: &heap::Heap) -> il::ArgRange |
| 339 | throws (binary::Error) |
| 340 | { |
| 341 | let n = try input.count(1); |
| 342 | let start = input.vals.len; |
| 343 | try input.vals.reserve(n, a) catch { |
| 344 | throw binary::Error::Storage; |
| 345 | }; |
| 346 | for _ in 0 .. n { |
| 347 | let item = try input.val(); |
| 348 | try input.vals.append(item, a) catch { |
| 349 | throw binary::Error::Storage; |
| 350 | }; |
| 351 | } |
| 352 | return il::ArgRange { start, len: n }; |
| 353 | } |
| 354 | |
| 355 | /// Read a checked bin operation tag. |
| 356 | fn (input: &mut Reader['wire]) binOp() -> il::BinOp throws (binary::Error) { |
| 357 | let tag = try input.byte() as u8; |
| 358 | match tag { |
| 359 | case super::BIN_ADD => return il::BinOp::Add, |
| 360 | case super::BIN_SUB => return il::BinOp::Sub, |
| 361 | case super::BIN_MUL => return il::BinOp::Mul, |
| 362 | case super::BIN_SDIV => return il::BinOp::Sdiv, |
| 363 | case super::BIN_UDIV => return il::BinOp::Udiv, |
| 364 | case super::BIN_SREM => return il::BinOp::Srem, |
| 365 | case super::BIN_UREM => return il::BinOp::Urem, |
| 366 | case super::BIN_EQ => return il::BinOp::Eq, |
| 367 | case super::BIN_NE => return il::BinOp::Ne, |
| 368 | case super::BIN_SLT => return il::BinOp::Slt, |
| 369 | case super::BIN_SGE => return il::BinOp::Sge, |
| 370 | case super::BIN_ULT => return il::BinOp::Ult, |
| 371 | case super::BIN_UGE => return il::BinOp::Uge, |
| 372 | case super::BIN_AND => return il::BinOp::And, |
| 373 | case super::BIN_OR => return il::BinOp::Or, |
| 374 | case super::BIN_XOR => return il::BinOp::Xor, |
| 375 | case super::BIN_SHL => return il::BinOp::Shl, |
| 376 | case super::BIN_SSHR => return il::BinOp::Sshr, |
| 377 | case super::BIN_USHR => return il::BinOp::Ushr, |
| 378 | case super::BIN_ROTR => return il::BinOp::RotR, |
| 379 | case super::BIN_ROTL => return il::BinOp::RotL, |
| 380 | else => throw binary::Error::Invalid, |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /// Read a checked un operation tag. |
| 385 | fn (input: &mut Reader['wire]) unOp() -> il::UnOp throws (binary::Error) { |
| 386 | let tag = try input.byte() as u8; |
| 387 | match tag { |
| 388 | case super::UN_NEG => return il::UnOp::Neg, |
| 389 | case super::UN_NOT => return il::UnOp::Not, |
| 390 | case super::UN_BYTE_SWAP => return il::UnOp::ByteSwap, |
| 391 | case super::UN_POP_COUNT => return il::UnOp::PopCount, |
| 392 | case super::UN_TRAILING_ZEROS => return il::UnOp::TrailingZeros, |
| 393 | case super::UN_LEADING_ZEROS => return il::UnOp::LeadingZeros, |
| 394 | else => throw binary::Error::Invalid, |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /// Read a checked cmp operation tag. |
| 399 | fn (input: &mut Reader['wire]) cmpOp() -> il::CmpOp throws (binary::Error) { |
| 400 | let tag = try input.byte() as u8; |
| 401 | match tag { |
| 402 | case super::CMP_EQ => return il::CmpOp::Eq, |
| 403 | case super::CMP_NE => return il::CmpOp::Ne, |
| 404 | case super::CMP_SLT => return il::CmpOp::Slt, |
| 405 | case super::CMP_ULT => return il::CmpOp::Ult, |
| 406 | else => throw binary::Error::Invalid, |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /// Read the operands of a slice instruction that changes the length or contents of a slice, |
| 411 | /// which has wire tag `tag`. |
| 412 | fn (input: &mut Reader['wire]) sliceInstr(tag: u8) -> il::Instr throws (binary::Error) { |
| 413 | let dst = try input.reg(); |
| 414 | if tag == super::INSTR_SLICE_LEN { |
| 415 | let layout = try input.layoutIndex(); |
| 416 | return il::Instr::SliceLen { dst, layout, length: try input.val() }; |
| 417 | } |
| 418 | let src = try input.reg(); |
| 419 | let layout = try input.layoutIndex(); |
| 420 | return il::Instr::SliceCopy { dst, src, layout }; |
| 421 | } |
| 422 | |
| 423 | /// Read the operands of `slice.grow`. |
| 424 | fn (input: &mut Reader['wire]) sliceGrow() -> il::Instr throws (binary::Error) { |
| 425 | let ok = try input.reg(); |
| 426 | let slice = try input.reg(); |
| 427 | let heap = try input.val(); |
| 428 | let layout = try input.layoutIndex(); |
| 429 | return il::Instr::SliceGrow { ok, slice, heap, layout, capacity: try input.val() }; |
| 430 | } |
| 431 | |
| 432 | /// Read one instruction and reconstruct its operand sequences. |
| 433 | export fn (input: &mut Reader['wire]) instr(a: &heap::Heap) -> il::Instr |
| 434 | throws (binary::Error) |
| 435 | { |
| 436 | let tag = try input.byte() as u8; |
| 437 | match tag { |
| 438 | case super::INSTR_RESERVE => { |
| 439 | let vdst = try input.reg(); |
| 440 | let vlayout = try input.layoutIndex(); |
| 441 | return il::Instr::Reserve { dst: vdst, layout: vlayout }; |
| 442 | } |
| 443 | case super::INSTR_LOAD, super::INSTR_SLOAD => { |
| 444 | let vtyp = try input.typ(); |
| 445 | let vdst = try input.reg(); |
| 446 | let vsrc = try input.reg(); |
| 447 | let voffset = try input.word() as i32; |
| 448 | if tag == super::INSTR_SLOAD { |
| 449 | return il::Instr::Sload { typ: vtyp, dst: vdst, src: vsrc, offset: voffset }; |
| 450 | } |
| 451 | return il::Instr::Load { typ: vtyp, dst: vdst, src: vsrc, offset: voffset }; |
| 452 | } |
| 453 | case super::INSTR_STORE => { |
| 454 | let vtyp = try input.typ(); |
| 455 | let vsrc = try input.val(); |
| 456 | let vdst = try input.reg(); |
| 457 | let voffset = try input.word() as i32; |
| 458 | return il::Instr::Store { typ: vtyp, src: vsrc, dst: vdst, offset: voffset }; |
| 459 | } |
| 460 | case super::INSTR_BLIT => { |
| 461 | let vdst = try input.reg(); |
| 462 | let vsrc = try input.reg(); |
| 463 | let vlayout = try input.layoutIndex(); |
| 464 | return il::Instr::Blit { dst: vdst, src: vsrc, layout: vlayout }; |
| 465 | } |
| 466 | case super::INSTR_COPY => { |
| 467 | let vdst = try input.reg(); |
| 468 | let vval = try input.val(); |
| 469 | return il::Instr::Copy { dst: vdst, val: vval }; |
| 470 | } |
| 471 | case super::INSTR_BINOP => { |
| 472 | let vop = try input.binOp(); |
| 473 | let vtyp = try input.typ(); |
| 474 | let vdst = try input.reg(); |
| 475 | let va = try input.val(); |
| 476 | let vb = try input.val(); |
| 477 | return il::Instr::BinOp { op: vop, typ: vtyp, dst: vdst, a: va, b: vb }; |
| 478 | } |
| 479 | case super::INSTR_UNOP => { |
| 480 | let vop = try input.unOp(); |
| 481 | let vtyp = try input.typ(); |
| 482 | let vdst = try input.reg(); |
| 483 | let va = try input.val(); |
| 484 | return il::Instr::UnOp { op: vop, typ: vtyp, dst: vdst, a: va }; |
| 485 | } |
| 486 | case super::INSTR_ZEXT, super::INSTR_SEXT => { |
| 487 | let vtyp = try input.typ(); |
| 488 | let vdst = try input.reg(); |
| 489 | let vval = try input.val(); |
| 490 | if tag == super::INSTR_SEXT { |
| 491 | return il::Instr::Sext { typ: vtyp, dst: vdst, val: vval }; |
| 492 | } |
| 493 | return il::Instr::Zext { typ: vtyp, dst: vdst, val: vval }; |
| 494 | } |
| 495 | case super::INSTR_CALL => { |
| 496 | let vretTy = try input.valType(); |
| 497 | let mut vdst: ?il::Reg = nil; |
| 498 | if try input.flag() { |
| 499 | set vdst = try input.reg(); |
| 500 | } |
| 501 | let vfunc = try input.val(); |
| 502 | let vargs = try input.values(a); |
| 503 | return il::Instr::Call { retTy: vretTy, dst: vdst, func: vfunc, args: vargs }; |
| 504 | } |
| 505 | case super::INSTR_RET => { |
| 506 | let mut vval: ?il::Val = nil; |
| 507 | if try input.flag() { |
| 508 | set vval = try input.val(); |
| 509 | } |
| 510 | return il::Instr::Ret { val: vval }; |
| 511 | } |
| 512 | case super::INSTR_JMP => { |
| 513 | let vtarget = try input.target(); |
| 514 | let vargs = try input.values(a); |
| 515 | return il::Instr::Jmp { target: vtarget, args: vargs }; |
| 516 | } |
| 517 | case super::INSTR_BR => { |
| 518 | let vop = try input.cmpOp(); |
| 519 | let vtyp = try input.typ(); |
| 520 | let va = try input.val(); |
| 521 | let vb = try input.val(); |
| 522 | let vthenTarget = try input.target(); |
| 523 | let vthenArgs = try input.values(a); |
| 524 | let velseTarget = try input.target(); |
| 525 | let velseArgs = try input.values(a); |
| 526 | return il::Instr::Br { |
| 527 | op: vop, |
| 528 | typ: vtyp, |
| 529 | a: va, |
| 530 | b: vb, |
| 531 | thenTarget: vthenTarget, |
| 532 | thenArgs: vthenArgs, |
| 533 | elseTarget: velseTarget, |
| 534 | elseArgs: velseArgs, |
| 535 | }; |
| 536 | } |
| 537 | case super::INSTR_SWITCH => { |
| 538 | let vval = try input.val(); |
| 539 | let vdefaultTarget = try input.target(); |
| 540 | let vdefaultArgs = try input.values(a); |
| 541 | let n = try input.count(binary::MIN_SWITCH_CASE_BYTES); |
| 542 | let start = input.cases.len; |
| 543 | try input.cases.reserve(n, a) catch { |
| 544 | throw binary::Error::Storage; |
| 545 | }; |
| 546 | for _ in 0 .. n { |
| 547 | let value = try input.integer(8) as i64; |
| 548 | let block = try input.target(); |
| 549 | let args = try input.values(a); |
| 550 | try input.cases.append(il::SwitchCase { value, target: block, args }, a) catch { |
| 551 | throw binary::Error::Storage; |
| 552 | }; |
| 553 | } |
| 554 | return il::Instr::Switch { |
| 555 | val: vval, |
| 556 | defaultTarget: vdefaultTarget, |
| 557 | defaultArgs: vdefaultArgs, |
| 558 | cases: il::CaseRange { start, len: n }, |
| 559 | }; |
| 560 | } |
| 561 | case super::INSTR_UNREACHABLE => { |
| 562 | return il::Instr::Unreachable; |
| 563 | } |
| 564 | case super::INSTR_ECALL => { |
| 565 | let vdst = try input.reg(); |
| 566 | let vnum = try input.val(); |
| 567 | let va0 = try input.val(); |
| 568 | let va1 = try input.val(); |
| 569 | let va2 = try input.val(); |
| 570 | let va3 = try input.val(); |
| 571 | let mut vbuf: ?il::EcallBuf = nil; |
| 572 | if try input.flag() { |
| 573 | let ptr = try input.reg(); |
| 574 | let size = try input.word() as u32; |
| 575 | set vbuf = il::EcallBuf { ptr, size }; |
| 576 | } |
| 577 | return il::Instr::Ecall { |
| 578 | dst: vdst, num: vnum, a0: va0, a1: va1, a2: va2, a3: va3, buf: vbuf |
| 579 | }; |
| 580 | } |
| 581 | case super::INSTR_PAGE_SLICE => { |
| 582 | let verr = try input.reg(); |
| 583 | let vslot = try input.reg(); |
| 584 | let vhandle = try input.val(); |
| 585 | let vwrite = try input.flag(); |
| 586 | let vlayout = try input.layoutIndex(); |
| 587 | return il::Instr::PageSlice { |
| 588 | err: verr, slot: vslot, handle: vhandle, write: vwrite, layout: vlayout |
| 589 | }; |
| 590 | } |
| 591 | case super::INSTR_PAGE_HEAP => { |
| 592 | let verr = try input.reg(); |
| 593 | let vslot = try input.reg(); |
| 594 | let vhandle = try input.val(); |
| 595 | return il::Instr::PageHeap { err: verr, slot: vslot, handle: vhandle }; |
| 596 | } |
| 597 | case super::INSTR_HEAP_RESET => { |
| 598 | return il::Instr::HeapReset { heap: try input.val() }; |
| 599 | } |
| 600 | case super::INSTR_DEVICE_READ => { |
| 601 | let t = try input.typ(); |
| 602 | let dst = try input.reg(); |
| 603 | let handle = try input.val(); |
| 604 | let offset = try input.val(); |
| 605 | return il::Instr::DeviceRead { typ: t, dst, handle, offset }; |
| 606 | } |
| 607 | case super::INSTR_DEVICE_WRITE => { |
| 608 | let t = try input.typ(); |
| 609 | let handle = try input.val(); |
| 610 | let offset = try input.val(); |
| 611 | let value = try input.val(); |
| 612 | return il::Instr::DeviceWrite { typ: t, handle, offset, value }; |
| 613 | } |
| 614 | case super::INSTR_FIELD => { |
| 615 | let vdst = try input.reg(); |
| 616 | let vsrc = try input.reg(); |
| 617 | let voffset = try input.word() as u32; |
| 618 | return il::Instr::Field { dst: vdst, src: vsrc, offset: voffset }; |
| 619 | } |
| 620 | case super::INSTR_ELEM => { |
| 621 | let vdst = try input.reg(); |
| 622 | let vbase = try input.reg(); |
| 623 | let vindex = try input.val(); |
| 624 | let vlayout = try input.layoutIndex(); |
| 625 | let vcount = try input.word() as u32; |
| 626 | return il::Instr::Elem { |
| 627 | dst: vdst, base: vbase, index: vindex, layout: vlayout, count: vcount |
| 628 | }; |
| 629 | } |
| 630 | case super::INSTR_BYTES_LOAD => { |
| 631 | let typ = try input.typ(); |
| 632 | let dst = try input.reg(); |
| 633 | let slice = try input.reg(); |
| 634 | let offset = try input.val(); |
| 635 | return il::Instr::BytesLoad { typ, dst, slice, offset }; |
| 636 | } |
| 637 | case super::INSTR_BYTES_STORE => { |
| 638 | let typ = try input.typ(); |
| 639 | let slice = try input.reg(); |
| 640 | let offset = try input.val(); |
| 641 | let value = try input.val(); |
| 642 | return il::Instr::BytesStore { typ, slice, offset, value }; |
| 643 | } |
| 644 | case super::INSTR_SLICE_ELEM => { |
| 645 | let vdst = try input.reg(); |
| 646 | let vslice = try input.reg(); |
| 647 | let vindex = try input.val(); |
| 648 | let vlayout = try input.layoutIndex(); |
| 649 | return il::Instr::SliceElem { |
| 650 | dst: vdst, slice: vslice, index: vindex, layout: vlayout |
| 651 | }; |
| 652 | } |
| 653 | case super::INSTR_SLICE_NEW => { |
| 654 | let vdst = try input.reg(); |
| 655 | let vbase = try input.val(); |
| 656 | let vlayout = try input.layoutIndex(); |
| 657 | let vcount = try input.word() as u32; |
| 658 | let vstart = try input.val(); |
| 659 | let vend = try input.val(); |
| 660 | return il::Instr::SliceNew { |
| 661 | dst: vdst, base: vbase, layout: vlayout, count: vcount, start: vstart, end: vend |
| 662 | }; |
| 663 | } |
| 664 | case super::INSTR_SLICE_SUB => { |
| 665 | let vdst = try input.reg(); |
| 666 | let vsrc = try input.reg(); |
| 667 | let vlayout = try input.layoutIndex(); |
| 668 | let vstart = try input.val(); |
| 669 | let vend = try input.val(); |
| 670 | return il::Instr::SliceSub { |
| 671 | dst: vdst, src: vsrc, layout: vlayout, start: vstart, end: vend |
| 672 | }; |
| 673 | } |
| 674 | case super::INSTR_SLICE_LEN, super::INSTR_SLICE_COPY => |
| 675 | return try input.sliceInstr(tag), |
| 676 | case super::INSTR_SLICE_GROW => return try input.sliceGrow(), |
| 677 | case super::INSTR_LOAD_PTR => { |
| 678 | let vdst = try input.reg(); |
| 679 | let vsrc = try input.reg(); |
| 680 | let voffset = try input.word() as i32; |
| 681 | let nullable = try input.flag(); |
| 682 | return il::Instr::LoadPtr { dst: vdst, src: vsrc, offset: voffset, nullable }; |
| 683 | } |
| 684 | case super::INSTR_STORE_PTR => { |
| 685 | let vsrc = try input.val(); |
| 686 | let vdst = try input.reg(); |
| 687 | let voffset = try input.word() as i32; |
| 688 | return il::Instr::StorePtr { src: vsrc, dst: vdst, offset: voffset }; |
| 689 | } |
| 690 | case super::INSTR_TRAIT_NEW => { |
| 691 | let vdst = try input.reg(); |
| 692 | let vrecv = try input.reg(); |
| 693 | let vtable = try input.val(); |
| 694 | return il::Instr::TraitNew { dst: vdst, recv: vrecv, table: vtable }; |
| 695 | } |
| 696 | case super::INSTR_TRAIT_COPY => { |
| 697 | let vdst = try input.reg(); |
| 698 | let vsrc = try input.reg(); |
| 699 | return il::Instr::TraitCopy { dst: vdst, src: vsrc }; |
| 700 | } |
| 701 | case super::INSTR_UNION_GET => { |
| 702 | let vdst = try input.reg(); |
| 703 | let vsrc = try input.reg(); |
| 704 | let vtag = try input.word() as u32; |
| 705 | let voffset = try input.word() as u32; |
| 706 | let vlayout = try input.layoutIndex(); |
| 707 | return il::Instr::UnionGet { |
| 708 | dst: vdst, src: vsrc, tag: vtag, offset: voffset, layout: vlayout |
| 709 | }; |
| 710 | } |
| 711 | case super::INSTR_UNION_SET => { |
| 712 | let vdst = try input.reg(); |
| 713 | let vtag = try input.word() as u32; |
| 714 | let voffset = try input.word() as u32; |
| 715 | let vsrc = try input.val(); |
| 716 | let vlayout = try input.optionalLayoutIndex(); |
| 717 | return il::Instr::UnionSet { |
| 718 | dst: vdst, tag: vtag, offset: voffset, src: vsrc, layout: vlayout |
| 719 | }; |
| 720 | } |
| 721 | case super::INSTR_TRAIT_CALL => { |
| 722 | let vretTy = try input.valType(); |
| 723 | let mut vdst: ?il::Reg = nil; |
| 724 | if try input.flag() { |
| 725 | set vdst = try input.reg(); |
| 726 | } |
| 727 | let vobj = try input.reg(); |
| 728 | let vmethod = try input.word() as u32; |
| 729 | let vreceiver = try input.word() as u32; |
| 730 | let vargs = try input.values(a); |
| 731 | if vreceiver >= vargs.len { |
| 732 | throw binary::Error::Invalid; |
| 733 | } |
| 734 | return il::Instr::TraitCall { |
| 735 | retTy: vretTy, |
| 736 | dst: vdst, |
| 737 | obj: vobj, |
| 738 | method: vmethod, |
| 739 | receiver: vreceiver, |
| 740 | args: vargs, |
| 741 | }; |
| 742 | } |
| 743 | case super::INSTR_EBREAK => { |
| 744 | return il::Instr::Ebreak; |
| 745 | } |
| 746 | case super::INSTR_MEMORYFENCE => { |
| 747 | return il::Instr::MemoryFence; |
| 748 | } |
| 749 | else => throw binary::Error::Invalid, |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | /// Read an initializer with a repetition count. |
| 754 | export fn (input: &mut Reader['wire]) dataValue(a: &heap::Heap) -> il::DataValue |
| 755 | throws (binary::Error) |
| 756 | { |
| 757 | let tag = try input.byte() as u8; |
| 758 | let mut item: il::DataItem = il::DataItem::Undef; |
| 759 | match tag { |
| 760 | case super::DATA_VAL => { |
| 761 | let t = try input.typ(); |
| 762 | let n = try input.integer(il::typeSize(t)); |
| 763 | set item = il::DataItem::Val { typ: t, val: n as i64 }; |
| 764 | } |
| 765 | case super::DATA_SYM => { |
| 766 | set item = il::DataItem::Sym(try input.symbol()); |
| 767 | } |
| 768 | case super::DATA_FN => { |
| 769 | set item = il::DataItem::Fn(try input.symbol()); |
| 770 | } |
| 771 | case super::DATA_STR => { |
| 772 | set item = il::DataItem::Str(try input.bytes(a)); |
| 773 | } |
| 774 | case super::DATA_UNDEF => { |
| 775 | set item = il::DataItem::Undef; |
| 776 | } |
| 777 | else => throw binary::Error::Invalid, |
| 778 | } |
| 779 | let n = try input.word() as u32; |
| 780 | return il::DataValue { item, count: n }; |
| 781 | } |