//: So far we have local variables, and we can nest local variables of short
//: lifetimes inside longer ones. Now let's support 'global' variables that
//: last for the life of a routine. If we create multiple routines they won't
//: have access to each other's globals.
//:
//: This feature is still experimental and half-baked. You can't name global
//: variables, and so like in most tests they don't get checked for types (the
//: only known hole in the type system, can cause memory corruption). We might
//: fix these issues if we ever use globals. Or we might just drop the feature
//: entirely.

:(scenario global_space)
def main [
  # pretend address:array:location; in practice we'll use new
  10:num <- copy 0  # refcount
  11:num <- copy 5  # length
  # pretend address:array:location; in practice we'll use new
  20:num <- copy 0  # refcount
  21:num <- copy 5  # length
  # actual start of this recipe
  global-space:space <- copy 20/unsafe
  default-space:space <- copy 10/unsafe
  1:num <- copy 23
  1:num/space:global <- copy 24
]
# store to default space: 10 + (skip refcount and length) 2 + (index) 1
+mem: storing 23 in location 13
# store to chained space: (contents of location 12) 20 + (refcount and length) 2 + (index) 1
+mem: storing 24 in location 23

//: to support it, create another special variable called global space
:(before "End is_disqualified Cases")
if (x.name == "global-space")
  x.initialized = true;
:(before "End is_special_name Cases")
if (s == "global-space") return true;

//: writes to this variable go to a field in the current routine
:(before "End routine Fields")
int global_space;
:(before "End routine Constructor")
global_space = 0;
:(after "Begin Preprocess write_memory(x, data)")
if (x.name == "global-space") {
  if (!scalar(data) || !is_space(x))
    raise << maybe(current_recipe_name()) << "'global-space' should be of type address:array:location, but tried to write '" << to_string(x.type) << <