codegen-mips.cc 46.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
#include "src/v8.h"
6

7
#if V8_TARGET_ARCH_MIPS
8

9 10 11
#include "src/codegen.h"
#include "src/macro-assembler.h"
#include "src/mips/simulator-mips.h"
12 13 14 15

namespace v8 {
namespace internal {

16

17 18 19 20 21 22 23 24 25 26 27 28 29
#define __ masm.


#if defined(USE_SIMULATOR)
byte* fast_exp_mips_machine_code = NULL;
double fast_exp_simulator(double x) {
  return Simulator::current(Isolate::Current())->CallFP(
      fast_exp_mips_machine_code, x, 0);
}
#endif


UnaryMathFunction CreateExpFunction() {
30
  if (!FLAG_fast_math) return &std::exp;
31
  size_t actual_size;
32 33
  byte* buffer =
      static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true));
34
  if (buffer == NULL) return &std::exp;
35 36 37 38 39 40 41 42 43 44 45 46 47
  ExternalReference::InitializeMathExpData();

  MacroAssembler masm(NULL, buffer, static_cast<int>(actual_size));

  {
    DoubleRegister input = f12;
    DoubleRegister result = f0;
    DoubleRegister double_scratch1 = f4;
    DoubleRegister double_scratch2 = f6;
    Register temp1 = t0;
    Register temp2 = t1;
    Register temp3 = t2;

48
    __ MovFromFloatParameter(input);
49 50 51 52 53
    __ Push(temp3, temp2, temp1);
    MathExpGenerator::EmitMathExp(
        &masm, input, result, double_scratch1, double_scratch2,
        temp1, temp2, temp3);
    __ Pop(temp3, temp2, temp1);
54
    __ MovToFloatResult(result);
55 56 57 58 59
    __ Ret();
  }

  CodeDesc desc;
  masm.GetCode(&desc);
60
  DCHECK(!RelocInfo::RequiresRelocation(desc));
61

62 63
  CpuFeatures::FlushICache(buffer, actual_size);
  base::OS::ProtectCode(buffer, actual_size);
64 65 66 67 68 69 70 71 72 73

#if !defined(USE_SIMULATOR)
  return FUNCTION_CAST<UnaryMathFunction>(buffer);
#else
  fast_exp_mips_machine_code = buffer;
  return &fast_exp_simulator;
#endif
}


