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

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: rebase :( 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)(),
rossberg 2015/04/23 13:29:37 Are you ever using this constructor?
conradw 2015/04/23 14:51:55 Yes, my C++ is rusty about what exactly is going o
rossberg 2015/04/23 15:36:58 I think it would be much simpler to just parameter
conradw 2015/04/23 16:08:02 Done. It's beautiful.
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) : OperatorParams({opcode, properties,
39 value_input_count,
40 frame_state_input_count,
41 effect_input_count, control_input_count,
rossberg 2015/04/23 13:29:37 Nit: weird indentation
conradw 2015/04/23 14:51:55 Made a stab at it.
42 value_output_count, effect_output_count,
43 control_output_count}),
44 constructor(cached) {}
45 };
38 46
39 47
40 const SharedOperator kSharedOperators[] = { 48 const SharedOperator kSharedOperators[] = {
41 #define SHARED(Name, properties, value_input_count, frame_state_input_count, \ 49 #define SHARED(Name, properties, value_input_count, frame_state_input_count, \
42 effect_input_count, control_input_count, value_output_count, \ 50 effect_input_count, control_input_count, value_output_count, \
43 effect_output_count, control_output_count) \ 51 effect_output_count, control_output_count) \
44 { \ 52 { \
45 &JSOperatorBuilder::Name, IrOpcode::kJS##Name, properties, \ 53 &JSOperatorBuilder::Name, IrOpcode::kJS##Name, properties, \
46 value_input_count, frame_state_input_count, effect_input_count, \ 54 value_input_count, frame_state_input_count, effect_input_count, \
47 control_input_count, value_output_count, effect_output_count, \ 55 control_input_count, value_output_count, effect_output_count, \
48 control_output_count \ 56 control_output_count \
49 } 57 }
50 SHARED(Equal, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2), 58 SHARED(Equal, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
51 SHARED(NotEqual, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2), 59 SHARED(NotEqual, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
52 SHARED(StrictEqual, Operator::kPure, 2, 0, 0, 0, 1, 0, 0), 60 SHARED(StrictEqual, Operator::kPure, 2, 0, 0, 0, 1, 0, 0),
53 SHARED(StrictNotEqual, Operator::kPure, 2, 0, 0, 0, 1, 0, 0), 61 SHARED(StrictNotEqual, Operator::kPure, 2, 0, 0, 0, 1, 0, 0),
62 SHARED(ToBoolean, Operator::kPure, 1, 0, 0, 0, 1, 0, 0),
63 SHARED(ToNumber, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2),
64 SHARED(ToString, Operator::kNoProperties, 1, 0, 1, 1, 1, 1, 2),
65 SHARED(ToName, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2),
66 SHARED(ToObject, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2),
67 SHARED(Yield, Operator::kNoProperties, 1, 0, 1, 1, 1, 1, 2),
68 SHARED(Create, Operator::kEliminatable, 0, 0, 1, 0, 1, 1, 0),
69 SHARED(HasProperty, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
70 SHARED(TypeOf, Operator::kPure, 1, 0, 0, 0, 1, 0, 0),
71 SHARED(InstanceOf, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
72 SHARED(CreateFunctionContext, Operator::kNoProperties, 1, 0, 1, 1, 1, 1, 2),
73 SHARED(CreateWithContext, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
74 SHARED(CreateBlockContext, Operator::kNoProperties, 2, 0, 1, 1, 1, 1, 2),
75 SHARED(CreateModuleContext, Operator::kNoProperties, 2, 0, 1, 1, 1, 1, 2),
76 SHARED(CreateScriptContext, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2)
77 #undef SHARED
78 };
79
80
81 struct SharedOperatorWithLanguageMode : OperatorParams {
82 const Operator* (JSOperatorBuilder::*constructor)(LanguageMode language_mode);
83 SharedOperatorWithLanguageMode(const Operator* (JSOperatorBuilder::*cached)
rossberg 2015/04/23 13:29:37 Or this one?
conradw 2015/04/23 14:51:55 Ditto for kSharedOperatorsWithlanguageMode[].
84 (LanguageMode language_mode),
85 IrOpcode::Value opcode, Operator::Properties properties,
86 int value_input_count, int frame_state_input_count,
87 int effect_input_count, int control_input_count,
88 int value_output_count, int effect_output_count,
89 int control_output_count) : OperatorParams({opcode, properties,
90 value_input_count,
91 frame_state_input_count,
92 effect_input_count, control_input_count,
93 value_output_count, effect_output_count,
94 control_output_count}),
95 constructor(cached) {}
96 };
97
98
99 const SharedOperatorWithLanguageMode kSharedOperatorsWithlanguageMode[] = {
100 #define SHARED(Name, properties, value_input_count, frame_state_input_count, \
101 effect_input_count, control_input_count, value_output_count, \
102 effect_output_count, control_output_count) \
103 { \
104 &JSOperatorBuilder::Name, IrOpcode::kJS##Name, properties, \
105 value_input_count, frame_state_input_count, effect_input_count, \
106 control_input_count, value_output_count, effect_output_count, \
107 control_output_count \
108 }
54 SHARED(LessThan, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2), 109 SHARED(LessThan, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
55 SHARED(GreaterThan, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2), 110 SHARED(GreaterThan, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
56 SHARED(LessThanOrEqual, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2), 111 SHARED(LessThanOrEqual, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
57 SHARED(GreaterThanOrEqual, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2), 112 SHARED(GreaterThanOrEqual, Operator::kNoProperties, 2, 1, 1, 1, 1, 1, 2),
58 SHARED(BitwiseOr, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 113 SHARED(BitwiseOr, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
59 SHARED(BitwiseXor, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 114 SHARED(BitwiseXor, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
60 SHARED(BitwiseAnd, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 115 SHARED(BitwiseAnd, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
61 SHARED(ShiftLeft, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 116 SHARED(ShiftLeft, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
62 SHARED(ShiftRight, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 117 SHARED(ShiftRight, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
63 SHARED(ShiftRightLogical, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 118 SHARED(ShiftRightLogical, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
64 SHARED(Add, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 119 SHARED(Add, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
65 SHARED(Subtract, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 120 SHARED(Subtract, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
66 SHARED(Multiply, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 121 SHARED(Multiply, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
67 SHARED(Divide, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 122 SHARED(Divide, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
68 SHARED(Modulus, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2), 123 SHARED(Modulus, Operator::kNoProperties, 2, 2, 1, 1, 1, 1, 2),
69 SHARED(UnaryNot, Operator::kPure, 1, 0, 0, 0, 1, 0, 0), 124 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 125 #undef SHARED
86 }; 126 };
87 127
128
129 void testNumberOfInputsAndOutputs(const OperatorParams& sop,
130 const Operator* op) {
131 const int context_input_count = 1;
132 EXPECT_EQ(sop.value_input_count, op->ValueInputCount());
133 EXPECT_EQ(context_input_count, OperatorProperties::GetContextInputCount(op));
134 EXPECT_EQ(sop.frame_state_input_count,
135 OperatorProperties::GetFrameStateInputCount(op));
136 EXPECT_EQ(sop.effect_input_count, op->EffectInputCount());
137 EXPECT_EQ(sop.control_input_count, op->ControlInputCount());
138 EXPECT_EQ(sop.value_input_count + context_input_count +
139 sop.frame_state_input_count + sop.effect_input_count +
140 sop.control_input_count,
141 OperatorProperties::GetTotalInputCount(op));
142
143 EXPECT_EQ(sop.value_output_count, op->ValueOutputCount());
144 EXPECT_EQ(sop.effect_output_count, op->EffectOutputCount());
145 EXPECT_EQ(sop.control_output_count, op->ControlOutputCount());
146 }
147
148
149 std::ostream& operator<<(std::ostream& os, const SharedOperator& sop) {
150 return os << IrOpcode::Mnemonic(sop.opcode);
151 }
152
153 std::ostream& operator<<(std::ostream& os,
154 const SharedOperatorWithLanguageMode& sop) {
155 return os << IrOpcode::Mnemonic(sop.opcode);
156 }
157
88 } // namespace 158 } // namespace
89 159
90 160
161 // -----------------------------------------------------------------------------
162 // Shared operators.
163
164
91 class JSSharedOperatorTest 165 class JSSharedOperatorTest
92 : public TestWithZone, 166 : public TestWithZone,
93 public ::testing::WithParamInterface<SharedOperator> {}; 167 public ::testing::WithParamInterface<SharedOperator> {};
94 168
95 169
96 TEST_P(JSSharedOperatorTest, InstancesAreGloballyShared) { 170 TEST_P(JSSharedOperatorTest, InstancesAreGloballyShared) {
97 const SharedOperator& sop = GetParam(); 171 const SharedOperator& sop = GetParam();
98 JSOperatorBuilder javascript1(zone()); 172 JSOperatorBuilder javascript1(zone());
99 JSOperatorBuilder javascript2(zone()); 173 JSOperatorBuilder javascript2(zone());
100 EXPECT_EQ((javascript1.*sop.constructor)(), (javascript2.*sop.constructor)()); 174 EXPECT_EQ((javascript1.*sop.constructor)(), (javascript2.*sop.constructor)());
101 } 175 }
102 176
103 177
104 TEST_P(JSSharedOperatorTest, NumberOfInputsAndOutputs) { 178 TEST_P(JSSharedOperatorTest, NumberOfInputsAndOutputs) {
105 JSOperatorBuilder javascript(zone()); 179 JSOperatorBuilder javascript(zone());
106 const SharedOperator& sop = GetParam(); 180 const SharedOperator& sop = GetParam();
107 const Operator* op = (javascript.*sop.constructor)(); 181 const Operator* op = (javascript.*sop.constructor)();
108 182 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 } 183 }
125 184
126 185
127 TEST_P(JSSharedOperatorTest, OpcodeIsCorrect) { 186 TEST_P(JSSharedOperatorTest, OpcodeIsCorrect) {
128 JSOperatorBuilder javascript(zone()); 187 JSOperatorBuilder javascript(zone());
129 const SharedOperator& sop = GetParam(); 188 const SharedOperator& sop = GetParam();
130 const Operator* op = (javascript.*sop.constructor)(); 189 const Operator* op = (javascript.*sop.constructor)();
131 EXPECT_EQ(sop.opcode, op->opcode()); 190 EXPECT_EQ(sop.opcode, op->opcode());
132 } 191 }
133 192
134 193
135 TEST_P(JSSharedOperatorTest, Properties) { 194 TEST_P(JSSharedOperatorTest, Properties) {
136 JSOperatorBuilder javascript(zone()); 195 JSOperatorBuilder javascript(zone());
137 const SharedOperator& sop = GetParam(); 196 const SharedOperator& sop = GetParam();
138 const Operator* op = (javascript.*sop.constructor)(); 197 const Operator* op = (javascript.*sop.constructor)();
139 EXPECT_EQ(sop.properties, op->properties()); 198 EXPECT_EQ(sop.properties, op->properties());
140 } 199 }
141 200
142 201
143 INSTANTIATE_TEST_CASE_P(JSOperatorTest, JSSharedOperatorTest, 202 INSTANTIATE_TEST_CASE_P(JSOperatorTest, JSSharedOperatorTest,
144 ::testing::ValuesIn(kSharedOperators)); 203 ::testing::ValuesIn(kSharedOperators));
145 204
205 // -----------------------------------------------------------------------------
206 // Shared operators which behave differently in strong mode
207
208
209 class JSSharedOperatorWithStrongTest
210 : public TestWithZone,
211 public ::testing::WithParamInterface<SharedOperatorWithLanguageMode> {};
212
213
214 TEST_P(JSSharedOperatorWithStrongTest, InstancesAreGloballyShared) {
215 const SharedOperatorWithLanguageMode& sop = GetParam();
216 JSOperatorBuilder javascript1(zone());
217 JSOperatorBuilder javascript2(zone());
218 EXPECT_EQ((javascript1.*sop.constructor)(LanguageMode::SLOPPY),
219 (javascript2.*sop.constructor)(LanguageMode::SLOPPY));
220 EXPECT_EQ((javascript1.*sop.constructor)(LanguageMode::STRONG),
221 (javascript2.*sop.constructor)(LanguageMode::STRONG));
222 }
223
224
225 TEST_P(JSSharedOperatorWithStrongTest, NumberOfInputsAndOutputs) {
226 JSOperatorBuilder javascript(zone());
227 const SharedOperatorWithLanguageMode& sop = GetParam();
228 const Operator* op_sloppy = (javascript.*sop.constructor)
229 (LanguageMode::SLOPPY);
230 testNumberOfInputsAndOutputs(sop, op_sloppy);
231 const Operator* op_strong = (javascript.*sop.constructor)
232 (LanguageMode::STRONG);
233 testNumberOfInputsAndOutputs(sop, op_strong);
234 }
235
236
237 TEST_P(JSSharedOperatorWithStrongTest, OpcodeIsCorrect) {
238 JSOperatorBuilder javascript(zone());
239 const SharedOperatorWithLanguageMode& sop = GetParam();
240 const Operator* op_sloppy = (javascript.*sop.constructor)
241 (LanguageMode::SLOPPY);
242 EXPECT_EQ(sop.opcode, op_sloppy->opcode());
243 const Operator* op_strong = (javascript.*sop.constructor)
244 (LanguageMode::STRONG);
245 EXPECT_EQ(sop.opcode, op_strong->opcode());
246 }
247
248
249 TEST_P(JSSharedOperatorWithStrongTest, Properties) {
250 JSOperatorBuilder javascript(zone());
251 const SharedOperatorWithLanguageMode& sop = GetParam();
252 const Operator* op_sloppy = (javascript.*sop.constructor)
253 (LanguageMode::SLOPPY);
254 EXPECT_EQ(sop.properties, op_sloppy->properties());
255 const Operator* op_strong = (javascript.*sop.constructor)
256 (LanguageMode::STRONG);
257 EXPECT_EQ(sop.properties, op_strong->properties());
258 }
259
260
261 INSTANTIATE_TEST_CASE_P(JSOperatorTest, JSSharedOperatorWithStrongTest,
262 ::testing::ValuesIn(kSharedOperatorsWithlanguageMode));
146 263
147 // ----------------------------------------------------------------------------- 264 // -----------------------------------------------------------------------------
148 // JSStoreProperty. 265 // JSStoreProperty.
149 266
150 267
151 class JSStorePropertyOperatorTest 268 class JSStorePropertyOperatorTest
152 : public TestWithZone, 269 : public TestWithZone,
153 public ::testing::WithParamInterface<LanguageMode> {}; 270 public ::testing::WithParamInterface<LanguageMode> {};
154 271
155 272
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 EXPECT_EQ(Operator::kNoProperties, op->properties()); 319 EXPECT_EQ(Operator::kNoProperties, op->properties());
203 } 320 }
204 321
205 322
206 INSTANTIATE_TEST_CASE_P(JSOperatorTest, JSStorePropertyOperatorTest, 323 INSTANTIATE_TEST_CASE_P(JSOperatorTest, JSStorePropertyOperatorTest,
207 ::testing::Values(SLOPPY, STRICT)); 324 ::testing::Values(SLOPPY, STRICT));
208 325
209 } // namespace compiler 326 } // namespace compiler
210 } // namespace internal 327 } // namespace internal
211 } // namespace v8 328 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698