Skip to content

Commit bbf29dd

Browse files
committed
Compute allocation strategy of CStruct on REPR compose.
1 parent 2d2074c commit bbf29dd

File tree

1 file changed

+16
-46
lines changed

1 file changed

+16
-46
lines changed

src/6model/reprs/CStruct.c

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,71 +45,42 @@ static PMC * introspection_call(PARROT_INTERP, PMC *WHAT, PMC *HOW, STRING *name
4545
return VTABLE_get_pmc_keyed_int(interp, cappy, 0);
4646
}
4747

48-
/* Helper to make an accessor call. */
49-
static PMC * accessor_call(PARROT_INTERP, PMC *obj, STRING *name) {
50-
PMC *old_ctx, *cappy;
51-
52-
/* Look up method; if there is none hand back a null. */
53-
PMC *meth = VTABLE_find_method(interp, obj, name);
54-
if (PMC_IS_NULL(meth))
55-
return meth;
56-
57-
/* Set up call capture. */
58-
old_ctx = Parrot_pcc_get_signature(interp, CURRENT_CONTEXT(interp));
59-
cappy = Parrot_pmc_new(interp, enum_class_CallContext);
60-
VTABLE_push_pmc(interp, cappy, obj);
61-
62-
/* Call. */
63-
Parrot_pcc_invoke_from_sig_object(interp, meth, cappy);
64-
65-
/* Grab result. */
66-
cappy = Parrot_pcc_get_signature(interp, CURRENT_CONTEXT(interp));
67-
Parrot_pcc_set_signature(interp, CURRENT_CONTEXT(interp), old_ctx);
68-
return VTABLE_get_pmc_keyed_int(interp, cappy, 0);
69-
}
70-
7148
/* Locates all of the attributes. Puts them onto a flattened, ordered
7249
* list of attributes (populating the passed flat_list). Also builds
7350
* the index mapping for doing named lookups. Note index is not related
7451
* to the storage position. */
75-
static PMC * index_mapping_and_flat_list(PARROT_INTERP, PMC *WHAT, CStructREPRData *repr_data) {
52+
static PMC * index_mapping_and_flat_list(PARROT_INTERP, PMC *mro, CStructREPRData *repr_data) {
7653
PMC *flat_list = Parrot_pmc_new(interp, enum_class_ResizablePMCArray);
7754
PMC *class_list = Parrot_pmc_new(interp, enum_class_ResizablePMCArray);
7855
PMC *attr_map_list = Parrot_pmc_new(interp, enum_class_ResizablePMCArray);
79-
STRING *attributes_str = Parrot_str_new_constant(interp, "attributes");
80-
STRING *parents_str = Parrot_str_new_constant(interp, "parents");
8156
STRING *name_str = Parrot_str_new_constant(interp, "name");
82-
STRING *mro_str = Parrot_str_new_constant(interp, "mro");
8357
INTVAL current_slot = 0;
8458

8559
INTVAL num_classes, i;
8660
CStructNameMap * result = NULL;
8761

88-
/* Get the MRO. */
89-
PMC *mro = introspection_call(interp, WHAT, STABLE(WHAT)->HOW, mro_str, 0);
90-
INTVAL mro_idx = VTABLE_elements(interp, mro);
91-
9262
/* Walk through the parents list. */
63+
INTVAL mro_idx = VTABLE_elements(interp, mro);
9364
while (mro_idx)
9465
{
9566
/* Get current class in MRO. */
96-
PMC *current_class = decontainerize(interp, VTABLE_get_pmc_keyed_int(interp, mro, --mro_idx));
97-
PMC *HOW = STABLE(current_class)->HOW;
67+
PMC *type_info = VTABLE_get_pmc_keyed_int(interp, mro, --mro_idx);
68+
PMC *current_class = decontainerize(interp, VTABLE_get_pmc_keyed_int(interp, type_info, 0));
9869

9970
/* Get its local parents; make sure we're not doing MI. */
100-
PMC *parents = introspection_call(interp, current_class, HOW, parents_str, 1);
71+
PMC *parents = VTABLE_get_pmc_keyed_int(interp, type_info, 2);
10172
INTVAL num_parents = VTABLE_elements(interp, parents);
10273
if (num_parents <= 1) {
10374
/* Get attributes and iterate over them. */
104-
PMC *attributes = introspection_call(interp, current_class, HOW, attributes_str, 1);
75+
PMC *attributes = VTABLE_get_pmc_keyed_int(interp, type_info, 1);
10576
PMC *attr_map = PMCNULL;
10677
PMC *attr_iter = VTABLE_get_iter(interp, attributes);
10778
while (VTABLE_get_bool(interp, attr_iter)) {
10879
/* Get attribute. */
10980
PMC * attr = VTABLE_shift_pmc(interp, attr_iter);
11081

11182
/* Get its name. */
112-
PMC *name_pmc = accessor_call(interp, attr, name_str);
83+
PMC *name_pmc = VTABLE_get_pmc_keyed_str(interp, attr, name_str);
11384
STRING *name = VTABLE_get_string(interp, name_pmc);
11485

11586
/* Allocate a slot. */
@@ -148,7 +119,7 @@ static PMC * index_mapping_and_flat_list(PARROT_INTERP, PMC *WHAT, CStructREPRDa
148119
/* This works out an allocation strategy for the object. It takes care of
149120
* "inlining" storage of attributes that are natively typed, as well as
150121
* noting unbox targets. */
151-
static void compute_allocation_strategy(PARROT_INTERP, PMC *WHAT, CStructREPRData *repr_data) {
122+
static void compute_allocation_strategy(PARROT_INTERP, PMC *repr_info, CStructREPRData *repr_data) {
152123
STRING *type_str = Parrot_str_new_constant(interp, "type");
153124
PMC *flat_list;
154125

@@ -164,7 +135,7 @@ static void compute_allocation_strategy(PARROT_INTERP, PMC *WHAT, CStructREPRDat
164135
Parrot_block_GC_mark(interp);
165136

166137
/* Compute index mapping table and get flat list of attributes. */
167-
flat_list = index_mapping_and_flat_list(interp, WHAT, repr_data);
138+
flat_list = index_mapping_and_flat_list(interp, repr_info, repr_data);
168139

169140
/* If we have no attributes in the index mapping, then just the header. */
170141
if (repr_data->name_to_index_mapping[0].class_key == NULL) {
@@ -195,7 +166,7 @@ static void compute_allocation_strategy(PARROT_INTERP, PMC *WHAT, CStructREPRDat
195166
for (i = 0; i < num_attrs; i++) {
196167
/* Fetch its type; see if it's some kind of unboxed type. */
197168
PMC *attr = VTABLE_get_pmc_keyed_int(interp, flat_list, i);
198-
PMC *type = accessor_call(interp, attr, type_str);
169+
PMC *type = VTABLE_get_pmc_keyed_str(interp, attr, type_str);
199170
INTVAL type_id = REPR(type)->ID;
200171
INTVAL bits = sizeof(void *) * 8;
201172
INTVAL align = ALIGNOF1(void *);
@@ -363,19 +334,18 @@ static PMC * type_object_for(PARROT_INTERP, PMC *HOW) {
363334

364335
/* Composes the representation. */
365336
static void compose(PARROT_INTERP, STable *st, PMC *repr_info) {
366-
/* TODO: move allocation strategy stuff here. */
337+
/* Compute allocation strategy. */
338+
CStructREPRData *repr_data = (CStructREPRData *) st->REPR_data;
339+
PMC *attr_info = VTABLE_get_pmc_keyed_str(interp, repr_info,
340+
Parrot_str_new_constant(interp, "attribute"));
341+
compute_allocation_strategy(interp, attr_info, repr_data);
342+
PARROT_GC_WRITE_BARRIER(interp, st->stable_pmc);
367343
}
368344

369345
/* Creates a new instance based on the type object. */
370346
static PMC * allocate(PARROT_INTERP, STable *st) {
371347
CStructInstance * obj;
372-
373-
/* Compute allocation strategy if we've not already done so. */
374348
CStructREPRData * repr_data = (CStructREPRData *) st->REPR_data;
375-
if (!repr_data->struct_size) {
376-
compute_allocation_strategy(interp, st->WHAT, repr_data);
377-
PARROT_GC_WRITE_BARRIER(interp, st->stable_pmc);
378-
}
379349

380350
/* Allocate and set up object instance. */
381351
obj = (CStructInstance *) Parrot_gc_allocate_fixed_size_storage(interp, sizeof(CStructInstance));

0 commit comments

Comments
 (0)