retain/release for integers; don't retain on stack push
authorcassowarii <2374677+cassowarii@users.noreply.github.com>
Fri, 10 Jul 2026 06:51:36 +0000 (23:51 -0700)
committercassowarii <2374677+cassowarii@users.noreply.github.com>
Fri, 10 Jul 2026 06:51:36 +0000 (23:51 -0700)
Makefile
src/compile/ir.c
src/data/integer.c
src/data/integer.h
src/data/value.c
src/vm/exec.c

index e40a60d..5159e2d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -14,13 +14,16 @@ build/a.out: $(OBJECTS)
        @mkdir -p $(dir $@)
        $(CC) $(CFLAGS) -I$(SRC) $(OBJECTS) $(LIBS) -o $@
 
-.PHONY: parsedebug optimized dev
+.PHONY: parsedebug optimized dev profile
 parsedebug: CFLAGS += -DPARSEDEBUG
 parsedebug: clean build/a.out
 
 optimized: CFLAGS=-Wall -O3 -flto
 optimized: clean build/a.out
 
+profile: CFLAGS=-Wall -O3 -flto -pg
+profile: clean build/a.out
+
 dev: clean build/a.out
 
 $(OBJ)/%.o: $(SRC)/%.c
index bca110b..4503cd3 100644 (file)
@@ -8,10 +8,6 @@
 
 #include <stdarg.h>
 
-#define BY_LET 1
-#define BY_DEF 2
-#define BY_PARAM 3
-
 typedef struct varmapentry {
   const char *name;
   usize name_len;
index 5d75198..db70adb 100644 (file)
@@ -73,6 +73,20 @@ hInteger sbInteger_new(i64 value) {
   }
 }
 
+void sbInteger_retain(hInteger a) {
+  if (is_bigint(a)) {
+    bigint *big = find_bigint_for_handle(a);
+    big->gcinfo.refcount ++;
+  }
+}
+
+void sbInteger_release(hInteger a) {
+  if (is_bigint(a)) {
+    bigint *big = find_bigint_for_handle(a);
+    big->gcinfo.refcount --;
+  }
+}
+
 hInteger sbInteger_sum(hInteger a, hInteger b) {
   if (!is_bigint(a) && !is_bigint(b)) {
     return sbInteger_new(a + b);
index 84c4075..238d132 100644 (file)
@@ -11,6 +11,10 @@ void sbInteger_sys_deinit();
 
 hInteger sbInteger_new(i64 value);
 
+void sbInteger_retain(hInteger a);
+
+void sbInteger_release(hInteger a);
+
 hInteger sbInteger_sum(hInteger a, hInteger b);
 
 hInteger sbInteger_diff(hInteger a, hInteger b);
index 41c8402..4d77ddb 100644 (file)
@@ -3,6 +3,7 @@
 #include "data/string.h"
 #include "data/list.h"
 #include "data/hashtable.h"
+#include "data/integer.h"
 
 #define FLAG_NONINTRINSIC (1ULL << 63)
 
@@ -111,14 +112,28 @@ void sbV_retain(const hV *a) {
    * because they are trivially copiable. some classes (integer, string) need
    * to sometimes be retained but sometimes not, so we delegate to them to
    * figure out if they need to or not. */
-  if (a->type == IT_STRING) {
-    sbString_clone(a->string);
+  switch (a->type) {
+    case IT_STRING:
+      sbString_clone(a->string);
+      break;
+    case IT_INTEGER:
+      sbInteger_retain(a->string);
+      break;
+    default:
+      /* nothing */
   }
 }
 
 void sbV_release(const hV *a) {
-  if (a->type == IT_STRING) {
-    sbString_release(a->string);
+  switch (a->type) {
+    case IT_STRING:
+      sbString_release(a->string);
+      break;
+    case IT_INTEGER:
+      sbInteger_release(a->string);
+      break;
+    default:
+      /* nothing */
   }
 }
 
index 3989faa..c18800b 100644 (file)
@@ -182,7 +182,6 @@ void store_local(hVm vm, usize local_index, const hV *value) {
 }
 
 void push_stack(hVm vm, hV *value) {
-  sbV_retain(value);
   *(hV**)vm->vsp = value;
   vm->vsp += sizeof(hV*);
   vm->xsp += sizeof(hV);
@@ -190,7 +189,6 @@ void push_stack(hVm vm, hV *value) {
 
 void push_stack_immediate(hVm vm, const hV *value) {
   /* save it on our own stack */
-  sbV_retain(value);
   *(hV*)vm->xsp = *value;
   *(hV**)vm->vsp = (hV*)vm->xsp;
   vm->vsp += sizeof(hV*);
@@ -200,16 +198,12 @@ void push_stack_immediate(hVm vm, const hV *value) {
 hV *pop_stack(hVm vm) {
   vm->vsp -= sizeof(hV*);
   vm->xsp -= sizeof(hV);
-  sbV_release(*(hV**)vm->vsp);
   return *(hV**)vm->vsp;
 }
 
 hV *npop_stack(hVm vm, usize count) {
   vm->vsp -= count * sizeof(hV*);
   vm->xsp -= count * sizeof(hV);
-  for (usize i = 0; i < count; i++) {
-    sbV_release(((hV**)vm->vsp)[i]);
-  }
   return *(hV**)vm->vsp;
 }