Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
First cut of storing/looking things up in the SC. Will need something…
… for putting one assembled at compile time in place too.
  • Loading branch information
jnthn committed Feb 18, 2011
1 parent 0a16798 commit 8b83fce
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
6 changes: 4 additions & 2 deletions build/Makefile.in
Expand Up @@ -159,13 +159,14 @@ METAMODEL_SOURCE = src/metamodel/rakudoobject.h src/metamodel/rakudoobject.c \
src/metamodel/reprs/P6str.h src/metamodel/reprs/P6str.c \
src/metamodel/reprs/P6num.h src/metamodel/reprs/P6num.c \
src/metamodel/multi_dispatch.h src/metamodel/multi_dispatch.c \
src/metamodel/storage_spec.h
src/metamodel/storage_spec.h src/metamodel/serialization_context.c \
src/metamodel/serialization_context.h

METAMODEL_OBJS = ../metamodel/rakudoobject$(O) ../metamodel/repr_registry$(O) \
../metamodel/knowhow_bootstrapper$(O) ../metamodel/reprs/KnowHOWREPR$(O) \
../metamodel/reprs/P6opaque$(O) ../metamodel/reprs/P6int$(O) \
../metamodel/reprs/P6str$(O) ../metamodel/reprs/P6num$(O) \
../metamodel/multi_dispatch$(O)
../metamodel/multi_dispatch$(O) ../metamodel/serialization_context$(O)

HOW_SOURCES = src/metamodel/how/KnowHOWAttribute.pm src/metamodel/how/NQPClassHOW.pm \
src/metamodel/how/NQPNativeHOW.pm src/metamodel/how/NQPAttribute.pm \
Expand Down Expand Up @@ -393,6 +394,7 @@ $(OPS_DIR)/$(OPS)$(LOAD_EXT): $(OPS_DIR)/$(OPS_SOURCE) $(DYNPMC)
cd src/metamodel/reprs && $(CC) -c @cc_o_out@P6str$(O) -I../../../$(PMC_DIR) $(CINCLUDES) $(CFLAGS) P6str.c
cd src/metamodel/reprs && $(CC) -c @cc_o_out@P6num$(O) -I../../../$(PMC_DIR) $(CINCLUDES) $(CFLAGS) P6num.c
cd src/metamodel && $(CC) -c @cc_o_out@multi_dispatch$(O) -I../../$(PMC_DIR) $(CINCLUDES) $(CFLAGS) multi_dispatch.c
cd src/metamodel && $(CC) -c @cc_o_out@serialization_context$(O) -I../../$(PMC_DIR) $(CINCLUDES) $(CFLAGS) serialization_context.c
cd $(OPS_DIR) && $(LD) @ld_out@$(OPS)$(LOAD_EXT) $(OPS)$(O) $(METAMODEL_OBJS) $(LINKARGS)

bootstrap-files: $(STAGE2_PBCS) $(SETTING_NQP)
Expand Down
70 changes: 70 additions & 0 deletions src/metamodel/serialization_context.c
@@ -0,0 +1,70 @@
/* This implements a little bit of support for serialization contexts.
* At the moment, its main role is to let us have a PMC array of objects
* per Parrot bytecode segment. This gives fast access to them. Note
* that if this ever made it into core Parrot, we'd hang it off the
* packfile data structure, or maybe consider whether there's a kind of
* unification with the "constants" segment. */

#define PARROT_IN_EXTENSION
#include "parrot/parrot.h"
#include "parrot/extend.h"

/* Array of root object arrays, one per SC. */
static PMC *sc_root_objects = NULL;

/* Array of bytecode segment locations. Related to the above array.
* Just an array of integers. Can be array of bytecode PMCs later. */
static PMC *sc_index_lookup = NULL;

/* Initializes the stores and register them with the GC. */
static void setup_sc_stores(PARROT_INTERP) {
sc_root_objects = pmc_new(interp, enum_class_ResizablePMCArray);
sc_index_lookup = pmc_new(interp, enum_class_ResizableIntegerArray);
Parrot_pmc_gc_register(interp, sc_root_objects);
Parrot_pmc_gc_register(interp, sc_index_lookup);
}

/* Gets an object from the serialization context associated with the
* current bytecode segment. */
PMC * SC_get_sc_object(PARROT_INTERP, INTVAL idx) {
/* Locate the SC root list for this bytecode segment. */
INTVAL addr = (INTVAL)interp->code;
INTVAL scs = VTABLE_elements(interp, sc_index_lookup);
INTVAL i;
for (i = 0; i < scs; i++) {
if (VTABLE_get_integer_keyed_int(interp, sc_index_lookup, i) == addr) {
/* Found it; grab object we're looking for. */
PMC *sc_root = VTABLE_get_pmc_keyed_int(interp, sc_root_objects, i);
return VTABLE_get_pmc_keyed_int(interp, sc_root, idx);
}
}

/* None found, just return PMCNULL. */
return PMCNULL;
}

/* Puts an object into the serialization context associated with the
* current bytecode segment. */
void SC_set_sc_object(PARROT_INTERP, INTVAL idx, PMC *object) {
/* Locate the SC root list for this bytecode segment. */
INTVAL addr = (INTVAL)interp->code;
INTVAL scs = VTABLE_elements(interp, sc_index_lookup);
PMC * sc_root = PMCNULL;
INTVAL i;
for (i = 0; i < scs; i++) {
if (VTABLE_get_integer_keyed_int(interp, sc_index_lookup, i) == addr) {
sc_root = VTABLE_get_pmc_keyed_int(interp, sc_root_objects, i);
break;
}
}

/* If we found none, add it. */
if (PMC_IS_NULL(sc_root)) {
sc_root = pmc_new(interp, enum_class_ResizablePMCArray);
VTABLE_set_pmc_keyed_int(interp, sc_root_objects, scs, sc_root);
VTABLE_set_integer_keyed_int(interp, sc_index_lookup, scs, addr);
}

/* Stash the value in the the SC. */
VTABLE_set_pmc_keyed_int(interp, sc_root, idx, object);
}
7 changes: 7 additions & 0 deletions src/metamodel/serialization_context.h
@@ -0,0 +1,7 @@
#ifndef SERIALIZATIONCONTEXT_H_GUARD
#define SERIALIZATIONCONTEXT_H_GUARD

PMC * SC_get_sc_object(PARROT_INTERP, INTVAL idx);
void SC_set_sc_object(PARROT_INTERP, INTVAL idx, PMC *object);

#endif
27 changes: 27 additions & 0 deletions src/ops/nqp.ops
Expand Up @@ -11,6 +11,7 @@ BEGIN_OPS_PREAMBLE
#include "../pmc/pmc_dispatchersub.h"
#include "../pmc/pmc_nqpmultisig.h"
#include "../metamodel/multi_dispatch.h"
#include "../metamodel/serialization_context.h"
#include "pmc_sub.h"

/* Did we do the dynop setup yet?
Expand Down Expand Up @@ -636,3 +637,29 @@ inline op stable_publish_vtable_mapping(in PMC, in PMC) :base_core {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use stable_publish_vtable_mapping with a RakudoObject");
}

/*

=item nqp_get_sc_object()

Fetches an object from the current segment's serialization context.

=cut

*/
inline op nqp_get_sc_object(out PMC, in INT) :base_core {
$1 = SC_get_sc_object(interp, $2);
}

/*

=item nqp_set_sc_object()

Stores an object in the current segment's serialization context.

=cut

*/
inline op nqp_set_sc_object(in INT, in PMC) :base_core {
SC_set_sc_object(interp, $1, $2);
}

0 comments on commit 8b83fce

Please sign in to comment.