Skip to content

Commit

Permalink
Added intefered_register_size in AddressTuple and Virtual Registers
Browse files Browse the repository at this point in the history
  • Loading branch information
9Tempest committed Dec 7, 2023
1 parent add082f commit b9b0167
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 104 deletions.
47 changes: 32 additions & 15 deletions gematria/basic_block/basic_block.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ std::string AddressTuple::ToString() const {
buffer << "base_register='" << base_register << "', ";
buffer << "base_register_size=" << base_register_size << ", ";
buffer << "base_register_intefered_register={";
for (const std::string& interfered_register : base_register_intefered_register) {
for (const std::string& interfered_register :
base_register_intefered_register) {
buffer << "'" << interfered_register << "', ";
}
buffer << "}, ";
Expand All @@ -74,7 +75,8 @@ std::string AddressTuple::ToString() const {
buffer << "index_Register='" << index_register << "', ";
buffer << "index_register_size=" << index_register_size << ", ";
buffer << "index_register_intefered_register={";
for (const std::string& interfered_register : index_register_intefered_register) {
for (const std::string& interfered_register :
index_register_intefered_register) {
buffer << "'" << interfered_register << "', ";
}
buffer << "}, ";
Expand All @@ -86,7 +88,8 @@ std::string AddressTuple::ToString() const {
buffer << "segment_register='" << segment_register << "', ";
buffer << "segment_register_size=" << segment_register_size << ", ";
buffer << "segment_register_intefered_register={";
for (const std::string& interfered_register : segment_register_intefered_register) {
for (const std::string& interfered_register :
segment_register_intefered_register) {
buffer << "'" << interfered_register << "', ";
}
buffer << "}, ";
Expand Down Expand Up @@ -126,12 +129,15 @@ bool InstructionOperand::operator==(const InstructionOperand& other) const {
}

InstructionOperand InstructionOperand::VirtualRegister(
const std::string register_name, size_t size, const std::vector<std::string>& interfered_registers) {
const std::string register_name, size_t size,
const std::vector<std::string>& interfered_registers,
std::vector<int> interfered_registers_size) {
InstructionOperand result;
result.type_ = OperandType::kVirtualRegister;
result.register_name_ = std::move(register_name);
result.size_ = size;
result.interfered_registers_ = std::move(interfered_registers);
result.interfered_registers_size_ = std::move(interfered_registers_size);
return result;
}

Expand Down Expand Up @@ -166,14 +172,16 @@ InstructionOperand InstructionOperand::Address(AddressTuple address_tuple) {
return result;
}

InstructionOperand InstructionOperand::Address(std::string base_register,
int64_t displacement,
std::string index_register,
int scaling,
std::string segment_register,
int base_register_size,
int index_register_size,
int segment_register_size) {
InstructionOperand InstructionOperand::Address(
std::string base_register, int64_t displacement, std::string index_register,
int scaling, std::string segment_register, int base_register_size,
int index_register_size, int segment_register_size,
const std::vector<std::string>& base_register_intefered_register,
const std::vector<std::string>& index_register_intefered_register,
const std::vector<std::string>& segment_register_intefered_register,
const std::vector<int>& base_register_intefered_register_size,
const std::vector<int>& index_register_intefered_register_size,
const std::vector<int>& segment_register_intefered_register_size) {
InstructionOperand result;
result.type_ = OperandType::kAddress;
result.address_.base_register = std::move(base_register);
Expand All @@ -184,9 +192,18 @@ InstructionOperand InstructionOperand::Address(std::string base_register,
result.address_.base_register_size = base_register_size;
result.address_.index_register_size = index_register_size;
result.address_.segment_register_size = segment_register_size;
result.address_.base_register_intefered_register = {};
result.address_.index_register_intefered_register = {};
result.address_.segment_register_intefered_register = {};
result.address_.base_register_intefered_register =
std::move(base_register_intefered_register);
result.address_.index_register_intefered_register =
std::move(index_register_intefered_register);
result.address_.segment_register_intefered_register =
std::move(segment_register_intefered_register);
result.address_.base_register_intefered_register_sizes =
std::move(base_register_intefered_register_size);
result.address_.index_register_intefered_register_sizes =
std::move(index_register_intefered_register_size);
result.address_.segment_register_intefered_register_sizes =
std::move(segment_register_intefered_register_size);
return result;
}

Expand Down
80 changes: 56 additions & 24 deletions gematria/basic_block/basic_block.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ inline constexpr std::string_view kDisplacementToken = "_DISPLACEMENT_";
inline constexpr std::string_view kVirtualRegisterToken = "_VREG";
inline std::string getVREG_TOKEN(size_t size) {
return std::string(kVirtualRegisterToken) + std::to_string(size) + "_";
}
}

// The type of an operand of an instruction.
enum class OperandType {
Expand Down Expand Up @@ -83,13 +83,17 @@ struct AddressTuple {
AddressTuple() {}
AddressTuple(const AddressTuple&) = default;
AddressTuple(AddressTuple&&) = default;
AddressTuple(std::string base_register, int64_t displacement,
std::string index_register, int scaling,
std::string segment_register, int base_register_size = 64,
int index_register_size = 64, int segment_register_size = 64,
const std::vector<std::string> base_register_intefered_register = {},
const std::vector<std::string> index_register_intefered_register = {},
const std::vector<std::string> segment_register_intefered_register = {})
AddressTuple(
std::string base_register, int64_t displacement,
std::string index_register, int scaling, std::string segment_register,
int base_register_size = 64, int index_register_size = 64,
int segment_register_size = 64,
const std::vector<std::string> base_register_intefered_register = {},
const std::vector<std::string> index_register_intefered_register = {},
const std::vector<std::string> segment_register_intefered_register = {},
const std::vector<int> base_register_intefered_register_sizes = {},
const std::vector<int> index_register_intefered_register_sizes = {},
const std::vector<int> segment_register_intefered_register_sizes = {})
: base_register(std::move(base_register)),
displacement(displacement),
index_register(std::move(index_register)),
Expand All @@ -98,10 +102,18 @@ struct AddressTuple {
base_register_size(std::move(base_register_size)),
index_register_size(std::move(index_register_size)),
segment_register_size(std::move(segment_register_size)),
base_register_intefered_register(std::move(base_register_intefered_register)),
index_register_intefered_register(std::move(index_register_intefered_register)),
segment_register_intefered_register(std::move(segment_register_intefered_register))
{}
base_register_intefered_register(
std::move(base_register_intefered_register)),
base_register_intefered_register_sizes(
std::move(base_register_intefered_register_sizes)),
index_register_intefered_register(
std::move(index_register_intefered_register)),
index_register_intefered_register_sizes(
std::move(index_register_intefered_register_sizes)),
segment_register_intefered_register(
std::move(segment_register_intefered_register)),
segment_register_intefered_register_sizes(
std::move(segment_register_intefered_register_sizes)) {}

AddressTuple& operator=(const AddressTuple&) = default;
AddressTuple& operator=(AddressTuple&&) = default;
Expand Down Expand Up @@ -144,12 +156,18 @@ struct AddressTuple {
// The size of the segment register. Used only when segment_register is
size_t segment_register_size;

// The name of the index register of the address. When empty, index register
// The name and size of the base register of the address. When empty, base
// register
std::vector<std::string> base_register_intefered_register;
// The name of the index register of the address. When empty, index register
std::vector<int> base_register_intefered_register_sizes;
// The name and size of the index register of the address. When empty, index
// register
std::vector<std::string> index_register_intefered_register;
// The name of the index register of the address. When empty, index register
std::vector<int> index_register_intefered_register_sizes;
// The name of the segment register of the address. When empty, segment
// register
std::vector<std::string> segment_register_intefered_register;
std::vector<int> segment_register_intefered_register_sizes;
};

std::ostream& operator<<(std::ostream& os, const AddressTuple& address_tuple);
Expand All @@ -170,19 +188,25 @@ class InstructionOperand {
InstructionOperand& operator=(InstructionOperand&&) = default;

// The operands must be created through one of the factory functions.
static InstructionOperand VirtualRegister(std::string register_name,
size_t size, const std::vector<std::string>& interfered_registers);
static InstructionOperand VirtualRegister(
std::string register_name, size_t size,
const std::vector<std::string>& interfered_registers,
std::vector<int> interfered_registers_size);
static InstructionOperand Register(std::string register_name);
static InstructionOperand ImmediateValue(uint64_t immediate_value);
static InstructionOperand FpImmediateValue(double fp_immediate_value);
static InstructionOperand Address(AddressTuple address_tuple);
static InstructionOperand Address(std::string base_register,
int64_t displacement,
std::string index_register, int scaling,
std::string segment_register,
int base_register_size = 64,
int index_register_size = 64,
int segment_register_size = 64);
static InstructionOperand Address(
std::string base_register, int64_t displacement,
std::string index_register, int scaling, std::string segment_register,
int base_register_size = 64, int index_register_size = 64,
int segment_register_size = 64,
const std::vector<std::string>& base_register_intefered_register = {},
const std::vector<std::string>& index_register_intefered_register = {},
const std::vector<std::string>& segment_register_intefered_register = {},
const std::vector<int>& base_register_intefered_register_sizes = {},
const std::vector<int>& index_register_intefered_register_sizes = {},
const std::vector<int>& segment_register_intefered_register_sizes = {});
static InstructionOperand MemoryLocation(int alias_group_id);

bool operator==(const InstructionOperand&) const;
Expand All @@ -198,9 +222,16 @@ class InstructionOperand {

std::vector<std::string> getInterferedRegisters() const {
assert(type_ == OperandType::kVirtualRegister);
assert(interfered_registers_.size() == interfered_registers_size_.size());
return interfered_registers_;
}

std::vector<int> getInterferedRegistersSize() const {
assert(type_ == OperandType::kVirtualRegister);
assert(interfered_registers_.size() == interfered_registers_size_.size());
return interfered_registers_size_;
}

// Returns a human-readable representation of the operand.
//
// This method implements the __str__() and __repr__() methods in the Python
Expand Down Expand Up @@ -258,6 +289,7 @@ class InstructionOperand {
AddressTuple address_;
int alias_group_id_ = 0;
std::vector<std::string> interfered_registers_;
std::vector<int> interfered_registers_size_;
};

std::ostream& operator<<(std::ostream& os, const InstructionOperand& operand);
Expand Down
69 changes: 46 additions & 23 deletions gematria/basic_block/basic_block_protos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@
namespace gematria {

namespace {
std::vector<std::string> ToVector(
std::vector<std::string> ToVector(
const google::protobuf::RepeatedPtrField<std::string>& protos) {
return std::vector<std::string>(protos.begin(), protos.end());
}
}

std::vector<int> ToVector(const google::protobuf::RepeatedField<int>& protos) {
return std::vector<int>(protos.begin(), protos.end());
}
} // namespace

AddressTuple AddressTupleFromProto(
const CanonicalizedOperandProto::AddressTuple& proto) {
auto result = AddressTuple(
Expand All @@ -40,17 +44,26 @@ AddressTuple AddressTupleFromProto(
/* index_register = */ proto.index_register(),
/* scaling = */ proto.scaling(),
/* segment_register = */ proto.segment());
if (proto.base_register()[0] == '%'){
if (proto.base_register()[0] == '%') {
result.base_register_size = proto.base_register_size();
result.base_register_intefered_register = ToVector(proto.base_register_intefered_register());
result.base_register_intefered_register =
ToVector(proto.base_register_intefered_register());
result.base_register_intefered_register_sizes =
ToVector(proto.base_register_intefered_register_sizes());
}
if (proto.index_register()[0] == '%'){
if (proto.index_register()[0] == '%') {
result.index_register_size = proto.index_register_size();
result.index_register_intefered_register = ToVector(proto.index_register_intefered_register());
result.index_register_intefered_register =
ToVector(proto.index_register_intefered_register());
result.index_register_intefered_register_sizes =
ToVector(proto.index_register_intefered_register_sizes());
}
if (proto.segment()[0] == '%'){
if (proto.segment()[0] == '%') {
result.segment_register_size = proto.segment_size();
result.segment_register_intefered_register = ToVector(proto.segment_intefered_register());
result.segment_register_intefered_register =
ToVector(proto.segment_intefered_register());
result.segment_register_intefered_register_sizes =
ToVector(proto.segment_intefered_register_sizes());
}
return result;
}
Expand All @@ -63,21 +76,29 @@ CanonicalizedOperandProto::AddressTuple ProtoFromAddressTuple(
proto.set_index_register(address_tuple.index_register);
proto.set_scaling(address_tuple.scaling);
proto.set_segment(address_tuple.segment_register);
if (!address_tuple.base_register.empty() && address_tuple.base_register[0] == '%') {
if (!address_tuple.base_register.empty() &&
address_tuple.base_register[0] == '%') {
proto.set_base_register_size(address_tuple.base_register_size);
for (auto interfered_register : address_tuple.base_register_intefered_register){
proto.add_base_register_intefered_register(std::move(interfered_register));
for (auto interfered_register :
address_tuple.base_register_intefered_register) {
proto.add_base_register_intefered_register(
std::move(interfered_register));
}
}
if (!address_tuple.index_register.empty() && address_tuple.index_register[0] == '%') {
if (!address_tuple.index_register.empty() &&
address_tuple.index_register[0] == '%') {
proto.set_index_register_size(address_tuple.index_register_size);
for (auto interfered_register : address_tuple.index_register_intefered_register){
proto.add_index_register_intefered_register(std::move(interfered_register));
for (auto interfered_register :
address_tuple.index_register_intefered_register) {
proto.add_index_register_intefered_register(
std::move(interfered_register));
}
}
if (!address_tuple.segment_register.empty() && address_tuple.segment_register[0] == '%') {
if (!address_tuple.segment_register.empty() &&
address_tuple.segment_register[0] == '%') {
proto.set_segment_size(address_tuple.segment_register_size);
for (auto interfered_register : address_tuple.segment_register_intefered_register){
for (auto interfered_register :
address_tuple.segment_register_intefered_register) {
proto.add_segment_intefered_register(std::move(interfered_register));
}
}
Expand All @@ -101,13 +122,15 @@ InstructionOperand InstructionOperandFromProto(
case CanonicalizedOperandProto::kMemory:
return InstructionOperand::MemoryLocation(
proto.memory().alias_group_id());
case CanonicalizedOperandProto::kVirtualRegister:
{
std::vector<std::string> interfered_registers = ToVector(proto.intefered_register());
return InstructionOperand::VirtualRegister(
proto.virtual_register().name(), proto.virtual_register().size(), interfered_registers);
}

case CanonicalizedOperandProto::kVirtualRegister: {
std::vector<std::string> interfered_registers =
ToVector(proto.intefered_register());
std::vector<int> interfered_register_sizes =
ToVector(proto.intefered_register_sizes());
return InstructionOperand::VirtualRegister(
proto.virtual_register().name(), proto.virtual_register().size(),
interfered_registers, interfered_register_sizes);
}
}
}

Expand Down
Loading

0 comments on commit b9b0167

Please sign in to comment.