blob: 105bd353fca48095a684c58884172ffb0f0f95b4 [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/ast/scopes.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006#include "src/code-stubs.h"
7#include "src/compiler.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include "src/compiler/common-operator.h"
9#include "src/compiler/frame.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010#include "src/compiler/linkage.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/compiler/node.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012#include "src/compiler/osr.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013#include "src/compiler/pipeline.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014
15namespace v8 {
16namespace internal {
17namespace compiler {
18
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019namespace {
20LinkageLocation regloc(Register reg) {
21 return LinkageLocation::ForRegister(reg.code());
22}
23
24
25MachineType reptyp(Representation representation) {
26 switch (representation.kind()) {
27 case Representation::kInteger8:
28 return MachineType::Int8();
29 case Representation::kUInteger8:
30 return MachineType::Uint8();
31 case Representation::kInteger16:
32 return MachineType::Int16();
33 case Representation::kUInteger16:
34 return MachineType::Uint16();
35 case Representation::kInteger32:
36 return MachineType::Int32();
37 case Representation::kSmi:
38 case Representation::kTagged:
39 case Representation::kHeapObject:
40 return MachineType::AnyTagged();
41 case Representation::kDouble:
42 return MachineType::Float64();
43 case Representation::kExternal:
44 return MachineType::Pointer();
45 case Representation::kNone:
46 case Representation::kNumRepresentations:
47 break;
48 }
49 UNREACHABLE();
50 return MachineType::None();
51}
52} // namespace
53
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054
Emily Bernierd0a1eb72015-03-24 16:35:39 -040055std::ostream& operator<<(std::ostream& os, const CallDescriptor::Kind& k) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056 switch (k) {
57 case CallDescriptor::kCallCodeObject:
58 os << "Code";
59 break;
60 case CallDescriptor::kCallJSFunction:
61 os << "JS";
62 break;
63 case CallDescriptor::kCallAddress:
64 os << "Addr";
65 break;
66 }
67 return os;
68}
69
70
Emily Bernierd0a1eb72015-03-24 16:35:39 -040071std::ostream& operator<<(std::ostream& os, const CallDescriptor& d) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072 // TODO(svenpanne) Output properties etc. and be less cryptic.
73 return os << d.kind() << ":" << d.debug_name() << ":r" << d.ReturnCount()
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074 << "s" << d.StackParameterCount() << "i" << d.InputCount() << "f"
75 << d.FrameStateCount() << "t" << d.SupportsTailCalls();
76}
77
78
79bool CallDescriptor::HasSameReturnLocationsAs(
80 const CallDescriptor* other) const {
81 if (ReturnCount() != other->ReturnCount()) return false;
82 for (size_t i = 0; i < ReturnCount(); ++i) {
83 if (GetReturnLocation(i) != other->GetReturnLocation(i)) return false;
84 }
85 return true;
86}
87
88
89bool CallDescriptor::CanTailCall(const Node* node,
90 int* stack_param_delta) const {
91 CallDescriptor const* other = OpParameter<CallDescriptor const*>(node);
92 size_t current_input = 0;
93 size_t other_input = 0;
94 *stack_param_delta = 0;
95 bool more_other = true;
96 bool more_this = true;
97 while (more_other || more_this) {
98 if (other_input < other->InputCount()) {
99 if (!other->GetInputLocation(other_input).IsRegister()) {
100 (*stack_param_delta)--;
101 }
102 } else {
103 more_other = false;
104 }
105 if (current_input < InputCount()) {
106 if (!GetInputLocation(current_input).IsRegister()) {
107 (*stack_param_delta)++;
108 }
109 } else {
110 more_this = false;
111 }
112 ++current_input;
113 ++other_input;
114 }
115 return HasSameReturnLocationsAs(OpParameter<CallDescriptor const*>(node));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000116}
117
118
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400119CallDescriptor* Linkage::ComputeIncoming(Zone* zone, CompilationInfo* info) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100120 DCHECK(!info->IsStub());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000121 if (info->has_literal()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000122 // If we already have the function literal, use the number of parameters
123 // plus the receiver.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000124 return GetJSCallDescriptor(zone, info->is_osr(),
125 1 + info->literal()->parameter_count(),
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400126 CallDescriptor::kNoFlags);
127 }
128 if (!info->closure().is_null()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 // If we are compiling a JS function, use a JS call descriptor,
130 // plus the receiver.
131 SharedFunctionInfo* shared = info->closure()->shared();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000132 return GetJSCallDescriptor(zone, info->is_osr(),
133 1 + shared->internal_formal_parameter_count(),
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400134 CallDescriptor::kNoFlags);
135 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000136 return nullptr; // TODO(titzer): ?
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137}
138
139
140// static
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000141int Linkage::FrameStateInputCount(Runtime::FunctionId function) {
142 // Most runtime functions need a FrameState. A few chosen ones that we know
143 // not to call into arbitrary JavaScript, not to throw, and not to deoptimize
144 // are blacklisted here and can be called without a FrameState.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145 switch (function) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000146 case Runtime::kAllocateInTargetSpace:
147 case Runtime::kCreateIterResultObject:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100148 case Runtime::kDefineDataPropertyInLiteral:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000149 case Runtime::kDefineGetterPropertyUnchecked: // TODO(jarin): Is it safe?
150 case Runtime::kDefineSetterPropertyUnchecked: // TODO(jarin): Is it safe?
151 case Runtime::kFinalizeClassDefinition: // TODO(conradw): Is it safe?
152 case Runtime::kForInDone:
153 case Runtime::kForInStep:
154 case Runtime::kGetSuperConstructor:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100155 case Runtime::kIsFunction:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000156 case Runtime::kNewClosure:
157 case Runtime::kNewClosure_Tenured:
158 case Runtime::kNewFunctionContext:
159 case Runtime::kPushBlockContext:
160 case Runtime::kPushCatchContext:
161 case Runtime::kReThrow:
162 case Runtime::kStringCompare:
Ben Murdochda12d292016-06-02 14:46:10 +0100163 case Runtime::kStringEqual:
164 case Runtime::kStringNotEqual:
165 case Runtime::kStringLessThan:
166 case Runtime::kStringLessThanOrEqual:
167 case Runtime::kStringGreaterThan:
168 case Runtime::kStringGreaterThanOrEqual:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000169 case Runtime::kTraceEnter:
170 case Runtime::kTraceExit:
171 return 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000172 case Runtime::kInlineGetPrototype:
Ben Murdochda12d292016-06-02 14:46:10 +0100173 case Runtime::kInlineNewObject:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000174 case Runtime::kInlineRegExpConstructResult:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400175 case Runtime::kInlineRegExpExec:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000176 case Runtime::kInlineSubString:
177 case Runtime::kInlineToInteger:
178 case Runtime::kInlineToLength:
179 case Runtime::kInlineToName:
180 case Runtime::kInlineToNumber:
181 case Runtime::kInlineToObject:
Ben Murdochda12d292016-06-02 14:46:10 +0100182 case Runtime::kInlineToPrimitive:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000183 case Runtime::kInlineToPrimitive_Number:
184 case Runtime::kInlineToPrimitive_String:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000185 case Runtime::kInlineToString:
186 return 1;
187 case Runtime::kInlineCall:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000188 case Runtime::kInlineDeoptimizeNow:
189 case Runtime::kInlineThrowNotDateError:
190 return 2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000191 default:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000192 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000193 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000194
195 // Most inlined runtime functions (except the ones listed above) can be called
196 // without a FrameState or will be lowered by JSIntrinsicLowering internally.
197 const Runtime::Function* const f = Runtime::FunctionForId(function);
198 if (f->intrinsic_type == Runtime::IntrinsicType::INLINE) return 0;
199
200 return 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000201}
202
203
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000204bool CallDescriptor::UsesOnlyRegisters() const {
205 for (size_t i = 0; i < InputCount(); ++i) {
206 if (!GetInputLocation(i).IsRegister()) return false;
207 }
208 for (size_t i = 0; i < ReturnCount(); ++i) {
209 if (!GetReturnLocation(i).IsRegister()) return false;
210 }
211 return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000212}
213
214
215CallDescriptor* Linkage::GetRuntimeCallDescriptor(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000216 Zone* zone, Runtime::FunctionId function_id, int js_parameter_count,
217 Operator::Properties properties, CallDescriptor::Flags flags) {
218 const size_t function_count = 1;
219 const size_t num_args_count = 1;
220 const size_t context_count = 1;
221 const size_t parameter_count = function_count +
222 static_cast<size_t>(js_parameter_count) +
223 num_args_count + context_count;
224
225 const Runtime::Function* function = Runtime::FunctionForId(function_id);
226 const size_t return_count = static_cast<size_t>(function->result_size);
227
228 LocationSignature::Builder locations(zone, return_count, parameter_count);
229 MachineSignature::Builder types(zone, return_count, parameter_count);
230
231 // Add returns.
232 if (locations.return_count_ > 0) {
233 locations.AddReturn(regloc(kReturnRegister0));
234 }
235 if (locations.return_count_ > 1) {
236 locations.AddReturn(regloc(kReturnRegister1));
237 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100238 if (locations.return_count_ > 2) {
239 locations.AddReturn(regloc(kReturnRegister2));
240 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000241 for (size_t i = 0; i < return_count; i++) {
242 types.AddReturn(MachineType::AnyTagged());
243 }
244
245 // All parameters to the runtime call go on the stack.
246 for (int i = 0; i < js_parameter_count; i++) {
247 locations.AddParam(
248 LinkageLocation::ForCallerFrameSlot(i - js_parameter_count));
249 types.AddParam(MachineType::AnyTagged());
250 }
251 // Add runtime function itself.
252 locations.AddParam(regloc(kRuntimeCallFunctionRegister));
253 types.AddParam(MachineType::AnyTagged());
254
255 // Add runtime call argument count.
256 locations.AddParam(regloc(kRuntimeCallArgCountRegister));
257 types.AddParam(MachineType::Pointer());
258
259 // Add context.
260 locations.AddParam(regloc(kContextRegister));
261 types.AddParam(MachineType::AnyTagged());
262
263 if (Linkage::FrameStateInputCount(function_id) == 0) {
264 flags = static_cast<CallDescriptor::Flags>(
265 flags & ~CallDescriptor::kNeedsFrameState);
266 }
267
268 // The target for runtime calls is a code object.
269 MachineType target_type = MachineType::AnyTagged();
270 LinkageLocation target_loc = LinkageLocation::ForAnyRegister();
271 return new (zone) CallDescriptor( // --
272 CallDescriptor::kCallCodeObject, // kind
273 target_type, // target MachineType
274 target_loc, // target location
275 types.Build(), // machine_sig
276 locations.Build(), // location_sig
277 js_parameter_count, // stack_parameter_count
278 properties, // properties
279 kNoCalleeSaved, // callee-saved
280 kNoCalleeSaved, // callee-saved fp
281 flags, // flags
282 function->name); // debug name
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000283}
284
285
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000286CallDescriptor* Linkage::GetJSCallDescriptor(Zone* zone, bool is_osr,
287 int js_parameter_count,
288 CallDescriptor::Flags flags) {
289 const size_t return_count = 1;
290 const size_t context_count = 1;
291 const size_t new_target_count = 1;
292 const size_t num_args_count = 1;
293 const size_t parameter_count =
294 js_parameter_count + new_target_count + num_args_count + context_count;
295
296 LocationSignature::Builder locations(zone, return_count, parameter_count);
297 MachineSignature::Builder types(zone, return_count, parameter_count);
298
299 // All JS calls have exactly one return value.
300 locations.AddReturn(regloc(kReturnRegister0));
301 types.AddReturn(MachineType::AnyTagged());
302
303 // All parameters to JS calls go on the stack.
304 for (int i = 0; i < js_parameter_count; i++) {
305 int spill_slot_index = i - js_parameter_count;
306 locations.AddParam(LinkageLocation::ForCallerFrameSlot(spill_slot_index));
307 types.AddParam(MachineType::AnyTagged());
308 }
309
310 // Add JavaScript call new target value.
311 locations.AddParam(regloc(kJavaScriptCallNewTargetRegister));
312 types.AddParam(MachineType::AnyTagged());
313
314 // Add JavaScript call argument count.
315 locations.AddParam(regloc(kJavaScriptCallArgCountRegister));
316 types.AddParam(MachineType::Int32());
317
318 // Add context.
319 locations.AddParam(regloc(kContextRegister));
320 types.AddParam(MachineType::AnyTagged());
321
322 // The target for JS function calls is the JSFunction object.
323 MachineType target_type = MachineType::AnyTagged();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100324 // When entering into an OSR function from unoptimized code the JSFunction
325 // is not in a register, but it is on the stack in the marker spill slot.
Ben Murdochda12d292016-06-02 14:46:10 +0100326 LinkageLocation target_loc = is_osr
327 ? LinkageLocation::ForSavedCallerFunction()
328 : regloc(kJSFunctionRegister);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000329 return new (zone) CallDescriptor( // --
330 CallDescriptor::kCallJSFunction, // kind
331 target_type, // target MachineType
332 target_loc, // target location
333 types.Build(), // machine_sig
334 locations.Build(), // location_sig
335 js_parameter_count, // stack_parameter_count
336 Operator::kNoProperties, // properties
337 kNoCalleeSaved, // callee-saved
338 kNoCalleeSaved, // callee-saved fp
339 CallDescriptor::kCanUseRoots | // flags
340 flags, // flags
341 "js-call");
342}
343
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000344// TODO(all): Add support for return representations/locations to
345// CallInterfaceDescriptor.
346// TODO(turbofan): cache call descriptors for code stub calls.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000347CallDescriptor* Linkage::GetStubCallDescriptor(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000348 Isolate* isolate, Zone* zone, const CallInterfaceDescriptor& descriptor,
349 int stack_parameter_count, CallDescriptor::Flags flags,
350 Operator::Properties properties, MachineType return_type,
351 size_t return_count) {
352 const int register_parameter_count = descriptor.GetRegisterParameterCount();
353 const int js_parameter_count =
354 register_parameter_count + stack_parameter_count;
355 const int context_count = 1;
356 const size_t parameter_count =
357 static_cast<size_t>(js_parameter_count + context_count);
358
359 LocationSignature::Builder locations(zone, return_count, parameter_count);
360 MachineSignature::Builder types(zone, return_count, parameter_count);
361
362 // Add returns.
363 if (locations.return_count_ > 0) {
364 locations.AddReturn(regloc(kReturnRegister0));
365 }
366 if (locations.return_count_ > 1) {
367 locations.AddReturn(regloc(kReturnRegister1));
368 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100369 if (locations.return_count_ > 2) {
370 locations.AddReturn(regloc(kReturnRegister2));
371 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000372 for (size_t i = 0; i < return_count; i++) {
373 types.AddReturn(return_type);
374 }
375
376 // Add parameters in registers and on the stack.
377 for (int i = 0; i < js_parameter_count; i++) {
378 if (i < register_parameter_count) {
379 // The first parameters go in registers.
380 Register reg = descriptor.GetRegisterParameter(i);
381 Representation rep =
382 RepresentationFromType(descriptor.GetParameterType(i));
383 locations.AddParam(regloc(reg));
384 types.AddParam(reptyp(rep));
385 } else {
386 // The rest of the parameters go on the stack.
387 int stack_slot = i - register_parameter_count - stack_parameter_count;
388 locations.AddParam(LinkageLocation::ForCallerFrameSlot(stack_slot));
389 types.AddParam(MachineType::AnyTagged());
390 }
391 }
392 // Add context.
393 locations.AddParam(regloc(kContextRegister));
394 types.AddParam(MachineType::AnyTagged());
395
396 // The target for stub calls is a code object.
397 MachineType target_type = MachineType::AnyTagged();
398 LinkageLocation target_loc = LinkageLocation::ForAnyRegister();
399 return new (zone) CallDescriptor( // --
400 CallDescriptor::kCallCodeObject, // kind
401 target_type, // target MachineType
402 target_loc, // target location
403 types.Build(), // machine_sig
404 locations.Build(), // location_sig
405 stack_parameter_count, // stack_parameter_count
406 properties, // properties
407 kNoCalleeSaved, // callee-saved registers
408 kNoCalleeSaved, // callee-saved fp
Ben Murdochda12d292016-06-02 14:46:10 +0100409 CallDescriptor::kCanUseRoots | // flags
410 flags, // flags
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000411 descriptor.DebugName(isolate));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000412}
413
414
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000415LinkageLocation Linkage::GetOsrValueLocation(int index) const {
416 CHECK(incoming_->IsJSFunctionCall());
417 int parameter_count = static_cast<int>(incoming_->JSParameterCount() - 1);
418 int first_stack_slot = OsrHelper::FirstStackSlotIndex(parameter_count);
419
420 if (index == kOsrContextSpillSlotIndex) {
421 // Context. Use the parameter location of the context spill slot.
422 // Parameter (arity + 2) is special for the context of the function frame.
423 // >> context_index = target + receiver + params + new_target + #args
424 int context_index = 1 + 1 + parameter_count + 1 + 1;
425 return incoming_->GetInputLocation(context_index);
426 } else if (index >= first_stack_slot) {
427 // Local variable stored in this (callee) stack.
428 int spill_index =
429 index - first_stack_slot + StandardFrameConstants::kFixedSlotCount;
430 return LinkageLocation::ForCalleeFrameSlot(spill_index);
431 } else {
432 // Parameter. Use the assigned location from the incoming call descriptor.
433 int parameter_index = 1 + index; // skip index 0, which is the target.
434 return incoming_->GetInputLocation(parameter_index);
435 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000436}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000437
438
439bool Linkage::ParameterHasSecondaryLocation(int index) const {
Ben Murdochda12d292016-06-02 14:46:10 +0100440 if (!incoming_->IsJSFunctionCall()) return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000441 LinkageLocation loc = GetParameterLocation(index);
442 return (loc == regloc(kJSFunctionRegister) ||
443 loc == regloc(kContextRegister));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000444}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000445
446LinkageLocation Linkage::GetParameterSecondaryLocation(int index) const {
447 DCHECK(ParameterHasSecondaryLocation(index));
448 LinkageLocation loc = GetParameterLocation(index);
449
450 if (loc == regloc(kJSFunctionRegister)) {
451 return LinkageLocation::ForCalleeFrameSlot(Frame::kJSFunctionSlot);
452 } else {
453 DCHECK(loc == regloc(kContextRegister));
454 return LinkageLocation::ForCalleeFrameSlot(Frame::kContextSlot);
455 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000456}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000457
458
459} // namespace compiler
460} // namespace internal
461} // namespace v8