~ubuntu-branches/ubuntu/vivid/nqp/vivid-proposed

« back to all changes in this revision

Viewing changes to src/pmc/nqplexinfo.pmc

  • Committer: Package Import Robot
  • Author(s): Alessandro Ghedini
  • Date: 2013-11-01 12:09:18 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20131101120918-kx51sl0sxl3exsxi
Tags: 2013.10-1
* New upstream release
* Bump versioned (Build-)Depends on parrot
* Update patches
* Install new README.pod
* Fix vcs-field-not-canonical
* Do not install rubyish examples
* Do not Depends on parrot-devel anymore
* Add 07_disable-serialization-tests.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This implements a container of static lexical information for
2
 
 * NQP (will also be used by Rakudo). It differs from Parrot's
3
 
 * LexInfo in that it provides support for static lexical values
4
 
 * that get set into each created lexpad by default. It will also
5
 
 * enable indexed lookups and, some day, may also allow for native
6
 
 * values to be stored too. */
7
 
pmclass NQPLexInfo
8
 
    provides hash
9
 
    auto_attrs
10
 
    dynpmc group nqp
11
 
    hll nqp
12
 
    maps LexInfo
13
 
{
14
 
    /* The static (not closure cloned) sub that this is lexpad is used for. */
15
 
    ATTR PMC *static_code;
16
 
    
17
 
    /* Hash mapping a name to the register that always holds its
18
 
     * value. */
19
 
    ATTR PMC *name_to_register_map;
20
 
    
21
 
    /* Hash mapping names to static values. This is the authoritative
22
 
     * source of these, but we always build a more quickly usable
23
 
     * cache in the next two attributes. */
24
 
    ATTR PMC *static_values;
25
 
    
26
 
    /* Integer array of slots that we always want to pre-initialize
27
 
     * with static values; built from the static_values hash. */
28
 
    ATTR PMC *static_slots_cache;
29
 
    
30
 
    /* Matching PMC array of the values to pre-init with; built from
31
 
    * the static_values hash. */
32
 
    ATTR PMC *static_values_cache;
33
 
 
34
 
    VTABLE void init() {
35
 
        SELF.init_pmc(PMCNULL);
36
 
    }
37
 
 
38
 
    VTABLE void init_pmc(PMC *sub) {
39
 
        /* Set up the lex info storage. */
40
 
        PMC *name_to_register_map = Parrot_pmc_new(interp, enum_class_Hash);
41
 
        VTABLE_init_int(interp, name_to_register_map, (INTVAL)enum_type_INTVAL);
42
 
        SET_ATTR_name_to_register_map(INTERP, SELF, name_to_register_map);
43
 
        
44
 
        /* Stash the sub. */
45
 
        SET_ATTR_static_code(INTERP, SELF, sub);
46
 
 
47
 
        /* Need custom mark. */
48
 
        PObj_custom_mark_SET(SELF);
49
 
    }
50
 
    
51
 
    VTABLE void mark() {
52
 
        PMC *name_to_register_map, *static_values;
53
 
        PMC *static_slots_cache, *static_values_cache;
54
 
        GET_ATTR_name_to_register_map(INTERP, SELF, name_to_register_map);
55
 
        Parrot_gc_mark_PMC_alive(INTERP, name_to_register_map);
56
 
        GET_ATTR_static_values(INTERP, SELF, static_values);
57
 
        Parrot_gc_mark_PMC_alive(INTERP, static_values);
58
 
        GET_ATTR_static_slots_cache(INTERP, SELF, static_slots_cache);
59
 
        Parrot_gc_mark_PMC_alive(INTERP, static_slots_cache);
60
 
        GET_ATTR_static_values_cache(INTERP, SELF, static_values_cache);
61
 
        Parrot_gc_mark_PMC_alive(INTERP, static_values_cache);
62
 
    }
63
 
    
64
 
    VTABLE INTVAL elements() {
65
 
        PMC *name_to_register_map;
66
 
        GET_ATTR_name_to_register_map(INTERP, SELF, name_to_register_map);
67
 
        return VTABLE_elements(interp, name_to_register_map);
68
 
    }
69
 
    
70
 
    VTABLE INTVAL get_integer_keyed_str(STRING *key) {
71
 
        PMC *name_to_register_map;
72
 
        GET_ATTR_name_to_register_map(INTERP, SELF, name_to_register_map);
73
 
        return VTABLE_get_integer_keyed_str(interp, name_to_register_map, key);
74
 
    }
75
 
    
76
 
    VTABLE INTVAL exists_keyed_str(STRING *key) {
77
 
        PMC *name_to_register_map;
78
 
        GET_ATTR_name_to_register_map(INTERP, SELF, name_to_register_map);
79
 
        return VTABLE_exists_keyed_str(interp, name_to_register_map, key);
80
 
    }
81
 
    
82
 
    VTABLE void set_integer_keyed_str(STRING *key, INTVAL value) {
83
 
        PMC *name_to_register_map;
84
 
        GET_ATTR_name_to_register_map(INTERP, SELF, name_to_register_map);
85
 
        VTABLE_set_integer_keyed_str(interp, name_to_register_map, key, value);
86
 
    }
87
 
    
88
 
    VTABLE void visit(PMC *info) {
89
 
        VISIT_PMC_ATTR(INTERP, info, SELF, NQPLexInfo, name_to_register_map);
90
 
        VISIT_PMC_ATTR(INTERP, info, SELF, NQPLexInfo, static_code);
91
 
        SUPER(info);
92
 
    }
93
 
    
94
 
    VTABLE void thaw(PMC *info) {
95
 
        UNUSED(INTERP)
96
 
        UNUSED(info)
97
 
        /* Need custom mark. */
98
 
        PObj_custom_mark_SET(SELF);
99
 
    }
100
 
    
101
 
    VTABLE PMC *get_iter() {
102
 
        PMC *name_to_register_map;
103
 
        GET_ATTR_name_to_register_map(INTERP, SELF, name_to_register_map);
104
 
        return VTABLE_get_iter(interp, name_to_register_map);
105
 
    }
106
 
 
107
 
/*
108
 
 
109
 
=item C<void declare_lex_preg(STRING *name, INTVAL preg)>
110
 
 
111
 
Declare a lexical variable that is an alias for a PMC register.  The PIR
112
 
compiler calls this method in response to a ".lex STRING, PREG" directive.
113
 
 
114
 
=cut
115
 
 
116
 
*/
117
 
 
118
 
    METHOD declare_lex_preg(STRING *name, INTVAL preg) {
119
 
        PMC *name_to_register_map;
120
 
        GET_ATTR_name_to_register_map(INTERP, SELF, name_to_register_map);
121
 
        VTABLE_set_integer_keyed_str(INTERP, name_to_register_map, name, preg);
122
 
    }
123
 
    
124
 
    METHOD set_static_lexpad_value(STRING *key, PMC *value) {
125
 
        /* Stash static value in hash. */
126
 
        PMC *svs;
127
 
        GET_ATTR_static_values(INTERP, SELF, svs);
128
 
        if (PMC_IS_NULL(svs)) {
129
 
            svs = Parrot_pmc_new(interp, enum_class_Hash);
130
 
            SET_ATTR_static_values(INTERP, SELF, svs);
131
 
        }
132
 
        VTABLE_set_pmc_keyed_str(interp, svs, key, value);
133
 
    }
134
 
    
135
 
    METHOD finish_static_lexpad() {
136
 
        /* Build caches from the static lexpad. */
137
 
        PMC *svs;
138
 
        GET_ATTR_static_values(INTERP, SELF, svs);
139
 
        if (PMC_IS_NULL(svs) || !VTABLE_elements(interp, svs)) {
140
 
            /* No values; clear caches. */
141
 
            SET_ATTR_static_slots_cache(INTERP, SELF, PMCNULL);
142
 
            SET_ATTR_static_values_cache(INTERP, SELF, PMCNULL);
143
 
        }
144
 
        else {
145
 
            /* Build caches. */
146
 
            PMC *slots  = Parrot_pmc_new(interp, enum_class_ResizableIntegerArray);
147
 
            PMC *values = Parrot_pmc_new(interp, enum_class_ResizablePMCArray);
148
 
            PMC *iter   = VTABLE_get_iter(interp, svs);
149
 
            while (VTABLE_get_bool(interp, iter)) {
150
 
                STRING *name  = VTABLE_shift_string(interp, iter);
151
 
                INTVAL  slot  = VTABLE_get_integer_keyed_str(interp, SELF, name);
152
 
                PMC    *value = VTABLE_get_pmc_keyed_str(interp, svs, name);
153
 
                VTABLE_push_integer(interp, slots, slot >> 2);
154
 
                VTABLE_push_pmc(interp, values, value);
155
 
            }
156
 
            SET_ATTR_static_slots_cache(INTERP, SELF, slots);
157
 
            SET_ATTR_static_values_cache(INTERP, SELF, values);
158
 
        }
159
 
    }
160
 
    
161
 
    METHOD get_static_code() {
162
 
        PMC *static_code;
163
 
        GET_ATTR_static_code(INTERP, SELF, static_code);
164
 
        RETURN (PMC *static_code);
165
 
    }
166
 
 
167
 
/*
168
 
 
169
 
=item C<PMC *inspect_str(STRING *what)>
170
 
 
171
 
Introspects this LexInfo structure. The only valid introspection key is
172
 
C<symbols>, which gets an array of the names of the symbols in this lexpad.
173
 
 
174
 
=cut
175
 
 
176
 
*/
177
 
 
178
 
    VTABLE PMC *inspect_str(STRING *what) {
179
 
        if (STRING_equal(INTERP, what, CONST_STRING(INTERP, "symbols"))) {
180
 
            PMC * const result    = Parrot_pmc_new(INTERP, enum_class_ResizableStringArray);
181
 
            PMC *name_to_register_map;
182
 
            Hash *hash;
183
 
            
184
 
            GET_ATTR_name_to_register_map(INTERP, SELF, name_to_register_map);
185
 
            hash = (Hash *)VTABLE_get_pointer(interp, name_to_register_map);
186
 
 
187
 
            parrot_hash_iterate(hash,
188
 
                PARROT_ASSERT(_bucket->key);
189
 
                VTABLE_push_string(INTERP, result, (STRING *)_bucket->key););
190
 
 
191
 
            return result;
192
 
        }
193
 
        else
194
 
            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
195
 
                "Unknown introspection value '%S'", what);
196
 
    }
197
 
}