Coverage Report

Created: 2017-04-15 07:07

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