blob: 21def6e9eb1bbd1e303672f50fb65123ac04049a [file] [log] [blame]
Andrew Kaylor31be5ef2013-04-29 17:49:40 +00001//===- MCJITTestBase.h - Common base class for MCJIT Unit tests ----------===//
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 class implements functionality shared by both MCJIT C API tests, and
11// the C++ API tests.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_UNITTESTS_EXECUTIONENGINE_MCJIT_MCJITTESTAPICOMMON_H
16#define LLVM_UNITTESTS_EXECUTIONENGINE_MCJIT_MCJITTESTAPICOMMON_H
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000017
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/Triple.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000020#include "llvm/IR/LegacyPassManager.h"
21#include "llvm/InitializePasses.h"
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000022#include "llvm/Support/Host.h"
23#include "llvm/Support/TargetSelect.h"
24
25// Used to skip tests on unsupported architectures and operating systems.
26// To skip a test, add this macro at the top of a test-case in a suite that
27// inherits from MCJITTestBase. See MCJITTest.cpp for examples.
28#define SKIP_UNSUPPORTED_PLATFORM \
29 do \
30 if (!ArchSupportsMCJIT() || !OSSupportsMCJIT()) \
31 return; \
32 while(0)
33
34namespace llvm {
35
36class MCJITTestAPICommon {
37protected:
38 MCJITTestAPICommon()
39 : HostTriple(sys::getProcessTriple())
40 {
41 InitializeNativeTarget();
42 InitializeNativeTargetAsmPrinter();
43
Chandler Carruth7b560d42015-09-09 17:55:00 +000044 // FIXME: It isn't at all clear why this is necesasry, but without it we
45 // fail to initialize the AssumptionCacheTracker.
46 initializeAssumptionCacheTrackerPass(*PassRegistry::getPassRegistry());
47
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000048#ifdef LLVM_ON_WIN32
49 // On Windows, generate ELF objects by specifying "-elf" in triple
50 HostTriple += "-elf";
51#endif // LLVM_ON_WIN32
52 HostTriple = Triple::normalize(HostTriple);
53 }
54
55 /// Returns true if the host architecture is known to support MCJIT
56 bool ArchSupportsMCJIT() {
57 Triple Host(HostTriple);
Renato Golincf6979d2013-05-19 20:10:10 +000058 // If ARCH is not supported, bail
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000059 if (std::find(SupportedArchs.begin(), SupportedArchs.end(), Host.getArch())
Renato Golincf6979d2013-05-19 20:10:10 +000060 == SupportedArchs.end())
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000061 return false;
Renato Golincf6979d2013-05-19 20:10:10 +000062
63 // If ARCH is supported and has no specific sub-arch support
64 if (std::find(HasSubArchs.begin(), HasSubArchs.end(), Host.getArch())
65 == HasSubArchs.end())
66 return true;
67
68 // If ARCH has sub-arch support, find it
69 SmallVectorImpl<std::string>::const_iterator I = SupportedSubArchs.begin();
70 for(; I != SupportedSubArchs.end(); ++I)
71 if (Host.getArchName().startswith(I->c_str()))
72 return true;
73
74 return false;
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000075 }
76
77 /// Returns true if the host OS is known to support MCJIT
78 bool OSSupportsMCJIT() {
79 Triple Host(HostTriple);
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +000080
81 if (std::find(UnsupportedEnvironments.begin(), UnsupportedEnvironments.end(),
82 Host.getEnvironment()) != UnsupportedEnvironments.end())
83 return false;
84
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000085 if (std::find(UnsupportedOSs.begin(), UnsupportedOSs.end(), Host.getOS())
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +000086 == UnsupportedOSs.end())
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000087 return true;
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +000088
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000089 return false;
90 }
91
92 std::string HostTriple;
93 SmallVector<Triple::ArchType, 4> SupportedArchs;
Renato Golincf6979d2013-05-19 20:10:10 +000094 SmallVector<Triple::ArchType, 1> HasSubArchs;
95 SmallVector<std::string, 2> SupportedSubArchs; // We need to own the memory
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000096 SmallVector<Triple::OSType, 4> UnsupportedOSs;
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +000097 SmallVector<Triple::EnvironmentType, 1> UnsupportedEnvironments;
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000098};
99
100} // namespace llvm
101
Benjamin Kramera7c40ef2014-08-13 16:26:38 +0000102#endif
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000103