| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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. | 
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 |  | 
 | 5 | #ifndef V8_ARGUMENTS_H_ | 
 | 6 | #define V8_ARGUMENTS_H_ | 
 | 7 |  | 
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include "src/allocation.h" | 
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame^] | 9 | #include "src/objects.h" | 
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 10 | #include "src/tracing/trace-event.h" | 
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 11 |  | 
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | //   } | 
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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. | 
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 29 |  | 
 | 30 | class Arguments BASE_EMBEDDED { | 
 | 31 |  public: | 
 | 32 |   Arguments(int length, Object** arguments) | 
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 33 |       : length_(length), arguments_(arguments) { | 
 | 34 |     DCHECK_GE(length_, 0); | 
 | 35 |   } | 
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 |  | 
 | 37 |   Object*& operator[] (int index) { | 
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 38 |     DCHECK_GE(index, 0); | 
 | 39 |     DCHECK_LT(static_cast<uint32_t>(index), static_cast<uint32_t>(length_)); | 
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 40 |     return *(reinterpret_cast<Object**>(reinterpret_cast<intptr_t>(arguments_) - | 
 | 41 |                                         index * kPointerSize)); | 
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 |   } | 
 | 43 |  | 
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame^] | 44 |   template <class S = Object> | 
 | 45 |   Handle<S> at(int index) { | 
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 |     return Handle<S>(reinterpret_cast<S**>(value)); | 
 | 51 |   } | 
 | 52 |  | 
| Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 53 |   int smi_at(int index) { | 
 | 54 |     return Smi::cast((*this)[index])->value(); | 
 | 55 |   } | 
 | 56 |  | 
 | 57 |   double number_at(int index) { | 
 | 58 |     return (*this)[index]->Number(); | 
 | 59 |   } | 
 | 60 |  | 
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 |   // Get the total number of arguments including the receiver. | 
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 62 |   int length() const { return static_cast<int>(length_); } | 
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 63 |  | 
 | 64 |   Object** arguments() { return arguments_; } | 
| Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 65 |  | 
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 66 |   Object** lowest_address() { return &this->operator[](length() - 1); } | 
 | 67 |  | 
 | 68 |   Object** highest_address() { return &this->operator[](0); } | 
 | 69 |  | 
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 |  private: | 
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 71 |   intptr_t length_; | 
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 72 |   Object** arguments_; | 
 | 73 | }; | 
 | 74 |  | 
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 75 | double ClobberDoubleRegisters(double x1, double x2, double x3, double x4); | 
 | 76 |  | 
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 77 | #ifdef DEBUG | 
 | 78 | #define CLOBBER_DOUBLE_REGISTERS() ClobberDoubleRegisters(1, 2, 3, 4); | 
 | 79 | #else | 
 | 80 | #define CLOBBER_DOUBLE_REGISTERS() | 
 | 81 | #endif | 
 | 82 |  | 
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 83 | // TODO(cbruni): add global flag to check whether any tracing events have been | 
 | 84 | // enabled. | 
 | 85 | #define RUNTIME_FUNCTION_RETURNS_TYPE(Type, Name)                             \ | 
 | 86 |   static INLINE(Type __RT_impl_##Name(Arguments args, Isolate* isolate));     \ | 
 | 87 |                                                                               \ | 
 | 88 |   V8_NOINLINE static Type Stats_##Name(int args_length, Object** args_object, \ | 
 | 89 |                                        Isolate* isolate) {                    \ | 
 | 90 |     RuntimeCallTimerScope timer(isolate, &RuntimeCallStats::Name);            \ | 
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 91 |     TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.runtime"),                     \ | 
 | 92 |                  "V8.Runtime_" #Name);                                        \ | 
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 93 |     Arguments args(args_length, args_object);                                 \ | 
 | 94 |     return __RT_impl_##Name(args, isolate);                                   \ | 
 | 95 |   }                                                                           \ | 
 | 96 |                                                                               \ | 
 | 97 |   Type Name(int args_length, Object** args_object, Isolate* isolate) {        \ | 
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 98 |     DCHECK(isolate->context() == nullptr || isolate->context()->IsContext()); \ | 
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 99 |     CLOBBER_DOUBLE_REGISTERS();                                               \ | 
| Ben Murdoch | c8c1d9e | 2017-03-08 14:04:23 +0000 | [diff] [blame] | 100 |     if (V8_UNLIKELY(FLAG_runtime_stats)) {                                    \ | 
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 101 |       return Stats_##Name(args_length, args_object, isolate);                 \ | 
 | 102 |     }                                                                         \ | 
 | 103 |     Arguments args(args_length, args_object);                                 \ | 
 | 104 |     return __RT_impl_##Name(args, isolate);                                   \ | 
 | 105 |   }                                                                           \ | 
 | 106 |                                                                               \ | 
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 107 |   static Type __RT_impl_##Name(Arguments args, Isolate* isolate) | 
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 108 |  | 
 | 109 | #define RUNTIME_FUNCTION(Name) RUNTIME_FUNCTION_RETURNS_TYPE(Object*, Name) | 
 | 110 | #define RUNTIME_FUNCTION_RETURN_PAIR(Name) \ | 
 | 111 |     RUNTIME_FUNCTION_RETURNS_TYPE(ObjectPair, Name) | 
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 112 | #define RUNTIME_FUNCTION_RETURN_TRIPLE(Name) \ | 
 | 113 |     RUNTIME_FUNCTION_RETURNS_TYPE(ObjectTriple, Name) | 
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 114 |  | 
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 115 | }  // namespace internal | 
 | 116 | }  // namespace v8 | 
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 117 |  | 
 | 118 | #endif  // V8_ARGUMENTS_H_ |