forked from bpftrace/bpftrace
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinteger_types.cpp
More file actions
64 lines (58 loc) · 1.84 KB
/
Copy pathinteger_types.cpp
File metadata and controls
64 lines (58 loc) · 1.84 KB
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
#include <climits>
#include <cstdint>
#include <optional>
#include "integer_types.h"
namespace bpftrace::ast {
SizedType get_integer_type(uint64_t n)
{
// Make the smallest possible signed sizedType based on n
// or an unsigned SizedType if it exceeds the largest int64
SizedType ty;
if (n <= std::numeric_limits<int8_t>::max()) {
ty = CreateInt8();
} else if (n <= std::numeric_limits<int16_t>::max()) {
ty = CreateInt16();
} else if (n <= std::numeric_limits<int32_t>::max()) {
ty = CreateInt32();
} else if (n <= std::numeric_limits<int64_t>::max()) {
ty = CreateInt64();
} else {
return CreateUInt64();
}
// Non-negative literals that fit in a signed type can be treated as
// either signed or unsigned. Values exceeding INT64_MAX require
// unsigned representation and are not flexible.
ty.SetSignFlexible(true);
return ty;
}
std::optional<SizedType> get_signed_integer_type(uint64_t n)
{
if (n <= std::numeric_limits<int8_t>::max()) {
return CreateInt8();
} else if (n <= std::numeric_limits<int16_t>::max()) {
return CreateInt16();
} else if (n <= std::numeric_limits<int32_t>::max()) {
return CreateInt32();
} else if (n <= std::numeric_limits<int64_t>::max()) {
return CreateInt64();
} else {
return std::nullopt;
}
}
SizedType get_signed_integer_type(int64_t n)
{
// Make the smallest possible sizedType based on n
if (std::numeric_limits<int8_t>::min() <= n &&
n <= std::numeric_limits<int8_t>::max()) {
return CreateInt8();
} else if (std::numeric_limits<int16_t>::min() <= n &&
n <= std::numeric_limits<int16_t>::max()) {
return CreateInt16();
} else if (std::numeric_limits<int32_t>::min() <= n &&
n <= std::numeric_limits<int32_t>::max()) {
return CreateInt32();
} else {
return CreateInt64();
}
}
} // namespace bpftrace::ast