about summary refs log tree commit diff stats
path: root/024jump.cc
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2018-06-17 16:23:14 -0700
committerKartik Agaram <vc@akkartik.com>2018-06-17 16:23:14 -0700
commitf5ee2463d00bdf1810c615555e91d993aa1032c3 (patch)
tree29d9b8500b15acdfcf9f0ecebaad657ab47b9179 /024jump.cc
parent92a3d0824b37e564f3d5bb7e042f97f991f25416 (diff)
downloadmu-f5ee2463d00bdf1810c615555e91d993aa1032c3.tar.gz
4264
Undo the relayout of 4259.
Diffstat (limited to '024jump.cc')
-rw-r--r--024jump.cc183
1 files changed, 183 insertions, 0 deletions
diff --git a/024jump.cc b/024jump.cc
new file mode 100644
index 00000000..37011290
--- /dev/null
+++ b/024jump.cc
@@ -0,0 +1,183 @@
+//: Jump primitives
+
+:(scenario jump_can_skip_instructions)
+def main [
+  jump 1:offset
+  1:num <- copy 1
+]
++run: jump {1: "offset"}
+-run: {1: "number"} <- copy {1: "literal"}
+-mem: storing 1 in location 1
+
+:(before "End Primitive Recipe Declarations")
+JUMP,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "jump", JUMP);
+:(before "End Primitive Recipe Checks")
+case JUMP: {
+  if (SIZE(inst.ingredients) != 1) {
+    raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' should get exactly one ingredient\n" << end();
+    break;
+  }
+  if (!is_literal(inst.ingredients.at(0))) {
+    raise << maybe(get(Recipe, r).name) << "first ingredient of '" << to_original_string(inst) << "' should be a label or offset, but '" << inst.ingredients.at(0).name << "' has type '" << names_to_string_without_quotes(inst.ingredients.at(0).type) << "'\n" << end();
+    break;
+  }
+  if (!inst.products.empty()) {
+    raise << maybe(get(Recipe, r).name) << "'jump' instructions write no products\n" << end();
+    break;
+  }
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case JUMP: {
+  assert(current_instruction().ingredients.at(0).initialized);
+  current_step_index() += ingredients.at(0).at(0)+1;
+  trace(9998, "run") << "jumping to instruction " << current_step_index() << end();
+  // skip rest of this instruction
+  write_products = false;
+  fall_through_to_next_instruction = false;
+  break;
+}
+
+//: special type to designate jump targets
+:(before "End Mu Types Initialization")
+put(Type_ordinal, "offset", 0);
+
+:(scenario jump_backward)
+def main [
+  jump 1:offset  # 0 -+
+  jump 3:offset  #    |   +-+ 1
+                 #   \/  /\ |
+  jump -2:offset #  2 +-->+ |
+]                #         \/ 3
++run: jump {1: "offset"}
++run: jump {-2: "offset"}
++run: jump {3: "offset"}
+
+:(scenario jump_takes_no_products)
+% Hide_errors = true;
+def main [
+  1:num <- jump 1
+]
++error: main: 'jump' instructions write no products
+
+:(before "End Primitive Recipe Declarations")
+JUMP_IF,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "jump-if", JUMP_IF);
+:(before "End Primitive Recipe Checks")
+case JUMP_IF: {
+  if (SIZE(inst.ingredients) != 2) {
+    raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' should get exactly two ingredients\n" << end();
+    break;
+  }
+  if (!is_mu_scalar(inst.ingredients.at(0))) {
+    raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' requires a boolean for its first ingredient, but '" << inst.ingredients.at(0).name << "' has type '" << names_to_string_without_quotes(inst.ingredients.at(0).type) << "'\n" << end();
+    break;
+  }
+  if (!is_literal(inst.ingredients.at(1))) {
+    raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' requires a label or offset for its second ingredient, but '" << inst.ingredients.at(1).name << "' has type '" << names_to_string_without_quotes(inst.ingredients.at(1).type) << "'\n" << end();
+    break;
+  }
+  if (!inst.products.empty()) {
+    raise << maybe(get(Recipe, r).name) << "'jump-if' instructions write no products\n" << end();
+    break;
+  }
+  // End JUMP_IF Checks
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case JUMP_IF: {
+  assert(current_instruction().ingredients.at(1).initialized);
+  if (!ingredients.at(0).at(0)) {
+    trace(9998, "run") << "jump-if fell through" << end();
+    break;
+  }
+  current_step_index() += ingredients.at(1).at(0)+1;
+  trace(9998, "run") << "jumping to instruction " << current_step_index() << end();
+  // skip rest of this instruction
+  write_products = false;
+  fall_through_to_next_instruction = false;
+  break;
+}
+
+:(scenario jump_if)
+def main [
+  jump-if 999, 1:offset
+  123:num <- copy 1
+]
++run: jump-if {999: "literal"}, {1: "offset"}
++run: jumping to instruction 2
+-run: {1: "number"} <- copy {1: "literal"}
+-mem: storing 1 in location 123
+
+:(scenario jump_if_fallthrough)
+def main [
+  jump-if 0, 1:offset
+  123:num <- copy 1
+]
++run: jump-if {0: "literal"}, {1: "offset"}
++run: jump-if fell through
++run: {123: "number"} <- copy {1: "literal"}
++mem: storing 1 in location 123
+
+:(before "End Primitive Recipe Declarations")
+JUMP_UNLESS,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "jump-unless", JUMP_UNLESS);
+:(before "End Primitive Recipe Checks")
+case JUMP_UNLESS: {
+  if (SIZE(inst.ingredients) != 2) {
+    raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' should get exactly two ingredients\n" << end();
+    break;
+  }
+  if (!is_mu_scalar(inst.ingredients.at(0))) {
+    raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' requires a boolean for its first ingredient, but '" << inst.ingredients.at(0).name << "' has type '" << names_to_string_without_quotes(inst.ingredients.at(0).type) << "'\n" << end();
+    break;
+  }
+  if (!is_literal(inst.ingredients.at(1))) {
+    raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' requires a label or offset for its second ingredient, but '" << inst.ingredients.at(1).name << "' has type '" << names_to_string_without_quotes(inst.ingredients.at(1).type) << "'\n" << end();
+    break;
+  }
+  if (!inst.products.empty()) {
+    raise << maybe(get(Recipe, r).name) << "'jump' instructions write no products\n" << end();
+    break;
+  }
+  // End JUMP_UNLESS Checks
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case JUMP_UNLESS: {
+  assert(current_instruction().ingredients.at(1).initialized);
+  if (ingredients.at(0).at(0)) {
+    trace(9998, "run") << "jump-unless fell through" << end();
+    break;
+  }
+  current_step_index() += ingredients.at(1).at(0)+1;
+  trace(9998, "run") << "jumping to instruction " << current_step_index() << end();
+  // skip rest of this instruction
+  write_products = false;
+  fall_through_to_next_instruction = false;
+  break;
+}
+
+:(scenario jump_unless)
+def main [
+  jump-unless 0, 1:offset
+  123:num <- copy 1
+]
++run: jump-unless {0: "literal"}, {1: "offset"}
++run: jumping to instruction 2
+-run: {123: "number"} <- copy {1: "literal"}
+-mem: storing 1 in location 123
+
+:(scenario jump_unless_fallthrough)
+def main [
+  jump-unless 999, 1:offset
+  123:num <- copy 1
+]
++run: jump-unless {999: "literal"}, {1: "offset"}
++run: jump-unless fell through
++run: {123: "number"} <- copy {1: "literal"}
++mem: storing 1 in location 123