| OLD | NEW |
| 1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 #include "prettyprinter.h" | 35 #include "prettyprinter.h" |
| 36 #include "register-allocator-inl.h" | 36 #include "register-allocator-inl.h" |
| 37 #include "rewriter.h" | 37 #include "rewriter.h" |
| 38 #include "runtime.h" | 38 #include "runtime.h" |
| 39 #include "scopeinfo.h" | 39 #include "scopeinfo.h" |
| 40 #include "stub-cache.h" | 40 #include "stub-cache.h" |
| 41 | 41 |
| 42 namespace v8 { | 42 namespace v8 { |
| 43 namespace internal { | 43 namespace internal { |
| 44 | 44 |
| 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 |
| 45 | 63 |
| 46 CodeGenerator* CodeGeneratorScope::top_ = NULL; | 64 CodeGenerator* CodeGeneratorScope::top_ = NULL; |
| 47 | 65 |
| 48 | 66 |
| 49 DeferredCode::DeferredCode() | 67 DeferredCode::DeferredCode() |
| 50 : masm_(CodeGeneratorScope::Current()->masm()), | 68 : masm_(CodeGeneratorScope::Current()->masm()), |
| 51 statement_position_(masm_->current_statement_position()), | 69 statement_position_(masm_->current_statement_position()), |
| 52 position_(masm_->current_position()) { | 70 position_(masm_->current_position()) { |
| 53 ASSERT(statement_position_ != RelocInfo::kNoPosition); | 71 ASSERT(statement_position_ != RelocInfo::kNoPosition); |
| 54 ASSERT(position_ != RelocInfo::kNoPosition); | 72 ASSERT(position_ != RelocInfo::kNoPosition); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 | 137 |
| 120 | 138 |
| 121 void CodeGenerator::DeleteFrame() { | 139 void CodeGenerator::DeleteFrame() { |
| 122 if (has_valid_frame()) { | 140 if (has_valid_frame()) { |
| 123 frame_->DetachFromCodeGenerator(); | 141 frame_->DetachFromCodeGenerator(); |
| 124 frame_ = NULL; | 142 frame_ = NULL; |
| 125 } | 143 } |
| 126 } | 144 } |
| 127 | 145 |
| 128 | 146 |
| 129 void CodeGenerator::MakeCodePrologue(FunctionLiteral* fun) { | 147 void CodeGenerator::MakeCodePrologue(CompilationInfo* info) { |
| 130 #ifdef DEBUG | 148 #ifdef DEBUG |
| 131 bool print_source = false; | 149 bool print_source = false; |
| 132 bool print_ast = false; | 150 bool print_ast = false; |
| 133 bool print_json_ast = false; | 151 bool print_json_ast = false; |
| 134 const char* ftype; | 152 const char* ftype; |
| 135 | 153 |
| 136 if (Bootstrapper::IsActive()) { | 154 if (Bootstrapper::IsActive()) { |
| 137 print_source = FLAG_print_builtin_source; | 155 print_source = FLAG_print_builtin_source; |
| 138 print_ast = FLAG_print_builtin_ast; | 156 print_ast = FLAG_print_builtin_ast; |
| 139 print_json_ast = FLAG_print_builtin_json_ast; | 157 print_json_ast = FLAG_print_builtin_json_ast; |
| 140 ftype = "builtin"; | 158 ftype = "builtin"; |
| 141 } else { | 159 } else { |
| 142 print_source = FLAG_print_source; | 160 print_source = FLAG_print_source; |
| 143 print_ast = FLAG_print_ast; | 161 print_ast = FLAG_print_ast; |
| 144 print_json_ast = FLAG_print_json_ast; | 162 print_json_ast = FLAG_print_json_ast; |
| 145 ftype = "user-defined"; | 163 ftype = "user-defined"; |
| 146 } | 164 } |
| 147 | 165 |
| 148 if (FLAG_trace_codegen || print_source || print_ast) { | 166 if (FLAG_trace_codegen || print_source || print_ast) { |
| 149 PrintF("*** Generate code for %s function: ", ftype); | 167 PrintF("*** Generate code for %s function: ", ftype); |
| 150 fun->name()->ShortPrint(); | 168 info->function()->name()->ShortPrint(); |
| 151 PrintF(" ***\n"); | 169 PrintF(" ***\n"); |
| 152 } | 170 } |
| 153 | 171 |
| 154 if (print_source) { | 172 if (print_source) { |
| 155 PrintF("--- Source from AST ---\n%s\n", PrettyPrinter().PrintProgram(fun)); | 173 PrintF("--- Source from AST ---\n%s\n", |
| 174 PrettyPrinter().PrintProgram(info->function())); |
| 156 } | 175 } |
| 157 | 176 |
| 158 if (print_ast) { | 177 if (print_ast) { |
| 159 PrintF("--- AST ---\n%s\n", AstPrinter().PrintProgram(fun)); | 178 PrintF("--- AST ---\n%s\n", |
| 179 AstPrinter().PrintProgram(info->function())); |
| 160 } | 180 } |
| 161 | 181 |
| 162 if (print_json_ast) { | 182 if (print_json_ast) { |
| 163 JsonAstBuilder builder; | 183 JsonAstBuilder builder; |
| 164 PrintF("%s", builder.BuildProgram(fun)); | 184 PrintF("%s", builder.BuildProgram(info->function())); |
| 165 } | 185 } |
| 166 #endif // DEBUG | 186 #endif // DEBUG |
| 167 } | 187 } |
| 168 | 188 |
| 169 | 189 |
| 170 Handle<Code> CodeGenerator::MakeCodeEpilogue(FunctionLiteral* fun, | 190 Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm, |
| 171 MacroAssembler* masm, | |
| 172 Code::Flags flags, | 191 Code::Flags flags, |
| 173 Handle<Script> script) { | 192 CompilationInfo* info) { |
| 174 // Allocate and install the code. | 193 // Allocate and install the code. |
| 175 CodeDesc desc; | 194 CodeDesc desc; |
| 176 masm->GetCode(&desc); | 195 masm->GetCode(&desc); |
| 177 ZoneScopeInfo sinfo(fun->scope()); | 196 ZoneScopeInfo sinfo(info->scope()); |
| 178 Handle<Code> code = | 197 Handle<Code> code = |
| 179 Factory::NewCode(desc, &sinfo, flags, masm->CodeObject()); | 198 Factory::NewCode(desc, &sinfo, flags, masm->CodeObject()); |
| 180 | 199 |
| 181 // Add unresolved entries in the code to the fixup list. | 200 // Add unresolved entries in the code to the fixup list. |
| 182 Bootstrapper::AddFixup(*code, masm); | 201 Bootstrapper::AddFixup(*code, masm); |
| 183 | 202 |
| 184 #ifdef ENABLE_DISASSEMBLER | 203 #ifdef ENABLE_DISASSEMBLER |
| 185 bool print_code = Bootstrapper::IsActive() | 204 bool print_code = Bootstrapper::IsActive() |
| 186 ? FLAG_print_builtin_code | 205 ? FLAG_print_builtin_code |
| 187 : FLAG_print_code; | 206 : FLAG_print_code; |
| 188 if (print_code) { | 207 if (print_code) { |
| 189 // Print the source code if available. | 208 // Print the source code if available. |
| 209 Handle<Script> script = info->script(); |
| 210 FunctionLiteral* function = info->function(); |
| 190 if (!script->IsUndefined() && !script->source()->IsUndefined()) { | 211 if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 191 PrintF("--- Raw source ---\n"); | 212 PrintF("--- Raw source ---\n"); |
| 192 StringInputBuffer stream(String::cast(script->source())); | 213 StringInputBuffer stream(String::cast(script->source())); |
| 193 stream.Seek(fun->start_position()); | 214 stream.Seek(function->start_position()); |
| 194 // fun->end_position() points to the last character in the stream. We | 215 // fun->end_position() points to the last character in the stream. We |
| 195 // need to compensate by adding one to calculate the length. | 216 // need to compensate by adding one to calculate the length. |
| 196 int source_len = fun->end_position() - fun->start_position() + 1; | 217 int source_len = |
| 218 function->end_position() - function->start_position() + 1; |
| 197 for (int i = 0; i < source_len; i++) { | 219 for (int i = 0; i < source_len; i++) { |
| 198 if (stream.has_more()) PrintF("%c", stream.GetNext()); | 220 if (stream.has_more()) PrintF("%c", stream.GetNext()); |
| 199 } | 221 } |
| 200 PrintF("\n\n"); | 222 PrintF("\n\n"); |
| 201 } | 223 } |
| 202 PrintF("--- Code ---\n"); | 224 PrintF("--- Code ---\n"); |
| 203 code->Disassemble(*fun->name()->ToCString()); | 225 code->Disassemble(*function->name()->ToCString()); |
| 204 } | 226 } |
| 205 #endif // ENABLE_DISASSEMBLER | 227 #endif // ENABLE_DISASSEMBLER |
| 206 | 228 |
| 207 if (!code.is_null()) { | 229 if (!code.is_null()) { |
| 208 Counters::total_compiled_code_size.Increment(code->instruction_size()); | 230 Counters::total_compiled_code_size.Increment(code->instruction_size()); |
| 209 } | 231 } |
| 210 return code; | 232 return code; |
| 211 } | 233 } |
| 212 | 234 |
| 213 | 235 |
| 214 // Generate the code. Takes a function literal, generates code for it, assemble | 236 // Generate the code. Takes a function literal, generates code for it, assemble |
| 215 // all the pieces into a Code object. This function is only to be called by | 237 // all the pieces into a Code object. This function is only to be called by |
| 216 // the compiler.cc code. | 238 // the compiler.cc code. |
| 217 Handle<Code> CodeGenerator::MakeCode(FunctionLiteral* fun, | 239 Handle<Code> CodeGenerator::MakeCode(CompilationInfo* info) { |
| 218 Handle<Script> script, | 240 Handle<Script> script = info->script(); |
| 219 bool is_eval, | |
| 220 CompilationInfo* info) { | |
| 221 if (!script->IsUndefined() && !script->source()->IsUndefined()) { | 241 if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 222 int len = String::cast(script->source())->length(); | 242 int len = String::cast(script->source())->length(); |
| 223 Counters::total_old_codegen_source_size.Increment(len); | 243 Counters::total_old_codegen_source_size.Increment(len); |
| 224 } | 244 } |
| 225 MakeCodePrologue(fun); | 245 MakeCodePrologue(info); |
| 226 // Generate code. | 246 // Generate code. |
| 227 const int kInitialBufferSize = 4 * KB; | 247 const int kInitialBufferSize = 4 * KB; |
| 228 MacroAssembler masm(NULL, kInitialBufferSize); | 248 MacroAssembler masm(NULL, kInitialBufferSize); |
| 229 CodeGenerator cgen(&masm, script, is_eval); | 249 CodeGenerator cgen(&masm); |
| 230 CodeGeneratorScope scope(&cgen); | 250 CodeGeneratorScope scope(&cgen); |
| 231 cgen.Generate(fun, PRIMARY, info); | 251 cgen.Generate(info, PRIMARY); |
| 232 if (cgen.HasStackOverflow()) { | 252 if (cgen.HasStackOverflow()) { |
| 233 ASSERT(!Top::has_pending_exception()); | 253 ASSERT(!Top::has_pending_exception()); |
| 234 return Handle<Code>::null(); | 254 return Handle<Code>::null(); |
| 235 } | 255 } |
| 236 | 256 |
| 237 InLoopFlag in_loop = (cgen.loop_nesting() != 0) ? IN_LOOP : NOT_IN_LOOP; | 257 InLoopFlag in_loop = (cgen.loop_nesting() != 0) ? IN_LOOP : NOT_IN_LOOP; |
| 238 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, in_loop); | 258 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, in_loop); |
| 239 return MakeCodeEpilogue(fun, cgen.masm(), flags, script); | 259 return MakeCodeEpilogue(cgen.masm(), flags, info); |
| 240 } | 260 } |
| 241 | 261 |
| 242 | 262 |
| 243 #ifdef ENABLE_LOGGING_AND_PROFILING | 263 #ifdef ENABLE_LOGGING_AND_PROFILING |
| 244 | 264 |
| 245 bool CodeGenerator::ShouldGenerateLog(Expression* type) { | 265 bool CodeGenerator::ShouldGenerateLog(Expression* type) { |
| 246 ASSERT(type != NULL); | 266 ASSERT(type != NULL); |
| 247 if (!Logger::is_logging()) return false; | 267 if (!Logger::is_logging()) return false; |
| 248 Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle()); | 268 Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle()); |
| 249 if (FLAG_log_regexp) { | 269 if (FLAG_log_regexp) { |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 case READ_LENGTH: GenerateReadLength(masm); break; | 495 case READ_LENGTH: GenerateReadLength(masm); break; |
| 476 case READ_ELEMENT: GenerateReadElement(masm); break; | 496 case READ_ELEMENT: GenerateReadElement(masm); break; |
| 477 case NEW_OBJECT: GenerateNewObject(masm); break; | 497 case NEW_OBJECT: GenerateNewObject(masm); break; |
| 478 } | 498 } |
| 479 } | 499 } |
| 480 | 500 |
| 481 | 501 |
| 482 int CEntryStub::MinorKey() { | 502 int CEntryStub::MinorKey() { |
| 483 ASSERT(result_size_ <= 2); | 503 ASSERT(result_size_ <= 2); |
| 484 #ifdef _WIN64 | 504 #ifdef _WIN64 |
| 485 const indirect_result = result_size_ > 1; | 505 return ExitFrameModeBits::encode(mode_) |
| 506 | IndirectResultBits::encode(result_size_ > 1); |
| 486 #else | 507 #else |
| 487 const bool indirect_result = false; | 508 return ExitFrameModeBits::encode(mode_); |
| 488 #endif | 509 #endif |
| 489 | |
| 490 return ExitFrameModeBits::encode(mode_) | |
| 491 | IndirectResultBits::encode(indirect_result > 1); | |
| 492 } | 510 } |
| 493 | 511 |
| 494 | 512 |
| 495 bool ApiGetterEntryStub::GetCustomCache(Code** code_out) { | 513 bool ApiGetterEntryStub::GetCustomCache(Code** code_out) { |
| 496 Object* cache = info()->load_stub_cache(); | 514 Object* cache = info()->load_stub_cache(); |
| 497 if (cache->IsUndefined()) { | 515 if (cache->IsUndefined()) { |
| 498 return false; | 516 return false; |
| 499 } else { | 517 } else { |
| 500 *code_out = Code::cast(cache); | 518 *code_out = Code::cast(cache); |
| 501 return true; | 519 return true; |
| 502 } | 520 } |
| 503 } | 521 } |
| 504 | 522 |
| 505 | 523 |
| 506 void ApiGetterEntryStub::SetCustomCache(Code* value) { | 524 void ApiGetterEntryStub::SetCustomCache(Code* value) { |
| 507 info()->set_load_stub_cache(value); | 525 info()->set_load_stub_cache(value); |
| 508 } | 526 } |
| 509 | 527 |
| 510 | 528 #ifdef ENABLE_DEBUGGER_SUPPORT |
| 511 void DebugerStatementStub::Generate(MacroAssembler* masm) { | 529 void DebuggerStatementStub::Generate(MacroAssembler* masm) { |
| 512 Runtime::Function* f = Runtime::FunctionForId(Runtime::kDebugBreak); | 530 Runtime::Function* f = Runtime::FunctionForId(Runtime::kDebugBreak); |
| 513 masm->TailCallRuntime(ExternalReference(f), 0, f->result_size); | 531 masm->TailCallRuntime(ExternalReference(f), 0, f->result_size); |
| 514 } | 532 } |
| 533 #endif |
| 515 | 534 |
| 516 | 535 |
| 517 } } // namespace v8::internal | 536 } } // namespace v8::internal |
| OLD | NEW |