Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2009 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 "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" |
| 34 | #include "oprofile-agent.h" |
| 35 | #include "prettyprinter.h" |
| 36 | #include "register-allocator-inl.h" |
| 37 | #include "rewriter.h" |
| 38 | #include "runtime.h" |
| 39 | #include "scopeinfo.h" |
| 40 | #include "stub-cache.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 41 | #include "virtual-frame-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | |
| 43 | namespace v8 { |
| 44 | namespace internal { |
| 45 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +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 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | |
| 65 | CodeGenerator* CodeGeneratorScope::top_ = NULL; |
| 66 | |
| 67 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 68 | void CodeGenerator::ProcessDeferred() { |
| 69 | while (!deferred_.is_empty()) { |
| 70 | DeferredCode* code = deferred_.RemoveLast(); |
| 71 | ASSERT(masm_ == code->masm()); |
| 72 | // Record position of deferred code stub. |
| 73 | masm_->RecordStatementPosition(code->statement_position()); |
| 74 | if (code->position() != RelocInfo::kNoPosition) { |
| 75 | masm_->RecordPosition(code->position()); |
| 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; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 141 | ftype = "user-defined"; |
| 142 | } |
| 143 | |
| 144 | if (FLAG_trace_codegen || print_source || print_ast) { |
| 145 | PrintF("*** Generate code for %s function: ", ftype); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 146 | info->function()->name()->ShortPrint(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 147 | PrintF(" ***\n"); |
| 148 | } |
| 149 | |
| 150 | if (print_source) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 151 | PrintF("--- Source from AST ---\n%s\n", |
| 152 | PrettyPrinter().PrintProgram(info->function())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | if (print_ast) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 156 | PrintF("--- AST ---\n%s\n", |
| 157 | AstPrinter().PrintProgram(info->function())); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | if (print_json_ast) { |
| 161 | JsonAstBuilder builder; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 162 | PrintF("%s", builder.BuildProgram(info->function())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 163 | } |
| 164 | #endif // DEBUG |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 165 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 166 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 168 | Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm, |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 169 | Code::Flags flags, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 170 | CompilationInfo* info) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 171 | // Allocate and install the code. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 172 | CodeDesc desc; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 173 | masm->GetCode(&desc); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 174 | Handle<Code> code = Factory::NewCode(desc, flags, masm->CodeObject()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 175 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 176 | #ifdef ENABLE_DISASSEMBLER |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 177 | bool print_code = Bootstrapper::IsActive() |
| 178 | ? FLAG_print_builtin_code |
| 179 | : FLAG_print_code; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 180 | if (print_code) { |
| 181 | // Print the source code if available. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 182 | Handle<Script> script = info->script(); |
| 183 | FunctionLiteral* function = info->function(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 184 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 185 | PrintF("--- Raw source ---\n"); |
| 186 | StringInputBuffer stream(String::cast(script->source())); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 187 | stream.Seek(function->start_position()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 188 | // fun->end_position() points to the last character in the stream. We |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 189 | // need to compensate by adding one to calculate the length. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 190 | int source_len = |
| 191 | function->end_position() - function->start_position() + 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 192 | for (int i = 0; i < source_len; i++) { |
| 193 | if (stream.has_more()) PrintF("%c", stream.GetNext()); |
| 194 | } |
| 195 | PrintF("\n\n"); |
| 196 | } |
| 197 | PrintF("--- Code ---\n"); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 198 | code->Disassemble(*function->name()->ToCString()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 199 | } |
| 200 | #endif // ENABLE_DISASSEMBLER |
| 201 | |
| 202 | if (!code.is_null()) { |
| 203 | Counters::total_compiled_code_size.Increment(code->instruction_size()); |
| 204 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 205 | return code; |
| 206 | } |
| 207 | |
| 208 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 209 | // Generate the code. Takes a function literal, generates code for it, assemble |
| 210 | // all the pieces into a Code object. This function is only to be called by |
| 211 | // the compiler.cc code. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 212 | Handle<Code> CodeGenerator::MakeCode(CompilationInfo* info) { |
| 213 | Handle<Script> script = info->script(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 214 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 215 | int len = String::cast(script->source())->length(); |
| 216 | Counters::total_old_codegen_source_size.Increment(len); |
| 217 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 218 | MakeCodePrologue(info); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 219 | // Generate code. |
| 220 | const int kInitialBufferSize = 4 * KB; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 221 | MacroAssembler masm(NULL, kInitialBufferSize); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 222 | CodeGenerator cgen(&masm); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 223 | CodeGeneratorScope scope(&cgen); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 224 | cgen.Generate(info); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 225 | if (cgen.HasStackOverflow()) { |
| 226 | ASSERT(!Top::has_pending_exception()); |
| 227 | return Handle<Code>::null(); |
| 228 | } |
| 229 | |
| 230 | InLoopFlag in_loop = (cgen.loop_nesting() != 0) ? IN_LOOP : NOT_IN_LOOP; |
| 231 | Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, in_loop); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 232 | return MakeCodeEpilogue(cgen.masm(), flags, info); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 236 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 237 | |
| 238 | bool CodeGenerator::ShouldGenerateLog(Expression* type) { |
| 239 | ASSERT(type != NULL); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 240 | if (!Logger::is_logging() && !CpuProfiler::is_profiling()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 241 | Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle()); |
| 242 | if (FLAG_log_regexp) { |
| 243 | static Vector<const char> kRegexp = CStrVector("regexp"); |
| 244 | if (name->IsEqualTo(kRegexp)) |
| 245 | return true; |
| 246 | } |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | #endif |
| 251 | |
| 252 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 253 | Handle<Code> CodeGenerator::ComputeCallInitialize( |
| 254 | int argc, |
| 255 | InLoopFlag in_loop) { |
| 256 | if (in_loop == IN_LOOP) { |
| 257 | // Force the creation of the corresponding stub outside loops, |
| 258 | // because it may be used when clearing the ICs later - it is |
| 259 | // possible for a series of IC transitions to lose the in-loop |
| 260 | // information, and the IC clearing code can't generate a stub |
| 261 | // that it needs so we need to ensure it is generated already. |
| 262 | ComputeCallInitialize(argc, NOT_IN_LOOP); |
| 263 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 264 | CALL_HEAP_FUNCTION( |
| 265 | StubCache::ComputeCallInitialize(argc, in_loop, Code::CALL_IC), |
| 266 | Code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 270 | Handle<Code> CodeGenerator::ComputeKeyedCallInitialize( |
| 271 | int argc, |
| 272 | InLoopFlag in_loop) { |
| 273 | if (in_loop == IN_LOOP) { |
| 274 | // Force the creation of the corresponding stub outside loops, |
| 275 | // because it may be used when clearing the ICs later - it is |
| 276 | // possible for a series of IC transitions to lose the in-loop |
| 277 | // information, and the IC clearing code can't generate a stub |
| 278 | // that it needs so we need to ensure it is generated already. |
| 279 | ComputeKeyedCallInitialize(argc, NOT_IN_LOOP); |
| 280 | } |
| 281 | CALL_HEAP_FUNCTION( |
| 282 | StubCache::ComputeCallInitialize(argc, in_loop, Code::KEYED_CALL_IC), |
| 283 | Code); |
| 284 | } |
| 285 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 286 | void CodeGenerator::ProcessDeclarations(ZoneList<Declaration*>* declarations) { |
| 287 | int length = declarations->length(); |
| 288 | int globals = 0; |
| 289 | for (int i = 0; i < length; i++) { |
| 290 | Declaration* node = declarations->at(i); |
| 291 | Variable* var = node->proxy()->var(); |
| 292 | Slot* slot = var->slot(); |
| 293 | |
| 294 | // If it was not possible to allocate the variable at compile |
| 295 | // time, we need to "declare" it at runtime to make sure it |
| 296 | // actually exists in the local context. |
| 297 | if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) { |
| 298 | VisitDeclaration(node); |
| 299 | } else { |
| 300 | // Count global variables and functions for later processing |
| 301 | globals++; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | // Return in case of no declared global functions or variables. |
| 306 | if (globals == 0) return; |
| 307 | |
| 308 | // Compute array of global variable and function declarations. |
| 309 | Handle<FixedArray> array = Factory::NewFixedArray(2 * globals, TENURED); |
| 310 | for (int j = 0, i = 0; i < length; i++) { |
| 311 | Declaration* node = declarations->at(i); |
| 312 | Variable* var = node->proxy()->var(); |
| 313 | Slot* slot = var->slot(); |
| 314 | |
| 315 | if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) { |
| 316 | // Skip - already processed. |
| 317 | } else { |
| 318 | array->set(j++, *(var->name())); |
| 319 | if (node->fun() == NULL) { |
| 320 | if (var->mode() == Variable::CONST) { |
| 321 | // In case this is const property use the hole. |
| 322 | array->set_the_hole(j++); |
| 323 | } else { |
| 324 | array->set_undefined(j++); |
| 325 | } |
| 326 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 327 | Handle<SharedFunctionInfo> function = |
| 328 | Compiler::BuildFunctionInfo(node->fun(), script(), this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 329 | // Check for stack-overflow exception. |
| 330 | if (HasStackOverflow()) return; |
| 331 | array->set(j++, *function); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | // Invoke the platform-dependent code generator to do the actual |
| 337 | // declaration the global variables and functions. |
| 338 | DeclareGlobals(array); |
| 339 | } |
| 340 | |
| 341 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 342 | // List of special runtime calls which are generated inline. For some of these |
| 343 | // functions the code will be generated inline, and for others a call to a code |
| 344 | // stub will be inlined. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 345 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 346 | #define INLINE_RUNTIME_ENTRY(Name, argc, ressize) \ |
| 347 | {&CodeGenerator::Generate##Name, "_" #Name, argc}, \ |
| 348 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 349 | CodeGenerator::InlineRuntimeLUT CodeGenerator::kInlineRuntimeLUT[] = { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 350 | INLINE_RUNTIME_FUNCTION_LIST(INLINE_RUNTIME_ENTRY) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 351 | }; |
| 352 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 353 | #undef INLINE_RUNTIME_ENTRY |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 354 | |
| 355 | CodeGenerator::InlineRuntimeLUT* CodeGenerator::FindInlineRuntimeLUT( |
| 356 | Handle<String> name) { |
| 357 | const int entries_count = |
| 358 | sizeof(kInlineRuntimeLUT) / sizeof(InlineRuntimeLUT); |
| 359 | for (int i = 0; i < entries_count; i++) { |
| 360 | InlineRuntimeLUT* entry = &kInlineRuntimeLUT[i]; |
| 361 | if (name->IsEqualTo(CStrVector(entry->name))) { |
| 362 | return entry; |
| 363 | } |
| 364 | } |
| 365 | return NULL; |
| 366 | } |
| 367 | |
| 368 | |
| 369 | bool CodeGenerator::CheckForInlineRuntimeCall(CallRuntime* node) { |
| 370 | ZoneList<Expression*>* args = node->arguments(); |
| 371 | Handle<String> name = node->name(); |
| 372 | if (name->length() > 0 && name->Get(0) == '_') { |
| 373 | InlineRuntimeLUT* entry = FindInlineRuntimeLUT(name); |
| 374 | if (entry != NULL) { |
| 375 | ((*this).*(entry->method))(args); |
| 376 | return true; |
| 377 | } |
| 378 | } |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | bool CodeGenerator::PatchInlineRuntimeEntry(Handle<String> name, |
| 384 | const CodeGenerator::InlineRuntimeLUT& new_entry, |
| 385 | CodeGenerator::InlineRuntimeLUT* old_entry) { |
| 386 | InlineRuntimeLUT* entry = FindInlineRuntimeLUT(name); |
| 387 | if (entry == NULL) return false; |
| 388 | if (old_entry != NULL) { |
| 389 | old_entry->name = entry->name; |
| 390 | old_entry->method = entry->method; |
| 391 | } |
| 392 | entry->name = new_entry.name; |
| 393 | entry->method = new_entry.method; |
| 394 | return true; |
| 395 | } |
| 396 | |
| 397 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 398 | int CodeGenerator::InlineRuntimeCallArgumentsCount(Handle<String> name) { |
| 399 | CodeGenerator::InlineRuntimeLUT* f = |
| 400 | CodeGenerator::FindInlineRuntimeLUT(name); |
| 401 | if (f != NULL) return f->nargs; |
| 402 | return -1; |
| 403 | } |
| 404 | |
| 405 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 406 | // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a |
| 407 | // known result for the test expression, with no side effects. |
| 408 | CodeGenerator::ConditionAnalysis CodeGenerator::AnalyzeCondition( |
| 409 | Expression* cond) { |
| 410 | if (cond == NULL) return ALWAYS_TRUE; |
| 411 | |
| 412 | Literal* lit = cond->AsLiteral(); |
| 413 | if (lit == NULL) return DONT_KNOW; |
| 414 | |
| 415 | if (lit->IsTrue()) { |
| 416 | return ALWAYS_TRUE; |
| 417 | } else if (lit->IsFalse()) { |
| 418 | return ALWAYS_FALSE; |
| 419 | } |
| 420 | |
| 421 | return DONT_KNOW; |
| 422 | } |
| 423 | |
| 424 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 425 | bool CodeGenerator::RecordPositions(MacroAssembler* masm, |
| 426 | int pos, |
| 427 | bool right_here) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 428 | if (pos != RelocInfo::kNoPosition) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 429 | masm->RecordStatementPosition(pos); |
| 430 | masm->RecordPosition(pos); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 431 | if (right_here) { |
| 432 | return masm->WriteRecordedPositions(); |
| 433 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 434 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 435 | return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | |
| 439 | void CodeGenerator::CodeForFunctionPosition(FunctionLiteral* fun) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 440 | if (FLAG_debug_info) RecordPositions(masm(), fun->start_position(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | |
| 444 | void CodeGenerator::CodeForReturnPosition(FunctionLiteral* fun) { |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 445 | if (FLAG_debug_info) RecordPositions(masm(), fun->end_position() - 1, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | |
| 449 | void CodeGenerator::CodeForStatementPosition(Statement* stmt) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 450 | if (FLAG_debug_info) RecordPositions(masm(), stmt->statement_pos(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 453 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 454 | void CodeGenerator::CodeForDoWhileConditionPosition(DoWhileStatement* stmt) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 455 | if (FLAG_debug_info) |
| 456 | RecordPositions(masm(), stmt->condition_position(), false); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 457 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 458 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 459 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 460 | void CodeGenerator::CodeForSourcePosition(int pos) { |
| 461 | if (FLAG_debug_info && pos != RelocInfo::kNoPosition) { |
| 462 | masm()->RecordPosition(pos); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 467 | const char* GenericUnaryOpStub::GetName() { |
| 468 | switch (op_) { |
| 469 | case Token::SUB: |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 470 | if (negative_zero_ == kStrictNegativeZero) { |
| 471 | return overwrite_ == UNARY_OVERWRITE |
| 472 | ? "GenericUnaryOpStub_SUB_Overwrite_Strict0" |
| 473 | : "GenericUnaryOpStub_SUB_Alloc_Strict0"; |
| 474 | } else { |
| 475 | return overwrite_ == UNARY_OVERWRITE |
| 476 | ? "GenericUnaryOpStub_SUB_Overwrite_Ignore0" |
| 477 | : "GenericUnaryOpStub_SUB_Alloc_Ignore0"; |
| 478 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 479 | case Token::BIT_NOT: |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 480 | return overwrite_ == UNARY_OVERWRITE |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 481 | ? "GenericUnaryOpStub_BIT_NOT_Overwrite" |
| 482 | : "GenericUnaryOpStub_BIT_NOT_Alloc"; |
| 483 | default: |
| 484 | UNREACHABLE(); |
| 485 | return "<unknown>"; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 490 | void ArgumentsAccessStub::Generate(MacroAssembler* masm) { |
| 491 | switch (type_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 492 | case READ_ELEMENT: GenerateReadElement(masm); break; |
| 493 | case NEW_OBJECT: GenerateNewObject(masm); break; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 498 | int CEntryStub::MinorKey() { |
| 499 | ASSERT(result_size_ <= 2); |
| 500 | #ifdef _WIN64 |
| 501 | return ExitFrameModeBits::encode(mode_) |
| 502 | | IndirectResultBits::encode(result_size_ > 1); |
| 503 | #else |
| 504 | return ExitFrameModeBits::encode(mode_); |
| 505 | #endif |
| 506 | } |
| 507 | |
| 508 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 509 | bool ApiGetterEntryStub::GetCustomCache(Code** code_out) { |
| 510 | Object* cache = info()->load_stub_cache(); |
| 511 | if (cache->IsUndefined()) { |
| 512 | return false; |
| 513 | } else { |
| 514 | *code_out = Code::cast(cache); |
| 515 | return true; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | |
| 520 | void ApiGetterEntryStub::SetCustomCache(Code* value) { |
| 521 | info()->set_load_stub_cache(value); |
| 522 | } |
| 523 | |
| 524 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 525 | } } // namespace v8::internal |