|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 041jump_target.cc</title>
<meta name="Generator" content="Vim/7.3">
<meta name="plugin-version" content="vim7.3_v6">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css">
<style type="text/css">
<!--
pre { font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-family: monospace; color: #eeeeee; background-color: #080808; }
.traceContains { color: #008000; }
.Identifier { color: #804000; }
.cSpecial { color: #008000; }
.Normal { color: #eeeeee; background-color: #080808; }
.traceAbsent { color: #c00000; }
.Special { color: #ff6060; }
.Constant { color: #00a0a0; }
.Delimiter { color: #a04060; }
.Comment { color: #9090ff; }
-->
</style>
</head>
<body>
<pre>
<span class="Comment">//: Support jumps to special labels called 'targets'. Targets must be in the</span>
<span class="Comment">//: same recipe as the jump, and must be unique in that recipe. Targets always</span>
<span class="Comment">//: start with a '+'.</span>
<span class="Comment">//:</span>
<span class="Comment">//: We'll also treat 'break' and 'loop' as jumps. The choice of name is</span>
<span class="Comment">//: just documentation about intent; use 'break' to indicate you're exiting</span>
<span class="Comment">//: one or more loop nests, and 'loop' to indicate you're skipping to the next</span>
<span class="Comment">//: iteration of some containing loop nest.</span>
<span class="Delimiter">:(scenario jump_to_label)</span>
def main [
jump +target:label
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
+target
]
<span class="traceAbsent">-mem: storing 0 in location 1</span>
<span class="Delimiter">:(before "End Mu Types Initialization")</span>
put<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">"label"</span><span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Instruction Modifying Transforms")</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>transform_labels<span class="Delimiter">);</span> <span class="Comment">// idempotent</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> transform_labels<span class="Delimiter">(</span><span class="Normal">const</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
map<string<span class="Delimiter">,</span> <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>> offset<span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> instruction& inst = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!inst<span class="Delimiter">.</span>label<span class="Delimiter">.</span>empty<span class="Delimiter">()</span> && inst<span class="Delimiter">.</span>label<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> == <span class="Constant">'+'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>offset<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>label<span class="Delimiter">))</span> <span class="Delimiter">{</span>
put<span class="Delimiter">(</span>offset<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>label<span class="Delimiter">,</span> i<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">else</span> <span class="Delimiter">{</span>
raise << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"duplicate label '"</span> << inst<span class="Delimiter">.</span>label << <span class="Constant">"'"</span> << end<span class="Delimiter">();</span>
<span class="Comment">// have all jumps skip some random but noticeable and deterministic amount of code</span>
put<span class="Delimiter">(</span>offset<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>label<span class="Delimiter">,</span> <span class="Constant">9999</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
instruction& inst = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name == <span class="Constant">"jump"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
raise << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"'jump' expects an ingredient but got none</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
replace_offset<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">),</span> offset<span class="Delimiter">,</span> i<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name == <span class="Constant">"jump-if"</span> || inst<span class="Delimiter">.</span>name == <span class="Constant">"jump-unless"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span> < <span class="Constant">2</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"'"</span> << inst<span class="Delimiter">.</span>name << <span class="Constant">"' expects 2 ingredients but got "</span> << SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span> << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
replace_offset<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">),</span> offset<span class="Delimiter">,</span> i<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">((</span>inst<span class="Delimiter">.</span>name == <span class="Constant">"loop"</span> || inst<span class="Delimiter">.</span>name == <span class="Constant">"break"</span><span class="Delimiter">)</span>
&& SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span> >= <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
replace_offset<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">),</span> offset<span class="Delimiter">,</span> i<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">((</span>inst<span class="Delimiter">.</span>name == <span class="Constant">"loop-if"</span> || inst<span class="Delimiter">.</span>name == <span class="Constant">"loop-unless"</span>
|| inst<span class="Delimiter">.</span>name == <span class="Constant">"break-if"</span> || inst<span class="Delimiter">.</span>name == <span class="Constant">"break-unless"</span><span class="Delimiter">)</span>
&& SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span> >= <span class="Constant">2</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
replace_offset<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">),</span> offset<span class="Delimiter">,</span> i<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> replace_offset<span class="Delimiter">(</span>reagent& x<span class="Delimiter">,</span> <span class="Comment">/*</span><span class="Comment">const</span><span class="Comment">*/</span> map<string<span class="Delimiter">,</span> <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>>& offset<span class="Delimiter">,</span> <span class="Normal">const</span> <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> current_offset<span class="Delimiter">,</span> <span class="Normal">const</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!is_literal<span class="Delimiter">(</span>x<span class="Delimiter">))</span> <span class="Delimiter">{</span>
raise << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"jump target must be offset or label but is "</span> << x<span class="Delimiter">.</span>original_string << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
x<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span> <span class="Comment">// no jump by default</span>
<span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>x<span class="Delimiter">.</span>initialized<span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>is_integer<span class="Delimiter">(</span>x<span class="Delimiter">.</span>name<span class="Delimiter">))</span> <span class="Identifier">return</span><span class="Delimiter">;</span> <span class="Comment">// non-labels will be handled like other number operands</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!is_jump_target<span class="Delimiter">(</span>x<span class="Delimiter">.</span>name<span class="Delimiter">))</span> <span class="Delimiter">{</span>
raise << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"can't jump to label "</span> << x<span class="Delimiter">.</span>name << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
x<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span> <span class="Comment">// no jump by default</span>
<span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>offset<span class="Delimiter">,</span> x<span class="Delimiter">.</span>name<span class="Delimiter">))</span> <span class="Delimiter">{</span>
raise << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"can't find label "</span> << x<span class="Delimiter">.</span>name << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
x<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span> <span class="Comment">// no jump by default</span>
<span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
x<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span>get<span class="Delimiter">(</span>offset<span class="Delimiter">,</span> x<span class="Delimiter">.</span>name<span class="Delimiter">)</span> - current_offset<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">bool</span> is_jump_target<span class="Delimiter">(</span>string label<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> label<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> == <span class="Constant">'+'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario break_to_label)</span>
def main [
<span class="Delimiter">{</span>
<span class="Delimiter">{</span>
<span class="Identifier">break</span> +target:label
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
+target
]
<span class="traceAbsent">-mem: storing 0 in location 1</span>
<span class="Delimiter">:(scenario jump_if_to_label)</span>
def main [
<span class="Delimiter">{</span>
<span class="Delimiter">{</span>
jump-<span class="Normal">if</span> <span class="Constant">1</span><span class="Delimiter">,</span> +target:label
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
+target
]
<span class="traceAbsent">-mem: storing 0 in location 1</span>
<span class="Delimiter">:(scenario loop_unless_to_label)</span>
def main [
<span class="Delimiter">{</span>
<span class="Delimiter">{</span>
loop-unless <span class="Constant">0</span><span class="Delimiter">,</span> +target:label <span class="Comment"># loop/break with a label don't care about braces</span>
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
+target
]
<span class="traceAbsent">-mem: storing 0 in location 1</span>
<span class="Delimiter">:(scenario jump_runs_code_after_label)</span>
def main [
<span class="Comment"># first a few lines of padding to exercise the offset computation</span>
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Constant">3</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
jump +target:label
<span class="Constant">4</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
+target
<span class="Constant">5</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+mem: storing 0 in location 5</span>
<span class="traceAbsent">-mem: storing 0 in location 4</span>
<span class="Delimiter">:(scenario jump_fails_without_target)</span>
<span class="Special">% Hide_errors = true;</span>
def main [
jump
]
<span class="traceContains">+error: main: 'jump' expects an ingredient but got none</span>
<span class="Delimiter">:(scenario jump_fails_without_target_2)</span>
<span class="Special">% Hide_errors = true;</span>
def main [
jump-<span class="Normal">if</span> <span class="Constant">1</span>/<span class="Constant">true</span>
]
<span class="traceContains">+error: main: 'jump-if' expects 2 ingredients but got 1</span>
<span class="Delimiter">:(scenario recipe_fails_on_duplicate_jump_target)</span>
<span class="Special">% Hide_errors = true;</span>
def main [
+label
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
+label
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+error: main: duplicate label '+label'</span>
<span class="Delimiter">:(scenario jump_ignores_nontarget_label)</span>
<span class="Special">% Hide_errors = true;</span>
def main [
<span class="Comment"># first a few lines of padding to exercise the offset computation</span>
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Constant">3</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
jump $target:label
<span class="Constant">4</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
$target
<span class="Constant">5</span>:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+error: main: can't jump to label $target</span>
</pre>
</body>
</html>
|