@@ -24,6 +24,123 @@ public enum JITError: Error, CustomStringConvertible {
2424 }
2525}
2626
27+ public final class JITValue {
28+ fileprivate let llvm : LLVMGenericValueRef
29+ fileprivate init ( llvm: LLVMGenericValueRef ) {
30+ self . llvm = llvm
31+ }
32+
33+ public convenience init ? ( int value: Int ) {
34+ guard
35+ let llvm = LLVMCreateGenericValueOfInt ( value. type. asLLVM ( ) ,
36+ UInt64 ( bitPattern: Int64 ( value) ) ,
37+ /*signed:*/ true . llvm)
38+ else {
39+ return nil
40+ }
41+ self . init ( llvm: llvm)
42+ }
43+
44+ public convenience init ? ( int value: UInt ) {
45+ guard
46+ let llvm = LLVMCreateGenericValueOfInt ( value. type. asLLVM ( ) ,
47+ UInt64 ( value) ,
48+ /*signed:*/ false . llvm)
49+ else {
50+ return nil
51+ }
52+ self . init ( llvm: llvm)
53+ }
54+
55+ public convenience init ? ( int value: Float ) {
56+ guard
57+ let llvm = LLVMCreateGenericValueOfFloat ( FloatType . float. asLLVM ( ) , Double ( value) )
58+ else {
59+ return nil
60+ }
61+ self . init ( llvm: llvm)
62+ }
63+
64+ public convenience init ? ( int value: Double ) {
65+ guard
66+ let llvm = LLVMCreateGenericValueOfFloat ( FloatType . double. asLLVM ( ) , value)
67+ else {
68+ return nil
69+ }
70+ self . init ( llvm: llvm)
71+ }
72+
73+
74+ public convenience init ? ( pointer value: UnsafeMutableRawPointer ? ) {
75+ guard
76+ let llvm = LLVMCreateGenericValueOfPointer ( value)
77+ else {
78+ return nil
79+ }
80+ self . init ( llvm: llvm)
81+ }
82+
83+ public convenience init ? ( int value: Constant < Signed > ) {
84+ let intValue = LLVMConstIntGetSExtValue ( value. asLLVM ( ) )
85+ guard
86+ let llvm = LLVMCreateGenericValueOfInt ( value. type. asLLVM ( ) ,
87+ UInt64 ( bitPattern: intValue) ,
88+ /*signed:*/ true . llvm)
89+ else {
90+ return nil
91+ }
92+ self . init ( llvm: llvm)
93+ }
94+
95+ public convenience init ? ( int value: Constant < Unsigned > ) {
96+ let intValue = LLVMConstIntGetZExtValue ( value. asLLVM ( ) )
97+ guard
98+ let llvm = LLVMCreateGenericValueOfInt ( value. type. asLLVM ( ) ,
99+ intValue, /*signed:*/ false . llvm)
100+ else {
101+ return nil
102+ }
103+ self . init ( llvm: llvm)
104+ }
105+
106+ public convenience init ? ( float value: Constant < Floating > ) {
107+ var infoLost : LLVMBool = 0
108+ let floatValue = LLVMConstRealGetDouble ( value. asLLVM ( ) , & infoLost)
109+ guard infoLost == 0 else {
110+ return nil
111+ }
112+ guard
113+ let llvm = LLVMCreateGenericValueOfFloat ( value. type. asLLVM ( ) , floatValue)
114+ else {
115+ return nil
116+ }
117+ self . init ( llvm: llvm)
118+ }
119+
120+ deinit {
121+ LLVMDisposeGenericValue ( self . llvm)
122+ }
123+
124+ public var bitwidth : UInt {
125+ let width = LLVMGenericValueIntWidth ( self . llvm)
126+ return UInt ( width)
127+ }
128+
129+ public var intValue : Int {
130+ let int = LLVMGenericValueToInt ( self . llvm, /*signed:*/ true . llvm)
131+ return Int ( bitPattern: UInt ( int) )
132+ }
133+
134+ public var unsignedIntValue : UInt {
135+ let int = LLVMGenericValueToInt ( self . llvm, /*signed:*/ false . llvm)
136+ return UInt ( int)
137+ }
138+
139+ public var pointerValue : UnsafeMutableRawPointer ? {
140+ return LLVMGenericValueToPointer ( self . llvm)
141+ }
142+ }
143+
27144/// A `JIT` is a Just-In-Time compiler that will compile and execute LLVM IR
28145/// that has been generated in a `Module`. It can execute arbitrary functions
29146/// and return the value the function generated, allowing you to write
@@ -68,11 +185,11 @@ public final class JIT {
68185 /// - function: The function you wish to execute
69186 /// - args: The arguments you wish to pass to the function
70187 /// - returns: The LLVM value that the function returned
71- public func runFunction( _ function: Function , args: [ IRValue ] ) -> IRValue {
72- var irArgs = args. map { $0. asLLVM ( ) as Optional }
188+ public func runFunction( _ function: Function , args: [ JITValue ] ) -> JITValue {
189+ var irArgs = args. map { $0. llvm as Optional }
73190 return irArgs. withUnsafeMutableBufferPointer { buf in
74- return LLVMRunFunction ( llvm, function. asLLVM ( ) ,
75- UInt32 ( buf. count) , buf. baseAddress)
191+ return JITValue ( llvm : LLVMRunFunction ( llvm, function. asLLVM ( ) ,
192+ UInt32 ( buf. count) , buf. baseAddress) )
76193 }
77194 }
78195
0 commit comments