aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-26 16:26:25 +0200
committerBad Diode <bd@badd10de.dev>2021-10-26 16:26:25 +0200
commit26c5b458a067388da968465ba9e102a084bf7a2c (patch)
treeb753f30558e4cc43337c151c061e0400a1ab734c
parent6445d865ab391e3f705c41839df12f9e294eb840 (diff)
downloadbdl-26c5b458a067388da968465ba9e102a084bf7a2c.tar.gz
bdl-26c5b458a067388da968465ba9e102a084bf7a2c.zip
Add stack trace for function call debugging
-rwxr-xr-xsrc/bytecode/vm.h17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/bytecode/vm.h b/src/bytecode/vm.h
index 33582b1..3f59aff 100755
--- a/src/bytecode/vm.h
+++ b/src/bytecode/vm.h
@@ -60,6 +60,22 @@ vm_reset(VM *vm) {
60} 60}
61 61
62// Helper macros for a more clear VM switch. 62// Helper macros for a more clear VM switch.
63#ifdef DEBUG
64#define STACK_TRACE() \
65 fprintf(stderr, "stack trace:\n"); \
66 for (ssize_t i = array_size(vm->frames) - 1; i >= 0 ; i--) { \
67 CallFrame frame = vm->frames[i]; \
68 Chunk *chunk = frame.chunk; \
69 size_t instruction = vm->pc - chunk->code - 1; \
70 fprintf(stderr, "\t%-4ld -> ", i); \
71 fprintf(stderr, "%.*s",(int)array_size(chunk->name), chunk->name); \
72 fprintf(stderr, ":%ld:%ld\n", chunk->lines[instruction].line, chunk->lines[instruction].col); \
73 vm->pc = frame.rp; \
74 }
75#else
76#define STACK_TRACE()
77#endif
78
63#define RUNTIME_ERROR(ERR) \ 79#define RUNTIME_ERROR(ERR) \
64 error_push((Error){ \ 80 error_push((Error){ \
65 .type = ERR_TYPE_RUNTIME, \ 81 .type = ERR_TYPE_RUNTIME, \
@@ -67,6 +83,7 @@ vm_reset(VM *vm) {
67 .line = frame->chunk->lines[vm->pc - frame->chunk->code - 1].line, \ 83 .line = frame->chunk->lines[vm->pc - frame->chunk->code - 1].line, \
68 .col = frame->chunk->lines[vm->pc - frame->chunk->code - 1].col, \ 84 .col = frame->chunk->lines[vm->pc - frame->chunk->code - 1].col, \
69 }); \ 85 }); \
86 STACK_TRACE() \
70 return 87 return
71 88
72#define FIXNUM_ARITHMETIC_OP(OP) \ 89#define FIXNUM_ARITHMETIC_OP(OP) \