blob: 0163580e90001a1bd40e63065a45dea06d734002 [file] [log] [blame]
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001// Copyright 2012 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;
65 const char* ftype;
66
Steve Block44f0eee2011-05-26 01:26:41 +010067 if (Isolate::Current()->bootstrapper()->IsActive()) {
Steve Blocka7e24c12009-10-30 11:49:00 +000068 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 Popescu31002712010-02-23 13:46:05 +000079 info->function()->name()->ShortPrint();
Steve Blocka7e24c12009-10-30 11:49:00 +000080 PrintF(" ***\n");
81 }
82
83 if (print_source) {
Andrei Popescu31002712010-02-23 13:46:05 +000084 PrintF("--- Source from AST ---\n%s\n",
85 PrettyPrinter().PrintProgram(info->function()));
Steve Blocka7e24c12009-10-30 11:49:00 +000086 }
87
88 if (print_ast) {
Andrei Popescu31002712010-02-23 13:46:05 +000089 PrintF("--- AST ---\n%s\n",
90 AstPrinter().PrintProgram(info->function()));
Steve Block3ce2e202009-11-05 08:53:23 +000091 }
Steve Blocka7e24c12009-10-30 11:49:00 +000092#endif // DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +000093}
Steve Blocka7e24c12009-10-30 11:49:00 +000094
Steve Blocka7e24c12009-10-30 11:49:00 +000095
Andrei Popescu31002712010-02-23 13:46:05 +000096Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm,
Steve Block3ce2e202009-11-05 08:53:23 +000097 Code::Flags flags,
Andrei Popescu31002712010-02-23 13:46:05 +000098 CompilationInfo* info) {
Steve Block44f0eee2011-05-26 01:26:41 +010099 Isolate* isolate = info->isolate();
100
Steve Block3ce2e202009-11-05 08:53:23 +0000101 // Allocate and install the code.
Steve Blocka7e24c12009-10-30 11:49:00 +0000102 CodeDesc desc;
Steve Block3ce2e202009-11-05 08:53:23 +0000103 masm->GetCode(&desc);
Steve Block44f0eee2011-05-26 01:26:41 +0100104 Handle<Code> code =
105 isolate->factory()->NewCode(desc, flags, masm->CodeObject());
Steve Blocka7e24c12009-10-30 11:49:00 +0000106
Ben Murdochb0fe1622011-05-05 13:52:32 +0100107 if (!code.is_null()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100108 isolate->counters()->total_compiled_code_size()->Increment(
109 code->instruction_size());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100110 }
111 return code;
112}
113
114
115void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000116#ifdef ENABLE_DISASSEMBLER
Steve Block44f0eee2011-05-26 01:26:41 +0100117 bool print_code = Isolate::Current()->bootstrapper()->IsActive()
Steve Block3ce2e202009-11-05 08:53:23 +0000118 ? FLAG_print_builtin_code
Ben Murdochb0fe1622011-05-05 13:52:32 +0100119 : (FLAG_print_code || (info->IsOptimizing() && FLAG_print_opt_code));
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100120 if (print_code) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000121 // Print the source code if available.
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100122 FunctionLiteral* function = info->function();
Andrei Popescu31002712010-02-23 13:46:05 +0000123 Handle<Script> script = info->script();
Steve Blocka7e24c12009-10-30 11:49:00 +0000124 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
125 PrintF("--- Raw source ---\n");
126 StringInputBuffer stream(String::cast(script->source()));
Andrei Popescu31002712010-02-23 13:46:05 +0000127 stream.Seek(function->start_position());
Steve Block3ce2e202009-11-05 08:53:23 +0000128 // fun->end_position() points to the last character in the stream. We
Steve Blocka7e24c12009-10-30 11:49:00 +0000129 // need to compensate by adding one to calculate the length.
Andrei Popescu31002712010-02-23 13:46:05 +0000130 int source_len =
131 function->end_position() - function->start_position() + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000132 for (int i = 0; i < source_len; i++) {
133 if (stream.has_more()) PrintF("%c", stream.GetNext());
134 }
135 PrintF("\n\n");
136 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100137 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 Blocka7e24c12009-10-30 11:49:00 +0000148 }
149#endif // ENABLE_DISASSEMBLER
Steve Blocka7e24c12009-10-30 11:49:00 +0000150}
151
Steve Block44f0eee2011-05-26 01:26:41 +0100152
Steve Blocka7e24c12009-10-30 11:49:00 +0000153bool CodeGenerator::ShouldGenerateLog(Expression* type) {
154 ASSERT(type != NULL);
Ben Murdoch257744e2011-11-30 15:57:28 +0000155 Isolate* isolate = Isolate::Current();
156 if (!isolate->logger()->is_logging() && !CpuProfiler::is_profiling(isolate)) {
157 return false;
158 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000159 Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle());
160 if (FLAG_log_regexp) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000161 if (name->IsEqualTo(CStrVector("regexp")))
Steve Blocka7e24c12009-10-30 11:49:00 +0000162 return true;
163 }
164 return false;
165}
166
Steve Blocka7e24c12009-10-30 11:49:00 +0000167
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100168bool CodeGenerator::RecordPositions(MacroAssembler* masm,
169 int pos,
170 bool right_here) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000171 if (pos != RelocInfo::kNoPosition) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800172 masm->positions_recorder()->RecordStatementPosition(pos);
173 masm->positions_recorder()->RecordPosition(pos);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100174 if (right_here) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800175 return masm->positions_recorder()->WriteRecordedPositions();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100176 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000177 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100178 return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000179}
180
181
Steve Blocka7e24c12009-10-30 11:49:00 +0000182void ArgumentsAccessStub::Generate(MacroAssembler* masm) {
183 switch (type_) {
Steve Block44f0eee2011-05-26 01:26:41 +0100184 case READ_ELEMENT:
185 GenerateReadElement(masm);
186 break;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000187 case NEW_NON_STRICT_FAST:
188 GenerateNewNonStrictFast(masm);
189 break;
190 case NEW_NON_STRICT_SLOW:
191 GenerateNewNonStrictSlow(masm);
192 break;
Steve Block44f0eee2011-05-26 01:26:41 +0100193 case NEW_STRICT:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000194 GenerateNewStrict(masm);
Steve Block44f0eee2011-05-26 01:26:41 +0100195 break;
Steve Blocka7e24c12009-10-30 11:49:00 +0000196 }
197}
198
199
Leon Clarke4515c472010-02-03 11:58:03 +0000200int CEntryStub::MinorKey() {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000201 int result = (save_doubles_ == kSaveFPRegs) ? 1 : 0;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100202 ASSERT(result_size_ == 1 || result_size_ == 2);
Leon Clarke4515c472010-02-03 11:58:03 +0000203#ifdef _WIN64
Ben Murdochb0fe1622011-05-05 13:52:32 +0100204 return result | ((result_size_ == 1) ? 0 : 2);
Leon Clarke4515c472010-02-03 11:58:03 +0000205#else
Ben Murdochb0fe1622011-05-05 13:52:32 +0100206 return result;
Leon Clarke4515c472010-02-03 11:58:03 +0000207#endif
208}
209
210
Steve Blocka7e24c12009-10-30 11:49:00 +0000211} } // namespace v8::internal