Explorar o código

free unused bytecode before starting jit, added hl_dyn_call

Nicolas Cannasse %!s(int64=9) %!d(string=hai) anos
pai
achega
e876439464
Modificáronse 6 ficheiros con 75 adicións e 70 borrados
  1. 45 40
      src/code.c
  2. 1 0
      src/hl.h
  3. 0 30
      src/hlc.h
  4. 1 0
      src/hlmodule.h
  5. 1 0
      src/main.c
  6. 27 0
      src/std/fun.c

+ 45 - 40
src/code.c

@@ -282,7 +282,7 @@ static void hl_read_opcode( hl_reader *r, hl_function *f, hl_opcode *o ) {
 				o->p1 = INDEX();
 				o->p2 = INDEX();
 				o->p3 = READ();
-				o->extra = (int*)hl_malloc(&r->code->alloc,sizeof(int) * o->p3);
+				o->extra = (int*)hl_malloc(&r->code->falloc,sizeof(int) * o->p3);
 				for(i=0;i<o->p3;i++)
 					o->extra[i] = INDEX();
 			}
@@ -292,7 +292,7 @@ static void hl_read_opcode( hl_reader *r, hl_function *f, hl_opcode *o ) {
 				int i;
 				o->p1 = UINDEX();
 				o->p2 = UINDEX();
-				o->extra = (int*)hl_malloc(&r->code->alloc,sizeof(int) * o->p2);
+				o->extra = (int*)hl_malloc(&r->code->falloc,sizeof(int) * o->p2);
 				for(i=0;i<o->p2;i++)
 					o->extra[i] = UINDEX();
 				o->p3 = UINDEX();
@@ -309,7 +309,7 @@ static void hl_read_opcode( hl_reader *r, hl_function *f, hl_opcode *o ) {
 			o->p1 = INDEX();
 			o->p2 = INDEX();
 			o->p3 = INDEX();
-			o->extra = (int*)hl_malloc(&r->code->alloc,sizeof(int) * size);
+			o->extra = (int*)hl_malloc(&r->code->falloc,sizeof(int) * size);
 			for(i=0;i<size;i++)
 				o->extra[i] = INDEX();
 		}
@@ -323,11 +323,11 @@ static void hl_read_function( hl_reader *r, hl_function *f ) {
 	f->findex = UINDEX();
 	f->nregs = UINDEX();
 	f->nops = UINDEX();
-	f->regs = (hl_type**)hl_malloc(&r->code->alloc, f->nregs * sizeof(hl_type*));
+	f->regs = (hl_type**)hl_malloc(&r->code->falloc, f->nregs * sizeof(hl_type*));
 	for(i=0;i<f->nregs;i++)
 		f->regs[i] = hl_get_type(r);
 	CHK_ERROR();
-	f->ops = (hl_opcode*)hl_malloc(&r->code->alloc, f->nops * sizeof(hl_opcode));
+	f->ops = (hl_opcode*)hl_malloc(&r->code->falloc, f->nops * sizeof(hl_opcode));
 	for(i=0;i<f->nops;i++)
 		hl_read_opcode(r, f, f->ops+i);
 }
@@ -369,41 +369,41 @@ static char **hl_read_strings( hl_reader *r, int nstrings, int **out_lens ) {
 static int *hl_read_debug_infos( hl_reader *r, int nops ) {
 	int curfile = -1, curline = 0;
 	hl_code *code = r->code;
-	int *debug = (int*)hl_malloc(&code->alloc, sizeof(int) * nops * 2);
-	int i = 0;	
-	while( i < nops ) {
-		int c = READ();
-		if( c & 1 ) {
-			c >>= 1;
-			curfile = (c << 8) | READ();
-			if( curfile >= code->ndebugfiles )
-				ERROR("Invalid debug file");
-		} else if( c & 2 ) {
-			int delta = c >> 6;
-			int count = (c >> 2) & 15;
-			if( i + count > nops )
-				ERROR("Outside range");
-			while( count-- ) {
-				debug[i<<1] = curfile;
-				debug[(i<<1)|1] = curline;
-				i++;
-			}
-			curline += delta;
-		} else if( c & 4 ) {
-			curline += c >> 3;
-			debug[i<<1] = curfile;
-			debug[(i<<1)|1] = curline;
-			i++;
-		} else {
-			unsigned char b2 = READ();
-			unsigned char b3 = READ();
-			curline = (c >> 3) | (b2 << 5) | (b3 << 13);
-			debug[i<<1] = curfile;
-			debug[(i<<1)|1] = curline;
-			i++;
-		}
-	}
-	return debug;
+	int *debug = (int*)hl_malloc(&code->alloc, sizeof(int) * nops * 2);
+	int i = 0;	
+	while( i < nops ) {
+		int c = READ();
+		if( c & 1 ) {
+			c >>= 1;
+			curfile = (c << 8) | READ();
+			if( curfile >= code->ndebugfiles )
+				ERROR("Invalid debug file");
+		} else if( c & 2 ) {
+			int delta = c >> 6;
+			int count = (c >> 2) & 15;
+			if( i + count > nops )
+				ERROR("Outside range");
+			while( count-- ) {
+				debug[i<<1] = curfile;
+				debug[(i<<1)|1] = curline;
+				i++;
+			}
+			curline += delta;
+		} else if( c & 4 ) {
+			curline += c >> 3;
+			debug[i<<1] = curfile;
+			debug[(i<<1)|1] = curline;
+			i++;
+		} else {
+			unsigned char b2 = READ();
+			unsigned char b3 = READ();
+			curline = (c >> 3) | (b2 << 5) | (b3 << 13);
+			debug[i<<1] = curfile;
+			debug[(i<<1)|1] = curline;
+			i++;
+		}
+	}
+	return debug;
 }
 
 hl_code *hl_code_read( const unsigned char *data, int size ) {
@@ -416,6 +416,7 @@ hl_code *hl_code_read( const unsigned char *data, int size ) {
 	hl_alloc_init(&alloc);
 	c = hl_zalloc(&alloc,sizeof(hl_code));
 	c->alloc = alloc;
+	hl_alloc_init(&c->falloc);
 	if( READ() != 'H' || READ() != 'L' || READ() != 'B' )
 		EXIT("Invalid header");
 	r->code = c;
@@ -479,3 +480,7 @@ hl_code *hl_code_read( const unsigned char *data, int size ) {
 	CHK_ERROR();
 	return c;
 }
+
+void hl_code_free( hl_code *c ) {
+	hl_free(&c->falloc);
+}

+ 1 - 0
src/hl.h

@@ -476,6 +476,7 @@ HL_API vclosure *hl_alloc_closure_ptr( hl_type *fullt, void *fvalue, void *ptr )
 HL_API vclosure *hl_make_fun_wrapper( vclosure *c, hl_type *to );
 HL_API void *hl_wrapper_call( void *value, void **args, vdynamic *ret );
 HL_API void *hl_dyn_call_obj( vdynamic *obj, hl_type *ft, int hfield, void **args, vdynamic *ret );
+HL_API vdynamic *hl_dyn_call( vclosure *c, vdynamic **args, int nargs );
 
 // ----------------------- ALLOC --------------------------------------------------
 

+ 0 - 30
src/hlc.h

@@ -58,36 +58,6 @@ static void hl_null_access() {
 	hl_error_msg(USTR("Null access"));
 }
 
-HL_API vdynamic *hl_call_method( vdynamic *c, varray *args );
-
-#define HLC_DYN_MAX_ARGS 9
-static vdynamic *hlc_dyn_call_args( vclosure *c, vdynamic **args, int nargs ) {
-	struct {
-		varray a;
-		vdynamic *args[HLC_DYN_MAX_ARGS+1];
-	} tmp;
-	vclosure ctmp;
-	int i = 0;
-	if( nargs > HLC_DYN_MAX_ARGS ) hl_error("Too many arguments");
-	tmp.a.t = &hlt_array;
-	tmp.a.at = &hlt_dyn;
-	tmp.a.size = nargs;
-	if( c->hasValue && c->t->fun->nargs >= 0 ) {
-		ctmp.t = c->t->fun->parent;
-		ctmp.hasValue = 0;
-		ctmp.fun = c->fun;
-		tmp.args[0] = hl_make_dyn(&c->value,ctmp.t->fun->args[0]);
-		tmp.a.size++;
-		for(i=0;i<nargs;i++)
-			tmp.args[i+1] = args[i];
-		c = &ctmp;
-	} else {
-		for(i=0;i<nargs;i++)
-			tmp.args[i] = args[i];
-	}
-	return hl_call_method((vdynamic*)c,&tmp.a);
-}
-
 #endif
 
 extern void *hlc_static_call(void *fun, hl_type *t, void **args, vdynamic *out);

+ 1 - 0
src/hlmodule.h

@@ -71,6 +71,7 @@ typedef struct {
 	hl_native*	natives;
 	hl_function*functions;
 	hl_alloc	alloc;
+	hl_alloc	falloc;
 } hl_code;
 
 typedef struct {

+ 1 - 0
src/main.c

@@ -98,6 +98,7 @@ int main(int argc, char *argv[]) {
 		return 2;
 	if( !hl_module_init(ctx.m) )
 		return 3;
+	hl_code_free(ctx.code);
 	hl_trap(trap, ctx.exc, on_exception);
 	hl_callback(ctx.m->functions_ptrs[ctx.m->code->entrypoint],ctx.code->functions[ctx.m->functions_indexes[ctx.m->code->entrypoint]].type,NULL,NULL);
 	hl_module_free(ctx.m);

+ 27 - 0
src/std/fun.c

@@ -175,6 +175,33 @@ HL_PRIM vdynamic* hl_call_method( vdynamic *c, varray *args ) {
 	return dret;
 }
 
+HL_PRIM vdynamic *hl_dyn_call( vclosure *c, vdynamic **args, int nargs ) {
+	struct {
+		varray a;
+		vdynamic *args[HL_MAX_ARGS+1];
+	} tmp;
+	vclosure ctmp;
+	int i = 0;
+	if( nargs > HL_MAX_ARGS ) hl_error("Too many arguments");
+	tmp.a.t = &hlt_array;
+	tmp.a.at = &hlt_dyn;
+	tmp.a.size = nargs;
+	if( c->hasValue && c->t->fun->nargs >= 0 ) {
+		ctmp.t = c->t->fun->parent;
+		ctmp.hasValue = 0;
+		ctmp.fun = c->fun;
+		tmp.args[0] = hl_make_dyn(&c->value,ctmp.t->fun->args[0]);
+		tmp.a.size++;
+		for(i=0;i<nargs;i++)
+			tmp.args[i+1] = args[i];
+		c = &ctmp;
+	} else {
+		for(i=0;i<nargs;i++)
+			tmp.args[i] = args[i];
+	}
+	return hl_call_method((vdynamic*)c,&tmp.a);
+}
+
 HL_PRIM void *hl_wrapper_call( void *_c, void **args, vdynamic *ret ) {
 	vclosure_wrapper *c = (vclosure_wrapper*)_c;
 	hl_type_fun *tfun = c->cl.t->fun;