diff options
-rw-r--r-- | 031container.cc | 127 | ||||
-rw-r--r-- | 042name.cc | 5 | ||||
-rw-r--r-- | 057shape_shifting_container.cc | 13 | ||||
-rw-r--r-- | 063wait.cc | 142 | ||||
-rw-r--r-- | 072channel.mu | 4 |
5 files changed, 143 insertions, 148 deletions
diff --git a/031container.cc b/031container.cc index 3a525f80..14a90501 100644 --- a/031container.cc +++ b/031container.cc @@ -281,133 +281,6 @@ def main [ ] # just don't die -//:: To write to elements of containers, you need their address. - -:(scenario get_address) -def main [ - 12:number <- copy 34 - 13:number <- copy 35 - 15:address:number <- get-address 12:point, 1:offset -] -+mem: storing 13 in location 15 - -:(before "End Primitive Recipe Declarations") -GET_ADDRESS, -:(before "End Primitive Recipe Numbers") -put(Recipe_ordinal, "get-address", GET_ADDRESS); -:(before "End Primitive Recipe Checks") -case GET_ADDRESS: { - if (SIZE(inst.ingredients) != 2) { - raise << maybe(get(Recipe, r).name) << "'get-address' expects exactly 2 ingredients in '" << to_original_string(inst) << "'\n" << end(); - break; - } - reagent base = inst.ingredients.at(0); - if (!canonize_type(base)) break; - 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-address' 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); - if (!is_literal(offset) || !is_mu_scalar(offset)) { - raise << maybe(get(Recipe, r).name) << "second ingredient of 'get-address' should have type 'offset', but got " << inst.ingredients.at(1).original_string << '\n' << end(); - break; - } - int offset_value = 0; - if (is_integer(offset.name)) { // later layers permit non-integer offsets - offset_value = to_integer(offset.name); - if (offset_value < 0 || offset_value >= SIZE(get(Type, base_type).elements)) { - raise << maybe(get(Recipe, r).name) << "invalid offset " << offset_value << " for " << get(Type, base_type).name << '\n' << end(); - break; - } - } - else { - offset_value = offset.value; - } - // same type as for GET.. - reagent element = element_type(base, offset_value); - // ..except for an address at the start - element.type = new type_tree("address", get(Type_ordinal, "address"), element.type); - if (!types_coercible(inst.products.at(0), element)) { - raise << maybe(get(Recipe, r).name) << "'get-address " << base.original_string << ", " << offset.original_string << "' should write to " << names_to_string_without_quotes(element.type) << " but " << inst.products.at(0).name << " has type " << names_to_string_without_quotes(inst.products.at(0).type) << '\n' << end(); - break; - } - break; -} -:(before "End Primitive Recipe Implementations") -case GET_ADDRESS: { - reagent base = current_instruction().ingredients.at(0); - canonize(base); - 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; - } - type_ordinal base_type = base.type->value; - int offset = ingredients.at(1).at(0); - if (offset < 0 || offset >= SIZE(get(Type, base_type).elements)) break; // copied from Check above - int result = base_address; - for (int i = 0; i < offset; ++i) - result += size_of(element_type(base, i)); - trace(9998, "run") << "address to copy is " << result << end(); - products.resize(1); - products.at(0).push_back(result); - break; -} - -:(scenario get_address_out_of_bounds) -% Hide_errors = true; -def main [ - 12:number <- copy 34 - 13:number <- copy 35 - 14:number <- copy 36 - get-address 12:point-number/raw, 2:offset # point-number occupies 3 locations but has only 2 fields; out of bounds -] -+error: main: invalid offset 2 for point-number - -:(scenario get_address_out_of_bounds_2) -% Hide_errors = true; -def main [ - 12:number <- copy 34 - 13:number <- copy 35 - 14:number <- copy 36 - get-address 12:point-number/raw, -1:offset -] -+error: main: invalid offset -1 for point-number - -:(scenario get_address_product_type_mismatch) -% Hide_errors = true; -container boolbool [ - x:boolean - y:boolean -] -def main [ - 12:boolean <- copy 1 - 13:boolean <- copy 0 - 15:boolean <- get-address 12:boolbool, 1:offset -] -+error: main: 'get-address 12:boolbool, 1:offset' should write to (address boolean) but 15 has type boolean - -:(scenario get_address_indirect) -# 'get-address' can read from container address -def main [ - 1:number <- copy 2 - 2:number <- copy 34 - 3:number <- copy 35 - 4:address:number <- get-address 1:address:point/lookup, 0:offset -] -+mem: storing 2 in location 4 - -:(scenario get_address_indirect2) -def main [ - 1:number <- copy 2 - 2:number <- copy 34 - 3:number <- copy 35 - 4:address:number <- copy 5/unsafe - 4:address:number/lookup <- get-address 1:address:point/lookup, 0:offset -] -+mem: storing 2 in location 5 - //:: To write to elements of containers, use 'put'. :(scenario put) diff --git a/042name.cc b/042name.cc index ade9c069..d697b40e 100644 --- a/042name.cc +++ b/042name.cc @@ -215,7 +215,8 @@ def main [ -error: main: mixing variable names and numeric addresses $error: 0 -//:: Support element names for containers in 'get' and 'get-address' and 'put'. +//:: Support element names for containers in 'get' and 'get-location' and 'put'. +//: (get-location is implemented later) :(scenario transform_names_transforms_container_elements) def main [ @@ -228,7 +229,7 @@ def main [ :(before "End transform_names(inst) Special-cases") // replace element names of containers with offsets -if (inst.name == "get" || inst.name == "get-address" || inst.name == "put") { +if (inst.name == "get" || inst.name == "get-location" || inst.name == "put") { //: avoid raising any errors here; later layers will support overloading new //: instructions with the same names (static dispatch), which could lead to //: spurious errors diff --git a/057shape_shifting_container.cc b/057shape_shifting_container.cc index 80cd43b7..acb3c7cb 100644 --- a/057shape_shifting_container.cc +++ b/057shape_shifting_container.cc @@ -459,19 +459,6 @@ def main [ ] +error: illegal type "foo" seems to be missing a type ingredient or three -//: get-address similarly - -:(scenario get_address_on_shape_shifting_container) -container foo:_t [ - x:_t - y:number -] -def main [ - 10:foo:point <- merge 14, 15, 16 - 1:address:number <- get-address 10:foo:point, 1:offset -] -+mem: storing 12 in location 1 - //: 'merge' on shape-shifting containers :(scenario merge_check_shape_shifting_container_containing_exclusive_container) diff --git a/063wait.cc b/063wait.cc index 1adc9ad4..71e3123e 100644 --- a/063wait.cc +++ b/063wait.cc @@ -7,8 +7,8 @@ def f1 [ 1:number <- copy 0 start-running f2 - 2:address:number <- copy 1/unsafe - wait-for-location 2:address:number + 2:location <- copy 1/unsafe + wait-for-location 2:location # now wait for f2 to run and modify location 1 before using its value 3:number <- copy 1:number ] @@ -62,8 +62,8 @@ case WAIT_FOR_LOCATION: { raise << maybe(get(Recipe, r).name) << "'wait-for-location' requires exactly one ingredient, but got " << to_original_string(inst) << '\n' << end(); break; } - if (!is_mu_address(inst.ingredients.at(0))) { - raise << maybe(get(Recipe, r).name) << "'wait-for-location' requires an address ingredient, but got " << inst.ingredients.at(0).original_string << '\n' << end(); + if (!is_mu_location(inst.ingredients.at(0))) { + raise << maybe(get(Recipe, r).name) << "'wait-for-location' requires a location ingredient, but got " << inst.ingredients.at(0).original_string << '\n' << end(); } break; } @@ -90,6 +90,140 @@ for (int i = 0; i < SIZE(Routines); ++i) { } } +//: primitive to help compute locations to wait for +//: only supports elements inside containers, no arrays or containers within +//: containers yet. + +:(scenario get_location) +def main [ + 12:number <- copy 34 + 13:number <- copy 35 + 15:location <- get-location 12:point, 1:offset +] ++mem: storing 13 in location 15 + +:(before "End Primitive Recipe Declarations") +GET_LOCATION, +:(before "End Primitive Recipe Numbers") +put(Recipe_ordinal, "get-location", GET_LOCATION); +:(before "End Primitive Recipe Checks") +case GET_LOCATION: { + if (SIZE(inst.ingredients) != 2) { + raise << maybe(get(Recipe, r).name) << "'get-location' expects exactly 2 ingredients in '" << to_original_string(inst) << "'\n" << end(); + break; + } + reagent base = inst.ingredients.at(0); + if (!canonize_type(base)) break; + 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-location' 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); + if (!is_literal(offset) || !is_mu_scalar(offset)) { + raise << maybe(get(Recipe, r).name) << "second ingredient of 'get-location' should have type 'offset', but got " << inst.ingredients.at(1).original_string << '\n' << end(); + break; + } + int offset_value = 0; + if (is_integer(offset.name)) { // later layers permit non-integer offsets + offset_value = to_integer(offset.name); + if (offset_value < 0 || offset_value >= SIZE(get(Type, base_type).elements)) { + raise << maybe(get(Recipe, r).name) << "invalid offset " << offset_value << " for " << get(Type, base_type).name << '\n' << end(); + break; + } + } + else { + offset_value = offset.value; + } + if (inst.products.empty()) break; + if (!is_mu_location(inst.products.at(0))) { + raise << maybe(get(Recipe, r).name) << "'get-location " << base.original_string << ", " << offset.original_string << "' should write to type location but " << inst.products.at(0).name << " has type " << names_to_string_without_quotes(inst.products.at(0).type) << '\n' << end(); + break; + } + break; +} +:(before "End Primitive Recipe Implementations") +case GET_LOCATION: { + reagent base = current_instruction().ingredients.at(0); + canonize(base); + 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; + } + type_ordinal base_type = base.type->value; + int offset = ingredients.at(1).at(0); + if (offset < 0 || offset >= SIZE(get(Type, base_type).elements)) break; // copied from Check above + int result = base_address; + for (int i = 0; i < offset; ++i) + result += size_of(element_type(base, i)); + trace(9998, "run") << "address to copy is " << result << end(); + products.resize(1); + products.at(0).push_back(result); + break; +} + +:(code) +bool is_mu_location(reagent x) { + if (!canonize_type(x)) return false; + if (!x.type) return false; + if (x.type->right) return false; + return x.type->value == get(Type_ordinal, "location"); +} + +:(scenario get_location_out_of_bounds) +% Hide_errors = true; +def main [ + 12:number <- copy 34 + 13:number <- copy 35 + 14:number <- copy 36 + get-location 12:point-number/raw, 2:offset # point-number occupies 3 locations but has only 2 fields; out of bounds +] ++error: main: invalid offset 2 for point-number + +:(scenario get_location_out_of_bounds_2) +% Hide_errors = true; +def main [ + 12:number <- copy 34 + 13:number <- copy 35 + 14:number <- copy 36 + get-location 12:point-number/raw, -1:offset +] ++error: main: invalid offset -1 for point-number + +:(scenario get_location_product_type_mismatch) +% Hide_errors = true; +container boolbool [ + x:boolean + y:boolean +] +def main [ + 12:boolean <- copy 1 + 13:boolean <- copy 0 + 15:boolean <- get-location 12:boolbool, 1:offset +] ++error: main: 'get-location 12:boolbool, 1:offset' should write to type location but 15 has type boolean + +:(scenario get_location_indirect) +# 'get-location' can read from container address +def main [ + 1:number <- copy 2 + 2:number <- copy 34 + 3:number <- copy 35 + 4:location <- get-location 1:address:point/lookup, 0:offset +] ++mem: storing 2 in location 4 + +:(scenario get_location_indirect2) +def main [ + 1:number <- copy 2 + 2:number <- copy 34 + 3:number <- copy 35 + 4:address:number <- copy 5/unsafe + 4:address:location/lookup <- get-location 1:address:point/lookup, 0:offset +] ++mem: storing 2 in location 5 + //: also allow waiting on a routine to stop running :(scenario wait_for_routine) diff --git a/072channel.mu b/072channel.mu index 5fcbc98d..81bc1479 100644 --- a/072channel.mu +++ b/072channel.mu @@ -65,7 +65,7 @@ def write out:address:shared:sink:_elem, val:_elem -> out:address:shared:sink:_e # block if chan is full full:boolean <- channel-full? chan break-unless full - full-address:address:number <- get-address *chan, first-full:offset + full-address:location <- get-location *chan, first-full:offset wait-for-location full-address } # store val @@ -95,7 +95,7 @@ def read in:address:shared:source:_elem -> result:_elem, in:address:shared:sourc # block if chan is empty empty?:boolean <- channel-empty? chan break-unless empty? - free-address:address:number <- get-address *chan, first-free:offset + free-address:location <- get-location *chan, first-free:offset wait-for-location free-address } # pull result off |