about summary refs log tree commit diff stats
path: root/html/052tangle.cc.html
Commit message (Expand)AuthorAgeFilesLines
* 3431Kartik K. Agaram2016-09-301-42/+43
* 3395Kartik K. Agaram2016-09-171-58/+63
* 3117Kartik K. Agaram2016-07-201-1/+0
* 3013Kartik K. Agaram2016-05-261-4/+8
* 2996Kartik K. Agaram2016-05-211-2/+33
* 2875Kartik K. Agaram2016-04-271-7/+8
* 2812Kartik K. Agaram2016-03-271-15/+24
* 2745Kartik K. Agaram2016-03-091-1/+1
* 2744Kartik K. Agaram2016-03-091-3/+3
* 2743Kartik K. Agaram2016-03-091-69/+61
* 2706 - update htmlKartik K. Agaram2016-02-251-5/+5
* 2625Kartik K. Agaram2016-02-021-1/+1
* 2605Kartik K. Agaram2016-01-261-7/+1
* 2611Kartik K. Agaram2015-11-291-1/+1
* 2423 - describe shape-shifting in html docsKartik K. Agaram2015-11-101-25/+27
* 2175Kartik K. Agaram2015-09-061-49/+176
* 2062Kartik K. Agaram2015-08-231-23/+22
* 1949Kartik K. Agaram2015-08-061-33/+130
* 1885Kartik K. Agaram2015-07-291-0/+235
span> ^
a654e4ec ^

3e1349d2 ^
a654e4ec ^
3e1349d2 ^

672e3e50 ^

a654e4ec ^





672e3e50 ^

a654e4ec ^
c5ffb6e1 ^
672e3e50 ^


f344b250 ^





672e3e50 ^
d009e158 ^

3e1349d2 ^
32b8fac2 ^
f344b250 ^



672e3e50 ^
d009e158 ^
672e3e50 ^
9570363a ^
3e1349d2 ^
32b8fac2 ^
f344b250 ^
32b8fac2 ^
f344b250 ^

c5ffb6e1 ^
f344b250 ^


672e3e50 ^


c5ffb6e1 ^

76755b28 ^
3e1349d2 ^
f344b250 ^
c5ffb6e1 ^



b2566a84 ^
4690ce81 ^
c5ffb6e1 ^

4690ce81 ^
c5ffb6e1 ^

672e3e50 ^
a654e4ec ^
c5ffb6e1 ^

672e3e50 ^
76755b28 ^
32b8fac2 ^
672e3e50 ^
4690ce81 ^


d990e8f0 ^
2f02189d ^
76755b28 ^


672e3e50 ^
672e3e50 ^
a654e4ec ^
d009e158 ^



a654e4ec ^
4690ce81 ^

a654e4ec ^
4690ce81 ^
d009e158 ^
4690ce81 ^
d009e158 ^






2f02189d ^

44c1aeef ^
2f02189d ^
2f02189d ^


44c1aeef ^



d009e158 ^

3e1349d2 ^
f344b250 ^

d009e158 ^


2f02189d ^





d009e158 ^
672e3e50 ^

9570363a ^
3e1349d2 ^
32b8fac2 ^
f344b250 ^

32b8fac2 ^
f344b250 ^

c5ffb6e1 ^
f344b250 ^

3e1349d2 ^
672e3e50 ^


32b8fac2 ^

672e3e50 ^


9570363a ^
3e1349d2 ^
32b8fac2 ^
f344b250 ^


32b8fac2 ^
f344b250 ^

c5ffb6e1 ^
f344b250 ^


672e3e50 ^


32b8fac2 ^

672e3e50 ^
83fcebf3 ^



3e1349d2 ^
4690ce81 ^
f344b250 ^

83fcebf3 ^

cd9bb850 ^
83fcebf3 ^
b2566a84 ^
4690ce81 ^
83fcebf3 ^

4690ce81 ^
83fcebf3 ^
76755b28 ^
83fcebf3 ^
f344b250 ^
83fcebf3 ^
4690ce81 ^
762b099e ^
83fcebf3 ^
2f02189d ^












83fcebf3 ^
2f02189d ^

762b099e ^

3e1349d2 ^
f344b250 ^


762b099e ^
3e1349d2 ^
762b099e ^
f344b250 ^

762b099e ^



2f02189d ^
3e1349d2 ^
2f02189d ^
f344b250 ^
2f02189d ^
f344b250 ^
2f02189d ^




a654e4ec ^

762b099e ^




762b099e ^
4690ce81 ^
762b099e ^
4690ce81 ^
a654e4ec ^


4690ce81 ^
a654e4ec ^
2f02189d ^



cd9bb850 ^
2f02189d ^
cd9bb850 ^


d990e8f0 ^
cd9bb850 ^


2f02189d ^


76755b28 ^
2f02189d ^
d009e158 ^
762b099e ^

2f02189d ^
a654e4ec ^
2f02189d ^
a654e4ec ^
2f02189d ^
a654e4ec ^


2f02189d ^
cd9bb850 ^

2f02189d ^
a654e4ec ^



d990e8f0 ^

















2f02189d ^









4690ce81 ^

a654e4ec ^
76755b28 ^
3e1349d2 ^

4690ce81 ^

f344b250 ^
762b099e ^
b2566a84 ^
2f02189d ^
3e1349d2 ^
f344b250 ^

2f02189d ^
3e1349d2 ^
2f02189d ^
3e1349d2 ^
f344b250 ^
2f02189d ^

f344b250 ^
2f02189d ^





