blob: 6722f59d600042ababd50964804ef60ec217d644 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005#include "src/code-stubs.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006#include "src/compiler.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007#include "src/parsing/parser.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/zone.h"
9
10#include "src/compiler/common-operator.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/compiler/graph.h"
12#include "src/compiler/linkage.h"
13#include "src/compiler/machine-operator.h"
14#include "src/compiler/node.h"
15#include "src/compiler/operator.h"
16#include "src/compiler/pipeline.h"
17#include "src/compiler/schedule.h"
18#include "test/cctest/cctest.h"
19
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000020namespace v8 {
21namespace internal {
22namespace compiler {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023
Emily Bernierd0a1eb72015-03-24 16:35:39 -040024static Operator dummy_operator(IrOpcode::kParameter, Operator::kNoWrite,
25 "dummy", 0, 0, 0, 0, 0, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000026
27// So we can get a real JS function.
28static Handle<JSFunction> Compile(const char* source) {
29 Isolate* isolate = CcTest::i_isolate();
30 Handle<String> source_code = isolate->factory()
31 ->NewStringFromUtf8(CStrVector(source))
32 .ToHandleChecked();
33 Handle<SharedFunctionInfo> shared_function = Compiler::CompileScript(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034 source_code, Handle<String>(), 0, 0, v8::ScriptOriginOptions(),
35 Handle<Object>(), Handle<Context>(isolate->native_context()), NULL, NULL,
36 v8::ScriptCompiler::kNoCompileOptions, NOT_NATIVES_CODE, false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037 return isolate->factory()->NewFunctionFromSharedFunctionInfo(
38 shared_function, isolate->native_context());
39}
40
41
42TEST(TestLinkageCreate) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043 HandleAndZoneScope handles;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044 Handle<JSFunction> function = Compile("a + b");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045 ParseInfo parse_info(handles.main_zone(), function);
46 CompilationInfo info(&parse_info);
47 CallDescriptor* descriptor = Linkage::ComputeIncoming(info.zone(), &info);
48 CHECK(descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049}
50
51
52TEST(TestLinkageJSFunctionIncoming) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053 const char* sources[] = {"(function() { })", "(function(a) { })",
54 "(function(a,b) { })", "(function(a,b,c) { })"};
55
56 for (int i = 0; i < 3; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000057 HandleAndZoneScope handles;
58 Handle<JSFunction> function =
59 Handle<JSFunction>::cast(v8::Utils::OpenHandle(
60 *v8::Local<v8::Function>::Cast(CompileRun(sources[i]))));
61 ParseInfo parse_info(handles.main_zone(), function);
62 CompilationInfo info(&parse_info);
63 CallDescriptor* descriptor = Linkage::ComputeIncoming(info.zone(), &info);
64 CHECK(descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065
Emily Bernierd0a1eb72015-03-24 16:35:39 -040066 CHECK_EQ(1 + i, static_cast<int>(descriptor->JSParameterCount()));
67 CHECK_EQ(1, static_cast<int>(descriptor->ReturnCount()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068 CHECK_EQ(Operator::kNoProperties, descriptor->properties());
69 CHECK_EQ(true, descriptor->IsJSFunctionCall());
70 }
71}
72
73
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074TEST(TestLinkageJSCall) {
75 HandleAndZoneScope handles;
76 Handle<JSFunction> function = Compile("a + c");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000077 ParseInfo parse_info(handles.main_zone(), function);
78 CompilationInfo info(&parse_info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079
80 for (int i = 0; i < 32; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081 CallDescriptor* descriptor = Linkage::GetJSCallDescriptor(
82 info.zone(), false, i, CallDescriptor::kNoFlags);
83 CHECK(descriptor);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040084 CHECK_EQ(i, static_cast<int>(descriptor->JSParameterCount()));
85 CHECK_EQ(1, static_cast<int>(descriptor->ReturnCount()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 CHECK_EQ(Operator::kNoProperties, descriptor->properties());
87 CHECK_EQ(true, descriptor->IsJSFunctionCall());
88 }
89}
90
91
92TEST(TestLinkageRuntimeCall) {
93 // TODO(titzer): test linkage creation for outgoing runtime calls.
94}
95
96
97TEST(TestLinkageStubCall) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010098 Isolate* isolate = CcTest::InitIsolateOnce();
99 Zone zone;
100 ToNumberStub stub(isolate);
101 CompilationInfo info("test", isolate, &zone, Code::ComputeFlags(Code::STUB));
102 CallInterfaceDescriptor interface_descriptor =
103 stub.GetCallInterfaceDescriptor();
104 CallDescriptor* descriptor = Linkage::GetStubCallDescriptor(
105 isolate, &zone, interface_descriptor, stub.GetStackParameterCount(),
106 CallDescriptor::kNoFlags, Operator::kNoProperties);
107 CHECK(descriptor);
108 CHECK_EQ(0, static_cast<int>(descriptor->StackParameterCount()));
109 CHECK_EQ(1, static_cast<int>(descriptor->ReturnCount()));
110 CHECK_EQ(Operator::kNoProperties, descriptor->properties());
111 CHECK_EQ(false, descriptor->IsJSFunctionCall());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112 // TODO(titzer): test linkage creation for outgoing stub calls.
113}
114
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000115} // namespace compiler
116} // namespace internal
117} // namespace v8