blob: 02090f9fe363d789ea023576a4f890caa927f47d [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 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.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_ARGUMENTS_H_
6#define V8_ARGUMENTS_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/allocation.h"
Ben Murdochda12d292016-06-02 14:46:10 +01009#include "src/objects-inl.h"
10#include "src/tracing/trace-event.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000011
Steve Blocka7e24c12009-10-30 11:49:00 +000012namespace v8 {
13namespace internal {
14
15// Arguments provides access to runtime call parameters.
16//
17// It uses the fact that the instance fields of Arguments
18// (length_, arguments_) are "overlayed" with the parameters
19// (no. of parameters, and the parameter pointer) passed so
20// that inside the C++ function, the parameters passed can
21// be accessed conveniently:
22//
23// Object* Runtime_function(Arguments args) {
24// ... use args[i] here ...
25// }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000026//
27// Note that length_ (whose value is in the integer range) is defined
28// as intptr_t to provide endian-neutrality on 64-bit archs.
Steve Blocka7e24c12009-10-30 11:49:00 +000029
30class Arguments BASE_EMBEDDED {
31 public:
32 Arguments(int length, Object** arguments)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033 : length_(length), arguments_(arguments) {
34 DCHECK_GE(length_, 0);
35 }
Steve Blocka7e24c12009-10-30 11:49:00 +000036
37 Object*& operator[] (int index) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038 DCHECK_GE(index, 0);
39 DCHECK_LT(static_cast<uint32_t>(index), static_cast<uint32_t>(length_));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040 return *(reinterpret_cast<Object**>(reinterpret_cast<intptr_t>(arguments_) -
41 index * kPointerSize));
Steve Blocka7e24c12009-10-30 11:49:00 +000042 }
43
44 template <class S> Handle<S> at(int index) {
45 Object** value = &((*this)[index]);
46 // This cast checks that the object we're accessing does indeed have the
47 // expected type.
48 S::cast(*value);
49 return Handle<S>(reinterpret_cast<S**>(value));
50 }
51
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000052 int smi_at(int index) {
53 return Smi::cast((*this)[index])->value();
54 }
55
56 double number_at(int index) {
57 return (*this)[index]->Number();
58 }
59
Steve Blocka7e24c12009-10-30 11:49:00 +000060 // Get the total number of arguments including the receiver.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061 int length() const { return static_cast<int>(length_); }
Steve Blocka7e24c12009-10-30 11:49:00 +000062
63 Object** arguments() { return arguments_; }
Ben Murdoch589d6972011-11-30 16:04:58 +000064
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000065 Object** lowest_address() { return &this->operator[](length() - 1); }
66
67 Object** highest_address() { return &this->operator[](0); }
68
Steve Blocka7e24c12009-10-30 11:49:00 +000069 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070 intptr_t length_;
Steve Blocka7e24c12009-10-30 11:49:00 +000071 Object** arguments_;
72};
73
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074double ClobberDoubleRegisters(double x1, double x2, double x3, double x4);
75
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076#ifdef DEBUG
77#define CLOBBER_DOUBLE_REGISTERS() ClobberDoubleRegisters(1, 2, 3, 4);
78#else
79#define CLOBBER_DOUBLE_REGISTERS()
80#endif
81
Ben Murdochda12d292016-06-02 14:46:10 +010082#define RUNTIME_FUNCTION_RETURNS_TYPE(Type, Name) \
83 static INLINE(Type __RT_impl_##Name(Arguments args, Isolate* isolate)); \
84 Type Name(int args_length, Object** args_object, Isolate* isolate) { \
85 CLOBBER_DOUBLE_REGISTERS(); \
86 Type value; \
87 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.runtime"), "V8." #Name); \
88 Arguments args(args_length, args_object); \
89 if (FLAG_runtime_call_stats) { \
90 RuntimeCallStats* stats = isolate->counters()->runtime_call_stats(); \
91 RuntimeCallTimerScope timer(isolate, &stats->Name); \
92 value = __RT_impl_##Name(args, isolate); \
93 } else { \
94 value = __RT_impl_##Name(args, isolate); \
95 } \
96 return value; \
97 } \
Ben Murdoch097c5b22016-05-18 11:27:45 +010098 static Type __RT_impl_##Name(Arguments args, Isolate* isolate)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099
100#define RUNTIME_FUNCTION(Name) RUNTIME_FUNCTION_RETURNS_TYPE(Object*, Name)
101#define RUNTIME_FUNCTION_RETURN_PAIR(Name) \
102 RUNTIME_FUNCTION_RETURNS_TYPE(ObjectPair, Name)
Ben Murdoch097c5b22016-05-18 11:27:45 +0100103#define RUNTIME_FUNCTION_RETURN_TRIPLE(Name) \
104 RUNTIME_FUNCTION_RETURNS_TYPE(ObjectTriple, Name)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000106} // namespace internal
107} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000108
109#endif // V8_ARGUMENTS_H_