Skip to content

Commit

Permalink
Got rootstack working finally...
Browse files Browse the repository at this point in the history
  • Loading branch information
chutasano committed Apr 1, 2018
1 parent f2bcf93 commit 80185a1
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 40 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ SOURCES=main.cpp test.cpp interp.cpp r0.cpp x0.cpp c0.cpp x0s.cpp
OBJECTS=$(SOURCES:.cpp=.o) compile.o
EXECUTABLE=main
ABSPATH=$(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))

VPATH = rep


all: $(SOURCES) helper_lib compile.o $(EXECUTABLE)

debug: CFLAGS += -DDEBUG
debug: main

$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@

Expand Down
2 changes: 0 additions & 2 deletions compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ using namespace std;
#define STR(a) _STR(a)
#define _STR(a) #a

#define DEBUG

static string to_asm(r0::P &p)
{
p.uniquify();
Expand Down
4 changes: 2 additions & 2 deletions rep/r0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ Var* Var::clone() const

void Var::uniquify(unordered_map<string, string> m)
{
auto it = m.find(this->name); // FIXME const iterator will be the "right"
const auto &it = m.find(this->name); // FIXME const iterator will be the "right"
// thing to do instead of auto
if (it != m.end())
{
Expand Down Expand Up @@ -466,7 +466,7 @@ void Let::uniquify(unordered_map<string, string> m)
c0::Arg* Let::to_c0(unordered_map<string, int> &vars, vector<c0::AS*> &stmts) const
{
stmts.push_back(new c0::S(this->name, this->ve->to_c0(vars, stmts)));
vars[this->name] = t;
vars[this->name] = this->ve->t;
return this->be->to_c0(vars, stmts);
}

Expand Down
69 changes: 54 additions & 15 deletions rep/x0s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ x0::P P::assign()
for (auto s : vars)
{
in.add_node(s.first, (s.second > TVEC) ? ROOTSTACK : STACK);
#ifdef DEBUG
cout << s.first << ":" << s.second << endl;
#endif
}
// get lifetime of all vars
unordered_map<string, pair<int, int> > lifetime;
Expand Down Expand Up @@ -92,6 +95,9 @@ x0::P P::assign()
{
for (pair<ICollect*, int> p : collects)
{
#ifdef DEBUG
cout << "vec_min, collect, vec_max: " << min << ", " << p.second << ", " << max << endl;
#endif
if (p.second <= max && p.second >= min)
{
#ifdef DEBUG
Expand Down Expand Up @@ -295,25 +301,40 @@ list<x0::I*> IJmp::assign(const s2vmap &vmap)

list <x0::I*> ICollect::assign(const s2vmap &vmap)
{
vector<x0::Dst*> move_refs;
int worst_offset = 0;
unsigned int worst_rootstack = 0;
// todo optimize this
for (Dst* r : live_references)
{
if (typeid(*r) == typeid(Var))
{
Var* v = static_cast<Var*>(r);
int offs = vmap.at(v->var).first - regs.size();
if (offs > (int)worst_rootstack)
{
worst_rootstack = (unsigned int)offs;
}
}
else
{
cerr << "HMMMM. x0s::ICollect does not support non-var vectors for now\n";
exit(4);
}
}
vector<x0::Dst*> vec_regs;
vector<x0::Dst*> rstack(live_references.size() + worst_rootstack);
for (Dst* r : live_references)
{
if (typeid(*r) != typeid(Var))
if (typeid(*r) == typeid(Var))
{
Var* v = static_cast<Var*>(r);
int offs = vmap.at(v->var).first - regs.size() + 1;
int offs = vmap.at(v->var).first - regs.size();
if (offs < 0)
{
move_refs.push_back(static_cast<x0::Dst*>(v->assign(vmap)));
vec_regs.push_back(static_cast<x0::Dst*>(v->assign(vmap)));
}
else
{
// get highest offset -> push registers on that
if (worst_offset < offs)
{
worst_offset = offs;
}
rstack.at(offs) = static_cast<x0::Dst*>(v->assign(vmap));
}
}
else
Expand All @@ -322,16 +343,34 @@ list <x0::I*> ICollect::assign(const s2vmap &vmap)
exit(4);
}
}
unsigned int i=0;
//assign registers to smallest available offsets in rootstack
for (x0::Dst* r : vec_regs)
{
for (; rstack.at(i) != nullptr; i++);
rstack.at(i) = r;
}
// now make sure the array is contiguous from 0 to live_ref.size, if not, appropriately
// swap elements to fill in the gaps
for (i = live_references.size(); i < rstack.size(); i++)
{
unsigned int j=0;
if (rstack.at(i) != nullptr)
{
for (; rstack.at(j) != nullptr; j++);
rstack.at(j) = rstack.at(i);
}
}
list<x0::I*> instrs;
for (int i = 0; i < (int)move_refs.size(); i++)
for (unsigned int i = 0; i < live_references.size(); i++)
{
instrs.push_back(new x0::ISrcDst(MOVQ, move_refs.at(i), new x0::Mem("r11", worst_offset+i)));
instrs.push_back(new x0::ISrcDst(MOVQ, rstack.at(i), new x0::Mem("r12", 8*i)));
}
ICall call_collect("_lang_collect", { new Reg("r12"), new Con(worst_offset+move_refs.size()-1)}, new Reg("r15"));
ICall call_collect("_lang_collect", { new Reg("r12"), new Con(live_references.size())}, new Reg("r15"));
instrs.splice(instrs.end(), call_collect.assign(vmap));
for (int i = 0; i < (int)move_refs.size(); i++)
for (unsigned int i = 0; i < live_references.size(); i++)
{
instrs.push_back(new x0::ISrcDst(MOVQ, new x0::Mem("r11", worst_offset+i), move_refs.at(i)));
instrs.push_back(new x0::ISrcDst(MOVQ, new x0::Mem("r12", 8*i), rstack.at(i)));
}

#ifdef DEBUG_BUILD
Expand Down
2 changes: 1 addition & 1 deletion runtime/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
all: lib.o

lib.o: lib.c
gcc -Wall -c lib.c -o lib.o
gcc -Wall -g -c lib.c -o lib.o

clean:
rm -f lib.o
Expand Down
19 changes: 17 additions & 2 deletions runtime/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,24 @@ void* _lang_init_rootstack(int64_t rootstack_size)
}

// returns next free ptr after stop&copy
void* _lang_collect(void* root_stack_ptr, int64_t size)
void* _lang_collect(int64_t** root_stack_ptr, int64_t size)
{
printf("COLLECT called with RSP=%ld, size=%ld\n", root_stack_ptr, size);
if (size == 0)
{
printf("COLLECT size = 0");
}
else if (debug)
{
printf("COLLECT called with RSP=%ld, size=%ld\n", root_stack_ptr, size);
debug = 0;
int i;
for (i = 0; i < size; i++)
{
_lang_print_vec(root_stack_ptr[i]);
}
debug = 1;
}

// for now just allocate more space
void* new_addr = malloc(sizeof(int64_t)*512);
_LANG_HEAP_END = new_addr + heapsize;
Expand Down
35 changes: 18 additions & 17 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ function<bool(r0::P, vec_t[])> testfunc;
static bool both(const r0::P &p, vec_t expect[])
{
bool woof = test_interp(p, expect);
//bool meow = test_compile(p, expect[0].val);
bool meow = test_compile(p, expect);
if (!woof)
{
Expand Down Expand Up @@ -185,10 +184,9 @@ static void tt(r0::E* e, int expect, int heap_size=2048)

void test_all()
{
// TODO switch to test both once compiler is implemented
//testfunc = test_interp;
//testfunc = test_compile;
testfunc = both;
testfunc = test_compile;
//testfunc = both;

cout << "Start Tests\n";

Expand Down Expand Up @@ -427,24 +425,27 @@ void test_all()

ts("Lots of active vectors");
{
UPTR(r0::Vector, v1, { n10, n23, nn1, bt, bf} );
UPTR(r0::Vector, v2, { n23, nn1, bt, bf} );
UPTR(r0::Vector, v3, { n10, n23, nn1, bt, bf} );
UPTR(r0::Vector, v4, { n10, n23, nn1, bt, bf} );
UPTR(r0::Vector, v5, { n10, n23, nn1, bt, bf} );
UPTR(r0::Vector, v6, { n10, n23, nn1, bt, bf} );
UPTR(r0::VectorRef, vr1, v1, 0);
UPTR(r0::VectorRef, vr2, v2, 0);
UPTR(r0::VectorRef, vr3, v3, 0);
UPTR(r0::VectorRef, vr4, v4, 0);
UPTR(r0::VectorRef, vr5, v5, 0);
UPTR(r0::VectorRef, vr6, v6, 0);
VAR(a);
VAR(b);
VAR(c);
UPTR(r0::Vector, _v1, { n10, n23, nn1, bt, bf} );
UPTR(r0::Vector, _v2, { n23, nn1, bt, bf} );
UPTR(r0::Vector, _v3, { nn1, n23, n10, n10, n10, n10} );
UPTR(r0::VectorRef, vr1, va, 0);
UPTR(r0::VectorRef, vr2, vb, 0);
UPTR(r0::VectorRef, vr3, vc, 0);
UPTR(r0::VectorRef, vr4, va, 0);
UPTR(r0::VectorRef, vr5, vb, 0);
UPTR(r0::VectorRef, vr6, vc, 0);
PLUS(vr1, vr2);
PLUS(bplus_vr1_vr2, vr3);
PLUS(vr4, vr5);
PLUS(bplus_vr4_vr5, vr6);
PLUS(bplus_bplus_vr1_vr2_vr3, bplus_bplus_vr4_vr5_vr6);
t(bplus_bplus_bplus_vr1_vr2_vr3_bplus_bplus_vr4_vr5_vr6, 73, 128);
LET(v3let, c, _v3, bplus_bplus_bplus_vr1_vr2_vr3_bplus_bplus_vr4_vr5_vr6 );
LET(v2let, b, _v2, v3let);
LET(v1let, a, _v1, v2let);
t(v1let, 64, 128);
}

cout << "Total tests failed: " << fails << endl;
Expand Down

0 comments on commit 80185a1

Please sign in to comment.