|
| 1 | +#if !NO_SWIFTPM |
| 2 | +import cllvm |
| 3 | +#endif |
| 4 | + |
| 5 | +public struct Constant: IRValue { |
| 6 | + internal enum Representation { |
| 7 | + case unsigned |
| 8 | + case signed |
| 9 | + case floating |
| 10 | + } |
| 11 | + |
| 12 | + internal let llvm: LLVMValueRef |
| 13 | + internal let repr: Representation |
| 14 | + |
| 15 | + /// Retrieves the underlying LLVM constant object. |
| 16 | + public func asLLVM() -> LLVMValueRef { |
| 17 | + return llvm |
| 18 | + } |
| 19 | + |
| 20 | + internal init(llvm: LLVMValueRef!, representation: Representation) { |
| 21 | + self.llvm = llvm |
| 22 | + self.repr = representation |
| 23 | + } |
| 24 | + |
| 25 | + public static func +(lhs: Constant, rhs: Constant) -> Constant { |
| 26 | + precondition(lhs.repr == rhs.repr, "Mixed-representation constant operations are disallowed") |
| 27 | + |
| 28 | + switch lhs.repr { |
| 29 | + case .signed: fallthrough |
| 30 | + case .unsigned: |
| 31 | + return Constant(llvm: LLVMConstAdd(lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 32 | + case .floating: |
| 33 | + return Constant(llvm: LLVMConstFAdd(lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public static func -(lhs: Constant, rhs: Constant) -> Constant { |
| 38 | + precondition(lhs.repr == rhs.repr, "Mixed-representation constant operations are disallowed") |
| 39 | + |
| 40 | + switch lhs.repr { |
| 41 | + case .signed: fallthrough |
| 42 | + case .unsigned: |
| 43 | + return Constant(llvm: LLVMConstSub(lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 44 | + case .floating: |
| 45 | + return Constant(llvm: LLVMConstFSub(lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public static func *(lhs: Constant, rhs: Constant) -> Constant { |
| 50 | + precondition(lhs.repr == rhs.repr, "Mixed-representation constant operations are disallowed") |
| 51 | + |
| 52 | + switch lhs.repr { |
| 53 | + case .signed: fallthrough |
| 54 | + case .unsigned: |
| 55 | + return Constant(llvm: LLVMConstMul(lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 56 | + case .floating: |
| 57 | + return Constant(llvm: LLVMConstFMul(lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public static func /(lhs: Constant, rhs: Constant) -> Constant { |
| 62 | + precondition(lhs.repr == rhs.repr, "Mixed-representation constant operations are disallowed") |
| 63 | + |
| 64 | + switch lhs.repr { |
| 65 | + case .signed: |
| 66 | + return Constant(llvm: LLVMConstSDiv(lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 67 | + case .unsigned: |
| 68 | + return Constant(llvm: LLVMConstUDiv(lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 69 | + case .floating: |
| 70 | + return Constant(llvm: LLVMConstFDiv(lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public static func ==(lhs: Constant, rhs: Constant) -> Constant { |
| 75 | + precondition(lhs.repr == rhs.repr, "Mixed-representation constant operations are disallowed") |
| 76 | + |
| 77 | + return Constant(llvm: LLVMConstICmp(IntPredicate.eq.llvm, lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 78 | + } |
| 79 | + |
| 80 | + public static func <(lhs: Constant, rhs: Constant) -> Constant { |
| 81 | + precondition(lhs.repr == rhs.repr, "Mixed-representation constant operations are disallowed") |
| 82 | + |
| 83 | + switch lhs.repr { |
| 84 | + case .signed: |
| 85 | + return Constant(llvm: LLVMConstICmp(IntPredicate.slt.llvm, lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 86 | + case .unsigned: |
| 87 | + return Constant(llvm: LLVMConstICmp(IntPredicate.ult.llvm, lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 88 | + case .floating: |
| 89 | + return Constant(llvm: LLVMConstFCmp(RealPredicate.olt.llvm, lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public static func >(lhs: Constant, rhs: Constant) -> Constant { |
| 94 | + precondition(lhs.repr == rhs.repr, "Mixed-representation constant operations are disallowed") |
| 95 | + |
| 96 | + switch lhs.repr { |
| 97 | + case .signed: |
| 98 | + return Constant(llvm: LLVMConstICmp(IntPredicate.sgt.llvm, lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 99 | + case .unsigned: |
| 100 | + return Constant(llvm: LLVMConstICmp(IntPredicate.ugt.llvm, lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 101 | + case .floating: |
| 102 | + return Constant(llvm: LLVMConstFCmp(RealPredicate.ogt.llvm, lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public static func <=(lhs: Constant, rhs: Constant) -> Constant { |
| 107 | + precondition(lhs.repr == rhs.repr, "Mixed-representation constant operations are disallowed") |
| 108 | + |
| 109 | + switch lhs.repr { |
| 110 | + case .signed: |
| 111 | + return Constant(llvm: LLVMConstICmp(IntPredicate.sle.llvm, lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 112 | + case .unsigned: |
| 113 | + return Constant(llvm: LLVMConstICmp(IntPredicate.ule.llvm, lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 114 | + case .floating: |
| 115 | + return Constant(llvm: LLVMConstFCmp(RealPredicate.ole.llvm, lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public static func >=(lhs: Constant, rhs: Constant) -> Constant { |
| 120 | + precondition(lhs.repr == rhs.repr, "Mixed-representation constant operations are disallowed") |
| 121 | + |
| 122 | + switch lhs.repr { |
| 123 | + case .signed: |
| 124 | + return Constant(llvm: LLVMConstICmp(IntPredicate.sge.llvm, lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 125 | + case .unsigned: |
| 126 | + return Constant(llvm: LLVMConstICmp(IntPredicate.uge.llvm, lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 127 | + case .floating: |
| 128 | + return Constant(llvm: LLVMConstFCmp(RealPredicate.oge.llvm, lhs.llvm, rhs.llvm), representation: lhs.repr) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments