arguments.h 4.89 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_EXECUTION_ARGUMENTS_H_
#define V8_EXECUTION_ARGUMENTS_H_
7

8
#include "src/allocation.h"
9
#include "src/handles/handles.h"
10
#include "src/logging/counters.h"
11
#include "src/objects.h"
12
#include "src/objects/slots.h"
13
#include "src/tracing/trace-event.h"
14

15 16
namespace v8 {
namespace internal {
17 18 19 20 21 22 23 24 25

// Arguments provides access to runtime call parameters.
//
// It uses the fact that the instance fields of Arguments
// (length_, arguments_) are "overlayed" with the parameters
// (no. of parameters, and the parameter pointer) passed so
// that inside the C++ function, the parameters passed can
// be accessed conveniently:
//
26
//   Object Runtime_function(Arguments args) {
27 28
//     ... use args[i] here ...
//   }
29 30 31
//
// Note that length_ (whose value is in the integer range) is defined
// as intptr_t to provide endian-neutrality on 64-bit archs.
32

33
class Arguments {
34
 public:
35
  Arguments(int length, Address* arguments)
36 37 38
      : length_(length), arguments_(arguments) {
    DCHECK_GE(length_, 0);
  }
39

40
  Object operator[](int index) { return Object(*address_of_arg_at(index)); }
41

42
  template <class S = Object>
43
  inline Handle<S> at(int index);
44

45
  inline int smi_at(int index);
46

47
  inline double number_at(int index);
48

49
  inline void set_at(int index, Object value) {
50
    *address_of_arg_at(index) = value.ptr();
51
  }
sgjesse@chromium.org's avatar
sgjesse@chromium.org committed
52

53 54
  inline FullObjectSlot slot_at(int index) {
    return FullObjectSlot(address_of_arg_at(index));
55
  }
56

57 58 59
  inline Address* address_of_arg_at(int index) {
    DCHECK_LT(static_cast<uint32_t>(index), static_cast<uint32_t>(length_));
    return reinterpret_cast<Address*>(reinterpret_cast<Address>(arguments_) -
60
                                      index * kSystemPointerSize);
61
  }
62

63 64 65 66
  // Get the total number of arguments including the receiver.
  int length() const { return static_cast<int>(length_); }

  // Arguments on the stack are in reverse order (compared to an array).
67 68
  FullObjectSlot first_slot() { return slot_at(length() - 1); }
  FullObjectSlot last_slot() { return slot_at(0); }
69

70
 private:
71
  intptr_t length_;
72
  Address* arguments_;
73 74
};

75 76
template <>
inline Handle<Object> Arguments::at(int index) {
77
  return Handle<Object>(address_of_arg_at(index));
78 79
}

80 81 82 83 84 85 86 87
double ClobberDoubleRegisters(double x1, double x2, double x3, double x4);

#ifdef DEBUG
#define CLOBBER_DOUBLE_REGISTERS() ClobberDoubleRegisters(1, 2, 3, 4);
#else
#define CLOBBER_DOUBLE_REGISTERS()
#endif

88 89
// TODO(cbruni): add global flag to check whether any tracing events have been
// enabled.
90 91 92
#define RUNTIME_FUNCTION_RETURNS_TYPE(Type, InternalType, Convert, Name)      \
  static V8_INLINE InternalType __RT_impl_##Name(Arguments args,              \
                                                 Isolate* isolate);           \
93
                                                                              \
94
  V8_NOINLINE static Type Stats_##Name(int args_length, Address* args_object, \
95
                                       Isolate* isolate) {                    \
96
    RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::k##Name);      \
97 98
    TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.runtime"),                     \
                 "V8.Runtime_" #Name);                                        \
99
    Arguments args(args_length, args_object);                                 \
100
    return Convert(__RT_impl_##Name(args, isolate));                          \
101 102
  }                                                                           \
                                                                              \
103
  Type Name(int args_length, Address* args_object, Isolate* isolate) {        \
104
    DCHECK(isolate->context().is_null() || isolate->context().IsContext());   \
105
    CLOBBER_DOUBLE_REGISTERS();                                               \
106
    if (V8_UNLIKELY(TracingFlags::is_runtime_stats_enabled())) {              \
107 108 109
      return Stats_##Name(args_length, args_object, isolate);                 \
    }                                                                         \
    Arguments args(args_length, args_object);                                 \
110
    return Convert(__RT_impl_##Name(args, isolate));                          \
111 112
  }                                                                           \
                                                                              \
113
  static InternalType __RT_impl_##Name(Arguments args, Isolate* isolate)
114

115
#define CONVERT_OBJECT(x) (x).ptr()
116 117 118
#define CONVERT_OBJECTPAIR(x) (x)

#define RUNTIME_FUNCTION(Name) \
119
  RUNTIME_FUNCTION_RETURNS_TYPE(Address, Object, CONVERT_OBJECT, Name)
120 121 122 123

#define RUNTIME_FUNCTION_RETURN_PAIR(Name)                                  \
  RUNTIME_FUNCTION_RETURNS_TYPE(ObjectPair, ObjectPair, CONVERT_OBJECTPAIR, \
                                Name)
124

125 126
}  // namespace internal
}  // namespace v8
127

128
#endif  // V8_EXECUTION_ARGUMENTS_H_