Home | History | Annotate | Download | only in src
      1 // Copyright 2011 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 #include "v8.h"
     29 
     30 #include "bootstrapper.h"
     31 #include "codegen.h"
     32 #include "compiler.h"
     33 #include "debug.h"
     34 #include "prettyprinter.h"
     35 #include "rewriter.h"
     36 #include "runtime.h"
     37 #include "scopeinfo.h"
     38 #include "stub-cache.h"
     39 
     40 namespace v8 {
     41 namespace internal {
     42 
     43 #define __ ACCESS_MASM(masm_)
     44 
     45 #ifdef DEBUG
     46 
     47 Comment::Comment(MacroAssembler* masm, const char* msg)
     48     : masm_(masm), msg_(msg) {
     49   __ RecordComment(msg);
     50 }
     51 
     52 
     53 Comment::~Comment() {
     54   if (msg_[0] == '[') __ RecordComment("]");
     55 }
     56 
     57 #endif  // DEBUG
     58 
     59 #undef __
     60 
     61 
     62 void CodeGenerator::MakeCodePrologue(CompilationInfo* info) {
     63 #ifdef DEBUG
     64   bool print_source = false;
     65   bool print_ast = false;
     66   bool print_json_ast = false;
     67   const char* ftype;
     68 
     69   if (Isolate::Current()->bootstrapper()->IsActive()) {
     70     print_source = FLAG_print_builtin_source;
     71     print_ast = FLAG_print_builtin_ast;
     72     print_json_ast = FLAG_print_builtin_json_ast;
     73     ftype = "builtin";
     74   } else {
     75     print_source = FLAG_print_source;
     76     print_ast = FLAG_print_ast;
     77     print_json_ast = FLAG_print_json_ast;
     78     Vector<const char> filter = CStrVector(FLAG_hydrogen_filter);
     79     if (print_source && !filter.is_empty()) {
     80       print_source = info->function()->name()->IsEqualTo(filter);
     81     }
     82     if (print_ast && !filter.is_empty()) {
     83       print_ast = info->function()->name()->IsEqualTo(filter);
     84     }
     85     if (print_json_ast && !filter.is_empty()) {
     86       print_json_ast = info->function()->name()->IsEqualTo(filter);
     87     }
     88     ftype = "user-defined";
     89   }
     90 
     91   if (FLAG_trace_codegen || print_source || print_ast) {
     92     PrintF("*** Generate code for %s function: ", ftype);
     93     info->function()->name()->ShortPrint();
     94     PrintF(" ***\n");
     95   }
     96 
     97   if (print_source) {
     98     PrintF("--- Source from AST ---\n%s\n",
     99            PrettyPrinter().PrintProgram(info->function()));
    100   }
    101 
    102   if (print_ast) {
    103     PrintF("--- AST ---\n%s\n",
    104            AstPrinter().PrintProgram(info->function()));
    105   }
    106 
    107   if (print_json_ast) {
    108     JsonAstBuilder builder;
    109     PrintF("%s", builder.BuildProgram(info->function()));
    110   }
    111 #endif  // DEBUG
    112 }
    113 
    114 
    115 Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm,
    116                                              Code::Flags flags,
    117                                              CompilationInfo* info) {
    118   Isolate* isolate = info->isolate();
    119 
    120   // Allocate and install the code.
    121   CodeDesc desc;
    122   masm->GetCode(&desc);
    123   Handle<Code> code =
    124       isolate->factory()->NewCode(desc, flags, masm->CodeObject());
    125 
    126   if (!code.is_null()) {
    127     isolate->counters()->total_compiled_code_size()->Increment(
    128         code->instruction_size());
    129   }
    130   return code;
    131 }
    132 
    133 
    134 void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) {
    135 #ifdef ENABLE_DISASSEMBLER
    136   bool print_code = Isolate::Current()->bootstrapper()->IsActive()
    137       ? FLAG_print_builtin_code
    138       : (FLAG_print_code || (info->IsOptimizing() && FLAG_print_opt_code));
    139   Vector<const char> filter = CStrVector(FLAG_hydrogen_filter);
    140   FunctionLiteral* function = info->function();
    141   bool match = filter.is_empty() || function->debug_name()->IsEqualTo(filter);
    142   if (print_code && match) {
    143     // Print the source code if available.
    144     Handle<Script> script = info->script();
    145     if (!script->IsUndefined() && !script->source()->IsUndefined()) {
    146       PrintF("--- Raw source ---\n");
    147       StringInputBuffer stream(String::cast(script->source()));
    148       stream.Seek(function->start_position());
    149       // fun->end_position() points to the last character in the stream. We
    150       // need to compensate by adding one to calculate the length.
    151       int source_len =
    152           function->end_position() - function->start_position() + 1;
    153       for (int i = 0; i < source_len; i++) {
    154         if (stream.has_more()) PrintF("%c", stream.GetNext());
    155       }
    156       PrintF("\n\n");
    157     }
    158     if (info->IsOptimizing()) {
    159       if (FLAG_print_unopt_code) {
    160         PrintF("--- Unoptimized code ---\n");
    161         info->closure()->shared()->code()->Disassemble(
    162             *function->debug_name()->ToCString());
    163       }
    164       PrintF("--- Optimized code ---\n");
    165     } else {
    166       PrintF("--- Code ---\n");
    167     }
    168     code->Disassemble(*function->debug_name()->ToCString());
    169   }
    170 #endif  // ENABLE_DISASSEMBLER
    171 }
    172 
    173 #ifdef ENABLE_LOGGING_AND_PROFILING
    174 
    175 static Vector<const char> kRegexp = CStrVector("regexp");
    176 
    177 bool CodeGenerator::ShouldGenerateLog(Expression* type) {
    178   ASSERT(type != NULL);
    179   if (!LOGGER->is_logging() && !CpuProfiler::is_profiling()) return false;
    180   Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle());
    181   if (FLAG_log_regexp) {
    182     if (name->IsEqualTo(kRegexp))
    183       return true;
    184   }
    185   return false;
    186 }
    187 
    188 #endif
    189 
    190 
    191 bool CodeGenerator::RecordPositions(MacroAssembler* masm,
    192                                     int pos,
    193                                     bool right_here) {
    194   if (pos != RelocInfo::kNoPosition) {
    195     masm->positions_recorder()->RecordStatementPosition(pos);
    196     masm->positions_recorder()->RecordPosition(pos);
    197     if (right_here) {
    198       return masm->positions_recorder()->WriteRecordedPositions();
    199     }
    200   }
    201   return false;
    202 }
    203 
    204 
    205 const char* GenericUnaryOpStub::GetName() {
    206   switch (op_) {
    207     case Token::SUB:
    208       if (negative_zero_ == kStrictNegativeZero) {
    209         return overwrite_ == UNARY_OVERWRITE
    210             ? "GenericUnaryOpStub_SUB_Overwrite_Strict0"
    211             : "GenericUnaryOpStub_SUB_Alloc_Strict0";
    212       } else {
    213         return overwrite_ == UNARY_OVERWRITE
    214             ? "GenericUnaryOpStub_SUB_Overwrite_Ignore0"
    215             : "GenericUnaryOpStub_SUB_Alloc_Ignore0";
    216       }
    217     case Token::BIT_NOT:
    218       return overwrite_ == UNARY_OVERWRITE
    219           ? "GenericUnaryOpStub_BIT_NOT_Overwrite"
    220           : "GenericUnaryOpStub_BIT_NOT_Alloc";
    221     default:
    222       UNREACHABLE();
    223       return "<unknown>";
    224   }
    225 }
    226 
    227 
    228 void ArgumentsAccessStub::Generate(MacroAssembler* masm) {
    229   switch (type_) {
    230     case READ_ELEMENT:
    231       GenerateReadElement(masm);
    232       break;
    233     case NEW_NON_STRICT:
    234     case NEW_STRICT:
    235       GenerateNewObject(masm);
    236       break;
    237   }
    238 }
    239 
    240 
    241 int CEntryStub::MinorKey() {
    242   ASSERT(result_size_ == 1 || result_size_ == 2);
    243   int result = save_doubles_ ? 1 : 0;
    244 #ifdef _WIN64
    245   return result | ((result_size_ == 1) ? 0 : 2);
    246 #else
    247   return result;
    248 #endif
    249 }
    250 
    251 
    252 } }  // namespace v8::internal
    253