2016-08-17 15:53:36 +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.
|
|
|
|
*/
|
|
|
|
|
2016-08-25 15:35:50 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <initializer_list>
|
2016-08-17 15:53:36 +01:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
2016-08-25 23:41:31 +01:00
|
|
|
#include <fmt/format.h>
|
2016-11-24 05:00:23 +00:00
|
|
|
#include <fmt/ostream.h>
|
2016-08-25 23:41:31 +01:00
|
|
|
|
2016-08-25 15:35:50 +01:00
|
|
|
#include "common/assert.h"
|
2019-04-15 10:28:41 +01:00
|
|
|
#include "common/memory_pool.h"
|
2018-01-01 15:23:56 +00:00
|
|
|
#include "frontend/A32/types.h"
|
2018-01-07 00:11:57 +00:00
|
|
|
#include "frontend/A64/types.h"
|
2016-08-17 15:53:36 +01:00
|
|
|
#include "frontend/ir/basic_block.h"
|
2019-04-15 10:28:41 +01:00
|
|
|
#include "frontend/ir/cond.h"
|
2016-08-25 15:35:50 +01:00
|
|
|
#include "frontend/ir/opcodes.h"
|
2016-08-17 15:53:36 +01:00
|
|
|
|
2018-01-26 13:51:48 +00:00
|
|
|
namespace Dynarmic::IR {
|
2016-08-17 15:53:36 +01:00
|
|
|
|
2019-04-15 10:27:32 +01:00
|
|
|
Block::Block(const LocationDescriptor& location)
|
2019-04-15 10:28:41 +01:00
|
|
|
: location{location}, end_location{location}, cond{Cond::AL},
|
2019-04-15 10:27:32 +01:00
|
|
|
instruction_alloc_pool{std::make_unique<Common::Pool>(sizeof(Inst), 4096)} {}
|
|
|
|
|
|
|
|
Block::~Block() = default;
|
|
|
|
|
|
|
|
Block::Block(Block&&) = default;
|
|
|
|
|
|
|
|
Block& Block::operator=(Block&&) = default;
|
|
|
|
|
2016-08-25 15:35:50 +01:00
|
|
|
void Block::AppendNewInst(Opcode opcode, std::initializer_list<IR::Value> args) {
|
2018-01-26 23:30:17 +00:00
|
|
|
PrependNewInst(end(), opcode, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
Block::iterator Block::PrependNewInst(iterator insertion_point, Opcode opcode, std::initializer_list<Value> args) {
|
2016-08-25 15:35:50 +01:00
|
|
|
IR::Inst* inst = new(instruction_alloc_pool->Alloc()) IR::Inst(opcode);
|
2018-01-07 00:11:57 +00:00
|
|
|
ASSERT(args.size() == inst->NumArgs());
|
2016-08-25 15:35:50 +01:00
|
|
|
|
|
|
|
std::for_each(args.begin(), args.end(), [&inst, index = size_t(0)](const auto& arg) mutable {
|
|
|
|
inst->SetArg(index, arg);
|
|
|
|
index++;
|
|
|
|
});
|
|
|
|
|
2018-01-26 23:30:17 +00:00
|
|
|
return instructions.insert_before(insertion_point, inst);
|
2016-08-25 15:35:50 +01:00
|
|
|
}
|
|
|
|
|
2016-09-05 11:54:09 +01:00
|
|
|
LocationDescriptor Block::Location() const {
|
2016-08-25 15:35:50 +01:00
|
|
|
return location;
|
|
|
|
}
|
|
|
|
|
2017-02-16 18:18:29 +00:00
|
|
|
LocationDescriptor Block::EndLocation() const {
|
|
|
|
return end_location;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Block::SetEndLocation(const LocationDescriptor& descriptor) {
|
|
|
|
end_location = descriptor;
|
|
|
|
}
|
|
|
|
|
2018-01-01 15:23:56 +00:00
|
|
|
Cond Block::GetCondition() const {
|
2016-08-25 15:35:50 +01:00
|
|
|
return cond;
|
|
|
|
}
|
|
|
|
|
2018-01-01 15:23:56 +00:00
|
|
|
void Block::SetCondition(Cond condition) {
|
2016-08-25 15:35:50 +01:00
|
|
|
cond = condition;
|
|
|
|
}
|
|
|
|
|
2016-09-05 11:54:09 +01:00
|
|
|
LocationDescriptor Block::ConditionFailedLocation() const {
|
2018-10-14 22:17:56 +01:00
|
|
|
return *cond_failed;
|
2016-08-25 15:35:50 +01:00
|
|
|
}
|
|
|
|
|
2016-09-05 11:54:09 +01:00
|
|
|
void Block::SetConditionFailedLocation(LocationDescriptor fail_location) {
|
2016-09-04 11:30:57 +01:00
|
|
|
cond_failed = fail_location;
|
2016-08-25 15:35:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t& Block::ConditionFailedCycleCount() {
|
|
|
|
return cond_failed_cycle_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t& Block::ConditionFailedCycleCount() const {
|
|
|
|
return cond_failed_cycle_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Block::HasConditionFailedLocation() const {
|
2018-10-14 22:17:56 +01:00
|
|
|
return cond_failed.has_value();
|
2016-08-25 15:35:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Block::InstructionList& Block::Instructions() {
|
|
|
|
return instructions;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Block::InstructionList& Block::Instructions() const {
|
|
|
|
return instructions;
|
|
|
|
}
|
|
|
|
|
2018-02-03 03:04:44 +00:00
|
|
|
Terminal Block::GetTerminal() const {
|
2016-08-25 15:35:50 +01:00
|
|
|
return terminal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Block::SetTerminal(Terminal term) {
|
|
|
|
ASSERT_MSG(!HasTerminal(), "Terminal has already been set.");
|
2019-05-24 03:05:44 +01:00
|
|
|
terminal = std::move(term);
|
2016-08-25 15:35:50 +01:00
|
|
|
}
|
|
|
|
|
2018-01-13 21:51:13 +00:00
|
|
|
void Block::ReplaceTerminal(Terminal term) {
|
|
|
|
ASSERT_MSG(HasTerminal(), "Terminal has not been set.");
|
2019-05-24 03:05:44 +01:00
|
|
|
terminal = std::move(term);
|
2018-01-13 21:51:13 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 15:35:50 +01:00
|
|
|
bool Block::HasTerminal() const {
|
|
|
|
return terminal.which() != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t& Block::CycleCount() {
|
|
|
|
return cycle_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t& Block::CycleCount() const {
|
|
|
|
return cycle_count;
|
|
|
|
}
|
|
|
|
|
2016-08-25 02:58:50 +01:00
|
|
|
static std::string TerminalToString(const Terminal& terminal_variant) {
|
|
|
|
switch (terminal_variant.which()) {
|
|
|
|
case 1: {
|
|
|
|
auto terminal = boost::get<IR::Term::Interpret>(terminal_variant);
|
2016-09-05 21:31:25 +01:00
|
|
|
return fmt::format("Interpret{{{}}}", terminal.next);
|
2016-08-25 02:58:50 +01:00
|
|
|
}
|
|
|
|
case 2: {
|
2016-08-25 23:41:31 +01:00
|
|
|
return "ReturnToDispatch{}";
|
2016-08-25 02:58:50 +01:00
|
|
|
}
|
|
|
|
case 3: {
|
|
|
|
auto terminal = boost::get<IR::Term::LinkBlock>(terminal_variant);
|
2016-09-05 21:31:25 +01:00
|
|
|
return fmt::format("LinkBlock{{{}}}", terminal.next);
|
2016-08-25 02:58:50 +01:00
|
|
|
}
|
|
|
|
case 4: {
|
|
|
|
auto terminal = boost::get<IR::Term::LinkBlockFast>(terminal_variant);
|
2016-09-05 21:31:25 +01:00
|
|
|
return fmt::format("LinkBlockFast{{{}}}", terminal.next);
|
2016-08-25 02:58:50 +01:00
|
|
|
}
|
|
|
|
case 5: {
|
2016-08-25 23:41:31 +01:00
|
|
|
return "PopRSBHint{}";
|
2016-08-25 02:58:50 +01:00
|
|
|
}
|
|
|
|
case 6: {
|
|
|
|
auto terminal = boost::get<IR::Term::If>(terminal_variant);
|
2018-01-17 00:34:33 +00:00
|
|
|
return fmt::format("If{{{}, {}, {}}}", A64::CondToString(terminal.if_), TerminalToString(terminal.then_), TerminalToString(terminal.else_));
|
2016-08-25 02:58:50 +01:00
|
|
|
}
|
|
|
|
case 7: {
|
2018-01-07 16:33:02 +00:00
|
|
|
auto terminal = boost::get<IR::Term::CheckBit>(terminal_variant);
|
|
|
|
return fmt::format("CheckBit{{{}, {}}}", TerminalToString(terminal.then_), TerminalToString(terminal.else_));
|
|
|
|
}
|
|
|
|
case 8: {
|
2016-08-25 02:58:50 +01:00
|
|
|
auto terminal = boost::get<IR::Term::CheckHalt>(terminal_variant);
|
2016-08-25 23:41:31 +01:00
|
|
|
return fmt::format("CheckHalt{{{}}}", TerminalToString(terminal.else_));
|
2016-08-25 02:58:50 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return "<invalid terminal>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:53:36 +01:00
|
|
|
std::string DumpBlock(const IR::Block& block) {
|
|
|
|
std::string ret;
|
|
|
|
|
2016-09-05 21:31:25 +01:00
|
|
|
ret += fmt::format("Block: location={}\n", block.Location());
|
2016-08-25 23:41:31 +01:00
|
|
|
ret += fmt::format("cycles={}", block.CycleCount());
|
2018-01-17 00:34:33 +00:00
|
|
|
ret += fmt::format(", entry_cond={}", A64::CondToString(block.GetCondition()));
|
2018-01-01 15:23:56 +00:00
|
|
|
if (block.GetCondition() != Cond::AL) {
|
2016-09-05 21:31:25 +01:00
|
|
|
ret += fmt::format(", cond_fail={}", block.ConditionFailedLocation());
|
2016-08-17 15:53:36 +01:00
|
|
|
}
|
2016-08-25 23:41:31 +01:00
|
|
|
ret += '\n';
|
2016-08-17 15:53:36 +01:00
|
|
|
|
|
|
|
std::map<const IR::Inst*, size_t> inst_to_index;
|
|
|
|
size_t index = 0;
|
|
|
|
|
|
|
|
const auto arg_to_string = [&inst_to_index](const IR::Value& arg) -> std::string {
|
|
|
|
if (arg.IsEmpty()) {
|
|
|
|
return "<null>";
|
|
|
|
} else if (!arg.IsImmediate()) {
|
2016-08-25 23:41:31 +01:00
|
|
|
return fmt::format("%{}", inst_to_index.at(arg.GetInst()));
|
2016-08-17 15:53:36 +01:00
|
|
|
}
|
|
|
|
switch (arg.GetType()) {
|
2016-08-22 23:40:30 +01:00
|
|
|
case Type::U1:
|
2016-08-25 23:41:31 +01:00
|
|
|
return fmt::format("#{}", arg.GetU1() ? '1' : '0');
|
2016-08-22 23:40:30 +01:00
|
|
|
case Type::U8:
|
2016-08-25 23:41:31 +01:00
|
|
|
return fmt::format("#{}", arg.GetU8());
|
2018-06-27 15:06:20 +01:00
|
|
|
case Type::U16:
|
|
|
|
return fmt::format("#{:#x}", arg.GetU16());
|
2016-08-22 23:40:30 +01:00
|
|
|
case Type::U32:
|
2016-08-25 23:41:31 +01:00
|
|
|
return fmt::format("#{:#x}", arg.GetU32());
|
2018-01-08 22:03:03 +00:00
|
|
|
case Type::U64:
|
|
|
|
return fmt::format("#{:#x}", arg.GetU64());
|
2018-01-05 20:30:41 +00:00
|
|
|
case Type::A32Reg:
|
2018-01-01 15:23:56 +00:00
|
|
|
return A32::RegToString(arg.GetA32RegRef());
|
2018-01-05 20:30:41 +00:00
|
|
|
case Type::A32ExtReg:
|
2018-01-01 15:23:56 +00:00
|
|
|
return A32::ExtRegToString(arg.GetA32ExtRegRef());
|
2018-01-07 00:11:57 +00:00
|
|
|
case Type::A64Reg:
|
|
|
|
return A64::RegToString(arg.GetA64RegRef());
|
|
|
|
case Type::A64Vec:
|
|
|
|
return A64::VecToString(arg.GetA64VecRef());
|
2016-08-22 23:40:30 +01:00
|
|
|
default:
|
|
|
|
return "<unknown immediate type>";
|
2016-08-17 15:53:36 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-23 17:35:57 +01:00
|
|
|
for (const auto& inst : block) {
|
|
|
|
const Opcode op = inst.GetOpcode();
|
2016-08-17 15:53:36 +01:00
|
|
|
|
2017-11-27 19:35:12 +00:00
|
|
|
ret += fmt::format("[{:016x}] ", reinterpret_cast<u64>(&inst));
|
2016-08-17 15:53:36 +01:00
|
|
|
if (GetTypeOf(op) != Type::Void) {
|
2016-08-25 23:41:31 +01:00
|
|
|
ret += fmt::format("%{:<5} = ", index);
|
2016-08-17 15:53:36 +01:00
|
|
|
} else {
|
|
|
|
ret += " "; // '%00000 = ' -> 1 + 5 + 3 = 9 spaces
|
|
|
|
}
|
|
|
|
|
|
|
|
ret += GetNameOf(op);
|
|
|
|
|
|
|
|
const size_t arg_count = GetNumArgsOf(op);
|
|
|
|
for (size_t arg_index = 0; arg_index < arg_count; arg_index++) {
|
2016-08-23 17:35:57 +01:00
|
|
|
const Value arg = inst.GetArg(arg_index);
|
2016-08-17 13:29:05 +01:00
|
|
|
|
2016-08-17 15:53:36 +01:00
|
|
|
ret += arg_index != 0 ? ", " : " ";
|
2016-08-17 13:29:05 +01:00
|
|
|
ret += arg_to_string(arg);
|
|
|
|
|
|
|
|
Type actual_type = arg.GetType();
|
|
|
|
Type expected_type = GetArgTypeOf(op, arg_index);
|
2016-08-19 01:34:14 +01:00
|
|
|
if (!AreTypesCompatible(actual_type, expected_type)) {
|
2016-08-25 23:41:31 +01:00
|
|
|
ret += fmt::format("<type error: {} != {}>", GetNameOf(actual_type), GetNameOf(expected_type));
|
2016-08-17 13:29:05 +01:00
|
|
|
}
|
2016-08-17 15:53:36 +01:00
|
|
|
}
|
|
|
|
|
2017-11-27 19:35:12 +00:00
|
|
|
ret += fmt::format(" (uses: {})", inst.UseCount());
|
|
|
|
|
2016-08-25 23:41:31 +01:00
|
|
|
ret += '\n';
|
2016-08-23 17:35:57 +01:00
|
|
|
inst_to_index[&inst] = index++;
|
2016-08-17 15:53:36 +01:00
|
|
|
}
|
|
|
|
|
2016-08-25 23:41:31 +01:00
|
|
|
ret += "terminal = " + TerminalToString(block.GetTerminal()) + '\n';
|
2016-08-25 02:58:50 +01:00
|
|
|
|
2016-08-17 15:53:36 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-26 13:51:48 +00:00
|
|
|
} // namespace Dynarmic::IR
|