2021-05-05 05:19:08 +00:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2021-05-07 09:31:30 +00:00
|
|
|
#include "shader_recompiler/backend/glasm/emit_context.h"
|
2021-05-05 05:19:08 +00:00
|
|
|
#include "shader_recompiler/backend/glasm/reg_alloc.h"
|
|
|
|
#include "shader_recompiler/exception.h"
|
|
|
|
#include "shader_recompiler/frontend/ir/value.h"
|
|
|
|
|
|
|
|
namespace Shader::Backend::GLASM {
|
|
|
|
namespace {
|
|
|
|
std::string Representation(Id id) {
|
|
|
|
if (id.is_condition_code != 0) {
|
|
|
|
throw NotImplementedException("Condition code");
|
|
|
|
}
|
|
|
|
if (id.is_spill != 0) {
|
|
|
|
throw NotImplementedException("Spilling");
|
|
|
|
}
|
|
|
|
const u32 index{static_cast<u32>(id.index)};
|
2021-05-07 09:31:30 +00:00
|
|
|
return fmt::format("R{}.x", index);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ImmValue(const IR::Value& value) {
|
|
|
|
switch (value.Type()) {
|
|
|
|
case IR::Type::U1:
|
|
|
|
return value.U1() ? "-1" : "0";
|
|
|
|
case IR::Type::U32:
|
|
|
|
return fmt::format("{}", value.U32());
|
|
|
|
case IR::Type::F32:
|
|
|
|
return fmt::format("{}", value.F32());
|
|
|
|
default:
|
2021-05-08 22:59:05 +00:00
|
|
|
throw NotImplementedException("Immediate type {}", value.Type());
|
2021-05-05 05:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
2021-05-07 09:31:30 +00:00
|
|
|
std::string RegAlloc::Define(IR::Inst& inst) {
|
|
|
|
const Id id{Alloc()};
|
2021-05-05 05:19:08 +00:00
|
|
|
inst.SetDefinition<Id>(id);
|
|
|
|
return Representation(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RegAlloc::Consume(const IR::Value& value) {
|
2021-05-07 09:31:30 +00:00
|
|
|
if (value.IsImmediate()) {
|
|
|
|
return ImmValue(value);
|
|
|
|
} else {
|
|
|
|
return Consume(*value.InstRecursive());
|
2021-05-05 05:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RegAlloc::Consume(IR::Inst& inst) {
|
|
|
|
const Id id{inst.Definition<Id>()};
|
|
|
|
inst.DestructiveRemoveUsage();
|
|
|
|
if (!inst.HasUses()) {
|
|
|
|
Free(id);
|
|
|
|
}
|
|
|
|
return Representation(inst.Definition<Id>());
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:31:30 +00:00
|
|
|
Id RegAlloc::Alloc() {
|
2021-05-05 05:19:08 +00:00
|
|
|
for (size_t reg = 0; reg < NUM_REGS; ++reg) {
|
|
|
|
if (register_use[reg]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
num_used_registers = std::max(num_used_registers, reg + 1);
|
|
|
|
register_use[reg] = true;
|
2021-05-08 19:46:32 +00:00
|
|
|
Id ret{};
|
|
|
|
ret.index.Assign(static_cast<u32>(reg));
|
|
|
|
ret.is_spill.Assign(0);
|
|
|
|
ret.is_condition_code.Assign(0);
|
|
|
|
return ret;
|
2021-05-05 05:19:08 +00:00
|
|
|
}
|
|
|
|
throw NotImplementedException("Register spilling");
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegAlloc::Free(Id id) {
|
|
|
|
if (id.is_spill != 0) {
|
|
|
|
throw NotImplementedException("Free spill");
|
|
|
|
}
|
|
|
|
register_use[id.index] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Shader::Backend::GLASM
|