2
0
Эх сурвалжийг харах

make sure to have all values starting with hl_type**

Nicolas Cannasse 10 жил өмнө
parent
commit
c57d10eba7
5 өөрчлөгдсөн 67 нэмэгдсэн , 48 устгасан
  1. 4 1
      src/alloc.c
  2. 6 4
      src/code.c
  3. 15 13
      src/hl.h
  4. 27 28
      src/jit.c
  5. 15 2
      src/module.c

+ 4 - 1
src/alloc.c

@@ -126,7 +126,7 @@ void hl_free_executable_memory( void *c ) {
 #endif
 }
 
-vdynamic *hl_alloc_dynamic( hl_type *t ) {
+vdynamic *hl_alloc_dynamic( hl_type **t ) {
 	vdynamic *d = (vdynamic*)malloc(sizeof(vdynamic));
 	d->t = t;
 	d->v.ptr = NULL;
@@ -148,6 +148,7 @@ vobj *hl_alloc_obj( hl_module *m, hl_type *t ) {
 
 vclosure *hl_alloc_closure_void( hl_module *m, int_val fid ) {
 	vclosure *c = (vclosure*)malloc(sizeof(vclosure));
+	c->t = &m->code->functions[m->functions_indexes[fid]].type;
 	c->fun = m->functions_ptrs[fid];
 	c->bits = 0;
 	return c;
@@ -155,6 +156,7 @@ vclosure *hl_alloc_closure_void( hl_module *m, int_val fid ) {
 
 vclosure *hl_alloc_closure_i32( hl_module *m, int_val fid, int v32 ) {
 	vclosure *c = (vclosure*)malloc(sizeof(vclosure));
+	c->t = &m->code->functions[m->functions_indexes[fid]].type;
 	c->fun = m->functions_ptrs[fid];
 	c->bits = CL_HAS_V32;
 	c->v32 = v32;
@@ -163,6 +165,7 @@ vclosure *hl_alloc_closure_i32( hl_module *m, int_val fid, int v32 ) {
 
 vclosure *hl_alloc_closure_i64( hl_module *m, int_val fid, int_val v64 ) {
 	vclosure *c = (vclosure*)malloc(sizeof(vclosure));
+	c->t = &m->code->functions[m->functions_indexes[fid]].type;
 	c->fun = m->functions_ptrs[fid];
 	c->bits = CL_HAS_V64;
 	c->v64 = v64;

+ 6 - 4
src/code.c

@@ -143,10 +143,6 @@ static int hl_get_global( hl_reader *r ) {
 
 static void hl_read_type( hl_reader *r, hl_type *t ) {
 	t->kind = READ();
-	if( t->kind >= HLAST ) {
-		ERROR("Invalid type");
-		return;
-	}
 	switch( t->kind ) {
 	case HFUN:
 		{
@@ -186,7 +182,13 @@ static void hl_read_type( hl_reader *r, hl_type *t ) {
 				p->pindex = INDEX();
 			}
 		}
+		break;
+	case HDYN | 0x80:
+		t->kind = HDYN;
+		t->dyn = hl_get_type(r);
+		break;
 	default:
+		if( t->kind >= HLAST ) ERROR("Invalid type");
 		break;
 	}
 }

+ 15 - 13
src/hl.h

@@ -84,7 +84,7 @@ typedef enum {
 	HF32	= 3,
 	HF64	= 4,
 	HBOOL	= 5,
-	HANY	= 6,
+	HDYN	= 6,
 	HFUN	= 7,
 	HOBJ	= 8,
 	// ---------
@@ -127,6 +127,7 @@ struct hl_type {
 	union {
 		hl_type_fun *fun;
 		hl_type_obj *obj;
+		hl_type	*dyn;
 	};
 };
 
@@ -182,6 +183,7 @@ typedef struct {
 	int *globals_indexes;
 	unsigned char *globals_data;
 	void **functions_ptrs;
+	int *functions_indexes;
 	void *jit_code;
 } hl_module;
 
@@ -210,6 +212,7 @@ hl_runtime_obj *hl_get_obj_proto( hl_module *m, hl_type *ot );
 
 jit_ctx *hl_jit_alloc();
 void hl_jit_free( jit_ctx *ctx );
+void hl_jit_init( jit_ctx *ctx, hl_module *m );
 int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f );
 void *hl_jit_code( jit_ctx *ctx, hl_module *m );
 
@@ -218,21 +221,19 @@ void *hl_jit_code( jit_ctx *ctx, hl_module *m );
 typedef struct vobj vobj;
 typedef struct vclosure vclosure;
 
-typedef union {
-	int i;
-	float f;
-	double d;
-	vobj *o;
-	vclosure *c;
-	void *ptr;
-} value;
-
 typedef struct {
-	hl_type *t;
+	hl_type **t;
 #	ifndef HL_64
 	int __pad; // force align
 #	endif
-	value v;
+	union {
+		int i;
+		float f;
+		double d;
+		vobj *o;
+		vclosure *c;
+		void *ptr;
+	} v;
 } vdynamic;
 
 typedef struct {
@@ -247,6 +248,7 @@ struct vobj {
 #define CL_HAS_V64		2
 
 struct vclosure {
+	hl_type **t;
 	void *fun;
 	int bits;
 	union {
@@ -267,7 +269,7 @@ void *hl_alloc_executable_memory( int size );
 void hl_free_executable_memory( void *ptr );
 
 void hl_call( void *f );
-vdynamic *hl_alloc_dynamic( hl_type *t );
+vdynamic *hl_alloc_dynamic( hl_type **t );
 vobj *hl_alloc_obj( hl_module *m, hl_type *t );
 
 vclosure *hl_alloc_closure_void( hl_module *m, int_val f );

+ 27 - 28
src/jit.c

@@ -197,7 +197,6 @@ struct jit_ctx {
 	int functionPos;
 	int allocOffset;
 	int currentPos;
-	int *functionReindex;
 	unsigned char *startBuf;
 	hl_module *m;
 	hl_function *f;
@@ -245,9 +244,15 @@ static preg *paddr( preg *r, void *p ) {
 
 static void jit_buf( jit_ctx *ctx ) {
 	if( BUF_POS() > ctx->bufSize - MAX_OP_SIZE ) {
-		int nsize = ctx->bufSize ? (ctx->bufSize * 4) / 3 : ctx->f->nops * 4;
+		int nsize = ctx->bufSize * 4 / 3;
 		unsigned char *nbuf;
 		int curpos;
+		if( nsize == 0 ) {
+			int i;
+			for(i=0;i<ctx->m->code->nfunctions;i++)
+				nsize += ctx->m->code->functions[i].nops;
+			nsize *= 4;
+		}
 		if( nsize < ctx->bufSize + MAX_OP_SIZE * 4 ) nsize = ctx->bufSize + MAX_OP_SIZE * 4;
 		curpos = BUF_POS();
 		nbuf = (unsigned char*)malloc(nsize);
@@ -940,7 +945,7 @@ static void call_native( jit_ctx *ctx, void *nativeFun, int size ) {
 }
 
 static void op_call_fun( jit_ctx *ctx, vreg *dst, int findex, int count, int *args ) {
-	int fid = findex < 0 ? -1 : ctx->functionReindex[findex];
+	int fid = findex < 0 ? -1 : ctx->m->functions_indexes[findex];
 	int isNative = fid >= ctx->m->code->nfunctions;
 	int i = 0, size = prepare_call_args(ctx,count,args,ctx->vregs,isNative);
 	preg p;
@@ -987,13 +992,13 @@ static void op_call_closure( jit_ctx *ctx, vreg *dst, vreg *f, int nargs, int *r
 	LOCK(r);
 	tmp = alloc_reg(ctx, RCPU);
 	// read bits
-	op32(ctx,MOV,tmp,pmem(&p,(CpuReg)r->id,HL_WSIZE,0));
+	op32(ctx,MOV,tmp,pmem(&p,(CpuReg)r->id,HL_WSIZE*2,0));
 	op32(ctx,CMP,tmp,pconst(&p,0));
 	XJump(JNeq,has_param);
 	{
 		// no argument call
 		int size = prepare_call_args(ctx,nargs,rargs,ctx->vregs,false);
-		op64(ctx,CALL,pmem(&p,(CpuReg)r->id,0,0),UNUSED);
+		op64(ctx,CALL,pmem(&p,(CpuReg)r->id,HL_WSIZE,0),UNUSED);
 		if( size ) op64(ctx,ADD,PESP,pconst(&p,size));
 		XJump(JAlways,end1);
 	}
@@ -1014,7 +1019,7 @@ static void op_call_closure( jit_ctx *ctx, vreg *dst, vreg *f, int nargs, int *r
 		fake.t = &ti32;
 		fake.current = alloc_reg(ctx,RCPU);
 		LOCK(fake.current);
-		op32(ctx,MOV,fake.current,pmem(&p,(CpuReg)r->id,HL_WSIZE*2,0));
+		op32(ctx,MOV,fake.current,pmem(&p,(CpuReg)r->id,HL_WSIZE*3,0));
 
 		// prepare the args
 		vargs = (vreg*)hl_malloc(&ctx->falloc,sizeof(vreg)*(nargs+1));
@@ -1028,7 +1033,7 @@ static void op_call_closure( jit_ctx *ctx, vreg *dst, vreg *f, int nargs, int *r
 
 		// call
 		size = prepare_call_args(ctx,nargs+1,args,vargs,false);
-		op64(ctx,CALL,pmem(&p,(CpuReg)r->id,0,0),UNUSED);
+		op64(ctx,CALL,pmem(&p,(CpuReg)r->id,HL_WSIZE,0),UNUSED);
 		op64(ctx,ADD,PESP,pconst(&p,size));
 		XJump(JAlways,end2);
 	}
@@ -1048,7 +1053,7 @@ static void op_call_closure( jit_ctx *ctx, vreg *dst, vreg *f, int nargs, int *r
 		fake.current = alloc_reg(ctx,RFPU);
 		LOCK(fake.current);
 
-		op64(ctx,MOVSD,fake.current,pmem(&p,(CpuReg)r->id,HL_WSIZE*2,0));
+		op64(ctx,MOVSD,fake.current,pmem(&p,(CpuReg)r->id,HL_WSIZE*3,0));
 
 		// prepare the args
 		vargs = (vreg*)hl_malloc(&ctx->falloc,sizeof(vreg)*(nargs+1));
@@ -1062,7 +1067,7 @@ static void op_call_closure( jit_ctx *ctx, vreg *dst, vreg *f, int nargs, int *r
 
 		// call
 		size = prepare_call_args(ctx,nargs+1,args,vargs,false);
-		op64(ctx,CALL,pmem(&p,(CpuReg)r->id,0,0),UNUSED);
+		op64(ctx,CALL,pmem(&p,(CpuReg)r->id,HL_WSIZE,0),UNUSED);
 		op64(ctx,ADD,PESP,pconst(&p,size));
 	}
 	patch_jump(ctx,end1);
@@ -1256,33 +1261,28 @@ void hl_jit_free( jit_ctx *ctx ) {
 	free(ctx->vregs);
 	free(ctx->opsPos);
 	free(ctx->startBuf);
-	free(ctx->functionReindex);
 	hl_free(&ctx->falloc);
 	hl_free(&ctx->galloc);
 	free(ctx);
 }
 
+void hl_jit_init( jit_ctx *ctx, hl_module *m ) {
+	int i;
+	ctx->m = m;
+	for(i=0;i<m->code->nfloats;i++) {
+		jit_buf(ctx);
+		*ctx->buf.d++ = m->code->floats[i];
+	}
+	while( BUF_POS() & 15 )
+		op32(ctx, NOP, UNUSED, UNUSED);
+}
+
 int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 	int i, size = 0, opCount;
-	int codePos;
+	int codePos = BUF_POS();
 	int nargs = f->type->fun->nargs;
 	preg p;
-	ctx->m = m;
 	ctx->f = f;
-	if( !ctx->functionReindex ) {
-		ctx->functionReindex = (int*)malloc(sizeof(int)*(m->code->nfunctions+m->code->nnatives));
-		memset(ctx->functionReindex,0xFF,sizeof(int)*(m->code->nfunctions+m->code->nnatives));
-		for(i=0;i<m->code->nfunctions;i++)
-			ctx->functionReindex[(m->code->functions + i)->findex] = i;
-		for(i=0;i<m->code->nnatives;i++)
-			ctx->functionReindex[(m->code->natives + i)->findex] = i + m->code->nfunctions;
-		for(i=0;i<m->code->nfloats;i++) {
-			jit_buf(ctx);
-			*ctx->buf.d++ = m->code->floats[i];
-		}
-		while( BUF_POS() & 15 )
-			op32(ctx, NOP, UNUSED, UNUSED);
-	}
 	if( f->nregs > ctx->maxRegs ) {
 		free(ctx->vregs);
 		ctx->vregs = (vreg*)malloc(sizeof(vreg) * f->nregs);
@@ -1302,7 +1302,6 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 		ctx->maxOps = f->nops;
 	}
 	memset(ctx->opsPos,0,(f->nops+1)*sizeof(int));
-	codePos = BUF_POS();
 	for(i=0;i<f->nregs;i++) {
 		vreg *r = R(i);
 		r->t = f->regs[i];
@@ -1421,7 +1420,7 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 		case OToAny:
 			{
 				vreg *r = R(o->p2);
-				int_val rt = (int_val)r->t;
+				int_val rt = (int_val)&f->regs[o->p1];
 				call_native_consts(ctx, hl_alloc_dynamic, &rt, 1);
 				// copy value to dynamic
 				if( IS_FLOAT(r) && !IS_64 ) {

+ 15 - 2
src/module.c

@@ -105,11 +105,13 @@ hl_module *hl_module_alloc( hl_code *c ) {
 	}
 	memset(m->globals_data,0,gsize);
 	m->functions_ptrs = (void**)malloc(sizeof(void*)*(c->nfunctions + c->nnatives));
-	if( m->functions_ptrs == NULL ) {
+	m->functions_indexes = (int*)malloc(sizeof(int)*(c->nfunctions + c->nnatives));
+	if( m->functions_ptrs == NULL || m->functions_indexes == NULL ) {
 		hl_module_free(m);
 		return NULL;
 	}
 	memset(m->functions_ptrs,0,sizeof(void*)*(c->nfunctions + c->nnatives));
+	memset(m->functions_indexes,0xFF,sizeof(void*)*(c->nfunctions + c->nnatives));
 	return m;
 }
 
@@ -119,7 +121,7 @@ static void null_function() {
 }
 
 static void do_log( vdynamic *v ) {
-	switch( v->t->kind ) {
+	switch( (*v->t)->dyn->kind ) {
 	case HI32:
 		printf("%di\n",v->v.i);
 		break;
@@ -148,10 +150,20 @@ int hl_module_init( hl_module *m ) {
 		hl_native *n = m->code->natives + i;
 		m->functions_ptrs[n->findex] = do_log;
 	}
+	// INIT indexes
+	for(i=0;i<m->code->nfunctions;i++) {
+		hl_function *f = m->code->functions + i;
+		m->functions_indexes[f->findex] = i;
+	}
+	for(i=0;i<m->code->nnatives;i++) {
+		hl_native *n = m->code->natives + i;
+		m->functions_indexes[n->findex] = i + m->code->nfunctions;
+	}
 	// JIT
 	ctx = hl_jit_alloc();
 	if( ctx == NULL )
 		return 0;
+	hl_jit_init(ctx, m);
 	for(i=0;i<m->code->nfunctions;i++) {
 		hl_function *f = m->code->functions + i;
 		int fpos = hl_jit_function(ctx, m, f);
@@ -172,6 +184,7 @@ int hl_module_init( hl_module *m ) {
 
 void hl_module_free( hl_module *m ) {
 	hl_free_executable_memory(m->code);
+	free(m->functions_indexes);
 	free(m->functions_ptrs);
 	free(m->globals_indexes);
 	free(m->globals_data);