From 0720b34f123b8277cb25b936af560fce70510ed7 Mon Sep 17 00:00:00 2001 From: Vasily Chekalkin Date: Sun, 20 Mar 2011 19:19:49 +1100 Subject: [PATCH] Implement lookup of ops by mapped id in PackfileOpMap --- src/pmc/packfileopmap.pmc | 39 +++++++++++++++++++++++++++++---------- t/pmc/packfileopmap.t | 25 +++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/pmc/packfileopmap.pmc b/src/pmc/packfileopmap.pmc index f7240ef4d8..b9e199a229 100644 --- a/src/pmc/packfileopmap.pmc +++ b/src/pmc/packfileopmap.pmc @@ -28,9 +28,9 @@ libraries that the ops come from. pmclass PackfileOpMap auto_attrs { - ATTR PMC *op_maps; /* RPA of Hashes of op lib maps */ - ATTR PMC *map_cache; /* Hash mapping full op names to numbers */ - ATTR INTVAL op_count; /* number of mapped ops*/ + ATTR PMC *op_maps; /* RPA of Hashes of op lib maps */ + ATTR PMC *map_cache; /* Hash mapping full op names to numbers */ + ATTR PMC *ops; /* RSA of mapped ops. */ /* @@ -51,7 +51,7 @@ Create empty PackfileOpMap for a given op library. attrs->op_maps = Parrot_pmc_new(INTERP, enum_class_ResizablePMCArray); attrs->map_cache = Parrot_pmc_new(INTERP, enum_class_Hash); - attrs->op_count = 0; + attrs->ops = Parrot_pmc_new(INTERP, enum_class_ResizableStringArray); core_ops_pmc = Parrot_pmc_new(INTERP, enum_class_String); VTABLE_set_string_native(INTERP, core_ops_pmc, CONST_STRING(INTERP, "core_ops")); @@ -84,7 +84,7 @@ already in the map, a new map will be created. VTABLE INTVAL get_integer_keyed(PMC *key) { STRING *key_str, *oplib_str; - PMC *map_cache, *op_maps; + PMC *map_cache, *op_maps, *ops; INTVAL op_map_count, i; GET_ATTR_map_cache(INTERP, SELF, map_cache); @@ -118,13 +118,14 @@ already in the map, a new map will be created. table_ops = VTABLE_get_pmc_keyed_str(INTERP, op_map, table_ops_str); lib_ops = VTABLE_get_pmc_keyed_str(INTERP, op_map, lib_ops_str); - GET_ATTR_op_count(INTERP, SELF, op_count); + GET_ATTR_ops(INTERP, SELF, ops); + op_count = VTABLE_elements(INTERP, ops); VTABLE_set_integer_keyed(INTERP, map_cache, key, op_count); VTABLE_push_integer(INTERP, table_ops, op_count); VTABLE_push_integer(INTERP, lib_ops, op_num); - SET_ATTR_op_count(INTERP, SELF, op_count + 1); + VTABLE_push_string(INTERP, ops, key_str); return op_count; } @@ -173,6 +174,19 @@ Get Op PMC by name. return STATICSELF.get_pmc_keyed_str(Parrot_key_string(INTERP, key)); } +/* +=item C + +Lookup op name by mapped id. + +=cut +*/ + VTABLE STRING* get_string_keyed_int(INTVAL key) { + PMC *ops; + GET_ATTR_ops(INTERP, SELF, ops); + return VTABLE_get_string_keyed_int(INTERP, ops, key); + } + /* @@ -186,10 +200,15 @@ Marks the object as live. VTABLE void mark() { PMC *tmp; + GET_ATTR_op_maps(INTERP, SELF, tmp); Parrot_gc_mark_PMC_alive(INTERP, tmp); + GET_ATTR_map_cache(INTERP, SELF, tmp); Parrot_gc_mark_PMC_alive(INTERP, tmp); + + GET_ATTR_ops(INTERP, SELF, tmp); + Parrot_gc_mark_PMC_alive(INTERP, tmp); } @@ -204,9 +223,9 @@ Get the number of filename mappings. */ VTABLE INTVAL get_integer() { - INTVAL op_count; - GET_ATTR_op_count(INTERP, SELF, op_count); - return op_count; + PMC *ops; + GET_ATTR_ops(INTERP, SELF, ops); + return VTABLE_elements(INTERP, ops); } /* diff --git a/t/pmc/packfileopmap.t b/t/pmc/packfileopmap.t index 9134f3dcc9..4e442951de 100644 --- a/t/pmc/packfileopmap.t +++ b/t/pmc/packfileopmap.t @@ -29,6 +29,8 @@ Tests the PackfileOpMap PMC. 'sanity'() 'basic'() 'load_lib'() + 'reverse_mapping'() + 'done_testing'() .end @@ -125,6 +127,29 @@ Tests the PackfileOpMap PMC. .return () .end +.sub 'reverse_mapping' + .local pmc opmap + opmap = new ['PackfileOpMap'] + + # Map few ops. + $I0 = opmap['say_sc'] + $I1 = opmap['returncc'] + $I2 = opmap['issame_i_p_p'] + $I3 = opmap['cmp_i_i_i'] + + $S0 = opmap[$I0] + is( $S0, "say_sc", "say_sc") + + $S0 = opmap[$I1] + is( $S0, "returncc", "returncc") + + $S0 = opmap[$I2] + is( $S0, "issame_i_p_p", "issame_i_p_p") + + $S0 = opmap[$I3] + is( $S0, "cmp_i_i_i", "cmp_i_i_i") + +.end # Local Variables: # mode: pir