mu/024jump.cc

156 lines
5.4 KiB
C++
Raw Normal View History

2015-04-18 14:50:51 +00:00
//: Jump primitives
2015-04-17 18:22:59 +00:00
:(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,
2015-02-20 01:28:25 +00:00
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "jump", JUMP);
2015-09-30 09:18:05 +00:00
:(before "End Primitive Recipe Checks")
2015-02-20 01:28:25 +00:00
case JUMP: {
2015-09-30 09:18:05 +00:00
if (SIZE(inst.ingredients) != 1) {
raise << maybe(get(Recipe, r).name) << "'" << inst.original_string << "' should get exactly one ingredient\n" << end();
break;
}
if (!is_literal(inst.ingredients.at(0))) {
raise << maybe(get(Recipe, r).name) << "first ingredient of '" << inst.original_string << "' 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;
}
2015-09-30 09:18:05 +00:00
break;
}
:(before "End Primitive Recipe Implementations")
case JUMP: {
assert(current_instruction().ingredients.at(0).initialized);
current_step_index() += ingredients.at(0).at(0)+1;
2015-10-29 19:09:23 +00:00
trace(9998, "run") << "jumping to instruction " << current_step_index() << end();
continue; // skip rest of this instruction
2015-02-20 01:28:25 +00:00
}
//: special type to designate jump targets
:(before "End Mu Types Initialization")
put(Type_ordinal, "offset", 0);
2015-02-20 01:28:25 +00:00
2015-04-24 17:19:03 +00:00
:(scenario jump_backward)
def main [
2015-03-17 05:47:28 +00:00
jump 1:offset # 0 -+
jump 3:offset # | +-+ 1
2015-03-17 05:47:28 +00:00
# \/ /\ |
jump -2:offset # 2 +-->+ |
] # \/ 3
+run: jump {1: "offset"}
+run: jump {-2: "offset"}
+run: jump {3: "offset"}
2015-02-20 02:25:25 +00:00
:(before "End Primitive Recipe Declarations")
JUMP_IF,
2015-02-20 02:25:25 +00:00
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "jump-if", JUMP_IF);
2015-09-30 09:18:05 +00:00
:(before "End Primitive Recipe Checks")
2015-02-20 02:25:25 +00:00
case JUMP_IF: {
2015-09-30 09:18:05 +00:00
if (SIZE(inst.ingredients) != 2) {
raise << maybe(get(Recipe, r).name) << "'" << inst.original_string << "' should get exactly two ingredients\n" << end();
break;
}
2015-09-30 09:18:05 +00:00
if (!is_mu_scalar(inst.ingredients.at(0))) {
raise << maybe(get(Recipe, r).name) << "'" << inst.original_string << "' 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) << "'" << inst.original_string << "' 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;
}
// End JUMP_IF Checks
2015-09-30 09:18:05 +00:00
break;
}
:(before "End Primitive Recipe Implementations")
case JUMP_IF: {
assert(current_instruction().ingredients.at(1).initialized);
if (!ingredients.at(0).at(0)) {
2015-10-29 19:09:23 +00:00
trace(9998, "run") << "jump-if fell through" << end();
2015-02-20 02:25:25 +00:00
break;
}
current_step_index() += ingredients.at(1).at(0)+1;
2015-10-29 19:09:23 +00:00
trace(9998, "run") << "jumping to instruction " << current_step_index() << end();
continue; // skip rest of this instruction
2015-02-20 02:25:25 +00:00
}
2015-04-24 17:19:03 +00:00
:(scenario jump_if)
def main [
jump-if 999, 1:offset
123:num <- copy 1
2015-02-20 02:25:25 +00:00
]
+run: jump-if {999: "literal"}, {1: "offset"}
2015-02-20 02:25:25 +00:00
+run: jumping to instruction 2
-run: {1: "number"} <- copy {1: "literal"}
-mem: storing 1 in location 123
2015-02-20 02:25:25 +00:00
2015-04-24 17:19:03 +00:00
:(scenario jump_if_fallthrough)
def main [
jump-if 0, 1:offset
123:num <- copy 1
2015-02-20 02:25:25 +00:00
]
+run: jump-if {0: "literal"}, {1: "offset"}
2015-02-20 02:25:25 +00:00
+run: jump-if fell through
+run: {123: "number"} <- copy {1: "literal"}
2015-03-24 06:59:59 +00:00
+mem: storing 1 in location 123
2015-02-20 02:25:25 +00:00
:(before "End Primitive Recipe Declarations")
JUMP_UNLESS,
2015-02-20 02:25:25 +00:00
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "jump-unless", JUMP_UNLESS);
2015-09-30 09:18:05 +00:00
:(before "End Primitive Recipe Checks")
2015-02-20 02:25:25 +00:00
case JUMP_UNLESS: {
2015-09-30 09:18:05 +00:00
if (SIZE(inst.ingredients) != 2) {
raise << maybe(get(Recipe, r).name) << "'" << inst.original_string << "' should get exactly two ingredients\n" << end();
break;
}
2015-09-30 09:18:05 +00:00
if (!is_mu_scalar(inst.ingredients.at(0))) {
raise << maybe(get(Recipe, r).name) << "'" << inst.original_string << "' 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) << "'" << inst.original_string << "' 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;
}
// End JUMP_UNLESS Checks
2015-09-30 09:18:05 +00:00
break;
}
:(before "End Primitive Recipe Implementations")
case JUMP_UNLESS: {
assert(current_instruction().ingredients.at(1).initialized);
if (ingredients.at(0).at(0)) {
2015-10-29 19:09:23 +00:00
trace(9998, "run") << "jump-unless fell through" << end();
2015-02-20 02:25:25 +00:00
break;
}
current_step_index() += ingredients.at(1).at(0)+1;
2015-10-29 19:09:23 +00:00
trace(9998, "run") << "jumping to instruction " << current_step_index() << end();
continue; // skip rest of this instruction
2015-02-20 02:25:25 +00:00
}
2015-04-24 17:19:03 +00:00
:(scenario jump_unless)
def main [
jump-unless 0, 1:offset
123:num <- copy 1
2015-02-20 02:25:25 +00:00
]
+run: jump-unless {0: "literal"}, {1: "offset"}
2015-02-20 02:25:25 +00:00
+run: jumping to instruction 2
-run: {123: "number"} <- copy {1: "literal"}
-mem: storing 1 in location 123
2015-02-20 02:25:25 +00:00
2015-04-24 17:19:03 +00:00
:(scenario jump_unless_fallthrough)
def main [
jump-unless 999, 1:offset
123:num <- copy 1
2015-02-20 02:25:25 +00:00
]
+run: jump-unless {999: "literal"}, {1: "offset"}
2015-02-20 02:25:25 +00:00
+run: jump-unless fell through
+run: {123: "number"} <- copy {1: "literal"}
2015-03-24 06:59:59 +00:00
+mem: storing 1 in location 123