Wrap more stuff, move some things around.
[cl-llvm.git] / src / core.i
blobfec951d42effdf2a610255759b22fe649ff349f8
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 %include "llvm-c/Core.h"
83 %insert("swiglisp") %{
84 ;;; The wrappers to expose the "InContext" versions of the functions
85 ;;; in a more lispy way.
86 (declaim (special *llvm-context*))
88 (defun LLVMModuleCreateWithName (ModuleId &optional (context *llvm-context*))
89 (%LLVMModuleCreateWithNameInContext ModuleId context))
91 (defun LLVMInt1Type (&optional (context *llvm-context*))
92 (%LLVMInt1TypeInContext context))
94 (defun LLVMInt8Type (&optional (context *llvm-context*))
95 (%LLVMInt8TypeInContext context))
97 (defun LLVMInt16Type (&optional (context *llvm-context*))
98 (%LLVMInt16TypeInContext context))
100 (defun LLVMInt32Type (&optional (context *llvm-context*))
101 (%LLVMInt32TypeInContext context))
103 (defun LLVMInt64Type (&optional (context *llvm-context*))
104 (%LLVMInt64TypeInContext context))
106 (defun LLVMIntType (NumBits &optional (context *llvm-context*))
107 (%LLVMIntTypeInContext context NumBits))
109 (defun LLVMFloatType (&optional (context *llvm-context*))
110 (%LLVMFloatTypeInContext context))
112 (defun LLVMDoubleType (&optional (context *llvm-context*))
113 (%LLVMDoubleTypeInContext context))
115 (defun LLVMX86FP80Type (&optional (context *llvm-context*))
116 (%LLVMX86FP80TypeInContext context))
118 (defun LLVMFP128Type (&optional (context *llvm-context*))
119 (%LLVMFP128TypeInContext context))
121 (defun LLVMPPCFP128Type (&optional (context *llvm-context*))
122 (%LLVMPPCFP128TypeInContext context))
124 ;; LLVMStructTypeInContext handled below.
126 (defun LLVMVoidType (&optional (context *llvm-context*))
127 (%LLVMVoidTypeInContext context))
129 (defun LLVMLabelType (&optional (context *llvm-context*))
130 (%LLVMLabelTypeInContext context))
132 (defun LLVMOpaqueType (&optional (context *llvm-context*))
133 (%LLVMOpaqueTypeInContext context))
135 ;; LLVMConstStringInContext handled below
136 ;; LLVMConstStructInContext handled below
138 (defun LLVMAppendBasicBlock (Fn Name &optional (context *llvm-context*))
139 (%LLVMAppendBasicBlockInContext context Fn Name))
141 (defun LLVMInsertBasicBlock (BB Name &optional (context *llvm-context*))
142 (%LLVMInsertBasicBlockInContext context BB Name))
144 (defun LLVMCreateBuilder (&optional (context *llvm-context*))
145 (%LLVMCreateBuilderInContext context))
148 ;; More complex wrappers for dealing with pointers.
150 (defmacro with-array-and-length ((array len list) &body body)
151 (let ((list-v (gensym "list")))
152 `(let* ((,list-v ,list)
153 (,len (length ,list-v)))
154 (cffi:with-foreign-object (,array :pointer ,len)
155 (loop for l in ,list-v
156 for i from 0
158 (setf (cffi:mem-aref ,array :pointer i) l))
159 (progn ,@body)))))
161 (defun LLVMFunctionType (ret params is-var-arg)
162 "Combines the C API's ParamTypes array and ParamCount arguments into
163 a single list PARAMS."
164 (with-array-and-length (array len params)
165 (%LLVMFunctionType ret array len is-var-arg)))
167 (defun LLVMGetParamTypes (function-type)
168 "Returns a list of param types rather than filling out a Dest pointer argument"
169 (let ((len (LLVMCountParamTypes function-type)))
170 (cffi:with-foreign-object (array :pointer len)
171 (%LLVMGetParamTypes function-type array)
172 (loop for i from 0 below len
173 collect
174 (cffi:mem-aref array :pointer i)))))
176 (defun LLVMStructType (element-types is-packed &optional (context *llvm-context*))
177 (with-array-and-length (array len element-types)
178 (%LLVMStructTypeInContext context array len is-packed)))
180 (defun LLVMGetStructElementTypes (struct-type)
181 "Returns a list of param types rather than filling out a Dest pointer argument"
182 (let ((len (LLVMCountStructElementTypes struct-type)))
183 (cffi:with-foreign-object (array :pointer len)
184 (%LLVMGetStructElementTypes struct-type array)
185 (loop for i from 0 below len
186 collect
187 (cffi:mem-aref array :pointer i)))))
189 (defun LLVMConstString (str dont-null-terminate &optional (context *llvm-context*))
190 (cffi:with-foreign-string ((foreign-string num-bytes) str)
191 (%LLVMConstStringInContext context foreign-string num-bytes dont-null-terminate)))
193 (defun LLVMConstStruct (vals packed-p &optional (context *llvm-context*))
194 (with-array-and-length (array len vals)
195 (%LLVMConstStructInContext context array len packed-p)))
197 (defun LLVMConstArray (type vals)
198 (with-array-and-length (array len vals)
199 (%LLVMConstArray type array len)))
201 (defun LLVMConstVector (vals)
202 (with-array-and-length (array len vals)
203 (%LLVMConstVector array len)))
205 (defun LLVMConstGEP (ptr indices)
206 (with-array-and-length (array len indices)
207 (%LLVMConstGEP ptr array len)))
209 (defun LLVMConstInBoundsGEP (ptr indices)
210 (with-array-and-length (array len indices)
211 (%LLVMConstInBoundsGEP ptr array len)))
213 (defun LLVMConstExtractValue (AggConstant indices)
214 (with-array-and-length (array len indices)
215 (%LLVMConstExtractValue AggConstant array len)))
217 (defun LLVMConstInsertValue (AggConstant ValConstant indices)
218 (with-array-and-length (array len indices)
219 (%LLVMConstInsertValue AggConstant ValConstant array len)))
221 (defun LLVMGetParams (fn)
222 "Returns a list of params rather than filling out a Dest pointer argument."
223 (let ((len (LLVMCountParams fn)))
224 (cffi:with-foreign-object (array :pointer len)
225 (%LLVMGetParams fn array)
226 (loop for i from 0 below len
227 collect
228 (cffi:mem-aref array :pointer i)))))
230 (defun LLVMAddIncoming (phi-node incoming-val incoming-block)
231 "Unlike the C API (but like the C++ API...), takes only a single
232 incoming val and incoming block. Call multiple times if you want to
233 add multiple incoming values."
234 (cffi:with-foreign-objects ((incoming-vals :pointer)
235 (incoming-blocks :pointer))
236 (setf (cffi:mem-aref incoming-vals :pointer 0) incoming-val)
237 (setf (cffi:mem-aref incoming-blocks :pointer 0) incoming-block)
238 (%LLVMAddIncoming phi-node incoming-vals incoming-blocks 1)))
240 (defun LLVMBuildAggregateRet (builder vals)
241 (with-array-and-length (array len vals)
242 (%LLVMBuildAggregateRet builder array len)))
244 (defun LLVMBuildInvoke (builder fn args then catch name)
245 (with-array-and-length (array len args)
246 (%LLVMBuildInvoke builder fn array len then catch name)))
248 (defun LLVMBuildGEP (builder ptr indices name)
249 (with-array-and-length (array len indices)
250 (%LLVMBuildGEP builder ptr array len name)))
252 (defun LLVMBuildInBoundsGEP (builder ptr indices name)
253 (with-array-and-length (array len indices)
254 (%LLVMBuildInBoundsGEP builder ptr array len name)))
256 (defun LLVMBuildCall (builder fn args name)
257 "Combines the C API's Args array and NumArgs arguments into a single
258 list ARGS."
259 (with-array-and-length (array len args)
260 (%LLVMBuildCall builder fn array len name)))
262 ;; TODO:
263 ;; LLVMCreateMemoryBufferWithContentsOfFile has OutMemBuf, OutMessage
264 ;; LLVMCreateMemoryBufferWithSTDIN has OutMemBuf, OutMessage