Appearance
| 1 | namespace IR is | |
| 2 | use System.Exception | |
| 3 | ||
| 4 | use Values.Call | |
| 5 | use Values.Value | |
| 6 | ||
| 7 | use Values.BLOCK | |
| 8 | ||
| 9 | class INNATE_OPERATION_GENERATOR( | |
| 10 | _logger: Logging.Logger, | |
| 11 | _boxer: VALUE_BOXER | |
| 12 | ) is | |
| 13 | _type_and_operation_handlers: Collections.MAP[string,(INNATE,BLOCK) -> void] | |
| 14 | _operation_handlers: Collections.MAP[string,(INNATE,BLOCK) -> void] | |
| 15 | _type_handlers: Collections.MAP[string,(INNATE,BLOCK) -> void] | |
| 16 | ||
| 17 | init(..) is | |
| 18 | _type_and_operation_handlers = Collections.MAP[string,(INNATE,BLOCK) -> void]() | |
| 19 | _operation_handlers = Collections.MAP[string,(INNATE,BLOCK) -> void]() | |
| 20 | _type_handlers = Collections.MAP[string,(INNATE,BLOCK) -> void]() | |
| 21 | ||
| 22 | _type_and_operation_handlers["compare.order"] = (value: INNATE, context: BLOCK) -> void is gen_compare_order(value, false, context); si | |
| 23 | _type_and_operation_handlers["compare.uorder"] = (value: INNATE, context: BLOCK) -> void is gen_compare_order(value, true, context); si | |
| 24 | _type_and_operation_handlers["compare.value"] = (value: INNATE, context: BLOCK) -> void is gen_compare_value(value, context); si | |
| 25 | _type_and_operation_handlers["compare.reference"] = (value: INNATE, context: BLOCK) -> void is gen_compare_reference(value, context); si | |
| 26 | ||
| 27 | ||
| 28 | _type_and_operation_handlers["bool.not"] = (value: INNATE, context: BLOCK) -> void is gen_bool_not(value, context); si | |
| 29 | _type_and_operation_handlers["reference.read"] = (value: INNATE, context: BLOCK) -> void is gen_reference_read(value, context); si | |
| 30 | _type_and_operation_handlers["reference.assign"] = (value: INNATE, context: BLOCK) -> void is gen_reference_assign(value, context); si | |
| 31 | _type_and_operation_handlers["type.size_of"] = (value: INNATE, context: BLOCK) -> void is gen_size_of(value, context); si | |
| 32 | ||
| 33 | _type_handlers["bool"] = (value: INNATE, context: BLOCK) -> void is gen_short_circuit_bool(value, context); si | |
| 34 | _type_handlers["arithmetic"] = (value: INNATE, context: BLOCK) -> void is gen_arithmetic(value, context); si | |
| 35 | _type_handlers["arithmetic_decimal"] = (value: INNATE, context: BLOCK) -> void is gen_arithmetic_decimal(value, context); si | |
| 36 | _type_handlers["compare_decimal"] = (value: INNATE, context: BLOCK) -> void is gen_compare_decimal(value, context); si | |
| 37 | _type_handlers["string"] = (value: INNATE, context: BLOCK) -> void is gen_string(value, context); si | |
| 38 | _type_handlers["range"] = (value: INNATE, context: BLOCK) -> void is gen_range(value, context); si | |
| 39 | ||
| 40 | // _type_handlers["object"] = (value: INNATE, context: CONTEXT) -> void is gen_object(value, context); si; | |
| 41 | si | |
| 42 | ||
| 43 | lower(value: INNATE, block: BLOCK) is | |
| 44 | if !IoC.CONTAINER.instance.build_flags.want_assembler then | |
| 45 | return | |
| 46 | fi | |
| 47 | ||
| 48 | let innate_name = value.innate_name | |
| 49 | ||
| 50 | if try_handle(innate_name, _type_and_operation_handlers, value, block) then | |
| 51 | return | |
| 52 | fi | |
| 53 | ||
| 54 | let operation_name = value.op_name | |
| 55 | ||
| 56 | if try_handle(operation_name, _operation_handlers, value, block) then | |
| 57 | return | |
| 58 | fi | |
| 59 | ||
| 60 | let type_name = value.type_name | |
| 61 | ||
| 62 | if try_handle(type_name, _type_handlers, value, block) then | |
| 63 | return | |
| 64 | fi | |
| 65 | ||
| 66 | throw System.Exception("unknown innate operation: {value.function.innate_name}") | |
| 67 | si | |
| 68 | ||
| 69 | try_handle( | |
| 70 | key: string, | |
| 71 | handlers: Collections.Map[string,(INNATE,BLOCK) -> void], | |
| 72 | value: INNATE, | |
| 73 | block: BLOCK) -> bool is | |
| 74 | ||
| 75 | if handlers.contains_key(key) then | |
| 76 | let handler = handlers[key] | |
| 77 | ||
| 78 | handler(value, block) | |
| 79 | return true | |
| 80 | fi | |
| 81 | return false | |
| 82 | si | |
| 83 | ||
| 84 | gen_arithmetic(value: INNATE, block: BLOCK) is | |
| 85 | block.add(Values.ARITHMETIC(_arithmetic_operation(value.op_name), value.arguments, value.type)) | |
| 86 | si | |
| 87 | ||
| 88 | // The size of the intrinsic's one type argument. | |
| 89 | gen_size_of(value: INNATE, block: BLOCK) is | |
| 90 | block.add(Values.SIZE_OF(value.function.generic_arguments[0], value.type)) | |
| 91 | si | |
| 92 | ||
| 93 | // The operation half of the intrinsic's declared name, which is | |
| 94 | // what the declaring assembly writes in its INTRINSIC_ATTRIBUTE. | |
| 95 | _arithmetic_operation(operation: string) -> ArithmeticOperation static => | |
| 96 | if operation =~ "add" then ArithmeticOperation.ADD | |
| 97 | elif operation =~ "sub" then ArithmeticOperation.SUBTRACT | |
| 98 | elif operation =~ "mul" then ArithmeticOperation.MULTIPLY | |
| 99 | elif operation =~ "div" then ArithmeticOperation.DIVIDE | |
| 100 | elif operation =~ "div_un" then ArithmeticOperation.DIVIDE_UNSIGNED | |
| 101 | elif operation =~ "rem" then ArithmeticOperation.REMAINDER | |
| 102 | elif operation =~ "rem_un" then ArithmeticOperation.REMAINDER_UNSIGNED | |
| 103 | elif operation =~ "neg" then ArithmeticOperation.NEGATE | |
| 104 | elif operation =~ "not" then ArithmeticOperation.COMPLEMENT | |
| 105 | elif operation =~ "and" then ArithmeticOperation.AND | |
| 106 | elif operation =~ "or" then ArithmeticOperation.OR | |
| 107 | elif operation =~ "xor" then ArithmeticOperation.XOR | |
| 108 | elif operation =~ "shl" then ArithmeticOperation.SHIFT_LEFT | |
| 109 | elif operation =~ "shr" then ArithmeticOperation.SHIFT_RIGHT | |
| 110 | elif operation =~ "ushr" then ArithmeticOperation.SHIFT_RIGHT_UNSIGNED | |
| 111 | else throw System.Exception("unexpected arithmetic operation: {operation}") | |
| 112 | fi | |
| 113 | ||
| 114 | gen_bool_not(value: INNATE, block: BLOCK) is | |
| 115 | assert value.arguments.count == 1 else "not bool missing arguments" | |
| 116 | ||
| 117 | block.add(Values.NOT.operator(value.arguments[0])) | |
| 118 | si | |
| 119 | ||
| 120 | // The left argument is the result unless it settles the | |
| 121 | // answer on its own: false for `and_then`, true for `or_else`. | |
| 122 | // Otherwise the result is the right argument. | |
| 123 | gen_short_circuit_bool(value: INNATE, block: BLOCK) is | |
| 124 | assert value.arguments.count == 2 else "short circuit bool missing arguments" | |
| 125 | ||
| 126 | let type = value.type | |
| 127 | let expansion = IR.EXPANSION(type) | |
| 128 | let result = expansion.temp("short_circuit", type) | |
| 129 | ||
| 130 | expansion.store(result, value.arguments[0]) | |
| 131 | ||
| 132 | let settled = expansion.region() | |
| 133 | ||
| 134 | expansion.exit_if( | |
| 135 | settled, | |
| 136 | if value.op_name =~ "and_then" then IR.BRANCH.Z else IR.BRANCH.NZ fi, | |
| 137 | result.load()) | |
| 138 | ||
| 139 | expansion.store(result, value.arguments[1]) | |
| 140 | expansion.close(settled) | |
| 141 | ||
| 142 | expansion.add(result.load()) | |
| 143 | ||
| 144 | for v in expansion.result.values do | |
| 145 | block.add(v) | |
| 146 | od | |
| 147 | si | |
| 148 | ||
| 149 | gen_compare_order(value: INNATE, is_unsigned: bool, block: BLOCK) is | |
| 150 | assert value.arguments.count == 2 else "compare order missing arguments" | |
| 151 | ||
| 152 | let actual_operation = value.actual_operation | |
| 153 | ||
| 154 | // A direct `<>` — written as the operator or reached as an | |
| 155 | // ordinary member call, so carrying no rewritten operation — | |
| 156 | // wants the ordering value itself. The relational operators | |
| 157 | // arrive here only via the parser's rewrite onto `<>`, with | |
| 158 | // the surface operator in actual_operation. | |
| 159 | if !actual_operation? \/ actual_operation =~ "<>" then | |
| 160 | gen_order(value, is_unsigned, block) | |
| 161 | return | |
| 162 | fi | |
| 163 | ||
| 164 | let comparison = | |
| 165 | if actual_operation =~ ">" then | |
| 166 | Comparison.GREATER | |
| 167 | elif actual_operation =~ ">=" then | |
| 168 | Comparison.GREATER_OR_EQUAL | |
| 169 | elif actual_operation =~ "<" then | |
| 170 | Comparison.LESS | |
| 171 | elif actual_operation =~ "<=" then | |
| 172 | Comparison.LESS_OR_EQUAL | |
| 173 | else | |
| 174 | throw System.Exception("unexpected compare order operation: {actual_operation}") | |
| 175 | fi | |
| 176 | ||
| 177 | block.add(Values.COMPARE(comparison, value.arguments[0], value.arguments[1], is_unsigned, value.type)) | |
| 178 | si | |
| 179 | ||
| 180 | // `a <> b` as the operator itself: the ordering value, composed | |
| 181 | // from the two comparisons so it agrees with them at every | |
| 182 | // operand width and in both signednesses - `a - b` would wrap to | |
| 183 | // the wrong sign at the type's extremes. The operands are | |
| 184 | // spilled to temps so their side effects happen once. | |
| 185 | gen_order(value: INNATE, is_unsigned: bool, block: BLOCK) is | |
| 186 | let left = TEMP(block, "order", value.arguments[0]) | |
| 187 | let right = TEMP(block, "order", value.arguments[1]) | |
| 188 | ||
| 189 | let bool_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_bool_type() | |
| 190 | ||
| 191 | // greater - less: negative when a < b, positive when a > b, | |
| 192 | // zero when neither | |
| 193 | block.add( | |
| 194 | Values.ARITHMETIC( | |
| 195 | ArithmeticOperation.SUBTRACT, | |
| 196 | Values.COMPARE(Comparison.GREATER, left.load(), right.load(), is_unsigned, bool_type), | |
| 197 | Values.COMPARE(Comparison.LESS, left.load(), right.load(), is_unsigned, bool_type), | |
| 198 | value.type)) | |
| 199 | si | |
| 200 | ||
| 201 | gen_string(value: INNATE, block: BLOCK) is | |
| 202 | let innate_name = value.function.innate_name | |
| 203 | ||
| 204 | if innate_name =~ "string.equals" then | |
| 205 | let equality = | |
| 206 | Values.RUNTIME( | |
| 207 | IR.RuntimeOperation.STRINGS_EQUAL, | |
| 208 | value.arguments[0], | |
| 209 | value.arguments[1], | |
| 210 | IoC.CONTAINER.instance.innate_symbol_lookup.get_bool_type()) | |
| 211 | ||
| 212 | if value.actual_operation =~ "!~" then | |
| 213 | block.add(Values.NOT.operator(equality)) | |
| 214 | else | |
| 215 | block.add(equality) | |
| 216 | fi | |
| 217 | else | |
| 218 | throw System.Exception("unexpected string innate operation: {innate_name}") | |
| 219 | fi | |
| 220 | si | |
| 221 | ||
| 222 | gen_compare_reference(value: INNATE, block: BLOCK) is | |
| 223 | assert value.arguments.count == 2 else "compare reference missing arguments" | |
| 224 | ||
| 225 | let left mut = value.arguments[0] | |
| 226 | let right mut = value.arguments[1] | |
| 227 | ||
| 228 | // A pointer against null. A pointer is a value type, so the | |
| 229 | // mismatch below would box it, and the CLI has no boxed | |
| 230 | // form for a pointer: what came out was an object reference | |
| 231 | // that is never null, so a null pointer read as present. | |
| 232 | // A null pointer is a zero native integer, which is what | |
| 233 | // the null side pushes instead. | |
| 234 | if _is_pointer(left) \/ _is_pointer(right) then | |
| 235 | if _is_null(left) then | |
| 236 | left = _native_zero(right.type!) | |
| 237 | fi | |
| 238 | ||
| 239 | if _is_null(right) then | |
| 240 | right = _native_zero(left.type!) | |
| 241 | fi | |
| 242 | elif left.is_value_type != right.is_value_type then | |
| 243 | left = _boxer.box_if_value(left) | |
| 244 | right = _boxer.box_if_value(right) | |
| 245 | fi | |
| 246 | ||
| 247 | block.add( | |
| 248 | Values.COMPARE( | |
| 249 | if value.actual_operation =~ "!=" then Comparison.NOT_EQUAL else Comparison.EQUAL fi, | |
| 250 | left, | |
| 251 | right, | |
| 252 | false, | |
| 253 | value.type)) | |
| 254 | si | |
| 255 | ||
| 256 | gen_compare_value(value: INNATE, block: BLOCK) is | |
| 257 | assert value.arguments.count == 2 else "compare value missing arguments" | |
| 258 | ||
| 259 | // `!=` is the negation of `==`; `!~` is the negation of `=~`. | |
| 260 | // An enum's `=~` reaches this innate (see ENUM_EQUALITY_OPERATOR), | |
| 261 | // so both spellings of inequality are NOT_EQUAL here. | |
| 262 | let comparison = | |
| 263 | if value.actual_operation =~ "!=" \/ value.actual_operation =~ "!~" then | |
| 264 | Comparison.NOT_EQUAL | |
| 265 | else | |
| 266 | Comparison.EQUAL | |
| 267 | fi | |
| 268 | ||
| 269 | block.add(Values.COMPARE(comparison, value.arguments[0], value.arguments[1], false, value.type)) | |
| 270 | si | |
| 271 | ||
| 272 | _is_pointer(value: Value) -> bool static => | |
| 273 | if let type = value.type then type.is_pointer else false fi | |
| 274 | ||
| 275 | _is_null(value: Value) -> bool static => | |
| 276 | if let type = value.type then type.is_null else false fi | |
| 277 | ||
| 278 | // The zero of a pointer type, typed as the pointer it is being | |
| 279 | // compared against so the block it goes into agrees about what | |
| 280 | // is on the stack. | |
| 281 | _native_zero(pointer_type: Semantic.Types.Type) -> Value static is | |
| 282 | Values.Literal.NUMBER(Values.Literal.CONSTANT.I4(0), pointer_type, Scalar.UINT_NATIVE) | |
| 283 | si | |
| 284 | ||
| 285 | gen_range(value: INNATE, block: BLOCK) is | |
| 286 | assert value.arguments.count == 2 else "create range incorrect arguments" | |
| 287 | ||
| 288 | if value.op_name !~ "inclusive" /\ value.op_name !~ "exclusive" then | |
| 289 | throw Exception("unexpected range innate operation: {value.innate_name}") | |
| 290 | fi | |
| 291 | ||
| 292 | block.add(Values.RUNTIME(IR.RuntimeOperation.NEW_RANGE, value.arguments[0], value.arguments[1], value.type)) | |
| 293 | ||
| 294 | return | |
| 295 | si | |
| 296 | ||
| 297 | gen_reference_read(value: INNATE, context: BLOCK) is | |
| 298 | // TODO | |
| 299 | si | |
| 300 | ||
| 301 | gen_reference_assign(value: INNATE, context: BLOCK) is | |
| 302 | // TODO | |
| 303 | si | |
| 304 | ||
| 305 | gen_arithmetic_decimal(value: INNATE, block: BLOCK) is | |
| 306 | let op = value.op_name | |
| 307 | ||
| 308 | let operation = | |
| 309 | if op =~ "neg" then | |
| 310 | IR.ArithmeticOperation.NEGATE | |
| 311 | elif op =~ "add" then | |
| 312 | IR.ArithmeticOperation.ADD | |
| 313 | elif op =~ "sub" then | |
| 314 | IR.ArithmeticOperation.SUBTRACT | |
| 315 | elif op =~ "mul" then | |
| 316 | IR.ArithmeticOperation.MULTIPLY | |
| 317 | elif op =~ "div" then | |
| 318 | IR.ArithmeticOperation.DIVIDE | |
| 319 | else | |
| 320 | throw System.Exception("unexpected arithmetic_decimal operation: {op}") | |
| 321 | fi | |
| 322 | ||
| 323 | let decimal_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_decimal_type() | |
| 324 | ||
| 325 | block.add(Values.DECIMAL_ARITHMETIC(operation, value.arguments, decimal_type)) | |
| 326 | si | |
| 327 | ||
| 328 | gen_compare_decimal(value: INNATE, block: BLOCK) is | |
| 329 | let bool_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_bool_type() | |
| 330 | let left = value.arguments[0] | |
| 331 | let right = value.arguments[1] | |
| 332 | ||
| 333 | let op = value.op_name | |
| 334 | ||
| 335 | if op =~ "equality" then | |
| 336 | let equality = Values.DECIMAL_COMPARE(IR.Comparison.EQUAL, left, right, bool_type) | |
| 337 | ||
| 338 | if value.actual_operation =~ "!=" then | |
| 339 | block.add(Values.NOT.operator(equality)) | |
| 340 | else | |
| 341 | block.add(equality) | |
| 342 | fi | |
| 343 | ||
| 344 | return | |
| 345 | fi | |
| 346 | ||
| 347 | if op =~ "order" then | |
| 348 | let actual = value.actual_operation | |
| 349 | ||
| 350 | // `<>` itself wants the ordering value, composed from the | |
| 351 | // two comparisons as gen_order does for the other scalars | |
| 352 | if !actual? \/ actual =~ "<>" then | |
| 353 | let left_temp = TEMP(block, "order", left) | |
| 354 | let right_temp = TEMP(block, "order", right) | |
| 355 | ||
| 356 | block.add( | |
| 357 | Values.ARITHMETIC( | |
| 358 | ArithmeticOperation.SUBTRACT, | |
| 359 | Values.DECIMAL_COMPARE(IR.Comparison.GREATER, left_temp.load(), right_temp.load(), bool_type), | |
| 360 | Values.DECIMAL_COMPARE(IR.Comparison.LESS, left_temp.load(), right_temp.load(), bool_type), | |
| 361 | value.type)) | |
| 362 | ||
| 363 | return | |
| 364 | fi | |
| 365 | ||
| 366 | let comparison = | |
| 367 | if actual =~ ">" then | |
| 368 | IR.Comparison.GREATER | |
| 369 | elif actual =~ ">=" then | |
| 370 | IR.Comparison.GREATER_OR_EQUAL | |
| 371 | elif actual =~ "<" then | |
| 372 | IR.Comparison.LESS | |
| 373 | elif actual =~ "<=" then | |
| 374 | IR.Comparison.LESS_OR_EQUAL | |
| 375 | else | |
| 376 | throw System.Exception("unexpected compare_decimal.order operation: {actual}") | |
| 377 | fi | |
| 378 | ||
| 379 | block.add(Values.DECIMAL_COMPARE(comparison, left, right, bool_type)) | |
| 380 | ||
| 381 | return | |
| 382 | fi | |
| 383 | ||
| 384 | throw System.Exception("unexpected compare_decimal operation: {op}") | |
| 385 | si | |
| 386 | ||
| 387 | gen_argument(value: INNATE, index: int, block: BLOCK) is | |
| 388 | block.add(value.arguments[index]) | |
| 389 | si | |
| 390 | ||
| 391 | gen_arguments(value: INNATE, block: BLOCK) is | |
| 392 | for a in value.arguments do | |
| 393 | block.add(a) | |
| 394 | od | |
| 395 | si | |
| 396 | si | |
| 397 | si |