Home | History | Annotate | Download | only in src
      1 // Copyright 2012 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "src/codegen.h"
      6 
      7 #if defined(V8_OS_AIX)
      8 #include <fenv.h>  // NOLINT(build/c++11)
      9 #endif
     10 
     11 #include <memory>
     12 
     13 #include "src/ast/prettyprinter.h"
     14 #include "src/bootstrapper.h"
     15 #include "src/compilation-info.h"
     16 #include "src/debug/debug.h"
     17 #include "src/eh-frame.h"
     18 #include "src/runtime/runtime.h"
     19 
     20 namespace v8 {
     21 namespace internal {
     22 
     23 
     24 #if defined(V8_OS_WIN)
     25 double 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 
     37 double modulo(double x, double y) {
     38 #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
     45   return std::fmod(x, y);
     46 #endif
     47 }
     48 #endif  // defined(V8_OS_WIN)
     49 
     50 
     51 #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   }
     64 
     65 UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction)
     66 
     67 #undef UNARY_MATH_FUNCTION
     68 
     69 
     70 #define __ ACCESS_MASM(masm_)
     71 
     72 #ifdef DEBUG
     73 
     74 Comment::Comment(MacroAssembler* masm, const char* msg)
     75     : masm_(masm), msg_(msg) {
     76   __ RecordComment(msg);
     77 }
     78 
     79 
     80 Comment::~Comment() {
     81   if (msg_[0] == '[') __ RecordComment("]");
     82 }
     83 
     84 #endif  // DEBUG
     85 
     86 #undef __
     87 
     88 
     89 void CodeGenerator::MakeCodePrologue(CompilationInfo* info, const char* kind) {
     90   bool print_ast = false;
     91   const char* ftype;
     92 
     93   if (info->isolate()->bootstrapper()->IsActive()) {
     94     print_ast = FLAG_print_builtin_ast;
     95     ftype = "builtin";
     96   } else {
     97     print_ast = FLAG_print_ast;
     98     ftype = "user-defined";
     99   }
    100 
    101   if (FLAG_trace_codegen || print_ast) {
    102     std::unique_ptr<char[]> name = info->GetDebugName();
    103     PrintF("[generating %s code for %s function: %s]\n", kind, ftype,
    104            name.get());
    105   }
    106 
    107 #ifdef DEBUG
    108   if (info->parse_info() && print_ast) {
    109     PrintF("--- AST ---\n%s\n",
    110            AstPrinter(info->isolate()).PrintProgram(info->literal()));
    111   }
    112 #endif  // DEBUG
    113 }
    114 
    115 Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm,
    116                                              EhFrameWriter* eh_frame_writer,
    117                                              CompilationInfo* info,
    118                                              Handle<Object> self_reference) {
    119   Isolate* isolate = info->isolate();
    120 
    121   // Allocate and install the code.
    122   CodeDesc desc;
    123   Code::Flags flags = info->code_flags();
    124   bool is_crankshafted =
    125       Code::ExtractKindFromFlags(flags) == Code::OPTIMIZED_FUNCTION ||
    126       info->IsStub();
    127   masm->GetCode(&desc);
    128   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);
    133   isolate->counters()->total_compiled_code_size()->Increment(
    134       code->instruction_size());
    135   isolate->heap()->IncrementCodeGeneratedBytes(is_crankshafted,
    136       code->instruction_size());
    137   return code;
    138 }
    139 
    140 
    141 void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) {
    142 #ifdef ENABLE_DISASSEMBLER
    143   AllowDeferredHandleDereference allow_deference_for_print_code;
    144   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) ||
    149              (info->IsOptimizing() && FLAG_print_opt_code &&
    150               info->shared_info()->PassesFilter(FLAG_print_opt_code_filter)));
    151   if (print_code) {
    152     std::unique_ptr<char[]> debug_name = info->GetDebugName();
    153     CodeTracer::Scope tracing_scope(info->isolate()->GetCodeTracer());
    154     OFStream os(tracing_scope.file());
    155 
    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);
    160     if (print_source) {
    161       Handle<SharedFunctionInfo> shared = info->shared_info();
    162       Handle<Script> script = info->script();
    163       if (!script->IsUndefined(isolate) &&
    164           !script->source()->IsUndefined(isolate)) {
    165         os << "--- Raw source ---\n";
    166         StringCharacterStream stream(String::cast(script->source()),
    167                                      shared->start_position());
    168         // fun->end_position() points to the last character in the stream. We
    169         // need to compensate by adding one to calculate the length.
    170         int source_len = shared->end_position() - shared->start_position() + 1;
    171         for (int i = 0; i < source_len; i++) {
    172           if (stream.HasMore()) {
    173             os << AsReversiblyEscapedUC16(stream.GetNext());
    174           }
    175         }
    176         os << "\n\n";
    177       }
    178     }
    179     if (info->IsOptimizing()) {
    180       if (FLAG_print_unopt_code && info->parse_info()) {
    181         os << "--- Unoptimized code ---\n";
    182         info->closure()->shared()->code()->Disassemble(debug_name.get(), os);
    183       }
    184       os << "--- Optimized code ---\n"
    185          << "optimization_id = " << info->optimization_id() << "\n";
    186     } else {
    187       os << "--- Code ---\n";
    188     }
    189     if (print_source) {
    190       Handle<SharedFunctionInfo> shared = info->shared_info();
    191       os << "source_position = " << shared->start_position() << "\n";
    192     }
    193     code->Disassemble(debug_name.get(), os);
    194     os << "--- End code ---\n";
    195   }
    196 #endif  // ENABLE_DISASSEMBLER
    197 }
    198 
    199 }  // namespace internal
    200 }  // namespace v8
    201