Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(227)

Side by Side Diff: test/unittests/compiler/js-operator-unittest.cc

Issue 1092353002: [strong] Disallow implicit conversions for binary arithmetic operations (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: cl feedback Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/compiler/js-operator.h" 5 #include "src/compiler/js-operator.h"
6 #include "src/compiler/opcodes.h" 6 #include "src/compiler/opcodes.h"
7 #include "src/compiler/operator.h" 7 #include "src/compiler/operator.h"
8 #include "src/compiler/operator-properties.h" 8 #include "src/compiler/operator-properties.h"
9 #include "test/unittests/test-utils.h" 9 #include "test/unittests/test-utils.h"
10 10
11 namespace v8 { 11 namespace v8 {
12 namespace internal { 12 namespace internal {
13 namespace compiler { 13 namespace compiler {
14 14
15 // -----------------------------------------------------------------------------
16 // Shared operators.
17
18 15
19 namespace { 16 namespace {
20 17
21 struct SharedOperator { 18 struct OperatorParams {
22 const Operator* (JSOperatorBuilder::*constructor)();
23 IrOpcode::Value opcode; 19 IrOpcode::Value opcode;
24 Operator::Properties properties; 20 Operator::Properties properties;
25 int value_input_count; 21 int value_input_count;
26 int frame_state_input_count; 22 int frame_state_input_count;
27 int effect_input_count; 23 int effect_input_count;
28 int control_input_count; 24 int control_input_count;
29 int value_output_count; 25 int value_output_count;
30 int effect_output_count; 26 int effect_output_count;
31 int control_output_count; 27 int control_output_count;
32 }; 28 };
33 29
34 30
35 std::ostream& operator<<(std::ostream& os, const SharedOperator& sop) { 31 struct SharedOperator : OperatorParams {
36 return os << IrOpcode::Mnemonic(sop.opcode); 32 const Operator* (JSOperatorBuilder::*constructor)();
37 } 33 SharedOperator(const Operator* (JSOperatorBuilder::*cached)(),
34 IrOpcode::Value opcode, Operator::Properties properties,
35 int value_input_count, int frame_state_input_count,
36 int effect_input_count, int control_input_count,
37 int value_output_count, int effect_output_count,
38 int control_output_count) :
39 OperatorParams({opcode, properties, value_input_count,
40 frame_state_input_count, effect_input_count,
41 control_input_count, value_output_count,
42 effect_output_count, control_output_count}),
43 constructor(cached) {}
44 };
38 45
39 46
40 const SharedOperator kSharedOperators[] = { 47 const SharedOperator kSharedOperators[] = {
41 #define SHARED(Name, properties, value_input_count, frame_state_input_count, \ 48 #define SHARED(Name, properties, value_input_count, frame_state_input_count, \
42 effect_input_count, control_input_count, value_output_count, \ 49 effect_input_count, control_input_count, value_output_count, \
43 effect_output_count, control_output_count) \ 50 effect_output_count, control_output_count) \
44 { \ 51 { \
45 &JSOperatorBuilder::Name, IrOpcode::kJS##Name, properties, \ 52 &JSOperatorBuilder::Name, IrOpcode::kJS##Name, properties, \
46 value_input_count, frame_state_input_count, effect_input_count, \ 53 value_input_count, frame_state_input_count, effect_input_count, \
47 control_input_count, value_output_count, effect_output_count, \ 54 control_input_count, value_output_count, effect_output_count, \
48 control_output_count \ 55 control_output_count \
49 } 56 }
50 SHARED(Equal, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2), 57 SHARED(Equal, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
51 SHARED(NotEqual, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2), 58 SHARED(NotEqual, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
52 SHARED(StrictEqual, Operator::kPure, 2, 0, 0, 0, 1, 0, 0), 59 SHARED(StrictEqual, Operator::kPure, 2, 0, 0, 0, 1, 0, 0),
53 SHARED(StrictNotEqual, Operator::kPure, 2, 0, 0, 0, 1, 0, 0), 60 SHARED(StrictNotEqual, Operator::kPure, 2, 0, 0, 0, 1, 0, 0),
61 SHARED(ToBoolean, Operator::kPure, 1, 0, 0, 0, 1, 0, 0),
62 SHARED(ToNumber, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2),
63 SHARED(ToString, Operator::kNoProperties, 1, 0, 1, 1, 1, 1, 2),
64 SHARED(ToName, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2),
65 SHARED(ToObject, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2),
66 SHARED(Yield, Operator::kNoProperties, 1, 0, 1, 1, 1, 1, 2),
67 SHARED(Create, Operator::kEliminatable, 0, 0, 1, 0, 1, 1, 0),
68 SHARED(HasProperty, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
69 SHARED(TypeOf, Operator::kPure, 1, 0, 0, 0, 1, 0, 0),
70 SHARED(InstanceOf, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
71 SHARED(CreateFunctionContext, Operator::kNoProperties, 1, 0, 1, 1, 1, 1, 2),
72 SHARED(CreateWithContext, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
73 SHARED(CreateBlockContext, Operator::kNoProperties, 2, 0, 1, 1, 1, 1, 2),
74 SHARED(CreateModuleContext, Operator::kNoProperties, 2, 0, 1, 1, 1, 1, 2),
75 SHARED(CreateScriptContext, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2)
76 #undef SHARED
77 };
78
79
80 struct SharedOperatorWithLanguageMode : OperatorParams {
81 const Operator* (JSOperatorBuilder::*constructor)(LanguageMode language_mode);
82 SharedOperatorWithLanguageMode(const Operator* (JSOperatorBuilder::*cached)
83 (LanguageMode language_mode),
84 IrOpcode::Value opcode, Operator::Properties properties,
85 int value_input_count, int frame_state_input_count,
86 int effect_input_count, int control_input_count,
87 int value_output_count, int effect_output_count,
88 int control_output_count) :
89 OperatorParams({opcode, properties, value_input_count,
90 frame_state_input_count, effect_input_count,
91 control_input_count, value_output_count,
92 effect_output_count, control_output_count}),
93 constructor(cached) {}
94 };
95
96
97 const SharedOperatorWithLanguageMode kSharedOperatorsWithlanguageMode[] = {
98 #define SHARED(Name, properties, value_input_count, frame_state_input_count, \
99 effect_input_count, control_input_count, value_output_count, \
100 effect_output_count, control_output_count) \
101 { \
102 &JSOperatorBuilder::Name, IrOpcode::kJS##Name, properties, \
103 value_input_count, frame_state_input_count, effect_input_count, \
104 control_input_count, value_output_count, effect_output_count, \
105 control_output_count \
106 }
54 SHARED(LessThan, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2), 107 SHARED(LessThan, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
55 SHARED(GreaterThan, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2), 108 SHARED(GreaterThan, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
56 SHARED(LessThanOrEqual, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2), 109 SHARED(LessThanOrEqual, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
57 SHARED(GreaterThanOrEqual, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2), 110 SHARED(GreaterThanOrEqual, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
58 SHARED(BitwiseOr, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 111 SHARED(BitwiseOr, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
59 SHARED(BitwiseXor, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 112 SHARED(BitwiseXor, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
60 SHARED(BitwiseAnd, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 113 SHARED(BitwiseAnd, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
61 SHARED(ShiftLeft, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 114 SHARED(ShiftLeft, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
62 SHARED(ShiftRight, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 115 SHARED(ShiftRight, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
63 SHARED(ShiftRightLogical, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 116 SHARED(ShiftRightLogical, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
64 SHARED(Add, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 117 SHARED(Add, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
65 SHARED(Subtract, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 118 SHARED(Subtract, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
66 SHARED(Multiply, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 119 SHARED(Multiply, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
67 SHARED(Divide, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 120 SHARED(Divide, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
68 SHARED(Modulus, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 121 SHARED(Modulus, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
69 SHARED(UnaryNot, Operator::kPure, 1, 0, 0, 0, 1, 0, 0), 122 SHARED(UnaryNot, Operator::kPure, 1, 0, 0, 0, 1, 0, 0),
70 SHARED(ToBoolean, Operator::kPure, 1, 0, 0, 0, 1, 0, 0),
71 SHARED(ToNumber, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2),
72 SHARED(ToString, Operator::kNoProperties, 1, 0, 1, 1, 1, 1, 2),
73 SHARED(ToName, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2),
74 SHARED(ToObject, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2),
75 SHARED(Yield, Operator::kNoProperties, 1, 0, 1, 1, 1, 1, 2),
76 SHARED(Create, Operator::kEliminatable, 0, 0, 1, 0, 1, 1, 0),
77 SHARED(HasProperty, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
78 SHARED(TypeOf, Operator::kPure, 1, 0, 0, 0, 1, 0, 0),
79 SHARED(InstanceOf, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
80 SHARED(CreateFunctionContext, Operator::kNoProperties, 1, 0, 1, 1, 1, 1, 2),
81 SHARED(CreateWithContext, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
82 SHARED(CreateBlockContext, Operator::kNoProperties, 2, 0, 1, 1, 1, 1, 2),
83 SHARED(CreateModuleContext, Operator::kNoProperties, 2, 0, 1, 1, 1, 1, 2),
84 SHARED(CreateScriptContext, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2)
85 #undef SHARED 123 #undef SHARED
86 }; 124 };
87 125
126
127 void testNumberOfInputsAndOutputs(const OperatorParams& sop,
128 const Operator* op) {
129 const int context_input_count = 1;
130 EXPECT_EQ(sop.value_input_count, op->ValueInputCount());
131 EXPECT_EQ(context_input_count, OperatorProperties::GetContextInputCount(op));
132 EXPECT_EQ(sop.frame_state_input_count,
133 OperatorProperties::GetFrameStateInputCount(op));
134 EXPECT_EQ(sop.effect_input_count, op->EffectInputCount());
135 EXPECT_EQ(sop.control_input_count, op->ControlInputCount());
136 EXPECT_EQ(sop.value_input_count + context_input_count +
137 sop.frame_state_input_count + sop.effect_input_count +
138 sop.control_input_count,
139 OperatorProperties::GetTotalInputCount(op));
140
141 EXPECT_EQ(sop.value_output_count, op->ValueOutputCount());
142 EXPECT_EQ(sop.effect_output_count, op->EffectOutputCount());
143 EXPECT_EQ(sop.control_output_count, op->ControlOutputCount());
144 }
145
146
147 std::ostream& operator<<(std::ostream& os, const SharedOperator& sop) {
148 return os << IrOpcode::Mnemonic(sop.opcode);
149 }
150
151 std::ostream& operator<<(std::ostream& os,
152 const SharedOperatorWithLanguageMode& sop) {
153 return os << IrOpcode::Mnemonic(sop.opcode);
154 }
155
88 } // namespace 156 } // namespace
89 157
90 158
159 // -----------------------------------------------------------------------------
160 // Shared operators.
161
162
91 class JSSharedOperatorTest 163 class JSSharedOperatorTest
92 : public TestWithZone, 164 : public TestWithZone,
93 public ::testing::WithParamInterface<SharedOperator> {}; 165 public ::testing::WithParamInterface<SharedOperator> {};
94 166
95 167
96 TEST_P(JSSharedOperatorTest, InstancesAreGloballyShared) { 168 TEST_P(JSSharedOperatorTest, InstancesAreGloballyShared) {
97 const SharedOperator& sop = GetParam(); 169 const SharedOperator& sop = GetParam();
98 JSOperatorBuilder javascript1(zone()); 170 JSOperatorBuilder javascript1(zone());
99 JSOperatorBuilder javascript2(zone()); 171 JSOperatorBuilder javascript2(zone());
100 EXPECT_EQ((javascript1.*sop.constructor)(), (javascript2.*sop.constructor)()); 172 EXPECT_EQ((javascript1.*sop.constructor)(), (javascript2.*sop.constructor)());
101 } 173 }
102 174
103 175
104 TEST_P(JSSharedOperatorTest, NumberOfInputsAndOutputs) { 176 TEST_P(JSSharedOperatorTest, NumberOfInputsAndOutputs) {
105 JSOperatorBuilder javascript(zone()); 177 JSOperatorBuilder javascript(zone());
106 const SharedOperator& sop = GetParam(); 178 const SharedOperator& sop = GetParam();
107 const Operator* op = (javascript.*sop.constructor)(); 179 const Operator* op = (javascript.*sop.constructor)();
108 180 testNumberOfInputsAndOutputs(sop, op);
109 const int context_input_count = 1;
110 EXPECT_EQ(sop.value_input_count, op->ValueInputCount());
111 EXPECT_EQ(context_input_count, OperatorProperties::GetContextInputCount(op));
112 EXPECT_EQ(sop.frame_state_input_count,
113 OperatorProperties::GetFrameStateInputCount(op));
114 EXPECT_EQ(sop.effect_input_count, op->EffectInputCount());
115 EXPECT_EQ(sop.control_input_count, op->ControlInputCount());
116 EXPECT_EQ(sop.value_input_count + context_input_count +
117 sop.frame_state_input_count + sop.effect_input_count +
118 sop.control_input_count,
119 OperatorProperties::GetTotalInputCount(op));
120
121 EXPECT_EQ(sop.value_output_count, op->ValueOutputCount());
122 EXPECT_EQ(sop.effect_output_count, op->EffectOutputCount());
123 EXPECT_EQ(sop.control_output_count, op->ControlOutputCount());
124 } 181 }
125 182
126 183
127 TEST_P(JSSharedOperatorTest, OpcodeIsCorrect) { 184 TEST_P(JSSharedOperatorTest, OpcodeIsCorrect) {
128 JSOperatorBuilder javascript(zone()); 185 JSOperatorBuilder javascript(zone());
129 const SharedOperator& sop = GetParam(); 186 const SharedOperator& sop = GetParam();
130 const Operator* op = (javascript.*sop.constructor)(); 187 const Operator* op = (javascript.*sop.constructor)();
131 EXPECT_EQ(sop.opcode, op->opcode()); 188 EXPECT_EQ(sop.opcode, op->opcode());
132 } 189 }
133 190
134 191
135 TEST_P(JSSharedOperatorTest, Properties) { 192 TEST_P(JSSharedOperatorTest, Properties) {
136 JSOperatorBuilder javascript(zone()); 193 JSOperatorBuilder javascript(zone());
137 const SharedOperator& sop = GetParam(); 194 const SharedOperator& sop = GetParam();
138 const Operator* op = (javascript.*sop.constructor)(); 195 const Operator* op = (javascript.*sop.constructor)();
139 EXPECT_EQ(sop.properties, op->properties()); 196 EXPECT_EQ(sop.properties, op->properties());
140 } 197 }
141 198
142 199
143 INSTANTIATE_TEST_CASE_P(JSOperatorTest, JSSharedOperatorTest, 200 INSTANTIATE_TEST_CASE_P(JSOperatorTest, JSSharedOperatorTest,
144 ::testing::ValuesIn(kSharedOperators)); 201 ::testing::ValuesIn(kSharedOperators));
145 202
203 // -----------------------------------------------------------------------------
204 // Shared operators which behave differently in strong mode
205
206
207 class JSSharedOperatorWithStrongTest
208 : public TestWithZone,
209 public ::testing::WithParamInterface<SharedOperatorWithLanguageMode> {};
210
211
212 TEST_P(JSSharedOperatorWithStrongTest, InstancesAreGloballyShared) {
213 const SharedOperatorWithLanguageMode& sop = GetParam();
214 JSOperatorBuilder javascript1(zone());
215 JSOperatorBuilder javascript2(zone());
216 EXPECT_EQ((javascript1.*sop.constructor)(LanguageMode::SLOPPY),
217 (javascript2.*sop.constructor)(LanguageMode::SLOPPY));
218 EXPECT_EQ((javascript1.*sop.constructor)(LanguageMode::STRONG),
219 (javascript2.*sop.constructor)(LanguageMode::STRONG));
220 }
221
222
223 TEST_P(JSSharedOperatorWithStrongTest, NumberOfInputsAndOutputs) {
224 JSOperatorBuilder javascript(zone());
225 const SharedOperatorWithLanguageMode& sop = GetParam();
226 const Operator* op_sloppy = (javascript.*sop.constructor)
227 (LanguageMode::SLOPPY);
228 testNumberOfInputsAndOutputs(sop, op_sloppy);
229 const Operator* op_strong = (javascript.*sop.constructor)
230 (LanguageMode::STRONG);
231 testNumberOfInputsAndOutputs(sop, op_strong);
232 }
233
234
235 TEST_P(JSSharedOperatorWithStrongTest, OpcodeIsCorrect) {
236 JSOperatorBuilder javascript(zone());
237 const SharedOperatorWithLanguageMode& sop = GetParam();
238 const Operator* op_sloppy = (javascript.*sop.constructor)
239 (LanguageMode::SLOPPY);
240 EXPECT_EQ(sop.opcode, op_sloppy->opcode());
241 const Operator* op_strong = (javascript.*sop.constructor)
242 (LanguageMode::STRONG);
243 EXPECT_EQ(sop.opcode, op_strong->opcode());
244 }
245
246
247 TEST_P(JSSharedOperatorWithStrongTest, Properties) {
248 JSOperatorBuilder javascript(zone());
249 const SharedOperatorWithLanguageMode& sop = GetParam();
250 const Operator* op_sloppy = (javascript.*sop.constructor)
251 (LanguageMode::SLOPPY);
252 EXPECT_EQ(sop.properties, op_sloppy->properties());
253 const Operator* op_strong = (javascript.*sop.constructor)
254 (LanguageMode::STRONG);
255 EXPECT_EQ(sop.properties, op_strong->properties());
256 }
257
258
259 INSTANTIATE_TEST_CASE_P(JSOperatorTest, JSSharedOperatorWithStrongTest,
260 ::testing::ValuesIn(kSharedOperatorsWithlanguageMode));
146 261
147 // ----------------------------------------------------------------------------- 262 // -----------------------------------------------------------------------------
148 // JSStoreProperty. 263 // JSStoreProperty.
149 264
150 265
151 class JSStorePropertyOperatorTest 266 class JSStorePropertyOperatorTest
152 : public TestWithZone, 267 : public TestWithZone,
153 public ::testing::WithParamInterface<LanguageMode> {}; 268 public ::testing::WithParamInterface<LanguageMode> {};
154 269
155 270
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 EXPECT_EQ(Operator::kNoProperties, op->properties()); 317 EXPECT_EQ(Operator::kNoProperties, op->properties());
203 } 318 }
204 319
205 320
206 INSTANTIATE_TEST_CASE_P(JSOperatorTest, JSStorePropertyOperatorTest, 321 INSTANTIATE_TEST_CASE_P(JSOperatorTest, JSStorePropertyOperatorTest,
207 ::testing::Values(SLOPPY, STRICT)); 322 ::testing::Values(SLOPPY, STRICT));
208 323
209 } // namespace compiler 324 } // namespace compiler
210 } // namespace internal 325 } // namespace internal
211 } // namespace v8 326 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698