summaryrefslogtreecommitdiff
path: root/deps/v8/test/unittests/compiler/run-tail-calls-unittest.cc
blob: 049faff5c58f674a33b560eda394f6f6d89edb83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2022 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.

#include "src/base/utils/random-number-generator.h"
#include "src/codegen/assembler-inl.h"
#include "src/codegen/code-stub-assembler.h"
#include "src/codegen/macro-assembler.h"
#include "src/objects/code-inl.h"
#include "test/common/code-assembler-tester.h"
#include "test/unittests/compiler/function-tester.h"
#include "test/unittests/test-utils.h"

namespace v8 {
namespace internal {
namespace compiler {

#define __ assembler.

namespace {

// Function that takes a number of pointer-sized integer arguments, calculates a
// weighted sum of them and returns it.
Handle<Code> BuildCallee(Isolate* isolate, CallDescriptor* call_descriptor) {
  CodeAssemblerTester tester(isolate, call_descriptor, "callee");
  CodeStubAssembler assembler(tester.state());
  int param_slots = static_cast<int>(call_descriptor->ParameterSlotCount());
  TNode<IntPtrT> sum = __ IntPtrConstant(0);
  for (int i = 0; i < param_slots; ++i) {
    TNode<WordT> product = __ IntPtrMul(__ UncheckedParameter<IntPtrT>(i),
                                        __ IntPtrConstant(i + 1));
    sum = __ Signed(__ IntPtrAdd(sum, product));
  }
  __ Return(sum);
  return tester.GenerateCodeCloseAndEscape();
}

// Function that tail-calls another function with a number of pointer-sized
// integer arguments.
Handle<Code> BuildCaller(Isolate* isolate, CallDescriptor* call_descriptor,
                         CallDescriptor* callee_descriptor) {
  CodeAssemblerTester tester(isolate, call_descriptor, "caller");
  CodeStubAssembler assembler(tester.state());
  std::vector<Node*> params;
  // The first parameter is always the callee.
  Handle<CodeT> code =
      ToCodeT(BuildCallee(isolate, callee_descriptor), isolate);
  params.push_back(__ HeapConstant(code));
  int param_slots = static_cast<int>(callee_descriptor->ParameterSlotCount());
  for (int i = 0; i < param_slots; ++i) {
    params.push_back(__ IntPtrConstant(i));
  }
  DCHECK_EQ(param_slots + 1, params.size());
  tester.raw_assembler_for_testing()->TailCallN(callee_descriptor,
                                                param_slots + 1, params.data());
  return tester.GenerateCodeCloseAndEscape();
}

// Setup function, which calls "caller".
Handle<Code> BuildSetupFunction(Isolate* isolate,
                                CallDescriptor* caller_descriptor,
                                CallDescriptor* callee_descriptor) {
  CodeAssemblerTester tester(isolate, 0);
  CodeStubAssembler assembler(tester.state());
  std::vector<Node*> params;
  // The first parameter is always the callee.
  Handle<CodeT> code = ToCodeT(
      BuildCaller(isolate, caller_descriptor, callee_descriptor), isolate);
  params.push_back(__ HeapConstant(code));
  // Set up arguments for "Caller".
  int param_slots = static_cast<int>(caller_descriptor->ParameterSlotCount());
  for (int i = 0; i < param_slots; ++i) {
    // Use values that are different from the ones we will pass to this
    // function's callee later.
    params.push_back(__ IntPtrConstant(i + 42));
  }
  DCHECK_EQ(param_slots + 1, params.size());
  TNode<IntPtrT> intptr_result =
      __ UncheckedCast<IntPtrT>(tester.raw_assembler_for_testing()->CallN(
          caller_descriptor, param_slots + 1, params.data()));
  __ Return(__ SmiTag(intptr_result));
  return tester.GenerateCodeCloseAndEscape();
}

CallDescriptor* CreateDescriptorForStackArguments(Zone* zone, int param_slots) {
  LocationSignature::Builder locations(zone, 1,
                                       static_cast<size_t>(param_slots));

  locations.AddReturn(LinkageLocation::ForRegister(kReturnRegister0.code(),
                                                   MachineType::IntPtr()));

  for (int i = 0; i < param_slots; ++i) {
    locations.AddParam(LinkageLocation::ForCallerFrameSlot(
        i - param_slots, MachineType::IntPtr()));
  }

  return zone->New<CallDescriptor>(
      CallDescriptor::kCallCodeObject,  // kind
      MachineType::AnyTagged(),         // target MachineType
      LinkageLocation::ForAnyRegister(
          MachineType::AnyTagged()),  // target location
      locations.Build(),              // location_sig
      param_slots,                    // stack parameter slots
      Operator::kNoProperties,        // properties
      kNoCalleeSaved,                 // callee-saved registers
      kNoCalleeSavedFp,               // callee-saved fp
      CallDescriptor::kNoFlags);      // flags
}

}  // namespace

class RunTailCallsTest : public TestWithContextAndZone {
 protected:
  // Test a tail call from a caller with n parameters to a callee with m
  // parameters. All parameters are pointer-sized.
  void TestHelper(int n, int m) {
    Isolate* isolate = i_isolate();
    CanonicalHandleScope canonical(isolate);
    CallDescriptor* caller_descriptor =
        CreateDescriptorForStackArguments(zone(), n);
    CallDescriptor* callee_descriptor =
        CreateDescriptorForStackArguments(zone(), m);
    Handle<Code> setup =
        BuildSetupFunction(isolate, caller_descriptor, callee_descriptor);
    FunctionTester ft(isolate, setup, 0);
    Handle<Object> result = ft.Call().ToHandleChecked();
    int expected = 0;
    for (int i = 0; i < m; ++i) expected += (i + 1) * i;
    CHECK_EQ(expected, Handle<Smi>::cast(result)->value());
  }
};

#undef __

TEST_F(RunTailCallsTest, CallerOddCalleeEven) {
  TestHelper(1, 0);
  TestHelper(1, 2);
  TestHelper(3, 2);
  TestHelper(3, 4);
}

TEST_F(RunTailCallsTest, CallerOddCalleeOdd) {
  TestHelper(1, 1);
  TestHelper(1, 3);
  TestHelper(3, 1);
  TestHelper(3, 3);
}

TEST_F(RunTailCallsTest, CallerEvenCalleeEven) {
  TestHelper(0, 0);
  TestHelper(0, 2);
  TestHelper(2, 0);
  TestHelper(2, 2);
}

TEST_F(RunTailCallsTest, CallerEvenCalleeOdd) {
  TestHelper(0, 1);
  TestHelper(0, 3);
  TestHelper(2, 1);
  TestHelper(2, 3);
}

TEST_F(RunTailCallsTest, FuzzStackParamCount) {
  const int kNumTests = 20;
  const int kMaxSlots = 30;
  base::RandomNumberGenerator* const rng =
      i_isolate()->random_number_generator();
  for (int i = 0; i < kNumTests; ++i) {
    int n = rng->NextInt(kMaxSlots);
    int m = rng->NextInt(kMaxSlots);
    TestHelper(n, m);
  }
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8