|
| 1 | +import LLVM |
| 2 | +import XCTest |
| 3 | +import Foundation |
| 4 | + |
| 5 | +class ConstantSpec : XCTestCase { |
| 6 | + func testConstants() { |
| 7 | + |
| 8 | + XCTAssert(fileCheckOutput(of: .stderr, withPrefixes: ["SIGNEDCONST"]) { |
| 9 | + // SIGNEDCONST: ; ModuleID = '[[ModuleName:ConstantTest]]' |
| 10 | + // SIGNEDCONST-NEXT: source_filename = "[[ModuleName]]" |
| 11 | + let module = Module(name: "ConstantTest") |
| 12 | + let builder = IRBuilder(module: module) |
| 13 | + // SIGNEDCONST: define void @main() { |
| 14 | + let main = builder.addFunction("main", |
| 15 | + type: FunctionType(argTypes: [], |
| 16 | + returnType: VoidType())) |
| 17 | + let constant = IntType.int64.constant(42) |
| 18 | + |
| 19 | + // SIGNEDCONST-NEXT: entry: |
| 20 | + let entry = main.appendBasicBlock(named: "entry") |
| 21 | + builder.positionAtEnd(of: entry) |
| 22 | + |
| 23 | + // SIGNEDCONST-NOT: %{{[0-9]+}} = add i64 %%{{[0-9]+}}, %%{{[0-9]+}} |
| 24 | + let val1 = builder.buildAdd(constant + constant, constant * constant) |
| 25 | + // SIGNEDCONST-NOT: %{{[0-9]+}} = sub i64 %%{{[0-9]+}}, %%{{[0-9]+}} |
| 26 | + let val2 = builder.buildSub(constant - constant, constant / constant) |
| 27 | + // SIGNEDCONST-NOT: %{{[0-9]+}} = mul i64 %%{{[0-9]+}}, %%{{[0-9]+}} |
| 28 | + let val3 = builder.buildMul(val1, val2) |
| 29 | + |
| 30 | + // SIGNEDCONST-NEXT: ret i64 -1848 |
| 31 | + builder.buildRet(val3) |
| 32 | + // SIGNEDCONST-NEXT: } |
| 33 | + module.dump() |
| 34 | + }) |
| 35 | + |
| 36 | + XCTAssert(fileCheckOutput(of: .stderr, withPrefixes: ["UNSIGNEDCONST"]) { |
| 37 | + // UNSIGNEDCONST: ; ModuleID = '[[ModuleName:ConstantTest]]' |
| 38 | + // UNSIGNEDCONST-NEXT: source_filename = "[[ModuleName]]" |
| 39 | + let module = Module(name: "ConstantTest") |
| 40 | + let builder = IRBuilder(module: module) |
| 41 | + // UNSIGNEDCONST: define void @main() { |
| 42 | + let main = builder.addFunction("main", |
| 43 | + type: FunctionType(argTypes: [], |
| 44 | + returnType: VoidType())) |
| 45 | + let constant = IntType.int64.constant(UInt64(42)) |
| 46 | + |
| 47 | + // UNSIGNEDCONST-NEXT: entry: |
| 48 | + let entry = main.appendBasicBlock(named: "entry") |
| 49 | + builder.positionAtEnd(of: entry) |
| 50 | + |
| 51 | + // UNSIGNEDCONST-NOT: %{{[0-9]+}} = add i64 %%{{[0-9]+}}, %%{{[0-9]+}} |
| 52 | + let val1 = builder.buildAdd(constant + constant, constant * constant) |
| 53 | + // UNSIGNEDCONST-NOT: %{{[0-9]+}} = sub i64 %%{{[0-9]+}}, %%{{[0-9]+}} |
| 54 | + let val2 = builder.buildSub(constant - constant, constant / constant) |
| 55 | + // UNSIGNEDCONST-NOT: %{{[0-9]+}} = mul i64 %%{{[0-9]+}}, %%{{[0-9]+}} |
| 56 | + let val3 = builder.buildMul(val1, val2) |
| 57 | + |
| 58 | + // UNSIGNEDCONST-NEXT: ret i64 -1848 |
| 59 | + builder.buildRet(val3) |
| 60 | + // UNSIGNEDCONST-NEXT: } |
| 61 | + module.dump() |
| 62 | + }) |
| 63 | + |
| 64 | + XCTAssert(fileCheckOutput(of: .stderr, withPrefixes: ["FLOATINGCONST"]) { |
| 65 | + // FLOATINGCONST: ; ModuleID = '[[ModuleName:ConstantTest]]' |
| 66 | + // FLOATINGCONST-NEXT: source_filename = "[[ModuleName]]" |
| 67 | + let module = Module(name: "ConstantTest") |
| 68 | + let builder = IRBuilder(module: module) |
| 69 | + // FLOATINGCONST: define void @main() { |
| 70 | + let main = builder.addFunction("main", |
| 71 | + type: FunctionType(argTypes: [], |
| 72 | + returnType: VoidType())) |
| 73 | + let constant = FloatType.double.constant(42.0) |
| 74 | + |
| 75 | + // FLOATINGCONST-NEXT: entry: |
| 76 | + let entry = main.appendBasicBlock(named: "entry") |
| 77 | + builder.positionAtEnd(of: entry) |
| 78 | + |
| 79 | + // FLOATINGCONST-NOT: %{{[0-9]+}} = add double %%{{[0-9]+}}, %%{{[0-9]+}} |
| 80 | + let val1 = builder.buildAdd(constant + constant, constant * constant) |
| 81 | + // FLOATINGCONST-NOT: %{{[0-9]+}} = sub double %%{{[0-9]+}}, %%{{[0-9]+}} |
| 82 | + let val2 = builder.buildSub(constant - constant, constant / constant) |
| 83 | + // FLOATINGCONST-NOT: %{{[0-9]+}} = mul double %%{{[0-9]+}}, %%{{[0-9]+}} |
| 84 | + let val3 = builder.buildMul(val1, val2) |
| 85 | + |
| 86 | + // FLOATINGCONST-NEXT: ret double -1.848000e+03 |
| 87 | + builder.buildRet(val3) |
| 88 | + // FLOATINGCONST-NEXT: } |
| 89 | + module.dump() |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
0 commit comments