about summary refs log tree commit diff stats
path: root/043space.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2017-10-21 07:07:30 -0700
committerKartik K. Agaram <vc@akkartik.com>2017-10-21 07:28:51 -0700
commit619bd33e01091d0b50478e96e97b6ae28f78fc6d (patch)
tree58a4104c19aacfdf6396147d252cc2129773763e /043space.cc
parent636837e7d9733206cba3a1947ec46d4f003b93ce (diff)
downloadmu-619bd33e01091d0b50478e96e97b6ae28f78fc6d.tar.gz
4087
Clean up the narrative of spaces as I struggle to reimplement
`local-scope` by the plan of commit 3992.
Diffstat (limited to '043space.cc')
-rw-r--r--043space.cc49
1 files changed, 26 insertions, 23 deletions
diff --git a/043space.cc b/043space.cc
index ca8ee648..378b20a9 100644
--- a/043space.cc
+++ b/043space.cc
@@ -1,12 +1,13 @@
 //: Spaces help isolate recipes from each other. You can create them at will,
 //: and all addresses in arguments are implicitly based on the 'default-space'
 //: (unless they have the /raw property)
+//:
+//: Spaces are often called 'scopes' in other languages. Stack frames are a
+//: limited form of space that can't outlive callers.
 
-//: A space is just an array of any scalar location.
+//: Under the hood, a space is an array of locations in memory.
 :(before "End Mu Types Initialization")
 put(Type_abbreviations, "space", new_type_tree("address:array:location"));
-//: Spaces are often called 'scopes' in other languages.
-put(Type_abbreviations, "scope", new_type_tree("address:array:location"));
 
 :(scenario set_default_space)
 # if default-space is 10, and if an array of 5 locals lies from location 12 to 16 (inclusive),
@@ -34,7 +35,8 @@ def main [
 ]
 +mem: storing 34 in location 8
 
-//:: first disable name conversion for 'default-space'
+//: precondition: disable name conversion for 'default-space'
+
 :(scenario convert_names_passes_default_space)
 % Hide_errors = true;
 def main [
@@ -49,13 +51,14 @@ if (x.name == "default-space")
 :(before "End is_special_name Special-cases")
 if (s == "default-space") return true;
 
-//:: now implement space support
+//: core implementation
+
 :(before "End call Fields")
 int default_space;
 :(before "End call Constructor")
 default_space = 0;
 
-:(before "End canonize(x) Special-cases")
+:(before "Begin canonize(x) Lookups")
 absolutize(x);
 :(code)
 void absolutize(reagent& x) {
@@ -85,7 +88,7 @@ int address(int offset, int base) {
   return base + /*skip length*/1 + offset;
 }
 
-//:: reads and writes to the 'default-space' variable have special behavior
+//: reads and writes to the 'default-space' variable have special behavior
 
 :(after "Begin Preprocess write_memory(x, data)")
 if (x.name == "default-space") {
@@ -181,6 +184,17 @@ if (s == "number-of-locals") return true;
 if (curr.name == "new-default-space") {
   rewrite_default_space_instruction(curr);
 }
+:(code)
+void rewrite_default_space_instruction(instruction& curr) {
+  if (!curr.ingredients.empty())
+    raise << "'" << to_original_string(curr) << "' can't take any ingredients\n" << end();
+  curr.name = "new";
+  curr.ingredients.push_back(reagent("location:type"));
+  curr.ingredients.push_back(reagent("number-of-locals:literal"));
+  if (!curr.products.empty())
+    raise << "new-default-space can't take any results\n" << end();
+  curr.products.push_back(reagent("default-space:space"));
+}
 :(after "Begin Preprocess read_memory(x)")
 if (x.name == "number-of-locals") {
   vector<double> result;
@@ -200,14 +214,14 @@ if (x.name == "number-of-locals") {
 
 :(scenario local_scope)
 def main [
-  1:&:@:location <- foo
-  2:&:@:location <- foo
-  3:bool <- equal 1:&, 2:&
+  1:num <- foo
+  2:num <- foo
+  3:bool <- equal 1:num, 2:num
 ]
 def foo [
   local-scope
-  x:num <- copy 34
-  return default-space:space
+  result:num <- copy default-space:space
+  return result:num
 ]
 # both calls to foo should have received the same default-space
 +mem: storing 1 in location 3
@@ -311,17 +325,6 @@ bool caller_uses_product(int product_index) {
   return !is_dummy(caller_inst.products.at(product_index));
 }
 
-void rewrite_default_space_instruction(instruction& curr) {
-  if (!curr.ingredients.empty())
-    raise << "'" << to_original_string(curr) << "' can't take any ingredients\n" << end();
-  curr.name = "new";
-  curr.ingredients.push_back(reagent("location:type"));
-  curr.ingredients.push_back(reagent("number-of-locals:literal"));
-  if (!curr.products.empty())
-    raise << "new-default-space can't take any results\n" << end();
-  curr.products.push_back(reagent("default-space:space"));
-}
-
 :(scenario local_scope_frees_up_addresses_inside_containers)
 container foo [
   x:num
319' href='#n319'>319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382