Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
niner committed Nov 28, 2020
1 parent a6118ff commit f530a56
Show file tree
Hide file tree
Showing 14 changed files with 299 additions and 31 deletions.
3 changes: 3 additions & 0 deletions src/6model/bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,9 @@ static void string_consts(MVMThreadContext *tc) {
string_creator(config, "config");
string_creator(replacement, "replacement");
string_creator(dot, ".");
string_creator(comp_unit, "comp_unit");
string_creator(sc, "sc");
string_creator(allowed_sc_deps, "allowed_sc_deps");
}

/* Drives the overall bootstrap process. */
Expand Down
15 changes: 15 additions & 0 deletions src/6model/parametric.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ void MVM_6model_parametric_parameterize(MVMThreadContext *tc, MVMObject *type, M
/* Use an existing parameterization if we have it. */
found = MVM_6model_parametric_try_find_parameterization(tc, st, params);
if (found) {
//fprintf(stderr, "parameterization found %s from %s\n", STABLE(found)->debug_name, MVM_string_utf8_maybe_encode_C_string(tc, MVM_sc_get_description(tc, MVM_sc_get_stable_sc(tc, STABLE(found)))));
/*
found->header.sc_forward_u.sc.sc_idx = 0;
found->header.sc_forward_u.sc.idx = 0;
STABLE(found)->header.sc_forward_u.sc.sc_idx = 0;
STABLE(found)->header.sc_forward_u.sc.idx = 0;
if (STABLE(found)->WHAT) {
STABLE(found)->WHAT->header.sc_forward_u.sc.sc_idx = 0;
STABLE(found)->WHAT->header.sc_forward_u.sc.idx = 0;
}
if (STABLE(found)->HOW) {
STABLE(found)->HOW->header.sc_forward_u.sc.sc_idx = 0;
STABLE(found)->HOW->header.sc_forward_u.sc.idx = 0;
}
*/
result->o = found;
return;
}
Expand Down
21 changes: 20 additions & 1 deletion src/6model/reprs/MVMCompUnit.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static void gc_mark(MVMThreadContext *tc, MVMSTable *st, void *data, MVMGCWorkli
/* Add various other referenced strings, etc. */
MVM_gc_worklist_add(tc, worklist, &body->hll_name);
MVM_gc_worklist_add(tc, worklist, &body->filename);
MVM_gc_worklist_add(tc, worklist, &body->sc);
}

/* Called by the VM in order to free memory associated with this object. */
Expand Down Expand Up @@ -216,12 +217,30 @@ const MVMREPROps * MVMCompUnit_initialize(MVMThreadContext *tc) {
return &MVMCompUnit_this_repr;
}

static void get_attribute(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMObject *class_handle, MVMString *name, MVMint64 hint, MVMRegister *result, MVMuint16 kind) {
if (MVM_string_equal(tc, name, tc->instance->str_consts.sc)) {
MVMCompUnitBody *body = (MVMCompUnitBody *)data;
result->o = (MVMObject*) body->sc;
}
else {
char *c_name = MVM_string_utf8_encode_C_string(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste, "No such attribute '%s' on type %s in a %s", c_name, MVM_6model_get_debug_name(tc, class_handle), MVM_6model_get_stable_debug_name(tc, st));
}
}

static const MVMREPROps MVMCompUnit_this_repr = {
type_object_for,
MVM_gc_allocate_object,
initialize,
copy_to,
MVM_REPR_DEFAULT_ATTR_FUNCS,
{
get_attribute,
MVM_REPR_DEFAULT_BIND_ATTRIBUTE,
MVM_REPR_DEFAULT_HINT_FOR,
MVM_REPR_DEFAULT_IS_ATTRIBUTE_INITIALIZED,
MVM_REPR_DEFAULT_ATTRIBUTE_AS_ATOMIC
},
MVM_REPR_DEFAULT_BOX_FUNCS,
MVM_REPR_DEFAULT_POS_FUNCS,
MVM_REPR_DEFAULT_ASS_FUNCS,
Expand Down
2 changes: 2 additions & 0 deletions src/6model/reprs/MVMCompUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ struct MVMCompUnitBody {

/* Was a frame in this compilation unit invoked yet? */
MVMuint8 invoked;

MVMSerializationContext *sc;
};
struct MVMCompUnit {
MVMObject common;
Expand Down
19 changes: 18 additions & 1 deletion src/6model/reprs/MVMContext.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,29 @@ const MVMREPROps * MVMContext_initialize(MVMThreadContext *tc) {
return &MVMContext_this_repr;
}

static void get_attribute(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMObject *class_handle, MVMString *name, MVMint64 hint, MVMRegister *result, MVMuint16 kind) {
if (MVM_string_equal(tc, name, tc->instance->str_consts.comp_unit)) {
result->o = (MVMObject*)MVM_context_get_frame(tc, (MVMContext*)root)->static_info->body.cu;
}
else {
char *c_name = MVM_string_utf8_encode_C_string(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste, "No such attribute '%s' on type %s in a %s", c_name, MVM_6model_get_debug_name(tc, class_handle), MVM_6model_get_stable_debug_name(tc, st));
}
}

static const MVMREPROps MVMContext_this_repr = {
type_object_for,
MVM_gc_allocate_object,
NULL, /* initialize */
copy_to,
MVM_REPR_DEFAULT_ATTR_FUNCS,
{
get_attribute,
MVM_REPR_DEFAULT_BIND_ATTRIBUTE,
MVM_REPR_DEFAULT_HINT_FOR,
MVM_REPR_DEFAULT_IS_ATTRIBUTE_INITIALIZED,
MVM_REPR_DEFAULT_ATTRIBUTE_AS_ATOMIC
},
MVM_REPR_DEFAULT_BOX_FUNCS,
MVM_REPR_DEFAULT_POS_FUNCS,
{
Expand Down
26 changes: 24 additions & 2 deletions src/6model/reprs/SCRef.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ static void SCRef_gc_mark(MVMThreadContext *tc, MVMSTable *st, void *data, MVMGC
MVMSerializationContextBody *sc = ((MVMSerializationContextBody **)data)[0];
MVMuint64 i;

//if (strcmp(MVM_string_utf8_encode_C_string(tc, sc->handle), "B236B1F0C35FA99DD97C3031E2E8A87B0E00EB59") == 0)
// fprintf(stderr, "Marking the SC\n");

MVM_gc_worklist_add(tc, worklist, &sc->handle);
MVM_gc_worklist_add(tc, worklist, &sc->description);
MVM_gc_worklist_add(tc, worklist, &sc->root_codes);
Expand Down Expand Up @@ -105,8 +108,9 @@ static void SCRef_gc_mark(MVMThreadContext *tc, MVMSTable *st, void *data, MVMGC
MVM_gc_worklist_add(tc, worklist, &(sc->sr->root.string_comp_unit));
MVM_gc_worklist_add(tc, worklist, &(sc->sr->codes_list));
MVM_gc_worklist_add(tc, worklist, &(sc->sr->current_object));

}

MVM_gc_worklist_add(tc, worklist, &sc->allowed_sc_deps);
}

/* Called by the VM in order to free memory associated with this object. */
Expand Down Expand Up @@ -231,12 +235,30 @@ const MVMREPROps * MVMSCRef_initialize(MVMThreadContext *tc) {
return &SCRef_this_repr;
}

static void bind_attribute(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMObject *class_handle, MVMString *name, MVMint64 hint, MVMRegister value, MVMuint16 kind) {
if (MVM_string_equal(tc, name, tc->instance->str_consts.allowed_sc_deps)) {
MVMSerializationContextBody *sc = ((MVMSerializationContextBody **)data)[0];
MVM_ASSIGN_REF(tc, &(root->header), sc->allowed_sc_deps, value.o);
}
else {
char *c_name = MVM_string_utf8_encode_C_string(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste, "No such attribute '%s' on type %s in a %s", c_name, MVM_6model_get_debug_name(tc, class_handle), MVM_6model_get_stable_debug_name(tc, st));
}
}

static const MVMREPROps SCRef_this_repr = {
type_object_for,
MVM_gc_allocate_object,
initialize,
copy_to,
MVM_REPR_DEFAULT_ATTR_FUNCS,
{
MVM_REPR_DEFAULT_GET_ATTRIBUTE,
bind_attribute,
MVM_REPR_DEFAULT_HINT_FOR,
MVM_REPR_DEFAULT_IS_ATTRIBUTE_INITIALIZED,
MVM_REPR_DEFAULT_ATTRIBUTE_AS_ATOMIC
},
MVM_REPR_DEFAULT_BOX_FUNCS,
MVM_REPR_DEFAULT_POS_FUNCS,
MVM_REPR_DEFAULT_ASS_FUNCS,
Expand Down
2 changes: 2 additions & 0 deletions src/6model/reprs/SCRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ struct MVMSerializationContextBody {

/* Reentrant mutex protecting lazy deserialization of the SC. */
MVMObject *mutex;

MVMObject *allowed_sc_deps;
};

struct MVMSerializationContext {
Expand Down
56 changes: 53 additions & 3 deletions src/6model/sc.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ MVMObject * MVM_sc_create(MVMThreadContext *tc, MVMString *handle) {
= MVM_str_hash_lvalue_fetch_nocheck(tc, &tc->instance->sc_weakhash, handle);
if (!entry->hash_handle.key) {
entry->hash_handle.key = handle;
//fprintf(stderr, "Created SC for %s\n", MVM_string_utf8_encode_C_string(tc, handle));

MVMSerializationContextBody *scb = MVM_calloc(1, sizeof(MVMSerializationContextBody));
entry->scb = scb;
Expand All @@ -41,6 +42,7 @@ MVMObject * MVM_sc_create(MVMThreadContext *tc, MVMString *handle) {
MVM_sc_add_all_scs_entry(tc, scb);
}
else {
//fprintf(stderr, "Found existing SC for %s\n", MVM_string_utf8_encode_C_string(tc, handle));
MVMSerializationContextBody *scb = entry->scb;
if (scb->sc) {
/* we lost a race to create it! */
Expand Down Expand Up @@ -98,7 +100,7 @@ MVMString * MVM_sc_get_handle(MVMThreadContext *tc, MVMSerializationContext *sc)

/* Given an SC, returns its description. */
MVMString * MVM_sc_get_description(MVMThreadContext *tc, MVMSerializationContext *sc) {
return sc->body->description;
return sc ? sc->body->description : NULL;
}

/* Given an SC, sets its description. */
Expand Down Expand Up @@ -212,10 +214,19 @@ MVMuint8 MVM_sc_is_object_immediately_available(MVMThreadContext *tc, MVMSeriali
MVMObject * MVM_sc_get_object(MVMThreadContext *tc, MVMSerializationContext *sc, MVMint64 idx) {
MVMObject **roots = sc->body->root_objects;
MVMint64 count = sc->body->num_objects;
if (MVM_LIKELY(idx >= 0 && idx < count))
if (MVM_LIKELY(idx >= 0 && idx < count)) {
if (roots[idx] && !sc_working(sc)) {
//fprintf(stderr, "Already deserialized: %p\n", roots[idx]);
return roots[idx];
}
else {
MVM_serialization_demand_object(tc, sc, idx);
//fprintf(stderr, "Deserialized %p\n", obj);
}
return roots[idx] && !sc_working(sc)
? roots[idx]
: MVM_serialization_demand_object(tc, sc, idx);
}
else {
char *c_description = MVM_string_utf8_encode_C_string(tc, sc->body->description);
char *waste[] = { c_description, NULL };
Expand Down Expand Up @@ -248,6 +259,11 @@ void MVM_sc_set_object(MVMThreadContext *tc, MVMSerializationContext *sc, MVMint
void MVM_sc_set_object_no_update(MVMThreadContext *tc, MVMSerializationContext *sc, MVMint64 idx, MVMObject *obj) {
if (idx < 0)
MVM_exception_throw_adhoc(tc, "Invalid (negative) object root index %"PRId64"", idx);
if (IS_CONCRETE(obj) && !REPR(obj)->serialize) {
MVM_gc_allocate_gen2_default_clear(tc);
MVM_exception_throw_adhoc(tc,
"Missing serialize REPR function for REPR %s (%s)", REPR(obj)->name, MVM_6model_get_debug_name(tc, obj));
}
if ((MVMuint64)idx < sc->body->num_objects) {
/* Just updating an existing one. */
MVM_ASSIGN_REF(tc, &(sc->common.header), sc->body->root_objects[idx], obj);
Expand Down Expand Up @@ -367,6 +383,8 @@ void MVM_sc_disclaim(MVMThreadContext *tc, MVMSerializationContext *sc) {
MVM_exception_throw_adhoc(tc,
"Must provide an SCRef operand to scdisclaim");

//fprintf(stderr, "sc_disclaim for %s\n", MVM_string_utf8_encode_C_string(tc, sc->body->handle));

root_objects = sc->body->root_objects;
count = sc->body->num_objects;
for (i = 0; i < count; i++) {
Expand Down Expand Up @@ -405,7 +423,35 @@ void MVM_sc_disclaim(MVMThreadContext *tc, MVMSerializationContext *sc) {
col = &obj->header;
col->sc_forward_u.sc.sc_idx = 0;
}
sc->body->root_codes = NULL;
MVM_repr_pos_set_elems(tc, root_codes, 0);

/* clean up repossession info as we don't own anything anymore */
MVM_repr_pos_set_elems(tc, sc->body->rep_indexes, 0);
MVM_repr_pos_set_elems(tc, sc->body->rep_scs, 0);

int type_index;
for (type_index = 0; type_index < 4; type_index++) {
if (tc->instance->int_const_cache->types[type_index] != NULL) {
for (i = 0; i < 16; i++) {
obj = tc->instance->int_const_cache->cache[type_index][i];
col = &obj->header;
if (col->sc_forward_u.sc.sc_idx == sc->body->sc_idx) {
#ifdef MVM_USE_OVERFLOW_SERIALIZATION_INDEX
if (col->flags1 & MVM_CF_SERIALZATION_INDEX_ALLOCATED) {
struct MVMSerializationIndex *const sci = col->sc_forward_u.sci;
col->sc_forward_u.sci = NULL;
MVM_free(sci);
}
col->sc_forward_u.sc.sc_idx = 0;
col->sc_forward_u.sc.idx = 0;
#else
col->sc_forward_u.sc.sc_idx = 0;
col->sc_forward_u.sc.idx = 0;
#endif
}
}
}
}
}

/* SC repossession barrier. */
Expand Down Expand Up @@ -469,6 +515,10 @@ void MVM_sc_wb_hit_obj(MVMThreadContext *tc, MVMObject *obj) {
MVM_repr_push_i(tc, comp_sc->body->rep_indexes, new_slot << 1);
MVM_repr_push_o(tc, comp_sc->body->rep_scs, (MVMObject *)MVM_sc_get_obj_sc(tc, obj));

if (0 == strcmp(STABLE(obj)->debug_name, "Stash")) {
fprintf(stderr, "Repossessing a stash %p\n", obj);
}

/* Update SC of the object, claiming it, and update index too. */
MVM_sc_set_obj_sc(tc, obj, comp_sc);
MVM_sc_set_idx_in_sc(&(obj->header), new_slot);
Expand Down
Loading

0 comments on commit f530a56

Please sign in to comment.