Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pubby committed Aug 7, 2022
1 parent d54f0dd commit 15d3e20
Show file tree
Hide file tree
Showing 32 changed files with 602 additions and 399 deletions.
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: all tests deps cleandeps clean run
.PHONY: all debug release tests deps cleandeps clean run
all: compiler tests
run: compiler
./compiler
Expand Down Expand Up @@ -31,9 +31,7 @@ GIT_COMMIT := "$(shell git describe --abbrev=8 --dirty --always --tags)"

override CXXFLAGS+= \
-std=c++20 \
-O3 \
-pthread \
-g \
-Wall \
-Wextra \
-Wno-unused-parameter \
Expand All @@ -44,8 +42,13 @@ override CXXFLAGS+= \
-pipe \
$(INCS) \
-DVERSION=\"$(VERSION)\" \
-DGIT_COMMIT=\"$(GIT_COMMIT)\" \
-DNDEBUG
-DGIT_COMMIT=\"$(GIT_COMMIT)\"

debug: CXXFLAGS += -O0 -g
debug: compiler

release: CXXFLAGS += -O3 -DNDEBUG
release: compiler

VPATH=$(SRCDIR)

Expand Down
2 changes: 1 addition & 1 deletion graph.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
for f in graphs/*.gv;
do
echo $f
dot $f -T svg -o ${f%%.*}.svg
dot $f -Goverlap=scale -T svg -o ${f%%.*}.svg
done

#rm graphs/*.gv
Expand Down
89 changes: 76 additions & 13 deletions src/array_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

#include <algorithm>
#include <array>
#include <cassert>
#include <memory>
#include <vector>
#include <list>

// A simple allocator that only supports allocation, not free.
// Memory is still cleared on pool destruction or the calling of 'clear()'.
Expand Down Expand Up @@ -44,6 +46,7 @@ class array_pool_t
{
used_size += 1;
reserve(1);
assert(used);
storage_t* storage = used->data + used->size;
new(storage) T(std::forward<Args>(args)...);
++used->size;
Expand Down Expand Up @@ -106,11 +109,17 @@ class array_pool_t
oversized.clear();
used_size = 0;

if(used)
used->free();

// Move the used list onto the free list.
std::unique_ptr<buffer_t>* last = &free;
while(*last)
last = &(*last)->prev;
*last = std::move(used);
if(first_free)
first_free->set_prev(used.release());
else
free = std::move(used);

first_free = first_used;
first_used = nullptr;
}

void shrink_to_fit()
Expand All @@ -119,7 +128,6 @@ class array_pool_t
}

// Calls Func on every allocated value.

template<typename Func>
void for_each(Func func)
{
Expand All @@ -134,6 +142,27 @@ class array_pool_t

std::size_t size() const { return used_size; }

// Steals all the allocations from 'other'.
// (Does not steal the free list)
void splice(array_pool_t& other)
{
if(!other.first_used)
return;

if(first_used)
first_used->set_prev(other.used.release());
else
used = std::move(other.used);

first_used = other.first_used;
other.first_used = nullptr;

oversized.splice(oversized.begin(), other.oversized);

used_size += other.used_size;
other.used_size = 0;
}

private:
using storage_t =
typename std::aligned_storage<sizeof(T), alignof(T)>::type;
Expand All @@ -148,17 +177,25 @@ class array_pool_t
if(free)
{
used = std::move(free);
free = std::move(used->prev);
free.reset(used->release_prev());
if(!free)
first_free = nullptr;
}
else
used.reset(new buffer_t());

assert(!used->prev);

// Zero the size.
used->size = 0;

// Update the pointer
used->prev = std::move(old_buffer);
// Update the pointers
used->set_prev(old_buffer.release());
if(!first_used)
first_used = used.get();
}

assert(used && used->size + size <= ChunkSize);
}

bool use_oversized(std::size_t size) const
Expand All @@ -181,19 +218,45 @@ class array_pool_t
public:
~buffer_t()
{
for(unsigned i = 0; i < size; ++i)
reinterpret_cast<T&>(data[i]).~T();
free();
}

void free()
{
// Implement non-recursively, to avoid stack overflows
for(buffer_t* buf = this; buf; buf = buf->prev)
{
for(unsigned i = 0; i < buf->size; ++i)
reinterpret_cast<T&>(buf->data[i]).~T();
buf->size = 0;
}
}

void set_prev(buffer_t* new_prev)
{
if(prev)
delete prev;
prev = new_prev;
}

buffer_t* release_prev()
{
buffer_t* ret = prev;
prev = nullptr;
return ret;
}

storage_t data[ChunkSize];
std::size_t size;
std::unique_ptr<buffer_t> prev;
std::size_t size = 0;
buffer_t* prev = nullptr;
};

std::unique_ptr<buffer_t> used;
std::unique_ptr<buffer_t> free;
std::vector<std::vector<T>> oversized;
std::list<std::vector<T>> oversized;
std::size_t used_size = 0;
buffer_t* first_used = nullptr;
buffer_t* first_free = nullptr;
};

#endif
7 changes: 3 additions & 4 deletions src/asm_proc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ void asm_proc_t::convert_long_branch_ops()
dist -= size_diff;

// Change to short instruction when in range
if(dist <= 127 || dist >= -128)
if(dist <= 127 && dist >= -128)
{
inst.op = new_op;
progress = true;

assert(bytes_between(i+1, label_i) <= 127);
assert(bytes_between(i+1, label_i) >= -128);
}
Expand Down Expand Up @@ -176,7 +176,6 @@ void asm_proc_t::optimize_short_jumps(bool initial)

void asm_proc_t::optimize(bool initial)
{
return; // TODO
// Order matters here.
absolute_to_zp();
optimize_short_jumps(initial);
Expand Down Expand Up @@ -292,7 +291,7 @@ void asm_proc_t::write_bytes(std::uint8_t* const start, int bank) const

auto const write_inst = [&](asm_inst_t const& inst)
{
assert(!(op_flags(inst.op) & ASMF_FAKE));
passert(!(op_flags(inst.op) & ASMF_FAKE), to_string(inst.op), inst.arg);
std::uint8_t const op = op_code(inst.op);

switch(op_addr_mode(inst.op))
Expand Down
2 changes: 1 addition & 1 deletion src/bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ void bitset_for_each(UInt bitset, Fn fn, unsigned span = 0)
while(bitset)
{
std::size_t bit = builtin::ctz(bitset);
bitset ^= 1 << bit;
bitset ^= 1ull << bit;
fn(Bit{bit + span});
}
}
Expand Down
22 changes: 10 additions & 12 deletions src/byteify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include "worklist.hpp"
#include "format.hpp"

SSA_VERSION(1);

namespace bc = ::boost::container;

namespace // anonymous
Expand Down Expand Up @@ -173,15 +171,20 @@ void byteify(ir_t& ir, fn_t const& fn)

if(ssa_flags(ssa_it->op()) & SSAF_INDEXES_PTR)
{
using namespace ssai::rw_ptr;

// Pointer accesses may create 'SSA_make_ptr' nodes.
if(ssa_it->input(0).holds_ref())
if(ssa_it->input(PTR).holds_ref())
{
assert(!ssa_it->input(PTR_HI).holds_ref());

ssa_ht const lo = cfg_node.emplace_ssa(
SSA_make_ptr_lo, TYPE_U, ssa_it->input(0));
SSA_make_ptr_lo, TYPE_U, ssa_it->input(PTR));
ssa_ht const hi = cfg_node.emplace_ssa(
SSA_make_ptr_hi, TYPE_U, ssa_it->input(0));
ssa_it->link_change_input(0, lo);
ssa_it->link_change_input(1, hi);
SSA_make_ptr_hi, TYPE_U, ssa_it->input(PTR));

ssa_it->link_change_input(PTR, lo);
ssa_it->link_change_input(PTR_HI, hi);
}
}

Expand All @@ -205,8 +208,6 @@ void byteify(ir_t& ir, fn_t const& fn)

if(!is_scalar(type.name()))
continue;

SSA_VERSION(1);

type_t split_type = TYPE_U;
if(ssa_it->type().name() == TYPE_TEA)
Expand Down Expand Up @@ -245,8 +246,6 @@ void byteify(ir_t& ir, fn_t const& fn)
cfg_node_t& cfg_node = *cfg_it;
for(ssa_ht ssa_it = cfg_node.ssa_begin(); ssa_it; ++ssa_it)
{
SSA_VERSION(1);

switch(ssa_it->op())
{
case SSA_fn_call:
Expand Down Expand Up @@ -515,7 +514,6 @@ void byteify(ir_t& ir, fn_t const& fn)
if(ssa_node->type() == TYPE_S)
ssa_node->set_type(TYPE_U);

SSA_VERSION(1);
switch(ssa_node->op())
{
case SSA_rol:
Expand Down
10 changes: 6 additions & 4 deletions src/cg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,14 @@ void code_gen(ir_t& ir, fn_t& fn)
// Setup 'ptr_alt's for ptr inputs.
if(ssa_flags(ssa_it->op()) & SSAF_INDEXES_PTR)
{
assert(ssa_it->input(0).holds_ref() == ssa_it->input(1).holds_ref());
using namespace ssai::rw_ptr;

if(ssa_it->input(0).holds_ref())
assert(ssa_it->input(PTR).holds_ref() == ssa_it->input(PTR_HI).holds_ref());

if(ssa_it->input(PTR).holds_ref())
{
ssa_ht const lo = ssa_it->input(0).handle();
ssa_ht const hi = ssa_it->input(1).handle();
ssa_ht const lo = ssa_it->input(PTR).handle();
ssa_ht const hi = ssa_it->input(PTR_HI).handle();
auto& lo_d = cg_data(lo);
auto& hi_d = cg_data(hi);

Expand Down
1 change: 1 addition & 0 deletions src/cg_cset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ssa_ht cset_head(ssa_ht h)
assert(h->op());
while(true)
{
// TODO: update pointers to point to head.
auto& d = cg_data(h);
if(d.cset_head.holds_ref())
{
Expand Down
Loading

0 comments on commit 15d3e20

Please sign in to comment.