brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.7 KiB · 7387571 Raw
140 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements the common infrastructure (including C bindings) for10// libLLVMTarget.a, which implements target information.11//12//===----------------------------------------------------------------------===//13 14#include "llvm-c/Target.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/Module.h"20#include "llvm/IR/Value.h"21#include "llvm/InitializePasses.h"22#include <cstring>23 24using namespace llvm;25 26// Avoid including "llvm-c/Core.h" for compile time, fwd-declare this instead.27extern "C" LLVMContextRef LLVMGetGlobalContext(void);28 29inline TargetLibraryInfoImpl *unwrap(LLVMTargetLibraryInfoRef P) {30  return reinterpret_cast<TargetLibraryInfoImpl*>(P);31}32 33inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfoImpl *P) {34  TargetLibraryInfoImpl *X = const_cast<TargetLibraryInfoImpl*>(P);35  return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);36}37 38void llvm::initializeTarget(PassRegistry &Registry) {39  initializeTargetLibraryInfoWrapperPassPass(Registry);40  initializeRuntimeLibraryInfoWrapperPass(Registry);41  initializeTargetTransformInfoWrapperPassPass(Registry);42}43 44LLVMTargetDataRef LLVMGetModuleDataLayout(LLVMModuleRef M) {45  return wrap(&unwrap(M)->getDataLayout());46}47 48void LLVMSetModuleDataLayout(LLVMModuleRef M, LLVMTargetDataRef DL) {49  unwrap(M)->setDataLayout(*unwrap(DL));50}51 52LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep) {53  return wrap(new DataLayout(StringRep));54}55 56void LLVMDisposeTargetData(LLVMTargetDataRef TD) {57  delete unwrap(TD);58}59 60void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,61                              LLVMPassManagerRef PM) {62  unwrap(PM)->add(new TargetLibraryInfoWrapperPass(*unwrap(TLI)));63}64 65char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD) {66  std::string StringRep = unwrap(TD)->getStringRepresentation();67  return strdup(StringRep.c_str());68}69 70LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD) {71  return unwrap(TD)->isLittleEndian() ? LLVMLittleEndian : LLVMBigEndian;72}73 74unsigned LLVMPointerSize(LLVMTargetDataRef TD) {75  return unwrap(TD)->getPointerSize(0);76}77 78unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD, unsigned AS) {79  return unwrap(TD)->getPointerSize(AS);80}81 82LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) {83  return wrap(unwrap(TD)->getIntPtrType(*unwrap(LLVMGetGlobalContext())));84}85 86LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS) {87  return wrap(unwrap(TD)->getIntPtrType(*unwrap(LLVMGetGlobalContext()), AS));88}89 90LLVMTypeRef LLVMIntPtrTypeInContext(LLVMContextRef C, LLVMTargetDataRef TD) {91  return wrap(unwrap(TD)->getIntPtrType(*unwrap(C)));92}93 94LLVMTypeRef LLVMIntPtrTypeForASInContext(LLVMContextRef C, LLVMTargetDataRef TD, unsigned AS) {95  return wrap(unwrap(TD)->getIntPtrType(*unwrap(C), AS));96}97 98unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty) {99  return unwrap(TD)->getTypeSizeInBits(unwrap(Ty));100}101 102unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {103  return unwrap(TD)->getTypeStoreSize(unwrap(Ty));104}105 106unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {107  return unwrap(TD)->getTypeAllocSize(unwrap(Ty));108}109 110unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {111  return unwrap(TD)->getABITypeAlign(unwrap(Ty)).value();112}113 114unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {115  return unwrap(TD)->getABITypeAlign(unwrap(Ty)).value();116}117 118unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {119  return unwrap(TD)->getPrefTypeAlign(unwrap(Ty)).value();120}121 122unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,123                                        LLVMValueRef GlobalVar) {124  return unwrap(TD)125      ->getPreferredAlign(unwrap<GlobalVariable>(GlobalVar))126      .value();127}128 129unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy,130                             unsigned long long Offset) {131  StructType *STy = unwrap<StructType>(StructTy);132  return unwrap(TD)->getStructLayout(STy)->getElementContainingOffset(Offset);133}134 135unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD, LLVMTypeRef StructTy,136                                       unsigned Element) {137  StructType *STy = unwrap<StructType>(StructTy);138  return unwrap(TD)->getStructLayout(STy)->getElementOffset(Element);139}140