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
|
|
|
|
|
2016-07-04 14:37:50 +01:00
|
|
|
#include <unordered_map>
|
2016-08-17 15:53:36 +01:00
|
|
|
#include <vector>
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2017-12-05 21:34:40 +00: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>
|
|
|
|
|
2016-11-25 20:32:22 +00:00
|
|
|
#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"
|
2016-09-05 11:54:09 +01:00
|
|
|
#include "frontend/ir/location_descriptor.h"
|
2016-08-17 15:53:36 +01:00
|
|
|
#include "frontend/ir/terminal.h"
|
2016-07-01 14:01:06 +01:00
|
|
|
|
|
|
|
namespace Dynarmic {
|
2016-09-03 21:48:03 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-07-01 14:01:06 +01:00
|
|
|
class EmitX64 final {
|
|
|
|
public:
|
2016-08-05 01:50:31 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
IR::LocationDescriptor start_location;
|
|
|
|
u32 end_location_pc;
|
2016-08-05 01:50:31 +01:00
|
|
|
};
|
2016-07-04 14:37:50 +01:00
|
|
|
|
2016-12-05 04:22:56 +00:00
|
|
|
EmitX64(BlockOfCode* code, UserCallbacks cb, Jit* jit_interface);
|
|
|
|
|
2016-08-26 19:14:25 +01: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 18:17:31 +01:00
|
|
|
|
|
|
|
/// Looks up an emitted host block in the cache.
|
2016-12-05 04:27:05 +00:00
|
|
|
boost::optional<BlockDescriptor> GetBasicBlock(IR::LocationDescriptor descriptor) const;
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2017-02-16 18:18:29 +00:00
|
|
|
/// Empties the entire cache.
|
2016-07-14 12:52:53 +01:00
|
|
|
void ClearCache();
|
|
|
|
|
2017-02-16 18:18:29 +00:00
|
|
|
/**
|
2017-12-05 21:34:40 +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
|
|
|
*/
|
2017-12-05 21:34:40 +00:00
|
|
|
void InvalidateCacheRanges(const boost::icl::interval_set<u32>& ranges);
|
2017-02-16 18:18:29 +00:00
|
|
|
|
2016-07-14 12:52:53 +01:00
|
|
|
private:
|
|
|
|
// Microinstruction emitters
|
2017-02-04 09:23:19 +00:00
|
|
|
#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
|
|
|
|
2016-07-14 12:52:53 +01:00
|
|
|
// Helpers
|
2016-07-04 14:37:50 +01:00
|
|
|
void EmitAddCycles(size_t cycles);
|
2016-08-18 18:16:18 +01:00
|
|
|
void EmitCondPrelude(const IR::Block& block);
|
2016-07-07 10:53:09 +01:00
|
|
|
|
2016-07-14 12:52:53 +01:00
|
|
|
// Terminal instruction emitters
|
2016-09-05 11:54:09 +01:00
|
|
|
void EmitTerminal(IR::Terminal terminal, IR::LocationDescriptor initial_location);
|
2017-02-16 19:40:51 +00:00
|
|
|
void EmitTerminal(IR::Term::Interpret terminal, IR::LocationDescriptor initial_location);
|
|
|
|
void EmitTerminal(IR::Term::ReturnToDispatch terminal, IR::LocationDescriptor initial_location);
|
|
|
|
void EmitTerminal(IR::Term::LinkBlock terminal, IR::LocationDescriptor initial_location);
|
|
|
|
void EmitTerminal(IR::Term::LinkBlockFast terminal, IR::LocationDescriptor initial_location);
|
|
|
|
void EmitTerminal(IR::Term::PopRSBHint terminal, IR::LocationDescriptor initial_location);
|
|
|
|
void EmitTerminal(IR::Term::If terminal, IR::LocationDescriptor initial_location);
|
|
|
|
void EmitTerminal(IR::Term::CheckHalt terminal, IR::LocationDescriptor initial_location);
|
2016-12-19 15:01:49 +00:00
|
|
|
|
|
|
|
// Patching
|
|
|
|
struct PatchInformation {
|
|
|
|
std::vector<CodePtr> jg;
|
|
|
|
std::vector<CodePtr> jmp;
|
|
|
|
std::vector<CodePtr> mov_rcx;
|
|
|
|
};
|
|
|
|
void Patch(const IR::LocationDescriptor& target_desc, CodePtr target_code_ptr);
|
|
|
|
void Unpatch(const IR::LocationDescriptor& target_desc);
|
2017-04-08 10:04:53 +01:00
|
|
|
void EmitPatchJg(const IR::LocationDescriptor& target_desc, CodePtr target_code_ptr = nullptr);
|
2016-12-19 15:01:49 +00:00
|
|
|
void EmitPatchJmp(const IR::LocationDescriptor& target_desc, CodePtr target_code_ptr = nullptr);
|
|
|
|
void EmitPatchMovRcx(CodePtr target_code_ptr = nullptr);
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2016-07-14 12:52:53 +01:00
|
|
|
// State
|
2016-08-07 18:08:48 +01:00
|
|
|
BlockOfCode* code;
|
2016-07-01 14:01:06 +01:00
|
|
|
UserCallbacks cb;
|
2017-12-05 21:34:40 +00:00
|
|
|
boost::icl::interval_map<u32, std::set<IR::LocationDescriptor>> block_ranges;
|
2016-07-07 10:53:09 +01:00
|
|
|
Jit* jit_interface;
|
2016-12-19 15:01:49 +00:00
|
|
|
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
|