Skip to content

Commit

Permalink
major refactoring;
Browse files Browse the repository at this point in the history
  • Loading branch information
NateSeymour committed Dec 29, 2024
1 parent f4d37b5 commit 304a3c9
Show file tree
Hide file tree
Showing 27 changed files with 215 additions and 195 deletions.
18 changes: 9 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ add_library(city
src/ir/Builder.h
src/ir/instruction/IRInstruction.cpp
src/ir/instruction/IRInstruction.h
src/backend/NativeModule.cpp
src/backend/NativeModule.h
src/Assembly.cpp
src/Assembly.h
src/ir/value/ConstantValue.cpp
Expand Down Expand Up @@ -62,28 +60,30 @@ add_library(city
src/ir/instruction/memory/StoreInst.cpp
src/backend/amd64/Amd64.cpp
src/backend/amd64/Amd64.h
src/backend/IRTranslator.h
src/backend/amd64/Amd64Translator.cpp
src/backend/amd64/Amd64Translator.h
src/backend/amd64/instruction/Amd64Instruction.cpp
src/backend/amd64/instruction/Amd64Instruction.h
src/backend/amd64/instruction/arithmetic/x86AddRM32Inst.h
src/backend/amd64/instruction/arithmetic/Amd64AddRM32Inst.h
src/ByteBuffer.h
src/backend/amd64/instruction/arithmetic/x86AddMI32Inst.h
src/backend/amd64/instruction/arithmetic/Amd64AddMI32Inst.h
src/ir/instruction/InstructionFunctor.h
src/backend/NativeInstruction.cpp
src/backend/NativeInstruction.h
src/backend/amd64/Amd64Register.cpp
src/backend/amd64/Amd64Register.h
src/backend/amd64/Amd64ModRM.h
src/backend/amd64/instruction/memory/Amd64PushO64.h
src/backend/amd64/instruction/memory/Amd64MovMR64.h
src/backend/amd64/instruction/memory/Amd64PopO64.h
src/backend/amd64/instruction/control/Amd64RetZONear.h
src/backend/amd64/instruction/memory/Amd64Push.h
src/backend/amd64/instruction/memory/Amd64Mov.h
src/backend/amd64/instruction/memory/Amd64Pop.h
src/backend/amd64/instruction/control/Amd64Ret.h
src/runtime/Windows.cpp
src/runtime/Windows.h
src/Object.cpp
src/Object.h
src/backend/amd64/Amd64Module.cpp
src/backend/amd64/Amd64Module.h
src/backend/IRTranslator.h
)
target_include_directories(city PUBLIC src)

Expand Down
39 changes: 15 additions & 24 deletions src/JIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,20 @@

using namespace city;

void JIT::TranslateIRModules()
void JIT::CompileIRModules()
{
for (auto &module : this->ir_modules_)
for (auto &[name, module] : this->ir_modules_)
{
auto native_module = this->backend_->BuildModule(module);
this->native_modules_.push_back(std::move(native_module));
this->objects_.insert({name, this->backend_->BuildModule(module)});
}

this->ir_modules_.clear();
}

void JIT::CompileNativeModules()
{
for (auto &native_module : this->native_modules_)
{
auto object = Object::FromNativeModule(native_module);
this->objects_.push_back(std::move(object));
}

this->native_modules_.clear();
}

Assembly JIT::LinkObjects() const
{
std::size_t allocation_size = 0;
for (auto const &object : this->objects_)
for (auto const &[name, object] : this->objects_)
{
allocation_size += object.GetBinarySize();
}
Expand All @@ -37,7 +25,7 @@ Assembly JIT::LinkObjects() const
Assembly assembly{std::move(native_memory)};

std::size_t object_insertion_offset = 0;
for (auto const &object : this->objects_)
for (auto const &[name, object] : this->objects_)
{
auto object_size = object.GetBinarySize();

Expand All @@ -47,10 +35,10 @@ Assembly JIT::LinkObjects() const

object_insertion_offset += object_size;

for (auto &[name, offset] : object.symtab_)
for (auto &[name, offset] : object.symbol_table_)
{
assembly.symbol_table_[name] = {
.raw = assembly_addr + offset,
.location = assembly_addr + reinterpret_cast<std::size_t>(offset.location),
.flags = SymbolFlags::Exectuable,
};
}
Expand All @@ -61,17 +49,20 @@ Assembly JIT::LinkObjects() const
return assembly;
}

void JIT::AddIRModule(IRModule module)
void JIT::InsertIRModule(IRModule module)
{
this->ir_modules_.push_back(std::move(module));
this->ir_modules_.insert({module.GetName(), std::move(module)});
}

void JIT::RemoveModule(std::string const &name) {}
void JIT::RemoveModule(std::string const &name)
{
this->ir_modules_.erase(name);
this->objects_.erase(name);
}

Assembly JIT::CompileAndLink()
{
this->TranslateIRModules();
this->CompileNativeModules();
this->CompileIRModules();

return this->LinkObjects();
}
Expand Down
21 changes: 10 additions & 11 deletions src/JIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#define CITY_COMPILER_H

#include <memory>
#include <unordered_map>
#include <vector>
#include "Assembly.h"
#include "Object.h"
#include "backend/Backend.h"
#include "backend/NativeModule.h"
#include "ir/IRModule.h"

namespace city
Expand All @@ -15,19 +15,13 @@ namespace city
{
std::unique_ptr<Backend> backend_;

std::vector<IRModule> ir_modules_;
std::vector<NativeModule> native_modules_;
std::vector<Object> objects_;
std::unordered_map<std::string, IRModule> ir_modules_;
std::unordered_map<std::string, Object> objects_;

/**
* Translates all IR modules to native modules and deletes the IR modules.
*/
void TranslateIRModules();

/**
* Translates all native modules into objects and deletes the native modules.
*/
void CompileNativeModules();
void CompileIRModules();

/**
* Links all objects together (non-destructively) and returns the final linked Assembly.
Expand All @@ -40,8 +34,13 @@ namespace city
* Adds a module to the compiler, transferring ownership to the compiler.
* @param module Module to transfer to the compiler
*/
void AddIRModule(IRModule module);
void InsertIRModule(IRModule module);

/**
* Removes a module (or corresponding object if already compiled) from the compiler.
* WARNING: May cause linker errors if other modules depend on the removed module.
* @param name Name of module to remove
*/
void RemoveModule(std::string const &name);

/**
Expand Down
32 changes: 1 addition & 31 deletions src/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,4 @@ std::size_t Object::GetBinarySize() const noexcept
return this->native_memory_.GetAllocationSize();
}

Object Object::FromNativeModule(NativeModule const &module)
{
// Allocate memory
std::size_t allocation_size = 0;
for (auto &inst : module.instructions_)
{
allocation_size += inst->GetBinarySize();
}
auto native_memory = NativeMemoryHandle::Allocate(allocation_size);

// Create object
Object object{std::move(native_memory)};

// Write to buffer
std::size_t offset = 0;
for (auto &inst : module.instructions_)
{
auto label = inst->GetLabel();
if (label != nullptr)
{
object.symtab_[label] = offset;
}

auto addr = object.native_memory_.GetAddressAtOffset(offset);
offset += inst->WriteToBuffer(addr);
}

return object;
}

Object::Object(NativeMemoryHandle native_memory) : native_memory_(std::move(native_memory)) {}
Object::Object(NativeMemoryHandle &&native_memory, SymbolTable &&symbol_table) : native_memory_(std::move(native_memory)), symbol_table_(std::move(symbol_table)) {}
14 changes: 3 additions & 11 deletions src/Object.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#ifndef OBJECT_H
#define OBJECT_H

#include <string>
#include <unordered_map>
#include "backend/NativeModule.h"
#include "Symbol.h"
#include "runtime/NativeMemoryHandle.h"

namespace city
Expand All @@ -16,18 +14,12 @@ namespace city

protected:
NativeMemoryHandle native_memory_;

/**
* Holds instruction labels with their offsets into the binary.
*/
std::unordered_map<std::string, std::size_t> symtab_;
SymbolTable symbol_table_;

public:
[[nodiscard]] std::size_t GetBinarySize() const noexcept;

static Object FromNativeModule(NativeModule const &module);

Object(NativeMemoryHandle native_memory);
Object(NativeMemoryHandle &&native_memory, SymbolTable &&symbol_table);
};
} // namespace city

Expand Down
8 changes: 6 additions & 2 deletions src/Symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define CITY_SYMBOL_H

#include <cstddef>
#include <string>
#include <unordered_map>

namespace city
{
Expand All @@ -12,15 +14,17 @@ namespace city

struct Symbol
{
std::byte *raw;
std::byte *location;
SymbolFlags flags;

template<typename T>
T *ToPointer() const noexcept
{
return reinterpret_cast<T *>(this->raw);
return reinterpret_cast<T *>(this->location);
}
};

using SymbolTable = std::unordered_map<std::string, Symbol>;
} // namespace city

#endif // CITY_SYMBOL_H
4 changes: 2 additions & 2 deletions src/backend/Backend.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#ifndef CITY_BACKEND_H
#define CITY_BACKEND_H

#include "NativeModule.h"
#include "Object.h"
#include "ir/IRModule.h"

namespace city
{
class Backend
{
public:
[[nodiscard]] virtual NativeModule BuildModule(IRModule &module) = 0;
[[nodiscard]] virtual Object BuildModule(IRModule &module) = 0;

/**
* Instantiates and returns a handle to the host-native compiler backend.
Expand Down
10 changes: 3 additions & 7 deletions src/backend/IRTranslator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef IRTRANSLATIONINTERFACE_H
#define IRTRANSLATIONINTERFACE_H
#ifndef IRTRANSLATOR_H
#define IRTRANSLATOR_H

#include "NativeModule.h"
#include "ir/instruction/InstructionFunctor.h"

namespace city
Expand All @@ -12,10 +11,7 @@ namespace city

struct IRTranslator : InstructionFunctor<IRTranslationResult>
{
NativeModule &module;

explicit IRTranslator(NativeModule &native_module) : module(native_module) {}
};
} // namespace city

#endif // IRTRANSLATIONINTERFACE_H
#endif // IRTRANSLATOR_H
3 changes: 0 additions & 3 deletions src/backend/NativeModule.cpp

This file was deleted.

31 changes: 0 additions & 31 deletions src/backend/NativeModule.h

This file was deleted.

Loading

0 comments on commit 304a3c9

Please sign in to comment.