From 86d1095df7989a3383ef498cfd2c2e7d0a096965 Mon Sep 17 00:00:00 2001 From: MerryMage Date: Sun, 7 Jan 2018 13:42:11 +0000 Subject: [PATCH] A64: Implement branch --- src/CMakeLists.txt | 1 + src/backend_x64/a64_emit_x64.cpp | 4 +-- src/frontend/A64/decoder/a64.h | 6 ++-- src/frontend/A64/location_descriptor.h | 2 +- src/frontend/A64/translate/impl/branch.cpp | 42 ++++++++++++++++++++++ 5 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 src/frontend/A64/translate/impl/branch.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9678890b..5f684831 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -58,6 +58,7 @@ add_library(dynarmic frontend/A64/ir_emitter.h frontend/A64/location_descriptor.cpp frontend/A64/location_descriptor.h + frontend/A64/translate/impl/branch.cpp frontend/A64/translate/impl/data_processing_addsub.cpp frontend/A64/translate/impl/data_processing_logical.cpp frontend/A64/translate/impl/data_processing_pcrel.cpp diff --git a/src/backend_x64/a64_emit_x64.cpp b/src/backend_x64/a64_emit_x64.cpp index 5fbeda8a..e4b97838 100644 --- a/src/backend_x64/a64_emit_x64.cpp +++ b/src/backend_x64/a64_emit_x64.cpp @@ -266,7 +266,7 @@ void A64EmitX64::EmitPatchJg(const IR::LocationDescriptor& target_desc, CodePtr code->mov(qword[r15 + offsetof(A64JitState, pc)], A64::LocationDescriptor{target_desc}.PC()); code->jg(code->GetReturnFromRunCodeAddress()); } - code->EnsurePatchLocationSize(patch_location, 14); + code->EnsurePatchLocationSize(patch_location, 17); } void A64EmitX64::EmitPatchJmp(const IR::LocationDescriptor& target_desc, CodePtr target_code_ptr) { @@ -277,7 +277,7 @@ void A64EmitX64::EmitPatchJmp(const IR::LocationDescriptor& target_desc, CodePtr code->mov(qword[r15 + offsetof(A64JitState, pc)], A64::LocationDescriptor{target_desc}.PC()); code->jmp(code->GetReturnFromRunCodeAddress()); } - code->EnsurePatchLocationSize(patch_location, 13); + code->EnsurePatchLocationSize(patch_location, 16); } void A64EmitX64::EmitPatchMovRcx(CodePtr target_code_ptr) { diff --git a/src/frontend/A64/decoder/a64.h b/src/frontend/A64/decoder/a64.h index 12d7b112..de29ab8d 100644 --- a/src/frontend/A64/decoder/a64.h +++ b/src/frontend/A64/decoder/a64.h @@ -59,7 +59,7 @@ std::vector> GetDecodeTable() { //INST(&V::EXTR, "EXTR", "z00100111N0mmmmmssssssnnnnnddddd"), // Conditional branch - //INST(&V::B_cond, "B.cond", "01010100iiiiiiiiiiiiiiiiiii0cccc"), + INST(&V::B_cond, "B.cond", "01010100iiiiiiiiiiiiiiiiiii0cccc"), // Exception generation //INST(&V::SVC, "SVC", "11010100000iiiiiiiiiiiiiiii00001"), @@ -113,8 +113,8 @@ std::vector> GetDecodeTable() { //INST(&V::DRPS, "DRPS", "11010110101111110000001111100000"), // Unconditonal branch (immediate) - //INST(&V::B_uncond, "B", "000101iiiiiiiiiiiiiiiiiiiiiiiiii"), - //INST(&V::BL, "BL", "100101iiiiiiiiiiiiiiiiiiiiiiiiii"), + INST(&V::B_uncond, "B", "000101iiiiiiiiiiiiiiiiiiiiiiiiii"), + INST(&V::BL, "BL", "100101iiiiiiiiiiiiiiiiiiiiiiiiii"), // Compare and branch (immediate) //INST(&V::CBZ, "CBZ", "z0110100iiiiiiiiiiiiiiiiiiittttt"), diff --git a/src/frontend/A64/location_descriptor.h b/src/frontend/A64/location_descriptor.h index 31a66e3f..a865c65b 100644 --- a/src/frontend/A64/location_descriptor.h +++ b/src/frontend/A64/location_descriptor.h @@ -44,7 +44,7 @@ public: return !operator==(o); } - LocationDescriptor SetPC(u32 new_pc) const { + LocationDescriptor SetPC(u64 new_pc) const { return LocationDescriptor(new_pc, fpcr); } diff --git a/src/frontend/A64/translate/impl/branch.cpp b/src/frontend/A64/translate/impl/branch.cpp new file mode 100644 index 00000000..305d81ce --- /dev/null +++ b/src/frontend/A64/translate/impl/branch.cpp @@ -0,0 +1,42 @@ +/* This file is part of the dynarmic project. + * Copyright (c) 2018 MerryMage + * This software may be used and distributed according to the terms of the GNU + * General Public License version 2 or any later version. + */ + +#include "frontend/A64/translate/impl/impl.h" + +namespace Dynarmic { +namespace A64 { + +bool TranslatorVisitor::B_cond(Imm<19> imm19, Cond cond) { + s64 offset = concatenate(imm19, Imm<2>{0}).SignExtend(); + + u64 target = ir.PC() + offset; + auto cond_pass = IR::Term::LinkBlock{ir.current_location.SetPC(target)}; + auto cond_fail = IR::Term::LinkBlock{ir.current_location.AdvancePC(4)}; + ir.SetTerm(IR::Term::If{cond, cond_pass, cond_fail}); + return false; +} + +bool TranslatorVisitor::B_uncond(Imm<26> imm26) { + s64 offset = concatenate(imm26, Imm<2>{0}).SignExtend(); + + u64 target = ir.PC() + offset; + ir.SetTerm(IR::Term::LinkBlock{ir.current_location.SetPC(target)}); + return false; +} + +bool TranslatorVisitor::BL(Imm<26> imm26) { + s64 offset = concatenate(imm26, Imm<2>{0}).SignExtend(); + + X(64, Reg::R30, ir.Imm64(ir.PC() + 4)); + ir.PushRSB(ir.current_location.AdvancePC(4)); + + u64 target = ir.PC() + offset; + ir.SetTerm(IR::Term::LinkBlock{ir.current_location.SetPC(target)}); + return false; +} + +} // namespace A64 +} // namespace Dynarmic