Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1 | // Copyright 2010 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 "codegen-inl.h" |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 32 | #include "compiler.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | #include "debug.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | #include "prettyprinter.h" |
| 35 | #include "register-allocator-inl.h" |
| 36 | #include "rewriter.h" |
| 37 | #include "runtime.h" |
| 38 | #include "scopeinfo.h" |
| 39 | #include "stub-cache.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 40 | #include "virtual-frame-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 41 | |
| 42 | namespace v8 { |
| 43 | namespace internal { |
| 44 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 45 | #define __ ACCESS_MASM(masm_) |
| 46 | |
| 47 | #ifdef DEBUG |
| 48 | |
| 49 | Comment::Comment(MacroAssembler* masm, const char* msg) |
| 50 | : masm_(masm), msg_(msg) { |
| 51 | __ RecordComment(msg); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | Comment::~Comment() { |
| 56 | if (msg_[0] == '[') __ RecordComment("]"); |
| 57 | } |
| 58 | |
| 59 | #endif // DEBUG |
| 60 | |
| 61 | #undef __ |
| 62 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 63 | |
| 64 | CodeGenerator* CodeGeneratorScope::top_ = NULL; |
| 65 | |
| 66 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 67 | void CodeGenerator::ProcessDeferred() { |
| 68 | while (!deferred_.is_empty()) { |
| 69 | DeferredCode* code = deferred_.RemoveLast(); |
| 70 | ASSERT(masm_ == code->masm()); |
| 71 | // Record position of deferred code stub. |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 72 | masm_->positions_recorder()->RecordStatementPosition( |
| 73 | code->statement_position()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 74 | if (code->position() != RelocInfo::kNoPosition) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 75 | masm_->positions_recorder()->RecordPosition(code->position()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 76 | } |
| 77 | // Generate the code. |
| 78 | Comment cmnt(masm_, code->comment()); |
| 79 | masm_->bind(code->entry_label()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 80 | if (code->AutoSaveAndRestore()) { |
| 81 | code->SaveRegisters(); |
| 82 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 83 | code->Generate(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 84 | if (code->AutoSaveAndRestore()) { |
| 85 | code->RestoreRegisters(); |
| 86 | code->Exit(); |
| 87 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 92 | void DeferredCode::Exit() { |
| 93 | masm_->jmp(exit_label()); |
| 94 | } |
| 95 | |
| 96 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | void CodeGenerator::SetFrame(VirtualFrame* new_frame, |
| 98 | RegisterFile* non_frame_registers) { |
| 99 | RegisterFile saved_counts; |
| 100 | if (has_valid_frame()) { |
| 101 | frame_->DetachFromCodeGenerator(); |
| 102 | // The remaining register reference counts are the non-frame ones. |
| 103 | allocator_->SaveTo(&saved_counts); |
| 104 | } |
| 105 | |
| 106 | if (new_frame != NULL) { |
| 107 | // Restore the non-frame register references that go with the new frame. |
| 108 | allocator_->RestoreFrom(non_frame_registers); |
| 109 | new_frame->AttachToCodeGenerator(); |
| 110 | } |
| 111 | |
| 112 | frame_ = new_frame; |
| 113 | saved_counts.CopyTo(non_frame_registers); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | void CodeGenerator::DeleteFrame() { |
| 118 | if (has_valid_frame()) { |
| 119 | frame_->DetachFromCodeGenerator(); |
| 120 | frame_ = NULL; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 125 | void CodeGenerator::MakeCodePrologue(CompilationInfo* info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 126 | #ifdef DEBUG |
| 127 | bool print_source = false; |
| 128 | bool print_ast = false; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 129 | bool print_json_ast = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 130 | const char* ftype; |
| 131 | |
| 132 | if (Bootstrapper::IsActive()) { |
| 133 | print_source = FLAG_print_builtin_source; |
| 134 | print_ast = FLAG_print_builtin_ast; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 135 | print_json_ast = FLAG_print_builtin_json_ast; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 136 | ftype = "builtin"; |
| 137 | } else { |
| 138 | print_source = FLAG_print_source; |
| 139 | print_ast = FLAG_print_ast; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 140 | print_json_ast = FLAG_print_json_ast; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 141 | Vector<const char> filter = CStrVector(FLAG_hydrogen_filter); |
| 142 | if (print_source && !filter.is_empty()) { |
| 143 | print_source = info->function()->name()->IsEqualTo(filter); |
| 144 | } |
| 145 | if (print_ast && !filter.is_empty()) { |
| 146 | print_ast = info->function()->name()->IsEqualTo(filter); |
| 147 | } |
| 148 | if (print_json_ast && !filter.is_empty()) { |
| 149 | print_json_ast = info->function()->name()->IsEqualTo(filter); |
| 150 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 151 | ftype = "user-defined"; |
| 152 | } |
| 153 | |
| 154 | if (FLAG_trace_codegen || print_source || print_ast) { |
| 155 | PrintF("*** Generate code for %s function: ", ftype); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 156 | info->function()->name()->ShortPrint(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 157 | PrintF(" ***\n"); |
| 158 | } |
| 159 | |
| 160 | if (print_source) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 161 | PrintF("--- Source from AST ---\n%s\n", |
| 162 | PrettyPrinter().PrintProgram(info->function())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | if (print_ast) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 166 | PrintF("--- AST ---\n%s\n", |
| 167 | AstPrinter().PrintProgram(info->function())); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | if (print_json_ast) { |
| 171 | JsonAstBuilder builder; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 172 | PrintF("%s", builder.BuildProgram(info->function())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 173 | } |
| 174 | #endif // DEBUG |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 175 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 176 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 178 | Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm, |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 179 | Code::Flags flags, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 180 | CompilationInfo* info) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 181 | // Allocate and install the code. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 182 | CodeDesc desc; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 183 | masm->GetCode(&desc); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 184 | Handle<Code> code = Factory::NewCode(desc, flags, masm->CodeObject()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 185 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 186 | if (!code.is_null()) { |
| 187 | Counters::total_compiled_code_size.Increment(code->instruction_size()); |
| 188 | } |
| 189 | return code; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 194 | #ifdef ENABLE_DISASSEMBLER |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 195 | bool print_code = Bootstrapper::IsActive() |
| 196 | ? FLAG_print_builtin_code |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 197 | : (FLAG_print_code || (info->IsOptimizing() && FLAG_print_opt_code)); |
| 198 | Vector<const char> filter = CStrVector(FLAG_hydrogen_filter); |
| 199 | FunctionLiteral* function = info->function(); |
| 200 | bool match = filter.is_empty() || function->debug_name()->IsEqualTo(filter); |
| 201 | if (print_code && match) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 202 | // Print the source code if available. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 203 | Handle<Script> script = info->script(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 204 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 205 | PrintF("--- Raw source ---\n"); |
| 206 | StringInputBuffer stream(String::cast(script->source())); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 207 | stream.Seek(function->start_position()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 208 | // fun->end_position() points to the last character in the stream. We |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 209 | // need to compensate by adding one to calculate the length. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 210 | int source_len = |
| 211 | function->end_position() - function->start_position() + 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 212 | for (int i = 0; i < source_len; i++) { |
| 213 | if (stream.has_more()) PrintF("%c", stream.GetNext()); |
| 214 | } |
| 215 | PrintF("\n\n"); |
| 216 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 217 | if (info->IsOptimizing()) { |
| 218 | if (FLAG_print_unopt_code) { |
| 219 | PrintF("--- Unoptimized code ---\n"); |
| 220 | info->closure()->shared()->code()->Disassemble( |
| 221 | *function->debug_name()->ToCString()); |
| 222 | } |
| 223 | PrintF("--- Optimized code ---\n"); |
| 224 | } else { |
| 225 | PrintF("--- Code ---\n"); |
| 226 | } |
| 227 | code->Disassemble(*function->debug_name()->ToCString()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 228 | } |
| 229 | #endif // ENABLE_DISASSEMBLER |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 233 | // Generate the code. Compile the AST and assemble all the pieces into a |
| 234 | // Code object. |
| 235 | bool CodeGenerator::MakeCode(CompilationInfo* info) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 236 | // When using Crankshaft the classic backend should never be used. |
| 237 | ASSERT(!V8::UseCrankshaft()); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 238 | Handle<Script> script = info->script(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 239 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 240 | int len = String::cast(script->source())->length(); |
| 241 | Counters::total_old_codegen_source_size.Increment(len); |
| 242 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 243 | if (FLAG_trace_codegen) { |
| 244 | PrintF("Classic Compiler - "); |
| 245 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 246 | MakeCodePrologue(info); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 247 | // Generate code. |
| 248 | const int kInitialBufferSize = 4 * KB; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 249 | MacroAssembler masm(NULL, kInitialBufferSize); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 250 | #ifdef ENABLE_GDB_JIT_INTERFACE |
| 251 | masm.positions_recorder()->StartGDBJITLineInfoRecording(); |
| 252 | #endif |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 253 | CodeGenerator cgen(&masm); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 254 | CodeGeneratorScope scope(&cgen); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 255 | cgen.Generate(info); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 256 | if (cgen.HasStackOverflow()) { |
| 257 | ASSERT(!Top::has_pending_exception()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 258 | return false; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 261 | InLoopFlag in_loop = info->is_in_loop() ? IN_LOOP : NOT_IN_LOOP; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 262 | Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, in_loop); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 263 | Handle<Code> code = MakeCodeEpilogue(cgen.masm(), flags, info); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 264 | // There is no stack check table in code generated by the classic backend. |
| 265 | code->SetNoStackCheckTable(); |
| 266 | CodeGenerator::PrintCode(code, info); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 267 | info->SetCode(code); // May be an empty handle. |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 268 | #ifdef ENABLE_GDB_JIT_INTERFACE |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 269 | if (FLAG_gdbjit && !code.is_null()) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 270 | GDBJITLineInfo* lineinfo = |
| 271 | masm.positions_recorder()->DetachGDBJITLineInfo(); |
| 272 | |
| 273 | GDBJIT(RegisterDetailedLineInfo(*code, lineinfo)); |
| 274 | } |
| 275 | #endif |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 276 | return !code.is_null(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 280 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 281 | |
| 282 | bool CodeGenerator::ShouldGenerateLog(Expression* type) { |
| 283 | ASSERT(type != NULL); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 284 | if (!Logger::is_logging() && !CpuProfiler::is_profiling()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 285 | Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle()); |
| 286 | if (FLAG_log_regexp) { |
| 287 | static Vector<const char> kRegexp = CStrVector("regexp"); |
| 288 | if (name->IsEqualTo(kRegexp)) |
| 289 | return true; |
| 290 | } |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | #endif |
| 295 | |
| 296 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 297 | void CodeGenerator::ProcessDeclarations(ZoneList<Declaration*>* declarations) { |
| 298 | int length = declarations->length(); |
| 299 | int globals = 0; |
| 300 | for (int i = 0; i < length; i++) { |
| 301 | Declaration* node = declarations->at(i); |
| 302 | Variable* var = node->proxy()->var(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 303 | Slot* slot = var->AsSlot(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 304 | |
| 305 | // If it was not possible to allocate the variable at compile |
| 306 | // time, we need to "declare" it at runtime to make sure it |
| 307 | // actually exists in the local context. |
| 308 | if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) { |
| 309 | VisitDeclaration(node); |
| 310 | } else { |
| 311 | // Count global variables and functions for later processing |
| 312 | globals++; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // Return in case of no declared global functions or variables. |
| 317 | if (globals == 0) return; |
| 318 | |
| 319 | // Compute array of global variable and function declarations. |
| 320 | Handle<FixedArray> array = Factory::NewFixedArray(2 * globals, TENURED); |
| 321 | for (int j = 0, i = 0; i < length; i++) { |
| 322 | Declaration* node = declarations->at(i); |
| 323 | Variable* var = node->proxy()->var(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 324 | Slot* slot = var->AsSlot(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 325 | |
| 326 | if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) { |
| 327 | // Skip - already processed. |
| 328 | } else { |
| 329 | array->set(j++, *(var->name())); |
| 330 | if (node->fun() == NULL) { |
| 331 | if (var->mode() == Variable::CONST) { |
| 332 | // In case this is const property use the hole. |
| 333 | array->set_the_hole(j++); |
| 334 | } else { |
| 335 | array->set_undefined(j++); |
| 336 | } |
| 337 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 338 | Handle<SharedFunctionInfo> function = |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 339 | Compiler::BuildFunctionInfo(node->fun(), script()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 340 | // Check for stack-overflow exception. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 341 | if (function.is_null()) { |
| 342 | SetStackOverflow(); |
| 343 | return; |
| 344 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 345 | array->set(j++, *function); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | // Invoke the platform-dependent code generator to do the actual |
| 351 | // declaration the global variables and functions. |
| 352 | DeclareGlobals(array); |
| 353 | } |
| 354 | |
| 355 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 356 | void CodeGenerator::VisitIncrementOperation(IncrementOperation* expr) { |
| 357 | UNREACHABLE(); |
| 358 | } |
| 359 | |
| 360 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 361 | // Lookup table for code generators for special runtime calls which are |
| 362 | // generated inline. |
| 363 | #define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \ |
| 364 | &CodeGenerator::Generate##Name, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 365 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 366 | const CodeGenerator::InlineFunctionGenerator |
| 367 | CodeGenerator::kInlineFunctionGenerators[] = { |
| 368 | INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS) |
| 369 | INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 370 | }; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 371 | #undef INLINE_FUNCTION_GENERATOR_ADDRESS |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 372 | |
| 373 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 374 | bool CodeGenerator::CheckForInlineRuntimeCall(CallRuntime* node) { |
| 375 | ZoneList<Expression*>* args = node->arguments(); |
| 376 | Handle<String> name = node->name(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 377 | Runtime::Function* function = node->function(); |
| 378 | if (function != NULL && function->intrinsic_type == Runtime::INLINE) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 379 | int lookup_index = static_cast<int>(function->function_id) - |
| 380 | static_cast<int>(Runtime::kFirstInlineFunction); |
| 381 | ASSERT(lookup_index >= 0); |
| 382 | ASSERT(static_cast<size_t>(lookup_index) < |
| 383 | ARRAY_SIZE(kInlineFunctionGenerators)); |
| 384 | InlineFunctionGenerator generator = kInlineFunctionGenerators[lookup_index]; |
| 385 | (this->*generator)(args); |
| 386 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 387 | } |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 392 | // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a |
| 393 | // known result for the test expression, with no side effects. |
| 394 | CodeGenerator::ConditionAnalysis CodeGenerator::AnalyzeCondition( |
| 395 | Expression* cond) { |
| 396 | if (cond == NULL) return ALWAYS_TRUE; |
| 397 | |
| 398 | Literal* lit = cond->AsLiteral(); |
| 399 | if (lit == NULL) return DONT_KNOW; |
| 400 | |
| 401 | if (lit->IsTrue()) { |
| 402 | return ALWAYS_TRUE; |
| 403 | } else if (lit->IsFalse()) { |
| 404 | return ALWAYS_FALSE; |
| 405 | } |
| 406 | |
| 407 | return DONT_KNOW; |
| 408 | } |
| 409 | |
| 410 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 411 | bool CodeGenerator::RecordPositions(MacroAssembler* masm, |
| 412 | int pos, |
| 413 | bool right_here) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 414 | if (pos != RelocInfo::kNoPosition) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 415 | masm->positions_recorder()->RecordStatementPosition(pos); |
| 416 | masm->positions_recorder()->RecordPosition(pos); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 417 | if (right_here) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 418 | return masm->positions_recorder()->WriteRecordedPositions(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 419 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 420 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 421 | return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | |
| 425 | void CodeGenerator::CodeForFunctionPosition(FunctionLiteral* fun) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 426 | if (FLAG_debug_info) RecordPositions(masm(), fun->start_position(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | |
| 430 | void CodeGenerator::CodeForReturnPosition(FunctionLiteral* fun) { |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 431 | if (FLAG_debug_info) RecordPositions(masm(), fun->end_position() - 1, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | |
| 435 | void CodeGenerator::CodeForStatementPosition(Statement* stmt) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 436 | if (FLAG_debug_info) RecordPositions(masm(), stmt->statement_pos(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 439 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 440 | void CodeGenerator::CodeForDoWhileConditionPosition(DoWhileStatement* stmt) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 441 | if (FLAG_debug_info) |
| 442 | RecordPositions(masm(), stmt->condition_position(), false); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 443 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 444 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 445 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 446 | void CodeGenerator::CodeForSourcePosition(int pos) { |
| 447 | if (FLAG_debug_info && pos != RelocInfo::kNoPosition) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 448 | masm()->positions_recorder()->RecordPosition(pos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
| 452 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 453 | const char* GenericUnaryOpStub::GetName() { |
| 454 | switch (op_) { |
| 455 | case Token::SUB: |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 456 | if (negative_zero_ == kStrictNegativeZero) { |
| 457 | return overwrite_ == UNARY_OVERWRITE |
| 458 | ? "GenericUnaryOpStub_SUB_Overwrite_Strict0" |
| 459 | : "GenericUnaryOpStub_SUB_Alloc_Strict0"; |
| 460 | } else { |
| 461 | return overwrite_ == UNARY_OVERWRITE |
| 462 | ? "GenericUnaryOpStub_SUB_Overwrite_Ignore0" |
| 463 | : "GenericUnaryOpStub_SUB_Alloc_Ignore0"; |
| 464 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 465 | case Token::BIT_NOT: |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 466 | return overwrite_ == UNARY_OVERWRITE |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 467 | ? "GenericUnaryOpStub_BIT_NOT_Overwrite" |
| 468 | : "GenericUnaryOpStub_BIT_NOT_Alloc"; |
| 469 | default: |
| 470 | UNREACHABLE(); |
| 471 | return "<unknown>"; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 476 | void ArgumentsAccessStub::Generate(MacroAssembler* masm) { |
| 477 | switch (type_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 478 | case READ_ELEMENT: GenerateReadElement(masm); break; |
| 479 | case NEW_OBJECT: GenerateNewObject(masm); break; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 484 | int CEntryStub::MinorKey() { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 485 | ASSERT(result_size_ == 1 || result_size_ == 2); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 486 | int result = save_doubles_ ? 1 : 0; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 487 | #ifdef _WIN64 |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 488 | return result | ((result_size_ == 1) ? 0 : 2); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 489 | #else |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 490 | return result; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 491 | #endif |
| 492 | } |
| 493 | |
| 494 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 495 | } } // namespace v8::internal |