arguments.h 5.54 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/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 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
template <ArgumentsType arguments_type>
34
class Arguments {
35
 public:
36
  Arguments(int length, Address* arguments)
37 38 39
      : length_(length), arguments_(arguments) {
    DCHECK_GE(length_, 0);
  }
40

41 42 43
  Object operator[](int index) const {
    return Object(*address_of_arg_at(index));
  }
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 51
  inline int tagged_index_at(int index) const;

52
  inline double number_at(int index) const;
53

54
  inline void set_at(int index, Object value) {
55
    *address_of_arg_at(index) = value.ptr();
56
  }
sgjesse@chromium.org's avatar
sgjesse@chromium.org committed
57

58
  inline FullObjectSlot slot_at(int index) const {
59
    return FullObjectSlot(address_of_arg_at(index));
60
  }
61

62
  inline Address* address_of_arg_at(int index) const {
63 64
    DCHECK_LE(static_cast<uint32_t>(index), static_cast<uint32_t>(length_));
    uintptr_t offset = index * kSystemPointerSize;
65 66 67 68 69
#ifdef V8_REVERSE_JSARGS
    if (arguments_type == ArgumentsType::kJS) {
      offset = (length_ - index - 1) * kSystemPointerSize;
    }
#endif
70
    return reinterpret_cast<Address*>(reinterpret_cast<Address>(arguments_) -
71
                                      offset);
72
  }
73

74 75 76 77
  // 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).
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  FullObjectSlot first_slot() const {
    int index = length() - 1;
#ifdef V8_REVERSE_JSARGS
    if (arguments_type == ArgumentsType::kJS) index = 0;
#endif
    return slot_at(index);
  }

  FullObjectSlot last_slot() const {
    int index = 0;
#ifdef V8_REVERSE_JSARGS
    if (arguments_type == ArgumentsType::kJS) index = length() - 1;
#endif
    return slot_at(index);
  }
93

94
 private:
95
  intptr_t length_;
96
  Address* arguments_;
97 98
};

99 100 101 102 103
template <ArgumentsType T>
template <class S>
Handle<S> Arguments<T>::at(int index) const {
  Handle<Object> obj = Handle<Object>(address_of_arg_at(index));
  return Handle<S>::cast(obj);
104 105
}

106 107 108 109 110 111 112 113
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

114 115
// TODO(cbruni): add global flag to check whether any tracing events have been
// enabled.
116
#define RUNTIME_FUNCTION_RETURNS_TYPE(Type, InternalType, Convert, Name)      \
117
  static V8_INLINE InternalType __RT_impl_##Name(RuntimeArguments args,       \
118
                                                 Isolate* isolate);           \
119
                                                                              \
120
  V8_NOINLINE static Type Stats_##Name(int args_length, Address* args_object, \
121
                                       Isolate* isolate) {                    \
122
    RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::k##Name);      \
123 124
    TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.runtime"),                     \
                 "V8.Runtime_" #Name);                                        \
125
    RuntimeArguments args(args_length, args_object);                          \
126
    return Convert(__RT_impl_##Name(args, isolate));                          \
127 128
  }                                                                           \
                                                                              \
129
  Type Name(int args_length, Address* args_object, Isolate* isolate) {        \
130
    DCHECK(isolate->context().is_null() || isolate->context().IsContext());   \
131
    CLOBBER_DOUBLE_REGISTERS();                                               \
132
    if (V8_UNLIKELY(TracingFlags::is_runtime_stats_enabled())) {              \
133 134
      return Stats_##Name(args_length, args_object, isolate);                 \
    }                                                                         \
135
    RuntimeArguments args(args_length, args_object);                          \
136
    return Convert(__RT_impl_##Name(args, isolate));                          \
137 138
  }                                                                           \
                                                                              \
139
  static InternalType __RT_impl_##Name(RuntimeArguments args, Isolate* isolate)
140

141
#define CONVERT_OBJECT(x) (x).ptr()
142 143 144
#define CONVERT_OBJECTPAIR(x) (x)

#define RUNTIME_FUNCTION(Name) \
145
  RUNTIME_FUNCTION_RETURNS_TYPE(Address, Object, CONVERT_OBJECT, Name)
146 147 148 149

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

151 152
}  // namespace internal
}  // namespace v8
153

154
#endif  // V8_EXECUTION_ARGUMENTS_H_