2
0
Nicolas Cannasse 8 жил өмнө
parent
commit
7a4fc00e2a
5 өөрчлөгдсөн 48 нэмэгдсэн , 69 устгасан
  1. 2 0
      src/hl.h
  2. 15 27
      src/jit.c
  3. 2 2
      src/std/buffer.c
  4. 2 1
      src/std/cast.c
  5. 27 39
      src/std/types.c

+ 2 - 0
src/hl.h

@@ -433,6 +433,7 @@ typedef struct {
 } vdynobj;
 } vdynobj;
 
 
 typedef struct _venum {
 typedef struct _venum {
+	hl_type *t;
 	int index;
 	int index;
 } venum;
 } venum;
 
 
@@ -459,6 +460,7 @@ HL_API bool hl_safe_cast( hl_type *t, hl_type *to );
 HL_API varray *hl_alloc_array( hl_type *t, int size );
 HL_API varray *hl_alloc_array( hl_type *t, int size );
 HL_API vdynamic *hl_alloc_dynamic( hl_type *t );
 HL_API vdynamic *hl_alloc_dynamic( hl_type *t );
 HL_API vdynamic *hl_alloc_obj( hl_type *t );
 HL_API vdynamic *hl_alloc_obj( hl_type *t );
+HL_API venum *hl_alloc_enum( hl_type *t, int index );
 HL_API vvirtual *hl_alloc_virtual( hl_type *t );
 HL_API vvirtual *hl_alloc_virtual( hl_type *t );
 HL_API vdynobj *hl_alloc_dynobj();
 HL_API vdynobj *hl_alloc_dynobj();
 HL_API vbyte *hl_alloc_bytes( int size );
 HL_API vbyte *hl_alloc_bytes( int size );

+ 15 - 27
src/jit.c

