deoptimizer-mips.cc 11.8 KB
Newer Older
1
// Copyright 2011 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/assembler-inl.h"
6
#include "src/deoptimizer.h"
7
#include "src/register-configuration.h"
8
#include "src/safepoint-table.h"
9

10 11 12
namespace v8 {
namespace internal {

13

14 15 16 17 18
#define __ masm()->


// This code tries to be close to ia32 code so that any changes can be
// easily ported.
19
void Deoptimizer::TableEntryGenerator::Generate() {
20 21 22 23 24 25 26 27 28
  GeneratePrologue();

  // Unlike on ARM we don't save all the registers, just the useful ones.
  // For the rest, there are gaps on the stack, so the offsets remain the same.
  const int kNumberOfRegisters = Register::kNumRegisters;

  RegList restored_regs = kJSCallerSaved | kCalleeSaved;
  RegList saved_regs = restored_regs | sp.bit() | ra.bit();

29 30
  const int kDoubleRegsSize = kDoubleSize * DoubleRegister::kNumRegisters;
  const int kFloatRegsSize = kFloatSize * FloatRegister::kNumRegisters;
31

32 33
  // Save all FPU registers before messing with them.
  __ Subu(sp, sp, Operand(kDoubleRegsSize));
34
  const RegisterConfiguration* config = RegisterConfiguration::Default();
35 36 37 38
  for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
    int code = config->GetAllocatableDoubleCode(i);
    const DoubleRegister fpu_reg = DoubleRegister::from_code(code);
    int offset = code * kDoubleSize;
39
    __ Sdc1(fpu_reg, MemOperand(sp, offset));
40 41
  }

42 43 44 45 46 47 48 49
  __ Subu(sp, sp, Operand(kFloatRegsSize));
  for (int i = 0; i < config->num_allocatable_float_registers(); ++i) {
    int code = config->GetAllocatableFloatCode(i);
    const FloatRegister fpu_reg = FloatRegister::from_code(code);
    int offset = code * kFloatSize;
    __ swc1(fpu_reg, MemOperand(sp, offset));
  }

50 51 52 53 54 55 56 57 58
  // Push saved_regs (needed to populate FrameDescription::registers_).
  // Leave gaps for other registers.
  __ Subu(sp, sp, kNumberOfRegisters * kPointerSize);
  for (int16_t i = kNumberOfRegisters - 1; i >= 0; i--) {
    if ((saved_regs & (1 << i)) != 0) {
      __ sw(ToRegister(i), MemOperand(sp, kPointerSize * i));
    }
  }

59 60
  __ li(a2, Operand(ExternalReference::Create(
                IsolateAddressId::kCEntryFPAddress, isolate())));
61 62
  __ sw(fp, MemOperand(a2));

63
  const int kSavedRegistersAreaSize =
64
      (kNumberOfRegisters * kPointerSize) + kDoubleRegsSize + kFloatRegsSize;
65 66 67 68

