summaryrefslogtreecommitdiff
path: root/erts/emulator/beam/jit/beam_jit_types.hpp
blob: 74edc66496293792e36f0088bbd081d95500f779 (plain)
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
/*
 * %CopyrightBegin%
 *
 * Copyright Ericsson AB 2023. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * %CopyrightEnd%
 */

#ifndef __BEAM_JIT_TYPES_HPP__
#define __BEAM_JIT_TYPES_HPP__

/* Type-safe wrapper around the definitions in beam_types.h. We've redefined it
 * as an `enum class` to force the usage of our helpers, which lets us check
 * for common usage errors at compile time. */
enum class BeamTypeId : int {
    None = BEAM_TYPE_NONE,

    Atom = BEAM_TYPE_ATOM,
    Bitstring = BEAM_TYPE_BITSTRING,
    BsMatchState = BEAM_TYPE_BS_MATCHSTATE,
    Cons = BEAM_TYPE_CONS,
    Float = BEAM_TYPE_FLOAT,
    Fun = BEAM_TYPE_FUN,
    Integer = BEAM_TYPE_INTEGER,
    Map = BEAM_TYPE_MAP,
    Nil = BEAM_TYPE_NIL,
    Pid = BEAM_TYPE_PID,
    Port = BEAM_TYPE_PORT,
    Reference = BEAM_TYPE_REFERENCE,
    Tuple = BEAM_TYPE_TUPLE,

    Any = BEAM_TYPE_ANY,

    Identifier = Pid | Port | Reference,
    List = Cons | Nil,
    Number = Float | Integer,

    /** @brief Types that can be boxed, including those that may also be
     * immediates (e.g. pids, integers). */
    MaybeBoxed = Bitstring | BsMatchState | Float | Fun | Integer | Map | Pid |
                 Port | Reference | Tuple,
    /** @brief Types that can be immediates, including those that may also be
     * boxed (e.g. pids, integers). */
    MaybeImmediate = Atom | Integer | Nil | Pid | Port,

    /** @brief Types that are _always_ boxed. */
    AlwaysBoxed = MaybeBoxed & ~(Cons | MaybeImmediate),
    /** @brief Types that are _always_ immediates. */
    AlwaysImmediate = MaybeImmediate & ~(Cons | MaybeBoxed),
};

template<BeamTypeId... T>
struct BeamTypeIdUnion;

template<>
struct BeamTypeIdUnion<> {
    static constexpr BeamTypeId value() {
        return BeamTypeId::None;
    }
};

template<BeamTypeId T, BeamTypeId... Rest>
struct BeamTypeIdUnion<T, Rest...> : BeamTypeIdUnion<Rest...> {
    using integral = std::underlying_type_t<BeamTypeId>;
    using super = BeamTypeIdUnion<Rest...>;

    /* Overlapping type specifications are redundant at best and a subtle error
     * at worst. We've had several bugs where `Integer | MaybeBoxed` was used
     * instead of `Integer | AlwaysBoxed` or similar, and erroneously drew the
     * conclusion that the value is always an integer when not boxed, when it
     * could also be a pid or port. */
    static constexpr bool no_overlap =
            (static_cast<integral>(super::value()) &
             static_cast<integral>(T)) == BEAM_TYPE_NONE;
    static constexpr bool no_boxed_overlap =
            no_overlap || (super::value() != BeamTypeId::MaybeBoxed &&
                           T != BeamTypeId::MaybeBoxed);
    static constexpr bool no_immed_overlap =
            no_overlap || (super::value() != BeamTypeId::MaybeImmediate &&
                           T != BeamTypeId::MaybeImmediate);

    static_assert(no_boxed_overlap,
                  "types must not overlap, did you mean to use "
                  "BeamTypeId::AlwaysBoxed here?");
    static_assert(no_immed_overlap,
                  "types must not overlap, did you mean to use "
                  "BeamTypeId::AlwaysImmediate here?");
    static_assert(no_overlap || no_boxed_overlap || no_immed_overlap,
                  "types must not overlap");

    static constexpr bool is_single_typed() {
        constexpr auto V = static_cast<integral>(value());
        return (static_cast<integral>(V) & (static_cast<integral>(V) - 1)) ==
               BEAM_TYPE_NONE;
    }

    static constexpr BeamTypeId value() {
        return static_cast<BeamTypeId>(static_cast<integral>(super::value()) |
                                       static_cast<integral>(T));
    }
};

struct BeamArgType : public BeamType {
    BeamTypeId type() const {
        return static_cast<BeamTypeId>(BeamType::type_union);
    }

    bool hasLowerBound() const {
        return metadata_flags & BEAM_TYPE_HAS_LOWER_BOUND;
    }

    bool hasUpperBound() const {
        return metadata_flags & BEAM_TYPE_HAS_UPPER_BOUND;
    }

    bool hasUnit() const {
        return metadata_flags & BEAM_TYPE_HAS_UNIT;
    }

    auto max() const {
        ASSERT(hasUpperBound());
        return BeamType::max;
    }

    auto min() const {
        ASSERT(hasLowerBound());
        return BeamType::min;
    }

    auto unit() const {
        ASSERT(hasUnit());
        return BeamType::size_unit;
    }
};

static_assert(std::is_standard_layout<BeamArgType>::value);

#endif