blob: 9d5969bb46ed19a73e49b6dc72ac50553e4beedf [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "bootstrapper.h"
31#include "code-stubs.h"
32#include "factory.h"
33#include "macro-assembler.h"
Andrei Popescu402d9372010-02-26 13:31:12 +000034#include "oprofile-agent.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000035
36namespace v8 {
37namespace internal {
38
Leon Clarkee46be812010-01-19 14:06:41 +000039bool CodeStub::FindCodeInCache(Code** code_out) {
40 if (has_custom_cache()) return GetCustomCache(code_out);
41 int index = Heap::code_stubs()->FindEntry(GetKey());
42 if (index != NumberDictionary::kNotFound) {
43 *code_out = Code::cast(Heap::code_stubs()->ValueAt(index));
44 return true;
Steve Blockd0582a62009-12-15 09:54:21 +000045 }
Leon Clarkee46be812010-01-19 14:06:41 +000046 return false;
47}
Steve Blockd0582a62009-12-15 09:54:21 +000048
Leon Clarkee46be812010-01-19 14:06:41 +000049
50void CodeStub::GenerateCode(MacroAssembler* masm) {
51 // Update the static counter each time a new code stub is generated.
52 Counters::code_stubs.Increment();
53 // Nested stubs are not allowed for leafs.
54 masm->set_allow_stub_calls(AllowsStubCalls());
55 // Generate the code for the stub.
56 masm->set_generating_stub(true);
57 Generate(masm);
58}
59
60
61void CodeStub::RecordCodeGeneration(Code* code, MacroAssembler* masm) {
62 code->set_major_key(MajorKey());
63
Steve Block6ded16b2010-05-10 14:33:55 +010064 OPROFILE(CreateNativeCodeRegion(GetName(),
65 code->instruction_start(),
66 code->instruction_size()));
67 PROFILE(CodeCreateEvent(Logger::STUB_TAG, code, GetName()));
Leon Clarkee46be812010-01-19 14:06:41 +000068 Counters::total_stubs_code_size.Increment(code->instruction_size());
69
70#ifdef ENABLE_DISASSEMBLER
71 if (FLAG_print_code_stubs) {
72#ifdef DEBUG
73 Print();
74#endif
75 code->Disassemble(GetName());
76 PrintF("\n");
77 }
78#endif
79}
80
81
Steve Block6ded16b2010-05-10 14:33:55 +010082int CodeStub::GetCodeKind() {
83 return Code::STUB;
84}
85
86
Leon Clarkee46be812010-01-19 14:06:41 +000087Handle<Code> CodeStub::GetCode() {
88 Code* code;
89 if (!FindCodeInCache(&code)) {
Steve Blockd0582a62009-12-15 09:54:21 +000090 v8::HandleScope scope;
Steve Blocka7e24c12009-10-30 11:49:00 +000091
Steve Blocka7e24c12009-10-30 11:49:00 +000092 // Generate the new code.
93 MacroAssembler masm(NULL, 256);
Leon Clarkee46be812010-01-19 14:06:41 +000094 GenerateCode(&masm);
Steve Blocka7e24c12009-10-30 11:49:00 +000095
96 // Create the code object.
97 CodeDesc desc;
98 masm.GetCode(&desc);
99
Leon Clarkee46be812010-01-19 14:06:41 +0000100 // Copy the generated code into a heap object.
Steve Block6ded16b2010-05-10 14:33:55 +0100101 Code::Flags flags = Code::ComputeFlags(
102 static_cast<Code::Kind>(GetCodeKind()),
103 InLoop(),
104 GetICState());
Leon Clarkee46be812010-01-19 14:06:41 +0000105 Handle<Code> new_object =
106 Factory::NewCode(desc, NULL, flags, masm.CodeObject());
107 RecordCodeGeneration(*new_object, &masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000108
Leon Clarkee46be812010-01-19 14:06:41 +0000109 if (has_custom_cache()) {
110 SetCustomCache(*new_object);
Steve Blockd0582a62009-12-15 09:54:21 +0000111 } else {
112 // Update the dictionary and the root in Heap.
113 Handle<NumberDictionary> dict =
114 Factory::DictionaryAtNumberPut(
115 Handle<NumberDictionary>(Heap::code_stubs()),
Leon Clarkee46be812010-01-19 14:06:41 +0000116 GetKey(),
117 new_object);
Steve Blockd0582a62009-12-15 09:54:21 +0000118 Heap::public_set_code_stubs(*dict);
119 }
Leon Clarkee46be812010-01-19 14:06:41 +0000120 code = *new_object;
Steve Blocka7e24c12009-10-30 11:49:00 +0000121 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000122
Leon Clarkee46be812010-01-19 14:06:41 +0000123 return Handle<Code>(code);
124}
125
126
127Object* CodeStub::TryGetCode() {
128 Code* code;
129 if (!FindCodeInCache(&code)) {
130 // Generate the new code.
131 MacroAssembler masm(NULL, 256);
132 GenerateCode(&masm);
133
134 // Create the code object.
135 CodeDesc desc;
136 masm.GetCode(&desc);
137
138 // Try to copy the generated code into a heap object.
Steve Block6ded16b2010-05-10 14:33:55 +0100139 Code::Flags flags = Code::ComputeFlags(
140 static_cast<Code::Kind>(GetCodeKind()),
141 InLoop(),
142 GetICState());
Leon Clarkee46be812010-01-19 14:06:41 +0000143 Object* new_object =
144 Heap::CreateCode(desc, NULL, flags, masm.CodeObject());
145 if (new_object->IsFailure()) return new_object;
146 code = Code::cast(new_object);
147 RecordCodeGeneration(code, &masm);
148
149 if (has_custom_cache()) {
150 SetCustomCache(code);
151 } else {
152 // Try to update the code cache but do not fail if unable.
153 new_object = Heap::code_stubs()->AtNumberPut(GetKey(), code);
154 if (!new_object->IsFailure()) {
155 Heap::public_set_code_stubs(NumberDictionary::cast(new_object));
156 }
157 }
158 }
159
160 return code;
Steve Blocka7e24c12009-10-30 11:49:00 +0000161}
162
163
Andrei Popescu31002712010-02-23 13:46:05 +0000164const char* CodeStub::MajorName(CodeStub::Major major_key,
165 bool allow_unknown_keys) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000166 switch (major_key) {
Steve Blockd0582a62009-12-15 09:54:21 +0000167#define DEF_CASE(name) case name: return #name;
168 CODE_STUB_LIST(DEF_CASE)
169#undef DEF_CASE
Steve Blocka7e24c12009-10-30 11:49:00 +0000170 default:
Andrei Popescu31002712010-02-23 13:46:05 +0000171 if (!allow_unknown_keys) {
172 UNREACHABLE();
173 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000174 return NULL;
175 }
176}
177
178
179} } // namespace v8::internal