about summary refs log tree commit diff stats
path: root/020run.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-05-07 15:06:53 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-05-07 15:29:13 -0700
commit0487a30e7078861ed7de42bdb21b5c71fb9b54a1 (patch)
treef7ccc4040b510403da90477947c1cf07ea91b627 /020run.cc
parent94fa5c95ad9c8beead183bb7c4b88c7c2c7ca6ec (diff)
downloadmu-0487a30e7078861ed7de42bdb21b5c71fb9b54a1.tar.gz
1298 - better ingredient/product handling
All primitives now always write to all their products. If a product is
not used that's fine, but if an instruction seems to expect too many
products mu will complain.

In the process, many primitives can operate on more than two ingredients
where it seems intuitive. You can add or divide more than two numbers
together, copy or negate multiple corresponding locations, etc.

There's one remaining bit of ugliness. Some instructions like
get/get-address, index/index-address, wait-for-location, these can
unnecessarily load values from memory when they don't need to.

Useful vim commands:
  %s/ingredients\[\([^\]]*\)\]/ingredients.at(\1)/gc
  %s/products\[\([^\]]*\)\]/products.at(\1)/gc
  .,$s/\[\(.\)]/.at(\1)/gc
Diffstat (limited to '020run.cc')
-rw-r--r--020run.cc38
1 files changed, 31 insertions, 7 deletions
diff --git a/020run.cc b/020run.cc
index d8fdcf55..7dadff6d 100644
--- a/020run.cc
+++ b/020run.cc
@@ -27,6 +27,15 @@ recipe main [
 +mem: location 1 is 23
 +mem: storing 23 in location 2
 
+:(scenario copy_multiple)
+recipe main [
+  1:integer, 2:integer <- copy 23:literal, 24:literal
+]
++run: ingredient 0 is 23
++run: ingredient 1 is 24
++mem: storing 23 in location 1
++mem: storing 24 in location 2
+
 :(before "End Types")
 // Book-keeping while running a recipe.
 //: Later layers will change this.
@@ -55,14 +64,20 @@ void run_current_routine()
     if (current_instruction().is_label) { ++current_step_index(); continue; }
     trace("run") << "instruction " << current_recipe_name() << '/' << current_step_index();
     trace("run") << current_instruction().to_string();
-//?     trace("run") << Memory[1033] << '\n'; //? 1
-//?     cout << "operation " << current_instruction().operation << '\n'; //? 3
+    // Read all ingredients.
+    vector<vector<long long int> > ingredients;
+    for (index_t i = 0; i < current_instruction().ingredients.size(); ++i) {
+      trace("run") << "ingredient " << i << " is " << current_instruction().ingredients.at(i).name;
+      ingredients.push_back(read_memory(current_instruction().ingredients.at(i)));
+    }
+    // Instructions below will write to 'products' or to 'instruction_counter'.
+    vector<vector<long long int> > products;
+    index_t instruction_counter = current_step_index();
+//?     cout << "AAA: " << current_instruction().to_string() << '\n'; //? 1
     switch (current_instruction().operation) {
       // Primitive Recipe Implementations
       case COPY: {
-        trace("run") << "ingredient 0 is " << current_instruction().ingredients[0].name;
-        vector<long long int> data = read_memory(current_instruction().ingredients[0]);
-        write_memory(current_instruction().products[0], data);
+        copy(ingredients.begin(), ingredients.end(), inserter(products, products.begin()));
         break;
       }
       // End Primitive Recipe Implementations
@@ -70,7 +85,16 @@ void run_current_routine()
         cout << "not a primitive op: " << current_instruction().operation << '\n';
       }
     }
-    ++current_step_index();
+//?     cout << "BBB: " << current_instruction().to_string() << '\n'; //? 1
+    if (products.size() < current_instruction().products.size())
+      raise << "failed to write to all products! " << current_instruction().to_string();
+//?     cout << "CCC: " << current_instruction().to_string() << '\n'; //? 1
+    for (index_t i = 0; i < current_instruction().products.size(); ++i) {
+      trace("run") << "product " << i << " is " << current_instruction().products.at(i).name;
+      write_memory(current_instruction().products.at(i), products.at(i));
+    }
+//?     cout << "DDD: " << current_instruction().to_string() << '\n'; //? 1
+    current_step_index() = instruction_counter+1;
   }
 }
 
@@ -146,7 +170,7 @@ void run(string form) {
 //:: Reading from memory, writing to memory.
 
 vector<long long int> read_memory(reagent x) {
-//?   cout << "read_memory: " << x.to_string() << '\n'; //? 1
+//?   cout << "read_memory: " << x.to_string() << '\n'; //? 2
   vector<long long int> result;
   if (isa_literal(x)) {
     result.push_back(x.value);