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>
|
2018-01-23 19:16:39 +00:00
|
|
|
#include <unordered_set>
|
2016-08-17 15:53:36 +01:00
|
|
|
#include <vector>
|
2016-07-01 14:01:06 +01:00
|
|
|
|
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"
|
2018-01-02 17:45:39 +00: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
|
|
|
|
2018-01-26 13:51:48 +00:00
|
|
|
namespace Dynarmic::IR {
|
2016-09-03 21:48:03 +01:00
|
|
|
class Block;
|
|
|
|
class Inst;
|
2018-01-26 13:51:48 +00:00
|
|
|
} // namespace Dynarmic::IR
|
2016-09-03 21:48:03 +01:00
|
|
|
|
2018-01-26 13:51:48 +00:00
|
|
|
namespace Dynarmic::BackendX64 {
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2016-12-05 04:14:58 +00:00
|
|
|
class BlockOfCode;
|
|
|
|
|
2018-01-01 23:40:34 +00:00
|
|
|
struct EmitContext {
|
|
|
|
EmitContext(RegAlloc& reg_alloc, IR::Block& block);
|
|
|
|
|
|
|
|
void EraseInstruction(IR::Inst* inst);
|
|
|
|
|
|
|
|
virtual bool FPSCR_RoundTowardsZero() const = 0;
|
|
|
|
virtual bool FPSCR_FTZ() const = 0;
|
|
|
|
virtual bool FPSCR_DN() const = 0;
|
2018-02-18 12:54:39 +00:00
|
|
|
virtual bool AccurateNaN() const { return true; }
|
2018-01-01 23:40:34 +00:00
|
|
|
|
|
|
|
RegAlloc& reg_alloc;
|
|
|
|
IR::Block& block;
|
|
|
|
};
|
|
|
|
|
2018-01-01 22:34:05 +00:00
|
|
|
class EmitX64 {
|
2016-07-01 14:01:06 +01:00
|
|
|
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
|
2016-08-05 01:50:31 +01:00
|
|
|
};
|
2016-07-04 14:37:50 +01:00
|
|
|
|
2018-02-03 14:28:57 +00:00
|
|
|
EmitX64(BlockOfCode& code);
|
2018-01-01 22:34:05 +00:00
|
|
|
virtual ~EmitX64();
|
2016-08-12 18:17:31 +01:00
|
|
|
|
|
|
|
/// Looks up an emitted host block in the cache.
|
2018-01-01 22:34: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.
|
2018-01-23 19:16:39 +00:00
|
|
|
virtual void ClearCache();
|
2016-07-14 12:52:53 +01:00
|
|
|
|
2018-01-23 19:16:39 +00:00
|
|
|
/// Invalidates a selection of basic blocks.
|
|
|
|
void InvalidateBasicBlocks(const std::unordered_set<IR::LocationDescriptor>& locations);
|
2017-02-16 18:18:29 +00:00
|
|
|
|
2018-01-01 22:34:05 +00:00
|
|
|
protected:
|
2016-07-14 12:52:53 +01:00
|
|
|
// Microinstruction emitters
|
2018-01-01 23:40:34 +00:00
|
|
|
#define OPCODE(name, type, ...) void Emit##name(EmitContext& ctx, IR::Inst* inst);
|
2018-01-01 22:34:05 +00:00
|
|
|
#define A32OPC(...)
|
2018-01-06 21:15:25 +00:00
|
|
|
#define A64OPC(...)
|
2016-07-31 14:52:28 +01:00
|
|
|
#include "frontend/ir/opcodes.inc"
|
|
|
|
#undef OPCODE
|
2018-01-01 16:19:43 +00:00
|
|
|
#undef A32OPC
|
2018-01-06 21:15:25 +00:00
|
|
|
#undef A64OPC
|
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);
|
2018-01-01 22:34:05 +00:00
|
|
|
Xbyak::Label EmitCond(IR::Cond cond);
|
2016-08-18 18:16:18 +01:00
|
|
|
void EmitCondPrelude(const IR::Block& block);
|
2018-01-01 22:34:05 +00:00
|
|
|
void PushRSBHelper(Xbyak::Reg64 loc_desc_reg, Xbyak::Reg64 index_reg, IR::LocationDescriptor target);
|
2016-07-07 10:53:09 +01:00
|
|
|
|
2016-07-14 12:52:53 +01:00
|
|
|
// Terminal instruction emitters
|
2018-01-01 22:34:05 +00:00
|
|
|
void EmitTerminal(IR::Terminal terminal, IR::LocationDescriptor initial_location);
|
|
|
|
virtual void EmitTerminalImpl(IR::Term::Interpret terminal, IR::LocationDescriptor initial_location) = 0;
|
|
|
|
virtual void EmitTerminalImpl(IR::Term::ReturnToDispatch terminal, IR::LocationDescriptor initial_location) = 0;
|
|
|
|
virtual void EmitTerminalImpl(IR::Term::LinkBlock terminal, IR::LocationDescriptor initial_location) = 0;
|
|
|
|
virtual void EmitTerminalImpl(IR::Term::LinkBlockFast terminal, IR::LocationDescriptor initial_location) = 0;
|
|
|
|
virtual void EmitTerminalImpl(IR::Term::PopRSBHint terminal, IR::LocationDescriptor initial_location) = 0;
|
|
|
|
virtual void EmitTerminalImpl(IR::Term::If terminal, IR::LocationDescriptor initial_location) = 0;
|
2018-01-07 16:33:02 +00:00
|
|
|
virtual void EmitTerminalImpl(IR::Term::CheckBit terminal, IR::LocationDescriptor initial_location) = 0;
|
2018-01-01 22:34:05 +00:00
|
|
|
virtual void EmitTerminalImpl(IR::Term::CheckHalt terminal, IR::LocationDescriptor initial_location) = 0;
|
2016-12-19 15:01:49 +00:00
|
|
|
|
|
|
|
// Patching
|
|
|
|
struct PatchInformation {
|
|
|
|
std::vector<CodePtr> jg;
|
|
|
|
std::vector<CodePtr> jmp;
|
|
|
|
std::vector<CodePtr> mov_rcx;
|
|
|
|
};
|
2018-01-01 22:34:05 +00:00
|
|
|
void Patch(const IR::LocationDescriptor& target_desc, CodePtr target_code_ptr);
|
|
|
|
void Unpatch(const IR::LocationDescriptor& target_desc);
|
|
|
|
virtual void EmitPatchJg(const IR::LocationDescriptor& target_desc, CodePtr target_code_ptr = nullptr) = 0;
|
|
|
|
virtual void EmitPatchJmp(const IR::LocationDescriptor& target_desc, CodePtr target_code_ptr = nullptr) = 0;
|
|
|
|
virtual void EmitPatchMovRcx(CodePtr target_code_ptr = nullptr) = 0;
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2016-07-14 12:52:53 +01:00
|
|
|
// State
|
2018-02-03 14:28:57 +00:00
|
|
|
BlockOfCode& code;
|
2018-01-01 22:34:05 +00:00
|
|
|
std::unordered_map<IR::LocationDescriptor, BlockDescriptor> block_descriptors;
|
|
|
|
std::unordered_map<IR::LocationDescriptor, PatchInformation> patch_information;
|
2016-07-01 14:01:06 +01:00
|
|
|
};
|
|
|
|
|
2018-01-26 13:51:48 +00:00
|
|
|
} // namespace Dynarmic::BackendX64
|