blob: d5f30ef5542a6b505ee5b01b53eaeb2f0096004c [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// 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
3// found in the LICENSE file.
4
5#include "src/compiler/js-operator.h"
6#include "src/compiler/opcodes.h"
7#include "src/compiler/operator.h"
8#include "src/compiler/operator-properties.h"
9#include "test/unittests/test-utils.h"
10
11namespace v8 {
12namespace internal {
13namespace compiler {
14
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015// -----------------------------------------------------------------------------
Ben Murdoch097c5b22016-05-18 11:27:45 +010016// Shared operators.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040017
18namespace {
19
20struct SharedOperator {
21 const Operator* (JSOperatorBuilder::*constructor)();
22 IrOpcode::Value opcode;
23 Operator::Properties properties;
24 int value_input_count;
25 int frame_state_input_count;
26 int effect_input_count;
27 int control_input_count;
28 int value_output_count;
29 int effect_output_count;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000030 int control_output_count;
31};
32
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033const SharedOperator kSharedOperators[] = {
34#define SHARED(Name, properties, value_input_count, frame_state_input_count, \
35 effect_input_count, control_input_count, value_output_count, \
36 effect_output_count, control_output_count) \
37 { \
38 &JSOperatorBuilder::Name, IrOpcode::kJS##Name, properties, \
39 value_input_count, frame_state_input_count, effect_input_count, \
40 control_input_count, value_output_count, effect_output_count, \
41 control_output_count \
42 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043 SHARED(ToNumber, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2),
44 SHARED(ToString, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2),
45 SHARED(ToName, Operator::kNoProperties, 1, 1, 1, 1, 1, 1, 2),
Ben Murdochc5610432016-08-08 18:44:38 +010046 SHARED(ToObject, Operator::kFoldable, 1, 1, 1, 1, 1, 1, 2),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047 SHARED(Create, Operator::kEliminatable, 2, 1, 1, 0, 1, 1, 0),
Ben Murdochc5610432016-08-08 18:44:38 +010048 SHARED(TypeOf, Operator::kPure, 1, 0, 0, 0, 1, 0, 0),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000049 SHARED(CreateWithContext, Operator::kNoProperties, 2, 0, 1, 1, 1, 1, 2),
50 SHARED(CreateModuleContext, Operator::kNoProperties, 2, 0, 1, 1, 1, 1, 2),
51#undef SHARED
Emily Bernierd0a1eb72015-03-24 16:35:39 -040052};
53
54
55std::ostream& operator<<(std::ostream& os, const SharedOperator& sop) {
56 return os << IrOpcode::Mnemonic(sop.opcode);
57}
58
Emily Bernierd0a1eb72015-03-24 16:35:39 -040059} // namespace
60
61
62class JSSharedOperatorTest
63 : public TestWithZone,
64 public ::testing::WithParamInterface<SharedOperator> {};
65
66
67TEST_P(JSSharedOperatorTest, InstancesAreGloballyShared) {
68 const SharedOperator& sop = GetParam();
69 JSOperatorBuilder javascript1(zone());
70 JSOperatorBuilder javascript2(zone());
71 EXPECT_EQ((javascript1.*sop.constructor)(), (javascript2.*sop.constructor)());
72}
73
74
75TEST_P(JSSharedOperatorTest, NumberOfInputsAndOutputs) {
76 JSOperatorBuilder javascript(zone());
77 const SharedOperator& sop = GetParam();
78 const Operator* op = (javascript.*sop.constructor)();
79
80 const int context_input_count = 1;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040081 EXPECT_EQ(sop.value_input_count, op->ValueInputCount());
82 EXPECT_EQ(context_input_count, OperatorProperties::GetContextInputCount(op));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000083 EXPECT_EQ(sop.frame_state_input_count,
Emily Bernierd0a1eb72015-03-24 16:35:39 -040084 OperatorProperties::GetFrameStateInputCount(op));
85 EXPECT_EQ(sop.effect_input_count, op->EffectInputCount());
86 EXPECT_EQ(sop.control_input_count, op->ControlInputCount());
87 EXPECT_EQ(sop.value_input_count + context_input_count +
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000088 sop.frame_state_input_count + sop.effect_input_count +
Emily Bernierd0a1eb72015-03-24 16:35:39 -040089 sop.control_input_count,
90 OperatorProperties::GetTotalInputCount(op));
91
92 EXPECT_EQ(sop.value_output_count, op->ValueOutputCount());
93 EXPECT_EQ(sop.effect_output_count, op->EffectOutputCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000094 EXPECT_EQ(sop.control_output_count, op->ControlOutputCount());
Emily Bernierd0a1eb72015-03-24 16:35:39 -040095}
96
97
98TEST_P(JSSharedOperatorTest, OpcodeIsCorrect) {
99 JSOperatorBuilder javascript(zone());
100 const SharedOperator& sop = GetParam();
101 const Operator* op = (javascript.*sop.constructor)();
102 EXPECT_EQ(sop.opcode, op->opcode());
103}
104
105
106TEST_P(JSSharedOperatorTest, Properties) {
107 JSOperatorBuilder javascript(zone());
108 const SharedOperator& sop = GetParam();
109 const Operator* op = (javascript.*sop.constructor)();
110 EXPECT_EQ(sop.properties, op->properties());
111}
112
113
114INSTANTIATE_TEST_CASE_P(JSOperatorTest, JSSharedOperatorTest,
115 ::testing::ValuesIn(kSharedOperators));
116
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400117} // namespace compiler
118} // namespace internal
119} // namespace v8