blob: 4597ae27669a4954ea1d7c841497dec6be0142af [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 Murdoch4a90d5f2016-03-22 12:00:34 +00006
7#if defined(V8_OS_AIX)
8#include <fenv.h> // NOLINT(build/c++11)
9#endif
10#include "src/ast/prettyprinter.h"
11#include "src/bootstrapper.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012#include "src/compiler.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013#include "src/debug/debug.h"
14#include "src/parsing/parser.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040015#include "src/runtime/runtime.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000016
17namespace v8 {
18namespace internal {
19
Ben Murdochb8a8cc12014-11-26 15:28:44 +000020
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000021#if defined(V8_OS_WIN)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022double modulo(double x, double y) {
23 // Workaround MS fmod bugs. ECMA-262 says:
24 // dividend is finite and divisor is an infinity => result equals dividend
25 // dividend is a zero and divisor is nonzero finite => result equals dividend
26 if (!(std::isfinite(x) && (!std::isfinite(y) && !std::isnan(y))) &&
27 !(x == 0 && (y != 0 && std::isfinite(y)))) {
28 x = fmod(x, y);
29 }
30 return x;
31}
32#else // POSIX
33
34double modulo(double x, double y) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035#if defined(V8_OS_AIX)
36 // AIX raises an underflow exception for (Number.MIN_VALUE % Number.MAX_VALUE)
37 feclearexcept(FE_ALL_EXCEPT);
38 double result = std::fmod(x, y);
39 int exception = fetestexcept(FE_UNDERFLOW);
40 return (exception ? x : result);
41#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042 return std::fmod(x, y);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045#endif // defined(V8_OS_WIN)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046
47
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000048#define UNARY_MATH_FUNCTION(name, generator) \
49 static UnaryMathFunctionWithIsolate fast_##name##_function = nullptr; \
50 double std_##name(double x, Isolate* isolate) { return std::name(x); } \
51 void init_fast_##name##_function(Isolate* isolate) { \
52 if (FLAG_fast_math) fast_##name##_function = generator(isolate); \
53 if (!fast_##name##_function) fast_##name##_function = std_##name; \
54 } \
55 void lazily_initialize_fast_##name(Isolate* isolate) { \
56 if (!fast_##name##_function) init_fast_##name##_function(isolate); \
57 } \
58 double fast_##name(double x, Isolate* isolate) { \
59 return (*fast_##name##_function)(x, isolate); \
60 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000062UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063
64#undef UNARY_MATH_FUNCTION
65
66
Andrei Popescu31002712010-02-23 13:46:05 +000067#define __ ACCESS_MASM(masm_)
68
69#ifdef DEBUG
70
71Comment::Comment(MacroAssembler* masm, const char* msg)
72 : masm_(masm), msg_(msg) {
73 __ RecordComment(msg);
74}
75
76
77Comment::~Comment() {
78 if (msg_[0] == '[') __ RecordComment("]");
79}
80
81#endif // DEBUG
82
83#undef __
84
Steve Blocka7e24c12009-10-30 11:49:00 +000085
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086void CodeGenerator::MakeCodePrologue(CompilationInfo* info, const char* kind) {
Steve Blocka7e24c12009-10-30 11:49:00 +000087 bool print_source = false;
88 bool print_ast = false;
89 const char* ftype;
90
Ben Murdochb8a8cc12014-11-26 15:28:44 +000091 if (info->isolate()->bootstrapper()->IsActive()) {
Steve Blocka7e24c12009-10-30 11:49:00 +000092 print_source = FLAG_print_builtin_source;
93 print_ast = FLAG_print_builtin_ast;
94 ftype = "builtin";
95 } else {
96 print_source = FLAG_print_source;
97 print_ast = FLAG_print_ast;
98 ftype = "user-defined";
99 }
100
101 if (FLAG_trace_codegen || print_source || print_ast) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000102 base::SmartArrayPointer<char> name = info->GetDebugName();
103 PrintF("[generating %s code for %s function: %s]\n", kind, ftype,
104 name.get());
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 }
106
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000107#ifdef DEBUG
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000108 if (info->parse_info() && print_source) {
Andrei Popescu31002712010-02-23 13:46:05 +0000109 PrintF("--- Source from AST ---\n%s\n",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000110 PrettyPrinter(info->isolate()).PrintProgram(info->literal()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000111 }
112
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113 if (info->parse_info() && print_ast) {
Andrei Popescu31002712010-02-23 13:46:05 +0000114 PrintF("--- AST ---\n%s\n",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000115 AstPrinter(info->isolate()).PrintProgram(info->literal()));
Steve Block3ce2e202009-11-05 08:53:23 +0000116 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000117#endif // DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +0000118}
Steve Blocka7e24c12009-10-30 11:49:00 +0000119
Steve Blocka7e24c12009-10-30 11:49:00 +0000120
Andrei Popescu31002712010-02-23 13:46:05 +0000121Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm,
Andrei Popescu31002712010-02-23 13:46:05 +0000122 CompilationInfo* info) {
Steve Block44f0eee2011-05-26 01:26:41 +0100123 Isolate* isolate = info->isolate();
124
Steve Block3ce2e202009-11-05 08:53:23 +0000125 // Allocate and install the code.
Steve Blocka7e24c12009-10-30 11:49:00 +0000126 CodeDesc desc;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100127 Code::Flags flags = info->code_flags();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128 bool is_crankshafted =
129 Code::ExtractKindFromFlags(flags) == Code::OPTIMIZED_FUNCTION ||
130 info->IsStub();
Steve Block3ce2e202009-11-05 08:53:23 +0000131 masm->GetCode(&desc);
Steve Block44f0eee2011-05-26 01:26:41 +0100132 Handle<Code> code =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133 isolate->factory()->NewCode(desc, flags, masm->CodeObject(),
134 false, is_crankshafted,
135 info->prologue_offset(),
136 info->is_debug() && !is_crankshafted);
137 isolate->counters()->total_compiled_code_size()->Increment(
138 code->instruction_size());
139 isolate->heap()->IncrementCodeGeneratedBytes(is_crankshafted,
140 code->instruction_size());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100141 return code;
142}
143
144
145void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000146#ifdef ENABLE_DISASSEMBLER
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000147 AllowDeferredHandleDereference allow_deference_for_print_code;
Ben Murdoch61f157c2016-09-16 13:49:30 +0100148 Isolate* isolate = info->isolate();
149 bool print_code =
150 isolate->bootstrapper()->IsActive()
151 ? FLAG_print_builtin_code
152 : (FLAG_print_code || (info->IsStub() && FLAG_print_code_stubs) ||
153 (info->IsOptimizing() && FLAG_print_opt_code));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100154 if (print_code) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000155 base::SmartArrayPointer<char> debug_name = info->GetDebugName();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156 CodeTracer::Scope tracing_scope(info->isolate()->GetCodeTracer());
157 OFStream os(tracing_scope.file());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000158
159 // Print the source code if available.
160 bool print_source =
161 info->parse_info() && (code->kind() == Code::OPTIMIZED_FUNCTION ||
162 code->kind() == Code::FUNCTION);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 if (print_source) {
Ben Murdochc5610432016-08-08 18:44:38 +0100164 Handle<SharedFunctionInfo> shared = info->shared_info();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000165 Handle<Script> script = info->script();
Ben Murdoch61f157c2016-09-16 13:49:30 +0100166 if (!script->IsUndefined(isolate) &&
167 !script->source()->IsUndefined(isolate)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000168 os << "--- Raw source ---\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169 StringCharacterStream stream(String::cast(script->source()),
Ben Murdochc5610432016-08-08 18:44:38 +0100170 shared->start_position());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000171 // fun->end_position() points to the last character in the stream. We
172 // need to compensate by adding one to calculate the length.
Ben Murdochc5610432016-08-08 18:44:38 +0100173 int source_len = shared->end_position() - shared->start_position() + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000174 for (int i = 0; i < source_len; i++) {
175 if (stream.HasMore()) {
176 os << AsReversiblyEscapedUC16(stream.GetNext());
177 }
178 }
179 os << "\n\n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000180 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000181 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100182 if (info->IsOptimizing()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000183 if (FLAG_print_unopt_code && info->parse_info()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000184 os << "--- Unoptimized code ---\n";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000185 info->closure()->shared()->code()->Disassemble(debug_name.get(), os);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100186 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000187 os << "--- Optimized code ---\n"
188 << "optimization_id = " << info->optimization_id() << "\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100189 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000190 os << "--- Code ---\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100191 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192 if (print_source) {
Ben Murdochc5610432016-08-08 18:44:38 +0100193 Handle<SharedFunctionInfo> shared = info->shared_info();
194 os << "source_position = " << shared->start_position() << "\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000195 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000196 code->Disassemble(debug_name.get(), os);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000197 os << "--- End code ---\n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000198 }
199#endif // ENABLE_DISASSEMBLER
Steve Blocka7e24c12009-10-30 11:49:00 +0000200}
201
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000202} // namespace internal
203} // namespace v8