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