blob: cdc9ba1553276ed9edaad35f8d0d25f0210ef7c6 [file] [log] [blame]
Ben Murdoch85b71792012-04-11 18:30:58 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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 Murdoch8b112d22011-06-08 16:22:53 +010031#include "codegen.h"
Steve Blockd0582a62009-12-15 09:54:21 +000032#include "compiler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000033#include "debug.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "prettyprinter.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000035#include "rewriter.h"
36#include "runtime.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000037#include "stub-cache.h"
38
39namespace v8 {
40namespace internal {
41
Andrei Popescu31002712010-02-23 13:46:05 +000042#define __ ACCESS_MASM(masm_)
43
44#ifdef DEBUG
45
46Comment::Comment(MacroAssembler* masm, const char* msg)
47 : masm_(masm), msg_(msg) {
48 __ RecordComment(msg);
49}
50
51
52Comment::~Comment() {
53 if (msg_[0] == '[') __ RecordComment("]");
54}
55
56#endif // DEBUG
57
58#undef __
59
Steve Blocka7e24c12009-10-30 11:49:00 +000060
Andrei Popescu31002712010-02-23 13:46:05 +000061void CodeGenerator::MakeCodePrologue(CompilationInfo* info) {
Steve Blocka7e24c12009-10-30 11:49:00 +000062#ifdef DEBUG
63 bool print_source = false;
64 bool print_ast = false;
Ben Murdoch85b71792012-04-11 18:30:58 +010065 bool print_json_ast = false;
Steve Blocka7e24c12009-10-30 11:49:00 +000066 const char* ftype;
67
Steve Block44f0eee2011-05-26 01:26:41 +010068 if (Isolate::Current()->bootstrapper()->IsActive()) {
Steve Blocka7e24c12009-10-30 11:49:00 +000069 print_source = FLAG_print_builtin_source;
70 print_ast = FLAG_print_builtin_ast;
Ben Murdoch85b71792012-04-11 18:30:58 +010071 print_json_ast = FLAG_print_builtin_json_ast;
Steve Blocka7e24c12009-10-30 11:49:00 +000072 ftype = "builtin";
73 } else {
74 print_source = FLAG_print_source;
75 print_ast = FLAG_print_ast;
Ben Murdoch85b71792012-04-11 18:30:58 +010076 print_json_ast = FLAG_print_json_ast;
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 }
Steve Blocka7e24c12009-10-30 11:49:00 +000087 ftype = "user-defined";
88 }
89
90 if (FLAG_trace_codegen || print_source || print_ast) {
91 PrintF("*** Generate code for %s function: ", ftype);
Andrei Popescu31002712010-02-23 13:46:05 +000092 info->function()->name()->ShortPrint();
Steve Blocka7e24c12009-10-30 11:49:00 +000093 PrintF(" ***\n");
94 }
95
96 if (print_source) {
Andrei Popescu31002712010-02-23 13:46:05 +000097 PrintF("--- Source from AST ---\n%s\n",
98 PrettyPrinter().PrintProgram(info->function()));
Steve Blocka7e24c12009-10-30 11:49:00 +000099 }
100
101 if (print_ast) {
Andrei Popescu31002712010-02-23 13:46:05 +0000102 PrintF("--- AST ---\n%s\n",
103 AstPrinter().PrintProgram(info->function()));
Steve Block3ce2e202009-11-05 08:53:23 +0000104 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100105
106 if (print_json_ast) {
107 JsonAstBuilder builder;
108 PrintF("%s", builder.BuildProgram(info->function()));
109 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000110#endif // DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +0000111}
Steve Blocka7e24c12009-10-30 11:49:00 +0000112
Steve Blocka7e24c12009-10-30 11:49:00 +0000113
Andrei Popescu31002712010-02-23 13:46:05 +0000114Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm,
Steve Block3ce2e202009-11-05 08:53:23 +0000115 Code::Flags flags,
Andrei Popescu31002712010-02-23 13:46:05 +0000116 CompilationInfo* info) {
Steve Block44f0eee2011-05-26 01:26:41 +0100117 Isolate* isolate = info->isolate();
118
Steve Block3ce2e202009-11-05 08:53:23 +0000119 // Allocate and install the code.
Steve Blocka7e24c12009-10-30 11:49:00 +0000120 CodeDesc desc;
Steve Block3ce2e202009-11-05 08:53:23 +0000121 masm->GetCode(&desc);
Steve Block44f0eee2011-05-26 01:26:41 +0100122 Handle<Code> code =
123 isolate->factory()->NewCode(desc, flags, masm->CodeObject());
Steve Blocka7e24c12009-10-30 11:49:00 +0000124
Ben Murdochb0fe1622011-05-05 13:52:32 +0100125 if (!code.is_null()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100126 isolate->counters()->total_compiled_code_size()->Increment(
127 code->instruction_size());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100128 }
129 return code;
130}
131
132
133void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000134#ifdef ENABLE_DISASSEMBLER
Steve Block44f0eee2011-05-26 01:26:41 +0100135 bool print_code = Isolate::Current()->bootstrapper()->IsActive()
Steve Block3ce2e202009-11-05 08:53:23 +0000136 ? FLAG_print_builtin_code
Ben Murdochb0fe1622011-05-05 13:52:32 +0100137 : (FLAG_print_code || (info->IsOptimizing() && FLAG_print_opt_code));
Ben Murdoch85b71792012-04-11 18:30:58 +0100138 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) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000142 // Print the source code if available.
Andrei Popescu31002712010-02-23 13:46:05 +0000143 Handle<Script> script = info->script();
Steve Blocka7e24c12009-10-30 11:49:00 +0000144 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
145 PrintF("--- Raw source ---\n");
146 StringInputBuffer stream(String::cast(script->source()));
Andrei Popescu31002712010-02-23 13:46:05 +0000147 stream.Seek(function->start_position());
Steve Block3ce2e202009-11-05 08:53:23 +0000148 // fun->end_position() points to the last character in the stream. We
Steve Blocka7e24c12009-10-30 11:49:00 +0000149 // need to compensate by adding one to calculate the length.
Andrei Popescu31002712010-02-23 13:46:05 +0000150 int source_len =
151 function->end_position() - function->start_position() + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 for (int i = 0; i < source_len; i++) {
153 if (stream.has_more()) PrintF("%c", stream.GetNext());
154 }
155 PrintF("\n\n");
156 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100157 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());
Steve Blocka7e24c12009-10-30 11:49:00 +0000168 }
169#endif // ENABLE_DISASSEMBLER
Steve Blocka7e24c12009-10-30 11:49:00 +0000170}
171
Steve Block44f0eee2011-05-26 01:26:41 +0100172
Steve Blocka7e24c12009-10-30 11:49:00 +0000173bool CodeGenerator::ShouldGenerateLog(Expression* type) {
174 ASSERT(type != NULL);
Ben Murdoch257744e2011-11-30 15:57:28 +0000175 Isolate* isolate = Isolate::Current();
176 if (!isolate->logger()->is_logging() && !CpuProfiler::is_profiling(isolate)) {
177 return false;
178 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000179 Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle());
180 if (FLAG_log_regexp) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000181 if (name->IsEqualTo(CStrVector("regexp")))
Steve Blocka7e24c12009-10-30 11:49:00 +0000182 return true;
183 }
184 return false;
185}
186
Steve Blocka7e24c12009-10-30 11:49:00 +0000187
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100188bool CodeGenerator::RecordPositions(MacroAssembler* masm,
189 int pos,
190 bool right_here) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000191 if (pos != RelocInfo::kNoPosition) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800192 masm->positions_recorder()->RecordStatementPosition(pos);
193 masm->positions_recorder()->RecordPosition(pos);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100194 if (right_here) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800195 return masm->positions_recorder()->WriteRecordedPositions();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100196 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000197 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100198 return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000199}
200
201
Steve Blocka7e24c12009-10-30 11:49:00 +0000202void ArgumentsAccessStub::Generate(MacroAssembler* masm) {
203 switch (type_) {
Steve Block44f0eee2011-05-26 01:26:41 +0100204 case READ_ELEMENT:
205 GenerateReadElement(masm);
206 break;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000207 case NEW_NON_STRICT_FAST:
208 GenerateNewNonStrictFast(masm);
209 break;
210 case NEW_NON_STRICT_SLOW:
211 GenerateNewNonStrictSlow(masm);
212 break;
Steve Block44f0eee2011-05-26 01:26:41 +0100213 case NEW_STRICT:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000214 GenerateNewStrict(masm);
Steve Block44f0eee2011-05-26 01:26:41 +0100215 break;
Steve Blocka7e24c12009-10-30 11:49:00 +0000216 }
217}
218
219
Leon Clarke4515c472010-02-03 11:58:03 +0000220int CEntryStub::MinorKey() {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100221 ASSERT(result_size_ == 1 || result_size_ == 2);
Ben Murdoch85b71792012-04-11 18:30:58 +0100222 int result = save_doubles_ ? 1 : 0;
Leon Clarke4515c472010-02-03 11:58:03 +0000223#ifdef _WIN64
Ben Murdochb0fe1622011-05-05 13:52:32 +0100224 return result | ((result_size_ == 1) ? 0 : 2);
Leon Clarke4515c472010-02-03 11:58:03 +0000225#else
Ben Murdochb0fe1622011-05-05 13:52:32 +0100226 return result;
Leon Clarke4515c472010-02-03 11:58:03 +0000227#endif
228}
229
230
Steve Blocka7e24c12009-10-30 11:49:00 +0000231} } // namespace v8::internal