diff --git a/CMakeLists.txt b/CMakeLists.txt index d41d7cb..c056e15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/src/Container.cpp b/src/Container.cpp deleted file mode 100644 index 4a4f681..0000000 --- a/src/Container.cpp +++ /dev/null @@ -1,6 +0,0 @@ - -#include "Container.h" - -namespace city -{ -} // city \ No newline at end of file diff --git a/src/backend/amd64/Amd64.cpp b/src/backend/amd64/Amd64.cpp index 795af1e..ad11efc 100644 --- a/src/backend/amd64/Amd64.cpp +++ b/src/backend/amd64/Amd64.cpp @@ -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_) diff --git a/src/backend/amd64/Amd64.h b/src/backend/amd64/Amd64.h index feb66ec..80e2809 100644 --- a/src/backend/amd64/Amd64.h +++ b/src/backend/amd64/Amd64.h @@ -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 { diff --git a/src/backend/amd64/Amd64Builder.cpp b/src/backend/amd64/Amd64Builder.cpp deleted file mode 100644 index 40bb3ba..0000000 --- a/src/backend/amd64/Amd64Builder.cpp +++ /dev/null @@ -1,86 +0,0 @@ -#include "Amd64Builder.h" - -#include "instruction/arithmetic/Amd64Add.h" -#include "instruction/control/Amd64Ret.h" -#include "instruction/memory/Amd64Mov.h" -#include "instruction/memory/Amd64Pop.h" - -using namespace city; - -Amd64Register &Amd64Builder::GetRegisterByCode(Amd64RegisterCode code) -{ - auto i = static_cast(code); - return this->registers_[i]; -} - -Amd64Register *Amd64Builder::FindUnusedRegister() const noexcept -{ - for (auto ® : this->registers_) - { - } - - return nullptr; -} - -void Amd64Builder::InsertAddInst(Amd64RegisterCode dst, Amd64RegisterCode src) -{ - this->module_.InsertInstruction(Amd64Add::MR64(dst, src)); -} - -void Amd64Builder::InsertReturnInst(Amd64ReturnType return_type) const -{ - switch (return_type) - { - case Amd64ReturnType::Near: - { - this->module_.InsertInstruction(Amd64Ret::ZONear()); - break; - } - - case Amd64ReturnType::Far: - { - this->module_.InsertInstruction(Amd64Ret::ZOFar()); - break; - } - } -} - -void Amd64Builder::InsertPopInst(Amd64RegisterCode reg) -{ - this->module_.InsertInstruction(Amd64Pop::O64(reg)); -} - -Amd64Register *Amd64Builder::MoveValueToUnusedRegister(Value *value) -{ - for (auto ® : this->registers_) - { - if (!reg.value_ || !reg.value_->IsUsed()) - { - return this->MoveValueToRegister(value, reg.code_, Amd64RegisterConflictStrategy::Discard); - } - } - - // TODO: start popping values out of the registers onto the stack. - - return nullptr; -} - -Amd64Register *Amd64Builder::MoveValueToRegister(Value *value, Amd64RegisterCode reg, Amd64RegisterConflictStrategy conflict_strategy) -{ - if (!value) - { - return nullptr; - } - - // WARNING: only discards current value for now. - auto &physical_reg = this->GetRegisterByCode(reg); - - if (value->IsCompileTimeConstant()) - { - this->module_.InsertInstruction(Amd64Mov::OIX(reg, value->GetDataBuffer())); - } - - return &physical_reg; -} - -Amd64Builder::Amd64Builder(Amd64Module &module) : module_(module) {} diff --git a/src/backend/amd64/Amd64Builder.h b/src/backend/amd64/Amd64Builder.h deleted file mode 100644 index 391924d..0000000 --- a/src/backend/amd64/Amd64Builder.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef CITY_AMD64BUILDER_H -#define CITY_AMD64BUILDER_H - -#include "Amd64.h" -#include "Amd64Module.h" - -namespace city -{ - enum class Amd64RegisterConflictStrategy - { - Push, - MoveToUnused, - Discard, - }; - - enum class Amd64ReturnType - { - Near, - Far, - }; - - class Amd64Builder - { - Amd64Module &module_; - - std::array registers_ = amd64_register_definitions; - - public: - [[nodiscard]] Amd64Register &GetRegisterByCode(Amd64RegisterCode code); - - [[nodiscard]] Amd64Register *FindUnusedRegister() const noexcept; - - void InsertAddInst(Amd64RegisterCode dst, Amd64RegisterCode src); - void InsertReturnInst(Amd64ReturnType return_type = Amd64ReturnType::Near) const; - void InsertPopInst(Amd64RegisterCode reg); - - [[nodiscard]] Amd64Register *MoveValueToUnusedRegister(Value *value); - Amd64Register *MoveValueToRegister(Value *value, Amd64RegisterCode reg, Amd64RegisterConflictStrategy conflict_strategy); - - explicit Amd64Builder(Amd64Module &module); - }; -} // namespace city - -#endif // CITY_AMD64BUILDER_H diff --git a/src/backend/amd64/Amd64Module.cpp b/src/backend/amd64/Amd64Module.cpp index 0bc08c3..bfd826b 100644 --- a/src/backend/amd64/Amd64Module.cpp +++ b/src/backend/amd64/Amd64Module.cpp @@ -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); -} diff --git a/src/backend/amd64/Amd64Module.h b/src/backend/amd64/Amd64Module.h index cba1977..0875ee8 100644 --- a/src/backend/amd64/Amd64Module.h +++ b/src/backend/amd64/Amd64Module.h @@ -2,25 +2,21 @@ #define AMD64MODULE_H #include -#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 instructions_; protected: - void InsertInstruction(Amd64Instruction &&inst); - - public: - [[nodiscard]] Amd64Builder CreateBuilder(); + void Insert(Amd64Instruction &&inst); }; } // namespace city diff --git a/src/backend/amd64/Amd64Translator.cpp b/src/backend/amd64/Amd64Translator.cpp index 47498c9..d2c706a 100644 --- a/src/backend/amd64/Amd64Translator.cpp +++ b/src/backend/amd64/Amd64Translator.cpp @@ -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(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 ® : this->registers_) + { + if (!reg.HasValue() || reg.GetValue()->IsUsed()) + { + return ® + } + } + + 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) {} diff --git a/src/backend/amd64/Amd64Translator.h b/src/backend/amd64/Amd64Translator.h index 10d58dd..32ee271 100644 --- a/src/backend/amd64/Amd64Translator.h +++ b/src/backend/amd64/Amd64Translator.h @@ -1,24 +1,62 @@ #ifndef X86_64TRANSLATIONINTERFACE_H #define X86_64TRANSLATIONINTERFACE_H -#include "Amd64Builder.h" #include "Amd64Module.h" #include "backend/IRTranslator.h" #include "ir/instruction/InstructionFunctor.h" namespace city { + enum class ConflictStrategy + { + Push, + MoveToUnused, + Discard, + }; + + enum class LoadType + { + Value, + Pointer, + Optimal, + }; + struct Amd64Translator : IRTranslator { Amd64Module &module; - Amd64Builder builder; + + std::array registers_ = amd64_register_definitions; + + [[nodiscard]] Amd64Register *GetRegisterByCode(Amd64RegisterCode code); + + /** + * Loads a value into a register without transferring its ownership. + * @param value + * @param reg + * @param strategy + * @param load_type + * @return + */ + [[nodiscard]] Amd64Register *LoadValue( + Value *value, Amd64Register *reg = nullptr, ConflictStrategy strategy = ConflictStrategy::Push, LoadType load_type = LoadType::Value) const; + + /** + * Moves value and transfers its ownership. + * @param value + * @param reg + * @param strategy + * @return + */ + [[nodiscard]] Amd64Register *MoveValue(Value *value, Amd64Register *reg, ConflictStrategy strategy); + + [[nodiscard]] Amd64Register *InstantiateValue(Value *value, Amd64Register *reg, ConflictStrategy strategy); + + [[nodiscard]] Amd64Register *FindUnusedRegister() noexcept; IRTranslationResult Translate(AddInst *instruction) override; - IRTranslationResult Translate(BranchInst *instruction) override; IRTranslationResult Translate(RetInst *instruction) override; - IRTranslationResult Translate(StoreInst *instruction) override; - explicit Amd64Translator(Amd64Module &module) : module(module), builder(module) {} + explicit Amd64Translator(Amd64Module &module) : module(module) {} }; } // namespace city diff --git a/src/backend/amd64/container/Amd64ContainerFunctor.cpp b/src/backend/amd64/container/Amd64ContainerFunctor.cpp new file mode 100644 index 0000000..6188927 --- /dev/null +++ b/src/backend/amd64/container/Amd64ContainerFunctor.cpp @@ -0,0 +1,8 @@ +// +// Created by Nathan on 1/14/2025. +// + +#include "Amd64ContainerFunctor.h" + +namespace city { +} // city \ No newline at end of file diff --git a/src/backend/amd64/container/Amd64ContainerFunctor.h b/src/backend/amd64/container/Amd64ContainerFunctor.h new file mode 100644 index 0000000..8e2dfca --- /dev/null +++ b/src/backend/amd64/container/Amd64ContainerFunctor.h @@ -0,0 +1,14 @@ +#ifndef AMD64CONTAINERFUNCTOR_H +#define AMD64CONTAINERFUNCTOR_H + +#include "Amd64Register.h" + +namespace city +{ + struct Amd64ContainerFunctor + { + void Visit(Amd64Register *reg); + }; +} // namespace city + +#endif // AMD64CONTAINERFUNCTOR_H diff --git a/src/backend/amd64/Amd64ModRM.h b/src/backend/amd64/container/Amd64ModRM.h similarity index 100% rename from src/backend/amd64/Amd64ModRM.h rename to src/backend/amd64/container/Amd64ModRM.h diff --git a/src/backend/amd64/Amd64Register.cpp b/src/backend/amd64/container/Amd64Register.cpp similarity index 100% rename from src/backend/amd64/Amd64Register.cpp rename to src/backend/amd64/container/Amd64Register.cpp diff --git a/src/backend/amd64/Amd64Register.h b/src/backend/amd64/container/Amd64Register.h similarity index 93% rename from src/backend/amd64/Amd64Register.h rename to src/backend/amd64/container/Amd64Register.h index efc3578..c6434bc 100644 --- a/src/backend/amd64/Amd64Register.h +++ b/src/backend/amd64/container/Amd64Register.h @@ -1,7 +1,7 @@ #ifndef CITY_X86REGISTER_H #define CITY_X86REGISTER_H -#include "../../Container.h" +#include "../../../Container.h" #include "Amd64ModRM.h" namespace city diff --git a/src/backend/amd64/instruction/Amd64Instruction.h b/src/backend/amd64/instruction/Amd64Instruction.h index 3fe2fe2..9fb62e6 100644 --- a/src/backend/amd64/instruction/Amd64Instruction.h +++ b/src/backend/amd64/instruction/Amd64Instruction.h @@ -3,10 +3,10 @@ #include #include +#include "../container/Amd64ModRM.h" #include "ByteBuffer.h" #include "backend/NativeInstruction.h" #include "backend/amd64/Amd64.h" -#include "backend/amd64/Amd64ModRM.h" namespace city { diff --git a/src/container/CompileTimeConstant.cpp b/src/container/CompileTimeConstant.cpp new file mode 100644 index 0000000..e88aeb3 --- /dev/null +++ b/src/container/CompileTimeConstant.cpp @@ -0,0 +1,8 @@ +// +// Created by Nathan on 1/14/2025. +// + +#include "CompileTimeConstant.h" + +namespace city { +} // city \ No newline at end of file diff --git a/src/container/CompileTimeConstant.h b/src/container/CompileTimeConstant.h new file mode 100644 index 0000000..ecd849c --- /dev/null +++ b/src/container/CompileTimeConstant.h @@ -0,0 +1,13 @@ +#ifndef COMPILETIMECONSTANT_H +#define COMPILETIMECONSTANT_H + +#include "Container.h" + +namespace city +{ + class CompileTimeConstant : public Container + { + }; +} // namespace city + +#endif // COMPILETIMECONSTANT_H diff --git a/src/container/Container.cpp b/src/container/Container.cpp new file mode 100644 index 0000000..24976c7 --- /dev/null +++ b/src/container/Container.cpp @@ -0,0 +1,19 @@ + +#include "Container.h" + +using namespace city; + +void Container::SetValue(Value *value) +{ + this->value_ = value; +} + +Value *Container::GetValue() const noexcept +{ + return this->value_; +} + +bool Container::HasValue() const noexcept +{ + return this->value_; +} diff --git a/src/Container.h b/src/container/Container.h similarity index 73% rename from src/Container.h rename to src/container/Container.h index 42e77ee..f8caa7c 100644 --- a/src/Container.h +++ b/src/container/Container.h @@ -13,16 +13,17 @@ namespace city }; class Value; - class Amd64Builder; class Container { - friend class Amd64Builder; - protected: Value *value_ = nullptr; public: + void SetValue(Value *value); + [[nodiscard]] Value *GetValue() const noexcept; + [[nodiscard]] bool HasValue() const noexcept; + virtual ~Container() = default; }; } // namespace city diff --git a/src/ir/IRBuilder.cpp b/src/ir/IRBuilder.cpp index 4c4703f..8e3003e 100644 --- a/src/ir/IRBuilder.cpp +++ b/src/ir/IRBuilder.cpp @@ -63,23 +63,17 @@ AddInst *IRBuilder::InsertAddInst(Value *lhs, Value *rhs) } auto return_value = this->ReserveLocalValue(lhs_type, StorageClass::Temporary); - auto addtmp = this->ReserveInstruction(lhs, rhs); - addtmp->SetReturnValue(return_value); + auto addtmp = this->ReserveInstruction(return_value, lhs, rhs); return addtmp; } -StoreInst *IRBuilder::InsertStoreInst(Value *dst, Value *src) +RetInst *IRBuilder::InsertRetInst(Value *return_value) { - return this->ReserveInstruction(dst, src); -} - -BranchInst *IRBuilder::InsertBranchInst(IRInstruction *target) -{ - return this->ReserveInstruction(target); -} + if (!return_value) + { + return_value = this->ReserveLocalValue(this->GetType()); + } -RetInst *IRBuilder::InsertRetInst(Value *ret) -{ - return this->ReserveInstruction(ret); + return this->ReserveInstruction(return_value); } diff --git a/src/ir/IRBuilder.h b/src/ir/IRBuilder.h index 7e41a62..20b5929 100644 --- a/src/ir/IRBuilder.h +++ b/src/ir/IRBuilder.h @@ -105,12 +105,8 @@ namespace city // Instructions - Arithmetic [[nodiscard]] AddInst *InsertAddInst(Value *lhs, Value *rhs); - // Instructions - Memory - StoreInst *InsertStoreInst(Value *dst, Value *src); - // Instructions - Control - BranchInst *InsertBranchInst(IRInstruction *target); - RetInst *InsertRetInst(Value *ret); + RetInst *InsertRetInst(Value *return_value = nullptr); // Constructors IRBuilder() = delete; diff --git a/src/ir/instruction/IRBinaryInstruction.cpp b/src/ir/instruction/IRBinaryInstruction.cpp index 9746509..714e9d9 100644 --- a/src/ir/instruction/IRBinaryInstruction.cpp +++ b/src/ir/instruction/IRBinaryInstruction.cpp @@ -12,4 +12,8 @@ Value *IRBinaryInstruction::GetRHS() const noexcept return this->rhs_; } -IRBinaryInstruction::IRBinaryInstruction(Value *lhs, Value *rhs) : lhs_(lhs), rhs_(rhs) {} +IRBinaryInstruction::IRBinaryInstruction(Value *return_value, Value *lhs, Value *rhs) : IRInstruction(return_value), lhs_(lhs), rhs_(rhs) +{ + lhs->IncrementReadCount(); + rhs->IncrementReadCount(); +} diff --git a/src/ir/instruction/IRBinaryInstruction.h b/src/ir/instruction/IRBinaryInstruction.h index 67656f2..2a82d7d 100644 --- a/src/ir/instruction/IRBinaryInstruction.h +++ b/src/ir/instruction/IRBinaryInstruction.h @@ -16,7 +16,7 @@ namespace city [[nodiscard]] Value *GetLHS() const noexcept; [[nodiscard]] Value *GetRHS() const noexcept; - IRBinaryInstruction(Value *lhs, Value *rhs); + IRBinaryInstruction(Value *return_value, Value *lhs, Value *rhs); }; } // namespace city diff --git a/src/ir/instruction/IRInstruction.cpp b/src/ir/instruction/IRInstruction.cpp index a182c76..a643e8e 100644 --- a/src/ir/instruction/IRInstruction.cpp +++ b/src/ir/instruction/IRInstruction.cpp @@ -2,17 +2,9 @@ using namespace city; -bool IRInstruction::HasReturnValue() const noexcept -{ - return this->return_value_ != nullptr; -} - -Value *IRInstruction::GetReturnValue() +Value *IRInstruction::GetReturnValue() const { return this->return_value_; } -void IRInstruction::SetReturnValue(Value *return_value) -{ - this->return_value_ = return_value; -} +IRInstruction::IRInstruction(Value *return_value) : return_value_(return_value) {} diff --git a/src/ir/instruction/IRInstruction.h b/src/ir/instruction/IRInstruction.h index 1cde932..2122580 100644 --- a/src/ir/instruction/IRInstruction.h +++ b/src/ir/instruction/IRInstruction.h @@ -14,15 +14,12 @@ namespace city Value *return_value_ = nullptr; - protected: - void SetReturnValue(Value *return_value); - public: virtual void Apply(IRTranslator *interface) = 0; - [[nodiscard]] virtual bool HasReturnValue() const noexcept; - [[nodiscard]] Value *GetReturnValue(); + [[nodiscard]] Value *GetReturnValue() const; + IRInstruction(Value *return_value); virtual ~IRInstruction() = default; }; } // namespace city diff --git a/src/ir/instruction/InstructionFunctor.h b/src/ir/instruction/InstructionFunctor.h index 5a2253f..b835a4b 100644 --- a/src/ir/instruction/InstructionFunctor.h +++ b/src/ir/instruction/InstructionFunctor.h @@ -2,9 +2,7 @@ #define INSTRUCTIONFUNCTOR_H #include "ir/instruction/arithmetic/AddInst.h" -#include "ir/instruction/control/BranchInst.h" #include "ir/instruction/control/RetInst.h" -#include "ir/instruction/memory/StoreInst.h" namespace city { @@ -12,9 +10,7 @@ namespace city struct InstructionFunctor { virtual ResultType Translate(AddInst *instruction) = 0; - virtual ResultType Translate(BranchInst *instruction) = 0; virtual ResultType Translate(RetInst *instruction) = 0; - virtual ResultType Translate(StoreInst *instruction) = 0; template ResultType Translate(T *) diff --git a/src/ir/instruction/arithmetic/AddInst.cpp b/src/ir/instruction/arithmetic/AddInst.cpp index 06299ef..19e126d 100644 --- a/src/ir/instruction/arithmetic/AddInst.cpp +++ b/src/ir/instruction/arithmetic/AddInst.cpp @@ -8,9 +8,4 @@ void AddInst::Apply(IRTranslator *interface) interface->Translate(this); } -bool AddInst::HasReturnValue() const noexcept -{ - return true; -} - -AddInst::AddInst(Value *lhs, Value *rhs) : IRBinaryInstruction(lhs, rhs) {} +AddInst::AddInst(Value *return_value, Value *lhs, Value *rhs) : IRBinaryInstruction(return_value, lhs, rhs) {} diff --git a/src/ir/instruction/arithmetic/AddInst.h b/src/ir/instruction/arithmetic/AddInst.h index e86555e..f0d5f76 100644 --- a/src/ir/instruction/arithmetic/AddInst.h +++ b/src/ir/instruction/arithmetic/AddInst.h @@ -10,9 +10,7 @@ namespace city public: void Apply(IRTranslator *interface) override; - [[nodiscard]] bool HasReturnValue() const noexcept override; - - AddInst(Value *lhs, Value *rhs); + AddInst(Value *return_value, Value *lhs, Value *rhs); }; } // namespace city diff --git a/src/ir/instruction/control/BranchInst.cpp b/src/ir/instruction/control/BranchInst.cpp deleted file mode 100644 index 0fa92c3..0000000 --- a/src/ir/instruction/control/BranchInst.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "BranchInst.h" -#include "backend/IRTranslator.h" - -using namespace city; - -void BranchInst::Apply(IRTranslator *interface) -{ - interface->Translate(this); -} - -BranchInst::BranchInst(IRInstruction *target) : target_(target) {} diff --git a/src/ir/instruction/control/BranchInst.h b/src/ir/instruction/control/BranchInst.h deleted file mode 100644 index 070d88f..0000000 --- a/src/ir/instruction/control/BranchInst.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef CITY_BRANCHINST_H -#define CITY_BRANCHINST_H - -#include "ir/instruction/IRInstruction.h" - -namespace city -{ - class IRTranslator; - - class BranchInst : public IRInstruction - { - IRInstruction *target_; - - public: - void Apply(IRTranslator *interface) override; - - BranchInst(IRInstruction *target); - }; -} // namespace city - -#endif // CITY_BRANCHINST_H diff --git a/src/ir/instruction/control/RetInst.cpp b/src/ir/instruction/control/RetInst.cpp index 2764f9d..eb0848e 100644 --- a/src/ir/instruction/control/RetInst.cpp +++ b/src/ir/instruction/control/RetInst.cpp @@ -8,7 +8,4 @@ void RetInst::Apply(IRTranslator *interface) interface->Translate(this); } -RetInst::RetInst(Value *value) -{ - this->SetReturnValue(value); -} +RetInst::RetInst(Value *return_value) : IRInstruction(return_value) {} diff --git a/src/ir/instruction/control/RetInst.h b/src/ir/instruction/control/RetInst.h index 1929911..2fcd428 100644 --- a/src/ir/instruction/control/RetInst.h +++ b/src/ir/instruction/control/RetInst.h @@ -12,7 +12,7 @@ namespace city public: void Apply(IRTranslator *interface) override; - RetInst(Value *value); + RetInst(Value *return_value); }; } // namespace city diff --git a/src/ir/instruction/memory/StoreInst.cpp b/src/ir/instruction/memory/StoreInst.cpp deleted file mode 100644 index 78bb566..0000000 --- a/src/ir/instruction/memory/StoreInst.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "StoreInst.h" -#include "backend/IRTranslator.h" - -using namespace city; - -void StoreInst::Apply(IRTranslator *interface) -{ - interface->Translate(this); -} - -StoreInst::StoreInst(Value *dst, Value *src) : dst_(dst), src_(src) {} diff --git a/src/ir/instruction/memory/StoreInst.h b/src/ir/instruction/memory/StoreInst.h deleted file mode 100644 index fa310a4..0000000 --- a/src/ir/instruction/memory/StoreInst.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef CITY_STOREINST_H -#define CITY_STOREINST_H - -#include "ir/instruction/IRInstruction.h" - -namespace city -{ - class IRTranslator; - - class StoreInst : public IRInstruction - { - Value *dst_; - Value *src_; - - public: - void Apply(IRTranslator *interface) override; - - StoreInst(Value *dst, Value *src); - }; -} // namespace city - -#endif // CITY_STOREINST_H diff --git a/src/type/Type.h b/src/type/Type.h index c01e8ea..c2f1853 100644 --- a/src/type/Type.h +++ b/src/type/Type.h @@ -27,6 +27,11 @@ namespace city { return this->size_ == rhs.size_ && this->native_type_ == rhs.native_type_; } + + [[nodiscard]] bool IsVoid() const noexcept + { + return this->native_type_ == NativeType::Void; + } }; } // namespace city diff --git a/src/value/Value.cpp b/src/value/Value.cpp index 33a57bb..038e9aa 100644 --- a/src/value/Value.cpp +++ b/src/value/Value.cpp @@ -8,14 +8,19 @@ void Value::IncrementReadCount() noexcept this->read_count_++; } -void Value::IncrementWriteCount() noexcept +void Value::DecrementReadCount() noexcept { - this->write_count_++; + this->read_count_--; } -bool Value::IsConstant() const noexcept +void Value::SetContainer(Container *container) { - return this->write_count_ == 0; + this->container_ = container; +} + +bool Value::IsInstantiated() const noexcept +{ + return this->container_; } bool Value::IsUsed() const noexcept diff --git a/src/value/Value.h b/src/value/Value.h index bff0a3a..adfc630 100644 --- a/src/value/Value.h +++ b/src/value/Value.h @@ -16,15 +16,16 @@ namespace city Container *container_ = nullptr; std::size_t read_count_ = 0; - std::size_t write_count_ = 0; public: void IncrementReadCount() noexcept; - void IncrementWriteCount() noexcept; + void DecrementReadCount() noexcept; + + void SetContainer(Container *container); [[nodiscard]] Type GetType() const noexcept; - [[nodiscard]] bool IsConstant() const noexcept; + [[nodiscard]] bool IsInstantiated() const noexcept; [[nodiscard]] bool IsUsed() const noexcept; [[nodiscard]] virtual bool IsCompileTimeConstant() const noexcept; [[nodiscard]] virtual std::vector const &GetDataBuffer() const;