aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-22 11:24:09 +0200
committerBad Diode <bd@badd10de.dev>2021-10-22 11:24:09 +0200
commitab7d7c155fb1bec5eed8f97462fbb656ea27dbb5 (patch)
treec0ab41a9078a67aa1fa9aa25a69942329200ea32
parentf4113cbcdc192b23f9b6e5e14b0a3e4afac35272 (diff)
downloadbdl-ab7d7c155fb1bec5eed8f97462fbb656ea27dbb5.tar.gz
bdl-ab7d7c155fb1bec5eed8f97462fbb656ea27dbb5.zip
Add VM structure and fix AdressSanitizer bugs
-rw-r--r--src/bytecode/chunk.h35
-rw-r--r--src/bytecode/debug.h26
-rw-r--r--src/bytecode/main.c39
-rw-r--r--src/bytecode/vm.h27
4 files changed, 85 insertions, 42 deletions
diff --git a/src/bytecode/chunk.h b/src/bytecode/chunk.h
index 6157057..e3c7383 100644
--- a/src/bytecode/chunk.h
+++ b/src/bytecode/chunk.h
@@ -15,20 +15,39 @@ typedef struct Chunk {
15 LineInfo *lines; 15 LineInfo *lines;
16} Chunk; 16} Chunk;
17 17
18void add_code(Chunk chunk, u8 byte, size_t line, size_t col); 18Chunk * chunk_init(void);
19size_t add_constant(Chunk chunk, Object obj); 19void add_code(Chunk *chunk, u8 byte, size_t line, size_t col);
20size_t add_constant(Chunk *chunk, Object obj);
21void chunk_free(Chunk *chunk);
22
23Chunk *
24chunk_init(void) {
25 Chunk *chunk = malloc(sizeof(Chunk));
26 array_init(chunk->code, 0);
27 array_init(chunk->constants, 0);
28 array_init(chunk->lines, 0);
29 return chunk;
30}
31
32void
33chunk_free(Chunk *chunk) {
34 array_free(chunk->code);
35 array_free(chunk->constants);
36 array_free(chunk->lines);
37 free(chunk);
38}
20 39
21void 40void
22add_code(Chunk chunk, u8 byte, size_t line, size_t col) { 41add_code(Chunk *chunk, u8 byte, size_t line, size_t col) {
23 array_push(chunk.code, byte); 42 array_push(chunk->code, byte);
24 LineInfo info = (LineInfo){line, col}; 43 LineInfo info = (LineInfo){line, col};
25 array_push(chunk.lines, info); 44 array_push(chunk->lines, info);
26} 45}
27 46
28size_t 47size_t
29add_constant(Chunk chunk, Object obj) { 48add_constant(Chunk *chunk, Object obj) {
30 size_t pos = array_size(chunk.constants); 49 size_t pos = array_size(chunk->constants);
31 array_push(chunk.constants, obj); 50 array_push(chunk->constants, obj);
32 return pos; 51 return pos;
33} 52}
34 53
diff --git a/src/bytecode/debug.h b/src/bytecode/debug.h
index ceedfbf..c891b77 100644
--- a/src/bytecode/debug.h
+++ b/src/bytecode/debug.h
@@ -3,47 +3,47 @@
3 3
4#include "chunk.h" 4#include "chunk.h"
5 5
6void disassemble_chunk(Chunk chunk, const char *name); 6void disassemble_chunk(Chunk *chunk, const char *name);
7size_t disassemble_instruction(Chunk chunk, size_t offset); 7size_t disassemble_instruction(Chunk *chunk, size_t offset);
8 8
9void 9void
10disassemble_chunk(Chunk chunk, const char *name) { 10disassemble_chunk(Chunk *chunk, const char *name) {
11 printf("== %s ==\n", name); 11 printf("== %s ==\n", name);
12 printf("code:\n"); 12 printf("code:\n");
13 size_t offset = 0; 13 size_t offset = 0;
14 while (offset < array_size(chunk.code)) { 14 while (offset < array_size(chunk->code)) {
15 offset = disassemble_instruction(chunk, offset); 15 offset = disassemble_instruction(chunk, offset);
16 } 16 }
17 printf("\nconstants:\n"); 17 printf("\nconstants:\n");
18 offset = 0; 18 offset = 0;
19 while (offset < array_size(chunk.constants)) { 19 while (offset < array_size(chunk->constants)) {
20 printf("\t%04ld -> ", offset); 20 printf("\t%04ld -> ", offset);
21 display(chunk.constants[offset]); 21 display(chunk->constants[offset]);
22 printf("\n"); 22 printf("\n");
23 offset++; 23 offset++;
24 } 24 }
25} 25}
26 26
27size_t 27size_t
28disassemble_instruction(Chunk chunk, size_t offset) { 28disassemble_instruction(Chunk *chunk, size_t offset) {
29 printf("\t%04ld ", offset); 29 printf("\t%04ld ", offset);
30 if (offset > 0 30 if (offset > 0
31 && chunk.lines[offset].line == chunk.lines[offset - 1].line 31 && chunk->lines[offset].line == chunk->lines[offset - 1].line
32 && chunk.lines[offset].col == chunk.lines[offset - 1].col) { 32 && chunk->lines[offset].col == chunk->lines[offset - 1].col) {
33 printf("%4s|%-4s ", " ", " "); 33 printf("%4s|%-4s ", " ", " ");
34 } else { 34 } else {
35 printf("%4ld:%-4ld ", chunk.lines[offset].line, chunk.lines[offset].col); 35 printf("%4ld:%-4ld ", chunk->lines[offset].line, chunk->lines[offset].col);
36 } 36 }
37 u8 instruction = chunk.code[offset]; 37 u8 instruction = chunk->code[offset];
38 switch (instruction) { 38 switch (instruction) {
39 case OP_RETURN: { 39 case OP_RETURN: {
40 printf("OP_RETURN\n"); 40 printf("OP_RETURN\n");
41 return offset + 1; 41 return offset + 1;
42 } break; 42 } break;
43 case OP_CONSTANT: { 43 case OP_CONSTANT: {
44 u8 constant = chunk.code[offset + 1]; 44 u8 constant = chunk->code[offset + 1];
45 printf("%-16s %4d (", "OP_CONSTANT", constant); 45 printf("%-16s %4d (", "OP_CONSTANT", constant);
46 display(chunk.constants[constant]); 46 display(chunk->constants[constant]);
47 printf(")\n"); 47 printf(")\n");
48 return offset + 2; 48 return offset + 2;
49 } break; 49 } break;
diff --git a/src/bytecode/main.c b/src/bytecode/main.c
index c994e08..ca1f441 100644
--- a/src/bytecode/main.c
+++ b/src/bytecode/main.c
@@ -4,9 +4,8 @@
4#include <stdlib.h> 4#include <stdlib.h>
5#include <string.h> 5#include <string.h>
6 6
7#include "types.h" 7#include "vm.h"
8#include "chunk.h" 8
9#include "darray.h"
10#include "ops.h" 9#include "ops.h"
11#include "debug.h" 10#include "debug.h"
12#include "errors.c" 11#include "errors.c"
@@ -14,9 +13,16 @@
14#include "read_line.c" 13#include "read_line.c"
15#include "string_view.c" 14#include "string_view.c"
16 15
16static VM vm;
17
17void 18void
18init(void) { 19init(void) {
19 // STUB 20 vm = vm_init();
21}
22
23void
24halt(void) {
25 vm_free(vm);
20} 26}
21 27
22void 28void
@@ -27,26 +33,14 @@ process_source(const StringView *source) {
27 return; 33 return;
28 } 34 }
29 35
30 // Test chunks and debugging utilities.
31 // Initialize chunk.
32 Chunk chunk = {0};
33 array_init(chunk.code, 0);
34 array_init(chunk.constants, 0);
35 array_init(chunk.lines, 0);
36
37 // Push some test instructions. 36 // Push some test instructions.
38 size_t const_idx = add_constant(chunk, 7); 37 size_t const_idx = add_constant(vm.chunk, 7);
39 add_code(chunk, OP_CONSTANT, 1, 1); 38 add_code(vm.chunk, OP_CONSTANT, 1, 1);
40 add_code(chunk, const_idx, 1, 1); 39 add_code(vm.chunk, const_idx, 1, 1);
41 add_code(chunk, OP_RETURN, 1, 1); 40 add_code(vm.chunk, OP_RETURN, 1, 1);
42 41
43 // Disassemble the chunk. 42 // Disassemble the chunk.
44 disassemble_chunk(chunk, "test chunk"); 43 disassemble_chunk(vm.chunk, "test chunk");
45
46 // Free chunk.
47 array_free(chunk.code);
48 array_free(chunk.constants);
49 array_free(chunk.lines);
50 44
51 array_free(tokens); 45 array_free(tokens);
52} 46}
@@ -183,6 +177,7 @@ main(int argc, char *argv[]) {
183 case 'i': { 177 case 'i': {
184 // Interactive mode. 178 // Interactive mode.
185 run_repl(); 179 run_repl();
180 halt();
186 return EXIT_SUCCESS; 181 return EXIT_SUCCESS;
187 } break; 182 } break;
188 default: { 183 default: {
@@ -195,6 +190,7 @@ main(int argc, char *argv[]) {
195 // Run from stdin. 190 // Run from stdin.
196 if (optind == argc) { 191 if (optind == argc) {
197 run_stdin(); 192 run_stdin();
193 halt();
198 return EXIT_SUCCESS; 194 return EXIT_SUCCESS;
199 } 195 }
200 196
@@ -205,5 +201,6 @@ main(int argc, char *argv[]) {
205 optind++; 201 optind++;
206 } 202 }
207 203
204 halt();
208 return EXIT_SUCCESS; 205 return EXIT_SUCCESS;
209} 206}
diff --git a/src/bytecode/vm.h b/src/bytecode/vm.h
new file mode 100644
index 0000000..36547cb
--- /dev/null
+++ b/src/bytecode/vm.h
@@ -0,0 +1,27 @@
1#ifndef BDL_VM_H
2#define BDL_VM_H
3
4#include "types.h"
5#include "chunk.h"
6
7typedef struct VM {
8 Chunk *chunk;
9} VM;
10
11VM vm_init(void);
12void vm_free(VM vm);
13
14VM
15vm_init(void) {
16 VM vm = {
17 .chunk = chunk_init(),
18 };
19 return vm;
20}
21
22void
23vm_free(VM vm) {
24 chunk_free(vm.chunk);
25}
26
27#endif // BDL_VM_H