about summary refs log blame commit diff stats
path: root/archive/2.transect/compiler8
blob: b3b3527152314968df1ae8b3830dba3c4250236e (plain) (tree)




















































                                                                                                    
== Goal

A memory-safe language with a simple translator to x86 that can be feasibly written in x86.

== Definitions of terms

Memory-safe: it should be impossible to:
  a) create a pointer out of arbitrary data, or
  b) to access heap memory after it's been freed.

Simple: do all the work in a 2-pass translator:
  Pass 1: check each instruction's types in isolation.
  Pass 2: emit code for each instruction in isolation.

== types

int
char
(address _)
(array _ n)
(ref _)

== implications

addresses can't be saved to stack or global,
      or included in compound types
      or used across a call (to eliminate possibility of free)

<reg x> : (address T) <- advance <reg/mem> : (array T), <reg offset> : (index T)

arrays require a size
(ref array _) may not include a size

argv has type (array (ref array char))

variables on stack, heap and global are references. The name points at the
address. Use '*' to get at the value.

instructions performing lookups write to register, so that we can reuse the register for temporaries
instructions performing lookups can't read from the register they write to.
  But most instructions read from the register they write to?! (in-out params)

== open questions

If bounds checks can take multiple instructions, why not perform array
indexing in a single statement in the language?

But we want addresses as intermediate points to combine instructions with.

Maybe disallow addresses to function calls, but allow addresses to non-heap
structures to be used spanning function calls and labels.

That's just for ergonomics. Doesn't add new capability.
601a5f4dbcc3b1db7d'>^
17b90929 ^
23692627 ^
795f5244 ^
0c1791a9 ^
c55e49b6 ^
795f5244 ^
23692627 ^

c55e49b6 ^





23692627 ^


17b90929 ^
c55e49b6 ^
23692627 ^

273f8be9 ^
c55e49b6 ^
17b90929 ^

23692627 ^
c55e49b6 ^
23692627 ^








17b90929 ^
c55e49b6 ^
23692627 ^
49c274f8 ^
23692627 ^

795f5244 ^
23692627 ^




c55e49b6 ^
23692627 ^



795f5244 ^
23692627 ^


795f5244 ^
23692627 ^
c55e49b6 ^
23692627 ^




795f5244 ^

23692627 ^


795f5244 ^

23692627 ^

b0450faf ^



23692627 ^


c55e49b6 ^






















23692627 ^

49c274f8 ^













0c1791a9 ^
23692627 ^
49c274f8 ^
f3760b0f ^
91abd257 ^
23692627 ^
f3760b0f ^
91abd257 ^







e39c574c ^

0c1791a9 ^


e39c574c ^

0c1791a9 ^
e39c574c ^
28ca3dbc ^
5729eb51 ^


91abd257 ^







ebdf923d ^
91abd257 ^
0c0bc3ae ^
5729eb51 ^
0c0bc3ae ^
5729eb51 ^
91abd257 ^

ebdf923d ^
0c0bc3ae ^
5729eb51 ^
0c0bc3ae ^
5729eb51 ^
91abd257 ^


5729eb51 ^
b954ac38 ^
91abd257 ^

b954ac38 ^
91abd257 ^


5729eb51 ^
91abd257 ^




b954ac38 ^
49c274f8 ^

0c1791a9 ^
ef96f57c ^

d11bf882 ^
d71d3b4a ^
91abd257 ^
d71d3b4a ^
687dd3f5 ^
d11bf882 ^
49c274f8 ^
ef96f57c ^

d11bf882 ^
d71d3b4a ^
91abd257 ^
d71d3b4a ^
d11bf882 ^
49c274f8 ^


ef96f57c ^



d11bf882 ^
e01277f7 ^
d11bf882 ^
49c274f8 ^

d11bf882 ^


1361f5ce ^
d11bf882 ^
805a4761 ^
e01277f7 ^
d11bf882 ^


fa8eda45 ^


d11bf882 ^

ef96f57c ^



49c274f8 ^

ef96f57c ^
0c1791a9 ^
360d45e6 ^
0c1791a9 ^


49c274f8 ^


d11bf882 ^
49c274f8 ^
d11bf882 ^
49c274f8 ^

fa8eda45 ^
23692627 ^
49c274f8 ^

0c0bc3ae ^
5729eb51 ^
49c274f8 ^
0c0bc3ae ^
5729eb51 ^
23692627 ^


49c274f8 ^
0c0bc3ae ^
5729eb51 ^
0c0bc3ae ^
5729eb51 ^
91abd257 ^

91abd257 ^
91abd257 ^
d379d683 ^
91abd257 ^




