jak-project/goalc/emitter/CodeTester.cpp

150 lines
3.5 KiB
C++
Raw Normal View History

2020-09-02 17:51:42 -04:00
/*!
* @file CodeTester.cpp
* The CodeTester is a utility to run the output of the compiler as part of a unit test.
* This is effective for tests which try all combinations of registers, etc.
*
* The CodeTester can't be used for tests requiring the full GOAL language/linking.
*/
#ifdef __linux__
#include <sys/mman.h>
#elif _WIN32
#include "third-party/mman/mman.h"
#endif
#include <cstdio>
2020-08-22 22:30:12 -04:00
#include "CodeTester.h"
#include "IGen.h"
#include "third-party/fmt/core.h"
2020-08-22 22:30:12 -04:00
2020-08-29 19:13:29 -04:00
namespace emitter {
2020-08-29 22:41:46 -04:00
CodeTester::CodeTester() : m_info(RegisterInfo::make_register_info()) {}
2020-08-22 22:30:12 -04:00
2020-09-02 17:51:42 -04:00
/*!
* Convert to a string for comparison against an assembler or tests.
*/
2020-08-29 19:13:29 -04:00
std::string CodeTester::dump_to_hex_string(bool nospace) {
2020-08-22 22:30:12 -04:00
std::string result;
char buff[32];
for (int i = 0; i < code_buffer_size; i++) {
2020-08-29 22:41:46 -04:00
if (nospace) {
2020-08-29 19:13:29 -04:00
sprintf(buff, "%02X", code_buffer[i]);
} else {
sprintf(buff, "%02x ", code_buffer[i]);
}
2020-08-22 22:30:12 -04:00
result += buff;
}
// remove trailing space
2020-08-29 19:13:29 -04:00
if (!nospace && !result.empty()) {
2020-08-22 22:30:12 -04:00
result.pop_back();
}
return result;
}
2020-09-02 17:51:42 -04:00
/*!
* Add an instruction to the buffer.
*/
2020-08-22 22:30:12 -04:00
void CodeTester::emit(const Instruction& instr) {
code_buffer_size += instr.emit(code_buffer + code_buffer_size);
ASSERT(code_buffer_size <= code_buffer_capacity);
2020-08-22 22:30:12 -04:00
}
2020-09-02 17:51:42 -04:00
/*!
* Add a return instruction to the buffer.
*/
2020-08-22 22:30:12 -04:00
void CodeTester::emit_return() {
emit(IGen::ret());
}
2020-09-02 17:51:42 -04:00
/*!
* Pop all GPRs off of the stack. Optionally exclude rax.
* Pops RSP always, which is weird, but doesn't cause issues.
*/
2020-08-22 22:30:12 -04:00
void CodeTester::emit_pop_all_gprs(bool exclude_rax) {
2020-08-29 19:13:29 -04:00
for (int i = 16; i-- > 0;) {
if (i != RAX || !exclude_rax) {
2020-08-22 22:30:12 -04:00
emit(IGen::pop_gpr64(i));
}
}
}
2020-09-02 17:51:42 -04:00
/*!
* Push all GPRs onto the stack. Optionally exclude RAX.
* Pushes RSP always, which is weird, but doesn't cause issues.
*/
2020-08-22 22:30:12 -04:00
void CodeTester::emit_push_all_gprs(bool exclude_rax) {
for (int i = 0; i < 16; i++) {
2020-08-29 19:13:29 -04:00
if (i != RAX || !exclude_rax) {
2020-08-22 22:30:12 -04:00
emit(IGen::push_gpr64(i));
}
2020-08-29 19:13:29 -04:00
}
}
2020-08-22 22:30:12 -04:00
2020-09-02 17:51:42 -04:00
/*!
* Push all xmm registers (all 128-bits) to the stack.
*/
2020-08-29 19:13:29 -04:00
void CodeTester::emit_push_all_xmms() {
emit(IGen::sub_gpr64_imm8s(RSP, 8));
2020-08-29 22:41:46 -04:00
for (int i = 0; i < 16; i++) {
2020-08-29 19:13:29 -04:00
emit(IGen::sub_gpr64_imm8s(RSP, 16));
emit(IGen::store128_gpr64_xmm128(RSP, XMM0 + i));
}
}
2020-09-02 17:51:42 -04:00
/*!
* Pop all xmm registers (all 128-bits) from the stack
*/
2020-08-29 19:13:29 -04:00
void CodeTester::emit_pop_all_xmms() {
2020-08-29 22:41:46 -04:00
for (int i = 0; i < 16; i++) {
2020-08-29 19:13:29 -04:00
emit(IGen::load128_xmm128_gpr64(XMM0 + i, RSP));
emit(IGen::add_gpr64_imm8s(RSP, 16));
2020-08-22 22:30:12 -04:00
}
2020-08-29 19:13:29 -04:00
emit(IGen::add_gpr64_imm8s(RSP, 8));
2020-08-22 22:30:12 -04:00
}
2020-09-02 17:51:42 -04:00
/*!
* Remove everything from the code buffer
*/
2020-08-22 22:30:12 -04:00
void CodeTester::clear() {
code_buffer_size = 0;
}
2020-09-02 17:51:42 -04:00
/*!
* Execute the buffered code with no arguments, return the value of RAX.
*/
2020-08-22 22:30:12 -04:00
u64 CodeTester::execute() {
return ((u64(*)())code_buffer)();
}
2020-09-02 17:51:42 -04:00
/*!
* Execute code buffer with arguments. Use get_c_abi_arg to figure out which registers the
* arguments will appear in (will handle windows/linux differences)
*/
2020-08-29 19:13:29 -04:00
u64 CodeTester::execute(u64 in0, u64 in1, u64 in2, u64 in3) {
return ((u64(*)(u64, u64, u64, u64))code_buffer)(in0, in1, in2, in3);
}
2020-09-02 17:51:42 -04:00
/*!
* Allocate a code buffer of the given size.
*/
2020-08-22 22:30:12 -04:00
void CodeTester::init_code_buffer(int capacity) {
code_buffer = (u8*)mmap(nullptr, capacity, PROT_EXEC | PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
if (code_buffer == (u8*)(-1)) {
ASSERT_MSG(false, "[CodeTester] Failed to map memory!");
2020-08-22 22:30:12 -04:00
}
code_buffer_capacity = capacity;
code_buffer_size = 0;
}
CodeTester::~CodeTester() {
if (code_buffer_capacity) {
munmap(code_buffer, code_buffer_capacity);
}
}
} // namespace emitter