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.
|
|
|
|
*/
|
|
|
|
|
2016-07-07 10:53:09 +01:00
|
|
|
#include "common/assert.h"
|
2016-07-14 14:39:43 +01:00
|
|
|
#include "ir_emitter.h"
|
2016-07-01 14:01:06 +01:00
|
|
|
|
|
|
|
namespace Dynarmic {
|
|
|
|
namespace Arm {
|
|
|
|
|
|
|
|
void IREmitter::Unimplemented() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-11 22:43:53 +01:00
|
|
|
u32 IREmitter::PC() {
|
|
|
|
u32 offset = current_location.TFlag ? 4 : 8;
|
|
|
|
return current_location.arm_pc + offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 IREmitter::AlignPC(size_t alignment) {
|
|
|
|
u32 pc = PC();
|
|
|
|
return static_cast<u32>(pc - pc % alignment);
|
|
|
|
}
|
|
|
|
|
2016-07-08 10:09:18 +01:00
|
|
|
IR::ValuePtr IREmitter::Imm1(bool value) {
|
|
|
|
auto imm1 = std::make_shared<IR::ImmU1>(value);
|
|
|
|
AddToBlock(imm1);
|
|
|
|
return imm1;
|
|
|
|
}
|
|
|
|
|
2016-07-01 14:01:06 +01:00
|
|
|
IR::ValuePtr IREmitter::Imm8(u8 i) {
|
|
|
|
auto imm8 = std::make_shared<IR::ImmU8>(i);
|
|
|
|
AddToBlock(imm8);
|
|
|
|
return imm8;
|
|
|
|
}
|
|
|
|
|
2016-07-08 10:09:18 +01:00
|
|
|
IR::ValuePtr IREmitter::Imm32(u32 i) {
|
|
|
|
auto imm32 = std::make_shared<IR::ImmU32>(i);
|
|
|
|
AddToBlock(imm32);
|
|
|
|
return imm32;
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::ValuePtr IREmitter::GetRegister(Reg reg) {
|
|
|
|
if (reg == Reg::PC) {
|
2016-07-11 22:43:53 +01:00
|
|
|
return Imm32(PC());
|
2016-07-08 10:09:18 +01:00
|
|
|
}
|
2016-07-01 14:01:06 +01:00
|
|
|
return Inst(IR::Opcode::GetRegister, { RegRef(reg) });
|
|
|
|
}
|
|
|
|
|
|
|
|
void IREmitter::SetRegister(const Reg reg, IR::ValuePtr value) {
|
2016-07-11 22:43:53 +01:00
|
|
|
ASSERT(reg != Reg::PC);
|
2016-07-01 14:01:06 +01:00
|
|
|
Inst(IR::Opcode::SetRegister, { RegRef(reg), value });
|
|
|
|
}
|
|
|
|
|
2016-07-08 10:09:18 +01:00
|
|
|
void IREmitter::ALUWritePC(IR::ValuePtr value) {
|
|
|
|
// This behaviour is ARM version-dependent.
|
2016-07-12 10:58:14 +01:00
|
|
|
// The below implementation is for ARMv6k
|
2016-07-18 21:04:39 +01:00
|
|
|
BranchWritePC(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IREmitter::BranchWritePC(IR::ValuePtr value) {
|
2016-07-12 10:58:14 +01:00
|
|
|
if (!current_location.TFlag) {
|
|
|
|
auto new_pc = And(value, Imm32(0xFFFFFFFC));
|
|
|
|
Inst(IR::Opcode::SetRegister, { RegRef(Reg::PC), new_pc });
|
|
|
|
} else {
|
|
|
|
auto new_pc = And(value, Imm32(0xFFFFFFFE));
|
|
|
|
Inst(IR::Opcode::SetRegister, { RegRef(Reg::PC), new_pc });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-18 21:04:39 +01:00
|
|
|
void IREmitter::BXWritePC(IR::ValuePtr value) {
|
|
|
|
Inst(IR::Opcode::BXWritePC, {value});
|
|
|
|
}
|
|
|
|
|
2016-07-12 10:58:14 +01:00
|
|
|
void IREmitter::LoadWritePC(IR::ValuePtr value) {
|
|
|
|
// This behaviour is ARM version-dependent.
|
|
|
|
// The below implementation is for ARMv6k
|
2016-07-18 21:04:39 +01:00
|
|
|
BXWritePC(value);
|
2016-07-08 10:09:18 +01:00
|
|
|
}
|
|
|
|
|
2016-07-14 14:04:43 +01:00
|
|
|
void IREmitter::CallSupervisor(IR::ValuePtr value) {
|
|
|
|
Inst(IR::Opcode::CallSupervisor, {value});
|
|
|
|
}
|
|
|
|
|
2016-07-01 14:01:06 +01:00
|
|
|
IR::ValuePtr IREmitter::GetCFlag() {
|
|
|
|
return Inst(IR::Opcode::GetCFlag, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
void IREmitter::SetNFlag(IR::ValuePtr value) {
|
|
|
|
Inst(IR::Opcode::SetNFlag, {value});
|
|
|
|
}
|
|
|
|
|
|
|
|
void IREmitter::SetZFlag(IR::ValuePtr value) {
|
|
|
|
Inst(IR::Opcode::SetZFlag, {value});
|
|
|
|
}
|
|
|
|
|
|
|
|
void IREmitter::SetCFlag(IR::ValuePtr value) {
|
|
|
|
Inst(IR::Opcode::SetCFlag, {value});
|
|
|
|
}
|
|
|
|
|
2016-07-08 10:09:18 +01:00
|
|
|
void IREmitter::SetVFlag(IR::ValuePtr value) {
|
|
|
|
Inst(IR::Opcode::SetVFlag, {value});
|
|
|
|
}
|
|
|
|
|
2016-07-11 23:06:35 +01:00
|
|
|
IR::ValuePtr IREmitter::LeastSignificantHalf(IR::ValuePtr value) {
|
|
|
|
return Inst(IR::Opcode::LeastSignificantHalf, {value});
|
|
|
|
}
|
|
|
|
|
2016-07-01 14:01:06 +01:00
|
|
|
IR::ValuePtr IREmitter::LeastSignificantByte(IR::ValuePtr value) {
|
|
|
|
return Inst(IR::Opcode::LeastSignificantByte, {value});
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::ValuePtr IREmitter::MostSignificantBit(IR::ValuePtr value) {
|
|
|
|
return Inst(IR::Opcode::MostSignificantBit, {value});
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::ValuePtr IREmitter::IsZero(IR::ValuePtr value) {
|
|
|
|
return Inst(IR::Opcode::IsZero, {value});
|
|
|
|
}
|
|
|
|
|
|
|
|
IREmitter::ResultAndCarry IREmitter::LogicalShiftLeft(IR::ValuePtr value_in, IR::ValuePtr shift_amount, IR::ValuePtr carry_in) {
|
|
|
|
auto result = Inst(IR::Opcode::LogicalShiftLeft, {value_in, shift_amount, carry_in});
|
|
|
|
auto carry_out = Inst(IR::Opcode::GetCarryFromOp, {result});
|
|
|
|
return {result, carry_out};
|
|
|
|
}
|
|
|
|
|
|
|
|
IREmitter::ResultAndCarry IREmitter::LogicalShiftRight(IR::ValuePtr value_in, IR::ValuePtr shift_amount, IR::ValuePtr carry_in) {
|
|
|
|
auto result = Inst(IR::Opcode::LogicalShiftRight, {value_in, shift_amount, carry_in});
|
|
|
|
auto carry_out = Inst(IR::Opcode::GetCarryFromOp, {result});
|
|
|
|
return {result, carry_out};
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:22:11 +01:00
|
|
|
IREmitter::ResultAndCarry IREmitter::ArithmeticShiftRight(IR::ValuePtr value_in, IR::ValuePtr shift_amount, IR::ValuePtr carry_in) {
|
|
|
|
auto result = Inst(IR::Opcode::ArithmeticShiftRight, {value_in, shift_amount, carry_in});
|
|
|
|
auto carry_out = Inst(IR::Opcode::GetCarryFromOp, {result});
|
|
|
|
return {result, carry_out};
|
|
|
|
}
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2016-07-10 01:18:17 +01:00
|
|
|
IREmitter::ResultAndCarry IREmitter::RotateRight(IR::ValuePtr value_in, IR::ValuePtr shift_amount, IR::ValuePtr carry_in) {
|
|
|
|
auto result = Inst(IR::Opcode::RotateRight, {value_in, shift_amount, carry_in});
|
|
|
|
auto carry_out = Inst(IR::Opcode::GetCarryFromOp, {result});
|
|
|
|
return {result, carry_out};
|
|
|
|
}
|
|
|
|
|
2016-07-08 10:09:18 +01:00
|
|
|
IREmitter::ResultAndCarryAndOverflow IREmitter::AddWithCarry(IR::ValuePtr a, IR::ValuePtr b, IR::ValuePtr carry_in) {
|
|
|
|
auto result = Inst(IR::Opcode::AddWithCarry, {a, b, carry_in});
|
|
|
|
auto carry_out = Inst(IR::Opcode::GetCarryFromOp, {result});
|
|
|
|
auto overflow = Inst(IR::Opcode::GetOverflowFromOp, {result});
|
|
|
|
return {result, carry_out, overflow};
|
|
|
|
}
|
|
|
|
|
2016-07-11 22:43:53 +01:00
|
|
|
IR::ValuePtr IREmitter::Add(IR::ValuePtr a, IR::ValuePtr b) {
|
|
|
|
return Inst(IR::Opcode::AddWithCarry, {a, b, Imm1(0)});
|
|
|
|
}
|
|
|
|
|
2016-07-08 11:49:30 +01:00
|
|
|
IREmitter::ResultAndCarryAndOverflow IREmitter::SubWithCarry(IR::ValuePtr a, IR::ValuePtr b, IR::ValuePtr carry_in) {
|
|
|
|
// This is equivalent to AddWithCarry(a, Not(b), carry_in).
|
|
|
|
auto result = Inst(IR::Opcode::SubWithCarry, {a, b, carry_in});
|
|
|
|
auto carry_out = Inst(IR::Opcode::GetCarryFromOp, {result});
|
|
|
|
auto overflow = Inst(IR::Opcode::GetOverflowFromOp, {result});
|
|
|
|
return {result, carry_out, overflow};
|
|
|
|
}
|
|
|
|
|
2016-07-18 15:11:16 +01:00
|
|
|
IR::ValuePtr IREmitter::Sub(IR::ValuePtr a, IR::ValuePtr b) {
|
|
|
|
return Inst(IR::Opcode::SubWithCarry, {a, b, Imm1(1)});
|
|
|
|
}
|
|
|
|
|
2016-07-08 10:43:28 +01:00
|
|
|
IR::ValuePtr IREmitter::And(IR::ValuePtr a, IR::ValuePtr b) {
|
|
|
|
return Inst(IR::Opcode::And, {a, b});
|
|
|
|
}
|
|
|
|
|
2016-07-08 11:14:50 +01:00
|
|
|
IR::ValuePtr IREmitter::Eor(IR::ValuePtr a, IR::ValuePtr b) {
|
|
|
|
return Inst(IR::Opcode::Eor, {a, b});
|
|
|
|
}
|
|
|
|
|
2016-07-10 02:06:38 +01:00
|
|
|
IR::ValuePtr IREmitter::Or(IR::ValuePtr a, IR::ValuePtr b) {
|
|
|
|
return Inst(IR::Opcode::Or, {a, b});
|
|
|
|
}
|
|
|
|
|
2016-07-10 03:44:45 +01:00
|
|
|
IR::ValuePtr IREmitter::Not(IR::ValuePtr a) {
|
|
|
|
return Inst(IR::Opcode::Not, {a});
|
|
|
|
}
|
|
|
|
|
2016-07-16 19:23:42 +01:00
|
|
|
IR::ValuePtr IREmitter::SignExtendHalfToWord(IR::ValuePtr a) {
|
|
|
|
return Inst(IR::Opcode::SignExtendHalfToWord, {a});
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::ValuePtr IREmitter::SignExtendByteToWord(IR::ValuePtr a) {
|
|
|
|
return Inst(IR::Opcode::SignExtendByteToWord, {a});
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::ValuePtr IREmitter::ZeroExtendHalfToWord(IR::ValuePtr a) {
|
|
|
|
return Inst(IR::Opcode::ZeroExtendHalfToWord, {a});
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::ValuePtr IREmitter::ZeroExtendByteToWord(IR::ValuePtr a) {
|
|
|
|
return Inst(IR::Opcode::ZeroExtendByteToWord, {a});
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::ValuePtr IREmitter::ByteReverseWord(IR::ValuePtr a) {
|
|
|
|
return Inst(IR::Opcode::ByteReverseWord, {a});
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::ValuePtr IREmitter::ByteReverseHalf(IR::ValuePtr a) {
|
|
|
|
return Inst(IR::Opcode::ByteReverseHalf, {a});
|
|
|
|
}
|
|
|
|
|
2016-07-20 15:34:17 +01:00
|
|
|
IR::ValuePtr IREmitter::ByteReverseDual(IR::ValuePtr a) {
|
|
|
|
return Inst(IR::Opcode::ByteReverseDual, {a});
|
|
|
|
}
|
2016-07-16 19:23:42 +01:00
|
|
|
|
2016-07-11 22:43:53 +01:00
|
|
|
IR::ValuePtr IREmitter::ReadMemory8(IR::ValuePtr vaddr) {
|
|
|
|
return Inst(IR::Opcode::ReadMemory8, {vaddr});
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::ValuePtr IREmitter::ReadMemory16(IR::ValuePtr vaddr) {
|
2016-07-20 15:34:17 +01:00
|
|
|
auto value = Inst(IR::Opcode::ReadMemory16, {vaddr});
|
|
|
|
return current_location.EFlag ? ByteReverseHalf(value) : value;
|
2016-07-11 22:43:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IR::ValuePtr IREmitter::ReadMemory32(IR::ValuePtr vaddr) {
|
2016-07-20 15:34:17 +01:00
|
|
|
auto value = Inst(IR::Opcode::ReadMemory32, {vaddr});
|
|
|
|
return current_location.EFlag ? ByteReverseWord(value) : value;
|
2016-07-11 22:43:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IR::ValuePtr IREmitter::ReadMemory64(IR::ValuePtr vaddr) {
|
2016-07-20 15:34:17 +01:00
|
|
|
auto value = Inst(IR::Opcode::ReadMemory64, {vaddr});
|
|
|
|
return current_location.EFlag ? ByteReverseDual(value) : value;
|
2016-07-11 22:43:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IREmitter::WriteMemory8(IR::ValuePtr vaddr, IR::ValuePtr value) {
|
|
|
|
Inst(IR::Opcode::WriteMemory8, {vaddr, value});
|
|
|
|
}
|
|
|
|
|
|
|
|
void IREmitter::WriteMemory16(IR::ValuePtr vaddr, IR::ValuePtr value) {
|
2016-07-20 15:34:17 +01:00
|
|
|
if (current_location.EFlag) {
|
|
|
|
value = ByteReverseHalf(value);
|
|
|
|
}
|
2016-07-11 22:43:53 +01:00
|
|
|
Inst(IR::Opcode::WriteMemory16, {vaddr, value});
|
|
|
|
}
|
|
|
|
|
|
|
|
void IREmitter::WriteMemory32(IR::ValuePtr vaddr, IR::ValuePtr value) {
|
2016-07-20 15:34:17 +01:00
|
|
|
if (current_location.EFlag) {
|
|
|
|
value = ByteReverseWord(value);
|
|
|
|
}
|
2016-07-11 22:43:53 +01:00
|
|
|
Inst(IR::Opcode::WriteMemory32, {vaddr, value});
|
|
|
|
}
|
|
|
|
|
|
|
|
void IREmitter::WriteMemory64(IR::ValuePtr vaddr, IR::ValuePtr value) {
|
2016-07-20 15:34:17 +01:00
|
|
|
if (current_location.EFlag) {
|
|
|
|
value = ByteReverseDual(value);
|
|
|
|
}
|
2016-07-11 22:43:53 +01:00
|
|
|
Inst(IR::Opcode::WriteMemory64, {vaddr, value});
|
|
|
|
}
|
|
|
|
|
2016-07-07 10:53:09 +01:00
|
|
|
void IREmitter::SetTerm(const IR::Terminal& terminal) {
|
|
|
|
ASSERT_MSG(block.terminal.which() == 0, "Terminal has already been set.");
|
|
|
|
block.terminal = terminal;
|
|
|
|
}
|
|
|
|
|
2016-07-01 14:01:06 +01:00
|
|
|
IR::ValuePtr IREmitter::Inst(IR::Opcode op, std::initializer_list<IR::ValuePtr> args) {
|
|
|
|
auto inst = std::make_shared<IR::Inst>(op);
|
|
|
|
assert(args.size() == inst->NumArgs());
|
|
|
|
|
|
|
|
std::for_each(args.begin(), args.end(), [&inst, op, index = size_t(0)](const auto& v) mutable {
|
|
|
|
assert(IR::GetArgTypeOf(op, index) == v->GetType());
|
|
|
|
inst->SetArg(index, v);
|
|
|
|
index++;
|
|
|
|
});
|
|
|
|
|
|
|
|
AddToBlock(inst);
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
IR::ValuePtr IREmitter::RegRef(Reg reg) {
|
|
|
|
auto regref = std::make_shared<IR::ImmRegRef>(reg);
|
|
|
|
AddToBlock(regref);
|
|
|
|
return regref;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IREmitter::AddToBlock(IR::ValuePtr value) {
|
|
|
|
block.instructions.emplace_back(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Arm
|
|
|
|
} // namespace Dynarmic
|
|
|
|
|