diff options
-rw-r--r-- | 030address.cc | 180 | ||||
-rw-r--r-- | 030container.cc (renamed from 031container.cc) | 52 | ||||
-rw-r--r-- | 031array.cc (renamed from 032array.cc) | 103 | ||||
-rw-r--r-- | 032exclusive_container.cc (renamed from 033exclusive_container.cc) | 34 | ||||
-rw-r--r-- | 033address.cc | 387 | ||||
-rw-r--r-- | 034new.cc (renamed from 037new.cc) | 0 | ||||
-rw-r--r-- | 035location_array.cc (renamed from 038location_array.cc) | 0 | ||||
-rw-r--r-- | 057shape_shifting_container.cc | 12 |
8 files changed, 465 insertions, 303 deletions
diff --git a/030address.cc b/030address.cc deleted file mode 100644 index 5f86b30a..00000000 --- a/030address.cc +++ /dev/null @@ -1,180 +0,0 @@ -//: Instructions can read from addresses pointing at other locations using the -//: 'lookup' property. - -:(scenario copy_indirect) -def main [ - 1:address:number <- copy 2/unsafe - 2:number <- copy 34 - # This loads location 1 as an address and looks up *that* location. - 3:number <- copy 1:address:number/lookup -] -+mem: storing 34 in location 3 - -:(before "End Preprocess read_memory(x)") -canonize(x); - -//: similarly, write to addresses pointing at other locations using the -//: 'lookup' property -:(scenario store_indirect) -def main [ - 1:address:number <- copy 2/unsafe - 1:address:number/lookup <- copy 34 -] -+mem: storing 34 in location 2 - -:(before "End Preprocess write_memory(x)") -canonize(x); -if (x.value == 0) { - raise << "can't write to location 0 in '" << to_original_string(current_instruction()) << "'\n" << end(); - return; -} - -//: writes to address 0 always loudly fail -:(scenario store_to_0_fails) -% Hide_errors = true; -def main [ - 1:address:number <- copy 0 - 1:address:number/lookup <- copy 34 -] --mem: storing 34 in location 0 -+error: can't write to location 0 in '1:address:number/lookup <- copy 34' - -:(code) -void canonize(reagent& x) { - if (is_literal(x)) return; - // End canonize(x) Special-cases - while (has_property(x, "lookup")) - lookup_memory(x); -} - -void lookup_memory(reagent& x) { - if (!x.type || x.type->value != get(Type_ordinal, "address")) { - raise << maybe(current_recipe_name()) << "tried to /lookup " << x.original_string << " but it isn't an address\n" << end(); - return; - } - // compute value - if (x.value == 0) { - raise << maybe(current_recipe_name()) << "tried to /lookup 0\n" << end(); - return; - } - trace(9999, "mem") << "location " << x.value << " is " << no_scientific(get_or_insert(Memory, x.value)) << end(); - x.set_value(get_or_insert(Memory, x.value)); - drop_from_type(x, "address"); - // End Drop Address In lookup_memory(x) - drop_one_lookup(x); -} - -:(after "bool types_strictly_match(reagent to, reagent from)") - if (!canonize_type(to)) return false; - if (!canonize_type(from)) return false; - -:(after "bool is_mu_array(reagent r)") - if (!canonize_type(r)) return false; - -:(after "bool is_mu_address(reagent r)") - if (!canonize_type(r)) return false; - -:(after "bool is_mu_number(reagent r)") - if (!canonize_type(r)) return false; -:(after "bool is_mu_boolean(reagent r)") - if (!canonize_type(r)) return false; - -:(before "End Compute Call Ingredient") -canonize_type(ingredient); -:(before "End Preprocess NEXT_INGREDIENT product") -canonize_type(product); -:(before "End Check REPLY Copy(lhs, rhs) -canonize_type(lhs); -canonize_type(rhs); - -:(code) -bool canonize_type(reagent& r) { - while (has_property(r, "lookup")) { - if (!r.type || r.type->value != get(Type_ordinal, "address")) { - raise << "can't lookup non-address: " << to_string(r) << ": " << to_string(r.type) << '\n' << end(); - return false; - } - drop_from_type(r, "address"); - // End Drop Address In canonize_type(r) - drop_one_lookup(r); - } - return true; -} - -void drop_from_type(reagent& r, string expected_type) { - if (r.type->name != expected_type) { - raise << "can't drop2 " << expected_type << " from " << to_string(r) << '\n' << end(); - return; - } - type_tree* tmp = r.type; - r.type = tmp->right; - tmp->right = NULL; - delete tmp; -} - -void drop_one_lookup(reagent& r) { - for (vector<pair<string, string_tree*> >::iterator p = r.properties.begin(); p != r.properties.end(); ++p) { - if (p->first == "lookup") { - r.properties.erase(p); - return; - } - } - assert(false); -} - -//:: abbreviation for '/lookup': a prefix '*' - -:(scenario lookup_abbreviation) -def main [ - 1:address:number <- copy 2/unsafe - 2:number <- copy 34 - 3:number <- copy *1:address:number -] -+parse: ingredient: {1: ("address" "number"), "lookup": ()} -+mem: storing 34 in location 3 - -:(before "End Parsing reagent") -{ - while (!name.empty() && name.at(0) == '*') { - name.erase(0, 1); - properties.push_back(pair<string, string_tree*>("lookup", NULL)); - } - if (name.empty()) - raise << "illegal name " << original_string << '\n' << end(); -} - -//:: helpers for debugging - -:(before "End Primitive Recipe Declarations") -_DUMP, -:(before "End Primitive Recipe Numbers") -put(Recipe_ordinal, "$dump", _DUMP); -:(before "End Primitive Recipe Implementations") -case _DUMP: { - reagent after_canonize = current_instruction().ingredients.at(0); - canonize(after_canonize); - cerr << maybe(current_recipe_name()) << current_instruction().ingredients.at(0).name << ' ' << no_scientific(current_instruction().ingredients.at(0).value) << " => " << no_scientific(after_canonize.value) << " => " << no_scientific(get_or_insert(Memory, after_canonize.value)) << '\n'; - break; -} - -//: grab an address, and then dump its value at intervals -//: useful for tracking down memory corruption (writing to an out-of-bounds address) -:(before "End Globals") -int Bar = -1; -:(before "End Primitive Recipe Declarations") -_BAR, -:(before "End Primitive Recipe Numbers") -put(Recipe_ordinal, "$bar", _BAR); -:(before "End Primitive Recipe Implementations") -case _BAR: { - if (current_instruction().ingredients.empty()) { - if (Bar != -1) cerr << Bar << ": " << no_scientific(get_or_insert(Memory, Bar)) << '\n'; - else cerr << '\n'; - } - else { - reagent tmp = current_instruction().ingredients.at(0); - canonize(tmp); - Bar = tmp.value; - } - break; -} diff --git a/031container.cc b/030container.cc index 963afb68..ead9be70 100644 --- a/031container.cc +++ b/030container.cc @@ -51,7 +51,7 @@ def main [ ] +mem: storing 36 in location 17 -//: Products of recipes can include containers and exclusive containers, addresses and arrays. +//: products of recipes can include containers :(scenario reply_container) def main [ 3:point <- f 2 @@ -151,7 +151,7 @@ case GET: { break; } reagent base = inst.ingredients.at(0); // new copy for every invocation - if (!canonize_type(base)) break; + // Update GET base in Check 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' should be a container, but got " << inst.ingredients.at(0).original_string << '\n' << end(); break; @@ -173,7 +173,7 @@ case GET: { } if (inst.products.empty()) break; reagent product = inst.products.at(0); - if (!canonize_type(product)) break; + // Update GET product in Check const reagent element = element_type(base, offset_value); if (!types_coercible(product, element)) { raise << maybe(get(Recipe, r).name) << "'get " << base.original_string << ", " << offset.original_string << "' should write to " << names_to_string_without_quotes(element.type) << " but " << product.name << " has type " << names_to_string_without_quotes(product.type) << '\n' << end(); @@ -184,7 +184,7 @@ case GET: { :(before "End Primitive Recipe Implementations") case GET: { reagent base = current_instruction().ingredients.at(0); - canonize(base); + // Update GET base in Run 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(); @@ -206,11 +206,11 @@ case GET: { } :(code) -const reagent element_type(const reagent& canonized_base, int offset_value) { +const reagent element_type(const reagent& base, int offset_value) { assert(offset_value >= 0); - assert(contains_key(Type, canonized_base.type->value)); - assert(!get(Type, canonized_base.type->value).name.empty()); - const type_info& info = get(Type, canonized_base.type->value); + assert(contains_key(Type, base.type->value)); + assert(!get(Type, base.type->value).name.empty()); + const type_info& info = get(Type, base.type->value); assert(info.kind == CONTAINER); reagent element = info.elements.at(offset_value); // End element_type Special-cases @@ -256,35 +256,6 @@ def main [ ] +error: main: 'get 12:point-number/raw, 1:offset' should write to number but 15 has type (address number) -//: 'get' can read from container address -:(scenario get_indirect) -def main [ - 1:number <- copy 2 - 2:number <- copy 34 - 3:number <- copy 35 - 4:number <- get 1:address:point/lookup, 0:offset -] -+mem: storing 34 in location 4 - -:(scenario get_indirect2) -def main [ - 1:number <- copy 2 - 2:number <- copy 34 - 3:number <- copy 35 - 4:address:number <- copy 5/unsafe - *4:address:number <- get 1:address:point/lookup, 0:offset -] -+mem: storing 34 in location 5 - -:(scenario include_nonlookup_properties) -def main [ - 1:number <- copy 2 - 2:number <- copy 34 - 3:number <- copy 35 - 4:number <- get 1:address:point/lookup/foo, 0:offset -] -+mem: storing 34 in location 4 - //: we might want to call 'get' without saving the results, say in a sandbox :(scenario get_without_product) @@ -318,13 +289,14 @@ case PUT: { break; } reagent base = inst.ingredients.at(0); - if (!canonize_type(base)) break; + // Update PUT base in Check 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 'put' 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); + // Update PUT offset in Check if (!is_literal(offset) || !is_mu_scalar(offset)) { raise << maybe(get(Recipe, r).name) << "second ingredient of 'put' should have type 'offset', but got " << inst.ingredients.at(1).original_string << '\n' << end(); break; @@ -351,7 +323,7 @@ case PUT: { :(before "End Primitive Recipe Implementations") case PUT: { reagent base = current_instruction().ingredients.at(0); - canonize(base); + // Update PUT base in Run 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(); @@ -713,7 +685,7 @@ void check_merge_calls(const recipe_ordinal r) { continue; } reagent product = inst.products.at(0); - if (!canonize_type(product)) continue; + // Update product While Type-checking Merge type_ordinal product_type = product.type->value; if (product_type == 0 || !contains_key(Type, product_type)) { raise << maybe(caller.name) << "'merge' should yield a container in '" << to_original_string(inst) << "'\n" << end(); diff --git a/032array.cc b/031array.cc index 44d063d9..ac1cc848 100644 --- a/032array.cc +++ b/031array.cc @@ -24,7 +24,7 @@ case CREATE_ARRAY: { break; } reagent product = inst.products.at(0); - canonize_type(product); + // Update CREATE_ARRAY product in Check if (!is_mu_array(product)) { raise << maybe(get(Recipe, r).name) << "'create-array' cannot create non-array " << product.original_string << '\n' << end(); break; @@ -47,10 +47,11 @@ case CREATE_ARRAY: { :(before "End Primitive Recipe Implementations") case CREATE_ARRAY: { reagent product = current_instruction().products.at(0); - canonize(product); + // Update CREATE_ARRAY product in Run int base_address = product.value; int array_length = to_integer(product.type->right->right->name); // initialize array size, so that size_of will work + trace(9999, "mem") << "storing " << array_length << " in location " << base_address << end(); put(Memory, base_address, array_length); // in array elements int size = size_of(product); // in locations trace(9998, "run") << "creating array of size " << size << '\n' << end(); @@ -81,20 +82,6 @@ def main [ +mem: storing 15 in location 7 +mem: storing 16 in location 8 -:(scenario copy_array_indirect) -def main [ - 1:array:number:3 <- create-array - 2:number <- copy 14 - 3:number <- copy 15 - 4:number <- copy 16 - 5:address:array:number <- copy 1/unsafe - 6:array:number <- copy *5:address:array:number -] -+mem: storing 3 in location 6 -+mem: storing 14 in location 7 -+mem: storing 15 in location 8 -+mem: storing 16 in location 9 - :(scenario stash_array) def main [ 1:array:number:3 <- create-array @@ -184,14 +171,20 @@ case INDEX: { break; } reagent base = inst.ingredients.at(0); - canonize_type(base); + // Update INDEX base in Check if (!is_mu_array(base)) { raise << maybe(get(Recipe, r).name) << "'index' on a non-array " << base.original_string << '\n' << end(); break; } + reagent index = inst.ingredients.at(1); + // Update INDEX index in Check + if (!is_mu_number(index)) { + raise << maybe(get(Recipe, r).name) << "second ingredient of 'index' should be a number, but got " << index.original_string << '\n' << end(); + break; + } if (inst.products.empty()) break; reagent product = inst.products.at(0); - canonize_type(product); + // Update INDEX product in Check reagent element; element.type = new type_tree(*array_element(base.type)); if (!types_coercible(product, element)) { @@ -203,22 +196,22 @@ case INDEX: { :(before "End Primitive Recipe Implementations") case INDEX: { reagent base = current_instruction().ingredients.at(0); - canonize(base); + // Update INDEX base in Run int base_address = base.value; trace(9998, "run") << "base address is " << base_address << end(); if (base_address == 0) { raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end(); break; } - reagent offset = current_instruction().ingredients.at(1); - canonize(offset); - vector<double> offset_val(read_memory(offset)); + reagent index = current_instruction().ingredients.at(1); + // Update INDEX index in Run + vector<double> index_val(read_memory(index)); type_tree* element_type = array_element(base.type); - if (offset_val.at(0) < 0 || offset_val.at(0) >= get_or_insert(Memory, base_address)) { - raise << maybe(current_recipe_name()) << "invalid index " << no_scientific(offset_val.at(0)) << '\n' << end(); + if (index_val.at(0) < 0 || index_val.at(0) >= get_or_insert(Memory, base_address)) { + raise << maybe(current_recipe_name()) << "invalid index " << no_scientific(index_val.at(0)) << '\n' << end(); break; } - int src = base_address + 1 + offset_val.at(0)*size_of(element_type); + int src = base_address + 1 + index_val.at(0)*size_of(element_type); trace(9998, "run") << "address to copy is " << src << end(); trace(9998, "run") << "its type is " << get(Type, element_type->value).name << end(); reagent element; @@ -247,17 +240,6 @@ int array_length(const reagent& x) { return get_or_insert(Memory, x.value); } -:(scenario index_indirect) -def main [ - 1:array:number:3 <- create-array - 2:number <- copy 14 - 3:number <- copy 15 - 4:number <- copy 16 - 5:address:array:number <- copy 1/unsafe - 6:number <- index *5:address:array:number, 1 -] -+mem: storing 15 in location 6 - :(scenario index_out_of_bounds) % Hide_errors = true; def main [ @@ -268,8 +250,7 @@ def main [ 5:number <- copy 14 6:number <- copy 15 7:number <- copy 16 - 8:address:array:point <- copy 1/unsafe - index *8:address:array:point, 4 # less than size of array in locations, but larger than its length in elements + index 1:array:number:3, 4 # less than size of array in locations, but larger than its length in elements ] +error: main: invalid index 4 @@ -283,8 +264,7 @@ def main [ 5:number <- copy 14 6:number <- copy 15 7:number <- copy 16 - 8:address:array:point <- copy 1/unsafe - index *8:address:array:point, -1 + index 1:array:point, -1 ] +error: main: invalid index -1 @@ -298,10 +278,9 @@ def main [ 5:number <- copy 14 6:number <- copy 15 7:number <- copy 16 - 8:address:array:point <- copy 1/unsafe - 9:number <- index *8:address:array:point, 0 + 9:number <- index 1:array:point, 0 ] -+error: main: 'index' on *8:address:array:point can't be saved in 9:number; type should be point ++error: main: 'index' on 1:array:point can't be saved in 9:number; type should be point //: we might want to call 'index' without saving the results, say in a sandbox @@ -338,17 +317,19 @@ case PUT_INDEX: { break; } reagent base = inst.ingredients.at(0); - if (!canonize_type(base)) break; + // Update PUT_INDEX base in Check if (!is_mu_array(base)) { raise << maybe(get(Recipe, r).name) << "'put-index' on a non-array " << base.original_string << '\n' << end(); break; } - if (!is_mu_number(inst.ingredients.at(1))) { + reagent index = inst.ingredients.at(1); + // Update PUT_INDEX index in Check + if (!is_mu_number(index)) { raise << maybe(get(Recipe, r).name) << "second ingredient of 'put-index' should have type 'number', but got " << inst.ingredients.at(1).original_string << '\n' << end(); break; } reagent value = inst.ingredients.at(2); - canonize_type(value); + // Update PUT_INDEX value in Check reagent element; element.type = new type_tree(*array_element(base.type)); if (!types_coercible(element, value)) { @@ -360,21 +341,21 @@ case PUT_INDEX: { :(before "End Primitive Recipe Implementations") case PUT_INDEX: { reagent base = current_instruction().ingredients.at(0); - canonize(base); + // Update PUT_INDEX base in Run 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; } - reagent offset = current_instruction().ingredients.at(1); - canonize(offset); - vector<double> offset_val(read_memory(offset)); + reagent index = current_instruction().ingredients.at(1); + // Update PUT_INDEX index in Run + vector<double> index_val(read_memory(index)); type_tree* element_type = array_element(base.type); - if (offset_val.at(0) < 0 || offset_val.at(0) >= get_or_insert(Memory, base_address)) { - raise << maybe(current_recipe_name()) << "invalid index " << no_scientific(offset_val.at(0)) << '\n' << end(); + if (index_val.at(0) < 0 || index_val.at(0) >= get_or_insert(Memory, base_address)) { + raise << maybe(current_recipe_name()) << "invalid index " << no_scientific(index_val.at(0)) << '\n' << end(); break; } - int address = base_address + 1 + offset_val.at(0)*size_of(element_type); + int address = base_address + 1 + index_val.at(0)*size_of(element_type); trace(9998, "run") << "address to copy to is " << address << end(); // optimization: directly write the element rather than updating 'product' // and writing the entire array @@ -438,24 +419,24 @@ case LENGTH: { raise << maybe(get(Recipe, r).name) << "'length' expects exactly 2 ingredients in '" << to_original_string(inst) << "'\n" << end(); break; } - reagent x = inst.ingredients.at(0); - canonize_type(x); - if (!is_mu_array(x)) { - raise << "tried to calculate length of non-array " << x.original_string << '\n' << end(); + reagent array = inst.ingredients.at(0); + // Update LENGTH array in Check + if (!is_mu_array(array)) { + raise << "tried to calculate length of non-array " << array.original_string << '\n' << end(); break; } break; } :(before "End Primitive Recipe Implementations") case LENGTH: { - reagent x = current_instruction().ingredients.at(0); - canonize(x); - if (x.value == 0) { + reagent array = current_instruction().ingredients.at(0); + // Update LENGTH array in Run + if (array.value == 0) { raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end(); break; } products.resize(1); - products.at(0).push_back(get_or_insert(Memory, x.value)); + products.at(0).push_back(get_or_insert(Memory, array.value)); break; } diff --git a/033exclusive_container.cc b/032exclusive_container.cc index 4a7a5e40..ad82824d 100644 --- a/033exclusive_container.cc +++ b/032exclusive_container.cc @@ -91,7 +91,7 @@ case MAYBE_CONVERT: { break; } reagent base = inst.ingredients.at(0); - canonize_type(base); + // Update MAYBE_CONVERT base in Check if (!base.type || !base.type->value || get(Type, base.type->value).kind != EXCLUSIVE_CONTAINER) { raise << maybe(caller.name) << "first ingredient of 'maybe-convert' should be an exclusive-container, but got " << base.original_string << '\n' << end(); break; @@ -106,7 +106,7 @@ case MAYBE_CONVERT: { break; } reagent product = inst.products.at(0); - if (!canonize_type(product)) break; + // Update MAYBE_CONVERT product in Check reagent& offset = inst.ingredients.at(1); populate_value(offset); if (offset.value >= SIZE(get(Type, base.type->value).elements)) { @@ -118,7 +118,9 @@ case MAYBE_CONVERT: { raise << maybe(caller.name) << "'maybe-convert " << base.original_string << ", " << inst.ingredients.at(1).original_string << "' should write to " << to_string(variant.type) << " but " << product.name << " has type " << to_string(product.type) << '\n' << end(); break; } - if (!is_mu_boolean(inst.products.at(1))) { + reagent status = inst.products.at(1); + // Update MAYBE_CONVERT status in Check + if (!is_mu_boolean(status)) { raise << maybe(get(Recipe, r).name) << "second product yielded by 'maybe-convert' should be a boolean, but tried to write to " << inst.products.at(1).original_string << '\n' << end(); break; } @@ -127,7 +129,7 @@ case MAYBE_CONVERT: { :(before "End Primitive Recipe Implementations") case MAYBE_CONVERT: { reagent base = current_instruction().ingredients.at(0); - canonize(base); + // Update MAYBE_CONVERT base in Run 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(); @@ -135,9 +137,9 @@ case MAYBE_CONVERT: { } int tag = current_instruction().ingredients.at(1).value; reagent product = current_instruction().products.at(0); - canonize(product); - reagent did_conversion_happen = current_instruction().products.at(1); - canonize(did_conversion_happen); + // Update MAYBE_CONVERT product in Run + reagent status = current_instruction().products.at(1); + // Update MAYBE_CONVERT status in Run // optimization: directly write results to only update first product when necessary if (tag == static_cast<int>(get_or_insert(Memory, base_address))) { const reagent variant = variant_type(base, tag); @@ -146,22 +148,22 @@ case MAYBE_CONVERT: { trace(9999, "mem") << "storing " << no_scientific(val) << " in location " << product.value+i << end(); put(Memory, product.value+i, val); } - trace(9999, "mem") << "storing 1 in location " << did_conversion_happen.value << end(); - put(Memory, did_conversion_happen.value, 1); + trace(9999, "mem") << "storing 1 in location " << status.value << end(); + put(Memory, status.value, 1); } else { - trace(9999, "mem") << "storing 0 in location " << did_conversion_happen.value << end(); - put(Memory, did_conversion_happen.value, 0); + trace(9999, "mem") << "storing 0 in location " << status.value << end(); + put(Memory, status.value, 0); } goto finish_instruction; } :(code) -const reagent variant_type(const reagent& canonized_base, int tag) { +const reagent variant_type(const reagent& base, int tag) { assert(tag >= 0); - assert(contains_key(Type, canonized_base.type->value)); - assert(!get(Type, canonized_base.type->value).name.empty()); - const type_info& info = get(Type, canonized_base.type->value); + assert(contains_key(Type, base.type->value)); + assert(!get(Type, base.type->value).name.empty()); + const type_info& info = get(Type, base.type->value); assert(info.kind == EXCLUSIVE_CONTAINER); reagent element = info.elements.at(tag); // End variant_type Special-cases @@ -384,7 +386,7 @@ if (current_step_index() < SIZE(Current_routine->steps()) && !current_instruction().products.empty() && current_instruction().products.at(0).type) { reagent x = current_instruction().products.at(0); - canonize(x); + // Update size_mismatch Check for MERGE(x) if (get(Type, x.type->value).kind == EXCLUSIVE_CONTAINER) return size_of(x) < SIZE(data); } diff --git a/033address.cc b/033address.cc new file mode 100644 index 00000000..1b956fe0 --- /dev/null +++ b/033address.cc @@ -0,0 +1,387 @@ +//: Instructions can read from addresses pointing at other locations using the +//: 'lookup' property. + +:(scenario copy_indirect) +def main [ + 1:address:number <- copy 2/unsafe + 2:number <- copy 34 + # This loads location 1 as an address and looks up *that* location. + 3:number <- copy 1:address:number/lookup +] ++mem: storing 34 in location 3 + +:(before "End Preprocess read_memory(x)") +canonize(x); + +//: similarly, write to addresses pointing at other locations using the +//: 'lookup' property +:(scenario store_indirect) +def main [ + 1:address:number <- copy 2/unsafe + 1:address:number/lookup <- copy 34 +] ++mem: storing 34 in location 2 + +:(before "End Preprocess write_memory(x)") +canonize(x); +if (x.value == 0) { + raise << "can't write to location 0 in '" << to_original_string(current_instruction()) << "'\n" << end(); + return; +} + +//: writes to address 0 always loudly fail +:(scenario store_to_0_fails) +% Hide_errors = true; +def main [ + 1:address:number <- copy 0 + 1:address:number/lookup <- copy 34 +] +-mem: storing 34 in location 0 ++error: can't write to location 0 in '1:address:number/lookup <- copy 34' + +:(code) +void canonize(reagent& x) { + if (is_literal(x)) return; + // End canonize(x) Special-cases + while (has_property(x, "lookup")) + lookup_memory(x); +} + +void lookup_memory(reagent& x) { + if (!x.type || x.type->value != get(Type_ordinal, "address")) { + raise << maybe(current_recipe_name()) << "tried to /lookup " << x.original_string << " but it isn't an address\n" << end(); + return; + } + // compute value + if (x.value == 0) { + raise << maybe(current_recipe_name()) << "tried to /lookup 0\n" << end(); + return; + } + trace(9999, "mem") << "location " << x.value << " is " << no_scientific(get_or_insert(Memory, x.value)) << end(); + x.set_value(get_or_insert(Memory, x.value)); + drop_from_type(x, "address"); + // End Drop Address In lookup_memory(x) + drop_one_lookup(x); +} + +:(after "bool types_strictly_match(reagent to, reagent from)") + if (!canonize_type(to)) return false; + if (!canonize_type(from)) return false; + +:(after "bool is_mu_array(reagent r)") + if (!canonize_type(r)) return false; + +:(after "bool is_mu_address(reagent r)") + if (!canonize_type(r)) return false; + +:(after "bool is_mu_number(reagent r)") + if (!canonize_type(r)) return false; +:(after "bool is_mu_boolean(reagent r)") + if (!canonize_type(r)) return false; + +:(after "Update product While Type-checking Merge") +if (!canonize_type(product)) continue; + +:(before "End Compute Call Ingredient") +canonize_type(ingredient); +:(before "End Preprocess NEXT_INGREDIENT product") +canonize_type(product); +:(before "End Check REPLY Copy(lhs, rhs) +canonize_type(lhs); +canonize_type(rhs); + +:(code) +bool canonize_type(reagent& r) { + while (has_property(r, "lookup")) { + if (!r.type || r.type->value != get(Type_ordinal, "address")) { + raise << "can't lookup non-address: " << to_string(r) << ": " << to_string(r.type) << '\n' << end(); + return false; + } + drop_from_type(r, "address"); + // End Drop Address In canonize_type(r) + drop_one_lookup(r); + } + return true; +} + +void drop_from_type(reagent& r, string expected_type) { + if (r.type->name != expected_type) { + raise << "can't drop2 " << expected_type << " from " << to_string(r) << '\n' << end(); + return; + } + type_tree* tmp = r.type; + r.type = tmp->right; + tmp->right = NULL; + delete tmp; +} + +void drop_one_lookup(reagent& r) { + for (vector<pair<string, string_tree*> >::iterator p = r.properties.begin(); p != r.properties.end(); ++p) { + if (p->first == "lookup") { + r.properties.erase(p); + return; + } + } + assert(false); +} + +//:: 'get' can read from container address +:(scenario get_indirect) +def main [ + 1:number <- copy 2 + 2:number <- copy 34 + 3:number <- copy 35 + 4:number <- get 1:address:point/lookup, 0:offset +] ++mem: storing 34 in location 4 + +:(scenario get_indirect2) +def main [ + 1:number <- copy 2 + 2:number <- copy 34 + 3:number <- copy 35 + 4:address:number <- copy 5/unsafe + *4:address:number <- get 1:address:point/lookup, 0:offset +] ++mem: storing 34 in location 5 + +:(scenario include_nonlookup_properties) +def main [ + 1:number <- copy 2 + 2:number <- copy 34 + 3:number <- copy 35 + 4:number <- get 1:address:point/lookup/foo, 0:offset +] ++mem: storing 34 in location 4 + +:(after "Update GET base in Check") +if (!canonize_type(base)) break; +:(after "Update GET product in Check") +if (!canonize_type(product)) break; +:(after "Update GET base in Run") +canonize(base); + +:(scenario put_indirect) +# 'put' can read from container address +def main [ + 1:number <- copy 2 + 2:number <- copy 34 + 3:number <- copy 35 + 1:address:point/lookup <- put 1:address:point/lookup, 0:offset, 36 +] ++mem: storing 36 in location 2 + +:(after "Update PUT base in Check") +if (!canonize_type(base)) break; +:(after "Update PUT offset in Check") +if (!canonize_type(offset)) break; +:(after "Update PUT base in Run") +canonize(base); + +:(scenario copy_array_indirect) +def main [ + 1:array:number:3 <- create-array + 2:number <- copy 14 + 3:number <- copy 15 + 4:number <- copy 16 + 5:address:array:number <- copy 1/unsafe + 6:array:number <- copy *5:address:array:number +] ++mem: storing 3 in location 6 ++mem: storing 14 in location 7 ++mem: storing 15 in location 8 ++mem: storing 16 in location 9 + +:(before "Update CREATE_ARRAY product in Check") +// 'create-array' does not support indirection. Static arrays are meant to be +// allocated on the 'stack'. +assert(!has_property(product, "lookup")); +:(before "Update CREATE_ARRAY product in Run") +// 'create-array' does not support indirection. Static arrays are meant to be +// allocated on the 'stack'. +assert(!has_property(product, "lookup")); + +:(scenario index_indirect) +def main [ + 1:array:number:3 <- create-array + 2:number <- copy 14 + 3:number <- copy 15 + 4:number <- copy 16 + 5:address:array:number <- copy 1/unsafe + 6:number <- index 5:address:array:number/lookup, 1 +] ++mem: storing 15 in location 6 + +:(before "Update INDEX base in Check") +if (!canonize_type(base)) break; +:(before "Update INDEX index in Check") +if (!canonize_type(index)) break; +:(before "Update INDEX product in Check") +if (!canonize_type(product)) break; + +:(before "Update INDEX base in Run") +canonize(base); +:(before "Update INDEX index in Run") +canonize(index); + +:(scenario put_index_indirect) +def main [ + 1:array:number:3 <- create-array + 2:number <- copy 14 + 3:number <- copy 15 + 4:number <- copy 16 + 5:address:array:number <- copy 1/unsafe + 5:address:array:number/lookup <- put-index 5:address:array:number/lookup, 1, 34 +] ++mem: storing 34 in location 3 + +:(scenario put_index_indirect_2) +def main [ + 1:array:number:3 <- create-array + 2:number <- copy 14 + 3:number <- copy 15 + 4:number <- copy 16 + 5:address:number <- copy 6/unsafe + 6:number <- copy 1 + 5:address:array:number/lookup <- put-index 1:array:number:3, 5:address:number/lookup, 34 +] ++mem: storing 34 in location 3 + +:(before "Update PUT_INDEX base in Check") +if (!canonize_type(base)) break; +:(before "Update PUT_INDEX index in Check") +if (!canonize_type(index)) break; +:(before "Update PUT_INDEX value in Check") +if (!canonize_type(value)) break; + +:(before "Update PUT_INDEX base in Run") +canonize(base); +:(before "Update PUT_INDEX index in Run") +canonize(index); + +:(scenario length_indirect) +def main [ + 1:array:number:3 <- create-array + 2:number <- copy 14 + 3:number <- copy 15 + 4:number <- copy 16 + 5:address:array:number <- copy 1/unsafe + 6:number <- length 5:address:array:number/lookup +] ++mem: storing 3 in location 6 + +:(before "Update LENGTH array in Check") +if (!canonize_type(array)) break; +:(before "Update LENGTH array in Run") +canonize(array); + +:(scenario maybe_convert_indirect) +def main [ + 1:number-or-point <- merge 0/number, 34 + 10:address:number-or-point <- copy 1/unsafe + 11:number, 12:boolean <- maybe-convert 10:address:number-or-point/lookup, i:variant +] ++mem: storing 34 in location 11 ++mem: storing 1 in location 12 + +:(scenario maybe_convert_indirect_2) +def main [ + 1:number-or-point <- merge 0/number, 34 + 10:address:number-or-point <- copy 1/unsafe + 11:address:number <- copy 20/unsafe + 11:address:number/lookup, 12:boolean <- maybe-convert 10:address:number-or-point/lookup, i:variant +] ++mem: storing 34 in location 20 ++mem: storing 1 in location 12 + +:(scenario maybe_convert_indirect_3) +def main [ + 1:number-or-point <- merge 0/number, 34 + 10:address:number-or-point <- copy 1/unsafe + 12:address:boolean <- copy 20/unsafe + 11:number, 12:address:boolean/lookup <- maybe-convert 10:address:number-or-point/lookup, i:variant +] ++mem: storing 34 in location 11 ++mem: storing 1 in location 20 + +:(before "Update MAYBE_CONVERT base in Check") +if (!canonize_type(base)) break; +:(before "Update MAYBE_CONVERT product in Check") +if (!canonize_type(product)) break; +:(before "Update MAYBE_CONVERT status in Check") +if (!canonize_type(status)) break; + +:(before "Update MAYBE_CONVERT base in Run") +canonize(base); +:(before "Update MAYBE_CONVERT product in Run") +canonize(product); +:(before "Update MAYBE_CONVERT status in Run") +canonize(status); + +:(scenario merge_exclusive_container_indirect) +def main [ + 1:address:number-or-point <- copy 10/unsafe + 1:address:number-or-point/lookup <- merge 0/number, 34 +] ++mem: storing 0 in location 10 ++mem: storing 34 in location 11 + +:(before "Update size_mismatch Check for MERGE(x) +canonize(x); + +//:: abbreviation for '/lookup': a prefix '*' + +:(scenario lookup_abbreviation) +def main [ + 1:address:number <- copy 2/unsafe + 2:number <- copy 34 + 3:number <- copy *1:address:number +] ++parse: ingredient: {1: ("address" "number"), "lookup": ()} ++mem: storing 34 in location 3 + +:(before "End Parsing reagent") +{ + while (!name.empty() && name.at(0) == '*') { + name.erase(0, 1); + properties.push_back(pair<string, string_tree*>("lookup", NULL)); + } + if (name.empty()) + raise << "illegal name " << original_string << '\n' << end(); +} + +//:: helpers for debugging + +:(before "End Primitive Recipe Declarations") +_DUMP, +:(before "End Primitive Recipe Numbers") +put(Recipe_ordinal, "$dump", _DUMP); +:(before "End Primitive Recipe Implementations") +case _DUMP: { + reagent after_canonize = current_instruction().ingredients.at(0); + canonize(after_canonize); + cerr << maybe(current_recipe_name()) << current_instruction().ingredients.at(0).name << ' ' << no_scientific(current_instruction().ingredients.at(0).value) << " => " << no_scientific(after_canonize.value) << " => " << no_scientific(get_or_insert(Memory, after_canonize.value)) << '\n'; + break; +} + +//: grab an address, and then dump its value at intervals +//: useful for tracking down memory corruption (writing to an out-of-bounds address) +:(before "End Globals") +int Bar = -1; +:(before "End Primitive Recipe Declarations") +_BAR, +:(before "End Primitive Recipe Numbers") +put(Recipe_ordinal, "$bar", _BAR); +:(before "End Primitive Recipe Implementations") +case _BAR: { + if (current_instruction().ingredients.empty()) { + if (Bar != -1) cerr << Bar << ": " << no_scientific(get_or_insert(Memory, Bar)) << '\n'; + else cerr << '\n'; + } + else { + reagent tmp = current_instruction().ingredients.at(0); + canonize(tmp); + Bar = tmp.value; + } + break; +} diff --git a/037new.cc b/034new.cc index ec30fee1..ec30fee1 100644 --- a/037new.cc +++ b/034new.cc diff --git a/038location_array.cc b/035location_array.cc index 86cf8e97..86cf8e97 100644 --- a/038location_array.cc +++ b/035location_array.cc diff --git a/057shape_shifting_container.cc b/057shape_shifting_container.cc index acb3c7cb..40ec3521 100644 --- a/057shape_shifting_container.cc +++ b/057shape_shifting_container.cc @@ -190,9 +190,9 @@ def main [ :(before "End element_type Special-cases") if (contains_type_ingredient(element)) { - if (!canonized_base.type->right) - raise << "illegal type " << names_to_string(canonized_base.type) << " seems to be missing a type ingredient or three\n" << end(); - replace_type_ingredients(element.type, canonized_base.type->right, info); + if (!base.type->right) + raise << "illegal type " << names_to_string(base.type) << " seems to be missing a type ingredient or three\n" << end(); + replace_type_ingredients(element.type, base.type->right, info); } :(code) @@ -541,7 +541,7 @@ def main [ :(before "End variant_type Special-cases") if (contains_type_ingredient(element)) { - if (!canonized_base.type->right) - raise << "illegal type '" << to_string(canonized_base.type) << "' seems to be missing a type ingredient or three\n" << end(); - replace_type_ingredients(element.type, canonized_base.type->right, info); + if (!base.type->right) + raise << "illegal type '" << to_string(base.type) << "' seems to be missing a type ingredient or three\n" << end(); + replace_type_ingredients(element.type, base.type->right, info); } |