5729eb51 ^
49c274f8 ^

5729eb51 ^



49c274f8 ^




b954ac38 ^
49c274f8 ^

fa8eda45 ^
49c274f8 ^
f3760b0f ^
fa8eda45 ^
b954ac38 ^
fa8eda45 ^


49c274f8 ^


23692627 ^
3fdb963b ^
28ca3dbc ^
0c0bc3ae ^
28ca3dbc ^
0c0bc3ae ^
28ca3dbc ^
91abd257 ^
28ca3dbc ^
0c0bc3ae ^
28ca3dbc ^
0c0bc3ae ^
28ca3dbc ^
91abd257 ^


28ca3dbc ^



91abd257 ^

28ca3dbc ^

91abd257 ^



c55e49b6 ^






17b90929 ^
3fdb963b ^



17b90929 ^
3fdb963b ^




17b90929 ^
3fdb963b ^






49c274f8 ^
17b90929 ^
49c274f8 ^



17b90929 ^
49c274f8 ^










c1720b71 ^
17b90929 ^
c1720b71 ^















b0450faf ^
17b90929 ^
b0450faf ^

















17b90929 ^
e39c574c ^

















d07aff34 ^
17b90929 ^
d07aff34 ^












0c1791a9 ^
17b90929 ^
0c1791a9 ^









5bbd91a0 ^















ef96f57c ^

























687dd3f5 ^














28ca3dbc ^














360d45e6 ^










c55e49b6 ^

























































5729eb51 ^


















c6354606 ^


















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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694

                                                                         
                                 









                                      
                                 







                               



                                                                             
 

                                                                        
 


                                                                                      
               


                                                                                                           


                                                                      
                                                     
 






                                                                             

                                                      
                                                                      
                                                                                                                
                                                                                             
                 
                                                                                                                          
                                                                   
                                                                                                                                                    
                                                  

                                                                             





                                         


       
                                                                                                                                           
                                                                               

                                                      
                                                                      
                                                                                        

                                                                                     
                                                                          
                                                                                  








                                                     
                                                                                             
                                                      
                                                
                                                               

              
                                                                               




                                                                
                                                                                         



                                                                        
                                                                  


                                                            
                                                                         
                                                           
                                                                                   




                                                                      

                                                                                                     


                                                            

                                                                              

                  



                                                                           


               






















                                                                                                               

 













                                                                                                 
                                                                                                           
                                                       
                                                                                       
                                                  
                                                                                           
                
                                         







                                                                                  

                                             


                                                                                                   

                                                                                                  
                               
   
                                                               


                                                                                             







                                                       
                                                                              
                                       
                                                               
                                                                             
                                                            
                                                                          

                                                           
                                                                               
                                                                 
                                                                             
                                                                 
                                                                           


   
                                                                                                        
                                                                                                                           

                                                                         
                                                                                                            


                                   
                                                                                                                               




                                                                                                                                                               
                                                                                                                 

 
                                                                                                                                                                             

                                                                                
                                                                 
                                                
                                               
                              
                                                                                                  
                                                                                                              
   

                                                            
                                                              
                                          
                                            
                           
                                                                                                           


   



                                                            
                                                                                                                                                                                                                                          
                                             
                                                                                                                                                                                          

 


                                                                                                                                                                                                                                                                                           
                                                                                              
                                                                                                                              
           
   


                                                                           


                                                                                                

                                                                                                                                     



                                                                               

          
                                                                                      
                                                                                                                            
                                                                                                                                                         


                      


        
                                                                                                                                                          
   
                                                                                                                                                          

 
                                                                                                    
                      

                                                                                
                                                                  
                                                                                 
                                                                             
                                                               
                                                                              


                                                              
                                                                                                 
                                                              
                                                                             
                                                           
                                                                          

                                                                                
                                                                              
                                                    
                                                           




                       
                                                                                                                  

                                                                                       



                                                                                                                 




                                                                
                                                                             

 
                                                                                                   
                    
                                                                                    
                                                                
                                                                                            


                                                                               


                                                  
 
 
                                                                                      
                                                                  
                                                                      
                                                               
                                                                   
                                                              
                                               
                                                              
                                                                  
                                                           
                                                               


   



                                                                                                            

           

                                                                                                                        



           






                                                                
                                   



                          
                                     




                                        
                                 






                               
 
                                         



                                  
                                                                      










                                  
 
                                                                















                                                                    
 
                                                                       

















                                              
                                                                         

















                                              
 
                                                         












                                                                          
 
                                       









                                                           















                                                       

























                                      














                                                                  














                                                       










                                                      

























































                                                                              


















                                                          


















                                                                                
//:: Like container definitions, recipes too can contain type parameters.

