Appearance
| 1 | namespace IR.WasmGC is | |
| 2 | use IR.Program | |
| 3 | ||
| 4 | use Wasm.INSTRUCTION | |
| 5 | use Wasm.SIMPLE_INSTRUCTION | |
| 6 | use Wasm.INDEX_INSTRUCTION | |
| 7 | use Wasm.I32_CONSTANT | |
| 8 | use Wasm.I64_CONSTANT | |
| 9 | use Wasm.F32_CONSTANT | |
| 10 | use Wasm.F64_CONSTANT | |
| 11 | use Wasm.BLOCK_INSTRUCTION | |
| 12 | use Wasm.BLOCK_TYPE | |
| 13 | use Wasm.VALUE_TYPE | |
| 14 | use Wasm.NumericType | |
| 15 | use Wasm.Opcode | |
| 16 | use Wasm.HEAP_TYPE | |
| 17 | use Wasm.TYPE_INSTRUCTION | |
| 18 | use Wasm.TYPE_FIELD_INSTRUCTION | |
| 19 | use Wasm.TYPE_LENGTH_INSTRUCTION | |
| 20 | use Wasm.HEAP_TYPE_INSTRUCTION | |
| 21 | use Wasm.CAST_BRANCH_INSTRUCTION | |
| 22 | use Wasm.THROW_INSTRUCTION | |
| 23 | use Wasm.TRY_TABLE_INSTRUCTION | |
| 24 | use Wasm.CATCH_CLAUSE | |
| 25 | use Wasm.CatchKind | |
| 26 | use Wasm.AbstractHeapType | |
| 27 | use Wasm.IF_INSTRUCTION | |
| 28 | use Wasm.CALL_INDIRECT_INSTRUCTION | |
| 29 | use Wasm.BR_TABLE_INSTRUCTION | |
| 30 | ||
| 31 | use Collections.LIST | |
| 32 | use Collections.MAP | |
| 33 | ||
| 34 | use Semantic.Types.Type | |
| 35 | use Semantic.Symbols.Function | |
| 36 | ||
| 37 | use Values.Value | |
| 38 | ||
| 39 | /// Lowers the IR values of one function body to wasm instructions. A | |
| 40 | /// construct it cannot lower yet is recorded in `unsupported` rather | |
| 41 | /// than thrown. | |
| 42 | class WASMGC_LOWERING: Values.Visitor is | |
| 43 | // Structured control flow maps directly: a REGION is a `block`, a | |
| 44 | // LOOP is a `loop` inside a `block`, a BREAK branches to the block | |
| 45 | // and a CONTINUE to the loop. Branch depths count the blocks open | |
| 46 | // between an exit and its target, so they are found from the nodes | |
| 47 | // open when the exit is lowered. | |
| 48 | ||
| 49 | _numbering: WASMGC_FUNCTION_NUMBERING | |
| 50 | _registry: WASMGC_TYPE_REGISTRY | |
| 51 | ||
| 52 | // The instantiation being compiled: the type arguments the body's | |
| 53 | // type parameters stand for. | |
| 54 | _instance: FUNCTION_INSTANCE? | |
| 55 | ||
| 56 | // What the body's type parameters stand for: the instantiation's | |
| 57 | // arguments, and in a generator frame's member, the generator's own | |
| 58 | // type parameters too, which its hoisted locals are still typed by. | |
| 59 | _body_type_map: Collections.Map[Semantic.Symbols.Symbol, Type] | |
| 60 | _imports: WASMGC_IMPORTS | |
| 61 | _strings: WASMGC_STRINGS? | |
| 62 | _exceptions: WASMGC_EXCEPTIONS? | |
| 63 | ||
| 64 | // An instance method's receiver is its parameter 0, so its | |
| 65 | // declared arguments start at 1. | |
| 66 | _receiver_offset: int | |
| 67 | ||
| 68 | _parameter_count: int | |
| 69 | _parameter_types: Collections.List[VALUE_TYPE] | |
| 70 | _return_type: Type? | |
| 71 | ||
| 72 | // The wasm type of the value most recently lowered, as held: for | |
| 73 | // a load, the type of the local, parameter, field or global it | |
| 74 | // reads, which can be wider than the type the IR gives it where | |
| 75 | // flow analysis has narrowed the variable. A consumer that needs | |
| 76 | // the narrower type casts to it. | |
| 77 | _top: VALUE_TYPE? => | |
| 78 | if let cast_to = _top_cast then | |
| 79 | cast_to | |
| 80 | elif let value = _top_value then | |
| 81 | _held_type(value) | |
| 82 | else | |
| 83 | null | |
| 84 | fi | |
| 85 | ||
| 86 | _top_value: Value? | |
| 87 | _top_cast: VALUE_TYPE? | |
| 88 | _locals: MAP[LOCAL, int] | |
| 89 | _local_types: LIST[VALUE_TYPE] | |
| 90 | ||
| 91 | _bodies: LIST[LIST[INSTRUCTION]] | |
| 92 | ||
| 93 | // The labels open at the point being lowered, innermost last: | |
| 94 | // each structured node's block, and a loop's own `loop` after | |
| 95 | // it, which is where a CONTINUE goes. | |
| 96 | _labels: LIST[(node: Values.Breakable?, is_loop_head: bool, finally_exits: WASMGC_FINALLY_EXITS?)] | |
| 97 | ||
| 98 | // The type each value-producing region being lowered results in, | |
| 99 | // which a value leaving it is cast to where it is held wider. | |
| 100 | _region_results: MAP[Values.Breakable, VALUE_TYPE] | |
| 101 | ||
| 102 | // Inside a resumable body: the dispatch that opens it, the state | |
| 103 | // each resume point is reached by, and the states whose resume | |
| 104 | // point lies inside each block, region and loop. | |
| 105 | _dispatch: Values.DISPATCH? | |
| 106 | _resume_point_states: MAP[LABEL, int] | |
| 107 | _resume_states: MAP[Value, LIST[int]] | |
| 108 | _folded_dispatches: LIST[Values.DISPATCH] | |
| 109 | ||
| 110 | // The region each await is lowered as, made once so that every | |
| 111 | // lowering of the body sees the same resume point. | |
| 112 | _await_structures: MAP[Values.AWAIT_SUSPEND, Values.REGION] | |
| 113 | ||
| 114 | _unsupported: LIST[string] | |
| 115 | // The struct-typed locals the body declared, and the scalar locals | |
| 116 | // that live in cells, each of which holds a default box from the | |
| 117 | // start, as a .NET local holds a zeroed value. | |
| 118 | _struct_locals: LIST[(index: int, type: CLASS_INSTANCE)] | |
| 119 | // The reference-typed locals that live in cells, each of which | |
| 120 | // holds an empty cell from the start. | |
| 121 | _reference_cell_locals: LIST[int] | |
| 122 | ||
| 123 | // Set while a call is lowered as the receiver of a call on a struct, | |
| 124 | // where an array element it reads is changed in place, not copied. | |
| 125 | _element_in_place: bool | |
| 126 | ||
| 127 | /// The kinds of construct the lowered values used that this | |
| 128 | /// target cannot generate yet, each once, in the order they were | |
| 129 | /// met. A body with any is not a valid function. | |
| 130 | unsupported: Collections.List[string] => _unsupported | |
| 131 | ||
| 132 | /// The wasm types of the locals the lowered values declared, in | |
| 133 | /// index order; the first is numbered after the parameters. | |
| 134 | local_types: LIST[VALUE_TYPE] => _local_types | |
| 135 | ||
| 136 | /// Where the source position of each statement is recorded, for | |
| 137 | /// the text form of the module, or null where it is not wanted. | |
| 138 | source_lines: WASMGC_WAT_ANNOTATIONS? public | |
| 139 | ||
| 140 | /// The names of the locals this body declared, by local index, | |
| 141 | /// recorded where source positions are. | |
| 142 | local_names: Collections.List[(index: int, name: string)] => _local_names | |
| 143 | ||
| 144 | _local_names: LIST[(index: int, name: string)] | |
| 145 | _pending_position: Source.LOCATION? | |
| 146 | _last_line: int | |
| 147 | ||
| 148 | /// Lowers a body of `function`. | |
| 149 | /// | |
| 150 | /// - numbering: the function indices, which a call numbers the function | |
| 151 | /// it reaches with | |
| 152 | /// - registry: the program's struct types and globals | |
| 153 | /// - imports: the module's imports, which a call to a host | |
| 154 | /// operation is a call of | |
| 155 | /// - parameter_types: the types of the function's parameters, the | |
| 156 | /// receiver first where it takes one | |
| 157 | init( | |
| 158 | numbering: WASMGC_FUNCTION_NUMBERING, | |
| 159 | registry: WASMGC_TYPE_REGISTRY, | |
| 160 | imports: WASMGC_IMPORTS, | |
| 161 | function: FUNCTION_INSTANCE, | |
| 162 | parameter_types: Collections.List[VALUE_TYPE] | |
| 163 | ) is | |
| 164 | init(numbering, registry, imports, null, null, function, parameter_types) | |
| 165 | si | |
| 166 | ||
| 167 | /// Lowers a body of `function` that can use strings. | |
| 168 | /// | |
| 169 | /// - numbering: the function indices, which a call numbers the function | |
| 170 | /// it reaches with | |
| 171 | /// - registry: the program's struct types and globals | |
| 172 | /// - imports: the module's imports, which a call to a host | |
| 173 | /// operation is a call of | |
| 174 | /// - strings: the string literals and helper functions, or null | |
| 175 | /// where the program has no string type | |
| 176 | /// - exceptions: the tag exceptions are thrown with, or null where | |
| 177 | /// the body neither throws nor catches | |
| 178 | /// - parameter_types: the types of the function's parameters, the | |
| 179 | /// receiver first where it takes one | |
| 180 | init( | |
| 181 | numbering: WASMGC_FUNCTION_NUMBERING, | |
| 182 | registry: WASMGC_TYPE_REGISTRY, | |
| 183 | imports: WASMGC_IMPORTS, | |
| 184 | strings: WASMGC_STRINGS?, | |
| 185 | exceptions: WASMGC_EXCEPTIONS?, | |
| 186 | function: FUNCTION_INSTANCE, | |
| 187 | parameter_types: Collections.List[VALUE_TYPE] | |
| 188 | ) is | |
| 189 | init(numbering, registry, imports, parameter_types) | |
| 190 | ||
| 191 | _strings = strings | |
| 192 | _exceptions = exceptions | |
| 193 | ||
| 194 | _instance = function | |
| 195 | _body_type_map = TYPE_MAPS.body_map(function) | |
| 196 | _receiver_offset = if function.definition.is_emitted_with_receiver then 1 else 0 fi | |
| 197 | _return_type = function.specialize(function.definition.return_type) | |
| 198 | si | |
| 199 | ||
| 200 | /// Lowers a body that takes no receiver and whose result needs no | |
| 201 | /// cast. | |
| 202 | init( | |
| 203 | numbering: WASMGC_FUNCTION_NUMBERING, | |
| 204 | registry: WASMGC_TYPE_REGISTRY, | |
| 205 | imports: WASMGC_IMPORTS, | |
| 206 | parameter_types: Collections.List[VALUE_TYPE] | |
| 207 | ) is | |
| 208 | _numbering = numbering | |
| 209 | _registry = registry | |
| 210 | _imports = imports | |
| 211 | _parameter_types = parameter_types | |
| 212 | _parameter_count = parameter_types.count | |
| 213 | ||
| 214 | _locals = MAP[LOCAL, int]() | |
| 215 | _body_type_map = MAP[Semantic.Symbols.Symbol, Type]() | |
| 216 | _local_types = LIST[VALUE_TYPE]() | |
| 217 | _local_names = LIST[(index: int, name: string)]() | |
| 218 | _bodies = LIST[LIST[INSTRUCTION]]() | |
| 219 | _labels = LIST[(node: Values.Breakable?, is_loop_head: bool, finally_exits: WASMGC_FINALLY_EXITS?)]() | |
| 220 | _region_results = MAP[Values.Breakable, VALUE_TYPE]() | |
| 221 | _resume_point_states = MAP[LABEL, int]() | |
| 222 | _resume_states = MAP[Value, LIST[int]]() | |
| 223 | _await_structures = MAP[Values.AWAIT_SUSPEND, Values.REGION]() | |
| 224 | _folded_dispatches = LIST[Values.DISPATCH]() | |
| 225 | _unsupported = LIST[string]() | |
| 226 | _struct_locals = LIST[(index: int, type: CLASS_INSTANCE)]() | |
| 227 | _reference_cell_locals = LIST[int]() | |
| 228 | si | |
| 229 | ||
| 230 | /// Lowers a whole body, answering its instructions. | |
| 231 | lower(values: Collections.Iterable[Value]) -> LIST[INSTRUCTION] is | |
| 232 | _begin() | |
| 233 | ||
| 234 | for value in values do | |
| 235 | _mark(value) | |
| 236 | gen(value) | |
| 237 | od | |
| 238 | ||
| 239 | let body = _end() | |
| 240 | ||
| 241 | if (_struct_locals.count == 0 /\ _reference_cell_locals.count == 0) \/ _registry.discovering then | |
| 242 | return body | |
| 243 | fi | |
| 244 | ||
| 245 | let result = LIST[INSTRUCTION]() | |
| 246 | ||
| 247 | for (index, type) in _struct_locals do | |
| 248 | result.add_range(_registry.new_instance(type)) | |
| 249 | result.add(INDEX_INSTRUCTION(Opcode.LOCAL_SET, index)) | |
| 250 | od | |
| 251 | ||
| 252 | for index in _reference_cell_locals do | |
| 253 | result.add(HEAP_TYPE_INSTRUCTION(Opcode.REF_NULL, HEAP_TYPE.of_abstract(AbstractHeapType.NONE))) | |
| 254 | result.add(TYPE_INSTRUCTION(Opcode.STRUCT_NEW, _registry.reference_cell_type_index)) | |
| 255 | result.add(INDEX_INSTRUCTION(Opcode.LOCAL_SET, index)) | |
| 256 | od | |
| 257 | ||
| 258 | result.add_range(body) | |
| 259 | ||
| 260 | return result | |
| 261 | si | |
| 262 | ||
| 263 | /// Lowers one value into the instructions being built. An absent | |
| 264 | /// value lowers to nothing. | |
| 265 | gen(value: Value?) is | |
| 266 | if value? then | |
| 267 | value.accept(self) | |
| 268 | ||
| 269 | _top_value = value | |
| 270 | _top_cast = null | |
| 271 | ||
| 272 | _unbox_narrowed(value) | |
| 273 | fi | |
| 274 | si | |
| 275 | ||
| 276 | // A load flow analysis has narrowed from a reference to a scalar or | |
| 277 | // an enum still reads the reference, which holds the value in a | |
| 278 | // box: the value is read out of the box. | |
| 279 | _unbox_narrowed(value: Value) is | |
| 280 | if _registry.discovering \/ !isa Values.Load.SYMBOL(value) then | |
| 281 | return | |
| 282 | fi | |
| 283 | ||
| 284 | // Only a reference held as the root can hold a box: a cell | |
| 285 | // holding a local is read through by the load itself. | |
| 286 | if let index = _registry.box_index(_specialize(value.type)), heap_type = _top?.heap_type() /\ heap_type.matches(_registry.object_reference.heap_type()!) then | |
| 287 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST, HEAP_TYPE.of_type_index(index))) | |
| 288 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, index, WASMGC_TYPE_REGISTRY.BOX_VALUE)) | |
| 289 | ||
| 290 | _top_cast = _value_type(value.type) | |
| 291 | fi | |
| 292 | si | |
| 293 | ||
| 294 | /// Lowers the address of `value`, which for a struct is the box the | |
| 295 | /// value lives in. | |
| 296 | gen_address(value: Value) is | |
| 297 | value.accept_address(self) | |
| 298 | ||
| 299 | _top_value = value | |
| 300 | _top_cast = null | |
| 301 | si | |
| 302 | ||
| 303 | // The struct a value of `type` is a box of, or null. | |
| 304 | _struct_of(type: Type?) -> CLASS_INSTANCE? => _registry.struct_of(_specialize(type)) | |
| 305 | ||
| 306 | // The value `value` boxes, seen through casts, where it is of a value | |
| 307 | // type that is not optional at this instantiation, and so the box can | |
| 308 | // never be absent. Null otherwise. | |
| 309 | _boxed_present_value(value: Value) -> Value? is | |
| 310 | let inner mut = value | |
| 311 | ||
| 312 | while true do | |
| 313 | if let checked: Values.CAST = inner then | |
| 314 | inner = checked.value | |
| 315 | else | |
| 316 | break | |
| 317 | fi | |
| 318 | od | |
| 319 | ||
| 320 | if let box: Values.BOX = inner, type = _specialize(box.value.type) /\ type.is_value_type /\ !type.is_optional then | |
| 321 | return box.value | |
| 322 | fi | |
| 323 | ||
| 324 | return null | |
| 325 | si | |
| 326 | ||
| 327 | ||
| 328 | // The struct a value-type optional `type` holds, or null. | |
| 329 | _optional_struct_of(type: Type?) -> CLASS_INSTANCE? is | |
| 330 | let specialized = _specialize(type) | |
| 331 | ||
| 332 | if !specialized? \/ !specialized.is_optional \/ !specialized.is_value_type \/ specialized.is_maybe then | |
| 333 | return null | |
| 334 | fi | |
| 335 | ||
| 336 | return _registry.struct_of(specialized.optional_inner_type) | |
| 337 | si | |
| 338 | // Whether a value of `type` is a struct whose box is changed in | |
| 339 | // place, so that a value read from somewhere is a copy of the box. | |
| 340 | _is_mutable_struct(type: Type?) -> bool => | |
| 341 | if let held = _struct_of(type) then !_registry.is_read_only(held) else false fi | |
| 342 | ||
| 343 | // A read of a mutable struct, left on the stack, becomes a copy of | |
| 344 | // its box, so that changing the copy leaves the original alone. | |
| 345 | // Each struct-typed field of the box is copied in turn. | |
| 346 | _copy_if_mutable(type: Type?) is | |
| 347 | if !_is_mutable_struct(type) \/ _registry.discovering then | |
| 348 | return | |
| 349 | fi | |
| 350 | ||
| 351 | _copy_struct(_struct_of(type)!) | |
| 352 | si | |
| 353 | ||
| 354 | // As _copy_if_mutable, for a value that may be absent: null stays | |
| 355 | // null. | |
| 356 | _copy_if_mutable_or_null(held: CLASS_INSTANCE) is | |
| 357 | if !_registry.is_struct(held) \/ _registry.is_read_only(held) \/ _registry.discovering then | |
| 358 | return | |
| 359 | fi | |
| 360 | ||
| 361 | let index = _registry.type_index(held)! | |
| 362 | let value_type = VALUE_TYPE.of_type_index(index, true) | |
| 363 | let source = _temp(value_type) | |
| 364 | ||
| 365 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, source)) | |
| 366 | _emit(Opcode.REF_IS_NULL) | |
| 367 | ||
| 368 | _begin() | |
| 369 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, source)) | |
| 370 | _copy_struct(held) | |
| 371 | let present = _end() | |
| 372 | ||
| 373 | _emit(IF_INSTRUCTION( | |
| 374 | BLOCK_TYPE.of_value(value_type), | |
| 375 | [HEAP_TYPE_INSTRUCTION(Opcode.REF_NULL, HEAP_TYPE.of_type_index(index))]: INSTRUCTION, | |
| 376 | present)) | |
| 377 | si | |
| 378 | ||
| 379 | _copy_struct(held: CLASS_INSTANCE) is | |
| 380 | let index = _registry.type_index(held)! | |
| 381 | let source = _temp(VALUE_TYPE.of_type_index(index, true)) | |
| 382 | let field_types = _registry.field_types(held) | |
| 383 | let count = _registry.field_value_types(held).count | |
| 384 | ||
| 385 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, source)) | |
| 386 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.descriptor_global(held))) | |
| 387 | ||
| 388 | for i in 1..count do | |
| 389 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, source)) | |
| 390 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, index, i)) | |
| 391 | ||
| 392 | if i < field_types.count then | |
| 393 | _copy_if_mutable(field_types[i]) | |
| 394 | fi | |
| 395 | od | |
| 396 | ||
| 397 | _emit(TYPE_INSTRUCTION(Opcode.STRUCT_NEW, index)) | |
| 398 | si | |
| 399 | ||
| 400 | // The receiver of a field access or call on a struct is its box, | |
| 401 | // read in place where the value has an address. | |
| 402 | _struct_receiver(from: Value) is | |
| 403 | if from.has_address /\ _struct_of(from.type)? then | |
| 404 | gen_address(from) | |
| 405 | elif isa Values.Call.INSTANCE(from) then | |
| 406 | _element_in_place = true | |
| 407 | gen(from) | |
| 408 | _element_in_place = false | |
| 409 | else | |
| 410 | gen(from) | |
| 411 | fi | |
| 412 | si | |
| 413 | ||
| 414 | // The wasm type `value` leaves on the stack. | |
| 415 | _held_type(value: Value) -> VALUE_TYPE? is | |
| 416 | if let wrapper: Values.TYPE_WRAPPER = value then | |
| 417 | return _held_type(wrapper.value) | |
| 418 | elif let block: Values.BLOCK = value /\ block.values.count > 0 /\ !isa Values.REGION(block.values[block.values.count - 1]) then | |
| 419 | // A block leaves its last value. | |
| 420 | return _held_type(block.values[block.values.count - 1]) | |
| 421 | elif let wrapper: Values.WRAPPER = value /\ wrapper.value? then | |
| 422 | return _held_type(wrapper.value) | |
| 423 | elif let view: Values.NARROW_VIEW = value then | |
| 424 | return _held_type(view.underlying) | |
| 425 | elif let projection: Values.NARROW_PROJECT = value then | |
| 426 | return _held_type(projection.projected) | |
| 427 | elif let conversion: Values.CAST = value /\ _registry.box_index(_specialize(conversion.type))? then | |
| 428 | return VALUE_TYPE.of_type_index(_registry.box_index(_specialize(conversion.type))!, true) | |
| 429 | elif let box: Values.BOX = value then | |
| 430 | if let index = _registry.box_index(_specialize(box.value.type)) then | |
| 431 | return VALUE_TYPE.of_type_index(index, true) | |
| 432 | fi | |
| 433 | ||
| 434 | return _held_type(box.value) | |
| 435 | elif let load: Values.Load.TEMP = value then | |
| 436 | return _local_type(_local(load.local)) | |
| 437 | elif let local: WASMGC_LOCAL_VALUE = value then | |
| 438 | return _local_type(local.index) | |
| 439 | elif let load: Values.Load.LOCAL_VARIABLE = value /\ Values.state_machine_field_for(load.symbol)? then | |
| 440 | return _held_type(_frame_field_load(Values.state_machine_field_for(load.symbol)!)) | |
| 441 | elif let load: Values.Load.LOCAL_ARGUMENT = value /\ Values.state_machine_field_for(load.symbol)? then | |
| 442 | return _held_type(_frame_field_load(Values.state_machine_field_for(load.symbol)!)) | |
| 443 | elif let load: Values.Load.LOCAL_VARIABLE = value then | |
| 444 | if let variable = cast Semantic.Symbols.Variable?(load.symbol) then | |
| 445 | // A local held in a cell is read out of it, as its value. | |
| 446 | if _cell_index(variable.ir_local)? then | |
| 447 | return _value_type(variable.ir_local.type) | |
| 448 | elif let held = _reference_cell_of(variable.ir_local) then | |
| 449 | return held | |
| 450 | fi | |
| 451 | ||
| 452 | return _local_type(_local(variable.ir_local)) | |
| 453 | fi | |
| 454 | elif let load: Values.Load.LOCAL_ARGUMENT = value then | |
| 455 | if let index = _argument_index(load.symbol) then | |
| 456 | return _local_type(index) | |
| 457 | fi | |
| 458 | elif let load: Values.Load.ARGUMENT = value then | |
| 459 | return _local_type(load.index + _receiver_offset) | |
| 460 | elif isa Values.Load.RECEIVER(value) \/ isa Values.Load.REFERENCE_SELF(value) then | |
| 461 | return _local_type(0) | |
| 462 | elif let load: Values.Load.INSTANCE_FIELD = value then | |
| 463 | if let owner = _owner_class(load.symbol.owner) then | |
| 464 | return _registry.field_of(load.symbol, owner)?.value_type | |
| 465 | fi | |
| 466 | elif let load: Values.Load.STATIC_FIELD = value then | |
| 467 | return _global_of(load.symbol)?.type | |
| 468 | elif let load: Values.Load.GLOBAL_FIELD = value then | |
| 469 | return _global_of(load.symbol)?.type | |
| 470 | elif let call: Values.Call.INSTANCE = value then | |
| 471 | return _call_result(call.function, value) | |
| 472 | elif let call: Values.Call.STATIC = value then | |
| 473 | return _call_result(call.function, value) | |
| 474 | elif let call: Values.Call.GLOBAL = value then | |
| 475 | return _call_result(call.function, value) | |
| 476 | fi | |
| 477 | ||
| 478 | return _value_type(value.type) | |
| 479 | si | |
| 480 | ||
| 481 | // What a call to `function` leaves: the type the function is | |
| 482 | // declared to return, which the call's own type may have been | |
| 483 | // narrowed past where flow analysis proved more about the result. | |
| 484 | _call_result(function: Semantic.Symbols.Function, call: Value) -> VALUE_TYPE? => | |
| 485 | _value_type(function.return_type) ?? _value_type(call.type) | |
| 486 | ||
| 487 | // The declared type of parameter or local `index`. | |
| 488 | _local_type(index: int) -> VALUE_TYPE? => | |
| 489 | if index < _parameter_count then | |
| 490 | _parameter_types[index] | |
| 491 | elif index - _parameter_count < _local_types.count then | |
| 492 | _local_types[index - _parameter_count] | |
| 493 | else | |
| 494 | null | |
| 495 | fi | |
| 496 | ||
| 497 | // Casts the value just lowered to `expected` where it is held as a | |
| 498 | // wider reference type, which a load of a variable flow analysis | |
| 499 | // has narrowed is. | |
| 500 | _coerce_top(expected: VALUE_TYPE?) is | |
| 501 | let held = _top | |
| 502 | ||
| 503 | if !expected? \/ !held? then | |
| 504 | return | |
| 505 | fi | |
| 506 | ||
| 507 | if let expected_heap = expected.heap_type() then | |
| 508 | if let held_heap = held.heap_type() then | |
| 509 | if _registry.is_held_as(held_heap, expected_heap) then | |
| 510 | return | |
| 511 | fi | |
| 512 | ||
| 513 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST_NULL, expected_heap)) | |
| 514 | _top_cast = expected | |
| 515 | fi | |
| 516 | fi | |
| 517 | si | |
| 518 | ||
| 519 | _begin() is | |
| 520 | _bodies.add(LIST[INSTRUCTION]()) | |
| 521 | si | |
| 522 | ||
| 523 | _end() -> LIST[INSTRUCTION] is | |
| 524 | let body = _bodies[_bodies.count - 1] | |
| 525 | ||
| 526 | _bodies.remove_at(_bodies.count - 1) | |
| 527 | ||
| 528 | return body | |
| 529 | si | |
| 530 | ||
| 531 | _emit(instruction: INSTRUCTION) is | |
| 532 | if let position = _pending_position then | |
| 533 | source_lines?.note_position(instruction, position) | |
| 534 | ||
| 535 | _pending_position = null | |
| 536 | fi | |
| 537 | ||
| 538 | _bodies[_bodies.count - 1].add(instruction) | |
| 539 | si | |
| 540 | ||
| 541 | // Records where `value` is, as the position of the first | |
| 542 | // instruction lowered after it, where positions are wanted and the | |
| 543 | // line differs from the last one recorded. | |
| 544 | _mark(value: Value) is | |
| 545 | if source_lines? /\ value.has_location /\ value.location.start_line != _last_line then | |
| 546 | _pending_position = value.location | |
| 547 | _last_line = value.location.start_line | |
| 548 | fi | |
| 549 | si | |
| 550 | ||
| 551 | _emit(opcode: Opcode) is | |
| 552 | _emit(SIMPLE_INSTRUCTION(opcode)) | |
| 553 | si | |
| 554 | ||
| 555 | _emit_all(instructions: Collections.Iterable[INSTRUCTION]) is | |
| 556 | for instruction in instructions do | |
| 557 | _emit(instruction) | |
| 558 | od | |
| 559 | si | |
| 560 | ||
| 561 | _report(construct: string) is | |
| 562 | for reported in _unsupported do | |
| 563 | if reported =~ construct then | |
| 564 | return | |
| 565 | fi | |
| 566 | od | |
| 567 | ||
| 568 | _unsupported.add(construct) | |
| 569 | si | |
| 570 | ||
| 571 | // The wasm type a value of `type` is held in, or null where it | |
| 572 | // is not a scalar the target can hold yet. | |
| 573 | _value_type(type: Type?) -> VALUE_TYPE? => _registry.value_type(_specialize(type)) | |
| 574 | ||
| 575 | // `type` with the type parameters of the body being compiled | |
| 576 | // replaced by the arguments of this instantiation. | |
| 577 | _specialize(type: Type?) -> Type? => | |
| 578 | if _instance? then TYPE_MAPS.specialize(type, _body_type_map) else type fi | |
| 579 | ||
| 580 | _type_map: Collections.Map[Semantic.Symbols.Symbol, Type] => _body_type_map | |
| 581 | ||
| 582 | // The index of the instantiation of `function` a reference from | |
| 583 | // this body names, numbered the first time it is asked for. | |
| 584 | _index_of(function: Function) -> int? => | |
| 585 | _numbering.index_of(_instance_of(function)) | |
| 586 | ||
| 587 | // The instantiation of `function` a reference from this body names. | |
| 588 | _instance_of(function: Function) -> FUNCTION_INSTANCE => | |
| 589 | FUNCTION_INSTANCE( | |
| 590 | cast Function?(function.root_specialized_from)!, | |
| 591 | TYPE_MAPS.function_map(function, _type_map)) | |
| 592 | ||
| 593 | // The class a member's owner is, at the arguments this body gives | |
| 594 | // it: a constructed generic names them itself, and a reference from | |
| 595 | // inside a generic class to its own members takes the body's. | |
| 596 | _owner_class(owner: Semantic.Scope?) -> CLASS_INSTANCE? is | |
| 597 | if let generic: Semantic.Symbols.GENERIC = owner then | |
| 598 | return _registry.class_of(_specialize(generic.type)) | |
| 599 | elif let classy: Semantic.Symbols.Classy = owner then | |
| 600 | return _registry.class_of(classy, _type_map) | |
| 601 | fi | |
| 602 | ||
| 603 | return null | |
| 604 | si | |
| 605 | ||
| 606 | // The global holding a static field or a namespace-level variable. | |
| 607 | _global_of(symbol: Semantic.Symbols.Symbol) -> (index: int, type: VALUE_TYPE)? => | |
| 608 | _registry.global_of(symbol, _owner_class(symbol.owner)) | |
| 609 | ||
| 610 | // The instantiation of the trait a member's owner is, at the | |
| 611 | // arguments this body gives it. | |
| 612 | _trait_instance(owner: Semantic.Scope?) -> CLASS_INSTANCE? is | |
| 613 | if let generic: Semantic.Symbols.GENERIC = owner then | |
| 614 | return _registry.dispatch.trait_of(_specialize(generic.type)!) | |
| 615 | elif let classy: Semantic.Symbols.Classy = owner then | |
| 616 | return _registry.dispatch.trait_of(classy, _type_map) | |
| 617 | fi | |
| 618 | ||
| 619 | return null | |
| 620 | si | |
| 621 | ||
| 622 | _numeric(type: Type?) -> NumericType? => WASMGC_TYPES.numeric_of(_specialize(type)) | |
| 623 | ||
| 624 | // Brings a value stored into a slot of a narrow kind back into | |
| 625 | // range, as a CIL store into a narrow local or argument does. | |
| 626 | _normalise_for(type: Type?) is | |
| 627 | if _convert_tuple_top(type) then | |
| 628 | return | |
| 629 | fi | |
| 630 | ||
| 631 | _coerce_top(_value_type(type)) | |
| 632 | ||
| 633 | if let scalar = WASMGC_TYPES.scalar(_specialize(type)) then | |
| 634 | if WASMGC_TYPES.is_narrow(scalar) then | |
| 635 | _emit_all(WASMGC_OPERATIONS.normalise(scalar)) | |
| 636 | fi | |
| 637 | fi | |
| 638 | si | |
| 639 | ||
| 640 | ||
| 641 | // A tuple held as one tuple struct goes where another is expected | |
| 642 | // by being rebuilt from its elements, each converted to the | |
| 643 | // element type the other holds, as .NET rebuilds a tuple at a | |
| 644 | // wider type: two tuple structs are unrelated types, so a cast | |
| 645 | // between them fails. False where the value just lowered is not | |
| 646 | // such a tuple. | |
| 647 | _convert_tuple_top(target: Type?) -> bool is | |
| 648 | let value = _top_value | |
| 649 | ||
| 650 | if !value? \/ _registry.discovering then | |
| 651 | return false | |
| 652 | fi | |
| 653 | ||
| 654 | let from = _struct_of(value.type) | |
| 655 | let to = _struct_of(target) | |
| 656 | ||
| 657 | if !from? \/ !to? \/ from =~ to \/ !_registry.is_read_only(from) \/ !_registry.is_read_only(to) then | |
| 658 | return false | |
| 659 | fi | |
| 660 | ||
| 661 | let from_types = _registry.field_value_types(from) | |
| 662 | let to_types = _registry.field_value_types(to) | |
| 663 | ||
| 664 | if from_types.count != to_types.count then | |
| 665 | return false | |
| 666 | fi | |
| 667 | ||
| 668 | let from_index = _registry.type_index(from)! | |
| 669 | let to_index = _registry.type_index(to)! | |
| 670 | let source = _temp(VALUE_TYPE.of_type_index(from_index, true)) | |
| 671 | ||
| 672 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, source)) | |
| 673 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.descriptor_global(to))) | |
| 674 | ||
| 675 | for i in 1..from_types.count do | |
| 676 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, source)) | |
| 677 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, from_index, i)) | |
| 678 | ||
| 679 | if let to_heap = to_types[i].heap_type(), from_heap = from_types[i].heap_type() /\ !_registry.is_held_as(from_heap, to_heap) then | |
| 680 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST_NULL, to_heap)) | |
| 681 | fi | |
| 682 | od | |
| 683 | ||
| 684 | _emit(TYPE_INSTRUCTION(Opcode.STRUCT_NEW, to_index)) | |
| 685 | ||
| 686 | _top_cast = VALUE_TYPE.of_type_index(to_index, true) | |
| 687 | ||
| 688 | return true | |
| 689 | si | |
| 690 | // The index of `local`, numbered after the parameters the first | |
| 691 | // time it is seen. | |
| 692 | _local(local: LOCAL) -> int is | |
| 693 | let index mut = 0 | |
| 694 | ||
| 695 | if _locals.try_get_value(local, index ref) then | |
| 696 | return index | |
| 697 | fi | |
| 698 | ||
| 699 | index = _parameter_count + _local_types.count | |
| 700 | ||
| 701 | _locals[local] = index | |
| 702 | ||
| 703 | if source_lines? then | |
| 704 | _local_names.add((index, local.name)) | |
| 705 | fi | |
| 706 | ||
| 707 | if let cell = _cell_index(local) then | |
| 708 | _local_types.add(VALUE_TYPE.of_type_index(cell, true)) | |
| 709 | _struct_locals.add((index, _registry.box_type(_specialize(local.type))!)) | |
| 710 | elif _reference_cell_of(local)? then | |
| 711 | _local_types.add(VALUE_TYPE.of_type_index(_registry.reference_cell_type_index, true)) | |
| 712 | _reference_cell_locals.add(index) | |
| 713 | elif let type = _value_type(local.type) then | |
| 714 | _local_types.add(type) | |
| 715 | ||
| 716 | if let held = _struct_of(local.type) then | |
| 717 | _struct_locals.add((index, held)) | |
| 718 | fi | |
| 719 | else | |
| 720 | _report("a local of type {local.type}") | |
| 721 | ||
| 722 | _local_types.add(VALUE_TYPE.of_numeric(NumericType.I32)) | |
| 723 | fi | |
| 724 | ||
| 725 | return index | |
| 726 | si | |
| 727 | ||
| 728 | // The zero of `type`: a default box for a struct, the zero or null | |
| 729 | // of the wasm type otherwise. The null type has no wasm type of its | |
| 730 | // own, and its null is the bottom of the reference types, which | |
| 731 | // any struct or array slot accepts. | |
| 732 | _zero(type: Type?) is | |
| 733 | if let held = _struct_of(type) then | |
| 734 | _emit_all(_registry.new_instance(held)) | |
| 735 | elif let value_type = _value_type(type) then | |
| 736 | _emit(WASMGC_TYPE_REGISTRY.default_value(value_type)) | |
| 737 | elif type?.is_null ?? false then | |
| 738 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_NULL, HEAP_TYPE.of_abstract(AbstractHeapType.NONE))) | |
| 739 | else | |
| 740 | _report("the default value of {type}") | |
| 741 | fi | |
| 742 | si | |
| 743 | ||
| 744 | // A local the lowering needs for itself, holding a value of | |
| 745 | // `value_type`. | |
| 746 | _temp(value_type: VALUE_TYPE) -> int is | |
| 747 | let index = _parameter_count + _local_types.count | |
| 748 | ||
| 749 | _local_types.add(value_type) | |
| 750 | ||
| 751 | return index | |
| 752 | si | |
| 753 | ||
| 754 | // The parameter `symbol` is passed in: its position among the | |
| 755 | // arguments of the function that owns it, after the receiver where | |
| 756 | // there is one. | |
| 757 | _argument_index(symbol: Semantic.Symbols.Symbol) -> int? is | |
| 758 | if let function = cast Function?(symbol.owner) then | |
| 759 | let names = function.argument_names | |
| 760 | ||
| 761 | for i in 0..names.count do | |
| 762 | if names[i] =~ symbol.name then | |
| 763 | return i + _receiver_offset | |
| 764 | fi | |
| 765 | od | |
| 766 | fi | |
| 767 | ||
| 768 | return null | |
| 769 | si | |
| 770 | ||
| 771 | _lower_all(values: Collections.Iterable[Value]) is | |
| 772 | for value in values do | |
| 773 | gen(value) | |
| 774 | od | |
| 775 | si | |
| 776 | ||
| 777 | // The arguments of a call, each brought into the range of the | |
| 778 | // parameter it is passed to. | |
| 779 | _lower_arguments(function: Function, arguments: Collections.List[Value]) is | |
| 780 | let parameters = function.arguments | |
| 781 | ||
| 782 | for i in 0..arguments.count do | |
| 783 | gen(arguments[i]) | |
| 784 | ||
| 785 | if i < parameters.count then | |
| 786 | _normalise_for(parameters[i]) | |
| 787 | fi | |
| 788 | od | |
| 789 | si | |
| 790 | ||
| 791 | _call(function: Function, arguments: Collections.List[Value]) is | |
| 792 | if WASMGC_IMPORTS.is_host(function.intrinsic_operation) then | |
| 793 | _call_host(function.intrinsic_operation!, arguments, function.return_type) | |
| 794 | return | |
| 795 | elif _lower_representation(function.intrinsic_operation, arguments) then | |
| 796 | return | |
| 797 | elif let opcode = WASMGC_OPERATIONS.reinterpretation(function.intrinsic_operation) then | |
| 798 | gen(arguments[0]) | |
| 799 | _emit(opcode) | |
| 800 | return | |
| 801 | elif function.intrinsic_operation =~ "array.new" then | |
| 802 | _new_array(_instance_of(function).specialize(cast Function?(function.root_specialized_from)!.return_type), arguments[0]) | |
| 803 | return | |
| 804 | elif function.intrinsic_operation =~ "array.set" then | |
| 805 | _store_array_element(arguments[0], arguments[1], arguments[2]) | |
| 806 | return | |
| 807 | elif function.intrinsic_operation =~ "type.size_of" then | |
| 808 | _emit(I32_CONSTANT(_size_of(function.generic_arguments[0]))) | |
| 809 | return | |
| 810 | fi | |
| 811 | ||
| 812 | let index_found = _index_of(function) | |
| 813 | ||
| 814 | if !index_found? then | |
| 815 | _report_uncompiled_call(_instance_of(function)) | |
| 816 | return | |
| 817 | fi | |
| 818 | ||
| 819 | let index = index_found | |
| 820 | ||
| 821 | _lower_arguments(function, arguments) | |
| 822 | ||
| 823 | _emit(INDEX_INSTRUCTION(Opcode.CALL, index)) | |
| 824 | si | |
| 825 | ||
| 826 | visit_value(value: Value) is | |
| 827 | if let local: WASMGC_LOCAL_VALUE = value then | |
| 828 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, local.index)) | |
| 829 | return | |
| 830 | fi | |
| 831 | ||
| 832 | _report("{value.get_type().name}") | |
| 833 | si | |
| 834 | ||
| 835 | visit_address_of_value(value: Value) is | |
| 836 | _report("the address of {value.get_type().name}") | |
| 837 | si | |
| 838 | ||
| 839 | visit(number: Values.Literal.NUMBER) is | |
| 840 | case number.constant | |
| 841 | when i4: Values.Literal.CONSTANT.I4 then _emit(I32_CONSTANT(i4.value)) | |
| 842 | when i8: Values.Literal.CONSTANT.I8 then _emit(I64_CONSTANT(i8.value)) | |
| 843 | when r4: Values.Literal.CONSTANT.R4 then _emit(F32_CONSTANT(r4.value)) | |
| 844 | when r8: Values.Literal.CONSTANT.R8 then _emit(F64_CONSTANT(r8.value)) | |
| 845 | esac | |
| 846 | ||
| 847 | if let to = number.narrowing then | |
| 848 | let from = | |
| 849 | case number.constant | |
| 850 | when _: Values.Literal.CONSTANT.I4 then Scalar.INT32 | |
| 851 | when _: Values.Literal.CONSTANT.I8 then Scalar.INT64 | |
| 852 | when _: Values.Literal.CONSTANT.R4 then Scalar.FLOAT32 | |
| 853 | when _: Values.Literal.CONSTANT.R8 then Scalar.FLOAT64 | |
| 854 | esac | |
| 855 | ||
| 856 | _emit_all(WASMGC_OPERATIONS.conversion(from, to)) | |
| 857 | fi | |
| 858 | si | |
| 859 | ||
| 860 | visit(string: Values.Literal.STRING) is | |
| 861 | if let strings = _strings then | |
| 862 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, strings.literal(string.value))) | |
| 863 | else | |
| 864 | _report("a string literal") | |
| 865 | fi | |
| 866 | si | |
| 867 | visit(decimal: Values.Literal.DECIMAL) is _report("a decimal literal") si | |
| 868 | // A bigint literal that fits in a long is built into the box's i64 | |
| 869 | // field. A wider one is built at run time from its decimal digits, | |
| 870 | // through the static `parse` the core library gives `bigint`. | |
| 871 | visit(bigint: Values.Literal.BIGINT) is | |
| 872 | let small: long mut | |
| 873 | ||
| 874 | if long.try_parse(bigint.value, small ref) then | |
| 875 | let long_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_long_type() | |
| 876 | ||
| 877 | _bigint_of_small(bigint.type, Values.Literal.NUMBER(Values.Literal.CONSTANT.I8(small), long_type)) | |
| 878 | elif let parse = _static_method(bigint.type, "parse", 1) then | |
| 879 | let string_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_string_type() | |
| 880 | ||
| 881 | gen(Values.Call.STATIC(parse, LIST[Value]([Values.Literal.STRING(bigint.value, string_type)]), bigint.type, null)) | |
| 882 | else | |
| 883 | _report("a bigint literal, whose type has no parse") | |
| 884 | fi | |
| 885 | si | |
| 886 | ||
| 887 | visit(load: Values.Load.SYMBOL) is _report("a load of {load.symbol.name}") si | |
| 888 | visit_address(load: Values.Load.SYMBOL) is _report("the address of {load.symbol.name}") si | |
| 889 | visit(load: Values.Load.INSTANCE_FIELD) is | |
| 890 | _field_get(load) | |
| 891 | _copy_if_mutable(load.type) | |
| 892 | si | |
| 893 | ||
| 894 | _field_get(load: Values.Load.INSTANCE_FIELD) is | |
| 895 | if let `field = _instance_field(load.from, load.symbol) then | |
| 896 | _struct_receiver(load.from!) | |
| 897 | _coerce_top(VALUE_TYPE.of_type_index(`field.type_index, true)) | |
| 898 | ||
| 899 | if _is_null_checked(load.from) then | |
| 900 | _check_not_null(VALUE_TYPE.of_type_index(`field.type_index, true)) | |
| 901 | fi | |
| 902 | ||
| 903 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, `field.type_index, `field.field_index)) | |
| 904 | fi | |
| 905 | si | |
| 906 | ||
| 907 | // Where an instance field lives, or null, reported, where the | |
| 908 | // object model does not hold it. | |
| 909 | _instance_field(from: Value?, symbol: Semantic.Symbols.Symbol) -> WASMGC_FIELD? is | |
| 910 | if !from? \/ (from.is_value_type /\ !_struct_of(from.type)?) then | |
| 911 | _report("a field of a value type") | |
| 912 | return null | |
| 913 | fi | |
| 914 | ||
| 915 | if let owner = _owner_class(symbol.owner) then | |
| 916 | if let `field = _registry.field_of(symbol, owner) then | |
| 917 | return `field | |
| 918 | fi | |
| 919 | fi | |
| 920 | ||
| 921 | _report("a field of {symbol.owner}") | |
| 922 | ||
| 923 | return null | |
| 924 | si | |
| 925 | visit_address(load: Values.Load.INSTANCE_FIELD) is | |
| 926 | if _struct_of(load.type)? then | |
| 927 | _field_get(load) | |
| 928 | else | |
| 929 | _report("a reference to a field or array element") | |
| 930 | fi | |
| 931 | si | |
| 932 | // A literal that captures nothing but its enclosing body's type | |
| 933 | // parameters is a function value with no receiver. | |
| 934 | visit(load: Values.Load.STATIC_ANONYMOUS_FUNCTION) is _anonymous_function(load.symbol, load.func_type) si | |
| 935 | visit(load: Values.Load.GLOBAL_ANONYMOUS_FUNCTION) is _anonymous_function(load.symbol, load.func_type) si | |
| 936 | ||
| 937 | _anonymous_function(symbol: Semantic.Symbols.Symbol, type: Type) is | |
| 938 | if let function: Semantic.Symbols.Function = symbol then | |
| 939 | gen(Values.MAKE_CLOSURE(type, function, Values.NULL(Semantic.Types.NULL()))) | |
| 940 | else | |
| 941 | _report("a function value") | |
| 942 | fi | |
| 943 | si | |
| 944 | ||
| 945 | visit(load: Values.Load.INSTANCE_ANONYMOUS_FUNCTION) is _report("a function value") si | |
| 946 | ||
| 947 | visit(load: Values.Load.TEMP) is | |
| 948 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, _local(load.local))) | |
| 949 | _copy_if_mutable(load.type) | |
| 950 | si | |
| 951 | ||
| 952 | visit_address(load: Values.Load.TEMP) is | |
| 953 | if _struct_of(load.type)? then | |
| 954 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, _local(load.local))) | |
| 955 | else | |
| 956 | _report("the address of a local") | |
| 957 | fi | |
| 958 | si | |
| 959 | visit(load: Values.Load.STATIC_FIELD) is | |
| 960 | _global_get(load.symbol) | |
| 961 | _copy_if_mutable(load.type) | |
| 962 | si | |
| 963 | ||
| 964 | _global_get(symbol: Semantic.Symbols.Symbol) is | |
| 965 | if let (index, _) = _global_of(symbol) then | |
| 966 | _ensure_initialised(symbol.owner) | |
| 967 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, index)) | |
| 968 | else | |
| 969 | _report("a static field of {symbol.owner}") | |
| 970 | fi | |
| 971 | si | |
| 972 | ||
| 973 | _global_set(symbol: Semantic.Symbols.Symbol, value: Value) is | |
| 974 | if let (index, _) = _global_of(symbol) then | |
| 975 | _ensure_initialised(symbol.owner) | |
| 976 | gen(value) | |
| 977 | ||
| 978 | _normalise_for(cast Semantic.Types.Typed?(symbol)?.type) | |
| 979 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_SET, index)) | |
| 980 | else | |
| 981 | _report("a static field of {symbol.owner}") | |
| 982 | fi | |
| 983 | si | |
| 984 | visit_address(load: Values.Load.STATIC_FIELD) is | |
| 985 | if _struct_of(load.type)? then | |
| 986 | _global_get(load.symbol) | |
| 987 | else | |
| 988 | _report("the address of a static field") | |
| 989 | fi | |
| 990 | si | |
| 991 | visit(load: Values.Load.GLOBAL_FIELD) is | |
| 992 | _global_get(load.symbol) | |
| 993 | _copy_if_mutable(load.type) | |
| 994 | si | |
| 995 | visit_address(load: Values.Load.GLOBAL_FIELD) is | |
| 996 | if _struct_of(load.type)? then | |
| 997 | _global_get(load.symbol) | |
| 998 | else | |
| 999 | _report("the address of a global variable") | |
| 1000 | fi | |
| 1001 | si | |
| 1002 | ||
| 1003 | visit(load: Values.Load.LOCAL_ARGUMENT) is | |
| 1004 | _argument_get(load) | |
| 1005 | _copy_if_mutable(load.type) | |
| 1006 | si | |
| 1007 | ||
| 1008 | _argument_get(load: Values.Load.LOCAL_ARGUMENT) is | |
| 1009 | let symbol = load.symbol | |
| 1010 | ||
| 1011 | if let state_machine_field = Values.state_machine_field_for(symbol) then | |
| 1012 | gen(_frame_field_load(state_machine_field)) | |
| 1013 | elif let packed_local = Values.packed_parameter_local_for(symbol) then | |
| 1014 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, _local(packed_local))) | |
| 1015 | elif let index = _argument_index(symbol) then | |
| 1016 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, index)) | |
| 1017 | else | |
| 1018 | _report("an argument of another function") | |
| 1019 | fi | |
| 1020 | si | |
| 1021 | ||
| 1022 | visit_address(load: Values.Load.LOCAL_ARGUMENT) is | |
| 1023 | if _struct_of(load.type)? then | |
| 1024 | _argument_get(load) | |
| 1025 | else | |
| 1026 | _report("the address of an argument") | |
| 1027 | fi | |
| 1028 | si | |
| 1029 | ||
| 1030 | // A generator's arguments and locals live in fields of its frame, | |
| 1031 | // which is the receiver of the frame's methods. | |
| 1032 | _frame_field_load(`field: Semantic.Symbols.Field) -> Value => | |
| 1033 | Values.Load.INSTANCE_FIELD(Values.Load.REFERENCE_SELF(cast Semantic.Symbols.Classy(`field.owner)), `field) | |
| 1034 | ||
| 1035 | _frame_field_store(`field: Semantic.Symbols.Field, value: Value) -> Value => | |
| 1036 | Values.Store.INSTANCE_FIELD(Values.Load.REFERENCE_SELF(cast Semantic.Symbols.Classy(`field.owner)), `field, value) | |
| 1037 | ||
| 1038 | visit(load: Values.Load.LOCAL_VARIABLE) is | |
| 1039 | _variable_get(load) | |
| 1040 | _copy_if_mutable(load.type) | |
| 1041 | si | |
| 1042 | ||
| 1043 | _variable_get(load: Values.Load.LOCAL_VARIABLE) is | |
| 1044 | if let state_machine_field = Values.state_machine_field_for(load.symbol) then | |
| 1045 | gen(_frame_field_load(state_machine_field)) | |
| 1046 | elif let variable = cast Semantic.Symbols.Variable?(load.symbol) then | |
| 1047 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, _local(variable.ir_local))) | |
| 1048 | ||
| 1049 | if let cell = _cell_index(variable.ir_local) then | |
| 1050 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, cell, WASMGC_TYPE_REGISTRY.BOX_VALUE)) | |
| 1051 | elif let held = _reference_cell_of(variable.ir_local) then | |
| 1052 | _reference_cell_get(held) | |
| 1053 | fi | |
| 1054 | else | |
| 1055 | _report("a load of {load.symbol.name}") | |
| 1056 | fi | |
| 1057 | si | |
| 1058 | ||
| 1059 | // The address of a scalar or reference-typed local is the cell it | |
| 1060 | // lives in, which the local holds in place of the value. | |
| 1061 | visit_address(load: Values.Load.LOCAL_VARIABLE) is | |
| 1062 | if let held = _struct_of(load.type) then | |
| 1063 | _variable_get(load) | |
| 1064 | ||
| 1065 | // A read-only struct's box is shared rather than copied, so | |
| 1066 | // the local may hold a box something else holds too. A write | |
| 1067 | // through the reference must change only the local, so it is | |
| 1068 | // given a box of its own first. | |
| 1069 | if _registry.is_read_only(held) /\ !_registry.discovering then | |
| 1070 | if let variable = cast Semantic.Symbols.Variable?(load.symbol) /\ !_registry.is_cell(variable.ir_local) /\ !Values.state_machine_field_for(load.symbol)? then | |
| 1071 | _copy_struct(held) | |
| 1072 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, _local(variable.ir_local))) | |
| 1073 | fi | |
| 1074 | fi | |
| 1075 | elif Values.state_machine_field_for(load.symbol)? then | |
| 1076 | _report("a generator or asynchronous function's local") | |
| 1077 | elif let variable = cast Semantic.Symbols.Variable?(load.symbol) /\ _has_cell(load.type) then | |
| 1078 | _registry.mark_cell(variable.ir_local) | |
| 1079 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, _local(variable.ir_local))) | |
| 1080 | else | |
| 1081 | _report("the address of a local") | |
| 1082 | fi | |
| 1083 | si | |
| 1084 | ||
| 1085 | // The box type of the cell `local` lives in, or null where it holds | |
| 1086 | // its value directly. | |
| 1087 | _cell_index(local: IR.LOCAL) -> int? => | |
| 1088 | if _registry.is_cell(local) then _registry.box_index(_specialize(local.type)) else null fi | |
| 1089 | ||
| 1090 | // The type the reference cell `local` lives in holds its value as, | |
| 1091 | // or null where it is not in a reference cell. | |
| 1092 | _reference_cell_of(local: IR.LOCAL) -> VALUE_TYPE? => | |
| 1093 | if _registry.is_cell(local) then _registry.reference_cell_value_type(_specialize(local.type)) else null fi | |
| 1094 | ||
| 1095 | // Whether a reference to a value of `type` is a cell: a box for a | |
| 1096 | // scalar, or a reference cell for a value of reference type. | |
| 1097 | _has_cell(type: Type?) -> bool => | |
| 1098 | _registry.box_index(_specialize(type))? \/ _registry.reference_cell_value_type(_specialize(type))? | |
| 1099 | ||
| 1100 | // Reads the value out of the reference cell on the stack, cast back | |
| 1101 | // to `held`, the type it was stored as. | |
| 1102 | _reference_cell_get(held: VALUE_TYPE) is | |
| 1103 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, _registry.reference_cell_type_index, WASMGC_TYPE_REGISTRY.REFERENCE_CELL_VALUE)) | |
| 1104 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST_NULL, held.heap_type()!)) | |
| 1105 | si | |
| 1106 | ||
| 1107 | visit(load: Values.Load.ARGUMENT) is | |
| 1108 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, load.index + _receiver_offset)) | |
| 1109 | _copy_if_mutable(load.type) | |
| 1110 | si | |
| 1111 | ||
| 1112 | visit(load: Values.Load.RECEIVER) is | |
| 1113 | _load_receiver() | |
| 1114 | si | |
| 1115 | ||
| 1116 | _load_receiver() is | |
| 1117 | if _receiver_offset == 0 then | |
| 1118 | _report("self outside an instance method") | |
| 1119 | return | |
| 1120 | fi | |
| 1121 | ||
| 1122 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, 0)) | |
| 1123 | si | |
| 1124 | ||
| 1125 | /// A composite value's structured expansion is lowered as any | |
| 1126 | /// other structured IR is. | |
| 1127 | visit(composite: Values.Composite) is | |
| 1128 | _lower_all(composite.expand().values) | |
| 1129 | si | |
| 1130 | ||
| 1131 | visit_address(coalesce: Values.COALESCE_LOAD) is _report("the address of a coalesced load") si | |
| 1132 | ||
| 1133 | // An asynchronous function's body constructs its frame, marks it not | |
| 1134 | // yet started, gives it a builder, starts it through the builder, | |
| 1135 | // and answers the builder's task, as the .NET async builder protocol | |
| 1136 | // has it. The builder's members are found as the .NET backend finds | |
| 1137 | // them, by name and argument count on the builder type the frame | |
| 1138 | // was given. | |
| 1139 | visit(launch: Values.ASYNC_METHOD_LAUNCH) is | |
| 1140 | let builder_type = launch.builder_field.type! | |
| 1141 | let local = launch.state_machine_local | |
| 1142 | let frame = Values.Load.TEMP(local, launch.state_machine_type) | |
| 1143 | ||
| 1144 | let arguments = | |
| 1145 | if let block: Values.BLOCK = launch.ctor_arg_loads then | |
| 1146 | LIST[Value](block.values) | |
| 1147 | else | |
| 1148 | LIST[Value]([launch.ctor_arg_loads]) | |
| 1149 | fi | |
| 1150 | ||
| 1151 | gen(Values.DECLARE_LOCAL(local)) | |
| 1152 | gen(Values.NEW(launch.state_machine_type, launch.constructor, arguments)) | |
| 1153 | gen(Values.STORE_TEMP(local)) | |
| 1154 | ||
| 1155 | gen(Values.Store.INSTANCE_FIELD(frame, launch.state_field, Values.Literal.NUMBER(-1, IoC.CONTAINER.instance.innate_symbol_lookup.get_int_type()))) | |
| 1156 | ||
| 1157 | if let create = _builder_member(builder_type, "create", 0) then | |
| 1158 | gen(Values.Store.INSTANCE_FIELD(frame, launch.builder_field, Values.Call.STATIC(create, LIST[Value](), builder_type, null))) | |
| 1159 | fi | |
| 1160 | ||
| 1161 | let builder = Values.Load.INSTANCE_FIELD(frame, launch.builder_field) | |
| 1162 | ||
| 1163 | if let start = _builder_generic_member(builder_type, "start", 1, LIST[Type]([launch.state_machine_type])) then | |
| 1164 | _call_builder(builder, start, LIST[Value]([frame]), LIST[bool]([true])) | |
| 1165 | fi | |
| 1166 | ||
| 1167 | _builder_task(builder, builder_type) | |
| 1168 | si | |
| 1169 | ||
| 1170 | // The end of a frame's `move_next`: the machine is finished, and the | |
| 1171 | // builder is handed the result, or the exception the handler caught, | |
| 1172 | // which the handler leaves on the stack. | |
| 1173 | visit(completion: Values.ASYNC_COMPLETION) is | |
| 1174 | let builder_type = completion.builder_field.type! | |
| 1175 | ||
| 1176 | let exception = | |
| 1177 | if let exception_type = completion.exception_type then | |
| 1178 | let local = _temp(_value_type(exception_type) ?? _registry.object_reference) | |
| 1179 | ||
| 1180 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, local)) | |
| 1181 | ||
| 1182 | local | |
| 1183 | else | |
| 1184 | null | |
| 1185 | fi | |
| 1186 | ||
| 1187 | gen(_frame_field_store(completion.state_field, Values.Literal.NUMBER(-1, IoC.CONTAINER.instance.innate_symbol_lookup.get_int_type()))) | |
| 1188 | ||
| 1189 | let builder = _frame_field_load(completion.builder_field) | |
| 1190 | ||
| 1191 | if let local = exception then | |
| 1192 | if let set_exception = _builder_member(builder_type, "set_exception", 1) then | |
| 1193 | let instance = _instance_of(set_exception) | |
| 1194 | ||
| 1195 | if let index = _numbering.index_of(instance) then | |
| 1196 | gen(builder) | |
| 1197 | _coerce_top(_local_type_of_receiver(instance)) | |
| 1198 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, local)) | |
| 1199 | _coerce_top(_value_type(instance.specialize(set_exception.arguments[0]))) | |
| 1200 | _emit(INDEX_INSTRUCTION(Opcode.CALL, index)) | |
| 1201 | else | |
| 1202 | _report_uncompiled_call(instance) | |
| 1203 | fi | |
| 1204 | fi | |
| 1205 | else | |
| 1206 | let arguments = | |
| 1207 | if let result = completion.result_field then | |
| 1208 | LIST[Value]([_frame_field_load(result)]) | |
| 1209 | else | |
| 1210 | LIST[Value]() | |
| 1211 | fi | |
| 1212 | ||
| 1213 | if let set_result = _builder_member(builder_type, "set_result", arguments.count) then | |
| 1214 | gen(Values.Call.INSTANCE(builder, set_result, arguments, null)) | |
| 1215 | fi | |
| 1216 | fi | |
| 1217 | si | |
| 1218 | ||
| 1219 | // A task-like that is complete from the start: a builder is created, | |
| 1220 | // given its result at once, and asked for its task. | |
| 1221 | visit(completed: Values.COMPLETED_TASK_LIKE) is | |
| 1222 | let builder_type = completed.builder_type | |
| 1223 | ||
| 1224 | let create = _builder_member(builder_type, "create", 0) | |
| 1225 | let set_result = _builder_member(builder_type, "set_result", 0) | |
| 1226 | ||
| 1227 | if !create? \/ !set_result? then | |
| 1228 | return | |
| 1229 | fi | |
| 1230 | ||
| 1231 | let local = IR.LOCAL(".completed_builder", builder_type) | |
| 1232 | let builder = Values.Load.TEMP(local, builder_type) | |
| 1233 | ||
| 1234 | gen(Values.DECLARE_LOCAL(local)) | |
| 1235 | gen(Values.Call.STATIC(create, LIST[Value](), builder_type, null)) | |
| 1236 | gen(Values.STORE_TEMP(local)) | |
| 1237 | ||
| 1238 | gen(Values.Call.INSTANCE(builder, set_result, LIST[Value](), null)) | |
| 1239 | ||
| 1240 | _builder_task(builder, builder_type) | |
| 1241 | si | |
| 1242 | ||
| 1243 | // An await is a region holding its steps and the resume point a | |
| 1244 | // later `move_next` re-enters at, and then the awaiter's result. | |
| 1245 | // The region is made once for each await, since every lowering of | |
| 1246 | // the body has to see the same resume point. | |
| 1247 | visit(suspend: Values.AWAIT_SUSPEND) is | |
| 1248 | gen(_await_structure(suspend)) | |
| 1249 | gen(suspend.get_result) | |
| 1250 | si | |
| 1251 | ||
| 1252 | _await_structure(suspend: Values.AWAIT_SUSPEND) -> Values.REGION is | |
| 1253 | let structure: Values.REGION mut | |
| 1254 | ||
| 1255 | if !_await_structures.try_get_value(suspend, structure ref) then | |
| 1256 | structure = suspend.structure() | |
| 1257 | _await_structures[suspend] = structure | |
| 1258 | fi | |
| 1259 | ||
| 1260 | return structure | |
| 1261 | si | |
| 1262 | ||
| 1263 | visit(step: Values.AWAIT_SUSPEND_STEP) is | |
| 1264 | let suspend = step.suspend | |
| 1265 | ||
| 1266 | case step.kind | |
| 1267 | when Values.AwaitSuspendStep.CAPTURE_AWAITER then | |
| 1268 | gen(_frame_field_store(suspend.awaiter_field, suspend.get_awaiter)) | |
| 1269 | ||
| 1270 | when Values.AwaitSuspendStep.REGISTER_CONTINUATION then | |
| 1271 | _stamp_state(suspend, suspend.state_number) | |
| 1272 | _register_continuation(suspend) | |
| 1273 | ||
| 1274 | when Values.AwaitSuspendStep.CLEAR_STATE then | |
| 1275 | _stamp_state(suspend, -1) | |
| 1276 | esac | |
| 1277 | si | |
| 1278 | ||
| 1279 | // The state goes to both the local the dispatch reads and the | |
| 1280 | // frame's field, which the next `move_next` starts from. | |
| 1281 | _stamp_state(suspend: Values.AWAIT_SUSPEND, state: int) is | |
| 1282 | gen(Values.Literal.NUMBER(state, IoC.CONTAINER.instance.innate_symbol_lookup.get_int_type())) | |
| 1283 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, _local(suspend.state_local))) | |
| 1284 | ||
| 1285 | gen(_frame_field_store(suspend.state_field, Values.Literal.NUMBER(state, IoC.CONTAINER.instance.innate_symbol_lookup.get_int_type()))) | |
| 1286 | si | |
| 1287 | ||
| 1288 | // Registers the frame's resumption with the awaiter through the | |
| 1289 | // builder's `await_on_completed`, or `await_unsafe_on_completed` for | |
| 1290 | // an awaiter implementing `ICriticalNotifyCompletion`. The awaiter | |
| 1291 | // goes by reference, as a cell holding the frame's copy, and the | |
| 1292 | // machine by reference or as the interface, as the member takes it. | |
| 1293 | _register_continuation(suspend: Values.AWAIT_SUSPEND) is | |
| 1294 | let builder_type = suspend.builder_field.type! | |
| 1295 | let name = if suspend.is_critical then "await_unsafe_on_completed" else "await_on_completed" fi | |
| 1296 | ||
| 1297 | let member = Semantic.TASK_LIKE.member_of_arity(builder_type, name, 2) | |
| 1298 | ||
| 1299 | if !member? then | |
| 1300 | _report("a builder with no {name}") | |
| 1301 | return | |
| 1302 | fi | |
| 1303 | ||
| 1304 | let machine_by_reference = isa Semantic.Types.REFERENCE(member.arguments[1]) | |
| 1305 | ||
| 1306 | let type_arguments = LIST[Type]([suspend.awaiter_type]) | |
| 1307 | ||
| 1308 | if member.generic_arguments.count > 1 then | |
| 1309 | type_arguments.add(suspend.state_machine_type) | |
| 1310 | fi | |
| 1311 | ||
| 1312 | if let await_on_completed = _builder_generic_member(builder_type, name, 2, type_arguments) then | |
| 1313 | let frame = Values.Load.REFERENCE_SELF(cast Semantic.Symbols.Classy(suspend.state_field.owner), suspend.state_machine_type) | |
| 1314 | ||
| 1315 | _call_builder( | |
| 1316 | _frame_field_load(suspend.builder_field), | |
| 1317 | await_on_completed, | |
| 1318 | LIST[Value]([_frame_field_load(suspend.awaiter_field), frame]), | |
| 1319 | LIST[bool]([true, machine_by_reference])) | |
| 1320 | fi | |
| 1321 | si | |
| 1322 | ||
| 1323 | // The builder member `name` taking `arity` arguments, or null, with | |
| 1324 | // the build reported, where the builder declares none. | |
| 1325 | _builder_member(builder_type: Type, name: string, arity: int) -> Semantic.Symbols.Function? is | |
| 1326 | let member = Semantic.TASK_LIKE.member_of_arity(builder_type, name, arity) | |
| 1327 | ||
| 1328 | if !member? then | |
| 1329 | _report("a builder with no {name} taking {arity} arguments") | |
| 1330 | fi | |
| 1331 | ||
| 1332 | return member | |
| 1333 | si | |
| 1334 | ||
| 1335 | // The generic builder member `name`, at `type_arguments`. | |
| 1336 | _builder_generic_member(builder_type: Type, name: string, arity: int, type_arguments: Collections.List[Type]) -> Semantic.Symbols.Function? is | |
| 1337 | if let member = _builder_member(builder_type, name, arity) then | |
| 1338 | return cast Semantic.Symbols.Function?(member.specialize(type_arguments)) | |
| 1339 | fi | |
| 1340 | ||
| 1341 | return null | |
| 1342 | si | |
| 1343 | ||
| 1344 | _builder_task(builder: Value, builder_type: Type) is | |
| 1345 | if let property: Semantic.Symbols.Property = builder_type.find_member("task") /\ property.read_function? then | |
| 1346 | gen(Values.Call.INSTANCE(builder, property.read_function, LIST[Value](), null)) | |
| 1347 | else | |
| 1348 | _report("a builder with no task") | |
| 1349 | fi | |
| 1350 | si | |
| 1351 | ||
| 1352 | // Calls a builder member whose `ref` arguments, those `by_cell` | |
| 1353 | // marks, are each given a cell of their own holding the value: a | |
| 1354 | // struct's box, or a reference cell for a value of reference type. | |
| 1355 | // The builder reads through the reference and does not write back. | |
| 1356 | _call_builder(from: Value, function: Semantic.Symbols.Function, arguments: Collections.List[Value], by_cell: Collections.List[bool]) is | |
| 1357 | let instance = _instance_of(function) | |
| 1358 | let index = _numbering.index_of(instance) | |
| 1359 | ||
| 1360 | if !index? then | |
| 1361 | _report_uncompiled_call(instance) | |
| 1362 | return | |
| 1363 | fi | |
| 1364 | ||
| 1365 | gen(from) | |
| 1366 | _coerce_top(_local_type_of_receiver(instance)) | |
| 1367 | ||
| 1368 | for i in 0..arguments.count do | |
| 1369 | let argument = arguments[i] | |
| 1370 | ||
| 1371 | gen(argument) | |
| 1372 | ||
| 1373 | if by_cell[i] then | |
| 1374 | if _struct_of(argument.type)? then | |
| 1375 | // A reference to a struct is its box, which the value is. | |
| 1376 | elif _registry.reference_cell_value_type(_specialize(argument.type))? then | |
| 1377 | _emit(TYPE_INSTRUCTION(Opcode.STRUCT_NEW, _registry.reference_cell_type_index)) | |
| 1378 | else | |
| 1379 | _report("a reference to a {argument.type} passed to a builder") | |
| 1380 | fi | |
| 1381 | else | |
| 1382 | _coerce_top(_value_type(instance.specialize(function.arguments[i]))) | |
| 1383 | fi | |
| 1384 | od | |
| 1385 | ||
| 1386 | _emit(INDEX_INSTRUCTION(Opcode.CALL, index)) | |
| 1387 | si | |
| 1388 | ||
| 1389 | visit(max_stack: Values.MAX_STACK) is si | |
| 1390 | ||
| 1391 | visit(equality: Values.REFERENCE_EQUALITY) is | |
| 1392 | gen(equality.left) | |
| 1393 | gen(equality.right) | |
| 1394 | ||
| 1395 | _emit(Opcode.REF_EQ) | |
| 1396 | si | |
| 1397 | // A MAYBE holding a value is built by the core library's constructor | |
| 1398 | // taking that value. | |
| 1399 | visit(construct: Values.CONSTRUCT_MAYBE) is | |
| 1400 | if let constructor = INTERPOLATION_CALLS.method(construct.type, "init", 1) then | |
| 1401 | gen(Values.NEW(construct.type, constructor, LIST[Value]([construct.value]))) | |
| 1402 | else | |
| 1403 | _report("an optional value, whose MAYBE has no constructor taking its value") | |
| 1404 | fi | |
| 1405 | si | |
| 1406 | // A value an earlier step left on the stack, where it still is. | |
| 1407 | visit(stack_top: Values.STACK_TOP) is si | |
| 1408 | visit(stack_top: Values.STACK_TOP_ADDRESS) is si | |
| 1409 | visit_address(stack_top: Values.STACK_TOP_ADDRESS) is si | |
| 1410 | ||
| 1411 | /// An `if` or `case` that produces a value is a block whose last | |
| 1412 | /// value is the region its arms leave, each carrying the value it | |
| 1413 | /// produces, so that region's wasm block results in the type the | |
| 1414 | /// block produces. | |
| 1415 | // Unlike the CIL backend, this one lowers a body more than once, | |
| 1416 | // while discovering and then once for each instantiation, so a block | |
| 1417 | // is not marked as emitted. | |
| 1418 | visit(block: Values.BLOCK) is | |
| 1419 | let values = block.values | |
| 1420 | ||
| 1421 | for i in 0..values.count do | |
| 1422 | let value = values[i] | |
| 1423 | ||
| 1424 | _mark(value) | |
| 1425 | ||
| 1426 | if i == values.count - 1 /\ _produces_value(block) /\ isa Values.REGION(value) then | |
| 1427 | let result = _value_type(block.type) | |
| 1428 | ||
| 1429 | if !result? then | |
| 1430 | _report("a value of type {block.type}") | |
| 1431 | fi | |
| 1432 | ||
| 1433 | _lower_region(cast Values.REGION(value), result) | |
| 1434 | else | |
| 1435 | gen(value) | |
| 1436 | fi | |
| 1437 | od | |
| 1438 | si | |
| 1439 | ||
| 1440 | // A body every path out of which diverges, by a throw or a return, | |
| 1441 | // is typed as an error, and produces no value. | |
| 1442 | _produces_value(block: Values.BLOCK) -> bool => | |
| 1443 | !WASMGC_TYPES.is_void(block.type) /\ !(block.type?.is_error ?? false) | |
| 1444 | ||
| 1445 | visit(block: Values.DUMMY_BLOCK) is | |
| 1446 | throw System.InvalidOperationException("dummy block generated, created at: {block.location} because: {block.why}") | |
| 1447 | si | |
| 1448 | ||
| 1449 | // `super` is a REFERENCE_SELF too: it is the same receiver, and | |
| 1450 | // only the calls made through it differ. | |
| 1451 | visit(self_load: Values.Load.REFERENCE_SELF) is | |
| 1452 | _load_receiver() | |
| 1453 | si | |
| 1454 | ||
| 1455 | visit_address(self_load: Values.Load.REFERENCE_SELF) is | |
| 1456 | if _struct_of(self_load.type)? then | |
| 1457 | _load_receiver() | |
| 1458 | else | |
| 1459 | _report("self") | |
| 1460 | fi | |
| 1461 | si | |
| 1462 | ||
| 1463 | // `self` inside a generator's or an asynchronous function's frame | |
| 1464 | // is the instance the frame was made for, which the frame holds in | |
| 1465 | // a field of its own, as the .NET target reads it. | |
| 1466 | visit(self_load: Values.Load.OUTER_SELF) is | |
| 1467 | gen(_frame_field_load(self_load.outer_self_field)) | |
| 1468 | si | |
| 1469 | ||
| 1470 | visit_address(self_load: Values.Load.OUTER_SELF) is | |
| 1471 | visit(self_load) | |
| 1472 | si | |
| 1473 | // A scalar's method takes the value itself as its receiver. A | |
| 1474 | // struct method's receiver is the box its value lives in, so its | |
| 1475 | // address is the receiver and its value a copy. | |
| 1476 | visit(self_load: Values.Load.VALUE_SELF) is | |
| 1477 | if WASMGC_TYPES.scalar(self_load.type)? then | |
| 1478 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, 0)) | |
| 1479 | elif _struct_of(self_load.type)? then | |
| 1480 | _load_receiver() | |
| 1481 | _copy_if_mutable(self_load.type) | |
| 1482 | else | |
| 1483 | _report("self") | |
| 1484 | fi | |
| 1485 | si | |
| 1486 | ||
| 1487 | visit_address(self_load: Values.Load.VALUE_SELF) is | |
| 1488 | if _struct_of(self_load.type)? then | |
| 1489 | _load_receiver() | |
| 1490 | else | |
| 1491 | _report("self") | |
| 1492 | fi | |
| 1493 | si | |
| 1494 | visit(runtime: Values.RUNTIME) is | |
| 1495 | if _lower_builder(runtime) then | |
| 1496 | return | |
| 1497 | fi | |
| 1498 | ||
| 1499 | if let exception = _runtime_exception(runtime.operation) then | |
| 1500 | _construct_runtime_exception(exception, runtime.operands) | |
| 1501 | return | |
| 1502 | fi | |
| 1503 | ||
| 1504 | // An object is built whole where it is constructed, so there is | |
| 1505 | // nothing for its constructor to initialise first. | |
| 1506 | if runtime.operation == IR.RuntimeOperation.INIT_OBJECT then | |
| 1507 | return | |
| 1508 | fi | |
| 1509 | ||
| 1510 | // A module runs on one thread. | |
| 1511 | if runtime.operation == IR.RuntimeOperation.CURRENT_THREAD_ID then | |
| 1512 | _emit(I32_CONSTANT(0)) | |
| 1513 | return | |
| 1514 | fi | |
| 1515 | ||
| 1516 | // A range is constructed from its two endpoints. | |
| 1517 | if runtime.operation == IR.RuntimeOperation.NEW_RANGE then | |
| 1518 | if let constructor = _method(runtime.type, "init", 2) then | |
| 1519 | gen(Values.NEW(runtime.type!, constructor, LIST[Value](runtime.operands))) | |
| 1520 | else | |
| 1521 | _report("a range, whose type has no constructor taking its endpoints") | |
| 1522 | fi | |
| 1523 | ||
| 1524 | return | |
| 1525 | fi | |
| 1526 | ||
| 1527 | let strings = _strings | |
| 1528 | ||
| 1529 | if !strings? then | |
| 1530 | _report("a runtime operation") | |
| 1531 | return | |
| 1532 | fi | |
| 1533 | ||
| 1534 | case runtime.operation | |
| 1535 | when IR.RuntimeOperation.STRINGS_EQUAL then | |
| 1536 | _call_string_helper(strings.equals_function, strings, runtime.operands) | |
| 1537 | ||
| 1538 | when IR.RuntimeOperation.CONCAT_STRINGS then | |
| 1539 | _call_string_helper(strings.concat_function, strings, runtime.operands) | |
| 1540 | ||
| 1541 | else | |
| 1542 | _report("the runtime operation {runtime.operation}") | |
| 1543 | esac | |
| 1544 | si | |
| 1545 | ||
| 1546 | _is_representation_operation(name: string) -> bool => | |
| 1547 | name =~ "string.length" \/ name =~ "string.char_at" \/ name =~ "string.concat" \/ | |
| 1548 | name =~ "string.from_char" \/ name =~ "string.substring" \/ name =~ "string.from_chars" \/ name =~ "string.copy_to" \/ | |
| 1549 | name =~ "object.type_name" | |
| 1550 | ||
| 1551 | // An interpolation's builder operations, as INTERPOLATION_CALLS | |
| 1552 | // gives them. | |
| 1553 | _lower_builder(runtime: Values.RUNTIME) -> bool is | |
| 1554 | let operands = runtime.operands | |
| 1555 | let calls = INTERPOLATION_CALLS(construct => _report(construct)) | |
| 1556 | ||
| 1557 | case runtime.operation | |
| 1558 | when IR.RuntimeOperation.NEW_STRING_BUILDER then | |
| 1559 | _gen_call(calls.new_builder(runtime.type, operands)) | |
| 1560 | ||
| 1561 | when IR.RuntimeOperation.APPEND_TEXT then | |
| 1562 | _gen_call_dropping(calls.append(operands[0], operands[1])) | |
| 1563 | ||
| 1564 | when IR.RuntimeOperation.APPEND_VALUE then | |
| 1565 | _gen_call_dropping(calls.append(operands[0], _text_of(calls, operands[1]))) | |
| 1566 | ||
| 1567 | when IR.RuntimeOperation.BUILD_STRING then | |
| 1568 | _gen_call(calls.build(operands[0])) | |
| 1569 | ||
| 1570 | when IR.RuntimeOperation.APPEND_ALIGNED_VALUE then | |
| 1571 | _gen_call_dropping(calls.append_aligned(operands[0], _text_of(calls, operands[1]), operands[2])) | |
| 1572 | ||
| 1573 | when IR.RuntimeOperation.APPEND_FORMATTED_VALUE then | |
| 1574 | _gen_call_dropping(calls.append(operands[0], calls.formatted_text_of(operands[1], operands[2], _specialize(operands[1].type)))) | |
| 1575 | ||
| 1576 | when IR.RuntimeOperation.APPEND_ALIGNED_FORMATTED_VALUE then | |
| 1577 | _gen_call_dropping(calls.append_aligned(operands[0], calls.formatted_text_of(operands[1], operands[3], _specialize(operands[1].type)), operands[2])) | |
| 1578 | ||
| 1579 | else | |
| 1580 | return false | |
| 1581 | esac | |
| 1582 | ||
| 1583 | return true | |
| 1584 | si | |
| 1585 | ||
| 1586 | // The text of `value`. Under a type parameter the instantiation | |
| 1587 | // decides: generic code is compiled once per instantiation, so the | |
| 1588 | // type is known here. | |
| 1589 | _text_of(calls: INTERPOLATION_CALLS, value: Value) -> Value => | |
| 1590 | calls.text_of(value, _specialize(value.type)) | |
| 1591 | ||
| 1592 | _gen_call(call: Value?) is | |
| 1593 | if let c = call then | |
| 1594 | gen(c) | |
| 1595 | fi | |
| 1596 | si | |
| 1597 | ||
| 1598 | // Lowers `call` and drops any result it leaves. | |
| 1599 | _gen_call_dropping(call: Value?) is | |
| 1600 | if let c = call then | |
| 1601 | gen(c) | |
| 1602 | ||
| 1603 | if !WASMGC_TYPES.is_void(c.type) then | |
| 1604 | _emit(Opcode.DROP) | |
| 1605 | fi | |
| 1606 | fi | |
| 1607 | si | |
| 1608 | ||
| 1609 | // The method of `type` named `name` that takes `arity` arguments. | |
| 1610 | _method(type: Type?, name: string, arity: int) -> Function? => INTERPOLATION_CALLS.method(type, name, arity) | |
| 1611 | ||
| 1612 | // The static method of `type` named `name` that takes `arity` | |
| 1613 | // arguments. | |
| 1614 | _static_method(type: Type?, name: string, arity: int) -> Function? is | |
| 1615 | let found = type?.find_member(name) | |
| 1616 | ||
| 1617 | if let group: Semantic.Symbols.FUNCTION_GROUP = found then | |
| 1618 | for function in group.functions do | |
| 1619 | if !function.is_instance /\ function.arguments.count == arity then | |
| 1620 | return function | |
| 1621 | fi | |
| 1622 | od | |
| 1623 | elif let function: Function = found /\ !function.is_instance /\ function.arguments.count == arity then | |
| 1624 | return function | |
| 1625 | fi | |
| 1626 | ||
| 1627 | return null | |
| 1628 | si | |
| 1629 | ||
| 1630 | _call_string_helper(index: int, strings: WASMGC_STRINGS, arguments: Collections.List[Value]) is | |
| 1631 | for argument in arguments do | |
| 1632 | gen(argument) | |
| 1633 | _coerce_top(strings.string_reference) | |
| 1634 | od | |
| 1635 | ||
| 1636 | _emit(INDEX_INSTRUCTION(Opcode.CALL, index)) | |
| 1637 | si | |
| 1638 | ||
| 1639 | // Whether a bigint's value is in its i64 field, or that field. | |
| 1640 | _lower_bigint_field(name: string, value: Value) is | |
| 1641 | let held = _registry.class_of(value.type) | |
| 1642 | ||
| 1643 | if !held? then | |
| 1644 | _report("{name} on a {value.type}") | |
| 1645 | return | |
| 1646 | fi | |
| 1647 | ||
| 1648 | let index = _registry.type_index(held)! | |
| 1649 | ||
| 1650 | gen(value) | |
| 1651 | _coerce_top(VALUE_TYPE.of_type_index(index, false)) | |
| 1652 | ||
| 1653 | if name =~ "bigint.small" then | |
| 1654 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, index, WASMGC_TYPE_REGISTRY.BIGINT_SMALL)) | |
| 1655 | else | |
| 1656 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, index, WASMGC_TYPE_REGISTRY.BIGINT_BIG)) | |
| 1657 | _emit(Opcode.REF_IS_NULL) | |
| 1658 | fi | |
| 1659 | si | |
| 1660 | ||
| 1661 | // The operations the core library declares on strings and objects | |
| 1662 | // that read their representation, which no ghūl code can reach. | |
| 1663 | _lower_representation(operation: string?, arguments: Collections.List[Value]) -> bool is | |
| 1664 | if !operation? then | |
| 1665 | return false | |
| 1666 | fi | |
| 1667 | ||
| 1668 | let name = operation | |
| 1669 | ||
| 1670 | if name =~ "object.get_type" then | |
| 1671 | gen(arguments[0]) | |
| 1672 | _coerce_top(_registry.object_reference) | |
| 1673 | ||
| 1674 | if _is_null_checked(arguments[0]) then | |
| 1675 | _check_not_null(_registry.object_reference) | |
| 1676 | fi | |
| 1677 | ||
| 1678 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, WASMGC_TYPE_REGISTRY.OBJECT_TYPE_INDEX, 0)) | |
| 1679 | _type_object() | |
| 1680 | ||
| 1681 | return true | |
| 1682 | fi | |
| 1683 | ||
| 1684 | if name =~ "object.display" then | |
| 1685 | _lower_display(arguments) | |
| 1686 | return true | |
| 1687 | fi | |
| 1688 | ||
| 1689 | if name =~ "object.memberwise_clone" then | |
| 1690 | _lower_clone(arguments[0]) | |
| 1691 | return true | |
| 1692 | fi | |
| 1693 | ||
| 1694 | if name =~ "object.identity_hash" then | |
| 1695 | _lower_identity_hash(arguments[0]) | |
| 1696 | return true | |
| 1697 | fi | |
| 1698 | ||
| 1699 | if name =~ "bigint.is_small" \/ name =~ "bigint.small" then | |
| 1700 | _lower_bigint_field(name, arguments[0]) | |
| 1701 | return true | |
| 1702 | fi | |
| 1703 | ||
| 1704 | let strings = _strings | |
| 1705 | ||
| 1706 | // Discovery runs before the strings exist, and only needs | |
| 1707 | // what the arguments reach. | |
| 1708 | if !strings? then | |
| 1709 | if _registry.discovering /\ _is_representation_operation(name) then | |
| 1710 | _lower_all(arguments) | |
| 1711 | return true | |
| 1712 | fi | |
| 1713 | ||
| 1714 | return false | |
| 1715 | fi | |
| 1716 | ||
| 1717 | let string_type = strings.string_type_index | |
| 1718 | let chars = _registry.chars_type_index | |
| 1719 | ||
| 1720 | if name =~ "string.length" then | |
| 1721 | gen(arguments[0]) | |
| 1722 | _coerce_top(strings.string_reference) | |
| 1723 | ||
| 1724 | if _is_null_checked(arguments[0]) then | |
| 1725 | _check_not_null(strings.string_reference) | |
| 1726 | fi | |
| 1727 | ||
| 1728 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, string_type, WASMGC_TYPE_REGISTRY.STRING_CHARS)) | |
| 1729 | _emit(Opcode.ARRAY_LEN) | |
| 1730 | elif name =~ "string.char_at" then | |
| 1731 | gen(arguments[0]) | |
| 1732 | _coerce_top(strings.string_reference) | |
| 1733 | ||
| 1734 | if _is_null_checked(arguments[0]) then | |
| 1735 | _check_not_null(strings.string_reference) | |
| 1736 | fi | |
| 1737 | ||
| 1738 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, string_type, WASMGC_TYPE_REGISTRY.STRING_CHARS)) | |
| 1739 | gen(arguments[1]) | |
| 1740 | _emit(TYPE_INSTRUCTION(Opcode.ARRAY_GET_U, chars)) | |
| 1741 | elif name =~ "string.concat" then | |
| 1742 | _call_string_helper(strings.concat_function, strings, arguments) | |
| 1743 | elif name =~ "string.substring" then | |
| 1744 | gen(arguments[0]) | |
| 1745 | _coerce_top(strings.string_reference) | |
| 1746 | gen(arguments[1]) | |
| 1747 | gen(arguments[2]) | |
| 1748 | _emit(INDEX_INSTRUCTION(Opcode.CALL, strings.substring_function)) | |
| 1749 | elif name =~ "string.from_chars" then | |
| 1750 | let array = _array_of(arguments[0].type) | |
| 1751 | ||
| 1752 | if !array? then | |
| 1753 | _report("string.from_chars over a value that is not an array") | |
| 1754 | return true | |
| 1755 | fi | |
| 1756 | ||
| 1757 | let storage = _registry.array_storage(array) | |
| 1758 | ||
| 1759 | gen(arguments[0]) | |
| 1760 | _coerce_top(VALUE_TYPE.of_type_index(storage.struct_index, true)) | |
| 1761 | _check_not_null(VALUE_TYPE.of_type_index(storage.struct_index, true)) | |
| 1762 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, storage.struct_index, 1)) | |
| 1763 | gen(arguments[1]) | |
| 1764 | gen(arguments[2]) | |
| 1765 | _emit(INDEX_INSTRUCTION(Opcode.CALL, strings.from_chars_function(storage.data_index))) | |
| 1766 | elif name =~ "string.copy_to" then | |
| 1767 | let array = _array_of(arguments[2].type) | |
| 1768 | ||
| 1769 | if !array? then | |
| 1770 | _report("string.copy_to into a value that is not an array") | |
| 1771 | return true | |
| 1772 | fi | |
| 1773 | ||
| 1774 | let storage = _registry.array_storage(array) | |
| 1775 | ||
| 1776 | gen(arguments[0]) | |
| 1777 | _coerce_top(strings.string_reference) | |
| 1778 | gen(arguments[1]) | |
| 1779 | gen(arguments[2]) | |
| 1780 | _coerce_top(VALUE_TYPE.of_type_index(storage.struct_index, true)) | |
| 1781 | _check_not_null(VALUE_TYPE.of_type_index(storage.struct_index, true)) | |
| 1782 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, storage.struct_index, 1)) | |
| 1783 | gen(arguments[3]) | |
| 1784 | gen(arguments[4]) | |
| 1785 | _emit(INDEX_INSTRUCTION(Opcode.CALL, strings.copy_to_function(storage.data_index))) | |
| 1786 | elif name =~ "string.from_char" then | |
| 1787 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.string_descriptor_global!)) | |
| 1788 | _emit(I32_CONSTANT(0)) | |
| 1789 | gen(arguments[0]) | |
| 1790 | _emit(TYPE_LENGTH_INSTRUCTION(Opcode.ARRAY_NEW_FIXED, chars, 1)) | |
| 1791 | _emit(TYPE_INSTRUCTION(Opcode.STRUCT_NEW, string_type)) | |
| 1792 | elif name =~ "object.type_name" then | |
| 1793 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.string_descriptor_global!)) | |
| 1794 | _emit(I32_CONSTANT(0)) | |
| 1795 | gen(arguments[0]) | |
| 1796 | _coerce_top(_registry.object_reference) | |
| 1797 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, WASMGC_TYPE_REGISTRY.OBJECT_TYPE_INDEX, 0)) | |
| 1798 | _emit( | |
| 1799 | TYPE_FIELD_INSTRUCTION( | |
| 1800 | Opcode.STRUCT_GET, | |
| 1801 | WASMGC_TYPE_REGISTRY.DESCRIPTOR_TYPE_INDEX, | |
| 1802 | WASMGC_TYPE_REGISTRY.DESCRIPTOR_NAME)) | |
| 1803 | _emit(TYPE_INSTRUCTION(Opcode.STRUCT_NEW, string_type)) | |
| 1804 | else | |
| 1805 | return false | |
| 1806 | fi | |
| 1807 | ||
| 1808 | return true | |
| 1809 | si | |
| 1810 | visit(creation: Values.CREATE_UNIT_VARIANT_INSTANCE) is _report("a union") si | |
| 1811 | ||
| 1812 | visit(dummy: Values.DUMMY) is | |
| 1813 | IoC.CONTAINER.instance.logger.poison(dummy.location, "generated dummy value: {dummy.type} from {dummy.location}") | |
| 1814 | si | |
| 1815 | ||
| 1816 | visit(type_expression: Values.TYPE_EXPRESSION) is | |
| 1817 | IoC.CONTAINER.instance.logger.poison(type_expression.location, "generated type expression: {type_expression.type} from {type_expression.location}") | |
| 1818 | si | |
| 1819 | ||
| 1820 | visit(store: Values.Need.STORE) is | |
| 1821 | assert false else "unresolved need store" | |
| 1822 | si | |
| 1823 | ||
| 1824 | // A store through a reference to a scalar or to a value of reference | |
| 1825 | // type sets the cell's value. One through a reference to a struct | |
| 1826 | // changes the box it refers to in place, field by field, so whatever | |
| 1827 | // else holds that box sees the new value, as it would on .NET. | |
| 1828 | visit(store: Values.Store.INDIRECT) is | |
| 1829 | if let held = _struct_of(store.type) then | |
| 1830 | let index = _registry.type_index(held)! | |
| 1831 | let target = _temp(VALUE_TYPE.of_type_index(index, true)) | |
| 1832 | let source = _temp(VALUE_TYPE.of_type_index(index, true)) | |
| 1833 | ||
| 1834 | gen(store.address) | |
| 1835 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, target)) | |
| 1836 | ||
| 1837 | gen(store.value) | |
| 1838 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, source)) | |
| 1839 | ||
| 1840 | // While discovering, the struct's layout is not built yet, | |
| 1841 | // and only what the operands reach matters. | |
| 1842 | if _registry.discovering then | |
| 1843 | return | |
| 1844 | fi | |
| 1845 | ||
| 1846 | for i in 1.._registry.field_value_types(held).count do | |
| 1847 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, target)) | |
| 1848 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, source)) | |
| 1849 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, index, i)) | |
| 1850 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_SET, index, i)) | |
| 1851 | od | |
| 1852 | elif let cell = _registry.box_index(_specialize(store.type)) then | |
| 1853 | gen(store.address) | |
| 1854 | gen(store.value) | |
| 1855 | ||
| 1856 | _normalise_for(store.type) | |
| 1857 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_SET, cell, WASMGC_TYPE_REGISTRY.BOX_VALUE)) | |
| 1858 | elif _registry.reference_cell_value_type(_specialize(store.type))? then | |
| 1859 | gen(store.address) | |
| 1860 | gen(store.value) | |
| 1861 | ||
| 1862 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_SET, _registry.reference_cell_type_index, WASMGC_TYPE_REGISTRY.REFERENCE_CELL_VALUE)) | |
| 1863 | else | |
| 1864 | _report("a store through a reference") | |
| 1865 | fi | |
| 1866 | si | |
| 1867 | ||
| 1868 | visit(wrapper: Values.TYPE_WRAPPER) is | |
| 1869 | gen(wrapper.value) | |
| 1870 | si | |
| 1871 | ||
| 1872 | visit_address(wrapper: Values.TYPE_WRAPPER) is _report("the address of a value") si | |
| 1873 | ||
| 1874 | visit(wrapper: Values.WRAPPER) is | |
| 1875 | assert wrapper.value? else "generate wrapper without value" | |
| 1876 | ||
| 1877 | gen(wrapper.value) | |
| 1878 | si | |
| 1879 | ||
| 1880 | visit_address(wrapper: Values.WRAPPER) is _report("the address of a value") si | |
| 1881 | // A present value-type optional is the box its value is held in. | |
| 1882 | // The value is produced before the box's descriptor is pushed, | |
| 1883 | // since it may read a receiver an earlier step left on the stack. | |
| 1884 | visit(wrap: Values.WRAP_OPTIONAL) is | |
| 1885 | // A MAYBE, the optional of an unconstrained type parameter, is the | |
| 1886 | // core library's struct, built by its constructor. | |
| 1887 | if _specialize(wrap.type)?.is_maybe ?? false then | |
| 1888 | gen(Values.CONSTRUCT_MAYBE(_specialize(wrap.type)!, wrap.value)) | |
| 1889 | return | |
| 1890 | fi | |
| 1891 | ||
| 1892 | let value = wrap.value | |
| 1893 | ||
| 1894 | if let index = _registry.box_index(_specialize(value.type)) then | |
| 1895 | _box(value, _registry.box_type(_specialize(value.type))!, index) | |
| 1896 | elif _struct_of(value.type)? then | |
| 1897 | gen(value) | |
| 1898 | else | |
| 1899 | _report("an optional value") | |
| 1900 | fi | |
| 1901 | si | |
| 1902 | ||
| 1903 | visit(projection: Values.NARROW_PROJECT) is | |
| 1904 | gen(projection.projected) | |
| 1905 | si | |
| 1906 | ||
| 1907 | visit(view: Values.NARROW_VIEW) is | |
| 1908 | gen(view.underlying) | |
| 1909 | si | |
| 1910 | ||
| 1911 | visit_address(view: Values.NARROW_VIEW) is _report("the address of a value") si | |
| 1912 | ||
| 1913 | /// An `@IL.output` region names CIL the build emits, and there is | |
| 1914 | /// none on this target. | |
| 1915 | visit(enter: Values.ENTER_FILE) is si | |
| 1916 | visit(leave: Values.LEAVE_FILE) is si | |
| 1917 | ||
| 1918 | // A function value holds a thunk with the signature its type calls | |
| 1919 | // through, and the receiver or frame the thunk passes on. | |
| 1920 | visit(closure: Values.MAKE_CLOSURE) is | |
| 1921 | let function = closure.function | |
| 1922 | ||
| 1923 | // A method reached through a trait is dispatched, as a call | |
| 1924 | // through the trait is, whatever is recorded as overriding it. | |
| 1925 | if (closure.is_virtual \/ function.is_abstract) /\ _is_trait_method(function) then | |
| 1926 | _trait_method_closure(closure) | |
| 1927 | return | |
| 1928 | fi | |
| 1929 | ||
| 1930 | let is_virtual = closure.is_virtual /\ !function.has_no_overriders | |
| 1931 | ||
| 1932 | let instance = _instance_of(function) | |
| 1933 | ||
| 1934 | if !_numbering.index_of(instance)? then | |
| 1935 | _report("a function value of {function.name}") | |
| 1936 | return | |
| 1937 | fi | |
| 1938 | ||
| 1939 | let signature = _registry.closure_signature(_specialize(closure.type)!) | |
| 1940 | ||
| 1941 | if !signature? then | |
| 1942 | _report("a function value of type {closure.type}") | |
| 1943 | return | |
| 1944 | fi | |
| 1945 | ||
| 1946 | let discards_result = _discards_result(closure.type, function) | |
| 1947 | ||
| 1948 | // A function with no receiver can still be held closed over its | |
| 1949 | // first argument, which the value then carries where a receiver | |
| 1950 | // would go. | |
| 1951 | let closes_over_first = !function.is_emitted_with_receiver /\ !isa Values.NULL(closure.receiver) | |
| 1952 | ||
| 1953 | let thunk mut = 0 | |
| 1954 | ||
| 1955 | if !is_virtual then | |
| 1956 | thunk = _registry.closure_thunk(signature, instance, discards_result, closes_over_first, _numbering) | |
| 1957 | elif _registry.discovering then | |
| 1958 | _registry.note_dispatched(function) | |
| 1959 | elif let slot = _registry.dispatch.slot_of(instance) then | |
| 1960 | _registry.dispatch.use_slot(slot, _numbering) | |
| 1961 | ||
| 1962 | let parameter_types = LIST[VALUE_TYPE?]() | |
| 1963 | ||
| 1964 | for argument in function.arguments do | |
| 1965 | parameter_types.add(_value_type(instance.specialize(argument))) | |
| 1966 | od | |
| 1967 | ||
| 1968 | thunk = _registry.closure_virtual_thunk(signature, slot, parameter_types, discards_result, _numbering) | |
| 1969 | else | |
| 1970 | _report("a function value of an overridden method") | |
| 1971 | return | |
| 1972 | fi | |
| 1973 | ||
| 1974 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.closure_descriptor_global)) | |
| 1975 | _emit(INDEX_INSTRUCTION(Opcode.REF_FUNC, thunk)) | |
| 1976 | ||
| 1977 | if (function.is_emitted_with_receiver \/ closes_over_first) /\ !isa Values.NULL(closure.receiver) then | |
| 1978 | gen(closure.receiver) | |
| 1979 | else | |
| 1980 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_NULL, _registry.object_reference.heap_type()!)) | |
| 1981 | fi | |
| 1982 | ||
| 1983 | _emit(TYPE_INSTRUCTION(Opcode.STRUCT_NEW, _registry.closure_type_index)) | |
| 1984 | si | |
| 1985 | ||
| 1986 | // A function value holding a trait method: a thunk that calls | |
| 1987 | // through the dispatch table row the method's selector names, and | |
| 1988 | // the receiver it calls on. | |
| 1989 | _trait_method_closure(closure: Values.MAKE_CLOSURE) is | |
| 1990 | let function = closure.function | |
| 1991 | let dispatch = _registry.dispatch | |
| 1992 | let trait_type = _trait_instance(function.owner) | |
| 1993 | let root = cast Function?(function.root_specialized_from)! | |
| 1994 | ||
| 1995 | if _registry.discovering then | |
| 1996 | _registry.note_dispatched(function) | |
| 1997 | ||
| 1998 | if trait_type? then | |
| 1999 | dispatch.selector_of(root, trait_type) | |
| 2000 | fi | |
| 2001 | ||
| 2002 | gen(closure.receiver) | |
| 2003 | return | |
| 2004 | fi | |
| 2005 | ||
| 2006 | let signature = _registry.closure_signature(_specialize(closure.type)!) | |
| 2007 | ||
| 2008 | if !signature? then | |
| 2009 | _report("a function value of type {closure.type}") | |
| 2010 | return | |
| 2011 | fi | |
| 2012 | ||
| 2013 | let selector = if trait_type? then dispatch.selector_of(root, trait_type) else null fi | |
| 2014 | ||
| 2015 | if !selector? then | |
| 2016 | _report("a function value of a trait method nothing implements") | |
| 2017 | return | |
| 2018 | fi | |
| 2019 | ||
| 2020 | dispatch.use_selector(selector, _numbering) | |
| 2021 | ||
| 2022 | let instance = _instance_of(function) | |
| 2023 | let parameter_types = LIST[VALUE_TYPE?]() | |
| 2024 | ||
| 2025 | for argument in function.arguments do | |
| 2026 | parameter_types.add(_value_type(instance.specialize(argument))) | |
| 2027 | od | |
| 2028 | ||
| 2029 | let thunk = _registry.closure_interface_thunk(signature, selector, parameter_types, _discards_result(closure.type, function), _numbering) | |
| 2030 | ||
| 2031 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.closure_descriptor_global)) | |
| 2032 | _emit(INDEX_INSTRUCTION(Opcode.REF_FUNC, thunk)) | |
| 2033 | gen(closure.receiver) | |
| 2034 | _emit(TYPE_INSTRUCTION(Opcode.STRUCT_NEW, _registry.closure_type_index)) | |
| 2035 | si | |
| 2036 | ||
| 2037 | // Whether a function value's type returns nothing while the | |
| 2038 | // function it holds returns something, which the thunk drops. | |
| 2039 | _discards_result(type: Type, function: Function) -> bool => | |
| 2040 | (type.is_action \/ WASMGC_TYPES.is_void(type.arguments[type.arguments.count - 1])) /\ | |
| 2041 | !WASMGC_TYPES.is_void(function.return_type) | |
| 2042 | ||
| 2043 | visit(address: Values.ARRAY_ELEMENT_ADDRESS) is _report("the address of an array element") si | |
| 2044 | visit_address(address: Values.ARRAY_ELEMENT_ADDRESS) is _report("a reference to a field or array element") si | |
| 2045 | ||
| 2046 | // An array is a struct holding its descriptor and its elements. | |
| 2047 | visit(array: Values.NEW_ARRAY) is | |
| 2048 | _new_array(array.type, array.count) | |
| 2049 | si | |
| 2050 | ||
| 2051 | // An array of `array_type` holding `count` elements, each the | |
| 2052 | // default value of the element type. | |
| 2053 | _new_array(array_type: Type?, count: Value) is | |
| 2054 | let type = _array_of(array_type) | |
| 2055 | ||
| 2056 | if !type? then | |
| 2057 | return | |
| 2058 | fi | |
| 2059 | ||
| 2060 | let storage = _registry.array_storage(type) | |
| 2061 | ||
| 2062 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.descriptor_global(type))) | |
| 2063 | ||
| 2064 | gen(count) | |
| 2065 | ||
| 2066 | _emit(TYPE_INSTRUCTION(Opcode.ARRAY_NEW_DEFAULT, storage.data_index)) | |
| 2067 | _load_array_element_descriptor(type) | |
| 2068 | _emit(TYPE_INSTRUCTION(Opcode.STRUCT_NEW, storage.struct_index)) | |
| 2069 | si | |
| 2070 | ||
| 2071 | visit(sequence: Values.SEQUENCE) is | |
| 2072 | let type = _array_of(sequence.type) | |
| 2073 | ||
| 2074 | if !type? then | |
| 2075 | return | |
| 2076 | fi | |
| 2077 | ||
| 2078 | let storage = _registry.array_storage(type) | |
| 2079 | ||
| 2080 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.descriptor_global(type))) | |
| 2081 | ||
| 2082 | // An element of a value type in an array of a reference type, | |
| 2083 | // an int in an object[], is boxed as it is stored. | |
| 2084 | let boxer = IoC.CONTAINER.instance.value_boxer | |
| 2085 | ||
| 2086 | for value in sequence.values do | |
| 2087 | gen(boxer.box_if_needed(value, sequence.element_type)) | |
| 2088 | _normalise_for(sequence.element_type) | |
| 2089 | od | |
| 2090 | ||
| 2091 | _emit(TYPE_LENGTH_INSTRUCTION(Opcode.ARRAY_NEW_FIXED, storage.data_index, sequence.values.count)) | |
| 2092 | _load_array_element_descriptor(type) | |
| 2093 | _emit(TYPE_INSTRUCTION(Opcode.STRUCT_NEW, storage.struct_index)) | |
| 2094 | si | |
| 2095 | ||
| 2096 | // A store into an array of references checks that the array was | |
| 2097 | // made to hold the value, since an array of a class is usable as an | |
| 2098 | // array of its superclass. A store that would break that traps. | |
| 2099 | visit(store: Values.STORE_ARRAY_ELEMENT) is | |
| 2100 | _store_array_element(store.array, store.index, store.element) | |
| 2101 | si | |
| 2102 | ||
| 2103 | _store_array_element(array_value: Value, index: Value, element: Value) is | |
| 2104 | let type = _array_of(array_value.type) | |
| 2105 | ||
| 2106 | if !type? then | |
| 2107 | return | |
| 2108 | fi | |
| 2109 | ||
| 2110 | let storage = _registry.array_storage(type) | |
| 2111 | let array = _temp(VALUE_TYPE.of_type_index(storage.struct_index, true)) | |
| 2112 | ||
| 2113 | gen(array_value) | |
| 2114 | _coerce_top(VALUE_TYPE.of_type_index(storage.struct_index, true)) | |
| 2115 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, array)) | |
| 2116 | _throw_if_null(array) | |
| 2117 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, storage.struct_index, 1)) | |
| 2118 | ||
| 2119 | gen(index) | |
| 2120 | _check_index(storage.data_index) | |
| 2121 | ||
| 2122 | // An element whose type is not the array's, an int in an object[], | |
| 2123 | // is boxed as it is stored. | |
| 2124 | gen(IoC.CONTAINER.instance.value_boxer.box_if_needed(element, _registry.array_element(type))) | |
| 2125 | _normalise_for(_registry.array_element(type)) | |
| 2126 | ||
| 2127 | if !_registry.discovering /\ _registry.array_store_is_checked(type) then | |
| 2128 | let value = _temp(_registry.object_reference) | |
| 2129 | ||
| 2130 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, value)) | |
| 2131 | _check_array_store(array, storage.struct_index, value) | |
| 2132 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, value)) | |
| 2133 | fi | |
| 2134 | ||
| 2135 | _emit(TYPE_INSTRUCTION(Opcode.ARRAY_SET, storage.data_index)) | |
| 2136 | si | |
| 2137 | ||
| 2138 | // With the data array and an index on the stack, throws the core | |
| 2139 | // library's IndexOutOfRangeException where the index is outside the | |
| 2140 | // array, which the engine would otherwise trap on, and leaves the two | |
| 2141 | // as they were. An index compares unsigned, so a negative one is | |
| 2142 | // outside too. | |
| 2143 | _check_index(data_index: int) is | |
| 2144 | if !_can_throw(Semantic.Lookups.RuntimeException.INDEX_OUT_OF_RANGE) then | |
| 2145 | return | |
| 2146 | fi | |
| 2147 | ||
| 2148 | let index = _temp(VALUE_TYPE.of_numeric(NumericType.I32)) | |
| 2149 | let data = _temp(VALUE_TYPE.of_type_index(data_index, true)) | |
| 2150 | ||
| 2151 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, index)) | |
| 2152 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, data)) | |
| 2153 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, index)) | |
| 2154 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, data)) | |
| 2155 | _emit(Opcode.ARRAY_LEN) | |
| 2156 | _emit(Opcode.I32_GE_U) | |
| 2157 | _throw_if(Semantic.Lookups.RuntimeException.INDEX_OUT_OF_RANGE, LIST[Value]()) | |
| 2158 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, data)) | |
| 2159 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, index)) | |
| 2160 | si | |
| 2161 | ||
| 2162 | // The descriptor of the class an array of `type` is made to hold, or | |
| 2163 | // null where a store into it needs no check. | |
| 2164 | _load_array_element_descriptor(type: CLASS_INSTANCE) is | |
| 2165 | if let global = _registry.array_element_descriptor(type) then | |
| 2166 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, global)) | |
| 2167 | else | |
| 2168 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_NULL, HEAP_TYPE.of_type_index(WASMGC_TYPE_REGISTRY.DESCRIPTOR_TYPE_INDEX))) | |
| 2169 | fi | |
| 2170 | si | |
| 2171 | ||
| 2172 | // Traps where the non-null `value` is not an instance of the | |
| 2173 | // element class `array` records. An array of the root or of a trait | |
| 2174 | // records none, and takes any value. | |
| 2175 | _check_array_store(array: int, struct_index: int, value: int) is | |
| 2176 | _emit(Opcode.DROP) | |
| 2177 | ||
| 2178 | let descriptor_type = VALUE_TYPE.of_type_index(WASMGC_TYPE_REGISTRY.DESCRIPTOR_TYPE_INDEX, true) | |
| 2179 | let element = _temp(descriptor_type) | |
| 2180 | let id = _temp(VALUE_TYPE.of_numeric(NumericType.I32)) | |
| 2181 | ||
| 2182 | let check = LIST[INSTRUCTION]() | |
| 2183 | ||
| 2184 | check.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, value)) | |
| 2185 | check.add(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, WASMGC_TYPE_REGISTRY.OBJECT_TYPE_INDEX, 0)) | |
| 2186 | check.add(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, WASMGC_TYPE_REGISTRY.DESCRIPTOR_TYPE_INDEX, WASMGC_TYPE_REGISTRY.DESCRIPTOR_CLASS_ID)) | |
| 2187 | check.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, element)) | |
| 2188 | check.add(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, WASMGC_TYPE_REGISTRY.DESCRIPTOR_TYPE_INDEX, WASMGC_TYPE_REGISTRY.DESCRIPTOR_CLASS_ID)) | |
| 2189 | check.add(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, id)) | |
| 2190 | check.add(SIMPLE_INSTRUCTION(Opcode.I32_SUB)) | |
| 2191 | check.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, element)) | |
| 2192 | check.add(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, WASMGC_TYPE_REGISTRY.DESCRIPTOR_TYPE_INDEX, WASMGC_TYPE_REGISTRY.DESCRIPTOR_LAST_DESCENDANT)) | |
| 2193 | check.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, id)) | |
| 2194 | check.add(SIMPLE_INSTRUCTION(Opcode.I32_SUB)) | |
| 2195 | check.add(SIMPLE_INSTRUCTION(Opcode.I32_GT_U)) | |
| 2196 | check.add(IF_INSTRUCTION(BLOCK_TYPE(), [SIMPLE_INSTRUCTION(Opcode.UNREACHABLE)]: INSTRUCTION, LIST[INSTRUCTION]())) | |
| 2197 | ||
| 2198 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, array)) | |
| 2199 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, struct_index, 2)) | |
| 2200 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, element)) | |
| 2201 | _emit(Opcode.REF_IS_NULL) | |
| 2202 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, value)) | |
| 2203 | _emit(Opcode.REF_IS_NULL) | |
| 2204 | _emit(Opcode.I32_OR) | |
| 2205 | _emit(IF_INSTRUCTION(BLOCK_TYPE(), LIST[INSTRUCTION](), check)) | |
| 2206 | si | |
| 2207 | ||
| 2208 | // The array type `type` is, or null, reported, where the target | |
| 2209 | // cannot hold it. | |
| 2210 | _array_of(type: Type?) -> CLASS_INSTANCE? is | |
| 2211 | if let array = _registry.array_of(_specialize(type)) then | |
| 2212 | return array | |
| 2213 | fi | |
| 2214 | ||
| 2215 | _report("an array of type {type}") | |
| 2216 | ||
| 2217 | return null | |
| 2218 | si | |
| 2219 | ||
| 2220 | // The built-in operation on an array that a call to `function` | |
| 2221 | // performs, or null where it is not one. | |
| 2222 | _array_operation(function: Function) -> string? is | |
| 2223 | if let operation = cast Function?(function.root_specialized_from)?.intrinsic_operation /\ operation.starts_with("array.") then | |
| 2224 | return operation | |
| 2225 | fi | |
| 2226 | ||
| 2227 | return null | |
| 2228 | si | |
| 2229 | ||
| 2230 | // The member the core library gives every array that `call` reaches | |
| 2231 | // on an array, whether it names that member or a trait member the | |
| 2232 | // array implements with it. Null where `call` is not on an array. | |
| 2233 | _array_member(call: Values.Call.INSTANCE) -> Function? is | |
| 2234 | let type = _registry.array_of(_specialize(call.from?.type)) | |
| 2235 | ||
| 2236 | if !type? then | |
| 2237 | return null | |
| 2238 | fi | |
| 2239 | ||
| 2240 | let called = cast Function?(call.function.root_specialized_from)! | |
| 2241 | ||
| 2242 | if called.owner == type.definition then | |
| 2243 | return called | |
| 2244 | fi | |
| 2245 | ||
| 2246 | let member = type.definition.find_member(called.name) | |
| 2247 | ||
| 2248 | let candidates = | |
| 2249 | if let group: Semantic.Symbols.FUNCTION_GROUP = member then | |
| 2250 | group.functions | |
| 2251 | elif let function: Function = member then | |
| 2252 | LIST[Function]([function]) | |
| 2253 | else | |
| 2254 | LIST[Function]() | |
| 2255 | fi | |
| 2256 | ||
| 2257 | for candidate in candidates do | |
| 2258 | if let overridees = candidate.overridees then | |
| 2259 | for overridee in overridees do | |
| 2260 | if overridee.root_specialized_from == called then | |
| 2261 | return candidate | |
| 2262 | fi | |
| 2263 | od | |
| 2264 | fi | |
| 2265 | od | |
| 2266 | ||
| 2267 | return null | |
| 2268 | si | |
| 2269 | ||
| 2270 | // The elements of the array `from` holds are read from the struct | |
| 2271 | // holding them, an element of a mutable struct as a copy unless it | |
| 2272 | // is read `in_place` as a receiver. | |
| 2273 | _lower_array_operation(operation: string, call: Values.Call.INSTANCE, in_place: bool) is | |
| 2274 | ||
| 2275 | let type = _array_of(call.from?.type) | |
| 2276 | ||
| 2277 | if !type? then | |
| 2278 | return | |
| 2279 | fi | |
| 2280 | ||
| 2281 | let storage = _registry.array_storage(type) | |
| 2282 | ||
| 2283 | gen(call.from) | |
| 2284 | _coerce_top(VALUE_TYPE.of_type_index(storage.struct_index, true)) | |
| 2285 | _check_not_null(VALUE_TYPE.of_type_index(storage.struct_index, true)) | |
| 2286 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, storage.struct_index, 1)) | |
| 2287 | ||
| 2288 | if operation =~ "array.count" then | |
| 2289 | _emit(Opcode.ARRAY_LEN) | |
| 2290 | elif operation =~ "array.get" then | |
| 2291 | gen(call.arguments[0]) | |
| 2292 | _check_index(storage.data_index) | |
| 2293 | ||
| 2294 | _emit(TYPE_INSTRUCTION(Opcode.ARRAY_GET, storage.data_index)) | |
| 2295 | ||
| 2296 | if let element = _value_type(_registry.array_element(type)), heap = element.heap_type() /\ !_registry.discovering then | |
| 2297 | if !heap.matches(_registry.object_reference.heap_type()!) then | |
| 2298 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST_NULL, heap)) | |
| 2299 | fi | |
| 2300 | fi | |
| 2301 | ||
| 2302 | if !in_place then | |
| 2303 | _copy_if_mutable(_registry.array_element(type)) | |
| 2304 | fi | |
| 2305 | else | |
| 2306 | _report("the array operation {operation}") | |
| 2307 | fi | |
| 2308 | si | |
| 2309 | ||
| 2310 | visit(compare: Values.COMPARE_ORDER_TO_ZERO) is | |
| 2311 | gen(compare.value) | |
| 2312 | ||
| 2313 | _emit(I32_CONSTANT(0)) | |
| 2314 | _compare(compare.comparison, NumericType.I32, false) | |
| 2315 | si | |
| 2316 | ||
| 2317 | visit(receiver: Values.CONSTRAINED) is _report("a constrained call") si | |
| 2318 | visit(arithmetic: Values.DECIMAL_ARITHMETIC) is _report("decimal arithmetic") si | |
| 2319 | visit(compare: Values.DECIMAL_COMPARE) is _report("decimal arithmetic") si | |
| 2320 | visit(convert: Values.DECIMAL_CONVERT) is _report("a decimal conversion") si | |
| 2321 | // A comparison over a type parameter compares as the instantiation's | |
| 2322 | // type argument does. | |
| 2323 | visit(equality: Values.EQUALITY_COMPARISON) is | |
| 2324 | _equal_values(equality.left, equality.right, _specialize(equality.left.type)) | |
| 2325 | si | |
| 2326 | ||
| 2327 | // `left =~ right` over values of the concrete `type`: by value for | |
| 2328 | // a scalar, by characters for a string, through the type's own `=~` | |
| 2329 | // where it declares one, and by reference otherwise. An absent | |
| 2330 | // value is equal only to another. | |
| 2331 | _equal_values(left: Value, right: Value, type: Type?) is | |
| 2332 | if let scalar = WASMGC_TYPES.scalar(type) then | |
| 2333 | _equal_scalars(left, right, WASMGC_TYPES.numeric(scalar)) | |
| 2334 | return | |
| 2335 | fi | |
| 2336 | ||
| 2337 | let value_type = _value_type(type) | |
| 2338 | ||
| 2339 | if let tuple: Semantic.Types.TUPLE = type then | |
| 2340 | _equal_tuples(left, right, tuple) | |
| 2341 | return | |
| 2342 | fi | |
| 2343 | ||
| 2344 | if !value_type? \/ !value_type.heap_type()? then | |
| 2345 | _report("an equality comparison of {type}") | |
| 2346 | return | |
| 2347 | fi | |
| 2348 | ||
| 2349 | if let strings = _strings /\ WASMGC_TYPES.is_string(type) then | |
| 2350 | _call_string_helper(strings.equals_function, strings, LIST[Value]([left, right])) | |
| 2351 | return | |
| 2352 | fi | |
| 2353 | ||
| 2354 | let operator = EQUALITY_OPERATOR.declared(type!) | |
| 2355 | ||
| 2356 | if !operator? then | |
| 2357 | gen(left) | |
| 2358 | gen(right) | |
| 2359 | _emit(Opcode.REF_EQ) | |
| 2360 | return | |
| 2361 | fi | |
| 2362 | ||
| 2363 | let left_local = _temp(value_type) | |
| 2364 | let right_local = _temp(value_type) | |
| 2365 | ||
| 2366 | gen(left) | |
| 2367 | _coerce_top(value_type) | |
| 2368 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, left_local)) | |
| 2369 | ||
| 2370 | gen(right) | |
| 2371 | _coerce_top(value_type) | |
| 2372 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, right_local)) | |
| 2373 | ||
| 2374 | let bool_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_bool_type() | |
| 2375 | let receiver = WASMGC_LOCAL_VALUE(left_local, type) | |
| 2376 | let argument = WASMGC_LOCAL_VALUE(right_local, type) | |
| 2377 | ||
| 2378 | let call: Value = | |
| 2379 | if type.is_value_type then | |
| 2380 | Values.Call.STRUCT(receiver, operator, LIST[Value]([argument]), bool_type) | |
| 2381 | else | |
| 2382 | Values.Call.INSTANCE(receiver, operator, LIST[Value]([argument]), bool_type) | |
| 2383 | fi | |
| 2384 | ||
| 2385 | _begin() | |
| 2386 | gen(call) | |
| 2387 | let compared = _end() | |
| 2388 | ||
| 2389 | let right_present = LIST[INSTRUCTION]() | |
| 2390 | ||
| 2391 | right_present.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, right_local)) | |
| 2392 | right_present.add(SIMPLE_INSTRUCTION(Opcode.REF_IS_NULL)) | |
| 2393 | right_present.add( | |
| 2394 | IF_INSTRUCTION( | |
| 2395 | BLOCK_TYPE.of_value(VALUE_TYPE.of_numeric(NumericType.I32)), | |
| 2396 | LIST[INSTRUCTION]([I32_CONSTANT(0)]: INSTRUCTION), | |
| 2397 | compared)) | |
| 2398 | ||
| 2399 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, left_local)) | |
| 2400 | _emit(Opcode.REF_IS_NULL) | |
| 2401 | _emit( | |
| 2402 | IF_INSTRUCTION( | |
| 2403 | BLOCK_TYPE.of_value(VALUE_TYPE.of_numeric(NumericType.I32)), | |
| 2404 | LIST[INSTRUCTION]([ | |
| 2405 | INDEX_INSTRUCTION(Opcode.LOCAL_GET, right_local), | |
| 2406 | SIMPLE_INSTRUCTION(Opcode.REF_IS_NULL) | |
| 2407 | ]: INSTRUCTION), | |
| 2408 | right_present)) | |
| 2409 | si | |
| 2410 | ||
| 2411 | // A tuple declares no `=~` and is compared element by element, each | |
| 2412 | // element as `=~` compares its own type, stopping at the first that | |
| 2413 | // differs. A tuple is a struct and is never absent. | |
| 2414 | _equal_tuples(left: Value, right: Value, tuple: Semantic.Types.TUPLE) is | |
| 2415 | let held = _struct_of(tuple) | |
| 2416 | ||
| 2417 | if !held? then | |
| 2418 | _report("an equality comparison of {tuple}") | |
| 2419 | return | |
| 2420 | fi | |
| 2421 | ||
| 2422 | if _registry.discovering then | |
| 2423 | gen(left) | |
| 2424 | gen(right) | |
| 2425 | return | |
| 2426 | fi | |
| 2427 | ||
| 2428 | let type_index = _registry.type_index(held)! | |
| 2429 | let tuple_type = VALUE_TYPE.of_type_index(type_index, true) | |
| 2430 | let field_types = _registry.field_value_types(held) | |
| 2431 | let element_types = tuple.arguments | |
| 2432 | let left_local = _temp(tuple_type) | |
| 2433 | let right_local = _temp(tuple_type) | |
| 2434 | ||
| 2435 | gen(left) | |
| 2436 | _coerce_top(tuple_type) | |
| 2437 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, left_local)) | |
| 2438 | ||
| 2439 | gen(right) | |
| 2440 | _coerce_top(tuple_type) | |
| 2441 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, right_local)) | |
| 2442 | ||
| 2443 | for instruction in _equal_tuple_elements(left_local, right_local, type_index, field_types, element_types, 0) do | |
| 2444 | _emit(instruction) | |
| 2445 | od | |
| 2446 | si | |
| 2447 | ||
| 2448 | // The instructions answering whether elements `i` onwards are equal, | |
| 2449 | // as nested ifs so that a difference skips the rest. | |
| 2450 | _equal_tuple_elements( | |
| 2451 | left_local: int, | |
| 2452 | right_local: int, | |
| 2453 | type_index: int, | |
| 2454 | field_types: Collections.List[VALUE_TYPE], | |
| 2455 | element_types: Collections.List[Type], | |
| 2456 | i: int | |
| 2457 | ) -> Collections.List[INSTRUCTION] is | |
| 2458 | if i >= element_types.count then | |
| 2459 | return LIST[INSTRUCTION]([I32_CONSTANT(1)]: INSTRUCTION) | |
| 2460 | fi | |
| 2461 | ||
| 2462 | let element_type = element_types[i] | |
| 2463 | let field_type = field_types[i + 1] | |
| 2464 | let left_element = _temp(field_type) | |
| 2465 | let right_element = _temp(field_type) | |
| 2466 | ||
| 2467 | _begin() | |
| 2468 | ||
| 2469 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, left_local)) | |
| 2470 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, type_index, i + 1)) | |
| 2471 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, left_element)) | |
| 2472 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, right_local)) | |
| 2473 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, type_index, i + 1)) | |
| 2474 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, right_element)) | |
| 2475 | ||
| 2476 | _equal_values(WASMGC_LOCAL_VALUE(left_element, element_type), WASMGC_LOCAL_VALUE(right_element, element_type), element_type) | |
| 2477 | ||
| 2478 | let rest = _equal_tuple_elements(left_local, right_local, type_index, field_types, element_types, i + 1) | |
| 2479 | ||
| 2480 | _emit( | |
| 2481 | IF_INSTRUCTION( | |
| 2482 | BLOCK_TYPE.of_value(VALUE_TYPE.of_numeric(NumericType.I32)), | |
| 2483 | rest, | |
| 2484 | LIST[INSTRUCTION]([I32_CONSTANT(0)]: INSTRUCTION))) | |
| 2485 | ||
| 2486 | return _end() | |
| 2487 | si | |
| 2488 | ||
| 2489 | // Integers are equal where their bits are; floating values where | |
| 2490 | // they compare equal, or where both are NaN. | |
| 2491 | _equal_scalars(left: Value, right: Value, type: NumericType) is | |
| 2492 | if type == NumericType.I32 \/ type == NumericType.I64 then | |
| 2493 | gen(left) | |
| 2494 | gen(right) | |
| 2495 | _emit(if type == NumericType.I32 then Opcode.I32_EQ else Opcode.I64_EQ fi) | |
| 2496 | return | |
| 2497 | fi | |
| 2498 | ||
| 2499 | let value_type = VALUE_TYPE.of_numeric(type) | |
| 2500 | let left_local = _temp(value_type) | |
| 2501 | let right_local = _temp(value_type) | |
| 2502 | let (eq, ne) = if type == NumericType.F32 then (Opcode.F32_EQ, Opcode.F32_NE) else (Opcode.F64_EQ, Opcode.F64_NE) fi | |
| 2503 | ||
| 2504 | gen(left) | |
| 2505 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, left_local)) | |
| 2506 | gen(right) | |
| 2507 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, right_local)) | |
| 2508 | _emit(eq) | |
| 2509 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, left_local)) | |
| 2510 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, left_local)) | |
| 2511 | _emit(ne) | |
| 2512 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, right_local)) | |
| 2513 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, right_local)) | |
| 2514 | _emit(ne) | |
| 2515 | _emit(Opcode.I32_AND) | |
| 2516 | _emit(Opcode.I32_OR) | |
| 2517 | si | |
| 2518 | ||
| 2519 | // An optional reference holds a value when it is not null. | |
| 2520 | visit(test: Values.HAS_VALUE) is | |
| 2521 | // A box of a value that is never absent is always present, so the | |
| 2522 | // value is evaluated for its effects and nothing is allocated. | |
| 2523 | if let boxed = _boxed_present_value(test.value) then | |
| 2524 | gen(boxed) | |
| 2525 | _emit(Opcode.DROP) | |
| 2526 | _emit(I32_CONSTANT(1)) | |
| 2527 | ||
| 2528 | return | |
| 2529 | fi | |
| 2530 | ||
| 2531 | gen(test.value) | |
| 2532 | ||
| 2533 | if !_top?.heap_type()? then | |
| 2534 | _report("an optional value of type {test.value.type}") | |
| 2535 | return | |
| 2536 | fi | |
| 2537 | ||
| 2538 | _emit(Opcode.REF_IS_NULL) | |
| 2539 | _emit(Opcode.I32_EQZ) | |
| 2540 | si | |
| 2541 | // `!` on a reference optional raises the core library's | |
| 2542 | // NullReferenceException where the value is absent, or traps where | |
| 2543 | // the library declares none. | |
| 2544 | visit(check: Values.REFERENCE_NULL_CHECK) is | |
| 2545 | gen(check.inner) | |
| 2546 | ||
| 2547 | let held = _top | |
| 2548 | ||
| 2549 | if !held? \/ !held.heap_type()? then | |
| 2550 | _report("a null check of {check.inner.type}") | |
| 2551 | return | |
| 2552 | fi | |
| 2553 | ||
| 2554 | if !_can_throw(Semantic.Lookups.RuntimeException.NULL_REFERENCE) then | |
| 2555 | _emit(Opcode.REF_AS_NON_NULL) | |
| 2556 | return | |
| 2557 | fi | |
| 2558 | ||
| 2559 | let value = _temp(held) | |
| 2560 | ||
| 2561 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, value)) | |
| 2562 | _emit(Opcode.REF_IS_NULL) | |
| 2563 | _throw_if(Semantic.Lookups.RuntimeException.NULL_REFERENCE, LIST[Value]()) | |
| 2564 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, value)) | |
| 2565 | _emit(Opcode.REF_AS_NON_NULL) | |
| 2566 | si | |
| 2567 | ||
| 2568 | // Whether the module can throw the core library's exception `name`: | |
| 2569 | // the library declares the class, and the module has an exception tag, | |
| 2570 | // which is asked for only then, since a library that declares no | |
| 2571 | // exceptions has none. Where it cannot, the operation is left to trap. | |
| 2572 | _can_throw(kind: Semantic.Lookups.RuntimeException) -> bool => | |
| 2573 | CORE_LIBRARY.exception_class(kind)? /\ !_registry.discovering /\ | |
| 2574 | _exceptions?.tag_index? /\ _exceptions?.exception? | |
| 2575 | ||
| 2576 | // Takes the i32 condition on the stack and, where it holds, throws | |
| 2577 | // the core library's exception for `kind` built from `operands`. Only | |
| 2578 | // called where `_can_throw(kind)`. | |
| 2579 | _throw_if(kind: Semantic.Lookups.RuntimeException, operands: Collections.List[Value]) is | |
| 2580 | let tag = _exceptions?.tag_index | |
| 2581 | let exception_type = _exceptions?.exception | |
| 2582 | ||
| 2583 | if !tag? \/ !exception_type? then | |
| 2584 | _emit(Opcode.DROP) | |
| 2585 | return | |
| 2586 | fi | |
| 2587 | ||
| 2588 | // The throw leaves the stack as it found it, so what is on top | |
| 2589 | // afterwards is what was there before the exception was built. | |
| 2590 | let top_value = _top_value | |
| 2591 | let top_cast = _top_cast | |
| 2592 | ||
| 2593 | _begin() | |
| 2594 | _construct_runtime_exception(kind, operands) | |
| 2595 | _coerce_top(exception_type) | |
| 2596 | _emit(Opcode.REF_AS_NON_NULL) | |
| 2597 | _emit(THROW_INSTRUCTION(tag)) | |
| 2598 | let raised = _end() | |
| 2599 | ||
| 2600 | _top_value = top_value | |
| 2601 | _top_cast = top_cast | |
| 2602 | ||
| 2603 | _emit(IF_INSTRUCTION(BLOCK_TYPE(), raised, LIST[INSTRUCTION]())) | |
| 2604 | si | |
| 2605 | ||
| 2606 | visit(conversion: Values.SCALAR_CONVERSION) is | |
| 2607 | gen(conversion.converted) | |
| 2608 | si | |
| 2609 | ||
| 2610 | visit(declaration: Values.DECLARE_LOCAL) is | |
| 2611 | _local(declaration.local) | |
| 2612 | si | |
| 2613 | ||
| 2614 | visit(store: Values.STORE_TEMP) is | |
| 2615 | _normalise_for(store.local.type) | |
| 2616 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, _local(store.local))) | |
| 2617 | si | |
| 2618 | ||
| 2619 | visit(tee: Values.TEE_TEMP) is | |
| 2620 | gen(tee.value) | |
| 2621 | ||
| 2622 | _normalise_for(tee.local.type) | |
| 2623 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, _local(tee.local))) | |
| 2624 | si | |
| 2625 | ||
| 2626 | visit(discard: Values.DISCARD) is | |
| 2627 | if let value = discard.value then | |
| 2628 | gen(value) | |
| 2629 | ||
| 2630 | if WASMGC_TYPES.is_void(value.type) then | |
| 2631 | return | |
| 2632 | fi | |
| 2633 | fi | |
| 2634 | ||
| 2635 | _emit(Opcode.DROP) | |
| 2636 | si | |
| 2637 | ||
| 2638 | ||
| 2639 | visit(ret: Values.RET) is | |
| 2640 | _normalise_for(_return_type) | |
| 2641 | _emit(Opcode.RETURN) | |
| 2642 | si | |
| 2643 | ||
| 2644 | visit(default_return: Values.DEFAULT_RETURN) is | |
| 2645 | if !WASMGC_TYPES.is_void(default_return.type) then | |
| 2646 | _zero(default_return.type) | |
| 2647 | fi | |
| 2648 | si | |
| 2649 | ||
| 2650 | visit(size_of: Values.SIZE_OF) is | |
| 2651 | _emit(I32_CONSTANT(_size_of(size_of.measured))) | |
| 2652 | si | |
| 2653 | ||
| 2654 | // The bytes a value of `type` takes where it is stored, as an array's | |
| 2655 | // element holds it: a numeric value its width, and anything held by | |
| 2656 | // reference the four bytes of a reference. | |
| 2657 | _size_of(type: Type) -> int is | |
| 2658 | if let numeric = _value_type(_specialize(type))?.numeric() then | |
| 2659 | return | |
| 2660 | case numeric | |
| 2661 | when NumericType.I64, NumericType.F64 then 8 | |
| 2662 | else 4 | |
| 2663 | esac | |
| 2664 | fi | |
| 2665 | ||
| 2666 | return 4 | |
| 2667 | si | |
| 2668 | ||
| 2669 | visit(default_value: Values.DEFAULT) is | |
| 2670 | _zero(default_value.type) | |
| 2671 | si | |
| 2672 | ||
| 2673 | // A discarded `ref` argument is a fresh cell holding the default | |
| 2674 | // value, which the callee writes and nothing reads. | |
| 2675 | visit_address(default_value: Values.DEFAULT) is | |
| 2676 | if let held = _struct_of(default_value.type) ?? _registry.box_type(_specialize(default_value.type)) then | |
| 2677 | _emit_all(_registry.new_instance(held)) | |
| 2678 | else | |
| 2679 | _report("the address of a default value") | |
| 2680 | fi | |
| 2681 | si | |
| 2682 | // A reference to a struct is its box, and one to a scalar or a value | |
| 2683 | // of reference type the cell the value lives in. Dereferencing one | |
| 2684 | // reads it, | |
| 2685 | // which for a mutable struct is a copy. | |
| 2686 | visit(deref: Values.DEREF) is | |
| 2687 | if _struct_of(deref.type)? then | |
| 2688 | gen(deref.value) | |
| 2689 | _copy_if_mutable(deref.type) | |
| 2690 | elif let cell = _registry.box_index(_specialize(deref.type)) then | |
| 2691 | gen(deref.value) | |
| 2692 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, cell, WASMGC_TYPE_REGISTRY.BOX_VALUE)) | |
| 2693 | elif let held = _registry.reference_cell_value_type(_specialize(deref.type)) then | |
| 2694 | gen(deref.value) | |
| 2695 | _reference_cell_get(held) | |
| 2696 | else | |
| 2697 | _report("a reference") | |
| 2698 | fi | |
| 2699 | si | |
| 2700 | ||
| 2701 | visit_address(deref: Values.DEREF) is | |
| 2702 | if _struct_of(deref.type)? then | |
| 2703 | gen(deref.value) | |
| 2704 | else | |
| 2705 | _report("a reference") | |
| 2706 | fi | |
| 2707 | si | |
| 2708 | ||
| 2709 | // The address of a struct is the box it lives in, or for a value | |
| 2710 | // with no location of its own, the box the value already is. | |
| 2711 | visit(address: Values.ADDRESS) is | |
| 2712 | if _struct_of(address.value.type)? then | |
| 2713 | _struct_receiver(address.value) | |
| 2714 | elif isa Semantic.Types.REFERENCE(_specialize(address.type)) /\ isa Values.Load.LOCAL_VARIABLE(address.value) /\ _has_cell(address.value.type) then | |
| 2715 | // A `ref` to a local is the cell the local lives in, which | |
| 2716 | // the callee writes through, whatever the local holds. | |
| 2717 | gen_address(address.value) | |
| 2718 | elif _optional_inner(address.value.type)? then | |
| 2719 | gen(address.value) | |
| 2720 | elif _has_cell(address.value.type) then | |
| 2721 | gen_address(address.value) | |
| 2722 | else | |
| 2723 | _report("a reference") | |
| 2724 | fi | |
| 2725 | si | |
| 2726 | ||
| 2727 | visit_address(address: Values.ADDRESS) is | |
| 2728 | visit(address) | |
| 2729 | si | |
| 2730 | ||
| 2731 | visit(steps: Values.STEPS) is | |
| 2732 | _lower_all(steps.steps) | |
| 2733 | si | |
| 2734 | ||
| 2735 | // Decrements the counter in place, leaving nothing. | |
| 2736 | visit(decrement: Values.DECREMENT) is | |
| 2737 | let counter = _local(decrement.counter) | |
| 2738 | ||
| 2739 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, counter)) | |
| 2740 | _emit(I32_CONSTANT(1)) | |
| 2741 | _emit(Opcode.I32_SUB) | |
| 2742 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, counter)) | |
| 2743 | si | |
| 2744 | ||
| 2745 | // Leaves the counter's value before the increment, and stores the | |
| 2746 | // incremented value back. | |
| 2747 | visit(increment: Values.POST_INCREMENT) is | |
| 2748 | let counter = _local(increment.counter) | |
| 2749 | ||
| 2750 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, counter)) | |
| 2751 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, counter)) | |
| 2752 | _emit(I32_CONSTANT(1)) | |
| 2753 | _emit(Opcode.I32_ADD) | |
| 2754 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, counter)) | |
| 2755 | si | |
| 2756 | ||
| 2757 | // Leaves the counter's value after the decrement. | |
| 2758 | visit(decrement: Values.PRE_DECREMENT) is | |
| 2759 | let counter = _local(decrement.counter) | |
| 2760 | ||
| 2761 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, counter)) | |
| 2762 | _emit(I32_CONSTANT(1)) | |
| 2763 | _emit(Opcode.I32_SUB) | |
| 2764 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, counter)) | |
| 2765 | si | |
| 2766 | ||
| 2767 | // The source is held in a local and the target built from its | |
| 2768 | // elements. | |
| 2769 | visit(repack: Values.TUPLE_REPACK) is | |
| 2770 | gen(repack.source) | |
| 2771 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, _local(repack.temp))) | |
| 2772 | gen(repack.built) | |
| 2773 | si | |
| 2774 | ||
| 2775 | visit(spilled: Values.SPILLED_SUBJECT) is | |
| 2776 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, _local(spilled.temp))) | |
| 2777 | ||
| 2778 | gen(spilled.built) | |
| 2779 | si | |
| 2780 | ||
| 2781 | visit(null_value: Values.NULL) is | |
| 2782 | _zero(null_value.type) | |
| 2783 | si | |
| 2784 | // The value is lowered before the descriptor is pushed, as a value | |
| 2785 | // can read an operand its enclosing construct left on the stack: | |
| 2786 | // the member call a coalescing access makes reads its receiver there. | |
| 2787 | _box(value: Value, box_type: CLASS_INSTANCE, index: int) is | |
| 2788 | _registry.note_boxed(box_type) | |
| 2789 | ||
| 2790 | gen(value) | |
| 2791 | ||
| 2792 | let held = _temp(_value_type(value.type)!) | |
| 2793 | ||
| 2794 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, held)) | |
| 2795 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.descriptor_global(box_type))) | |
| 2796 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, held)) | |
| 2797 | _emit(TYPE_INSTRUCTION(Opcode.STRUCT_NEW, index)) | |
| 2798 | si | |
| 2799 | ||
| 2800 | // A scalar is boxed in a struct carrying its type's descriptor, so | |
| 2801 | // a boxed int and a boxed long answer type tests differently. A | |
| 2802 | // reference needs no box. | |
| 2803 | visit(box: Values.BOX) is | |
| 2804 | let value = box.value | |
| 2805 | ||
| 2806 | if let index = _registry.box_index(_specialize(value.type)) then | |
| 2807 | _box(value, _registry.box_type(_specialize(value.type))!, index) | |
| 2808 | elif _value_type(value.type)?.heap_type()? then | |
| 2809 | gen(value) | |
| 2810 | else | |
| 2811 | _report("boxing a {value.type}") | |
| 2812 | fi | |
| 2813 | si | |
| 2814 | ||
| 2815 | // A null or a box of another type traps, where .NET throws. | |
| 2816 | // Unboxing through a type parameter follows the cast that tested | |
| 2817 | // the box, so there a null is the declined cast and gives the | |
| 2818 | // default, and a reference type is the reference itself. | |
| 2819 | visit(unbox: Values.UNBOX) is | |
| 2820 | gen(unbox.value) | |
| 2821 | ||
| 2822 | if unbox.type?.is_type_variable ?? false then | |
| 2823 | if let defaulted = _default_box(unbox.type) then | |
| 2824 | let held = _temp(_registry.object_reference) | |
| 2825 | ||
| 2826 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, held)) | |
| 2827 | _emit(Opcode.REF_IS_NULL) | |
| 2828 | ||
| 2829 | defaulted.add(INDEX_INSTRUCTION(Opcode.LOCAL_SET, held)) | |
| 2830 | ||
| 2831 | _emit(IF_INSTRUCTION(BLOCK_TYPE(), defaulted, LIST[INSTRUCTION]())) | |
| 2832 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, held)) | |
| 2833 | elif let heap = _value_type(unbox.type)?.heap_type() then | |
| 2834 | if !(_specialize(unbox.type)?.is_object ?? false) then | |
| 2835 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST_NULL, heap)) | |
| 2836 | fi | |
| 2837 | ||
| 2838 | return | |
| 2839 | fi | |
| 2840 | fi | |
| 2841 | ||
| 2842 | _unbox_top(unbox.type) | |
| 2843 | si | |
| 2844 | ||
| 2845 | // Unboxes the reference on the stack to a value of `type`. | |
| 2846 | _unbox_top(type: Type?) is | |
| 2847 | if let index = _registry.box_index(_specialize(type)) then | |
| 2848 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST, HEAP_TYPE.of_type_index(index))) | |
| 2849 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, index, WASMGC_TYPE_REGISTRY.BOX_VALUE)) | |
| 2850 | elif let held = _struct_of(type) then | |
| 2851 | // A boxed struct is the struct's own box, so unboxing it | |
| 2852 | // is a cast, and for a mutable struct a copy. | |
| 2853 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST, HEAP_TYPE.of_type_index(_registry.type_index(held)!))) | |
| 2854 | _copy_if_mutable(type) | |
| 2855 | elif let index = _optional_box_index(type) then | |
| 2856 | // A present value-type optional is the box itself. | |
| 2857 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST_NULL, HEAP_TYPE.of_type_index(index))) | |
| 2858 | elif let held = _optional_struct_of(type) then | |
| 2859 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST_NULL, HEAP_TYPE.of_type_index(_registry.type_index(held)!))) | |
| 2860 | _copy_if_mutable_or_null(held) | |
| 2861 | else | |
| 2862 | _report("unboxing a {type}") | |
| 2863 | fi | |
| 2864 | si | |
| 2865 | ||
| 2866 | // The instructions that make a box holding the default value of | |
| 2867 | // `type`, a scalar or a struct, or null where it is neither. | |
| 2868 | _default_box(type: Type?) -> LIST[INSTRUCTION]? is | |
| 2869 | if let held = _struct_of(type) then | |
| 2870 | return LIST[INSTRUCTION](_registry.new_instance(held)) | |
| 2871 | elif let index = _registry.box_index(_specialize(type)), box_type = _registry.box_type(_specialize(type)), value_type = _value_type(type) then | |
| 2872 | _registry.note_boxed(box_type) | |
| 2873 | ||
| 2874 | return LIST[INSTRUCTION]([ | |
| 2875 | INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.descriptor_global(box_type)), | |
| 2876 | WASMGC_TYPE_REGISTRY.default_value(value_type), | |
| 2877 | TYPE_INSTRUCTION(Opcode.STRUCT_NEW, index) | |
| 2878 | ]: INSTRUCTION) | |
| 2879 | fi | |
| 2880 | ||
| 2881 | return null | |
| 2882 | si | |
| 2883 | // A cast to a class the program declares is checked against the | |
| 2884 | // struct type, which holds exactly that class and those derived | |
| 2885 | // from it. A cast that faults on failure keeps null, as CIL's | |
| 2886 | // castclass does; one that declines answers null instead. | |
| 2887 | visit(conversion: Values.CAST) is | |
| 2888 | let value = _type_test_operand(conversion.value) | |
| 2889 | ||
| 2890 | if !value? then | |
| 2891 | _report("a cast from {conversion.value.type}") | |
| 2892 | return | |
| 2893 | fi | |
| 2894 | ||
| 2895 | if let ids = _trait_class_ids(conversion.type) then | |
| 2896 | _cast_to_trait(value, ids, conversion.faults_on_failure) | |
| 2897 | return | |
| 2898 | fi | |
| 2899 | ||
| 2900 | // Array types sharing a struct type are told apart by class | |
| 2901 | // number, as a trait's implementors are. | |
| 2902 | if let array = _registry.array_of(_specialize(conversion.type)) then | |
| 2903 | _cast_to_trait(value, _registry.array_class_ids(array), conversion.faults_on_failure) | |
| 2904 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST_NULL, HEAP_TYPE.of_type_index(_registry.array_storage(array).struct_index))) | |
| 2905 | return | |
| 2906 | fi | |
| 2907 | ||
| 2908 | if let held = _optional_struct_of(conversion.type) then | |
| 2909 | _cast_to_optional_struct(value, held, conversion.faults_on_failure) | |
| 2910 | return | |
| 2911 | fi | |
| 2912 | ||
| 2913 | let target = _registry.type_index(_specialize(conversion.type)) ?? _optional_box_index(conversion.type) | |
| 2914 | ||
| 2915 | if !target? then | |
| 2916 | if _is_unchecked_widening(conversion.type) then | |
| 2917 | gen(value) | |
| 2918 | else | |
| 2919 | _report("a cast to {conversion.type}") | |
| 2920 | fi | |
| 2921 | ||
| 2922 | return | |
| 2923 | fi | |
| 2924 | ||
| 2925 | gen(value) | |
| 2926 | ||
| 2927 | let target_heap = HEAP_TYPE.of_type_index(target) | |
| 2928 | ||
| 2929 | let source_type mut = _top! | |
| 2930 | ||
| 2931 | if _registry.is_held_as(source_type.heap_type()!, target_heap) then | |
| 2932 | return | |
| 2933 | fi | |
| 2934 | ||
| 2935 | // A cast tests from a type the target is held as, which an | |
| 2936 | // operand of an unrelated type, as a type parameter can be, is | |
| 2937 | // not: it is tested as an object instead. | |
| 2938 | if !_registry.is_held_as(target_heap, source_type.heap_type()!) then | |
| 2939 | source_type = _registry.object_reference | |
| 2940 | fi | |
| 2941 | ||
| 2942 | // A cast that faults throws InvalidCastException, as .NET's | |
| 2943 | // castclass does, where the core library declares it, and | |
| 2944 | // traps otherwise. A null passes. | |
| 2945 | if conversion.faults_on_failure then | |
| 2946 | _check_cast(source_type, target_heap) | |
| 2947 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST_NULL, target_heap)) | |
| 2948 | return | |
| 2949 | fi | |
| 2950 | ||
| 2951 | let held = _temp(source_type) | |
| 2952 | ||
| 2953 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, held)) | |
| 2954 | ||
| 2955 | let body = LIST[INSTRUCTION]() | |
| 2956 | ||
| 2957 | body.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, held)) | |
| 2958 | body.add(CAST_BRANCH_INSTRUCTION(Opcode.BR_ON_CAST, 0, source_type.heap_type()!, true, target_heap, false)) | |
| 2959 | body.add(SIMPLE_INSTRUCTION(Opcode.DROP)) | |
| 2960 | body.add(HEAP_TYPE_INSTRUCTION(Opcode.REF_NULL, target_heap)) | |
| 2961 | ||
| 2962 | _emit(BLOCK_INSTRUCTION(Opcode.BLOCK, BLOCK_TYPE.of_value(VALUE_TYPE.of_type_index(target, true)), body)) | |
| 2963 | si | |
| 2964 | ||
| 2965 | // A cast to an optional struct answers its box, or null where the | |
| 2966 | // value is some other type, and copies a mutable struct as an | |
| 2967 | // unbox does. | |
| 2968 | _cast_to_optional_struct(value: Value, held: CLASS_INSTANCE, faults: bool) is | |
| 2969 | let index = _registry.type_index(held)! | |
| 2970 | let target_heap = HEAP_TYPE.of_type_index(index) | |
| 2971 | ||
| 2972 | gen(value) | |
| 2973 | ||
| 2974 | if faults then | |
| 2975 | _check_cast(_top!, target_heap) | |
| 2976 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST_NULL, target_heap)) | |
| 2977 | else | |
| 2978 | let source_type = _top! | |
| 2979 | let source = _temp(source_type) | |
| 2980 | ||
| 2981 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, source)) | |
| 2982 | ||
| 2983 | let body = LIST[INSTRUCTION]() | |
| 2984 | ||
| 2985 | body.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, source)) | |
| 2986 | body.add(CAST_BRANCH_INSTRUCTION(Opcode.BR_ON_CAST, 0, source_type.heap_type()!, true, target_heap, false)) | |
| 2987 | body.add(SIMPLE_INSTRUCTION(Opcode.DROP)) | |
| 2988 | body.add(HEAP_TYPE_INSTRUCTION(Opcode.REF_NULL, target_heap)) | |
| 2989 | ||
| 2990 | _emit(BLOCK_INSTRUCTION(Opcode.BLOCK, BLOCK_TYPE.of_value(VALUE_TYPE.of_type_index(index, true)), body)) | |
| 2991 | fi | |
| 2992 | ||
| 2993 | _copy_if_mutable_or_null(held) | |
| 2994 | si | |
| 2995 | ||
| 2996 | // Whether a read through `from` is tested for null first: not where it | |
| 2997 | // is the receiver, which its caller tested, nor where it is a value. | |
| 2998 | _is_null_checked(from: Value?) -> bool => | |
| 2999 | from? /\ !from.is_value_type /\ !isa Values.Load.REFERENCE_SELF(from) /\ !isa Values.Load.RECEIVER(from) | |
| 3000 | ||
| 3001 | // Throws the core library's NullReferenceException where the reference | |
| 3002 | // in the local `held` is null, leaving the stack as it was. Where the | |
| 3003 | // library declares none, the read after it traps instead. | |
| 3004 | _throw_if_null(held: int) is | |
| 3005 | if !_can_throw(Semantic.Lookups.RuntimeException.NULL_REFERENCE) then | |
| 3006 | return | |
| 3007 | fi | |
| 3008 | ||
| 3009 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, held)) | |
| 3010 | _emit(Opcode.REF_IS_NULL) | |
| 3011 | _throw_if(Semantic.Lookups.RuntimeException.NULL_REFERENCE, LIST[Value]()) | |
| 3012 | si | |
| 3013 | ||
| 3014 | // Throws the core library's NullReferenceException where the reference | |
| 3015 | // on the stack, of type `type`, is null, leaving it where it was. | |
| 3016 | _check_not_null(type: VALUE_TYPE) is | |
| 3017 | if !_can_throw(Semantic.Lookups.RuntimeException.NULL_REFERENCE) then | |
| 3018 | return | |
| 3019 | fi | |
| 3020 | ||
| 3021 | let held = _temp(type) | |
| 3022 | ||
| 3023 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, held)) | |
| 3024 | _throw_if_null(held) | |
| 3025 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, held)) | |
| 3026 | si | |
| 3027 | ||
| 3028 | // Throws the core library's InvalidCastException where the reference | |
| 3029 | // on the stack, of type `source`, is neither null nor an instance of | |
| 3030 | // `target`, leaving the reference where it was. Where the library | |
| 3031 | // declares no such exception, the cast after it traps instead. | |
| 3032 | _check_cast(source: VALUE_TYPE, target: HEAP_TYPE) is | |
| 3033 | if !_can_throw(Semantic.Lookups.RuntimeException.INVALID_CAST) then | |
| 3034 | return | |
| 3035 | fi | |
| 3036 | ||
| 3037 | let held = _temp(source) | |
| 3038 | ||
| 3039 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, held)) | |
| 3040 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_TEST_NULL, target)) | |
| 3041 | _emit(Opcode.I32_EQZ) | |
| 3042 | _throw_if(Semantic.Lookups.RuntimeException.INVALID_CAST, LIST[Value]()) | |
| 3043 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, held)) | |
| 3044 | si | |
| 3045 | ||
| 3046 | // Takes the i32 condition on the stack, true where a cast has failed, | |
| 3047 | // and throws the core library's InvalidCastException, or traps where | |
| 3048 | // the library declares none. | |
| 3049 | _fail_cast() is | |
| 3050 | if _can_throw(Semantic.Lookups.RuntimeException.INVALID_CAST) then | |
| 3051 | _throw_if(Semantic.Lookups.RuntimeException.INVALID_CAST, LIST[Value]()) | |
| 3052 | else | |
| 3053 | _emit(IF_INSTRUCTION(BLOCK_TYPE(), [SIMPLE_INSTRUCTION(Opcode.UNREACHABLE)]: INSTRUCTION, LIST[INSTRUCTION]())) | |
| 3054 | fi | |
| 3055 | si | |
| 3056 | ||
| 3057 | // A cast to a trait answers the value where its class implements | |
| 3058 | // the trait. Otherwise it throws where the cast faults on failure, | |
| 3059 | // and answers null where it declines. | |
| 3060 | _cast_to_trait(value: Value, ids: Collections.List[int], faults: bool) is | |
| 3061 | let held = _temp(_registry.object_reference) | |
| 3062 | ||
| 3063 | gen(value) | |
| 3064 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, held)) | |
| 3065 | _test_class_ids(ids) | |
| 3066 | ||
| 3067 | let failed = LIST[INSTRUCTION]() | |
| 3068 | ||
| 3069 | if faults then | |
| 3070 | _begin() | |
| 3071 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, held)) | |
| 3072 | _emit(Opcode.REF_IS_NULL) | |
| 3073 | _emit(Opcode.I32_EQZ) | |
| 3074 | _fail_cast() | |
| 3075 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, held)) | |
| 3076 | failed.add_range(_end()) | |
| 3077 | else | |
| 3078 | failed.add(HEAP_TYPE_INSTRUCTION(Opcode.REF_NULL, _registry.object_reference.heap_type()!)) | |
| 3079 | fi | |
| 3080 | ||
| 3081 | _emit( | |
| 3082 | IF_INSTRUCTION( | |
| 3083 | BLOCK_TYPE.of_value(_registry.object_reference), | |
| 3084 | [INDEX_INSTRUCTION(Opcode.LOCAL_GET, held)]: INSTRUCTION, | |
| 3085 | failed)) | |
| 3086 | si | |
| 3087 | ||
| 3088 | // An instance of a class the program declares is tested against | |
| 3089 | // its struct type; null is not an instance of anything. | |
| 3090 | visit(test: Values.ISA) is | |
| 3091 | let value = _type_test_operand(test.value) | |
| 3092 | ||
| 3093 | if !value? then | |
| 3094 | _report("a type test of {test.value.type}") | |
| 3095 | return | |
| 3096 | fi | |
| 3097 | ||
| 3098 | if let array = _registry.array_of(_specialize(test.isa_type)) then | |
| 3099 | gen(value) | |
| 3100 | ||
| 3101 | _test_class_ids(_registry.array_class_ids(array)) | |
| 3102 | elif let target = _registry.type_index(_specialize(test.isa_type)) then | |
| 3103 | gen(value) | |
| 3104 | ||
| 3105 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_TEST, HEAP_TYPE.of_type_index(target))) | |
| 3106 | elif let ids = _trait_class_ids(test.isa_type) then | |
| 3107 | gen(value) | |
| 3108 | ||
| 3109 | _test_class_ids(ids) | |
| 3110 | else | |
| 3111 | _report("a type test against {test.isa_type}") | |
| 3112 | fi | |
| 3113 | si | |
| 3114 | ||
| 3115 | // The numbers of the classes implementing `type`, where it is a | |
| 3116 | // trait that is not generic. | |
| 3117 | _trait_class_ids(type: Type?) -> Collections.List[int]? is | |
| 3118 | if !type? \/ !type.is_trait then | |
| 3119 | return null | |
| 3120 | fi | |
| 3121 | ||
| 3122 | if let trait_type = _registry.dispatch.trait_of(_specialize(type)!) then | |
| 3123 | let ids = LIST[int]() | |
| 3124 | ||
| 3125 | if let implementors = _registry.dispatch.implementors(trait_type) then | |
| 3126 | for implementor in implementors do | |
| 3127 | ids.add(_registry.numbering.id(implementor)) | |
| 3128 | od | |
| 3129 | fi | |
| 3130 | ||
| 3131 | return ids | |
| 3132 | fi | |
| 3133 | ||
| 3134 | return null | |
| 3135 | si | |
| 3136 | ||
| 3137 | // Replaces the reference on the stack with whether it is an object | |
| 3138 | // whose class number is among `ids`. Null is not. | |
| 3139 | _test_class_ids(ids: Collections.List[int]) is | |
| 3140 | let held = _temp(_registry.object_reference) | |
| 3141 | let id = _temp(VALUE_TYPE.of_numeric(NumericType.I32)) | |
| 3142 | ||
| 3143 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, held)) | |
| 3144 | _emit(Opcode.REF_IS_NULL) | |
| 3145 | ||
| 3146 | let present = LIST[INSTRUCTION]() | |
| 3147 | ||
| 3148 | present.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, held)) | |
| 3149 | present.add(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, WASMGC_TYPE_REGISTRY.OBJECT_TYPE_INDEX, 0)) | |
| 3150 | present.add(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, WASMGC_TYPE_REGISTRY.DESCRIPTOR_TYPE_INDEX, WASMGC_TYPE_REGISTRY.DESCRIPTOR_CLASS_ID)) | |
| 3151 | present.add(INDEX_INSTRUCTION(Opcode.LOCAL_SET, id)) | |
| 3152 | present.add(I32_CONSTANT(0)) | |
| 3153 | ||
| 3154 | for (low, high) in CLASS_ID_RANGES.of(ids) do | |
| 3155 | present.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, id)) | |
| 3156 | present.add(I32_CONSTANT(low)) | |
| 3157 | present.add(SIMPLE_INSTRUCTION(Opcode.I32_SUB)) | |
| 3158 | present.add(I32_CONSTANT(high - low)) | |
| 3159 | present.add(SIMPLE_INSTRUCTION(Opcode.I32_LE_U)) | |
| 3160 | present.add(SIMPLE_INSTRUCTION(Opcode.I32_OR)) | |
| 3161 | od | |
| 3162 | ||
| 3163 | _emit(IF_INSTRUCTION(BLOCK_TYPE.of_value(VALUE_TYPE.of_numeric(NumericType.I32)), [I32_CONSTANT(0)]: INSTRUCTION, present)) | |
| 3164 | si | |
| 3165 | ||
| 3166 | // The reference a type test examines: the value itself, or a | |
| 3167 | // scalar boxed. Null where the value is neither. A value whose type | |
| 3168 | // is a type parameter is whichever of the two its instantiation is, | |
| 3169 | // since each instantiation of a generic body is lowered on its own. | |
| 3170 | _type_test_operand(value: Value) -> Value? is | |
| 3171 | let mode = Values.TYPE_TEST_OPERAND.mode(value) | |
| 3172 | ||
| 3173 | if value.type?.type_variable_side? then | |
| 3174 | let concrete = _specialize(value.type) | |
| 3175 | ||
| 3176 | if _registry.box_index(concrete)? then | |
| 3177 | return Values.BOX(value) | |
| 3178 | elif _value_type(concrete)?.heap_type()? then | |
| 3179 | return value | |
| 3180 | fi | |
| 3181 | ||
| 3182 | return null | |
| 3183 | fi | |
| 3184 | ||
| 3185 | if mode == Values.TypeTestOperandMode.AS_IS /\ _value_type(value.type)?.heap_type()? then | |
| 3186 | return value | |
| 3187 | elif mode == Values.TypeTestOperandMode.BOX /\ _registry.box_index(_specialize(value.type))? then | |
| 3188 | return Values.TYPE_TEST_OPERAND.operand(value) | |
| 3189 | elif mode == Values.TypeTestOperandMode.BOX /\ _value_type(value.type)?.heap_type()? then | |
| 3190 | // A struct, or a bigint, is held as a reference already, | |
| 3191 | // which is what boxing it gives. | |
| 3192 | return Values.TYPE_TEST_OPERAND.operand(value) | |
| 3193 | elif mode == Values.TypeTestOperandMode.UNWRAP_MAYBE then | |
| 3194 | return Values.TYPE_TEST_OPERAND.operand(value) | |
| 3195 | elif _optional_box_index(value.type)? \/ _optional_struct_of(value.type)? then | |
| 3196 | // A value-type optional is held as its value's box, or null | |
| 3197 | // where it is absent, which is the reference the test reads. | |
| 3198 | return value | |
| 3199 | fi | |
| 3200 | ||
| 3201 | return null | |
| 3202 | si | |
| 3203 | ||
| 3204 | // The box a value-type optional over a scalar is held as, or null | |
| 3205 | // where `type` is not one. | |
| 3206 | _optional_box_index(type: Type?) -> int? is | |
| 3207 | if let optional = _specialize(type) /\ optional.is_optional /\ optional.is_value_type /\ !optional.is_maybe then | |
| 3208 | return _registry.box_index(optional.optional_inner_type) | |
| 3209 | fi | |
| 3210 | ||
| 3211 | return null | |
| 3212 | si | |
| 3213 | ||
| 3214 | // A cast to `object`: every reference is held as one already. | |
| 3215 | _is_unchecked_widening(to: Type?) -> bool => | |
| 3216 | if let to_class: Semantic.Symbols.Classy = to?.symbol then to_class.is_object else false fi | |
| 3217 | visit(type_of: Values.TYPEOF) is | |
| 3218 | if let held = _registry.descriptor_class_of(_specialize(type_of.typeof_type)) then | |
| 3219 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.descriptor_global(held))) | |
| 3220 | _type_object() | |
| 3221 | else | |
| 3222 | _report("typeof {type_of.typeof_type}") | |
| 3223 | fi | |
| 3224 | si | |
| 3225 | // A tuple is a read-only struct built from its elements in order. | |
| 3226 | visit(tuple: Values.TUPLE) is | |
| 3227 | let held = _struct_of(tuple.type) | |
| 3228 | ||
| 3229 | if !held? then | |
| 3230 | _report("a tuple") | |
| 3231 | return | |
| 3232 | fi | |
| 3233 | ||
| 3234 | if _registry.discovering then | |
| 3235 | for value in tuple.values do | |
| 3236 | gen(value) | |
| 3237 | od | |
| 3238 | ||
| 3239 | return | |
| 3240 | fi | |
| 3241 | ||
| 3242 | let field_types = _registry.field_value_types(held) | |
| 3243 | ||
| 3244 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.descriptor_global(held))) | |
| 3245 | ||
| 3246 | for i in 0..tuple.values.count do | |
| 3247 | gen(tuple.values[i]) | |
| 3248 | _coerce_top(field_types[i + 1]) | |
| 3249 | od | |
| 3250 | ||
| 3251 | _emit(TYPE_INSTRUCTION(Opcode.STRUCT_NEW, _registry.type_index(held)!)) | |
| 3252 | si | |
| 3253 | // A new object holds its descriptor and the default value of each | |
| 3254 | // field, and its constructor then runs on it. A unit variant is | |
| 3255 | // the one shared instance. | |
| 3256 | visit(construction: Values.NEW) is | |
| 3257 | let constructor = construction.constructor | |
| 3258 | ||
| 3259 | if constructor.intrinsic_operation =~ "bigint.of_small" then | |
| 3260 | _bigint_of_small(construction.type, construction.arguments[0]) | |
| 3261 | return | |
| 3262 | fi | |
| 3263 | ||
| 3264 | // A constructor the host provides answers the whole value, as a | |
| 3265 | // bigint built from a scalar does. | |
| 3266 | if WASMGC_IMPORTS.is_host(constructor.intrinsic_operation) then | |
| 3267 | _call_host(constructor.intrinsic_operation!, construction.arguments, construction.type) | |
| 3268 | return | |
| 3269 | fi | |
| 3270 | ||
| 3271 | let type = _owner_class(constructor.owner) | |
| 3272 | ||
| 3273 | // The root is constructed like any class: `object()` makes an | |
| 3274 | // instance with no state of its own. | |
| 3275 | if !type? \/ !(_registry.is_object_type(type) \/ _registry.is_object(type)) then | |
| 3276 | _report("constructing a {construction.type}") | |
| 3277 | return | |
| 3278 | fi | |
| 3279 | ||
| 3280 | if type.definition.is_unit_variant then | |
| 3281 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.unit_variant_global(type)!)) | |
| 3282 | return | |
| 3283 | fi | |
| 3284 | ||
| 3285 | _construct(type, constructor, construction.arguments) | |
| 3286 | si | |
| 3287 | ||
| 3288 | _construct(type: CLASS_INSTANCE, constructor: Function, arguments: Collections.List[Value]) is | |
| 3289 | let index_found = _index_of(constructor) | |
| 3290 | ||
| 3291 | if !index_found? then | |
| 3292 | _report("a constructor of {type}") | |
| 3293 | return | |
| 3294 | fi | |
| 3295 | ||
| 3296 | let index = index_found | |
| 3297 | ||
| 3298 | _ensure_initialised(constructor.owner) | |
| 3299 | ||
| 3300 | let instance = _temp(VALUE_TYPE.of_type_index(_registry.type_index(type)!, true)) | |
| 3301 | ||
| 3302 | _emit_all(_registry.new_instance(type)) | |
| 3303 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, instance)) | |
| 3304 | ||
| 3305 | _lower_arguments(constructor, arguments) | |
| 3306 | ||
| 3307 | _emit(INDEX_INSTRUCTION(Opcode.CALL, index)) | |
| 3308 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, instance)) | |
| 3309 | si | |
| 3310 | ||
| 3311 | // Replaces the descriptor on the stack with its type object: the | |
| 3312 | // one it caches, or, the first time, a new core library type object | |
| 3313 | // made from the descriptor's name and cached there, so a type has | |
| 3314 | // one type object however it is asked for. | |
| 3315 | _type_object() is | |
| 3316 | let found = _type_object_constructor() | |
| 3317 | ||
| 3318 | if !found? then | |
| 3319 | _report("a type object") | |
| 3320 | return | |
| 3321 | fi | |
| 3322 | ||
| 3323 | let (type, constructor) = found | |
| 3324 | let string_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_string_type() | |
| 3325 | ||
| 3326 | if _registry.discovering then | |
| 3327 | _construct(type, constructor, [WASMGC_LOCAL_VALUE(0, string_type)]: Value) | |
| 3328 | return | |
| 3329 | fi | |
| 3330 | ||
| 3331 | let descriptor_type = WASMGC_TYPE_REGISTRY.DESCRIPTOR_TYPE_INDEX | |
| 3332 | let cache = WASMGC_TYPE_REGISTRY.DESCRIPTOR_TYPE_OBJECT | |
| 3333 | let type_index = _registry.type_index(type)! | |
| 3334 | let descriptor = _temp(VALUE_TYPE.of_type_index(descriptor_type, false)) | |
| 3335 | let name = _temp(VALUE_TYPE.of_type_index(_strings!.string_type_index, true)) | |
| 3336 | let made = _temp(VALUE_TYPE.of_type_index(type_index, true)) | |
| 3337 | ||
| 3338 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, descriptor)) | |
| 3339 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, descriptor_type, cache)) | |
| 3340 | _emit(Opcode.REF_IS_NULL) | |
| 3341 | ||
| 3342 | _begin() | |
| 3343 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.string_descriptor_global!)) | |
| 3344 | _emit(I32_CONSTANT(0)) | |
| 3345 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, descriptor)) | |
| 3346 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, descriptor_type, WASMGC_TYPE_REGISTRY.DESCRIPTOR_NAME)) | |
| 3347 | _emit(TYPE_INSTRUCTION(Opcode.STRUCT_NEW, _strings!.string_type_index)) | |
| 3348 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, name)) | |
| 3349 | _construct(type, constructor, [WASMGC_LOCAL_VALUE(name, string_type)]: Value) | |
| 3350 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, made)) | |
| 3351 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, descriptor)) | |
| 3352 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, made)) | |
| 3353 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_SET, descriptor_type, cache)) | |
| 3354 | let make = _end() | |
| 3355 | ||
| 3356 | _emit(IF_INSTRUCTION(BLOCK_TYPE(), make, LIST[INSTRUCTION]())) | |
| 3357 | ||
| 3358 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, descriptor)) | |
| 3359 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, descriptor_type, cache)) | |
| 3360 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST, HEAP_TYPE.of_type_index(type_index))) | |
| 3361 | si | |
| 3362 | ||
| 3363 | // Whether a call to `function` asks for its receiver's type object, | |
| 3364 | // which no type can override: a member declaring the same name | |
| 3365 | // hides it. | |
| 3366 | _is_get_type(function: Function) -> bool => | |
| 3367 | cast Function?(function.root_specialized_from)?.intrinsic_operation =~ "object.get_type" | |
| 3368 | ||
| 3369 | // The type object of `call`'s receiver: the one its descriptor | |
| 3370 | // stands for, or, for a scalar, the one its box's does. | |
| 3371 | _get_type(call: Values.Call.INSTANCE) is | |
| 3372 | let receiver: Value mut = call.from | |
| 3373 | ||
| 3374 | if let constrained: Values.CONSTRAINED = receiver then | |
| 3375 | receiver = if let address: Values.ADDRESS = constrained.value then address.value else constrained.value fi | |
| 3376 | ||
| 3377 | if let box = _registry.box_type(_specialize(constrained.type)) then | |
| 3378 | gen(receiver) | |
| 3379 | _emit(Opcode.DROP) | |
| 3380 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.descriptor_global(box))) | |
| 3381 | _type_object() | |
| 3382 | return | |
| 3383 | fi | |
| 3384 | fi | |
| 3385 | ||
| 3386 | gen(receiver) | |
| 3387 | _coerce_top(_registry.object_reference) | |
| 3388 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, WASMGC_TYPE_REGISTRY.OBJECT_TYPE_INDEX, 0)) | |
| 3389 | _type_object() | |
| 3390 | si | |
| 3391 | ||
| 3392 | // The core library's type object class and its constructor taking | |
| 3393 | // the type's name, or null where the library declares none. | |
| 3394 | _type_object_constructor() -> (CLASS_INSTANCE, Function)? is | |
| 3395 | if let constructor = CORE_LIBRARY.constructor(CORE_LIBRARY.type_class(), 1) then | |
| 3396 | if let type = _owner_class(constructor.owner) /\ _registry.is_object_type(type) then | |
| 3397 | return (type, constructor) | |
| 3398 | fi | |
| 3399 | fi | |
| 3400 | ||
| 3401 | return null | |
| 3402 | si | |
| 3403 | ||
| 3404 | // An exception the runtime makes is an instance of the class the | |
| 3405 | // core library declares for it, made by its constructor taking | |
| 3406 | // the operation's operands. Null for any other operation. | |
| 3407 | _runtime_exception(operation: IR.RuntimeOperation) -> Semantic.Lookups.RuntimeException? => | |
| 3408 | case operation | |
| 3409 | when IR.RuntimeOperation.NEW_NOT_IMPLEMENTED then Semantic.Lookups.RuntimeException.NOT_IMPLEMENTED | |
| 3410 | when IR.RuntimeOperation.NEW_NOT_SUPPORTED then Semantic.Lookups.RuntimeException.NOT_SUPPORTED | |
| 3411 | when IR.RuntimeOperation.NEW_ASSERT_FAILED then Semantic.Lookups.RuntimeException.ASSERT_FAILED | |
| 3412 | else null | |
| 3413 | esac | |
| 3414 | ||
| 3415 | _construct_runtime_exception(kind: Semantic.Lookups.RuntimeException, operands: Collections.List[Value]) is | |
| 3416 | if let constructor = CORE_LIBRARY.constructor(CORE_LIBRARY.exception_class(kind), operands.count) then | |
| 3417 | if let type = _owner_class(constructor.owner) /\ _registry.is_object_type(type) then | |
| 3418 | _construct(type, constructor, operands) | |
| 3419 | return | |
| 3420 | fi | |
| 3421 | fi | |
| 3422 | ||
| 3423 | _report("a {Semantic.Lookups.RUNTIME_EXCEPTIONS.name_of(kind)} constructor taking {operands.count} arguments") | |
| 3424 | si | |
| 3425 | ||
| 3426 | visit(negation: Values.NOT) is | |
| 3427 | gen(negation.value) | |
| 3428 | ||
| 3429 | _emit(Opcode.I32_EQZ) | |
| 3430 | si | |
| 3431 | ||
| 3432 | visit(arithmetic: Values.ARITHMETIC) is | |
| 3433 | let numeric = _numeric(arithmetic.type) | |
| 3434 | ||
| 3435 | if !numeric? then | |
| 3436 | _report("arithmetic on {arithmetic.type}") | |
| 3437 | return | |
| 3438 | fi | |
| 3439 | ||
| 3440 | let operation = arithmetic.operation | |
| 3441 | ||
| 3442 | if operation == ArithmeticOperation.NEGATE then | |
| 3443 | _negate(arithmetic.operands[0], numeric) | |
| 3444 | elif operation == ArithmeticOperation.COMPLEMENT then | |
| 3445 | _complement(arithmetic.operands[0], numeric) | |
| 3446 | elif let opcode = WASMGC_OPERATIONS.arithmetic(operation, numeric) then | |
| 3447 | gen(arithmetic.operands[0]) | |
| 3448 | ||
| 3449 | // An unsigned shift of a narrow signed kind shifts in zeros | |
| 3450 | // at the kind's own width, so its sign-extended operand is | |
| 3451 | // first cut back to that width. | |
| 3452 | if operation == ArithmeticOperation.SHIFT_RIGHT_UNSIGNED then | |
| 3453 | let scalar = WASMGC_TYPES.scalar(_specialize(arithmetic.type)) | |
| 3454 | ||
| 3455 | if scalar =~ Scalar.INT8 then | |
| 3456 | _emit_all(WASMGC_OPERATIONS.normalise(Scalar.UINT8)) | |
| 3457 | elif scalar =~ Scalar.INT16 then | |
| 3458 | _emit_all(WASMGC_OPERATIONS.normalise(Scalar.UINT16)) | |
| 3459 | fi | |
| 3460 | fi | |
| 3461 | ||
| 3462 | gen(arithmetic.operands[1]) | |
| 3463 | ||
| 3464 | // A shift count is an int whatever the shifted value is, | |
| 3465 | // and a 64-bit shift takes its count as an i64. | |
| 3466 | if numeric == NumericType.I64 /\ _is_shift(operation) /\ _numeric(arithmetic.operands[1].type) =~ NumericType.I32 then | |
| 3467 | _emit(Opcode.I64_EXTEND_I32_U) | |
| 3468 | fi | |
| 3469 | ||
| 3470 | if _is_division(operation) /\ (numeric == NumericType.I32 \/ numeric == NumericType.I64) then | |
| 3471 | _check_division(operation, numeric, arithmetic.type) | |
| 3472 | fi | |
| 3473 | ||
| 3474 | _emit(opcode) | |
| 3475 | else | |
| 3476 | _report("{operation} on {arithmetic.type}") | |
| 3477 | return | |
| 3478 | fi | |
| 3479 | ||
| 3480 | // A narrow kind's arithmetic runs at 32 bits, so its result is | |
| 3481 | // wrapped back to the kind's width as CIL's narrow stores do. | |
| 3482 | if let scalar = WASMGC_TYPES.scalar(_specialize(arithmetic.type)) then | |
| 3483 | if WASMGC_TYPES.is_narrow(scalar) then | |
| 3484 | _emit_all(WASMGC_OPERATIONS.normalise(scalar)) | |
| 3485 | fi | |
| 3486 | fi | |
| 3487 | si | |
| 3488 | ||
| 3489 | _is_division(operation: ArithmeticOperation) -> bool => | |
| 3490 | operation == ArithmeticOperation.DIVIDE \/ | |
| 3491 | operation == ArithmeticOperation.DIVIDE_UNSIGNED \/ | |
| 3492 | operation == ArithmeticOperation.REMAINDER \/ | |
| 3493 | operation == ArithmeticOperation.REMAINDER_UNSIGNED | |
| 3494 | ||
| 3495 | // With an integer division's two operands on the stack, throws the | |
| 3496 | // core library's DivideByZeroException where the divisor is zero, and | |
| 3497 | // its OverflowException where a signed 32- or 64-bit division or | |
| 3498 | // remainder divides the type's least value by -1, as .NET does; the | |
| 3499 | // engine would trap on the first and the second, and a narrow kind | |
| 3500 | // divides at 32 bits, where its least value does not overflow. Leaves | |
| 3501 | // the operands as they were. | |
| 3502 | _check_division(operation: ArithmeticOperation, numeric: NumericType, type: Type?) is | |
| 3503 | let zero = _can_throw(Semantic.Lookups.RuntimeException.DIVIDE_BY_ZERO) | |
| 3504 | ||
| 3505 | let narrow = | |
| 3506 | if let scalar = WASMGC_TYPES.scalar(_specialize(type)) then WASMGC_TYPES.is_narrow(scalar) else false fi | |
| 3507 | ||
| 3508 | let overflow = | |
| 3509 | _can_throw(Semantic.Lookups.RuntimeException.OVERFLOW) /\ !narrow /\ | |
| 3510 | (operation == ArithmeticOperation.DIVIDE \/ operation == ArithmeticOperation.REMAINDER) | |
| 3511 | ||
| 3512 | if !zero /\ !overflow then | |
| 3513 | return | |
| 3514 | fi | |
| 3515 | ||
| 3516 | let wide = numeric == NumericType.I64 | |
| 3517 | let divisor = _temp(VALUE_TYPE.of_numeric(numeric)) | |
| 3518 | let dividend = _temp(VALUE_TYPE.of_numeric(numeric)) | |
| 3519 | ||
| 3520 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, divisor)) | |
| 3521 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, dividend)) | |
| 3522 | ||
| 3523 | if zero then | |
| 3524 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, divisor)) | |
| 3525 | _emit(if wide then Opcode.I64_EQZ else Opcode.I32_EQZ fi) | |
| 3526 | _throw_if(Semantic.Lookups.RuntimeException.DIVIDE_BY_ZERO, LIST[Value]()) | |
| 3527 | fi | |
| 3528 | ||
| 3529 | if overflow then | |
| 3530 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, dividend)) | |
| 3531 | ||
| 3532 | if wide then | |
| 3533 | _emit(I64_CONSTANT(-9223372036854775807L - 1L)) | |
| 3534 | _emit(Opcode.I64_EQ) | |
| 3535 | else | |
| 3536 | _emit(I32_CONSTANT(-2147483647 - 1)) | |
| 3537 | _emit(Opcode.I32_EQ) | |
| 3538 | fi | |
| 3539 | ||
| 3540 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, divisor)) | |
| 3541 | ||
| 3542 | if wide then | |
| 3543 | _emit(I64_CONSTANT(-1L)) | |
| 3544 | _emit(Opcode.I64_EQ) | |
| 3545 | else | |
| 3546 | _emit(I32_CONSTANT(-1)) | |
| 3547 | _emit(Opcode.I32_EQ) | |
| 3548 | fi | |
| 3549 | ||
| 3550 | _emit(Opcode.I32_AND) | |
| 3551 | _throw_if(Semantic.Lookups.RuntimeException.OVERFLOW, LIST[Value]()) | |
| 3552 | fi | |
| 3553 | ||
| 3554 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, dividend)) | |
| 3555 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, divisor)) | |
| 3556 | si | |
| 3557 | ||
| 3558 | _is_shift(operation: ArithmeticOperation) -> bool => | |
| 3559 | operation == ArithmeticOperation.SHIFT_LEFT \/ | |
| 3560 | operation == ArithmeticOperation.SHIFT_RIGHT \/ | |
| 3561 | operation == ArithmeticOperation.SHIFT_RIGHT_UNSIGNED | |
| 3562 | ||
| 3563 | // Wasm has no integer negation: an integer is subtracted from | |
| 3564 | // zero, and a floating value has its sign flipped. | |
| 3565 | _negate(operand: Value, type: NumericType) is | |
| 3566 | case type | |
| 3567 | when NumericType.I32 then | |
| 3568 | _emit(I32_CONSTANT(0)) | |
| 3569 | gen(operand) | |
| 3570 | _emit(Opcode.I32_SUB) | |
| 3571 | when NumericType.I64 then | |
| 3572 | _emit(I64_CONSTANT(0L)) | |
| 3573 | gen(operand) | |
| 3574 | _emit(Opcode.I64_SUB) | |
| 3575 | when NumericType.F32 then | |
| 3576 | gen(operand) | |
| 3577 | _emit(Opcode.F32_NEG) | |
| 3578 | when NumericType.F64 then | |
| 3579 | gen(operand) | |
| 3580 | _emit(Opcode.F64_NEG) | |
| 3581 | else | |
| 3582 | _report("negation of {type}") | |
| 3583 | esac | |
| 3584 | si | |
| 3585 | ||
| 3586 | // Wasm has no bitwise not: every bit is flipped by an exclusive | |
| 3587 | // or with all ones. | |
| 3588 | _complement(operand: Value, type: NumericType) is | |
| 3589 | gen(operand) | |
| 3590 | ||
| 3591 | case type | |
| 3592 | when NumericType.I32 then | |
| 3593 | _emit(I32_CONSTANT(-1)) | |
| 3594 | _emit(Opcode.I32_XOR) | |
| 3595 | when NumericType.I64 then | |
| 3596 | _emit(I64_CONSTANT(-1L)) | |
| 3597 | _emit(Opcode.I64_XOR) | |
| 3598 | else | |
| 3599 | _report("complement of {type}") | |
| 3600 | esac | |
| 3601 | si | |
| 3602 | ||
| 3603 | visit(compare: Values.COMPARE) is | |
| 3604 | if _value_type(compare.left.type)?.heap_type()? then | |
| 3605 | _compare_references(compare) | |
| 3606 | return | |
| 3607 | fi | |
| 3608 | ||
| 3609 | let numeric = _numeric(compare.left.type) | |
| 3610 | ||
| 3611 | if !numeric? then | |
| 3612 | _report("a comparison of {compare.left.type}") | |
| 3613 | return | |
| 3614 | fi | |
| 3615 | ||
| 3616 | gen(compare.left) | |
| 3617 | gen(compare.right) | |
| 3618 | ||
| 3619 | _compare(compare.comparison, numeric, compare.is_unsigned) | |
| 3620 | si | |
| 3621 | ||
| 3622 | // Two references are equal when they are the same object. | |
| 3623 | _compare_references(compare: Values.COMPARE) is | |
| 3624 | gen(compare.left) | |
| 3625 | gen(compare.right) | |
| 3626 | ||
| 3627 | case compare.comparison | |
| 3628 | when Comparison.EQUAL then | |
| 3629 | _emit(Opcode.REF_EQ) | |
| 3630 | when Comparison.NOT_EQUAL then | |
| 3631 | _emit(Opcode.REF_EQ) | |
| 3632 | _emit(Opcode.I32_EQZ) | |
| 3633 | else | |
| 3634 | _report("an ordering of references") | |
| 3635 | esac | |
| 3636 | si | |
| 3637 | ||
| 3638 | _compare(comparison: Comparison, type: NumericType, is_unsigned: bool) is | |
| 3639 | if let (opcode, negates) = WASMGC_OPERATIONS.comparison(comparison, type, is_unsigned) then | |
| 3640 | _emit(opcode) | |
| 3641 | ||
| 3642 | if negates then | |
| 3643 | _emit(Opcode.I32_EQZ) | |
| 3644 | fi | |
| 3645 | else | |
| 3646 | _report("a comparison of {type}") | |
| 3647 | fi | |
| 3648 | si | |
| 3649 | ||
| 3650 | visit(convert: Values.CONVERT) is | |
| 3651 | gen(convert.value) | |
| 3652 | ||
| 3653 | // A conversion from a reference is an unboxing: the value held | |
| 3654 | // as an object is the box of a scalar of the target type. | |
| 3655 | if !convert.from? /\ _top?.heap_type()? then | |
| 3656 | if let index = _registry.box_index(_specialize(convert.type)) then | |
| 3657 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST, HEAP_TYPE.of_type_index(index))) | |
| 3658 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, index, WASMGC_TYPE_REGISTRY.BOX_VALUE)) | |
| 3659 | else | |
| 3660 | _report("unboxing a {convert.type}") | |
| 3661 | fi | |
| 3662 | ||
| 3663 | return | |
| 3664 | fi | |
| 3665 | ||
| 3666 | _emit_all(WASMGC_OPERATIONS.conversion(convert.from, convert.to)) | |
| 3667 | si | |
| 3668 | ||
| 3669 | visit(node: Values.Breakable) is | |
| 3670 | assert node.is_closed else "structured node generated before it was closed" | |
| 3671 | ||
| 3672 | if let region: Values.REGION = node then | |
| 3673 | _lower_region(region, null) | |
| 3674 | elif let loop: Values.LOOP = node then | |
| 3675 | _lower_loop(loop) | |
| 3676 | elif let `try: Values.TRY = node then | |
| 3677 | if let `finally = `try.finally_region then | |
| 3678 | if `try.catches.count > 0 then | |
| 3679 | _report("a try with both catches and a finally") | |
| 3680 | else | |
| 3681 | _lower_try_finally(`try, `finally) | |
| 3682 | fi | |
| 3683 | elif `try.catches.count > 0 then | |
| 3684 | _lower_try_catch(`try) | |
| 3685 | else | |
| 3686 | _report("a try with no handler") | |
| 3687 | fi | |
| 3688 | elif let resumable: Values.RESUMABLE = node then | |
| 3689 | _lower_resumable(resumable) | |
| 3690 | else | |
| 3691 | _report("a generator or asynchronous function") | |
| 3692 | fi | |
| 3693 | si | |
| 3694 | ||
| 3695 | // A state machine's resumable body is a block. Its dispatch | |
| 3696 | // re-enters the body at the resume point the state names by | |
| 3697 | // descending through each region and loop that holds it, since a | |
| 3698 | // wasm branch can leave a block but never enter one. | |
| 3699 | _lower_resumable(resumable: Values.RESUMABLE) is | |
| 3700 | _dispatch = _find_dispatch(resumable.body.values) | |
| 3701 | _resume_point_states.clear() | |
| 3702 | _resume_states.clear() | |
| 3703 | _folded_dispatches.clear() | |
| 3704 | ||
| 3705 | if _dispatch? then | |
| 3706 | // A try body carries a dispatch of its own, which the | |
| 3707 | // entry dispatch reaches through the try's first | |
| 3708 | // instruction. The descent can enter a try_table as it | |
| 3709 | // enters any block, so every dispatch's entries map a | |
| 3710 | // state to its resume point directly. The entry | |
| 3711 | // dispatch's targets for states inside a try are no | |
| 3712 | // resume point, so they map nothing. | |
| 3713 | _collect_dispatches(resumable.body.values, _folded_dispatches) | |
| 3714 | ||
| 3715 | let states = Collections.SET[int]() | |
| 3716 | ||
| 3717 | for dispatch in _folded_dispatches do | |
| 3718 | for (state, target) in dispatch.entries do | |
| 3719 | _resume_point_states[target] = state | |
| 3720 | states.add(state) | |
| 3721 | od | |
| 3722 | od | |
| 3723 | ||
| 3724 | if _collect_resume_states(resumable).count != states.count then | |
| 3725 | _report("a yield or await with no resume point") | |
| 3726 | fi | |
| 3727 | fi | |
| 3728 | ||
| 3729 | _push_label(resumable, false) | |
| 3730 | _begin() | |
| 3731 | ||
| 3732 | _lower_body(resumable, resumable.body.values) | |
| 3733 | ||
| 3734 | let body = _end() | |
| 3735 | _pop_label() | |
| 3736 | ||
| 3737 | _emit(BLOCK_INSTRUCTION(Opcode.BLOCK, BLOCK_TYPE(), body)) | |
| 3738 | ||
| 3739 | // A body no exit leaves ends in a return, which wasm does not | |
| 3740 | // know, since it takes the end of a block to be reachable. | |
| 3741 | if !resumable.is_targeted then | |
| 3742 | _emit(Opcode.UNREACHABLE) | |
| 3743 | fi | |
| 3744 | ||
| 3745 | _dispatch = null | |
| 3746 | si | |
| 3747 | ||
| 3748 | // The first dispatch in `values`. An asynchronous function's body | |
| 3749 | // is inside the try that hands an escaping exception to its builder, | |
| 3750 | // and the try's body opens with the dispatch. | |
| 3751 | _find_dispatch(values: Collections.Iterable[Value]) -> Values.DISPATCH? is | |
| 3752 | for value in values do | |
| 3753 | if let dispatch: Values.DISPATCH = value then | |
| 3754 | return dispatch | |
| 3755 | elif let block: Values.BLOCK = value then | |
| 3756 | if let dispatch = _find_dispatch(block.values) then | |
| 3757 | return dispatch | |
| 3758 | fi | |
| 3759 | elif let breakable: Values.Breakable = value then | |
| 3760 | if let dispatch = _find_dispatch(breakable.body.values) then | |
| 3761 | return dispatch | |
| 3762 | fi | |
| 3763 | fi | |
| 3764 | od | |
| 3765 | ||
| 3766 | return null | |
| 3767 | si | |
| 3768 | ||
| 3769 | // Every dispatch in `values`, the entry dispatch first and then | |
| 3770 | // those opening the try bodies inside. | |
| 3771 | _collect_dispatches(values: Collections.Iterable[Value], into: LIST[Values.DISPATCH]) is | |
| 3772 | for value in values do | |
| 3773 | if let dispatch: Values.DISPATCH = value then | |
| 3774 | into.add(dispatch) | |
| 3775 | elif let block: Values.BLOCK = value then | |
| 3776 | _collect_dispatches(block.values, into) | |
| 3777 | elif let breakable: Values.Breakable = value then | |
| 3778 | _collect_dispatches(breakable.body.values, into) | |
| 3779 | fi | |
| 3780 | od | |
| 3781 | si | |
| 3782 | ||
| 3783 | // The states whose resume point lies inside `value`, recorded | |
| 3784 | // against each block, region and loop holding any. | |
| 3785 | _collect_resume_states(value: Value) -> LIST[int] is | |
| 3786 | let result = LIST[int]() | |
| 3787 | ||
| 3788 | if let point: Values.RESUME_POINT = value then | |
| 3789 | let state: int mut | |
| 3790 | ||
| 3791 | if _resume_point_states.try_get_value(point.label, state ref) then | |
| 3792 | result.add(state) | |
| 3793 | fi | |
| 3794 | ||
| 3795 | return result | |
| 3796 | fi | |
| 3797 | ||
| 3798 | // An await's resume point is in the region it is lowered as, and | |
| 3799 | // a statement holding an await is entered through the await: the | |
| 3800 | // .NET target re-enters at the same point with nothing evaluated | |
| 3801 | // before it, so nothing in the statement comes before the await | |
| 3802 | // either, and lowering the statement again reaches the region, | |
| 3803 | // whose own dispatch goes on to the resume point. | |
| 3804 | if let suspend = _await_in(value) then | |
| 3805 | result.add_range(_collect_resume_states(_await_structure(suspend))) | |
| 3806 | ||
| 3807 | if result.count > 0 then | |
| 3808 | _resume_states[value] = result | |
| 3809 | fi | |
| 3810 | ||
| 3811 | return result | |
| 3812 | fi | |
| 3813 | ||
| 3814 | let children = | |
| 3815 | if let block: Values.BLOCK = value then | |
| 3816 | block.values | |
| 3817 | elif let breakable: Values.Breakable = value then | |
| 3818 | breakable.body.values | |
| 3819 | else | |
| 3820 | null | |
| 3821 | fi | |
| 3822 | ||
| 3823 | if !children? then | |
| 3824 | return result | |
| 3825 | fi | |
| 3826 | ||
| 3827 | for child in children do | |
| 3828 | result.add_range(_collect_resume_states(child)) | |
| 3829 | od | |
| 3830 | ||
| 3831 | if result.count > 0 then | |
| 3832 | _resume_states[value] = result | |
| 3833 | fi | |
| 3834 | ||
| 3835 | return result | |
| 3836 | si | |
| 3837 | ||
| 3838 | // Lowers `values`, the body of `node`. Where a resume point lies | |
| 3839 | // inside, the body opens with a `br_table` over the state that | |
| 3840 | // branches to just before the resume point, or before the region | |
| 3841 | // or loop holding it, which then does the same. Any other state | |
| 3842 | // falls through to the start of the body. | |
| 3843 | _lower_body(node: Value, values: Collections.Iterable[Value]) is | |
| 3844 | if !_resume_states.contains_key(node) then | |
| 3845 | for value in values do | |
| 3846 | _mark(value) | |
| 3847 | gen(value) | |
| 3848 | od | |
| 3849 | ||
| 3850 | return | |
| 3851 | fi | |
| 3852 | ||
| 3853 | let items = LIST[Value]() | |
| 3854 | ||
| 3855 | _flatten(values, items) | |
| 3856 | ||
| 3857 | // The state is read after the dispatch has loaded it. | |
| 3858 | let start mut = 0 | |
| 3859 | ||
| 3860 | for i in 0..items.count do | |
| 3861 | if isa Values.DISPATCH(items[i]) then | |
| 3862 | start = i + 1 | |
| 3863 | fi | |
| 3864 | od | |
| 3865 | ||
| 3866 | let targets = LIST[int]() | |
| 3867 | let table = LIST[int]() | |
| 3868 | ||
| 3869 | for i in start..items.count do | |
| 3870 | let states = _states_inside(items[i]) | |
| 3871 | ||
| 3872 | if states.count > 0 then | |
| 3873 | targets.add(i) | |
| 3874 | ||
| 3875 | for state in states do | |
| 3876 | while table.count <= state do | |
| 3877 | table.add(0) | |
| 3878 | od | |
| 3879 | ||
| 3880 | table[state] = targets.count | |
| 3881 | od | |
| 3882 | fi | |
| 3883 | od | |
| 3884 | ||
| 3885 | for i in 0..start do | |
| 3886 | gen(items[i]) | |
| 3887 | od | |
| 3888 | ||
| 3889 | // One block for each target, ending just before it, and | |
| 3890 | // inside them all one that the default leaves at once. | |
| 3891 | for i in 0::targets.count do | |
| 3892 | _push_label() | |
| 3893 | _begin() | |
| 3894 | od | |
| 3895 | ||
| 3896 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, _local(_dispatch!.state_local))) | |
| 3897 | _emit(BR_TABLE_INSTRUCTION(table.to_array(), 0)) | |
| 3898 | ||
| 3899 | _end_dispatch_block() | |
| 3900 | ||
| 3901 | let next mut = start | |
| 3902 | ||
| 3903 | for target in targets do | |
| 3904 | for i in next..target do | |
| 3905 | gen(items[i]) | |
| 3906 | od | |
| 3907 | ||
| 3908 | _end_dispatch_block() | |
| 3909 | ||
| 3910 | next = target | |
| 3911 | od | |
| 3912 | ||
| 3913 | for i in next..items.count do | |
| 3914 | gen(items[i]) | |
| 3915 | od | |
| 3916 | si | |
| 3917 | ||
| 3918 | _end_dispatch_block() is | |
| 3919 | let body = _end() | |
| 3920 | _pop_label() | |
| 3921 | ||
| 3922 | _emit(BLOCK_INSTRUCTION(Opcode.BLOCK, BLOCK_TYPE(), body)) | |
| 3923 | si | |
| 3924 | ||
| 3925 | // `values` with every block holding a resume point opened up in | |
| 3926 | // place, so that the dispatch can branch to just before it. Only | |
| 3927 | // a block that produces nothing can be opened up. | |
| 3928 | _flatten(values: Collections.Iterable[Value], into: LIST[Value]) is | |
| 3929 | for value in values do | |
| 3930 | if let block: Values.BLOCK = value /\ _resume_states.contains_key(block) then | |
| 3931 | if !WASMGC_TYPES.is_void(block.type) then | |
| 3932 | _report("a yield or await inside an expression") | |
| 3933 | fi | |
| 3934 | ||
| 3935 | _flatten(block.values, into) | |
| 3936 | else | |
| 3937 | into.add(value) | |
| 3938 | fi | |
| 3939 | od | |
| 3940 | si | |
| 3941 | ||
| 3942 | // The await `value` is or holds as the value it computes: the await | |
| 3943 | // itself, or the await a discarded value, a store or a return is | |
| 3944 | // made from. | |
| 3945 | _await_in(value: Value?) -> Values.AWAIT_SUSPEND? is | |
| 3946 | if let suspend: Values.AWAIT_SUSPEND = value then | |
| 3947 | return suspend | |
| 3948 | elif let discard: Values.DISCARD = value then | |
| 3949 | return _await_in(discard.value) | |
| 3950 | elif let store: Values.Store.INSTANCE_FIELD = value then | |
| 3951 | return _await_in(store.value) | |
| 3952 | elif let store: Values.Store.LOCAL_VARIABLE = value then | |
| 3953 | return _await_in(store.value) | |
| 3954 | elif let store: Values.Store.LOCAL_ARGUMENT = value then | |
| 3955 | return _await_in(store.value) | |
| 3956 | elif let tee: Values.TEE_TEMP = value then | |
| 3957 | return _await_in(tee.value) | |
| 3958 | fi | |
| 3959 | ||
| 3960 | return null | |
| 3961 | si | |
| 3962 | ||
| 3963 | _states_inside(value: Value) -> Collections.List[int] is | |
| 3964 | if let point: Values.RESUME_POINT = value then | |
| 3965 | let state: int mut | |
| 3966 | ||
| 3967 | if _resume_point_states.try_get_value(point.label, state ref) then | |
| 3968 | return [state] | |
| 3969 | fi | |
| 3970 | else | |
| 3971 | let states: LIST[int] mut | |
| 3972 | ||
| 3973 | if _resume_states.try_get_value(value, states ref) then | |
| 3974 | return states | |
| 3975 | fi | |
| 3976 | fi | |
| 3977 | ||
| 3978 | return [] | |
| 3979 | si | |
| 3980 | ||
| 3981 | _lower_region(region: Values.REGION, result: VALUE_TYPE?) is | |
| 3982 | _push_label(region, false) | |
| 3983 | ||
| 3984 | if let type = result then | |
| 3985 | _region_results[region] = type | |
| 3986 | fi | |
| 3987 | ||
| 3988 | _begin() | |
| 3989 | ||
| 3990 | let values = region.body.values | |
| 3991 | ||
| 3992 | // A region that ends in another produces its value through | |
| 3993 | // that one: a block expression's statements end with the value | |
| 3994 | // they leave, and that region falls through holding it. | |
| 3995 | let last_region = | |
| 3996 | if result? /\ values.count > 0 /\ !_resume_states.contains_key(region) then | |
| 3997 | cast Values.REGION?(values[values.count - 1]) | |
| 3998 | else | |
| 3999 | null | |
| 4000 | fi | |
| 4001 | ||
| 4002 | if let last = last_region then | |
| 4003 | for i in 0..values.count - 1 do | |
| 4004 | _mark(values[i]) | |
| 4005 | gen(values[i]) | |
| 4006 | od | |
| 4007 | ||
| 4008 | _lower_region(last, result) | |
| 4009 | else | |
| 4010 | _lower_body(region, values) | |
| 4011 | fi | |
| 4012 | ||
| 4013 | // A region that produces a value is otherwise left only by its | |
| 4014 | // arms, each of which breaks out carrying it, so the end of its | |
| 4015 | // last arm does not fall through. Wasm takes the end of a block | |
| 4016 | // to be reachable, so it is told. | |
| 4017 | if result? /\ !last_region? /\ values.count > 0 /\ isa Values.Breakable(values[values.count - 1]) then | |
| 4018 | _emit(Opcode.UNREACHABLE) | |
| 4019 | fi | |
| 4020 | ||
| 4021 | let body = _end() | |
| 4022 | _pop_label() | |
| 4023 | _region_results.remove(region) | |
| 4024 | ||
| 4025 | _emit(BLOCK_INSTRUCTION(Opcode.BLOCK, _block_type(result), body)) | |
| 4026 | si | |
| 4027 | ||
| 4028 | _block_type(result: VALUE_TYPE?) -> BLOCK_TYPE => | |
| 4029 | if let type = result then BLOCK_TYPE.of_value(type) else BLOCK_TYPE() fi | |
| 4030 | ||
| 4031 | // `block (loop body (br 0))`: the end of the body goes round | |
| 4032 | // again, a BREAK leaves the block, and a CONTINUE branches to the | |
| 4033 | // loop, which starts it again. | |
| 4034 | _lower_loop(loop: Values.LOOP) is | |
| 4035 | _push_label(loop, false) | |
| 4036 | _begin() | |
| 4037 | ||
| 4038 | _push_label(loop, true) | |
| 4039 | _begin() | |
| 4040 | ||
| 4041 | _lower_body(loop, loop.body.values) | |
| 4042 | ||
| 4043 | _emit(INDEX_INSTRUCTION(Opcode.BR, 0)) | |
| 4044 | ||
| 4045 | let body = _end() | |
| 4046 | _pop_label() | |
| 4047 | ||
| 4048 | _emit(BLOCK_INSTRUCTION(Opcode.LOOP, BLOCK_TYPE(), body)) | |
| 4049 | ||
| 4050 | let outer = _end() | |
| 4051 | _pop_label() | |
| 4052 | ||
| 4053 | _emit(BLOCK_INSTRUCTION(Opcode.BLOCK, BLOCK_TYPE(), outer)) | |
| 4054 | si | |
| 4055 | ||
| 4056 | _push_label(node: Values.Breakable?, is_loop_head: bool) is | |
| 4057 | _labels.add((node, is_loop_head, null)) | |
| 4058 | si | |
| 4059 | ||
| 4060 | // A label no exit names: a block the lowering adds for itself. | |
| 4061 | _push_label() is | |
| 4062 | _labels.add((null, false, null)) | |
| 4063 | si | |
| 4064 | ||
| 4065 | // The label of the `try_table` around the protected body of a try | |
| 4066 | // with a finally. An exit that finds it before its target leaves | |
| 4067 | // through the finally. | |
| 4068 | _push_label(finally_exits: WASMGC_FINALLY_EXITS) is | |
| 4069 | _labels.add((null, false, finally_exits)) | |
| 4070 | si | |
| 4071 | ||
| 4072 | _pop_label() is | |
| 4073 | _labels.remove_at(_labels.count - 1) | |
| 4074 | si | |
| 4075 | ||
| 4076 | // How many labels lie between the point being lowered and the | |
| 4077 | // one at `position` on the label stack: the label a branch | |
| 4078 | // instruction names. | |
| 4079 | _depth(position: int) -> int => _labels.count - 1 - position | |
| 4080 | ||
| 4081 | // An exit branches to its target's label, unless it finds the | |
| 4082 | // protected body of a try with a finally on the way there. | |
| 4083 | visit(exit: Values.Exit) is | |
| 4084 | let is_continue = isa Values.CONTINUE(exit) | |
| 4085 | ||
| 4086 | let i mut = _labels.count - 1 | |
| 4087 | ||
| 4088 | while i >= 0 do | |
| 4089 | let label = _labels[i] | |
| 4090 | ||
| 4091 | if label.node == exit.target /\ label.is_loop_head == is_continue then | |
| 4092 | if !is_continue then | |
| 4093 | if let node = label.node /\ _region_results.contains_key(node) then | |
| 4094 | _coerce_top(_region_results[node]) | |
| 4095 | fi | |
| 4096 | fi | |
| 4097 | ||
| 4098 | _branch(exit, _depth(i)) | |
| 4099 | return | |
| 4100 | elif let finally_exits = label.finally_exits then | |
| 4101 | _leave_through_finally(exit, finally_exits) | |
| 4102 | return | |
| 4103 | fi | |
| 4104 | ||
| 4105 | i = i - 1 | |
| 4106 | od | |
| 4107 | ||
| 4108 | _report("an exit from a construct the lowering does not handle") | |
| 4109 | si | |
| 4110 | ||
| 4111 | _branch(exit: Values.Exit, depth: int) is | |
| 4112 | if exit.condition == BRANCH.ALWAYS then | |
| 4113 | _emit(INDEX_INSTRUCTION(Opcode.BR, depth)) | |
| 4114 | return | |
| 4115 | fi | |
| 4116 | ||
| 4117 | if !_test(exit.condition, exit.operands) then | |
| 4118 | return | |
| 4119 | fi | |
| 4120 | ||
| 4121 | _emit(INDEX_INSTRUCTION(Opcode.BR_IF, depth)) | |
| 4122 | si | |
| 4123 | ||
| 4124 | // Records which exit is being taken and branches to the finally, | |
| 4125 | // after which the exit is taken again. | |
| 4126 | _leave_through_finally(exit: Values.Exit, finally_exits: WASMGC_FINALLY_EXITS) is | |
| 4127 | if exit.condition != BRANCH.ALWAYS then | |
| 4128 | _report("a conditional exit from a try with a finally") | |
| 4129 | return | |
| 4130 | fi | |
| 4131 | ||
| 4132 | _emit(I32_CONSTANT(finally_exits.number(exit))) | |
| 4133 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, finally_exits.selector)) | |
| 4134 | _emit(INDEX_INSTRUCTION(Opcode.BR, _depth(finally_exits.finally_label))) | |
| 4135 | si | |
| 4136 | ||
| 4137 | // Every exception is thrown with the one tag, carrying the | |
| 4138 | // exception as a non-null reference to the exception type. | |
| 4139 | visit(thrown: Values.THROW) is | |
| 4140 | let exception = thrown.exception | |
| 4141 | ||
| 4142 | if !exception? then | |
| 4143 | _report("a rethrow") | |
| 4144 | return | |
| 4145 | fi | |
| 4146 | ||
| 4147 | gen(exception) | |
| 4148 | ||
| 4149 | let tag = _exceptions?.tag_index | |
| 4150 | let exception_type = _exceptions?.exception | |
| 4151 | ||
| 4152 | if !tag? \/ !exception_type? then | |
| 4153 | _report("a throw") | |
| 4154 | return | |
| 4155 | fi | |
| 4156 | ||
| 4157 | _coerce_top(exception_type) | |
| 4158 | ||
| 4159 | _emit(Opcode.REF_AS_NON_NULL) | |
| 4160 | ||
| 4161 | // Thrown by the host where the build declared it, under --debug, | |
| 4162 | // so the exception carries the stack it was thrown from. | |
| 4163 | if let host_throw = _imports.index_of(WASMGC_IMPORTS.THROW) then | |
| 4164 | _emit(INDEX_INSTRUCTION(Opcode.CALL, host_throw)) | |
| 4165 | _emit(Opcode.UNREACHABLE) | |
| 4166 | else | |
| 4167 | _emit(THROW_INSTRUCTION(tag)) | |
| 4168 | fi | |
| 4169 | si | |
| 4170 | ||
| 4171 | // The protected body is a `try_table` whose handler receives the | |
| 4172 | // exception and the reference that rethrows it. Each catch tests | |
| 4173 | // the exception against its type in turn; one that matches has | |
| 4174 | // the exception on the stack where its body starts, as the body | |
| 4175 | // expects. One no catch matches is rethrown. | |
| 4176 | _lower_try_catch(`try: Values.TRY) is | |
| 4177 | let tag = _exceptions?.tag_index | |
| 4178 | let exception = _exceptions?.exception | |
| 4179 | ||
| 4180 | if !tag? \/ !exception? then | |
| 4181 | _lower_unhandled(`try) | |
| 4182 | _report("a catch") | |
| 4183 | return | |
| 4184 | fi | |
| 4185 | ||
| 4186 | let caught = _temp(exception) | |
| 4187 | let rethrow = _temp(VALUE_TYPE.of_abstract(AbstractHeapType.EXN)) | |
| 4188 | ||
| 4189 | _push_label(`try, false) | |
| 4190 | _begin() | |
| 4191 | ||
| 4192 | _push_label() | |
| 4193 | _begin() | |
| 4194 | ||
| 4195 | _push_label() | |
| 4196 | _begin() | |
| 4197 | ||
| 4198 | _lower_body(`try, `try.body.values) | |
| 4199 | ||
| 4200 | let guarded = _end() | |
| 4201 | _pop_label() | |
| 4202 | ||
| 4203 | // A catch clause's label is counted from outside the | |
| 4204 | // `try_table`, so 0 is the handler block. | |
| 4205 | _emit(TRY_TABLE_INSTRUCTION(BLOCK_TYPE(), [CATCH_CLAUSE(CatchKind.CATCH_REF, tag, 0)], guarded)) | |
| 4206 | _emit(INDEX_INSTRUCTION(Opcode.BR, 1)) | |
| 4207 | ||
| 4208 | let handler = _end() | |
| 4209 | _pop_label() | |
| 4210 | ||
| 4211 | _emit(BLOCK_INSTRUCTION(Opcode.BLOCK, BLOCK_TYPE.of_type_index(_exceptions!.handler_type), handler)) | |
| 4212 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, rethrow)) | |
| 4213 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, caught)) | |
| 4214 | ||
| 4215 | for `catch in `try.catches do | |
| 4216 | _lower_catch(`catch, caught, exception) | |
| 4217 | od | |
| 4218 | ||
| 4219 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, rethrow)) | |
| 4220 | _emit(Opcode.THROW_REF) | |
| 4221 | ||
| 4222 | let body = _end() | |
| 4223 | _pop_label() | |
| 4224 | ||
| 4225 | _emit(BLOCK_INSTRUCTION(Opcode.BLOCK, BLOCK_TYPE(), body)) | |
| 4226 | si | |
| 4227 | ||
| 4228 | // A block a catch body runs in, left early where the exception is | |
| 4229 | // not of the catch's type. | |
| 4230 | _lower_catch(`catch: Values.CATCH, caught: int, exception: VALUE_TYPE) is | |
| 4231 | let target = _registry.type_index(`catch.type) | |
| 4232 | ||
| 4233 | if !target? then | |
| 4234 | _report("a catch of {`catch.type}") | |
| 4235 | return | |
| 4236 | fi | |
| 4237 | ||
| 4238 | let target_heap = HEAP_TYPE.of_type_index(target) | |
| 4239 | let is_every_exception = _registry.is_held_as(exception.heap_type()!, target_heap) | |
| 4240 | ||
| 4241 | _push_label() | |
| 4242 | _begin() | |
| 4243 | ||
| 4244 | if !is_every_exception then | |
| 4245 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, caught)) | |
| 4246 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_TEST, target_heap)) | |
| 4247 | _emit(Opcode.I32_EQZ) | |
| 4248 | _emit(INDEX_INSTRUCTION(Opcode.BR_IF, 0)) | |
| 4249 | fi | |
| 4250 | ||
| 4251 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, caught)) | |
| 4252 | ||
| 4253 | if is_every_exception then | |
| 4254 | _top_cast = exception | |
| 4255 | else | |
| 4256 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST, target_heap)) | |
| 4257 | _top_cast = VALUE_TYPE.of_type_index(target, false) | |
| 4258 | fi | |
| 4259 | ||
| 4260 | _top_value = null | |
| 4261 | ||
| 4262 | _lower_all(`catch.body.values) | |
| 4263 | ||
| 4264 | let body = _end() | |
| 4265 | _pop_label() | |
| 4266 | ||
| 4267 | _emit(BLOCK_INSTRUCTION(Opcode.BLOCK, BLOCK_TYPE(), body)) | |
| 4268 | si | |
| 4269 | ||
| 4270 | // The protected body runs inside a `try_table` that catches | |
| 4271 | // everything. An exception runs the finally and is rethrown. An | |
| 4272 | // exit from the body records which it is and branches to the | |
| 4273 | // finally, after which it is taken again from there, so an exit | |
| 4274 | // crossing several finallies runs each of them in turn. | |
| 4275 | _lower_try_finally(`try: Values.TRY, `finally: Values.REGION) is | |
| 4276 | let exception = VALUE_TYPE.of_abstract(AbstractHeapType.EXN) | |
| 4277 | ||
| 4278 | let finally_exits = WASMGC_FINALLY_EXITS(_temp(VALUE_TYPE.of_numeric(NumericType.I32))) | |
| 4279 | let rethrow = _temp(exception) | |
| 4280 | ||
| 4281 | _emit(I32_CONSTANT(0)) | |
| 4282 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, finally_exits.selector)) | |
| 4283 | ||
| 4284 | _push_label(`try, false) | |
| 4285 | _begin() | |
| 4286 | ||
| 4287 | _push_label() | |
| 4288 | _begin() | |
| 4289 | ||
| 4290 | finally_exits.finally_label = _labels.count - 1 | |
| 4291 | ||
| 4292 | _push_label() | |
| 4293 | _begin() | |
| 4294 | ||
| 4295 | _push_label(finally_exits) | |
| 4296 | _begin() | |
| 4297 | ||
| 4298 | _lower_body(`try, `try.body.values) | |
| 4299 | ||
| 4300 | let guarded = _end() | |
| 4301 | _pop_label() | |
| 4302 | ||
| 4303 | _emit(TRY_TABLE_INSTRUCTION(BLOCK_TYPE(), [CATCH_CLAUSE(CatchKind.CATCH_ALL_REF, 0, 0)], guarded)) | |
| 4304 | _emit(INDEX_INSTRUCTION(Opcode.BR, 1)) | |
| 4305 | ||
| 4306 | let exceptional = _end() | |
| 4307 | _pop_label() | |
| 4308 | ||
| 4309 | _emit(BLOCK_INSTRUCTION(Opcode.BLOCK, BLOCK_TYPE.of_value(exception), exceptional)) | |
| 4310 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, rethrow)) | |
| 4311 | ||
| 4312 | _lower_region(`finally, null) | |
| 4313 | ||
| 4314 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, rethrow)) | |
| 4315 | _emit(Opcode.THROW_REF) | |
| 4316 | ||
| 4317 | let normal = _end() | |
| 4318 | _pop_label() | |
| 4319 | ||
| 4320 | _emit(BLOCK_INSTRUCTION(Opcode.BLOCK, BLOCK_TYPE(), normal)) | |
| 4321 | ||
| 4322 | _lower_region(`finally, null) | |
| 4323 | ||
| 4324 | let exits = finally_exits.exits | |
| 4325 | ||
| 4326 | for i in 0..exits.count do | |
| 4327 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, finally_exits.selector)) | |
| 4328 | _emit(I32_CONSTANT(i + 1)) | |
| 4329 | _emit(Opcode.I32_EQ) | |
| 4330 | ||
| 4331 | _push_label() | |
| 4332 | _begin() | |
| 4333 | ||
| 4334 | gen(exits[i]) | |
| 4335 | ||
| 4336 | let taken = _end() | |
| 4337 | _pop_label() | |
| 4338 | ||
| 4339 | _emit(IF_INSTRUCTION(BLOCK_TYPE(), taken, LIST[INSTRUCTION]())) | |
| 4340 | od | |
| 4341 | ||
| 4342 | let body = _end() | |
| 4343 | _pop_label() | |
| 4344 | ||
| 4345 | _emit(BLOCK_INSTRUCTION(Opcode.BLOCK, BLOCK_TYPE(), body)) | |
| 4346 | si | |
| 4347 | ||
| 4348 | // The body and catches of a try lowered for what they reach alone, | |
| 4349 | // where there is no tag to catch with. | |
| 4350 | _lower_unhandled(`try: Values.TRY) is | |
| 4351 | _push_label(`try, false) | |
| 4352 | _begin() | |
| 4353 | ||
| 4354 | _lower_body(`try, `try.body.values) | |
| 4355 | ||
| 4356 | for `catch in `try.catches do | |
| 4357 | _lower_all(`catch.body.values) | |
| 4358 | od | |
| 4359 | ||
| 4360 | _end() | |
| 4361 | _pop_label() | |
| 4362 | si | |
| 4363 | ||
| 4364 | // Leaves an i32 on the stack that is nonzero when `condition` | |
| 4365 | // holds of `operands`. False where it cannot. | |
| 4366 | _test(condition: BRANCH, operands: Collections.List[Value]) -> bool is | |
| 4367 | if _value_type(operands[0].type)?.heap_type()? then | |
| 4368 | return _test_reference(condition, operands) | |
| 4369 | fi | |
| 4370 | ||
| 4371 | let numeric = _numeric(operands[0].type) | |
| 4372 | ||
| 4373 | if !numeric? then | |
| 4374 | _report("a condition on {operands[0].type}") | |
| 4375 | return false | |
| 4376 | fi | |
| 4377 | ||
| 4378 | _lower_all(operands) | |
| 4379 | ||
| 4380 | if condition == BRANCH.Z \/ condition == BRANCH.NZ then | |
| 4381 | case numeric | |
| 4382 | when NumericType.I32 then | |
| 4383 | if condition == BRANCH.Z then | |
| 4384 | _emit(Opcode.I32_EQZ) | |
| 4385 | fi | |
| 4386 | when NumericType.I64 then | |
| 4387 | _emit(Opcode.I64_EQZ) | |
| 4388 | ||
| 4389 | if condition == BRANCH.NZ then | |
| 4390 | _emit(Opcode.I32_EQZ) | |
| 4391 | fi | |
| 4392 | else | |
| 4393 | _report("a condition on {operands[0].type}") | |
| 4394 | return false | |
| 4395 | esac | |
| 4396 | ||
| 4397 | return true | |
| 4398 | fi | |
| 4399 | ||
| 4400 | if let (comparison, is_unsigned) = WASMGC_OPERATIONS.branch_comparison(condition) then | |
| 4401 | _compare(comparison, numeric, is_unsigned) | |
| 4402 | ||
| 4403 | return true | |
| 4404 | fi | |
| 4405 | ||
| 4406 | _report("the condition {condition}") | |
| 4407 | ||
| 4408 | return false | |
| 4409 | si | |
| 4410 | ||
| 4411 | // A reference is zero when it is null, and two references are | |
| 4412 | // equal when they are the same object. | |
| 4413 | _test_reference(condition: BRANCH, operands: Collections.List[Value]) -> bool is | |
| 4414 | _lower_all(operands) | |
| 4415 | ||
| 4416 | case condition | |
| 4417 | when BRANCH.Z then | |
| 4418 | _emit(Opcode.REF_IS_NULL) | |
| 4419 | when BRANCH.NZ then | |
| 4420 | _emit(Opcode.REF_IS_NULL) | |
| 4421 | _emit(Opcode.I32_EQZ) | |
| 4422 | when BRANCH.EQ then | |
| 4423 | _emit(Opcode.REF_EQ) | |
| 4424 | when BRANCH.NE then | |
| 4425 | _emit(Opcode.REF_EQ) | |
| 4426 | _emit(Opcode.I32_EQZ) | |
| 4427 | else | |
| 4428 | _report("the condition {condition} on a reference") | |
| 4429 | return false | |
| 4430 | esac | |
| 4431 | ||
| 4432 | return true | |
| 4433 | si | |
| 4434 | ||
| 4435 | // The resumable body re-enters itself where the state says, by | |
| 4436 | // descent from its start, so a dispatch it holds - its own or one | |
| 4437 | // opening a try body inside it - emits nothing of its own. | |
| 4438 | visit(dispatch: Values.DISPATCH) is | |
| 4439 | if !_folded_dispatches.contains(dispatch) then | |
| 4440 | _report("a yield or await with no resume point") | |
| 4441 | fi | |
| 4442 | si | |
| 4443 | ||
| 4444 | visit(resume: Values.RESUME_POINT) is si | |
| 4445 | ||
| 4446 | visit(call: Values.Call.GLOBAL) is | |
| 4447 | _call(call.function, call.arguments) | |
| 4448 | si | |
| 4449 | ||
| 4450 | /// A static method takes no receiver, so it is called as a | |
| 4451 | /// global function is. | |
| 4452 | visit(call: Values.Call.STATIC) is | |
| 4453 | if call.constrained_receiver? then | |
| 4454 | _report("a constrained call") | |
| 4455 | else | |
| 4456 | _ensure_initialised(call.function.owner) | |
| 4457 | _call(call.function, call.arguments) | |
| 4458 | fi | |
| 4459 | si | |
| 4460 | ||
| 4461 | // A type's static constructor runs once, before the type is first | |
| 4462 | // used: before a static member is read, written or called, and | |
| 4463 | // before an instance is made. Its flag is set before the body runs, | |
| 4464 | // so a use of the type from inside it does not run it again. | |
| 4465 | _ensure_initialised(owner: Semantic.Scope?) is | |
| 4466 | if let type = _owner_class(owner) then | |
| 4467 | if let (constructor, initialised) = _registry.static_constructor(type) then | |
| 4468 | let index_found = _numbering.index_of(constructor) | |
| 4469 | ||
| 4470 | if !index_found? then | |
| 4471 | _report("a static constructor of {type}") | |
| 4472 | return | |
| 4473 | fi | |
| 4474 | ||
| 4475 | let index = index_found | |
| 4476 | ||
| 4477 | let run = LIST[INSTRUCTION]() | |
| 4478 | ||
| 4479 | run.add(I32_CONSTANT(1)) | |
| 4480 | run.add(INDEX_INSTRUCTION(Opcode.GLOBAL_SET, initialised)) | |
| 4481 | run.add(INDEX_INSTRUCTION(Opcode.CALL, index)) | |
| 4482 | ||
| 4483 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, initialised)) | |
| 4484 | _emit(Opcode.I32_EQZ) | |
| 4485 | _emit(IF_INSTRUCTION(BLOCK_TYPE(), run, LIST[INSTRUCTION]())) | |
| 4486 | fi | |
| 4487 | fi | |
| 4488 | si | |
| 4489 | ||
| 4490 | // A method the core library gives a scalar takes the value itself | |
| 4491 | // as its receiver. A struct method takes the box as its receiver, | |
| 4492 | // so a call on a struct changes it in place. | |
| 4493 | visit(call: Values.Call.STRUCT) is | |
| 4494 | let function = call.function | |
| 4495 | let instance = _instance_of(function) | |
| 4496 | ||
| 4497 | if let owner = _owner_class(function.owner) /\ _registry.is_struct(owner) then | |
| 4498 | let index = _index_of(function) | |
| 4499 | ||
| 4500 | if !index? then | |
| 4501 | _report("a call to {function.name}") | |
| 4502 | return | |
| 4503 | fi | |
| 4504 | ||
| 4505 | gen(call.from) | |
| 4506 | _coerce_top(_local_type_of_receiver(instance)) | |
| 4507 | ||
| 4508 | _lower_arguments(function, call.arguments) | |
| 4509 | ||
| 4510 | _emit(INDEX_INSTRUCTION(Opcode.CALL, index)) | |
| 4511 | return | |
| 4512 | fi | |
| 4513 | ||
| 4514 | let receiver = _registry.receiver_type(instance) | |
| 4515 | ||
| 4516 | if !receiver? \/ !receiver.numeric()? then | |
| 4517 | _report("a struct method call") | |
| 4518 | return | |
| 4519 | fi | |
| 4520 | ||
| 4521 | let index = _numbering.index_of(instance) | |
| 4522 | ||
| 4523 | if !index? then | |
| 4524 | _report("a call to {call.function.name}") | |
| 4525 | return | |
| 4526 | fi | |
| 4527 | ||
| 4528 | if let address: Values.ADDRESS = call.from then | |
| 4529 | gen(address.value) | |
| 4530 | else | |
| 4531 | gen(call.from) | |
| 4532 | fi | |
| 4533 | ||
| 4534 | _lower_arguments(call.function, call.arguments) | |
| 4535 | ||
| 4536 | _emit(INDEX_INSTRUCTION(Opcode.CALL, index)) | |
| 4537 | si | |
| 4538 | // A call is direct where the dispatch says so, and where nothing | |
| 4539 | // in the program overrides the method, since the whole program is | |
| 4540 | // known. Dispatch through a vtable or a trait is not lowered yet. | |
| 4541 | visit(call: Values.Call.INSTANCE) is | |
| 4542 | let function = call.function | |
| 4543 | let dispatch = call.dispatch | |
| 4544 | let in_place = _element_in_place | |
| 4545 | _element_in_place = false | |
| 4546 | ||
| 4547 | if function.is_constructor /\ _is_object_constructor(function) then | |
| 4548 | return | |
| 4549 | fi | |
| 4550 | ||
| 4551 | if _is_get_type(function) then | |
| 4552 | _get_type(call) | |
| 4553 | return | |
| 4554 | fi | |
| 4555 | ||
| 4556 | // An enum value is held as its integer, so its to_string is the | |
| 4557 | // text the core library gives it from the enum's members. | |
| 4558 | if function.name =~ "to_string" /\ call.arguments.count == 0 then | |
| 4559 | if let text = INTERPOLATION_CALLS.enum_text(call.from, _specialize(call.from.type)) then | |
| 4560 | gen(text) | |
| 4561 | return | |
| 4562 | fi | |
| 4563 | fi | |
| 4564 | ||
| 4565 | if let constrained: Values.CONSTRAINED = call.from then | |
| 4566 | _call_constrained(call, constrained) | |
| 4567 | return | |
| 4568 | fi | |
| 4569 | ||
| 4570 | if let member = _array_member(call) then | |
| 4571 | if let operation = _array_operation(member) then | |
| 4572 | _lower_array_operation(operation, call, in_place) | |
| 4573 | else | |
| 4574 | _call_direct(call, FUNCTION_INSTANCE(member, TYPE_MAPS.function_map(function, _type_map))) | |
| 4575 | fi | |
| 4576 | ||
| 4577 | return | |
| 4578 | fi | |
| 4579 | ||
| 4580 | // A method of an instantiated generic trait is owned by the | |
| 4581 | // instantiation rather than the trait, so the call reads as | |
| 4582 | // virtual; its definition says where it dispatches. | |
| 4583 | if dispatch == IR.Dispatch.VIRTUAL /\ _is_trait_method(function) then | |
| 4584 | _call_interface(call) | |
| 4585 | return | |
| 4586 | elif dispatch == IR.Dispatch.VIRTUAL /\ !function.has_no_overriders then | |
| 4587 | _call_virtual(call) | |
| 4588 | return | |
| 4589 | elif dispatch == IR.Dispatch.INTERFACE then | |
| 4590 | _call_interface(call) | |
| 4591 | return | |
| 4592 | elif dispatch != IR.Dispatch.DIRECT /\ dispatch != IR.Dispatch.VIRTUAL then | |
| 4593 | _report("a {dispatch} call") | |
| 4594 | return | |
| 4595 | fi | |
| 4596 | ||
| 4597 | _call_direct(call, _instance_of(function)) | |
| 4598 | si | |
| 4599 | ||
| 4600 | // A call on a type parameter's value, once the instantiation has | |
| 4601 | // made its type concrete. A scalar's hash code is computed in | |
| 4602 | // place, a member the core library gives the scalar is called on | |
| 4603 | // the value itself, and any other member is called on the boxed | |
| 4604 | // value, as the CIL constrained call does; anything held as a | |
| 4605 | // reference is called as any value of its type is. | |
| 4606 | _call_constrained(call: Values.Call.INSTANCE, constrained: Values.CONSTRAINED) is | |
| 4607 | let receiver = | |
| 4608 | if let address: Values.ADDRESS = constrained.value then address.value else constrained.value fi | |
| 4609 | ||
| 4610 | let type = _specialize(constrained.type) | |
| 4611 | ||
| 4612 | if let scalar = WASMGC_TYPES.scalar(type) then | |
| 4613 | if call.function.name =~ "get_hash_code" /\ call.arguments.count == 0 then | |
| 4614 | gen(receiver) | |
| 4615 | let numeric = WASMGC_TYPES.numeric(scalar) | |
| 4616 | ||
| 4617 | _emit_all( | |
| 4618 | WASMGC_OPERATIONS.hash( | |
| 4619 | numeric, | |
| 4620 | _temp(VALUE_TYPE.of_numeric(NumericType.I64)), | |
| 4621 | _temp(VALUE_TYPE.of_numeric(numeric)))) | |
| 4622 | elif let own = _scalar_member(type, call.function) then | |
| 4623 | _call_direct(Values.Call.INSTANCE(receiver, own, call.arguments, call.type), FUNCTION_INSTANCE.of(own)) | |
| 4624 | else | |
| 4625 | gen(Values.Call.INSTANCE(Values.BOX(receiver), call.function, call.arguments, call.type)) | |
| 4626 | fi | |
| 4627 | ||
| 4628 | return | |
| 4629 | fi | |
| 4630 | ||
| 4631 | gen(Values.Call.INSTANCE(receiver, call.function, call.arguments, call.type)) | |
| 4632 | si | |
| 4633 | ||
| 4634 | // The member of the scalar `type` that the core library declares | |
| 4635 | // in place of `function`, which the root declares: same name, same | |
| 4636 | // arity. | |
| 4637 | _scalar_member(type: Type?, function: Function) -> Function? is | |
| 4638 | if let own = _method(type, function.name, function.arguments.count) then | |
| 4639 | if let owner: Semantic.Symbols.Classy = own.owner /\ !owner.is_object then | |
| 4640 | return own | |
| 4641 | fi | |
| 4642 | fi | |
| 4643 | ||
| 4644 | return null | |
| 4645 | si | |
| 4646 | ||
| 4647 | // A function past the instantiation depth the target follows calls | |
| 4648 | // itself at ever deeper type arguments; anything else uncompiled | |
| 4649 | // is reported as the call it is. | |
| 4650 | _report_uncompiled_call(instance: FUNCTION_INSTANCE) is | |
| 4651 | if TYPE_MAPS.depth(instance.type_map.values) > INSTANTIATION_LIMITS.MAXIMUM_TYPE_ARGUMENT_DEPTH then | |
| 4652 | _report("{instance.definition.name} calling itself at ever deeper type arguments") | |
| 4653 | else | |
| 4654 | _report("a call to {instance.definition.name}") | |
| 4655 | fi | |
| 4656 | si | |
| 4657 | ||
| 4658 | _call_direct(call: Values.Call.INSTANCE, function: FUNCTION_INSTANCE) is | |
| 4659 | let index_found = _numbering.index_of(function) | |
| 4660 | ||
| 4661 | if !index_found? then | |
| 4662 | _report_uncompiled_call(function) | |
| 4663 | return | |
| 4664 | fi | |
| 4665 | ||
| 4666 | let index = index_found | |
| 4667 | ||
| 4668 | gen(call.from) | |
| 4669 | ||
| 4670 | let receiver = _local_type_of_receiver(function) | |
| 4671 | ||
| 4672 | // A scalar's method reached through a trait it implements is | |
| 4673 | // handed the box the value is held in, and takes the value. | |
| 4674 | if receiver?.numeric()? /\ (_top?.heap_type()?.matches(_registry.object_reference.heap_type()!) ?? false) then | |
| 4675 | if let owner: Semantic.Symbols.Classy = function.definition.owner, box = _registry.box_index(Semantic.Types.NAMED(owner)) then | |
| 4676 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST, HEAP_TYPE.of_type_index(box))) | |
| 4677 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, box, WASMGC_TYPE_REGISTRY.BOX_VALUE)) | |
| 4678 | fi | |
| 4679 | else | |
| 4680 | _coerce_top(receiver) | |
| 4681 | fi | |
| 4682 | ||
| 4683 | let tested = if let r = receiver /\ r.heap_type()? /\ _is_null_checked(call.from) /\ _can_throw(Semantic.Lookups.RuntimeException.NULL_REFERENCE) then _temp(r) else null fi | |
| 4684 | ||
| 4685 | if let t = tested then | |
| 4686 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, t)) | |
| 4687 | fi | |
| 4688 | ||
| 4689 | _lower_arguments(call.function, call.arguments) | |
| 4690 | ||
| 4691 | if let t = tested then | |
| 4692 | _throw_if_null(t) | |
| 4693 | fi | |
| 4694 | ||
| 4695 | _emit(INDEX_INSTRUCTION(Opcode.CALL, index)) | |
| 4696 | si | |
| 4697 | ||
| 4698 | // The receiver's vtable is read from its descriptor and the | |
| 4699 | // method's slot called. The receiver is held in a local, since it | |
| 4700 | // is both the first argument and where the vtable comes from. | |
| 4701 | _call_virtual(call: Values.Call.INSTANCE) is | |
| 4702 | let function = call.function | |
| 4703 | let dispatch = _registry.dispatch | |
| 4704 | ||
| 4705 | if _registry.discovering then | |
| 4706 | _registry.note_dispatched(function) | |
| 4707 | gen(call.from) | |
| 4708 | _lower_all(call.arguments) | |
| 4709 | return | |
| 4710 | fi | |
| 4711 | ||
| 4712 | let slot = dispatch.slot_of(_instance_of(function)) | |
| 4713 | ||
| 4714 | if !slot? then | |
| 4715 | _report("a virtual call to {function.name}") | |
| 4716 | return | |
| 4717 | fi | |
| 4718 | ||
| 4719 | dispatch.use_slot(slot, _numbering) | |
| 4720 | ||
| 4721 | let receiver_type = VALUE_TYPE.of_type_index(_registry.type_index(slot.owner)!, true) | |
| 4722 | let receiver = _temp(receiver_type) | |
| 4723 | ||
| 4724 | gen(call.from) | |
| 4725 | _coerce_top(receiver_type) | |
| 4726 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, receiver)) | |
| 4727 | ||
| 4728 | _lower_arguments(function, call.arguments) | |
| 4729 | ||
| 4730 | let vtable_type = dispatch.vtable_type(slot.owner)! | |
| 4731 | ||
| 4732 | if _is_null_checked(call.from) then | |
| 4733 | _throw_if_null(receiver) | |
| 4734 | fi | |
| 4735 | ||
| 4736 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, receiver)) | |
| 4737 | _load_descriptor() | |
| 4738 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, WASMGC_TYPE_REGISTRY.DESCRIPTOR_TYPE_INDEX, WASMGC_TYPE_REGISTRY.DESCRIPTOR_VTABLE)) | |
| 4739 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST, HEAP_TYPE.of_type_index(vtable_type))) | |
| 4740 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, vtable_type, slot.index)) | |
| 4741 | _emit(TYPE_INSTRUCTION(Opcode.CALL_REF, slot.signature)) | |
| 4742 | si | |
| 4743 | ||
| 4744 | // Writes a value for display through the displayer its descriptor | |
| 4745 | // holds, answering whether it has one. | |
| 4746 | _lower_display(arguments: Collections.List[Value]) is | |
| 4747 | if let state_class = _registry.class_of(_specialize(arguments[1].type)) then | |
| 4748 | _registry.note_display(state_class) | |
| 4749 | fi | |
| 4750 | ||
| 4751 | let value = _temp(_registry.object_reference) | |
| 4752 | let state = _temp(_registry.object_reference) | |
| 4753 | let displayer = _temp(VALUE_TYPE.of_abstract(AbstractHeapType.FUNC)) | |
| 4754 | let signature = _registry.display_signature | |
| 4755 | ||
| 4756 | gen(arguments[0]) | |
| 4757 | _coerce_top(_registry.object_reference) | |
| 4758 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, value)) | |
| 4759 | _load_descriptor() | |
| 4760 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, WASMGC_TYPE_REGISTRY.DESCRIPTOR_TYPE_INDEX, WASMGC_TYPE_REGISTRY.DESCRIPTOR_DISPLAY)) | |
| 4761 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, displayer)) | |
| 4762 | ||
| 4763 | gen(arguments[1]) | |
| 4764 | _coerce_top(_registry.object_reference) | |
| 4765 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, state)) | |
| 4766 | ||
| 4767 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, displayer)) | |
| 4768 | _emit(Opcode.REF_IS_NULL) | |
| 4769 | _emit( | |
| 4770 | IF_INSTRUCTION( | |
| 4771 | BLOCK_TYPE.of_value(VALUE_TYPE.of_numeric(NumericType.I32)), | |
| 4772 | [I32_CONSTANT(0)]: INSTRUCTION, | |
| 4773 | [ | |
| 4774 | INDEX_INSTRUCTION(Opcode.LOCAL_GET, value), | |
| 4775 | INDEX_INSTRUCTION(Opcode.LOCAL_GET, state), | |
| 4776 | INDEX_INSTRUCTION(Opcode.LOCAL_GET, displayer), | |
| 4777 | HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST, HEAP_TYPE.of_type_index(signature)), | |
| 4778 | TYPE_INSTRUCTION(Opcode.CALL_REF, signature), | |
| 4779 | I32_CONSTANT(1) | |
| 4780 | ]: INSTRUCTION)) | |
| 4781 | si | |
| 4782 | ||
| 4783 | // Copies an object through the cloner its descriptor holds, which | |
| 4784 | // makes a new instance of the object's own class. | |
| 4785 | _lower_clone(value: Value) is | |
| 4786 | _registry.note_clone() | |
| 4787 | ||
| 4788 | let object = _temp(_registry.object_reference) | |
| 4789 | let cloner = _temp(VALUE_TYPE.of_abstract(AbstractHeapType.FUNC)) | |
| 4790 | let signature = _registry.clone_signature | |
| 4791 | ||
| 4792 | gen(value) | |
| 4793 | _coerce_top(_registry.object_reference) | |
| 4794 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, object)) | |
| 4795 | _load_descriptor() | |
| 4796 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, WASMGC_TYPE_REGISTRY.DESCRIPTOR_TYPE_INDEX, WASMGC_TYPE_REGISTRY.DESCRIPTOR_CLONE)) | |
| 4797 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, cloner)) | |
| 4798 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, object)) | |
| 4799 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, cloner)) | |
| 4800 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST, HEAP_TYPE.of_type_index(signature))) | |
| 4801 | _emit(TYPE_INSTRUCTION(Opcode.CALL_REF, signature)) | |
| 4802 | si | |
| 4803 | ||
| 4804 | // The hash of an object's identity, which the host keeps for as long | |
| 4805 | // as the object lives, since a WasmGC reference has no address. | |
| 4806 | _lower_identity_hash(value: Value) is | |
| 4807 | let index = _imports.index_of(WASMGC_IMPORTS.IDENTITY_HASH) | |
| 4808 | ||
| 4809 | if !index? then | |
| 4810 | _report("object.identity_hash, which no host import provides") | |
| 4811 | return | |
| 4812 | fi | |
| 4813 | ||
| 4814 | gen(value) | |
| 4815 | _emit(INDEX_INSTRUCTION(Opcode.CALL, index)) | |
| 4816 | si | |
| 4817 | ||
| 4818 | // Reads the descriptor of the object on the stack, which traps | |
| 4819 | // where it is null. | |
| 4820 | _load_descriptor() is | |
| 4821 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, WASMGC_TYPE_REGISTRY.OBJECT_TYPE_INDEX, 0)) | |
| 4822 | si | |
| 4823 | ||
| 4824 | // A trait call with one possible implementation in the program is | |
| 4825 | // direct. Any other reads the trait table at the selector's row, | |
| 4826 | // offset by the receiver's class number. | |
| 4827 | _call_interface(call: Values.Call.INSTANCE) is | |
| 4828 | let function = call.function | |
| 4829 | let dispatch = _registry.dispatch | |
| 4830 | let root = cast Function?(function.root_specialized_from)! | |
| 4831 | let generic_arguments = DISPATCH_PLAN.own_type_arguments(_instance_of(function)) | |
| 4832 | ||
| 4833 | if _registry.discovering then | |
| 4834 | // A method generic in its own right has a row for each | |
| 4835 | // instantiation a call is recorded at. | |
| 4836 | if let arguments = generic_arguments, trait_type = _trait_instance(function.owner) then | |
| 4837 | dispatch.note_generic_call(root, trait_type, arguments) | |
| 4838 | else | |
| 4839 | _registry.note_dispatched(function) | |
| 4840 | ||
| 4841 | // The trait instance the call goes through, and the one | |
| 4842 | // declaring the member, are known to the planner, which | |
| 4843 | // gives each the implementors of its variant instances. | |
| 4844 | if let trait_type = _trait_instance(function.owner) then | |
| 4845 | dispatch.selector_of(root, trait_type) | |
| 4846 | fi | |
| 4847 | fi | |
| 4848 | ||
| 4849 | gen(call.from) | |
| 4850 | _lower_all(call.arguments) | |
| 4851 | return | |
| 4852 | fi | |
| 4853 | ||
| 4854 | let trait_type = _trait_instance(function.owner) | |
| 4855 | ||
| 4856 | let selector = | |
| 4857 | if let t = trait_type then | |
| 4858 | if let arguments = generic_arguments then dispatch.generic_selector_of(root, t, arguments) else dispatch.selector_of(root, t) fi | |
| 4859 | else | |
| 4860 | null | |
| 4861 | fi | |
| 4862 | ||
| 4863 | if !selector? then | |
| 4864 | // Nothing in the program implements the trait, so no value | |
| 4865 | // the call could be made on exists. The receiver and the | |
| 4866 | // arguments are still evaluated first, as they are before a | |
| 4867 | // call on null fails on .NET. | |
| 4868 | if let trait_class = trait_type /\ !dispatch.implementors(trait_class)? then | |
| 4869 | gen(call.from) | |
| 4870 | _lower_all(call.arguments) | |
| 4871 | _emit(Opcode.UNREACHABLE) | |
| 4872 | else | |
| 4873 | _report("a trait call to {function.name}") | |
| 4874 | fi | |
| 4875 | ||
| 4876 | return | |
| 4877 | fi | |
| 4878 | ||
| 4879 | if let only = dispatch.only_implementation(selector) then | |
| 4880 | _call_direct(call, only) | |
| 4881 | return | |
| 4882 | fi | |
| 4883 | ||
| 4884 | dispatch.use_selector(selector, _numbering) | |
| 4885 | ||
| 4886 | let receiver = _temp(_registry.object_reference) | |
| 4887 | ||
| 4888 | gen(call.from) | |
| 4889 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, receiver)) | |
| 4890 | ||
| 4891 | _lower_arguments(function, call.arguments) | |
| 4892 | ||
| 4893 | if _is_null_checked(call.from) then | |
| 4894 | _throw_if_null(receiver) | |
| 4895 | fi | |
| 4896 | ||
| 4897 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, receiver)) | |
| 4898 | _load_descriptor() | |
| 4899 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, WASMGC_TYPE_REGISTRY.DESCRIPTOR_TYPE_INDEX, WASMGC_TYPE_REGISTRY.DESCRIPTOR_CLASS_ID)) | |
| 4900 | _emit(I32_CONSTANT(selector.offset)) | |
| 4901 | _emit(Opcode.I32_ADD) | |
| 4902 | _emit(CALL_INDIRECT_INSTRUCTION(selector.signature, 0)) | |
| 4903 | si | |
| 4904 | ||
| 4905 | _is_trait_method(function: Function) -> bool => | |
| 4906 | cast Semantic.Symbols.Classy?(function.root_specialized_from.owner)?.is_trait ?? false | |
| 4907 | ||
| 4908 | _local_type_of_receiver(function: FUNCTION_INSTANCE) -> VALUE_TYPE? => | |
| 4909 | _registry.receiver_type(function) | |
| 4910 | ||
| 4911 | // The root class has no constructor body to run, so a call to it | |
| 4912 | // from a constructor does nothing. | |
| 4913 | _is_object_constructor(function: Function) -> bool => | |
| 4914 | if let owner: Semantic.Symbols.Classy = function.owner then owner.is_object else false fi | |
| 4915 | // The function value is held in a local: its receiver is the | |
| 4916 | // first argument, and its function is called after the rest. | |
| 4917 | visit(call: Values.Call.CLOSURE) is | |
| 4918 | let func_type = call.func_type ?? call.from.type | |
| 4919 | ||
| 4920 | let signature = if func_type? then _registry.closure_signature(_specialize(func_type)!) else null fi | |
| 4921 | ||
| 4922 | if !signature? then | |
| 4923 | _report("a call to a function value of type {func_type}") | |
| 4924 | return | |
| 4925 | fi | |
| 4926 | ||
| 4927 | let closure_type = VALUE_TYPE.of_type_index(_registry.closure_type_index, true) | |
| 4928 | let closure = _temp(closure_type) | |
| 4929 | ||
| 4930 | gen(call.from) | |
| 4931 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, closure)) | |
| 4932 | _throw_if_null(closure) | |
| 4933 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, _registry.closure_type_index, WASMGC_TYPE_REGISTRY.CLOSURE_RECEIVER)) | |
| 4934 | ||
| 4935 | let parameters = func_type!.arguments | |
| 4936 | ||
| 4937 | for i in 0..call.arguments.count do | |
| 4938 | gen(call.arguments[i]) | |
| 4939 | ||
| 4940 | if i < parameters.count then | |
| 4941 | _normalise_for(parameters[i]) | |
| 4942 | fi | |
| 4943 | od | |
| 4944 | ||
| 4945 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, closure)) | |
| 4946 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, _registry.closure_type_index, WASMGC_TYPE_REGISTRY.CLOSURE_FUNCTION)) | |
| 4947 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST, HEAP_TYPE.of_type_index(signature))) | |
| 4948 | _emit(TYPE_INSTRUCTION(Opcode.CALL_REF, signature)) | |
| 4949 | ||
| 4950 | // The signature holds a reference result as the root, so it is | |
| 4951 | // cast back to the type the call gives it. | |
| 4952 | if let heap_type = _value_type(call.type)?.heap_type() /\ !heap_type.matches(_registry.object_reference.heap_type()!) then | |
| 4953 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST_NULL, heap_type)) | |
| 4954 | fi | |
| 4955 | si | |
| 4956 | ||
| 4957 | // An operation the host provides is a call of its import rather | |
| 4958 | // than an operation the generator lowers. | |
| 4959 | visit(call: Values.Call.INNATE) is | |
| 4960 | if WASMGC_IMPORTS.is_host(call.innate_name) then | |
| 4961 | _call_host(call.innate_name, call.arguments, call.type) | |
| 4962 | elif _lower_representation(call.innate_name, call.arguments) then | |
| 4963 | return | |
| 4964 | elif call.innate_name.starts_with("nullable.") then | |
| 4965 | _lower_nullable(call.innate_name, call.from) | |
| 4966 | elif !call.is_lowered then | |
| 4967 | _registry.note_lowered(call) | |
| 4968 | gen(call.lower()) | |
| 4969 | elif _registry.was_lowered(call) then | |
| 4970 | // Lowered by an earlier pass over the same body: a body is | |
| 4971 | // lowered while discovering and for each instantiation. | |
| 4972 | gen(call.lowering) | |
| 4973 | fi | |
| 4974 | si | |
| 4975 | ||
| 4976 | // A value-type optional is a reference to the box its value lives | |
| 4977 | // in, null where it is absent. Its receiver is reached by address, | |
| 4978 | // which for an optional is the reference itself. | |
| 4979 | _lower_nullable(operation: string, from: Value?) is | |
| 4980 | if !from? then | |
| 4981 | _report("the operation {operation}") | |
| 4982 | return | |
| 4983 | fi | |
| 4984 | ||
| 4985 | let receiver = from | |
| 4986 | let inner = _optional_inner(receiver.type) | |
| 4987 | ||
| 4988 | if operation =~ "nullable.has_value" then | |
| 4989 | _gen_optional(receiver) | |
| 4990 | _emit(Opcode.REF_IS_NULL) | |
| 4991 | _emit(Opcode.I32_EQZ) | |
| 4992 | elif operation =~ "nullable.value" /\ inner? then | |
| 4993 | _gen_optional(receiver) | |
| 4994 | _check_present() | |
| 4995 | ||
| 4996 | if let index = _registry.box_index(_specialize(inner)) then | |
| 4997 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST, HEAP_TYPE.of_type_index(index))) | |
| 4998 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, index, WASMGC_TYPE_REGISTRY.BOX_VALUE)) | |
| 4999 | elif let held = _struct_of(inner) then | |
| 5000 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST, HEAP_TYPE.of_type_index(_registry.type_index(held)!))) | |
| 5001 | _copy_if_mutable(inner) | |
| 5002 | else | |
| 5003 | _report("a value-type optional of {inner}") | |
| 5004 | fi | |
| 5005 | else | |
| 5006 | _report("the operation {operation}") | |
| 5007 | fi | |
| 5008 | si | |
| 5009 | ||
| 5010 | // With a value-type optional's box on the stack, throws the core | |
| 5011 | // library's InvalidOperationException where it is absent, with the | |
| 5012 | // message .NET gives, rather than letting the cast to the box trap. | |
| 5013 | _check_present() is | |
| 5014 | let held = _top | |
| 5015 | ||
| 5016 | if !held? \/ !_can_throw(Semantic.Lookups.RuntimeException.INVALID_OPERATION) then | |
| 5017 | return | |
| 5018 | fi | |
| 5019 | ||
| 5020 | let box = _temp(held) | |
| 5021 | let string_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_string_type() | |
| 5022 | ||
| 5023 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, box)) | |
| 5024 | _emit(Opcode.REF_IS_NULL) | |
| 5025 | _throw_if(Semantic.Lookups.RuntimeException.INVALID_OPERATION, LIST[Value]([Values.Literal.STRING("Nullable object must have a value.", string_type)])) | |
| 5026 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, box)) | |
| 5027 | si | |
| 5028 | ||
| 5029 | // The value type a value-type optional `type`, or a reference to | |
| 5030 | // one, holds, or null where it is neither. | |
| 5031 | _optional_inner(type: Type?) -> Type? is | |
| 5032 | let specialized mut = _specialize(type) | |
| 5033 | ||
| 5034 | if let reference: Semantic.Types.REFERENCE = specialized then | |
| 5035 | specialized = reference.arguments[0] | |
| 5036 | fi | |
| 5037 | ||
| 5038 | if specialized? /\ specialized.is_optional /\ specialized.is_value_type /\ !specialized.is_maybe then | |
| 5039 | return specialized.optional_inner_type | |
| 5040 | fi | |
| 5041 | ||
| 5042 | return null | |
| 5043 | si | |
| 5044 | ||
| 5045 | // Lowers `value`, a value-type optional or its address, to the | |
| 5046 | // reference that holds it. | |
| 5047 | _gen_optional(value: Value) is | |
| 5048 | if let address: Values.ADDRESS = value then | |
| 5049 | gen(address.value) | |
| 5050 | else | |
| 5051 | gen(value) | |
| 5052 | fi | |
| 5053 | si | |
| 5054 | ||
| 5055 | _call_host(operation: string, arguments: Collections.List[Value], result: Type?) is | |
| 5056 | let index = _imports.index_of(operation) | |
| 5057 | ||
| 5058 | if !index? then | |
| 5059 | _report("{operation}, which no host import provides") | |
| 5060 | return | |
| 5061 | fi | |
| 5062 | ||
| 5063 | for argument in arguments do | |
| 5064 | gen(argument) | |
| 5065 | ||
| 5066 | if WASMGC_TYPES.is_string(argument.type) then | |
| 5067 | _to_host(argument.type) | |
| 5068 | elif WASMGC_TYPES.is_bigint(argument.type) then | |
| 5069 | _bigint_to_host(argument.type!) | |
| 5070 | fi | |
| 5071 | od | |
| 5072 | ||
| 5073 | _emit(INDEX_INSTRUCTION(Opcode.CALL, index)) | |
| 5074 | ||
| 5075 | if result? /\ WASMGC_TYPES.is_string(result) then | |
| 5076 | _from_host() | |
| 5077 | elif result? /\ WASMGC_TYPES.is_bigint(result) then | |
| 5078 | _bigint_from_host(result) | |
| 5079 | fi | |
| 5080 | si | |
| 5081 | ||
| 5082 | // Turns the bigint on the stack into the host's integer: the one | |
| 5083 | // its box holds, or, for a value its i64 field holds, one the host | |
| 5084 | // makes from it. | |
| 5085 | _bigint_to_host(type: Type) is | |
| 5086 | let held = _registry.class_of(type) | |
| 5087 | ||
| 5088 | if !held? then | |
| 5089 | _report("a bigint passed to the host") | |
| 5090 | return | |
| 5091 | fi | |
| 5092 | ||
| 5093 | let from_long = _imports.index_of("host.bigint_from_long") | |
| 5094 | ||
| 5095 | if !from_long? then | |
| 5096 | _report("a bigint passed to the host, with no host.bigint_from_long") | |
| 5097 | return | |
| 5098 | fi | |
| 5099 | ||
| 5100 | let index = _registry.type_index(held)! | |
| 5101 | let box = _temp(VALUE_TYPE.of_type_index(index, true)) | |
| 5102 | let big = _temp(VALUE_TYPE.of_abstract(AbstractHeapType.EXTERN)) | |
| 5103 | ||
| 5104 | _coerce_top(VALUE_TYPE.of_type_index(index, true)) | |
| 5105 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, box)) | |
| 5106 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, index, WASMGC_TYPE_REGISTRY.BIGINT_BIG)) | |
| 5107 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, big)) | |
| 5108 | _emit(Opcode.REF_IS_NULL) | |
| 5109 | ||
| 5110 | let from_small = LIST[INSTRUCTION]() | |
| 5111 | ||
| 5112 | from_small.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, box)) | |
| 5113 | from_small.add(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, index, WASMGC_TYPE_REGISTRY.BIGINT_SMALL)) | |
| 5114 | from_small.add(INDEX_INSTRUCTION(Opcode.CALL, from_long)) | |
| 5115 | ||
| 5116 | let held_big = LIST[INSTRUCTION]() | |
| 5117 | ||
| 5118 | held_big.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, big)) | |
| 5119 | ||
| 5120 | _emit( | |
| 5121 | IF_INSTRUCTION( | |
| 5122 | BLOCK_TYPE.of_value(VALUE_TYPE.of_abstract(AbstractHeapType.EXTERN)), | |
| 5123 | from_small, | |
| 5124 | held_big)) | |
| 5125 | si | |
| 5126 | ||
| 5127 | // A bigint whose value its i64 field holds: `value`, a long or an int. | |
| 5128 | _bigint_of_small(type: Type?, value: Value) is | |
| 5129 | let held = _registry.class_of(type) | |
| 5130 | ||
| 5131 | if !held? then | |
| 5132 | _report("a bigint made from a {value.type}") | |
| 5133 | return | |
| 5134 | fi | |
| 5135 | ||
| 5136 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.descriptor_global(held))) | |
| 5137 | gen(value) | |
| 5138 | ||
| 5139 | if WASMGC_TYPES.scalar(value.type) =~ Scalar.INT32 then | |
| 5140 | _emit(Opcode.I64_EXTEND_I32_S) | |
| 5141 | fi | |
| 5142 | ||
| 5143 | _emit(HEAP_TYPE_INSTRUCTION(Opcode.REF_NULL, HEAP_TYPE.of_abstract(AbstractHeapType.NOEXTERN))) | |
| 5144 | _emit(TYPE_INSTRUCTION(Opcode.STRUCT_NEW, _registry.type_index(held)!)) | |
| 5145 | si | |
| 5146 | ||
| 5147 | // Wraps the host integer on the stack in a new bigint box. | |
| 5148 | _bigint_from_host(type: Type) is | |
| 5149 | let held = _registry.class_of(type) | |
| 5150 | ||
| 5151 | // The core library's `_canonical` moves a value that fits in an | |
| 5152 | // i64 into the box's i64 field, which is where every other | |
| 5153 | // bigint operation looks for it first. | |
| 5154 | let canonical = _static_method(type, "_canonical", 1) | |
| 5155 | ||
| 5156 | if !canonical? then | |
| 5157 | _report("a bigint the host answers with, with no bigint._canonical") | |
| 5158 | return | |
| 5159 | fi | |
| 5160 | ||
| 5161 | if _registry.discovering then | |
| 5162 | _index_of(canonical) | |
| 5163 | return | |
| 5164 | fi | |
| 5165 | ||
| 5166 | if !held? then | |
| 5167 | _report("a bigint the host answers with") | |
| 5168 | return | |
| 5169 | fi | |
| 5170 | ||
| 5171 | let value = _temp(VALUE_TYPE.of_abstract(AbstractHeapType.EXTERN)) | |
| 5172 | ||
| 5173 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, value)) | |
| 5174 | _emit(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, _registry.descriptor_global(held))) | |
| 5175 | _emit(I64_CONSTANT(0L)) | |
| 5176 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, value)) | |
| 5177 | _emit(TYPE_INSTRUCTION(Opcode.STRUCT_NEW, _registry.type_index(held)!)) | |
| 5178 | ||
| 5179 | let box = _temp(VALUE_TYPE.of_type_index(_registry.type_index(held)!, false)) | |
| 5180 | ||
| 5181 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, box)) | |
| 5182 | _call(canonical, LIST[Value]([WASMGC_LOCAL_VALUE(box, type)])) | |
| 5183 | si | |
| 5184 | ||
| 5185 | // Converts the JavaScript string a host import answered with, which | |
| 5186 | // can be null, to a string. | |
| 5187 | _from_host() is | |
| 5188 | if let strings = _strings then | |
| 5189 | _emit(INDEX_INSTRUCTION(Opcode.CALL, strings.from_host_function)) | |
| 5190 | else | |
| 5191 | _report("a string the host answers with") | |
| 5192 | fi | |
| 5193 | si | |
| 5194 | ||
| 5195 | // Converts the ghūl string on the stack to the JavaScript string | |
| 5196 | // a host import takes. | |
| 5197 | _to_host(type: Type?) is | |
| 5198 | if let strings = _strings then | |
| 5199 | _coerce_top(strings.string_reference) | |
| 5200 | _emit(INDEX_INSTRUCTION(Opcode.CALL, strings.to_host_function)) | |
| 5201 | else | |
| 5202 | _report("a string passed to the host") | |
| 5203 | fi | |
| 5204 | si | |
| 5205 | ||
| 5206 | visit(store: Values.Store.SYMBOL) is _report("a store to {store.symbol.name}") si | |
| 5207 | ||
| 5208 | visit(store: Values.Store.LOCAL_VARIABLE) is | |
| 5209 | if let state_machine_field = Values.state_machine_field_for(store.symbol) then | |
| 5210 | gen(_frame_field_store(state_machine_field, store.value)) | |
| 5211 | elif let variable = cast Semantic.Symbols.Variable?(store.symbol) then | |
| 5212 | if let cell = _cell_index(variable.ir_local) then | |
| 5213 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, _local(variable.ir_local))) | |
| 5214 | gen(store.value) | |
| 5215 | ||
| 5216 | _normalise_for(variable.ir_local.type) | |
| 5217 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_SET, cell, WASMGC_TYPE_REGISTRY.BOX_VALUE)) | |
| 5218 | return | |
| 5219 | fi | |
| 5220 | ||
| 5221 | if _reference_cell_of(variable.ir_local)? then | |
| 5222 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_GET, _local(variable.ir_local))) | |
| 5223 | gen(store.value) | |
| 5224 | ||
| 5225 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_SET, _registry.reference_cell_type_index, WASMGC_TYPE_REGISTRY.REFERENCE_CELL_VALUE)) | |
| 5226 | return | |
| 5227 | fi | |
| 5228 | ||
| 5229 | gen(store.value) | |
| 5230 | ||
| 5231 | _normalise_for(variable.ir_local.type) | |
| 5232 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, _local(variable.ir_local))) | |
| 5233 | else | |
| 5234 | _report("a store to {store.symbol.name}") | |
| 5235 | fi | |
| 5236 | si | |
| 5237 | ||
| 5238 | visit(store: Values.Store.LOCAL_ARGUMENT) is | |
| 5239 | let symbol = store.symbol | |
| 5240 | ||
| 5241 | if let state_machine_field = Values.state_machine_field_for(symbol) then | |
| 5242 | gen(_frame_field_store(state_machine_field, store.value)) | |
| 5243 | elif let packed_local = Values.packed_parameter_local_for(symbol) then | |
| 5244 | gen(store.value) | |
| 5245 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, _local(packed_local))) | |
| 5246 | elif let index = _argument_index(symbol) then | |
| 5247 | gen(store.value) | |
| 5248 | ||
| 5249 | _normalise_for(symbol.type) | |
| 5250 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_SET, index)) | |
| 5251 | else | |
| 5252 | _report("a store to an argument of another function") | |
| 5253 | fi | |
| 5254 | si | |
| 5255 | ||
| 5256 | visit(store: Values.Store.STATIC_FIELD) is | |
| 5257 | _global_set(store.symbol, store.value) | |
| 5258 | si | |
| 5259 | ||
| 5260 | visit(store: Values.Store.GLOBAL_FIELD) is | |
| 5261 | _global_set(store.symbol, store.value) | |
| 5262 | si | |
| 5263 | ||
| 5264 | visit(store: Values.Store.INSTANCE_FIELD) is | |
| 5265 | if let `field = _instance_field(store.from, store.symbol) then | |
| 5266 | _struct_receiver(store.from!) | |
| 5267 | _coerce_top(VALUE_TYPE.of_type_index(`field.type_index, true)) | |
| 5268 | ||
| 5269 | let checked = _is_null_checked(store.from) /\ _can_throw(Semantic.Lookups.RuntimeException.NULL_REFERENCE) | |
| 5270 | let receiver = if checked then _temp(VALUE_TYPE.of_type_index(`field.type_index, true)) else 0 fi | |
| 5271 | ||
| 5272 | if checked then | |
| 5273 | _emit(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, receiver)) | |
| 5274 | fi | |
| 5275 | ||
| 5276 | gen(store.value) | |
| 5277 | ||
| 5278 | _normalise_for(store.type) | |
| 5279 | ||
| 5280 | if checked then | |
| 5281 | _throw_if_null(receiver) | |
| 5282 | fi | |
| 5283 | _emit(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_SET, `field.type_index, `field.field_index)) | |
| 5284 | fi | |
| 5285 | si | |
| 5286 | si | |
| 5287 | si |