diff --git a/Sources/LLVM/IRBuilder.swift b/Sources/LLVM/IRBuilder.swift index ac02d1c4..e82165c8 100644 --- a/Sources/LLVM/IRBuilder.swift +++ b/Sources/LLVM/IRBuilder.swift @@ -1075,7 +1075,7 @@ public class IRBuilder { /// - returns: An integer value representing the value of the given pointer /// converted to the given integer type. public func buildPtrToInt(_ val: IRValue, type: IntType, name: String = "") -> IRValue { - return LLVMBuildIntToPtr(llvm, val.asLLVM(), type.asLLVM(), name) + return LLVMBuildPtrToInt(llvm, val.asLLVM(), type.asLLVM(), name) } /// Builds an integer-to-floating instruction to convert the given integer diff --git a/Tests/LLVMTests/IRBuilderSpec.swift b/Tests/LLVMTests/IRBuilderSpec.swift index 0a21933d..53d06871 100644 --- a/Tests/LLVMTests/IRBuilderSpec.swift +++ b/Tests/LLVMTests/IRBuilderSpec.swift @@ -252,6 +252,44 @@ class IRBuilderSpec : XCTestCase { // CONTROLFLOW-NEXT: } module.dump() }) + + XCTAssert(fileCheckOutput(of: .stderr, withPrefixes: ["CAST"]) { + // CAST: ; ModuleID = '[[ModuleName:IRBuilderTest]]' + // CAST-NEXT: source_filename = "[[ModuleName]]" + let module = Module(name: "IRBuilderTest") + let builder = IRBuilder(module: module) + + // CAST: define i32 @main() { + let main = builder.addFunction("main", + type: FunctionType(argTypes: [], + returnType: IntType.int32)) + + // CAST-NEXT: entry: + let entry = main.appendBasicBlock(named: "entry") + builder.positionAtEnd(of: entry) + + // CAST-NEXT: %0 = alloca i64 + let alloca = builder.buildAlloca(type: IntType.int64) + + // CAST-NEXT: %1 = ptrtoint i64* %0 to i64 + _ = builder.buildPtrToInt(alloca, type: IntType.int64) + + // CAST-NEXT: %2 = load i64, i64* %0 + let val = builder.buildLoad(alloca) + + // CAST-NEXT: %3 = inttoptr i64 %2 to i64* + _ = builder.buildIntToPtr(val, + type: PointerType(pointee: IntType.int64)) + + // CAST-NEXT: %4 = bitcast i64* %0 to i8* + _ = builder.buildBitCast(alloca, type: PointerType.toVoid) + + // CAST-NEXT: ret i32 0 + builder.buildRet(IntType.int32.constant(0)) + + // CAST-NEXT: } + module.dump() + }) } #if !os(macOS)