vile / scheme / machine_fusion.v
71 lines · 70 sloc · 2.97 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3// The ordinary loop can consume two pure operand loads with their
4// primitive call. Original bytecode stays intact; guarded or rejected operands
5// execute normally, preserving their individual errors and source locations.
6@[direct_array_access]
7fn (mut m Machine) fuse_primitive_operands(vm &VM, callable Value) bool {
8 // Near a step limit, execute the original sequence so a denied operand or
9 // application keeps its exact position and its ordering with other errors.
10 if vm.options.max_steps > 0 && vm.options.max_steps - m.steps < 4 { return false }
11 p := callable.procedure_pointer()
12 if p.mode != .primitive || !isnil(p.env) || voidptr(p.owner) != voidptr(vm.owner)
13 || (int(p.primitive) > int(Primitive.greater_equal)
14 && p.primitive !in [.quotient, .remainder, .modulo, .vector_ref]) {
15 return false
16 }
17 if m.ip + 2 >= m.function.instructions.len { return false }
18 call := m.function.instructions[m.ip + 2]
19 if call.op !in [.call, .tail_call] || call.a != 2 { return false }
20 mut operands := [2]Value{}
21 for i in 0 .. 2 {
22 ins := m.function.instructions[m.ip + i]
23 operands[i] = match ins.op {
24 .constant { m.function.constants[int(ins.a)] }
25 .local { m.env.ancestor(int(ins.a)).slots[int(ins.b)] }
26 .global {
27 slot := m.function.global_slots[int(ins.a)]
28 if slot < 0 { return false }
29 vm.globals[slot]
30 }
31 else { return false }
32 }
33 if operands[i].tag == .uninitialized { return false }
34 }
35 x, y := operands[0], operands[1]
36 mut value := Value{}
37 if x.tag == .integer && y.tag == .integer {
38 if int(p.primitive) <= int(Primitive.greater_equal) {
39 value = small_integer_call(p.primitive, x.whole(), y.whole())
40 if value.tag == .unspecified { return false }
41 } else if p.primitive in [.quotient, .remainder, .modulo] {
42 xv, yv := x.whole(), y.whole()
43 if yv == 0 || (p.primitive == .quotient && xv == exact_min && yv == -1) { return false }
44 value = small_integer_division(p.primitive, xv, yv, false, xv < 0, yv < 0) or { return false }
45 } else { return false }
46 } else if p.primitive == .vector_ref && x.tag == .vector && y.tag == .integer {
47 index := y.whole()
48 items := x.vector_pointer().items
49 if index < 0 || index >= items.len { return false }
50 value = unsafe { (&Value(items.data))[index] }
51 } else { return false }
52 // The global load was already charged. Preserve both operand loads, the
53 // call instruction and its application, even with no instruction limit.
54 m.steps += 4
55 m.ip += 3
56 m.origin = call.origin
57 if call.op == .tail_call {
58 m.finish_call(value)
59 } else if m.ip < m.function.instructions.len && m.function.instructions[m.ip].op == .jump_false
60 && (vm.options.max_steps == 0 || m.steps < vm.options.max_steps) {
61 // A conditional consumes this result immediately. Preserve its separate
62 // virtual charge and origin, without a temporary push/pop or dispatch.
63 branch := m.function.instructions[m.ip]
64 m.steps++
65 m.origin = branch.origin
66 m.ip = if value.is_true() { m.ip + 1 } else { int(branch.a) }
67 } else {
68 m.push(value)
69 }
70 return true
71}
72