blob: 692fa64bb69d34e2fe2f1ff6c815c2a5b7852b1e [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"
15#include "src/profiler/cpu-profiler.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040016#include "src/runtime/runtime.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000017
18namespace v8 {
19namespace internal {
20
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022#if defined(V8_OS_WIN)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023double modulo(double x, double y) {
24 // Workaround MS fmod bugs. ECMA-262 says:
25 // dividend is finite and divisor is an infinity => result equals dividend
26 // dividend is a zero and divisor is nonzero finite => result equals dividend
27 if (!(std::isfinite(x) && (!std::isfinite(y) && !std::isnan(y))) &&
28 !(x == 0 && (y != 0 && std::isfinite(y)))) {
29 x = fmod(x, y);
30 }
31 return x;
32}
33#else // POSIX
34
35double modulo(double x, double y) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000036#if defined(V8_OS_AIX)
37 // AIX raises an underflow exception for (Number.MIN_VALUE % Number.MAX_VALUE)
38 feclearexcept(FE_ALL_EXCEPT);
39 double result = std::fmod(x, y);
40 int exception = fetestexcept(FE_UNDERFLOW);
41 return (exception ? x : result);
42#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043 return std::fmod(x, y);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046#endif // defined(V8_OS_WIN)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047
48
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000049#define UNARY_MATH_FUNCTION(name, generator) \
50 static UnaryMathFunctionWithIsolate fast_##name##_function = nullptr; \
51 double std_##name(double x, Isolate* isolate) { return std::name(x); } \
52 void init_fast_##name##_function(Isolate* isolate) { \
53 if (FLAG_fast_math) fast_##name##_function = generator(isolate); \
54 if (!fast_##name##_function) fast_##name##_function = std_##name; \
55 } \
56 void lazily_initialize_fast_##name(Isolate* isolate) { \
57 if (!fast_##name##_function) init_fast_##name##_function(isolate); \
58 } \
59 double fast_##name(double x, Isolate* isolate) { \
60 return (*fast_##name##_function)(x, isolate); \
61 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000063UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction)
64UNARY_MATH_FUNCTION(exp, CreateExpFunction)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065
66#undef UNARY_MATH_FUNCTION
67
68
Andrei Popescu31002712010-02-23 13:46:05 +000069#define __ ACCESS_MASM(masm_)
70
71#ifdef DEBUG
72
73Comment::Comment(MacroAssembler* masm, const char* msg)
74 : masm_(masm), msg_(msg) {
75 __ RecordComment(msg);
76}
77
78
79Comment::~Comment() {
80 if (msg_[0] == '[') __ RecordComment("]");
81}
82
83#endif // DEBUG
84
85#undef __
86
Steve Blocka7e24c12009-10-30 11:49:00 +000087
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088void CodeGenerator::MakeCodePrologue(CompilationInfo* info, const char* kind) {
Steve Blocka7e24c12009-10-30 11:49:00 +000089 bool print_source = false;
90 bool print_ast = false;
91 const char* ftype;
92
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093 if (info->isolate()->bootstrapper()->IsActive()) {
Steve Blocka7e24c12009-10-30 11:49:00 +000094 print_source = FLAG_print_builtin_source;
95 print_ast = FLAG_print_builtin_ast;
96 ftype = "builtin";
97 } else {
98 print_source = FLAG_print_source;
99 print_ast = FLAG_print_ast;
100 ftype = "user-defined";
101 }
102
103 if (FLAG_trace_codegen || print_source || print_ast) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000104 base::SmartArrayPointer<char> name = info->GetDebugName();
105 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 Murdoch4a90d5f2016-03-22 12:00:34 +0000110 if (info->parse_info() && print_source) {
Andrei Popescu31002712010-02-23 13:46:05 +0000111 PrintF("--- Source from AST ---\n%s\n",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000112 PrettyPrinter(info->isolate()).PrintProgram(info->literal()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000113 }
114
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000115 if (info->parse_info() && print_ast) {
Andrei Popescu31002712010-02-23 13:46:05 +0000116 PrintF("--- AST ---\n%s\n",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000117 AstPrinter(info->isolate()).PrintProgram(info->literal()));
Steve Block3ce2e202009-11-05 08:53:23 +0000118 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000119#endif // DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +0000120}
Steve Blocka7e24c12009-10-30 11:49:00 +0000121
Steve Blocka7e24c12009-10-30 11:49:00 +0000122
Andrei Popescu31002712010-02-23 13:46:05 +0000123Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm,
Andrei Popescu31002712010-02-23 13:46:05 +0000124 CompilationInfo* info) {
Steve Block44f0eee2011-05-26 01:26:41 +0100125 Isolate* isolate = info->isolate();
126
Steve Block3ce2e202009-11-05 08:53:23 +0000127 // Allocate and install the code.
Steve Blocka7e24c12009-10-30 11:49:00 +0000128 CodeDesc desc;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100129 Code::Flags flags = info->code_flags();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000130 bool is_crankshafted =
131 Code::ExtractKindFromFlags(flags) == Code::OPTIMIZED_FUNCTION ||
132 info->IsStub();
Steve Block3ce2e202009-11-05 08:53:23 +0000133 masm->GetCode(&desc);
Steve Block44f0eee2011-05-26 01:26:41 +0100134 Handle<Code> code =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135 isolate->factory()->NewCode(desc, flags, masm->CodeObject(),
136 false, is_crankshafted,
137 info->prologue_offset(),
138 info->is_debug() && !is_crankshafted);
139 isolate->counters()->total_compiled_code_size()->Increment(
140 code->instruction_size());
141 isolate->heap()->IncrementCodeGeneratedBytes(is_crankshafted,
142 code->instruction_size());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100143 return code;
144}
145
146
147void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000148#ifdef ENABLE_DISASSEMBLER
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149 AllowDeferredHandleDereference allow_deference_for_print_code;
150 bool print_code = info->isolate()->bootstrapper()->IsActive()
Steve Block3ce2e202009-11-05 08:53:23 +0000151 ? FLAG_print_builtin_code
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152 : (FLAG_print_code ||
153 (info->IsStub() && FLAG_print_code_stubs) ||
154 (info->IsOptimizing() && FLAG_print_opt_code));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100155 if (print_code) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000156 base::SmartArrayPointer<char> debug_name = info->GetDebugName();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000157 CodeTracer::Scope tracing_scope(info->isolate()->GetCodeTracer());
158 OFStream os(tracing_scope.file());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000159
160 // Print the source code if available.
161 bool print_source =
162 info->parse_info() && (code->kind() == Code::OPTIMIZED_FUNCTION ||
163 code->kind() == Code::FUNCTION);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000164 if (print_source) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000165 FunctionLiteral* literal = info->literal();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000166 Handle<Script> script = info->script();
167 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
168 os << "--- Raw source ---\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169 StringCharacterStream stream(String::cast(script->source()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000170 literal->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.
173 int source_len =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000174 literal->end_position() - literal->start_position() + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000175 for (int i = 0; i < source_len; i++) {
176 if (stream.HasMore()) {
177 os << AsReversiblyEscapedUC16(stream.GetNext());
178 }
179 }
180 os << "\n\n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000181 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000182 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100183 if (info->IsOptimizing()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000184 if (FLAG_print_unopt_code && info->parse_info()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000185 os << "--- Unoptimized code ---\n";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000186 info->closure()->shared()->code()->Disassemble(debug_name.get(), os);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100187 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000188 os << "--- Optimized code ---\n"
189 << "optimization_id = " << info->optimization_id() << "\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100190 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000191 os << "--- Code ---\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100192 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000193 if (print_source) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000194 FunctionLiteral* literal = info->literal();
195 os << "source_position = " << literal->start_position() << "\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000196 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000197 code->Disassemble(debug_name.get(), os);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000198 os << "--- End code ---\n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000199 }
200#endif // ENABLE_DISASSEMBLER
Steve Blocka7e24c12009-10-30 11:49:00 +0000201}
202
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000203} // namespace internal
204} // namespace v8