:(scenario shape_shifting_recipe)
recipe main [
  10:point <- merge 14, 15
  11:point <- foo 10:point
]
# non-matching variant
recipe foo a:number -> result:number [
  local-scope
  load-ingredients
  result <- copy 34
]
# matching shape-shifting variant
recipe foo a:_t -> result:_t [
  local-scope
  load-ingredients
  result <- copy a
]
+mem: storing 14 in location 11
+mem: storing 15 in location 12

//: Before anything else, disable transforms for shape-shifting recipes and
//: make sure we never try to actually run a shape-shifting recipe. We should
//: be rewriting such instructions to *specializations* with the type
//: ingredients filled in.

:(before "End Transform Checks")
if (any_type_ingredient_in_header(/*recipe_ordinal*/p->first)) continue;

:(after "Running One Instruction")
if (Current_routine->calls.front().running_step_index == 0
    && any_type_ingredient_in_header(Current_routine->calls.front().running_recipe)) {
//?   DUMP("");
  raise_error << "ran into unspecialized shape-shifting recipe " << current_recipe_name() << '\n' << end();
}

//: Make sure we don't match up literals with type ingredients without
//: specialization.
:(before "End valid_type_for_literal Special-cases")
if (contains_type_ingredient_name(lhs)) return false;

//: We'll be creating recipes without loading them from anywhere by
//: *specializing* existing recipes, so make sure we don't clear any of those
//: when we start running tests.
:(before "End Loading .mu Files")
recently_added_recipes.clear();
recently_added_types.clear();

:(before "End Instruction Dispatch(inst, best_score)")
if (best_score == -1) {
//?   if (inst.name == "push-duplex") Trace_stream = new trace_stream;
  trace(9992, "transform") << "no variant found; searching for variant with suitable type ingredients" << end();
  recipe_ordinal exemplar = pick_matching_shape_shifting_variant(variants, inst, best_score);
  if (exemplar) {
    trace(9992, "transform") << "found variant to specialize: " << exemplar << ' ' << get(Recipe, exemplar).name << end();
    variants.push_back(new_variant(exemplar, inst, caller_recipe));
//?     cerr << "-- replacing " << inst.name << " with " << get(Recipe, variants.back()).name << '\n' << debug_string(get(Recipe, variants.back()));
    inst.name = get(Recipe, variants.back()).name;
    trace(9992, "transform") << "new specialization: " << inst.name << end();
  }
//?   if (inst.name == "push-duplex") {
//?     cerr << "======== {\n";
//?     cerr << inst.to_string() << '\n';
//?     DUMP("");
//?     cerr << "======== }\n";
//?   }
}

:(code)
recipe_ordinal pick_matching_shape_shifting_variant(vector<recipe_ordinal>& variants, const instruction& inst, long long int& best_score) {
//?   cerr << "---- " << inst.name << ": " << non_ghost_size(variants) << '\n';
  recipe_ordinal result = 0;
  for (long long int i = 0; i < SIZE(variants); ++i) {
    if (variants.at(i) == -1) continue;  // ghost from a previous test
//?     cerr << "-- variant " << i << "\n" << debug_string(get(Recipe, variants.at(i)));
    trace(9992, "transform") << "checking shape-shifting variant " << i << end();
    long long int current_score = shape_shifting_variant_score(inst, variants.at(i));
    trace(9992, "transform") << "final score: " << current_score << end();
//?     cerr << get(Recipe, variants.at(i)).name << ": " << current_score << '\n';
    if (current_score > best_score) {
      trace(9992, "transform") << "matches" << end();
      result = variants.at(i);
      best_score = current_score;
    }
  }
  return result;
}

long long int shape_shifting_variant_score(const instruction& inst, recipe_ordinal variant) {
//?   cerr << "======== " << inst.to_string() << '\n';
  if (!any_type_ingredient_in_header(variant)) {
    trace(9993, "transform") << "no type ingredients" << end();
    return -1;
  }
  const vector<reagent>& header_ingredients = get(Recipe, variant).ingredients;
  if (SIZE(inst.ingredients) < SIZE(header_ingredients)) {
    trace(9993, "transform") << "too few ingredients" << end();
    return -1;
  }
  for (long long int i = 0; i < SIZE(header_ingredients); ++i) {
    if (!deeply_equal_concrete_types(header_ingredients.at(i), inst.ingredients.at(i))) {
      trace(9993, "transform") << "mismatch: ingredient " << i << end();
      return -1;
    }
  }
  if (SIZE(inst.products) > SIZE(get(Recipe, variant).products)) {
    trace(9993, "transform") << "too few products" << end();
    return -1;
  }
  const vector<reagent>& header_products = get(Recipe, variant).products;
  for (long long int i = 0; i < SIZE(inst.products); ++i) {
    if (!deeply_equal_concrete_types(header_products.at(i), inst.products.at(i))) {
      trace(9993, "transform") << "mismatch: product " << i << end();
      return -1;
    }
  }
  // the greater the number of unused ingredients, the lower the score
  return 100 - (SIZE(get(Recipe, variant).products)-SIZE(inst.products))
             - (SIZE(inst.ingredients)-SIZE(get(Recipe, variant).ingredients));  // ok to go negative
}

