Appearance
| 1 | namespace IR.Values is | |
| 2 | use Ghul.Pipes | |
| 3 | ||
| 4 | use Semantic.Types.Type | |
| 5 | ||
| 6 | // Structured control flow that a BREAK can leave: a body of values | |
| 7 | // followed by an end. | |
| 8 | // | |
| 9 | // The labels belong to the CIL lowering. `position` is the source | |
| 10 | // position current where the node begins: the lowering marks it | |
| 11 | // against the labels and branches it adds. | |
| 12 | class Breakable: Value abstract is | |
| 13 | end: IR.LABEL | |
| 14 | body: BLOCK | |
| 15 | ||
| 16 | position: Source.LOCATION? | |
| 17 | ||
| 18 | is_closed: bool | |
| 19 | ||
| 20 | // Whether any exit leaves this node. | |
| 21 | is_targeted: bool | |
| 22 | ||
| 23 | // A position is marked by the lowering, against the values it | |
| 24 | // adds, so the node itself has none to mark. | |
| 25 | has_location: bool => false | |
| 26 | ||
| 27 | init(end: IR.LABEL, void_type: Type) is | |
| 28 | super.init() | |
| 29 | ||
| 30 | self.end = end | |
| 31 | ||
| 32 | position = IoC.CONTAINER.instance.location_stack.current | |
| 33 | ||
| 34 | body = BLOCK(void_type) | |
| 35 | si | |
| 36 | ||
| 37 | mark_targeted() is | |
| 38 | is_targeted = true | |
| 39 | si | |
| 40 | ||
| 41 | // Makes the lowering mark no source position for this node, for | |
| 42 | // control flow that no statement in the source wrote. | |
| 43 | clear_position() is | |
| 44 | position = null | |
| 45 | si | |
| 46 | ||
| 47 | // Records that the body is complete. | |
| 48 | close() is | |
| 49 | assert !is_closed else "structured node is already closed" | |
| 50 | ||
| 51 | is_closed = true | |
| 52 | si | |
| 53 | ||
| 54 | accept(visitor: Visitor) is | |
| 55 | visitor.visit(self) | |
| 56 | si | |
| 57 | si | |
| 58 | ||
| 59 | // A sequence of values that a BREAK can leave early, continuing | |
| 60 | // after its end. | |
| 61 | class REGION: Breakable is | |
| 62 | init(end: IR.LABEL, void_type: Type) is | |
| 63 | super.init(end, void_type) | |
| 64 | si | |
| 65 | ||
| 66 | to_string() -> string => "region {end}:[{body}]" | |
| 67 | si | |
| 68 | ||
| 69 | // A loop: the body repeats until a BREAK leaves it. A CONTINUE, | |
| 70 | // and reaching the end of the body, both go round again. The CIL | |
| 71 | // lowering places `start` before the body and `end` after the | |
| 72 | // branch back to `start`. | |
| 73 | class LOOP: Breakable is | |
| 74 | start: IR.LABEL | |
| 75 | ||
| 76 | init(start: IR.LABEL, end: IR.LABEL, void_type: Type) is | |
| 77 | super.init(end, void_type) | |
| 78 | ||
| 79 | self.start = start | |
| 80 | si | |
| 81 | ||
| 82 | to_string() -> string => "loop {start}:[{body}]" | |
| 83 | si | |
| 84 | ||
| 85 | // The body of a state machine's resumable method. A suspend made | |
| 86 | // inside a protected region cannot return directly, so it leaves | |
| 87 | // this node instead, continuing after the body where the method | |
| 88 | // returns. An end that no exit targets is not marked. | |
| 89 | class RESUMABLE: Breakable is | |
| 90 | init(end: IR.LABEL, void_type: Type) is | |
| 91 | super.init(end, void_type) | |
| 92 | si | |
| 93 | ||
| 94 | to_string() -> string => "resumable {end}:[{body}]" | |
| 95 | si | |
| 96 | ||
| 97 | // A point a state machine resumes at, which a DISPATCH reaches by | |
| 98 | // its label. | |
| 99 | class RESUME_POINT: Value is | |
| 100 | label: IR.LABEL | |
| 101 | position: Source.LOCATION? | |
| 102 | ||
| 103 | has_location: bool => false | |
| 104 | ||
| 105 | init(label: IR.LABEL) is | |
| 106 | super.init() | |
| 107 | ||
| 108 | self.label = label | |
| 109 | position = IoC.CONTAINER.instance.location_stack.current | |
| 110 | si | |
| 111 | ||
| 112 | // Makes the lowering mark no source position for this point. | |
| 113 | clear_position() is | |
| 114 | position = null | |
| 115 | si | |
| 116 | ||
| 117 | accept(visitor: Visitor) is | |
| 118 | visitor.visit(self) | |
| 119 | si | |
| 120 | ||
| 121 | to_string() -> string => "resume {label}" | |
| 122 | si | |
| 123 | ||
| 124 | // Where a resumed state machine finds its place: compares the state | |
| 125 | // held in the local `state_local` with each entry's state, and | |
| 126 | // jumps to the matching entry's resume point. A dispatch opens a | |
| 127 | // resumable body and each protected region inside one, so a jump | |
| 128 | // never enters a region from outside it: a dispatch reaches a | |
| 129 | // resume point in an inner region by jumping to that region's own | |
| 130 | // dispatch, whose `top` is a resume point too. | |
| 131 | class DISPATCH: Value is | |
| 132 | state_local: IR.LOCAL | |
| 133 | state_type: Type => state_local.type | |
| 134 | top: IR.LABEL | |
| 135 | entries: Collections.LIST[(state: int, target: IR.LABEL)] | |
| 136 | ||
| 137 | position: Source.LOCATION? | |
| 138 | ||
| 139 | has_location: bool => false | |
| 140 | ||
| 141 | init(state_local: IR.LOCAL) is | |
| 142 | super.init() | |
| 143 | ||
| 144 | self.state_local = state_local | |
| 145 | ||
| 146 | top = IR.LABEL() | |
| 147 | entries = Collections.LIST[(state: int, target: IR.LABEL)]() | |
| 148 | ||
| 149 | position = IoC.CONTAINER.instance.location_stack.current | |
| 150 | si | |
| 151 | ||
| 152 | add_entry(state: int, target: IR.LABEL) is | |
| 153 | entries.add((state, target)) | |
| 154 | si | |
| 155 | ||
| 156 | accept(visitor: Visitor) is | |
| 157 | visitor.visit(self) | |
| 158 | si | |
| 159 | ||
| 160 | to_string() -> string => "dispatch {top} {state_local}:[{entries |> map(e => "{e.state} -> {e.target}") |> join(", ")}]" | |
| 161 | si | |
| 162 | ||
| 163 | // A protected body with catch handlers or a finally, never both: a | |
| 164 | // try with both is a TRY with a finally whose body is a TRY with | |
| 165 | // the catches. Leaving the TRY continues after the whole construct. | |
| 166 | // An end that no exit targets is not marked. | |
| 167 | class TRY: Breakable is | |
| 168 | catches: Collections.LIST[CATCH] | |
| 169 | finally_region: REGION? public | |
| 170 | ||
| 171 | init(end: IR.LABEL, void_type: Type) is | |
| 172 | super.init(end, void_type) | |
| 173 | ||
| 174 | catches = Collections.LIST[CATCH]() | |
| 175 | si | |
| 176 | ||
| 177 | to_string() -> string => | |
| 178 | "try {end}:[{body}] {catches |> join(" ")} finally {finally_region}" | |
| 179 | si | |
| 180 | ||
| 181 | // A catch handler of a TRY. It handles an exception of `type`. The | |
| 182 | // exception is on the CIL evaluation stack where `body` starts. | |
| 183 | class CATCH is | |
| 184 | type: Type | |
| 185 | body: BLOCK | |
| 186 | ||
| 187 | position: Source.LOCATION? | |
| 188 | ||
| 189 | init(type: Type, void_type: Type) is | |
| 190 | self.type = type | |
| 191 | ||
| 192 | position = IoC.CONTAINER.instance.location_stack.current | |
| 193 | body = BLOCK(void_type) | |
| 194 | si | |
| 195 | ||
| 196 | // Makes the lowering mark no source position for this handler. | |
| 197 | clear_position() is | |
| 198 | position = null | |
| 199 | si | |
| 200 | ||
| 201 | to_string() -> string => "catch:[{body}]" | |
| 202 | si | |
| 203 | ||
| 204 | // An exit from an enclosing structured node, taken when | |
| 205 | // `condition` holds of `operands`, or always where the condition | |
| 206 | // is ALWAYS. | |
| 207 | class Exit: Value abstract is | |
| 208 | target: Breakable | |
| 209 | condition: IR.BRANCH | |
| 210 | operands: Collections.List[Value] | |
| 211 | ||
| 212 | position: Source.LOCATION? | |
| 213 | ||
| 214 | has_location: bool => false | |
| 215 | ||
| 216 | init(target: Breakable, condition: IR.BRANCH, operands: Collections.List[Value]) is | |
| 217 | super.init() | |
| 218 | ||
| 219 | assert (condition == IR.BRANCH.ALWAYS) == (operands.count == 0) else "an exit takes operands exactly when it is conditional" | |
| 220 | assert operands.count <= 2 else "an exit compares at most two operands" | |
| 221 | ||
| 222 | self.target = target | |
| 223 | self.condition = condition | |
| 224 | self.operands = operands | |
| 225 | ||
| 226 | target.mark_targeted() | |
| 227 | ||
| 228 | position = IoC.CONTAINER.instance.location_stack.current | |
| 229 | si | |
| 230 | ||
| 231 | // Makes the lowering mark no source position for this exit. | |
| 232 | clear_position() is | |
| 233 | position = null | |
| 234 | si | |
| 235 | ||
| 236 | accept(visitor: Visitor) is | |
| 237 | visitor.visit(self) | |
| 238 | si | |
| 239 | ||
| 240 | operands_of(operand: Value) -> Collections.List[Value] static is | |
| 241 | let result = Collections.LIST[Value]() | |
| 242 | ||
| 243 | result.add(operand) | |
| 244 | ||
| 245 | return result | |
| 246 | si | |
| 247 | ||
| 248 | operands_of(left: Value, right: Value) -> Collections.List[Value] static is | |
| 249 | let result = Collections.LIST[Value]() | |
| 250 | ||
| 251 | result.add(left) | |
| 252 | result.add(right) | |
| 253 | ||
| 254 | return result | |
| 255 | si | |
| 256 | si | |
| 257 | ||
| 258 | // Leaves `target`, continuing after its end. | |
| 259 | class BREAK: Exit is | |
| 260 | init(target: Breakable) is | |
| 261 | super.init(target, IR.BRANCH.ALWAYS, Collections.LIST[Value]()) | |
| 262 | si | |
| 263 | ||
| 264 | init(target: Breakable, condition: IR.BRANCH, operands: Collections.List[Value]) is | |
| 265 | super.init(target, condition, operands) | |
| 266 | si | |
| 267 | ||
| 268 | init(target: Breakable, condition: IR.BRANCH, operand: Value) is | |
| 269 | super.init(target, condition, operands_of(operand)) | |
| 270 | si | |
| 271 | ||
| 272 | init(target: Breakable, condition: IR.BRANCH, left: Value, right: Value) is | |
| 273 | super.init(target, condition, operands_of(left, right)) | |
| 274 | si | |
| 275 | ||
| 276 | to_string() -> string => "break {target.end} {condition}({operands |> join(", ")})" | |
| 277 | si | |
| 278 | ||
| 279 | // Starts `loop`'s next iteration. | |
| 280 | class CONTINUE: Exit is | |
| 281 | loop: LOOP | |
| 282 | ||
| 283 | init(loop: LOOP) is | |
| 284 | super.init(loop, IR.BRANCH.ALWAYS, Collections.LIST[Value]()) | |
| 285 | ||
| 286 | self.loop = loop | |
| 287 | si | |
| 288 | ||
| 289 | init(loop: LOOP, condition: IR.BRANCH, operands: Collections.List[Value]) is | |
| 290 | super.init(loop, condition, operands) | |
| 291 | ||
| 292 | self.loop = loop | |
| 293 | si | |
| 294 | ||
| 295 | init(loop: LOOP, condition: IR.BRANCH, operand: Value) is | |
| 296 | super.init(loop, condition, operands_of(operand)) | |
| 297 | ||
| 298 | self.loop = loop | |
| 299 | si | |
| 300 | ||
| 301 | init(loop: LOOP, condition: IR.BRANCH, left: Value, right: Value) is | |
| 302 | super.init(loop, condition, operands_of(left, right)) | |
| 303 | ||
| 304 | self.loop = loop | |
| 305 | si | |
| 306 | ||
| 307 | to_string() -> string => "continue {loop.start} {condition}({operands |> join(", ")})" | |
| 308 | si | |
| 309 | si |