1 // 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.
4
5 #ifndef V8_EXECUTION_ARGUMENTS_H_
6 #define V8_EXECUTION_ARGUMENTS_H_
7
8 #include "src/handles/handles.h"
9 #include "src/logging/counters.h"
10 #include "src/objects/objects.h"
11 #include "src/objects/slots.h"
12 #include "src/tracing/trace-event.h"
13 #include "src/utils/allocation.h"
14
15 namespace v8 {
16 namespace internal {
17
18 // Arguments provides access to runtime call parameters.
19 //
20 // It uses the fact that the instance fields of Arguments
21 // (length_, arguments_) are "overlayed" with the parameters
22 // (no. of parameters, and the parameter pointer) passed so
23 // that inside the C++ function, the parameters passed can
24 // be accessed conveniently:
25 //
26 // Object Runtime_function(Arguments args) {
27 // ... use args[i] here ...
28 // }
29 //
30 // Note that length_ (whose value is in the integer range) is defined
31 // as intptr_t to provide endian-neutrality on 64-bit archs.
32
33 template <ArgumentsType arguments_type>
34 class Arguments {
35 public:
Arguments(int length,Address * arguments)36 Arguments(int length, Address* arguments)
37 : length_(length), arguments_(arguments) {
38 DCHECK_GE(length_, 0);
39 }
40
41 Object operator[](int index) const {
42 return Object(*address_of_arg_at(index));
43 }
44
45 template <class S = Object>
46 inline Handle<S> at(int index) const;
47
48 inline int smi_at(int index) const;
49
50 inline int tagged_index_at(int index) const;
51
52 inline double number_at(int index) const;
53
set_at(int index,Object value)54 inline void set_at(int index, Object value) {
55 *address_of_arg_at(index) = value.ptr();
56 }
57
slot_at(int index)58 inline FullObjectSlot slot_at(int index) const {
59 return FullObjectSlot(address_of_arg_at(index));
60 }
61
address_of_arg_at(int index)62 inline Address* address_of_arg_at(int index) const {
63 DCHECK_LE(static_cast<uint32_t>(index), static_cast<uint32_t>(length_));
64 uintptr_t offset = index * kSystemPointerSize;
65 if (arguments_type == ArgumentsType::kJS) {
66 offset = (length_ - index - 1) * kSystemPointerSize;
67 }
68 return reinterpret_cast<Address*>(reinterpret_cast<Address>(arguments_) -
69 offset);
70 }
71
72 // Get the total number of arguments including the receiver.
length()73 int length() const { return static_cast<int>(length_); }
74
75 // Arguments on the stack are in reverse order (compared to an array).
first_slot()76 FullObjectSlot first_slot() const {
77 int index = length() - 1;
78 if (arguments_type == ArgumentsType::kJS) index = 0;
79 return slot_at(index);
80 }
81
last_slot()82 FullObjectSlot last_slot() const {
83 int index = 0;
84 if (arguments_type == ArgumentsType::kJS) index = length() - 1;
85 return slot_at(index);
86 }
87
88 private:
89 intptr_t length_;
90 Address* arguments_;
91 };
92
93 template <ArgumentsType T>
94 template <class S>
at(int index)95 Handle<S> Arguments<T>::at(int index) const {
96 Handle<Object> obj = Handle<Object>(address_of_arg_at(index));
97 return Handle<S>::cast(obj);
98 }
99
100 double ClobberDoubleRegisters(double x1, double x2, double x3, double x4);
101
102 #ifdef DEBUG
103 #define CLOBBER_DOUBLE_REGISTERS() ClobberDoubleRegisters(1, 2, 3, 4);
104 #else
105 #define CLOBBER_DOUBLE_REGISTERS()
106 #endif
107
108 // TODO(cbruni): add global flag to check whether any tracing events have been
109 // enabled.
110 #define RUNTIME_FUNCTION_RETURNS_TYPE(Type, InternalType, Convert, Name) \
111 static V8_INLINE InternalType __RT_impl_##Name(RuntimeArguments args, \
112 Isolate* isolate); \
113 \
114 V8_NOINLINE static Type Stats_##Name(int args_length, Address* args_object, \
115 Isolate* isolate) { \
116 RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::k##Name); \
117 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.runtime"), \
118 "V8.Runtime_" #Name); \
119 RuntimeArguments args(args_length, args_object); \
120 return Convert(__RT_impl_##Name(args, isolate)); \
121 } \
122 \
123 Type Name(int args_length, Address* args_object, Isolate* isolate) { \
124 DCHECK(isolate->context().is_null() || isolate->context().IsContext()); \
125 CLOBBER_DOUBLE_REGISTERS(); \
126 if (V8_UNLIKELY(TracingFlags::is_runtime_stats_enabled())) { \
127 return Stats_##Name(args_length, args_object, isolate); \
128 } \
129 RuntimeArguments args(args_length, args_object); \
130 return Convert(__RT_impl_##Name(args, isolate)); \
131 } \
132 \
133 static InternalType __RT_impl_##Name(RuntimeArguments args, Isolate* isolate)
134
135 #define CONVERT_OBJECT(x) (x).ptr()
136 #define CONVERT_OBJECTPAIR(x) (x)
137
138 #define RUNTIME_FUNCTION(Name) \
139 RUNTIME_FUNCTION_RETURNS_TYPE(Address, Object, CONVERT_OBJECT, Name)
140
141 #define RUNTIME_FUNCTION_RETURN_PAIR(Name) \
142 RUNTIME_FUNCTION_RETURNS_TYPE(ObjectPair, ObjectPair, CONVERT_OBJECTPAIR, \
143 Name)
144
145 } // namespace internal
146 } // namespace v8
147
148 #endif // V8_EXECUTION_ARGUMENTS_H_
149