arguments.h 4.39 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 7

#ifndef V8_ARGUMENTS_H_
#define V8_ARGUMENTS_H_

8
#include "src/allocation.h"
9
#include "src/objects.h"
10
#include "src/tracing/trace-event.h"
11

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

class Arguments BASE_EMBEDDED {
 public:
32
  Arguments(int length, Object** arguments)
33 34 35
      : length_(length), arguments_(arguments) {
    DCHECK_GE(length_, 0);
  }
36

37
  Object*& operator[] (int index) {
38 39
    DCHECK_GE(index, 0);
    DCHECK_LT(static_cast<uint32_t>(index), static_cast<uint32_t>(length_));
40 41
    return *(reinterpret_cast<Object**>(reinterpret_cast<intptr_t>(arguments_) -
                                        index * kPointerSize));
42 43
  }

44 45
  template <class S = Object>
  Handle<S> at(int index) {
46 47 48 49 50 51 52
    Object** value = &((*this)[index]);
    // This cast checks that the object we're accessing does indeed have the
    // expected type.
    S::cast(*value);
    return Handle<S>(reinterpret_cast<S**>(value));
  }

jgruber's avatar
jgruber committed
53
  int smi_at(int index) { return Smi::ToInt((*this)[index]); }
54 55 56 57 58

  double number_at(int index) {
    return (*this)[index]->Number();
  }

59
  // Get the total number of arguments including the receiver.
60
  int length() const { return static_cast<int>(length_); }
sgjesse@chromium.org's avatar
sgjesse@chromium.org committed
61

62
  Object** arguments() { return arguments_; }
63

64 65 66 67
  Object** lowest_address() { return &this->operator[](length() - 1); }

  Object** highest_address() { return &this->operator[](0); }

68
 private:
69
  intptr_t length_;
70 71 72
  Object** arguments_;
};

73 74 75 76 77 78 79 80
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

81 82 83 84 85 86 87
// TODO(cbruni): add global flag to check whether any tracing events have been
// enabled.
#define RUNTIME_FUNCTION_RETURNS_TYPE(Type, Name)                             \
  static INLINE(Type __RT_impl_##Name(Arguments args, Isolate* isolate));     \
                                                                              \
  V8_NOINLINE static Type Stats_##Name(int args_length, Object** args_object, \
                                       Isolate* isolate) {                    \
88
    RuntimeCallTimerScope timer(isolate, &RuntimeCallStats::Name);            \
89 90
    TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.runtime"),                     \
                 "V8.Runtime_" #Name);                                        \
91 92 93 94 95
    Arguments args(args_length, args_object);                                 \
    return __RT_impl_##Name(args, isolate);                                   \
  }                                                                           \
                                                                              \
  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 100 101 102 103 104
      return Stats_##Name(args_length, args_object, isolate);                 \
    }                                                                         \
    Arguments args(args_length, args_object);                                 \
    return __RT_impl_##Name(args, isolate);                                   \
  }                                                                           \
                                                                              \
105
  static Type __RT_impl_##Name(Arguments args, Isolate* isolate)
106

107 108 109 110
#define RUNTIME_FUNCTION(Name) RUNTIME_FUNCTION_RETURNS_TYPE(Object*, Name)
#define RUNTIME_FUNCTION_RETURN_PAIR(Name) \
    RUNTIME_FUNCTION_RETURNS_TYPE(ObjectPair, Name)

111 112
}  // namespace internal
}  // namespace v8
113 114

#endif  // V8_ARGUMENTS_H_