2014-08-08 07:04:07 +00:00
|
|
|
// Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_
|
|
|
|
#define V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_
|
|
|
|
|
|
|
|
#include <deque>
|
2014-08-20 09:16:30 +00:00
|
|
|
#include <set>
|
2014-08-08 07:04:07 +00:00
|
|
|
|
2014-08-14 06:33:50 +00:00
|
|
|
#include "src/base/utils/random-number-generator.h"
|
2014-08-08 07:04:07 +00:00
|
|
|
#include "src/compiler/instruction-selector.h"
|
|
|
|
#include "src/compiler/raw-machine-assembler.h"
|
|
|
|
#include "test/compiler-unittests/compiler-unittests.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
|
|
|
class InstructionSelectorTest : public CompilerTest {
|
|
|
|
public:
|
2014-08-14 06:33:50 +00:00
|
|
|
InstructionSelectorTest();
|
2014-08-08 07:04:07 +00:00
|
|
|
virtual ~InstructionSelectorTest() {}
|
|
|
|
|
2014-08-14 06:33:50 +00:00
|
|
|
base::RandomNumberGenerator* rng() { return &rng_; }
|
|
|
|
|
2014-08-08 07:04:07 +00:00
|
|
|
protected:
|
|
|
|
class Stream;
|
|
|
|
|
|
|
|
enum StreamBuilderMode { kAllInstructions, kTargetInstructions };
|
|
|
|
|
|
|
|
class StreamBuilder V8_FINAL : public RawMachineAssembler {
|
|
|
|
public:
|
2014-08-11 15:55:28 +00:00
|
|
|
StreamBuilder(InstructionSelectorTest* test, MachineType return_type)
|
2014-08-08 07:04:07 +00:00
|
|
|
: RawMachineAssembler(new (test->zone()) Graph(test->zone()),
|
|
|
|
CallDescriptorBuilder(test->zone(), return_type)),
|
|
|
|
test_(test) {}
|
2014-08-11 15:55:28 +00:00
|
|
|
StreamBuilder(InstructionSelectorTest* test, MachineType return_type,
|
|
|
|
MachineType parameter0_type)
|
2014-08-08 07:04:07 +00:00
|
|
|
: RawMachineAssembler(new (test->zone()) Graph(test->zone()),
|
|
|
|
CallDescriptorBuilder(test->zone(), return_type,
|
|
|
|
parameter0_type)),
|
|
|
|
test_(test) {}
|
2014-08-11 15:55:28 +00:00
|
|
|
StreamBuilder(InstructionSelectorTest* test, MachineType return_type,
|
|
|
|
MachineType parameter0_type, MachineType parameter1_type)
|
2014-08-08 07:04:07 +00:00
|
|
|
: RawMachineAssembler(
|
|
|
|
new (test->zone()) Graph(test->zone()),
|
|
|
|
CallDescriptorBuilder(test->zone(), return_type, parameter0_type,
|
|
|
|
parameter1_type)),
|
|
|
|
test_(test) {}
|
2014-08-14 06:33:50 +00:00
|
|
|
StreamBuilder(InstructionSelectorTest* test, MachineType return_type,
|
|
|
|
MachineType parameter0_type, MachineType parameter1_type,
|
|
|
|
MachineType parameter2_type)
|
|
|
|
: RawMachineAssembler(
|
|
|
|
new (test->zone()) Graph(test->zone()),
|
|
|
|
CallDescriptorBuilder(test->zone(), return_type, parameter0_type,
|
|
|
|
parameter1_type, parameter2_type)),
|
|
|
|
test_(test) {}
|
2014-08-08 07:04:07 +00:00
|
|
|
|
|
|
|
Stream Build(CpuFeature feature) {
|
|
|
|
return Build(InstructionSelector::Features(feature));
|
|
|
|
}
|
|
|
|
Stream Build(CpuFeature feature1, CpuFeature feature2) {
|
|
|
|
return Build(InstructionSelector::Features(feature1, feature2));
|
|
|
|
}
|
|
|
|
Stream Build(StreamBuilderMode mode = kTargetInstructions) {
|
|
|
|
return Build(InstructionSelector::Features(), mode);
|
|
|
|
}
|
|
|
|
Stream Build(InstructionSelector::Features features,
|
|
|
|
StreamBuilderMode mode = kTargetInstructions);
|
|
|
|
|
|
|
|
private:
|
|
|
|
MachineCallDescriptorBuilder* CallDescriptorBuilder(
|
2014-08-11 15:55:28 +00:00
|
|
|
Zone* zone, MachineType return_type) {
|
2014-08-08 07:04:07 +00:00
|
|
|
return new (zone) MachineCallDescriptorBuilder(return_type, 0, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineCallDescriptorBuilder* CallDescriptorBuilder(
|
2014-08-11 15:55:28 +00:00
|
|
|
Zone* zone, MachineType return_type, MachineType parameter0_type) {
|
|
|
|
MachineType* parameter_types = zone->NewArray<MachineType>(1);
|
2014-08-08 07:04:07 +00:00
|
|
|
parameter_types[0] = parameter0_type;
|
|
|
|
return new (zone)
|
|
|
|
MachineCallDescriptorBuilder(return_type, 1, parameter_types);
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineCallDescriptorBuilder* CallDescriptorBuilder(
|
2014-08-11 15:55:28 +00:00
|
|
|
Zone* zone, MachineType return_type, MachineType parameter0_type,
|
|
|
|
MachineType parameter1_type) {
|
|
|
|
MachineType* parameter_types = zone->NewArray<MachineType>(2);
|
2014-08-08 07:04:07 +00:00
|
|
|
parameter_types[0] = parameter0_type;
|
|
|
|
parameter_types[1] = parameter1_type;
|
|
|
|
return new (zone)
|
|
|
|
MachineCallDescriptorBuilder(return_type, 2, parameter_types);
|
|
|
|
}
|
|
|
|
|
2014-08-14 06:33:50 +00:00
|
|
|
MachineCallDescriptorBuilder* CallDescriptorBuilder(
|
|
|
|
Zone* zone, MachineType return_type, MachineType parameter0_type,
|
|
|
|
MachineType parameter1_type, MachineType parameter2_type) {
|
|
|
|
MachineType* parameter_types = zone->NewArray<MachineType>(3);
|
|
|
|
parameter_types[0] = parameter0_type;
|
|
|
|
parameter_types[1] = parameter1_type;
|
|
|
|
parameter_types[2] = parameter2_type;
|
|
|
|
return new (zone)
|
|
|
|
MachineCallDescriptorBuilder(return_type, 3, parameter_types);
|
|
|
|
}
|
|
|
|
|
2014-08-08 07:04:07 +00:00
|
|
|
private:
|
|
|
|
InstructionSelectorTest* test_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Stream V8_FINAL {
|
|
|
|
public:
|
|
|
|
size_t size() const { return instructions_.size(); }
|
|
|
|
const Instruction* operator[](size_t index) const {
|
|
|
|
EXPECT_LT(index, size());
|
|
|
|
return instructions_[index];
|
|
|
|
}
|
|
|
|
|
2014-08-20 09:16:30 +00:00
|
|
|
bool IsDouble(int virtual_register) const {
|
|
|
|
return doubles_.find(virtual_register) != doubles_.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsReference(int virtual_register) const {
|
|
|
|
return references_.find(virtual_register) != references_.end();
|
|
|
|
}
|
|
|
|
|
2014-08-08 07:04:07 +00:00
|
|
|
int32_t ToInt32(const InstructionOperand* operand) const {
|
|
|
|
return ToConstant(operand).ToInt32();
|
|
|
|
}
|
|
|
|
|
2014-08-14 06:33:50 +00:00
|
|
|
int ToVreg(const InstructionOperand* operand) const {
|
|
|
|
EXPECT_EQ(InstructionOperand::UNALLOCATED, operand->kind());
|
|
|
|
return UnallocatedOperand::cast(operand)->virtual_register();
|
|
|
|
}
|
|
|
|
|
2014-08-08 07:04:07 +00:00
|
|
|
private:
|
|
|
|
Constant ToConstant(const InstructionOperand* operand) const {
|
|
|
|
ConstantMap::const_iterator i;
|
|
|
|
if (operand->IsConstant()) {
|
|
|
|
i = constants_.find(operand->index());
|
2014-08-19 08:27:33 +00:00
|
|
|
EXPECT_FALSE(constants_.end() == i);
|
2014-08-08 07:04:07 +00:00
|
|
|
} else {
|
|
|
|
EXPECT_EQ(InstructionOperand::IMMEDIATE, operand->kind());
|
|
|
|
i = immediates_.find(operand->index());
|
2014-08-19 08:27:33 +00:00
|
|
|
EXPECT_FALSE(immediates_.end() == i);
|
2014-08-08 07:04:07 +00:00
|
|
|
}
|
|
|
|
EXPECT_EQ(operand->index(), i->first);
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend class StreamBuilder;
|
|
|
|
|
|
|
|
typedef std::map<int, Constant> ConstantMap;
|
|
|
|
|
|
|
|
ConstantMap constants_;
|
|
|
|
ConstantMap immediates_;
|
|
|
|
std::deque<Instruction*> instructions_;
|
2014-08-20 09:16:30 +00:00
|
|
|
std::set<int> doubles_;
|
|
|
|
std::set<int> references_;
|
2014-08-08 07:04:07 +00:00
|
|
|
};
|
2014-08-14 06:33:50 +00:00
|
|
|
|
|
|
|
base::RandomNumberGenerator rng_;
|
2014-08-08 07:04:07 +00:00
|
|
|
};
|
|
|
|
|
2014-08-14 06:33:50 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class InstructionSelectorTestWithParam
|
|
|
|
: public InstructionSelectorTest,
|
|
|
|
public ::testing::WithParamInterface<T> {};
|
|
|
|
|
2014-08-08 07:04:07 +00:00
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
|
|
|
|
|
|
|
#endif // V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_
|