1
2
3
4
5 :(scenario static_dispatch)
6 def main [
7 7:num/raw <- test 3
8 ]
9 def test a:num -> z:num [
10 z <- copy 1
11 ]
12 def test a:num, b:num -> z:num [
13 z <- copy 2
14 ]
15 +mem: storing 1 in location 7
16
17
18
19
20 :(before "End Globals")
21 map<string, vector<recipe_ordinal> > Recipe_variants;
22 :(before "End One-time Setup")
23 put(Recipe_variants, "main", vector<recipe_ordinal>());
24
25 :(before "End Globals")
26 map<string, vector<recipe_ordinal> > Recipe_variants_snapshot;
27 :(before "End save_snapshots")
28 Recipe_variants_snapshot = Recipe_variants;
29 :(before "End restore_snapshots")
30 Recipe_variants = Recipe_variants_snapshot;
31
32 :(before "End Load Recipe Header(result)")
33
34 if (result.name != "main" && contains_key(Recipe_ordinal, result.name)) {
35 const recipe_ordinal r = get(Recipe_ordinal, result.name);
36 if (!contains_key(Recipe, r) || get(Recipe, r).has_header) {
37 string new_name = matching_variant_name(result);
38 if (new_name.empty()) {
39
40 new_name = next_unused_recipe_name(result.name);
41 put(Recipe_ordinal, new_name, Next_recipe_ordinal++);
42 get_or_insert(Recipe_variants, result.name).push_back(get(Recipe_ordinal, new_name));
43 }
44 trace("load") << "switching " << result.name << " to " << new_name << end();
45 result.name = new_name;
46 result.is_autogenerated = true;
47 }
48 }
49 else {
50
51 put(Recipe_ordinal, result.name, Next_recipe_ordinal++);
52 get_or_insert(Recipe_variants, result.name).push_back(get(Recipe_ordinal, result.name));
53 }
54
55 :(code)
56 string matching_variant_name(const recipe& rr) {
57 const vector<recipe_ordinal>& variants = get_or_insert(Recipe_variants, rr.name);
58 for (int i = 0; i < SIZE(variants); ++i) {
59 if (!contains_key(Recipe, variants.at(i))) continue;
60 const recipe& candidate = get(Recipe, variants.at(i));
61 if (!all_reagents_match(rr, candidate)) continue;
62 return candidate.name;
63 }
64 return "";
65 }
66
67 bool all_reagents_match(const recipe& r1, const recipe& r2) {
68 if (SIZE(r1.ingredients) != SIZE(r2.ingredients)) return false;
69 if (SIZE(r1.products) != SIZE(r2.products)) return false;
70 for (int i = 0; i < SIZE(r1.ingredients); ++i) {
71 expand_type_abbreviations(r1.ingredients.at(i).type);
72 expand_type_abbreviations(r2.ingredients.at(i).type);
73 if (!deeply_equal_type_names(r1.ingredients.at(i), r2.ingredients.at(i)))
74 return false;
75 }
76 for (int i = 0; i < SIZE(r1.products); ++i) {
77 expand_type_abbreviations(r1.products.at(i).type);
78 expand_type_abbreviations(r2.products.at(i).type);
79 if (!deeply_equal_type_names(r1.products.at(i), r2.products.at(i)))
80 return false;
81 }
82 return true;
83 }
84
85 :(before "End Globals")
86 set<string> Literal_type_names;
87 :(before "End One-time Setup")
88 Literal_type_names.insert("number");
89 Literal_type_names.insert("character");
90 :(code)
91 bool deeply_equal_type_names(const reagent& a, const reagent& b) {
92 return deeply_equal_type_names(a.type, b.type);
93 }
94 bool deeply_equal_type_names(const type_tree* a, const type_tree* b) {
95 if (!a) return !b;
96 if (!b) return !a;
97 if (a->atom != b->atom) return false;
98 if (a->atom) {
99 if (a->name == "literal" && b->name == "literal")
100 return true;
101 if (a->name == "literal")
102 return Literal_type_names.find(b->name) != Literal_type_names.end();
103 if (b->name == "literal")
104 return Literal_type_names.find(a->name) != Literal_type_names.end();
105 return a->name == b->name;
106 }
107 return deeply_equal_type_names(a->left, b->left)
108 && deeply_equal_type_names(a->right, b->right);
109 }
110
111 string next_unused_recipe_name(const string& recipe_name) {
112 for (int i = 2; ; ++i) {
113 ostringstream out;
114 out << recipe_name << '_' << i;
115 if (!contains_key(Recipe_ordinal, out.str()))
116 return out.str();
117 }
118 }
119
120
121
122
123 :(scenario static_dispatch_picks_most_similar_variant)
124 def main [
125 7:num/raw <- test 3, 4, 5
126 ]
127 def test a:num -> z:num [
128 z <- copy 1
129 ]
130 def test a:num, b:num -> z:num [
131 z <- copy 2
132 ]
133 +mem: storing 2 in location 7
134
135
136 :(before "End check_or_set_invalid_types")
137 for (int i = 0; i < SIZE(caller.ingredients); ++i)
138 check_or_set_invalid_types(caller.ingredients.at(i).type, maybe(caller.name), "recipe header ingredient");
139 for (int i = 0; i < SIZE(caller.products); ++i)
140 check_or_set_invalid_types(caller.products.at(i).type, maybe(caller.name), "recipe header product");
141
142
143 :(before "End recipe Fields")
144 string original_name;
145
146 :(before "End Load Recipe Name")
147 result.original_name = result.name;
148
149
150 :(after "Transform.push_back(transform_names)")
151 Transform.push_back(resolve_ambiguous_calls);
152
153
154
155
156
157
158
159 :(before "End Globals")
160 list<call> Resolve_stack;
161
162 :(code)
163 void resolve_ambiguous_calls(const recipe_ordinal r) {
164 recipe& caller_recipe = get(Recipe, r);
165 trace(9991, "transform") << "--- resolve ambiguous calls for recipe " << caller_recipe.name << end();
166 for (int index = 0; index < SIZE(caller_recipe.steps); ++index) {
167 instruction& inst = caller_recipe.steps.at(index);
168 if (inst.is_label) continue;
169 resolve_ambiguous_call(r, index, inst, caller_recipe);
170 }
171 }
172
173 void resolve_ambiguous_call(const recipe_ordinal r, int index, instruction& inst, const recipe& caller_recipe) {
174
175 if (non_ghost_size(get_or_insert(Recipe_variants, inst.name)) == 0) return;
176 trace(9992, "transform") << "instruction " << to_original_string(inst) << end();
177 Resolve_stack.push_front(call(r, index));
178 string new_name = best_variant(inst, caller_recipe);
179 if (!new_name.empty())
180 inst.name = new_name;
181 assert(Resolve_stack.front().running_recipe == r);
182 assert(Resolve_stack.front().running_step_index == index);
183 Resolve_stack.pop_front();
184 }
185
186 string best_variant(const instruction& inst, const recipe& caller_recipe) {
187 const vector<recipe_ordinal>& variants = get(Recipe_variants, inst.name);
188 vector<recipe_ordinal> candidates;
189
190
191
192 candidates = strictly_matching_variants(inst, variants);
193 if (!candidates.empty()) return best_variant(inst, candidates).name;
194
195
196
197
198
199
200
201
202 candidates = matching_variants(inst, variants);
203 if (!candidates.empty()) return best_variant(inst, candidates).name;
204
205
206 if (!is_primitive(get(Recipe_ordinal, inst.name))) {
207 if (SIZE(variants) == 1) {
208 raise << maybe(caller_recipe.name) << "types don't match in call for '" << to_original_string(inst) << "'\n" << end();
209 raise << " which tries to call '" << original_header_label(get(Recipe, variants.at(0))) << "'\n" << end();
210 }
211 else {
212 raise << maybe(caller_recipe.name) << "failed to find a matching call for '" << to_original_string(inst) << "'\n" << end();
213 raise << " available variants are:\n" << end();
214 for (int i = 0; i < SIZE(variants); ++i)
215 raise << " " << original_header_label(get(Recipe, variants.at(i))) << '\n' << end();
216 }
217 for (list<call>::iterator p = ++Resolve_stack.begin(); p != Resolve_stack.end(); ++p) {
218 const recipe& specializer_recipe = get(Recipe, p->running_recipe);
219 const instruction& specializer_inst = specializer_recipe.steps.at(p->running_step_index);
220 if (specializer_recipe.name != "interactive")
221 raise << " (from '" << to_original_string(specializer_inst) << "' in " << specializer_recipe.name << ")\n" << end();
222 else
223 raise << " (from '" << to_original_string(specializer_inst) << "')\n" << end();
224
225 if (specializer_inst.products.at(0).name.find("stash_") == 0) {
226 instruction stash_inst;
227 if (next_stash(*p, &stash_inst)) {
228 if (specializer_recipe.name != "interactive")
229 raise << " (part of '" << to_original_string(stash_inst) << "' in " << specializer_recipe.name << ")\n" << end();
230 else
231 raise << " (part of '" << to_original_string(stash_inst) << "')\n" << end();
232 }
233 }
234 }
235 }
236 return "";
237 }
238
239
240 vector<recipe_ordinal> strictly_matching_variants(const instruction& inst, const vector<recipe_ordinal>& variants) {
241 vector<recipe_ordinal> result;
242 for (int i = 0; i < SIZE(variants); ++i) {
243 if (variants.at(i) == -1) continue;
244 trace(9992, "transform") << "checking variant (strict) " << i << ": " << header_label(variants.at(i)) << end();
245 if (all_header_reagents_strictly_match(inst, get(Recipe, variants.at(i))))
246 result.push_back(variants.at(i));
247 }
248 return result;
249 }
250
251 bool all_header_reagents_strictly_match(const instruction& inst, const recipe& variant) {
252 for (int i = 0; i < min(SIZE(inst.ingredients), SIZE(variant.ingredients)); ++i) {
253 if (!types_strictly_match(variant.ingredients.at(i), inst.ingredients.at(i))) {
254 trace(9993, "transform") << "strict match failed: ingredient " << i << end();
255 return false;
256 }
257 }
258 for (int i = 0; i < min(SIZE(inst.products), SIZE(variant.products)); ++i) {
259 if (is_dummy(inst.products.at(i))) continue;
260 if (!types_strictly_match(variant.products.at(i), inst.products.at(i))) {
261 trace(9993, "transform") << "strict match failed: product " << i << end();
262 return false;
263 }
264 }
265 return true;
266 }
267
268
269 vector<recipe_ordinal> matching_variants(const instruction& inst, const vector<recipe_ordinal>& variants) {
270 vector<recipe_ordinal> result;
271 for (int i = 0; i < SIZE(variants); ++i) {
272 if (variants.at(i) == -1) continue;
273 trace(9992, "transform") << "checking variant " << i << ": " << header_label(variants.at(i)) << end();
274 if (all_header_reagents_match(inst, get(Recipe, variants.at(i))))
275 result.push_back(variants.at(i));
276 }
277 return result;
278 }
279
280 bool all_header_reagents_match(const instruction& inst, const recipe& variant) {
281 for (int i = 0; i < min(SIZE(inst.ingredients), SIZE(variant.ingredients)); ++i) {
282 if (!types_match(variant.ingredients.at(i), inst.ingredients.at(i))) {
283 trace(9993, "transform") << "match failed: ingredient " << i << end();
284 return false;
285 }
286 }
287 for (int i = 0; i < min(SIZE(variant.products), SIZE(inst.products)); ++i) {
288 if (is_dummy(inst.products.at(i))) continue;
289 if (!types_match(variant.products.at(i), inst.products.at(i))) {
290 trace(9993, "transform") << "match failed: product " << i << end();
291 return false;
292 }
293 }
294 return true;
295 }
296
297
298 const recipe& best_variant(const instruction& inst, vector<recipe_ordinal>& candidates) {
299 assert(!candidates.empty());
300 if (SIZE(candidates) == 1) return get(Recipe, candidates.at(0));
301 int min_score = 999;
302 int min_index = 0;
303 for (int i = 0; i < SIZE(candidates); ++i) {
304 const recipe& candidate = get(Recipe, candidates.at(i));
305
306 int score = abs(SIZE(candidate.products)-SIZE(inst.products))
307 + abs(SIZE(candidate.ingredients)-SIZE(inst.ingredients));
308
309 for (int j = 0; j < SIZE(candidate.ingredients); ++j) {
310 if (is_mu_address(candidate.ingredients.at(j)))
311 ++score;
312 }
313 for (int j = 0; j < SIZE(candidate.products); ++j) {
314 if (is_mu_address(candidate.products.at(j)))
315 ++score;
316 }
317 assert(score < 999);
318 if (score < min_score) {
319 min_score = score;
320 min_index = i;
321 }
322 }
323 return get(Recipe, candidates.at(min_index));
324 }
325
326 int non_ghost_size(vector<recipe_ordinal>& variants) {
327 int result = 0;
328 for (int i = 0; i < SIZE(variants); ++i)
329 if (variants.at(i) != -1) ++result;
330 return result;
331 }
332
333 bool next_stash(const call& c, instruction* stash_inst) {
334 const recipe& specializer_recipe = get(Recipe, c.running_recipe);
335 int index = c.running_step_index;
336 for (++index; index < SIZE(specializer_recipe.steps); ++index) {
337 const instruction& inst = specializer_recipe.steps.at(index);
338 if (inst.name == "stash") {
339 *stash_inst = inst;
340 return true;
341 }
342 }
343 return false;
344 }
345
346 :(scenario static_dispatch_disabled_in_recipe_without_variants)
347 def main [
348 1:num <- test 3
349 ]
350 def test [
351 2:num <- next-ingredient
352 return 34
353 ]
354 +mem: storing 34 in location 1
355
356 :(scenario static_dispatch_disabled_on_headerless_definition)
357 % Hide_errors = true;
358 def test a:num -> z:num [
359 z <- copy 1
360 ]
361 def test [
362 return 34
363 ]
364 +error: redefining recipe test
365
366 :(scenario static_dispatch_disabled_on_headerless_definition_2)
367 % Hide_errors = true;
368 def test [
369 return 34
370 ]
371 def test a:num -> z:num [
372 z <- copy 1
373 ]
374 +error: redefining recipe test
375
376 :(scenario static_dispatch_on_primitive_names)
377 def main [
378 1:num <- copy 34
379 2:num <- copy 34
380 3:bool <- equal 1:num, 2:num
381 4:bool <- copy false
382 5:bool <- copy false
383 6:bool <- equal 4:bool, 5:bool
384 ]
385
386 def equal x:num, y:num -> z:bool [
387 local-scope
388 load-ingredients
389 z <- copy false
390 ]
391
392 +mem: storing 0 in location 3
393
394 +mem: storing 1 in location 6
395
396 :(scenario static_dispatch_works_with_dummy_results_for_containers)
397 def main [
398 _ <- test 3, 4
399 ]
56 </span> 89/copy 3/mod/direct 1/rm32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 4/r32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy esp to ecx</span>
<span id="L57" class="LineNr"> 57 </span> <span class="subxComment"># clear(stack)</span>
<span id="L58" class="LineNr"> 58 </span> <span class="subxS2Comment"># . . push args</span>
<span id="L59" class="LineNr"> 59 </span> 51/push-ecx
<span id="L60" class="LineNr"> 60 </span> <span class="subxS2Comment"># . . call</span>
<span id="L61" class="LineNr"> 61 </span> e8/call <a href='092stack.subx.html#L13'>clear-stack</a>/disp32
<span id="L62" class="LineNr"> 62 </span> <span class="subxS2Comment"># . . discard args</span>
<span id="L63" class="LineNr"> 63 </span> 81 0/subop/add 3/mod/direct 4/rm32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 4/imm32 <span class="subxComment"># add to esp</span>
<span id="L64" class="LineNr"> 64 </span> <span class="subxComment"># top should be 0</span>
<span id="L65" class="LineNr"> 65 </span> 58/pop-to-eax
<span id="L66" class="LineNr"> 66 </span> <span class="subxS1Comment"># . check-ints-equal(eax, 0, msg)</span>
<span id="L67" class="LineNr"> 67 </span> <span class="subxS2Comment"># . . push args</span>
<span id="L68" class="LineNr"> 68 </span> 68/push <span class="Constant">"F - <a href='092stack.subx.html#L47'>test-clear-stack</a>: top"</span>/imm32
<span id="L69" class="LineNr"> 69 </span> 68/push 0/imm32
<span id="L70" class="LineNr"> 70 </span> 50/push-eax
<span id="L71" class="LineNr"> 71 </span> <span class="subxS2Comment"># . . call</span>
<span id="L72" class="LineNr"> 72 </span> e8/call <a href='051test.subx.html#L23'>check-ints-equal</a>/disp32
<span id="L73" class="LineNr"> 73 </span> <span class="subxS2Comment"># . . discard args</span>
<span id="L74" class="LineNr"> 74 </span> 81 0/subop/add 3/mod/direct 4/rm32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0xc/imm32 <span class="subxComment"># add to esp</span>
<span id="L75" class="LineNr"> 75 </span> <span class="subxComment"># size should remain 8</span>
<span id="L76" class="LineNr"> 76 </span> 58/pop-to-eax
<span id="L77" class="LineNr"> 77 </span> <span class="subxS1Comment"># . check-ints-equal(eax, 8, msg)</span>
<span id="L78" class="LineNr"> 78 </span> <span class="subxS2Comment"># . . push args</span>
<span id="L79" class="LineNr"> 79 </span> 68/push <span class="Constant">"F - <a href='092stack.subx.html#L47'>test-clear-stack</a>: size"</span>/imm32
<span id="L80" class="LineNr"> 80 </span> 68/push 8/imm32
<span id="L81" class="LineNr"> 81 </span> 50/push-eax
<span id="L82" class="LineNr"> 82 </span> <span class="subxS2Comment"># . . call</span>
<span id="L83" class="LineNr"> 83 </span> e8/call <a href='051test.subx.html#L23'>check-ints-equal</a>/disp32
<span id="L84" class="LineNr"> 84 </span> <span class="subxS2Comment"># . . discard args</span>
<span id="L85" class="LineNr"> 85 </span> 81 0/subop/add 3/mod/direct 4/rm32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <