diff options
author | Bad Diode <bd@badd10de.dev> | 2021-10-24 09:26:25 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2021-10-24 09:26:25 +0200 |
commit | f372586069ea0a92db65bc90cf844c1a35187430 (patch) | |
tree | b6b9c9cf27844505a3863567aeff057d339ad87d /src/bytecode/chunk.h | |
parent | 8aa57dd5a87b454ce99a336ed86a6bd4d6f77c1e (diff) | |
download | bdl-f372586069ea0a92db65bc90cf844c1a35187430.tar.gz bdl-f372586069ea0a92db65bc90cf844c1a35187430.zip |
Remove constant duplication on compile time
Diffstat (limited to 'src/bytecode/chunk.h')
-rwxr-xr-x | src/bytecode/chunk.h | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/src/bytecode/chunk.h b/src/bytecode/chunk.h index aafa0ec..5fbc000 100755 --- a/src/bytecode/chunk.h +++ b/src/bytecode/chunk.h | |||
@@ -34,9 +34,7 @@ chunk_free(Chunk *chunk) { | |||
34 | array_free(chunk->code); | 34 | array_free(chunk->code); |
35 | for (size_t i = 0; i < array_size(chunk->constants); i++) { | 35 | for (size_t i = 0; i < array_size(chunk->constants); i++) { |
36 | Object obj = chunk->constants[i]; | 36 | Object obj = chunk->constants[i]; |
37 | if (IS_STRING(obj) || IS_SYMBOL(obj)) { | 37 | object_free(obj); |
38 | array_free(obj.text); | ||
39 | } | ||
40 | } | 38 | } |
41 | array_free(chunk->constants); | 39 | array_free(chunk->constants); |
42 | array_free(chunk->lines); | 40 | array_free(chunk->lines); |
@@ -52,12 +50,12 @@ add_code(Chunk *chunk, u8 byte, size_t line, size_t col) { | |||
52 | 50 | ||
53 | size_t | 51 | size_t |
54 | add_constant(Chunk *chunk, Object obj) { | 52 | add_constant(Chunk *chunk, Object obj) { |
55 | // FIXME?: Since we are using a single byte to store constant indices, we | ||
56 | // can only have 256 stored constants. If we need more we may need to add | ||
57 | // another instruction OP_CONSTANT_16 to have at least two bytes for | ||
58 | // constants. Alternatively, we could make that the default. Either way, for | ||
59 | // now it's fine. | ||
60 | size_t pos = array_size(chunk->constants); | 53 | size_t pos = array_size(chunk->constants); |
54 | for (size_t i = 0; i < pos; i++) { | ||
55 | if (object_equal(obj, chunk->constants[i])) { | ||
56 | return i; | ||
57 | } | ||
58 | } | ||
61 | array_push(chunk->constants, obj); | 59 | array_push(chunk->constants, obj); |
62 | return pos; | 60 | return pos; |
63 | } | 61 | } |