-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfvalue.h
464 lines (405 loc) · 9.69 KB
/
fvalue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
* Copyright (c) 2017 The National University of Singapore.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _FVALUE_H
#define _FVALUE_H
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "fgc.h"
namespace F
{
template <typename _T, typename _U>
_T _bit_cast(const _U _u)
{
union _cast_helper {
_T _t_val;
_U _u_val;
_cast_helper()
{
std::memset(this, 0, sizeof(*this));
}
} _helper;
_helper._u_val = _u;
return _helper._t_val;
}
/*
* Word object.
*/
typedef uintptr_t Word;
/*
* A boxed object.
*/
template <typename _T>
struct _Boxed
{
private:
_T *_ptr;
public:
_Boxed() : _ptr(nullptr)
{
}
_Boxed(const _T &_x)
{
_ptr = (_T *)gc_malloc(sizeof(_T));
*_ptr = _x;
}
_Boxed(_T *_x) : _ptr(_x)
{
}
operator _T&() const
{
return *_ptr;
}
};
/*
* Value<T> object.
*/
template <typename _T>
struct Value
{
private:
uint8_t _val[sizeof(Word)];
public:
Value()
{
*(Word *)_val = (Word)0;
}
Value(const _T &_x)
{
if (sizeof(_T) <= sizeof(_val))
{
*(Word *)_val = (Word)0;
*(_T *)_val = _x;
}
else
{
_T *_ptr = (_T *)gc_malloc(sizeof(_T));
*_ptr = _x;
*(_T **)_val = _ptr;
}
}
operator const _T&() const
{
if (sizeof(_T) <= sizeof(_val))
return *(_T *)_val;
else
return **(_T **)_val;
}
bool operator== (Value<_T> _y) const
{
return *(Word *)_val == *(Word *)_y._val;
}
};
/*
* Optional<T> object.
*/
template <typename _T>
struct Optional
{
private:
bool _have;
Value<_T> _val;
public:
Optional() : _have(false), _val()
{
}
Optional(const _T &_x) : _have(true), _val(_x)
{
}
Optional(Value<_T> &_x) : _have(true), _val(_x)
{
}
operator const _T&() const
{
if (!_have)
error("Optional<T> value not present");
return _val;
}
operator Value<_T>() const
{
if (!_have)
error("Optional<T> value not present");
return _val;
}
bool _is_empty()
{
return !_have;
}
const _T &_get()
{
if (!_have)
error("Optional<T> value not present");
return _val;
}
};
/**
* Test if an optional is empty.
*/
template <typename _T>
inline PURE bool empty(Optional<_T> _x)
{
return _x._is_empty();
}
/**
* If an optional is non-empty, get its value.
*/
template <typename _T>
inline PURE const _T &get(Optional<_T> _x)
{
return _x._get();
}
#define _UNION_TAG_BITS 4
#define _UNION_TAG_MAX 16
#define _UNION_TAG_MASK (_UNION_TAG_MAX-1)
#define _UNION_DIRECT_SIZE (sizeof(Word)/2)
template <unsigned _tag, typename... _Ts>
struct _tag_helper
{
// Empty
};
template <unsigned _tag, typename _U, typename... _Ts>
struct _tag_helper<_tag, _U, _U, _Ts...>
{
constexpr unsigned _get_tag() const
{
static_assert(_tag <= _UNION_TAG_MAX,
"Union<...> with too many elements");
return _tag;
}
};
template <unsigned _tag, typename _U, typename _T, typename... _Ts>
struct _tag_helper<_tag, _U, _T, _Ts...>
{
_tag_helper<_tag + 1, _U, _Ts...> _helper;
constexpr unsigned _get_tag() const
{
return _helper._get_tag();
}
};
template <typename _T>
struct _is_boxed_helper
{
static const bool _value = false;
};
template <typename _T>
struct _is_boxed_helper<_Boxed<_T> *>
{
static const bool _value = true;
};
/*
* Union<T1, ..., Tn> object.
*/
template <typename... _Ts>
struct Union
{
private:
uint8_t _val[sizeof(Word)];
public:
Union()
{
*(Word *)_val = (Word)0;
}
template <typename _U>
Union(const _U &_x)
{
_tag_helper<0, _U, _Ts...> _helper;
unsigned _tag = _helper._get_tag();
if (sizeof(_U) <= _UNION_DIRECT_SIZE)
{
*(Word *)_val = (Word)0;
*(_U *)(_val + _UNION_DIRECT_SIZE) = _x;
}
else if (_is_boxed_helper<_U>::_value)
std::memcpy(_val, &_x, sizeof(_val));
else
{
_U *_ptr = (_U *)gc_malloc(sizeof(_U));
*_ptr = _x;
*(_U **)_val = _ptr;
}
*(Word *)_val = *(Word *)_val | (Word)_tag;
}
template <typename _U>
Union(Value<_U> _x)
{
_tag_helper<0, _U, _Ts...> _helper;
unsigned _tag = _helper._get_tag();
if (sizeof(_U) <= _UNION_DIRECT_SIZE)
{
*(Word *)_val = (Word)0;
*(_U *)(_val + _UNION_DIRECT_SIZE) = _U(_x);
}
else if (_is_boxed_helper<_U>::_value)
std::memcpy(_val, &_x, sizeof(_val));
else if (sizeof(_U) <= sizeof(_val))
{
_U *_ptr = (_U *)gc_malloc(sizeof(_U));
*_ptr = _U(_x);
*(_U **)_val = _ptr;
}
else
std::memcpy(_val, &_x, sizeof(_val));
*(Word *)_val = *(Word *)_val | (Word)_tag;
}
template <typename _U>
operator const _U&() const
{
_tag_helper<0, _U, _Ts...> _helper;
unsigned _tag = _helper._get_tag();
if ((*(Word *)_val & _UNION_TAG_MASK) != _tag)
{
_U *_null = nullptr;
return *_null;
}
if (sizeof(_U) <= _UNION_DIRECT_SIZE)
return *(_U *)(_val + _UNION_DIRECT_SIZE);
else
{
Word _ptr = *(Word *)_val;
_ptr -= (Word)_tag;
return *(_U *)_ptr;
}
}
template <typename _U>
operator Value<_U>() const
{
_tag_helper<0, _U, _Ts...> _helper;
unsigned _tag = _helper._get_tag();
if ((*(Word *)_val & _UNION_TAG_MASK) != _tag)
{
_U *_null = nullptr;
return *_null;
}
if (sizeof(_U) <= sizeof(_val))
return Value<_U>(*(_U *)(_val + _UNION_DIRECT_SIZE));
else
{
Word _ptr = *(Word *)_val;
_ptr -= (Word)_tag;
return _bit_cast<Value<_U>>(_ptr);
}
}
template <typename _U>
static constexpr unsigned index()
{
return (_tag_helper<0, _U, _Ts...>){}._get_tag();
}
bool operator== (Union<_Ts...> _y) const
{
return *(Word *)_val == *(Word *)_y._val;
}
unsigned _get_index()
{
return (unsigned)(*(Word *)_val & _UNION_TAG_MASK);
}
};
/**
* Get the index of the type of value stored in the union object.
*/
template <typename... _Ts>
inline PURE unsigned index(Union<_Ts...> _x)
{
return _x._get_index();
}
/*
* Result<T...> object.
*/
template <typename... _T>
struct Result { };
template <typename _T>
struct Result<_T>
{
_T _result_0;
};
template <typename _T, typename _U>
struct Result<_T, _U>
{
_T _result_0;
_U _result_1;
};
template <typename _T, typename _U, typename _V>
struct Result<_T, _U, _V>
{
_T _result_0;
_U _result_1;
_V _result_2;
};
template <typename _T, typename _U, typename _V, typename _W>
struct Result<_T, _U, _V, _W>
{
_T _result_0;
_U _result_1;
_V _result_2;
_W _result_3;
};
template <typename _T, typename _U, typename _V, typename _W, typename _X>
struct Result<_T, _U, _V, _W, _X>
{
_T _result_0;
_U _result_1;
_V _result_2;
_W _result_3;
_X _result_4;
};
template <typename _T, typename _U, typename _V, typename _W, typename _X, typename _Y>
struct Result<_T, _U, _V, _W, _X, _Y>
{
_T _result_0;
_U _result_1;
_V _result_2;
_W _result_3;
_X _result_4;
_Y _result_5;
};
template <typename _T, typename _U, typename _V, typename _W, typename _X, typename _Y, typename _Z>
struct Result<_T, _U, _V, _W, _X, _Y, _Z>
{
_T _result_0;
_U _result_1;
_V _result_2;
_W _result_3;
_X _result_4;
_Y _result_5;
_Z _result_6;
};
template <typename _T, typename _U, typename _V, typename _W, typename _X, typename _Y, typename _Z, typename _A>
struct Result<_T, _U, _V, _W, _X, _Y, _Z, _A>
{
_T _result_0;
_U _result_1;
_V _result_2;
_W _result_3;
_X _result_4;
_Y _result_5;
_Z _result_6;
_A _result_7;
};
} /* namespace F */
#endif /* FVALUE_H */