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