Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use IO.Std | |
| 3 | ||
| 4 | use System.Text.StringBuilder | |
| 5 | ||
| 6 | use Logging | |
| 7 | use Trees | |
| 8 | use Source | |
| 9 | ||
| 10 | use IR | |
| 11 | use IR.Values | |
| 12 | ||
| 13 | use Ghul.Pipes | |
| 14 | ||
| 15 | ||
| 16 | // Expression IL: call/await/spill/cast walks, await-suspend construction and string interpolation. | |
| 17 | partial GENERATE_IL is | |
| 18 | // Override CALL.walk's default args-first order so SPILLs in | |
| 19 | // the receiver position emit before AWAIT_SUSPENDs in the | |
| 20 | // argument position — needed for source-order side effects | |
| 21 | // across instance-call receivers. | |
| 22 | pre(call: Trees.Expressions.CALL) -> bool is | |
| 23 | super.pre(call) | |
| 24 | ||
| 25 | call.function.walk(self) | |
| 26 | call.arguments.walk(self) | |
| 27 | ||
| 28 | return true | |
| 29 | si | |
| 30 | ||
| 31 | visit(`await: Trees.Expressions.AWAIT) is | |
| 32 | // Emit AWAIT_SUSPEND eagerly, stash the result into a | |
| 33 | // fresh frame field (NOT a CLR local — CLR locals reset | |
| 34 | // on every MoveNext re-entry), and rebind the wrapper to | |
| 35 | // a `ldfld <field>` load. The CLR-local TEMP is a | |
| 36 | // one-MoveNext bridge: we can't `ldarg.0` before | |
| 37 | // AWAIT_SUSPEND (would push across its internal labels), | |
| 38 | // so the suspend's result goes to a tmp first, then | |
| 39 | // `ldarg.0 / ldloc / stfld`. | |
| 40 | let await_wrapper = cast Values.WRAPPER?(`await.value) | |
| 41 | ||
| 42 | if !await_wrapper? then | |
| 43 | return | |
| 44 | fi | |
| 45 | ||
| 46 | let casm = _current_async_state_machine | |
| 47 | ||
| 48 | if !casm? \/ current_function != casm.function then | |
| 49 | _logger.error(`await.location, "await outside async state-machine context") | |
| 50 | return | |
| 51 | fi | |
| 52 | ||
| 53 | let operand_value = `await.operand.value | |
| 54 | if !operand_value? then return; fi | |
| 55 | let operand_type = operand_value.type | |
| 56 | if !operand_type? then return; fi | |
| 57 | ||
| 58 | let awaitable = `await.awaitable | |
| 59 | if !awaitable? then return; fi | |
| 60 | ||
| 61 | let suspend = | |
| 62 | _build_await_suspend(`await.location, operand_value, awaitable, `await.awaiter_field) | |
| 63 | ||
| 64 | let frame = casm.frame | |
| 65 | ||
| 66 | assert frame? else "async state machine has no frame at await emission" | |
| 67 | ||
| 68 | let suspend_type = suspend.type | |
| 69 | ||
| 70 | if !suspend_type.is_void then | |
| 71 | let result_field = `await.result_field | |
| 72 | ||
| 73 | assert result_field? else "await has no result field at IL generation" | |
| 74 | ||
| 75 | let result_temp = TEMP(current_block, "await_bridge", suspend) | |
| 76 | ||
| 77 | add(_build_frame_field_store(frame, result_field, result_temp.load())) | |
| 78 | ||
| 79 | await_wrapper.value = | |
| 80 | IR.Values.Load.INSTANCE_FIELD( | |
| 81 | IR.Values.Load.REFERENCE_SELF(frame, frame.type), | |
| 82 | result_field | |
| 83 | ) | |
| 84 | else | |
| 85 | // void-async — emit the suspend IL eagerly. The wrapper | |
| 86 | // already holds a void-typed DUMMY which surrounding | |
| 87 | // emit paths must skip (the type check in | |
| 88 | // visit(Statements.EXPRESSION) handles the only place | |
| 89 | // this matters in practice — bare `await E;`). | |
| 90 | add(suspend) | |
| 91 | fi | |
| 92 | si | |
| 93 | ||
| 94 | visit(spill: Trees.Expressions.SPILL) is | |
| 95 | // Eagerly emit the operand's IL, then `stfld` to a fresh | |
| 96 | // frame field. The wrapper's value becomes a | |
| 97 | // `ldarg.0; ldfld <field>` load that surrounding IR | |
| 98 | // consumes lazily. Used by the SPILL_AWAITS pass to | |
| 99 | // persist intermediate values evaluated to the left of an | |
| 100 | // `await` so they survive the suspend. | |
| 101 | let spill_wrapper = cast Values.WRAPPER?(spill.value) | |
| 102 | ||
| 103 | if !spill_wrapper? then | |
| 104 | return | |
| 105 | fi | |
| 106 | ||
| 107 | let operand_value = spill.operand.value | |
| 108 | if !operand_value? then return; fi | |
| 109 | ||
| 110 | let casm = _current_async_state_machine | |
| 111 | ||
| 112 | // Outside an async body nothing suspends, so the value only | |
| 113 | // has to be off the stack while a protected region opens, | |
| 114 | // and a local holds it. | |
| 115 | if !casm? \/ current_function != casm.function \/ !spill.spill_field? then | |
| 116 | spill_wrapper.value = IR.TEMP(current_block, "spill", operand_value).load() | |
| 117 | return | |
| 118 | fi | |
| 119 | ||
| 120 | let frame = casm.frame | |
| 121 | ||
| 122 | assert frame? else "async state machine has no frame at spill emission" | |
| 123 | ||
| 124 | let spill_field = spill.spill_field | |
| 125 | ||
| 126 | assert spill_field? else "spill has no frame field at IL generation" | |
| 127 | ||
| 128 | add(_build_frame_field_store(frame, spill_field, operand_value)) | |
| 129 | ||
| 130 | spill_wrapper.value = | |
| 131 | IR.Values.Load.INSTANCE_FIELD( | |
| 132 | IR.Values.Load.REFERENCE_SELF(frame, frame.type), | |
| 133 | spill_field | |
| 134 | ) | |
| 135 | si | |
| 136 | ||
| 137 | visit(`cast: Trees.Expressions.CAST) is | |
| 138 | let cast_wrapper = cast Values.WRAPPER?(`cast.value) | |
| 139 | ||
| 140 | if !cast_wrapper? then return; fi | |
| 141 | ||
| 142 | let right_value = `cast.right.value | |
| 143 | if !right_value? then return; fi | |
| 144 | let cast_type = cast_wrapper.type | |
| 145 | if !cast_type? then return; fi | |
| 146 | ||
| 147 | // Use the WRAPPER's type, not `cast.type_expression.type` | |
| 148 | // — for a bare-variant cast on a generic union (a `cast V(x)` | |
| 149 | // or the `cast` produced by `if let v: V = x` lowering), | |
| 150 | // compile_expressions.visit(CAST) already substituted the | |
| 151 | // receiver-specialized variant type, but the user's | |
| 152 | // type_expression still reads as the open generic. | |
| 153 | cast_wrapper.value = | |
| 154 | _type_caster | |
| 155 | .cast_value( | |
| 156 | `cast.location, | |
| 157 | right_value, | |
| 158 | cast_type, | |
| 159 | true, | |
| 160 | `cast.is_checked | |
| 161 | ) | |
| 162 | si | |
| 163 | ||
| 164 | // Build an AWAIT_SUSPEND for `awaited`: allocates a state | |
| 165 | // number + a cold resume label + a fresh `$awaiter_N` | |
| 166 | // field, and records the (state, awaiter, label) triple | |
| 167 | // for entry-dispatch population. | |
| 168 | // | |
| 169 | // The three member calls the suspend makes are built as | |
| 170 | // ordinary call values, so a struct awaitable or awaiter is | |
| 171 | // addressed and a class one dispatched exactly as a call | |
| 172 | // written in source would be. | |
| 173 | _build_await_suspend( | |
| 174 | location: Source.LOCATION, | |
| 175 | awaited: Value, | |
| 176 | awaitable: Semantic.AWAITABLE, | |
| 177 | declared_awaiter_field: Semantic.Symbols.Field? | |
| 178 | ) -> IR.Values.AWAIT_SUSPEND is | |
| 179 | let async_sm = _current_async_state_machine! | |
| 180 | let frame = async_sm.frame | |
| 181 | ||
| 182 | assert frame? else "async state machine has no frame at await suspend emission" | |
| 183 | ||
| 184 | let awaiter_type = awaitable.awaiter_type | |
| 185 | let state = async_sm.allocate_state() | |
| 186 | let cold_resume = IR.LABEL() | |
| 187 | let awaiter_field = | |
| 188 | declared_awaiter_field ?? frame.declare_awaiter_field(awaiter_type) | |
| 189 | ||
| 190 | async_sm.record_label(state, awaiter_field, cold_resume) | |
| 191 | ||
| 192 | _register_resume_with_dispatch(state, cold_resume) | |
| 193 | ||
| 194 | let frame_type = | |
| 195 | _specialized_frame_type(frame, async_sm.get_construction_type_arguments(), location) ?? frame.type! | |
| 196 | ||
| 197 | let no_arguments = Collections.LIST[Value]() | |
| 198 | ||
| 199 | let get_awaiter = | |
| 200 | awaitable.get_awaiter.call(location, awaited, no_arguments, null, _function_caller) | |
| 201 | ||
| 202 | let awaiter_load = () => | |
| 203 | IR.Values.Load.INSTANCE_FIELD( | |
| 204 | IR.Values.Load.REFERENCE_SELF(frame, frame_type), | |
| 205 | awaiter_field | |
| 206 | ) | |
| 207 | ||
| 208 | let is_completed = | |
| 209 | awaitable.is_completed.call(location, awaiter_load(), no_arguments, null, _function_caller) | |
| 210 | ||
| 211 | let get_result = | |
| 212 | awaitable.get_result.call(location, awaiter_load(), no_arguments, null, _function_caller) | |
| 213 | ||
| 214 | return IR.Values.AWAIT_SUSPEND( | |
| 215 | awaitable.result_type, | |
| 216 | get_awaiter, | |
| 217 | is_completed, | |
| 218 | get_result, | |
| 219 | awaiter_type, | |
| 220 | awaitable.is_critical, | |
| 221 | state, | |
| 222 | _current_state_local()!, | |
| 223 | cold_resume, | |
| 224 | _current_async_resumable!, | |
| 225 | frame_type, | |
| 226 | frame.state_field, | |
| 227 | awaiter_field, | |
| 228 | frame.builder_field! | |
| 229 | ) | |
| 230 | si | |
| 231 | ||
| 232 | visit(interpolation: Expressions.STRING_INTERPOLATION) is | |
| 233 | ensure_runtime_symbols_are_materialized() | |
| 234 | ||
| 235 | enter_block(cast IR.Values.BLOCK?(interpolation.value)!) | |
| 236 | ||
| 237 | let string_type = _innate_symbol_lookup.get_string_type() | |
| 238 | ||
| 239 | let literal_length = Literal.NUMBER(interpolation.literal_length, _innate_symbol_lookup.get_int_type()) | |
| 240 | let expression_count = Literal.NUMBER(interpolation.expression_count, _innate_symbol_lookup.get_int_type()) | |
| 241 | ||
| 242 | let interpolator = | |
| 243 | TEMP( | |
| 244 | current_block, | |
| 245 | "interpolator", | |
| 246 | Values.RUNTIME( | |
| 247 | IR.RuntimeOperation.NEW_STRING_BUILDER, | |
| 248 | literal_length, | |
| 249 | expression_count, | |
| 250 | _innate_symbol_lookup.get_interpolated_string_handler_type() | |
| 251 | ) | |
| 252 | ) | |
| 253 | ||
| 254 | for eaf in interpolation.values do | |
| 255 | let expression = eaf.expression | |
| 256 | let alignment = eaf.alignment | |
| 257 | let format = eaf.format | |
| 258 | let expr_value = expression.value! | |
| 259 | ||
| 260 | if !eaf.is_expression then | |
| 261 | let literal = cast Expressions.Literals.STRING?(expression)! | |
| 262 | ||
| 263 | if literal.value_string.length == 0 then | |
| 264 | continue | |
| 265 | fi | |
| 266 | ||
| 267 | add(Values.RUNTIME(IR.RuntimeOperation.APPEND_TEXT, interpolator.load(), expr_value, null)) | |
| 268 | elif !format? /\ !alignment? then | |
| 269 | _append_interpolated_value(interpolation.location, interpolator, expr_value) | |
| 270 | else | |
| 271 | let operands = Collections.LIST[Value](4) | |
| 272 | ||
| 273 | operands.add(interpolator.load()) | |
| 274 | operands.add(expr_value) | |
| 275 | ||
| 276 | let operation: IR.RuntimeOperation mut | |
| 277 | ||
| 278 | if format? /\ alignment? then | |
| 279 | operands.add(alignment.value!) | |
| 280 | operands.add(Literal.STRING(format, string_type)) | |
| 281 | ||
| 282 | operation = IR.RuntimeOperation.APPEND_ALIGNED_FORMATTED_VALUE | |
| 283 | elif format? then | |
| 284 | operands.add(Literal.STRING(format, string_type)) | |
| 285 | ||
| 286 | operation = IR.RuntimeOperation.APPEND_FORMATTED_VALUE | |
| 287 | else | |
| 288 | operands.add(alignment!.value!) | |
| 289 | ||
| 290 | operation = IR.RuntimeOperation.APPEND_ALIGNED_VALUE | |
| 291 | fi | |
| 292 | ||
| 293 | add(Values.RUNTIME(operation, operands, null, expr_value.type!)) | |
| 294 | fi | |
| 295 | od | |
| 296 | ||
| 297 | add(Values.RUNTIME(IR.RuntimeOperation.BUILD_STRING, interpolator.load(), string_type)) | |
| 298 | ||
| 299 | leave_block() | |
| 300 | si | |
| 301 | // Append an interpolated value given no format or alignment, as | |
| 302 | // INTERPOLATED_VALUE_CLASSIFIER says it reads. | |
| 303 | _append_interpolated_value(location: Source.LOCATION, interpolator: TEMP, value: Value) is | |
| 304 | let type = value.type! | |
| 305 | ||
| 306 | case _interpolated_value_classifier.classify(type, false) | |
| 307 | when Semantic.InterpolatedValueRendering.BOOL then | |
| 308 | _append_bool(location, interpolator, value) | |
| 309 | ||
| 310 | when Semantic.InterpolatedValueRendering.OPTIONAL then | |
| 311 | _append_optional(location, interpolator, value) | |
| 312 | ||
| 313 | when Semantic.InterpolatedValueRendering.DISPLAYED then | |
| 314 | if let display = _display_function then | |
| 315 | _append_formatted(location, interpolator, display.call(location, null, [value]:Value, null, _function_caller)) | |
| 316 | else | |
| 317 | _append_formatted(location, interpolator, value) | |
| 318 | fi | |
| 319 | ||
| 320 | else | |
| 321 | _append_formatted(location, interpolator, value) | |
| 322 | esac | |
| 323 | si | |
| 324 | ||
| 325 | _append_formatted(location: Source.LOCATION, interpolator: TEMP, value: Value) is | |
| 326 | add(Values.RUNTIME(IR.RuntimeOperation.APPEND_VALUE, [interpolator.load(), value]:Value, null, value.type!)) | |
| 327 | si | |
| 328 | ||
| 329 | _append_text(location: Source.LOCATION, interpolator: TEMP, text: string) is | |
| 330 | add( | |
| 331 | Values.RUNTIME( | |
| 332 | IR.RuntimeOperation.APPEND_TEXT, | |
| 333 | interpolator.load(), | |
| 334 | Literal.STRING(text, _innate_symbol_lookup.get_string_type()), | |
| 335 | null)) | |
| 336 | si | |
| 337 | ||
| 338 | _append_bool(location: Source.LOCATION, interpolator: TEMP, value: Value) is | |
| 339 | let done = _open_region(LABEL()) | |
| 340 | let true_arm = _open_region(LABEL()) | |
| 341 | ||
| 342 | add(Values.BREAK(true_arm, IR.BRANCH.Z, value)) | |
| 343 | _append_text(location, interpolator, "true") | |
| 344 | add(Values.BREAK(done)) | |
| 345 | ||
| 346 | _close_structured(true_arm) | |
| 347 | _append_text(location, interpolator, "false") | |
| 348 | ||
| 349 | _close_structured(done) | |
| 350 | si | |
| 351 | ||
| 352 | // Append what an optional holds, by the rules for its non-optional | |
| 353 | // type, or `null` when it holds nothing. | |
| 354 | _append_optional(location: Source.LOCATION, interpolator: TEMP, value: Value) is | |
| 355 | let type = value.type! | |
| 356 | let inner = type.optional_inner_type! | |
| 357 | ||
| 358 | if type.is_value_type /\ (!type.find_member("has_value")? \/ !type.find_member("value")?) then | |
| 359 | _append_formatted(location, interpolator, value) | |
| 360 | ||
| 361 | return | |
| 362 | fi | |
| 363 | ||
| 364 | let done = _open_region(LABEL()) | |
| 365 | let present = _open_region(LABEL()) | |
| 366 | ||
| 367 | if type.is_value_type then | |
| 368 | let has_value = type.find_member("has_value")! | |
| 369 | let payload = type.find_member("value")! | |
| 370 | ||
| 371 | let holder = TEMP(current_block, "optional", value) | |
| 372 | let symbol_loader = IoC.CONTAINER.instance.symbol_loader | |
| 373 | ||
| 374 | add(Values.BREAK(present, IR.BRANCH.Z, has_value.load(location, IR.Values.ADDRESS(holder.load()), symbol_loader))) | |
| 375 | _append_interpolated_value(location, interpolator, payload.load(location, IR.Values.ADDRESS(holder.load()), symbol_loader)) | |
| 376 | else | |
| 377 | let holder = TEMP(current_block, "optional", inner) | |
| 378 | ||
| 379 | holder.store(value) | |
| 380 | ||
| 381 | add(Values.BREAK(present, IR.BRANCH.Z, holder.load())) | |
| 382 | _append_interpolated_value(location, interpolator, holder.load()) | |
| 383 | fi | |
| 384 | ||
| 385 | add(Values.BREAK(done)) | |
| 386 | ||
| 387 | _close_structured(present) | |
| 388 | _append_text(location, interpolator, "null") | |
| 389 | ||
| 390 | _close_structured(done) | |
| 391 | si | |
| 392 | si | |
| 393 | si |