diff options
-rw-r--r-- | cpp/.traces/get_address_indirect | 31 | ||||
-rw-r--r-- | cpp/.traces/get_indirect | 29 | ||||
-rw-r--r-- | cpp/018address | 76 |
3 files changed, 136 insertions, 0 deletions
diff --git a/cpp/.traces/get_address_indirect b/cpp/.traces/get_address_indirect new file mode 100644 index 00000000..60ddfd9b --- /dev/null +++ b/cpp/.traces/get_address_indirect @@ -0,0 +1,31 @@ +parse/0: instruction: 1 +parse/0: ingredient: {name: "2", type: 0} +parse/0: product: {name: "1", type: 1} +parse/0: instruction: 1 +parse/0: ingredient: {name: "34", type: 0} +parse/0: product: {name: "2", type: 1} +parse/0: instruction: 1 +parse/0: ingredient: {name: "35", type: 0} +parse/0: product: {name: "3", type: 1} +parse/0: instruction: 18 +parse/0: ingredient: {name: "1", type: 2-4, property: deref:} +parse/0: ingredient: {name: "0", type: 0} +parse/0: product: {name: "4", type: 1} +run/0: instruction 0 +run/0: ingredient 0 is 2 +mem/0: storing in location 1 +run/0: instruction 1 +run/0: ingredient 0 is 34 +mem/0: storing in location 2 +run/0: instruction 2 +run/0: ingredient 0 is 35 +mem/0: storing in location 3 +run/0: instruction 3 +run/0: ingredient 0 is 1 +mem/0: location 1 is 2 +run/0: ingredient 1 is 0 +run/0: address to copy is 2 +run/0: its type is 1 +mem/0: location 2 is 34 +run/0: product 0 is 34 +mem/0: storing in location 4 diff --git a/cpp/.traces/get_indirect b/cpp/.traces/get_indirect new file mode 100644 index 00000000..6d7b2c24 --- /dev/null +++ b/cpp/.traces/get_indirect @@ -0,0 +1,29 @@ +parse/0: instruction: 1 +parse/0: ingredient: {name: "2", type: 0} +parse/0: product: {name: "1", type: 1} +parse/0: instruction: 1 +parse/0: ingredient: {name: "34", type: 0} +parse/0: product: {name: "2", type: 1} +parse/0: instruction: 1 +parse/0: ingredient: {name: "35", type: 0} +parse/0: product: {name: "3", type: 1} +parse/0: instruction: 19 +parse/0: ingredient: {name: "1", type: 2-4, property: deref:} +parse/0: ingredient: {name: "0", type: 0} +parse/0: product: {name: "4", type: 1} +run/0: instruction 0 +run/0: ingredient 0 is 2 +mem/0: storing in location 1 +run/0: instruction 1 +run/0: ingredient 0 is 34 +mem/0: storing in location 2 +run/0: instruction 2 +run/0: ingredient 0 is 35 +mem/0: storing in location 3 +run/0: instruction 3 +run/0: ingredient 0 is 1 +mem/0: location 1 is 2 +run/0: ingredient 1 is 0 +run/0: address to copy is 2 +run/0: product 0 is 2 +mem/0: storing in location 4 diff --git a/cpp/018address b/cpp/018address index 5e53a8ce..621ad7e3 100644 --- a/cpp/018address +++ b/cpp/018address @@ -94,3 +94,79 @@ reagent deref(reagent x) { } return result; } + +:(scenario "get_address_indirect") +# 'get' can read from record address +recipe main [ + 1:integer <- copy 2:literal + 2:integer <- copy 34:literal + 3:integer <- copy 35:literal + 4:integer <- get 1:address:point/deref, 0:offset +] ++run: instruction 3 ++run: address to copy is 2 ++run: product 0 is 34 ++mem: storing in location 4 + +:(replace{} "case GET:") +case GET: { + trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; + reagent base = canonize(instructions[pc].ingredients[0]); + int base_address = to_int(base.name); + int base_type = base.types[0]; + assert(Type[base_type].is_record); + trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name; + assert(instructions[pc].ingredients[1].types.size() == 1); + assert(instructions[pc].ingredients[1].types[0] == 0); // must be literal + size_t offset = to_int(instructions[pc].ingredients[1].name); + int src = base_address; + for (size_t i = 0; i < offset; ++i) { + src += size_of(reagent(Type[base_type].elements[i][0])); + } + trace("run") << "address to copy is " << src; + assert(Type[base_type].is_record); + assert(Type[base_type].elements.size() > offset); + int src_type = Type[base_type].elements[offset][0]; + trace("run") << "its type is " << src_type; + ostringstream s; + s << src; + reagent tmp(s.str()); + tmp.types.push_back(src_type); + vector<int> result(read_memory(tmp)); + trace("run") << "product 0 is " << result[0]; + write_memory(instructions[pc].products[0], result); + break; +} + +:(scenario "get_indirect") +# 'get' can read from record address +recipe main [ + 1:integer <- copy 2:literal + 2:integer <- copy 34:literal + 3:integer <- copy 35:literal + 4:integer <- get-address 1:address:point/deref, 0:offset +] ++run: instruction 3 ++run: address to copy is 2 ++run: product 0 is 2 + +:(replace{} "case GET_ADDRESS:") +case GET_ADDRESS: { + trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; + reagent base = canonize(instructions[pc].ingredients[0]); + int base_address = to_int(base.name); + int base_type = base.types[0]; + assert(Type[base_type].is_record); + trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name; + assert(instructions[pc].ingredients[1].types.size() == 1); + assert(instructions[pc].ingredients[1].types[0] == 0); // must be literal + size_t offset = to_int(instructions[pc].ingredients[1].name); + int src = base_address+offset; + trace("run") << "address to copy is " << src; + vector<int> result; + result.push_back(src); + trace("run") << "product 0 is " << result[0]; + write_memory(instructions[pc].products[0], result); + break; +} + |