  // Get the bailout id from the stack.
  __ lw(a2, MemOperand(sp, kSavedRegistersAreaSize));

69
  // Get the address of the location in the code object (a3) (return
70 71
  // address for lazy deoptimization) and compute the fp-to-sp delta in
  // register t0.
72 73 74
  __ mov(a3, ra);
  // Correct one word for bailout id.
  __ Addu(t0, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
75 76 77 78 79

  __ Subu(t0, fp, t0);

  // Allocate a new deoptimizer object.
  __ PrepareCallCFunction(6, t1);
80 81 82 83 84
  // Pass four arguments in a0 to a3 and fifth & sixth arguments on stack.
  __ mov(a0, zero_reg);
  Label context_check;
  __ lw(a1, MemOperand(fp, CommonFrameConstants::kContextOrFrameTypeOffset));
  __ JumpIfSmi(a1, &context_check);
85
  __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
86
  __ bind(&context_check);
87
  __ li(a1, Operand(static_cast<int>(deopt_kind())));
88 89 90
  // a2: bailout id already loaded.
  // a3: code address or 0 already loaded.
  __ sw(t0, CFunctionArgumentOperand(5));  // Fp-to-sp delta.
91
  __ li(t1, Operand(ExternalReference::isolate_address(isolate())));
92 93 94 95
  __ sw(t1, CFunctionArgumentOperand(6));  // Isolate.
  // Call Deoptimizer::New().
  {
    AllowExternalCallThatCantCauseGC scope(masm());
96
    __ CallCFunction(ExternalReference::new_deoptimizer_function(), 6);
97 98 99 100 101 102 103 104 105
  }

  // Preserve "deoptimizer" object in register v0 and get the input
  // frame descriptor pointer to a1 (deoptimizer->input_);
  // Move deopt-obj to a0 for call to Deoptimizer::ComputeOutputFrames() below.
  __ mov(a0, v0);
  __ lw(a1, MemOperand(v0, Deoptimizer::input_offset()));

  // Copy core registers into FrameDescription::registers_[kNumRegisters].
106
  DCHECK_EQ(Register::kNumRegisters, kNumberOfRegisters);
107 108 109 110 111 112 113 114 115 116 117
  for (int i = 0; i < kNumberOfRegisters; i++) {
    int offset = (i * kPointerSize) + FrameDescription::registers_offset();
    if ((saved_regs & (1 << i)) != 0) {
      __ lw(a2, MemOperand(sp, i * kPointerSize));
      __ sw(a2, MemOperand(a1, offset));
    } else if (FLAG_debug_code) {
      __ li(a2, kDebugZapValue);
      __ sw(a2, MemOperand(a1, offset));
    }
  }

118
  int double_regs_offset = FrameDescription::double_registers_offset();
119 120
  // Copy FPU registers to
  // double_registers_[DoubleRegister::kNumAllocatableRegisters]
121 122 123
  for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
    int code = config->GetAllocatableDoubleCode(i);
    int dst_offset = code * kDoubleSize + double_regs_offset;
124 125
    int src_offset =
        code * kDoubleSize + kNumberOfRegisters * kPointerSize + kFloatRegsSize;
126 127
    __ Ldc1(f0, MemOperand(sp, src_offset));
    __ Sdc1(f0, MemOperand(a1, dst_offset));
128 129
  }

130 131 132 133 134 135 136 137 138 139 140
  // Copy FPU registers to
  // float_registers_[FloatRegister::kNumAllocatableRegisters]
  int float_regs_offset = FrameDescription::float_registers_offset();
  for (int i = 0; i < config->num_allocatable_float_registers(); ++i) {
    int code = config->GetAllocatableFloatCode(i);
    int dst_offset = code * kFloatSize + float_regs_offset;
    int src_offset = code * kFloatSize + kNumberOfRegisters * kPointerSize;
    __ lwc1(f0, MemOperand(sp, src_offset));
    __ swc1(f0, MemOperand(a1, dst_offset));
  }

141 142
  // Remove the bailout id and the saved registers from the stack.
  __ Addu(sp, sp, Operand(kSavedRegistersAreaSize + (1 * kPointerSize)));
143 144 145 146 147 148 149 150 151 152 153

  // Compute a pointer to the unwinding limit in register a2; that is
  // the first stack slot not part of the input frame.
  __ lw(a2, MemOperand(a1, FrameDescription::frame_size_offset()));
  __ Addu(a2, a2, sp);

  // Unwind the stack down to - but not including - the unwinding
  // limit and copy the contents of the activation frame to the input
  // frame description.
  __ Addu(a3, a1, Operand(FrameDescription::frame_content_offset()));
  Label pop_loop;
154
  Label pop_loop_header;
155
  __ BranchShort(&pop_loop_header);
156 157 158
  __ bind(&pop_loop);
  __ pop(t0);
  __ sw(t0, MemOperand(a3, 0));
159 160
  __ addiu(a3, a3, sizeof(uint32_t));
  __ bind(&pop_loop_header);
161
  __ BranchShort(&pop_loop, ne, a2, Operand(sp));
162 163 164 165 166 167 168 169

