1 //===- 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 
15 #ifndef LLVM_UNITTESTS_EXECUTIONENGINE_MCJIT_MCJITTESTAPICOMMON_H
16 #define LLVM_UNITTESTS_EXECUTIONENGINE_MCJIT_MCJITTESTAPICOMMON_H
17 
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/Support/Host.h"
21 #include "llvm/Support/TargetSelect.h"
22 
23 // Used to skip tests on unsupported architectures and operating systems.
24 // To skip a test, add this macro at the top of a test-case in a suite that
25 // inherits from MCJITTestBase. See MCJITTest.cpp for examples.
26 #define SKIP_UNSUPPORTED_PLATFORM \
27   do \
28     if (!ArchSupportsMCJIT() || !OSSupportsMCJIT()) \
29       return; \
30   while(0)
31 
32 namespace llvm {
33 
34 class MCJITTestAPICommon {
35 protected:
MCJITTestAPICommon()36   MCJITTestAPICommon()
37     : HostTriple(sys::getProcessTriple())
38   {
39     InitializeNativeTarget();
40     InitializeNativeTargetAsmPrinter();
41 
42 #ifdef LLVM_ON_WIN32
43     // On Windows, generate ELF objects by specifying "-elf" in triple
44     HostTriple += "-elf";
45 #endif // LLVM_ON_WIN32
46     HostTriple = Triple::normalize(HostTriple);
47   }
48 
49   /// Returns true if the host architecture is known to support MCJIT
ArchSupportsMCJIT()50   bool ArchSupportsMCJIT() {
51     Triple Host(HostTriple);
52     // If ARCH is not supported, bail
53     if (std::find(SupportedArchs.begin(), SupportedArchs.end(), Host.getArch())
54         == SupportedArchs.end())
55       return false;
56 
57     // If ARCH is supported and has no specific sub-arch support
58     if (std::find(HasSubArchs.begin(), HasSubArchs.end(), Host.getArch())
59         == HasSubArchs.end())
60       return true;
61 
62     // If ARCH has sub-arch support, find it
63     SmallVectorImpl<std::string>::const_iterator I = SupportedSubArchs.begin();
64     for(; I != SupportedSubArchs.end(); ++I)
65       if (Host.getArchName().startswith(I->c_str()))
66         return true;
67 
68     return false;
69   }
70 
71   /// Returns true if the host OS is known to support MCJIT
OSSupportsMCJIT()72   bool OSSupportsMCJIT() {
73     Triple Host(HostTriple);
74 
75     if (std::find(UnsupportedEnvironments.begin(), UnsupportedEnvironments.end(),
76                   Host.getEnvironment()) != UnsupportedEnvironments.end())
77       return false;
78 
79     if (std::find(UnsupportedOSs.begin(), UnsupportedOSs.end(), Host.getOS())
80         == UnsupportedOSs.end())
81       return true;
82 
83     return false;
84   }
85 
86   std::string HostTriple;
87   SmallVector<Triple::ArchType, 4> SupportedArchs;
88   SmallVector<Triple::ArchType, 1> HasSubArchs;
89   SmallVector<std::string, 2> SupportedSubArchs; // We need to own the memory
90   SmallVector<Triple::OSType, 4> UnsupportedOSs;
91   SmallVector<Triple::EnvironmentType, 1> UnsupportedEnvironments;
92 };
93 
94 } // namespace llvm
95 
96 #endif
97 
98