blob: 0163580e90001a1bd40e63065a45dea06d734002 [file] [log] [blame]
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
ager@chromium.org7c537e22008-10-16 08:43:32 +000030#include "bootstrapper.h"
karlklose@chromium.org44bc7082011-04-11 12:33:05 +000031#include "codegen.h"
ager@chromium.orgc4c92722009-11-18 14:12:51 +000032#include "compiler.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033#include "debug.h"
ager@chromium.org7c537e22008-10-16 08:43:32 +000034#include "prettyprinter.h"
ager@chromium.org71daaf62009-04-01 07:22:49 +000035#include "rewriter.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036#include "runtime.h"
37#include "stub-cache.h"
38
kasperl@chromium.org71affb52009-05-26 05:44:31 +000039namespace v8 {
40namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
ager@chromium.org5c838252010-02-19 08:53:10 +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
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000060
ager@chromium.org5c838252010-02-19 08:53:10 +000061void CodeGenerator::MakeCodePrologue(CompilationInfo* info) {
ager@chromium.org7c537e22008-10-16 08:43:32 +000062#ifdef DEBUG
63 bool print_source = false;
64 bool print_ast = false;
65 const char* ftype;
66
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000067 if (Isolate::Current()->bootstrapper()->IsActive()) {
ager@chromium.org7c537e22008-10-16 08:43:32 +000068 print_source = FLAG_print_builtin_source;
69 print_ast = FLAG_print_builtin_ast;
ager@chromium.org7c537e22008-10-16 08:43:32 +000070 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.org5c838252010-02-19 08:53:10 +000079 info->function()->name()->ShortPrint();
ager@chromium.org7c537e22008-10-16 08:43:32 +000080 PrintF(" ***\n");
81 }
82
83 if (print_source) {
ager@chromium.org5c838252010-02-19 08:53:10 +000084 PrintF("--- Source from AST ---\n%s\n",
85 PrettyPrinter().PrintProgram(info->function()));
ager@chromium.org7c537e22008-10-16 08:43:32 +000086 }
87
88 if (print_ast) {
ager@chromium.org5c838252010-02-19 08:53:10 +000089 PrintF("--- AST ---\n%s\n",
90 AstPrinter().PrintProgram(info->function()));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000091 }
ager@chromium.org7c537e22008-10-16 08:43:32 +000092#endif // DEBUG
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000093}
ager@chromium.org7c537e22008-10-16 08:43:32 +000094
ager@chromium.org7c537e22008-10-16 08:43:32 +000095
ager@chromium.org5c838252010-02-19 08:53:10 +000096Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000097 Code::Flags flags,
ager@chromium.org5c838252010-02-19 08:53:10 +000098 CompilationInfo* info) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000099 Isolate* isolate = info->isolate();
100
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000101 // Allocate and install the code.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000102 CodeDesc desc;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000103 masm->GetCode(&desc);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000104 Handle<Code> code =
105 isolate->factory()->NewCode(desc, flags, masm->CodeObject());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000106
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000107 if (!code.is_null()) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000108 isolate->counters()->total_compiled_code_size()->Increment(
109 code->instruction_size());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000110 }
111 return code;
112}
113
114
115void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000116#ifdef ENABLE_DISASSEMBLER
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000117 bool print_code = Isolate::Current()->bootstrapper()->IsActive()
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000118 ? FLAG_print_builtin_code
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000119 : (FLAG_print_code || (info->IsOptimizing() && FLAG_print_opt_code));
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +0000120 if (print_code) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000121 // Print the source code if available.
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +0000122 FunctionLiteral* function = info->function();
ager@chromium.org5c838252010-02-19 08:53:10 +0000123 Handle<Script> script = info->script();
ager@chromium.org7c537e22008-10-16 08:43:32 +0000124 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
125 PrintF("--- Raw source ---\n");
126 StringInputBuffer stream(String::cast(script->source()));
ager@chromium.org5c838252010-02-19 08:53:10 +0000127 stream.Seek(function->start_position());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000128 // fun->end_position() points to the last character in the stream. We
ager@chromium.org7c537e22008-10-16 08:43:32 +0000129 // need to compensate by adding one to calculate the length.
ager@chromium.org5c838252010-02-19 08:53:10 +0000130 int source_len =
131 function->end_position() - function->start_position() + 1;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000132 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.org023421e2010-12-21 12:19:12 +0000137 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.org7c537e22008-10-16 08:43:32 +0000148 }
149#endif // ENABLE_DISASSEMBLER
ager@chromium.org7c537e22008-10-16 08:43:32 +0000150}
151
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000152
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000153bool CodeGenerator::ShouldGenerateLog(Expression* type) {
154 ASSERT(type != NULL);
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000155 Isolate* isolate = Isolate::Current();
156 if (!isolate->logger()->is_logging() && !CpuProfiler::is_profiling(isolate)) {
157 return false;
158 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000159 Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle());
160 if (FLAG_log_regexp) {
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000161 if (name->IsEqualTo(CStrVector("regexp")))
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000162 return true;
163 }
164 return false;
165}
166
167
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000168bool CodeGenerator::RecordPositions(MacroAssembler* masm,
169 int pos,
170 bool right_here) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000171 if (pos != RelocInfo::kNoPosition) {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000172 masm->positions_recorder()->RecordStatementPosition(pos);
173 masm->positions_recorder()->RecordPosition(pos);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000174 if (right_here) {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000175 return masm->positions_recorder()->WriteRecordedPositions();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000176 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000177 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000178 return false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000179}
180
181
ager@chromium.org7c537e22008-10-16 08:43:32 +0000182void ArgumentsAccessStub::Generate(MacroAssembler* masm) {
183 switch (type_) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000184 case READ_ELEMENT:
185 GenerateReadElement(masm);
186 break;
whesse@chromium.org7b260152011-06-20 15:33:18 +0000187 case NEW_NON_STRICT_FAST:
188 GenerateNewNonStrictFast(masm);
189 break;
190 case NEW_NON_STRICT_SLOW:
191 GenerateNewNonStrictSlow(masm);
192 break;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000193 case NEW_STRICT:
whesse@chromium.org7b260152011-06-20 15:33:18 +0000194 GenerateNewStrict(masm);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000195 break;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000196 }
197}
198
199
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000200int CEntryStub::MinorKey() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000201 int result = (save_doubles_ == kSaveFPRegs) ? 1 : 0;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000202 ASSERT(result_size_ == 1 || result_size_ == 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000203#ifdef _WIN64
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000204 return result | ((result_size_ == 1) ? 0 : 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000205#else
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000206 return result;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000207#endif
208}
209
210
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211} } // namespace v8::internal