aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-24 09:26:25 +0200
committerBad Diode <bd@badd10de.dev>2021-10-24 09:26:25 +0200
commitf372586069ea0a92db65bc90cf844c1a35187430 (patch)
treeb6b9c9cf27844505a3863567aeff057d339ad87d /src/bytecode
parent8aa57dd5a87b454ce99a336ed86a6bd4d6f77c1e (diff)
downloadbdl-f372586069ea0a92db65bc90cf844c1a35187430.tar.gz
bdl-f372586069ea0a92db65bc90cf844c1a35187430.zip
Remove constant duplication on compile time
Diffstat (limited to 'src/bytecode')
-rwxr-xr-xsrc/bytecode/chunk.h14
-rwxr-xr-xsrc/bytecode/objects.h38
2 files changed, 44 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
53size_t 51size_t
54add_constant(Chunk *chunk, Object obj) { 52add_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}
diff --git a/src/bytecode/objects.h b/src/bytecode/objects.h
index 17809d5..fc5e069 100755
--- a/src/bytecode/objects.h
+++ b/src/bytecode/objects.h
@@ -121,4 +121,42 @@ display(Object obj) {
121 return; 121 return;
122} 122}
123 123
124void
125object_free(Object obj) {
126 if (IS_STRING(obj) || IS_SYMBOL(obj)) {
127 array_free(obj.text);
128 }
129}
130
131bool
132object_equal(Object a, Object b) {
133 if (a.type != b.type) {
134 return false;
135 }
136 switch (a.type) {
137 case OBJ_TYPE_TRUE:
138 case OBJ_TYPE_FALSE: {
139 return true;
140 } break;
141 case OBJ_TYPE_FIXNUM: {
142 return a.fixnum == b.fixnum;
143 } break;
144 case OBJ_TYPE_SYMBOL:
145 case OBJ_TYPE_STRING: {
146 if (array_size(a.text) != array_size(b.text)) {
147 return false;
148 }
149 for (size_t i = 0; i < array_size(a.text); i++) {
150 if (a.text[i] != b.text[i]) {
151 return false;
152 }
153 }
154 } break;
155 default: {
156 return false;
157 } break;
158 }
159 return true;
160}
161
124#endif // BDL_OBJECTS_H 162#endif // BDL_OBJECTS_H