Make the "name" argument of LLVMBuild* an optional argument,
[cl-llvm.git] / src / core.i
blob169af63309da52711b32c8d643d1fe7f3e30376f
1 %include "typemaps.i"
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";
30 %ignore LLVMInt1Type;
31 %rename LLVMInt1TypeInContext "%%LLVMInt1TypeInContext";
32 %ignore LLVMInt8Type;
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";
40 %ignore LLVMIntType;
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";
56 %ignore LLVMVoidType;
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 %insert("swiglisp") %{
81 ;; A little hack make the Name argument for LLVMBuild* be optional and
82 ;; default to the empty string.
83 (defmacro wrap-defcfun ((c-name lisp-name &rest options) return-type &body args)
84 (if (and (eql (mismatch "LLVMBuild" c-name) 9)
85 (equal (last args) '((Name :string))))
86 (let ((real-lisp-name (intern (concatenate 'string "%" (symbol-name lisp-name))
87 (symbol-package lisp-name)))
88 (mod-args (map 'list #'car (butlast args))))
89 `(progn
90 ( cffi:defcfun (,c-name ,real-lisp-name ,@options) ,return-type ,@args)
91 (defmacro ,lisp-name (,@mod-args &optional (name ""))
92 `(,',real-lisp-name ,,@mod-args ,name))))
93 `( cffi:defcfun (,c-name ,lisp-name ,@options) ,return-type ,@args)))
95 %include "llvm-c/Core.h"
98 %insert("swiglisp") %{
99 ;;; The wrappers to expose the "InContext" versions of the functions
100 ;;; in a more lispy way.
101 (declaim (special *llvm-context*))
103 (defun LLVMModuleCreateWithName (ModuleId &optional (context *llvm-context*))
104 (%LLVMModuleCreateWithNameInContext ModuleId context))
106 (defun LLVMInt1Type (&optional (context *llvm-context*))
107 (%LLVMInt1TypeInContext context))
109 (defun LLVMInt8Type (&optional (context *llvm-context*))
110 (%LLVMInt8TypeInContext context))
112 (defun LLVMInt16Type (&optional (context *llvm-context*))
113 (%LLVMInt16TypeInContext context))
115 (defun LLVMInt32Type (&optional (context *llvm-context*))
116 (%LLVMInt32TypeInContext context))
118 (defun LLVMInt64Type (&optional (context *llvm-context*))
119 (%LLVMInt64TypeInContext context))
121 (defun LLVMIntType (NumBits &optional (context *llvm-context*))
122 (%LLVMIntTypeInContext context NumBits))
124 (defun LLVMFloatType (&optional (context *llvm-context*))
125 (%LLVMFloatTypeInContext context))
127 (defun LLVMDoubleType (&optional (context *llvm-context*))
128 (%LLVMDoubleTypeInContext context))
130 (defun LLVMX86FP80Type (&optional (context *llvm-context*))
131 (%LLVMX86FP80TypeInContext context))
133 (defun LLVMFP128Type (&optional (context *llvm-context*))
134 (%LLVMFP128TypeInContext context))
136 (defun LLVMPPCFP128Type (&optional (context *llvm-context*))
137 (%LLVMPPCFP128TypeInContext context))
139 ;; LLVMStructTypeInContext handled below.
141 (defun LLVMVoidType (&optional (context *llvm-context*))
142 (%LLVMVoidTypeInContext context))
144 (defun LLVMLabelType (&optional (context *llvm-context*))
145 (%LLVMLabelTypeInContext context))
147 (defun LLVMOpaqueType (&optional (context *llvm-context*))
148 (%LLVMOpaqueTypeInContext context))
150 ;; LLVMConstStringInContext handled below
151 ;; LLVMConstStructInContext handled below
153 (defun LLVMAppendBasicBlock (Fn Name &optional (context *llvm-context*))
154 (%LLVMAppendBasicBlockInContext context Fn Name))
156 (defun LLVMInsertBasicBlock (BB Name &optional (context *llvm-context*))
157 (%LLVMInsertBasicBlockInContext context BB Name))
159 (defun LLVMCreateBuilder (&optional (context *llvm-context*))
160 (%LLVMCreateBuilderInContext context))
163 ;; More complex wrappers for dealing with pointers.
165 (defmacro with-array-and-length ((array len list) &body body)
166 (let ((list-v (gensym "list")))
167 `(let* ((,list-v ,list)
168 (,len (length ,list-v)))
169 (cffi:with-foreign-object (,array :pointer ,len)
170 (loop for l in ,list-v
171 for i from 0
173 (setf (cffi:mem-aref ,array :pointer i) l))
174 (progn ,@body)))))
176 (defun LLVMFunctionType (ret params is-var-arg)
177 "Combines the C API's ParamTypes array and ParamCount arguments into
178 a single list PARAMS."
179 (with-array-and-length (array len params)
180 (%LLVMFunctionType ret array len is-var-arg)))
182 (defun LLVMGetParamTypes (function-type)
183 "Returns a list of param types rather than filling out a Dest pointer argument"
184 (let ((len (LLVMCountParamTypes function-type)))
185 (cffi:with-foreign-object (array :pointer len)
186 (%LLVMGetParamTypes function-type array)
187 (loop for i from 0 below len
188 collect
189 (cffi:mem-aref array :pointer i)))))
191 (defun LLVMStructType (element-types is-packed &optional (context *llvm-context*))
192 (with-array-and-length (array len element-types)
193 (%LLVMStructTypeInContext context array len is-packed)))
195 (defun LLVMGetStructElementTypes (struct-type)
196 "Returns a list of param types rather than filling out a Dest pointer argument"
197 (let ((len (LLVMCountStructElementTypes struct-type)))
198 (cffi:with-foreign-object (array :pointer len)
199 (%LLVMGetStructElementTypes struct-type array)
200 (loop for i from 0 below len
201 collect
202 (cffi:mem-aref array :pointer i)))))
204 (defun LLVMConstString (str dont-null-terminate &optional (context *llvm-context*))
205 (cffi:with-foreign-string ((foreign-string num-bytes) str)
206 (%LLVMConstStringInContext context foreign-string num-bytes dont-null-terminate)))
208 (defun LLVMConstStruct (vals packed-p &optional (context *llvm-context*))
209 (with-array-and-length (array len vals)
210 (%LLVMConstStructInContext context array len packed-p)))
212 (defun LLVMConstArray (type vals)
213 (with-array-and-length (array len vals)
214 (%LLVMConstArray type array len)))
216 (defun LLVMConstVector (vals)
217 (with-array-and-length (array len vals)
218 (%LLVMConstVector array len)))
220 (defun LLVMConstGEP (ptr indices)
221 (with-array-and-length (array len indices)
222 (%LLVMConstGEP ptr array len)))
224 (defun LLVMConstInBoundsGEP (ptr indices)
225 (with-array-and-length (array len indices)
226 (%LLVMConstInBoundsGEP ptr array len)))
228 (defun LLVMConstExtractValue (AggConstant indices)
229 (with-array-and-length (array len indices)
230 (%LLVMConstExtractValue AggConstant array len)))
232 (defun LLVMConstInsertValue (AggConstant ValConstant indices)
233 (with-array-and-length (array len indices)
234 (%LLVMConstInsertValue AggConstant ValConstant array len)))
236 (defun LLVMGetParams (fn)
237 "Returns a list of params rather than filling out a Dest pointer argument."
238 (let ((len (LLVMCountParams fn)))
239 (cffi:with-foreign-object (array :pointer len)
240 (%LLVMGetParams fn array)
241 (loop for i from 0 below len
242 collect
243 (cffi:mem-aref array :pointer i)))))
245 (defun LLVMAddIncoming (phi-node incoming-val incoming-block)
246 "Unlike the C API (but like the C++ API...), takes only a single
247 incoming val and incoming block. Call multiple times if you want to
248 add multiple incoming values."
249 (cffi:with-foreign-objects ((incoming-vals :pointer)
250 (incoming-blocks :pointer))
251 (setf (cffi:mem-aref incoming-vals :pointer 0) incoming-val)
252 (setf (cffi:mem-aref incoming-blocks :pointer 0) incoming-block)
253 (%LLVMAddIncoming phi-node incoming-vals incoming-blocks 1)))
255 (defun LLVMBuildAggregateRet (builder vals)
256 (with-array-and-length (array len vals)
257 (%LLVMBuildAggregateRet builder array len)))
259 (defun LLVMBuildInvoke (builder fn args then catch &optional (name ""))
260 (with-array-and-length (array len args)
261 (%LLVMBuildInvoke builder fn array len then catch name)))
263 (defun LLVMBuildGEP (builder ptr indices &optional (name ""))
264 (with-array-and-length (array len indices)
265 (%LLVMBuildGEP builder ptr array len name)))
267 (defun LLVMBuildInBoundsGEP (builder ptr indices &optional (name ""))
268 (with-array-and-length (array len indices)
269 (%LLVMBuildInBoundsGEP builder ptr array len name)))
271 (defun LLVMBuildCall (builder fn args &optional (name ""))
272 "Combines the C API's Args array and NumArgs arguments into a single
273 list ARGS."
274 (with-array-and-length (array len args)
275 (%LLVMBuildCall builder fn array len name)))
277 ;; TODO:
278 ;; LLVMCreateMemoryBufferWithContentsOfFile has OutMemBuf, OutMessage
279 ;; LLVMCreateMemoryBufferWithSTDIN has OutMemBuf, OutMessage