2016-07-01 15:01:06 +02:00
|
|
|
/* This file is part of the dynarmic project.
|
|
|
|
* Copyright (c) 2016 MerryMage
|
|
|
|
* This software may be used and distributed according to the terms of the GNU
|
|
|
|
* General Public License version 2 or any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <set>
|
2016-07-04 15:37:50 +02:00
|
|
|
#include <unordered_map>
|
2016-08-17 16:53:36 +02:00
|
|
|
#include <vector>
|
2016-07-01 15:01:06 +02:00
|
|
|
|
2016-08-12 19:17:31 +02:00
|
|
|
#include <boost/optional.hpp>
|
2016-08-24 21:07:08 +02:00
|
|
|
#include <xbyak.h>
|
2016-08-12 19:17:31 +02:00
|
|
|
|
2016-08-07 19:08:48 +02:00
|
|
|
#include "backend_x64/block_of_code.h"
|
2016-07-01 15:01:06 +02:00
|
|
|
#include "backend_x64/reg_alloc.h"
|
2016-08-25 19:22:08 +02:00
|
|
|
#include "dynarmic/callbacks.h"
|
2016-08-17 16:53:36 +02:00
|
|
|
#include "frontend/arm_types.h"
|
|
|
|
#include "frontend/ir/basic_block.h"
|
|
|
|
#include "frontend/ir/microinstruction.h"
|
|
|
|
#include "frontend/ir/terminal.h"
|
2016-07-01 15:01:06 +02:00
|
|
|
|
|
|
|
namespace Dynarmic {
|
|
|
|
namespace BackendX64 {
|
|
|
|
|
|
|
|
class EmitX64 final {
|
|
|
|
public:
|
2016-08-07 19:08:48 +02:00
|
|
|
EmitX64(BlockOfCode* code, UserCallbacks cb, Jit* jit_interface)
|
|
|
|
: reg_alloc(code), code(code), cb(cb), jit_interface(jit_interface) {}
|
2016-07-01 15:01:06 +02:00
|
|
|
|
2016-08-05 02:50:31 +02:00
|
|
|
struct BlockDescriptor {
|
2016-08-12 19:17:31 +02:00
|
|
|
CodePtr code_ptr; ///< Entrypoint of emitted code
|
|
|
|
size_t size; ///< Length in bytes of emitted code
|
2016-08-05 02:50:31 +02:00
|
|
|
};
|
2016-07-04 15:37:50 +02:00
|
|
|
|
2016-08-26 20:14:25 +02:00
|
|
|
/**
|
|
|
|
* Emit host machine code for a basic block with intermediate representation `ir`.
|
|
|
|
* @note ir is modified.
|
|
|
|
*/
|
|
|
|
BlockDescriptor Emit(IR::Block& ir);
|
2016-08-12 19:17:31 +02:00
|
|
|
|
|
|
|
/// Looks up an emitted host block in the cache.
|
|
|
|
boost::optional<BlockDescriptor> GetBasicBlock(Arm::LocationDescriptor descriptor) {
|
2016-07-04 15:37:50 +02:00
|
|
|
auto iter = basic_blocks.find(descriptor);
|
2016-08-12 19:17:31 +02:00
|
|
|
if (iter == basic_blocks.end())
|
|
|
|
return boost::none;
|
|
|
|
return boost::make_optional<BlockDescriptor>(iter->second);
|
2016-07-04 15:37:50 +02:00
|
|
|
}
|
2016-07-01 15:01:06 +02:00
|
|
|
|
2016-08-12 19:17:31 +02:00
|
|
|
/// Empties the cache.
|
2016-07-14 13:52:53 +02:00
|
|
|
void ClearCache();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Microinstruction emitters
|
2016-07-31 15:52:28 +02:00
|
|
|
#define OPCODE(name, type, ...) void Emit##name(IR::Block& block, IR::Inst* inst);
|
|
|
|
#include "frontend/ir/opcodes.inc"
|
|
|
|
#undef OPCODE
|
2016-07-01 15:01:06 +02:00
|
|
|
|
2016-07-14 13:52:53 +02:00
|
|
|
// Helpers
|
2016-07-04 15:37:50 +02:00
|
|
|
void EmitAddCycles(size_t cycles);
|
2016-08-18 19:16:18 +02:00
|
|
|
void EmitCondPrelude(const IR::Block& block);
|
2016-07-07 11:53:09 +02:00
|
|
|
|
2016-07-14 13:52:53 +02:00
|
|
|
// Terminal instruction emitters
|
2016-07-07 11:53:09 +02:00
|
|
|
void EmitTerminal(IR::Terminal terminal, Arm::LocationDescriptor initial_location);
|
|
|
|
void EmitTerminalInterpret(IR::Term::Interpret terminal, Arm::LocationDescriptor initial_location);
|
|
|
|
void EmitTerminalReturnToDispatch(IR::Term::ReturnToDispatch terminal, Arm::LocationDescriptor initial_location);
|
|
|
|
void EmitTerminalLinkBlock(IR::Term::LinkBlock terminal, Arm::LocationDescriptor initial_location);
|
|
|
|
void EmitTerminalLinkBlockFast(IR::Term::LinkBlockFast terminal, Arm::LocationDescriptor initial_location);
|
|
|
|
void EmitTerminalPopRSBHint(IR::Term::PopRSBHint terminal, Arm::LocationDescriptor initial_location);
|
|
|
|
void EmitTerminalIf(IR::Term::If terminal, Arm::LocationDescriptor initial_location);
|
2016-08-15 16:02:08 +02:00
|
|
|
void EmitTerminalCheckHalt(IR::Term::CheckHalt terminal, Arm::LocationDescriptor initial_location);
|
2016-08-07 23:11:39 +02:00
|
|
|
void Patch(Arm::LocationDescriptor desc, CodePtr bb);
|
2016-07-01 15:01:06 +02:00
|
|
|
|
2016-07-14 13:52:53 +02:00
|
|
|
// Per-block state
|
2016-07-01 15:01:06 +02:00
|
|
|
RegAlloc reg_alloc;
|
2016-07-04 15:37:50 +02:00
|
|
|
|
2016-07-14 13:52:53 +02:00
|
|
|
// State
|
2016-08-07 19:08:48 +02:00
|
|
|
BlockOfCode* code;
|
2016-07-01 15:01:06 +02:00
|
|
|
UserCallbacks cb;
|
2016-07-07 11:53:09 +02:00
|
|
|
Jit* jit_interface;
|
2016-08-13 01:10:23 +02:00
|
|
|
std::unordered_map<u64, CodePtr> unique_hash_to_code_ptr;
|
|
|
|
std::unordered_map<u64, std::vector<CodePtr>> patch_unique_hash_locations;
|
2016-09-03 13:48:47 +02:00
|
|
|
std::unordered_map<Arm::LocationDescriptor, BlockDescriptor> basic_blocks;
|
|
|
|
std::unordered_map<Arm::LocationDescriptor, std::vector<CodePtr>> patch_jg_locations;
|
|
|
|
std::unordered_map<Arm::LocationDescriptor, std::vector<CodePtr>> patch_jmp_locations;
|
2016-07-01 15:01:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace BackendX64
|
|
|
|
} // namespace Dynarmic
|