lithium-codegen-arm.h 14.6 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 7

#ifndef V8_ARM_LITHIUM_CODEGEN_ARM_H_
#define V8_ARM_LITHIUM_CODEGEN_ARM_H_

8 9 10 11 12 13 14 15
#include "src/arm/lithium-arm.h"

#include "src/arm/lithium-gap-resolver-arm.h"
#include "src/deoptimizer.h"
#include "src/lithium-codegen.h"
#include "src/safepoint-table.h"
#include "src/scopes.h"
#include "src/utils.h"
16 17 18 19 20 21 22 23

namespace v8 {
namespace internal {

// Forward declarations.
class LDeferredCode;
class SafepointGenerator;

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

43

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

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

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

62 63 64 65
  LinkRegisterStatus GetLinkRegisterState() const {
    return frame_is_built_ ? kLRHasBeenSaved : kLRHasNotBeenSaved;
  }

66 67 68 69 70 71 72 73
  // 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.
74
  DwVfpRegister ToDoubleRegister(LOperand* op) const;
75 76

  // LOperand is loaded into dbl_scratch, unless already a double register.
77 78 79
  DwVfpRegister EmitLoadDoubleRegister(LOperand* op,
                                       SwVfpRegister flt_scratch,
                                       DwVfpRegister dbl_scratch);
80
  int32_t ToRepresentation(LConstantOperand* op, const Representation& r) const;
81
  int32_t ToInteger32(LConstantOperand* op) const;
82
  Smi* ToSmi(LConstantOperand* op) const;
83
  double ToDouble(LConstantOperand* op) const;
84 85 86 87 88
  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;

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

93 94 95 96 97 98 99 100 101 102 103
  // 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.
  bool GenerateCode();

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

  // Deferred code support.
  void DoDeferredNumberTagD(LNumberTagD* instr);
104 105

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

112
  void DoDeferredTaggedToI(LTaggedToI* instr);
113
  void DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr);
114
  void DoDeferredStackCheck(LStackCheck* instr);
115
  void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
116
  void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
117
  void DoDeferredAllocate(LAllocate* instr);
118
  void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
119
                                       Label* map_check, Label* bool_load);
120
  void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
121 122 123 124
  void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
                                   Register result,
                                   Register object,
                                   Register index);
125

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

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

138
  // Emit frame translation commands for an environment.
139
  void WriteTranslation(LEnvironment* environment, Translation* translation);
140

141 142 143 144 145 146
  // 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:
147
  StrictMode strict_mode() const { return info()->strict_mode(); }
148

149 150
  Scope* scope() const { return scope_; }

151
  Register scratch0() { return r9; }
152
  LowDwVfpRegister double_scratch0() { return kScratchDoubleReg; }
153

154 155 156 157 158 159 160 161 162
  LInstruction* GetNextInstruction();

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

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

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) V8_OVERRIDE;
173 174
  bool GeneratePrologue();
  bool GenerateDeferredCode();
175
  bool GenerateDeoptJumpTable();
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
  enum SafepointMode {
    RECORD_SIMPLE_SAFEPOINT,
    RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
  };

186 187
  int CallCodeSize(Handle<Code> code, RelocInfo::Mode mode);

188 189 190 191 192 193 194 195 196 197 198 199
  void CallCode(
      Handle<Code> code,
      RelocInfo::Mode mode,
      LInstruction* instr,
      TargetAddressStorageMode storage_mode = CAN_INLINE_TARGET_ADDRESS);

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

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

206 207 208
  void CallRuntime(Runtime::FunctionId id,
                   int num_arguments,
                   LInstruction* instr) {
209
    const Runtime::Function* function = Runtime::FunctionForId(id);
210 211 212
    CallRuntime(function, num_arguments, instr);
  }

213
  void LoadContextFromDeferred(LOperand* context);
214 215
  void CallRuntimeFromDeferred(Runtime::FunctionId id,
                               int argc,
216 217
                               LInstruction* instr,
                               LOperand* context);
218

219 220 221 222 223
  enum R1State {
    R1_UNINITIALIZED,
    R1_CONTAINS_TARGET
  };

224
  // Generate a direct call to a known function.  Expects the function
225
  // to be in r1.
226
  void CallKnownFunction(Handle<JSFunction> function,
227
                         int formal_parameter_count,
228
                         int arity,
229
                         LInstruction* instr,
230
                         R1State r1_state);
231

232 233
  void RecordSafepointWithLazyDeopt(LInstruction* instr,
                                    SafepointMode safepoint_mode);
234

235 236
  void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
                                            Safepoint::DeoptMode mode);
237
  void DeoptimizeIf(Condition condition,
238 239
                    LEnvironment* environment,
                    Deoptimizer::BailoutType bailout_type);
