Taints the non-acquire RMW's store address with the load part
[oota-llvm.git] / bindings / go / llvm / target.go
1 //===- target.go - Bindings for target ------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines bindings for the target component.
11 //
12 //===----------------------------------------------------------------------===//
13
14 package llvm
15
16 /*
17 #include "llvm-c/Core.h"
18 #include "llvm-c/Target.h"
19 #include "llvm-c/TargetMachine.h"
20 #include <stdlib.h>
21 */
22 import "C"
23 import "unsafe"
24 import "errors"
25
26 type (
27         TargetData struct {
28                 C C.LLVMTargetDataRef
29         }
30         Target struct {
31                 C C.LLVMTargetRef
32         }
33         TargetMachine struct {
34                 C C.LLVMTargetMachineRef
35         }
36         ByteOrdering    C.enum_LLVMByteOrdering
37         RelocMode       C.LLVMRelocMode
38         CodeGenOptLevel C.LLVMCodeGenOptLevel
39         CodeGenFileType C.LLVMCodeGenFileType
40         CodeModel       C.LLVMCodeModel
41 )
42
43 const (
44         BigEndian    ByteOrdering = C.LLVMBigEndian
45         LittleEndian ByteOrdering = C.LLVMLittleEndian
46 )
47
48 const (
49         RelocDefault      RelocMode = C.LLVMRelocDefault
50         RelocStatic       RelocMode = C.LLVMRelocStatic
51         RelocPIC          RelocMode = C.LLVMRelocPIC
52         RelocDynamicNoPic RelocMode = C.LLVMRelocDynamicNoPic
53 )
54
55 const (
56         CodeGenLevelNone       CodeGenOptLevel = C.LLVMCodeGenLevelNone
57         CodeGenLevelLess       CodeGenOptLevel = C.LLVMCodeGenLevelLess
58         CodeGenLevelDefault    CodeGenOptLevel = C.LLVMCodeGenLevelDefault
59         CodeGenLevelAggressive CodeGenOptLevel = C.LLVMCodeGenLevelAggressive
60 )
61
62 const (
63         CodeModelDefault    CodeModel = C.LLVMCodeModelDefault
64         CodeModelJITDefault CodeModel = C.LLVMCodeModelJITDefault
65         CodeModelSmall      CodeModel = C.LLVMCodeModelSmall
66         CodeModelKernel     CodeModel = C.LLVMCodeModelKernel
67         CodeModelMedium     CodeModel = C.LLVMCodeModelMedium
68         CodeModelLarge      CodeModel = C.LLVMCodeModelLarge
69 )
70
71 const (
72         AssemblyFile CodeGenFileType = C.LLVMAssemblyFile
73         ObjectFile   CodeGenFileType = C.LLVMObjectFile
74 )
75
76 // InitializeAllTargetInfos - The main program should call this function if it
77 // wants access to all available targets that LLVM is configured to support.
78 func InitializeAllTargetInfos() { C.LLVMInitializeAllTargetInfos() }
79
80 // InitializeAllTargets - The main program should call this function if it wants
81 // to link in all available targets that LLVM is configured to support.
82 func InitializeAllTargets() { C.LLVMInitializeAllTargets() }
83
84 func InitializeAllTargetMCs() { C.LLVMInitializeAllTargetMCs() }
85
86 func InitializeAllAsmParsers() { C.LLVMInitializeAllAsmParsers() }
87
88 func InitializeAllAsmPrinters() { C.LLVMInitializeAllAsmPrinters() }
89
90 var initializeNativeTargetError = errors.New("Failed to initialize native target")
91
92 // InitializeNativeTarget - The main program should call this function to
93 // initialize the native target corresponding to the host. This is useful
94 // for JIT applications to ensure that the target gets linked in correctly.
95 func InitializeNativeTarget() error {
96         fail := C.LLVMInitializeNativeTarget()
97         if fail != 0 {
98                 return initializeNativeTargetError
99         }
100         return nil
101 }
102
103 func InitializeNativeAsmPrinter() error {
104         fail := C.LLVMInitializeNativeAsmPrinter()
105         if fail != 0 {
106                 return initializeNativeTargetError
107         }
108         return nil
109 }
110
111 //-------------------------------------------------------------------------
112 // llvm.TargetData
113 //-------------------------------------------------------------------------
114
115 // Creates target data from a target layout string.
116 // See the constructor llvm::TargetData::TargetData.
117 func NewTargetData(rep string) (td TargetData) {
118         crep := C.CString(rep)
119         defer C.free(unsafe.Pointer(crep))
120         td.C = C.LLVMCreateTargetData(crep)
121         return
122 }
123
124 // Adds target data information to a pass manager. This does not take ownership
125 // of the target data.
126 // See the method llvm::PassManagerBase::add.
127 func (pm PassManager) Add(td TargetData) {
128         C.LLVMAddTargetData(td.C, pm.C)
129 }
130
131 // Converts target data to a target layout string. The string must be disposed
132 // with LLVMDisposeMessage.
133 // See the constructor llvm::TargetData::TargetData.
134 func (td TargetData) String() (s string) {
135         cmsg := C.LLVMCopyStringRepOfTargetData(td.C)
136         s = C.GoString(cmsg)
137         C.LLVMDisposeMessage(cmsg)
138         return
139 }
140
141 // Returns the byte order of a target, either BigEndian or LittleEndian.
142 // See the method llvm::TargetData::isLittleEndian.
143 func (td TargetData) ByteOrder() ByteOrdering { return ByteOrdering(C.LLVMByteOrder(td.C)) }
144
145 // Returns the pointer size in bytes for a target.
146 // See the method llvm::TargetData::getPointerSize.
147 func (td TargetData) PointerSize() int { return int(C.LLVMPointerSize(td.C)) }
148
149 // Returns the integer type that is the same size as a pointer on a target.
150 // See the method llvm::TargetData::getIntPtrType.
151 func (td TargetData) IntPtrType() (t Type) { t.C = C.LLVMIntPtrType(td.C); return }
152
153 // Computes the size of a type in bytes for a target.
154 // See the method llvm::TargetData::getTypeSizeInBits.
155 func (td TargetData) TypeSizeInBits(t Type) uint64 {
156         return uint64(C.LLVMSizeOfTypeInBits(td.C, t.C))
157 }
158
159 // Computes the storage size of a type in bytes for a target.
160 // See the method llvm::TargetData::getTypeStoreSize.
161 func (td TargetData) TypeStoreSize(t Type) uint64 {
162         return uint64(C.LLVMStoreSizeOfType(td.C, t.C))
163 }
164
165 // Computes the ABI size of a type in bytes for a target.
166 // See the method llvm::TargetData::getTypeAllocSize.
167 func (td TargetData) TypeAllocSize(t Type) uint64 {
168         return uint64(C.LLVMABISizeOfType(td.C, t.C))
169 }
170
171 // Computes the ABI alignment of a type in bytes for a target.
172 // See the method llvm::TargetData::getABITypeAlignment.
173 func (td TargetData) ABITypeAlignment(t Type) int {
174         return int(C.LLVMABIAlignmentOfType(td.C, t.C))
175 }
176
177 // Computes the call frame alignment of a type in bytes for a target.
178 // See the method llvm::TargetData::getCallFrameTypeAlignment.
179 func (td TargetData) CallFrameTypeAlignment(t Type) int {
180         return int(C.LLVMCallFrameAlignmentOfType(td.C, t.C))
181 }
182
183 // Computes the preferred alignment of a type in bytes for a target.
184 // See the method llvm::TargetData::getPrefTypeAlignment.
185 func (td TargetData) PrefTypeAlignment(t Type) int {
186         return int(C.LLVMPreferredAlignmentOfType(td.C, t.C))
187 }
188
189 // Computes the preferred alignment of a global variable in bytes for a target.
190 // See the method llvm::TargetData::getPreferredAlignment.
191 func (td TargetData) PreferredAlignment(g Value) int {
192         return int(C.LLVMPreferredAlignmentOfGlobal(td.C, g.C))
193 }
194
195 // Computes the structure element that contains the byte offset for a target.
196 // See the method llvm::StructLayout::getElementContainingOffset.
197 func (td TargetData) ElementContainingOffset(t Type, offset uint64) int {
198         return int(C.LLVMElementAtOffset(td.C, t.C, C.ulonglong(offset)))
199 }
200
201 // Computes the byte offset of the indexed struct element for a target.
202 // See the method llvm::StructLayout::getElementOffset.
203 func (td TargetData) ElementOffset(t Type, element int) uint64 {
204         return uint64(C.LLVMOffsetOfElement(td.C, t.C, C.unsigned(element)))
205 }
206
207 // Deallocates a TargetData.
208 // See the destructor llvm::TargetData::~TargetData.
209 func (td TargetData) Dispose() { C.LLVMDisposeTargetData(td.C) }
210
211 //-------------------------------------------------------------------------
212 // llvm.Target
213 //-------------------------------------------------------------------------
214
215 func FirstTarget() Target {
216         return Target{C.LLVMGetFirstTarget()}
217 }
218
219 func (t Target) NextTarget() Target {
220         return Target{C.LLVMGetNextTarget(t.C)}
221 }
222
223 func GetTargetFromTriple(triple string) (t Target, err error) {
224         var errstr *C.char
225         ctriple := C.CString(triple)
226         defer C.free(unsafe.Pointer(ctriple))
227         fail := C.LLVMGetTargetFromTriple(ctriple, &t.C, &errstr)
228         if fail != 0 {
229                 err = errors.New(C.GoString(errstr))
230                 C.free(unsafe.Pointer(errstr))
231         }
232         return
233 }
234
235 func (t Target) Name() string {
236         return C.GoString(C.LLVMGetTargetName(t.C))
237 }
238
239 func (t Target) Description() string {
240         return C.GoString(C.LLVMGetTargetDescription(t.C))
241 }
242
243 //-------------------------------------------------------------------------
244 // llvm.TargetMachine
245 //-------------------------------------------------------------------------
246
247 // CreateTargetMachine creates a new TargetMachine.
248 func (t Target) CreateTargetMachine(Triple string, CPU string, Features string,
249         Level CodeGenOptLevel, Reloc RelocMode,
250         CodeModel CodeModel) (tm TargetMachine) {
251         cTriple := C.CString(Triple)
252         defer C.free(unsafe.Pointer(cTriple))
253         cCPU := C.CString(CPU)
254         defer C.free(unsafe.Pointer(cCPU))
255         cFeatures := C.CString(Features)
256         defer C.free(unsafe.Pointer(cFeatures))
257         tm.C = C.LLVMCreateTargetMachine(t.C, cTriple, cCPU, cFeatures,
258                 C.LLVMCodeGenOptLevel(Level),
259                 C.LLVMRelocMode(Reloc),
260                 C.LLVMCodeModel(CodeModel))
261         return
262 }
263
264 // Triple returns the triple describing the machine (arch-vendor-os).
265 func (tm TargetMachine) Triple() string {
266         cstr := C.LLVMGetTargetMachineTriple(tm.C)
267         return C.GoString(cstr)
268 }
269
270 // TargetData returns the TargetData for the machine.
271 func (tm TargetMachine) TargetData() TargetData {
272         return TargetData{C.LLVMGetTargetMachineData(tm.C)}
273 }
274
275 func (tm TargetMachine) EmitToMemoryBuffer(m Module, ft CodeGenFileType) (MemoryBuffer, error) {
276         var errstr *C.char
277         var mb MemoryBuffer
278         fail := C.LLVMTargetMachineEmitToMemoryBuffer(tm.C, m.C, C.LLVMCodeGenFileType(ft), &errstr, &mb.C)
279         if fail != 0 {
280                 err := errors.New(C.GoString(errstr))
281                 C.free(unsafe.Pointer(errstr))
282                 return MemoryBuffer{}, err
283         }
284         return mb, nil
285 }
286
287 func (tm TargetMachine) AddAnalysisPasses(pm PassManager) {
288         C.LLVMAddAnalysisPasses(tm.C, pm.C)
289 }
290
291 // Dispose releases resources related to the TargetMachine.
292 func (tm TargetMachine) Dispose() {
293         C.LLVMDisposeTargetMachine(tm.C)
294 }
295
296 func DefaultTargetTriple() (triple string) {
297         cTriple := C.LLVMGetDefaultTargetTriple()
298         defer C.free(unsafe.Pointer(cTriple))
299         triple = C.GoString(cTriple)
300         return
301 }