Skip to content

Commit 8208490

Browse files
committed
refactor: setup infrastructure of the new architecture (call stack, stack frames) in the vm
1 parent 1ce791b commit 8208490

2 files changed

Lines changed: 41 additions & 29 deletions

File tree

src/vm.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ VM vm;
1717
static void reset_stack()
1818
{
1919
vm.stack_top = vm.stack;
20+
vm.frame_count = 0;
2021
}
2122

2223
static void runtime_error(const char* format, ...)
@@ -27,8 +28,9 @@ static void runtime_error(const char* format, ...)
2728
va_end(args);
2829
fputs("\n", stderr);
2930

30-
size_t instruction = vm.ip - vm.chunk->code - 1;
31-
int line = vm.chunk->lines[instruction];
31+
CallFrame* frame = &vm.frames[vm.frame_count - 1];
32+
size_t instruction = frame->ip - frame->function->chunk.code - 1;
33+
int line = frame->function->chunk.lines[instruction];
3234
fprintf(stderr, "[Line %d] in script\n", line);
3335

3436
reset_stack();
@@ -88,9 +90,11 @@ static void concatenate()
8890

8991
static InterpretResult run()
9092
{
91-
#define READ_BYTE() (*vm.ip++)
92-
#define READ_CONSTANT() (vm.chunk->constants.values[READ_BYTE()])
93-
#define READ_SHORT() (vm.ip += 2, (u16)(vm.ip[-2] << 8) | vm.ip[-1])
93+
CallFrame* frame = &vm.frames[vm.frame_count - 1];
94+
95+
#define READ_BYTE() (*frame->ip++)
96+
#define READ_SHORT() (frame->ip += 2, (u16)(frame->ip[-2] << 8) | frame->ip[-1])
97+
#define READ_CONSTANT() (frame->function->chunk.constants.values[READ_BYTE()])
9498
#define READ_STRING() AS_STRING(READ_CONSTANT())
9599
#define BINARY_OP(value_type, op) \
96100
do \
@@ -116,7 +120,8 @@ static InterpretResult run()
116120
printf(" ]");
117121
}
118122
printf("\n");
119-
disassemble_instruction(vm.chunk, (int)(vm.ip - vm.chunk->code));
123+
disassemble_instruction(&frame->function->chunk,
124+
(int)(frame->ip - frame->function->chunk.code));
120125
#endif
121126
u8 instruction;
122127
switch (instruction = READ_BYTE())
@@ -142,13 +147,13 @@ static InterpretResult run()
142147
case OP_GET_LOCAL:
143148
{
144149
u8 slot = READ_BYTE();
145-
push(vm.stack[slot]);
150+
push(frame->slots[slot]);
146151
break;
147152
}
148153
case OP_SET_LOCAL:
149154
{
150155
u8 slot = READ_BYTE();
151-
vm.stack[slot] = peek(0);
156+
frame->slots[slot] = peek(0);
152157
break;
153158
}
154159
case OP_GET_GLOBAL:
@@ -241,20 +246,20 @@ static InterpretResult run()
241246
case OP_JUMP:
242247
{
243248
u16 offset = READ_SHORT();
244-
vm.ip += offset;
249+
frame->ip += offset;
245250
break;
246251
}
247252
case OP_JUMP_IF_FALSE:
248253
{
249254
u16 offset = READ_SHORT();
250255
if (is_falsey(peek(0)))
251-
vm.ip += offset;
256+
frame->ip += offset;
252257
break;
253258
}
254259
case OP_LOOP:
255260
{
256261
u16 offset = READ_SHORT();
257-
vm.ip -= offset;
262+
frame->ip -= offset;
258263
break;
259264
}
260265
case OP_RETURN:
@@ -274,17 +279,15 @@ InterpretResult interpret(char* source)
274279
Chunk chunk;
275280
init_chunk(&chunk);
276281

277-
if (!compile(source, &chunk))
278-
{
279-
free_chunk(&chunk);
282+
ObjFunction* function = compile(source, &chunk);
283+
if (function == NULL)
280284
return INTERPRET_COMPILE_ERROR;
281-
}
282-
283-
vm.chunk = &chunk;
284-
vm.ip = vm.chunk->code;
285285

286-
InterpretResult result = run();
286+
push(OBJ_VAL(function));
287+
CallFrame* frame = &vm.frames[vm.frame_count++];
288+
frame->function = function;
289+
frame->ip = function->chunk.code;
290+
frame->slots = vm.stack;
287291

288-
free_chunk(&chunk);
289-
return result;
292+
return run();
290293
}

src/vm.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,29 @@
33

44
#include "chunk.h"
55
#include "common.h"
6+
#include "object.h"
67
#include "table.h"
78
#include "value.h"
89

9-
#define STACK_MAX 256
10+
#define FRAMES_MAX 64
11+
#define STACK_MAX (FRAMES_MAX * UINT8_COUNT)
1012

1113
typedef struct
1214
{
13-
Chunk* chunk;
14-
u8* ip; // aka of Instrction Pointer
15-
Value stack[STACK_MAX];
16-
Value* stack_top;
17-
Table globals;
18-
Table strings; // for string interning just like (string pool in java)
19-
Obj* objects;
15+
ObjFunction* function;
16+
u8* ip;
17+
Value* slots;
18+
} CallFrame;
19+
20+
typedef struct
21+
{
22+
CallFrame frames[FRAMES_MAX];
23+
int frame_count;
24+
Value stack[STACK_MAX];
25+
Value* stack_top;
26+
Table globals;
27+
Table strings; // for string interning just like (string pool in java)
28+
Obj* objects;
2029
} VM;
2130

2231
typedef enum

0 commit comments

Comments
 (0)