ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 30 | #include "bootstrapper.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 31 | #include "codegen-inl.h" |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 32 | #include "compiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "debug.h" |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 34 | #include "oprofile-agent.h" |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 35 | #include "prettyprinter.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 36 | #include "register-allocator-inl.h" |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 37 | #include "rewriter.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | #include "runtime.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 39 | #include "scopeinfo.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | #include "stub-cache.h" |
sgjesse@chromium.org | 833cdd7 | 2010-02-26 10:06:16 +0000 | [diff] [blame] | 41 | #include "virtual-frame-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 43 | namespace v8 { |
| 44 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 46 | #define __ ACCESS_MASM(masm_) |
| 47 | |
| 48 | #ifdef DEBUG |
| 49 | |
| 50 | Comment::Comment(MacroAssembler* masm, const char* msg) |
| 51 | : masm_(masm), msg_(msg) { |
| 52 | __ RecordComment(msg); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | Comment::~Comment() { |
| 57 | if (msg_[0] == '[') __ RecordComment("]"); |
| 58 | } |
| 59 | |
| 60 | #endif // DEBUG |
| 61 | |
| 62 | #undef __ |
| 63 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 64 | |
| 65 | CodeGenerator* CodeGeneratorScope::top_ = NULL; |
| 66 | |
| 67 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 68 | void CodeGenerator::ProcessDeferred() { |
| 69 | while (!deferred_.is_empty()) { |
| 70 | DeferredCode* code = deferred_.RemoveLast(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 71 | ASSERT(masm_ == code->masm()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 72 | // Record position of deferred code stub. |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 73 | masm_->RecordStatementPosition(code->statement_position()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 74 | if (code->position() != RelocInfo::kNoPosition) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 75 | masm_->RecordPosition(code->position()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 76 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 77 | // Generate the code. |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 78 | Comment cmnt(masm_, code->comment()); |
| 79 | masm_->bind(code->entry_label()); |
| 80 | code->SaveRegisters(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 81 | code->Generate(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 82 | code->RestoreRegisters(); |
| 83 | masm_->jmp(code->exit_label()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | |
| 88 | void CodeGenerator::SetFrame(VirtualFrame* new_frame, |
| 89 | RegisterFile* non_frame_registers) { |
| 90 | RegisterFile saved_counts; |
| 91 | if (has_valid_frame()) { |
| 92 | frame_->DetachFromCodeGenerator(); |
| 93 | // The remaining register reference counts are the non-frame ones. |
| 94 | allocator_->SaveTo(&saved_counts); |
| 95 | } |
| 96 | |
| 97 | if (new_frame != NULL) { |
| 98 | // Restore the non-frame register references that go with the new frame. |
| 99 | allocator_->RestoreFrom(non_frame_registers); |
| 100 | new_frame->AttachToCodeGenerator(); |
| 101 | } |
| 102 | |
| 103 | frame_ = new_frame; |
| 104 | saved_counts.CopyTo(non_frame_registers); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void CodeGenerator::DeleteFrame() { |
| 109 | if (has_valid_frame()) { |
| 110 | frame_->DetachFromCodeGenerator(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 111 | frame_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 116 | void CodeGenerator::MakeCodePrologue(CompilationInfo* info) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 117 | #ifdef DEBUG |
| 118 | bool print_source = false; |
| 119 | bool print_ast = false; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 120 | bool print_json_ast = false; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 121 | const char* ftype; |
| 122 | |
| 123 | if (Bootstrapper::IsActive()) { |
| 124 | print_source = FLAG_print_builtin_source; |
| 125 | print_ast = FLAG_print_builtin_ast; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 126 | print_json_ast = FLAG_print_builtin_json_ast; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 127 | ftype = "builtin"; |
| 128 | } else { |
| 129 | print_source = FLAG_print_source; |
| 130 | print_ast = FLAG_print_ast; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 131 | print_json_ast = FLAG_print_json_ast; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 132 | ftype = "user-defined"; |
| 133 | } |
| 134 | |
| 135 | if (FLAG_trace_codegen || print_source || print_ast) { |
| 136 | PrintF("*** Generate code for %s function: ", ftype); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 137 | info->function()->name()->ShortPrint(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 138 | PrintF(" ***\n"); |
| 139 | } |
| 140 | |
| 141 | if (print_source) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 142 | PrintF("--- Source from AST ---\n%s\n", |
| 143 | PrettyPrinter().PrintProgram(info->function())); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | if (print_ast) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 147 | PrintF("--- AST ---\n%s\n", |
| 148 | AstPrinter().PrintProgram(info->function())); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | if (print_json_ast) { |
| 152 | JsonAstBuilder builder; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 153 | PrintF("%s", builder.BuildProgram(info->function())); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 154 | } |
| 155 | #endif // DEBUG |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 156 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 157 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 158 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 159 | Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm, |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 160 | Code::Flags flags, |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 161 | CompilationInfo* info) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 162 | // Allocate and install the code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 163 | CodeDesc desc; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 164 | masm->GetCode(&desc); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 165 | Handle<Code> code = Factory::NewCode(desc, flags, masm->CodeObject()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 166 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 167 | #ifdef ENABLE_DISASSEMBLER |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 168 | bool print_code = Bootstrapper::IsActive() |
| 169 | ? FLAG_print_builtin_code |
| 170 | : FLAG_print_code; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 171 | if (print_code) { |
| 172 | // Print the source code if available. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 173 | Handle<Script> script = info->script(); |
| 174 | FunctionLiteral* function = info->function(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 175 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 176 | PrintF("--- Raw source ---\n"); |
| 177 | StringInputBuffer stream(String::cast(script->source())); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 178 | stream.Seek(function->start_position()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 179 | // fun->end_position() points to the last character in the stream. We |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 180 | // need to compensate by adding one to calculate the length. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 181 | int source_len = |
| 182 | function->end_position() - function->start_position() + 1; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 183 | for (int i = 0; i < source_len; i++) { |
| 184 | if (stream.has_more()) PrintF("%c", stream.GetNext()); |
| 185 | } |
| 186 | PrintF("\n\n"); |
| 187 | } |
| 188 | PrintF("--- Code ---\n"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 189 | code->Disassemble(*function->name()->ToCString()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 190 | } |
| 191 | #endif // ENABLE_DISASSEMBLER |
| 192 | |
| 193 | if (!code.is_null()) { |
| 194 | Counters::total_compiled_code_size.Increment(code->instruction_size()); |
| 195 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 196 | return code; |
| 197 | } |
| 198 | |
| 199 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 200 | // Generate the code. Takes a function literal, generates code for it, assemble |
| 201 | // all the pieces into a Code object. This function is only to be called by |
| 202 | // the compiler.cc code. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 203 | Handle<Code> CodeGenerator::MakeCode(CompilationInfo* info) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 204 | Handle<Script> script = info->script(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 205 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 206 | int len = String::cast(script->source())->length(); |
| 207 | Counters::total_old_codegen_source_size.Increment(len); |
| 208 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 209 | MakeCodePrologue(info); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 210 | // Generate code. |
| 211 | const int kInitialBufferSize = 4 * KB; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 212 | MacroAssembler masm(NULL, kInitialBufferSize); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 213 | CodeGenerator cgen(&masm); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 214 | CodeGeneratorScope scope(&cgen); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 215 | cgen.Generate(info); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 216 | if (cgen.HasStackOverflow()) { |
| 217 | ASSERT(!Top::has_pending_exception()); |
| 218 | return Handle<Code>::null(); |
| 219 | } |
| 220 | |
| 221 | InLoopFlag in_loop = (cgen.loop_nesting() != 0) ? IN_LOOP : NOT_IN_LOOP; |
| 222 | Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, in_loop); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 223 | return MakeCodeEpilogue(cgen.masm(), flags, info); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 227 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 228 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 229 | bool CodeGenerator::ShouldGenerateLog(Expression* type) { |
| 230 | ASSERT(type != NULL); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 231 | if (!Logger::is_logging() && !CpuProfiler::is_profiling()) return false; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 232 | Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle()); |
| 233 | if (FLAG_log_regexp) { |
| 234 | static Vector<const char> kRegexp = CStrVector("regexp"); |
| 235 | if (name->IsEqualTo(kRegexp)) |
| 236 | return true; |
| 237 | } |
| 238 | return false; |
| 239 | } |
| 240 | |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 241 | #endif |
| 242 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 243 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 244 | Handle<Code> CodeGenerator::ComputeCallInitialize( |
| 245 | int argc, |
| 246 | InLoopFlag in_loop) { |
| 247 | if (in_loop == IN_LOOP) { |
| 248 | // Force the creation of the corresponding stub outside loops, |
| 249 | // because it may be used when clearing the ICs later - it is |
| 250 | // possible for a series of IC transitions to lose the in-loop |
| 251 | // information, and the IC clearing code can't generate a stub |
| 252 | // that it needs so we need to ensure it is generated already. |
| 253 | ComputeCallInitialize(argc, NOT_IN_LOOP); |
| 254 | } |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 255 | CALL_HEAP_FUNCTION( |
| 256 | StubCache::ComputeCallInitialize(argc, in_loop, Code::CALL_IC), |
| 257 | Code); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 261 | Handle<Code> CodeGenerator::ComputeKeyedCallInitialize( |
| 262 | int argc, |
| 263 | InLoopFlag in_loop) { |
| 264 | if (in_loop == IN_LOOP) { |
| 265 | // Force the creation of the corresponding stub outside loops, |
| 266 | // because it may be used when clearing the ICs later - it is |
| 267 | // possible for a series of IC transitions to lose the in-loop |
| 268 | // information, and the IC clearing code can't generate a stub |
| 269 | // that it needs so we need to ensure it is generated already. |
| 270 | ComputeKeyedCallInitialize(argc, NOT_IN_LOOP); |
| 271 | } |
| 272 | CALL_HEAP_FUNCTION( |
| 273 | StubCache::ComputeCallInitialize(argc, in_loop, Code::KEYED_CALL_IC), |
| 274 | Code); |
| 275 | } |
| 276 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 277 | void CodeGenerator::ProcessDeclarations(ZoneList<Declaration*>* declarations) { |
| 278 | int length = declarations->length(); |
| 279 | int globals = 0; |
| 280 | for (int i = 0; i < length; i++) { |
| 281 | Declaration* node = declarations->at(i); |
| 282 | Variable* var = node->proxy()->var(); |
| 283 | Slot* slot = var->slot(); |
| 284 | |
| 285 | // If it was not possible to allocate the variable at compile |
| 286 | // time, we need to "declare" it at runtime to make sure it |
| 287 | // actually exists in the local context. |
| 288 | if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) { |
| 289 | VisitDeclaration(node); |
| 290 | } else { |
| 291 | // Count global variables and functions for later processing |
| 292 | globals++; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // Return in case of no declared global functions or variables. |
| 297 | if (globals == 0) return; |
| 298 | |
| 299 | // Compute array of global variable and function declarations. |
| 300 | Handle<FixedArray> array = Factory::NewFixedArray(2 * globals, TENURED); |
| 301 | for (int j = 0, i = 0; i < length; i++) { |
| 302 | Declaration* node = declarations->at(i); |
| 303 | Variable* var = node->proxy()->var(); |
| 304 | Slot* slot = var->slot(); |
| 305 | |
| 306 | if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) { |
| 307 | // Skip - already processed. |
| 308 | } else { |
| 309 | array->set(j++, *(var->name())); |
| 310 | if (node->fun() == NULL) { |
| 311 | if (var->mode() == Variable::CONST) { |
| 312 | // In case this is const property use the hole. |
| 313 | array->set_the_hole(j++); |
| 314 | } else { |
| 315 | array->set_undefined(j++); |
| 316 | } |
| 317 | } else { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 318 | Handle<SharedFunctionInfo> function = |
| 319 | Compiler::BuildFunctionInfo(node->fun(), script(), this); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 320 | // Check for stack-overflow exception. |
| 321 | if (HasStackOverflow()) return; |
| 322 | array->set(j++, *function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | // Invoke the platform-dependent code generator to do the actual |
| 328 | // declaration the global variables and functions. |
| 329 | DeclareGlobals(array); |
| 330 | } |
| 331 | |
| 332 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 333 | // List of special runtime calls which are generated inline. For some of these |
| 334 | // functions the code will be generated inline, and for others a call to a code |
| 335 | // stub will be inlined. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 336 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 337 | #define INLINE_RUNTIME_ENTRY(Name, argc, ressize) \ |
| 338 | {&CodeGenerator::Generate##Name, "_" #Name, argc}, \ |
| 339 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 340 | CodeGenerator::InlineRuntimeLUT CodeGenerator::kInlineRuntimeLUT[] = { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 341 | INLINE_RUNTIME_FUNCTION_LIST(INLINE_RUNTIME_ENTRY) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 342 | }; |
| 343 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 344 | #undef INLINE_RUNTIME_ENTRY |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 345 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 346 | CodeGenerator::InlineRuntimeLUT* CodeGenerator::FindInlineRuntimeLUT( |
| 347 | Handle<String> name) { |
| 348 | const int entries_count = |
| 349 | sizeof(kInlineRuntimeLUT) / sizeof(InlineRuntimeLUT); |
| 350 | for (int i = 0; i < entries_count; i++) { |
| 351 | InlineRuntimeLUT* entry = &kInlineRuntimeLUT[i]; |
| 352 | if (name->IsEqualTo(CStrVector(entry->name))) { |
| 353 | return entry; |
| 354 | } |
| 355 | } |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 360 | bool CodeGenerator::CheckForInlineRuntimeCall(CallRuntime* node) { |
| 361 | ZoneList<Expression*>* args = node->arguments(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 362 | Handle<String> name = node->name(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 363 | if (name->length() > 0 && name->Get(0) == '_') { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 364 | InlineRuntimeLUT* entry = FindInlineRuntimeLUT(name); |
| 365 | if (entry != NULL) { |
| 366 | ((*this).*(entry->method))(args); |
| 367 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 374 | bool CodeGenerator::PatchInlineRuntimeEntry(Handle<String> name, |
| 375 | const CodeGenerator::InlineRuntimeLUT& new_entry, |
| 376 | CodeGenerator::InlineRuntimeLUT* old_entry) { |
| 377 | InlineRuntimeLUT* entry = FindInlineRuntimeLUT(name); |
| 378 | if (entry == NULL) return false; |
| 379 | if (old_entry != NULL) { |
| 380 | old_entry->name = entry->name; |
| 381 | old_entry->method = entry->method; |
| 382 | } |
| 383 | entry->name = new_entry.name; |
| 384 | entry->method = new_entry.method; |
| 385 | return true; |
| 386 | } |
| 387 | |
| 388 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 389 | int CodeGenerator::InlineRuntimeCallArgumentsCount(Handle<String> name) { |
| 390 | CodeGenerator::InlineRuntimeLUT* f = |
| 391 | CodeGenerator::FindInlineRuntimeLUT(name); |
| 392 | if (f != NULL) return f->nargs; |
| 393 | return -1; |
| 394 | } |
| 395 | |
| 396 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 397 | // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a |
| 398 | // known result for the test expression, with no side effects. |
| 399 | CodeGenerator::ConditionAnalysis CodeGenerator::AnalyzeCondition( |
| 400 | Expression* cond) { |
| 401 | if (cond == NULL) return ALWAYS_TRUE; |
| 402 | |
| 403 | Literal* lit = cond->AsLiteral(); |
| 404 | if (lit == NULL) return DONT_KNOW; |
| 405 | |
| 406 | if (lit->IsTrue()) { |
| 407 | return ALWAYS_TRUE; |
| 408 | } else if (lit->IsFalse()) { |
| 409 | return ALWAYS_FALSE; |
| 410 | } |
| 411 | |
| 412 | return DONT_KNOW; |
| 413 | } |
| 414 | |
| 415 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 416 | bool CodeGenerator::RecordPositions(MacroAssembler* masm, |
| 417 | int pos, |
| 418 | bool right_here) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 419 | if (pos != RelocInfo::kNoPosition) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 420 | masm->RecordStatementPosition(pos); |
| 421 | masm->RecordPosition(pos); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 422 | if (right_here) { |
| 423 | return masm->WriteRecordedPositions(); |
| 424 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 425 | } |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 426 | return false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 430 | void CodeGenerator::CodeForFunctionPosition(FunctionLiteral* fun) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 431 | if (FLAG_debug_info) RecordPositions(masm(), fun->start_position(), false); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 435 | void CodeGenerator::CodeForReturnPosition(FunctionLiteral* fun) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 436 | if (FLAG_debug_info) RecordPositions(masm(), fun->end_position(), false); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 440 | void CodeGenerator::CodeForStatementPosition(Statement* stmt) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 441 | if (FLAG_debug_info) RecordPositions(masm(), stmt->statement_pos(), false); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 442 | } |
| 443 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 444 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 445 | void CodeGenerator::CodeForDoWhileConditionPosition(DoWhileStatement* stmt) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 446 | if (FLAG_debug_info) |
| 447 | RecordPositions(masm(), stmt->condition_position(), false); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 448 | } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 449 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 450 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 451 | void CodeGenerator::CodeForSourcePosition(int pos) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 452 | if (FLAG_debug_info && pos != RelocInfo::kNoPosition) { |
| 453 | masm()->RecordPosition(pos); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
| 457 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 458 | const char* GenericUnaryOpStub::GetName() { |
| 459 | switch (op_) { |
| 460 | case Token::SUB: |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 461 | if (negative_zero_ == kStrictNegativeZero) { |
| 462 | return overwrite_ == UNARY_OVERWRITE |
| 463 | ? "GenericUnaryOpStub_SUB_Overwrite_Strict0" |
| 464 | : "GenericUnaryOpStub_SUB_Alloc_Strict0"; |
| 465 | } else { |
| 466 | return overwrite_ == UNARY_OVERWRITE |
| 467 | ? "GenericUnaryOpStub_SUB_Overwrite_Ignore0" |
| 468 | : "GenericUnaryOpStub_SUB_Alloc_Ignore0"; |
| 469 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 470 | case Token::BIT_NOT: |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 471 | return overwrite_ == UNARY_OVERWRITE |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 472 | ? "GenericUnaryOpStub_BIT_NOT_Overwrite" |
| 473 | : "GenericUnaryOpStub_BIT_NOT_Alloc"; |
| 474 | default: |
| 475 | UNREACHABLE(); |
| 476 | return "<unknown>"; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 481 | void ArgumentsAccessStub::Generate(MacroAssembler* masm) { |
| 482 | switch (type_) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 483 | case READ_ELEMENT: GenerateReadElement(masm); break; |
| 484 | case NEW_OBJECT: GenerateNewObject(masm); break; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 489 | int CEntryStub::MinorKey() { |
| 490 | ASSERT(result_size_ <= 2); |
| 491 | #ifdef _WIN64 |
| 492 | return ExitFrameModeBits::encode(mode_) |
| 493 | | IndirectResultBits::encode(result_size_ > 1); |
| 494 | #else |
| 495 | return ExitFrameModeBits::encode(mode_); |
| 496 | #endif |
| 497 | } |
| 498 | |
| 499 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 500 | bool ApiGetterEntryStub::GetCustomCache(Code** code_out) { |
| 501 | Object* cache = info()->load_stub_cache(); |
| 502 | if (cache->IsUndefined()) { |
| 503 | return false; |
| 504 | } else { |
| 505 | *code_out = Code::cast(cache); |
| 506 | return true; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | |
| 511 | void ApiGetterEntryStub::SetCustomCache(Code* value) { |
| 512 | info()->set_load_stub_cache(value); |
| 513 | } |
| 514 | |
| 515 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 516 | } } // namespace v8::internal |