runtime.cc 4.18 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
#include "src/v8.h"
6

7 8
#include "src/runtime/runtime.h"
#include "src/runtime/runtime-utils.h"
9

10 11
namespace v8 {
namespace internal {
12

13 14 15 16 17 18 19 20 21
// Header of runtime functions.
#define F(name, number_of_args, result_size)                    \
  Object* Runtime_##name(int args_length, Object** args_object, \
                         Isolate* isolate);

#define P(name, number_of_args, result_size)                       \
  ObjectPair Runtime_##name(int args_length, Object** args_object, \
                            Isolate* isolate);

22 23 24
// Reference implementation for inlined runtime functions.  Only used when the
// compiler does not support a certain intrinsic.  Don't optimize these, but
// implement the intrinsic in the respective compiler instead.
25 26 27 28 29 30 31 32
#define I(name, number_of_args, result_size)                             \
  Object* RuntimeReference_##name(int args_length, Object** args_object, \
                                  Isolate* isolate);

RUNTIME_FUNCTION_LIST_RETURN_OBJECT(F)
RUNTIME_FUNCTION_LIST_RETURN_PAIR(P)
INLINE_OPTIMIZED_FUNCTION_LIST(F)
INLINE_FUNCTION_LIST(I)
33

34 35 36
#undef I
#undef F
#undef P
37 38


39 40 41 42 43 44
#define F(name, number_of_args, result_size)                                  \
  {                                                                           \
    Runtime::k##name, Runtime::RUNTIME, #name, FUNCTION_ADDR(Runtime_##name), \
        number_of_args, result_size                                           \
  }                                                                           \
  ,
45 46


47 48 49 50 51 52
#define I(name, number_of_args, result_size)                                \
  {                                                                         \
    Runtime::kInline##name, Runtime::INLINE, "_" #name,                     \
        FUNCTION_ADDR(RuntimeReference_##name), number_of_args, result_size \
  }                                                                         \
  ,
53

54

55 56 57 58 59 60
#define IO(name, number_of_args, result_size)                              \
  {                                                                        \
    Runtime::kInlineOptimized##name, Runtime::INLINE_OPTIMIZED, "_" #name, \
        FUNCTION_ADDR(Runtime_##name), number_of_args, result_size         \
  }                                                                        \
  ,
61 62


63
static const Runtime::Function kIntrinsicFunctions[] = {
64 65
    RUNTIME_FUNCTION_LIST(F) INLINE_OPTIMIZED_FUNCTION_LIST(F)
    INLINE_FUNCTION_LIST(I) INLINE_OPTIMIZED_FUNCTION_LIST(IO)};
66

67
#undef IO
68 69 70
#undef I
#undef F

71

72 73
void Runtime::InitializeIntrinsicFunctionNames(Isolate* isolate,
                                               Handle<NameDictionary> dict) {
74
  DCHECK(dict->NumberOfElements() == 0);
75
  HandleScope scope(isolate);
76
  for (int i = 0; i < kNumFunctions; ++i) {
77 78
    const char* name = kIntrinsicFunctions[i].name;
    if (name == NULL) continue;
79
    Handle<NameDictionary> new_dict = NameDictionary::Add(
80
        dict, isolate->factory()->InternalizeUtf8String(name),
81
        Handle<Smi>(Smi::FromInt(i), isolate), PropertyDetails(NONE, DATA, 0));
82 83
    // The dictionary does not need to grow.
    CHECK(new_dict.is_identical_to(dict));
84
  }
85 86 87
}


88
const Runtime::Function* Runtime::FunctionForName(Handle<String> name) {
89
  Heap* heap = name->GetHeap();
90
  int entry = heap->intrinsic_function_names()->FindEntry(name);
91
  if (entry != kNotFound) {
92
    Object* smi_index = heap->intrinsic_function_names()->ValueAt(entry);
93 94
    int function_index = Smi::cast(smi_index)->value();
    return &(kIntrinsicFunctions[function_index]);
95 96 97 98 99
  }
  return NULL;
}


100
const Runtime::Function* Runtime::FunctionForEntry(Address entry) {
101
  for (size_t i = 0; i < arraysize(kIntrinsicFunctions); ++i) {
102 103 104 105 106 107 108 109
    if (entry == kIntrinsicFunctions[i].entry) {
      return &(kIntrinsicFunctions[i]);
    }
  }
  return NULL;
}


110
const Runtime::Function* Runtime::FunctionForId(Runtime::FunctionId id) {
111 112
  return &(kIntrinsicFunctions[static_cast<int>(id)]);
}
113 114 115 116


std::ostream& operator<<(std::ostream& os, Runtime::FunctionId id) {
  return os << Runtime::FunctionForId(id)->name;
117
}
118 119 120

}  // namespace internal
}  // namespace v8