baabe7a6d776df9e9740f07fcbf8941a9ddef911
[cl-llvm.git] / src / core.i
blobbaabe7a6d776df9e9740f07fcbf8941a9ddef911
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";
75 %include "llvm-c/Core.h"
78 %insert("swiglisp") %{
79 ;;; The wrappers to expose the "InContext" versions of the functions
80 ;;; in a more lispy way.
81 (declaim (special *llvm-context*))
83 (defun LLVMModuleCreateWithName (ModuleId &optional (context *llvm-context*))
84 (%LLVMModuleCreateWithNameInContext ModuleId context))
86 (defun LLVMInt1Type (&optional (context *llvm-context*))
87 (%LLVMInt1TypeInContext context))
89 (defun LLVMInt8Type (&optional (context *llvm-context*))
90 (%LLVMInt8TypeInContext context))
92 (defun LLVMInt16Type (&optional (context *llvm-context*))
93 (%LLVMInt16TypeInContext context))
95 (defun LLVMInt32Type (&optional (context *llvm-context*))
96 (%LLVMInt32TypeInContext context))
98 (defun LLVMInt64Type (&optional (context *llvm-context*))
99 (%LLVMInt64TypeInContext context))
101 (defun LLVMIntType (NumBits &optional (context *llvm-context*))
102 (%LLVMIntTypeInContext context NumBits))
104 (defun LLVMFloatType (&optional (context *llvm-context*))
105 (%LLVMFloatTypeInContext context))
107 (defun LLVMDoubleType (&optional (context *llvm-context*))
108 (%LLVMDoubleTypeInContext context))
110 (defun LLVMX86FP80Type (&optional (context *llvm-context*))
111 (%LLVMX86FP80TypeInContext context))
113 (defun LLVMFP128Type (&optional (context *llvm-context*))
114 (%LLVMFP128TypeInContext context))
116 (defun LLVMPPCFP128Type (&optional (context *llvm-context*))
117 (%LLVMPPCFP128TypeInContext context))
119 ;; LLVMStructTypeInContext handled below.
121 (defun LLVMVoidType (&optional (context *llvm-context*))
122 (%LLVMVoidTypeInContext context))
124 (defun LLVMLabelType (&optional (context *llvm-context*))
125 (%LLVMLabelTypeInContext context))
127 (defun LLVMOpaqueType (&optional (context *llvm-context*))
128 (%LLVMOpaqueTypeInContext context))
130 ;; LLVMConstStringInContext handled below
131 ;; LLVMConstStructInContext handled below
133 (defun LLVMAppendBasicBlock (Fn Name &optional (context *llvm-context*))
134 (%LLVMAppendBasicBlockInContext context Fn Name))
136 (defun LLVMInsertBasicBlock (BB Name &optional (context *llvm-context*))
137 (%LLVMInsertBasicBlockInContext context BB Name))
139 (defun LLVMCreateBuilder (&optional (context *llvm-context*))
140 (%LLVMCreateBuilderInContext context))
143 ;; More complex wrappers for dealing with pointers.
145 (defmacro with-array-and-length ((array len list) &body body)
146 (let ((list-v (gensym "list")))
147 `(let* ((,list-v ,list)
148 (,len (length ,list-v)))
149 (cffi:with-foreign-object (,array :pointer ,len)
150 (loop for l in ,list-v
151 for i from 0
153 (setf (cffi:mem-aref ,array :pointer i) l))
154 (progn ,@body)))))
156 (defun LLVMFunctionType (ret params is-var-arg)
157 "Combines the C API's ParamTypes array and ParamCount arguments into
158 a single list PARAMS."
159 (with-array-and-length (array len params)
160 (%LLVMFunctionType ret array len is-var-arg)))
162 (defun LLVMGetParamTypes (function-type)
163 "Returns a list of param types rather than filling out a Dest pointer argument"
164 (let ((len (LLVMCountParamTypes function-type)))
165 (cffi:with-foreign-object (array :pointer len)
166 (%LLVMGetParamTypes function-type array)
167 (loop for i from 0 below len
168 collect
169 (cffi:mem-aref array :pointer i)))))
171 (defun LLVMStructType (element-types is-packed &optional (context *llvm-context*))
172 (with-array-and-length (array len element-types)
173 (%LLVMStructTypeInContext context array len is-packed)))
175 (defun LLVMGetStructElementTypes (struct-type)
176 "Returns a list of param types rather than filling out a Dest pointer argument"
177 (let ((len (LLVMCountStructElementTypes struct-type)))
178 (cffi:with-foreign-object (array :pointer len)
179 (%LLVMGetStructElementTypes struct-type array)
180 (loop for i from 0 below len
181 collect
182 (cffi:mem-aref array :pointer i)))))
184 (defun LLVMConstString (str dont-null-terminate &optional (context *llvm-context*))
185 (cffi:with-foreign-string ((foreign-string num-bytes) str)
186 (%LLVMConstStringInContext context foreign-string num-bytes dont-null-terminate)))
188 (defun LLVMConstStruct (vals packed-p &optional (context *llvm-context*))
189 (with-array-and-length (array len vals)
190 (%LLVMConstStructInContext context array len packed-p)))
192 (defun LLVMConstArray (type vals)
193 (with-array-and-length (array len vals)
194 (%LLVMConstArray type array len)))
196 (defun LLVMConstVector (vals)
197 (with-array-and-length (array len vals)
198 (%LLVMConstVector array len)))
200 (defun LLVMConstGEP (ptr indices)
201 (with-array-and-length (array len indices)
202 (%LLVMConstGEP ptr array len)))
204 (defun LLVMConstInBoundsGEP (ptr indices)
205 (with-array-and-length (array len indices)
206 (%LLVMConstInBoundsGEP ptr array len)))
208 (defun LLVMConstExtractValue (AggConstant indices)
209 (with-array-and-length (array len indices)
210 (%LLVMConstExtractValue AggConstant array len)))
212 (defun LLVMConstInsertValue (AggConstant ValConstant indices)
213 (with-array-and-length (array len indices)
214 (%LLVMConstInsertValue AggConstant ValConstant array len)))
216 (defun LLVMGetParams (fn)
217 "Returns a list of params rather than filling out a Dest pointer argument."
218 (let ((len (LLVMCountParams fn)))
219 (cffi:with-foreign-object (array :pointer len)
220 (%LLVMGetParams fn array)
221 (loop for i from 0 below len
222 collect
223 (cffi:mem-aref array :pointer i)))))
225 (defun LLVMAddIncoming (phi-node incoming-val incoming-block)
226 "Unlike the C API (but like the C++ API...), takes only a single
227 incoming val and incoming block. Call multiple times if you want to
228 add multiple incoming values."
229 (cffi:with-foreign-objects ((incoming-vals :pointer)
230 (incoming-blocks :pointer))
231 (setf (cffi:mem-aref incoming-vals :pointer 0) incoming-val)
232 (setf (cffi:mem-aref incoming-blocks :pointer 0) incoming-block)
233 (%LLVMAddIncoming phi-node incoming-vals incoming-blocks 1)))
235 (defun LLVMBuildAggregateRet (builder vals)
236 (with-array-and-length (array len vals)
237 (%LLVMBuildAggregateRet builder array len)))
239 (defun LLVMBuildInvoke (builder fn args then catch name)
240 (with-array-and-length (array len args)
241 (%LLVMBuildInvoke builder fn array len then catch name)))
243 (defun LLVMBuildGEP (builder ptr indices name)
244 (with-array-and-length (array len indices)
245 (%LLVMBuildGEP builder ptr array len name)))
247 (defun LLVMBuildInBoundsGEP (builder ptr indices name)
248 (with-array-and-length (array len indices)
249 (%LLVMBuildInBoundsGEP builder ptr array len name)))
251 (defun LLVMBuildCall (builder fn args name)
252 "Combines the C API's Args array and NumArgs arguments into a single
253 list ARGS."
254 (with-array-and-length (array len args)
255 (%LLVMBuildCall builder fn array len name)))
257 ;; TODO:
258 ;; LLVMCreateMemoryBufferWithContentsOfFile has OutMemBuf, OutMessage
259 ;; LLVMCreateMemoryBufferWithSTDIN has OutMemBuf, OutMessage