Skip to content

Commit a0a9bb4

Browse files
authored
Fix GC bugs
1 parent 329b871 commit a0a9bb4

File tree

4 files changed

+60
-56
lines changed

4 files changed

+60
-56
lines changed

cbplibumka/libumka.depend

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
"umka_types.h"
153153
"umka_ident.h"
154154

155-
1589677817 source:c:\users\vasiliy\desktop\umka-lang-master\src\umka_vm.c
155+
1589727538 source:c:\users\vasiliy\desktop\umka-lang-master\src\umka_vm.c
156156
<stdio.h>
157157
<stdlib.h>
158158
<string.h>

cbplibumka/libumka.layout

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,54 @@
22
<CodeBlocks_layout_file>
33
<FileVersion major="1" minor="0" />
44
<ActiveTarget name="Debug" />
5-
<File name="..\src\umka_decl.c" open="1" top="0" tabpos="15" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
6-
<Cursor>
7-
<Cursor1 position="13556" topLine="43" />
8-
</Cursor>
9-
</File>
10-
<File name="..\src\umka_vm.c" open="1" top="1" tabpos="21" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
11-
<Cursor>
12-
<Cursor1 position="22022" topLine="297" />
13-
</Cursor>
14-
</File>
15-
<File name="..\src\umka_const.c" open="1" top="0" tabpos="14" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
5+
<File name="..\src\umka_expr.c" open="1" top="0" tabpos="13" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
166
<Cursor>
17-
<Cursor1 position="4921" topLine="113" />
7+
<Cursor1 position="678" topLine="19" />
188
</Cursor>
199
</File>
20-
<File name="..\src\umka_lexer.h" open="1" top="0" tabpos="11" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
10+
<File name="..\src\umka_gen.c" open="1" top="0" tabpos="10" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
2111
<Cursor>
22-
<Cursor1 position="143" topLine="99" />
12+
<Cursor1 position="5922" topLine="222" />
2313
</Cursor>
2414
</File>
25-
<File name="..\src\umka_expr.c" open="1" top="0" tabpos="13" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
15+
<File name="..\src\umka_stmt.h" open="1" top="0" tabpos="17" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
2616
<Cursor>
27-
<Cursor1 position="1168" topLine="36" />
17+
<Cursor1 position="94" topLine="0" />
2818
</Cursor>
2919
</File>
3020
<File name="..\src\umka_api.c" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
3121
<Cursor>
3222
<Cursor1 position="1815" topLine="9" />
3323
</Cursor>
3424
</File>
35-
<File name="..\src\umka_gen.c" open="1" top="0" tabpos="10" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
25+
<File name="..\src\umka_const.h" open="1" top="0" tabpos="18" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
3626
<Cursor>
37-
<Cursor1 position="1493" topLine="65" />
27+
<Cursor1 position="269" topLine="0" />
3828
</Cursor>
3929
</File>
40-
<File name="..\src\umka_stmt.h" open="1" top="0" tabpos="17" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
30+
<File name="..\src\umka_types.h" open="1" top="0" tabpos="19" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
4131
<Cursor>
42-
<Cursor1 position="108" topLine="0" />
32+
<Cursor1 position="1857" topLine="75" />
4333
</Cursor>
4434
</File>
4535
<File name="..\src\umka_common.h" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
4636
<Cursor>
4737
<Cursor1 position="1289" topLine="87" />
4838
</Cursor>
4939
</File>
50-
<File name="..\src\umka_types.h" open="1" top="0" tabpos="19" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
51-
<Cursor>
52-
<Cursor1 position="2393" topLine="89" />
53-
</Cursor>
54-
</File>
55-
<File name="..\src\umka_compiler.h" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
56-
<Cursor>
57-
<Cursor1 position="792" topLine="21" />
58-
</Cursor>
59-
</File>
6040
<File name="..\src\umka_lexer.c" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
6141
<Cursor>
6242
<Cursor1 position="2349" topLine="128" />
6343
</Cursor>
6444
</File>
6545
<File name="..\src\umka_vm.h" open="1" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
6646
<Cursor>
67-
<Cursor1 position="2935" topLine="129" />
47+
<Cursor1 position="897" topLine="18" />
6848
</Cursor>
6949
</File>
70-
<File name="..\src\umka_const.h" open="1" top="0" tabpos="18" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
50+
<File name="..\src\umka_compiler.h" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
7151
<Cursor>
72-
<Cursor1 position="269" topLine="0" />
52+
<Cursor1 position="792" topLine="21" />
7353
</Cursor>
7454
</File>
7555
<File name="..\src\umka_runtime.c" open="1" top="0" tabpos="8" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
@@ -84,27 +64,47 @@
8464
</File>
8565
<File name="..\src\umka_stmt.c" open="1" top="0" tabpos="9" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
8666
<Cursor>
87-
<Cursor1 position="14542" topLine="486" />
67+
<Cursor1 position="188" topLine="4" />
8868
</Cursor>
8969
</File>
90-
<File name="..\src\umka_common.c" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
70+
<File name="..\src\umka_gen.h" open="1" top="0" tabpos="12" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
9171
<Cursor>
92-
<Cursor1 position="2297" topLine="94" />
72+
<Cursor1 position="1077" topLine="42" />
9373
</Cursor>
9474
</File>
95-
<File name="..\src\umka_gen.h" open="1" top="0" tabpos="12" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
75+
<File name="..\src\umka_const.c" open="1" top="0" tabpos="14" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
9676
<Cursor>
97-
<Cursor1 position="1489" topLine="42" />
77+
<Cursor1 position="4921" topLine="113" />
9878
</Cursor>
9979
</File>
10080
<File name="..\src\umka_types.c" open="1" top="0" tabpos="20" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
10181
<Cursor>
102-
<Cursor1 position="5590" topLine="209" />
82+
<Cursor1 position="3164" topLine="128" />
83+
</Cursor>
84+
</File>
85+
<File name="..\src\umka_common.c" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
86+
<Cursor>
87+
<Cursor1 position="4774" topLine="202" />
88+
</Cursor>
89+
</File>
90+
<File name="..\src\umka_decl.c" open="1" top="0" tabpos="15" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
91+
<Cursor>
92+
<Cursor1 position="13821" topLine="459" />
93+
</Cursor>
94+
</File>
95+
<File name="..\src\umka_vm.c" open="1" top="1" tabpos="21" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
96+
<Cursor>
97+
<Cursor1 position="9481" topLine="324" />
10398
</Cursor>
10499
</File>
105100
<File name="..\src\umka_compiler.c" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
106101
<Cursor>
107102
<Cursor1 position="1411" topLine="24" />
108103
</Cursor>
109104
</File>
105+
<File name="..\src\umka_lexer.h" open="1" top="0" tabpos="11" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
106+
<Cursor>
107+
<Cursor1 position="143" topLine="99" />
108+
</Cursor>
109+
</File>
110110
</CodeBlocks_layout_file>

