[svn:parrot] r48378 - branches/dynop_mapping/src/pmc

plobsing at svn.parrot.org plobsing at svn.parrot.org
Tue Aug 10 04:45:13 UTC 2010


Author: plobsing
Date: Tue Aug 10 04:45:12 2010
New Revision: 48378
URL: https://trac.parrot.org/parrot/changeset/48378

Log:
rework OpLib and Opcode to fit new model better.

Modified:
   branches/dynop_mapping/src/pmc/opcode.pmc
   branches/dynop_mapping/src/pmc/oplib.pmc

Modified: branches/dynop_mapping/src/pmc/opcode.pmc
==============================================================================
--- branches/dynop_mapping/src/pmc/opcode.pmc	Tue Aug 10 01:55:41 2010	(r48377)
+++ branches/dynop_mapping/src/pmc/opcode.pmc	Tue Aug 10 04:45:12 2010	(r48378)
@@ -23,19 +23,12 @@
 pmclass Opcode auto_attrs {
     ATTR op_info_t *info;
     ATTR INTVAL op_number;
-    ATTR STRING *full_name_cache;
 
     VTABLE void init() {
         Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
             "Opcode must be created from OpLib.");
     }
 
-    VTABLE void mark() {
-        Parrot_Opcode_attributes * const attrs = PARROT_OPCODE(SELF);
-        if (attrs->full_name_cache)
-            Parrot_gc_mark_STRING_alive(INTERP, attrs->full_name_cache);
-    }
-
     VTABLE void set_pointer(void *i) {
         Parrot_Opcode_attributes * const attrs = PARROT_OPCODE(SELF);
         if (attrs->info)
@@ -44,16 +37,6 @@
         attrs->info = (op_info_t *)i;
     }
 
-    VTABLE void set_string_native(STRING *name) {
-        char * const cstr = Parrot_str_to_cstring(INTERP, name);
-        const INTVAL num = INTERP->op_lib->op_code(INTERP, cstr, 1);
-        Parrot_str_free_cstring(cstr);
-        if (num == -1)
-            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
-                "Opcode: Opcode %S not found", name);
-        VTABLE_set_integer_native(INTERP, SELF, num);
-    }
-
     VTABLE INTVAL get_integer() {
         Parrot_Opcode_attributes * const attrs = PARROT_OPCODE(SELF);
         if (!attrs->info)
@@ -62,27 +45,18 @@
     }
 
     VTABLE void set_integer_native(INTVAL value) {
-        const INTVAL opcount = INTERP->op_lib->op_count;
         Parrot_Opcode_attributes * const attrs = PARROT_OPCODE(SELF);
         if (attrs->info)
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
                 "Opcode has already been initialized");