240
  void DeoptimizeIf(Condition condition, LEnvironment* environment);
241

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

  void PopulateDeoptimizationLiteralsWithInlinedFunctions();

  Register ToRegister(int index) const;
255
  DwVfpRegister ToDoubleRegister(int index) const;
256

257 258 259 260
  MemOperand BuildSeqStringOperand(Register string,
                                   LOperand* index,
                                   String::Encoding encoding);

261
  void EmitIntegerMathAbs(LMathAbs* instr);
262 263

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

  void RecordAndWritePosition(int position) V8_OVERRIDE;
275 276

  static Condition TokenToCondition(Token::Value op, bool is_unsigned);
277
  void EmitGoto(int block);
278 279

  // EmitBranch expects to be the last instruction of a block.
280
  template<class InstrType>
281 282 283
  void EmitBranch(InstrType instr, Condition condition);
  template<class InstrType>
  void EmitFalseBranch(InstrType instr, Condition condition);
284
  void EmitNumberUntagD(Register input,
285
                        DwVfpRegister result,
286
                        bool allow_undefined_as_nan,
287
                        bool deoptimize_on_minus_zero,
288 289
                        LEnvironment* env,
                        NumberUntagDMode mode);
290 291 292 293

  // 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.
294 295 296 297
  Condition EmitTypeofIs(Label* true_label,
                         Label* false_label,
                         Register input,
                         Handle<String> type_name);
298

299 300 301 302 303 304 305 306
  // 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,
                         Label* is_not_object,
                         Label* is_object);

307 308 309 310 311
  // 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,
312 313
                         Label* is_not_string,
                         SmiCheck check_needed);
314

315 316 317 318
  // Emits optimized code for %_IsConstructCall().
  // Caller should branch on equal condition.
  void EmitIsConstructCall(Register temp1, Register temp2);

319 320 321 322 323
  // 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,
324 325
                    int* offset,
                    AllocationSiteMode mode);
326

327
  void EnsureSpaceForLazyDeopt(int space_needed) V8_OVERRIDE;
328 329 330 331 332 333
  void DoLoadKeyedExternalArray(LLoadKeyed* instr);
  void DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr);
  void DoLoadKeyedFixedArray(LLoadKeyed* instr);
  void DoStoreKeyedExternalArray(LStoreKeyed* instr);
  void DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr);
  void DoStoreKeyedFixedArray(LStoreKeyed* instr);
334

335
  ZoneList<LEnvironment*> deoptimizations_;
336
  ZoneList<Deoptimizer::JumpTableEntry> deopt_jump_table_;
337 338 339 340 341 342
  ZoneList<Handle<Object> > deoptimization_literals_;
  int inlined_function_count_;
  Scope* const scope_;
  TranslationBuffer translations_;
  ZoneList<LDeferredCode*> deferred_;
  int osr_pc_offset_;
343
  bool frame_is_built_;
344 345 346 347 348

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

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

352 353
  Safepoint::Kind expected_safepoint_kind_;

354
  class PushSafepointRegistersScope V8_FINAL BASE_EMBEDDED {
355
   public:
356
    explicit PushSafepointRegistersScope(LCodeGen* codegen)
357
        : codegen_(codegen) {
358 359
      DCHECK(codegen_->info()->is_calling());
      DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
360 361
      codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
      codegen_->masm_->PushSafepointRegisters();
362 363 364
    }

    ~PushSafepointRegistersScope() {
365
      DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
366
      codegen_->masm_->PopSafepointRegisters();
367 368 369 370 371 372 373
      codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
    }

   private:
    LCodeGen* codegen_;
  };

374 375 376 377 378 379 380
  friend class LDeferredCode;
  friend class LEnvironment;
  friend class SafepointGenerator;
  DISALLOW_COPY_AND_ASSIGN(LCodeGen);
};


381
class LDeferredCode : public ZoneObject {
382 383
 public:
  explicit LDeferredCode(LCodeGen* codegen)
384 385 386
      : codegen_(codegen),
        external_exit_(NULL),
        instruction_index_(codegen->current_instruction_) {
387 388 389
    codegen->AddDeferredCode(this);
  }

390
  virtual ~LDeferredCode() {}
391
  virtual void Generate() = 0;
392
  virtual LInstruction* instr() = 0;
393

394
  void SetExit(Label* exit) { external_exit_ = exit; }
395 396
  Label* entry() { return &entry_; }
  Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
397
  int instruction_index() const { return instruction_index_; }
398 399 400 401 402 403 404 405 406 407

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

 private:
  LCodeGen* codegen_;
  Label entry_;
  Label exit_;
  Label* external_exit_;
408
  int instruction_index_;
409 410 411 412 413
};

} }  // namespace v8::internal

#endif  // V8_ARM_LITHIUM_CODEGEN_ARM_H_