3e1349d2 ^
f344b250 ^

2f02189d ^
3e1349d2 ^
2f02189d ^
3e1349d2 ^
f344b250 ^

2f02189d ^


3e1349d2 ^
2f02189d ^
3e1349d2 ^

f344b250 ^
3e1349d2 ^
f344b250 ^
2f02189d ^
















3e1349d2 ^
2f02189d ^
3e1349d2 ^
f344b250 ^
2f02189d ^
3e1349d2 ^
2f02189d ^






b2566a84 ^


4690ce81 ^
b2566a84 ^


4690ce81 ^


b2566a84 ^

4690ce81 ^

b2566a84 ^
4690ce81 ^
b2566a84 ^
4690ce81 ^

b2566a84 ^
2f02189d ^
b2566a84 ^


dcc060c7 ^
b2566a84 ^
dcc060c7 ^
b2566a84 ^
dcc060c7 ^
b2566a84 ^

4690ce81 ^
a654e4ec ^
4690ce81 ^


b2566a84 ^
b2566a84 ^


4690ce81 ^








a654e4ec ^
4690ce81 ^




672e3e50 ^


a654e4ec ^
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



                                                                                          
                               

                                                
                                  

                                                                                         

                       
                                                                                                 
                                                                                            

                                      
                                
                             
                             
                                   
                            

                              
                                  
                                                                           

                               

        





                               

       
                         
                                                                                                          


                                                                                                           





                                                                                                                                                                                                                                                                                                                     
                                                            

                                                                                                                           
                                        
                                                                                          



                                                                                                                                                       
 
                                                                  
 
                                                                         
                                        
                                                                                          
                                                                                                                 
                                                                                          

                                                                                                                                                         
                                                            


                                                                                                                                                                                                               


                                                                 

                                                                                          
                                                  
                                        
                                                                                                                                                                                                                             



                                                              
                                                                              
                                                                                                                                                                                               

                                                                                                                   
                                                                                                                                                                                                                                                                     

                                                                    
                                                                    
                                                                             

                                                                               
 
                                                                                  
                                                                            
                                      


                                                                                                                                                                                                                                                                                             
                                                                                                                                              
                                                                                                                                                                                                                                                                                                                                                                                                                                      


                                                                                                                                                                                                                                                                                                                                                                       
                                
 
                                                                                                                                                                                           



                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                   

                                                                                                                                                                                                                                                           
                                                                                                                                                                  
                                                                                                                                                   
                                              
                                                                                                                                                                                                                                                                                                                                           






                                                                                                                                                                                                                              

                                                                                                                                                                                                                                
                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                                                                                                                                                                     


                                                                                                                                                                                                      



                                                                                                                                                                                          

                                                            
                                        

                                                                                                                                   


                                                                 





                                                                                                                                                                                                                                
 

                                                  
                                                                                
                                        
                                                                                                       

                                                                                                                 
                                                                                          

                                                                                                                                                         
                                                            

                                                                                                                                   
                                                                                                                                                                                                    


                                                                 

                                                                                                                                                                                                                                                                                                                                                                           


                                                    
                                                                                  
                                        
                                                                                                   


                                                                                                                                                       
                                                                                          

                                                                                                                                                         
                                                            


                                                                                                                                                                                               


                                                                 

                                                                                                                                                                                                                                                                                                                                                                           
 



                                                                                                             
                                        
                                                                           

                                                                                                           

                                                                                            
                                                          
 
                                                                              
                                                                                                                                                                                                  

                                                                                                                   
                                                                                                                                                                                                                                                                        
 
                                                                                                 
                                                              
                                                                                                         
                                                                     
                                                                                                                                                                                                                                       
                                                                                                        
                                












                                                                                                                                                                                                                                                                                                                                                                 
 

                                                                                                                

                                                      
                                        


                                                                                                                                                                                                                       
 
                                       
             

                                                                                                            



                                                                                            
                                                                         
                                        
             
                                                                                                             
 
                                                        




                                                                                                                                                                                                                                 

                                                                                                             




                                                                                 
                                      
                                                                                                                     
                                                                                   
                                                                                                                                                                                               


                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                             
                                                                                           



                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                               
                                                          


                                                                                                                                                                                                                                                                                                                                                                                                         
                                                                      


                                                                                                                                                                                                                                                                                                                                                                       


                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                    
                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

                                
                                      
                                                                                                              
                                                                                    
                                                                                                                                                                                          
                                                                                                                                       


                                                                                                                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                             

                                                                                                                                                          
                                    



                                                                                                            

















                                                                                                                                                                                                                                                                                                                                                                                                                           









                                                                                                                                                                                                                                                                                                                                                                            

                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                     
                                                                                                                          

                                                                                                                                        

                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                             
                                
 
                                                                                           
                                           

                                         
 
                                        
             
                                                                                                                                                      
                                                                                                                                                         

                                                                           
                                                             





                                                                                          
                                           

                                         
 
                                     
             
                                                                                                                                                      

                                                                                                                                                         


                                                                                  
                                        
                                                                          

                                                                                                                                                                                             
                                                                                                                        
                                                                                                                                                                                                                                                       
                                                                                                                   
















                                                                                                                                                 
                                     
             
                                                                                                                                                      
                   
 
                                        






                                                                      


                                                                                              
                                                                                                                                       


                                                                                                                                                                                  


                                                                                                                                                                                                                                                                                             

                                                                                                       

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
                                                                                                                    
                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                                 

                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                       


                                                                 
                                                                                                       
                                                             
                                                                                                      
                                                                
                                                                                                       

                                      
                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                                                       


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
                                                                                                               


                                                                                                            








                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                   




                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       


       
                                     
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 043space.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.Constant { color: #00a0a0; }
.traceAbsent { color: #c00000; }
.cSpecial { color: #008000; }
.muRecipe { color: #ff8700; }
.SalientComment { color: #00ffff; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.Special { color: #c00000; }
.traceContains { color: #008000; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
.muData { color: #ffff00; }
.Identifier { color: #fcb165; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Spaces help isolate recipes from each other. You can create them at will,</span>
<span class="Comment">//: and all addresses in arguments are implicitly based on the 'default-space'</span>
<span class="Comment">//: (unless they have the /raw property)</span>

<span class="Comment">//: A space is just an array of any scalar location.</span>
<span class="Delimiter">:(before &quot;End Mu Types Initialization&quot;)</span>
put<span class="Delimiter">(</span>Type_abbreviations<span class="Delimiter">,</span> <span class="Constant">&quot;space&quot;</span><span class="Delimiter">,</span> new_type_tree<span class="Delimiter">(</span><span class="Constant">&quot;address:array:location&quot;</span><span class="Delimiter">));</span>
<span class="Comment">//: Spaces are often called 'scopes' in other languages.</span>
put<span class="Delimiter">(</span>Type_abbreviations<span class="Delimiter">,</span> <span class="Constant">&quot;scope&quot;</span><span class="Delimiter">,</span> new_type_tree<span class="Delimiter">(</span><span class="Constant">&quot;address:array:location&quot;</span><span class="Delimiter">));</span>

<span class="Delimiter">:(scenario set_default_space)</span>
<span class="Comment"># if default-space is 10, and if an array of 5 locals lies from location 12 to 16 (inclusive),</span>
<span class="Comment"># then local 0 is really location 12, local 1 is really location 13, and so on.</span>
<span class="muRecipe">def</span> main [
  <span class="Comment"># pretend address:array:location; in practice we'll use new</span>
  <span class="Constant">10</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># refcount</span>
  <span class="Constant">11</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">5</span>  <span class="Comment"># length</span>
  <span class="Normal">default</span>-space:space<span class="Special"> &lt;- </span>copy <span class="Constant">10</span>/unsafe
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>
]
<span class="traceContains">+mem: storing 23 in location 13</span>

<span class="Delimiter">:(scenario lookup_sidesteps_default_space)</span>
<span class="muRecipe">def</span> main [
  <span class="Comment"># pretend pointer from outside (2000 reserved for refcount)</span>
  <span class="Constant">2001</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
  <span class="Comment"># pretend address:array:location; in practice we'll use new</span>
  <span class="Constant">1000</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># refcount</span>
  <span class="Constant">1001</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">5</span>  <span class="Comment"># length</span>
  <span class="Comment"># actual start of this recipe</span>
  <span class="Normal">default</span>-space:space<span class="Special"> &lt;- </span>copy <span class="Constant">1000</span>/unsafe
  <span class="Constant">1</span>:&amp;:num<span class="Special"> &lt;- </span>copy <span class="Constant">2000</span>/unsafe  <span class="Comment"># even local variables always contain raw addresses</span>
  <span class="Constant">8</span>:num/<span class="Special">raw &lt;- </span>copy *<span class="Constant">1</span>:&amp;:num
]
<span class="traceContains">+mem: storing 34 in location 8</span>

<span class="SalientComment">//:: first disable name conversion for 'default-space'</span>
<span class="Delimiter">:(scenario convert_names_passes_default_space)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main [
  <span class="Normal">default</span>-space:num<span class="Delimiter">,</span> x:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span><span class="Delimiter">,</span> <span class="Constant">1</span>
]
<span class="traceContains">+name: assign x 1</span>
<span class="traceAbsent">-name: assign default-space 1</span>

<span class="Delimiter">:(before &quot;End is_disqualified Cases&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>x<span class="Delimiter">.</span>name == <span class="Constant">&quot;default-space&quot;</span><span class="Delimiter">)</span>
  x<span class="Delimiter">.</span>initialized = <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End is_special_name Cases&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>s == <span class="Constant">&quot;default-space&quot;</span><span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>

<span class="SalientComment">//:: now implement space support</span>
<span class="Delimiter">:(before &quot;End call Fields&quot;)</span>
<span class="Normal">int</span> default_space<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End call Constructor&quot;)</span>
default_space = <span class="Constant">0</span><span class="Delimiter">;</span>

<span class="Delimiter">:(before &quot;End canonize(x) Special-cases&quot;)</span>
absolutize<span class="Delimiter">(</span>x<span class="Delimiter">);</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> absolutize<span class="Delimiter">(</span>reagent&amp; x<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_raw<span class="Delimiter">(</span>x<span class="Delimiter">)</span> || is_dummy<span class="Delimiter">(</span>x<span class="Delimiter">))</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>x<span class="Delimiter">.</span>name == <span class="Constant">&quot;default-space&quot;</span><span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!x<span class="Delimiter">.</span>initialized<span class="Delimiter">)</span>
    raise &lt;&lt; to_original_string<span class="Delimiter">(</span>current_instruction<span class="Delimiter">())</span> &lt;&lt; <span class="Constant">&quot;: reagent not initialized: '&quot;</span> &lt;&lt; x<span class="Delimiter">.</span>original_string &lt;&lt; <span class="Constant">&quot;'</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  x<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span>address<span class="Delimiter">(</span>x<span class="Delimiter">.</span>value<span class="Delimiter">,</span> space_base<span class="Delimiter">(</span>x<span class="Delimiter">)));</span>
  x<span class="Delimiter">.</span>properties<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>pair&lt;string<span class="Delimiter">,</span> string_tree*&gt;<span class="Delimiter">(</span><span class="Constant">&quot;raw&quot;</span><span class="Delimiter">,</span> <span class="Constant">NULL</span><span class="Delimiter">));</span>
  assert<span class="Delimiter">(</span>is_raw<span class="Delimiter">(</span>x<span class="Delimiter">));</span>
<span class="Delimiter">}</span>

<span class="Normal">int</span> space_base<span class="Delimiter">(</span><span class="Normal">const</span> reagent&amp; x<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Comment">// temporary stub; will be replaced in a later layer</span>
  <span class="Identifier">return</span> current_call<span class="Delimiter">().</span>default_space ? <span class="Delimiter">(</span>current_call<span class="Delimiter">().</span>default_space+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">)</span> : <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Normal">int</span> address<span class="Delimiter">(</span><span class="Normal">int</span> offset<span class="Delimiter">,</span> <span class="Normal">int</span> base<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>offset &gt;= <span class="Constant">0</span><span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>base == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">return</span> offset<span class="Delimiter">;</span>  <span class="Comment">// raw</span>
  <span class="Normal">int</span> size = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> base<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>offset &gt;= size<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Comment">// todo: test</span>
    raise &lt;&lt; <span class="Constant">&quot;location &quot;</span> &lt;&lt; offset &lt;&lt; <span class="Constant">&quot; is out of bounds &quot;</span> &lt;&lt; size &lt;&lt; <span class="Constant">&quot; at &quot;</span> &lt;&lt; base &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> base + <span class="Comment">/*</span><span class="Comment">skip length</span><span class="Comment">*/</span><span class="Constant">1</span> + offset<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="SalientComment">//:: reads and writes to the 'default-space' variable have special behavior</span>

<span class="Delimiter">:(after &quot;Begin Preprocess write_memory(x, data)&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>x<span class="Delimiter">.</span>name == <span class="Constant">&quot;default-space&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!scalar<span class="Delimiter">(</span>data<span class="Delimiter">)</span> || !is_space<span class="Delimiter">(</span>x<span class="Delimiter">))</span>
    raise &lt;&lt; maybe<span class="Delimiter">(</span>current_recipe_name<span class="Delimiter">())</span> &lt;&lt; <span class="Constant">&quot;'default-space' should be of type address:array:location, but is &quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>x<span class="Delimiter">.</span>type<span class="Delimiter">)</span> &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
  current_call<span class="Delimiter">().</span>default_space = data<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
  <span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">bool</span> is_space<span class="Delimiter">(</span><span class="Normal">const</span> reagent&amp; r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Identifier">return</span> is_address_of_array_of_numbers<span class="Delimiter">(</span>r<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario get_default_space)</span>
<span class="muRecipe">def</span> main [
  <span class="Normal">default</span>-space:space<span class="Special"> &lt;- </span>copy <span class="Constant">10</span>/unsafe
  <span class="Constant">1</span>:space/<span class="Special">raw &lt;- </span>copy <span class="Normal">default</span>-space:space
]
<span class="traceContains">+mem: storing 10 in location 1</span>

<span class="Delimiter">:(after &quot;Begin Preprocess read_memory(x)&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>x<span class="Delimiter">.</span>name == <span class="Constant">&quot;default-space&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  vector&lt;<span class="Normal">double</span>&gt; result<span class="Delimiter">;</span>
  result<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>current_call<span class="Delimiter">().</span>default_space<span class="Delimiter">);</span>
  <span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="SalientComment">//:: fix 'get'</span>

<span class="Delimiter">:(scenario lookup_sidesteps_default_space_in_get)</span>
<span class="muRecipe">def</span> main [
  <span class="Comment"># pretend pointer to container from outside (2000 reserved for refcount)</span>
  <span class="Constant">2001</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
  <span class="Constant">2002</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">35</span>
  <span class="Comment"># pretend address:array:location; in practice we'll use new</span>
  <span class="Constant">1000</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># refcount</span>
  <span class="Constant">1001</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">5</span>  <span class="Comment"># length</span>
  <span class="Comment"># actual start of this recipe</span>
  <span class="Normal">default</span>-space:space<span class="Special"> &lt;- </span>copy <span class="Constant">1000</span>/unsafe
  <span class="Constant">1</span>:&amp;:point<span class="Special"> &lt;- </span>copy <span class="Constant">2000</span>/unsafe
  <span class="Constant">9</span>:num/<span class="Special">raw &lt;- </span>get *<span class="Constant">1</span>:&amp;:point<span class="Delimiter">,</span> <span class="Constant">1:offset</span>
]
<span class="traceContains">+mem: storing 35 in location 9</span>

<span class="Delimiter">:(before &quot;Read element&quot; following &quot;case GET:&quot;)</span>
element<span class="Delimiter">.</span>properties<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>pair&lt;string<span class="Delimiter">,</span> string_tree*&gt;<span class="Delimiter">(</span><span class="Constant">&quot;raw&quot;</span><span class="Delimiter">,</span> <span class="Constant">NULL</span><span class="Delimiter">));</span>

<span class="SalientComment">//:: fix 'index'</span>

<span class="Delimiter">:(scenario lookup_sidesteps_default_space_in_index)</span>
<span class="muRecipe">def</span> main [
  <span class="Comment"># pretend pointer to array from outside (2000 reserved for refcount)</span>
  <span class="Constant">2001</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">2</span>  <span class="Comment"># length</span>
  <span class="Constant">2002</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
  <span class="Constant">2003</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">35</span>
  <span class="Comment"># pretend address:array:location; in practice we'll use new</span>
  <span class="Constant">1000</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># refcount</span>
  <span class="Constant">1001</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">5</span>  <span class="Comment"># length</span>
  <span class="Comment"># actual start of this recipe</span>
  <span class="Normal">default</span>-space:space<span class="Special"> &lt;- </span>copy <span class="Constant">1000</span>/unsafe
  <span class="Constant">1</span>:&amp;:@:num<span class="Special"> &lt;- </span>copy <span class="Constant">2000</span>/unsafe
  <span class="Constant">9</span>:num/<span class="Special">raw &lt;- </span>index *<span class="Constant">1</span>:&amp;:@:num<span class="Delimiter">,</span> <span class="Constant">1</span>
]
<span class="traceContains">+mem: storing 35 in location 9</span>

<span class="Delimiter">:(before &quot;Read element&quot; following &quot;case INDEX:&quot;)</span>
element<span class="Delimiter">.</span>properties<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>pair&lt;string<span class="Delimiter">,</span> string_tree*&gt;<span class="Delimiter">(</span><span class="Constant">&quot;raw&quot;</span><span class="Delimiter">,</span> <span class="Constant">NULL</span><span class="Delimiter">));</span>

<span class="SalientComment">//:: convenience operation to automatically deduce the amount of space to</span>
<span class="SalientComment">//:: allocate in a default space with names</span>

<span class="Delimiter">:(scenario new_default_space)</span>
<span class="muRecipe">def</span> main [
  <span class="Normal">new</span>-<span class="Normal">default</span>-space
  <span class="Normal">x</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Normal">y</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">3</span>
]
<span class="Comment"># allocate space for x and y, as well as the chaining slot at 0</span>
<span class="traceContains">+mem: array length is 3</span>

<span class="Delimiter">:(before &quot;End is_disqualified Cases&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>x<span class="Delimiter">.</span>name == <span class="Constant">&quot;number-of-locals&quot;</span><span class="Delimiter">)</span>
  x<span class="Delimiter">.</span>initialized = <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End is_special_name Cases&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>s == <span class="Constant">&quot;number-of-locals&quot;</span><span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>

<span class="Delimiter">:(before &quot;End Rewrite Instruction(curr, recipe result)&quot;)</span>
<span class="Comment">// rewrite `new-default-space` to</span>
<span class="Comment">//   `default-space:space &lt;- new location:type, number-of-locals:literal`</span>
<span class="Comment">// where N is Name[recipe][&quot;&quot;]</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">&quot;new-default-space&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  rewrite_default_space_instruction<span class="Delimiter">(</span>curr<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(after &quot;Begin Preprocess read_memory(x)&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>x<span class="Delimiter">.</span>name == <span class="Constant">&quot;number-of-locals&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  vector&lt;<span class="Normal">double</span>&gt; result<span class="Delimiter">;</span>
  result<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>Name[get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> current_recipe_name<span class="Delimiter">())</span>][<span class="Constant">&quot;&quot;</span>]<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>result<span class="Delimiter">.</span>back<span class="Delimiter">()</span> == <span class="Constant">0</span><span class="Delimiter">)</span>
    raise &lt;&lt; <span class="Constant">&quot;no space allocated for default-space in recipe &quot;</span> &lt;&lt; current_recipe_name<span class="Delimiter">()</span> &lt;&lt; <span class="Constant">&quot;; are you using names?</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(after &quot;Begin Preprocess write_memory(x, data)&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>x<span class="Delimiter">.</span>name == <span class="Constant">&quot;number-of-locals&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  raise &lt;&lt; maybe<span class="Delimiter">(</span>current_recipe_name<span class="Delimiter">())</span> &lt;&lt; <span class="Constant">&quot;can't write to special name 'number-of-locals'</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="SalientComment">//:: 'local-scope' is like 'new-default-space' except that we'll reclaim the</span>
<span class="SalientComment">//:: default-space when the routine exits</span>

<span class="Delimiter">:(scenario local_scope)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:&amp;<span class="Special"> &lt;- </span>foo
  <span class="Constant">2</span>:&amp;<span class="Special"> &lt;- </span>foo
  <span class="Constant">3</span>:<span class="Normal">bool</span><span class="Special"> &lt;- </span>equal <span class="Constant">1</span>:&amp;<span class="Delimiter">,</span> <span class="Constant">2</span>:&amp;
]
<span class="muRecipe">def</span> foo [
  local-scope
  <span class="Normal">x</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
  <span class="Identifier">return</span> <span class="Normal">default</span>-space:space
]
<span class="Comment"># both calls to foo should have received the same default-space</span>
<span class="traceContains">+mem: storing 1 in location 3</span>

<span class="Delimiter">:(scenario local_scope_frees_up_addresses)</span>
<span class="muRecipe">def</span> main [
  local-scope
  <span class="Normal">x</span>:text<span class="Special"> &lt;- </span><span class="Normal">new</span> [abc]
]
<span class="traceContains">+mem: clearing x:text</span>

<span class="Delimiter">:(before &quot;End Rewrite Instruction(curr, recipe result)&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">&quot;local-scope&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  rewrite_default_space_instruction<span class="Delimiter">(</span>curr<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Comment">//: todo: do this in a transform, rather than magically in the reply instruction</span>
<span class="Delimiter">:(after &quot;Falling Through End Of Recipe&quot;)</span>
try_reclaim_locals<span class="Delimiter">();</span>
<span class="Delimiter">:(after &quot;Starting Reply&quot;)</span>
try_reclaim_locals<span class="Delimiter">();</span>

<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> try_reclaim_locals<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  <span class="Comment">// only reclaim routines starting with 'local-scope'</span>
  <span class="Normal">const</span> recipe_ordinal r = get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> current_recipe_name<span class="Delimiter">());</span>
  <span class="Normal">const</span> recipe&amp; exiting_recipe = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>exiting_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
  <span class="Normal">const</span> instruction&amp; inst = exiting_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>old_name != <span class="Constant">&quot;local-scope&quot;</span><span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
  <span class="Comment">// reclaim any local variables unless they're being returned</span>
  vector&lt;<span class="Normal">double</span>&gt; zeros<span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Comment">/*</span><span class="Comment">leave default space for last</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>exiting_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">const</span> instruction&amp; inst = exiting_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Normal">const</span> reagent&amp; product = inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
      <span class="Comment">// local variables only</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>has_property<span class="Delimiter">(</span>product<span class="Delimiter">,</span> <span class="Constant">&quot;lookup&quot;</span><span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>has_property<span class="Delimiter">(</span>product<span class="Delimiter">,</span> <span class="Constant">&quot;raw&quot;</span><span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>  <span class="Comment">// tests often want to check such locations after they run</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>escaping<span class="Delimiter">(</span>product<span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
      <span class="Comment">// End Checks For Reclaiming Locals</span>
      trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;mem&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;clearing &quot;</span> &lt;&lt; product<span class="Delimiter">.</span>original_string &lt;&lt; end<span class="Delimiter">();</span>
      zeros<span class="Delimiter">.</span>resize<span class="Delimiter">(</span>size_of<span class="Delimiter">(</span>product<span class="Delimiter">));</span>
      write_memory<span class="Delimiter">(</span>product<span class="Delimiter">,</span> zeros<span class="Delimiter">);</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;mem&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;automatically abandoning &quot;</span> &lt;&lt; current_call<span class="Delimiter">().</span>default_space &lt;&lt; end<span class="Delimiter">();</span>
  abandon<span class="Delimiter">(</span>current_call<span class="Delimiter">().</span>default_space<span class="Delimiter">,</span>
          inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>type<span class="Delimiter">-&gt;</span>right<span class="Delimiter">,</span>
          <span class="Comment">/*</span><span class="Comment">refcount</span><span class="Comment">*/</span><span class="Constant">1</span> + <span class="Comment">/*</span><span class="Comment">array length</span><span class="Comment">*/</span><span class="Constant">1</span> + <span class="Comment">/*</span><span class="Comment">number-of-locals</span><span class="Comment">*/</span>Name[r][<span class="Constant">&quot;&quot;</span>]<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(code)</span>
<span class="Comment">// is this reagent one of the values returned by the current (reply) instruction?</span>
<span class="Comment">// is the corresponding ingredient saved in the caller?</span>
<span class="Normal">bool</span> escaping<span class="Delimiter">(</span><span class="Normal">const</span> reagent&amp; r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>Current_routine<span class="Delimiter">);</span>  <span class="Comment">// run-time only</span>
  <span class="Comment">// nothing escapes when you fall through past end of recipe</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>current_step_index<span class="Delimiter">()</span> &gt;= SIZE<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-&gt;</span>steps<span class="Delimiter">()))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>r == current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">))</span> <span class="Delimiter">{</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>caller_uses_product<span class="Delimiter">(</span>i<span class="Delimiter">))</span>
        <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Comment">//: since we don't decrement refcounts for escaping values above, make sure we</span>
<span class="Comment">//: don't increment them when the caller saves them either</span>

<span class="Delimiter">:(after &quot;Write Products of Instruction&quot;)</span>
Update_refcounts_in_write_memory = should_update_refcounts_in_write_memory<span class="Delimiter">();</span>
<span class="Delimiter">:(before &quot;End Write Products of Instruction&quot;)</span>
Update_refcounts_in_write_memory = <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">bool</span> should_update_refcounts_in_write_memory<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  <span class="Normal">const</span> instruction&amp; inst = current_instruction<span class="Delimiter">();</span>
  <span class="Comment">// End should_update_refcounts_in_write_memory Special-cases For Primitives</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>operation &lt; MAX_PRIMITIVE_RECIPES<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>operation<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Normal">const</span> recipe&amp; callee = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>operation<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>callee<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Identifier">return</span> callee<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>old_name != <span class="Constant">&quot;local-scope&quot;</span><span class="Delimiter">;</span>  <span class="Comment">// callees that call local-scope are already dealt with before return</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> caller_uses_product<span class="Delimiter">(</span><span class="Normal">int</span> product_index<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>Current_routine<span class="Delimiter">);</span>  <span class="Comment">// run-time only</span>
  assert<span class="Delimiter">(</span>!Current_routine<span class="Delimiter">-&gt;</span>calls<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>Current_routine<span class="Delimiter">-&gt;</span>calls<span class="Delimiter">.</span>size<span class="Delimiter">()</span> == <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">const</span> call&amp; caller = *++Current_routine<span class="Delimiter">-&gt;</span>calls<span class="Delimiter">.</span>begin<span class="Delimiter">();</span>
  <span class="Normal">const</span> instruction&amp; caller_inst = to_instruction<span class="Delimiter">(</span>caller<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>product_index &gt;= SIZE<span class="Delimiter">(</span>caller_inst<span class="Delimiter">.</span>products<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Identifier">return</span> !is_dummy<span class="Delimiter">(</span>caller_inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>product_index<span class="Delimiter">));</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> rewrite_default_space_instruction<span class="Delimiter">(</span>instruction&amp; curr<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>empty<span class="Delimiter">())</span>
    raise &lt;&lt; to_original_string<span class="Delimiter">(</span>curr<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot; can't take any ingredients</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  curr<span class="Delimiter">.</span>name = <span class="Constant">&quot;new&quot;</span><span class="Delimiter">;</span>
  <span class="Constant">curr.ingredients.push_back(reagent(&quot;location:type</span><span class="Constant">&quot;));</span>
  <span class="Constant">curr.ingredients.push_back(reagent(&quot;number-of-locals:literal</span><span class="Constant">&quot;));</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">())</span>
    raise &lt;&lt; <span class="Constant">&quot;new-default-space can't take any results</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>reagent<span class="Delimiter">(</span><span class="Constant">&quot;default-space:space&quot;</span><span class="Delimiter">));</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario local_scope_frees_up_addresses_inside_containers)</span>
<span class="muData">container</span> foo [
  <span class="Normal">x</span>:num
  <span class="Normal">y</span>:&amp;:num
]
<span class="muRecipe">def</span> main [
  local-scope
  <span class="Normal">x</span>:&amp;:num<span class="Special"> &lt;- </span><span class="Normal">new</span> <span class="Constant">number:type</span>
  <span class="Normal">y</span>:foo<span class="Special"> &lt;- </span>merge <span class="Constant">34</span><span class="Delimiter">,</span> x:&amp;:num
  <span class="Comment"># x and y are both cleared when main returns</span>
]
<span class="traceContains">+mem: clearing x:&amp;:num</span>
<span class="traceContains">+mem: decrementing refcount of 1006: 2 -&gt; 1</span>
<span class="traceContains">+mem: clearing y:foo</span>
<span class="traceContains">+mem: decrementing refcount of 1006: 1 -&gt; 0</span>
<span class="traceContains">+mem: automatically abandoning 1006</span>

<span class="Delimiter">:(scenario local_scope_returns_addresses_inside_containers)</span>
<span class="muData">container</span> foo [
  <span class="Normal">x</span>:num
  <span class="Normal">y</span>:&amp;:num
]
<span class="muRecipe">def</span> f [
  local-scope
  <span class="Normal">x</span>:&amp;:num<span class="Special"> &lt;- </span><span class="Normal">new</span> <span class="Constant">number:type</span>
  *x:&amp;:num<span class="Special"> &lt;- </span>copy <span class="Constant">12</span>
  <span class="Normal">y</span>:foo<span class="Special"> &lt;- </span>merge <span class="Constant">34</span><span class="Delimiter">,</span> x:&amp;:num
  <span class="Comment"># since y is 'escaping' f, it should not be cleared</span>
  <span class="Identifier">return</span> y:foo
]
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:foo<span class="Special"> &lt;- </span>f
  <span class="Constant">3</span>:num<span class="Special"> &lt;- </span>get <span class="Constant">1</span>:foo<span class="Delimiter">,</span> <span class="Constant">x:offset</span>
  <span class="Constant">4</span>:&amp;:num<span class="Special"> &lt;- </span>get <span class="Constant">1</span>:foo<span class="Delimiter">,</span> <span class="Constant">y:offset</span>
  <span class="Constant">5</span>:num<span class="Special"> &lt;- </span>copy *<span class="Constant">4</span>:&amp;:num
  <span class="Constant">1</span>:foo<span class="Special"> &lt;- </span>put <span class="Constant">1</span>:foo<span class="Delimiter">,</span> <span class="Constant">y:offset</span><span class="Delimiter">,</span> <span class="Constant">0</span>
  <span class="Constant">4</span>:&amp;:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+mem: storing 34 in location 1</span>
<span class="traceContains">+mem: storing 1006 in location 2</span>
<span class="traceContains">+mem: storing 34 in location 3</span>
<span class="Comment"># refcount of 1:foo shouldn't include any stray ones from f</span>
<span class="traceContains">+run: {4: (&quot;address&quot; &quot;number&quot;)} &lt;- get {1: &quot;foo&quot;}, {y: &quot;offset&quot;}</span>
<span class="traceContains">+mem: incrementing refcount of 1006: 1 -&gt; 2</span>
<span class="Comment"># 1:foo wasn't abandoned/cleared</span>
<span class="traceContains">+run: {5: &quot;number&quot;} &lt;- copy {4: (&quot;address&quot; &quot;number&quot;), &quot;lookup&quot;: ()}</span>
<span class="traceContains">+mem: storing 12 in location 5</span>
<span class="traceContains">+run: {1: &quot;foo&quot;} &lt;- put {1: &quot;foo&quot;}, {y: &quot;offset&quot;}, {0: &quot;literal&quot;}</span>
<span class="traceContains">+mem: decrementing refcount of 1006: 2 -&gt; 1</span>
<span class="traceContains">+run: {4: (&quot;address&quot; &quot;number&quot;)} &lt;- copy {0: &quot;literal&quot;}</span>
<span class="traceContains">+mem: decrementing refcount of 1006: 1 -&gt; 0</span>
<span class="traceContains">+mem: automatically abandoning 1006</span>

<span class="Delimiter">:(scenario local_scope_claims_return_values_when_not_saved)</span>
<span class="muRecipe">def</span> f [
  local-scope
  <span class="Normal">x</span>:&amp;:num<span class="Special"> &lt;- </span><span class="Normal">new</span> <span class="Constant">number:type</span>
  reply x:&amp;:num
]
<span class="muRecipe">def</span> main [
  f  <span class="Comment"># doesn't save result</span>
]
<span class="Comment"># x reclaimed</span>
<span class="traceContains">+mem: automatically abandoning 1004</span>
<span class="Comment"># f's local scope reclaimed</span>
<span class="traceContains">+mem: automatically abandoning 1000</span>

<span class="SalientComment">//:: all recipes must set default-space one way or another</span>

<span class="Delimiter">:(before &quot;End Globals&quot;)</span>
<span class="Normal">bool</span> Hide_missing_default_space_errors = <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Checks&quot;)</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>check_default_space<span class="Delimiter">);</span>  <span class="Comment">// idempotent</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> check_default_space<span class="Delimiter">(</span><span class="Normal">const</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>Hide_missing_default_space_errors<span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>  <span class="Comment">// skip previous core tests; this is only for mu code</span>
  <span class="Normal">const</span> recipe&amp; caller = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
  <span class="Comment">// skip scenarios (later layer)</span>
  <span class="Comment">// user code should never create recipes with underscores in their names</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">&quot;scenario_&quot;</span><span class="Delimiter">)</span> == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>  <span class="Comment">// skip mu scenarios which will use raw memory locations</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">&quot;run_&quot;</span><span class="Delimiter">)</span> == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>  <span class="Comment">// skip calls to 'run', which should be in scenarios and will also use raw memory locations</span>
  <span class="Comment">// assume recipes with only numeric addresses know what they're doing (usually tests)</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!contains_non_special_name<span class="Delimiter">(</span>r<span class="Delimiter">))</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
  trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;--- check that recipe &quot;</span> &lt;&lt; caller<span class="Delimiter">.</span>name &lt;&lt; <span class="Constant">&quot; sets default-space&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>caller<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>caller<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">()</span>
      || caller<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name != <span class="Constant">&quot;default-space&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    raise &lt;&lt; caller<span class="Delimiter">.</span>name &lt;&lt; <span class="Constant">&quot; does not seem to start with default-space or local-scope</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(after &quot;Load .mu Core&quot;)</span>
Hide_missing_default_space_errors = <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">:(after &quot;Test Runs&quot;)</span>
Hide_missing_default_space_errors = <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">:(after &quot;Running Main&quot;)</span>
Hide_missing_default_space_errors = <span class="Constant">false</span><span class="Delimiter">;</span>

<span class="Delimiter">:(code)</span>
<span class="Normal">bool</span> contains_non_special_name<span class="Delimiter">(</span><span class="Normal">const</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span>map&lt;string<span class="Delimiter">,</span> <span class="Normal">int</span>&gt;::iterator p = Name[r]<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != Name[r]<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>p<span class="Delimiter">-&gt;</span>first<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>p<span class="Delimiter">-&gt;</span>first<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">&quot;stash_&quot;</span><span class="Delimiter">)</span> == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>  <span class="Comment">// generated by rewrite_stashes_to_text (cross-layer)</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!is_special_name<span class="Delimiter">(</span>p<span class="Delimiter">-&gt;</span>first<span class="Delimiter">))</span>
      <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Comment">// reagent comparison -- only between reagents in a single recipe</span>
<span class="Normal">bool</span> <span class="Normal">operator</span>==<span class="Delimiter">(</span><span class="Normal">const</span> reagent&amp; a<span class="Delimiter">,</span> <span class="Normal">const</span> reagent&amp; b<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>a<span class="Delimiter">.</span>name != b<span class="Delimiter">.</span>name<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>property<span class="Delimiter">(</span>a<span class="Delimiter">,</span> <span class="Constant">&quot;space&quot;</span><span class="Delimiter">)</span> != property<span class="Delimiter">(</span>b<span class="Delimiter">,</span> <span class="Constant">&quot;space&quot;</span><span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> <span class="Normal">operator</span>&lt;<span class="Delimiter">(</span><span class="Normal">const</span> reagent&amp; a<span class="Delimiter">,</span> <span class="Normal">const</span> reagent&amp; b<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">int</span> aspace = <span class="Constant">0</span><span class="Delimiter">,</span> bspace = <span class="Constant">0</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>has_property<span class="Delimiter">(</span>a<span class="Delimiter">,</span> <span class="Constant">&quot;space&quot;</span><span class="Delimiter">))</span> aspace = to_integer<span class="Delimiter">(</span>property<span class="Delimiter">(</span>a<span class="Delimiter">,</span> <span class="Constant">&quot;space&quot;</span><span class="Delimiter">)-&gt;</span>value<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>has_property<span class="Delimiter">(</span>b<span class="Delimiter">,</span> <span class="Constant">&quot;space&quot;</span><span class="Delimiter">))</span> bspace = to_integer<span class="Delimiter">(</span>property<span class="Delimiter">(</span>b<span class="Delimiter">,</span> <span class="Constant">&quot;space&quot;</span><span class="Delimiter">)-&gt;</span>value<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>aspace != bspace<span class="Delimiter">)</span> <span class="Identifier">return</span> aspace &lt; bspace<span class="Delimiter">;</span>
  <span class="Identifier">return</span> a<span class="Delimiter">.</span>name &lt; b<span class="Delimiter">.</span>name<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->