blob: 2ead1b3c25da42a6829a742363314741f5e47141 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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 { namespace internal {
36
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037Handle<Code> CodeStub::GetCode() {
38 uint32_t key = GetKey();
39 int index = Heap::code_stubs()->FindNumberEntry(key);
40 if (index == -1) {
41 HandleScope scope;
42
43 // Update the static counter each time a new code stub is generated.
44 Counters::code_stubs.Increment();
45
46 // Generate the new code.
47 MacroAssembler masm(NULL, 256);
48
kasper.lund7276f142008-07-30 08:49:36 +000049 // Nested stubs are not allowed for leafs.
50 masm.set_allow_stub_calls(AllowsStubCalls());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051
52 // Generate the code for the stub.
kasper.lund7276f142008-07-30 08:49:36 +000053 masm.set_generating_stub(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000054 Generate(&masm);
55
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056 // Create the code object.
57 CodeDesc desc;
58 masm.GetCode(&desc);
59
kasper.lund7276f142008-07-30 08:49:36 +000060 // Copy the generated code into a heap object, and store the major key.
61 Code::Flags flags = Code::ComputeFlags(Code::STUB);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000062 Handle<Code> code = Factory::NewCode(desc, NULL, flags);
kasper.lund7276f142008-07-30 08:49:36 +000063 code->set_major_key(MajorKey());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000064
65 // Add unresolved entries in the code to the fixup list.
66 Bootstrapper::AddFixup(*code, &masm);
67
ager@chromium.org236ad962008-09-25 09:45:57 +000068 LOG(CodeCreateEvent("Stub", *code, GetName()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000069 Counters::total_stubs_code_size.Increment(code->instruction_size());
70
71#ifdef DEBUG
72 if (FLAG_print_code_stubs) {
73 Print();
74 code->Print();
75 PrintF("\n");
76 }
77#endif
78
79 // Update the dictionary and the root in Heap.
80 Handle<Dictionary> dict =
81 Factory::DictionaryAtNumberPut(Handle<Dictionary>(Heap::code_stubs()),
82 key,
83 code);
84 Heap::set_code_stubs(*dict);
85 index = Heap::code_stubs()->FindNumberEntry(key);
86 }
87 ASSERT(index != -1);
88
89 return Handle<Code>(Code::cast(Heap::code_stubs()->ValueAt(index)));
90}
91
92
93const char* CodeStub::MajorName(CodeStub::Major major_key) {
94 switch (major_key) {
95 case CallFunction:
96 return "CallFunction";
kasper.lund7276f142008-07-30 08:49:36 +000097 case GenericBinaryOp:
98 return "GenericBinaryOp";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099 case SmiOp:
100 return "SmiOp";
101 case Compare:
102 return "Compare";
103 case RecordWrite:
104 return "RecordWrite";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105 case StackCheck:
106 return "StackCheck";
107 case UnarySub:
108 return "UnarySub";
109 case RevertToNumber:
110 return "RevertToNumber";
kasper.lund7276f142008-07-30 08:49:36 +0000111 case ToBoolean:
112 return "ToBoolean";
ager@chromium.org7c537e22008-10-16 08:43:32 +0000113 case Instanceof:
114 return "Instanceof";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000115 case CounterOp:
116 return "CounterOp";
117 case ArgumentsAccess:
118 return "ArgumentsAccess";
119 case Runtime:
120 return "Runtime";
121 case CEntry:
122 return "CEntry";
123 case JSEntry:
124 return "JSEntry";
125 case GetProperty:
126 return "GetProperty";
127 case SetProperty:
128 return "SetProperty";
129 case InvokeBuiltin:
130 return "InvokeBuiltin";
131 case JSExit:
132 return "JSExit";
133 default:
134 UNREACHABLE();
135 return NULL;
136 }
137}
138
139
140} } // namespace v8::internal