OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "v8.h" | 28 #include "v8.h" |
29 | 29 |
30 #include "bootstrapper.h" | 30 #include "bootstrapper.h" |
31 #include "code-stubs.h" | 31 #include "code-stubs.h" |
32 #include "factory.h" | 32 #include "factory.h" |
33 #include "macro-assembler.h" | 33 #include "macro-assembler.h" |
34 | 34 |
35 namespace v8 { | 35 namespace v8 { |
36 namespace internal { | 36 namespace internal { |
37 | 37 |
| 38 bool 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; |
| 44 } |
| 45 return false; |
| 46 } |
| 47 |
| 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(); |
| 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 |
| 60 void 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 |
38 Handle<Code> CodeStub::GetCode() { | 81 Handle<Code> CodeStub::GetCode() { |
39 bool custom_cache = has_custom_cache(); | 82 Code* code; |
40 | 83 if (!FindCodeInCache(&code)) { |
41 int index = 0; | |
42 uint32_t key = 0; | |
43 if (custom_cache) { | |
44 Code* cached; | |
45 if (GetCustomCache(&cached)) { | |
46 return Handle<Code>(cached); | |
47 } else { | |
48 index = NumberDictionary::kNotFound; | |
49 } | |
50 } else { | |
51 key = GetKey(); | |
52 index = Heap::code_stubs()->FindEntry(key); | |
53 if (index != NumberDictionary::kNotFound) | |
54 return Handle<Code>(Code::cast(Heap::code_stubs()->ValueAt(index))); | |
55 } | |
56 | |
57 Code* result; | |
58 { | |
59 v8::HandleScope scope; | 84 v8::HandleScope scope; |
60 | 85 |
61 // Update the static counter each time a new code stub is generated. | |
62 Counters::code_stubs.Increment(); | |
63 | |
64 // Generate the new code. | 86 // Generate the new code. |
65 MacroAssembler masm(NULL, 256); | 87 MacroAssembler masm(NULL, 256); |
66 | 88 GenerateCode(&masm); |
67 // Nested stubs are not allowed for leafs. | |
68 masm.set_allow_stub_calls(AllowsStubCalls()); | |
69 | |
70 // Generate the code for the stub. | |
71 masm.set_generating_stub(true); | |
72 Generate(&masm); | |
73 | 89 |
74 // Create the code object. | 90 // Create the code object. |
75 CodeDesc desc; | 91 CodeDesc desc; |
76 masm.GetCode(&desc); | 92 masm.GetCode(&desc); |
77 | 93 |
78 // Copy the generated code into a heap object, and store the major key. | 94 // Copy the generated code into a heap object. |
79 Code::Flags flags = Code::ComputeFlags(Code::STUB, InLoop()); | 95 Code::Flags flags = Code::ComputeFlags(Code::STUB, InLoop()); |
80 Handle<Code> code = Factory::NewCode(desc, NULL, flags, masm.CodeObject()); | 96 Handle<Code> new_object = |
81 code->set_major_key(MajorKey()); | 97 Factory::NewCode(desc, NULL, flags, masm.CodeObject()); |
| 98 RecordCodeGeneration(*new_object, &masm); |
82 | 99 |
83 // Add unresolved entries in the code to the fixup list. | 100 if (has_custom_cache()) { |
84 Bootstrapper::AddFixup(*code, &masm); | 101 SetCustomCache(*new_object); |
85 | |
86 LOG(CodeCreateEvent(Logger::STUB_TAG, *code, GetName())); | |
87 Counters::total_stubs_code_size.Increment(code->instruction_size()); | |
88 | |
89 #ifdef ENABLE_DISASSEMBLER | |
90 if (FLAG_print_code_stubs) { | |
91 #ifdef DEBUG | |
92 Print(); | |
93 #endif | |
94 code->Disassemble(GetName()); | |
95 PrintF("\n"); | |
96 } | |
97 #endif | |
98 | |
99 if (custom_cache) { | |
100 SetCustomCache(*code); | |
101 } else { | 102 } else { |
102 // Update the dictionary and the root in Heap. | 103 // Update the dictionary and the root in Heap. |
103 Handle<NumberDictionary> dict = | 104 Handle<NumberDictionary> dict = |
104 Factory::DictionaryAtNumberPut( | 105 Factory::DictionaryAtNumberPut( |
105 Handle<NumberDictionary>(Heap::code_stubs()), | 106 Handle<NumberDictionary>(Heap::code_stubs()), |
106 key, | 107 GetKey(), |
107 code); | 108 new_object); |
108 Heap::public_set_code_stubs(*dict); | 109 Heap::public_set_code_stubs(*dict); |
109 } | 110 } |
110 result = *code; | 111 code = *new_object; |
111 } | 112 } |
112 | 113 |
113 return Handle<Code>(result); | 114 return Handle<Code>(code); |
| 115 } |
| 116 |
| 117 |
| 118 Object* 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; |
114 } | 149 } |
115 | 150 |
116 | 151 |
117 const char* CodeStub::MajorName(CodeStub::Major major_key) { | 152 const char* CodeStub::MajorName(CodeStub::Major major_key) { |
118 switch (major_key) { | 153 switch (major_key) { |
119 #define DEF_CASE(name) case name: return #name; | 154 #define DEF_CASE(name) case name: return #name; |
120 CODE_STUB_LIST(DEF_CASE) | 155 CODE_STUB_LIST(DEF_CASE) |
121 #undef DEF_CASE | 156 #undef DEF_CASE |
122 default: | 157 default: |
123 UNREACHABLE(); | 158 UNREACHABLE(); |
124 return NULL; | 159 return NULL; |
125 } | 160 } |
126 } | 161 } |
127 | 162 |
128 | 163 |
129 } } // namespace v8::internal | 164 } } // namespace v8::internal |
OLD | NEW |