Coverage Report

Created: 2018-07-03 15:31

/home/travis/build/MoarVM/MoarVM/src/6model/reprs/P6num.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 P6num_this_repr;
5
6
586
static void mk_storage_spec(MVMThreadContext *tc, MVMuint16 bits, MVMStorageSpec *spec) {
7
586
    spec->bits = bits;
8
586
    spec->inlineable      = MVM_STORAGE_SPEC_INLINED;
9
586
    spec->boxed_primitive = MVM_STORAGE_SPEC_BP_NUM;
10
586
    spec->can_box         = MVM_STORAGE_SPEC_CAN_BOX_NUM;
11
586
    switch (bits) {
12
442
        case 64: spec->align = ALIGNOF(MVMnum64); break;
13
144
        case 32: spec->align = ALIGNOF(MVMnum32); break;
14
0
        default: spec->align = ALIGNOF(MVMnum64); break;
15
586
    }
16
586
}
17
18
/* Creates a new type object of this representation, and associates it with
19
 * the given HOW. */
20
149
static MVMObject * type_object_for(MVMThreadContext *tc, MVMObject *HOW) {
21
149
    MVMSTable *st  = MVM_gc_allocate_stable(tc, &P6num_this_repr, HOW);
22
149
23
149
    MVMROOT(tc, st, {
24
149
        MVMObject *obj = MVM_gc_allocate_type_object(tc, st);
25
149
        MVMP6numREPRData *repr_data = (MVMP6numREPRData *)MVM_malloc(sizeof(MVMP6numREPRData));
26
149
27
149
        repr_data->bits = sizeof(MVMnum64) * 8;
28
149
        mk_storage_spec(tc, repr_data->bits, &repr_data->storage_spec);
29
149
        MVM_ASSIGN_REF(tc, &(st->header), st->WHAT, obj);
30
149
        st->size = sizeof(MVMP6num);
31
149
        st->REPR_data = repr_data;
32
149
    });
33
149
34
149
    return st->WHAT;
35
149
}
36
37
/* Copies the body of one object to another. */
38
1
static void copy_to(MVMThreadContext *tc, MVMSTable *st, void *src, MVMObject *dest_root, void *dest) {
39
1
    MVMP6numREPRData *repr_data = (MVMP6numREPRData *)st->REPR_data;
40
1
    MVMP6numBody     *src_body  = (MVMP6numBody *)src;
41
1
    MVMP6numBody     *dest_body = (MVMP6numBody *)dest;
42
1
    switch (repr_data->bits) {
43
0
        case 32: dest_body->value.n32 = src_body->value.n32; break;
44
1
        default: dest_body->value.n64 = src_body->value.n64; break;
45
1
    }
46
1
}
47
48
7.44M
static void set_num(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMnum64 value) {
49
7.44M
    MVMP6numREPRData *repr_data = (MVMP6numREPRData *)st->REPR_data;
50
7.44M
    switch (repr_data->bits) {
51
0
        case 32: ((MVMP6numBody *)data)->value.n32 = (MVMnum32)value; break;
52
7.44M
        default: ((MVMP6numBody *)data)->value.n64 = value; break;
53
7.44M
    }
54
7.44M
}
55
56
8.52M
static MVMnum64 get_num(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data) {
57
8.52M
    MVMP6numREPRData *repr_data = (MVMP6numREPRData *)st->REPR_data;
58
8.52M
    switch (repr_data->bits) {
59
0
        case 32: return ((MVMP6numBody *)data)->value.n32;
60
8.52M
        default: return ((MVMP6numBody *)data)->value.n64;
61
8.52M
    }
62
8.52M
}
63
64
/* Marks the representation data in an STable.*/
65
0
static void gc_free_repr_data(MVMThreadContext *tc, MVMSTable *st) {
66
0
    MVM_free(st->REPR_data);
67
0
}
68
69
static const MVMStorageSpec default_storage_spec = {
70
    MVM_STORAGE_SPEC_INLINED,     /* inlineable */
71
    sizeof(MVMnum64) * 8,         /* bits */
72
    ALIGNOF(MVMnum64),            /* align */
73
    MVM_STORAGE_SPEC_BP_NUM,      /* boxed_primitive */
74
    MVM_STORAGE_SPEC_CAN_BOX_NUM, /* can_box */
75
    0,                            /* is_unsigned */
76
};
77
78
79
/* Gets the storage specification for this representation. */
80
8.52M
static const MVMStorageSpec * get_storage_spec(MVMThreadContext *tc, MVMSTable *st) {
81
8.52M
    MVMP6numREPRData *repr_data = (MVMP6numREPRData *)st->REPR_data;
82
8.52M
    if (repr_data && repr_data->bits)
83
8.52M
        return &repr_data->storage_spec;
84
0
    return &default_storage_spec;
85
8.52M
}
86
87
/* Compose the representation. */
88
5
static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info_hash) {
89
5
    MVMP6numREPRData *repr_data = (MVMP6numREPRData *)st->REPR_data;
90
5
    MVMStringConsts  str_consts = tc->instance->str_consts;
91
5
92
5
    MVMObject *info = MVM_repr_at_key_o(tc, info_hash, str_consts.float_str);
93
5
    if (!MVM_is_null(tc, info)) {
94
0
        MVMObject *bits_o = MVM_repr_at_key_o(tc, info, str_consts.bits);
95
0
96
0
        if (!MVM_is_null(tc, bits_o)) {
97
0
            repr_data->bits = MVM_repr_get_int(tc, bits_o);
98
0
99
0
            switch (repr_data->bits) {
100
0
                case MVM_P6NUM_C_TYPE_FLOAT:      repr_data->bits = 8 * sizeof(float);       break;
101
0
                case MVM_P6NUM_C_TYPE_DOUBLE:     repr_data->bits = 8 * sizeof(double);      break;
102
0
                case MVM_P6NUM_C_TYPE_LONGDOUBLE: repr_data->bits = 8 * sizeof(long double); break;
103
0
            }
104
0
105
0
            if (repr_data->bits != 32 && repr_data->bits != 64)
106
0
                MVM_exception_throw_adhoc(tc, "MVMP6num: Unsupported num size (%dbit)", repr_data->bits);
107
0
        }
108
0
    }
109
5
    if (repr_data->bits)
110
5
        mk_storage_spec(tc, repr_data->bits, &repr_data->storage_spec);
111
5
}
112
113
/* Set the size of the STable. */
114
432
static void deserialize_stable_size(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) {
115
432
    st->size = sizeof(MVMP6num);
116
432
}
117
118
/* Serializes the REPR data. */
119
0
static void serialize_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMSerializationWriter *writer) {
120
0
    MVMP6numREPRData *repr_data = (MVMP6numREPRData *)st->REPR_data;
121
0
    MVM_serialization_write_int(tc, writer, repr_data->bits);
122
0
}
123
124
/* Deserializes representation data. */
125
432
static void deserialize_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) {
126
432
    MVMP6numREPRData *repr_data = (MVMP6numREPRData *)MVM_malloc(sizeof(MVMP6numREPRData));
127
432
128
432
129
432
    repr_data->bits        = MVM_serialization_read_int(tc, reader);
130
432
131
432
    if (repr_data->bits !=  1 && repr_data->bits !=  2 && repr_data->bits !=  4 && repr_data->bits != 8
132
432
     && repr_data->bits != 16 && repr_data->bits != 32 && repr_data->bits != 64)
133
0
        MVM_exception_throw_adhoc(tc, "MVMP6num: Unsupported int size (%dbit)", repr_data->bits);
134
432
135
432
    if (repr_data->bits)
136
432
        mk_storage_spec(tc, repr_data->bits, &repr_data->storage_spec);
137
432
    st->REPR_data = repr_data;
138
432
}
139
140
5
static void deserialize(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMSerializationReader *reader) {
141
5
    MVMnum64 value = MVM_serialization_read_num(tc, reader);
142
5
    set_num(tc, st, root, data, value);
143
5
}
144
145
5
static void serialize(MVMThreadContext *tc, MVMSTable *st, void *data, MVMSerializationWriter *writer) {
146
5
    MVM_serialization_write_num(tc, writer, get_num(tc, st, NULL, data));
147
5
}
148
149
/* Initializes the representation. */
150
144
const MVMREPROps * MVMP6num_initialize(MVMThreadContext *tc) {
151
144
    return &P6num_this_repr;
152
144
}
153
154
static const MVMREPROps P6num_this_repr = {
155
    type_object_for,
156
    MVM_gc_allocate_object,
157
    NULL, /* initialize */
158
    copy_to,
159
    MVM_REPR_DEFAULT_ATTR_FUNCS,
160
    {
161
        MVM_REPR_DEFAULT_SET_INT,
162
        MVM_REPR_DEFAULT_GET_INT,
163
        set_num,
164
        get_num,
165
        MVM_REPR_DEFAULT_SET_STR,
166
        MVM_REPR_DEFAULT_GET_STR,
167
        MVM_REPR_DEFAULT_SET_UINT,
168
        MVM_REPR_DEFAULT_GET_UINT,
169
        MVM_REPR_DEFAULT_GET_BOXED_REF
170
    },    /* box_funcs */
171
    MVM_REPR_DEFAULT_POS_FUNCS,
172
    MVM_REPR_DEFAULT_ASS_FUNCS,
173
    MVM_REPR_DEFAULT_ELEMS,
174
    get_storage_spec,
175
    NULL, /* change_type */
176
    serialize,
177
    deserialize,
178
    serialize_repr_data,
179
    deserialize_repr_data,
180
    deserialize_stable_size,
181
    NULL, /* gc_mark */
182
    NULL, /* gc_free */
183
    NULL, /* gc_cleanup */
184
    NULL, /* gc_mark_repr_data */
185
    gc_free_repr_data,
186
    compose,
187
    NULL, /* spesh */
188
    "P6num", /* name */
189
    MVM_REPR_ID_P6num,
190
    NULL, /* unmanaged_size */
191
    NULL, /* describe_refs */
192
};