11cb0ef41Sopenharmony_ci// Copyright 2011 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/execution/mips/simulator-mips.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// Only build the simulator if not compiling for real MIPS hardware.
81cb0ef41Sopenharmony_ci#if defined(USE_SIMULATOR)
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include <limits.h>
111cb0ef41Sopenharmony_ci#include <stdarg.h>
121cb0ef41Sopenharmony_ci#include <stdlib.h>
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci#include <cmath>
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci#include "src/base/bits.h"
171cb0ef41Sopenharmony_ci#include "src/base/lazy-instance.h"
181cb0ef41Sopenharmony_ci#include "src/base/platform/platform.h"
191cb0ef41Sopenharmony_ci#include "src/base/platform/wrappers.h"
201cb0ef41Sopenharmony_ci#include "src/base/vector.h"
211cb0ef41Sopenharmony_ci#include "src/codegen/assembler-inl.h"
221cb0ef41Sopenharmony_ci#include "src/codegen/macro-assembler.h"
231cb0ef41Sopenharmony_ci#include "src/codegen/mips/constants-mips.h"
241cb0ef41Sopenharmony_ci#include "src/diagnostics/disasm.h"
251cb0ef41Sopenharmony_ci#include "src/heap/combined-heap.h"
261cb0ef41Sopenharmony_ci#include "src/runtime/runtime-utils.h"
271cb0ef41Sopenharmony_ci#include "src/utils/ostreams.h"
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_cinamespace v8 {
301cb0ef41Sopenharmony_cinamespace internal {
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciDEFINE_LAZY_LEAKY_OBJECT_GETTER(Simulator::GlobalMonitor,
331cb0ef41Sopenharmony_ci                                Simulator::GlobalMonitor::Get)
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci// Utils functions.
361cb0ef41Sopenharmony_cibool HaveSameSign(int32_t a, int32_t b) { return ((a ^ b) >= 0); }
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciuint32_t get_fcsr_condition_bit(uint32_t cc) {
391cb0ef41Sopenharmony_ci  if (cc == 0) {
401cb0ef41Sopenharmony_ci    return 23;
411cb0ef41Sopenharmony_ci  } else {
421cb0ef41Sopenharmony_ci    return 24 + cc;
431cb0ef41Sopenharmony_ci  }
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci// This macro provides a platform independent use of sscanf. The reason for
471cb0ef41Sopenharmony_ci// SScanF not being implemented in a platform independent was through
481cb0ef41Sopenharmony_ci// ::v8::internal::OS in the same way as SNPrintF is that the Windows C Run-Time
491cb0ef41Sopenharmony_ci// Library does not provide vsscanf.
501cb0ef41Sopenharmony_ci#define SScanF sscanf
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci// The MipsDebugger class is used by the simulator while debugging simulated
531cb0ef41Sopenharmony_ci// code.
541cb0ef41Sopenharmony_ciclass MipsDebugger {
551cb0ef41Sopenharmony_ci public:
561cb0ef41Sopenharmony_ci  explicit MipsDebugger(Simulator* sim) : sim_(sim) {}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  void Stop(Instruction* instr);
591cb0ef41Sopenharmony_ci  void Debug();
601cb0ef41Sopenharmony_ci  // Print all registers with a nice formatting.
611cb0ef41Sopenharmony_ci  void PrintAllRegs();
621cb0ef41Sopenharmony_ci  void PrintAllRegsIncludingFPU();
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci private:
651cb0ef41Sopenharmony_ci  // We set the breakpoint code to 0xFFFFF to easily recognize it.
661cb0ef41Sopenharmony_ci  static const Instr kBreakpointInstr = SPECIAL | BREAK | 0xFFFFF << 6;
671cb0ef41Sopenharmony_ci  static const Instr kNopInstr = 0x0;
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  Simulator* sim_;
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  int32_t GetRegisterValue(int regnum);
721cb0ef41Sopenharmony_ci  int32_t GetFPURegisterValue32(int regnum);
731cb0ef41Sopenharmony_ci  int64_t GetFPURegisterValue64(int regnum);
741cb0ef41Sopenharmony_ci  float GetFPURegisterValueFloat(int regnum);
751cb0ef41Sopenharmony_ci  double GetFPURegisterValueDouble(int regnum);
761cb0ef41Sopenharmony_ci  bool GetValue(const char* desc, int32_t* value);
771cb0ef41Sopenharmony_ci  bool GetValue(const char* desc, int64_t* value);
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  // Set or delete a breakpoint. Returns true if successful.
801cb0ef41Sopenharmony_ci  bool SetBreakpoint(Instruction* breakpc);
811cb0ef41Sopenharmony_ci  bool DeleteBreakpoint(Instruction* breakpc);
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  // Undo and redo all breakpoints. This is needed to bracket disassembly and
841cb0ef41Sopenharmony_ci  // execution to skip past breakpoints when run from the debugger.
851cb0ef41Sopenharmony_ci  void UndoBreakpoints();
861cb0ef41Sopenharmony_ci  void RedoBreakpoints();
871cb0ef41Sopenharmony_ci};
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci#define UNSUPPORTED() printf("Sim: Unsupported instruction.\n");
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_civoid MipsDebugger::Stop(Instruction* instr) {
921cb0ef41Sopenharmony_ci  // Get the stop code.
931cb0ef41Sopenharmony_ci  uint32_t code = instr->Bits(25, 6);
941cb0ef41Sopenharmony_ci  PrintF("Simulator hit (%u)\n", code);
951cb0ef41Sopenharmony_ci  Debug();
961cb0ef41Sopenharmony_ci}
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ciint32_t MipsDebugger::GetRegisterValue(int regnum) {
991cb0ef41Sopenharmony_ci  if (regnum == kNumSimuRegisters) {
1001cb0ef41Sopenharmony_ci    return sim_->get_pc();
1011cb0ef41Sopenharmony_ci  } else {
1021cb0ef41Sopenharmony_ci    return sim_->get_register(regnum);
1031cb0ef41Sopenharmony_ci  }
1041cb0ef41Sopenharmony_ci}
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ciint32_t MipsDebugger::GetFPURegisterValue32(int regnum) {
1071cb0ef41Sopenharmony_ci  if (regnum == kNumFPURegisters) {
1081cb0ef41Sopenharmony_ci    return sim_->get_pc();
1091cb0ef41Sopenharmony_ci  } else {
1101cb0ef41Sopenharmony_ci    return sim_->get_fpu_register_word(regnum);
1111cb0ef41Sopenharmony_ci  }
1121cb0ef41Sopenharmony_ci}
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ciint64_t MipsDebugger::GetFPURegisterValue64(int regnum) {
1151cb0ef41Sopenharmony_ci  if (regnum == kNumFPURegisters) {
1161cb0ef41Sopenharmony_ci    return sim_->get_pc();
1171cb0ef41Sopenharmony_ci  } else {
1181cb0ef41Sopenharmony_ci    return sim_->get_fpu_register(regnum);
1191cb0ef41Sopenharmony_ci  }
1201cb0ef41Sopenharmony_ci}
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_cifloat MipsDebugger::GetFPURegisterValueFloat(int regnum) {
1231cb0ef41Sopenharmony_ci  if (regnum == kNumFPURegisters) {
1241cb0ef41Sopenharmony_ci    return sim_->get_pc();
1251cb0ef41Sopenharmony_ci  } else {
1261cb0ef41Sopenharmony_ci    return sim_->get_fpu_register_float(regnum);
1271cb0ef41Sopenharmony_ci  }
1281cb0ef41Sopenharmony_ci}
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_cidouble MipsDebugger::GetFPURegisterValueDouble(int regnum) {
1311cb0ef41Sopenharmony_ci  if (regnum == kNumFPURegisters) {
1321cb0ef41Sopenharmony_ci    return sim_->get_pc();
1331cb0ef41Sopenharmony_ci  } else {
1341cb0ef41Sopenharmony_ci    return sim_->get_fpu_register_double(regnum);
1351cb0ef41Sopenharmony_ci  }
1361cb0ef41Sopenharmony_ci}
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_cibool MipsDebugger::GetValue(const char* desc, int32_t* value) {
1391cb0ef41Sopenharmony_ci  int regnum = Registers::Number(desc);
1401cb0ef41Sopenharmony_ci  int fpuregnum = FPURegisters::Number(desc);
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci  if (regnum != kInvalidRegister) {
1431cb0ef41Sopenharmony_ci    *value = GetRegisterValue(regnum);
1441cb0ef41Sopenharmony_ci    return true;
1451cb0ef41Sopenharmony_ci  } else if (fpuregnum != kInvalidFPURegister) {
1461cb0ef41Sopenharmony_ci    *value = GetFPURegisterValue32(fpuregnum);
1471cb0ef41Sopenharmony_ci    return true;
1481cb0ef41Sopenharmony_ci  } else if (strncmp(desc, "0x", 2) == 0) {
1491cb0ef41Sopenharmony_ci    return SScanF(desc, "%x", reinterpret_cast<uint32_t*>(value)) == 1;
1501cb0ef41Sopenharmony_ci  } else {
1511cb0ef41Sopenharmony_ci    return SScanF(desc, "%i", value) == 1;
1521cb0ef41Sopenharmony_ci  }
1531cb0ef41Sopenharmony_ci}
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_cibool MipsDebugger::GetValue(const char* desc, int64_t* value) {
1561cb0ef41Sopenharmony_ci  int regnum = Registers::Number(desc);
1571cb0ef41Sopenharmony_ci  int fpuregnum = FPURegisters::Number(desc);
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci  if (regnum != kInvalidRegister) {
1601cb0ef41Sopenharmony_ci    *value = GetRegisterValue(regnum);
1611cb0ef41Sopenharmony_ci    return true;
1621cb0ef41Sopenharmony_ci  } else if (fpuregnum != kInvalidFPURegister) {
1631cb0ef41Sopenharmony_ci    *value = GetFPURegisterValue64(fpuregnum);
1641cb0ef41Sopenharmony_ci    return true;
1651cb0ef41Sopenharmony_ci  } else if (strncmp(desc, "0x", 2) == 0) {
1661cb0ef41Sopenharmony_ci    return SScanF(desc + 2, "%" SCNx64, reinterpret_cast<uint64_t*>(value)) ==
1671cb0ef41Sopenharmony_ci           1;
1681cb0ef41Sopenharmony_ci  } else {
1691cb0ef41Sopenharmony_ci    return SScanF(desc, "%" SCNu64, reinterpret_cast<uint64_t*>(value)) == 1;
1701cb0ef41Sopenharmony_ci  }
1711cb0ef41Sopenharmony_ci}
1721cb0ef41Sopenharmony_ci
1731cb0ef41Sopenharmony_cibool MipsDebugger::SetBreakpoint(Instruction* breakpc) {
1741cb0ef41Sopenharmony_ci  // Check if a breakpoint can be set. If not return without any side-effects.
1751cb0ef41Sopenharmony_ci  if (sim_->break_pc_ != nullptr) {
1761cb0ef41Sopenharmony_ci    return false;
1771cb0ef41Sopenharmony_ci  }
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci  // Set the breakpoint.
1801cb0ef41Sopenharmony_ci  sim_->break_pc_ = breakpc;
1811cb0ef41Sopenharmony_ci  sim_->break_instr_ = breakpc->InstructionBits();
1821cb0ef41Sopenharmony_ci  // Not setting the breakpoint instruction in the code itself. It will be set
1831cb0ef41Sopenharmony_ci  // when the debugger shell continues.
1841cb0ef41Sopenharmony_ci  return true;
1851cb0ef41Sopenharmony_ci}
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_cibool MipsDebugger::DeleteBreakpoint(Instruction* breakpc) {
1881cb0ef41Sopenharmony_ci  if (sim_->break_pc_ != nullptr) {
1891cb0ef41Sopenharmony_ci    sim_->break_pc_->SetInstructionBits(sim_->break_instr_);
1901cb0ef41Sopenharmony_ci  }
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci  sim_->break_pc_ = nullptr;
1931cb0ef41Sopenharmony_ci  sim_->break_instr_ = 0;
1941cb0ef41Sopenharmony_ci  return true;
1951cb0ef41Sopenharmony_ci}
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_civoid MipsDebugger::UndoBreakpoints() {
1981cb0ef41Sopenharmony_ci  if (sim_->break_pc_ != nullptr) {
1991cb0ef41Sopenharmony_ci    sim_->break_pc_->SetInstructionBits(sim_->break_instr_);
2001cb0ef41Sopenharmony_ci  }
2011cb0ef41Sopenharmony_ci}
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_civoid MipsDebugger::RedoBreakpoints() {
2041cb0ef41Sopenharmony_ci  if (sim_->break_pc_ != nullptr) {
2051cb0ef41Sopenharmony_ci    sim_->break_pc_->SetInstructionBits(kBreakpointInstr);
2061cb0ef41Sopenharmony_ci  }
2071cb0ef41Sopenharmony_ci}
2081cb0ef41Sopenharmony_ci
2091cb0ef41Sopenharmony_civoid MipsDebugger::PrintAllRegs() {
2101cb0ef41Sopenharmony_ci#define REG_INFO(n) Registers::Name(n), GetRegisterValue(n), GetRegisterValue(n)
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ci  PrintF("\n");
2131cb0ef41Sopenharmony_ci  // at, v0, a0.
2141cb0ef41Sopenharmony_ci  PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", REG_INFO(1),
2151cb0ef41Sopenharmony_ci         REG_INFO(2), REG_INFO(4));
2161cb0ef41Sopenharmony_ci  // v1, a1.
2171cb0ef41Sopenharmony_ci  PrintF("%26s\t%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", "", REG_INFO(3),
2181cb0ef41Sopenharmony_ci         REG_INFO(5));
2191cb0ef41Sopenharmony_ci  // a2.
2201cb0ef41Sopenharmony_ci  PrintF("%26s\t%26s\t%3s: 0x%08x %10d\n", "", "", REG_INFO(6));
2211cb0ef41Sopenharmony_ci  // a3.
2221cb0ef41Sopenharmony_ci  PrintF("%26s\t%26s\t%3s: 0x%08x %10d\n", "", "", REG_INFO(7));
2231cb0ef41Sopenharmony_ci  PrintF("\n");
2241cb0ef41Sopenharmony_ci  // t0-t7, s0-s7
2251cb0ef41Sopenharmony_ci  for (int i = 0; i < 8; i++) {
2261cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", REG_INFO(8 + i),
2271cb0ef41Sopenharmony_ci           REG_INFO(16 + i));
2281cb0ef41Sopenharmony_ci  }
2291cb0ef41Sopenharmony_ci  PrintF("\n");
2301cb0ef41Sopenharmony_ci  // t8, k0, LO.
2311cb0ef41Sopenharmony_ci  PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", REG_INFO(24),
2321cb0ef41Sopenharmony_ci         REG_INFO(26), REG_INFO(32));
2331cb0ef41Sopenharmony_ci  // t9, k1, HI.
2341cb0ef41Sopenharmony_ci  PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", REG_INFO(25),
2351cb0ef41Sopenharmony_ci         REG_INFO(27), REG_INFO(33));
2361cb0ef41Sopenharmony_ci  // sp, fp, gp.
2371cb0ef41Sopenharmony_ci  PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", REG_INFO(29),
2381cb0ef41Sopenharmony_ci         REG_INFO(30), REG_INFO(28));
2391cb0ef41Sopenharmony_ci  // pc.
2401cb0ef41Sopenharmony_ci  PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", REG_INFO(31), REG_INFO(34));
2411cb0ef41Sopenharmony_ci
2421cb0ef41Sopenharmony_ci#undef REG_INFO
2431cb0ef41Sopenharmony_ci}
2441cb0ef41Sopenharmony_ci
2451cb0ef41Sopenharmony_civoid MipsDebugger::PrintAllRegsIncludingFPU() {
2461cb0ef41Sopenharmony_ci#define FPU_REG_INFO32(n)                                     \
2471cb0ef41Sopenharmony_ci  FPURegisters::Name(n), FPURegisters::Name(n + 1),           \
2481cb0ef41Sopenharmony_ci      GetFPURegisterValue32(n + 1), GetFPURegisterValue32(n), \
2491cb0ef41Sopenharmony_ci      GetFPURegisterValueDouble(n)
2501cb0ef41Sopenharmony_ci
2511cb0ef41Sopenharmony_ci#define FPU_REG_INFO64(n) \
2521cb0ef41Sopenharmony_ci  FPURegisters::Name(n), GetFPURegisterValue64(n), GetFPURegisterValueDouble(n)
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ci  PrintAllRegs();
2551cb0ef41Sopenharmony_ci
2561cb0ef41Sopenharmony_ci  PrintF("\n\n");
2571cb0ef41Sopenharmony_ci  // f0, f1, f2, ... f31.
2581cb0ef41Sopenharmony_ci  // This must be a compile-time switch,
2591cb0ef41Sopenharmony_ci  // compiler will throw out warnings otherwise.
2601cb0ef41Sopenharmony_ci  if (kFpuMode == kFP64) {
2611cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(0));
2621cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(1));
2631cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(2));
2641cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(3));
2651cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(4));
2661cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(5));
2671cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(6));
2681cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(7));
2691cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(8));
2701cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(9));
2711cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(10));
2721cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(11));
2731cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(12));
2741cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(13));
2751cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(14));
2761cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(15));
2771cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(16));
2781cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(17));
2791cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(18));
2801cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(19));
2811cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(20));
2821cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(21));
2831cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(22));
2841cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(23));
2851cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(24));
2861cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(25));
2871cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(26));
2881cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(27));
2891cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(28));
2901cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(29));
2911cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(30));
2921cb0ef41Sopenharmony_ci    PrintF("%3s: 0x%016llx %16.4e\n", FPU_REG_INFO64(31));
2931cb0ef41Sopenharmony_ci  } else {
2941cb0ef41Sopenharmony_ci    PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO32(0));
2951cb0ef41Sopenharmony_ci    PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO32(2));
2961cb0ef41Sopenharmony_ci    PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO32(4));
2971cb0ef41Sopenharmony_ci    PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO32(6));
2981cb0ef41Sopenharmony_ci    PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO32(8));
2991cb0ef41Sopenharmony_ci    PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO32(10));
3001cb0ef41Sopenharmony_ci    PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO32(12));
3011cb0ef41Sopenharmony_ci    PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO32(14));
3021cb0ef41Sopenharmony_ci    PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO32(16));
3031cb0ef41Sopenharmony_ci    PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO32(18));
3041cb0ef41Sopenharmony_ci    PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO32(20));
3051cb0ef41Sopenharmony_ci    PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO32(22));
3061cb0ef41Sopenharmony_ci    PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO32(24));
3071cb0ef41Sopenharmony_ci    PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO32(26));
3081cb0ef41Sopenharmony_ci    PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO32(28));
3091cb0ef41Sopenharmony_ci    PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO32(30));
3101cb0ef41Sopenharmony_ci  }
3111cb0ef41Sopenharmony_ci
3121cb0ef41Sopenharmony_ci#undef FPU_REG_INFO32
3131cb0ef41Sopenharmony_ci#undef FPU_REG_INFO64
3141cb0ef41Sopenharmony_ci}
3151cb0ef41Sopenharmony_ci
3161cb0ef41Sopenharmony_civoid MipsDebugger::Debug() {
3171cb0ef41Sopenharmony_ci  intptr_t last_pc = -1;
3181cb0ef41Sopenharmony_ci  bool done = false;
3191cb0ef41Sopenharmony_ci
3201cb0ef41Sopenharmony_ci#define COMMAND_SIZE 63
3211cb0ef41Sopenharmony_ci#define ARG_SIZE 255
3221cb0ef41Sopenharmony_ci
3231cb0ef41Sopenharmony_ci#define STR(a) #a
3241cb0ef41Sopenharmony_ci#define XSTR(a) STR(a)
3251cb0ef41Sopenharmony_ci
3261cb0ef41Sopenharmony_ci  char cmd[COMMAND_SIZE + 1];
3271cb0ef41Sopenharmony_ci  char arg1[ARG_SIZE + 1];
3281cb0ef41Sopenharmony_ci  char arg2[ARG_SIZE + 1];
3291cb0ef41Sopenharmony_ci  char* argv[3] = {cmd, arg1, arg2};
3301cb0ef41Sopenharmony_ci
3311cb0ef41Sopenharmony_ci  // Make sure to have a proper terminating character if reaching the limit.
3321cb0ef41Sopenharmony_ci  cmd[COMMAND_SIZE] = 0;
3331cb0ef41Sopenharmony_ci  arg1[ARG_SIZE] = 0;
3341cb0ef41Sopenharmony_ci  arg2[ARG_SIZE] = 0;
3351cb0ef41Sopenharmony_ci
3361cb0ef41Sopenharmony_ci  // Undo all set breakpoints while running in the debugger shell. This will
3371cb0ef41Sopenharmony_ci  // make them invisible to all commands.
3381cb0ef41Sopenharmony_ci  UndoBreakpoints();
3391cb0ef41Sopenharmony_ci
3401cb0ef41Sopenharmony_ci  while (!done && (sim_->get_pc() != Simulator::end_sim_pc)) {
3411cb0ef41Sopenharmony_ci    if (last_pc != sim_->get_pc()) {
3421cb0ef41Sopenharmony_ci      disasm::NameConverter converter;
3431cb0ef41Sopenharmony_ci      disasm::Disassembler dasm(converter);
3441cb0ef41Sopenharmony_ci      // Use a reasonably large buffer.
3451cb0ef41Sopenharmony_ci      v8::base::EmbeddedVector<char, 256> buffer;
3461cb0ef41Sopenharmony_ci      dasm.InstructionDecode(buffer, reinterpret_cast<byte*>(sim_->get_pc()));
3471cb0ef41Sopenharmony_ci      PrintF("  0x%08x  %s\n", sim_->get_pc(), buffer.begin());
3481cb0ef41Sopenharmony_ci      last_pc = sim_->get_pc();
3491cb0ef41Sopenharmony_ci    }
3501cb0ef41Sopenharmony_ci    char* line = ReadLine("sim> ");
3511cb0ef41Sopenharmony_ci    if (line == nullptr) {
3521cb0ef41Sopenharmony_ci      break;
3531cb0ef41Sopenharmony_ci    } else {
3541cb0ef41Sopenharmony_ci      char* last_input = sim_->last_debugger_input();
3551cb0ef41Sopenharmony_ci      if (strcmp(line, "\n") == 0 && last_input != nullptr) {
3561cb0ef41Sopenharmony_ci        line = last_input;
3571cb0ef41Sopenharmony_ci      } else {
3581cb0ef41Sopenharmony_ci        // Ownership is transferred to sim_;
3591cb0ef41Sopenharmony_ci        sim_->set_last_debugger_input(line);
3601cb0ef41Sopenharmony_ci      }
3611cb0ef41Sopenharmony_ci      // Use sscanf to parse the individual parts of the command line. At the
3621cb0ef41Sopenharmony_ci      // moment no command expects more than two parameters.
3631cb0ef41Sopenharmony_ci      int argc = SScanF(line,
3641cb0ef41Sopenharmony_ci                        "%" XSTR(COMMAND_SIZE) "s "
3651cb0ef41Sopenharmony_ci                        "%" XSTR(ARG_SIZE) "s "
3661cb0ef41Sopenharmony_ci                        "%" XSTR(ARG_SIZE) "s",
3671cb0ef41Sopenharmony_ci                        cmd, arg1, arg2);
3681cb0ef41Sopenharmony_ci      if ((strcmp(cmd, "si") == 0) || (strcmp(cmd, "stepi") == 0)) {
3691cb0ef41Sopenharmony_ci        Instruction* instr = reinterpret_cast<Instruction*>(sim_->get_pc());
3701cb0ef41Sopenharmony_ci        if (!(instr->IsTrap()) ||
3711cb0ef41Sopenharmony_ci            instr->InstructionBits() == rtCallRedirInstr) {
3721cb0ef41Sopenharmony_ci          sim_->InstructionDecode(
3731cb0ef41Sopenharmony_ci              reinterpret_cast<Instruction*>(sim_->get_pc()));
3741cb0ef41Sopenharmony_ci        } else {
3751cb0ef41Sopenharmony_ci          // Allow si to jump over generated breakpoints.
3761cb0ef41Sopenharmony_ci          PrintF("/!\\ Jumping over generated breakpoint.\n");
3771cb0ef41Sopenharmony_ci          sim_->set_pc(sim_->get_pc() + kInstrSize);
3781cb0ef41Sopenharmony_ci        }
3791cb0ef41Sopenharmony_ci      } else if ((strcmp(cmd, "c") == 0) || (strcmp(cmd, "cont") == 0)) {
3801cb0ef41Sopenharmony_ci        // Execute the one instruction we broke at with breakpoints disabled.
3811cb0ef41Sopenharmony_ci        sim_->InstructionDecode(reinterpret_cast<Instruction*>(sim_->get_pc()));
3821cb0ef41Sopenharmony_ci        // Leave the debugger shell.
3831cb0ef41Sopenharmony_ci        done = true;
3841cb0ef41Sopenharmony_ci      } else if ((strcmp(cmd, "p") == 0) || (strcmp(cmd, "print") == 0)) {
3851cb0ef41Sopenharmony_ci        if (argc == 2) {
3861cb0ef41Sopenharmony_ci          if (strcmp(arg1, "all") == 0) {
3871cb0ef41Sopenharmony_ci            PrintAllRegs();
3881cb0ef41Sopenharmony_ci          } else if (strcmp(arg1, "allf") == 0) {
3891cb0ef41Sopenharmony_ci            PrintAllRegsIncludingFPU();
3901cb0ef41Sopenharmony_ci          } else {
3911cb0ef41Sopenharmony_ci            int regnum = Registers::Number(arg1);
3921cb0ef41Sopenharmony_ci            int fpuregnum = FPURegisters::Number(arg1);
3931cb0ef41Sopenharmony_ci
3941cb0ef41Sopenharmony_ci            if (regnum != kInvalidRegister) {
3951cb0ef41Sopenharmony_ci              int32_t value;
3961cb0ef41Sopenharmony_ci              value = GetRegisterValue(regnum);
3971cb0ef41Sopenharmony_ci              PrintF("%s: 0x%08x %d \n", arg1, value, value);
3981cb0ef41Sopenharmony_ci            } else if (fpuregnum != kInvalidFPURegister) {
3991cb0ef41Sopenharmony_ci              if (IsFp64Mode()) {
4001cb0ef41Sopenharmony_ci                int64_t value;
4011cb0ef41Sopenharmony_ci                double dvalue;
4021cb0ef41Sopenharmony_ci                value = GetFPURegisterValue64(fpuregnum);
4031cb0ef41Sopenharmony_ci                dvalue = GetFPURegisterValueDouble(fpuregnum);
4041cb0ef41Sopenharmony_ci                PrintF("%3s: 0x%016llx %16.4e\n", FPURegisters::Name(fpuregnum),
4051cb0ef41Sopenharmony_ci                       value, dvalue);
4061cb0ef41Sopenharmony_ci              } else {
4071cb0ef41Sopenharmony_ci                if (fpuregnum % 2 == 1) {
4081cb0ef41Sopenharmony_ci                  int32_t value;
4091cb0ef41Sopenharmony_ci                  float fvalue;
4101cb0ef41Sopenharmony_ci                  value = GetFPURegisterValue32(fpuregnum);
4111cb0ef41Sopenharmony_ci                  fvalue = GetFPURegisterValueFloat(fpuregnum);
4121cb0ef41Sopenharmony_ci                  PrintF("%s: 0x%08x %11.4e\n", arg1, value, fvalue);
4131cb0ef41Sopenharmony_ci                } else {
4141cb0ef41Sopenharmony_ci                  double dfvalue;
4151cb0ef41Sopenharmony_ci                  int32_t lvalue1 = GetFPURegisterValue32(fpuregnum);
4161cb0ef41Sopenharmony_ci                  int32_t lvalue2 = GetFPURegisterValue32(fpuregnum + 1);
4171cb0ef41Sopenharmony_ci                  dfvalue = GetFPURegisterValueDouble(fpuregnum);
4181cb0ef41Sopenharmony_ci                  PrintF("%3s,%3s: 0x%08x%08x %16.4e\n",
4191cb0ef41Sopenharmony_ci                         FPURegisters::Name(fpuregnum + 1),
4201cb0ef41Sopenharmony_ci                         FPURegisters::Name(fpuregnum), lvalue1, lvalue2,
4211cb0ef41Sopenharmony_ci                         dfvalue);
4221cb0ef41Sopenharmony_ci                }
4231cb0ef41Sopenharmony_ci              }
4241cb0ef41Sopenharmony_ci            } else {
4251cb0ef41Sopenharmony_ci              PrintF("%s unrecognized\n", arg1);
4261cb0ef41Sopenharmony_ci            }
4271cb0ef41Sopenharmony_ci          }
4281cb0ef41Sopenharmony_ci        } else {
4291cb0ef41Sopenharmony_ci          if (argc == 3) {
4301cb0ef41Sopenharmony_ci            if (strcmp(arg2, "single") == 0) {
4311cb0ef41Sopenharmony_ci              int32_t value;
4321cb0ef41Sopenharmony_ci              float fvalue;
4331cb0ef41Sopenharmony_ci              int fpuregnum = FPURegisters::Number(arg1);
4341cb0ef41Sopenharmony_ci
4351cb0ef41Sopenharmony_ci              if (fpuregnum != kInvalidFPURegister) {
4361cb0ef41Sopenharmony_ci                value = GetFPURegisterValue32(fpuregnum);
4371cb0ef41Sopenharmony_ci                fvalue = GetFPURegisterValueFloat(fpuregnum);
4381cb0ef41Sopenharmony_ci                PrintF("%s: 0x%08x %11.4e\n", arg1, value, fvalue);
4391cb0ef41Sopenharmony_ci              } else {
4401cb0ef41Sopenharmony_ci                PrintF("%s unrecognized\n", arg1);
4411cb0ef41Sopenharmony_ci              }
4421cb0ef41Sopenharmony_ci            } else {
4431cb0ef41Sopenharmony_ci              PrintF("print <fpu register> single\n");
4441cb0ef41Sopenharmony_ci            }
4451cb0ef41Sopenharmony_ci          } else {
4461cb0ef41Sopenharmony_ci            PrintF("print <register> or print <fpu register> single\n");
4471cb0ef41Sopenharmony_ci          }
4481cb0ef41Sopenharmony_ci        }
4491cb0ef41Sopenharmony_ci      } else if ((strcmp(cmd, "po") == 0) ||
4501cb0ef41Sopenharmony_ci                 (strcmp(cmd, "printobject") == 0)) {
4511cb0ef41Sopenharmony_ci        if (argc == 2) {
4521cb0ef41Sopenharmony_ci          int32_t value;
4531cb0ef41Sopenharmony_ci          StdoutStream os;
4541cb0ef41Sopenharmony_ci          if (GetValue(arg1, &value)) {
4551cb0ef41Sopenharmony_ci            Object obj(value);
4561cb0ef41Sopenharmony_ci            os << arg1 << ": \n";
4571cb0ef41Sopenharmony_ci#ifdef DEBUG
4581cb0ef41Sopenharmony_ci            obj.Print(os);
4591cb0ef41Sopenharmony_ci            os << "\n";
4601cb0ef41Sopenharmony_ci#else
4611cb0ef41Sopenharmony_ci            os << Brief(obj) << "\n";
4621cb0ef41Sopenharmony_ci#endif
4631cb0ef41Sopenharmony_ci          } else {
4641cb0ef41Sopenharmony_ci            os << arg1 << " unrecognized\n";
4651cb0ef41Sopenharmony_ci          }
4661cb0ef41Sopenharmony_ci        } else {
4671cb0ef41Sopenharmony_ci          PrintF("printobject <value>\n");
4681cb0ef41Sopenharmony_ci        }
4691cb0ef41Sopenharmony_ci      } else if (strcmp(cmd, "stack") == 0 || strcmp(cmd, "mem") == 0 ||
4701cb0ef41Sopenharmony_ci                 strcmp(cmd, "dump") == 0) {
4711cb0ef41Sopenharmony_ci        int32_t* cur = nullptr;
4721cb0ef41Sopenharmony_ci        int32_t* end = nullptr;
4731cb0ef41Sopenharmony_ci        int next_arg = 1;
4741cb0ef41Sopenharmony_ci
4751cb0ef41Sopenharmony_ci        if (strcmp(cmd, "stack") == 0) {
4761cb0ef41Sopenharmony_ci          cur = reinterpret_cast<int32_t*>(sim_->get_register(Simulator::sp));
4771cb0ef41Sopenharmony_ci        } else {  // Command "mem".
4781cb0ef41Sopenharmony_ci          int32_t value;
4791cb0ef41Sopenharmony_ci          if (!GetValue(arg1, &value)) {
4801cb0ef41Sopenharmony_ci            PrintF("%s unrecognized\n", arg1);
4811cb0ef41Sopenharmony_ci            continue;
4821cb0ef41Sopenharmony_ci          }
4831cb0ef41Sopenharmony_ci          cur = reinterpret_cast<int32_t*>(value);
4841cb0ef41Sopenharmony_ci          next_arg++;
4851cb0ef41Sopenharmony_ci        }
4861cb0ef41Sopenharmony_ci
4871cb0ef41Sopenharmony_ci        // TODO(palfia): optimize this.
4881cb0ef41Sopenharmony_ci        if (IsFp64Mode()) {
4891cb0ef41Sopenharmony_ci          int64_t words;
4901cb0ef41Sopenharmony_ci          if (argc == next_arg) {
4911cb0ef41Sopenharmony_ci            words = 10;
4921cb0ef41Sopenharmony_ci          } else {
4931cb0ef41Sopenharmony_ci            if (!GetValue(argv[next_arg], &words)) {
4941cb0ef41Sopenharmony_ci              words = 10;
4951cb0ef41Sopenharmony_ci            }
4961cb0ef41Sopenharmony_ci          }
4971cb0ef41Sopenharmony_ci          end = cur + words;
4981cb0ef41Sopenharmony_ci        } else {
4991cb0ef41Sopenharmony_ci          int32_t words;
5001cb0ef41Sopenharmony_ci          if (argc == next_arg) {
5011cb0ef41Sopenharmony_ci            words = 10;
5021cb0ef41Sopenharmony_ci          } else {
5031cb0ef41Sopenharmony_ci            if (!GetValue(argv[next_arg], &words)) {
5041cb0ef41Sopenharmony_ci              words = 10;
5051cb0ef41Sopenharmony_ci            }
5061cb0ef41Sopenharmony_ci          }
5071cb0ef41Sopenharmony_ci          end = cur + words;
5081cb0ef41Sopenharmony_ci        }
5091cb0ef41Sopenharmony_ci
5101cb0ef41Sopenharmony_ci        bool skip_obj_print = (strcmp(cmd, "dump") == 0);
5111cb0ef41Sopenharmony_ci        while (cur < end) {
5121cb0ef41Sopenharmony_ci          PrintF("  0x%08" PRIxPTR ":  0x%08x %10d",
5131cb0ef41Sopenharmony_ci                 reinterpret_cast<intptr_t>(cur), *cur, *cur);
5141cb0ef41Sopenharmony_ci          Object obj(*cur);
5151cb0ef41Sopenharmony_ci          Heap* current_heap = sim_->isolate_->heap();
5161cb0ef41Sopenharmony_ci          if (!skip_obj_print) {
5171cb0ef41Sopenharmony_ci            if (obj.IsSmi() ||
5181cb0ef41Sopenharmony_ci                IsValidHeapObject(current_heap, HeapObject::cast(obj))) {
5191cb0ef41Sopenharmony_ci              PrintF(" (");
5201cb0ef41Sopenharmony_ci              if (obj.IsSmi()) {
5211cb0ef41Sopenharmony_ci                PrintF("smi %d", Smi::ToInt(obj));
5221cb0ef41Sopenharmony_ci              } else {
5231cb0ef41Sopenharmony_ci                obj.ShortPrint();
5241cb0ef41Sopenharmony_ci              }
5251cb0ef41Sopenharmony_ci              PrintF(")");
5261cb0ef41Sopenharmony_ci            }
5271cb0ef41Sopenharmony_ci          }
5281cb0ef41Sopenharmony_ci          PrintF("\n");
5291cb0ef41Sopenharmony_ci          cur++;
5301cb0ef41Sopenharmony_ci        }
5311cb0ef41Sopenharmony_ci
5321cb0ef41Sopenharmony_ci      } else if ((strcmp(cmd, "disasm") == 0) || (strcmp(cmd, "dpc") == 0) ||
5331cb0ef41Sopenharmony_ci                 (strcmp(cmd, "di") == 0)) {
5341cb0ef41Sopenharmony_ci        disasm::NameConverter converter;
5351cb0ef41Sopenharmony_ci        disasm::Disassembler dasm(converter);
5361cb0ef41Sopenharmony_ci        // Use a reasonably large buffer.
5371cb0ef41Sopenharmony_ci        v8::base::EmbeddedVector<char, 256> buffer;
5381cb0ef41Sopenharmony_ci
5391cb0ef41Sopenharmony_ci        byte* cur = nullptr;
5401cb0ef41Sopenharmony_ci        byte* end = nullptr;
5411cb0ef41Sopenharmony_ci
5421cb0ef41Sopenharmony_ci        if (argc == 1) {
5431cb0ef41Sopenharmony_ci          cur = reinterpret_cast<byte*>(sim_->get_pc());
5441cb0ef41Sopenharmony_ci          end = cur + (10 * kInstrSize);
5451cb0ef41Sopenharmony_ci        } else if (argc == 2) {
5461cb0ef41Sopenharmony_ci          int regnum = Registers::Number(arg1);
5471cb0ef41Sopenharmony_ci          if (regnum != kInvalidRegister || strncmp(arg1, "0x", 2) == 0) {
5481cb0ef41Sopenharmony_ci            // The argument is an address or a register name.
5491cb0ef41Sopenharmony_ci            int32_t value;
5501cb0ef41Sopenharmony_ci            if (GetValue(arg1, &value)) {
5511cb0ef41Sopenharmony_ci              cur = reinterpret_cast<byte*>(value);
5521cb0ef41Sopenharmony_ci              // Disassemble 10 instructions at <arg1>.
5531cb0ef41Sopenharmony_ci              end = cur + (10 * kInstrSize);
5541cb0ef41Sopenharmony_ci            }
5551cb0ef41Sopenharmony_ci          } else {
5561cb0ef41Sopenharmony_ci            // The argument is the number of instructions.
5571cb0ef41Sopenharmony_ci            int32_t value;
5581cb0ef41Sopenharmony_ci            if (GetValue(arg1, &value)) {
5591cb0ef41Sopenharmony_ci              cur = reinterpret_cast<byte*>(sim_->get_pc());
5601cb0ef41Sopenharmony_ci              // Disassemble <arg1> instructions.
5611cb0ef41Sopenharmony_ci              end = cur + (value * kInstrSize);
5621cb0ef41Sopenharmony_ci            }
5631cb0ef41Sopenharmony_ci          }
5641cb0ef41Sopenharmony_ci        } else {
5651cb0ef41Sopenharmony_ci          int32_t value1;
5661cb0ef41Sopenharmony_ci          int32_t value2;
5671cb0ef41Sopenharmony_ci          if (GetValue(arg1, &value1) && GetValue(arg2, &value2)) {
5681cb0ef41Sopenharmony_ci            cur = reinterpret_cast<byte*>(value1);
5691cb0ef41Sopenharmony_ci            end = cur + (value2 * kInstrSize);
5701cb0ef41Sopenharmony_ci          }
5711cb0ef41Sopenharmony_ci        }
5721cb0ef41Sopenharmony_ci
5731cb0ef41Sopenharmony_ci        while (cur < end) {
5741cb0ef41Sopenharmony_ci          dasm.InstructionDecode(buffer, cur);
5751cb0ef41Sopenharmony_ci          PrintF("  0x%08" PRIxPTR "  %s\n", reinterpret_cast<intptr_t>(cur),
5761cb0ef41Sopenharmony_ci                 buffer.begin());
5771cb0ef41Sopenharmony_ci          cur += kInstrSize;
5781cb0ef41Sopenharmony_ci        }
5791cb0ef41Sopenharmony_ci      } else if (strcmp(cmd, "gdb") == 0) {
5801cb0ef41Sopenharmony_ci        PrintF("relinquishing control to gdb\n");
5811cb0ef41Sopenharmony_ci        v8::base::OS::DebugBreak();
5821cb0ef41Sopenharmony_ci        PrintF("regaining control from gdb\n");
5831cb0ef41Sopenharmony_ci      } else if (strcmp(cmd, "break") == 0) {
5841cb0ef41Sopenharmony_ci        if (argc == 2) {
5851cb0ef41Sopenharmony_ci          int32_t value;
5861cb0ef41Sopenharmony_ci          if (GetValue(arg1, &value)) {
5871cb0ef41Sopenharmony_ci            if (!SetBreakpoint(reinterpret_cast<Instruction*>(value))) {
5881cb0ef41Sopenharmony_ci              PrintF("setting breakpoint failed\n");
5891cb0ef41Sopenharmony_ci            }
5901cb0ef41Sopenharmony_ci          } else {
5911cb0ef41Sopenharmony_ci            PrintF("%s unrecognized\n", arg1);
5921cb0ef41Sopenharmony_ci          }
5931cb0ef41Sopenharmony_ci        } else {
5941cb0ef41Sopenharmony_ci          PrintF("break <address>\n");
5951cb0ef41Sopenharmony_ci        }
5961cb0ef41Sopenharmony_ci      } else if (strcmp(cmd, "del") == 0) {
5971cb0ef41Sopenharmony_ci        if (!DeleteBreakpoint(nullptr)) {
5981cb0ef41Sopenharmony_ci          PrintF("deleting breakpoint failed\n");
5991cb0ef41Sopenharmony_ci        }
6001cb0ef41Sopenharmony_ci      } else if (strcmp(cmd, "flags") == 0) {
6011cb0ef41Sopenharmony_ci        PrintF("No flags on MIPS !\n");
6021cb0ef41Sopenharmony_ci      } else if (strcmp(cmd, "stop") == 0) {
6031cb0ef41Sopenharmony_ci        int32_t value;
6041cb0ef41Sopenharmony_ci        intptr_t stop_pc = sim_->get_pc() - 2 * kInstrSize;
6051cb0ef41Sopenharmony_ci        Instruction* stop_instr = reinterpret_cast<Instruction*>(stop_pc);
6061cb0ef41Sopenharmony_ci        Instruction* msg_address =
6071cb0ef41Sopenharmony_ci            reinterpret_cast<Instruction*>(stop_pc + kInstrSize);
6081cb0ef41Sopenharmony_ci        if ((argc == 2) && (strcmp(arg1, "unstop") == 0)) {
6091cb0ef41Sopenharmony_ci          // Remove the current stop.
6101cb0ef41Sopenharmony_ci          if (sim_->IsStopInstruction(stop_instr)) {
6111cb0ef41Sopenharmony_ci            stop_instr->SetInstructionBits(kNopInstr);
6121cb0ef41Sopenharmony_ci            msg_address->SetInstructionBits(kNopInstr);
6131cb0ef41Sopenharmony_ci          } else {
6141cb0ef41Sopenharmony_ci            PrintF("Not at debugger stop.\n");
6151cb0ef41Sopenharmony_ci          }
6161cb0ef41Sopenharmony_ci        } else if (argc == 3) {
6171cb0ef41Sopenharmony_ci          // Print information about all/the specified breakpoint(s).
6181cb0ef41Sopenharmony_ci          if (strcmp(arg1, "info") == 0) {
6191cb0ef41Sopenharmony_ci            if (strcmp(arg2, "all") == 0) {
6201cb0ef41Sopenharmony_ci              PrintF("Stop information:\n");
6211cb0ef41Sopenharmony_ci              for (uint32_t i = kMaxWatchpointCode + 1; i <= kMaxStopCode;
6221cb0ef41Sopenharmony_ci                   i++) {
6231cb0ef41Sopenharmony_ci                sim_->PrintStopInfo(i);
6241cb0ef41Sopenharmony_ci              }
6251cb0ef41Sopenharmony_ci            } else if (GetValue(arg2, &value)) {
6261cb0ef41Sopenharmony_ci              sim_->PrintStopInfo(value);
6271cb0ef41Sopenharmony_ci            } else {
6281cb0ef41Sopenharmony_ci              PrintF("Unrecognized argument.\n");
6291cb0ef41Sopenharmony_ci            }
6301cb0ef41Sopenharmony_ci          } else if (strcmp(arg1, "enable") == 0) {
6311cb0ef41Sopenharmony_ci            // Enable all/the specified breakpoint(s).
6321cb0ef41Sopenharmony_ci            if (strcmp(arg2, "all") == 0) {
6331cb0ef41Sopenharmony_ci              for (uint32_t i = kMaxWatchpointCode + 1; i <= kMaxStopCode;
6341cb0ef41Sopenharmony_ci                   i++) {
6351cb0ef41Sopenharmony_ci                sim_->EnableStop(i);
6361cb0ef41Sopenharmony_ci              }
6371cb0ef41Sopenharmony_ci            } else if (GetValue(arg2, &value)) {
6381cb0ef41Sopenharmony_ci              sim_->EnableStop(value);
6391cb0ef41Sopenharmony_ci            } else {
6401cb0ef41Sopenharmony_ci              PrintF("Unrecognized argument.\n");
6411cb0ef41Sopenharmony_ci            }
6421cb0ef41Sopenharmony_ci          } else if (strcmp(arg1, "disable") == 0) {
6431cb0ef41Sopenharmony_ci            // Disable all/the specified breakpoint(s).
6441cb0ef41Sopenharmony_ci            if (strcmp(arg2, "all") == 0) {
6451cb0ef41Sopenharmony_ci              for (uint32_t i = kMaxWatchpointCode + 1; i <= kMaxStopCode;
6461cb0ef41Sopenharmony_ci                   i++) {
6471cb0ef41Sopenharmony_ci                sim_->DisableStop(i);
6481cb0ef41Sopenharmony_ci              }
6491cb0ef41Sopenharmony_ci            } else if (GetValue(arg2, &value)) {
6501cb0ef41Sopenharmony_ci              sim_->DisableStop(value);
6511cb0ef41Sopenharmony_ci            } else {
6521cb0ef41Sopenharmony_ci              PrintF("Unrecognized argument.\n");
6531cb0ef41Sopenharmony_ci            }
6541cb0ef41Sopenharmony_ci          }
6551cb0ef41Sopenharmony_ci        } else {
6561cb0ef41Sopenharmony_ci          PrintF("Wrong usage. Use help command for more information.\n");
6571cb0ef41Sopenharmony_ci        }
6581cb0ef41Sopenharmony_ci      } else if ((strcmp(cmd, "stat") == 0) || (strcmp(cmd, "st") == 0)) {
6591cb0ef41Sopenharmony_ci        // Print registers and disassemble.
6601cb0ef41Sopenharmony_ci        PrintAllRegs();
6611cb0ef41Sopenharmony_ci        PrintF("\n");
6621cb0ef41Sopenharmony_ci
6631cb0ef41Sopenharmony_ci        disasm::NameConverter converter;
6641cb0ef41Sopenharmony_ci        disasm::Disassembler dasm(converter);
6651cb0ef41Sopenharmony_ci        // Use a reasonably large buffer.
6661cb0ef41Sopenharmony_ci        v8::base::EmbeddedVector<char, 256> buffer;
6671cb0ef41Sopenharmony_ci
6681cb0ef41Sopenharmony_ci        byte* cur = nullptr;
6691cb0ef41Sopenharmony_ci        byte* end = nullptr;
6701cb0ef41Sopenharmony_ci
6711cb0ef41Sopenharmony_ci        if (argc == 1) {
6721cb0ef41Sopenharmony_ci          cur = reinterpret_cast<byte*>(sim_->get_pc());
6731cb0ef41Sopenharmony_ci          end = cur + (10 * kInstrSize);
6741cb0ef41Sopenharmony_ci        } else if (argc == 2) {
6751cb0ef41Sopenharmony_ci          int32_t value;
6761cb0ef41Sopenharmony_ci          if (GetValue(arg1, &value)) {
6771cb0ef41Sopenharmony_ci            cur = reinterpret_cast<byte*>(value);
6781cb0ef41Sopenharmony_ci            // no length parameter passed, assume 10 instructions
6791cb0ef41Sopenharmony_ci            end = cur + (10 * kInstrSize);
6801cb0ef41Sopenharmony_ci          }
6811cb0ef41Sopenharmony_ci        } else {
6821cb0ef41Sopenharmony_ci          int32_t value1;
6831cb0ef41Sopenharmony_ci          int32_t value2;
6841cb0ef41Sopenharmony_ci          if (GetValue(arg1, &value1) && GetValue(arg2, &value2)) {
6851cb0ef41Sopenharmony_ci            cur = reinterpret_cast<byte*>(value1);
6861cb0ef41Sopenharmony_ci            end = cur + (value2 * kInstrSize);
6871cb0ef41Sopenharmony_ci          }
6881cb0ef41Sopenharmony_ci        }
6891cb0ef41Sopenharmony_ci
6901cb0ef41Sopenharmony_ci        while (cur < end) {
6911cb0ef41Sopenharmony_ci          dasm.InstructionDecode(buffer, cur);
6921cb0ef41Sopenharmony_ci          PrintF("  0x%08" PRIxPTR "  %s\n", reinterpret_cast<intptr_t>(cur),
6931cb0ef41Sopenharmony_ci                 buffer.begin());
6941cb0ef41Sopenharmony_ci          cur += kInstrSize;
6951cb0ef41Sopenharmony_ci        }
6961cb0ef41Sopenharmony_ci      } else if ((strcmp(cmd, "h") == 0) || (strcmp(cmd, "help") == 0)) {
6971cb0ef41Sopenharmony_ci        PrintF("cont\n");
6981cb0ef41Sopenharmony_ci        PrintF("  continue execution (alias 'c')\n");
6991cb0ef41Sopenharmony_ci        PrintF("stepi\n");
7001cb0ef41Sopenharmony_ci        PrintF("  step one instruction (alias 'si')\n");
7011cb0ef41Sopenharmony_ci        PrintF("print <register>\n");
7021cb0ef41Sopenharmony_ci        PrintF("  print register content (alias 'p')\n");
7031cb0ef41Sopenharmony_ci        PrintF("  use register name 'all' to print all registers\n");
7041cb0ef41Sopenharmony_ci        PrintF("printobject <register>\n");
7051cb0ef41Sopenharmony_ci        PrintF("  print an object from a register (alias 'po')\n");
7061cb0ef41Sopenharmony_ci        PrintF("stack [<words>]\n");
7071cb0ef41Sopenharmony_ci        PrintF("  dump stack content, default dump 10 words)\n");
7081cb0ef41Sopenharmony_ci        PrintF("mem <address> [<words>]\n");
7091cb0ef41Sopenharmony_ci        PrintF("  dump memory content, default dump 10 words)\n");
7101cb0ef41Sopenharmony_ci        PrintF("dump [<words>]\n");
7111cb0ef41Sopenharmony_ci        PrintF(
7121cb0ef41Sopenharmony_ci            "  dump memory content without pretty printing JS objects, default "
7131cb0ef41Sopenharmony_ci            "dump 10 words)\n");
7141cb0ef41Sopenharmony_ci        PrintF("flags\n");
7151cb0ef41Sopenharmony_ci        PrintF("  print flags\n");
7161cb0ef41Sopenharmony_ci        PrintF("disasm [<instructions>]\n");
7171cb0ef41Sopenharmony_ci        PrintF("disasm [<address/register>]\n");
7181cb0ef41Sopenharmony_ci        PrintF("disasm [[<address/register>] <instructions>]\n");
7191cb0ef41Sopenharmony_ci        PrintF("  disassemble code, default is 10 instructions\n");
7201cb0ef41Sopenharmony_ci        PrintF("  from pc (alias 'di')\n");
7211cb0ef41Sopenharmony_ci        PrintF("gdb\n");
7221cb0ef41Sopenharmony_ci        PrintF("  enter gdb\n");
7231cb0ef41Sopenharmony_ci        PrintF("break <address>\n");
7241cb0ef41Sopenharmony_ci        PrintF("  set a break point on the address\n");
7251cb0ef41Sopenharmony_ci        PrintF("del\n");
7261cb0ef41Sopenharmony_ci        PrintF("  delete the breakpoint\n");
7271cb0ef41Sopenharmony_ci        PrintF("stop feature:\n");
7281cb0ef41Sopenharmony_ci        PrintF("  Description:\n");
7291cb0ef41Sopenharmony_ci        PrintF("    Stops are debug instructions inserted by\n");
7301cb0ef41Sopenharmony_ci        PrintF("    the Assembler::stop() function.\n");
7311cb0ef41Sopenharmony_ci        PrintF("    When hitting a stop, the Simulator will\n");
7321cb0ef41Sopenharmony_ci        PrintF("    stop and give control to the Debugger.\n");
7331cb0ef41Sopenharmony_ci        PrintF("    All stop codes are watched:\n");
7341cb0ef41Sopenharmony_ci        PrintF("    - They can be enabled / disabled: the Simulator\n");
7351cb0ef41Sopenharmony_ci        PrintF("       will / won't stop when hitting them.\n");
7361cb0ef41Sopenharmony_ci        PrintF("    - The Simulator keeps track of how many times they \n");
7371cb0ef41Sopenharmony_ci        PrintF("      are met. (See the info command.) Going over a\n");
7381cb0ef41Sopenharmony_ci        PrintF("      disabled stop still increases its counter. \n");
7391cb0ef41Sopenharmony_ci        PrintF("  Commands:\n");
7401cb0ef41Sopenharmony_ci        PrintF("    stop info all/<code> : print infos about number <code>\n");
7411cb0ef41Sopenharmony_ci        PrintF("      or all stop(s).\n");
7421cb0ef41Sopenharmony_ci        PrintF("    stop enable/disable all/<code> : enables / disables\n");
7431cb0ef41Sopenharmony_ci        PrintF("      all or number <code> stop(s)\n");
7441cb0ef41Sopenharmony_ci        PrintF("    stop unstop\n");
7451cb0ef41Sopenharmony_ci        PrintF("      ignore the stop instruction at the current location\n");
7461cb0ef41Sopenharmony_ci        PrintF("      from now on\n");
7471cb0ef41Sopenharmony_ci      } else {
7481cb0ef41Sopenharmony_ci        PrintF("Unknown command: %s\n", cmd);
7491cb0ef41Sopenharmony_ci      }
7501cb0ef41Sopenharmony_ci    }
7511cb0ef41Sopenharmony_ci  }
7521cb0ef41Sopenharmony_ci
7531cb0ef41Sopenharmony_ci  // Add all the breakpoints back to stop execution and enter the debugger
7541cb0ef41Sopenharmony_ci  // shell when hit.
7551cb0ef41Sopenharmony_ci  RedoBreakpoints();
7561cb0ef41Sopenharmony_ci
7571cb0ef41Sopenharmony_ci#undef COMMAND_SIZE
7581cb0ef41Sopenharmony_ci#undef ARG_SIZE
7591cb0ef41Sopenharmony_ci
7601cb0ef41Sopenharmony_ci#undef STR
7611cb0ef41Sopenharmony_ci#undef XSTR
7621cb0ef41Sopenharmony_ci}
7631cb0ef41Sopenharmony_ci
7641cb0ef41Sopenharmony_cibool Simulator::ICacheMatch(void* one, void* two) {
7651cb0ef41Sopenharmony_ci  DCHECK_EQ(reinterpret_cast<intptr_t>(one) & CachePage::kPageMask, 0);
7661cb0ef41Sopenharmony_ci  DCHECK_EQ(reinterpret_cast<intptr_t>(two) & CachePage::kPageMask, 0);
7671cb0ef41Sopenharmony_ci  return one == two;
7681cb0ef41Sopenharmony_ci}
7691cb0ef41Sopenharmony_ci
7701cb0ef41Sopenharmony_cistatic uint32_t ICacheHash(void* key) {
7711cb0ef41Sopenharmony_ci  return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key)) >> 2;
7721cb0ef41Sopenharmony_ci}
7731cb0ef41Sopenharmony_ci
7741cb0ef41Sopenharmony_cistatic bool AllOnOnePage(uintptr_t start, int size) {
7751cb0ef41Sopenharmony_ci  intptr_t start_page = (start & ~CachePage::kPageMask);
7761cb0ef41Sopenharmony_ci  intptr_t end_page = ((start + size) & ~CachePage::kPageMask);
7771cb0ef41Sopenharmony_ci  return start_page == end_page;
7781cb0ef41Sopenharmony_ci}
7791cb0ef41Sopenharmony_ci
7801cb0ef41Sopenharmony_civoid Simulator::set_last_debugger_input(char* input) {
7811cb0ef41Sopenharmony_ci  DeleteArray(last_debugger_input_);
7821cb0ef41Sopenharmony_ci  last_debugger_input_ = input;
7831cb0ef41Sopenharmony_ci}
7841cb0ef41Sopenharmony_ci
7851cb0ef41Sopenharmony_civoid Simulator::SetRedirectInstruction(Instruction* instruction) {
7861cb0ef41Sopenharmony_ci  instruction->SetInstructionBits(rtCallRedirInstr);
7871cb0ef41Sopenharmony_ci}
7881cb0ef41Sopenharmony_ci
7891cb0ef41Sopenharmony_civoid Simulator::FlushICache(base::CustomMatcherHashMap* i_cache,
7901cb0ef41Sopenharmony_ci                            void* start_addr, size_t size) {
7911cb0ef41Sopenharmony_ci  intptr_t start = reinterpret_cast<intptr_t>(start_addr);
7921cb0ef41Sopenharmony_ci  int intra_line = (start & CachePage::kLineMask);
7931cb0ef41Sopenharmony_ci  start -= intra_line;
7941cb0ef41Sopenharmony_ci  size += intra_line;
7951cb0ef41Sopenharmony_ci  size = ((size - 1) | CachePage::kLineMask) + 1;
7961cb0ef41Sopenharmony_ci  int offset = (start & CachePage::kPageMask);
7971cb0ef41Sopenharmony_ci  while (!AllOnOnePage(start, size - 1)) {
7981cb0ef41Sopenharmony_ci    int bytes_to_flush = CachePage::kPageSize - offset;
7991cb0ef41Sopenharmony_ci    FlushOnePage(i_cache, start, bytes_to_flush);
8001cb0ef41Sopenharmony_ci    start += bytes_to_flush;
8011cb0ef41Sopenharmony_ci    size -= bytes_to_flush;
8021cb0ef41Sopenharmony_ci    DCHECK_EQ(0, start & CachePage::kPageMask);
8031cb0ef41Sopenharmony_ci    offset = 0;
8041cb0ef41Sopenharmony_ci  }
8051cb0ef41Sopenharmony_ci  if (size != 0) {
8061cb0ef41Sopenharmony_ci    FlushOnePage(i_cache, start, size);
8071cb0ef41Sopenharmony_ci  }
8081cb0ef41Sopenharmony_ci}
8091cb0ef41Sopenharmony_ci
8101cb0ef41Sopenharmony_ciCachePage* Simulator::GetCachePage(base::CustomMatcherHashMap* i_cache,
8111cb0ef41Sopenharmony_ci                                   void* page) {
8121cb0ef41Sopenharmony_ci  base::CustomMatcherHashMap::Entry* entry =
8131cb0ef41Sopenharmony_ci      i_cache->LookupOrInsert(page, ICacheHash(page));
8141cb0ef41Sopenharmony_ci  if (entry->value == nullptr) {
8151cb0ef41Sopenharmony_ci    CachePage* new_page = new CachePage();
8161cb0ef41Sopenharmony_ci    entry->value = new_page;
8171cb0ef41Sopenharmony_ci  }
8181cb0ef41Sopenharmony_ci  return reinterpret_cast<CachePage*>(entry->value);
8191cb0ef41Sopenharmony_ci}
8201cb0ef41Sopenharmony_ci
8211cb0ef41Sopenharmony_ci// Flush from start up to and not including start + size.
8221cb0ef41Sopenharmony_civoid Simulator::FlushOnePage(base::CustomMatcherHashMap* i_cache,
8231cb0ef41Sopenharmony_ci                             intptr_t start, int size) {
8241cb0ef41Sopenharmony_ci  DCHECK_LE(size, CachePage::kPageSize);
8251cb0ef41Sopenharmony_ci  DCHECK(AllOnOnePage(start, size - 1));
8261cb0ef41Sopenharmony_ci  DCHECK_EQ(start & CachePage::kLineMask, 0);
8271cb0ef41Sopenharmony_ci  DCHECK_EQ(size & CachePage::kLineMask, 0);
8281cb0ef41Sopenharmony_ci  void* page = reinterpret_cast<void*>(start & (~CachePage::kPageMask));
8291cb0ef41Sopenharmony_ci  int offset = (start & CachePage::kPageMask);
8301cb0ef41Sopenharmony_ci  CachePage* cache_page = GetCachePage(i_cache, page);
8311cb0ef41Sopenharmony_ci  char* valid_bytemap = cache_page->ValidityByte(offset);
8321cb0ef41Sopenharmony_ci  memset(valid_bytemap, CachePage::LINE_INVALID, size >> CachePage::kLineShift);
8331cb0ef41Sopenharmony_ci}
8341cb0ef41Sopenharmony_ci
8351cb0ef41Sopenharmony_civoid Simulator::CheckICache(base::CustomMatcherHashMap* i_cache,
8361cb0ef41Sopenharmony_ci                            Instruction* instr) {
8371cb0ef41Sopenharmony_ci  intptr_t address = reinterpret_cast<intptr_t>(instr);
8381cb0ef41Sopenharmony_ci  void* page = reinterpret_cast<void*>(address & (~CachePage::kPageMask));
8391cb0ef41Sopenharmony_ci  void* line = reinterpret_cast<void*>(address & (~CachePage::kLineMask));
8401cb0ef41Sopenharmony_ci  int offset = (address & CachePage::kPageMask);
8411cb0ef41Sopenharmony_ci  CachePage* cache_page = GetCachePage(i_cache, page);
8421cb0ef41Sopenharmony_ci  char* cache_valid_byte = cache_page->ValidityByte(offset);
8431cb0ef41Sopenharmony_ci  bool cache_hit = (*cache_valid_byte == CachePage::LINE_VALID);
8441cb0ef41Sopenharmony_ci  char* cached_line = cache_page->CachedData(offset & ~CachePage::kLineMask);
8451cb0ef41Sopenharmony_ci  if (cache_hit) {
8461cb0ef41Sopenharmony_ci    // Check that the data in memory matches the contents of the I-cache.
8471cb0ef41Sopenharmony_ci    CHECK_EQ(0, memcmp(reinterpret_cast<void*>(instr),
8481cb0ef41Sopenharmony_ci                       cache_page->CachedData(offset), kInstrSize));
8491cb0ef41Sopenharmony_ci  } else {
8501cb0ef41Sopenharmony_ci    // Cache miss.  Load memory into the cache.
8511cb0ef41Sopenharmony_ci    memcpy(cached_line, line, CachePage::kLineLength);
8521cb0ef41Sopenharmony_ci    *cache_valid_byte = CachePage::LINE_VALID;
8531cb0ef41Sopenharmony_ci  }
8541cb0ef41Sopenharmony_ci}
8551cb0ef41Sopenharmony_ci
8561cb0ef41Sopenharmony_ciSimulator::Simulator(Isolate* isolate) : isolate_(isolate) {
8571cb0ef41Sopenharmony_ci  // Set up simulator support first. Some of this information is needed to
8581cb0ef41Sopenharmony_ci  // setup the architecture state.
8591cb0ef41Sopenharmony_ci  stack_size_ = FLAG_sim_stack_size * KB;
8601cb0ef41Sopenharmony_ci  stack_ = reinterpret_cast<char*>(base::Malloc(stack_size_));
8611cb0ef41Sopenharmony_ci  pc_modified_ = false;
8621cb0ef41Sopenharmony_ci  icount_ = 0;
8631cb0ef41Sopenharmony_ci  break_count_ = 0;
8641cb0ef41Sopenharmony_ci  break_pc_ = nullptr;
8651cb0ef41Sopenharmony_ci  break_instr_ = 0;
8661cb0ef41Sopenharmony_ci
8671cb0ef41Sopenharmony_ci  // Set up architecture state.
8681cb0ef41Sopenharmony_ci  // All registers are initialized to zero to start with.
8691cb0ef41Sopenharmony_ci  for (int i = 0; i < kNumSimuRegisters; i++) {
8701cb0ef41Sopenharmony_ci    registers_[i] = 0;
8711cb0ef41Sopenharmony_ci  }
8721cb0ef41Sopenharmony_ci  for (int i = 0; i < kNumFPURegisters; i++) {
8731cb0ef41Sopenharmony_ci    FPUregisters_[2 * i] = 0;
8741cb0ef41Sopenharmony_ci    FPUregisters_[2 * i + 1] = 0;  // upper part for MSA ASE
8751cb0ef41Sopenharmony_ci  }
8761cb0ef41Sopenharmony_ci  if (IsMipsArchVariant(kMips32r6)) {
8771cb0ef41Sopenharmony_ci    FCSR_ = kFCSRNaN2008FlagMask;
8781cb0ef41Sopenharmony_ci    MSACSR_ = 0;
8791cb0ef41Sopenharmony_ci  } else {
8801cb0ef41Sopenharmony_ci    DCHECK(IsMipsArchVariant(kMips32r1) || IsMipsArchVariant(kMips32r2));
8811cb0ef41Sopenharmony_ci    FCSR_ = 0;
8821cb0ef41Sopenharmony_ci  }
8831cb0ef41Sopenharmony_ci
8841cb0ef41Sopenharmony_ci  // The sp is initialized to point to the bottom (high address) of the
8851cb0ef41Sopenharmony_ci  // allocated stack area. To be safe in potential stack underflows we leave
8861cb0ef41Sopenharmony_ci  // some buffer below.
8871cb0ef41Sopenharmony_ci  registers_[sp] = reinterpret_cast<int32_t>(stack_) + stack_size_ - 64;
8881cb0ef41Sopenharmony_ci  // The ra and pc are initialized to a known bad value that will cause an
8891cb0ef41Sopenharmony_ci  // access violation if the simulator ever tries to execute it.
8901cb0ef41Sopenharmony_ci  registers_[pc] = bad_ra;
8911cb0ef41Sopenharmony_ci  registers_[ra] = bad_ra;
8921cb0ef41Sopenharmony_ci  last_debugger_input_ = nullptr;
8931cb0ef41Sopenharmony_ci}
8941cb0ef41Sopenharmony_ci
8951cb0ef41Sopenharmony_ciSimulator::~Simulator() {
8961cb0ef41Sopenharmony_ci  GlobalMonitor::Get()->RemoveLinkedAddress(&global_monitor_thread_);
8971cb0ef41Sopenharmony_ci  base::Free(stack_);
8981cb0ef41Sopenharmony_ci}
8991cb0ef41Sopenharmony_ci
9001cb0ef41Sopenharmony_ci// Get the active Simulator for the current thread.
9011cb0ef41Sopenharmony_ciSimulator* Simulator::current(Isolate* isolate) {
9021cb0ef41Sopenharmony_ci  v8::internal::Isolate::PerIsolateThreadData* isolate_data =
9031cb0ef41Sopenharmony_ci      isolate->FindOrAllocatePerThreadDataForThisThread();
9041cb0ef41Sopenharmony_ci  DCHECK_NOT_NULL(isolate_data);
9051cb0ef41Sopenharmony_ci
9061cb0ef41Sopenharmony_ci  Simulator* sim = isolate_data->simulator();
9071cb0ef41Sopenharmony_ci  if (sim == nullptr) {
9081cb0ef41Sopenharmony_ci    // TODO(146): delete the simulator object when a thread/isolate goes away.
9091cb0ef41Sopenharmony_ci    sim = new Simulator(isolate);
9101cb0ef41Sopenharmony_ci    isolate_data->set_simulator(sim);
9111cb0ef41Sopenharmony_ci  }
9121cb0ef41Sopenharmony_ci  return sim;
9131cb0ef41Sopenharmony_ci}
9141cb0ef41Sopenharmony_ci
9151cb0ef41Sopenharmony_ci// Sets the register in the architecture state. It will also deal with updating
9161cb0ef41Sopenharmony_ci// Simulator internal state for special registers such as PC.
9171cb0ef41Sopenharmony_civoid Simulator::set_register(int reg, int32_t value) {
9181cb0ef41Sopenharmony_ci  DCHECK((reg >= 0) && (reg < kNumSimuRegisters));
9191cb0ef41Sopenharmony_ci  if (reg == pc) {
9201cb0ef41Sopenharmony_ci    pc_modified_ = true;
9211cb0ef41Sopenharmony_ci  }
9221cb0ef41Sopenharmony_ci
9231cb0ef41Sopenharmony_ci  // Zero register always holds 0.
9241cb0ef41Sopenharmony_ci  registers_[reg] = (reg == 0) ? 0 : value;
9251cb0ef41Sopenharmony_ci}
9261cb0ef41Sopenharmony_ci
9271cb0ef41Sopenharmony_civoid Simulator::set_dw_register(int reg, const int* dbl) {
9281cb0ef41Sopenharmony_ci  DCHECK((reg >= 0) && (reg < kNumSimuRegisters));
9291cb0ef41Sopenharmony_ci  registers_[reg] = dbl[0];
9301cb0ef41Sopenharmony_ci  registers_[reg + 1] = dbl[1];
9311cb0ef41Sopenharmony_ci}
9321cb0ef41Sopenharmony_ci
9331cb0ef41Sopenharmony_civoid Simulator::set_fpu_register(int fpureg, int64_t value) {
9341cb0ef41Sopenharmony_ci  DCHECK(IsFp64Mode());
9351cb0ef41Sopenharmony_ci  DCHECK((fpureg >= 0) && (fpureg < kNumFPURegisters));
9361cb0ef41Sopenharmony_ci  FPUregisters_[fpureg * 2] = value;
9371cb0ef41Sopenharmony_ci}
9381cb0ef41Sopenharmony_ci
9391cb0ef41Sopenharmony_civoid Simulator::set_fpu_register_word(int fpureg, int32_t value) {
9401cb0ef41Sopenharmony_ci  // Set ONLY lower 32-bits, leaving upper bits untouched.
9411cb0ef41Sopenharmony_ci  // TODO(plind): big endian issue.
9421cb0ef41Sopenharmony_ci  DCHECK((fpureg >= 0) && (fpureg < kNumFPURegisters));
9431cb0ef41Sopenharmony_ci  int32_t* pword = reinterpret_cast<int32_t*>(&FPUregisters_[fpureg * 2]);
9441cb0ef41Sopenharmony_ci  *pword = value;
9451cb0ef41Sopenharmony_ci}
9461cb0ef41Sopenharmony_ci
9471cb0ef41Sopenharmony_civoid Simulator::set_fpu_register_hi_word(int fpureg, int32_t value) {
9481cb0ef41Sopenharmony_ci  // Set ONLY upper 32-bits, leaving lower bits untouched.
9491cb0ef41Sopenharmony_ci  // TODO(plind): big endian issue.
9501cb0ef41Sopenharmony_ci  DCHECK((fpureg >= 0) && (fpureg < kNumFPURegisters));
9511cb0ef41Sopenharmony_ci  int32_t* phiword =
9521cb0ef41Sopenharmony_ci      (reinterpret_cast<int32_t*>(&FPUregisters_[fpureg * 2])) + 1;
9531cb0ef41Sopenharmony_ci  *phiword = value;
9541cb0ef41Sopenharmony_ci}
9551cb0ef41Sopenharmony_ci
9561cb0ef41Sopenharmony_civoid Simulator::set_fpu_register_float(int fpureg, float value) {
9571cb0ef41Sopenharmony_ci  DCHECK((fpureg >= 0) && (fpureg < kNumFPURegisters));
9581cb0ef41Sopenharmony_ci  *bit_cast<float*>(&FPUregisters_[fpureg * 2]) = value;
9591cb0ef41Sopenharmony_ci}
9601cb0ef41Sopenharmony_ci
9611cb0ef41Sopenharmony_civoid Simulator::set_fpu_register_double(int fpureg, double value) {
9621cb0ef41Sopenharmony_ci  if (IsFp64Mode()) {
9631cb0ef41Sopenharmony_ci    DCHECK((fpureg >= 0) && (fpureg < kNumFPURegisters));
9641cb0ef41Sopenharmony_ci    *bit_cast<double*>(&FPUregisters_[fpureg * 2]) = value;
9651cb0ef41Sopenharmony_ci  } else {
9661cb0ef41Sopenharmony_ci    DCHECK((fpureg >= 0) && (fpureg < kNumFPURegisters) && ((fpureg % 2) == 0));
9671cb0ef41Sopenharmony_ci    int64_t i64 = bit_cast<int64_t>(value);
9681cb0ef41Sopenharmony_ci    set_fpu_register_word(fpureg, i64 & 0xFFFFFFFF);
9691cb0ef41Sopenharmony_ci    set_fpu_register_word(fpureg + 1, i64 >> 32);
9701cb0ef41Sopenharmony_ci  }
9711cb0ef41Sopenharmony_ci}
9721cb0ef41Sopenharmony_ci
9731cb0ef41Sopenharmony_ci// Get the register from the architecture state. This function does handle
9741cb0ef41Sopenharmony_ci// the special case of accessing the PC register.
9751cb0ef41Sopenharmony_ciint32_t Simulator::get_register(int reg) const {
9761cb0ef41Sopenharmony_ci  DCHECK((reg >= 0) && (reg < kNumSimuRegisters));
9771cb0ef41Sopenharmony_ci  if (reg == 0)
9781cb0ef41Sopenharmony_ci    return 0;
9791cb0ef41Sopenharmony_ci  else
9801cb0ef41Sopenharmony_ci    return registers_[reg] + ((reg == pc) ? Instruction::kPCReadOffset : 0);
9811cb0ef41Sopenharmony_ci}
9821cb0ef41Sopenharmony_ci
9831cb0ef41Sopenharmony_cidouble Simulator::get_double_from_register_pair(int reg) {
9841cb0ef41Sopenharmony_ci  // TODO(plind): bad ABI stuff, refactor or remove.
9851cb0ef41Sopenharmony_ci  DCHECK((reg >= 0) && (reg < kNumSimuRegisters) && ((reg % 2) == 0));
9861cb0ef41Sopenharmony_ci
9871cb0ef41Sopenharmony_ci  double dm_val = 0.0;
9881cb0ef41Sopenharmony_ci  // Read the bits from the unsigned integer register_[] array
9891cb0ef41Sopenharmony_ci  // into the double precision floating point value and return it.
9901cb0ef41Sopenharmony_ci  char buffer[2 * sizeof(registers_[0])];
9911cb0ef41Sopenharmony_ci  memcpy(buffer, &registers_[reg], 2 * sizeof(registers_[0]));
9921cb0ef41Sopenharmony_ci  memcpy(&dm_val, buffer, 2 * sizeof(registers_[0]));
9931cb0ef41Sopenharmony_ci  return (dm_val);
9941cb0ef41Sopenharmony_ci}
9951cb0ef41Sopenharmony_ci
9961cb0ef41Sopenharmony_ciint64_t Simulator::get_fpu_register(int fpureg) const {
9971cb0ef41Sopenharmony_ci  if (IsFp64Mode()) {
9981cb0ef41Sopenharmony_ci    DCHECK((fpureg >= 0) && (fpureg < kNumFPURegisters));
9991cb0ef41Sopenharmony_ci    return FPUregisters_[fpureg * 2];
10001cb0ef41Sopenharmony_ci  } else {
10011cb0ef41Sopenharmony_ci    DCHECK((fpureg >= 0) && (fpureg < kNumFPURegisters) && ((fpureg % 2) == 0));
10021cb0ef41Sopenharmony_ci    uint64_t i64;
10031cb0ef41Sopenharmony_ci    i64 = static_cast<uint32_t>(get_fpu_register_word(fpureg));
10041cb0ef41Sopenharmony_ci    i64 |= static_cast<uint64_t>(get_fpu_register_word(fpureg + 1)) << 32;
10051cb0ef41Sopenharmony_ci    return static_cast<int64_t>(i64);
10061cb0ef41Sopenharmony_ci  }
10071cb0ef41Sopenharmony_ci}
10081cb0ef41Sopenharmony_ci
10091cb0ef41Sopenharmony_ciint32_t Simulator::get_fpu_register_word(int fpureg) const {
10101cb0ef41Sopenharmony_ci  DCHECK((fpureg >= 0) && (fpureg < kNumFPURegisters));
10111cb0ef41Sopenharmony_ci  return static_cast<int32_t>(FPUregisters_[fpureg * 2] & 0xFFFFFFFF);
10121cb0ef41Sopenharmony_ci}
10131cb0ef41Sopenharmony_ci
10141cb0ef41Sopenharmony_ciint32_t Simulator::get_fpu_register_signed_word(int fpureg) const {
10151cb0ef41Sopenharmony_ci  DCHECK((fpureg >= 0) && (fpureg < kNumFPURegisters));
10161cb0ef41Sopenharmony_ci  return static_cast<int32_t>(FPUregisters_[fpureg * 2] & 0xFFFFFFFF);
10171cb0ef41Sopenharmony_ci}
10181cb0ef41Sopenharmony_ci
10191cb0ef41Sopenharmony_ciint32_t Simulator::get_fpu_register_hi_word(int fpureg) const {
10201cb0ef41Sopenharmony_ci  DCHECK((fpureg >= 0) && (fpureg < kNumFPURegisters));
10211cb0ef41Sopenharmony_ci  return static_cast<int32_t>((FPUregisters_[fpureg * 2] >> 32) & 0xFFFFFFFF);
10221cb0ef41Sopenharmony_ci}
10231cb0ef41Sopenharmony_ci
10241cb0ef41Sopenharmony_cifloat Simulator::get_fpu_register_float(int fpureg) const {
10251cb0ef41Sopenharmony_ci  DCHECK((fpureg >= 0) && (fpureg < kNumFPURegisters));
10261cb0ef41Sopenharmony_ci  return *bit_cast<float*>(const_cast<int64_t*>(&FPUregisters_[fpureg * 2]));
10271cb0ef41Sopenharmony_ci}
10281cb0ef41Sopenharmony_ci
10291cb0ef41Sopenharmony_cidouble Simulator::get_fpu_register_double(int fpureg) const {
10301cb0ef41Sopenharmony_ci  if (IsFp64Mode()) {
10311cb0ef41Sopenharmony_ci    DCHECK((fpureg >= 0) && (fpureg < kNumFPURegisters));
10321cb0ef41Sopenharmony_ci    return *bit_cast<double*>(&FPUregisters_[fpureg * 2]);
10331cb0ef41Sopenharmony_ci  } else {
10341cb0ef41Sopenharmony_ci    DCHECK((fpureg >= 0) && (fpureg < kNumFPURegisters) && ((fpureg % 2) == 0));
10351cb0ef41Sopenharmony_ci    int64_t i64;
10361cb0ef41Sopenharmony_ci    i64 = static_cast<uint32_t>(get_fpu_register_word(fpureg));
10371cb0ef41Sopenharmony_ci    i64 |= static_cast<uint64_t>(get_fpu_register_word(fpureg + 1)) << 32;
10381cb0ef41Sopenharmony_ci    return bit_cast<double>(i64);
10391cb0ef41Sopenharmony_ci  }
10401cb0ef41Sopenharmony_ci}
10411cb0ef41Sopenharmony_ci
10421cb0ef41Sopenharmony_citemplate <typename T>
10431cb0ef41Sopenharmony_civoid Simulator::get_msa_register(int wreg, T* value) {
10441cb0ef41Sopenharmony_ci  DCHECK((wreg >= 0) && (wreg < kNumMSARegisters));
10451cb0ef41Sopenharmony_ci  memcpy(value, FPUregisters_ + wreg * 2, kSimd128Size);
10461cb0ef41Sopenharmony_ci}
10471cb0ef41Sopenharmony_ci
10481cb0ef41Sopenharmony_citemplate <typename T>
10491cb0ef41Sopenharmony_civoid Simulator::set_msa_register(int wreg, const T* value) {
10501cb0ef41Sopenharmony_ci  DCHECK((wreg >= 0) && (wreg < kNumMSARegisters));
10511cb0ef41Sopenharmony_ci  memcpy(FPUregisters_ + wreg * 2, value, kSimd128Size);
10521cb0ef41Sopenharmony_ci}
10531cb0ef41Sopenharmony_ci
10541cb0ef41Sopenharmony_ci// Runtime FP routines take up to two double arguments and zero
10551cb0ef41Sopenharmony_ci// or one integer arguments. All are constructed here,
10561cb0ef41Sopenharmony_ci// from a0-a3 or f12 and f14.
10571cb0ef41Sopenharmony_civoid Simulator::GetFpArgs(double* x, double* y, int32_t* z) {
10581cb0ef41Sopenharmony_ci  if (!IsMipsSoftFloatABI) {
10591cb0ef41Sopenharmony_ci    *x = get_fpu_register_double(12);
10601cb0ef41Sopenharmony_ci    *y = get_fpu_register_double(14);
10611cb0ef41Sopenharmony_ci    *z = get_register(a2);
10621cb0ef41Sopenharmony_ci  } else {
10631cb0ef41Sopenharmony_ci    // TODO(plind): bad ABI stuff, refactor or remove.
10641cb0ef41Sopenharmony_ci    // We use a char buffer to get around the strict-aliasing rules which
10651cb0ef41Sopenharmony_ci    // otherwise allow the compiler to optimize away the copy.
10661cb0ef41Sopenharmony_ci    char buffer[sizeof(*x)];
10671cb0ef41Sopenharmony_ci    int32_t* reg_buffer = reinterpret_cast<int32_t*>(buffer);
10681cb0ef41Sopenharmony_ci
10691cb0ef41Sopenharmony_ci    // Registers a0 and a1 -> x.
10701cb0ef41Sopenharmony_ci    reg_buffer[0] = get_register(a0);
10711cb0ef41Sopenharmony_ci    reg_buffer[1] = get_register(a1);
10721cb0ef41Sopenharmony_ci    memcpy(x, buffer, sizeof(buffer));
10731cb0ef41Sopenharmony_ci    // Registers a2 and a3 -> y.
10741cb0ef41Sopenharmony_ci    reg_buffer[0] = get_register(a2);
10751cb0ef41Sopenharmony_ci    reg_buffer[1] = get_register(a3);
10761cb0ef41Sopenharmony_ci    memcpy(y, buffer, sizeof(buffer));
10771cb0ef41Sopenharmony_ci    // Register 2 -> z.
10781cb0ef41Sopenharmony_ci    reg_buffer[0] = get_register(a2);
10791cb0ef41Sopenharmony_ci    memcpy(z, buffer, sizeof(*z));
10801cb0ef41Sopenharmony_ci  }
10811cb0ef41Sopenharmony_ci}
10821cb0ef41Sopenharmony_ci
10831cb0ef41Sopenharmony_ci// The return value is either in v0/v1 or f0.
10841cb0ef41Sopenharmony_civoid Simulator::SetFpResult(const double& result) {
10851cb0ef41Sopenharmony_ci  if (!IsMipsSoftFloatABI) {
10861cb0ef41Sopenharmony_ci    set_fpu_register_double(0, result);
10871cb0ef41Sopenharmony_ci  } else {
10881cb0ef41Sopenharmony_ci    char buffer[2 * sizeof(registers_[0])];
10891cb0ef41Sopenharmony_ci    int32_t* reg_buffer = reinterpret_cast<int32_t*>(buffer);
10901cb0ef41Sopenharmony_ci    memcpy(buffer, &result, sizeof(buffer));
10911cb0ef41Sopenharmony_ci    // Copy result to v0 and v1.
10921cb0ef41Sopenharmony_ci    set_register(v0, reg_buffer[0]);
10931cb0ef41Sopenharmony_ci    set_register(v1, reg_buffer[1]);
10941cb0ef41Sopenharmony_ci  }
10951cb0ef41Sopenharmony_ci}
10961cb0ef41Sopenharmony_ci
10971cb0ef41Sopenharmony_ci// Helper functions for setting and testing the FCSR register's bits.
10981cb0ef41Sopenharmony_civoid Simulator::set_fcsr_bit(uint32_t cc, bool value) {
10991cb0ef41Sopenharmony_ci  if (value) {
11001cb0ef41Sopenharmony_ci    FCSR_ |= (1 << cc);
11011cb0ef41Sopenharmony_ci  } else {
11021cb0ef41Sopenharmony_ci    FCSR_ &= ~(1 << cc);
11031cb0ef41Sopenharmony_ci  }
11041cb0ef41Sopenharmony_ci}
11051cb0ef41Sopenharmony_ci
11061cb0ef41Sopenharmony_cibool Simulator::test_fcsr_bit(uint32_t cc) { return FCSR_ & (1 << cc); }
11071cb0ef41Sopenharmony_ci
11081cb0ef41Sopenharmony_civoid Simulator::clear_fcsr_cause() {
11091cb0ef41Sopenharmony_ci  FCSR_ &= ~kFCSRCauseMask;
11101cb0ef41Sopenharmony_ci}
11111cb0ef41Sopenharmony_ci
11121cb0ef41Sopenharmony_civoid Simulator::set_fcsr_rounding_mode(FPURoundingMode mode) {
11131cb0ef41Sopenharmony_ci  FCSR_ |= mode & kFPURoundingModeMask;
11141cb0ef41Sopenharmony_ci}
11151cb0ef41Sopenharmony_ci
11161cb0ef41Sopenharmony_civoid Simulator::set_msacsr_rounding_mode(FPURoundingMode mode) {
11171cb0ef41Sopenharmony_ci  MSACSR_ |= mode & kFPURoundingModeMask;
11181cb0ef41Sopenharmony_ci}
11191cb0ef41Sopenharmony_ci
11201cb0ef41Sopenharmony_ciunsigned int Simulator::get_fcsr_rounding_mode() {
11211cb0ef41Sopenharmony_ci  return FCSR_ & kFPURoundingModeMask;
11221cb0ef41Sopenharmony_ci}
11231cb0ef41Sopenharmony_ci
11241cb0ef41Sopenharmony_ciunsigned int Simulator::get_msacsr_rounding_mode() {
11251cb0ef41Sopenharmony_ci  return MSACSR_ & kFPURoundingModeMask;
11261cb0ef41Sopenharmony_ci}
11271cb0ef41Sopenharmony_ci
11281cb0ef41Sopenharmony_civoid Simulator::set_fpu_register_word_invalid_result(float original,
11291cb0ef41Sopenharmony_ci                                                     float rounded) {
11301cb0ef41Sopenharmony_ci  if (FCSR_ & kFCSRNaN2008FlagMask) {
11311cb0ef41Sopenharmony_ci    double max_int32 = std::numeric_limits<int32_t>::max();
11321cb0ef41Sopenharmony_ci    double min_int32 = std::numeric_limits<int32_t>::min();
11331cb0ef41Sopenharmony_ci    if (std::isnan(original)) {
11341cb0ef41Sopenharmony_ci      set_fpu_register_word(fd_reg(), 0);
11351cb0ef41Sopenharmony_ci    } else if (rounded > max_int32) {
11361cb0ef41Sopenharmony_ci      set_fpu_register_word(fd_reg(), kFPUInvalidResult);
11371cb0ef41Sopenharmony_ci    } else if (rounded < min_int32) {
11381cb0ef41Sopenharmony_ci      set_fpu_register_word(fd_reg(), kFPUInvalidResultNegative);
11391cb0ef41Sopenharmony_ci    } else {
11401cb0ef41Sopenharmony_ci      UNREACHABLE();
11411cb0ef41Sopenharmony_ci    }
11421cb0ef41Sopenharmony_ci  } else {
11431cb0ef41Sopenharmony_ci    set_fpu_register_word(fd_reg(), kFPUInvalidResult);
11441cb0ef41Sopenharmony_ci  }
11451cb0ef41Sopenharmony_ci}
11461cb0ef41Sopenharmony_ci
11471cb0ef41Sopenharmony_civoid Simulator::set_fpu_register_invalid_result(float original, float rounded) {
11481cb0ef41Sopenharmony_ci  if (FCSR_ & kFCSRNaN2008FlagMask) {
11491cb0ef41Sopenharmony_ci    double max_int32 = std::numeric_limits<int32_t>::max();
11501cb0ef41Sopenharmony_ci    double min_int32 = std::numeric_limits<int32_t>::min();
11511cb0ef41Sopenharmony_ci    if (std::isnan(original)) {
11521cb0ef41Sopenharmony_ci      set_fpu_register(fd_reg(), 0);
11531cb0ef41Sopenharmony_ci    } else if (rounded > max_int32) {
11541cb0ef41Sopenharmony_ci      set_fpu_register(fd_reg(), kFPUInvalidResult);
11551cb0ef41Sopenharmony_ci    } else if (rounded < min_int32) {
11561cb0ef41Sopenharmony_ci      set_fpu_register(fd_reg(), kFPUInvalidResultNegative);
11571cb0ef41Sopenharmony_ci    } else {
11581cb0ef41Sopenharmony_ci      UNREACHABLE();
11591cb0ef41Sopenharmony_ci    }
11601cb0ef41Sopenharmony_ci  } else {
11611cb0ef41Sopenharmony_ci    set_fpu_register(fd_reg(), kFPUInvalidResult);
11621cb0ef41Sopenharmony_ci  }
11631cb0ef41Sopenharmony_ci}
11641cb0ef41Sopenharmony_ci
11651cb0ef41Sopenharmony_civoid Simulator::set_fpu_register_invalid_result64(float original,
11661cb0ef41Sopenharmony_ci                                                  float rounded) {
11671cb0ef41Sopenharmony_ci  if (FCSR_ & kFCSRNaN2008FlagMask) {
11681cb0ef41Sopenharmony_ci    // The value of INT64_MAX (2^63-1) can't be represented as double exactly,
11691cb0ef41Sopenharmony_ci    // loading the most accurate representation into max_int64, which is 2^63.
11701cb0ef41Sopenharmony_ci    double max_int64 = static_cast<double>(std::numeric_limits<int64_t>::max());
11711cb0ef41Sopenharmony_ci    double min_int64 = std::numeric_limits<int64_t>::min();
11721cb0ef41Sopenharmony_ci    if (std::isnan(original)) {
11731cb0ef41Sopenharmony_ci      set_fpu_register(fd_reg(), 0);
11741cb0ef41Sopenharmony_ci    } else if (rounded >= max_int64) {
11751cb0ef41Sopenharmony_ci      set_fpu_register(fd_reg(), kFPU64InvalidResult);
11761cb0ef41Sopenharmony_ci    } else if (rounded < min_int64) {
11771cb0ef41Sopenharmony_ci      set_fpu_register(fd_reg(), kFPU64InvalidResultNegative);
11781cb0ef41Sopenharmony_ci    } else {
11791cb0ef41Sopenharmony_ci      UNREACHABLE();
11801cb0ef41Sopenharmony_ci    }
11811cb0ef41Sopenharmony_ci  } else {
11821cb0ef41Sopenharmony_ci    set_fpu_register(fd_reg(), kFPU64InvalidResult);
11831cb0ef41Sopenharmony_ci  }
11841cb0ef41Sopenharmony_ci}
11851cb0ef41Sopenharmony_ci
11861cb0ef41Sopenharmony_civoid Simulator::set_fpu_register_word_invalid_result(double original,
11871cb0ef41Sopenharmony_ci                                                     double rounded) {
11881cb0ef41Sopenharmony_ci  if (FCSR_ & kFCSRNaN2008FlagMask) {
11891cb0ef41Sopenharmony_ci    double max_int32 = std::numeric_limits<int32_t>::max();
11901cb0ef41Sopenharmony_ci    double min_int32 = std::numeric_limits<int32_t>::min();
11911cb0ef41Sopenharmony_ci    if (std::isnan(original)) {
11921cb0ef41Sopenharmony_ci      set_fpu_register_word(fd_reg(), 0);
11931cb0ef41Sopenharmony_ci    } else if (rounded > max_int32) {
11941cb0ef41Sopenharmony_ci      set_fpu_register_word(fd_reg(), kFPUInvalidResult);
11951cb0ef41Sopenharmony_ci    } else if (rounded < min_int32) {
11961cb0ef41Sopenharmony_ci      set_fpu_register_word(fd_reg(), kFPUInvalidResultNegative);
11971cb0ef41Sopenharmony_ci    } else {
11981cb0ef41Sopenharmony_ci      UNREACHABLE();
11991cb0ef41Sopenharmony_ci    }
12001cb0ef41Sopenharmony_ci  } else {
12011cb0ef41Sopenharmony_ci    set_fpu_register_word(fd_reg(), kFPUInvalidResult);
12021cb0ef41Sopenharmony_ci  }
12031cb0ef41Sopenharmony_ci}
12041cb0ef41Sopenharmony_ci
12051cb0ef41Sopenharmony_civoid Simulator::set_fpu_register_invalid_result(double original,
12061cb0ef41Sopenharmony_ci                                                double rounded) {
12071cb0ef41Sopenharmony_ci  if (FCSR_ & kFCSRNaN2008FlagMask) {
12081cb0ef41Sopenharmony_ci    double max_int32 = std::numeric_limits<int32_t>::max();
12091cb0ef41Sopenharmony_ci    double min_int32 = std::numeric_limits<int32_t>::min();
12101cb0ef41Sopenharmony_ci    if (std::isnan(original)) {
12111cb0ef41Sopenharmony_ci      set_fpu_register(fd_reg(), 0);
12121cb0ef41Sopenharmony_ci    } else if (rounded > max_int32) {
12131cb0ef41Sopenharmony_ci      set_fpu_register(fd_reg(), kFPUInvalidResult);
12141cb0ef41Sopenharmony_ci    } else if (rounded < min_int32) {
12151cb0ef41Sopenharmony_ci      set_fpu_register(fd_reg(), kFPUInvalidResultNegative);
12161cb0ef41Sopenharmony_ci    } else {
12171cb0ef41Sopenharmony_ci      UNREACHABLE();
12181cb0ef41Sopenharmony_ci    }
12191cb0ef41Sopenharmony_ci  } else {
12201cb0ef41Sopenharmony_ci    set_fpu_register(fd_reg(), kFPUInvalidResult);
12211cb0ef41Sopenharmony_ci  }
12221cb0ef41Sopenharmony_ci}
12231cb0ef41Sopenharmony_ci
12241cb0ef41Sopenharmony_civoid Simulator::set_fpu_register_invalid_result64(double original,
12251cb0ef41Sopenharmony_ci                                                  double rounded) {
12261cb0ef41Sopenharmony_ci  if (FCSR_ & kFCSRNaN2008FlagMask) {
12271cb0ef41Sopenharmony_ci    // The value of INT64_MAX (2^63-1) can't be represented as double exactly,
12281cb0ef41Sopenharmony_ci    // loading the most accurate representation into max_int64, which is 2^63.
12291cb0ef41Sopenharmony_ci    double max_int64 = static_cast<double>(std::numeric_limits<int64_t>::max());
12301cb0ef41Sopenharmony_ci    double min_int64 = std::numeric_limits<int64_t>::min();
12311cb0ef41Sopenharmony_ci    if (std::isnan(original)) {
12321cb0ef41Sopenharmony_ci      set_fpu_register(fd_reg(), 0);
12331cb0ef41Sopenharmony_ci    } else if (rounded >= max_int64) {
12341cb0ef41Sopenharmony_ci      set_fpu_register(fd_reg(), kFPU64InvalidResult);
12351cb0ef41Sopenharmony_ci    } else if (rounded < min_int64) {
12361cb0ef41Sopenharmony_ci      set_fpu_register(fd_reg(), kFPU64InvalidResultNegative);
12371cb0ef41Sopenharmony_ci    } else {
12381cb0ef41Sopenharmony_ci      UNREACHABLE();
12391cb0ef41Sopenharmony_ci    }
12401cb0ef41Sopenharmony_ci  } else {
12411cb0ef41Sopenharmony_ci    set_fpu_register(fd_reg(), kFPU64InvalidResult);
12421cb0ef41Sopenharmony_ci  }
12431cb0ef41Sopenharmony_ci}
12441cb0ef41Sopenharmony_ci
12451cb0ef41Sopenharmony_ci// Sets the rounding error codes in FCSR based on the result of the rounding.
12461cb0ef41Sopenharmony_ci// Returns true if the operation was invalid.
12471cb0ef41Sopenharmony_cibool Simulator::set_fcsr_round_error(double original, double rounded) {
12481cb0ef41Sopenharmony_ci  bool ret = false;
12491cb0ef41Sopenharmony_ci  double max_int32 = std::numeric_limits<int32_t>::max();
12501cb0ef41Sopenharmony_ci  double min_int32 = std::numeric_limits<int32_t>::min();
12511cb0ef41Sopenharmony_ci
12521cb0ef41Sopenharmony_ci  clear_fcsr_cause();
12531cb0ef41Sopenharmony_ci
12541cb0ef41Sopenharmony_ci  if (!std::isfinite(original) || !std::isfinite(rounded)) {
12551cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInvalidOpFlagBit, true);
12561cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInvalidOpCauseBit, true);
12571cb0ef41Sopenharmony_ci    ret = true;
12581cb0ef41Sopenharmony_ci  }
12591cb0ef41Sopenharmony_ci
12601cb0ef41Sopenharmony_ci  if (original != rounded) {
12611cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInexactFlagBit, true);
12621cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInexactCauseBit, true);
12631cb0ef41Sopenharmony_ci  }
12641cb0ef41Sopenharmony_ci
12651cb0ef41Sopenharmony_ci  if (rounded < DBL_MIN && rounded > -DBL_MIN && rounded != 0) {
12661cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRUnderflowFlagBit, true);
12671cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRUnderflowCauseBit, true);
12681cb0ef41Sopenharmony_ci    ret = true;
12691cb0ef41Sopenharmony_ci  }
12701cb0ef41Sopenharmony_ci
12711cb0ef41Sopenharmony_ci  if (rounded > max_int32 || rounded < min_int32) {
12721cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSROverflowFlagBit, true);
12731cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSROverflowCauseBit, true);
12741cb0ef41Sopenharmony_ci    // The reference is not really clear but it seems this is required:
12751cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInvalidOpFlagBit, true);
12761cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInvalidOpCauseBit, true);
12771cb0ef41Sopenharmony_ci    ret = true;
12781cb0ef41Sopenharmony_ci  }
12791cb0ef41Sopenharmony_ci
12801cb0ef41Sopenharmony_ci  return ret;
12811cb0ef41Sopenharmony_ci}
12821cb0ef41Sopenharmony_ci
12831cb0ef41Sopenharmony_ci// Sets the rounding error codes in FCSR based on the result of the rounding.
12841cb0ef41Sopenharmony_ci// Returns true if the operation was invalid.
12851cb0ef41Sopenharmony_cibool Simulator::set_fcsr_round64_error(double original, double rounded) {
12861cb0ef41Sopenharmony_ci  bool ret = false;
12871cb0ef41Sopenharmony_ci  // The value of INT64_MAX (2^63-1) can't be represented as double exactly,
12881cb0ef41Sopenharmony_ci  // loading the most accurate representation into max_int64, which is 2^63.
12891cb0ef41Sopenharmony_ci  double max_int64 = static_cast<double>(std::numeric_limits<int64_t>::max());
12901cb0ef41Sopenharmony_ci  double min_int64 = std::numeric_limits<int64_t>::min();
12911cb0ef41Sopenharmony_ci
12921cb0ef41Sopenharmony_ci  clear_fcsr_cause();
12931cb0ef41Sopenharmony_ci
12941cb0ef41Sopenharmony_ci  if (!std::isfinite(original) || !std::isfinite(rounded)) {
12951cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInvalidOpFlagBit, true);
12961cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInvalidOpCauseBit, true);
12971cb0ef41Sopenharmony_ci    ret = true;
12981cb0ef41Sopenharmony_ci  }
12991cb0ef41Sopenharmony_ci
13001cb0ef41Sopenharmony_ci  if (original != rounded) {
13011cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInexactFlagBit, true);
13021cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInexactCauseBit, true);
13031cb0ef41Sopenharmony_ci  }
13041cb0ef41Sopenharmony_ci
13051cb0ef41Sopenharmony_ci  if (rounded < DBL_MIN && rounded > -DBL_MIN && rounded != 0) {
13061cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRUnderflowFlagBit, true);
13071cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRUnderflowCauseBit, true);
13081cb0ef41Sopenharmony_ci    ret = true;
13091cb0ef41Sopenharmony_ci  }
13101cb0ef41Sopenharmony_ci
13111cb0ef41Sopenharmony_ci  if (rounded >= max_int64 || rounded < min_int64) {
13121cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSROverflowFlagBit, true);
13131cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSROverflowCauseBit, true);
13141cb0ef41Sopenharmony_ci    // The reference is not really clear but it seems this is required:
13151cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInvalidOpFlagBit, true);
13161cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInvalidOpCauseBit, true);
13171cb0ef41Sopenharmony_ci    ret = true;
13181cb0ef41Sopenharmony_ci  }
13191cb0ef41Sopenharmony_ci
13201cb0ef41Sopenharmony_ci  return ret;
13211cb0ef41Sopenharmony_ci}
13221cb0ef41Sopenharmony_ci
13231cb0ef41Sopenharmony_ci// Sets the rounding error codes in FCSR based on the result of the rounding.
13241cb0ef41Sopenharmony_ci// Returns true if the operation was invalid.
13251cb0ef41Sopenharmony_cibool Simulator::set_fcsr_round_error(float original, float rounded) {
13261cb0ef41Sopenharmony_ci  bool ret = false;
13271cb0ef41Sopenharmony_ci  double max_int32 = std::numeric_limits<int32_t>::max();
13281cb0ef41Sopenharmony_ci  double min_int32 = std::numeric_limits<int32_t>::min();
13291cb0ef41Sopenharmony_ci
13301cb0ef41Sopenharmony_ci  clear_fcsr_cause();
13311cb0ef41Sopenharmony_ci
13321cb0ef41Sopenharmony_ci  if (!std::isfinite(original) || !std::isfinite(rounded)) {
13331cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInvalidOpFlagBit, true);
13341cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInvalidOpCauseBit, true);
13351cb0ef41Sopenharmony_ci    ret = true;
13361cb0ef41Sopenharmony_ci  }
13371cb0ef41Sopenharmony_ci
13381cb0ef41Sopenharmony_ci  if (original != rounded) {
13391cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInexactFlagBit, true);
13401cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInexactCauseBit, true);
13411cb0ef41Sopenharmony_ci  }
13421cb0ef41Sopenharmony_ci
13431cb0ef41Sopenharmony_ci  if (rounded < FLT_MIN && rounded > -FLT_MIN && rounded != 0) {
13441cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRUnderflowFlagBit, true);
13451cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRUnderflowCauseBit, true);
13461cb0ef41Sopenharmony_ci    ret = true;
13471cb0ef41Sopenharmony_ci  }
13481cb0ef41Sopenharmony_ci
13491cb0ef41Sopenharmony_ci  if (rounded > max_int32 || rounded < min_int32) {
13501cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSROverflowFlagBit, true);
13511cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSROverflowCauseBit, true);
13521cb0ef41Sopenharmony_ci    // The reference is not really clear but it seems this is required:
13531cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInvalidOpFlagBit, true);
13541cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInvalidOpCauseBit, true);
13551cb0ef41Sopenharmony_ci    ret = true;
13561cb0ef41Sopenharmony_ci  }
13571cb0ef41Sopenharmony_ci
13581cb0ef41Sopenharmony_ci  return ret;
13591cb0ef41Sopenharmony_ci}
13601cb0ef41Sopenharmony_ci
13611cb0ef41Sopenharmony_ci// Sets the rounding error codes in FCSR based on the result of the rounding.
13621cb0ef41Sopenharmony_ci// Returns true if the operation was invalid.
13631cb0ef41Sopenharmony_cibool Simulator::set_fcsr_round64_error(float original, float rounded) {
13641cb0ef41Sopenharmony_ci  bool ret = false;
13651cb0ef41Sopenharmony_ci  // The value of INT64_MAX (2^63-1) can't be represented as double exactly,
13661cb0ef41Sopenharmony_ci  // loading the most accurate representation into max_int64, which is 2^63.
13671cb0ef41Sopenharmony_ci  double max_int64 = static_cast<double>(std::numeric_limits<int64_t>::max());
13681cb0ef41Sopenharmony_ci  double min_int64 = std::numeric_limits<int64_t>::min();
13691cb0ef41Sopenharmony_ci
13701cb0ef41Sopenharmony_ci  clear_fcsr_cause();
13711cb0ef41Sopenharmony_ci
13721cb0ef41Sopenharmony_ci  if (!std::isfinite(original) || !std::isfinite(rounded)) {
13731cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInvalidOpFlagBit, true);
13741cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInvalidOpCauseBit, true);
13751cb0ef41Sopenharmony_ci    ret = true;
13761cb0ef41Sopenharmony_ci  }
13771cb0ef41Sopenharmony_ci
13781cb0ef41Sopenharmony_ci  if (original != rounded) {
13791cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInexactFlagBit, true);
13801cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInexactCauseBit, true);
13811cb0ef41Sopenharmony_ci  }
13821cb0ef41Sopenharmony_ci
13831cb0ef41Sopenharmony_ci  if (rounded < FLT_MIN && rounded > -FLT_MIN && rounded != 0) {
13841cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRUnderflowFlagBit, true);
13851cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRUnderflowCauseBit, true);
13861cb0ef41Sopenharmony_ci    ret = true;
13871cb0ef41Sopenharmony_ci  }
13881cb0ef41Sopenharmony_ci
13891cb0ef41Sopenharmony_ci  if (rounded >= max_int64 || rounded < min_int64) {
13901cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSROverflowFlagBit, true);
13911cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSROverflowCauseBit, true);
13921cb0ef41Sopenharmony_ci    // The reference is not really clear but it seems this is required:
13931cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInvalidOpFlagBit, true);
13941cb0ef41Sopenharmony_ci    set_fcsr_bit(kFCSRInvalidOpCauseBit, true);
13951cb0ef41Sopenharmony_ci    ret = true;
13961cb0ef41Sopenharmony_ci  }
13971cb0ef41Sopenharmony_ci
13981cb0ef41Sopenharmony_ci  return ret;
13991cb0ef41Sopenharmony_ci}
14001cb0ef41Sopenharmony_ci
14011cb0ef41Sopenharmony_civoid Simulator::round_according_to_fcsr(double toRound, double* rounded,
14021cb0ef41Sopenharmony_ci                                        int32_t* rounded_int, double fs) {
14031cb0ef41Sopenharmony_ci  // 0 RN (round to nearest): Round a result to the nearest
14041cb0ef41Sopenharmony_ci  // representable value; if the result is exactly halfway between
14051cb0ef41Sopenharmony_ci  // two representable values, round to zero. Behave like round_w_d.
14061cb0ef41Sopenharmony_ci
14071cb0ef41Sopenharmony_ci  // 1 RZ (round toward zero): Round a result to the closest
14081cb0ef41Sopenharmony_ci  // representable value whose absolute value is less than or
14091cb0ef41Sopenharmony_ci  // equal to the infinitely accurate result. Behave like trunc_w_d.
14101cb0ef41Sopenharmony_ci
14111cb0ef41Sopenharmony_ci  // 2 RP (round up, or toward  infinity): Round a result to the
14121cb0ef41Sopenharmony_ci  // next representable value up. Behave like ceil_w_d.
14131cb0ef41Sopenharmony_ci
14141cb0ef41Sopenharmony_ci  // 3 RD (round down, or toward −infinity): Round a result to
14151cb0ef41Sopenharmony_ci  // the next representable value down. Behave like floor_w_d.
14161cb0ef41Sopenharmony_ci  switch (get_fcsr_rounding_mode()) {
14171cb0ef41Sopenharmony_ci    case kRoundToNearest:
14181cb0ef41Sopenharmony_ci      *rounded = std::floor(fs + 0.5);
14191cb0ef41Sopenharmony_ci      *rounded_int = static_cast<int32_t>(*rounded);
14201cb0ef41Sopenharmony_ci      if ((*rounded_int & 1) != 0 && *rounded_int - fs == 0.5) {
14211cb0ef41Sopenharmony_ci        // If the number is halfway between two integers,
14221cb0ef41Sopenharmony_ci        // round to the even one.
14231cb0ef41Sopenharmony_ci        *rounded_int -= 1;
14241cb0ef41Sopenharmony_ci        *rounded -= 1.;
14251cb0ef41Sopenharmony_ci      }
14261cb0ef41Sopenharmony_ci      break;
14271cb0ef41Sopenharmony_ci    case kRoundToZero:
14281cb0ef41Sopenharmony_ci      *rounded = trunc(fs);
14291cb0ef41Sopenharmony_ci      *rounded_int = static_cast<int32_t>(*rounded);
14301cb0ef41Sopenharmony_ci      break;
14311cb0ef41Sopenharmony_ci    case kRoundToPlusInf:
14321cb0ef41Sopenharmony_ci      *rounded = std::ceil(fs);
14331cb0ef41Sopenharmony_ci      *rounded_int = static_cast<int32_t>(*rounded);
14341cb0ef41Sopenharmony_ci      break;
14351cb0ef41Sopenharmony_ci    case kRoundToMinusInf:
14361cb0ef41Sopenharmony_ci      *rounded = std::floor(fs);
14371cb0ef41Sopenharmony_ci      *rounded_int = static_cast<int32_t>(*rounded);
14381cb0ef41Sopenharmony_ci      break;
14391cb0ef41Sopenharmony_ci  }
14401cb0ef41Sopenharmony_ci}
14411cb0ef41Sopenharmony_ci
14421cb0ef41Sopenharmony_civoid Simulator::round_according_to_fcsr(float toRound, float* rounded,
14431cb0ef41Sopenharmony_ci                                        int32_t* rounded_int, float fs) {
14441cb0ef41Sopenharmony_ci  // 0 RN (round to nearest): Round a result to the nearest
14451cb0ef41Sopenharmony_ci  // representable value; if the result is exactly halfway between
14461cb0ef41Sopenharmony_ci  // two representable values, round to zero. Behave like round_w_d.
14471cb0ef41Sopenharmony_ci
14481cb0ef41Sopenharmony_ci  // 1 RZ (round toward zero): Round a result to the closest
14491cb0ef41Sopenharmony_ci  // representable value whose absolute value is less than or
14501cb0ef41Sopenharmony_ci  // equal to the infinitely accurate result. Behave like trunc_w_d.
14511cb0ef41Sopenharmony_ci
14521cb0ef41Sopenharmony_ci  // 2 RP (round up, or toward  infinity): Round a result to the
14531cb0ef41Sopenharmony_ci  // next representable value up. Behave like ceil_w_d.
14541cb0ef41Sopenharmony_ci
14551cb0ef41Sopenharmony_ci  // 3 RD (round down, or toward −infinity): Round a result to
14561cb0ef41Sopenharmony_ci  // the next representable value down. Behave like floor_w_d.
14571cb0ef41Sopenharmony_ci  switch (get_fcsr_rounding_mode()) {
14581cb0ef41Sopenharmony_ci    case kRoundToNearest:
14591cb0ef41Sopenharmony_ci      *rounded = std::floor(fs + 0.5);
14601cb0ef41Sopenharmony_ci      *rounded_int = static_cast<int32_t>(*rounded);
14611cb0ef41Sopenharmony_ci      if ((*rounded_int & 1) != 0 && *rounded_int - fs == 0.5) {
14621cb0ef41Sopenharmony_ci        // If the number is halfway between two integers,
14631cb0ef41Sopenharmony_ci        // round to the even one.
14641cb0ef41Sopenharmony_ci        *rounded_int -= 1;
14651cb0ef41Sopenharmony_ci        *rounded -= 1.f;
14661cb0ef41Sopenharmony_ci      }
14671cb0ef41Sopenharmony_ci      break;
14681cb0ef41Sopenharmony_ci    case kRoundToZero:
14691cb0ef41Sopenharmony_ci      *rounded = trunc(fs);
14701cb0ef41Sopenharmony_ci      *rounded_int = static_cast<int32_t>(*rounded);
14711cb0ef41Sopenharmony_ci      break;
14721cb0ef41Sopenharmony_ci    case kRoundToPlusInf:
14731cb0ef41Sopenharmony_ci      *rounded = std::ceil(fs);
14741cb0ef41Sopenharmony_ci      *rounded_int = static_cast<int32_t>(*rounded);
14751cb0ef41Sopenharmony_ci      break;
14761cb0ef41Sopenharmony_ci    case kRoundToMinusInf:
14771cb0ef41Sopenharmony_ci      *rounded = std::floor(fs);
14781cb0ef41Sopenharmony_ci      *rounded_int = static_cast<int32_t>(*rounded);
14791cb0ef41Sopenharmony_ci      break;
14801cb0ef41Sopenharmony_ci  }
14811cb0ef41Sopenharmony_ci}
14821cb0ef41Sopenharmony_ci
14831cb0ef41Sopenharmony_citemplate <typename T_fp, typename T_int>
14841cb0ef41Sopenharmony_civoid Simulator::round_according_to_msacsr(T_fp toRound, T_fp* rounded,
14851cb0ef41Sopenharmony_ci                                          T_int* rounded_int) {
14861cb0ef41Sopenharmony_ci  // 0 RN (round to nearest): Round a result to the nearest
14871cb0ef41Sopenharmony_ci  // representable value; if the result is exactly halfway between
14881cb0ef41Sopenharmony_ci  // two representable values, round to zero. Behave like round_w_d.
14891cb0ef41Sopenharmony_ci
14901cb0ef41Sopenharmony_ci  // 1 RZ (round toward zero): Round a result to the closest
14911cb0ef41Sopenharmony_ci  // representable value whose absolute value is less than or
14921cb0ef41Sopenharmony_ci  // equal to the infinitely accurate result. Behave like trunc_w_d.
14931cb0ef41Sopenharmony_ci
14941cb0ef41Sopenharmony_ci  // 2 RP (round up, or toward  infinity): Round a result to the
14951cb0ef41Sopenharmony_ci  // next representable value up. Behave like ceil_w_d.
14961cb0ef41Sopenharmony_ci
14971cb0ef41Sopenharmony_ci  // 3 RD (round down, or toward −infinity): Round a result to
14981cb0ef41Sopenharmony_ci  // the next representable value down. Behave like floor_w_d.
14991cb0ef41Sopenharmony_ci  switch (get_msacsr_rounding_mode()) {
15001cb0ef41Sopenharmony_ci    case kRoundToNearest:
15011cb0ef41Sopenharmony_ci      *rounded = std::floor(toRound + 0.5);
15021cb0ef41Sopenharmony_ci      *rounded_int = static_cast<T_int>(*rounded);
15031cb0ef41Sopenharmony_ci      if ((*rounded_int & 1) != 0 && *rounded_int - toRound == 0.5) {
15041cb0ef41Sopenharmony_ci        // If the number is halfway between two integers,
15051cb0ef41Sopenharmony_ci        // round to the even one.
15061cb0ef41Sopenharmony_ci        *rounded_int -= 1;
15071cb0ef41Sopenharmony_ci        *rounded -= 1;
15081cb0ef41Sopenharmony_ci      }
15091cb0ef41Sopenharmony_ci      break;
15101cb0ef41Sopenharmony_ci    case kRoundToZero:
15111cb0ef41Sopenharmony_ci      *rounded = trunc(toRound);
15121cb0ef41Sopenharmony_ci      *rounded_int = static_cast<T_int>(*rounded);
15131cb0ef41Sopenharmony_ci      break;
15141cb0ef41Sopenharmony_ci    case kRoundToPlusInf:
15151cb0ef41Sopenharmony_ci      *rounded = std::ceil(toRound);
15161cb0ef41Sopenharmony_ci      *rounded_int = static_cast<T_int>(*rounded);
15171cb0ef41Sopenharmony_ci      break;
15181cb0ef41Sopenharmony_ci    case kRoundToMinusInf:
15191cb0ef41Sopenharmony_ci      *rounded = std::floor(toRound);
15201cb0ef41Sopenharmony_ci      *rounded_int = static_cast<T_int>(*rounded);
15211cb0ef41Sopenharmony_ci      break;
15221cb0ef41Sopenharmony_ci  }
15231cb0ef41Sopenharmony_ci}
15241cb0ef41Sopenharmony_ci
15251cb0ef41Sopenharmony_civoid Simulator::round64_according_to_fcsr(double toRound, double* rounded,
15261cb0ef41Sopenharmony_ci                                          int64_t* rounded_int, double fs) {
15271cb0ef41Sopenharmony_ci  // 0 RN (round to nearest): Round a result to the nearest
15281cb0ef41Sopenharmony_ci  // representable value; if the result is exactly halfway between
15291cb0ef41Sopenharmony_ci  // two representable values, round to zero. Behave like round_w_d.
15301cb0ef41Sopenharmony_ci
15311cb0ef41Sopenharmony_ci  // 1 RZ (round toward zero): Round a result to the closest
15321cb0ef41Sopenharmony_ci  // representable value whose absolute value is less than or.
15331cb0ef41Sopenharmony_ci  // equal to the infinitely accurate result. Behave like trunc_w_d.
15341cb0ef41Sopenharmony_ci
15351cb0ef41Sopenharmony_ci  // 2 RP (round up, or toward +infinity): Round a result to the
15361cb0ef41Sopenharmony_ci  // next representable value up. Behave like ceil_w_d.
15371cb0ef41Sopenharmony_ci
15381cb0ef41Sopenharmony_ci  // 3 RN (round down, or toward −infinity): Round a result to
15391cb0ef41Sopenharmony_ci  // the next representable value down. Behave like floor_w_d.
15401cb0ef41Sopenharmony_ci  switch (FCSR_ & 3) {
15411cb0ef41Sopenharmony_ci    case kRoundToNearest:
15421cb0ef41Sopenharmony_ci      *rounded = std::floor(fs + 0.5);
15431cb0ef41Sopenharmony_ci      *rounded_int = static_cast<int64_t>(*rounded);
15441cb0ef41Sopenharmony_ci      if ((*rounded_int & 1) != 0 && *rounded_int - fs == 0.5) {
15451cb0ef41Sopenharmony_ci        // If the number is halfway between two integers,
15461cb0ef41Sopenharmony_ci        // round to the even one.
15471cb0ef41Sopenharmony_ci        *rounded_int -= 1;
15481cb0ef41Sopenharmony_ci        *rounded -= 1.;
15491cb0ef41Sopenharmony_ci      }
15501cb0ef41Sopenharmony_ci      break;
15511cb0ef41Sopenharmony_ci    case kRoundToZero:
15521cb0ef41Sopenharmony_ci      *rounded = trunc(fs);
15531cb0ef41Sopenharmony_ci      *rounded_int = static_cast<int64_t>(*rounded);
15541cb0ef41Sopenharmony_ci      break;
15551cb0ef41Sopenharmony_ci    case kRoundToPlusInf:
15561cb0ef41Sopenharmony_ci      *rounded = std::ceil(fs);
15571cb0ef41Sopenharmony_ci      *rounded_int = static_cast<int64_t>(*rounded);
15581cb0ef41Sopenharmony_ci      break;
15591cb0ef41Sopenharmony_ci    case kRoundToMinusInf:
15601cb0ef41Sopenharmony_ci      *rounded = std::floor(fs);
15611cb0ef41Sopenharmony_ci      *rounded_int = static_cast<int64_t>(*rounded);
15621cb0ef41Sopenharmony_ci      break;
15631cb0ef41Sopenharmony_ci  }
15641cb0ef41Sopenharmony_ci}
15651cb0ef41Sopenharmony_ci
15661cb0ef41Sopenharmony_civoid Simulator::round64_according_to_fcsr(float toRound, float* rounded,
15671cb0ef41Sopenharmony_ci                                          int64_t* rounded_int, float fs) {
15681cb0ef41Sopenharmony_ci  // 0 RN (round to nearest): Round a result to the nearest
15691cb0ef41Sopenharmony_ci  // representable value; if the result is exactly halfway between
15701cb0ef41Sopenharmony_ci  // two representable values, round to zero. Behave like round_w_d.
15711cb0ef41Sopenharmony_ci
15721cb0ef41Sopenharmony_ci  // 1 RZ (round toward zero): Round a result to the closest
15731cb0ef41Sopenharmony_ci  // representable value whose absolute value is less than or.
15741cb0ef41Sopenharmony_ci  // equal to the infinitely accurate result. Behave like trunc_w_d.
15751cb0ef41Sopenharmony_ci
15761cb0ef41Sopenharmony_ci  // 2 RP (round up, or toward +infinity): Round a result to the
15771cb0ef41Sopenharmony_ci  // next representable value up. Behave like ceil_w_d.
15781cb0ef41Sopenharmony_ci
15791cb0ef41Sopenharmony_ci  // 3 RN (round down, or toward −infinity): Round a result to
15801cb0ef41Sopenharmony_ci  // the next representable value down. Behave like floor_w_d.
15811cb0ef41Sopenharmony_ci  switch (FCSR_ & 3) {
15821cb0ef41Sopenharmony_ci    case kRoundToNearest:
15831cb0ef41Sopenharmony_ci      *rounded = std::floor(fs + 0.5);
15841cb0ef41Sopenharmony_ci      *rounded_int = static_cast<int64_t>(*rounded);
15851cb0ef41Sopenharmony_ci      if ((*rounded_int & 1) != 0 && *rounded_int - fs == 0.5) {
15861cb0ef41Sopenharmony_ci        // If the number is halfway between two integers,
15871cb0ef41Sopenharmony_ci        // round to the even one.
15881cb0ef41Sopenharmony_ci        *rounded_int -= 1;
15891cb0ef41Sopenharmony_ci        *rounded -= 1.f;
15901cb0ef41Sopenharmony_ci      }
15911cb0ef41Sopenharmony_ci      break;
15921cb0ef41Sopenharmony_ci    case kRoundToZero:
15931cb0ef41Sopenharmony_ci      *rounded = trunc(fs);
15941cb0ef41Sopenharmony_ci      *rounded_int = static_cast<int64_t>(*rounded);
15951cb0ef41Sopenharmony_ci      break;
15961cb0ef41Sopenharmony_ci    case kRoundToPlusInf:
15971cb0ef41Sopenharmony_ci      *rounded = std::ceil(fs);
15981cb0ef41Sopenharmony_ci      *rounded_int = static_cast<int64_t>(*rounded);
15991cb0ef41Sopenharmony_ci      break;
16001cb0ef41Sopenharmony_ci    case kRoundToMinusInf:
16011cb0ef41Sopenharmony_ci      *rounded = std::floor(fs);
16021cb0ef41Sopenharmony_ci      *rounded_int = static_cast<int64_t>(*rounded);
16031cb0ef41Sopenharmony_ci      break;
16041cb0ef41Sopenharmony_ci  }
16051cb0ef41Sopenharmony_ci}
16061cb0ef41Sopenharmony_ci
16071cb0ef41Sopenharmony_ci// Raw access to the PC register.
16081cb0ef41Sopenharmony_civoid Simulator::set_pc(int32_t value) {
16091cb0ef41Sopenharmony_ci  pc_modified_ = true;
16101cb0ef41Sopenharmony_ci  registers_[pc] = value;
16111cb0ef41Sopenharmony_ci}
16121cb0ef41Sopenharmony_ci
16131cb0ef41Sopenharmony_cibool Simulator::has_bad_pc() const {
16141cb0ef41Sopenharmony_ci  return ((registers_[pc] == bad_ra) || (registers_[pc] == end_sim_pc));
16151cb0ef41Sopenharmony_ci}
16161cb0ef41Sopenharmony_ci
16171cb0ef41Sopenharmony_ci// Raw access to the PC register without the special adjustment when reading.
16181cb0ef41Sopenharmony_ciint32_t Simulator::get_pc() const { return registers_[pc]; }
16191cb0ef41Sopenharmony_ci
16201cb0ef41Sopenharmony_ci// The MIPS cannot do unaligned reads and writes.  On some MIPS platforms an
16211cb0ef41Sopenharmony_ci// interrupt is caused.  On others it does a funky rotation thing.  For now we
16221cb0ef41Sopenharmony_ci// simply disallow unaligned reads, but at some point we may want to move to
16231cb0ef41Sopenharmony_ci// emulating the rotate behaviour.  Note that simulator runs have the runtime
16241cb0ef41Sopenharmony_ci// system running directly on the host system and only generated code is
16251cb0ef41Sopenharmony_ci// executed in the simulator.  Since the host is typically IA32 we will not
16261cb0ef41Sopenharmony_ci// get the correct MIPS-like behaviour on unaligned accesses.
16271cb0ef41Sopenharmony_ci
16281cb0ef41Sopenharmony_civoid Simulator::TraceRegWr(int32_t value, TraceType t) {
16291cb0ef41Sopenharmony_ci  if (::v8::internal::FLAG_trace_sim) {
16301cb0ef41Sopenharmony_ci    union {
16311cb0ef41Sopenharmony_ci      int32_t fmt_int32;
16321cb0ef41Sopenharmony_ci      float fmt_float;
16331cb0ef41Sopenharmony_ci    } v;
16341cb0ef41Sopenharmony_ci    v.fmt_int32 = value;
16351cb0ef41Sopenharmony_ci
16361cb0ef41Sopenharmony_ci    switch (t) {
16371cb0ef41Sopenharmony_ci      case WORD:
16381cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
16391cb0ef41Sopenharmony_ci                 "%08" PRIx32 "    (%" PRIu64 ")    int32:%" PRId32
16401cb0ef41Sopenharmony_ci                 " uint32:%" PRIu32,
16411cb0ef41Sopenharmony_ci                 value, icount_, value, value);
16421cb0ef41Sopenharmony_ci        break;
16431cb0ef41Sopenharmony_ci      case FLOAT:
16441cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_, "%08" PRIx32 "    (%" PRIu64 ")    flt:%e",
16451cb0ef41Sopenharmony_ci                 v.fmt_int32, icount_, v.fmt_float);
16461cb0ef41Sopenharmony_ci        break;
16471cb0ef41Sopenharmony_ci      default:
16481cb0ef41Sopenharmony_ci        UNREACHABLE();
16491cb0ef41Sopenharmony_ci    }
16501cb0ef41Sopenharmony_ci  }
16511cb0ef41Sopenharmony_ci}
16521cb0ef41Sopenharmony_ci
16531cb0ef41Sopenharmony_civoid Simulator::TraceRegWr(int64_t value, TraceType t) {
16541cb0ef41Sopenharmony_ci  if (::v8::internal::FLAG_trace_sim) {
16551cb0ef41Sopenharmony_ci    union {
16561cb0ef41Sopenharmony_ci      int64_t fmt_int64;
16571cb0ef41Sopenharmony_ci      double fmt_double;
16581cb0ef41Sopenharmony_ci    } v;
16591cb0ef41Sopenharmony_ci    v.fmt_int64 = value;
16601cb0ef41Sopenharmony_ci
16611cb0ef41Sopenharmony_ci    switch (t) {
16621cb0ef41Sopenharmony_ci      case DWORD:
16631cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
16641cb0ef41Sopenharmony_ci                 "%016" PRIx64 "    (%" PRIu64 ")    int64:%" PRId64
16651cb0ef41Sopenharmony_ci                 " uint64:%" PRIu64,
16661cb0ef41Sopenharmony_ci                 value, icount_, value, value);
16671cb0ef41Sopenharmony_ci        break;
16681cb0ef41Sopenharmony_ci      case DOUBLE:
16691cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_, "%016" PRIx64 "    (%" PRIu64 ")    dbl:%e",
16701cb0ef41Sopenharmony_ci                 v.fmt_int64, icount_, v.fmt_double);
16711cb0ef41Sopenharmony_ci        break;
16721cb0ef41Sopenharmony_ci      default:
16731cb0ef41Sopenharmony_ci        UNREACHABLE();
16741cb0ef41Sopenharmony_ci    }
16751cb0ef41Sopenharmony_ci  }
16761cb0ef41Sopenharmony_ci}
16771cb0ef41Sopenharmony_ci
16781cb0ef41Sopenharmony_citemplate <typename T>
16791cb0ef41Sopenharmony_civoid Simulator::TraceMSARegWr(T* value, TraceType t) {
16801cb0ef41Sopenharmony_ci  if (::v8::internal::FLAG_trace_sim) {
16811cb0ef41Sopenharmony_ci    union {
16821cb0ef41Sopenharmony_ci      uint8_t b[16];
16831cb0ef41Sopenharmony_ci      uint16_t h[8];
16841cb0ef41Sopenharmony_ci      uint32_t w[4];
16851cb0ef41Sopenharmony_ci      uint64_t d[2];
16861cb0ef41Sopenharmony_ci      float f[4];
16871cb0ef41Sopenharmony_ci      double df[2];
16881cb0ef41Sopenharmony_ci    } v;
16891cb0ef41Sopenharmony_ci    memcpy(v.b, value, kSimd128Size);
16901cb0ef41Sopenharmony_ci    switch (t) {
16911cb0ef41Sopenharmony_ci      case BYTE:
16921cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
16931cb0ef41Sopenharmony_ci                 "LO: %016" PRIx64 "  HI: %016" PRIx64 "    (%" PRIu64 ")",
16941cb0ef41Sopenharmony_ci                 v.d[0], v.d[1], icount_);
16951cb0ef41Sopenharmony_ci        break;
16961cb0ef41Sopenharmony_ci      case HALF:
16971cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
16981cb0ef41Sopenharmony_ci                 "LO: %016" PRIx64 "  HI: %016" PRIx64 "    (%" PRIu64 ")",
16991cb0ef41Sopenharmony_ci                 v.d[0], v.d[1], icount_);
17001cb0ef41Sopenharmony_ci        break;
17011cb0ef41Sopenharmony_ci      case WORD:
17021cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
17031cb0ef41Sopenharmony_ci                 "LO: %016" PRIx64 "  HI: %016" PRIx64 "    (%" PRIu64
17041cb0ef41Sopenharmony_ci                 ")    int32[0..3]:%" PRId32 "  %" PRId32 "  %" PRId32
17051cb0ef41Sopenharmony_ci                 "  %" PRId32,
17061cb0ef41Sopenharmony_ci                 v.d[0], v.d[1], icount_, v.w[0], v.w[1], v.w[2], v.w[3]);
17071cb0ef41Sopenharmony_ci        break;
17081cb0ef41Sopenharmony_ci      case DWORD:
17091cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
17101cb0ef41Sopenharmony_ci                 "LO: %016" PRIx64 "  HI: %016" PRIx64 "    (%" PRIu64 ")",
17111cb0ef41Sopenharmony_ci                 v.d[0], v.d[1], icount_);
17121cb0ef41Sopenharmony_ci        break;
17131cb0ef41Sopenharmony_ci      case FLOAT:
17141cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
17151cb0ef41Sopenharmony_ci                 "LO: %016" PRIx64 "  HI: %016" PRIx64 "    (%" PRIu64
17161cb0ef41Sopenharmony_ci                 ")    flt[0..3]:%e  %e  %e  %e",
17171cb0ef41Sopenharmony_ci                 v.d[0], v.d[1], icount_, v.f[0], v.f[1], v.f[2], v.f[3]);
17181cb0ef41Sopenharmony_ci        break;
17191cb0ef41Sopenharmony_ci      case DOUBLE:
17201cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
17211cb0ef41Sopenharmony_ci                 "LO: %016" PRIx64 "  HI: %016" PRIx64 "    (%" PRIu64
17221cb0ef41Sopenharmony_ci                 ")    dbl[0..1]:%e  %e",
17231cb0ef41Sopenharmony_ci                 v.d[0], v.d[1], icount_, v.df[0], v.df[1]);
17241cb0ef41Sopenharmony_ci        break;
17251cb0ef41Sopenharmony_ci      default:
17261cb0ef41Sopenharmony_ci        UNREACHABLE();
17271cb0ef41Sopenharmony_ci    }
17281cb0ef41Sopenharmony_ci  }
17291cb0ef41Sopenharmony_ci}
17301cb0ef41Sopenharmony_ci
17311cb0ef41Sopenharmony_citemplate <typename T>
17321cb0ef41Sopenharmony_civoid Simulator::TraceMSARegWr(T* value) {
17331cb0ef41Sopenharmony_ci  if (::v8::internal::FLAG_trace_sim) {
17341cb0ef41Sopenharmony_ci    union {
17351cb0ef41Sopenharmony_ci      uint8_t b[kMSALanesByte];
17361cb0ef41Sopenharmony_ci      uint16_t h[kMSALanesHalf];
17371cb0ef41Sopenharmony_ci      uint32_t w[kMSALanesWord];
17381cb0ef41Sopenharmony_ci      uint64_t d[kMSALanesDword];
17391cb0ef41Sopenharmony_ci      float f[kMSALanesWord];
17401cb0ef41Sopenharmony_ci      double df[kMSALanesDword];
17411cb0ef41Sopenharmony_ci    } v;
17421cb0ef41Sopenharmony_ci    memcpy(v.b, value, kMSALanesByte);
17431cb0ef41Sopenharmony_ci
17441cb0ef41Sopenharmony_ci    if (std::is_same<T, int32_t>::value) {
17451cb0ef41Sopenharmony_ci      SNPrintF(trace_buf_,
17461cb0ef41Sopenharmony_ci               "LO: %016" PRIx64 "  HI: %016" PRIx64 "    (%" PRIu64
17471cb0ef41Sopenharmony_ci               ")    int32[0..3]:%" PRId32 "  %" PRId32 "  %" PRId32
17481cb0ef41Sopenharmony_ci               "  %" PRId32,
17491cb0ef41Sopenharmony_ci               v.d[0], v.d[1], icount_, v.w[0], v.w[1], v.w[2], v.w[3]);
17501cb0ef41Sopenharmony_ci    } else if (std::is_same<T, float>::value) {
17511cb0ef41Sopenharmony_ci      SNPrintF(trace_buf_,
17521cb0ef41Sopenharmony_ci               "LO: %016" PRIx64 "  HI: %016" PRIx64 "    (%" PRIu64
17531cb0ef41Sopenharmony_ci               ")    flt[0..3]:%e  %e  %e  %e",
17541cb0ef41Sopenharmony_ci               v.d[0], v.d[1], icount_, v.f[0], v.f[1], v.f[2], v.f[3]);
17551cb0ef41Sopenharmony_ci    } else if (std::is_same<T, double>::value) {
17561cb0ef41Sopenharmony_ci      SNPrintF(trace_buf_,
17571cb0ef41Sopenharmony_ci               "LO: %016" PRIx64 "  HI: %016" PRIx64 "    (%" PRIu64
17581cb0ef41Sopenharmony_ci               ")    dbl[0..1]:%e  %e",
17591cb0ef41Sopenharmony_ci               v.d[0], v.d[1], icount_, v.df[0], v.df[1]);
17601cb0ef41Sopenharmony_ci    } else {
17611cb0ef41Sopenharmony_ci      SNPrintF(trace_buf_,
17621cb0ef41Sopenharmony_ci               "LO: %016" PRIx64 "  HI: %016" PRIx64 "    (%" PRIu64 ")",
17631cb0ef41Sopenharmony_ci               v.d[0], v.d[1], icount_);
17641cb0ef41Sopenharmony_ci    }
17651cb0ef41Sopenharmony_ci  }
17661cb0ef41Sopenharmony_ci}
17671cb0ef41Sopenharmony_ci
17681cb0ef41Sopenharmony_ci// TODO(plind): consider making icount_ printing a flag option.
17691cb0ef41Sopenharmony_civoid Simulator::TraceMemRd(int32_t addr, int32_t value, TraceType t) {
17701cb0ef41Sopenharmony_ci  if (::v8::internal::FLAG_trace_sim) {
17711cb0ef41Sopenharmony_ci    union {
17721cb0ef41Sopenharmony_ci      int32_t fmt_int32;
17731cb0ef41Sopenharmony_ci      float fmt_float;
17741cb0ef41Sopenharmony_ci    } v;
17751cb0ef41Sopenharmony_ci    v.fmt_int32 = value;
17761cb0ef41Sopenharmony_ci
17771cb0ef41Sopenharmony_ci    switch (t) {
17781cb0ef41Sopenharmony_ci      case WORD:
17791cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
17801cb0ef41Sopenharmony_ci                 "%08" PRIx32 " <-- [%08" PRIx32 "]    (%" PRIu64
17811cb0ef41Sopenharmony_ci                 ")    int32:%" PRId32 " uint32:%" PRIu32,
17821cb0ef41Sopenharmony_ci                 value, addr, icount_, value, value);
17831cb0ef41Sopenharmony_ci        break;
17841cb0ef41Sopenharmony_ci      case FLOAT:
17851cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
17861cb0ef41Sopenharmony_ci                 "%08" PRIx32 " <-- [%08" PRIx32 "]    (%" PRIu64 ")    flt:%e",
17871cb0ef41Sopenharmony_ci                 v.fmt_int32, addr, icount_, v.fmt_float);
17881cb0ef41Sopenharmony_ci        break;
17891cb0ef41Sopenharmony_ci      default:
17901cb0ef41Sopenharmony_ci        UNREACHABLE();
17911cb0ef41Sopenharmony_ci    }
17921cb0ef41Sopenharmony_ci  }
17931cb0ef41Sopenharmony_ci}
17941cb0ef41Sopenharmony_ci
17951cb0ef41Sopenharmony_civoid Simulator::TraceMemWr(int32_t addr, int32_t value, TraceType t) {
17961cb0ef41Sopenharmony_ci  if (::v8::internal::FLAG_trace_sim) {
17971cb0ef41Sopenharmony_ci    switch (t) {
17981cb0ef41Sopenharmony_ci      case BYTE:
17991cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
18001cb0ef41Sopenharmony_ci                 "      %02" PRIx8 " --> [%08" PRIx32 "]    (%" PRIu64 ")",
18011cb0ef41Sopenharmony_ci                 static_cast<uint8_t>(value), addr, icount_);
18021cb0ef41Sopenharmony_ci        break;
18031cb0ef41Sopenharmony_ci      case HALF:
18041cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
18051cb0ef41Sopenharmony_ci                 "    %04" PRIx16 " --> [%08" PRIx32 "]    (%" PRIu64 ")",
18061cb0ef41Sopenharmony_ci                 static_cast<uint16_t>(value), addr, icount_);
18071cb0ef41Sopenharmony_ci        break;
18081cb0ef41Sopenharmony_ci      case WORD:
18091cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
18101cb0ef41Sopenharmony_ci                 "%08" PRIx32 " --> [%08" PRIx32 "]    (%" PRIu64 ")", value,
18111cb0ef41Sopenharmony_ci                 addr, icount_);
18121cb0ef41Sopenharmony_ci        break;
18131cb0ef41Sopenharmony_ci      default:
18141cb0ef41Sopenharmony_ci        UNREACHABLE();
18151cb0ef41Sopenharmony_ci    }
18161cb0ef41Sopenharmony_ci  }
18171cb0ef41Sopenharmony_ci}
18181cb0ef41Sopenharmony_ci
18191cb0ef41Sopenharmony_citemplate <typename T>
18201cb0ef41Sopenharmony_civoid Simulator::TraceMemRd(int32_t addr, T value) {
18211cb0ef41Sopenharmony_ci  if (::v8::internal::FLAG_trace_sim) {
18221cb0ef41Sopenharmony_ci    switch (sizeof(T)) {
18231cb0ef41Sopenharmony_ci      case 1:
18241cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
18251cb0ef41Sopenharmony_ci                 "%08" PRIx8 " <-- [%08" PRIx32 "]    (%" PRIu64
18261cb0ef41Sopenharmony_ci                 ")    int8:%" PRId8 " uint8:%" PRIu8,
18271cb0ef41Sopenharmony_ci                 static_cast<uint8_t>(value), addr, icount_,
18281cb0ef41Sopenharmony_ci                 static_cast<int8_t>(value), static_cast<uint8_t>(value));
18291cb0ef41Sopenharmony_ci        break;
18301cb0ef41Sopenharmony_ci      case 2:
18311cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
18321cb0ef41Sopenharmony_ci                 "%08" PRIx16 " <-- [%08" PRIx32 "]    (%" PRIu64
18331cb0ef41Sopenharmony_ci                 ")    int16:%" PRId16 " uint16:%" PRIu16,
18341cb0ef41Sopenharmony_ci                 static_cast<uint16_t>(value), addr, icount_,
18351cb0ef41Sopenharmony_ci                 static_cast<int16_t>(value), static_cast<uint16_t>(value));
18361cb0ef41Sopenharmony_ci        break;
18371cb0ef41Sopenharmony_ci      case 4:
18381cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
18391cb0ef41Sopenharmony_ci                 "%08" PRIx32 " <-- [%08" PRIx32 "]    (%" PRIu64
18401cb0ef41Sopenharmony_ci                 ")    int32:%" PRId32 " uint32:%" PRIu32,
18411cb0ef41Sopenharmony_ci                 static_cast<uint32_t>(value), addr, icount_,
18421cb0ef41Sopenharmony_ci                 static_cast<int32_t>(value), static_cast<uint32_t>(value));
18431cb0ef41Sopenharmony_ci        break;
18441cb0ef41Sopenharmony_ci      case 8:
18451cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
18461cb0ef41Sopenharmony_ci                 "%08" PRIx64 " <-- [%08" PRIx32 "]    (%" PRIu64
18471cb0ef41Sopenharmony_ci                 ")    int64:%" PRId64 " uint64:%" PRIu64,
18481cb0ef41Sopenharmony_ci                 static_cast<uint64_t>(value), addr, icount_,
18491cb0ef41Sopenharmony_ci                 static_cast<int64_t>(value), static_cast<uint64_t>(value));
18501cb0ef41Sopenharmony_ci        break;
18511cb0ef41Sopenharmony_ci      default:
18521cb0ef41Sopenharmony_ci        UNREACHABLE();
18531cb0ef41Sopenharmony_ci    }
18541cb0ef41Sopenharmony_ci  }
18551cb0ef41Sopenharmony_ci}
18561cb0ef41Sopenharmony_ci
18571cb0ef41Sopenharmony_citemplate <typename T>
18581cb0ef41Sopenharmony_civoid Simulator::TraceMemWr(int32_t addr, T value) {
18591cb0ef41Sopenharmony_ci  if (::v8::internal::FLAG_trace_sim) {
18601cb0ef41Sopenharmony_ci    switch (sizeof(T)) {
18611cb0ef41Sopenharmony_ci      case 1:
18621cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
18631cb0ef41Sopenharmony_ci                 "      %02" PRIx8 " --> [%08" PRIx32 "]    (%" PRIu64 ")",
18641cb0ef41Sopenharmony_ci                 static_cast<uint8_t>(value), addr, icount_);
18651cb0ef41Sopenharmony_ci        break;
18661cb0ef41Sopenharmony_ci      case 2:
18671cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
18681cb0ef41Sopenharmony_ci                 "    %04" PRIx16 " --> [%08" PRIx32 "]    (%" PRIu64 ")",
18691cb0ef41Sopenharmony_ci                 static_cast<uint16_t>(value), addr, icount_);
18701cb0ef41Sopenharmony_ci        break;
18711cb0ef41Sopenharmony_ci      case 4:
18721cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
18731cb0ef41Sopenharmony_ci                 "%08" PRIx32 " --> [%08" PRIx32 "]    (%" PRIu64 ")",
18741cb0ef41Sopenharmony_ci                 static_cast<uint32_t>(value), addr, icount_);
18751cb0ef41Sopenharmony_ci        break;
18761cb0ef41Sopenharmony_ci      case 8:
18771cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
18781cb0ef41Sopenharmony_ci                 "%16" PRIx64 " --> [%08" PRIx32 "]    (%" PRIu64 ")",
18791cb0ef41Sopenharmony_ci                 static_cast<uint64_t>(value), addr, icount_);
18801cb0ef41Sopenharmony_ci        break;
18811cb0ef41Sopenharmony_ci      default:
18821cb0ef41Sopenharmony_ci        UNREACHABLE();
18831cb0ef41Sopenharmony_ci    }
18841cb0ef41Sopenharmony_ci  }
18851cb0ef41Sopenharmony_ci}
18861cb0ef41Sopenharmony_ci
18871cb0ef41Sopenharmony_civoid Simulator::TraceMemRd(int32_t addr, int64_t value, TraceType t) {
18881cb0ef41Sopenharmony_ci  if (::v8::internal::FLAG_trace_sim) {
18891cb0ef41Sopenharmony_ci    union {
18901cb0ef41Sopenharmony_ci      int64_t fmt_int64;
18911cb0ef41Sopenharmony_ci      int32_t fmt_int32[2];
18921cb0ef41Sopenharmony_ci      float fmt_float[2];
18931cb0ef41Sopenharmony_ci      double fmt_double;
18941cb0ef41Sopenharmony_ci    } v;
18951cb0ef41Sopenharmony_ci    v.fmt_int64 = value;
18961cb0ef41Sopenharmony_ci
18971cb0ef41Sopenharmony_ci    switch (t) {
18981cb0ef41Sopenharmony_ci      case DWORD:
18991cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
19001cb0ef41Sopenharmony_ci                 "%016" PRIx64 " <-- [%08" PRIx32 "]    (%" PRIu64
19011cb0ef41Sopenharmony_ci                 ")    int64:%" PRId64 " uint64:%" PRIu64,
19021cb0ef41Sopenharmony_ci                 v.fmt_int64, addr, icount_, v.fmt_int64, v.fmt_int64);
19031cb0ef41Sopenharmony_ci        break;
19041cb0ef41Sopenharmony_ci      case DOUBLE:
19051cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
19061cb0ef41Sopenharmony_ci                 "%016" PRIx64 " <-- [%08" PRIx32 "]    (%" PRIu64
19071cb0ef41Sopenharmony_ci                 ")    dbl:%e",
19081cb0ef41Sopenharmony_ci                 v.fmt_int64, addr, icount_, v.fmt_double);
19091cb0ef41Sopenharmony_ci        break;
19101cb0ef41Sopenharmony_ci      case FLOAT_DOUBLE:
19111cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
19121cb0ef41Sopenharmony_ci                 "%08" PRIx32 " <-- [%08" PRIx32 "]    (%" PRIu64
19131cb0ef41Sopenharmony_ci                 ")    flt:%e dbl:%e",
19141cb0ef41Sopenharmony_ci                 v.fmt_int32[1], addr, icount_, v.fmt_float[1], v.fmt_double);
19151cb0ef41Sopenharmony_ci        break;
19161cb0ef41Sopenharmony_ci      default:
19171cb0ef41Sopenharmony_ci        UNREACHABLE();
19181cb0ef41Sopenharmony_ci    }
19191cb0ef41Sopenharmony_ci  }
19201cb0ef41Sopenharmony_ci}
19211cb0ef41Sopenharmony_ci
19221cb0ef41Sopenharmony_civoid Simulator::TraceMemWr(int32_t addr, int64_t value, TraceType t) {
19231cb0ef41Sopenharmony_ci  if (::v8::internal::FLAG_trace_sim) {
19241cb0ef41Sopenharmony_ci    switch (t) {
19251cb0ef41Sopenharmony_ci      case DWORD:
19261cb0ef41Sopenharmony_ci        SNPrintF(trace_buf_,
19271cb0ef41Sopenharmony_ci                 "%016" PRIx64 " --> [%08" PRIx32 "]    (%" PRIu64 ")", value,
19281cb0ef41Sopenharmony_ci                 addr, icount_);
19291cb0ef41Sopenharmony_ci        break;
19301cb0ef41Sopenharmony_ci      default:
19311cb0ef41Sopenharmony_ci        UNREACHABLE();
19321cb0ef41Sopenharmony_ci    }
19331cb0ef41Sopenharmony_ci  }
19341cb0ef41Sopenharmony_ci}
19351cb0ef41Sopenharmony_ci
19361cb0ef41Sopenharmony_ciint Simulator::ReadW(int32_t addr, Instruction* instr, TraceType t) {
19371cb0ef41Sopenharmony_ci  if (addr >= 0 && addr < 0x400) {
19381cb0ef41Sopenharmony_ci    // This has to be a nullptr-dereference, drop into debugger.
19391cb0ef41Sopenharmony_ci    PrintF("Memory read from bad address: 0x%08x, pc=0x%08" PRIxPTR "\n", addr,
19401cb0ef41Sopenharmony_ci           reinterpret_cast<intptr_t>(instr));
19411cb0ef41Sopenharmony_ci    MipsDebugger dbg(this);
19421cb0ef41Sopenharmony_ci    dbg.Debug();
19431cb0ef41Sopenharmony_ci  }
19441cb0ef41Sopenharmony_ci  if ((addr & kPointerAlignmentMask) == 0 || IsMipsArchVariant(kMips32r6)) {
19451cb0ef41Sopenharmony_ci    local_monitor_.NotifyLoad();
19461cb0ef41Sopenharmony_ci    intptr_t* ptr = reinterpret_cast<intptr_t*>(addr);
19471cb0ef41Sopenharmony_ci    switch (t) {
19481cb0ef41Sopenharmony_ci      case WORD:
19491cb0ef41Sopenharmony_ci        TraceMemRd(addr, static_cast<int32_t>(*ptr), t);
19501cb0ef41Sopenharmony_ci        break;
19511cb0ef41Sopenharmony_ci      case FLOAT:
19521cb0ef41Sopenharmony_ci        // This TraceType is allowed but tracing for this value will be omitted.
19531cb0ef41Sopenharmony_ci        break;
19541cb0ef41Sopenharmony_ci      default:
19551cb0ef41Sopenharmony_ci        UNREACHABLE();
19561cb0ef41Sopenharmony_ci    }
19571cb0ef41Sopenharmony_ci    return *ptr;
19581cb0ef41Sopenharmony_ci  }
19591cb0ef41Sopenharmony_ci  PrintF("Unaligned read at 0x%08x, pc=0x%08" V8PRIxPTR "\n", addr,
19601cb0ef41Sopenharmony_ci         reinterpret_cast<intptr_t>(instr));
19611cb0ef41Sopenharmony_ci  MipsDebugger dbg(this);
19621cb0ef41Sopenharmony_ci  dbg.Debug();
19631cb0ef41Sopenharmony_ci  return 0;
19641cb0ef41Sopenharmony_ci}
19651cb0ef41Sopenharmony_ci
19661cb0ef41Sopenharmony_civoid Simulator::WriteW(int32_t addr, int value, Instruction* instr) {
19671cb0ef41Sopenharmony_ci  if (addr >= 0 && addr < 0x400) {
19681cb0ef41Sopenharmony_ci    // This has to be a nullptr-dereference, drop into debugger.
19691cb0ef41Sopenharmony_ci    PrintF("Memory write to bad address: 0x%08x, pc=0x%08" PRIxPTR "\n", addr,
19701cb0ef41Sopenharmony_ci           reinterpret_cast<intptr_t>(instr));
19711cb0ef41Sopenharmony_ci    MipsDebugger dbg(this);
19721cb0ef41Sopenharmony_ci    dbg.Debug();
19731cb0ef41Sopenharmony_ci  }
19741cb0ef41Sopenharmony_ci  if ((addr & kPointerAlignmentMask) == 0 || IsMipsArchVariant(kMips32r6)) {
19751cb0ef41Sopenharmony_ci    local_monitor_.NotifyStore();
19761cb0ef41Sopenharmony_ci    base::MutexGuard lock_guard(&GlobalMonitor::Get()->mutex);
19771cb0ef41Sopenharmony_ci    GlobalMonitor::Get()->NotifyStore_Locked(&global_monitor_thread_);
19781cb0ef41Sopenharmony_ci    intptr_t* ptr = reinterpret_cast<intptr_t*>(addr);
19791cb0ef41Sopenharmony_ci    TraceMemWr(addr, value, WORD);
19801cb0ef41Sopenharmony_ci    *ptr = value;
19811cb0ef41Sopenharmony_ci    return;
19821cb0ef41Sopenharmony_ci  }
19831cb0ef41Sopenharmony_ci  PrintF("Unaligned write at 0x%08x, pc=0x%08" V8PRIxPTR "\n", addr,
19841cb0ef41Sopenharmony_ci         reinterpret_cast<intptr_t>(instr));
19851cb0ef41Sopenharmony_ci  MipsDebugger dbg(this);
19861cb0ef41Sopenharmony_ci  dbg.Debug();
19871cb0ef41Sopenharmony_ci}
19881cb0ef41Sopenharmony_ci
19891cb0ef41Sopenharmony_civoid Simulator::WriteConditionalW(int32_t addr, int32_t value,
19901cb0ef41Sopenharmony_ci                                  Instruction* instr, int32_t rt_reg) {
19911cb0ef41Sopenharmony_ci  if (addr >= 0 && addr < 0x400) {
19921cb0ef41Sopenharmony_ci    // This has to be a nullptr-dereference, drop into debugger.
19931cb0ef41Sopenharmony_ci    PrintF("Memory write to bad address: 0x%08x, pc=0x%08" PRIxPTR "\n", addr,
19941cb0ef41Sopenharmony_ci           reinterpret_cast<intptr_t>(instr));
19951cb0ef41Sopenharmony_ci    MipsDebugger dbg(this);
19961cb0ef41Sopenharmony_ci    dbg.Debug();
19971cb0ef41Sopenharmony_ci  }
19981cb0ef41Sopenharmony_ci  if ((addr & kPointerAlignmentMask) == 0 || IsMipsArchVariant(kMips32r6)) {
19991cb0ef41Sopenharmony_ci    base::MutexGuard lock_guard(&GlobalMonitor::Get()->mutex);
20001cb0ef41Sopenharmony_ci    if (local_monitor_.NotifyStoreConditional(addr, TransactionSize::Word) &&
20011cb0ef41Sopenharmony_ci        GlobalMonitor::Get()->NotifyStoreConditional_Locked(
20021cb0ef41Sopenharmony_ci            addr, &global_monitor_thread_)) {
20031cb0ef41Sopenharmony_ci      local_monitor_.NotifyStore();
20041cb0ef41Sopenharmony_ci      GlobalMonitor::Get()->NotifyStore_Locked(&global_monitor_thread_);
20051cb0ef41Sopenharmony_ci      TraceMemWr(addr, value, WORD);
20061cb0ef41Sopenharmony_ci      int* ptr = reinterpret_cast<int*>(addr);
20071cb0ef41Sopenharmony_ci      *ptr = value;
20081cb0ef41Sopenharmony_ci      set_register(rt_reg, 1);
20091cb0ef41Sopenharmony_ci    } else {
20101cb0ef41Sopenharmony_ci      set_register(rt_reg, 0);
20111cb0ef41Sopenharmony_ci    }
20121cb0ef41Sopenharmony_ci    return;
20131cb0ef41Sopenharmony_ci  }
20141cb0ef41Sopenharmony_ci  PrintF("Unaligned write at 0x%08x, pc=0x%08" V8PRIxPTR "\n", addr,
20151cb0ef41Sopenharmony_ci         reinterpret_cast<intptr_t>(instr));
20161cb0ef41Sopenharmony_ci  MipsDebugger dbg(this);
20171cb0ef41Sopenharmony_ci  dbg.Debug();
20181cb0ef41Sopenharmony_ci}
20191cb0ef41Sopenharmony_ci
20201cb0ef41Sopenharmony_cidouble Simulator::ReadD(int32_t addr, Instruction* instr) {
20211cb0ef41Sopenharmony_ci  if ((addr & kDoubleAlignmentMask) == 0 || IsMipsArchVariant(kMips32r6)) {
20221cb0ef41Sopenharmony_ci    local_monitor_.NotifyLoad();
20231cb0ef41Sopenharmony_ci    double* ptr = reinterpret_cast<double*>(addr);
20241cb0ef41Sopenharmony_ci    return *ptr;
20251cb0ef41Sopenharmony_ci  }
20261cb0ef41Sopenharmony_ci  PrintF("Unaligned (double) read at 0x%08x, pc=0x%08" V8PRIxPTR "\n", addr,
20271cb0ef41Sopenharmony_ci         reinterpret_cast<intptr_t>(instr));
20281cb0ef41Sopenharmony_ci  base::OS::Abort();
20291cb0ef41Sopenharmony_ci}
20301cb0ef41Sopenharmony_ci
20311cb0ef41Sopenharmony_civoid Simulator::WriteD(int32_t addr, double value, Instruction* instr) {
20321cb0ef41Sopenharmony_ci  if ((addr & kDoubleAlignmentMask) == 0 || IsMipsArchVariant(kMips32r6)) {
20331cb0ef41Sopenharmony_ci    local_monitor_.NotifyStore();
20341cb0ef41Sopenharmony_ci    base::MutexGuard lock_guard(&GlobalMonitor::Get()->mutex);
20351cb0ef41Sopenharmony_ci    GlobalMonitor::Get()->NotifyStore_Locked(&global_monitor_thread_);
20361cb0ef41Sopenharmony_ci    double* ptr = reinterpret_cast<double*>(addr);
20371cb0ef41Sopenharmony_ci    *ptr = value;
20381cb0ef41Sopenharmony_ci    return;
20391cb0ef41Sopenharmony_ci  }
20401cb0ef41Sopenharmony_ci  PrintF("Unaligned (double) write at 0x%08x, pc=0x%08" V8PRIxPTR "\n", addr,
20411cb0ef41Sopenharmony_ci         reinterpret_cast<intptr_t>(instr));
20421cb0ef41Sopenharmony_ci  base::OS::Abort();
20431cb0ef41Sopenharmony_ci}
20441cb0ef41Sopenharmony_ci
20451cb0ef41Sopenharmony_ciuint16_t Simulator::ReadHU(int32_t addr, Instruction* instr) {
20461cb0ef41Sopenharmony_ci  if ((addr & 1) == 0 || IsMipsArchVariant(kMips32r6)) {
20471cb0ef41Sopenharmony_ci    local_monitor_.NotifyLoad();
20481cb0ef41Sopenharmony_ci    uint16_t* ptr = reinterpret_cast<uint16_t*>(addr);
20491cb0ef41Sopenharmony_ci    TraceMemRd(addr, static_cast<int32_t>(*ptr));
20501cb0ef41Sopenharmony_ci    return *ptr;
20511cb0ef41Sopenharmony_ci  }
20521cb0ef41Sopenharmony_ci  PrintF("Unaligned unsigned halfword read at 0x%08x, pc=0x%08" V8PRIxPTR "\n",
20531cb0ef41Sopenharmony_ci         addr, reinterpret_cast<intptr_t>(instr));
20541cb0ef41Sopenharmony_ci  base::OS::Abort();
20551cb0ef41Sopenharmony_ci}
20561cb0ef41Sopenharmony_ci
20571cb0ef41Sopenharmony_ciint16_t Simulator::ReadH(int32_t addr, Instruction* instr) {
20581cb0ef41Sopenharmony_ci  if ((addr & 1) == 0 || IsMipsArchVariant(kMips32r6)) {
20591cb0ef41Sopenharmony_ci    local_monitor_.NotifyLoad();
20601cb0ef41Sopenharmony_ci    int16_t* ptr = reinterpret_cast<int16_t*>(addr);
20611cb0ef41Sopenharmony_ci    TraceMemRd(addr, static_cast<int32_t>(*ptr));
20621cb0ef41Sopenharmony_ci    return *ptr;
20631cb0ef41Sopenharmony_ci  }
20641cb0ef41Sopenharmony_ci  PrintF("Unaligned signed halfword read at 0x%08x, pc=0x%08" V8PRIxPTR "\n",
20651cb0ef41Sopenharmony_ci         addr, reinterpret_cast<intptr_t>(instr));
20661cb0ef41Sopenharmony_ci  base::OS::Abort();
20671cb0ef41Sopenharmony_ci}
20681cb0ef41Sopenharmony_ci
20691cb0ef41Sopenharmony_civoid Simulator::WriteH(int32_t addr, uint16_t value, Instruction* instr) {
20701cb0ef41Sopenharmony_ci  if ((addr & 1) == 0 || IsMipsArchVariant(kMips32r6)) {
20711cb0ef41Sopenharmony_ci    local_monitor_.NotifyStore();
20721cb0ef41Sopenharmony_ci    base::MutexGuard lock_guard(&GlobalMonitor::Get()->mutex);
20731cb0ef41Sopenharmony_ci    GlobalMonitor::Get()->NotifyStore_Locked(&global_monitor_thread_);
20741cb0ef41Sopenharmony_ci    uint16_t* ptr = reinterpret_cast<uint16_t*>(addr);
20751cb0ef41Sopenharmony_ci    TraceMemWr(addr, value, HALF);
20761cb0ef41Sopenharmony_ci    *ptr = value;
20771cb0ef41Sopenharmony_ci    return;
20781cb0ef41Sopenharmony_ci  }
20791cb0ef41Sopenharmony_ci  PrintF("Unaligned unsigned halfword write at 0x%08x, pc=0x%08" V8PRIxPTR "\n",
20801cb0ef41Sopenharmony_ci         addr, reinterpret_cast<intptr_t>(instr));
20811cb0ef41Sopenharmony_ci  base::OS::Abort();
20821cb0ef41Sopenharmony_ci}
20831cb0ef41Sopenharmony_ci
20841cb0ef41Sopenharmony_civoid Simulator::WriteH(int32_t addr, int16_t value, Instruction* instr) {
20851cb0ef41Sopenharmony_ci  if ((addr & 1) == 0 || IsMipsArchVariant(kMips32r6)) {
20861cb0ef41Sopenharmony_ci    local_monitor_.NotifyStore();
20871cb0ef41Sopenharmony_ci    base::MutexGuard lock_guard(&GlobalMonitor::Get()->mutex);
20881cb0ef41Sopenharmony_ci    GlobalMonitor::Get()->NotifyStore_Locked(&global_monitor_thread_);
20891cb0ef41Sopenharmony_ci    int16_t* ptr = reinterpret_cast<int16_t*>(addr);
20901cb0ef41Sopenharmony_ci    TraceMemWr(addr, value, HALF);
20911cb0ef41Sopenharmony_ci    *ptr = value;
20921cb0ef41Sopenharmony_ci    return;
20931cb0ef41Sopenharmony_ci  }
20941cb0ef41Sopenharmony_ci  PrintF("Unaligned halfword write at 0x%08x, pc=0x%08" V8PRIxPTR "\n", addr,
20951cb0ef41Sopenharmony_ci         reinterpret_cast<intptr_t>(instr));
20961cb0ef41Sopenharmony_ci  base::OS::Abort();
20971cb0ef41Sopenharmony_ci}
20981cb0ef41Sopenharmony_ci
20991cb0ef41Sopenharmony_ciuint32_t Simulator::ReadBU(int32_t addr) {
21001cb0ef41Sopenharmony_ci  local_monitor_.NotifyLoad();
21011cb0ef41Sopenharmony_ci  uint8_t* ptr = reinterpret_cast<uint8_t*>(addr);
21021cb0ef41Sopenharmony_ci  TraceMemRd(addr, static_cast<int32_t>(*ptr));
21031cb0ef41Sopenharmony_ci  return *ptr & 0xFF;
21041cb0ef41Sopenharmony_ci}
21051cb0ef41Sopenharmony_ci
21061cb0ef41Sopenharmony_ciint32_t Simulator::ReadB(int32_t addr) {
21071cb0ef41Sopenharmony_ci  local_monitor_.NotifyLoad();
21081cb0ef41Sopenharmony_ci  int8_t* ptr = reinterpret_cast<int8_t*>(addr);
21091cb0ef41Sopenharmony_ci  TraceMemRd(addr, static_cast<int32_t>(*ptr));
21101cb0ef41Sopenharmony_ci  return *ptr;
21111cb0ef41Sopenharmony_ci}
21121cb0ef41Sopenharmony_ci
21131cb0ef41Sopenharmony_civoid Simulator::WriteB(int32_t addr, uint8_t value) {
21141cb0ef41Sopenharmony_ci  local_monitor_.NotifyStore();
21151cb0ef41Sopenharmony_ci  base::MutexGuard lock_guard(&GlobalMonitor::Get()->mutex);
21161cb0ef41Sopenharmony_ci  GlobalMonitor::Get()->NotifyStore_Locked(&global_monitor_thread_);
21171cb0ef41Sopenharmony_ci  uint8_t* ptr = reinterpret_cast<uint8_t*>(addr);
21181cb0ef41Sopenharmony_ci  TraceMemWr(addr, value, BYTE);
21191cb0ef41Sopenharmony_ci  *ptr = value;
21201cb0ef41Sopenharmony_ci}
21211cb0ef41Sopenharmony_ci
21221cb0ef41Sopenharmony_civoid Simulator::WriteB(int32_t addr, int8_t value) {
21231cb0ef41Sopenharmony_ci  local_monitor_.NotifyStore();
21241cb0ef41Sopenharmony_ci  base::MutexGuard lock_guard(&GlobalMonitor::Get()->mutex);
21251cb0ef41Sopenharmony_ci  GlobalMonitor::Get()->NotifyStore_Locked(&global_monitor_thread_);
21261cb0ef41Sopenharmony_ci  int8_t* ptr = reinterpret_cast<int8_t*>(addr);
21271cb0ef41Sopenharmony_ci  TraceMemWr(addr, value, BYTE);
21281cb0ef41Sopenharmony_ci  *ptr = value;
21291cb0ef41Sopenharmony_ci}
21301cb0ef41Sopenharmony_ci
21311cb0ef41Sopenharmony_citemplate <typename T>
21321cb0ef41Sopenharmony_ciT Simulator::ReadMem(int32_t addr, Instruction* instr) {
21331cb0ef41Sopenharmony_ci  int alignment_mask = (1 << sizeof(T)) - 1;
21341cb0ef41Sopenharmony_ci  if ((addr & alignment_mask) == 0 || IsMipsArchVariant(kMips32r6)) {
21351cb0ef41Sopenharmony_ci    local_monitor_.NotifyLoad();
21361cb0ef41Sopenharmony_ci    T* ptr = reinterpret_cast<T*>(addr);
21371cb0ef41Sopenharmony_ci    TraceMemRd(addr, *ptr);
21381cb0ef41Sopenharmony_ci    return *ptr;
21391cb0ef41Sopenharmony_ci  }
21401cb0ef41Sopenharmony_ci  PrintF("Unaligned read of type sizeof(%d) at 0x%08x, pc=0x%08" V8PRIxPTR "\n",
21411cb0ef41Sopenharmony_ci         sizeof(T), addr, reinterpret_cast<intptr_t>(instr));
21421cb0ef41Sopenharmony_ci  base::OS::Abort();
21431cb0ef41Sopenharmony_ci  return 0;
21441cb0ef41Sopenharmony_ci}
21451cb0ef41Sopenharmony_ci
21461cb0ef41Sopenharmony_citemplate <typename T>
21471cb0ef41Sopenharmony_civoid Simulator::WriteMem(int32_t addr, T value, Instruction* instr) {
21481cb0ef41Sopenharmony_ci  local_monitor_.NotifyStore();
21491cb0ef41Sopenharmony_ci  base::MutexGuard lock_guard(&GlobalMonitor::Get()->mutex);
21501cb0ef41Sopenharmony_ci  GlobalMonitor::Get()->NotifyStore_Locked(&global_monitor_thread_);
21511cb0ef41Sopenharmony_ci  int alignment_mask = (1 << sizeof(T)) - 1;
21521cb0ef41Sopenharmony_ci  if ((addr & alignment_mask) == 0 || IsMipsArchVariant(kMips32r6)) {
21531cb0ef41Sopenharmony_ci    T* ptr = reinterpret_cast<T*>(addr);
21541cb0ef41Sopenharmony_ci    *ptr = value;
21551cb0ef41Sopenharmony_ci    TraceMemWr(addr, value);
21561cb0ef41Sopenharmony_ci    return;
21571cb0ef41Sopenharmony_ci  }
21581cb0ef41Sopenharmony_ci  PrintF("Unaligned write of type sizeof(%d) at 0x%08x, pc=0x%08" V8PRIxPTR
21591cb0ef41Sopenharmony_ci         "\n",
21601cb0ef41Sopenharmony_ci         sizeof(T), addr, reinterpret_cast<intptr_t>(instr));
21611cb0ef41Sopenharmony_ci  base::OS::Abort();
21621cb0ef41Sopenharmony_ci}
21631cb0ef41Sopenharmony_ci
21641cb0ef41Sopenharmony_ci// Returns the limit of the stack area to enable checking for stack overflows.
21651cb0ef41Sopenharmony_ciuintptr_t Simulator::StackLimit(uintptr_t c_limit) const {
21661cb0ef41Sopenharmony_ci  // The simulator uses a separate JS stack. If we have exhausted the C stack,
21671cb0ef41Sopenharmony_ci  // we also drop down the JS limit to reflect the exhaustion on the JS stack.
21681cb0ef41Sopenharmony_ci  if (base::Stack::GetCurrentStackPosition() < c_limit) {
21691cb0ef41Sopenharmony_ci    return reinterpret_cast<uintptr_t>(get_sp());
21701cb0ef41Sopenharmony_ci  }
21711cb0ef41Sopenharmony_ci
21721cb0ef41Sopenharmony_ci  // Otherwise the limit is the JS stack. Leave a safety margin of 1024 bytes
21731cb0ef41Sopenharmony_ci  // to prevent overrunning the stack when pushing values.
21741cb0ef41Sopenharmony_ci  return reinterpret_cast<uintptr_t>(stack_) + 1024;
21751cb0ef41Sopenharmony_ci}
21761cb0ef41Sopenharmony_ci
21771cb0ef41Sopenharmony_ci// Unsupported instructions use Format to print an error and stop execution.
21781cb0ef41Sopenharmony_civoid Simulator::Format(Instruction* instr, const char* format) {
21791cb0ef41Sopenharmony_ci  PrintF("Simulator found unsupported instruction:\n 0x%08" PRIxPTR ": %s\n",
21801cb0ef41Sopenharmony_ci         reinterpret_cast<intptr_t>(instr), format);
21811cb0ef41Sopenharmony_ci  UNIMPLEMENTED_MIPS();
21821cb0ef41Sopenharmony_ci}
21831cb0ef41Sopenharmony_ci
21841cb0ef41Sopenharmony_ci// Calls into the V8 runtime are based on this very simple interface.
21851cb0ef41Sopenharmony_ci// Note: To be able to return two values from some calls the code in runtime.cc
21861cb0ef41Sopenharmony_ci// uses the ObjectPair which is essentially two 32-bit values stuffed into a
21871cb0ef41Sopenharmony_ci// 64-bit value. With the code below we assume that all runtime calls return
21881cb0ef41Sopenharmony_ci// 64 bits of result. If they don't, the v1 result register contains a bogus
21891cb0ef41Sopenharmony_ci// value, which is fine because it is caller-saved.
21901cb0ef41Sopenharmony_ciusing SimulatorRuntimeCall = int64_t (*)(
21911cb0ef41Sopenharmony_ci    int32_t arg0, int32_t arg1, int32_t arg2, int32_t arg3, int32_t arg4,
21921cb0ef41Sopenharmony_ci    int32_t arg5, int32_t arg6, int32_t arg7, int32_t arg8, int32_t arg9,
21931cb0ef41Sopenharmony_ci    int32_t arg10, int32_t arg11, int32_t arg12, int32_t arg13, int32_t arg14,
21941cb0ef41Sopenharmony_ci    int32_t arg15, int32_t arg16, int32_t arg17, int32_t arg18, int32_t arg19);
21951cb0ef41Sopenharmony_ci
21961cb0ef41Sopenharmony_ci// These prototypes handle the four types of FP calls.
21971cb0ef41Sopenharmony_ciusing SimulatorRuntimeCompareCall = int64_t (*)(double darg0, double darg1);
21981cb0ef41Sopenharmony_ciusing SimulatorRuntimeFPFPCall = double (*)(double darg0, double darg1);
21991cb0ef41Sopenharmony_ciusing SimulatorRuntimeFPCall = double (*)(double darg0);
22001cb0ef41Sopenharmony_ciusing SimulatorRuntimeFPIntCall = double (*)(double darg0, int32_t arg0);
22011cb0ef41Sopenharmony_ci
22021cb0ef41Sopenharmony_ci// This signature supports direct call in to API function native callback
22031cb0ef41Sopenharmony_ci// (refer to InvocationCallback in v8.h).
22041cb0ef41Sopenharmony_ciusing SimulatorRuntimeDirectApiCall = void (*)(int32_t arg0);
22051cb0ef41Sopenharmony_ciusing SimulatorRuntimeProfilingApiCall = void (*)(int32_t arg0, void* arg1);
22061cb0ef41Sopenharmony_ci
22071cb0ef41Sopenharmony_ci// This signature supports direct call to accessor getter callback.
22081cb0ef41Sopenharmony_ciusing SimulatorRuntimeDirectGetterCall = void (*)(int32_t arg0, int32_t arg1);
22091cb0ef41Sopenharmony_ciusing SimulatorRuntimeProfilingGetterCall = void (*)(int32_t arg0, int32_t arg1,
22101cb0ef41Sopenharmony_ci                                                     void* arg2);
22111cb0ef41Sopenharmony_ci
22121cb0ef41Sopenharmony_ci// Software interrupt instructions are used by the simulator to call into the
22131cb0ef41Sopenharmony_ci// C-based V8 runtime. They are also used for debugging with simulator.
22141cb0ef41Sopenharmony_civoid Simulator::SoftwareInterrupt() {
22151cb0ef41Sopenharmony_ci  // There are several instructions that could get us here,
22161cb0ef41Sopenharmony_ci  // the break_ instruction, or several variants of traps. All
22171cb0ef41Sopenharmony_ci  // Are "SPECIAL" class opcode, and are distinuished by function.
22181cb0ef41Sopenharmony_ci  int32_t func = instr_.FunctionFieldRaw();
22191cb0ef41Sopenharmony_ci  uint32_t code = (func == BREAK) ? instr_.Bits(25, 6) : -1;
22201cb0ef41Sopenharmony_ci
22211cb0ef41Sopenharmony_ci  // We first check if we met a call_rt_redirected.
22221cb0ef41Sopenharmony_ci  if (instr_.InstructionBits() == rtCallRedirInstr) {
22231cb0ef41Sopenharmony_ci    Redirection* redirection = Redirection::FromInstruction(instr_.instr());
22241cb0ef41Sopenharmony_ci    int32_t arg0 = get_register(a0);
22251cb0ef41Sopenharmony_ci    int32_t arg1 = get_register(a1);
22261cb0ef41Sopenharmony_ci    int32_t arg2 = get_register(a2);
22271cb0ef41Sopenharmony_ci    int32_t arg3 = get_register(a3);
22281cb0ef41Sopenharmony_ci
22291cb0ef41Sopenharmony_ci    int32_t* stack_pointer = reinterpret_cast<int32_t*>(get_register(sp));
22301cb0ef41Sopenharmony_ci    // Args 4 and 5 are on the stack after the reserved space for args 0..3.
22311cb0ef41Sopenharmony_ci    int32_t arg4 = stack_pointer[4];
22321cb0ef41Sopenharmony_ci    int32_t arg5 = stack_pointer[5];
22331cb0ef41Sopenharmony_ci    int32_t arg6 = stack_pointer[6];
22341cb0ef41Sopenharmony_ci    int32_t arg7 = stack_pointer[7];
22351cb0ef41Sopenharmony_ci    int32_t arg8 = stack_pointer[8];
22361cb0ef41Sopenharmony_ci    int32_t arg9 = stack_pointer[9];
22371cb0ef41Sopenharmony_ci    int32_t arg10 = stack_pointer[10];
22381cb0ef41Sopenharmony_ci    int32_t arg11 = stack_pointer[11];
22391cb0ef41Sopenharmony_ci    int32_t arg12 = stack_pointer[12];
22401cb0ef41Sopenharmony_ci    int32_t arg13 = stack_pointer[13];
22411cb0ef41Sopenharmony_ci    int32_t arg14 = stack_pointer[14];
22421cb0ef41Sopenharmony_ci    int32_t arg15 = stack_pointer[15];
22431cb0ef41Sopenharmony_ci    int32_t arg16 = stack_pointer[16];
22441cb0ef41Sopenharmony_ci    int32_t arg17 = stack_pointer[17];
22451cb0ef41Sopenharmony_ci    int32_t arg18 = stack_pointer[18];
22461cb0ef41Sopenharmony_ci    int32_t arg19 = stack_pointer[19];
22471cb0ef41Sopenharmony_ci    STATIC_ASSERT(kMaxCParameters == 20);
22481cb0ef41Sopenharmony_ci
22491cb0ef41Sopenharmony_ci    bool fp_call =
22501cb0ef41Sopenharmony_ci        (redirection->type() == ExternalReference::BUILTIN_FP_FP_CALL) ||
22511cb0ef41Sopenharmony_ci        (redirection->type() == ExternalReference::BUILTIN_COMPARE_CALL) ||
22521cb0ef41Sopenharmony_ci        (redirection->type() == ExternalReference::BUILTIN_FP_CALL) ||
22531cb0ef41Sopenharmony_ci        (redirection->type() == ExternalReference::BUILTIN_FP_INT_CALL);
22541cb0ef41Sopenharmony_ci
22551cb0ef41Sopenharmony_ci    if (!IsMipsSoftFloatABI) {
22561cb0ef41Sopenharmony_ci      // With the hard floating point calling convention, double
22571cb0ef41Sopenharmony_ci      // arguments are passed in FPU registers. Fetch the arguments
22581cb0ef41Sopenharmony_ci      // from there and call the builtin using soft floating point
22591cb0ef41Sopenharmony_ci      // convention.
22601cb0ef41Sopenharmony_ci      switch (redirection->type()) {
22611cb0ef41Sopenharmony_ci        case ExternalReference::BUILTIN_FP_FP_CALL:
22621cb0ef41Sopenharmony_ci        case ExternalReference::BUILTIN_COMPARE_CALL:
22631cb0ef41Sopenharmony_ci          if (IsFp64Mode()) {
22641cb0ef41Sopenharmony_ci            arg0 = get_fpu_register_word(f12);
22651cb0ef41Sopenharmony_ci            arg1 = get_fpu_register_hi_word(f12);
22661cb0ef41Sopenharmony_ci            arg2 = get_fpu_register_word(f14);
22671cb0ef41Sopenharmony_ci            arg3 = get_fpu_register_hi_word(f14);
22681cb0ef41Sopenharmony_ci          } else {
22691cb0ef41Sopenharmony_ci            arg0 = get_fpu_register_word(f12);
22701cb0ef41Sopenharmony_ci            arg1 = get_fpu_register_word(f13);
22711cb0ef41Sopenharmony_ci            arg2 = get_fpu_register_word(f14);
22721cb0ef41Sopenharmony_ci            arg3 = get_fpu_register_word(f15);
22731cb0ef41Sopenharmony_ci          }
22741cb0ef41Sopenharmony_ci          break;
22751cb0ef41Sopenharmony_ci        case ExternalReference::BUILTIN_FP_CALL:
22761cb0ef41Sopenharmony_ci          if (IsFp64Mode()) {
22771cb0ef41Sopenharmony_ci            arg0 = get_fpu_register_word(f12);
22781cb0ef41Sopenharmony_ci            arg1 = get_fpu_register_hi_word(f12);
22791cb0ef41Sopenharmony_ci          } else {
22801cb0ef41Sopenharmony_ci            arg0 = get_fpu_register_word(f12);
22811cb0ef41Sopenharmony_ci            arg1 = get_fpu_register_word(f13);
22821cb0ef41Sopenharmony_ci          }
22831cb0ef41Sopenharmony_ci          break;
22841cb0ef41Sopenharmony_ci        case ExternalReference::BUILTIN_FP_INT_CALL:
22851cb0ef41Sopenharmony_ci          if (IsFp64Mode()) {
22861cb0ef41Sopenharmony_ci            arg0 = get_fpu_register_word(f12);
22871cb0ef41Sopenharmony_ci            arg1 = get_fpu_register_hi_word(f12);
22881cb0ef41Sopenharmony_ci          } else {
22891cb0ef41Sopenharmony_ci            arg0 = get_fpu_register_word(f12);
22901cb0ef41Sopenharmony_ci            arg1 = get_fpu_register_word(f13);
22911cb0ef41Sopenharmony_ci          }
22921cb0ef41Sopenharmony_ci          arg2 = get_register(a2);
22931cb0ef41Sopenharmony_ci          break;
22941cb0ef41Sopenharmony_ci        default:
22951cb0ef41Sopenharmony_ci          break;
22961cb0ef41Sopenharmony_ci      }
22971cb0ef41Sopenharmony_ci    }
22981cb0ef41Sopenharmony_ci
22991cb0ef41Sopenharmony_ci    // This is dodgy but it works because the C entry stubs are never moved.
23001cb0ef41Sopenharmony_ci    // See comment in codegen-arm.cc and bug 1242173.
23011cb0ef41Sopenharmony_ci    int32_t saved_ra = get_register(ra);
23021cb0ef41Sopenharmony_ci
23031cb0ef41Sopenharmony_ci    intptr_t external =
23041cb0ef41Sopenharmony_ci        reinterpret_cast<intptr_t>(redirection->external_function());
23051cb0ef41Sopenharmony_ci
23061cb0ef41Sopenharmony_ci    // Based on CpuFeatures::IsSupported(FPU), Mips will use either hardware
23071cb0ef41Sopenharmony_ci    // FPU, or gcc soft-float routines. Hardware FPU is simulated in this
23081cb0ef41Sopenharmony_ci    // simulator. Soft-float has additional abstraction of ExternalReference,
23091cb0ef41Sopenharmony_ci    // to support serialization.
23101cb0ef41Sopenharmony_ci    if (fp_call) {
23111cb0ef41Sopenharmony_ci      double dval0, dval1;  // one or two double parameters
23121cb0ef41Sopenharmony_ci      int32_t ival;         // zero or one integer parameters
23131cb0ef41Sopenharmony_ci      int64_t iresult = 0;  // integer return value
23141cb0ef41Sopenharmony_ci      double dresult = 0;   // double return value
23151cb0ef41Sopenharmony_ci      GetFpArgs(&dval0, &dval1, &ival);
23161cb0ef41Sopenharmony_ci      SimulatorRuntimeCall generic_target =
23171cb0ef41Sopenharmony_ci          reinterpret_cast<SimulatorRuntimeCall>(external);
23181cb0ef41Sopenharmony_ci      if (::v8::internal::FLAG_trace_sim) {
23191cb0ef41Sopenharmony_ci        switch (redirection->type()) {
23201cb0ef41Sopenharmony_ci          case ExternalReference::BUILTIN_FP_FP_CALL:
23211cb0ef41Sopenharmony_ci          case ExternalReference::BUILTIN_COMPARE_CALL:
23221cb0ef41Sopenharmony_ci            PrintF("Call to host function at %p with args %f, %f",
23231cb0ef41Sopenharmony_ci                   reinterpret_cast<void*>(FUNCTION_ADDR(generic_target)),
23241cb0ef41Sopenharmony_ci                   dval0, dval1);
23251cb0ef41Sopenharmony_ci            break;
23261cb0ef41Sopenharmony_ci          case ExternalReference::BUILTIN_FP_CALL:
23271cb0ef41Sopenharmony_ci            PrintF("Call to host function at %p with arg %f",
23281cb0ef41Sopenharmony_ci                   reinterpret_cast<void*>(FUNCTION_ADDR(generic_target)),
23291cb0ef41Sopenharmony_ci                   dval0);
23301cb0ef41Sopenharmony_ci            break;
23311cb0ef41Sopenharmony_ci          case ExternalReference::BUILTIN_FP_INT_CALL:
23321cb0ef41Sopenharmony_ci            PrintF("Call to host function at %p with args %f, %d",
23331cb0ef41Sopenharmony_ci                   reinterpret_cast<void*>(FUNCTION_ADDR(generic_target)),
23341cb0ef41Sopenharmony_ci                   dval0, ival);
23351cb0ef41Sopenharmony_ci            break;
23361cb0ef41Sopenharmony_ci          default:
23371cb0ef41Sopenharmony_ci            UNREACHABLE();
23381cb0ef41Sopenharmony_ci        }
23391cb0ef41Sopenharmony_ci      }
23401cb0ef41Sopenharmony_ci      switch (redirection->type()) {
23411cb0ef41Sopenharmony_ci        case ExternalReference::BUILTIN_COMPARE_CALL: {
23421cb0ef41Sopenharmony_ci          SimulatorRuntimeCompareCall target =
23431cb0ef41Sopenharmony_ci              reinterpret_cast<SimulatorRuntimeCompareCall>(external);
23441cb0ef41Sopenharmony_ci          iresult = target(dval0, dval1);
23451cb0ef41Sopenharmony_ci          set_register(v0, static_cast<int32_t>(iresult));
23461cb0ef41Sopenharmony_ci          set_register(v1, static_cast<int32_t>(iresult >> 32));
23471cb0ef41Sopenharmony_ci          break;
23481cb0ef41Sopenharmony_ci        }
23491cb0ef41Sopenharmony_ci        case ExternalReference::BUILTIN_FP_FP_CALL: {
23501cb0ef41Sopenharmony_ci          SimulatorRuntimeFPFPCall target =
23511cb0ef41Sopenharmony_ci              reinterpret_cast<SimulatorRuntimeFPFPCall>(external);
23521cb0ef41Sopenharmony_ci          dresult = target(dval0, dval1);
23531cb0ef41Sopenharmony_ci          SetFpResult(dresult);
23541cb0ef41Sopenharmony_ci          break;
23551cb0ef41Sopenharmony_ci        }
23561cb0ef41Sopenharmony_ci        case ExternalReference::BUILTIN_FP_CALL: {
23571cb0ef41Sopenharmony_ci          SimulatorRuntimeFPCall target =
23581cb0ef41Sopenharmony_ci              reinterpret_cast<SimulatorRuntimeFPCall>(external);
23591cb0ef41Sopenharmony_ci          dresult = target(dval0);
23601cb0ef41Sopenharmony_ci          SetFpResult(dresult);
23611cb0ef41Sopenharmony_ci          break;
23621cb0ef41Sopenharmony_ci        }
23631cb0ef41Sopenharmony_ci        case ExternalReference::BUILTIN_FP_INT_CALL: {
23641cb0ef41Sopenharmony_ci          SimulatorRuntimeFPIntCall target =
23651cb0ef41Sopenharmony_ci              reinterpret_cast<SimulatorRuntimeFPIntCall>(external);
23661cb0ef41Sopenharmony_ci          dresult = target(dval0, ival);
23671cb0ef41Sopenharmony_ci          SetFpResult(dresult);
23681cb0ef41Sopenharmony_ci          break;
23691cb0ef41Sopenharmony_ci        }
23701cb0ef41Sopenharmony_ci        default:
23711cb0ef41Sopenharmony_ci          UNREACHABLE();
23721cb0ef41Sopenharmony_ci      }
23731cb0ef41Sopenharmony_ci      if (::v8::internal::FLAG_trace_sim) {
23741cb0ef41Sopenharmony_ci        switch (redirection->type()) {
23751cb0ef41Sopenharmony_ci          case ExternalReference::BUILTIN_COMPARE_CALL:
23761cb0ef41Sopenharmony_ci            PrintF("Returned %08x\n", static_cast<int32_t>(iresult));
23771cb0ef41Sopenharmony_ci            break;
23781cb0ef41Sopenharmony_ci          case ExternalReference::BUILTIN_FP_FP_CALL:
23791cb0ef41Sopenharmony_ci          case ExternalReference::BUILTIN_FP_CALL:
23801cb0ef41Sopenharmony_ci          case ExternalReference::BUILTIN_FP_INT_CALL:
23811cb0ef41Sopenharmony_ci            PrintF("Returned %f\n", dresult);
23821cb0ef41Sopenharmony_ci            break;
23831cb0ef41Sopenharmony_ci          default:
23841cb0ef41Sopenharmony_ci            UNREACHABLE();
23851cb0ef41Sopenharmony_ci        }
23861cb0ef41Sopenharmony_ci      }
23871cb0ef41Sopenharmony_ci    } else if (redirection->type() == ExternalReference::DIRECT_API_CALL) {
23881cb0ef41Sopenharmony_ci      if (::v8::internal::FLAG_trace_sim) {
23891cb0ef41Sopenharmony_ci        PrintF("Call to host function at %p args %08x\n",
23901cb0ef41Sopenharmony_ci               reinterpret_cast<void*>(external), arg0);
23911cb0ef41Sopenharmony_ci      }
23921cb0ef41Sopenharmony_ci      SimulatorRuntimeDirectApiCall target =
23931cb0ef41Sopenharmony_ci          reinterpret_cast<SimulatorRuntimeDirectApiCall>(external);
23941cb0ef41Sopenharmony_ci      target(arg0);
23951cb0ef41Sopenharmony_ci    } else if (redirection->type() == ExternalReference::PROFILING_API_CALL) {
23961cb0ef41Sopenharmony_ci      if (::v8::internal::FLAG_trace_sim) {
23971cb0ef41Sopenharmony_ci        PrintF("Call to host function at %p args %08x %08x\n",
23981cb0ef41Sopenharmony_ci               reinterpret_cast<void*>(external), arg0, arg1);
23991cb0ef41Sopenharmony_ci      }
24001cb0ef41Sopenharmony_ci      SimulatorRuntimeProfilingApiCall target =
24011cb0ef41Sopenharmony_ci          reinterpret_cast<SimulatorRuntimeProfilingApiCall>(external);
24021cb0ef41Sopenharmony_ci      target(arg0, Redirection::ReverseRedirection(arg1));
24031cb0ef41Sopenharmony_ci    } else if (redirection->type() == ExternalReference::DIRECT_GETTER_CALL) {
24041cb0ef41Sopenharmony_ci      if (::v8::internal::FLAG_trace_sim) {
24051cb0ef41Sopenharmony_ci        PrintF("Call to host function at %p args %08x %08x\n",
24061cb0ef41Sopenharmony_ci               reinterpret_cast<void*>(external), arg0, arg1);
24071cb0ef41Sopenharmony_ci      }
24081cb0ef41Sopenharmony_ci      SimulatorRuntimeDirectGetterCall target =
24091cb0ef41Sopenharmony_ci          reinterpret_cast<SimulatorRuntimeDirectGetterCall>(external);
24101cb0ef41Sopenharmony_ci      target(arg0, arg1);
24111cb0ef41Sopenharmony_ci    } else if (redirection->type() ==
24121cb0ef41Sopenharmony_ci               ExternalReference::PROFILING_GETTER_CALL) {
24131cb0ef41Sopenharmony_ci      if (::v8::internal::FLAG_trace_sim) {
24141cb0ef41Sopenharmony_ci        PrintF("Call to host function at %p args %08x %08x %08x\n",
24151cb0ef41Sopenharmony_ci               reinterpret_cast<void*>(external), arg0, arg1, arg2);
24161cb0ef41Sopenharmony_ci      }
24171cb0ef41Sopenharmony_ci      SimulatorRuntimeProfilingGetterCall target =
24181cb0ef41Sopenharmony_ci          reinterpret_cast<SimulatorRuntimeProfilingGetterCall>(external);
24191cb0ef41Sopenharmony_ci      target(arg0, arg1, Redirection::ReverseRedirection(arg2));
24201cb0ef41Sopenharmony_ci    } else {
24211cb0ef41Sopenharmony_ci      DCHECK(redirection->type() == ExternalReference::BUILTIN_CALL ||
24221cb0ef41Sopenharmony_ci             redirection->type() == ExternalReference::BUILTIN_CALL_PAIR);
24231cb0ef41Sopenharmony_ci      SimulatorRuntimeCall target =
24241cb0ef41Sopenharmony_ci          reinterpret_cast<SimulatorRuntimeCall>(external);
24251cb0ef41Sopenharmony_ci      if (::v8::internal::FLAG_trace_sim) {
24261cb0ef41Sopenharmony_ci        PrintF(
24271cb0ef41Sopenharmony_ci            "Call to host function at %p "
24281cb0ef41Sopenharmony_ci            "args %08x, %08x, %08x, %08x, %08x, %08x, %08x, %08x, %08x, %08xi, "
24291cb0ef41Sopenharmony_ci            "%08xi, %08xi, %08xi, %08xi, %08xi, %08xi, %08xi, %08xi, %08xi, "
24301cb0ef41Sopenharmony_ci            "%08xi\n",
24311cb0ef41Sopenharmony_ci            reinterpret_cast<void*>(FUNCTION_ADDR(target)), arg0, arg1, arg2,
24321cb0ef41Sopenharmony_ci            arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12,
24331cb0ef41Sopenharmony_ci            arg13, arg14, arg15, arg16, arg17, arg18, arg19);
24341cb0ef41Sopenharmony_ci      }
24351cb0ef41Sopenharmony_ci      int64_t result = target(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7,
24361cb0ef41Sopenharmony_ci                              arg8, arg9, arg10, arg11, arg12, arg13, arg14,
24371cb0ef41Sopenharmony_ci                              arg15, arg16, arg17, arg18, arg19);
24381cb0ef41Sopenharmony_ci      set_register(v0, static_cast<int32_t>(result));
24391cb0ef41Sopenharmony_ci      set_register(v1, static_cast<int32_t>(result >> 32));
24401cb0ef41Sopenharmony_ci    }
24411cb0ef41Sopenharmony_ci    if (::v8::internal::FLAG_trace_sim) {
24421cb0ef41Sopenharmony_ci      PrintF("Returned %08x : %08x\n", get_register(v1), get_register(v0));
24431cb0ef41Sopenharmony_ci    }
24441cb0ef41Sopenharmony_ci    set_register(ra, saved_ra);
24451cb0ef41Sopenharmony_ci    set_pc(get_register(ra));
24461cb0ef41Sopenharmony_ci
24471cb0ef41Sopenharmony_ci  } else if (func == BREAK && code <= kMaxStopCode) {
24481cb0ef41Sopenharmony_ci    if (IsWatchpoint(code)) {
24491cb0ef41Sopenharmony_ci      PrintWatchpoint(code);
24501cb0ef41Sopenharmony_ci    } else {
24511cb0ef41Sopenharmony_ci      IncreaseStopCounter(code);
24521cb0ef41Sopenharmony_ci      HandleStop(code, instr_.instr());
24531cb0ef41Sopenharmony_ci    }
24541cb0ef41Sopenharmony_ci  } else {
24551cb0ef41Sopenharmony_ci    // All remaining break_ codes, and all traps are handled here.
24561cb0ef41Sopenharmony_ci    MipsDebugger dbg(this);
24571cb0ef41Sopenharmony_ci    dbg.Debug();
24581cb0ef41Sopenharmony_ci  }
24591cb0ef41Sopenharmony_ci}
24601cb0ef41Sopenharmony_ci
24611cb0ef41Sopenharmony_ci// Stop helper functions.
24621cb0ef41Sopenharmony_cibool Simulator::IsWatchpoint(uint32_t code) {
24631cb0ef41Sopenharmony_ci  return (code <= kMaxWatchpointCode);
24641cb0ef41Sopenharmony_ci}
24651cb0ef41Sopenharmony_ci
24661cb0ef41Sopenharmony_civoid Simulator::PrintWatchpoint(uint32_t code) {
24671cb0ef41Sopenharmony_ci  MipsDebugger dbg(this);
24681cb0ef41Sopenharmony_ci  ++break_count_;
24691cb0ef41Sopenharmony_ci  PrintF("\n---- break %d marker: %3d  (instr count: %" PRIu64
24701cb0ef41Sopenharmony_ci         ") ----------"
24711cb0ef41Sopenharmony_ci         "----------------------------------",
24721cb0ef41Sopenharmony_ci         code, break_count_, icount_);
24731cb0ef41Sopenharmony_ci  dbg.PrintAllRegs();  // Print registers and continue running.
24741cb0ef41Sopenharmony_ci}
24751cb0ef41Sopenharmony_ci
24761cb0ef41Sopenharmony_civoid Simulator::HandleStop(uint32_t code, Instruction* instr) {
24771cb0ef41Sopenharmony_ci  // Stop if it is enabled, otherwise go on jumping over the stop
24781cb0ef41Sopenharmony_ci  // and the message address.
24791cb0ef41Sopenharmony_ci  if (IsEnabledStop(code)) {
24801cb0ef41Sopenharmony_ci    MipsDebugger dbg(this);
24811cb0ef41Sopenharmony_ci    dbg.Stop(instr);
24821cb0ef41Sopenharmony_ci  }
24831cb0ef41Sopenharmony_ci}
24841cb0ef41Sopenharmony_ci
24851cb0ef41Sopenharmony_cibool Simulator::IsStopInstruction(Instruction* instr) {
24861cb0ef41Sopenharmony_ci  int32_t func = instr->FunctionFieldRaw();
24871cb0ef41Sopenharmony_ci  uint32_t code = static_cast<uint32_t>(instr->Bits(25, 6));
24881cb0ef41Sopenharmony_ci  return (func == BREAK) && code > kMaxWatchpointCode && code <= kMaxStopCode;
24891cb0ef41Sopenharmony_ci}
24901cb0ef41Sopenharmony_ci
24911cb0ef41Sopenharmony_cibool Simulator::IsEnabledStop(uint32_t code) {
24921cb0ef41Sopenharmony_ci  DCHECK_LE(code, kMaxStopCode);
24931cb0ef41Sopenharmony_ci  DCHECK_GT(code, kMaxWatchpointCode);
24941cb0ef41Sopenharmony_ci  return !(watched_stops_[code].count & kStopDisabledBit);
24951cb0ef41Sopenharmony_ci}
24961cb0ef41Sopenharmony_ci
24971cb0ef41Sopenharmony_civoid Simulator::EnableStop(uint32_t code) {
24981cb0ef41Sopenharmony_ci  if (!IsEnabledStop(code)) {
24991cb0ef41Sopenharmony_ci    watched_stops_[code].count &= ~kStopDisabledBit;
25001cb0ef41Sopenharmony_ci  }
25011cb0ef41Sopenharmony_ci}
25021cb0ef41Sopenharmony_ci
25031cb0ef41Sopenharmony_civoid Simulator::DisableStop(uint32_t code) {
25041cb0ef41Sopenharmony_ci  if (IsEnabledStop(code)) {
25051cb0ef41Sopenharmony_ci    watched_stops_[code].count |= kStopDisabledBit;
25061cb0ef41Sopenharmony_ci  }
25071cb0ef41Sopenharmony_ci}
25081cb0ef41Sopenharmony_ci
25091cb0ef41Sopenharmony_civoid Simulator::IncreaseStopCounter(uint32_t code) {
25101cb0ef41Sopenharmony_ci  DCHECK_LE(code, kMaxStopCode);
25111cb0ef41Sopenharmony_ci  if ((watched_stops_[code].count & ~(1 << 31)) == 0x7FFFFFFF) {
25121cb0ef41Sopenharmony_ci    PrintF(
25131cb0ef41Sopenharmony_ci        "Stop counter for code %i has overflowed.\n"
25141cb0ef41Sopenharmony_ci        "Enabling this code and reseting the counter to 0.\n",
25151cb0ef41Sopenharmony_ci        code);
25161cb0ef41Sopenharmony_ci    watched_stops_[code].count = 0;
25171cb0ef41Sopenharmony_ci    EnableStop(code);
25181cb0ef41Sopenharmony_ci  } else {
25191cb0ef41Sopenharmony_ci    watched_stops_[code].count++;
25201cb0ef41Sopenharmony_ci  }
25211cb0ef41Sopenharmony_ci}
25221cb0ef41Sopenharmony_ci
25231cb0ef41Sopenharmony_ci// Print a stop status.
25241cb0ef41Sopenharmony_civoid Simulator::PrintStopInfo(uint32_t code) {
25251cb0ef41Sopenharmony_ci  if (code <= kMaxWatchpointCode) {
25261cb0ef41Sopenharmony_ci    PrintF("That is a watchpoint, not a stop.\n");
25271cb0ef41Sopenharmony_ci    return;
25281cb0ef41Sopenharmony_ci  } else if (code > kMaxStopCode) {
25291cb0ef41Sopenharmony_ci    PrintF("Code too large, only %u stops can be used\n", kMaxStopCode + 1);
25301cb0ef41Sopenharmony_ci    return;
25311cb0ef41Sopenharmony_ci  }
25321cb0ef41Sopenharmony_ci  const char* state = IsEnabledStop(code) ? "Enabled" : "Disabled";
25331cb0ef41Sopenharmony_ci  int32_t count = watched_stops_[code].count & ~kStopDisabledBit;
25341cb0ef41Sopenharmony_ci  // Don't print the state of unused breakpoints.
25351cb0ef41Sopenharmony_ci  if (count != 0) {
25361cb0ef41Sopenharmony_ci    if (watched_stops_[code].desc) {
25371cb0ef41Sopenharmony_ci      PrintF("stop %i - 0x%x: \t%s, \tcounter = %i, \t%s\n", code, code, state,
25381cb0ef41Sopenharmony_ci             count, watched_stops_[code].desc);
25391cb0ef41Sopenharmony_ci    } else {
25401cb0ef41Sopenharmony_ci      PrintF("stop %i - 0x%x: \t%s, \tcounter = %i\n", code, code, state,
25411cb0ef41Sopenharmony_ci             count);
25421cb0ef41Sopenharmony_ci    }
25431cb0ef41Sopenharmony_ci  }
25441cb0ef41Sopenharmony_ci}
25451cb0ef41Sopenharmony_ci
25461cb0ef41Sopenharmony_civoid Simulator::SignalException(Exception e) {
25471cb0ef41Sopenharmony_ci  FATAL("Error: Exception %i raised.", static_cast<int>(e));
25481cb0ef41Sopenharmony_ci}
25491cb0ef41Sopenharmony_ci
25501cb0ef41Sopenharmony_ci// Min/Max template functions for Double and Single arguments.
25511cb0ef41Sopenharmony_ci
25521cb0ef41Sopenharmony_citemplate <typename T>
25531cb0ef41Sopenharmony_cistatic T FPAbs(T a);
25541cb0ef41Sopenharmony_ci
25551cb0ef41Sopenharmony_citemplate <>
25561cb0ef41Sopenharmony_cidouble FPAbs<double>(double a) {
25571cb0ef41Sopenharmony_ci  return fabs(a);
25581cb0ef41Sopenharmony_ci}
25591cb0ef41Sopenharmony_ci
25601cb0ef41Sopenharmony_citemplate <>
25611cb0ef41Sopenharmony_cifloat FPAbs<float>(float a) {
25621cb0ef41Sopenharmony_ci  return fabsf(a);
25631cb0ef41Sopenharmony_ci}
25641cb0ef41Sopenharmony_ci
25651cb0ef41Sopenharmony_citemplate <typename T>
25661cb0ef41Sopenharmony_cistatic bool FPUProcessNaNsAndZeros(T a, T b, MaxMinKind kind, T* result) {
25671cb0ef41Sopenharmony_ci  if (std::isnan(a) && std::isnan(b)) {
25681cb0ef41Sopenharmony_ci    *result = a;
25691cb0ef41Sopenharmony_ci  } else if (std::isnan(a)) {
25701cb0ef41Sopenharmony_ci    *result = b;
25711cb0ef41Sopenharmony_ci  } else if (std::isnan(b)) {
25721cb0ef41Sopenharmony_ci    *result = a;
25731cb0ef41Sopenharmony_ci  } else if (b == a) {
25741cb0ef41Sopenharmony_ci    // Handle -0.0 == 0.0 case.
25751cb0ef41Sopenharmony_ci    // std::signbit() returns int 0 or 1 so subtracting MaxMinKind::kMax
25761cb0ef41Sopenharmony_ci    // negates the result.
25771cb0ef41Sopenharmony_ci    *result = std::signbit(b) - static_cast<int>(kind) ? b : a;
25781cb0ef41Sopenharmony_ci  } else {
25791cb0ef41Sopenharmony_ci    return false;
25801cb0ef41Sopenharmony_ci  }
25811cb0ef41Sopenharmony_ci  return true;
25821cb0ef41Sopenharmony_ci}
25831cb0ef41Sopenharmony_ci
25841cb0ef41Sopenharmony_citemplate <typename T>
25851cb0ef41Sopenharmony_cistatic T FPUMin(T a, T b) {
25861cb0ef41Sopenharmony_ci  T result;
25871cb0ef41Sopenharmony_ci  if (FPUProcessNaNsAndZeros(a, b, MaxMinKind::kMin, &result)) {
25881cb0ef41Sopenharmony_ci    return result;
25891cb0ef41Sopenharmony_ci  } else {
25901cb0ef41Sopenharmony_ci    return b < a ? b : a;
25911cb0ef41Sopenharmony_ci  }
25921cb0ef41Sopenharmony_ci}
25931cb0ef41Sopenharmony_ci
25941cb0ef41Sopenharmony_citemplate <typename T>
25951cb0ef41Sopenharmony_cistatic T FPUMax(T a, T b) {
25961cb0ef41Sopenharmony_ci  T result;
25971cb0ef41Sopenharmony_ci  if (FPUProcessNaNsAndZeros(a, b, MaxMinKind::kMax, &result)) {
25981cb0ef41Sopenharmony_ci    return result;
25991cb0ef41Sopenharmony_ci  } else {
26001cb0ef41Sopenharmony_ci    return b > a ? b : a;
26011cb0ef41Sopenharmony_ci  }
26021cb0ef41Sopenharmony_ci}
26031cb0ef41Sopenharmony_ci
26041cb0ef41Sopenharmony_citemplate <typename T>
26051cb0ef41Sopenharmony_cistatic T FPUMinA(T a, T b) {
26061cb0ef41Sopenharmony_ci  T result;
26071cb0ef41Sopenharmony_ci  if (!FPUProcessNaNsAndZeros(a, b, MaxMinKind::kMin, &result)) {
26081cb0ef41Sopenharmony_ci    if (FPAbs(a) < FPAbs(b)) {
26091cb0ef41Sopenharmony_ci      result = a;
26101cb0ef41Sopenharmony_ci    } else if (FPAbs(b) < FPAbs(a)) {
26111cb0ef41Sopenharmony_ci      result = b;
26121cb0ef41Sopenharmony_ci    } else {
26131cb0ef41Sopenharmony_ci      result = a < b ? a : b;
26141cb0ef41Sopenharmony_ci    }
26151cb0ef41Sopenharmony_ci  }
26161cb0ef41Sopenharmony_ci  return result;
26171cb0ef41Sopenharmony_ci}
26181cb0ef41Sopenharmony_ci
26191cb0ef41Sopenharmony_citemplate <typename T>
26201cb0ef41Sopenharmony_cistatic T FPUMaxA(T a, T b) {
26211cb0ef41Sopenharmony_ci  T result;
26221cb0ef41Sopenharmony_ci  if (!FPUProcessNaNsAndZeros(a, b, MaxMinKind::kMin, &result)) {
26231cb0ef41Sopenharmony_ci    if (FPAbs(a) > FPAbs(b)) {
26241cb0ef41Sopenharmony_ci      result = a;
26251cb0ef41Sopenharmony_ci    } else if (FPAbs(b) > FPAbs(a)) {
26261cb0ef41Sopenharmony_ci      result = b;
26271cb0ef41Sopenharmony_ci    } else {
26281cb0ef41Sopenharmony_ci      result = a > b ? a : b;
26291cb0ef41Sopenharmony_ci    }
26301cb0ef41Sopenharmony_ci  }
26311cb0ef41Sopenharmony_ci  return result;
26321cb0ef41Sopenharmony_ci}
26331cb0ef41Sopenharmony_ci
26341cb0ef41Sopenharmony_cienum class KeepSign : bool { no = false, yes };
26351cb0ef41Sopenharmony_ci
26361cb0ef41Sopenharmony_citemplate <typename T, typename std::enable_if<std::is_floating_point<T>::value,
26371cb0ef41Sopenharmony_ci                                              int>::type = 0>
26381cb0ef41Sopenharmony_ciT FPUCanonalizeNaNArg(T result, T arg, KeepSign keepSign = KeepSign::no) {
26391cb0ef41Sopenharmony_ci  DCHECK(std::isnan(arg));
26401cb0ef41Sopenharmony_ci  T qNaN = std::numeric_limits<T>::quiet_NaN();
26411cb0ef41Sopenharmony_ci  if (keepSign == KeepSign::yes) {
26421cb0ef41Sopenharmony_ci    return std::copysign(qNaN, result);
26431cb0ef41Sopenharmony_ci  }
26441cb0ef41Sopenharmony_ci  return qNaN;
26451cb0ef41Sopenharmony_ci}
26461cb0ef41Sopenharmony_ci
26471cb0ef41Sopenharmony_citemplate <typename T>
26481cb0ef41Sopenharmony_ciT FPUCanonalizeNaNArgs(T result, KeepSign keepSign, T first) {
26491cb0ef41Sopenharmony_ci  if (std::isnan(first)) {
26501cb0ef41Sopenharmony_ci    return FPUCanonalizeNaNArg(result, first, keepSign);
26511cb0ef41Sopenharmony_ci  }
26521cb0ef41Sopenharmony_ci  return result;
26531cb0ef41Sopenharmony_ci}
26541cb0ef41Sopenharmony_ci
26551cb0ef41Sopenharmony_citemplate <typename T, typename... Args>
26561cb0ef41Sopenharmony_ciT FPUCanonalizeNaNArgs(T result, KeepSign keepSign, T first, Args... args) {
26571cb0ef41Sopenharmony_ci  if (std::isnan(first)) {
26581cb0ef41Sopenharmony_ci    return FPUCanonalizeNaNArg(result, first, keepSign);
26591cb0ef41Sopenharmony_ci  }
26601cb0ef41Sopenharmony_ci  return FPUCanonalizeNaNArgs(result, keepSign, args...);
26611cb0ef41Sopenharmony_ci}
26621cb0ef41Sopenharmony_ci
26631cb0ef41Sopenharmony_citemplate <typename Func, typename T, typename... Args>
26641cb0ef41Sopenharmony_ciT FPUCanonalizeOperation(Func f, T first, Args... args) {
26651cb0ef41Sopenharmony_ci  return FPUCanonalizeOperation(f, KeepSign::no, first, args...);
26661cb0ef41Sopenharmony_ci}
26671cb0ef41Sopenharmony_ci
26681cb0ef41Sopenharmony_citemplate <typename Func, typename T, typename... Args>
26691cb0ef41Sopenharmony_ciT FPUCanonalizeOperation(Func f, KeepSign keepSign, T first, Args... args) {
26701cb0ef41Sopenharmony_ci  T result = f(first, args...);
26711cb0ef41Sopenharmony_ci  if (std::isnan(result)) {
26721cb0ef41Sopenharmony_ci    result = FPUCanonalizeNaNArgs(result, keepSign, first, args...);
26731cb0ef41Sopenharmony_ci  }
26741cb0ef41Sopenharmony_ci  return result;
26751cb0ef41Sopenharmony_ci}
26761cb0ef41Sopenharmony_ci
26771cb0ef41Sopenharmony_ci// Handle execution based on instruction types.
26781cb0ef41Sopenharmony_ci
26791cb0ef41Sopenharmony_civoid Simulator::DecodeTypeRegisterDRsType() {
26801cb0ef41Sopenharmony_ci  double ft, fs, fd;
26811cb0ef41Sopenharmony_ci  uint32_t cc, fcsr_cc;
26821cb0ef41Sopenharmony_ci  int64_t i64;
26831cb0ef41Sopenharmony_ci  fs = get_fpu_register_double(fs_reg());
26841cb0ef41Sopenharmony_ci  ft = (instr_.FunctionFieldRaw() != MOVF) ? get_fpu_register_double(ft_reg())
26851cb0ef41Sopenharmony_ci                                           : 0.0;
26861cb0ef41Sopenharmony_ci  fd = get_fpu_register_double(fd_reg());
26871cb0ef41Sopenharmony_ci  int64_t ft_int = bit_cast<int64_t>(ft);
26881cb0ef41Sopenharmony_ci  int64_t fd_int = bit_cast<int64_t>(fd);
26891cb0ef41Sopenharmony_ci  cc = instr_.FCccValue();
26901cb0ef41Sopenharmony_ci  fcsr_cc = get_fcsr_condition_bit(cc);
26911cb0ef41Sopenharmony_ci  switch (instr_.FunctionFieldRaw()) {
26921cb0ef41Sopenharmony_ci    case RINT: {
26931cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
26941cb0ef41Sopenharmony_ci      double result, temp, temp_result;
26951cb0ef41Sopenharmony_ci      double upper = std::ceil(fs);
26961cb0ef41Sopenharmony_ci      double lower = std::floor(fs);
26971cb0ef41Sopenharmony_ci      switch (get_fcsr_rounding_mode()) {
26981cb0ef41Sopenharmony_ci        case kRoundToNearest:
26991cb0ef41Sopenharmony_ci          if (upper - fs < fs - lower) {
27001cb0ef41Sopenharmony_ci            result = upper;
27011cb0ef41Sopenharmony_ci          } else if (upper - fs > fs - lower) {
27021cb0ef41Sopenharmony_ci            result = lower;
27031cb0ef41Sopenharmony_ci          } else {
27041cb0ef41Sopenharmony_ci            temp_result = upper / 2;
27051cb0ef41Sopenharmony_ci            double reminder = modf(temp_result, &temp);
27061cb0ef41Sopenharmony_ci            if (reminder == 0) {
27071cb0ef41Sopenharmony_ci              result = upper;
27081cb0ef41Sopenharmony_ci            } else {
27091cb0ef41Sopenharmony_ci              result = lower;
27101cb0ef41Sopenharmony_ci            }
27111cb0ef41Sopenharmony_ci          }
27121cb0ef41Sopenharmony_ci          break;
27131cb0ef41Sopenharmony_ci        case kRoundToZero:
27141cb0ef41Sopenharmony_ci          result = (fs > 0 ? lower : upper);
27151cb0ef41Sopenharmony_ci          break;
27161cb0ef41Sopenharmony_ci        case kRoundToPlusInf:
27171cb0ef41Sopenharmony_ci          result = upper;
27181cb0ef41Sopenharmony_ci          break;
27191cb0ef41Sopenharmony_ci        case kRoundToMinusInf:
27201cb0ef41Sopenharmony_ci          result = lower;
27211cb0ef41Sopenharmony_ci          break;
27221cb0ef41Sopenharmony_ci      }
27231cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), result);
27241cb0ef41Sopenharmony_ci      if (result != fs) {
27251cb0ef41Sopenharmony_ci        set_fcsr_bit(kFCSRInexactFlagBit, true);
27261cb0ef41Sopenharmony_ci      }
27271cb0ef41Sopenharmony_ci      break;
27281cb0ef41Sopenharmony_ci    }
27291cb0ef41Sopenharmony_ci    case SEL:
27301cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
27311cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), (fd_int & 0x1) == 0 ? fs : ft);
27321cb0ef41Sopenharmony_ci      break;
27331cb0ef41Sopenharmony_ci    case SELEQZ_C:
27341cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
27351cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), (ft_int & 0x1) == 0 ? fs : 0.0);
27361cb0ef41Sopenharmony_ci      break;
27371cb0ef41Sopenharmony_ci    case SELNEZ_C:
27381cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
27391cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), (ft_int & 0x1) != 0 ? fs : 0.0);
27401cb0ef41Sopenharmony_ci      break;
27411cb0ef41Sopenharmony_ci    case MOVZ_C: {
27421cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r2));
27431cb0ef41Sopenharmony_ci      if (rt() == 0) {
27441cb0ef41Sopenharmony_ci        SetFPUDoubleResult(fd_reg(), fs);
27451cb0ef41Sopenharmony_ci      }
27461cb0ef41Sopenharmony_ci      break;
27471cb0ef41Sopenharmony_ci    }
27481cb0ef41Sopenharmony_ci    case MOVN_C: {
27491cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r2));
27501cb0ef41Sopenharmony_ci      int32_t rt_reg = instr_.RtValue();
27511cb0ef41Sopenharmony_ci      int32_t rt = get_register(rt_reg);
27521cb0ef41Sopenharmony_ci      if (rt != 0) {
27531cb0ef41Sopenharmony_ci        SetFPUDoubleResult(fd_reg(), fs);
27541cb0ef41Sopenharmony_ci      }
27551cb0ef41Sopenharmony_ci      break;
27561cb0ef41Sopenharmony_ci    }
27571cb0ef41Sopenharmony_ci    case MOVF: {
27581cb0ef41Sopenharmony_ci      // Same function field for MOVT.D and MOVF.D
27591cb0ef41Sopenharmony_ci      uint32_t ft_cc = (ft_reg() >> 2) & 0x7;
27601cb0ef41Sopenharmony_ci      ft_cc = get_fcsr_condition_bit(ft_cc);
27611cb0ef41Sopenharmony_ci      if (instr_.Bit(16)) {  // Read Tf bit.
27621cb0ef41Sopenharmony_ci        // MOVT.D
27631cb0ef41Sopenharmony_ci        if (test_fcsr_bit(ft_cc)) SetFPUDoubleResult(fd_reg(), fs);
27641cb0ef41Sopenharmony_ci      } else {
27651cb0ef41Sopenharmony_ci        // MOVF.D
27661cb0ef41Sopenharmony_ci        if (!test_fcsr_bit(ft_cc)) SetFPUDoubleResult(fd_reg(), fs);
27671cb0ef41Sopenharmony_ci      }
27681cb0ef41Sopenharmony_ci      break;
27691cb0ef41Sopenharmony_ci    }
27701cb0ef41Sopenharmony_ci    case MIN:
27711cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
27721cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), FPUMin(ft, fs));
27731cb0ef41Sopenharmony_ci      break;
27741cb0ef41Sopenharmony_ci    case MAX:
27751cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
27761cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), FPUMax(ft, fs));
27771cb0ef41Sopenharmony_ci      break;
27781cb0ef41Sopenharmony_ci    case MINA:
27791cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
27801cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), FPUMinA(ft, fs));
27811cb0ef41Sopenharmony_ci      break;
27821cb0ef41Sopenharmony_ci    case MAXA:
27831cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
27841cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), FPUMaxA(ft, fs));
27851cb0ef41Sopenharmony_ci      break;
27861cb0ef41Sopenharmony_ci    case ADD_D:
27871cb0ef41Sopenharmony_ci      SetFPUDoubleResult(
27881cb0ef41Sopenharmony_ci          fd_reg(),
27891cb0ef41Sopenharmony_ci          FPUCanonalizeOperation(
27901cb0ef41Sopenharmony_ci              [](double lhs, double rhs) { return lhs + rhs; }, fs, ft));
27911cb0ef41Sopenharmony_ci      break;
27921cb0ef41Sopenharmony_ci    case SUB_D:
27931cb0ef41Sopenharmony_ci      SetFPUDoubleResult(
27941cb0ef41Sopenharmony_ci          fd_reg(),
27951cb0ef41Sopenharmony_ci          FPUCanonalizeOperation(
27961cb0ef41Sopenharmony_ci              [](double lhs, double rhs) { return lhs - rhs; }, fs, ft));
27971cb0ef41Sopenharmony_ci      break;
27981cb0ef41Sopenharmony_ci    case MADDF_D:
27991cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
28001cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), std::fma(fs, ft, fd));
28011cb0ef41Sopenharmony_ci      break;
28021cb0ef41Sopenharmony_ci    case MSUBF_D:
28031cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
28041cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), std::fma(-fs, ft, fd));
28051cb0ef41Sopenharmony_ci      break;
28061cb0ef41Sopenharmony_ci    case MUL_D:
28071cb0ef41Sopenharmony_ci      SetFPUDoubleResult(
28081cb0ef41Sopenharmony_ci          fd_reg(),
28091cb0ef41Sopenharmony_ci          FPUCanonalizeOperation(
28101cb0ef41Sopenharmony_ci              [](double lhs, double rhs) { return lhs * rhs; }, fs, ft));
28111cb0ef41Sopenharmony_ci      break;
28121cb0ef41Sopenharmony_ci    case DIV_D:
28131cb0ef41Sopenharmony_ci      SetFPUDoubleResult(
28141cb0ef41Sopenharmony_ci          fd_reg(),
28151cb0ef41Sopenharmony_ci          FPUCanonalizeOperation(
28161cb0ef41Sopenharmony_ci              [](double lhs, double rhs) { return lhs / rhs; }, fs, ft));
28171cb0ef41Sopenharmony_ci      break;
28181cb0ef41Sopenharmony_ci    case ABS_D:
28191cb0ef41Sopenharmony_ci      SetFPUDoubleResult(
28201cb0ef41Sopenharmony_ci          fd_reg(),
28211cb0ef41Sopenharmony_ci          FPUCanonalizeOperation([](double fs) { return FPAbs(fs); }, fs));
28221cb0ef41Sopenharmony_ci      break;
28231cb0ef41Sopenharmony_ci    case MOV_D:
28241cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), fs);
28251cb0ef41Sopenharmony_ci      break;
28261cb0ef41Sopenharmony_ci    case NEG_D:
28271cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(),
28281cb0ef41Sopenharmony_ci                         FPUCanonalizeOperation([](double src) { return -src; },
28291cb0ef41Sopenharmony_ci                                                KeepSign::yes, fs));
28301cb0ef41Sopenharmony_ci      break;
28311cb0ef41Sopenharmony_ci    case SQRT_D:
28321cb0ef41Sopenharmony_ci      SetFPUDoubleResult(
28331cb0ef41Sopenharmony_ci          fd_reg(),
28341cb0ef41Sopenharmony_ci          FPUCanonalizeOperation([](double fs) { return std::sqrt(fs); }, fs));
28351cb0ef41Sopenharmony_ci      break;
28361cb0ef41Sopenharmony_ci    case RSQRT_D:
28371cb0ef41Sopenharmony_ci      SetFPUDoubleResult(
28381cb0ef41Sopenharmony_ci          fd_reg(), FPUCanonalizeOperation(
28391cb0ef41Sopenharmony_ci                        [](double fs) { return 1.0 / std::sqrt(fs); }, fs));
28401cb0ef41Sopenharmony_ci      break;
28411cb0ef41Sopenharmony_ci    case RECIP_D:
28421cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), FPUCanonalizeOperation(
28431cb0ef41Sopenharmony_ci                                       [](double fs) { return 1.0 / fs; }, fs));
28441cb0ef41Sopenharmony_ci      break;
28451cb0ef41Sopenharmony_ci    case C_UN_D:
28461cb0ef41Sopenharmony_ci      set_fcsr_bit(fcsr_cc, std::isnan(fs) || std::isnan(ft));
28471cb0ef41Sopenharmony_ci      TraceRegWr(test_fcsr_bit(fcsr_cc));
28481cb0ef41Sopenharmony_ci      break;
28491cb0ef41Sopenharmony_ci    case C_EQ_D:
28501cb0ef41Sopenharmony_ci      set_fcsr_bit(fcsr_cc, (fs == ft));
28511cb0ef41Sopenharmony_ci      TraceRegWr(test_fcsr_bit(fcsr_cc));
28521cb0ef41Sopenharmony_ci      break;
28531cb0ef41Sopenharmony_ci    case C_UEQ_D:
28541cb0ef41Sopenharmony_ci      set_fcsr_bit(fcsr_cc, (fs == ft) || (std::isnan(fs) || std::isnan(ft)));
28551cb0ef41Sopenharmony_ci      TraceRegWr(test_fcsr_bit(fcsr_cc));
28561cb0ef41Sopenharmony_ci      break;
28571cb0ef41Sopenharmony_ci    case C_OLT_D:
28581cb0ef41Sopenharmony_ci      set_fcsr_bit(fcsr_cc, (fs < ft));
28591cb0ef41Sopenharmony_ci      TraceRegWr(test_fcsr_bit(fcsr_cc));
28601cb0ef41Sopenharmony_ci      break;
28611cb0ef41Sopenharmony_ci    case C_ULT_D:
28621cb0ef41Sopenharmony_ci      set_fcsr_bit(fcsr_cc, (fs < ft) || (std::isnan(fs) || std::isnan(ft)));
28631cb0ef41Sopenharmony_ci      TraceRegWr(test_fcsr_bit(fcsr_cc));
28641cb0ef41Sopenharmony_ci      break;
28651cb0ef41Sopenharmony_ci    case C_OLE_D:
28661cb0ef41Sopenharmony_ci      set_fcsr_bit(fcsr_cc, (fs <= ft));
28671cb0ef41Sopenharmony_ci      TraceRegWr(test_fcsr_bit(fcsr_cc));
28681cb0ef41Sopenharmony_ci      break;
28691cb0ef41Sopenharmony_ci    case C_ULE_D:
28701cb0ef41Sopenharmony_ci      set_fcsr_bit(fcsr_cc, (fs <= ft) || (std::isnan(fs) || std::isnan(ft)));
28711cb0ef41Sopenharmony_ci      TraceRegWr(test_fcsr_bit(fcsr_cc));
28721cb0ef41Sopenharmony_ci      break;
28731cb0ef41Sopenharmony_ci    case CVT_W_D: {  // Convert double to word.
28741cb0ef41Sopenharmony_ci      double rounded;
28751cb0ef41Sopenharmony_ci      int32_t result;
28761cb0ef41Sopenharmony_ci      round_according_to_fcsr(fs, &rounded, &result, fs);
28771cb0ef41Sopenharmony_ci      SetFPUWordResult(fd_reg(), result);
28781cb0ef41Sopenharmony_ci      if (set_fcsr_round_error(fs, rounded)) {
28791cb0ef41Sopenharmony_ci        set_fpu_register_word_invalid_result(fs, rounded);
28801cb0ef41Sopenharmony_ci      }
28811cb0ef41Sopenharmony_ci    } break;
28821cb0ef41Sopenharmony_ci    case ROUND_W_D:  // Round double to word (round half to even).
28831cb0ef41Sopenharmony_ci    {
28841cb0ef41Sopenharmony_ci      double rounded = std::floor(fs + 0.5);
28851cb0ef41Sopenharmony_ci      int32_t result = static_cast<int32_t>(rounded);
28861cb0ef41Sopenharmony_ci      if ((result & 1) != 0 && result - fs == 0.5) {
28871cb0ef41Sopenharmony_ci        // If the number is halfway between two integers,
28881cb0ef41Sopenharmony_ci        // round to the even one.
28891cb0ef41Sopenharmony_ci        result--;
28901cb0ef41Sopenharmony_ci      }
28911cb0ef41Sopenharmony_ci      SetFPUWordResult(fd_reg(), result);
28921cb0ef41Sopenharmony_ci      if (set_fcsr_round_error(fs, rounded)) {
28931cb0ef41Sopenharmony_ci        set_fpu_register_word_invalid_result(fs, rounded);
28941cb0ef41Sopenharmony_ci      }
28951cb0ef41Sopenharmony_ci    } break;
28961cb0ef41Sopenharmony_ci    case TRUNC_W_D:  // Truncate double to word (round towards 0).
28971cb0ef41Sopenharmony_ci    {
28981cb0ef41Sopenharmony_ci      double rounded = trunc(fs);
28991cb0ef41Sopenharmony_ci      int32_t result = static_cast<int32_t>(rounded);
29001cb0ef41Sopenharmony_ci      SetFPUWordResult(fd_reg(), result);
29011cb0ef41Sopenharmony_ci      if (set_fcsr_round_error(fs, rounded)) {
29021cb0ef41Sopenharmony_ci        set_fpu_register_word_invalid_result(fs, rounded);
29031cb0ef41Sopenharmony_ci      }
29041cb0ef41Sopenharmony_ci    } break;
29051cb0ef41Sopenharmony_ci    case FLOOR_W_D:  // Round double to word towards negative infinity.
29061cb0ef41Sopenharmony_ci    {
29071cb0ef41Sopenharmony_ci      double rounded = std::floor(fs);
29081cb0ef41Sopenharmony_ci      int32_t result = static_cast<int32_t>(rounded);
29091cb0ef41Sopenharmony_ci      SetFPUWordResult(fd_reg(), result);
29101cb0ef41Sopenharmony_ci      if (set_fcsr_round_error(fs, rounded)) {
29111cb0ef41Sopenharmony_ci        set_fpu_register_word_invalid_result(fs, rounded);
29121cb0ef41Sopenharmony_ci      }
29131cb0ef41Sopenharmony_ci    } break;
29141cb0ef41Sopenharmony_ci    case CEIL_W_D:  // Round double to word towards positive infinity.
29151cb0ef41Sopenharmony_ci    {
29161cb0ef41Sopenharmony_ci      double rounded = std::ceil(fs);
29171cb0ef41Sopenharmony_ci      int32_t result = static_cast<int32_t>(rounded);
29181cb0ef41Sopenharmony_ci      SetFPUWordResult(fd_reg(), result);
29191cb0ef41Sopenharmony_ci      if (set_fcsr_round_error(fs, rounded)) {
29201cb0ef41Sopenharmony_ci        set_fpu_register_word_invalid_result(fs, rounded);
29211cb0ef41Sopenharmony_ci      }
29221cb0ef41Sopenharmony_ci    } break;
29231cb0ef41Sopenharmony_ci    case CVT_S_D:  // Convert double to float (single).
29241cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), static_cast<float>(fs));
29251cb0ef41Sopenharmony_ci      break;
29261cb0ef41Sopenharmony_ci    case CVT_L_D: {  // Mips32r2: Truncate double to 64-bit long-word.
29271cb0ef41Sopenharmony_ci      if (IsFp64Mode()) {
29281cb0ef41Sopenharmony_ci        int64_t result;
29291cb0ef41Sopenharmony_ci        double rounded;
29301cb0ef41Sopenharmony_ci        round64_according_to_fcsr(fs, &rounded, &result, fs);
29311cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), result);
29321cb0ef41Sopenharmony_ci        if (set_fcsr_round64_error(fs, rounded)) {
29331cb0ef41Sopenharmony_ci          set_fpu_register_invalid_result64(fs, rounded);
29341cb0ef41Sopenharmony_ci        }
29351cb0ef41Sopenharmony_ci      } else {
29361cb0ef41Sopenharmony_ci        UNSUPPORTED();
29371cb0ef41Sopenharmony_ci      }
29381cb0ef41Sopenharmony_ci      break;
29391cb0ef41Sopenharmony_ci    }
29401cb0ef41Sopenharmony_ci    case TRUNC_L_D: {  // Mips32r2 instruction.
29411cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6));
29421cb0ef41Sopenharmony_ci      double rounded = trunc(fs);
29431cb0ef41Sopenharmony_ci      i64 = static_cast<int64_t>(rounded);
29441cb0ef41Sopenharmony_ci      if (IsFp64Mode()) {
29451cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), i64);
29461cb0ef41Sopenharmony_ci        if (set_fcsr_round64_error(fs, rounded)) {
29471cb0ef41Sopenharmony_ci          set_fpu_register_invalid_result64(fs, rounded);
29481cb0ef41Sopenharmony_ci        }
29491cb0ef41Sopenharmony_ci      } else {
29501cb0ef41Sopenharmony_ci        UNSUPPORTED();
29511cb0ef41Sopenharmony_ci      }
29521cb0ef41Sopenharmony_ci      break;
29531cb0ef41Sopenharmony_ci    }
29541cb0ef41Sopenharmony_ci    case ROUND_L_D: {  // Mips32r2 instruction.
29551cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6));
29561cb0ef41Sopenharmony_ci      double rounded = std::floor(fs + 0.5);
29571cb0ef41Sopenharmony_ci      int64_t result = static_cast<int64_t>(rounded);
29581cb0ef41Sopenharmony_ci      if ((result & 1) != 0 && result - fs == 0.5) {
29591cb0ef41Sopenharmony_ci        // If the number is halfway between two integers,
29601cb0ef41Sopenharmony_ci        // round to the even one.
29611cb0ef41Sopenharmony_ci        result--;
29621cb0ef41Sopenharmony_ci      }
29631cb0ef41Sopenharmony_ci      int64_t i64 = static_cast<int64_t>(result);
29641cb0ef41Sopenharmony_ci      if (IsFp64Mode()) {
29651cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), i64);
29661cb0ef41Sopenharmony_ci        if (set_fcsr_round64_error(fs, rounded)) {
29671cb0ef41Sopenharmony_ci          set_fpu_register_invalid_result64(fs, rounded);
29681cb0ef41Sopenharmony_ci        }
29691cb0ef41Sopenharmony_ci      } else {
29701cb0ef41Sopenharmony_ci        UNSUPPORTED();
29711cb0ef41Sopenharmony_ci      }
29721cb0ef41Sopenharmony_ci      break;
29731cb0ef41Sopenharmony_ci    }
29741cb0ef41Sopenharmony_ci    case FLOOR_L_D: {  // Mips32r2 instruction.
29751cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6));
29761cb0ef41Sopenharmony_ci      double rounded = std::floor(fs);
29771cb0ef41Sopenharmony_ci      int64_t i64 = static_cast<int64_t>(rounded);
29781cb0ef41Sopenharmony_ci      if (IsFp64Mode()) {
29791cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), i64);
29801cb0ef41Sopenharmony_ci        if (set_fcsr_round64_error(fs, rounded)) {
29811cb0ef41Sopenharmony_ci          set_fpu_register_invalid_result64(fs, rounded);
29821cb0ef41Sopenharmony_ci        }
29831cb0ef41Sopenharmony_ci      } else {
29841cb0ef41Sopenharmony_ci        UNSUPPORTED();
29851cb0ef41Sopenharmony_ci      }
29861cb0ef41Sopenharmony_ci      break;
29871cb0ef41Sopenharmony_ci    }
29881cb0ef41Sopenharmony_ci    case CEIL_L_D: {  // Mips32r2 instruction.
29891cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6));
29901cb0ef41Sopenharmony_ci      double rounded = std::ceil(fs);
29911cb0ef41Sopenharmony_ci      int64_t i64 = static_cast<int64_t>(rounded);
29921cb0ef41Sopenharmony_ci      if (IsFp64Mode()) {
29931cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), i64);
29941cb0ef41Sopenharmony_ci        if (set_fcsr_round64_error(fs, rounded)) {
29951cb0ef41Sopenharmony_ci          set_fpu_register_invalid_result64(fs, rounded);
29961cb0ef41Sopenharmony_ci        }
29971cb0ef41Sopenharmony_ci      } else {
29981cb0ef41Sopenharmony_ci        UNSUPPORTED();
29991cb0ef41Sopenharmony_ci      }
30001cb0ef41Sopenharmony_ci      break;
30011cb0ef41Sopenharmony_ci    }
30021cb0ef41Sopenharmony_ci    case CLASS_D: {  // Mips32r6 instruction
30031cb0ef41Sopenharmony_ci      // Convert double input to uint64_t for easier bit manipulation
30041cb0ef41Sopenharmony_ci      uint64_t classed = bit_cast<uint64_t>(fs);
30051cb0ef41Sopenharmony_ci
30061cb0ef41Sopenharmony_ci      // Extracting sign, exponent and mantissa from the input double
30071cb0ef41Sopenharmony_ci      uint32_t sign = (classed >> 63) & 1;
30081cb0ef41Sopenharmony_ci      uint32_t exponent = (classed >> 52) & 0x00000000000007FF;
30091cb0ef41Sopenharmony_ci      uint64_t mantissa = classed & 0x000FFFFFFFFFFFFF;
30101cb0ef41Sopenharmony_ci      uint64_t result;
30111cb0ef41Sopenharmony_ci      double dResult;
30121cb0ef41Sopenharmony_ci
30131cb0ef41Sopenharmony_ci      // Setting flags if input double is negative infinity,
30141cb0ef41Sopenharmony_ci      // positive infinity, negative zero or positive zero
30151cb0ef41Sopenharmony_ci      bool negInf = (classed == 0xFFF0000000000000);
30161cb0ef41Sopenharmony_ci      bool posInf = (classed == 0x7FF0000000000000);
30171cb0ef41Sopenharmony_ci      bool negZero = (classed == 0x8000000000000000);
30181cb0ef41Sopenharmony_ci      bool posZero = (classed == 0x0000000000000000);
30191cb0ef41Sopenharmony_ci
30201cb0ef41Sopenharmony_ci      bool signalingNan;
30211cb0ef41Sopenharmony_ci      bool quietNan;
30221cb0ef41Sopenharmony_ci      bool negSubnorm;
30231cb0ef41Sopenharmony_ci      bool posSubnorm;
30241cb0ef41Sopenharmony_ci      bool negNorm;
30251cb0ef41Sopenharmony_ci      bool posNorm;
30261cb0ef41Sopenharmony_ci
30271cb0ef41Sopenharmony_ci      // Setting flags if double is NaN
30281cb0ef41Sopenharmony_ci      signalingNan = false;
30291cb0ef41Sopenharmony_ci      quietNan = false;
30301cb0ef41Sopenharmony_ci      if (!negInf && !posInf && exponent == 0x7FF) {
30311cb0ef41Sopenharmony_ci        quietNan = ((mantissa & 0x0008000000000000) != 0) &&
30321cb0ef41Sopenharmony_ci                   ((mantissa & (0x0008000000000000 - 1)) == 0);
30331cb0ef41Sopenharmony_ci        signalingNan = !quietNan;
30341cb0ef41Sopenharmony_ci      }
30351cb0ef41Sopenharmony_ci
30361cb0ef41Sopenharmony_ci      // Setting flags if double is subnormal number
30371cb0ef41Sopenharmony_ci      posSubnorm = false;
30381cb0ef41Sopenharmony_ci      negSubnorm = false;
30391cb0ef41Sopenharmony_ci      if ((exponent == 0) && (mantissa != 0)) {
30401cb0ef41Sopenharmony_ci        DCHECK(sign == 0 || sign == 1);
30411cb0ef41Sopenharmony_ci        posSubnorm = (sign == 0);
30421cb0ef41Sopenharmony_ci        negSubnorm = (sign == 1);
30431cb0ef41Sopenharmony_ci      }
30441cb0ef41Sopenharmony_ci
30451cb0ef41Sopenharmony_ci      // Setting flags if double is normal number
30461cb0ef41Sopenharmony_ci      posNorm = false;
30471cb0ef41Sopenharmony_ci      negNorm = false;
30481cb0ef41Sopenharmony_ci      if (!posSubnorm && !negSubnorm && !posInf && !negInf && !signalingNan &&
30491cb0ef41Sopenharmony_ci          !quietNan && !negZero && !posZero) {
30501cb0ef41Sopenharmony_ci        DCHECK(sign == 0 || sign == 1);
30511cb0ef41Sopenharmony_ci        posNorm = (sign == 0);
30521cb0ef41Sopenharmony_ci        negNorm = (sign == 1);
30531cb0ef41Sopenharmony_ci      }
30541cb0ef41Sopenharmony_ci
30551cb0ef41Sopenharmony_ci      // Calculating result according to description of CLASS.D instruction
30561cb0ef41Sopenharmony_ci      result = (posZero << 9) | (posSubnorm << 8) | (posNorm << 7) |
30571cb0ef41Sopenharmony_ci               (posInf << 6) | (negZero << 5) | (negSubnorm << 4) |
30581cb0ef41Sopenharmony_ci               (negNorm << 3) | (negInf << 2) | (quietNan << 1) | signalingNan;
30591cb0ef41Sopenharmony_ci
30601cb0ef41Sopenharmony_ci      DCHECK_NE(result, 0);
30611cb0ef41Sopenharmony_ci
30621cb0ef41Sopenharmony_ci      dResult = bit_cast<double>(result);
30631cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), dResult);
30641cb0ef41Sopenharmony_ci
30651cb0ef41Sopenharmony_ci      break;
30661cb0ef41Sopenharmony_ci    }
30671cb0ef41Sopenharmony_ci    case C_F_D: {
30681cb0ef41Sopenharmony_ci      set_fcsr_bit(fcsr_cc, false);
30691cb0ef41Sopenharmony_ci      TraceRegWr(test_fcsr_bit(fcsr_cc));
30701cb0ef41Sopenharmony_ci      break;
30711cb0ef41Sopenharmony_ci    }
30721cb0ef41Sopenharmony_ci    default:
30731cb0ef41Sopenharmony_ci      UNREACHABLE();
30741cb0ef41Sopenharmony_ci  }
30751cb0ef41Sopenharmony_ci}
30761cb0ef41Sopenharmony_ci
30771cb0ef41Sopenharmony_civoid Simulator::DecodeTypeRegisterWRsType() {
30781cb0ef41Sopenharmony_ci  float fs = get_fpu_register_float(fs_reg());
30791cb0ef41Sopenharmony_ci  float ft = get_fpu_register_float(ft_reg());
30801cb0ef41Sopenharmony_ci  int32_t alu_out = 0x12345678;
30811cb0ef41Sopenharmony_ci  switch (instr_.FunctionFieldRaw()) {
30821cb0ef41Sopenharmony_ci    case CVT_S_W:  // Convert word to float (single).
30831cb0ef41Sopenharmony_ci      alu_out = get_fpu_register_signed_word(fs_reg());
30841cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), static_cast<float>(alu_out));
30851cb0ef41Sopenharmony_ci      break;
30861cb0ef41Sopenharmony_ci    case CVT_D_W:  // Convert word to double.
30871cb0ef41Sopenharmony_ci      alu_out = get_fpu_register_signed_word(fs_reg());
30881cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), static_cast<double>(alu_out));
30891cb0ef41Sopenharmony_ci      break;
30901cb0ef41Sopenharmony_ci    case CMP_AF:
30911cb0ef41Sopenharmony_ci      SetFPUWordResult(fd_reg(), 0);
30921cb0ef41Sopenharmony_ci      break;
30931cb0ef41Sopenharmony_ci    case CMP_UN:
30941cb0ef41Sopenharmony_ci      if (std::isnan(fs) || std::isnan(ft)) {
30951cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), -1);
30961cb0ef41Sopenharmony_ci      } else {
30971cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), 0);
30981cb0ef41Sopenharmony_ci      }
30991cb0ef41Sopenharmony_ci      break;
31001cb0ef41Sopenharmony_ci    case CMP_EQ:
31011cb0ef41Sopenharmony_ci      if (fs == ft) {
31021cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), -1);
31031cb0ef41Sopenharmony_ci      } else {
31041cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), 0);
31051cb0ef41Sopenharmony_ci      }
31061cb0ef41Sopenharmony_ci      break;
31071cb0ef41Sopenharmony_ci    case CMP_UEQ:
31081cb0ef41Sopenharmony_ci      if ((fs == ft) || (std::isnan(fs) || std::isnan(ft))) {
31091cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), -1);
31101cb0ef41Sopenharmony_ci      } else {
31111cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), 0);
31121cb0ef41Sopenharmony_ci      }
31131cb0ef41Sopenharmony_ci      break;
31141cb0ef41Sopenharmony_ci    case CMP_LT:
31151cb0ef41Sopenharmony_ci      if (fs < ft) {
31161cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), -1);
31171cb0ef41Sopenharmony_ci      } else {
31181cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), 0);
31191cb0ef41Sopenharmony_ci      }
31201cb0ef41Sopenharmony_ci      break;
31211cb0ef41Sopenharmony_ci    case CMP_ULT:
31221cb0ef41Sopenharmony_ci      if ((fs < ft) || (std::isnan(fs) || std::isnan(ft))) {
31231cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), -1);
31241cb0ef41Sopenharmony_ci      } else {
31251cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), 0);
31261cb0ef41Sopenharmony_ci      }
31271cb0ef41Sopenharmony_ci      break;
31281cb0ef41Sopenharmony_ci    case CMP_LE:
31291cb0ef41Sopenharmony_ci      if (fs <= ft) {
31301cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), -1);
31311cb0ef41Sopenharmony_ci      } else {
31321cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), 0);
31331cb0ef41Sopenharmony_ci      }
31341cb0ef41Sopenharmony_ci      break;
31351cb0ef41Sopenharmony_ci    case CMP_ULE:
31361cb0ef41Sopenharmony_ci      if ((fs <= ft) || (std::isnan(fs) || std::isnan(ft))) {
31371cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), -1);
31381cb0ef41Sopenharmony_ci      } else {
31391cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), 0);
31401cb0ef41Sopenharmony_ci      }
31411cb0ef41Sopenharmony_ci      break;
31421cb0ef41Sopenharmony_ci    case CMP_OR:
31431cb0ef41Sopenharmony_ci      if (!std::isnan(fs) && !std::isnan(ft)) {
31441cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), -1);
31451cb0ef41Sopenharmony_ci      } else {
31461cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), 0);
31471cb0ef41Sopenharmony_ci      }
31481cb0ef41Sopenharmony_ci      break;
31491cb0ef41Sopenharmony_ci    case CMP_UNE:
31501cb0ef41Sopenharmony_ci      if ((fs != ft) || (std::isnan(fs) || std::isnan(ft))) {
31511cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), -1);
31521cb0ef41Sopenharmony_ci      } else {
31531cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), 0);
31541cb0ef41Sopenharmony_ci      }
31551cb0ef41Sopenharmony_ci      break;
31561cb0ef41Sopenharmony_ci    case CMP_NE:
31571cb0ef41Sopenharmony_ci      if (fs != ft) {
31581cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), -1);
31591cb0ef41Sopenharmony_ci      } else {
31601cb0ef41Sopenharmony_ci        SetFPUWordResult(fd_reg(), 0);
31611cb0ef41Sopenharmony_ci      }
31621cb0ef41Sopenharmony_ci      break;
31631cb0ef41Sopenharmony_ci    default:
31641cb0ef41Sopenharmony_ci      UNREACHABLE();
31651cb0ef41Sopenharmony_ci  }
31661cb0ef41Sopenharmony_ci}
31671cb0ef41Sopenharmony_ci
31681cb0ef41Sopenharmony_civoid Simulator::DecodeTypeRegisterSRsType() {
31691cb0ef41Sopenharmony_ci  float fs, ft, fd;
31701cb0ef41Sopenharmony_ci  fs = get_fpu_register_float(fs_reg());
31711cb0ef41Sopenharmony_ci  ft = get_fpu_register_float(ft_reg());
31721cb0ef41Sopenharmony_ci  fd = get_fpu_register_float(fd_reg());
31731cb0ef41Sopenharmony_ci  int32_t ft_int = bit_cast<int32_t>(ft);
31741cb0ef41Sopenharmony_ci  int32_t fd_int = bit_cast<int32_t>(fd);
31751cb0ef41Sopenharmony_ci  uint32_t cc, fcsr_cc;
31761cb0ef41Sopenharmony_ci  cc = instr_.FCccValue();
31771cb0ef41Sopenharmony_ci  fcsr_cc = get_fcsr_condition_bit(cc);
31781cb0ef41Sopenharmony_ci  switch (instr_.FunctionFieldRaw()) {
31791cb0ef41Sopenharmony_ci    case RINT: {
31801cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
31811cb0ef41Sopenharmony_ci      float result, temp_result;
31821cb0ef41Sopenharmony_ci      double temp;
31831cb0ef41Sopenharmony_ci      float upper = std::ceil(fs);
31841cb0ef41Sopenharmony_ci      float lower = std::floor(fs);
31851cb0ef41Sopenharmony_ci      switch (get_fcsr_rounding_mode()) {
31861cb0ef41Sopenharmony_ci        case kRoundToNearest:
31871cb0ef41Sopenharmony_ci          if (upper - fs < fs - lower) {
31881cb0ef41Sopenharmony_ci            result = upper;
31891cb0ef41Sopenharmony_ci          } else if (upper - fs > fs - lower) {
31901cb0ef41Sopenharmony_ci            result = lower;
31911cb0ef41Sopenharmony_ci          } else {
31921cb0ef41Sopenharmony_ci            temp_result = upper / 2;
31931cb0ef41Sopenharmony_ci            float reminder = modf(temp_result, &temp);
31941cb0ef41Sopenharmony_ci            if (reminder == 0) {
31951cb0ef41Sopenharmony_ci              result = upper;
31961cb0ef41Sopenharmony_ci            } else {
31971cb0ef41Sopenharmony_ci              result = lower;
31981cb0ef41Sopenharmony_ci            }
31991cb0ef41Sopenharmony_ci          }
32001cb0ef41Sopenharmony_ci          break;
32011cb0ef41Sopenharmony_ci        case kRoundToZero:
32021cb0ef41Sopenharmony_ci          result = (fs > 0 ? lower : upper);
32031cb0ef41Sopenharmony_ci          break;
32041cb0ef41Sopenharmony_ci        case kRoundToPlusInf:
32051cb0ef41Sopenharmony_ci          result = upper;
32061cb0ef41Sopenharmony_ci          break;
32071cb0ef41Sopenharmony_ci        case kRoundToMinusInf:
32081cb0ef41Sopenharmony_ci          result = lower;
32091cb0ef41Sopenharmony_ci          break;
32101cb0ef41Sopenharmony_ci      }
32111cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), result);
32121cb0ef41Sopenharmony_ci      if (result != fs) {
32131cb0ef41Sopenharmony_ci        set_fcsr_bit(kFCSRInexactFlagBit, true);
32141cb0ef41Sopenharmony_ci      }
32151cb0ef41Sopenharmony_ci      break;
32161cb0ef41Sopenharmony_ci    }
32171cb0ef41Sopenharmony_ci    case ADD_S:
32181cb0ef41Sopenharmony_ci      SetFPUFloatResult(
32191cb0ef41Sopenharmony_ci          fd_reg(),
32201cb0ef41Sopenharmony_ci          FPUCanonalizeOperation([](float lhs, float rhs) { return lhs + rhs; },
32211cb0ef41Sopenharmony_ci                                 fs, ft));
32221cb0ef41Sopenharmony_ci      break;
32231cb0ef41Sopenharmony_ci    case SUB_S:
32241cb0ef41Sopenharmony_ci      SetFPUFloatResult(
32251cb0ef41Sopenharmony_ci          fd_reg(),
32261cb0ef41Sopenharmony_ci          FPUCanonalizeOperation([](float lhs, float rhs) { return lhs - rhs; },
32271cb0ef41Sopenharmony_ci                                 fs, ft));
32281cb0ef41Sopenharmony_ci      break;
32291cb0ef41Sopenharmony_ci    case MADDF_S:
32301cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
32311cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), std::fma(fs, ft, fd));
32321cb0ef41Sopenharmony_ci      break;
32331cb0ef41Sopenharmony_ci    case MSUBF_S:
32341cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
32351cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), std::fma(-fs, ft, fd));
32361cb0ef41Sopenharmony_ci      break;
32371cb0ef41Sopenharmony_ci    case MUL_S:
32381cb0ef41Sopenharmony_ci      SetFPUFloatResult(
32391cb0ef41Sopenharmony_ci          fd_reg(),
32401cb0ef41Sopenharmony_ci          FPUCanonalizeOperation([](float lhs, float rhs) { return lhs * rhs; },
32411cb0ef41Sopenharmony_ci                                 fs, ft));
32421cb0ef41Sopenharmony_ci      break;
32431cb0ef41Sopenharmony_ci    case DIV_S:
32441cb0ef41Sopenharmony_ci      SetFPUFloatResult(
32451cb0ef41Sopenharmony_ci          fd_reg(),
32461cb0ef41Sopenharmony_ci          FPUCanonalizeOperation([](float lhs, float rhs) { return lhs / rhs; },
32471cb0ef41Sopenharmony_ci                                 fs, ft));
32481cb0ef41Sopenharmony_ci      break;
32491cb0ef41Sopenharmony_ci    case ABS_S:
32501cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), FPUCanonalizeOperation(
32511cb0ef41Sopenharmony_ci                                      [](float fs) { return FPAbs(fs); }, fs));
32521cb0ef41Sopenharmony_ci      break;
32531cb0ef41Sopenharmony_ci    case MOV_S:
32541cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), fs);
32551cb0ef41Sopenharmony_ci      break;
32561cb0ef41Sopenharmony_ci    case NEG_S:
32571cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(),
32581cb0ef41Sopenharmony_ci                        FPUCanonalizeOperation([](float src) { return -src; },
32591cb0ef41Sopenharmony_ci                                               KeepSign::yes, fs));
32601cb0ef41Sopenharmony_ci      break;
32611cb0ef41Sopenharmony_ci    case SQRT_S:
32621cb0ef41Sopenharmony_ci      SetFPUFloatResult(
32631cb0ef41Sopenharmony_ci          fd_reg(),
32641cb0ef41Sopenharmony_ci          FPUCanonalizeOperation([](float src) { return std::sqrt(src); }, fs));
32651cb0ef41Sopenharmony_ci      break;
32661cb0ef41Sopenharmony_ci    case RSQRT_S:
32671cb0ef41Sopenharmony_ci      SetFPUFloatResult(
32681cb0ef41Sopenharmony_ci          fd_reg(), FPUCanonalizeOperation(
32691cb0ef41Sopenharmony_ci                        [](float src) { return 1.0 / std::sqrt(src); }, fs));
32701cb0ef41Sopenharmony_ci      break;
32711cb0ef41Sopenharmony_ci    case RECIP_S:
32721cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), FPUCanonalizeOperation(
32731cb0ef41Sopenharmony_ci                                      [](float src) { return 1.0 / src; }, fs));
32741cb0ef41Sopenharmony_ci      break;
32751cb0ef41Sopenharmony_ci    case C_F_D:
32761cb0ef41Sopenharmony_ci      set_fcsr_bit(fcsr_cc, false);
32771cb0ef41Sopenharmony_ci      TraceRegWr(test_fcsr_bit(fcsr_cc));
32781cb0ef41Sopenharmony_ci      break;
32791cb0ef41Sopenharmony_ci    case C_UN_D:
32801cb0ef41Sopenharmony_ci      set_fcsr_bit(fcsr_cc, std::isnan(fs) || std::isnan(ft));
32811cb0ef41Sopenharmony_ci      TraceRegWr(test_fcsr_bit(fcsr_cc));
32821cb0ef41Sopenharmony_ci      break;
32831cb0ef41Sopenharmony_ci    case C_EQ_D:
32841cb0ef41Sopenharmony_ci      set_fcsr_bit(fcsr_cc, (fs == ft));
32851cb0ef41Sopenharmony_ci      TraceRegWr(test_fcsr_bit(fcsr_cc));
32861cb0ef41Sopenharmony_ci      break;
32871cb0ef41Sopenharmony_ci    case C_UEQ_D:
32881cb0ef41Sopenharmony_ci      set_fcsr_bit(fcsr_cc, (fs == ft) || (std::isnan(fs) || std::isnan(ft)));
32891cb0ef41Sopenharmony_ci      TraceRegWr(test_fcsr_bit(fcsr_cc));
32901cb0ef41Sopenharmony_ci      break;
32911cb0ef41Sopenharmony_ci    case C_OLT_D:
32921cb0ef41Sopenharmony_ci      set_fcsr_bit(fcsr_cc, (fs < ft));
32931cb0ef41Sopenharmony_ci      TraceRegWr(test_fcsr_bit(fcsr_cc));
32941cb0ef41Sopenharmony_ci      break;
32951cb0ef41Sopenharmony_ci    case C_ULT_D:
32961cb0ef41Sopenharmony_ci      set_fcsr_bit(fcsr_cc, (fs < ft) || (std::isnan(fs) || std::isnan(ft)));
32971cb0ef41Sopenharmony_ci      TraceRegWr(test_fcsr_bit(fcsr_cc));
32981cb0ef41Sopenharmony_ci      break;
32991cb0ef41Sopenharmony_ci    case C_OLE_D:
33001cb0ef41Sopenharmony_ci      set_fcsr_bit(fcsr_cc, (fs <= ft));
33011cb0ef41Sopenharmony_ci      TraceRegWr(test_fcsr_bit(fcsr_cc));
33021cb0ef41Sopenharmony_ci      break;
33031cb0ef41Sopenharmony_ci    case C_ULE_D:
33041cb0ef41Sopenharmony_ci      set_fcsr_bit(fcsr_cc, (fs <= ft) || (std::isnan(fs) || std::isnan(ft)));
33051cb0ef41Sopenharmony_ci      TraceRegWr(test_fcsr_bit(fcsr_cc));
33061cb0ef41Sopenharmony_ci      break;
33071cb0ef41Sopenharmony_ci    case CVT_D_S:
33081cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), static_cast<double>(fs));
33091cb0ef41Sopenharmony_ci      break;
33101cb0ef41Sopenharmony_ci    case SEL:
33111cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
33121cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), (fd_int & 0x1) == 0 ? fs : ft);
33131cb0ef41Sopenharmony_ci      break;
33141cb0ef41Sopenharmony_ci    case CLASS_S: {  // Mips32r6 instruction
33151cb0ef41Sopenharmony_ci      // Convert float input to uint32_t for easier bit manipulation
33161cb0ef41Sopenharmony_ci      float fs = get_fpu_register_float(fs_reg());
33171cb0ef41Sopenharmony_ci      uint32_t classed = bit_cast<uint32_t>(fs);
33181cb0ef41Sopenharmony_ci
33191cb0ef41Sopenharmony_ci      // Extracting sign, exponent and mantissa from the input float
33201cb0ef41Sopenharmony_ci      uint32_t sign = (classed >> 31) & 1;
33211cb0ef41Sopenharmony_ci      uint32_t exponent = (classed >> 23) & 0x000000FF;
33221cb0ef41Sopenharmony_ci      uint32_t mantissa = classed & 0x007FFFFF;
33231cb0ef41Sopenharmony_ci      uint32_t result;
33241cb0ef41Sopenharmony_ci      float fResult;
33251cb0ef41Sopenharmony_ci
33261cb0ef41Sopenharmony_ci      // Setting flags if input float is negative infinity,
33271cb0ef41Sopenharmony_ci      // positive infinity, negative zero or positive zero
33281cb0ef41Sopenharmony_ci      bool negInf = (classed == 0xFF800000);
33291cb0ef41Sopenharmony_ci      bool posInf = (classed == 0x7F800000);
33301cb0ef41Sopenharmony_ci      bool negZero = (classed == 0x80000000);
33311cb0ef41Sopenharmony_ci      bool posZero = (classed == 0x00000000);
33321cb0ef41Sopenharmony_ci
33331cb0ef41Sopenharmony_ci      bool signalingNan;
33341cb0ef41Sopenharmony_ci      bool quietNan;
33351cb0ef41Sopenharmony_ci      bool negSubnorm;
33361cb0ef41Sopenharmony_ci      bool posSubnorm;
33371cb0ef41Sopenharmony_ci      bool negNorm;
33381cb0ef41Sopenharmony_ci      bool posNorm;
33391cb0ef41Sopenharmony_ci
33401cb0ef41Sopenharmony_ci      // Setting flags if float is NaN
33411cb0ef41Sopenharmony_ci      signalingNan = false;
33421cb0ef41Sopenharmony_ci      quietNan = false;
33431cb0ef41Sopenharmony_ci      if (!negInf && !posInf && (exponent == 0xFF)) {
33441cb0ef41Sopenharmony_ci        quietNan = ((mantissa & 0x00200000) == 0) &&
33451cb0ef41Sopenharmony_ci                   ((mantissa & (0x00200000 - 1)) == 0);
33461cb0ef41Sopenharmony_ci        signalingNan = !quietNan;
33471cb0ef41Sopenharmony_ci      }
33481cb0ef41Sopenharmony_ci
33491cb0ef41Sopenharmony_ci      // Setting flags if float is subnormal number
33501cb0ef41Sopenharmony_ci      posSubnorm = false;
33511cb0ef41Sopenharmony_ci      negSubnorm = false;
33521cb0ef41Sopenharmony_ci      if ((exponent == 0) && (mantissa != 0)) {
33531cb0ef41Sopenharmony_ci        DCHECK(sign == 0 || sign == 1);
33541cb0ef41Sopenharmony_ci        posSubnorm = (sign == 0);
33551cb0ef41Sopenharmony_ci        negSubnorm = (sign == 1);
33561cb0ef41Sopenharmony_ci      }
33571cb0ef41Sopenharmony_ci
33581cb0ef41Sopenharmony_ci      // Setting flags if float is normal number
33591cb0ef41Sopenharmony_ci      posNorm = false;
33601cb0ef41Sopenharmony_ci      negNorm = false;
33611cb0ef41Sopenharmony_ci      if (!posSubnorm && !negSubnorm && !posInf && !negInf && !signalingNan &&
33621cb0ef41Sopenharmony_ci          !quietNan && !negZero && !posZero) {
33631cb0ef41Sopenharmony_ci        DCHECK(sign == 0 || sign == 1);
33641cb0ef41Sopenharmony_ci        posNorm = (sign == 0);
33651cb0ef41Sopenharmony_ci        negNorm = (sign == 1);
33661cb0ef41Sopenharmony_ci      }
33671cb0ef41Sopenharmony_ci
33681cb0ef41Sopenharmony_ci      // Calculating result according to description of CLASS.S instruction
33691cb0ef41Sopenharmony_ci      result = (posZero << 9) | (posSubnorm << 8) | (posNorm << 7) |
33701cb0ef41Sopenharmony_ci               (posInf << 6) | (negZero << 5) | (negSubnorm << 4) |
33711cb0ef41Sopenharmony_ci               (negNorm << 3) | (negInf << 2) | (quietNan << 1) | signalingNan;
33721cb0ef41Sopenharmony_ci
33731cb0ef41Sopenharmony_ci      DCHECK_NE(result, 0);
33741cb0ef41Sopenharmony_ci
33751cb0ef41Sopenharmony_ci      fResult = bit_cast<float>(result);
33761cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), fResult);
33771cb0ef41Sopenharmony_ci
33781cb0ef41Sopenharmony_ci      break;
33791cb0ef41Sopenharmony_ci    }
33801cb0ef41Sopenharmony_ci    case SELEQZ_C:
33811cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
33821cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), (ft_int & 0x1) == 0
33831cb0ef41Sopenharmony_ci                                      ? get_fpu_register_float(fs_reg())
33841cb0ef41Sopenharmony_ci                                      : 0.0);
33851cb0ef41Sopenharmony_ci      break;
33861cb0ef41Sopenharmony_ci    case SELNEZ_C:
33871cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
33881cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), (ft_int & 0x1) != 0
33891cb0ef41Sopenharmony_ci                                      ? get_fpu_register_float(fs_reg())
33901cb0ef41Sopenharmony_ci                                      : 0.0);
33911cb0ef41Sopenharmony_ci      break;
33921cb0ef41Sopenharmony_ci    case MOVZ_C: {
33931cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r2));
33941cb0ef41Sopenharmony_ci      if (rt() == 0) {
33951cb0ef41Sopenharmony_ci        SetFPUFloatResult(fd_reg(), fs);
33961cb0ef41Sopenharmony_ci      }
33971cb0ef41Sopenharmony_ci      break;
33981cb0ef41Sopenharmony_ci    }
33991cb0ef41Sopenharmony_ci    case MOVN_C: {
34001cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r2));
34011cb0ef41Sopenharmony_ci      if (rt() != 0) {
34021cb0ef41Sopenharmony_ci        SetFPUFloatResult(fd_reg(), fs);
34031cb0ef41Sopenharmony_ci      }
34041cb0ef41Sopenharmony_ci      break;
34051cb0ef41Sopenharmony_ci    }
34061cb0ef41Sopenharmony_ci    case MOVF: {
34071cb0ef41Sopenharmony_ci      // Same function field for MOVT.D and MOVF.D
34081cb0ef41Sopenharmony_ci      uint32_t ft_cc = (ft_reg() >> 2) & 0x7;
34091cb0ef41Sopenharmony_ci      ft_cc = get_fcsr_condition_bit(ft_cc);
34101cb0ef41Sopenharmony_ci
34111cb0ef41Sopenharmony_ci      if (instr_.Bit(16)) {  // Read Tf bit.
34121cb0ef41Sopenharmony_ci        // MOVT.D
34131cb0ef41Sopenharmony_ci        if (test_fcsr_bit(ft_cc)) SetFPUFloatResult(fd_reg(), fs);
34141cb0ef41Sopenharmony_ci      } else {
34151cb0ef41Sopenharmony_ci        // MOVF.D
34161cb0ef41Sopenharmony_ci        if (!test_fcsr_bit(ft_cc)) SetFPUFloatResult(fd_reg(), fs);
34171cb0ef41Sopenharmony_ci      }
34181cb0ef41Sopenharmony_ci      break;
34191cb0ef41Sopenharmony_ci    }
34201cb0ef41Sopenharmony_ci    case TRUNC_W_S: {  // Truncate single to word (round towards 0).
34211cb0ef41Sopenharmony_ci      float rounded = trunc(fs);
34221cb0ef41Sopenharmony_ci      int32_t result = static_cast<int32_t>(rounded);
34231cb0ef41Sopenharmony_ci      SetFPUWordResult(fd_reg(), result);
34241cb0ef41Sopenharmony_ci      if (set_fcsr_round_error(fs, rounded)) {
34251cb0ef41Sopenharmony_ci        set_fpu_register_word_invalid_result(fs, rounded);
34261cb0ef41Sopenharmony_ci      }
34271cb0ef41Sopenharmony_ci    } break;
34281cb0ef41Sopenharmony_ci    case TRUNC_L_S: {  // Mips32r2 instruction.
34291cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6));
34301cb0ef41Sopenharmony_ci      float rounded = trunc(fs);
34311cb0ef41Sopenharmony_ci      int64_t i64 = static_cast<int64_t>(rounded);
34321cb0ef41Sopenharmony_ci      if (IsFp64Mode()) {
34331cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), i64);
34341cb0ef41Sopenharmony_ci        if (set_fcsr_round64_error(fs, rounded)) {
34351cb0ef41Sopenharmony_ci          set_fpu_register_invalid_result64(fs, rounded);
34361cb0ef41Sopenharmony_ci        }
34371cb0ef41Sopenharmony_ci      } else {
34381cb0ef41Sopenharmony_ci        UNSUPPORTED();
34391cb0ef41Sopenharmony_ci      }
34401cb0ef41Sopenharmony_ci      break;
34411cb0ef41Sopenharmony_ci    }
34421cb0ef41Sopenharmony_ci    case FLOOR_W_S:  // Round double to word towards negative infinity.
34431cb0ef41Sopenharmony_ci    {
34441cb0ef41Sopenharmony_ci      float rounded = std::floor(fs);
34451cb0ef41Sopenharmony_ci      int32_t result = static_cast<int32_t>(rounded);
34461cb0ef41Sopenharmony_ci      SetFPUWordResult(fd_reg(), result);
34471cb0ef41Sopenharmony_ci      if (set_fcsr_round_error(fs, rounded)) {
34481cb0ef41Sopenharmony_ci        set_fpu_register_word_invalid_result(fs, rounded);
34491cb0ef41Sopenharmony_ci      }
34501cb0ef41Sopenharmony_ci    } break;
34511cb0ef41Sopenharmony_ci    case FLOOR_L_S: {  // Mips32r2 instruction.
34521cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6));
34531cb0ef41Sopenharmony_ci      float rounded = std::floor(fs);
34541cb0ef41Sopenharmony_ci      int64_t i64 = static_cast<int64_t>(rounded);
34551cb0ef41Sopenharmony_ci      if (IsFp64Mode()) {
34561cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), i64);
34571cb0ef41Sopenharmony_ci        if (set_fcsr_round64_error(fs, rounded)) {
34581cb0ef41Sopenharmony_ci          set_fpu_register_invalid_result64(fs, rounded);
34591cb0ef41Sopenharmony_ci        }
34601cb0ef41Sopenharmony_ci      } else {
34611cb0ef41Sopenharmony_ci        UNSUPPORTED();
34621cb0ef41Sopenharmony_ci      }
34631cb0ef41Sopenharmony_ci      break;
34641cb0ef41Sopenharmony_ci    }
34651cb0ef41Sopenharmony_ci    case ROUND_W_S: {
34661cb0ef41Sopenharmony_ci      float rounded = std::floor(fs + 0.5);
34671cb0ef41Sopenharmony_ci      int32_t result = static_cast<int32_t>(rounded);
34681cb0ef41Sopenharmony_ci      if ((result & 1) != 0 && result - fs == 0.5) {
34691cb0ef41Sopenharmony_ci        // If the number is halfway between two integers,
34701cb0ef41Sopenharmony_ci        // round to the even one.
34711cb0ef41Sopenharmony_ci        result--;
34721cb0ef41Sopenharmony_ci      }
34731cb0ef41Sopenharmony_ci      SetFPUWordResult(fd_reg(), result);
34741cb0ef41Sopenharmony_ci      if (set_fcsr_round_error(fs, rounded)) {
34751cb0ef41Sopenharmony_ci        set_fpu_register_word_invalid_result(fs, rounded);
34761cb0ef41Sopenharmony_ci      }
34771cb0ef41Sopenharmony_ci      break;
34781cb0ef41Sopenharmony_ci    }
34791cb0ef41Sopenharmony_ci    case ROUND_L_S: {  // Mips32r2 instruction.
34801cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6));
34811cb0ef41Sopenharmony_ci      float rounded = std::floor(fs + 0.5);
34821cb0ef41Sopenharmony_ci      int64_t result = static_cast<int64_t>(rounded);
34831cb0ef41Sopenharmony_ci      if ((result & 1) != 0 && result - fs == 0.5) {
34841cb0ef41Sopenharmony_ci        // If the number is halfway between two integers,
34851cb0ef41Sopenharmony_ci        // round to the even one.
34861cb0ef41Sopenharmony_ci        result--;
34871cb0ef41Sopenharmony_ci      }
34881cb0ef41Sopenharmony_ci      int64_t i64 = static_cast<int64_t>(result);
34891cb0ef41Sopenharmony_ci      if (IsFp64Mode()) {
34901cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), i64);
34911cb0ef41Sopenharmony_ci        if (set_fcsr_round64_error(fs, rounded)) {
34921cb0ef41Sopenharmony_ci          set_fpu_register_invalid_result64(fs, rounded);
34931cb0ef41Sopenharmony_ci        }
34941cb0ef41Sopenharmony_ci      } else {
34951cb0ef41Sopenharmony_ci        UNSUPPORTED();
34961cb0ef41Sopenharmony_ci      }
34971cb0ef41Sopenharmony_ci      break;
34981cb0ef41Sopenharmony_ci    }
34991cb0ef41Sopenharmony_ci    case CEIL_W_S:  // Round double to word towards positive infinity.
35001cb0ef41Sopenharmony_ci    {
35011cb0ef41Sopenharmony_ci      float rounded = std::ceil(fs);
35021cb0ef41Sopenharmony_ci      int32_t result = static_cast<int32_t>(rounded);
35031cb0ef41Sopenharmony_ci      SetFPUWordResult(fd_reg(), result);
35041cb0ef41Sopenharmony_ci      if (set_fcsr_round_error(fs, rounded)) {
35051cb0ef41Sopenharmony_ci        set_fpu_register_word_invalid_result(fs, rounded);
35061cb0ef41Sopenharmony_ci      }
35071cb0ef41Sopenharmony_ci    } break;
35081cb0ef41Sopenharmony_ci    case CEIL_L_S: {  // Mips32r2 instruction.
35091cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6));
35101cb0ef41Sopenharmony_ci      float rounded = std::ceil(fs);
35111cb0ef41Sopenharmony_ci      int64_t i64 = static_cast<int64_t>(rounded);
35121cb0ef41Sopenharmony_ci      if (IsFp64Mode()) {
35131cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), i64);
35141cb0ef41Sopenharmony_ci        if (set_fcsr_round64_error(fs, rounded)) {
35151cb0ef41Sopenharmony_ci          set_fpu_register_invalid_result64(fs, rounded);
35161cb0ef41Sopenharmony_ci        }
35171cb0ef41Sopenharmony_ci      } else {
35181cb0ef41Sopenharmony_ci        UNSUPPORTED();
35191cb0ef41Sopenharmony_ci      }
35201cb0ef41Sopenharmony_ci      break;
35211cb0ef41Sopenharmony_ci    }
35221cb0ef41Sopenharmony_ci    case MIN:
35231cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
35241cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), FPUMin(ft, fs));
35251cb0ef41Sopenharmony_ci      break;
35261cb0ef41Sopenharmony_ci    case MAX:
35271cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
35281cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), FPUMax(ft, fs));
35291cb0ef41Sopenharmony_ci      break;
35301cb0ef41Sopenharmony_ci    case MINA:
35311cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
35321cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), FPUMinA(ft, fs));
35331cb0ef41Sopenharmony_ci      break;
35341cb0ef41Sopenharmony_ci    case MAXA:
35351cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
35361cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), FPUMaxA(ft, fs));
35371cb0ef41Sopenharmony_ci      break;
35381cb0ef41Sopenharmony_ci    case CVT_L_S: {
35391cb0ef41Sopenharmony_ci      if (IsFp64Mode()) {
35401cb0ef41Sopenharmony_ci        int64_t result;
35411cb0ef41Sopenharmony_ci        float rounded;
35421cb0ef41Sopenharmony_ci        round64_according_to_fcsr(fs, &rounded, &result, fs);
35431cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), result);
35441cb0ef41Sopenharmony_ci        if (set_fcsr_round64_error(fs, rounded)) {
35451cb0ef41Sopenharmony_ci          set_fpu_register_invalid_result64(fs, rounded);
35461cb0ef41Sopenharmony_ci        }
35471cb0ef41Sopenharmony_ci      } else {
35481cb0ef41Sopenharmony_ci        UNSUPPORTED();
35491cb0ef41Sopenharmony_ci      }
35501cb0ef41Sopenharmony_ci      break;
35511cb0ef41Sopenharmony_ci    }
35521cb0ef41Sopenharmony_ci    case CVT_W_S: {
35531cb0ef41Sopenharmony_ci      float rounded;
35541cb0ef41Sopenharmony_ci      int32_t result;
35551cb0ef41Sopenharmony_ci      round_according_to_fcsr(fs, &rounded, &result, fs);
35561cb0ef41Sopenharmony_ci      SetFPUWordResult(fd_reg(), result);
35571cb0ef41Sopenharmony_ci      if (set_fcsr_round_error(fs, rounded)) {
35581cb0ef41Sopenharmony_ci        set_fpu_register_word_invalid_result(fs, rounded);
35591cb0ef41Sopenharmony_ci      }
35601cb0ef41Sopenharmony_ci      break;
35611cb0ef41Sopenharmony_ci    }
35621cb0ef41Sopenharmony_ci    default:
35631cb0ef41Sopenharmony_ci      // CVT_W_S CVT_L_S  ROUND_W_S ROUND_L_S FLOOR_W_S FLOOR_L_S
35641cb0ef41Sopenharmony_ci      // CEIL_W_S CEIL_L_S CVT_PS_S are unimplemented.
35651cb0ef41Sopenharmony_ci      UNREACHABLE();
35661cb0ef41Sopenharmony_ci  }
35671cb0ef41Sopenharmony_ci}
35681cb0ef41Sopenharmony_ci
35691cb0ef41Sopenharmony_civoid Simulator::DecodeTypeRegisterLRsType() {
35701cb0ef41Sopenharmony_ci  double fs = get_fpu_register_double(fs_reg());
35711cb0ef41Sopenharmony_ci  double ft = get_fpu_register_double(ft_reg());
35721cb0ef41Sopenharmony_ci  switch (instr_.FunctionFieldRaw()) {
35731cb0ef41Sopenharmony_ci    case CVT_D_L:  // Mips32r2 instruction.
35741cb0ef41Sopenharmony_ci      // Watch the signs here, we want 2 32-bit vals
35751cb0ef41Sopenharmony_ci      // to make a sign-64.
35761cb0ef41Sopenharmony_ci      int64_t i64;
35771cb0ef41Sopenharmony_ci      if (IsFp64Mode()) {
35781cb0ef41Sopenharmony_ci        i64 = get_fpu_register(fs_reg());
35791cb0ef41Sopenharmony_ci      } else {
35801cb0ef41Sopenharmony_ci        i64 = static_cast<uint32_t>(get_fpu_register_word(fs_reg()));
35811cb0ef41Sopenharmony_ci        i64 |= static_cast<int64_t>(get_fpu_register_word(fs_reg() + 1)) << 32;
35821cb0ef41Sopenharmony_ci      }
35831cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), static_cast<double>(i64));
35841cb0ef41Sopenharmony_ci      break;
35851cb0ef41Sopenharmony_ci    case CVT_S_L:
35861cb0ef41Sopenharmony_ci      if (IsFp64Mode()) {
35871cb0ef41Sopenharmony_ci        i64 = get_fpu_register(fs_reg());
35881cb0ef41Sopenharmony_ci      } else {
35891cb0ef41Sopenharmony_ci        i64 = static_cast<uint32_t>(get_fpu_register_word(fs_reg()));
35901cb0ef41Sopenharmony_ci        i64 |= static_cast<int64_t>(get_fpu_register_word(fs_reg() + 1)) << 32;
35911cb0ef41Sopenharmony_ci      }
35921cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), static_cast<float>(i64));
35931cb0ef41Sopenharmony_ci      break;
35941cb0ef41Sopenharmony_ci    case CMP_AF:  // Mips64r6 CMP.D instructions.
35951cb0ef41Sopenharmony_ci      SetFPUResult(fd_reg(), 0);
35961cb0ef41Sopenharmony_ci      break;
35971cb0ef41Sopenharmony_ci    case CMP_UN:
35981cb0ef41Sopenharmony_ci      if (std::isnan(fs) || std::isnan(ft)) {
35991cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), -1);
36001cb0ef41Sopenharmony_ci      } else {
36011cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), 0);
36021cb0ef41Sopenharmony_ci      }
36031cb0ef41Sopenharmony_ci      break;
36041cb0ef41Sopenharmony_ci    case CMP_EQ:
36051cb0ef41Sopenharmony_ci      if (fs == ft) {
36061cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), -1);
36071cb0ef41Sopenharmony_ci      } else {
36081cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), 0);
36091cb0ef41Sopenharmony_ci      }
36101cb0ef41Sopenharmony_ci      break;
36111cb0ef41Sopenharmony_ci    case CMP_UEQ:
36121cb0ef41Sopenharmony_ci      if ((fs == ft) || (std::isnan(fs) || std::isnan(ft))) {
36131cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), -1);
36141cb0ef41Sopenharmony_ci      } else {
36151cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), 0);
36161cb0ef41Sopenharmony_ci      }
36171cb0ef41Sopenharmony_ci      break;
36181cb0ef41Sopenharmony_ci    case CMP_LT:
36191cb0ef41Sopenharmony_ci      if (fs < ft) {
36201cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), -1);
36211cb0ef41Sopenharmony_ci      } else {
36221cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), 0);
36231cb0ef41Sopenharmony_ci      }
36241cb0ef41Sopenharmony_ci      break;
36251cb0ef41Sopenharmony_ci    case CMP_ULT:
36261cb0ef41Sopenharmony_ci      if ((fs < ft) || (std::isnan(fs) || std::isnan(ft))) {
36271cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), -1);
36281cb0ef41Sopenharmony_ci      } else {
36291cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), 0);
36301cb0ef41Sopenharmony_ci      }
36311cb0ef41Sopenharmony_ci      break;
36321cb0ef41Sopenharmony_ci    case CMP_LE:
36331cb0ef41Sopenharmony_ci      if (fs <= ft) {
36341cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), -1);
36351cb0ef41Sopenharmony_ci      } else {
36361cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), 0);
36371cb0ef41Sopenharmony_ci      }
36381cb0ef41Sopenharmony_ci      break;
36391cb0ef41Sopenharmony_ci    case CMP_ULE:
36401cb0ef41Sopenharmony_ci      if ((fs <= ft) || (std::isnan(fs) || std::isnan(ft))) {
36411cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), -1);
36421cb0ef41Sopenharmony_ci      } else {
36431cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), 0);
36441cb0ef41Sopenharmony_ci      }
36451cb0ef41Sopenharmony_ci      break;
36461cb0ef41Sopenharmony_ci    case CMP_OR:
36471cb0ef41Sopenharmony_ci      if (!std::isnan(fs) && !std::isnan(ft)) {
36481cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), -1);
36491cb0ef41Sopenharmony_ci      } else {
36501cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), 0);
36511cb0ef41Sopenharmony_ci      }
36521cb0ef41Sopenharmony_ci      break;
36531cb0ef41Sopenharmony_ci    case CMP_UNE:
36541cb0ef41Sopenharmony_ci      if ((fs != ft) || (std::isnan(fs) || std::isnan(ft))) {
36551cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), -1);
36561cb0ef41Sopenharmony_ci      } else {
36571cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), 0);
36581cb0ef41Sopenharmony_ci      }
36591cb0ef41Sopenharmony_ci      break;
36601cb0ef41Sopenharmony_ci    case CMP_NE:
36611cb0ef41Sopenharmony_ci      if (fs != ft && (!std::isnan(fs) && !std::isnan(ft))) {
36621cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), -1);
36631cb0ef41Sopenharmony_ci      } else {
36641cb0ef41Sopenharmony_ci        SetFPUResult(fd_reg(), 0);
36651cb0ef41Sopenharmony_ci      }
36661cb0ef41Sopenharmony_ci      break;
36671cb0ef41Sopenharmony_ci    default:
36681cb0ef41Sopenharmony_ci      UNREACHABLE();
36691cb0ef41Sopenharmony_ci  }
36701cb0ef41Sopenharmony_ci}
36711cb0ef41Sopenharmony_ci
36721cb0ef41Sopenharmony_civoid Simulator::DecodeTypeRegisterCOP1() {
36731cb0ef41Sopenharmony_ci  switch (instr_.RsFieldRaw()) {
36741cb0ef41Sopenharmony_ci    case CFC1:
36751cb0ef41Sopenharmony_ci      // At the moment only FCSR is supported.
36761cb0ef41Sopenharmony_ci      DCHECK_EQ(fs_reg(), kFCSRRegister);
36771cb0ef41Sopenharmony_ci      SetResult(rt_reg(), FCSR_);
36781cb0ef41Sopenharmony_ci      break;
36791cb0ef41Sopenharmony_ci    case MFC1:
36801cb0ef41Sopenharmony_ci      SetResult(rt_reg(), get_fpu_register_word(fs_reg()));
36811cb0ef41Sopenharmony_ci      break;
36821cb0ef41Sopenharmony_ci    case MFHC1:
36831cb0ef41Sopenharmony_ci      if (IsFp64Mode()) {
36841cb0ef41Sopenharmony_ci        SetResult(rt_reg(), get_fpu_register_hi_word(fs_reg()));
36851cb0ef41Sopenharmony_ci      } else {
36861cb0ef41Sopenharmony_ci        SetResult(rt_reg(), get_fpu_register_word(fs_reg() + 1));
36871cb0ef41Sopenharmony_ci      }
36881cb0ef41Sopenharmony_ci      break;
36891cb0ef41Sopenharmony_ci    case CTC1: {
36901cb0ef41Sopenharmony_ci      // At the moment only FCSR is supported.
36911cb0ef41Sopenharmony_ci      DCHECK_EQ(fs_reg(), kFCSRRegister);
36921cb0ef41Sopenharmony_ci      int32_t reg = registers_[rt_reg()];
36931cb0ef41Sopenharmony_ci      if (IsMipsArchVariant(kMips32r6)) {
36941cb0ef41Sopenharmony_ci        FCSR_ = reg | kFCSRNaN2008FlagMask;
36951cb0ef41Sopenharmony_ci      } else {
36961cb0ef41Sopenharmony_ci        DCHECK(IsMipsArchVariant(kMips32r1) || IsMipsArchVariant(kMips32r2));
36971cb0ef41Sopenharmony_ci        FCSR_ = reg & ~kFCSRNaN2008FlagMask;
36981cb0ef41Sopenharmony_ci      }
36991cb0ef41Sopenharmony_ci      TraceRegWr(static_cast<int32_t>(FCSR_));
37001cb0ef41Sopenharmony_ci      break;
37011cb0ef41Sopenharmony_ci    }
37021cb0ef41Sopenharmony_ci    case MTC1:
37031cb0ef41Sopenharmony_ci      // Hardware writes upper 32-bits to zero on mtc1.
37041cb0ef41Sopenharmony_ci      set_fpu_register_hi_word(fs_reg(), 0);
37051cb0ef41Sopenharmony_ci      set_fpu_register_word(fs_reg(), registers_[rt_reg()]);
37061cb0ef41Sopenharmony_ci      TraceRegWr(get_fpu_register_word(fs_reg()), FLOAT);
37071cb0ef41Sopenharmony_ci      break;
37081cb0ef41Sopenharmony_ci    case MTHC1:
37091cb0ef41Sopenharmony_ci      if (IsFp64Mode()) {
37101cb0ef41Sopenharmony_ci        set_fpu_register_hi_word(fs_reg(), registers_[rt_reg()]);
37111cb0ef41Sopenharmony_ci        TraceRegWr(get_fpu_register(fs_reg()), DOUBLE);
37121cb0ef41Sopenharmony_ci      } else {
37131cb0ef41Sopenharmony_ci        set_fpu_register_word(fs_reg() + 1, registers_[rt_reg()]);
37141cb0ef41Sopenharmony_ci        if (fs_reg() % 2) {
37151cb0ef41Sopenharmony_ci          TraceRegWr(get_fpu_register_word(fs_reg() + 1), FLOAT);
37161cb0ef41Sopenharmony_ci        } else {
37171cb0ef41Sopenharmony_ci          TraceRegWr(get_fpu_register(fs_reg()), DOUBLE);
37181cb0ef41Sopenharmony_ci        }
37191cb0ef41Sopenharmony_ci      }
37201cb0ef41Sopenharmony_ci      break;
37211cb0ef41Sopenharmony_ci    case S: {
37221cb0ef41Sopenharmony_ci      DecodeTypeRegisterSRsType();
37231cb0ef41Sopenharmony_ci      break;
37241cb0ef41Sopenharmony_ci    }
37251cb0ef41Sopenharmony_ci    case D:
37261cb0ef41Sopenharmony_ci      DecodeTypeRegisterDRsType();
37271cb0ef41Sopenharmony_ci      break;
37281cb0ef41Sopenharmony_ci    case W:
37291cb0ef41Sopenharmony_ci      DecodeTypeRegisterWRsType();
37301cb0ef41Sopenharmony_ci      break;
37311cb0ef41Sopenharmony_ci    case L:
37321cb0ef41Sopenharmony_ci      DecodeTypeRegisterLRsType();
37331cb0ef41Sopenharmony_ci      break;
37341cb0ef41Sopenharmony_ci    case PS:
37351cb0ef41Sopenharmony_ci      // Not implemented.
37361cb0ef41Sopenharmony_ci      UNREACHABLE();
37371cb0ef41Sopenharmony_ci    default:
37381cb0ef41Sopenharmony_ci      UNREACHABLE();
37391cb0ef41Sopenharmony_ci  }
37401cb0ef41Sopenharmony_ci}
37411cb0ef41Sopenharmony_ci
37421cb0ef41Sopenharmony_civoid Simulator::DecodeTypeRegisterCOP1X() {
37431cb0ef41Sopenharmony_ci  switch (instr_.FunctionFieldRaw()) {
37441cb0ef41Sopenharmony_ci    case MADD_S: {
37451cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r2));
37461cb0ef41Sopenharmony_ci      float fr, ft, fs;
37471cb0ef41Sopenharmony_ci      fr = get_fpu_register_float(fr_reg());
37481cb0ef41Sopenharmony_ci      fs = get_fpu_register_float(fs_reg());
37491cb0ef41Sopenharmony_ci      ft = get_fpu_register_float(ft_reg());
37501cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), fs * ft + fr);
37511cb0ef41Sopenharmony_ci      break;
37521cb0ef41Sopenharmony_ci    }
37531cb0ef41Sopenharmony_ci    case MSUB_S: {
37541cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r2));
37551cb0ef41Sopenharmony_ci      float fr, ft, fs;
37561cb0ef41Sopenharmony_ci      fr = get_fpu_register_float(fr_reg());
37571cb0ef41Sopenharmony_ci      fs = get_fpu_register_float(fs_reg());
37581cb0ef41Sopenharmony_ci      ft = get_fpu_register_float(ft_reg());
37591cb0ef41Sopenharmony_ci      SetFPUFloatResult(fd_reg(), fs * ft - fr);
37601cb0ef41Sopenharmony_ci      break;
37611cb0ef41Sopenharmony_ci    }
37621cb0ef41Sopenharmony_ci    case MADD_D: {
37631cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r2));
37641cb0ef41Sopenharmony_ci      double fr, ft, fs;
37651cb0ef41Sopenharmony_ci      fr = get_fpu_register_double(fr_reg());
37661cb0ef41Sopenharmony_ci      fs = get_fpu_register_double(fs_reg());
37671cb0ef41Sopenharmony_ci      ft = get_fpu_register_double(ft_reg());
37681cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), fs * ft + fr);
37691cb0ef41Sopenharmony_ci      break;
37701cb0ef41Sopenharmony_ci    }
37711cb0ef41Sopenharmony_ci    case MSUB_D: {
37721cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r2));
37731cb0ef41Sopenharmony_ci      double fr, ft, fs;
37741cb0ef41Sopenharmony_ci      fr = get_fpu_register_double(fr_reg());
37751cb0ef41Sopenharmony_ci      fs = get_fpu_register_double(fs_reg());
37761cb0ef41Sopenharmony_ci      ft = get_fpu_register_double(ft_reg());
37771cb0ef41Sopenharmony_ci      SetFPUDoubleResult(fd_reg(), fs * ft - fr);
37781cb0ef41Sopenharmony_ci      break;
37791cb0ef41Sopenharmony_ci    }
37801cb0ef41Sopenharmony_ci    default:
37811cb0ef41Sopenharmony_ci      UNREACHABLE();
37821cb0ef41Sopenharmony_ci  }
37831cb0ef41Sopenharmony_ci}
37841cb0ef41Sopenharmony_ci
37851cb0ef41Sopenharmony_civoid Simulator::DecodeTypeRegisterSPECIAL() {
37861cb0ef41Sopenharmony_ci  int64_t alu_out = 0x12345678;
37871cb0ef41Sopenharmony_ci  int64_t i64hilo = 0;
37881cb0ef41Sopenharmony_ci  uint64_t u64hilo = 0;
37891cb0ef41Sopenharmony_ci  bool do_interrupt = false;
37901cb0ef41Sopenharmony_ci
37911cb0ef41Sopenharmony_ci  switch (instr_.FunctionFieldRaw()) {
37921cb0ef41Sopenharmony_ci    case SELEQZ_S:
37931cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
37941cb0ef41Sopenharmony_ci      SetResult(rd_reg(), rt() == 0 ? rs() : 0);
37951cb0ef41Sopenharmony_ci      break;
37961cb0ef41Sopenharmony_ci    case SELNEZ_S:
37971cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
37981cb0ef41Sopenharmony_ci      SetResult(rd_reg(), rt() != 0 ? rs() : 0);
37991cb0ef41Sopenharmony_ci      break;
38001cb0ef41Sopenharmony_ci    case JR: {
38011cb0ef41Sopenharmony_ci      int32_t next_pc = rs();
38021cb0ef41Sopenharmony_ci      int32_t current_pc = get_pc();
38031cb0ef41Sopenharmony_ci      Instruction* branch_delay_instr =
38041cb0ef41Sopenharmony_ci          reinterpret_cast<Instruction*>(current_pc + kInstrSize);
38051cb0ef41Sopenharmony_ci      BranchDelayInstructionDecode(branch_delay_instr);
38061cb0ef41Sopenharmony_ci      set_pc(next_pc);
38071cb0ef41Sopenharmony_ci      pc_modified_ = true;
38081cb0ef41Sopenharmony_ci      break;
38091cb0ef41Sopenharmony_ci    }
38101cb0ef41Sopenharmony_ci    case JALR: {
38111cb0ef41Sopenharmony_ci      int32_t next_pc = rs();
38121cb0ef41Sopenharmony_ci      int32_t return_addr_reg = rd_reg();
38131cb0ef41Sopenharmony_ci      int32_t current_pc = get_pc();
38141cb0ef41Sopenharmony_ci      Instruction* branch_delay_instr =
38151cb0ef41Sopenharmony_ci          reinterpret_cast<Instruction*>(current_pc + kInstrSize);
38161cb0ef41Sopenharmony_ci      BranchDelayInstructionDecode(branch_delay_instr);
38171cb0ef41Sopenharmony_ci      set_register(return_addr_reg, current_pc + 2 * kInstrSize);
38181cb0ef41Sopenharmony_ci      set_pc(next_pc);
38191cb0ef41Sopenharmony_ci      pc_modified_ = true;
38201cb0ef41Sopenharmony_ci      break;
38211cb0ef41Sopenharmony_ci    }
38221cb0ef41Sopenharmony_ci    case SLL:
38231cb0ef41Sopenharmony_ci      alu_out = rt() << sa();
38241cb0ef41Sopenharmony_ci      SetResult(rd_reg(), static_cast<int32_t>(alu_out));
38251cb0ef41Sopenharmony_ci      break;
38261cb0ef41Sopenharmony_ci    case SRL:
38271cb0ef41Sopenharmony_ci      if (rs_reg() == 0) {
38281cb0ef41Sopenharmony_ci        // Regular logical right shift of a word by a fixed number of
38291cb0ef41Sopenharmony_ci        // bits instruction. RS field is always equal to 0.
38301cb0ef41Sopenharmony_ci        alu_out = rt_u() >> sa();
38311cb0ef41Sopenharmony_ci      } else {
38321cb0ef41Sopenharmony_ci        // Logical right-rotate of a word by a fixed number of bits. This
38331cb0ef41Sopenharmony_ci        // is special case of SRL instruction, added in MIPS32 Release 2.
38341cb0ef41Sopenharmony_ci        // RS field is equal to 00001.
38351cb0ef41Sopenharmony_ci        alu_out = base::bits::RotateRight32(rt_u(), sa());
38361cb0ef41Sopenharmony_ci      }
38371cb0ef41Sopenharmony_ci      SetResult(rd_reg(), static_cast<int32_t>(alu_out));
38381cb0ef41Sopenharmony_ci      break;
38391cb0ef41Sopenharmony_ci    case SRA:
38401cb0ef41Sopenharmony_ci      alu_out = rt() >> sa();
38411cb0ef41Sopenharmony_ci      SetResult(rd_reg(), static_cast<int32_t>(alu_out));
38421cb0ef41Sopenharmony_ci      break;
38431cb0ef41Sopenharmony_ci    case SLLV:
38441cb0ef41Sopenharmony_ci      alu_out = rt() << rs();
38451cb0ef41Sopenharmony_ci      SetResult(rd_reg(), static_cast<int32_t>(alu_out));
38461cb0ef41Sopenharmony_ci      break;
38471cb0ef41Sopenharmony_ci    case SRLV:
38481cb0ef41Sopenharmony_ci      if (sa() == 0) {
38491cb0ef41Sopenharmony_ci        // Regular logical right-shift of a word by a variable number of
38501cb0ef41Sopenharmony_ci        // bits instruction. SA field is always equal to 0.
38511cb0ef41Sopenharmony_ci        alu_out = rt_u() >> rs();
38521cb0ef41Sopenharmony_ci      } else {
38531cb0ef41Sopenharmony_ci        // Logical right-rotate of a word by a variable number of bits.
38541cb0ef41Sopenharmony_ci        // This is special case od SRLV instruction, added in MIPS32
38551cb0ef41Sopenharmony_ci        // Release 2. SA field is equal to 00001.
38561cb0ef41Sopenharmony_ci        alu_out = base::bits::RotateRight32(rt_u(), rs_u());
38571cb0ef41Sopenharmony_ci      }
38581cb0ef41Sopenharmony_ci      SetResult(rd_reg(), static_cast<int32_t>(alu_out));
38591cb0ef41Sopenharmony_ci      break;
38601cb0ef41Sopenharmony_ci    case SRAV:
38611cb0ef41Sopenharmony_ci      SetResult(rd_reg(), rt() >> rs());
38621cb0ef41Sopenharmony_ci      break;
38631cb0ef41Sopenharmony_ci    case LSA: {
38641cb0ef41Sopenharmony_ci      DCHECK(IsMipsArchVariant(kMips32r6));
38651cb0ef41Sopenharmony_ci      int8_t sa = lsa_sa() + 1;
38661cb0ef41Sopenharmony_ci      int32_t _rt = rt();
38671cb0ef41Sopenharmony_ci      int32_t _rs = rs();
38681cb0ef41Sopenharmony_ci      int32_t res = _rs << sa;
38691cb0ef41Sopenharmony_ci      res += _rt;
38701cb0ef41Sopenharmony_ci      DCHECK_EQ(res, (rs() << (lsa_sa() + 1)) + rt());
38711cb0ef41Sopenharmony_ci      USE(res);
38721cb0ef41Sopenharmony_ci      SetResult(rd_reg(), (rs() << (lsa_sa() + 1)) + rt());
38731cb0ef41Sopenharmony_ci      break;
38741cb0ef41Sopenharmony_ci    }
38751cb0ef41Sopenharmony_ci    case MFHI:  // MFHI == CLZ on R6.
38761cb0ef41Sopenharmony_ci      if (!IsMipsArchVariant(kMips32r6)) {
38771cb0ef41Sopenharmony_ci        DCHECK_EQ(sa(), 0);
38781cb0ef41Sopenharmony_ci        alu_out = get_register(HI);
38791cb0ef41Sopenharmony_ci      } else {
38801cb0ef41Sopenharmony_ci        // MIPS spec: If no bits were set in GPR rs, the result written to
38811cb0ef41Sopenharmony_ci        // GPR rd is 32.
38821cb0ef41Sopenharmony_ci        DCHECK_EQ(sa(), 1);
38831cb0ef41Sopenharmony_ci        alu_out = base::bits::CountLeadingZeros32(rs_u());
38841cb0ef41Sopenharmony_ci      }
38851cb0ef41Sopenharmony_ci      SetResult(rd_reg(), static_cast<int32_t>(alu_out));
38861cb0ef41Sopenharmony_ci      break;
38871cb0ef41Sopenharmony_ci    case MFLO:
38881cb0ef41Sopenharmony_ci      alu_out = get_register(LO);
38891cb0ef41Sopenharmony_ci      SetResult(rd_reg(), static_cast<int32_t>(alu_out));
38901cb0ef41Sopenharmony_ci      break;
38911cb0ef41Sopenharmony_ci    // Instructions using HI and LO registers.
38921cb0ef41Sopenharmony_ci    case MULT:
38931cb0ef41Sopenharmony_ci      i64hilo = static_cast<int64_t>(rs()) * static_cast<int64_t>(rt());
38941cb0ef41Sopenharmony_ci      if (!IsMipsArchVariant(kMips32r6)) {
38951cb0ef41Sopenharmony_ci        set_register(LO, static_cast<int32_t>(i64hilo & 0xFFFFFFFF));
38961cb0ef41Sopenharmony_ci        set_register(HI, static_cast<int32_t>(i64hilo >> 32));
38971cb0ef41Sopenharmony_ci      } else {
38981cb0ef41Sopenharmony_ci        switch (sa()) {
38991cb0ef41Sopenharmony_ci          case MUL_OP:
39001cb0ef41Sopenharmony_ci            SetResult(rd_reg(), static_cast<int32_t>(i64hilo & 0xFFFFFFFF));
39011cb0ef41Sopenharmony_ci            break;
39021cb0ef41Sopenharmony_ci          case MUH_OP:
39031cb0ef41Sopenharmony_ci            SetResult(rd_reg(), static_cast<int32_t>(i64hilo >> 32));
39041cb0ef41Sopenharmony_ci            break;
39051cb0ef41Sopenharmony_ci          default:
39061cb0ef41Sopenharmony_ci            UNIMPLEMENTED_MIPS();
39071cb0ef41Sopenharmony_ci            break;
39081cb0ef41Sopenharmony_ci        }
39091cb0ef41Sopenharmony_ci      }
39101cb0ef41Sopenharmony_ci      break;
39111cb0ef41Sopenharmony_ci    case MULTU:
39121cb0ef41Sopenharmony_ci      u64hilo = static_cast<uint64_t>(rs_u()) * static_cast<uint64_t>(rt_u());
39131cb0ef41Sopenharmony_ci      if (!IsMipsArchVariant(kMips32r6)) {
39141cb0ef41Sopenharmony_ci        set_register(LO, static_cast<int32_t>(u64hilo & 0xFFFFFFFF));
39151cb0ef41Sopenharmony_ci        set_register(HI, static_cast<int32_t>(u64hilo >> 32));
39161cb0ef41Sopenharmony_ci      } else {
39171cb0ef41Sopenharmony_ci        switch (sa()) {
39181cb0ef41Sopenharmony_ci          case MUL_OP:
39191cb0ef41Sopenharmony_ci            SetResult(rd_reg(), static_cast<int32_t>(u64hilo & 0xFFFFFFFF));
39201cb0ef41Sopenharmony_ci            break;
39211cb0ef41Sopenharmony_ci          case MUH_OP:
39221cb0ef41Sopenharmony_ci            SetResult(rd_reg(), static_cast<int32_t>(u64hilo >> 32));
39231cb0ef41Sopenharmony_ci            break;
39241cb0ef41Sopenharmony_ci          default:
39251cb0ef41Sopenharmony_ci            UNIMPLEMENTED_MIPS();
39261cb0ef41Sopenharmony_ci            break;
39271cb0ef41Sopenharmony_ci        }
39281cb0ef41Sopenharmony_ci      }
39291cb0ef41Sopenharmony_ci      break;
39301cb0ef41Sopenharmony_ci    case DIV:
39311cb0ef41Sopenharmony_ci      if (IsMipsArchVariant(kMips32r6)) {
39321cb0ef41Sopenharmony_ci        switch (sa()) {
39331cb0ef41Sopenharmony_ci          case DIV_OP:
39341cb0ef41Sopenharmony_ci            if (rs() == INT_MIN && rt() == -1) {
39351cb0ef41Sopenharmony_ci              SetResult(rd_reg(), INT_MIN);
39361cb0ef41Sopenharmony_ci            } else if (rt() != 0) {
39371cb0ef41Sopenharmony_ci              SetResult(rd_reg(), rs() / rt());
39381cb0ef41Sopenharmony_ci            }
39391cb0ef41Sopenharmony_ci            break;
39401cb0ef41Sopenharmony_ci          case MOD_OP:
39411cb0ef41Sopenharmony_ci            if (rs() == INT_MIN && rt() == -1) {
39421cb0ef41Sopenharmony_ci              SetResult(rd_reg(), 0);
39431cb0ef41Sopenharmony_ci            } else if (rt() != 0) {
39441cb0ef41Sopenharmony_ci              SetResult(rd_reg(), rs() % rt());
39451cb0ef41Sopenharmony_ci            }
39461cb0ef41Sopenharmony_ci            break;
39471cb0ef41Sopenharmony_ci          default:
39481cb0ef41Sopenharmony_ci            UNIMPLEMENTED_MIPS();
39491cb0ef41Sopenharmony_ci            break;
39501cb0ef41Sopenharmony_ci        }
39511cb0ef41Sopenharmony_ci      } else {
39521cb0ef41Sopenharmony_ci        // Divide by zero and overflow was not checked in the
39531cb0ef41Sopenharmony_ci        // configuration step - div and divu do not raise exceptions. On
39541cb0ef41Sopenharmony_ci        // division by 0 the result will be UNPREDICTABLE. On overflow
39551cb0ef41Sopenharmony_ci        // (INT_MIN/-1), return INT_MIN which is what the hardware does.
39561cb0ef41Sopenharmony_ci        if (rs() == INT_MIN && rt() == -1) {
39571cb0ef41Sopenharmony_ci          set_register(LO, INT_MIN);
39581cb0ef41Sopenharmony_ci          set_register(HI, 0);
39591cb0ef41Sopenharmony_ci        } else if (rt() != 0) {
39601cb0ef41Sopenharmony_ci          set_register(LO, rs() / rt());
39611cb0ef41Sopenharmony_ci          set_register(HI, rs() % rt());
39621cb0ef41Sopenharmony_ci        }
39631cb0ef41Sopenharmony_ci      }
39641cb0ef41Sopenharmony_ci      break;
39651cb0ef41Sopenharmony_ci    case DIVU:
39661cb0ef41Sopenharmony_ci      if (IsMipsArchVariant(kMips32r6)) {
39671cb0ef41Sopenharmony_ci        switch (sa()) {
39681cb0ef41Sopenharmony_ci          case DIV_OP:
39691cb0ef41Sopenharmony_ci            if (rt_u() != 0) {
39701cb0ef41Sopenharmony_ci              SetResult(rd_reg(), rs_u() / rt_u());
39711cb0ef41Sopenharmony_ci            }
39721cb0ef41Sopenharmony_ci            break;
39731cb0ef41Sopenharmony_ci          case MOD_OP:
39741cb0ef41Sopenharmony_ci            if (rt_u() != 0) {
39751cb0ef41Sopenharmony_ci              SetResult(rd_reg(), rs_u() % rt_u());
39761cb0ef41Sopenharmony_ci            }
39771cb0ef41Sopenharmony_ci            break;
39781cb0ef41Sopenharmony_ci          default:
39791cb0ef41Sopenharmony_ci            UNIMPLEMENTED_MIPS();
39801cb0ef41Sopenharmony_ci            break;
39811cb0ef41Sopenharmony_ci        }
39821cb0ef41Sopenharmony_ci      } else {
39831cb0ef41Sopenharmony_ci        if (rt_u() != 0) {
39841cb0ef41Sopenharmony_ci          set_register(LO, rs_u() / rt_u());
39851cb0ef41Sopenharmony_ci          set_register(HI, rs_u() % rt_u());
39861cb0ef41Sopenharmony_ci        }
39871cb0ef41Sopenharmony_ci      }
39881cb0ef41Sopenharmony_ci      break;
39891cb0ef41Sopenharmony_ci    case ADD:
39901cb0ef41Sopenharmony_ci      if (HaveSameSign(rs(), rt())) {
39911cb0ef41Sopenharmony_ci        if (rs() > 0) {
39921cb0ef41Sopenharmony_ci          if (rs() <= (Registers::kMaxValue - rt())) {
39931cb0ef41Sopenharmony_ci            SignalException(kIntegerOverflow);
39941cb0ef41Sopenharmony_ci          }
39951cb0ef41Sopenharmony_ci        } else if (rs() < 0) {
39961cb0ef41Sopenharmony_ci          if (rs() >= (Registers::kMinValue - rt())) {
39971cb0ef41Sopenharmony_ci            SignalException(kIntegerUnderflow);
39981cb0ef41Sopenharmony_ci          }
39991cb0ef41Sopenharmony_ci        }
40001cb0ef41Sopenharmony_ci      }
40011cb0ef41Sopenharmony_ci      SetResult(rd_reg(), rs() + rt());
40021cb0ef41Sopenharmony_ci      break;
40031cb0ef41Sopenharmony_ci    case ADDU:
40041cb0ef41Sopenharmony_ci      SetResult(rd_reg(), rs() + rt());
40051cb0ef41Sopenharmony_ci      break;
40061cb0ef41Sopenharmony_ci    case SUB:
40071cb0ef41Sopenharmony_ci      if (!HaveSameSign(rs(), rt())) {
40081cb0ef41Sopenharmony_ci        if (rs() > 0) {
40091cb0ef41Sopenharmony_ci          if (rs() <= (Registers::kMaxValue + rt())) {
40101cb0ef41Sopenharmony_ci            SignalException(kIntegerOverflow);
40111cb0ef41Sopenharmony_ci          }
40121cb0ef41Sopenharmony_ci        } else if (rs() < 0) {
40131cb0ef41Sopenharmony_ci          if (rs() >= (Registers::kMinValue + rt())) {
40141cb0ef41Sopenharmony_ci            SignalException(kIntegerUnderflow);
40151cb0ef41Sopenharmony_ci          }
40161cb0ef41Sopenharmony_ci        }
40171cb0ef41Sopenharmony_ci      }
40181cb0ef41Sopenharmony_ci      SetResult(rd_reg(), rs() - rt());
40191cb0ef41Sopenharmony_ci      break;
40201cb0ef41Sopenharmony_ci    case SUBU:
40211cb0ef41Sopenharmony_ci      SetResult(rd_reg(), rs() - rt());
40221cb0ef41Sopenharmony_ci      break;
40231cb0ef41Sopenharmony_ci    case AND:
40241cb0ef41Sopenharmony_ci      SetResult(rd_reg(), rs() & rt());
40251cb0ef41Sopenharmony_ci      break;
40261cb0ef41Sopenharmony_ci    case OR:
40271cb0ef41Sopenharmony_ci      SetResult(rd_reg(), rs() | rt());
40281cb0ef41Sopenharmony_ci      break;
40291cb0ef41Sopenharmony_ci    case XOR:
40301cb0ef41Sopenharmony_ci      SetResult(rd_reg(), rs() ^ rt());
40311cb0ef41Sopenharmony_ci      break;
40321cb0ef41Sopenharmony_ci    case NOR:
40331cb0ef41Sopenharmony_ci      SetResult(rd_reg(), ~(rs() | rt()));
40341cb0ef41Sopenharmony_ci      break;
40351cb0ef41Sopenharmony_ci    case SLT:
40361cb0ef41Sopenharmony_ci      SetResult(rd_reg(), rs() < rt() ? 1 : 0);
40371cb0ef41Sopenharmony_ci      break;
40381cb0ef41Sopenharmony_ci    case SLTU:
40391cb0ef41Sopenharmony_ci      SetResult(rd_reg(), rs_u() < rt_u() ? 1 : 0);
40401cb0ef41Sopenharmony_ci      break;
40411cb0ef41Sopenharmony_ci    // Break and trap instructions.
40421cb0ef41Sopenharmony_ci    case BREAK:
40431cb0ef41Sopenharmony_ci      do_interrupt = true;
40441cb0ef41Sopenharmony_ci      break;
40451cb0ef41Sopenharmony_ci    case TGE:
40461cb0ef41Sopenharmony_ci      do_interrupt = rs() >= rt();
40471cb0ef41Sopenharmony_ci      break;
40481cb0ef41Sopenharmony_ci    case TGEU:
40491cb0ef41Sopenharmony_ci      do_interrupt = rs_u() >= rt_u();
40501cb0ef41Sopenharmony_ci      break;
40511cb0ef41Sopenharmony_ci    case TLT:
40521cb0ef41Sopenharmony_ci      do_interrupt = rs() < rt();
40531cb0ef41Sopenharmony_ci      break;
40541cb0ef41Sopenharmony_ci    case TLTU:
40551cb0ef41Sopenharmony_ci      do_interrupt = rs_u() < rt_u();
40561cb0ef41Sopenharmony_ci      break;
40571cb0ef41Sopenharmony_ci    case TEQ:
40581cb0ef41Sopenharmony_ci      do_interrupt = rs() == rt();
40591cb0ef41Sopenharmony_ci      break;
40601cb0ef41Sopenharmony_ci    case TNE:
40611cb0ef41Sopenharmony_ci      do_interrupt = rs() != rt();
40621cb0ef41Sopenharmony_ci      break;
40631cb0ef41Sopenharmony_ci    case SYNC:
40641cb0ef41Sopenharmony_ci      // TODO(palfia): Ignore sync instruction for now.
40651cb0ef41Sopenharmony_ci      break;
40661cb0ef41Sopenharmony_ci    // Conditional moves.
40671cb0ef41Sopenharmony_ci    case MOVN:
40681cb0ef41Sopenharmony_ci      if (rt()) {
40691cb0ef41Sopenharmony_ci        SetResult(rd_reg(), rs());
40701cb0ef41Sopenharmony_ci      }
40711cb0ef41Sopenharmony_ci      break;
40721cb0ef41Sopenharmony_ci    case MOVCI: {
40731cb0ef41Sopenharmony_ci      uint32_t cc = instr_.FBccValue();
40741cb0ef41Sopenharmony_ci      uint32_t fcsr_cc = get_fcsr_condition_bit(cc);
40751cb0ef41Sopenharmony_ci      if (instr_.Bit(16)) {  // Read Tf bit.
40761cb0ef41Sopenharmony_ci        if (test_fcsr_bit(fcsr_cc)) set_register(rd_reg(), rs());
40771cb0ef41Sopenharmony_ci      } else {
40781cb0ef41Sopenharmony_ci        if (!test_fcsr_bit(fcsr_cc)) set_register(rd_reg(), rs());
40791cb0ef41Sopenharmony_ci      }
40801cb0ef41Sopenharmony_ci      break;
40811cb0ef41Sopenharmony_ci    }
40821cb0ef41Sopenharmony_ci    case MOVZ:
40831cb0ef41Sopenharmony_ci      if (!rt()) {
40841cb0ef41Sopenharmony_ci        SetResult(rd_reg(), rs());
40851cb0ef41Sopenharmony_ci      }
40861cb0ef41Sopenharmony_ci      break;
40871cb0ef41Sopenharmony_ci    default:
40881cb0ef41Sopenharmony_ci      UNREACHABLE();
40891cb0ef41Sopenharmony_ci  }
40901cb0ef41Sopenharmony_ci  if (do_interrupt) {
40911cb0ef41Sopenharmony_ci    SoftwareInterrupt();
40921cb0ef41Sopenharmony_ci  }
40931cb0ef41Sopenharmony_ci}
40941cb0ef41Sopenharmony_ci
40951cb0ef41Sopenharmony_civoid Simulator::DecodeTypeRegisterSPECIAL2() {
40961cb0ef41Sopenharmony_ci  int32_t alu_out;
40971cb0ef41Sopenharmony_ci  switch (instr_.FunctionFieldRaw()) {
40981cb0ef41Sopenharmony_ci    case MUL:
40991cb0ef41Sopenharmony_ci      // Only the lower 32 bits are kept.
41001cb0ef41Sopenharmony_ci      alu_out = rs_u() * rt_u();
41011cb0ef41Sopenharmony_ci      // HI and LO are UNPREDICTABLE after the operation.
41021cb0ef41Sopenharmony_ci      set_register(LO, Unpredictable);
41031cb0ef41Sopenharmony_ci      set_register(HI, Unpredictable);
41041cb0ef41Sopenharmony_ci      break;
41051cb0ef41Sopenharmony_ci    case CLZ:
41061cb0ef41Sopenharmony_ci      // MIPS32 spec: If no bits were set in GPR rs, the result written to
41071cb0ef41Sopenharmony_ci      // GPR rd is 32.
41081cb0ef41Sopenharmony_ci      alu_out = base::bits::CountLeadingZeros32(rs_u());
41091cb0ef41Sopenharmony_ci      break;
41101cb0ef41Sopenharmony_ci    default:
41111cb0ef41Sopenharmony_ci      alu_out = 0x12345678;
41121cb0ef41Sopenharmony_ci      UNREACHABLE();
41131cb0ef41Sopenharmony_ci  }
41141cb0ef41Sopenharmony_ci  SetResult(rd_reg(), alu_out);
41151cb0ef41Sopenharmony_ci}
41161cb0ef41Sopenharmony_ci
41171cb0ef41Sopenharmony_civoid Simulator::DecodeTypeRegisterSPECIAL3() {
41181cb0ef41Sopenharmony_ci  int32_t alu_out;
41191cb0ef41Sopenharmony_ci  switch (instr_.FunctionFieldRaw()) {
41201cb0ef41Sopenharmony_ci    case INS: {  // Mips32r2 instruction.
41211cb0ef41Sopenharmony_ci      // Interpret rd field as 5-bit msb of insert.
41221cb0ef41Sopenharmony_ci      uint16_t msb = rd_reg();
41231cb0ef41Sopenharmony_ci      // Interpret sa field as 5-bit lsb of insert.
41241cb0ef41Sopenharmony_ci      uint16_t lsb = sa();
41251cb0ef41Sopenharmony_ci      uint16_t size = msb - lsb + 1;
41261cb0ef41Sopenharmony_ci      uint32_t mask;
41271cb0ef41Sopenharmony_ci      if (size < 32) {
41281cb0ef41Sopenharmony_ci        mask = (1 << size) - 1;
41291cb0ef41Sopenharmony_ci      } else {
41301cb0ef41Sopenharmony_ci        mask = std::numeric_limits<uint32_t>::max();
41311cb0ef41Sopenharmony_ci      }
41321cb0ef41Sopenharmony_ci      alu_out = (rt_u() & ~(mask << lsb)) | ((rs_u() & mask) << lsb);
41331cb0ef41Sopenharmony_ci      // Ins instr leaves result in Rt, rather than Rd.
41341cb0ef41Sopenharmony_ci      SetResult(rt_reg(), alu_out);
41351cb0ef41Sopenharmony_ci      break;
41361cb0ef41Sopenharmony_ci    }
41371cb0ef41Sopenharmony_ci    case EXT: {  // Mips32r2 instruction.
41381cb0ef41Sopenharmony_ci      // Interpret rd field as 5-bit msb of extract.
41391cb0ef41Sopenharmony_ci      uint16_t msb = rd_reg();
41401cb0ef41Sopenharmony_ci      // Interpret sa field as 5-bit lsb of extract.
41411cb0ef41Sopenharmony_ci      uint16_t lsb = sa();
41421cb0ef41Sopenharmony_ci      uint16_t size = msb + 1;
41431cb0ef41Sopenharmony_ci      uint32_t mask;
41441cb0ef41Sopenharmony_ci      if (size < 32) {
41451cb0ef41Sopenharmony_ci        mask = (1 << size) - 1;
41461cb0ef41Sopenharmony_ci      } else {
41471cb0ef41Sopenharmony_ci        mask = std::numeric_limits<uint32_t>::max();
41481cb0ef41Sopenharmony_ci      }
41491cb0ef41Sopenharmony_ci      alu_out = (rs_u() & (mask << lsb)) >> lsb;
41501cb0ef41Sopenharmony_ci      SetResult(rt_reg(), alu_out);
41511cb0ef41Sopenharmony_ci      break;
41521cb0ef41Sopenharmony_ci    }
41531cb0ef41Sopenharmony_ci    case BSHFL: {
41541cb0ef41Sopenharmony_ci      int sa = instr_.SaFieldRaw() >> kSaShift;
41551cb0ef41Sopenharmony_ci      switch (sa) {
41561cb0ef41Sopenharmony_ci        case BITSWAP: {
41571cb0ef41Sopenharmony_ci          uint32_t input = static_cast<uint32_t>(rt());
41581cb0ef41Sopenharmony_ci          uint32_t output = 0;
41591cb0ef41Sopenharmony_ci          uint8_t i_byte, o_byte;
41601cb0ef41Sopenharmony_ci
41611cb0ef41Sopenharmony_ci          // Reverse the bit in byte for each individual byte
41621cb0ef41Sopenharmony_ci          for (int i = 0; i < 4; i++) {
41631cb0ef41Sopenharmony_ci            output = output >> 8;
41641cb0ef41Sopenharmony_ci            i_byte = input & 0xFF;
41651cb0ef41Sopenharmony_ci
41661cb0ef41Sopenharmony_ci            // Fast way to reverse bits in byte
41671cb0ef41Sopenharmony_ci            // Devised by Sean Anderson, July 13, 2001
41681cb0ef41Sopenharmony_ci            o_byte = static_cast<uint8_t>(((i_byte * 0x0802LU & 0x22110LU) |
41691cb0ef41Sopenharmony_ci                                           (i_byte * 0x8020LU & 0x88440LU)) *
41701cb0ef41Sopenharmony_ci                                              0x10101LU >>
41711cb0ef41Sopenharmony_ci                                          16);
41721cb0ef41Sopenharmony_ci
41731cb0ef41Sopenharmony_ci            output = output | (static_cast<uint32_t>(o_byte << 24));
41741cb0ef41Sopenharmony_ci            input = input >> 8;
41751cb0ef41Sopenharmony_ci          }
41761cb0ef41Sopenharmony_ci
41771cb0ef41Sopenharmony_ci          alu_out = static_cast<int32_t>(output);
41781cb0ef41Sopenharmony_ci          break;
41791cb0ef41Sopenharmony_ci        }
41801cb0ef41Sopenharmony_ci        case SEB: {
41811cb0ef41Sopenharmony_ci          uint8_t input = static_cast<uint8_t>(rt());
41821cb0ef41Sopenharmony_ci          uint32_t output = input;
41831cb0ef41Sopenharmony_ci          uint32_t mask = 0x00000080;
41841cb0ef41Sopenharmony_ci
41851cb0ef41Sopenharmony_ci          // Extending sign
41861cb0ef41Sopenharmony_ci          if (mask & input) {
41871cb0ef41Sopenharmony_ci            output |= 0xFFFFFF00;
41881cb0ef41Sopenharmony_ci          }
41891cb0ef41Sopenharmony_ci
41901cb0ef41Sopenharmony_ci          alu_out = static_cast<int32_t>(output);
41911cb0ef41Sopenharmony_ci          break;
41921cb0ef41Sopenharmony_ci        }
41931cb0ef41Sopenharmony_ci        case SEH: {
41941cb0ef41Sopenharmony_ci          uint16_t input = static_cast<uint16_t>(rt());
41951cb0ef41Sopenharmony_ci          uint32_t output = input;
41961cb0ef41Sopenharmony_ci          uint32_t mask = 0x00008000;
41971cb0ef41Sopenharmony_ci
41981cb0ef41Sopenharmony_ci          // Extending sign
41991cb0ef41Sopenharmony_ci          if (mask & input) {
42001cb0ef41Sopenharmony_ci            output |= 0xFFFF0000;
42011cb0ef41Sopenharmony_ci          }
42021cb0ef41Sopenharmony_ci
42031cb0ef41Sopenharmony_ci          alu_out = static_cast<int32_t>(output);
42041cb0ef41Sopenharmony_ci          break;
42051cb0ef41Sopenharmony_ci        }
42061cb0ef41Sopenharmony_ci        case WSBH: {
42071cb0ef41Sopenharmony_ci          uint32_t input = static_cast<uint32_t>(rt());
42081cb0ef41Sopenharmony_ci          uint32_t output = 0;
42091cb0ef41Sopenharmony_ci
42101cb0ef41Sopenharmony_ci          uint32_t mask = 0xFF000000;
42111cb0ef41Sopenharmony_ci          for (int i = 0; i < 4; i++) {
42121cb0ef41Sopenharmony_ci            uint32_t tmp = mask & input;
42131cb0ef41Sopenharmony_ci            if (i % 2 == 0) {
42141cb0ef41Sopenharmony_ci              tmp = tmp >> 8;
42151cb0ef41Sopenharmony_ci            } else {
42161cb0ef41Sopenharmony_ci              tmp = tmp << 8;
42171cb0ef41Sopenharmony_ci            }
42181cb0ef41Sopenharmony_ci            output = output | tmp;
42191cb0ef41Sopenharmony_ci            mask = mask >> 8;
42201cb0ef41Sopenharmony_ci          }
42211cb0ef41Sopenharmony_ci
42221cb0ef41Sopenharmony_ci          alu_out = static_cast<int32_t>(output);
42231cb0ef41Sopenharmony_ci          break;
42241cb0ef41Sopenharmony_ci        }
42251cb0ef41Sopenharmony_ci        default: {
42261cb0ef41Sopenharmony_ci          const uint8_t bp = instr_.Bp2Value();
42271cb0ef41Sopenharmony_ci          sa >>= kBp2Bits;
42281cb0ef41Sopenharmony_ci          switch (sa) {
42291cb0ef41Sopenharmony_ci            case ALIGN: {
42301cb0ef41Sopenharmony_ci              if (bp == 0) {
42311cb0ef41Sopenharmony_ci                alu_out = static_cast<int32_t>(rt());
42321cb0ef41Sopenharmony_ci              } else {
42331cb0ef41Sopenharmony_ci                uint32_t rt_hi = rt() << (8 * bp);
42341cb0ef41Sopenharmony_ci                uint32_t rs_lo = rs() >> (8 * (4 - bp));
42351cb0ef41Sopenharmony_ci                alu_out = static_cast<int32_t>(rt_hi | rs_lo);
42361cb0ef41Sopenharmony_ci              }
42371cb0ef41Sopenharmony_ci              break;
42381cb0ef41Sopenharmony_ci            }
42391cb0ef41Sopenharmony_ci            default:
42401cb0ef41Sopenharmony_ci              alu_out = 0x12345678;
42411cb0ef41Sopenharmony_ci              UNREACHABLE();
42421cb0ef41Sopenharmony_ci          }
42431cb0ef41Sopenharmony_ci        }
42441cb0ef41Sopenharmony_ci      }
42451cb0ef41Sopenharmony_ci      SetResult(rd_reg(), alu_out);
42461cb0ef41Sopenharmony_ci      break;
42471cb0ef41Sopenharmony_ci    }
42481cb0ef41Sopenharmony_ci    default:
42491cb0ef41Sopenharmony_ci      UNREACHABLE();
42501cb0ef41Sopenharmony_ci  }
42511cb0ef41Sopenharmony_ci}
42521cb0ef41Sopenharmony_ci
42531cb0ef41Sopenharmony_ciint Simulator::DecodeMsaDataFormat() {
42541cb0ef41Sopenharmony_ci  int df = -1;
42551cb0ef41Sopenharmony_ci  if (instr_.IsMSABranchInstr()) {
42561cb0ef41Sopenharmony_ci    switch (instr_.RsFieldRaw()) {
42571cb0ef41Sopenharmony_ci      case BZ_V:
42581cb0ef41Sopenharmony_ci      case BNZ_V:
42591cb0ef41Sopenharmony_ci        df = MSA_VECT;
42601cb0ef41Sopenharmony_ci        break;
42611cb0ef41Sopenharmony_ci      case BZ_B:
42621cb0ef41Sopenharmony_ci      case BNZ_B:
42631cb0ef41Sopenharmony_ci        df = MSA_BYTE;
42641cb0ef41Sopenharmony_ci        break;
42651cb0ef41Sopenharmony_ci      case BZ_H:
42661cb0ef41Sopenharmony_ci      case BNZ_H:
42671cb0ef41Sopenharmony_ci        df = MSA_HALF;
42681cb0ef41Sopenharmony_ci        break;
42691cb0ef41Sopenharmony_ci      case BZ_W:
42701cb0ef41Sopenharmony_ci      case BNZ_W:
42711cb0ef41Sopenharmony_ci        df = MSA_WORD;
42721cb0ef41Sopenharmony_ci        break;
42731cb0ef41Sopenharmony_ci      case BZ_D:
42741cb0ef41Sopenharmony_ci      case BNZ_D:
42751cb0ef41Sopenharmony_ci        df = MSA_DWORD;
42761cb0ef41Sopenharmony_ci        break;
42771cb0ef41Sopenharmony_ci      default:
42781cb0ef41Sopenharmony_ci        UNREACHABLE();
42791cb0ef41Sopenharmony_ci    }
42801cb0ef41Sopenharmony_ci  } else {
42811cb0ef41Sopenharmony_ci    int DF[] = {MSA_BYTE, MSA_HALF, MSA_WORD, MSA_DWORD};
42821cb0ef41Sopenharmony_ci    switch (instr_.MSAMinorOpcodeField()) {
42831cb0ef41Sopenharmony_ci      case kMsaMinorI5:
42841cb0ef41Sopenharmony_ci      case kMsaMinorI10:
42851cb0ef41Sopenharmony_ci      case kMsaMinor3R:
42861cb0ef41Sopenharmony_ci        df = DF[instr_.Bits(22, 21)];
42871cb0ef41Sopenharmony_ci        break;
42881cb0ef41Sopenharmony_ci      case kMsaMinorMI10:
42891cb0ef41Sopenharmony_ci        df = DF[instr_.Bits(1, 0)];
42901cb0ef41Sopenharmony_ci        break;
42911cb0ef41Sopenharmony_ci      case kMsaMinorBIT:
42921cb0ef41Sopenharmony_ci        df = DF[instr_.MsaBitDf()];
42931cb0ef41Sopenharmony_ci        break;
42941cb0ef41Sopenharmony_ci      case kMsaMinorELM:
42951cb0ef41Sopenharmony_ci        df = DF[instr_.MsaElmDf()];
42961cb0ef41Sopenharmony_ci        break;
42971cb0ef41Sopenharmony_ci      case kMsaMinor3RF: {
42981cb0ef41Sopenharmony_ci        uint32_t opcode = instr_.InstructionBits() & kMsa3RFMask;
42991cb0ef41Sopenharmony_ci        switch (opcode) {
43001cb0ef41Sopenharmony_ci          case FEXDO:
43011cb0ef41Sopenharmony_ci          case FTQ:
43021cb0ef41Sopenharmony_ci          case MUL_Q:
43031cb0ef41Sopenharmony_ci          case MADD_Q:
43041cb0ef41Sopenharmony_ci          case MSUB_Q:
43051cb0ef41Sopenharmony_ci          case MULR_Q:
43061cb0ef41Sopenharmony_ci          case MADDR_Q:
43071cb0ef41Sopenharmony_ci          case MSUBR_Q:
43081cb0ef41Sopenharmony_ci            df = DF[1 + instr_.Bit(21)];
43091cb0ef41Sopenharmony_ci            break;
43101cb0ef41Sopenharmony_ci          default:
43111cb0ef41Sopenharmony_ci            df = DF[2 + instr_.Bit(21)];
43121cb0ef41Sopenharmony_ci            break;
43131cb0ef41Sopenharmony_ci        }
43141cb0ef41Sopenharmony_ci      } break;
43151cb0ef41Sopenharmony_ci      case kMsaMinor2R:
43161cb0ef41Sopenharmony_ci        df = DF[instr_.Bits(17, 16)];
43171cb0ef41Sopenharmony_ci        break;
43181cb0ef41Sopenharmony_ci      case kMsaMinor2RF:
43191cb0ef41Sopenharmony_ci        df = DF[2 + instr_.Bit(16)];
43201cb0ef41Sopenharmony_ci        break;
43211cb0ef41Sopenharmony_ci      default:
43221cb0ef41Sopenharmony_ci        UNREACHABLE();
43231cb0ef41Sopenharmony_ci    }
43241cb0ef41Sopenharmony_ci  }
43251cb0ef41Sopenharmony_ci  return df;
43261cb0ef41Sopenharmony_ci}
43271cb0ef41Sopenharmony_ci
43281cb0ef41Sopenharmony_civoid Simulator::DecodeTypeMsaI8() {
43291cb0ef41Sopenharmony_ci  DCHECK(IsMipsArchVariant(kMips32r6));
43301cb0ef41Sopenharmony_ci  DCHECK(CpuFeatures::IsSupported(MIPS_SIMD));
43311cb0ef41Sopenharmony_ci  uint32_t opcode = instr_.InstructionBits() & kMsaI8Mask;
43321cb0ef41Sopenharmony_ci  int8_t i8 = instr_.MsaImm8Value();
43331cb0ef41Sopenharmony_ci  msa_reg_t ws, wd;
43341cb0ef41Sopenharmony_ci
43351cb0ef41Sopenharmony_ci  switch (opcode) {
43361cb0ef41Sopenharmony_ci    case ANDI_B:
43371cb0ef41Sopenharmony_ci      get_msa_register(instr_.WsValue(), ws.b);
43381cb0ef41Sopenharmony_ci      for (int i = 0; i < kMSALanesByte; i++) {
43391cb0ef41Sopenharmony_ci        wd.b[i] = ws.b[i] & i8;
43401cb0ef41Sopenharmony_ci      }
43411cb0ef41Sopenharmony_ci      set_msa_register(instr_.WdValue(), wd.b);
43421cb0ef41Sopenharmony_ci      TraceMSARegWr(wd.b);
43431cb0ef41Sopenharmony_ci      break;
43441cb0ef41Sopenharmony_ci    case ORI_B:
43451cb0ef41Sopenharmony_ci      get_msa_register(instr_.WsValue(), ws.b);
43461cb0ef41Sopenharmony_ci      for (int i = 0; i < kMSALanesByte; i++) {
43471cb0ef41Sopenharmony_ci        wd.b[i] = ws.b[i] | i8;
43481cb0ef41Sopenharmony_ci      }
43491cb0ef41Sopenharmony_ci      set_msa_register(instr_.WdValue(), wd.b);
43501cb0ef41Sopenharmony_ci      TraceMSARegWr(wd.b);
43511cb0ef41Sopenharmony_ci      break;
43521cb0ef41Sopenharmony_ci    case NORI_B:
43531cb0ef41Sopenharmony_ci      get_msa_register(instr_.WsValue(), ws.b);
43541cb0ef41Sopenharmony_ci      for (int i = 0; i < kMSALanesByte; i++) {
43551cb0ef41Sopenharmony_ci        wd.b[i] = ~(ws.b[i] | i8);
43561cb0ef41Sopenharmony_ci      }
43571cb0ef41Sopenharmony_ci      set_msa_register(instr_.WdValue(), wd.b);
43581cb0ef41Sopenharmony_ci      TraceMSARegWr(wd.b);
43591cb0ef41Sopenharmony_ci      break;
43601cb0ef41Sopenharmony_ci    case XORI_B:
43611cb0ef41Sopenharmony_ci      get_msa_register(instr_.WsValue(), ws.b);
43621cb0ef41Sopenharmony_ci      for (int i = 0; i < kMSALanesByte; i++) {
43631cb0ef41Sopenharmony_ci        wd.b[i] = ws.b[i] ^ i8;
43641cb0ef41Sopenharmony_ci      }
43651cb0ef41Sopenharmony_ci      set_msa_register(instr_.WdValue(), wd.b);
43661cb0ef41Sopenharmony_ci      TraceMSARegWr(wd.b);
43671cb0ef41Sopenharmony_ci      break;
43681cb0ef41Sopenharmony_ci    case BMNZI_B:
43691cb0ef41Sopenharmony_ci      get_msa_register(instr_.WsValue(), ws.b);
43701cb0ef41Sopenharmony_ci      get_msa_register(instr_.WdValue(), wd.b);
43711cb0ef41Sopenharmony_ci      for (int i = 0; i < kMSALanesByte; i++) {
43721cb0ef41Sopenharmony_ci        wd.b[i] = (ws.b[i] & i8) | (wd.b[i] & ~i8);
43731cb0ef41Sopenharmony_ci      }
43741cb0ef41Sopenharmony_ci      set_msa_register(instr_.WdValue(), wd.b);
43751cb0ef41Sopenharmony_ci      TraceMSARegWr(wd.b);
43761cb0ef41Sopenharmony_ci      break;
43771cb0ef41Sopenharmony_ci    case BMZI_B:
43781cb0ef41Sopenharmony_ci      get_msa_register(instr_.WsValue(), ws.b);
43791cb0ef41Sopenharmony_ci      get_msa_register(instr_.WdValue(), wd.b);
43801cb0ef41Sopenharmony_ci      for (int i = 0; i < kMSALanesByte; i++) {
43811cb0ef41Sopenharmony_ci        wd.b[i] = (ws.b[i] & ~i8) | (wd.b[i] & i8);
43821cb0ef41Sopenharmony_ci      }
43831cb0ef41Sopenharmony_ci      set_msa_register(instr_.WdValue(), wd.b);
43841cb0ef41Sopenharmony_ci      TraceMSARegWr(wd.b);
43851cb0ef41Sopenharmony_ci      break;
43861cb0ef41Sopenharmony_ci    case BSELI_B:
43871cb0ef41Sopenharmony_ci      get_msa_register(instr_.WsValue(), ws.b);
43881cb0ef41Sopenharmony_ci      get_msa_register(instr_.WdValue(), wd.b);
43891cb0ef41Sopenharmony_ci      for (int i = 0; i < kMSALanesByte; i++) {
43901cb0ef41Sopenharmony_ci        wd.b[i] = (ws.b[i] & ~wd.b[i]) | (wd.b[i] & i8);
43911cb0ef41Sopenharmony_ci      }
43921cb0ef41Sopenharmony_ci      set_msa_register(instr_.WdValue(), wd.b);
43931cb0ef41Sopenharmony_ci      TraceMSARegWr(wd.b);
43941cb0ef41Sopenharmony_ci      break;
43951cb0ef41Sopenharmony_ci    case SHF_B:
43961cb0ef41Sopenharmony_ci      get_msa_register(instr_.WsValue(), ws.b);
43971cb0ef41Sopenharmony_ci      for (int i = 0; i < kMSALanesByte; i++) {
43981cb0ef41Sopenharmony_ci        int j = i % 4;
43991cb0ef41Sopenharmony_ci        int k = (i8 >> (2 * j)) & 0x3;
44001cb0ef41Sopenharmony_ci        wd.b[i] = ws.b[i - j + k];
44011cb0ef41Sopenharmony_ci      }
44021cb0ef41Sopenharmony_ci      set_msa_register(instr_.WdValue(), wd.b);
44031cb0ef41Sopenharmony_ci      TraceMSARegWr(wd.b);
44041cb0ef41Sopenharmony_ci      break;
44051cb0ef41Sopenharmony_ci    case SHF_H:
44061cb0ef41Sopenharmony_ci      get_msa_register(instr_.WsValue(), ws.h);
44071cb0ef41Sopenharmony_ci      for (int i = 0; i < kMSALanesHalf; i++) {
44081cb0ef41Sopenharmony_ci        int j = i % 4;
44091cb0ef41Sopenharmony_ci        int k = (i8 >> (2 * j)) & 0x3;
44101cb0ef41Sopenharmony_ci        wd.h[i] = ws.h[i - j + k];
44111cb0ef41Sopenharmony_ci      }
44121cb0ef41Sopenharmony_ci      set_msa_register(instr_.WdValue(), wd.h);
44131cb0ef41Sopenharmony_ci      TraceMSARegWr(wd.h);
44141cb0ef41Sopenharmony_ci      break;
44151cb0ef41Sopenharmony_ci    case SHF_W:
44161cb0ef41Sopenharmony_ci      get_msa_register(instr_.WsValue(), ws.w);
44171cb0ef41Sopenharmony_ci      for (int i = 0; i < kMSALanesWord; i++) {
44181cb0ef41Sopenharmony_ci        int j = (i8 >> (2 * i)) & 0x3;
44191cb0ef41Sopenharmony_ci        wd.w[i] = ws.w[j];
44201cb0ef41Sopenharmony_ci      }
44211cb0ef41Sopenharmony_ci      set_msa_register(instr_.WdValue(), wd.w);
44221cb0ef41Sopenharmony_ci      TraceMSARegWr(wd.w);
44231cb0ef41Sopenharmony_ci      break;
44241cb0ef41Sopenharmony_ci    default:
44251cb0ef41Sopenharmony_ci      UNREACHABLE();
44261cb0ef41Sopenharmony_ci  }
44271cb0ef41Sopenharmony_ci}
44281cb0ef41Sopenharmony_ci
44291cb0ef41Sopenharmony_citemplate <typename T>
44301cb0ef41Sopenharmony_ciT Simulator::MsaI5InstrHelper(uint32_t opcode, T ws, int32_t i5) {
44311cb0ef41Sopenharmony_ci  T res;
44321cb0ef41Sopenharmony_ci  uint32_t ui5 = i5 & 0x1Fu;
44331cb0ef41Sopenharmony_ci  uint64_t ws_u64 = static_cast<uint64_t>(ws);
44341cb0ef41Sopenharmony_ci  uint64_t ui5_u64 = static_cast<uint64_t>(ui5);
44351cb0ef41Sopenharmony_ci
44361cb0ef41Sopenharmony_ci  switch (opcode) {
44371cb0ef41Sopenharmony_ci    case ADDVI:
44381cb0ef41Sopenharmony_ci      res = static_cast<T>(ws + ui5);
44391cb0ef41Sopenharmony_ci      break;
44401cb0ef41Sopenharmony_ci    case SUBVI:
44411cb0ef41Sopenharmony_ci      res = static_cast<T>(ws - ui5);
44421cb0ef41Sopenharmony_ci      break;
44431cb0ef41Sopenharmony_ci    case MAXI_S:
44441cb0ef41Sopenharmony_ci      res = static_cast<T>(std::max(ws, static_cast<T>(i5)));
44451cb0ef41Sopenharmony_ci      break;
44461cb0ef41Sopenharmony_ci    case MINI_S:
44471cb0ef41Sopenharmony_ci      res = static_cast<T>(std::min(ws, static_cast<T>(i5)));
44481cb0ef41Sopenharmony_ci      break;
44491cb0ef41Sopenharmony_ci    case MAXI_U:
44501cb0ef41Sopenharmony_ci      res = static_cast<T>(std::max(ws_u64, ui5_u64));
44511cb0ef41Sopenharmony_ci      break;
44521cb0ef41Sopenharmony_ci    case MINI_U:
44531cb0ef41Sopenharmony_ci      res = static_cast<T>(std::min(ws_u64, ui5_u64));
44541cb0ef41Sopenharmony_ci      break;
44551cb0ef41Sopenharmony_ci    case CEQI:
44561cb0ef41Sopenharmony_ci      res = static_cast<T>(!Compare(ws, static_cast<T>(i5)) ? -1ull : 0ull);
44571cb0ef41Sopenharmony_ci      break;
44581cb0ef41Sopenharmony_ci    case CLTI_S:
44591cb0ef41Sopenharmony_ci      res = static_cast<T>((Compare(ws, static_cast<T>(i5)) == -1) ? -1ull
44601cb0ef41Sopenharmony_ci                                                                   : 0ull);
44611cb0ef41Sopenharmony_ci      break;
44621cb0ef41Sopenharmony_ci    case CLTI_U:
44631cb0ef41Sopenharmony_ci      res = static_cast<T>((Compare(ws_u64, ui5_u64) == -1) ? -1ull : 0ull);
44641cb0ef41Sopenharmony_ci      break;
44651cb0ef41Sopenharmony_ci    case CLEI_S:
44661cb0ef41Sopenharmony_ci      res =
44671cb0ef41Sopenharmony_ci          static_cast<T>((Compare(ws, static_cast<T>(i5)) != 1) ? -1ull : 0ull);
44681cb0ef41Sopenharmony_ci      break;
44691cb0ef41Sopenharmony_ci    case CLEI_U:
44701cb0ef41Sopenharmony_ci      res = static_cast<T>((Compare(ws_u64, ui5_u64) != 1) ? -1ull : 0ull);
44711cb0ef41Sopenharmony_ci      break;
44721cb0ef41Sopenharmony_ci    default:
44731cb0ef41Sopenharmony_ci      UNREACHABLE();
44741cb0ef41Sopenharmony_ci  }
44751cb0ef41Sopenharmony_ci  return res;
44761cb0ef41Sopenharmony_ci}
44771cb0ef41Sopenharmony_ci
44781cb0ef41Sopenharmony_civoid Simulator::DecodeTypeMsaI5() {
44791cb0ef41Sopenharmony_ci  DCHECK(IsMipsArchVariant(kMips32r6));
44801cb0ef41Sopenharmony_ci  DCHECK(CpuFeatures::IsSupported(MIPS_SIMD));
44811cb0ef41Sopenharmony_ci  uint32_t opcode = instr_.InstructionBits() & kMsaI5Mask;
44821cb0ef41Sopenharmony_ci  msa_reg_t ws, wd;
44831cb0ef41Sopenharmony_ci
44841cb0ef41Sopenharmony_ci  // sign extend 5bit value to int32_t
44851cb0ef41Sopenharmony_ci  int32_t i5 = static_cast<int32_t>(instr_.MsaImm5Value() << 27) >> 27;
44861cb0ef41Sopenharmony_ci
44871cb0ef41Sopenharmony_ci#define MSA_I5_DF(elem, num_of_lanes)                      \
44881cb0ef41Sopenharmony_ci  get_msa_register(instr_.WsValue(), ws.elem);             \
44891cb0ef41Sopenharmony_ci  for (int i = 0; i < num_of_lanes; i++) {                 \
44901cb0ef41Sopenharmony_ci    wd.elem[i] = MsaI5InstrHelper(opcode, ws.elem[i], i5); \
44911cb0ef41Sopenharmony_ci  }                                                        \
44921cb0ef41Sopenharmony_ci  set_msa_register(instr_.WdValue(), wd.elem);             \
44931cb0ef41Sopenharmony_ci  TraceMSARegWr(wd.elem)
44941cb0ef41Sopenharmony_ci
44951cb0ef41Sopenharmony_ci  switch (DecodeMsaDataFormat()) {
44961cb0ef41Sopenharmony_ci    case MSA_BYTE:
44971cb0ef41Sopenharmony_ci      MSA_I5_DF(b, kMSALanesByte);
44981cb0ef41Sopenharmony_ci      break;
44991cb0ef41Sopenharmony_ci    case MSA_HALF:
45001cb0ef41Sopenharmony_ci      MSA_I5_DF(h, kMSALanesHalf);
45011cb0ef41Sopenharmony_ci      break;
45021cb0ef41Sopenharmony_ci    case MSA_WORD:
45031cb0ef41Sopenharmony_ci      MSA_I5_DF(w, kMSALanesWord);
45041cb0ef41Sopenharmony_ci      break;
45051cb0ef41Sopenharmony_ci    case MSA_DWORD:
45061cb0ef41Sopenharmony_ci      MSA_I5_DF(d, kMSALanesDword);
45071cb0ef41Sopenharmony_ci      break;
45081cb0ef41Sopenharmony_ci    default:
45091cb0ef41Sopenharmony_ci      UNREACHABLE();
45101cb0ef41Sopenharmony_ci  }
45111cb0ef41Sopenharmony_ci#undef MSA_I5_DF
45121cb0ef41Sopenharmony_ci}
45131cb0ef41Sopenharmony_ci
45141cb0ef41Sopenharmony_civoid Simulator::DecodeTypeMsaI10() {
45151cb0ef41Sopenharmony_ci  DCHECK(IsMipsArchVariant(kMips32r6));
45161cb0ef41Sopenharmony_ci  DCHECK(CpuFeatures::IsSupported(MIPS_SIMD));
45171cb0ef41Sopenharmony_ci  uint32_t opcode = instr_.InstructionBits() & kMsaI5Mask;
45181cb0ef41Sopenharmony_ci  int64_t s10 = (static_cast<int64_t>(instr_.MsaImm10Value()) << 54) >> 54;
45191cb0ef41Sopenharmony_ci  msa_reg_t wd;
45201cb0ef41Sopenharmony_ci
45211cb0ef41Sopenharmony_ci#define MSA_I10_DF(elem, num_of_lanes, T)      \
45221cb0ef41Sopenharmony_ci  for (int i = 0; i < num_of_lanes; ++i) {     \
45231cb0ef41Sopenharmony_ci    wd.elem[i] = static_cast<T>(s10);          \
45241cb0ef41Sopenharmony_ci  }                                            \
45251cb0ef41Sopenharmony_ci  set_msa_register(instr_.WdValue(), wd.elem); \
45261cb0ef41Sopenharmony_ci  TraceMSARegWr(wd.elem)
45271cb0ef41Sopenharmony_ci
45281cb0ef41Sopenharmony_ci  if (opcode == LDI) {
45291cb0ef41Sopenharmony_ci    switch (DecodeMsaDataFormat()) {
45301cb0ef41Sopenharmony_ci      case MSA_BYTE:
45311cb0ef41Sopenharmony_ci        MSA_I10_DF(b, kMSALanesByte, int8_t);
45321cb0ef41Sopenharmony_ci        break;
45331cb0ef41Sopenharmony_ci      case MSA_HALF:
45341cb0ef41Sopenharmony_ci        MSA_I10_DF(h, kMSALanesHalf, int16_t);
45351cb0ef41Sopenharmony_ci        break;
45361cb0ef41Sopenharmony_ci      case MSA_WORD:
45371cb0ef41Sopenharmony_ci        MSA_I10_DF(w, kMSALanesWord, int32_t);
45381cb0ef41Sopenharmony_ci        break;
45391cb0ef41Sopenharmony_ci      case MSA_DWORD:
45401cb0ef41Sopenharmony_ci        MSA_I10_DF(d, kMSALanesDword, int64_t);
45411cb0ef41Sopenharmony_ci        break;
45421cb0ef41Sopenharmony_ci      default:
45431cb0ef41Sopenharmony_ci        UNREACHABLE();
45441cb0ef41Sopenharmony_ci    }
45451cb0ef41Sopenharmony_ci  } else {
45461cb0ef41Sopenharmony_ci    UNREACHABLE();
45471cb0ef41Sopenharmony_ci  }
45481cb0ef41Sopenharmony_ci#undef MSA_I10_DF
45491cb0ef41Sopenharmony_ci}
45501cb0ef41Sopenharmony_ci
45511cb0ef41Sopenharmony_civoid Simulator::DecodeTypeMsaELM() {
45521cb0ef41Sopenharmony_ci  DCHECK(IsMipsArchVariant(kMips32r6));
45531cb0ef41Sopenharmony_ci  DCHECK(CpuFeatures::IsSupported(MIPS_SIMD));
45541cb0ef41Sopenharmony_ci  uint32_t opcode = instr_.InstructionBits() & kMsaLongerELMMask;
45551cb0ef41Sopenharmony_ci  int32_t n = instr_.MsaElmNValue();
45561cb0ef41Sopenharmony_ci  int32_t alu_out;
45571cb0ef41Sopenharmony_ci  switch (opcode) {
45581cb0ef41Sopenharmony_ci    case CTCMSA:
45591cb0ef41Sopenharmony_ci      DCHECK_EQ(sa(), kMSACSRRegister);
45601cb0ef41Sopenharmony_ci      MSACSR_ = bit_cast<uint32_t>(registers_[rd_reg()]);
45611cb0ef41Sopenharmony_ci      TraceRegWr(static_cast<int32_t>(MSACSR_));
45621cb0ef41Sopenharmony_ci      break;
45631cb0ef41Sopenharmony_ci    case CFCMSA:
45641cb0ef41Sopenharmony_ci      DCHECK_EQ(rd_reg(), kMSACSRRegister);
45651cb0ef41Sopenharmony_ci      SetResult(sa(), bit_cast<int32_t>(MSACSR_));
45661cb0ef41Sopenharmony_ci      break;
45671cb0ef41Sopenharmony_ci    case MOVE_V: {
45681cb0ef41Sopenharmony_ci      msa_reg_t ws;
45691cb0ef41Sopenharmony_ci      get_msa_register(ws_reg(), &ws);
45701cb0ef41Sopenharmony_ci      set_msa_register(wd_reg(), &ws);
45711cb0ef41Sopenharmony_ci      TraceMSARegWr(&ws);
45721cb0ef41Sopenharmony_ci    } break;
45731cb0ef41Sopenharmony_ci    default:
45741cb0ef41Sopenharmony_ci      opcode &= kMsaELMMask;
45751cb0ef41Sopenharmony_ci      switch (opcode) {
45761cb0ef41Sopenharmony_ci        case COPY_S:
45771cb0ef41Sopenharmony_ci        case COPY_U: {
45781cb0ef41Sopenharmony_ci          msa_reg_t ws;
45791cb0ef41Sopenharmony_ci          switch (DecodeMsaDataFormat()) {
45801cb0ef41Sopenharmony_ci            case MSA_BYTE: {
45811cb0ef41Sopenharmony_ci              DCHECK_LT(n, kMSALanesByte);
45821cb0ef41Sopenharmony_ci              get_msa_register(instr_.WsValue(), ws.b);
45831cb0ef41Sopenharmony_ci              alu_out = static_cast<int32_t>(ws.b[n]);
45841cb0ef41Sopenharmony_ci              SetResult(wd_reg(),
45851cb0ef41Sopenharmony_ci                        (opcode == COPY_U) ? alu_out & 0xFFu : alu_out);
45861cb0ef41Sopenharmony_ci              break;
45871cb0ef41Sopenharmony_ci            }
45881cb0ef41Sopenharmony_ci            case MSA_HALF: {
45891cb0ef41Sopenharmony_ci              DCHECK_LT(n, kMSALanesHalf);
45901cb0ef41Sopenharmony_ci              get_msa_register(instr_.WsValue(), ws.h);
45911cb0ef41Sopenharmony_ci              alu_out = static_cast<int32_t>(ws.h[n]);
45921cb0ef41Sopenharmony_ci              SetResult(wd_reg(),
45931cb0ef41Sopenharmony_ci                        (opcode == COPY_U) ? alu_out & 0xFFFFu : alu_out);
45941cb0ef41Sopenharmony_ci              break;
45951cb0ef41Sopenharmony_ci            }
45961cb0ef41Sopenharmony_ci            case MSA_WORD: {
45971cb0ef41Sopenharmony_ci              DCHECK_LT(n, kMSALanesWord);
45981cb0ef41Sopenharmony_ci              get_msa_register(instr_.WsValue(), ws.w);
45991cb0ef41Sopenharmony_ci              alu_out = static_cast<int32_t>(ws.w[n]);
46001cb0ef41Sopenharmony_ci              SetResult(wd_reg(), alu_out);
46011cb0ef41Sopenharmony_ci              break;
46021cb0ef41Sopenharmony_ci            }
46031cb0ef41Sopenharmony_ci            default:
46041cb0ef41Sopenharmony_ci              UNREACHABLE();
46051cb0ef41Sopenharmony_ci          }
46061cb0ef41Sopenharmony_ci        } break;
46071cb0ef41Sopenharmony_ci        case INSERT: {
46081cb0ef41Sopenharmony_ci          msa_reg_t wd;
46091cb0ef41Sopenharmony_ci          switch (DecodeMsaDataFormat()) {
46101cb0ef41Sopenharmony_ci            case MSA_BYTE: {
46111cb0ef41Sopenharmony_ci              DCHECK_LT(n, kMSALanesByte);
46121cb0ef41Sopenharmony_ci              int32_t rs = get_register(instr_.WsValue());
46131cb0ef41Sopenharmony_ci              get_msa_register(instr_.WdValue(), wd.b);
46141cb0ef41Sopenharmony_ci              wd.b[n] = rs & 0xFFu;
46151cb0ef41Sopenharmony_ci              set_msa_register(instr_.WdValue(), wd.b);
46161cb0ef41Sopenharmony_ci              TraceMSARegWr(wd.b);
46171cb0ef41Sopenharmony_ci              break;
46181cb0ef41Sopenharmony_ci            }
46191cb0ef41Sopenharmony_ci            case MSA_HALF: {
46201cb0ef41Sopenharmony_ci              DCHECK_LT(n, kMSALanesHalf);
46211cb0ef41Sopenharmony_ci              int32_t rs = get_register(instr_.WsValue());
46221cb0ef41Sopenharmony_ci              get_msa_register(instr_.WdValue(), wd.h);
46231cb0ef41Sopenharmony_ci              wd.h[n] = rs & 0xFFFFu;
46241cb0ef41Sopenharmony_ci              set_msa_register(instr_.WdValue(), wd.h);
46251cb0ef41Sopenharmony_ci              TraceMSARegWr(wd.h);
46261cb0ef41Sopenharmony_ci              break;
46271cb0ef41Sopenharmony_ci            }
46281cb0ef41Sopenharmony_ci            case MSA_WORD: {
46291cb0ef41Sopenharmony_ci              DCHECK_LT(n, kMSALanesWord);
46301cb0ef41Sopenharmony_ci              int32_t rs = get_register(instr_.WsValue());
46311cb0ef41Sopenharmony_ci              get_msa_register(instr_.WdValue(), wd.w);
46321cb0ef41Sopenharmony_ci              wd.w[n] = rs;
46331cb0ef41Sopenharmony_ci              set_msa_register(instr_.WdValue(), wd.w);
46341cb0ef41Sopenharmony_ci              TraceMSARegWr(wd.w);
46351cb0ef41Sopenharmony_ci              break;
46361cb0ef41Sopenharmony_ci            }
46371cb0ef41Sopenharmony_ci            default:
46381cb0ef41Sopenharmony_ci              UNREACHABLE();
46391cb0ef41Sopenharmony_ci          }
46401cb0ef41Sopenharmony_ci        } break;
46411cb0ef41Sopenharmony_ci        case SLDI: {
46421cb0ef41Sopenharmony_ci          uint8_t v[32];
46431cb0ef41Sopenharmony_ci          msa_reg_t ws;
46441cb0ef41Sopenharmony_ci          msa_reg_t wd;
46451cb0ef41Sopenharmony_ci          get_msa_register(ws_reg(), &ws);
46461cb0ef41Sopenharmony_ci          get_msa_register(wd_reg(), &wd);
46471cb0ef41Sopenharmony_ci#define SLDI_DF(s, k)                \
46481cb0ef41Sopenharmony_ci  for (unsigned i = 0; i < s; i++) { \
46491cb0ef41Sopenharmony_ci    v[i] = ws.b[s * k + i];          \
46501cb0ef41Sopenharmony_ci    v[i + s] = wd.b[s * k + i];      \
46511cb0ef41Sopenharmony_ci  }                                  \
46521cb0ef41Sopenharmony_ci  for (unsigned i = 0; i < s; i++) { \
46531cb0ef41Sopenharmony_ci    wd.b[s * k + i] = v[i + n];      \
46541cb0ef41Sopenharmony_ci  }
46551cb0ef41Sopenharmony_ci          switch (DecodeMsaDataFormat()) {
46561cb0ef41Sopenharmony_ci            case MSA_BYTE:
46571cb0ef41Sopenharmony_ci              DCHECK(n < kMSALanesByte);
46581cb0ef41Sopenharmony_ci              SLDI_DF(kMSARegSize / sizeof(int8_t) / kBitsPerByte, 0)
46591cb0ef41Sopenharmony_ci              break;
46601cb0ef41Sopenharmony_ci            case MSA_HALF:
46611cb0ef41Sopenharmony_ci              DCHECK(n < kMSALanesHalf);
46621cb0ef41Sopenharmony_ci              for (int k = 0; k < 2; ++k) {
46631cb0ef41Sopenharmony_ci                SLDI_DF(kMSARegSize / sizeof(int16_t) / kBitsPerByte, k)
46641cb0ef41Sopenharmony_ci              }
46651cb0ef41Sopenharmony_ci              break;
46661cb0ef41Sopenharmony_ci            case MSA_WORD:
46671cb0ef41Sopenharmony_ci              DCHECK(n < kMSALanesWord);
46681cb0ef41Sopenharmony_ci              for (int k = 0; k < 4; ++k) {
46691cb0ef41Sopenharmony_ci                SLDI_DF(kMSARegSize / sizeof(int32_t) / kBitsPerByte, k)
46701cb0ef41Sopenharmony_ci              }
46711cb0ef41Sopenharmony_ci              break;
46721cb0ef41Sopenharmony_ci            case MSA_DWORD:
46731cb0ef41Sopenharmony_ci              DCHECK(n < kMSALanesDword);
46741cb0ef41Sopenharmony_ci              for (int k = 0; k < 8; ++k) {
46751cb0ef41Sopenharmony_ci                SLDI_DF(kMSARegSize / sizeof(int64_t) / kBitsPerByte, k)
46761cb0ef41Sopenharmony_ci              }
46771cb0ef41Sopenharmony_ci              break;
46781cb0ef41Sopenharmony_ci            default:
46791cb0ef41Sopenharmony_ci              UNREACHABLE();
46801cb0ef41Sopenharmony_ci          }
46811cb0ef41Sopenharmony_ci          set_msa_register(wd_reg(), &wd);
46821cb0ef41Sopenharmony_ci          TraceMSARegWr(&wd);
46831cb0ef41Sopenharmony_ci        } break;
46841cb0ef41Sopenharmony_ci#undef SLDI_DF
46851cb0ef41Sopenharmony_ci        case SPLATI:
46861cb0ef41Sopenharmony_ci        case INSVE:
46871cb0ef41Sopenharmony_ci          UNIMPLEMENTED();
46881cb0ef41Sopenharmony_ci        default:
46891cb0ef41Sopenharmony_ci          UNREACHABLE();
46901cb0ef41Sopenharmony_ci      }
46911cb0ef41Sopenharmony_ci      break;
46921cb0ef41Sopenharmony_ci  }
46931cb0ef41Sopenharmony_ci}
46941cb0ef41Sopenharmony_ci
46951cb0ef41Sopenharmony_citemplate <typename T>
46961cb0ef41Sopenharmony_ciT Simulator::MsaBitInstrHelper(uint32_t opcode, T wd, T ws, int32_t m) {
46971cb0ef41Sopenharmony_ci  using uT = typename std::make_unsigned<T>::type;
46981cb0ef41Sopenharmony_ci  T res;
46991cb0ef41Sopenharmony_ci  switch (opcode) {
47001cb0ef41Sopenharmony_ci    case SLLI:
47011cb0ef41Sopenharmony_ci      res = static_cast<T>(ws << m);
47021cb0ef41Sopenharmony_ci      break;
47031cb0ef41Sopenharmony_ci    case SRAI:
47041cb0ef41Sopenharmony_ci      res = static_cast<T>(ArithmeticShiftRight(ws, m));
47051cb0ef41Sopenharmony_ci      break;
47061cb0ef41Sopenharmony_ci    case SRLI:
47071cb0ef41Sopenharmony_ci      res = static_cast<T>(static_cast<uT>(ws) >> m);
47081cb0ef41Sopenharmony_ci      break;
47091cb0ef41Sopenharmony_ci    case BCLRI:
47101cb0ef41Sopenharmony_ci      res = static_cast<T>(static_cast<T>(~(1ull << m)) & ws);
47111cb0ef41Sopenharmony_ci      break;
47121cb0ef41Sopenharmony_ci    case BSETI:
47131cb0ef41Sopenharmony_ci      res = static_cast<T>(static_cast<T>(1ull << m) | ws);
47141cb0ef41Sopenharmony_ci      break;
47151cb0ef41Sopenharmony_ci    case BNEGI:
47161cb0ef41Sopenharmony_ci      res = static_cast<T>(static_cast<T>(1ull << m) ^ ws);
47171cb0ef41Sopenharmony_ci      break;
47181cb0ef41Sopenharmony_ci    case BINSLI: {
47191cb0ef41Sopenharmony_ci      int elem_size = 8 * sizeof(T);
47201cb0ef41Sopenharmony_ci      int bits = m + 1;
47211cb0ef41Sopenharmony_ci      if (bits == elem_size) {
47221cb0ef41Sopenharmony_ci        res = static_cast<T>(ws);
47231cb0ef41Sopenharmony_ci      } else {
47241cb0ef41Sopenharmony_ci        uint64_t mask = ((1ull << bits) - 1) << (elem_size - bits);
47251cb0ef41Sopenharmony_ci        res = static_cast<T>((static_cast<T>(mask) & ws) |
47261cb0ef41Sopenharmony_ci                             (static_cast<T>(~mask) & wd));
47271cb0ef41Sopenharmony_ci      }
47281cb0ef41Sopenharmony_ci    } break;
47291cb0ef41Sopenharmony_ci    case BINSRI: {
47301cb0ef41Sopenharmony_ci      int elem_size = 8 * sizeof(T);
47311cb0ef41Sopenharmony_ci      int bits = m + 1;
47321cb0ef41Sopenharmony_ci      if (bits == elem_size) {
47331cb0ef41Sopenharmony_ci        res = static_cast<T>(ws);
47341cb0ef41Sopenharmony_ci      } else {
47351cb0ef41Sopenharmony_ci        uint64_t mask = (1ull << bits) - 1;
47361cb0ef41Sopenharmony_ci        res = static_cast<T>((static_cast<T>(mask) & ws) |
47371cb0ef41Sopenharmony_ci                             (static_cast<T>(~mask) & wd));
47381cb0ef41Sopenharmony_ci      }
47391cb0ef41Sopenharmony_ci    } break;
47401cb0ef41Sopenharmony_ci    case SAT_S: {
47411cb0ef41Sopenharmony_ci#define M_MAX_INT(x) static_cast<int64_t>((1LL << ((x)-1)) - 1)
47421cb0ef41Sopenharmony_ci#define M_MIN_INT(x) static_cast<int64_t>(-(1LL << ((x)-1)))
47431cb0ef41Sopenharmony_ci      int shift = 64 - 8 * sizeof(T);
47441cb0ef41Sopenharmony_ci      int64_t ws_i64 = (static_cast<int64_t>(ws) << shift) >> shift;
47451cb0ef41Sopenharmony_ci      res = static_cast<T>(ws_i64 < M_MIN_INT(m + 1)
47461cb0ef41Sopenharmony_ci                               ? M_MIN_INT(m + 1)
47471cb0ef41Sopenharmony_ci                               : ws_i64 > M_MAX_INT(m + 1) ? M_MAX_INT(m + 1)
47481cb0ef41Sopenharmony_ci                                                           : ws_i64);
47491cb0ef41Sopenharmony_ci#undef M_MAX_INT
47501cb0ef41Sopenharmony_ci#undef M_MIN_INT
47511cb0ef41Sopenharmony_ci    } break;
47521cb0ef41Sopenharmony_ci    case SAT_U: {
47531cb0ef41Sopenharmony_ci#define M_MAX_UINT(x) static_cast<uint64_t>(-1ULL >> (64 - (x)))
47541cb0ef41Sopenharmony_ci      uint64_t mask = static_cast<uint64_t>(-1ULL >> (64 - 8 * sizeof(T)));
47551cb0ef41Sopenharmony_ci      uint64_t ws_u64 = static_cast<uint64_t>(ws) & mask;
47561cb0ef41Sopenharmony_ci      res = static_cast<T>(ws_u64 < M_MAX_UINT(m + 1) ? ws_u64
47571cb0ef41Sopenharmony_ci                                                      : M_MAX_UINT(m + 1));
47581cb0ef41Sopenharmony_ci#undef M_MAX_UINT
47591cb0ef41Sopenharmony_ci    } break;
47601cb0ef41Sopenharmony_ci    case SRARI:
47611cb0ef41Sopenharmony_ci      if (!m) {
47621cb0ef41Sopenharmony_ci        res = static_cast<T>(ws);
47631cb0ef41Sopenharmony_ci      } else {
47641cb0ef41Sopenharmony_ci        res = static_cast<T>(ArithmeticShiftRight(ws, m)) +
47651cb0ef41Sopenharmony_ci              static_cast<T>((ws >> (m - 1)) & 0x1);
47661cb0ef41Sopenharmony_ci      }
47671cb0ef41Sopenharmony_ci      break;
47681cb0ef41Sopenharmony_ci    case SRLRI:
47691cb0ef41Sopenharmony_ci      if (!m) {
47701cb0ef41Sopenharmony_ci        res = static_cast<T>(ws);
47711cb0ef41Sopenharmony_ci      } else {
47721cb0ef41Sopenharmony_ci        res = static_cast<T>(static_cast<uT>(ws) >> m) +
47731cb0ef41Sopenharmony_ci              static_cast<T>((ws >> (m - 1)) & 0x1);
47741cb0ef41Sopenharmony_ci      }
47751cb0ef41Sopenharmony_ci      break;
47761cb0ef41Sopenharmony_ci    default:
47771cb0ef41Sopenharmony_ci      UNREACHABLE();
47781cb0ef41Sopenharmony_ci  }
47791cb0ef41Sopenharmony_ci  return res;
47801cb0ef41Sopenharmony_ci}
47811cb0ef41Sopenharmony_ci
47821cb0ef41Sopenharmony_civoid Simulator::DecodeTypeMsaBIT() {
47831cb0ef41Sopenharmony_ci  DCHECK(IsMipsArchVariant(kMips32r6));
47841cb0ef41Sopenharmony_ci  DCHECK(CpuFeatures::IsSupported(MIPS_SIMD));
47851cb0ef41Sopenharmony_ci  uint32_t opcode = instr_.InstructionBits() & kMsaBITMask;
47861cb0ef41Sopenharmony_ci  int32_t m = instr_.MsaBitMValue();
47871cb0ef41Sopenharmony_ci  msa_reg_t wd, ws;
47881cb0ef41Sopenharmony_ci
47891cb0ef41Sopenharmony_ci#define MSA_BIT_DF(elem, num_of_lanes)                                 \
47901cb0ef41Sopenharmony_ci  get_msa_register(instr_.WsValue(), ws.elem);                         \
47911cb0ef41Sopenharmony_ci  if (opcode == BINSLI || opcode == BINSRI) {                          \
47921cb0ef41Sopenharmony_ci    get_msa_register(instr_.WdValue(), wd.elem);                       \
47931cb0ef41Sopenharmony_ci  }                                                                    \
47941cb0ef41Sopenharmony_ci  for (int i = 0; i < num_of_lanes; i++) {                             \
47951cb0ef41Sopenharmony_ci    wd.elem[i] = MsaBitInstrHelper(opcode, wd.elem[i], ws.elem[i], m); \
47961cb0ef41Sopenharmony_ci  }                                                                    \
47971cb0ef41Sopenharmony_ci  set_msa_register(instr_.WdValue(), wd.elem);                         \
47981cb0ef41Sopenharmony_ci  TraceMSARegWr(wd.elem)
47991cb0ef41Sopenharmony_ci
48001cb0ef41Sopenharmony_ci  switch (DecodeMsaDataFormat()) {
48011cb0ef41Sopenharmony_ci    case MSA_BYTE:
48021cb0ef41Sopenharmony_ci      DCHECK(m < kMSARegSize / kMSALanesByte);
48031cb0ef41Sopenharmony_ci      MSA_BIT_DF(b, kMSALanesByte);
48041cb0ef41Sopenharmony_ci      break;
48051cb0ef41Sopenharmony_ci    case MSA_HALF:
48061cb0ef41Sopenharmony_ci      DCHECK(m < kMSARegSize / kMSALanesHalf);
48071cb0ef41Sopenharmony_ci      MSA_BIT_DF(h, kMSALanesHalf);
48081cb0ef41Sopenharmony_ci      break;
48091cb0ef41Sopenharmony_ci    case MSA_WORD:
48101cb0ef41Sopenharmony_ci      DCHECK(m < kMSARegSize / kMSALanesWord);
48111cb0ef41Sopenharmony_ci      MSA_BIT_DF(w, kMSALanesWord);
48121cb0ef41Sopenharmony_ci      break;
48131cb0ef41Sopenharmony_ci    case MSA_DWORD:
48141cb0ef41Sopenharmony_ci      DCHECK(m < kMSARegSize / kMSALanesDword);
48151cb0ef41Sopenharmony_ci      MSA_BIT_DF(d, kMSALanesDword);
48161cb0ef41Sopenharmony_ci      break;
48171cb0ef41Sopenharmony_ci    default:
48181cb0ef41Sopenharmony_ci      UNREACHABLE();
48191cb0ef41Sopenharmony_ci  }
48201cb0ef41Sopenharmony_ci#undef MSA_BIT_DF
48211cb0ef41Sopenharmony_ci}
48221cb0ef41Sopenharmony_ci
48231cb0ef41Sopenharmony_civoid Simulator::DecodeTypeMsaMI10() {
48241cb0ef41Sopenharmony_ci  DCHECK(IsMipsArchVariant(kMips32r6));
48251cb0ef41Sopenharmony_ci  DCHECK(CpuFeatures::IsSupported(MIPS_SIMD));
48261cb0ef41Sopenharmony_ci  uint32_t opcode = instr_.InstructionBits() & kMsaMI10Mask;
48271cb0ef41Sopenharmony_ci  int32_t s10 = (static_cast<int32_t>(instr_.MsaImmMI10Value()) << 22) >> 22;
48281cb0ef41Sopenharmony_ci  int32_t rs = get_register(instr_.WsValue());
48291cb0ef41Sopenharmony_ci  int32_t addr;
48301cb0ef41Sopenharmony_ci  msa_reg_t wd;
48311cb0ef41Sopenharmony_ci
48321cb0ef41Sopenharmony_ci#define MSA_MI10_LOAD(elem, num_of_lanes, T)       \
48331cb0ef41Sopenharmony_ci  for (int i = 0; i < num_of_lanes; ++i) {         \
48341cb0ef41Sopenharmony_ci    addr = rs + (s10 + i) * sizeof(T);             \
48351cb0ef41Sopenharmony_ci    wd.elem[i] = ReadMem<T>(addr, instr_.instr()); \
48361cb0ef41Sopenharmony_ci  }                                                \
48371cb0ef41Sopenharmony_ci  set_msa_register(instr_.WdValue(), wd.elem);
48381cb0ef41Sopenharmony_ci
48391cb0ef41Sopenharmony_ci#define MSA_MI10_STORE(elem, num_of_lanes, T)      \
48401cb0ef41Sopenharmony_ci  get_msa_register(instr_.WdValue(), wd.elem);     \
48411cb0ef41Sopenharmony_ci  for (int i = 0; i < num_of_lanes; ++i) {         \
48421cb0ef41Sopenharmony_ci    addr = rs + (s10 + i) * sizeof(T);             \
48431cb0ef41Sopenharmony_ci    WriteMem<T>(addr, wd.elem[i], instr_.instr()); \
48441cb0ef41Sopenharmony_ci  }
48451cb0ef41Sopenharmony_ci
48461cb0ef41Sopenharmony_ci  if (opcode == MSA_LD) {
48471cb0ef41Sopenharmony_ci    switch (DecodeMsaDataFormat()) {
48481cb0ef41Sopenharmony_ci      case MSA_BYTE:
48491cb0ef41Sopenharmony_ci        MSA_MI10_LOAD(b, kMSALanesByte, int8_t);
48501cb0ef41Sopenharmony_ci        break;
48511cb0ef41Sopenharmony_ci      case MSA_HALF:
48521cb0ef41Sopenharmony_ci        MSA_MI10_LOAD(h, kMSALanesHalf, int16_t);
48531cb0ef41Sopenharmony_ci        break;
48541cb0ef41Sopenharmony_ci      case MSA_WORD:
48551cb0ef41Sopenharmony_ci        MSA_MI10_LOAD(w, kMSALanesWord, int32_t);
48561cb0ef41Sopenharmony_ci        break;
48571cb0ef41Sopenharmony_ci      case MSA_DWORD:
48581cb0ef41Sopenharmony_ci        MSA_MI10_LOAD(d, kMSALanesDword, int64_t);
48591cb0ef41Sopenharmony_ci        break;
48601cb0ef41Sopenharmony_ci      default:
48611cb0ef41Sopenharmony_ci        UNREACHABLE();
48621cb0ef41Sopenharmony_ci    }
48631cb0ef41Sopenharmony_ci  } else if (opcode == MSA_ST) {
48641cb0ef41Sopenharmony_ci    switch (DecodeMsaDataFormat()) {
48651cb0ef41Sopenharmony_ci      case MSA_BYTE:
48661cb0ef41Sopenharmony_ci        MSA_MI10_STORE(b, kMSALanesByte, int8_t);
48671cb0ef41Sopenharmony_ci        break;
48681cb0ef41Sopenharmony_ci      case MSA_HALF:
48691cb0ef41Sopenharmony_ci        MSA_MI10_STORE(h, kMSALanesHalf, int16_t);
48701cb0ef41Sopenharmony_ci        break;
48711cb0ef41Sopenharmony_ci      case MSA_WORD:
48721cb0ef41Sopenharmony_ci        MSA_MI10_STORE(w, kMSALanesWord, int32_t);
48731cb0ef41Sopenharmony_ci        break;
48741cb0ef41Sopenharmony_ci      case MSA_DWORD:
48751cb0ef41Sopenharmony_ci        MSA_MI10_STORE(d, kMSALanesDword, int64_t);
48761cb0ef41Sopenharmony_ci        break;
48771cb0ef41Sopenharmony_ci      default:
48781cb0ef41Sopenharmony_ci        UNREACHABLE();
48791cb0ef41Sopenharmony_ci    }
48801cb0ef41Sopenharmony_ci  } else {
48811cb0ef41Sopenharmony_ci    UNREACHABLE();
48821cb0ef41Sopenharmony_ci  }
48831cb0ef41Sopenharmony_ci
48841cb0ef41Sopenharmony_ci#undef MSA_MI10_LOAD
48851cb0ef41Sopenharmony_ci#undef MSA_MI10_STORE
48861cb0ef41Sopenharmony_ci}
48871cb0ef41Sopenharmony_ci
48881cb0ef41Sopenharmony_citemplate <typename T>
48891cb0ef41Sopenharmony_ciT Simulator::Msa3RInstrHelper(uint32_t opcode, T wd, T ws, T wt) {
48901cb0ef41Sopenharmony_ci  using uT = typename std::make_unsigned<T>::type;
48911cb0ef41Sopenharmony_ci  T res;
48921cb0ef41Sopenharmony_ci  T wt_modulo = wt % (sizeof(T) * 8);
48931cb0ef41Sopenharmony_ci  switch (opcode) {
48941cb0ef41Sopenharmony_ci    case SLL_MSA:
48951cb0ef41Sopenharmony_ci      res = static_cast<T>(ws << wt_modulo);
48961cb0ef41Sopenharmony_ci      break;
48971cb0ef41Sopenharmony_ci    case SRA_MSA:
48981cb0ef41Sopenharmony_ci      res = static_cast<T>(ArithmeticShiftRight(ws, wt_modulo));
48991cb0ef41Sopenharmony_ci      break;
49001cb0ef41Sopenharmony_ci    case SRL_MSA:
49011cb0ef41Sopenharmony_ci      res = static_cast<T>(static_cast<uT>(ws) >> wt_modulo);
49021cb0ef41Sopenharmony_ci      break;
49031cb0ef41Sopenharmony_ci    case BCLR:
49041cb0ef41Sopenharmony_ci      res = static_cast<T>(static_cast<T>(~(1ull << wt_modulo)) & ws);
49051cb0ef41Sopenharmony_ci      break;
49061cb0ef41Sopenharmony_ci    case BSET:
49071cb0ef41Sopenharmony_ci      res = static_cast<T>(static_cast<T>(1ull << wt_modulo) | ws);
49081cb0ef41Sopenharmony_ci      break;
49091cb0ef41Sopenharmony_ci    case BNEG:
49101cb0ef41Sopenharmony_ci      res = static_cast<T>(static_cast<T>(1ull << wt_modulo) ^ ws);
49111cb0ef41Sopenharmony_ci      break;
49121cb0ef41Sopenharmony_ci    case BINSL: {
49131cb0ef41Sopenharmony_ci      int elem_size = 8 * sizeof(T);
49141cb0ef41Sopenharmony_ci      int bits = wt_modulo + 1;
49151cb0ef41Sopenharmony_ci      if (bits == elem_size) {
49161cb0ef41Sopenharmony_ci        res = static_cast<T>(ws);
49171cb0ef41Sopenharmony_ci      } else {
49181cb0ef41Sopenharmony_ci        uint64_t mask = ((1ull << bits) - 1) << (elem_size - bits);
49191cb0ef41Sopenharmony_ci        res = static_cast<T>((static_cast<T>(mask) & ws) |
49201cb0ef41Sopenharmony_ci                             (static_cast<T>(~mask) & wd));
49211cb0ef41Sopenharmony_ci      }
49221cb0ef41Sopenharmony_ci    } break;
49231cb0ef41Sopenharmony_ci    case BINSR: {
49241cb0ef41Sopenharmony_ci      int elem_size = 8 * sizeof(T);
49251cb0ef41Sopenharmony_ci      int bits = wt_modulo + 1;
49261cb0ef41Sopenharmony_ci      if (bits == elem_size) {
49271cb0ef41Sopenharmony_ci        res = static_cast<T>(ws);
49281cb0ef41Sopenharmony_ci      } else {
49291cb0ef41Sopenharmony_ci        uint64_t mask = (1ull << bits) - 1;
49301cb0ef41Sopenharmony_ci        res = static_cast<T>((static_cast<T>(mask) & ws) |
49311cb0ef41Sopenharmony_ci                             (static_cast<T>(~mask) & wd));
49321cb0ef41Sopenharmony_ci      }
49331cb0ef41Sopenharmony_ci    } break;
49341cb0ef41Sopenharmony_ci    case ADDV:
49351cb0ef41Sopenharmony_ci      res = ws + wt;
49361cb0ef41Sopenharmony_ci      break;
49371cb0ef41Sopenharmony_ci    case SUBV:
49381cb0ef41Sopenharmony_ci      res = ws - wt;
49391cb0ef41Sopenharmony_ci      break;
49401cb0ef41Sopenharmony_ci    case MAX_S:
49411cb0ef41Sopenharmony_ci      res = std::max(ws, wt);
49421cb0ef41Sopenharmony_ci      break;
49431cb0ef41Sopenharmony_ci    case MAX_U:
49441cb0ef41Sopenharmony_ci      res = static_cast<T>(std::max(static_cast<uT>(ws), static_cast<uT>(wt)));
49451cb0ef41Sopenharmony_ci      break;
49461cb0ef41Sopenharmony_ci    case MIN_S:
49471cb0ef41Sopenharmony_ci      res = std::min(ws, wt);
49481cb0ef41Sopenharmony_ci      break;
49491cb0ef41Sopenharmony_ci    case MIN_U:
49501cb0ef41Sopenharmony_ci      res = static_cast<T>(std::min(static_cast<uT>(ws), static_cast<uT>(wt)));
49511cb0ef41Sopenharmony_ci      break;
49521cb0ef41Sopenharmony_ci    case MAX_A:
49531cb0ef41Sopenharmony_ci      // We use negative abs in order to avoid problems
49541cb0ef41Sopenharmony_ci      // with corner case for MIN_INT
49551cb0ef41Sopenharmony_ci      res = Nabs(ws) < Nabs(wt) ? ws : wt;
49561cb0ef41Sopenharmony_ci      break;
49571cb0ef41Sopenharmony_ci    case MIN_A:
49581cb0ef41Sopenharmony_ci      // We use negative abs in order to avoid problems
49591cb0ef41Sopenharmony_ci      // with corner case for MIN_INT
49601cb0ef41Sopenharmony_ci      res = Nabs(ws) > Nabs(wt) ? ws : wt;
49611cb0ef41Sopenharmony_ci      break;
49621cb0ef41Sopenharmony_ci    case CEQ:
49631cb0ef41Sopenharmony_ci      res = static_cast<T>(!Compare(ws, wt) ? -1ull : 0ull);
49641cb0ef41Sopenharmony_ci      break;
49651cb0ef41Sopenharmony_ci    case CLT_S:
49661cb0ef41Sopenharmony_ci      res = static_cast<T>((Compare(ws, wt) == -1) ? -1ull : 0ull);
49671cb0ef41Sopenharmony_ci      break;
49681cb0ef41Sopenharmony_ci    case CLT_U:
49691cb0ef41Sopenharmony_ci      res = static_cast<T>(
49701cb0ef41Sopenharmony_ci          (Compare(static_cast<uT>(ws), static_cast<uT>(wt)) == -1) ? -1ull
49711cb0ef41Sopenharmony_ci                                                                    : 0ull);
49721cb0ef41Sopenharmony_ci      break;
49731cb0ef41Sopenharmony_ci    case CLE_S:
49741cb0ef41Sopenharmony_ci      res = static_cast<T>((Compare(ws, wt) != 1) ? -1ull : 0ull);
49751cb0ef41Sopenharmony_ci      break;
49761cb0ef41Sopenharmony_ci    case CLE_U:
49771cb0ef41Sopenharmony_ci      res = static_cast<T>(
49781cb0ef41Sopenharmony_ci          (Compare(static_cast<uT>(ws), static_cast<uT>(wt)) != 1) ? -1ull
49791cb0ef41Sopenharmony_ci                                                                   : 0ull);
49801cb0ef41Sopenharmony_ci      break;
49811cb0ef41Sopenharmony_ci    case ADD_A:
49821cb0ef41Sopenharmony_ci      res = static_cast<T>(Abs(ws) + Abs(wt));
49831cb0ef41Sopenharmony_ci      break;
49841cb0ef41Sopenharmony_ci    case ADDS_A: {
49851cb0ef41Sopenharmony_ci      T ws_nabs = Nabs(ws);
49861cb0ef41Sopenharmony_ci      T wt_nabs = Nabs(wt);
49871cb0ef41Sopenharmony_ci      if (ws_nabs < -std::numeric_limits<T>::max() - wt_nabs) {
49881cb0ef41Sopenharmony_ci        res = std::numeric_limits<T>::max();
49891cb0ef41Sopenharmony_ci      } else {
49901cb0ef41Sopenharmony_ci        res = -(ws_nabs + wt_nabs);
49911cb0ef41Sopenharmony_ci      }
49921cb0ef41Sopenharmony_ci    } break;
49931cb0ef41Sopenharmony_ci    case ADDS_S:
49941cb0ef41Sopenharmony_ci      res = SaturateAdd(ws, wt);
49951cb0ef41Sopenharmony_ci      break;
49961cb0ef41Sopenharmony_ci    case ADDS_U: {
49971cb0ef41Sopenharmony_ci      uT ws_u = static_cast<uT>(ws);
49981cb0ef41Sopenharmony_ci      uT wt_u = static_cast<uT>(wt);
49991cb0ef41Sopenharmony_ci      res = static_cast<T>(SaturateAdd(ws_u, wt_u));
50001cb0ef41Sopenharmony_ci    } break;
50011cb0ef41Sopenharmony_ci    case AVE_S:
50021cb0ef41Sopenharmony_ci      res = static_cast<T>((wt & ws) + ((wt ^ ws) >> 1));
50031cb0ef41Sopenharmony_ci      break;
50041cb0ef41Sopenharmony_ci    case AVE_U: {
50051cb0ef41Sopenharmony_ci      uT ws_u = static_cast<uT>(ws);
50061cb0ef41Sopenharmony_ci      uT wt_u = static_cast<uT>(wt);
50071cb0ef41Sopenharmony_ci      res = static_cast<T>((wt_u & ws_u) + ((wt_u ^ ws_u) >> 1));
50081cb0ef41Sopenharmony_ci    } break;
50091cb0ef41Sopenharmony_ci    case AVER_S:
50101cb0ef41Sopenharmony_ci      res = static_cast<T>((wt | ws) - ((wt ^ ws) >> 1));
50111cb0ef41Sopenharmony_ci      break;
50121cb0ef41Sopenharmony_ci    case AVER_U: {
50131cb0ef41Sopenharmony_ci      uT ws_u = static_cast<uT>(ws);
50141cb0ef41Sopenharmony_ci      uT wt_u = static_cast<uT>(wt);
50151cb0ef41Sopenharmony_ci      res = static_cast<T>((wt_u | ws_u) - ((wt_u ^ ws_u) >> 1));
50161cb0ef41Sopenharmony_ci    } break;
50171cb0ef41Sopenharmony_ci    case SUBS_S:
50181cb0ef41Sopenharmony_ci      res = SaturateSub(ws, wt);
50191cb0ef41Sopenharmony_ci      break;
50201cb0ef41Sopenharmony_ci    case SUBS_U: {
50211cb0ef41Sopenharmony_ci      uT ws_u = static_cast<uT>(ws);
50221cb0ef41Sopenharmony_ci      uT wt_u = static_cast<uT>(wt);
50231cb0ef41Sopenharmony_ci      res = static_cast<T>(SaturateSub(ws_u, wt_u));
50241cb0ef41Sopenharmony_ci    } break;
50251cb0ef41Sopenharmony_ci    case SUBSUS_U: {
50261cb0ef41Sopenharmony_ci      uT wsu = static_cast<uT>(ws);
50271cb0ef41Sopenharmony_ci      if (wt > 0) {
50281cb0ef41Sopenharmony_ci        uT wtu = static_cast<uT>(wt);
50291cb0ef41Sopenharmony_ci        if (wtu > wsu) {
50301cb0ef41Sopenharmony_ci          res = 0;
50311cb0ef41Sopenharmony_ci        } else {
50321cb0ef41Sopenharmony_ci          res = static_cast<T>(wsu - wtu);
50331cb0ef41Sopenharmony_ci        }
50341cb0ef41Sopenharmony_ci      } else {
50351cb0ef41Sopenharmony_ci        if (wsu > std::numeric_limits<uT>::max() + wt) {
50361cb0ef41Sopenharmony_ci          res = static_cast<T>(std::numeric_limits<uT>::max());
50371cb0ef41Sopenharmony_ci        } else {
50381cb0ef41Sopenharmony_ci          res = static_cast<T>(wsu - wt);
50391cb0ef41Sopenharmony_ci        }
50401cb0ef41Sopenharmony_ci      }
50411cb0ef41Sopenharmony_ci    } break;
50421cb0ef41Sopenharmony_ci    case SUBSUU_S: {
50431cb0ef41Sopenharmony_ci      uT wsu = static_cast<uT>(ws);
50441cb0ef41Sopenharmony_ci      uT wtu = static_cast<uT>(wt);
50451cb0ef41Sopenharmony_ci      uT wdu;
50461cb0ef41Sopenharmony_ci      if (wsu > wtu) {
50471cb0ef41Sopenharmony_ci        wdu = wsu - wtu;
50481cb0ef41Sopenharmony_ci        if (wdu > std::numeric_limits<T>::max()) {
50491cb0ef41Sopenharmony_ci          res = std::numeric_limits<T>::max();
50501cb0ef41Sopenharmony_ci        } else {
50511cb0ef41Sopenharmony_ci          res = static_cast<T>(wdu);
50521cb0ef41Sopenharmony_ci        }
50531cb0ef41Sopenharmony_ci      } else {
50541cb0ef41Sopenharmony_ci        wdu = wtu - wsu;
50551cb0ef41Sopenharmony_ci        CHECK(-std::numeric_limits<T>::max() ==
50561cb0ef41Sopenharmony_ci              std::numeric_limits<T>::min() + 1);
50571cb0ef41Sopenharmony_ci        if (wdu <= std::numeric_limits<T>::max()) {
50581cb0ef41Sopenharmony_ci          res = -static_cast<T>(wdu);
50591cb0ef41Sopenharmony_ci        } else {
50601cb0ef41Sopenharmony_ci          res = std::numeric_limits<T>::min();
50611cb0ef41Sopenharmony_ci        }
50621cb0ef41Sopenharmony_ci      }
50631cb0ef41Sopenharmony_ci    } break;
50641cb0ef41Sopenharmony_ci    case ASUB_S:
50651cb0ef41Sopenharmony_ci      res = static_cast<T>(Abs(ws - wt));
50661cb0ef41Sopenharmony_ci      break;
50671cb0ef41Sopenharmony_ci    case ASUB_U: {
50681cb0ef41Sopenharmony_ci      uT wsu = static_cast<uT>(ws);
50691cb0ef41Sopenharmony_ci      uT wtu = static_cast<uT>(wt);
50701cb0ef41Sopenharmony_ci      res = static_cast<T>(wsu > wtu ? wsu - wtu : wtu - wsu);
50711cb0ef41Sopenharmony_ci    } break;
50721cb0ef41Sopenharmony_ci    case MULV:
50731cb0ef41Sopenharmony_ci      res = ws * wt;
50741cb0ef41Sopenharmony_ci      break;
50751cb0ef41Sopenharmony_ci    case MADDV:
50761cb0ef41Sopenharmony_ci      res = wd + ws * wt;
50771cb0ef41Sopenharmony_ci      break;
50781cb0ef41Sopenharmony_ci    case MSUBV:
50791cb0ef41Sopenharmony_ci      res = wd - ws * wt;
50801cb0ef41Sopenharmony_ci      break;
50811cb0ef41Sopenharmony_ci    case DIV_S_MSA:
50821cb0ef41Sopenharmony_ci      res = wt != 0 ? ws / wt : static_cast<T>(Unpredictable);
50831cb0ef41Sopenharmony_ci      break;
50841cb0ef41Sopenharmony_ci    case DIV_U:
50851cb0ef41Sopenharmony_ci      res = wt != 0 ? static_cast<T>(static_cast<uT>(ws) / static_cast<uT>(wt))
50861cb0ef41Sopenharmony_ci                    : static_cast<T>(Unpredictable);
50871cb0ef41Sopenharmony_ci      break;
50881cb0ef41Sopenharmony_ci    case MOD_S:
50891cb0ef41Sopenharmony_ci      res = wt != 0 ? ws % wt : static_cast<T>(Unpredictable);
50901cb0ef41Sopenharmony_ci      break;
50911cb0ef41Sopenharmony_ci    case MOD_U:
50921cb0ef41Sopenharmony_ci      res = wt != 0 ? static_cast<T>(static_cast<uT>(ws) % static_cast<uT>(wt))
50931cb0ef41Sopenharmony_ci                    : static_cast<T>(Unpredictable);
50941cb0ef41Sopenharmony_ci      break;
50951cb0ef41Sopenharmony_ci    case DOTP_S:
50961cb0ef41Sopenharmony_ci    case DOTP_U:
50971cb0ef41Sopenharmony_ci    case DPADD_S:
50981cb0ef41Sopenharmony_ci    case DPADD_U:
50991cb0ef41Sopenharmony_ci    case DPSUB_S:
51001cb0ef41Sopenharmony_ci    case DPSUB_U:
51011cb0ef41Sopenharmony_ci    case SLD:
51021cb0ef41Sopenharmony_ci    case SPLAT:
51031cb0ef41Sopenharmony_ci      UNIMPLEMENTED();
51041cb0ef41Sopenharmony_ci      break;
51051cb0ef41Sopenharmony_ci    case SRAR: {
51061cb0ef41Sopenharmony_ci      int bit = wt_modulo == 0 ? 0 : (ws >> (wt_modulo - 1)) & 1;
51071cb0ef41Sopenharmony_ci      res = static_cast<T>(ArithmeticShiftRight(ws, wt_modulo) + bit);
51081cb0ef41Sopenharmony_ci    } break;
51091cb0ef41Sopenharmony_ci    case SRLR: {
51101cb0ef41Sopenharmony_ci      uT wsu = static_cast<uT>(ws);
51111cb0ef41Sopenharmony_ci      int bit = wt_modulo == 0 ? 0 : (wsu >> (wt_modulo - 1)) & 1;
51121cb0ef41Sopenharmony_ci      res = static_cast<T>((wsu >> wt_modulo) + bit);
51131cb0ef41Sopenharmony_ci    } break;
51141cb0ef41Sopenharmony_ci    default:
51151cb0ef41Sopenharmony_ci      UNREACHABLE();
51161cb0ef41Sopenharmony_ci  }
51171cb0ef41Sopenharmony_ci  return res;
51181cb0ef41Sopenharmony_ci}
51191cb0ef41Sopenharmony_ci
51201cb0ef41Sopenharmony_citemplate <typename T_int, typename T_reg>
51211cb0ef41Sopenharmony_civoid Msa3RInstrHelper_shuffle(const uint32_t opcode, T_reg ws, T_reg wt,
51221cb0ef41Sopenharmony_ci                              T_reg wd, const int i, const int num_of_lanes) {
51231cb0ef41Sopenharmony_ci  T_int *ws_p, *wt_p, *wd_p;
51241cb0ef41Sopenharmony_ci  ws_p = reinterpret_cast<T_int*>(ws);
51251cb0ef41Sopenharmony_ci  wt_p = reinterpret_cast<T_int*>(wt);
51261cb0ef41Sopenharmony_ci  wd_p = reinterpret_cast<T_int*>(wd);
51271cb0ef41Sopenharmony_ci  switch (opcode) {
51281cb0ef41Sopenharmony_ci    case PCKEV:
51291cb0ef41Sopenharmony_ci      wd_p[i] = wt_p[2 * i];
51301cb0ef41Sopenharmony_ci      wd_p[i + num_of_lanes / 2] = ws_p[2 * i];
51311cb0ef41Sopenharmony_ci      break;
51321cb0ef41Sopenharmony_ci    case PCKOD:
51331cb0ef41Sopenharmony_ci      wd_p[i] = wt_p[2 * i + 1];
51341cb0ef41Sopenharmony_ci      wd_p[i + num_of_lanes / 2] = ws_p[2 * i + 1];
51351cb0ef41Sopenharmony_ci      break;
51361cb0ef41Sopenharmony_ci    case ILVL:
51371cb0ef41Sopenharmony_ci      wd_p[2 * i] = wt_p[i + num_of_lanes / 2];
51381cb0ef41Sopenharmony_ci      wd_p[2 * i + 1] = ws_p[i + num_of_lanes / 2];
51391cb0ef41Sopenharmony_ci      break;
51401cb0ef41Sopenharmony_ci    case ILVR:
51411cb0ef41Sopenharmony_ci      wd_p[2 * i] = wt_p[i];
51421cb0ef41Sopenharmony_ci      wd_p[2 * i + 1] = ws_p[i];
51431cb0ef41Sopenharmony_ci      break;
51441cb0ef41Sopenharmony_ci    case ILVEV:
51451cb0ef41Sopenharmony_ci      wd_p[2 * i] = wt_p[2 * i];
51461cb0ef41Sopenharmony_ci      wd_p[2 * i + 1] = ws_p[2 * i];
51471cb0ef41Sopenharmony_ci      break;
51481cb0ef41Sopenharmony_ci    case ILVOD:
51491cb0ef41Sopenharmony_ci      wd_p[2 * i] = wt_p[2 * i + 1];
51501cb0ef41Sopenharmony_ci      wd_p[2 * i + 1] = ws_p[2 * i + 1];
51511cb0ef41Sopenharmony_ci      break;
51521cb0ef41Sopenharmony_ci    case VSHF: {
51531cb0ef41Sopenharmony_ci      const int mask_not_valid = 0xC0;
51541cb0ef41Sopenharmony_ci      const int mask_6_bits = 0x3F;
51551cb0ef41Sopenharmony_ci      if ((wd_p[i] & mask_not_valid)) {
51561cb0ef41Sopenharmony_ci        wd_p[i] = 0;
51571cb0ef41Sopenharmony_ci      } else {
51581cb0ef41Sopenharmony_ci        int k = (wd_p[i] & mask_6_bits) % (num_of_lanes * 2);
51591cb0ef41Sopenharmony_ci        wd_p[i] = k >= num_of_lanes ? ws_p[k - num_of_lanes] : wt_p[k];
51601cb0ef41Sopenharmony_ci      }
51611cb0ef41Sopenharmony_ci    } break;
51621cb0ef41Sopenharmony_ci    default:
51631cb0ef41Sopenharmony_ci      UNREACHABLE();
51641cb0ef41Sopenharmony_ci  }
51651cb0ef41Sopenharmony_ci}
51661cb0ef41Sopenharmony_ci
51671cb0ef41Sopenharmony_citemplate <typename T_int, typename T_smaller_int, typename T_reg>
51681cb0ef41Sopenharmony_civoid Msa3RInstrHelper_horizontal(const uint32_t opcode, T_reg ws, T_reg wt,
51691cb0ef41Sopenharmony_ci                                 T_reg wd, const int i,
51701cb0ef41Sopenharmony_ci                                 const int num_of_lanes) {
51711cb0ef41Sopenharmony_ci  using T_uint = typename std::make_unsigned<T_int>::type;
51721cb0ef41Sopenharmony_ci  using T_smaller_uint = typename std::make_unsigned<T_smaller_int>::type;
51731cb0ef41Sopenharmony_ci  T_int* wd_p;
51741cb0ef41Sopenharmony_ci  T_smaller_int *ws_p, *wt_p;
51751cb0ef41Sopenharmony_ci  ws_p = reinterpret_cast<T_smaller_int*>(ws);
51761cb0ef41Sopenharmony_ci  wt_p = reinterpret_cast<T_smaller_int*>(wt);
51771cb0ef41Sopenharmony_ci  wd_p = reinterpret_cast<T_int*>(wd);
51781cb0ef41Sopenharmony_ci  T_uint* wd_pu;
51791cb0ef41Sopenharmony_ci  T_smaller_uint *ws_pu, *wt_pu;
51801cb0ef41Sopenharmony_ci  ws_pu = reinterpret_cast<T_smaller_uint*>(ws);
51811cb0ef41Sopenharmony_ci  wt_pu = reinterpret_cast<T_smaller_uint*>(wt);
51821cb0ef41Sopenharmony_ci  wd_pu = reinterpret_cast<T_uint*>(wd);
51831cb0ef41Sopenharmony_ci  switch (opcode) {
51841cb0ef41Sopenharmony_ci    case HADD_S:
51851cb0ef41Sopenharmony_ci      wd_p[i] =
51861cb0ef41Sopenharmony_ci          static_cast<T_int>(ws_p[2 * i + 1]) + static_cast<T_int>(wt_p[2 * i]);
51871cb0ef41Sopenharmony_ci      break;
51881cb0ef41Sopenharmony_ci    case HADD_U:
51891cb0ef41Sopenharmony_ci      wd_pu[i] = static_cast<T_uint>(ws_pu[2 * i + 1]) +
51901cb0ef41Sopenharmony_ci                 static_cast<T_uint>(wt_pu[2 * i]);
51911cb0ef41Sopenharmony_ci      break;
51921cb0ef41Sopenharmony_ci    case HSUB_S:
51931cb0ef41Sopenharmony_ci      wd_p[i] =
51941cb0ef41Sopenharmony_ci          static_cast<T_int>(ws_p[2 * i + 1]) - static_cast<T_int>(wt_p[2 * i]);
51951cb0ef41Sopenharmony_ci      break;
51961cb0ef41Sopenharmony_ci    case HSUB_U:
51971cb0ef41Sopenharmony_ci      wd_pu[i] = static_cast<T_uint>(ws_pu[2 * i + 1]) -
51981cb0ef41Sopenharmony_ci                 static_cast<T_uint>(wt_pu[2 * i]);
51991cb0ef41Sopenharmony_ci      break;
52001cb0ef41Sopenharmony_ci    default:
52011cb0ef41Sopenharmony_ci      UNREACHABLE();
52021cb0ef41Sopenharmony_ci  }
52031cb0ef41Sopenharmony_ci}
52041cb0ef41Sopenharmony_ci
52051cb0ef41Sopenharmony_civoid Simulator::DecodeTypeMsa3R() {
52061cb0ef41Sopenharmony_ci  DCHECK(IsMipsArchVariant(kMips32r6));
52071cb0ef41Sopenharmony_ci  DCHECK(CpuFeatures::IsSupported(MIPS_SIMD));
52081cb0ef41Sopenharmony_ci  uint32_t opcode = instr_.InstructionBits() & kMsa3RMask;
52091cb0ef41Sopenharmony_ci  msa_reg_t ws, wd, wt;
52101cb0ef41Sopenharmony_ci  get_msa_register(ws_reg(), &ws);
52111cb0ef41Sopenharmony_ci  get_msa_register(wt_reg(), &wt);
52121cb0ef41Sopenharmony_ci  get_msa_register(wd_reg(), &wd);
52131cb0ef41Sopenharmony_ci  switch (opcode) {
52141cb0ef41Sopenharmony_ci    case HADD_S:
52151cb0ef41Sopenharmony_ci    case HADD_U:
52161cb0ef41Sopenharmony_ci    case HSUB_S:
52171cb0ef41Sopenharmony_ci    case HSUB_U:
52181cb0ef41Sopenharmony_ci#define HORIZONTAL_ARITHMETIC_DF(num_of_lanes, int_type, lesser_int_type) \
52191cb0ef41Sopenharmony_ci  for (int i = 0; i < num_of_lanes; ++i) {                                \
52201cb0ef41Sopenharmony_ci    Msa3RInstrHelper_horizontal<int_type, lesser_int_type>(               \
52211cb0ef41Sopenharmony_ci        opcode, &ws, &wt, &wd, i, num_of_lanes);                          \
52221cb0ef41Sopenharmony_ci  }
52231cb0ef41Sopenharmony_ci      switch (DecodeMsaDataFormat()) {
52241cb0ef41Sopenharmony_ci        case MSA_HALF:
52251cb0ef41Sopenharmony_ci          HORIZONTAL_ARITHMETIC_DF(kMSALanesHalf, int16_t, int8_t);
52261cb0ef41Sopenharmony_ci          break;
52271cb0ef41Sopenharmony_ci        case MSA_WORD:
52281cb0ef41Sopenharmony_ci          HORIZONTAL_ARITHMETIC_DF(kMSALanesWord, int32_t, int16_t);
52291cb0ef41Sopenharmony_ci          break;
52301cb0ef41Sopenharmony_ci        case MSA_DWORD:
52311cb0ef41Sopenharmony_ci          HORIZONTAL_ARITHMETIC_DF(kMSALanesDword, int64_t, int32_t);
52321cb0ef41Sopenharmony_ci          break;
52331cb0ef41Sopenharmony_ci        default:
52341cb0ef41Sopenharmony_ci          UNREACHABLE();
52351cb0ef41Sopenharmony_ci      }
52361cb0ef41Sopenharmony_ci      break;
52371cb0ef41Sopenharmony_ci#undef HORIZONTAL_ARITHMETIC_DF
52381cb0ef41Sopenharmony_ci    case VSHF:
52391cb0ef41Sopenharmony_ci#define VSHF_DF(num_of_lanes, int_type)                          \
52401cb0ef41Sopenharmony_ci  for (int i = 0; i < num_of_lanes; ++i) {                       \
52411cb0ef41Sopenharmony_ci    Msa3RInstrHelper_shuffle<int_type>(opcode, &ws, &wt, &wd, i, \
52421cb0ef41Sopenharmony_ci                                       num_of_lanes);            \
52431cb0ef41Sopenharmony_ci  }
52441cb0ef41Sopenharmony_ci      switch (DecodeMsaDataFormat()) {
52451cb0ef41Sopenharmony_ci        case MSA_BYTE:
52461cb0ef41Sopenharmony_ci          VSHF_DF(kMSALanesByte, int8_t);
52471cb0ef41Sopenharmony_ci          break;
52481cb0ef41Sopenharmony_ci        case MSA_HALF:
52491cb0ef41Sopenharmony_ci          VSHF_DF(kMSALanesHalf, int16_t);
52501cb0ef41Sopenharmony_ci          break;
52511cb0ef41Sopenharmony_ci        case MSA_WORD:
52521cb0ef41Sopenharmony_ci          VSHF_DF(kMSALanesWord, int32_t);
52531cb0ef41Sopenharmony_ci          break;
52541cb0ef41Sopenharmony_ci        case MSA_DWORD:
52551cb0ef41Sopenharmony_ci          VSHF_DF(kMSALanesDword, int64_t);
52561cb0ef41Sopenharmony_ci          break;
52571cb0ef41Sopenharmony_ci        default:
52581cb0ef41Sopenharmony_ci          UNREACHABLE();
52591cb0ef41Sopenharmony_ci      }
52601cb0ef41Sopenharmony_ci#undef VSHF_DF
52611cb0ef41Sopenharmony_ci      break;
52621cb0ef41Sopenharmony_ci    case PCKEV:
52631cb0ef41Sopenharmony_ci    case PCKOD:
52641cb0ef41Sopenharmony_ci    case ILVL:
52651cb0ef41Sopenharmony_ci    case ILVR:
52661cb0ef41Sopenharmony_ci    case ILVEV:
52671cb0ef41Sopenharmony_ci    case ILVOD:
52681cb0ef41Sopenharmony_ci#define INTERLEAVE_PACK_DF(num_of_lanes, int_type)               \
52691cb0ef41Sopenharmony_ci  for (int i = 0; i < num_of_lanes / 2; ++i) {                   \
52701cb0ef41Sopenharmony_ci    Msa3RInstrHelper_shuffle<int_type>(opcode, &ws, &wt, &wd, i, \
52711cb0ef41Sopenharmony_ci                                       num_of_lanes);            \
52721cb0ef41Sopenharmony_ci  }
52731cb0ef41Sopenharmony_ci      switch (DecodeMsaDataFormat()) {
52741cb0ef41Sopenharmony_ci        case MSA_BYTE:
52751cb0ef41Sopenharmony_ci          INTERLEAVE_PACK_DF(kMSALanesByte, int8_t);
52761cb0ef41Sopenharmony_ci          break;
52771cb0ef41Sopenharmony_ci        case MSA_HALF:
52781cb0ef41Sopenharmony_ci          INTERLEAVE_PACK_DF(kMSALanesHalf, int16_t);
52791cb0ef41Sopenharmony_ci          break;
52801cb0ef41Sopenharmony_ci        case MSA_WORD:
52811cb0ef41Sopenharmony_ci          INTERLEAVE_PACK_DF(kMSALanesWord, int32_t);
52821cb0ef41Sopenharmony_ci          break;
52831cb0ef41Sopenharmony_ci        case MSA_DWORD:
52841cb0ef41Sopenharmony_ci          INTERLEAVE_PACK_DF(kMSALanesDword, int64_t);
52851cb0ef41Sopenharmony_ci          break;
52861cb0ef41Sopenharmony_ci        default:
52871cb0ef41Sopenharmony_ci          UNREACHABLE();
52881cb0ef41Sopenharmony_ci      }
52891cb0ef41Sopenharmony_ci      break;
52901cb0ef41Sopenharmony_ci#undef INTERLEAVE_PACK_DF
52911cb0ef41Sopenharmony_ci    default:
52921cb0ef41Sopenharmony_ci#define MSA_3R_DF(elem, num_of_lanes)                                          \
52931cb0ef41Sopenharmony_ci  for (int i = 0; i < num_of_lanes; i++) {                                     \
52941cb0ef41Sopenharmony_ci    wd.elem[i] = Msa3RInstrHelper(opcode, wd.elem[i], ws.elem[i], wt.elem[i]); \
52951cb0ef41Sopenharmony_ci  }
52961cb0ef41Sopenharmony_ci
52971cb0ef41Sopenharmony_ci      switch (DecodeMsaDataFormat()) {
52981cb0ef41Sopenharmony_ci        case MSA_BYTE:
52991cb0ef41Sopenharmony_ci          MSA_3R_DF(b, kMSALanesByte);
53001cb0ef41Sopenharmony_ci          break;
53011cb0ef41Sopenharmony_ci        case MSA_HALF:
53021cb0ef41Sopenharmony_ci          MSA_3R_DF(h, kMSALanesHalf);
53031cb0ef41Sopenharmony_ci          break;
53041cb0ef41Sopenharmony_ci        case MSA_WORD:
53051cb0ef41Sopenharmony_ci          MSA_3R_DF(w, kMSALanesWord);
53061cb0ef41Sopenharmony_ci          break;
53071cb0ef41Sopenharmony_ci        case MSA_DWORD:
53081cb0ef41Sopenharmony_ci          MSA_3R_DF(d, kMSALanesDword);
53091cb0ef41Sopenharmony_ci          break;
53101cb0ef41Sopenharmony_ci        default:
53111cb0ef41Sopenharmony_ci          UNREACHABLE();
53121cb0ef41Sopenharmony_ci      }
53131cb0ef41Sopenharmony_ci#undef MSA_3R_DF
53141cb0ef41Sopenharmony_ci      break;
53151cb0ef41Sopenharmony_ci  }
53161cb0ef41Sopenharmony_ci  set_msa_register(wd_reg(), &wd);
53171cb0ef41Sopenharmony_ci  TraceMSARegWr(&wd);
53181cb0ef41Sopenharmony_ci}
53191cb0ef41Sopenharmony_ci
53201cb0ef41Sopenharmony_citemplate <typename T_int, typename T_fp, typename T_reg>
53211cb0ef41Sopenharmony_civoid Msa3RFInstrHelper(uint32_t opcode, T_reg ws, T_reg wt, T_reg* wd) {
53221cb0ef41Sopenharmony_ci  const T_int all_ones = static_cast<T_int>(-1);
53231cb0ef41Sopenharmony_ci  const T_fp s_element = *reinterpret_cast<T_fp*>(&ws);
53241cb0ef41Sopenharmony_ci  const T_fp t_element = *reinterpret_cast<T_fp*>(&wt);
53251cb0ef41Sopenharmony_ci  switch (opcode) {
53261cb0ef41Sopenharmony_ci    case FCUN: {
53271cb0ef41Sopenharmony_ci      if (std::isnan(s_element) || std::isnan(t_element)) {
53281cb0ef41Sopenharmony_ci        *wd = all_ones;
53291cb0ef41Sopenharmony_ci      } else {
53301cb0ef41Sopenharmony_ci        *wd = 0;
53311cb0ef41Sopenharmony_ci      }
53321cb0ef41Sopenharmony_ci    } break;
53331cb0ef41Sopenharmony_ci    case FCEQ: {
53341cb0ef41Sopenharmony_ci      if (s_element != t_element || std::isnan(s_element) ||
53351cb0ef41Sopenharmony_ci          std::isnan(t_element)) {
53361cb0ef41Sopenharmony_ci        *wd = 0;
53371cb0ef41Sopenharmony_ci      } else {
53381cb0ef41Sopenharmony_ci        *wd = all_ones;
53391cb0ef41Sopenharmony_ci      }
53401cb0ef41Sopenharmony_ci    } break;
53411cb0ef41Sopenharmony_ci    case FCUEQ: {
53421cb0ef41Sopenharmony_ci      if (s_element == t_element || std::isnan(s_element) ||
53431cb0ef41Sopenharmony_ci          std::isnan(t_element)) {
53441cb0ef41Sopenharmony_ci        *wd = all_ones;
53451cb0ef41Sopenharmony_ci      } else {
53461cb0ef41Sopenharmony_ci        *wd = 0;
53471cb0ef41Sopenharmony_ci      }
53481cb0ef41Sopenharmony_ci    } break;
53491cb0ef41Sopenharmony_ci    case FCLT: {
53501cb0ef41Sopenharmony_ci      if (s_element >= t_element || std::isnan(s_element) ||
53511cb0ef41Sopenharmony_ci          std::isnan(t_element)) {
53521cb0ef41Sopenharmony_ci        *wd = 0;
53531cb0ef41Sopenharmony_ci      } else {
53541cb0ef41Sopenharmony_ci        *wd = all_ones;
53551cb0ef41Sopenharmony_ci      }
53561cb0ef41Sopenharmony_ci    } break;
53571cb0ef41Sopenharmony_ci    case FCULT: {
53581cb0ef41Sopenharmony_ci      if (s_element < t_element || std::isnan(s_element) ||
53591cb0ef41Sopenharmony_ci          std::isnan(t_element)) {
53601cb0ef41Sopenharmony_ci        *wd = all_ones;
53611cb0ef41Sopenharmony_ci      } else {
53621cb0ef41Sopenharmony_ci        *wd = 0;
53631cb0ef41Sopenharmony_ci      }
53641cb0ef41Sopenharmony_ci    } break;
53651cb0ef41Sopenharmony_ci    case FCLE: {
53661cb0ef41Sopenharmony_ci      if (s_element > t_element || std::isnan(s_element) ||
53671cb0ef41Sopenharmony_ci          std::isnan(t_element)) {
53681cb0ef41Sopenharmony_ci        *wd = 0;
53691cb0ef41Sopenharmony_ci      } else {
53701cb0ef41Sopenharmony_ci        *wd = all_ones;
53711cb0ef41Sopenharmony_ci      }
53721cb0ef41Sopenharmony_ci    } break;
53731cb0ef41Sopenharmony_ci    case FCULE: {
53741cb0ef41Sopenharmony_ci      if (s_element <= t_element || std::isnan(s_element) ||
53751cb0ef41Sopenharmony_ci          std::isnan(t_element)) {
53761cb0ef41Sopenharmony_ci        *wd = all_ones;
53771cb0ef41Sopenharmony_ci      } else {
53781cb0ef41Sopenharmony_ci        *wd = 0;
53791cb0ef41Sopenharmony_ci      }
53801cb0ef41Sopenharmony_ci    } break;
53811cb0ef41Sopenharmony_ci    case FCOR: {
53821cb0ef41Sopenharmony_ci      if (std::isnan(s_element) || std::isnan(t_element)) {
53831cb0ef41Sopenharmony_ci        *wd = 0;
53841cb0ef41Sopenharmony_ci      } else {
53851cb0ef41Sopenharmony_ci        *wd = all_ones;
53861cb0ef41Sopenharmony_ci      }
53871cb0ef41Sopenharmony_ci    } break;
53881cb0ef41Sopenharmony_ci    case FCUNE: {
53891cb0ef41Sopenharmony_ci      if (s_element != t_element || std::isnan(s_element) ||
53901cb0ef41Sopenharmony_ci          std::isnan(t_element)) {
53911cb0ef41Sopenharmony_ci        *wd = all_ones;
53921cb0ef41Sopenharmony_ci      } else {
53931cb0ef41Sopenharmony_ci        *wd = 0;
53941cb0ef41Sopenharmony_ci      }
53951cb0ef41Sopenharmony_ci    } break;
53961cb0ef41Sopenharmony_ci    case FCNE: {
53971cb0ef41Sopenharmony_ci      if (s_element == t_element || std::isnan(s_element) ||
53981cb0ef41Sopenharmony_ci          std::isnan(t_element)) {
53991cb0ef41Sopenharmony_ci        *wd = 0;
54001cb0ef41Sopenharmony_ci      } else {
54011cb0ef41Sopenharmony_ci        *wd = all_ones;
54021cb0ef41Sopenharmony_ci      }
54031cb0ef41Sopenharmony_ci    } break;
54041cb0ef41Sopenharmony_ci    case FADD:
54051cb0ef41Sopenharmony_ci      *wd = bit_cast<T_int>(s_element + t_element);
54061cb0ef41Sopenharmony_ci      break;
54071cb0ef41Sopenharmony_ci    case FSUB:
54081cb0ef41Sopenharmony_ci      *wd = bit_cast<T_int>(s_element - t_element);
54091cb0ef41Sopenharmony_ci      break;
54101cb0ef41Sopenharmony_ci    case FMUL:
54111cb0ef41Sopenharmony_ci      *wd = bit_cast<T_int>(s_element * t_element);
54121cb0ef41Sopenharmony_ci      break;
54131cb0ef41Sopenharmony_ci    case FDIV: {
54141cb0ef41Sopenharmony_ci      if (t_element == 0) {
54151cb0ef41Sopenharmony_ci        *wd = bit_cast<T_int>(std::numeric_limits<T_fp>::quiet_NaN());
54161cb0ef41Sopenharmony_ci      } else {
54171cb0ef41Sopenharmony_ci        *wd = bit_cast<T_int>(s_element / t_element);
54181cb0ef41Sopenharmony_ci      }
54191cb0ef41Sopenharmony_ci    } break;
54201cb0ef41Sopenharmony_ci    case FMADD:
54211cb0ef41Sopenharmony_ci      *wd = bit_cast<T_int>(
54221cb0ef41Sopenharmony_ci          std::fma(s_element, t_element, *reinterpret_cast<T_fp*>(wd)));
54231cb0ef41Sopenharmony_ci      break;
54241cb0ef41Sopenharmony_ci    case FMSUB:
54251cb0ef41Sopenharmony_ci      *wd = bit_cast<T_int>(
54261cb0ef41Sopenharmony_ci          std::fma(s_element, -t_element, *reinterpret_cast<T_fp*>(wd)));
54271cb0ef41Sopenharmony_ci      break;
54281cb0ef41Sopenharmony_ci    case FEXP2:
54291cb0ef41Sopenharmony_ci      *wd = bit_cast<T_int>(std::ldexp(s_element, static_cast<int>(wt)));
54301cb0ef41Sopenharmony_ci      break;
54311cb0ef41Sopenharmony_ci    case FMIN:
54321cb0ef41Sopenharmony_ci      *wd = bit_cast<T_int>(std::min(s_element, t_element));
54331cb0ef41Sopenharmony_ci      break;
54341cb0ef41Sopenharmony_ci    case FMAX:
54351cb0ef41Sopenharmony_ci      *wd = bit_cast<T_int>(std::max(s_element, t_element));
54361cb0ef41Sopenharmony_ci      break;
54371cb0ef41Sopenharmony_ci    case FMIN_A: {
54381cb0ef41Sopenharmony_ci      *wd = bit_cast<T_int>(
54391cb0ef41Sopenharmony_ci          std::fabs(s_element) < std::fabs(t_element) ? s_element : t_element);
54401cb0ef41Sopenharmony_ci    } break;
54411cb0ef41Sopenharmony_ci    case FMAX_A: {
54421cb0ef41Sopenharmony_ci      *wd = bit_cast<T_int>(
54431cb0ef41Sopenharmony_ci          std::fabs(s_element) > std::fabs(t_element) ? s_element : t_element);
54441cb0ef41Sopenharmony_ci    } break;
54451cb0ef41Sopenharmony_ci    case FSOR:
54461cb0ef41Sopenharmony_ci    case FSUNE:
54471cb0ef41Sopenharmony_ci    case FSNE:
54481cb0ef41Sopenharmony_ci    case FSAF:
54491cb0ef41Sopenharmony_ci    case FSUN:
54501cb0ef41Sopenharmony_ci    case FSEQ:
54511cb0ef41Sopenharmony_ci    case FSUEQ:
54521cb0ef41Sopenharmony_ci    case FSLT:
54531cb0ef41Sopenharmony_ci    case FSULT:
54541cb0ef41Sopenharmony_ci    case FSLE:
54551cb0ef41Sopenharmony_ci    case FSULE:
54561cb0ef41Sopenharmony_ci      UNIMPLEMENTED();
54571cb0ef41Sopenharmony_ci      break;
54581cb0ef41Sopenharmony_ci    default:
54591cb0ef41Sopenharmony_ci      UNREACHABLE();
54601cb0ef41Sopenharmony_ci  }
54611cb0ef41Sopenharmony_ci}
54621cb0ef41Sopenharmony_ci
54631cb0ef41Sopenharmony_citemplate <typename T_int, typename T_int_dbl, typename T_reg>
54641cb0ef41Sopenharmony_civoid Msa3RFInstrHelper2(uint32_t opcode, T_reg ws, T_reg wt, T_reg* wd) {
54651cb0ef41Sopenharmony_ci  //  using T_uint = typename std::make_unsigned<T_int>::type;
54661cb0ef41Sopenharmony_ci  using T_uint_dbl = typename std::make_unsigned<T_int_dbl>::type;
54671cb0ef41Sopenharmony_ci  const T_int max_int = std::numeric_limits<T_int>::max();
54681cb0ef41Sopenharmony_ci  const T_int min_int = std::numeric_limits<T_int>::min();
54691cb0ef41Sopenharmony_ci  const int shift = kBitsPerByte * sizeof(T_int) - 1;
54701cb0ef41Sopenharmony_ci  const T_int_dbl reg_s = ws;
54711cb0ef41Sopenharmony_ci  const T_int_dbl reg_t = wt;
54721cb0ef41Sopenharmony_ci  T_int_dbl product, result;
54731cb0ef41Sopenharmony_ci  product = reg_s * reg_t;
54741cb0ef41Sopenharmony_ci  switch (opcode) {
54751cb0ef41Sopenharmony_ci    case MUL_Q: {
54761cb0ef41Sopenharmony_ci      const T_int_dbl min_fix_dbl =
54771cb0ef41Sopenharmony_ci          bit_cast<T_uint_dbl>(std::numeric_limits<T_int_dbl>::min()) >> 1U;
54781cb0ef41Sopenharmony_ci      const T_int_dbl max_fix_dbl = std::numeric_limits<T_int_dbl>::max() >> 1U;
54791cb0ef41Sopenharmony_ci      if (product == min_fix_dbl) {
54801cb0ef41Sopenharmony_ci        product = max_fix_dbl;
54811cb0ef41Sopenharmony_ci      }
54821cb0ef41Sopenharmony_ci      *wd = static_cast<T_int>(product >> shift);
54831cb0ef41Sopenharmony_ci    } break;
54841cb0ef41Sopenharmony_ci    case MADD_Q: {
54851cb0ef41Sopenharmony_ci      result = (product + (static_cast<T_int_dbl>(*wd) << shift)) >> shift;
54861cb0ef41Sopenharmony_ci      *wd = static_cast<T_int>(
54871cb0ef41Sopenharmony_ci          result > max_int ? max_int : result < min_int ? min_int : result);
54881cb0ef41Sopenharmony_ci    } break;
54891cb0ef41Sopenharmony_ci    case MSUB_Q: {
54901cb0ef41Sopenharmony_ci      result = (-product + (static_cast<T_int_dbl>(*wd) << shift)) >> shift;
54911cb0ef41Sopenharmony_ci      *wd = static_cast<T_int>(
54921cb0ef41Sopenharmony_ci          result > max_int ? max_int : result < min_int ? min_int : result);
54931cb0ef41Sopenharmony_ci    } break;
54941cb0ef41Sopenharmony_ci    case MULR_Q: {
54951cb0ef41Sopenharmony_ci      const T_int_dbl min_fix_dbl =
54961cb0ef41Sopenharmony_ci          bit_cast<T_uint_dbl>(std::numeric_limits<T_int_dbl>::min()) >> 1U;
54971cb0ef41Sopenharmony_ci      const T_int_dbl max_fix_dbl = std::numeric_limits<T_int_dbl>::max() >> 1U;
54981cb0ef41Sopenharmony_ci      if (product == min_fix_dbl) {
54991cb0ef41Sopenharmony_ci        *wd = static_cast<T_int>(max_fix_dbl >> shift);
55001cb0ef41Sopenharmony_ci        break;
55011cb0ef41Sopenharmony_ci      }
55021cb0ef41Sopenharmony_ci      *wd = static_cast<T_int>((product + (1 << (shift - 1))) >> shift);
55031cb0ef41Sopenharmony_ci    } break;
55041cb0ef41Sopenharmony_ci    case MADDR_Q: {
55051cb0ef41Sopenharmony_ci      result = (product + (static_cast<T_int_dbl>(*wd) << shift) +
55061cb0ef41Sopenharmony_ci                (1 << (shift - 1))) >>
55071cb0ef41Sopenharmony_ci               shift;
55081cb0ef41Sopenharmony_ci      *wd = static_cast<T_int>(
55091cb0ef41Sopenharmony_ci          result > max_int ? max_int : result < min_int ? min_int : result);
55101cb0ef41Sopenharmony_ci    } break;
55111cb0ef41Sopenharmony_ci    case MSUBR_Q: {
55121cb0ef41Sopenharmony_ci      result = (-product + (static_cast<T_int_dbl>(*wd) << shift) +
55131cb0ef41Sopenharmony_ci                (1 << (shift - 1))) >>
55141cb0ef41Sopenharmony_ci               shift;
55151cb0ef41Sopenharmony_ci      *wd = static_cast<T_int>(
55161cb0ef41Sopenharmony_ci          result > max_int ? max_int : result < min_int ? min_int : result);
55171cb0ef41Sopenharmony_ci    } break;
55181cb0ef41Sopenharmony_ci    default:
55191cb0ef41Sopenharmony_ci      UNREACHABLE();
55201cb0ef41Sopenharmony_ci  }
55211cb0ef41Sopenharmony_ci}
55221cb0ef41Sopenharmony_ci
55231cb0ef41Sopenharmony_civoid Simulator::DecodeTypeMsa3RF() {
55241cb0ef41Sopenharmony_ci  DCHECK(IsMipsArchVariant(kMips32r6));
55251cb0ef41Sopenharmony_ci  DCHECK(CpuFeatures::IsSupported(MIPS_SIMD));
55261cb0ef41Sopenharmony_ci  uint32_t opcode = instr_.InstructionBits() & kMsa3RFMask;
55271cb0ef41Sopenharmony_ci  msa_reg_t wd, ws, wt;
55281cb0ef41Sopenharmony_ci  if (opcode != FCAF) {
55291cb0ef41Sopenharmony_ci    get_msa_register(ws_reg(), &ws);
55301cb0ef41Sopenharmony_ci    get_msa_register(wt_reg(), &wt);
55311cb0ef41Sopenharmony_ci  }
55321cb0ef41Sopenharmony_ci  switch (opcode) {
55331cb0ef41Sopenharmony_ci    case FCAF:
55341cb0ef41Sopenharmony_ci      wd.d[0] = 0;
55351cb0ef41Sopenharmony_ci      wd.d[1] = 0;
55361cb0ef41Sopenharmony_ci      break;
55371cb0ef41Sopenharmony_ci    case FEXDO:
55381cb0ef41Sopenharmony_ci#define PACK_FLOAT16(sign, exp, frac) \
55391cb0ef41Sopenharmony_ci  static_cast<uint16_t>(((sign) << 15) + ((exp) << 10) + (frac))
55401cb0ef41Sopenharmony_ci#define FEXDO_DF(source, dst)                                        \
55411cb0ef41Sopenharmony_ci  do {                                                               \
55421cb0ef41Sopenharmony_ci    element = source;                                                \
55431cb0ef41Sopenharmony_ci    aSign = element >> 31;                                           \
55441cb0ef41Sopenharmony_ci    aExp = element >> 23 & 0xFF;                                     \
55451cb0ef41Sopenharmony_ci    aFrac = element & 0x007FFFFF;                                    \
55461cb0ef41Sopenharmony_ci    if (aExp == 0xFF) {                                              \
55471cb0ef41Sopenharmony_ci      if (aFrac) {                                                   \
55481cb0ef41Sopenharmony_ci        /* Input is a NaN */                                         \
55491cb0ef41Sopenharmony_ci        dst = 0x7DFFU;                                               \
55501cb0ef41Sopenharmony_ci        break;                                                       \
55511cb0ef41Sopenharmony_ci      }                                                              \
55521cb0ef41Sopenharmony_ci      /* Infinity */                                                 \
55531cb0ef41Sopenharmony_ci      dst = PACK_FLOAT16(aSign, 0x1F, 0);                            \
55541cb0ef41Sopenharmony_ci      break;                                                         \
55551cb0ef41Sopenharmony_ci    } else if (aExp == 0 && aFrac == 0) {                            \
55561cb0ef41Sopenharmony_ci      dst = PACK_FLOAT16(aSign, 0, 0);                               \
55571cb0ef41Sopenharmony_ci      break;                                                         \
55581cb0ef41Sopenharmony_ci    } else {                                                         \
55591cb0ef41Sopenharmony_ci      int maxexp = 29;                                               \
55601cb0ef41Sopenharmony_ci      uint32_t mask;                                                 \
55611cb0ef41Sopenharmony_ci      uint32_t increment;                                            \
55621cb0ef41Sopenharmony_ci      bool rounding_bumps_exp;                                       \
55631cb0ef41Sopenharmony_ci      aFrac |= 0x00800000;                                           \
55641cb0ef41Sopenharmony_ci      aExp -= 0x71;                                                  \
55651cb0ef41Sopenharmony_ci      if (aExp < 1) {                                                \
55661cb0ef41Sopenharmony_ci        /* Will be denormal in halfprec */                           \
55671cb0ef41Sopenharmony_ci        mask = 0x00FFFFFF;                                           \
55681cb0ef41Sopenharmony_ci        if (aExp >= -11) {                                           \
55691cb0ef41Sopenharmony_ci          mask >>= 11 + aExp;                                        \
55701cb0ef41Sopenharmony_ci        }                                                            \
55711cb0ef41Sopenharmony_ci      } else {                                                       \
55721cb0ef41Sopenharmony_ci        /* Normal number in halfprec */                              \
55731cb0ef41Sopenharmony_ci        mask = 0x00001FFF;                                           \
55741cb0ef41Sopenharmony_ci      }                                                              \
55751cb0ef41Sopenharmony_ci      switch (MSACSR_ & 3) {                                         \
55761cb0ef41Sopenharmony_ci        case kRoundToNearest:                                        \
55771cb0ef41Sopenharmony_ci          increment = (mask + 1) >> 1;                               \
55781cb0ef41Sopenharmony_ci          if ((aFrac & mask) == increment) {                         \
55791cb0ef41Sopenharmony_ci            increment = aFrac & (increment << 1);                    \
55801cb0ef41Sopenharmony_ci          }                                                          \
55811cb0ef41Sopenharmony_ci          break;                                                     \
55821cb0ef41Sopenharmony_ci        case kRoundToPlusInf:                                        \
55831cb0ef41Sopenharmony_ci          increment = aSign ? 0 : mask;                              \
55841cb0ef41Sopenharmony_ci          break;                                                     \
55851cb0ef41Sopenharmony_ci        case kRoundToMinusInf:                                       \
55861cb0ef41Sopenharmony_ci          increment = aSign ? mask : 0;                              \
55871cb0ef41Sopenharmony_ci          break;                                                     \
55881cb0ef41Sopenharmony_ci        case kRoundToZero:                                           \
55891cb0ef41Sopenharmony_ci          increment = 0;                                             \
55901cb0ef41Sopenharmony_ci          break;                                                     \
55911cb0ef41Sopenharmony_ci      }                                                              \
55921cb0ef41Sopenharmony_ci      rounding_bumps_exp = (aFrac + increment >= 0x01000000);        \
55931cb0ef41Sopenharmony_ci      if (aExp > maxexp || (aExp == maxexp && rounding_bumps_exp)) { \
55941cb0ef41Sopenharmony_ci        dst = PACK_FLOAT16(aSign, 0x1F, 0);                          \
55951cb0ef41Sopenharmony_ci        break;                                                       \
55961cb0ef41Sopenharmony_ci      }                                                              \
55971cb0ef41Sopenharmony_ci      aFrac += increment;                                            \
55981cb0ef41Sopenharmony_ci      if (rounding_bumps_exp) {                                      \
55991cb0ef41Sopenharmony_ci        aFrac >>= 1;                                                 \
56001cb0ef41Sopenharmony_ci        aExp++;                                                      \
56011cb0ef41Sopenharmony_ci      }                                                              \
56021cb0ef41Sopenharmony_ci      if (aExp < -10) {                                              \
56031cb0ef41Sopenharmony_ci        dst = PACK_FLOAT16(aSign, 0, 0);                             \
56041cb0ef41Sopenharmony_ci        break;                                                       \
56051cb0ef41Sopenharmony_ci      }                                                              \
56061cb0ef41Sopenharmony_ci      if (aExp < 0) {                                                \
56071cb0ef41Sopenharmony_ci        aFrac >>= -aExp;                                             \
56081cb0ef41Sopenharmony_ci        aExp = 0;                                                    \
56091cb0ef41Sopenharmony_ci      }                                                              \
56101cb0ef41Sopenharmony_ci      dst = PACK_FLOAT16(aSign, aExp, aFrac >> 13);                  \
56111cb0ef41Sopenharmony_ci    }                                                                \
56121cb0ef41Sopenharmony_ci  } while (0);
56131cb0ef41Sopenharmony_ci      switch (DecodeMsaDataFormat()) {
56141cb0ef41Sopenharmony_ci        case MSA_HALF:
56151cb0ef41Sopenharmony_ci          for (int i = 0; i < kMSALanesWord; i++) {
56161cb0ef41Sopenharmony_ci            uint_fast32_t element;
56171cb0ef41Sopenharmony_ci            uint_fast32_t aSign, aFrac;
56181cb0ef41Sopenharmony_ci            int_fast32_t aExp;
56191cb0ef41Sopenharmony_ci            FEXDO_DF(ws.uw[i], wd.uh[i + kMSALanesHalf / 2])
56201cb0ef41Sopenharmony_ci            FEXDO_DF(wt.uw[i], wd.uh[i])
56211cb0ef41Sopenharmony_ci          }
56221cb0ef41Sopenharmony_ci          break;
56231cb0ef41Sopenharmony_ci        case MSA_WORD:
56241cb0ef41Sopenharmony_ci          for (int i = 0; i < kMSALanesDword; i++) {
56251cb0ef41Sopenharmony_ci            wd.w[i + kMSALanesWord / 2] = bit_cast<int32_t>(
56261cb0ef41Sopenharmony_ci                static_cast<float>(bit_cast<double>(ws.d[i])));
56271cb0ef41Sopenharmony_ci            wd.w[i] = bit_cast<int32_t>(
56281cb0ef41Sopenharmony_ci                static_cast<float>(bit_cast<double>(wt.d[i])));
56291cb0ef41Sopenharmony_ci          }
56301cb0ef41Sopenharmony_ci          break;
56311cb0ef41Sopenharmony_ci        default:
56321cb0ef41Sopenharmony_ci          UNREACHABLE();
56331cb0ef41Sopenharmony_ci      }
56341cb0ef41Sopenharmony_ci      break;
56351cb0ef41Sopenharmony_ci#undef PACK_FLOAT16
56361cb0ef41Sopenharmony_ci#undef FEXDO_DF
56371cb0ef41Sopenharmony_ci    case FTQ:
56381cb0ef41Sopenharmony_ci#define FTQ_DF(source, dst, fp_type, int_type)                  \
56391cb0ef41Sopenharmony_ci  element = bit_cast<fp_type>(source) *                         \
56401cb0ef41Sopenharmony_ci            (1U << (sizeof(int_type) * kBitsPerByte - 1));      \
56411cb0ef41Sopenharmony_ci  if (element > std::numeric_limits<int_type>::max()) {         \
56421cb0ef41Sopenharmony_ci    dst = std::numeric_limits<int_type>::max();                 \
56431cb0ef41Sopenharmony_ci  } else if (element < std::numeric_limits<int_type>::min()) {  \
56441cb0ef41Sopenharmony_ci    dst = std::numeric_limits<int_type>::min();                 \
56451cb0ef41Sopenharmony_ci  } else if (std::isnan(element)) {                             \
56461cb0ef41Sopenharmony_ci    dst = 0;                                                    \
56471cb0ef41Sopenharmony_ci  } else {                                                      \
56481cb0ef41Sopenharmony_ci    int_type fixed_point;                                       \
56491cb0ef41Sopenharmony_ci    round_according_to_msacsr(element, &element, &fixed_point); \
56501cb0ef41Sopenharmony_ci    dst = fixed_point;                                          \
56511cb0ef41Sopenharmony_ci  }
56521cb0ef41Sopenharmony_ci
56531cb0ef41Sopenharmony_ci      switch (DecodeMsaDataFormat()) {
56541cb0ef41Sopenharmony_ci        case MSA_HALF:
56551cb0ef41Sopenharmony_ci          for (int i = 0; i < kMSALanesWord; i++) {
56561cb0ef41Sopenharmony_ci            float element;
56571cb0ef41Sopenharmony_ci            FTQ_DF(ws.w[i], wd.h[i + kMSALanesHalf / 2], float, int16_t)
56581cb0ef41Sopenharmony_ci            FTQ_DF(wt.w[i], wd.h[i], float, int16_t)
56591cb0ef41Sopenharmony_ci          }
56601cb0ef41Sopenharmony_ci          break;
56611cb0ef41Sopenharmony_ci        case MSA_WORD:
56621cb0ef41Sopenharmony_ci          double element;
56631cb0ef41Sopenharmony_ci          for (int i = 0; i < kMSALanesDword; i++) {
56641cb0ef41Sopenharmony_ci            FTQ_DF(ws.d[i], wd.w[i + kMSALanesWord / 2], double, int32_t)
56651cb0ef41Sopenharmony_ci            FTQ_DF(wt.d[i], wd.w[i], double, int32_t)
56661cb0ef41Sopenharmony_ci          }
56671cb0ef41Sopenharmony_ci          break;
56681cb0ef41Sopenharmony_ci        default:
56691cb0ef41Sopenharmony_ci          UNREACHABLE();
56701cb0ef41Sopenharmony_ci      }
56711cb0ef41Sopenharmony_ci      break;
56721cb0ef41Sopenharmony_ci#undef FTQ_DF
56731cb0ef41Sopenharmony_ci#define MSA_3RF_DF(T1, T2, Lanes, ws, wt, wd)         \
56741cb0ef41Sopenharmony_ci  for (int i = 0; i < Lanes; i++) {                   \
56751cb0ef41Sopenharmony_ci    Msa3RFInstrHelper<T1, T2>(opcode, ws, wt, &(wd)); \
56761cb0ef41Sopenharmony_ci  }
56771cb0ef41Sopenharmony_ci#define MSA_3RF_DF2(T1, T2, Lanes, ws, wt, wd)         \
56781cb0ef41Sopenharmony_ci  for (int i = 0; i < Lanes; i++) {                    \
56791cb0ef41Sopenharmony_ci    Msa3RFInstrHelper2<T1, T2>(opcode, ws, wt, &(wd)); \
56801cb0ef41Sopenharmony_ci  }
56811cb0ef41Sopenharmony_ci    case MADD_Q:
56821cb0ef41Sopenharmony_ci    case MSUB_Q:
56831cb0ef41Sopenharmony_ci    case MADDR_Q:
56841cb0ef41Sopenharmony_ci    case MSUBR_Q:
56851cb0ef41Sopenharmony_ci      get_msa_register(wd_reg(), &wd);
56861cb0ef41Sopenharmony_ci      V8_FALLTHROUGH;
56871cb0ef41Sopenharmony_ci    case MUL_Q:
56881cb0ef41Sopenharmony_ci    case MULR_Q:
56891cb0ef41Sopenharmony_ci      switch (DecodeMsaDataFormat()) {
56901cb0ef41Sopenharmony_ci        case MSA_HALF:
56911cb0ef41Sopenharmony_ci          MSA_3RF_DF2(int16_t, int32_t, kMSALanesHalf, ws.h[i], wt.h[i],
56921cb0ef41Sopenharmony_ci                      wd.h[i])
56931cb0ef41Sopenharmony_ci          break;
56941cb0ef41Sopenharmony_ci        case MSA_WORD:
56951cb0ef41Sopenharmony_ci          MSA_3RF_DF2(int32_t, int64_t, kMSALanesWord, ws.w[i], wt.w[i],
56961cb0ef41Sopenharmony_ci                      wd.w[i])
56971cb0ef41Sopenharmony_ci          break;
56981cb0ef41Sopenharmony_ci        default:
56991cb0ef41Sopenharmony_ci          UNREACHABLE();
57001cb0ef41Sopenharmony_ci      }
57011cb0ef41Sopenharmony_ci      break;
57021cb0ef41Sopenharmony_ci    default:
57031cb0ef41Sopenharmony_ci      if (opcode == FMADD || opcode == FMSUB) {
57041cb0ef41Sopenharmony_ci        get_msa_register(wd_reg(), &wd);
57051cb0ef41Sopenharmony_ci      }
57061cb0ef41Sopenharmony_ci      switch (DecodeMsaDataFormat()) {
57071cb0ef41Sopenharmony_ci        case MSA_WORD:
57081cb0ef41Sopenharmony_ci          MSA_3RF_DF(int32_t, float, kMSALanesWord, ws.w[i], wt.w[i], wd.w[i])
57091cb0ef41Sopenharmony_ci          break;
57101cb0ef41Sopenharmony_ci        case MSA_DWORD:
57111cb0ef41Sopenharmony_ci          MSA_3RF_DF(int64_t, double, kMSALanesDword, ws.d[i], wt.d[i], wd.d[i])
57121cb0ef41Sopenharmony_ci          break;
57131cb0ef41Sopenharmony_ci        default:
57141cb0ef41Sopenharmony_ci          UNREACHABLE();
57151cb0ef41Sopenharmony_ci      }
57161cb0ef41Sopenharmony_ci      break;
57171cb0ef41Sopenharmony_ci#undef MSA_3RF_DF
57181cb0ef41Sopenharmony_ci#undef MSA_3RF_DF2
57191cb0ef41Sopenharmony_ci  }
57201cb0ef41Sopenharmony_ci  set_msa_register(wd_reg(), &wd);
57211cb0ef41Sopenharmony_ci  TraceMSARegWr(&wd);
57221cb0ef41Sopenharmony_ci}
57231cb0ef41Sopenharmony_ci
57241cb0ef41Sopenharmony_civoid Simulator::DecodeTypeMsaVec() {
57251cb0ef41Sopenharmony_ci  DCHECK(IsMipsArchVariant(kMips32r6));
57261cb0ef41Sopenharmony_ci  DCHECK(CpuFeatures::IsSupported(MIPS_SIMD));
57271cb0ef41Sopenharmony_ci  uint32_t opcode = instr_.InstructionBits() & kMsaVECMask;
57281cb0ef41Sopenharmony_ci  msa_reg_t wd, ws, wt;
57291cb0ef41Sopenharmony_ci
57301cb0ef41Sopenharmony_ci  get_msa_register(instr_.WsValue(), ws.w);
57311cb0ef41Sopenharmony_ci  get_msa_register(instr_.WtValue(), wt.w);
57321cb0ef41Sopenharmony_ci  if (opcode == BMNZ_V || opcode == BMZ_V || opcode == BSEL_V) {
57331cb0ef41Sopenharmony_ci    get_msa_register(instr_.WdValue(), wd.w);
57341cb0ef41Sopenharmony_ci  }
57351cb0ef41Sopenharmony_ci
57361cb0ef41Sopenharmony_ci  for (int i = 0; i < kMSALanesWord; i++) {
57371cb0ef41Sopenharmony_ci    switch (opcode) {
57381cb0ef41Sopenharmony_ci      case AND_V:
57391cb0ef41Sopenharmony_ci        wd.w[i] = ws.w[i] & wt.w[i];
57401cb0ef41Sopenharmony_ci        break;
57411cb0ef41Sopenharmony_ci      case OR_V:
57421cb0ef41Sopenharmony_ci        wd.w[i] = ws.w[i] | wt.w[i];
57431cb0ef41Sopenharmony_ci        break;
57441cb0ef41Sopenharmony_ci      case NOR_V:
57451cb0ef41Sopenharmony_ci        wd.w[i] = ~(ws.w[i] | wt.w[i]);
57461cb0ef41Sopenharmony_ci        break;
57471cb0ef41Sopenharmony_ci      case XOR_V:
57481cb0ef41Sopenharmony_ci        wd.w[i] = ws.w[i] ^ wt.w[i];
57491cb0ef41Sopenharmony_ci        break;
57501cb0ef41Sopenharmony_ci      case BMNZ_V:
57511cb0ef41Sopenharmony_ci        wd.w[i] = (wt.w[i] & ws.w[i]) | (~wt.w[i] & wd.w[i]);
57521cb0ef41Sopenharmony_ci        break;
57531cb0ef41Sopenharmony_ci      case BMZ_V:
57541cb0ef41Sopenharmony_ci        wd.w[i] = (~wt.w[i] & ws.w[i]) | (wt.w[i] & wd.w[i]);
57551cb0ef41Sopenharmony_ci        break;
57561cb0ef41Sopenharmony_ci      case BSEL_V:
57571cb0ef41Sopenharmony_ci        wd.w[i] = (~wd.w[i] & ws.w[i]) | (wd.w[i] & wt.w[i]);
57581cb0ef41Sopenharmony_ci        break;
57591cb0ef41Sopenharmony_ci      default:
57601cb0ef41Sopenharmony_ci        UNREACHABLE();
57611cb0ef41Sopenharmony_ci    }
57621cb0ef41Sopenharmony_ci  }
57631cb0ef41Sopenharmony_ci  set_msa_register(instr_.WdValue(), wd.w);
57641cb0ef41Sopenharmony_ci  TraceMSARegWr(wd.d);
57651cb0ef41Sopenharmony_ci}
57661cb0ef41Sopenharmony_ci
57671cb0ef41Sopenharmony_civoid Simulator::DecodeTypeMsa2R() {
57681cb0ef41Sopenharmony_ci  DCHECK(IsMipsArchVariant(kMips32r6));
57691cb0ef41Sopenharmony_ci  DCHECK(CpuFeatures::IsSupported(MIPS_SIMD));
57701cb0ef41Sopenharmony_ci  uint32_t opcode = instr_.InstructionBits() & kMsa2RMask;
57711cb0ef41Sopenharmony_ci  msa_reg_t wd, ws;
57721cb0ef41Sopenharmony_ci  switch (opcode) {
57731cb0ef41Sopenharmony_ci    case FILL:
57741cb0ef41Sopenharmony_ci      switch (DecodeMsaDataFormat()) {
57751cb0ef41Sopenharmony_ci        case MSA_BYTE: {
57761cb0ef41Sopenharmony_ci          int32_t rs = get_register(instr_.WsValue());
57771cb0ef41Sopenharmony_ci          for (int i = 0; i < kMSALanesByte; i++) {
57781cb0ef41Sopenharmony_ci            wd.b[i] = rs & 0xFFu;
57791cb0ef41Sopenharmony_ci          }
57801cb0ef41Sopenharmony_ci          set_msa_register(instr_.WdValue(), wd.b);
57811cb0ef41Sopenharmony_ci          TraceMSARegWr(wd.b);
57821cb0ef41Sopenharmony_ci          break;
57831cb0ef41Sopenharmony_ci        }
57841cb0ef41Sopenharmony_ci        case MSA_HALF: {
57851cb0ef41Sopenharmony_ci          int32_t rs = get_register(instr_.WsValue());
57861cb0ef41Sopenharmony_ci          for (int i = 0; i < kMSALanesHalf; i++) {
57871cb0ef41Sopenharmony_ci            wd.h[i] = rs & 0xFFFFu;
57881cb0ef41Sopenharmony_ci          }
57891cb0ef41Sopenharmony_ci          set_msa_register(instr_.WdValue(), wd.h);
57901cb0ef41Sopenharmony_ci          TraceMSARegWr(wd.h);
57911cb0ef41Sopenharmony_ci          break;
57921cb0ef41Sopenharmony_ci        }
57931cb0ef41Sopenharmony_ci        case MSA_WORD: {
57941cb0ef41Sopenharmony_ci          int32_t rs = get_register(instr_.WsValue());
57951cb0ef41Sopenharmony_ci          for (int i = 0; i < kMSALanesWord; i++) {
57961cb0ef41Sopenharmony_ci            wd.w[i] = rs;
57971cb0ef41Sopenharmony_ci          }
57981cb0ef41Sopenharmony_ci          set_msa_register(instr_.WdValue(), wd.w);
57991cb0ef41Sopenharmony_ci          TraceMSARegWr(wd.w);
58001cb0ef41Sopenharmony_ci          break;
58011cb0ef41Sopenharmony_ci        }
58021cb0ef41Sopenharmony_ci        default:
58031cb0ef41Sopenharmony_ci          UNREACHABLE();
58041cb0ef41Sopenharmony_ci      }
58051cb0ef41Sopenharmony_ci      break;
58061cb0ef41Sopenharmony_ci    case PCNT:
58071cb0ef41Sopenharmony_ci#define PCNT_DF(elem, num_of_lanes)                       \
58081cb0ef41Sopenharmony_ci  get_msa_register(instr_.WsValue(), ws.elem);            \
58091cb0ef41Sopenharmony_ci  for (int i = 0; i < num_of_lanes; i++) {                \
58101cb0ef41Sopenharmony_ci    uint64_t u64elem = static_cast<uint64_t>(ws.elem[i]); \
58111cb0ef41Sopenharmony_ci    wd.elem[i] = base::bits::CountPopulation(u64elem);    \
58121cb0ef41Sopenharmony_ci  }                                                       \
58131cb0ef41Sopenharmony_ci  set_msa_register(instr_.WdValue(), wd.elem);            \
58141cb0ef41Sopenharmony_ci  TraceMSARegWr(wd.elem)
58151cb0ef41Sopenharmony_ci
58161cb0ef41Sopenharmony_ci      switch (DecodeMsaDataFormat()) {
58171cb0ef41Sopenharmony_ci        case MSA_BYTE:
58181cb0ef41Sopenharmony_ci          PCNT_DF(ub, kMSALanesByte);
58191cb0ef41Sopenharmony_ci          break;
58201cb0ef41Sopenharmony_ci        case MSA_HALF:
58211cb0ef41Sopenharmony_ci          PCNT_DF(uh, kMSALanesHalf);
58221cb0ef41Sopenharmony_ci          break;
58231cb0ef41Sopenharmony_ci        case MSA_WORD:
58241cb0ef41Sopenharmony_ci          PCNT_DF(uw, kMSALanesWord);
58251cb0ef41Sopenharmony_ci          break;
58261cb0ef41Sopenharmony_ci        case MSA_DWORD:
58271cb0ef41Sopenharmony_ci          PCNT_DF(ud, kMSALanesDword);
58281cb0ef41Sopenharmony_ci          break;
58291cb0ef41Sopenharmony_ci        default:
58301cb0ef41Sopenharmony_ci          UNREACHABLE();
58311cb0ef41Sopenharmony_ci      }
58321cb0ef41Sopenharmony_ci#undef PCNT_DF
58331cb0ef41Sopenharmony_ci      break;
58341cb0ef41Sopenharmony_ci    case NLOC:
58351cb0ef41Sopenharmony_ci#define NLOC_DF(elem, num_of_lanes)                                         \
58361cb0ef41Sopenharmony_ci  get_msa_register(instr_.WsValue(), ws.elem);                              \
58371cb0ef41Sopenharmony_ci  for (int i = 0; i < num_of_lanes; i++) {                                  \
58381cb0ef41Sopenharmony_ci    const uint64_t mask = (num_of_lanes == kMSALanesDword)                  \
58391cb0ef41Sopenharmony_ci                              ? UINT64_MAX                                  \
58401cb0ef41Sopenharmony_ci                              : (1ULL << (kMSARegSize / num_of_lanes)) - 1; \
58411cb0ef41Sopenharmony_ci    uint64_t u64elem = static_cast<uint64_t>(~ws.elem[i]) & mask;           \
58421cb0ef41Sopenharmony_ci    wd.elem[i] = base::bits::CountLeadingZeros64(u64elem) -                 \
58431cb0ef41Sopenharmony_ci                 (64 - kMSARegSize / num_of_lanes);                         \
58441cb0ef41Sopenharmony_ci  }                                                                         \
58451cb0ef41Sopenharmony_ci  set_msa_register(instr_.WdValue(), wd.elem);                              \
58461cb0ef41Sopenharmony_ci  TraceMSARegWr(wd.elem)
58471cb0ef41Sopenharmony_ci
58481cb0ef41Sopenharmony_ci      switch (DecodeMsaDataFormat()) {
58491cb0ef41Sopenharmony_ci        case MSA_BYTE:
58501cb0ef41Sopenharmony_ci          NLOC_DF(ub, kMSALanesByte);
58511cb0ef41Sopenharmony_ci          break;
58521cb0ef41Sopenharmony_ci        case MSA_HALF:
58531cb0ef41Sopenharmony_ci          NLOC_DF(uh, kMSALanesHalf);
58541cb0ef41Sopenharmony_ci          break;
58551cb0ef41Sopenharmony_ci        case MSA_WORD:
58561cb0ef41Sopenharmony_ci          NLOC_DF(uw, kMSALanesWord);
58571cb0ef41Sopenharmony_ci          break;
58581cb0ef41Sopenharmony_ci        case MSA_DWORD:
58591cb0ef41Sopenharmony_ci          NLOC_DF(ud, kMSALanesDword);
58601cb0ef41Sopenharmony_ci          break;
58611cb0ef41Sopenharmony_ci        default:
58621cb0ef41Sopenharmony_ci          UNREACHABLE();
58631cb0ef41Sopenharmony_ci      }
58641cb0ef41Sopenharmony_ci#undef NLOC_DF
58651cb0ef41Sopenharmony_ci      break;
58661cb0ef41Sopenharmony_ci    case NLZC:
58671cb0ef41Sopenharmony_ci#define NLZC_DF(elem, num_of_lanes)                         \
58681cb0ef41Sopenharmony_ci  get_msa_register(instr_.WsValue(), ws.elem);              \
58691cb0ef41Sopenharmony_ci  for (int i = 0; i < num_of_lanes; i++) {                  \
58701cb0ef41Sopenharmony_ci    uint64_t u64elem = static_cast<uint64_t>(ws.elem[i]);   \
58711cb0ef41Sopenharmony_ci    wd.elem[i] = base::bits::CountLeadingZeros64(u64elem) - \
58721cb0ef41Sopenharmony_ci                 (64 - kMSARegSize / num_of_lanes);         \
58731cb0ef41Sopenharmony_ci  }                                                         \
58741cb0ef41Sopenharmony_ci  set_msa_register(instr_.WdValue(), wd.elem);              \
58751cb0ef41Sopenharmony_ci  TraceMSARegWr(wd.elem)
58761cb0ef41Sopenharmony_ci
58771cb0ef41Sopenharmony_ci      switch (DecodeMsaDataFormat()) {
58781cb0ef41Sopenharmony_ci        case MSA_BYTE:
58791cb0ef41Sopenharmony_ci          NLZC_DF(ub, kMSALanesByte);
58801cb0ef41Sopenharmony_ci          break;
58811cb0ef41Sopenharmony_ci        case MSA_HALF:
58821cb0ef41Sopenharmony_ci          NLZC_DF(uh, kMSALanesHalf);
58831cb0ef41Sopenharmony_ci          break;
58841cb0ef41Sopenharmony_ci        case MSA_WORD:
58851cb0ef41Sopenharmony_ci          NLZC_DF(uw, kMSALanesWord);
58861cb0ef41Sopenharmony_ci          break;
58871cb0ef41Sopenharmony_ci        case MSA_DWORD:
58881cb0ef41Sopenharmony_ci          NLZC_DF(ud, kMSALanesDword);
58891cb0ef41Sopenharmony_ci          break;
58901cb0ef41Sopenharmony_ci        default:
58911cb0ef41Sopenharmony_ci          UNREACHABLE();
58921cb0ef41Sopenharmony_ci      }
58931cb0ef41Sopenharmony_ci#undef NLZC_DF
58941cb0ef41Sopenharmony_ci      break;
58951cb0ef41Sopenharmony_ci    default:
58961cb0ef41Sopenharmony_ci      UNREACHABLE();
58971cb0ef41Sopenharmony_ci  }
58981cb0ef41Sopenharmony_ci}
58991cb0ef41Sopenharmony_ci
59001cb0ef41Sopenharmony_ci#define BIT(n) (0x1LL << n)
59011cb0ef41Sopenharmony_ci#define QUIET_BIT_S(nan) (bit_cast<int32_t>(nan) & BIT(22))
59021cb0ef41Sopenharmony_ci#define QUIET_BIT_D(nan) (bit_cast<int64_t>(nan) & BIT(51))
59031cb0ef41Sopenharmony_cistatic inline bool isSnan(float fp) { return !QUIET_BIT_S(fp); }
59041cb0ef41Sopenharmony_cistatic inline bool isSnan(double fp) { return !QUIET_BIT_D(fp); }
59051cb0ef41Sopenharmony_ci#undef QUIET_BIT_S
59061cb0ef41Sopenharmony_ci#undef QUIET_BIT_D
59071cb0ef41Sopenharmony_ci
59081cb0ef41Sopenharmony_citemplate <typename T_int, typename T_fp, typename T_src, typename T_dst>
59091cb0ef41Sopenharmony_ciT_int Msa2RFInstrHelper(uint32_t opcode, T_src src, T_dst* dst,
59101cb0ef41Sopenharmony_ci                        Simulator* sim) {
59111cb0ef41Sopenharmony_ci  using T_uint = typename std::make_unsigned<T_int>::type;
59121cb0ef41Sopenharmony_ci  switch (opcode) {
59131cb0ef41Sopenharmony_ci    case FCLASS: {
59141cb0ef41Sopenharmony_ci#define SNAN_BIT BIT(0)
59151cb0ef41Sopenharmony_ci#define QNAN_BIT BIT(1)
59161cb0ef41Sopenharmony_ci#define NEG_INFINITY_BIT BIT(2)
59171cb0ef41Sopenharmony_ci#define NEG_NORMAL_BIT BIT(3)
59181cb0ef41Sopenharmony_ci#define NEG_SUBNORMAL_BIT BIT(4)
59191cb0ef41Sopenharmony_ci#define NEG_ZERO_BIT BIT(5)
59201cb0ef41Sopenharmony_ci#define POS_INFINITY_BIT BIT(6)
59211cb0ef41Sopenharmony_ci#define POS_NORMAL_BIT BIT(7)
59221cb0ef41Sopenharmony_ci#define POS_SUBNORMAL_BIT BIT(8)
59231cb0ef41Sopenharmony_ci#define POS_ZERO_BIT BIT(9)
59241cb0ef41Sopenharmony_ci      T_fp element = *reinterpret_cast<T_fp*>(&src);
59251cb0ef41Sopenharmony_ci      switch (std::fpclassify(element)) {
59261cb0ef41Sopenharmony_ci        case FP_INFINITE:
59271cb0ef41Sopenharmony_ci          if (std::signbit(element)) {
59281cb0ef41Sopenharmony_ci            *dst = NEG_INFINITY_BIT;
59291cb0ef41Sopenharmony_ci          } else {
59301cb0ef41Sopenharmony_ci            *dst = POS_INFINITY_BIT;
59311cb0ef41Sopenharmony_ci          }
59321cb0ef41Sopenharmony_ci          break;
59331cb0ef41Sopenharmony_ci        case FP_NAN:
59341cb0ef41Sopenharmony_ci          if (isSnan(element)) {
59351cb0ef41Sopenharmony_ci            *dst = SNAN_BIT;
59361cb0ef41Sopenharmony_ci          } else {
59371cb0ef41Sopenharmony_ci            *dst = QNAN_BIT;
59381cb0ef41Sopenharmony_ci          }
59391cb0ef41Sopenharmony_ci          break;
59401cb0ef41Sopenharmony_ci        case FP_NORMAL:
59411cb0ef41Sopenharmony_ci          if (std::signbit(element)) {
59421cb0ef41Sopenharmony_ci            *dst = NEG_NORMAL_BIT;
59431cb0ef41Sopenharmony_ci          } else {
59441cb0ef41Sopenharmony_ci            *dst = POS_NORMAL_BIT;
59451cb0ef41Sopenharmony_ci          }
59461cb0ef41Sopenharmony_ci          break;
59471cb0ef41Sopenharmony_ci        case FP_SUBNORMAL:
59481cb0ef41Sopenharmony_ci          if (std::signbit(element)) {
59491cb0ef41Sopenharmony_ci            *dst = NEG_SUBNORMAL_BIT;
59501cb0ef41Sopenharmony_ci          } else {
59511cb0ef41Sopenharmony_ci            *dst = POS_SUBNORMAL_BIT;
59521cb0ef41Sopenharmony_ci          }
59531cb0ef41Sopenharmony_ci          break;
59541cb0ef41Sopenharmony_ci        case FP_ZERO:
59551cb0ef41Sopenharmony_ci          if (std::signbit(element)) {
59561cb0ef41Sopenharmony_ci            *dst = NEG_ZERO_BIT;
59571cb0ef41Sopenharmony_ci          } else {
59581cb0ef41Sopenharmony_ci            *dst = POS_ZERO_BIT;
59591cb0ef41Sopenharmony_ci          }
59601cb0ef41Sopenharmony_ci          break;
59611cb0ef41Sopenharmony_ci        default:
59621cb0ef41Sopenharmony_ci          UNREACHABLE();
59631cb0ef41Sopenharmony_ci      }
59641cb0ef41Sopenharmony_ci      break;
59651cb0ef41Sopenharmony_ci    }
59661cb0ef41Sopenharmony_ci#undef BIT
59671cb0ef41Sopenharmony_ci#undef SNAN_BIT
59681cb0ef41Sopenharmony_ci#undef QNAN_BIT
59691cb0ef41Sopenharmony_ci#undef NEG_INFINITY_BIT
59701cb0ef41Sopenharmony_ci#undef NEG_NORMAL_BIT
59711cb0ef41Sopenharmony_ci#undef NEG_SUBNORMAL_BIT
59721cb0ef41Sopenharmony_ci#undef NEG_ZERO_BIT
59731cb0ef41Sopenharmony_ci#undef POS_INFINITY_BIT
59741cb0ef41Sopenharmony_ci#undef POS_NORMAL_BIT
59751cb0ef41Sopenharmony_ci#undef POS_SUBNORMAL_BIT
59761cb0ef41Sopenharmony_ci#undef POS_ZERO_BIT
59771cb0ef41Sopenharmony_ci    case FTRUNC_S: {
59781cb0ef41Sopenharmony_ci      T_fp element = bit_cast<T_fp>(src);
59791cb0ef41Sopenharmony_ci      const T_int max_int = std::numeric_limits<T_int>::max();
59801cb0ef41Sopenharmony_ci      const T_int min_int = std::numeric_limits<T_int>::min();
59811cb0ef41Sopenharmony_ci      if (std::isnan(element)) {
59821cb0ef41Sopenharmony_ci        *dst = 0;
59831cb0ef41Sopenharmony_ci      } else if (element >= static_cast<T_fp>(max_int) || element <= min_int) {
59841cb0ef41Sopenharmony_ci        *dst = element >= static_cast<T_fp>(max_int) ? max_int : min_int;
59851cb0ef41Sopenharmony_ci      } else {
59861cb0ef41Sopenharmony_ci        *dst = static_cast<T_int>(std::trunc(element));
59871cb0ef41Sopenharmony_ci      }
59881cb0ef41Sopenharmony_ci      break;
59891cb0ef41Sopenharmony_ci    }
59901cb0ef41Sopenharmony_ci    case FTRUNC_U: {
59911cb0ef41Sopenharmony_ci      T_fp element = bit_cast<T_fp>(src);
59921cb0ef41Sopenharmony_ci      const T_uint max_int = std::numeric_limits<T_uint>::max();
59931cb0ef41Sopenharmony_ci      if (std::isnan(element)) {
59941cb0ef41Sopenharmony_ci        *dst = 0;
59951cb0ef41Sopenharmony_ci      } else if (element >= static_cast<T_fp>(max_int) || element <= 0) {
59961cb0ef41Sopenharmony_ci        *dst = element >= static_cast<T_fp>(max_int) ? max_int : 0;
59971cb0ef41Sopenharmony_ci      } else {
59981cb0ef41Sopenharmony_ci        *dst = static_cast<T_uint>(std::trunc(element));
59991cb0ef41Sopenharmony_ci      }
60001cb0ef41Sopenharmony_ci      break;
60011cb0ef41Sopenharmony_ci    }
60021cb0ef41Sopenharmony_ci    case FSQRT: {
60031cb0ef41Sopenharmony_ci      T_fp element = bit_cast<T_fp>(src);
60041cb0ef41Sopenharmony_ci      if (element < 0 || std::isnan(element)) {
60051cb0ef41Sopenharmony_ci        *dst = bit_cast<T_int>(std::numeric_limits<T_fp>::quiet_NaN());
60061cb0ef41Sopenharmony_ci      } else {
60071cb0ef41Sopenharmony_ci        *dst = bit_cast<T_int>(std::sqrt(element));
60081cb0ef41Sopenharmony_ci      }
60091cb0ef41Sopenharmony_ci      break;
60101cb0ef41Sopenharmony_ci    }
60111cb0ef41Sopenharmony_ci    case FRSQRT: {
60121cb0ef41Sopenharmony_ci      T_fp element = bit_cast<T_fp>(src);
60131cb0ef41Sopenharmony_ci      if (element < 0 || std::isnan(element)) {
60141cb0ef41Sopenharmony_ci        *dst = bit_cast<T_int>(std::numeric_limits<T_fp>::quiet_NaN());
60151cb0ef41Sopenharmony_ci      } else {
60161cb0ef41Sopenharmony_ci        *dst = bit_cast<T_int>(1 / std::sqrt(element));
60171cb0ef41Sopenharmony_ci      }
60181cb0ef41Sopenharmony_ci      break;
60191cb0ef41Sopenharmony_ci    }
60201cb0ef41Sopenharmony_ci    case FRCP: {
60211cb0ef41Sopenharmony_ci      T_fp element = bit_cast<T_fp>(src);
60221cb0ef41Sopenharmony_ci      if (std::isnan(element)) {
60231cb0ef41Sopenharmony_ci        *dst = bit_cast<T_int>(std::numeric_limits<T_fp>::quiet_NaN());
60241cb0ef41Sopenharmony_ci      } else {
60251cb0ef41Sopenharmony_ci        *dst = bit_cast<T_int>(1 / element);
60261cb0ef41Sopenharmony_ci      }
60271cb0ef41Sopenharmony_ci      break;
60281cb0ef41Sopenharmony_ci    }
60291cb0ef41Sopenharmony_ci    case FRINT: {
60301cb0ef41Sopenharmony_ci      T_fp element = bit_cast<T_fp>(src);
60311cb0ef41Sopenharmony_ci      if (std::isnan(element)) {
60321cb0ef41Sopenharmony_ci        *dst = bit_cast<T_int>(std::numeric_limits<T_fp>::quiet_NaN());
60331cb0ef41Sopenharmony_ci      } else {
60341cb0ef41Sopenharmony_ci        T_int dummy;
60351cb0ef41Sopenharmony_ci        sim->round_according_to_msacsr<T_fp, T_int>(element, &element, &dummy);
60361cb0ef41Sopenharmony_ci        *dst = bit_cast<T_int>(element);
60371cb0ef41Sopenharmony_ci      }
60381cb0ef41Sopenharmony_ci      break;
60391cb0ef41Sopenharmony_ci    }
60401cb0ef41Sopenharmony_ci    case FLOG2: {
60411cb0ef41Sopenharmony_ci      T_fp element = bit_cast<T_fp>(src);
60421cb0ef41Sopenharmony_ci      switch (std::fpclassify(element)) {
60431cb0ef41Sopenharmony_ci        case FP_NORMAL:
60441cb0ef41Sopenharmony_ci        case FP_SUBNORMAL:
60451cb0ef41Sopenharmony_ci          *dst = bit_cast<T_int>(std::logb(element));
60461cb0ef41Sopenharmony_ci          break;
60471cb0ef41Sopenharmony_ci        case FP_ZERO:
60481cb0ef41Sopenharmony_ci          *dst = bit_cast<T_int>(-std::numeric_limits<T_fp>::infinity());
60491cb0ef41Sopenharmony_ci          break;
60501cb0ef41Sopenharmony_ci        case FP_NAN:
60511cb0ef41Sopenharmony_ci          *dst = bit_cast<T_int>(std::numeric_limits<T_fp>::quiet_NaN());
60521cb0ef41Sopenharmony_ci          break;
60531cb0ef41Sopenharmony_ci        case FP_INFINITE:
60541cb0ef41Sopenharmony_ci          if (element < 0) {
60551cb0ef41Sopenharmony_ci            *dst = bit_cast<T_int>(std::numeric_limits<T_fp>::quiet_NaN());
60561cb0ef41Sopenharmony_ci          } else {
60571cb0ef41Sopenharmony_ci            *dst = bit_cast<T_int>(std::numeric_limits<T_fp>::infinity());
60581cb0ef41Sopenharmony_ci          }
60591cb0ef41Sopenharmony_ci          break;
60601cb0ef41Sopenharmony_ci        default:
60611cb0ef41Sopenharmony_ci          UNREACHABLE();
60621cb0ef41Sopenharmony_ci      }
60631cb0ef41Sopenharmony_ci      break;
60641cb0ef41Sopenharmony_ci    }
60651cb0ef41Sopenharmony_ci    case FTINT_S: {
60661cb0ef41Sopenharmony_ci      T_fp element = bit_cast<T_fp>(src);
60671cb0ef41Sopenharmony_ci      const T_int max_int = std::numeric_limits<T_int>::max();
60681cb0ef41Sopenharmony_ci      const T_int min_int = std::numeric_limits<T_int>::min();
60691cb0ef41Sopenharmony_ci      if (std::isnan(element)) {
60701cb0ef41Sopenharmony_ci        *dst = 0;
60711cb0ef41Sopenharmony_ci      } else if (element < min_int || element > static_cast<T_fp>(max_int)) {
60721cb0ef41Sopenharmony_ci        *dst = element > static_cast<T_fp>(max_int) ? max_int : min_int;
60731cb0ef41Sopenharmony_ci      } else {
60741cb0ef41Sopenharmony_ci        sim->round_according_to_msacsr<T_fp, T_int>(element, &element, dst);
60751cb0ef41Sopenharmony_ci      }
60761cb0ef41Sopenharmony_ci      break;
60771cb0ef41Sopenharmony_ci    }
60781cb0ef41Sopenharmony_ci    case FTINT_U: {
60791cb0ef41Sopenharmony_ci      T_fp element = bit_cast<T_fp>(src);
60801cb0ef41Sopenharmony_ci      const T_uint max_uint = std::numeric_limits<T_uint>::max();
60811cb0ef41Sopenharmony_ci      if (std::isnan(element)) {
60821cb0ef41Sopenharmony_ci        *dst = 0;
60831cb0ef41Sopenharmony_ci      } else if (element < 0 || element > static_cast<T_fp>(max_uint)) {
60841cb0ef41Sopenharmony_ci        *dst = element > static_cast<T_fp>(max_uint) ? max_uint : 0;
60851cb0ef41Sopenharmony_ci      } else {
60861cb0ef41Sopenharmony_ci        T_uint res;
60871cb0ef41Sopenharmony_ci        sim->round_according_to_msacsr<T_fp, T_uint>(element, &element, &res);
60881cb0ef41Sopenharmony_ci        *dst = *reinterpret_cast<T_int*>(&res);
60891cb0ef41Sopenharmony_ci      }
60901cb0ef41Sopenharmony_ci      break;
60911cb0ef41Sopenharmony_ci    }
60921cb0ef41Sopenharmony_ci    case FFINT_S:
60931cb0ef41Sopenharmony_ci      *dst = bit_cast<T_int>(static_cast<T_fp>(src));
60941cb0ef41Sopenharmony_ci      break;
60951cb0ef41Sopenharmony_ci    case FFINT_U:
60961cb0ef41Sopenharmony_ci      using uT_src = typename std::make_unsigned<T_src>::type;
60971cb0ef41Sopenharmony_ci      *dst = bit_cast<T_int>(static_cast<T_fp>(bit_cast<uT_src>(src)));
60981cb0ef41Sopenharmony_ci      break;
60991cb0ef41Sopenharmony_ci    default:
61001cb0ef41Sopenharmony_ci      UNREACHABLE();
61011cb0ef41Sopenharmony_ci  }
61021cb0ef41Sopenharmony_ci  return 0;
61031cb0ef41Sopenharmony_ci}
61041cb0ef41Sopenharmony_ci
61051cb0ef41Sopenharmony_citemplate <typename T_int, typename T_fp, typename T_reg>
61061cb0ef41Sopenharmony_ciT_int Msa2RFInstrHelper2(uint32_t opcode, T_reg ws, int i) {
61071cb0ef41Sopenharmony_ci  switch (opcode) {
61081cb0ef41Sopenharmony_ci#define EXTRACT_FLOAT16_SIGN(fp16) (fp16 >> 15)
61091cb0ef41Sopenharmony_ci#define EXTRACT_FLOAT16_EXP(fp16) (fp16 >> 10 & 0x1F)
61101cb0ef41Sopenharmony_ci#define EXTRACT_FLOAT16_FRAC(fp16) (fp16 & 0x3FF)
61111cb0ef41Sopenharmony_ci#define PACK_FLOAT32(sign, exp, frac) \
61121cb0ef41Sopenharmony_ci  static_cast<uint32_t>(((sign) << 31) + ((exp) << 23) + (frac))
61131cb0ef41Sopenharmony_ci#define FEXUP_DF(src_index)                                                   \
61141cb0ef41Sopenharmony_ci  uint_fast16_t element = ws.uh[src_index];                                   \
61151cb0ef41Sopenharmony_ci  uint_fast32_t aSign, aFrac;                                                 \
61161cb0ef41Sopenharmony_ci  int_fast32_t aExp;                                                          \
61171cb0ef41Sopenharmony_ci  aSign = EXTRACT_FLOAT16_SIGN(element);                                      \
61181cb0ef41Sopenharmony_ci  aExp = EXTRACT_FLOAT16_EXP(element);                                        \
61191cb0ef41Sopenharmony_ci  aFrac = EXTRACT_FLOAT16_FRAC(element);                                      \
61201cb0ef41Sopenharmony_ci  if (V8_LIKELY(aExp && aExp != 0x1F)) {                                      \
61211cb0ef41Sopenharmony_ci    return PACK_FLOAT32(aSign, aExp + 0x70, aFrac << 13);                     \
61221cb0ef41Sopenharmony_ci  } else if (aExp == 0x1F) {                                                  \
61231cb0ef41Sopenharmony_ci    if (aFrac) {                                                              \
61241cb0ef41Sopenharmony_ci      return bit_cast<int32_t>(std::numeric_limits<float>::quiet_NaN());      \
61251cb0ef41Sopenharmony_ci    } else {                                                                  \
61261cb0ef41Sopenharmony_ci      return bit_cast<uint32_t>(std::numeric_limits<float>::infinity()) |     \
61271cb0ef41Sopenharmony_ci             static_cast<uint32_t>(aSign) << 31;                              \
61281cb0ef41Sopenharmony_ci    }                                                                         \
61291cb0ef41Sopenharmony_ci  } else {                                                                    \
61301cb0ef41Sopenharmony_ci    if (aFrac == 0) {                                                         \
61311cb0ef41Sopenharmony_ci      return PACK_FLOAT32(aSign, 0, 0);                                       \
61321cb0ef41Sopenharmony_ci    } else {                                                                  \
61331cb0ef41Sopenharmony_ci      int_fast16_t shiftCount =                                               \
61341cb0ef41Sopenharmony_ci          base::bits::CountLeadingZeros32(static_cast<uint32_t>(aFrac)) - 21; \
61351cb0ef41Sopenharmony_ci      aFrac <<= shiftCount;                                                   \
61361cb0ef41Sopenharmony_ci      aExp = -shiftCount;                                                     \
61371cb0ef41Sopenharmony_ci      return PACK_FLOAT32(aSign, aExp + 0x70, aFrac << 13);                   \
61381cb0ef41Sopenharmony_ci    }                                                                         \
61391cb0ef41Sopenharmony_ci  }
61401cb0ef41Sopenharmony_ci    case FEXUPL:
61411cb0ef41Sopenharmony_ci      if (std::is_same<int32_t, T_int>::value) {
61421cb0ef41Sopenharmony_ci        FEXUP_DF(i + kMSALanesWord)
61431cb0ef41Sopenharmony_ci      } else {
61441cb0ef41Sopenharmony_ci        return bit_cast<int64_t>(
61451cb0ef41Sopenharmony_ci            static_cast<double>(bit_cast<float>(ws.w[i + kMSALanesDword])));
61461cb0ef41Sopenharmony_ci      }
61471cb0ef41Sopenharmony_ci    case FEXUPR:
61481cb0ef41Sopenharmony_ci      if (std::is_same<int32_t, T_int>::value) {
61491cb0ef41Sopenharmony_ci        FEXUP_DF(i)
61501cb0ef41Sopenharmony_ci      } else {
61511cb0ef41Sopenharmony_ci        return bit_cast<int64_t>(static_cast<double>(bit_cast<float>(ws.w[i])));
61521cb0ef41Sopenharmony_ci      }
61531cb0ef41Sopenharmony_ci    case FFQL: {
61541cb0ef41Sopenharmony_ci      if (std::is_same<int32_t, T_int>::value) {
61551cb0ef41Sopenharmony_ci        return bit_cast<int32_t>(static_cast<float>(ws.h[i + kMSALanesWord]) /
61561cb0ef41Sopenharmony_ci                                 (1U << 15));
61571cb0ef41Sopenharmony_ci      } else {
61581cb0ef41Sopenharmony_ci        return bit_cast<int64_t>(static_cast<double>(ws.w[i + kMSALanesDword]) /
61591cb0ef41Sopenharmony_ci                                 (1U << 31));
61601cb0ef41Sopenharmony_ci      }
61611cb0ef41Sopenharmony_ci      break;
61621cb0ef41Sopenharmony_ci    }
61631cb0ef41Sopenharmony_ci    case FFQR: {
61641cb0ef41Sopenharmony_ci      if (std::is_same<int32_t, T_int>::value) {
61651cb0ef41Sopenharmony_ci        return bit_cast<int32_t>(static_cast<float>(ws.h[i]) / (1U << 15));
61661cb0ef41Sopenharmony_ci      } else {
61671cb0ef41Sopenharmony_ci        return bit_cast<int64_t>(static_cast<double>(ws.w[i]) / (1U << 31));
61681cb0ef41Sopenharmony_ci      }
61691cb0ef41Sopenharmony_ci      break;
61701cb0ef41Sopenharmony_ci      default:
61711cb0ef41Sopenharmony_ci        UNREACHABLE();
61721cb0ef41Sopenharmony_ci    }
61731cb0ef41Sopenharmony_ci  }
61741cb0ef41Sopenharmony_ci#undef EXTRACT_FLOAT16_SIGN
61751cb0ef41Sopenharmony_ci#undef EXTRACT_FLOAT16_EXP
61761cb0ef41Sopenharmony_ci#undef EXTRACT_FLOAT16_FRAC
61771cb0ef41Sopenharmony_ci#undef PACK_FLOAT32
61781cb0ef41Sopenharmony_ci#undef FEXUP_DF
61791cb0ef41Sopenharmony_ci}
61801cb0ef41Sopenharmony_ci
61811cb0ef41Sopenharmony_civoid Simulator::DecodeTypeMsa2RF() {
61821cb0ef41Sopenharmony_ci  DCHECK(IsMipsArchVariant(kMips32r6));
61831cb0ef41Sopenharmony_ci  DCHECK(CpuFeatures::IsSupported(MIPS_SIMD));
61841cb0ef41Sopenharmony_ci  uint32_t opcode = instr_.InstructionBits() & kMsa2RFMask;
61851cb0ef41Sopenharmony_ci  msa_reg_t wd, ws;
61861cb0ef41Sopenharmony_ci  get_msa_register(ws_reg(), &ws);
61871cb0ef41Sopenharmony_ci  if (opcode == FEXUPL || opcode == FEXUPR || opcode == FFQL ||
61881cb0ef41Sopenharmony_ci      opcode == FFQR) {
61891cb0ef41Sopenharmony_ci    switch (DecodeMsaDataFormat()) {
61901cb0ef41Sopenharmony_ci      case MSA_WORD:
61911cb0ef41Sopenharmony_ci        for (int i = 0; i < kMSALanesWord; i++) {
61921cb0ef41Sopenharmony_ci          wd.w[i] = Msa2RFInstrHelper2<int32_t, float>(opcode, ws, i);
61931cb0ef41Sopenharmony_ci        }
61941cb0ef41Sopenharmony_ci        break;
61951cb0ef41Sopenharmony_ci      case MSA_DWORD:
61961cb0ef41Sopenharmony_ci        for (int i = 0; i < kMSALanesDword; i++) {
61971cb0ef41Sopenharmony_ci          wd.d[i] = Msa2RFInstrHelper2<int64_t, double>(opcode, ws, i);
61981cb0ef41Sopenharmony_ci        }
61991cb0ef41Sopenharmony_ci        break;
62001cb0ef41Sopenharmony_ci      default:
62011cb0ef41Sopenharmony_ci        UNREACHABLE();
62021cb0ef41Sopenharmony_ci    }
62031cb0ef41Sopenharmony_ci  } else {
62041cb0ef41Sopenharmony_ci    switch (DecodeMsaDataFormat()) {
62051cb0ef41Sopenharmony_ci      case MSA_WORD:
62061cb0ef41Sopenharmony_ci        for (int i = 0; i < kMSALanesWord; i++) {
62071cb0ef41Sopenharmony_ci          Msa2RFInstrHelper<int32_t, float>(opcode, ws.w[i], &wd.w[i], this);
62081cb0ef41Sopenharmony_ci        }
62091cb0ef41Sopenharmony_ci        break;
62101cb0ef41Sopenharmony_ci      case MSA_DWORD:
62111cb0ef41Sopenharmony_ci        for (int i = 0; i < kMSALanesDword; i++) {
62121cb0ef41Sopenharmony_ci          Msa2RFInstrHelper<int64_t, double>(opcode, ws.d[i], &wd.d[i], this);
62131cb0ef41Sopenharmony_ci        }
62141cb0ef41Sopenharmony_ci        break;
62151cb0ef41Sopenharmony_ci      default:
62161cb0ef41Sopenharmony_ci        UNREACHABLE();
62171cb0ef41Sopenharmony_ci    }
62181cb0ef41Sopenharmony_ci  }
62191cb0ef41Sopenharmony_ci  set_msa_register(wd_reg(), &wd);
62201cb0ef41Sopenharmony_ci  TraceMSARegWr(&wd);
62211cb0ef41Sopenharmony_ci}
62221cb0ef41Sopenharmony_ci
62231cb0ef41Sopenharmony_civoid Simulator::DecodeTypeRegister() {
62241cb0ef41Sopenharmony_ci  // ---------- Execution.
62251cb0ef41Sopenharmony_ci  switch (instr_.OpcodeFieldRaw()) {
62261cb0ef41Sopenharmony_ci    case COP1:
62271cb0ef41Sopenharmony_ci      DecodeTypeRegisterCOP1();
62281cb0ef41Sopenharmony_ci      break;
62291cb0ef41Sopenharmony_ci    case COP1X:
62301cb0ef41Sopenharmony_ci      DecodeTypeRegisterCOP1X();
62311cb0ef41Sopenharmony_ci      break;
62321cb0ef41Sopenharmony_ci    case SPECIAL:
62331cb0ef41Sopenharmony_ci      DecodeTypeRegisterSPECIAL();
62341cb0ef41Sopenharmony_ci      break;
62351cb0ef41Sopenharmony_ci    case SPECIAL2:
62361cb0ef41Sopenharmony_ci      DecodeTypeRegisterSPECIAL2();
62371cb0ef41Sopenharmony_ci      break;
62381cb0ef41Sopenharmony_ci    case SPECIAL3:
62391cb0ef41Sopenharmony_ci      DecodeTypeRegisterSPECIAL3();
62401cb0ef41Sopenharmony_ci      break;
62411cb0ef41Sopenharmony_ci    case MSA:
62421cb0ef41Sopenharmony_ci      switch (instr_.MSAMinorOpcodeField()) {
62431cb0ef41Sopenharmony_ci        case kMsaMinor3R:
62441cb0ef41Sopenharmony_ci          DecodeTypeMsa3R();
62451cb0ef41Sopenharmony_ci          break;
62461cb0ef41Sopenharmony_ci        case kMsaMinor3RF:
62471cb0ef41Sopenharmony_ci          DecodeTypeMsa3RF();
62481cb0ef41Sopenharmony_ci          break;
62491cb0ef41Sopenharmony_ci        case kMsaMinorVEC:
62501cb0ef41Sopenharmony_ci          DecodeTypeMsaVec();
62511cb0ef41Sopenharmony_ci          break;
62521cb0ef41Sopenharmony_ci        case kMsaMinor2R:
62531cb0ef41Sopenharmony_ci          DecodeTypeMsa2R();
62541cb0ef41Sopenharmony_ci          break;
62551cb0ef41Sopenharmony_ci        case kMsaMinor2RF:
62561cb0ef41Sopenharmony_ci          DecodeTypeMsa2RF();
62571cb0ef41Sopenharmony_ci          break;
62581cb0ef41Sopenharmony_ci        case kMsaMinorELM:
62591cb0ef41Sopenharmony_ci          DecodeTypeMsaELM();
62601cb0ef41Sopenharmony_ci          break;
62611cb0ef41Sopenharmony_ci        default:
62621cb0ef41Sopenharmony_ci          UNREACHABLE();
62631cb0ef41Sopenharmony_ci      }
62641cb0ef41Sopenharmony_ci      break;
62651cb0ef41Sopenharmony_ci    default:
62661cb0ef41Sopenharmony_ci      UNREACHABLE();
62671cb0ef41Sopenharmony_ci  }
62681cb0ef41Sopenharmony_ci}
62691cb0ef41Sopenharmony_ci
62701cb0ef41Sopenharmony_ci// Type 2: instructions using a 16, 21 or 26 bits immediate. (e.g. beq, beqc).
62711cb0ef41Sopenharmony_civoid Simulator::DecodeTypeImmediate() {
62721cb0ef41Sopenharmony_ci  // Instruction fields.
62731cb0ef41Sopenharmony_ci  Opcode op = instr_.OpcodeFieldRaw();
62741cb0ef41Sopenharmony_ci  int32_t rs_reg = instr_.RsValue();
62751cb0ef41Sopenharmony_ci  int32_t rs = get_register(instr_.RsValue());
62761cb0ef41Sopenharmony_ci  uint32_t rs_u = static_cast<uint32_t>(rs);
62771cb0ef41Sopenharmony_ci  int32_t rt_reg = instr_.RtValue();  // Destination register.
62781cb0ef41Sopenharmony_ci  int32_t rt = get_register(rt_reg);
62791cb0ef41Sopenharmony_ci  int16_t imm16 = instr_.Imm16Value();
62801cb0ef41Sopenharmony_ci
62811cb0ef41Sopenharmony_ci  int32_t ft_reg = instr_.FtValue();  // Destination register.
62821cb0ef41Sopenharmony_ci
62831cb0ef41Sopenharmony_ci  // Zero extended immediate.
62841cb0ef41Sopenharmony_ci  uint32_t oe_imm16 = 0xFFFF & imm16;
62851cb0ef41Sopenharmony_ci  // Sign extended immediate.
62861cb0ef41Sopenharmony_ci  int32_t se_imm16 = imm16;
62871cb0ef41Sopenharmony_ci
62881cb0ef41Sopenharmony_ci  // Next pc.
62891cb0ef41Sopenharmony_ci  int32_t next_pc = bad_ra;
62901cb0ef41Sopenharmony_ci
62911cb0ef41Sopenharmony_ci  // Used for conditional branch instructions.
62921cb0ef41Sopenharmony_ci  bool execute_branch_delay_instruction = false;
62931cb0ef41Sopenharmony_ci
62941cb0ef41Sopenharmony_ci  // Used for arithmetic instructions.
62951cb0ef41Sopenharmony_ci  int32_t alu_out = 0;
62961cb0ef41Sopenharmony_ci
62971cb0ef41Sopenharmony_ci  // Used for memory instructions.
62981cb0ef41Sopenharmony_ci  int32_t addr = 0x0;
62991cb0ef41Sopenharmony_ci
63001cb0ef41Sopenharmony_ci  // Branch instructions common part.
63011cb0ef41Sopenharmony_ci  auto BranchAndLinkHelper =
63021cb0ef41Sopenharmony_ci      [this, &next_pc, &execute_branch_delay_instruction](bool do_branch) {
63031cb0ef41Sopenharmony_ci        execute_branch_delay_instruction = true;
63041cb0ef41Sopenharmony_ci        int32_t current_pc = get_pc();
63051cb0ef41Sopenharmony_ci        set_register(31, current_pc + 2 * kInstrSize);
63061cb0ef41Sopenharmony_ci        if (do_branch) {
63071cb0ef41Sopenharmony_ci          int16_t imm16 = this->instr_.Imm16Value();
63081cb0ef41Sopenharmony_ci          next_pc = current_pc + (imm16 << 2) + kInstrSize;
63091cb0ef41Sopenharmony_ci        } else {
63101cb0ef41Sopenharmony_ci          next_pc = current_pc + 2 * kInstrSize;
63111cb0ef41Sopenharmony_ci        }
63121cb0ef41Sopenharmony_ci      };
63131cb0ef41Sopenharmony_ci
63141cb0ef41Sopenharmony_ci  auto BranchHelper = [this, &next_pc,
63151cb0ef41Sopenharmony_ci                       &execute_branch_delay_instruction](bool do_branch) {
63161cb0ef41Sopenharmony_ci    execute_branch_delay_instruction = true;
63171cb0ef41Sopenharmony_ci    int32_t current_pc = get_pc();
63181cb0ef41Sopenharmony_ci    if (do_branch) {
63191cb0ef41Sopenharmony_ci      int16_t imm16 = this->instr_.Imm16Value();
63201cb0ef41Sopenharmony_ci      next_pc = current_pc + (imm16 << 2) + kInstrSize;
63211cb0ef41Sopenharmony_ci    } else {
63221cb0ef41Sopenharmony_ci      next_pc = current_pc + 2 * kInstrSize;
63231cb0ef41Sopenharmony_ci    }
63241cb0ef41Sopenharmony_ci  };
63251cb0ef41Sopenharmony_ci
63261cb0ef41Sopenharmony_ci  auto BranchHelper_MSA = [this, &next_pc, imm16,
63271cb0ef41Sopenharmony_ci                           &execute_branch_delay_instruction](bool do_branch) {
63281cb0ef41Sopenharmony_ci    execute_branch_delay_instruction = true;
63291cb0ef41Sopenharmony_ci    int32_t current_pc = get_pc();
63301cb0ef41Sopenharmony_ci    const int32_t bitsIn16Int = sizeof(int16_t) * kBitsPerByte;
63311cb0ef41Sopenharmony_ci    if (do_branch) {
63321cb0ef41Sopenharmony_ci      if (FLAG_debug_code) {
63331cb0ef41Sopenharmony_ci        int16_t bits = imm16 & 0xFC;
63341cb0ef41Sopenharmony_ci        if (imm16 >= 0) {
63351cb0ef41Sopenharmony_ci          CHECK_EQ(bits, 0);
63361cb0ef41Sopenharmony_ci        } else {
63371cb0ef41Sopenharmony_ci          CHECK_EQ(bits ^ 0xFC, 0);
63381cb0ef41Sopenharmony_ci        }
63391cb0ef41Sopenharmony_ci      }
63401cb0ef41Sopenharmony_ci      // jump range :[pc + kInstrSize - 512 * kInstrSize,
63411cb0ef41Sopenharmony_ci      //              pc + kInstrSize + 511 * kInstrSize]
63421cb0ef41Sopenharmony_ci      int16_t offset = static_cast<int16_t>(imm16 << (bitsIn16Int - 10)) >>
63431cb0ef41Sopenharmony_ci                       (bitsIn16Int - 12);
63441cb0ef41Sopenharmony_ci      next_pc = current_pc + offset + kInstrSize;
63451cb0ef41Sopenharmony_ci    } else {
63461cb0ef41Sopenharmony_ci      next_pc = current_pc + 2 * kInstrSize;
63471cb0ef41Sopenharmony_ci    }
63481cb0ef41Sopenharmony_ci  };
63491cb0ef41Sopenharmony_ci
63501cb0ef41Sopenharmony_ci  auto BranchAndLinkCompactHelper = [this, &next_pc](bool do_branch, int bits) {
63511cb0ef41Sopenharmony_ci    int32_t current_pc = get_pc();
63521cb0ef41Sopenharmony_ci    CheckForbiddenSlot(current_pc);
63531cb0ef41Sopenharmony_ci    if (do_branch) {
63541cb0ef41Sopenharmony_ci      int32_t imm = this->instr_.ImmValue(bits);
63551cb0ef41Sopenharmony_ci      imm <<= 32 - bits;
63561cb0ef41Sopenharmony_ci      imm >>= 32 - bits;
63571cb0ef41Sopenharmony_ci      next_pc = current_pc + (imm << 2) + kInstrSize;
63581cb0ef41Sopenharmony_ci      set_register(31, current_pc + kInstrSize);
63591cb0ef41Sopenharmony_ci    }
63601cb0ef41Sopenharmony_ci  };
63611cb0ef41Sopenharmony_ci
63621cb0ef41Sopenharmony_ci  auto BranchCompactHelper = [this, &next_pc](bool do_branch, int bits) {
63631cb0ef41Sopenharmony_ci    int32_t current_pc = get_pc();
63641cb0ef41Sopenharmony_ci    CheckForbiddenSlot(current_pc);
63651cb0ef41Sopenharmony_ci    if (do_branch) {
63661cb0ef41Sopenharmony_ci      int32_t imm = this->instr_.ImmValue(bits);
63671cb0ef41Sopenharmony_ci      imm <<= 32 - bits;
63681cb0ef41Sopenharmony_ci      imm >>= 32 - bits;
63691cb0ef41Sopenharmony_ci      next_pc = get_pc() + (imm << 2) + kInstrSize;
63701cb0ef41Sopenharmony_ci    }
63711cb0ef41Sopenharmony_ci  };
63721cb0ef41Sopenharmony_ci
63731cb0ef41Sopenharmony_ci  switch (op) {
63741cb0ef41Sopenharmony_ci    // ------------- COP1. Coprocessor instructions.
63751cb0ef41Sopenharmony_ci    case COP1:
63761cb0ef41Sopenharmony_ci      switch (instr_.RsFieldRaw()) {
63771cb0ef41Sopenharmony_ci        case BC1: {  // Branch on coprocessor condition.
63781cb0ef41Sopenharmony_ci          // Floating point.
63791cb0ef41Sopenharmony_ci          uint32_t cc = instr_.FBccValue();
63801cb0ef41Sopenharmony_ci          uint32_t fcsr_cc = get_fcsr_condition_bit(cc);
63811cb0ef41Sopenharmony_ci          uint32_t cc_value = test_fcsr_bit(fcsr_cc);
63821cb0ef41Sopenharmony_ci          bool do_branch = (instr_.FBtrueValue()) ? cc_value : !cc_value;
63831cb0ef41Sopenharmony_ci          BranchHelper(do_branch);
63841cb0ef41Sopenharmony_ci          break;
63851cb0ef41Sopenharmony_ci        }
63861cb0ef41Sopenharmony_ci        case BC1EQZ:
63871cb0ef41Sopenharmony_ci          BranchHelper(!(get_fpu_register(ft_reg) & 0x1));
63881cb0ef41Sopenharmony_ci          break;
63891cb0ef41Sopenharmony_ci        case BC1NEZ:
63901cb0ef41Sopenharmony_ci          BranchHelper(get_fpu_register(ft_reg) & 0x1);
63911cb0ef41Sopenharmony_ci          break;
63921cb0ef41Sopenharmony_ci        case BZ_V: {
63931cb0ef41Sopenharmony_ci          msa_reg_t wt;
63941cb0ef41Sopenharmony_ci          get_msa_register(wt_reg(), &wt);
63951cb0ef41Sopenharmony_ci          BranchHelper_MSA(wt.d[0] == 0 && wt.d[1] == 0);
63961cb0ef41Sopenharmony_ci        } break;
63971cb0ef41Sopenharmony_ci#define BZ_DF(witdh, lanes)          \
63981cb0ef41Sopenharmony_ci  {                                  \
63991cb0ef41Sopenharmony_ci    msa_reg_t wt;                    \
64001cb0ef41Sopenharmony_ci    get_msa_register(wt_reg(), &wt); \
64011cb0ef41Sopenharmony_ci    int i;                           \
64021cb0ef41Sopenharmony_ci    for (i = 0; i < lanes; ++i) {    \
64031cb0ef41Sopenharmony_ci      if (wt.witdh[i] == 0) {        \
64041cb0ef41Sopenharmony_ci        break;                       \
64051cb0ef41Sopenharmony_ci      }                              \
64061cb0ef41Sopenharmony_ci    }                                \
64071cb0ef41Sopenharmony_ci    BranchHelper_MSA(i != lanes);    \
64081cb0ef41Sopenharmony_ci  }
64091cb0ef41Sopenharmony_ci        case BZ_B:
64101cb0ef41Sopenharmony_ci          BZ_DF(b, kMSALanesByte)
64111cb0ef41Sopenharmony_ci          break;
64121cb0ef41Sopenharmony_ci        case BZ_H:
64131cb0ef41Sopenharmony_ci          BZ_DF(h, kMSALanesHalf)
64141cb0ef41Sopenharmony_ci          break;
64151cb0ef41Sopenharmony_ci        case BZ_W:
64161cb0ef41Sopenharmony_ci          BZ_DF(w, kMSALanesWord)
64171cb0ef41Sopenharmony_ci          break;
64181cb0ef41Sopenharmony_ci        case BZ_D:
64191cb0ef41Sopenharmony_ci          BZ_DF(d, kMSALanesDword)
64201cb0ef41Sopenharmony_ci          break;
64211cb0ef41Sopenharmony_ci#undef BZ_DF
64221cb0ef41Sopenharmony_ci        case BNZ_V: {
64231cb0ef41Sopenharmony_ci          msa_reg_t wt;
64241cb0ef41Sopenharmony_ci          get_msa_register(wt_reg(), &wt);
64251cb0ef41Sopenharmony_ci          BranchHelper_MSA(wt.d[0] != 0 || wt.d[1] != 0);
64261cb0ef41Sopenharmony_ci        } break;
64271cb0ef41Sopenharmony_ci#define BNZ_DF(witdh, lanes)         \
64281cb0ef41Sopenharmony_ci  {                                  \
64291cb0ef41Sopenharmony_ci    msa_reg_t wt;                    \
64301cb0ef41Sopenharmony_ci    get_msa_register(wt_reg(), &wt); \
64311cb0ef41Sopenharmony_ci    int i;                           \
64321cb0ef41Sopenharmony_ci    for (i = 0; i < lanes; ++i) {    \
64331cb0ef41Sopenharmony_ci      if (wt.witdh[i] == 0) {        \
64341cb0ef41Sopenharmony_ci        break;                       \
64351cb0ef41Sopenharmony_ci      }                              \
64361cb0ef41Sopenharmony_ci    }                                \
64371cb0ef41Sopenharmony_ci    BranchHelper_MSA(i == lanes);    \
64381cb0ef41Sopenharmony_ci  }
64391cb0ef41Sopenharmony_ci        case BNZ_B:
64401cb0ef41Sopenharmony_ci          BNZ_DF(b, kMSALanesByte)
64411cb0ef41Sopenharmony_ci          break;
64421cb0ef41Sopenharmony_ci        case BNZ_H:
64431cb0ef41Sopenharmony_ci          BNZ_DF(h, kMSALanesHalf)
64441cb0ef41Sopenharmony_ci          break;
64451cb0ef41Sopenharmony_ci        case BNZ_W:
64461cb0ef41Sopenharmony_ci          BNZ_DF(w, kMSALanesWord)
64471cb0ef41Sopenharmony_ci          break;
64481cb0ef41Sopenharmony_ci        case BNZ_D:
64491cb0ef41Sopenharmony_ci          BNZ_DF(d, kMSALanesDword)
64501cb0ef41Sopenharmony_ci          break;
64511cb0ef41Sopenharmony_ci#undef BNZ_DF
64521cb0ef41Sopenharmony_ci        default:
64531cb0ef41Sopenharmony_ci          UNREACHABLE();
64541cb0ef41Sopenharmony_ci      }
64551cb0ef41Sopenharmony_ci      break;
64561cb0ef41Sopenharmony_ci    // ------------- REGIMM class.
64571cb0ef41Sopenharmony_ci    case REGIMM:
64581cb0ef41Sopenharmony_ci      switch (instr_.RtFieldRaw()) {
64591cb0ef41Sopenharmony_ci        case BLTZ:
64601cb0ef41Sopenharmony_ci          BranchHelper(rs < 0);
64611cb0ef41Sopenharmony_ci          break;
64621cb0ef41Sopenharmony_ci        case BGEZ:
64631cb0ef41Sopenharmony_ci          BranchHelper(rs >= 0);
64641cb0ef41Sopenharmony_ci          break;
64651cb0ef41Sopenharmony_ci        case BLTZAL:
64661cb0ef41Sopenharmony_ci          BranchAndLinkHelper(rs < 0);
64671cb0ef41Sopenharmony_ci          break;
64681cb0ef41Sopenharmony_ci        case BGEZAL:
64691cb0ef41Sopenharmony_ci          BranchAndLinkHelper(rs >= 0);
64701cb0ef41Sopenharmony_ci          break;
64711cb0ef41Sopenharmony_ci        default:
64721cb0ef41Sopenharmony_ci          UNREACHABLE();
64731cb0ef41Sopenharmony_ci      }
64741cb0ef41Sopenharmony_ci      break;  // case REGIMM.
64751cb0ef41Sopenharmony_ci    // ------------- Branch instructions.
64761cb0ef41Sopenharmony_ci    // When comparing to zero, the encoding of rt field is always 0, so we don't
64771cb0ef41Sopenharmony_ci    // need to replace rt with zero.
64781cb0ef41Sopenharmony_ci    case BEQ:
64791cb0ef41Sopenharmony_ci      BranchHelper(rs == rt);
64801cb0ef41Sopenharmony_ci      break;
64811cb0ef41Sopenharmony_ci    case BNE:
64821cb0ef41Sopenharmony_ci      BranchHelper(rs != rt);
64831cb0ef41Sopenharmony_ci      break;
64841cb0ef41Sopenharmony_ci    case POP06:  // BLEZALC, BGEZALC, BGEUC, BLEZ (pre-r6)
64851cb0ef41Sopenharmony_ci      if (IsMipsArchVariant(kMips32r6)) {
64861cb0ef41Sopenharmony_ci        if (rt_reg != 0) {
64871cb0ef41Sopenharmony_ci          if (rs_reg == 0) {  // BLEZALC
64881cb0ef41Sopenharmony_ci            BranchAndLinkCompactHelper(rt <= 0, 16);
64891cb0ef41Sopenharmony_ci          } else {
64901cb0ef41Sopenharmony_ci            if (rs_reg == rt_reg) {  // BGEZALC
64911cb0ef41Sopenharmony_ci              BranchAndLinkCompactHelper(rt >= 0, 16);
64921cb0ef41Sopenharmony_ci            } else {  // BGEUC
64931cb0ef41Sopenharmony_ci              BranchCompactHelper(
64941cb0ef41Sopenharmony_ci                  static_cast<uint32_t>(rs) >= static_cast<uint32_t>(rt), 16);
64951cb0ef41Sopenharmony_ci            }
64961cb0ef41Sopenharmony_ci          }
64971cb0ef41Sopenharmony_ci        } else {  // BLEZ
64981cb0ef41Sopenharmony_ci          BranchHelper(rs <= 0);
64991cb0ef41Sopenharmony_ci        }
65001cb0ef41Sopenharmony_ci      } else {  // BLEZ
65011cb0ef41Sopenharmony_ci        BranchHelper(rs <= 0);
65021cb0ef41Sopenharmony_ci      }
65031cb0ef41Sopenharmony_ci      break;
65041cb0ef41Sopenharmony_ci    case POP07:  // BGTZALC, BLTZALC, BLTUC, BGTZ (pre-r6)
65051cb0ef41Sopenharmony_ci      if (IsMipsArchVariant(kMips32r6)) {
65061cb0ef41Sopenharmony_ci        if (rt_reg != 0) {
65071cb0ef41Sopenharmony_ci          if (rs_reg == 0) {  // BGTZALC
65081cb0ef41Sopenharmony_ci            BranchAndLinkCompactHelper(rt > 0, 16);
65091cb0ef41Sopenharmony_ci          } else {
65101cb0ef41Sopenharmony_ci            if (rt_reg == rs_reg) {  // BLTZALC
65111cb0ef41Sopenharmony_ci              BranchAndLinkCompactHelper(rt < 0, 16);
65121cb0ef41Sopenharmony_ci            } else {  // BLTUC
65131cb0ef41Sopenharmony_ci              BranchCompactHelper(
65141cb0ef41Sopenharmony_ci                  static_cast<uint32_t>(rs) < static_cast<uint32_t>(rt), 16);
65151cb0ef41Sopenharmony_ci            }
65161cb0ef41Sopenharmony_ci          }
65171cb0ef41Sopenharmony_ci        } else {  // BGTZ
65181cb0ef41Sopenharmony_ci          BranchHelper(rs > 0);
65191cb0ef41Sopenharmony_ci        }
65201cb0ef41Sopenharmony_ci      } else {  // BGTZ
65211cb0ef41Sopenharmony_ci        BranchHelper(rs > 0);
65221cb0ef41Sopenharmony_ci      }
65231cb0ef41Sopenharmony_ci      break;
65241cb0ef41Sopenharmony_ci    case POP26:  // BLEZC, BGEZC, BGEC/BLEC / BLEZL (pre-r6)
65251cb0ef41Sopenharmony_ci      if (IsMipsArchVariant(kMips32r6)) {
65261cb0ef41Sopenharmony_ci        if (rt_reg != 0) {
65271cb0ef41Sopenharmony_ci          if (rs_reg == 0) {  // BLEZC
65281cb0ef41Sopenharmony_ci            BranchCompactHelper(rt <= 0, 16);
65291cb0ef41Sopenharmony_ci          } else {
65301cb0ef41Sopenharmony_ci            if (rs_reg == rt_reg) {  // BGEZC
65311cb0ef41Sopenharmony_ci              BranchCompactHelper(rt >= 0, 16);
65321cb0ef41Sopenharmony_ci            } else {  // BGEC/BLEC
65331cb0ef41Sopenharmony_ci              BranchCompactHelper(rs >= rt, 16);
65341cb0ef41Sopenharmony_ci            }
65351cb0ef41Sopenharmony_ci          }
65361cb0ef41Sopenharmony_ci        }
65371cb0ef41Sopenharmony_ci      } else {  // BLEZL
65381cb0ef41Sopenharmony_ci        BranchAndLinkHelper(rs <= 0);
65391cb0ef41Sopenharmony_ci      }
65401cb0ef41Sopenharmony_ci      break;
65411cb0ef41Sopenharmony_ci    case POP27:  // BGTZC, BLTZC, BLTC/BGTC / BGTZL (pre-r6)
65421cb0ef41Sopenharmony_ci      if (IsMipsArchVariant(kMips32r6)) {
65431cb0ef41Sopenharmony_ci        if (rt_reg != 0) {
65441cb0ef41Sopenharmony_ci          if (rs_reg == 0) {  // BGTZC
65451cb0ef41Sopenharmony_ci            BranchCompactHelper(rt > 0, 16);
65461cb0ef41Sopenharmony_ci          } else {
65471cb0ef41Sopenharmony_ci            if (rs_reg == rt_reg) {  // BLTZC
65481cb0ef41Sopenharmony_ci              BranchCompactHelper(rt < 0, 16);
65491cb0ef41Sopenharmony_ci            } else {  // BLTC/BGTC
65501cb0ef41Sopenharmony_ci              BranchCompactHelper(rs < rt, 16);
65511cb0ef41Sopenharmony_ci            }
65521cb0ef41Sopenharmony_ci          }
65531cb0ef41Sopenharmony_ci        }
65541cb0ef41Sopenharmony_ci      } else {  // BGTZL
65551cb0ef41Sopenharmony_ci        BranchAndLinkHelper(rs > 0);
65561cb0ef41Sopenharmony_ci      }
65571cb0ef41Sopenharmony_ci      break;
65581cb0ef41Sopenharmony_ci    case POP66:           // BEQZC, JIC
65591cb0ef41Sopenharmony_ci      if (rs_reg != 0) {  // BEQZC
65601cb0ef41Sopenharmony_ci        BranchCompactHelper(rs == 0, 21);
65611cb0ef41Sopenharmony_ci      } else {  // JIC
65621cb0ef41Sopenharmony_ci        next_pc = rt + imm16;
65631cb0ef41Sopenharmony_ci      }
65641cb0ef41Sopenharmony_ci      break;
65651cb0ef41Sopenharmony_ci    case POP76:           // BNEZC, JIALC
65661cb0ef41Sopenharmony_ci      if (rs_reg != 0) {  // BNEZC
65671cb0ef41Sopenharmony_ci        BranchCompactHelper(rs != 0, 21);
65681cb0ef41Sopenharmony_ci      } else {  // JIALC
65691cb0ef41Sopenharmony_ci        set_register(31, get_pc() + kInstrSize);
65701cb0ef41Sopenharmony_ci        next_pc = rt + imm16;
65711cb0ef41Sopenharmony_ci      }
65721cb0ef41Sopenharmony_ci      break;
65731cb0ef41Sopenharmony_ci    case BC:
65741cb0ef41Sopenharmony_ci      BranchCompactHelper(true, 26);
65751cb0ef41Sopenharmony_ci      break;
65761cb0ef41Sopenharmony_ci    case BALC:
65771cb0ef41Sopenharmony_ci      BranchAndLinkCompactHelper(true, 26);
65781cb0ef41Sopenharmony_ci      break;
65791cb0ef41Sopenharmony_ci    case POP10:  // BOVC, BEQZALC, BEQC / ADDI (pre-r6)
65801cb0ef41Sopenharmony_ci      if (IsMipsArchVariant(kMips32r6)) {
65811cb0ef41Sopenharmony_ci        if (rs_reg >= rt_reg) {  // BOVC
65821cb0ef41Sopenharmony_ci          if (HaveSameSign(rs, rt)) {
65831cb0ef41Sopenharmony_ci            if (rs > 0) {
65841cb0ef41Sopenharmony_ci              BranchCompactHelper(rs > Registers::kMaxValue - rt, 16);
65851cb0ef41Sopenharmony_ci            } else if (rs < 0) {
65861cb0ef41Sopenharmony_ci              BranchCompactHelper(rs < Registers::kMinValue - rt, 16);
65871cb0ef41Sopenharmony_ci            }
65881cb0ef41Sopenharmony_ci          }
65891cb0ef41Sopenharmony_ci        } else {
65901cb0ef41Sopenharmony_ci          if (rs_reg == 0) {  // BEQZALC
65911cb0ef41Sopenharmony_ci            BranchAndLinkCompactHelper(rt == 0, 16);
65921cb0ef41Sopenharmony_ci          } else {  // BEQC
65931cb0ef41Sopenharmony_ci            BranchCompactHelper(rt == rs, 16);
65941cb0ef41Sopenharmony_ci          }
65951cb0ef41Sopenharmony_ci        }
65961cb0ef41Sopenharmony_ci      } else {  // ADDI
65971cb0ef41Sopenharmony_ci        if (HaveSameSign(rs, se_imm16)) {
65981cb0ef41Sopenharmony_ci          if (rs > 0) {
65991cb0ef41Sopenharmony_ci            if (rs <= Registers::kMaxValue - se_imm16) {
66001cb0ef41Sopenharmony_ci              SignalException(kIntegerOverflow);
66011cb0ef41Sopenharmony_ci            }
66021cb0ef41Sopenharmony_ci          } else if (rs < 0) {
66031cb0ef41Sopenharmony_ci            if (rs >= Registers::kMinValue - se_imm16) {
66041cb0ef41Sopenharmony_ci              SignalException(kIntegerUnderflow);
66051cb0ef41Sopenharmony_ci            }
66061cb0ef41Sopenharmony_ci          }
66071cb0ef41Sopenharmony_ci        }
66081cb0ef41Sopenharmony_ci        SetResult(rt_reg, rs + se_imm16);
66091cb0ef41Sopenharmony_ci      }
66101cb0ef41Sopenharmony_ci      break;
66111cb0ef41Sopenharmony_ci    case POP30:  // BNVC, BNEZALC, BNEC / DADDI (pre-r6)
66121cb0ef41Sopenharmony_ci      if (IsMipsArchVariant(kMips32r6)) {
66131cb0ef41Sopenharmony_ci        if (rs_reg >= rt_reg) {  // BNVC
66141cb0ef41Sopenharmony_ci          if (!HaveSameSign(rs, rt) || rs == 0 || rt == 0) {
66151cb0ef41Sopenharmony_ci            BranchCompactHelper(true, 16);
66161cb0ef41Sopenharmony_ci          } else {
66171cb0ef41Sopenharmony_ci            if (rs > 0) {
66181cb0ef41Sopenharmony_ci              BranchCompactHelper(rs <= Registers::kMaxValue - rt, 16);
66191cb0ef41Sopenharmony_ci            } else if (rs < 0) {
66201cb0ef41Sopenharmony_ci              BranchCompactHelper(rs >= Registers::kMinValue - rt, 16);
66211cb0ef41Sopenharmony_ci            }
66221cb0ef41Sopenharmony_ci          }
66231cb0ef41Sopenharmony_ci        } else {
66241cb0ef41Sopenharmony_ci          if (rs_reg == 0) {  // BNEZALC
66251cb0ef41Sopenharmony_ci            BranchAndLinkCompactHelper(rt != 0, 16);
66261cb0ef41Sopenharmony_ci          } else {  // BNEC
66271cb0ef41Sopenharmony_ci            BranchCompactHelper(rt != rs, 16);
66281cb0ef41Sopenharmony_ci          }
66291cb0ef41Sopenharmony_ci        }
66301cb0ef41Sopenharmony_ci      }
66311cb0ef41Sopenharmony_ci      break;
66321cb0ef41Sopenharmony_ci    // ------------- Arithmetic instructions.
66331cb0ef41Sopenharmony_ci    case ADDIU:
66341cb0ef41Sopenharmony_ci      SetResult(rt_reg, rs + se_imm16);
66351cb0ef41Sopenharmony_ci      break;
66361cb0ef41Sopenharmony_ci    case SLTI:
66371cb0ef41Sopenharmony_ci      SetResult(rt_reg, rs < se_imm16 ? 1 : 0);
66381cb0ef41Sopenharmony_ci      break;
66391cb0ef41Sopenharmony_ci    case SLTIU:
66401cb0ef41Sopenharmony_ci      SetResult(rt_reg, rs_u < static_cast<uint32_t>(se_imm16) ? 1 : 0);
66411cb0ef41Sopenharmony_ci      break;
66421cb0ef41Sopenharmony_ci    case ANDI:
66431cb0ef41Sopenharmony_ci      SetResult(rt_reg, rs & oe_imm16);
66441cb0ef41Sopenharmony_ci      break;
66451cb0ef41Sopenharmony_ci    case ORI:
66461cb0ef41Sopenharmony_ci      SetResult(rt_reg, rs | oe_imm16);
66471cb0ef41Sopenharmony_ci      break;
66481cb0ef41Sopenharmony_ci    case XORI:
66491cb0ef41Sopenharmony_ci      SetResult(rt_reg, rs ^ oe_imm16);
66501cb0ef41Sopenharmony_ci      break;
66511cb0ef41Sopenharmony_ci    case LUI:
66521cb0ef41Sopenharmony_ci      if (rs_reg != 0) {
66531cb0ef41Sopenharmony_ci        // AUI
66541cb0ef41Sopenharmony_ci        DCHECK(IsMipsArchVariant(kMips32r6));
66551cb0ef41Sopenharmony_ci        SetResult(rt_reg, rs + (se_imm16 << 16));
66561cb0ef41Sopenharmony_ci      } else {
66571cb0ef41Sopenharmony_ci        // LUI
66581cb0ef41Sopenharmony_ci        SetResult(rt_reg, oe_imm16 << 16);
66591cb0ef41Sopenharmony_ci      }
66601cb0ef41Sopenharmony_ci      break;
66611cb0ef41Sopenharmony_ci    // ------------- Memory instructions.
66621cb0ef41Sopenharmony_ci    case LB:
66631cb0ef41Sopenharmony_ci      set_register(rt_reg, ReadB(rs + se_imm16));
66641cb0ef41Sopenharmony_ci      break;
66651cb0ef41Sopenharmony_ci    case LH:
66661cb0ef41Sopenharmony_ci      set_register(rt_reg, ReadH(rs + se_imm16, instr_.instr()));
66671cb0ef41Sopenharmony_ci      break;
66681cb0ef41Sopenharmony_ci    case LWL: {
66691cb0ef41Sopenharmony_ci      // al_offset is offset of the effective address within an aligned word.
66701cb0ef41Sopenharmony_ci      uint8_t al_offset = (rs + se_imm16) & kPointerAlignmentMask;
66711cb0ef41Sopenharmony_ci      uint8_t byte_shift = kPointerAlignmentMask - al_offset;
66721cb0ef41Sopenharmony_ci      uint32_t mask = (1 << byte_shift * 8) - 1;
66731cb0ef41Sopenharmony_ci      addr = rs + se_imm16 - al_offset;
66741cb0ef41Sopenharmony_ci      alu_out = ReadW(addr, instr_.instr());
66751cb0ef41Sopenharmony_ci      alu_out <<= byte_shift * 8;
66761cb0ef41Sopenharmony_ci      alu_out |= rt & mask;
66771cb0ef41Sopenharmony_ci      set_register(rt_reg, alu_out);
66781cb0ef41Sopenharmony_ci      break;
66791cb0ef41Sopenharmony_ci    }
66801cb0ef41Sopenharmony_ci    case LW:
66811cb0ef41Sopenharmony_ci      set_register(rt_reg, ReadW(rs + se_imm16, instr_.instr()));
66821cb0ef41Sopenharmony_ci      break;
66831cb0ef41Sopenharmony_ci    case LBU:
66841cb0ef41Sopenharmony_ci      set_register(rt_reg, ReadBU(rs + se_imm16));
66851cb0ef41Sopenharmony_ci      break;
66861cb0ef41Sopenharmony_ci    case LHU:
66871cb0ef41Sopenharmony_ci      set_register(rt_reg, ReadHU(rs + se_imm16, instr_.instr()));
66881cb0ef41Sopenharmony_ci      break;
66891cb0ef41Sopenharmony_ci    case LWR: {
66901cb0ef41Sopenharmony_ci      // al_offset is offset of the effective address within an aligned word.
66911cb0ef41Sopenharmony_ci      uint8_t al_offset = (rs + se_imm16) & kPointerAlignmentMask;
66921cb0ef41Sopenharmony_ci      uint8_t byte_shift = kPointerAlignmentMask - al_offset;
66931cb0ef41Sopenharmony_ci      uint32_t mask = al_offset ? (~0 << (byte_shift + 1) * 8) : 0;
66941cb0ef41Sopenharmony_ci      addr = rs + se_imm16 - al_offset;
66951cb0ef41Sopenharmony_ci      alu_out = ReadW(addr, instr_.instr());
66961cb0ef41Sopenharmony_ci      alu_out = static_cast<uint32_t>(alu_out) >> al_offset * 8;
66971cb0ef41Sopenharmony_ci      alu_out |= rt & mask;
66981cb0ef41Sopenharmony_ci      set_register(rt_reg, alu_out);
66991cb0ef41Sopenharmony_ci      break;
67001cb0ef41Sopenharmony_ci    }
67011cb0ef41Sopenharmony_ci    case SB:
67021cb0ef41Sopenharmony_ci      WriteB(rs + se_imm16, static_cast<int8_t>(rt));
67031cb0ef41Sopenharmony_ci      break;
67041cb0ef41Sopenharmony_ci    case SH:
67051cb0ef41Sopenharmony_ci      WriteH(rs + se_imm16, static_cast<uint16_t>(rt), instr_.instr());
67061cb0ef41Sopenharmony_ci      break;
67071cb0ef41Sopenharmony_ci    case SWL: {
67081cb0ef41Sopenharmony_ci      uint8_t al_offset = (rs + se_imm16) & kPointerAlignmentMask;
67091cb0ef41Sopenharmony_ci      uint8_t byte_shift = kPointerAlignmentMask - al_offset;
67101cb0ef41Sopenharmony_ci      uint32_t mask = byte_shift ? (~0 << (al_offset + 1) * 8) : 0;
67111cb0ef41Sopenharmony_ci      addr = rs + se_imm16 - al_offset;
67121cb0ef41Sopenharmony_ci      // Value to be written in memory.
67131cb0ef41Sopenharmony_ci      uint32_t mem_value = ReadW(addr, instr_.instr()) & mask;
67141cb0ef41Sopenharmony_ci      mem_value |= static_cast<uint32_t>(rt) >> byte_shift * 8;
67151cb0ef41Sopenharmony_ci      WriteW(addr, mem_value, instr_.instr());
67161cb0ef41Sopenharmony_ci      break;
67171cb0ef41Sopenharmony_ci    }
67181cb0ef41Sopenharmony_ci    case SW:
67191cb0ef41Sopenharmony_ci      WriteW(rs + se_imm16, rt, instr_.instr());
67201cb0ef41Sopenharmony_ci      break;
67211cb0ef41Sopenharmony_ci    case SWR: {
67221cb0ef41Sopenharmony_ci      uint8_t al_offset = (rs + se_imm16) & kPointerAlignmentMask;
67231cb0ef41Sopenharmony_ci      uint32_t mask = (1 << al_offset * 8) - 1;
67241cb0ef41Sopenharmony_ci      addr = rs + se_imm16 - al_offset;
67251cb0ef41Sopenharmony_ci      uint32_t mem_value = ReadW(addr, instr_.instr());
67261cb0ef41Sopenharmony_ci      mem_value = (rt << al_offset * 8) | (mem_value & mask);
67271cb0ef41Sopenharmony_ci      WriteW(addr, mem_value, instr_.instr());
67281cb0ef41Sopenharmony_ci      break;
67291cb0ef41Sopenharmony_ci    }
67301cb0ef41Sopenharmony_ci    case LL: {
67311cb0ef41Sopenharmony_ci      DCHECK(!IsMipsArchVariant(kMips32r6));
67321cb0ef41Sopenharmony_ci      base::MutexGuard lock_guard(&GlobalMonitor::Get()->mutex);
67331cb0ef41Sopenharmony_ci      addr = rs + se_imm16;
67341cb0ef41Sopenharmony_ci      set_register(rt_reg, ReadW(addr, instr_.instr()));
67351cb0ef41Sopenharmony_ci      local_monitor_.NotifyLoadLinked(addr, TransactionSize::Word);
67361cb0ef41Sopenharmony_ci      GlobalMonitor::Get()->NotifyLoadLinked_Locked(addr,
67371cb0ef41Sopenharmony_ci                                                    &global_monitor_thread_);
67381cb0ef41Sopenharmony_ci      break;
67391cb0ef41Sopenharmony_ci    }
67401cb0ef41Sopenharmony_ci    case SC: {
67411cb0ef41Sopenharmony_ci      DCHECK(!IsMipsArchVariant(kMips32r6));
67421cb0ef41Sopenharmony_ci      addr = rs + se_imm16;
67431cb0ef41Sopenharmony_ci      WriteConditionalW(addr, rt, instr_.instr(), rt_reg);
67441cb0ef41Sopenharmony_ci      break;
67451cb0ef41Sopenharmony_ci    }
67461cb0ef41Sopenharmony_ci    case LWC1:
67471cb0ef41Sopenharmony_ci      set_fpu_register_hi_word(ft_reg, 0);
67481cb0ef41Sopenharmony_ci      set_fpu_register_word(ft_reg,
67491cb0ef41Sopenharmony_ci                            ReadW(rs + se_imm16, instr_.instr(), FLOAT));
67501cb0ef41Sopenharmony_ci      if (ft_reg % 2) {
67511cb0ef41Sopenharmony_ci        TraceMemRd(rs + se_imm16, get_fpu_register(ft_reg - 1), FLOAT_DOUBLE);
67521cb0ef41Sopenharmony_ci      } else {
67531cb0ef41Sopenharmony_ci        TraceMemRd(rs + se_imm16, get_fpu_register_word(ft_reg), FLOAT);
67541cb0ef41Sopenharmony_ci      }
67551cb0ef41Sopenharmony_ci      break;
67561cb0ef41Sopenharmony_ci    case LDC1:
67571cb0ef41Sopenharmony_ci      set_fpu_register_double(ft_reg, ReadD(rs + se_imm16, instr_.instr()));
67581cb0ef41Sopenharmony_ci      TraceMemRd(rs + se_imm16, get_fpu_register(ft_reg), DOUBLE);
67591cb0ef41Sopenharmony_ci      break;
67601cb0ef41Sopenharmony_ci    case SWC1:
67611cb0ef41Sopenharmony_ci      WriteW(rs + se_imm16, get_fpu_register_word(ft_reg), instr_.instr());
67621cb0ef41Sopenharmony_ci      TraceMemWr(rs + se_imm16, get_fpu_register_word(ft_reg));
67631cb0ef41Sopenharmony_ci      break;
67641cb0ef41Sopenharmony_ci    case SDC1:
67651cb0ef41Sopenharmony_ci      WriteD(rs + se_imm16, get_fpu_register_double(ft_reg), instr_.instr());
67661cb0ef41Sopenharmony_ci      TraceMemWr(rs + se_imm16, get_fpu_register(ft_reg));
67671cb0ef41Sopenharmony_ci      break;
67681cb0ef41Sopenharmony_ci    // ------------- PC-Relative instructions.
67691cb0ef41Sopenharmony_ci    case PCREL: {
67701cb0ef41Sopenharmony_ci      // rt field: checking 5-bits.
67711cb0ef41Sopenharmony_ci      int32_t imm21 = instr_.Imm21Value();
67721cb0ef41Sopenharmony_ci      int32_t current_pc = get_pc();
67731cb0ef41Sopenharmony_ci      uint8_t rt = (imm21 >> kImm16Bits);
67741cb0ef41Sopenharmony_ci      switch (rt) {
67751cb0ef41Sopenharmony_ci        case ALUIPC:
67761cb0ef41Sopenharmony_ci          addr = current_pc + (se_imm16 << 16);
67771cb0ef41Sopenharmony_ci          alu_out = static_cast<int64_t>(~0x0FFFF) & addr;
67781cb0ef41Sopenharmony_ci          break;
67791cb0ef41Sopenharmony_ci        case AUIPC:
67801cb0ef41Sopenharmony_ci          alu_out = current_pc + (se_imm16 << 16);
67811cb0ef41Sopenharmony_ci          break;
67821cb0ef41Sopenharmony_ci        default: {
67831cb0ef41Sopenharmony_ci          int32_t imm19 = instr_.Imm19Value();
67841cb0ef41Sopenharmony_ci          // rt field: checking the most significant 2-bits.
67851cb0ef41Sopenharmony_ci          rt = (imm21 >> kImm19Bits);
67861cb0ef41Sopenharmony_ci          switch (rt) {
67871cb0ef41Sopenharmony_ci            case LWPC: {
67881cb0ef41Sopenharmony_ci              // Set sign.
67891cb0ef41Sopenharmony_ci              imm19 <<= (kOpcodeBits + kRsBits + 2);
67901cb0ef41Sopenharmony_ci              imm19 >>= (kOpcodeBits + kRsBits + 2);
67911cb0ef41Sopenharmony_ci              addr = current_pc + (imm19 << 2);
67921cb0ef41Sopenharmony_ci              uint32_t* ptr = reinterpret_cast<uint32_t*>(addr);
67931cb0ef41Sopenharmony_ci              alu_out = *ptr;
67941cb0ef41Sopenharmony_ci              break;
67951cb0ef41Sopenharmony_ci            }
67961cb0ef41Sopenharmony_ci            case ADDIUPC: {
67971cb0ef41Sopenharmony_ci              int32_t se_imm19 = imm19 | ((imm19 & 0x40000) ? 0xFFF80000 : 0);
67981cb0ef41Sopenharmony_ci              alu_out = current_pc + (se_imm19 << 2);
67991cb0ef41Sopenharmony_ci              break;
68001cb0ef41Sopenharmony_ci            }
68011cb0ef41Sopenharmony_ci            default:
68021cb0ef41Sopenharmony_ci              UNREACHABLE();
68031cb0ef41Sopenharmony_ci          }
68041cb0ef41Sopenharmony_ci        }
68051cb0ef41Sopenharmony_ci      }
68061cb0ef41Sopenharmony_ci      SetResult(rs_reg, alu_out);
68071cb0ef41Sopenharmony_ci      break;
68081cb0ef41Sopenharmony_ci    }
68091cb0ef41Sopenharmony_ci    case SPECIAL3: {
68101cb0ef41Sopenharmony_ci      switch (instr_.FunctionFieldRaw()) {
68111cb0ef41Sopenharmony_ci        case LL_R6: {
68121cb0ef41Sopenharmony_ci          DCHECK(IsMipsArchVariant(kMips32r6));
68131cb0ef41Sopenharmony_ci          base::MutexGuard lock_guard(&GlobalMonitor::Get()->mutex);
68141cb0ef41Sopenharmony_ci          int32_t base = get_register(instr_.BaseValue());
68151cb0ef41Sopenharmony_ci          int32_t offset9 = instr_.Imm9Value();
68161cb0ef41Sopenharmony_ci          addr = base + offset9;
68171cb0ef41Sopenharmony_ci          DCHECK_EQ(addr & kPointerAlignmentMask, 0);
68181cb0ef41Sopenharmony_ci          set_register(rt_reg, ReadW(base + offset9, instr_.instr()));
68191cb0ef41Sopenharmony_ci          local_monitor_.NotifyLoadLinked(addr, TransactionSize::Word);
68201cb0ef41Sopenharmony_ci          GlobalMonitor::Get()->NotifyLoadLinked_Locked(
68211cb0ef41Sopenharmony_ci              addr, &global_monitor_thread_);
68221cb0ef41Sopenharmony_ci          break;
68231cb0ef41Sopenharmony_ci        }
68241cb0ef41Sopenharmony_ci        case SC_R6: {
68251cb0ef41Sopenharmony_ci          DCHECK(IsMipsArchVariant(kMips32r6));
68261cb0ef41Sopenharmony_ci          int32_t base = get_register(instr_.BaseValue());
68271cb0ef41Sopenharmony_ci          int32_t offset9 = instr_.Imm9Value();
68281cb0ef41Sopenharmony_ci          addr = base + offset9;
68291cb0ef41Sopenharmony_ci          DCHECK_EQ(addr & kPointerAlignmentMask, 0);
68301cb0ef41Sopenharmony_ci          WriteConditionalW(addr, rt, instr_.instr(), rt_reg);
68311cb0ef41Sopenharmony_ci          break;
68321cb0ef41Sopenharmony_ci        }
68331cb0ef41Sopenharmony_ci        default:
68341cb0ef41Sopenharmony_ci          UNREACHABLE();
68351cb0ef41Sopenharmony_ci      }
68361cb0ef41Sopenharmony_ci      break;
68371cb0ef41Sopenharmony_ci    }
68381cb0ef41Sopenharmony_ci    case MSA:
68391cb0ef41Sopenharmony_ci      switch (instr_.MSAMinorOpcodeField()) {
68401cb0ef41Sopenharmony_ci        case kMsaMinorI8:
68411cb0ef41Sopenharmony_ci          DecodeTypeMsaI8();
68421cb0ef41Sopenharmony_ci          break;
68431cb0ef41Sopenharmony_ci        case kMsaMinorI5:
68441cb0ef41Sopenharmony_ci          DecodeTypeMsaI5();
68451cb0ef41Sopenharmony_ci          break;
68461cb0ef41Sopenharmony_ci        case kMsaMinorI10:
68471cb0ef41Sopenharmony_ci          DecodeTypeMsaI10();
68481cb0ef41Sopenharmony_ci          break;
68491cb0ef41Sopenharmony_ci        case kMsaMinorELM:
68501cb0ef41Sopenharmony_ci          DecodeTypeMsaELM();
68511cb0ef41Sopenharmony_ci          break;
68521cb0ef41Sopenharmony_ci        case kMsaMinorBIT:
68531cb0ef41Sopenharmony_ci          DecodeTypeMsaBIT();
68541cb0ef41Sopenharmony_ci          break;
68551cb0ef41Sopenharmony_ci        case kMsaMinorMI10:
68561cb0ef41Sopenharmony_ci          DecodeTypeMsaMI10();
68571cb0ef41Sopenharmony_ci          break;
68581cb0ef41Sopenharmony_ci        default:
68591cb0ef41Sopenharmony_ci          UNREACHABLE();
68601cb0ef41Sopenharmony_ci      }
68611cb0ef41Sopenharmony_ci      break;
68621cb0ef41Sopenharmony_ci    default:
68631cb0ef41Sopenharmony_ci      UNREACHABLE();
68641cb0ef41Sopenharmony_ci  }
68651cb0ef41Sopenharmony_ci
68661cb0ef41Sopenharmony_ci  if (execute_branch_delay_instruction) {
68671cb0ef41Sopenharmony_ci    // Execute branch delay slot
68681cb0ef41Sopenharmony_ci    // We don't check for end_sim_pc. First it should not be met as the current
68691cb0ef41Sopenharmony_ci    // pc is valid. Secondly a jump should always execute its branch delay slot.
68701cb0ef41Sopenharmony_ci    Instruction* branch_delay_instr =
68711cb0ef41Sopenharmony_ci        reinterpret_cast<Instruction*>(get_pc() + kInstrSize);
68721cb0ef41Sopenharmony_ci    BranchDelayInstructionDecode(branch_delay_instr);
68731cb0ef41Sopenharmony_ci  }
68741cb0ef41Sopenharmony_ci
68751cb0ef41Sopenharmony_ci  // If needed update pc after the branch delay execution.
68761cb0ef41Sopenharmony_ci  if (next_pc != bad_ra) {
68771cb0ef41Sopenharmony_ci    set_pc(next_pc);
68781cb0ef41Sopenharmony_ci  }
68791cb0ef41Sopenharmony_ci}
68801cb0ef41Sopenharmony_ci
68811cb0ef41Sopenharmony_ci// Type 3: instructions using a 26 bytes immediate. (e.g. j, jal).
68821cb0ef41Sopenharmony_civoid Simulator::DecodeTypeJump() {
68831cb0ef41Sopenharmony_ci  // instr_ will be overwritten by BranchDelayInstructionDecode(), so we save
68841cb0ef41Sopenharmony_ci  // the result of IsLinkingInstruction now.
68851cb0ef41Sopenharmony_ci  bool isLinkingInstr = instr_.IsLinkingInstruction();
68861cb0ef41Sopenharmony_ci  // Get current pc.
68871cb0ef41Sopenharmony_ci  int32_t current_pc = get_pc();
68881cb0ef41Sopenharmony_ci  // Get unchanged bits of pc.
68891cb0ef41Sopenharmony_ci  int32_t pc_high_bits = current_pc & 0xF0000000;
68901cb0ef41Sopenharmony_ci  // Next pc.
68911cb0ef41Sopenharmony_ci
68921cb0ef41Sopenharmony_ci  int32_t next_pc = pc_high_bits | (instr_.Imm26Value() << 2);
68931cb0ef41Sopenharmony_ci
68941cb0ef41Sopenharmony_ci  // Execute branch delay slot.
68951cb0ef41Sopenharmony_ci  // We don't check for end_sim_pc. First it should not be met as the current pc
68961cb0ef41Sopenharmony_ci  // is valid. Secondly a jump should always execute its branch delay slot.
68971cb0ef41Sopenharmony_ci  Instruction* branch_delay_instr =
68981cb0ef41Sopenharmony_ci      reinterpret_cast<Instruction*>(current_pc + kInstrSize);
68991cb0ef41Sopenharmony_ci  BranchDelayInstructionDecode(branch_delay_instr);
69001cb0ef41Sopenharmony_ci
69011cb0ef41Sopenharmony_ci  // Update pc and ra if necessary.
69021cb0ef41Sopenharmony_ci  // Do this after the branch delay execution.
69031cb0ef41Sopenharmony_ci  if (isLinkingInstr) {
69041cb0ef41Sopenharmony_ci    set_register(31, current_pc + 2 * kInstrSize);
69051cb0ef41Sopenharmony_ci  }
69061cb0ef41Sopenharmony_ci  set_pc(next_pc);
69071cb0ef41Sopenharmony_ci  pc_modified_ = true;
69081cb0ef41Sopenharmony_ci}
69091cb0ef41Sopenharmony_ci
69101cb0ef41Sopenharmony_ci// Executes the current instruction.
69111cb0ef41Sopenharmony_civoid Simulator::InstructionDecode(Instruction* instr) {
69121cb0ef41Sopenharmony_ci  if (v8::internal::FLAG_check_icache) {
69131cb0ef41Sopenharmony_ci    CheckICache(i_cache(), instr);
69141cb0ef41Sopenharmony_ci  }
69151cb0ef41Sopenharmony_ci  pc_modified_ = false;
69161cb0ef41Sopenharmony_ci  v8::base::EmbeddedVector<char, 256> buffer;
69171cb0ef41Sopenharmony_ci  if (::v8::internal::FLAG_trace_sim) {
69181cb0ef41Sopenharmony_ci    SNPrintF(trace_buf_, "%s", "");
69191cb0ef41Sopenharmony_ci    disasm::NameConverter converter;
69201cb0ef41Sopenharmony_ci    disasm::Disassembler dasm(converter);
69211cb0ef41Sopenharmony_ci    dasm.InstructionDecode(buffer, reinterpret_cast<byte*>(instr));
69221cb0ef41Sopenharmony_ci  }
69231cb0ef41Sopenharmony_ci
69241cb0ef41Sopenharmony_ci  instr_ = instr;
69251cb0ef41Sopenharmony_ci  switch (instr_.InstructionType()) {
69261cb0ef41Sopenharmony_ci    case Instruction::kRegisterType:
69271cb0ef41Sopenharmony_ci      DecodeTypeRegister();
69281cb0ef41Sopenharmony_ci      break;
69291cb0ef41Sopenharmony_ci    case Instruction::kImmediateType:
69301cb0ef41Sopenharmony_ci      DecodeTypeImmediate();
69311cb0ef41Sopenharmony_ci      break;
69321cb0ef41Sopenharmony_ci    case Instruction::kJumpType:
69331cb0ef41Sopenharmony_ci      DecodeTypeJump();
69341cb0ef41Sopenharmony_ci      break;
69351cb0ef41Sopenharmony_ci    default:
69361cb0ef41Sopenharmony_ci      UNSUPPORTED();
69371cb0ef41Sopenharmony_ci  }
69381cb0ef41Sopenharmony_ci  if (::v8::internal::FLAG_trace_sim) {
69391cb0ef41Sopenharmony_ci    PrintF("  0x%08" PRIxPTR "  %-44s   %s\n",
69401cb0ef41Sopenharmony_ci           reinterpret_cast<intptr_t>(instr), buffer.begin(),
69411cb0ef41Sopenharmony_ci           trace_buf_.begin());
69421cb0ef41Sopenharmony_ci  }
69431cb0ef41Sopenharmony_ci  if (!pc_modified_) {
69441cb0ef41Sopenharmony_ci    set_register(pc, reinterpret_cast<int32_t>(instr) + kInstrSize);
69451cb0ef41Sopenharmony_ci  }
69461cb0ef41Sopenharmony_ci}
69471cb0ef41Sopenharmony_ci
69481cb0ef41Sopenharmony_civoid Simulator::Execute() {
69491cb0ef41Sopenharmony_ci  // Get the PC to simulate. Cannot use the accessor here as we need the
69501cb0ef41Sopenharmony_ci  // raw PC value and not the one used as input to arithmetic instructions.
69511cb0ef41Sopenharmony_ci  int program_counter = get_pc();
69521cb0ef41Sopenharmony_ci  if (::v8::internal::FLAG_stop_sim_at == 0) {
69531cb0ef41Sopenharmony_ci    // Fast version of the dispatch loop without checking whether the simulator
69541cb0ef41Sopenharmony_ci    // should be stopping at a particular executed instruction.
69551cb0ef41Sopenharmony_ci    while (program_counter != end_sim_pc) {
69561cb0ef41Sopenharmony_ci      Instruction* instr = reinterpret_cast<Instruction*>(program_counter);
69571cb0ef41Sopenharmony_ci      icount_++;
69581cb0ef41Sopenharmony_ci      InstructionDecode(instr);
69591cb0ef41Sopenharmony_ci      program_counter = get_pc();
69601cb0ef41Sopenharmony_ci    }
69611cb0ef41Sopenharmony_ci  } else {
69621cb0ef41Sopenharmony_ci    // FLAG_stop_sim_at is at the non-default value. Stop in the debugger when
69631cb0ef41Sopenharmony_ci    // we reach the particular instruction count.
69641cb0ef41Sopenharmony_ci    while (program_counter != end_sim_pc) {
69651cb0ef41Sopenharmony_ci      Instruction* instr = reinterpret_cast<Instruction*>(program_counter);
69661cb0ef41Sopenharmony_ci      icount_++;
69671cb0ef41Sopenharmony_ci      if (icount_ == static_cast<uint64_t>(::v8::internal::FLAG_stop_sim_at)) {
69681cb0ef41Sopenharmony_ci        MipsDebugger dbg(this);
69691cb0ef41Sopenharmony_ci        dbg.Debug();
69701cb0ef41Sopenharmony_ci      } else {
69711cb0ef41Sopenharmony_ci        InstructionDecode(instr);
69721cb0ef41Sopenharmony_ci      }
69731cb0ef41Sopenharmony_ci      program_counter = get_pc();
69741cb0ef41Sopenharmony_ci    }
69751cb0ef41Sopenharmony_ci  }
69761cb0ef41Sopenharmony_ci}
69771cb0ef41Sopenharmony_ci
69781cb0ef41Sopenharmony_civoid Simulator::CallInternal(Address entry) {
69791cb0ef41Sopenharmony_ci  // Adjust JS-based stack limit to C-based stack limit.
69801cb0ef41Sopenharmony_ci  isolate_->stack_guard()->AdjustStackLimitForSimulator();
69811cb0ef41Sopenharmony_ci
69821cb0ef41Sopenharmony_ci  // Prepare to execute the code at entry.
69831cb0ef41Sopenharmony_ci  set_register(pc, static_cast<int32_t>(entry));
69841cb0ef41Sopenharmony_ci  // Put down marker for end of simulation. The simulator will stop simulation
69851cb0ef41Sopenharmony_ci  // when the PC reaches this value. By saving the "end simulation" value into
69861cb0ef41Sopenharmony_ci  // the LR the simulation stops when returning to this call point.
69871cb0ef41Sopenharmony_ci  set_register(ra, end_sim_pc);
69881cb0ef41Sopenharmony_ci
69891cb0ef41Sopenharmony_ci  // Remember the values of callee-saved registers.
69901cb0ef41Sopenharmony_ci  // The code below assumes that r9 is not used as sb (static base) in
69911cb0ef41Sopenharmony_ci  // simulator code and therefore is regarded as a callee-saved register.
69921cb0ef41Sopenharmony_ci  int32_t s0_val = get_register(s0);
69931cb0ef41Sopenharmony_ci  int32_t s1_val = get_register(s1);
69941cb0ef41Sopenharmony_ci  int32_t s2_val = get_register(s2);
69951cb0ef41Sopenharmony_ci  int32_t s3_val = get_register(s3);
69961cb0ef41Sopenharmony_ci  int32_t s4_val = get_register(s4);
69971cb0ef41Sopenharmony_ci  int32_t s5_val = get_register(s5);
69981cb0ef41Sopenharmony_ci  int32_t s6_val = get_register(s6);
69991cb0ef41Sopenharmony_ci  int32_t s7_val = get_register(s7);
70001cb0ef41Sopenharmony_ci  int32_t gp_val = get_register(gp);
70011cb0ef41Sopenharmony_ci  int32_t sp_val = get_register(sp);
70021cb0ef41Sopenharmony_ci  int32_t fp_val = get_register(fp);
70031cb0ef41Sopenharmony_ci
70041cb0ef41Sopenharmony_ci  // Set up the callee-saved registers with a known value. To be able to check
70051cb0ef41Sopenharmony_ci  // that they are preserved properly across JS execution.
70061cb0ef41Sopenharmony_ci  int32_t callee_saved_value = static_cast<int32_t>(icount_);
70071cb0ef41Sopenharmony_ci  set_register(s0, callee_saved_value);
70081cb0ef41Sopenharmony_ci  set_register(s1, callee_saved_value);
70091cb0ef41Sopenharmony_ci  set_register(s2, callee_saved_value);
70101cb0ef41Sopenharmony_ci  set_register(s3, callee_saved_value);
70111cb0ef41Sopenharmony_ci  set_register(s4, callee_saved_value);
70121cb0ef41Sopenharmony_ci  set_register(s5, callee_saved_value);
70131cb0ef41Sopenharmony_ci  set_register(s6, callee_saved_value);
70141cb0ef41Sopenharmony_ci  set_register(s7, callee_saved_value);
70151cb0ef41Sopenharmony_ci  set_register(gp, callee_saved_value);
70161cb0ef41Sopenharmony_ci  set_register(fp, callee_saved_value);
70171cb0ef41Sopenharmony_ci
70181cb0ef41Sopenharmony_ci  // Start the simulation.
70191cb0ef41Sopenharmony_ci  Execute();
70201cb0ef41Sopenharmony_ci
70211cb0ef41Sopenharmony_ci  // Check that the callee-saved registers have been preserved.
70221cb0ef41Sopenharmony_ci  CHECK_EQ(callee_saved_value, get_register(s0));
70231cb0ef41Sopenharmony_ci  CHECK_EQ(callee_saved_value, get_register(s1));
70241cb0ef41Sopenharmony_ci  CHECK_EQ(callee_saved_value, get_register(s2));
70251cb0ef41Sopenharmony_ci  CHECK_EQ(callee_saved_value, get_register(s3));
70261cb0ef41Sopenharmony_ci  CHECK_EQ(callee_saved_value, get_register(s4));
70271cb0ef41Sopenharmony_ci  CHECK_EQ(callee_saved_value, get_register(s5));
70281cb0ef41Sopenharmony_ci  CHECK_EQ(callee_saved_value, get_register(s6));
70291cb0ef41Sopenharmony_ci  CHECK_EQ(callee_saved_value, get_register(s7));
70301cb0ef41Sopenharmony_ci  CHECK_EQ(callee_saved_value, get_register(gp));
70311cb0ef41Sopenharmony_ci  CHECK_EQ(callee_saved_value, get_register(fp));
70321cb0ef41Sopenharmony_ci
70331cb0ef41Sopenharmony_ci  // Restore callee-saved registers with the original value.
70341cb0ef41Sopenharmony_ci  set_register(s0, s0_val);
70351cb0ef41Sopenharmony_ci  set_register(s1, s1_val);
70361cb0ef41Sopenharmony_ci  set_register(s2, s2_val);
70371cb0ef41Sopenharmony_ci  set_register(s3, s3_val);
70381cb0ef41Sopenharmony_ci  set_register(s4, s4_val);
70391cb0ef41Sopenharmony_ci  set_register(s5, s5_val);
70401cb0ef41Sopenharmony_ci  set_register(s6, s6_val);
70411cb0ef41Sopenharmony_ci  set_register(s7, s7_val);
70421cb0ef41Sopenharmony_ci  set_register(gp, gp_val);
70431cb0ef41Sopenharmony_ci  set_register(sp, sp_val);
70441cb0ef41Sopenharmony_ci  set_register(fp, fp_val);
70451cb0ef41Sopenharmony_ci}
70461cb0ef41Sopenharmony_ci
70471cb0ef41Sopenharmony_ciintptr_t Simulator::CallImpl(Address entry, int argument_count,
70481cb0ef41Sopenharmony_ci                             const intptr_t* arguments) {
70491cb0ef41Sopenharmony_ci  // Set up arguments.
70501cb0ef41Sopenharmony_ci
70511cb0ef41Sopenharmony_ci  // First four arguments passed in registers.
70521cb0ef41Sopenharmony_ci  int reg_arg_count = std::min(4, argument_count);
70531cb0ef41Sopenharmony_ci  if (reg_arg_count > 0) set_register(a0, arguments[0]);
70541cb0ef41Sopenharmony_ci  if (reg_arg_count > 1) set_register(a1, arguments[1]);
70551cb0ef41Sopenharmony_ci  if (reg_arg_count > 2) set_register(a2, arguments[2]);
70561cb0ef41Sopenharmony_ci  if (reg_arg_count > 3) set_register(a3, arguments[3]);
70571cb0ef41Sopenharmony_ci
70581cb0ef41Sopenharmony_ci  // Remaining arguments passed on stack.
70591cb0ef41Sopenharmony_ci  int original_stack = get_register(sp);
70601cb0ef41Sopenharmony_ci  // Compute position of stack on entry to generated code.
70611cb0ef41Sopenharmony_ci  int entry_stack = (original_stack - (argument_count - 4) * sizeof(int32_t) -
70621cb0ef41Sopenharmony_ci                     kCArgsSlotsSize);
70631cb0ef41Sopenharmony_ci  if (base::OS::ActivationFrameAlignment() != 0) {
70641cb0ef41Sopenharmony_ci    entry_stack &= -base::OS::ActivationFrameAlignment();
70651cb0ef41Sopenharmony_ci  }
70661cb0ef41Sopenharmony_ci  // Store remaining arguments on stack, from low to high memory.
70671cb0ef41Sopenharmony_ci  intptr_t* stack_argument = reinterpret_cast<intptr_t*>(entry_stack);
70681cb0ef41Sopenharmony_ci  memcpy(stack_argument + kCArgSlotCount, arguments + reg_arg_count,
70691cb0ef41Sopenharmony_ci         (argument_count - reg_arg_count) * sizeof(*arguments));
70701cb0ef41Sopenharmony_ci  set_register(sp, entry_stack);
70711cb0ef41Sopenharmony_ci
70721cb0ef41Sopenharmony_ci  CallInternal(entry);
70731cb0ef41Sopenharmony_ci
70741cb0ef41Sopenharmony_ci  // Pop stack passed arguments.
70751cb0ef41Sopenharmony_ci  CHECK_EQ(entry_stack, get_register(sp));
70761cb0ef41Sopenharmony_ci  set_register(sp, original_stack);
70771cb0ef41Sopenharmony_ci
70781cb0ef41Sopenharmony_ci  return get_register(v0);
70791cb0ef41Sopenharmony_ci}
70801cb0ef41Sopenharmony_ci
70811cb0ef41Sopenharmony_cidouble Simulator::CallFP(Address entry, double d0, double d1) {
70821cb0ef41Sopenharmony_ci  if (!IsMipsSoftFloatABI) {
70831cb0ef41Sopenharmony_ci    set_fpu_register_double(f12, d0);
70841cb0ef41Sopenharmony_ci    set_fpu_register_double(f14, d1);
70851cb0ef41Sopenharmony_ci  } else {
70861cb0ef41Sopenharmony_ci    int buffer[2];
70871cb0ef41Sopenharmony_ci    DCHECK(sizeof(buffer[0]) * 2 == sizeof(d0));
70881cb0ef41Sopenharmony_ci    memcpy(buffer, &d0, sizeof(d0));
70891cb0ef41Sopenharmony_ci    set_dw_register(a0, buffer);
70901cb0ef41Sopenharmony_ci    memcpy(buffer, &d1, sizeof(d1));
70911cb0ef41Sopenharmony_ci    set_dw_register(a2, buffer);
70921cb0ef41Sopenharmony_ci  }
70931cb0ef41Sopenharmony_ci  CallInternal(entry);
70941cb0ef41Sopenharmony_ci  if (!IsMipsSoftFloatABI) {
70951cb0ef41Sopenharmony_ci    return get_fpu_register_double(f0);
70961cb0ef41Sopenharmony_ci  } else {
70971cb0ef41Sopenharmony_ci    return get_double_from_register_pair(v0);
70981cb0ef41Sopenharmony_ci  }
70991cb0ef41Sopenharmony_ci}
71001cb0ef41Sopenharmony_ci
71011cb0ef41Sopenharmony_ciuintptr_t Simulator::PushAddress(uintptr_t address) {
71021cb0ef41Sopenharmony_ci  int new_sp = get_register(sp) - sizeof(uintptr_t);
71031cb0ef41Sopenharmony_ci  uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(new_sp);
71041cb0ef41Sopenharmony_ci  *stack_slot = address;
71051cb0ef41Sopenharmony_ci  set_register(sp, new_sp);
71061cb0ef41Sopenharmony_ci  return new_sp;
71071cb0ef41Sopenharmony_ci}
71081cb0ef41Sopenharmony_ci
71091cb0ef41Sopenharmony_ciuintptr_t Simulator::PopAddress() {
71101cb0ef41Sopenharmony_ci  int current_sp = get_register(sp);
71111cb0ef41Sopenharmony_ci  uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(current_sp);
71121cb0ef41Sopenharmony_ci  uintptr_t address = *stack_slot;
71131cb0ef41Sopenharmony_ci  set_register(sp, current_sp + sizeof(uintptr_t));
71141cb0ef41Sopenharmony_ci  return address;
71151cb0ef41Sopenharmony_ci}
71161cb0ef41Sopenharmony_ci
71171cb0ef41Sopenharmony_ciSimulator::LocalMonitor::LocalMonitor()
71181cb0ef41Sopenharmony_ci    : access_state_(MonitorAccess::Open),
71191cb0ef41Sopenharmony_ci      tagged_addr_(0),
71201cb0ef41Sopenharmony_ci      size_(TransactionSize::None) {}
71211cb0ef41Sopenharmony_ci
71221cb0ef41Sopenharmony_civoid Simulator::LocalMonitor::Clear() {
71231cb0ef41Sopenharmony_ci  access_state_ = MonitorAccess::Open;
71241cb0ef41Sopenharmony_ci  tagged_addr_ = 0;
71251cb0ef41Sopenharmony_ci  size_ = TransactionSize::None;
71261cb0ef41Sopenharmony_ci}
71271cb0ef41Sopenharmony_ci
71281cb0ef41Sopenharmony_civoid Simulator::LocalMonitor::NotifyLoad() {
71291cb0ef41Sopenharmony_ci  if (access_state_ == MonitorAccess::RMW) {
71301cb0ef41Sopenharmony_ci    // A non linked load could clear the local monitor. As a result, it's
71311cb0ef41Sopenharmony_ci    // most strict to unconditionally clear the local monitor on load.
71321cb0ef41Sopenharmony_ci    Clear();
71331cb0ef41Sopenharmony_ci  }
71341cb0ef41Sopenharmony_ci}
71351cb0ef41Sopenharmony_ci
71361cb0ef41Sopenharmony_civoid Simulator::LocalMonitor::NotifyLoadLinked(uintptr_t addr,
71371cb0ef41Sopenharmony_ci                                               TransactionSize size) {
71381cb0ef41Sopenharmony_ci  access_state_ = MonitorAccess::RMW;
71391cb0ef41Sopenharmony_ci  tagged_addr_ = addr;
71401cb0ef41Sopenharmony_ci  size_ = size;
71411cb0ef41Sopenharmony_ci}
71421cb0ef41Sopenharmony_ci
71431cb0ef41Sopenharmony_civoid Simulator::LocalMonitor::NotifyStore() {
71441cb0ef41Sopenharmony_ci  if (access_state_ == MonitorAccess::RMW) {
71451cb0ef41Sopenharmony_ci    // A non exclusive store could clear the local monitor. As a result, it's
71461cb0ef41Sopenharmony_ci    // most strict to unconditionally clear the local monitor on store.
71471cb0ef41Sopenharmony_ci    Clear();
71481cb0ef41Sopenharmony_ci  }
71491cb0ef41Sopenharmony_ci}
71501cb0ef41Sopenharmony_ci
71511cb0ef41Sopenharmony_cibool Simulator::LocalMonitor::NotifyStoreConditional(uintptr_t addr,
71521cb0ef41Sopenharmony_ci                                                     TransactionSize size) {
71531cb0ef41Sopenharmony_ci  if (access_state_ == MonitorAccess::RMW) {
71541cb0ef41Sopenharmony_ci    if (addr == tagged_addr_ && size_ == size) {
71551cb0ef41Sopenharmony_ci      Clear();
71561cb0ef41Sopenharmony_ci      return true;
71571cb0ef41Sopenharmony_ci    } else {
71581cb0ef41Sopenharmony_ci      return false;
71591cb0ef41Sopenharmony_ci    }
71601cb0ef41Sopenharmony_ci  } else {
71611cb0ef41Sopenharmony_ci    DCHECK(access_state_ == MonitorAccess::Open);
71621cb0ef41Sopenharmony_ci    return false;
71631cb0ef41Sopenharmony_ci  }
71641cb0ef41Sopenharmony_ci}
71651cb0ef41Sopenharmony_ci
71661cb0ef41Sopenharmony_ciSimulator::GlobalMonitor::LinkedAddress::LinkedAddress()
71671cb0ef41Sopenharmony_ci    : access_state_(MonitorAccess::Open),
71681cb0ef41Sopenharmony_ci      tagged_addr_(0),
71691cb0ef41Sopenharmony_ci      next_(nullptr),
71701cb0ef41Sopenharmony_ci      prev_(nullptr),
71711cb0ef41Sopenharmony_ci      failure_counter_(0) {}
71721cb0ef41Sopenharmony_ci
71731cb0ef41Sopenharmony_civoid Simulator::GlobalMonitor::LinkedAddress::Clear_Locked() {
71741cb0ef41Sopenharmony_ci  access_state_ = MonitorAccess::Open;
71751cb0ef41Sopenharmony_ci  tagged_addr_ = 0;
71761cb0ef41Sopenharmony_ci}
71771cb0ef41Sopenharmony_ci
71781cb0ef41Sopenharmony_civoid Simulator::GlobalMonitor::LinkedAddress::NotifyLoadLinked_Locked(
71791cb0ef41Sopenharmony_ci    uintptr_t addr) {
71801cb0ef41Sopenharmony_ci  access_state_ = MonitorAccess::RMW;
71811cb0ef41Sopenharmony_ci  tagged_addr_ = addr;
71821cb0ef41Sopenharmony_ci}
71831cb0ef41Sopenharmony_ci
71841cb0ef41Sopenharmony_civoid Simulator::GlobalMonitor::LinkedAddress::NotifyStore_Locked() {
71851cb0ef41Sopenharmony_ci  if (access_state_ == MonitorAccess::RMW) {
71861cb0ef41Sopenharmony_ci    // A non exclusive store could clear the global monitor. As a result, it's
71871cb0ef41Sopenharmony_ci    // most strict to unconditionally clear global monitors on store.
71881cb0ef41Sopenharmony_ci    Clear_Locked();
71891cb0ef41Sopenharmony_ci  }
71901cb0ef41Sopenharmony_ci}
71911cb0ef41Sopenharmony_ci
71921cb0ef41Sopenharmony_cibool Simulator::GlobalMonitor::LinkedAddress::NotifyStoreConditional_Locked(
71931cb0ef41Sopenharmony_ci    uintptr_t addr, bool is_requesting_processor) {
71941cb0ef41Sopenharmony_ci  if (access_state_ == MonitorAccess::RMW) {
71951cb0ef41Sopenharmony_ci    if (is_requesting_processor) {
71961cb0ef41Sopenharmony_ci      if (addr == tagged_addr_) {
71971cb0ef41Sopenharmony_ci        Clear_Locked();
71981cb0ef41Sopenharmony_ci        // Introduce occasional sc/scd failures. This is to simulate the
71991cb0ef41Sopenharmony_ci        // behavior of hardware, which can randomly fail due to background
72001cb0ef41Sopenharmony_ci        // cache evictions.
72011cb0ef41Sopenharmony_ci        if (failure_counter_++ >= kMaxFailureCounter) {
72021cb0ef41Sopenharmony_ci          failure_counter_ = 0;
72031cb0ef41Sopenharmony_ci          return false;
72041cb0ef41Sopenharmony_ci        } else {
72051cb0ef41Sopenharmony_ci          return true;
72061cb0ef41Sopenharmony_ci        }
72071cb0ef41Sopenharmony_ci      }
72081cb0ef41Sopenharmony_ci    } else if ((addr & kExclusiveTaggedAddrMask) ==
72091cb0ef41Sopenharmony_ci               (tagged_addr_ & kExclusiveTaggedAddrMask)) {
72101cb0ef41Sopenharmony_ci      // Check the masked addresses when responding to a successful lock by
72111cb0ef41Sopenharmony_ci      // another thread so the implementation is more conservative (i.e. the
72121cb0ef41Sopenharmony_ci      // granularity of locking is as large as possible.)
72131cb0ef41Sopenharmony_ci      Clear_Locked();
72141cb0ef41Sopenharmony_ci      return false;
72151cb0ef41Sopenharmony_ci    }
72161cb0ef41Sopenharmony_ci  }
72171cb0ef41Sopenharmony_ci  return false;
72181cb0ef41Sopenharmony_ci}
72191cb0ef41Sopenharmony_ci
72201cb0ef41Sopenharmony_civoid Simulator::GlobalMonitor::NotifyLoadLinked_Locked(
72211cb0ef41Sopenharmony_ci    uintptr_t addr, LinkedAddress* linked_address) {
72221cb0ef41Sopenharmony_ci  linked_address->NotifyLoadLinked_Locked(addr);
72231cb0ef41Sopenharmony_ci  PrependProcessor_Locked(linked_address);
72241cb0ef41Sopenharmony_ci}
72251cb0ef41Sopenharmony_ci
72261cb0ef41Sopenharmony_civoid Simulator::GlobalMonitor::NotifyStore_Locked(
72271cb0ef41Sopenharmony_ci    LinkedAddress* linked_address) {
72281cb0ef41Sopenharmony_ci  // Notify each thread of the store operation.
72291cb0ef41Sopenharmony_ci  for (LinkedAddress* iter = head_; iter; iter = iter->next_) {
72301cb0ef41Sopenharmony_ci    iter->NotifyStore_Locked();
72311cb0ef41Sopenharmony_ci  }
72321cb0ef41Sopenharmony_ci}
72331cb0ef41Sopenharmony_ci
72341cb0ef41Sopenharmony_cibool Simulator::GlobalMonitor::NotifyStoreConditional_Locked(
72351cb0ef41Sopenharmony_ci    uintptr_t addr, LinkedAddress* linked_address) {
72361cb0ef41Sopenharmony_ci  DCHECK(IsProcessorInLinkedList_Locked(linked_address));
72371cb0ef41Sopenharmony_ci  if (linked_address->NotifyStoreConditional_Locked(addr, true)) {
72381cb0ef41Sopenharmony_ci    // Notify the other processors that this StoreConditional succeeded.
72391cb0ef41Sopenharmony_ci    for (LinkedAddress* iter = head_; iter; iter = iter->next_) {
72401cb0ef41Sopenharmony_ci      if (iter != linked_address) {
72411cb0ef41Sopenharmony_ci        iter->NotifyStoreConditional_Locked(addr, false);
72421cb0ef41Sopenharmony_ci      }
72431cb0ef41Sopenharmony_ci    }
72441cb0ef41Sopenharmony_ci    return true;
72451cb0ef41Sopenharmony_ci  } else {
72461cb0ef41Sopenharmony_ci    return false;
72471cb0ef41Sopenharmony_ci  }
72481cb0ef41Sopenharmony_ci}
72491cb0ef41Sopenharmony_ci
72501cb0ef41Sopenharmony_cibool Simulator::GlobalMonitor::IsProcessorInLinkedList_Locked(
72511cb0ef41Sopenharmony_ci    LinkedAddress* linked_address) const {
72521cb0ef41Sopenharmony_ci  return head_ == linked_address || linked_address->next_ ||
72531cb0ef41Sopenharmony_ci         linked_address->prev_;
72541cb0ef41Sopenharmony_ci}
72551cb0ef41Sopenharmony_ci
72561cb0ef41Sopenharmony_civoid Simulator::GlobalMonitor::PrependProcessor_Locked(
72571cb0ef41Sopenharmony_ci    LinkedAddress* linked_address) {
72581cb0ef41Sopenharmony_ci  if (IsProcessorInLinkedList_Locked(linked_address)) {
72591cb0ef41Sopenharmony_ci    return;
72601cb0ef41Sopenharmony_ci  }
72611cb0ef41Sopenharmony_ci
72621cb0ef41Sopenharmony_ci  if (head_) {
72631cb0ef41Sopenharmony_ci    head_->prev_ = linked_address;
72641cb0ef41Sopenharmony_ci  }
72651cb0ef41Sopenharmony_ci  linked_address->prev_ = nullptr;
72661cb0ef41Sopenharmony_ci  linked_address->next_ = head_;
72671cb0ef41Sopenharmony_ci  head_ = linked_address;
72681cb0ef41Sopenharmony_ci}
72691cb0ef41Sopenharmony_ci
72701cb0ef41Sopenharmony_civoid Simulator::GlobalMonitor::RemoveLinkedAddress(
72711cb0ef41Sopenharmony_ci    LinkedAddress* linked_address) {
72721cb0ef41Sopenharmony_ci  base::MutexGuard lock_guard(&mutex);
72731cb0ef41Sopenharmony_ci  if (!IsProcessorInLinkedList_Locked(linked_address)) {
72741cb0ef41Sopenharmony_ci    return;
72751cb0ef41Sopenharmony_ci  }
72761cb0ef41Sopenharmony_ci
72771cb0ef41Sopenharmony_ci  if (linked_address->prev_) {
72781cb0ef41Sopenharmony_ci    linked_address->prev_->next_ = linked_address->next_;
72791cb0ef41Sopenharmony_ci  } else {
72801cb0ef41Sopenharmony_ci    head_ = linked_address->next_;
72811cb0ef41Sopenharmony_ci  }
72821cb0ef41Sopenharmony_ci  if (linked_address->next_) {
72831cb0ef41Sopenharmony_ci    linked_address->next_->prev_ = linked_address->prev_;
72841cb0ef41Sopenharmony_ci  }
72851cb0ef41Sopenharmony_ci  linked_address->prev_ = nullptr;
72861cb0ef41Sopenharmony_ci  linked_address->next_ = nullptr;
72871cb0ef41Sopenharmony_ci}
72881cb0ef41Sopenharmony_ci
72891cb0ef41Sopenharmony_ci#undef UNSUPPORTED
72901cb0ef41Sopenharmony_ci#undef SScanF
72911cb0ef41Sopenharmony_ci
72921cb0ef41Sopenharmony_ci}  // namespace internal
72931cb0ef41Sopenharmony_ci}  // namespace v8
72941cb0ef41Sopenharmony_ci
72951cb0ef41Sopenharmony_ci#endif  // USE_SIMULATOR
7296