Target.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //===-- Target.cpp --------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the common infrastructure (including C bindings) for
  10. // libLLVMTarget.a, which implements target information.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm-c/Target.h"
  14. #include "llvm-c/Initialization.h"
  15. #include "llvm/Analysis/TargetLibraryInfo.h"
  16. #include "llvm/IR/DataLayout.h"
  17. #include "llvm/IR/LLVMContext.h"
  18. #include "llvm/IR/LegacyPassManager.h"
  19. #include "llvm/IR/Value.h"
  20. #include "llvm/InitializePasses.h"
  21. #include <cstring>
  22. using namespace llvm;
  23. // Avoid including "llvm-c/Core.h" for compile time, fwd-declare this instead.
  24. extern "C" LLVMContextRef LLVMGetGlobalContext(void);
  25. inline TargetLibraryInfoImpl *unwrap(LLVMTargetLibraryInfoRef P) {
  26. return reinterpret_cast<TargetLibraryInfoImpl*>(P);
  27. }
  28. inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfoImpl *P) {
  29. TargetLibraryInfoImpl *X = const_cast<TargetLibraryInfoImpl*>(P);
  30. return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
  31. }
  32. void llvm::initializeTarget(PassRegistry &Registry) {
  33. initializeTargetLibraryInfoWrapperPassPass(Registry);
  34. initializeTargetTransformInfoWrapperPassPass(Registry);
  35. }
  36. void LLVMInitializeTarget(LLVMPassRegistryRef R) {
  37. initializeTarget(*unwrap(R));
  38. }
  39. LLVMTargetDataRef LLVMGetModuleDataLayout(LLVMModuleRef M) {
  40. return wrap(&unwrap(M)->getDataLayout());
  41. }
  42. void LLVMSetModuleDataLayout(LLVMModuleRef M, LLVMTargetDataRef DL) {
  43. unwrap(M)->setDataLayout(*unwrap(DL));
  44. }
  45. LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep) {
  46. return wrap(new DataLayout(StringRep));
  47. }
  48. void LLVMDisposeTargetData(LLVMTargetDataRef TD) {
  49. delete unwrap(TD);
  50. }
  51. void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,
  52. LLVMPassManagerRef PM) {
  53. unwrap(PM)->add(new TargetLibraryInfoWrapperPass(*unwrap(TLI)));
  54. }
  55. char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD) {
  56. std::string StringRep = unwrap(TD)->getStringRepresentation();
  57. return strdup(StringRep.c_str());
  58. }
  59. LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD) {
  60. return unwrap(TD)->isLittleEndian() ? LLVMLittleEndian : LLVMBigEndian;
  61. }
  62. unsigned LLVMPointerSize(LLVMTargetDataRef TD) {
  63. return unwrap(TD)->getPointerSize(0);
  64. }
  65. unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD, unsigned AS) {
  66. return unwrap(TD)->getPointerSize(AS);
  67. }
  68. LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) {
  69. return wrap(unwrap(TD)->getIntPtrType(*unwrap(LLVMGetGlobalContext())));
  70. }
  71. LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS) {
  72. return wrap(unwrap(TD)->getIntPtrType(*unwrap(LLVMGetGlobalContext()), AS));
  73. }
  74. LLVMTypeRef LLVMIntPtrTypeInContext(LLVMContextRef C, LLVMTargetDataRef TD) {
  75. return wrap(unwrap(TD)->getIntPtrType(*unwrap(C)));
  76. }
  77. LLVMTypeRef LLVMIntPtrTypeForASInContext(LLVMContextRef C, LLVMTargetDataRef TD, unsigned AS) {
  78. return wrap(unwrap(TD)->getIntPtrType(*unwrap(C), AS));
  79. }
  80. unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
  81. return unwrap(TD)->getTypeSizeInBits(unwrap(Ty));
  82. }
  83. unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
  84. return unwrap(TD)->getTypeStoreSize(unwrap(Ty));
  85. }
  86. unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
  87. return unwrap(TD)->getTypeAllocSize(unwrap(Ty));
  88. }
  89. unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
  90. return unwrap(TD)->getABITypeAlign(unwrap(Ty)).value();
  91. }
  92. unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
  93. return unwrap(TD)->getABITypeAlign(unwrap(Ty)).value();
  94. }
  95. unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
  96. return unwrap(TD)->getPrefTypeAlign(unwrap(Ty)).value();
  97. }
  98. unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,
  99. LLVMValueRef GlobalVar) {
  100. return unwrap(TD)
  101. ->getPreferredAlign(unwrap<GlobalVariable>(GlobalVar))
  102. .value();
  103. }
  104. unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
  105. unsigned long long Offset) {
  106. StructType *STy = unwrap<StructType>(StructTy);
  107. return unwrap(TD)->getStructLayout(STy)->getElementContainingOffset(Offset);
  108. }
  109. unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
  110. unsigned Element) {
  111. StructType *STy = unwrap<StructType>(StructTy);
  112. return unwrap(TD)->getStructLayout(STy)->getElementOffset(Element);
  113. }