LLVM API Documentation

Target.cpp
Go to the documentation of this file.
00001 //===-- Target.cpp --------------------------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the common infrastructure (including C bindings) for 
00011 // libLLVMTarget.a, which implements target information.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm-c/Target.h"
00016 #include "llvm-c/Initialization.h"
00017 #include "llvm/IR/DataLayout.h"
00018 #include "llvm/IR/LLVMContext.h"
00019 #include "llvm/IR/Value.h"
00020 #include "llvm/InitializePasses.h"
00021 #include "llvm/PassManager.h"
00022 #include "llvm/Target/TargetLibraryInfo.h"
00023 #include <cstring>
00024 
00025 using namespace llvm;
00026 
00027 inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
00028   return reinterpret_cast<TargetLibraryInfo*>(P);
00029 }
00030 
00031 inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
00032   TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
00033   return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
00034 }
00035 
00036 void llvm::initializeTarget(PassRegistry &Registry) {
00037   initializeDataLayoutPassPass(Registry);
00038   initializeTargetLibraryInfoPass(Registry);
00039 }
00040 
00041 void LLVMInitializeTarget(LLVMPassRegistryRef R) {
00042   initializeTarget(*unwrap(R));
00043 }
00044 
00045 LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep) {
00046   return wrap(new DataLayout(StringRep));
00047 }
00048 
00049 void LLVMAddTargetData(LLVMTargetDataRef TD, LLVMPassManagerRef PM) {
00050   // The DataLayoutPass must now be in sync with the module. Unfortunatelly we
00051   // cannot enforce that from the C api.
00052   unwrap(PM)->add(new DataLayoutPass());
00053 }
00054 
00055 void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,
00056                               LLVMPassManagerRef PM) {
00057   unwrap(PM)->add(new TargetLibraryInfo(*unwrap(TLI)));
00058 }
00059 
00060 char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD) {
00061   std::string StringRep = unwrap(TD)->getStringRepresentation();
00062   return strdup(StringRep.c_str());
00063 }
00064 
00065 LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD) {
00066   return unwrap(TD)->isLittleEndian() ? LLVMLittleEndian : LLVMBigEndian;
00067 }
00068 
00069 unsigned LLVMPointerSize(LLVMTargetDataRef TD) {
00070   return unwrap(TD)->getPointerSize(0);
00071 }
00072 
00073 unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD, unsigned AS) {
00074   return unwrap(TD)->getPointerSize(AS);
00075 }
00076 
00077 LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) {
00078   return wrap(unwrap(TD)->getIntPtrType(getGlobalContext()));
00079 }
00080 
00081 LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS) {
00082   return wrap(unwrap(TD)->getIntPtrType(getGlobalContext(), AS));
00083 }
00084 
00085 LLVMTypeRef LLVMIntPtrTypeInContext(LLVMContextRef C, LLVMTargetDataRef TD) {
00086   return wrap(unwrap(TD)->getIntPtrType(*unwrap(C)));
00087 }
00088 
00089 LLVMTypeRef LLVMIntPtrTypeForASInContext(LLVMContextRef C, LLVMTargetDataRef TD, unsigned AS) {
00090   return wrap(unwrap(TD)->getIntPtrType(*unwrap(C), AS));
00091 }
00092 
00093 unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
00094   return unwrap(TD)->getTypeSizeInBits(unwrap(Ty));
00095 }
00096 
00097 unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
00098   return unwrap(TD)->getTypeStoreSize(unwrap(Ty));
00099 }
00100 
00101 unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
00102   return unwrap(TD)->getTypeAllocSize(unwrap(Ty));
00103 }
00104 
00105 unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
00106   return unwrap(TD)->getABITypeAlignment(unwrap(Ty));
00107 }
00108 
00109 unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
00110   return unwrap(TD)->getABITypeAlignment(unwrap(Ty));
00111 }
00112 
00113 unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
00114   return unwrap(TD)->getPrefTypeAlignment(unwrap(Ty));
00115 }
00116 
00117 unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,
00118                                         LLVMValueRef GlobalVar) {
00119   return unwrap(TD)->getPreferredAlignment(unwrap<GlobalVariable>(GlobalVar));
00120 }
00121 
00122 unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
00123                              unsigned long long Offset) {
00124   StructType *STy = unwrap<StructType>(StructTy);
00125   return unwrap(TD)->getStructLayout(STy)->getElementContainingOffset(Offset);
00126 }
00127 
00128 unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
00129                                        unsigned Element) {
00130   StructType *STy = unwrap<StructType>(StructTy);
00131   return unwrap(TD)->getStructLayout(STy)->getElementOffset(Element);
00132 }
00133 
00134 void LLVMDisposeTargetData(LLVMTargetDataRef TD) {
00135   delete unwrap(TD);
00136 }