blob: 09581aa82ac1ae1f4dba521dc2994cea55eab3dd [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"
34
35namespace v8 {
36namespace internal {
37
Leon Clarkee46be812010-01-19 14:06:41 +000038bool CodeStub::FindCodeInCache(Code** code_out) {
39 if (has_custom_cache()) return GetCustomCache(code_out);
40 int index = Heap::code_stubs()->FindEntry(GetKey());
41 if (index != NumberDictionary::kNotFound) {
42 *code_out = Code::cast(Heap::code_stubs()->ValueAt(index));
43 return true;
Steve Blockd0582a62009-12-15 09:54:21 +000044 }
Leon Clarkee46be812010-01-19 14:06:41 +000045 return false;
46}
Steve Blockd0582a62009-12-15 09:54:21 +000047
Leon Clarkee46be812010-01-19 14:06:41 +000048
49void CodeStub::GenerateCode(MacroAssembler* masm) {
50 // Update the static counter each time a new code stub is generated.
51 Counters::code_stubs.Increment();
52 // Nested stubs are not allowed for leafs.
53 masm->set_allow_stub_calls(AllowsStubCalls());
54 // Generate the code for the stub.
55 masm->set_generating_stub(true);
56 Generate(masm);
57}
58
59
60void CodeStub::RecordCodeGeneration(Code* code, MacroAssembler* masm) {
61 code->set_major_key(MajorKey());
62
63 // Add unresolved entries in the code to the fixup list.
64 Bootstrapper::AddFixup(code, masm);
65
66 LOG(CodeCreateEvent(Logger::STUB_TAG, code, GetName()));
67 Counters::total_stubs_code_size.Increment(code->instruction_size());
68
69#ifdef ENABLE_DISASSEMBLER
70 if (FLAG_print_code_stubs) {
71#ifdef DEBUG
72 Print();
73#endif
74 code->Disassemble(GetName());
75 PrintF("\n");
76 }
77#endif
78}
79
80
81Handle<Code> CodeStub::GetCode() {
82 Code* code;
83 if (!FindCodeInCache(&code)) {
Steve Blockd0582a62009-12-15 09:54:21 +000084 v8::HandleScope scope;
Steve Blocka7e24c12009-10-30 11:49:00 +000085
Steve Blocka7e24c12009-10-30 11:49:00 +000086 // Generate the new code.
87 MacroAssembler masm(NULL, 256);
Leon Clarkee46be812010-01-19 14:06:41 +000088 GenerateCode(&masm);
Steve Blocka7e24c12009-10-30 11:49:00 +000089
90 // Create the code object.
91 CodeDesc desc;
92 masm.GetCode(&desc);
93
Leon Clarkee46be812010-01-19 14:06:41 +000094 // Copy the generated code into a heap object.
Steve Blocka7e24c12009-10-30 11:49:00 +000095 Code::Flags flags = Code::ComputeFlags(Code::STUB, InLoop());
Leon Clarkee46be812010-01-19 14:06:41 +000096 Handle<Code> new_object =
97 Factory::NewCode(desc, NULL, flags, masm.CodeObject());
98 RecordCodeGeneration(*new_object, &masm);
Steve Blocka7e24c12009-10-30 11:49:00 +000099
Leon Clarkee46be812010-01-19 14:06:41 +0000100 if (has_custom_cache()) {
101 SetCustomCache(*new_object);
Steve Blockd0582a62009-12-15 09:54:21 +0000102 } else {
103 // Update the dictionary and the root in Heap.
104 Handle<NumberDictionary> dict =
105 Factory::DictionaryAtNumberPut(
106 Handle<NumberDictionary>(Heap::code_stubs()),
Leon Clarkee46be812010-01-19 14:06:41 +0000107 GetKey(),
108 new_object);
Steve Blockd0582a62009-12-15 09:54:21 +0000109 Heap::public_set_code_stubs(*dict);
110 }
Leon Clarkee46be812010-01-19 14:06:41 +0000111 code = *new_object;
Steve Blocka7e24c12009-10-30 11:49:00 +0000112 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000113
Leon Clarkee46be812010-01-19 14:06:41 +0000114 return Handle<Code>(code);
115}
116
117
118Object* CodeStub::TryGetCode() {
119 Code* code;
120 if (!FindCodeInCache(&code)) {
121 // Generate the new code.
122 MacroAssembler masm(NULL, 256);
123 GenerateCode(&masm);
124
125 // Create the code object.
126 CodeDesc desc;
127 masm.GetCode(&desc);
128
129 // Try to copy the generated code into a heap object.
130 Code::Flags flags = Code::ComputeFlags(Code::STUB, InLoop());
131 Object* new_object =
132 Heap::CreateCode(desc, NULL, flags, masm.CodeObject());
133 if (new_object->IsFailure()) return new_object;
134 code = Code::cast(new_object);
135 RecordCodeGeneration(code, &masm);
136
137 if (has_custom_cache()) {
138 SetCustomCache(code);
139 } else {
140 // Try to update the code cache but do not fail if unable.
141 new_object = Heap::code_stubs()->AtNumberPut(GetKey(), code);
142 if (!new_object->IsFailure()) {
143 Heap::public_set_code_stubs(NumberDictionary::cast(new_object));
144 }
145 }
146 }
147
148 return code;
Steve Blocka7e24c12009-10-30 11:49:00 +0000149}
150
151
152const char* CodeStub::MajorName(CodeStub::Major major_key) {
153 switch (major_key) {
Steve Blockd0582a62009-12-15 09:54:21 +0000154#define DEF_CASE(name) case name: return #name;
155 CODE_STUB_LIST(DEF_CASE)
156#undef DEF_CASE
Steve Blocka7e24c12009-10-30 11:49:00 +0000157 default:
158 UNREACHABLE();
159 return NULL;
160 }
161}
162
163
164} } // namespace v8::internal