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" |
| 32 | #include "debug.h" |
| 33 | #include "oprofile-agent.h" |
| 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" |
| 40 | |
| 41 | namespace v8 { |
| 42 | namespace internal { |
| 43 | |
| 44 | |
| 45 | CodeGenerator* CodeGeneratorScope::top_ = NULL; |
| 46 | |
| 47 | |
| 48 | DeferredCode::DeferredCode() |
| 49 | : masm_(CodeGeneratorScope::Current()->masm()), |
| 50 | statement_position_(masm_->current_statement_position()), |
| 51 | position_(masm_->current_position()) { |
| 52 | ASSERT(statement_position_ != RelocInfo::kNoPosition); |
| 53 | ASSERT(position_ != RelocInfo::kNoPosition); |
| 54 | |
| 55 | CodeGeneratorScope::Current()->AddDeferred(this); |
| 56 | #ifdef DEBUG |
| 57 | comment_ = ""; |
| 58 | #endif |
| 59 | |
| 60 | // Copy the register locations from the code generator's frame. |
| 61 | // These are the registers that will be spilled on entry to the |
| 62 | // deferred code and restored on exit. |
| 63 | VirtualFrame* frame = CodeGeneratorScope::Current()->frame(); |
| 64 | int sp_offset = frame->fp_relative(frame->stack_pointer_); |
| 65 | for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
| 66 | int loc = frame->register_location(i); |
| 67 | if (loc == VirtualFrame::kIllegalIndex) { |
| 68 | registers_[i] = kIgnore; |
| 69 | } else if (frame->elements_[loc].is_synced()) { |
| 70 | // Needs to be restored on exit but not saved on entry. |
| 71 | registers_[i] = frame->fp_relative(loc) | kSyncedFlag; |
| 72 | } else { |
| 73 | int offset = frame->fp_relative(loc); |
| 74 | registers_[i] = (offset < sp_offset) ? kPush : offset; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
| 80 | void CodeGenerator::ProcessDeferred() { |
| 81 | while (!deferred_.is_empty()) { |
| 82 | DeferredCode* code = deferred_.RemoveLast(); |
| 83 | ASSERT(masm_ == code->masm()); |
| 84 | // Record position of deferred code stub. |
| 85 | masm_->RecordStatementPosition(code->statement_position()); |
| 86 | if (code->position() != RelocInfo::kNoPosition) { |
| 87 | masm_->RecordPosition(code->position()); |
| 88 | } |
| 89 | // Generate the code. |
| 90 | Comment cmnt(masm_, code->comment()); |
| 91 | masm_->bind(code->entry_label()); |
| 92 | code->SaveRegisters(); |
| 93 | code->Generate(); |
| 94 | code->RestoreRegisters(); |
| 95 | masm_->jmp(code->exit_label()); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | void CodeGenerator::SetFrame(VirtualFrame* new_frame, |
| 101 | RegisterFile* non_frame_registers) { |
| 102 | RegisterFile saved_counts; |
| 103 | if (has_valid_frame()) { |
| 104 | frame_->DetachFromCodeGenerator(); |
| 105 | // The remaining register reference counts are the non-frame ones. |
| 106 | allocator_->SaveTo(&saved_counts); |
| 107 | } |
| 108 | |
| 109 | if (new_frame != NULL) { |
| 110 | // Restore the non-frame register references that go with the new frame. |
| 111 | allocator_->RestoreFrom(non_frame_registers); |
| 112 | new_frame->AttachToCodeGenerator(); |
| 113 | } |
| 114 | |
| 115 | frame_ = new_frame; |
| 116 | saved_counts.CopyTo(non_frame_registers); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | void CodeGenerator::DeleteFrame() { |
| 121 | if (has_valid_frame()) { |
| 122 | frame_->DetachFromCodeGenerator(); |
| 123 | frame_ = NULL; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 128 | void CodeGenerator::MakeCodePrologue(FunctionLiteral* fun) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 129 | #ifdef DEBUG |
| 130 | bool print_source = false; |
| 131 | bool print_ast = false; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 132 | bool print_json_ast = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 133 | const char* ftype; |
| 134 | |
| 135 | if (Bootstrapper::IsActive()) { |
| 136 | print_source = FLAG_print_builtin_source; |
| 137 | print_ast = FLAG_print_builtin_ast; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 138 | print_json_ast = FLAG_print_builtin_json_ast; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 139 | ftype = "builtin"; |
| 140 | } else { |
| 141 | print_source = FLAG_print_source; |
| 142 | print_ast = FLAG_print_ast; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 143 | print_json_ast = FLAG_print_json_ast; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 144 | ftype = "user-defined"; |
| 145 | } |
| 146 | |
| 147 | if (FLAG_trace_codegen || print_source || print_ast) { |
| 148 | PrintF("*** Generate code for %s function: ", ftype); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 149 | fun->name()->ShortPrint(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 150 | PrintF(" ***\n"); |
| 151 | } |
| 152 | |
| 153 | if (print_source) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 154 | PrintF("--- Source from AST ---\n%s\n", PrettyPrinter().PrintProgram(fun)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | if (print_ast) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 158 | PrintF("--- AST ---\n%s\n", AstPrinter().PrintProgram(fun)); |
| 159 | } |
| 160 | |
| 161 | if (print_json_ast) { |
| 162 | JsonAstBuilder builder; |
| 163 | PrintF("%s", builder.BuildProgram(fun)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 164 | } |
| 165 | #endif // DEBUG |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 166 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 168 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 169 | Handle<Code> CodeGenerator::MakeCodeEpilogue(FunctionLiteral* fun, |
| 170 | MacroAssembler* masm, |
| 171 | Code::Flags flags, |
| 172 | Handle<Script> script) { |
| 173 | // Allocate and install the code. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 174 | CodeDesc desc; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 175 | masm->GetCode(&desc); |
| 176 | ZoneScopeInfo sinfo(fun->scope()); |
| 177 | Handle<Code> code = |
| 178 | Factory::NewCode(desc, &sinfo, flags, masm->CodeObject()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 179 | |
| 180 | // Add unresolved entries in the code to the fixup list. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 181 | Bootstrapper::AddFixup(*code, masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 182 | |
| 183 | #ifdef ENABLE_DISASSEMBLER |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 184 | bool print_code = Bootstrapper::IsActive() |
| 185 | ? FLAG_print_builtin_code |
| 186 | : FLAG_print_code; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | if (print_code) { |
| 188 | // Print the source code if available. |
| 189 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 190 | PrintF("--- Raw source ---\n"); |
| 191 | StringInputBuffer stream(String::cast(script->source())); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 192 | stream.Seek(fun->start_position()); |
| 193 | // fun->end_position() points to the last character in the stream. We |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 194 | // need to compensate by adding one to calculate the length. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 195 | int source_len = fun->end_position() - fun->start_position() + 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 196 | for (int i = 0; i < source_len; i++) { |
| 197 | if (stream.has_more()) PrintF("%c", stream.GetNext()); |
| 198 | } |
| 199 | PrintF("\n\n"); |
| 200 | } |
| 201 | PrintF("--- Code ---\n"); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 202 | code->Disassemble(*fun->name()->ToCString()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | } |
| 204 | #endif // ENABLE_DISASSEMBLER |
| 205 | |
| 206 | if (!code.is_null()) { |
| 207 | Counters::total_compiled_code_size.Increment(code->instruction_size()); |
| 208 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 209 | return code; |
| 210 | } |
| 211 | |
| 212 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 213 | // Generate the code. Takes a function literal, generates code for it, assemble |
| 214 | // all the pieces into a Code object. This function is only to be called by |
| 215 | // the compiler.cc code. |
| 216 | Handle<Code> CodeGenerator::MakeCode(FunctionLiteral* fun, |
| 217 | Handle<Script> script, |
| 218 | bool is_eval) { |
| 219 | MakeCodePrologue(fun); |
| 220 | // Generate code. |
| 221 | const int kInitialBufferSize = 4 * KB; |
| 222 | CodeGenerator cgen(kInitialBufferSize, script, is_eval); |
| 223 | CodeGeneratorScope scope(&cgen); |
| 224 | cgen.GenCode(fun); |
| 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); |
| 232 | return MakeCodeEpilogue(fun, cgen.masm(), flags, script); |
| 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); |
| 240 | if (!Logger::is_logging()) return false; |
| 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 | |
| 253 | // Sets the function info on a function. |
| 254 | // The start_position points to the first '(' character after the function name |
| 255 | // in the full script source. When counting characters in the script source the |
| 256 | // the first character is number 0 (not 1). |
| 257 | void CodeGenerator::SetFunctionInfo(Handle<JSFunction> fun, |
| 258 | FunctionLiteral* lit, |
| 259 | bool is_toplevel, |
| 260 | Handle<Script> script) { |
| 261 | fun->shared()->set_length(lit->num_parameters()); |
| 262 | fun->shared()->set_formal_parameter_count(lit->num_parameters()); |
| 263 | fun->shared()->set_script(*script); |
| 264 | fun->shared()->set_function_token_position(lit->function_token_position()); |
| 265 | fun->shared()->set_start_position(lit->start_position()); |
| 266 | fun->shared()->set_end_position(lit->end_position()); |
| 267 | fun->shared()->set_is_expression(lit->is_expression()); |
| 268 | fun->shared()->set_is_toplevel(is_toplevel); |
| 269 | fun->shared()->set_inferred_name(*lit->inferred_name()); |
| 270 | fun->shared()->SetThisPropertyAssignmentsInfo( |
| 271 | lit->has_only_this_property_assignments(), |
| 272 | lit->has_only_simple_this_property_assignments(), |
| 273 | *lit->this_property_assignments()); |
| 274 | } |
| 275 | |
| 276 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 277 | Handle<Code> CodeGenerator::ComputeLazyCompile(int argc) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 278 | CALL_HEAP_FUNCTION(StubCache::ComputeLazyCompile(argc), Code); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | Handle<JSFunction> CodeGenerator::BuildBoilerplate(FunctionLiteral* node) { |
| 283 | #ifdef DEBUG |
| 284 | // We should not try to compile the same function literal more than |
| 285 | // once. |
| 286 | node->mark_as_compiled(); |
| 287 | #endif |
| 288 | |
| 289 | // Determine if the function can be lazily compiled. This is |
| 290 | // necessary to allow some of our builtin JS files to be lazily |
| 291 | // compiled. These builtins cannot be handled lazily by the parser, |
| 292 | // since we have to know if a function uses the special natives |
| 293 | // syntax, which is something the parser records. |
| 294 | bool allow_lazy = node->AllowsLazyCompilation(); |
| 295 | |
| 296 | // Generate code |
| 297 | Handle<Code> code; |
| 298 | if (FLAG_lazy && allow_lazy) { |
| 299 | code = ComputeLazyCompile(node->num_parameters()); |
| 300 | } else { |
| 301 | // The bodies of function literals have not yet been visited by |
| 302 | // the AST optimizer/analyzer. |
| 303 | if (!Rewriter::Optimize(node)) { |
| 304 | return Handle<JSFunction>::null(); |
| 305 | } |
| 306 | |
| 307 | code = MakeCode(node, script_, false); |
| 308 | |
| 309 | // Check for stack-overflow exception. |
| 310 | if (code.is_null()) { |
| 311 | SetStackOverflow(); |
| 312 | return Handle<JSFunction>::null(); |
| 313 | } |
| 314 | |
| 315 | // Function compilation complete. |
| 316 | LOG(CodeCreateEvent(Logger::FUNCTION_TAG, *code, *node->name())); |
| 317 | |
| 318 | #ifdef ENABLE_OPROFILE_AGENT |
| 319 | OProfileAgent::CreateNativeCodeRegion(*node->name(), |
| 320 | code->instruction_start(), |
| 321 | code->instruction_size()); |
| 322 | #endif |
| 323 | } |
| 324 | |
| 325 | // Create a boilerplate function. |
| 326 | Handle<JSFunction> function = |
| 327 | Factory::NewFunctionBoilerplate(node->name(), |
| 328 | node->materialized_literal_count(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 329 | code); |
| 330 | CodeGenerator::SetFunctionInfo(function, node, false, script_); |
| 331 | |
| 332 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 333 | // Notify debugger that a new function has been added. |
| 334 | Debugger::OnNewFunction(function); |
| 335 | #endif |
| 336 | |
| 337 | // Set the expected number of properties for instances and return |
| 338 | // the resulting function. |
| 339 | SetExpectedNofPropertiesFromEstimate(function, |
| 340 | node->expected_property_count()); |
| 341 | return function; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | Handle<Code> CodeGenerator::ComputeCallInitialize( |
| 346 | int argc, |
| 347 | InLoopFlag in_loop) { |
| 348 | if (in_loop == IN_LOOP) { |
| 349 | // Force the creation of the corresponding stub outside loops, |
| 350 | // because it may be used when clearing the ICs later - it is |
| 351 | // possible for a series of IC transitions to lose the in-loop |
| 352 | // information, and the IC clearing code can't generate a stub |
| 353 | // that it needs so we need to ensure it is generated already. |
| 354 | ComputeCallInitialize(argc, NOT_IN_LOOP); |
| 355 | } |
| 356 | CALL_HEAP_FUNCTION(StubCache::ComputeCallInitialize(argc, in_loop), Code); |
| 357 | } |
| 358 | |
| 359 | |
| 360 | void CodeGenerator::ProcessDeclarations(ZoneList<Declaration*>* declarations) { |
| 361 | int length = declarations->length(); |
| 362 | int globals = 0; |
| 363 | for (int i = 0; i < length; i++) { |
| 364 | Declaration* node = declarations->at(i); |
| 365 | Variable* var = node->proxy()->var(); |
| 366 | Slot* slot = var->slot(); |
| 367 | |
| 368 | // If it was not possible to allocate the variable at compile |
| 369 | // time, we need to "declare" it at runtime to make sure it |
| 370 | // actually exists in the local context. |
| 371 | if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) { |
| 372 | VisitDeclaration(node); |
| 373 | } else { |
| 374 | // Count global variables and functions for later processing |
| 375 | globals++; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // Return in case of no declared global functions or variables. |
| 380 | if (globals == 0) return; |
| 381 | |
| 382 | // Compute array of global variable and function declarations. |
| 383 | Handle<FixedArray> array = Factory::NewFixedArray(2 * globals, TENURED); |
| 384 | for (int j = 0, i = 0; i < length; i++) { |
| 385 | Declaration* node = declarations->at(i); |
| 386 | Variable* var = node->proxy()->var(); |
| 387 | Slot* slot = var->slot(); |
| 388 | |
| 389 | if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) { |
| 390 | // Skip - already processed. |
| 391 | } else { |
| 392 | array->set(j++, *(var->name())); |
| 393 | if (node->fun() == NULL) { |
| 394 | if (var->mode() == Variable::CONST) { |
| 395 | // In case this is const property use the hole. |
| 396 | array->set_the_hole(j++); |
| 397 | } else { |
| 398 | array->set_undefined(j++); |
| 399 | } |
| 400 | } else { |
| 401 | Handle<JSFunction> function = BuildBoilerplate(node->fun()); |
| 402 | // Check for stack-overflow exception. |
| 403 | if (HasStackOverflow()) return; |
| 404 | array->set(j++, *function); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | // Invoke the platform-dependent code generator to do the actual |
| 410 | // declaration the global variables and functions. |
| 411 | DeclareGlobals(array); |
| 412 | } |
| 413 | |
| 414 | |
| 415 | |
| 416 | // Special cases: These 'runtime calls' manipulate the current |
| 417 | // frame and are only used 1 or two places, so we generate them |
| 418 | // inline instead of generating calls to them. They are used |
| 419 | // for implementing Function.prototype.call() and |
| 420 | // Function.prototype.apply(). |
| 421 | CodeGenerator::InlineRuntimeLUT CodeGenerator::kInlineRuntimeLUT[] = { |
| 422 | {&CodeGenerator::GenerateIsSmi, "_IsSmi"}, |
| 423 | {&CodeGenerator::GenerateIsNonNegativeSmi, "_IsNonNegativeSmi"}, |
| 424 | {&CodeGenerator::GenerateIsArray, "_IsArray"}, |
| 425 | {&CodeGenerator::GenerateIsConstructCall, "_IsConstructCall"}, |
| 426 | {&CodeGenerator::GenerateArgumentsLength, "_ArgumentsLength"}, |
| 427 | {&CodeGenerator::GenerateArgumentsAccess, "_Arguments"}, |
| 428 | {&CodeGenerator::GenerateClassOf, "_ClassOf"}, |
| 429 | {&CodeGenerator::GenerateValueOf, "_ValueOf"}, |
| 430 | {&CodeGenerator::GenerateSetValueOf, "_SetValueOf"}, |
| 431 | {&CodeGenerator::GenerateFastCharCodeAt, "_FastCharCodeAt"}, |
| 432 | {&CodeGenerator::GenerateObjectEquals, "_ObjectEquals"}, |
| 433 | {&CodeGenerator::GenerateLog, "_Log"}, |
| 434 | {&CodeGenerator::GenerateRandomPositiveSmi, "_RandomPositiveSmi"}, |
| 435 | {&CodeGenerator::GenerateMathSin, "_Math_sin"}, |
| 436 | {&CodeGenerator::GenerateMathCos, "_Math_cos"} |
| 437 | }; |
| 438 | |
| 439 | |
| 440 | CodeGenerator::InlineRuntimeLUT* CodeGenerator::FindInlineRuntimeLUT( |
| 441 | Handle<String> name) { |
| 442 | const int entries_count = |
| 443 | sizeof(kInlineRuntimeLUT) / sizeof(InlineRuntimeLUT); |
| 444 | for (int i = 0; i < entries_count; i++) { |
| 445 | InlineRuntimeLUT* entry = &kInlineRuntimeLUT[i]; |
| 446 | if (name->IsEqualTo(CStrVector(entry->name))) { |
| 447 | return entry; |
| 448 | } |
| 449 | } |
| 450 | return NULL; |
| 451 | } |
| 452 | |
| 453 | |
| 454 | bool CodeGenerator::CheckForInlineRuntimeCall(CallRuntime* node) { |
| 455 | ZoneList<Expression*>* args = node->arguments(); |
| 456 | Handle<String> name = node->name(); |
| 457 | if (name->length() > 0 && name->Get(0) == '_') { |
| 458 | InlineRuntimeLUT* entry = FindInlineRuntimeLUT(name); |
| 459 | if (entry != NULL) { |
| 460 | ((*this).*(entry->method))(args); |
| 461 | return true; |
| 462 | } |
| 463 | } |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | |
| 468 | bool CodeGenerator::PatchInlineRuntimeEntry(Handle<String> name, |
| 469 | const CodeGenerator::InlineRuntimeLUT& new_entry, |
| 470 | CodeGenerator::InlineRuntimeLUT* old_entry) { |
| 471 | InlineRuntimeLUT* entry = FindInlineRuntimeLUT(name); |
| 472 | if (entry == NULL) return false; |
| 473 | if (old_entry != NULL) { |
| 474 | old_entry->name = entry->name; |
| 475 | old_entry->method = entry->method; |
| 476 | } |
| 477 | entry->name = new_entry.name; |
| 478 | entry->method = new_entry.method; |
| 479 | return true; |
| 480 | } |
| 481 | |
| 482 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 483 | // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a |
| 484 | // known result for the test expression, with no side effects. |
| 485 | CodeGenerator::ConditionAnalysis CodeGenerator::AnalyzeCondition( |
| 486 | Expression* cond) { |
| 487 | if (cond == NULL) return ALWAYS_TRUE; |
| 488 | |
| 489 | Literal* lit = cond->AsLiteral(); |
| 490 | if (lit == NULL) return DONT_KNOW; |
| 491 | |
| 492 | if (lit->IsTrue()) { |
| 493 | return ALWAYS_TRUE; |
| 494 | } else if (lit->IsFalse()) { |
| 495 | return ALWAYS_FALSE; |
| 496 | } |
| 497 | |
| 498 | return DONT_KNOW; |
| 499 | } |
| 500 | |
| 501 | |
| 502 | void CodeGenerator::RecordPositions(MacroAssembler* masm, int pos) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 503 | if (pos != RelocInfo::kNoPosition) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 504 | masm->RecordStatementPosition(pos); |
| 505 | masm->RecordPosition(pos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
| 509 | |
| 510 | void CodeGenerator::CodeForFunctionPosition(FunctionLiteral* fun) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 511 | if (FLAG_debug_info) RecordPositions(masm(), fun->start_position()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | |
| 515 | void CodeGenerator::CodeForReturnPosition(FunctionLiteral* fun) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 516 | if (FLAG_debug_info) RecordPositions(masm(), fun->end_position()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | |
| 520 | void CodeGenerator::CodeForStatementPosition(Statement* stmt) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame^] | 521 | if (FLAG_debug_info) RecordPositions(masm(), stmt->statement_pos()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | |
| 525 | void CodeGenerator::CodeForSourcePosition(int pos) { |
| 526 | if (FLAG_debug_info && pos != RelocInfo::kNoPosition) { |
| 527 | masm()->RecordPosition(pos); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | |
| 532 | const char* RuntimeStub::GetName() { |
| 533 | return Runtime::FunctionForId(id_)->stub_name; |
| 534 | } |
| 535 | |
| 536 | |
| 537 | void RuntimeStub::Generate(MacroAssembler* masm) { |
| 538 | Runtime::Function* f = Runtime::FunctionForId(id_); |
| 539 | masm->TailCallRuntime(ExternalReference(f), |
| 540 | num_arguments_, |
| 541 | f->result_size); |
| 542 | } |
| 543 | |
| 544 | |
| 545 | void ArgumentsAccessStub::Generate(MacroAssembler* masm) { |
| 546 | switch (type_) { |
| 547 | case READ_LENGTH: GenerateReadLength(masm); break; |
| 548 | case READ_ELEMENT: GenerateReadElement(masm); break; |
| 549 | case NEW_OBJECT: GenerateNewObject(masm); break; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | |
| 554 | } } // namespace v8::internal |