Home | History | Annotate | Download | only in Target
      1 //===-- Target.cpp --------------------------------------------------------===//
      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 implements the common infrastructure (including C bindings) for
     11 // libLLVMTarget.a, which implements target information.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "llvm-c/Target.h"
     16 #include "llvm-c/Initialization.h"
     17 #include "llvm/InitializePasses.h"
     18 #include "llvm/PassManager.h"
     19 #include "llvm/Target/TargetData.h"
     20 #include "llvm/Target/TargetLibraryInfo.h"
     21 #include "llvm/LLVMContext.h"
     22 #include <cstring>
     23 
     24 using namespace llvm;
     25 
     26 void llvm::initializeTarget(PassRegistry &Registry) {
     27   initializeTargetDataPass(Registry);
     28   initializeTargetLibraryInfoPass(Registry);
     29 }
     30 
     31 void LLVMInitializeTarget(LLVMPassRegistryRef R) {
     32   initializeTarget(*unwrap(R));
     33 }
     34 
     35 LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep) {
     36   return wrap(new TargetData(StringRep));
     37 }
     38 
     39 void LLVMAddTargetData(LLVMTargetDataRef TD, LLVMPassManagerRef PM) {
     40   unwrap(PM)->add(new TargetData(*unwrap(TD)));
     41 }
     42 
     43 void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,
     44                               LLVMPassManagerRef PM) {
     45   unwrap(PM)->add(new TargetLibraryInfo(*unwrap(TLI)));
     46 }
     47 
     48 char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD) {
     49   std::string StringRep = unwrap(TD)->getStringRepresentation();
     50   return strdup(StringRep.c_str());
     51 }
     52 
     53 LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD) {
     54   return unwrap(TD)->isLittleEndian() ? LLVMLittleEndian : LLVMBigEndian;
     55 }
     56 
     57 unsigned LLVMPointerSize(LLVMTargetDataRef TD) {
     58   return unwrap(TD)->getPointerSize();
     59 }
     60 
     61 LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) {
     62   return wrap(unwrap(TD)->getIntPtrType(getGlobalContext()));
     63 }
     64 
     65 unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
     66   return unwrap(TD)->getTypeSizeInBits(unwrap(Ty));
     67 }
     68 
     69 unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
     70   return unwrap(TD)->getTypeStoreSize(unwrap(Ty));
     71 }
     72 
     73 unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
     74   return unwrap(TD)->getTypeAllocSize(unwrap(Ty));
     75 }
     76 
     77 unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
     78   return unwrap(TD)->getABITypeAlignment(unwrap(Ty));
     79 }
     80 
     81 unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
     82   return unwrap(TD)->getCallFrameTypeAlignment(unwrap(Ty));
     83 }
     84 
     85 unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
     86   return unwrap(TD)->getPrefTypeAlignment(unwrap(Ty));
     87 }
     88 
     89 unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,
     90                                         LLVMValueRef GlobalVar) {
     91   return unwrap(TD)->getPreferredAlignment(unwrap<GlobalVariable>(GlobalVar));
     92 }
     93 
     94 unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
     95                              unsigned long long Offset) {
     96   StructType *STy = unwrap<StructType>(StructTy);
     97   return unwrap(TD)->getStructLayout(STy)->getElementContainingOffset(Offset);
     98 }
     99 
    100 unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
    101                                        unsigned Element) {
    102   StructType *STy = unwrap<StructType>(StructTy);
    103   return unwrap(TD)->getStructLayout(STy)->getElementOffset(Element);
    104 }
    105 
    106 void LLVMDisposeTargetData(LLVMTargetDataRef TD) {
    107   delete unwrap(TD);
    108 }
    109