Appearance
| 1 | namespace IR.WasmGC is | |
| 2 | use IR.Program | |
| 3 | ||
| 4 | use Wasm.FUNCTION | |
| 5 | use Wasm.INSTRUCTION | |
| 6 | use Wasm.SIMPLE_INSTRUCTION | |
| 7 | use Wasm.INDEX_INSTRUCTION | |
| 8 | use Wasm.HEAP_TYPE_INSTRUCTION | |
| 9 | use Wasm.TYPE_FIELD_INSTRUCTION | |
| 10 | use Wasm.BLOCK_INSTRUCTION | |
| 11 | use Wasm.BLOCK_TYPE | |
| 12 | use Wasm.HEAP_TYPE | |
| 13 | use Wasm.Opcode | |
| 14 | ||
| 15 | use Collections.LIST | |
| 16 | use Collections.MAP | |
| 17 | ||
| 18 | use Semantic.Types.Type | |
| 19 | use Semantic.Symbols.Symbol | |
| 20 | use Semantic.Symbols.Classy | |
| 21 | use Semantic.Symbols.Function | |
| 22 | use Semantic.Symbols.Property | |
| 23 | ||
| 24 | use IR.Emitter.SRM_MEMBER_ORDER | |
| 25 | use IR.Emitter.SRM_EMITTED_TYPE | |
| 26 | ||
| 27 | /// The displayers: for each type with no text of its own, a function | |
| 28 | /// that writes a value of it for display through the core library's | |
| 29 | /// display state. A sequence is written as its elements, a tuple as | |
| 30 | /// its parts, and anything else as its type and public properties. | |
| 31 | /// | |
| 32 | /// A displayer is reached through its type's descriptor, and only | |
| 33 | /// where the program renders values for display at all. | |
| 34 | class WASMGC_DISPLAY is | |
| 35 | _registry: WASMGC_TYPE_REGISTRY | |
| 36 | _plan: DISPLAY_PLAN | |
| 37 | ||
| 38 | init(registry: WASMGC_TYPE_REGISTRY) is | |
| 39 | _registry = registry | |
| 40 | _plan = registry.display_plan | |
| 41 | si | |
| 42 | ||
| 43 | /// Reserves the displayer of each type that has one, once the walk | |
| 44 | /// has numbered everything they call, and records it in the type's | |
| 45 | /// descriptor. | |
| 46 | reserve(numbering: WASMGC_FUNCTION_NUMBERING, strings: WASMGC_STRINGS?) is | |
| 47 | let helpers = _plan.helpers | |
| 48 | ||
| 49 | if !helpers? \/ !strings? then | |
| 50 | return | |
| 51 | fi | |
| 52 | ||
| 53 | for type in _registry.classes_and_arrays do | |
| 54 | if let body = _body(type, helpers, numbering, strings) then | |
| 55 | let signature = _registry.display_signature | |
| 56 | ||
| 57 | _registry.set_displayer( | |
| 58 | type, | |
| 59 | numbering.reserve(() -> FUNCTION => FUNCTION(signature, [], body))) | |
| 60 | fi | |
| 61 | od | |
| 62 | si | |
| 63 | ||
| 64 | // The displayer of `type`, taking the value and the state, or null | |
| 65 | // where it has none or something it calls was not compiled. | |
| 66 | _body( | |
| 67 | type: CLASS_INSTANCE, | |
| 68 | helpers: DISPLAY_HELPERS, | |
| 69 | numbering: WASMGC_FUNCTION_NUMBERING, | |
| 70 | strings: WASMGC_STRINGS | |
| 71 | ) -> LIST[INSTRUCTION]? is | |
| 72 | let calls = _plan.calls(type, helpers) | |
| 73 | ||
| 74 | if calls.count == 0 then | |
| 75 | return null | |
| 76 | fi | |
| 77 | ||
| 78 | let indices = LIST[int]() | |
| 79 | ||
| 80 | for call in calls do | |
| 81 | if let index = numbering.indexed(call) then | |
| 82 | indices.add(index) | |
| 83 | else | |
| 84 | return null | |
| 85 | fi | |
| 86 | od | |
| 87 | ||
| 88 | let state = HEAP_TYPE.of_type_index(_registry.type_index(helpers.state)!) | |
| 89 | ||
| 90 | let load_state = [ | |
| 91 | INDEX_INSTRUCTION(Opcode.LOCAL_GET, 1), | |
| 92 | HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST_NULL, state) | |
| 93 | ]: INSTRUCTION | |
| 94 | ||
| 95 | let body = LIST[INSTRUCTION]() | |
| 96 | ||
| 97 | if _plan.element_type(type)? then | |
| 98 | body.add_range(load_state) | |
| 99 | body.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, 0)) | |
| 100 | body.add(INDEX_INSTRUCTION(Opcode.CALL, indices[0])) | |
| 101 | elif _plan.is_tuple(type) then | |
| 102 | let held = HEAP_TYPE.of_type_index(_registry.type_index(type)!) | |
| 103 | ||
| 104 | body.add_range(load_state) | |
| 105 | body.add(INDEX_INSTRUCTION(Opcode.CALL, indices[0])) | |
| 106 | ||
| 107 | for i in 0..type.arguments.count do | |
| 108 | body.add_range(load_state) | |
| 109 | body.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, 0)) | |
| 110 | body.add(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST_NULL, held)) | |
| 111 | ||
| 112 | // A tuple's first field is its descriptor. | |
| 113 | body.add(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, _registry.type_index(type)!, i + 1)) | |
| 114 | body.add(INDEX_INSTRUCTION(Opcode.CALL, indices[i + 1])) | |
| 115 | od | |
| 116 | ||
| 117 | body.add_range(load_state) | |
| 118 | body.add(INDEX_INSTRUCTION(Opcode.CALL, indices[indices.count - 1])) | |
| 119 | else | |
| 120 | body.add_range(load_state) | |
| 121 | body.add(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, strings.literal(_plan.display_name(type)))) | |
| 122 | body.add(INDEX_INSTRUCTION(Opcode.CALL, indices[0])) | |
| 123 | ||
| 124 | let members = LIST[INSTRUCTION]() | |
| 125 | ||
| 126 | // The calls after the opening one, in the order `_calls` | |
| 127 | // gives them. | |
| 128 | let next mut = 1 | |
| 129 | ||
| 130 | for member in _plan.members(type) do | |
| 131 | members.add_range(load_state) | |
| 132 | members.add(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, strings.literal(member.name))) | |
| 133 | members.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, 0)) | |
| 134 | ||
| 135 | if let getter = member.getter then | |
| 136 | if let receiver = _registry.receiver_type(getter)?.heap_type() then | |
| 137 | members.add(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST_NULL, receiver)) | |
| 138 | fi | |
| 139 | ||
| 140 | members.add(INDEX_INSTRUCTION(Opcode.CALL, indices[next])) | |
| 141 | next = next + 1 | |
| 142 | elif let `field = member.`field /\ _registry.field_of(`field, type)? then | |
| 143 | let location = _registry.field_of(`field, type)! | |
| 144 | ||
| 145 | members.add(HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST_NULL, HEAP_TYPE.of_type_index(location.type_index))) | |
| 146 | members.add(TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, location.type_index, location.field_index)) | |
| 147 | else | |
| 148 | return null | |
| 149 | fi | |
| 150 | ||
| 151 | members.add(INDEX_INSTRUCTION(Opcode.CALL, indices[next])) | |
| 152 | next = next + 1 | |
| 153 | ||
| 154 | // The state answers false once its element budget is | |
| 155 | // spent. | |
| 156 | members.add(SIMPLE_INSTRUCTION(Opcode.I32_EQZ)) | |
| 157 | members.add(INDEX_INSTRUCTION(Opcode.BR_IF, 0)) | |
| 158 | od | |
| 159 | ||
| 160 | body.add(BLOCK_INSTRUCTION(Opcode.BLOCK, BLOCK_TYPE(), members)) | |
| 161 | ||
| 162 | body.add_range(load_state) | |
| 163 | body.add(INDEX_INSTRUCTION(Opcode.CALL, indices[indices.count - 1])) | |
| 164 | fi | |
| 165 | ||
| 166 | return body | |
| 167 | si | |
| 168 | si | |
| 169 | si |