arguments.h 5.64 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/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 17
namespace v8 {
namespace internal {
18 19 20 21 22 23 24 25 26

// 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:
//
27
//   Object Runtime_function(Arguments args) {
28 29
//     ... use args[i] here ...
//   }
30 31 32
//
// Note that length_ (whose value is in the integer range) is defined
// as intptr_t to provide endian-neutrality on 64-bit archs.
33

34
template <ArgumentsType arguments_type>
35
class Arguments {
36
 public:
37 38 39 40 41 42 43 44 45 46 47 48
  // Scope to temporarily change the value of an argument.
  class ChangeValueScope {
   public:
    inline ChangeValueScope(Isolate* isolate, Arguments* args, int index,
                            Object value);
    ~ChangeValueScope() { *location_ = old_value_->ptr(); }

   private:
    Address* location_;
    Handle<Object> old_value_;
  };

49
  Arguments(int length, Address* arguments)
50 51 52
      : length_(length), arguments_(arguments) {
    DCHECK_GE(length_, 0);
  }
53

54
  V8_INLINE Object operator[](int index) const {
55 56
    return Object(*address_of_arg_at(index));
  }
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

68
  V8_INLINE Address* address_of_arg_at(int index) const {
69 70
    DCHECK_LE(static_cast<uint32_t>(index), static_cast<uint32_t>(length_));
    uintptr_t offset = index * kSystemPointerSize;
71 72 73
    if (arguments_type == ArgumentsType::kJS) {
      offset = (length_ - index - 1) * kSystemPointerSize;
    }
74
    return reinterpret_cast<Address*>(reinterpret_cast<Address>(arguments_) -
75
                                      offset);
76
  }
77

78
  // Get the total number of arguments including the receiver.
79
  V8_INLINE int length() const { return static_cast<int>(length_); }
80

81
 private:
82
  intptr_t length_;
83
  Address* arguments_;
84 85
};

86 87 88 89 90
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);
91 92
}

93 94 95 96 97 98
#ifdef DEBUG
#define CLOBBER_DOUBLE_REGISTERS() ClobberDoubleRegisters(1, 2, 3, 4);
#else
#define CLOBBER_DOUBLE_REGISTERS()
#endif

99 100
// TODO(cbruni): add global flag to check whether any tracing events have been
// enabled.
101 102
#ifdef V8_RUNTIME_CALL_STATS
#define RUNTIME_ENTRY_WITH_RCS(Type, InternalType, Convert, Name)             \
103
  V8_NOINLINE static Type Stats_##Name(int args_length, Address* args_object, \
104
                                       Isolate* isolate) {                    \
105
    RCS_SCOPE(isolate, RuntimeCallCounterId::k##Name);                        \
106 107
    TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.runtime"),                     \
                 "V8.Runtime_" #Name);                                        \
108
    RuntimeArguments args(args_length, args_object);                          \
109
    return Convert(__RT_impl_##Name(args, isolate));                          \
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  }

#define TEST_AND_CALL_RCS(Name)                                \
  if (V8_UNLIKELY(TracingFlags::is_runtime_stats_enabled())) { \
    return Stats_##Name(args_length, args_object, isolate);    \
  }

#else  // V8_RUNTIME_CALL_STATS
#define RUNTIME_ENTRY_WITH_RCS(Type, InternalType, Convert, Name)
#define TEST_AND_CALL_RCS(Name)

#endif  // V8_RUNTIME_CALL_STATS

#define RUNTIME_FUNCTION_RETURNS_TYPE(Type, InternalType, Convert, Name)    \
  static V8_INLINE InternalType __RT_impl_##Name(RuntimeArguments args,     \
                                                 Isolate* isolate);         \
  RUNTIME_ENTRY_WITH_RCS(Type, InternalType, Convert, Name)                 \
  Type Name(int args_length, Address* args_object, Isolate* isolate) {      \
    DCHECK(isolate->context().is_null() || isolate->context().IsContext()); \
    CLOBBER_DOUBLE_REGISTERS();                                             \
    TEST_AND_CALL_RCS(Name)                                                 \
    RuntimeArguments args(args_length, args_object);                        \
    return Convert(__RT_impl_##Name(args, isolate));                        \
  }                                                                         \
                                                                            \
135
  static InternalType __RT_impl_##Name(RuntimeArguments args, Isolate* isolate)
136

137 138 139 140 141 142 143
#ifdef DEBUG
#define BUILTIN_CONVERT_RESULT(x) (isolate->VerifyBuiltinsResult(x)).ptr()
#define BUILTIN_CONVERT_RESULT_PAIR(x) isolate->VerifyBuiltinsResult(x)
#else  // DEBUG
#define BUILTIN_CONVERT_RESULT(x) (x).ptr()
#define BUILTIN_CONVERT_RESULT_PAIR(x) (x)
#endif  // DEBUG
144 145

#define RUNTIME_FUNCTION(Name) \
146
  RUNTIME_FUNCTION_RETURNS_TYPE(Address, Object, BUILTIN_CONVERT_RESULT, Name)
147

148 149 150
#define RUNTIME_FUNCTION_RETURN_PAIR(Name)              \
  RUNTIME_FUNCTION_RETURNS_TYPE(ObjectPair, ObjectPair, \
                                BUILTIN_CONVERT_RESULT_PAIR, Name)
151

152 153
}  // namespace internal
}  // namespace v8
154

155
#endif  // V8_EXECUTION_ARGUMENTS_H_