Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 32 | #include "stub-cache.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | #include "factory.h" |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 34 | #include "gdb-jit.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | #include "macro-assembler.h" |
| 36 | |
| 37 | namespace v8 { |
| 38 | namespace internal { |
| 39 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 40 | bool CodeStub::FindCodeInCache(Code** code_out) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 41 | Heap* heap = Isolate::Current()->heap(); |
| 42 | int index = heap->code_stubs()->FindEntry(GetKey()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 43 | if (index != NumberDictionary::kNotFound) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 44 | *code_out = Code::cast(heap->code_stubs()->ValueAt(index)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 45 | return true; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 46 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 47 | return false; |
| 48 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 49 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 50 | |
| 51 | void CodeStub::GenerateCode(MacroAssembler* masm) { |
| 52 | // Update the static counter each time a new code stub is generated. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 53 | masm->isolate()->counters()->code_stubs()->Increment(); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 54 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 55 | // Nested stubs are not allowed for leafs. |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 56 | AllowStubCallsScope allow_scope(masm, AllowsStubCalls()); |
| 57 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 58 | // Generate the code for the stub. |
| 59 | masm->set_generating_stub(true); |
| 60 | Generate(masm); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | void CodeStub::RecordCodeGeneration(Code* code, MacroAssembler* masm) { |
| 65 | code->set_major_key(MajorKey()); |
| 66 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 67 | Isolate* isolate = masm->isolate(); |
| 68 | PROFILE(isolate, CodeCreateEvent(Logger::STUB_TAG, code, GetName())); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 69 | GDBJIT(AddCode(GDBJITInterface::STUB, GetName(), code)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 70 | Counters* counters = isolate->counters(); |
| 71 | counters->total_stubs_code_size()->Increment(code->instruction_size()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 72 | |
| 73 | #ifdef ENABLE_DISASSEMBLER |
| 74 | if (FLAG_print_code_stubs) { |
| 75 | #ifdef DEBUG |
| 76 | Print(); |
| 77 | #endif |
| 78 | code->Disassemble(GetName()); |
| 79 | PrintF("\n"); |
| 80 | } |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 85 | int CodeStub::GetCodeKind() { |
| 86 | return Code::STUB; |
| 87 | } |
| 88 | |
| 89 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 90 | Handle<Code> CodeStub::GetCode() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 91 | Isolate* isolate = Isolate::Current(); |
| 92 | Factory* factory = isolate->factory(); |
| 93 | Heap* heap = isolate->heap(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 94 | Code* code; |
| 95 | if (!FindCodeInCache(&code)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 96 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 98 | // Generate the new code. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 99 | MacroAssembler masm(isolate, NULL, 256); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 100 | GenerateCode(&masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 101 | |
| 102 | // Create the code object. |
| 103 | CodeDesc desc; |
| 104 | masm.GetCode(&desc); |
| 105 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 106 | // Copy the generated code into a heap object. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 107 | Code::Flags flags = Code::ComputeFlags( |
| 108 | static_cast<Code::Kind>(GetCodeKind()), |
| 109 | InLoop(), |
| 110 | GetICState()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 111 | Handle<Code> new_object = factory->NewCode( |
| 112 | desc, flags, masm.CodeObject(), NeedsImmovableCode()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 113 | RecordCodeGeneration(*new_object, &masm); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 114 | FinishCode(*new_object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 115 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 116 | // Update the dictionary and the root in Heap. |
| 117 | Handle<NumberDictionary> dict = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 118 | factory->DictionaryAtNumberPut( |
| 119 | Handle<NumberDictionary>(heap->code_stubs()), |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 120 | GetKey(), |
| 121 | new_object); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 122 | heap->public_set_code_stubs(*dict); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 123 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 124 | code = *new_object; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 125 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 126 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 127 | ASSERT(!NeedsImmovableCode() || heap->lo_space()->Contains(code)); |
| 128 | return Handle<Code>(code, isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 132 | MaybeObject* CodeStub::TryGetCode() { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 133 | Code* code; |
| 134 | if (!FindCodeInCache(&code)) { |
| 135 | // Generate the new code. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 136 | MacroAssembler masm(Isolate::Current(), NULL, 256); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 137 | GenerateCode(&masm); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 138 | Heap* heap = masm.isolate()->heap(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 139 | |
| 140 | // Create the code object. |
| 141 | CodeDesc desc; |
| 142 | masm.GetCode(&desc); |
| 143 | |
| 144 | // Try to copy the generated code into a heap object. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 145 | Code::Flags flags = Code::ComputeFlags( |
| 146 | static_cast<Code::Kind>(GetCodeKind()), |
| 147 | InLoop(), |
| 148 | GetICState()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 149 | Object* new_object; |
| 150 | { MaybeObject* maybe_new_object = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 151 | heap->CreateCode(desc, flags, masm.CodeObject()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 152 | if (!maybe_new_object->ToObject(&new_object)) return maybe_new_object; |
| 153 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 154 | code = Code::cast(new_object); |
| 155 | RecordCodeGeneration(code, &masm); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 156 | FinishCode(code); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 157 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 158 | // Try to update the code cache but do not fail if unable. |
| 159 | MaybeObject* maybe_new_object = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 160 | heap->code_stubs()->AtNumberPut(GetKey(), code); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 161 | if (maybe_new_object->ToObject(&new_object)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 162 | heap->public_set_code_stubs(NumberDictionary::cast(new_object)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | return code; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 170 | const char* CodeStub::MajorName(CodeStub::Major major_key, |
| 171 | bool allow_unknown_keys) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 172 | switch (major_key) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 173 | #define DEF_CASE(name) case name: return #name; |
| 174 | CODE_STUB_LIST(DEF_CASE) |
| 175 | #undef DEF_CASE |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 176 | default: |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 177 | if (!allow_unknown_keys) { |
| 178 | UNREACHABLE(); |
| 179 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 180 | return NULL; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 185 | int ICCompareStub::MinorKey() { |
| 186 | return OpField::encode(op_ - Token::EQ) | StateField::encode(state_); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | void ICCompareStub::Generate(MacroAssembler* masm) { |
| 191 | switch (state_) { |
| 192 | case CompareIC::UNINITIALIZED: |
| 193 | GenerateMiss(masm); |
| 194 | break; |
| 195 | case CompareIC::SMIS: |
| 196 | GenerateSmis(masm); |
| 197 | break; |
| 198 | case CompareIC::HEAP_NUMBERS: |
| 199 | GenerateHeapNumbers(masm); |
| 200 | break; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 201 | case CompareIC::STRINGS: |
| 202 | GenerateStrings(masm); |
| 203 | break; |
| 204 | case CompareIC::SYMBOLS: |
| 205 | GenerateSymbols(masm); |
| 206 | break; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 207 | case CompareIC::OBJECTS: |
| 208 | GenerateObjects(masm); |
| 209 | break; |
| 210 | default: |
| 211 | UNREACHABLE(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 216 | const char* InstanceofStub::GetName() { |
| 217 | if (name_ != NULL) return name_; |
| 218 | const int kMaxNameLength = 100; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 219 | name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray( |
| 220 | kMaxNameLength); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 221 | if (name_ == NULL) return "OOM"; |
| 222 | |
| 223 | const char* args = ""; |
| 224 | if (HasArgsInRegisters()) { |
| 225 | args = "_REGS"; |
| 226 | } |
| 227 | |
| 228 | const char* inline_check = ""; |
| 229 | if (HasCallSiteInlineCheck()) { |
| 230 | inline_check = "_INLINE"; |
| 231 | } |
| 232 | |
| 233 | const char* return_true_false_object = ""; |
| 234 | if (ReturnTrueFalseObject()) { |
| 235 | return_true_false_object = "_TRUEFALSE"; |
| 236 | } |
| 237 | |
| 238 | OS::SNPrintF(Vector<char>(name_, kMaxNameLength), |
| 239 | "InstanceofStub%s%s%s", |
| 240 | args, |
| 241 | inline_check, |
| 242 | return_true_false_object); |
| 243 | return name_; |
| 244 | } |
| 245 | |
| 246 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 247 | void KeyedLoadFastElementStub::Generate(MacroAssembler* masm) { |
| 248 | KeyedLoadStubCompiler::GenerateLoadFastElement(masm); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | void KeyedStoreFastElementStub::Generate(MacroAssembler* masm) { |
| 253 | KeyedStoreStubCompiler::GenerateStoreFastElement(masm, is_js_array_); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | void KeyedLoadExternalArrayStub::Generate(MacroAssembler* masm) { |
| 258 | KeyedLoadStubCompiler::GenerateLoadExternalArray(masm, array_type_); |
| 259 | } |
| 260 | |
| 261 | |
| 262 | void KeyedStoreExternalArrayStub::Generate(MacroAssembler* masm) { |
| 263 | KeyedStoreStubCompiler::GenerateStoreExternalArray(masm, array_type_); |
| 264 | } |
| 265 | |
| 266 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 267 | } } // namespace v8::internal |