bool any_type_ingredient_in_header(recipe_ordinal variant) {
  for (long long int i = 0; i < SIZE(get(Recipe, variant).ingredients); ++i) {
    if (contains_type_ingredient_name(get(Recipe, variant).ingredients.at(i)))
      return true;
  }
  for (long long int i = 0; i < SIZE(get(Recipe, variant).products); ++i) {
    if (contains_type_ingredient_name(get(Recipe, variant).products.at(i)))
      return true;
  }
  return false;
}

bool deeply_equal_concrete_types(reagent lhs, reagent rhs) {
//?   cerr << debug_string(lhs) << " vs " << debug_string(rhs) << '\n';
//?   bool result = deeply_equal_concrete_types(lhs.properties.at(0).second, rhs.properties.at(0).second, rhs);
//?   cerr << "  => " << result << '\n';
//?   return result;
//?   cerr << "== " << debug_string(lhs) << " vs " << debug_string(rhs) << '\n';
  canonize_type(lhs);
  canonize_type(rhs);
  return deeply_equal_concrete_types(lhs.properties.at(0).second, rhs.properties.at(0).second, rhs);
}

bool deeply_equal_concrete_types(const string_tree* lhs, const string_tree* rhs, const reagent& rhs_reagent) {
  if (!lhs) return !rhs;
  if (!rhs) return !lhs;
  if (is_type_ingredient_name(lhs->value)) return true;  // type ingredient matches anything
  if (Literal_type_names.find(lhs->value) != Literal_type_names.end())
    return Literal_type_names.find(rhs->value) != Literal_type_names.end();
  if (rhs->value == "literal" && lhs->value == "address")
    return rhs_reagent.name == "0";
//?   cerr << lhs->value << " vs " << rhs->value << '\n';
  return lhs->value == rhs->value
      && deeply_equal_concrete_types(lhs->left, rhs->left, rhs_reagent)
      && deeply_equal_concrete_types(lhs->right, rhs->right, rhs_reagent);
}

bool contains_type_ingredient_name(const reagent& x) {
  return contains_type_ingredient_name(x.properties.at(0).second);
}

bool contains_type_ingredient_name(const string_tree* type) {
  if (!type) return false;
  if (is_type_ingredient_name(type->value)) return true;
  return contains_type_ingredient_name(type->left) || contains_type_ingredient_name(type->right);
}

bool is_type_ingredient_name(const string& type) {
  return !type.empty() && type.at(0) == '_';
}

recipe_ordinal new_variant(recipe_ordinal exemplar, const instruction& inst, const recipe& caller_recipe) {
  string new_name = next_unused_recipe_name(inst.name);
  trace(9993, "transform") << "switching " << inst.name << " to " << new_name << end();
  assert(!contains_key(Recipe_ordinal, new_name));
  recipe_ordinal new_recipe_ordinal = put(Recipe_ordinal, new_name, Next_recipe_ordinal++);
  // make a copy
  assert(contains_key(Recipe, exemplar));
  assert(!contains_key(Recipe, new_recipe_ordinal));
  recently_added_recipes.push_back(new_recipe_ordinal);
  put(Recipe, new_recipe_ordinal, get(Recipe, exemplar));
  recipe& new_recipe = get(Recipe, new_recipe_ordinal);
  // Since the exemplar never ran any transforms, we have to redo some of the
  // work of the check_types_by_name transform while supporting type-ingredients.
  compute_type_names(new_recipe);
  // that gives enough information to replace type-ingredients with concrete types
  {
    map<string, const string_tree*> mappings;
    bool error = false;
    compute_type_ingredient_mappings(get(Recipe, exemplar), inst, mappings, caller_recipe, &error);
    if (!error) replace_type_ingredients(new_recipe, mappings);
    for (map<string, const string_tree*>::iterator p = mappings.begin(); p != mappings.end(); ++p)
      delete p->second;
    if (error) return exemplar;
  }
  ensure_all_concrete_types(new_recipe, get(Recipe, exemplar));
  // update the name after specialization is complete (so earlier error messages look better)
  new_recipe.name = new_name;
  // perform all transforms on the new specialization
  for (long long int t = 0; t < SIZE(Transform); ++t) {
    (*Transform.at(t))(new_recipe_ordinal);
  }
  new_recipe.transformed_until = SIZE(Transform)-1;
  return new_recipe_ordinal;
}

