3 // LLVMAttribute is a bitfield
4 %feature
("bitfield") LLVMAttribute
;
6 typedef unsigned char uint8_t
;
8 // Rename
/wrap functions that take pointer arguments
10 %rename LLVMFunctionType
"%%LLVMFunctionType";
11 %rename LLVMGetParamTypes
"%%LLVMGetParamTypes";
12 %rename LLVMGetStructElementTypes
"%%LLVMGetStructElementTypes";
13 %rename LLVMConstArray
"%%LLVMConstArray";
14 %rename LLVMConstVector
"%%LLVMConstVector";
15 %rename LLVMConstGEP
"%%LLVMConstGEP";
16 %rename LLVMConstInBoundsGEP
"%%LLVMConstInBoundsGEP";
17 %rename LLVMConstExtractValue
"%%LLVMConstExtractValue";
18 %rename LLVMConstInsertValue
"%%LLVMConstInsertValue";
19 %rename LLVMGetParams
"%%LLVMGetParams";
20 %rename LLVMAddIncoming
"%%LLVMAddIncoming";
21 %rename LLVMBuildAggregateRet
"%%LLVMBuildAggregateRet";
22 %rename LLVMBuildInvoke
"%%LLVMBuildInvoke";
23 %rename LLVMBuildGEP
"%%LLVMBuildGEP";
24 %rename LLVMBuildInBoundsGEP
"%%LLVMBuildInBoundsGEP";
25 %rename LLVMBuildCall
"%%LLVMBuildCall";
27 // Rename
/wrap
"InContext" functions.
28 %ignore LLVMModuleCreateWithName
;
29 %rename LLVMModuleCreateWithNameInContext
"%%LLVMModuleCreateWithNameInContext";
31 %rename LLVMInt1TypeInContext
"%%LLVMInt1TypeInContext";
33 %rename LLVMInt8TypeInContext
"%%LLVMInt8TypeInContext";
34 %ignore LLVMInt16Type
;
35 %rename LLVMInt16TypeInContext
"%%LLVMInt16TypeInContext";
36 %ignore LLVMInt32Type
;
37 %rename LLVMInt32TypeInContext
"%%LLVMInt32TypeInContext";
38 %ignore LLVMInt64Type
;
39 %rename LLVMInt64TypeInContext
"%%LLVMInt64TypeInContext";
41 %rename LLVMIntTypeInContext
"%%LLVMIntTypeInContext";
42 %ignore LLVMFloatType
;
43 %rename LLVMFloatTypeInContext
"%%LLVMFloatTypeInContext";
45 %ignore LLVMDoubleType
;
46 %rename LLVMDoubleTypeInContext
"%%LLVMDoubleTypeInContext";
47 %ignore LLVMX86FP80Type
;
48 %rename LLVMX86FP80TypeInContext
"%%LLVMX86FP80TypeInContext";
49 %ignore LLVMFP128Type
;
50 %rename LLVMFP128TypeInContext
"%%LLVMFP128TypeInContext";
51 %ignore LLVMPPCFP128Type
;
53 %rename LLVMPPCFP128TypeInContext
"%%LLVMPPCFP128TypeInContext";
54 %ignore LLVMStructType
;
55 %rename LLVMStructTypeInContext
"%%LLVMStructTypeInContext";
58 %rename LLVMVoidTypeInContext
"%%LLVMVoidTypeInContext";
59 %ignore LLVMLabelType
;
60 %rename LLVMLabelTypeInContext
"%%LLVMLabelTypeInContext";
61 %ignore LLVMOpaqueType
;
62 %rename LLVMOpaqueTypeInContext
"%%LLVMOpaqueTypeInContext";
64 %ignore LLVMConstString
;
65 %rename LLVMConstStringInContext
"%%LLVMConstStringInContext";
66 %ignore LLVMConstStruct
;
67 %rename LLVMConstStructInContext
"%%LLVMConstStructInContext";
68 %ignore LLVMAppendBasicBlock
;
69 %rename LLVMAppendBasicBlockInContext
"%%LLVMAppendBasicBlockInContext";
70 %ignore LLVMInsertBasicBlock
;
71 %rename LLVMInsertBasicBlockInContext
"%%LLVMInsertBasicBlockInContext";
72 %ignore LLVMCreateBuilder
;
73 %rename LLVMCreateBuilderInContext
"%%LLVMCreateBuilderInContext";
76 // Discard broken functions
, in favor of those defined in llvm-extras.cpp
77 %ignore LLVMAddFunctionAttr
;
78 %ignore LLVMRemoveFunctionAttr
;
80 // LLVMConstInt is replaced in llvm-extras.i with a bignum-capable fn
81 %rename LLVMConstInt
"%%LLVMConstInt";
83 %insert
("swiglisp") %{
84 ;; A little hack make the Name argument for LLVMBuild
* be optional and
85 ;; default to the empty string.
86 (defmacro wrap-defcfun
((c-name lisp-name
&rest options) return-type &body args)
87 (if
(and
(eql
(mismatch
"LLVMBuild" c-name
) 9)
88 (equal
(last args
) '
((Name
:string
))))
89 (let
((real-lisp-name
(intern
(concatenate 'string
"%" (symbol-name lisp-name
))
90 (symbol-package lisp-name
)))
91 (mod-args
(map 'list #'car
(butlast args
))))
93 ( cffi
:defcfun
(,c-name
,real-lisp-name
,@options
) ,return-type
,@args
)
94 (defmacro
,lisp-name
(,@mod-args
&optional (name ""))
95 `
(,'
,real-lisp-name
,,@mod-args
,name
))))
96 `
( cffi
:defcfun
(,c-name
,lisp-name
,@options
) ,return-type
,@args
)))
98 %include
"llvm-c/Core.h"
101 %insert
("swiglisp") %{
102 ;;; The wrappers to expose the
"InContext" versions of the functions
103 ;;; in a more lispy way.
104 (declaim
(special
*llvm-context
*))
106 (defun LLVMModuleCreateWithName
(ModuleId
&optional (context *llvm-context*))
107 (%LLVMModuleCreateWithNameInContext ModuleId context
))
109 (defun LLVMInt1Type
(&optional (context *llvm-context*))
110 (%LLVMInt1TypeInContext context
))
112 (defun LLVMInt8Type
(&optional (context *llvm-context*))
113 (%LLVMInt8TypeInContext context
))
115 (defun LLVMInt16Type
(&optional (context *llvm-context*))
116 (%LLVMInt16TypeInContext context
))
118 (defun LLVMInt32Type
(&optional (context *llvm-context*))
119 (%LLVMInt32TypeInContext context
))
121 (defun LLVMInt64Type
(&optional (context *llvm-context*))
122 (%LLVMInt64TypeInContext context
))
124 (defun LLVMIntType
(NumBits
&optional (context *llvm-context*))
125 (%LLVMIntTypeInContext context NumBits
))
127 (defun LLVMFloatType
(&optional (context *llvm-context*))
128 (%LLVMFloatTypeInContext context
))
130 (defun LLVMDoubleType
(&optional (context *llvm-context*))
131 (%LLVMDoubleTypeInContext context
))
133 (defun LLVMX86FP80Type
(&optional (context *llvm-context*))
134 (%LLVMX86FP80TypeInContext context
))
136 (defun LLVMFP128Type
(&optional (context *llvm-context*))
137 (%LLVMFP128TypeInContext context
))
139 (defun LLVMPPCFP128Type
(&optional (context *llvm-context*))
140 (%LLVMPPCFP128TypeInContext context
))
142 ;; LLVMStructTypeInContext handled below.
144 (defun LLVMVoidType
(&optional (context *llvm-context*))
145 (%LLVMVoidTypeInContext context
))
147 (defun LLVMLabelType
(&optional (context *llvm-context*))
148 (%LLVMLabelTypeInContext context
))
150 (defun LLVMOpaqueType
(&optional (context *llvm-context*))
151 (%LLVMOpaqueTypeInContext context
))
153 ;; LLVMConstStringInContext handled below
154 ;; LLVMConstStructInContext handled below
156 (defun LLVMAppendBasicBlock
(Fn Name
&optional (context *llvm-context*))
157 (%LLVMAppendBasicBlockInContext context Fn Name
))
159 (defun LLVMInsertBasicBlock
(BB Name
&optional (context *llvm-context*))
160 (%LLVMInsertBasicBlockInContext context BB Name
))
162 (defun LLVMCreateBuilder
(&optional (context *llvm-context*))
163 (%LLVMCreateBuilderInContext context
))
166 ;; More complex wrappers for dealing with pointers.
168 (defmacro with-array-and-length
((array len list
) &body body)
169 (let
((list-v
(gensym
"list")))
170 `
(let
* ((,list-v
,list
)
171 (,len
(length
,list-v
)))
172 (cffi
:with-foreign-object
(,array
:pointer
,len
)
173 (loop for l in
,list-v
176 (setf
(cffi
:mem-aref
,array
:pointer i
) l
))
179 (defun LLVMFunctionType
(ret params is-var-arg
)
180 "Combines the C API's ParamTypes array and ParamCount arguments into
181 a single list PARAMS."
182 (with-array-and-length
(array len params
)
183 (%LLVMFunctionType ret array len is-var-arg
)))
185 (defun LLVMGetParamTypes
(function-type
)
186 "Returns a list of param types rather than filling out a Dest pointer argument"
187 (let
((len
(LLVMCountParamTypes function-type
)))
188 (cffi
:with-foreign-object
(array
:pointer len
)
189 (%LLVMGetParamTypes function-type array
)
190 (loop for i from
0 below len
192 (cffi
:mem-aref array
:pointer i
)))))
194 (defun LLVMStructType
(element-types is-packed
&optional (context *llvm-context*))
195 (with-array-and-length
(array len element-types
)
196 (%LLVMStructTypeInContext context array len is-packed
)))
198 (defun LLVMGetStructElementTypes
(struct-type
)
199 "Returns a list of param types rather than filling out a Dest pointer argument"
200 (let
((len
(LLVMCountStructElementTypes struct-type
)))
201 (cffi
:with-foreign-object
(array
:pointer len
)
202 (%LLVMGetStructElementTypes struct-type array
)
203 (loop for i from
0 below len
205 (cffi
:mem-aref array
:pointer i
)))))
207 (defun LLVMConstString
(str dont-null-terminate
&optional (context *llvm-context*))
208 (cffi
:with-foreign-string
((foreign-string num-bytes
) str
)
209 (%LLVMConstStringInContext context foreign-string num-bytes dont-null-terminate
)))
211 (defun LLVMConstStruct
(vals packed-p
&optional (context *llvm-context*))
212 (with-array-and-length
(array len vals
)
213 (%LLVMConstStructInContext context array len packed-p
)))
215 (defun LLVMConstArray
(type vals
)
216 (with-array-and-length
(array len vals
)
217 (%LLVMConstArray type array len
)))
219 (defun LLVMConstVector
(vals
)
220 (with-array-and-length
(array len vals
)
221 (%LLVMConstVector array len
)))
223 (defun LLVMConstGEP
(ptr indices
)
224 (with-array-and-length
(array len indices
)
225 (%LLVMConstGEP ptr array len
)))
227 (defun LLVMConstInBoundsGEP
(ptr indices
)
228 (with-array-and-length
(array len indices
)
229 (%LLVMConstInBoundsGEP ptr array len
)))
231 (defun LLVMConstExtractValue
(AggConstant indices
)
232 (with-array-and-length
(array len indices
)
233 (%LLVMConstExtractValue AggConstant array len
)))
235 (defun LLVMConstInsertValue
(AggConstant ValConstant indices
)
236 (with-array-and-length
(array len indices
)
237 (%LLVMConstInsertValue AggConstant ValConstant array len
)))
239 (defun LLVMGetParams
(fn
)
240 "Returns a list of params rather than filling out a Dest pointer argument."
241 (let
((len
(LLVMCountParams fn
)))
242 (cffi
:with-foreign-object
(array
:pointer len
)
243 (%LLVMGetParams fn array
)
244 (loop for i from
0 below len
246 (cffi
:mem-aref array
:pointer i
)))))
248 (defun LLVMAddIncoming
(phi-node incoming-val incoming-block
)
249 "Unlike the C API (but like the C++ API...), takes only a single
250 incoming val and incoming block. Call multiple times if you want to
251 add multiple incoming values."
252 (cffi
:with-foreign-objects
((incoming-vals
:pointer
)
253 (incoming-blocks
:pointer
))
254 (setf
(cffi
:mem-aref incoming-vals
:pointer
0) incoming-val
)
255 (setf
(cffi
:mem-aref incoming-blocks
:pointer
0) incoming-block
)
256 (%LLVMAddIncoming phi-node incoming-vals incoming-blocks
1)))
258 (defun LLVMBuildAggregateRet
(builder vals
)
259 (with-array-and-length
(array len vals
)
260 (%LLVMBuildAggregateRet builder array len
)))
262 (defun LLVMBuildInvoke
(builder fn args then catch
&optional (name ""))
263 (with-array-and-length
(array len args
)
264 (%LLVMBuildInvoke builder fn array len then catch name
)))
266 (defun LLVMBuildGEP
(builder ptr indices
&optional (name ""))
267 (with-array-and-length
(array len indices
)
268 (%LLVMBuildGEP builder ptr array len name
)))
270 (defun LLVMBuildInBoundsGEP
(builder ptr indices
&optional (name ""))
271 (with-array-and-length
(array len indices
)
272 (%LLVMBuildInBoundsGEP builder ptr array len name
)))
274 (defun LLVMBuildCall
(builder fn args
&optional (name ""))
275 "Combines the C API's Args array and NumArgs arguments into a single
277 (with-array-and-length
(array len args
)
278 (%LLVMBuildCall builder fn array len name
)))
281 ;; LLVMCreateMemoryBufferWithContentsOfFile has OutMemBuf
, OutMessage
282 ;; LLVMCreateMemoryBufferWithSTDIN has OutMemBuf
, OutMessage