From ea3a1eae38871b40639eca64998101c0647e0781 Mon Sep 17 00:00:00 2001 From: Harlan Date: Sun, 29 Jan 2017 01:17:57 -0500 Subject: [PATCH 1/2] Fix copy-paste error causing PtrToInt to use IntToPtr --- Sources/LLVM/IRBuilder.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From ca1992e2505981515f82d2e985962a37004aafa3 Mon Sep 17 00:00:00 2001 From: Harlan Haskins Date: Sun, 29 Jan 2017 01:31:27 -0500 Subject: [PATCH 2/2] Added tests for cast operations --- Tests/LLVMTests/IRBuilderSpec.swift | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) 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)