blob: a3d3d1fffdbef5a161a82d94081fc11988f38a3f [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
David Majnemer0d955d02016-08-11 22:21:41 +000018#include "llvm/ADT/STLExtras.h"
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000019#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/Triple.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000021#include "llvm/IR/LegacyPassManager.h"
22#include "llvm/InitializePasses.h"
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000023#include "llvm/Support/Host.h"
24#include "llvm/Support/TargetSelect.h"
25
26// Used to skip tests on unsupported architectures and operating systems.
27// To skip a test, add this macro at the top of a test-case in a suite that
28// inherits from MCJITTestBase. See MCJITTest.cpp for examples.
29#define SKIP_UNSUPPORTED_PLATFORM \
30 do \
31 if (!ArchSupportsMCJIT() || !OSSupportsMCJIT()) \
32 return; \
33 while(0)
34
35namespace llvm {
36
37class MCJITTestAPICommon {
38protected:
39 MCJITTestAPICommon()
40 : HostTriple(sys::getProcessTriple())
41 {
42 InitializeNativeTarget();
43 InitializeNativeTargetAsmPrinter();
44
Daniel Jasperaec2fa32016-12-19 08:22:17 +000045 // FIXME: It isn't at all clear why this is necesasry, but without it we
46 // fail to initialize the AssumptionCacheTracker.
47 initializeAssumptionCacheTrackerPass(*PassRegistry::getPassRegistry());
48
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000049#ifdef LLVM_ON_WIN32
50 // On Windows, generate ELF objects by specifying "-elf" in triple
51 HostTriple += "-elf";
52#endif // LLVM_ON_WIN32
53 HostTriple = Triple::normalize(HostTriple);
54 }
55
56 /// Returns true if the host architecture is known to support MCJIT
57 bool ArchSupportsMCJIT() {
58 Triple Host(HostTriple);
Renato Golincf6979d2013-05-19 20:10:10 +000059 // If ARCH is not supported, bail
David Majnemer0d955d02016-08-11 22:21:41 +000060 if (!is_contained(SupportedArchs, Host.getArch()))
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
David Majnemer0d955d02016-08-11 22:21:41 +000064 if (!is_contained(HasSubArchs, Host.getArch()))
Renato Golincf6979d2013-05-19 20:10:10 +000065 return true;
66
67 // If ARCH has sub-arch support, find it
68 SmallVectorImpl<std::string>::const_iterator I = SupportedSubArchs.begin();
69 for(; I != SupportedSubArchs.end(); ++I)
Malcolm Parsons06ac79c2016-11-02 16:43:50 +000070 if (Host.getArchName().startswith(*I))
Renato Golincf6979d2013-05-19 20:10:10 +000071 return true;
72
73 return false;
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000074 }
75
76 /// Returns true if the host OS is known to support MCJIT
77 bool OSSupportsMCJIT() {
78 Triple Host(HostTriple);
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +000079
David Majnemer0d955d02016-08-11 22:21:41 +000080 if (find(UnsupportedEnvironments, Host.getEnvironment()) !=
81 UnsupportedEnvironments.end())
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +000082 return false;
83
David Majnemer0d955d02016-08-11 22:21:41 +000084 if (!is_contained(UnsupportedOSs, Host.getOS()))
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000085 return true;
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +000086
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000087 return false;
88 }
89
90 std::string HostTriple;
91 SmallVector<Triple::ArchType, 4> SupportedArchs;
Renato Golincf6979d2013-05-19 20:10:10 +000092 SmallVector<Triple::ArchType, 1> HasSubArchs;
93 SmallVector<std::string, 2> SupportedSubArchs; // We need to own the memory
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000094 SmallVector<Triple::OSType, 4> UnsupportedOSs;
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +000095 SmallVector<Triple::EnvironmentType, 1> UnsupportedEnvironments;
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000096};
97
98} // namespace llvm
99
Benjamin Kramera7c40ef2014-08-13 16:26:38 +0000100#endif
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000101