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" |
| 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 | |
| 67 | DeferredCode::DeferredCode() |
| 68 | : masm_(CodeGeneratorScope::Current()->masm()), |
| 69 | statement_position_(masm_->current_statement_position()), |
| 70 | position_(masm_->current_position()) { |
| 71 | ASSERT(statement_position_ != RelocInfo::kNoPosition); |
| 72 | ASSERT(position_ != RelocInfo::kNoPosition); |
| 73 | |
| 74 | CodeGeneratorScope::Current()->AddDeferred(this); |
| 75 | #ifdef DEBUG |
| 76 | comment_ = ""; |
| 77 | #endif |
| 78 | |
| 79 | // Copy the register locations from the code generator's frame. |
| 80 | // These are the registers that will be spilled on entry to the |
| 81 | // deferred code and restored on exit. |
| 82 | VirtualFrame* frame = CodeGeneratorScope::Current()->frame(); |
| 83 | int sp_offset = frame->fp_relative(frame->stack_pointer_); |
| 84 | for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
| 85 | int loc = frame->register_location(i); |
| 86 | if (loc == VirtualFrame::kIllegalIndex) { |
| 87 | registers_[i] = kIgnore; |
| 88 | } else if (frame->elements_[loc].is_synced()) { |
| 89 | // Needs to be restored on exit but not saved on entry. |
| 90 | registers_[i] = frame->fp_relative(loc) | kSyncedFlag; |
| 91 | } else { |
| 92 | int offset = frame->fp_relative(loc); |
| 93 | registers_[i] = (offset < sp_offset) ? kPush : offset; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | |
| 99 | void CodeGenerator::ProcessDeferred() { |
| 100 | while (!deferred_.is_empty()) { |
| 101 | DeferredCode* code = deferred_.RemoveLast(); |
| 102 | ASSERT(masm_ == code->masm()); |
| 103 | // Record position of deferred code stub. |
| 104 | masm_->RecordStatementPosition(code->statement_position()); |
| 105 | if (code->position() != RelocInfo::kNoPosition) { |
| 106 | masm_->RecordPosition(code->position()); |
| 107 | } |
| 108 | // Generate the code. |
| 109 | Comment cmnt(masm_, code->comment()); |
| 110 | masm_->bind(code->entry_label()); |
| 111 | code->SaveRegisters(); |
| 112 | code->Generate(); |
| 113 | code->RestoreRegisters(); |
| 114 | masm_->jmp(code->exit_label()); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
| 119 | void CodeGenerator::SetFrame(VirtualFrame* new_frame, |
| 120 | RegisterFile* non_frame_registers) { |
| 121 | RegisterFile saved_counts; |
| 122 | if (has_valid_frame()) { |
| 123 | frame_->DetachFromCodeGenerator(); |
| 124 | // The remaining register reference counts are the non-frame ones. |
| 125 | allocator_->SaveTo(&saved_counts); |
| 126 | } |
| 127 | |
| 128 | if (new_frame != NULL) { |
| 129 | // Restore the non-frame register references that go with the new frame. |
| 130 | allocator_->RestoreFrom(non_frame_registers); |
| 131 | new_frame->AttachToCodeGenerator(); |
| 132 | } |
| 133 | |
| 134 | frame_ = new_frame; |
| 135 | saved_counts.CopyTo(non_frame_registers); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | void CodeGenerator::DeleteFrame() { |
| 140 | if (has_valid_frame()) { |
| 141 | frame_->DetachFromCodeGenerator(); |
| 142 | frame_ = NULL; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 147 | void CodeGenerator::MakeCodePrologue(CompilationInfo* info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 148 | #ifdef DEBUG |
| 149 | bool print_source = false; |
| 150 | bool print_ast = false; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 151 | bool print_json_ast = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 152 | const char* ftype; |
| 153 | |
| 154 | if (Bootstrapper::IsActive()) { |
| 155 | print_source = FLAG_print_builtin_source; |
| 156 | print_ast = FLAG_print_builtin_ast; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 157 | print_json_ast = FLAG_print_builtin_json_ast; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 158 | ftype = "builtin"; |
| 159 | } else { |
| 160 | print_source = FLAG_print_source; |
| 161 | print_ast = FLAG_print_ast; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 162 | print_json_ast = FLAG_print_json_ast; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 163 | ftype = "user-defined"; |
| 164 | } |
| 165 | |
| 166 | if (FLAG_trace_codegen || print_source || print_ast) { |
| 167 | PrintF("*** Generate code for %s function: ", ftype); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 168 | info->function()->name()->ShortPrint(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 169 | PrintF(" ***\n"); |
| 170 | } |
| 171 | |
| 172 | if (print_source) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 173 | PrintF("--- Source from AST ---\n%s\n", |
| 174 | PrettyPrinter().PrintProgram(info->function())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | if (print_ast) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 178 | PrintF("--- AST ---\n%s\n", |
| 179 | AstPrinter().PrintProgram(info->function())); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | if (print_json_ast) { |
| 183 | JsonAstBuilder builder; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 184 | PrintF("%s", builder.BuildProgram(info->function())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 185 | } |
| 186 | #endif // DEBUG |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 187 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 188 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 189 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 190 | Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm, |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 191 | Code::Flags flags, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 192 | CompilationInfo* info) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 193 | // Allocate and install the code. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 194 | CodeDesc desc; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 195 | masm->GetCode(&desc); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 196 | ZoneScopeInfo sinfo(info->scope()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 197 | Handle<Code> code = |
| 198 | Factory::NewCode(desc, &sinfo, flags, masm->CodeObject()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 199 | |
| 200 | // Add unresolved entries in the code to the fixup list. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 201 | Bootstrapper::AddFixup(*code, masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 202 | |
| 203 | #ifdef ENABLE_DISASSEMBLER |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 204 | bool print_code = Bootstrapper::IsActive() |
| 205 | ? FLAG_print_builtin_code |
| 206 | : FLAG_print_code; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 207 | if (print_code) { |
| 208 | // Print the source code if available. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 209 | Handle<Script> script = info->script(); |
| 210 | FunctionLiteral* function = info->function(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 211 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 212 | PrintF("--- Raw source ---\n"); |
| 213 | StringInputBuffer stream(String::cast(script->source())); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 214 | stream.Seek(function->start_position()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 215 | // fun->end_position() points to the last character in the stream. We |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 216 | // need to compensate by adding one to calculate the length. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 217 | int source_len = |
| 218 | function->end_position() - function->start_position() + 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 219 | for (int i = 0; i < source_len; i++) { |
| 220 | if (stream.has_more()) PrintF("%c", stream.GetNext()); |
| 221 | } |
| 222 | PrintF("\n\n"); |
| 223 | } |
| 224 | PrintF("--- Code ---\n"); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 225 | code->Disassemble(*function->name()->ToCString()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 226 | } |
| 227 | #endif // ENABLE_DISASSEMBLER |
| 228 | |
| 229 | if (!code.is_null()) { |
| 230 | Counters::total_compiled_code_size.Increment(code->instruction_size()); |
| 231 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 232 | return code; |
| 233 | } |
| 234 | |
| 235 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 236 | // Generate the code. Takes a function literal, generates code for it, assemble |
| 237 | // all the pieces into a Code object. This function is only to be called by |
| 238 | // the compiler.cc code. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 239 | Handle<Code> CodeGenerator::MakeCode(CompilationInfo* info) { |
| 240 | Handle<Script> script = info->script(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 241 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 242 | int len = String::cast(script->source())->length(); |
| 243 | Counters::total_old_codegen_source_size.Increment(len); |
| 244 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 245 | MakeCodePrologue(info); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 246 | // Generate code. |
| 247 | const int kInitialBufferSize = 4 * KB; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 248 | MacroAssembler masm(NULL, kInitialBufferSize); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 249 | CodeGenerator cgen(&masm); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 250 | CodeGeneratorScope scope(&cgen); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 251 | cgen.Generate(info, PRIMARY); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 252 | if (cgen.HasStackOverflow()) { |
| 253 | ASSERT(!Top::has_pending_exception()); |
| 254 | return Handle<Code>::null(); |
| 255 | } |
| 256 | |
| 257 | InLoopFlag in_loop = (cgen.loop_nesting() != 0) ? IN_LOOP : NOT_IN_LOOP; |
| 258 | Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, in_loop); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 259 | return MakeCodeEpilogue(cgen.masm(), flags, info); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 263 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 264 | |
| 265 | bool CodeGenerator::ShouldGenerateLog(Expression* type) { |
| 266 | ASSERT(type != NULL); |
| 267 | if (!Logger::is_logging()) return false; |
| 268 | Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle()); |
| 269 | if (FLAG_log_regexp) { |
| 270 | static Vector<const char> kRegexp = CStrVector("regexp"); |
| 271 | if (name->IsEqualTo(kRegexp)) |
| 272 | return true; |
| 273 | } |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | #endif |
| 278 | |
| 279 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 280 | Handle<Code> CodeGenerator::ComputeCallInitialize( |
| 281 | int argc, |
| 282 | InLoopFlag in_loop) { |
| 283 | if (in_loop == IN_LOOP) { |
| 284 | // Force the creation of the corresponding stub outside loops, |
| 285 | // because it may be used when clearing the ICs later - it is |
| 286 | // possible for a series of IC transitions to lose the in-loop |
| 287 | // information, and the IC clearing code can't generate a stub |
| 288 | // that it needs so we need to ensure it is generated already. |
| 289 | ComputeCallInitialize(argc, NOT_IN_LOOP); |
| 290 | } |
| 291 | CALL_HEAP_FUNCTION(StubCache::ComputeCallInitialize(argc, in_loop), Code); |
| 292 | } |
| 293 | |
| 294 | |
| 295 | void CodeGenerator::ProcessDeclarations(ZoneList<Declaration*>* declarations) { |
| 296 | int length = declarations->length(); |
| 297 | int globals = 0; |
| 298 | for (int i = 0; i < length; i++) { |
| 299 | Declaration* node = declarations->at(i); |
| 300 | Variable* var = node->proxy()->var(); |
| 301 | Slot* slot = var->slot(); |
| 302 | |
| 303 | // If it was not possible to allocate the variable at compile |
| 304 | // time, we need to "declare" it at runtime to make sure it |
| 305 | // actually exists in the local context. |
| 306 | if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) { |
| 307 | VisitDeclaration(node); |
| 308 | } else { |
| 309 | // Count global variables and functions for later processing |
| 310 | globals++; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // Return in case of no declared global functions or variables. |
| 315 | if (globals == 0) return; |
| 316 | |
| 317 | // Compute array of global variable and function declarations. |
| 318 | Handle<FixedArray> array = Factory::NewFixedArray(2 * globals, TENURED); |
| 319 | for (int j = 0, i = 0; i < length; i++) { |
| 320 | Declaration* node = declarations->at(i); |
| 321 | Variable* var = node->proxy()->var(); |
| 322 | Slot* slot = var->slot(); |
| 323 | |
| 324 | if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) { |
| 325 | // Skip - already processed. |
| 326 | } else { |
| 327 | array->set(j++, *(var->name())); |
| 328 | if (node->fun() == NULL) { |
| 329 | if (var->mode() == Variable::CONST) { |
| 330 | // In case this is const property use the hole. |
| 331 | array->set_the_hole(j++); |
| 332 | } else { |
| 333 | array->set_undefined(j++); |
| 334 | } |
| 335 | } else { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 336 | Handle<JSFunction> function = |
| 337 | Compiler::BuildBoilerplate(node->fun(), script(), this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 338 | // Check for stack-overflow exception. |
| 339 | if (HasStackOverflow()) return; |
| 340 | array->set(j++, *function); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // Invoke the platform-dependent code generator to do the actual |
| 346 | // declaration the global variables and functions. |
| 347 | DeclareGlobals(array); |
| 348 | } |
| 349 | |
| 350 | |
| 351 | |
| 352 | // Special cases: These 'runtime calls' manipulate the current |
| 353 | // frame and are only used 1 or two places, so we generate them |
| 354 | // inline instead of generating calls to them. They are used |
| 355 | // for implementing Function.prototype.call() and |
| 356 | // Function.prototype.apply(). |
| 357 | CodeGenerator::InlineRuntimeLUT CodeGenerator::kInlineRuntimeLUT[] = { |
| 358 | {&CodeGenerator::GenerateIsSmi, "_IsSmi"}, |
| 359 | {&CodeGenerator::GenerateIsNonNegativeSmi, "_IsNonNegativeSmi"}, |
| 360 | {&CodeGenerator::GenerateIsArray, "_IsArray"}, |
| 361 | {&CodeGenerator::GenerateIsConstructCall, "_IsConstructCall"}, |
| 362 | {&CodeGenerator::GenerateArgumentsLength, "_ArgumentsLength"}, |
| 363 | {&CodeGenerator::GenerateArgumentsAccess, "_Arguments"}, |
| 364 | {&CodeGenerator::GenerateClassOf, "_ClassOf"}, |
| 365 | {&CodeGenerator::GenerateValueOf, "_ValueOf"}, |
| 366 | {&CodeGenerator::GenerateSetValueOf, "_SetValueOf"}, |
| 367 | {&CodeGenerator::GenerateFastCharCodeAt, "_FastCharCodeAt"}, |
| 368 | {&CodeGenerator::GenerateObjectEquals, "_ObjectEquals"}, |
| 369 | {&CodeGenerator::GenerateLog, "_Log"}, |
| 370 | {&CodeGenerator::GenerateRandomPositiveSmi, "_RandomPositiveSmi"}, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 371 | {&CodeGenerator::GenerateIsObject, "_IsObject"}, |
| 372 | {&CodeGenerator::GenerateIsFunction, "_IsFunction"}, |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 373 | {&CodeGenerator::GenerateIsUndetectableObject, "_IsUndetectableObject"}, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 374 | {&CodeGenerator::GenerateStringAdd, "_StringAdd"}, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 375 | {&CodeGenerator::GenerateSubString, "_SubString"}, |
| 376 | {&CodeGenerator::GenerateStringCompare, "_StringCompare"}, |
| 377 | {&CodeGenerator::GenerateRegExpExec, "_RegExpExec"}, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 378 | }; |
| 379 | |
| 380 | |
| 381 | CodeGenerator::InlineRuntimeLUT* CodeGenerator::FindInlineRuntimeLUT( |
| 382 | Handle<String> name) { |
| 383 | const int entries_count = |
| 384 | sizeof(kInlineRuntimeLUT) / sizeof(InlineRuntimeLUT); |
| 385 | for (int i = 0; i < entries_count; i++) { |
| 386 | InlineRuntimeLUT* entry = &kInlineRuntimeLUT[i]; |
| 387 | if (name->IsEqualTo(CStrVector(entry->name))) { |
| 388 | return entry; |
| 389 | } |
| 390 | } |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | |
| 395 | bool CodeGenerator::CheckForInlineRuntimeCall(CallRuntime* node) { |
| 396 | ZoneList<Expression*>* args = node->arguments(); |
| 397 | Handle<String> name = node->name(); |
| 398 | if (name->length() > 0 && name->Get(0) == '_') { |
| 399 | InlineRuntimeLUT* entry = FindInlineRuntimeLUT(name); |
| 400 | if (entry != NULL) { |
| 401 | ((*this).*(entry->method))(args); |
| 402 | return true; |
| 403 | } |
| 404 | } |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | |
| 409 | bool CodeGenerator::PatchInlineRuntimeEntry(Handle<String> name, |
| 410 | const CodeGenerator::InlineRuntimeLUT& new_entry, |
| 411 | CodeGenerator::InlineRuntimeLUT* old_entry) { |
| 412 | InlineRuntimeLUT* entry = FindInlineRuntimeLUT(name); |
| 413 | if (entry == NULL) return false; |
| 414 | if (old_entry != NULL) { |
| 415 | old_entry->name = entry->name; |
| 416 | old_entry->method = entry->method; |
| 417 | } |
| 418 | entry->name = new_entry.name; |
| 419 | entry->method = new_entry.method; |
| 420 | return true; |
| 421 | } |
| 422 | |
| 423 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 424 | // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a |
| 425 | // known result for the test expression, with no side effects. |
| 426 | CodeGenerator::ConditionAnalysis CodeGenerator::AnalyzeCondition( |
| 427 | Expression* cond) { |
| 428 | if (cond == NULL) return ALWAYS_TRUE; |
| 429 | |
| 430 | Literal* lit = cond->AsLiteral(); |
| 431 | if (lit == NULL) return DONT_KNOW; |
| 432 | |
| 433 | if (lit->IsTrue()) { |
| 434 | return ALWAYS_TRUE; |
| 435 | } else if (lit->IsFalse()) { |
| 436 | return ALWAYS_FALSE; |
| 437 | } |
| 438 | |
| 439 | return DONT_KNOW; |
| 440 | } |
| 441 | |
| 442 | |
| 443 | void CodeGenerator::RecordPositions(MacroAssembler* masm, int pos) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 444 | if (pos != RelocInfo::kNoPosition) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 445 | masm->RecordStatementPosition(pos); |
| 446 | masm->RecordPosition(pos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
| 450 | |
| 451 | void CodeGenerator::CodeForFunctionPosition(FunctionLiteral* fun) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 452 | if (FLAG_debug_info) RecordPositions(masm(), fun->start_position()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | |
| 456 | void CodeGenerator::CodeForReturnPosition(FunctionLiteral* fun) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 457 | if (FLAG_debug_info) RecordPositions(masm(), fun->end_position()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | |
| 461 | void CodeGenerator::CodeForStatementPosition(Statement* stmt) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 462 | if (FLAG_debug_info) RecordPositions(masm(), stmt->statement_pos()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 465 | void CodeGenerator::CodeForDoWhileConditionPosition(DoWhileStatement* stmt) { |
| 466 | if (FLAG_debug_info) RecordPositions(masm(), stmt->condition_position()); |
| 467 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 468 | |
| 469 | void CodeGenerator::CodeForSourcePosition(int pos) { |
| 470 | if (FLAG_debug_info && pos != RelocInfo::kNoPosition) { |
| 471 | masm()->RecordPosition(pos); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 476 | const char* GenericUnaryOpStub::GetName() { |
| 477 | switch (op_) { |
| 478 | case Token::SUB: |
| 479 | return overwrite_ |
| 480 | ? "GenericUnaryOpStub_SUB_Overwrite" |
| 481 | : "GenericUnaryOpStub_SUB_Alloc"; |
| 482 | case Token::BIT_NOT: |
| 483 | return overwrite_ |
| 484 | ? "GenericUnaryOpStub_BIT_NOT_Overwrite" |
| 485 | : "GenericUnaryOpStub_BIT_NOT_Alloc"; |
| 486 | default: |
| 487 | UNREACHABLE(); |
| 488 | return "<unknown>"; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 493 | void ArgumentsAccessStub::Generate(MacroAssembler* masm) { |
| 494 | switch (type_) { |
| 495 | case READ_LENGTH: GenerateReadLength(masm); break; |
| 496 | case READ_ELEMENT: GenerateReadElement(masm); break; |
| 497 | case NEW_OBJECT: GenerateNewObject(masm); break; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 502 | int CEntryStub::MinorKey() { |
| 503 | ASSERT(result_size_ <= 2); |
| 504 | #ifdef _WIN64 |
| 505 | return ExitFrameModeBits::encode(mode_) |
| 506 | | IndirectResultBits::encode(result_size_ > 1); |
| 507 | #else |
| 508 | return ExitFrameModeBits::encode(mode_); |
| 509 | #endif |
| 510 | } |
| 511 | |
| 512 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 513 | bool ApiGetterEntryStub::GetCustomCache(Code** code_out) { |
| 514 | Object* cache = info()->load_stub_cache(); |
| 515 | if (cache->IsUndefined()) { |
| 516 | return false; |
| 517 | } else { |
| 518 | *code_out = Code::cast(cache); |
| 519 | return true; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | |
| 524 | void ApiGetterEntryStub::SetCustomCache(Code* value) { |
| 525 | info()->set_load_stub_cache(value); |
| 526 | } |
| 527 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 528 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 529 | void DebuggerStatementStub::Generate(MacroAssembler* masm) { |
| 530 | Runtime::Function* f = Runtime::FunctionForId(Runtime::kDebugBreak); |
| 531 | masm->TailCallRuntime(ExternalReference(f), 0, f->result_size); |
| 532 | } |
| 533 | #endif |
| 534 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 535 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 536 | } } // namespace v8::internal |