d01c141424cb4a5e88f3909742fff075607f1eaa
[platform/upstream/nodejs.git] / deps / v8 / src / runtime / runtime.cc
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.
4
5 #include "src/v8.h"
6
7 #include "src/runtime/runtime.h"
8 #include "src/runtime/runtime-utils.h"
9
10 namespace v8 {
11 namespace internal {
12
13 // Header of runtime functions.
14 #define F(name, number_of_args, result_size)                    \
15   Object* Runtime_##name(int args_length, Object** args_object, \
16                          Isolate* isolate);
17
18 #define P(name, number_of_args, result_size)                       \
19   ObjectPair Runtime_##name(int args_length, Object** args_object, \
20                             Isolate* isolate);
21
22 // Reference implementation for inlined runtime functions.  Only used when the
23 // compiler does not support a certain intrinsic.  Don't optimize these, but
24 // implement the intrinsic in the respective compiler instead.
25 #define I(name, number_of_args, result_size)                             \
26   Object* RuntimeReference_##name(int args_length, Object** args_object, \
27                                   Isolate* isolate);
28
29 RUNTIME_FUNCTION_LIST_RETURN_OBJECT(F)
30 RUNTIME_FUNCTION_LIST_RETURN_PAIR(P)
31 INLINE_OPTIMIZED_FUNCTION_LIST(F)
32 INLINE_FUNCTION_LIST(I)
33
34 #undef I
35 #undef F
36 #undef P
37
38
39 #define F(name, number_of_args, result_size)                                  \
40   {                                                                           \
41     Runtime::k##name, Runtime::RUNTIME, #name, FUNCTION_ADDR(Runtime_##name), \
42         number_of_args, result_size                                           \
43   }                                                                           \
44   ,
45
46
47 #define I(name, number_of_args, result_size)                                \
48   {                                                                         \
49     Runtime::kInline##name, Runtime::INLINE, "_" #name,                     \
50         FUNCTION_ADDR(RuntimeReference_##name), number_of_args, result_size \
51   }                                                                         \
52   ,
53
54
55 #define IO(name, number_of_args, result_size)                              \
56   {                                                                        \
57     Runtime::kInlineOptimized##name, Runtime::INLINE_OPTIMIZED, "_" #name, \
58         FUNCTION_ADDR(Runtime_##name), number_of_args, result_size         \
59   }                                                                        \
60   ,
61
62
63 static const Runtime::Function kIntrinsicFunctions[] = {
64     RUNTIME_FUNCTION_LIST(F) INLINE_OPTIMIZED_FUNCTION_LIST(F)
65     INLINE_FUNCTION_LIST(I) INLINE_OPTIMIZED_FUNCTION_LIST(IO)};
66
67 #undef IO
68 #undef I
69 #undef F
70
71
72 void Runtime::InitializeIntrinsicFunctionNames(Isolate* isolate,
73                                                Handle<NameDictionary> dict) {
74   DCHECK(dict->NumberOfElements() == 0);
75   HandleScope scope(isolate);
76   for (int i = 0; i < kNumFunctions; ++i) {
77     const char* name = kIntrinsicFunctions[i].name;
78     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     // The dictionary does not need to grow.
83     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     int function_index = Smi::cast(smi_index)->value();
94     return &(kIntrinsicFunctions[function_index]);
95   }
96   return NULL;
97 }
98
99
100 const Runtime::Function* Runtime::FunctionForEntry(Address entry) {
101   for (size_t i = 0; i < arraysize(kIntrinsicFunctions); ++i) {
102     if (entry == kIntrinsicFunctions[i].entry) {
103       return &(kIntrinsicFunctions[i]);
104     }
105   }
106   return NULL;
107 }
108
109
110 const Runtime::Function* Runtime::FunctionForId(Runtime::FunctionId id) {
111   return &(kIntrinsicFunctions[static_cast<int>(id)]);
112 }
113
114
115 std::ostream& operator<<(std::ostream& os, Runtime::FunctionId id) {
116   return os << Runtime::FunctionForId(id)->name;
117 }
118
119 }  // namespace internal
120 }  // namespace v8