interpreter fixes

This commit is contained in:
ProgramSnail 2024-12-17 04:15:25 +03:00
parent b25d441287
commit 73d3fbc388
5 changed files with 29 additions and 22 deletions

View file

@ -173,7 +173,9 @@ static inline void s_enter_f(char *rp, bool is_closure_call, auint args_sz,
s_failure(&s, "not enough parameters in function stack"); s_failure(&s, "not enough parameters in function stack");
} }
void *closure = is_closure_call ? s_nth(args_sz) : NULL; void *closure = is_closure_call ? *s_nth(args_sz) : NULL;
// printf("is_closure_call: %i, closure: %li\n", is_closure_call,
// (aint)closure);
// s_push_nil(s); // sp contains value, frame starts with next value // s_push_nil(s); // sp contains value, frame starts with next value
s_pushn_nil(frame_sz()); s_pushn_nil(frame_sz());
@ -289,15 +291,15 @@ static inline void **var_by_category(enum VarCategory category, size_t id) {
s_failure(&s, "not boxed value expected in closure index"); s_failure(&s, "not boxed value expected in closure index");
} }
data *d = TO_DATA(s.fp->closure); data *d = TO_DATA(s.fp->closure);
size_t count = get_len(d) - 1; int count = get_len(d) - 1;
#ifdef DEBUG_VERSION #ifdef DEBUG_VERSION
printf("id is %i, count is %i\n", id, count); printf("id is %i, count is %i\n", id, count);
#endif #endif
if (count <= id) { if ((int64_t)id >= count) {
s_failure(&s, s_failure(&s,
"can't read arguments: too big id"); //, %i >= %ul", id, count); "can't read arguments: too big id"); //, %i >= %ul", id, count);
} }
return (void **)d->contents + id; // order is not important return ((void **)d->contents) + count - id; // order is not important
} }
return var; return var;

View file

