| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include "src/codegen.h" |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6 | |
| 7 | #if defined(V8_OS_AIX) |
| 8 | #include <fenv.h> // NOLINT(build/c++11) |
| 9 | #endif |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 10 | |
| 11 | #include <memory> |
| 12 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13 | #include "src/ast/prettyprinter.h" |
| 14 | #include "src/bootstrapper.h" |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 15 | #include "src/compilation-info.h" |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame^] | 16 | #include "src/counters.h" |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17 | #include "src/debug/debug.h" |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 18 | #include "src/eh-frame.h" |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame^] | 19 | #include "src/objects-inl.h" |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 20 | #include "src/runtime/runtime.h" |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 21 | |
| 22 | namespace v8 { |
| 23 | namespace internal { |
| 24 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 25 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 26 | #if defined(V8_OS_WIN) |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 27 | double 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 | |
| 39 | double modulo(double x, double y) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 40 | #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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 47 | return std::fmod(x, y); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 48 | #endif |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 49 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 50 | #endif // defined(V8_OS_WIN) |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 51 | |
| 52 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 53 | #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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 66 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 67 | UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction) |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 68 | |
| 69 | #undef UNARY_MATH_FUNCTION |
| 70 | |
| 71 | |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 72 | #define __ ACCESS_MASM(masm_) |
| 73 | |
| 74 | #ifdef DEBUG |
| 75 | |
| 76 | Comment::Comment(MacroAssembler* masm, const char* msg) |
| 77 | : masm_(masm), msg_(msg) { |
| 78 | __ RecordComment(msg); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | Comment::~Comment() { |
| 83 | if (msg_[0] == '[') __ RecordComment("]"); |
| 84 | } |
| 85 | |
| 86 | #endif // DEBUG |
| 87 | |
| 88 | #undef __ |
| 89 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 90 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 91 | void CodeGenerator::MakeCodePrologue(CompilationInfo* info, const char* kind) { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | bool print_ast = false; |
| 93 | const char* ftype; |
| 94 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 95 | if (info->isolate()->bootstrapper()->IsActive()) { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 96 | print_ast = FLAG_print_builtin_ast; |
| 97 | ftype = "builtin"; |
| 98 | } else { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 99 | print_ast = FLAG_print_ast; |
| 100 | ftype = "user-defined"; |
| 101 | } |
| 102 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 103 | if (FLAG_trace_codegen || print_ast) { |
| 104 | std::unique_ptr<char[]> name = info->GetDebugName(); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 105 | PrintF("[generating %s code for %s function: %s]\n", kind, ftype, |
| 106 | name.get()); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 109 | #ifdef DEBUG |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 110 | if (info->parse_info() && print_ast) { |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 111 | PrintF("--- AST ---\n%s\n", |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 112 | AstPrinter(info->isolate()).PrintProgram(info->literal())); |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 113 | } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 114 | #endif // DEBUG |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 115 | } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 116 | |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 117 | Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm, |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 118 | EhFrameWriter* eh_frame_writer, |
| 119 | CompilationInfo* info, |
| 120 | Handle<Object> self_reference) { |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 121 | Isolate* isolate = info->isolate(); |
| 122 | |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 123 | // Allocate and install the code. |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 124 | CodeDesc desc; |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 125 | Code::Flags flags = info->code_flags(); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 126 | bool is_crankshafted = |
| 127 | Code::ExtractKindFromFlags(flags) == Code::OPTIMIZED_FUNCTION || |
| 128 | info->IsStub(); |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 129 | masm->GetCode(&desc); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 130 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 135 | isolate->counters()->total_compiled_code_size()->Increment( |
| 136 | code->instruction_size()); |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 137 | return code; |
| 138 | } |
| 139 | |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame^] | 140 | // Print function's source if it was not printed before. |
| 141 | // Return a sequential id under which this function was printed. |
| 142 | static 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. |
| 194 | static 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. |
| 213 | static 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 Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 228 | |
| 229 | void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) { |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame^] | 230 | if (FLAG_print_opt_source && info->IsOptimizing()) { |
| 231 | DumpParticipatingSource(info); |
| 232 | } |
| 233 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | #ifdef ENABLE_DISASSEMBLER |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 235 | AllowDeferredHandleDereference allow_deference_for_print_code; |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 236 | 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 Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 241 | (info->IsOptimizing() && FLAG_print_opt_code && |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame^] | 242 | info->shared_info()->PassesFilter(FLAG_print_opt_code_filter)) || |
| 243 | (info->IsWasm() && FLAG_print_wasm_code)); |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 244 | if (print_code) { |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 245 | std::unique_ptr<char[]> debug_name = info->GetDebugName(); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 246 | CodeTracer::Scope tracing_scope(info->isolate()->GetCodeTracer()); |
| 247 | OFStream os(tracing_scope.file()); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 248 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 253 | if (print_source) { |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 254 | Handle<SharedFunctionInfo> shared = info->shared_info(); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 255 | Handle<Script> script = info->script(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 256 | if (!script->IsUndefined(isolate) && |
| 257 | !script->source()->IsUndefined(isolate)) { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 258 | os << "--- Raw source ---\n"; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 259 | StringCharacterStream stream(String::cast(script->source()), |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 260 | shared->start_position()); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 261 | // fun->end_position() points to the last character in the stream. We |
| 262 | // need to compensate by adding one to calculate the length. |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 263 | int source_len = shared->end_position() - shared->start_position() + 1; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 264 | for (int i = 0; i < source_len; i++) { |
| 265 | if (stream.HasMore()) { |
| 266 | os << AsReversiblyEscapedUC16(stream.GetNext()); |
| 267 | } |
| 268 | } |
| 269 | os << "\n\n"; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 270 | } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 271 | } |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 272 | if (info->IsOptimizing()) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 273 | if (FLAG_print_unopt_code && info->parse_info()) { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 274 | os << "--- Unoptimized code ---\n"; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 275 | info->closure()->shared()->code()->Disassemble(debug_name.get(), os); |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 276 | } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 277 | os << "--- Optimized code ---\n" |
| 278 | << "optimization_id = " << info->optimization_id() << "\n"; |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 279 | } else { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 280 | os << "--- Code ---\n"; |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 281 | } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 282 | if (print_source) { |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 283 | Handle<SharedFunctionInfo> shared = info->shared_info(); |
| 284 | os << "source_position = " << shared->start_position() << "\n"; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 285 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 286 | code->Disassemble(debug_name.get(), os); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 287 | os << "--- End code ---\n"; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 288 | } |
| 289 | #endif // ENABLE_DISASSEMBLER |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 292 | } // namespace internal |
| 293 | } // namespace v8 |