Coverage Report

Created: 2018-07-03 15:31

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