Răsfoiți Sursa

support for constant values (bytecode ver=4)

Nicolas Cannasse 7 ani în urmă
părinte
comite
d4ea2bb2f6
3 a modificat fișierele cu 57 adăugiri și 1 ștergeri
  1. 13 1
      src/code.c
  2. 8 0
      src/hlmodule.h
  3. 36 0
      src/module.c

+ 13 - 1
src/code.c

@@ -434,7 +434,7 @@ hl_code *hl_code_read( const unsigned char *data, int size ) {
 		EXIT("Invalid header");
 	r->code = c;
 	c->version = READ();
-	if( c->version <= 1 || c->version > 3 ) {
+	if( c->version <= 1 || c->version > 4 ) {
 		printf("VER=%d\n",c->version);
 		EXIT("Unsupported bytecode version");
 	}
@@ -446,6 +446,7 @@ hl_code *hl_code_read( const unsigned char *data, int size ) {
 	c->nglobals = UINDEX();
 	c->nnatives = UINDEX();
 	c->nfunctions = UINDEX();
+	c->nconstants = c->version >= 4 ? UINDEX() : 0;
 	c->entrypoint = UINDEX();	
 	c->hasdebug = flags & 1;
 	CHK_ERROR();
@@ -501,6 +502,17 @@ hl_code *hl_code_read( const unsigned char *data, int size ) {
 		}
 	}
 	CHK_ERROR();
+	ALLOC(c->constants, hl_constant, c->nconstants);
+	for (i = 0; i < c->nconstants; i++) {
+		int j;
+		hl_constant *k = c->constants + i;
+		k->global = UINDEX();
+		k->nfields = UINDEX();
+		ALLOC(k->fields, int, k->nfields);
+		for (j = 0; j < k->nfields; j++)
+			k->fields[j] = UINDEX();
+		CHK_ERROR();
+	}
 	return c;
 }
 

+ 8 - 0
src/hlmodule.h

@@ -50,6 +50,12 @@ typedef struct {
 	const uchar *field;
 } hl_function;
 
+typedef struct {
+	int global;
+	int nfields;
+	int *fields;
+} hl_constant;
+
 typedef struct {
 	int version;
 	int nints;
@@ -59,6 +65,7 @@ typedef struct {
 	int nglobals;
 	int nnatives;
 	int nfunctions;
+	int nconstants;
 	int entrypoint;
 	int ndebugfiles;
 	bool hasdebug;
@@ -73,6 +80,7 @@ typedef struct {
 	hl_type**	globals;
 	hl_native*	natives;
 	hl_function*functions;
+	hl_constant*constants;
 	hl_alloc	alloc;
 	hl_alloc	falloc;
 } hl_code;

+ 36 - 0
src/module.c

@@ -395,6 +395,42 @@ int hl_module_init( hl_module *m, void *stack_top_val ) {
 		hl_function *f = m->code->functions + i;
 		m->functions_ptrs[f->findex] = ((unsigned char*)m->jit_code) + ((int_val)m->functions_ptrs[f->findex]);
 	}
+	// INIT constants
+	for (i = 0; i<m->code->nconstants; i++) {
+		int j;
+		hl_constant *c = m->code->constants + i;
+		hl_type *t = m->code->globals[c->global];
+		hl_runtime_obj *rt;
+		vdynamic **global = (vdynamic**)(m->globals_data + m->globals_indexes[c->global]);
+		vdynamic *v = NULL;
+		switch (t->kind) {
+		case HOBJ:
+			rt = hl_get_obj_rt(t);
+			v = (vdynamic*)malloc(rt->size);
+			v->t = t;
+			for (j = 0; j<c->nfields; j++) {
+				int idx = c->fields[j];
+				hl_type *ft = t->obj->fields[j].t;
+				void *addr = (char*)v + rt->fields_indexes[j];
+				switch (ft->kind) {
+				case HI32:
+					*(int*)addr = m->code->ints[idx];
+					break;
+				case HBYTES:
+					*(const void**)addr = hl_get_ustring(m->code, idx);
+					break;
+				default:
+					hl_fatal("assert");
+				}
+			}
+			break;
+		default:
+			hl_fatal("assert");
+		}
+		*global = v;
+		hl_remove_root(global);
+	}
+	// DONE
 	cur_module = m;
 	stack_top = stack_top_val;
 	hl_setup_exception(module_resolve_symbol, module_capture_stack);