Coverage Report

Created: 2017-04-15 07:07

/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
528
static void mk_storage_spec(MVMThreadContext *tc, MVMuint16 bits, MVMStorageSpec *spec) {
7
528
    spec->bits = bits;
8
528
    spec->inlineable      = MVM_STORAGE_SPEC_INLINED;
9
528
    spec->boxed_primitive = MVM_STORAGE_SPEC_BP_NUM;
10
528
    spec->can_box         = MVM_STORAGE_SPEC_CAN_BOX_NUM;
11
528
    switch (bits) {
12
398
        case 64: spec->align = ALIGNOF(MVMnum64); break;
13
130
        case 32: spec->align = ALIGNOF(MVMnum32); break;
14
0
        default: spec->align = ALIGNOF(MVMnum64); break;
15
528
    }
16
528
}
17
18
/* Creates a new type object of this representation, and associates it with
19
 * the given HOW. */
20
134
static MVMObject * type_object_for(MVMThreadContext *tc, MVMObject *HOW) {
21
134
    MVMSTable *st  = MVM_gc_allocate_stable(tc, &P6num_this_repr, HOW);
22
134
23
134
    MVMROOT(tc, st, {
24
134
        MVMObject *obj = MVM_gc_allocate_type_object(tc, st);
25
134
        MVMP6numREPRData *repr_data = (MVMP6numREPRData *)MVM_malloc(sizeof(MVMP6numREPRData));
26
134
27
134
        repr_data->bits = sizeof(MVMnum64) * 8;
28
134
        mk_storage_spec(tc, repr_data->bits, &repr_data->storage_spec);
29
134
        MVM_ASSIGN_REF(tc, &(st->header), st->WHAT, obj);
30
134
        st->size = sizeof(MVMP6num);
31
134
        st->REPR_data = repr_data;
32
134
    });
33
134
34
134
    return st->WHAT;
35
134
}
36
37
/* Copies the body of one object to another. */
38
0
static void copy_to(MVMThreadContext *tc, MVMSTable *st, void *src, MVMObject *dest_root, void *dest) {
39
0
    MVMP6numREPRData *repr_data = (MVMP6numREPRData *)st->REPR_data;
40
0
    MVMP6numBody     *src_body  = (MVMP6numBody *)src;
41
0
    MVMP6numBody     *dest_body = (MVMP6numBody *)dest;
42
0
    switch (repr_data->bits) {
43
0
        case 32: dest_body->value.n32 = src_body->value.n32; break;
44
0
        default: dest_body->value.n64 = src_body->value.n64; break;
45
0
    }
46
0
}
47
48
387k
static void set_num(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMnum64 value) {
49
387k
    MVMP6numREPRData *repr_data = (MVMP6numREPRData *)st->REPR_data;
50
387k
    switch (repr_data->bits) {
51
0
        case 32: ((MVMP6numBody *)data)->value.n32 = (MVMnum32)value; break;
52
387k
        default: ((MVMP6numBody *)data)->value.n64 = value; break;
53
387k
    }
54
387k
}
55
56
1.25M
static MVMnum64 get_num(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data) {
57
1.25M
    MVMP6numREPRData *repr_data = (MVMP6numREPRData *)st->REPR_data;
58
1.25M
    switch (repr_data->bits) {
59
0
        case 32: return ((MVMP6numBody *)data)->value.n32;
60
1.25M
        default: return ((MVMP6numBody *)data)->value.n64;
61
1.25M
    }
62
1.25M
}
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
1.25M
static const MVMStorageSpec * get_storage_spec(MVMThreadContext *tc, MVMSTable *st) {
81
1.25M
    MVMP6numREPRData *repr_data = (MVMP6numREPRData *)st->REPR_data;
82
1.25M
    if (repr_data && repr_data->bits)
83
1.25M
        return &repr_data->storage_spec;
84
0
    return &default_storage_spec;
85
1.25M
}
86
87
/* Compose the representation. */
88
4
static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info_hash) {
89
4
    MVMP6numREPRData *repr_data = (MVMP6numREPRData *)st->REPR_data;
90
4
    MVMStringConsts  str_consts = tc->instance->str_consts;
91
4
92
4
    MVMObject *info = MVM_repr_at_key_o(tc, info_hash, str_consts.float_str);
93
4
    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
4
    if (repr_data->bits)
110
4
        mk_storage_spec(tc, repr_data->bits, &repr_data->storage_spec);
111
4
}
112
113
/* Set the size of the STable. */
114
390
static void deserialize_stable_size(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) {
115
390
    st->size = sizeof(MVMP6num);
116
390
}
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
390
static void deserialize_repr_data(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) {
126
390
    MVMP6numREPRData *repr_data = (MVMP6numREPRData *)MVM_malloc(sizeof(MVMP6numREPRData));
127
390
128
390
129
390
    repr_data->bits        = MVM_serialization_read_int(tc, reader);
130
390
131
390
    if (repr_data->bits !=  1 && repr_data->bits !=  2 && repr_data->bits !=  4 && repr_data->bits != 8
132
390
     && 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
390
135
390
    if (repr_data->bits)
136
390
        mk_storage_spec(tc, repr_data->bits, &repr_data->storage_spec);
137
390
    st->REPR_data = repr_data;
138
390
}
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
130
const MVMREPROps * MVMP6num_initialize(MVMThreadContext *tc) {
151
130
    return &P6num_this_repr;
152
130
}
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
};