Make LLVMConstInt transparently work with bignums.
[cl-llvm.git] / src / llvm-extras.i
blobb81775d4e571897deed6e5e9e408df71fcc4909e
1 %include "typemaps.i"
3 %rename CLLLVM_LLVMAddFunctionAttr "LLVMAddFunctionAttr";
4 %rename CLLLVM_LLVMRemoveFunctionAttr "LLVMRemoveFunctionAttr";
5 %rename CLLLVM_LLVMAddRetAttr "LLVMAddRetAttr";
6 %rename CLLLVM_LLVMRemoveRetAttr "LLVMRemoveRetAttr";
7 %rename CLLLVM_LLVMGetRetAttr "LLVMGetRetAttr";
9 %rename CLLLVM_LLVMConstIntOfBigVal "LLVMConstIntOfBigVal";
11 %include "./llvm-extras.cpp"
13 %insert("swiglisp") %{
14 (defun LLVMConstInt (Ty Num)
15 (declare (type integer Num))
16 ;; If the type is <= 64 bits, it's easy: we can just pass the
17 ;; (truncated) number into %LLVMConstInt directly. Otherwise,
18 ;; convert the number to a bunch of 64-bit words, and pass those to
19 ;; LLVMConstIntOfBigVal.
20 (let ((bitwidth (LLVMGetIntTypeWidth Ty)))
21 (if (< bitwidth 65)
22 (%LLVMConstInt Ty (logand Num #xffffffffffffffff) 0)
23 (let ((n-words (ceiling bitwidth 64)))
24 (cffi:with-foreign-object (array :unsigned-long-long n-words)
25 (loop for i from 0 below n-words
27 (setf (cffi:mem-aref array :unsigned-long-long i)
28 (logand Num #xffffffffffffffff))
29 (setf Num (ash Num -64)))
30 (LLVMConstIntOfBigVal Ty n-words array))))))