void compute_type_names(recipe& variant) {
  trace(9993, "transform") << "compute type names: " << variant.name << end();
  map<string, string_tree*> type_names;
  for (long long int i = 0; i < SIZE(variant.ingredients); ++i)
    save_or_deduce_type_name(variant.ingredients.at(i), type_names, variant);
  for (long long int i = 0; i < SIZE(variant.products); ++i)
    save_or_deduce_type_name(variant.products.at(i), type_names, variant);
  for (long long int i = 0; i < SIZE(variant.steps); ++i) {
    instruction& inst = variant.steps.at(i);
    trace(9993, "transform") << "  instruction: " << inst.to_string() << end();
    for (long long int in = 0; in < SIZE(inst.ingredients); ++in)
      save_or_deduce_type_name(inst.ingredients.at(in), type_names, variant);
    for (long long int out = 0; out < SIZE(inst.products); ++out)
      save_or_deduce_type_name(inst.products.at(out), type_names, variant);
  }
}

void save_or_deduce_type_name(reagent& x, map<string, string_tree*>& type_name, const recipe& variant) {
  trace(9994, "transform") << "    checking " << x.to_string() << ": " << debug_string(x.properties.at(0).second) << end();
  if (!x.properties.at(0).second && contains_key(type_name, x.name)) {
    x.properties.at(0).second = new string_tree(*get(type_name, x.name));
    trace(9994, "transform") << "    deducing type to " << debug_string(x.properties.at(0).second) << end();
    return;
  }
  if (!x.properties.at(0).second) {
    raise_error << maybe(variant.name) << "unknown type for " << x.original_string << " (check the name for typos)\n" << end();
    return;
  }
  if (contains_key(type_name, x.name)) return;
  if (x.properties.at(0).second->value == "offset" || x.properties.at(0).second->value == "variant") return;  // special-case for container-access instructions
  put(type_name, x.name, x.properties.at(0).second);
  trace(9993, "transform") << "type of " << x.name << " is " << debug_string(x.properties.at(0).second) << end();
}

void compute_type_ingredient_mappings(const recipe& exemplar, const instruction& inst, map<string, const string_tree*>& mappings, const recipe& caller_recipe, bool* error) {
  long long int limit = min(SIZE(inst.ingredients), SIZE(exemplar.ingredients));
  for (long long int i = 0; i < limit; ++i) {
    const reagent& exemplar_reagent = exemplar.ingredients.at(i);
    reagent ingredient = inst.ingredients.at(i);
    assert(ingredient.properties.at(0).second);
    canonize_type(ingredient);
    if (is_mu_address(exemplar_reagent) && ingredient.name == "0") continue;  // assume it matches
    accumulate_type_ingredients(exemplar_reagent, ingredient, mappings, exemplar, inst, caller_recipe, error);
  }
  limit = min(SIZE(inst.products), SIZE(exemplar.products));
  for (long long int i = 0; i < limit; ++i) {
    const reagent& exemplar_reagent = exemplar.products.at(i);
    reagent product = inst.products.at(i);
    assert(product.properties.at(0).second);
    canonize_type(product);
    accumulate_type_ingredients(exemplar_reagent, product, mappings, exemplar, inst, caller_recipe, error);
  }
}

inline long long int min(long long int a, long long int b) {
  return (a < b) ? a : b;
}

void accumulate_type_ingredients(const reagent& exemplar_reagent, reagent& refinement, map<string, const string_tree*>& mappings, const recipe& exemplar, const instruction& call_instruction, const recipe& caller_recipe, bool* error) {
  assert(refinement.properties.at(0).second);
  accumulate_type_ingredients(exemplar_reagent.properties.at(0).second, refinement.properties.at(0).second, mappings, exemplar, exemplar_reagent, call_instruction, caller_recipe, error);
}

