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 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
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 bool is_base_of_continuation;
112 :(before "End call Constructor")
113 is_base_of_continuation = false;
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 = true;
134 Current_routine->calls.push_front(call(Recipe_ordinal[current_instruction().ingredients.at(0).name]));
135 ingredients.erase(ingredients.begin());
136 finish_call_housekeeping(caller_instruction, ingredients);
137 continue;
138 }
139
140
141
142
143 :(before "End Types")
144 struct delimited_continuation {
145 call_stack frames;
146 int nrefs;
147 delimited_continuation(call_stack::iterator begin, call_stack::iterator end) :frames(call_stack(begin, end)), nrefs(0) {}
148 };
149 :(before "End Globals")
150 map<long long int, delimited_continuation> Delimited_continuation;
151 long long int Next_delimited_continuation_id = 1;
152 :(before "End Reset")
153 Delimited_continuation.clear();
154 Next_delimited_continuation_id = 1;
155
156 :(before "End Primitive Recipe Declarations")
157 RETURN_CONTINUATION_UNTIL_MARK,
158 :(before "End Primitive Recipe Numbers")
159 Recipe_ordinal["return-continuation-until-mark"] = RETURN_CONTINUATION_UNTIL_MARK;
160 :(before "End Primitive Recipe Checks")
161 case RETURN_CONTINUATION_UNTIL_MARK: {
162 break;
163 }
164 :(before "End Primitive Recipe Implementations")
165 case RETURN_CONTINUATION_UNTIL_MARK: {
166
167
168 Current_routine->calls.front().ingredient_atoms.clear();
169 Current_routine->calls.front().next_ingredient_to_process = 0;
170
171 call_stack::iterator find_base_of_continuation(call_stack& c);
172 call_stack::iterator base = find_base_of_continuation(Current_routine->calls);
173 if (base == Current_routine->calls.end()) {
174 ¦ raise << maybe(current_recipe_name()) << "couldn't find a 'call-with-continuation-mark' to return to\n" << end();
175 ¦ raise << maybe(current_recipe_name()) << "call stack:\n" << end();
176 ¦ for (call_stack::iterator p = Current_routine->calls.begin(); p != Current_routine->calls.end(); ++p)
177 ¦ ¦ raise << maybe(current_recipe_name()) << " " << get(Recipe, p->running_recipe).name << '\n' << end();
178 ¦ break;
179 }
180 trace("run") << "creating continuation " << Next_delimited_continuation_id << end();
181 put(Delimited_continuation, Next_delimited_continuation_id, delimited_continuation(Current_routine->calls.begin(), base));
182 while (Current_routine->calls.begin() != base) {
183 ¦ if (Trace_stream) {
184 ¦ ¦ --Trace_stream->callstack_depth;
185 ¦ ¦ assert(Trace_stream->callstack_depth >= 0);
186 ¦ }
187 ¦ Current_routine->calls.pop_front();
188 }
189
190 products.resize(1);
191 products.at(0).push_back(Next_delimited_continuation_id);
192
193 copy(ingredients.begin(), ingredients.end(), inserter(products, products.end()));
194 ++Next_delimited_continuation_id;
195 break;
196 }
197
198 :(code)
199 call_stack::iterator find_base_of_continuation(call_stack& c) {
200 for (call_stack::iterator p = c.begin(); p != c.end(); ++p)
201 ¦ if (p->is_base_of_continuation) return p;
202 return c.end();
203 }
204
205
206 :(after "Begin Call")
207 if (is_mu_continuation(current_instruction().ingredients.at(0))) {
208
209 assert(scalar(ingredients.at(0)));
210 trace("run") << "calling continuation " << ingredients.at(0).at(0) << end();
211 if (!contains_key(Delimited_continuation, ingredients.at(0).at(0)))
212 ¦ raise << maybe(current_recipe_name()) << "no such delimited continuation " << current_instruction().ingredients.at(0).original_string << '\n' << end();
213 const call_stack& new_frames = get(Delimited_continuation, ingredients.at(0).at(0)).frames;
214 for (call_stack::const_reverse_iterator p = new_frames.rbegin(); p != new_frames.rend(); ++p) {
215 ¦ Current_routine->calls.push_front(*p);
216 ¦
217 ¦ increment_refcount(Current_routine->calls.front().default_space);
218 }
219 if (Trace_stream) {
220 ¦ Trace_stream->callstack_depth += SIZE(new_frames);
221 ¦ trace("trace") << "calling delimited continuation; growing callstack depth to " << Trace_stream->callstack_depth << end();
222 ¦ assert(Trace_stream->callstack_depth < 9000);
223 }
224
225 copy(++ingredients.begin(), ingredients.end(), inserter(products, products.begin()));
226 break;
227 }
228
229 :(scenario continuations_can_return_values)
230 def main [
231 local-scope
232 k:continuation, 1:num/raw <- call-with-continuation-mark f
233 ]
234 def f [
235 local-scope
236 g
237 ]
238 def g [
239 local-scope
240 return-continuation-until-mark 34
241 stash [continuation called]
242 ]
243
244 +mem: new alloc: 1000
245 +run: {k: "continuation"}, {1: "number", "raw": ()} <- call-with-continuation-mark {f: "recipe-literal"}
246
247 +mem: new alloc: 1004
248
249 +mem: new alloc: 1007
250
251 +run: return-continuation-until-mark {34: "literal"}
252
253 +mem: storing 34 in location 1
254
255
256 +mem: trying to reclaim local k:continuation
257 +mem: automatically abandoning 1007
258 +mem: automatically abandoning 1004
259 +mem: automatically abandoning 1000
260
261 -app: continuation called
262
263
264
265 :(scenario continuations_preserve_local_scopes)
266 def main [
267 local-scope
268 k:continuation <- call-with-continuation-mark f
269 call k
270 return 34
271 ]
272 def f [
273 local-scope
274 g
275 ]
276 def g [
277 local-scope
278 return-continuation-until-mark
279 add 1, 1
280 ]
281
282 +mem: new alloc: 1000
283 +run: {k: "continuation"} <- call-with-continuation-mark {f: "recipe-literal"}
284
285 +mem: new alloc: 1004
286
287 +mem: new alloc: 1007
288
289 +run: return-continuation-until-mark
290
291
292 +run: call {k: "continuation"}
293 +run: add {1: "literal"}, {1: "literal"}
294 +run: return {34: "literal"}
295
296 +mem: trying to reclaim local k:continuation
297
298 +mem: automatically abandoning 1007
299 +mem: automatically abandoning 1004
300
301 +mem: automatically abandoning 1000
302
303 :(before "End Increment Refcounts(canonized_x)")
304 if (is_mu_continuation(canonized_x)) {
305 int continuation_id = data.at(0);
306 if (continuation_id == 0) return;
307 if (!contains_key(Delimited_continuation, continuation_id)) {
308 ¦ raise << maybe(current_recipe_name()) << "missing delimited continuation: " << canonized_x.name << '\n';
309 ¦ return;
310 }
311 delimited_continuation& curr = get(Delimited_continuation, continuation_id);
312 trace("run") << "incrementing refcount of continuation " << continuation_id << ": " << curr.nrefs << " -> " << 1+curr.nrefs << end();
313 ++curr.nrefs;
314 return;
315 }
316
317 :(before "End Decrement Refcounts(canonized_x)")
318 if (is_mu_continuation(canonized_x)) {
319 int continuation_id = get_or_insert(Memory, canonized_x.value);
320 if (continuation_id == 0) return;
321 delimited_continuation& curr = get(Delimited_continuation, continuation_id);
322 assert(curr.nrefs > 0);
323 --curr.nrefs;
324 trace("run") << "decrementing refcount of continuation " << continuation_id << ": " << 1+curr.nrefs << " -> " << curr.nrefs << end();
325 if (curr.nrefs > 0) return;
326 trace("run") << "reclaiming continuation " << continuation_id << end();
327
328
329 for (call_stack::const_iterator p = curr.frames.begin(); p != curr.frames.end(); ++p) {
330 ¦ Current_routine->calls.push_front(*p);
331 ¦ reclaim_default_space();
332 ¦ Current_routine->calls.pop_front();
333 }
334 Delimited_continuation.erase(continuation_id);
335 return;
336 }
337
338 :(code)
339 bool is_mu_continuation(reagent x) {
340 canonize_type(x);
341 return x.type && x.type->atom && x.type->value == get(Type_ordinal, "continuation");
342 }
343
344 :(scenario continuations_can_be_copied)
345 def main [
346 local-scope
347 k:continuation <- call-with-continuation-mark f
348 k2:continuation <- copy k
349
350 ]
351 def f [
352 local-scope
353 load-ingredients
354 return-continuation-until-mark
355 return 0
356 ]
357 $error: 0