Appearance
| 1 | namespace IR.Values is | |
| 2 | use Semantic.Types.Type | |
| 3 | ||
| 4 | // `a =~ b` where both operands are a sequence - an array, or a | |
| 5 | // list - compared by count and then element by element. | |
| 6 | // | |
| 7 | // The element comparison arrives already built against a load of | |
| 8 | // the index local this node declares, so what the element type's | |
| 9 | // equality is has been decided by the caller in the same way as for | |
| 10 | // any other operand; this node only supplies the loop around it. | |
| 11 | // Both counts are read twice, so they arrive as repeatable loads. | |
| 12 | class SEQUENCE_EQUALITY: Composite is | |
| 13 | left_count: Value | |
| 14 | right_count: Value | |
| 15 | index: IR.LOCAL | |
| 16 | element_equals: Value | |
| 17 | _result_type: Type | |
| 18 | ||
| 19 | type: Type => _result_type | |
| 20 | is_lightweight_pure: bool => false | |
| 21 | ||
| 22 | init( | |
| 23 | left_count: Value, | |
| 24 | right_count: Value, | |
| 25 | index: IR.LOCAL, | |
| 26 | element_equals: Value, | |
| 27 | result_type: Type | |
| 28 | ) is | |
| 29 | super.init() | |
| 30 | ||
| 31 | self.left_count = left_count | |
| 32 | self.right_count = right_count | |
| 33 | self.index = index | |
| 34 | self.element_equals = element_equals | |
| 35 | self._result_type = result_type | |
| 36 | si | |
| 37 | ||
| 38 | // Unequal counts, or the first element pair to compare unequal, | |
| 39 | // leave the comparison false. | |
| 40 | expand() -> BLOCK is | |
| 41 | let expansion = IR.EXPANSION(type) | |
| 42 | let result = expansion.temp("sequence_equality", type) | |
| 43 | let index_type = index.type | |
| 44 | ||
| 45 | expansion.declare(index) | |
| 46 | ||
| 47 | let done = expansion.region() | |
| 48 | let unequal = expansion.region() | |
| 49 | ||
| 50 | expansion.exit_if(unequal, IR.BRANCH.NE, left_count, right_count) | |
| 51 | expansion.store(index, Literal.NUMBER(0, index_type)) | |
| 52 | ||
| 53 | let loop = expansion.loop() | |
| 54 | ||
| 55 | expansion.exit_if(loop, IR.BRANCH.GE, index.load(), left_count) | |
| 56 | expansion.exit_if(unequal, IR.BRANCH.Z, element_equals) | |
| 57 | expansion.store( | |
| 58 | index, | |
| 59 | ARITHMETIC(IR.ArithmeticOperation.ADD, index.load(), Literal.NUMBER(1, index_type), index_type).unmarked() | |
| 60 | ) | |
| 61 | expansion.close(loop) | |
| 62 | ||
| 63 | expansion.store(result, Literal.NUMBER(1, type)) | |
| 64 | expansion.exit(done) | |
| 65 | expansion.close(unequal) | |
| 66 | ||
| 67 | expansion.store(result, Literal.NUMBER(0, type)) | |
| 68 | expansion.close(done) | |
| 69 | ||
| 70 | expansion.add(result.load()) | |
| 71 | ||
| 72 | return expansion.result | |
| 73 | si | |
| 74 | ||
| 75 | to_string() -> string => | |
| 76 | "sequence-equality:[{type}]({left_count},{right_count},{index},{element_equals})" | |
| 77 | si | |
| 78 | ||
| 79 | // `a =~ b` on two references of a class declaring neither `=~` nor | |
| 80 | // `<>`: the same thing as `a == b`, reached from a synthesized | |
| 81 | // comparison where the operator could not have been written. | |
| 82 | class REFERENCE_EQUALITY: Value is | |
| 83 | left: Value | |
| 84 | right: Value | |
| 85 | _result_type: Type | |
| 86 | ||
| 87 | type: Type => _result_type | |
| 88 | is_lightweight_pure: bool => false | |
| 89 | ||
| 90 | init(left: Value, right: Value, result_type: Type) is | |
| 91 | super.init() | |
| 92 | ||
| 93 | self.left = left | |
| 94 | self.right = right | |
| 95 | self._result_type = result_type | |
| 96 | si | |
| 97 | ||
| 98 | accept(visitor: Visitor) is | |
| 99 | visitor.visit(self) | |
| 100 | si | |
| 101 | ||
| 102 | to_string() -> string => "reference-equality:[{type}]({left},{right})" | |
| 103 | si | |
| 104 | si |