@ -8,11 +8,12 @@ suffix=".lama"
for test in ../regression/*.lama; do for test in ../regression/*.lama; do
echo $test echo $test
lamac -b $test > /dev/null lamac -b $test > /dev/null
./byterun.exe -v test*.bc > /dev/null test_file="${test%.*}"
echo $test_file
cat $test_file.input | ./byterun.exe -vi test*.bc > /dev/null
rm test*.bc rm test*.bc
echo "done" echo "done"
done done
rm test.bc
rm *.o rm *.o

View file

@ -128,11 +128,11 @@ void analyze(Bytefile *bf) {
current_ip = ip; current_ip = ip;
saved_current_ip = current_ip; saved_current_ip = current_ip;
// #ifdef DEBUG_VERSION #ifdef DEBUG_VERSION
const auto [cmd, l] = parse_command(&ip, bf, std::cout); const auto [cmd, l] = parse_command(&ip, bf, std::cout);
// #else #else
// const auto [cmd, l] = parse_command(&ip, bf); const auto [cmd, l] = parse_command(&ip, bf);
// #endif #endif
if (current_begin_counter == nullptr && cmd != Cmd::BEGIN && if (current_begin_counter == nullptr && cmd != Cmd::BEGIN &&
cmd != Cmd::CBEGIN) { cmd != Cmd::CBEGIN) {
@ -144,9 +144,9 @@ void analyze(Bytefile *bf) {
} }
current_stack_depth = visited[current_ip - bf->code_ptr]; current_stack_depth = visited[current_ip - bf->code_ptr];
// #ifdef DEBUG_VERSION #ifdef DEBUG_VERSION
std::cout << " -- [" << current_stack_depth << ']' << '\n'; std::cout << " -- [" << current_stack_depth << ']' << '\n';
// #endif #endif
++current_ip; // skip command byte ++current_ip; // skip command byte

View file

@ -14,7 +14,7 @@ void *__stop_custom_data;
#define ASSERT_UNBOXED(memo, x) \ #define ASSERT_UNBOXED(memo, x) \
do \ do \
if (!UNBOXED(x)) \ if (!UNBOXED(x)) \
failure("unboxed value expected in %s\n", memo); \ failure("%s: unboxed value expected in %s\n", __LINE__, memo); \
while (0) while (0)
struct State s; struct State s;
@ -94,9 +94,9 @@ void run(Bytefile *bf, int argc, char **argv) {
s.instr_ip = s.ip; s.instr_ip = s.ip;
uint8_t x = ip_read_byte(&s.ip), h = (x & 0xF0) >> 4, l = x & 0x0F; uint8_t x = ip_read_byte(&s.ip), h = (x & 0xF0) >> 4, l = x & 0x0F;
#ifdef DEBUG_VERSION // #ifdef DEBUG_VERSION
printf("0x%.8x\n", s.ip - bf->code_ptr - 1); printf("0x%.8x: %s\n", s.ip - bf->code_ptr - 1, read_cmd(s.ip - 1, s.bf));
#endif // #endif
switch (h) { switch (h) {
case CMD_EXIT: case CMD_EXIT:
@ -106,10 +106,13 @@ void run(Bytefile *bf, int argc, char **argv) {
case CMD_BINOP: { // BINOP ops[l-1] case CMD_BINOP: { // BINOP ops[l-1]
void *snd = s_pop(); void *snd = s_pop();
void *fst = s_pop(); void *fst = s_pop();
if (l == CMD_BINOP_SUB) { int op = l - 1;
if (op == CMD_BINOP_SUB) {
s_push_i(Ls__Infix_45(fst, snd)); s_push_i(Ls__Infix_45(fst, snd));
} else if (op == CMD_BINOP_EQ) {
s_push_i(Ls__Infix_6161(fst, snd));
} else { } else {
switch (l - 1) { switch (op) {
#define BINOP_OPR(val, op) \ #define BINOP_OPR(val, op) \
case val: \ case val: \
ASSERT_UNBOXED("captured op:1", fst); \ ASSERT_UNBOXED("captured op:1", fst); \
@ -357,7 +360,8 @@ void run(Bytefile *bf, int argc, char **argv) {
#endif #endif
s_push(bf->code_ptr + call_offset); s_push(bf->code_ptr + call_offset);
void *closure = Bclosure((aint *)__gc_stack_top, args_count); void *closure = Bclosure((aint *)__gc_stack_top, BOX(args_count));
// printf("args is %li, count is %li\n", args_count, get_len(TO_DATA(closure)));
s_popn(args_count + 1); s_popn(args_count + 1);
s_push(closure); s_push(closure);
@ -397,8 +401,8 @@ void run(Bytefile *bf, int argc, char **argv) {
aint args_count = ip_read_int(&s.ip); aint args_count = ip_read_int(&s.ip);
#ifdef DEBUG_VERSION #ifdef DEBUG_VERSION
printf("tag hash is %i, n is %i, peek is %i\n", printf("tag hash is %i, n is %i, peek is %i, unboxed: %li\n",
UNBOX(LtagHash((char *)name)), args_count, s_peek(&s)); UNBOX(LtagHash((char *)name)), args_count, s_peek(&s), UNBOXED(s_peek(&s)));
#endif #endif
s_push_i(Btag(s_pop(), LtagHash((char *)name), BOX(args_count))); s_push_i(Btag(s_pop(), LtagHash((char *)name), BOX(args_count)));

View file

@ -596,7 +596,7 @@ void print_file(const Bytefile &bf, std::ostream &out) {
while (true) { while (true) {
out << std::setfill('0') << std::setw(8) << std::hex << ip - bf.code_ptr out << std::setfill('0') << std::setw(8) << std::hex << ip - bf.code_ptr
<< ": "; << ": " << std::dec;
const auto [cmd, l] = parse_command(&ip, &bf, out); const auto [cmd, l] = parse_command(&ip, &bf, out);
out << '\n'; out << '\n';