Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use Source | |
| 3 | use Trees | |
| 4 | use Logging | |
| 5 | ||
| 6 | // SPILL_AWAITS — wraps operand-position subexpressions evaluated | |
| 7 | // to the left of an `await` in `Expressions.SPILL` so their | |
| 8 | // values land in a frame field rather than the CLR stack. AWAIT | |
| 9 | // suspends with an internal `leave`, and the CLR requires the | |
| 10 | // evaluation stack to be empty at the suspend point — anything | |
| 11 | // not yet consumed by an enclosing operation would be lost on | |
| 12 | // re-entry (CLR locals reset; only frame fields persist). | |
| 13 | // | |
| 14 | // A protected region has the same requirement: a `try`, or the | |
| 15 | // region a disposing `let` or a `use` opens, can only be entered | |
| 16 | // with an empty stack. Outside an async body there is no suspend | |
| 17 | // to survive, so a spilled value goes to a local instead. | |
| 18 | // | |
| 19 | // Rule (applied bottom-up at every composite node): if any | |
| 20 | // later-evaluated child contains an `await` or a protected | |
| 21 | // region, wrap every prior child in `SPILL`. The operand-position contract (one stack | |
| 22 | // value per operand) makes the rule uniform — no purity | |
| 23 | // analysis needed. | |
| 24 | class SPILL_AWAITS: Visitor is | |
| 25 | init() is | |
| 26 | super.init() | |
| 27 | si | |
| 28 | ||
| 29 | apply(node: Node) is | |
| 30 | node.walk(self) | |
| 31 | si | |
| 32 | ||
| 33 | visit(b: Expressions.BINARY) is | |
| 34 | if _needs_empty_stack(b.right) then | |
| 35 | _wrap_unless_await(b, b.left) | |
| 36 | ||
| 37 | if _contains_protected_region(b.right) then | |
| 38 | _wrap_unless_await(b, b.right) | |
| 39 | fi | |
| 40 | fi | |
| 41 | si | |
| 42 | ||
| 43 | // CALL: receiver first, then args in source order. An await | |
| 44 | // in any arg means wrap every prior arg, plus the | |
| 45 | // MEMBER receiver if this is an instance call. | |
| 46 | visit(call: Expressions.CALL) is | |
| 47 | if call.arguments.count == 0 then | |
| 48 | return | |
| 49 | fi | |
| 50 | ||
| 51 | let latest = _spill_elements( | |
| 52 | call.arguments.expressions, | |
| 53 | (i: int, e: Expressions.Expression) is call.arguments.replace_element(i, e) si) | |
| 54 | ||
| 55 | if latest < 0 then | |
| 56 | return | |
| 57 | fi | |
| 58 | ||
| 59 | if isa Expressions.MEMBER(call.function) then | |
| 60 | let mem = cast Expressions.MEMBER(call.function) | |
| 61 | if !_is_await_node(mem.left) /\ !_is_spill_node(mem.left) then | |
| 62 | mem.replace_child( | |
| 63 | mem.left, | |
| 64 | Expressions.SPILL(mem.left.location, mem.left) | |
| 65 | ) | |
| 66 | fi | |
| 67 | fi | |
| 68 | si | |
| 69 | ||
| 70 | // A store pushes its target's receiver, and an index its index, | |
| 71 | // before the value it stores, so all of them are evaluated ahead | |
| 72 | // of a value holding a protected region. | |
| 73 | visit(assign: Statements.ASSIGNMENT) is | |
| 74 | if !_contains_protected_region(assign.right) then | |
| 75 | return | |
| 76 | fi | |
| 77 | ||
| 78 | if let member: Expressions.MEMBER = assign.left then | |
| 79 | _wrap_unless_await(member, member.left) | |
| 80 | elif let idx: Expressions.INDEX = assign.left then | |
| 81 | _wrap_unless_await(idx, idx.left) | |
| 82 | _wrap_unless_await(idx, idx.index) | |
| 83 | fi | |
| 84 | ||
| 85 | if !_is_spill_node(assign.right) then | |
| 86 | assign.replace_right(Expressions.SPILL(assign.right.location, assign.right)) | |
| 87 | fi | |
| 88 | si | |
| 89 | ||
| 90 | visit(idx: Expressions.INDEX) is | |
| 91 | if _needs_empty_stack(idx.index) then | |
| 92 | if !_is_await_node(idx.left) /\ !_is_spill_node(idx.left) then | |
| 93 | idx.replace_child( | |
| 94 | idx.left, | |
| 95 | Expressions.SPILL(idx.left.location, idx.left) | |
| 96 | ) | |
| 97 | fi | |
| 98 | ||
| 99 | if _contains_protected_region(idx.index) then | |
| 100 | _wrap_unless_await(idx, idx.index) | |
| 101 | fi | |
| 102 | fi | |
| 103 | si | |
| 104 | ||
| 105 | visit(construct: Expressions.CONSTRUCT) is | |
| 106 | if construct.arguments.count == 0 then | |
| 107 | return | |
| 108 | fi | |
| 109 | ||
| 110 | _spill_elements( | |
| 111 | construct.arguments.expressions, | |
| 112 | (i: int, e: Expressions.Expression) is construct.arguments.replace_element(i, e) si) | |
| 113 | si | |
| 114 | ||
| 115 | visit(t: Expressions.TUPLE) is | |
| 116 | if t.elements.count == 0 then | |
| 117 | return | |
| 118 | fi | |
| 119 | ||
| 120 | _spill_elements( | |
| 121 | t.elements.expressions, | |
| 122 | (i: int, e: Expressions.Expression) is t.elements.replace_element(i, e) si) | |
| 123 | si | |
| 124 | ||
| 125 | // An interpolated string keeps the text it is building on the | |
| 126 | // stack while each hole is evaluated. | |
| 127 | visit(s: Expressions.STRING_INTERPOLATION) is | |
| 128 | let holes = Collections.LIST[Expressions.Expression]() | |
| 129 | let positions = Collections.LIST[int]() | |
| 130 | ||
| 131 | for index in 0..s.values.count do | |
| 132 | let fragment = s.values[index] | |
| 133 | ||
| 134 | if fragment.is_expression then | |
| 135 | holes.add(fragment.expression) | |
| 136 | positions.add(index) | |
| 137 | fi | |
| 138 | od | |
| 139 | ||
| 140 | _spill_elements(holes, (i: int, e: Expressions.Expression) is s.replace_expression(positions[i], e) si) | |
| 141 | si | |
| 142 | ||
| 143 | // Wraps every element evaluated before the last one that needs an | |
| 144 | // empty stack. An await leaves that stack empty itself when it is | |
| 145 | // emitted, but a protected region is emitted where its value is | |
| 146 | // used, so an element holding one is spilled too: it is then | |
| 147 | // evaluated ahead of the operation, in source order, with nothing | |
| 148 | // beneath it. Answers the index of that last element, or -1. | |
| 149 | _spill_elements( | |
| 150 | elements: Collections.List[Expressions.Expression], | |
| 151 | replace: (int, Expressions.Expression) -> void | |
| 152 | ) -> int is | |
| 153 | let latest mut = -1 | |
| 154 | ||
| 155 | for i in 0..elements.count do | |
| 156 | if _needs_empty_stack(elements[i]) then | |
| 157 | latest = i | |
| 158 | fi | |
| 159 | od | |
| 160 | ||
| 161 | if latest < 0 then | |
| 162 | return latest | |
| 163 | fi | |
| 164 | ||
| 165 | let last = if _contains_protected_region(elements[latest]) then latest + 1 else latest fi | |
| 166 | ||
| 167 | for i in 0..last do | |
| 168 | let e = elements[i] | |
| 169 | ||
| 170 | if !_is_await_node(e) /\ !_is_spill_node(e) then | |
| 171 | replace(i, Expressions.SPILL(e.location, e)) | |
| 172 | fi | |
| 173 | od | |
| 174 | ||
| 175 | return latest | |
| 176 | si | |
| 177 | ||
| 178 | _wrap_unless_await(parent: Expressions.Expression, child: Expressions.Expression) is | |
| 179 | if _is_await_node(child) then | |
| 180 | return | |
| 181 | fi | |
| 182 | parent.replace_child(child, Expressions.SPILL(child.location, child)) | |
| 183 | si | |
| 184 | ||
| 185 | _is_await_node(e: Expressions.Expression) -> bool => | |
| 186 | isa Expressions.AWAIT(e) | |
| 187 | ||
| 188 | _is_spill_node(e: Expressions.Expression) -> bool => | |
| 189 | isa Expressions.SPILL(e) | |
| 190 | ||
| 191 | _needs_empty_stack(e: Expressions.Expression) -> bool is | |
| 192 | let scanner = CONTAINS_AWAIT_SCANNER() | |
| 193 | e.walk(scanner) | |
| 194 | ||
| 195 | return scanner.found \/ _contains_protected_region(e) | |
| 196 | si | |
| 197 | ||
| 198 | _contains_protected_region(e: Expressions.Expression) -> bool is | |
| 199 | let scanner = CONTAINS_PROTECTED_REGION_SCANNER() | |
| 200 | e.walk(scanner) | |
| 201 | ||
| 202 | return scanner.found | |
| 203 | si | |
| 204 | si | |
| 205 | ||
| 206 | // Scans for any `Expressions.AWAIT`. Stops at function-literal | |
| 207 | // boundaries — a nested lambda's awaits belong to its own | |
| 208 | // state machine. | |
| 209 | class CONTAINS_AWAIT_SCANNER: Visitor is | |
| 210 | found: bool public | |
| 211 | ||
| 212 | init() is | |
| 213 | super.init() | |
| 214 | si | |
| 215 | ||
| 216 | pre(a: Expressions.AWAIT) -> bool is | |
| 217 | found = true | |
| 218 | return true | |
| 219 | si | |
| 220 | ||
| 221 | pre(f: Expressions.FUNCTION) -> bool => true | |
| 222 | si | |
| 223 | ||
| 224 | // Scans for anything that opens a protected region: a `try`, a | |
| 225 | // `let use` or `let use ... in`, or a `use`. Stops at function-literal | |
| 226 | // boundaries, since a nested lambda's regions are in its own body. | |
| 227 | class CONTAINS_PROTECTED_REGION_SCANNER: Visitor is | |
| 228 | found: bool public | |
| 229 | ||
| 230 | init() is | |
| 231 | super.init() | |
| 232 | si | |
| 233 | ||
| 234 | pre(t: Statements.TRY) -> bool is | |
| 235 | found = true | |
| 236 | return true | |
| 237 | si | |
| 238 | ||
| 239 | pre(l: Statements.LET) -> bool is | |
| 240 | if l.want_dispose then | |
| 241 | found = true | |
| 242 | fi | |
| 243 | ||
| 244 | return found | |
| 245 | si | |
| 246 | ||
| 247 | pre(l: Expressions.LET_IN) -> bool is | |
| 248 | if l.want_dispose then | |
| 249 | found = true | |
| 250 | fi | |
| 251 | ||
| 252 | return found | |
| 253 | si | |
| 254 | ||
| 255 | pre(u: Expressions.USE) -> bool is | |
| 256 | found = true | |
| 257 | return true | |
| 258 | si | |
| 259 | ||
| 260 | pre(f: Expressions.FUNCTION) -> bool => true | |
| 261 | si | |
| 262 | ||
| 263 | // Scans for any state-machine suspend site (`Expressions.AWAIT` | |
| 264 | // or `Statements.YIELD`) without descending into a nested | |
| 265 | // function literal. Used by generate-il to decide whether a | |
| 266 | // composite value-BLOCK (LIST in expression position, `if`/ | |
| 267 | // `case` in expression position) needs to be spilled to a | |
| 268 | // frame field — captured-suspend IL inside the BLOCK has to | |
| 269 | // live in the outer MoveNext stream, not get replayed inside | |
| 270 | // a consumer's stack setup. | |
| 271 | class CONTAINS_SUSPEND_SCANNER: Visitor is | |
| 272 | found: bool public | |
| 273 | ||
| 274 | init() is | |
| 275 | super.init() | |
| 276 | si | |
| 277 | ||
| 278 | pre(a: Expressions.AWAIT) -> bool is | |
| 279 | found = true | |
| 280 | return true | |
| 281 | si | |
| 282 | ||
| 283 | pre(y: Statements.YIELD) -> bool is | |
| 284 | found = true | |
| 285 | return true | |
| 286 | si | |
| 287 | ||
| 288 | pre(y: Statements.YIELD_ALL) -> bool is | |
| 289 | found = true | |
| 290 | return true | |
| 291 | si | |
| 292 | ||
| 293 | pre(f: Expressions.FUNCTION) -> bool => true | |
| 294 | si | |
| 295 | ||
| 296 | si |