Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 1 | // Copyright 2011 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" |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 31 | #include "codegen.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" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | #include "prettyprinter.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | #include "rewriter.h" |
| 36 | #include "runtime.h" |
| 37 | #include "scopeinfo.h" |
| 38 | #include "stub-cache.h" |
| 39 | |
| 40 | namespace v8 { |
| 41 | namespace internal { |
| 42 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 43 | #define __ ACCESS_MASM(masm_) |
| 44 | |
| 45 | #ifdef DEBUG |
| 46 | |
| 47 | Comment::Comment(MacroAssembler* masm, const char* msg) |
| 48 | : masm_(masm), msg_(msg) { |
| 49 | __ RecordComment(msg); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | Comment::~Comment() { |
| 54 | if (msg_[0] == '[') __ RecordComment("]"); |
| 55 | } |
| 56 | |
| 57 | #endif // DEBUG |
| 58 | |
| 59 | #undef __ |
| 60 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 62 | void CodeGenerator::MakeCodePrologue(CompilationInfo* info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 63 | #ifdef DEBUG |
| 64 | bool print_source = false; |
| 65 | bool print_ast = false; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 66 | bool print_json_ast = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 67 | const char* ftype; |
| 68 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 69 | if (Isolate::Current()->bootstrapper()->IsActive()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | print_source = FLAG_print_builtin_source; |
| 71 | print_ast = FLAG_print_builtin_ast; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 72 | print_json_ast = FLAG_print_builtin_json_ast; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 73 | ftype = "builtin"; |
| 74 | } else { |
| 75 | print_source = FLAG_print_source; |
| 76 | print_ast = FLAG_print_ast; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 77 | print_json_ast = FLAG_print_json_ast; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 78 | Vector<const char> filter = CStrVector(FLAG_hydrogen_filter); |
| 79 | if (print_source && !filter.is_empty()) { |
| 80 | print_source = info->function()->name()->IsEqualTo(filter); |
| 81 | } |
| 82 | if (print_ast && !filter.is_empty()) { |
| 83 | print_ast = info->function()->name()->IsEqualTo(filter); |
| 84 | } |
| 85 | if (print_json_ast && !filter.is_empty()) { |
| 86 | print_json_ast = info->function()->name()->IsEqualTo(filter); |
| 87 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | ftype = "user-defined"; |
| 89 | } |
| 90 | |
| 91 | if (FLAG_trace_codegen || print_source || print_ast) { |
| 92 | PrintF("*** Generate code for %s function: ", ftype); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 93 | info->function()->name()->ShortPrint(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 94 | PrintF(" ***\n"); |
| 95 | } |
| 96 | |
| 97 | if (print_source) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 98 | PrintF("--- Source from AST ---\n%s\n", |
| 99 | PrettyPrinter().PrintProgram(info->function())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | if (print_ast) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 103 | PrintF("--- AST ---\n%s\n", |
| 104 | AstPrinter().PrintProgram(info->function())); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | if (print_json_ast) { |
| 108 | JsonAstBuilder builder; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 109 | PrintF("%s", builder.BuildProgram(info->function())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 110 | } |
| 111 | #endif // DEBUG |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 112 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 114 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 115 | Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm, |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 116 | Code::Flags flags, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 117 | CompilationInfo* info) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 118 | Isolate* isolate = info->isolate(); |
| 119 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 120 | // Allocate and install the code. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 121 | CodeDesc desc; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 122 | masm->GetCode(&desc); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 123 | Handle<Code> code = |
| 124 | isolate->factory()->NewCode(desc, flags, masm->CodeObject()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 125 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 126 | if (!code.is_null()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 127 | isolate->counters()->total_compiled_code_size()->Increment( |
| 128 | code->instruction_size()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 129 | } |
| 130 | return code; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 135 | #ifdef ENABLE_DISASSEMBLER |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 136 | bool print_code = Isolate::Current()->bootstrapper()->IsActive() |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 137 | ? FLAG_print_builtin_code |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 138 | : (FLAG_print_code || (info->IsOptimizing() && FLAG_print_opt_code)); |
| 139 | Vector<const char> filter = CStrVector(FLAG_hydrogen_filter); |
| 140 | FunctionLiteral* function = info->function(); |
| 141 | bool match = filter.is_empty() || function->debug_name()->IsEqualTo(filter); |
| 142 | if (print_code && match) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 143 | // Print the source code if available. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 144 | Handle<Script> script = info->script(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 145 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 146 | PrintF("--- Raw source ---\n"); |
| 147 | StringInputBuffer stream(String::cast(script->source())); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 148 | stream.Seek(function->start_position()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 149 | // fun->end_position() points to the last character in the stream. We |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 150 | // need to compensate by adding one to calculate the length. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 151 | int source_len = |
| 152 | function->end_position() - function->start_position() + 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 153 | for (int i = 0; i < source_len; i++) { |
| 154 | if (stream.has_more()) PrintF("%c", stream.GetNext()); |
| 155 | } |
| 156 | PrintF("\n\n"); |
| 157 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 158 | if (info->IsOptimizing()) { |
| 159 | if (FLAG_print_unopt_code) { |
| 160 | PrintF("--- Unoptimized code ---\n"); |
| 161 | info->closure()->shared()->code()->Disassemble( |
| 162 | *function->debug_name()->ToCString()); |
| 163 | } |
| 164 | PrintF("--- Optimized code ---\n"); |
| 165 | } else { |
| 166 | PrintF("--- Code ---\n"); |
| 167 | } |
| 168 | code->Disassemble(*function->debug_name()->ToCString()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 169 | } |
| 170 | #endif // ENABLE_DISASSEMBLER |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 173 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 174 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 175 | static Vector<const char> kRegexp = CStrVector("regexp"); |
| 176 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | bool CodeGenerator::ShouldGenerateLog(Expression* type) { |
| 178 | ASSERT(type != NULL); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 179 | if (!LOGGER->is_logging() && !CpuProfiler::is_profiling()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 180 | Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle()); |
| 181 | if (FLAG_log_regexp) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 182 | if (name->IsEqualTo(kRegexp)) |
| 183 | return true; |
| 184 | } |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | #endif |
| 189 | |
| 190 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 191 | bool CodeGenerator::RecordPositions(MacroAssembler* masm, |
| 192 | int pos, |
| 193 | bool right_here) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 194 | if (pos != RelocInfo::kNoPosition) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 195 | masm->positions_recorder()->RecordStatementPosition(pos); |
| 196 | masm->positions_recorder()->RecordPosition(pos); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 197 | if (right_here) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 198 | return masm->positions_recorder()->WriteRecordedPositions(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 199 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 200 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 201 | return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 205 | const char* GenericUnaryOpStub::GetName() { |
| 206 | switch (op_) { |
| 207 | case Token::SUB: |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 208 | if (negative_zero_ == kStrictNegativeZero) { |
| 209 | return overwrite_ == UNARY_OVERWRITE |
| 210 | ? "GenericUnaryOpStub_SUB_Overwrite_Strict0" |
| 211 | : "GenericUnaryOpStub_SUB_Alloc_Strict0"; |
| 212 | } else { |
| 213 | return overwrite_ == UNARY_OVERWRITE |
| 214 | ? "GenericUnaryOpStub_SUB_Overwrite_Ignore0" |
| 215 | : "GenericUnaryOpStub_SUB_Alloc_Ignore0"; |
| 216 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 217 | case Token::BIT_NOT: |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 218 | return overwrite_ == UNARY_OVERWRITE |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 219 | ? "GenericUnaryOpStub_BIT_NOT_Overwrite" |
| 220 | : "GenericUnaryOpStub_BIT_NOT_Alloc"; |
| 221 | default: |
| 222 | UNREACHABLE(); |
| 223 | return "<unknown>"; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 228 | void ArgumentsAccessStub::Generate(MacroAssembler* masm) { |
| 229 | switch (type_) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 230 | case READ_ELEMENT: |
| 231 | GenerateReadElement(masm); |
| 232 | break; |
| 233 | case NEW_NON_STRICT: |
| 234 | case NEW_STRICT: |
| 235 | GenerateNewObject(masm); |
| 236 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 241 | int CEntryStub::MinorKey() { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 242 | ASSERT(result_size_ == 1 || result_size_ == 2); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 243 | int result = save_doubles_ ? 1 : 0; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 244 | #ifdef _WIN64 |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 245 | return result | ((result_size_ == 1) ? 0 : 2); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 246 | #else |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 247 | return result; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 248 | #endif |
| 249 | } |
| 250 | |
| 251 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 252 | } } // namespace v8::internal |