dynarmic/src/backend_x64/emit_x64.h

111 lines
3.7 KiB
C
Raw Normal View History

2016-07-01 14:01:06 +01: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 <unordered_map>
#include <vector>
2016-07-01 14:01:06 +01:00
#include <boost/icl/interval_map.hpp>
#include <boost/icl/interval_set.hpp>
2016-08-12 18:17:31 +01:00
#include <boost/optional.hpp>
#include <xbyak_util.h>
2016-07-01 14:01:06 +01:00
#include "backend_x64/reg_alloc.h"
2017-02-16 18:18:29 +00:00
#include "common/address_range.h"
2016-08-25 18:22:08 +01:00
#include "dynarmic/callbacks.h"
2018-01-01 15:23:56 +00:00
#include "frontend/A32/location_descriptor.h"
#include "frontend/ir/terminal.h"
2016-07-01 14:01:06 +01:00
namespace Dynarmic {
class Jit;
namespace IR {
class Block;
class Inst;
} // namespace IR
2016-07-01 14:01:06 +01:00
namespace BackendX64 {
2016-12-05 04:14:58 +00:00
class BlockOfCode;
2018-01-01 15:23:56 +00:00
class A32EmitX64 final {
2016-07-01 14:01:06 +01:00
public:
struct BlockDescriptor {
2017-02-16 18:18:29 +00:00
CodePtr entrypoint; // Entrypoint of emitted code
size_t size; // Length in bytes of emitted code
2018-01-01 15:23:56 +00:00
A32::LocationDescriptor start_location;
boost::icl::discrete_interval<u32> range;
};
2018-01-01 15:23:56 +00:00
A32EmitX64(BlockOfCode* code, UserCallbacks cb, Jit* jit_interface);
/**
* Emit host machine code for a basic block with intermediate representation `ir`.
* @note ir is modified.
*/
BlockDescriptor Emit(IR::Block& ir);
2016-08-12 18:17:31 +01:00
/// Looks up an emitted host block in the cache.
2018-01-01 15:23:56 +00:00
boost::optional<BlockDescriptor> GetBasicBlock(A32::LocationDescriptor descriptor) const;
2016-07-01 14:01:06 +01:00
2017-02-16 18:18:29 +00:00
/// Empties the entire cache.
void ClearCache();
2017-02-16 18:18:29 +00:00
/**
* Invalidate the cache at a set of ranges of addresses.
* @param ranges The set of ranges of addresses to invalidate the cache at.
2017-02-16 18:18:29 +00:00
*/
void InvalidateCacheRanges(const boost::icl::interval_set<u32>& ranges);
2017-02-16 18:18:29 +00:00
private:
// Microinstruction emitters
#define OPCODE(name, type, ...) void Emit##name(RegAlloc& reg_alloc, IR::Block& block, IR::Inst* inst);
2016-07-31 14:52:28 +01:00
#include "frontend/ir/opcodes.inc"
#undef OPCODE
2016-07-01 14:01:06 +01:00
// Helpers
void EmitAddCycles(size_t cycles);
void EmitCondPrelude(const IR::Block& block);
2017-11-27 20:29:19 +00:00
void PushRSBHelper(Xbyak::Reg64 loc_desc_reg, Xbyak::Reg64 index_reg, u64 target_hash);
2016-07-07 10:53:09 +01:00
// Terminal instruction emitters
2018-01-01 15:23:56 +00:00
void EmitTerminal(IR::Terminal terminal, A32::LocationDescriptor initial_location);
void EmitTerminal(IR::Term::Interpret terminal, A32::LocationDescriptor initial_location);
void EmitTerminal(IR::Term::ReturnToDispatch terminal, A32::LocationDescriptor initial_location);
void EmitTerminal(IR::Term::LinkBlock terminal, A32::LocationDescriptor initial_location);
void EmitTerminal(IR::Term::LinkBlockFast terminal, A32::LocationDescriptor initial_location);
void EmitTerminal(IR::Term::PopRSBHint terminal, A32::LocationDescriptor initial_location);
void EmitTerminal(IR::Term::If terminal, A32::LocationDescriptor initial_location);
void EmitTerminal(IR::Term::CheckHalt terminal, A32::LocationDescriptor initial_location);
// Patching
struct PatchInformation {
std::vector<CodePtr> jg;
std::vector<CodePtr> jmp;
std::vector<CodePtr> mov_rcx;
};
2018-01-01 15:23:56 +00:00
void Patch(const A32::LocationDescriptor& target_desc, CodePtr target_code_ptr);
void Unpatch(const A32::LocationDescriptor& target_desc);
void EmitPatchJg(const A32::LocationDescriptor& target_desc, CodePtr target_code_ptr = nullptr);
void EmitPatchJmp(const A32::LocationDescriptor& target_desc, CodePtr target_code_ptr = nullptr);
void EmitPatchMovRcx(CodePtr target_code_ptr = nullptr);
2016-07-01 14:01:06 +01:00
// State
BlockOfCode* code;
2016-07-01 14:01:06 +01:00
UserCallbacks cb;
2018-01-01 15:23:56 +00:00
boost::icl::interval_map<u32, std::set<A32::LocationDescriptor>> block_ranges;
2016-07-07 10:53:09 +01:00
Jit* jit_interface;
std::unordered_map<u64, BlockDescriptor> block_descriptors;
std::unordered_map<u64, PatchInformation> patch_information;
2016-07-01 14:01:06 +01:00
};
} // namespace BackendX64
} // namespace Dynarmic