Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "bootstrapper.h" |
| 31 | #include "codegen-inl.h" |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 32 | #include "compiler.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | #include "debug.h" |
| 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. |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 73 | masm_->positions_recorder()->RecordStatementPosition( |
| 74 | code->statement_position()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 75 | if (code->position() != RelocInfo::kNoPosition) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 76 | masm_->positions_recorder()->RecordPosition(code->position()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 77 | } |
| 78 | // Generate the code. |
| 79 | Comment cmnt(masm_, code->comment()); |
| 80 | masm_->bind(code->entry_label()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 81 | if (code->AutoSaveAndRestore()) { |
| 82 | code->SaveRegisters(); |
| 83 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 84 | code->Generate(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 85 | if (code->AutoSaveAndRestore()) { |
| 86 | code->RestoreRegisters(); |
| 87 | code->Exit(); |
| 88 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 93 | void DeferredCode::Exit() { |
| 94 | masm_->jmp(exit_label()); |
| 95 | } |
| 96 | |
| 97 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 98 | void CodeGenerator::SetFrame(VirtualFrame* new_frame, |
| 99 | RegisterFile* non_frame_registers) { |
| 100 | RegisterFile saved_counts; |
| 101 | if (has_valid_frame()) { |
| 102 | frame_->DetachFromCodeGenerator(); |
| 103 | // The remaining register reference counts are the non-frame ones. |
| 104 | allocator_->SaveTo(&saved_counts); |
| 105 | } |
| 106 | |
| 107 | if (new_frame != NULL) { |
| 108 | // Restore the non-frame register references that go with the new frame. |
| 109 | allocator_->RestoreFrom(non_frame_registers); |
| 110 | new_frame->AttachToCodeGenerator(); |
| 111 | } |
| 112 | |
| 113 | frame_ = new_frame; |
| 114 | saved_counts.CopyTo(non_frame_registers); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | void CodeGenerator::DeleteFrame() { |
| 119 | if (has_valid_frame()) { |
| 120 | frame_->DetachFromCodeGenerator(); |
| 121 | frame_ = NULL; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 126 | void CodeGenerator::MakeCodePrologue(CompilationInfo* info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 127 | #ifdef DEBUG |
| 128 | bool print_source = false; |
| 129 | bool print_ast = false; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 130 | bool print_json_ast = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 131 | const char* ftype; |
| 132 | |
| 133 | if (Bootstrapper::IsActive()) { |
| 134 | print_source = FLAG_print_builtin_source; |
| 135 | print_ast = FLAG_print_builtin_ast; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 136 | print_json_ast = FLAG_print_builtin_json_ast; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 137 | ftype = "builtin"; |
| 138 | } else { |
| 139 | print_source = FLAG_print_source; |
| 140 | print_ast = FLAG_print_ast; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 141 | print_json_ast = FLAG_print_json_ast; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 142 | ftype = "user-defined"; |
| 143 | } |
| 144 | |
| 145 | if (FLAG_trace_codegen || print_source || print_ast) { |
| 146 | PrintF("*** Generate code for %s function: ", ftype); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 147 | info->function()->name()->ShortPrint(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 148 | PrintF(" ***\n"); |
| 149 | } |
| 150 | |
| 151 | if (print_source) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 152 | PrintF("--- Source from AST ---\n%s\n", |
| 153 | PrettyPrinter().PrintProgram(info->function())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | if (print_ast) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 157 | PrintF("--- AST ---\n%s\n", |
| 158 | AstPrinter().PrintProgram(info->function())); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | if (print_json_ast) { |
| 162 | JsonAstBuilder builder; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 163 | PrintF("%s", builder.BuildProgram(info->function())); |
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 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 169 | Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm, |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 170 | Code::Flags flags, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 171 | CompilationInfo* info) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 172 | // Allocate and install the code. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 173 | CodeDesc desc; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 174 | masm->GetCode(&desc); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 175 | Handle<Code> code = Factory::NewCode(desc, flags, masm->CodeObject()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 176 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | #ifdef ENABLE_DISASSEMBLER |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 178 | bool print_code = Bootstrapper::IsActive() |
| 179 | ? FLAG_print_builtin_code |
| 180 | : FLAG_print_code; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 181 | if (print_code) { |
| 182 | // Print the source code if available. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 183 | Handle<Script> script = info->script(); |
| 184 | FunctionLiteral* function = info->function(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 185 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 186 | PrintF("--- Raw source ---\n"); |
| 187 | StringInputBuffer stream(String::cast(script->source())); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 188 | stream.Seek(function->start_position()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 189 | // fun->end_position() points to the last character in the stream. We |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 190 | // need to compensate by adding one to calculate the length. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 191 | int source_len = |
| 192 | function->end_position() - function->start_position() + 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 193 | for (int i = 0; i < source_len; i++) { |
| 194 | if (stream.has_more()) PrintF("%c", stream.GetNext()); |
| 195 | } |
| 196 | PrintF("\n\n"); |
| 197 | } |
| 198 | PrintF("--- Code ---\n"); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 199 | code->Disassemble(*function->name()->ToCString()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 200 | } |
| 201 | #endif // ENABLE_DISASSEMBLER |
| 202 | |
| 203 | if (!code.is_null()) { |
| 204 | Counters::total_compiled_code_size.Increment(code->instruction_size()); |
| 205 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 206 | return code; |
| 207 | } |
| 208 | |
| 209 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 210 | // Generate the code. Compile the AST and assemble all the pieces into a |
| 211 | // Code object. |
| 212 | bool CodeGenerator::MakeCode(CompilationInfo* info) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 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()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 227 | return false; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 230 | InLoopFlag in_loop = info->is_in_loop() ? IN_LOOP : NOT_IN_LOOP; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 231 | Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, in_loop); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 232 | Handle<Code> code = MakeCodeEpilogue(cgen.masm(), flags, info); |
| 233 | info->SetCode(code); // May be an empty handle. |
| 234 | return !code.is_null(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 238 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 239 | |
| 240 | bool CodeGenerator::ShouldGenerateLog(Expression* type) { |
| 241 | ASSERT(type != NULL); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 242 | if (!Logger::is_logging() && !CpuProfiler::is_profiling()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 243 | Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle()); |
| 244 | if (FLAG_log_regexp) { |
| 245 | static Vector<const char> kRegexp = CStrVector("regexp"); |
| 246 | if (name->IsEqualTo(kRegexp)) |
| 247 | return true; |
| 248 | } |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | #endif |
| 253 | |
| 254 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 255 | void CodeGenerator::ProcessDeclarations(ZoneList<Declaration*>* declarations) { |
| 256 | int length = declarations->length(); |
| 257 | int globals = 0; |
| 258 | for (int i = 0; i < length; i++) { |
| 259 | Declaration* node = declarations->at(i); |
| 260 | Variable* var = node->proxy()->var(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 261 | Slot* slot = var->AsSlot(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 262 | |
| 263 | // If it was not possible to allocate the variable at compile |
| 264 | // time, we need to "declare" it at runtime to make sure it |
| 265 | // actually exists in the local context. |
| 266 | if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) { |
| 267 | VisitDeclaration(node); |
| 268 | } else { |
| 269 | // Count global variables and functions for later processing |
| 270 | globals++; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // Return in case of no declared global functions or variables. |
| 275 | if (globals == 0) return; |
| 276 | |
| 277 | // Compute array of global variable and function declarations. |
| 278 | Handle<FixedArray> array = Factory::NewFixedArray(2 * globals, TENURED); |
| 279 | for (int j = 0, i = 0; i < length; i++) { |
| 280 | Declaration* node = declarations->at(i); |
| 281 | Variable* var = node->proxy()->var(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 282 | Slot* slot = var->AsSlot(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 283 | |
| 284 | if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) { |
| 285 | // Skip - already processed. |
| 286 | } else { |
| 287 | array->set(j++, *(var->name())); |
| 288 | if (node->fun() == NULL) { |
| 289 | if (var->mode() == Variable::CONST) { |
| 290 | // In case this is const property use the hole. |
| 291 | array->set_the_hole(j++); |
| 292 | } else { |
| 293 | array->set_undefined(j++); |
| 294 | } |
| 295 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 296 | Handle<SharedFunctionInfo> function = |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 297 | Compiler::BuildFunctionInfo(node->fun(), script()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 298 | // Check for stack-overflow exception. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 299 | if (function.is_null()) { |
| 300 | SetStackOverflow(); |
| 301 | return; |
| 302 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 303 | array->set(j++, *function); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // Invoke the platform-dependent code generator to do the actual |
| 309 | // declaration the global variables and functions. |
| 310 | DeclareGlobals(array); |
| 311 | } |
| 312 | |
| 313 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 314 | void CodeGenerator::VisitIncrementOperation(IncrementOperation* expr) { |
| 315 | UNREACHABLE(); |
| 316 | } |
| 317 | |
| 318 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 319 | // Lookup table for code generators for special runtime calls which are |
| 320 | // generated inline. |
| 321 | #define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \ |
| 322 | &CodeGenerator::Generate##Name, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 323 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 324 | const CodeGenerator::InlineFunctionGenerator |
| 325 | CodeGenerator::kInlineFunctionGenerators[] = { |
| 326 | INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS) |
| 327 | INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 328 | }; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 329 | #undef INLINE_FUNCTION_GENERATOR_ADDRESS |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 330 | |
| 331 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 332 | bool CodeGenerator::CheckForInlineRuntimeCall(CallRuntime* node) { |
| 333 | ZoneList<Expression*>* args = node->arguments(); |
| 334 | Handle<String> name = node->name(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 335 | Runtime::Function* function = node->function(); |
| 336 | if (function != NULL && function->intrinsic_type == Runtime::INLINE) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 337 | int lookup_index = static_cast<int>(function->function_id) - |
| 338 | static_cast<int>(Runtime::kFirstInlineFunction); |
| 339 | ASSERT(lookup_index >= 0); |
| 340 | ASSERT(static_cast<size_t>(lookup_index) < |
| 341 | ARRAY_SIZE(kInlineFunctionGenerators)); |
| 342 | InlineFunctionGenerator generator = kInlineFunctionGenerators[lookup_index]; |
| 343 | (this->*generator)(args); |
| 344 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 345 | } |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 350 | // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a |
| 351 | // known result for the test expression, with no side effects. |
| 352 | CodeGenerator::ConditionAnalysis CodeGenerator::AnalyzeCondition( |
| 353 | Expression* cond) { |
| 354 | if (cond == NULL) return ALWAYS_TRUE; |
| 355 | |
| 356 | Literal* lit = cond->AsLiteral(); |
| 357 | if (lit == NULL) return DONT_KNOW; |
| 358 | |
| 359 | if (lit->IsTrue()) { |
| 360 | return ALWAYS_TRUE; |
| 361 | } else if (lit->IsFalse()) { |
| 362 | return ALWAYS_FALSE; |
| 363 | } |
| 364 | |
| 365 | return DONT_KNOW; |
| 366 | } |
| 367 | |
| 368 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 369 | bool CodeGenerator::RecordPositions(MacroAssembler* masm, |
| 370 | int pos, |
| 371 | bool right_here) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 372 | if (pos != RelocInfo::kNoPosition) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 373 | masm->positions_recorder()->RecordStatementPosition(pos); |
| 374 | masm->positions_recorder()->RecordPosition(pos); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 375 | if (right_here) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 376 | return masm->positions_recorder()->WriteRecordedPositions(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 377 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 378 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 379 | return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | |
| 383 | void CodeGenerator::CodeForFunctionPosition(FunctionLiteral* fun) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 384 | if (FLAG_debug_info) RecordPositions(masm(), fun->start_position(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | |
| 388 | void CodeGenerator::CodeForReturnPosition(FunctionLiteral* fun) { |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 389 | if (FLAG_debug_info) RecordPositions(masm(), fun->end_position() - 1, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | |
| 393 | void CodeGenerator::CodeForStatementPosition(Statement* stmt) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 394 | if (FLAG_debug_info) RecordPositions(masm(), stmt->statement_pos(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 397 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 398 | void CodeGenerator::CodeForDoWhileConditionPosition(DoWhileStatement* stmt) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 399 | if (FLAG_debug_info) |
| 400 | RecordPositions(masm(), stmt->condition_position(), false); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 401 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 402 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 403 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 404 | void CodeGenerator::CodeForSourcePosition(int pos) { |
| 405 | if (FLAG_debug_info && pos != RelocInfo::kNoPosition) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 406 | masm()->positions_recorder()->RecordPosition(pos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
| 410 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 411 | const char* GenericUnaryOpStub::GetName() { |
| 412 | switch (op_) { |
| 413 | case Token::SUB: |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 414 | if (negative_zero_ == kStrictNegativeZero) { |
| 415 | return overwrite_ == UNARY_OVERWRITE |
| 416 | ? "GenericUnaryOpStub_SUB_Overwrite_Strict0" |
| 417 | : "GenericUnaryOpStub_SUB_Alloc_Strict0"; |
| 418 | } else { |
| 419 | return overwrite_ == UNARY_OVERWRITE |
| 420 | ? "GenericUnaryOpStub_SUB_Overwrite_Ignore0" |
| 421 | : "GenericUnaryOpStub_SUB_Alloc_Ignore0"; |
| 422 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 423 | case Token::BIT_NOT: |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 424 | return overwrite_ == UNARY_OVERWRITE |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 425 | ? "GenericUnaryOpStub_BIT_NOT_Overwrite" |
| 426 | : "GenericUnaryOpStub_BIT_NOT_Alloc"; |
| 427 | default: |
| 428 | UNREACHABLE(); |
| 429 | return "<unknown>"; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 434 | void ArgumentsAccessStub::Generate(MacroAssembler* masm) { |
| 435 | switch (type_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 436 | case READ_ELEMENT: GenerateReadElement(masm); break; |
| 437 | case NEW_OBJECT: GenerateNewObject(masm); break; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 442 | int CEntryStub::MinorKey() { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 443 | ASSERT(result_size_ == 1 || result_size_ == 2); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 444 | #ifdef _WIN64 |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 445 | return result_size_ == 1 ? 0 : 1; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 446 | #else |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 447 | return 0; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 448 | #endif |
| 449 | } |
| 450 | |
| 451 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 452 | } } // namespace v8::internal |