Start to clean things up to get into a state usable by others.
[cl-llvm.git] / src / core.i
blobee5c2c089a64e43b3b49d4f2d7f69f61aed6aac7
1 %include "typemaps.i"
3 typedef unsigned char uint8_t;
5 // Rename/wrap functions that take pointer arguments
7 %rename LLVMFunctionType "%%LLVMFunctionType";
8 %rename LLVMGetParamTypes "%%LLVMGetParamTypes";
9 %rename LLVMGetStructElementTypes "%%LLVMGetStructElementTypes";
10 %rename LLVMConstArray "%%LLVMConstArray";
11 %rename LLVMConstVector "%%LLVMConstVector";
12 %rename LLVMConstGEP "%%LLVMConstGEP";
13 %rename LLVMConstInBoundsGEP "%%LLVMConstInBoundsGEP";
14 %rename LLVMConstExtractValue "%%LLVMConstExtractValue";
15 %rename LLVMConstInsertValue "%%LLVMConstInsertValue";
16 %rename LLVMGetParams "%%LLVMGetParams";
17 %rename LLVMAddIncoming "%%LLVMAddIncoming";
18 %rename LLVMBuildAggregateRet "%%LLVMBuildAggregateRet";
19 %rename LLVMBuildInvoke "%%LLVMBuildInvoke";
20 %rename LLVMBuildGEP "%%LLVMBuildGEP";
21 %rename LLVMBuildInBoundsGEP "%%LLVMBuildInBoundsGEP";
22 %rename LLVMBuildCall "%%LLVMBuildCall";
24 // Rename/wrap "InContext" functions.
25 %ignore LLVMModuleCreateWithName;
26 %rename LLVMModuleCreateWithNameInContext "%%LLVMModuleCreateWithNameInContext";
27 %ignore LLVMInt1Type;
28 %rename LLVMInt1TypeInContext "%%LLVMInt1TypeInContext";
29 %ignore LLVMInt8Type;
30 %rename LLVMInt8TypeInContext "%%LLVMInt8TypeInContext";
31 %ignore LLVMInt16Type;
32 %rename LLVMInt16TypeInContext "%%LLVMInt16TypeInContext";
33 %ignore LLVMInt32Type;
34 %rename LLVMInt32TypeInContext "%%LLVMInt32TypeInContext";
35 %ignore LLVMInt64Type;
36 %rename LLVMInt64TypeInContext "%%LLVMInt64TypeInContext";
37 %ignore LLVMIntType;
38 %rename LLVMIntTypeInContext "%%LLVMIntTypeInContext";
39 %ignore LLVMFloatType;
40 %rename LLVMFloatTypeInContext "%%LLVMFloatTypeInContext";
42 %ignore LLVMDoubleType;
43 %rename LLVMDoubleTypeInContext "%%LLVMDoubleTypeInContext";
44 %ignore LLVMX86FP80Type;
45 %rename LLVMX86FP80TypeInContext "%%LLVMX86FP80TypeInContext";
46 %ignore LLVMFP128Type;
47 %rename LLVMFP128TypeInContext "%%LLVMFP128TypeInContext";
48 %ignore LLVMPPCFP128Type;
50 %rename LLVMPPCFP128TypeInContext "%%LLVMPPCFP128TypeInContext";
51 %ignore LLVMStructType;
52 %rename LLVMStructTypeInContext "%%LLVMStructTypeInContext";
53 %ignore LLVMVoidType;
55 %rename LLVMVoidTypeInContext "%%LLVMVoidTypeInContext";
56 %ignore LLVMLabelType;
57 %rename LLVMLabelTypeInContext "%%LLVMLabelTypeInContext";
58 %ignore LLVMOpaqueType;
59 %rename LLVMOpaqueTypeInContext "%%LLVMOpaqueTypeInContext";
61 %ignore LLVMConstString;
62 %rename LLVMConstStringInContext "%%LLVMConstStringInContext";
63 %ignore LLVMConstStruct;
64 %rename LLVMConstStructInContext "%%LLVMConstStructInContext";
65 %ignore LLVMAppendBasicBlock;
66 %rename LLVMAppendBasicBlockInContext "%%LLVMAppendBasicBlockInContext";
67 %ignore LLVMInsertBasicBlock;
68 %rename LLVMInsertBasicBlockInContext "%%LLVMInsertBasicBlockInContext";
69 %ignore LLVMCreateBuilder;
70 %rename LLVMCreateBuilderInContext "%%LLVMCreateBuilderInContext";
72 %include "llvm-c/Core.h"
75 %insert("swiglisp") %{
76 ;;; The wrappers to expose the "InContext" versions of the functions
77 ;;; in a more lispy way.
78 (declaim (special *llvm-context*))
80 (defun LLVMModuleCreateWithName (ModuleId &optional (context *llvm-context*))
81 (%LLVMModuleCreateWithNameInContext ModuleId context))
83 (defun LLVMInt1Type (&optional (context *llvm-context*))
84 (%LLVMInt1TypeInContext context))
86 (defun LLVMInt8Type (&optional (context *llvm-context*))
87 (%LLVMInt8TypeInContext context))
89 (defun LLVMInt16Type (&optional (context *llvm-context*))
90 (%LLVMInt16TypeInContext context))
92 (defun LLVMInt32Type (&optional (context *llvm-context*))
93 (%LLVMInt32TypeInContext context))
95 (defun LLVMInt64Type (&optional (context *llvm-context*))
96 (%LLVMInt64TypeInContext context))
98 (defun LLVMIntType (NumBits &optional (context *llvm-context*))
99 (%LLVMIntTypeInContext context NumBits))
101 (defun LLVMFloatType (&optional (context *llvm-context*))
102 (%LLVMFloatTypeInContext context))
104 (defun LLVMDoubleType (&optional (context *llvm-context*))
105 (%LLVMDoubleTypeInContext context))
107 (defun LLVMX86FP80Type (&optional (context *llvm-context*))
108 (%LLVMX86FP80TypeInContext context))
110 (defun LLVMFP128Type (&optional (context *llvm-context*))
111 (%LLVMFP128TypeInContext context))
113 (defun LLVMPPCFP128Type (&optional (context *llvm-context*))
114 (%LLVMPPCFP128TypeInContext context))
116 ;; LLVMStructTypeInContext handled below.
118 (defun LLVMVoidType (&optional (context *llvm-context*))
119 (%LLVMVoidTypeInContext context))
121 (defun LLVMLabelType (&optional (context *llvm-context*))
122 (%LLVMLabelTypeInContext context))
124 (defun LLVMOpaqueType (&optional (context *llvm-context*))
125 (%LLVMOpaqueTypeInContext context))
127 ;; LLVMConstStringInContext handled below
128 ;; LLVMConstStructInContext handled below
130 (defun LLVMAppendBasicBlock (Fn Name &optional (context *llvm-context*))
131 (%LLVMAppendBasicBlockInContext context Fn Name))
133 (defun LLVMInsertBasicBlock (BB Name &optional (context *llvm-context*))
134 (%LLVMInsertBasicBlockInContext context BB Name))
136 (defun LLVMCreateBuilder (&optional (context *llvm-context*))
137 (%LLVMCreateBuilderInContext context))
140 ;; More complex wrappers for dealing with pointers.
142 (defmacro with-array-and-length ((array len list) &body body)
143 (let ((list-v (gensym "list")))
144 `(let* ((,list-v ,list)
145 (,len (length ,list-v)))
146 (cffi:with-foreign-object (,array :pointer ,len)
147 (loop for l in ,list-v
148 for i from 0
150 (setf (cffi:mem-aref ,array :pointer i) l))
151 (progn ,@body)))))
153 (defun LLVMFunctionType (ret params is-var-arg)
154 "Combines the C API's ParamTypes array and ParamCount arguments into
155 a single list PARAMS."
156 (with-array-and-length (array len params)
157 (%LLVMFunctionType ret array len is-var-arg)))
159 (defun LLVMGetParamTypes (function-type)
160 "Returns a list of param types rather than filling out a Dest pointer argument"
161 (let ((len (LLVMCountParamTypes function-type)))
162 (cffi:with-foreign-object (array :pointer len)
163 (%LLVMGetParamTypes function-type array)
164 (loop for i from 0 below len
165 collect
166 (cffi:mem-aref array :pointer i)))))
168 (defun LLVMStructType (element-types is-packed &optional (context *llvm-context*))
169 (with-array-and-length (array len element-types)
170 (%LLVMStructTypeInContext context array len is-packed)))
172 (defun LLVMGetStructElementTypes (struct-type)
173 "Returns a list of param types rather than filling out a Dest pointer argument"
174 (let ((len (LLVMCountStructElementTypes struct-type)))
175 (cffi:with-foreign-object (array :pointer len)
176 (%LLVMGetStructElementTypes struct-type array)
177 (loop for i from 0 below len
178 collect
179 (cffi:mem-aref array :pointer i)))))
181 (defun LLVMConstString (str dont-null-terminate &optional (context *llvm-context*))
182 (cffi:with-foreign-string ((foreign-string num-bytes) str)
183 (%LLVMConstStringInContext context foreign-string num-bytes dont-null-terminate)))
185 (defun LLVMConstStruct (vals packed-p &optional (context *llvm-context*))
186 (with-array-and-length (array len vals)
187 (%LLVMConstStructInContext context array len packed-p)))
189 (defun LLVMConstArray (type vals)
190 (with-array-and-length (array len vals)
191 (%LLVMConstArray type array len)))
193 (defun LLVMConstVector (vals)
194 (with-array-and-length (array len vals)
195 (%LLVMConstVector array len)))
197 (defun LLVMConstGEP (ptr indices)
198 (with-array-and-length (array len indices)
199 (%LLVMConstGEP ptr array len)))
201 (defun LLVMConstInBoundsGEP (ptr indices)
202 (with-array-and-length (array len indices)
203 (%LLVMConstInBoundsGEP ptr array len)))
205 (defun LLVMConstExtractValue (AggConstant indices)
206 (with-array-and-length (array len indices)
207 (%LLVMConstExtractValue AggConstant array len)))
209 (defun LLVMConstInsertValue (AggConstant ValConstant indices)
210 (with-array-and-length (array len indices)
211 (%LLVMConstInsertValue AggConstant ValConstant array len)))
213 (defun LLVMGetParams (fn)
214 "Returns a list of params rather than filling out a Dest pointer argument."
215 (let ((len (LLVMCountParams fn)))
216 (cffi:with-foreign-object (array :pointer len)
217 (%LLVMGetParams fn array)
218 (loop for i from 0 below len
219 collect
220 (cffi:mem-aref array :pointer i)))))
222 (defun LLVMAddIncoming (phi-node incoming-val incoming-block)
223 "Unlike the C API (but like the C++ API...), takes only a single
224 incoming val and incoming block. Call multiple times if you want to
225 add multiple incoming values."
226 (cffi:with-foreign-objects ((incoming-vals :pointer)
227 (incoming-blocks :pointer))
228 (setf (cffi:mem-aref incoming-vals :pointer 0) incoming-val)
229 (setf (cffi:mem-aref incoming-blocks :pointer 0) incoming-block)
230 (%LLVMAddIncoming phi-node incoming-vals incoming-blocks 1)))
232 (defun LLVMBuildAggregateRet (builder vals)
233 (with-array-and-length (array len vals)
234 (%LLVMBuildAggregateRet builder array len)))
236 (defun LLVMBuildInvoke (builder fn args then catch name)
237 (with-array-and-length (array len args)
238 (%LLVMBuildInvoke builder fn array len then catch name)))
240 (defun LLVMBuildGEP (builder ptr indices name)
241 (with-array-and-length (array len indices)
242 (%LLVMBuildGEP builder ptr array len name)))
244 (defun LLVMBuildInBoundsGEP (builder ptr indices name)
245 (with-array-and-length (array len indices)
246 (%LLVMBuildInBoundsGEP builder ptr array len name)))
248 (defun LLVMBuildCall (builder fn args name)
249 "Combines the C API's Args array and NumArgs arguments into a single
250 list ARGS."
251 (with-array-and-length (array len args)
252 (%LLVMBuildCall builder fn array len name)))
254 ;; TODO:
255 ;; LLVMCreateMemoryBufferWithContentsOfFile has OutMemBuf, OutMessage
256 ;; LLVMCreateMemoryBufferWithSTDIN has OutMemBuf, OutMessage