106 lines · c
1//===- MCJITTestBase.h - Common base class for MCJIT Unit tests ----------===//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 class implements functionality shared by both MCJIT C API tests, and10// the C++ API tests.11//12//===----------------------------------------------------------------------===//13 14#ifndef LLVM_UNITTESTS_EXECUTIONENGINE_MCJIT_MCJITTESTAPICOMMON_H15#define LLVM_UNITTESTS_EXECUTIONENGINE_MCJIT_MCJITTESTAPICOMMON_H16 17#include "llvm/ADT/STLExtras.h"18#include "llvm/ADT/SmallVector.h"19#include "llvm/IR/LegacyPassManager.h"20#include "llvm/InitializePasses.h"21#include "llvm/MC/TargetRegistry.h"22#include "llvm/PassRegistry.h"23#include "llvm/Support/TargetSelect.h"24#include "llvm/TargetParser/Host.h"25#include "llvm/TargetParser/Triple.h"26 27// Used to skip tests on unsupported architectures and operating systems.28// To skip a test, add this macro at the top of a test-case in a suite that29// inherits from MCJITTestBase. See MCJITTest.cpp for examples.30#define SKIP_UNSUPPORTED_PLATFORM \31 do \32 if (!ArchSupportsMCJIT() || !OSSupportsMCJIT() || !HostCanBeTargeted()) \33 GTEST_SKIP(); \34 while(0)35 36namespace llvm {37 38class MCJITTestAPICommon {39protected:40 MCJITTestAPICommon() : HostTripleName(sys::getProcessTriple()) {41 InitializeNativeTarget();42 InitializeNativeTargetAsmPrinter();43 44 // FIXME: It isn't at all clear why this is necesasry, but without it we45 // fail to initialize the AssumptionCacheTracker.46 initializeAssumptionCacheTrackerPass(*PassRegistry::getPassRegistry());47 48#ifdef _WIN3249 // On Windows, generate ELF objects by specifying "-elf" in triple50 HostTripleName += "-elf";51#endif // _WIN3252 HostTripleName = Triple::normalize(HostTripleName);53 HostTriple = Triple(HostTripleName);54 }55 56 bool HostCanBeTargeted() {57 std::string Error;58 return TargetRegistry::lookupTarget(HostTriple, Error) != nullptr;59 }60 61 /// Returns true if the host architecture is known to support MCJIT62 bool ArchSupportsMCJIT() {63 Triple Host(HostTriple);64 // If ARCH is not supported, bail65 if (!is_contained(SupportedArchs, Host.getArch()))66 return false;67 68 // If ARCH is supported and has no specific sub-arch support69 if (!is_contained(HasSubArchs, Host.getArch()))70 return true;71 72 // If ARCH has sub-arch support, find it73 SmallVectorImpl<std::string>::const_iterator I = SupportedSubArchs.begin();74 for(; I != SupportedSubArchs.end(); ++I)75 if (Host.getArchName().starts_with(*I))76 return true;77 78 return false;79 }80 81 /// Returns true if the host OS is known to support MCJIT82 bool OSSupportsMCJIT() {83 if (find(UnsupportedEnvironments, HostTriple.getEnvironment()) !=84 UnsupportedEnvironments.end())85 return false;86 87 if (!is_contained(UnsupportedOSs, HostTriple.getOS()))88 return true;89 90 return false;91 }92 93 std::string HostTripleName;94 Triple HostTriple;95 SmallVector<Triple::ArchType, 4> SupportedArchs;96 SmallVector<Triple::ArchType, 1> HasSubArchs;97 SmallVector<std::string, 2> SupportedSubArchs; // We need to own the memory98 SmallVector<Triple::OSType, 4> UnsupportedOSs;99 SmallVector<Triple::EnvironmentType, 1> UnsupportedEnvironments;100};101 102} // namespace llvm103 104#endif105 106