Coverage Report

Created: 2017-04-15 07:07

/home/travis/build/MoarVM/MoarVM/src/6model/reprs/CPPStruct.c
Line
Count
Source (jump to first uncovered line)
1
#include "moar.h"
2
3
/* This representation's function pointer table. */
4
static const MVMREPROps CPPStruct_this_repr;
5
6
/* Locates all of the attributes. Puts them onto a flattened, ordered
7
 * list of attributes (populating the passed flat_list). Also builds
8
 * the index mapping for doing named lookups. Note index is not related
9
 * to the storage position. */
10
0
static MVMObject * index_mapping_and_flat_list(MVMThreadContext *tc, MVMObject *mro, MVMCPPStructREPRData *repr_data) {
11
0
    MVMInstance *instance  = tc->instance;
12
0
    MVMObject *flat_list, *class_list, *attr_map_list;
13
0
    MVMint32  num_classes, i, current_slot = 0;
14
0
    MVMCPPStructNameMap *result;
15
0
16
0
    MVMint32 mro_idx = MVM_repr_elems(tc, mro);
17
0
18
0
    MVM_gc_root_temp_push(tc, (MVMCollectable **)&mro);
19
0
20
0
    flat_list = MVM_repr_alloc_init(tc, MVM_hll_current(tc)->slurpy_array_type);
21
0
    MVM_gc_root_temp_push(tc, (MVMCollectable **)&flat_list);
22
0
23
0
    class_list = MVM_repr_alloc_init(tc, MVM_hll_current(tc)->slurpy_array_type);
24
0
    MVM_gc_root_temp_push(tc, (MVMCollectable **)&class_list);
25
0
26
0
    attr_map_list = MVM_repr_alloc_init(tc, MVM_hll_current(tc)->slurpy_array_type);
27
0
    MVM_gc_root_temp_push(tc, (MVMCollectable **)&attr_map_list);
28
0
29
0
    /* Walk through the parents list. */
30
0
    while (mro_idx)
31
0
    {
32
0
        /* Get current class in MRO. */
33
0
        MVMObject *type_info     = MVM_repr_at_pos_o(tc, mro, --mro_idx);
34
0
        MVMObject *current_class = MVM_repr_at_pos_o(tc, type_info, 0);
35
0
36
0
        /* Get its local parents; make sure we're not doing MI. */
37
0
        MVMObject *parents     = MVM_repr_at_pos_o(tc, type_info, 2);
38
0
        MVMint32  num_parents = MVM_repr_elems(tc, parents);
39
0
        if (num_parents <= 1) {
40
0
            /* Get attributes and iterate over them. */
41
0
            MVMObject *attributes     = MVM_repr_at_pos_o(tc, type_info, 1);
42
0
            MVMIter * const attr_iter = (MVMIter *)MVM_iter(tc, attributes);
43
0
            MVMObject *attr_map = NULL;
44
0
45
0
            if (MVM_iter_istrue(tc, attr_iter)) {
46
0
                MVM_gc_root_temp_push(tc, (MVMCollectable **)&attr_iter);
47
0
                attr_map = MVM_repr_alloc_init(tc, MVM_hll_current(tc)->slurpy_hash_type);
48
0
                MVM_gc_root_temp_push(tc, (MVMCollectable **)&attr_map);
49
0
            }
50
0
51
0
            while (MVM_iter_istrue(tc, attr_iter)) {
52
0
                MVMObject *current_slot_obj = MVM_repr_box_int(tc, MVM_hll_current(tc)->int_box_type, current_slot);
53
0
                MVMObject *attr, *name_obj;
54
0
                MVMString *name;
55
0
56
0
                MVM_repr_shift_o(tc, (MVMObject *)attr_iter);
57
0
58
0
                /* Get attribute. */
59
0
                attr = MVM_iterval(tc, attr_iter);
60
0
61
0
                /* Get its name. */
62
0
                name_obj = MVM_repr_at_key_o(tc, attr, instance->str_consts.name);
63
0
                name     = MVM_repr_get_str(tc, name_obj);
64
0
65
0
                MVM_repr_bind_key_o(tc, attr_map, name, current_slot_obj);
66
0
67
0
                current_slot++;
68
0
69
0
                /* Push attr onto the flat list. */
70
0
                MVM_repr_push_o(tc, flat_list, attr);
71
0
            }
72
0
73
0
            if (attr_map) {
74
0
                MVM_gc_root_temp_pop_n(tc, 2);
75
0
            }
76
0
77
0
            /* Add to class list and map list. */
78
0
            MVM_repr_push_o(tc, class_list, current_class);
79
0
            MVM_repr_push_o(tc, attr_map_list, attr_map);
80
0
        }
81
0
        else {
82
0
            MVM_exception_throw_adhoc(tc,
83
0
                "CPPStruct representation does not support multiple inheritance");
84
0
        }
85
0
    }
86
0
87
0
    MVM_gc_root_temp_pop_n(tc, 4);
88
0
89
0
    /* We can now form the name map. */
90
0
    num_classes = MVM_repr_elems(tc, class_list);
91
0
    result = (MVMCPPStructNameMap *) MVM_malloc(sizeof(MVMCPPStructNameMap) * (1 + num_classes));
92
0
93
0
    for (i = 0; i < num_classes; i++) {
94
0
        result[i].class_key = MVM_repr_at_pos_o(tc, class_list, i);
95
0
        result[i].name_map  = MVM_repr_at_pos_o(tc, attr_map_list, i);
96
0
    }
97
0
98
0
    /* set the end to be NULL, it's useful for iteration. */
99
0
    result[i].class_key = NULL;
100
0
101
0
    repr_data->name_to_index_mapping = result;
102
0
103
0
    return flat_list;
104
0
}
105
106
0
static MVMint32 round_up_to_multi(MVMint32 i, MVMint32 m) {
107
0
    return (MVMint32)((i + m - 1) / m) * m;
108
0
}
109
110
/* This works out an allocation strategy for the object. It takes care of
111
 * "inlining" storage of attributes that are natively typed, as well as
112
 * noting unbox targets. */
