Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // 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 | |
| 20 | namespace v8 { |
| 21 | namespace internal { |
| 22 | namespace compiler { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | CallDescriptor* 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); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 30 | const RegisterConfiguration* config = RegisterConfiguration::Turbofan(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 31 | |
| 32 | // Add return location(s). |
| 33 | CHECK(return_count <= config->num_allocatable_general_registers()); |
| 34 | for (int i = 0; i < return_count; i++) { |
| 35 | msig.AddReturn(MachineType::Int32()); |
| 36 | locations.AddReturn( |
| 37 | LinkageLocation::ForRegister(config->allocatable_general_codes()[i])); |
| 38 | } |
| 39 | |
| 40 | // Add register and/or stack parameter(s). |
| 41 | CHECK(param_count <= config->num_allocatable_general_registers()); |
| 42 | for (int i = 0; i < param_count; i++) { |
| 43 | msig.AddParam(MachineType::Int32()); |
| 44 | locations.AddParam( |
| 45 | LinkageLocation::ForRegister(config->allocatable_general_codes()[i])); |
| 46 | } |
| 47 | |
| 48 | const RegList kCalleeSaveRegisters = 0; |
| 49 | const RegList kCalleeSaveFPRegisters = 0; |
| 50 | |
| 51 | // The target for WASM calls is always a code object. |
| 52 | MachineType target_type = MachineType::AnyTagged(); |
| 53 | LinkageLocation target_loc = LinkageLocation::ForAnyRegister(); |
| 54 | return new (zone) CallDescriptor( // -- |
| 55 | CallDescriptor::kCallCodeObject, // kind |
| 56 | target_type, // target MachineType |
| 57 | target_loc, // target location |
| 58 | msig.Build(), // machine_sig |
| 59 | locations.Build(), // location_sig |
| 60 | 0, // js_parameter_count |
| 61 | compiler::Operator::kNoProperties, // properties |
| 62 | kCalleeSaveRegisters, // callee-saved registers |
| 63 | kCalleeSaveFPRegisters, // callee-saved fp regs |
| 64 | CallDescriptor::kNoFlags, // flags |
| 65 | "c-call"); |
| 66 | } |
| 67 | } // namespace |
| 68 | |
| 69 | |
| 70 | TEST(ReturnThreeValues) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 71 | base::AccountingAllocator allocator; |
| 72 | Zone zone(&allocator); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 73 | CallDescriptor* desc = GetCallDescriptor(&zone, 3, 2); |
| 74 | HandleAndZoneScope handles; |
| 75 | RawMachineAssembler m(handles.main_isolate(), |
| 76 | new (handles.main_zone()) Graph(handles.main_zone()), |
| 77 | desc, MachineType::PointerRepresentation(), |
| 78 | InstructionSelector::SupportedMachineOperatorFlags()); |
| 79 | |
| 80 | Node* p0 = m.Parameter(0); |
| 81 | Node* p1 = m.Parameter(1); |
| 82 | Node* add = m.Int32Add(p0, p1); |
| 83 | Node* sub = m.Int32Sub(p0, p1); |
| 84 | Node* mul = m.Int32Mul(p0, p1); |
| 85 | m.Return(add, sub, mul); |
| 86 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 87 | CompilationInfo info(ArrayVector("testing"), handles.main_isolate(), |
| 88 | handles.main_zone()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 89 | Handle<Code> code = |
| 90 | Pipeline::GenerateCodeForTesting(&info, desc, m.graph(), m.Export()); |
| 91 | #ifdef ENABLE_DISASSEMBLER |
| 92 | if (FLAG_print_code) { |
| 93 | OFStream os(stdout); |
| 94 | code->Disassemble("three_value", os); |
| 95 | } |
| 96 | #endif |
| 97 | |
| 98 | RawMachineAssemblerTester<int32_t> mt; |
| 99 | Node* a = mt.Int32Constant(123); |
| 100 | Node* b = mt.Int32Constant(456); |
| 101 | Node* ret3 = mt.AddNode(mt.common()->Call(desc), mt.HeapConstant(code), a, b); |
| 102 | Node* x = mt.AddNode(mt.common()->Projection(0), ret3); |
| 103 | Node* y = mt.AddNode(mt.common()->Projection(1), ret3); |
| 104 | Node* z = mt.AddNode(mt.common()->Projection(2), ret3); |
| 105 | Node* ret = mt.Int32Add(mt.Int32Add(x, y), z); |
| 106 | mt.Return(ret); |
| 107 | #ifdef ENABLE_DISASSEMBLER |
| 108 | Handle<Code> code2 = mt.GetCode(); |
| 109 | if (FLAG_print_code) { |
| 110 | OFStream os(stdout); |
| 111 | code2->Disassemble("three_value_call", os); |
| 112 | } |
| 113 | #endif |
| 114 | CHECK_EQ((123 + 456) + (123 - 456) + (123 * 456), mt.Call()); |
| 115 | } |
| 116 | |
| 117 | } // namespace compiler |
| 118 | } // namespace internal |
| 119 | } // namespace v8 |