| OLD | NEW |
| 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 #ifndef V8_CCTEST_COMPILER_CODEGEN_TESTER_H_ | 5 #ifndef V8_CCTEST_COMPILER_CODEGEN_TESTER_H_ |
| 6 #define V8_CCTEST_COMPILER_CODEGEN_TESTER_H_ | 6 #define V8_CCTEST_COMPILER_CODEGEN_TESTER_H_ |
| 7 | 7 |
| 8 #include "src/v8.h" | 8 #include "src/v8.h" |
| 9 | 9 |
| 10 #include "src/compiler/instruction-selector.h" | 10 #include "src/compiler/instruction-selector.h" |
| 11 #include "src/compiler/pipeline.h" | 11 #include "src/compiler/pipeline.h" |
| 12 #include "src/compiler/raw-machine-assembler.h" | 12 #include "src/compiler/raw-machine-assembler.h" |
| 13 #include "src/simulator.h" | 13 #include "src/simulator.h" |
| 14 #include "test/cctest/compiler/call-tester.h" | 14 #include "test/cctest/compiler/call-tester.h" |
| 15 | 15 |
| 16 namespace v8 { | 16 namespace v8 { |
| 17 namespace internal { | 17 namespace internal { |
| 18 namespace compiler { | 18 namespace compiler { |
| 19 | 19 |
| 20 template <typename MachineAssembler> | 20 template <typename ReturnType> |
| 21 class MachineAssemblerTester : public HandleAndZoneScope, | 21 class RawMachineAssemblerTester : public HandleAndZoneScope, |
| 22 public CallHelper, | 22 public CallHelper<ReturnType>, |
| 23 public MachineAssembler { | 23 public RawMachineAssembler { |
| 24 public: | 24 public: |
| 25 MachineAssemblerTester(MachineType return_type, MachineType p0, | 25 RawMachineAssemblerTester(MachineType p0 = kMachNone, |
| 26 MachineType p1, MachineType p2, MachineType p3, | 26 MachineType p1 = kMachNone, |
| 27 MachineType p4, | 27 MachineType p2 = kMachNone, |
| 28 MachineOperatorBuilder::Flags flags = | 28 MachineType p3 = kMachNone, |
| 29 MachineOperatorBuilder::Flag::kNoFlags) | 29 MachineType p4 = kMachNone) |
| 30 : HandleAndZoneScope(), | 30 : HandleAndZoneScope(), |
| 31 CallHelper( | 31 CallHelper<ReturnType>( |
| 32 main_isolate(), | 32 main_isolate(), |
| 33 MakeMachineSignature(main_zone(), return_type, p0, p1, p2, p3, p4)), | 33 MakeMachineSignature( |
| 34 MachineAssembler( | 34 main_zone(), ReturnValueTraits<ReturnType>::Representation(), |
| 35 p0, p1, p2, p3, p4)), |
| 36 RawMachineAssembler( |
| 35 main_isolate(), new (main_zone()) Graph(main_zone()), | 37 main_isolate(), new (main_zone()) Graph(main_zone()), |
| 36 MakeMachineSignature(main_zone(), return_type, p0, p1, p2, p3, p4), | 38 MakeMachineSignature( |
| 37 kMachPtr, flags) {} | 39 main_zone(), ReturnValueTraits<ReturnType>::Representation(), |
| 38 | 40 p0, p1, p2, p3, p4), |
| 39 Node* LoadFromPointer(void* address, MachineType rep, int32_t offset = 0) { | 41 kMachPtr, InstructionSelector::SupportedMachineOperatorFlags()) {} |
| 40 return this->Load(rep, this->PointerConstant(address), | |
| 41 this->Int32Constant(offset)); | |
| 42 } | |
| 43 | |
| 44 void StoreToPointer(void* address, MachineType rep, Node* node) { | |
| 45 this->Store(rep, this->PointerConstant(address), node); | |
| 46 } | |
| 47 | |
| 48 Node* StringConstant(const char* string) { | |
| 49 return this->HeapConstant( | |
| 50 this->isolate()->factory()->InternalizeUtf8String(string)); | |
| 51 } | |
| 52 | 42 |
| 53 void CheckNumber(double expected, Object* number) { | 43 void CheckNumber(double expected, Object* number) { |
| 54 CHECK(this->isolate()->factory()->NewNumber(expected)->SameValue(number)); | 44 CHECK(this->isolate()->factory()->NewNumber(expected)->SameValue(number)); |
| 55 } | 45 } |
| 56 | 46 |
| 57 void CheckString(const char* expected, Object* string) { | 47 void CheckString(const char* expected, Object* string) { |
| 58 CHECK( | 48 CHECK( |
| 59 this->isolate()->factory()->InternalizeUtf8String(expected)->SameValue( | 49 this->isolate()->factory()->InternalizeUtf8String(expected)->SameValue( |
| 60 string)); | 50 string)); |
| 61 } | 51 } |
| 62 | 52 |
| 63 void GenerateCode() { Generate(); } | 53 void GenerateCode() { Generate(); } |
| 64 | 54 |
| 65 protected: | 55 protected: |
| 66 virtual byte* Generate() { | 56 virtual byte* Generate() { |
| 67 if (code_.is_null()) { | 57 if (code_.is_null()) { |
| 68 Schedule* schedule = this->Export(); | 58 Schedule* schedule = this->Export(); |
| 69 CallDescriptor* call_descriptor = this->call_descriptor(); | 59 CallDescriptor* call_descriptor = this->call_descriptor(); |
| 70 Graph* graph = this->graph(); | 60 Graph* graph = this->graph(); |
| 71 code_ = Pipeline::GenerateCodeForTesting(this->isolate(), call_descriptor, | 61 code_ = Pipeline::GenerateCodeForTesting(this->isolate(), call_descriptor, |
| 72 graph, schedule); | 62 graph, schedule); |
| 73 } | 63 } |
| 74 return this->code_.ToHandleChecked()->entry(); | 64 return this->code_.ToHandleChecked()->entry(); |
| 75 } | 65 } |
| 76 | 66 |
| 77 private: | 67 private: |
| 78 MaybeHandle<Code> code_; | 68 MaybeHandle<Code> code_; |
| 79 }; | |
| 80 | 69 |
| 70 // TODO(titzer): factor me elsewhere. |
| 71 static MachineSignature* MakeMachineSignature( |
| 72 Zone* zone, MachineType return_type, MachineType p0 = kMachNone, |
| 73 MachineType p1 = kMachNone, MachineType p2 = kMachNone, |
| 74 MachineType p3 = kMachNone, MachineType p4 = kMachNone) { |
| 75 // Count the number of parameters. |
| 76 size_t param_count = 5; |
| 77 MachineType types[] = {p0, p1, p2, p3, p4}; |
| 78 while (param_count > 0 && types[param_count - 1] == kMachNone) |
| 79 param_count--; |
| 80 size_t return_count = return_type == kMachNone ? 0 : 1; |
| 81 | 81 |
| 82 template <typename ReturnType> | 82 // Build the machine signature. |
| 83 class RawMachineAssemblerTester | 83 MachineSignature::Builder builder(zone, return_count, param_count); |
| 84 : public MachineAssemblerTester<RawMachineAssembler>, | 84 if (return_count > 0) builder.AddReturn(return_type); |
| 85 public CallHelper2<ReturnType, RawMachineAssemblerTester<ReturnType> > { | 85 for (size_t i = 0; i < param_count; i++) { |
| 86 public: | 86 builder.AddParam(types[i]); |
| 87 RawMachineAssemblerTester(MachineType p0 = kMachNone, | |
| 88 MachineType p1 = kMachNone, | |
| 89 MachineType p2 = kMachNone, | |
| 90 MachineType p3 = kMachNone, | |
| 91 MachineType p4 = kMachNone) | |
| 92 : MachineAssemblerTester<RawMachineAssembler>( | |
| 93 ReturnValueTraits<ReturnType>::Representation(), p0, p1, p2, p3, p4, | |
| 94 InstructionSelector::SupportedMachineOperatorFlags()) {} | |
| 95 | |
| 96 template <typename Ci, typename Fn> | |
| 97 void Run(const Ci& ci, const Fn& fn) { | |
| 98 typename Ci::const_iterator i; | |
| 99 for (i = ci.begin(); i != ci.end(); ++i) { | |
| 100 CHECK_EQ(fn(*i), this->Call(*i)); | |
| 101 } | 87 } |
| 102 } | 88 return builder.Build(); |
| 103 | |
| 104 template <typename Ci, typename Cj, typename Fn> | |
| 105 void Run(const Ci& ci, const Cj& cj, const Fn& fn) { | |
| 106 typename Ci::const_iterator i; | |
| 107 typename Cj::const_iterator j; | |
| 108 for (i = ci.begin(); i != ci.end(); ++i) { | |
| 109 for (j = cj.begin(); j != cj.end(); ++j) { | |
| 110 CHECK_EQ(fn(*i, *j), this->Call(*i, *j)); | |
| 111 } | |
| 112 } | |
| 113 } | 89 } |
| 114 }; | 90 }; |
| 115 | 91 |
| 116 | 92 |
| 117 static const bool USE_RESULT_BUFFER = true; | 93 static const bool USE_RESULT_BUFFER = true; |
| 118 static const bool USE_RETURN_REGISTER = false; | 94 static const bool USE_RETURN_REGISTER = false; |
| 119 static const int32_t CHECK_VALUE = 0x99BEEDCE; | 95 static const int32_t CHECK_VALUE = 0x99BEEDCE; |
| 120 | 96 |
| 121 | 97 |
| 122 // TODO(titzer): use the C-style calling convention, or any register-based | 98 // TODO(titzer): use the C-style calling convention, or any register-based |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 } else { | 335 } else { |
| 360 CHECK_EQ(x, y); | 336 CHECK_EQ(x, y); |
| 361 } | 337 } |
| 362 } | 338 } |
| 363 | 339 |
| 364 } // namespace compiler | 340 } // namespace compiler |
| 365 } // namespace internal | 341 } // namespace internal |
| 366 } // namespace v8 | 342 } // namespace v8 |
| 367 | 343 |
| 368 #endif // V8_CCTEST_COMPILER_CODEGEN_TESTER_H_ | 344 #endif // V8_CCTEST_COMPILER_CODEGEN_TESTER_H_ |
| OLD | NEW |