llvm.org GIT mirror llvm / 798ace2
Initial version of Go bindings. This code is based on the existing LLVM Go bindings project hosted at: https://github.com/go-llvm/llvm Note that all contributors to the gollvm project have agreed to relicense their changes under the LLVM license and submit them to the LLVM project. Differential Revision: http://reviews.llvm.org/D5684 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@219976 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 4 years ago
45 changed file(s) with 4426 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
4848 tools/polly
4949 # Sphinx build tree, if building in-source dir.
5050 docs/_build
51
52 #==============================================================================#
53 # Files created in tree by the Go bindings.
54 #==============================================================================#
55 bindings/go/llvm/llvm_config.go
56 bindings/go/llvm/workdir
201201 DOXYGEN := @DOXYGEN@
202202 GROFF := @GROFF@
203203 GZIPBIN := @GZIPBIN@
204 GO := @GO@
204205 OCAMLC := @OCAMLC@
205206 OCAMLOPT := @OCAMLOPT@
206207 OCAMLDEP := @OCAMLDEP@
12861286 AC_PATH_PROG(GZIPBIN, [gzip])
12871287 AC_PATH_PROG(PDFROFF, [pdfroff])
12881288 AC_PATH_PROG(ZIP, [zip])
1289 AC_PATH_PROG(GO, [go])
12891290 AC_PATH_PROGS(OCAMLC, [ocamlc])
12901291 AC_PATH_PROGS(OCAMLOPT, [ocamlopt])
12911292 AC_PATH_PROGS(OCAMLDEP, [ocamldep])
18661867 if test "x$OCAMLC" != x -a "x$OCAMLDEP" != x ; then
18671868 BINDINGS_TO_BUILD="ocaml $BINDINGS_TO_BUILD"
18681869 fi
1870 if test "x$GO" != x ; then
1871 if $GO run ${srcdir}/bindings/go/conftest.go ; then
1872 BINDINGS_TO_BUILD="go $BINDINGS_TO_BUILD"
1873 fi
1874 fi
18691875 fi
18701876 AC_SUBST(BINDINGS_TO_BUILD,$BINDINGS_TO_BUILD)
18711877
19001906 fi
19011907 fi
19021908 ;;
1909 go)
1910 if test "x$GO" = x ; then
1911 AC_MSG_WARN([--enable-bindings=go specified, but go not found. Try configure GO=/path/to/go])
1912 binding_prereqs_failed=1
1913 else
1914 if $GO run ${srcdir}/bindings/go/conftest.go ; then
1915 :
1916 else
1917 AC_MSG_WARN([--enable-bindings=go specified, but need at least Go 1.2. Try configure GO=/path/to/go])
1918 binding_prereqs_failed=1
1919 fi
1920 fi
1921 ;;
19031922 esac
19041923 done
19051924 if test "$binding_prereqs_failed" = 1 ; then
1010
1111 include $(LEVEL)/Makefile.config
1212
13 PARALLEL_DIRS = $(BINDINGS_TO_BUILD)
13 PARALLEL_DIRS =
14
15 ifneq (,$(filter ocaml,$(BINDINGS_TO_BUILD)))
16 PARALLEL_DIRS += ocaml
17 endif
1418
1519 include $(LEVEL)/Makefile.common
0 This directory contains LLVM bindings for the Go programming language
1 (http://golang.org).
2
3 Prerequisites
4 -------------
5
6 * Go 1.2+.
7 * CMake (to build LLVM).
8
9 Using the bindings
10 ------------------
11
12 The package path "llvm.org/llvm/bindings/go/llvm" can be used to
13 import the latest development version of LLVM from SVN. Paths such as
14 "llvm.org/llvm.v36/bindings/go/llvm" refer to released versions of LLVM.
15
16 It is recommended to use the "-d" flag with "go get" to download the
17 package or a dependency, as an additional step is required to build LLVM
18 (see "Building LLVM" below).
19
20 Building LLVM
21 -------------
22
23 The script "build.sh" in this directory can be used to build LLVM and prepare
24 it to be used by the bindings. If you receive an error message from "go build"
25 like this:
26
27 ./analysis.go:4:84: fatal error: llvm-c/Analysis.h: No such file or directory
28 #include // If you are getting an error here read bindings/go/README.txt
29
30 or like this:
31
32 ./llvm_dep.go:5: undefined: run_build_sh
33
34 it means that LLVM needs to be built or updated by running the script.
35
36 $ $GOPATH/src/llvm.org/llvm/bindings/go/build.sh
37
38 Any command line arguments supplied to the script are passed to LLVM's CMake
39 build system. A good set of arguments to use during development are:
40
41 $ $GOPATH/src/llvm.org/llvm/bindings/go/build.sh -DCMAKE_BUILD_TYPE=Debug -DLLVM_TARGETS_TO_BUILD=host -DBUILD_SHARED_LIBS=ON
42
43 Note that CMake keeps a cache of build settings so once you have built
44 LLVM there is no need to pass these arguments again after updating.
45
46 Alternatively, you can build LLVM yourself, but you must then set the
47 CGO_CPPFLAGS, CGO_CXXFLAGS and CGO_LDFLAGS environment variables:
48
49 $ export CGO_CPPFLAGS="`/path/to/llvm-build/bin/llvm-config --cppflags`"
50 $ export CGO_CXXFLAGS=-std=c++11
51 $ export CGO_LDFLAGS="`/path/to/llvm-build/bin/llvm-config --ldflags --libs --system-libs all`"
52 $ go build -tags byollvm
0 #!/bin/sh -xe
1
2 llvm_components="\
3 all-targets \
4 analysis \
5 asmparser \
6 asmprinter \
7 bitreader \
8 bitwriter \
9 codegen \
10 core \
11 debuginfo \
12 executionengine \
13 instrumentation \
14 interpreter \
15 ipo \
16 irreader \
17 linker \
18 mc \
19 mcjit \
20 objcarcopts \
21 option \
22 profiledata \
23 scalaropts \
24 support \
25 target \
26 "
27
28 if [ "$1" == "--print-components" ] ; then
29 echo $llvm_components
30 exit 0
31 fi
32
33 gollvmdir=$(dirname "$0")/llvm
34
35 workdir=$gollvmdir/workdir
36 llvmdir=$gollvmdir/../../..
37 llvm_builddir=$workdir/llvm_build
38
39 mkdir -p $llvm_builddir
40
41 cmake_flags="../../../../.. $@"
42 llvm_config="$llvm_builddir/bin/llvm-config"
43
44 if test -n "`which ninja`" ; then
45 # If Ninja is available, we can speed up the build by building only the
46 # required subset of LLVM.
47 (cd $llvm_builddir && cmake -G Ninja $cmake_flags)
48 ninja -C $llvm_builddir llvm-config
49 llvm_buildtargets="$($llvm_config --libs $llvm_components | sed -e 's/-l//g')"
50 ninja -C $llvm_builddir $llvm_buildtargets FileCheck
51 else
52 (cd $llvm_builddir && cmake $cmake_flags)
53 make -C $llvm_builddir -j4
54 fi
55
56 llvm_version="$($llvm_config --version)"
57 llvm_cflags="$($llvm_config --cppflags)"
58 llvm_ldflags="$($llvm_config --ldflags) $($llvm_config --libs $llvm_components) $($llvm_config --system-libs)"
59 if [ $(uname) != "Darwin" ]; then
60 # OS X doesn't like -rpath with cgo. See:
61 # https://code.google.com/p/go/issues/detail?id=7293
62 llvm_ldflags="-Wl,-rpath,$($llvm_config --libdir) $llvm_ldflags"
63 fi
64 sed -e "s#@LLVM_CFLAGS@#$llvm_cflags#g; s#@LLVM_LDFLAGS@#$llvm_ldflags#g" $gollvmdir/llvm_config.go.in > \
65 $gollvmdir/llvm_config.go
66 printf "package llvm\n\nconst Version = \"%s\"\n" "$llvm_version" > $gollvmdir/version.go
0 package main
1
2 import (
3 "go/build"
4 "os"
5 )
6
7 // Tests that the Go compiler is at least version 1.2.
8 func main() {
9 for _, tag := range build.Default.ReleaseTags {
10 if tag == "go1.2" {
11 os.Exit(0)
12 }
13 }
14 os.Exit(1)
15 }
0 //===- DIBuilderBindings.cpp - Bindings for DIBuilder ---------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines C bindings for the DIBuilder class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "DIBuilderBindings.h"
14
15 #include "llvm/IR/Module.h"
16 #include "llvm/IR/DIBuilder.h"
17
18 using namespace llvm;
19
20 namespace {
21 template
22 T unwrapDI(LLVMValueRef v) {
23 return v ? T(unwrap(v)) : T();
24 }
25 }
26
27 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef)
28
29 LLVMDIBuilderRef LLVMNewDIBuilder(LLVMModuleRef mref) {
30 Module *m = unwrap(mref);
31 return wrap(new DIBuilder(*m));
32 }
33
34 void LLVMDIBuilderDestroy(LLVMDIBuilderRef dref) {
35 DIBuilder *d = unwrap(dref);
36 delete d;
37 }
38
39 void LLVMDIBuilderFinalize(LLVMDIBuilderRef dref) { unwrap(dref)->finalize(); }
40
41 LLVMValueRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Dref,
42 unsigned Lang, const char *File,
43 const char *Dir,
44 const char *Producer, int Optimized,
45 const char *Flags,
46 unsigned RuntimeVersion) {
47 DIBuilder *D = unwrap(Dref);
48 DICompileUnit CU = D->createCompileUnit(Lang, File, Dir, Producer, Optimized,
49 Flags, RuntimeVersion);
50 return wrap(CU);
51 }
52
53 LLVMValueRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref, const char *File,
54 const char *Dir) {
55 DIBuilder *D = unwrap(Dref);
56 DIFile F = D->createFile(File, Dir);
57 return wrap(F);
58 }
59
60 LLVMValueRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,
61 LLVMValueRef Scope,
62 LLVMValueRef File, unsigned Line,
63 unsigned Column) {
64 DIBuilder *D = unwrap(Dref);
65 DILexicalBlock LB = D->createLexicalBlock(
66 unwrapDI(Scope), unwrapDI(File), Line, Column);
67 return wrap(LB);
68 }
69
70 LLVMValueRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,
71 LLVMValueRef Scope,
72 LLVMValueRef File,
73 unsigned Discriminator) {
74 DIBuilder *D = unwrap(Dref);
75 DILexicalBlockFile LBF = D->createLexicalBlockFile(
76 unwrapDI(Scope), unwrapDI(File), Discriminator);
77 return wrap(LBF);
78 }
79
80 LLVMValueRef LLVMDIBuilderCreateFunction(
81 LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
82 const char *LinkageName, LLVMValueRef File, unsigned Line,
83 LLVMValueRef CompositeType, int IsLocalToUnit, int IsDefinition,
84 unsigned ScopeLine, unsigned Flags, int IsOptimized, LLVMValueRef Func) {
85 DIBuilder *D = unwrap(Dref);
86 DISubprogram SP = D->createFunction(
87 unwrapDI(Scope), Name, LinkageName, unwrapDI(File),
88 Line, unwrapDI(CompositeType), IsLocalToUnit,
89 IsDefinition, ScopeLine, Flags, IsOptimized, unwrap(Func));
90 return wrap(SP);
91 }
92
93 LLVMValueRef LLVMDIBuilderCreateLocalVariable(
94 LLVMDIBuilderRef Dref, unsigned Tag, LLVMValueRef Scope, const char *Name,
95 LLVMValueRef File, unsigned Line, LLVMValueRef Ty, int AlwaysPreserve,
96 unsigned Flags, unsigned ArgNo) {
97 DIBuilder *D = unwrap(Dref);
98 DIVariable V = D->createLocalVariable(
99 Tag, unwrapDI(Scope), Name, unwrapDI(File), Line,
100 unwrapDI(Ty), AlwaysPreserve, Flags, ArgNo);
101 return wrap(V);
102 }
103
104 LLVMValueRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref,
105 const char *Name, uint64_t SizeInBits,
106 uint64_t AlignInBits,
107 unsigned Encoding) {
108 DIBuilder *D = unwrap(Dref);
109 DIBasicType T = D->createBasicType(Name, SizeInBits, AlignInBits, Encoding);
110 return wrap(T);
111 }
112
113 LLVMValueRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
114 LLVMValueRef PointeeType,
115 uint64_t SizeInBits,
116 uint64_t AlignInBits,
117 const char *Name) {
118 DIBuilder *D = unwrap(Dref);
119 DIDerivedType T = D->createPointerType(unwrapDI(PointeeType),
120 SizeInBits, AlignInBits, Name);
121 return wrap(T);
122 }
123
124 LLVMValueRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref,
125 LLVMValueRef File,
126 LLVMValueRef ParameterTypes) {
127 DIBuilder *D = unwrap(Dref);
128 DICompositeType CT = D->createSubroutineType(
129 unwrapDI(File), unwrapDI(ParameterTypes));
130 return wrap(CT);
131 }
132
133 LLVMValueRef LLVMDIBuilderCreateStructType(
134 LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
135 LLVMValueRef File, unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
136 unsigned Flags, LLVMValueRef DerivedFrom, LLVMValueRef ElementTypes) {
137 DIBuilder *D = unwrap(Dref);
138 DICompositeType CT = D->createStructType(
139 unwrapDI(Scope), Name, unwrapDI(File), Line,
140 SizeInBits, AlignInBits, Flags, unwrapDI(DerivedFrom),
141 unwrapDI(ElementTypes));
142 return wrap(CT);
143 }
144
145 LLVMValueRef LLVMDIBuilderCreateMemberType(
146 LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
147 LLVMValueRef File, unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
148 uint64_t OffsetInBits, unsigned Flags, LLVMValueRef Ty) {
149 DIBuilder *D = unwrap(Dref);
150 DIDerivedType DT = D->createMemberType(
151 unwrapDI(Scope), Name, unwrapDI(File), Line,
152 SizeInBits, AlignInBits, OffsetInBits, Flags, unwrapDI(Ty));
153 return wrap(DT);
154 }
155
156 LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
157 uint64_t SizeInBits,
158 uint64_t AlignInBits,
159 LLVMValueRef ElementType,
160 LLVMValueRef Subscripts) {
161 DIBuilder *D = unwrap(Dref);
162 DICompositeType CT =
163 D->createArrayType(SizeInBits, AlignInBits, unwrapDI(ElementType),
164 unwrapDI(Subscripts));
165 return wrap(CT);
166 }
167
168 LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref, LLVMValueRef Ty,
169 const char *Name, LLVMValueRef File,
170 unsigned Line, LLVMValueRef Context) {
171 DIBuilder *D = unwrap(Dref);
172 DIDerivedType DT =
173 D->createTypedef(unwrapDI(Ty), Name, unwrapDI(File), Line,
174 unwrapDI(Context));
175 return wrap(DT);
176 }
177
178 LLVMValueRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref, int64_t Lo,
179 int64_t Count) {
180 DIBuilder *D = unwrap(Dref);
181 DISubrange S = D->getOrCreateSubrange(Lo, Count);
182 return wrap(S);
183 }
184
185 LLVMValueRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
186 LLVMValueRef *Data, size_t Length) {
187 DIBuilder *D = unwrap(Dref);
188 Value **DataValue = unwrap(Data);
189 ArrayRef Elements(DataValue, Length);
190 DIArray A = D->getOrCreateArray(Elements);
191 return wrap(A);
192 }
193
194 LLVMValueRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
195 LLVMValueRef *Data,
196 size_t Length) {
197 DIBuilder *D = unwrap(Dref);
198 Value **DataValue = unwrap(Data);
199 ArrayRef Elements(DataValue, Length);
200 DITypeArray A = D->getOrCreateTypeArray(Elements);
201 return wrap(A);
202 }
203
204 LLVMValueRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, int64_t *Addr,
205 size_t Length) {
206 DIBuilder *D = unwrap(Dref);
207 DIExpression Expr = D->createExpression(ArrayRef(Addr, Length));
208 return wrap(Expr);
209 }
210
211 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
212 LLVMValueRef Storage,
213 LLVMValueRef VarInfo,
214 LLVMValueRef Expr,
215 LLVMBasicBlockRef Block) {
216 DIBuilder *D = unwrap(Dref);
217 Instruction *Instr =
218 D->insertDeclare(unwrap(Storage), unwrapDI(VarInfo),
219 unwrapDI(Expr), unwrap(Block));
220 return wrap(Instr);
221 }
0 //===- DIBuilderBindings.h - Bindings for DIBuilder -------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines C bindings for the DIBuilder class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_BINDINGS_GO_LLVM_DIBUILDERBINDINGS_H
14 #define LLVM_BINDINGS_GO_LLVM_DIBUILDERBINDINGS_H
15
16 #include "llvm-c/Core.h"
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 // FIXME: These bindings shouldn't be Go-specific and should eventually move to
23 // a (somewhat) less stable collection of C APIs for use in creating bindings of
24 // LLVM in other languages.
25
26 typedef struct LLVMOpaqueDIBuilder *LLVMDIBuilderRef;
27
28 LLVMDIBuilderRef LLVMNewDIBuilder(LLVMModuleRef m);
29
30 void LLVMDIBuilderDestroy(LLVMDIBuilderRef d);
31 void LLVMDIBuilderFinalize(LLVMDIBuilderRef d);
32
33 LLVMValueRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef D,
34 unsigned Language, const char *File,
35 const char *Dir,
36 const char *Producer, int Optimized,
37 const char *Flags,
38 unsigned RuntimeVersion);
39
40 LLVMValueRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef D, const char *File,
41 const char *Dir);
42
43 LLVMValueRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef D,
44 LLVMValueRef Scope,
45 LLVMValueRef File, unsigned Line,
46 unsigned Column);
47
48 LLVMValueRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef D,
49 LLVMValueRef Scope,
50 LLVMValueRef File,
51 unsigned Discriminator);
52
53 LLVMValueRef LLVMDIBuilderCreateFunction(
54 LLVMDIBuilderRef D, LLVMValueRef Scope, const char *Name,
55 const char *LinkageName, LLVMValueRef File, unsigned Line,
56 LLVMValueRef CompositeType, int IsLocalToUnit, int IsDefinition,
57 unsigned ScopeLine, unsigned Flags, int IsOptimized, LLVMValueRef Function);
58
59 LLVMValueRef LLVMDIBuilderCreateLocalVariable(
60 LLVMDIBuilderRef D, unsigned Tag, LLVMValueRef Scope, const char *Name,
61 LLVMValueRef File, unsigned Line, LLVMValueRef Ty, int AlwaysPreserve,
62 unsigned Flags, unsigned ArgNo);
63
64 LLVMValueRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef D, const char *Name,
65 uint64_t SizeInBits,
66 uint64_t AlignInBits,
67 unsigned Encoding);
68
69 LLVMValueRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef D,
70 LLVMValueRef PointeeType,
71 uint64_t SizeInBits,
72 uint64_t AlignInBits,
73 const char *Name);
74
75 LLVMValueRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef D,
76 LLVMValueRef File,
77 LLVMValueRef ParameterTypes);
78
79 LLVMValueRef LLVMDIBuilderCreateStructType(
80 LLVMDIBuilderRef D, LLVMValueRef Scope, const char *Name, LLVMValueRef File,
81 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags,
82 LLVMValueRef DerivedFrom, LLVMValueRef ElementTypes);
83
84 LLVMValueRef LLVMDIBuilderCreateMemberType(
85 LLVMDIBuilderRef D, LLVMValueRef Scope, const char *Name, LLVMValueRef File,
86 unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
87 uint64_t OffsetInBits, unsigned Flags, LLVMValueRef Ty);
88
89 LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef D,
90 uint64_t SizeInBits,
91 uint64_t AlignInBits,
92 LLVMValueRef ElementType,
93 LLVMValueRef Subscripts);
94
95 LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef D, LLVMValueRef Ty,
96 const char *Name, LLVMValueRef File,
97 unsigned Line, LLVMValueRef Context);
98
99 LLVMValueRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef D, int64_t Lo,
100 int64_t Count);
101
102 LLVMValueRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef D,
103 LLVMValueRef *Data, size_t Length);
104
105 LLVMValueRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef D,
106 LLVMValueRef *Data,
107 size_t Length);
108
109 LLVMValueRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, int64_t *Addr,
110 size_t Length);
111
112 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef D,
113 LLVMValueRef Storage,
114 LLVMValueRef VarInfo,
115 LLVMValueRef Expr,
116 LLVMBasicBlockRef Block);
117
118 #ifdef __cplusplus
119 } // extern "C"
120 #endif
121
122 #endif
0 //===- IRBindings.cpp - Additional bindings for ir ------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines additional C bindings for the ir component.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "IRBindings.h"
14
15 #include "llvm/IR/Attributes.h"
16 #include "llvm/IR/Function.h"
17
18 using namespace llvm;
19
20 void LLVMAddFunctionAttr2(LLVMValueRef Fn, uint64_t PA) {
21 Function *Func = unwrap(Fn);
22 const AttributeSet PAL = Func->getAttributes();
23 AttrBuilder B(PA);
24 const AttributeSet PALnew =
25 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
26 AttributeSet::get(Func->getContext(),
27 AttributeSet::FunctionIndex, B));
28 Func->setAttributes(PALnew);
29 }
30
31 uint64_t LLVMGetFunctionAttr2(LLVMValueRef Fn) {
32 Function *Func = unwrap(Fn);
33 const AttributeSet PAL = Func->getAttributes();
34 return PAL.Raw(AttributeSet::FunctionIndex);
35 }
36
37 void LLVMRemoveFunctionAttr2(LLVMValueRef Fn, uint64_t PA) {
38 Function *Func = unwrap(Fn);
39 const AttributeSet PAL = Func->getAttributes();
40 AttrBuilder B(PA);
41 const AttributeSet PALnew =
42 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
43 AttributeSet::get(Func->getContext(),
44 AttributeSet::FunctionIndex, B));
45 Func->setAttributes(PALnew);
46 }
0 //===- IRBindings.h - Additional bindings for IR ----------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines additional C bindings for the IR component.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_BINDINGS_GO_LLVM_IRBINDINGS_H
14 #define LLVM_BINDINGS_GO_LLVM_IRBINDINGS_H
15
16 #include "llvm-c/Core.h"
17 #include
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 // These functions duplicate the LLVM*FunctionAttr functions in the stable C
24 // API. We cannot use the existing functions because they take 32-bit attribute
25 // values, and the Go bindings expose all of the LLVM attributes, some of which
26 // have values >= 1<<32.
27
28 void LLVMAddFunctionAttr2(LLVMValueRef Fn, uint64_t PA);
29 uint64_t LLVMGetFunctionAttr2(LLVMValueRef Fn);
30 void LLVMRemoveFunctionAttr2(LLVMValueRef Fn, uint64_t PA);
31
32 #ifdef __cplusplus
33 }
34 #endif
35
36 #endif
0 //===- InstrumentationBindings.cpp - instrumentation bindings -------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines C bindings for the instrumentation component.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "InstrumentationBindings.h"
14
15 #include "llvm-c/Core.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/Transforms/Instrumentation.h"
19
20 using namespace llvm;
21
22 void LLVMAddAddressSanitizerFunctionPass(LLVMPassManagerRef PM) {
23 unwrap(PM)->add(createAddressSanitizerFunctionPass());
24 }
25
26 void LLVMAddAddressSanitizerModulePass(LLVMPassManagerRef PM) {
27 unwrap(PM)->add(createAddressSanitizerModulePass());
28 }
29
30 void LLVMAddThreadSanitizerPass(LLVMPassManagerRef PM) {
31 unwrap(PM)->add(createThreadSanitizerPass());
32 }
33
34 void LLVMAddMemorySanitizerPass(LLVMPassManagerRef PM) {
35 unwrap(PM)->add(createMemorySanitizerPass());
36 }
37
38 void LLVMAddDataFlowSanitizerPass(LLVMPassManagerRef PM,
39 const char *ABIListFile) {
40 unwrap(PM)->add(createDataFlowSanitizerPass(ABIListFile));
41 }
0 //===- InstrumentationBindings.h - instrumentation bindings -----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines C bindings for the Transforms/Instrumentation component.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_BINDINGS_GO_LLVM_INSTRUMENTATIONBINDINGS_H
14 #define LLVM_BINDINGS_GO_LLVM_INSTRUMENTATIONBINDINGS_H
15
16 #include "llvm-c/Core.h"
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 // FIXME: These bindings shouldn't be Go-specific and should eventually move to
23 // a (somewhat) less stable collection of C APIs for use in creating bindings of
24 // LLVM in other languages.
25
26 void LLVMAddAddressSanitizerFunctionPass(LLVMPassManagerRef PM);
27 void LLVMAddAddressSanitizerModulePass(LLVMPassManagerRef PM);
28 void LLVMAddThreadSanitizerPass(LLVMPassManagerRef PM);
29 void LLVMAddMemorySanitizerPass(LLVMPassManagerRef PM);
30 void LLVMAddDataFlowSanitizerPass(LLVMPassManagerRef PM,
31 const char *ABIListFile);
32
33 #ifdef __cplusplus
34 }
35 #endif
36
37 #endif
0 //===- SupportBindings.cpp - Additional bindings for support --------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines additional C bindings for the support component.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "SupportBindings.h"
14
15 #include "llvm/Support/DynamicLibrary.h"
16 #include
17 #include
18
19 void LLVMLoadLibraryPermanently2(const char *Filename, char **ErrMsg) {
20 std::string ErrMsgStr;
21 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Filename, &ErrMsgStr)) {
22 *ErrMsg = static_cast(malloc(ErrMsgStr.size() + 1));
23 memcpy(static_cast(*ErrMsg),
24 static_cast(ErrMsgStr.c_str()), ErrMsgStr.size() + 1);
25 }
26 }
0 //===- SupportBindings.h - Additional bindings for Support ------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines additional C bindings for the Support component.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_BINDINGS_GO_LLVM_SUPPORTBINDINGS_H
14 #define LLVM_BINDINGS_GO_LLVM_SUPPORTBINDINGS_H
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 // This function duplicates the LLVMLoadLibraryPermanently function in the
21 // stable C API and adds an extra ErrMsg parameter to retrieve the error
22 // message.
23 void LLVMLoadLibraryPermanently2(const char *Filename, char **ErrMsg);
24
25 #ifdef __cplusplus
26 }
27 #endif
28
29 #endif
0 //===- analysis.go - Bindings for analysis --------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines bindings for the analysis component.
10 //
11 //===----------------------------------------------------------------------===//
12
13 package llvm
14
15 /*
16 #include "llvm-c/Analysis.h" // If you are getting an error here read bindings/go/README.txt
17 #include
18 */
19 import "C"
20 import "errors"
21
22 type VerifierFailureAction C.LLVMVerifierFailureAction
23
24 const (
25 // verifier will print to stderr and abort()
26 AbortProcessAction VerifierFailureAction = C.LLVMAbortProcessAction
27 // verifier will print to stderr and return 1
28 PrintMessageAction VerifierFailureAction = C.LLVMPrintMessageAction
29 // verifier will just return 1
30 ReturnStatusAction VerifierFailureAction = C.LLVMReturnStatusAction
31 )
32
33 // Verifies that a module is valid, taking the specified action if not.
34 // Optionally returns a human-readable description of any invalid constructs.
35 func VerifyModule(m Module, a VerifierFailureAction) error {
36 var cmsg *C.char
37 broken := C.LLVMVerifyModule(m.C, C.LLVMVerifierFailureAction(a), &cmsg)
38
39 // C++'s verifyModule means isModuleBroken, so it returns false if
40 // there are no errors
41 if broken != 0 {
42 err := errors.New(C.GoString(cmsg))
43 C.LLVMDisposeMessage(cmsg)
44 return err
45 }
46 return nil
47 }
48
49 var verifyFunctionError = errors.New("Function is broken")
50
51 // Verifies that a single function is valid, taking the specified action.
52 // Useful for debugging.
53 func VerifyFunction(f Value, a VerifierFailureAction) error {
54 broken := C.LLVMVerifyFunction(f.C, C.LLVMVerifierFailureAction(a))
55
56 // C++'s verifyFunction means isFunctionBroken, so it returns false if
57 // there are no errors
58 if broken != 0 {
59 return verifyFunctionError
60 }
61 return nil
62 }
63
64 // Open up a ghostview window that displays the CFG of the current function.
65 // Useful for debugging.
66 func ViewFunctionCFG(f Value) { C.LLVMViewFunctionCFG(f.C) }
67 func ViewFunctionCFGOnly(f Value) { C.LLVMViewFunctionCFGOnly(f.C) }
0 //===- bitreader.go - Bindings for bitreader ------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines bindings for the bitreader component.
10 //
11 //===----------------------------------------------------------------------===//
12
13 package llvm
14
15 /*
16 #include "llvm-c/BitReader.h"
17 #include
18 */
19 import "C"
20
21 import (
22 "errors"
23 "unsafe"
24 )
25
26 // ParseBitcodeFile parses the LLVM IR (bitcode) in the file with the
27 // specified name, and returns a new LLVM module.
28 func ParseBitcodeFile(name string) (Module, error) {
29 var buf C.LLVMMemoryBufferRef
30 var errmsg *C.char
31 var cfilename *C.char = C.CString(name)
32 defer C.free(unsafe.Pointer(cfilename))
33 result := C.LLVMCreateMemoryBufferWithContentsOfFile(cfilename, &buf, &errmsg)
34 if result != 0 {
35 err := errors.New(C.GoString(errmsg))
36 C.free(unsafe.Pointer(errmsg))
37 return Module{}, err
38 }
39 defer C.LLVMDisposeMemoryBuffer(buf)
40
41 var m Module
42 if C.LLVMParseBitcode(buf, &m.C, &errmsg) == 0 {
43 return m, nil
44 }
45
46 err := errors.New(C.GoString(errmsg))
47 C.free(unsafe.Pointer(errmsg))
48 return Module{}, err
49 }
0 //===- bitwriter.go - Bindings for bitwriter ------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines bindings for the bitwriter component.
10 //
11 //===----------------------------------------------------------------------===//
12
13 package llvm
14
15 /*
16 #include "llvm-c/BitWriter.h"
17 #include
18 */
19 import "C"
20 import "os"
21 import "errors"
22
23 var writeBitcodeToFileErr = errors.New("Failed to write bitcode to file")
24
25 func WriteBitcodeToFile(m Module, file *os.File) error {
26 fail := C.LLVMWriteBitcodeToFD(m.C, C.int(file.Fd()), C.int(0), C.int(0))
27 if fail != 0 {
28 return writeBitcodeToFileErr
29 }
30 return nil
31 }
32
33 func WriteBitcodeToMemoryBuffer(m Module) MemoryBuffer {
34 mb := C.LLVMWriteBitcodeToMemoryBuffer(m.C)
35 return MemoryBuffer{mb}
36 }
37
38 // TODO(nsf): Figure out way how to make it work with io.Writer
0 //===- dibuilder.go - Bindings for DIBuilder ------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines bindings for the DIBuilder class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 package llvm
14
15 /*
16 #include "DIBuilderBindings.h"
17 #include
18 */
19 import "C"
20
21 import (
22 "debug/dwarf"
23 "unsafe"
24 )
25
26 type DwarfTag uint32
27
28 const (
29 DW_TAG_lexical_block DwarfTag = 0x0b
30 DW_TAG_compile_unit DwarfTag = 0x11
31 DW_TAG_variable DwarfTag = 0x34
32 DW_TAG_base_type DwarfTag = 0x24
33 DW_TAG_pointer_type DwarfTag = 0x0F
34 DW_TAG_structure_type DwarfTag = 0x13
35 DW_TAG_subroutine_type DwarfTag = 0x15
36 DW_TAG_file_type DwarfTag = 0x29
37 DW_TAG_subprogram DwarfTag = 0x2E
38 DW_TAG_auto_variable DwarfTag = 0x100
39 DW_TAG_arg_variable DwarfTag = 0x101
40 )
41
42 const (
43 FlagPrivate = 1 << iota
44 FlagProtected
45 FlagFwdDecl
46 FlagAppleBlock
47 FlagBlockByrefStruct
48 FlagVirtual
49 FlagArtificial
50 FlagExplicit
51 FlagPrototyped
52 FlagObjcClassComplete
53 FlagObjectPointer
54 FlagVector
55 FlagStaticMember
56 FlagIndirectVariable
57 )
58
59 type DwarfLang uint32
60
61 const (
62 // http://dwarfstd.org/ShowIssue.php?issue=101014.1&type=open
63 DW_LANG_Go DwarfLang = 0x0016
64 )
65
66 type DwarfTypeEncoding uint32
67
68 const (
69 DW_ATE_address DwarfTypeEncoding = 0x01
70 DW_ATE_boolean DwarfTypeEncoding = 0x02
71 DW_ATE_complex_float DwarfTypeEncoding = 0x03
72 DW_ATE_float DwarfTypeEncoding = 0x04
73 DW_ATE_signed DwarfTypeEncoding = 0x05
74 DW_ATE_signed_char DwarfTypeEncoding = 0x06
75 DW_ATE_unsigned DwarfTypeEncoding = 0x07
76 DW_ATE_unsigned_char DwarfTypeEncoding = 0x08
77 DW_ATE_imaginary_float DwarfTypeEncoding = 0x09
78 DW_ATE_packed_decimal DwarfTypeEncoding = 0x0a
79 DW_ATE_numeric_string DwarfTypeEncoding = 0x0b
80 DW_ATE_edited DwarfTypeEncoding = 0x0c
81 DW_ATE_signed_fixed DwarfTypeEncoding = 0x0d
82 DW_ATE_unsigned_fixed DwarfTypeEncoding = 0x0e
83 DW_ATE_decimal_float DwarfTypeEncoding = 0x0f
84 DW_ATE_UTF DwarfTypeEncoding = 0x10
85 DW_ATE_lo_user DwarfTypeEncoding = 0x80
86 DW_ATE_hi_user DwarfTypeEncoding = 0xff
87 )
88
89 // DIBuilder is a wrapper for the LLVM DIBuilder class.
90 type DIBuilder struct {
91 ref C.LLVMDIBuilderRef
92 m Module
93 }
94
95 // NewDIBuilder creates a new DIBuilder, associated with the given module.
96 func NewDIBuilder(m Module) *DIBuilder {
97 d := C.LLVMNewDIBuilder(m.C)
98 return &DIBuilder{ref: d, m: m}
99 }
100
101 // Destroy destroys the DIBuilder.
102 func (d *DIBuilder) Destroy() {
103 C.LLVMDIBuilderDestroy(d.ref)
104 }
105
106 // FInalize finalizes the debug information generated by the DIBuilder.
107 func (d *DIBuilder) Finalize() {
108 C.LLVMDIBuilderFinalize(d.ref)
109 }
110
111 // DICompileUnit holds the values for creating compile unit debug metadata.
112 type DICompileUnit struct {
113 Language DwarfLang
114 File string
115 Dir string
116 Producer string
117 Optimized bool
118 Flags string
119 RuntimeVersion int
120 }
121
122 // CreateCompileUnit creates compile unit debug metadata.
123 func (d *DIBuilder) CreateCompileUnit(cu DICompileUnit) Value {
124 file := C.CString(cu.File)
125 defer C.free(unsafe.Pointer(file))
126 dir := C.CString(cu.Dir)
127 defer C.free(unsafe.Pointer(dir))
128 producer := C.CString(cu.Producer)
129 defer C.free(unsafe.Pointer(producer))
130 flags := C.CString(cu.Flags)
131 defer C.free(unsafe.Pointer(flags))
132 result := C.LLVMDIBuilderCreateCompileUnit(
133 d.ref,
134 C.unsigned(cu.Language),
135 file, dir,
136 producer,
137 boolToCInt(cu.Optimized),
138 flags,
139 C.unsigned(cu.RuntimeVersion),
140 )
141 return Value{C: result}
142 }
143
144 // CreateCompileUnit creates file debug metadata.
145 func (d *DIBuilder) CreateFile(filename, dir string) Value {
146 cfilename := C.CString(filename)
147 defer C.free(unsafe.Pointer(cfilename))
148 cdir := C.CString(dir)
149 defer C.free(unsafe.Pointer(cdir))
150 result := C.LLVMDIBuilderCreateFile(d.ref, cfilename, cdir)
151 return Value{C: result}
152 }
153
154 // DILexicalBlock holds the values for creating lexical block debug metadata.
155 type DILexicalBlock struct {
156 File Value
157 Line int
158 Column int
159 }
160
161 // CreateCompileUnit creates lexical block debug metadata.
162 func (d *DIBuilder) CreateLexicalBlock(diScope Value, b DILexicalBlock) Value {
163 result := C.LLVMDIBuilderCreateLexicalBlock(
164 d.ref,
165 diScope.C,
166 b.File.C,
167 C.unsigned(b.Line),
168 C.unsigned(b.Column),
169 )
170 return Value{C: result}
171 }
172
173 func (d *DIBuilder) CreateLexicalBlockFile(diScope Value, diFile Value, discriminator int) Value {
174 result := C.LLVMDIBuilderCreateLexicalBlockFile(d.ref, diScope.C, diFile.C,
175 C.unsigned(discriminator))
176 return Value{C: result}
177 }
178
179 // DIFunction holds the values for creating function debug metadata.
180 type DIFunction struct {
181 Name string
182 LinkageName string
183 File Value
184 Line int
185 Type Value
186 LocalToUnit bool
187 IsDefinition bool
188 ScopeLine int
189 Flags int
190 Optimized bool
191 Function Value
192 }
193
194 // CreateCompileUnit creates function debug metadata.
195 func (d *DIBuilder) CreateFunction(diScope Value, f DIFunction) Value {
196 name := C.CString(f.Name)
197 defer C.free(unsafe.Pointer(name))
198 linkageName := C.CString(f.LinkageName)
199 defer C.free(unsafe.Pointer(linkageName))
200 result := C.LLVMDIBuilderCreateFunction(
201 d.ref,
202 diScope.C,
203 name,
204 linkageName,
205 f.File.C,
206 C.unsigned(f.Line),
207 f.Type.C,
208 boolToCInt(f.LocalToUnit),
209 boolToCInt(f.IsDefinition),
210 C.unsigned(f.ScopeLine),
211 C.unsigned(f.Flags),
212 boolToCInt(f.Optimized),
213 f.Function.C,
214 )
215 return Value{C: result}
216 }
217
218 // DILocalVariable holds the values for creating local variable debug metadata.
219 type DILocalVariable struct {
220 Tag dwarf.Tag
221 Name string
222 File Value
223 Line int
224 Type Value
225 AlwaysPreserve bool
226 Flags int
227
228 // ArgNo is the 1-based index of the argument in the function's
229 // parameter list if it is an argument, or 0 otherwise.
230 ArgNo int
231 }
232
233 // CreateLocalVariable creates local variable debug metadata.
234 func (d *DIBuilder) CreateLocalVariable(scope Value, v DILocalVariable) Value {
235 name := C.CString(v.Name)
236 defer C.free(unsafe.Pointer(name))
237 result := C.LLVMDIBuilderCreateLocalVariable(
238 d.ref,
239 C.unsigned(v.Tag),
240 scope.C,
241 name,
242 v.File.C,
243 C.unsigned(v.Line),
244 v.Type.C,
245 boolToCInt(v.AlwaysPreserve),
246 C.unsigned(v.Flags),
247 C.unsigned(v.ArgNo),
248 )
249 return Value{C: result}
250 }
251
252 // DIBasicType holds the values for creating basic type debug metadata.
253 type DIBasicType struct {
254 Name string
255 SizeInBits uint64
256 AlignInBits uint64
257 Encoding DwarfTypeEncoding
258 }
259
260 // CreateBasicType creates basic type debug metadata.
261 func (d *DIBuilder) CreateBasicType(t DIBasicType) Value {
262 name := C.CString(t.Name)
263 defer C.free(unsafe.Pointer(name))
264 result := C.LLVMDIBuilderCreateBasicType(
265 d.ref,
266 name,
267 C.uint64_t(t.SizeInBits),
268 C.uint64_t(t.AlignInBits),
269 C.unsigned(t.Encoding),
270 )
271 return Value{C: result}
272 }
273
274 // DIPointerType holds the values for creating pointer type debug metadata.
275 type DIPointerType struct {
276 Pointee Value
277 SizeInBits uint64
278 AlignInBits uint64 // optional
279 Name string // optional
280 }
281
282 // CreateBasicType creates basic type debug metadata.
283 func (d *DIBuilder) CreatePointerType(t DIPointerType) Value {
284 name := C.CString(t.Name)
285 defer C.free(unsafe.Pointer(name))
286 result := C.LLVMDIBuilderCreatePointerType(
287 d.ref,
288 t.Pointee.C,
289 C.uint64_t(t.SizeInBits),
290 C.uint64_t(t.AlignInBits),
291 name,
292 )
293 return Value{C: result}
294 }
295
296 // DISubroutineType holds the values for creating subroutine type debug metadata.
297 type DISubroutineType struct {
298 // File is the file in which the subroutine type is defined.
299 File Value
300
301 // Parameters contains the subroutine parameter types,
302 // including the return type at the 0th index.
303 Parameters []Value
304 }
305
306 // CreateSubroutineType creates subroutine type debug metadata.
307 func (d *DIBuilder) CreateSubroutineType(t DISubroutineType) Value {
308 params := d.getOrCreateTypeArray(t.Parameters)
309 result := C.LLVMDIBuilderCreateSubroutineType(d.ref, t.File.C, params.C)
310 return Value{C: result}
311 }
312
313 // DIStructType holds the values for creating struct type debug metadata.
314 type DIStructType struct {
315 Name string
316 File Value
317 Line int
318 SizeInBits uint64
319 AlignInBits uint64
320 Flags int
321 DerivedFrom Value
322 Elements []Value
323 }
324
325 // CreateStructType creates struct type debug metadata.
326 func (d *DIBuilder) CreateStructType(scope Value, t DIStructType) Value {
327 elements := d.getOrCreateArray(t.Elements)
328 name := C.CString(t.Name)
329 defer C.free(unsafe.Pointer(name))
330 result := C.LLVMDIBuilderCreateStructType(
331 d.ref,
332 scope.C,
333 name,
334 t.File.C,
335 C.unsigned(t.Line),
336 C.uint64_t(t.SizeInBits),
337 C.uint64_t(t.AlignInBits),
338 C.unsigned(t.Flags),
339 t.DerivedFrom.C,
340 elements.C,
341 )
342 return Value{C: result}
343 }
344
345 // DIMemberType holds the values for creating member type debug metadata.
346 type DIMemberType struct {
347 Name string
348 File Value
349 Line int
350 SizeInBits uint64
351 AlignInBits uint64
352 OffsetInBits uint64
353 Flags int
354 Type Value
355 }
356
357 // CreateMemberType creates struct type debug metadata.
358 func (d *DIBuilder) CreateMemberType(scope Value, t DIMemberType) Value {
359 name := C.CString(t.Name)
360 defer C.free(unsafe.Pointer(name))
361 result := C.LLVMDIBuilderCreateMemberType(
362 d.ref,
363 scope.C,
364 name,
365 t.File.C,
366 C.unsigned(t.Line),
367 C.uint64_t(t.SizeInBits),
368 C.uint64_t(t.AlignInBits),
369 C.uint64_t(t.OffsetInBits),
370 C.unsigned(t.Flags),
371 t.Type.C,
372 )
373 return Value{C: result}
374 }
375
376 // DISubrange describes an integer value range.
377 type DISubrange struct {
378 Lo int64
379 Count int64
380 }
381
382 // DIArrayType holds the values for creating array type debug metadata.
383 type DIArrayType struct {
384 SizeInBits uint64
385 AlignInBits uint64
386 ElementType Value
387 Subscripts []DISubrange
388 }
389
390 // CreateArrayType creates struct type debug metadata.
391 func (d *DIBuilder) CreateArrayType(t DIArrayType) Value {
392 subscriptsSlice := make([]Value, len(t.Subscripts))
393 for i, s := range t.Subscripts {
394 subscriptsSlice[i] = d.getOrCreateSubrange(s.Lo, s.Count)
395 }
396 subscripts := d.getOrCreateArray(subscriptsSlice)
397 result := C.LLVMDIBuilderCreateArrayType(
398 d.ref,
399 C.uint64_t(t.SizeInBits),
400 C.uint64_t(t.AlignInBits),
401 t.ElementType.C,
402 subscripts.C,
403 )
404 return Value{C: result}
405 }
406
407 // DITypedef holds the values for creating typedef type debug metadata.
408 type DITypedef struct {
409 Type Value
410 Name string
411 File Value
412 Line int
413 Context Value
414 }
415
416 // CreateTypedef creates typedef type debug metadata.
417 func (d *DIBuilder) CreateTypedef(t DITypedef) Value {
418 name := C.CString(t.Name)
419 defer C.free(unsafe.Pointer(name))
420 result := C.LLVMDIBuilderCreateTypedef(
421 d.ref,
422 t.Type.C,
423 name,
424 t.File.C,
425 C.unsigned(t.Line),
426 t.Context.C,
427 )
428 return Value{C: result}
429 }
430
431 // getOrCreateSubrange gets a metadata node for the specified subrange,
432 // creating if required.
433 func (d *DIBuilder) getOrCreateSubrange(lo, count int64) Value {
434 result := C.LLVMDIBuilderGetOrCreateSubrange(d.ref, C.int64_t(lo), C.int64_t(count))
435 return Value{C: result}
436 }
437
438 // getOrCreateArray gets a metadata node containing the specified values,
439 // creating if required.
440 func (d *DIBuilder) getOrCreateArray(values []Value) Value {
441 if len(values) == 0 {
442 return Value{}
443 }
444 var data *C.LLVMValueRef
445 length := len(values)
446 if length > 0 {
447 data = &values[0].C
448 }
449 result := C.LLVMDIBuilderGetOrCreateArray(d.ref, data, C.size_t(length))
450 return Value{C: result}
451 }
452
453 // getOrCreateTypeArray gets a metadata node for a type array containing the
454 // specified values, creating if required.
455 func (d *DIBuilder) getOrCreateTypeArray(values []Value) Value {
456 if len(values) == 0 {
457 return Value{}
458 }
459 var data *C.LLVMValueRef
460 length := len(values)
461 if length > 0 {
462 data = &values[0].C
463 }
464 result := C.LLVMDIBuilderGetOrCreateTypeArray(d.ref, data, C.size_t(length))
465 return Value{C: result}
466 }
467
468 // CreateExpression creates a new descriptor for the specified
469 // variable which has a complex address expression for its address.
470 func (d *DIBuilder) CreateExpression(addr []int64) Value {
471 var data *C.int64_t
472 if len(addr) > 0 {
473 data = (*C.int64_t)(unsafe.Pointer(&addr[0]))
474 }
475 result := C.LLVMDIBuilderCreateExpression(d.ref, data, C.size_t(len(addr)))
476 return Value{C: result}
477 }
478
479 // InsertDeclareAtEnd inserts a call to llvm.dbg.declare at the end of the
480 // specified basic block for the given value and associated debug metadata.
481 func (d *DIBuilder) InsertDeclareAtEnd(v, diVarInfo, expr Value, bb BasicBlock) Value {
482 result := C.LLVMDIBuilderInsertDeclareAtEnd(d.ref, v.C, diVarInfo.C, expr.C, bb.C)
483 return Value{C: result}
484 }
485
486 func boolToCInt(v bool) C.int {
487 if v {
488 return 1
489 }
490 return 0
491 }
0 //===- executionengine.go - Bindings for executionengine ------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines bindings for the executionengine component.
10 //
11 //===----------------------------------------------------------------------===//
12
13 package llvm
14
15 /*
16 #include "llvm-c/ExecutionEngine.h"
17 #include
18 */
19 import "C"
20 import "unsafe"
21 import "errors"
22
23 func LinkInMCJIT() { C.LLVMLinkInMCJIT() }
24 func LinkInInterpreter() { C.LLVMLinkInInterpreter() }
25
26 type GenericValue struct {
27 C C.LLVMGenericValueRef
28 }
29 type ExecutionEngine struct {
30 C C.LLVMExecutionEngineRef
31 }
32 type MCJITCompilerOptions struct {
33 OptLevel uint
34 CodeModel CodeModel
35 NoFramePointerElim bool
36 EnableFastISel bool
37 }
38
39 // helpers
40 func llvmGenericValueRefPtr(t *GenericValue) *C.LLVMGenericValueRef {
41 return (*C.LLVMGenericValueRef)(unsafe.Pointer(t))
42 }
43
44 //-------------------------------------------------------------------------
45 // llvm.GenericValue
46 //-------------------------------------------------------------------------
47
48 func NewGenericValueFromInt(t Type, n uint64, signed bool) (g GenericValue) {
49 g.C = C.LLVMCreateGenericValueOfInt(t.C, C.ulonglong(n), boolToLLVMBool(signed))
50 return
51 }
52 func NewGenericValueFromPointer(p unsafe.Pointer) (g GenericValue) {
53 g.C = C.LLVMCreateGenericValueOfPointer(p)
54 return
55 }
56 func NewGenericValueFromFloat(t Type, n float64) (g GenericValue) {
57 g.C = C.LLVMCreateGenericValueOfFloat(t.C, C.double(n))
58 return
59 }
60 func (g GenericValue) IntWidth() int { return int(C.LLVMGenericValueIntWidth(g.C)) }
61 func (g GenericValue) Int(signed bool) uint64 {
62 return uint64(C.LLVMGenericValueToInt(g.C, boolToLLVMBool(signed)))
63 }
64 func (g GenericValue) Float(t Type) float64 {
65 return float64(C.LLVMGenericValueToFloat(t.C, g.C))
66 }
67 func (g GenericValue) Pointer() unsafe.Pointer {
68 return C.LLVMGenericValueToPointer(g.C)
69 }
70 func (g GenericValue) Dispose() { C.LLVMDisposeGenericValue(g.C) }
71
72 //-------------------------------------------------------------------------
73 // llvm.ExecutionEngine
74 //-------------------------------------------------------------------------
75
76 func NewExecutionEngine(m Module) (ee ExecutionEngine, err error) {
77 var cmsg *C.char
78 fail := C.LLVMCreateExecutionEngineForModule(&ee.C, m.C, &cmsg)
79 if fail != 0 {
80 ee.C = nil
81 err = errors.New(C.GoString(cmsg))
82 C.LLVMDisposeMessage(cmsg)
83 }
84 return
85 }
86
87 func NewInterpreter(m Module) (ee ExecutionEngine, err error) {
88 var cmsg *C.char
89 fail := C.LLVMCreateInterpreterForModule(&ee.C, m.C, &cmsg)
90 if fail != 0 {
91 ee.C = nil
92 err = errors.New(C.GoString(cmsg))
93 C.LLVMDisposeMessage(cmsg)
94 }
95 return
96 }
97
98 func NewMCJITCompiler(m Module, options MCJITCompilerOptions) (ee ExecutionEngine, err error) {
99 var cmsg *C.char
100 copts := C.struct_LLVMMCJITCompilerOptions{
101 OptLevel: C.unsigned(options.OptLevel),
102 CodeModel: C.LLVMCodeModel(options.CodeModel),
103 NoFramePointerElim: boolToLLVMBool(options.NoFramePointerElim),
104 EnableFastISel: boolToLLVMBool(options.EnableFastISel),
105 }
106 fail := C.LLVMCreateMCJITCompilerForModule(&ee.C, m.C, &copts, C.size_t(unsafe.Sizeof(copts)), &cmsg)
107 if fail != 0 {
108 ee.C = nil
109 err = errors.New(C.GoString(cmsg))
110 C.LLVMDisposeMessage(cmsg)
111 }
112 return
113 }
114
115 func (ee ExecutionEngine) Dispose() { C.LLVMDisposeExecutionEngine(ee.C) }
116 func (ee ExecutionEngine) RunStaticConstructors() { C.LLVMRunStaticConstructors(ee.C) }
117 func (ee ExecutionEngine) RunStaticDestructors() { C.LLVMRunStaticDestructors(ee.C) }
118
119 func (ee ExecutionEngine) RunFunction(f Value, args []GenericValue) (g GenericValue) {
120 nargs := len(args)
121 var argptr *GenericValue
122 if nargs > 0 {
123 argptr = &args[0]
124 }
125 g.C = C.LLVMRunFunction(ee.C, f.C,
126 C.unsigned(nargs), llvmGenericValueRefPtr(argptr))
127 return
128 }
129
130 func (ee ExecutionEngine) FreeMachineCodeForFunction(f Value) {
131 C.LLVMFreeMachineCodeForFunction(ee.C, f.C)
132 }
133 func (ee ExecutionEngine) AddModule(m Module) { C.LLVMAddModule(ee.C, m.C) }
134
135 func (ee ExecutionEngine) RemoveModule(m Module) {
136 var modtmp C.LLVMModuleRef
137 C.LLVMRemoveModule(ee.C, m.C, &modtmp, nil)
138 }
139
140 func (ee ExecutionEngine) FindFunction(name string) (f Value) {
141 cname := C.CString(name)
142 defer C.free(unsafe.Pointer(cname))
143 C.LLVMFindFunction(ee.C, cname, &f.C)
144 return
145 }
146
147 func (ee ExecutionEngine) RecompileAndRelinkFunction(f Value) unsafe.Pointer {
148 return C.LLVMRecompileAndRelinkFunction(ee.C, f.C)
149 }
150
151 func (ee ExecutionEngine) TargetData() (td TargetData) {
152 td.C = C.LLVMGetExecutionEngineTargetData(ee.C)
153 return
154 }
155
156 func (ee ExecutionEngine) AddGlobalMapping(global Value, addr unsafe.Pointer) {
157 C.LLVMAddGlobalMapping(ee.C, global.C, addr)
158 }
159
160 func (ee ExecutionEngine) PointerToGlobal(global Value) unsafe.Pointer {
161 return C.LLVMGetPointerToGlobal(ee.C, global.C)
162 }
0 //===- executionengine_test.go - Tests for executionengine ----------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file tests bindings for the executionengine component.
10 //
11 //===----------------------------------------------------------------------===//
12
13 package llvm
14
15 import (
16 "testing"
17 )
18
19 func TestFactorial(t *testing.T) {
20 LinkInMCJIT()
21 InitializeNativeTarget()
22 InitializeNativeAsmPrinter()
23
24 mod := NewModule("fac_module")
25
26 fac_args := []Type{Int32Type()}
27 fac_type := FunctionType(Int32Type(), fac_args, false)
28 fac := AddFunction(mod, "fac", fac_type)
29 fac.SetFunctionCallConv(CCallConv)
30 n := fac.Param(0)
31
32 entry := AddBasicBlock(fac, "entry")
33 iftrue := AddBasicBlock(fac, "iftrue")
34 iffalse := AddBasicBlock(fac, "iffalse")
35 end := AddBasicBlock(fac, "end")
36
37 builder := NewBuilder()
38 defer builder.Dispose()
39
40 builder.SetInsertPointAtEnd(entry)
41 If := builder.CreateICmp(IntEQ, n, ConstInt(Int32Type(), 0, false), "cmptmp")
42 builder.CreateCondBr(If, iftrue, iffalse)
43
44 builder.SetInsertPointAtEnd(iftrue)
45 res_iftrue := ConstInt(Int32Type(), 1, false)
46 builder.CreateBr(end)
47
48 builder.SetInsertPointAtEnd(iffalse)
49 n_minus := builder.CreateSub(n, ConstInt(Int32Type(), 1, false), "subtmp")
50 call_fac_args := []Value{n_minus}
51 call_fac := builder.CreateCall(fac, call_fac_args, "calltmp")
52 res_iffalse := builder.CreateMul(n, call_fac, "multmp")
53 builder.CreateBr(end)
54
55 builder.SetInsertPointAtEnd(end)
56 res := builder.CreatePHI(Int32Type(), "result")
57 phi_vals := []Value{res_iftrue, res_iffalse}
58 phi_blocks := []BasicBlock{iftrue, iffalse}
59 res.AddIncoming(phi_vals, phi_blocks)
60 builder.CreateRet(res)
61
62 err := VerifyModule(mod, ReturnStatusAction)
63 if err != nil {
64 t.Errorf("Error verifying module: %s", err)
65 return
66 }
67
68 engine, err := NewMCJITCompiler(mod, MCJITCompilerOptions{OptLevel: 2})
69 if err != nil {
70 t.Errorf("Error creating JIT: %s", err)
71 return
72 }
73 defer engine.Dispose()
74
75 pass := NewPassManager()
76 defer pass.Dispose()
77
78 pass.Add(engine.TargetData())
79 pass.AddConstantPropagationPass()
80 pass.AddInstructionCombiningPass()
81 pass.AddPromoteMemoryToRegisterPass()
82 pass.AddGVNPass()
83 pass.AddCFGSimplificationPass()
84 pass.Run(mod)
85
86 exec_args := []GenericValue{NewGenericValueFromInt(Int32Type(), 10, false)}
87 exec_res := engine.RunFunction(fac, exec_args)
88 var fac10 uint64 = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
89 if exec_res.Int(false) != fac10 {
90 t.Errorf("Expected %d, got %d", fac10, exec_res.Int(false))
91 }
92 }
0 //===- ir.go - Bindings for ir --------------------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines bindings for the ir component.
10 //
11 //===----------------------------------------------------------------------===//
12
13 package llvm
14
15 /*
16 #include "llvm-c/Core.h"
17 #include "IRBindings.h"
18 #include
19 */
20 import "C"
21 import "unsafe"
22 import "errors"
23
24 type (
25 // We use these weird structs here because *Ref types are pointers and
26 // Go's spec says that a pointer cannot be used as a receiver base type.
27 Context struct {
28 C C.LLVMContextRef
29 }
30 Module struct {
31 C C.LLVMModuleRef
32 }
33 Type struct {
34 C C.LLVMTypeRef
35 }
36 Value struct {
37 C C.LLVMValueRef
38 }
39 BasicBlock struct {
40 C C.LLVMBasicBlockRef
41 }
42 Builder struct {
43 C C.LLVMBuilderRef
44 }
45 ModuleProvider struct {
46 C C.LLVMModuleProviderRef
47 }
48 MemoryBuffer struct {
49 C C.LLVMMemoryBufferRef
50 }
51 PassManager struct {
52 C C.LLVMPassManagerRef
53 }
54 Use struct {
55 C C.LLVMUseRef
56 }
57 Attribute uint64
58 Opcode C.LLVMOpcode
59 TypeKind C.LLVMTypeKind
60 Linkage C.LLVMLinkage
61 Visibility C.LLVMVisibility
62 CallConv C.LLVMCallConv
63 IntPredicate C.LLVMIntPredicate
64 FloatPredicate C.LLVMRealPredicate
65 LandingPadClause C.LLVMLandingPadClauseTy
66 )
67
68 func (c Context) IsNil() bool { return c.C == nil }
69 func (c Module) IsNil() bool { return c.C == nil }
70 func (c Type) IsNil() bool { return c.C == nil }
71 func (c Value) IsNil() bool { return c.C == nil }
72 func (c BasicBlock) IsNil() bool { return c.C == nil }
73 func (c Builder) IsNil() bool { return c.C == nil }
74 func (c ModuleProvider) IsNil() bool { return c.C == nil }
75 func (c MemoryBuffer) IsNil() bool { return c.C == nil }
76 func (c PassManager) IsNil() bool { return c.C == nil }
77 func (c Use) IsNil() bool { return c.C == nil }
78
79 // helpers
80 func llvmTypeRefPtr(t *Type) *C.LLVMTypeRef { return (*C.LLVMTypeRef)(unsafe.Pointer(t)) }
81 func llvmValueRefPtr(t *Value) *C.LLVMValueRef { return (*C.LLVMValueRef)(unsafe.Pointer(t)) }
82 func llvmBasicBlockRefPtr(t *BasicBlock) *C.LLVMBasicBlockRef {
83 return (*C.LLVMBasicBlockRef)(unsafe.Pointer(t))
84 }
85 func boolToLLVMBool(b bool) C.LLVMBool {
86 if b {
87 return C.LLVMBool(1)
88 }
89 return C.LLVMBool(0)
90 }
91
92 func llvmValueRefs(values []Value) (*C.LLVMValueRef, C.unsigned) {
93 var pt *C.LLVMValueRef
94 ptlen := C.unsigned(len(values))
95 if ptlen > 0 {
96 pt = llvmValueRefPtr(&values[0])
97 }
98 return pt, ptlen
99 }
100
101 //-------------------------------------------------------------------------
102 // llvm.Attribute
103 //-------------------------------------------------------------------------
104
105 const (
106 NoneAttribute Attribute = 0
107 ZExtAttribute Attribute = C.LLVMZExtAttribute
108 SExtAttribute Attribute = C.LLVMSExtAttribute
109 NoReturnAttribute Attribute = C.LLVMNoReturnAttribute
110 InRegAttribute Attribute = C.LLVMInRegAttribute
111 StructRetAttribute Attribute = C.LLVMStructRetAttribute
112 NoUnwindAttribute Attribute = C.LLVMNoUnwindAttribute
113 NoAliasAttribute Attribute = C.LLVMNoAliasAttribute
114 ByValAttribute Attribute = C.LLVMByValAttribute
115 NestAttribute Attribute = C.LLVMNestAttribute
116 ReadNoneAttribute Attribute = C.LLVMReadNoneAttribute
117 ReadOnlyAttribute Attribute = C.LLVMReadOnlyAttribute
118 NoInlineAttribute Attribute = C.LLVMNoInlineAttribute
119 AlwaysInlineAttribute Attribute = C.LLVMAlwaysInlineAttribute
120 OptimizeForSizeAttribute Attribute = C.LLVMOptimizeForSizeAttribute
121 StackProtectAttribute Attribute = C.LLVMStackProtectAttribute
122 StackProtectReqAttribute Attribute = C.LLVMStackProtectReqAttribute
123 Alignment Attribute = C.LLVMAlignment
124 NoCaptureAttribute Attribute = C.LLVMNoCaptureAttribute
125 NoRedZoneAttribute Attribute = C.LLVMNoRedZoneAttribute
126 NoImplicitFloatAttribute Attribute = C.LLVMNoImplicitFloatAttribute
127 NakedAttribute Attribute = C.LLVMNakedAttribute
128 InlineHintAttribute Attribute = C.LLVMInlineHintAttribute
129 StackAlignment Attribute = C.LLVMStackAlignment
130 ReturnsTwiceAttribute Attribute = C.LLVMReturnsTwice
131 UWTableAttribute Attribute = C.LLVMUWTable
132 NonLazyBindAttribute Attribute = 1 << 31
133 SanitizeAddressAttribute Attribute = 1 << 32
134 MinSizeAttribute Attribute = 1 << 33
135 NoDuplicateAttribute Attribute = 1 << 34
136 StackProtectStrongAttribute Attribute = 1 << 35
137 SanitizeThreadAttribute Attribute = 1 << 36
138 SanitizeMemoryAttribute Attribute = 1 << 37
139 NoBuiltinAttribute Attribute = 1 << 38
140 ReturnedAttribute Attribute = 1 << 39
141 ColdAttribute Attribute = 1 << 40
142 BuiltinAttribute Attribute = 1 << 41
143 OptimizeNoneAttribute Attribute = 1 << 42
144 InAllocaAttribute Attribute = 1 << 43
145 NonNullAttribute Attribute = 1 << 44
146 JumpTableAttribute Attribute = 1 << 45
147 )
148
149 //-------------------------------------------------------------------------
150 // llvm.Opcode
151 //-------------------------------------------------------------------------
152
153 const (
154 Ret Opcode = C.LLVMRet
155 Br Opcode = C.LLVMBr
156 Switch Opcode = C.LLVMSwitch
157 IndirectBr Opcode = C.LLVMIndirectBr
158 Invoke Opcode = C.LLVMInvoke
159 Unreachable Opcode = C.LLVMUnreachable
160
161 // Standard Binary Operators
162 Add Opcode = C.LLVMAdd
163 FAdd Opcode = C.LLVMFAdd
164 Sub Opcode = C.LLVMSub
165 FSub Opcode = C.LLVMFSub
166 Mul Opcode = C.LLVMMul
167 FMul Opcode = C.LLVMFMul
168 UDiv Opcode = C.LLVMUDiv
169 SDiv Opcode = C.LLVMSDiv
170 FDiv Opcode = C.LLVMFDiv
171 URem Opcode = C.LLVMURem
172 SRem Opcode = C.LLVMSRem
173 FRem Opcode = C.LLVMFRem
174
175 // Logical Operators
176 Shl Opcode = C.LLVMShl
177 LShr Opcode = C.LLVMLShr
178 AShr Opcode = C.LLVMAShr
179 And Opcode = C.LLVMAnd
180 Or Opcode = C.LLVMOr
181 Xor Opcode = C.LLVMXor
182
183 // Memory Operators
184 Alloca Opcode = C.LLVMAlloca
185 Load Opcode = C.LLVMLoad
186 Store Opcode = C.LLVMStore
187 GetElementPtr Opcode = C.LLVMGetElementPtr
188
189 // Cast Operators
190 Trunc Opcode = C.LLVMTrunc
191 ZExt Opcode = C.LLVMZExt
192 SExt Opcode = C.LLVMSExt
193 FPToUI Opcode = C.LLVMFPToUI
194 FPToSI Opcode = C.LLVMFPToSI
195 UIToFP Opcode = C.LLVMUIToFP
196 SIToFP Opcode = C.LLVMSIToFP
197 FPTrunc Opcode = C.LLVMFPTrunc
198 FPExt Opcode = C.LLVMFPExt
199 PtrToInt Opcode = C.LLVMPtrToInt
200 IntToPtr Opcode = C.LLVMIntToPtr
201 BitCast Opcode = C.LLVMBitCast
202
203 // Other Operators
204 ICmp Opcode = C.LLVMICmp
205 FCmp Opcode = C.LLVMFCmp
206 PHI Opcode = C.LLVMPHI
207 Call Opcode = C.LLVMCall
208 Select Opcode = C.LLVMSelect
209 // UserOp1
210 // UserOp2
211 VAArg Opcode = C.LLVMVAArg
212 ExtractElement Opcode = C.LLVMExtractElement
213 InsertElement Opcode = C.LLVMInsertElement
214 ShuffleVector Opcode = C.LLVMShuffleVector
215 ExtractValue Opcode = C.LLVMExtractValue
216 InsertValue Opcode = C.LLVMInsertValue
217 )
218
219 //-------------------------------------------------------------------------
220 // llvm.TypeKind
221 //-------------------------------------------------------------------------
222
223 const (
224 VoidTypeKind TypeKind = C.LLVMVoidTypeKind
225 FloatTypeKind TypeKind = C.LLVMFloatTypeKind
226 DoubleTypeKind TypeKind = C.LLVMDoubleTypeKind
227 X86_FP80TypeKind TypeKind = C.LLVMX86_FP80TypeKind
228 FP128TypeKind TypeKind = C.LLVMFP128TypeKind
229 PPC_FP128TypeKind TypeKind = C.LLVMPPC_FP128TypeKind
230 LabelTypeKind TypeKind = C.LLVMLabelTypeKind
231 IntegerTypeKind TypeKind = C.LLVMIntegerTypeKind
232 FunctionTypeKind TypeKind = C.LLVMFunctionTypeKind
233 StructTypeKind TypeKind = C.LLVMStructTypeKind
234 ArrayTypeKind TypeKind = C.LLVMArrayTypeKind
235 PointerTypeKind TypeKind = C.LLVMPointerTypeKind
236 VectorTypeKind TypeKind = C.LLVMVectorTypeKind
237 MetadataTypeKind TypeKind = C.LLVMMetadataTypeKind
238 )
239
240 //-------------------------------------------------------------------------
241 // llvm.Linkage
242 //-------------------------------------------------------------------------
243
244 const (
245 ExternalLinkage Linkage = C.LLVMExternalLinkage
246 AvailableExternallyLinkage Linkage = C.LLVMAvailableExternallyLinkage
247 LinkOnceAnyLinkage Linkage = C.LLVMLinkOnceAnyLinkage
248 LinkOnceODRLinkage Linkage = C.LLVMLinkOnceODRLinkage
249 WeakAnyLinkage Linkage = C.LLVMWeakAnyLinkage
250 WeakODRLinkage Linkage = C.LLVMWeakODRLinkage
251 AppendingLinkage Linkage = C.LLVMAppendingLinkage
252 InternalLinkage Linkage = C.LLVMInternalLinkage
253 PrivateLinkage Linkage = C.LLVMPrivateLinkage
254 ExternalWeakLinkage Linkage = C.LLVMExternalWeakLinkage
255 CommonLinkage Linkage = C.LLVMCommonLinkage
256 )
257
258 //-------------------------------------------------------------------------
259 // llvm.Visibility
260 //-------------------------------------------------------------------------
261
262 const (
263 DefaultVisibility Visibility = C.LLVMDefaultVisibility
264 HiddenVisibility Visibility = C.LLVMHiddenVisibility
265 ProtectedVisibility Visibility = C.LLVMProtectedVisibility
266 )
267
268 //-------------------------------------------------------------------------
269 // llvm.CallConv
270 //-------------------------------------------------------------------------
271
272 const (
273 CCallConv CallConv = C.LLVMCCallConv
274 FastCallConv CallConv = C.LLVMFastCallConv
275 ColdCallConv CallConv = C.LLVMColdCallConv
276 X86StdcallCallConv CallConv = C.LLVMX86StdcallCallConv
277 X86FastcallCallConv CallConv = C.LLVMX86FastcallCallConv
278 )
279
280 //-------------------------------------------------------------------------
281 // llvm.IntPredicate
282 //-------------------------------------------------------------------------
283
284 const (
285 IntEQ IntPredicate = C.LLVMIntEQ
286 IntNE IntPredicate = C.LLVMIntNE
287 IntUGT IntPredicate = C.LLVMIntUGT
288 IntUGE IntPredicate = C.LLVMIntUGE
289 IntULT IntPredicate = C.LLVMIntULT
290 IntULE IntPredicate = C.LLVMIntULE
291 IntSGT IntPredicate = C.LLVMIntSGT
292 IntSGE IntPredicate = C.LLVMIntSGE
293 IntSLT IntPredicate = C.LLVMIntSLT
294 IntSLE IntPredicate = C.LLVMIntSLE
295 )
296
297 //-------------------------------------------------------------------------
298 // llvm.FloatPredicate
299 //-------------------------------------------------------------------------
300
301 const (
302 FloatPredicateFalse FloatPredicate = C.LLVMRealPredicateFalse
303 FloatOEQ FloatPredicate = C.LLVMRealOEQ
304 FloatOGT FloatPredicate = C.LLVMRealOGT
305 FloatOGE FloatPredicate = C.LLVMRealOGE
306 FloatOLT FloatPredicate = C.LLVMRealOLT
307 FloatOLE FloatPredicate = C.LLVMRealOLE
308 FloatONE FloatPredicate = C.LLVMRealONE
309 FloatORD FloatPredicate = C.LLVMRealORD
310 FloatUNO FloatPredicate = C.LLVMRealUNO
311 FloatUEQ FloatPredicate = C.LLVMRealUEQ
312 FloatUGT FloatPredicate = C.LLVMRealUGT
313 FloatUGE FloatPredicate = C.LLVMRealUGE
314 FloatULT FloatPredicate = C.LLVMRealULT
315 FloatULE FloatPredicate = C.LLVMRealULE
316 FloatUNE FloatPredicate = C.LLVMRealUNE
317 FloatPredicateTrue FloatPredicate = C.LLVMRealPredicateTrue
318 )
319
320 //-------------------------------------------------------------------------
321 // llvm.LandingPadClause
322 //-------------------------------------------------------------------------
323
324 const (
325 LandingPadCatch LandingPadClause = C.LLVMLandingPadCatch
326 LandingPadFilter LandingPadClause = C.LLVMLandingPadFilter
327 )
328
329 //-------------------------------------------------------------------------
330 // llvm.Context
331 //-------------------------------------------------------------------------
332
333 func NewContext() Context { return Context{C.LLVMContextCreate()} }
334 func GlobalContext() Context { return Context{C.LLVMGetGlobalContext()} }
335 func (c Context) Dispose() { C.LLVMContextDispose(c.C) }
336
337 func (c Context) MDKindID(name string) (id int) {
338 cname := C.CString(name)
339 defer C.free(unsafe.Pointer(cname))
340 id = int(C.LLVMGetMDKindIDInContext(c.C, cname, C.unsigned(len(name))))
341 return
342 }
343
344 func MDKindID(name string) (id int) {
345 cname := C.CString(name)
346 defer C.free(unsafe.Pointer(cname))
347 id = int(C.LLVMGetMDKindID(cname, C.unsigned(len(name))))
348 return
349 }
350
351 //-------------------------------------------------------------------------
352 // llvm.Module
353 //-------------------------------------------------------------------------
354
355 // Create and destroy modules.
356 // See llvm::Module::Module.
357 func NewModule(name string) (m Module) {
358 cname := C.CString(name)
359 defer C.free(unsafe.Pointer(cname))
360 m.C = C.LLVMModuleCreateWithName(cname)
361 return
362 }
363
364 func (c Context) NewModule(name string) (m Module) {
365 cname := C.CString(name)
366 defer C.free(unsafe.Pointer(cname))
367 m.C = C.LLVMModuleCreateWithNameInContext(cname, c.C)
368 return
369 }
370
371 // See llvm::Module::~Module
372 func (m Module) Dispose() { C.LLVMDisposeModule(m.C) }
373
374 // Data layout. See Module::getDataLayout.
375 func (m Module) DataLayout() string {
376 clayout := C.LLVMGetDataLayout(m.C)
377 return C.GoString(clayout)
378 }
379
380 func (m Module) SetDataLayout(layout string) {
381 clayout := C.CString(layout)
382 defer C.free(unsafe.Pointer(clayout))
383 C.LLVMSetDataLayout(m.C, clayout)
384 }
385
386 // Target triple. See Module::getTargetTriple.
387 func (m Module) Target() string {
388 ctarget := C.LLVMGetTarget(m.C)
389 return C.GoString(ctarget)
390 }
391 func (m Module) SetTarget(target string) {
392 ctarget := C.CString(target)
393 defer C.free(unsafe.Pointer(ctarget))
394 C.LLVMSetTarget(m.C, ctarget)
395 }
396
397 func (m Module) GetTypeByName(name string) (t Type) {
398 cname := C.CString(name)
399 defer C.free(unsafe.Pointer(cname))
400 t.C = C.LLVMGetTypeByName(m.C, cname)
401 return
402 }
403
404 // See Module::dump.
405 func (m Module) Dump() {
406 C.LLVMDumpModule(m.C)
407 }
408
409 func (m Module) String() string {
410 cir := C.LLVMPrintModuleToString(m.C)
411 defer C.free(unsafe.Pointer(cir))
412 ir := C.GoString(cir)
413 return ir
414 }
415
416 // See Module::setModuleInlineAsm.
417 func (m Module) SetInlineAsm(asm string) {
418 casm := C.CString(asm)
419 defer C.free(unsafe.Pointer(casm))
420 C.LLVMSetModuleInlineAsm(m.C, casm)
421 }
422
423 func (m Module) AddNamedMetadataOperand(name string, operand Value) {
424 cname := C.CString(name)
425 defer C.free(unsafe.Pointer(cname))
426 C.LLVMAddNamedMetadataOperand(m.C, cname, operand.C)
427 }
428
429 func (m Module) Context() (c Context) {
430 c.C = C.LLVMGetModuleContext(m.C)
431 return
432 }
433
434 //-------------------------------------------------------------------------
435 // llvm.Type
436 //-------------------------------------------------------------------------
437
438 // LLVM types conform to the following hierarchy:
439 //
440 // types:
441 // integer type
442 // real type
443 // function type
444 // sequence types:
445 // array type
446 // pointer type
447 // vector type
448 // void type
449 // label type
450 // opaque type
451
452 // See llvm::LLVMTypeKind::getTypeID.
453 func (t Type) TypeKind() TypeKind { return TypeKind(C.LLVMGetTypeKind(t.C)) }
454
455 // See llvm::LLVMType::getContext.
456 func (t Type) Context() (c Context) {
457 c.C = C.LLVMGetTypeContext(t.C)
458 return
459 }
460
461 // Operations on integer types
462 func (c Context) Int1Type() (t Type) { t.C = C.LLVMInt1TypeInContext(c.C); return }
463 func (c Context) Int8Type() (t Type) { t.C = C.LLVMInt8TypeInContext(c.C); return }
464 func (c Context) Int16Type() (t Type) { t.C = C.LLVMInt16TypeInContext(c.C); return }
465 func (c Context) Int32Type() (t Type) { t.C = C.LLVMInt32TypeInContext(c.C); return }
466 func (c Context) Int64Type() (t Type) { t.C = C.LLVMInt64TypeInContext(c.C); return }
467 func (c Context) IntType(numbits int) (t Type) {
468 t.C = C.LLVMIntTypeInContext(c.C, C.unsigned(numbits))
469 return
470 }
471
472 func Int1Type() (t Type) { t.C = C.LLVMInt1Type(); return }
473 func Int8Type() (t Type) { t.C = C.LLVMInt8Type(); return }
474 func Int16Type() (t Type) { t.C = C.LLVMInt16Type(); return }
475 func Int32Type() (t Type) { t.C = C.LLVMInt32Type(); return }
476 func Int64Type() (t Type) { t.C = C.LLVMInt64Type(); return }
477
478 func IntType(numbits int) (t Type) {
479 t.C = C.LLVMIntType(C.unsigned(numbits))
480 return
481 }
482
483 func (t Type) IntTypeWidth() int {
484 return int(C.LLVMGetIntTypeWidth(t.C))
485 }
486
487 // Operations on real types
488 func (c Context) FloatType() (t Type) { t.C = C.LLVMFloatTypeInContext(c.C); return }
489 func (c Context) DoubleType() (t Type) { t.C = C.LLVMDoubleTypeInContext(c.C); return }
490 func (c Context) X86FP80Type() (t Type) { t.C = C.LLVMX86FP80TypeInContext(c.C); return }
491 func (c Context) FP128Type() (t Type) { t.C = C.LLVMFP128TypeInContext(c.C); return }
492 func (c Context) PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128TypeInContext(c.C); return }
493
494 func FloatType() (t Type) { t.C = C.LLVMFloatType(); return }
495 func DoubleType() (t Type) { t.C = C.LLVMDoubleType(); return }
496 func X86FP80Type() (t Type) { t.C = C.LLVMX86FP80Type(); return }
497 func FP128Type() (t Type) { t.C = C.LLVMFP128Type(); return }
498 func PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128Type(); return }
499
500 // Operations on function types
501 func FunctionType(returnType Type, paramTypes []Type, isVarArg bool) (t Type) {
502 var pt *C.LLVMTypeRef
503 var ptlen C.unsigned
504 if len(paramTypes) > 0 {
505 pt = llvmTypeRefPtr(¶mTypes[0])
506 ptlen = C.unsigned(len(paramTypes))
507 }
508 t.C = C.LLVMFunctionType(returnType.C,
509 pt,
510 ptlen,
511 boolToLLVMBool(isVarArg))
512 return
513 }
514
515 func (t Type) IsFunctionVarArg() bool { return C.LLVMIsFunctionVarArg(t.C) != 0 }
516 func (t Type) ReturnType() (rt Type) { rt.C = C.LLVMGetReturnType(t.C); return }
517 func (t Type) ParamTypesCount() int { return int(C.LLVMCountParamTypes(t.C)) }
518 func (t Type) ParamTypes() []Type {
519 count := t.ParamTypesCount()
520 if count > 0 {
521 out := make([]Type, count)
522 C.LLVMGetParamTypes(t.C, llvmTypeRefPtr(&out[0]))
523 return out
524 }
525 return nil
526 }
527
528 // Operations on struct types
529 func (c Context) StructType(elementTypes []Type, packed bool) (t Type) {
530 var pt *C.LLVMTypeRef
531 var ptlen C.unsigned
532 if len(elementTypes) > 0 {
533 pt = llvmTypeRefPtr(&elementTypes[0])
534 ptlen = C.unsigned(len(elementTypes))
535 }
536 t.C = C.LLVMStructTypeInContext(c.C,
537 pt,
538 ptlen,
539 boolToLLVMBool(packed))
540 return
541 }
542
543 func StructType(elementTypes []Type, packed bool) (t Type) {
544 var pt *C.LLVMTypeRef
545 var ptlen C.unsigned
546 if len(elementTypes) > 0 {
547 pt = llvmTypeRefPtr(&elementTypes[0])
548 ptlen = C.unsigned(len(elementTypes))
549 }
550 t.C = C.LLVMStructType(pt, ptlen, boolToLLVMBool(packed))
551 return
552 }
553
554 func (c Context) StructCreateNamed(name string) (t Type) {
555 cname := C.CString(name)
556 defer C.free(unsafe.Pointer(cname))
557 t.C = C.LLVMStructCreateNamed(c.C, cname)
558 return
559 }
560
561 func (t Type) StructName() string {
562 return C.GoString(C.LLVMGetStructName(t.C))
563 }
564
565 func (t Type) StructSetBody(elementTypes []Type, packed bool) {
566 var pt *C.LLVMTypeRef
567 var ptlen C.unsigned
568 if len(elementTypes) > 0 {
569 pt = llvmTypeRefPtr(&elementTypes[0])
570 ptlen = C.unsigned(len(elementTypes))
571 }
572 C.LLVMStructSetBody(t.C, pt, ptlen, boolToLLVMBool(packed))
573 }
574
575 func (t Type) IsStructPacked() bool { return C.LLVMIsPackedStruct(t.C) != 0 }
576 func (t Type) StructElementTypesCount() int { return int(C.LLVMCountStructElementTypes(t.C)) }
577 func (t Type) StructElementTypes() []Type {
578 out := make([]Type, t.StructElementTypesCount())
579 if len(out) > 0 {
580 C.LLVMGetStructElementTypes(t.C, llvmTypeRefPtr(&out[0]))
581 }
582 return out
583 }
584
585 // Operations on array, pointer, and vector types (sequence types)
586 func ArrayType(elementType Type, elementCount int) (t Type) {
587 t.C = C.LLVMArrayType(elementType.C, C.unsigned(elementCount))
588 return
589 }
590 func PointerType(elementType Type, addressSpace int) (t Type) {
591 t.C = C.LLVMPointerType(elementType.C, C.unsigned(addressSpace))
592 return
593 }
594 func VectorType(elementType Type, elementCount int) (t Type) {
595 t.C = C.LLVMVectorType(elementType.C, C.unsigned(elementCount))
596 return
597 }
598
599 func (t Type) ElementType() (rt Type) { rt.C = C.LLVMGetElementType(t.C); return }
600 func (t Type) ArrayLength() int { return int(C.LLVMGetArrayLength(t.C)) }
601 func (t Type) PointerAddressSpace() int { return int(C.LLVMGetPointerAddressSpace(t.C)) }
602 func (t Type) VectorSize() int { return int(C.LLVMGetVectorSize(t.C)) }
603
604 // Operations on other types
605 func (c Context) VoidType() (t Type) { t.C = C.LLVMVoidTypeInContext(c.C); return }
606 func (c Context) LabelType() (t Type) { t.C = C.LLVMLabelTypeInContext(c.C); return }
607
608 func VoidType() (t Type) { t.C = C.LLVMVoidType(); return }
609 func LabelType() (t Type) { t.C = C.LLVMLabelType(); return }
610
611 //-------------------------------------------------------------------------
612 // llvm.Value
613 //-------------------------------------------------------------------------
614
615 // Operations on all values
616 func (v Value) Type() (t Type) { t.C = C.LLVMTypeOf(v.C); return }
617 func (v Value) Name() string { return C.GoString(C.LLVMGetValueName(v.C)) }
618 func (v Value) SetName(name string) {
619 cname := C.CString(name)
620 defer C.free(unsafe.Pointer(cname))
621 C.LLVMSetValueName(v.C, cname)
622 }
623 func (v Value) Dump() { C.LLVMDumpValue(v.C) }
624 func (v Value) ReplaceAllUsesWith(nv Value) { C.LLVMReplaceAllUsesWith(v.C, nv.C) }
625 func (v Value) HasMetadata() bool { return C.LLVMHasMetadata(v.C) != 0 }
626 func (v Value) Metadata(kind int) (rv Value) {
627 rv.C = C.LLVMGetMetadata(v.C, C.unsigned(kind))
628 return
629 }
630 func (v Value) SetMetadata(kind int, node Value) {
631 C.LLVMSetMetadata(v.C, C.unsigned(kind), node.C)
632 }
633
634 // Conversion functions.
635 // Return the input value if it is an instance of the specified class, otherwise NULL.
636 // See llvm::dyn_cast_or_null<>.
637 func (v Value) IsAArgument() (rv Value) { rv.C = C.LLVMIsAArgument(v.C); return }
638 func (v Value) IsABasicBlock() (rv Value) { rv.C = C.LLVMIsABasicBlock(v.C); return }
639 func (v Value) IsAInlineAsm() (rv Value) { rv.C = C.LLVMIsAInlineAsm(v.C); return }
640 func (v Value) IsAUser() (rv Value) { rv.C = C.LLVMIsAUser(v.C); return }
641 func (v Value) IsAConstant() (rv Value) { rv.C = C.LLVMIsAConstant(v.C); return }
642 func (v Value) IsAConstantAggregateZero() (rv Value) {
643 rv.C = C.LLVMIsAConstantAggregateZero(v.C)
644 return
645 }
646 func (v Value) IsAConstantArray() (rv Value) { rv.C = C.LLVMIsAConstantArray(v.C); return }
647 func (v Value) IsAConstantExpr() (rv Value) { rv.C = C.LLVMIsAConstantExpr(v.C); return }
648 func (v Value) IsAConstantFP() (rv Value) { rv.C = C.LLVMIsAConstantFP(v.C); return }
649 func (v Value) IsAConstantInt() (rv Value) { rv.C = C.LLVMIsAConstantInt(v.C); return }
650 func (v Value) IsAConstantPointerNull() (rv Value) { rv.C = C.LLVMIsAConstantPointerNull(v.C); return }
651 func (v Value) IsAConstantStruct() (rv Value) { rv.C = C.LLVMIsAConstantStruct(v.C); return }
652 func (v Value) IsAConstantVector() (rv Value) { rv.C = C.LLVMIsAConstantVector(v.C); return }
653 func (v Value) IsAGlobalValue() (rv Value) { rv.C = C.LLVMIsAGlobalValue(v.C); return }
654 func (v Value) IsAFunction() (rv Value) { rv.C = C.LLVMIsAFunction(v.C); return }
655 func (v Value) IsAGlobalAlias() (rv Value) { rv.C = C.LLVMIsAGlobalAlias(v.C); return }
656 func (v Value) IsAGlobalVariable() (rv Value) { rv.C = C.LLVMIsAGlobalVariable(v.C); return }
657 func (v Value) IsAUndefValue() (rv Value) { rv.C = C.LLVMIsAUndefValue(v.C); return }
658 func (v Value) IsAInstruction() (rv Value) { rv.C = C.LLVMIsAInstruction(v.C); return }
659 func (v Value) IsABinaryOperator() (rv Value) { rv.C = C.LLVMIsABinaryOperator(v.C); return }
660 func (v Value) IsACallInst() (rv Value) { rv.C = C.LLVMIsACallInst(v.C); return }
661 func (v Value) IsAIntrinsicInst() (rv Value) { rv.C = C.LLVMIsAIntrinsicInst(v.C); return }
662 func (v Value) IsADbgInfoIntrinsic() (rv Value) { rv.C = C.LLVMIsADbgInfoIntrinsic(v.C); return }
663 func (v Value) IsADbgDeclareInst() (rv Value) { rv.C = C.LLVMIsADbgDeclareInst(v.C); return }
664 func (v Value) IsAMemIntrinsic() (rv Value) { rv.C = C.LLVMIsAMemIntrinsic(v.C); return }
665 func (v Value) IsAMemCpyInst() (rv Value) { rv.C = C.LLVMIsAMemCpyInst(v.C); return }
666 func (v Value) IsAMemMoveInst() (rv Value) { rv.C = C.LLVMIsAMemMoveInst(v.C); return }
667 func (v Value) IsAMemSetInst() (rv Value) { rv.C = C.LLVMIsAMemSetInst(v.C); return }
668 func (v Value) IsACmpInst() (rv Value) { rv.C = C.LLVMIsACmpInst(v.C); return }
669 func (v Value) IsAFCmpInst() (rv Value) { rv.C = C.LLVMIsAFCmpInst(v.C); return }
670 func (v Value) IsAICmpInst() (rv Value) { rv.C = C.LLVMIsAICmpInst(v.C); return }
671 func (v Value) IsAExtractElementInst() (rv Value) { rv.C = C.LLVMIsAExtractElementInst(v.C); return }
672 func (v Value) IsAGetElementPtrInst() (rv Value) { rv.C = C.LLVMIsAGetElementPtrInst(v.C); return }
673 func (v Value) IsAInsertElementInst() (rv Value) { rv.C = C.LLVMIsAInsertElementInst(v.C); return }
674 func (v Value) IsAInsertValueInst() (rv Value) { rv.C = C.LLVMIsAInsertValueInst(v.C); return }
675 func (v Value) IsAPHINode() (rv Value) { rv.C = C.LLVMIsAPHINode(v.C); return }
676 func (v Value) IsASelectInst() (rv Value) { rv.C = C.LLVMIsASelectInst(v.C); return }
677 func (v Value) IsAShuffleVectorInst() (rv Value) { rv.C = C.LLVMIsAShuffleVectorInst(v.C); return }
678 func (v Value) IsAStoreInst() (rv Value) { rv.C = C.LLVMIsAStoreInst(v.C); return }
679 func (v Value) IsATerminatorInst() (rv Value) { rv.C = C.LLVMIsATerminatorInst(v.C); return }
680 func (v Value) IsABranchInst() (rv Value) { rv.C = C.LLVMIsABranchInst(v.C); return }
681 func (v Value) IsAInvokeInst() (rv Value) { rv.C = C.LLVMIsAInvokeInst(v.C); return }
682 func (v Value) IsAReturnInst() (rv Value) { rv.C = C.LLVMIsAReturnInst(v.C); return }
683 func (v Value) IsASwitchInst() (rv Value) { rv.C = C.LLVMIsASwitchInst(v.C); return }
684 func (v Value) IsAUnreachableInst() (rv Value) { rv.C = C.LLVMIsAUnreachableInst(v.C); return }
685 func (v Value) IsAUnaryInstruction() (rv Value) { rv.C = C.LLVMIsAUnaryInstruction(v.C); return }
686 func (v Value) IsAAllocaInst() (rv Value) { rv.C = C.LLVMIsAAllocaInst(v.C); return }
687 func (v Value) IsACastInst() (rv Value) { rv.C = C.LLVMIsACastInst(v.C); return }
688 func (v Value) IsABitCastInst() (rv Value) { rv.C = C.LLVMIsABitCastInst(v.C); return }
689 func (v Value) IsAFPExtInst() (rv Value) { rv.C = C.LLVMIsAFPExtInst(v.C); return }
690 func (v Value) IsAFPToSIInst() (rv Value) { rv.C = C.LLVMIsAFPToSIInst(v.C); return }
691 func (v Value) IsAFPToUIInst() (rv Value) { rv.C = C.LLVMIsAFPToUIInst(v.C); return }
692 func (v Value) IsAFPTruncInst() (rv Value) { rv.C = C.LLVMIsAFPTruncInst(v.C); return }
693 func (v Value) IsAIntToPtrInst() (rv Value) { rv.C = C.LLVMIsAIntToPtrInst(v.C); return }
694 func (v Value) IsAPtrToIntInst() (rv Value) { rv.C = C.LLVMIsAPtrToIntInst(v.C); return }
695 func (v Value) IsASExtInst() (rv Value) { rv.C = C.LLVMIsASExtInst(v.C); return }
696 func (v Value) IsASIToFPInst() (rv Value) { rv.C = C.LLVMIsASIToFPInst(v.C); return }
697 func (v Value) IsATruncInst() (rv Value) { rv.C = C.LLVMIsATruncInst(v.C); return }
698 func (v Value) IsAUIToFPInst() (rv Value) { rv.C = C.LLVMIsAUIToFPInst(v.C); return }
699 func (v Value) IsAZExtInst() (rv Value) { rv.C = C.LLVMIsAZExtInst(v.C); return }
700 func (v Value) IsAExtractValueInst() (rv Value) { rv.C = C.LLVMIsAExtractValueInst(v.C); return }
701 func (v Value) IsALoadInst() (rv Value) { rv.C = C.LLVMIsALoadInst(v.C); return }
702 func (v Value) IsAVAArgInst() (rv Value) { rv.C = C.LLVMIsAVAArgInst(v.C); return }
703
704 // Operations on Uses
705 func (v Value) FirstUse() (u Use) { u.C = C.LLVMGetFirstUse(v.C); return }
706 func (u Use) NextUse() (ru Use) { ru.C = C.LLVMGetNextUse(u.C); return }
707 func (u Use) User() (v Value) { v.C = C.LLVMGetUser(u.C); return }
708 func (u Use) UsedValue() (v Value) { v.C = C.LLVMGetUsedValue(u.C); return }
709
710 // Operations on Users
711 func (v Value) Operand(i int) (rv Value) { rv.C = C.LLVMGetOperand(v.C, C.unsigned(i)); return }
712 func (v Value) SetOperand(i int, op Value) { C.LLVMSetOperand(v.C, C.unsigned(i), op.C) }
713 func (v Value) OperandsCount() int { return int(C.LLVMGetNumOperands(v.C)) }
714
715 // Operations on constants of any type
716 func ConstNull(t Type) (v Value) { v.C = C.LLVMConstNull(t.C); return }
717 func ConstAllOnes(t Type) (v Value) { v.C = C.LLVMConstAllOnes(t.C); return }
718 func Undef(t Type) (v Value) { v.C = C.LLVMGetUndef(t.C); return }
719 func (v Value) IsConstant() bool { return C.LLVMIsConstant(v.C) != 0 }
720 func (v Value) IsNull() bool { return C.LLVMIsNull(v.C) != 0 }
721 func (v Value) IsUndef() bool { return C.LLVMIsUndef(v.C) != 0 }
722 func ConstPointerNull(t Type) (v Value) { v.C = C.LLVMConstPointerNull(t.C); return }
723
724 // Operations on metadata
725 func (c Context) MDString(str string) (v Value) {
726 cstr := C.CString(str)
727 defer C.free(unsafe.Pointer(cstr))
728 v.C = C.LLVMMDStringInContext(c.C, cstr, C.unsigned(len(str)))
729 return
730 }
731 func MDString(str string) (v Value) {
732 cstr := C.CString(str)
733 defer C.free(unsafe.Pointer(cstr))
734 v.C = C.LLVMMDString(cstr, C.unsigned(len(str)))
735 return
736 }
737 func (c Context) MDNode(vals []Value) (v Value) {
738 ptr, nvals := llvmValueRefs(vals)
739 v.C = C.LLVMMDNodeInContext(c.C, ptr, nvals)
740 return
741 }
742 func MDNode(vals []Value) (v Value) {
743 ptr, nvals := llvmValueRefs(vals)
744 v.C = C.LLVMMDNode(ptr, nvals)
745 return
746 }
747
748 // Operations on scalar constants
749 func ConstInt(t Type, n uint64, signExtend bool) (v Value) {
750 v.C = C.LLVMConstInt(t.C,
751 C.ulonglong(n),
752 boolToLLVMBool(signExtend))
753 return
754 }
755 func ConstIntFromString(t Type, str string, radix int) (v Value) {
756 cstr := C.CString(str)
757 defer C.free(unsafe.Pointer(cstr))
758 v.C = C.LLVMConstIntOfString(t.C, cstr, C.uint8_t(radix))
759 return
760 }
761 func ConstFloat(t Type, n float64) (v Value) {
762 v.C = C.LLVMConstReal(t.C, C.double(n))
763 return
764 }
765 func ConstFloatFromString(t Type, str string) (v Value) {
766 cstr := C.CString(str)
767 defer C.free(unsafe.Pointer(cstr))
768 v.C = C.LLVMConstRealOfString(t.C, cstr)
769 return
770 }
771
772 func (v Value) ZExtValue() uint64 { return uint64(C.LLVMConstIntGetZExtValue(v.C)) }
773 func (v Value) SExtValue() int64 { return int64(C.LLVMConstIntGetSExtValue(v.C)) }
774
775 // Operations on composite constants
776 func (c Context) ConstString(str string, addnull bool) (v Value) {
777 cstr := C.CString(str)
778 defer C.free(unsafe.Pointer(cstr))
779 v.C = C.LLVMConstStringInContext(c.C, cstr,
780 C.unsigned(len(str)), boolToLLVMBool(!addnull))
781 return
782 }
783 func (c Context) ConstStruct(constVals []Value, packed bool) (v Value) {
784 ptr, nvals := llvmValueRefs(constVals)
785 v.C = C.LLVMConstStructInContext(c.C, ptr, nvals,
786 boolToLLVMBool(packed))
787 return
788 }
789 func ConstNamedStruct(t Type, constVals []Value) (v Value) {
790 ptr, nvals := llvmValueRefs(constVals)
791 v.C = C.LLVMConstNamedStruct(t.C, ptr, nvals)
792 return
793 }
794 func ConstString(str string, addnull bool) (v Value) {
795 cstr := C.CString(str)
796 defer C.free(unsafe.Pointer(cstr))
797 v.C = C.LLVMConstString(cstr,
798 C.unsigned(len(str)), boolToLLVMBool(!addnull))
799 return
800 }
801 func ConstArray(t Type, constVals []Value) (v Value) {
802 ptr, nvals := llvmValueRefs(constVals)
803 v.C = C.LLVMConstArray(t.C, ptr, nvals)
804 return
805 }
806 func ConstStruct(constVals []Value, packed bool) (v Value) {
807 ptr, nvals := llvmValueRefs(constVals)
808 v.C = C.LLVMConstStruct(ptr, nvals, boolToLLVMBool(packed))
809 return
810 }
811 func ConstVector(scalarConstVals []Value, packed bool) (v Value) {
812 ptr, nvals := llvmValueRefs(scalarConstVals)
813 v.C = C.LLVMConstVector(ptr, nvals)
814 return
815 }
816
817 // Constant expressions
818 func (v Value) Opcode() Opcode { return Opcode(C.LLVMGetConstOpcode(v.C)) }
819 func (v Value) InstructionOpcode() Opcode { return Opcode(C.LLVMGetInstructionOpcode(v.C)) }
820 func AlignOf(t Type) (v Value) { v.C = C.LLVMAlignOf(t.C); return }
821 func SizeOf(t Type) (v Value) { v.C = C.LLVMSizeOf(t.C); return }
822 func ConstNeg(v Value) (rv Value) { rv.C = C.LLVMConstNeg(v.C); return }
823 func ConstNSWNeg(v Value) (rv Value) { rv.C = C.LLVMConstNSWNeg(v.C); return }
824 func ConstNUWNeg(v Value) (rv Value) { rv.C = C.LLVMConstNUWNeg(v.C); return }
825 func ConstFNeg(v Value) (rv Value) { rv.C = C.LLVMConstFNeg(v.C); return }
826 func ConstNot(v Value) (rv Value) { rv.C = C.LLVMConstNot(v.C); return }
827 func ConstAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAdd(lhs.C, rhs.C); return }
828 func ConstNSWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWAdd(lhs.C, rhs.C); return }
829 func ConstNUWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWAdd(lhs.C, rhs.C); return }
830 func ConstFAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFAdd(lhs.C, rhs.C); return }
831 func ConstSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSub(lhs.C, rhs.C); return }
832 func ConstNSWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWSub(lhs.C, rhs.C); return }
833 func ConstNUWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWSub(lhs.C, rhs.C); return }
834 func ConstFSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFSub(lhs.C, rhs.C); return }
835 func ConstMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstMul(lhs.C, rhs.C); return }
836 func ConstNSWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWMul(lhs.C, rhs.C); return }
837 func ConstNUWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWMul(lhs.C, rhs.C); return }
838 func ConstFMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFMul(lhs.C, rhs.C); return }
839 func ConstUDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstUDiv(lhs.C, rhs.C); return }
840 func ConstSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSDiv(lhs.C, rhs.C); return }
841 func ConstExactSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstExactSDiv(lhs.C, rhs.C); return }
842 func ConstFDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFDiv(lhs.C, rhs.C); return }
843 func ConstURem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstURem(lhs.C, rhs.C); return }
844 func ConstSRem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSRem(lhs.C, rhs.C); return }
845 func ConstFRem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFRem(lhs.C, rhs.C); return }
846 func ConstAnd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAnd(lhs.C, rhs.C); return }
847 func ConstOr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstOr(lhs.C, rhs.C); return }
848 func ConstXor(lhs, rhs Value) (v Value) { v.C = C.LLVMConstXor(lhs.C, rhs.C); return }
849
850 func ConstICmp(pred IntPredicate, lhs, rhs Value) (v Value) {
851 v.C = C.LLVMConstICmp(C.LLVMIntPredicate(pred), lhs.C, rhs.C)
852 return
853 }
854 func ConstFCmp(pred FloatPredicate, lhs, rhs Value) (v Value) {
855 v.C = C.LLVMConstFCmp(C.LLVMRealPredicate(pred), lhs.C, rhs.C)
856 return
857 }
858
859 func ConstShl(lhs, rhs Value) (v Value) { v.C = C.LLVMConstShl(lhs.C, rhs.C); return }
860 func ConstLShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstLShr(lhs.C, rhs.C); return }
861 func ConstAShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAShr(lhs.C, rhs.C); return }
862
863 func ConstGEP(v Value, indices []Value) (rv Value) {
864 ptr, nvals := llvmValueRefs(indices)
865 rv.C = C.LLVMConstGEP(v.C, ptr, nvals)
866 return
867 }
868 func ConstInBoundsGEP(v Value, indices []Value) (rv Value) {
869 ptr, nvals := llvmValueRefs(indices)
870 rv.C = C.LLVMConstInBoundsGEP(v.C, ptr, nvals)
871 return
872 }
873 func ConstTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstTrunc(v.C, t.C); return }
874 func ConstSExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExt(v.C, t.C); return }
875 func ConstZExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExt(v.C, t.C); return }
876 func ConstFPTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPTrunc(v.C, t.C); return }
877 func ConstFPExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPExt(v.C, t.C); return }
878 func ConstUIToFP(v Value, t Type) (rv Value) { rv.C = C.LLVMConstUIToFP(v.C, t.C); return }
879 func ConstSIToFP(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSIToFP(v.C, t.C); return }
880 func ConstFPToUI(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPToUI(v.C, t.C); return }
881 func ConstFPToSI(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPToSI(v.C, t.C); return }
882 func ConstPtrToInt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPtrToInt(v.C, t.C); return }
883 func ConstIntToPtr(v Value, t Type) (rv Value) { rv.C = C.LLVMConstIntToPtr(v.C, t.C); return }
884 func ConstBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstBitCast(v.C, t.C); return }
885 func ConstZExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExtOrBitCast(v.C, t.C); return }
886 func ConstSExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExtOrBitCast(v.C, t.C); return }
887 func ConstTruncOrBitCast(v Value, t Type) (rv Value) {
888 rv.C = C.LLVMConstTruncOrBitCast(v.C, t.C)
889 return
890 }
891 func ConstPointerCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPointerCast(v.C, t.C); return }
892 func ConstIntCast(v Value, t Type, signed bool) (rv Value) {
893 rv.C = C.LLVMConstIntCast(v.C, t.C, boolToLLVMBool(signed))
894 return
895 }
896 func ConstFPCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPCast(v.C, t.C); return }
897 func ConstSelect(cond, iftrue, iffalse Value) (rv Value) {
898 rv.C = C.LLVMConstSelect(cond.C, iftrue.C, iffalse.C)
899 return
900 }
901 func ConstExtractElement(vec, i Value) (rv Value) {
902 rv.C = C.LLVMConstExtractElement(vec.C, i.C)
903 return
904 }
905 func ConstInsertElement(vec, elem, i Value) (rv Value) {
906 rv.C = C.LLVMConstInsertElement(vec.C, elem.C, i.C)
907 return
908 }
909 func ConstShuffleVector(veca, vecb, mask Value) (rv Value) {
910 rv.C = C.LLVMConstShuffleVector(veca.C, vecb.C, mask.C)
911 return
912 }
913
914 //TODO
915 //LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
916 // unsigned NumIdx);
917
918 func ConstExtractValue(agg Value, indices []uint32) (rv Value) {
919 n := len(indices)
920 if n == 0 {
921 panic("one or more indices are required")
922 }
923 ptr := (*C.unsigned)(&indices[0])
924 rv.C = C.LLVMConstExtractValue(agg.C, ptr, C.unsigned(n))
925 return
926 }
927
928 func ConstInsertValue(agg, val Value, indices []uint32) (rv Value) {
929 n := len(indices)
930 if n == 0 {
931 panic("one or more indices are required")
932 }
933 ptr := (*C.unsigned)(&indices[0])
934 rv.C = C.LLVMConstInsertValue(agg.C, val.C, ptr, C.unsigned(n))
935 return
936 }
937
938 func BlockAddress(f Value, bb BasicBlock) (v Value) {
939 v.C = C.LLVMBlockAddress(f.C, bb.C)
940 return
941 }
942
943 // Operations on global variables, functions, and aliases (globals)
944 func (v Value) GlobalParent() (m Module) { m.C = C.LLVMGetGlobalParent(v.C); return }
945 func (v Value) IsDeclaration() bool { return C.LLVMIsDeclaration(v.C) != 0 }
946 func (v Value) Linkage() Linkage { return Linkage(C.LLVMGetLinkage(v.C)) }
947 func (v Value) SetLinkage(l Linkage) { C.LLVMSetLinkage(v.C, C.LLVMLinkage(l)) }
948 func (v Value) Section() string { return C.GoString(C.LLVMGetSection(v.C)) }
949 func (v Value) SetSection(str string) {
950 cstr := C.CString(str)
951 defer C.free(unsafe.Pointer(cstr))
952 C.LLVMSetSection(v.C, cstr)
953 }
954 func (v Value) Visibility() Visibility { return Visibility(C.LLVMGetVisibility(v.C)) }
955 func (v Value) SetVisibility(vi Visibility) { C.LLVMSetVisibility(v.C, C.LLVMVisibility(vi)) }
956 func (v Value) Alignment() int { return int(C.LLVMGetAlignment(v.C)) }
957 func (v Value) SetAlignment(a int) { C.LLVMSetAlignment(v.C, C.unsigned(a)) }
958
959 // Operations on global variables
960 func AddGlobal(m Module, t Type, name string) (v Value) {
961 cname := C.CString(name)
962 defer C.free(unsafe.Pointer(cname))
963 v.C = C.LLVMAddGlobal(m.C, t.C, cname)
964 return
965 }
966 func AddGlobalInAddressSpace(m Module, t Type, name string, addressSpace int) (v Value) {
967 cname := C.CString(name)
968 defer C.free(unsafe.Pointer(cname))
969 v.C = C.LLVMAddGlobalInAddressSpace(m.C, t.C, cname, C.unsigned(addressSpace))
970 return
971 }
972 func (m Module) NamedGlobal(name string) (v Value) {
973 cname := C.CString(name)
974 defer C.free(unsafe.Pointer(cname))
975 v.C = C.LLVMGetNamedGlobal(m.C, cname)
976 return
977 }
978
979 func (m Module) FirstGlobal() (v Value) { v.C = C.LLVMGetFirstGlobal(m.C); return }
980 func (m Module) LastGlobal() (v Value) { v.C = C.LLVMGetLastGlobal(m.C); return }
981 func NextGlobal(v Value) (rv Value) { rv.C = C.LLVMGetNextGlobal(v.C); return }
982 func PrevGlobal(v Value) (rv Value) { rv.C = C.LLVMGetPreviousGlobal(v.C); return }
983 func (v Value) EraseFromParentAsGlobal() { C.LLVMDeleteGlobal(v.C) }
984 func (v Value) Initializer() (rv Value) { rv.C = C.LLVMGetInitializer(v.C); return }
985 func (v Value) SetInitializer(cv Value) { C.LLVMSetInitializer(v.C, cv.C) }
986 func (v Value) IsThreadLocal() bool { return C.LLVMIsThreadLocal(v.C) != 0 }
987 func (v Value) SetThreadLocal(tl bool) { C.LLVMSetThreadLocal(v.C, boolToLLVMBool(tl)) }
988 func (v Value) IsGlobalConstant() bool { return C.LLVMIsGlobalConstant(v.C) != 0 }
989 func (v Value) SetGlobalConstant(gc bool) { C.LLVMSetGlobalConstant(v.C, boolToLLVMBool(gc)) }
990
991 // Operations on aliases
992 func AddAlias(m Module, t Type, aliasee Value, name string) (v Value) {
993 cname := C.CString(name)
994 defer C.free(unsafe.Pointer(cname))
995 v.C = C.LLVMAddAlias(m.C, t.C, aliasee.C, cname)
996 return
997 }
998
999 // Operations on functions
1000 func AddFunction(m Module, name string, ft Type) (v Value) {
1001 cname := C.CString(name)
1002 defer C.free(unsafe.Pointer(cname))
1003 v.C = C.LLVMAddFunction(m.C, cname, ft.C)
1004 return
1005 }
1006
1007 func (m Module) NamedFunction(name string) (v Value) {
1008 cname := C.CString(name)
1009 defer C.free(unsafe.Pointer(cname))
1010 v.C = C.LLVMGetNamedFunction(m.C, cname)
1011 return
1012 }
1013
1014 func (m Module) FirstFunction() (v Value) { v.C = C.LLVMGetFirstFunction(m.C); return }
1015 func (m Module) LastFunction() (v Value) { v.C = C.LLVMGetLastFunction(m.C); return }
1016 func NextFunction(v Value) (rv Value) { rv.C = C.LLVMGetNextFunction(v.C); return }
1017 func PrevFunction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousFunction(v.C); return }
1018 func (v Value) EraseFromParentAsFunction() { C.LLVMDeleteFunction(v.C) }
1019 func (v Value) IntrinsicID() int { return int(C.LLVMGetIntrinsicID(v.C)) }
1020 func (v Value) FunctionCallConv() CallConv {
1021 return CallConv(C.LLVMCallConv(C.LLVMGetFunctionCallConv(v.C)))
1022 }
1023 func (v Value) SetFunctionCallConv(cc CallConv) { C.LLVMSetFunctionCallConv(v.C, C.unsigned(cc)) }
1024 func (v Value) GC() string { return C.GoString(C.LLVMGetGC(v.C)) }
1025 func (v Value) SetGC(name string) {
1026 cname := C.CString(name)
1027 defer C.free(unsafe.Pointer(cname))
1028 C.LLVMSetGC(v.C, cname)
1029 }
1030 func (v Value) AddFunctionAttr(a Attribute) { C.LLVMAddFunctionAttr2(v.C, C.uint64_t(a)) }
1031 func (v Value) FunctionAttr() Attribute { return Attribute(C.LLVMGetFunctionAttr2(v.C)) }
1032 func (v Value) RemoveFunctionAttr(a Attribute) { C.LLVMRemoveFunctionAttr2(v.C, C.uint64_t(a)) }
1033 func (v Value) AddTargetDependentFunctionAttr(attr, value string) {
1034 cattr := C.CString(attr)
1035 defer C.free(unsafe.Pointer(cattr))
1036 cvalue := C.CString(value)
1037 defer C.free(unsafe.Pointer(cvalue))
1038 C.LLVMAddTargetDependentFunctionAttr(v.C, cattr, cvalue)
1039 }
1040
1041 // Operations on parameters
1042 func (v Value) ParamsCount() int { return int(C.LLVMCountParams(v.C)) }
1043 func (v Value) Params() []Value {
1044 out := make([]Value, v.ParamsCount())
1045 if len(out) > 0 {
1046 C.LLVMGetParams(v.C, llvmValueRefPtr(&out[0]))
1047 }
1048 return out
1049 }
1050 func (v Value) Param(i int) (rv Value) { rv.C = C.LLVMGetParam(v.C, C.unsigned(i)); return }
1051 func (v Value) ParamParent() (rv Value) { rv.C = C.LLVMGetParamParent(v.C); return }
1052 func (v Value) FirstParam() (rv Value) { rv.C = C.LLVMGetFirstParam(v.C); return }
1053 func (v Value) LastParam() (rv Value) { rv.C = C.LLVMGetLastParam(v.C); return }
1054 func NextParam(v Value) (rv Value) { rv.C = C.LLVMGetNextParam(v.C); return }
1055 func PrevParam(v Value) (rv Value) { rv.C = C.LLVMGetPreviousParam(v.C); return }
1056 func (v Value) AddAttribute(a Attribute) {
1057 if a >= 1<<32 {
1058 panic("attribute value currently unsupported")
1059 }
1060 C.LLVMAddAttribute(v.C, C.LLVMAttribute(a))
1061 }
1062 func (v Value) RemoveAttribute(a Attribute) {
1063 if a >= 1<<32 {
1064 panic("attribute value currently unsupported")
1065 }
1066 C.LLVMRemoveAttribute(v.C, C.LLVMAttribute(a))
1067 }
1068 func (v Value) Attribute() Attribute { return Attribute(C.LLVMGetAttribute(v.C)) }
1069 func (v Value) SetParamAlignment(align int) { C.LLVMSetParamAlignment(v.C, C.unsigned(align)) }
1070
1071 // Operations on basic blocks
1072 func (bb BasicBlock) AsValue() (v Value) { v.C = C.LLVMBasicBlockAsValue(bb.C); return }
1073 func (v Value) IsBasicBlock() bool { return C.LLVMValueIsBasicBlock(v.C) != 0 }
1074 func (v Value) AsBasicBlock() (bb BasicBlock) { bb.C = C.LLVMValueAsBasicBlock(v.C); return }
1075 func (bb BasicBlock) Parent() (v Value) { v.C = C.LLVMGetBasicBlockParent(bb.C); return }
1076 func (v Value) BasicBlocksCount() int { return int(C.LLVMCountBasicBlocks(v.C)) }
1077 func (v Value) BasicBlocks() []BasicBlock {
1078 out := make([]BasicBlock, v.BasicBlocksCount())
1079 C.LLVMGetBasicBlocks(v.C, llvmBasicBlockRefPtr(&out[0]))
1080 return out
1081 }
1082 func (v Value) FirstBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetFirstBasicBlock(v.C); return }
1083 func (v Value) LastBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetLastBasicBlock(v.C); return }
1084 func NextBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetNextBasicBlock(bb.C); return }
1085 func PrevBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetPreviousBasicBlock(bb.C); return }
1086 func (v Value) EntryBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetEntryBasicBlock(v.C); return }
1087 func (c Context) AddBasicBlock(f Value, name string) (bb BasicBlock) {
1088 cname := C.CString(name)
1089 defer C.free(unsafe.Pointer(cname))
1090 bb.C = C.LLVMAppendBasicBlockInContext(c.C, f.C, cname)
1091 return
1092 }
1093 func (c Context) InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1094 cname := C.CString(name)
1095 defer C.free(unsafe.Pointer(cname))
1096 bb.C = C.LLVMInsertBasicBlockInContext(c.C, ref.C, cname)
1097 return
1098 }
1099 func AddBasicBlock(f Value, name string) (bb BasicBlock) {
1100 cname := C.CString(name)
1101 defer C.free(unsafe.Pointer(cname))
1102 bb.C = C.LLVMAppendBasicBlock(f.C, cname)
1103 return
1104 }
1105 func InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1106 cname := C.CString(name)
1107 defer C.free(unsafe.Pointer(cname))
1108 bb.C = C.LLVMInsertBasicBlock(ref.C, cname)
1109 return
1110 }
1111 func (bb BasicBlock) EraseFromParent() { C.LLVMDeleteBasicBlock(bb.C) }
1112 func (bb BasicBlock) MoveBefore(pos BasicBlock) { C.LLVMMoveBasicBlockBefore(bb.C, pos.C) }
1113 func (bb BasicBlock) MoveAfter(pos BasicBlock) { C.LLVMMoveBasicBlockAfter(bb.C, pos.C) }
1114
1115 // Operations on instructions
1116 func (v Value) InstructionParent() (bb BasicBlock) { bb.C = C.LLVMGetInstructionParent(v.C); return }
1117 func (bb BasicBlock) FirstInstruction() (v Value) { v.C = C.LLVMGetFirstInstruction(bb.C); return }
1118 func (bb BasicBlock) LastInstruction() (v Value) { v.C = C.LLVMGetLastInstruction(bb.C); return }
1119 func NextInstruction(v Value) (rv Value) { rv.C = C.LLVMGetNextInstruction(v.C); return }
1120 func PrevInstruction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousInstruction(v.C); return }
1121
1122 // Operations on call sites
1123 func (v Value) SetInstructionCallConv(cc CallConv) {
1124 C.LLVMSetInstructionCallConv(v.C, C.unsigned(cc))
1125 }
1126 func (v Value) InstructionCallConv() CallConv {
1127 return CallConv(C.LLVMCallConv(C.LLVMGetInstructionCallConv(v.C)))
1128 }
1129 func (v Value) AddInstrAttribute(i int, a Attribute) {
1130 if a >= 1<<32 {
1131 panic("attribute value currently unsupported")
1132 }
1133 C.LLVMAddInstrAttribute(v.C, C.unsigned(i), C.LLVMAttribute(a))
1134 }
1135 func (v Value) RemoveInstrAttribute(i int, a Attribute) {
1136 if a >= 1<<32 {
1137 panic("attribute value currently unsupported")
1138 }
1139 C.LLVMRemoveInstrAttribute(v.C, C.unsigned(i), C.LLVMAttribute(a))
1140 }
1141 func (v Value) SetInstrParamAlignment(i int, align int) {
1142 C.LLVMSetInstrParamAlignment(v.C, C.unsigned(i), C.unsigned(align))
1143 }
1144
1145 // Operations on call instructions (only)
1146 func (v Value) IsTailCall() bool { return C.LLVMIsTailCall(v.C) != 0 }
1147 func (v Value) SetTailCall(is bool) { C.LLVMSetTailCall(v.C, boolToLLVMBool(is)) }
1148
1149 // Operations on phi nodes
1150 func (v Value) AddIncoming(vals []Value, blocks []BasicBlock) {
1151 ptr, nvals := llvmValueRefs(vals)
1152 C.LLVMAddIncoming(v.C, ptr, llvmBasicBlockRefPtr(&blocks[0]), nvals)
1153 }
1154 func (v Value) IncomingCount() int { return int(C.LLVMCountIncoming(v.C)) }
1155 func (v Value) IncomingValue(i int) (rv Value) {
1156 rv.C = C.LLVMGetIncomingValue(v.C, C.unsigned(i))
1157 return
1158 }
1159 func (v Value) IncomingBlock(i int) (bb BasicBlock) {
1160 bb.C = C.LLVMGetIncomingBlock(v.C, C.unsigned(i))
1161 return
1162 }
1163
1164 //-------------------------------------------------------------------------
1165 // llvm.Builder
1166 //-------------------------------------------------------------------------
1167
1168 // An instruction builder represents a point within a basic block, and is the
1169 // exclusive means of building instructions using the C interface.
1170
1171 func (c Context) NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilderInContext(c.C); return }
1172 func NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilder(); return }
1173 func (b Builder) SetInsertPoint(block BasicBlock, instr Value) {
1174 C.LLVMPositionBuilder(b.C, block.C, instr.C)
1175 }
1176 func (b Builder) SetInsertPointBefore(instr Value) { C.LLVMPositionBuilderBefore(b.C, instr.C) }
1177 func (b Builder) SetInsertPointAtEnd(block BasicBlock) { C.LLVMPositionBuilderAtEnd(b.C, block.C) }
1178 func (b Builder) GetInsertBlock() (bb BasicBlock) { bb.C = C.LLVMGetInsertBlock(b.C); return }
1179 func (b Builder) ClearInsertionPoint() { C.LLVMClearInsertionPosition(b.C) }
1180 func (b Builder) Insert(instr Value) { C.LLVMInsertIntoBuilder(b.C, instr.C) }
1181 func (b Builder) InsertWithName(instr Value, name string) {
1182 cname := C.CString(name)
1183 defer C.free(unsafe.Pointer(cname))
1184 C.LLVMInsertIntoBuilderWithName(b.C, instr.C, cname)
1185 }
1186 func (b Builder) Dispose() { C.LLVMDisposeBuilder(b.C) }
1187
1188 // Metadata
1189 func (b Builder) SetCurrentDebugLocation(v Value) { C.LLVMSetCurrentDebugLocation(b.C, v.C) }
1190 func (b Builder) CurrentDebugLocation() (v Value) { v.C = C.LLVMGetCurrentDebugLocation(b.C); return }
1191 func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) }
1192 func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value {
1193 f := module.NamedFunction("llvm.dbg.declare")
1194 if f.IsNil() {
1195 ftyp := FunctionType(VoidType(), []Type{storage.Type(), md.Type()}, false)
1196 f = AddFunction(module, "llvm.dbg.declare", ftyp)
1197 }
1198 return b.CreateCall(f, []Value{storage, md}, "")
1199 }
1200
1201 // Terminators
1202 func (b Builder) CreateRetVoid() (rv Value) { rv.C = C.LLVMBuildRetVoid(b.C); return }
1203 func (b Builder) CreateRet(v Value) (rv Value) { rv.C = C.LLVMBuildRet(b.C, v.C); return }
1204 func (b Builder) CreateAggregateRet(vs []Value) (rv Value) {
1205 ptr, nvals := llvmValueRefs(vs)
1206 rv.C = C.LLVMBuildAggregateRet(b.C, ptr, nvals)
1207 return
1208 }
1209 func (b Builder) CreateBr(bb BasicBlock) (rv Value) { rv.C = C.LLVMBuildBr(b.C, bb.C); return }
1210 func (b Builder) CreateCondBr(ifv Value, thenb, elseb BasicBlock) (rv Value) {
1211 rv.C = C.LLVMBuildCondBr(b.C, ifv.C, thenb.C, elseb.C)
1212 return
1213 }
1214 func (b Builder) CreateSwitch(v Value, elseb BasicBlock, numCases int) (rv Value) {
1215 rv.C = C.LLVMBuildSwitch(b.C, v.C, elseb.C, C.unsigned(numCases))
1216 return
1217 }
1218 func (b Builder) CreateIndirectBr(addr Value, numDests int) (rv Value) {
1219 rv.C = C.LLVMBuildIndirectBr(b.C, addr.C, C.unsigned(numDests))
1220 return
1221 }
1222 func (b Builder) CreateInvoke(fn Value, args []Value, then, catch BasicBlock, name string) (rv Value) {
1223 cname := C.CString(name)
1224 defer C.free(unsafe.Pointer(cname))
1225 ptr, nvals := llvmValueRefs(args)
1226 rv.C = C.LLVMBuildInvoke(b.C, fn.C, ptr, nvals, then.C, catch.C, cname)
1227 return
1228 }
1229 func (b Builder) CreateUnreachable() (rv Value) { rv.C = C.LLVMBuildUnreachable(b.C); return }
1230
1231 // Add a case to the switch instruction
1232 func (v Value) AddCase(on Value, dest BasicBlock) { C.LLVMAddCase(v.C, on.C, dest.C) }
1233
1234 // Add a destination to the indirectbr instruction
1235 func (v Value) AddDest(dest BasicBlock) { C.LLVMAddDestination(v.C, dest.C) }
1236
1237 // Arithmetic
1238 func (b Builder) CreateAdd(lhs, rhs Value, name string) (v Value) {
1239 cname := C.CString(name)
1240 defer C.free(unsafe.Pointer(cname))
1241 v.C = C.LLVMBuildAdd(b.C, lhs.C, rhs.C, cname)
1242 return
1243 }
1244 func (b Builder) CreateNSWAdd(lhs, rhs Value, name string) (v Value) {
1245 cname := C.CString(name)
1246 defer C.free(unsafe.Pointer(cname))
1247 v.C = C.LLVMBuildNSWAdd(b.C, lhs.C, rhs.C, cname)
1248 return
1249 }
1250 func (b Builder) CreateNUWAdd(lhs, rhs Value, name string) (v Value) {
1251 cname := C.CString(name)
1252 defer C.free(unsafe.Pointer(cname))
1253 v.C = C.LLVMBuildNUWAdd(b.C, lhs.C, rhs.C, cname)
1254 return
1255 }
1256 func (b Builder) CreateFAdd(lhs, rhs Value, name string) (v Value) {
1257 cname := C.CString(name)
1258 defer C.free(unsafe.Pointer(cname))
1259 v.C = C.LLVMBuildFAdd(b.C, lhs.C, rhs.C, cname)
1260 return
1261 }
1262 func (b Builder) CreateSub(lhs, rhs Value, name string) (v Value) {
1263 cname := C.CString(name)
1264 defer C.free(unsafe.Pointer(cname))
1265 v.C = C.LLVMBuildSub(b.C, lhs.C, rhs.C, cname)
1266 return
1267 }
1268 func (b Builder) CreateNSWSub(lhs, rhs Value, name string) (v Value) {
1269 cname := C.CString(name)
1270 defer C.free(unsafe.Pointer(cname))
1271 v.C = C.LLVMBuildNSWSub(b.C, lhs.C, rhs.C, cname)
1272 return
1273 }
1274 func (b Builder) CreateNUWSub(lhs, rhs Value, name string) (v Value) {
1275 cname := C.CString(name)
1276 defer C.free(unsafe.Pointer(cname))
1277 v.C = C.LLVMBuildNUWSub(b.C, lhs.C, rhs.C, cname)
1278 return
1279 }
1280 func (b Builder) CreateFSub(lhs, rhs Value, name string) (v Value) {
1281 cname := C.CString(name)
1282 v.C = C.LLVMBuildFSub(b.C, lhs.C, rhs.C, cname)
1283 C.free(unsafe.Pointer(cname))
1284 return
1285 }
1286 func (b Builder) CreateMul(lhs, rhs Value, name string) (v Value) {
1287 cname := C.CString(name)
1288 defer C.free(unsafe.Pointer(cname))
1289 v.C = C.LLVMBuildMul(b.C, lhs.C, rhs.C, cname)
1290 return
1291 }
1292 func (b Builder) CreateNSWMul(lhs, rhs Value, name string) (v Value) {
1293 cname := C.CString(name)
1294 defer C.free(unsafe.Pointer(cname))
1295 v.C = C.LLVMBuildNSWMul(b.C, lhs.C, rhs.C, cname)
1296 return
1297 }
1298 func (b Builder) CreateNUWMul(lhs, rhs Value, name string) (v Value) {
1299 cname := C.CString(name)
1300 defer C.free(unsafe.Pointer(cname))
1301 v.C = C.LLVMBuildNUWMul(b.C, lhs.C, rhs.C, cname)
1302 return
1303 }
1304 func (b Builder) CreateFMul(lhs, rhs Value, name string) (v Value) {
1305 cname := C.CString(name)
1306 defer C.free(unsafe.Pointer(cname))
1307 v.C = C.LLVMBuildFMul(b.C, lhs.C, rhs.C, cname)
1308 return
1309 }
1310 func (b Builder) CreateUDiv(lhs, rhs Value, name string) (v Value) {
1311 cname := C.CString(name)
1312 defer C.free(unsafe.Pointer(cname))
1313 v.C = C.LLVMBuildUDiv(b.C, lhs.C, rhs.C, cname)
1314 return
1315 }
1316 func (b Builder) CreateSDiv(lhs, rhs Value, name string) (v Value) {
1317 cname := C.CString(name)
1318 defer C.free(unsafe.Pointer(cname))
1319 v.C = C.LLVMBuildSDiv(b.C, lhs.C, rhs.C, cname)
1320 return
1321 }
1322 func (b Builder) CreateExactSDiv(lhs, rhs Value, name string) (v Value) {
1323 cname := C.CString(name)
1324 defer C.free(unsafe.Pointer(cname))
1325 v.C = C.LLVMBuildExactSDiv(b.C, lhs.C, rhs.C, cname)
1326 return
1327 }
1328 func (b Builder) CreateFDiv(lhs, rhs Value, name string) (v Value) {
1329 cname := C.CString(name)
1330 defer C.free(unsafe.Pointer(cname))
1331 v.C = C.LLVMBuildFDiv(b.C, lhs.C, rhs.C, cname)
1332 return
1333 }
1334 func (b Builder) CreateURem(lhs, rhs Value, name string) (v Value) {
1335 cname := C.CString(name)
1336 defer C.free(unsafe.Pointer(cname))
1337 v.C = C.LLVMBuildURem(b.C, lhs.C, rhs.C, cname)
1338 return
1339 }
1340 func (b Builder) CreateSRem(lhs, rhs Value, name string) (v Value) {
1341 cname := C.CString(name)
1342 defer C.free(unsafe.Pointer(cname))
1343 v.C = C.LLVMBuildSRem(b.C, lhs.C, rhs.C, cname)
1344 return
1345 }
1346 func (b Builder) CreateFRem(lhs, rhs Value, name string) (v Value) {
1347 cname := C.CString(name)
1348 defer C.free(unsafe.Pointer(cname))
1349 v.C = C.LLVMBuildFRem(b.C, lhs.C, rhs.C, cname)
1350 return
1351 }
1352 func (b Builder) CreateShl(lhs, rhs Value, name string) (v Value) {
1353 cname := C.CString(name)
1354 defer C.free(unsafe.Pointer(cname))
1355 v.C = C.LLVMBuildShl(b.C, lhs.C, rhs.C, cname)
1356 return
1357 }
1358 func (b Builder) CreateLShr(lhs, rhs Value, name string) (v Value) {
1359 cname := C.CString(name)
1360 defer C.free(unsafe.Pointer(cname))
1361 v.C = C.LLVMBuildLShr(b.C, lhs.C, rhs.C, cname)
1362 return
1363 }
1364 func (b Builder) CreateAShr(lhs, rhs Value, name string) (v Value) {
1365 cname := C.CString(name)
1366 defer C.free(unsafe.Pointer(cname))
1367 v.C = C.LLVMBuildAShr(b.C, lhs.C, rhs.C, cname)
1368 return
1369 }
1370 func (b Builder) CreateAnd(lhs, rhs Value, name string) (v Value) {
1371 cname := C.CString(name)
1372 defer C.free(unsafe.Pointer(cname))
1373 v.C = C.LLVMBuildAnd(b.C, lhs.C, rhs.C, cname)
1374 return
1375 }
1376 func (b Builder) CreateOr(lhs, rhs Value, name string) (v Value) {
1377 cname := C.CString(name)
1378 defer C.free(unsafe.Pointer(cname))
1379 v.C = C.LLVMBuildOr(b.C, lhs.C, rhs.C, cname)
1380 return
1381 }
1382 func (b Builder) CreateXor(lhs, rhs Value, name string) (v Value) {
1383 cname := C.CString(name)
1384 defer C.free(unsafe.Pointer(cname))
1385 v.C = C.LLVMBuildXor(b.C, lhs.C, rhs.C, cname)
1386 return
1387 }
1388 func (b Builder) CreateBinOp(op Opcode, lhs, rhs Value, name string) (v Value) {
1389 cname := C.CString(name)
1390 defer C.free(unsafe.Pointer(cname))
1391 v.C = C.LLVMBuildBinOp(b.C, C.LLVMOpcode(op), lhs.C, rhs.C, cname)
1392 return
1393 }
1394 func (b Builder) CreateNeg(v Value, name string) (rv Value) {
1395 cname := C.CString(name)
1396 defer C.free(unsafe.Pointer(cname))
1397 rv.C = C.LLVMBuildNeg(b.C, v.C, cname)
1398 return
1399 }
1400 func (b Builder) CreateNSWNeg(v Value, name string) (rv Value) {
1401 cname := C.CString(name)
1402 defer C.free(unsafe.Pointer(cname))
1403 rv.C = C.LLVMBuildNSWNeg(b.C, v.C, cname)
1404 return
1405 }
1406 func (b Builder) CreateNUWNeg(v Value, name string) (rv Value) {
1407 cname := C.CString(name)
1408 defer C.free(unsafe.Pointer(cname))
1409 rv.C = C.LLVMBuildNUWNeg(b.C, v.C, cname)
1410 return
1411 }
1412 func (b Builder) CreateFNeg(v Value, name string) (rv Value) {
1413 cname := C.CString(name)
1414 defer C.free(unsafe.Pointer(cname))
1415 rv.C = C.LLVMBuildFNeg(b.C, v.C, cname)
1416 return
1417 }
1418 func (b Builder) CreateNot(v Value, name string) (rv Value) {
1419 cname := C.CString(name)
1420 defer C.free(unsafe.Pointer(cname))
1421 rv.C = C.LLVMBuildNot(b.C, v.C, cname)
1422 return
1423 }
1424
1425 // Memory
1426
1427 func (b Builder) CreateMalloc(t Type, name string) (v Value) {
1428 cname := C.CString(name)
1429 defer C.free(unsafe.Pointer(cname))
1430 v.C = C.LLVMBuildMalloc(b.C, t.C, cname)
1431 return
1432 }
1433 func (b Builder) CreateArrayMalloc(t Type, val Value, name string) (v Value) {
1434 cname := C.CString(name)
1435 defer C.free(unsafe.Pointer(cname))
1436 v.C = C.LLVMBuildArrayMalloc(b.C, t.C, val.C, cname)
1437 return
1438 }
1439 func (b Builder) CreateAlloca(t Type, name string) (v Value) {
1440 cname := C.CString(name)
1441 defer C.free(unsafe.Pointer(cname))
1442 v.C = C.LLVMBuildAlloca(b.C, t.C, cname)
1443 return
1444 }
1445 func (b Builder) CreateArrayAlloca(t Type, val Value, name string) (v Value) {
1446 cname := C.CString(name)
1447 defer C.free(unsafe.Pointer(cname))
1448 v.C = C.LLVMBuildArrayAlloca(b.C, t.C, val.C, cname)
1449 return
1450 }
1451 func (b Builder) CreateFree(p Value) (v Value) {
1452 v.C = C.LLVMBuildFree(b.C, p.C)
1453 return
1454 }
1455 func (b Builder) CreateLoad(p Value, name string) (v Value) {
1456 cname := C.CString(name)
1457 defer C.free(unsafe.Pointer(cname))
1458 v.C = C.LLVMBuildLoad(b.C, p.C, cname)
1459 return
1460 }
1461 func (b Builder) CreateStore(val Value, p Value) (v Value) {
1462 v.C = C.LLVMBuildStore(b.C, val.C, p.C)
1463 return
1464 }
1465 func (b Builder) CreateGEP(p Value, indices []Value, name string) (v Value) {
1466 cname := C.CString(name)
1467 defer C.free(unsafe.Pointer(cname))
1468 ptr, nvals := llvmValueRefs(indices)
1469 v.C = C.LLVMBuildGEP(b.C, p.C, ptr, nvals, cname)
1470 return
1471 }
1472 func (b Builder) CreateInBoundsGEP(p Value, indices []Value, name string) (v Value) {
1473 cname := C.CString(name)
1474 defer C.free(unsafe.Pointer(cname))
1475 ptr, nvals := llvmValueRefs(indices)
1476 v.C = C.LLVMBuildInBoundsGEP(b.C, p.C, ptr, nvals, cname)
1477 return
1478 }
1479 func (b Builder) CreateStructGEP(p Value, i int, name string) (v Value) {
1480 cname := C.CString(name)
1481 defer C.free(unsafe.Pointer(cname))
1482 v.C = C.LLVMBuildStructGEP(b.C, p.C, C.unsigned(i), cname)
1483 return
1484 }
1485 func (b Builder) CreateGlobalString(str, name string) (v Value) {
1486 cstr := C.CString(str)
1487 defer C.free(unsafe.Pointer(cstr))
1488 cname := C.CString(name)
1489 defer C.free(unsafe.Pointer(cname))
1490 v.C = C.LLVMBuildGlobalString(b.C, cstr, cname)
1491 return
1492 }
1493 func (b Builder) CreateGlobalStringPtr(str, name string) (v Value) {
1494 cstr := C.CString(str)
1495 defer C.free(unsafe.Pointer(cstr))
1496 cname := C.CString(name)
1497 defer C.free(unsafe.Pointer(cname))
1498 v.C = C.LLVMBuildGlobalStringPtr(b.C, cstr, cname)
1499 return
1500 }
1501
1502 // Casts
1503 func (b Builder) CreateTrunc(val Value, t Type, name string) (v Value) {
1504 cname := C.CString(name)
1505 defer C.free(unsafe.Pointer(cname))
1506 v.C = C.LLVMBuildTrunc(b.C, val.C, t.C, cname)
1507 return
1508 }
1509 func (b Builder) CreateZExt(val Value, t Type, name string) (v Value) {
1510 cname := C.CString(name)
1511 defer C.free(unsafe.Pointer(cname))
1512 v.C = C.LLVMBuildZExt(b.C, val.C, t.C, cname)
1513 return
1514 }
1515 func (b Builder) CreateSExt(val Value, t Type, name string) (v Value) {
1516 cname := C.CString(name)
1517 defer C.free(unsafe.Pointer(cname))
1518 v.C = C.LLVMBuildSExt(b.C, val.C, t.C, cname)
1519 return
1520 }
1521 func (b Builder) CreateFPToUI(val Value, t Type, name string) (v Value) {
1522 cname := C.CString(name)
1523 defer C.free(unsafe.Pointer(cname))
1524 v.C = C.LLVMBuildFPToUI(b.C, val.C, t.C, cname)
1525 return
1526 }
1527 func (b Builder) CreateFPToSI(val Value, t Type, name string) (v Value) {
1528 cname := C.CString(name)
1529 defer C.free(unsafe.Pointer(cname))
1530 v.C = C.LLVMBuildFPToSI(b.C, val.C, t.C, cname)
1531 return
1532 }
1533 func (b Builder) CreateUIToFP(val Value, t Type, name string) (v Value) {
1534 cname := C.CString(name)
1535 defer C.free(unsafe.Pointer(cname))
1536 v.C = C.LLVMBuildUIToFP(b.C, val.C, t.C, cname)
1537 return
1538 }
1539 func (b Builder) CreateSIToFP(val Value, t Type, name string) (v Value) {
1540 cname := C.CString(name)
1541 defer C.free(unsafe.Pointer(cname))
1542 v.C = C.LLVMBuildSIToFP(b.C, val.C, t.C, cname)
1543 return
1544 }
1545 func (b Builder) CreateFPTrunc(val Value, t Type, name string) (v Value) {
1546 cname := C.CString(name)
1547 defer C.free(unsafe.Pointer(cname))
1548 v.C = C.LLVMBuildFPTrunc(b.C, val.C, t.C, cname)
1549 return
1550 }
1551 func (b Builder) CreateFPExt(val Value, t Type, name string) (v Value) {
1552 cname := C.CString(name)
1553 defer C.free(unsafe.Pointer(cname))
1554 v.C = C.LLVMBuildFPExt(b.C, val.C, t.C, cname)
1555 return
1556 }
1557 func (b Builder) CreatePtrToInt(val Value, t Type, name string) (v Value) {
1558 cname := C.CString(name)
1559 defer C.free(unsafe.Pointer(cname))
1560 v.C = C.LLVMBuildPtrToInt(b.C, val.C, t.C, cname)
1561 return
1562 }
1563 func (b Builder) CreateIntToPtr(val Value, t Type, name string) (v Value) {
1564 cname := C.CString(name)
1565 defer C.free(unsafe.Pointer(cname))
1566 v.C = C.LLVMBuildIntToPtr(b.C, val.C, t.C, cname)
1567 return
1568 }
1569 func (b Builder) CreateBitCast(val Value, t Type, name string) (v Value) {
1570 cname := C.CString(name)
1571 defer C.free(unsafe.Pointer(cname))
1572 v.C = C.LLVMBuildBitCast(b.C, val.C, t.C, cname)
1573 return
1574 }
1575 func (b Builder) CreateZExtOrBitCast(val Value, t Type, name string) (v Value) {
1576 cname := C.CString(name)
1577 defer C.free(unsafe.Pointer(cname))
1578 v.C = C.LLVMBuildZExtOrBitCast(b.C, val.C, t.C, cname)
1579 return
1580 }
1581 func (b Builder) CreateSExtOrBitCast(val Value, t Type, name string) (v Value) {
1582 cname := C.CString(name)
1583 defer C.free(unsafe.Pointer(cname))
1584 v.C = C.LLVMBuildSExtOrBitCast(b.C, val.C, t.C, cname)
1585 return
1586 }
1587 func (b Builder) CreateTruncOrBitCast(val Value, t Type, name string) (v Value) {
1588 cname := C.CString(name)
1589 defer C.free(unsafe.Pointer(cname))
1590 v.C = C.LLVMBuildTruncOrBitCast(b.C, val.C, t.C, cname)
1591 return
1592 }
1593 func (b Builder) CreateCast(val Value, op Opcode, t Type, name string) (v Value) {
1594 cname := C.CString(name)
1595 defer C.free(unsafe.Pointer(cname))
1596 v.C = C.LLVMBuildCast(b.C, C.LLVMOpcode(op), val.C, t.C, cname)
1597 return
1598 } //
1599 func (b Builder) CreatePointerCast(val Value, t Type, name string) (v Value) {
1600 cname := C.CString(name)
1601 defer C.free(unsafe.Pointer(cname))
1602 v.C = C.LLVMBuildPointerCast(b.C, val.C, t.C, cname)
1603 return
1604 }
1605 func (b Builder) CreateIntCast(val Value, t Type, name string) (v Value) {
1606 cname := C.CString(name)
1607 defer C.free(unsafe.Pointer(cname))
1608 v.C = C.LLVMBuildIntCast(b.C, val.C, t.C, cname)
1609 return
1610 }
1611 func (b Builder) CreateFPCast(val Value, t Type, name string) (v Value) {
1612 cname := C.CString(name)
1613 defer C.free(unsafe.Pointer(cname))
1614 v.C = C.LLVMBuildFPCast(b.C, val.C, t.C, cname)
1615 return
1616 }
1617
1618 // Comparisons
1619 func (b Builder) CreateICmp(pred IntPredicate, lhs, rhs Value, name string) (v Value) {
1620 cname := C.CString(name)
1621 defer C.free(unsafe.Pointer(cname))
1622 v.C = C.LLVMBuildICmp(b.C, C.LLVMIntPredicate(pred), lhs.C, rhs.C, cname)
1623 return
1624 }
1625 func (b Builder) CreateFCmp(pred FloatPredicate, lhs, rhs Value, name string) (v Value) {
1626 cname := C.CString(name)
1627 defer C.free(unsafe.Pointer(cname))
1628 v.C = C.LLVMBuildFCmp(b.C, C.LLVMRealPredicate(pred), lhs.C, rhs.C, cname)
1629 return
1630 }
1631
1632 // Miscellaneous instructions
1633 func (b Builder) CreatePHI(t Type, name string) (v Value) {
1634 cname := C.CString(name)
1635 defer C.free(unsafe.Pointer(cname))
1636 v.C = C.LLVMBuildPhi(b.C, t.C, cname)
1637 return
1638 }
1639 func (b Builder) CreateCall(fn Value, args []Value, name string) (v Value) {
1640 cname := C.CString(name)
1641 defer C.free(unsafe.Pointer(cname))
1642 ptr, nvals := llvmValueRefs(args)
1643 v.C = C.LLVMBuildCall(b.C, fn.C, ptr, nvals, cname)
1644 return
1645 }
1646
1647 func (b Builder) CreateSelect(ifv, thenv, elsev Value, name string) (v Value) {
1648 cname := C.CString(name)
1649 defer C.free(unsafe.Pointer(cname))
1650 v.C = C.LLVMBuildSelect(b.C, ifv.C, thenv.C, elsev.C, cname)
1651 return
1652 }
1653
1654 func (b Builder) CreateVAArg(list Value, t Type, name string) (v Value) {
1655 cname := C.CString(name)
1656 defer C.free(unsafe.Pointer(cname))
1657 v.C = C.LLVMBuildVAArg(b.C, list.C, t.C, cname)
1658 return
1659 }
1660 func (b Builder) CreateExtractElement(vec, i Value, name string) (v Value) {
1661 cname := C.CString(name)
1662 defer C.free(unsafe.Pointer(cname))
1663 v.C = C.LLVMBuildExtractElement(b.C, vec.C, i.C, cname)
1664 return
1665 }
1666 func (b Builder) CreateInsertElement(vec, elt, i Value, name string) (v Value) {
1667 cname := C.CString(name)
1668 defer C.free(unsafe.Pointer(cname))
1669 v.C = C.LLVMBuildInsertElement(b.C, vec.C, elt.C, i.C, cname)
1670 return
1671 }
1672 func (b Builder) CreateShuffleVector(v1, v2, mask Value, name string) (v Value) {
1673 cname := C.CString(name)
1674 defer C.free(unsafe.Pointer(cname))
1675 v.C = C.LLVMBuildShuffleVector(b.C, v1.C, v2.C, mask.C, cname)
1676 return
1677 }
1678 func (b Builder) CreateExtractValue(agg Value, i int, name string) (v Value) {
1679 cname := C.CString(name)
1680 defer C.free(unsafe.Pointer(cname))
1681 v.C = C.LLVMBuildExtractValue(b.C, agg.C, C.unsigned(i), cname)
1682 return
1683 }
1684 func (b Builder) CreateInsertValue(agg, elt Value, i int, name string) (v Value) {
1685 cname := C.CString(name)
1686 defer C.free(unsafe.Pointer(cname))
1687 v.C = C.LLVMBuildInsertValue(b.C, agg.C, elt.C, C.unsigned(i), cname)
1688 return
1689 }
1690
1691 func (b Builder) CreateIsNull(val Value, name string) (v Value) {
1692 cname := C.CString(name)
1693 defer C.free(unsafe.Pointer(cname))
1694 v.C = C.LLVMBuildIsNull(b.C, val.C, cname)
1695 return
1696 }
1697 func (b Builder) CreateIsNotNull(val Value, name string) (v Value) {
1698 cname := C.CString(name)
1699 defer C.free(unsafe.Pointer(cname))
1700 v.C = C.LLVMBuildIsNotNull(b.C, val.C, cname)
1701 return
1702 }
1703 func (b Builder) CreatePtrDiff(lhs, rhs Value, name string) (v Value) {
1704 cname := C.CString(name)
1705 defer C.free(unsafe.Pointer(cname))
1706 v.C = C.LLVMBuildPtrDiff(b.C, lhs.C, rhs.C, cname)
1707 return
1708 }
1709
1710 func (b Builder) CreateLandingPad(t Type, personality Value, nclauses int, name string) (l Value) {
1711 cname := C.CString(name)
1712 defer C.free(unsafe.Pointer(cname))
1713 l.C = C.LLVMBuildLandingPad(b.C, t.C, personality.C, C.unsigned(nclauses), cname)
1714 return l
1715 }
1716
1717 func (l Value) AddClause(v Value) {
1718 C.LLVMAddClause(l.C, v.C)
1719 }
1720
1721 func (l Value) SetCleanup(cleanup bool) {
1722 C.LLVMSetCleanup(l.C, boolToLLVMBool(cleanup))
1723 }
1724
1725 func (b Builder) CreateResume(ex Value) (v Value) {
1726 v.C = C.LLVMBuildResume(b.C, ex.C)
1727 return
1728 }
1729
1730 //-------------------------------------------------------------------------
1731 // llvm.ModuleProvider
1732 //-------------------------------------------------------------------------
1733
1734 // Changes the type of M so it can be passed to FunctionPassManagers and the
1735 // JIT. They take ModuleProviders for historical reasons.
1736 func NewModuleProviderForModule(m Module) (mp ModuleProvider) {
1737 mp.C = C.LLVMCreateModuleProviderForExistingModule(m.C)
1738 return
1739 }
1740
1741 // Destroys the module M.
1742 func (mp ModuleProvider) Dispose() { C.LLVMDisposeModuleProvider(mp.C) }
1743
1744 //-------------------------------------------------------------------------
1745 // llvm.MemoryBuffer
1746 //-------------------------------------------------------------------------
1747
1748 func NewMemoryBufferFromFile(path string) (b MemoryBuffer, err error) {
1749 var cmsg *C.char
1750 cpath := C.CString(path)
1751 defer C.free(unsafe.Pointer(cpath))
1752 fail := C.LLVMCreateMemoryBufferWithContentsOfFile(cpath, &b.C, &cmsg)
1753 if fail != 0 {
1754 b.C = nil
1755 err = errors.New(C.GoString(cmsg))
1756