about summary refs log tree commit diff stats
path: root/cpp/021address
diff options
context:
space:
mode:
Diffstat (limited to 'cpp/021address')
-rw-r--r--cpp/021address117
1 files changed, 117 insertions, 0 deletions
diff --git a/cpp/021address b/cpp/021address
new file mode 100644
index 00000000..22355fc6
--- /dev/null
+++ b/cpp/021address
@@ -0,0 +1,117 @@
+//: Instructions can read from addresses pointing at other locations using the
+//: 'deref' property.
+:(scenario "copy_indirect")
+recipe main [
+  1:address:integer <- copy 2:literal
+  2:integer <- copy 34:literal
+  # This loads location 1 as an address and looks up *that* location.
+  3:integer <- copy 1:address:integer/deref
+]
++run: instruction main/2
++mem: location 1 is 2
++mem: location 2 is 34
++mem: storing 34 in location 3
+
+:(before "int base = x.value" following "vector<int> read_memory(reagent x)")
+x = canonize(x);
+
+//: similarly, write to addresses pointing at other locations using the
+//: 'deref' property
+:(scenario "store_indirect")
+recipe main [
+  1:address:integer <- copy 2:literal
+  1:address:integer/deref <- copy 34:literal
+]
++run: instruction main/1
++mem: location 1 is 2
++mem: storing 34 in location 2
+
+:(before "int base = x.value" following "void write_memory(reagent x, vector<int> data)")
+x = canonize(x);
+
+:(code)
+reagent canonize(reagent x) {
+//?   cout << "canonize\n"; //? 1
+  reagent r = x;
+//?   cout << x.to_string() << " => " << r.to_string() << '\n'; //? 1
+  while (has_property(r, "deref"))
+    r = deref(r);
+  return r;
+}
+
+bool has_property(reagent x, string name) {
+  for (size_t i = 0; i < x.properties.size(); ++i) {
+    if (x.properties[i].first == name) return true;
+  }
+  return false;
+}
+
+reagent deref(reagent x) {
+//?   cout << "deref: " << x.to_string() << "\n"; //? 2
+  static const int ADDRESS = Type_number["address"];
+  reagent result;
+  assert(x.types[0] == ADDRESS);
+
+  // compute value
+  result.set_value(Memory[x.value]);
+  trace("mem") << "location " << x.value << " is " << result.value;
+
+  // populate types
+  copy(++x.types.begin(), x.types.end(), inserter(result.types, result.types.begin()));
+
+  // drop-one 'deref'
+  int i = 0;
+  int len = x.properties.size();
+  for (i = 0; i < len; ++i) {
+    if (x.properties[i].first == "deref") break;
+    result.properties.push_back(x.properties[i]);
+  }
+  ++i;  // skip first deref
+  for (; i < len; ++i) {
+    result.properties.push_back(x.properties[i]);
+  }
+  return result;
+}
+
+//: 'get' can read from container address
+:(scenario "get_indirect")
+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 main/3
++run: address to copy is 2
++run: product 0 is 34
++mem: storing 34 in location 4
+
+:(scenario "include_nonderef_properties")
+recipe main [
+  1:integer <- copy 2:literal
+  2:integer <- copy 34:literal
+  3:integer <- copy 35:literal
+  4:integer <- get 1:address:point/deref/foo, 0:offset
+]
++run: instruction main/3
++run: address to copy is 2
++run: product 0 is 34
++mem: storing 34 in location 4
+
+:(after "reagent base = " following "case GET:")
+base = canonize(base);
+
+:(scenario "get_address_indirect")
+# 'get' can read from container 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 main/3
++run: address to copy is 2
++run: product 0 is 2
+
+:(after "reagent base = " following "case GET_ADDRESS:")
+base = canonize(base);
s revision' href='/gbmor/getwtxt/blame/svc/handlers.go?h=v0.4.15&id=c9a35a4a6879f8070ba5d1cd0dda74e9cf99e702'>^
adee7fa ^
3d2994b ^
69adb21 ^
3d2994b ^

3da400b ^
adee7fa ^
69adb21 ^
adee7fa ^
3d2994b ^
1168570 ^
3d2994b ^

adee7fa ^
69adb21 ^

adee7fa ^

e9d4a6b ^
1168570 ^





3d2994b ^
1168570 ^




3d2994b ^
1168570 ^

90638ac ^


824556a ^
90638ac ^


90638ac ^






824556a ^
90638ac ^





1168570 ^

824556a ^
1168570 ^



824556a ^
1168570 ^





824556a ^
90638ac ^

824556a ^




90638ac ^
824556a ^
90638ac ^



505c5d8 ^


37be25f ^

505c5d8 ^
90638ac ^
37be25f ^

4f0847b ^
90638ac ^
37be25f ^
a9d7d11 ^
505c5d8 ^
90638ac ^
a9d7d11 ^

824556a ^
a9d7d11 ^
505c5d8 ^
78f4d8a ^
505c5d8 ^


a6f47c0 ^

dec003b ^
1168570 ^
505c5d8 ^
f4eb1c7 ^
824556a ^


1168570 ^
1168570 ^



df1d1ef ^
1168570 ^



dec003b ^

824556a ^
dec003b ^

1168570 ^
90638ac ^
dec003b ^

a6f47c0 ^

dec003b ^
1168570 ^
dec003b ^
f4eb1c7 ^
824556a ^
1168570 ^







1168570 ^


eca8525 ^
90638ac ^
1168570 ^

a6f47c0 ^

1168570 ^
a6f47c0 ^
eca8525 ^
1168570 ^
824556a ^
1168570 ^




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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