Skip to content

Commit

Permalink
Implement lookup of ops by mapped id in PackfileOpMap
Browse files Browse the repository at this point in the history
  • Loading branch information
bacek committed Mar 20, 2011
1 parent 0c4badb commit 0720b34
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/pmc/packfileopmap.pmc
Expand Up @@ -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. */

/*

Expand All @@ -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"));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -173,6 +174,19 @@ Get Op PMC by name.
return STATICSELF.get_pmc_keyed_str(Parrot_key_string(INTERP, key));
}

/*
=item C<get_string_keyed_int()>

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);
}


/*

Expand All @@ -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);
}


Expand All @@ -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);
}

/*
Expand Down
25 changes: 25 additions & 0 deletions t/pmc/packfileopmap.t
Expand Up @@ -29,6 +29,8 @@ Tests the PackfileOpMap PMC.
'sanity'()
'basic'()
'load_lib'()
'reverse_mapping'()

'done_testing'()
.end

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0720b34

Please sign in to comment.