Commit 80ab7165 authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

clean up stack API a bit before introducing B stack

parent 9ea474a6
......@@ -32,12 +32,12 @@ exec(Code* expr, int frame_ptr, Thunk* target, bool force)
case VAR_ARG:
{
Thunk* var = stack[frame_ptr - ((VarEntry*) expr)->index];
Thunk* var = arg(frame_ptr, ((VarEntry*) expr)->index);
return forward_to(target, var);
}
case VAR_LOCAL:
{
Thunk* var = stack[frame_ptr + ((VarEntry*) expr)->index + 1];
Thunk* var = local(frame_ptr, ((VarEntry*) expr)->index + 1);
return forward_to(target, var);
}
}
......@@ -59,27 +59,27 @@ exec(Code* expr, int frame_ptr, Thunk* target, bool force)
if (force && slice->type == FT_PRIM) {
Thunk args[expr->nr_args];
int old_top = stack_top;
int old_top = stack_top_a;
for (int i = 0; i < expr->nr_args; i++) {
pushs(exec(((AppEntry*) expr)->args[i], frame_ptr, &args[i], true));
push_a(exec(((AppEntry*) expr)->args[i], frame_ptr, &args[i], true));
}
thunk = ((PrimEntry*) slice)->exec(target);
stack_top = old_top;
stack_top_a = old_top;
}
else if (force && slice->type == FT_FUN) {
int old_top = stack_top;
for (int i = 0; i < expr->nr_args; i++) {
pushs(exec(((AppEntry*) expr)->args[i], frame_ptr, NULL, is_strict_fun_arg((FunEntry*) slice, i)));
int old_top = stack_top_a;
for (int i = 0; i < expr->nr_args; i++) {
push_a(exec(((AppEntry*) expr)->args[i], frame_ptr, NULL, is_strict_fun_arg((FunEntry*) slice, i)));
}
thunk = exec(((FunEntry*) slice)->body, stack_top - 1, target, true);
thunk = exec(((FunEntry*) slice)->body, stack_top_a - 1, target, true);
stack_top = old_top;
stack_top_a = old_top;
}
else {
thunk = updateF(target, slice);
......@@ -94,7 +94,7 @@ exec(Code* expr, int frame_ptr, Thunk* target, bool force)
}
case VAR_ARG:
{
Thunk* basethunk = eval(stack[frame_ptr - var->index]);
Thunk* basethunk = eval(arg(frame_ptr, var->index));
Desc* slice =
get_slice(basethunk->desc->type == FT_SLICE ?
......@@ -135,7 +135,7 @@ exec(Code* expr, int frame_ptr, Thunk* target, bool force)
// Put the constructor arguments to the stack if matches
for (int i = 0; i < pattern->desc->arity; i++) {
pushs(pattern->_args[i]);
push_a(pattern->_args[i]);
}
// Fall through on purpose
......
......@@ -56,10 +56,10 @@ int main() {
// 44
// char* funstream = "160 F13 example.Start0 0 AF11 example.map2 VF11 example.incAF17 _predefined._Cons2 LI1 AF17 _predefined._Cons2 LI2 AF17 _predefined._Cons2 LI3 VF16 _predefined._Nil36 F11 example.inc1 1 AF3 add2 VA0 LI1 148 F11 example.map2 1 SVA0 2 C16 _predefined._NilVF16 _predefined._NilC17 _predefined._ConsAF17 _predefined._Cons2 AA1 1 VL0 AF11 example.map2 VA1 VL1 50 A2 17 _predefined._Cons2 0 16 _predefined._Nil0 0 ";
// [_predefined._Cons [2] [_predefined._Cons [3] [_predefined._Cons [4] [_predefined._Nil]]]]
char* funstream = "44 F13 example.Start0 0 AF11 example.fib1 LI36 119 F11 example.fib1 1 IAF2 lt2 VA0 LI2 LI1 AF3 add2 AF11 example.fib1 AF3 sub2 VA0 LI1 AF11 example.fib1 AF3 sub2 VA0 LI2 ";
//char* funstream = "44 F13 example.Start0 0 AF11 example.fib1 LI36 119 F11 example.fib1 1 IAF2 lt2 VA0 LI2 LI1 AF3 add2 AF11 example.fib1 AF3 sub2 VA0 LI1 AF11 example.fib1 AF3 sub2 VA0 LI2 ";
// fib
//char* funstream = "29 F9 Braun.int1 1 IVA0 LI1 LI0 27 F9 Braun.and2 2 IVA1 VA0 L099 F9 Braun.all2 1 SVA0 2 C9 Flite.NilL1C10 Flite.ConsAF9 Braun.and2 AA1 1 VL0 AF9 Braun.all2 VA1 VL1 98 F16 Braun._c;39;3_203 4 IAF3 eqB2 VA2 L0L0IAF3 eqB2 VA2 L1AF14 Braun.equal_182 VA1 VA0 VF7 nomatch175 F14 Braun.equal_182 3 SVA1 2 C9 Flite.NilSVA0 2 C9 Flite.NilL1C10 Flite.ConsL0C10 Flite.ConsSVA0 2 C9 Flite.NilL0C10 Flite.ConsAF16 Braun._c;39;3_203 AF3 eqI2 VL0 VL2 VL1 VL3 176 F9 Braun.ilv2 3 SVA1 2 C9 Flite.NilVA0 C10 Flite.ConsSVA0 2 C9 Flite.NilAF10 Flite.Cons2 VL0 VL1 C10 Flite.ConsAF10 Flite.Cons2 VL0 AF10 Flite.Cons2 VL2 AF9 Braun.ilv2 VL1 VL3 153 F12 Braun.toList1 1 SVA0 2 C11 Braun.EmptyVF9 Flite.NilC12 Braun.BranchAF10 Flite.Cons2 VL0 AF9 Braun.ilv2 AF12 Braun.toList1 VL1 AF12 Braun.toList1 VL2 40 A2 11 Braun.Empty0 0 12 Braun.Branch3 0 167 F12 Braun.insert2 1 SVA0 2 C11 Braun.EmptyAF12 Braun.Branch3 VA1 VF11 Braun.EmptyVF11 Braun.EmptyC12 Braun.BranchAF12 Braun.Branch3 VA1 AF12 Braun.insert2 VL0 VL2 VL1 119 F14 Braun.fromList1 1 SVA0 2 C9 Flite.NilVF11 Braun.EmptyC10 Flite.ConsAF12 Braun.insert2 VL0 AF14 Braun.fromList1 VL1 90 F13 Braun.prop_171 1 AF14 Braun.equal_182 VA0 AF12 Braun.toList1 AF14 Braun.fromList1 VA0 118 F15 Braun.replicate2 2 IAF3 eqI2 VA1 LI0 VF9 Flite.NilAF10 Flite.Cons2 VA0 AF15 Braun.replicate2 AF3 sub2 VA1 LI1 VA0 44 F11 Braun.<=_162 3 AF3 not1 AF2 lt2 VA0 VA1 35 A2 9 Flite.Nil0 0 10 Flite.Cons2 0 121 F12 Braun.fromTo2 3 IAF11 Braun.<=_162 VA1 VA0 AF10 Flite.Cons2 VA1 AF12 Braun.fromTo2 AF3 add2 VA1 LI1 VA0 VF9 Flite.Nil125 F11 Braun.Start0 0 AF9 Braun.int1 AF9 Braun.all2 VF13 Braun.prop_17AF15 Braun.replicate2 LI6000 AF12 Braun.fromTo2 LI0 LI255 27 F4 main0 0 VF11 Braun.Start";
char* funstream = "29 F9 Braun.int1 1 IVA0 LI1 LI0 27 F9 Braun.and2 2 IVA1 VA0 L099 F9 Braun.all2 1 SVA0 2 C9 Flite.NilL1C10 Flite.ConsAF9 Braun.and2 AA1 1 VL0 AF9 Braun.all2 VA1 VL1 98 F16 Braun._c;39;3_203 4 IAF3 eqB2 VA2 L0L0IAF3 eqB2 VA2 L1AF14 Braun.equal_182 VA1 VA0 VF7 nomatch175 F14 Braun.equal_182 3 SVA1 2 C9 Flite.NilSVA0 2 C9 Flite.NilL1C10 Flite.ConsL0C10 Flite.ConsSVA0 2 C9 Flite.NilL0C10 Flite.ConsAF16 Braun._c;39;3_203 AF3 eqI2 VL0 VL2 VL1 VL3 176 F9 Braun.ilv2 3 SVA1 2 C9 Flite.NilVA0 C10 Flite.ConsSVA0 2 C9 Flite.NilAF10 Flite.Cons2 VL0 VL1 C10 Flite.ConsAF10 Flite.Cons2 VL0 AF10 Flite.Cons2 VL2 AF9 Braun.ilv2 VL1 VL3 153 F12 Braun.toList1 1 SVA0 2 C11 Braun.EmptyVF9 Flite.NilC12 Braun.BranchAF10 Flite.Cons2 VL0 AF9 Braun.ilv2 AF12 Braun.toList1 VL1 AF12 Braun.toList1 VL2 40 A2 11 Braun.Empty0 0 12 Braun.Branch3 0 167 F12 Braun.insert2 1 SVA0 2 C11 Braun.EmptyAF12 Braun.Branch3 VA1 VF11 Braun.EmptyVF11 Braun.EmptyC12 Braun.BranchAF12 Braun.Branch3 VA1 AF12 Braun.insert2 VL0 VL2 VL1 119 F14 Braun.fromList1 1 SVA0 2 C9 Flite.NilVF11 Braun.EmptyC10 Flite.ConsAF12 Braun.insert2 VL0 AF14 Braun.fromList1 VL1 90 F13 Braun.prop_171 1 AF14 Braun.equal_182 VA0 AF12 Braun.toList1 AF14 Braun.fromList1 VA0 118 F15 Braun.replicate2 2 IAF3 eqI2 VA1 LI0 VF9 Flite.NilAF10 Flite.Cons2 VA0 AF15 Braun.replicate2 AF3 sub2 VA1 LI1 VA0 44 F11 Braun.<=_162 3 AF3 not1 AF2 lt2 VA0 VA1 35 A2 9 Flite.Nil0 0 10 Flite.Cons2 0 121 F12 Braun.fromTo2 3 IAF11 Braun.<=_162 VA1 VA0 AF10 Flite.Cons2 VA1 AF12 Braun.fromTo2 AF3 add2 VA1 LI1 VA0 VF9 Flite.Nil125 F11 Braun.Start0 0 AF9 Braun.int1 AF9 Braun.all2 VF13 Braun.prop_17AF15 Braun.replicate2 LI6000 AF12 Braun.fromTo2 LI0 LI255 27 F4 main0 0 VF11 Braun.Start";
//braun
#ifdef DEBUG
......@@ -72,12 +72,12 @@ int main() {
// TODO: put it into a special "expression" space, instead of "code"
char *exprstream = "VF13 example.Start";
//char *exprstream = "VF11 Braun.Start";
//char *exprstream = "VF13 example.Start";
char *exprstream = "VF11 Braun.Start";
Code* expr = parseTerm(&exprstream);
Thunk* res = exec(expr, stack_top, NULL, true);
Thunk* res = exec(expr, stack_top_a, NULL, true);
eval(res);
print(res, true);
......
......@@ -9,8 +9,8 @@ int code_alloc;
int heap_alloc;
int nr_heap_alloc;
int stack_top;
Thunk* stack[STACK_SIZE];
int stack_top_a;
Thunk* stack_a[STACK_SIZE_A];
#define heap_size 1024*1024*1024
char* heap_start;
......@@ -28,7 +28,7 @@ void init_mem() {
heap_alloc = 0;
nr_heap_alloc = 0;
stack_top = 0;
stack_top_a = 0;
heap_start = (char*) malloc(heap_size);
assert(heap_start != NULL);
......
......@@ -3,15 +3,18 @@
#include "thunk.h"
#define STACK_SIZE 10240
#define STACK_SIZE_A 10240
#define STACK_SIZE_B 10240
extern int stack_top;
extern Thunk* stack[STACK_SIZE];
extern int stack_top_a;
extern Thunk* stack_a[STACK_SIZE_A];
#define get(i) stack[stack_top-1-(i)]
#define pops() stack[--stack_top]
#define pushs(r) stack[stack_top++]=(r)
#define pop(i) stack_top -= i
#define peek_a() stack_a[stack_top_a-1]
#define pop_a() stack_a[--stack_top_a]
#define push_a(r) stack_a[stack_top_a++]=(r)
#define arg(base, idx) stack_a[base-idx]
#define local(base, idx) stack_a[base+idx]
void init_mem();
void print_stat();
......
......@@ -7,8 +7,8 @@
#include "mem.h"
struct Thunk* __add(Thunk* target) {
Thunk* arg1 = stack[stack_top - 2];
Thunk* arg2 = stack[stack_top - 1];
Thunk* arg1 = arg(stack_top_a, 2);
Thunk* arg2 = arg(stack_top_a, 1);
arg1 = eval(arg1);
arg2 = eval(arg2);
......@@ -17,8 +17,8 @@ struct Thunk* __add(Thunk* target) {
}
struct Thunk* __sub(Thunk* target) {
Thunk* arg1 = stack[stack_top - 2];
Thunk* arg2 = stack[stack_top - 1];
Thunk* arg1 = arg(stack_top_a, 2);
Thunk* arg2 = arg(stack_top_a, 1);
arg1 = eval(arg1);
arg2 = eval(arg2);
......@@ -27,8 +27,8 @@ struct Thunk* __sub(Thunk* target) {
}
struct Thunk* __gt(Thunk* target) {
Thunk* arg1 = stack[stack_top - 2];
Thunk* arg2 = stack[stack_top - 1];
Thunk* arg1 = arg(stack_top_a, 2);
Thunk* arg2 = arg(stack_top_a, 1);
arg1 = eval(arg1);
arg2 = eval(arg2);
......@@ -37,8 +37,8 @@ struct Thunk* __gt(Thunk* target) {
}
struct Thunk* __lt(Thunk* target) {
Thunk* arg1 = stack[stack_top - 2];
Thunk* arg2 = stack[stack_top - 1];
Thunk* arg1 = arg(stack_top_a, 2);
Thunk* arg2 = arg(stack_top_a, 1);
arg1 = eval(arg1);
arg2 = eval(arg2);
......@@ -47,8 +47,8 @@ struct Thunk* __lt(Thunk* target) {
}
struct Thunk* __eqI(Thunk* target) {
Thunk* arg1 = stack[stack_top - 2];
Thunk* arg2 = stack[stack_top - 1];
Thunk* arg1 = arg(stack_top_a, 2);
Thunk* arg2 = arg(stack_top_a, 1);
arg1 = eval(arg1);
arg2 = eval(arg2);
......@@ -57,8 +57,8 @@ struct Thunk* __eqI(Thunk* target) {
}
struct Thunk* __eqB(Thunk* target) {
Thunk* arg1 = stack[stack_top - 2];
Thunk* arg2 = stack[stack_top - 1];
Thunk* arg1 = arg(stack_top_a, 2);
Thunk* arg2 = arg(stack_top_a, 1);
arg1 = eval(arg1);
arg2 = eval(arg2);
......@@ -67,7 +67,7 @@ struct Thunk* __eqB(Thunk* target) {
}
struct Thunk* __not(Thunk* target) {
Thunk* arg1 = stack[stack_top - 1];
Thunk* arg1 = arg(stack_top_a, 1);
arg1 = eval(arg1);
......
......@@ -147,26 +147,26 @@ struct Thunk* eval(Thunk* thunk) {
return thunk;
case FT_FUN:
int old_top;
old_top = stack_top;
old_top = stack_top_a;
for (int i = 0; i < thunk->desc->arity; i++) {
// TODO: handle strictness
pushs(thunk->_args[i]);
push_a(thunk->_args[i]);
}
thunk = exec(((FunEntry*) thunk->desc)->body, stack_top - 1, thunk, true);
thunk = exec(((FunEntry*) thunk->desc)->body, stack_top_a - 1, thunk, true);
stack_top = old_top;
stack_top_a = old_top;
break;
case FT_PRIM:
old_top = stack_top;
old_top = stack_top_a;
for (int i = 0; i < thunk->desc->arity; i++) {
pushs(thunk->_args[i]);
push_a(thunk->_args[i]);
}
((PrimEntry*) thunk->desc)->exec(thunk);
stack_top = old_top;
stack_top_a = old_top;
break;
default:
printf("eval: unhandled DESC\n");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment