lithium-codegen-mips.h 16.1 KB
Newer Older
1
// Copyright 2012 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 6
#ifndef V8_MIPS_LITHIUM_CODEGEN_MIPS_H_
#define V8_MIPS_LITHIUM_CODEGEN_MIPS_H_
7

8
#include "src/deoptimizer.h"
9
#include "src/lithium-codegen.h"
10 11 12 13 14
#include "src/mips/lithium-gap-resolver-mips.h"
#include "src/mips/lithium-mips.h"
#include "src/safepoint-table.h"
#include "src/scopes.h"
#include "src/utils.h"
15

16 17 18
namespace v8 {
namespace internal {

19 20
// Forward declarations.
class LDeferredCode;
21
class SafepointGenerator;
22

23
class LCodeGen: public LCodeGenBase {
24
 public:
25
  LCodeGen(LChunk* chunk, MacroAssembler* assembler, CompilationInfo* info)
26
      : LCodeGenBase(chunk, assembler, info),
27
        deoptimizations_(4, info->zone()),
28
        jump_table_(4, info->zone()),
29
        deoptimization_literals_(8, info->zone()),
30 31
        inlined_function_count_(0),
        scope_(info->scope()),
32 33
        translations_(info->zone()),
        deferred_(8, info->zone()),
34
        osr_pc_offset_(-1),
35
        frame_is_built_(false),
36
        safepoints_(info->zone()),
37
        resolver_(this),
38
        expected_safepoint_kind_(Safepoint::kSimple) {
39 40 41 42
    PopulateDeoptimizationLiteralsWithInlinedFunctions();
  }


43 44 45 46 47 48 49 50
  int LookupDestination(int block_id) const {
    return chunk()->LookupDestination(block_id);
  }

  bool IsNextEmittedBlock(int block_id) const {
    return LookupDestination(block_id) == GetNextEmittedBlock();
  }

51 52 53
  bool NeedsEagerFrame() const {
    return GetStackSlotCount() > 0 ||
        info()->is_non_deferred_calling() ||
54 55
        !info()->IsStub() ||
        info()->requires_frame();
56 57 58 59 60
  }
  bool NeedsDeferredFrame() const {
    return !NeedsEagerFrame() && info()->is_deferred_calling();
  }

61 62 63 64
  RAStatus GetRAState() const {
    return frame_is_built_ ? kRAHasBeenSaved : kRAHasNotBeenSaved;
  }

65 66 67 68 69 70 71 72 73 74 75 76 77 78
  // Support for converting LOperands to assembler types.
  // LOperand must be a register.
  Register ToRegister(LOperand* op) const;

  // LOperand is loaded into scratch, unless already a register.
  Register EmitLoadRegister(LOperand* op, Register scratch);

  // LOperand must be a double register.
  DoubleRegister ToDoubleRegister(LOperand* op) const;

  // LOperand is loaded into dbl_scratch, unless already a double register.
  DoubleRegister EmitLoadDoubleRegister(LOperand* op,
                                        FloatRegister flt_scratch,
                                        DoubleRegister dbl_scratch);
79
  int32_t ToRepresentation(LConstantOperand* op, const Representation& r) const;
80
  int32_t ToInteger32(LConstantOperand* op) const;
81
  Smi* ToSmi(LConstantOperand* op) const;
82 83 84 85 86
  double ToDouble(LConstantOperand* op) const;
  Operand ToOperand(LOperand* op);
  MemOperand ToMemOperand(LOperand* op) const;
  // Returns a MemOperand pointing to the high word of a DoubleStackSlot.
  MemOperand ToHighMemOperand(LOperand* op) const;
87 88

  bool IsInteger32(LConstantOperand* op) const;
89
  bool IsSmi(LConstantOperand* op) const;
90
  Handle<Object> ToHandle(LConstantOperand* op) const;
91

92 93 94
  // Try to generate code for the entire chunk, but it may fail if the
  // chunk contains constructs we cannot handle. Returns true if the
  // code generation attempt succeeded.
95
  bool GenerateCode();
96

97 98
  // Finish the code by setting stack height, safepoint, and bailout
  // information on it.
99 100 101
  void FinishCode(Handle<Code> code);

  void DoDeferredNumberTagD(LNumberTagD* instr);
102 103

  enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
104 105 106 107 108
  void DoDeferredNumberTagIU(LInstruction* instr,
                             LOperand* value,
                             LOperand* temp1,
                             LOperand* temp2,
                             IntegerSignedness signedness);
109

110
  void DoDeferredTaggedToI(LTaggedToI* instr);
111
  void DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr);
112 113 114
  void DoDeferredStackCheck(LStackCheck* instr);
  void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
  void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
115
  void DoDeferredAllocate(LAllocate* instr);
116 117
  void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
                                       Label* map_check);
118

119
  void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
120 121 122 123
  void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
                                   Register result,
                                   Register object,
                                   Register index);
124

125 126 127 128
  // Parallel move support.
  void DoParallelMove(LParallelMove* move);
  void DoGap(LGap* instr);

129 130 131 132 133 134
  MemOperand PrepareKeyedOperand(Register key,
                                 Register base,
                                 bool key_is_constant,
                                 int constant_key,
                                 int element_size,
                                 int shift_size,
135
                                 int base_offset);
136

137
  // Emit frame translation commands for an environment.
138
  void WriteTranslation(LEnvironment* environment, Translation* translation);
139 140 141 142 143 144 145

  // Declare methods that deal with the individual node types.
#define DECLARE_DO(type) void Do##type(L##type* node);
  LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
#undef DECLARE_DO

 private:
146
  StrictMode strict_mode() const { return info()->strict_mode(); }
147 148 149

  Scope* scope() const { return scope_; }

150 151 152
  Register scratch0() { return kLithiumScratchReg; }
  Register scratch1() { return kLithiumScratchReg2; }
  DoubleRegister double_scratch0() { return kLithiumScratchDouble; }
153 154 155 156 157 158 159 160 161 162 163 164

  LInstruction* GetNextInstruction();

  void EmitClassOfTest(Label* if_true,
                       Label* if_false,
                       Handle<String> class_name,
                       Register input,
                       Register temporary,
                       Register temporary2);

  int GetStackSlotCount() const { return chunk()->spill_slot_count(); }

165
  void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
166

167 168 169
  void SaveCallerDoubles();
  void RestoreCallerDoubles();

170 171
  // Code generation passes.  Returns true if code generation should
  // continue.
172
  void GenerateBodyInstructionPre(LInstruction* instr) OVERRIDE;
173 174
  bool GeneratePrologue();
  bool GenerateDeferredCode();
175
  bool GenerateJumpTable();
176 177
  bool GenerateSafepointTable();

178 179 180
  // Generates the custom OSR entrypoint and sets the osr_pc_offset.
  void GenerateOsrPrologue();

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  enum SafepointMode {
    RECORD_SIMPLE_SAFEPOINT,
    RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
  };

  void CallCode(Handle<Code> code,
                RelocInfo::Mode mode,
                LInstruction* instr);

  void CallCodeGeneric(Handle<Code> code,
                       RelocInfo::Mode mode,
                       LInstruction* instr,
                       SafepointMode safepoint_mode);

  void CallRuntime(const Runtime::Function* function,
                   int num_arguments,
197 198
                   LInstruction* instr,
                   SaveFPRegsMode save_doubles = kDontSaveFPRegs);
199 200 201 202 203 204 205 206

  void CallRuntime(Runtime::FunctionId id,
                   int num_arguments,
                   LInstruction* instr) {
    const Runtime::Function* function = Runtime::FunctionForId(id);
    CallRuntime(function, num_arguments, instr);
  }

207
  void LoadContextFromDeferred(LOperand* context);
208 209
  void CallRuntimeFromDeferred(Runtime::FunctionId id,
                               int argc,
210 211
                               LInstruction* instr,
                               LOperand* context);
212

213 214 215 216 217
  enum A1State {
    A1_UNINITIALIZED,
    A1_CONTAINS_TARGET
  };

218 219 220
  // Generate a direct call to a known function.  Expects the function
  // to be in a1.
  void CallKnownFunction(Handle<JSFunction> function,
221
                         int formal_parameter_count,
222 223
                         int arity,
                         LInstruction* instr,
224
                         A1State a1_state);
225

226 227
  void RecordSafepointWithLazyDeopt(LInstruction* instr,
                                    SafepointMode safepoint_mode);
228

229 230
  void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
                                            Safepoint::DeoptMode mode);
231
  void DeoptimizeIf(Condition condition, LInstruction* instr,
232
                    Deoptimizer::BailoutType bailout_type, const char* detail,
233
                    Register src1 = zero_reg,
234
                    const Operand& src2 = Operand(zero_reg));
235
  void DeoptimizeIf(Condition condition, LInstruction* instr,
236 237
                    const char* detail = NULL, Register src1 = zero_reg,
                    const Operand& src2 = Operand(zero_reg));
238

239 240
  void AddToTranslation(LEnvironment* environment,
                        Translation* translation,
241
                        LOperand* op,
242
                        bool is_tagged,
243 244 245
                        bool is_uint32,
                        int* object_index_pointer,
                        int* dematerialized_index_pointer);
246 247 248 249 250 251 252 253
  void PopulateDeoptimizationData(Handle<Code> code);
  int DefineDeoptimizationLiteral(Handle<Object> literal);

  void PopulateDeoptimizationLiteralsWithInlinedFunctions();

  Register ToRegister(int index) const;
  DoubleRegister ToDoubleRegister(int index) const;

254 255 256 257
  MemOperand BuildSeqStringOperand(Register string,
                                   LOperand* index,
                                   String::Encoding encoding);

258
  void EmitIntegerMathAbs(LMathAbs* instr);
259 260 261 262 263

  // Support for recording safepoint and position information.
  void RecordSafepoint(LPointerMap* pointers,
                       Safepoint::Kind kind,
                       int arguments,
264 265 266
                       Safepoint::DeoptMode mode);
  void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
  void RecordSafepoint(Safepoint::DeoptMode mode);
267 268
  void RecordSafepointWithRegisters(LPointerMap* pointers,
                                    int arguments,
269
                                    Safepoint::DeoptMode mode);
270

271
  void RecordAndWritePosition(int position) OVERRIDE;
272 273 274

  static Condition TokenToCondition(Token::Value op, bool is_unsigned);
  void EmitGoto(int block);
275 276

  // EmitBranch expects to be the last instruction of a block.
277 278
  template<class InstrType>
  void EmitBranch(InstrType instr,
279
                  Condition condition,
280 281
                  Register src1,
                  const Operand& src2);
282 283
  template<class InstrType>
  void EmitBranchF(InstrType instr,
284
                   Condition condition,
285 286
                   FPURegister src1,
                   FPURegister src2);
287
  template<class InstrType>
288 289 290 291 292
  void EmitFalseBranch(InstrType instr,
                       Condition condition,
                       Register src1,
                       const Operand& src2);
  template<class InstrType>
293 294 295 296
  void EmitFalseBranchF(InstrType instr,
                        Condition condition,
                        FPURegister src1,
                        FPURegister src2);
297
  void EmitCmpI(LOperand* left, LOperand* right);
298 299
  void EmitNumberUntagD(LNumberUntagD* instr, Register input,
                        DoubleRegister result, NumberUntagDMode mode);
300 301 302 303 304 305 306 307 308 309

  // Emits optimized code for typeof x == "y".  Modifies input register.
  // Returns the condition on which a final split to
  // true and false label should be made, to optimize fallthrough.
  // Returns two registers in cmp1 and cmp2 that can be used in the
  // Branch instruction after EmitTypeofIs.
  Condition EmitTypeofIs(Label* true_label,
                         Label* false_label,
                         Register input,
                         Handle<String> type_name,
310 311
                         Register* cmp1,
                         Operand* cmp2);
312 313 314 315 316 317

  // Emits optimized code for %_IsObject(x).  Preserves input register.
  // Returns the condition on which a final split to
  // true and false label should be made, to optimize fallthrough.
  Condition EmitIsObject(Register input,
                         Register temp1,
318
                         Register temp2,
319 320 321
                         Label* is_not_object,
                         Label* is_object);

322 323 324 325 326
  // Emits optimized code for %_IsString(x).  Preserves input register.
  // Returns the condition on which a final split to
  // true and false label should be made, to optimize fallthrough.
  Condition EmitIsString(Register input,
                         Register temp1,
327 328
                         Label* is_not_string,
                         SmiCheck check_needed);
329

330 331 332 333
  // Emits optimized code for %_IsConstructCall().
  // Caller should branch on equal condition.
  void EmitIsConstructCall(Register temp1, Register temp2);

334 335 336 337 338
  // Emits optimized code to deep-copy the contents of statically known
  // object graphs (e.g. object literal boilerplate).
  void EmitDeepCopy(Handle<JSObject> object,
                    Register result,
                    Register source,
339 340
                    int* offset,
                    AllocationSiteMode mode);
341 342 343 344 345 346 347 348 349 350 351
  // Emit optimized code for integer division.
  // Inputs are signed.
  // All registers are clobbered.
  // If 'remainder' is no_reg, it is not computed.
  void EmitSignedIntegerDivisionByConstant(Register result,
                                           Register dividend,
                                           int32_t divisor,
                                           Register remainder,
                                           Register scratch,
                                           LEnvironment* environment);

352

353
  void EnsureSpaceForLazyDeopt(int space_needed) OVERRIDE;
354 355 356 357 358 359
  void DoLoadKeyedExternalArray(LLoadKeyed* instr);
  void DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr);
  void DoLoadKeyedFixedArray(LLoadKeyed* instr);
  void DoStoreKeyedExternalArray(LStoreKeyed* instr);
  void DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr);
  void DoStoreKeyedFixedArray(LStoreKeyed* instr);
360

361 362 363
  template <class T>
  void EmitVectorLoadICRegisters(T* instr);

364
  ZoneList<LEnvironment*> deoptimizations_;
365
  ZoneList<Deoptimizer::JumpTableEntry> jump_table_;
366 367 368 369 370 371
  ZoneList<Handle<Object> > deoptimization_literals_;
  int inlined_function_count_;
  Scope* const scope_;
  TranslationBuffer translations_;
  ZoneList<LDeferredCode*> deferred_;
  int osr_pc_offset_;
372
  bool frame_is_built_;
373 374 375 376 377 378 379 380 381 382

  // Builder that keeps track of safepoints in the code. The table
  // itself is emitted at the end of the generated code.
  SafepointTableBuilder safepoints_;

  // Compiler from a set of parallel moves to a sequential list of moves.
  LGapResolver resolver_;

  Safepoint::Kind expected_safepoint_kind_;

383
  class PushSafepointRegistersScope FINAL BASE_EMBEDDED {
384
   public:
385
    explicit PushSafepointRegistersScope(LCodeGen* codegen)
386
        : codegen_(codegen) {
387 388
      DCHECK(codegen_->info()->is_calling());
      DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
389 390 391 392 393
      codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;

      StoreRegistersStateStub stub(codegen_->isolate());
      codegen_->masm_->push(ra);
      codegen_->masm_->CallStub(&stub);
394 395 396
    }

    ~PushSafepointRegistersScope() {
397
      DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
398 399 400
      RestoreRegistersStateStub stub(codegen_->isolate());
      codegen_->masm_->push(ra);
      codegen_->masm_->CallStub(&stub);
401 402 403 404 405 406 407 408 409 410 411 412 413 414
      codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
    }

   private:
    LCodeGen* codegen_;
  };

  friend class LDeferredCode;
  friend class LEnvironment;
  friend class SafepointGenerator;
  DISALLOW_COPY_AND_ASSIGN(LCodeGen);
};


415
class LDeferredCode : public ZoneObject {
416 417 418 419 420 421 422 423
 public:
  explicit LDeferredCode(LCodeGen* codegen)
      : codegen_(codegen),
        external_exit_(NULL),
        instruction_index_(codegen->current_instruction_) {
    codegen->AddDeferredCode(this);
  }

424
  virtual ~LDeferredCode() {}
425 426 427
  virtual void Generate() = 0;
  virtual LInstruction* instr() = 0;

428
  void SetExit(Label* exit) { external_exit_ = exit; }
429 430 431 432 433 434 435 436 437 438 439 440 441 442
  Label* entry() { return &entry_; }
  Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
  int instruction_index() const { return instruction_index_; }

 protected:
  LCodeGen* codegen() const { return codegen_; }
  MacroAssembler* masm() const { return codegen_->masm(); }

 private:
  LCodeGen* codegen_;
  Label entry_;
  Label exit_;
  Label* external_exit_;
  int instruction_index_;
443
};
444

445 446
} }  // namespace v8::internal

447
#endif  // V8_MIPS_LITHIUM_CODEGEN_MIPS_H_