OLD | NEW |
(Empty) | |
| 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_CODEGEN_TESTER_H_ |
| 6 #define V8_CCTEST_COMPILER_CODEGEN_TESTER_H_ |
| 7 |
| 8 #include "src/v8.h" |
| 9 |
| 10 #include "src/compiler/pipeline.h" |
| 11 #include "src/compiler/raw-machine-assembler.h" |
| 12 #include "src/compiler/structured-machine-assembler.h" |
| 13 #include "src/simulator.h" |
| 14 #include "test/cctest/compiler/call-tester.h" |
| 15 |
| 16 namespace v8 { |
| 17 namespace internal { |
| 18 namespace compiler { |
| 19 |
| 20 template <typename MachineAssembler> |
| 21 class MachineAssemblerTester : public HandleAndZoneScope, |
| 22 public CallHelper, |
| 23 public MachineAssembler { |
| 24 public: |
| 25 MachineAssemblerTester(MachineRepresentation return_type, |
| 26 MachineRepresentation p0, MachineRepresentation p1, |
| 27 MachineRepresentation p2, MachineRepresentation p3, |
| 28 MachineRepresentation p4) |
| 29 : HandleAndZoneScope(), |
| 30 CallHelper(main_isolate()), |
| 31 MachineAssembler(new (main_zone()) Graph(main_zone()), |
| 32 ToCallDescriptorBuilder(main_zone(), return_type, p0, |
| 33 p1, p2, p3, p4), |
| 34 MachineOperatorBuilder::pointer_rep()) {} |
| 35 |
| 36 Node* LoadFromPointer(void* address, MachineRepresentation rep, |
| 37 int32_t offset = 0) { |
| 38 return this->Load(rep, this->PointerConstant(address), |
| 39 this->Int32Constant(offset)); |
| 40 } |
| 41 |
| 42 void StoreToPointer(void* address, MachineRepresentation rep, Node* node) { |
| 43 this->Store(rep, this->PointerConstant(address), node); |
| 44 } |
| 45 |
| 46 Node* StringConstant(const char* string) { |
| 47 return this->HeapConstant( |
| 48 this->isolate()->factory()->InternalizeUtf8String(string)); |
| 49 } |
| 50 |
| 51 void CheckNumber(double expected, Object* number) { |
| 52 CHECK(this->isolate()->factory()->NewNumber(expected)->SameValue(number)); |
| 53 } |
| 54 |
| 55 void CheckString(const char* expected, Object* string) { |
| 56 CHECK( |
| 57 this->isolate()->factory()->InternalizeUtf8String(expected)->SameValue( |
| 58 string)); |
| 59 } |
| 60 |
| 61 void GenerateCode() { Generate(); } |
| 62 |
| 63 protected: |
| 64 virtual void VerifyParameters(int parameter_count, |
| 65 MachineRepresentation* parameter_types) { |
| 66 CHECK_EQ(this->parameter_count(), parameter_count); |
| 67 const MachineRepresentation* expected_types = this->parameter_types(); |
| 68 for (int i = 0; i < parameter_count; i++) { |
| 69 CHECK_EQ(expected_types[i], parameter_types[i]); |
| 70 } |
| 71 } |
| 72 |
| 73 virtual byte* Generate() { |
| 74 if (code_.is_null()) { |
| 75 Schedule* schedule = this->Export(); |
| 76 CallDescriptor* call_descriptor = this->call_descriptor(); |
| 77 Graph* graph = this->graph(); |
| 78 CompilationInfo info(graph->zone()->isolate(), graph->zone()); |
| 79 Linkage linkage(&info, call_descriptor); |
| 80 Pipeline pipeline(&info); |
| 81 code_ = pipeline.GenerateCodeForMachineGraph(&linkage, graph, schedule); |
| 82 } |
| 83 return this->code_.ToHandleChecked()->entry(); |
| 84 } |
| 85 |
| 86 private: |
| 87 MaybeHandle<Code> code_; |
| 88 }; |
| 89 |
| 90 |
| 91 template <typename ReturnType> |
| 92 class RawMachineAssemblerTester |
| 93 : public MachineAssemblerTester<RawMachineAssembler>, |
| 94 public CallHelper2<ReturnType, RawMachineAssemblerTester<ReturnType> > { |
| 95 public: |
| 96 RawMachineAssemblerTester(MachineRepresentation p0 = kMachineLast, |
| 97 MachineRepresentation p1 = kMachineLast, |
| 98 MachineRepresentation p2 = kMachineLast, |
| 99 MachineRepresentation p3 = kMachineLast, |
| 100 MachineRepresentation p4 = kMachineLast) |
| 101 : MachineAssemblerTester(ReturnValueTraits<ReturnType>::Representation(), |
| 102 p0, p1, p2, p3, p4) {} |
| 103 }; |
| 104 |
| 105 |
| 106 template <typename ReturnType> |
| 107 class StructuredMachineAssemblerTester |
| 108 : public MachineAssemblerTester<StructuredMachineAssembler>, |
| 109 public CallHelper2<ReturnType, |
| 110 StructuredMachineAssemblerTester<ReturnType> > { |
| 111 public: |
| 112 StructuredMachineAssemblerTester(MachineRepresentation p0 = kMachineLast, |
| 113 MachineRepresentation p1 = kMachineLast, |
| 114 MachineRepresentation p2 = kMachineLast, |
| 115 MachineRepresentation p3 = kMachineLast, |
| 116 MachineRepresentation p4 = kMachineLast) |
| 117 : MachineAssemblerTester(ReturnValueTraits<ReturnType>::Representation(), |
| 118 p0, p1, p2, p3, p4) {} |
| 119 }; |
| 120 |
| 121 |
| 122 static const bool USE_RESULT_BUFFER = true; |
| 123 static const bool USE_RETURN_REGISTER = false; |
| 124 |
| 125 // TODO(titzer): use the C-style calling convention, or any register-based |
| 126 // calling convention for binop tests. |
| 127 template <typename CType, MachineRepresentation rep, bool use_result_buffer> |
| 128 class BinopTester { |
| 129 public: |
| 130 static const int32_t CHECK_VALUE = 0x99BEEDCE; |
| 131 |
| 132 explicit BinopTester(RawMachineAssemblerTester<int32_t>* tester) |
| 133 : T(tester), |
| 134 param0(T->LoadFromPointer(&p0, rep)), |
| 135 param1(T->LoadFromPointer(&p1, rep)), |
| 136 p0(static_cast<CType>(0)), |
| 137 p1(static_cast<CType>(0)), |
| 138 result(static_cast<CType>(0)) {} |
| 139 |
| 140 RawMachineAssemblerTester<int32_t>* T; |
| 141 Node* param0; |
| 142 Node* param1; |
| 143 |
| 144 CType call(CType a0, CType a1) { |
| 145 p0 = a0; |
| 146 p1 = a1; |
| 147 if (use_result_buffer) { |
| 148 CHECK_EQ(CHECK_VALUE, T->Call()); |
| 149 return result; |
| 150 } else { |
| 151 return T->Call(); |
| 152 } |
| 153 } |
| 154 |
| 155 void AddReturn(Node* val) { |
| 156 if (use_result_buffer) { |
| 157 T->Store(rep, T->PointerConstant(&result), T->Int32Constant(0), val); |
| 158 T->Return(T->Int32Constant(CHECK_VALUE)); |
| 159 } else { |
| 160 T->Return(val); |
| 161 } |
| 162 } |
| 163 |
| 164 protected: |
| 165 CType p0; |
| 166 CType p1; |
| 167 CType result; |
| 168 }; |
| 169 |
| 170 |
| 171 // A helper class for testing code sequences that take two int parameters and |
| 172 // return an int value. |
| 173 class Int32BinopTester |
| 174 : public BinopTester<int32_t, kMachineWord32, USE_RETURN_REGISTER> { |
| 175 public: |
| 176 explicit Int32BinopTester(RawMachineAssemblerTester<int32_t>* tester) |
| 177 : BinopTester<int32_t, kMachineWord32, USE_RETURN_REGISTER>(tester) {} |
| 178 |
| 179 int32_t call(uint32_t a0, uint32_t a1) { |
| 180 p0 = static_cast<int32_t>(a0); |
| 181 p1 = static_cast<int32_t>(a1); |
| 182 return T->Call(); |
| 183 } |
| 184 }; |
| 185 |
| 186 |
| 187 // A helper class for testing code sequences that take two double parameters and |
| 188 // return a double value. |
| 189 // TODO(titzer): figure out how to return doubles correctly on ia32. |
| 190 class Float64BinopTester |
| 191 : public BinopTester<double, kMachineFloat64, USE_RESULT_BUFFER> { |
| 192 public: |
| 193 explicit Float64BinopTester(RawMachineAssemblerTester<int32_t>* tester) |
| 194 : BinopTester<double, kMachineFloat64, USE_RESULT_BUFFER>(tester) {} |
| 195 }; |
| 196 |
| 197 |
| 198 // A helper class for testing code sequences that take two pointer parameters |
| 199 // and return a pointer value. |
| 200 // TODO(titzer): pick word size of pointers based on V8_TARGET. |
| 201 template <typename Type> |
| 202 class PointerBinopTester |
| 203 : public BinopTester<Type*, kMachineWord32, USE_RETURN_REGISTER> { |
| 204 public: |
| 205 explicit PointerBinopTester(RawMachineAssemblerTester<int32_t>* tester) |
| 206 : BinopTester<Type*, kMachineWord32, USE_RETURN_REGISTER>(tester) {} |
| 207 }; |
| 208 |
| 209 |
| 210 // A helper class for testing code sequences that take two tagged parameters and |
| 211 // return a tagged value. |
| 212 template <typename Type> |
| 213 class TaggedBinopTester |
| 214 : public BinopTester<Type*, kMachineTagged, USE_RETURN_REGISTER> { |
| 215 public: |
| 216 explicit TaggedBinopTester(RawMachineAssemblerTester<int32_t>* tester) |
| 217 : BinopTester<Type*, kMachineTagged, USE_RETURN_REGISTER>(tester) {} |
| 218 }; |
| 219 |
| 220 // A helper class for testing compares. Wraps a machine opcode and provides |
| 221 // evaluation routines and the operators. |
| 222 class CompareWrapper { |
| 223 public: |
| 224 explicit CompareWrapper(IrOpcode::Value op) : opcode(op) {} |
| 225 |
| 226 Node* MakeNode(RawMachineAssemblerTester<int32_t>* m, Node* a, Node* b) { |
| 227 return m->NewNode(op(m->machine()), a, b); |
| 228 } |
| 229 |
| 230 Operator* op(MachineOperatorBuilder* machine) { |
| 231 switch (opcode) { |
| 232 case IrOpcode::kWord32Equal: |
| 233 return machine->Word32Equal(); |
| 234 case IrOpcode::kInt32LessThan: |
| 235 return machine->Int32LessThan(); |
| 236 case IrOpcode::kInt32LessThanOrEqual: |
| 237 return machine->Int32LessThanOrEqual(); |
| 238 case IrOpcode::kUint32LessThan: |
| 239 return machine->Uint32LessThan(); |
| 240 case IrOpcode::kUint32LessThanOrEqual: |
| 241 return machine->Uint32LessThanOrEqual(); |
| 242 case IrOpcode::kFloat64Equal: |
| 243 return machine->Float64Equal(); |
| 244 case IrOpcode::kFloat64LessThan: |
| 245 return machine->Float64LessThan(); |
| 246 case IrOpcode::kFloat64LessThanOrEqual: |
| 247 return machine->Float64LessThanOrEqual(); |
| 248 default: |
| 249 UNREACHABLE(); |
| 250 } |
| 251 return NULL; |
| 252 } |
| 253 |
| 254 bool Int32Compare(int32_t a, int32_t b) { |
| 255 switch (opcode) { |
| 256 case IrOpcode::kWord32Equal: |
| 257 return a == b; |
| 258 case IrOpcode::kInt32LessThan: |
| 259 return a < b; |
| 260 case IrOpcode::kInt32LessThanOrEqual: |
| 261 return a <= b; |
| 262 case IrOpcode::kUint32LessThan: |
| 263 return static_cast<uint32_t>(a) < static_cast<uint32_t>(b); |
| 264 case IrOpcode::kUint32LessThanOrEqual: |
| 265 return static_cast<uint32_t>(a) <= static_cast<uint32_t>(b); |
| 266 default: |
| 267 UNREACHABLE(); |
| 268 } |
| 269 return false; |
| 270 } |
| 271 |
| 272 bool Float64Compare(double a, double b) { |
| 273 switch (opcode) { |
| 274 case IrOpcode::kFloat64Equal: |
| 275 return a == b; |
| 276 case IrOpcode::kFloat64LessThan: |
| 277 return a < b; |
| 278 case IrOpcode::kFloat64LessThanOrEqual: |
| 279 return a <= b; |
| 280 default: |
| 281 UNREACHABLE(); |
| 282 } |
| 283 return false; |
| 284 } |
| 285 |
| 286 IrOpcode::Value opcode; |
| 287 }; |
| 288 |
| 289 |
| 290 // A small closure class to generate code for a function of two inputs that |
| 291 // produces a single output so that it can be used in many different contexts. |
| 292 // The {expected()} method should compute the expected output for a given |
| 293 // pair of inputs. |
| 294 template <typename T> |
| 295 class BinopGen { |
| 296 public: |
| 297 virtual void gen(RawMachineAssemblerTester<int32_t>* m, Node* a, Node* b) = 0; |
| 298 virtual T expected(T a, T b) = 0; |
| 299 virtual ~BinopGen() {} |
| 300 }; |
| 301 |
| 302 // A helper class to generate various combination of input shape combinations |
| 303 // and run the generated code to ensure it produces the correct results. |
| 304 class Int32BinopInputShapeTester { |
| 305 public: |
| 306 explicit Int32BinopInputShapeTester(BinopGen<int32_t>* g) : gen(g) {} |
| 307 |
| 308 void TestAllInputShapes(); |
| 309 |
| 310 private: |
| 311 BinopGen<int32_t>* gen; |
| 312 int32_t input_a; |
| 313 int32_t input_b; |
| 314 |
| 315 void Run(RawMachineAssemblerTester<int32_t>* m); |
| 316 void RunLeft(RawMachineAssemblerTester<int32_t>* m); |
| 317 void RunRight(RawMachineAssemblerTester<int32_t>* m); |
| 318 }; |
| 319 } // namespace compiler |
| 320 } // namespace internal |
| 321 } // namespace v8 |
| 322 |
| 323 #endif // V8_CCTEST_COMPILER_CODEGEN_TESTER_H_ |
OLD | NEW |