lithium-codegen.cc 9.86 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/lithium-codegen.h"
6

7 8 9 10
#include <sstream>

#include "src/v8.h"

11
#if V8_TARGET_ARCH_IA32
12 13
#include "src/ia32/lithium-ia32.h"  // NOLINT
#include "src/ia32/lithium-codegen-ia32.h"  // NOLINT
14
#elif V8_TARGET_ARCH_X64
15 16
#include "src/x64/lithium-x64.h"  // NOLINT
#include "src/x64/lithium-codegen-x64.h"  // NOLINT
17
#elif V8_TARGET_ARCH_ARM
18 19
#include "src/arm/lithium-arm.h"  // NOLINT
#include "src/arm/lithium-codegen-arm.h"  // NOLINT
20
#elif V8_TARGET_ARCH_ARM64
21 22
#include "src/arm64/lithium-arm64.h"  // NOLINT
#include "src/arm64/lithium-codegen-arm64.h"  // NOLINT
23
#elif V8_TARGET_ARCH_MIPS
24 25
#include "src/mips/lithium-mips.h"  // NOLINT
#include "src/mips/lithium-codegen-mips.h"  // NOLINT
26 27 28
#elif V8_TARGET_ARCH_MIPS64
#include "src/mips64/lithium-mips64.h"  // NOLINT
#include "src/mips64/lithium-codegen-mips64.h"  // NOLINT
danno@chromium.org's avatar
danno@chromium.org committed
29
#elif V8_TARGET_ARCH_X87
30 31
#include "src/x87/lithium-x87.h"  // NOLINT
#include "src/x87/lithium-codegen-x87.h"  // NOLINT
32 33 34
#elif V8_TARGET_ARCH_PPC
#include "src/ppc/lithium-ppc.h"          // NOLINT
#include "src/ppc/lithium-codegen-ppc.h"  // NOLINT
35 36 37 38 39 40 41 42 43 44 45 46 47
#else
#error Unsupported target architecture.
#endif

