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 // We have to initialize one of the int to ensure that
198 // the entire struct is initialized.
199 m_u.i = 0;
200 m_u.rds.first = first.m_bits;
201 m_u.rds.second = second.m_bits;
202 }
203
204 union {
205 struct {
206 ResultType::Type first;
207 ResultType::Type second;
208 } rds;
209 int i;
210 } m_u;
211
212 ResultType first() const
213 {
214 return ResultType(m_u.rds.first);
215 }
216
217 ResultType second() const
218 {
219 return ResultType(m_u.rds.second);
220 }
221
222 int toInt()
223 {
224 return m_u.i;
225 }
226 static OperandTypes fromInt(int value)
227 {
228 OperandTypes types;
229 types.m_u.i = value;
230 return types;
231 }
232
233 void dump(PrintStream& out) const
234 {
235 out.print("OperandTypes(", first(), ", ", second(), ")");
236 }
237 };
238
239} // namespace JSC
240