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 | // Local-variable IL: assignments, variable visits, refutable bindings and destructuring. | |
| 17 | partial GENERATE_IL is | |
| 18 | visit(assign: Statements.ASSIGNMENT) is | |
| 19 | super.visit(assign) | |
| 20 | ||
| 21 | let v = assign.value | |
| 22 | if v? then | |
| 23 | add(v) | |
| 24 | fi | |
| 25 | si | |
| 26 | ||
| 27 | visit(expression: Statements.EXPRESSION) is | |
| 28 | super.visit(expression) | |
| 29 | ||
| 30 | try | |
| 31 | if !expression.want_value /\ expression.expression.value? then | |
| 32 | let value = expression.expression.value | |
| 33 | ||
| 34 | // `NULL.matches` answers true for every type, so a | |
| 35 | // null-typed value reads as void here. It is not: emit | |
| 36 | // it and pop, as for any other discarded statement | |
| 37 | // value. | |
| 38 | let is_void = | |
| 39 | value.type? /\ | |
| 40 | !value.type.is_null /\ | |
| 41 | value.type.matches(_innate_symbol_lookup.get_void_type()) | |
| 42 | ||
| 43 | // Bare `await E;` with void result: visit(AWAIT) | |
| 44 | // emitted the suspend eagerly; re-emitting the | |
| 45 | // wrapper would call gen on its unreplaced DUMMY. | |
| 46 | if is_void then | |
| 47 | if !isa Values.WRAPPER(value) then | |
| 48 | add(value) | |
| 49 | fi | |
| 50 | else | |
| 51 | add(Values.DISCARD(value)) | |
| 52 | fi | |
| 53 | fi | |
| 54 | catch e: System.Exception | |
| 55 | _logger.exception(expression.location, e, "caught exception generating IL for expression") | |
| 56 | yrt | |
| 57 | si | |
| 58 | ||
| 59 | // A local's declaration is a slot in the body's signature, so | |
| 60 | // it is only a local at all inside a block. Answers whether | |
| 61 | // the symbol was taken as one. | |
| 62 | _declare_local(symbol: Semantic.Symbols.Symbol?) -> bool is | |
| 63 | if !_block_context.is_in_block then | |
| 64 | return false | |
| 65 | fi | |
| 66 | ||
| 67 | if let local: Semantic.Symbols.LOCAL_VARIABLE = symbol /\ local.type? then | |
| 68 | current_block.add(Values.DECLARE_LOCAL(local.ir_local)) | |
| 69 | fi | |
| 70 | ||
| 71 | return true | |
| 72 | si | |
| 73 | ||
| 74 | pre(variable: Variables.VARIABLE) -> bool is | |
| 75 | super.pre(variable) | |
| 76 | ||
| 77 | if variable.want_dispose then | |
| 78 | // variable will be declared outside the enclosing .try | |
| 79 | // so don't declare it here | |
| 80 | return false | |
| 81 | fi | |
| 82 | ||
| 83 | // Generator / async body locals: `state_machine_field` | |
| 84 | // was wired up in compile-expressions (so closures | |
| 85 | // freezing inside the body capture the right | |
| 86 | // ldarg.0/ldfld IL). Skip emitting `.locals init` here; | |
| 87 | // load/store routes through the frame field via | |
| 88 | // state_machine_field. | |
| 89 | let cf = current_function | |
| 90 | if cf? then | |
| 91 | let sm_for_function = Semantic.Symbols.state_machine_for(cf) | |
| 92 | ||
| 93 | if sm_for_function? /\ sm_for_function.frame? then | |
| 94 | return false | |
| 95 | fi | |
| 96 | ||
| 97 | let async_sm = Semantic.Symbols.async_state_machine_for(cf) | |
| 98 | ||
| 99 | if async_sm? /\ async_sm.frame? then | |
| 100 | return false | |
| 101 | fi | |
| 102 | fi | |
| 103 | ||
| 104 | // A local's declaration is a value in the block: metadata | |
| 105 | // has no `.locals init` directive, and the slot it | |
| 106 | // allocates has to be in place before any load of the | |
| 107 | // same name is encoded. A declaration at namespace or | |
| 108 | // class scope is a field, whose row the structure walk | |
| 109 | // writes, so nothing is emitted for one here. | |
| 110 | if _block_context.is_in_block then | |
| 111 | for name in variable.names do | |
| 112 | _declare_local(find(name)) | |
| 113 | od | |
| 114 | fi | |
| 115 | ||
| 116 | return false | |
| 117 | si | |
| 118 | ||
| 119 | visit(v: Variables.VARIABLE) is | |
| 120 | let initializer = v.initializer | |
| 121 | if initializer? then | |
| 122 | let init_value = initializer.value | |
| 123 | if init_value? then | |
| 124 | gen_destructuring_initialize(v.left, init_value) | |
| 125 | return | |
| 126 | fi | |
| 127 | fi | |
| 128 | ||
| 129 | // Destructured formal argument: no initializer expression - | |
| 130 | // the "source" to destructure is the synthesised physical | |
| 131 | // parameter declare-members attached to this VARIABLE. | |
| 132 | if v.is_argument /\ !v.left.is_simple_name then | |
| 133 | let group_symbol = symbol_for(v) | |
| 134 | ||
| 135 | if group_symbol? then | |
| 136 | gen_destructuring_initialize(v.left, group_symbol.load(v.location, null, _symbol_loader)) | |
| 137 | fi | |
| 138 | ||
| 139 | return | |
| 140 | fi | |
| 141 | ||
| 142 | // No initializer, but the local is captured by a closure | |
| 143 | // and reassigned, so `mark-boxed-locals` promoted its slot | |
| 144 | // to `Ghul.BOX[T]`. Allocate the empty box at declaration — | |
| 145 | // `Variable.store` routes through `_store_boxed_local`, | |
| 146 | // which emits `newobj Ghul.BOX[T]::.ctor()` when the value | |
| 147 | // is null. Without this the slot stays null, then the | |
| 148 | // closure frame's ctor stashes a null box reference and | |
| 149 | // every later read or write NREs. | |
| 150 | if v.left.is_simple_name then | |
| 151 | // is_simple_name => SIMPLE_VARIABLE_LEFT, whose name is non-null | |
| 152 | let name = v.left.name! | |
| 153 | let symbol = find(name) | |
| 154 | ||
| 155 | if let variable: Semantic.Symbols.LOCAL_VARIABLE = symbol /\ variable.is_boxed then | |
| 156 | add(variable.store_empty_box(v.left.location, _symbol_loader)) | |
| 157 | return | |
| 158 | fi | |
| 159 | fi | |
| 160 | ||
| 161 | // Loop-scoped `let x: T;` (no initializer) needs an | |
| 162 | // explicit default store: .NET only initialises locals | |
| 163 | // on method entry, so without re-zeroing each iteration | |
| 164 | // the variable carries its previous iteration's value | |
| 165 | // (issue #483). Outside a loop, the method-entry | |
| 166 | // initialisation is correct and we leave the existing | |
| 167 | // behaviour alone. Catch handlers receive their value | |
| 168 | // from the runtime — skip the default-store there too. | |
| 169 | if _loops.is_in_loop /\ !_in_catch_variable then | |
| 170 | gen_destructuring_default(v.left) | |
| 171 | fi | |
| 172 | si | |
| 173 | ||
| 174 | // `if let` clauses' bound names are declared as CLR locals | |
| 175 | // here — same as `pre(VARIABLE)` does for a plain let. The | |
| 176 | // store IL itself is emitted later by | |
| 177 | // `gen_destructuring_initialize` in the if-branch handler. | |
| 178 | pre(rb: Statements.REFUTABLE_BINDING) -> bool is | |
| 179 | let cf = current_function | |
| 180 | if cf? then | |
| 181 | let sm_for_function = Semantic.Symbols.state_machine_for(cf) | |
| 182 | ||
| 183 | if sm_for_function? /\ sm_for_function.frame? then | |
| 184 | return false | |
| 185 | fi | |
| 186 | ||
| 187 | let async_sm = Semantic.Symbols.async_state_machine_for(cf) | |
| 188 | ||
| 189 | if async_sm? /\ async_sm.frame? then | |
| 190 | return false | |
| 191 | fi | |
| 192 | fi | |
| 193 | ||
| 194 | for c in rb.clauses do | |
| 195 | for name in c.pattern.names! do | |
| 196 | _declare_local(find(name)) | |
| 197 | od | |
| 198 | od | |
| 199 | ||
| 200 | return false | |
| 201 | si | |
| 202 | ||
| 203 | visit(rb: Statements.REFUTABLE_BINDING) is | |
| 204 | si | |
| 205 | ||
| 206 | gen_destructuring_default(left: Variables.VariableLeft) is | |
| 207 | if left.is_simple_name then | |
| 208 | // is_simple_name => SIMPLE_VARIABLE_LEFT, whose name is non-null | |
| 209 | let name = left.name! | |
| 210 | let symbol = find(name) | |
| 211 | ||
| 212 | if !symbol? \/ symbol.is_argument then | |
| 213 | return | |
| 214 | fi | |
| 215 | let symbol_type = symbol.type | |
| 216 | if !symbol_type? then return; fi | |
| 217 | ||
| 218 | let default_value = IR.Values.DEFAULT(symbol_type) | |
| 219 | ||
| 220 | let store = symbol.store(left.location, null, default_value, _symbol_loader, true) | |
| 221 | ||
| 222 | add(store) | |
| 223 | else | |
| 224 | for e in left.elements! do | |
| 225 | gen_destructuring_default(e) | |
| 226 | od | |
| 227 | fi | |
| 228 | si | |
| 229 | ||
| 230 | // TODO use Pre/Visit instead | |
| 231 | gen_destructuring_initialize(left: Variables.VariableLeft, value: Value) is | |
| 232 | gen_destructuring_initialize(left, value, null) | |
| 233 | si | |
| 234 | ||
| 235 | // Per-element narrowing: when `narrow_fail` is set (the `if | |
| 236 | // let` path), an element carrying a `type_expression` emits | |
| 237 | // `isinst T` and leaves `narrow_fail` on a mismatch before | |
| 238 | // binding. The cast result is what gets stored / recursively | |
| 239 | // destructured, so the bound slot is typed T. Plain `let` and | |
| 240 | // `for` pass null and just store the raw value (the declared | |
| 241 | // element type is a compile-time assertion only, not a runtime | |
| 242 | // test). | |
| 243 | gen_destructuring_initialize( | |
| 244 | left: Variables.VariableLeft, | |
| 245 | value: Value, | |
| 246 | narrow_fail: Values.Breakable? | |
| 247 | ) is | |
| 248 | // Literal-leaf inside a destructure pattern — runtime | |
| 249 | // value-equality test against the source position. Only | |
| 250 | // valid in refutable contexts (`if let` / `case`-when); | |
| 251 | // a plain `let` with a literal leaf is rejected during | |
| 252 | // semantic analysis, so by the time we get here a literal | |
| 253 | // leaf without a narrow_fail is a no-op (the | |
| 254 | // emit-nothing path). | |
| 255 | if isa Variables.LITERAL_VARIABLE_LEFT(left) then | |
| 256 | if narrow_fail? then | |
| 257 | let literal_leaf = cast Variables.LITERAL_VARIABLE_LEFT(left) | |
| 258 | let leaf_state = _variable_left_state.get(literal_leaf) | |
| 259 | ||
| 260 | // The value-equality test compile-expressions built | |
| 261 | // for this leaf embeds the literal and reads the | |
| 262 | // source position through a hole; fill the hole and | |
| 263 | // branch on the result. The test can read the | |
| 264 | // operand more than once - a null-safe `=~` tests | |
| 265 | // presence before dispatching - and an instance | |
| 266 | // operator on a value type needs its address, so | |
| 267 | // spill anything that is not already a local. | |
| 268 | if let | |
| 269 | test = leaf_state?.match_test, | |
| 270 | operand = leaf_state?.match_operand | |
| 271 | then | |
| 272 | operand.value = | |
| 273 | if value.is_lightweight_pure /\ value.has_address then | |
| 274 | value | |
| 275 | else | |
| 276 | TEMP(current_block, "literal-leaf", value).load() | |
| 277 | fi | |
| 278 | ||
| 279 | add(Values.BREAK(narrow_fail, BRANCH.Z, test)) | |
| 280 | ||
| 281 | return | |
| 282 | fi | |
| 283 | ||
| 284 | // No test: a `null` leaf, or a type for which | |
| 285 | // neither `=~` nor `<>` resolved. Compare raw, which | |
| 286 | // is what a bare `==` does too. | |
| 287 | literal_leaf.expression.walk(self) | |
| 288 | ||
| 289 | let leaf_value = literal_leaf.expression.value | |
| 290 | if leaf_value? then | |
| 291 | add(Values.BREAK(narrow_fail, BRANCH.NE, value, leaf_value)) | |
| 292 | fi | |
| 293 | fi | |
| 294 | ||
| 295 | return | |
| 296 | fi | |
| 297 | ||
| 298 | let narrowed mut = value | |
| 299 | ||
| 300 | let type_expression = left.type_expression | |
| 301 | if | |
| 302 | type_expression? /\ | |
| 303 | type_expression.type? /\ | |
| 304 | narrow_fail? | |
| 305 | then | |
| 306 | let cast_value = IR.Values.CAST(type_expression.type, value, false) | |
| 307 | let temp = TEMP(current_block, "narrow", cast_value) | |
| 308 | ||
| 309 | add(Values.BREAK(narrow_fail, BRANCH.Z, temp.load())) | |
| 310 | ||
| 311 | narrowed = temp.load() | |
| 312 | fi | |
| 313 | ||
| 314 | if left.is_simple_name then | |
| 315 | // is_simple_name => SIMPLE_VARIABLE_LEFT, whose name is non-null | |
| 316 | let name = left.name! | |
| 317 | let symbol = find(name) | |
| 318 | ||
| 319 | if !symbol? then | |
| 320 | return | |
| 321 | fi | |
| 322 | ||
| 323 | let store = symbol.store(left.location, null, narrowed, _symbol_loader, true) | |
| 324 | ||
| 325 | add(store) | |
| 326 | else | |
| 327 | let from_type = narrowed.type | |
| 328 | ||
| 329 | if !from_type? then return; fi | |
| 330 | ||
| 331 | // !is_simple_name => a destructure group, whose elements are non-null | |
| 332 | let elements = left.elements! | |
| 333 | ||
| 334 | let get_from = | |
| 335 | if narrowed.is_lightweight_pure then | |
| 336 | () => narrowed | |
| 337 | else | |
| 338 | let temp = TEMP(current_block, "destructure", narrowed) | |
| 339 | () => temp.load() | |
| 340 | fi | |
| 341 | ||
| 342 | let is_named_group = elements.count > 0 /\ elements[0].source_field_name? | |
| 343 | ||
| 344 | let strategy = | |
| 345 | if is_named_group then | |
| 346 | let field_names = Collections.LIST[string?]() | |
| 347 | for e in elements do | |
| 348 | let sfn = e.source_field_name | |
| 349 | field_names.add(if sfn? then sfn.name else null fi) | |
| 350 | od | |
| 351 | DESTRUCTURE_RESOLVER.resolve_strategy_by_name(from_type, field_names) | |
| 352 | else | |
| 353 | DESTRUCTURE_RESOLVER.resolve_strategy(from_type, elements.count) | |
| 354 | fi | |
| 355 | ||
| 356 | if strategy.is_deconstruct then | |
| 357 | // is_deconstruct ⇔ deconstruct_function? per its definition | |
| 358 | let deconstruct = strategy.deconstruct_function! | |
| 359 | let arg_temps = Collections.LIST[IR.TEMP]() | |
| 360 | let call_args = Collections.LIST[Value]() | |
| 361 | ||
| 362 | for i in 0..deconstruct.arguments.count do | |
| 363 | let ref_type = deconstruct.arguments[i] | |
| 364 | let element_type = ref_type.get_element_type() | |
| 365 | assert element_type? else "deconstruct arg type has no element type" | |
| 366 | let arg_temp = IR.TEMP(current_block, "destructure_arg", i, element_type) | |
| 367 | ||
| 368 | arg_temps.add(arg_temp) | |
| 369 | call_args.add(IR.Values.ADDRESS(arg_temp.load(), ref_type)) | |
| 370 | od | |
| 371 | ||
| 372 | let call_value = | |
| 373 | deconstruct.call( | |
| 374 | left.location, | |
| 375 | get_from(), | |
| 376 | call_args, | |
| 377 | null, | |
| 378 | _function_caller | |
| 379 | ) | |
| 380 | ||
| 381 | add(call_value) | |
| 382 | ||
| 383 | for (i, e) in elements |> index() do | |
| 384 | gen_destructuring_initialize(e, arg_temps[i].load(), narrow_fail) | |
| 385 | od | |
| 386 | else | |
| 387 | let members = strategy.members | |
| 388 | ||
| 389 | for (i, e) in elements |> index() do | |
| 390 | let member = members[i] | |
| 391 | ||
| 392 | if member? then | |
| 393 | let member_value = member.load(LOCATION.internal, get_from(), _symbol_loader) | |
| 394 | ||
| 395 | gen_destructuring_initialize(e, member_value, narrow_fail) | |
| 396 | fi | |
| 397 | od | |
| 398 | fi | |
| 399 | fi | |
| 400 | si | |
| 401 | ||
| 402 | si | |
| 403 | si |