:(scenario copy_indirect)
recipe main [
1:address:number <- copy 2:literal
2:number <- copy 34:literal
3:number <- copy 1:address:number/deref
]
+run: instruction main/2
+mem: location 1 is 2
+mem: location 2 is 34
+mem: storing 34 in location 3
:(before "index_t base = x.value" following "vector<double> read_memory(reagent x)")
x = canonize(x);
:(scenario store_indirect)
recipe main [
1:address:number <- copy 2:literal
1:address:number/deref <- copy 34:literal
]
+run: instruction main/1
+mem: location 1 is 2
+mem: storing 34 in location 2
:(before "index_t base = x.value" following "void write_memory(reagent x, vector<double> data)")
x = canonize(x);
:(code)
reagent canonize(reagent x) {
if (isa_literal(x)) return x;
reagent r = x;
while (has_property(r, "deref"))
r = deref(r);
return r;
}
reagent deref(reagent x) {
static const type_number ADDRESS = Type_number["address"];
reagent result;
assert(x.types.at(0) == ADDRESS);
result.set_value(Memory[x.value]);
trace("mem") << "location " << x.value << " is " << result.value;
copy(++x.types.begin(), x.types.end(), inserter(result.types, result.types.begin()));
index_t i = 0;
size_t len = x.properties.size();
for (i = 0; i < len; ++i) {
if (x.properties.at(i).first == "deref") break;
result.properties.push_back(x.properties.at(i));
}
++i;
for (; i < len; ++i) {
result.properties.push_back(x.properties.at(i));
}
return result;
}
:(scenario get_indirect)
recipe main [
1:number <- copy 2:literal
2:number <- copy 34:literal
3:number <- copy 35:literal
4:number <- get 1:address:point/deref, 0:offset
]
+run: instruction main/3
+run: address to copy is 2
+run: product 0 is 4
+mem: storing 34 in location 4
:(scenario include_nonderef_properties)
recipe main [
1:number <- copy 2:literal
2:number <- copy 34:literal
3:number <- copy 35:literal
4:number <- get 1:address:point/deref/foo, 0:offset
]
+run: instruction main/3
+run: address to copy is 2
+run: product 0 is 4
+mem: storing 34 in location 4
:(after "reagent base = " following "case GET:")
base = canonize(base);
:(scenario get_address_indirect)
recipe main [
1:number <- copy 2:literal
2:number <- copy 34:literal
3:number <- copy 35:literal
4:number <- get-address 1:address:point/deref, 0:offset
]
+run: instruction main/3
+run: address to copy is 2
+run: product 0 is 4
:(after "reagent base = " following "case GET_ADDRESS:")
base = canonize(base);
:(code)
bool has_property(reagent x, string name) {
for (index_t i = 0; i < x.properties.size(); ++i) {
if (x.properties.at(i).first == name) return true;
}
return false;
}
vector<string> property(const reagent& r, const string& name) {
for (index_t p = 0; p != r.properties.size(); ++p) {
if (r.properties.at(p).first == name)
return r.properties.at(p).second;
}
return vector<string>();
}