blob: f0de2d023a17becba50181a5586c4a549a2221ef [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
45#ifdef LLVM_ON_WIN32
46 // On Windows, generate ELF objects by specifying "-elf" in triple
47 HostTriple += "-elf";
48#endif // LLVM_ON_WIN32
49 HostTriple = Triple::normalize(HostTriple);
50 }
51
52 /// Returns true if the host architecture is known to support MCJIT
53 bool ArchSupportsMCJIT() {
54 Triple Host(HostTriple);
Renato Golincf6979d2013-05-19 20:10:10 +000055 // If ARCH is not supported, bail
David Majnemer0d955d02016-08-11 22:21:41 +000056 if (!is_contained(SupportedArchs, Host.getArch()))
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000057 return false;
Renato Golincf6979d2013-05-19 20:10:10 +000058
59 // If ARCH is supported and has no specific sub-arch support
David Majnemer0d955d02016-08-11 22:21:41 +000060 if (!is_contained(HasSubArchs, Host.getArch()))
Renato Golincf6979d2013-05-19 20:10:10 +000061 return true;
62
63 // If ARCH has sub-arch support, find it
64 SmallVectorImpl<std::string>::const_iterator I = SupportedSubArchs.begin();
65 for(; I != SupportedSubArchs.end(); ++I)
Malcolm Parsons06ac79c2016-11-02 16:43:50 +000066 if (Host.getArchName().startswith(*I))
Renato Golincf6979d2013-05-19 20:10:10 +000067 return true;
68
69 return false;
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000070 }
71
72 /// Returns true if the host OS is known to support MCJIT
73 bool OSSupportsMCJIT() {
74 Triple Host(HostTriple);
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +000075
David Majnemer0d955d02016-08-11 22:21:41 +000076 if (find(UnsupportedEnvironments, Host.getEnvironment()) !=
77 UnsupportedEnvironments.end())
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +000078 return false;
79
David Majnemer0d955d02016-08-11 22:21:41 +000080 if (!is_contained(UnsupportedOSs, Host.getOS()))
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000081 return true;
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +000082
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000083 return false;
84 }
85
86 std::string HostTriple;
87 SmallVector<Triple::ArchType, 4> SupportedArchs;
Renato Golincf6979d2013-05-19 20:10:10 +000088 SmallVector<Triple::ArchType, 1> HasSubArchs;
89 SmallVector<std::string, 2> SupportedSubArchs; // We need to own the memory
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000090 SmallVector<Triple::OSType, 4> UnsupportedOSs;
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +000091 SmallVector<Triple::EnvironmentType, 1> UnsupportedEnvironments;
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000092};
93
94} // namespace llvm
95
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000096#endif
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000097