Skip to content

Commit

Permalink
large refactoring;
Browse files Browse the repository at this point in the history
  • Loading branch information
NateSeymour committed Jan 14, 2025
1 parent b56dc1f commit 348c94f
Show file tree
Hide file tree
Showing 38 changed files with 209 additions and 311 deletions.
20 changes: 9 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ add_library(city
src/Assembly.cpp
src/Assembly.h
src/ByteBuffer.h
src/Container.cpp
src/Container.h
src/container/Container.cpp
src/container/Container.h
src/JIT.cpp
src/JIT.h
src/Object.cpp
Expand All @@ -35,13 +35,11 @@ add_library(city
src/backend/aarch64/instruction/AArch64Instruction.h
src/backend/amd64/Amd64.cpp
src/backend/amd64/Amd64.h
src/backend/amd64/Amd64Builder.cpp
src/backend/amd64/Amd64Builder.h
src/backend/amd64/Amd64ModRM.h
src/backend/amd64/container/Amd64ModRM.h
src/backend/amd64/Amd64Module.cpp
src/backend/amd64/Amd64Module.h
src/backend/amd64/Amd64Register.cpp
src/backend/amd64/Amd64Register.h
src/backend/amd64/container/Amd64Register.cpp
src/backend/amd64/container/Amd64Register.h
src/backend/amd64/Amd64Translator.cpp
src/backend/amd64/Amd64Translator.h
src/backend/amd64/instruction/Amd64Instruction.cpp
Expand All @@ -63,12 +61,8 @@ add_library(city
src/ir/instruction/InstructionFunctor.h
src/ir/instruction/arithmetic/AddInst.cpp
src/ir/instruction/arithmetic/AddInst.h
src/ir/instruction/control/BranchInst.cpp
src/ir/instruction/control/BranchInst.h
src/ir/instruction/control/RetInst.cpp
src/ir/instruction/control/RetInst.h
src/ir/instruction/memory/StoreInst.cpp
src/ir/instruction/memory/StoreInst.h
src/runtime/MacOS.cpp
src/runtime/MacOS.h
src/runtime/NativeMemoryHandle.cpp
Expand All @@ -85,6 +79,10 @@ add_library(city
src/ir/instruction/IRBinaryInstruction.cpp
src/ir/instruction/IRBinaryInstruction.h
src/backend/amd64/instruction/arithmetic/Amd64Add.h
src/backend/amd64/container/Amd64ContainerFunctor.cpp
src/backend/amd64/container/Amd64ContainerFunctor.h
src/container/CompileTimeConstant.cpp
src/container/CompileTimeConstant.h
)
target_include_directories(city PUBLIC src)

Expand Down
6 changes: 0 additions & 6 deletions src/Container.cpp

This file was deleted.

4 changes: 2 additions & 2 deletions src/backend/amd64/Amd64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ Object Amd64::BuildModule(IRModule &ir_module)
// Function Prolog
auto entry = Amd64Push::O64(Amd64RegisterCode::RBP);
entry.SetLabel(name);
amd64_module.InsertInstruction(std::move(entry));
amd64_module.Insert(std::move(entry));

amd64_module.InsertInstruction(Amd64Mov::MR64(Amd64RegisterCode::RBP, Amd64RegisterCode::RSP));
amd64_module.Insert(Amd64Mov::MR64(Amd64RegisterCode::RBP, Amd64RegisterCode::RSP));

// Function Body
for (auto &block : function->blocks_)
Expand Down
2 changes: 1 addition & 1 deletion src/backend/amd64/Amd64.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef X86_64_H
#define X86_64_H

#include "Amd64Register.h"
#include "backend/Backend.h"
#include "container/Amd64Register.h"

namespace city
{
Expand Down
86 changes: 0 additions & 86 deletions src/backend/amd64/Amd64Builder.cpp

This file was deleted.

44 changes: 0 additions & 44 deletions src/backend/amd64/Amd64Builder.h

This file was deleted.

8 changes: 1 addition & 7 deletions src/backend/amd64/Amd64Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@
//

#include "Amd64Module.h"
#include "Amd64Builder.h"

using namespace city;

void Amd64Module::InsertInstruction(Amd64Instruction &&inst)
void Amd64Module::Insert(Amd64Instruction &&inst)
{
this->instructions_.push_back(inst);
}

Amd64Builder Amd64Module::CreateBuilder()
{
return Amd64Builder(*this);
}
10 changes: 3 additions & 7 deletions src/backend/amd64/Amd64Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@
#define AMD64MODULE_H

#include <vector>
#include "Amd64.h"
#include "instruction/Amd64Instruction.h"

namespace city
{
class Amd64Builder;
class Amd64Translator;

class Amd64Module
{
friend class Amd64;
friend class Amd64Builder;
friend class Amd64Translator;

std::vector<Amd64Instruction> instructions_;

protected:
void InsertInstruction(Amd64Instruction &&inst);

public:
[[nodiscard]] Amd64Builder CreateBuilder();
void Insert(Amd64Instruction &&inst);
};
} // namespace city

Expand Down
54 changes: 42 additions & 12 deletions src/backend/amd64/Amd64Translator.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,61 @@
#include "Amd64Translator.h"

#include "instruction/arithmetic/Amd64Add.h"
#include "instruction/control/Amd64Ret.h"
#include "instruction/memory/Amd64Pop.h"

using namespace city;

Amd64Register *Amd64Translator::GetRegisterByCode(Amd64RegisterCode code)
{
auto const i = static_cast<std::uint8_t>(code);
return &this->registers_[i];
}

Amd64Register *Amd64Translator::LoadValue(Value *value, Amd64Register *reg, ConflictStrategy strategy, LoadType load_type) const {}

Amd64Register *Amd64Translator::MoveValue(Value *value, Amd64Register *reg, ConflictStrategy strategy) {}
Amd64Register *Amd64Translator::InstantiateValue(Value *value, Amd64Register *reg, ConflictStrategy strategy) {}

Amd64Register *Amd64Translator::FindUnusedRegister() noexcept
{
for (auto &reg : this->registers_)
{
if (!reg.HasValue() || reg.GetValue()->IsUsed())
{
return &reg;
}
}

return nullptr;
}

IRTranslationResult Amd64Translator::Translate(AddInst *instruction)
{
auto lhs = this->builder.MoveValueToUnusedRegister(instruction->GetLHS());
auto rhs = this->builder.MoveValueToUnusedRegister(instruction->GetRHS());
auto dsttmp = this->LoadValue(instruction->GetLHS());
auto srctmp = this->LoadValue(instruction->GetRHS());

this->builder.InsertAddInst(lhs->GetCode(), rhs->GetCode());
this->module.Insert(Amd64Add::MR64(dsttmp->GetCode(), srctmp->GetCode()));
(void)this->InstantiateValue(instruction->GetReturnValue(), dsttmp, ConflictStrategy::Discard);

instruction->GetLHS()->DecrementReadCount();
instruction->GetRHS()->DecrementReadCount();

return {};
}

IRTranslationResult Amd64Translator::Translate(BranchInst *instruction) {}

IRTranslationResult Amd64Translator::Translate(RetInst *instruction)
{
if (instruction->HasReturnValue())
auto return_value = instruction->GetReturnValue();

if (!return_value->GetType().IsVoid())
{
auto return_value = instruction->GetReturnValue();
this->builder.MoveValueToRegister(return_value, Amd64RegisterCode::RAX, Amd64RegisterConflictStrategy::Discard);
auto rax = this->GetRegisterByCode(Amd64RegisterCode::RAX);
(void)this->MoveValue(return_value, rax, ConflictStrategy::Discard);
}

this->builder.InsertPopInst(Amd64RegisterCode::RBP);
this->builder.InsertReturnInst();
this->module.Insert(Amd64Pop::O64(Amd64RegisterCode::RBP));
this->module.Insert(Amd64Ret::ZONear());

return {};
}

IRTranslationResult Amd64Translator::Translate(StoreInst *instruction) {}
Loading

0 comments on commit 348c94f

Please sign in to comment.