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