asmjit_test_compiler.h (1937B)
1 // This file is part of AsmJit project <https://asmjit.com> 2 // 3 // See <asmjit/core.h> or LICENSE.md for license and copyright information 4 // SPDX-License-Identifier: Zlib 5 6 #ifndef ASMJIT_TEST_COMPILER_H_INCLUDED 7 #define ASMJIT_TEST_COMPILER_H_INCLUDED 8 9 #include <asmjit/core.h> 10 11 #include <memory> 12 #include <vector> 13 14 class SimpleErrorHandler : public asmjit::ErrorHandler { 15 public: 16 SimpleErrorHandler() 17 : _err(asmjit::Error::kOk) {} 18 19 void handle_error(asmjit::Error err, const char* message, asmjit::BaseEmitter* origin) override { 20 asmjit::Support::maybe_unused(origin); 21 _err = err; 22 _message.assign(message); 23 } 24 25 asmjit::Error _err; 26 asmjit::String _message; 27 }; 28 29 //! A test case interface for testing AsmJit's Compiler. 30 class TestCase { 31 public: 32 TestCase(const char* name, asmjit::Arch arch) { 33 if (name) 34 _name.assign(name); 35 _arch = arch; 36 } 37 38 virtual ~TestCase() {} 39 40 inline const char* name() const { return _name.data(); } 41 inline asmjit::Arch arch() const { return _arch; } 42 43 virtual void compile(asmjit::BaseCompiler& cc) = 0; 44 virtual bool run(void* func, asmjit::String& result, asmjit::String& expect) = 0; 45 46 asmjit::String _name; 47 asmjit::Arch _arch; 48 }; 49 50 class TestApp { 51 public: 52 std::vector<std::unique_ptr<TestCase>> _tests; 53 54 const char* _arch = nullptr; 55 const char* _filter = nullptr; 56 bool _help_only = false; 57 bool _verbose = false; 58 bool _dump_asm = false; 59 bool _dump_hex = false; 60 61 unsigned _num_tests = 0; 62 unsigned _num_failed = 0; 63 size_t _output_size = 0; 64 65 TestApp() noexcept 66 : _arch("all") {} 67 ~TestApp() noexcept {} 68 69 void add(TestCase* test) noexcept { 70 _tests.push_back(std::unique_ptr<TestCase>(test)); 71 } 72 73 template<class T> 74 inline void add_t() { T::add(*this); } 75 76 int handle_args(int argc, const char* const* argv); 77 void show_info(); 78 79 bool should_run(const TestCase* tc); 80 int run(); 81 }; 82 83 #endif // ASMJIT_TEST_COMPILER_H_INCLUDED