blob: afd8a6f592972da8bf5d9e150a4b7fa5566ae191 [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 Murdoch014dc512016-03-22 12:00:34 +000016#include "src/debug/debug.h"
Ben Murdochf91f0612016-11-29 16:50:11 +000017#include "src/eh-frame.h"
Emily Bernier958fae72015-03-24 16:35:39 -040018#include "src/runtime/runtime.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000019
20namespace v8 {
21namespace internal {
22
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023
Ben Murdoch014dc512016-03-22 12:00:34 +000024#if defined(V8_OS_WIN)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025double modulo(double x, double y) {
26 // Workaround MS fmod bugs. ECMA-262 says:
27 // dividend is finite and divisor is an infinity => result equals dividend
28 // dividend is a zero and divisor is nonzero finite => result equals dividend
29 if (!(std::isfinite(x) && (!std::isfinite(y) && !std::isnan(y))) &&
30 !(x == 0 && (y != 0 && std::isfinite(y)))) {
31 x = fmod(x, y);
32 }
33 return x;
34}
35#else // POSIX
36
37double modulo(double x, double y) {
Ben Murdoch014dc512016-03-22 12:00:34 +000038#if defined(V8_OS_AIX)
39 // AIX raises an underflow exception for (Number.MIN_VALUE % Number.MAX_VALUE)
40 feclearexcept(FE_ALL_EXCEPT);
41 double result = std::fmod(x, y);
42 int exception = fetestexcept(FE_UNDERFLOW);
43 return (exception ? x : result);
44#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045 return std::fmod(x, y);
Ben Murdoch014dc512016-03-22 12:00:34 +000046#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047}
Ben Murdoch014dc512016-03-22 12:00:34 +000048#endif // defined(V8_OS_WIN)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049
50
Ben Murdoch014dc512016-03-22 12:00:34 +000051#define UNARY_MATH_FUNCTION(name, generator) \
52 static UnaryMathFunctionWithIsolate fast_##name##_function = nullptr; \
53 double std_##name(double x, Isolate* isolate) { return std::name(x); } \
54 void init_fast_##name##_function(Isolate* isolate) { \
55 if (FLAG_fast_math) fast_##name##_function = generator(isolate); \
56 if (!fast_##name##_function) fast_##name##_function = std_##name; \
57 } \
58 void lazily_initialize_fast_##name(Isolate* isolate) { \
59 if (!fast_##name##_function) init_fast_##name##_function(isolate); \
60 } \
61 double fast_##name(double x, Isolate* isolate) { \
62 return (*fast_##name##_function)(x, isolate); \
63 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064
Ben Murdoch014dc512016-03-22 12:00:34 +000065UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066
67#undef UNARY_MATH_FUNCTION
68
69
Andrei Popescu31002712010-02-23 13:46:05 +000070#define __ ACCESS_MASM(masm_)
71
72#ifdef DEBUG
73
74Comment::Comment(MacroAssembler* masm, const char* msg)
75 : masm_(masm), msg_(msg) {
76 __ RecordComment(msg);
77}
78
79
80Comment::~Comment() {
81 if (msg_[0] == '[') __ RecordComment("]");
82}
83
84#endif // DEBUG
85
86#undef __
87
Steve Blocka7e24c12009-10-30 11:49:00 +000088
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089void CodeGenerator::MakeCodePrologue(CompilationInfo* info, const char* kind) {
Steve Blocka7e24c12009-10-30 11:49:00 +000090 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_ast = FLAG_print_builtin_ast;
95 ftype = "builtin";
96 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +000097 print_ast = FLAG_print_ast;
98 ftype = "user-defined";
99 }
100
Ben Murdochf91f0612016-11-29 16:50:11 +0000101 if (FLAG_trace_codegen || print_ast) {
102 std::unique_ptr<char[]> name = info->GetDebugName();
Ben Murdoch014dc512016-03-22 12:00:34 +0000103 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 Murdoch014dc512016-03-22 12:00:34 +0000108 if (info->parse_info() && print_ast) {
Andrei Popescu31002712010-02-23 13:46:05 +0000109 PrintF("--- AST ---\n%s\n",
Ben Murdoch014dc512016-03-22 12:00:34 +0000110 AstPrinter(info->isolate()).PrintProgram(info->literal()));
Steve Block3ce2e202009-11-05 08:53:23 +0000111 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000112#endif // DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +0000113}
Steve Blocka7e24c12009-10-30 11:49:00 +0000114
Andrei Popescu31002712010-02-23 13:46:05 +0000115Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm,
Ben Murdochf91f0612016-11-29 16:50:11 +0000116 EhFrameWriter* eh_frame_writer,
117 CompilationInfo* info,
118 Handle<Object> self_reference) {
Steve Block44f0eee2011-05-26 01:26:41 +0100119 Isolate* isolate = info->isolate();
120
Steve Block3ce2e202009-11-05 08:53:23 +0000121 // Allocate and install the code.
Steve Blocka7e24c12009-10-30 11:49:00 +0000122 CodeDesc desc;
Ben Murdoch109988c2016-05-18 11:27:45 +0100123 Code::Flags flags = info->code_flags();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124 bool is_crankshafted =
125 Code::ExtractKindFromFlags(flags) == Code::OPTIMIZED_FUNCTION ||
126 info->IsStub();
Steve Block3ce2e202009-11-05 08:53:23 +0000127 masm->GetCode(&desc);
Ben Murdochf91f0612016-11-29 16:50:11 +0000128 if (eh_frame_writer) eh_frame_writer->GetEhFrame(&desc);
129
130 Handle<Code> code = isolate->factory()->NewCode(
131 desc, flags, self_reference, false, is_crankshafted,
132 info->prologue_offset(), info->is_debug() && !is_crankshafted);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133 isolate->counters()->total_compiled_code_size()->Increment(
134 code->instruction_size());
135 isolate->heap()->IncrementCodeGeneratedBytes(is_crankshafted,
136 code->instruction_size());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100137 return code;
138}
139
140
141void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000142#ifdef ENABLE_DISASSEMBLER
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000143 AllowDeferredHandleDereference allow_deference_for_print_code;
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100144 Isolate* isolate = info->isolate();
145 bool print_code =
146 isolate->bootstrapper()->IsActive()
147 ? FLAG_print_builtin_code
148 : (FLAG_print_code || (info->IsStub() && FLAG_print_code_stubs) ||
Ben Murdochf3b273f2017-01-17 12:11:28 +0000149 (info->IsOptimizing() && FLAG_print_opt_code &&
150 info->shared_info()->PassesFilter(FLAG_print_opt_code_filter)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100151 if (print_code) {
Ben Murdochf91f0612016-11-29 16:50:11 +0000152 std::unique_ptr<char[]> debug_name = info->GetDebugName();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000153 CodeTracer::Scope tracing_scope(info->isolate()->GetCodeTracer());
154 OFStream os(tracing_scope.file());
Ben Murdoch014dc512016-03-22 12:00:34 +0000155
156 // Print the source code if available.
157 bool print_source =
158 info->parse_info() && (code->kind() == Code::OPTIMIZED_FUNCTION ||
159 code->kind() == Code::FUNCTION);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160 if (print_source) {
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100161 Handle<SharedFunctionInfo> shared = info->shared_info();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162 Handle<Script> script = info->script();
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100163 if (!script->IsUndefined(isolate) &&
164 !script->source()->IsUndefined(isolate)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000165 os << "--- Raw source ---\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000166 StringCharacterStream stream(String::cast(script->source()),
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100167 shared->start_position());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000168 // fun->end_position() points to the last character in the stream. We
169 // need to compensate by adding one to calculate the length.
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100170 int source_len = shared->end_position() - shared->start_position() + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000171 for (int i = 0; i < source_len; i++) {
172 if (stream.HasMore()) {
173 os << AsReversiblyEscapedUC16(stream.GetNext());
174 }
175 }
176 os << "\n\n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000177 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000178 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100179 if (info->IsOptimizing()) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000180 if (FLAG_print_unopt_code && info->parse_info()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181 os << "--- Unoptimized code ---\n";
Ben Murdoch014dc512016-03-22 12:00:34 +0000182 info->closure()->shared()->code()->Disassemble(debug_name.get(), os);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100183 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000184 os << "--- Optimized code ---\n"
185 << "optimization_id = " << info->optimization_id() << "\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100186 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000187 os << "--- Code ---\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100188 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000189 if (print_source) {
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100190 Handle<SharedFunctionInfo> shared = info->shared_info();
191 os << "source_position = " << shared->start_position() << "\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000193 code->Disassemble(debug_name.get(), os);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 os << "--- End code ---\n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000195 }
196#endif // ENABLE_DISASSEMBLER
Steve Blocka7e24c12009-10-30 11:49:00 +0000197}
198
Ben Murdoch014dc512016-03-22 12:00:34 +0000199} // namespace internal
200} // namespace v8