113
0
static void compute_allocation_strategy(MVMThreadContext *tc, MVMObject *repr_info, MVMCPPStructREPRData *repr_data) {
114
0
    /* Compute index mapping table and get flat list of attributes. */
115
0
    MVMObject *flat_list = index_mapping_and_flat_list(tc, repr_info, repr_data);
116
0
117
0
    /* If we have no attributes in the index mapping, then just the header. */
118
0
    if (repr_data->name_to_index_mapping[0].class_key == NULL) {
119
0
        repr_data->struct_size = 1; /* avoid 0-byte malloc */
120
0
        repr_data->struct_align = ALIGNOF(void *);
121
0
    }
122
0
123
0
    /* Otherwise, we need to compute the allocation strategy.  */
124
0
    else {
125
0
        /* We track the size of the struct, which is what we'll want offsets into. */
126
0
        MVMint32 cur_size    = 0;
127
0
        MVMint32 struct_size = 0;
128
0
129
0
        /* Get number of attributes and set up various counters. */
130
0
        MVMint32 num_attrs        = MVM_repr_elems(tc, flat_list);
131
0
        MVMint32 info_alloc       = num_attrs == 0 ? 1 : num_attrs;
132
0
        MVMint32 cur_obj_attr     = 0;
133
0
        MVMint32 cur_init_slot    = 0;
134
0
        MVMint32 i;
135
0
136
0
        /* Allocate location/offset arrays and GC mark info arrays. */
137
0
        repr_data->num_attributes      = num_attrs;
138
0
        repr_data->attribute_locations = (MVMint32 *)   MVM_malloc(info_alloc * sizeof(MVMint32));
139
0
        repr_data->struct_offsets      = (MVMint32 *)   MVM_malloc(info_alloc * sizeof(MVMint32));
140
0
        repr_data->flattened_stables   = (MVMSTable **) MVM_calloc(info_alloc, sizeof(MVMObject *));
141
0
        repr_data->member_types        = (MVMObject **) MVM_calloc(info_alloc, sizeof(MVMObject *));
142
0
        repr_data->struct_align        = 0;
143
0
144
0
        /* Go over the attributes and arrange their allocation. */
145
0
        for (i = 0; i < num_attrs; i++) {
146
0
            /* Fetch its type; see if it's some kind of unboxed type. */
147
0
            MVMObject *attr  = MVM_repr_at_pos_o(tc, flat_list, i);
148
0
            MVMObject *type  = MVM_repr_at_key_o(tc, attr, tc->instance->str_consts.type);
149
0
            MVMObject *inlined_val = MVM_repr_at_key_o(tc, attr, tc->instance->str_consts.inlined);
150
0
            MVMint64 inlined = !MVM_is_null(tc, inlined_val) && MVM_repr_get_int(tc, inlined_val);
151
0
            MVMint32   bits  = sizeof(void *) * 8;
152
0
            MVMint32   align = ALIGNOF(void *);
153
0
            if (!MVM_is_null(tc, type)) {
154
0
                /* See if it's a type that we know how to handle in a C struct. */
155
0
                const MVMStorageSpec *spec = REPR(type)->get_storage_spec(tc, STABLE(type));
156
0
                MVMint32  type_id    = REPR(type)->ID;
157
0
                if (spec->inlineable == MVM_STORAGE_SPEC_INLINED &&
158
0
                        (spec->boxed_primitive == MVM_STORAGE_SPEC_BP_INT ||
159
0
                         spec->boxed_primitive == MVM_STORAGE_SPEC_BP_NUM)) {
160
0
                    /* It's a boxed int or num; pretty easy. It'll just live in the
161
0
                     * body of the struct. Instead of masking in i here (which
162
0
                     * would be the parallel to how we handle boxed types) we
163
0
                     * repurpose it to store the bit-width of the type, so
164
0
                     * that get_attribute_ref can find it later. */
165
0
                    bits = spec->bits;
166
0
                    align = spec->align;
167
0
168
0
                    repr_data->attribute_locations[i] = (bits << MVM_CPPSTRUCT_ATTR_SHIFT) | MVM_CPPSTRUCT_ATTR_IN_STRUCT;
169
0
                    repr_data->flattened_stables[i] = STABLE(type);
170
0
                    if (REPR(type)->initialize) {
171
0
                        if (!repr_data->initialize_slots)
172
0
                            repr_data->initialize_slots = (MVMint32 *) MVM_calloc(info_alloc + 1, sizeof(MVMint32));
173
0
                        repr_data->initialize_slots[cur_init_slot] = i;
174
0
                        cur_init_slot++;
175
0
                    }
176
0
                }
177
0
                else if (spec->can_box & MVM_STORAGE_SPEC_CAN_BOX_STR) {
178
0
                    /* It's a string of some kind.  */
179
0
                    repr_data->num_child_objs++;
180
0
                    repr_data->attribute_locations[i] = (cur_obj_attr++ << MVM_CPPSTRUCT_ATTR_SHIFT) | MVM_CPPSTRUCT_ATTR_STRING;
181
0
                    repr_data->member_types[i] = type;
182
0
                    repr_data->flattened_stables[i] = STABLE(type);
183
0
                    if (REPR(type)->initialize) {
184
0
                        if (!repr_data->initialize_slots)
185
0
                            repr_data->initialize_slots = (MVMint32 *) MVM_calloc(info_alloc + 1, sizeof(MVMint32));
186
0
                        repr_data->initialize_slots[cur_init_slot] = i;
187
0
                        cur_init_slot++;
188
0
                    }
189
0
                }
190
0
                else if (type_id == MVM_REPR_ID_MVMCArray) {
191
0
                    /* It's a CArray of some kind.  */
192
0
                    repr_data->num_child_objs++;
193
0
                    repr_data->attribute_locations[i] = (cur_obj_attr++ << MVM_CPPSTRUCT_ATTR_SHIFT) | MVM_CPPSTRUCT_ATTR_CARRAY;
194
0
                    repr_data->member_types[i] = type;
195
0
                }
196
0
                else if (type_id == MVM_REPR_ID_MVMCStruct) {
197
0
                    /* It's a CStruct. */
198
0
                    repr_data->num_child_objs++;
199
0
                    repr_data->attribute_locations[i] = (cur_obj_attr++ << MVM_CPPSTRUCT_ATTR_SHIFT) | MVM_CPPSTRUCT_ATTR_CSTRUCT;
200
0
                    repr_data->member_types[i] = type;
201
0
                    if (inlined) {
202
0
                        MVMCStructREPRData *cstruct_repr_data = (MVMCStructREPRData *)STABLE(type)->REPR_data;
203
0
                        bits                                  = cstruct_repr_data->struct_size * 8;
204
0
                        align                                 = cstruct_repr_data->struct_align;
205
0
                        repr_data->attribute_locations[i]    |= MVM_CSTRUCT_ATTR_INLINED;
206
0
                    }
207
0
                }
208
0
                else if (type_id == MVM_REPR_ID_MVMCPPStruct) {
209
0
                    /* It's a CPPStruct. */
210
0
                    repr_data->num_child_objs++;
211
0
                    repr_data->attribute_locations[i] = (cur_obj_attr++ << MVM_CPPSTRUCT_ATTR_SHIFT) | MVM_CPPSTRUCT_ATTR_CPPSTRUCT;
212
0
                    repr_data->member_types[i] = type;
213
0
                    if (inlined) {
214
0
                        MVMCPPStructREPRData *cppstruct_repr_data = (MVMCPPStructREPRData *)STABLE(type)->REPR_data;
215
0
                        bits                                      = cppstruct_repr_data->struct_size * 8;
216
0
                        align                                     = cppstruct_repr_data->struct_align;
217
0
                        repr_data->attribute_locations[i]        |= MVM_CPPSTRUCT_ATTR_INLINED;
218
0
                    }
219
0
                }
220
0
                else if (type_id == MVM_REPR_ID_MVMCUnion) {
221
0
                    /* It's a CUnion. */
222
0
                    repr_data->num_child_objs++;
223
0
                    repr_data->attribute_locations[i] = (cur_obj_attr++ << MVM_CSTRUCT_ATTR_SHIFT) | MVM_CSTRUCT_ATTR_CUNION;
224
0
                    repr_data->member_types[i] = type;
225
0
                    if (inlined) {
226
0
                        MVMCUnionREPRData *cunion_repr_data = (MVMCUnionREPRData *)STABLE(type)->REPR_data;
227
0
                        bits                                = cunion_repr_data->struct_size * 8;
228
0
                        align                               = cunion_repr_data->struct_align;
229
0
                        repr_data->attribute_locations[i]  |= MVM_CSTRUCT_ATTR_INLINED;
230
0
                    }
231
0
                }
232
0
                else if (type_id == MVM_REPR_ID_MVMCPointer) {
233
0
                    /* It's a CPointer. */
234
0
                    repr_data->num_child_objs++;
235
0
                    repr_data->attribute_locations[i] = (cur_obj_attr++ << MVM_CPPSTRUCT_ATTR_SHIFT) | MVM_CPPSTRUCT_ATTR_CPTR;
236
0
                    repr_data->member_types[i] = type;
237
0
                }
238
0
                else {
239
0
                    MVM_exception_throw_adhoc(tc,
240
0
                        "CPPStruct representation only handles attributes of type:\n"
241
0
                        "  (u)int8, (u)int16, (u)int32, (u)int64, (u)long, (u)longlong, num32, num64, (s)size_t, bool, Str\n"
242
0
                        "  and types with representation: CArray, CPointer, CStruct, CPPStruct and CUnion");
243
0
                }
244
0
            }
245
0
            else {
246
0
                MVM_exception_throw_adhoc(tc,
247
0
                    "CPPStruct representation requires the types of all attributes to be specified");
248
0
            }
249
0
250
0
            if (bits % 8) {
251
0
                 MVM_exception_throw_adhoc(tc,
252
0
                    "CPPStruct only supports native types that are a multiple of 8 bits wide (was passed: %"PRId32")", bits);
253
0
            }
254
0
255
0
            /* Do allocation. */
256
0
            /* C structure needs careful alignment. If cur_size is not aligned
257
0
             * to align bytes (cur_size % align), make sure it is before we
258
0
             * add the next element. */
259
0
            if (cur_size % align) {
260
0
                cur_size += align - cur_size % align;
261
0
            }
262
0
263
0
            if (align > repr_data->struct_align)
264
0
                repr_data->struct_align = align;
265
0
266
0
            repr_data->struct_offsets[i] = cur_size;
267
0
            cur_size += bits / 8;
268
0
269
0
            struct_size = round_up_to_multi(struct_size, align) + bits/8;
270
0
        }
271
0
272
0
        /* Finally, put computed allocation size in place; it's body size plus
273
0
         * header size. Also number of markables and sentinels. */
274
0
        repr_data->struct_size = round_up_to_multi(struct_size, repr_data->struct_align);
275
0
        if (repr_data->initialize_slots)
276
0
            repr_data->initialize_slots[cur_init_slot] = -1;
277
0
    }
278
0
}
279
280
/* Helper for reading a pointer at the specified offset. */
281
0
static void * get_ptr_at_offset(void *data, MVMint32 offset) {
282
0
    void *location = (char *)data + offset;
283
0
    return *((void **)location);
284
0
}
285
286
/* Helper for writing a pointer at the specified offset. */
287
0
static void set_ptr_at_offset(void *data, MVMint32 offset, void *value) {
288
0
    void *location = (char *)data + offset;
289
0
    *((void **)location) = value;
290
0
}
291
292
/* Helper for finding a slot number. */
293
0
static MVMint32 try_get_slot(MVMThreadContext *tc, MVMCPPStructREPRData *repr_data, MVMObject *class_key, MVMString *name) {
294
0
    if (repr_data->name_to_index_mapping) {
295
0
        MVMCPPStructNameMap *cur_map_entry = repr_data->name_to_index_mapping;
296
0
        while (cur_map_entry->class_key != NULL) {
297
0
            if (cur_map_entry->class_key == class_key) {
298
0
                MVMObject *slot_obj = MVM_repr_at_key_o(tc, cur_map_entry->name_map, name);
299
0
                if (IS_CONCRETE(slot_obj))
300
0
                    return MVM_repr_get_int(tc, slot_obj);
301
0
                break;
302
0
            }
303
0
            cur_map_entry++;
304
0
        }
305
0
    }
306
0
    return -1;
307
0
}
308
309
/* Creates a new type object of this representation, and associates it with
310
 * the given HOW. */
