瀏覽代碼

added HPACKED support

Nicolas Cannasse 3 年之前
父節點
當前提交
05827bd982
共有 6 個文件被更改,包括 53 次插入4 次删除
  1. 1 0
      src/code.c
  2. 2 1
      src/hl.h
  3. 18 0
      src/jit.c
  4. 1 1
      src/std/buffer.c
  5. 20 1
      src/std/obj.c
  6. 11 1
      src/std/types.c

+ 1 - 0
src/code.c

@@ -247,6 +247,7 @@ static void hl_read_type( hl_reader *r, hl_type *t ) {
 		}
 		break;
 	case HNULL:
+	case HPACKED:
 		t->tparam = hl_get_type(r);
 		break;
 	default:

+ 2 - 1
src/hl.h

@@ -329,8 +329,9 @@ typedef enum {
 	HNULL	= 19,
 	HMETHOD = 20,
 	HSTRUCT	= 21,
+	HPACKED = 22,
 	// ---------
-	HLAST	= 22,
+	HLAST	= 23,
 	_H_FORCE_INT = 0x7FFFFFFF
 } hl_type_kind;
 

+ 18 - 0
src/jit.c

@@ -3381,6 +3381,15 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 					{
 						hl_runtime_obj *rt = hl_get_obj_rt(ra->t);
 						preg *rr = alloc_cpu(ctx,ra, true);
+						if( dst->t->kind == HSTRUCT ) {
+							hl_type *ft = hl_obj_field_fetch(ra->t,o->p3)->t;
+							if( ft->kind == HPACKED ) {
+								preg *r = alloc_reg(ctx,RCPU);
+								op64(ctx,LEA,r,pmem(&p,(CpuReg)rr->id,rt->fields_indexes[o->p3]));
+								store(ctx,dst,r,true);
+								break;
+							}
+						}
 						copy_to(ctx,dst,pmem(&p, (CpuReg)rr->id, rt->fields_indexes[o->p3]));
 					}
 					break;
@@ -3487,6 +3496,15 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 				vreg *r = R(0);
 				hl_runtime_obj *rt = hl_get_obj_rt(r->t);
 				preg *rr = alloc_cpu(ctx,r, true);
+				if( dst->t->kind == HSTRUCT ) {
+					hl_type *ft = hl_obj_field_fetch(r->t,o->p2)->t;
+					if( ft->kind == HPACKED ) {
+						preg *r = alloc_reg(ctx,RCPU);
+						op64(ctx,LEA,r,pmem(&p,(CpuReg)rr->id,rt->fields_indexes[o->p2]));
+						store(ctx,dst,r,true);
+						break;
+					}
+				}
 				copy_to(ctx,dst,pmem(&p, (CpuReg)rr->id, rt->fields_indexes[o->p2]));
 			}
 			break;

+ 1 - 1
src/std/buffer.c

