Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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/builtins/setup-builtins.h" | |
| 6 | |
| 7 #include "src/builtins/builtins.h" | |
| 8 #include "src/code-events.h" | |
| 9 #include "src/compiler/code-assembler.h" | |
| 10 #include "src/interface-descriptors.h" | |
| 11 #include "src/isolate.h" | |
| 12 | |
| 13 namespace v8 { | |
| 14 namespace internal { | |
| 15 | |
| 16 // Forward declarations for C++ builtins. | |
| 17 #define FORWARD_DECLARE(Name) \ | |
| 18 Object* Builtin_##Name(int argc, Object** args, Isolate* isolate); | |
| 19 BUILTIN_LIST_C(FORWARD_DECLARE) | |
| 20 | |
| 21 namespace { | |
| 22 void PostBuildProfileAndTracing(Isolate* isolate, Code* code, | |
| 23 const char* name) { | |
| 24 PROFILE(isolate, CodeCreateEvent(CodeEventListener::BUILTIN_TAG, | |
| 25 AbstractCode::cast(code), name)); | |
| 26 #ifdef ENABLE_DISASSEMBLER | |
| 27 if (FLAG_print_builtin_code) { | |
| 28 CodeTracer::Scope trace_scope(isolate->GetCodeTracer()); | |
| 29 OFStream os(trace_scope.file()); | |
| 30 os << "Builtin: " << name << "\n"; | |
| 31 code->Disassemble(name, os); | |
| 32 os << "\n"; | |
| 33 } | |
| 34 #endif | |
| 35 } | |
| 36 | |
| 37 typedef void (*MacroAssemblerGenerator)(MacroAssembler*); | |
| 38 typedef void (*CodeAssemblerGenerator)(compiler::CodeAssemblerState*); | |
| 39 | |
| 40 Code* BuildWithMacroAssembler(Isolate* isolate, | |
| 41 MacroAssemblerGenerator generator, | |
| 42 Code::Flags flags, const char* s_name) { | |
| 43 HandleScope scope(isolate); | |
| 44 const size_t buffer_size = 32 * KB; | |
| 45 byte buffer[buffer_size]; // NOLINT(runtime/arrays) | |
| 46 MacroAssembler masm(isolate, buffer, buffer_size, CodeObjectRequired::kYes); | |
| 47 DCHECK(!masm.has_frame()); | |
| 48 generator(&masm); | |
| 49 CodeDesc desc; | |
| 50 masm.GetCode(&desc); | |
| 51 Handle<Code> code = | |
| 52 isolate->factory()->NewCode(desc, flags, masm.CodeObject()); | |
| 53 PostBuildProfileAndTracing(isolate, *code, s_name); | |
| 54 return *code; | |
| 55 } | |
| 56 | |
| 57 Code* BuildAdaptor(Isolate* isolate, Address builtin_address, | |
| 58 Builtins::ExitFrameType exit_frame_type, Code::Flags flags, | |
| 59 const char* name) { | |
| 60 HandleScope scope(isolate); | |
| 61 const size_t buffer_size = 32 * KB; | |
| 62 byte buffer[buffer_size]; // NOLINT(runtime/arrays) | |
| 63 MacroAssembler masm(isolate, buffer, buffer_size, CodeObjectRequired::kYes); | |
| 64 DCHECK(!masm.has_frame()); | |
| 65 Builtins::Generate_Adaptor(&masm, builtin_address, exit_frame_type); | |
| 66 CodeDesc desc; | |
| 67 masm.GetCode(&desc); | |
| 68 Handle<Code> code = | |
| 69 isolate->factory()->NewCode(desc, flags, masm.CodeObject()); | |
| 70 PostBuildProfileAndTracing(isolate, *code, name); | |
| 71 return *code; | |
| 72 } | |
| 73 | |
| 74 // Builder for builtins implemented in TurboFan with JS linkage. | |
| 75 Code* BuildWithCodeStubAssemblerJS(Isolate* isolate, | |
| 76 CodeAssemblerGenerator generator, int argc, | |
| 77 Code::Flags flags, const char* name) { | |
| 78 HandleScope scope(isolate); | |
| 79 Zone zone(isolate->allocator(), ZONE_NAME); | |
| 80 const int argc_with_recv = | |
| 81 (argc == SharedFunctionInfo::kDontAdaptArgumentsSentinel) ? 0 : argc + 1; | |
| 82 compiler::CodeAssemblerState state(isolate, &zone, argc_with_recv, flags, | |
| 83 name); | |
| 84 generator(&state); | |
| 85 Handle<Code> code = compiler::CodeAssembler::GenerateCode(&state); | |
| 86 PostBuildProfileAndTracing(isolate, *code, name); | |
| 87 return *code; | |
| 88 } | |
| 89 | |
| 90 // Builder for builtins implemented in TurboFan with CallStub linkage. | |
| 91 Code* BuildWithCodeStubAssemblerCS(Isolate* isolate, | |
| 92 CodeAssemblerGenerator generator, | |
| 93 CallDescriptors::Key interface_descriptor, | |
| 94 Code::Flags flags, const char* name, | |
| 95 int result_size) { | |
| 96 HandleScope scope(isolate); | |
| 97 Zone zone(isolate->allocator(), ZONE_NAME); | |
| 98 // The interface descriptor with given key must be initialized at this point | |
| 99 // and this construction just queries the details from the descriptors table. | |
| 100 CallInterfaceDescriptor descriptor(isolate, interface_descriptor); | |
| 101 // Ensure descriptor is already initialized. | |
| 102 DCHECK_LE(0, descriptor.GetRegisterParameterCount()); | |
| 103 compiler::CodeAssemblerState state(isolate, &zone, descriptor, flags, name, | |
| 104 result_size); | |
| 105 generator(&state); | |
| 106 Handle<Code> code = compiler::CodeAssembler::GenerateCode(&state); | |
| 107 PostBuildProfileAndTracing(isolate, *code, name); | |
| 108 return *code; | |
| 109 } | |
| 110 } // anonymous namespace | |
| 111 | |
| 112 void SetupBuiltinsDelegate::AddBuiltin(Builtins* builtins, int index, | |
|
Jakob Kummerow
2017/03/21 16:38:00
This function (and its call sites below) is new; t
| |
| 113 Code* code) { | |
| 114 builtins->builtins_[index] = code; | |
| 115 code->set_builtin_index(index); | |
| 116 } | |
| 117 | |
| 118 void SetupBuiltinsDelegate::SetupBuiltinsInternal(Isolate* isolate) { | |
| 119 Builtins* builtins = isolate->builtins(); | |
| 120 DCHECK(!builtins->initialized_); | |
| 121 | |
| 122 // Create a scope for the handles in the builtins. | |
| 123 HandleScope scope(isolate); | |
| 124 | |
| 125 int index = 0; | |
| 126 const Code::Flags kBuiltinFlags = Code::ComputeFlags(Code::BUILTIN); | |
| 127 Code* code; | |
| 128 #define BUILD_CPP(Name) \ | |
| 129 code = BuildAdaptor(isolate, FUNCTION_ADDR(Builtin_##Name), \ | |
| 130 Builtins::BUILTIN_EXIT, kBuiltinFlags, #Name); \ | |
| 131 AddBuiltin(builtins, index++, code); | |
| 132 #define BUILD_API(Name) \ | |
| 133 code = BuildAdaptor(isolate, FUNCTION_ADDR(Builtin_##Name), Builtins::EXIT, \ | |
| 134 kBuiltinFlags, #Name); \ | |
| 135 AddBuiltin(builtins, index++, code); | |
| 136 #define BUILD_TFJ(Name, Argc, ...) \ | |
| 137 code = BuildWithCodeStubAssemblerJS(isolate, &Builtins::Generate_##Name, \ | |
| 138 Argc, kBuiltinFlags, #Name); \ | |
| 139 AddBuiltin(builtins, index++, code); | |
| 140 #define BUILD_TFS(Name, Kind, Extra, InterfaceDescriptor, result_size) \ | |
| 141 { InterfaceDescriptor##Descriptor descriptor(isolate); } \ | |
| 142 code = BuildWithCodeStubAssemblerCS(isolate, &Builtins::Generate_##Name, \ | |
| 143 CallDescriptors::InterfaceDescriptor, \ | |
| 144 Code::ComputeFlags(Code::Kind, Extra), \ | |
| 145 #Name, result_size); \ | |
| 146 AddBuiltin(builtins, index++, code); | |
| 147 #define BUILD_ASM(Name) \ | |
| 148 code = BuildWithMacroAssembler(isolate, Builtins::Generate_##Name, \ | |
| 149 kBuiltinFlags, #Name); \ | |
| 150 AddBuiltin(builtins, index++, code); | |
| 151 #define BUILD_ASH(Name, Kind, Extra) \ | |
| 152 code = \ | |
| 153 BuildWithMacroAssembler(isolate, Builtins::Generate_##Name, \ | |
| 154 Code::ComputeFlags(Code::Kind, Extra), #Name); \ | |
| 155 AddBuiltin(builtins, index++, code); | |
| 156 | |
| 157 BUILTIN_LIST(BUILD_CPP, BUILD_API, BUILD_TFJ, BUILD_TFS, BUILD_ASM, BUILD_ASH, | |
| 158 BUILD_ASM); | |
| 159 | |
| 160 #undef BUILD_CPP | |
| 161 #undef BUILD_API | |
| 162 #undef BUILD_TFJ | |
| 163 #undef BUILD_TFS | |
| 164 #undef BUILD_ASM | |
| 165 #undef BUILD_ASH | |
| 166 CHECK_EQ(Builtins::builtin_count, index); | |
| 167 | |
| 168 #define SET_PROMISE_REJECTION_PREDICTION(Name) \ | |
| 169 Code::cast(builtins->builtins_[Builtins::k##Name]) \ | |
| 170 ->set_is_promise_rejection(true); | |
| 171 | |
| 172 BUILTIN_PROMISE_REJECTION_PREDICTION_LIST(SET_PROMISE_REJECTION_PREDICTION) | |
| 173 #undef SET_PROMISE_REJECTION_PREDICTION | |
| 174 | |
| 175 #define SET_EXCEPTION_CAUGHT_PREDICTION(Name) \ | |
| 176 Code::cast(builtins->builtins_[Builtins::k##Name]) \ | |
| 177 ->set_is_exception_caught(true); | |
| 178 | |
| 179 BUILTIN_EXCEPTION_CAUGHT_PREDICTION_LIST(SET_EXCEPTION_CAUGHT_PREDICTION) | |
| 180 #undef SET_EXCEPTION_CAUGHT_PREDICTION | |
| 181 | |
| 182 #define SET_CODE_NON_TAGGED_PARAMS(Name) \ | |
| 183 Code::cast(builtins->builtins_[Builtins::k##Name]) \ | |
| 184 ->set_has_tagged_params(false); | |
| 185 | |
| 186 BUILTINS_WITH_UNTAGGED_PARAMS(SET_CODE_NON_TAGGED_PARAMS) | |
| 187 #undef SET_CODE_NON_TAGGED_PARAMS | |
| 188 | |
| 189 isolate->builtins()->MarkInitialized(); | |
| 190 } | |
| 191 | |
| 192 } // namespace internal | |
| 193 } // namespace v8 | |
| OLD | NEW |