blob: 459ca50447dc3c92ace28fdf864a4b160c60bff4 [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// 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
10namespace v8 {
11namespace 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// TODO(mstarzinger): These are place-holder stubs for TurboFan and will
26// eventually all have a C++ implementation and this macro will be gone.
27#define I(name, number_of_args, result_size) \
28 Object* RuntimeReference_##name(int args_length, Object** args_object, \
29 Isolate* isolate);
30
31RUNTIME_FUNCTION_LIST_RETURN_OBJECT(F)
32RUNTIME_FUNCTION_LIST_RETURN_PAIR(P)
33INLINE_OPTIMIZED_FUNCTION_LIST(F)
34INLINE_FUNCTION_LIST(I)
35
36#undef I
37#undef F
38#undef P
39
40
41#define F(name, number_of_args, result_size) \
42 { \
43 Runtime::k##name, Runtime::RUNTIME, #name, FUNCTION_ADDR(Runtime_##name), \
44 number_of_args, result_size \
45 } \
46 ,
47
48
49#define I(name, number_of_args, result_size) \
50 { \
51 Runtime::kInline##name, Runtime::INLINE, "_" #name, \
52 FUNCTION_ADDR(RuntimeReference_##name), number_of_args, result_size \
53 } \
54 ,
55
56
57#define IO(name, number_of_args, result_size) \
58 { \
59 Runtime::kInlineOptimized##name, Runtime::INLINE_OPTIMIZED, "_" #name, \
60 FUNCTION_ADDR(Runtime_##name), number_of_args, result_size \
61 } \
62 ,
63
64
65static const Runtime::Function kIntrinsicFunctions[] = {
66 RUNTIME_FUNCTION_LIST(F) INLINE_OPTIMIZED_FUNCTION_LIST(F)
67 INLINE_FUNCTION_LIST(I) INLINE_OPTIMIZED_FUNCTION_LIST(IO)};
68
69#undef IO
70#undef I
71#undef F
72
73
74void Runtime::InitializeIntrinsicFunctionNames(Isolate* isolate,
75 Handle<NameDictionary> dict) {
76 DCHECK(dict->NumberOfElements() == 0);
77 HandleScope scope(isolate);
78 for (int i = 0; i < kNumFunctions; ++i) {
79 const char* name = kIntrinsicFunctions[i].name;
80 if (name == NULL) continue;
81 Handle<NameDictionary> new_dict = NameDictionary::Add(
82 dict, isolate->factory()->InternalizeUtf8String(name),
83 Handle<Smi>(Smi::FromInt(i), isolate), PropertyDetails(NONE, FIELD, 0));
84 // The dictionary does not need to grow.
85 CHECK(new_dict.is_identical_to(dict));
86 }
87}
88
89
90const Runtime::Function* Runtime::FunctionForName(Handle<String> name) {
91 Heap* heap = name->GetHeap();
92 int entry = heap->intrinsic_function_names()->FindEntry(name);
93 if (entry != kNotFound) {
94 Object* smi_index = heap->intrinsic_function_names()->ValueAt(entry);
95 int function_index = Smi::cast(smi_index)->value();
96 return &(kIntrinsicFunctions[function_index]);
97 }
98 return NULL;
99}
100
101
102const Runtime::Function* Runtime::FunctionForEntry(Address entry) {
103 for (size_t i = 0; i < arraysize(kIntrinsicFunctions); ++i) {
104 if (entry == kIntrinsicFunctions[i].entry) {
105 return &(kIntrinsicFunctions[i]);
106 }
107 }
108 return NULL;
109}
110
111
112const Runtime::Function* Runtime::FunctionForId(Runtime::FunctionId id) {
113 return &(kIntrinsicFunctions[static_cast<int>(id)]);
114}
115
116
117std::ostream& operator<<(std::ostream& os, Runtime::FunctionId id) {
118 return os << Runtime::FunctionForId(id)->name;
119}
120
121} // namespace internal
122} // namespace v8