changeset 78:abc6f3d644a4

Use Boehm-GC for garbage collection. Also make no_impl print on stderr rather than standard in and return a non-zero error code.
author Mike Pavone <pavone@retrodev.com>
date Sun, 15 Jul 2012 18:11:00 -0700
parents 8a9b96888b7d
children 7f635666c73d
files cbackend.js runtime/object.c runtime/progfoot.inc runtime/proghead.inc
diffstat 4 files changed, 17 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/cbackend.js	Sun Jul 15 13:13:06 2012 -0700
+++ b/cbackend.js	Sun Jul 15 18:11:00 2012 -0700
@@ -442,7 +442,7 @@
 		vars: {str: 'string *'},
 		lines: [
 			'str = (string *)make_object(&string_meta, NULL, 0);',
-			'str->data = malloc(12);',
+			'str->data = GC_MALLOC(12);',
 			'sprintf(str->data, "%d", self->num);',
 			'str->length = str->bytes = strlen(str->data);',
 			'return &(str->header);'
@@ -494,7 +494,7 @@
 		lines: [
 			'if (self->storage == self->size) {',
 			'	self->storage *= 2;',
-			'	tmp = realloc(self->data, self->storage * sizeof(object *));',
+			'	tmp = GC_REALLOC(self->data, self->storage * sizeof(object *));',
 			'	if (!tmp) {',
 			'		fputs("Failed to increase array size\\n", stderr);',
 			'		exit(1);',
@@ -577,7 +577,7 @@
 			'out = (string *)make_object(&string_meta, NULL, 0);',
 			'out->bytes = self->bytes + argb->bytes;',
 			'out->length = self->length + argb->length;',
-			'out->data = malloc(out->bytes+1);',
+			'out->data = GC_MALLOC_ATOMIC(out->bytes+1);',
 			'memcpy(out->data, self->data, self->bytes);',
 			'memcpy(out->data + self->bytes, argb->data, argb->bytes + 1);',
 			'return &(out->header);'
@@ -640,7 +640,7 @@
 			'filedes = va_arg(args, obj_int32 *);',
 			'size = va_arg(args, obj_int32 *);',
 			'str = (string *)make_object(&string_meta, NULL, 0);',
-			'str->data = malloc(size->num + 1);',
+			'str->data = GC_MALLOC_ATOMIC(size->num + 1);',
 			'str->length = str->bytes = read(filedes->num, str->data, size->num);',
 			'if (str->bytes < 0) { str->bytes = str->length = 0; }',
 			'str->data[str->bytes] = 0;',
@@ -854,7 +854,7 @@
 		}
 		forwarddec += '} lambda_' + mynum + '_env;\n'
 		
-		var myenvinit = '\tlambda_' + mynum + '_env * myenv = malloc(sizeof(lambda_' + mynum + '_env));\n';
+		var myenvinit = '\tlambda_' + mynum + '_env * myenv = GC_MALLOC(sizeof(lambda_' + mynum + '_env));\n';
 		if (this.symbols.needsParentEnv) {
 			myenvinit += '\tmyenv->parent = env;\n';
 		}
--- a/runtime/object.c	Sun Jul 15 13:13:06 2012 -0700
+++ b/runtime/object.c	Sun Jul 15 18:11:00 2012 -0700
@@ -2,11 +2,12 @@
 #include <stdarg.h>
 #include <stdlib.h>
 #include <stddef.h>
+#include <gc/gc.h>
 
 object * make_object(obj_meta * meta, void * parent, int num_props, ...)
 {
 	va_list args;
-	object * newobj = malloc(meta->size);
+	object * newobj = GC_MALLOC(meta->size);
 	newobj->meta = meta;
 	newobj->parent = parent;
 	
--- a/runtime/progfoot.inc	Sun Jul 15 13:13:06 2012 -0700
+++ b/runtime/progfoot.inc	Sun Jul 15 18:11:00 2012 -0700
@@ -1,14 +1,15 @@
 
 array * alloc_array(uint32_t num_els)
 {
-	array * arr = malloc(sizeof(array));
+	array * arr = GC_MALLOC(sizeof(array));
 	arr->header.meta = &array_meta;
 	arr->header.parent = NULL;
 	arr->storage = arr->size = num_els;
 	if (num_els < 4) {
 		arr->storage = 4;
 	}
-	arr->data = malloc(sizeof(object *) * arr->storage);
+	arr->data = GC_MALLOC(sizeof(object *) * arr->storage);
+	return arr;
 }
 
 object * make_array(uint32_t num_els, ...)
@@ -25,7 +26,7 @@
 
 object * make_lambda(void * env, closure_func func)
 {
-	lambda * ret = malloc(sizeof(lambda));
+	lambda * ret = GC_MALLOC(sizeof(lambda));
 	ret->header.meta = &lambda_meta;
 	ret->header.parent = NULL;
 	ret->env = env;
@@ -37,6 +38,7 @@
 {
 	int i;
 	string * arg;
+	GC_INIT();
 	array * arr = alloc_array(argc);
 	for (i = 0; i < argc; ++i) {
 		arg = (string *)make_object(&string_meta, NULL, 0);
--- a/runtime/proghead.inc	Sun Jul 15 13:13:06 2012 -0700
+++ b/runtime/proghead.inc	Sun Jul 15 18:11:00 2012 -0700
@@ -2,19 +2,15 @@
 #include <stddef.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <gc/gc.h>
 
 object * main_module;
 
 object * no_impl(uint32_t method_id, uint32_t num_args, object * self, va_list args)
 {
-	printf("method %d is not implemented on object %p\n", method_id, self);
-	printf("main_module %p\n", main_module);
-	exit(0);
+	fprintf(stderr, "method %d is not implemented on object %p\n", method_id, self);
+	fprintf(stderr, "main_module %p\n", main_module);
+	exit(1);
 	return NULL;
 }
-/*
-obj_meta lambda_meta = {
-	sizeof(closure), 
-	{no_impl, no_impl, no_impl, no_impl, no_impl, no_impl, no_impl, no_impl, 
-	 no_impl, no_impl, no_impl, no_impl, no_impl, no_impl, no_impl, no_impl}
-};*/
+