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
3 // found in the LICENSE file.
4 
5 #ifndef V8_CCTEST_COMPILER_VALUE_HELPER_H_
6 #define V8_CCTEST_COMPILER_VALUE_HELPER_H_
7 
8 #include "src/v8.h"
9 
10 #include "src/compiler/common-operator.h"
11 #include "src/compiler/node.h"
12 #include "src/compiler/node-matchers.h"
13 #include "src/isolate.h"
14 #include "src/objects.h"
15 #include "test/cctest/cctest.h"
16 
17 namespace v8 {
18 namespace internal {
19 namespace compiler {
20 
21 // A collection of utilities related to numerical and heap values, including
22 // example input values of various types, including int32_t, uint32_t, double,
23 // etc.
24 class ValueHelper {
25  public:
26   Isolate* isolate_;
27 
ValueHelper()28   ValueHelper() : isolate_(CcTest::InitIsolateOnce()) {}
29 
CheckFloat64Constant(double expected,Node * node)30   void CheckFloat64Constant(double expected, Node* node) {
31     CHECK_EQ(IrOpcode::kFloat64Constant, node->opcode());
32     CHECK_EQ(expected, OpParameter<double>(node));
33   }
34 
CheckNumberConstant(double expected,Node * node)35   void CheckNumberConstant(double expected, Node* node) {
36     CHECK_EQ(IrOpcode::kNumberConstant, node->opcode());
37     CHECK_EQ(expected, OpParameter<double>(node));
38   }
39 
CheckInt32Constant(int32_t expected,Node * node)40   void CheckInt32Constant(int32_t expected, Node* node) {
41     CHECK_EQ(IrOpcode::kInt32Constant, node->opcode());
42     CHECK_EQ(expected, OpParameter<int32_t>(node));
43   }
44 
CheckUint32Constant(int32_t expected,Node * node)45   void CheckUint32Constant(int32_t expected, Node* node) {
46     CHECK_EQ(IrOpcode::kInt32Constant, node->opcode());
47     CHECK_EQ(expected, OpParameter<uint32_t>(node));
48   }
49 
CheckHeapConstant(Object * expected,Node * node)50   void CheckHeapConstant(Object* expected, Node* node) {
51     CHECK_EQ(IrOpcode::kHeapConstant, node->opcode());
52     CHECK_EQ(expected, *OpParameter<Unique<Object> >(node).handle());
53   }
54 
CheckTrue(Node * node)55   void CheckTrue(Node* node) {
56     CheckHeapConstant(isolate_->heap()->true_value(), node);
57   }
58 
CheckFalse(Node * node)59   void CheckFalse(Node* node) {
60     CheckHeapConstant(isolate_->heap()->false_value(), node);
61   }
62 
float64_vector()63   static std::vector<double> float64_vector() {
64     static const double nan = v8::base::OS::nan_value();
65     static const double values[] = {
66         0.125,           0.25,            0.375,          0.5,
67         1.25,            -1.75,           2,              5.125,
68         6.25,            0.0,             -0.0,           982983.25,
69         888,             2147483647.0,    -999.75,        3.1e7,
70         -2e66,           3e-88,           -2147483648.0,  V8_INFINITY,
71         -V8_INFINITY,    nan,             2147483647.375, 2147483647.75,
72         2147483648.0,    2147483648.25,   2147483649.25,  -2147483647.0,
73         -2147483647.125, -2147483647.875, -2147483648.25, -2147483649.5};
74     return std::vector<double>(&values[0], &values[arraysize(values)]);
75   }
76 
int32_vector()77   static const std::vector<int32_t> int32_vector() {
78     std::vector<uint32_t> values = uint32_vector();
79     return std::vector<int32_t>(values.begin(), values.end());
80   }
81 
uint32_vector()82   static const std::vector<uint32_t> uint32_vector() {
83     static const uint32_t kValues[] = {
84         0x00000000, 0x00000001, 0xffffffff, 0x1b09788b, 0x04c5fce8, 0xcc0de5bf,
85         0x273a798e, 0x187937a3, 0xece3af83, 0x5495a16b, 0x0b668ecc, 0x11223344,
86         0x0000009e, 0x00000043, 0x0000af73, 0x0000116b, 0x00658ecc, 0x002b3b4c,
87         0x88776655, 0x70000000, 0x07200000, 0x7fffffff, 0x56123761, 0x7fffff00,
88         0x761c4761, 0x80000000, 0x88888888, 0xa0000000, 0xdddddddd, 0xe0000000,
89         0xeeeeeeee, 0xfffffffd, 0xf0000000, 0x007fffff, 0x003fffff, 0x001fffff,
90         0x000fffff, 0x0007ffff, 0x0003ffff, 0x0001ffff, 0x0000ffff, 0x00007fff,
91         0x00003fff, 0x00001fff, 0x00000fff, 0x000007ff, 0x000003ff, 0x000001ff};
92     return std::vector<uint32_t>(&kValues[0], &kValues[arraysize(kValues)]);
93   }
94 
95   static const std::vector<double> nan_vector(size_t limit = 0) {
96     static const double nan = v8::base::OS::nan_value();
97     static const double values[] = {-nan,               -V8_INFINITY * -0.0,
98                                     -V8_INFINITY * 0.0, V8_INFINITY * -0.0,
99                                     V8_INFINITY * 0.0,  nan};
100     return std::vector<double>(&values[0], &values[arraysize(values)]);
101   }
102 
ror_vector()103   static const std::vector<uint32_t> ror_vector() {
104     static const uint32_t kValues[31] = {
105         1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16,
106         17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
107     return std::vector<uint32_t>(&kValues[0], &kValues[arraysize(kValues)]);
108   }
109 };
110 
111 // Helper macros that can be used in FOR_INT32_INPUTS(i) { ... *i ... }
112 // Watch out, these macros aren't hygenic; they pollute your scope. Thanks STL.
113 #define FOR_INPUTS(ctype, itype, var)                           \
114   std::vector<ctype> var##_vec = ValueHelper::itype##_vector(); \
115   for (std::vector<ctype>::iterator var = var##_vec.begin();    \
116        var != var##_vec.end(); ++var)
117 
118 #define FOR_INT32_INPUTS(var) FOR_INPUTS(int32_t, int32, var)
119 #define FOR_UINT32_INPUTS(var) FOR_INPUTS(uint32_t, uint32, var)
120 #define FOR_FLOAT64_INPUTS(var) FOR_INPUTS(double, float64, var)
121 
122 #define FOR_INT32_SHIFTS(var) for (int32_t var = 0; var < 32; var++)
123 
124 #define FOR_UINT32_SHIFTS(var) for (uint32_t var = 0; var < 32; var++)
125 
126 }  // namespace compiler
127 }  // namespace internal
128 }  // namespace v8
129 
130 #endif  // V8_CCTEST_COMPILER_VALUE_HELPER_H_
131