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