Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 1 | // Copyright 2012 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" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 37 | #include "stub-cache.h" |
| 38 | |
| 39 | namespace v8 { |
| 40 | namespace internal { |
| 41 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +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 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 60 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 61 | void CodeGenerator::MakeCodePrologue(CompilationInfo* info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 62 | #ifdef DEBUG |
| 63 | bool print_source = false; |
| 64 | bool print_ast = false; |
| 65 | const char* ftype; |
| 66 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 67 | if (Isolate::Current()->bootstrapper()->IsActive()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 68 | print_source = FLAG_print_builtin_source; |
| 69 | print_ast = FLAG_print_builtin_ast; |
| 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); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 79 | info->function()->name()->ShortPrint(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 80 | PrintF(" ***\n"); |
| 81 | } |
| 82 | |
| 83 | if (print_source) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 84 | PrintF("--- Source from AST ---\n%s\n", |
| 85 | PrettyPrinter().PrintProgram(info->function())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | if (print_ast) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 89 | PrintF("--- AST ---\n%s\n", |
| 90 | AstPrinter().PrintProgram(info->function())); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 91 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | #endif // DEBUG |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 93 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 94 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 96 | Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm, |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 97 | Code::Flags flags, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 98 | CompilationInfo* info) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 99 | Isolate* isolate = info->isolate(); |
| 100 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 101 | // Allocate and install the code. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 102 | CodeDesc desc; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 103 | masm->GetCode(&desc); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 104 | Handle<Code> code = |
| 105 | isolate->factory()->NewCode(desc, flags, masm->CodeObject()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 106 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 107 | if (!code.is_null()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 108 | isolate->counters()->total_compiled_code_size()->Increment( |
| 109 | code->instruction_size()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 110 | } |
| 111 | return code; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 116 | #ifdef ENABLE_DISASSEMBLER |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 117 | bool print_code = Isolate::Current()->bootstrapper()->IsActive() |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 118 | ? FLAG_print_builtin_code |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 119 | : (FLAG_print_code || (info->IsOptimizing() && FLAG_print_opt_code)); |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 120 | if (print_code) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 121 | // Print the source code if available. |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame^] | 122 | FunctionLiteral* function = info->function(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 123 | Handle<Script> script = info->script(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 124 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 125 | PrintF("--- Raw source ---\n"); |
| 126 | StringInputBuffer stream(String::cast(script->source())); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 127 | stream.Seek(function->start_position()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 128 | // fun->end_position() points to the last character in the stream. We |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 129 | // need to compensate by adding one to calculate the length. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 130 | int source_len = |
| 131 | function->end_position() - function->start_position() + 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +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 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [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()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 148 | } |
| 149 | #endif // ENABLE_DISASSEMBLER |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 152 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 153 | bool CodeGenerator::ShouldGenerateLog(Expression* type) { |
| 154 | ASSERT(type != NULL); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 155 | Isolate* isolate = Isolate::Current(); |
| 156 | if (!isolate->logger()->is_logging() && !CpuProfiler::is_profiling(isolate)) { |
| 157 | return false; |
| 158 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 159 | Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle()); |
| 160 | if (FLAG_log_regexp) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 161 | if (name->IsEqualTo(CStrVector("regexp"))) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 162 | return true; |
| 163 | } |
| 164 | return false; |
| 165 | } |
| 166 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 168 | bool CodeGenerator::RecordPositions(MacroAssembler* masm, |
| 169 | int pos, |
| 170 | bool right_here) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | if (pos != RelocInfo::kNoPosition) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 172 | masm->positions_recorder()->RecordStatementPosition(pos); |
| 173 | masm->positions_recorder()->RecordPosition(pos); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 174 | if (right_here) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 175 | return masm->positions_recorder()->WriteRecordedPositions(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 176 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 178 | return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 182 | void ArgumentsAccessStub::Generate(MacroAssembler* masm) { |
| 183 | switch (type_) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 184 | case READ_ELEMENT: |
| 185 | GenerateReadElement(masm); |
| 186 | break; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +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; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 193 | case NEW_STRICT: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 194 | GenerateNewStrict(masm); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 195 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 200 | int CEntryStub::MinorKey() { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 201 | int result = (save_doubles_ == kSaveFPRegs) ? 1 : 0; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 202 | ASSERT(result_size_ == 1 || result_size_ == 2); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 203 | #ifdef _WIN64 |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 204 | return result | ((result_size_ == 1) ? 0 : 2); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 205 | #else |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 206 | return result; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 207 | #endif |
| 208 | } |
| 209 | |
| 210 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 211 | } } // namespace v8::internal |