From 3d76d2a2dc36c3f55fc5648334a5845b404f1c37 Mon Sep 17 00:00:00 2001 From: Clemens Hammacher Date: Thu, 4 May 2017 16:19:04 +0200 Subject: [PATCH] [cctest] Refactor CallHelper to use variadic templates Instead of explicitly instantiating six copies of each function for zero to five arguments, define them as variadic template. This avoids the arbitrary limit to five arguments, and avoids lots of code duplication. Drive-by fix: [iwyu] Add missing include. R=mstarzinger@chromium.org Change-Id: Id7edde3611ddfade16c7afbbf4b9000cedd76468 Reviewed-on: https://chromium-review.googlesource.com/496247 Reviewed-by: Michael Starzinger Commit-Queue: Clemens Hammacher Cr-Commit-Position: refs/heads/master@{#45130} --- test/cctest/compiler/call-tester.h | 194 ++++------------------------- 1 file changed, 21 insertions(+), 173 deletions(-) diff --git a/test/cctest/compiler/call-tester.h b/test/cctest/compiler/call-tester.h index 77d2ce1e95..06fb1b5202 100644 --- a/test/cctest/compiler/call-tester.h +++ b/test/cctest/compiler/call-tester.h @@ -5,6 +5,7 @@ #ifndef V8_CCTEST_COMPILER_CALL_TESTER_H_ #define V8_CCTEST_COMPILER_CALL_TESTER_H_ +#include "src/handles.h" #include "src/simulator.h" #include "test/cctest/compiler/c-signature.h" @@ -127,45 +128,11 @@ class CallHelper { } virtual ~CallHelper() {} - R Call() { - typedef R V8_CDECL FType(); - CSignature::VerifyParams(csig_); - return DoCall(FUNCTION_CAST(Generate())); - } - - template - R Call(P1 p1) { - typedef R V8_CDECL FType(P1); - CSignature::VerifyParams(csig_); - return DoCall(FUNCTION_CAST(Generate()), p1); - } - - template - R Call(P1 p1, P2 p2) { - typedef R V8_CDECL FType(P1, P2); - CSignature::VerifyParams(csig_); - return DoCall(FUNCTION_CAST(Generate()), p1, p2); - } - - template - R Call(P1 p1, P2 p2, P3 p3) { - typedef R V8_CDECL FType(P1, P2, P3); - CSignature::VerifyParams(csig_); - return DoCall(FUNCTION_CAST(Generate()), p1, p2, p3); - } - - template - R Call(P1 p1, P2 p2, P3 p3, P4 p4) { - typedef R V8_CDECL FType(P1, P2, P3, P4); - CSignature::VerifyParams(csig_); - return DoCall(FUNCTION_CAST(Generate()), p1, p2, p3, p4); - } - - template - R Call(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { - typedef R V8_CDECL FType(P1, P2, P3, P4, P5); - CSignature::VerifyParams(csig_); - return DoCall(FUNCTION_CAST(Generate()), p1, p2, p3, p4, p5); + template + R Call(Params... args) { + using FType = R(V8_CDECL*)(Params...); + CSignature::VerifyParams(csig_); + return DoCall(FUNCTION_CAST(Generate()), args...); } protected: @@ -180,47 +147,11 @@ class CallHelper { return static_cast(simulator->CallInt64(f, args)); } - template - R DoCall(F* f) { - Simulator::CallArgument args[] = {Simulator::CallArgument::End()}; - return CastReturnValue(CallSimulator(FUNCTION_ADDR(f), args)); - } - template - R DoCall(F* f, P1 p1) { - Simulator::CallArgument args[] = {Simulator::CallArgument(p1), - Simulator::CallArgument::End()}; - return CastReturnValue(CallSimulator(FUNCTION_ADDR(f), args)); - } - template - R DoCall(F* f, P1 p1, P2 p2) { - Simulator::CallArgument args[] = {Simulator::CallArgument(p1), - Simulator::CallArgument(p2), - Simulator::CallArgument::End()}; - return CastReturnValue(CallSimulator(FUNCTION_ADDR(f), args)); - } - template - R DoCall(F* f, P1 p1, P2 p2, P3 p3) { - Simulator::CallArgument args[] = { - Simulator::CallArgument(p1), Simulator::CallArgument(p2), - Simulator::CallArgument(p3), Simulator::CallArgument::End()}; - return CastReturnValue(CallSimulator(FUNCTION_ADDR(f), args)); - } - template - R DoCall(F* f, P1 p1, P2 p2, P3 p3, P4 p4) { - Simulator::CallArgument args[] = { - Simulator::CallArgument(p1), Simulator::CallArgument(p2), - Simulator::CallArgument(p3), Simulator::CallArgument(p4), - Simulator::CallArgument::End()}; - return CastReturnValue(CallSimulator(FUNCTION_ADDR(f), args)); - } - template - R DoCall(F* f, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { - Simulator::CallArgument args[] = { - Simulator::CallArgument(p1), Simulator::CallArgument(p2), - Simulator::CallArgument(p3), Simulator::CallArgument(p4), - Simulator::CallArgument(p5), Simulator::CallArgument::End()}; - return CastReturnValue(CallSimulator(FUNCTION_ADDR(f), args)); + template + R DoCall(F* f, Params... args) { + Simulator::CallArgument args_arr[] = {Simulator::CallArgument(args)..., + Simulator::CallArgument::End()}; + return CastReturnValue(CallSimulator(FUNCTION_ADDR(f), args_arr)); } #elif USE_SIMULATOR && \ (V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_PPC64 || V8_TARGET_ARCH_S390X) @@ -230,42 +161,10 @@ class CallHelper { return static_cast(simulator->Call(f, 5, p1, p2, p3, p4, p5)); } - - template - R DoCall(F* f) { - return CastReturnValue(CallSimulator(FUNCTION_ADDR(f))); - } - template - R DoCall(F* f, P1 p1) { - return CastReturnValue( - CallSimulator(FUNCTION_ADDR(f), ParameterTraits::Cast(p1))); - } - template - R DoCall(F* f, P1 p1, P2 p2) { - return CastReturnValue(CallSimulator(FUNCTION_ADDR(f), - ParameterTraits::Cast(p1), - ParameterTraits::Cast(p2))); - } - template - R DoCall(F* f, P1 p1, P2 p2, P3 p3) { + template + R DoCall(F* f, Params... args) { return CastReturnValue(CallSimulator( - FUNCTION_ADDR(f), ParameterTraits::Cast(p1), - ParameterTraits::Cast(p2), ParameterTraits::Cast(p3))); - } - template - R DoCall(F* f, P1 p1, P2 p2, P3 p3, P4 p4) { - return CastReturnValue(CallSimulator( - FUNCTION_ADDR(f), ParameterTraits::Cast(p1), - ParameterTraits::Cast(p2), ParameterTraits::Cast(p3), - ParameterTraits::Cast(p4))); - } - template - R DoCall(F* f, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { - return CastReturnValue(CallSimulator( - FUNCTION_ADDR(f), ParameterTraits::Cast(p1), - ParameterTraits::Cast(p2), ParameterTraits::Cast(p3), - ParameterTraits::Cast(p4), ParameterTraits::Cast(p5))); + FUNCTION_ADDR(f), ParameterTraits::Cast(args)...)); } #elif USE_SIMULATOR && (V8_TARGET_ARCH_ARM || V8_TARGET_ARCH_MIPS || \ V8_TARGET_ARCH_PPC || V8_TARGET_ARCH_S390) @@ -274,67 +173,16 @@ class CallHelper { Simulator* simulator = Simulator::current(isolate_); return static_cast(simulator->Call(f, 5, p1, p2, p3, p4, p5)); } - template - R DoCall(F* f) { - return CastReturnValue(CallSimulator(FUNCTION_ADDR(f))); - } - template - R DoCall(F* f, P1 p1) { - return CastReturnValue( - CallSimulator(FUNCTION_ADDR(f), ParameterTraits::Cast(p1))); - } - template - R DoCall(F* f, P1 p1, P2 p2) { - return CastReturnValue(CallSimulator(FUNCTION_ADDR(f), - ParameterTraits::Cast(p1), - ParameterTraits::Cast(p2))); - } - template - R DoCall(F* f, P1 p1, P2 p2, P3 p3) { + + template + R DoCall(F* f, Params... args) { return CastReturnValue(CallSimulator( - FUNCTION_ADDR(f), ParameterTraits::Cast(p1), - ParameterTraits::Cast(p2), ParameterTraits::Cast(p3))); - } - template - R DoCall(F* f, P1 p1, P2 p2, P3 p3, P4 p4) { - return CastReturnValue(CallSimulator( - FUNCTION_ADDR(f), ParameterTraits::Cast(p1), - ParameterTraits::Cast(p2), ParameterTraits::Cast(p3), - ParameterTraits::Cast(p4))); - } - template - R DoCall(F* f, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { - return CastReturnValue(CallSimulator( - FUNCTION_ADDR(f), ParameterTraits::Cast(p1), - ParameterTraits::Cast(p2), ParameterTraits::Cast(p3), - ParameterTraits::Cast(p4), ParameterTraits::Cast(p5))); + FUNCTION_ADDR(f), ParameterTraits::Cast(args)...)); } #else - template - R DoCall(F* f) { - return f(); - } - template - R DoCall(F* f, P1 p1) { - return f(p1); - } - template - R DoCall(F* f, P1 p1, P2 p2) { - return f(p1, p2); - } - template - R DoCall(F* f, P1 p1, P2 p2, P3 p3) { - return f(p1, p2, p3); - } - template - R DoCall(F* f, P1 p1, P2 p2, P3 p3, P4 p4) { - return f(p1, p2, p3, p4); - } - template - R DoCall(F* f, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { - return f(p1, p2, p3, p4, p5); + template + R DoCall(F* f, Params... args) { + return f(args...); } #endif