1
2
3
4
5
6
7
8
9
10
11
12 :(before "End Mu Types Initialization")
13 put(Type_abbreviations, "space", new_type_tree("address:array:location"));
14
15 :(scenario set_default_space)
16
17
18 def main [
19
20 10:num <- copy 5
21 default-space:space <- copy 10/unsafe
22 1:num <- copy 23
23 ]
24 +mem: storing 23 in location 12
25
26 :(scenario lookup_sidesteps_default_space)
27 def main [
28
29 2000:num <- copy 34
30
31 1000:num <- copy 5
32
33 default-space:space <- copy 1000/unsafe
34 1:&:num <- copy 2000/unsafe
35 8:num/raw <- copy *1:&:num
36 ]
37 +mem: storing 34 in location 8
38
39
40
41 :(scenario convert_names_passes_default_space)
42 % Hide_errors = true;
43 def main [
44 default-space:num, x:num <- copy 0, 1
45 ]
46 +name: assign x 1
47 -name: assign default-space 1
48
49 :(before "End is_disqualified Special-cases")
50 if (x.name == "default-space")
51 x.initialized = true;
52 :(before "End is_special_name Special-cases")
53 if (s == "default-space") return true;
54
55
56
57 :(before "End call Fields")
58 int default_space;
59 :(before "End call Constructor")
60 default_space = 0;
61
62 :(before "Begin canonize(x) Lookups")
63 absolutize(x);
64 :(code)
65 void absolutize(reagent& x) {
66 if (is_raw(x) || is_dummy(x)) return;
67 if (x.name == "default-space") return;
68 if (!x.initialized)
69 raise << to_original_string(current_instruction()) << ": reagent not initialized: '" << x.original_string << "'\n" << end();
70 x.set_value(address(x.value, space_base(x)));
71 x.properties.push_back(pair<string, string_tree*>("raw", NULL));
72 assert(is_raw(x));
73 }
74
75
76 int space_base(const reagent& x) {
77 return current_call().default_space ? current_call().default_space : 0;
78 }
79
80 int address(int offset, int base) {
81 assert(offset >= 0);
82 if (base == 0) return offset;
83 int size = get_or_insert(Memory, base);
84 if (offset >= size) {
85
86 raise << current_recipe_name() << ": location " << offset << " is out of bounds " << size << " at " << base << '\n' << end();
87 DUMP("");
88 exit(1);
89 return 0;
90 }
91 return base + 1 + offset;
92 }
93
94
95
96 :(after "Begin Preprocess write_memory(x, data)")
97 if (x.name == "default-space") {
98 if (!scalar(data) || !is_mu_space(x))
99 raise << maybe(current_recipe_name()) << "'default-space' should be of type address:array:location, but is " << to_string(x.type) << '\n' << end();
100 current_call().default_space = data.at(0);
101 return;
102 }
103 :(code)
104 bool is_mu_space(reagent x) {
105 canonize_type(x);
106 if (!is_compound_type_starting_with(x.type, "address")) return false;
107 drop_from_type(x, "address");
108 if (!is_compound_type_starting_with(x.type, "array")) return false;
109 drop_from_type(x, "array");
110 return x.type && x.type->atom && x.type->name == "location";
111 }
112
113 :(scenario get_default_space)
114 def main [
115 default-space:space <- copy 10/unsafe
116 1:space/raw <- copy default-space:space
117 ]
118 +mem: storing 10 in location 1
119
120 :(after "Begin Preprocess read_memory(x)")
121 if (x.name == "default-space") {
122 vector<double> result;
123 result.push_back(current_call().default_space);
124 return result;
125 }
126
127
128
129 :(scenario lookup_sidesteps_default_space_in_get)
130 def main [
131
132 2000:num <- copy 34
133 2001:num <- copy 35
134
135 1000:num <- copy 5
136
137 default-space:space <- copy 1000/unsafe
138 1:&:point <- copy 2000/unsafe
139 9:num/raw <- get *1:&:point, 1:offset
140 ]
141 +mem: storing 35 in location 9
142
143 :(before "Read element" following "case GET:")
144 element.properties.push_back(pair<string, string_tree*>("raw", NULL));
145
146
147
148 :(scenario lookup_sidesteps_default_space_in_index)
149 def main [
150
151 2000:num <- copy 2
152 2001:num <- copy 34
153 2002:num <- copy 35
154
155 1000:num <- copy 5
156
157 default-space:space <- copy 1000/unsafe
158 1:&:@:num <- copy 2000/unsafe
159 9:num/raw <- index *1:&:@:num, 1
160 ]
161 +mem: storing 35 in location 9
162
163 :(before "Read element" following "case INDEX:")
164 element.properties.push_back(pair<string, string_tree*>("raw", NULL));
165
166
167
168
169 :(scenario local_scope)
170 def main [
171 local-scope
172 x:num <- copy 0
173 y:num <- copy 3
174 ]
175
176 +mem: array length is 3
177
178 :(before "End is_disqualified Special-cases")
179 if (x.name == "number-of-locals")
180 x.initialized = true;
181 :(before "End is_special_name Special-cases")
182 if (s == "number-of-locals") return true;
183
184 :(before "End Rewrite Instruction(curr, recipe result)")
185
186
187
188
189
190 if (curr.name == "local-scope") {
191 rewrite_default_space_instruction(curr);
192 }
193 :(code)
194 void rewrite_default_space_instruction(instruction& curr) {
195 if (!curr.ingredients.empty())
196 raise << "'" << to_original_string(curr) << "' can't take any ingredients\n" << end();
197 curr.name = "new";
198 curr.ingredients.push_back(reagent("location:type"));
199 curr.ingredients.push_back(reagent("number-of-locals:literal"));
200 if (!curr.products.empty())
201 raise << "local-scope can't take any results\n" << end();
202 curr.products.push_back(reagent("default-space:space"));
203 }
204 :(after "Begin Preprocess read_memory(x)")
205 if (x.name == "number-of-locals") {
206 vector<double> result;
207 result.push_back(Name[get(Recipe_ordinal, current_recipe_name())][""]);
208 if (result.back() == 0)
209 raise << "no space allocated for default-space in recipe " << current_recipe_name() << "; are you using names?\n" << end();
210 return result;
211 }
212 :(after "Begin Preprocess write_memory(x, data)")
213 if (x.name == "number-of-locals") {
214 raise << maybe(current_recipe_name()) << "can't write to special name 'number-of-locals'\n" << end();
215 return;
216 }
217
218
219
220 :(before "End Globals")
221 bool Hide_missing_default_space_errors = true;
222 :(before "End Checks")
223 Transform.push_back(check_default_space);
224 :(code)
225 void check_default_space(const recipe_ordinal r) {
226 if (Hide_missing_default_space_errors) return;
227 const recipe& caller = get(Recipe, r);
228
229
230 if (!contains_non_special_name(r)) return;
231 trace(9991, "transform") << "--- check that recipe " << caller.name << " sets default-space" << end();
232 if (caller.steps.empty()) return;
233 if (!starts_by_setting_default_space(caller))
234 raise << caller.name << " does not seem to start with 'local-scope' or 'default-space'\n" << end();
235 }
236 bool starts_by_setting_default_space(const recipe& r) {
237 return !r.steps.empty()
238 && !r.steps.at(0).products.empty()
239 && r.steps.at(0).products.at(0).name == "default-space";
240 }
241
242 :(after "Load Mu Prelude")
243 Hide_missing_default_space_errors = false;
244 :(after "Test Runs")
245 Hide_missing_default_space_errors = true;
246 :(after "Running Main")
247 Hide_missing_default_space_errors = false;
248
249 :(code)
250 bool contains_non_special_name(const recipe_ordinal r) {
251 for (map<string, int>::iterator p = Name[r].begin(); p != Name[r].end(); ++p) {
252 if (p->first.empty()) continue;
253 if (p->first.find("stash_") == 0) continue;
254 if (!is_special_name(p->first))
255 return true;
256 }
257 return false;
258 }
259
260
261 bool operator==(const reagent& a, const reagent& b) {
262 if (a.name != b.name) return false;
263 if (property(a, "space") != property(b, "space")) return false;
264 return true;
265 }
266
267 bool operator<(const reagent& a, const reagent& b) {
268 int aspace = 0, bspace = 0;
269 if (has_property(a, "space")) aspace = to_integer(property(a, "space")->value);
270 if (has_property(b, "space")) bspace = to_integer(property(b, "space")->value);
271 if (aspace != bspace) return aspace < bspace;
272 return a.name < b.name;
273 }