311
0
static MVMObject * type_object_for(MVMThreadContext *tc, MVMObject *HOW) {
312
0
    MVMSTable *st  = MVM_gc_allocate_stable(tc, &CPPStruct_this_repr, HOW);
313
0
314
0
    MVMROOT(tc, st, {
315
0
        MVMObject *obj = MVM_gc_allocate_type_object(tc, st);
316
0
        MVM_ASSIGN_REF(tc, &(st->header), st->WHAT, obj);
317
0
        st->size = sizeof(MVMCPPStruct);
318
0
    });
319
0
320
0
    return st->WHAT;
321
0
}
322
323
/* Composes the representation. */
324
0
static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *repr_info) {
325
0
    /* Compute allocation strategy. */
326
0
    MVMCPPStructREPRData *repr_data = MVM_calloc(1, sizeof(MVMCPPStructREPRData));
327
0
    MVMObject *attr_info = MVM_repr_at_key_o(tc, repr_info, tc->instance->str_consts.attribute);
328
0
    compute_allocation_strategy(tc, attr_info, repr_data);
329
0
    st->REPR_data = repr_data;
330
0
}
331
332
/* Initialize a new instance. */
333
0
static void initialize(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data) {
334
0
    MVMCPPStructREPRData * repr_data = (MVMCPPStructREPRData *)st->REPR_data;
335
0
336
0
    /* Allocate object body. */
337
0
    MVMCPPStructBody *body = (MVMCPPStructBody *)data;
338
0
    body->cppstruct = MVM_calloc(1, repr_data->struct_size > 0 ? repr_data->struct_size : 1);
339
0
340
0
    /* Allocate child obj array. */
341
0
    if (repr_data->num_child_objs > 0)
342
0
        body->child_objs = (MVMObject **)MVM_calloc(repr_data->num_child_objs,
343
0
            sizeof(MVMObject *));
344
0
345
0
    /* Initialize the slots. */
346
0
    if (repr_data->initialize_slots) {
347
0
        MVMint32 i;
348
0
        for (i = 0; repr_data->initialize_slots[i] >= 0; i++) {
349
0
            MVMint32  offset = repr_data->struct_offsets[repr_data->initialize_slots[i]];
350
0
            MVMSTable *st     = repr_data->flattened_stables[repr_data->initialize_slots[i]];
351
0
            st->REPR->initialize(tc, st, root, (char *)body->cppstruct + offset);
352
0
        }
353
0
    }
354
0
}
355
356
/* Copies to the body of one object to another. */
357
0
static void copy_to(MVMThreadContext *tc, MVMSTable *st, void *src, MVMObject *dest_root, void *dest) {
358
0
    MVM_exception_throw_adhoc(tc, "cloning a CPPStruct is NYI");
359
0
}
360
361
/* Helper for complaining about attribute access errors. */
362
MVM_NO_RETURN static void no_such_attribute(MVMThreadContext *tc, const char *action, MVMObject *class_handle, MVMString *name) MVM_NO_RETURN_GCC;
363
0
static void no_such_attribute(MVMThreadContext *tc, const char *action, MVMObject *class_handle, MVMString *name) {
364
0
    char *c_name = MVM_string_utf8_encode_C_string(tc, name);
365
0
    char *waste[] = { c_name, NULL };
366
0
    MVM_exception_throw_adhoc_free(tc, waste, "Can not %s non-existent attribute '%s'",
367
0
        action, c_name);
368
0
}
369
370
/* Helper to die because this type doesn't support attributes. */
371
MVM_NO_RETURN static void die_no_attrs(MVMThreadContext *tc) MVM_NO_RETURN_GCC;
372
0
static void die_no_attrs(MVMThreadContext *tc) {
373
0
    MVM_exception_throw_adhoc(tc,
374
0
        "CPPStruct representation attribute not yet fully implemented");
375
0
}
376
377
static void get_attribute(MVMThreadContext *tc, MVMSTable *st, MVMObject *root,
378
        void *data, MVMObject *class_handle, MVMString *name, MVMint64 hint,
379
0
        MVMRegister *result_reg, MVMuint16 kind) {
380
0
    MVMCPPStructREPRData *repr_data = (MVMCPPStructREPRData *)st->REPR_data;
381
0
    MVMCPPStructBody *body = (MVMCPPStructBody *)data;
382
0
    MVMint64 slot;
383
0
384
0
    if (!repr_data)
385
0
        MVM_exception_throw_adhoc(tc, "CPPStruct: must compose before using get_attribute");
386
0
387
0
    slot = hint >= 0 ? hint : try_get_slot(tc, repr_data, class_handle, name);
388
0
    if (slot >= 0) {
389
0
        MVMSTable *attr_st = repr_data->flattened_stables[slot];
390
0
        switch (kind) {
391
0
        case MVM_reg_obj: {
392
0
            MVMint32 type      = repr_data->attribute_locations[slot] & MVM_CPPSTRUCT_ATTR_MASK;
393
0
            MVMint32 real_slot = repr_data->attribute_locations[slot] >> MVM_CPPSTRUCT_ATTR_SHIFT;
394
0
395
0
            if (type == MVM_CPPSTRUCT_ATTR_IN_STRUCT) {
396
0
                MVM_exception_throw_adhoc(tc,
397
0
                    "CPPStruct can't perform boxed get on flattened attributes yet");
398
0
            }
399
0
            else {
400
0
                MVMObject *typeobj = repr_data->member_types[slot];
401
0
                MVMObject *obj     = body->child_objs[real_slot];
402
0
                if (!obj) {
403
0
                    /* No cached object. */
404
0
                    void *cobj = get_ptr_at_offset(body->cppstruct, repr_data->struct_offsets[slot]);
405
0
                    if (cobj) {
406
0
                        MVMObject **child_objs = body->child_objs;
407
0
                        if (type == MVM_CPPSTRUCT_ATTR_CARRAY) {
408
0
                            obj = MVM_nativecall_make_carray(tc, typeobj, cobj);
409
0
                        }
410
0
                        else if(type == MVM_CPPSTRUCT_ATTR_CSTRUCT) {
411
0
                            if (repr_data->attribute_locations[slot] & MVM_CPPSTRUCT_ATTR_INLINED)
412
0
                                obj = MVM_nativecall_make_cstruct(tc, typeobj,
413
0
                                    (char *)body->cppstruct + repr_data->struct_offsets[slot]);
414
0
                            else
415
0
                                obj = MVM_nativecall_make_cstruct(tc, typeobj, cobj);
416
0
                        }
417
0
                        else if(type == MVM_CPPSTRUCT_ATTR_CPPSTRUCT) {
418
0
                            if (repr_data->attribute_locations[slot] & MVM_CPPSTRUCT_ATTR_INLINED)
419
0
                                obj = MVM_nativecall_make_cppstruct(tc, typeobj,
420
0
                                    (char *)body->cppstruct + repr_data->struct_offsets[slot]);
421
0
                            else
422
0
                                obj = MVM_nativecall_make_cppstruct(tc, typeobj, cobj);
423
0
                        }
424
0
                        else if(type == MVM_CPPSTRUCT_ATTR_CUNION) {
425
0
                            if (repr_data->attribute_locations[slot] & MVM_CPPSTRUCT_ATTR_INLINED)
426
0
                                obj = MVM_nativecall_make_cunion(tc, typeobj,
427
0
                                    (char *)body->cppstruct + repr_data->struct_offsets[slot]);
428
0
                            else
429
0
                                obj = MVM_nativecall_make_cunion(tc, typeobj, cobj);
430
0
                        }
431
0
                        else if(type == MVM_CPPSTRUCT_ATTR_CPTR) {
432
0
                            obj = MVM_nativecall_make_cpointer(tc, typeobj, cobj);
433
0
                        }
434
0
                        else if(type == MVM_CPPSTRUCT_ATTR_STRING) {
435
0
                            MVMROOT(tc, typeobj, {
436
0
                                MVMString *str = MVM_string_utf8_decode(tc, tc->instance->VMString,
437
0
                                    cobj, strlen(cobj));
438
0
                                obj = MVM_repr_box_str(tc, typeobj, str);
439
0
                            });
440
0
                        }
441
0
                        child_objs[real_slot] = obj;
442
0
                    }
443
0
                    else {
444
0
                        obj = typeobj;
445
0
                    }
446
0
                }
447
0
                result_reg->o = obj;
448
0
            }
449
0
            break;
450
0
        }
451
0
        case MVM_reg_int64: {
452
0
            if (attr_st)
453
0
                result_reg->i64 = attr_st->REPR->box_funcs.get_int(tc, attr_st, root,
454
0
                    ((char *)body->cppstruct) + repr_data->struct_offsets[slot]);
455
0
            else
456
0
                MVM_exception_throw_adhoc(tc, "CPPStruct: invalid native get of object attribute");
457
0
            break;
458
0
        }
459
0
        case MVM_reg_num64: {
460
0
            if (attr_st)
461
0
                result_reg->n64 = attr_st->REPR->box_funcs.get_num(tc, attr_st, root,
462
0
                    ((char *)body->cppstruct) + repr_data->struct_offsets[slot]);
463
0
            else
464
0
                MVM_exception_throw_adhoc(tc, "CPPStruct: invalid native get of object attribute");
465
0
            break;
466
0
        }
467
0
        case MVM_reg_str: {
468
0
            if (attr_st)
469
0
                result_reg->s = attr_st->REPR->box_funcs.get_str(tc, attr_st, root,
470
0
                    ((char *)body->cppstruct) + repr_data->struct_offsets[slot]);
471
0
            else
472
0
                MVM_exception_throw_adhoc(tc, "CPPStruct: invalid native get of object attribute");
473
0
            if (!result_reg->s)
474
0
                result_reg->s = tc->instance->str_consts.empty;
475
0
            break;
476
0
        }
477
0
        default:
478
0
            MVM_exception_throw_adhoc(tc, "CPPStruct: invalid kind in attribute get");
479
0
        }
480
0
    }
481
0
    else {
482
0
        no_such_attribute(tc, "bind", class_handle, name);
483
0
    }
484
0
}
485
486
/* Binds the given value to the specified attribute. */
487
static void bind_attribute(MVMThreadContext *tc, MVMSTable *st, MVMObject *root,
488
        void *data, MVMObject *class_handle, MVMString *name, MVMint64 hint,
489
0
        MVMRegister value_reg, MVMuint16 kind) {
490
0
    MVMCPPStructREPRData *repr_data = (MVMCPPStructREPRData *)st->REPR_data;
491
0
    MVMCPPStructBody *body = (MVMCPPStructBody *)data;
492
0
    MVMint64 slot;
493
0
494
0
    if (!repr_data)
495
0
        MVM_exception_throw_adhoc(tc, "CPPStruct: must compose before using bind_attribute");
496
0
497
0
    slot = hint >= 0 ? hint : try_get_slot(tc, repr_data, class_handle, name);
498
0
    if (slot >= 0) {
499
0
        MVMSTable *attr_st = repr_data->flattened_stables[slot];
500
0
        switch (kind) {
501
0
        case MVM_reg_obj: {
502
0
            MVMObject *value = value_reg.o;
503
0
            MVMint32   type  = repr_data->attribute_locations[slot] & MVM_CPPSTRUCT_ATTR_MASK;
504
0
505
0
            if (type == MVM_CPPSTRUCT_ATTR_IN_STRUCT) {
506
0
                MVM_exception_throw_adhoc(tc,
507
0
                    "CPPStruct can't perform boxed bind on flattened attributes yet");
508
0
            }
509
0
            else {
510
0
                MVMint32   real_slot = repr_data->attribute_locations[slot] >> MVM_CPPSTRUCT_ATTR_SHIFT;
511
0
512
0
                if (IS_CONCRETE(value)) {
513
0
                    void *cobj       = NULL;
514
0
515
0
                    MVM_ASSIGN_REF(tc, &(root->header), body->child_objs[real_slot], value);
516
0
517
0
                    /* Set cobj to correct pointer based on type of value. */
518
0
                    if (type == MVM_CPPSTRUCT_ATTR_CARRAY) {
519
0
                        if (REPR(value)->ID != MVM_REPR_ID_MVMCArray)
520
0
                            MVM_exception_throw_adhoc(tc,
521
0
                                "Can only store CArray attribute in CArray slot in CPPStruct");
522
0
                        cobj = ((MVMCArray *)value)->body.storage;
523
0
                    }
524
0
                    else if (type == MVM_CPPSTRUCT_ATTR_CSTRUCT) {
525
0
                        if (REPR(value)->ID != MVM_REPR_ID_MVMCStruct)
526
0
                            MVM_exception_throw_adhoc(tc,
527
0
                                "Can only store CStruct attribute in CStruct slot in CPPStruct");
528
0
                        cobj = ((MVMCStruct *)value)->body.cstruct;
529
0
                    }
530
0
                    else if (type == MVM_CPPSTRUCT_ATTR_CPPSTRUCT) {
531
0
                        if (REPR(value)->ID != MVM_REPR_ID_MVMCPPStruct)
532
0
                            MVM_exception_throw_adhoc(tc,
533
0
                                "Can only store CPPStruct attribute in CPPStruct slot in CPPStruct");
534
0
                        cobj = ((MVMCPPStruct *)value)->body.cppstruct;
535
0
                    }
536
0
                    else if (type == MVM_CPPSTRUCT_ATTR_CUNION) {
537
0
                        if (REPR(value)->ID != MVM_REPR_ID_MVMCUnion)
538
0
                            MVM_exception_throw_adhoc(tc,
539
0
                                "Can only store CUnion attribute in CUnion slot in CPPStruct");
540
0
                        cobj = ((MVMCUnion *)value)->body.cunion;
541
0
                    }
542
0
                    else if (type == MVM_CPPSTRUCT_ATTR_CPTR) {
543
0
                        if (REPR(value)->ID != MVM_REPR_ID_MVMCPointer)
544
0
                            MVM_exception_throw_adhoc(tc,
545
0
                                "Can only store CPointer attribute in CPointer slot in CPPStruct");
546
0
                        cobj = ((MVMCPointer *)value)->body.ptr;
547
0
                    }
548
0
                    else if (type == MVM_CPPSTRUCT_ATTR_STRING) {
549
0
                        MVMString *str  = MVM_repr_get_str(tc, value);
550
0
                        cobj = MVM_string_utf8_encode_C_string(tc, str);
551
0
                    }
552
0
553
0
                    set_ptr_at_offset(body->cppstruct, repr_data->struct_offsets[slot], cobj);
554
0
                }
555
0
                else {
556
0
                    body->child_objs[real_slot] = NULL;
557
0
                    set_ptr_at_offset(body->cppstruct, repr_data->struct_offsets[slot], NULL);
558
0
                }
559
0
            }
560
0
            break;
561
0
        }
562
0
        case MVM_reg_int64: {
563
0
            if (attr_st)
564
0
                attr_st->REPR->box_funcs.set_int(tc, attr_st, root,
565
0
                    ((char *)body->cppstruct) + repr_data->struct_offsets[slot], value_reg.i64);
566
0
            else
567
0
                MVM_exception_throw_adhoc(tc, "CPPStruct: invalid native binding to object attribute");
568
0
            break;
569
0
        }
570
0
        case MVM_reg_num64: {
571
0
            if (attr_st)
572
0
                attr_st->REPR->box_funcs.set_num(tc, attr_st, root,
573
0
                    ((char *)body->cppstruct) + repr_data->struct_offsets[slot], value_reg.n64);
574
0
            else
575
0
                MVM_exception_throw_adhoc(tc, "CPPStruct: invalid native binding to object attribute");
576
0
            break;
577
0
        }
578
0
        case MVM_reg_str: {
579
0
            if (attr_st)
580
0
                attr_st->REPR->box_funcs.set_str(tc, attr_st, root,
581
0
                    ((char *)body->cppstruct) + repr_data->struct_offsets[slot], value_reg.s);
582
0
            else
583
0
                MVM_exception_throw_adhoc(tc, "CPPStruct: invalid native binding to object attribute");
584
0
            break;
585
0
        }
586
0
        default:
587
0
            MVM_exception_throw_adhoc(tc, "CPPStruct: invalid kind in attribute bind");
588
0
        }
589
0
    }
590
0
    else {
591
0
        no_such_attribute(tc, "bind", class_handle, name);
592
0
    }
593
0
}
594
595
596
/* Checks if an attribute has been initialized. */
597
0
static MVMint64 is_attribute_initialized(MVMThreadContext *tc, MVMSTable *st, void *data, MVMObject *class_handle, MVMString *name, MVMint64 hint) {
598
0
    die_no_attrs(tc);
599
0
}
600
601
/* Gets the hint for the given attribute ID. */
602
0
static MVMint64 hint_for(MVMThreadContext *tc, MVMSTable *st, MVMObject *class_handle, MVMString *name) {
603
0
    return MVM_NO_HINT;
604
0
}
605
606
/* Adds held objects to the GC worklist. */
607
0
static void gc_mark(MVMThreadContext *tc, MVMSTable *st, void *data, MVMGCWorklist *worklist) {
608
0
    MVMCPPStructREPRData *repr_data = (MVMCPPStructREPRData *) st->REPR_data;
609
0
    MVMCPPStructBody *body = (MVMCPPStructBody *)data;
610
0
    MVMint32 i;
611
0
    for (i = 0; i < repr_data->num_child_objs; i++)
612
0
        MVM_gc_worklist_add(tc, worklist, &body->child_objs[i]);
613
0
}
614
615
/* Marks the representation data in an STable.*/
616
0
static void gc_mark_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMGCWorklist *worklist) {
617
0
    MVMCPPStructREPRData *repr_data = (MVMCPPStructREPRData *)st->REPR_data;
618
0
    if (repr_data) {
619
0
        MVMint32 i;
620
0
        if (repr_data->name_to_index_mapping) {
621
0
            MVMCPPStructNameMap *map = repr_data->name_to_index_mapping;
622
0
            for (i = 0; map[i].class_key; i++) {
623
0
                MVM_gc_worklist_add(tc, worklist, &map[i].class_key);
624
0
                MVM_gc_worklist_add(tc, worklist, &map[i].name_map);
625
0
            }
626
0
        }
627
0
628
0
        if (repr_data->flattened_stables) {
629
0
            MVMSTable **flattened_stables = repr_data->flattened_stables;
630
0
            for (i = 0; i < repr_data->num_attributes; i++)
631
0
                MVM_gc_worklist_add(tc, worklist, &flattened_stables[i]);
632
0
        }
633
0
634
0
        if (repr_data->member_types) {
635
0
            MVMObject **member_types = repr_data->member_types;
636
0
            for (i = 0; i < repr_data->num_attributes; i++)
637
0
                MVM_gc_worklist_add(tc, worklist, &member_types[i]);
638
0
        }
639
0
    }
640
0
}
641
642
/* Free representation data. */
643
0
static void gc_free_repr_data(MVMThreadContext *tc, MVMSTable *st) {
644
0
    MVMCPPStructREPRData *repr_data = (MVMCPPStructREPRData *)st->REPR_data;
645
0
646
0
    /* May not have survived to composition. */
647
0
    if (repr_data == NULL)
648
0
        return;
649
0
650
0
    if (repr_data->name_to_index_mapping) {
651
0
        MVM_free(repr_data->name_to_index_mapping);
652
0
        MVM_free(repr_data->attribute_locations);
653
0
        MVM_free(repr_data->struct_offsets);
654
0
        MVM_free(repr_data->flattened_stables);
655
0
        MVM_free(repr_data->member_types);
656
0
        MVM_free(repr_data->initialize_slots);
657
0
    }
658
0
659
0
    MVM_free(st->REPR_data);
660
0
}
661
662
/* This is called to do any cleanup of resources when an object gets
663
 * embedded inside another one. Never called on a top-level object. */
