summaryrefslogtreecommitdiff
path: root/deps/v8/test/cctest/compiler/test-run-stubs.cc
blob: 607efa135b9cc1975e680d5d9a15b4ff74a32c64 (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
// Copyright 2015 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 "src/bootstrapper.h"
#include "src/code-stubs.h"
#include "src/compiler/common-operator.h"
#include "src/compiler/graph.h"
#include "src/compiler/js-graph.h"
#include "src/compiler/js-operator.h"
#include "src/compiler/linkage.h"
#include "src/compiler/machine-operator.h"
#include "src/compiler/pipeline.h"
#include "src/parser.h"
#include "test/cctest/compiler/function-tester.h"

using namespace v8::internal;
using namespace v8::internal::compiler;


TEST(RunOptimizedMathFloorStub) {
  HandleAndZoneScope scope;
  Isolate* isolate = scope.main_isolate();

  // Create code and an accompanying descriptor.
  MathFloorStub stub(isolate, TurboFanIC::CALL_FROM_OPTIMIZED_CODE);
  Handle<Code> code = stub.GenerateCode();
  Zone* zone = scope.main_zone();
  CompilationInfo info(&stub, isolate, zone);
  CallDescriptor* descriptor = Linkage::ComputeIncoming(zone, &info);
  Handle<FixedArray> tv = isolate->factory()->NewFixedArray(10);

  // Create a function to call the code using the descriptor.
  Graph graph(zone);
  CommonOperatorBuilder common(zone);
  JSOperatorBuilder javascript(zone);
  MachineOperatorBuilder machine(zone);
  JSGraph js(isolate, &graph, &common, &javascript, &machine);

  // FunctionTester (ab)uses a 2-argument function
  Node* start = graph.NewNode(common.Start(4));
  // Parameter 0 is the number to round
  Node* numberParam = graph.NewNode(common.Parameter(1), start);
  Unique<HeapObject> u = Unique<HeapObject>::CreateImmovable(code);
  Node* theCode = graph.NewNode(common.HeapConstant(u));
  Unique<HeapObject> tvu = Unique<HeapObject>::CreateImmovable(tv);
  Node* vector = graph.NewNode(common.HeapConstant(tvu));
  Node* dummyContext = graph.NewNode(common.NumberConstant(0.0));
  Node* call =
      graph.NewNode(common.Call(descriptor), theCode, js.UndefinedConstant(),
                    js.OneConstant(), vector, js.UndefinedConstant(),
                    numberParam, dummyContext, start, start);
  Node* ret = graph.NewNode(common.Return(), call, call, start);
  Node* end = graph.NewNode(common.End(1), ret);
  graph.SetStart(start);
  graph.SetEnd(end);
  FunctionTester ft(&graph);

  Handle<Object> value = ft.Val(1.5);
  Handle<Object> result = ft.Call(value, value).ToHandleChecked();
  CHECK_EQ(1, Smi::cast(*result)->value());
}


TEST(RunStringLengthTFStub) {
  HandleAndZoneScope scope;
  Isolate* isolate = scope.main_isolate();
  Zone* zone = scope.main_zone();

  // Create code and an accompanying descriptor.
  StringLengthTFStub stub(isolate);
  Handle<Code> code = stub.GenerateCode();
  CompilationInfo info(&stub, isolate, zone);
  CallDescriptor* descriptor = Linkage::ComputeIncoming(zone, &info);

  // Create a function to call the code using the descriptor.
  Graph graph(zone);
  CommonOperatorBuilder common(zone);
  // FunctionTester (ab)uses a 4-argument function
  Node* start = graph.NewNode(common.Start(6));
  // Parameter 0 is the receiver
  Node* receiverParam = graph.NewNode(common.Parameter(1), start);
  Node* nameParam = graph.NewNode(common.Parameter(2), start);
  Node* slotParam = graph.NewNode(common.Parameter(3), start);
  Node* vectorParam = graph.NewNode(common.Parameter(4), start);
  Unique<HeapObject> u = Unique<HeapObject>::CreateImmovable(code);
  Node* theCode = graph.NewNode(common.HeapConstant(u));
  Node* dummyContext = graph.NewNode(common.NumberConstant(0.0));
  Node* call =
      graph.NewNode(common.Call(descriptor), theCode, receiverParam, nameParam,
                    slotParam, vectorParam, dummyContext, start, start);
  Node* ret = graph.NewNode(common.Return(), call, call, start);
  Node* end = graph.NewNode(common.End(1), ret);
  graph.SetStart(start);
  graph.SetEnd(end);
  FunctionTester ft(&graph);

  // Actuall call through to the stub, verifying its result.
  const char* testString = "Und das Lamm schrie HURZ!";
  Handle<JSReceiver> receiverArg =
      Object::ToObject(isolate, ft.Val(testString)).ToHandleChecked();
  Handle<String> nameArg = ft.Val("length");
  Handle<Object> slot = ft.Val(0.0);
  Handle<Object> vector = ft.Val(0.0);
  Handle<Object> result =
      ft.Call(receiverArg, nameArg, slot, vector).ToHandleChecked();
  CHECK_EQ(static_cast<int>(strlen(testString)), Smi::cast(*result)->value());
}


TEST(RunStringAddTFStub) {
  HandleAndZoneScope scope;
  Isolate* isolate = scope.main_isolate();
  Zone* zone = scope.main_zone();

  // Create code and an accompanying descriptor.
  StringAddTFStub stub(isolate, STRING_ADD_CHECK_BOTH, NOT_TENURED);
  Handle<Code> code = stub.GenerateCode();
  CompilationInfo info(&stub, isolate, zone);
  CallDescriptor* descriptor = Linkage::ComputeIncoming(zone, &info);

  // Create a function to call the code using the descriptor.
  Graph graph(zone);
  CommonOperatorBuilder common(zone);
  // FunctionTester (ab)uses a 2-argument function
  Node* start = graph.NewNode(common.Start(4));
  // Parameter 0 is the receiver
  Node* leftParam = graph.NewNode(common.Parameter(1), start);
  Node* rightParam = graph.NewNode(common.Parameter(2), start);
  Unique<HeapObject> u = Unique<HeapObject>::CreateImmovable(code);
  Node* theCode = graph.NewNode(common.HeapConstant(u));
  Node* dummyContext = graph.NewNode(common.NumberConstant(0.0));
  Node* call = graph.NewNode(common.Call(descriptor), theCode, leftParam,
                             rightParam, dummyContext, start, start);
  Node* ret = graph.NewNode(common.Return(), call, call, start);
  Node* end = graph.NewNode(common.End(1), ret);
  graph.SetStart(start);
  graph.SetEnd(end);
  FunctionTester ft(&graph);

  // Actuall call through to the stub, verifying its result.
  Handle<String> leftArg = ft.Val("links");
  Handle<String> rightArg = ft.Val("rechts");
  Handle<Object> result = ft.Call(leftArg, rightArg).ToHandleChecked();
  CHECK(String::Equals(ft.Val("linksrechts"), Handle<String>::cast(result)));
}