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 :(before "End Mu Types Initialization")
50 type_ordinal continuation = Type_ordinal["continuation"] = Next_type_ordinal++;
51 Type[continuation].name = "continuation";
52
53
54 :(before "End is_mu_recipe Atom Cases(r)")
55 if (r.type->name == "continuation") return true;
56
57
58
59 :(after "Begin Reagent->Recipe(r, recipe_header)")
60 if (r.type->atom && r.type->name == "continuation") {
61 result_header.has_header = false;
62 return result_header;
63 }
64
65 :(scenario delimited_continuation)
66 recipe main [
67 1:continuation <- call-with-continuation-mark 233/mark, f, 77
68 2:number <- copy 5
69 {
70 ¦ 2:number <- call 1:continuation, 2:number
71 ¦ 3:boolean <- greater-or-equal 2:number, 8
72 ¦ break-if 3:boolean
73 ¦ loop
74 }
75 ]
76 recipe f [
77 11:number <- next-ingredient
78 12:number <- g 11:number
79 return 12:number
80 ]
81 recipe g [
82 21:number <- next-ingredient
83 22:number <- return-continuation-until-mark 233/mark
84 23:number <- add 22:number, 1
85 return 23:number
86 ]
87
88 +mem: storing 77 in location 21
89 +run: {2: "number"} <- copy {5: "literal"}
90 +mem: storing 5 in location 2
91
92 +run: {2: "number"} <- call {1: "continuation"}, {2: "number"}
93 +mem: storing 5 in location 22
94 +mem: storing 6 in location 2
95 +run: {2: "number"} <- call {1: "continuation"}, {2: "number"}
96 +mem: storing 6 in location 22
97 +mem: storing 7 in location 2
98 +run: {2: "number"} <- call {1: "continuation"}, {2: "number"}
99 +mem: storing 7 in location 22
100 +mem: storing 8 in location 2
101
102 -mem: storing 77 in location 22
103
104 -mem: storing 5 in location 21
105 -mem: storing 6 in location 21
106 -mem: storing 7 in location 21
107
108 -mem: storing 9 in location 2
109
110 :(before "End call Fields")
111 int is_base_of_continuation;
112 :(before "End call Constructor")
113 is_base_of_continuation = 0;
114
115 :(before "End Primitive Recipe Declarations")
116 CALL_WITH_CONTINUATION_MARK,
117 :(before "End Primitive Recipe Numbers")
118 Recipe_ordinal["call-with-continuation-mark"] = CALL_WITH_CONTINUATION_MARK;
119 :(before "End Primitive Recipe Checks")
120 case CALL_WITH_CONTINUATION_MARK: {
121 break;
122 }
123 :(before "End Primitive Recipe Implementations")
124 case CALL_WITH_CONTINUATION_MARK: {
125
126
127 if (Trace_stream) {
128 ¦ ++Trace_stream->callstack_depth;
129 ¦ trace("trace") << "delimited continuation; incrementing callstack depth to " << Trace_stream->callstack_depth << end();
130 ¦ assert(Trace_stream->callstack_depth < 9000);
131 }
132 const instruction& caller_instruction = current_instruction();
133 Current_routine->calls.front().is_base_of_continuation = current_instruction().ingredients.at(0).value;
134 Current_routine->calls.push_front(call(Recipe_ordinal[current_instruction().ingredients.at(1).name]));
135 ingredients.erase(ingredients.begin());
136 ingredients.erase(ingredients.begin());
137 finish_call_housekeeping(caller_instruction, ingredients);
138 continue;
139 }
140
141
142
143
144 :(before "End Types")
145 struct delimited_continuation {
146 call_stack frames;
147 int nrefs;
148 delimited_continuation(call_stack::iterator begin, call_stack::iterator end) :frames(call_stack(begin, end)), nrefs(0) {}
149 };
150 :(before "End Globals")
151 map<long long int, delimited_continuation> Delimited_continuation;
152 long long int Next_delimited_continuation_id = 1;
153 :(before "End Reset")
154 Delimited_continuation.clear();
155 Next_delimited_continuation_id = 1;
156
157 :(before "End Primitive Recipe Declarations")
158 RETURN_CONTINUATION_UNTIL_MARK,
159 :(before "End Primitive Recipe Numbers")
160 Recipe_ordinal["return-continuation-until-mark"] = RETURN_CONTINUATION_UNTIL_MARK;
161 :(before "End Primitive Recipe Checks")
162 case RETURN_CONTINUATION_UNTIL_MARK: {
163 break;
164 }
165 :(before "End Primitive Recipe Implementations")
166 case RETURN_CONTINUATION_UNTIL_MARK: {
167
168
169
170 Current_routine->calls.front().ingredient_atoms.clear();
171 Current_routine->calls.front().next_ingredient_to_process = 0;
172
173 call_stack::iterator find_base_of_continuation(call_stack&, int);
174 call_stack::iterator base = find_base_of_continuation(Current_routine->calls, current_instruction().ingredients.at(0).value);
175 if (base == Current_routine->calls.end()) {
176 ¦ raise << maybe(current_recipe_name()) << "couldn't find a 'call-with-continuation-mark' to return to\n" << end();
177 ¦ raise << maybe(current_recipe_name()) << "call stack:\n" << end();
178 ¦ for (call_stack::iterator p = Current_routine->calls.begin(); p != Current_routine->calls.end(); ++p)
179 ¦ ¦ raise << maybe(current_recipe_name()) << " " << get(Recipe, p->running_recipe).name << '\n' << end();
180 ¦ break;
181 }
182 trace("run") << "creating continuation " << Next_delimited_continuation_id << end();
183 put(Delimited_continuation, Next_delimited_continuation_id, delimited_continuation(Current_routine->calls.begin(), base));
184 while (Current_routine->calls.begin() != base) {
185 ¦ if (Trace_stream) {
186 ¦ ¦ --Trace_stream->callstack_depth;
187 ¦ ¦ assert(Trace_stream->callstack_depth >= 0);
188 ¦ }
189 ¦ Current_routine->calls.pop_front();
190 }
191
192 products.resize(1);
193 products.at(0).push_back(Next_delimited_continuation_id);
194
195 copy(++ingredients.begin(), ingredients.end(), inserter(products, products.end()));
196 ++Next_delimited_continuation_id;
197 break;
198 }
199
200 :(code)
201 call_stack::iterator find_base_of_continuation(call_stack& c, int mark) {
202 for (call_stack::iterator p = c.begin(); p != c.end(); ++p)
203 ¦ if (p->is_base_of_continuation == mark) return p;
204 return c.end();
205 }
206
207
208 :(after "Begin Call")
209 if (is_mu_continuation(current_instruction().ingredients.at(0))) {
210
211 assert(scalar(ingredients.at(0)));
212 trace("run") << "calling continuation " << ingredients.at(0).at(0) << end();
213 if (!contains_key(Delimited_continuation, ingredients.at(0).at(0)))
214 ¦ raise << maybe(current_recipe_name()) << "no such delimited continuation " << current_instruction().ingredients.at(0).original_string << '\n' << end();
215 const call_stack& new_frames = get(Delimited_continuation, ingredients.at(0).at(0)).frames;
216 for (call_stack::const_reverse_iterator p = new_frames.rbegin(); p != new_frames.rend(); ++p) {
217 ¦ Current_routine->calls.push_front(*p);
218 ¦
219 ¦ increment_refcount(Current_routine->calls.front().default_space);
220 }
221 if (Trace_stream) {
222 ¦ Trace_stream->callstack_depth += SIZE(new_frames);
223 ¦ trace("trace") << "calling delimited continuation; growing callstack depth to " << Trace_stream->callstack_depth << end();
224 ¦ assert(Trace_stream->callstack_depth < 9000);
225 }
226
227 copy(++ingredients.begin(), ingredients.end(), inserter(products, products.begin()));
228 break;
229 }
230
231 :(scenario continuations_can_return_values)
232 def main [
233 local-scope
234 k:continuation, 1:num/raw <- call-with-continuation-mark 233/mark, f
235 ]
236 def f [
237 local-scope
238 g
239 ]
240 def g [
241 local-scope
242 return-continuation-until-mark 233/mark, 34
243 stash [continuation called]
244 ]
245
246 +mem: new alloc: 1000
247 +run: {k: "continuation"}, {1: "number", "raw": ()} <- call-with-continuation-mark {233: "literal", "mark": ()}, {f: "recipe-literal"}
248
249 +mem: new alloc: 1004
250
251 +mem: new alloc: 1007
252
253 +run: return-continuation-until-mark {233: "literal", "mark": ()}, {34: "literal"}
254
255 +mem: storing 34 in location 1
256
257
258 +mem: trying to reclaim local k:continuation
259 +mem: automatically abandoning 1007
260 +mem: automatically abandoning 1004
261 +mem: automatically abandoning 1000
262
263 -app: continuation called
264
265 :(scenario continuations_continue_to_matching_mark)
266 def main [
267 local-scope
268 k:continuation, 1:num/raw <- call-with-continuation-mark 233/mark, f
269 add 1, 1
270 ]
271 def f [
272 local-scope
273 k2:continuation <- call-with-continuation-mark 234/mark, g
274 add 2, 2
275 ]
276 def g [
277 local-scope
278 return-continuation-until-mark 233/mark, 34
279 stash [continuation called]
280 ]
281 +run: add {1: "literal"}, {1: "literal"}
282 -run: add {2: "literal"}, {2: "literal"}
283
284
285
286 :(scenario call_shape_shifting_recipe_with_continuation_mark)
287 def main [
288 1:num <- call-with-continuation-mark 233/mark, f, 34
289 ]
290 def f x:_elem -> y:_elem [
291 local-scope
292 load-ingredients
293 y <- copy x
294 ]
295 +mem: storing 34 in location 1
296
297 :(before "End resolve_ambiguous_call(r, index, inst, caller_recipe) Special-cases")
298 if (inst.name == "call-with-continuation-mark") {
299 if (SIZE(inst.ingredients) > 1 && is_recipe_literal(inst.ingredients.at(1))) {
300 resolve_indirect_continuation_call(r, index, inst, caller_recipe);
301 return;
302 }
303 }
304 :(code)
305 void resolve_indirect_continuation_call(const recipe_ordinal r, int index, instruction& inst, const recipe& caller_recipe) {
306 instruction inst2;
307 inst2.name = inst.ingredients.at(1).name;
308 for (int i = 2; i < SIZE(inst.ingredients); ++i)
309 ¦ inst2.ingredients.push_back(inst.ingredients.at(i));
310 for (int i = 1; i < SIZE(inst.products); ++i)
311 ¦ inst2.products.push_back(inst.products.at(i));
312 resolve_ambiguous_call(r, index, inst2, caller_recipe);
313 inst.ingredients.at(1).name = inst2.name;
314 inst.ingredients.at(1).set_value(get(Recipe_ordinal, inst2.name));
315 }
316
317 :(scenario call_shape_shifting_recipe_with_continuation_mark_and_no_outputs)
318 def main [
319 1:continuation <- call-with-continuation-mark 233/mark, f, 34
320 ]
321 def f x:_elem [
322 local-scope
323 load-ingredients
324 return-continuation-until-mark 233/mark
325 ]
326 $error: 0
327
328 :(scenario continuation1)
329 def main [
330 local-scope
331 k:continuation <- call-with-continuation-mark 233/mark, create-yielder
332 10:num/raw <- call k
333 ]
334 def create-yielder -> n:num [
335 local-scope
336 load-inputs
337 return-continuation-until-mark 233/mark
338 return 1
339 ]
340 +mem: storing 1 in location 10
341 $error: 0
342
343
344
345 :(scenario continuations_preserve_local_scopes)
346 def main [
347 local-scope
348 k:continuation <- call-with-continuation-mark 233/mark, f
349 call k
350 return 34
351 ]
352 def f [
353 local-scope
354 g
355 ]
356 def g [
357 local-scope
358 return-continuation-until-mark 233/mark
359 add 1, 1
360 ]
361
362 +mem: new alloc: 1000
363 +run: {k: "continuation"} <- call-with-continuation-mark {233: "literal", "mark": ()}, {f: "recipe-literal"}
364
365 +mem: new alloc: 1004
366
367 +mem: new alloc: 1007
368
369 +run: return-continuation-until-mark {233: "literal", "mark": ()}
370
371
372 +run: call {k: "continuation"}
373 +run: add {1: "literal"}, {1: "literal"}
374 +run: return {34: "literal"}
375
376 +mem: trying to reclaim local k:continuation
377
378 +mem: automatically abandoning 1007
379 +mem: automatically abandoning 1004
380
381 +mem: automatically abandoning 1000
382
383 :(before "End Increment Refcounts(canonized_x)")
384 if (is_mu_continuation(canonized_x)) {
385 int continuation_id = data.at(0);
386 if (continuation_id == 0) return;
387 if (!contains_key(Delimited_continuation, continuation_id)) {
388 ¦ raise << maybe(current_recipe_name()) << "missing delimited continuation: " << canonized_x.name << '\n';
389 ¦ return;
390 }
391 delimited_continuation& curr = get(Delimited_continuation, continuation_id);
392 trace("run") << "incrementing refcount of continuation " << continuation_id << ": " << curr.nrefs << " -> " << 1+curr.nrefs << end();
393 ++curr.nrefs;
394 return;
395 }
396
397 :(before "End Decrement Refcounts(canonized_x)")
398 if (is_mu_continuation(canonized_x)) {
399 int continuation_id = get_or_insert(Memory, canonized_x.value);
400 if (continuation_id == 0) return;
401 delimited_continuation& curr = get(Delimited_continuation, continuation_id);
402 assert(curr.nrefs > 0);
403 --curr.nrefs;
404 trace("run") << "decrementing refcount of continuation " << continuation_id << ": " << 1+curr.nrefs << " -> " << curr.nrefs << end();
405 if (curr.nrefs > 0) return;
406 trace("run") << "reclaiming continuation " << continuation_id << end();
407
408
409 for (call_stack::const_iterator p = curr.frames.begin(); p != curr.frames.end(); ++p) {
410 ¦ Current_routine->calls.push_front(*p);
411 ¦ reclaim_default_space();
412 ¦ Current_routine->calls.pop_front();
413 }
414 Delimited_continuation.erase(continuation_id);
415 return;
416 }
417
418 :(scenario continuations_can_be_copied)
419 def main [
420 local-scope
421 k:continuation <- call-with-continuation-mark 233/mark, f
422 k2:continuation <- copy k
423
424 ]
425 def f [
426 local-scope
427 load-ingredients
428 return-continuation-until-mark 233/mark
429 return 0
430 ]
431 $error: 0
432
433 :(code)
434 bool is_mu_continuation(reagent x) {
435 canonize_type(x);
436 return x.type && x.type->atom && x.type->value == get(Type_ordinal, "continuation");
437 }
438
439
440 void dump(const int continuation_id) {
441 if (!contains_key(Delimited_continuation, continuation_id)) {
442 ¦ raise << "missing delimited continuation: " << continuation_id << '\n' << end();
443 ¦ return;
444 }
445 delimited_continuation& curr = get(Delimited_continuation, continuation_id);
446 dump(curr.frames);
447 }