Line data Source code
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_ARGUMENTS_H_
6 : #define V8_ARGUMENTS_H_
7 :
8 : #include "src/allocation.h"
9 : #include "src/objects.h"
10 : #include "src/tracing/trace-event.h"
11 :
12 : namespace v8 {
13 : namespace 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 : // }
26 : //
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.
29 :
30 : class Arguments BASE_EMBEDDED {
31 : public:
32 502011 : Arguments(int length, Object** arguments)
33 226500968 : : length_(length), arguments_(arguments) {
34 : DCHECK_GE(length_, 0);
35 502011 : }
36 :
37 392577057 : Object*& operator[] (int index) {
38 : DCHECK_GE(index, 0);
39 : DCHECK_LT(static_cast<uint32_t>(index), static_cast<uint32_t>(length_));
40 1735991544 : return *(reinterpret_cast<Object**>(reinterpret_cast<intptr_t>(arguments_) -
41 1752438939 : index * kPointerSize));
42 : }
43 :
44 : template <class S = Object>
45 329818447 : Handle<S> at(int index) {
46 : Object** value = &((*this)[index]);
47 : // This cast checks that the object we're accessing does indeed have the
48 : // expected type.
49 : S::cast(*value);
50 308776419 : return Handle<S>(reinterpret_cast<S**>(value));
51 : }
52 :
53 282615453 : int smi_at(int index) { return Smi::ToInt((*this)[index]); }
54 :
55 60 : double number_at(int index) {
56 120 : return (*this)[index]->Number();
57 : }
58 :
59 : // Get the total number of arguments including the receiver.
60 60741756 : int length() const { return static_cast<int>(length_); }
61 :
62 502011 : Object** arguments() { return arguments_; }
63 :
64 62 : Object** lowest_address() { return &this->operator[](length() - 1); }
65 :
66 62 : Object** highest_address() { return &this->operator[](0); }
67 :
68 : private:
69 : intptr_t length_;
70 : Object** arguments_;
71 : };
72 :
73 : double ClobberDoubleRegisters(double x1, double x2, double x3, double x4);
74 :
75 : #ifdef DEBUG
76 : #define CLOBBER_DOUBLE_REGISTERS() ClobberDoubleRegisters(1, 2, 3, 4);
77 : #else
78 : #define CLOBBER_DOUBLE_REGISTERS()
79 : #endif
80 :
81 : // TODO(cbruni): add global flag to check whether any tracing events have been
82 : // enabled.
83 : #define RUNTIME_FUNCTION_RETURNS_TYPE(Type, Name) \
84 : static INLINE(Type __RT_impl_##Name(Arguments args, Isolate* isolate)); \
85 : \
86 : V8_NOINLINE static Type Stats_##Name(int args_length, Object** args_object, \
87 : Isolate* isolate) { \
88 : RuntimeCallTimerScope timer(isolate, &RuntimeCallStats::Name); \
89 : TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.runtime"), \
90 : "V8.Runtime_" #Name); \
91 : Arguments args(args_length, args_object); \
92 : return __RT_impl_##Name(args, isolate); \
93 : } \
94 : \
95 : Type Name(int args_length, Object** args_object, Isolate* isolate) { \
96 : DCHECK(isolate->context() == nullptr || isolate->context()->IsContext()); \
97 : CLOBBER_DOUBLE_REGISTERS(); \
98 : if (V8_UNLIKELY(FLAG_runtime_stats)) { \
99 : return Stats_##Name(args_length, args_object, isolate); \
100 : } \
101 : Arguments args(args_length, args_object); \
102 : return __RT_impl_##Name(args, isolate); \
103 : } \
104 : \
105 : static Type __RT_impl_##Name(Arguments args, Isolate* isolate)
106 :
107 : #define RUNTIME_FUNCTION(Name) RUNTIME_FUNCTION_RETURNS_TYPE(Object*, Name)
108 : #define RUNTIME_FUNCTION_RETURN_PAIR(Name) \
109 : RUNTIME_FUNCTION_RETURNS_TYPE(ObjectPair, Name)
110 :
111 : } // namespace internal
112 : } // namespace v8
113 :
114 : #endif // V8_ARGUMENTS_H_
|