cbpumka/umka.layout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</File>
1010
<File name="..\src\umka.c" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
1111
<Cursor>
12-
<Cursor1 position="134" topLine="6" />
12+
<Cursor1 position="356" topLine="6" />
1313
</Cursor>
1414
</File>
1515
</CodeBlocks_layout_file>

src/umka_vm.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,8 @@ static bool chunkTryDecCnt(HeapChunks *chunks, void *ptr)
190190
}
191191

192192

193-
// Virtual machine
194-
195193
// Static copies of some external functions that allow inlining
194+
196195
static int alignRuntime(int size, int alignment)
197196
{
198197
return ((size + (alignment - 1)) / alignment) * alignment;
@@ -206,6 +205,8 @@ static bool typeGarbageCollectedRuntime(Type *type)
206205
}
207206

208207

208+
// Virtual machine
209+
209210
void vmInit(VM *vm, Instruction *code, int stackSize, ErrorFunc error)
210211
{
211212
vm->fiber = malloc(sizeof(Fiber));
@@ -333,21 +334,21 @@ static void doBasicChangeRefCnt(Fiber *fiber, HeapChunks *chunks, void *ptr, Typ
333334
}
334335

335336
newExtraRefCnt = chunk->refCnt - 1;
336-
}
337337

338-
if (0 && ptr && typeGarbageCollectedRuntime(type->base))
339-
{
340-
void *data = ptr;
341-
if (type->base->kind == TYPE_PTR)
342-
data = *(void **)data;
338+
if (ptr && typeGarbageCollectedRuntime(type->base))
339+
{
340+
void *data = ptr;
341+
if (type->base->kind == TYPE_PTR)
342+
data = *(void **)data;
343343

344-
doBasicChangeRefCnt(fiber, chunks, data, type->base, newExtraRefCnt, false, tokKind, error);
345-
}
344+
doBasicChangeRefCnt(fiber, chunks, data, type->base, newExtraRefCnt, false, tokKind, error);
345+
}
346346

347-
if (!root && chunk && chunk->extraRefCnt > 0)
348-
chunk->extraRefCnt--;
347+
if (!root && chunk->extraRefCnt > 0)
348+
chunk->extraRefCnt--;
349349

350-
chunkTryDecCnt(chunks, ptr);
350+
chunkTryDecCnt(chunks, ptr);
351+
}
351352
}
352353
break;
353354
}
@@ -558,12 +559,15 @@ static void doBuiltinMakeFrom(Fiber *fiber, HeapChunks *chunks)
558559

559560

560561
// fn append(array: [var] type, item: ^type): [var] type
561-
static void doBuiltinAppend(Fiber *fiber, HeapChunks *chunks)
562+
static void doBuiltinAppend(Fiber *fiber, HeapChunks *chunks, ErrorFunc error)
562563
{
563564
DynArray *result = (fiber->top++)->ptrVal;
564565
void *item = (fiber->top++)->ptrVal;
565566
DynArray *array = (fiber->top++)->ptrVal;
566567

568+
if (!array->data)
569+
error("Dynamic array is not initialized");
570+
567571
result->len = array->len + 1;
568572
result->itemSize = array->itemSize;
569573
result->data = chunkAdd(chunks, result->len * result->itemSize)->ptr;
@@ -1082,7 +1086,7 @@ static void doCallBuiltin(Fiber *fiber, Fiber **newFiber, HeapChunks *chunks, Er
10821086
case BUILTIN_NEW: fiber->top->ptrVal = chunkAdd(chunks, fiber->top->intVal)->ptr; break;
10831087
case BUILTIN_MAKE: doBuiltinMake(fiber, chunks); break;
10841088
case BUILTIN_MAKEFROM: doBuiltinMakeFrom(fiber, chunks); break;
1085-
case BUILTIN_APPEND: doBuiltinAppend(fiber, chunks); break;
1089+
case BUILTIN_APPEND: doBuiltinAppend(fiber, chunks, error); break;
10861090
case BUILTIN_LEN: doBuiltinLen(fiber, error); break;
10871091
case BUILTIN_SIZEOF: error("Illegal instruction"); return; // Done at compile time
10881092

0 commit comments

Comments
 (0)