Coverage Report

Created: 2018-07-03 15:31

/home/travis/build/MoarVM/MoarVM/src/6model/reprs/MVMCallCapture.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 MVMCallCapture_this_repr;
5
6
/* Creates a new type object of this representation, and associates it with
7
 * the given HOW. Also sets the invocation protocol handler in the STable. */
8
144
static MVMObject * type_object_for(MVMThreadContext *tc, MVMObject *HOW) {
9
144
    MVMSTable *st = MVM_gc_allocate_stable(tc, &MVMCallCapture_this_repr, HOW);
10
144
11
144
    MVMROOT(tc, st, {
12
144
        MVMObject *obj = MVM_gc_allocate_type_object(tc, st);
13
144
        MVM_ASSIGN_REF(tc, &(st->header), st->WHAT, obj);
14
144
        st->size = sizeof(MVMCallCapture);
15
144
    });
16
144
17
144
    return st->WHAT;
18
144
}
19
20
/* Copies the body of one object to another. */
21
1
static void copy_to(MVMThreadContext *tc, MVMSTable *st, void *src, MVMObject *dest_root, void *dest) {
22
1
    MVMCallCaptureBody *src_body  = (MVMCallCaptureBody *)src;
23
1
    MVMCallCaptureBody *dest_body = (MVMCallCaptureBody *)dest;
24
1
25
1
    MVMuint32 arg_size = src_body->apc->arg_count * sizeof(MVMRegister);
26
1
    MVMRegister *args = MVM_malloc(arg_size);
27
1
    memcpy(args, src_body->apc->args, arg_size);
28
1
29
1
    dest_body->apc = (MVMArgProcContext *)MVM_calloc(1, sizeof(MVMArgProcContext));
30
1
    MVM_args_proc_init(tc, dest_body->apc,
31
1
        MVM_args_copy_uninterned_callsite(tc, src_body->apc), args);
32
1
}
33
34
/* Adds held objects to the GC worklist. */
35
238
static void gc_mark(MVMThreadContext *tc, MVMSTable *st, void *data, MVMGCWorklist *worklist) {
36
238
    MVMCallCaptureBody *body = (MVMCallCaptureBody *)data;
37
238
    MVMArgProcContext *ctx = body->apc;
38
238
    MVMuint8  *flag_map = body->apc->callsite->arg_flags;
39
238
    MVMuint16  count = ctx->arg_count;
40
238
    MVMuint16  i, flag;
41
735
    for (i = 0, flag = 0; i < count; i++, flag++) {
42
497
        if (flag_map[flag] & MVM_CALLSITE_ARG_NAMED) {
43
21
            /* Current position is name, then next is value. */
44
21
            MVM_gc_worklist_add(tc, worklist, &ctx->args[i].s);
45
21
            i++;
46
21
        }
47
497
        if (flag_map[flag] & MVM_CALLSITE_ARG_STR || flag_map[flag] & MVM_CALLSITE_ARG_OBJ)
48
497
            MVM_gc_worklist_add(tc, worklist, &ctx->args[i].o);
49
497
    }
50
238
}
51
52
/* Called by the VM in order to free memory associated with this object. */
53
3.83k
static void gc_free(MVMThreadContext *tc, MVMObject *obj) {
54
3.83k
    MVMCallCapture *ctx = (MVMCallCapture *)obj;
55
3.83k
    /* We made our own copy of the callsite, args buffer and processing
56
3.83k
     * context, so free them all. */
57
3.83k
    if (ctx->body.apc) {
58
3.83k
        MVMCallsite *cs = ctx->body.apc->callsite;
59
3.83k
        if (cs && !cs->is_interned) {
60
0
            MVM_free(cs->arg_flags);
61
0
            MVM_free(cs);
62
0
        }
63
3.83k
        if (ctx->body.apc->named_used_size > 64)
64
0
            MVM_fixed_size_free(tc, tc->instance->fsa,
65
0
                ctx->body.apc->named_used_size,
66
0
                ctx->body.apc->named_used.byte_array);
67
3.83k
        MVM_free(ctx->body.apc->args);
68
3.83k
        MVM_free(ctx->body.apc);
69
3.83k
    }
70
3.83k
}
71
72
static const MVMStorageSpec storage_spec = {
73
    MVM_STORAGE_SPEC_REFERENCE, /* inlineable */
74
    0,                          /* bits */
75
    0,                          /* align */
76
    MVM_STORAGE_SPEC_BP_NONE,   /* boxed_primitive */
77
    0,                          /* can_box */
78
    0,                          /* is_unsigned */
79
};
80
81
/* Gets the storage specification for this representation. */
82
0
static const MVMStorageSpec * get_storage_spec(MVMThreadContext *tc, MVMSTable *st) {
83
0
    return &storage_spec;
84
0
}
85
86
/* Compose the representation. */
87
0
static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info) {
88
0
    /* Nothing to do for this REPR. */
89
0
}
90
91
/* Initializes the representation. */
92
144
const MVMREPROps * MVMCallCapture_initialize(MVMThreadContext *tc) {
93
144
    return &MVMCallCapture_this_repr;
94
144
}
95
96
static const MVMREPROps MVMCallCapture_this_repr = {
97
    type_object_for,
98
    MVM_gc_allocate_object,
99
    NULL, /* initialize */
100
    copy_to,
101
    MVM_REPR_DEFAULT_ATTR_FUNCS,
102
    MVM_REPR_DEFAULT_BOX_FUNCS,
103
    MVM_REPR_DEFAULT_POS_FUNCS,
104
    MVM_REPR_DEFAULT_ASS_FUNCS,
105
    MVM_REPR_DEFAULT_ELEMS,
106
    get_storage_spec,
107
    NULL, /* change_type */
108
    NULL, /* serialize */
109
    NULL, /* deserialize */
110
    NULL, /* serialize_repr_data */
111
    NULL, /* deserialize_repr_data */
112
    NULL, /* deserialize_stable_size */
113
    gc_mark,
114
    gc_free,
115
    NULL, /* gc_cleanup */
116
    NULL, /* gc_mark_repr_data */
117
    NULL, /* gc_free_repr_data */
118
    compose,
119
    NULL, /* spesh */
120
    "MVMCallCapture", /* name */
121
    MVM_REPR_ID_MVMCallCapture,
122
    NULL, /* unmanaged_size */
123
    NULL, /* describe_refs */
124
};
125
126
/* This function was only introduced for the benefit of the JIT. */
127
0
MVMint64 MVM_capture_pos_primspec(MVMThreadContext *tc, MVMObject *obj, MVMint64 i) {
128
0
    if (IS_CONCRETE(obj) && REPR(obj)->ID == MVM_REPR_ID_MVMCallCapture) {
129
0
        MVMCallCapture *cc = (MVMCallCapture *)obj;
130
0
        if (i >= 0 && i < cc->body.apc->num_pos) {
131
0
            MVMCallsiteEntry *arg_flags = cc->body.apc->arg_flags
132
0
                ? cc->body.apc->arg_flags
133
0
                : cc->body.apc->callsite->arg_flags;
134
0
            switch (arg_flags[i] & MVM_CALLSITE_ARG_MASK) {
135
0
                case MVM_CALLSITE_ARG_INT:
136
0
                    return MVM_STORAGE_SPEC_BP_INT;
137
0
                case MVM_CALLSITE_ARG_NUM:
138
0
                    return MVM_STORAGE_SPEC_BP_NUM;
139
0
                case MVM_CALLSITE_ARG_STR:
140
0
                    return MVM_STORAGE_SPEC_BP_STR;
141
0
                default:
142
0
                    return MVM_STORAGE_SPEC_BP_NONE;
143
0
            }
144
0
        }
145
0
        else {
146
0
            MVM_exception_throw_adhoc(tc,
147
0
                "Bad argument index given to captureposprimspec");
148
0
        }
149
0
    }
150
0
    else {
151
0
        MVM_exception_throw_adhoc(tc, "captureposprimspec needs a MVMCallCapture");
152
0
    }
153
0
}