From 619bd33e01091d0b50478e96e97b6ae28f78fc6d Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 21 Oct 2017 07:07:30 -0700 Subject: 4087 Clean up the narrative of spaces as I struggle to reimplement `local-scope` by the plan of commit 3992. --- 035lookup.cc | 2 +- 043space.cc | 49 ++++++++++++++++++++++++++----------------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/035lookup.cc b/035lookup.cc index b5a1ba02..a0cb8fb2 100644 --- a/035lookup.cc +++ b/035lookup.cc @@ -79,7 +79,7 @@ def main [ :(code) void canonize(reagent& x) { if (is_literal(x)) return; - // End canonize(x) Special-cases + // Begin canonize(x) Lookups while (has_property(x, "lookup")) lookup_memory(x); } 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 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 -- cgit 1.4.1-2-gfad0 d='n62' href='#n62'>62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272