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