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