Appearance
| 1 | namespace IR.WasmGC is | |
| 2 | use Wasm.INSTRUCTION | |
| 3 | use Wasm.SIMPLE_INSTRUCTION | |
| 4 | use Wasm.I32_CONSTANT | |
| 5 | use Wasm.I64_CONSTANT | |
| 6 | use Wasm.F32_CONSTANT | |
| 7 | use Wasm.F64_CONSTANT | |
| 8 | use Wasm.INDEX_INSTRUCTION | |
| 9 | use Wasm.Opcode | |
| 10 | use Wasm.NumericType | |
| 11 | ||
| 12 | use Collections.LIST | |
| 13 | ||
| 14 | /// How the WasmGC backend encodes the target-neutral operations. | |
| 15 | class WASMGC_OPERATIONS is | |
| 16 | /// The instructions replacing a scalar of `type` on the stack with | |
| 17 | /// its hash code: equal values hash alike, zero of either sign and | |
| 18 | /// every NaN included. | |
| 19 | /// | |
| 20 | /// - type: how the scalar is held | |
| 21 | /// - scratch: a local of type i64, which a 64-bit value is folded | |
| 22 | /// through | |
| 23 | /// - float_scratch: a local of `type`, which a floating value is | |
| 24 | /// tested through; unused for an integer | |
| 25 | hash(type: NumericType, scratch: int, float_scratch: int) -> LIST[INSTRUCTION] static is | |
| 26 | let result = LIST[INSTRUCTION]() | |
| 27 | ||
| 28 | case type | |
| 29 | when NumericType.I32 then | |
| 30 | return result | |
| 31 | when NumericType.F32 then | |
| 32 | _canonical_float(result, float_scratch, F32_CONSTANT(0.0s), Opcode.F32_ADD, Opcode.F32_NE) | |
| 33 | result.add(SIMPLE_INSTRUCTION(Opcode.I32_REINTERPRET_F32)) | |
| 34 | return result | |
| 35 | when NumericType.F64 then | |
| 36 | _canonical_float(result, float_scratch, F64_CONSTANT(0.0), Opcode.F64_ADD, Opcode.F64_NE) | |
| 37 | result.add(SIMPLE_INSTRUCTION(Opcode.I64_REINTERPRET_F64)) | |
| 38 | else | |
| 39 | esac | |
| 40 | ||
| 41 | result.add(INDEX_INSTRUCTION(Opcode.LOCAL_TEE, scratch)) | |
| 42 | result.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, scratch)) | |
| 43 | result.add(I64_CONSTANT(32L)) | |
| 44 | result.add(SIMPLE_INSTRUCTION(Opcode.I64_SHR_U)) | |
| 45 | result.add(SIMPLE_INSTRUCTION(Opcode.I64_XOR)) | |
| 46 | result.add(SIMPLE_INSTRUCTION(Opcode.I32_WRAP_I64)) | |
| 47 | ||
| 48 | return result | |
| 49 | si | |
| 50 | ||
| 51 | /// Replaces the value of `type` on the stack with the integer bits | |
| 52 | /// `=~` compares it by: a floating value has NaN and -0 made canonical | |
| 53 | /// first, as `hash` does, and an integer stays as it is. | |
| 54 | /// | |
| 55 | /// - type: the kind of the value | |
| 56 | /// - float_scratch: a local of `type`, used for a floating value | |
| 57 | canonical_bits(type: NumericType, float_scratch: int) -> LIST[INSTRUCTION] static is | |
| 58 | let result = LIST[INSTRUCTION]() | |
| 59 | ||
| 60 | if type == NumericType.F32 then | |
| 61 | _canonical_float(result, float_scratch, F32_CONSTANT(0.0s), Opcode.F32_ADD, Opcode.F32_NE) | |
| 62 | result.add(SIMPLE_INSTRUCTION(Opcode.I32_REINTERPRET_F32)) | |
| 63 | elif type == NumericType.F64 then | |
| 64 | _canonical_float(result, float_scratch, F64_CONSTANT(0.0), Opcode.F64_ADD, Opcode.F64_NE) | |
| 65 | result.add(SIMPLE_INSTRUCTION(Opcode.I64_REINTERPRET_F64)) | |
| 66 | fi | |
| 67 | ||
| 68 | return result | |
| 69 | si | |
| 70 | ||
| 71 | /// The instruction comparing two of what `canonical_bits` gives for | |
| 72 | /// `type`. | |
| 73 | bits_equal(type: NumericType) -> Opcode static => | |
| 74 | if type == NumericType.I32 \/ type == NumericType.F32 then Opcode.I32_EQ else Opcode.I64_EQ fi | |
| 75 | ||
| 76 | // Replaces the floating value on the stack with zero where it is a | |
| 77 | // NaN, and otherwise with itself plus zero, which turns -0 into +0: | |
| 78 | // the values `=~` reports equal then have the same bits. | |
| 79 | _canonical_float(result: LIST[INSTRUCTION], scratch: int, zero: INSTRUCTION, add: Opcode, not_equal: Opcode) static is | |
| 80 | result.add(INDEX_INSTRUCTION(Opcode.LOCAL_SET, scratch)) | |
| 81 | result.add(zero) | |
| 82 | result.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, scratch)) | |
| 83 | result.add(zero) | |
| 84 | result.add(SIMPLE_INSTRUCTION(add)) | |
| 85 | result.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, scratch)) | |
| 86 | result.add(INDEX_INSTRUCTION(Opcode.LOCAL_GET, scratch)) | |
| 87 | result.add(SIMPLE_INSTRUCTION(not_equal)) | |
| 88 | result.add(SIMPLE_INSTRUCTION(Opcode.SELECT)) | |
| 89 | si | |
| 90 | ||
| 91 | /// The instruction for a binary arithmetic or bitwise operation on | |
| 92 | /// two values of `type`, or null where wasm has none: there is no | |
| 93 | /// floating remainder, and the bitwise operations are integer | |
| 94 | /// only. NEGATE and COMPLEMENT take one operand and are encoded by | |
| 95 | /// the lowering. | |
| 96 | arithmetic(operation: ArithmeticOperation, type: NumericType) -> Opcode? static => | |
| 97 | case type | |
| 98 | when NumericType.I32 then _i32_arithmetic(operation) | |
| 99 | when NumericType.I64 then _i64_arithmetic(operation) | |
| 100 | when NumericType.F32 then _f32_arithmetic(operation) | |
| 101 | when NumericType.F64 then _f64_arithmetic(operation) | |
| 102 | else null | |
| 103 | esac | |
| 104 | ||
| 105 | _i32_arithmetic(operation: ArithmeticOperation) -> Opcode? static => | |
| 106 | case operation | |
| 107 | when ArithmeticOperation.ADD then Opcode.I32_ADD | |
| 108 | when ArithmeticOperation.SUBTRACT then Opcode.I32_SUB | |
| 109 | when ArithmeticOperation.MULTIPLY then Opcode.I32_MUL | |
| 110 | when ArithmeticOperation.DIVIDE then Opcode.I32_DIV_S | |
| 111 | when ArithmeticOperation.DIVIDE_UNSIGNED then Opcode.I32_DIV_U | |
| 112 | when ArithmeticOperation.REMAINDER then Opcode.I32_REM_S | |
| 113 | when ArithmeticOperation.REMAINDER_UNSIGNED then Opcode.I32_REM_U | |
| 114 | when ArithmeticOperation.AND then Opcode.I32_AND | |
| 115 | when ArithmeticOperation.OR then Opcode.I32_OR | |
| 116 | when ArithmeticOperation.XOR then Opcode.I32_XOR | |
| 117 | when ArithmeticOperation.SHIFT_LEFT then Opcode.I32_SHL | |
| 118 | when ArithmeticOperation.SHIFT_RIGHT then Opcode.I32_SHR_S | |
| 119 | when ArithmeticOperation.SHIFT_RIGHT_UNSIGNED then Opcode.I32_SHR_U | |
| 120 | else null | |
| 121 | esac | |
| 122 | ||
| 123 | _i64_arithmetic(operation: ArithmeticOperation) -> Opcode? static => | |
| 124 | case operation | |
| 125 | when ArithmeticOperation.ADD then Opcode.I64_ADD | |
| 126 | when ArithmeticOperation.SUBTRACT then Opcode.I64_SUB | |
| 127 | when ArithmeticOperation.MULTIPLY then Opcode.I64_MUL | |
| 128 | when ArithmeticOperation.DIVIDE then Opcode.I64_DIV_S | |
| 129 | when ArithmeticOperation.DIVIDE_UNSIGNED then Opcode.I64_DIV_U | |
| 130 | when ArithmeticOperation.REMAINDER then Opcode.I64_REM_S | |
| 131 | when ArithmeticOperation.REMAINDER_UNSIGNED then Opcode.I64_REM_U | |
| 132 | when ArithmeticOperation.AND then Opcode.I64_AND | |
| 133 | when ArithmeticOperation.OR then Opcode.I64_OR | |
| 134 | when ArithmeticOperation.XOR then Opcode.I64_XOR | |
| 135 | when ArithmeticOperation.SHIFT_LEFT then Opcode.I64_SHL | |
| 136 | when ArithmeticOperation.SHIFT_RIGHT then Opcode.I64_SHR_S | |
| 137 | when ArithmeticOperation.SHIFT_RIGHT_UNSIGNED then Opcode.I64_SHR_U | |
| 138 | else null | |
| 139 | esac | |
| 140 | ||
| 141 | _f32_arithmetic(operation: ArithmeticOperation) -> Opcode? static => | |
| 142 | case operation | |
| 143 | when ArithmeticOperation.ADD then Opcode.F32_ADD | |
| 144 | when ArithmeticOperation.SUBTRACT then Opcode.F32_SUB | |
| 145 | when ArithmeticOperation.MULTIPLY then Opcode.F32_MUL | |
| 146 | when ArithmeticOperation.DIVIDE then Opcode.F32_DIV | |
| 147 | else null | |
| 148 | esac | |
| 149 | ||
| 150 | _f64_arithmetic(operation: ArithmeticOperation) -> Opcode? static => | |
| 151 | case operation | |
| 152 | when ArithmeticOperation.ADD then Opcode.F64_ADD | |
| 153 | when ArithmeticOperation.SUBTRACT then Opcode.F64_SUB | |
| 154 | when ArithmeticOperation.MULTIPLY then Opcode.F64_MUL | |
| 155 | when ArithmeticOperation.DIVIDE then Opcode.F64_DIV | |
| 156 | else null | |
| 157 | esac | |
| 158 | ||
| 159 | /// The comparison of two values of `type`, and whether its answer | |
| 160 | /// is then negated. An unsigned integer comparison orders by | |
| 161 | /// magnitude. An unsigned floating comparison answers true when | |
| 162 | /// the operands are unordered, which is the negation of the | |
| 163 | /// ordered comparison of the opposite sense. | |
| 164 | comparison(comparison: Comparison, type: NumericType, is_unsigned: bool) -> (opcode: Opcode, negates: bool)? static is | |
| 165 | if type == NumericType.F32 \/ type == NumericType.F64 then | |
| 166 | return _float_comparison(comparison, type == NumericType.F64, is_unsigned) | |
| 167 | fi | |
| 168 | ||
| 169 | let wide = type == NumericType.I64 | |
| 170 | ||
| 171 | return | |
| 172 | case comparison | |
| 173 | when Comparison.EQUAL then (if wide then Opcode.I64_EQ else Opcode.I32_EQ fi, false) | |
| 174 | when Comparison.NOT_EQUAL then (if wide then Opcode.I64_NE else Opcode.I32_NE fi, false) | |
| 175 | when Comparison.LESS then (_integer(wide, is_unsigned, Opcode.I32_LT_S, Opcode.I32_LT_U, Opcode.I64_LT_S, Opcode.I64_LT_U), false) | |
| 176 | when Comparison.LESS_OR_EQUAL then (_integer(wide, is_unsigned, Opcode.I32_LE_S, Opcode.I32_LE_U, Opcode.I64_LE_S, Opcode.I64_LE_U), false) | |
| 177 | when Comparison.GREATER then (_integer(wide, is_unsigned, Opcode.I32_GT_S, Opcode.I32_GT_U, Opcode.I64_GT_S, Opcode.I64_GT_U), false) | |
| 178 | when Comparison.GREATER_OR_EQUAL then (_integer(wide, is_unsigned, Opcode.I32_GE_S, Opcode.I32_GE_U, Opcode.I64_GE_S, Opcode.I64_GE_U), false) | |
| 179 | esac | |
| 180 | si | |
| 181 | ||
| 182 | _integer(wide: bool, is_unsigned: bool, i32_s: Opcode, i32_u: Opcode, i64_s: Opcode, i64_u: Opcode) -> Opcode static => | |
| 183 | if wide then | |
| 184 | if is_unsigned then i64_u else i64_s fi | |
| 185 | else | |
| 186 | if is_unsigned then i32_u else i32_s fi | |
| 187 | fi | |
| 188 | ||
| 189 | _float_comparison(comparison: Comparison, wide: bool, unordered: bool) -> (opcode: Opcode, negates: bool) static is | |
| 190 | if !unordered then | |
| 191 | return | |
| 192 | case comparison | |
| 193 | when Comparison.EQUAL then (if wide then Opcode.F64_EQ else Opcode.F32_EQ fi, false) | |
| 194 | when Comparison.NOT_EQUAL then (if wide then Opcode.F64_NE else Opcode.F32_NE fi, false) | |
| 195 | when Comparison.LESS then (if wide then Opcode.F64_LT else Opcode.F32_LT fi, false) | |
| 196 | when Comparison.LESS_OR_EQUAL then (if wide then Opcode.F64_LE else Opcode.F32_LE fi, false) | |
| 197 | when Comparison.GREATER then (if wide then Opcode.F64_GT else Opcode.F32_GT fi, false) | |
| 198 | when Comparison.GREATER_OR_EQUAL then (if wide then Opcode.F64_GE else Opcode.F32_GE fi, false) | |
| 199 | esac | |
| 200 | fi | |
| 201 | ||
| 202 | return | |
| 203 | case comparison | |
| 204 | when Comparison.EQUAL then (if wide then Opcode.F64_NE else Opcode.F32_NE fi, true) | |
| 205 | when Comparison.NOT_EQUAL then (if wide then Opcode.F64_NE else Opcode.F32_NE fi, false) | |
| 206 | when Comparison.LESS then (if wide then Opcode.F64_GE else Opcode.F32_GE fi, true) | |
| 207 | when Comparison.LESS_OR_EQUAL then (if wide then Opcode.F64_GT else Opcode.F32_GT fi, true) | |
| 208 | when Comparison.GREATER then (if wide then Opcode.F64_LE else Opcode.F32_LE fi, true) | |
| 209 | when Comparison.GREATER_OR_EQUAL then (if wide then Opcode.F64_LT else Opcode.F32_LT fi, true) | |
| 210 | esac | |
| 211 | si | |
| 212 | ||
| 213 | /// The comparison a conditional exit tests, as one of the six | |
| 214 | /// comparisons and whether it orders unsigned (or, between | |
| 215 | /// floating values, answers true for unordered operands). Z and | |
| 216 | /// NZ test a single operand and are encoded by the lowering. | |
| 217 | branch_comparison(condition: BRANCH) -> (comparison: Comparison, is_unsigned: bool)? static => | |
| 218 | case condition | |
| 219 | when BRANCH.EQ then (Comparison.EQUAL, false) | |
| 220 | when BRANCH.NE then (Comparison.NOT_EQUAL, true) | |
| 221 | when BRANCH.GE then (Comparison.GREATER_OR_EQUAL, false) | |
| 222 | when BRANCH.GEU then (Comparison.GREATER_OR_EQUAL, true) | |
| 223 | when BRANCH.GT then (Comparison.GREATER, false) | |
| 224 | when BRANCH.GTU then (Comparison.GREATER, true) | |
| 225 | when BRANCH.LE then (Comparison.LESS_OR_EQUAL, false) | |
| 226 | when BRANCH.LEU then (Comparison.LESS_OR_EQUAL, true) | |
| 227 | when BRANCH.LT then (Comparison.LESS, false) | |
| 228 | when BRANCH.LTU then (Comparison.LESS, true) | |
| 229 | else null | |
| 230 | esac | |
| 231 | ||
| 232 | /// Brings a value of a narrow kind, held in an i32, back into its | |
| 233 | /// range: sign-extended for a signed kind and masked for an | |
| 234 | /// unsigned one, which is what CIL's narrowing conversions and | |
| 235 | /// narrow stores do. | |
| 236 | normalise(scalar: Scalar) -> LIST[INSTRUCTION] static is | |
| 237 | let result = LIST[INSTRUCTION]() | |
| 238 | ||
| 239 | case scalar | |
| 240 | when Scalar.INT8 then | |
| 241 | result.add(SIMPLE_INSTRUCTION(Opcode.I32_EXTEND8_S)) | |
| 242 | when Scalar.INT16 then | |
| 243 | result.add(SIMPLE_INSTRUCTION(Opcode.I32_EXTEND16_S)) | |
| 244 | when Scalar.UINT8, Scalar.BOOL then | |
| 245 | result.add(I32_CONSTANT(0xFF)) | |
| 246 | result.add(SIMPLE_INSTRUCTION(Opcode.I32_AND)) | |
| 247 | when Scalar.UINT16, Scalar.CHAR then | |
| 248 | result.add(I32_CONSTANT(0xFFFF)) | |
| 249 | result.add(SIMPLE_INSTRUCTION(Opcode.I32_AND)) | |
| 250 | else | |
| 251 | result | |
| 252 | esac | |
| 253 | ||
| 254 | return result | |
| 255 | si | |
| 256 | ||
| 257 | /// The instruction for an intrinsic that reads a floating value's | |
| 258 | /// bits as the unsigned integer of the same width, or null where | |
| 259 | /// `operation` is not one. | |
| 260 | reinterpretation(operation: string?) -> Opcode? static => | |
| 261 | if operation =~ "double.bits" then | |
| 262 | Opcode.I64_REINTERPRET_F64 | |
| 263 | elif operation =~ "single.bits" then | |
| 264 | Opcode.I32_REINTERPRET_F32 | |
| 265 | else | |
| 266 | null | |
| 267 | fi | |
| 268 | ||
| 269 | /// The conversion of a value of kind `from` to kind `to`, saturating | |
| 270 | /// from floating to integer as .NET does. | |
| 271 | /// | |
| 272 | /// - from: the source kind, or null for an enum, held as a signed i32 | |
| 273 | conversion(from: Scalar?, to: Scalar) -> LIST[INSTRUCTION] static is | |
| 274 | let result = LIST[INSTRUCTION]() | |
| 275 | ||
| 276 | let source = if let f = from then WASMGC_TYPES.numeric(f) else NumericType.I32 fi | |
| 277 | let source_unsigned = if let f = from then WASMGC_TYPES.is_unsigned(f) else false fi | |
| 278 | let target = WASMGC_TYPES.numeric(to) | |
| 279 | // .NET converts a floating value to a type narrower than 32 bits | |
| 280 | // through int, saturating there and then truncating, whatever the | |
| 281 | // narrower type's sign. | |
| 282 | let target_unsigned = WASMGC_TYPES.is_unsigned(to) /\ !WASMGC_TYPES.is_narrow(to) | |
| 283 | ||
| 284 | let opcode = _change(source, source_unsigned, target, target_unsigned) | |
| 285 | ||
| 286 | if let op = opcode then | |
| 287 | result.add(SIMPLE_INSTRUCTION(op)) | |
| 288 | fi | |
| 289 | ||
| 290 | if WASMGC_TYPES.is_narrow(to) then | |
| 291 | result.add_range(normalise(to)) | |
| 292 | fi | |
| 293 | ||
| 294 | return result | |
| 295 | si | |
| 296 | ||
| 297 | // The one instruction that moves a value between wasm types, or | |
| 298 | // null where both kinds share one. | |
| 299 | _change(source: NumericType, source_unsigned: bool, target: NumericType, target_unsigned: bool) -> Opcode? static => | |
| 300 | case target | |
| 301 | when NumericType.I32 then | |
| 302 | case source | |
| 303 | when NumericType.I64 then Opcode.I32_WRAP_I64 | |
| 304 | when NumericType.F32 then if target_unsigned then Opcode.I32_TRUNC_SAT_F32_U else Opcode.I32_TRUNC_SAT_F32_S fi | |
| 305 | when NumericType.F64 then if target_unsigned then Opcode.I32_TRUNC_SAT_F64_U else Opcode.I32_TRUNC_SAT_F64_S fi | |
| 306 | else null | |
| 307 | esac | |
| 308 | when NumericType.I64 then | |
| 309 | case source | |
| 310 | when NumericType.I32 then if source_unsigned then Opcode.I64_EXTEND_I32_U else Opcode.I64_EXTEND_I32_S fi | |
| 311 | when NumericType.F32 then if target_unsigned then Opcode.I64_TRUNC_SAT_F32_U else Opcode.I64_TRUNC_SAT_F32_S fi | |
| 312 | when NumericType.F64 then if target_unsigned then Opcode.I64_TRUNC_SAT_F64_U else Opcode.I64_TRUNC_SAT_F64_S fi | |
| 313 | else null | |
| 314 | esac | |
| 315 | when NumericType.F32 then | |
| 316 | case source | |
| 317 | when NumericType.I32 then if source_unsigned then Opcode.F32_CONVERT_I32_U else Opcode.F32_CONVERT_I32_S fi | |
| 318 | when NumericType.I64 then if source_unsigned then Opcode.F32_CONVERT_I64_U else Opcode.F32_CONVERT_I64_S fi | |
| 319 | when NumericType.F64 then Opcode.F32_DEMOTE_F64 | |
| 320 | else null | |
| 321 | esac | |
| 322 | when NumericType.F64 then | |
| 323 | case source | |
| 324 | when NumericType.I32 then if source_unsigned then Opcode.F64_CONVERT_I32_U else Opcode.F64_CONVERT_I32_S fi | |
| 325 | when NumericType.I64 then if source_unsigned then Opcode.F64_CONVERT_I64_U else Opcode.F64_CONVERT_I64_S fi | |
| 326 | when NumericType.F32 then Opcode.F64_PROMOTE_F32 | |
| 327 | else null | |
| 328 | esac | |
| 329 | else null | |
| 330 | esac | |
| 331 | si | |
| 332 | si |