From d55e77387fd9cc3eae2f17a6c1993f3bcb061a60 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 6 Nov 2017 01:28:53 -0800 Subject: 4117 - done with delimited continuations At least this particular implementation of them. Let's play with them now for a while, see if they're fully equivalent to shift/reduce. --- html/076continuation.cc.html | 579 ++++++++++++++++++++++--------------------- 1 file changed, 291 insertions(+), 288 deletions(-) (limited to 'html/076continuation.cc.html') diff --git a/html/076continuation.cc.html b/html/076continuation.cc.html index 3b5dbe58..1aaf4423 100644 --- a/html/076continuation.cc.html +++ b/html/076continuation.cc.html @@ -95,296 +95,299 @@ if ('onhashchange' in window) { 31 //: * 'return-continuation-until-mark' can take ingredients to return just 32 //: like other 'return' instructions. It just implicitly also returns a 33 //: continuation as the first result. See continuation4.mu. - 34 //: - 35 //: Caveats: - 36 //: * At the moment we can't statically type-check continuations. So we raise - 37 //: runtime errors for a call that doesn't return a continuation when the - 38 //: caller expects, or one that returns a continuation when the caller - 39 //: doesn't expect it. This shouldn't cause memory corruption, though. - 40 //: There should still be no way to lookup addresses that aren't allocated. - 41 - 42 :(before "End Mu Types Initialization") - 43 type_ordinal continuation = Type_ordinal["continuation"] = Next_type_ordinal++; - 44 Type[continuation].name = "continuation"; - 45 - 46 //: A continuation can be called like a recipe. - 47 :(before "End is_mu_recipe Atom Cases(r)") - 48 if (r.type->name == "continuation") return true; - 49 - 50 //: However, it can't be type-checked like most recipes. Pretend it's like a - 51 //: header-less recipe. - 52 :(after "Begin Reagent->Recipe(r, recipe_header)") - 53 if (r.type->atom && r.type->name == "continuation") { - 54 result_header.has_header = false; - 55 return result_header; - 56 } - 57 - 58 :(scenario delimited_continuation) - 59 recipe main [ - 60 1:continuation <- call-with-continuation-mark f, 77 # 77 is an argument to f - 61 2:number <- copy 5 - 62 { - 63 ¦ 2:number <- call 1:continuation, 2:number # 2 is an argument to g, the 'top' of the continuation - 64 ¦ 3:boolean <- greater-or-equal 2:number, 8 - 65 ¦ break-if 3:boolean - 66 ¦ loop - 67 } - 68 ] - 69 recipe f [ - 70 11:number <- next-ingredient - 71 12:number <- g 11:number - 72 return 12:number - 73 ] - 74 recipe g [ - 75 21:number <- next-ingredient - 76 rewind-ingredients - 77 return-continuation-until-mark - 78 # calls of the continuation start from here - 79 22:number <- next-ingredient - 80 23:number <- add 22:number, 1 - 81 return 23:number - 82 ] - 83 # first call of 'g' executes the part before return-continuation-until-mark - 84 +mem: storing 77 in location 21 - 85 +run: {2: "number"} <- copy {5: "literal"} - 86 +mem: storing 5 in location 2 - 87 # calls of the continuation execute the part after return-continuation-until-mark - 88 +run: {2: "number"} <- call {1: "continuation"}, {2: "number"} - 89 +mem: storing 5 in location 22 - 90 +mem: storing 6 in location 2 + 34 //: * Conversely, you can pass ingredients to a continuation when calling it, + 35 //: to make it available to products of 'return-continuation-until-mark'. + 36 //: + 37 //: Inspired by James and Sabry, "Yield: Mainstream delimited continuations", + 38 //: Workshop on the Theory and Practice of Delimited Continuations, 2011. + 39 //: https://www.cs.indiana.edu/~sabry/papers/yield.pdf + 40 //: + 41 //: Caveats: + 42 //: * At the moment we can't statically type-check continuations. So we raise + 43 //: runtime errors for a call that doesn't return a continuation when the + 44 //: caller expects, or one that returns a continuation when the caller + 45 //: doesn't expect it. This shouldn't cause memory corruption, though. + 46 //: There should still be no way to lookup addresses that aren't allocated. + 47 + 48 :(before "End Mu Types Initialization") + 49 type_ordinal continuation = Type_ordinal["continuation"] = Next_type_ordinal++; + 50 Type[continuation].name = "continuation"; + 51 + 52 //: A continuation can be called like a recipe. + 53 :(before "End is_mu_recipe Atom Cases(r)") + 54 if (r.type->name == "continuation") return true; + 55 + 56 //: However, it can't be type-checked like most recipes. Pretend it's like a + 57 //: header-less recipe. + 58 :(after "Begin Reagent->Recipe(r, recipe_header)") + 59 if (r.type->atom && r.type->name == "continuation") { + 60 result_header.has_header = false; + 61 return result_header; + 62 } + 63 + 64 :(scenario delimited_continuation) + 65 recipe main [ + 66 1:continuation <- call-with-continuation-mark f, 77 # 77 is an argument to f + 67 2:number <- copy 5 + 68 { + 69 ¦ 2:number <- call 1:continuation, 2:number # jump to 'return-continuation-until-mark' below + 70 ¦ 3:boolean <- greater-or-equal 2:number, 8 + 71 ¦ break-if 3:boolean + 72 ¦ loop + 73 } + 74 ] + 75 recipe f [ + 76 11:number <- next-ingredient + 77 12:number <- g 11:number + 78 return 12:number + 79 ] + 80 recipe g [ + 81 21:number <- next-ingredient + 82 22:number <- return-continuation-until-mark + 83 23:number <- add 22:number, 1 + 84 return 23:number + 85 ] + 86 # first call of 'g' executes the part before return-continuation-until-mark + 87 +mem: storing 77 in location 21 + 88 +run: {2: "number"} <- copy {5: "literal"} + 89 +mem: storing 5 in location 2 + 90 # calls of the continuation execute the part after return-continuation-until-mark 91 +run: {2: "number"} <- call {1: "continuation"}, {2: "number"} - 92 +mem: storing 6 in location 22 - 93 +mem: storing 7 in location 2 + 92 +mem: storing 5 in location 22 + 93 +mem: storing 6 in location 2 94 +run: {2: "number"} <- call {1: "continuation"}, {2: "number"} - 95 +mem: storing 7 in location 22 - 96 +mem: storing 8 in location 2 - 97 # first call of 'g' does not execute the part after return-continuation-until-mark - 98 -mem: storing 77 in location 22 - 99 # calls of the continuation don't execute the part before return-continuation-until-mark -100 -mem: storing 5 in location 21 -101 -mem: storing 6 in location 21 -102 -mem: storing 7 in location 21 -103 # termination -104 -mem: storing 9 in location 2 -105 -106 :(before "End call Fields") -107 bool is_base_of_continuation; -108 :(before "End call Constructor") -109 is_base_of_continuation = false; -110 -111 :(before "End Primitive Recipe Declarations") -112 CALL_WITH_CONTINUATION_MARK, -113 :(before "End Primitive Recipe Numbers") -114 Recipe_ordinal["call-with-continuation-mark"] = CALL_WITH_CONTINUATION_MARK; -115 :(before "End Primitive Recipe Checks") -116 case CALL_WITH_CONTINUATION_MARK: { -117 break; -118 } -119 :(before "End Primitive Recipe Implementations") -120 case CALL_WITH_CONTINUATION_MARK: { -121 // like call, but mark the current call as a 'base of continuation' call -122 // before pushing the next one on it -123 if (Trace_stream) { -124 ¦ ++Trace_stream->callstack_depth; -125 ¦ trace("trace") << "delimited continuation; incrementing callstack depth to " << Trace_stream->callstack_depth << end(); -126 ¦ assert(Trace_stream->callstack_depth < 9000); // 9998-101 plus cushion -127 } -128 const instruction& caller_instruction = current_instruction(); -129 Current_routine->calls.front().is_base_of_continuation = true; -130 Current_routine->calls.push_front(call(Recipe_ordinal[current_instruction().ingredients.at(0).name])); -131 ingredients.erase(ingredients.begin()); // drop the callee -132 finish_call_housekeeping(caller_instruction, ingredients); -133 continue; -134 } -135 -136 //: save the slice of current call stack until the 'call-with-continuation-mark' -137 //: call, and return it as the result. -138 //: todo: implement delimited continuations in Mu's memory -139 :(before "End Globals") -140 map<long long int, call_stack> Delimited_continuation; -141 long long int Next_delimited_continuation_id = 1; // 0 is null just like an address -142 :(before "End Reset") -143 Delimited_continuation.clear(); -144 Next_delimited_continuation_id = 1; -145 -146 :(before "End Primitive Recipe Declarations") -147 RETURN_CONTINUATION_UNTIL_MARK, -148 :(before "End Primitive Recipe Numbers") -149 Recipe_ordinal["return-continuation-until-mark"] = RETURN_CONTINUATION_UNTIL_MARK; -150 :(before "End Primitive Recipe Checks") -151 case RETURN_CONTINUATION_UNTIL_MARK: { -152 break; -153 } -154 :(before "End Primitive Recipe Implementations") -155 case RETURN_CONTINUATION_UNTIL_MARK: { -156 // first clear any existing ingredients, to isolate the creation of the -157 // continuation from its calls -158 Current_routine->calls.front().ingredient_atoms.clear(); -159 Current_routine->calls.front().next_ingredient_to_process = 0; -160 // copy the current call stack until the most recent marked call -161 call_stack::iterator find_base_of_continuation(call_stack& c); // manual prototype containing '::' -162 call_stack::iterator base = find_base_of_continuation(Current_routine->calls); -163 if (base == Current_routine->calls.end()) { -164 ¦ raise << maybe(current_recipe_name()) << "couldn't find a 'call-with-continuation-mark' to return to\n" << end(); -165 ¦ raise << maybe(current_recipe_name()) << "call stack:\n" << end(); -166 ¦ for (call_stack::iterator p = Current_routine->calls.begin(); p != Current_routine->calls.end(); ++p) -167 ¦ ¦ raise << maybe(current_recipe_name()) << " " << get(Recipe, p->running_recipe).name << '\n' << end(); -168 ¦ break; -169 } -170 trace("run") << "creating continuation " << Next_delimited_continuation_id << end(); -171 Delimited_continuation[Next_delimited_continuation_id] = call_stack(Current_routine->calls.begin(), base); -172 while (Current_routine->calls.begin() != base) { -173 ¦ if (Trace_stream) { -174 ¦ ¦ --Trace_stream->callstack_depth; -175 ¦ ¦ assert(Trace_stream->callstack_depth >= 0); -176 ¦ } -177 ¦ Current_routine->calls.pop_front(); -178 } -179 // return it as the result of the marked call -180 products.resize(1); -181 products.at(0).push_back(Next_delimited_continuation_id); -182 // return any other ingredients passed in -183 copy(ingredients.begin(), ingredients.end(), inserter(products, products.end())); -184 ++Next_delimited_continuation_id; -185 break; // continue to process rest of marked call -186 } -187 -188 :(code) -189 call_stack::iterator find_base_of_continuation(call_stack& c) { -190 for (call_stack::iterator p = c.begin(); p != c.end(); ++p) -191 ¦ if (p->is_base_of_continuation) return p; -192 return c.end(); -193 } -194 -195 //: overload 'call' for continuations -196 :(after "Begin Call") -197 if (current_instruction().ingredients.at(0).type->atom -198 ¦ && current_instruction().ingredients.at(0).type->name == "continuation") { -199 // copy multiple calls on to current call stack -200 assert(scalar(ingredients.at(0))); -201 trace("run") << "calling continuation " << ingredients.at(0).at(0) << end(); -202 if (Delimited_continuation.find(ingredients.at(0).at(0)) == Delimited_continuation.end()) -203 ¦ raise << maybe(current_recipe_name()) << "no such delimited continuation " << current_instruction().ingredients.at(0).original_string << '\n' << end(); -204 const call_stack& new_calls = Delimited_continuation[ingredients.at(0).at(0)]; -205 const call& caller = (SIZE(new_calls) > 1) ? *++new_calls.begin() : Current_routine->calls.front(); -206 for (call_stack::const_reverse_iterator p = new_calls.rbegin(); p != new_calls.rend(); ++p) { -207 ¦ Current_routine->calls.push_front(*p); -208 ¦ // ensure that the presence of a continuation keeps its stack frames from being reclaimed -209 ¦ int space_address = Current_routine->calls.front().default_space; -210 ¦ if (space_address != 0) { -211 ¦ ¦ int refcount = get_or_insert(Memory, space_address); -212 ¦ ¦ trace("mem") << "incrementing refcount of " << space_address << ": " << refcount << " -> " << refcount+1 << end(); -213 ¦ ¦ put(Memory, space_address, refcount+1); -214 ¦ } -215 } -216 if (Trace_stream) { -217 ¦ Trace_stream->callstack_depth += SIZE(new_calls); -218 ¦ trace("trace") << "calling delimited continuation; growing callstack depth to " << Trace_stream->callstack_depth << end(); -219 ¦ assert(Trace_stream->callstack_depth < 9000); // 9998-101 plus cushion -220 } -221 ++current_step_index(); // skip past the return-continuation-until-mark -222 ingredients.erase(ingredients.begin()); // drop the callee -223 finish_call_housekeeping(to_instruction(caller), ingredients); -224 continue; -225 } -226 -227 //: Ensure that the presence of a continuation keeps its stack frames from being reclaimed. -228 -229 :(scenario continuations_preserve_local_scopes) -230 def main [ -231 local-scope -232 k:continuation <- call-with-continuation-mark f -233 call k -234 return 34 -235 ] -236 def f [ -237 local-scope -238 g -239 ] -240 def g [ -241 local-scope -242 return-continuation-until-mark -243 add 1, 1 -244 ] -245 # entering main -246 +mem: new alloc: 1000 -247 +run: {k: "continuation"} <- call-with-continuation-mark {f: "recipe-literal"} -248 # entering f -249 +mem: new alloc: 1004 -250 # entering g -251 +mem: new alloc: 1007 -252 # return control to main -253 +run: return-continuation-until-mark -254 # no allocs abandoned yet -255 # finish running main -256 +run: call {k: "continuation"} -257 +run: add {1: "literal"}, {1: "literal"} -258 +run: return {34: "literal"} -259 # now k is reclaimed -260 +mem: trying to reclaim local k:continuation -261 # at this point all allocs in the continuation are abandoned -262 +mem: automatically abandoning 1007 -263 +mem: automatically abandoning 1004 -264 # finally the alloc for main is abandoned -265 +mem: automatically abandoning 1000 -266 -267 :(after "Begin Decrement Refcounts(canonized_x)") -268 if (is_mu_continuation(canonized_x)) { -269 int continuation_id = get_or_insert(Memory, canonized_x.value); -270 trace("run") << "reclaiming continuation " << continuation_id << end(); -271 if (continuation_id == 0) return; -272 const call_stack& continuation_calls = get(Delimited_continuation, continuation_id); -273 // temporarily push the stack frames for the continuation one last time on to the call stack -274 for (call_stack::const_reverse_iterator p = continuation_calls.rbegin(); p != continuation_calls.rend(); ++p) -275 ¦ Current_routine->calls.push_front(*p); -276 // reclaim their spaces while popping them -277 // (because reclaim_default_space() relies on the default-space being reclaimed being at the top of the stack) -278 for (call_stack::const_iterator p = continuation_calls.begin(); p != continuation_calls.end(); ++p) { -279 ¦ reclaim_default_space(); -280 ¦ Current_routine->calls.pop_front(); -281 } -282 return; -283 } -284 -285 :(code) -286 bool is_mu_continuation(reagent/*copy*/ x) { -287 canonize_type(x); -288 return x.type && x.type->atom && x.type->value == get(Type_ordinal, "continuation"); -289 } -290 -291 :(scenario continuations_can_return_values) -292 def main [ -293 local-scope -294 k:continuation, 1:num/raw <- call-with-continuation-mark f -295 ] -296 def f [ -297 local-scope -298 g -299 ] -300 def g [ -301 local-scope -302 return-continuation-until-mark 34 -303 stash [continuation called] -304 ] -305 # entering main -306 +mem: new alloc: 1000 -307 +run: {k: "continuation"}, {1: "number", "raw": ()} <- call-with-continuation-mark {f: "recipe-literal"} -308 # entering f -309 +mem: new alloc: 1004 -310 # entering g -311 +mem: new alloc: 1007 -312 # return control to main -313 +run: return-continuation-until-mark {34: "literal"} -314 # no allocs abandoned yet -315 +mem: storing 34 in location 1 -316 # end of main -317 # make sure no memory leaks.. -318 +mem: trying to reclaim local k:continuation -319 +mem: automatically abandoning 1007 -320 +mem: automatically abandoning 1004 -321 +mem: automatically abandoning 1000 -322 # ..even though we never called the continuation -323 -app: continuation called + 95 +mem: storing 6 in location 22 + 96 +mem: storing 7 in location 2 + 97 +run: {2: "number"} <- call {1: "continuation"}, {2: "number"} + 98 +mem: storing 7 in location 22 + 99 +mem: storing 8 in location 2 +100 # first call of 'g' does not execute the part after return-continuation-until-mark +101 -mem: storing 77 in location 22 +102 # calls of the continuation don't execute the part before return-continuation-until-mark +103 -mem: storing 5 in location 21 +104 -mem: storing 6 in location 21 +105 -mem: storing 7 in location 21 +106 # termination +107 -mem: storing 9 in location 2 +108 +109 :(before "End call Fields") +110 bool is_base_of_continuation; +111 :(before "End call Constructor") +112 is_base_of_continuation = false; +113 +114 :(before "End Primitive Recipe Declarations") +115 CALL_WITH_CONTINUATION_MARK, +116 :(before "End Primitive Recipe Numbers") +117 Recipe_ordinal["call-with-continuation-mark"] = CALL_WITH_CONTINUATION_MARK; +118 :(before "End Primitive Recipe Checks") +119 case CALL_WITH_CONTINUATION_MARK: { +120 break; +121 } +122 :(before "End Primitive Recipe Implementations") +123 case CALL_WITH_CONTINUATION_MARK: { +124 // like call, but mark the current call as a 'base of continuation' call +125 // before pushing the next one on it +126 if (Trace_stream) { +127 ¦ ++Trace_stream->callstack_depth; +128 ¦ trace("trace") << "delimited continuation; incrementing callstack depth to " << Trace_stream->callstack_depth << end(); +129 ¦ assert(Trace_stream->callstack_depth < 9000); // 9998-101 plus cushion +130 } +131 const instruction& caller_instruction = current_instruction(); +132 Current_routine->calls.front().is_base_of_continuation = true; +133 Current_routine->calls.push_front(call(Recipe_ordinal[current_instruction().ingredients.at(0).name])); +134 ingredients.erase(ingredients.begin()); // drop the callee +135 finish_call_housekeeping(caller_instruction, ingredients); +136 continue; +137 } +138 +139 //: save the slice of current call stack until the 'call-with-continuation-mark' +140 //: call, and return it as the result. +141 //: todo: implement delimited continuations in Mu's memory +142 :(before "End Globals") +143 map<long long int, call_stack> Delimited_continuation; +144 long long int Next_delimited_continuation_id = 1; // 0 is null just like an address +145 :(before "End Reset") +146 Delimited_continuation.clear(); +147 Next_delimited_continuation_id = 1; +148 +149 :(before "End Primitive Recipe Declarations") +150 RETURN_CONTINUATION_UNTIL_MARK, +151 :(before "End Primitive Recipe Numbers") +152 Recipe_ordinal["return-continuation-until-mark"] = RETURN_CONTINUATION_UNTIL_MARK; +153 :(before "End Primitive Recipe Checks") +154 case RETURN_CONTINUATION_UNTIL_MARK: { +155 break; +156 } +157 :(before "End Primitive Recipe Implementations") +158 case RETURN_CONTINUATION_UNTIL_MARK: { +159 // first clear any existing ingredients, to isolate the creation of the +160 // continuation from its calls +161 Current_routine->calls.front().ingredient_atoms.clear(); +162 Current_routine->calls.front().next_ingredient_to_process = 0; +163 // copy the current call stack until the most recent marked call +164 call_stack::iterator find_base_of_continuation(call_stack& c); // manual prototype containing '::' +165 call_stack::iterator base = find_base_of_continuation(Current_routine->calls); +166 if (base == Current_routine->calls.end()) { +167 ¦ raise << maybe(current_recipe_name()) << "couldn't find a 'call-with-continuation-mark' to return to\n" << end(); +168 ¦ raise << maybe(current_recipe_name()) << "call stack:\n" << end(); +169 ¦ for (call_stack::iterator p = Current_routine->calls.begin(); p != Current_routine->calls.end(); ++p) +170 ¦ ¦ raise << maybe(current_recipe_name()) << " " << get(Recipe, p->running_recipe).name << '\n' << end(); +171 ¦ break; +172 } +173 trace("run") << "creating continuation " << Next_delimited_continuation_id << end(); +174 Delimited_continuation[Next_delimited_continuation_id] = call_stack(Current_routine->calls.begin(), base); +175 while (Current_routine->calls.begin() != base) { +176 ¦ if (Trace_stream) { +177 ¦ ¦ --Trace_stream->callstack_depth; +178 ¦ ¦ assert(Trace_stream->callstack_depth >= 0); +179 ¦ } +180 ¦ Current_routine->calls.pop_front(); +181 } +182 // return it as the result of the marked call +183 products.resize(1); +184 products.at(0).push_back(Next_delimited_continuation_id); +185 // return any other ingredients passed in +186 copy(ingredients.begin(), ingredients.end(), inserter(products, products.end())); +187 ++Next_delimited_continuation_id; +188 break; // continue to process rest of marked call +189 } +190 +191 :(code) +192 call_stack::iterator find_base_of_continuation(call_stack& c) { +193 for (call_stack::iterator p = c.begin(); p != c.end(); ++p) +194 ¦ if (p->is_base_of_continuation) return p; +195 return c.end(); +196 } +197 +198 //: overload 'call' for continuations +199 :(after "Begin Call") +200 if (current_instruction().ingredients.at(0).type->atom +201 ¦ && current_instruction().ingredients.at(0).type->name == "continuation") { +202 // copy multiple calls on to current call stack +203 assert(scalar(ingredients.at(0))); +204 trace("run") << "calling continuation " << ingredients.at(0).at(0) << end(); +205 if (Delimited_continuation.find(ingredients.at(0).at(0)) == Delimited_continuation.end()) +206 ¦ raise << maybe(current_recipe_name()) << "no such delimited continuation " << current_instruction().ingredients.at(0).original_string << '\n' << end(); +207 const call_stack& new_calls = Delimited_continuation[ingredients.at(0).at(0)]; +208 const call& caller = (SIZE(new_calls) > 1) ? *++new_calls.begin() : Current_routine->calls.front(); +209 for (call_stack::const_reverse_iterator p = new_calls.rbegin(); p != new_calls.rend(); ++p) { +210 ¦ Current_routine->calls.push_front(*p); +211 ¦ // ensure that the presence of a continuation keeps its stack frames from being reclaimed +212 ¦ int space_address = Current_routine->calls.front().default_space; +213 ¦ if (space_address != 0) { +214 ¦ ¦ int refcount = get_or_insert(Memory, space_address); +215 ¦ ¦ trace("mem") << "incrementing refcount of " << space_address << ": " << refcount << " -> " << refcount+1 << end(); +216 ¦ ¦ put(Memory, space_address, refcount+1); +217 ¦ } +218 } +219 if (Trace_stream) { +220 ¦ Trace_stream->callstack_depth += SIZE(new_calls); +221 ¦ trace("trace") << "calling delimited continuation; growing callstack depth to " << Trace_stream->callstack_depth << end(); +222 ¦ assert(Trace_stream->callstack_depth < 9000); // 9998-101 plus cushion +223 } +224 ingredients.erase(ingredients.begin()); // drop the callee +225 finish_call_housekeeping(to_instruction(caller), ingredients); +226 copy(ingredients.begin(), ingredients.end(), inserter(products, products.begin())); +227 break; // record results of resuming 'return-continuation-until-mark' instruction +228 } +229 +230 //: Ensure that the presence of a continuation keeps its stack frames from being reclaimed. +231 +232 :(scenario continuations_preserve_local_scopes) +233 def main [ +234 local-scope +235 k:continuation <- call-with-continuation-mark f +236 call k +237 return 34 +238 ] +239 def f [ +240 local-scope +241 g +242 ] +243 def g [ +244 local-scope +245 return-continuation-until-mark +246 add 1, 1 +247 ] +248 # entering main +249 +mem: new alloc: 1000 +250 +run: {k: "continuation"} <- call-with-continuation-mark {f: "recipe-literal"} +251 # entering f +252 +mem: new alloc: 1004 +253 # entering g +254 +mem: new alloc: 1007 +255 # return control to main +256 +run: return-continuation-until-mark +257 # no allocs abandoned yet +258 # finish running main +259 +run: call {k: "continuation"} +260 +run: add {1: "literal"}, {1: "literal"} +261 +run: return {34: "literal"} +262 # now k is reclaimed +263 +mem: trying to reclaim local k:continuation +264 # at this point all allocs in the continuation are abandoned +265 +mem: automatically abandoning 1007 +266 +mem: automatically abandoning 1004 +267 # finally the alloc for main is abandoned +268 +mem: automatically abandoning 1000 +269 +270 :(after "Begin Decrement Refcounts(canonized_x)") +271 if (is_mu_continuation(canonized_x)) { +272 int continuation_id = get_or_insert(Memory, canonized_x.value); +273 trace("run") << "reclaiming continuation " << continuation_id << end(); +274 if (continuation_id == 0) return; +275 const call_stack& continuation_calls = get(Delimited_continuation, continuation_id); +276 // temporarily push the stack frames for the continuation one last time on to the call stack +277 for (call_stack::const_reverse_iterator p = continuation_calls.rbegin(); p != continuation_calls.rend(); ++p) +278 ¦ Current_routine->calls.push_front(*p); +279 // reclaim their spaces while popping them +280 // (because reclaim_default_space() relies on the default-space being reclaimed being at the top of the stack) +281 for (call_stack::const_iterator p = continuation_calls.begin(); p != continuation_calls.end(); ++p) { +282 ¦ reclaim_default_space(); +283 ¦ Current_routine->calls.pop_front(); +284 } +285 return; +286 } +287 +288 :(code) +289 bool is_mu_continuation(reagent/*copy*/ x) { +290 canonize_type(x); +291 return x.type && x.type->atom && x.type->value == get(Type_ordinal, "continuation"); +292 } +293 +294 :(scenario continuations_can_return_values) +295 def main [ +296 local-scope +297 k:continuation, 1:num/raw <- call-with-continuation-mark f +298 ] +299 def f [ +300 local-scope +301 g +302 ] +303 def g [ +304 local-scope +305 return-continuation-until-mark 34 +306 stash [continuation called] +307 ] +308 # entering main +309 +mem: new alloc: 1000 +310 +run: {k: "continuation"}, {1: "number", "raw": ()} <- call-with-continuation-mark {f: "recipe-literal"} +311 # entering f +312 +mem: new alloc: 1004 +313 # entering g +314 +mem: new alloc: 1007 +315 # return control to main +316 +run: return-continuation-until-mark {34: "literal"} +317 # no allocs abandoned yet +318 +mem: storing 34 in location 1 +319 # end of main +320 # make sure no memory leaks.. +321 +mem: trying to reclaim local k:continuation +322 +mem: automatically abandoning 1007 +323 +mem: automatically abandoning 1004 +324 +mem: automatically abandoning 1000 +325 # ..even though we never called the continuation +326 -app: continuation called -- cgit 1.4.1-2-gfad0