#! @file interpreter.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the interpreter: one loop over the operations of the machine's #! code, each reading the words of the frame its operands name. A call copies #! its arguments into the frame of the function it calls, which begins where #! the caller's ends, and a return writes the words it gives into those the #! call names for them. The operations run most are tested for first, and #! those run least are `slow_op`'s, which the code `jit.sakura` compiles #! calls too. A function compiled to the processor's code runs as that code, #! whether the machine begins with it or calls it. import sakura::compiler; module sakura::machine; #! Spends a step of an evaluation's budget, of the steps the host allows a #! call, or of a thread's turn, which ends once it has taken its share, letting #! another run. #! @param this The machine. #! @returns Why the run stops, or `none` where it goes on. def interpreter::spent(&mut this): halt { if this.options.evaluating { return this.exhausted() ? halt::evaluation_limit : halt::none; } return halt::step_limit if this.options.steps != 0 && this.exhausted(); return halt::none if this.threads == null; this.countdown--; return halt::none if this.countdown != 0; return this.next_turn(); } #! Spends a step of an evaluation's budget, where any is left. #! @param this The machine. #! @returns Whether none was left. def interpreter::exhausted(&mut this): bool { return true if this.budget == 0; this.budget--; return false; } #! Runs the call on top of the calls until it returns, leaving the calls #! beneath it, of which there are `base`. A fault records where every call #! was, and lets go of them all. #! @param this The machine. #! @param base How many calls are beneath the one run. #! @returns Why it stopped, or `none` where the call returned. pub def interpreter::execute(&mut this, base: u64): halt { val routines := this.routines.span.data; val first: call_record = this.calls.last(); var current: u32 = first.function; var code := routines[current].code; var fp := first.fp; var pc: u64 = 0; var origin: u64 = 0; var stop := halt::none; val evaluating := this.options.evaluating; # Steps are counted against an evaluation's budget, the steps the host # allows, or a thread's turn. var counting := evaluating || this.options.steps != 0 || this.threads != null; # A function compiled runs as the processor's code, unless the stack of the # system's thread is too near its end, where it is interpreted. if this.jit_on { val entry := this.native_of(current); if entry != 0 { val ran := this.run_native(entry, fp); if ran != jit_declined { if ran == 0 { this.leave(base); return halt::none; } return this.stopped(halt_numbered(ran), this.jit_origin); } } } # A case that stops the run says why in `stop` and leaves the loop, so `stop` # is written only on the way out, and the loop carries it nowhere. while true { origin = pc; val kind := code[pc]; match kind { bc::move as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]]; pc += bc_widths[bc::move as u64]; } bc::konst as u32 => { fp[code[pc + 1]] = code[pc + 2] as u64 | (code[pc + 3] as u64 << 32); pc += bc_widths[bc::konst as u64]; } bc::load_i64 as u32 => { val address := fp[code[pc + 2]]; if address < null_page { stop = halt::null_reference; break; } fp[code[pc + 1]] = ((address + code[pc + 3] as u64) as *u64)[0]; pc += bc_widths[bc::load_i64 as u64]; } bc::store_64 as u32 => { val address := fp[code[pc + 1]]; if address < null_page { stop = halt::null_reference; break; } ((address + code[pc + 2] as u64) as *mut u64)[0] = fp[code[pc + 3]]; pc += bc_widths[bc::store_64 as u64]; } bc::cell as u32 => { fp[code[pc + 1]] = fp as u64 + code[pc + 2] as u64; pc += bc_widths[bc::cell as u64]; } bc::add as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] + fp[code[pc + 3]]; pc += bc_widths[bc::add as u64]; } bc::jump as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = code[pc + 1] as u64; } bc::jump_if as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = fp[code[pc + 1]] != 0 ? code[pc + 2] as u64 : pc + bc_widths[bc::jump_if as u64]; } bc::jump_zero as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = fp[code[pc + 1]] == 0 ? code[pc + 2] as u64 : pc + bc_widths[bc::jump_zero as u64]; } bc::eq as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] == fp[code[pc + 3]] ? 1 : 0; pc += bc_widths[bc::eq as u64]; } bc::ne as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] != fp[code[pc + 3]] ? 1 : 0; pc += bc_widths[bc::ne as u64]; } bc::ltu as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] < fp[code[pc + 3]] ? 1 : 0; pc += bc_widths[bc::ltu as u64]; } bc::lts as u32 => { fp[code[pc + 1]] = (fp[code[pc + 2]] as i64) < (fp[code[pc + 3]] as i64) ? 1 : 0; pc += bc_widths[bc::lts as u64]; } bc::add_k as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] + bc_constant(code[pc + 3]); pc += bc_widths[bc::add_k as u64]; } bc::add_k_jump as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } fp[code[pc + 1]] = fp[code[pc + 2]] + bc_constant(code[pc + 3]); pc = code[pc + 4] as u64; } bc::move_jump as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } fp[code[pc + 1]] = fp[code[pc + 2]]; pc = code[pc + 3] as u64; } bc::load_u8_x as u32 => { val address := fp[code[pc + 2]] + fp[code[pc + 3]]; if address < null_page { stop = halt::null_reference; break; } fp[code[pc + 1]] = ((address + code[pc + 4] as u64) as *u8)[0] as u64; pc += bc_widths[bc::load_u8_x as u64]; } bc::store_8_x as u32 => { val address := fp[code[pc + 1]] + fp[code[pc + 2]]; if address < null_page { stop = halt::null_reference; break; } ((address + code[pc + 3] as u64) as *mut u8)[0] = fp[code[pc + 4]] as u8; pc += bc_widths[bc::store_8_x as u64]; } bc::jump_ltu as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = fp[code[pc + 1]] < fp[code[pc + 2]] ? code[pc + 3] as u64 : pc + bc_widths[bc::jump_ltu as u64]; } bc::jump_leu as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = fp[code[pc + 1]] <= fp[code[pc + 2]] ? code[pc + 3] as u64 : pc + bc_widths[bc::jump_leu as u64]; } bc::jump_eq as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = fp[code[pc + 1]] == fp[code[pc + 2]] ? code[pc + 3] as u64 : pc + bc_widths[bc::jump_eq as u64]; } bc::jump_ne as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = fp[code[pc + 1]] != fp[code[pc + 2]] ? code[pc + 3] as u64 : pc + bc_widths[bc::jump_ne as u64]; } bc::jump_lts as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = (fp[code[pc + 1]] as i64) < (fp[code[pc + 2]] as i64) ? code[pc + 3] as u64 : pc + bc_widths[bc::jump_lts as u64]; } bc::jump_les as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = (fp[code[pc + 1]] as i64) <= (fp[code[pc + 2]] as i64) ? code[pc + 3] as u64 : pc + bc_widths[bc::jump_les as u64]; } bc::jump_eq_k as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = fp[code[pc + 1]] == bc_constant(code[pc + 2]) ? code[pc + 3] as u64 : pc + bc_widths[bc::jump_eq_k as u64]; } bc::jump_ne_k as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = fp[code[pc + 1]] != bc_constant(code[pc + 2]) ? code[pc + 3] as u64 : pc + bc_widths[bc::jump_ne_k as u64]; } bc::jump_lts_k as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = (fp[code[pc + 1]] as i64) < (bc_constant(code[pc + 2]) as i64) ? code[pc + 3] as u64 : pc + bc_widths[bc::jump_lts_k as u64]; } bc::jump_ltu_k as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = fp[code[pc + 1]] < bc_constant(code[pc + 2]) ? code[pc + 3] as u64 : pc + bc_widths[bc::jump_ltu_k as u64]; } bc::jump_les_k as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = (fp[code[pc + 1]] as i64) <= (bc_constant(code[pc + 2]) as i64) ? code[pc + 3] as u64 : pc + bc_widths[bc::jump_les_k as u64]; } bc::jump_leu_k as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = fp[code[pc + 1]] <= bc_constant(code[pc + 2]) ? code[pc + 3] as u64 : pc + bc_widths[bc::jump_leu_k as u64]; } bc::jump_gts_k as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = (fp[code[pc + 1]] as i64) > (bc_constant(code[pc + 2]) as i64) ? code[pc + 3] as u64 : pc + bc_widths[bc::jump_gts_k as u64]; } bc::jump_gtu_k as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = fp[code[pc + 1]] > bc_constant(code[pc + 2]) ? code[pc + 3] as u64 : pc + bc_widths[bc::jump_gtu_k as u64]; } bc::jump_ges_k as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = (fp[code[pc + 1]] as i64) >= (bc_constant(code[pc + 2]) as i64) ? code[pc + 3] as u64 : pc + bc_widths[bc::jump_ges_k as u64]; } bc::jump_geu_k as u32 => { if counting { stop = this.spent(); break if stop != halt::none; } pc = fp[code[pc + 1]] >= bc_constant(code[pc + 2]) ? code[pc + 3] as u64 : pc + bc_widths[bc::jump_geu_k as u64]; } bc::check_ltu as u32 => { if fp[code[pc + 1]] >= fp[code[pc + 2]] { stop = halt_of(code[pc + 3] as u64); break; } pc += bc_widths[bc::check_ltu as u64]; } bc::check_ltu_k as u32 => { if fp[code[pc + 1]] >= bc_constant(code[pc + 2]) { stop = halt_of(code[pc + 3] as u64); break; } pc += bc_widths[bc::check_ltu_k as u64]; } bc::band_k as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] & bc_constant(code[pc + 3]); pc += bc_widths[bc::band_k as u64]; } bc::shl_k as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] << bc_constant(code[pc + 3]); pc += bc_widths[bc::shl_k as u64]; } bc::shru_k as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] >> bc_constant(code[pc + 3]); pc += bc_widths[bc::shru_k as u64]; } bc::shrs_k as u32 => { fp[code[pc + 1]] = ((fp[code[pc + 2]] as i64) >> bc_constant(code[pc + 3])) as u64; pc += bc_widths[bc::shrs_k as u64]; } bc::mul_k as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] * bc_constant(code[pc + 3]); pc += bc_widths[bc::mul_k as u64]; } bc::store_k64 as u32 => { val address := fp[code[pc + 1]]; if address < null_page { stop = halt::null_reference; break; } ((address + code[pc + 2] as u64) as *mut u64)[0] = code[pc + 3] as u64 | (code[pc + 4] as u64 << 32); pc += bc_widths[bc::store_k64 as u64]; } bc::store_k8 as u32, bc::store_k16 as u32, bc::store_k32 as u32 => { val address := fp[code[pc + 1]]; if address < null_page { stop = halt::null_reference; break; } val at := address + code[pc + 2] as u64; if kind == bc::store_k8 as u32 { (at as *mut u8)[0] = code[pc + 3] as u8; } else if kind == bc::store_k16 as u32 { (at as *mut u16)[0] = code[pc + 3] as u16; } else { (at as *mut u32)[0] = code[pc + 3]; } pc += bc_widths[bc::store_k8 as u64]; } bc::load_u8 as u32 => { val address := fp[code[pc + 2]]; if address < null_page { stop = halt::null_reference; break; } fp[code[pc + 1]] = ((address + code[pc + 3] as u64) as *u8)[0] as u64; pc += bc_widths[bc::load_u8 as u64]; } bc::call as u32 => { val callee := code[pc + 1]; val argc := code[pc + 2] as u64; val results := pc + 3 + argc; val back := results + 1 + code[results] as u64; val held: routine = routines[callee]; val next := (fp as u64 + routines[current].frame) as *mut u64; if counting { stop = this.spent(); break if stop != halt::none; } if next as u64 + held.frame > this.stack_end { stop = halt::stack_overflow; break; } val k := this.pass(&held, next, fp, code, pc + 3, 0, 0); val dst: u64 = held.returned != 0 ? fp[code[pc + 3 + k]] : 0; this.enter(callee, &held, next, back, pc, results, dst); if this.jit_on { val ran := this.call_native(callee, next, fp, code, results, current); if ran != jit_declined { if ran != 0 { stop = halt_numbered(ran); origin = this.jit_origin; break; } counting = evaluating || this.options.steps != 0 || this.threads != null; pc = back; continue; } } current = callee; code = held.code; fp = next; pc = 0; } bc::ret as u32 => { val count := code[pc + 1] as u64; val done: call_record = this.calls.last(); if this.calls.length == base + 1 { for i in 0..count { this.results[i] = fp[code[pc + 2 + i]]; } this.leave(base); return halt::none; } val caller: call_record = this.calls.at(this.calls.length - 2); val back_code := routines[caller.function].code; if done.results != none { for i in 0..count { caller.fp[back_code[done.results + 1 + i]] = fp[code[pc + 2 + i]]; } } this.calls.truncate(); current = caller.function; code = back_code; fp = caller.fp; pc = done.back; this.top = fp as u64 + routines[current].frame; } bc::sub as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] - fp[code[pc + 3]]; pc += bc_widths[bc::sub as u64]; } bc::leu as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] <= fp[code[pc + 3]] ? 1 : 0; pc += bc_widths[bc::leu as u64]; } bc::les as u32 => { fp[code[pc + 1]] = (fp[code[pc + 2]] as i64) <= (fp[code[pc + 3]] as i64) ? 1 : 0; pc += bc_widths[bc::les as u64]; } bc::mul as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] * fp[code[pc + 3]]; pc += bc_widths[bc::mul as u64]; } bc::band as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] & fp[code[pc + 3]]; pc += bc_widths[bc::band as u64]; } bc::bor as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] | fp[code[pc + 3]]; pc += bc_widths[bc::bor as u64]; } bc::wrap as u32 => { var value := fp[code[pc + 2]]; val bits := code[pc + 3] as u64; if bits < 64 { val mask := (1u << bits) - 1; value = value & mask; if code[pc + 4] != 0 && (value >> (bits - 1)) != 0 { value = value | ~mask; } } fp[code[pc + 1]] = value; pc += bc_widths[bc::wrap as u64]; } bc::load_i32 as u32 => { val address := fp[code[pc + 2]]; if address < null_page { stop = halt::null_reference; break; } fp[code[pc + 1]] = (((address + code[pc + 3] as u64) as *i32)[0] as i64) as u64; pc += bc_widths[bc::load_i32 as u64]; } bc::load_u32 as u32 => { val address := fp[code[pc + 2]]; if address < null_page { stop = halt::null_reference; break; } fp[code[pc + 1]] = ((address + code[pc + 3] as u64) as *u32)[0] as u64; pc += bc_widths[bc::load_u32 as u64]; } bc::store_8 as u32 => { val address := fp[code[pc + 1]]; if address < null_page { stop = halt::null_reference; break; } ((address + code[pc + 2] as u64) as *mut u8)[0] = fp[code[pc + 3]] as u8; pc += bc_widths[bc::store_8 as u64]; } bc::store_32 as u32 => { val address := fp[code[pc + 1]]; if address < null_page { stop = halt::null_reference; break; } ((address + code[pc + 2] as u64) as *mut u32)[0] = fp[code[pc + 3]] as u32; pc += bc_widths[bc::store_32 as u64]; } bc::copy as u32 => { val to := fp[code[pc + 1]]; val from := fp[code[pc + 2]]; val size := code[pc + 3] as u64; if size != 0 && (to < null_page || from < null_page) { stop = halt::null_reference; break; } memcpy(to as *mut u8, from as *u8, size); if this.options.checks { this.copy_unwritten(to, from, size); } pc += bc_widths[bc::copy as u64]; } bc::clear as u32 => { val to := fp[code[pc + 1]]; val size := code[pc + 2] as u64; if size != 0 && to < null_page { stop = halt::null_reference; break; } memset(to as *mut u8, 0, size); if this.options.checks { this.wrote(to, size); } pc += bc_widths[bc::clear as u64]; } bc::check as u32 => { if fp[code[pc + 1]] == 0 { stop = halt_of(code[pc + 2] as u64); break; } pc += bc_widths[bc::check as u64]; } bc::global as u32 => { fp[code[pc + 1]] = this.globals as u64 + code[pc + 2] as u64; pc += bc_widths[bc::global as u64]; } bc::datum as u32 => { fp[code[pc + 1]] = this.data[code[pc + 2]] + code[pc + 3] as u64; pc += bc_widths[bc::datum as u64]; } bc::call_trait as u32 => { val argc := code[pc + 3] as u64; val args := pc + 4; val results := args + argc; val back := results + 1 + code[results] as u64; val slot := code[pc + 2] as u64; val vtable := fp[code[args + 1]]; if vtable >= this.c.program.vtables.length || slot >= this.c.program.vtables[vtable].functions.length { stop = halt::null_reference; break; } val callee: u32 = this.c.program.vtables[vtable].functions[slot]; if callee == no_function { stop = halt::null_reference; break; } if counting { stop = this.spent(); break if stop != halt::none; } val held: routine = routines[callee]; val next := (fp as u64 + routines[current].frame) as *mut u64; if next as u64 + held.frame > this.stack_end { stop = halt::stack_overflow; break; } # `this` is the reference the trait reference holds, and a default of a # trait this one composes is given the vtable whose slots it addresses. var k: u64 = 2; if held.count != 0 { val taken: param_slot = this.params[held.params as u64]; val at := taken.offset / word_size; next[at] = fp[code[args]]; if taken.words == 2 { next[at + 1] = this.c.program.vtables[vtable].owners[slot] as u64; } k = this.pass(&held, next, fp, code, args, 2, 1); } val dst: u64 = held.returned != 0 ? fp[code[args + k]] : 0; this.enter(callee, &held, next, back, pc, results, dst); if this.jit_on { val ran := this.call_native(callee, next, fp, code, results, current); if ran != jit_declined { if ran != 0 { stop = halt_numbered(ran); origin = this.jit_origin; break; } counting = evaluating || this.options.steps != 0 || this.threads != null; pc = back; continue; } } current = callee; code = held.code; fp = next; pc = 0; } bc::call_value as u32 => { val argc := code[pc + 2] as u64; val args := pc + 3; val results := args + argc; val back := results + 1 + code[results] as u64; val index := fp[code[args]] - 1; if index >= this.routines.length { stop = halt::null_reference; break; } if counting { stop = this.spent(); break if stop != halt::none; } val callee := index as u32; val held: routine = routines[callee]; val next := (fp as u64 + routines[current].frame) as *mut u64; if next as u64 + held.frame > this.stack_end { stop = halt::stack_overflow; break; } val k := this.pass(&held, next, fp, code, args, 2, 0); if held.closure { next[held.env / word_size] = fp[code[args + 1]]; } val dst: u64 = held.returned != 0 ? fp[code[args + k]] : 0; this.enter(callee, &held, next, back, pc, results, dst); if this.jit_on { val ran := this.call_native(callee, next, fp, code, results, current); if ran != jit_declined { if ran != 0 { stop = halt_numbered(ran); origin = this.jit_origin; break; } counting = evaluating || this.options.steps != 0 || this.threads != null; pc = back; continue; } } current = callee; code = held.code; fp = next; pc = 0; } bc::ret_memory as u32 => { val address := fp[code[pc + 1]]; val done: call_record = this.calls.last(); val size := routines[current].returned; if done.dst != 0 && address != 0 { memcpy(done.dst as *mut u8, address as *u8, size); } if this.calls.length == base + 1 { this.leave(base); return halt::none; } val caller: call_record = this.calls.at(this.calls.length - 2); this.calls.truncate(); current = caller.function; code = routines[current].code; fp = caller.fp; pc = done.back; this.top = fp as u64 + routines[current].frame; } bc::shl as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] << fp[code[pc + 3]]; pc += bc_widths[bc::shl as u64]; } bc::shru as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] >> fp[code[pc + 3]]; pc += bc_widths[bc::shru as u64]; } bc::shrs as u32 => { fp[code[pc + 1]] = ((fp[code[pc + 2]] as i64) >> fp[code[pc + 3]]) as u64; pc += bc_widths[bc::shrs as u64]; } bc::bxor as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] ^ fp[code[pc + 3]]; pc += bc_widths[bc::bxor as u64]; } bc::divu as u32, bc::remu as u32, bc::divs as u32, bc::rems as u32 => { val a := fp[code[pc + 2]]; val b := fp[code[pc + 3]]; if b == 0 { stop = halt::division; break; } var made: u64 = 0; if kind == bc::divu as u32 { made = a / b; } else if kind == bc::remu as u32 { made = a % b; } else if (b as i64) == -1 { made = kind == bc::divs as u32 ? 0 - a : 0; } else if kind == bc::divs as u32 { made = ((a as i64) / (b as i64)) as u64; } else { made = ((a as i64) % (b as i64)) as u64; } fp[code[pc + 1]] = made; pc += bc_widths[bc::divu as u64]; } bc::eqsu as u32 => { val a := fp[code[pc + 2]]; val b := fp[code[pc + 3]]; fp[code[pc + 1]] = (a as i64) >= 0 && a == b ? 1 : 0; pc += bc_widths[bc::eqsu as u64]; } bc::ltsu as u32 => { val a := fp[code[pc + 2]]; val b := fp[code[pc + 3]]; fp[code[pc + 1]] = (a as i64) < 0 || a < b ? 1 : 0; pc += bc_widths[bc::ltsu as u64]; } bc::lesu as u32 => { val a := fp[code[pc + 2]]; val b := fp[code[pc + 3]]; fp[code[pc + 1]] = (a as i64) < 0 || a <= b ? 1 : 0; pc += bc_widths[bc::lesu as u64]; } bc::ltus as u32 => { val a := fp[code[pc + 2]]; val b := fp[code[pc + 3]]; fp[code[pc + 1]] = (b as i64) >= 0 && a < b ? 1 : 0; pc += bc_widths[bc::ltus as u64]; } bc::leus as u32 => { val a := fp[code[pc + 2]]; val b := fp[code[pc + 3]]; fp[code[pc + 1]] = (b as i64) >= 0 && a <= b ? 1 : 0; pc += bc_widths[bc::leus as u64]; } # Each arithmetic of reals is a case of its own, as code works them # often, and each of `f32`s rounds what the `f64`s' gives. bc::fadd as u32 => { val reals := fp as *mut f64; reals[code[pc + 1]] = reals[code[pc + 2]] + reals[code[pc + 3]]; pc += bc_widths[bc::fadd as u64]; } bc::fsub as u32 => { val reals := fp as *mut f64; reals[code[pc + 1]] = reals[code[pc + 2]] - reals[code[pc + 3]]; pc += bc_widths[bc::fsub as u64]; } bc::fmul as u32 => { val reals := fp as *mut f64; reals[code[pc + 1]] = reals[code[pc + 2]] * reals[code[pc + 3]]; pc += bc_widths[bc::fmul as u64]; } bc::fdiv as u32 => { val reals := fp as *mut f64; reals[code[pc + 1]] = reals[code[pc + 2]] / reals[code[pc + 3]]; pc += bc_widths[bc::fdiv as u64]; } bc::frem as u32 => { val reals := fp as *mut f64; reals[code[pc + 1]] = reals[code[pc + 2]] % reals[code[pc + 3]]; pc += bc_widths[bc::frem as u64]; } bc::fadd_f32 as u32 => { val reals := fp as *mut f64; reals[code[pc + 1]] = ((reals[code[pc + 2]] + reals[code[pc + 3]]) as f32) as f64; pc += bc_widths[bc::fadd_f32 as u64]; } bc::fsub_f32 as u32 => { val reals := fp as *mut f64; reals[code[pc + 1]] = ((reals[code[pc + 2]] - reals[code[pc + 3]]) as f32) as f64; pc += bc_widths[bc::fsub_f32 as u64]; } bc::fmul_f32 as u32 => { val reals := fp as *mut f64; reals[code[pc + 1]] = ((reals[code[pc + 2]] * reals[code[pc + 3]]) as f32) as f64; pc += bc_widths[bc::fmul_f32 as u64]; } bc::fdiv_f32 as u32 => { val reals := fp as *mut f64; reals[code[pc + 1]] = ((reals[code[pc + 2]] / reals[code[pc + 3]]) as f32) as f64; pc += bc_widths[bc::fdiv_f32 as u64]; } bc::feq as u32, bc::fne as u32, bc::flt as u32, bc::fle as u32 => { val reals := fp as *mut f64; val x := reals[code[pc + 2]]; val y := reals[code[pc + 3]]; val holds := match kind { bc::feq as u32 => x == y, bc::fne as u32 => x != y, bc::flt as u32 => x < y, else => x <= y }; fp[code[pc + 1]] = holds ? 1 : 0; pc += bc_widths[bc::feq as u64]; } bc::neg as u32 => { fp[code[pc + 1]] = 0 - fp[code[pc + 2]]; pc += bc_widths[bc::neg as u64]; } bc::bnot as u32 => { fp[code[pc + 1]] = ~fp[code[pc + 2]]; pc += bc_widths[bc::bnot as u64]; } bc::lnot as u32 => { fp[code[pc + 1]] = fp[code[pc + 2]] == 0 ? 1 : 0; pc += bc_widths[bc::lnot as u64]; } bc::fsingle as u32 => { val reals := fp as *mut f64; reals[code[pc + 1]] = (reals[code[pc + 2]] as f32) as f64; pc += bc_widths[bc::fsingle as u64]; } bc::fneg as u32 => { val reals := fp as *mut f64; reals[code[pc + 1]] = -reals[code[pc + 2]]; pc += bc_widths[bc::fneg as u64]; } bc::itof as u32, bc::utof as u32 => { val reals := fp as *mut f64; val word := fp[code[pc + 2]]; reals[code[pc + 1]] = kind == bc::itof as u32 ? (word as i64) as f64 : word as f64; pc += bc_widths[bc::itof as u64]; } bc::fsqrt as u32 => { val reals := fp as *mut f64; reals[code[pc + 1]] = root_of(reals[code[pc + 2]]); pc += bc_widths[bc::fsqrt as u64]; } bc::ones as u32, bc::clz as u32, bc::ctz as u32, bc::bswap as u32 => { val word := fp[code[pc + 2]]; fp[code[pc + 1]] = match kind { bc::ones as u32 => ones_of(word), bc::clz as u32 => leading_zeros_of(word), bc::ctz as u32 => trailing_zeros_of(word), else => bytes_swapped(word) }; pc += bc_widths[bc::ones as u64]; } bc::ftoi as u32 => { val reals := fp as *mut f64; fp[code[pc + 1]] = (reals[code[pc + 2]] as i64) as u64; pc += bc_widths[bc::ftoi as u64]; } bc::ftou as u32 => { val reals := fp as *mut f64; fp[code[pc + 1]] = reals[code[pc + 2]] as u64; pc += bc_widths[bc::ftou as u64]; } bc::select as u32 => { # A real is chosen by its bits, as a word is. fp[code[pc + 1]] = fp[code[pc + 2]] != 0 ? fp[code[pc + 3]] : fp[code[pc + 4]]; pc += bc_widths[bc::select as u64]; } bc::load_i8 as u32, bc::load_i16 as u32, bc::load_u16 as u32, bc::load_f32 as u32 => { val address := fp[code[pc + 2]]; if address < null_page { stop = halt::null_reference; break; } val at := address + code[pc + 3] as u64; if kind == bc::load_f32 as u32 { (fp as *mut f64)[code[pc + 1]] = (at as *f32)[0] as f64; } else if kind == bc::load_i8 as u32 { fp[code[pc + 1]] = ((at as *i8)[0] as i64) as u64; } else if kind == bc::load_i16 as u32 { fp[code[pc + 1]] = ((at as *i16)[0] as i64) as u64; } else { fp[code[pc + 1]] = (at as *u16)[0] as u64; } pc += bc_widths[bc::load_i8 as u64]; } bc::store_16 as u32, bc::store_f32 as u32 => { val address := fp[code[pc + 1]]; if address < null_page { stop = halt::null_reference; break; } val at := address + code[pc + 2] as u64; if kind == bc::store_f32 as u32 { (at as *mut f32)[0] = (fp as *mut f64)[code[pc + 3]] as f32; } else { (at as *mut u16)[0] = fp[code[pc + 3]] as u16; } pc += bc_widths[bc::store_16 as u64]; } bc::switch as u32 => { val value := fp[code[pc + 1]]; val cases := code[pc + 2] as u64; var target := code[pc + 3 + 3 * cases] as u64; for i in 0..cases { val at := pc + 3 + 3 * i; if (code[at] as u64 | (code[at + 1] as u64 << 32)) == value { target = code[at + 2] as u64; break; } } if counting { stop = this.spent(); break if stop != halt::none; } pc = target; } bc::safepoint as u32 => { # Handlers run in the thread the program began with. if evaluating || (this.threads != null && this.strand != this.threads[0].strands[0]) || !signals_came() { pc += bc_widths[bc::safepoint as u64]; continue; } val callee := code[pc + 1]; val held: routine = routines[callee]; val next := (fp as u64 + routines[current].frame) as *mut u64; if next as u64 + held.frame > this.stack_end { stop = halt::stack_overflow; break; } this.enter(callee, &held, next, pc + 2, pc, none, 0); current = callee; code = held.code; fp = next; pc = 0; } else => { stop = this.slow_op(kind, fp, code, pc, current); break if stop != halt::none; # Starting a thread has the threads take turns, by steps. counting = evaluating || this.options.steps != 0 || this.threads != null; pc += op_width(code, pc); } } } return this.stopped(stop, origin); } #! Ends a run that stopped: where another thread stopped the program, its #! trace is the one reported, and otherwise where each call was is recorded. #! @param this The machine. #! @param stop Why it stopped. #! @param origin Where in its code the innermost call was. #! @returns Why it stopped. def interpreter::stopped(&mut this, stop: halt, origin: u64): halt { if stop == halt::elsewhere { if this.options.checks && this.threads[0].stop == halt::exited { this.keep_frames(); } this.calls.clear(); this.depth = 0; this.top = this.stack as u64; return stop; } this.record(origin); return stop; } #! How many words an operation of the code takes, its operation and operands #! together, as `bc_widths` says and the counts those followed by one give. #! @param code The code. #! @param pc Where the operation is. #! @returns The words. pub def op_width(code: *u32, pc: u64): u64 { val kind := code[pc]; val base := bc_widths[kind as u64]; return match kind { bc::call as u32, bc::call_value as u32, bc::call_c as u32 => base + code[pc + 2] as u64 + code[pc + 3 + code[pc + 2] as u64] as u64, bc::call_trait as u32 => base + code[pc + 3] as u64 + code[pc + 4 + code[pc + 3] as u64] as u64, bc::format_value as u32 => base + code[pc + 2] as u64, bc::switch as u32 => base + 3 * code[pc + 2] as u64, bc::ret as u32 => base + code[pc + 1] as u64, else => base }; } #! Runs one of the operations code runs least: memory the program gives back #! itself, text being formatted, the system, C, threads, atomics, what stops the #! program, and what only a run checking its memory or evaluating constants #! has. Neither the interpreter's loop nor compiled code holds these itself. #! @param this The machine. #! @param kind The operation. #! @param fp The frame. #! @param code The code of the function running. #! @param pc Where the operation is. #! @param current The function running. #! @returns Why the run stops, or `none` where it goes on past the operation. pub def interpreter::slow_op(&mut this, kind: u32, fp: *mut u64, code: *u32, pc: u64, current: u32): halt { val evaluating := this.options.evaluating; match kind { bc::allocate as u32 => { fp[code[pc + 1]] = this.allocate(fp[code[pc + 2]], code[pc + 3] == 0); if this.options.checks { this.note_site(fp[code[pc + 1]], pc as u64); } } bc::release as u32 => { return this.release(fp[code[pc + 1]]); } bc::resize as u32 => { val address := fp[code[pc + 2]]; return halt::invalid_release if address != 0 && !this.blocks.contains(&address); # Running with its memory checked, the block keeps where it was allocated. var site: u64 = none; if this.options.checks && this.sites.get(&address) is some(was) { site = was; } fp[code[pc + 1]] = this.resize(address, fp[code[pc + 3]]); if this.options.checks { if site == none { this.note_site(fp[code[pc + 1]], pc as u64); } else { this.sites.set(fp[code[pc + 1]], site); } } } bc::equal as u32 => { fp[code[pc + 1]] = this.equal(fp[code[pc + 2]], fp[code[pc + 3]], code[pc + 4], 0) ? 1 : 0; val stopped := this.read_stopped(); return stopped if stopped != halt::none; } bc::retrait as u32 => { val map: u32 = code[pc + 3]; val vtable := fp[code[pc + 2]]; return halt::null_reference if vtable >= this.c.program.retraits[map].vtables.length; fp[code[pc + 1]] = this.c.program.retraits[map].vtables[vtable] as u64; } bc::format as u32 => { this.begin_text(); } bc::format_text as u32 => { val index: u32 = code[pc + 1]; val length := this.c.program.data[index].bytes.length; this.add_text(this.data[index], length == 0 ? 0 : length - 1); } bc::format_value as u32 => { this.format_operands(code[pc + 1], fp, code, pc + 3, code[pc + 2] as u64); val stopped := this.read_stopped(); return stopped if stopped != halt::none; } bc::format_bytes as u32 => { this.add_text(fp[code[pc + 1]], fp[code[pc + 2]]); } bc::format_owned as u32 => { val made := this.owned_text(); if this.options.checks { this.note_site(made, pc as u64); } fp[code[pc + 1]] = made; fp[code[pc + 2]] = made == 0 ? 0 : this.blocks[made]; } bc::syscall as u32 => { return halt::not_constant if evaluating; # Other threads run while it waits in the system, but around starting a # copy of the program, where none may hold the turn the copy needs. val number := fp[code[pc + 2]]; val copies := number == 56 || number == 57 || number == 58; val mine := this.strand; var spec: i64[2]; val time := number == 202 ? this.wait_time(fp[code[pc + 4]], fp[code[pc + 6]], (&mut spec) as *mut i64) : fp[code[pc + 6]]; if !copies { this.pause(); } val result := system_call(number, fp[code[pc + 3]], fp[code[pc + 4]], fp[code[pc + 5]], time, fp[code[pc + 7]], fp[code[pc + 8]], this); if !copies { val after := this.resume(mine); return after if after != halt::none; } if copies && result == 0 { this.forked(); } if this.options.checks && result as i64 >= 0 { this.note_mapping(number, fp[code[pc + 3]], fp[code[pc + 4]], fp[code[pc + 5]], result); } # What the system wrote through an address it was given is not known, so # all it may have reached counts as written. if this.options.checks { for k in 3..9u { this.wrote_through(fp[code[pc + k]]); } } fp[code[pc + 1]] = result; } bc::environ as u32 => { return halt::not_constant if evaluating; fp[code[pc + 1]] = this.environment(); } bc::signals as u32 => { return halt::not_constant if evaluating; _ = signals_came(); fp[code[pc + 1]] = noted_signals; noted_signals = 0; } bc::catch as u32 => { return halt::not_constant if evaluating; fp[code[pc + 1]] = catch_signal(fp[code[pc + 2]] as i64) as u64; } bc::exit as u32 => { this.status = fp[code[pc + 1]] as i32; if this.options.checks { this.keep_frames(); } this.calls.clear(); this.depth = 0; this.top = this.stack as u64; return halt::exited; } bc::abort as u32 => { return halt::aborted; } bc::fault as u32 => { # Another thread may have faulted first, whose text is kept. if this.fault_text.length == 0 { this.fault_text = string::from(span::::over(fp[code[pc + 1]] as *u8, fp[code[pc + 2]]), nothing {}); } return halt::faulted; } bc::trap as u32 => { return halt_of(code[pc + 1] as u64); } bc::trap_word as u32 => { return halt_of(fp[code[pc + 1]]); } bc::ready as u32 => { val index := code[pc + 1] as u64; if index >= this.ready.length || !this.ready[index] { this.waiting = index as u32; return halt::not_evaluated; } } bc::reach as u32 => { val held := evaluating ? this.reaches(fp[code[pc + 1]], fp[code[pc + 2]], code[pc + 3] as u64) : this.checked_reach(fp[code[pc + 1]], fp[code[pc + 2]], code[pc + 3] as u64); return halt::invalid_access if !held; } bc::same as u32 => { return halt::address if !this.same_memory(fp[code[pc + 1]], fp[code[pc + 2]]); } bc::atomic as u32 => { val operation := code[pc + 2]; if operation == atomic_fence { fence(order::seq_cst); } else { val address := fp[code[pc + 4]]; return halt::null_reference if address < null_page; # An operation taking fewer operands names the frame's scratch word for # the others, which nothing need have written, so only those it takes # are read. val a := operation == atomic_load ? 0u : fp[code[pc + 5]]; val b := operation == atomic_compare_exchange ? fp[code[pc + 6]] : 0u; fp[code[pc + 1]] = atomically(address, operation, code[pc + 3] as u64, a, b); if this.options.checks { this.wrote(address, word_size); } } this.race_atomic(fp[code[pc + 4]], operation, fp[code[pc + 7]]); } bc::start as u32 => { return halt::not_constant if evaluating; val function := fp[code[pc + 2]]; return halt::null_reference if function == 0 || function > this.routines.length; fp[code[pc + 1]] = this.start_thread(function, fp[code[pc + 3]], fp[code[pc + 4]]) as u64; } bc::unreachable as u32 => { return halt::unreachable; } bc::forget as u32 => { this.forget(fp[code[pc + 1]], code[pc + 2] as u64); this.framed_unwritten = true; } bc::watch_read as u32 => { val address := fp[code[pc + 1]]; return halt::unwritten if address >= null_page && this.unwritten_in(address + code[pc + 2] as u64, code[pc + 3] as u64); if this.races.on && address >= null_page { val raced := this.race_access(address + code[pc + 2] as u64, code[pc + 3] as u64, false, current, pc); return raced if raced != halt::none; } } bc::watch_write as u32 => { this.wrote(fp[code[pc + 1]] + code[pc + 2] as u64, code[pc + 3] as u64); if this.races.on && fp[code[pc + 1]] >= null_page { val raced := this.race_access(fp[code[pc + 1]] + code[pc + 2] as u64, code[pc + 3] as u64, true, current, pc); return raced if raced != halt::none; } } bc::call_c as u32 => { # Only the system running the machine, Windows, is called, as the C # machine calls C there; elsewhere this machine does not call C. when os == windows { return halt::not_constant if evaluating; val argc := code[pc + 2] as u64; # What C gives `system` only its own code reads, so the program may # hold memory of C's once code of its own calls C, even C `system` # declares, as where a global of its own is given its value. if this.options.checks && !this.called_c { this.called_c = !this.in_system_code(current, pc); } val halted := this.call_foreign(code, pc, fp); return halted if halted != halt::none; if this.options.checks { for k in 1..argc { this.wrote_through(fp[code[pc + 3 + k]]); } } } else { return evaluating ? halt::not_constant : halt::unsupported; } } bc::c_address as u32 => { when os == windows { return halt::not_constant if evaluating; fp[code[pc + 1]] = this.c_address(code[pc + 2]); } else { return evaluating ? halt::not_constant : halt::unsupported; } } bc::callback as u32 => { when os == windows { return halt::not_constant if evaluating; val made := this.callback_address(code[pc + 2]); return halt::out_of_memory if made == 0; fp[code[pc + 1]] = made; } else { return evaluating ? halt::not_constant : halt::unsupported; } } else => { # What only an executable has, this machine does not do. return evaluating ? halt::not_constant : halt::unsupported; } } return halt::none; } #! Copies the arguments of a call into the frame of the function it calls, as #! the function's parameters say, from the operand and parameter given on. #! @param this The machine. #! @param held The function called. #! @param next Its frame. #! @param fp The caller's frame. #! @param code The caller's code. #! @param args Where the call's operands begin in the code. #! @param from The first operand copied. #! @param first The first parameter copied. #! @returns The operand after the last copied. def interpreter::pass(&mut this, held: &routine, next: *mut u64, fp: *mut u64, code: *u32, args: u64, from: u64, first: u32): u64 { var k := from; for p in first..held.count { val slot: param_slot = this.params[held.params as u64 + p as u64]; if slot.memory { val source := fp[code[args + k]]; if source != 0 { memcpy((next as u64 + slot.offset) as *mut u8, source as *u8, slot.size); } k++; continue; } val at := slot.offset / word_size; for w in 0..slot.words { next[at + w as u64] = fp[code[args + k]]; k++; } } return k; } #! Begins a call: the cells of the frame of the function called that are read #! before they are written are zeroed, and the call is recorded. #! @param this The machine. #! @param callee The function called. #! @param held How the machine calls it. #! @param next Its frame. #! @param back Where the caller goes on. #! @param site Where the call is in the caller's code. #! @param results Where the caller's code lists the words of the results, or #! `none`. #! @param dst Where a result held in memory is written. def interpreter::enter(&mut this, callee: u32, held: &routine, next: *mut u64, back: u64, site: u64, results: u64, dst: u64): void { if held.zeroed > held.cells { memset((next as u64 + held.cells) as *mut u8, 0, held.zeroed - held.cells); } # Running with its memory checked, what a frame before left never written is # not this one's. if this.framed_unwritten { this.wrote(next as u64, held.frame); } this.calls.append(call_record { function = callee, fp = next, back = back, site = site, results = results, dst = dst }); this.top = next as u64 + held.frame; } #! Ends the call the machine began, leaving those beneath it. #! @param this The machine. #! @param base How many calls are beneath it. def interpreter::leave(&mut this, base: u64): void { this.calls.truncate(); if base == 0 { this.top = this.stack as u64; return; } val under: call_record = this.calls.at(base - 1); this.top = under.fp as u64 + this.routines[under.function].frame; }