@@ -233,7 +233,7 @@ static void hl_buffer_rec( hl_buffer *b, vdynamic *v, vlist *stack ) {
 	case HSTRUCT:
 		{
 			hl_type_obj *o = v->t->obj;
-			if( o->rt == NULL || o->rt->toStringFun == NULL ) {
+			if( o->rt == NULL || hl_get_obj_proto(v->t)->toStringFun == NULL ) {
 				if( v->t->kind == HSTRUCT ) hl_buffer_char(b,'@');
 				hl_buffer_str(b,o->name);
 			} else

+ 20 - 1
src/std/obj.c

@@ -233,12 +233,25 @@ HL_PRIM hl_runtime_obj *hl_get_obj_rt( hl_type *ot ) {
 	nlookup = 0;
 	for(i=0;i<o->nfields;i++) {
 		hl_type *ft = o->fields[i].t;
-		size += hl_pad_struct(size,ft);
+		hl_type *pad = ft;
+		while( pad->kind == HPACKED ) {
+			// align on first field
+			pad = pad->tparam;
+			while( pad->obj->super && hl_get_obj_rt(pad->obj->super)->nfields ) pad = pad->obj->super;
+			pad = pad->obj->fields[0].t;
+		}
+		size += hl_pad_struct(size,pad);
 		t->fields_indexes[i+start] = size;
 		if( *o->fields[i].name )
 			hl_lookup_insert(t->lookup,nlookup++,o->fields[i].hashed_name,o->fields[i].t,size);
 		else
 			t->nlookup--;
+		if( ft->kind == HPACKED ) {
+			hl_runtime_obj *rts = hl_get_obj_rt(ft->tparam);
+			size += rts->size;
+			if( rts->hasPtr ) t->hasPtr = true;
+			continue;
+		}
 		size += hl_type_size(ft);
 		if( !t->hasPtr && hl_is_ptr(ft) ) t->hasPtr = true;
 	}
@@ -277,6 +290,12 @@ HL_PRIM hl_runtime_obj *hl_get_obj_rt( hl_type *ot ) {
 			hl_type *ft = o->fields[i].t;
 			if( hl_is_ptr(ft) ) {
 				int pos = t->fields_indexes[i + start] / HL_WSIZE;
+				if( ft->kind == HPACKED ) {
+					hl_runtime_obj *rts = hl_get_obj_rt(ft->tparam);
+					if( rts->t->mark_bits )
+						memcpy(mark + (pos>>5), rts->t->mark_bits, hl_mark_size(rts->size));
+					continue;
+				}
 				mark[pos >> 5] |= 1 << (pos & 31);
 			}
 		}

+ 11 - 1
src/std/types.c

@@ -37,7 +37,7 @@ static const uchar *TSTR[] = {
 	USTR("void"), USTR("i8"), USTR("i16"), USTR("i32"), USTR("i64"), USTR("f32"), USTR("f64"),
 	USTR("bool"), USTR("bytes"), USTR("dynamic"), NULL, NULL,
 	USTR("array"), USTR("type"), NULL, NULL, USTR("dynobj"),
-	NULL, NULL, NULL, NULL, NULL
+	NULL, NULL, NULL, NULL, NULL, NULL
 };
 
 static int T_SIZES[] = {
@@ -63,6 +63,7 @@ static int T_SIZES[] = {
 	HL_WSIZE, // NULL
 	HL_WSIZE, // METHOD
 	HL_WSIZE, // STRUCT
+	0, // PACKED
 };
 
 HL_PRIM int hl_type_size( hl_type *t ) {
@@ -124,6 +125,7 @@ HL_PRIM bool hl_same_type( hl_type *a, hl_type *b ) {
 		return true;
 	case HREF:
 	case HNULL:
+	case HPACKED:
 		return hl_same_type(a->tparam, b->tparam);
 	case HFUN:
 	case HMETHOD:
@@ -175,6 +177,7 @@ HL_PRIM bool hl_is_dynamic( hl_type *t ) {
 		true, // HNULL
 		false, // HMETHOD
 		false, // HSTRUCT
+		false, // HPACKED
 	};
 	return T_IS_DYNAMIC[t->kind];
 }
@@ -226,6 +229,8 @@ HL_PRIM bool hl_safe_cast( hl_type *t, hl_type *to ) {
 			return true;
 		}
 		break;
+	case HPACKED:
+		return hl_safe_cast(t->tparam, to);
 	default:
 		break;
 	}
@@ -307,6 +312,11 @@ static void hl_type_str_rec( hl_buffer *b, hl_type *t, tlist *parents ) {
 		hl_type_str_rec(b,t->tparam,l);
 		hl_buffer_char(b,'>');
 		break;
+	case HPACKED:
+		hl_buffer_str(b, USTR("packed<"));
+		hl_type_str_rec(b,t->tparam,l);
+		hl_buffer_char(b,'>');
+		break;
 	default:
 		hl_buffer_str(b,USTR("???"));
 		break;