1/*
2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28namespace JSC {
29
30 struct ResultType {
31 private:
32 friend struct OperandTypes;
33
34 using Type = uint8_t;
35 static constexpr Type TypeInt32 = 0x1 << 0;
36 static constexpr Type TypeMaybeNumber = 0x1 << 1;
37 static constexpr Type TypeMaybeString = 0x1 << 2;
38 static constexpr Type TypeMaybeBigInt = 0x1 << 3;
39 static constexpr Type TypeMaybeNull = 0x1 << 4;
40 static constexpr Type TypeMaybeBool = 0x1 << 5;
41 static constexpr Type TypeMaybeOther = 0x1 << 6;
42
43 static constexpr Type TypeBits = TypeMaybeNumber | TypeMaybeString | TypeMaybeBigInt | TypeMaybeNull | TypeMaybeBool | TypeMaybeOther;
44
45 public:
46 static constexpr int numBitsNeeded = 7;
47 static_assert((TypeBits & ((1 << numBitsNeeded) - 1)) == TypeBits, "This is necessary for correctness.");
48
49 constexpr explicit ResultType(Type type)
50 : m_bits(type)
51 {
52 }
53
54 constexpr bool isInt32() const
55 {
56 return m_bits & TypeInt32;
57 }
58
59 constexpr bool definitelyIsNumber() const
60 {
61 return (m_bits & TypeBits) == TypeMaybeNumber;
62 }
63
64 constexpr bool definitelyIsString() const
65 {
66 return (m_bits & TypeBits) == TypeMaybeString;
67 }
68
69 constexpr bool definitelyIsBoolean() const
70 {
71 return (m_bits & TypeBits) == TypeMaybeBool;
72 }
73
74 constexpr bool definitelyIsBigInt() const
75 {
76 return (m_bits & TypeBits) == TypeMaybeBigInt;
77 }
78
79 constexpr bool mightBeNumber() const
80 {
81 return m_bits & TypeMaybeNumber;
82 }
83
84 constexpr bool isNotNumber() const
85 {
86 return !mightBeNumber();
87 }
88
89 constexpr bool mightBeBigInt() const
90 {
91 return m_bits & TypeMaybeBigInt;
92 }
93
94 constexpr bool isNotBigInt() const
95 {
96 return !mightBeBigInt();
97 }
98
99 static constexpr ResultType nullType()
100 {
101 return ResultType(TypeMaybeNull);
102 }
103
104 static constexpr ResultType booleanType()
105 {
106 return ResultType(TypeMaybeBool);
107 }
108
109 static constexpr ResultType numberType()
110 {
111 return ResultType(TypeMaybeNumber);
112 }
113
114 static constexpr ResultType numberTypeIsInt32()
115 {
116 return ResultType(TypeInt32 | TypeMaybeNumber);
117 }
118
119 static constexpr ResultType stringOrNumberType()
120 {
121 return ResultType(TypeMaybeNumber | TypeMaybeString);
122 }
123
124 static constexpr ResultType addResultType()
125 {
126 return ResultType(TypeMaybeNumber | TypeMaybeString | TypeMaybeBigInt);
127 }
128
129 static constexpr ResultType stringType()
130 {
131 return ResultType(TypeMaybeString);
132 }
133
134 static constexpr ResultType bigIntType()
135 {
136 return ResultType(TypeMaybeBigInt);
137 }
138
139 static constexpr ResultType bigIntOrInt32Type()
140 {
141 return ResultType(TypeMaybeBigInt | TypeInt32);
142 }
143
144 static constexpr ResultType unknownType()
145 {
146 return ResultType(TypeBits);
147 }
148
149 static constexpr ResultType forAdd(ResultType op1, ResultType op2)
150 {
151 if (op1.definitelyIsNumber() && op2.definitelyIsNumber())
152 return numberType();
153 if (op1.definitelyIsString() || op2.definitelyIsString())
154 return stringType();
155 if (op1.definitelyIsBigInt() && op2.definitelyIsBigInt())
156 return bigIntType();
157 return addResultType();
158 }
159
160 // Unlike in C, a logical op produces the value of the
161 // last expression evaluated (and not true or false).
162 static constexpr ResultType forLogicalOp(ResultType op1, ResultType op2)
163 {
164 if (op1.definitelyIsBoolean() && op2.definitelyIsBoolean())
165 return booleanType();
166 if (op1.definitelyIsNumber() && op2.definitelyIsNumber())
167 return numberType();
168 if (op1.definitelyIsString() && op2.definitelyIsString())
169 return stringType();
170 if (op1.definitelyIsBigInt() && op2.definitelyIsBigInt())
171 return bigIntType();
172 return unknownType();
173 }
174
175 static constexpr ResultType forBitOp()
176 {
177 return bigIntOrInt32Type();
178 }
179
180 constexpr Type bits() const { return m_bits; }
181
182 void dump(PrintStream& out) const
183 {
184 // FIXME: more meaningful information
185 // https://bugs.webkit.org/show_bug.cgi?id=190930
186 out.print(bits());
187 }
188
189 private:
190 Type m_bits;
191 };
192
193 struct OperandTypes
194 {
195 OperandTypes(ResultType first = ResultType::unknownType(), ResultType second = ResultType::unknownType())
196 {
197 m_first = first.m_bits;
198 m_second = second.m_bits;
199 }
200
201 ResultType::Type m_first;
202 ResultType::Type m_second;
203
204 ResultType first() const
205 {
206 return ResultType(m_first);
207 }
208
209 ResultType second() const
210 {
211 return ResultType(m_second);
212 }
213
214 uint16_t bits()
215 {
216 static_assert(sizeof(OperandTypes) == sizeof(uint16_t));
217 return bitwise_cast<uint16_t>(*this);
218 }
219
220 static OperandTypes fromBits(uint16_t bits)
221 {
222 return bitwise_cast<OperandTypes>(bits);
223 }
224
225 void dump(PrintStream& out) const
226 {
227 out.print("OperandTypes(", first(), ", ", second(), ")");
228 }
229 };
230
231} // namespace JSC
232