|
| 1 | +import CLLVM |
| 2 | + |
| 3 | +/// Functions in this group correspond to APIs on `ConstantExpressions` . |
| 4 | +public enum ConstantExpressions { |
| 5 | + public static func getConstOpcode(constantVal: ValueRef) -> Opcode { |
| 6 | + let opcode = LLVMGetConstOpcode(constantVal.valueRef) |
| 7 | + return Opcode(from: opcode)! |
| 8 | + } |
| 9 | + |
| 10 | + public static func alignOf(typeRef: TypeRef) -> Value { |
| 11 | + let valueRef = LLVMAlignOf(typeRef.typeRef)! |
| 12 | + return Value(llvm: valueRef) |
| 13 | + } |
| 14 | + |
| 15 | + public static func sizeOf(typeRef: TypeRef) -> Value { |
| 16 | + let valueRef = LLVMSizeOf(typeRef.typeRef)! |
| 17 | + return Value(llvm: valueRef) |
| 18 | + } |
| 19 | + |
| 20 | + public func constNSWNeg(_ constantVal: ValueRef) -> Value { |
| 21 | + let valueRef = LLVMConstNSWNeg(constantVal.valueRef)! |
| 22 | + return Value(llvm: valueRef) |
| 23 | + } |
| 24 | + |
| 25 | + public func constNUWNeg(_ constantVal: ValueRef) -> Value { |
| 26 | + let valueRef = LLVMConstNUWNeg(constantVal.valueRef)! |
| 27 | + return Value(llvm: valueRef) |
| 28 | + } |
| 29 | + |
| 30 | + public static func constNeg(constantVal: ValueRef) -> Value { |
| 31 | + let valueRef = LLVMConstNeg(constantVal.valueRef)! |
| 32 | + return Value(llvm: valueRef) |
| 33 | + } |
| 34 | + |
| 35 | + public func constNot(_ constantVal: LLVMValueRef) -> LLVMValueRef { |
| 36 | + LLVMConstNot(constantVal) |
| 37 | + } |
| 38 | + |
| 39 | + public func constAdd(_ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 40 | + let valueRef = LLVMConstAdd(lhsConstant.valueRef, rhsConstant.valueRef)! |
| 41 | + return Value(llvm: valueRef) |
| 42 | + } |
| 43 | + |
| 44 | + public func constNSWAdd(_ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 45 | + let valueRef = LLVMConstNSWAdd(lhsConstant.valueRef, rhsConstant.valueRef)! |
| 46 | + return Value(llvm: valueRef) |
| 47 | + } |
| 48 | + |
| 49 | + public func constNUWAdd(_ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 50 | + let valueRef = LLVMConstNUWAdd(lhsConstant.valueRef, rhsConstant.valueRef)! |
| 51 | + return Value(llvm: valueRef) |
| 52 | + } |
| 53 | + |
| 54 | + public func constSub(_ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 55 | + let valueRef = LLVMConstSub(lhsConstant.valueRef, rhsConstant.valueRef)! |
| 56 | + return Value(llvm: valueRef) |
| 57 | + } |
| 58 | + |
| 59 | + public func constNSWSub(_ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 60 | + let valueRef = LLVMConstNSWSub(lhsConstant.valueRef, rhsConstant.valueRef)! |
| 61 | + return Value(llvm: valueRef) |
| 62 | + } |
| 63 | + |
| 64 | + public func constNUWSub(_ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 65 | + let valueRef = LLVMConstNUWSub(lhsConstant.valueRef, rhsConstant.valueRef)! |
| 66 | + return Value(llvm: valueRef) |
| 67 | + } |
| 68 | + |
| 69 | + public func constMul(_ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 70 | + let valueRef = LLVMConstMul(lhsConstant.valueRef, rhsConstant.valueRef)! |
| 71 | + return Value(llvm: valueRef) |
| 72 | + } |
| 73 | + |
| 74 | + public func constNSWMul(_ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 75 | + let valueRef = LLVMConstNSWMul(lhsConstant.valueRef, rhsConstant.valueRef)! |
| 76 | + return Value(llvm: valueRef) |
| 77 | + } |
| 78 | + |
| 79 | + public func constNUWMul(_ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 80 | + let valueRef = LLVMConstNUWMul(lhsConstant.valueRef, rhsConstant.valueRef)! |
| 81 | + return Value(llvm: valueRef) |
| 82 | + } |
| 83 | + |
| 84 | + public func constAnd(_ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 85 | + let valueRef = LLVMConstAnd(lhsConstant.valueRef, rhsConstant.valueRef)! |
| 86 | + return Value(llvm: valueRef) |
| 87 | + } |
| 88 | + |
| 89 | + public func constOr(_ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 90 | + let valueRef = LLVMConstOr(lhsConstant.valueRef, rhsConstant.valueRef)! |
| 91 | + return Value(llvm: valueRef) |
| 92 | + } |
| 93 | + |
| 94 | + public func constXor(_ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 95 | + let valueRef = LLVMConstXor(lhsConstant.valueRef, rhsConstant.valueRef)! |
| 96 | + return Value(llvm: valueRef) |
| 97 | + } |
| 98 | + |
| 99 | + public func constICmp(_ predicate: IntPredicate, _ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 100 | + let valueRef = LLVMConstICmp(predicate.llvm, lhsConstant.valueRef, rhsConstant.valueRef)! |
| 101 | + return Value(llvm: valueRef) |
| 102 | + } |
| 103 | + |
| 104 | + public func constFCmp(_ predicate: RealPredicate, _ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 105 | + let valueRef = LLVMConstFCmp(predicate.llvm, lhsConstant.valueRef, rhsConstant.valueRef)! |
| 106 | + return Value(llvm: valueRef) |
| 107 | + } |
| 108 | + |
| 109 | + public func constShl(_ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 110 | + let valueRef = LLVMConstShl(lhsConstant.valueRef, rhsConstant.valueRef)! |
| 111 | + return Value(llvm: valueRef) |
| 112 | + } |
| 113 | + |
| 114 | + public func constLShr(_ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 115 | + let valueRef = LLVMConstLShr(lhsConstant.valueRef, rhsConstant.valueRef)! |
| 116 | + return Value(llvm: valueRef) |
| 117 | + } |
| 118 | + |
| 119 | + public func constAShr(_ lhsConstant: ValueRef, _ rhsConstant: ValueRef) -> Value { |
| 120 | + let valueRef = LLVMConstAShr(lhsConstant.valueRef, rhsConstant.valueRef)! |
| 121 | + return Value(llvm: valueRef) |
| 122 | + } |
| 123 | + |
| 124 | + func constGEP2(_ type: LLVMTypeRef, _ constantVal: LLVMValueRef, _ constantIndices: [LLVMValueRef], _ numIndices: UInt32) -> LLVMValueRef { |
| 125 | + let indices = UnsafeMutablePointer<LLVMValueRef?>.allocate(capacity: Int(numIndices)) |
| 126 | + defer { |
| 127 | + indices.deallocate() |
| 128 | + } |
| 129 | + |
| 130 | + for (index, value) in constantIndices.enumerated() { |
| 131 | + guard index < numIndices else { break } |
| 132 | + indices[index] = value |
| 133 | + } |
| 134 | + |
| 135 | + return LLVMConstGEP2(type, constantVal, indices, numIndices) |
| 136 | + } |
| 137 | + |
| 138 | + func constInBoundsGEP2(_ type: LLVMTypeRef, _ constantVal: LLVMValueRef, _ constantIndices: [LLVMValueRef], _ numIndices: UInt32) -> LLVMValueRef { |
| 139 | + let indices = UnsafeMutablePointer<LLVMValueRef?>.allocate(capacity: Int(numIndices)) |
| 140 | + defer { |
| 141 | + indices.deallocate() |
| 142 | + } |
| 143 | + |
| 144 | + for (index, value) in constantIndices.enumerated() { |
| 145 | + guard index < numIndices else { break } |
| 146 | + indices[index] = value |
| 147 | + } |
| 148 | + |
| 149 | + return LLVMConstInBoundsGEP2(type, constantVal, indices, numIndices) |
| 150 | + } |
| 151 | + |
| 152 | + func constTrunc(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 153 | + LLVMConstTrunc(constantVal, toType) |
| 154 | + } |
| 155 | + |
| 156 | + func constSExt(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 157 | + LLVMConstSExt(constantVal, toType) |
| 158 | + } |
| 159 | + |
| 160 | + func constZExt(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 161 | + LLVMConstZExt(constantVal, toType) |
| 162 | + } |
| 163 | + |
| 164 | + func constFPTrunc(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 165 | + LLVMConstFPTrunc(constantVal, toType) |
| 166 | + } |
| 167 | + |
| 168 | + func constFPExt(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 169 | + LLVMConstFPExt(constantVal, toType) |
| 170 | + } |
| 171 | + |
| 172 | + func constUIToFP(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 173 | + LLVMConstUIToFP(constantVal, toType) |
| 174 | + } |
| 175 | + |
| 176 | + func constSIToFP(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 177 | + LLVMConstSIToFP(constantVal, toType) |
| 178 | + } |
| 179 | + |
| 180 | + func constFPToUI(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 181 | + LLVMConstFPToUI(constantVal, toType) |
| 182 | + } |
| 183 | + |
| 184 | + func constFPToSI(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 185 | + LLVMConstFPToSI(constantVal, toType) |
| 186 | + } |
| 187 | + |
| 188 | + func constPtrToInt(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 189 | + LLVMConstPtrToInt(constantVal, toType) |
| 190 | + } |
| 191 | + |
| 192 | + func constIntToPtr(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 193 | + LLVMConstIntToPtr(constantVal, toType) |
| 194 | + } |
| 195 | + |
| 196 | + func constBitCast(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 197 | + LLVMConstBitCast(constantVal, toType) |
| 198 | + } |
| 199 | + |
| 200 | + func constAddrSpaceCast(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 201 | + LLVMConstAddrSpaceCast(constantVal, toType) |
| 202 | + } |
| 203 | + |
| 204 | + func constZExtOrBitCast(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 205 | + LLVMConstZExtOrBitCast(constantVal, toType) |
| 206 | + } |
| 207 | + |
| 208 | + func constSExtOrBitCast(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 209 | + LLVMConstSExtOrBitCast(constantVal, toType) |
| 210 | + } |
| 211 | + |
| 212 | + func constTruncOrBitCast(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 213 | + LLVMConstTruncOrBitCast(constantVal, toType) |
| 214 | + } |
| 215 | + |
| 216 | + func constPointerCast(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 217 | + LLVMConstPointerCast(constantVal, toType) |
| 218 | + } |
| 219 | + |
| 220 | + func constIntCast(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef, _ isSigned: Bool) -> LLVMValueRef { |
| 221 | + LLVMConstIntCast(constantVal, toType, LLVMBool(isSigned ? 1 : 0)) |
| 222 | + } |
| 223 | + |
| 224 | + func constFPCast(_ constantVal: LLVMValueRef, _ toType: LLVMTypeRef) -> LLVMValueRef { |
| 225 | + LLVMConstFPCast(constantVal, toType) |
| 226 | + } |
| 227 | + |
| 228 | + func constExtractElement(_ vectorConstant: LLVMValueRef, _ indexConstant: LLVMValueRef) -> LLVMValueRef { |
| 229 | + LLVMConstExtractElement(vectorConstant, indexConstant) |
| 230 | + } |
| 231 | + |
| 232 | + func constInsertElement(_ vectorConstant: LLVMValueRef, _ elementValueConstant: LLVMValueRef, _ indexConstant: LLVMValueRef) -> LLVMValueRef { |
| 233 | + LLVMConstInsertElement(vectorConstant, elementValueConstant, indexConstant) |
| 234 | + } |
| 235 | + |
| 236 | + func constShuffleVector(_ vectorAConstant: LLVMValueRef, _ vectorBConstant: LLVMValueRef, _ maskConstant: LLVMValueRef) -> LLVMValueRef { |
| 237 | + LLVMConstShuffleVector(vectorAConstant, vectorBConstant, maskConstant) |
| 238 | + } |
| 239 | + |
| 240 | + func blockAddress(_ function: LLVMValueRef, _ basicBlock: LLVMBasicBlockRef) -> LLVMValueRef { |
| 241 | + LLVMBlockAddress(function, basicBlock) |
| 242 | + } |
| 243 | + |
| 244 | + @available(*, deprecated, message: "Use LLVMGetInlineAsm instead") |
| 245 | + func constInlineAsm(type: LLVMTypeRef, asmString: String, constraints: String, hasSideEffects: Bool, isAlignStack: Bool) -> LLVMValueRef { |
| 246 | + asmString.withCString { asmStr in |
| 247 | + constraints.withCString { consStr in |
| 248 | + LLVMConstInlineAsm(type, asmStr, consStr, LLVMBool(hasSideEffects ? 1 : 0), LLVMBool(isAlignStack ? 1 : 0)) |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | +} |
0 commit comments