diff options
Diffstat (limited to 'html/020run.cc.html')
-rw-r--r-- | html/020run.cc.html | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/html/020run.cc.html b/html/020run.cc.html new file mode 100644 index 00000000..9f16cdce --- /dev/null +++ b/html/020run.cc.html @@ -0,0 +1,246 @@ +<!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>~/Desktop/s/mu/020run.cc.html</title> +<meta name="Generator" content="Vim/7.4"> +<meta name="plugin-version" content="vim7.4_v1"> +<meta name="syntax" content="cpp"> +<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy="> +<meta name="colorscheme" content="minimal"> +<style type="text/css"> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #d0d0d0; background-color: #000000; } +body { font-family: monospace; color: #d0d0d0; background-color: #000000; } +* { font-size: 1em; } +.cSpecial { color: #008000; } +.CommentedCode { color: #6c6c6c; } +.SalientComment { color: #00ffff; } +.Constant { color: #008080; } +.Comment { color: #8080ff; } +.Delimiter { color: #c000c0; } +.Special { color: #ff6060; } +.Identifier { color: #008080; } +.traceContains { color: #008000; } +.traceAbsent { color: #c00000; } +--> +</style> + +<script type='text/javascript'> +<!-- + +--> +</script> +</head> +<body> +<pre id='vimCodeElement'> +<span class="Comment">//: Phase 3: Start running a loaded and transformed recipe.</span> +<span class="Comment">//:</span> +<span class="Comment">//: So far we've seen recipes as lists of instructions, and instructions point</span> +<span class="Comment">//: at other recipes. To kick things off mu needs to know how to run certain</span> +<span class="Comment">//: 'primitive' recipes. That will then give the ability to run recipes</span> +<span class="Comment">//: containing these primitives.</span> +<span class="Comment">//:</span> +<span class="Comment">//: This layer defines a skeleton with just two primitive recipes: IDLE which</span> +<span class="Comment">//: does nothing, and COPY, which can copy numbers from one memory location to</span> +<span class="Comment">//: another. Later layers will add more primitives.</span> + +<span class="Delimiter">:(scenario copy_literal)</span> +recipe main [ + <span class="Constant">1</span>:integer<span class="Special"> <- </span>copy <span class="Constant">23</span>:literal +] +<span class="traceContains">+run: instruction main/0</span> +<span class="traceContains">+run: ingredient 0 is 23</span> +<span class="traceContains">+mem: storing 23 in location 1</span> + +<span class="Delimiter">:(scenario copy)</span> +recipe main [ + <span class="Constant">1</span>:integer<span class="Special"> <- </span>copy <span class="Constant">23</span>:literal + <span class="Constant">2</span>:integer<span class="Special"> <- </span>copy <span class="Constant">1</span>:integer +] +<span class="traceContains">+run: instruction main/1</span> +<span class="traceContains">+run: ingredient 0 is 1</span> +<span class="traceContains">+mem: location 1 is 23</span> +<span class="traceContains">+mem: storing 23 in location 2</span> + +<span class="Delimiter">:(before "End Types")</span> +<span class="Comment">// Book-keeping while running a recipe.</span> +<span class="Comment">//: Later layers will change this.</span> +struct routine <span class="Delimiter">{</span> + recipe_number running_recipe<span class="Delimiter">;</span> + index_t running_step_index<span class="Delimiter">;</span> + routine<span class="Delimiter">(</span>recipe_number r<span class="Delimiter">)</span> :running_recipe<span class="Delimiter">(</span>r<span class="Delimiter">),</span> running_step_index<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{}</span> + bool completed<span class="Delimiter">()</span> const<span class="Delimiter">;</span> +<span class="Delimiter">};</span> + +<span class="Delimiter">:(before "End Globals")</span> +routine* Current_routine = <span class="Constant">NULL</span><span class="Delimiter">;</span> + +<span class="Delimiter">:(code)</span> +void run<span class="Delimiter">(</span>recipe_number r<span class="Delimiter">)</span> <span class="Delimiter">{</span> + routine rr<span class="Delimiter">(</span>r<span class="Delimiter">);</span> + Current_routine = &rr<span class="Delimiter">;</span> + run_current_routine<span class="Delimiter">();</span> +<span class="Delimiter">}</span> + +void run_current_routine<span class="Delimiter">()</span> +<span class="Delimiter">{</span> <span class="Comment">// curly on a separate line, because later layers will modify header</span> + while <span class="Delimiter">(</span>!Current_routine<span class="Delimiter">-></span>completed<span class="Delimiter">())</span> <span class="Comment">// later layers will modify condition</span> + <span class="Delimiter">{</span> + <span class="Comment">// Running One Instruction.</span> + if <span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>is_label<span class="Delimiter">)</span> <span class="Delimiter">{</span> ++current_step_index<span class="Delimiter">();</span> <span class="Identifier">continue</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> + trace<span class="Delimiter">(</span><span class="Constant">"run"</span><span class="Delimiter">)</span> << <span class="Constant">"instruction "</span> << current_recipe_name<span class="Delimiter">()</span> << <span class="Constant">'/'</span> << current_step_index<span class="Delimiter">();</span> + trace<span class="Delimiter">(</span><span class="Constant">"run"</span><span class="Delimiter">)</span> << current_instruction<span class="Delimiter">().</span>to_string<span class="Delimiter">();</span> +<span class="CommentedCode">//? cout << "operation " << current_instruction().operation << '\n'; //? 3</span> + switch <span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>operation<span class="Delimiter">)</span> <span class="Delimiter">{</span> + <span class="Comment">// Primitive Recipe Implementations</span> + case COPY: <span class="Delimiter">{</span> + trace<span class="Delimiter">(</span><span class="Constant">"run"</span><span class="Delimiter">)</span> << <span class="Constant">"ingredient 0 is "</span> << current_instruction<span class="Delimiter">().</span>ingredients[<span class="Constant">0</span>]<span class="Delimiter">.</span>name<span class="Delimiter">;</span> + vector<long long int> data = read_memory<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients[<span class="Constant">0</span>]<span class="Delimiter">);</span> + write_memory<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>products[<span class="Constant">0</span>]<span class="Delimiter">,</span> data<span class="Delimiter">);</span> + <span class="Identifier">break</span><span class="Delimiter">;</span> + <span class="Delimiter">}</span> + <span class="Comment">// End Primitive Recipe Implementations</span> + default: <span class="Delimiter">{</span> + cout << <span class="Constant">"not a primitive op: "</span> << current_instruction<span class="Delimiter">().</span>operation << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span> + <span class="Delimiter">}</span> + <span class="Delimiter">}</span> + ++current_step_index<span class="Delimiter">();</span> + <span class="Delimiter">}</span> +<span class="Delimiter">}</span> + +<span class="Comment">//: Some helpers.</span> +<span class="Comment">//: We'll need to override these later as we change the definition of routine.</span> +<span class="Comment">//: Important that they return referrences into the routine.</span> + +inline index_t& current_step_index<span class="Delimiter">()</span> <span class="Delimiter">{</span> + <span class="Identifier">return</span> Current_routine<span class="Delimiter">-></span>running_step_index<span class="Delimiter">;</span> +<span class="Delimiter">}</span> + +inline const string& current_recipe_name<span class="Delimiter">()</span> <span class="Delimiter">{</span> + <span class="Identifier">return</span> Recipe[Current_routine<span class="Delimiter">-></span>running_recipe]<span class="Delimiter">.</span>name<span class="Delimiter">;</span> +<span class="Delimiter">}</span> + +inline const instruction& current_instruction<span class="Delimiter">()</span> <span class="Delimiter">{</span> + <span class="Identifier">return</span> Recipe[Current_routine<span class="Delimiter">-></span>running_recipe]<span class="Delimiter">.</span>steps[Current_routine<span class="Delimiter">-></span>running_step_index]<span class="Delimiter">;</span> +<span class="Delimiter">}</span> + +inline bool routine::completed<span class="Delimiter">()</span> const <span class="Delimiter">{</span> + <span class="Identifier">return</span> running_step_index >= Recipe[running_recipe]<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>size<span class="Delimiter">();</span> +<span class="Delimiter">}</span> + +<span class="Delimiter">:(before "End Commandline Parsing")</span> +if <span class="Delimiter">(</span>argc > <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> + for <span class="Delimiter">(</span>int i = <span class="Constant">1</span><span class="Delimiter">;</span> i < argc<span class="Delimiter">;</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span> + load_permanently<span class="Delimiter">(</span>argv[i]<span class="Delimiter">);</span> + <span class="Delimiter">}</span> +<span class="Delimiter">}</span> + +<span class="Delimiter">:(before "End Main")</span> +if <span class="Delimiter">(</span>!Run_tests<span class="Delimiter">)</span> <span class="Delimiter">{</span> + setup<span class="Delimiter">();</span> + Trace_stream = new trace_stream<span class="Delimiter">;</span> +<span class="CommentedCode">//? Trace_stream->dump_layer = "all"; //? 2</span> + transform_all<span class="Delimiter">();</span> + recipe_number r = Recipe_number[string<span class="Delimiter">(</span><span class="Constant">"main"</span><span class="Delimiter">)</span>]<span class="Delimiter">;</span> +<span class="CommentedCode">//? Trace_stream->dump_layer = "all"; //? 1</span> + if <span class="Delimiter">(</span>r<span class="Delimiter">)</span> run<span class="Delimiter">(</span>r<span class="Delimiter">);</span> +<span class="CommentedCode">//? dump_memory(); //? 1</span> + teardown<span class="Delimiter">();</span> +<span class="Delimiter">}</span> + +<span class="Delimiter">:(code)</span> +void load_permanently<span class="Delimiter">(</span>string filename<span class="Delimiter">)</span> <span class="Delimiter">{</span> + ifstream fin<span class="Delimiter">(</span>filename<span class="Delimiter">.</span>c_str<span class="Delimiter">());</span> + if <span class="Delimiter">(</span>!fin<span class="Delimiter">)</span> <span class="Delimiter">{</span> + raise << <span class="Constant">"no such file "</span> << filename << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span> + <span class="Identifier">return</span><span class="Delimiter">;</span> + <span class="Delimiter">}</span> + fin >> std::noskipws<span class="Delimiter">;</span> + load<span class="Delimiter">(</span>fin<span class="Delimiter">);</span> + transform_all<span class="Delimiter">();</span> + fin<span class="Delimiter">.</span>close<span class="Delimiter">();</span> + <span class="Comment">// freeze everything so it doesn't get cleared by tests</span> + recently_added_recipes<span class="Delimiter">.</span>clear<span class="Delimiter">();</span> + recently_added_types<span class="Delimiter">.</span>clear<span class="Delimiter">();</span> +<span class="Delimiter">}</span> + +<span class="SalientComment">//:: On startup, load everything in core.mu</span> +<span class="Delimiter">:(before "End Load Recipes")</span> +load_permanently<span class="Delimiter">(</span><span class="Constant">"core.mu"</span><span class="Delimiter">);</span> + +<span class="Delimiter">:(code)</span> +<span class="Comment">// helper for tests</span> +void run<span class="Delimiter">(</span>string form<span class="Delimiter">)</span> <span class="Delimiter">{</span> + vector<recipe_number> tmp = load<span class="Delimiter">(</span>form<span class="Delimiter">);</span> + if <span class="Delimiter">(</span>tmp<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span><span class="Delimiter">;</span> + transform_all<span class="Delimiter">();</span> + run<span class="Delimiter">(</span>tmp<span class="Delimiter">.</span>front<span class="Delimiter">());</span> +<span class="Delimiter">}</span> + +<span class="SalientComment">//:: Reading from memory, writing to memory.</span> + +vector<long long int> read_memory<span class="Delimiter">(</span>reagent x<span class="Delimiter">)</span> <span class="Delimiter">{</span> +<span class="CommentedCode">//? cout << "read_memory: " << x.to_string() << '\n'; //? 1</span> + vector<long long int> result<span class="Delimiter">;</span> + if <span class="Delimiter">(</span>isa_literal<span class="Delimiter">(</span>x<span class="Delimiter">))</span> <span class="Delimiter">{</span> + result<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>x<span class="Delimiter">.</span>value<span class="Delimiter">);</span> + <span class="Identifier">return</span> result<span class="Delimiter">;</span> + <span class="Delimiter">}</span> + index_t base = x<span class="Delimiter">.</span>value<span class="Delimiter">;</span> + size_t size = size_of<span class="Delimiter">(</span>x<span class="Delimiter">);</span> + for <span class="Delimiter">(</span>index_t offset = <span class="Constant">0</span><span class="Delimiter">;</span> offset < size<span class="Delimiter">;</span> ++offset<span class="Delimiter">)</span> <span class="Delimiter">{</span> + int val = Memory[base+offset]<span class="Delimiter">;</span> + trace<span class="Delimiter">(</span><span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"location "</span> << base+offset << <span class="Constant">" is "</span> << val<span class="Delimiter">;</span> + result<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>val<span class="Delimiter">);</span> + <span class="Delimiter">}</span> + <span class="Identifier">return</span> result<span class="Delimiter">;</span> +<span class="Delimiter">}</span> + +void write_memory<span class="Delimiter">(</span>reagent x<span class="Delimiter">,</span> vector<long long int> data<span class="Delimiter">)</span> <span class="Delimiter">{</span> + if <span class="Delimiter">(</span>is_dummy<span class="Delimiter">(</span>x<span class="Delimiter">))</span> <span class="Identifier">return</span><span class="Delimiter">;</span> + index_t base = x<span class="Delimiter">.</span>value<span class="Delimiter">;</span> + if <span class="Delimiter">(</span>size_of<span class="Delimiter">(</span>x<span class="Delimiter">)</span> != data<span class="Delimiter">.</span>size<span class="Delimiter">())</span> + raise << <span class="Constant">"size mismatch in storing to "</span> << x<span class="Delimiter">.</span>to_string<span class="Delimiter">()</span> << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span> + for <span class="Delimiter">(</span>index_t offset = <span class="Constant">0</span><span class="Delimiter">;</span> offset < data<span class="Delimiter">.</span>size<span class="Delimiter">();</span> ++offset<span class="Delimiter">)</span> <span class="Delimiter">{</span> + trace<span class="Delimiter">(</span><span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"storing "</span> << data[offset] << <span class="Constant">" in location "</span> << base+offset<span class="Delimiter">;</span> + Memory[base+offset] = data[offset]<span class="Delimiter">;</span> + <span class="Delimiter">}</span> +<span class="Delimiter">}</span> + +<span class="Delimiter">:(code)</span> +size_t size_of<span class="Delimiter">(</span>const reagent& r<span class="Delimiter">)</span> <span class="Delimiter">{</span> + <span class="Identifier">return</span> size_of<span class="Delimiter">(</span>r<span class="Delimiter">.</span>types<span class="Delimiter">);</span> +<span class="Delimiter">}</span> +size_t size_of<span class="Delimiter">(</span>const vector<type_number>& types<span class="Delimiter">)</span> <span class="Delimiter">{</span> + <span class="Comment">// End size_of(types) Cases</span> + <span class="Identifier">return</span> <span class="Constant">1</span><span class="Delimiter">;</span> +<span class="Delimiter">}</span> + +bool is_dummy<span class="Delimiter">(</span>const reagent& x<span class="Delimiter">)</span> <span class="Delimiter">{</span> + <span class="Identifier">return</span> x<span class="Delimiter">.</span>name == <span class="Constant">"_"</span><span class="Delimiter">;</span> +<span class="Delimiter">}</span> + +bool isa_literal<span class="Delimiter">(</span>const reagent& r<span class="Delimiter">)</span> <span class="Delimiter">{</span> + <span class="Identifier">return</span> r<span class="Delimiter">.</span>types<span class="Delimiter">.</span>size<span class="Delimiter">()</span> == <span class="Constant">1</span> && r<span class="Delimiter">.</span>types[<span class="Constant">0</span>] == <span class="Constant">0</span><span class="Delimiter">;</span> +<span class="Delimiter">}</span> + +<span class="Delimiter">:(scenario run_label)</span> +recipe main [ + +foo + <span class="Constant">1</span>:integer<span class="Special"> <- </span>copy <span class="Constant">23</span>:literal + <span class="Constant">2</span>:integer<span class="Special"> <- </span>copy <span class="Constant">1</span>:integer +] +<span class="traceContains">+run: instruction main/1</span> +<span class="traceContains">+run: instruction main/2</span> +<span class="traceAbsent">-run: instruction main/0</span> + +<span class="Delimiter">:(scenario run_dummy)</span> +recipe main [ + _<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal +] +<span class="traceContains">+run: instruction main/0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> |