1 //: Phase 3: Start running a loaded and transformed recipe.
  2 //:
  3 //:   The process of running Mu code:
  4 //:     load -> transform -> run
  5 //:
  6 //: So far we've seen recipes as lists of instructions, and instructions point
  7 //: at other recipes. To kick things off Mu needs to know how to run certain
  8 //: 'primitive' recipes. That will then give the ability to run recipes
  9 //: containing these primitives.
 10 //:
 11 //: This layer defines a skeleton with just two primitive recipes: IDLE which
 12 //: does nothing, and COPY, which can copy numbers from one memory location to
 13 //: another. Later layers will add more primitives.
 14 
 15 :(scenario copy_literal)
 16 def main [
 17   1:num <- copy 23
 18 ]
 19 +run: {1: "number"} <- copy {23: "literal"}
 20 +mem: storing 23 in location 1
 21 
 22 :(scenario copy)
 23 def main [
 24   1:num <- copy 23
 25   2:num <- copy 1:num
 26 ]
 27 +run: {2: "number"} <- copy {1: "number"}
 28 +mem: location 1 is 23
 29 +mem: storing 23 in location 2
 30 
 31 :(scenario copy_multiple)
 32 def main [
 33   1:num, 2:num <- copy 23, 24
 34 ]
 35 +mem: storing 23 in location 1
 36 +mem: storing 24 in location 2
 37 
 38 :(before "End Types")
 39 // Book-keeping while running a recipe.
 40 //: Later layers will replace this to support running multiple routines at once.
 41 struct routine {
 42   recipe_ordinal running_recipe;
 43   int running_step_index;
 44   routine(recipe_ordinal r) :running_recipe(r), running_step_index(0) {}
 45   bool completed() const;
 46   const vector<instruction>& steps() const;
 47 };
 48 
 49 :(before "End Globals")
 50 routine* Current_routine = NULL;
 51 :(before "End Reset")
 52 Current_routine = NULL;
 53 
 54 :(code)
 55 void run(const recipe_ordinal r) {
 56   routine rr(r);
 57   Current_routine = &rr;
 58   run_current_routine();
 59   Current_routine = NULL;
 60 }
 61 
 62 void run_current_routine() {
 63   while (should_continue_running(Current_routine)) {  // beware: may modify Current_routine
 64   ¦ // Running One Instruction
 65   ¦ if (current_instruction().is_label) { ++current_step_index();  continue; }
 66   ¦ trace(Initial_callstack_depth + Trace_stream->callstack_depth, "run") << to_string(current_instruction()) << end();
 67   ¦ if (get_or_insert(Memory, 0) != 0) {
 68   ¦ ¦ raise << "something wrote to location 0; this should never happen\n" << end();
 69   ¦ ¦ put(Memory, 0, 0);
 70   ¦ }
 71   ¦ // read all ingredients from memory, each potentially spanning multiple locations
 72   ¦ vector<vector<double> > ingredients;
 73   ¦ if (should_copy_ingredients()) {
 74   ¦ ¦ for (int i = 0;  i < SIZE(current_instruction().ingredients);  ++i)
 75   ¦ ¦ ¦ ingredients.push_back(read_memory(current_instruction().ingredients.at(i)));
 76   ¦ }
 77   ¦ // instructions below will write to 'products'
 78   ¦ vector<vector<double> > products;
 79   ¦ //: This will be a large switch that later layers will often insert cases
 80   ¦ //: into. Never call 'continue' within it. Instead, we'll explicitly
 81   ¦ //: control which of the following stages after the switch we run for each
 82   ¦ //: instruction.
 83   ¦ bool write_products = true;
 84   ¦ bool fall_through_to_next_instruction = true;
 85   ¦ switch (current_instruction().operation) {
 86   ¦ ¦ // Primitive Recipe Implementations
 87   ¦ ¦ case COPY: {
 88   ¦ ¦ ¦ copy(ingredients.begin(), ingredients.end(), inserter(products, products.begin()));
 89   ¦ ¦ ¦ break;
 90   ¦ ¦ }
 91   ¦ ¦ // End Primitive Recipe Implementations
 92   ¦ ¦ default: {
 93   ¦ ¦ ¦ cout << "not a primitive op: " << current_instruction().operation << '\n';
 94   ¦ ¦ }
 95   ¦ }
 96   ¦ //: used by a later layer
 97   ¦ if (write_products) {
 98   ¦ ¦ if (SIZE(products) < SIZE(current_instruction().products)) {
 99   ¦ ¦ ¦ raise << SIZE(products) << " vs " << SIZE(current_instruction().products) << ": failed to write to all products in '" << to_original_string(current_instruction()) << "'\n" << end();
100   ¦ ¦ }
101   ¦ ¦ else {
102   ¦ ¦ ¦ for (int i = 0;  i < SIZE(current_instruction().products);  ++i) {
103   ¦ ¦ ¦ ¦ // Writing Instruction Product(i)
104   ¦ ¦ ¦ ¦ write_memory(current_instruction().products.at(i), products.at(i));
105   ¦ ¦ ¦ }
106   ¦ ¦ }
107   ¦ }
108   ¦ // End Running One Instruction
109   ¦ if (fall_through_to_next_instruction)
110   ¦ ¦ ++current_step_index();
111   }
112   stop_running_current_routine:;
113 }
114 
115 :(code)
116 //: hook replaced in a later layer
117 bool should_continue_running(const routine* current_routine) {
118   assert(current_routine == Current_routine);  // argument passed in just to make caller readable above
119   return !Current_routine->completed();
120 }
121 
122 bool should_copy_ingredients() {
123   // End should_copy_ingredients Special-cases
124   return true;
125 }
126 
127 //: Some helpers.
128 //: Important that they return references into the current routine.
129 
130 //: hook replaced in a later layer
131 int& current_step_index() {
132   return Current_routine->running_step_index;
133 }
134 
135 //: hook replaced in a later layer
136 recipe_ordinal currently_running_recipe() {
137   return Current_routine->running_recipe;
138 }
139 
140 //: hook replaced in a later layer
141 const string& current_recipe_name() {
142   return get(Recipe, Current_routine->running_recipe).name;
143 }
144 
145 //: hook replaced in a later layer
146 const recipe& current_recipe() {
147   return get(Recipe, Current_routine->running_recipe);
148 }
149 
150 //: hook replaced in a later layer
151 const instruction& current_instruction() {
152   return get(Recipe, Current_routine->running_recipe).steps.at(Current_routine->running_step_index);
153 }
154 
155 //: hook replaced in a later layer
156 bool routine::completed() const {
157   return running_step_index >= SIZE(get(Recipe, running_recipe).steps);
158 }
159 
160 //: hook replaced in a later layer
161 const vector<instruction>& routine::steps() const {
162   return get(Recipe, running_recipe).steps;
163 }
164 
165 //:: Startup flow
166 
167 //: Step 1: load all .mu files with numeric prefixes (in order)
168 :(before "End Load Recipes")
169 // Load Mu Prelude
170 //? Save_trace = true;
171 //? START_TRACING_UNTIL_END_OF_SCOPE;
172 load_file_or_directory("core.mu");
173 //? DUMP("");
174 //? exit(0);
175 
176 //: Step 2: load any .mu files provided at the commandline
177 :(before "End Commandline Parsing")
178 // Check For .mu Files
179 //? START_TRACING_UNTIL_END_OF_SCOPE
180 //? Dump_trace = true;
181 if (argc > 1) {
182   // skip argv[0]
183   ++argv;
184   --argc;
185   while (argc > 0) {
186   ¦ // ignore argv past '--'; that's commandline args for 'main'
187   ¦ if (string(*argv) == "--") break;
188   ¦ if (starts_with(*argv, "--"))
189   ¦ ¦ cerr << "treating " << *argv << " as a file rather than an option\n";
190   ¦ load_file_or_directory(*argv);
191   ¦ --argc;
192   ¦ ++argv;
193   }
194   if (Run_tests) Recipe.erase(get(Recipe_ordinal, "main"));
195 }
196 transform_all();
197 //? cerr << to_original_string(get(Type_ordinal, "editor")) << '\n';
198 //? cerr << to_original_string(get(Recipe, get(Recipe_ordinal, "event-loop"))) << '\n';
199 //? DUMP("");
200 //? exit(0);
201 if (trace_contains_errors()) return 1;
202 save_snapshots();
203 
204 //: Step 3: if we aren't running tests, locate a recipe called 'main' and
205 //: start running it.
206 :(before "End Main")
207 if (!Run_tests && contains_key(Recipe_ordinal, "main") && contains_key(Recipe, get(Recipe_ordinal, "main"))) {
208   // Running Main
209   reset();
210   if (Start_tracing) {
211   ¦ Trace_stream = new trace_stream;
212   ¦ Save_trace = true;
213   }
214   trace(2, "run") << "=== Starting to run" << end();
215   assert(Num_calls_to_transform_all == 1);
216   run_main(argc, argv);
217 }
218 :(code)
219 void run_main(int argc, char* argv[]) {
220   recipe_ordinal r = get(Recipe_ordinal, "main");
221   if (r) run(r);
222 }
223 
224 //: By default we don't maintain the trace while running main because its
225 //: overheads can grow rapidly. However, it's useful when debugging.
226 :(before "End Globals")
227 bool Start_tracing = false;
228 :(before "End Commandline Options(*arg)")
229 else if (is_equal(*arg, "--trace")) {
230   Start_tracing = true;
231 }
232 
233 :(code)
234 void cleanup_main() {
235   if (Save_trace && Trace_stream) {
236   ¦ cerr << "writing trace to 'last_run'\n";
237   ¦ ofstream fout("last_run");
238   ¦ fout << Trace_stream->readable_contents("");
239   ¦ fout.close();
240   }
241   if (Trace_stream) delete Trace_stream, Trace_stream = NULL;
242 }
243 :(before "End One-time Setup")
244 atexit(cleanup_main);
245 
246 :(code)
247 void load_file_or_directory(string filename) {
248   if (is_directory(filename)) {
249   ¦ load_all(filename);
250   ¦ return;
251   }
252   ifstream fin(filename.c_str());
253   if (!fin) {
254   ¦ cerr << "no such file '" << filename << "'\n" << end();  // don't raise, just warn. just in case it's just a name for a scenario to run.
255   ¦ return;
256   }
257   trace(9990, "load") << "=== " << filename << end();
258   load(fin);
259   fin.close();
260 }
261 
262 bool is_directory(string path) {
263   struct stat info;
264   if (stat(path.c_str(), &info)) return false;  // error
265   return info.st_mode & S_IFDIR;
266 }
267 
268 void load_all(string dir) {
269   dirent** files;
270   int num_files = scandir(dir.c_str(), &files, NULL, alphasort);
271   for (int i = 0;  i < num_files;  ++i) {
272   ¦ string curr_file = files[i]->d_name;
273   ¦ if (isdigit(curr_file.at(0)))
274   ¦ ¦ load_file_or_directory(dir+'/'+curr_file);
275   ¦ free(files[i]);
276   ¦ files[i] = NULL;
277   }
278   free(files);
279 }
280 :(before "End Includes")
281 #include <dirent.h>
282 #include <sys/stat.h>
283 
284 //:: Reading from memory, writing to memory.
285 
286 :(code)
287 vector<double> read_memory(reagent/*copy*/ x) {
288   // Begin Preprocess read_memory(x)
289   vector<double> result;
290   if (is_literal(x)) {
291   ¦ result.push_back(x.value);
292   ¦ return result;
293   }
294   // End Preprocess read_memory(x)
295   int size = size_of(x);
296   for (int offset = 0;  offset < size;  ++offset) {
297   ¦ double val = get_or_insert(Memory, x.value+offset);
298   ¦ trace(9999, "mem") << "location " << x.value+offset << " is " << no_scientific(val) << end();
299   ¦ result.push_back(val);
300   }
301   return result;
302 }
303 
304 void write_memory(reagent/*copy*/ x, const vector<double>& data) {
305   assert(Current_routine);  // run-time only
306   // Begin Preprocess write_memory(x, data)
307   if (!x.type) {
308   ¦ raise << "can't write to '" << to_string(x) << "'; no type\n" << end();
309   ¦ return;
310   }
311   if (is_dummy(x)) return;
312   if (is_literal(x)) return;
313   // End Preprocess write_memory(x, data)
314   if (x.value == 0) {
315   ¦ raise << "can't write to location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
316   ¦ return;
317   }
318   if (size_mismatch(x, data)) {
319   ¦ raise << maybe(current_recipe_name()) << "size mismatch in storing to '" << x.original_string << "' (" << size_of(x) << " vs " << SIZE(data) << ") at '" << to_original_string(current_instruction()) << "'\n" << end();
320   ¦ return;
321   }
322   // End write_memory(x) Special-cases
323   for (int offset = 0;  offset < SIZE(data);  ++offset) {
324   ¦ assert(x.value+offset > 0);
325   ¦ trace(9999, "mem") << "storing " << no_scientific(data.at(offset)) << " in location " << x.value+offset << end();
326   ¦ put(Memory, x.value+offset, data.at(offset));
327   }
328 }
329 
330 :(code)
331 int size_of(const reagent& r) {
332   if (!r.type) return 0;
333   // End size_of(reagent r) Special-cases
334   return size_of(r.type);
335 }
336 int size_of(const type_tree* type) {
337   if (!type) return 0;
338   if (type->atom) {
339   ¦ if (type->value == -1) return 1;  // error value, but we'll raise it elsewhere
340   ¦ if (type->value == 0) return 1;
341   ¦ // End size_of(type) Atom Special-cases
342   }
343   else {
344   ¦ if (!type->left->atom) {
345   ¦ ¦ raise << "invalid type " << to_string(type) << '\n' << end();
346   ¦ ¦ return 0;
347   ¦ }
348   ¦ if (type->left->value == get(Type_ordinal, "address")) return 1;
349   ¦ // End size_of(type) Non-atom Special-cases
350   }
351   // End size_of(type) Special-cases
352   return 1;
353 }
354 
355 bool size_mismatch(const reagent& x, const vector<double>& data) {
356   if (!x.type) return true;
357   // End size_mismatch(x) Special-cases
358 //?   if (size_of(x) != SIZE(data)) cerr << size_of(x) << " vs " << SIZE(data) << '\n';
359   return size_of(x) != SIZE(data);
360 }
361 
362 bool is_literal(const reagent& r) {
363   return is_literal(r.type);
364 }
365 bool is_literal(const type_tree* type) {
366   if (!type) return false;
367   if (!type->atom) return false;
368   return type->value == 0;
369 }
370 
371 bool scalar(const vector<int>& x) {
372   return SIZE(x) == 1;
373 }
374 bool scalar(const vector<double>& x) {
375   return SIZE(x) == 1;
376 }
377 
378 // helper for tests
379 void run(const string& form) {
380   vector<recipe_ordinal> tmp = load(form);
381   transform_all();
382   if (tmp.empty()) return;
383   if (trace_contains_errors()) return;
384   // if a test defines main, it probably wants to start there regardless of
385   // definition order
386   if (contains_key(Recipe, get(Recipe_ordinal, "main")))
387   ¦ run(get(Recipe_ordinal, "main"));
388   else
389   ¦ run(tmp.front());
390 }
391 
392 :(scenario run_label)
393 def main [
394   +foo
395   1:num <- copy 23
396   2:num <- copy 1:num
397 ]
398 +run: {1: "number"} <- copy {23: "literal"}
399 +run: {2: "number"} <- copy {1: "number"}
400 -run: +foo
401 
402 :(scenario run_dummy)
403 def main [
404   _ <- copy 0
405 ]
406 +run: _ <- copy {0: "literal"}
407 
408 :(scenario write_to_0_disallowed)
409 % Hide_errors = true;
410 def main [
411   0:num <- copy 34
412 ]
413 -mem: storing 34 in location 0
414 
415 //: Mu is robust to various combinations of commas and spaces. You just have
416 //: to put spaces around the '<-'.
417 
418 :(scenario comma_without_space)
419 def main [
420   1:num, 2:num <- copy 2,2
421 ]
422 +mem: storing 2 in location 1
423 
424 :(scenario space_without_comma)
425 def main [
426   1:num, 2:num <- copy 2 2
427 ]
428 +mem: storing 2 in location 1
429 
430 :(scenario comma_before_space)
431 def main [
432   1:num, 2:num <- copy 2, 2
433 ]
434 +mem: storing 2 in location 1
435 
436 :(scenario comma_after_space)
437 def main [
438   1:num, 2:num <- copy 2 ,2
439 ]
440 +mem: storing 2 in location 1
441 
442 //:: Counters for trying to understand where Mu programs are spending their
443 //:: time.
444 
445 :(before "End Globals")
446 bool Run_profiler = false;
447 // We'll key profile information by recipe_ordinal rather than name because
448 // it's more efficient, and because later layers will show more than just the
449 // name of a recipe.
450 //
451 // One drawback: if you're clearing recipes your profile will be inaccurate.
452 // So far that happens in tests, and in 'run-sandboxed' in a later layer.
453 map<recipe_ordinal, int> Instructions_running;
454 :(before "End Commandline Options(*arg)")
455 else if (is_equal(*arg, "--profile")) {
456   Run_profiler = true;
457 }
458 :(after "Running One Instruction")
459 if (Run_profiler) Instructions_running[currently_running_recipe()]++;
460 :(before "End One-time Setup")
461 atexit(dump_profile);
462 :(code)
463 void dump_profile() {
464   if (!Run_profiler) return;
465   if (Run_tests) {
466   ¦ cerr << "It's not a good idea to profile a run with tests, since tests can create conflicting recipes and mislead you. To try it anyway, comment out this check in the code.\n";
467   ¦ return;
468   }
469   ofstream fout;
470   fout.open("profile.instructions");
471   if (fout) {
472   ¦ for (map<recipe_ordinal, int>::iterator p = Instructions_running.begin();  p != Instructions_running.end();  ++p) {
473   ¦ ¦ fout << std::setw(9) << p->second << ' ' << header_label(p->first) << '\n';
474   ¦ }
475   }
476   fout.close();
477   // End dump_profile
478 }
479 
480 // overridden in a later layer
481 string header_label(const recipe_ordinal r) {
482   return get(Recipe, r).name;
483 }