summaryrefslogtreecommitdiff
path: root/deps/v8/test/cctest/compiler/test-multiple-return.cc
blob: 1f46e870774861b58ecea72927dc8197f5876545 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// 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.

#include <cmath>
#include <functional>
#include <limits>
#include <memory>

#include "src/assembler.h"
#include "src/base/bits.h"
#include "src/codegen.h"
#include "src/compiler.h"
#include "src/compiler/linkage.h"
#include "src/machine-type.h"
#include "src/macro-assembler.h"
#include "src/objects-inl.h"
#include "test/cctest/cctest.h"
#include "test/cctest/compiler/codegen-tester.h"
#include "test/cctest/compiler/value-helper.h"

namespace v8 {
namespace internal {
namespace compiler {

namespace {

int size(MachineType type) {
  return 1 << ElementSizeLog2Of(type.representation());
}

int num_registers(MachineType type) {
  const RegisterConfiguration* config = RegisterConfiguration::Default();
  switch (type.representation()) {
    case MachineRepresentation::kWord32:
    case MachineRepresentation::kWord64:
      return config->num_allocatable_general_registers();
    case MachineRepresentation::kFloat32:
      return config->num_allocatable_float_registers();
    case MachineRepresentation::kFloat64:
      return config->num_allocatable_double_registers();
    default:
      UNREACHABLE();
  }
}

const int* codes(MachineType type) {
  const RegisterConfiguration* config = RegisterConfiguration::Default();
  switch (type.representation()) {
    case MachineRepresentation::kWord32:
    case MachineRepresentation::kWord64:
      return config->allocatable_general_codes();
    case MachineRepresentation::kFloat32:
      return config->allocatable_float_codes();
    case MachineRepresentation::kFloat64:
      return config->allocatable_double_codes();
    default:
      UNREACHABLE();
  }
}

CallDescriptor* CreateMonoCallDescriptor(Zone* zone, int return_count,
                                         int param_count, MachineType type) {
  LocationSignature::Builder locations(zone, return_count, param_count);

  int span = std::max(1, size(type) / kPointerSize);
  int stack_params = 0;
  for (int i = 0; i < param_count; i++) {
    LinkageLocation location = LinkageLocation::ForAnyRegister();
    if (i < num_registers(type)) {
      location = LinkageLocation::ForRegister(codes(type)[i], type);
    } else {
      int slot = span * (i - param_count);
      location = LinkageLocation::ForCallerFrameSlot(slot, type);
      stack_params += span;
    }
    locations.AddParam(location);
  }

  int stack_returns = 0;
  for (int i = 0; i < return_count; i++) {
    LinkageLocation location = LinkageLocation::ForAnyRegister();
    if (i < num_registers(type)) {
      location = LinkageLocation::ForRegister(codes(type)[i], type);
    } else {
      int slot = span * (num_registers(type) - i) - stack_params - 1;
      location = LinkageLocation::ForCallerFrameSlot(slot, type);
      stack_returns += span;
    }
    locations.AddReturn(location);
  }

  const RegList kCalleeSaveRegisters = 0;
  const RegList kCalleeSaveFPRegisters = 0;

  MachineType target_type = MachineType::AnyTagged();
  LinkageLocation target_loc = LinkageLocation::ForAnyRegister(target_type);
  return new (zone) CallDescriptor(       // --
      CallDescriptor::kCallCodeObject,    // kind
      target_type,                        // target MachineType
      target_loc,                         // target location
      locations.Build(),                  // location_sig
      stack_params,                       // on-stack parameter count
      compiler::Operator::kNoProperties,  // properties
      kCalleeSaveRegisters,               // callee-saved registers
      kCalleeSaveFPRegisters,             // callee-saved fp regs
      CallDescriptor::kNoFlags,           // flags
      "c-call",                           // debug name
      0,                                  // allocatable registers
      stack_returns);                     // on-stack return count
}

}  // namespace

Node* Constant(RawMachineAssembler& m, MachineType type, int value) {
  switch (type.representation()) {
    case MachineRepresentation::kWord32:
      return m.Int32Constant(static_cast<int32_t>(value));
    case MachineRepresentation::kWord64:
      return m.Int64Constant(static_cast<int64_t>(value));
    case MachineRepresentation::kFloat32:
      return m.Float32Constant(static_cast<float>(value));
    case MachineRepresentation::kFloat64:
      return m.Float64Constant(static_cast<double>(value));
    default:
      UNREACHABLE();
  }
}

Node* Add(RawMachineAssembler& m, MachineType type, Node* a, Node* b) {
  switch (type.representation()) {
    case MachineRepresentation::kWord32:
      return m.Int32Add(a, b);
    case MachineRepresentation::kWord64:
      return m.Int64Add(a, b);
    case MachineRepresentation::kFloat32:
      return m.Float32Add(a, b);
    case MachineRepresentation::kFloat64:
      return m.Float64Add(a, b);
    default:
      UNREACHABLE();
  }
}

Node* Sub(RawMachineAssembler& m, MachineType type, Node* a, Node* b) {
  switch (type.representation()) {
    case MachineRepresentation::kWord32:
      return m.Int32Sub(a, b);
    case MachineRepresentation::kWord64:
      return m.Int64Sub(a, b);
    case MachineRepresentation::kFloat32:
      return m.Float32Sub(a, b);
    case MachineRepresentation::kFloat64:
      return m.Float64Sub(a, b);
    default:
      UNREACHABLE();
  }
}

Node* Mul(RawMachineAssembler& m, MachineType type, Node* a, Node* b) {
  switch (type.representation()) {
    case MachineRepresentation::kWord32:
      return m.Int32Mul(a, b);
    case MachineRepresentation::kWord64:
      return m.Int64Mul(a, b);
    case MachineRepresentation::kFloat32:
      return m.Float32Mul(a, b);
    case MachineRepresentation::kFloat64:
      return m.Float64Mul(a, b);
    default:
      UNREACHABLE();
  }
}

Node* ToInt32(RawMachineAssembler& m, MachineType type, Node* a) {
  switch (type.representation()) {
    case MachineRepresentation::kWord32:
      return a;
    case MachineRepresentation::kWord64:
      return m.TruncateInt64ToInt32(a);
    case MachineRepresentation::kFloat32:
      return m.TruncateFloat32ToInt32(a);
    case MachineRepresentation::kFloat64:
      return m.RoundFloat64ToInt32(a);
    default:
      UNREACHABLE();
  }
}

void TestReturnMultipleValues(MachineType type) {
  const int kMaxCount = 20;
  for (int count = 0; count < kMaxCount; ++count) {
    printf("\n==== type = %s, count = %d ====\n\n\n",
           MachineReprToString(type.representation()), count);
    v8::internal::AccountingAllocator allocator;
    Zone zone(&allocator, ZONE_NAME);
    CallDescriptor* desc = CreateMonoCallDescriptor(&zone, count, 2, type);
    HandleAndZoneScope handles;
    RawMachineAssembler m(handles.main_isolate(),
                          new (handles.main_zone()) Graph(handles.main_zone()),
                          desc, MachineType::PointerRepresentation(),
                          InstructionSelector::SupportedMachineOperatorFlags());

    Node* p0 = m.Parameter(0);
    Node* p1 = m.Parameter(1);
    typedef Node* Node_ptr;
    std::unique_ptr<Node_ptr[]> returns(new Node_ptr[count]);
    for (int i = 0; i < count; ++i) {
      if (i % 3 == 0) returns[i] = Add(m, type, p0, p1);
      if (i % 3 == 1) returns[i] = Sub(m, type, p0, p1);
      if (i % 3 == 2) returns[i] = Mul(m, type, p0, p1);
    }
    m.Return(count, returns.get());

    CompilationInfo info(ArrayVector("testing"), handles.main_zone(),
                         Code::STUB);
    Handle<Code> code = Pipeline::GenerateCodeForTesting(
        &info, handles.main_isolate(), desc, m.graph(), m.Export());
#ifdef ENABLE_DISASSEMBLER
    if (FLAG_print_code) {
      OFStream os(stdout);
      code->Disassemble("multi_value", os);
    }
#endif

    const int a = 47, b = 12;
    int expect = 0;
    for (int i = 0, sign = +1; i < count; ++i) {
      if (i % 3 == 0) expect += sign * (a + b);
      if (i % 3 == 1) expect += sign * (a - b);
      if (i % 3 == 2) expect += sign * (a * b);
      if (i % 4 == 0) sign = -sign;
    }

    RawMachineAssemblerTester<int32_t> mt;
    Node* na = Constant(mt, type, a);
    Node* nb = Constant(mt, type, b);
    Node* ret_multi =
        mt.AddNode(mt.common()->Call(desc), mt.HeapConstant(code), na, nb);
    Node* ret = Constant(mt, type, 0);
    bool sign = false;
    for (int i = 0; i < count; ++i) {
      Node* x = (count == 1)
                    ? ret_multi
                    : mt.AddNode(mt.common()->Projection(i), ret_multi);
      ret = sign ? Sub(mt, type, ret, x) : Add(mt, type, ret, x);
      if (i % 4 == 0) sign = !sign;
    }
    mt.Return(ToInt32(mt, type, ret));
#ifdef ENABLE_DISASSEMBLER
    Handle<Code> code2 = mt.GetCode();
    if (FLAG_print_code) {
      OFStream os(stdout);
      code2->Disassemble("multi_value_call", os);
    }
#endif
    CHECK_EQ(expect, mt.Call());
  }
}

#define TEST_MULTI(Type, type) \
  TEST(ReturnMultiple##Type) { TestReturnMultipleValues(type); }

TEST_MULTI(Int32, MachineType::Int32())
#if (!V8_TARGET_ARCH_32_BIT)
TEST_MULTI(Int64, MachineType::Int64())
#endif
TEST_MULTI(Float32, MachineType::Float32())
TEST_MULTI(Float64, MachineType::Float64())

#undef TEST_MULTI

void ReturnLastValue(MachineType type) {
  int slot_counts[] = {1, 2, 3, 600};
  for (auto slot_count : slot_counts) {
    v8::internal::AccountingAllocator allocator;
    Zone zone(&allocator, ZONE_NAME);
    const int return_count = num_registers(type) + slot_count;

    CallDescriptor* desc =
        CreateMonoCallDescriptor(&zone, return_count, 0, type);

    HandleAndZoneScope handles;
    RawMachineAssembler m(handles.main_isolate(),
                          new (handles.main_zone()) Graph(handles.main_zone()),
                          desc, MachineType::PointerRepresentation(),
                          InstructionSelector::SupportedMachineOperatorFlags());

    std::unique_ptr<Node* []> returns(new Node*[return_count]);

    for (int i = 0; i < return_count; ++i) {
      returns[i] = Constant(m, type, i);
    }

    m.Return(return_count, returns.get());

    CompilationInfo info(ArrayVector("testing"), handles.main_zone(),
                         Code::STUB);
    Handle<Code> code = Pipeline::GenerateCodeForTesting(
        &info, handles.main_isolate(), desc, m.graph(), m.Export());

    // Generate caller.
    int expect = return_count - 1;
    RawMachineAssemblerTester<int32_t> mt;
    Node* code_node = mt.HeapConstant(code);

    Node* call = mt.AddNode(mt.common()->Call(desc), 1, &code_node);

    mt.Return(ToInt32(
        mt, type, mt.AddNode(mt.common()->Projection(return_count - 1), call)));

    CHECK_EQ(expect, mt.Call());
  }
}

TEST(ReturnLastValueInt32) { ReturnLastValue(MachineType::Int32()); }
#if (!V8_TARGET_ARCH_32_BIT)
TEST(ReturnLastValueInt64) { ReturnLastValue(MachineType::Int64()); }
#endif
TEST(ReturnLastValueFloat32) { ReturnLastValue(MachineType::Float32()); }
TEST(ReturnLastValueFloat64) { ReturnLastValue(MachineType::Float64()); }

void ReturnSumOfReturns(MachineType type) {
  for (int unused_stack_slots = 0; unused_stack_slots <= 2;
       ++unused_stack_slots) {
    v8::internal::AccountingAllocator allocator;
    Zone zone(&allocator, ZONE_NAME);
    // Let {unused_stack_slots + 1} returns be on the stack.
    const int return_count = num_registers(type) + unused_stack_slots + 1;

    CallDescriptor* desc =
        CreateMonoCallDescriptor(&zone, return_count, 0, type);

    HandleAndZoneScope handles;
    RawMachineAssembler m(handles.main_isolate(),
                          new (handles.main_zone()) Graph(handles.main_zone()),
                          desc, MachineType::PointerRepresentation(),
                          InstructionSelector::SupportedMachineOperatorFlags());

    std::unique_ptr<Node* []> returns(new Node*[return_count]);

    for (int i = 0; i < return_count; ++i) {
      returns[i] = Constant(m, type, i);
    }

    m.Return(return_count, returns.get());

    CompilationInfo info(ArrayVector("testing"), handles.main_zone(),
                         Code::STUB);
    Handle<Code> code = Pipeline::GenerateCodeForTesting(
        &info, handles.main_isolate(), desc, m.graph(), m.Export());

    // Generate caller.
    RawMachineAssemblerTester<int32_t> mt;
    Node* code_node = mt.HeapConstant(code);

    Node* call = mt.AddNode(mt.common()->Call(desc), 1, &code_node);

    uint32_t expect = 0;
    Node* result = mt.Int32Constant(0);

    for (int i = 0; i < return_count; ++i) {
      expect += i;
      result = mt.Int32Add(
          result,
          ToInt32(mt, type, mt.AddNode(mt.common()->Projection(i), call)));
    }

    mt.Return(result);

    CHECK_EQ(expect, mt.Call());
  }
}

TEST(ReturnSumOfReturnsInt32) { ReturnSumOfReturns(MachineType::Int32()); }
#if (!V8_TARGET_ARCH_32_BIT)
TEST(ReturnSumOfReturnsInt64) { ReturnSumOfReturns(MachineType::Int64()); }
#endif
TEST(ReturnSumOfReturnsFloat32) { ReturnSumOfReturns(MachineType::Float32()); }
TEST(ReturnSumOfReturnsFloat64) { ReturnSumOfReturns(MachineType::Float64()); }

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