1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/compiler/type-hints.h"
6
7 namespace v8 {
8 namespace internal {
9 namespace compiler {
10
operator <<(std::ostream & os,BinaryOperationHints::Hint hint)11 std::ostream& operator<<(std::ostream& os, BinaryOperationHints::Hint hint) {
12 switch (hint) {
13 case BinaryOperationHints::kNone:
14 return os << "None";
15 case BinaryOperationHints::kSignedSmall:
16 return os << "SignedSmall";
17 case BinaryOperationHints::kSigned32:
18 return os << "Signed32";
19 case BinaryOperationHints::kNumberOrUndefined:
20 return os << "NumberOrUndefined";
21 case BinaryOperationHints::kString:
22 return os << "String";
23 case BinaryOperationHints::kAny:
24 return os << "Any";
25 }
26 UNREACHABLE();
27 return os;
28 }
29
operator <<(std::ostream & os,BinaryOperationHints hints)30 std::ostream& operator<<(std::ostream& os, BinaryOperationHints hints) {
31 return os << hints.left() << "*" << hints.right() << "->" << hints.result();
32 }
33
operator <<(std::ostream & os,CompareOperationHints::Hint hint)34 std::ostream& operator<<(std::ostream& os, CompareOperationHints::Hint hint) {
35 switch (hint) {
36 case CompareOperationHints::kNone:
37 return os << "None";
38 case CompareOperationHints::kBoolean:
39 return os << "Boolean";
40 case CompareOperationHints::kSignedSmall:
41 return os << "SignedSmall";
42 case CompareOperationHints::kNumber:
43 return os << "Number";
44 case CompareOperationHints::kString:
45 return os << "String";
46 case CompareOperationHints::kInternalizedString:
47 return os << "InternalizedString";
48 case CompareOperationHints::kUniqueName:
49 return os << "UniqueName";
50 case CompareOperationHints::kReceiver:
51 return os << "Receiver";
52 case CompareOperationHints::kAny:
53 return os << "Any";
54 }
55 UNREACHABLE();
56 return os;
57 }
58
operator <<(std::ostream & os,CompareOperationHints hints)59 std::ostream& operator<<(std::ostream& os, CompareOperationHints hints) {
60 return os << hints.left() << "*" << hints.right() << " (" << hints.combined()
61 << ")";
62 }
63
operator <<(std::ostream & os,ToBooleanHint hint)64 std::ostream& operator<<(std::ostream& os, ToBooleanHint hint) {
65 switch (hint) {
66 case ToBooleanHint::kNone:
67 return os << "None";
68 case ToBooleanHint::kUndefined:
69 return os << "Undefined";
70 case ToBooleanHint::kBoolean:
71 return os << "Boolean";
72 case ToBooleanHint::kNull:
73 return os << "Null";
74 case ToBooleanHint::kSmallInteger:
75 return os << "SmallInteger";
76 case ToBooleanHint::kReceiver:
77 return os << "Receiver";
78 case ToBooleanHint::kString:
79 return os << "String";
80 case ToBooleanHint::kSymbol:
81 return os << "Symbol";
82 case ToBooleanHint::kHeapNumber:
83 return os << "HeapNumber";
84 case ToBooleanHint::kSimdValue:
85 return os << "SimdValue";
86 case ToBooleanHint::kAny:
87 return os << "Any";
88 }
89 UNREACHABLE();
90 return os;
91 }
92
operator <<(std::ostream & os,ToBooleanHints hints)93 std::ostream& operator<<(std::ostream& os, ToBooleanHints hints) {
94 if (hints == ToBooleanHint::kAny) return os << "Any";
95 if (hints == ToBooleanHint::kNone) return os << "None";
96 bool first = true;
97 for (ToBooleanHints::mask_type i = 0; i < sizeof(i) * CHAR_BIT; ++i) {
98 ToBooleanHint const hint = static_cast<ToBooleanHint>(1u << i);
99 if (hints & hint) {
100 if (!first) os << "|";
101 first = false;
102 os << hint;
103 }
104 }
105 return os;
106 }
107
108 // static
Is(Hint h1,Hint h2)109 bool BinaryOperationHints::Is(Hint h1, Hint h2) {
110 if (h1 == h2) return true;
111 switch (h1) {
112 case kNone:
113 return true;
114 case kSignedSmall:
115 return h2 == kSigned32 || h2 == kNumberOrUndefined || h2 == kAny;
116 case kSigned32:
117 return h2 == kNumberOrUndefined || h2 == kAny;
118 case kNumberOrUndefined:
119 return h2 == kAny;
120 case kString:
121 return h2 == kAny;
122 case kAny:
123 return false;
124 }
125 UNREACHABLE();
126 return false;
127 }
128
129 // static
Combine(Hint h1,Hint h2)130 BinaryOperationHints::Hint BinaryOperationHints::Combine(Hint h1, Hint h2) {
131 if (Is(h1, h2)) return h2;
132 if (Is(h2, h1)) return h1;
133 return kAny;
134 }
135
136 } // namespace compiler
137 } // namespace internal
138 } // namespace v8
139