codegen-mips64.cc 30.3 KB
Newer Older
1 2 3 4
// Copyright 2012 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#include "src/mips64/codegen-mips64.h"

7 8
#if V8_TARGET_ARCH_MIPS64

9 10
#include <memory>

11 12 13 14 15 16 17 18 19 20 21 22
#include "src/codegen.h"
#include "src/macro-assembler.h"
#include "src/mips64/simulator-mips64.h"

namespace v8 {
namespace internal {


#define __ masm.


#if defined(V8_HOST_ARCH_MIPS)
23 24
MemCopyUint8Function CreateMemCopyUint8Function(Isolate* isolate,
                                                MemCopyUint8Function stub) {
25 26 27 28 29 30 31
#if defined(USE_SIMULATOR)
  return stub;
#else

  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(base::OS::Allocate(3 * KB, &actual_size, true));
32
  if (buffer == nullptr) return stub;
33 34 35

  // This code assumes that cache lines are 32 bytes and if the cache line is
  // larger it will not work correctly.
36
  MacroAssembler masm(isolate, buffer, static_cast<int>(actual_size),
37
                      CodeObjectRequired::kNo);
38 39 40 41 42 43 44 45 46

  {
    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;
thakis's avatar
thakis committed
47 48
    // The maximum size of a prefetch, it must not be less than pref_chunk.
    // If the real size of a prefetch is greater than max_pref_size and
49 50 51
    // the kPrefHintPrepareForStore hint is used, the code will not work
    // correctly.
    uint32_t max_pref_size = 128;
52
    DCHECK(pref_chunk < max_pref_size);
53 54 55 56 57 58 59 60 61 62 63 64

    // 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.
65
    DCHECK(pref_hint_store != kPrefHintPrepareForStore ||
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
           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(a6, a2, 2 * loadstore_chunk);
    __ bne(a6, 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.

86 87 88 89 90 91 92 93 94 95 96
    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);
    }
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 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

    // 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 "a4-32" address. This means:
    // for x=128 the last "safe" a0 address is "a4-160". Alternatively, for
    // x=64 the last "safe" a0 address is "a4-96". In the current version we
    // will use "pref hint, 128(a0)", so "a4-160" is the limit.
    if (pref_hint_store == kPrefHintPrepareForStore) {
      __ addu(a4, a0, a2);  // a4 is the "past the end" address.
      __ Subu(t9, a4, 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(a4, 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(a5, 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(a6, MemOperand(a1, 2, loadstore_chunk));
    __ lw(a7, MemOperand(a1, 3, loadstore_chunk));
    __ lw(t0, MemOperand(a1, 4, loadstore_chunk));
    __ lw(t1, MemOperand(a1, 5, loadstore_chunk));
    __ lw(t2, MemOperand(a1, 6, loadstore_chunk));
    __ lw(t3, MemOperand(a1, 7, loadstore_chunk));
    __ Pref(pref_hint_load, MemOperand(a1, 4 * pref_chunk));

    __ sw(a4, MemOperand(a0));
    __ sw(a5, MemOperand(a0, 1, loadstore_chunk));
    __ sw(a6, MemOperand(a0, 2, loadstore_chunk));
    __ sw(a7, MemOperand(a0, 3, loadstore_chunk));
    __ sw(t0, MemOperand(a0, 4, loadstore_chunk));
    __ sw(t1, MemOperand(a0, 5, loadstore_chunk));
    __ sw(t2, MemOperand(a0, 6, loadstore_chunk));
    __ sw(t3, MemOperand(a0, 7, loadstore_chunk));

    __ lw(a4, MemOperand(a1, 8, loadstore_chunk));
    __ lw(a5, MemOperand(a1, 9, loadstore_chunk));
    __ lw(a6, MemOperand(a1, 10, loadstore_chunk));
    __ lw(a7, MemOperand(a1, 11, loadstore_chunk));
    __ lw(t0, MemOperand(a1, 12, loadstore_chunk));
    __ lw(t1, MemOperand(a1, 13, loadstore_chunk));
    __ lw(t2, MemOperand(a1, 14, loadstore_chunk));
    __ lw(t3, MemOperand(a1, 15, loadstore_chunk));
    __ Pref(pref_hint_load, MemOperand(a1, 5 * pref_chunk));

    __ sw(a4, MemOperand(a0, 8, loadstore_chunk));
    __ sw(a5, MemOperand(a0, 9, loadstore_chunk));
    __ sw(a6, MemOperand(a0, 10, loadstore_chunk));
    __ sw(a7, MemOperand(a0, 11, loadstore_chunk));
    __ sw(t0, MemOperand(a0, 12, loadstore_chunk));
    __ sw(t1, MemOperand(a0, 13, loadstore_chunk));
    __ sw(t2, MemOperand(a0, 14, loadstore_chunk));
    __ sw(t3, 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(a4, MemOperand(a1));
    __ lw(a5, MemOperand(a1, 1, loadstore_chunk));
    __ lw(a6, MemOperand(a1, 2, loadstore_chunk));
    __ lw(a7, MemOperand(a1, 3, loadstore_chunk));
    __ lw(t0, MemOperand(a1, 4, loadstore_chunk));
    __ lw(t1, MemOperand(a1, 5, loadstore_chunk));
    __ lw(t2, MemOperand(a1, 6, loadstore_chunk));
    __ lw(t3, MemOperand(a1, 7, loadstore_chunk));
    __ addiu(a1, a1, 8 * loadstore_chunk);
    __ sw(a4, MemOperand(a0));
    __ sw(a5, MemOperand(a0, 1, loadstore_chunk));
    __ sw(a6, MemOperand(a0, 2, loadstore_chunk));
    __ sw(a7, MemOperand(a0, 3, loadstore_chunk));
    __ sw(t0, MemOperand(a0, 4, loadstore_chunk));
    __ sw(t1, MemOperand(a0, 5, loadstore_chunk));
    __ sw(t2, MemOperand(a0, 6, loadstore_chunk));
    __ sw(t3, 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(a7, MemOperand(a1));
    __ addiu(a0, a0, loadstore_chunk);
    __ addiu(a1, a1, loadstore_chunk);
    __ bne(a0, a3, &wordCopy_loop);
    __ sw(a7, 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.

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    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);
    }
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

    // 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(a4, a0, a2);
      __ Subu(t9, a4, 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);
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    if (kArchEndian == kLittle) {
      __ Pref(pref_hint_load, MemOperand(a1, 3 * pref_chunk));
      __ lwr(a4, MemOperand(a1));
      __ lwr(a5, MemOperand(a1, 1, loadstore_chunk));
      __ lwr(a6, 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(a7, 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(t0, MemOperand(a1, 4, loadstore_chunk));
      __ lwr(t1, MemOperand(a1, 5, loadstore_chunk));
      __ lwr(t2, MemOperand(a1, 6, loadstore_chunk));
      __ lwr(t3, MemOperand(a1, 7, loadstore_chunk));
      __ lwl(a4,
             MemOperand(a1, 1, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(a5,
             MemOperand(a1, 2, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(a6,
             MemOperand(a1, 3, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(a7,
             MemOperand(a1, 4, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t0,
             MemOperand(a1, 5, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t1,
             MemOperand(a1, 6, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t2,
             MemOperand(a1, 7, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t3,
             MemOperand(a1, 8, loadstore_chunk, MemOperand::offset_minus_one));
    } else {
      __ Pref(pref_hint_load, MemOperand(a1, 3 * pref_chunk));
      __ lwl(a4, MemOperand(a1));
      __ lwl(a5, MemOperand(a1, 1, loadstore_chunk));
      __ lwl(a6, 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(a7, 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(t0, MemOperand(a1, 4, loadstore_chunk));
      __ lwl(t1, MemOperand(a1, 5, loadstore_chunk));
      __ lwl(t2, MemOperand(a1, 6, loadstore_chunk));
      __ lwl(t3, MemOperand(a1, 7, loadstore_chunk));
      __ lwr(a4,
             MemOperand(a1, 1, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(a5,
             MemOperand(a1, 2, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(a6,
             MemOperand(a1, 3, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(a7,
             MemOperand(a1, 4, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t0,
             MemOperand(a1, 5, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t1,
             MemOperand(a1, 6, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t2,
             MemOperand(a1, 7, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t3,
             MemOperand(a1, 8, loadstore_chunk, MemOperand::offset_minus_one));
363 364 365 366 367 368 369 370 371 372
    }
    __ Pref(pref_hint_load, MemOperand(a1, 4 * pref_chunk));
    __ sw(a4, MemOperand(a0));
    __ sw(a5, MemOperand(a0, 1, loadstore_chunk));
    __ sw(a6, MemOperand(a0, 2, loadstore_chunk));
    __ sw(a7, MemOperand(a0, 3, loadstore_chunk));
    __ sw(t0, MemOperand(a0, 4, loadstore_chunk));
    __ sw(t1, MemOperand(a0, 5, loadstore_chunk));
    __ sw(t2, MemOperand(a0, 6, loadstore_chunk));
    __ sw(t3, MemOperand(a0, 7, loadstore_chunk));
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 412 413 414 415 416 417 418 419 420 421 422 423
    if (kArchEndian == kLittle) {
      __ lwr(a4, MemOperand(a1, 8, loadstore_chunk));
      __ lwr(a5, MemOperand(a1, 9, loadstore_chunk));
      __ lwr(a6, MemOperand(a1, 10, loadstore_chunk));
      __ lwr(a7, MemOperand(a1, 11, loadstore_chunk));
      __ lwr(t0, MemOperand(a1, 12, loadstore_chunk));
      __ lwr(t1, MemOperand(a1, 13, loadstore_chunk));
      __ lwr(t2, MemOperand(a1, 14, loadstore_chunk));
      __ lwr(t3, MemOperand(a1, 15, loadstore_chunk));
      __ lwl(a4,
             MemOperand(a1, 9, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(a5,
             MemOperand(a1, 10, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(a6,
             MemOperand(a1, 11, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(a7,
             MemOperand(a1, 12, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t0,
             MemOperand(a1, 13, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t1,
             MemOperand(a1, 14, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t2,
             MemOperand(a1, 15, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t3,
             MemOperand(a1, 16, loadstore_chunk, MemOperand::offset_minus_one));
    } else {
      __ lwl(a4, MemOperand(a1, 8, loadstore_chunk));
      __ lwl(a5, MemOperand(a1, 9, loadstore_chunk));
      __ lwl(a6, MemOperand(a1, 10, loadstore_chunk));
      __ lwl(a7, MemOperand(a1, 11, loadstore_chunk));
      __ lwl(t0, MemOperand(a1, 12, loadstore_chunk));
      __ lwl(t1, MemOperand(a1, 13, loadstore_chunk));
      __ lwl(t2, MemOperand(a1, 14, loadstore_chunk));
      __ lwl(t3, MemOperand(a1, 15, loadstore_chunk));
      __ lwr(a4,
             MemOperand(a1, 9, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(a5,
             MemOperand(a1, 10, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(a6,
             MemOperand(a1, 11, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(a7,
             MemOperand(a1, 12, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t0,
             MemOperand(a1, 13, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t1,
             MemOperand(a1, 14, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t2,
             MemOperand(a1, 15, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t3,
             MemOperand(a1, 16, loadstore_chunk, MemOperand::offset_minus_one));
    }
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
    __ Pref(pref_hint_load, MemOperand(a1, 5 * pref_chunk));
    __ sw(a4, MemOperand(a0, 8, loadstore_chunk));
    __ sw(a5, MemOperand(a0, 9, loadstore_chunk));
    __ sw(a6, MemOperand(a0, 10, loadstore_chunk));
    __ sw(a7, MemOperand(a0, 11, loadstore_chunk));
    __ sw(t0, MemOperand(a0, 12, loadstore_chunk));
    __ sw(t1, MemOperand(a0, 13, loadstore_chunk));
    __ sw(t2, MemOperand(a0, 14, loadstore_chunk));
    __ sw(t3, 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.
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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
    if (kArchEndian == kLittle) {
      __ lwr(a4, MemOperand(a1));
      __ lwr(a5, MemOperand(a1, 1, loadstore_chunk));
      __ lwr(a6, MemOperand(a1, 2, loadstore_chunk));
      __ lwr(a7, MemOperand(a1, 3, loadstore_chunk));
      __ lwr(t0, MemOperand(a1, 4, loadstore_chunk));
      __ lwr(t1, MemOperand(a1, 5, loadstore_chunk));
      __ lwr(t2, MemOperand(a1, 6, loadstore_chunk));
      __ lwr(t3, MemOperand(a1, 7, loadstore_chunk));
      __ lwl(a4,
             MemOperand(a1, 1, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(a5,
             MemOperand(a1, 2, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(a6,
             MemOperand(a1, 3, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(a7,
             MemOperand(a1, 4, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t0,
             MemOperand(a1, 5, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t1,
             MemOperand(a1, 6, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t2,
             MemOperand(a1, 7, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwl(t3,
             MemOperand(a1, 8, loadstore_chunk, MemOperand::offset_minus_one));
    } else {
      __ lwl(a4, MemOperand(a1));
      __ lwl(a5, MemOperand(a1, 1, loadstore_chunk));
      __ lwl(a6, MemOperand(a1, 2, loadstore_chunk));
      __ lwl(a7, MemOperand(a1, 3, loadstore_chunk));
      __ lwl(t0, MemOperand(a1, 4, loadstore_chunk));
      __ lwl(t1, MemOperand(a1, 5, loadstore_chunk));
      __ lwl(t2, MemOperand(a1, 6, loadstore_chunk));
      __ lwl(t3, MemOperand(a1, 7, loadstore_chunk));
      __ lwr(a4,
             MemOperand(a1, 1, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(a5,
             MemOperand(a1, 2, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(a6,
             MemOperand(a1, 3, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(a7,
             MemOperand(a1, 4, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t0,
             MemOperand(a1, 5, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t1,
             MemOperand(a1, 6, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t2,
             MemOperand(a1, 7, loadstore_chunk, MemOperand::offset_minus_one));
      __ lwr(t3,
             MemOperand(a1, 8, loadstore_chunk, MemOperand::offset_minus_one));
    }
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
    __ addiu(a1, a1, 8 * loadstore_chunk);
    __ sw(a4, MemOperand(a0));
    __ sw(a5, MemOperand(a0, 1, loadstore_chunk));
    __ sw(a6, MemOperand(a0, 2, loadstore_chunk));
    __ sw(a7, MemOperand(a0, 3, loadstore_chunk));
    __ sw(t0, MemOperand(a0, 4, loadstore_chunk));
    __ sw(t1, MemOperand(a0, 5, loadstore_chunk));
    __ sw(t2, MemOperand(a0, 6, loadstore_chunk));
    __ sw(t3, 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);
518 519 520 521 522 523 524 525 526
    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));
    }
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
    __ 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);
549
  DCHECK(!RelocInfo::RequiresRelocation(desc));
550

551
  Assembler::FlushICache(isolate, buffer, actual_size);
552 553 554 555 556 557
  base::OS::ProtectCode(buffer, actual_size);
  return FUNCTION_CAST<MemCopyUint8Function>(buffer);
#endif
}
#endif

558
UnaryMathFunctionWithIsolate CreateSqrtFunction(Isolate* isolate) {
559
#if defined(USE_SIMULATOR)
560
  return nullptr;
561 562 563 564
#else
  size_t actual_size;
  byte* buffer =
      static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true));
565
  if (buffer == nullptr) return nullptr;
566

567
  MacroAssembler masm(isolate, buffer, static_cast<int>(actual_size),
568
                      CodeObjectRequired::kNo);
569 570 571 572 573 574 575 576

  __ MovFromFloatParameter(f12);
  __ sqrt_d(f0, f12);
  __ MovToFloatResult(f0);
  __ Ret();

  CodeDesc desc;
  masm.GetCode(&desc);
577
  DCHECK(!RelocInfo::RequiresRelocation(desc));
578

579
  Assembler::FlushICache(isolate, buffer, actual_size);
580
  base::OS::ProtectCode(buffer, actual_size);
581
  return FUNCTION_CAST<UnaryMathFunctionWithIsolate>(buffer);
582 583 584 585 586 587 588 589 590 591 592
#endif
}

#undef __


// -------------------------------------------------------------------------
// Platform-specific RuntimeCallHelper functions.

void StubRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const {
  masm->EnterFrame(StackFrame::INTERNAL);
593
  DCHECK(!masm->has_frame());
594 595 596 597 598 599
  masm->set_has_frame(true);
}


void StubRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
  masm->LeaveFrame(StackFrame::INTERNAL);
600
  DCHECK(masm->has_frame());
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
  masm->set_has_frame(false);
}


// -------------------------------------------------------------------------
// Code generators

#define __ ACCESS_MASM(masm)

void StringCharLoadGenerator::Generate(MacroAssembler* masm,
                                       Register string,
                                       Register index,
                                       Register result,
                                       Label* call_runtime) {
  // Fetch the instance type of the receiver into result register.
  __ ld(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;
  __ ld(result, FieldMemOperand(string, SlicedString::kOffsetOffset));
  __ ld(string, FieldMemOperand(string, SlicedString::kParentOffset));
  __ dsra32(at, result, 0);
  __ Daddu(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);
  __ ld(result, FieldMemOperand(string, ConsString::kSecondOffset));
  __ LoadRoot(at, Heap::kempty_stringRootIndex);
  __ Branch(call_runtime, ne, result, Operand(at));
  // Get the first of the two strings and load its instance type.
  __ ld(string, FieldMemOperand(string, ConsString::kFirstOffset));

  __ bind(&indirect_string_loaded);
  __ ld(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
  STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
  __ Daddu(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));
    __ Assert(eq, kExternalStringExpectedButNotFound,
        at, Operand(zero_reg));
  }
  // Rule out short external strings.
  STATIC_ASSERT(kShortExternalStringTag != 0);
  __ And(at, result, Operand(kShortExternalStringMask));
  __ Branch(call_runtime, ne, at, Operand(zero_reg));
  __ ld(string, FieldMemOperand(string, ExternalString::kResourceDataOffset));

684
  Label one_byte, done;
685 686 687
  __ bind(&check_encoding);
  STATIC_ASSERT(kTwoByteStringTag == 0);
  __ And(at, result, Operand(kStringEncodingMask));
688
  __ Branch(&one_byte, ne, at, Operand(zero_reg));
689
  // Two-byte string.
690
  __ Dlsa(at, string, index, 1);
691 692
  __ lhu(result, MemOperand(at));
  __ jmp(&done);
693 694
  __ bind(&one_byte);
  // One_byte string.
695 696 697 698 699 700 701 702 703 704 705
  __ Daddu(at, string, index);
  __ lbu(result, MemOperand(at));
  __ bind(&done);
}

#ifdef DEBUG
// nop(CODE_AGE_MARKER_NOP)
static const uint32_t kCodeAgePatchFirstInstruction = 0x00010180;
#endif


706 707
CodeAgingHelper::CodeAgingHelper(Isolate* isolate) {
  USE(isolate);
708
  DCHECK(young_sequence_.length() == kNoCodeAgeSequenceLength);
709 710 711 712
  // 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.
713
  std::unique_ptr<CodePatcher> patcher(
714 715 716
      new CodePatcher(isolate, young_sequence_.start(),
                      young_sequence_.length() / Assembler::kInstrSize,
                      CodePatcher::DONT_FLUSH));
717
  PredictableCodeSizeScope scope(patcher->masm(), young_sequence_.length());
718
  patcher->masm()->PushStandardFrame(a1);
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
  patcher->masm()->nop(Assembler::CODE_AGE_SEQUENCE_NOP);
  patcher->masm()->nop(Assembler::CODE_AGE_SEQUENCE_NOP);
  patcher->masm()->nop(Assembler::CODE_AGE_SEQUENCE_NOP);
}


#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);
734
  DCHECK(result || isolate->code_aging_helper()->IsOld(sequence));
735 736 737
  return result;
}

738 739
Code::Age Code::GetCodeAge(Isolate* isolate, byte* sequence) {
  if (IsYoungSequence(isolate, sequence)) return kNoAgeCodeAge;
740

741 742 743 744
  Address target_address =
      Assembler::target_address_at(sequence + Assembler::kInstrSize);
  Code* stub = GetCodeFromTargetAddress(target_address);
  return GetAgeOfCodeAgeStub(stub);
745 746
}

747 748
void Code::PatchPlatformCodeAge(Isolate* isolate, byte* sequence,
                                Code::Age age) {
749 750 751
  uint32_t young_length = isolate->code_aging_helper()->young_sequence_length();
  if (age == kNoAgeCodeAge) {
    isolate->code_aging_helper()->CopyYoungSequenceTo(sequence);
752
    Assembler::FlushICache(isolate, sequence, young_length);
753
  } else {
754
    Code* stub = GetCodeAgeStub(isolate, age);
755 756
    CodePatcher patcher(isolate, sequence,
                        young_length / Assembler::kInstrSize);
757 758 759
    // Mark this code sequence for FindPlatformCodeAgeSequence().
    patcher.masm()->nop(Assembler::CODE_AGE_MARKER_NOP);
    // Load the stub address to t9 and call it,
760
    // GetCodeAge() extracts the stub address from this instruction.
761 762 763 764 765 766 767 768 769 770 771 772 773 774
    patcher.masm()->li(
        t9,
        Operand(reinterpret_cast<uint64_t>(stub->instruction_start())),
        ADDRESS_LOAD);
    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.
  }
}


#undef __

775 776
}  // namespace internal
}  // namespace v8
777 778

#endif  // V8_TARGET_ARCH_MIPS64