From dec0e3595714331167a224703a9bfa5873fbd281 Mon Sep 17 00:00:00 2001 From: Ben Vanik Date: Sun, 29 Dec 2013 19:54:17 -0800 Subject: [PATCH] More X64 backend skeleton work. --- src/alloy/backend/ivm/ivm_assembler.cc | 2 +- src/alloy/backend/ivm/ivm_backend.cc | 2 +- src/alloy/backend/ivm/sources.gypi | 1 + src/alloy/backend/ivm/tracing.h | 56 +++++++++++++++ src/alloy/backend/tracing.h | 22 +----- src/alloy/backend/x64/lir/lir_builder.h | 46 ++++++++++++ src/alloy/backend/x64/lir/lir_instr.h | 34 +++++++++ src/alloy/backend/x64/lir/lir_opcodes.cc | 29 ++++++++ src/alloy/backend/x64/lir/lir_opcodes.h | 71 +++++++++++++++++++ src/alloy/backend/x64/lir/lir_opcodes.inl | 15 ++++ src/alloy/backend/x64/lir/lircodes.cc | 0 src/alloy/backend/x64/lir/lircodes.h | 0 src/alloy/backend/x64/lir/lircodes.inl | 0 src/alloy/backend/x64/lir/sources.gypi | 6 +- src/alloy/backend/x64/lir/tracing.h | 39 ++++++++++ src/alloy/backend/x64/optimizer/optimizer.cc | 58 +++++++++++++++ src/alloy/backend/x64/optimizer/optimizer.h | 54 ++++++++++++++ .../backend/x64/optimizer/optimizer_pass.cc | 29 ++++++++ .../backend/x64/optimizer/optimizer_pass.h | 49 +++++++++++++ .../backend/x64/optimizer/optimizer_passes.h | 15 ++++ .../x64/optimizer/passes/redundant_mov_pass.h | 39 ++++++++++ src/alloy/backend/x64/optimizer/tracing.h | 47 ++++++++++++ src/alloy/backend/x64/tracing.h | 59 +++++++++++++++ src/alloy/backend/x64/x64_assembler.cc | 59 +++++++++++++++ src/alloy/backend/x64/x64_assembler.h | 47 ++++++++++++ src/alloy/backend/x64/x64_backend.cc | 44 ++++++++++++ src/alloy/backend/x64/x64_backend.h | 42 +++++++++++ src/alloy/backend/x64/x64_codegen.cc | 32 +++++++++ src/alloy/backend/x64/x64_codegen.h | 44 ++++++++++++ src/alloy/backend/x64/x64_emitter.cc | 32 +++++++++ src/alloy/backend/x64/x64_emitter.h | 44 ++++++++++++ src/alloy/backend/x64/x64_function.cc | 39 ++++++++++ src/alloy/backend/x64/x64_function.h | 45 ++++++++++++ src/alloy/runtime/runtime.cc | 19 +++-- 34 files changed, 1091 insertions(+), 29 deletions(-) create mode 100644 src/alloy/backend/ivm/tracing.h create mode 100644 src/alloy/backend/x64/lir/lir_opcodes.cc create mode 100644 src/alloy/backend/x64/lir/lir_opcodes.h create mode 100644 src/alloy/backend/x64/lir/lir_opcodes.inl delete mode 100644 src/alloy/backend/x64/lir/lircodes.cc delete mode 100644 src/alloy/backend/x64/lir/lircodes.h delete mode 100644 src/alloy/backend/x64/lir/lircodes.inl diff --git a/src/alloy/backend/ivm/ivm_assembler.cc b/src/alloy/backend/ivm/ivm_assembler.cc index 5eb049d33..ec75bcf5a 100644 --- a/src/alloy/backend/ivm/ivm_assembler.cc +++ b/src/alloy/backend/ivm/ivm_assembler.cc @@ -10,9 +10,9 @@ #include #include -#include #include #include +#include #include #include #include diff --git a/src/alloy/backend/ivm/ivm_backend.cc b/src/alloy/backend/ivm/ivm_backend.cc index 83546ae10..5d8955593 100644 --- a/src/alloy/backend/ivm/ivm_backend.cc +++ b/src/alloy/backend/ivm/ivm_backend.cc @@ -9,8 +9,8 @@ #include -#include #include +#include using namespace alloy; using namespace alloy::backend; diff --git a/src/alloy/backend/ivm/sources.gypi b/src/alloy/backend/ivm/sources.gypi index 0e9c55d1c..4b285c6e6 100644 --- a/src/alloy/backend/ivm/sources.gypi +++ b/src/alloy/backend/ivm/sources.gypi @@ -9,5 +9,6 @@ 'ivm_backend.h', 'ivm_function.cc', 'ivm_function.h', + 'tracing.h', ], } diff --git a/src/alloy/backend/ivm/tracing.h b/src/alloy/backend/ivm/tracing.h new file mode 100644 index 000000000..a1fcdf20d --- /dev/null +++ b/src/alloy/backend/ivm/tracing.h @@ -0,0 +1,56 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef ALLOY_BACKEND_IVM_TRACING_H_ +#define ALLOY_BACKEND_IVM_TRACING_H_ + +#include + + +namespace alloy { +namespace backend { +namespace ivm { + +const uint32_t ALLOY_BACKEND_IVM = + alloy::backend::EventType::ALLOY_BACKEND_IVM; + + +class EventType { +public: + enum { + ALLOY_BACKEND_IVM_INIT = ALLOY_BACKEND_IVM | (1), + ALLOY_BACKEND_IVM_DEINIT = ALLOY_BACKEND_IVM | (2), + + ALLOY_BACKEND_IVM_ASSEMBLER = ALLOY_BACKEND_IVM | (1 << 20), + ALLOY_BACKEND_IVM_ASSEMBLER_INIT = ALLOY_BACKEND_IVM_ASSEMBLER | (1), + ALLOY_BACKEND_IVM_ASSEMBLER_DEINIT = ALLOY_BACKEND_IVM_ASSEMBLER | (2), + }; + + typedef struct { + static const uint32_t event_type = ALLOY_BACKEND_IVM_INIT; + } Init; + typedef struct { + static const uint32_t event_type = ALLOY_BACKEND_IVM_DEINIT; + } Deinit; + + typedef struct { + static const uint32_t event_type = ALLOY_BACKEND_IVM_ASSEMBLER_INIT; + } AssemblerInit; + typedef struct { + static const uint32_t event_type = ALLOY_BACKEND_IVM_ASSEMBLER_DEINIT; + } AssemblerDeinit; +}; + + +} // namespace ivm +} // namespace backend +} // namespace alloy + + +#endif // ALLOY_BACKEND_IVM_TRACING_H_ diff --git a/src/alloy/backend/tracing.h b/src/alloy/backend/tracing.h index c137f633c..5aca2bd80 100644 --- a/src/alloy/backend/tracing.h +++ b/src/alloy/backend/tracing.h @@ -23,27 +23,9 @@ const uint32_t ALLOY_BACKEND = alloy::tracing::EventType::ALLOY_BACKEND; class EventType { public: enum { - ALLOY_BACKEND_INIT = ALLOY_BACKEND | (1), - ALLOY_BACKEND_DEINIT = ALLOY_BACKEND | (2), - - ALLOY_BACKEND_ASSEMBLER = ALLOY_BACKEND | (1 << 25), - ALLOY_BACKEND_ASSEMBLER_INIT = ALLOY_BACKEND_ASSEMBLER | (1), - ALLOY_BACKEND_ASSEMBLER_DEINIT = ALLOY_BACKEND_ASSEMBLER | (2), + ALLOY_BACKEND_IVM = ALLOY_BACKEND | (1 << 24), + ALLOY_BACKEND_X64 = ALLOY_BACKEND | (2 << 24), }; - - typedef struct { - static const uint32_t event_type = ALLOY_BACKEND_INIT; - } Init; - typedef struct { - static const uint32_t event_type = ALLOY_BACKEND_DEINIT; - } Deinit; - - typedef struct { - static const uint32_t event_type = ALLOY_BACKEND_ASSEMBLER_INIT; - } AssemblerInit; - typedef struct { - static const uint32_t event_type = ALLOY_BACKEND_ASSEMBLER_DEINIT; - } AssemblerDeinit; }; diff --git a/src/alloy/backend/x64/lir/lir_builder.h b/src/alloy/backend/x64/lir/lir_builder.h index e69de29bb..bbd9b6c87 100644 --- a/src/alloy/backend/x64/lir/lir_builder.h +++ b/src/alloy/backend/x64/lir/lir_builder.h @@ -0,0 +1,46 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef ALLOY_BACKEND_X64_LIR_LIR_BUILDER_H_ +#define ALLOY_BACKEND_X64_LIR_LIR_BUILDER_H_ + +#include +#include + + +namespace alloy { +namespace backend { +namespace x64 { +namespace lir { + + +class LIRBuilder { +public: + LIRBuilder(); + virtual ~LIRBuilder(); + + virtual void Reset(); + virtual int Finalize(); + + void Dump(StringBuffer* str); + + Arena* arena() const { return arena_; } + +protected: + Arena* arena_; +}; + + +} // namespace lir +} // namespace x64 +} // namespace backend +} // namespace alloy + + +#endif // ALLOY_BACKEND_X64_LIR_LIR_BUILDER_H_ diff --git a/src/alloy/backend/x64/lir/lir_instr.h b/src/alloy/backend/x64/lir/lir_instr.h index e69de29bb..0c72f23f4 100644 --- a/src/alloy/backend/x64/lir/lir_instr.h +++ b/src/alloy/backend/x64/lir/lir_instr.h @@ -0,0 +1,34 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef ALLOY_BACKEND_X64_LIR_LIR_INSTR_H_ +#define ALLOY_BACKEND_X64_LIR_LIR_INSTR_H_ + +#include +#include + + +namespace alloy { +namespace backend { +namespace x64 { +namespace lir { + + +class LIRInstr { +public: +}; + + +} // namespace lir +} // namespace x64 +} // namespace backend +} // namespace alloy + + +#endif // ALLOY_BACKEND_X64_LIR_LIR_INSTR_H_ diff --git a/src/alloy/backend/x64/lir/lir_opcodes.cc b/src/alloy/backend/x64/lir/lir_opcodes.cc new file mode 100644 index 000000000..42d1b568d --- /dev/null +++ b/src/alloy/backend/x64/lir/lir_opcodes.cc @@ -0,0 +1,29 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#include + +using namespace alloy; +using namespace alloy::backend::x64::lir; + + +namespace alloy { +namespace backend { +namespace x64 { +namespace lir { + +#define DEFINE_OPCODE(num, name, sig, flags) \ + static const LIROpcodeInfo num##_info = { flags, sig, name, num, }; +#include +#undef DEFINE_OPCODE + +} // namespace lir +} // namespace x64 +} // namespace backend +} // namespace alloy diff --git a/src/alloy/backend/x64/lir/lir_opcodes.h b/src/alloy/backend/x64/lir/lir_opcodes.h new file mode 100644 index 000000000..8abb53405 --- /dev/null +++ b/src/alloy/backend/x64/lir/lir_opcodes.h @@ -0,0 +1,71 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef ALLOY_BACKEND_X64_LIR_LIR_OPCODES_H_ +#define ALLOY_BACKEND_X64_LIR_LIR_OPCODES_H_ + +#include + + +namespace alloy { +namespace backend { +namespace x64 { +namespace lir { + + +enum LIROpcode { + LIR_OPCODE_MOV_I32, + + __LIR_OPCODE_MAX_VALUE, // Keep at end. +}; + +enum LIROpcodeFlags { + LIR_OPCODE_FLAG_BRANCH = (1 << 1), + LIR_OPCODE_FLAG_MEMORY = (1 << 2), + LIR_OPCODE_FLAG_COMMUNATIVE = (1 << 3), + LIR_OPCODE_FLAG_VOLATILE = (1 << 4), + LIR_OPCODE_FLAG_IGNORE = (1 << 5), + LIR_OPCODE_FLAG_HIDE = (1 << 6), +}; + +enum LIROpcodeSignatureType { + // 3 bits max (0-7) + LIR_OPCODE_SIG_TYPE_X = 0, +}; + +enum LIROpcodeSignature { + LIR_OPCODE_SIG_X = (LIR_OPCODE_SIG_TYPE_X), +}; + +#define GET_LIR_OPCODE_SIG_TYPE_DEST(sig) (LIROpcodeSignatureType)(sig & 0x7) +#define GET_LIR_OPCODE_SIG_TYPE_SRC1(sig) (LIROpcodeSignatureType)((sig >> 3) & 0x7) +#define GET_LIR_OPCODE_SIG_TYPE_SRC2(sig) (LIROpcodeSignatureType)((sig >> 6) & 0x7) +#define GET_LIR_OPCODE_SIG_TYPE_SRC3(sig) (LIROpcodeSignatureType)((sig >> 9) & 0x7) + +typedef struct { + uint32_t flags; + uint32_t signature; + const char* name; + LIROpcode num; +} LIROpcodeInfo; + + +#define DEFINE_OPCODE(num, name, sig, flags) \ + extern const LIROpcodeInfo num##_info; +#include +#undef DEFINE_OPCODE + + +} // namespace lir +} // namespace x64 +} // namespace backend +} // namespace alloy + + +#endif // ALLOY_BACKEND_X64_LIR_LIR_OPCODES_H_ diff --git a/src/alloy/backend/x64/lir/lir_opcodes.inl b/src/alloy/backend/x64/lir/lir_opcodes.inl new file mode 100644 index 000000000..5fcb6eaef --- /dev/null +++ b/src/alloy/backend/x64/lir/lir_opcodes.inl @@ -0,0 +1,15 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + + + DEFINE_OPCODE( + LIR_OPCODE_MOV_I32, + "mov.i32", + LIR_OPCODE_SIG_X, + 0); diff --git a/src/alloy/backend/x64/lir/lircodes.cc b/src/alloy/backend/x64/lir/lircodes.cc deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/alloy/backend/x64/lir/lircodes.h b/src/alloy/backend/x64/lir/lircodes.h deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/alloy/backend/x64/lir/lircodes.inl b/src/alloy/backend/x64/lir/lircodes.inl deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/alloy/backend/x64/lir/sources.gypi b/src/alloy/backend/x64/lir/sources.gypi index 50538ff83..d6b2f21e8 100644 --- a/src/alloy/backend/x64/lir/sources.gypi +++ b/src/alloy/backend/x64/lir/sources.gypi @@ -4,9 +4,9 @@ 'lir_builder.cc', 'lir_builder.h', 'lir_instr.h', - 'lircodes.cc', - 'lircodes.h', - 'lircodes.inl', + 'lir_opcodes.cc', + 'lir_opcodes.h', + 'lir_opcodes.inl', 'tracing.h', ], } diff --git a/src/alloy/backend/x64/lir/tracing.h b/src/alloy/backend/x64/lir/tracing.h index e69de29bb..0b6d7382a 100644 --- a/src/alloy/backend/x64/lir/tracing.h +++ b/src/alloy/backend/x64/lir/tracing.h @@ -0,0 +1,39 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef ALLOY_BACKEND_X64_LIR_TRACING_H_ +#define ALLOY_BACKEND_X64_LIR_TRACING_H_ + +#include + + +namespace alloy { +namespace backend { +namespace x64 { +namespace lir { + +const uint32_t ALLOY_BACKEND_X64_LIR = + alloy::backend::x64::EventType::ALLOY_BACKEND_X64_LIR; + + +class EventType { +public: + enum { + ALLOY_BACKEND_X64_LIR_FOO = ALLOY_BACKEND_X64_LIR | (1), + }; +}; + + +} // namespace lir +} // namespace x64 +} // namespace backend +} // namespace alloy + + +#endif // ALLOY_BACKEND_X64_LIR_TRACING_H_ diff --git a/src/alloy/backend/x64/optimizer/optimizer.cc b/src/alloy/backend/x64/optimizer/optimizer.cc index e69de29bb..6aa25b00a 100644 --- a/src/alloy/backend/x64/optimizer/optimizer.cc +++ b/src/alloy/backend/x64/optimizer/optimizer.cc @@ -0,0 +1,58 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#include + +#include +#include + +using namespace alloy; +using namespace alloy::backend::x64::lir; +using namespace alloy::backend::x64::optimizer; +using namespace alloy::runtime; + + +Optimizer::Optimizer(Runtime* runtime) : + runtime_(runtime) { + alloy::tracing::WriteEvent(EventType::Init({ + })); +} + +Optimizer::~Optimizer() { + Reset(); + + for (auto it = passes_.begin(); it != passes_.end(); ++it) { + OptimizerPass* pass = *it; + delete pass; + } + + alloy::tracing::WriteEvent(EventType::Deinit({ + })); +} + +void Optimizer::AddPass(OptimizerPass* pass) { + pass->Initialize(this); + passes_.push_back(pass); +} + +void Optimizer::Reset() { +} + +int Optimizer::Optimize(LIRBuilder* builder) { + // TODO(benvanik): sophisticated stuff. Run passes in parallel, run until they + // stop changing things, etc. + for (auto it = passes_.begin(); it != passes_.end(); ++it) { + OptimizerPass* pass = *it; + if (pass->Run(builder)) { + return 1; + } + } + + return 0; +} diff --git a/src/alloy/backend/x64/optimizer/optimizer.h b/src/alloy/backend/x64/optimizer/optimizer.h index e69de29bb..a991fb2a2 100644 --- a/src/alloy/backend/x64/optimizer/optimizer.h +++ b/src/alloy/backend/x64/optimizer/optimizer.h @@ -0,0 +1,54 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef ALLOY_BACKEND_X64_OPTIMIZER_OPTIMIZER_H_ +#define ALLOY_BACKEND_X64_OPTIMIZER_OPTIMIZER_H_ + +#include +#include + +namespace alloy { namespace runtime { class Runtime; } } + + +namespace alloy { +namespace backend { +namespace x64 { +namespace optimizer { + +class OptimizerPass; + + +class Optimizer { +public: + Optimizer(runtime::Runtime* runtime); + ~Optimizer(); + + runtime::Runtime* runtime() const { return runtime_; } + + void AddPass(OptimizerPass* pass); + + void Reset(); + + int Optimize(lir::LIRBuilder* builder); + +private: + runtime::Runtime* runtime_; + + typedef std::vector PassList; + PassList passes_; +}; + + +} // namespace optimizer +} // namespace x64 +} // namespace backend +} // namespace alloy + + +#endif // ALLOY_BACKEND_X64_OPTIMIZER_OPTIMIZER_H_ diff --git a/src/alloy/backend/x64/optimizer/optimizer_pass.cc b/src/alloy/backend/x64/optimizer/optimizer_pass.cc index e69de29bb..bc00b2705 100644 --- a/src/alloy/backend/x64/optimizer/optimizer_pass.cc +++ b/src/alloy/backend/x64/optimizer/optimizer_pass.cc @@ -0,0 +1,29 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#include + +#include + +using namespace alloy; +using namespace alloy::backend::x64::optimizer; + + +OptimizerPass::OptimizerPass() : + runtime_(0), optimizer_(0) { +} + +OptimizerPass::~OptimizerPass() { +} + +int OptimizerPass::Initialize(Optimizer* optimizer) { + runtime_ = optimizer->runtime(); + optimizer_ = optimizer; + return 0; +} diff --git a/src/alloy/backend/x64/optimizer/optimizer_pass.h b/src/alloy/backend/x64/optimizer/optimizer_pass.h index e69de29bb..c5ad80616 100644 --- a/src/alloy/backend/x64/optimizer/optimizer_pass.h +++ b/src/alloy/backend/x64/optimizer/optimizer_pass.h @@ -0,0 +1,49 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef ALLOY_BACKEND_X64_OPTIMIZER_OPTIMIZER_PASS_H_ +#define ALLOY_BACKEND_X64_OPTIMIZER_OPTIMIZER_PASS_H_ + +#include + +#include + +namespace alloy { namespace runtime { class Runtime; } } + + +namespace alloy { +namespace backend { +namespace x64 { +namespace optimizer { + +class Optimizer; + + +class OptimizerPass { +public: + OptimizerPass(); + virtual ~OptimizerPass(); + + virtual int Initialize(Optimizer* optimizer); + + virtual int Run(lir::LIRBuilder* builder) = 0; + +protected: + runtime::Runtime* runtime_; + Optimizer* optimizer_; +}; + + +} // namespace optimizer +} // namespace x64 +} // namespace backend +} // namespace alloy + + +#endif // ALLOY_BACKEND_X64_OPTIMIZER_OPTIMIZER_PASS_H_ diff --git a/src/alloy/backend/x64/optimizer/optimizer_passes.h b/src/alloy/backend/x64/optimizer/optimizer_passes.h index e69de29bb..2c2a0e5dd 100644 --- a/src/alloy/backend/x64/optimizer/optimizer_passes.h +++ b/src/alloy/backend/x64/optimizer/optimizer_passes.h @@ -0,0 +1,15 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef ALLOY_BACKEND_X64_OPTIMIZER_PASSES_H_ +#define ALLOY_BACKEND_X64_OPTIMIZER_PASSES_H_ + +#include + +#endif // ALLOY_BACKEND_X64_OPTIMIZER_PASSES_H_ diff --git a/src/alloy/backend/x64/optimizer/passes/redundant_mov_pass.h b/src/alloy/backend/x64/optimizer/passes/redundant_mov_pass.h index e69de29bb..06796584d 100644 --- a/src/alloy/backend/x64/optimizer/passes/redundant_mov_pass.h +++ b/src/alloy/backend/x64/optimizer/passes/redundant_mov_pass.h @@ -0,0 +1,39 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef ALLOY_COMPILER_PASSES_SIMPLIFICATION_PASS_H_ +#define ALLOY_COMPILER_PASSES_SIMPLIFICATION_PASS_H_ + +#include + + +namespace alloy { +namespace backend { +namespace x64 { +namespace optimizer { +namespace passes { + + +class RedundantMovPass : public OptimizerPass { +public: + RedundantMovPass(); + virtual ~RedundantMovPass(); + + virtual int Run(lir::LIRBuilder* builder); +}; + + +} // namespace passes +} // namespace optimizer +} // namespace x64 +} // namespace backend +} // namespace alloy + + +#endif // ALLOY_COMPILER_PASSES_SIMPLIFICATION_PASS_H_ diff --git a/src/alloy/backend/x64/optimizer/tracing.h b/src/alloy/backend/x64/optimizer/tracing.h index e69de29bb..d2a8bad04 100644 --- a/src/alloy/backend/x64/optimizer/tracing.h +++ b/src/alloy/backend/x64/optimizer/tracing.h @@ -0,0 +1,47 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef ALLOY_BACKEND_X64_OPTIMIZER_TRACING_H_ +#define ALLOY_BACKEND_X64_OPTIMIZER_TRACING_H_ + +#include + + +namespace alloy { +namespace backend { +namespace x64 { +namespace optimizer { + +const uint32_t ALLOY_BACKEND_X64_OPTIMIZER = + alloy::backend::x64::EventType::ALLOY_BACKEND_X64_OPTIMIZER; + + +class EventType { +public: + enum { + ALLOY_BACKEND_X64_OPTIMIZER_INIT = ALLOY_BACKEND_X64_OPTIMIZER | (1), + ALLOY_BACKEND_X64_OPTIMIZER_DEINIT = ALLOY_BACKEND_X64_OPTIMIZER | (2), + }; + + typedef struct { + static const uint32_t event_type = ALLOY_BACKEND_X64_OPTIMIZER_INIT; + } Init; + typedef struct { + static const uint32_t event_type = ALLOY_BACKEND_X64_OPTIMIZER_DEINIT; + } Deinit; +}; + + +} // namespace optimizer +} // namespace x64 +} // namespace backend +} // namespace alloy + + +#endif // ALLOY_BACKEND_X64_OPTIMIZER_TRACING_H_ diff --git a/src/alloy/backend/x64/tracing.h b/src/alloy/backend/x64/tracing.h index e69de29bb..e24a15f4a 100644 --- a/src/alloy/backend/x64/tracing.h +++ b/src/alloy/backend/x64/tracing.h @@ -0,0 +1,59 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef ALLOY_BACKEND_X64_TRACING_H_ +#define ALLOY_BACKEND_X64_TRACING_H_ + +#include + + +namespace alloy { +namespace backend { +namespace x64 { + +const uint32_t ALLOY_BACKEND_X64 = + alloy::backend::EventType::ALLOY_BACKEND_X64; + + +class EventType { +public: + enum { + ALLOY_BACKEND_X64_INIT = ALLOY_BACKEND_X64 | (1), + ALLOY_BACKEND_X64_DEINIT = ALLOY_BACKEND_X64 | (2), + + ALLOY_BACKEND_X64_ASSEMBLER = ALLOY_BACKEND_X64 | (1 << 20), + ALLOY_BACKEND_X64_ASSEMBLER_INIT = ALLOY_BACKEND_X64_ASSEMBLER | (1), + ALLOY_BACKEND_X64_ASSEMBLER_DEINIT = ALLOY_BACKEND_X64_ASSEMBLER | (2), + + ALLOY_BACKEND_X64_LIR = ALLOY_BACKEND_X64 | (2 << 20), + ALLOY_BACKEND_X64_OPTIMIZER = ALLOY_BACKEND_X64 | (3 << 20), + }; + + typedef struct { + static const uint32_t event_type = ALLOY_BACKEND_X64_INIT; + } Init; + typedef struct { + static const uint32_t event_type = ALLOY_BACKEND_X64_DEINIT; + } Deinit; + + typedef struct { + static const uint32_t event_type = ALLOY_BACKEND_X64_ASSEMBLER_INIT; + } AssemblerInit; + typedef struct { + static const uint32_t event_type = ALLOY_BACKEND_X64_ASSEMBLER_DEINIT; + } AssemblerDeinit; +}; + + +} // namespace x64 +} // namespace backend +} // namespace alloy + + +#endif // ALLOY_BACKEND_X64_TRACING_H_ diff --git a/src/alloy/backend/x64/x64_assembler.cc b/src/alloy/backend/x64/x64_assembler.cc index e69de29bb..09f809a16 100644 --- a/src/alloy/backend/x64/x64_assembler.cc +++ b/src/alloy/backend/x64/x64_assembler.cc @@ -0,0 +1,59 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#include + +#include +#include +#include +#include +#include +#include + +using namespace alloy; +using namespace alloy::backend; +using namespace alloy::backend::x64; +using namespace alloy::hir; +using namespace alloy::runtime; + + +X64Assembler::X64Assembler(X64Backend* backend) : + Assembler(backend) { +} + +X64Assembler::~X64Assembler() { + alloy::tracing::WriteEvent(EventType::AssemblerDeinit({ + })); +} + +int X64Assembler::Initialize() { + int result = Assembler::Initialize(); + if (result) { + return result; + } + + alloy::tracing::WriteEvent(EventType::AssemblerInit({ + })); + + return result; +} + +void X64Assembler::Reset() { + Assembler::Reset(); +} + +int X64Assembler::Assemble( + FunctionInfo* symbol_info, HIRBuilder* builder, + DebugInfo* debug_info, Function** out_function) { + X64Function* fn = new X64Function(symbol_info); + fn->set_debug_info(debug_info); + + *out_function = fn; + return 0; +} diff --git a/src/alloy/backend/x64/x64_assembler.h b/src/alloy/backend/x64/x64_assembler.h index e69de29bb..a883eadca 100644 --- a/src/alloy/backend/x64/x64_assembler.h +++ b/src/alloy/backend/x64/x64_assembler.h @@ -0,0 +1,47 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef ALLOY_BACKEND_X64_X64_ASSEMBLER_H_ +#define ALLOY_BACKEND_X64_X64_ASSEMBLER_H_ + +#include + +#include + + +namespace alloy { +namespace backend { +namespace x64 { + +class X64Backend; + + +class X64Assembler : public Assembler { +public: + X64Assembler(X64Backend* backend); + virtual ~X64Assembler(); + + virtual int Initialize(); + + virtual void Reset(); + + virtual int Assemble( + runtime::FunctionInfo* symbol_info, hir::HIRBuilder* builder, + runtime::DebugInfo* debug_info, runtime::Function** out_function); + +private: +}; + + +} // namespace x64 +} // namespace backend +} // namespace alloy + + +#endif // ALLOY_BACKEND_X64_X64_ASSEMBLER_H_ diff --git a/src/alloy/backend/x64/x64_backend.cc b/src/alloy/backend/x64/x64_backend.cc index e69de29bb..dcebf165e 100644 --- a/src/alloy/backend/x64/x64_backend.cc +++ b/src/alloy/backend/x64/x64_backend.cc @@ -0,0 +1,44 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#include + +#include +#include + +using namespace alloy; +using namespace alloy::backend; +using namespace alloy::backend::x64; +using namespace alloy::runtime; + + +X64Backend::X64Backend(Runtime* runtime) : + Backend(runtime) { +} + +X64Backend::~X64Backend() { + alloy::tracing::WriteEvent(EventType::Deinit({ + })); +} + +int X64Backend::Initialize() { + int result = Backend::Initialize(); + if (result) { + return result; + } + + alloy::tracing::WriteEvent(EventType::Init({ + })); + + return result; +} + +Assembler* X64Backend::CreateAssembler() { + return new X64Assembler(this); +} diff --git a/src/alloy/backend/x64/x64_backend.h b/src/alloy/backend/x64/x64_backend.h index e69de29bb..e9102d418 100644 --- a/src/alloy/backend/x64/x64_backend.h +++ b/src/alloy/backend/x64/x64_backend.h @@ -0,0 +1,42 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef ALLOY_BACKEND_X64_X64_BACKEND_H_ +#define ALLOY_BACKEND_X64_X64_BACKEND_H_ + +#include + +#include + + +namespace alloy { +namespace backend { +namespace x64 { + + +#define ALLOY_HAS_X64_BACKEND 1 + + +class X64Backend : public Backend { +public: + X64Backend(runtime::Runtime* runtime); + virtual ~X64Backend(); + + virtual int Initialize(); + + virtual Assembler* CreateAssembler(); +}; + + +} // namespace x64 +} // namespace backend +} // namespace alloy + + +#endif // ALLOY_BACKEND_X64_X64_BACKEND_H_ diff --git a/src/alloy/backend/x64/x64_codegen.cc b/src/alloy/backend/x64/x64_codegen.cc index e69de29bb..153292e58 100644 --- a/src/alloy/backend/x64/x64_codegen.cc +++ b/src/alloy/backend/x64/x64_codegen.cc @@ -0,0 +1,32 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#include + +#include + +using namespace alloy; +using namespace alloy::backend; +using namespace alloy::backend::x64; +using namespace alloy::runtime; + + +X64Codegen::X64Codegen(X64Backend* backend) : + backend_(backend) { +} + +X64Codegen::~X64Codegen() { +} + +int X64Codegen::Initialize() { + return 0; +} + +void X64Codegen::Reset() { +} diff --git a/src/alloy/backend/x64/x64_codegen.h b/src/alloy/backend/x64/x64_codegen.h index e69de29bb..0fd0dd8bb 100644 --- a/src/alloy/backend/x64/x64_codegen.h +++ b/src/alloy/backend/x64/x64_codegen.h @@ -0,0 +1,44 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef ALLOY_BACKEND_X64_X64_CODEGEN_H_ +#define ALLOY_BACKEND_X64_X64_CODEGEN_H_ + +#include + + +namespace alloy { +namespace backend { +namespace x64 { + +class X64Backend; + + +class X64Codegen { +public: + X64Codegen(X64Backend* backend); + ~X64Codegen(); + + int Initialize(); + + void Reset(); + + // + +private: + X64Backend* backend_; +}; + + +} // namespace x64 +} // namespace backend +} // namespace alloy + + +#endif // ALLOY_BACKEND_X64_X64_CODEGEN_H_ diff --git a/src/alloy/backend/x64/x64_emitter.cc b/src/alloy/backend/x64/x64_emitter.cc index e69de29bb..274fbeae4 100644 --- a/src/alloy/backend/x64/x64_emitter.cc +++ b/src/alloy/backend/x64/x64_emitter.cc @@ -0,0 +1,32 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#include + +#include + +using namespace alloy; +using namespace alloy::backend; +using namespace alloy::backend::x64; +using namespace alloy::runtime; + + +X64Emitter::X64Emitter(X64Backend* backend) : + backend_(backend) { +} + +X64Emitter::~X64Emitter() { +} + +int X64Emitter::Initialize() { + return 0; +} + +void X64Emitter::Reset() { +} diff --git a/src/alloy/backend/x64/x64_emitter.h b/src/alloy/backend/x64/x64_emitter.h index e69de29bb..ea38efaf3 100644 --- a/src/alloy/backend/x64/x64_emitter.h +++ b/src/alloy/backend/x64/x64_emitter.h @@ -0,0 +1,44 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef ALLOY_BACKEND_X64_X64_EMITTER_H_ +#define ALLOY_BACKEND_X64_X64_EMITTER_H_ + +#include + + +namespace alloy { +namespace backend { +namespace x64 { + +class X64Backend; + + +class X64Emitter { +public: + X64Emitter(X64Backend* backend); + ~X64Emitter(); + + int Initialize(); + + void Reset(); + + // + +private: + X64Backend* backend_; +}; + + +} // namespace x64 +} // namespace backend +} // namespace alloy + + +#endif // ALLOY_BACKEND_X64_X64_EMITTER_H_ diff --git a/src/alloy/backend/x64/x64_function.cc b/src/alloy/backend/x64/x64_function.cc index e69de29bb..3279408fa 100644 --- a/src/alloy/backend/x64/x64_function.cc +++ b/src/alloy/backend/x64/x64_function.cc @@ -0,0 +1,39 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#include + +#include +#include +#include + +using namespace alloy; +using namespace alloy::backend; +using namespace alloy::backend::x64; +using namespace alloy::runtime; + + +X64Function::X64Function(FunctionInfo* symbol_info) : + GuestFunction(symbol_info) { +} + +X64Function::~X64Function() { +} + +int X64Function::AddBreakpointImpl(Breakpoint* breakpoint) { + return 0; +} + +int X64Function::RemoveBreakpointImpl(Breakpoint* breakpoint) { + return 0; +} + +int X64Function::CallImpl(ThreadState* thread_state, uint64_t return_address) { + return 0; +} diff --git a/src/alloy/backend/x64/x64_function.h b/src/alloy/backend/x64/x64_function.h index e69de29bb..379c5143f 100644 --- a/src/alloy/backend/x64/x64_function.h +++ b/src/alloy/backend/x64/x64_function.h @@ -0,0 +1,45 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2013 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef ALLOY_BACKEND_X64_X64_FUNCTION_H_ +#define ALLOY_BACKEND_X64_X64_FUNCTION_H_ + +#include +#include +#include + + +namespace alloy { +namespace backend { +namespace x64 { + + +class X64Function : public runtime::GuestFunction { +public: + X64Function(runtime::FunctionInfo* symbol_info); + virtual ~X64Function(); + + //void Setup(TranslationContext& ctx); + +protected: + virtual int AddBreakpointImpl(runtime::Breakpoint* breakpoint); + virtual int RemoveBreakpointImpl(runtime::Breakpoint* breakpoint); + virtual int CallImpl(runtime::ThreadState* thread_state, + uint64_t return_address); + +private: +}; + + +} // namespace x64 +} // namespace backend +} // namespace alloy + + +#endif // ALLOY_BACKEND_X64_X64_FUNCTION_H_ diff --git a/src/alloy/runtime/runtime.cc b/src/alloy/runtime/runtime.cc index 360e18184..2cda2c6f8 100644 --- a/src/alloy/runtime/runtime.cc +++ b/src/alloy/runtime/runtime.cc @@ -58,6 +58,7 @@ Runtime::~Runtime() { // TODO(benvanik): based on compiler support #include +#include int Runtime::Initialize(Frontend* frontend, Backend* backend) { // Must be initialized by subclass before calling into this. @@ -76,21 +77,31 @@ int Runtime::Initialize(Frontend* frontend, Backend* backend) { } if (!backend) { +#if defined(ALLOY_HAS_X64_BACKEND) && ALLOY_HAS_X64_BACKEND + if (FLAGS_runtime_backend == "x64") { + backend = new alloy::backend::x64::X64Backend( + this); + } +#endif // ALLOY_HAS_X64_BACKEND #if defined(ALLOY_HAS_IVM_BACKEND) && ALLOY_HAS_IVM_BACKEND if (FLAGS_runtime_backend == "ivm") { backend = new alloy::backend::ivm::IVMBackend( this); } -#endif - // x64/etc +#endif // ALLOY_HAS_IVM_BACKEND if (FLAGS_runtime_backend == "any") { - // x64/etc +#if defined(ALLOY_HAS_X64_BACKEND) && ALLOY_HAS_X64_BACKEND + /*if (!backend) { + backend = new alloy::backend::x64::X64Backend( + this); + }*/ +#endif // ALLOY_HAS_X64_BACKEND #if defined(ALLOY_HAS_IVM_BACKEND) && ALLOY_HAS_IVM_BACKEND if (!backend) { backend = new alloy::backend::ivm::IVMBackend( this); } -#endif +#endif // ALLOY_HAS_IVM_BACKEND } }