664
0
static void gc_cleanup(MVMThreadContext *tc, MVMSTable *st, void *data) {
665
0
    MVMCPPStructBody *body = (MVMCPPStructBody *)data;
666
0
    if (body->child_objs)
667
0
        MVM_free(body->child_objs);
668
0
    /* XXX For some reason, this causes crashes at the moment. Need to
669
0
     * work out why. */
670
0
    /*if (body->cppstruct)
671
0
        MVM_free(body->cppstruct);*/
672
0
}
673
674
/* Called by the VM in order to free memory associated with this object. */
675
0
static void gc_free(MVMThreadContext *tc, MVMObject *obj) {
676
0
    gc_cleanup(tc, STABLE(obj), OBJECT_BODY(obj));
677
0
}
678
679
static const MVMStorageSpec storage_spec = {
680
    MVM_STORAGE_SPEC_REFERENCE, /* inlineable */
681
    sizeof(void*) * 8,          /* bits */
682
    ALIGNOF(void*),             /* align */
683
    MVM_STORAGE_SPEC_BP_NONE,   /* boxed_primitive */
684
    0,                          /* can_box */
685
    0,                          /* is_unsigned */
686
};
687
688
/* Gets the storage specification for this representation. */
689
0
static const MVMStorageSpec * get_storage_spec(MVMThreadContext *tc, MVMSTable *st) {
690
0
    return &storage_spec;
691
0
}
692
693
/* Serializes the REPR data. */
694
0
static void serialize_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMSerializationWriter *writer) {
695
0
    MVMCPPStructREPRData *repr_data = (MVMCPPStructREPRData *)st->REPR_data;
696
0
    MVMint32 i, num_classes, num_slots;
697
0
698
0
    MVM_serialization_write_int(tc, writer, repr_data->struct_size);
699
0
    MVM_serialization_write_int(tc, writer, repr_data->struct_align);
700
0
    MVM_serialization_write_int(tc, writer, repr_data->num_attributes);
701
0
    MVM_serialization_write_int(tc, writer, repr_data->num_child_objs);
702
0
    for(i = 0; i < repr_data->num_attributes; i++){
703
0
        MVM_serialization_write_int(tc, writer, repr_data->attribute_locations[i]);
704
0
        MVM_serialization_write_int(tc, writer, repr_data->struct_offsets[i]);
705
0
706
0
        MVM_serialization_write_int(tc, writer, repr_data->flattened_stables[i] != NULL);
707
0
        if (repr_data->flattened_stables[i])
708
0
            MVM_serialization_write_stable_ref(tc, writer, repr_data->flattened_stables[i]);
709
0
710
0
        MVM_serialization_write_ref(tc, writer, repr_data->member_types[i]);
711
0
    }
712
0
713
0
    i=0;
714
0
    while (repr_data->name_to_index_mapping[i].class_key)
715
0
        i++;
716
0
    num_classes = i;
717
0
    MVM_serialization_write_int(tc, writer, num_classes);
718
0
    for(i = 0; i < num_classes; i++){
719
0
        MVM_serialization_write_ref(tc, writer, repr_data->name_to_index_mapping[i].class_key);
720
0
        MVM_serialization_write_ref(tc, writer, repr_data->name_to_index_mapping[i].name_map);
721
0
    }
722
0
723
0
    i=0;
724
0
    while(repr_data->initialize_slots && repr_data->initialize_slots[i] != -1)
725
0
        i++;
726
0
    num_slots = i;
727
0
    MVM_serialization_write_int(tc, writer, num_slots);
728
0
    for(i = 0; i < num_slots; i++){
729
0
        MVM_serialization_write_int(tc, writer, repr_data->initialize_slots[i]);
730
0
    }
731
0
}
732
733
/* Deserializes the REPR data. */
734
0
static void deserialize_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) {
735
0
    MVMCPPStructREPRData *repr_data = (MVMCPPStructREPRData *) MVM_malloc(sizeof(MVMCPPStructREPRData));
736
0
    MVMint32 i, num_classes, num_slots;
737
0
738
0
    repr_data->struct_size    = MVM_serialization_read_int(tc, reader);
739
0
    if (reader->root.version >= 17) {
740
0
        repr_data->struct_align = MVM_serialization_read_int(tc, reader);
741
0
    }
742
0
    repr_data->num_attributes = MVM_serialization_read_int(tc, reader);
743
0
    repr_data->num_child_objs = MVM_serialization_read_int(tc, reader);
744
0
745
0
    repr_data->attribute_locations = (MVMint32 *)MVM_malloc(sizeof(MVMint32) * repr_data->num_attributes);
746
0
    repr_data->struct_offsets      = (MVMint32 *)MVM_malloc(sizeof(MVMint32) * repr_data->num_attributes);
747
0
    repr_data->flattened_stables   = (MVMSTable **)MVM_malloc(repr_data->num_attributes * sizeof(MVMSTable *));
748
0
    repr_data->member_types        = (MVMObject **)MVM_malloc(repr_data->num_attributes * sizeof(MVMObject *));
749
0
750
0
    for(i = 0; i < repr_data->num_attributes; i++) {
751
0
        repr_data->attribute_locations[i] = MVM_serialization_read_int(tc, reader);
752
0
        repr_data->struct_offsets[i] = MVM_serialization_read_int(tc, reader);
753
0
754
0
        if(MVM_serialization_read_int(tc, reader)){
755
0
            MVM_ASSIGN_REF(tc, &(st->header), repr_data->flattened_stables[i], MVM_serialization_read_stable_ref(tc, reader));
756
0
        }
757
0
        else {
758
0
            repr_data->flattened_stables[i] = NULL;
759
0
        }
760
0
761
0
        repr_data->member_types[i] = MVM_serialization_read_ref(tc, reader);
762
0
    }
763
0
764
0
    num_classes = MVM_serialization_read_int(tc, reader);
765
0
    repr_data->name_to_index_mapping = (MVMCPPStructNameMap *)MVM_malloc(sizeof(MVMCPPStructNameMap) * (1 + num_classes));
766
0
    for(i = 0; i < num_classes; i++){
767
0
        repr_data->name_to_index_mapping[i].class_key = MVM_serialization_read_ref(tc, reader);
768
0
        repr_data->name_to_index_mapping[i].name_map = MVM_serialization_read_ref(tc, reader);
769
0
    }
770
0
    repr_data->name_to_index_mapping[i].class_key = NULL;
771
0
    repr_data->name_to_index_mapping[i].name_map = NULL;
772
0
773
0
    num_slots = MVM_serialization_read_int(tc, reader);
774
0
    repr_data->initialize_slots = (MVMint32 *)MVM_malloc(sizeof(MVMint32) * (1 + num_slots));
775
0
    for(i = 0; i < num_slots; i++){
776
0
        repr_data->initialize_slots[i] = MVM_serialization_read_int(tc, reader);
777
0
    }
778
0
    repr_data->initialize_slots[i] = -1;
779
0
780
0
    st->REPR_data = repr_data;
781
0
}
782
783
0
static void deserialize_stable_size(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) {
784
0
    st->size = sizeof(MVMCPPStruct);
785
0
}
786
787
/* Initializes the representation. */
788
130
const MVMREPROps * MVMCPPStruct_initialize(MVMThreadContext *tc) {
789
130
    return &CPPStruct_this_repr;
790
130
}
791
792
static const MVMREPROps CPPStruct_this_repr = {
793
    type_object_for,
794
    MVM_gc_allocate_object,
795
    initialize,
796
    copy_to,
797
    {
798
        get_attribute,
799
        bind_attribute,
800
        hint_for,
801
        is_attribute_initialized
802
    },   /* attr_funcs */
803
    MVM_REPR_DEFAULT_BOX_FUNCS,
804
    MVM_REPR_DEFAULT_POS_FUNCS,
805
    MVM_REPR_DEFAULT_ASS_FUNCS,
806
    MVM_REPR_DEFAULT_ELEMS,
807
    get_storage_spec,
808
    NULL, /* change_type */
809
    NULL, /* serialize */
810
    NULL, /* deserialize */
811
    serialize_repr_data,
812
    deserialize_repr_data,
813
    deserialize_stable_size,
814
    gc_mark,
815
    gc_free,
816
    gc_cleanup,
817
    gc_mark_repr_data,
818
    gc_free_repr_data,
819
    compose,
820
    NULL, /* spesh */
821
    "CPPStruct", /* name */
822
    MVM_REPR_ID_MVMCPPStruct,
823
    NULL, /* unmanaged_size */
824
    NULL, /* describe_refs */
825
};