@@ -303,10 +303,16 @@ static preg *pconst( preg *r, int c ) {
 }
 }
 
 
 static preg *pconst64( preg *r, int_val c ) {
 static preg *pconst64( preg *r, int_val c ) {
+#ifdef HL_64
+	if( (c&0xFFFFFFFF) == c )
+		return pconst(r,(int)c);
 	r->kind = RCONST;
 	r->kind = RCONST;
 	r->id = 0xC064C064;
 	r->id = 0xC064C064;
 	r->holds = (vreg*)c;
 	r->holds = (vreg*)c;
 	return r;
 	return r;
+#else
+	return pconst(r,(int)c);
+#endif
 }
 }
 
 
 static preg *paddr( preg *r, void *p ) {
 static preg *paddr( preg *r, void *p ) {
@@ -3027,11 +3033,9 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 		case OMakeEnum:
 		case OMakeEnum:
 			{
 			{
 				hl_enum_construct *c = &dst->t->tenum->constructs[o->p2];
 				hl_enum_construct *c = &dst->t->tenum->constructs[o->p2];
-				int_val args[] = { (int_val)dst->t, c->size, c->hasptr?MEM_KIND_RAW:MEM_KIND_NOPTR };
+				int_val args[] = { (int_val)dst->t, o->p2 };
 				int i;
 				int i;
-				call_native_consts(ctx, hl_gc_alloc_gen, args, 3);
-				op32(ctx,MOV,REG_AT(Ecx),pconst(&p,o->p2));
-				op32(ctx,MOV,pmem(&p,Eax,0),REG_AT(Ecx));
+				call_native_consts(ctx, hl_alloc_enum, args, 2);
 				RLOCK(PEAX);
 				RLOCK(PEAX);
 				for(i=0;i<c->nparams;i++) {
 				for(i=0;i<c->nparams;i++) {
 					preg *r = fetch(R(o->extra[i]));
 					preg *r = fetch(R(o->extra[i]));
@@ -3044,12 +3048,9 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 			break;
 			break;
 		case OEnumAlloc:
 		case OEnumAlloc:
 			{
 			{
-				hl_enum_construct *c = &dst->t->tenum->constructs[o->p2];
-				int_val args[] = { (int_val)dst->t, c->size, (c->hasptr?MEM_KIND_RAW:MEM_KIND_NOPTR) | MEM_ZERO };
-				call_native_consts(ctx, hl_gc_alloc_gen, args, 3);
+				int_val args[] = { (int_val)dst->t, o->p2 };
+				call_native_consts(ctx, hl_alloc_enum, args, 2);
 				store(ctx, dst, PEAX, true);
 				store(ctx, dst, PEAX, true);
-				op32(ctx,MOV,REG_AT(Ecx),pconst(&p,o->p2));
-				op32(ctx,MOV,pmem(&p,Eax,0),REG_AT(Ecx));
 			}
 			}
 			break;
 			break;
 		case OEnumField:
 		case OEnumField:
@@ -3181,24 +3182,11 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 			}
 			}
 			break;
 			break;
 		case OEnumIndex:
 		case OEnumIndex:
-			switch( ra->t->kind ) {
-			case HENUM:
-				{
-					preg *r = alloc_reg(ctx,RCPU);
-					op64(ctx,MOV,r,pmem(&p,alloc_cpu(ctx,ra,true)->id,0));
-					store(ctx,dst,r,true);
-					break;
-				}
-			case HDYN:
-				{
-					preg *r = alloc_reg(ctx,RCPU);
-					op64(ctx,MOV,r,pmem(&p,alloc_cpu(ctx,ra,true)->id,HDYN_VALUE)); // read dynamic ptr
-					op64(ctx,MOV,r,pmem(&p,r->id,0)); // read index
-					store(ctx,dst,r,true);
-					break;
-				}
-			default:
-				ASSERT(ra->t->kind);
+			{
+				preg *r = alloc_reg(ctx,RCPU);
+				op64(ctx,MOV,r,pmem(&p,alloc_cpu(ctx,ra,true)->id,HL_WSIZE));
+				store(ctx,dst,r,true);
+				break;
 			}
 			}
 			break;
 			break;
 		case OSwitch:
 		case OSwitch:

+ 2 - 2
src/std/buffer.c

@@ -341,7 +341,7 @@ static void hl_buffer_rec( hl_buffer *b, vdynamic *v, vlist *stack ) {
 			int i;
 			int i;
 			vlist l;
 			vlist l;
 			vlist *vtmp = stack;
 			vlist *vtmp = stack;
-			hl_enum_construct *c = v->t->tenum->constructs + ((venum*)v->v.ptr)->index;
+			hl_enum_construct *c = v->t->tenum->constructs + ((venum*)v)->index;
 			if( !c->nparams ) {
 			if( !c->nparams ) {
 				hl_buffer_str(b, c->name);
 				hl_buffer_str(b, c->name);
 				break;
 				break;
@@ -359,7 +359,7 @@ static void hl_buffer_rec( hl_buffer *b, vdynamic *v, vlist *stack ) {
 			hl_buffer_char(b,'(');
 			hl_buffer_char(b,'(');
 			for(i=0;i<c->nparams;i++) {
 			for(i=0;i<c->nparams;i++) {
 				if( i ) hl_buffer_char(b,',');
 				if( i ) hl_buffer_char(b,',');
-				hl_buffer_addr(b,(char*)v->v.ptr + c->offsets[i],c->params[i], &l);
+				hl_buffer_addr(b,(char*)v + c->offsets[i],c->params[i], &l);
 			}
 			}
 			hl_buffer_char(b,')');
 			hl_buffer_char(b,')');
 		}
 		}

+ 2 - 1
src/std/cast.c

@@ -20,6 +20,7 @@
  * DEALINGS IN THE SOFTWARE.
  * DEALINGS IN THE SOFTWARE.
  */
  */
 #include <hl.h>
 #include <hl.h>
+#include <math.h>
 
 
 #define TK2(a,b)		((a) | ((b)<<5))
 #define TK2(a,b)		((a) | ((b)<<5))
 
 
@@ -66,7 +67,6 @@ HL_PRIM vdynamic *hl_make_dyn( void *data, hl_type *t ) {
 	case HTYPE:
 	case HTYPE:
 	case HREF:
 	case HREF:
 	case HABSTRACT:
 	case HABSTRACT:
-	case HENUM:
 		{
 		{
 			void *p = *(void**)data;
 			void *p = *(void**)data;
 			if( p == NULL ) return NULL;
 			if( p == NULL ) return NULL;
@@ -348,6 +348,7 @@ HL_PRIM int hl_dyn_compare( vdynamic *a, vdynamic *b ) {
 			return a->t->obj->rt->compareFun(a,b);
 			return a->t->obj->rt->compareFun(a,b);
 		return a > b ? 1 : -1;
 		return a > b ? 1 : -1;
 	case TK2(HENUM,HENUM):
 	case TK2(HENUM,HENUM):
+		return a > b ? 1 : -1;
 	case TK2(HTYPE,HTYPE):
 	case TK2(HTYPE,HTYPE):
 	case TK2(HBYTES,HBYTES):
 	case TK2(HBYTES,HBYTES):
 		return a->v.ptr != b->v.ptr;
 		return a->v.ptr != b->v.ptr;

+ 27 - 39
src/std/types.c

@@ -154,7 +154,7 @@ HL_PRIM bool hl_is_dynamic( hl_type *t ) {
 		true, // HVIRTUAL
 		true, // HVIRTUAL
 		true, // HDYNOBJ
 		true, // HDYNOBJ
 		false, // HABSTRACT
 		false, // HABSTRACT
-		false, // HENUM
+		true, // HENUM
 		true, // HNULL
 		true, // HNULL
 	};
 	};
 	return T_IS_DYNAMIC[t->kind];
 	return T_IS_DYNAMIC[t->kind];
@@ -323,7 +323,7 @@ HL_PRIM void hl_init_enum( hl_type *et, hl_module_context *m ) {
 	for(i=0;i<et->tenum->nconstructs;i++) {
 	for(i=0;i<et->tenum->nconstructs;i++) {
 		hl_enum_construct *c = et->tenum->constructs + i;
 		hl_enum_construct *c = et->tenum->constructs + i;
 		c->hasptr = false;
 		c->hasptr = false;
-		c->size = sizeof(int); // index
+		c->size = sizeof(venum); // t + index
 		for(j=0;j<c->nparams;j++) {
 		for(j=0;j<c->nparams;j++) {
 			hl_type *t = c->params[j];
 			hl_type *t = c->params[j];
 			c->size += hl_pad_size(c->size,t);
 			c->size += hl_pad_size(c->size,t);
@@ -364,13 +364,8 @@ HL_PRIM varray* hl_type_enum_values( hl_type *t ) {
 	int i;
 	int i;
 	for( i=0; i<t->tenum->nconstructs;i++) {
 	for( i=0; i<t->tenum->nconstructs;i++) {
 		hl_enum_construct *c = t->tenum->constructs + i;
 		hl_enum_construct *c = t->tenum->constructs + i;
-		if(c->nparams == 0) {
-			venum *e = hl_gc_alloc_noptr(c->size);
-			vdynamic *v = hl_alloc_dynamic(t);
-			e->index = i;
-			v->v.ptr = e;
-			hl_aptr(a,vdynamic*)[i] = v;
-		}
+		if(c->nparams == 0)
+			hl_aptr(a,venum*)[i] = hl_alloc_enum(t, i);
 	}
 	}
 	return a;
 	return a;
 }
 }
@@ -461,41 +456,31 @@ HL_PRIM bool hl_type_set_global( hl_type *t, vdynamic *v ) {
 	return false;
 	return false;
 }
 }
 
 
-HL_PRIM bool hl_type_enum_eq( vdynamic *a, vdynamic *b ) {
+HL_PRIM bool hl_type_enum_eq( venum *a, venum *b ) {
 	int i;
 	int i;
-	venum *ea, *eb;
 	hl_enum_construct *c;
 	hl_enum_construct *c;
 	if( a == b )
 	if( a == b )
 		return true;
 		return true;
-	if( !a || !b || a->t != b->t || a->t->kind != HENUM )
+	if( !a || !b || a->t != b->t )
 		return false;
 		return false;
-	ea = (venum*)a->v.ptr;
-	eb = (venum*)b->v.ptr;
-	if( ea->index != eb->index )
+	if( a->index != b->index )
 		return false;
 		return false;
-	c = a->t->tenum->constructs + ea->index;
+	c = a->t->tenum->constructs + a->index;
 	for(i=0;i<c->nparams;i++) {
 	for(i=0;i<c->nparams;i++) {
 		hl_type *t = c->params[i];
 		hl_type *t = c->params[i];
 		switch( t->kind ) {
 		switch( t->kind ) {
 		case HENUM:
 		case HENUM:
 			{
 			{
-				vdynamic pa, pb;
-				pa.t = pb.t = t;
-				pa.v.ptr = *(void**)((char*)ea + c->offsets[i]);
-				pb.v.ptr = *(void**)((char*)eb + c->offsets[i]);
-				if( !hl_type_enum_eq(&pa,&pb) )
+				venum *pa = *(venum**)((char*)a + c->offsets[i]);
+				venum *pb = *(venum**)((char*)b + c->offsets[i]);
+				if( !hl_type_enum_eq(pa,pb) )
 					return false;
 					return false;
 			}
 			}
 			break;
 			break;
 		default:
 		default:
 			{
 			{
-				vdynamic *pa = hl_make_dyn((char*)ea + c->offsets[i],t);
-				vdynamic *pb = hl_make_dyn((char*)eb + c->offsets[i],t);
-				if( pa && pa->t->kind == HENUM && pb && pa->t == pb->t ) {
-					if( !hl_type_enum_eq(pa,pb) )
-						return false;
-					continue;
-				}
+				vdynamic *pa = hl_make_dyn((char*)a + c->offsets[i],t);
+				vdynamic *pb = hl_make_dyn((char*)b + c->offsets[i],t);
 				if( hl_dyn_compare(pa,pb) )
 				if( hl_dyn_compare(pa,pb) )
 					return false;
 					return false;
 			}
 			}
@@ -505,26 +490,29 @@ HL_PRIM bool hl_type_enum_eq( vdynamic *a, vdynamic *b ) {
 	return true;
 	return true;
 }
 }
 
 
-HL_PRIM vdynamic *hl_alloc_enum( hl_type *t, int index, varray *args, int nargs ) {
+HL_PRIM venum *hl_alloc_enum( hl_type *t, int index ) {
+	hl_enum_construct *c = t->tenum->constructs + index;
+	venum *v = (venum*)hl_gc_alloc_gen(t, c->size, MEM_KIND_DYNAMIC | (c->hasptr ? 0 : MEM_KIND_NOPTR) | MEM_ZERO);
+	v->t = t;
+	v->index = index;
+	return v;
+}
+
+HL_PRIM venum *hl_alloc_enum_dyn( hl_type *t, int index, varray *args, int nargs ) {
 	hl_enum_construct *c = t->tenum->constructs + index;
 	hl_enum_construct *c = t->tenum->constructs + index;
 	venum *e;
 	venum *e;
-	vdynamic *v;
 	int i;
 	int i;
 	if( c->nparams != nargs || args->size < nargs )
 	if( c->nparams != nargs || args->size < nargs )
 		return NULL;
 		return NULL;
-	e = (venum*)(c->hasptr ? hl_gc_alloc_raw(c->size) : hl_gc_alloc_noptr(c->size));
-	e->index = index;
+	e = hl_alloc_enum(t, index);
 	for(i=0;i<c->nparams;i++)
 	for(i=0;i<c->nparams;i++)
 		hl_write_dyn((char*)e+c->offsets[i],c->params[i],hl_aptr(args,vdynamic*)[i]);
 		hl_write_dyn((char*)e+c->offsets[i],c->params[i],hl_aptr(args,vdynamic*)[i]);
-	v = hl_alloc_dynamic(t);
-	v->v.ptr = e;
-	return v;
+	return e;
 }
 }
 
 
-HL_PRIM varray *hl_enum_parameters( vdynamic *v ) {
+HL_PRIM varray *hl_enum_parameters( venum *e ) {
 	varray *a;
 	varray *a;
-	venum *e = (venum*)v->v.ptr;
-	hl_enum_construct *c = v->t->tenum->constructs + e->index;
+	hl_enum_construct *c = e->t->tenum->constructs + e->index;
 	int i;
 	int i;
 	a = hl_alloc_array(&hlt_dyn,c->nparams);
 	a = hl_alloc_array(&hlt_dyn,c->nparams);
 	for(i=0;i<c->nparams;i++)
 	for(i=0;i<c->nparams;i++)
@@ -541,6 +529,6 @@ DEFINE_PRIM(_DYN, type_get_global, _TYPE);
 DEFINE_PRIM(_ARR, type_enum_fields, _TYPE);
 DEFINE_PRIM(_ARR, type_enum_fields, _TYPE);
 DEFINE_PRIM(_ARR, type_enum_values, _TYPE);
 DEFINE_PRIM(_ARR, type_enum_values, _TYPE);
 DEFINE_PRIM(_BOOL, type_enum_eq, _DYN _DYN);
 DEFINE_PRIM(_BOOL, type_enum_eq, _DYN _DYN);
-DEFINE_PRIM(_DYN, alloc_enum, _TYPE _I32 _ARR _I32);
+DEFINE_PRIM(_DYN, alloc_enum_dyn, _TYPE _I32 _ARR _I32);
 DEFINE_PRIM(_ARR, enum_parameters, _DYN);
 DEFINE_PRIM(_ARR, enum_parameters, _DYN);
 DEFINE_PRIM(_BOOL, type_set_global, _TYPE _DYN);
 DEFINE_PRIM(_BOOL, type_set_global, _TYPE _DYN);