1 //: Update refcounts when copying addresses.
   2 //: The top of the address layer has more on refcounts.
   3 
   4 :(scenario refcounts)
   5 def main [
   6   1:address:num <- copy 1000/unsafe
   7   2:address:num <- copy 1:address:num
   8   1:address:num <- copy 0
   9   2:address:num <- copy 0
  10 ]
  11 +run: {1: ("address" "number")} <- copy {1000: "literal", "unsafe": ()}
  12 +mem: incrementing refcount of 1000: 0 -> 1
  13 +run: {2: ("address" "number")} <- copy {1: ("address" "number")}
  14 +mem: incrementing refcount of 1000: 1 -> 2
  15 +run: {1: ("address" "number")} <- copy {0: "literal"}
  16 +mem: decrementing refcount of 1000: 2 -> 1
  17 +run: {2: ("address" "number")} <- copy {0: "literal"}
  18 +mem: decrementing refcount of 1000: 1 -> 0
  19 
  20 :(before "End write_memory(x) Special-cases")
  21 update_any_refcounts(x, data);
  22 
  23 :(before "End Globals")
  24 bool Reclaim_memory = true;
  25 :(before "End Commandline Options(*arg)")
  26 else if (is_equal(*arg, "--no-reclaim")) {
  27   cerr << "Disabling memory reclamation. Some tests will fail.\n";
  28   Reclaim_memory = false;
  29 }
  30 :(code)
  31 void update_any_refcounts(const reagent& canonized_x, const vector<double>& data) {
  32   if (!Reclaim_memory) return;
  33   if (!should_update_refcounts()) return;
  34   increment_any_refcounts(canonized_x, data);  // increment first so we don't reclaim on x <- copy x
  35   decrement_any_refcounts(canonized_x);
  36 }
  37 
  38 //: escape hatch for a later layer
  39 bool should_update_refcounts() {
  40   // End should_update_refcounts() Special-cases
  41   return true;
  42 }
  43 
  44 void increment_any_refcounts(const reagent& canonized_x, const vector<double>& data) {
  45   if (is_mu_address(canonized_x)) {
  46   ¦ assert(scalar(data));
  47   ¦ assert(!canonized_x.metadata.size);
  48   ¦ increment_refcount(data.at(0));
  49   }
  50   // End Increment Refcounts(canonized_x)
  51 }
  52 
  53 void increment_refcount(int new_address) {
  54   assert(new_address >= 0);
  55   if (new_address == 0) return;
  56   int new_refcount = get_or_insert(Memory, new_address);
  57   trace(9999, "mem") << "incrementing refcount of " << new_address << ": " << new_refcount << " -> " << new_refcount+1 << end();
  58   put(Memory, new_address, new_refcount+1);
  59 }
  60 
  61 void decrement_any_refcounts(const reagent& canonized_x) {
  62   if (is_mu_address(canonized_x)) {
  63   ¦ assert(canonized_x.value);
  64   ¦ assert(!canonized_x.metadata.size);
  65   ¦ decrement_refcount(get_or_insert(Memory, canonized_x.value), payload_type(canonized_x.type), payload_size(canonized_x));
  66   }
  67   // End Decrement Refcounts(canonized_x)
  68 }
  69 
  70 void decrement_refcount(int old_address, const type_tree* payload_type, int payload_size) {
  71   assert(old_address >= 0);
  72   if (old_address) {
  73   ¦ int old_refcount = get_or_insert(Memory, old_address);
  74   ¦ trace(9999, "mem") << "decrementing refcount of " << old_address << ": " << old_refcount << " -> " << old_refcount-1 << end();
  75   ¦ --old_refcount;
  76   ¦ put(Memory, old_address, old_refcount);
  77   ¦ if (old_refcount < 0) {
  78   ¦ ¦ tb_shutdown();
  79   ¦ ¦ cerr << "Negative refcount!!! " << old_address << ' ' << old_refcount << '\n';
  80   ¦ ¦ if (Trace_stream) {
  81   ¦ ¦ ¦ cerr << "Saving trace to last_trace.\n";
  82   ¦ ¦ ¦ ofstream fout("last_trace");
  83   ¦ ¦ ¦ fout << Trace_stream->readable_contents("");
  84   ¦ ¦ ¦ fout.close();
  85   ¦ ¦ }
  86   ¦ ¦ exit(0);
  87   ¦ }
  88   ¦ // End Decrement Refcount(old_address, payload_type, payload_size)
  89   }
  90 }
  91 
  92 int payload_size(reagent/*copy*/ x) {
  93   x.properties.push_back(pair<string, string_tree*>("lookup", NULL));
  94   lookup_memory_core(x, /*check for nulls*/false);
  95   return size_of(x) + /*refcount*/1;
  96 }
  97 
  98 :(scenario refcounts_reflexive)
  99 def main [
 100   1:address:num <- new number:type
 101   # idempotent copies leave refcount unchanged
 102   1:address:num <- copy 1:address:num
 103 ]
 104 +run: {1: ("address" "number")} <- new {number: "type"}
 105 +mem: incrementing refcount of 1000: 0 -> 1
 106 +run: {1: ("address" "number")} <- copy {1: ("address" "number")}
 107 +mem: incrementing refcount of 1000: 1 -> 2
 108 +mem: decrementing refcount of 1000: 2 -> 1
 109 
 110 :(scenario refcounts_call)
 111 def main [
 112   1:address:num <- new number:type
 113   # passing in addresses to recipes increments refcount
 114   foo 1:address:num
 115   # return does NOT yet decrement refcount; memory must be explicitly managed
 116   1:address:num <- new number:type
 117 ]
 118 def foo [
 119   2:address:num <- next-ingredient
 120 ]
 121 +run: {1: ("address" "number")} <- new {number: "type"}
 122 +mem: incrementing refcount of 1000: 0 -> 1
 123 +run: foo {1: ("address" "number")}
 124 # leave ambiguous precisely when the next increment happens
 125 +mem: incrementing refcount of 1000: 1 -> 2
 126 +run: {1: ("address" "number")} <- new {number: "type"}
 127 +mem: decrementing refcount of 1000: 2 -> 1
 128 
 129 //: fix up any instructions that don't follow the usual flow of read_memory
 130 //: before the RUN switch, and write_memory after
 131 
 132 :(scenario refcounts_put)
 133 container foo [
 134   x:address:num
 135 ]
 136 def main [
 137   1:address:num <- new number:type
 138   2:address:foo <- new foo:type
 139   *2:address:foo <- put *2:address:foo, x:offset, 1:address:num
 140 ]
 141 +run: {1: ("address" "number")} <- new {number: "type"}
 142 +mem: incrementing refcount of 1000: 0 -> 1
 143 +run: {2: ("address" "foo")} <- new {foo: "type"}
 144 +mem: incrementing refcount of 1002: 0 -> 1
 145 +run: {2: ("address" "foo"), "lookup": ()} <- put {2: ("address" "foo"), "lookup": ()}, {x: "offset"}, {1: ("address" "number")}
 146 # put increments refcount
 147 +mem: incrementing refcount of 1000: 1 -> 2
 148 
 149 :(after "Write Memory in PUT in Run")
 150 reagent/*copy*/ element = element_type(base.type, offset);
 151 assert(!has_property(element, "lookup"));
 152 element.set_value(address);
 153 update_any_refcounts(element, ingredients.at(2));
 154 
 155 :(scenario refcounts_put_index)
 156 def main [
 157   1:address:num <- new number:type
 158   2:address:array:address:num <- new {(address number): type}, 3
 159   *2:address:array:address:num <- put-index *2:address:array:address:num, 0, 1:address:num
 160 ]
 161 +run: {1: ("address" "number")} <- new {number: "type"}
 162 +mem: incrementing refcount of 1000: 0 -> 1
 163 +run: {2: ("address" "array" "address" "number")} <- new {(address number): "type"}, {3: "literal"}
 164 +mem: incrementing refcount of 1002: 0 -> 1
 165 +run: {2: ("address" "array" "address" "number"), "lookup": ()} <- put-index {2: ("address" "array" "address" "number"), "lookup": ()}, {0: "literal"}, {1: ("address" "number")}
 166 # put-index increments refcount
 167 +mem: incrementing refcount of 1000: 1 -> 2
 168 
 169 :(after "Write Memory in PUT_INDEX in Run")
 170 reagent/*local*/ element;
 171 element.set_value(address);
 172 element.type = copy_array_element(base.type);
 173 update_any_refcounts(element, value);
 174 
 175 :(scenario refcounts_maybe_convert)
 176 exclusive-container foo [
 177   x:num
 178   p:address:num
 179 ]
 180 def main [
 181   1:address:num <- new number:type
 182   2:foo <- merge 1/p, 1:address:num
 183   4:address:num, 5:bool <- maybe-convert 2:foo, 1:variant/p
 184 ]
 185 +run: {1: ("address" "number")} <- new {number: "type"}
 186 +mem: incrementing refcount of 1000: 0 -> 1
 187 # merging in an address increments refcount
 188 +run: {2: "foo"} <- merge {1: "literal", "p": ()}, {1: ("address" "number")}
 189 +mem: incrementing refcount of 1000: 1 -> 2
 190 +run: {4: ("address" "number")}, {5: "boolean"} <- maybe-convert {2: "foo"}, {1: "variant", "p": ()}
 191 # maybe-convert increments refcount on success
 192 +mem: incrementing refcount of 1000: 2 -> 3
 193 
 194 :(after "Write Memory in Successful MAYBE_CONVERT")
 195 // todo: double-check data here as well
 196 vector<double> data;
 197 for (int i = 0;  i < size_of(product);  ++i)
 198   data.push_back(get_or_insert(Memory, base_address+/*skip tag*/1+i));
 199 update_any_refcounts(product, data);
 200 
 201 //:: manage refcounts in instructions that copy multiple locations at a time
 202 
 203 :(scenario refcounts_copy_nested)
 204 container foo [
 205   x:address:num  # address inside container
 206 ]
 207 def main [
 208   1:address:num <- new number:type
 209   2:address:foo <- new foo:type
 210   *2:address:foo <- put *2:address:foo, x:offset, 1:address:num
 211   3:foo <- copy *2:address:foo
 212 ]
 213 +transform: compute address offsets for container foo
 214 +transform: checking container foo, element 0
 215 +transform: address at offset 0
 216 +run: {1: ("address" "number")} <- new {number: "type"}
 217 +mem: incrementing refcount of 1000: 0 -> 1
 218 +run: {2: ("address" "foo"), "lookup": ()} <- put {2: ("address" "foo"), "lookup": ()}, {x: "offset"}, {1: ("address" "number")}
 219 +mem: incrementing refcount of 1000: 1 -> 2
 220 # copying a container increments refcounts of any contained addresses
 221 +run: {3: "foo"} <- copy {2: ("address" "foo"), "lookup": ()}
 222 +mem: incrementing refcount of 1000: 2 -> 3
 223 
 224 :(before "End type_tree Definition")
 225 struct address_element_info {
 226   // Where inside a container type (after flattening nested containers!) the
 227   // address lies
 228   int offset;
 229 
 230   // All the information we need to compute sizes of items inside an address
 231   // inside a container. `payload_type` Doesn't need to be a full-scale
 232   // reagent, since an address inside a container can never be an array, and
 233   // arrays are the only type that need to know their location to compute their
 234   // size.
 235   const type_tree* payload_type;
 236 
 237   address_element_info(int o, const type_tree* p);
 238   address_element_info(const address_element_info& other);
 239   ~address_element_info();
 240   address_element_info& operator=(const address_element_info& other);
 241 };
 242 :(code)
 243 address_element_info::address_element_info(int o, const type_tree* p) {
 244   offset = o;
 245   payload_type = p;
 246 }
 247 address_element_info::address_element_info(const address_element_info& other) {
 248   offset = other.offset;
 249   payload_type = copy(other.payload_type);
 250 }
 251 address_element_info::~address_element_info() {
 252   if (payload_type) {
 253   ¦ delete payload_type;
 254   ¦ payload_type = NULL;
 255   }
 256 }
 257 address_element_info& address_element_info::operator=(const address_element_info& other) {
 258   offset = other.offset;
 259   if (payload_type) delete payload_type;
 260   payload_type = copy(other.payload_type);
 261   return *this;
 262 }
 263 
 264 :(before "End type_tree Definition")
 265 // For exclusive containers we might sometimes have an address at some offset
 266 // if some other offset has a specific tag. This struct encapsulates such
 267 // guards.
 268 struct tag_condition_info {
 269   int offset;
 270   int tag;
 271   tag_condition_info(int o, int t) :offset(o), tag(t) {}
 272 };
 273 
 274 :(before "End container_metadata Fields")
 275 // a list of facts of the form:
 276 //
 277 //  IF offset o1 has tag t2 AND offset o2 has tag t2 AND .., THEN
 278 //    for all address_element_infos:
 279 //      you need to update refcounts for the address at offset pointing to a payload of type payload_type (just in case we need to abandon something in the process)
 280 map<set<tag_condition_info>, set<address_element_info> > address;
 281 :(code)
 282 bool operator<(const set<tag_condition_info>& a, const set<tag_condition_info>& b) {
 283   if (a.size() != b.size()) return a.size() < b.size();
 284   for (set<tag_condition_info>::const_iterator pa = a.begin(), pb = b.begin();  pa != a.end();  ++pa, ++pb) {
 285   ¦ if (pa->offset != pb->offset) return pa->offset < pb->offset;
 286   ¦ if (pa->tag != pb->tag) return pa->tag < pb->tag;
 287   }
 288   return false;  // equal
 289 }
 290 bool operator<(const tag_condition_info& a, const tag_condition_info& b) {
 291   if (a.offset != b.offset) return a.offset < b.offset;
 292   if (a.tag != b.tag) return a.tag < b.tag;
 293   return false;  // equal
 294 }
 295 bool operator<(const set<address_element_info>& a, const set<address_element_info>& b) {
 296   if (a.size() != b.size()) return a.size() < b.size();
 297   for (set<address_element_info>::const_iterator pa = a.begin(), pb = b.begin();  pa != a.end();  ++pa, ++pb) {
 298   ¦ if (pa->offset != pb->offset) return pa->offset < pb->offset;
 299   }
 300   return false;  // equal
 301 }
 302 bool operator<(const address_element_info& a, const address_element_info& b) {
 303   if (a.offset != b.offset) return a.offset < b.offset;
 304   return false;  // equal
 305 }
 306 
 307 //: populate metadata.address in a separate transform, because it requires
 308 //: already knowing the sizes of all types
 309 
 310 :(after "Transform.push_back(compute_container_sizes)")
 311 Transform.push_back(compute_container_address_offsets);  // idempotent
 312 :(code)
 313 void compute_container_address_offsets(const recipe_ordinal r) {
 314   recipe& caller = get(Recipe, r);
 315   trace(9992, "transform") << "--- compute address offsets for " << caller.name << end();
 316   for (int i = 0;  i < SIZE(caller.steps);  ++i) {
 317   ¦ instruction& inst = caller.steps.at(i);
 318   ¦ trace(9993, "transform") << "- compute address offsets for " << to_string(inst) << end();
 319   ¦ for (int i = 0;  i < SIZE(inst.ingredients);  ++i)
 320   ¦ ¦ compute_container_address_offsets(inst.ingredients.at(i), " in '"+inst.original_string+"'");
 321   ¦ for (int i = 0;  i < SIZE(inst.products);  ++i)
 322   ¦ ¦ compute_container_address_offsets(inst.products.at(i), " in '"+inst.original_string+"'");
 323   }
 324 }
 325 
 326 void compute_container_address_offsets(reagent& r, const string& location_for_error_messages) {
 327   if (is_literal(r) || is_dummy(r)) return;
 328   compute_container_address_offsets(r.type, location_for_error_messages);
 329   if (contains_key(Container_metadata, r.type))
 330   ¦ r.metadata = get(Container_metadata, r.type);
 331 }
 332 
 333 // the recursive structure of this function needs to exactly match
 334 // compute_container_sizes
 335 void compute_container_address_offsets(const type_tree* type, const string& location_for_error_messages) {
 336   if (!type) return;
 337   if (!type->atom) {
 338   ¦ if (!type->left->atom) {
 339   ¦ ¦ raise << "invalid type " << to_string(type) << location_for_error_messages << '\n' << end();
 340   ¦ ¦ return;
 341   ¦ }
 342   ¦ if (type->left->name == "address")
 343   ¦ ¦ compute_container_address_offsets(payload_type(type), location_for_error_messages);
 344   ¦ else if (type->left->name == "array")
 345   ¦ ¦ compute_container_address_offsets(array_element(type), location_for_error_messages);
 346   ¦ // End compute_container_address_offsets Non-atom Special-cases
 347   }
 348   const type_tree* base_type = type;
 349   // Update base_type in compute_container_address_offsets
 350   if (!contains_key(Type, base_type->value)) return;  // error raised elsewhere
 351   type_info& info = get(Type, base_type->value);
 352   if (info.kind == CONTAINER) {
 353   ¦ compute_container_address_offsets(info, type, location_for_error_messages);
 354   }
 355   if (info.kind == EXCLUSIVE_CONTAINER) {
 356   ¦ compute_exclusive_container_address_offsets(info, type, location_for_error_messages);
 357   }
 358 }
 359 
 360 void compute_container_address_offsets(const type_info& container_info, const type_tree* full_type, const string& location_for_error_messages) {
 361   container_metadata& metadata = get(Container_metadata, full_type);
 362   if (!metadata.address.empty()) return;
 363   trace(9994, "transform") << "compute address offsets for container " << container_info.name << end();
 364   append_addresses(0, full_type, metadata.address, set<tag_condition_info>(), location_for_error_messages);
 365 }
 366 
 367 void compute_exclusive_container_address_offsets(const type_info& exclusive_container_info, const type_tree* full_type, const string& location_for_error_messages) {
 368   container_metadata& metadata = get(Container_metadata, full_type);
 369   trace(9994, "transform") << "compute address offsets for exclusive container " << exclusive_container_info.name << end();
 370   for (int tag = 0;  tag < SIZE(exclusive_container_info.elements);  ++tag) {
 371   ¦ set<tag_condition_info> key;
 372   ¦ key.insert(tag_condition_info(/*tag is at offset*/0, tag));
 373   ¦ append_addresses(/*skip tag offset*/1, variant_type(full_type, tag).type, metadata.address, key, location_for_error_messages);
 374   }
 375 }
 376 
 377 void append_addresses(int base_offset, const type_tree* type, map<set<tag_condition_info>, set<address_element_info> >& out, const set<tag_condition_info>& key, const string& location_for_error_messages) {
 378   if (is_mu_address(type)) {
 379   ¦ get_or_insert(out, key).insert(address_element_info(base_offset, new type_tree(*payload_type(type))));
 380   ¦ return;
 381   }
 382   const type_tree* base_type = type;
 383   // Update base_type in append_container_address_offsets
 384   const type_info& info = get(Type, base_type->value);
 385   if (info.kind == CONTAINER) {
 386   ¦ for (int curr_index = 0, curr_offset = base_offset;  curr_index < SIZE(info.elements);  ++curr_index) {
 387   ¦ ¦ trace(9993, "transform") << "checking container " << base_type->name << ", element " << curr_index << end();
 388   ¦ ¦ reagent/*copy*/ element = element_type(type, curr_index);  // not base_type
 389   ¦ ¦ // Compute Container Address Offset(element)
 390   ¦ ¦ if (is_mu_address(element)) {
 391   ¦ ¦ ¦ trace(9993, "transform") << "address at offset " << curr_offset << end();
 392   ¦ ¦ ¦ get_or_insert(out, key).insert(address_element_info(curr_offset, new type_tree(*payload_type(element.type))));
 393   ¦ ¦ ¦ ++curr_offset;
 394   ¦ ¦ }
 395   ¦ ¦ else if (is_mu_array(element)) {
 396   ¦ ¦ ¦ curr_offset += /*array length*/1;
 397   ¦ ¦ ¦ const type_tree* array_element_type = array_element(element.type);
 398   ¦ ¦ ¦ int array_element_size = size_of(array_element_type);
 399   ¦ ¦ ¦ for (int i = 0; i < static_array_length(element.type); ++i) {
 400   ¦ ¦ ¦ ¦ append_addresses(curr_offset, array_element_type, out, key, location_for_error_messages);
 401   ¦ ¦ ¦ ¦ curr_offset += array_element_size;
 402   ¦ ¦ ¦ }
 403   ¦ ¦ }
 404   ¦ ¦ else if (is_mu_container(element)) {
 405   ¦ ¦ ¦ append_addresses(curr_offset, element.type, out, key, location_for_error_messages);
 406   ¦ ¦ ¦ curr_offset += size_of(element);
 407   ¦ ¦ }
 408   ¦ ¦ else if (is_mu_exclusive_container(element)) {
 409   ¦ ¦ ¦ const type_tree* element_base_type = element.type;
 410   ¦ ¦ ¦ // Update element_base_type For Exclusive Container in append_addresses
 411   ¦ ¦ ¦ const type_info& element_info = get(Type, element_base_type->value);
 412   ¦ ¦ ¦ for (int tag = 0;  tag < SIZE(element_info.elements);  ++tag) {
 413   ¦ ¦ ¦ ¦ set<tag_condition_info> new_key = key;
 414   ¦ ¦ ¦ ¦ new_key.insert(tag_condition_info(curr_offset, tag));
 415   ¦ ¦ ¦ ¦ if (!contains_key(out, new_key))
 416   ¦ ¦ ¦ ¦ ¦ append_addresses(curr_offset+/*skip tag*/1, variant_type(element.type, tag).type, out, new_key, location_for_error_messages);
 417   ¦ ¦ ¦ }
 418   ¦ ¦ ¦ curr_offset += size_of(element);
 419   ¦ ¦ }
 420   ¦ ¦ else {
 421   ¦ ¦ ¦ // non-address primitive
 422   ¦ ¦ ¦ ++curr_offset;
 423   ¦ ¦ }
 424   ¦ }
 425   }
 426   else if (info.kind == EXCLUSIVE_CONTAINER) {
 427   ¦ for (int tag = 0;  tag < SIZE(info.elements);  ++tag) {
 428   ¦ ¦ set<tag_condition_info> new_key = key;
 429   ¦ ¦ new_key.insert(tag_condition_info(base_offset, tag));
 430   ¦ ¦ if (!contains_key(out, new_key))
 431   ¦ ¦ ¦ append_addresses(base_offset+/*skip tag*/1, variant_type(type, tag).type, out, new_key, location_for_error_messages);
 432   ¦ }
 433   }
 434 }
 435 
 436 //: for the following unit tests we'll do the work of the transform by hand
 437 
 438 :(before "End Unit Tests")
 439 void test_container_address_offsets_empty() {
 440   int old_size = SIZE(Container_metadata);
 441   // define a container with no addresses
 442   reagent r("x:point");
 443   compute_container_sizes(r, "");  // need to first pre-populate the metadata
 444   // scan
 445   compute_container_address_offsets(r, "");
 446   // global metadata contains just the entry for foo
 447   // no entries for non-container types or other junk
 448   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 449   // the reagent we scanned knows it has no addresses
 450   CHECK(r.metadata.address.empty());
 451   // the global table contains an identical entry
 452   CHECK(contains_key(Container_metadata, r.type));
 453   CHECK(get(Container_metadata, r.type).address.empty());
 454   // compute_container_address_offsets creates no new entries
 455   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 456 }
 457 
 458 void test_container_address_offsets() {
 459   int old_size = SIZE(Container_metadata);
 460   // define a container with an address at offset 0 that we have the size for
 461   run("container foo [\n"
 462   ¦ ¦ "  x:address:num\n"
 463   ¦ ¦ "]\n");
 464   reagent r("x:foo");
 465   compute_container_sizes(r, "");  // need to first pre-populate the metadata
 466   // scan
 467   compute_container_address_offsets(r, "");
 468   // global metadata contains just the entry for foo
 469   // no entries for non-container types or other junk
 470   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 471   // the reagent we scanned knows it has an address at offset 0
 472   CHECK_EQ(SIZE(r.metadata.address), 1);
 473   CHECK(contains_key(r.metadata.address, set<tag_condition_info>()));
 474   const set<address_element_info>& address_offsets = get(r.metadata.address, set<tag_condition_info>());  // unconditional for containers
 475   CHECK_EQ(SIZE(address_offsets), 1);
 476   CHECK_EQ(address_offsets.begin()->offset, 0);
 477   CHECK(address_offsets.begin()->payload_type->atom);
 478   CHECK_EQ(address_offsets.begin()->payload_type->name, "number");
 479   // the global table contains an identical entry
 480   CHECK(contains_key(Container_metadata, r.type));
 481   const set<address_element_info>& address_offsets2 = get(get(Container_metadata, r.type).address, set<tag_condition_info>());
 482   CHECK_EQ(SIZE(address_offsets2), 1);
 483   CHECK_EQ(address_offsets2.begin()->offset, 0);
 484   CHECK(address_offsets2.begin()->payload_type->atom);
 485   CHECK_EQ(address_offsets2.begin()->payload_type->name, "number");
 486   // compute_container_address_offsets creates no new entries
 487   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 488 }
 489 
 490 void test_container_address_offsets_2() {
 491   int old_size = SIZE(Container_metadata);
 492   // define a container with an address at offset 1 that we have the size for
 493   run("container foo [\n"
 494   ¦ ¦ "  x:num\n"
 495   ¦ ¦ "  y:address:num\n"
 496   ¦ ¦ "]\n");
 497   reagent r("x:foo");
 498   compute_container_sizes(r, "");  // need to first pre-populate the metadata
 499   // global metadata contains just the entry for foo
 500   // no entries for non-container types or other junk
 501   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 502   // scan
 503   compute_container_address_offsets(r, "");
 504   // compute_container_address_offsets creates no new entries
 505   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 506   // the reagent we scanned knows it has an address at offset 1
 507   CHECK_EQ(SIZE(r.metadata.address), 1);
 508   CHECK(contains_key(r.metadata.address, set<tag_condition_info>()));
 509   const set<address_element_info>& address_offsets = get(r.metadata.address, set<tag_condition_info>());
 510   CHECK_EQ(SIZE(address_offsets), 1);
 511   CHECK_EQ(address_offsets.begin()->offset, 1);  //
 512   CHECK(address_offsets.begin()->payload_type->atom);
 513   CHECK_EQ(address_offsets.begin()->payload_type->name, "number");
 514   // the global table contains an identical entry
 515   CHECK(contains_key(Container_metadata, r.type));
 516   const set<address_element_info>& address_offsets2 = get(get(Container_metadata, r.type).address, set<tag_condition_info>());
 517   CHECK_EQ(SIZE(address_offsets2), 1);
 518   CHECK_EQ(address_offsets2.begin()->offset, 1);  //
 519   CHECK(address_offsets2.begin()->payload_type->atom);
 520   CHECK_EQ(address_offsets2.begin()->payload_type->name, "number");
 521 }
 522 
 523 void test_container_address_offsets_nested() {
 524   int old_size = SIZE(Container_metadata);
 525   // define a container with a nested container containing an address
 526   run("container foo [\n"
 527   ¦ ¦ "  x:address:num\n"
 528   ¦ ¦ "  y:num\n"
 529   ¦ ¦ "]\n"
 530   ¦ ¦ "container bar [\n"
 531   ¦ ¦ "  p:point\n"
 532   ¦ ¦ "  f:foo\n"  // nested container containing address
 533   ¦ ¦ "]\n");
 534   reagent r("x:bar");
 535   compute_container_sizes(r, "");  // need to first pre-populate the metadata
 536   // global metadata contains entries for bar and included types: point and foo
 537   // no entries for non-container types or other junk
 538   CHECK_EQ(SIZE(Container_metadata)-old_size, 3);
 539   // scan
 540   compute_container_address_offsets(r, "");
 541   // the reagent we scanned knows it has an address at offset 2
 542   CHECK_EQ(SIZE(r.metadata.address), 1);
 543   CHECK(contains_key(r.metadata.address, set<tag_condition_info>()));
 544   const set<address_element_info>& address_offsets = get(r.metadata.address, set<tag_condition_info>());
 545   CHECK_EQ(SIZE(address_offsets), 1);
 546   CHECK_EQ(address_offsets.begin()->offset, 2);  //
 547   CHECK(address_offsets.begin()->payload_type->atom);
 548   CHECK_EQ(address_offsets.begin()->payload_type->name, "number");
 549   // the global table also knows its address offset
 550   CHECK(contains_key(Container_metadata, r.type));
 551   const set<address_element_info>& address_offsets2 = get(get(Container_metadata, r.type).address, set<tag_condition_info>());
 552   CHECK_EQ(SIZE(address_offsets2), 1);
 553   CHECK_EQ(address_offsets2.begin()->offset, 2);  //
 554   CHECK(address_offsets2.begin()->payload_type->atom);
 555   CHECK_EQ(address_offsets2.begin()->payload_type->name, "number");
 556   // compute_container_address_offsets creates no new entries
 557   CHECK_EQ(SIZE(Container_metadata)-old_size, 3);
 558 }
 559 
 560 void test_container_address_offsets_from_address() {
 561   int old_size = SIZE(Container_metadata);
 562   // define a container with an address at offset 0
 563   run("container foo [\n"
 564   ¦ ¦ "  x:address:num\n"
 565   ¦ ¦ "]\n");
 566   reagent r("x:address:foo");
 567   compute_container_sizes(r, "");  // need to first pre-populate the metadata
 568   // global metadata contains just the entry for foo
 569   // no entries for non-container types or other junk
 570   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 571   // scan an address to the container
 572   compute_container_address_offsets(r, "");
 573   // compute_container_address_offsets creates no new entries
 574   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 575   // scanning precomputed metadata for the container
 576   reagent container("x:foo");
 577   CHECK(contains_key(Container_metadata, container.type));
 578   const set<address_element_info>& address_offsets2 = get(get(Container_metadata, container.type).address, set<tag_condition_info>());
 579   CHECK_EQ(SIZE(address_offsets2), 1);
 580   CHECK_EQ(address_offsets2.begin()->offset, 0);
 581   CHECK(address_offsets2.begin()->payload_type->atom);
 582   CHECK_EQ(address_offsets2.begin()->payload_type->name, "number");
 583 }
 584 
 585 void test_container_address_offsets_from_array() {
 586   int old_size = SIZE(Container_metadata);
 587   // define a container with an address at offset 0
 588   run("container foo [\n"
 589   ¦ ¦ "  x:address:num\n"
 590   ¦ ¦ "]\n");
 591   reagent r("x:array:foo");
 592   compute_container_sizes(r, "");  // need to first pre-populate the metadata
 593   // global metadata contains just the entry for foo
 594   // no entries for non-container types or other junk
 595   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 596   // scan an array of the container
 597   compute_container_address_offsets(r, "");
 598   // compute_container_address_offsets creates no new entries
 599   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 600   // scanning precomputed metadata for the container
 601   reagent container("x:foo");
 602   CHECK(contains_key(Container_metadata, container.type));
 603   const set<address_element_info>& address_offsets2 = get(get(Container_metadata, container.type).address, set<tag_condition_info>());
 604   CHECK_EQ(SIZE(address_offsets2), 1);
 605   CHECK_EQ(address_offsets2.begin()->offset, 0);
 606   CHECK(address_offsets2.begin()->payload_type->atom);
 607   CHECK_EQ(address_offsets2.begin()->payload_type->name, "number");
 608 }
 609 
 610 void test_container_address_offsets_from_address_to_array() {
 611   int old_size = SIZE(Container_metadata);
 612   // define a container with an address at offset 0
 613   run("container foo [\n"
 614   ¦ ¦ "  x:address:num\n"
 615   ¦ ¦ "]\n");
 616   reagent r("x:address:array:foo");
 617   compute_container_sizes(r, "");  // need to first pre-populate the metadata
 618   // global metadata contains just the entry for foo
 619   // no entries for non-container types or other junk
 620   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 621   // scan an address to an array of the container
 622   compute_container_address_offsets(r, "");
 623   // compute_container_address_offsets creates no new entries
 624   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 625   // scanning precomputed metadata for the container
 626   reagent container("x:foo");
 627   CHECK(contains_key(Container_metadata, container.type));
 628   const set<address_element_info>& address_offsets2 = get(get(Container_metadata, container.type).address, set<tag_condition_info>());
 629   CHECK_EQ(SIZE(address_offsets2), 1);
 630   CHECK_EQ(address_offsets2.begin()->offset, 0);
 631   CHECK(address_offsets2.begin()->payload_type->atom);
 632   CHECK_EQ(address_offsets2.begin()->payload_type->name, "number");
 633 }
 634 
 635 void test_container_address_offsets_from_static_array() {
 636   int old_size = SIZE(Container_metadata);
 637   // define a container with an address at offset 0
 638   run("container foo [\n"
 639   ¦ ¦ "  x:address:num\n"
 640   ¦ ¦ "]\n");
 641   reagent r("x:array:foo:10");
 642   compute_container_sizes(r, "");  // need to first pre-populate the metadata
 643   // global metadata contains just the entry for foo
 644   // no entries for non-container types or other junk
 645   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 646   // scan a static array of the container
 647   compute_container_address_offsets(r, "");
 648   // compute_container_address_offsets creates no new entries
 649   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 650   // scanning precomputed metadata for the container
 651   reagent container("x:foo");
 652   CHECK(contains_key(Container_metadata, container.type));
 653   const set<address_element_info>& address_offsets2 = get(get(Container_metadata, container.type).address, set<tag_condition_info>());
 654   CHECK_EQ(SIZE(address_offsets2), 1);
 655   CHECK_EQ(address_offsets2.begin()->offset, 0);
 656   CHECK(address_offsets2.begin()->payload_type->atom);
 657   CHECK_EQ(address_offsets2.begin()->payload_type->name, "number");
 658 }
 659 
 660 void test_container_address_offsets_from_address_to_static_array() {
 661   int old_size = SIZE(Container_metadata);
 662   // define a container with an address at offset 0
 663   run("container foo [\n"
 664   ¦ ¦ "  x:address:num\n"
 665   ¦ ¦ "]\n");
 666   reagent r("x:address:array:foo:10");
 667   compute_container_sizes(r, "");  // need to first pre-populate the metadata
 668   // global metadata contains just the entry for foo
 669   // no entries for non-container types or other junk
 670   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 671   // scan an address to a static array of the container
 672   compute_container_address_offsets(r, "");
 673   // compute_container_address_offsets creates no new entries
 674   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 675   // scanning precomputed metadata for the container
 676   reagent container("x:foo");
 677   CHECK(contains_key(Container_metadata, container.type));
 678   const set<address_element_info>& address_offsets2 = get(get(Container_metadata, container.type).address, set<tag_condition_info>());
 679   CHECK_EQ(SIZE(address_offsets2), 1);
 680   CHECK_EQ(address_offsets2.begin()->offset, 0);
 681   CHECK(address_offsets2.begin()->payload_type->atom);
 682   CHECK_EQ(address_offsets2.begin()->payload_type->name, "number");
 683 }
 684 
 685 void test_container_address_offsets_from_repeated_address_and_array_types() {
 686   int old_size = SIZE(Container_metadata);
 687   // define a container with an address at offset 0
 688   run("container foo [\n"
 689   ¦ ¦ "  x:address:num\n"
 690   ¦ ¦ "]\n");
 691   // scan a deep nest of 'address' and 'array' types modifying a container
 692   reagent r("x:address:array:address:address:array:foo:10");
 693   compute_container_sizes(r, "");  // need to first pre-populate the metadata
 694   // global metadata contains just the entry for foo
 695   // no entries for non-container types or other junk
 696   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 697   compute_container_address_offsets(r, "");
 698   // compute_container_address_offsets creates no new entries
 699   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
 700   // scanning precomputed metadata for the container
 701   reagent container("x:foo");
 702   CHECK(contains_key(Container_metadata, container.type));
 703   const set<address_element_info>& address_offsets2 = get(get(Container_metadata, container.type).address, set<tag_condition_info>());
 704   CHECK_EQ(SIZE(address_offsets2), 1);
 705   CHECK_EQ(address_offsets2.begin()->offset, 0);
 706   CHECK(address_offsets2.begin()->payload_type->atom);
 707   CHECK_EQ(address_offsets2.begin()->payload_type->name, "number");
 708 }
 709 
 710 //: use metadata.address to update refcounts within containers, arrays and
 711 //: exclusive containers
 712 
 713 :(before "End Increment Refcounts(canonized_x)")
 714 if (is_mu_container(canonized_x) || is_mu_exclusive_container(canonized_x)) {
 715   const container_metadata& metadata = get(Container_metadata, canonized_x.type);
 716   for (map<set<tag_condition_info>, set<address_element_info> >::const_iterator p = metadata.address.begin();  p != metadata.address.end();  ++p) {
 717   ¦ if (!all_match(data, p->first)) continue;
 718   ¦ for (set<address_element_info>::const_iterator info = p->second.begin();  info != p->second.end();  ++info)
 719   ¦ ¦ increment_refcount(data.at(info->offset));
 720   }
 721 }
 722 
 723 :(before "End Decrement Refcounts(canonized_x)")
 724 if (is_mu_container(canonized_x) || is_mu_exclusive_container(canonized_x)) {
 725   trace(9999, "mem") << "need to read old value of '" << to_string(canonized_x) << "' to figure out what refcounts to decrement" << end();
 726   // read from canonized_x but without canonizing again
 727   // todo: inline without running canonize all over again
 728   reagent/*copy*/ tmp = canonized_x;
 729   tmp.properties.push_back(pair<string, string_tree*>("raw", NULL));
 730   vector<double> data = read_memory(tmp);
 731   trace(9999, "mem") << "done reading old value of '" << to_string(canonized_x) << "'" << end();
 732   const container_metadata& metadata = get(Container_metadata, canonized_x.type);
 733   for (map<set<tag_condition_info>, set<address_element_info> >::const_iterator p = metadata.address.begin();  p != metadata.address.end();  ++p) {
 734   ¦ if (!all_match(data, p->first)) continue;
 735   ¦ for (set<address_element_info>::const_iterator info = p->second.begin();  info != p->second.end();  ++info) {
 736   ¦ ¦ int element_address = get_or_insert(Memory, canonized_x.value + info->offset);
 737   ¦ ¦ reagent/*local*/ element;
 738   ¦ ¦ element.set_value(element_address+/*skip refcount*/1);
 739   ¦ ¦ element.type = new type_tree(*info->payload_type);
 740   ¦ ¦ decrement_refcount(element_address, info->payload_type, size_of(element)+/*refcount*/1);
 741   ¦ }
 742   }
 743 }
 744 
 745 :(code)
 746 bool all_match(const vector<double>& data, const set<tag_condition_info>& conditions) {
 747   for (set<tag_condition_info>::const_iterator p = conditions.begin();  p != conditions.end();  ++p) {
 748   ¦ if (data.at(p->offset) != p->tag)
 749   ¦ ¦ return false;
 750   }
 751   return true;
 752 }
 753 
 754 :(scenario refcounts_put_container)
 755 container foo [
 756   a:bar  # contains an address
 757 ]
 758 container bar [
 759   x:address:num
 760 ]
 761 def main [
 762   1:address:num <- new number:type
 763   2:bar <- merge 1:address:num
 764   3:address:foo <- new foo:type
 765   *3:address:foo <- put *3:address:foo, a:offset, 2:bar
 766 ]
 767 +run: {1: ("address" "number")} <- new {number: "type"}
 768 +mem: incrementing refcount of 1000: 0 -> 1
 769 +run: {2: "bar"} <- merge {1: ("address" "number")}
 770 +mem: incrementing refcount of 1000: 1 -> 2
 771 +run: {3: ("address" "foo"), "lookup": ()} <- put {3: ("address" "foo"), "lookup": ()}, {a: "offset"}, {2: "bar"}
 772 # put increments refcount inside container
 773 +mem: incrementing refcount of 1000: 2 -> 3
 774 
 775 :(scenario refcounts_put_index_array)
 776 container bar [
 777   x:address:num
 778 ]
 779 def main [
 780   1:address:num <- new number:type
 781   2:bar <- merge 1:address:num
 782   3:address:array:bar <- new bar:type, 3
 783   *3:address:array:bar <- put-index *3:address:array:bar, 0, 2:bar
 784 ]
 785 +run: {1: ("address" "number")} <- new {number: "type"}
 786 +mem: incrementing refcount of 1000: 0 -> 1
 787 +run: {2: "bar"} <- merge {1: ("address" "number")}
 788 +mem: incrementing refcount of 1000: 1 -> 2
 789 +run: {3: ("address" "array" "bar"), "lookup": ()} <- put-index {3: ("address" "array" "bar"), "lookup": ()}, {0: "literal"}, {2: "bar"}
 790 # put-index increments refcount inside container
 791 +mem: incrementing refcount of 1000: 2 -> 3
 792 
 793 :(scenario refcounts_maybe_convert_container)
 794 exclusive-container foo [
 795   a:num
 796   b:bar  # contains an address
 797 ]
 798 container bar [
 799   x:address:num
 800 ]
 801 def main [
 802   1:address:num <- new number:type
 803   2:bar <- merge 1:address:num
 804   3:foo <- merge 1/b, 2:bar
 805   5:bar, 6:bool <- maybe-convert 3:foo, 1:variant/b
 806 ]
 807 +run: {1: ("address" "number")} <- new {number: "type"}
 808 +mem: incrementing refcount of 1000: 0 -> 1
 809 +run: {2: "bar"} <- merge {1: ("address" "number")}
 810 +mem: incrementing refcount of 1000: 1 -> 2
 811 +run: {3: "foo"} <- merge {1: "literal", "b": ()}, {2: "bar"}
 812 +mem: incrementing refcount of 1000: 2 -> 3
 813 +run: {5: "bar"}, {6: "boolean"} <- maybe-convert {3: "foo"}, {1: "variant", "b": ()}
 814 +mem: incrementing refcount of 1000: 3 -> 4
 815 
 816 :(scenario refcounts_copy_doubly_nested)
 817 container foo [
 818   a:bar  # no addresses
 819   b:curr  # contains addresses
 820 ]
 821 container bar [
 822   x:num
 823   y:num
 824 ]
 825 container curr [
 826   x:num
 827   y:address:num  # address inside container inside container
 828 ]
 829 def main [
 830   1:address:num <- new number:type
 831   2:address:curr <- new curr:type
 832   *2:address:curr <- put *2:address:curr, 1:offset/y, 1:address:num
 833   3:address:foo <- new foo:type
 834   *3:address:foo <- put *3:address:foo, 1:offset/b, *2:address:curr
 835   4:foo <- copy *3:address:foo
 836 ]
 837 +transform: compute address offsets for container foo
 838 +transform: checking container foo, element 1
 839 +transform: address at offset 3
 840 +run: {1: ("address" "number")} <- new {number: "type"}
 841 +mem: incrementing refcount of 1000: 0 -> 1
 842 # storing an address in a container updates its refcount
 843 +run: {2: ("address" "curr"), "lookup": ()} <- put {2: ("address" "curr"), "lookup": ()}, {1: "offset", "y": ()}, {1: ("address" "number")}
 844 +mem: incrementing refcount of 1000: 1 -> 2
 845 # storing a container in a container updates refcounts of any contained addresses
 846 +run: {3: ("address" "foo"), "lookup": ()} <- put {3: ("address" "foo"), "lookup": ()}, {1: "offset", "b": ()}, {2: ("address" "curr"), "lookup": ()}
 847 +mem: incrementing refcount of 1000: 2 -> 3
 848 # copying a container containing a container containing an address updates refcount
 849 +run: {4: "foo"} <- copy {3: ("address" "foo"), "lookup": ()}
 850 +mem: incrementing refcount of 1000: 3 -> 4
 851 
 852 :(scenario refcounts_copy_exclusive_container_within_container)
 853 container foo [
 854   a:num
 855   b:bar
 856 ]
 857 exclusive-container bar [
 858   x:num
 859   y:num
 860   z:address:num
 861 ]
 862 def main [
 863   1:address:num <- new number:type
 864   2:bar <- merge 0/x, 34
 865   3:foo <- merge 12, 2:bar
 866   5:bar <- merge 1/y, 35
 867   6:foo <- merge 13, 5:bar
 868   8:bar <- merge 2/z, 1:address:num
 869   9:foo <- merge 14, 8:bar
 870   11:foo <- copy 9:foo
 871 ]
 872 +run: {1: ("address" "number")} <- new {number: "type"}
 873 +mem: incrementing refcount of 1000: 0 -> 1
 874 # no change while merging items of other types
 875 +run: {8: "bar"} <- merge {2: "literal", "z": ()}, {1: ("address" "number")}
 876 +mem: incrementing refcount of 1000: 1 -> 2
 877 +run: {9: "foo"} <- merge {14: "literal"}, {8: "bar"}
 878 +mem: incrementing refcount of 1000: 2 -> 3
 879 +run: {11: "foo"} <- copy {9: "foo"}
 880 +mem: incrementing refcount of 1000: 3 -> 4
 881 
 882 :(scenario refcounts_copy_container_within_exclusive_container)
 883 exclusive-container foo [
 884   a:num
 885   b:bar
 886 ]
 887 container bar [
 888   x:num
 889   y:num
 890   z:address:num
 891 ]
 892 def main [
 893   1:address:num <- new number:type
 894   2:foo <- merge 0/a, 34
 895   6:foo <- merge 0/a, 35
 896   10:bar <- merge 2/x, 15/y, 1:address:num
 897   13:foo <- merge 1/b, 10:bar
 898   17:foo <- copy 13:foo
 899 ]
 900 +run: {1: ("address" "number")} <- new {number: "type"}
 901 +mem: incrementing refcount of 1000: 0 -> 1
 902 # no change while merging items of other types
 903 +run: {10: "bar"} <- merge {2: "literal", "x": ()}, {15: "literal", "y": ()}, {1: ("address" "number")}
 904 +mem: incrementing refcount of 1000: 1 -> 2
 905 +run: {13: "foo"} <- merge {1: "literal", "b": ()}, {10: "bar"}
 906 +mem: incrementing refcount of 1000: 2 -> 3
 907 +run: {17: "foo"} <- copy {13: "foo"}
 908 +mem: incrementing refcount of 1000: 3 -> 4
 909 
 910 :(scenario refcounts_copy_exclusive_container_within_exclusive_container)
 911 exclusive-container foo [
 912   a:num
 913   b:bar
 914 ]
 915 exclusive-container bar [
 916   x:num
 917   y:address:num
 918 ]
 919 def main [
 920   1:address:num <- new number:type
 921   10:foo <- merge 1/b, 1/y, 1:address:num
 922   20:foo <- copy 10:foo
 923 ]
 924 +run: {1: ("address" "number")} <- new {number: "type"}
 925 +mem: incrementing refcount of 1000: 0 -> 1
 926 # no change while merging items of other types
 927 +run: {10: "foo"} <- merge {1: "literal", "b": ()}, {1: "literal", "y": ()}, {1: ("address" "number")}
 928 +mem: incrementing refcount of 1000: 1 -> 2
 929 +run: {20: "foo"} <- copy {10: "foo"}
 930 +mem: incrementing refcount of 1000: 2 -> 3
 931 
 932 :(scenario refcounts_copy_array_within_container)
 933 container foo [
 934   x:address:array:num
 935 ]
 936 def main [
 937   1:address:array:num <- new number:type, 3
 938   2:foo <- merge 1:address:array:num
 939   3:address:array:num <- new number:type, 5
 940   2:foo <- merge 3:address:array:num
 941 ]
 942 +run: {1: ("address" "array" "number")} <- new {number: "type"}, {3: "literal"}
 943 +mem: incrementing refcount of 1000: 0 -> 1
 944 +run: {2: "foo"} <- merge {1: ("address" "array" "number")}
 945 +mem: incrementing refcount of 1000: 1 -> 2
 946 +run: {2: "foo"} <- merge {3: ("address" "array" "number")}
 947 +mem: decrementing refcount of 1000: 2 -> 1
 948 
 949 :(scenario refcounts_copy_address_within_static_array_within_container)
 950 container foo [
 951   a:array:bar:3
 952   b:address:num
 953 ]
 954 container bar [
 955   y:num
 956   z:address:num
 957 ]
 958 def main [
 959   1:address:num <- new number:type
 960   2:bar <- merge 34, 1:address:num
 961   10:array:bar:3 <- create-array
 962   put-index 10:array:bar:3, 1, 2:bar
 963   20:foo <- merge 10:array:bar:3, 1:address:num
 964   1:address:num <- copy 0
 965   2:bar <- merge 34, 1:address:num
 966   put-index 10:array:bar:3, 1, 2:bar
 967   20:foo <- merge 10:array:bar:3, 1:address:num
 968 ]
 969 +run: {1: ("address" "number")} <- new {number: "type"}
 970 +mem: incrementing refcount of 1000: 0 -> 1
 971 +run: {2: "bar"} <- merge {34: "literal"}, {1: ("address" "number")}
 972 +mem: incrementing refcount of 1000: 1 -> 2
 973 +run: put-index {10: ("array" "bar" "3")}, {1: "literal"}, {2: "bar"}
 974 +mem: incrementing refcount of 1000: 2 -> 3
 975 +run: {20: "foo"} <- merge {10: ("array" "bar" "3")}, {1: ("address" "number")}
 976 +mem: incrementing refcount of 1000: 3 -> 4
 977 +mem: incrementing refcount of 1000: 4 -> 5
 978 +run: {1: ("address" "number")} <- copy {0: "literal"}
 979 +mem: decrementing refcount of 1000: 5 -> 4
 980 +run: {2: "bar"} <- merge {34: "literal"}, {1: ("address" "number")}
 981 +mem: decrementing refcount of 1000: 4 -> 3
 982 +run: put-index {10: ("array" "bar" "3")}, {1: "literal"}, {2: "bar"}
 983 +mem: decrementing refcount of 1000: 3 -> 2
 984 +run: {20: "foo"} <- merge {10: ("array" "bar" "3")}, {1: ("address" "number")}
 985 +mem: decrementing refcount of 1000: 2 -> 1
 986 +mem: decrementing refcount of 1000: 1 -> 0
 987 
 988 :(scenario refcounts_handle_exclusive_containers_with_different_tags)
 989 container foo1 [
 990   x:address:num
 991   y:num
 992 ]
 993 container foo2 [
 994   x:num
 995   y:address:num
 996 ]
 997 exclusive-container bar [
 998   a:foo1
 999   b:foo2
1000 ]
1001 def main [
1002   1:address:num <- copy 12000/unsafe  # pretend allocation
1003   *1:address:num <- copy 34
1004   2:bar <- merge 0/foo1, 1:address:num, 97
1005   5:address:num <- copy 13000/unsafe  # pretend allocation
1006   *5:address:num <- copy 35
1007   6:bar <- merge 1/foo2, 98, 5:address:num
1008   2:bar <- copy 6:bar
1009 ]
1010 +run: {2: "bar"} <- merge {0: "literal", "foo1": ()}, {1: ("address" "number")}, {97: "literal"}
1011 +mem: incrementing refcount of 12000: 1 -> 2
1012 +run: {6: "bar"} <- merge {1: "literal", "foo2": ()}, {98: "literal"}, {5: ("address" "number")}
1013 +mem: incrementing refcount of 13000: 1 -> 2
1014 +run: {2: "bar"} <- copy {6: "bar"}
1015 +mem: incrementing refcount of 13000: 2 -> 3
1016 +mem: decrementing refcount of 12000: 2 -> 1
1017 
1018 :(code)
1019 bool is_mu_container(const reagent& r) {
1020   return is_mu_container(r.type);
1021 }
1022 bool is_mu_container(const type_tree* type) {
1023   if (!type) return false;
1024   // End is_mu_container(type) Special-cases
1025   if (type->value == 0) return false;
1026   type_info& info = get(Type, type->value);
1027   return info.kind == CONTAINER;
1028 }
1029 
1030 bool is_mu_exclusive_container(const reagent& r) {
1031   return is_mu_exclusive_container(r.type);
1032 }
1033 bool is_mu_exclusive_container(const type_tree* type) {
1034   if (!type) return false;
1035   // End is_mu_exclusive_container(type) Special-cases
1036   if (type->value == 0) return false;
1037   type_info& info = get(Type, type->value);
1038   return info.kind == EXCLUSIVE_CONTAINER;
1039 }