Appearance
| 1 | namespace IR.Values is | |
| 2 | use Semantic.Types.Type | |
| 3 | ||
| 4 | // The result of a coalescing member access `a?.b` or method call | |
| 5 | // `a?.m(...)`. The receiver is short-circuited to absent on null | |
| 6 | // (reference-type optional) or HasValue=false (value-type optional | |
| 7 | // via NULLABLE / MAYBE); otherwise the member value - a field or | |
| 8 | // property load, or a whole call including argument evaluation - | |
| 9 | // is produced against the unwrapped receiver. | |
| 10 | // | |
| 11 | // Built by COMPILE_ACCESS.build_coalesce_wrap. The construction | |
| 12 | // wires sub-Values whose gen() composes the final IL inline - no | |
| 13 | // `if let` tree-rewrite, no semantic-pass spilling. | |
| 14 | // | |
| 15 | // The receiver is held in a temporary, or, for a value-type | |
| 16 | // receiver that already has an address, read again through that | |
| 17 | // address. The member value reads its receiver from the stack, as | |
| 18 | // the value extract and the presence test read the receiver's | |
| 19 | // address. | |
| 20 | // | |
| 21 | // The null_value is `ldnull` for a reference-type result and a | |
| 22 | // fresh DEFAULT for a value-type result (DEFAULT hoists a | |
| 23 | // `.locals init` and pushes the zero value). A void member value | |
| 24 | // (a coalesced call to a void method) has no null_value - neither | |
| 25 | // arm leaves a result on the stack. | |
| 26 | // | |
| 27 | // A static member reached through `?.` never consumes the tested | |
| 28 | // receiver: with `receiver_consumed` false the present arm is the | |
| 29 | // member value alone, and the value-type shape skips the value | |
| 30 | // extract entirely. | |
| 31 | class COALESCE_LOAD: Composite is | |
| 32 | receiver: Value | |
| 33 | presence_test: Value? | |
| 34 | value_extract: Value? | |
| 35 | member_load: Value | |
| 36 | null_value: Value? | |
| 37 | _result_type: Type | |
| 38 | _receiver_is_value_type: bool | |
| 39 | _receiver_consumed: bool | |
| 40 | ||
| 41 | type: Type => _result_type | |
| 42 | is_lightweight_pure: bool => false | |
| 43 | ||
| 44 | receiver_is_value_type: bool => _receiver_is_value_type | |
| 45 | ||
| 46 | // Reference-type receiver constructor. `member_load`'s | |
| 47 | // receiver should be a STACK_TOP - gen() leaves the receiver | |
| 48 | // on the stack ready for the access op. | |
| 49 | init( | |
| 50 | receiver: Value, | |
| 51 | member_load: Value, | |
| 52 | null_value: Value?, | |
| 53 | result_type: Type, | |
| 54 | receiver_consumed: bool | |
| 55 | ) is | |
| 56 | super.init() | |
| 57 | ||
| 58 | self.receiver = receiver | |
| 59 | self.member_load = member_load | |
| 60 | self.null_value = null_value | |
| 61 | self._result_type = result_type | |
| 62 | self._receiver_is_value_type = false | |
| 63 | self._receiver_consumed = receiver_consumed | |
| 64 | si | |
| 65 | ||
| 66 | // Value-type receiver constructor. `receiver`'s address is | |
| 67 | // pushed once, dup'd, and used by `presence_test` (HasValue) | |
| 68 | // and `value_extract` (Value). Both receivers should consume | |
| 69 | // the address from the stack - built with a STACK_TOP_ADDRESS | |
| 70 | // standing in for 'this'. `value_extract` is null when the | |
| 71 | // member value doesn't consume the receiver. | |
| 72 | init( | |
| 73 | receiver: Value, | |
| 74 | presence_test: Value, | |
| 75 | value_extract: Value?, | |
| 76 | member_load: Value, | |
| 77 | null_value: Value?, | |
| 78 | result_type: Type, | |
| 79 | receiver_consumed: bool | |
| 80 | ) is | |
| 81 | super.init() | |
| 82 | ||
| 83 | self.receiver = receiver | |
| 84 | self.presence_test = presence_test | |
| 85 | self.value_extract = value_extract | |
| 86 | self.member_load = member_load | |
| 87 | self.null_value = null_value | |
| 88 | self._result_type = result_type | |
| 89 | self._receiver_is_value_type = true | |
| 90 | self._receiver_consumed = receiver_consumed | |
| 91 | si | |
| 92 | ||
| 93 | expand() -> BLOCK is | |
| 94 | let expansion = IR.EXPANSION(type) | |
| 95 | let has_result = !type.is_void | |
| 96 | ||
| 97 | // A repeatable read of the receiver: its address for a value | |
| 98 | // type, its value for a reference. | |
| 99 | let read_receiver: () -> Value = | |
| 100 | if _receiver_is_value_type /\ receiver.has_address then | |
| 101 | () => cast Value(ADDRESS(receiver)) | |
| 102 | else | |
| 103 | let held = expansion.temp("coalesce.receiver", receiver.type!) | |
| 104 | ||
| 105 | expansion.store(held, receiver) | |
| 106 | ||
| 107 | if _receiver_is_value_type then | |
| 108 | () => cast Value(ADDRESS(held.load())) | |
| 109 | else | |
| 110 | () => held.load() | |
| 111 | fi | |
| 112 | fi | |
| 113 | ||
| 114 | let result: IR.LOCAL? = if has_result then expansion.temp("coalesce.result", type) else null fi | |
| 115 | ||
| 116 | let done = expansion.region() | |
| 117 | let absent = expansion.region() | |
| 118 | ||
| 119 | let present: Value = | |
| 120 | if _receiver_is_value_type then | |
| 121 | expansion.exit_if(absent, IR.BRANCH.Z, STEPS([read_receiver(), presence_test!]:Value)) | |
| 122 | ||
| 123 | if _receiver_consumed then | |
| 124 | STEPS([read_receiver(), value_extract!, member_load]:Value) | |
| 125 | else | |
| 126 | member_load | |
| 127 | fi | |
| 128 | else | |
| 129 | expansion.exit_if(absent, IR.BRANCH.Z, read_receiver()) | |
| 130 | ||
| 131 | if _receiver_consumed then | |
| 132 | STEPS([read_receiver(), member_load]:Value) | |
| 133 | else | |
| 134 | member_load | |
| 135 | fi | |
| 136 | fi | |
| 137 | ||
| 138 | if let r = result then | |
| 139 | expansion.store(r, present) | |
| 140 | else | |
| 141 | expansion.add(present) | |
| 142 | fi | |
| 143 | ||
| 144 | expansion.exit(done) | |
| 145 | expansion.close(absent) | |
| 146 | ||
| 147 | if let r = result, absent_value = null_value then | |
| 148 | expansion.store(r, absent_value) | |
| 149 | fi | |
| 150 | ||
| 151 | expansion.close(done) | |
| 152 | ||
| 153 | if let r = result then | |
| 154 | expansion.add(r.load()) | |
| 155 | fi | |
| 156 | ||
| 157 | return expansion.result | |
| 158 | si | |
| 159 | ||
| 160 | accept_address(visitor: Visitor) is | |
| 161 | visitor.visit_address(self) | |
| 162 | si | |
| 163 | ||
| 164 | to_string() -> string => | |
| 165 | "coalesce-load:[{type}]({receiver},{member_load})" | |
| 166 | si | |
| 167 | ||
| 168 | // A no-op Value that types as `T` and stands in for "the value | |
| 169 | // (reference or struct) already on the IL stack". Embedded as | |
| 170 | // the `from` of a `Load.INSTANCE_FIELD` / property-getter CALL | |
| 171 | // when an outer composite (COALESCE_LOAD) has already pushed | |
| 172 | // the receiver - letting the inner load emit only its trailing | |
| 173 | // access op. | |
| 174 | class STACK_TOP: Value is | |
| 175 | _type: Type? | |
| 176 | ||
| 177 | type: Type? => _type | |
| 178 | is_value_type: bool => _type? /\ _type.is_value_type | |
| 179 | is_lightweight_pure: bool => true | |
| 180 | ||
| 181 | init(type: Type) is | |
| 182 | super.init() | |
| 183 | self._type = type | |
| 184 | si | |
| 185 | ||
| 186 | accept(visitor: Visitor) is | |
| 187 | visitor.visit(self) | |
| 188 | si | |
| 189 | ||
| 190 | to_string() -> string => | |
| 191 | "stack-top:[{type}]" | |
| 192 | si | |
| 193 | ||
| 194 | // Like STACK_TOP but reports `has_address` - the IL stack | |
| 195 | // already holds a managed pointer (e.g. the dup'd address of a | |
| 196 | // value-type optional receiver). The struct-aware load paths | |
| 197 | // gate on `has_address`, so an `INSTANCE_FIELD` / property | |
| 198 | // built against this stands in for the implicit `ADDRESS` step | |
| 199 | // and emits only its trailing op. | |
| 200 | class STACK_TOP_ADDRESS: Value is | |
| 201 | _type: Type? | |
| 202 | ||
| 203 | type: Type? => _type | |
| 204 | is_value_type: bool => _type? /\ _type.is_value_type | |
| 205 | has_address: bool => true | |
| 206 | is_lightweight_pure: bool => true | |
| 207 | ||
| 208 | init(type: Type) is | |
| 209 | super.init() | |
| 210 | self._type = type | |
| 211 | si | |
| 212 | ||
| 213 | accept(visitor: Visitor) is | |
| 214 | visitor.visit(self) | |
| 215 | si | |
| 216 | ||
| 217 | accept_address(visitor: Visitor) is | |
| 218 | visitor.visit_address(self) | |
| 219 | si | |
| 220 | ||
| 221 | to_string() -> string => | |
| 222 | "stack-top-address:[{type}]" | |
| 223 | si | |
| 224 | si |