Skip to content

Commit

Permalink
teach the 6model reprs about varints
Browse files Browse the repository at this point in the history
this currently segfaults upon
deserialization while building nqp.
  • Loading branch information
timo committed Jan 26, 2014
1 parent e51cf28 commit ce705b0
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 66 deletions.
20 changes: 10 additions & 10 deletions src/6model/reprs/MVMArray.c
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ static void deserialize(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, vo
MVMArrayBody *body = (MVMArrayBody *)data;
MVMint64 i;

body->elems = reader->read_int(tc, reader);
body->elems = reader->read_varint(tc, reader);
body->ssize = body->elems;
if (body->ssize)
body->slots.any = malloc(body->ssize * repr_data->elem_size);
Expand All @@ -898,16 +898,16 @@ static void deserialize(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, vo
MVM_ASSIGN_REF(tc, root, body->slots.s[i], reader->read_str(tc, reader));
break;
case MVM_ARRAY_I64:
body->slots.i64[i] = reader->read_int(tc, reader);
body->slots.i64[i] = reader->read_varint(tc, reader);
break;
case MVM_ARRAY_I32:
body->slots.i32[i] = (MVMint32)reader->read_int(tc, reader);
body->slots.i32[i] = (MVMint32)reader->read_varint(tc, reader);
break;
case MVM_ARRAY_I16:
body->slots.i16[i] = (MVMint16)reader->read_int(tc, reader);
body->slots.i16[i] = (MVMint16)reader->read_varint(tc, reader);
break;
case MVM_ARRAY_I8:
body->slots.i8[i] = (MVMint8)reader->read_int(tc, reader);
body->slots.i8[i] = (MVMint8)reader->read_varint(tc, reader);
break;
case MVM_ARRAY_N64:
body->slots.n64[i] = reader->read_num(tc, reader);
Expand All @@ -926,7 +926,7 @@ static void serialize(MVMThreadContext *tc, MVMSTable *st, void *data, MVMSerial
MVMArrayBody *body = (MVMArrayBody *)data;
MVMint64 i;

writer->write_int(tc, writer, body->elems);
writer->write_varint(tc, writer, body->elems);
for (i = 0; i < body->elems; i++) {
switch (repr_data->slot_type) {
case MVM_ARRAY_OBJ:
Expand All @@ -936,16 +936,16 @@ static void serialize(MVMThreadContext *tc, MVMSTable *st, void *data, MVMSerial
writer->write_str(tc, writer, body->slots.s[body->start + i]);
break;
case MVM_ARRAY_I64:
writer->write_int(tc, writer, (MVMint64)body->slots.i64[body->start + i]);
writer->write_varint(tc, writer, (MVMint64)body->slots.i64[body->start + i]);
break;
case MVM_ARRAY_I32:
writer->write_int(tc, writer, (MVMint64)body->slots.i32[body->start + i]);
writer->write_varint(tc, writer, (MVMint64)body->slots.i32[body->start + i]);
break;
case MVM_ARRAY_I16:
writer->write_int(tc, writer, (MVMint64)body->slots.i16[body->start + i]);
writer->write_varint(tc, writer, (MVMint64)body->slots.i16[body->start + i]);
break;
case MVM_ARRAY_I8:
writer->write_int(tc, writer, (MVMint64)body->slots.i8[body->start + i]);
writer->write_varint(tc, writer, (MVMint64)body->slots.i8[body->start + i]);
break;
case MVM_ARRAY_N64:
writer->write_num(tc, writer, (MVMnum64)body->slots.n64[body->start + i]);
Expand Down
28 changes: 14 additions & 14 deletions src/6model/reprs/NFA.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,33 @@ static void serialize(MVMThreadContext *tc, MVMSTable *st, void *data, MVMSerial
writer->write_ref(tc, writer, body->fates);

/* Write number of states. */
writer->write_int(tc, writer, body->num_states);
writer->write_varint(tc, writer, body->num_states);

/* Write state edge list counts. */
for (i = 0; i < body->num_states; i++)
writer->write_int(tc, writer, body->num_state_edges[i]);
writer->write_varint(tc, writer, body->num_state_edges[i]);

/* Write state graph. */
for (i = 0; i < body->num_states; i++) {
for (j = 0; j < body->num_state_edges[i]; j++) {
writer->write_int(tc, writer, body->states[i][j].act);
writer->write_int(tc, writer, body->states[i][j].to);
writer->write_varint(tc, writer, body->states[i][j].act);
writer->write_varint(tc, writer, body->states[i][j].to);
switch (body->states[i][j].act) {
case MVM_NFA_EDGE_FATE:
case MVM_NFA_EDGE_CODEPOINT:
case MVM_NFA_EDGE_CODEPOINT_NEG:
case MVM_NFA_EDGE_CHARCLASS:
case MVM_NFA_EDGE_CHARCLASS_NEG:
writer->write_int(tc, writer, body->states[i][j].arg.i);
writer->write_varint(tc, writer, body->states[i][j].arg.i);
break;
case MVM_NFA_EDGE_CHARLIST:
case MVM_NFA_EDGE_CHARLIST_NEG:
writer->write_str(tc, writer, body->states[i][j].arg.s);
break;
case MVM_NFA_EDGE_CODEPOINT_I:
case MVM_NFA_EDGE_CODEPOINT_I_NEG: {
writer->write_int(tc, writer, body->states[i][j].arg.uclc.lc);
writer->write_int(tc, writer, body->states[i][j].arg.uclc.uc);
writer->write_varint(tc, writer, body->states[i][j].arg.uclc.lc);
writer->write_varint(tc, writer, body->states[i][j].arg.uclc.uc);
break;
}
}
Expand All @@ -118,13 +118,13 @@ static void deserialize(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, vo
body->fates = reader->read_ref(tc, reader);

/* Read number of states. */
body->num_states = reader->read_int(tc, reader);
body->num_states = reader->read_varint(tc, reader);

if (body->num_states > 0) {
/* Read state edge list counts. */
body->num_state_edges = malloc(body->num_states * sizeof(MVMint64));
for (i = 0; i < body->num_states; i++)
body->num_state_edges[i] = reader->read_int(tc, reader);
body->num_state_edges[i] = reader->read_varint(tc, reader);

/* Read state graph. */
body->states = malloc(body->num_states * sizeof(MVMNFAStateInfo *));
Expand All @@ -133,24 +133,24 @@ static void deserialize(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, vo
if (edges > 0)
body->states[i] = malloc(edges * sizeof(MVMNFAStateInfo));
for (j = 0; j < edges; j++) {
body->states[i][j].act = reader->read_int(tc, reader);
body->states[i][j].to = reader->read_int(tc, reader);
body->states[i][j].act = reader->read_varint(tc, reader);
body->states[i][j].to = reader->read_varint(tc, reader);
switch (body->states[i][j].act) {
case MVM_NFA_EDGE_FATE:
case MVM_NFA_EDGE_CODEPOINT:
case MVM_NFA_EDGE_CODEPOINT_NEG:
case MVM_NFA_EDGE_CHARCLASS:
case MVM_NFA_EDGE_CHARCLASS_NEG:
body->states[i][j].arg.i = reader->read_int(tc, reader);
body->states[i][j].arg.i = reader->read_varint(tc, reader);
break;
case MVM_NFA_EDGE_CHARLIST:
case MVM_NFA_EDGE_CHARLIST_NEG:
MVM_ASSIGN_REF(tc, root, body->states[i][j].arg.s, reader->read_str(tc, reader));
break;
case MVM_NFA_EDGE_CODEPOINT_I:
case MVM_NFA_EDGE_CODEPOINT_I_NEG: {
body->states[i][j].arg.uclc.lc = reader->read_int(tc, reader);
body->states[i][j].arg.uclc.uc = reader->read_int(tc, reader);
body->states[i][j].arg.uclc.lc = reader->read_varint(tc, reader);
body->states[i][j].arg.uclc.uc = reader->read_varint(tc, reader);
break;
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/6model/reprs/P6int.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,22 @@ static void deserialize_stable_size(MVMThreadContext *tc, MVMSTable *st, MVMSeri
/* Serializes the REPR data. */
static void serialize_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMSerializationWriter *writer) {
MVMP6intREPRData *repr_data = (MVMP6intREPRData *)st->REPR_data;
writer->write_int16(tc, writer, repr_data->bits);
writer->write_int16(tc, writer, repr_data->is_unsigned);
writer->write_varint(tc, writer, repr_data->bits);
writer->write_varint(tc, writer, repr_data->is_unsigned);
}

/* Deserializes representation data. */
static void deserialize_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) {
MVMP6intREPRData *repr_data = (MVMP6intREPRData *)malloc(sizeof(MVMP6intREPRData));

if (reader->root.version >= 8) {
repr_data->bits = reader->read_int16(tc, reader);
repr_data->is_unsigned = reader->read_int16(tc, reader);
if (reader->root.version >= 9) {
repr_data->bits = reader->read_varint(tc, reader);
repr_data->is_unsigned = reader->read_varint(tc, reader);
} else {
repr_data->bits = reader->read_int16(tc, reader);
repr_data->is_unsigned = reader->read_int16(tc, reader);
}
if (repr_data->bits != 1 && repr_data->bits != 2 && repr_data->bits != 4 && repr_data->bits != 8
&& repr_data->bits != 16 && repr_data->bits != 32 && repr_data->bits != 64)
MVM_exception_throw_adhoc(tc, "MVMP6int: Unsupported int size (%dbit)", repr_data->bits);
Expand All @@ -123,11 +128,11 @@ static void deserialize_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMSerial
}

static void deserialize(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMSerializationReader *reader) {
((MVMP6intBody *)data)->value = reader->read_int(tc, reader);
((MVMP6intBody *)data)->value = reader->read_varint(tc, reader);
}

static void serialize(MVMThreadContext *tc, MVMSTable *st, void *data, MVMSerializationWriter *writer) {
writer->write_int(tc, writer, ((MVMP6intBody *)data)->value);
writer->write_varint(tc, writer, ((MVMP6intBody *)data)->value);
}

/* Initializes the representation. */
Expand Down
7 changes: 5 additions & 2 deletions src/6model/reprs/P6num.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,18 @@ static void deserialize_stable_size(MVMThreadContext *tc, MVMSTable *st, MVMSeri
/* Serializes the REPR data. */
static void serialize_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMSerializationWriter *writer) {
MVMP6numREPRData *repr_data = (MVMP6numREPRData *)st->REPR_data;
writer->write_int16(tc, writer, repr_data->bits);
writer->write_varint(tc, writer, repr_data->bits);
}

/* Deserializes representation data. */
static void deserialize_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) {
MVMP6numREPRData *repr_data = (MVMP6numREPRData *)malloc(sizeof(MVMP6numREPRData));

if (reader->root.version >= 8) {
repr_data->bits = reader->read_int16(tc, reader);
if (reader->root.version >= 9)
repr_data->bits = reader->read_varint(tc, reader);
else
repr_data->bits = reader->read_int16(tc, reader);
if (repr_data->bits != 1 && repr_data->bits != 2 && repr_data->bits != 4 && repr_data->bits != 8
&& repr_data->bits != 16 && repr_data->bits != 32 && repr_data->bits != 64)
MVM_exception_throw_adhoc(tc, "MVMP6num: Unsupported int size (%dbit)", repr_data->bits);
Expand Down
76 changes: 42 additions & 34 deletions src/6model/reprs/P6opaque.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,11 +836,11 @@ static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info_hash) {
static void deserialize_stable_size(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) {
/* To calculate size, we need number of attributes and to know about
* anything flattend in. */
MVMint64 num_attributes = reader->read_int(tc, reader);
MVMint64 num_attributes = reader->read_varint(tc, reader);
MVMuint32 cur_offset = 0;
MVMint64 i;
for (i = 0; i < num_attributes; i++) {
if (reader->read_int(tc, reader)) {
if (reader->read_varint(tc, reader)) {
MVMSTable *st = reader->read_stable_ref(tc, reader);
MVMStorageSpec ss = st->REPR->get_storage_spec(tc, st);
if (ss.inlineable)
Expand Down Expand Up @@ -869,60 +869,60 @@ static void serialize_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMSerializ
MVM_exception_throw_adhoc(tc,
"Representation must be composed before it can be serialized");

writer->write_int(tc, writer, repr_data->num_attributes);
writer->write_varint(tc, writer, repr_data->num_attributes);

for (i = 0; i < repr_data->num_attributes; i++) {
writer->write_int(tc, writer, repr_data->flattened_stables[i] != NULL);
writer->write_varint(tc, writer, repr_data->flattened_stables[i] != NULL);
if (repr_data->flattened_stables[i])
writer->write_stable_ref(tc, writer, repr_data->flattened_stables[i]);
}

writer->write_int(tc, writer, repr_data->mi);
writer->write_varint(tc, writer, repr_data->mi);

if (repr_data->auto_viv_values) {
writer->write_int(tc, writer, 1);
writer->write_varint(tc, writer, 1);
for (i = 0; i < repr_data->num_attributes; i++)
writer->write_ref(tc, writer, repr_data->auto_viv_values[i]);
}
else {
writer->write_int(tc, writer, 0);
writer->write_varint(tc, writer, 0);
}

writer->write_int(tc, writer, repr_data->unbox_int_slot);
writer->write_int(tc, writer, repr_data->unbox_num_slot);
writer->write_int(tc, writer, repr_data->unbox_str_slot);
writer->write_varint(tc, writer, repr_data->unbox_int_slot);
writer->write_varint(tc, writer, repr_data->unbox_num_slot);
writer->write_varint(tc, writer, repr_data->unbox_str_slot);

if (repr_data->unbox_slots) {
writer->write_int(tc, writer, 1);
writer->write_varint(tc, writer, 1);
for (i = 0; i < repr_data->num_attributes; i++) {
writer->write_int(tc, writer, repr_data->unbox_slots[i].repr_id);
writer->write_int(tc, writer, repr_data->unbox_slots[i].slot);
writer->write_varint(tc, writer, repr_data->unbox_slots[i].repr_id);
writer->write_varint(tc, writer, repr_data->unbox_slots[i].slot);
}
}
else {
writer->write_int(tc, writer, 0);
writer->write_varint(tc, writer, 0);
}

i = 0;
while (repr_data->name_to_index_mapping[i].class_key)
i++;
num_classes = i;
writer->write_int(tc, writer, num_classes);
writer->write_varint(tc, writer, num_classes);
for (i = 0; i < num_classes; i++) {
const MVMuint32 num_attrs = repr_data->name_to_index_mapping[i].num_attrs;
MVMuint32 j;
writer->write_ref(tc, writer, repr_data->name_to_index_mapping[i].class_key);
writer->write_int16(tc, writer, REFVAR_VM_HASH_STR_VAR);
writer->write_int32(tc, writer, num_attrs);
writer->write_varint(tc, writer, REFVAR_VM_HASH_STR_VAR);
writer->write_varint(tc, writer, num_attrs);
for (j = 0; j < num_attrs; j++) {
MVM_repr_set_int(tc, slot, repr_data->name_to_index_mapping[i].slots[j]);
writer->write_str(tc, writer, repr_data->name_to_index_mapping[i].names[j]);
writer->write_ref(tc, writer, slot);
}
}

writer->write_int(tc, writer, repr_data->pos_del_slot);
writer->write_int(tc, writer, repr_data->ass_del_slot);
writer->write_varint(tc, writer, repr_data->pos_del_slot);
writer->write_varint(tc, writer, repr_data->ass_del_slot);
}

/* Deserializes representation data. */
Expand All @@ -932,45 +932,53 @@ static void deserialize_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMSerial

MVMP6opaqueREPRData *repr_data = calloc(1, sizeof(MVMP6opaqueREPRData));

repr_data->num_attributes = (MVMuint16)reader->read_int(tc, reader);
repr_data->num_attributes = (MVMuint16)reader->read_varint(tc, reader);

repr_data->flattened_stables = (MVMSTable **)malloc(P6OMAX(repr_data->num_attributes, 1) * sizeof(MVMSTable *));
for (i = 0; i < repr_data->num_attributes; i++)
if (reader->read_int(tc, reader)) {
if (reader->read_varint(tc, reader)) {
MVM_ASSIGN_REF(tc, st, repr_data->flattened_stables[i], reader->read_stable_ref(tc, reader));
}
else {
repr_data->flattened_stables[i] = NULL;
}

repr_data->mi = reader->read_int(tc, reader);
repr_data->mi = reader->read_varint(tc, reader);

if (reader->read_int(tc, reader)) {
if (reader->read_varint(tc, reader)) {
repr_data->auto_viv_values = (MVMObject **)malloc(P6OMAX(repr_data->num_attributes, 1) * sizeof(MVMObject *));
for (i = 0; i < repr_data->num_attributes; i++)
MVM_ASSIGN_REF(tc, st, repr_data->auto_viv_values[i], reader->read_ref(tc, reader));
}

repr_data->unbox_int_slot = reader->read_int(tc, reader);
repr_data->unbox_num_slot = reader->read_int(tc, reader);
repr_data->unbox_str_slot = reader->read_int(tc, reader);
repr_data->unbox_int_slot = reader->read_varint(tc, reader);
repr_data->unbox_num_slot = reader->read_varint(tc, reader);
repr_data->unbox_str_slot = reader->read_varint(tc, reader);

if (reader->read_int(tc, reader)) {
if (reader->read_varint(tc, reader)) {
repr_data->unbox_slots = (MVMP6opaqueBoxedTypeMap *)malloc(P6OMAX(repr_data->num_attributes, 1) * sizeof(MVMP6opaqueBoxedTypeMap));
for (i = 0; i < repr_data->num_attributes; i++) {
repr_data->unbox_slots[i].repr_id = reader->read_int(tc, reader);
repr_data->unbox_slots[i].slot = reader->read_int(tc, reader);
repr_data->unbox_slots[i].repr_id = reader->read_varint(tc, reader);
repr_data->unbox_slots[i].slot = reader->read_varint(tc, reader);
}
}

num_classes = (MVMuint16)reader->read_int(tc, reader);
num_classes = (MVMuint16)reader->read_varint(tc, reader);
repr_data->name_to_index_mapping = (MVMP6opaqueNameMap *)calloc(1, (num_classes + 1) * sizeof(MVMP6opaqueNameMap));
for (i = 0; i < num_classes; i++) {
MVMint32 num_attrs = 0;
MVM_ASSIGN_REF(tc, st, repr_data->name_to_index_mapping[i].class_key,
reader->read_ref(tc, reader));
if (reader->read_int16(tc, reader) == REFVAR_VM_HASH_STR_VAR) {
num_attrs = reader->read_int32(tc, reader);
MVMint64 refvar_value;
if (reader->root.version >= 9)
refvar_value = reader->read_varint(tc, reader);
else
refvar_value = reader->read_int16(tc, reader);
if (refvar_value == REFVAR_VM_HASH_STR_VAR) {
if (reader->root.version >= 9)
num_attrs = reader->read_varint(tc, reader);
else
num_attrs = reader->read_int32(tc, reader);
repr_data->name_to_index_mapping[i].names = (MVMString **)malloc(P6OMAX(num_attrs, 1) * sizeof(MVMString *));
repr_data->name_to_index_mapping[i].slots = (MVMuint16 *)malloc(P6OMAX(num_attrs, 1) * sizeof(MVMuint16));
for (j = 0; j < num_attrs; j++) {
Expand All @@ -983,8 +991,8 @@ static void deserialize_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMSerial
repr_data->name_to_index_mapping[i].num_attrs = num_attrs;
}

repr_data->pos_del_slot = (MVMint16)reader->read_int(tc, reader);
repr_data->ass_del_slot = (MVMint16)reader->read_int(tc, reader);
repr_data->pos_del_slot = (MVMint16)reader->read_varint(tc, reader);
repr_data->ass_del_slot = (MVMint16)reader->read_varint(tc, reader);

/* Re-calculate the remaining info, which is platform specific or
* derived information. */
Expand Down

0 comments on commit ce705b0

Please sign in to comment.