1 //===- target.go - Bindings for target ------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines bindings for the target component.
12 //===----------------------------------------------------------------------===//
17 #include "llvm-c/Core.h"
18 #include "llvm-c/Target.h"
19 #include "llvm-c/TargetMachine.h"
33 TargetMachine struct {
34 C C.LLVMTargetMachineRef
36 ByteOrdering C.enum_LLVMByteOrdering
37 RelocMode C.LLVMRelocMode
38 CodeGenOptLevel C.LLVMCodeGenOptLevel
39 CodeGenFileType C.LLVMCodeGenFileType
40 CodeModel C.LLVMCodeModel
44 BigEndian ByteOrdering = C.LLVMBigEndian
45 LittleEndian ByteOrdering = C.LLVMLittleEndian
49 RelocDefault RelocMode = C.LLVMRelocDefault
50 RelocStatic RelocMode = C.LLVMRelocStatic
51 RelocPIC RelocMode = C.LLVMRelocPIC
52 RelocDynamicNoPic RelocMode = C.LLVMRelocDynamicNoPic
56 CodeGenLevelNone CodeGenOptLevel = C.LLVMCodeGenLevelNone
57 CodeGenLevelLess CodeGenOptLevel = C.LLVMCodeGenLevelLess
58 CodeGenLevelDefault CodeGenOptLevel = C.LLVMCodeGenLevelDefault
59 CodeGenLevelAggressive CodeGenOptLevel = C.LLVMCodeGenLevelAggressive
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
72 AssemblyFile CodeGenFileType = C.LLVMAssemblyFile
73 ObjectFile CodeGenFileType = C.LLVMObjectFile
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() }
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() }
84 func InitializeAllTargetMCs() { C.LLVMInitializeAllTargetMCs() }
86 func InitializeAllAsmParsers() { C.LLVMInitializeAllAsmParsers() }
88 func InitializeAllAsmPrinters() { C.LLVMInitializeAllAsmPrinters() }
90 var initializeNativeTargetError = errors.New("Failed to initialize native target")
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()
98 return initializeNativeTargetError
103 func InitializeNativeAsmPrinter() error {
104 fail := C.LLVMInitializeNativeAsmPrinter()
106 return initializeNativeTargetError
111 //-------------------------------------------------------------------------
113 //-------------------------------------------------------------------------
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)
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)
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)
137 C.LLVMDisposeMessage(cmsg)
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)) }
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)) }
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 }
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))
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))
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))
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))
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))
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))
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))
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)))
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)))
207 // Deallocates a TargetData.
208 // See the destructor llvm::TargetData::~TargetData.
209 func (td TargetData) Dispose() { C.LLVMDisposeTargetData(td.C) }
211 //-------------------------------------------------------------------------
213 //-------------------------------------------------------------------------
215 func FirstTarget() Target {
216 return Target{C.LLVMGetFirstTarget()}
219 func (t Target) NextTarget() Target {
220 return Target{C.LLVMGetNextTarget(t.C)}
223 func GetTargetFromTriple(triple string) (t Target, err error) {
225 ctriple := C.CString(triple)
226 defer C.free(unsafe.Pointer(ctriple))
227 fail := C.LLVMGetTargetFromTriple(ctriple, &t.C, &errstr)
229 err = errors.New(C.GoString(errstr))
230 C.free(unsafe.Pointer(errstr))
235 func (t Target) Name() string {
236 return C.GoString(C.LLVMGetTargetName(t.C))
239 func (t Target) Description() string {
240 return C.GoString(C.LLVMGetTargetDescription(t.C))
243 //-------------------------------------------------------------------------
244 // llvm.TargetMachine
245 //-------------------------------------------------------------------------
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))
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)
270 // TargetData returns the TargetData for the machine.
271 func (tm TargetMachine) TargetData() TargetData {
272 return TargetData{C.LLVMGetTargetMachineData(tm.C)}
275 func (tm TargetMachine) EmitToMemoryBuffer(m Module, ft CodeGenFileType) (MemoryBuffer, error) {
278 fail := C.LLVMTargetMachineEmitToMemoryBuffer(tm.C, m.C, C.LLVMCodeGenFileType(ft), &errstr, &mb.C)
280 err := errors.New(C.GoString(errstr))
281 C.free(unsafe.Pointer(errstr))
282 return MemoryBuffer{}, err
287 func (tm TargetMachine) AddAnalysisPasses(pm PassManager) {
288 C.LLVMAddAnalysisPasses(tm.C, pm.C)
291 // Dispose releases resources related to the TargetMachine.
292 func (tm TargetMachine) Dispose() {
293 C.LLVMDisposeTargetMachine(tm.C)
296 func DefaultTargetTriple() (triple string) {
297 cTriple := C.LLVMGetDefaultTargetTriple()
298 defer C.free(unsafe.Pointer(cTriple))
299 triple = C.GoString(cTriple)