blob: 9c24c60b72635b23e00570c06f231b1d82a9a58d [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
38Handle<Code> CodeStub::GetCode() {
39 uint32_t key = GetKey();
40 int index = Heap::code_stubs()->FindEntry(key);
41 if (index == NumberDictionary::kNotFound) {
42 HandleScope scope;
43
44 // Update the static counter each time a new code stub is generated.
45 Counters::code_stubs.Increment();
46
47 // Generate the new code.
48 MacroAssembler masm(NULL, 256);
49
50 // Nested stubs are not allowed for leafs.
51 masm.set_allow_stub_calls(AllowsStubCalls());
52
53 // Generate the code for the stub.
54 masm.set_generating_stub(true);
55 Generate(&masm);
56
57 // Create the code object.
58 CodeDesc desc;
59 masm.GetCode(&desc);
60
61 // Copy the generated code into a heap object, and store the major key.
62 Code::Flags flags = Code::ComputeFlags(Code::STUB, InLoop());
63 Handle<Code> code = Factory::NewCode(desc, NULL, flags, masm.CodeObject());
64 code->set_major_key(MajorKey());
65
66 // Add unresolved entries in the code to the fixup list.
67 Bootstrapper::AddFixup(*code, &masm);
68
69 LOG(CodeCreateEvent(Logger::STUB_TAG, *code, GetName()));
70 Counters::total_stubs_code_size.Increment(code->instruction_size());
71
72#ifdef ENABLE_DISASSEMBLER
73 if (FLAG_print_code_stubs) {
74#ifdef DEBUG
75 Print();
76#endif
77 code->Disassemble(GetName());
78 PrintF("\n");
79 }
80#endif
81
82 // Update the dictionary and the root in Heap.
83 Handle<NumberDictionary> dict =
84 Factory::DictionaryAtNumberPut(
85 Handle<NumberDictionary>(Heap::code_stubs()),
86 key,
87 code);
88 Heap::public_set_code_stubs(*dict);
89 index = Heap::code_stubs()->FindEntry(key);
90 }
91 ASSERT(index != NumberDictionary::kNotFound);
92
93 return Handle<Code>(Code::cast(Heap::code_stubs()->ValueAt(index)));
94}
95
96
97const char* CodeStub::MajorName(CodeStub::Major major_key) {
98 switch (major_key) {
99 case CallFunction:
100 return "CallFunction";
101 case GenericBinaryOp:
102 return "GenericBinaryOp";
103 case SmiOp:
104 return "SmiOp";
105 case Compare:
106 return "Compare";
107 case RecordWrite:
108 return "RecordWrite";
109 case StackCheck:
110 return "StackCheck";
111 case UnarySub:
112 return "UnarySub";
113 case RevertToNumber:
114 return "RevertToNumber";
115 case ToBoolean:
116 return "ToBoolean";
117 case Instanceof:
118 return "Instanceof";
119 case CounterOp:
120 return "CounterOp";
121 case ArgumentsAccess:
122 return "ArgumentsAccess";
123 case Runtime:
124 return "Runtime";
125 case CEntry:
126 return "CEntry";
127 case JSEntry:
128 return "JSEntry";
129 case GetProperty:
130 return "GetProperty";
131 case SetProperty:
132 return "SetProperty";
133 case InvokeBuiltin:
134 return "InvokeBuiltin";
135 case JSExit:
136 return "JSExit";
137 case ConvertToDouble:
138 return "ConvertToDouble";
139 case WriteInt32ToHeapNumber:
140 return "WriteInt32ToHeapNumber";
141 default:
142 UNREACHABLE();
143 return NULL;
144 }
145}
146
147
148} } // namespace v8::internal