Skip to content

Commit

Permalink
make assembly more ergonomic;
Browse files Browse the repository at this point in the history
  • Loading branch information
NateSeymour committed Dec 31, 2024
1 parent 9c78aa8 commit c699f0b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/Assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

using namespace city;

Symbol Assembly::operator[](std::string const &symbol) const
{
return this->symbol_table_.at(symbol);
}

Symbol Assembly::Lookup(std::string const &symbol)
{
return this->symbol_table_[symbol];
Expand Down
1 change: 1 addition & 0 deletions src/Assembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace city
std::unordered_map<std::string, Symbol> symbol_table_;

public:
[[nodiscard]] Symbol operator[](std::string const &symbol) const;
[[nodiscard]] Symbol Lookup(std::string const &symbol);

Assembly(NativeMemoryHandle native_memory);
Expand Down
20 changes: 9 additions & 11 deletions test/amd64.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@ TEST_F(Amd64TestRunner, ReturnVoidFunction)
city::IRModule module{"test"};
auto builder = module.CreateBuilder();

auto function = builder.CreateFunction("return_void");
builder.SetInsertPoint(function);

(void)builder.CreateFunction("return_void");
builder.InsertRetInst(nullptr);

this->jit.InsertIRModule(std::move(module));
auto assembly = this->jit.CompileAndLink();

auto test_symbol = assembly.Lookup("return_void");
auto test = test_symbol.ToPointer<void()>();
auto test = assembly["return_void"].ToPointer<void()>();

test();

Expand All @@ -34,22 +31,23 @@ TEST_F(Amd64TestRunner, ReturnVoidFunction)

TEST_F(Amd64TestRunner, ReturnConstantFunction)
{
int const EXPECTED_RETURN_VALUE = 69;

city::IRModule module{"test"};
auto builder = module.CreateBuilder();

auto function = builder.CreateFunction("return_constant");
builder.SetInsertPoint(function);
auto return_type = builder.GetType<int>();
(void)builder.CreateFunction("return_constant", return_type);

auto return_value = builder.CreateConstant<int>(69);
auto return_value = builder.CreateConstant<int>(EXPECTED_RETURN_VALUE);
builder.InsertRetInst(return_value);

this->jit.InsertIRModule(std::move(module));
auto assembly = this->jit.CompileAndLink();

auto test_symbol = assembly.Lookup("return_constant");
auto test = test_symbol.ToPointer<int()>();
auto test = assembly["return_constant"].ToPointer<int()>();

ASSERT_EQ(test(), 69);
ASSERT_EQ(test(), EXPECTED_RETURN_VALUE);

this->jit.RemoveModule("test");
}

0 comments on commit c699f0b

Please sign in to comment.