-        if (value >= opcount || value < 0)
-            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
-                "Opcode: Opcode index %d out of bounds", value);
-        attrs->info      = INTERP->code->op_info_table[value];
         attrs->op_number = value;
     }
 
     VTABLE STRING* get_string() {
         Parrot_Opcode_attributes * const attrs = PARROT_OPCODE(SELF);
-        if (attrs->full_name_cache == NULL) {
-            const char * const name = attrs->info->full_name;
-            const INTVAL len = strlen(name);
-            STRING * const newstr = Parrot_str_new(INTERP, name, len);
-            attrs->full_name_cache = newstr;
-        }
-        return attrs->full_name_cache;
+        const char * const name                = attrs->info->full_name;
+        const INTVAL len                       = strlen(name);
+        return Parrot_str_new(INTERP, name, len);
     }
 
     VTABLE INTVAL elements() {

Modified: branches/dynop_mapping/src/pmc/oplib.pmc
==============================================================================
--- branches/dynop_mapping/src/pmc/oplib.pmc	Tue Aug 10 01:55:41 2010	(r48377)
+++ branches/dynop_mapping/src/pmc/oplib.pmc	Tue Aug 10 04:45:12 2010	(r48378)
@@ -20,46 +20,48 @@
 /* HEADERIZER BEGIN: static */
 /* HEADERIZER END: static */
 
-/* TODO: Since Opcode PMCs are essentially read-only after initialization
-         here, we should cache them. A FixedPMCArray would be okay, an
-         INTVAL->PMC HASH might be better, since it's unlikely that we will
-         need to cache even a majority of the ~1300 ops. */
-static PMC *OPLIB_PMC_INSTANCE;
-static PMC *OPLIB_OPCODE_CACHE;
-pmclass OpLib singleton {
-    void class_init() {
-        OPLIB_PMC_INSTANCE = NULL;
-        OPLIB_OPCODE_CACHE = NULL;
-    }
-
-    VTABLE void *get_pointer() {
-        return OPLIB_PMC_INSTANCE;
-    }
+pmclass OpLib auto_attrs {
+    ATTR op_lib_t *oplib;
 
-    VTABLE void set_pointer(void *ptr) {
-        OPLIB_PMC_INSTANCE = (PMC *)ptr;
+    VTABLE void init() {
+        Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
+                "OpLib must be initialized with an oplib name");
     }
 
-    VTABLE void init() {
-        if (OPLIB_OPCODE_CACHE == NULL) {
-            OPLIB_OPCODE_CACHE = Parrot_pmc_new(INTERP, enum_class_Hash);
-            Parrot_pmc_gc_register(INTERP, OPLIB_OPCODE_CACHE);
+    VTABLE void init_pmc(PMC *name_pmc) {
+        STRING   *name      = VTABLE_get_string(INTERP, name_pmc);
+        char     *name_cstr = Parrot_str_to_cstring(INTERP, name);
+        op_lib_t *oplib     = NULL;
+        int       i;
+
+        for (i = 0; i < INTERP->n_libs; i++) {
+            if (STREQ(name_cstr, INTERP->all_op_libs[i]->name)) {
+                oplib = INTERP->all_op_libs[i];
+                break;
+            }
         }
-        PObj_custom_mark_SET(SELF);
-    }
 
-    VTABLE void mark() {
-        if (OPLIB_OPCODE_CACHE != NULL)
-            Parrot_gc_mark_PMC_alive(INTERP, OPLIB_OPCODE_CACHE);
+        Parrot_str_free_cstring(name_cstr);
+
+        if (!oplib)
+            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_LIBRARY_NOT_LOADED,
+                    "Could not find oplib `%s'");
+
+        SET_ATTR_oplib(INTERP, SELF, oplib);
     }
 
     /* Look up an opnumber given the name of the op. First we look for the
        specific name, then the more general short name. */
     VTABLE INTVAL get_integer_keyed_str(STRING *name) {
-        char * const cstr = Parrot_str_to_cstring(INTERP, name);
-        INTVAL num = INTERP->op_lib->op_code(INTERP, cstr, 1);
+        op_lib_t     *oplib;
+        char * const  cstr = Parrot_str_to_cstring(INTERP, name);
+        INTVAL        num;
+
+        GET_ATTR_oplib(INTERP, SELF, oplib);
+        num  = oplib->op_code(INTERP, cstr, 1);
         if (num == -1)
-            num = INTERP->op_lib->op_code(INTERP, cstr, 0);
+            num = oplib->op_code(INTERP, cstr, 0);
+
         Parrot_str_free_cstring(cstr);
         return num;
     }
@@ -70,17 +72,11 @@
     }
 
     VTABLE PMC* get_pmc_keyed_str(STRING *name) {
-        if (VTABLE_defined_keyed_str(INTERP, OPLIB_OPCODE_CACHE, name)) {
-            PMC * const op = VTABLE_get_pmc_keyed_str(INTERP, OPLIB_OPCODE_CACHE, name);
-            return op;
-        }
-        else {
-            PMC * const op = Parrot_pmc_new_noinit(INTERP, enum_class_Opcode);
-            VTABLE_set_string_native(INTERP, op, name);
-            PObj_custom_mark_SET(op);
-            VTABLE_set_pmc_keyed_str(INTERP, OPLIB_OPCODE_CACHE, name, op);
-            return op;
-        }
+        const INTVAL  num = STATICSELF.get_integer_keyed_str(name);
+        if (num == -1)
+            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
+                "Opcode: Opcode %S not found", name);
+        return STATICSELF.get_pmc_keyed_int(num);
     }
 
     VTABLE PMC* get_pmc_keyed(PMC *key) {
@@ -89,18 +85,25 @@
     }
 
     VTABLE PMC* get_pmc_keyed_int(INTVAL value) {
-        if ((UINTVAL)value >= INTERP->op_lib->op_count)
+        op_lib_t *oplib;
+        GET_ATTR_oplib(INTERP, SELF, oplib);
+        if ((UINTVAL)value >= oplib->op_count ||
+                     value <  0) {
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
-                "OpLib: Opcode index %d out of bounds", value);
+                "OpLib `%s': Opcode index %d out of bounds", oplib->name, value);
+        }
         else {
-            const char * const name   = INTERP->code->op_info_table[value]->full_name;
-            STRING     * const newstr = Parrot_str_new(INTERP, name, 0);
-            return VTABLE_get_pmc_keyed_str(INTERP, SELF, newstr);
+            PMC * const op = Parrot_pmc_new_noinit(INTERP, enum_class_Opcode);
+            VTABLE_set_integer_native(INTERP, op, value);
+            VTABLE_set_pointer(INTERP, op, &oplib->op_info_table[value]);
+            return op;
         }
     }
 
     VTABLE INTVAL elements() {
-        return INTERP->op_lib->op_count;
+        op_lib_t *oplib;
+        GET_ATTR_oplib(INTERP, SELF, oplib);
+        return oplib->op_count;
     }
 
     VTABLE INTVAL get_integer() {
@@ -110,11 +113,14 @@
     METHOD op_family(STRING *shortname)
     {
         char * const sname = Parrot_str_to_cstring(INTERP, shortname);
-        const op_lib_t  * const op_lib = INTERP->op_lib;
-        const op_info_t * const table  = op_lib->op_info_table;
+        op_lib_t  *oplib;
+        op_info_t *table;
         PMC *result = PMCNULL;
         UINTVAL i;
-        for (i = 0; i < op_lib->op_count; ++i) {
+
+        GET_ATTR_oplib(INTERP, SELF, oplib);
+        table = oplib->op_info_table;
+        for (i = 0; i < oplib->op_count; ++i) {
             if (strcmp(table[i].name, sname) == 0) {
                 if (PMC_IS_NULL(result))
                     result = Parrot_pmc_new(INTERP, enum_class_ResizablePMCArray);


More information about the parrot-commits mailing list