1 //: Support jumps to a specific label (the 'jump target') in the same recipe.
2 //: Jump targets must be unique and unambiguous within any recipe.
4 //: The 'break' and 'loop' pseudo instructions can also take jump targets.
5 //: Which instruction you use is just documentation about intent; use 'break'
6 //: to indicate you're exiting one or more loop nests, and 'loop' to indicate
7 //: you're skipping to the next iteration of some containing loop nest.
9 //: Since they have to be unique in a recipe, not all labels can be jump
11 bool is_jump_target(const string
& label
) {
12 if (label
== "{" || label
== "}") return false;
13 // End is_jump_target Special-cases
14 return is_label_word(label
);
17 void test_jump_to_label() {
20 " jump +target:label\n"
25 CHECK_TRACE_DOESNT_CONTAIN("mem: storing 0 in location 1");
28 :(before
"End Mu Types Initialization")
29 put(Type_ordinal
, "label", 0);
31 :(before
"End Instruction Modifying Transforms")
32 Transform
.push_back(transform_labels
); // idempotent
35 void transform_labels(const recipe_ordinal r
) {
36 map
<string
, int> offset
;
37 for (int i
= 0; i
< SIZE(get(Recipe
, r
).steps
); ++i
) {
38 const instruction
& inst
= get(Recipe
, r
).steps
.at(i
);
39 if (!inst
.is_label
) continue;
40 if (is_jump_target(inst
.label
)) {
41 if (!contains_key(offset
, inst
.label
)) {
42 put(offset
, inst
.label
, i
);
45 raise
<< maybe(get(Recipe
, r
).name
) << "duplicate label '" << inst
.label
<< "'" << end();
46 // have all jumps skip some random but noticeable and deterministic amount of code
47 put(offset
, inst
.label
, 9999);
51 for (int i
= 0; i
< SIZE(get(Recipe
, r
).steps
); ++i
) {
52 instruction
& inst
= get(Recipe
, r
).steps
.at(i
);
53 if (inst
.name
== "jump") {
54 if (inst
.ingredients
.empty()) {
55 raise
<< maybe(get(Recipe
, r
).name
) << "'" << to_original_string(inst
) << "' expects an ingredient but got 0\n" << end();
58 replace_offset(inst
.ingredients
.at(0), offset
, i
, r
);
60 if (inst
.name
== "jump-if" || inst
.name
== "jump-unless") {
61 if (SIZE(inst
.ingredients
) < 2) {
62 raise
<< maybe(get(Recipe
, r
).name
) << "'" << to_original_string(inst
) << "' expects 2 ingredients but got " << SIZE(inst
.ingredients
) << '\n' << end();
65 replace_offset(inst
.ingredients
.at(1), offset
, i
, r
);
67 if ((inst
.name
== "loop" || inst
.name
== "break")
68 && SIZE(inst
.ingredients
) >= 1) {
69 replace_offset(inst
.ingredients
.at(0), offset
, i
, r
);
71 if ((inst
.name
== "loop-if" || inst
.name
== "loop-unless"
72 || inst
.name
== "break-if" || inst
.name
== "break-unless")
73 && SIZE(inst
.ingredients
) >= 2) {
74 replace_offset(inst
.ingredients
.at(1), offset
, i
, r
);
79 void replace_offset(reagent
& x
, /*const*/ map
<string
, int>& offset
, const int current_offset
, const recipe_ordinal r
) {
81 raise
<< maybe(get(Recipe
, r
).name
) << "jump target must be offset or label but is '" << x
.original_string
<< "'\n" << end();
82 x
.set_value(0); // no jump by default
85 if (x
.initialized
) return;
86 if (is_integer(x
.name
)) return; // non-labels will be handled like other number operands
87 if (!is_jump_target(x
.name
)) {
88 raise
<< maybe(get(Recipe
, r
).name
) << "can't jump to label '" << x
.name
<< "'\n" << end();
89 x
.set_value(0); // no jump by default
92 if (!contains_key(offset
, x
.name
)) {
93 raise
<< maybe(get(Recipe
, r
).name
) << "can't find label '" << x
.name
<< "'\n" << end();
94 x
.set_value(0); // no jump by default
97 x
.set_value(get(offset
, x
.name
) - current_offset
);
100 void test_break_to_label() {
105 " break +target:label\n"
112 CHECK_TRACE_DOESNT_CONTAIN("mem: storing 0 in location 1");
115 void test_jump_if_to_label() {
120 " jump-if 1, +target:label\n"
127 CHECK_TRACE_DOESNT_CONTAIN("mem: storing 0 in location 1");
130 void test_loop_unless_to_label() {
135 " loop-unless 0, +target:label\n" // loop/break with a label don't care about braces
142 CHECK_TRACE_DOESNT_CONTAIN("mem: storing 0 in location 1");
145 void test_jump_runs_code_after_label() {
148 // first a few lines of padding to exercise the offset computation
152 " jump +target:label\n"
158 CHECK_TRACE_CONTENTS(
159 "mem: storing 0 in location 5\n"
161 CHECK_TRACE_DOESNT_CONTAIN("mem: storing 0 in location 4");
164 void test_jump_fails_without_target() {
171 CHECK_TRACE_CONTENTS(
172 "error: main: 'jump' expects an ingredient but got 0\n"
176 void test_jump_fails_without_target_2() {
183 CHECK_TRACE_CONTENTS(
184 "error: main: 'jump-if true' expects 2 ingredients but got 1\n"
188 void test_recipe_fails_on_duplicate_jump_target() {
198 CHECK_TRACE_CONTENTS(
199 "error: main: duplicate label '+label'\n"
203 void test_jump_ignores_nontarget_label() {
207 // first a few lines of padding to exercise the offset computation
211 " jump $target:label\n"
217 CHECK_TRACE_CONTENTS(
218 "error: main: can't jump to label '$target'\n"