blob: 11837e97ba838a6c922e550da0425e2f476ad3b6 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/codegen.h"
Ben Murdoch014dc512016-03-22 12:00:34 +00006
7#if defined(V8_OS_AIX)
8#include <fenv.h> // NOLINT(build/c++11)
9#endif
Ben Murdochf91f0612016-11-29 16:50:11 +000010
11#include <memory>
12
Ben Murdoch014dc512016-03-22 12:00:34 +000013#include "src/ast/prettyprinter.h"
14#include "src/bootstrapper.h"
Ben Murdochf3b273f2017-01-17 12:11:28 +000015#include "src/compilation-info.h"
Ben Murdoch62ed6312017-06-06 11:06:27 +010016#include "src/counters.h"
Ben Murdoch014dc512016-03-22 12:00:34 +000017#include "src/debug/debug.h"
Ben Murdochf91f0612016-11-29 16:50:11 +000018#include "src/eh-frame.h"
Ben Murdoch62ed6312017-06-06 11:06:27 +010019#include "src/objects-inl.h"
Emily Bernier958fae72015-03-24 16:35:39 -040020#include "src/runtime/runtime.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000021
22namespace v8 {
23namespace internal {
24
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025
Ben Murdoch014dc512016-03-22 12:00:34 +000026#if defined(V8_OS_WIN)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027double modulo(double x, double y) {
28 // Workaround MS fmod bugs. ECMA-262 says:
29 // dividend is finite and divisor is an infinity => result equals dividend
30 // dividend is a zero and divisor is nonzero finite => result equals dividend
31 if (!(std::isfinite(x) && (!std::isfinite(y) && !std::isnan(y))) &&
32 !(x == 0 && (y != 0 && std::isfinite(y)))) {
33 x = fmod(x, y);
34 }
35 return x;
36}
37#else // POSIX
38
39double modulo(double x, double y) {
Ben Murdoch014dc512016-03-22 12:00:34 +000040#if defined(V8_OS_AIX)
41 // AIX raises an underflow exception for (Number.MIN_VALUE % Number.MAX_VALUE)
42 feclearexcept(FE_ALL_EXCEPT);
43 double result = std::fmod(x, y);
44 int exception = fetestexcept(FE_UNDERFLOW);
45 return (exception ? x : result);
46#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047 return std::fmod(x, y);
Ben Murdoch014dc512016-03-22 12:00:34 +000048#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049}
Ben Murdoch014dc512016-03-22 12:00:34 +000050#endif // defined(V8_OS_WIN)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051
52
Ben Murdoch014dc512016-03-22 12:00:34 +000053#define UNARY_MATH_FUNCTION(name, generator) \
54 static UnaryMathFunctionWithIsolate fast_##name##_function = nullptr; \
55 double std_##name(double x, Isolate* isolate) { return std::name(x); } \
56 void init_fast_##name##_function(Isolate* isolate) { \
57 if (FLAG_fast_math) fast_##name##_function = generator(isolate); \
58 if (!fast_##name##_function) fast_##name##_function = std_##name; \
59 } \
60 void lazily_initialize_fast_##name(Isolate* isolate) { \
61 if (!fast_##name##_function) init_fast_##name##_function(isolate); \
62 } \
63 double fast_##name(double x, Isolate* isolate) { \
64 return (*fast_##name##_function)(x, isolate); \
65 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066
Ben Murdoch014dc512016-03-22 12:00:34 +000067UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068
69#undef UNARY_MATH_FUNCTION
70
71
Andrei Popescu31002712010-02-23 13:46:05 +000072#define __ ACCESS_MASM(masm_)
73
74#ifdef DEBUG
75
76Comment::Comment(MacroAssembler* masm, const char* msg)
77 : masm_(masm), msg_(msg) {
78 __ RecordComment(msg);
79}
80
81
82Comment::~Comment() {
83 if (msg_[0] == '[') __ RecordComment("]");
84}
85
86#endif // DEBUG
87
88#undef __
89
Steve Blocka7e24c12009-10-30 11:49:00 +000090
Ben Murdochb8a8cc12014-11-26 15:28:44 +000091void CodeGenerator::MakeCodePrologue(CompilationInfo* info, const char* kind) {
Steve Blocka7e24c12009-10-30 11:49:00 +000092 bool print_ast = false;
93 const char* ftype;
94
Ben Murdochb8a8cc12014-11-26 15:28:44 +000095 if (info->isolate()->bootstrapper()->IsActive()) {
Steve Blocka7e24c12009-10-30 11:49:00 +000096 print_ast = FLAG_print_builtin_ast;
97 ftype = "builtin";
98 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +000099 print_ast = FLAG_print_ast;
100 ftype = "user-defined";
101 }
102
Ben Murdochf91f0612016-11-29 16:50:11 +0000103 if (FLAG_trace_codegen || print_ast) {
104 std::unique_ptr<char[]> name = info->GetDebugName();
Ben Murdoch014dc512016-03-22 12:00:34 +0000105 PrintF("[generating %s code for %s function: %s]\n", kind, ftype,
106 name.get());
Steve Blocka7e24c12009-10-30 11:49:00 +0000107 }
108
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109#ifdef DEBUG
Ben Murdoch014dc512016-03-22 12:00:34 +0000110 if (info->parse_info() && print_ast) {
Andrei Popescu31002712010-02-23 13:46:05 +0000111 PrintF("--- AST ---\n%s\n",
Ben Murdoch014dc512016-03-22 12:00:34 +0000112 AstPrinter(info->isolate()).PrintProgram(info->literal()));
Steve Block3ce2e202009-11-05 08:53:23 +0000113 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000114#endif // DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +0000115}
Steve Blocka7e24c12009-10-30 11:49:00 +0000116
Andrei Popescu31002712010-02-23 13:46:05 +0000117Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm,
Ben Murdochf91f0612016-11-29 16:50:11 +0000118 EhFrameWriter* eh_frame_writer,
119 CompilationInfo* info,
120 Handle<Object> self_reference) {
Steve Block44f0eee2011-05-26 01:26:41 +0100121 Isolate* isolate = info->isolate();
122
Steve Block3ce2e202009-11-05 08:53:23 +0000123 // Allocate and install the code.
Steve Blocka7e24c12009-10-30 11:49:00 +0000124 CodeDesc desc;
Ben Murdoch109988c2016-05-18 11:27:45 +0100125 Code::Flags flags = info->code_flags();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000126 bool is_crankshafted =
127 Code::ExtractKindFromFlags(flags) == Code::OPTIMIZED_FUNCTION ||
128 info->IsStub();
Steve Block3ce2e202009-11-05 08:53:23 +0000129 masm->GetCode(&desc);
Ben Murdochf91f0612016-11-29 16:50:11 +0000130 if (eh_frame_writer) eh_frame_writer->GetEhFrame(&desc);
131
132 Handle<Code> code = isolate->factory()->NewCode(
133 desc, flags, self_reference, false, is_crankshafted,
134 info->prologue_offset(), info->is_debug() && !is_crankshafted);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135 isolate->counters()->total_compiled_code_size()->Increment(
136 code->instruction_size());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100137 return code;
138}
139
Ben Murdoch62ed6312017-06-06 11:06:27 +0100140// Print function's source if it was not printed before.
141// Return a sequential id under which this function was printed.
142static int PrintFunctionSource(CompilationInfo* info,
143 std::vector<Handle<SharedFunctionInfo>>* printed,
144 int inlining_id,
145 Handle<SharedFunctionInfo> shared) {
146 // Outermost function has source id -1 and inlined functions take
147 // source ids starting from 0.
148 int source_id = -1;
149 if (inlining_id != SourcePosition::kNotInlined) {
150 for (unsigned i = 0; i < printed->size(); i++) {
151 if (printed->at(i).is_identical_to(shared)) {
152 return i;
153 }
154 }
155 source_id = static_cast<int>(printed->size());
156 printed->push_back(shared);
157 }
158
159 Isolate* isolate = info->isolate();
160 if (!shared->script()->IsUndefined(isolate)) {
161 Handle<Script> script(Script::cast(shared->script()), isolate);
162
163 if (!script->source()->IsUndefined(isolate)) {
164 CodeTracer::Scope tracing_scope(isolate->GetCodeTracer());
165 Object* source_name = script->name();
166 OFStream os(tracing_scope.file());
167 os << "--- FUNCTION SOURCE (";
168 if (source_name->IsString()) {
169 os << String::cast(source_name)->ToCString().get() << ":";
170 }
171 os << shared->DebugName()->ToCString().get() << ") id{";
172 os << info->optimization_id() << "," << source_id << "} start{";
173 os << shared->start_position() << "} ---\n";
174 {
175 DisallowHeapAllocation no_allocation;
176 int start = shared->start_position();
177 int len = shared->end_position() - start;
178 String::SubStringRange source(String::cast(script->source()), start,
179 len);
180 for (const auto& c : source) {
181 os << AsReversiblyEscapedUC16(c);
182 }
183 }
184
185 os << "\n--- END ---\n";
186 }
187 }
188
189 return source_id;
190}
191
192// Print information for the given inlining: which function was inlined and
193// where the inlining occured.
194static void PrintInlinedFunctionInfo(
195 CompilationInfo* info, int source_id, int inlining_id,
196 const CompilationInfo::InlinedFunctionHolder& h) {
197 CodeTracer::Scope tracing_scope(info->isolate()->GetCodeTracer());
198 OFStream os(tracing_scope.file());
199 os << "INLINE (" << h.shared_info->DebugName()->ToCString().get() << ") id{"
200 << info->optimization_id() << "," << source_id << "} AS " << inlining_id
201 << " AT ";
202 const SourcePosition position = h.position.position;
203 if (position.IsKnown()) {
204 os << "<" << position.InliningId() << ":" << position.ScriptOffset() << ">";
205 } else {
206 os << "<?>";
207 }
208 os << std::endl;
209}
210
211// Print the source of all functions that participated in this optimizing
212// compilation. For inlined functions print source position of their inlining.
213static void DumpParticipatingSource(CompilationInfo* info) {
214 AllowDeferredHandleDereference allow_deference_for_print_code;
215
216 std::vector<Handle<SharedFunctionInfo>> printed;
217 printed.reserve(info->inlined_functions().size());
218
219 PrintFunctionSource(info, &printed, SourcePosition::kNotInlined,
220 info->shared_info());
221 const auto& inlined = info->inlined_functions();
222 for (unsigned id = 0; id < inlined.size(); id++) {
223 const int source_id =
224 PrintFunctionSource(info, &printed, id, inlined[id].shared_info);
225 PrintInlinedFunctionInfo(info, source_id, id, inlined[id]);
226 }
227}
Ben Murdochb0fe1622011-05-05 13:52:32 +0100228
229void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) {
Ben Murdoch62ed6312017-06-06 11:06:27 +0100230 if (FLAG_print_opt_source && info->IsOptimizing()) {
231 DumpParticipatingSource(info);
232 }
233
Steve Blocka7e24c12009-10-30 11:49:00 +0000234#ifdef ENABLE_DISASSEMBLER
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000235 AllowDeferredHandleDereference allow_deference_for_print_code;
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100236 Isolate* isolate = info->isolate();
237 bool print_code =
238 isolate->bootstrapper()->IsActive()
239 ? FLAG_print_builtin_code
240 : (FLAG_print_code || (info->IsStub() && FLAG_print_code_stubs) ||
Ben Murdochf3b273f2017-01-17 12:11:28 +0000241 (info->IsOptimizing() && FLAG_print_opt_code &&
Ben Murdoch62ed6312017-06-06 11:06:27 +0100242 info->shared_info()->PassesFilter(FLAG_print_opt_code_filter)) ||
243 (info->IsWasm() && FLAG_print_wasm_code));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100244 if (print_code) {
Ben Murdochf91f0612016-11-29 16:50:11 +0000245 std::unique_ptr<char[]> debug_name = info->GetDebugName();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000246 CodeTracer::Scope tracing_scope(info->isolate()->GetCodeTracer());
247 OFStream os(tracing_scope.file());
Ben Murdoch014dc512016-03-22 12:00:34 +0000248
249 // Print the source code if available.
250 bool print_source =
251 info->parse_info() && (code->kind() == Code::OPTIMIZED_FUNCTION ||
252 code->kind() == Code::FUNCTION);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253 if (print_source) {
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100254 Handle<SharedFunctionInfo> shared = info->shared_info();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255 Handle<Script> script = info->script();
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100256 if (!script->IsUndefined(isolate) &&
257 !script->source()->IsUndefined(isolate)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000258 os << "--- Raw source ---\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000259 StringCharacterStream stream(String::cast(script->source()),
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100260 shared->start_position());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000261 // fun->end_position() points to the last character in the stream. We
262 // need to compensate by adding one to calculate the length.
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100263 int source_len = shared->end_position() - shared->start_position() + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000264 for (int i = 0; i < source_len; i++) {
265 if (stream.HasMore()) {
266 os << AsReversiblyEscapedUC16(stream.GetNext());
267 }
268 }
269 os << "\n\n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000270 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000271 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100272 if (info->IsOptimizing()) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000273 if (FLAG_print_unopt_code && info->parse_info()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000274 os << "--- Unoptimized code ---\n";
Ben Murdoch014dc512016-03-22 12:00:34 +0000275 info->closure()->shared()->code()->Disassemble(debug_name.get(), os);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100276 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277 os << "--- Optimized code ---\n"
278 << "optimization_id = " << info->optimization_id() << "\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100279 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000280 os << "--- Code ---\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100281 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282 if (print_source) {
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100283 Handle<SharedFunctionInfo> shared = info->shared_info();
284 os << "source_position = " << shared->start_position() << "\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000285 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000286 code->Disassemble(debug_name.get(), os);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000287 os << "--- End code ---\n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000288 }
289#endif // ENABLE_DISASSEMBLER
Steve Blocka7e24c12009-10-30 11:49:00 +0000290}
291
Ben Murdoch014dc512016-03-22 12:00:34 +0000292} // namespace internal
293} // namespace v8