void accumulate_type_ingredients(const string_tree* exemplar_type, const string_tree* refinement_type, map<string, const string_tree*>& mappings, const recipe& exemplar, const reagent& exemplar_reagent, const instruction& call_instruction, const recipe& caller_recipe, bool* error) {
  if (!exemplar_type) return;
  if (!refinement_type) {
    // todo: make this smarter; only warn if exemplar_type contains some *new* type ingredient
    raise_error << maybe(exemplar.name) << "missing type ingredient in " << exemplar_reagent.original_string << '\n' << end();
    return;
  }
  if (!exemplar_type->value.empty() && exemplar_type->value.at(0) == '_') {
    assert(!refinement_type->value.empty());
    if (exemplar_type->right) {
      raise_error << "type_ingredients in non-last position not currently supported\n" << end();
      return;
    }
    if (!contains_key(mappings, exemplar_type->value)) {
      trace(9993, "transform") << "adding mapping from " << exemplar_type->value << " to " << debug_string(refinement_type) << end();
      if (refinement_type->value == "literal")
        put(mappings, exemplar_type->value, new string_tree("number"));
      else
        put(mappings, exemplar_type->value, new string_tree(*refinement_type));
    }
    else {
      if (!deeply_equal_types(get(mappings, exemplar_type->value), refinement_type)) {
        raise_error << maybe(caller_recipe.name) << "no call found for '" << call_instruction.to_string() << "'\n" << end();
//?         cerr << exemplar_type->value << ": " << debug_string(get(mappings, exemplar_type->value)) << " vs " << debug_string(refinement_type) << '\n';
        *error = true;
        return;
      }
    }
  }
  else {
    accumulate_type_ingredients(exemplar_type->left, refinement_type->left, mappings, exemplar, exemplar_reagent, call_instruction, caller_recipe, error);
  }
  accumulate_type_ingredients(exemplar_type->right, refinement_type->right, mappings, exemplar, exemplar_reagent, call_instruction, caller_recipe, error);
}

void replace_type_ingredients(recipe& new_recipe, const map<string, const string_tree*>& mappings) {
  // update its header
  if (mappings.empty()) return;
  trace(9993, "transform") << "replacing in recipe header ingredients" << end();
  for (long long int i = 0; i < SIZE(new_recipe.ingredients); ++i)
    replace_type_ingredients(new_recipe.ingredients.at(i), mappings, new_recipe);
  trace(9993, "transform") << "replacing in recipe header products" << end();
  for (long long int i = 0; i < SIZE(new_recipe.products); ++i)
    replace_type_ingredients(new_recipe.products.at(i), mappings, new_recipe);
  // update its body
  for (long long int i = 0; i < SIZE(new_recipe.steps); ++i) {
    instruction& inst = new_recipe.steps.at(i);
    trace(9993, "transform") << "replacing in instruction '" << inst.to_string() << "'" << end();
    for (long long int j = 0; j < SIZE(inst.ingredients); ++j)
      replace_type_ingredients(inst.ingredients.at(j), mappings, new_recipe);
    for (long long int j = 0; j < SIZE(inst.products); ++j)
      replace_type_ingredients(inst.products.at(j), mappings, new_recipe);
    // special-case for new: replace type ingredient in first ingredient *value*
    if (inst.name == "new" && inst.ingredients.at(0).name.at(0) != '[') {
      string_tree* type_name = parse_string_tree(inst.ingredients.at(0).name);
      replace_type_ingredients(type_name, mappings);
      inst.ingredients.at(0).name = type_name->to_string();
      delete type_name;
    }
  }
}

void replace_type_ingredients(reagent& x, const map<string, const string_tree*>& mappings, const recipe& caller) {
  trace(9993, "transform") << "replacing in ingredient " << x.original_string << end();
  // replace properties
  if (!x.properties.at(0).second) {
    raise_error << "specializing " << caller.name << ": missing type for " << x.original_string << '\n' << end();
    return;
  }
  replace_type_ingredients(x.properties.at(0).second, mappings);
  // refresh types from properties
  delete x.type;
  x.type = new_type_tree(x.properties.at(0).second);
  if (x.type)
    trace(9993, "transform") << "  after: " << debug_string(x.type) << end();
}

void replace_type_ingredients(string_tree* type, const map<string, const string_tree*>& mappings) {
  if (!type) return;
  if (is_type_ingredient_name(type->value) && contains_key(mappings, type->value)) {
    const string_tree* replacement = get(mappings, type->value);
    trace(9993, "transform") << type->value << " => " << debug_string(replacement) << end();
    type->value = replacement->value;
    if (replacement->left) type->left = new string_tree(*replacement->left);
    if (replacement->right) type->right = new string_tree(*replacement->right);
  }
  replace_type_ingredients(type->left, mappings);
  replace_type_ingredients(type->right, mappings);
}

