about summary refs log tree commit diff stats
path: root/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp')
-rw-r--r--cpp/020run31
-rw-r--r--cpp/035call34
-rw-r--r--cpp/036call_ingredient24
-rw-r--r--cpp/037call_reply8
-rw-r--r--cpp/038scheduler12
5 files changed, 56 insertions, 53 deletions
diff --git a/cpp/020run b/cpp/020run
index 54a1433b..d5a161d1 100644
--- a/cpp/020run
+++ b/cpp/020run
@@ -41,22 +41,23 @@ routine* Current_routine = NULL;
 
 :(code)
 void run(recipe_number r) {
-  run(routine(r));
+  routine rr(r);
+  Current_routine = &rr;
+  run_current_routine();
 }
 
-void run(routine rr)
+void run_current_routine()
 {  // curly on a separate line, because later layers will modify header
-  Current_routine = &rr;
-  while (!done(rr))  // later layers will modify condition
+  while (!done())  // later layers will modify condition
   {
     // Running One Instruction.
-    vector<instruction>& instructions = steps(rr);
-    size_t& pc = running_at(rr);
+    vector<instruction>& instructions = steps();
+    size_t& pc = running_at();
 //?     trace("foo") << "2: " << pc << " " << &pc; //? 1
     if (instructions[pc].is_label) { ++pc; continue; }
 //?     cout << "AAA " << Trace_stream << " ^" << Trace_stream->dump_layer << "$\n"; //? 1
 //?     trace("foo") << "2.5: " << pc << " " << &pc; //? 1
-    trace("run") << "instruction " << recipe_name(rr) << '/' << pc;
+    trace("run") << "instruction " << recipe_name() << '/' << pc;
 //?     cout << "operation " << instructions[pc].operation << '\n'; //? 3
 //?     if (!instructions[pc].products.empty()) trace("foo") << "AAA product 0 is " << instructions[pc].products[0].to_string(); //? 1
     switch (instructions[pc].operation) {
@@ -82,20 +83,20 @@ void run(routine rr)
 //: We'll need to override these later as we change the definition of routine.
 //: Important that they return referrences into the routine.
 
-inline size_t& running_at(routine& rr) {
-  return rr.running_at;
+inline size_t& running_at() {
+  return Current_routine->running_at;
 }
 
-inline string recipe_name(routine& rr) {
-  return Recipe[rr.running_recipe].name;
+inline string recipe_name() {
+  return Recipe[Current_routine->running_recipe].name;
 }
 
-inline vector<instruction>& steps(routine& rr) {
-  return Recipe[rr.running_recipe].steps;
+inline vector<instruction>& steps() {
+  return Recipe[Current_routine->running_recipe].steps;
 }
 
-inline bool done(routine& rr) {
-  return running_at(rr) >= steps(rr).size();
+inline bool done() {
+  return running_at() >= steps().size();
 }
 
 :(before "End Commandline Parsing")
diff --git a/cpp/035call b/cpp/035call
index 58de7d57..28c570dd 100644
--- a/cpp/035call
+++ b/cpp/035call
@@ -52,17 +52,17 @@ struct routine {
 
 //:: now update routine's helpers
 
-:(replace{} "inline size_t& running_at(routine& rr)")
-inline size_t& running_at(routine& rr) {
-  return rr.calls.top().pc;
+:(replace{} "inline size_t& running_at()")
+inline size_t& running_at() {
+  return Current_routine->calls.top().pc;
 }
-:(replace{} "inline string recipe_name(routine& rr)")
-inline string recipe_name(routine& rr) {
-  return Recipe[rr.calls.top().running_recipe].name;
+:(replace{} "inline string recipe_name()")
+inline string recipe_name() {
+  return Recipe[Current_routine->calls.top().running_recipe].name;
 }
-:(replace{} "inline vector<instruction>& steps(routine& rr)")
-inline vector<instruction>& steps(routine& rr) {
-  return Recipe[rr.calls.top().running_recipe].steps;
+:(replace{} "inline vector<instruction>& steps()")
+inline vector<instruction>& steps() {
+  return Recipe[Current_routine->calls.top().running_recipe].steps;
 }
 
 :(replace{} "default:" following "End Primitive Recipe Implementations")
@@ -72,27 +72,27 @@ default: {
     raise << "undefined operation " << instructions[pc].operation << ": " << instructions[pc].name << '\n';
     break;
   }
-  rr.calls.push(call(instructions[pc].operation));
+  Current_routine->calls.push(call(instructions[pc].operation));
   continue;  // not done with caller; don't increment pc
 }
 
 //:: finally, we need to fix the termination conditions for the run loop
 
-:(replace{} "inline bool done(routine& rr)")
-inline bool done(routine& rr) {
-  return rr.calls.empty();
+:(replace{} "inline bool done()")
+inline bool done() {
+  return Current_routine->calls.empty();
 }
 
 :(before "Running One Instruction")
 // when we reach the end of one call, we may reach the end of the one below
 // it, and the one below that, and so on
 //? trace("foo") << "0: " << pc << " " << &pc; //? 1
-while (running_at(rr) >= steps(rr).size()) {
+while (running_at() >= steps().size()) {
 //?   trace("foo") << "pop"; //? 1
-  rr.calls.pop();
-  if (rr.calls.empty()) return;
+  Current_routine->calls.pop();
+  if (Current_routine->calls.empty()) return;
   // todo: no results returned warning
-  ++running_at(rr);
+  ++running_at();
 }
 //? trace("foo") << "1: " << pc << " " << &pc; //? 1
 
diff --git a/cpp/036call_ingredient b/cpp/036call_ingredient
index a1b80959..5c222fc4 100644
--- a/cpp/036call_ingredient
+++ b/cpp/036call_ingredient
@@ -28,12 +28,12 @@ size_t next_ingredient_to_process;
 :(replace{} "call(recipe_number r)")
 call(recipe_number r) :running_recipe(r), pc(0), next_ingredient_to_process(0) {}
 
-:(replace "rr.calls.push(call(instructions[pc].operation))" following "End Primitive Recipe Implementations")
+:(replace "Current_routine->calls.push(call(instructions[pc].operation))" following "End Primitive Recipe Implementations")
 call callee(instructions[pc].operation);
 for (vector<reagent>::iterator p = instructions[pc].ingredients.begin(); p != instructions[pc].ingredients.end(); ++p) {
   callee.ingredient_atoms.push_back(read_memory(*p));
 }
-rr.calls.push(callee);
+Current_routine->calls.push(callee);
 
 :(before "End Primitive Recipe Declarations")
 NEXT_INGREDIENT,
@@ -41,17 +41,17 @@ NEXT_INGREDIENT,
 Recipe_number["next-ingredient"] = NEXT_INGREDIENT;
 :(before "End Primitive Recipe Implementations")
 case NEXT_INGREDIENT: {
-  if (rr.calls.top().next_ingredient_to_process < rr.calls.top().ingredient_atoms.size()) {
+  if (Current_routine->calls.top().next_ingredient_to_process < Current_routine->calls.top().ingredient_atoms.size()) {
     trace("run") << "product 0 is "
-        << rr.calls.top().ingredient_atoms[rr.calls.top().next_ingredient_to_process][0];
+        << Current_routine->calls.top().ingredient_atoms[Current_routine->calls.top().next_ingredient_to_process][0];
     write_memory(instructions[pc].products[0],
-        rr.calls.top().ingredient_atoms[rr.calls.top().next_ingredient_to_process]);
+        Current_routine->calls.top().ingredient_atoms[Current_routine->calls.top().next_ingredient_to_process]);
     if (instructions[pc].products.size() > 1) {
       vector<int> ingredient_exists;
       ingredient_exists.push_back(1);
       write_memory(instructions[pc].products[1], ingredient_exists);
     }
-    ++rr.calls.top().next_ingredient_to_process;
+    ++Current_routine->calls.top().next_ingredient_to_process;
   }
   else {
     if (instructions[pc].products.size() > 1) {
@@ -84,7 +84,7 @@ REWIND_INGREDIENTS,
 Recipe_number["rewind-ingredients"] = REWIND_INGREDIENTS;
 :(before "End Primitive Recipe Implementations")
 case REWIND_INGREDIENTS: {
-  rr.calls.top().next_ingredient_to_process = 0;
+  Current_routine->calls.top().next_ingredient_to_process = 0;
   break;
 }
 
@@ -105,18 +105,18 @@ INGREDIENT,
 Recipe_number["ingredient"] = INGREDIENT;
 :(before "End Primitive Recipe Implementations")
 case INGREDIENT: {
-  if (static_cast<size_t>(instructions[pc].ingredients[0].value) < rr.calls.top().ingredient_atoms.size()) {
-    rr.calls.top().next_ingredient_to_process = instructions[pc].ingredients[0].value;
+  if (static_cast<size_t>(instructions[pc].ingredients[0].value) < Current_routine->calls.top().ingredient_atoms.size()) {
+    Current_routine->calls.top().next_ingredient_to_process = instructions[pc].ingredients[0].value;
     trace("run") << "product 0 is "
-        << rr.calls.top().ingredient_atoms[rr.calls.top().next_ingredient_to_process][0];
+        << Current_routine->calls.top().ingredient_atoms[Current_routine->calls.top().next_ingredient_to_process][0];
     write_memory(instructions[pc].products[0],
-        rr.calls.top().ingredient_atoms[rr.calls.top().next_ingredient_to_process]);
+        Current_routine->calls.top().ingredient_atoms[Current_routine->calls.top().next_ingredient_to_process]);
     if (instructions[pc].products.size() > 1) {
       vector<int> ingredient_exists;
       ingredient_exists.push_back(1);
       write_memory(instructions[pc].products[1], ingredient_exists);
     }
-    ++rr.calls.top().next_ingredient_to_process;
+    ++Current_routine->calls.top().next_ingredient_to_process;
   }
   else {
     if (instructions[pc].products.size() > 1) {
diff --git a/cpp/037call_reply b/cpp/037call_reply
index 22548eef..76e0ab9f 100644
--- a/cpp/037call_reply
+++ b/cpp/037call_reply
@@ -25,10 +25,10 @@ case REPLY: {
   for (size_t i = 0; i < instructions[pc].ingredients.size(); ++i) {
     callee_results.push_back(read_memory(instructions[pc].ingredients[i]));
   }
-  rr.calls.pop();
-  assert(!rr.calls.empty());
-  size_t& caller_pc = rr.calls.top().pc;
-  instruction& caller_instruction = Recipe[rr.calls.top().running_recipe].steps[caller_pc];
+  Current_routine->calls.pop();
+  assert(!Current_routine->calls.empty());
+  size_t& caller_pc = Current_routine->calls.top().pc;
+  instruction& caller_instruction = Recipe[Current_routine->calls.top().running_recipe].steps[caller_pc];
   assert(caller_instruction.products.size() <= callee_results.size());
   for (size_t i = 0; i < caller_instruction.products.size(); ++i) {
     trace("run") << "result " << i << " is " << to_string(callee_results[i]);
diff --git a/cpp/038scheduler b/cpp/038scheduler
index 3196795c..5ca6cbdc 100644
--- a/cpp/038scheduler
+++ b/cpp/038scheduler
@@ -8,12 +8,14 @@ size_t Scheduling_interval = 500;
 //: todo: these changes are ugly and brittle
 :(replace{} "void run(recipe_number r)")
 void run(recipe_number r) {
-  run(routine(r), Scheduling_interval);
+  routine rr(r);
+  Current_routine = &rr;
+  run_current_routine(Scheduling_interval);
 }
-:(replace "void run(routine rr)")
-void run(routine rr, size_t time_slice)
-:(replace "while (!done(rr))" following "void run(routine rr, size_t time_slice)")
+:(replace "void run_current_routine()")
+void run_current_routine(size_t time_slice)
+:(replace "while (!done())" following "void run_current_routine(size_t time_slice)")
 size_t ninstrs = 0;
-while (!done(rr) && ninstrs < time_slice)
+while (!done() && ninstrs < time_slice)
 :(after "Running One Instruction")
 ninstrs++;