plind44@gmail.com's avatar
plind44@gmail.com committed
74
#if defined(V8_HOST_ARCH_MIPS)
75
MemCopyUint8Function CreateMemCopyUint8Function(MemCopyUint8Function stub) {
76 77
#if defined(USE_SIMULATOR) || defined(_MIPS_ARCH_MIPS32R6) || \
    defined(_MIPS_ARCH_MIPS32RX)
plind44@gmail.com's avatar
plind44@gmail.com committed
78 79 80
  return stub;
#else
  size_t actual_size;
81 82
  byte* buffer =
      static_cast<byte*>(base::OS::Allocate(3 * KB, &actual_size, true));
plind44@gmail.com's avatar
plind44@gmail.com committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  if (buffer == NULL) return stub;

  // This code assumes that cache lines are 32 bytes and if the cache line is
  // larger it will not work correctly.
  MacroAssembler masm(NULL, buffer, static_cast<int>(actual_size));

  {
    Label lastb, unaligned, aligned, chkw,
          loop16w, chk1w, wordCopy_loop, skip_pref, lastbloop,
          leave, ua_chk16w, ua_loop16w, ua_skip_pref, ua_chkw,
          ua_chk1w, ua_wordCopy_loop, ua_smallCopy, ua_smallCopy_loop;

    // The size of each prefetch.
    uint32_t pref_chunk = 32;
    // The maximum size of a prefetch, it must not be less then pref_chunk.
    // If the real size of a prefetch is greater then max_pref_size and
    // the kPrefHintPrepareForStore hint is used, the code will not work
    // correctly.
    uint32_t max_pref_size = 128;
102
    DCHECK(pref_chunk < max_pref_size);
plind44@gmail.com's avatar
plind44@gmail.com committed
103 104 105 106 107 108 109 110 111 112 113 114

    // pref_limit is set based on the fact that we never use an offset
    // greater then 5 on a store pref and that a single pref can
    // never be larger then max_pref_size.
    uint32_t pref_limit = (5 * pref_chunk) + max_pref_size;
    int32_t pref_hint_load = kPrefHintLoadStreamed;
    int32_t pref_hint_store = kPrefHintPrepareForStore;
    uint32_t loadstore_chunk = 4;

    // The initial prefetches may fetch bytes that are before the buffer being
    // copied. Start copies with an offset of 4 so avoid this situation when
    // using kPrefHintPrepareForStore.
115
    DCHECK(pref_hint_store != kPrefHintPrepareForStore ||
plind44@gmail.com's avatar
plind44@gmail.com committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
           pref_chunk * 4 >= max_pref_size);

    // If the size is less than 8, go to lastb. Regardless of size,
    // copy dst pointer to v0 for the retuen value.
    __ slti(t2, a2, 2 * loadstore_chunk);
    __ bne(t2, zero_reg, &lastb);
    __ mov(v0, a0);  // In delay slot.

    // If src and dst have different alignments, go to unaligned, if they
    // have the same alignment (but are not actually aligned) do a partial
    // load/store to make them aligned. If they are both already aligned
    // we can start copying at aligned.
    __ xor_(t8, a1, a0);
    __ andi(t8, t8, loadstore_chunk - 1);  // t8 is a0/a1 word-displacement.
    __ bne(t8, zero_reg, &unaligned);
    __ subu(a3, zero_reg, a0);  // In delay slot.

    __ andi(a3, a3, loadstore_chunk - 1);  // Copy a3 bytes to align a0/a1.
    __ beq(a3, zero_reg, &aligned);  // Already aligned.
    __ subu(a2, a2, a3);  // In delay slot. a2 is the remining bytes count.

137 138 139 140 141 142 143 144 145 146 147
    if (kArchEndian == kLittle) {
      __ lwr(t8, MemOperand(a1));
      __ addu(a1, a1, a3);
      __ swr(t8, MemOperand(a0));
      __ addu(a0, a0, a3);
    } else {
      __ lwl(t8, MemOperand(a1));
      __ addu(a1, a1, a3);
      __ swl(t8, MemOperand(a0));
      __ addu(a0, a0, a3);
    }
plind44@gmail.com's avatar
plind44@gmail.com committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 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 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    // Now dst/src are both aligned to (word) aligned addresses. Set a2 to
    // count how many bytes we have to copy after all the 64 byte chunks are
    // copied and a3 to the dst pointer after all the 64 byte chunks have been
    // copied. We will loop, incrementing a0 and a1 until a0 equals a3.
    __ bind(&aligned);
    __ andi(t8, a2, 0x3f);
    __ beq(a2, t8, &chkw);  // Less than 64?
    __ subu(a3, a2, t8);  // In delay slot.
    __ addu(a3, a0, a3);  // Now a3 is the final dst after loop.

    // When in the loop we prefetch with kPrefHintPrepareForStore hint,
    // in this case the a0+x should be past the "t0-32" address. This means:
    // for x=128 the last "safe" a0 address is "t0-160". Alternatively, for
    // x=64 the last "safe" a0 address is "t0-96". In the current version we
    // will use "pref hint, 128(a0)", so "t0-160" is the limit.
    if (pref_hint_store == kPrefHintPrepareForStore) {
      __ addu(t0, a0, a2);  // t0 is the "past the end" address.
      __ Subu(t9, t0, pref_limit);  // t9 is the "last safe pref" address.
    }

    __ Pref(pref_hint_load, MemOperand(a1, 0 * pref_chunk));
    __ Pref(pref_hint_load, MemOperand(a1, 1 * pref_chunk));
    __ Pref(pref_hint_load, MemOperand(a1, 2 * pref_chunk));
    __ Pref(pref_hint_load, MemOperand(a1, 3 * pref_chunk));

    if (pref_hint_store != kPrefHintPrepareForStore) {
      __ Pref(pref_hint_store, MemOperand(a0, 1 * pref_chunk));
      __ Pref(pref_hint_store, MemOperand(a0, 2 * pref_chunk));
      __ Pref(pref_hint_store, MemOperand(a0, 3 * pref_chunk));
    }
    __ bind(&loop16w);
    __ lw(t0, MemOperand(a1));

    if (pref_hint_store == kPrefHintPrepareForStore) {
      __ sltu(v1, t9, a0);  // If a0 > t9, don't use next prefetch.
      __ Branch(USE_DELAY_SLOT, &skip_pref, gt, v1, Operand(zero_reg));
    }
    __ lw(t1, MemOperand(a1, 1, loadstore_chunk));  // Maybe in delay slot.

    __ Pref(pref_hint_store, MemOperand(a0, 4 * pref_chunk));
    __ Pref(pref_hint_store, MemOperand(a0, 5 * pref_chunk));

    __ bind(&skip_pref);
    __ lw(t2, MemOperand(a1, 2, loadstore_chunk));
    __ lw(t3, MemOperand(a1, 3, loadstore_chunk));
    __ lw(t4, MemOperand(a1, 4, loadstore_chunk));
    __ lw(t5, MemOperand(a1, 5, loadstore_chunk));
    __ lw(t6, MemOperand(a1, 6, loadstore_chunk));
    __ lw(t7, MemOperand(a1, 7, loadstore_chunk));
    __ Pref(pref_hint_load, MemOperand(a1, 4 * pref_chunk));

    __ sw(t0, MemOperand(a0));
    __ sw(t1, MemOperand(a0, 1, loadstore_chunk));
    __ sw(t2, MemOperand(a0, 2, loadstore_chunk));
    __ sw(t3, MemOperand(a0, 3, loadstore_chunk));
    __ sw(t4, MemOperand(a0, 4, loadstore_chunk));
    __ sw(t5, MemOperand(a0, 5, loadstore_chunk));
    __ sw(t6, MemOperand(a0, 6, loadstore_chunk));
    __ sw(t7, MemOperand(a0, 7, loadstore_chunk));

    __ lw(t0, MemOperand(a1, 8, loadstore_chunk));
    __ lw(t1, MemOperand(a1, 9, loadstore_chunk));
    __ lw(t2, MemOperand(a1, 10, loadstore_chunk));
    __ lw(t3, MemOperand(a1, 11, loadstore_chunk));
    __ lw(t4, MemOperand(a1, 12, loadstore_chunk));
    __ lw(t5, MemOperand(a1, 13, loadstore_chunk));
    __ lw(t6, MemOperand(a1, 14, loadstore_chunk));
    __ lw(t7, MemOperand(a1, 15, loadstore_chunk));
    __ Pref(pref_hint_load, MemOperand(a1, 5 * pref_chunk));

    __ sw(t0, MemOperand(a0, 8, loadstore_chunk));
    __ sw(t1, MemOperand(a0, 9, loadstore_chunk));
    __ sw(t2, MemOperand(a0, 10, loadstore_chunk));
    __ sw(t3, MemOperand(a0, 11, loadstore_chunk));
    __ sw(t4, MemOperand(a0, 12, loadstore_chunk));
    __ sw(t5, MemOperand(a0, 13, loadstore_chunk));
    __ sw(t6, MemOperand(a0, 14, loadstore_chunk));
    __ sw(t7, MemOperand(a0, 15, loadstore_chunk));
    __ addiu(a0, a0, 16 * loadstore_chunk);
    __ bne(a0, a3, &loop16w);
    __ addiu(a1, a1, 16 * loadstore_chunk);  // In delay slot.
    __ mov(a2, t8);

    // Here we have src and dest word-aligned but less than 64-bytes to go.
    // Check for a 32 bytes chunk and copy if there is one. Otherwise jump
    // down to chk1w to handle the tail end of the copy.
    __ bind(&chkw);
    __ Pref(pref_hint_load, MemOperand(a1, 0 * pref_chunk));
    __ andi(t8, a2, 0x1f);
    __ beq(a2, t8, &chk1w);  // Less than 32?
    __ nop();  // In delay slot.
    __ lw(t0, MemOperand(a1));
    __ lw(t1, MemOperand(a1, 1, loadstore_chunk));
    __ lw(t2, MemOperand(a1, 2, loadstore_chunk));
    __ lw(t3, MemOperand(a1, 3, loadstore_chunk));
    __ lw(t4, MemOperand(a1, 4, loadstore_chunk));
    __ lw(t5, MemOperand(a1, 5, loadstore_chunk));
    __ lw(t6, MemOperand(a1, 6, loadstore_chunk));
    __ lw(t7, MemOperand(a1, 7, loadstore_chunk));
    __ addiu(a1, a1, 8 * loadstore_chunk);
    __ sw(t0, MemOperand(a0));
    __ sw(t1, MemOperand(a0, 1, loadstore_chunk));
    __ sw(t2, MemOperand(a0, 2, loadstore_chunk));
    __ sw(t3, MemOperand(a0, 3, loadstore_chunk));
    __ sw(t4, MemOperand(a0, 4, loadstore_chunk));
    __ sw(t5, MemOperand(a0, 5, loadstore_chunk));
    __ sw(t6, MemOperand(a0, 6, loadstore_chunk));
    __ sw(t7, MemOperand(a0, 7, loadstore_chunk));
    __ addiu(a0, a0, 8 * loadstore_chunk);

    // Here we have less than 32 bytes to copy. Set up for a loop to copy
    // one word at a time. Set a2 to count how many bytes we have to copy
    // after all the word chunks are copied and a3 to the dst pointer after
    // all the word chunks have been copied. We will loop, incrementing a0
    // and a1 untill a0 equals a3.
    __ bind(&chk1w);
    __ andi(a2, t8, loadstore_chunk - 1);
    __ beq(a2, t8, &lastb);
    __ subu(a3, t8, a2);  // In delay slot.
    __ addu(a3, a0, a3);

    __ bind(&wordCopy_loop);
    __ lw(t3, MemOperand(a1));
    __ addiu(a0, a0, loadstore_chunk);
    __ addiu(a1, a1, loadstore_chunk);
    __ bne(a0, a3, &wordCopy_loop);
    __ sw(t3, MemOperand(a0, -1, loadstore_chunk));  // In delay slot.

    __ bind(&lastb);
    __ Branch(&leave, le, a2, Operand(zero_reg));
    __ addu(a3, a0, a2);

    __ bind(&lastbloop);
    __ lb(v1, MemOperand(a1));
    __ addiu(a0, a0, 1);
    __ addiu(a1, a1, 1);
    __ bne(a0, a3, &lastbloop);
    __ sb(v1, MemOperand(a0, -1));  // In delay slot.

    __ bind(&leave);
    __ jr(ra);
    __ nop();

    // Unaligned case. Only the dst gets aligned so we need to do partial
    // loads of the source followed by normal stores to the dst (once we
    // have aligned the destination).
    __ bind(&unaligned);
    __ andi(a3, a3, loadstore_chunk - 1);  // Copy a3 bytes to align a0/a1.
    __ beq(a3, zero_reg, &ua_chk16w);
    __ subu(a2, a2, a3);  // In delay slot.

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    if (kArchEndian == kLittle) {
      __ lwr(v1, MemOperand(a1));
      __ lwl(v1,
             MemOperand(a1, 1, loadstore_chunk, MemOperand::offset_minus_one));
      __ addu(a1, a1, a3);
      __ swr(v1, MemOperand(a0));
      __ addu(a0, a0, a3);
    } else {
      __ lwl(v1, MemOperand(a1));
      __ lwr(v1,
             MemOperand(a1, 1, loadstore_chunk, MemOperand::offset_minus_one));
      __ addu(a1, a1, a3);
      __ swl(v1, MemOperand(a0));
      __ addu(a0, a0, a3);
    }
plind44@gmail.com's avatar
plind44@gmail.com committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

    // Now the dst (but not the source) is aligned. Set a2 to count how many
    // bytes we have to copy after all the 64 byte chunks are copied and a3 to
    // the dst pointer after all the 64 byte chunks have been copied. We will
    // loop, incrementing a0 and a1 until a0 equals a3.
    __ bind(&ua_chk16w);
    __ andi(t8, a2, 0x3f);
    __ beq(a2, t8, &ua_chkw);
    __ subu(a3, a2, t8);  // In delay slot.
    __ addu(a3, a0, a3);

    if (pref_hint_store == kPrefHintPrepareForStore) {
      __ addu(t0, a0, a2);
      __ Subu(t9, t0, pref_limit);
    }

    __ Pref(pref_hint_load, MemOperand(a1, 0 * pref_chunk));
    __ Pref(pref_hint_load, MemOperand(a1, 1 * pref_chunk));
    __ Pref(pref_hint_load, MemOperand(a1, 2 * pref_chunk));

    if (pref_hint_store != kPrefHintPrepareForStore) {
      __ Pref(pref_hint_store, MemOperand(a0, 1 * pref_chunk));
      __ Pref(pref_hint_store, MemOperand(a0, 2 * pref_chunk));
      __ Pref(pref_hint_store, MemOperand(a0, 3 * pref_chunk));
    }

    __ bind(&ua_loop16w);
    __ Pref(pref_hint_load, MemOperand(a1, 3 * pref_chunk));
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
    if (kArchEndian == kLittle) {
      __ lwr(t0, MemOperand(a1));
      __ lwr(t1, MemOperand(a1, 1, loadstore_chunk));
      __ lwr(t2, MemOperand(a1, 2, loadstore_chunk));

      if (pref_hint_store == kPrefHintPrepareForStore) {
        __ sltu(v1, t9, a0);
        __ Branch(USE_DELAY_SLOT, &ua_skip_pref, gt, v1, Operand(zero_reg));
      }
      __ lwr(t3, MemOperand(a1, 3, loadstore_chunk));  // Maybe in delay slot.

      __ Pref(pref_hint_store, MemOperand(a0, 4 * pref_chunk));
      __ Pref(pref_hint_store, MemOperand(a0, 5 * pref_chunk));

      __ bind(&ua_skip_pref);
      __ lwr(t4, MemOperand(a1, 4, loadstore_chunk));
      __ lwr(t5, MemOperand(a1, 5, loadstore_chunk));
      __ lwr(t6, MemOperand(a1, 6, loadstore_chunk));
      __ lwr(t7, MemOperand(a1, 7, loadstore_chunk));
      __ lwl(t0,
             MemOperand(a1, 1, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t1,
             MemOperand(a1, 2, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t2,
             MemOperand(a1, 3, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t3,
             MemOperand(a1, 4, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t4,
             MemOperand(a1, 5, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t5,
             MemOperand(a1, 6, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t6,
             MemOperand(a1, 7, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t7,
             MemOperand(a1, 8, loadstore_chunk, MemOperand::offset_minus_one));
    } else {
      __ lwl(t0, MemOperand(a1));
      __ lwl(t1, MemOperand(a1, 1, loadstore_chunk));
      __ lwl(t2, MemOperand(a1, 2, loadstore_chunk));

      if (pref_hint_store == kPrefHintPrepareForStore) {
        __ sltu(v1, t9, a0);
        __ Branch(USE_DELAY_SLOT, &ua_skip_pref, gt, v1, Operand(zero_reg));
      }
      __ lwl(t3, MemOperand(a1, 3, loadstore_chunk));  // Maybe in delay slot.

      __ Pref(pref_hint_store, MemOperand(a0, 4 * pref_chunk));
      __ Pref(pref_hint_store, MemOperand(a0, 5 * pref_chunk));

      __ bind(&ua_skip_pref);
      __ lwl(t4, MemOperand(a1, 4, loadstore_chunk));
      __ lwl(t5, MemOperand(a1, 5, loadstore_chunk));
      __ lwl(t6, MemOperand(a1, 6, loadstore_chunk));
      __ lwl(t7, MemOperand(a1, 7, loadstore_chunk));
      __ lwr(t0,
             MemOperand(a1, 1, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t1,
             MemOperand(a1, 2, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t2,
             MemOperand(a1, 3, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t3,
             MemOperand(a1, 4, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t4,
             MemOperand(a1, 5, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t5,
             MemOperand(a1, 6, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t6,
             MemOperand(a1, 7, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t7,
             MemOperand(a1, 8, loadstore_chunk, MemOperand::offset_minus_one));
plind44@gmail.com's avatar
plind44@gmail.com committed
412 413 414 415 416 417 418 419 420 421
    }
    __ Pref(pref_hint_load, MemOperand(a1, 4 * pref_chunk));
    __ sw(t0, MemOperand(a0));
    __ sw(t1, MemOperand(a0, 1, loadstore_chunk));
    __ sw(t2, MemOperand(a0, 2, loadstore_chunk));
    __ sw(t3, MemOperand(a0, 3, loadstore_chunk));
    __ sw(t4, MemOperand(a0, 4, loadstore_chunk));
    __ sw(t5, MemOperand(a0, 5, loadstore_chunk));
    __ sw(t6, MemOperand(a0, 6, loadstore_chunk));
    __ sw(t7, MemOperand(a0, 7, loadstore_chunk));
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
    if (kArchEndian == kLittle) {
      __ lwr(t0, MemOperand(a1, 8, loadstore_chunk));
      __ lwr(t1, MemOperand(a1, 9, loadstore_chunk));
      __ lwr(t2, MemOperand(a1, 10, loadstore_chunk));
      __ lwr(t3, MemOperand(a1, 11, loadstore_chunk));
      __ lwr(t4, MemOperand(a1, 12, loadstore_chunk));
      __ lwr(t5, MemOperand(a1, 13, loadstore_chunk));
      __ lwr(t6, MemOperand(a1, 14, loadstore_chunk));
      __ lwr(t7, MemOperand(a1, 15, loadstore_chunk));
      __ lwl(t0,
             MemOperand(a1, 9, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t1,
             MemOperand(a1, 10, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t2,
             MemOperand(a1, 11, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t3,
             MemOperand(a1, 12, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t4,
             MemOperand(a1, 13, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t5,
             MemOperand(a1, 14, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t6,
             MemOperand(a1, 15, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t7,
             MemOperand(a1, 16, loadstore_chunk, MemOperand::offset_minus_one));
    } else {
      __ lwl(t0, MemOperand(a1, 8, loadstore_chunk));
      __ lwl(t1, MemOperand(a1, 9, loadstore_chunk));
      __ lwl(t2, MemOperand(a1, 10, loadstore_chunk));
      __ lwl(t3, MemOperand(a1, 11, loadstore_chunk));
      __ lwl(t4, MemOperand(a1, 12, loadstore_chunk));
      __ lwl(t5, MemOperand(a1, 13, loadstore_chunk));
      __ lwl(t6, MemOperand(a1, 14, loadstore_chunk));
      __ lwl(t7, MemOperand(a1, 15, loadstore_chunk));
      __ lwr(t0,
             MemOperand(a1, 9, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t1,
             MemOperand(a1, 10, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t2,
             MemOperand(a1, 11, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t3,
             MemOperand(a1, 12, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t4,
             MemOperand(a1, 13, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t5,
             MemOperand(a1, 14, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t6,
             MemOperand(a1, 15, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t7,
             MemOperand(a1, 16, loadstore_chunk, MemOperand::offset_minus_one));
    }
plind44@gmail.com's avatar
plind44@gmail.com committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
    __ Pref(pref_hint_load, MemOperand(a1, 5 * pref_chunk));
    __ sw(t0, MemOperand(a0, 8, loadstore_chunk));
    __ sw(t1, MemOperand(a0, 9, loadstore_chunk));
    __ sw(t2, MemOperand(a0, 10, loadstore_chunk));
    __ sw(t3, MemOperand(a0, 11, loadstore_chunk));
    __ sw(t4, MemOperand(a0, 12, loadstore_chunk));
    __ sw(t5, MemOperand(a0, 13, loadstore_chunk));
    __ sw(t6, MemOperand(a0, 14, loadstore_chunk));
    __ sw(t7, MemOperand(a0, 15, loadstore_chunk));
    __ addiu(a0, a0, 16 * loadstore_chunk);
    __ bne(a0, a3, &ua_loop16w);
    __ addiu(a1, a1, 16 * loadstore_chunk);  // In delay slot.
    __ mov(a2, t8);

    // Here less than 64-bytes. Check for
    // a 32 byte chunk and copy if there is one. Otherwise jump down to
    // ua_chk1w to handle the tail end of the copy.
    __ bind(&ua_chkw);
    __ Pref(pref_hint_load, MemOperand(a1));
    __ andi(t8, a2, 0x1f);

    __ beq(a2, t8, &ua_chk1w);
    __ nop();  // In delay slot.
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
    if (kArchEndian == kLittle) {
      __ lwr(t0, MemOperand(a1));
      __ lwr(t1, MemOperand(a1, 1, loadstore_chunk));
      __ lwr(t2, MemOperand(a1, 2, loadstore_chunk));
      __ lwr(t3, MemOperand(a1, 3, loadstore_chunk));
      __ lwr(t4, MemOperand(a1, 4, loadstore_chunk));
      __ lwr(t5, MemOperand(a1, 5, loadstore_chunk));
      __ lwr(t6, MemOperand(a1, 6, loadstore_chunk));
      __ lwr(t7, MemOperand(a1, 7, loadstore_chunk));
      __ lwl(t0,
             MemOperand(a1, 1, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t1,
             MemOperand(a1, 2, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t2,
             MemOperand(a1, 3, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t3,
             MemOperand(a1, 4, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t4,
             MemOperand(a1, 5, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t5,
             MemOperand(a1, 6, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t6,
             MemOperand(a1, 7, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t7,
             MemOperand(a1, 8, loadstore_chunk, MemOperand::offset_minus_one));
    } else {
      __ lwl(t0, MemOperand(a1));
      __ lwl(t1, MemOperand(a1, 1, loadstore_chunk));
      __ lwl(t2, MemOperand(a1, 2, loadstore_chunk));
      __ lwl(t3, MemOperand(a1, 3, loadstore_chunk));
      __ lwl(t4, MemOperand(a1, 4, loadstore_chunk));
      __ lwl(t5, MemOperand(a1, 5, loadstore_chunk));
      __ lwl(t6, MemOperand(a1, 6, loadstore_chunk));
      __ lwl(t7, MemOperand(a1, 7, loadstore_chunk));
      __ lwr(t0,
             MemOperand(a1, 1, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t1,
             MemOperand(a1, 2, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t2,
             MemOperand(a1, 3, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t3,
             MemOperand(a1, 4, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t4,
             MemOperand(a1, 5, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t5,
             MemOperand(a1, 6, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t6,
             MemOperand(a1, 7, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t7,
             MemOperand(a1, 8, loadstore_chunk, MemOperand::offset_minus_one));
    }
plind44@gmail.com's avatar
plind44@gmail.com committed
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
    __ addiu(a1, a1, 8 * loadstore_chunk);
    __ sw(t0, MemOperand(a0));
    __ sw(t1, MemOperand(a0, 1, loadstore_chunk));
    __ sw(t2, MemOperand(a0, 2, loadstore_chunk));
    __ sw(t3, MemOperand(a0, 3, loadstore_chunk));
    __ sw(t4, MemOperand(a0, 4, loadstore_chunk));
    __ sw(t5, MemOperand(a0, 5, loadstore_chunk));
    __ sw(t6, MemOperand(a0, 6, loadstore_chunk));
    __ sw(t7, MemOperand(a0, 7, loadstore_chunk));
    __ addiu(a0, a0, 8 * loadstore_chunk);

    // Less than 32 bytes to copy. Set up for a loop to
    // copy one word at a time.
    __ bind(&ua_chk1w);
    __ andi(a2, t8, loadstore_chunk - 1);
    __ beq(a2, t8, &ua_smallCopy);
    __ subu(a3, t8, a2);  // In delay slot.
    __ addu(a3, a0, a3);

    __ bind(&ua_wordCopy_loop);
567 568 569 570 571 572 573 574 575
    if (kArchEndian == kLittle) {
      __ lwr(v1, MemOperand(a1));
      __ lwl(v1,
             MemOperand(a1, 1, loadstore_chunk, MemOperand::offset_minus_one));
    } else {
      __ lwl(v1, MemOperand(a1));
      __ lwr(v1,
             MemOperand(a1, 1, loadstore_chunk, MemOperand::offset_minus_one));
    }
plind44@gmail.com's avatar
plind44@gmail.com committed
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
    __ addiu(a0, a0, loadstore_chunk);
    __ addiu(a1, a1, loadstore_chunk);
    __ bne(a0, a3, &ua_wordCopy_loop);
    __ sw(v1, MemOperand(a0, -1, loadstore_chunk));  // In delay slot.

    // Copy the last 8 bytes.
    __ bind(&ua_smallCopy);
    __ beq(a2, zero_reg, &leave);
    __ addu(a3, a0, a2);  // In delay slot.

    __ bind(&ua_smallCopy_loop);
    __ lb(v1, MemOperand(a1));
    __ addiu(a0, a0, 1);
    __ addiu(a1, a1, 1);
    __ bne(a0, a3, &ua_smallCopy_loop);
    __ sb(v1, MemOperand(a0, -1));  // In delay slot.

    __ jr(ra);
    __ nop();
  }
  CodeDesc desc;
  masm.GetCode(&desc);
598
  DCHECK(!RelocInfo::RequiresRelocation(desc));
plind44@gmail.com's avatar
plind44@gmail.com committed
599

600 601
  CpuFeatures::FlushICache(buffer, actual_size);
  base::OS::ProtectCode(buffer, actual_size);
602
  return FUNCTION_CAST<MemCopyUint8Function>(buffer);
plind44@gmail.com's avatar
plind44@gmail.com committed
603 604 605 606
#endif
}
#endif

607 608 609 610 611
UnaryMathFunction CreateSqrtFunction() {
#if defined(USE_SIMULATOR)
  return &std::sqrt;
#else
  size_t actual_size;
612 613
  byte* buffer =
      static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true));
614
  if (buffer == NULL) return &std::sqrt;
615

616
  MacroAssembler masm(NULL, buffer, static_cast<int>(actual_size));
617

618
  __ MovFromFloatParameter(f12);
619
  __ sqrt_d(f0, f12);
620
  __ MovToFloatResult(f0);
621 622 623 624
  __ Ret();

  CodeDesc desc;
  masm.GetCode(&desc);
625
  DCHECK(!RelocInfo::RequiresRelocation(desc));
626

627 628
  CpuFeatures::FlushICache(buffer, actual_size);
  base::OS::ProtectCode(buffer, actual_size);
629 630
  return FUNCTION_CAST<UnaryMathFunction>(buffer);
#endif
631 632
}

633 634
#undef __

635

636 637 638 639
// -------------------------------------------------------------------------
// Platform-specific RuntimeCallHelper functions.

void StubRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const {
640
  masm->EnterFrame(StackFrame::INTERNAL);
641
  DCHECK(!masm->has_frame());
642
  masm->set_has_frame(true);
643 644 645 646
}


void StubRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
647
  masm->LeaveFrame(StackFrame::INTERNAL);
648
  DCHECK(masm->has_frame());
649
  masm->set_has_frame(false);
650 651
}

652

653 654 655
// -------------------------------------------------------------------------
// Code generators

656 657
#define __ ACCESS_MASM(masm)

658
void ElementsTransitionGenerator::GenerateMapChangeElementsTransition(
659 660 661 662 663 664
    MacroAssembler* masm,
    Register receiver,
    Register key,
    Register value,
    Register target_map,
    AllocationSiteMode mode,
665
    Label* allocation_memento_found) {
666
  Register scratch_elements = t0;
667
  DCHECK(!AreAliased(receiver, key, value, target_map,
668 669
                     scratch_elements));

670
  if (mode == TRACK_ALLOCATION_SITE) {
671
    DCHECK(allocation_memento_found != NULL);
672 673
    __ JumpIfJSArrayHasAllocationMemento(
        receiver, scratch_elements, allocation_memento_found);
674 675
  }

676
  // Set transitioned map.
677 678
  __ sw(target_map, FieldMemOperand(receiver, HeapObject::kMapOffset));
  __ RecordWriteField(receiver,
679
                      HeapObject::kMapOffset,
680
                      target_map,
681 682 683 684 685 686 687 688
                      t5,
                      kRAHasNotBeenSaved,
                      kDontSaveFPRegs,
                      EMIT_REMEMBERED_SET,
                      OMIT_SMI_CHECK);
}


689
void ElementsTransitionGenerator::GenerateSmiToDouble(
690 691 692 693 694 695 696 697
    MacroAssembler* masm,
    Register receiver,
    Register key,
    Register value,
    Register target_map,
    AllocationSiteMode mode,
    Label* fail) {
  // Register ra contains the return address.
698
  Label loop, entry, convert_hole, gc_required, only_change_map, done;
699 700 701 702 703 704 705 706 707 708 709
  Register elements = t0;
  Register length = t1;
  Register array = t2;
  Register array_end = array;

  // target_map parameter can be clobbered.
  Register scratch1 = target_map;
  Register scratch2 = t5;
  Register scratch3 = t3;

  // Verify input registers don't conflict with locals.
710
  DCHECK(!AreAliased(receiver, key, value, target_map,
711
                     elements, length, array, scratch2));
712 713 714

  Register scratch = t6;

715
  if (mode == TRACK_ALLOCATION_SITE) {
716
    __ JumpIfJSArrayHasAllocationMemento(receiver, elements, fail);
717 718
  }

719 720
  // Check for empty arrays, which only require a map transition and no changes
  // to the backing store.
721
  __ lw(elements, FieldMemOperand(receiver, JSObject::kElementsOffset));
722
  __ LoadRoot(at, Heap::kEmptyFixedArrayRootIndex);
723
  __ Branch(&only_change_map, eq, at, Operand(elements));
724 725

  __ push(ra);
726 727 728
  __ lw(length, FieldMemOperand(elements, FixedArray::kLengthOffset));
  // elements: source FixedArray
  // length: number of elements (smi-tagged)
729 730

  // Allocate new FixedDoubleArray.
731
  __ sll(scratch, length, 2);
732
  __ Addu(scratch, scratch, FixedDoubleArray::kHeaderSize);
733 734
  __ Allocate(scratch, array, t3, scratch2, &gc_required, DOUBLE_ALIGNMENT);
  // array: destination FixedDoubleArray, not tagged as heap object
735

736
  // Set destination FixedDoubleArray's length and map.
737 738
  __ LoadRoot(scratch2, Heap::kFixedDoubleArrayMapRootIndex);
  __ sw(length, MemOperand(array, FixedDoubleArray::kLengthOffset));
739
  // Update receiver's map.
740
  __ sw(scratch2, MemOperand(array, HeapObject::kMapOffset));
741

742 743
  __ sw(target_map, FieldMemOperand(receiver, HeapObject::kMapOffset));
  __ RecordWriteField(receiver,
744
                      HeapObject::kMapOffset,
745 746
                      target_map,
                      scratch2,
747 748
                      kRAHasBeenSaved,
                      kDontSaveFPRegs,
749
                      OMIT_REMEMBERED_SET,
750 751
                      OMIT_SMI_CHECK);
  // Replace receiver's backing store with newly created FixedDoubleArray.
752 753 754
  __ Addu(scratch1, array, Operand(kHeapObjectTag));
  __ sw(scratch1, FieldMemOperand(a2, JSObject::kElementsOffset));
  __ RecordWriteField(receiver,
755
                      JSObject::kElementsOffset,
756 757
                      scratch1,
                      scratch2,
758 759 760 761 762 763 764
                      kRAHasBeenSaved,
                      kDontSaveFPRegs,
                      EMIT_REMEMBERED_SET,
                      OMIT_SMI_CHECK);


  // Prepare for conversion loop.
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
  __ Addu(scratch1, elements,
      Operand(FixedArray::kHeaderSize - kHeapObjectTag));
  __ Addu(scratch3, array, Operand(FixedDoubleArray::kHeaderSize));
  __ sll(at, length, 2);
  __ Addu(array_end, scratch3, at);

  // Repurpose registers no longer in use.
  Register hole_lower = elements;
  Register hole_upper = length;

  __ li(hole_lower, Operand(kHoleNanLower32));
  // scratch1: begin of source FixedArray element fields, not tagged
  // hole_lower: kHoleNanLower32
  // hole_upper: kHoleNanUpper32
  // array_end: end of destination FixedDoubleArray, not tagged
  // scratch3: begin of FixedDoubleArray element fields, not tagged
  __ Branch(USE_DELAY_SLOT, &entry);
  __ li(hole_upper, Operand(kHoleNanUpper32));  // In delay slot.
783

784
  __ bind(&only_change_map);
785 786
  __ sw(target_map, FieldMemOperand(receiver, HeapObject::kMapOffset));
  __ RecordWriteField(receiver,
787
                      HeapObject::kMapOffset,
788 789
                      target_map,
                      scratch2,
790
                      kRAHasBeenSaved,
791 792 793 794 795
                      kDontSaveFPRegs,
                      OMIT_REMEMBERED_SET,
                      OMIT_SMI_CHECK);
  __ Branch(&done);

796 797
  // Call into runtime if GC is required.
  __ bind(&gc_required);
798
  __ lw(ra, MemOperand(sp, 0));
799
  __ Branch(USE_DELAY_SLOT, fail);
800
  __ addiu(sp, sp, kPointerSize);  // In delay slot.
801 802 803

  // Convert and copy elements.
  __ bind(&loop);
804 805 806 807
  __ lw(scratch2, MemOperand(scratch1));
  __ Addu(scratch1, scratch1, kIntSize);
  // scratch2: current element
  __ UntagAndJumpIfNotSmi(scratch2, scratch2, &convert_hole);
808 809

  // Normal smi, convert to double and store.
810
  __ mtc1(scratch2, f0);
811
  __ cvt_d_w(f0, f0);
812 813 814
  __ sdc1(f0, MemOperand(scratch3));
  __ Branch(USE_DELAY_SLOT, &entry);
  __ addiu(scratch3, scratch3, kDoubleSize);  // In delay slot.
815 816 817

  // Hole found, store the-hole NaN.
  __ bind(&convert_hole);
818
  if (FLAG_debug_code) {
819
    // Restore a "smi-untagged" heap object.
820 821
    __ SmiTag(scratch2);
    __ Or(scratch2, scratch2, Operand(1));
822
    __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
823
    __ Assert(eq, kObjectFoundInSmiOnlyArray, at, Operand(scratch2));
824
  }
825 826 827 828
  // mantissa
  __ sw(hole_lower, MemOperand(scratch3, Register::kMantissaOffset));
  // exponent
  __ sw(hole_upper, MemOperand(scratch3, Register::kExponentOffset));
829
  __ bind(&entry);
830
  __ addiu(scratch3, scratch3, kDoubleSize);
831 832

  __ Branch(&loop, lt, scratch3, Operand(array_end));
833

834
  __ bind(&done);
835
  __ pop(ra);
836 837 838 839
}


void ElementsTransitionGenerator::GenerateDoubleToObject(
840 841 842 843 844 845 846 847
    MacroAssembler* masm,
    Register receiver,
    Register key,
    Register value,
    Register target_map,
    AllocationSiteMode mode,
    Label* fail) {
  // Register ra contains the return address.
848
  Label entry, loop, convert_hole, gc_required, only_change_map;
849 850 851 852 853 854
  Register elements = t0;
  Register array = t2;
  Register length = t1;
  Register scratch = t5;

  // Verify input registers don't conflict with locals.
855
  DCHECK(!AreAliased(receiver, key, value, target_map,
856
                     elements, array, length, scratch));
857

858
  if (mode == TRACK_ALLOCATION_SITE) {
859
    __ JumpIfJSArrayHasAllocationMemento(receiver, elements, fail);
860 861
  }

862 863
  // Check for empty arrays, which only require a map transition and no changes
  // to the backing store.
864
  __ lw(elements, FieldMemOperand(receiver, JSObject::kElementsOffset));
865
  __ LoadRoot(at, Heap::kEmptyFixedArrayRootIndex);
866
  __ Branch(&only_change_map, eq, at, Operand(elements));
867

868 869
  __ MultiPush(
      value.bit() | key.bit() | receiver.bit() | target_map.bit() | ra.bit());
870

871 872 873
  __ lw(length, FieldMemOperand(elements, FixedArray::kLengthOffset));
  // elements: source FixedArray
  // length: number of elements (smi-tagged)
874 875

  // Allocate new FixedArray.
876 877 878 879 880 881 882 883 884
  // Re-use value and target_map registers, as they have been saved on the
  // stack.
  Register array_size = value;
  Register allocate_scratch = target_map;
  __ sll(array_size, length, 1);
  __ Addu(array_size, array_size, FixedDoubleArray::kHeaderSize);
  __ Allocate(array_size, array, allocate_scratch, scratch, &gc_required,
              NO_ALLOCATION_FLAGS);
  // array: destination FixedArray, not tagged as heap object
885
  // Set destination FixedDoubleArray's length and map.
886 887 888
  __ LoadRoot(scratch, Heap::kFixedArrayMapRootIndex);
  __ sw(length, MemOperand(array, FixedDoubleArray::kLengthOffset));
  __ sw(scratch, MemOperand(array, HeapObject::kMapOffset));
889 890

  // Prepare for conversion loop.
891 892 893 894 895
  Register src_elements = elements;
  Register dst_elements = target_map;
  Register dst_end = length;
  Register heap_number_map = scratch;
  __ Addu(src_elements, src_elements, Operand(
896 897
        FixedDoubleArray::kHeaderSize - kHeapObjectTag
        + Register::kExponentOffset));
898 899 900 901 902
  __ Addu(dst_elements, array, Operand(FixedArray::kHeaderSize));
  __ Addu(array, array, Operand(kHeapObjectTag));
  __ sll(dst_end, dst_end, 1);
  __ Addu(dst_end, dst_elements, dst_end);
  __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
903
  // Using offsetted addresses.
904 905 906 907 908 909
  // dst_elements: begin of destination FixedArray element fields, not tagged
  // src_elements: begin of source FixedDoubleArray element fields, not tagged,
  //               points to the exponent
  // dst_end: end of destination FixedArray, not tagged
  // array: destination FixedArray
  // heap_number_map: heap number map
910 911 912 913
  __ Branch(&entry);

  // Call into runtime if GC is required.
  __ bind(&gc_required);
914 915
  __ MultiPop(
      value.bit() | key.bit() | receiver.bit() | target_map.bit() | ra.bit());
916 917 918 919

  __ Branch(fail);

  __ bind(&loop);
920 921 922 923 924
  Register upper_bits = key;
  __ lw(upper_bits, MemOperand(src_elements));
  __ Addu(src_elements, src_elements, kDoubleSize);
  // upper_bits: current element's upper 32 bit
  // src_elements: address of next element's upper 32 bit
925 926 927
  __ Branch(&convert_hole, eq, a1, Operand(kHoleNanUpper32));

  // Non-hole double, copy value into a heap number.
928 929 930 931 932 933 934 935 936
  Register heap_number = receiver;
  Register scratch2 = value;
  Register scratch3 = t6;
  __ AllocateHeapNumber(heap_number, scratch2, scratch3, heap_number_map,
                        &gc_required);
  // heap_number: new heap number
  // Load mantissa of current element, src_elements
  // point to exponent of next element.
  __ lw(scratch2, MemOperand(src_elements, (Register::kMantissaOffset
937
      - Register::kExponentOffset - kDoubleSize)));
938 939 940 941 942 943 944 945
  __ sw(scratch2, FieldMemOperand(heap_number, HeapNumber::kMantissaOffset));
  __ sw(upper_bits, FieldMemOperand(heap_number, HeapNumber::kExponentOffset));
  __ mov(scratch2, dst_elements);
  __ sw(heap_number, MemOperand(dst_elements));
  __ Addu(dst_elements, dst_elements, kIntSize);
  __ RecordWrite(array,
                 scratch2,
                 heap_number,
946 947 948 949 950 951 952 953
                 kRAHasBeenSaved,
                 kDontSaveFPRegs,
                 EMIT_REMEMBERED_SET,
                 OMIT_SMI_CHECK);
  __ Branch(&entry);

  // Replace the-hole NaN with the-hole pointer.
  __ bind(&convert_hole);
954 955 956
  __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex);
  __ sw(scratch2, MemOperand(dst_elements));
  __ Addu(dst_elements, dst_elements, kIntSize);
957 958

  __ bind(&entry);
959
  __ Branch(&loop, lt, dst_elements, Operand(dst_end));
960

961
  __ MultiPop(receiver.bit() | target_map.bit() | value.bit() | key.bit());
962
  // Replace receiver's backing store with newly created and filled FixedArray.
963 964
  __ sw(array, FieldMemOperand(receiver, JSObject::kElementsOffset));
  __ RecordWriteField(receiver,
965
                      JSObject::kElementsOffset,
966 967
                      array,
                      scratch,
968 969 970 971 972
                      kRAHasBeenSaved,
                      kDontSaveFPRegs,
                      EMIT_REMEMBERED_SET,
                      OMIT_SMI_CHECK);
  __ pop(ra);
973 974 975

  __ bind(&only_change_map);
  // Update receiver's map.
976 977
  __ sw(target_map, FieldMemOperand(receiver, HeapObject::kMapOffset));
  __ RecordWriteField(receiver,
978
                      HeapObject::kMapOffset,
979 980
                      target_map,
                      scratch,
981 982 983 984
                      kRAHasNotBeenSaved,
                      kDontSaveFPRegs,
                      OMIT_REMEMBERED_SET,
                      OMIT_SMI_CHECK);
985 986
}

987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

void StringCharLoadGenerator::Generate(MacroAssembler* masm,
                                       Register string,
                                       Register index,
                                       Register result,
                                       Label* call_runtime) {
  // Fetch the instance type of the receiver into result register.
  __ lw(result, FieldMemOperand(string, HeapObject::kMapOffset));
  __ lbu(result, FieldMemOperand(result, Map::kInstanceTypeOffset));

  // We need special handling for indirect strings.
  Label check_sequential;
  __ And(at, result, Operand(kIsIndirectStringMask));
  __ Branch(&check_sequential, eq, at, Operand(zero_reg));

  // Dispatch on the indirect string shape: slice or cons.
  Label cons_string;
  __ And(at, result, Operand(kSlicedNotConsMask));
  __ Branch(&cons_string, eq, at, Operand(zero_reg));

  // Handle slices.
  Label indirect_string_loaded;
  __ lw(result, FieldMemOperand(string, SlicedString::kOffsetOffset));
1010
  __ lw(string, FieldMemOperand(string, SlicedString::kParentOffset));
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
  __ sra(at, result, kSmiTagSize);
  __ Addu(index, index, at);
  __ jmp(&indirect_string_loaded);

  // Handle cons strings.
  // Check whether the right hand side is the empty string (i.e. if
  // this is really a flat string in a cons string). If that is not
  // the case we would rather go to the runtime system now to flatten
  // the string.
  __ bind(&cons_string);
  __ lw(result, FieldMemOperand(string, ConsString::kSecondOffset));
1022
  __ LoadRoot(at, Heap::kempty_stringRootIndex);
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
  __ Branch(call_runtime, ne, result, Operand(at));
  // Get the first of the two strings and load its instance type.
  __ lw(string, FieldMemOperand(string, ConsString::kFirstOffset));

  __ bind(&indirect_string_loaded);
  __ lw(result, FieldMemOperand(string, HeapObject::kMapOffset));
  __ lbu(result, FieldMemOperand(result, Map::kInstanceTypeOffset));

  // Distinguish sequential and external strings. Only these two string
  // representations can reach here (slices and flat cons strings have been
  // reduced to the underlying sequential or external string).
  Label external_string, check_encoding;
  __ bind(&check_sequential);
  STATIC_ASSERT(kSeqStringTag == 0);
  __ And(at, result, Operand(kStringRepresentationMask));
  __ Branch(&external_string, ne, at, Operand(zero_reg));

  // Prepare sequential strings
1041
  STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
  __ Addu(string,
          string,
          SeqTwoByteString::kHeaderSize - kHeapObjectTag);
  __ jmp(&check_encoding);

  // Handle external strings.
  __ bind(&external_string);
  if (FLAG_debug_code) {
    // Assert that we do not have a cons or slice (indirect strings) here.
    // Sequential strings have already been ruled out.
    __ And(at, result, Operand(kIsIndirectStringMask));
1053
    __ Assert(eq, kExternalStringExpectedButNotFound,
1054 1055 1056
        at, Operand(zero_reg));
  }
  // Rule out short external strings.
1057
  STATIC_ASSERT(kShortExternalStringTag != 0);
1058 1059 1060 1061
  __ And(at, result, Operand(kShortExternalStringMask));
  __ Branch(call_runtime, ne, at, Operand(zero_reg));
  __ lw(string, FieldMemOperand(string, ExternalString::kResourceDataOffset));

1062
  Label one_byte, done;
1063 1064 1065
  __ bind(&check_encoding);
  STATIC_ASSERT(kTwoByteStringTag == 0);
  __ And(at, result, Operand(kStringEncodingMask));
1066
  __ Branch(&one_byte, ne, at, Operand(zero_reg));
1067 1068 1069 1070 1071
  // Two-byte string.
  __ sll(at, index, 1);
  __ Addu(at, string, at);
  __ lhu(result, MemOperand(at));
  __ jmp(&done);
1072 1073
  __ bind(&one_byte);
  // One_byte string.
1074 1075 1076 1077 1078
  __ Addu(at, string, index);
  __ lbu(result, MemOperand(at));
  __ bind(&done);
}

1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092

static MemOperand ExpConstant(int index, Register base) {
  return MemOperand(base, index * kDoubleSize);
}


void MathExpGenerator::EmitMathExp(MacroAssembler* masm,
                                   DoubleRegister input,
                                   DoubleRegister result,
                                   DoubleRegister double_scratch1,
                                   DoubleRegister double_scratch2,
                                   Register temp1,
                                   Register temp2,
                                   Register temp3) {
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
  DCHECK(!input.is(result));
  DCHECK(!input.is(double_scratch1));
  DCHECK(!input.is(double_scratch2));
  DCHECK(!result.is(double_scratch1));
  DCHECK(!result.is(double_scratch2));
  DCHECK(!double_scratch1.is(double_scratch2));
  DCHECK(!temp1.is(temp2));
  DCHECK(!temp1.is(temp3));
  DCHECK(!temp2.is(temp3));
  DCHECK(ExternalReference::math_exp_constants(0).address() != NULL);
1103
  DCHECK(!masm->serializer_enabled());  // External references not serializable.
1104

1105
  Label zero, infinity, done;
1106 1107 1108 1109

  __ li(temp3, Operand(ExternalReference::math_exp_constants(0)));

  __ ldc1(double_scratch1, ExpConstant(0, temp3));
1110 1111
  __ BranchF(&zero, NULL, ge, double_scratch1, input);

1112
  __ ldc1(double_scratch2, ExpConstant(1, temp3));
1113 1114
  __ BranchF(&infinity, NULL, ge, input, double_scratch2);

1115 1116 1117 1118
  __ ldc1(double_scratch1, ExpConstant(3, temp3));
  __ ldc1(result, ExpConstant(4, temp3));
  __ mul_d(double_scratch1, double_scratch1, input);
  __ add_d(double_scratch1, double_scratch1, result);
1119
  __ FmoveLow(temp2, double_scratch1);
1120 1121 1122 1123 1124 1125
  __ sub_d(double_scratch1, double_scratch1, result);
  __ ldc1(result, ExpConstant(6, temp3));
  __ ldc1(double_scratch2, ExpConstant(5, temp3));
  __ mul_d(double_scratch1, double_scratch1, double_scratch2);
  __ sub_d(double_scratch1, double_scratch1, input);
  __ sub_d(result, result, double_scratch1);
1126 1127
  __ mul_d(double_scratch2, double_scratch1, double_scratch1);
  __ mul_d(result, result, double_scratch2);
1128 1129 1130
  __ ldc1(double_scratch2, ExpConstant(7, temp3));
  __ mul_d(result, result, double_scratch2);
  __ sub_d(result, result, double_scratch1);
1131
  // Mov 1 in double_scratch2 as math_exp_constants_array[8] == 1.
1132
  DCHECK(*reinterpret_cast<double*>
1133 1134
         (ExternalReference::math_exp_constants(8).address()) == 1);
  __ Move(double_scratch2, 1);
1135
  __ add_d(result, result, double_scratch2);
1136 1137
  __ srl(temp1, temp2, 11);
  __ Ext(temp2, temp2, 0, 11);
1138 1139 1140 1141 1142
  __ Addu(temp1, temp1, Operand(0x3ff));

  // Must not call ExpConstant() after overwriting temp3!
  __ li(temp3, Operand(ExternalReference::math_exp_log_table()));
  __ sll(at, temp2, 3);
1143
  __ Addu(temp3, temp3, Operand(at));
1144 1145
  __ lw(temp2, MemOperand(temp3, Register::kMantissaOffset));
  __ lw(temp3, MemOperand(temp3, Register::kExponentOffset));
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
  // The first word is loaded is the lower number register.
  if (temp2.code() < temp3.code()) {
    __ sll(at, temp1, 20);
    __ Or(temp1, temp3, at);
    __ Move(double_scratch1, temp2, temp1);
  } else {
    __ sll(at, temp1, 20);
    __ Or(temp1, temp2, at);
    __ Move(double_scratch1, temp3, temp1);
  }
  __ mul_d(result, result, double_scratch1);
1157
  __ BranchShort(&done);
1158 1159 1160

  __ bind(&zero);
  __ Move(result, kDoubleRegZero);
1161
  __ BranchShort(&done);
1162 1163 1164 1165

  __ bind(&infinity);
  __ ldc1(result, ExpConstant(2, temp3));

1166 1167 1168
  __ bind(&done);
}

1169
#ifdef DEBUG
1170 1171
// nop(CODE_AGE_MARKER_NOP)
static const uint32_t kCodeAgePatchFirstInstruction = 0x00010180;
1172
#endif
1173

1174 1175

CodeAgingHelper::CodeAgingHelper() {
1176
  DCHECK(young_sequence_.length() == kNoCodeAgeSequenceLength);
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
  // Since patcher is a large object, allocate it dynamically when needed,
  // to avoid overloading the stack in stress conditions.
  // DONT_FLUSH is used because the CodeAgingHelper is initialized early in
  // the process, before MIPS simulator ICache is setup.
  SmartPointer<CodePatcher> patcher(
      new CodePatcher(young_sequence_.start(),
                      young_sequence_.length() / Assembler::kInstrSize,
                      CodePatcher::DONT_FLUSH));
  PredictableCodeSizeScope scope(patcher->masm(), young_sequence_.length());
  patcher->masm()->Push(ra, fp, cp, a1);
  patcher->masm()->nop(Assembler::CODE_AGE_SEQUENCE_NOP);
  patcher->masm()->Addu(
      fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
1190
}
1191

1192

1193 1194 1195 1196 1197 1198 1199 1200 1201
#ifdef DEBUG
bool CodeAgingHelper::IsOld(byte* candidate) const {
  return Memory::uint32_at(candidate) == kCodeAgePatchFirstInstruction;
}
#endif


bool Code::IsYoungSequence(Isolate* isolate, byte* sequence) {
  bool result = isolate->code_aging_helper()->IsYoung(sequence);
1202
  DCHECK(result || isolate->code_aging_helper()->IsOld(sequence));
1203
  return result;
1204 1205 1206
}


1207
void Code::GetCodeAgeAndParity(Isolate* isolate, byte* sequence, Age* age,
1208
                               MarkingParity* parity) {
1209
  if (IsYoungSequence(isolate, sequence)) {
1210
    *age = kNoAgeCodeAge;
1211 1212
    *parity = NO_MARKING_PARITY;
  } else {
1213 1214
    Address target_address = Assembler::target_address_at(
        sequence + Assembler::kInstrSize);
1215 1216 1217
    Code* stub = GetCodeFromTargetAddress(target_address);
    GetCodeAgeAndParity(stub, age, parity);
  }
1218 1219 1220
}


1221 1222
void Code::PatchPlatformCodeAge(Isolate* isolate,
                                byte* sequence,
1223 1224
                                Code::Age age,
                                MarkingParity parity) {
1225
  uint32_t young_length = isolate->code_aging_helper()->young_sequence_length();
1226
  if (age == kNoAgeCodeAge) {
1227
    isolate->code_aging_helper()->CopyYoungSequenceTo(sequence);
1228
    CpuFeatures::FlushICache(sequence, young_length);
1229
  } else {
1230
    Code* stub = GetCodeAgeStub(isolate, age, parity);
1231
    CodePatcher patcher(sequence, young_length / Assembler::kInstrSize);
1232
    // Mark this code sequence for FindPlatformCodeAgeSequence().
1233
    patcher.masm()->nop(Assembler::CODE_AGE_MARKER_NOP);
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
    // Load the stub address to t9 and call it,
    // GetCodeAgeAndParity() extracts the stub address from this instruction.
    patcher.masm()->li(
        t9,
        Operand(reinterpret_cast<uint32_t>(stub->instruction_start())),
        CONSTANT_SIZE);
    patcher.masm()->nop();  // Prevent jalr to jal optimization.
    patcher.masm()->jalr(t9, a0);
    patcher.masm()->nop();  // Branch delay slot nop.
    patcher.masm()->nop();  // Pad the empty space.
1244
  }
1245 1246
}

1247 1248 1249

#undef __

1250
} }  // namespace v8::internal
1251 1252

#endif  // V8_TARGET_ARCH_MIPS