void ensure_all_concrete_types(/*const*/ recipe& new_recipe, const recipe& exemplar) {
  for (long long int i = 0; i < SIZE(new_recipe.ingredients); ++i)
    ensure_all_concrete_types(new_recipe.ingredients.at(i), exemplar);
  for (long long int i = 0; i < SIZE(new_recipe.products); ++i)
    ensure_all_concrete_types(new_recipe.products.at(i), exemplar);
  for (long long int i = 0; i < SIZE(new_recipe.steps); ++i) {
    instruction& inst = new_recipe.steps.at(i);
    for (long long int j = 0; j < SIZE(inst.ingredients); ++j)
      ensure_all_concrete_types(inst.ingredients.at(j), exemplar);
    for (long long int j = 0; j < SIZE(inst.products); ++j)
      ensure_all_concrete_types(inst.products.at(j), exemplar);
  }
}

void ensure_all_concrete_types(/*const*/ reagent& x, const recipe& exemplar) {
  if (!x.type) {
    raise_error << maybe(exemplar.name) << "failed to map a type to " << x.original_string << '\n' << end();
    x.type = new type_tree(0);  // just to prevent crashes later
    return;
  }
  if (x.type->value == -1) {
    raise_error << maybe(exemplar.name) << "failed to map a type to the unknown " << x.original_string << '\n' << end();
    return;
  }
}

long long int non_ghost_size(vector<recipe_ordinal>& variants) {
  long long int result = 0;
  for (long long int i = 0; i < SIZE(variants); ++i)
    if (variants.at(i) != -1) ++result;
  return result;
}

:(scenario shape_shifting_recipe_2)
recipe main [
  10:point <- merge 14, 15
  11:point <- foo 10:point
]
# non-matching shape-shifting variant
recipe foo a:_t, b:_t -> result:number [
  local-scope
  load-ingredients
  result <- copy 34
]
# matching shape-shifting variant
recipe foo a:_t -> result:_t [
  local-scope
  load-ingredients
  result <- copy a
]
+mem: storing 14 in location 11
+mem: storing 15 in location 12

:(scenario shape_shifting_recipe_nonroot)
recipe main [
  10:foo:point <- merge 14, 15, 16
  20:point/raw <- bar 10:foo:point
]
# shape-shifting recipe with type ingredient following some other type
recipe bar a:foo:_t -> result:_t [
  local-scope
  load-ingredients
  result <- get a, x:offset
]
container foo:_t [
  x:_t
  y:number
]
+mem: storing 14 in location 20
+mem: storing 15 in location 21

:(scenario shape_shifting_recipe_type_deduction_ignores_offsets)
recipe main [
  10:foo:point <- merge 14, 15, 16
  20:point/raw <- bar 10:foo:point
]
recipe bar a:foo:_t -> result:_t [
  local-scope
  load-ingredients
  x:number <- copy 1
  result <- get a, x:offset  # shouldn't collide with other variable
]
container foo:_t [
  x:_t
  y:number
]
+mem: storing 14 in location 20
+mem: storing 15 in location 21

:(scenario shape_shifting_recipe_handles_shape_shifting_new_ingredient)
recipe main [
  1:address:foo:point <- bar 3
  11:foo:point <- copy *1:address:foo:point
]
container foo:_t [
  x:_t
  y:number
]
recipe bar x:number -> result:address:foo:_t [
  local-scope
  load-ingredients
  # new refers to _t in its ingredient *value*
  result <- new {(foo _t) : type}
]
+mem: storing 0 in location 11
+mem: storing 0 in location 12
+mem: storing 0 in location 13

:(scenario shape_shifting_recipe_handles_shape_shifting_new_ingredient_2)
recipe main [
  1:address:foo:point <- bar 3
  11:foo:point <- copy *1:address:foo:point
]
recipe bar x:number -> result:address:foo:_t [
  local-scope
  load-ingredients
  # new refers to _t in its ingredient *value*
  result <- new {(foo _t) : type}
]
# container defined after use
container foo:_t [
  x:_t
  y:number
]
+mem: storing 0 in location 11
+mem: storing 0 in location 12
+mem: storing 0 in location 13

:(scenario shape_shifting_recipe_supports_compound_types)
recipe main [
  1:address:point <- new point:type
  2:address:number <- get-address *1:address:point, y:offset
  *2:address:number <- copy 34
  3:address:point <- bar 1:address:point  # specialize _t to address:point
  4:point <- copy *3:address:point
]
recipe bar a:_t -> result:_t [
  local-scope
  load-ingredients
  result <- copy a
]
+mem: storing 34 in location 5

