From 496697df4e8195aecee881b10b6760630fc1a27c Mon Sep 17 00:00:00 2001 From: "titzer@chromium.org" Date: Thu, 28 Aug 2014 13:17:38 +0000 Subject: [PATCH] Add MachineSignature, which is an encapsulation of the machine types for parameters and return values in a graph. This utility will be used to simplify Linkage and fix representation inference to work with graphs where parameters and return values are something other than tagged. It will also make testing representation inference a lot easier, since we can then exactly nail down the machine types of parameters and returns. This CL also adds c-signature.h, which demonstrates how to convert C function signatures into MachineSignatures. The CSignatures will be used in tests to make it easier and simpler to codegen tests. R=jarin@chromium.org BUG= Review URL: https://codereview.chromium.org/515173002 git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@23490 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/compiler/change-lowering.cc | 1 + src/compiler/linkage-impl.h | 6 +- src/compiler/linkage.h | 2 +- src/compiler/machine-operator.h | 6 +- src/compiler/machine-type.h | 26 ++++ src/compiler/raw-machine-assembler.h | 2 +- src/compiler/simplified-operator-reducer.cc | 1 + src/compiler/structured-machine-assembler.h | 2 +- test/cctest/cctest.gyp | 1 + test/cctest/compiler/c-signature.h | 133 +++++++++++++++++++++ test/cctest/compiler/codegen-tester.h | 2 +- test/cctest/compiler/instruction-selector-tester.h | 2 +- test/cctest/compiler/test-run-machops.cc | 24 ++-- test/cctest/compiler/test-simplified-lowering.cc | 5 +- .../compiler/test-structured-ifbuilder-fuzzer.cc | 4 +- .../compiler/test-structured-machine-assembler.cc | 5 +- 16 files changed, 185 insertions(+), 37 deletions(-) create mode 100644 test/cctest/compiler/c-signature.h diff --git a/src/compiler/change-lowering.cc b/src/compiler/change-lowering.cc index 1024f02..edf9bc5 100644 --- a/src/compiler/change-lowering.cc +++ b/src/compiler/change-lowering.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "src/compiler/change-lowering.h" +#include "src/compiler/machine-operator.h" #include "src/compiler/js-graph.h" diff --git a/src/compiler/linkage-impl.h b/src/compiler/linkage-impl.h index 6f0159e..83f306f 100644 --- a/src/compiler/linkage-impl.h +++ b/src/compiler/linkage-impl.h @@ -21,8 +21,7 @@ class LinkageHelper { } static inline LinkageLocation WordRegisterLocation(Register reg) { - return LinkageLocation(MachineOperatorBuilder::pointer_rep(), - Register::ToAllocationIndex(reg)); + return LinkageLocation(kMachPtr, Register::ToAllocationIndex(reg)); } static LinkageLocation UnconstrainedRegister(MachineType rep) { @@ -180,8 +179,7 @@ class LinkageHelper { int index = 0; locations[index++] = TaggedRegisterLocation(LinkageTraits::ReturnValueReg()); - locations[index++] = LinkageHelper::UnconstrainedRegister( - MachineOperatorBuilder::pointer_rep()); + locations[index++] = LinkageHelper::UnconstrainedRegister(kMachPtr); // TODO(dcarney): test with lots of parameters. int i = 0; for (; i < LinkageTraits::CRegisterParametersLength() && i < num_params; diff --git a/src/compiler/linkage.h b/src/compiler/linkage.h index 5e887b7..be35c6c 100644 --- a/src/compiler/linkage.h +++ b/src/compiler/linkage.h @@ -9,7 +9,7 @@ #include "src/code-stubs.h" #include "src/compiler/frame.h" -#include "src/compiler/machine-operator.h" +#include "src/compiler/machine-type.h" #include "src/compiler/node.h" #include "src/compiler/operator.h" #include "src/zone.h" diff --git a/src/compiler/machine-operator.h b/src/compiler/machine-operator.h index 402d489..9119ed6 100644 --- a/src/compiler/machine-operator.h +++ b/src/compiler/machine-operator.h @@ -31,7 +31,7 @@ struct StoreRepresentation { // for generating code to run on architectures such as ia32, x64, arm, etc. class MachineOperatorBuilder { public: - explicit MachineOperatorBuilder(Zone* zone, MachineType word = pointer_rep()) + explicit MachineOperatorBuilder(Zone* zone, MachineType word = kMachPtr) : zone_(zone), word_(word) { CHECK(word == kRepWord32 || word == kRepWord64); } @@ -156,10 +156,6 @@ class MachineOperatorBuilder { inline bool is64() const { return word_ == kRepWord64; } inline MachineType word() const { return word_; } - static inline MachineType pointer_rep() { - return kPointerSize == 8 ? kRepWord64 : kRepWord32; - } - #undef WORD_SIZE #undef UNOP #undef BINOP diff --git a/src/compiler/machine-type.h b/src/compiler/machine-type.h index ca37527..6e749ab 100644 --- a/src/compiler/machine-type.h +++ b/src/compiler/machine-type.h @@ -108,6 +108,32 @@ inline int ElementSizeOf(MachineType machine_type) { } } +// Describes the inputs and outputs of a function or call in terms of machine +// types. +class MachineSignature { + public: + MachineSignature(uint8_t return_count, uint16_t param_count, + MachineType* reps) + : return_count_(return_count), param_count_(param_count), reps_(reps) {} + + int GetReturnCount() const { return return_count_; } + int GetParamCount() const { return param_count_; } + + MachineType GetParameterType(int index) const { + DCHECK(index >= 0 && index < param_count_); + return reps_[return_count_ + index]; + } + + MachineType GetReturnType(int index = 0) const { + DCHECK(index >= 0 && index < return_count_); + return reps_[index]; + } + + protected: + uint8_t return_count_; + uint16_t param_count_; + MachineType* reps_; +}; } // namespace compiler } // namespace internal } // namespace v8 diff --git a/src/compiler/raw-machine-assembler.h b/src/compiler/raw-machine-assembler.h index f7cc8a3..095c892 100644 --- a/src/compiler/raw-machine-assembler.h +++ b/src/compiler/raw-machine-assembler.h @@ -47,7 +47,7 @@ class RawMachineAssembler : public GraphBuilder, RawMachineAssembler(Graph* graph, MachineCallDescriptorBuilder* call_descriptor_builder, - MachineType word = MachineOperatorBuilder::pointer_rep()); + MachineType word = kMachPtr); virtual ~RawMachineAssembler() {} Isolate* isolate() const { return zone()->isolate(); } diff --git a/src/compiler/simplified-operator-reducer.cc b/src/compiler/simplified-operator-reducer.cc index 2d1a002..ac6153e 100644 --- a/src/compiler/simplified-operator-reducer.cc +++ b/src/compiler/simplified-operator-reducer.cc @@ -4,6 +4,7 @@ #include "src/compiler/generic-node-inl.h" #include "src/compiler/js-graph.h" +#include "src/compiler/machine-operator.h" #include "src/compiler/node-matchers.h" #include "src/compiler/simplified-operator-reducer.h" diff --git a/src/compiler/structured-machine-assembler.h b/src/compiler/structured-machine-assembler.h index 6bac9cd..7fe34a1 100644 --- a/src/compiler/structured-machine-assembler.h +++ b/src/compiler/structured-machine-assembler.h @@ -63,7 +63,7 @@ class StructuredMachineAssembler StructuredMachineAssembler( Graph* graph, MachineCallDescriptorBuilder* call_descriptor_builder, - MachineType word = MachineOperatorBuilder::pointer_rep()); + MachineType word = kMachPtr); virtual ~StructuredMachineAssembler() {} Isolate* isolate() const { return zone()->isolate(); } diff --git a/test/cctest/cctest.gyp b/test/cctest/cctest.gyp index bb1ece1..aad95da 100644 --- a/test/cctest/cctest.gyp +++ b/test/cctest/cctest.gyp @@ -44,6 +44,7 @@ ], 'sources': [ ### gcmole(all) ### '<(generated_file)', + 'compiler/c-signature.h', 'compiler/codegen-tester.cc', 'compiler/codegen-tester.h', 'compiler/function-tester.h', diff --git a/test/cctest/compiler/c-signature.h b/test/cctest/compiler/c-signature.h new file mode 100644 index 0000000..5d161db --- /dev/null +++ b/test/cctest/compiler/c-signature.h @@ -0,0 +1,133 @@ +// 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_C_SIGNATURE_H_ +#define V8_COMPILER_C_SIGNATURE_H_ + +#include "src/compiler/machine-type.h" + +namespace v8 { +namespace internal { +namespace compiler { + +template +inline MachineType MachineTypeForC() { + CHECK(false); // Instantiated with invalid type. + return kMachNone; +} + +template <> +inline MachineType MachineTypeForC() { + return kMachNone; +} + +template <> +inline MachineType MachineTypeForC() { + return kMachInt8; +} + +template <> +inline MachineType MachineTypeForC() { + return kMachUint8; +} + +template <> +inline MachineType MachineTypeForC() { + return kMachInt16; +} + +template <> +inline MachineType MachineTypeForC() { + return kMachUint16; +} + +template <> +inline MachineType MachineTypeForC() { + return kMachInt32; +} + +template <> +inline MachineType MachineTypeForC() { + return kMachUint32; +} + +template <> +inline MachineType MachineTypeForC() { + return kMachInt64; +} + +template <> +inline MachineType MachineTypeForC() { + return kMachUint64; +} + +template <> +inline MachineType MachineTypeForC() { + return kMachFloat64; +} + +template <> +inline MachineType MachineTypeForC() { + return kMachAnyTagged; +} + +template +class CSignatureOf : public MachineSignature { + protected: + MachineType storage_[1 + kParamCount]; + + CSignatureOf() + : MachineSignature(MachineTypeForC() != kMachNone ? 1 : 0, + kParamCount, + reinterpret_cast(&storage_)) { + if (return_count_ == 1) storage_[0] = MachineTypeForC(); + } + void Set(int index, MachineType type) { + DCHECK(index >= 0 && index < kParamCount); + reps_[return_count_ + index] = type; + } +}; + +// Helper classes for instantiating Signature objects to be callable from C. +template +class CSignature0 : public CSignatureOf { + public: + CSignature0() : CSignatureOf() {} +}; + +template +class CSignature1 : public CSignatureOf { + public: + CSignature1() : CSignatureOf() { + this->Set(0, MachineTypeForC()); + } +}; + +template +class CSignature2 : public CSignatureOf { + public: + CSignature2() : CSignatureOf() { + this->Set(0, MachineTypeForC()); + this->Set(1, MachineTypeForC()); + } +}; + +template +class CSignature3 : public CSignatureOf { + public: + CSignature3() : CSignatureOf() { + this->Set(0, MachineTypeForC()); + this->Set(1, MachineTypeForC()); + this->Set(2, MachineTypeForC()); + } +}; + +static const CSignature2 int32_int32_to_int32; +static const CSignature2 uint32_uint32_to_uint32; +static const CSignature2 float64_float64_to_float64; +} +} +} // namespace v8::internal::compiler + +#endif // V8_COMPILER_C_SIGNATURE_H_ diff --git a/test/cctest/compiler/codegen-tester.h b/test/cctest/compiler/codegen-tester.h index 469f86c..6c24277 100644 --- a/test/cctest/compiler/codegen-tester.h +++ b/test/cctest/compiler/codegen-tester.h @@ -30,7 +30,7 @@ class MachineAssemblerTester : public HandleAndZoneScope, MachineAssembler(new (main_zone()) Graph(main_zone()), ToCallDescriptorBuilder(main_zone(), return_type, p0, p1, p2, p3, p4), - MachineOperatorBuilder::pointer_rep()) {} + kMachPtr) {} Node* LoadFromPointer(void* address, MachineType rep, int32_t offset = 0) { return this->Load(rep, this->PointerConstant(address), diff --git a/test/cctest/compiler/instruction-selector-tester.h b/test/cctest/compiler/instruction-selector-tester.h index aa29a0c..3a28b2e 100644 --- a/test/cctest/compiler/instruction-selector-tester.h +++ b/test/cctest/compiler/instruction-selector-tester.h @@ -40,7 +40,7 @@ class InstructionSelectorTester : public HandleAndZoneScope, new (main_zone()) Graph(main_zone()), new (main_zone()) MachineCallDescriptorBuilder( kMachInt32, kParameterCount, BuildParameterArray(main_zone())), - MachineOperatorBuilder::pointer_rep()) {} + kMachPtr) {} void SelectInstructions(CpuFeature feature) { SelectInstructions(InstructionSelector::Features(feature)); diff --git a/test/cctest/compiler/test-run-machops.cc b/test/cctest/compiler/test-run-machops.cc index 59796c3..f0555b5 100644 --- a/test/cctest/compiler/test-run-machops.cc +++ b/test/cctest/compiler/test-run-machops.cc @@ -3534,10 +3534,9 @@ TEST(RunCallSeven) { RawMachineAssemblerTester m; Node** args = NULL; MachineType* arg_types = NULL; - Node* function = - call_direct ? m.PointerConstant(function_address) - : m.LoadFromPointer(&function_address, - MachineOperatorBuilder::pointer_rep()); + Node* function = call_direct + ? m.PointerConstant(function_address) + : m.LoadFromPointer(&function_address, kMachPtr); m.Return(m.CallC(function, kMachInt32, arg_types, args, 0)); CHECK_EQ(7, m.Call()); @@ -3554,10 +3553,9 @@ TEST(RunCallUnaryMinus) { RawMachineAssemblerTester m(kMachInt32); Node* args[] = {m.Parameter(0)}; MachineType arg_types[] = {kMachInt32}; - Node* function = - call_direct ? m.PointerConstant(function_address) - : m.LoadFromPointer(&function_address, - MachineOperatorBuilder::pointer_rep()); + Node* function = call_direct + ? m.PointerConstant(function_address) + : m.LoadFromPointer(&function_address, kMachPtr); m.Return(m.CallC(function, kMachInt32, arg_types, args, 1)); FOR_INT32_INPUTS(i) { @@ -3577,10 +3575,9 @@ TEST(RunCallAPlusTwoB) { RawMachineAssemblerTester m(kMachInt32, kMachInt32); Node* args[] = {m.Parameter(0), m.Parameter(1)}; MachineType arg_types[] = {kMachInt32, kMachInt32}; - Node* function = - call_direct ? m.PointerConstant(function_address) - : m.LoadFromPointer(&function_address, - MachineOperatorBuilder::pointer_rep()); + Node* function = call_direct + ? m.PointerConstant(function_address) + : m.LoadFromPointer(&function_address, kMachPtr); m.Return(m.CallC(function, kMachInt32, arg_types, args, 2)); FOR_INT32_INPUTS(i) { @@ -3819,8 +3816,7 @@ TEST(RunLoadStoreTruncation) { static void IntPtrCompare(intptr_t left, intptr_t right) { for (int test = 0; test < 7; test++) { - RawMachineAssemblerTester m(MachineOperatorBuilder::pointer_rep(), - MachineOperatorBuilder::pointer_rep()); + RawMachineAssemblerTester m(kMachPtr, kMachPtr); Node* p0 = m.Parameter(0); Node* p1 = m.Parameter(1); Node* res = NULL; diff --git a/test/cctest/compiler/test-simplified-lowering.cc b/test/cctest/compiler/test-simplified-lowering.cc index 4a65435..ac72d78 100644 --- a/test/cctest/compiler/test-simplified-lowering.cc +++ b/test/cctest/compiler/test-simplified-lowering.cc @@ -72,9 +72,8 @@ FieldAccess ForJSObjectProperties() { FieldAccess ForArrayBufferBackingStore() { FieldAccess access = { - kTaggedBase, JSArrayBuffer::kBackingStoreOffset, - Handle(), Type::UntaggedPtr(), - MachineOperatorBuilder::pointer_rep(), + kTaggedBase, JSArrayBuffer::kBackingStoreOffset, Handle(), + Type::UntaggedPtr(), kMachPtr, }; return access; } diff --git a/test/cctest/compiler/test-structured-ifbuilder-fuzzer.cc b/test/cctest/compiler/test-structured-ifbuilder-fuzzer.cc index 74cba29..e1c8286 100644 --- a/test/cctest/compiler/test-structured-ifbuilder-fuzzer.cc +++ b/test/cctest/compiler/test-structured-ifbuilder-fuzzer.cc @@ -267,9 +267,7 @@ class IfBuilderModel { class IfBuilderGenerator : public StructuredMachineAssemblerTester { public: IfBuilderGenerator() - : StructuredMachineAssemblerTester( - MachineOperatorBuilder::pointer_rep(), - MachineOperatorBuilder::pointer_rep()), + : StructuredMachineAssemblerTester(kMachPtr, kMachPtr), var_(NewVariable(Int32Constant(kInitalVar))), c_(this), m_(this->zone()), diff --git a/test/cctest/compiler/test-structured-machine-assembler.cc b/test/cctest/compiler/test-structured-machine-assembler.cc index 2ddd240..eca07fa 100644 --- a/test/cctest/compiler/test-structured-machine-assembler.cc +++ b/test/cctest/compiler/test-structured-machine-assembler.cc @@ -878,9 +878,8 @@ TEST(RunSimpleExpressionVariable1) { class QuicksortHelper : public StructuredMachineAssemblerTester { public: QuicksortHelper() - : StructuredMachineAssemblerTester( - MachineOperatorBuilder::pointer_rep(), kMachInt32, - MachineOperatorBuilder::pointer_rep(), kMachInt32), + : StructuredMachineAssemblerTester(kMachPtr, kMachInt32, + kMachPtr, kMachInt32), input_(NULL), stack_limit_(NULL), one_(Int32Constant(1)), -- 2.7.4