  // Compute the output frame in the deoptimizer.
  __ push(a0);  // Preserve deoptimizer object across call.
  // a0: deoptimizer object; a1: scratch.
  __ PrepareCallCFunction(1, a1);
  // Call Deoptimizer::ComputeOutputFrames().
  {
    AllowExternalCallThatCantCauseGC scope(masm());
170
    __ CallCFunction(ExternalReference::compute_output_frames_function(), 1);
171 172 173
  }
  __ pop(a0);  // Restore deoptimizer object (class Deoptimizer).

174 175
  __ lw(sp, MemOperand(a0, Deoptimizer::caller_frame_top_offset()));

176
  // Replace the current (input) frame with the output frames.
177 178
  Label outer_push_loop, inner_push_loop,
      outer_loop_header, inner_loop_header;
179
  // Outer loop state: t0 = current "FrameDescription** output_",
180 181
  // a1 = one past the last FrameDescription**.
  __ lw(a1, MemOperand(a0, Deoptimizer::output_count_offset()));
182
  __ lw(t0, MemOperand(a0, Deoptimizer::output_offset()));  // t0 is output_.
183
  __ Lsa(a1, t0, a1, kPointerSizeLog2);
184
  __ BranchShort(&outer_loop_header);
185 186
  __ bind(&outer_push_loop);
  // Inner loop state: a2 = current FrameDescription*, a3 = loop index.
187
  __ lw(a2, MemOperand(t0, 0));  // output_[ix]
188
  __ lw(a3, MemOperand(a2, FrameDescription::frame_size_offset()));
189
  __ BranchShort(&inner_loop_header);
190 191 192 193 194
  __ bind(&inner_push_loop);
  __ Subu(a3, a3, Operand(sizeof(uint32_t)));
  __ Addu(t2, a2, Operand(a3));
  __ lw(t3, MemOperand(t2, FrameDescription::frame_content_offset()));
  __ push(t3);
195
  __ bind(&inner_loop_header);
196
  __ BranchShort(&inner_push_loop, ne, a3, Operand(zero_reg));
197

198
  __ Addu(t0, t0, Operand(kPointerSize));
199
  __ bind(&outer_loop_header);
200
  __ BranchShort(&outer_push_loop, lt, t0, Operand(a1));
201

202
  __ lw(a1, MemOperand(a0, Deoptimizer::input_offset()));
203 204 205 206
  for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
    int code = config->GetAllocatableDoubleCode(i);
    const DoubleRegister fpu_reg = DoubleRegister::from_code(code);
    int src_offset = code * kDoubleSize + double_regs_offset;
207
    __ Ldc1(fpu_reg, MemOperand(a1, src_offset));
208
  }
209

210
  // Push pc and continuation from the last output frame.
211 212 213 214 215 216 217 218
  __ lw(t2, MemOperand(a2, FrameDescription::pc_offset()));
  __ push(t2);
  __ lw(t2, MemOperand(a2, FrameDescription::continuation_offset()));
  __ push(t2);


  // Technically restoring 'at' should work unless zero_reg is also restored
  // but it's safer to check for this.
219
  DCHECK(!(at.bit() & restored_regs));
220 221 222 223 224 225 226 227 228 229 230 231 232
  // Restore the registers from the last output frame.
  __ mov(at, a2);
  for (int i = kNumberOfRegisters - 1; i >= 0; i--) {
    int offset = (i * kPointerSize) + FrameDescription::registers_offset();
    if ((restored_regs & (1 << i)) != 0) {
      __ lw(ToRegister(i), MemOperand(at, offset));
    }
  }

