Skip to content

Instantly share code, notes, and snippets.

@FROGGS
Created January 7, 2014 13:44
Show Gist options
  • Save FROGGS/04f5ffded5f1515f236d to your computer and use it in GitHub Desktop.
Save FROGGS/04f5ffded5f1515f236d to your computer and use it in GitHub Desktop.
diff --git a/src/6model/bootstrap.c b/src/6model/bootstrap.c
index a8d88cc..f1a6912 100644
--- a/src/6model/bootstrap.c
+++ b/src/6model/bootstrap.c
@@ -601,6 +601,7 @@ void MVM_6model_bootstrap(MVMThreadContext *tc) {
string_creator(tc, str_type, "type");
string_creator(tc, str_box_target, "box_target");
string_creator(tc, str_attribute, "attribute");
+ string_creator(tc, str_int, "int");
string_creator(tc, str_array, "array");
/* Bootstrap the KnowHOW type, giving it a meta-object. */
diff --git a/src/6model/reprs/P6int.c b/src/6model/reprs/P6int.c
index ad008ef..a245779 100644
--- a/src/6model/reprs/P6int.c
+++ b/src/6model/reprs/P6int.c
@@ -3,6 +3,10 @@
/* This representation's function pointer table. */
static const MVMREPROps this_repr;
+/* Some strings. */
+static MVMString *str_int = NULL;
+static MVMString *str_type = NULL;
+
/* Creates a new type object of this representation, and associates it with
* the given HOW. */
static MVMObject * type_object_for(MVMThreadContext *tc, MVMObject *HOW) {
@@ -10,8 +14,14 @@ static MVMObject * type_object_for(MVMThreadContext *tc, MVMObject *HOW) {
MVMROOT(tc, st, {
MVMObject *obj = MVM_gc_allocate_type_object(tc, st);
+ MVMP6intREPRData *repr_data = (MVMP6intREPRData *)malloc(sizeof(MVMP6intREPRData));
+
+ repr_data->bits = sizeof(MVMint64) * 8;
+ repr_data->is_unsigned = 0;
+
MVM_ASSIGN_REF(tc, st, st->WHAT, obj);
st->size = sizeof(MVMP6int);
+ st->REPR_data = repr_data;
});
return st->WHAT;
@@ -37,19 +47,57 @@ static MVMint64 get_int(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, vo
return ((MVMP6intBody *)data)->value;
}
+/* Marks the representation data in an STable.*/
+static void gc_mark_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMGCWorklist *worklist) {
+ MVMP6intREPRData *repr_data = (MVMP6intREPRData *)st->REPR_data;
+ if (repr_data == NULL)
+ return;
+ //~ MVM_gc_worklist_add(tc, worklist, &repr_data->elem_type);
+}
+
+/* Marks the representation data in an STable.*/
+static void gc_free_repr_data(MVMThreadContext *tc, MVMSTable *st) {
+ MVM_checked_free_null(st->REPR_data);
+}
+
/* Gets the storage specification for this representation. */
static MVMStorageSpec get_storage_spec(MVMThreadContext *tc, MVMSTable *st) {
+ MVMP6intREPRData *repr_data = (MVMP6intREPRData *)st->REPR_data;
MVMStorageSpec spec;
spec.inlineable = MVM_STORAGE_SPEC_INLINED;
- spec.bits = sizeof(MVMint64) * 8;
spec.boxed_primitive = MVM_STORAGE_SPEC_BP_INT;
spec.can_box = MVM_STORAGE_SPEC_CAN_BOX_INT;
+
+ if (repr_data && repr_data->bits) {
+ if (repr_data->bits < 64)
+ printf("%s:%d %d\n", __FILE__, __LINE__, repr_data->bits);
+ spec.bits = repr_data->bits;
+ }
+ else {
+ spec.bits = sizeof(MVMint64) * 8;
+ }
+
return spec;
}
/* Compose the representation. */
-static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info) {
- /* XXX size conveyed here */
+static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info_hash) {
+ MVMP6intREPRData *repr_data = (MVMP6intREPRData *)st->REPR_data;
+
+ MVMObject *info = MVM_repr_at_key_o(tc, info_hash, str_int);
+ printf("%s:%d\n", __FILE__, __LINE__);
+ if (info != NULL) {
+ printf("%s:%d\n", __FILE__, __LINE__);
+ MVMObject *type = MVM_repr_at_key_o(tc, info, str_type);
+ if (type != NULL) {
+ MVMStorageSpec spec = REPR(type)->get_storage_spec(tc, STABLE(type));
+ if (spec.bits == 1 || spec.bits == 2 || spec.bits == 4 || spec.bits == 8
+ || spec.bits == 16 || spec.bits == 32 || spec.bits == 64)
+ repr_data->bits = spec.bits;
+ else
+ MVM_exception_throw_adhoc(tc, "MVMP6int: Unsupported int size");
+ }
+ }
}
/* Set the size of the STable. */
@@ -67,6 +115,13 @@ static void serialize(MVMThreadContext *tc, MVMSTable *st, void *data, MVMSerial
/* Initializes the representation. */
const MVMREPROps * MVMP6int_initialize(MVMThreadContext *tc) {
+ printf("%s:%d\n", __FILE__, __LINE__);
+ /* Set up some constant strings we'll need. */
+ str_int = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "int");
+ MVM_gc_root_add_permanent(tc, (MVMCollectable **)&str_int);
+ str_type = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "type");
+ MVM_gc_root_add_permanent(tc, (MVMCollectable **)&str_type);
+
return &this_repr;
}
@@ -98,8 +153,8 @@ static const MVMREPROps this_repr = {
NULL, /* gc_mark */
NULL, /* gc_free */
NULL, /* gc_cleanup */
- NULL, /* gc_mark_repr_data */
- NULL, /* gc_free_repr_data */
+ gc_mark_repr_data,
+ gc_free_repr_data,
compose,
"P6int", /* name */
MVM_REPR_ID_P6int,
diff --git a/src/6model/reprs/P6int.h b/src/6model/reprs/P6int.h
index 280219c..c7234fe 100644
--- a/src/6model/reprs/P6int.h
+++ b/src/6model/reprs/P6int.h
@@ -8,5 +8,11 @@ struct MVMP6int {
MVMP6intBody body;
};
+/* The bit width requirement is shared for all instances of the same type. */
+struct MVMP6intREPRData {
+ MVMint64 bits;
+ MVMint64 is_unsigned;
+};
+
/* Function for REPR setup. */
const MVMREPROps * MVMP6int_initialize(MVMThreadContext *tc);
diff --git a/src/io/fileops.c b/src/io/fileops.c
index a866291..43843f7 100644
--- a/src/io/fileops.c
+++ b/src/io/fileops.c
@@ -594,8 +594,10 @@ void MVM_file_write_fhb(MVMThreadContext *tc, MVMObject *oshandle, MVMObject *bu
/* Ensure the target is in the correct form. */
if (!IS_CONCRETE(buffer) || REPR(buffer)->ID != MVM_REPR_ID_MVMArray)
MVM_exception_throw_adhoc(tc, "write_fhb requires a native array to read from");
- if (((MVMArrayREPRData *)STABLE(buffer)->REPR_data)->slot_type != 1)
+ if (((MVMArrayREPRData *)STABLE(buffer)->REPR_data)->slot_type != MVM_ARRAY_I8) {
+ printf("%d\n", ((MVMArrayREPRData *)STABLE(buffer)->REPR_data)->slot_type);
MVM_exception_throw_adhoc(tc, "write_fhb requires a native array of int8");
+ }
output = ((MVMArray *)buffer)->body.slots.i8;
output_size = ((MVMArray *)buffer)->body.elems;
diff --git a/src/types.h b/src/types.h
index 88833b9..8602855 100644
--- a/src/types.h
+++ b/src/types.h
@@ -73,6 +73,7 @@ typedef struct MVMP6bigint MVMP6bigint;
typedef struct MVMP6bigintBody MVMP6bigintBody;
typedef struct MVMP6int MVMP6int;
typedef struct MVMP6intBody MVMP6intBody;
+typedef struct MVMP6intREPRData MVMP6intREPRData;
typedef struct MVMP6num MVMP6num;
typedef struct MVMP6numBody MVMP6numBody;
typedef struct MVMP6opaque MVMP6opaque;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment