code-generator.h 5.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_COMPILER_CODE_GENERATOR_H_
#define V8_COMPILER_CODE_GENERATOR_H_

#include <deque>

#include "src/compiler/gap-resolver.h"
#include "src/compiler/instruction.h"
#include "src/deoptimizer.h"
#include "src/macro-assembler.h"
#include "src/safepoint-table.h"

namespace v8 {
namespace internal {
namespace compiler {

// Generates native code for a sequence of instructions.
21
class CodeGenerator FINAL : public GapResolver::Assembler {
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
 public:
  explicit CodeGenerator(InstructionSequence* code);

  // Generate native code.
  Handle<Code> GenerateCode();

  InstructionSequence* code() const { return code_; }
  Frame* frame() const { return code()->frame(); }
  Graph* graph() const { return code()->graph(); }
  Isolate* isolate() const { return zone()->isolate(); }
  Linkage* linkage() const { return code()->linkage(); }
  Schedule* schedule() const { return code()->schedule(); }

 private:
  MacroAssembler* masm() { return &masm_; }
  GapResolver* resolver() { return &resolver_; }
  SafepointTableBuilder* safepoints() { return &safepoints_; }
  Zone* zone() const { return code()->zone(); }

  // Checks if {block} will appear directly after {current_block_} when
  // assembling code, in which case, a fall-through can be used.
  bool IsNextInAssemblyOrder(const BasicBlock* block) const {
    return block->rpo_number_ == (current_block_->rpo_number_ + 1) &&
           block->deferred_ == current_block_->deferred_;
  }

  // Record a safepoint with the given pointer map.
49 50
  void RecordSafepoint(PointerMap* pointers, Safepoint::Kind kind,
                       int arguments, Safepoint::DeoptMode deopt_mode);
51 52 53 54 55 56 57 58 59 60 61 62 63 64

  // Assemble code for the specified instruction.
  void AssembleInstruction(Instruction* instr);
  void AssembleSourcePosition(SourcePositionInstruction* instr);
  void AssembleGap(GapInstruction* gap);

  // ===========================================================================
  // ============= Architecture-specific code generation methods. ==============
  // ===========================================================================

  void AssembleArchInstruction(Instruction* instr);
  void AssembleArchBranch(Instruction* instr, FlagsCondition condition);
  void AssembleArchBoolean(Instruction* instr, FlagsCondition condition);

65 66
  void AssembleDeoptimizerCall(int deoptimization_id);

67 68 69 70 71 72 73 74 75 76 77 78 79
  // Generates an architecture-specific, descriptor-specific prologue
  // to set up a stack frame.
  void AssemblePrologue();
  // Generates an architecture-specific, descriptor-specific return sequence
  // to tear down a stack frame.
  void AssembleReturn();

  // ===========================================================================
  // ============== Architecture-specific gap resolver methods. ================
  // ===========================================================================

  // Interface used by the gap resolver to emit moves and swaps.
  virtual void AssembleMove(InstructionOperand* source,
80
                            InstructionOperand* destination) OVERRIDE;
81
  virtual void AssembleSwap(InstructionOperand* source,
82
                            InstructionOperand* destination) OVERRIDE;
83 84 85

  // ===========================================================================
  // Deoptimization table construction
86
  void AddSafepointAndDeopt(Instruction* instr);
87 88
  void PopulateDeoptimizationData(Handle<Code> code);
  int DefineDeoptimizationLiteral(Handle<Object> literal);
89
  FrameStateDescriptor* GetFrameStateDescriptor(Instruction* instr,
90
                                                size_t frame_state_offset);
91
  int BuildTranslation(Instruction* instr, int pc_offset,
92
                       size_t frame_state_offset,
93
                       OutputFrameStateCombine state_combine);
94 95
  void BuildTranslationForFrameStateDescriptor(
      FrameStateDescriptor* descriptor, Instruction* instr,
96
      Translation* translation, size_t frame_state_offset,
97
      OutputFrameStateCombine state_combine);
98 99
  void AddTranslationForOperand(Translation* translation, Instruction* instr,
                                InstructionOperand* op);
100
  void AddNopForSmiCodeInlining();
101 102
  void EnsureSpaceForLazyDeopt();
  void MarkLazyDeoptSite();
103

104
  // ===========================================================================
105
  struct DeoptimizationState : ZoneObject {
106 107 108
   public:
    BailoutId bailout_id() const { return bailout_id_; }
    int translation_id() const { return translation_id_; }
109
    int pc_offset() const { return pc_offset_; }
110

111 112 113 114
    DeoptimizationState(BailoutId bailout_id, int translation_id, int pc_offset)
        : bailout_id_(bailout_id),
          translation_id_(translation_id),
          pc_offset_(pc_offset) {}
115 116 117 118

   private:
    BailoutId bailout_id_;
    int translation_id_;
119
    int pc_offset_;
120 121 122 123 124 125 126 127
  };

  InstructionSequence* code_;
  BasicBlock* current_block_;
  SourcePosition current_source_position_;
  MacroAssembler masm_;
  GapResolver resolver_;
  SafepointTableBuilder safepoints_;
128 129
  ZoneDeque<DeoptimizationState*> deoptimization_states_;
  ZoneDeque<Handle<Object> > deoptimization_literals_;
130
  TranslationBuffer translations_;
131
  int last_lazy_deopt_pc_;
132 133 134 135 136 137 138
};

}  // namespace compiler
}  // namespace internal
}  // namespace v8

#endif  // V8_COMPILER_CODE_GENERATOR_H