2014-01-25 07:26:15 +01:00
|
|
|
/**
|
|
|
|
|
******************************************************************************
|
|
|
|
|
* Xenia : Xbox 360 Emulator Research Project *
|
|
|
|
|
******************************************************************************
|
|
|
|
|
* Copyright 2014 Ben Vanik. All rights reserved. *
|
|
|
|
|
* Released under the BSD license - see LICENSE in the root for more details. *
|
|
|
|
|
******************************************************************************
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <alloy/compiler/passes/finalization_pass.h>
|
|
|
|
|
|
|
|
|
|
#include <alloy/backend/backend.h>
|
|
|
|
|
#include <alloy/compiler/compiler.h>
|
|
|
|
|
#include <alloy/runtime/runtime.h>
|
2014-08-22 05:49:47 +02:00
|
|
|
#include <xenia/profiling.h>
|
2014-01-25 07:26:15 +01:00
|
|
|
|
2014-07-11 05:20:00 +02:00
|
|
|
namespace alloy {
|
|
|
|
|
namespace compiler {
|
|
|
|
|
namespace passes {
|
|
|
|
|
|
|
|
|
|
// TODO(benvanik): remove when enums redefined.
|
2014-01-25 07:26:15 +01:00
|
|
|
using namespace alloy::hir;
|
|
|
|
|
|
2014-07-11 05:20:00 +02:00
|
|
|
using alloy::hir::HIRBuilder;
|
2014-01-25 07:26:15 +01:00
|
|
|
|
2014-07-11 05:20:00 +02:00
|
|
|
FinalizationPass::FinalizationPass() : CompilerPass() {}
|
2014-01-25 07:26:15 +01:00
|
|
|
|
2014-07-11 05:20:00 +02:00
|
|
|
FinalizationPass::~FinalizationPass() {}
|
2014-01-25 07:26:15 +01:00
|
|
|
|
|
|
|
|
int FinalizationPass::Run(HIRBuilder* builder) {
|
2014-05-29 04:19:39 +02:00
|
|
|
SCOPE_profile_cpu_f("alloy");
|
|
|
|
|
|
2014-01-25 07:26:15 +01:00
|
|
|
// Process the HIR and prepare it for lowering.
|
|
|
|
|
// After this is done the HIR should be ready for emitting.
|
|
|
|
|
|
|
|
|
|
auto arena = builder->arena();
|
|
|
|
|
|
2014-01-26 06:20:28 +01:00
|
|
|
uint32_t block_ordinal = 0;
|
2014-01-25 07:26:15 +01:00
|
|
|
auto block = builder->first_block();
|
|
|
|
|
while (block) {
|
2014-01-26 06:20:28 +01:00
|
|
|
block->ordinal = block_ordinal++;
|
|
|
|
|
|
2014-01-25 07:26:15 +01:00
|
|
|
// Ensure all labels have names.
|
|
|
|
|
auto label = block->label_head;
|
|
|
|
|
while (label) {
|
|
|
|
|
if (!label->name) {
|
2014-05-14 09:14:56 +02:00
|
|
|
const size_t label_len = 6 + 4 + 1;
|
|
|
|
|
char* name = (char*)arena->Alloc(label_len);
|
2014-08-17 20:48:29 +02:00
|
|
|
snprintf(name, label_len, "_label%d", label->id);
|
2014-01-25 07:26:15 +01:00
|
|
|
label->name = name;
|
|
|
|
|
}
|
|
|
|
|
label = label->next;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-26 06:35:59 +01:00
|
|
|
// Remove unneeded jumps.
|
|
|
|
|
auto tail = block->instr_tail;
|
|
|
|
|
if (tail && tail->opcode == &OPCODE_BRANCH_info) {
|
|
|
|
|
// Jump. Check target.
|
|
|
|
|
auto target = tail->src1.label;
|
|
|
|
|
if (target->block == block->next) {
|
|
|
|
|
// Jumping to subsequent block. Remove.
|
|
|
|
|
tail->Remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-26 05:30:18 +01:00
|
|
|
|
2014-01-25 07:26:15 +01:00
|
|
|
block = block->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2014-07-11 05:20:00 +02:00
|
|
|
|
|
|
|
|
} // namespace passes
|
|
|
|
|
} // namespace compiler
|
|
|
|
|
} // namespace alloy
|