namespace v8 {
namespace internal {


HGraph* LCodeGenBase::graph() const {
  return chunk()->graph();
}


48
LCodeGenBase::LCodeGenBase(LChunk* chunk, MacroAssembler* assembler,
49 50 51 52 53 54 55 56 57
                           CompilationInfo* info)
    : chunk_(static_cast<LPlatformChunk*>(chunk)),
      masm_(assembler),
      info_(info),
      zone_(info->zone()),
      status_(UNUSED),
      current_block_(-1),
      current_instruction_(-1),
      instructions_(chunk->instructions()),
58 59
      deoptimization_literals_(8, info->zone()),
      last_lazy_deopt_pc_(0) {}
60 61 62


bool LCodeGenBase::GenerateBody() {
63
  DCHECK(is_generating());
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  bool emit_instructions = true;
  LCodeGen* codegen = static_cast<LCodeGen*>(this);
  for (current_instruction_ = 0;
       !is_aborted() && current_instruction_ < instructions_->length();
       current_instruction_++) {
    LInstruction* instr = instructions_->at(current_instruction_);

    // Don't emit code for basic blocks with a replacement.
    if (instr->IsLabel()) {
      emit_instructions = !LLabel::cast(instr)->HasReplacement() &&
          (!FLAG_unreachable_code_elimination ||
           instr->hydrogen_value()->block()->IsReachable());
      if (FLAG_code_comments && !emit_instructions) {
        Comment(
            ";;; <@%d,#%d> -------------------- B%d (unreachable/replaced) "
            "--------------------",
            current_instruction_,
            instr->hydrogen_value()->id(),
            instr->hydrogen_value()->block()->block_id());
      }
    }
    if (!emit_instructions) continue;

    if (FLAG_code_comments && instr->HasInterestingComment(codegen)) {
      Comment(";;; <@%d,#%d> %s",
              current_instruction_,
              instr->hydrogen_value()->id(),
              instr->Mnemonic());
    }

    GenerateBodyInstructionPre(instr);

96
    HValue* value = instr->hydrogen_value();
97 98 99
    if (!value->position().IsUnknown()) {
      RecordAndWritePosition(
        chunk()->graph()->SourcePositionToScriptPosition(value->position()));
100
    }
101 102 103 104 105 106 107 108 109 110 111

    instr->CompileToNative(codegen);

    GenerateBodyInstructionPost(instr);
  }
  EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
  last_lazy_deopt_pc_ = masm()->pc_offset();
  return !is_aborted();
}


112 113
void LCodeGenBase::CheckEnvironmentUsage() {
#ifdef DEBUG
114
  bool dead_block = false;
115 116
  for (int i = 0; i < instructions_->length(); i++) {
    LInstruction* instr = instructions_->at(i);
117 118 119 120 121 122
    HValue* hval = instr->hydrogen_value();
    if (instr->IsLabel()) dead_block = LLabel::cast(instr)->HasReplacement();
    if (dead_block || !hval->block()->IsReachable()) continue;

    HInstruction* hinstr = HInstruction::cast(hval);
    if (!hinstr->CanDeoptimize() && instr->HasEnvironment()) {
123
      V8_Fatal(__FILE__, __LINE__, "CanDeoptimize is wrong for %s (%s)",
124 125 126 127
               hinstr->Mnemonic(), instr->Mnemonic());
    }

    if (instr->HasEnvironment() && !instr->environment()->has_been_used()) {
128
      V8_Fatal(__FILE__, __LINE__, "unused environment for %s (%s)",
129
               hinstr->Mnemonic(), instr->Mnemonic());
130 131 132 133 134 135
    }
  }
#endif
}


136 137 138
void LCodeGenBase::Comment(const char* format, ...) {
  if (!FLAG_code_comments) return;
  char buffer[4 * KB];
139
  StringBuilder builder(buffer, arraysize(buffer));
140 141 142 143 144 145 146 147
  va_list arguments;
  va_start(arguments, format);
  builder.AddFormattedList(format, arguments);
  va_end(arguments);

  // Copy the string before recording it in the assembler to avoid
  // issues when the stack allocated buffer goes out of scope.
  size_t length = builder.position();
148
  Vector<char> copy = Vector<char>::New(static_cast<int>(length) + 1);
149
  MemCopy(copy.start(), builder.Finalize(), copy.length());
150 151 152 153
  masm()->RecordComment(copy.start());
}


154
void LCodeGenBase::DeoptComment(const Deoptimizer::DeoptInfo& deopt_info) {
155
  masm()->RecordDeoptReason(deopt_info.deopt_reason, deopt_info.position);
156 157 158
}


159 160
int LCodeGenBase::GetNextEmittedBlock() const {
  for (int i = current_block_ + 1; i < graph()->blocks()->length(); ++i) {
161
    if (!graph()->blocks()->at(i)->IsReachable()) continue;
162 163 164 165 166 167
    if (!chunk_->GetLabel(i)->HasReplacement()) return i;
  }
  return -1;
}


168
void LCodeGenBase::Abort(BailoutReason reason) {
169 170 171 172 173 174 175
  info()->AbortOptimization(reason);
  status_ = ABORTED;
}


void LCodeGenBase::Retry(BailoutReason reason) {
  info()->RetryOptimization(reason);
176 177 178 179
  status_ = ABORTED;
}


180
void LCodeGenBase::AddDeprecationDependency(Handle<Map> map) {
181
  if (map->is_deprecated()) return Retry(kMapBecameDeprecated);
182 183 184 185
  chunk_->AddDeprecationDependency(map);
}


186
void LCodeGenBase::AddStabilityDependency(Handle<Map> map) {
187
  if (!map->is_stable()) return Retry(kMapBecameUnstable);
188 189 190
  chunk_->AddStabilityDependency(map);
}

191

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
int LCodeGenBase::DefineDeoptimizationLiteral(Handle<Object> literal) {
  int result = deoptimization_literals_.length();
  for (int i = 0; i < deoptimization_literals_.length(); ++i) {
    if (deoptimization_literals_[i].is_identical_to(literal)) return i;
  }
  deoptimization_literals_.Add(literal, zone());
  return result;
}


void LCodeGenBase::WriteTranslationFrame(LEnvironment* environment,
                                         Translation* translation) {
  int translation_size = environment->translation_size();
  // The output frame height does not include the parameters.
  int height = translation_size - environment->parameter_count();

  switch (environment->frame_type()) {
    case JS_FUNCTION: {
      int shared_id = DefineDeoptimizationLiteral(
          environment->entry() ? environment->entry()->shared()
                               : info()->shared_info());
      translation->BeginJSFrame(environment->ast_id(), shared_id, height);
      if (info()->closure().is_identical_to(environment->closure())) {
        translation->StoreJSFrameFunction();
      } else {
        int closure_id = DefineDeoptimizationLiteral(environment->closure());
        translation->StoreLiteral(closure_id);
      }
      break;
    }
    case JS_CONSTRUCT: {
      int shared_id = DefineDeoptimizationLiteral(
          environment->entry() ? environment->entry()->shared()
                               : info()->shared_info());
      translation->BeginConstructStubFrame(shared_id, translation_size);
      if (info()->closure().is_identical_to(environment->closure())) {
        translation->StoreJSFrameFunction();
      } else {
        int closure_id = DefineDeoptimizationLiteral(environment->closure());
        translation->StoreLiteral(closure_id);
      }
      break;
    }
    case JS_GETTER: {
      DCHECK(translation_size == 1);
      DCHECK(height == 0);
      int shared_id = DefineDeoptimizationLiteral(
          environment->entry() ? environment->entry()->shared()
                               : info()->shared_info());
      translation->BeginGetterStubFrame(shared_id);
      if (info()->closure().is_identical_to(environment->closure())) {
        translation->StoreJSFrameFunction();
      } else {
        int closure_id = DefineDeoptimizationLiteral(environment->closure());
        translation->StoreLiteral(closure_id);
      }
      break;
    }
    case JS_SETTER: {
      DCHECK(translation_size == 2);
      DCHECK(height == 0);
      int shared_id = DefineDeoptimizationLiteral(
          environment->entry() ? environment->entry()->shared()
                               : info()->shared_info());
      translation->BeginSetterStubFrame(shared_id);
      if (info()->closure().is_identical_to(environment->closure())) {
        translation->StoreJSFrameFunction();
      } else {
        int closure_id = DefineDeoptimizationLiteral(environment->closure());
        translation->StoreLiteral(closure_id);
      }
      break;
    }
    case ARGUMENTS_ADAPTOR: {
      int shared_id = DefineDeoptimizationLiteral(
          environment->entry() ? environment->entry()->shared()
                               : info()->shared_info());
      translation->BeginArgumentsAdaptorFrame(shared_id, translation_size);
      if (info()->closure().is_identical_to(environment->closure())) {
        translation->StoreJSFrameFunction();
      } else {
        int closure_id = DefineDeoptimizationLiteral(environment->closure());
        translation->StoreLiteral(closure_id);
      }
      break;
    }
    case STUB:
      translation->BeginCompiledStubFrame(translation_size);
      break;
  }
}


285 286 287 288 289 290 291 292
Deoptimizer::DeoptInfo LCodeGenBase::MakeDeoptInfo(
    LInstruction* instr, Deoptimizer::DeoptReason deopt_reason) {
  Deoptimizer::DeoptInfo deopt_info(instr->hydrogen_value()->position(),
                                    instr->Mnemonic(), deopt_reason);
  HEnterInlined* enter_inlined = instr->environment()->entry();
  deopt_info.inlining_id = enter_inlined ? enter_inlined->inlining_id() : 0;
  return deopt_info;
}
293 294
}  // namespace internal
}  // namespace v8