yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 30 | #include "bootstrapper.h" |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 31 | #include "codegen.h" |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 32 | #include "compiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "debug.h" |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 34 | #include "prettyprinter.h" |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 35 | #include "rewriter.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | #include "runtime.h" |
| 37 | #include "stub-cache.h" |
| 38 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 39 | namespace v8 { |
| 40 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 42 | #define __ ACCESS_MASM(masm_) |
| 43 | |
| 44 | #ifdef DEBUG |
| 45 | |
| 46 | Comment::Comment(MacroAssembler* masm, const char* msg) |
| 47 | : masm_(masm), msg_(msg) { |
| 48 | __ RecordComment(msg); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | Comment::~Comment() { |
| 53 | if (msg_[0] == '[') __ RecordComment("]"); |
| 54 | } |
| 55 | |
| 56 | #endif // DEBUG |
| 57 | |
| 58 | #undef __ |
| 59 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 60 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 61 | void CodeGenerator::MakeCodePrologue(CompilationInfo* info) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 62 | #ifdef DEBUG |
| 63 | bool print_source = false; |
| 64 | bool print_ast = false; |
| 65 | const char* ftype; |
| 66 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 67 | if (Isolate::Current()->bootstrapper()->IsActive()) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 68 | print_source = FLAG_print_builtin_source; |
| 69 | print_ast = FLAG_print_builtin_ast; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 70 | ftype = "builtin"; |
| 71 | } else { |
| 72 | print_source = FLAG_print_source; |
| 73 | print_ast = FLAG_print_ast; |
| 74 | ftype = "user-defined"; |
| 75 | } |
| 76 | |
| 77 | if (FLAG_trace_codegen || print_source || print_ast) { |
| 78 | PrintF("*** Generate code for %s function: ", ftype); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 79 | info->function()->name()->ShortPrint(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 80 | PrintF(" ***\n"); |
| 81 | } |
| 82 | |
| 83 | if (print_source) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 84 | PrintF("--- Source from AST ---\n%s\n", |
| 85 | PrettyPrinter().PrintProgram(info->function())); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | if (print_ast) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 89 | PrintF("--- AST ---\n%s\n", |
| 90 | AstPrinter().PrintProgram(info->function())); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 91 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 92 | #endif // DEBUG |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 93 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 94 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 95 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 96 | Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm, |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 97 | Code::Flags flags, |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 98 | CompilationInfo* info) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 99 | Isolate* isolate = info->isolate(); |
| 100 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 101 | // Allocate and install the code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 102 | CodeDesc desc; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 103 | masm->GetCode(&desc); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 104 | Handle<Code> code = |
| 105 | isolate->factory()->NewCode(desc, flags, masm->CodeObject()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 106 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 107 | if (!code.is_null()) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 108 | isolate->counters()->total_compiled_code_size()->Increment( |
| 109 | code->instruction_size()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 110 | } |
| 111 | return code; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 116 | #ifdef ENABLE_DISASSEMBLER |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 117 | bool print_code = Isolate::Current()->bootstrapper()->IsActive() |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 118 | ? FLAG_print_builtin_code |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 119 | : (FLAG_print_code || (info->IsOptimizing() && FLAG_print_opt_code)); |
mstarzinger@chromium.org | 3233d2f | 2012-03-14 11:16:03 +0000 | [diff] [blame] | 120 | if (print_code) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 121 | // Print the source code if available. |
mstarzinger@chromium.org | 3233d2f | 2012-03-14 11:16:03 +0000 | [diff] [blame] | 122 | FunctionLiteral* function = info->function(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 123 | Handle<Script> script = info->script(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 124 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 125 | PrintF("--- Raw source ---\n"); |
| 126 | StringInputBuffer stream(String::cast(script->source())); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 127 | stream.Seek(function->start_position()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 128 | // fun->end_position() points to the last character in the stream. We |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 129 | // need to compensate by adding one to calculate the length. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 130 | int source_len = |
| 131 | function->end_position() - function->start_position() + 1; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 132 | for (int i = 0; i < source_len; i++) { |
| 133 | if (stream.has_more()) PrintF("%c", stream.GetNext()); |
| 134 | } |
| 135 | PrintF("\n\n"); |
| 136 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 137 | if (info->IsOptimizing()) { |
| 138 | if (FLAG_print_unopt_code) { |
| 139 | PrintF("--- Unoptimized code ---\n"); |
| 140 | info->closure()->shared()->code()->Disassemble( |
| 141 | *function->debug_name()->ToCString()); |
| 142 | } |
| 143 | PrintF("--- Optimized code ---\n"); |
| 144 | } else { |
| 145 | PrintF("--- Code ---\n"); |
| 146 | } |
| 147 | code->Disassemble(*function->debug_name()->ToCString()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 148 | } |
| 149 | #endif // ENABLE_DISASSEMBLER |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 150 | } |
| 151 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 152 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 153 | bool CodeGenerator::ShouldGenerateLog(Expression* type) { |
| 154 | ASSERT(type != NULL); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 155 | Isolate* isolate = Isolate::Current(); |
| 156 | if (!isolate->logger()->is_logging() && !CpuProfiler::is_profiling(isolate)) { |
| 157 | return false; |
| 158 | } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 159 | Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle()); |
| 160 | if (FLAG_log_regexp) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 161 | if (name->IsEqualTo(CStrVector("regexp"))) |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 162 | return true; |
| 163 | } |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 168 | bool CodeGenerator::RecordPositions(MacroAssembler* masm, |
| 169 | int pos, |
| 170 | bool right_here) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 171 | if (pos != RelocInfo::kNoPosition) { |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 172 | masm->positions_recorder()->RecordStatementPosition(pos); |
| 173 | masm->positions_recorder()->RecordPosition(pos); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 174 | if (right_here) { |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 175 | return masm->positions_recorder()->WriteRecordedPositions(); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 176 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 177 | } |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 178 | return false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 182 | void ArgumentsAccessStub::Generate(MacroAssembler* masm) { |
| 183 | switch (type_) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 184 | case READ_ELEMENT: |
| 185 | GenerateReadElement(masm); |
| 186 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 187 | case NEW_NON_STRICT_FAST: |
| 188 | GenerateNewNonStrictFast(masm); |
| 189 | break; |
| 190 | case NEW_NON_STRICT_SLOW: |
| 191 | GenerateNewNonStrictSlow(masm); |
| 192 | break; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 193 | case NEW_STRICT: |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 194 | GenerateNewStrict(masm); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 195 | break; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 200 | int CEntryStub::MinorKey() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 201 | int result = (save_doubles_ == kSaveFPRegs) ? 1 : 0; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 202 | ASSERT(result_size_ == 1 || result_size_ == 2); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 203 | #ifdef _WIN64 |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 204 | return result | ((result_size_ == 1) ? 0 : 2); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 205 | #else |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 206 | return result; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 207 | #endif |
| 208 | } |
| 209 | |
| 210 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 211 | } } // namespace v8::internal |