blob: 53bae5e0f37a6012964b2cef4417d1ee51cfee96 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <cmath>
6#include <functional>
7#include <limits>
8
9#include "src/assembler.h"
10#include "src/base/bits.h"
11#include "src/base/utils/random-number-generator.h"
12#include "src/codegen.h"
13#include "src/compiler.h"
14#include "src/compiler/linkage.h"
15#include "src/macro-assembler.h"
16#include "test/cctest/cctest.h"
17#include "test/cctest/compiler/codegen-tester.h"
18#include "test/cctest/compiler/value-helper.h"
19
20namespace v8 {
21namespace internal {
22namespace compiler {
23
24namespace {
25
26CallDescriptor* GetCallDescriptor(Zone* zone, int return_count,
27 int param_count) {
28 MachineSignature::Builder msig(zone, return_count, param_count);
29 LocationSignature::Builder locations(zone, return_count, param_count);
30 const RegisterConfiguration* config =
31 RegisterConfiguration::ArchDefault(RegisterConfiguration::TURBOFAN);
32
33 // Add return location(s).
34 CHECK(return_count <= config->num_allocatable_general_registers());
35 for (int i = 0; i < return_count; i++) {
36 msig.AddReturn(MachineType::Int32());
37 locations.AddReturn(
38 LinkageLocation::ForRegister(config->allocatable_general_codes()[i]));
39 }
40
41 // Add register and/or stack parameter(s).
42 CHECK(param_count <= config->num_allocatable_general_registers());
43 for (int i = 0; i < param_count; i++) {
44 msig.AddParam(MachineType::Int32());
45 locations.AddParam(
46 LinkageLocation::ForRegister(config->allocatable_general_codes()[i]));
47 }
48
49 const RegList kCalleeSaveRegisters = 0;
50 const RegList kCalleeSaveFPRegisters = 0;
51
52 // The target for WASM calls is always a code object.
53 MachineType target_type = MachineType::AnyTagged();
54 LinkageLocation target_loc = LinkageLocation::ForAnyRegister();
55 return new (zone) CallDescriptor( // --
56 CallDescriptor::kCallCodeObject, // kind
57 target_type, // target MachineType
58 target_loc, // target location
59 msig.Build(), // machine_sig
60 locations.Build(), // location_sig
61 0, // js_parameter_count
62 compiler::Operator::kNoProperties, // properties
63 kCalleeSaveRegisters, // callee-saved registers
64 kCalleeSaveFPRegisters, // callee-saved fp regs
65 CallDescriptor::kNoFlags, // flags
66 "c-call");
67}
68} // namespace
69
70
71TEST(ReturnThreeValues) {
Ben Murdochda12d292016-06-02 14:46:10 +010072 base::AccountingAllocator allocator;
73 Zone zone(&allocator);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074 CallDescriptor* desc = GetCallDescriptor(&zone, 3, 2);
75 HandleAndZoneScope handles;
76 RawMachineAssembler m(handles.main_isolate(),
77 new (handles.main_zone()) Graph(handles.main_zone()),
78 desc, MachineType::PointerRepresentation(),
79 InstructionSelector::SupportedMachineOperatorFlags());
80
81 Node* p0 = m.Parameter(0);
82 Node* p1 = m.Parameter(1);
83 Node* add = m.Int32Add(p0, p1);
84 Node* sub = m.Int32Sub(p0, p1);
85 Node* mul = m.Int32Mul(p0, p1);
86 m.Return(add, sub, mul);
87
Ben Murdochc5610432016-08-08 18:44:38 +010088 CompilationInfo info(ArrayVector("testing"), handles.main_isolate(),
89 handles.main_zone());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000090 Handle<Code> code =
91 Pipeline::GenerateCodeForTesting(&info, desc, m.graph(), m.Export());
92#ifdef ENABLE_DISASSEMBLER
93 if (FLAG_print_code) {
94 OFStream os(stdout);
95 code->Disassemble("three_value", os);
96 }
97#endif
98
99 RawMachineAssemblerTester<int32_t> mt;
100 Node* a = mt.Int32Constant(123);
101 Node* b = mt.Int32Constant(456);
102 Node* ret3 = mt.AddNode(mt.common()->Call(desc), mt.HeapConstant(code), a, b);
103 Node* x = mt.AddNode(mt.common()->Projection(0), ret3);
104 Node* y = mt.AddNode(mt.common()->Projection(1), ret3);
105 Node* z = mt.AddNode(mt.common()->Projection(2), ret3);
106 Node* ret = mt.Int32Add(mt.Int32Add(x, y), z);
107 mt.Return(ret);
108#ifdef ENABLE_DISASSEMBLER
109 Handle<Code> code2 = mt.GetCode();
110 if (FLAG_print_code) {
111 OFStream os(stdout);
112 code2->Disassemble("three_value_call", os);
113 }
114#endif
115 CHECK_EQ((123 + 456) + (123 - 456) + (123 * 456), mt.Call());
116}
117
118} // namespace compiler
119} // namespace internal
120} // namespace v8