:(scenario shape_shifting_recipe_error)
% Hide_errors = true;
recipe main [
  a:number <- copy 3
  b:address:number <- foo a
]
recipe foo a:_t -> b:_t [
  load-ingredients
  b <- copy a
]
+error: main: no call found for 'b:address:number <- foo a'

:(scenario specialize_inside_recipe_without_header)
recipe main [
  foo 3
]
recipe foo [
  local-scope
  x:number <- next-ingredient  # ensure no header
  1:number/raw <- bar x  # call a shape-shifting recipe
]
recipe bar x:_elem -> y:_elem [
  local-scope
  load-ingredients
  y <- add x, 1
]
+mem: storing 4 in location 1

:(scenario specialize_with_literal)
recipe main [
  local-scope
  # permit literal to map to number
  1:number/raw <- foo 3
]
recipe foo x:_elem -> y:_elem [
  local-scope
  load-ingredients
  y <- add x, 1
]
+mem: storing 4 in location 1

:(scenario specialize_with_literal_2)
recipe main [
  local-scope
  # permit literal to map to character
  1:character/raw <- foo 3
]
recipe foo x:_elem -> y:_elem [
  local-scope
  load-ingredients
  y <- add x, 1
]
+mem: storing 4 in location 1

:(scenario specialize_with_literal_3)
% Hide_errors = true;
recipe main [
  local-scope
  # permit '0' to map to address to shape-shifting type-ingredient
  1:address:character/raw <- foo 0
]
recipe foo x:address:_elem -> y:address:_elem [
  local-scope
  load-ingredients
  y <- copy x
]
+mem: storing 0 in location 1
$error: 0

:(scenario specialize_with_literal_4)
% Hide_errors = true;
recipe main [
  local-scope
  # ambiguous call: what's the type of its ingredient?!
  foo 0
]
recipe foo x:address:_elem -> y:address:_elem [
  local-scope
  load-ingredients
  y <- copy x
]
+error: foo: failed to map a type to x
+error: foo: failed to map a type to y

:(scenario specialize_with_literal_5)
recipe main [
  foo 3, 4  # recipe mapping two variables to literals
]
recipe foo x:_elem, y:_elem [
  local-scope
  load-ingredients
  1:number/raw <- add x, y
]
+mem: storing 7 in location 1

:(scenario multiple_shape_shifting_variants)
# try to call two different shape-shifting recipes with the same name
recipe main [
  e1:d1:number <- merge 3
  e2:d2:number <- merge 4, 5
  1:number/raw <- foo e1
  2:number/raw <- foo e2
]
# the two shape-shifting definitions
recipe foo a:d1:_elem -> b:number [
  local-scope
  load-ingredients
  reply 34
]
recipe foo a:d2:_elem -> b:number [
  local-scope
  load-ingredients
  reply 35
]
# the shape-shifting containers they use
container d1:_elem [
  x:_elem
]
container d2:_elem [
  x:number
  y:_elem
]
+mem: storing 34 in location 1
+mem: storing 35 in location 2

:(scenario multiple_shape_shifting_variants_2)
# static dispatch between shape-shifting variants, _including pointer lookups_
recipe main [
  e1:d1:number <- merge 3
  e2:address:d2:number <- new {(d2 number): type}
  1:number/raw <- foo e1
  2:number/raw <- foo *e2  # different from previous scenario
]
recipe foo a:d1:_elem -> b:number [
  local-scope
  load-ingredients
  reply 34
]
recipe foo a:d2:_elem -> b:number [
  local-scope
  load-ingredients
  reply 35
]
container d1:_elem [
  x:_elem
]
container d2:_elem [
  x:number
  y:_elem
]
+mem: storing 34 in location 1
+mem: storing 35 in location 2

:(scenario missing_type_in_shape_shifting_recipe)
% Hide_errors = true;
recipe main [
  a:d1:number <- merge 3
  foo a
]
recipe foo a:d1:_elem -> b:number [
  local-scope
  load-ingredients
  copy e  # no such variable
  reply 34
]
container d1:_elem [
  x:_elem
]
+error: foo: unknown type for e (check the name for typos)
+error: specializing foo: missing type for e
# and it doesn't crash

:(scenario missing_type_in_shape_shifting_recipe_2)
% Hide_errors = true;
recipe main [
  a:d1:number <- merge 3
  foo a
]
recipe foo a:d1:_elem -> b:number [
  local-scope
  load-ingredients
  get e, x:offset  # unknown variable in a 'get', which does some extra checking
  reply 34
]
container d1:_elem [
  x:_elem
]
+error: foo: unknown type for e (check the name for typos)
+error: specializing foo: missing type for e
# and it doesn't crash