  __ pop(at);  // Get continuation, leave pc on stack.
  __ pop(ra);
  __ Jump(at);
  __ stop("Unreachable.");
233
}
234

235

236
// Maximum size of a table entry generated below.
237
#ifdef _MIPS_ARCH_MIPS32R6
238
const int Deoptimizer::table_entry_size_ = 2 * kInstrSize;
239
#else
240
const int Deoptimizer::table_entry_size_ = 3 * kInstrSize;
241
#endif
242

243
void Deoptimizer::TableEntryGenerator::GeneratePrologue() {
244 245
  Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());

246 247
  // Create a sequence of deoptimization entries.
  // Note that registers are still live when jumping to an entry.
248
  Label table_start, done, trampoline_jump;
249
  __ bind(&table_start);
250 251 252

#ifdef _MIPS_ARCH_MIPS32R6
  int kMaxEntriesBranchReach =
253
      (1 << (kImm26Bits - 2)) / (table_entry_size_ / kInstrSize);
254
#else
255 256
  int kMaxEntriesBranchReach =
      (1 << (kImm16Bits - 2)) / (table_entry_size_ / kInstrSize);
257
#endif
258 259 260 261 262 263 264

  if (count() <= kMaxEntriesBranchReach) {
    // Common case.
    for (int i = 0; i < count(); i++) {
      Label start;
      __ bind(&start);
      DCHECK(is_int16(i));
265
      if (IsMipsArchVariant(kMips32r6)) {
266
        __ li(kScratchReg, i);
267 268 269
        __ BranchShort(PROTECT, &done);
      } else {
        __ BranchShort(USE_DELAY_SLOT, &done);  // Expose delay slot.
270
        __ li(kScratchReg, i);                  // In the delay slot.
271 272
        __ nop();
      }
273 274 275 276 277 278
      DCHECK_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
    }

    DCHECK_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
        count() * table_entry_size_);
    __ bind(&done);
279
    __ Push(kScratchReg);
280
  } else {
281
    DCHECK(!IsMipsArchVariant(kMips32r6));
282
    // Uncommon case, the branch cannot reach.
283 284
    // Create mini trampoline to reach the end of the table
    for (int i = 0, j = 0; i < count(); i++, j++) {
285 286 287
      Label start;
      __ bind(&start);
      DCHECK(is_int16(i));
288 289
      if (j >= kMaxEntriesBranchReach) {
        j = 0;
290
        __ li(kScratchReg, i);
291 292 293 294 295 296
        __ bind(&trampoline_jump);
        trampoline_jump = Label();
        __ BranchShort(USE_DELAY_SLOT, &trampoline_jump);
        __ nop();
      } else {
        __ BranchShort(USE_DELAY_SLOT, &trampoline_jump);  // Expose delay slot.
297
        __ li(kScratchReg, i);                             // In the delay slot.
298 299
        __ nop();
      }
300 301
      DCHECK_EQ(table_entry_size_, masm()->SizeOfCodeGeneratedSince(&start));
    }
302

303 304
    DCHECK_EQ(masm()->SizeOfCodeGeneratedSince(&table_start),
        count() * table_entry_size_);
305
    __ bind(&trampoline_jump);
306
    __ Push(kScratchReg);
307
  }
308 309
}

310
bool Deoptimizer::PadTopOfStackRegister() { return false; }
311 312 313 314 315 316 317 318 319 320 321

void FrameDescription::SetCallerPc(unsigned offset, intptr_t value) {
  SetFrameSlot(offset, value);
}


void FrameDescription::SetCallerFp(unsigned offset, intptr_t value) {
  SetFrameSlot(offset, value);
}


322
void FrameDescription::SetCallerConstantPool(unsigned offset, intptr_t value) {
323
  // No embedded constant pool support.
324 325 326 327
  UNREACHABLE();
}


328 329
#undef __

330

331 332
}  // namespace internal
}  // namespace v8