blob: 436b0f3d1072d20f9a5107e3062c688af6de681e [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
Ben Murdoch61f157c2016-09-16 13:49:30 +010010#include "src/code-factory.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/compiler/common-operator.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012#include "src/compiler/graph.h"
13#include "src/compiler/linkage.h"
14#include "src/compiler/machine-operator.h"
15#include "src/compiler/node.h"
16#include "src/compiler/operator.h"
17#include "src/compiler/pipeline.h"
18#include "src/compiler/schedule.h"
19#include "test/cctest/cctest.h"
20
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000021namespace v8 {
22namespace internal {
23namespace compiler {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000024
Emily Bernierd0a1eb72015-03-24 16:35:39 -040025static Operator dummy_operator(IrOpcode::kParameter, Operator::kNoWrite,
26 "dummy", 0, 0, 0, 0, 0, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027
28// So we can get a real JS function.
29static Handle<JSFunction> Compile(const char* source) {
30 Isolate* isolate = CcTest::i_isolate();
31 Handle<String> source_code = isolate->factory()
32 ->NewStringFromUtf8(CStrVector(source))
33 .ToHandleChecked();
Ben Murdochda12d292016-06-02 14:46:10 +010034 Handle<SharedFunctionInfo> shared = Compiler::GetSharedFunctionInfoForScript(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035 source_code, Handle<String>(), 0, 0, v8::ScriptOriginOptions(),
36 Handle<Object>(), Handle<Context>(isolate->native_context()), NULL, NULL,
37 v8::ScriptCompiler::kNoCompileOptions, NOT_NATIVES_CODE, false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038 return isolate->factory()->NewFunctionFromSharedFunctionInfo(
Ben Murdochda12d292016-06-02 14:46:10 +010039 shared, isolate->native_context());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040}
41
42
43TEST(TestLinkageCreate) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044 HandleAndZoneScope handles;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045 Handle<JSFunction> function = Compile("a + b");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046 ParseInfo parse_info(handles.main_zone(), function);
Ben Murdochc5610432016-08-08 18:44:38 +010047 CompilationInfo info(&parse_info, function);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000048 CallDescriptor* descriptor = Linkage::ComputeIncoming(info.zone(), &info);
49 CHECK(descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050}
51
52
53TEST(TestLinkageJSFunctionIncoming) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054 const char* sources[] = {"(function() { })", "(function(a) { })",
55 "(function(a,b) { })", "(function(a,b,c) { })"};
56
57 for (int i = 0; i < 3; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000058 HandleAndZoneScope handles;
59 Handle<JSFunction> function =
60 Handle<JSFunction>::cast(v8::Utils::OpenHandle(
61 *v8::Local<v8::Function>::Cast(CompileRun(sources[i]))));
62 ParseInfo parse_info(handles.main_zone(), function);
Ben Murdochc5610432016-08-08 18:44:38 +010063 CompilationInfo info(&parse_info, function);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000064 CallDescriptor* descriptor = Linkage::ComputeIncoming(info.zone(), &info);
65 CHECK(descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066
Emily Bernierd0a1eb72015-03-24 16:35:39 -040067 CHECK_EQ(1 + i, static_cast<int>(descriptor->JSParameterCount()));
68 CHECK_EQ(1, static_cast<int>(descriptor->ReturnCount()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 CHECK_EQ(Operator::kNoProperties, descriptor->properties());
70 CHECK_EQ(true, descriptor->IsJSFunctionCall());
71 }
72}
73
74
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075TEST(TestLinkageJSCall) {
76 HandleAndZoneScope handles;
77 Handle<JSFunction> function = Compile("a + c");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000078 ParseInfo parse_info(handles.main_zone(), function);
Ben Murdochc5610432016-08-08 18:44:38 +010079 CompilationInfo info(&parse_info, function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080
81 for (int i = 0; i < 32; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000082 CallDescriptor* descriptor = Linkage::GetJSCallDescriptor(
83 info.zone(), false, i, CallDescriptor::kNoFlags);
84 CHECK(descriptor);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040085 CHECK_EQ(i, static_cast<int>(descriptor->JSParameterCount()));
86 CHECK_EQ(1, static_cast<int>(descriptor->ReturnCount()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000087 CHECK_EQ(Operator::kNoProperties, descriptor->properties());
88 CHECK_EQ(true, descriptor->IsJSFunctionCall());
89 }
90}
91
92
93TEST(TestLinkageRuntimeCall) {
94 // TODO(titzer): test linkage creation for outgoing runtime calls.
95}
96
97
98TEST(TestLinkageStubCall) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010099 Isolate* isolate = CcTest::InitIsolateOnce();
Ben Murdochda12d292016-06-02 14:46:10 +0100100 Zone zone(isolate->allocator());
Ben Murdoch61f157c2016-09-16 13:49:30 +0100101 Callable callable = CodeFactory::ToNumber(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +0100102 CompilationInfo info(ArrayVector("test"), isolate, &zone,
103 Code::ComputeFlags(Code::STUB));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100104 CallDescriptor* descriptor = Linkage::GetStubCallDescriptor(
Ben Murdoch61f157c2016-09-16 13:49:30 +0100105 isolate, &zone, callable.descriptor(), 0, CallDescriptor::kNoFlags,
106 Operator::kNoProperties);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100107 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