-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheri_compressed_cap_macros.h
123 lines (110 loc) · 6.56 KB
/
cheri_compressed_cap_macros.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
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2018-2020 Alex Richardson
* All rights reserved.
*
* This software was developed by SRI International and the University of
* Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
* ("CTSRD"), as part of the DARPA CRASH research programme.
*
* This software was developed by SRI International and the University of
* Cambridge Computer Laboratory (Department of Computer Science and
* Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
* DARPA SSITH research programme.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 _cc_debug_assert
#ifdef cheri_debug_assert
#define _cc_debug_assert(cond) cheri_debug_assert(cond)
#else
#include <assert.h>
#define _cc_debug_assert(cond) assert(cond)
#endif
#endif
#if !defined(NDEBUG) && defined(__cplusplus) && defined(__EXCEPTIONS)
#include <stdexcept>
#define _cc_api_requirement(cond, msg) \
do { \
if (!(cond)) { \
throw std::invalid_argument(msg); \
} \
} while (false)
#else
#define _cc_api_requirement(cond, msg) _cc_debug_assert((cond) && msg)
#endif
#ifndef _CC_CONCAT
#define _CC_CONCAT1(x, y) x##y
#define _CC_CONCAT(x, y) _CC_CONCAT1(x, y)
#define _CC_EXPAND1(x) x
#define _CC_EXPAND(x) _CC_EXPAND1(x)
#define _CC_STRINGIFY2(x) #x
#define _CC_STRINGIFY1(x) _CC_STRINGIFY2(x)
#define _CC_STRINGIFY(x) _CC_STRINGIFY1(x)
#ifdef __cplusplus
// Some versions of GCC dont't like _Static_assert() in C++ mode
#define _CC_STATIC_ASSERT(cond, msg) static_assert(cond, msg)
#else
#define _CC_STATIC_ASSERT(cond, msg) _Static_assert(cond, msg)
#endif
#define _cc_N(name) _CC_CONCAT(_CC_CONCAT(_CC_CONCAT(cc, CC_FORMAT_LOWER), _), name)
#define _CC_N(name) _CC_CONCAT(_CC_CONCAT(_CC_CONCAT(CC, CC_FORMAT_UPPER), _), name)
#define _CC_BITMASK64(nbits) ((UINT64_C(1) << (nbits)) - UINT64_C(1))
// NB: Do not use GNU statement expressions as this is used by LLVM which warns
// on any uses during its build. These are therefore unsafe if any arguments
// have side-effects.
#define _CC_MIN(a, b) ((a) < (b) ? (a) : (b))
#define _CC_MAX(a, b) ((a) > (b) ? (a) : (b))
#define _CC_FIELD(name, last, start) \
_CC_N(FIELD_##name##_START) = (start - _CC_N(ADDR_WIDTH)), \
_CC_N(FIELD_##name##_LAST) = (last - _CC_N(ADDR_WIDTH)), \
_CC_N(FIELD_##name##_SIZE) = _CC_N(FIELD_##name##_LAST) - _CC_N(FIELD_##name##_START) + 1, \
_CC_N(FIELD_##name##_MASK_NOT_SHIFTED) = _CC_BITMASK64(_CC_N(FIELD_##name##_SIZE)), \
_CC_N(FIELD_##name##_MASK64) = (uint64_t)_CC_N(FIELD_##name##_MASK_NOT_SHIFTED) << _CC_N(FIELD_##name##_START), \
_CC_N(FIELD_##name##_MAX_VALUE) = _CC_N(FIELD_##name##_MASK_NOT_SHIFTED)
#define _CC_ENCODE_FIELD(value, name) \
((uint64_t)((value) & _CC_N(FIELD_##name##_MAX_VALUE)) << _CC_N(FIELD_##name##_START))
#define _CC_EXTRACT_FIELD(pesbt, name) _cc_N(getbits)((pesbt), _CC_N(FIELD_##name##_START), _CC_N(FIELD_##name##_SIZE))
#define _CC_DEPOSIT_FIELD(pesbt, value, name) \
__extension__({ \
_cc_debug_assert(value <= _CC_N(FIELD_##name##_MAX_VALUE)); \
((pesbt) & ~_CC_N(FIELD_##name##_MASK64)) | _CC_ENCODE_FIELD(value, name); \
})
#define _CC_ENCODE_SPLIT_FIELD(value, HIGH, LOW) \
_CC_ENCODE_FIELD((value) >> _CC_N(FIELD_##LOW##_SIZE), HIGH) | _CC_ENCODE_FIELD(value, LOW)
#define _CC_EXTRACT_SPLIT_FIELD(pesbt, HIGH, LOW) \
(_CC_EXTRACT_FIELD(pesbt, LOW) | (_CC_EXTRACT_FIELD(pesbt, HIGH) << _CC_N(FIELD_##LOW##_SIZE)))
#define _CC_ENCODE_SPLIT_EXPONENT(E) _CC_ENCODE_SPLIT_FIELD(E, EXPONENT_HIGH_PART, EXPONENT_LOW_PART)
#define _CC_EXTRACT_SPLIT_EXPONENT(pesbt) _CC_EXTRACT_SPLIT_FIELD(pesbt, EXPONENT_HIGH_PART, EXPONENT_LOW_PART)
#define _CC_SPECIAL_OTYPE(name, val) \
_CC_N(name) = (_CC_N(SPECIAL_OTYPE_VAL)(val)), _CC_N(name##_SIGNED) = (_CC_N(SPECIAL_OTYPE_VAL_SIGNED)(val))
#ifdef __cplusplus
template <size_t a, size_t b> static constexpr bool check_same() {
static_assert(a == b, "");
return true;
}
#define _CC_STATIC_ASSERT_SAME(a, b) static_assert(check_same<a, b>(), "")
#else
#define _CC_STATIC_ASSERT_SAME(a, b) _Static_assert((a) == (b), "")
#endif
#endif // _CC_CONCAT