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
|
<!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 - 037new.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<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-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 1.05em; }
.traceContains { color: #008000; }
.SalientComment { color: #00ffff; }
.cSpecial { color: #008000; }
.CommentedCode { color: #6c6c6c; }
.Comment { color: #9090ff; }
.Delimiter { color: #a04060; }
.Special { color: #ff6060; }
.Identifier { color: #804000; }
.Constant { color: #00a0a0; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Creating space for new variables at runtime.</span>
<span class="Comment">//: Mu has two primitives for managing allocations:</span>
<span class="Comment">//: - 'allocate' reserves a specified amount of space</span>
<span class="Comment">//: - 'abandon' returns allocated space to be reused by future calls to 'allocate'</span>
<span class="Comment">//:</span>
<span class="Comment">//: In practice it's useful to let programs copy addresses anywhere they want,</span>
<span class="Comment">//: but a prime source of (particularly security) bugs is accessing memory</span>
<span class="Comment">//: after it's been abandoned. To avoid this, mu programs use a safer</span>
<span class="Comment">//: primitive called 'new', which adds two features:</span>
<span class="Comment">//:</span>
<span class="Comment">//: - it takes a type rather than a size, to save you the trouble of</span>
<span class="Comment">//: calculating sizes of different variables.</span>
<span class="Comment">//: - it allocates an extra location where it tracks so-called 'reference</span>
<span class="Comment">//: counts' or refcounts: the number of address variables in your program that</span>
<span class="Comment">//: point to this allocation. The initial refcount of an allocation starts out</span>
<span class="Comment">//: at 1 (the product of the 'new' instruction). When other variables are</span>
<span class="Comment">//: copied from it the refcount is incremented. When a variable stops pointing</span>
<span class="Comment">//: at it the refcount is decremented. When the refcount goes to 0 the</span>
<span class="Comment">//: allocation is automatically abandoned.</span>
<span class="Comment">//:</span>
<span class="Comment">//: Mu programs guarantee you'll have no memory corruption bugs as long as you</span>
<span class="Comment">//: use 'new' and never use 'allocate' or 'abandon'. However, they don't help</span>
<span class="Comment">//: you at all to remember to abandon memory after you're done with it. To</span>
<span class="Comment">//: minimize memory use, be sure to reset allocated addresses to 0 when you're</span>
<span class="Comment">//: done with them.</span>
<span class="Comment">//: To help you distinguish addresses that point at allocations, 'new' returns</span>
<span class="Comment">//: type address:shared:___. Think of 'shared' as a generic container that</span>
<span class="Comment">//: contains one extra field: the refcount. However, lookup operations will</span>
<span class="Comment">//: transparently drop the 'shared' and access to the refcount. Copying</span>
<span class="Comment">//: between shared and non-shared addresses is forbidden.</span>
<span class="Delimiter">:(before "End Mu Types Initialization")</span>
type_ordinal shared = put<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">"shared"</span><span class="Delimiter">,</span> Next_type_ordinal++<span class="Delimiter">);</span>
get_or_insert<span class="Delimiter">(</span>Type<span class="Delimiter">,</span> shared<span class="Delimiter">).</span>name = <span class="Constant">"shared"</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Drop Address In lookup_memory(x)")</span>
if <span class="Delimiter">(</span>x<span class="Delimiter">.</span>type<span class="Delimiter">-></span>name == <span class="Constant">"shared"</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">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"skipping refcount at "</span> << x<span class="Delimiter">.</span>value << end<span class="Delimiter">();</span>
x<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span>x<span class="Delimiter">.</span>value+<span class="Constant">1</span><span class="Delimiter">);</span> <span class="Comment">// skip refcount</span>
drop_from_type<span class="Delimiter">(</span>x<span class="Delimiter">,</span> <span class="Constant">"shared"</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Drop Address In canonize_type(r)")</span>
if <span class="Delimiter">(</span>r<span class="Delimiter">.</span>type<span class="Delimiter">-></span>name == <span class="Constant">"shared"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
drop_from_type<span class="Delimiter">(</span>r<span class="Delimiter">,</span> <span class="Constant">"shared"</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenarios run)</span>
<span class="Delimiter">:(scenario new)</span>
<span class="Comment"># call new two times with identical arguments; you should get back different results</span>
recipe main [
<span class="Constant">1</span>:address:shared:number/<span class="Special">raw <- </span>new number:type
<span class="Constant">2</span>:address:shared:number/<span class="Special">raw <- </span>new number:type
<span class="Constant">3</span>:boolean/<span class="Special">raw <- </span>equal <span class="Constant">1</span>:address:shared:number/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">2</span>:address:shared:number/<span class="Special">raw</span>
]
<span class="traceContains">+mem: storing 0 in location 3</span>
<span class="Delimiter">:(before "End Globals")</span>
long long int Memory_allocated_until = Reserved_for_tests<span class="Delimiter">;</span>
long long int Initial_memory_per_routine = <span class="Constant">100000</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Setup")</span>
Memory_allocated_until = Reserved_for_tests<span class="Delimiter">;</span>
Initial_memory_per_routine = <span class="Constant">100000</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End routine Fields")</span>
long long int alloc<span class="Delimiter">,</span> alloc_max<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End routine Constructor")</span>
alloc = Memory_allocated_until<span class="Delimiter">;</span>
Memory_allocated_until += Initial_memory_per_routine<span class="Delimiter">;</span>
alloc_max = Memory_allocated_until<span class="Delimiter">;</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"new"</span><span class="Delimiter">)</span> << <span class="Constant">"routine allocated memory from "</span> << alloc << <span class="Constant">" to "</span> << alloc_max << end<span class="Delimiter">();</span>
<span class="SalientComment">//:: 'new' takes a weird 'type' as its first ingredient; don't error on it</span>
<span class="Delimiter">:(before "End Mu Types Initialization")</span>
put<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">"type"</span><span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>
<span class="SalientComment">//:: typecheck 'new' instructions</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
NEW<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"new"</span><span class="Delimiter">,</span> NEW<span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</span>
case NEW: <span class="Delimiter">{</span>
const recipe& caller = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
if <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>empty<span class="Delimiter">()</span> || SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span> > <span class="Constant">2</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise_error << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"'new' requires one or two ingredients, but got "</span> << to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">// End NEW Check Special-cases</span>
reagent type = inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
if <span class="Delimiter">(</span>!is_mu_type_literal<span class="Delimiter">(</span>type<span class="Delimiter">))</span> <span class="Delimiter">{</span>
raise_error << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"first ingredient of 'new' should be a type, but got "</span> << type<span class="Delimiter">.</span>original_string << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
if <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
raise_error << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"result of 'new' should never be ignored</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
if <span class="Delimiter">(</span>!product_of_new_is_valid<span class="Delimiter">(</span>inst<span class="Delimiter">))</span> <span class="Delimiter">{</span>
raise_error << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"product of 'new' has incorrect type: "</span> << to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
bool product_of_new_is_valid<span class="Delimiter">(</span>const instruction& inst<span class="Delimiter">)</span> <span class="Delimiter">{</span>
reagent product = inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
canonize_type<span class="Delimiter">(</span>product<span class="Delimiter">);</span>
if <span class="Delimiter">(</span>!product<span class="Delimiter">.</span>type || product<span class="Delimiter">.</span>type<span class="Delimiter">-></span>value != get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">"address"</span><span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
drop_from_type<span class="Delimiter">(</span>product<span class="Delimiter">,</span> <span class="Constant">"address"</span><span class="Delimiter">);</span>
if <span class="Delimiter">(</span>!product<span class="Delimiter">.</span>type || product<span class="Delimiter">.</span>type<span class="Delimiter">-></span>value != get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">"shared"</span><span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
drop_from_type<span class="Delimiter">(</span>product<span class="Delimiter">,</span> <span class="Constant">"shared"</span><span class="Delimiter">);</span>
if <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span> > <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// array allocation</span>
if <span class="Delimiter">(</span>!product<span class="Delimiter">.</span>type || product<span class="Delimiter">.</span>type<span class="Delimiter">-></span>value != get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">"array"</span><span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
drop_from_type<span class="Delimiter">(</span>product<span class="Delimiter">,</span> <span class="Constant">"array"</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
reagent expected_product<span class="Delimiter">(</span><span class="Constant">"x:"</span>+inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name<span class="Delimiter">);</span>
<span class="Comment">// End Post-processing(expected_product) When Checking 'new'</span>
<span class="Identifier">return</span> types_strictly_match<span class="Delimiter">(</span>product<span class="Delimiter">,</span> expected_product<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="SalientComment">//:: translate 'new' to 'allocate' instructions that take a size instead of a type</span>
<span class="Delimiter">:(after "Transform.push_back(check_instruction)")</span> <span class="Comment">// check_instruction will guard against direct 'allocate' instructions below</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>transform_new_to_allocate<span class="Delimiter">);</span> <span class="Comment">// idempotent</span>
<span class="Delimiter">:(code)</span>
void transform_new_to_allocate<span class="Delimiter">(</span>const recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"--- convert 'new' to 'allocate' for recipe "</span> << get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name << end<span class="Delimiter">();</span>
for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
instruction& inst = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
<span class="Comment">// Convert 'new' To 'allocate'</span>
if <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name == <span class="Constant">"new"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
inst<span class="Delimiter">.</span>operation = ALLOCATE<span class="Delimiter">;</span>
string_tree* type_name = new string_tree<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name<span class="Delimiter">);</span>
<span class="Comment">// End Post-processing(type_name) When Converting 'new'</span>
type_tree* type = new_type_tree<span class="Delimiter">(</span>type_name<span class="Delimiter">);</span>
inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>set_value<span class="Delimiter">(</span>size_of<span class="Delimiter">(</span>type<span class="Delimiter">));</span>
trace<span class="Delimiter">(</span><span class="Constant">9992</span><span class="Delimiter">,</span> <span class="Constant">"new"</span><span class="Delimiter">)</span> << <span class="Constant">"size of "</span> << to_string<span class="Delimiter">(</span>type_name<span class="Delimiter">)</span> << <span class="Constant">" is "</span> << inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>value << end<span class="Delimiter">();</span>
delete type<span class="Delimiter">;</span>
delete type_name<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="SalientComment">//:: implement 'allocate' based on size</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
ALLOCATE<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"allocate"</span><span class="Delimiter">,</span> ALLOCATE<span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
case ALLOCATE: <span class="Delimiter">{</span>
<span class="Comment">// compute the space we need</span>
long long int size = ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
if <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>ingredients<span class="Delimiter">)</span> > <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// array</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"array size is "</span> << ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> << end<span class="Delimiter">();</span>
size = <span class="Comment">/*</span><span class="Comment">space for length</span><span class="Comment">*/</span><span class="Constant">1</span> + size*ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Comment">// include space for refcount</span>
size++<span class="Delimiter">;</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"allocating size "</span> << size << end<span class="Delimiter">();</span>
<span class="CommentedCode">//? Total_alloc += size;</span>
<span class="CommentedCode">//? Num_alloc++;</span>
<span class="Comment">// compute the region of memory to return</span>
<span class="Comment">// really crappy at the moment</span>
ensure_space<span class="Delimiter">(</span>size<span class="Delimiter">);</span>
const long long int result = Current_routine<span class="Delimiter">-></span>alloc<span class="Delimiter">;</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"new alloc: "</span> << result << end<span class="Delimiter">();</span>
<span class="Comment">// save result</span>
products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>result<span class="Delimiter">);</span>
<span class="Comment">// initialize allocated space</span>
for <span class="Delimiter">(</span>long long int address = result<span class="Delimiter">;</span> address < result+size<span class="Delimiter">;</span> ++address<span class="Delimiter">)</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> address<span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Comment">// initialize array length</span>
if <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">)</span> > <span class="Constant">1</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">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"storing "</span> << ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> << <span class="Constant">" in location "</span> << result+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span> << end<span class="Delimiter">();</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> result+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">,</span> ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">));</span>
<span class="Delimiter">}</span>
<span class="Comment">// bump</span>
Current_routine<span class="Delimiter">-></span>alloc += size<span class="Delimiter">;</span>
<span class="Comment">// no support for reclaiming memory</span>
assert<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>alloc <= Current_routine<span class="Delimiter">-></span>alloc_max<span class="Delimiter">);</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="SalientComment">//:: ensure we never call 'allocate' directly; its types are not checked</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</span>
case ALLOCATE: <span class="Delimiter">{</span>
raise_error << <span class="Constant">"never call 'allocate' directly'; always use 'new'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="SalientComment">//:: ensure we never call 'new' without translating it (unless we add special-cases later)</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
case NEW: <span class="Delimiter">{</span>
raise_error << <span class="Constant">"no implementation for 'new'; why wasn't it translated to 'allocate'? Please save a copy of your program and send it to Kartik.</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="CommentedCode">//? :(before "End Globals")</span>
<span class="CommentedCode">//? long long int Total_alloc = 0;</span>
<span class="CommentedCode">//? long long int Num_alloc = 0;</span>
<span class="CommentedCode">//? long long int Total_free = 0;</span>
<span class="CommentedCode">//? long long int Num_free = 0;</span>
<span class="CommentedCode">//? :(before "End Setup")</span>
<span class="CommentedCode">//? Total_alloc = Num_alloc = Total_free = Num_free = 0;</span>
<span class="CommentedCode">//? :(before "End Teardown")</span>
<span class="CommentedCode">//? cerr << Total_alloc << "/" << Num_alloc</span>
<span class="CommentedCode">//? << " vs " << Total_free << "/" << Num_free << '\n';</span>
<span class="CommentedCode">//? cerr << SIZE(Memory) << '\n';</span>
<span class="Delimiter">:(code)</span>
void ensure_space<span class="Delimiter">(</span>long long int size<span class="Delimiter">)</span> <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>size > Initial_memory_per_routine<span class="Delimiter">)</span> <span class="Delimiter">{</span>
tb_shutdown<span class="Delimiter">();</span>
cerr << <span class="Constant">"can't allocate "</span> << size << <span class="Constant">" locations, that's too much compared to "</span> << Initial_memory_per_routine << <span class="Constant">".</span><span class="cSpecial">\n</span><span class="Constant">"</span><span class="Delimiter">;</span>
exit<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
if <span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>alloc + size > Current_routine<span class="Delimiter">-></span>alloc_max<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// waste the remaining space and create a new chunk</span>
Current_routine<span class="Delimiter">-></span>alloc = Memory_allocated_until<span class="Delimiter">;</span>
Memory_allocated_until += Initial_memory_per_routine<span class="Delimiter">;</span>
Current_routine<span class="Delimiter">-></span>alloc_max = Memory_allocated_until<span class="Delimiter">;</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"new"</span><span class="Delimiter">)</span> << <span class="Constant">"routine allocated memory from "</span> << Current_routine<span class="Delimiter">-></span>alloc << <span class="Constant">" to "</span> << Current_routine<span class="Delimiter">-></span>alloc_max << end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario new_initializes)</span>
<span class="Special">% Memory_allocated_until = 10;</span>
<span class="Special">% put(Memory, Memory_allocated_until, 1);</span>
recipe main [
<span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>new number:type
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy *<span class="Constant">1</span>:address:shared:number
]
<span class="traceContains">+mem: storing 0 in location 2</span>
<span class="Delimiter">:(scenario new_error)</span>
<span class="Special">% Hide_errors = true;</span>
recipe main [
<span class="Constant">1</span>:address:number/<span class="Special">raw <- </span>new number:type
]
<span class="traceContains">+error: main: product of 'new' has incorrect type: 1:address:number/raw <- new number:type</span>
<span class="Delimiter">:(scenario new_array)</span>
recipe main [
<span class="Constant">1</span>:address:shared:array:number/<span class="Special">raw <- </span>new number:type<span class="Delimiter">,</span> <span class="Constant">5</span>
<span class="Constant">2</span>:address:shared:number/<span class="Special">raw <- </span>new number:type
<span class="Constant">3</span>:number/<span class="Special">raw <- </span>subtract <span class="Constant">2</span>:address:shared:number/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">1</span>:address:shared:array:number/<span class="Special">raw</span>
]
<span class="traceContains">+run: 1:address:shared:array:number/raw <- new number:type, 5</span>
<span class="traceContains">+mem: array size is 5</span>
<span class="Comment"># don't forget the extra location for array size, and the second extra location for the refcount</span>
<span class="traceContains">+mem: storing 7 in location 3</span>
<span class="Delimiter">:(scenario new_empty_array)</span>
recipe main [
<span class="Constant">1</span>:address:shared:array:number/<span class="Special">raw <- </span>new number:type<span class="Delimiter">,</span> <span class="Constant">0</span>
<span class="Constant">2</span>:address:shared:number/<span class="Special">raw <- </span>new number:type
<span class="Constant">3</span>:number/<span class="Special">raw <- </span>subtract <span class="Constant">2</span>:address:shared:number/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">1</span>:address:shared:array:number/<span class="Special">raw</span>
]
<span class="traceContains">+run: 1:address:shared:array:number/raw <- new number:type, 0</span>
<span class="traceContains">+mem: array size is 0</span>
<span class="Comment"># one location for array size, and one for the refcount</span>
<span class="traceContains">+mem: storing 2 in location 3</span>
<span class="Comment">//: If a routine runs out of its initial allocation, it should allocate more.</span>
<span class="Delimiter">:(scenario new_overflow)</span>
<span class="Special">% Initial_memory_per_routine = 3; // barely enough room for point allocation below</span>
recipe main [
<span class="Constant">1</span>:address:shared:number/<span class="Special">raw <- </span>new number:type
<span class="Constant">2</span>:address:shared:point/<span class="Special">raw <- </span>new point:type <span class="Comment"># not enough room in initial page</span>
]
<span class="traceContains">+new: routine allocated memory from 1000 to 1003</span>
<span class="traceContains">+new: routine allocated memory from 1003 to 1006</span>
<span class="SalientComment">//:: A way to return memory, and to reuse reclaimed memory.</span>
<span class="Comment">//: todo: custodians, etc. Following malloc/free is a temporary hack.</span>
<span class="Delimiter">:(scenario new_reclaim)</span>
recipe main [
<span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>new number:type
<span class="Constant">2</span>:address:shared:number<span class="Special"> <- </span>copy <span class="Constant">1</span>:address:shared:number <span class="Comment"># because 1 will get reset during abandon below</span>
abandon <span class="Constant">1</span>:address:shared:number <span class="Comment"># unsafe</span>
<span class="Constant">3</span>:address:shared:number<span class="Special"> <- </span>new number:type <span class="Comment"># must be same size as abandoned memory to reuse</span>
<span class="Constant">4</span>:boolean<span class="Special"> <- </span>equal <span class="Constant">2</span>:address:shared:number<span class="Delimiter">,</span> <span class="Constant">3</span>:address:shared:number
]
<span class="Comment"># both allocations should have returned the same address</span>
<span class="traceContains">+mem: storing 1 in location 4</span>
<span class="Delimiter">:(before "End Globals")</span>
map<long long int<span class="Delimiter">,</span> long long int> Free_list<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Setup")</span>
Free_list<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
ABANDON<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"abandon"</span><span class="Delimiter">,</span> ABANDON<span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</span>
case ABANDON: <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span> != <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise_error << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"'abandon' requires one ingredient, but got '"</span> << to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
reagent types = inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
canonize_type<span class="Delimiter">(</span>types<span class="Delimiter">);</span>
if <span class="Delimiter">(</span>!types<span class="Delimiter">.</span>type || types<span class="Delimiter">.</span>type<span class="Delimiter">-></span>value != get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">"address"</span><span class="Delimiter">)</span> || types<span class="Delimiter">.</span>type<span class="Delimiter">-></span>right<span class="Delimiter">-></span>value != get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">"shared"</span><span class="Delimiter">))</span> <span class="Delimiter">{</span>
raise_error << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"first ingredient of 'abandon' should be an address:shared:___, but got "</span> << inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>original_string << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
case ABANDON: <span class="Delimiter">{</span>
long long int address = ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"abandon"</span><span class="Delimiter">)</span> << <span class="Constant">"address to abandon is "</span> << address << end<span class="Delimiter">();</span>
reagent types = current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"abandon"</span><span class="Delimiter">)</span> << <span class="Constant">"value of ingredient is "</span> << types<span class="Delimiter">.</span>value << end<span class="Delimiter">();</span>
canonize<span class="Delimiter">(</span>types<span class="Delimiter">);</span>
<span class="Comment">// lookup_memory without drop_one_lookup {</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"abandon"</span><span class="Delimiter">)</span> << <span class="Constant">"value of ingredient after canonization is "</span> << types<span class="Delimiter">.</span>value << end<span class="Delimiter">();</span>
long long int address_location = types<span class="Delimiter">.</span>value<span class="Delimiter">;</span>
types<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> types<span class="Delimiter">.</span>value<span class="Delimiter">)</span>+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">);</span>
drop_from_type<span class="Delimiter">(</span>types<span class="Delimiter">,</span> <span class="Constant">"address"</span><span class="Delimiter">);</span>
drop_from_type<span class="Delimiter">(</span>types<span class="Delimiter">,</span> <span class="Constant">"shared"</span><span class="Delimiter">);</span>
<span class="Comment">// }</span>
abandon<span class="Delimiter">(</span>address<span class="Delimiter">,</span> size_of<span class="Delimiter">(</span>types<span class="Delimiter">)</span>+<span class="Comment">/*</span><span class="Comment">refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">);</span>
<span class="Comment">// clear the address</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"resetting location "</span> << address_location << end<span class="Delimiter">();</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> address_location<span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
void abandon<span class="Delimiter">(</span>long long int address<span class="Delimiter">,</span> long long int size<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">"abandon"</span><span class="Delimiter">)</span> << <span class="Constant">"saving in free-list of size "</span> << size << end<span class="Delimiter">();</span>
<span class="CommentedCode">//? Total_free += size;</span>
<span class="CommentedCode">//? Num_free++;</span>
<span class="CommentedCode">//? cerr << "abandon: " << size << '\n';</span>
<span class="Comment">// clear memory</span>
for <span class="Delimiter">(</span>long long int curr = address<span class="Delimiter">;</span> curr < address+size<span class="Delimiter">;</span> ++curr<span class="Delimiter">)</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr<span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Comment">// append existing free list to address</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> address<span class="Delimiter">,</span> get_or_insert<span class="Delimiter">(</span>Free_list<span class="Delimiter">,</span> size<span class="Delimiter">));</span>
put<span class="Delimiter">(</span>Free_list<span class="Delimiter">,</span> size<span class="Delimiter">,</span> address<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "ensure_space(size)" following "case ALLOCATE")</span>
if <span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Free_list<span class="Delimiter">,</span> size<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">"abandon"</span><span class="Delimiter">)</span> << <span class="Constant">"picking up space from free-list of size "</span> << size << end<span class="Delimiter">();</span>
long long int result = get_or_insert<span class="Delimiter">(</span>Free_list<span class="Delimiter">,</span> size<span class="Delimiter">);</span>
put<span class="Delimiter">(</span>Free_list<span class="Delimiter">,</span> size<span class="Delimiter">,</span> get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> result<span class="Delimiter">));</span>
for <span class="Delimiter">(</span>long long int curr = result+<span class="Constant">1</span><span class="Delimiter">;</span> curr < result+size<span class="Delimiter">;</span> ++curr<span class="Delimiter">)</span> <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr<span class="Delimiter">)</span> != <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise_error << maybe<span class="Delimiter">(</span>current_recipe_name<span class="Delimiter">())</span> << <span class="Constant">"memory in free list was not zeroed out: "</span> << curr << <span class="Constant">'/'</span> << result << <span class="Constant">"; somebody wrote to us after free!!!</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span> <span class="Comment">// always fatal</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
if <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">)</span> > <span class="Constant">1</span><span class="Delimiter">)</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> result+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">,</span> ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">));</span>
else
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> result<span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>result<span class="Delimiter">);</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario new_differing_size_no_reclaim)</span>
recipe main [
<span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>new number:type
<span class="Constant">2</span>:address:shared:number<span class="Special"> <- </span>copy <span class="Constant">1</span>:address:shared:number
abandon <span class="Constant">1</span>:address:shared:number
<span class="Constant">3</span>:address:shared:array:number<span class="Special"> <- </span>new number:type<span class="Delimiter">,</span> <span class="Constant">2</span> <span class="Comment"># different size</span>
<span class="Constant">4</span>:boolean<span class="Special"> <- </span>equal <span class="Constant">2</span>:address:shared:number<span class="Delimiter">,</span> <span class="Constant">3</span>:address:shared:array:number
]
<span class="Comment"># no reuse</span>
<span class="traceContains">+mem: storing 0 in location 4</span>
<span class="Delimiter">:(scenario new_reclaim_array)</span>
recipe main [
<span class="Constant">1</span>:address:shared:array:number<span class="Special"> <- </span>new number:type<span class="Delimiter">,</span> <span class="Constant">2</span>
<span class="Constant">2</span>:address:shared:array:number<span class="Special"> <- </span>copy <span class="Constant">1</span>:address:shared:array:number
abandon <span class="Constant">1</span>:address:shared:array:number <span class="Comment"># unsafe</span>
<span class="Constant">3</span>:address:shared:array:number<span class="Special"> <- </span>new number:type<span class="Delimiter">,</span> <span class="Constant">2</span>
<span class="Constant">4</span>:boolean<span class="Special"> <- </span>equal <span class="Constant">2</span>:address:shared:array:number<span class="Delimiter">,</span> <span class="Constant">3</span>:address:shared:array:number
]
<span class="Comment"># reuse</span>
<span class="traceContains">+mem: storing 1 in location 4</span>
<span class="Delimiter">:(scenario reset_on_abandon)</span>
recipe main [
<span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>new number:type
abandon <span class="Constant">1</span>:address:shared:number
]
<span class="Comment"># reuse</span>
<span class="traceContains">+run: abandon 1:address:shared:number</span>
<span class="traceContains">+mem: resetting location 1</span>
<span class="SalientComment">//:: Manage refcounts when copying addresses.</span>
<span class="Delimiter">:(scenario refcounts)</span>
recipe main [
<span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>copy <span class="Constant">1000</span>/unsafe
<span class="Constant">2</span>:address:shared:number<span class="Special"> <- </span>copy <span class="Constant">1</span>:address:shared:number
<span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Constant">2</span>:address:shared:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+run: 1:address:shared:number <- copy 1000/unsafe</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span>
<span class="traceContains">+run: 2:address:shared:number <- copy 1:address:shared:number</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 1 -> 2</span>
<span class="traceContains">+run: 1:address:shared:number <- copy 0</span>
<span class="traceContains">+mem: decrementing refcount of 1000: 2 -> 1</span>
<span class="traceContains">+run: 2:address:shared:number <- copy 0</span>
<span class="traceContains">+mem: decrementing refcount of 1000: 1 -> 0</span>
<span class="Comment"># the /unsafe corrupts memory but fortunately we won't be running any more 'new' in this scenario</span>
<span class="traceContains">+mem: automatically abandoning 1000</span>
<span class="Delimiter">:(before "End write_memory(reagent x, long long int base) Special-cases")</span>
if <span class="Delimiter">(</span>x<span class="Delimiter">.</span>type<span class="Delimiter">-></span>value == get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">"address"</span><span class="Delimiter">)</span>
&& x<span class="Delimiter">.</span>type<span class="Delimiter">-></span>right
&& x<span class="Delimiter">.</span>type<span class="Delimiter">-></span>right<span class="Delimiter">-></span>value == get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">"shared"</span><span class="Delimiter">))</span> <span class="Delimiter">{</span>
<span class="Comment">// compute old address of x, as well as new address we want to write in</span>
long long int old_address = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> x<span class="Delimiter">.</span>value<span class="Delimiter">);</span>
assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>data<span class="Delimiter">));</span>
long long int new_address = data<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Comment">// decrement refcount of old address</span>
if <span class="Delimiter">(</span>old_address<span class="Delimiter">)</span> <span class="Delimiter">{</span>
long long int old_refcount = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> old_address<span class="Delimiter">);</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"decrementing refcount of "</span> << old_address << <span class="Constant">": "</span> << old_refcount << <span class="Constant">" -> "</span> << <span class="Delimiter">(</span>old_refcount-<span class="Constant">1</span><span class="Delimiter">)</span> << end<span class="Delimiter">();</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> old_address<span class="Delimiter">,</span> old_refcount-<span class="Constant">1</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Comment">// perform the write</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"storing "</span> << no_scientific<span class="Delimiter">(</span>data<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span> << <span class="Constant">" in location "</span> << base << end<span class="Delimiter">();</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> base<span class="Delimiter">,</span> new_address<span class="Delimiter">);</span>
<span class="Comment">// increment refcount of new address</span>
if <span class="Delimiter">(</span>new_address<span class="Delimiter">)</span> <span class="Delimiter">{</span>
long long int new_refcount = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> new_address<span class="Delimiter">);</span>
assert<span class="Delimiter">(</span>new_refcount >= <span class="Constant">0</span><span class="Delimiter">);</span> <span class="Comment">// == 0 only when new_address == old_address</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"incrementing refcount of "</span> << new_address << <span class="Constant">": "</span> << new_refcount << <span class="Constant">" -> "</span> << <span class="Delimiter">(</span>new_refcount+<span class="Constant">1</span><span class="Delimiter">)</span> << end<span class="Delimiter">();</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> new_address<span class="Delimiter">,</span> new_refcount+<span class="Constant">1</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Comment">// abandon old address if necessary</span>
<span class="Comment">// do this after all refcount updates are done just in case old and new are identical</span>
assert<span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> old_address<span class="Delimiter">)</span> >= <span class="Constant">0</span><span class="Delimiter">);</span>
if <span class="Delimiter">(</span>old_address && get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> old_address<span class="Delimiter">)</span> == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// lookup_memory without drop_one_lookup {</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"automatically abandoning "</span> << old_address << end<span class="Delimiter">();</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"computing size to abandon at "</span> << x<span class="Delimiter">.</span>value << end<span class="Delimiter">();</span>
x<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> x<span class="Delimiter">.</span>value<span class="Delimiter">)</span>+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">);</span>
drop_from_type<span class="Delimiter">(</span>x<span class="Delimiter">,</span> <span class="Constant">"address"</span><span class="Delimiter">);</span>
drop_from_type<span class="Delimiter">(</span>x<span class="Delimiter">,</span> <span class="Constant">"shared"</span><span class="Delimiter">);</span>
<span class="Comment">// }</span>
abandon<span class="Delimiter">(</span>old_address<span class="Delimiter">,</span> size_of<span class="Delimiter">(</span>x<span class="Delimiter">)</span>+<span class="Comment">/*</span><span class="Comment">refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario refcounts_2)</span>
recipe main [
<span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>new number:type
<span class="Comment"># over-writing one allocation with another</span>
<span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>new number:type
<span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+run: 1:address:shared:number <- new number:type</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span>
<span class="traceContains">+run: 1:address:shared:number <- new number:type</span>
<span class="traceContains">+mem: automatically abandoning 1000</span>
<span class="Delimiter">:(scenario refcounts_3)</span>
recipe main [
<span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>new number:type
<span class="Comment"># passing in addresses to recipes increments refcount</span>
foo <span class="Constant">1</span>:address:shared:number
<span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
recipe foo [
<span class="Constant">2</span>:address:shared:number<span class="Special"> <- </span>next-ingredient
<span class="Comment"># return does NOT yet decrement refcount; memory must be explicitly managed</span>
<span class="Constant">2</span>:address:shared:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+run: 1:address:shared:number <- new number:type</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span>
<span class="traceContains">+run: 2:address:shared:number <- next-ingredient</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 1 -> 2</span>
<span class="traceContains">+run: 2:address:shared:number <- copy 0</span>
<span class="traceContains">+mem: decrementing refcount of 1000: 2 -> 1</span>
<span class="traceContains">+run: 1:address:shared:number <- copy 0</span>
<span class="traceContains">+mem: decrementing refcount of 1000: 1 -> 0</span>
<span class="traceContains">+mem: automatically abandoning 1000</span>
<span class="Delimiter">:(scenario refcounts_4)</span>
recipe main [
<span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>new number:type
<span class="Comment"># idempotent copies leave refcount unchanged</span>
<span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>copy <span class="Constant">1</span>:address:shared:number
]
<span class="traceContains">+run: 1:address:shared:number <- new number:type</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span>
<span class="traceContains">+run: 1:address:shared:number <- copy 1:address:shared:number</span>
<span class="traceContains">+mem: decrementing refcount of 1000: 1 -> 0</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span>
<span class="Delimiter">:(scenario refcounts_5)</span>
recipe main [
<span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>new number:type
<span class="Comment"># passing in addresses to recipes increments refcount</span>
foo <span class="Constant">1</span>:address:shared:number
<span class="Comment"># return does NOT yet decrement refcount; memory must be explicitly managed</span>
<span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>new number:type
]
recipe foo [
<span class="Constant">2</span>:address:shared:number<span class="Special"> <- </span>next-ingredient
]
<span class="traceContains">+run: 1:address:shared:number <- new number:type</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span>
<span class="traceContains">+run: 2:address:shared:number <- next-ingredient</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 1 -> 2</span>
<span class="traceContains">+run: 1:address:shared:number <- new number:type</span>
<span class="traceContains">+mem: decrementing refcount of 1000: 2 -> 1</span>
<span class="SalientComment">//:: Extend 'new' to handle a unicode string literal argument.</span>
<span class="Delimiter">:(scenario new_string)</span>
recipe main [
<span class="Constant">1</span>:address:shared:array:character<span class="Special"> <- </span>new [abc def]
<span class="Constant">2</span>:character<span class="Special"> <- </span>index *<span class="Constant">1</span>:address:shared:array:character<span class="Delimiter">,</span> <span class="Constant">5</span>
]
<span class="Comment"># number code for 'e'</span>
<span class="traceContains">+mem: storing 101 in location 2</span>
<span class="Delimiter">:(scenario new_string_handles_unicode)</span>
recipe main [
<span class="Constant">1</span>:address:shared:array:character<span class="Special"> <- </span>new [a«c]
<span class="Constant">2</span>:number<span class="Special"> <- </span>length *<span class="Constant">1</span>:address:shared:array:character
<span class="Constant">3</span>:character<span class="Special"> <- </span>index *<span class="Constant">1</span>:address:shared:array:character<span class="Delimiter">,</span> <span class="Constant">1</span>
]
<span class="traceContains">+mem: storing 3 in location 2</span>
<span class="Comment"># unicode for '«'</span>
<span class="traceContains">+mem: storing 171 in location 3</span>
<span class="Delimiter">:(before "End NEW Check Special-cases")</span>
if <span class="Delimiter">(</span>is_literal_string<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)))</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "Convert 'new' To 'allocate'")</span>
if <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name == <span class="Constant">"new"</span> && is_literal_string<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">:(after "case NEW" following "Primitive Recipe Implementations")</span>
if <span class="Delimiter">(</span>is_literal_string<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)))</span> <span class="Delimiter">{</span>
products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>new_mu_string<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name<span class="Delimiter">));</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
long long int new_mu_string<span class="Delimiter">(</span>const string& contents<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// allocate an array just large enough for it</span>
long long int string_length = unicode_length<span class="Delimiter">(</span>contents<span class="Delimiter">);</span>
<span class="CommentedCode">//? Total_alloc += string_length+1;</span>
<span class="CommentedCode">//? Num_alloc++;</span>
ensure_space<span class="Delimiter">(</span>string_length+<span class="Constant">1</span><span class="Delimiter">);</span> <span class="Comment">// don't forget the extra location for array size</span>
<span class="Comment">// initialize string</span>
long long int result = Current_routine<span class="Delimiter">-></span>alloc<span class="Delimiter">;</span>
<span class="Comment">// initialize refcount</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-></span>alloc++<span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Comment">// store length</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-></span>alloc++<span class="Delimiter">,</span> string_length<span class="Delimiter">);</span>
long long int curr = <span class="Constant">0</span><span class="Delimiter">;</span>
const char* raw_contents = contents<span class="Delimiter">.</span>c_str<span class="Delimiter">();</span>
for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i < string_length<span class="Delimiter">;</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
uint32_t curr_character<span class="Delimiter">;</span>
assert<span class="Delimiter">(</span>curr < SIZE<span class="Delimiter">(</span>contents<span class="Delimiter">));</span>
tb_utf8_char_to_unicode<span class="Delimiter">(</span>&curr_character<span class="Delimiter">,</span> &raw_contents[curr]<span class="Delimiter">);</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-></span>alloc<span class="Delimiter">,</span> curr_character<span class="Delimiter">);</span>
curr += tb_utf8_char_length<span class="Delimiter">(</span>raw_contents[curr]<span class="Delimiter">);</span>
++Current_routine<span class="Delimiter">-></span>alloc<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">// mu strings are not null-terminated in memory</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">//: stash recognizes strings</span>
<span class="Delimiter">:(scenario stash_string)</span>
recipe main [
<span class="Constant">1</span>:address:shared:array:character<span class="Special"> <- </span>new [abc]
stash [foo:]<span class="Delimiter">,</span> <span class="Constant">1</span>:address:shared:array:character
]
<span class="traceContains">+app: foo: abc</span>
<span class="Delimiter">:(before "End print Special-cases(reagent r, data)")</span>
if <span class="Delimiter">(</span>is_mu_string<span class="Delimiter">(</span>r<span class="Delimiter">))</span> <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>data<span class="Delimiter">));</span>
<span class="Identifier">return</span> read_mu_string<span class="Delimiter">(</span>data<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span>+<span class="Constant">' '</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario unicode_string)</span>
recipe main [
<span class="Constant">1</span>:address:shared:array:character<span class="Special"> <- </span>new [♠]
stash [foo:]<span class="Delimiter">,</span> <span class="Constant">1</span>:address:shared:array:character
]
<span class="traceContains">+app: foo: ♠</span>
<span class="Delimiter">:(scenario stash_space_after_string)</span>
recipe main [
<span class="Constant">1</span>:address:shared:array:character<span class="Special"> <- </span>new [abc]
stash <span class="Constant">1</span>:address:shared:array:character<span class="Delimiter">,</span> [foo]
]
<span class="traceContains">+app: abc foo</span>
<span class="Comment">//: Allocate more to routine when initializing a literal string</span>
<span class="Delimiter">:(scenario new_string_overflow)</span>
<span class="Special">% Initial_memory_per_routine = 2;</span>
recipe main [
<span class="Constant">1</span>:address:shared:number/<span class="Special">raw <- </span>new number:type
<span class="Constant">2</span>:address:shared:array:character/<span class="Special">raw <- </span>new [a] <span class="Comment"># not enough room in initial page, if you take the array size into account</span>
]
<span class="traceContains">+new: routine allocated memory from 1000 to 1002</span>
<span class="traceContains">+new: routine allocated memory from 1002 to 1004</span>
<span class="Comment">//: helpers</span>
<span class="Delimiter">:(code)</span>
long long int unicode_length<span class="Delimiter">(</span>const string& s<span class="Delimiter">)</span> <span class="Delimiter">{</span>
const char* in = s<span class="Delimiter">.</span>c_str<span class="Delimiter">();</span>
long long int result = <span class="Constant">0</span><span class="Delimiter">;</span>
long long int curr = <span class="Constant">0</span><span class="Delimiter">;</span>
while <span class="Delimiter">(</span>curr < SIZE<span class="Delimiter">(</span>s<span class="Delimiter">))</span> <span class="Delimiter">{</span> <span class="Comment">// carefully bounds-check on the string</span>
<span class="Comment">// before accessing its raw pointer</span>
++result<span class="Delimiter">;</span>
curr += tb_utf8_char_length<span class="Delimiter">(</span>in[curr]<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
string read_mu_string<span class="Delimiter">(</span>long long int address<span class="Delimiter">)</span> <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>address == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">""</span><span class="Delimiter">;</span>
address++<span class="Delimiter">;</span> <span class="Comment">// skip refcount</span>
long long int size = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> address<span class="Delimiter">);</span>
if <span class="Delimiter">(</span>size == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">""</span><span class="Delimiter">;</span>
ostringstream tmp<span class="Delimiter">;</span>
for <span class="Delimiter">(</span>long long int curr = address+<span class="Constant">1</span><span class="Delimiter">;</span> curr <= address+size<span class="Delimiter">;</span> ++curr<span class="Delimiter">)</span> <span class="Delimiter">{</span>
tmp << to_unicode<span class="Delimiter">(</span>static_cast<uint32_t><span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr<span class="Delimiter">)));</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> tmp<span class="Delimiter">.</span>str<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
bool is_mu_type_literal<span class="Delimiter">(</span>reagent r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> is_literal<span class="Delimiter">(</span>r<span class="Delimiter">)</span> && r<span class="Delimiter">.</span>type && r<span class="Delimiter">.</span>type<span class="Delimiter">-></span>name == <span class="Constant">"type"</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
|