about summary refs log tree commit diff stats
path: root/063wait.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-04-23 12:24:00 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-04-23 12:25:28 -0700
commit538815670a8747e68dd95cd43e0ac1fce184f781 (patch)
treecb5263aba168c82f9e3bd3e3659fc5b6da12086a /063wait.cc
parenta67028dcb87a71403a82ad754f2d5f26b4374608 (diff)
downloadmu-538815670a8747e68dd95cd43e0ac1fce184f781.tar.gz
2859 - rename 'get-address' to 'get-location'
This reinfoces that it's only really intended to be used by
'wait-for-location'. To reinforce that we also move it to the same layer
as 'wait-for-location'.
Diffstat (limited to '063wait.cc')
-rw-r--r--063wait.cc142
1 files changed, 138 insertions, 4 deletions
diff --git a/063wait.cc b/063wait.cc
index 1adc9ad4..71e3123e 100644
--- a/063wait.cc
+++ b/063wait.cc
@@ -7,8 +7,8 @@
 def f1 [
   1:number <- copy 0
   start-running f2
-  2:address:number <- copy 1/unsafe
-  wait-for-location 2:address:number
+  2:location <- copy 1/unsafe
+  wait-for-location 2:location
   # now wait for f2 to run and modify location 1 before using its value
   3:number <- copy 1:number
 ]
@@ -62,8 +62,8 @@ case WAIT_FOR_LOCATION: {
     raise << maybe(get(Recipe, r).name) << "'wait-for-location' requires exactly one ingredient, but got " << to_original_string(inst) << '\n' << end();
     break;
   }
-  if (!is_mu_address(inst.ingredients.at(0))) {
-    raise << maybe(get(Recipe, r).name) << "'wait-for-location' requires an address ingredient, but got " << inst.ingredients.at(0).original_string << '\n' << end();
+  if (!is_mu_location(inst.ingredients.at(0))) {
+    raise << maybe(get(Recipe, r).name) << "'wait-for-location' requires a location ingredient, but got " << inst.ingredients.at(0).original_string << '\n' << end();
   }
   break;
 }
@@ -90,6 +90,140 @@ for (int i = 0; i < SIZE(Routines); ++i) {
   }
 }
 
+//: primitive to help compute locations to wait for
+//: only supports elements inside containers, no arrays or containers within
+//: containers yet.
+
+:(scenario get_location)
+def main [
+  12:number <- copy 34
+  13:number <- copy 35
+  15:location <- get-location 12:point, 1:offset
+]
++mem: storing 13 in location 15
+
+:(before "End Primitive Recipe Declarations")
+GET_LOCATION,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "get-location", GET_LOCATION);
+:(before "End Primitive Recipe Checks")
+case GET_LOCATION: {
+  if (SIZE(inst.ingredients) != 2) {
+    raise << maybe(get(Recipe, r).name) << "'get-location' expects exactly 2 ingredients in '" << to_original_string(inst) << "'\n" << end();
+    break;
+  }
+  reagent base = inst.ingredients.at(0);
+  if (!canonize_type(base)) break;
+  if (!base.type || !base.type->value || !contains_key(Type, base.type->value) || get(Type, base.type->value).kind != CONTAINER) {
+    raise << maybe(get(Recipe, r).name) << "first ingredient of 'get-location' should be a container, but got " << inst.ingredients.at(0).original_string << '\n' << end();
+    break;
+  }
+  type_ordinal base_type = base.type->value;
+  reagent offset = inst.ingredients.at(1);
+  if (!is_literal(offset) || !is_mu_scalar(offset)) {
+    raise << maybe(get(Recipe, r).name) << "second ingredient of 'get-location' should have type 'offset', but got " << inst.ingredients.at(1).original_string << '\n' << end();
+    break;
+  }
+  int offset_value = 0;
+  if (is_integer(offset.name)) {  // later layers permit non-integer offsets
+    offset_value = to_integer(offset.name);
+    if (offset_value < 0 || offset_value >= SIZE(get(Type, base_type).elements)) {
+      raise << maybe(get(Recipe, r).name) << "invalid offset " << offset_value << " for " << get(Type, base_type).name << '\n' << end();
+      break;
+    }
+  }
+  else {
+    offset_value = offset.value;
+  }
+  if (inst.products.empty()) break;
+  if (!is_mu_location(inst.products.at(0))) {
+    raise << maybe(get(Recipe, r).name) << "'get-location " << base.original_string << ", " << offset.original_string << "' should write to type location but " << inst.products.at(0).name << " has type " << names_to_string_without_quotes(inst.products.at(0).type) << '\n' << end();
+    break;
+  }
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case GET_LOCATION: {
+  reagent base = current_instruction().ingredients.at(0);
+  canonize(base);
+  int base_address = base.value;
+  if (base_address == 0) {
+    raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
+    break;
+  }
+  type_ordinal base_type = base.type->value;
+  int offset = ingredients.at(1).at(0);
+  if (offset < 0 || offset >= SIZE(get(Type, base_type).elements)) break;  // copied from Check above
+  int result = base_address;
+  for (int i = 0; i < offset; ++i)
+    result += size_of(element_type(base, i));
+  trace(9998, "run") << "address to copy is " << result << end();
+  products.resize(1);
+  products.at(0).push_back(result);
+  break;
+}
+
+:(code)
+bool is_mu_location(reagent x) {
+  if (!canonize_type(x)) return false;
+  if (!x.type) return false;
+  if (x.type->right) return false;
+  return x.type->value == get(Type_ordinal, "location");
+}
+
+:(scenario get_location_out_of_bounds)
+% Hide_errors = true;
+def main [
+  12:number <- copy 34
+  13:number <- copy 35
+  14:number <- copy 36
+  get-location 12:point-number/raw, 2:offset  # point-number occupies 3 locations but has only 2 fields; out of bounds
+]
++error: main: invalid offset 2 for point-number
+
+:(scenario get_location_out_of_bounds_2)
+% Hide_errors = true;
+def main [
+  12:number <- copy 34
+  13:number <- copy 35
+  14:number <- copy 36
+  get-location 12:point-number/raw, -1:offset
+]
++error: main: invalid offset -1 for point-number
+
+:(scenario get_location_product_type_mismatch)
+% Hide_errors = true;
+container boolbool [
+  x:boolean
+  y:boolean
+]
+def main [
+  12:boolean <- copy 1
+  13:boolean <- copy 0
+  15:boolean <- get-location 12:boolbool, 1:offset
+]
++error: main: 'get-location 12:boolbool, 1:offset' should write to type location but 15 has type boolean
+
+:(scenario get_location_indirect)
+# 'get-location' can read from container address
+def main [
+  1:number <- copy 2
+  2:number <- copy 34
+  3:number <- copy 35
+  4:location <- get-location 1:address:point/lookup, 0:offset
+]
++mem: storing 2 in location 4
+
+:(scenario get_location_indirect2)
+def main [
+  1:number <- copy 2
+  2:number <- copy 34
+  3:number <- copy 35
+  4:address:number <- copy 5/unsafe
+  4:address:location/lookup <- get-location 1:address:point/lookup, 0:offset
+]
++mem: storing 2 in location 5
+
 //: also allow waiting on a routine to stop running
 
 :(scenario wait_for_routine)