:value". fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s\n", clr_reg_name, XRegNameForCode(code, r31mode), clr_reg_value, reg(code, r31mode), clr_normal); } void Simulator::PrintFPRegister(unsigned code, PrintFPRegisterSizes sizes) { // The template is "# v:bits (d:value, ...)". DCHECK(sizes != 0); DCHECK((sizes & kPrintAllFPRegValues) == sizes); // Print the raw bits. fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s (", clr_fpreg_name, VRegNameForCode(code), clr_fpreg_value, fpreg(code), clr_normal); // Print all requested value interpretations. bool need_separator = false; if (sizes & kPrintDRegValue) { fprintf(stream_, "%s%s%s: %s%g%s", need_separator ? ", " : "", clr_fpreg_name, DRegNameForCode(code), clr_fpreg_value, fpreg(code), clr_normal); need_separator = true; } if (sizes & kPrintSRegValue) { fprintf(stream_, "%s%s%s: %s%g%s", need_separator ? ", " : "", clr_fpreg_name, SRegNameForCode(code), clr_fpreg_value, fpreg(code), clr_normal); need_separator = true; } // End the value list. fprintf(stream_, ")\n"); } void Simulator::PrintSystemRegister(SystemRegister id) { switch (id) { case NZCV: fprintf(stream_, "# %sNZCV: %sN:%d Z:%d C:%d V:%d%s\n", clr_flag_name, clr_flag_value, nzcv().N(), nzcv().Z(), nzcv().C(), nzcv().V(), clr_normal); break; case FPCR: { static const char * rmode[] = { "0b00 (Round to Nearest)", "0b01 (Round towards Plus Infinity)", "0b10 (Round towards Minus Infinity)", "0b11 (Round towards Zero)" }; DCHECK(fpcr().RMode() < arraysize(rmode)); fprintf(stream_, "# %sFPCR: %sAHP:%d DN:%d FZ:%d RMode:%s%s\n", clr_flag_name, clr_flag_value, fpcr().AHP(), fpcr().DN(), fpcr().FZ(), rmode[fpcr().RMode()], clr_normal); break; } default: UNREACHABLE(); } } void Simulator::PrintRead(uintptr_t address, size_t size, unsigned reg_code) { USE(size); // Size is unused here. // The template is "# x:value <- address". fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s", clr_reg_name, XRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); fprintf(stream_, " <- %s0x%016" PRIxPTR "%s\n", clr_memory_address, address, clr_normal); } void Simulator::PrintReadFP(uintptr_t address, size_t size, unsigned reg_code) { // The template is "# reg:bits (reg:value) <- address". switch (size) { case kSRegSize: fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s (%s%s: %s%gf%s)", clr_fpreg_name, VRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal, clr_fpreg_name, SRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal); break; case kDRegSize: fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s (%s%s: %s%g%s)", clr_fpreg_name, VRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal, clr_fpreg_name, DRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal); break; default: UNREACHABLE(); } fprintf(stream_, " <- %s0x%016" PRIxPTR "%s\n", clr_memory_address, address, clr_normal); } void Simulator::PrintWrite(uintptr_t address, size_t size, unsigned reg_code) { // The template is "# reg:value -> address". To keep the trace tidy and // readable, the value is aligned with the values in the register trace. switch (size) { case kByteSizeInBytes: fprintf(stream_, "# %s%5s<7:0>: %s0x%02" PRIx8 "%s", clr_reg_name, WRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); break; case kHalfWordSizeInBytes: fprintf(stream_, "# %s%5s<15:0>: %s0x%04" PRIx16 "%s", clr_reg_name, WRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); break; case kWRegSize: fprintf(stream_, "# %s%5s: %s0x%08" PRIx32 "%s", clr_reg_name, WRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); break; case kXRegSize: fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s", clr_reg_name, XRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); break; default: UNREACHABLE(); } fprintf(stream_, " -> %s0x%016" PRIxPTR "%s\n", clr_memory_address, address, clr_normal); } void Simulator::PrintWriteFP(uintptr_t address, size_t size, unsigned reg_code) { // The template is "# reg:bits (reg:value) -> address". To keep the trace tidy // and readable, the value is aligned with the values in the register trace. switch (size) { case kSRegSize: fprintf(stream_, "# %s%5s<31:0>: %s0x%08" PRIx32 "%s (%s%s: %s%gf%s)", clr_fpreg_name, VRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal, clr_fpreg_name, SRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal); break; case kDRegSize: fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s (%s%s: %s%g%s)", clr_fpreg_name, VRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal, clr_fpreg_name, DRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal); break; default: UNREACHABLE(); } fprintf(stream_, " -> %s0x%016" PRIxPTR "%s\n", clr_memory_address, address, clr_normal); } // Visitors--------------------------------------------------------------------- void Simulator::VisitUnimplemented(Instruction* instr) { fprintf(stream_, "Unimplemented instruction at %p: 0x%08" PRIx32 "\n", reinterpret_cast(instr), instr->InstructionBits()); UNIMPLEMENTED(); } void Simulator::VisitUnallocated(Instruction* instr) { fprintf(stream_, "Unallocated instruction at %p: 0x%08" PRIx32 "\n", reinterpret_cast(instr), instr->InstructionBits()); UNIMPLEMENTED(); } void Simulator::VisitPCRelAddressing(Instruction* instr) { switch (instr->Mask(PCRelAddressingMask)) { case ADR: set_reg(instr->Rd(), instr->ImmPCOffsetTarget()); break; case ADRP: // Not implemented in the assembler. UNIMPLEMENTED(); break; default: UNREACHABLE(); break; } } void Simulator::VisitUnconditionalBranch(Instruction* instr) { switch (instr->Mask(UnconditionalBranchMask)) { case BL: set_lr(instr->following()); // Fall through. case B: set_pc(instr->ImmPCOffsetTarget()); break; default: UNREACHABLE(); } } void Simulator::VisitConditionalBranch(Instruction* instr) { DCHECK(instr->Mask(ConditionalBranchMask) == B_cond); if (ConditionPassed(static_cast(instr->ConditionBranch()))) { set_pc(instr->ImmPCOffsetTarget()); } } void Simulator::VisitUnconditionalBranchToRegister(Instruction* instr) { Instruction* target = reg(instr->Rn()); switch (instr->Mask(UnconditionalBranchToRegisterMask)) { case BLR: { set_lr(instr->following()); if (instr->Rn() == 31) { // BLR XZR is used as a guard for the constant pool. We should never hit // this, but if we do trap to allow debugging. Debug(); } // Fall through. } case BR: case RET: set_pc(target); break; default: UNIMPLEMENTED(); } } void Simulator::VisitTestBranch(Instruction* instr) { unsigned bit_pos = (instr->ImmTestBranchBit5() << 5) | instr->ImmTestBranchBit40(); bool take_branch = ((xreg(instr->Rt()) & (1UL << bit_pos)) == 0); switch (instr->Mask(TestBranchMask)) { case TBZ: break; case TBNZ: take_branch = !take_branch; break; default: UNIMPLEMENTED(); } if (take_branch) { set_pc(instr->ImmPCOffsetTarget()); } } void Simulator::VisitCompareBranch(Instruction* instr) { unsigned rt = instr->Rt(); bool take_branch = false; switch (instr->Mask(CompareBranchMask)) { case CBZ_w: take_branch = (wreg(rt) == 0); break; case CBZ_x: take_branch = (xreg(rt) == 0); break; case CBNZ_w: take_branch = (wreg(rt) != 0); break; case CBNZ_x: take_branch = (xreg(rt) != 0); break; default: UNIMPLEMENTED(); } if (take_branch) { set_pc(instr->ImmPCOffsetTarget()); } } template void Simulator::AddSubHelper(Instruction* instr, T op2) { bool set_flags = instr->FlagsUpdate(); T new_val = 0; Instr operation = instr->Mask(AddSubOpMask); switch (operation) { case ADD: case ADDS: { new_val = AddWithCarry(set_flags, reg(instr->Rn(), instr->RnMode()), op2); break; } case SUB: case SUBS: { new_val = AddWithCarry(set_flags, reg(instr->Rn(), instr->RnMode()), ~op2, 1); break; } default: UNREACHABLE(); } set_reg(instr->Rd(), new_val, instr->RdMode()); } void Simulator::VisitAddSubShifted(Instruction* instr) { Shift shift_type = static_cast(instr->ShiftDP()); unsigned shift_amount = instr->ImmDPShift(); if (instr->SixtyFourBits()) { int64_t op2 = ShiftOperand(xreg(instr->Rm()), shift_type, shift_amount); AddSubHelper(instr, op2); } else { int32_t op2 = ShiftOperand(wreg(instr->Rm()), shift_type, shift_amount); AddSubHelper(instr, op2); } } void Simulator::VisitAddSubImmediate(Instruction* instr) { int64_t op2 = instr->ImmAddSub() << ((instr->ShiftAddSub() == 1) ? 12 : 0); if (instr->SixtyFourBits()) { AddSubHelper(instr, op2); } else { AddSubHelper(instr, op2); } } void Simulator::VisitAddSubExtended(Instruction* instr) { Extend ext = static_cast(instr->ExtendMode()); unsigned left_shift = instr->ImmExtendShift(); if (instr->SixtyFourBits()) { int64_t op2 = ExtendValue(xreg(instr->Rm()), ext, left_shift); AddSubHelper(instr, op2); } else { int32_t op2 = ExtendValue(wreg(instr->Rm()), ext, left_shift); AddSubHelper(instr, op2); } } void Simulator::VisitAddSubWithCarry(Instruction* instr) { if (instr->SixtyFourBits()) { AddSubWithCarry(instr); } else { AddSubWithCarry(instr); } } void Simulator::VisitLogicalShifted(Instruction* instr) { Shift shift_type = static_cast(instr->ShiftDP()); unsigned shift_amount = instr->ImmDPShift(); if (instr->SixtyFourBits()) { int64_t op2 = ShiftOperand(xreg(instr->Rm()), shift_type, shift_amount); op2 = (instr->Mask(NOT) == NOT) ? ~op2 : op2; LogicalHelper(instr, op2); } else { int32_t op2 = ShiftOperand(wreg(instr->Rm()), shift_type, shift_amount); op2 = (instr->Mask(NOT) == NOT) ? ~op2 : op2; LogicalHelper(instr, op2); } } void Simulator::VisitLogicalImmediate(Instruction* instr) { if (instr->SixtyFourBits()) { LogicalHelper(instr, instr->ImmLogical()); } else { LogicalHelper(instr, instr->ImmLogical()); } } template void Simulator::LogicalHelper(Instruction* instr, T op2) { T op1 = reg(instr->Rn()); T result = 0; bool update_flags = false; // Switch on the logical operation, stripping out the NOT bit, as it has a // different meaning for logical immediate instructions. switch (instr->Mask(LogicalOpMask & ~NOT)) { case ANDS: update_flags = true; // Fall through. case AND: result = op1 & op2; break; case ORR: result = op1 | op2; break; case EOR: result = op1 ^ op2; break; default: UNIMPLEMENTED(); } if (update_flags) { nzcv().SetN(CalcNFlag(result)); nzcv().SetZ(CalcZFlag(result)); nzcv().SetC(0); nzcv().SetV(0); LogSystemRegister(NZCV); } set_reg(instr->Rd(), result, instr->RdMode()); } void Simulator::VisitConditionalCompareRegister(Instruction* instr) { if (instr->SixtyFourBits()) { ConditionalCompareHelper(instr, xreg(instr->Rm())); } else { ConditionalCompareHelper(instr, wreg(instr->Rm())); } } void Simulator::VisitConditionalCompareImmediate(Instruction* instr) { if (instr->SixtyFourBits()) { ConditionalCompareHelper(instr, instr->ImmCondCmp()); } else { ConditionalCompareHelper(instr, instr->ImmCondCmp()); } } template void Simulator::ConditionalCompareHelper(Instruction* instr, T op2) { T op1 = reg(instr->Rn()); if (ConditionPassed(static_cast(instr->Condition()))) { // If the condition passes, set the status flags to the result of comparing // the operands. if (instr->Mask(ConditionalCompareMask) == CCMP) { AddWithCarry(true, op1, ~op2, 1); } else { DCHECK(instr->Mask(ConditionalCompareMask) == CCMN); AddWithCarry(true, op1, op2, 0); } } else { // If the condition fails, set the status flags to the nzcv immediate. nzcv().SetFlags(instr->Nzcv()); LogSystemRegister(NZCV); } } void Simulator::VisitLoadStoreUnsignedOffset(Instruction* instr) { int offset = instr->ImmLSUnsigned() << instr->SizeLS(); LoadStoreHelper(instr, offset, Offset); } void Simulator::VisitLoadStoreUnscaledOffset(Instruction* instr) { LoadStoreHelper(instr, instr->ImmLS(), Offset); } void Simulator::VisitLoadStorePreIndex(Instruction* instr) { LoadStoreHelper(instr, instr->ImmLS(), PreIndex); } void Simulator::VisitLoadStorePostIndex(Instruction* instr) { LoadStoreHelper(instr, instr->ImmLS(), PostIndex); } void Simulator::VisitLoadStoreRegisterOffset(Instruction* instr) { Extend ext = static_cast(instr->ExtendMode()); DCHECK((ext == UXTW) || (ext == UXTX) || (ext == SXTW) || (ext == SXTX)); unsigned shift_amount = instr->ImmShiftLS() * instr->SizeLS(); int64_t offset = ExtendValue(xreg(instr->Rm()), ext, shift_amount); LoadStoreHelper(instr, offset, Offset); } void Simulator::LoadStoreHelper(Instruction* instr, int64_t offset, AddrMode addrmode) { unsigned srcdst = instr->Rt(); unsigned addr_reg = instr->Rn(); uintptr_t address = LoadStoreAddress(addr_reg, offset, addrmode); uintptr_t stack = 0; // Handle the writeback for stores before the store. On a CPU the writeback // and the store are atomic, but when running on the simulator it is possible // to be interrupted in between. The simulator is not thread safe and V8 does // not require it to be to run JavaScript therefore the profiler may sample // the "simulated" CPU in the middle of load/store with writeback. The code // below ensures that push operations are safe even when interrupted: the // stack pointer will be decremented before adding an element to the stack. if (instr->IsStore()) { LoadStoreWriteBack(addr_reg, offset, addrmode); // For store the address post writeback is used to check access below the // stack. stack = sp(); } LoadStoreOp op = static_cast(instr->Mask(LoadStoreOpMask)); switch (op) { // Use _no_log variants to suppress the register trace (LOG_REGS, // LOG_FP_REGS). We will print a more detailed log. case LDRB_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDRH_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDR_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDR_x: set_xreg_no_log(srcdst, MemoryRead(address)); break; case LDRSB_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDRSH_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDRSB_x: set_xreg_no_log(srcdst, MemoryRead(address)); break; case LDRSH_x: set_xreg_no_log(srcdst, MemoryRead(address)); break; case LDRSW_x: set_xreg_no_log(srcdst, MemoryRead(address)); break; case LDR_s: set_sreg_no_log(srcdst, MemoryRead(address)); break; case LDR_d: set_dreg_no_log(srcdst, MemoryRead(address)); break; case STRB_w: MemoryWrite(address, wreg(srcdst)); break; case STRH_w: MemoryWrite(address, wreg(srcdst)); break; case STR_w: MemoryWrite(address, wreg(srcdst)); break; case STR_x: MemoryWrite(address, xreg(srcdst)); break; case STR_s: MemoryWrite(address, sreg(srcdst)); break; case STR_d: MemoryWrite(address, dreg(srcdst)); break; default: UNIMPLEMENTED(); } // Print a detailed trace (including the memory address) instead of the basic // register:value trace generated by set_*reg(). size_t access_size = 1 << instr->SizeLS(); if (instr->IsLoad()) { if ((op == LDR_s) || (op == LDR_d)) { LogReadFP(address, access_size, srcdst); } else { LogRead(address, access_size, srcdst); } } else { if ((op == STR_s) || (op == STR_d)) { LogWriteFP(address, access_size, srcdst); } else { LogWrite(address, access_size, srcdst); } } // Handle the writeback for loads after the load to ensure safe pop // operation even when interrupted in the middle of it. The stack pointer // is only updated after the load so pop(fp) will never break the invariant // sp <= fp expected while walking the stack in the sampler. if (instr->IsLoad()) { // For loads the address pre writeback is used to check access below the // stack. stack = sp(); LoadStoreWriteBack(addr_reg, offset, addrmode); } // Accesses below the stack pointer (but above the platform stack limit) are // not allowed in the ABI. CheckMemoryAccess(address, stack); } void Simulator::VisitLoadStorePairOffset(Instruction* instr) { LoadStorePairHelper(instr, Offset); } void Simulator::VisitLoadStorePairPreIndex(Instruction* instr) { LoadStorePairHelper(instr, PreIndex); } void Simulator::VisitLoadStorePairPostIndex(Instruction* instr) { LoadStorePairHelper(instr, PostIndex); } void Simulator::VisitLoadStorePairNonTemporal(Instruction* instr) { LoadStorePairHelper(instr, Offset); } void Simulator::LoadStorePairHelper(Instruction* instr, AddrMode addrmode) { unsigned rt = instr->Rt(); unsigned rt2 = instr->Rt2(); unsigned addr_reg = instr->Rn(); size_t access_size = 1 << instr->SizeLSPair(); int64_t offset = instr->ImmLSPair() * access_size; uintptr_t address = LoadStoreAddress(addr_reg, offset, addrmode); uintptr_t address2 = address + access_size; uintptr_t stack = 0; // Handle the writeback for stores before the store. On a CPU the writeback // and the store are atomic, but when running on the simulator it is possible // to be interrupted in between. The simulator is not thread safe and V8 does // not require it to be to run JavaScript therefore the profiler may sample // the "simulated" CPU in the middle of load/store with writeback. The code // below ensures that push operations are safe even when interrupted: the // stack pointer will be decremented before adding an element to the stack. if (instr->IsStore()) { LoadStoreWriteBack(addr_reg, offset, addrmode); // For store the address post writeback is used to check access below the // stack. stack = sp(); } LoadStorePairOp op = static_cast(instr->Mask(LoadStorePairMask)); // 'rt' and 'rt2' can only be aliased for stores. DCHECK(((op & LoadStorePairLBit) == 0) || (rt != rt2)); switch (op) { // Use _no_log variants to suppress the register trace (LOG_REGS, // LOG_FP_REGS). We will print a more detailed log. case LDP_w: { DCHECK(access_size == kWRegSize); set_wreg_no_log(rt, MemoryRead(address)); set_wreg_no_log(rt2, MemoryRead(address2)); break; } case LDP_s: { DCHECK(access_size == kSRegSize); set_sreg_no_log(rt, MemoryRead(address)); set_sreg_no_log(rt2, MemoryRead(address2)); break; } case LDP_x: { DCHECK(access_size == kXRegSize); set_xreg_no_log(rt, MemoryRead(address)); set_xreg_no_log(rt2, MemoryRead(address2)); break; } case LDP_d: { DCHECK(access_size == kDRegSize); set_dreg_no_log(rt, MemoryRead(address)); set_dreg_no_log(rt2, MemoryRead(address2)); break; } case LDPSW_x: { DCHECK(access_size == kWRegSize); set_xreg_no_log(rt, MemoryRead(address)); set_xreg_no_log(rt2, MemoryRead(address2)); break; } case STP_w: { DCHECK(access_size == kWRegSize); MemoryWrite(address, wreg(rt)); MemoryWrite(address2, wreg(rt2)); break; } case STP_s: { DCHECK(access_size == kSRegSize); MemoryWrite(address, sreg(rt)); MemoryWrite(address2, sreg(rt2)); break; } case STP_x: { DCHECK(access_size == kXRegSize); MemoryWrite(address, xreg(rt)); MemoryWrite(address2, xreg(rt2)); break; } case STP_d: { DCHECK(access_size == kDRegSize); MemoryWrite(address, dreg(rt)); MemoryWrite(address2, dreg(rt2)); break; } default: UNREACHABLE(); } // Print a detailed trace (including the memory address) instead of the basic // register:value trace generated by set_*reg(). if (instr->IsLoad()) { if ((op == LDP_s) || (op == LDP_d)) { LogReadFP(address, access_size, rt); LogReadFP(address2, access_size, rt2); } else { LogRead(address, access_size, rt); LogRead(address2, access_size, rt2); } } else { if ((op == STP_s) || (op == STP_d)) { LogWriteFP(address, access_size, rt); LogWriteFP(address2, access_size, rt2); } else { LogWrite(address, access_size, rt); LogWrite(address2, access_size, rt2); } } // Handle the writeback for loads after the load to ensure safe pop // operation even when interrupted in the middle of it. The stack pointer // is only updated after the load so pop(fp) will never break the invariant // sp <= fp expected while walking the stack in the sampler. if (instr->IsLoad()) { // For loads the address pre writeback is used to check access below the // stack. stack = sp(); LoadStoreWriteBack(addr_reg, offset, addrmode); } // Accesses below the stack pointer (but above the platform stack limit) are // not allowed in the ABI. CheckMemoryAccess(address, stack); } void Simulator::VisitLoadLiteral(Instruction* instr) { uintptr_t address = instr->LiteralAddress(); unsigned rt = instr->Rt(); switch (instr->Mask(LoadLiteralMask)) { // Use _no_log variants to suppress the register trace (LOG_REGS, // LOG_FP_REGS), then print a more detailed log. case LDR_w_lit: set_wreg_no_log(rt, MemoryRead(address)); LogRead(address, kWRegSize, rt); break; case LDR_x_lit: set_xreg_no_log(rt, MemoryRead(address)); LogRead(address, kXRegSize, rt); break; case LDR_s_lit: set_sreg_no_log(rt, MemoryRead(address)); LogReadFP(address, kSRegSize, rt); break; case LDR_d_lit: set_dreg_no_log(rt, MemoryRead(address)); LogReadFP(address, kDRegSize, rt); break; default: UNREACHABLE(); } } uintptr_t Simulator::LoadStoreAddress(unsigned addr_reg, int64_t offset, AddrMode addrmode) { const unsigned kSPRegCode = kSPRegInternalCode & kRegCodeMask; uint64_t address = xreg(addr_reg, Reg31IsStackPointer); if ((addr_reg == kSPRegCode) && ((address % 16) != 0)) { // When the base register is SP the stack pointer is required to be // quadword aligned prior to the address calculation and write-backs. // Misalignment will cause a stack alignment fault. FATAL("ALIGNMENT EXCEPTION"); } if ((addrmode == Offset) || (addrmode == PreIndex)) { address += offset; } return address; } void Simulator::LoadStoreWriteBack(unsigned addr_reg, int64_t offset, AddrMode addrmode) { if ((addrmode == PreIndex) || (addrmode == PostIndex)) { DCHECK(offset != 0); uint64_t address = xreg(addr_reg, Reg31IsStackPointer); set_reg(addr_reg, address + offset, Reg31IsStackPointer); } } void Simulator::CheckMemoryAccess(uintptr_t address, uintptr_t stack) { if ((address >= stack_limit_) && (address < stack)) { fprintf(stream_, "ACCESS BELOW STACK POINTER:\n"); fprintf(stream_, " sp is here: 0x%016" PRIx64 "\n", static_cast(stack)); fprintf(stream_, " access was here: 0x%016" PRIx64 "\n", static_cast(address)); fprintf(stream_, " stack limit is here: 0x%016" PRIx64 "\n", static_cast(stack_limit_)); fprintf(stream_, "\n"); FATAL("ACCESS BELOW STACK POINTER"); } } void Simulator::VisitMoveWideImmediate(Instruction* instr) { MoveWideImmediateOp mov_op = static_cast(instr->Mask(MoveWideImmediateMask)); int64_t new_xn_val = 0; bool is_64_bits = instr->SixtyFourBits() == 1; // Shift is limited for W operations. DCHECK(is_64_bits || (instr->ShiftMoveWide() < 2)); // Get the shifted immediate. int64_t shift = instr->ShiftMoveWide() * 16; int64_t shifted_imm16 = instr->ImmMoveWide() << shift; // Compute the new value. switch (mov_op) { case MOVN_w: case MOVN_x: { new_xn_val = ~shifted_imm16; if (!is_64_bits) new_xn_val &= kWRegMask; break; } case MOVK_w: case MOVK_x: { unsigned reg_code = instr->Rd(); int64_t prev_xn_val = is_64_bits ? xreg(reg_code) : wreg(reg_code); new_xn_val = (prev_xn_val & ~(0xffffL << shift)) | shifted_imm16; break; } case MOVZ_w: case MOVZ_x: { new_xn_val = shifted_imm16; break; } default: UNREACHABLE(); } // Update the destination register. set_xreg(instr->Rd(), new_xn_val); } void Simulator::VisitConditionalSelect(Instruction* instr) { if (ConditionFailed(static_cast(instr->Condition()))) { uint64_t new_val = xreg(instr->Rm()); switch (instr->Mask(ConditionalSelectMask)) { case CSEL_w: set_wreg(instr->Rd(), new_val); break; case CSEL_x: set_xreg(instr->Rd(), new_val); break; case CSINC_w: set_wreg(instr->Rd(), new_val + 1); break; case CSINC_x: set_xreg(instr->Rd(), new_val + 1); break; case CSINV_w: set_wreg(instr->Rd(), ~new_val); break; case CSINV_x: set_xreg(instr->Rd(), ~new_val); break; case CSNEG_w: set_wreg(instr->Rd(), -new_val); break; case CSNEG_x: set_xreg(instr->Rd(), -new_val); break; default: UNIMPLEMENTED(); } } else { if (instr->SixtyFourBits()) { set_xreg(instr->Rd(), xreg(instr->Rn())); } else { set_wreg(instr->Rd(), wreg(instr->Rn())); } } } void Simulator::VisitDataProcessing1Source(Instruction* instr) { unsigned dst = instr->Rd(); unsigned src = instr->Rn(); switch (instr->Mask(DataProcessing1SourceMask)) { case RBIT_w: set_wreg(dst, ReverseBits(wreg(src), kWRegSizeInBits)); break; case RBIT_x: set_xreg(dst, ReverseBits(xreg(src), kXRegSizeInBits)); break; case REV16_w: set_wreg(dst, ReverseBytes(wreg(src), Reverse16)); break; case REV16_x: set_xreg(dst, ReverseBytes(xreg(src), Reverse16)); break; case REV_w: set_wreg(dst, ReverseBytes(wreg(src), Reverse32)); break; case REV32_x: set_xreg(dst, ReverseBytes(xreg(src), Reverse32)); break; case REV_x: set_xreg(dst, ReverseBytes(xreg(src), Reverse64)); break; case CLZ_w: set_wreg(dst, CountLeadingZeros(wreg(src), kWRegSizeInBits)); break; case CLZ_x: set_xreg(dst, CountLeadingZeros(xreg(src), kXRegSizeInBits)); break; case CLS_w: { set_wreg(dst, CountLeadingSignBits(wreg(src), kWRegSizeInBits)); break; } case CLS_x: { set_xreg(dst, CountLeadingSignBits(xreg(src), kXRegSizeInBits)); break; } default: UNIMPLEMENTED(); } } uint64_t Simulator::ReverseBits(uint64_t value, unsigned num_bits) { DCHECK((num_bits == kWRegSizeInBits) || (num_bits == kXRegSizeInBits)); uint64_t result = 0; for (unsigned i = 0; i < num_bits; i++) { result = (result << 1) | (value & 1); value >>= 1; } return result; } uint64_t Simulator::ReverseBytes(uint64_t value, ReverseByteMode mode) { // Split the 64-bit value into an 8-bit array, where b[0] is the least // significant byte, and b[7] is the most significant. uint8_t bytes[8]; uint64_t mask = 0xff00000000000000UL; for (int i = 7; i >= 0; i--) { bytes[i] = (value & mask) >> (i * 8); mask >>= 8; } // Permutation tables for REV instructions. // permute_table[Reverse16] is used by REV16_x, REV16_w // permute_table[Reverse32] is used by REV32_x, REV_w // permute_table[Reverse64] is used by REV_x DCHECK((Reverse16 == 0) && (Reverse32 == 1) && (Reverse64 == 2)); static const uint8_t permute_table[3][8] = { {6, 7, 4, 5, 2, 3, 0, 1}, {4, 5, 6, 7, 0, 1, 2, 3}, {0, 1, 2, 3, 4, 5, 6, 7} }; uint64_t result = 0; for (int i = 0; i < 8; i++) { result <<= 8; result |= bytes[permute_table[mode][i]]; } return result; } template void Simulator::DataProcessing2Source(Instruction* instr) { Shift shift_op = NO_SHIFT; T result = 0; switch (instr->Mask(DataProcessing2SourceMask)) { case SDIV_w: case SDIV_x: { T rn = reg(instr->Rn()); T rm = reg(instr->Rm()); if ((rn == std::numeric_limits::min()) && (rm == -1)) { result = std::numeric_limits::min(); } else if (rm == 0) { // Division by zero can be trapped, but not on A-class processors. result = 0; } else { result = rn / rm; } break; } case UDIV_w: case UDIV_x: { typedef typename make_unsigned::type unsignedT; unsignedT rn = static_cast(reg(instr->Rn())); unsignedT rm = static_cast(reg(instr->Rm())); if (rm == 0) { // Division by zero can be trapped, but not on A-class processors. result = 0; } else { result = rn / rm; } break; } case LSLV_w: case LSLV_x: shift_op = LSL; break; case LSRV_w: case LSRV_x: shift_op = LSR; break; case ASRV_w: case ASRV_x: shift_op = ASR; break; case RORV_w: case RORV_x: shift_op = ROR; break; default: UNIMPLEMENTED(); } if (shift_op != NO_SHIFT) { // Shift distance encoded in the least-significant five/six bits of the // register. unsigned shift = wreg(instr->Rm()); if (sizeof(T) == kWRegSize) { shift &= kShiftAmountWRegMask; } else { shift &= kShiftAmountXRegMask; } result = ShiftOperand(reg(instr->Rn()), shift_op, shift); } set_reg(instr->Rd(), result); } void Simulator::VisitDataProcessing2Source(Instruction* instr) { if (instr->SixtyFourBits()) { DataProcessing2Source(instr); } else { DataProcessing2Source(instr); } } // The algorithm used is described in section 8.2 of // Hacker's Delight, by Henry S. Warren, Jr. // It assumes that a right shift on a signed integer is an arithmetic shift. static int64_t MultiplyHighSigned(int64_t u, int64_t v) { uint64_t u0, v0, w0; int64_t u1, v1, w1, w2, t; u0 = u & 0xffffffffL; u1 = u >> 32; v0 = v & 0xffffffffL; v1 = v >> 32; w0 = u0 * v0; t = u1 * v0 + (w0 >> 32); w1 = t & 0xffffffffL; w2 = t >> 32; w1 = u0 * v1 + w1; return u1 * v1 + w2 + (w1 >> 32); } void Simulator::VisitDataProcessing3Source(Instruction* instr) { int64_t result = 0; // Extract and sign- or zero-extend 32-bit arguments for widening operations. uint64_t rn_u32 = reg(instr->Rn()); uint64_t rm_u32 = reg(instr->Rm()); int64_t rn_s32 = reg(instr->Rn()); int64_t rm_s32 = reg(instr->Rm()); switch (instr->Mask(DataProcessing3SourceMask)) { case MADD_w: case MADD_x: result = xreg(instr->Ra()) + (xreg(instr->Rn()) * xreg(instr->Rm())); break; case MSUB_w: case MSUB_x: result = xreg(instr->Ra()) - (xreg(instr->Rn()) * xreg(instr->Rm())); break; case SMADDL_x: result = xreg(instr->Ra()) + (rn_s32 * rm_s32); break; case SMSUBL_x: result = xreg(instr->Ra()) - (rn_s32 * rm_s32); break; case UMADDL_x: result = xreg(instr->Ra()) + (rn_u32 * rm_u32); break; case UMSUBL_x: result = xreg(instr->Ra()) - (rn_u32 * rm_u32); break; case SMULH_x: DCHECK(instr->Ra() == kZeroRegCode); result = MultiplyHighSigned(xreg(instr->Rn()), xreg(instr->Rm())); break; default: UNIMPLEMENTED(); } if (instr->SixtyFourBits()) { set_xreg(instr->Rd(), result); } else { set_wreg(instr->Rd(), result); } } template void Simulator::BitfieldHelper(Instruction* instr) { typedef typename make_unsigned::type unsignedT; T reg_size = sizeof(T) * 8; T R = instr->ImmR(); T S = instr->ImmS(); T diff = S - R; T mask; if (diff >= 0) { mask = diff < reg_size - 1 ? (static_cast(1) << (diff + 1)) - 1 : static_cast(-1); } else { mask = ((1L << (S + 1)) - 1); mask = (static_cast(mask) >> R) | (mask << (reg_size - R)); diff += reg_size; } // inzero indicates if the extracted bitfield is inserted into the // destination register value or in zero. // If extend is true, extend the sign of the extracted bitfield. bool inzero = false; bool extend = false; switch (instr->Mask(BitfieldMask)) { case BFM_x: case BFM_w: break; case SBFM_x: case SBFM_w: inzero = true; extend = true; break; case UBFM_x: case UBFM_w: inzero = true; break; default: UNIMPLEMENTED(); } T dst = inzero ? 0 : reg(instr->Rd()); T src = reg(instr->Rn()); // Rotate source bitfield into place. T result = (static_cast(src) >> R) | (src << (reg_size - R)); // Determine the sign extension. T topbits_preshift = (static_cast(1) << (reg_size - diff - 1)) - 1; T signbits = (extend && ((src >> S) & 1) ? topbits_preshift : 0) << (diff + 1); // Merge sign extension, dest/zero and bitfield. result = signbits | (result & mask) | (dst & ~mask); set_reg(instr->Rd(), result); } void Simulator::VisitBitfield(Instruction* instr) { if (instr->SixtyFourBits()) { BitfieldHelper(instr); } else { BitfieldHelper(instr); } } void Simulator::VisitExtract(Instruction* instr) { if (instr->SixtyFourBits()) { Extract(instr); } else { Extract(instr); } } void Simulator::VisitFPImmediate(Instruction* instr) { AssertSupportedFPCR(); unsigned dest = instr->Rd(); switch (instr->Mask(FPImmediateMask)) { case FMOV_s_imm: set_sreg(dest, instr->ImmFP32()); break; case FMOV_d_imm: set_dreg(dest, instr->ImmFP64()); break; default: UNREACHABLE(); } } void Simulator::VisitFPIntegerConvert(Instruction* instr) { AssertSupportedFPCR(); unsigned dst = instr->Rd(); unsigned src = instr->Rn(); FPRounding round = fpcr().RMode(); switch (instr->Mask(FPIntegerConvertMask)) { case FCVTAS_ws: set_wreg(dst, FPToInt32(sreg(src), FPTieAway)); break; case FCVTAS_xs: set_xreg(dst, FPToInt64(sreg(src), FPTieAway)); break; case FCVTAS_wd: set_wreg(dst, FPToInt32(dreg(src), FPTieAway)); break; case FCVTAS_xd: set_xreg(dst, FPToInt64(dreg(src), FPTieAway)); break; case FCVTAU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPTieAway)); break; case FCVTAU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPTieAway)); break; case FCVTAU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPTieAway)); break; case FCVTAU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPTieAway)); break; case FCVTMS_ws: set_wreg(dst, FPToInt32(sreg(src), FPNegativeInfinity)); break; case FCVTMS_xs: set_xreg(dst, FPToInt64(sreg(src), FPNegativeInfinity)); break; case FCVTMS_wd: set_wreg(dst, FPToInt32(dreg(src), FPNegativeInfinity)); break; case FCVTMS_xd: set_xreg(dst, FPToInt64(dreg(src), FPNegativeInfinity)); break; case FCVTMU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPNegativeInfinity)); break; case FCVTMU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPNegativeInfinity)); break; case FCVTMU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPNegativeInfinity)); break; case FCVTMU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPNegativeInfinity)); break; case FCVTNS_ws: set_wreg(dst, FPToInt32(sreg(src), FPTieEven)); break; case FCVTNS_xs: set_xreg(dst, FPToInt64(sreg(src), FPTieEven)); break; case FCVTNS_wd: set_wreg(dst, FPToInt32(dreg(src), FPTieEven)); break; case FCVTNS_xd: set_xreg(dst, FPToInt64(dreg(src), FPTieEven)); break; case FCVTNU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPTieEven)); break; case FCVTNU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPTieEven)); break; case FCVTNU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPTieEven)); break; case FCVTNU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPTieEven)); break; case FCVTZS_ws: set_wreg(dst, FPToInt32(sreg(src), FPZero)); break; case FCVTZS_xs: set_xreg(dst, FPToInt64(sreg(src), FPZero)); break; case FCVTZS_wd: set_wreg(dst, FPToInt32(dreg(src), FPZero)); break; case FCVTZS_xd: set_xreg(dst, FPToInt64(dreg(src), FPZero)); break; case FCVTZU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPZero)); break; case FCVTZU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPZero)); break; case FCVTZU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPZero)); break; case FCVTZU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPZero)); break; case FMOV_ws: set_wreg(dst, sreg_bits(src)); break; case FMOV_xd: set_xreg(dst, dreg_bits(src)); break; case FMOV_sw: set_sreg_bits(dst, wreg(src)); break; case FMOV_dx: set_dreg_bits(dst, xreg(src)); break; // A 32-bit input can be handled in the same way as a 64-bit input, since // the sign- or zero-extension will not affect the conversion. case SCVTF_dx: set_dreg(dst, FixedToDouble(xreg(src), 0, round)); break; case SCVTF_dw: set_dreg(dst, FixedToDouble(wreg(src), 0, round)); break; case UCVTF_dx: set_dreg(dst, UFixedToDouble(xreg(src), 0, round)); break; case UCVTF_dw: { set_dreg(dst, UFixedToDouble(reg(src), 0, round)); break; } case SCVTF_sx: set_sreg(dst, FixedToFloat(xreg(src), 0, round)); break; case SCVTF_sw: set_sreg(dst, FixedToFloat(wreg(src), 0, round)); break; case UCVTF_sx: set_sreg(dst, UFixedToFloat(xreg(src), 0, round)); break; case UCVTF_sw: { set_sreg(dst, UFixedToFloat(reg(src), 0, round)); break; } default: UNREACHABLE(); } } void Simulator::VisitFPFixedPointConvert(Instruction* instr) { AssertSupportedFPCR(); unsigned dst = instr->Rd(); unsigned src = instr->Rn(); int fbits = 64 - instr->FPScale(); FPRounding round = fpcr().RMode(); switch (instr->Mask(FPFixedPointConvertMask)) { // A 32-bit input can be handled in the same way as a 64-bit input, since // the sign- or zero-extension will not affect the conversion. case SCVTF_dx_fixed: set_dreg(dst, FixedToDouble(xreg(src), fbits, round)); break; case SCVTF_dw_fixed: set_dreg(dst, FixedToDouble(wreg(src), fbits, round)); break; case UCVTF_dx_fixed: set_dreg(dst, UFixedToDouble(xreg(src), fbits, round)); break; case UCVTF_dw_fixed: { set_dreg(dst, UFixedToDouble(reg(src), fbits, round)); break; } case SCVTF_sx_fixed: set_sreg(dst, FixedToFloat(xreg(src), fbits, round)); break; case SCVTF_sw_fixed: set_sreg(dst, FixedToFloat(wreg(src), fbits, round)); break; case UCVTF_sx_fixed: set_sreg(dst, UFixedToFloat(xreg(src), fbits, round)); break; case UCVTF_sw_fixed: { set_sreg(dst, UFixedToFloat(reg(src), fbits, round)); break; } default: UNREACHABLE(); } } int32_t Simulator::FPToInt32(double value, FPRounding rmode) { value = FPRoundInt(value, rmode); if (value >= kWMaxInt) { return kWMaxInt; } else if (value < kWMinInt) { return kWMinInt; } return std::isnan(value) ? 0 : static_cast(value); } int64_t Simulator::FPToInt64(double value, FPRounding rmode) { value = FPRoundInt(value, rmode); if (value >= kXMaxInt) { return kXMaxInt; } else if (value < kXMinInt) { return kXMinInt; } return std::isnan(value) ? 0 : static_cast(value); } uint32_t Simulator::FPToUInt32(double value, FPRounding rmode) { value = FPRoundInt(value, rmode); if (value >= kWMaxUInt) { return kWMaxUInt; } else if (value < 0.0) { return 0; } return std::isnan(value) ? 0 : static_cast(value); } uint64_t Simulator::FPToUInt64(double value, FPRounding rmode) { value = FPRoundInt(value, rmode); if (value >= kXMaxUInt) { return kXMaxUInt; } else if (value < 0.0) { return 0; } return std::isnan(value) ? 0 : static_cast(value); } void Simulator::VisitFPCompare(Instruction* instr) { AssertSupportedFPCR(); unsigned reg_size = (instr->Mask(FP64) == FP64) ? kDRegSizeInBits : kSRegSizeInBits; double fn_val = fpreg(reg_size, instr->Rn()); switch (instr->Mask(FPCompareMask)) { case FCMP_s: case FCMP_d: FPCompare(fn_val, fpreg(reg_size, instr->Rm())); break; case FCMP_s_zero: case FCMP_d_zero: FPCompare(fn_val, 0.0); break; default: UNIMPLEMENTED(); } } void Simulator::VisitFPConditionalCompare(Instruction* instr) { AssertSupportedFPCR(); switch (instr->Mask(FPConditionalCompareMask)) { case FCCMP_s: case FCCMP_d: { if (ConditionPassed(static_cast(instr->Condition()))) { // If the condition passes, set the status flags to the result of // comparing the operands. unsigned reg_size = (instr->Mask(FP64) == FP64) ? kDRegSizeInBits : kSRegSizeInBits; FPCompare(fpreg(reg_size, instr->Rn()), fpreg(reg_size, instr->Rm())); } else { // If the condition fails, set the status flags to the nzcv immediate. nzcv().SetFlags(instr->Nzcv()); LogSystemRegister(NZCV); } break; } default: UNIMPLEMENTED(); } } void Simulator::VisitFPConditionalSelect(Instruction* instr) { AssertSupportedFPCR(); Instr selected; if (ConditionPassed(static_cast(instr->Condition()))) { selected = instr->Rn(); } else { selected = instr->Rm(); } switch (instr->Mask(FPConditionalSelectMask)) { case FCSEL_s: set_sreg(instr->Rd(), sreg(selected)); break; case FCSEL_d: set_dreg(instr->Rd(), dreg(selected)); break; default: UNIMPLEMENTED(); } } void Simulator::VisitFPDataProcessing1Source(Instruction* instr) { AssertSupportedFPCR(); unsigned fd = instr->Rd(); unsigned fn = instr->Rn(); switch (instr->Mask(FPDataProcessing1SourceMask)) { case FMOV_s: set_sreg(fd, sreg(fn)); break; case FMOV_d: set_dreg(fd, dreg(fn)); break; case FABS_s: set_sreg(fd, std::fabs(sreg(fn))); break; case FABS_d: set_dreg(fd, std::fabs(dreg(fn))); break; case FNEG_s: set_sreg(fd, -sreg(fn)); break; case FNEG_d: set_dreg(fd, -dreg(fn)); break; case FSQRT_s: set_sreg(fd, FPSqrt(sreg(fn))); break; case FSQRT_d: set_dreg(fd, FPSqrt(dreg(fn))); break; case FRINTA_s: set_sreg(fd, FPRoundInt(sreg(fn), FPTieAway)); break; case FRINTA_d: set_dreg(fd, FPRoundInt(dreg(fn), FPTieAway)); break; case FRINTM_s: set_sreg(fd, FPRoundInt(sreg(fn), FPNegativeInfinity)); break; case FRINTM_d: set_dreg(fd, FPRoundInt(dreg(fn), FPNegativeInfinity)); break; case FRINTN_s: set_sreg(fd, FPRoundInt(sreg(fn), FPTieEven)); break; case FRINTN_d: set_dreg(fd, FPRoundInt(dreg(fn), FPTieEven)); break; case FRINTZ_s: set_sreg(fd, FPRoundInt(sreg(fn), FPZero)); break; case FRINTZ_d: set_dreg(fd, FPRoundInt(dreg(fn), FPZero)); break; case FCVT_ds: set_dreg(fd, FPToDouble(sreg(fn))); break; case FCVT_sd: set_sreg(fd, FPToFloat(dreg(fn), FPTieEven)); break; default: UNIMPLEMENTED(); } } // Assemble the specified IEEE-754 components into the target type and apply // appropriate rounding. // sign: 0 = positive, 1 = negative // exponent: Unbiased IEEE-754 exponent. // mantissa: The mantissa of the input. The top bit (which is not encoded for // normal IEEE-754 values) must not be omitted. This bit has the // value 'pow(2, exponent)'. // // The input value is assumed to be a normalized value. That is, the input may // not be infinity or NaN. If the source value is subnormal, it must be // normalized before calling this function such that the highest set bit in the // mantissa has the value 'pow(2, exponent)'. // // Callers should use FPRoundToFloat or FPRoundToDouble directly, rather than // calling a templated FPRound. template static T FPRound(int64_t sign, int64_t exponent, uint64_t mantissa, FPRounding round_mode) { DCHECK((sign == 0) || (sign == 1)); // Only the FPTieEven rounding mode is implemented. DCHECK(round_mode == FPTieEven); USE(round_mode); // Rounding can promote subnormals to normals, and normals to infinities. For // example, a double with exponent 127 (FLT_MAX_EXP) would appear to be // encodable as a float, but rounding based on the low-order mantissa bits // could make it overflow. With ties-to-even rounding, this value would become // an infinity. // ---- Rounding Method ---- // // The exponent is irrelevant in the rounding operation, so we treat the // lowest-order bit that will fit into the result ('onebit') as having // the value '1'. Similarly, the highest-order bit that won't fit into // the result ('halfbit') has the value '0.5'. The 'point' sits between // 'onebit' and 'halfbit': // // These bits fit into the result. // |---------------------| // mantissa = 0bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // || // / | // / halfbit // onebit // // For subnormal outputs, the range of representable bits is smaller and // the position of onebit and halfbit depends on the exponent of the // input, but the method is otherwise similar. // // onebit(frac) // | // | halfbit(frac) halfbit(adjusted) // | / / // | | | // 0b00.0 (exact) -> 0b00.0 (exact) -> 0b00 // 0b00.0... -> 0b00.0... -> 0b00 // 0b00.1 (exact) -> 0b00.0111..111 -> 0b00 // 0b00.1... -> 0b00.1... -> 0b01 // 0b01.0 (exact) -> 0b01.0 (exact) -> 0b01 // 0b01.0... -> 0b01.0... -> 0b01 // 0b01.1 (exact) -> 0b01.1 (exact) -> 0b10 // 0b01.1... -> 0b01.1... -> 0b10 // 0b10.0 (exact) -> 0b10.0 (exact) -> 0b10 // 0b10.0... -> 0b10.0... -> 0b10 // 0b10.1 (exact) -> 0b10.0111..111 -> 0b10 // 0b10.1... -> 0b10.1... -> 0b11 // 0b11.0 (exact) -> 0b11.0 (exact) -> 0b11 // ... / | / | // / | / | // / | // adjusted = frac - (halfbit(mantissa) & ~onebit(frac)); / | // // mantissa = (mantissa >> shift) + halfbit(adjusted); static const int mantissa_offset = 0; static const int exponent_offset = mantissa_offset + mbits; static const int sign_offset = exponent_offset + ebits; STATIC_ASSERT(sign_offset == (sizeof(T) * kByteSize - 1)); // Bail out early for zero inputs. if (mantissa == 0) { return sign << sign_offset; } // If all bits in the exponent are set, the value is infinite or NaN. // This is true for all binary IEEE-754 formats. static const int infinite_exponent = (1 << ebits) - 1; static const int max_normal_exponent = infinite_exponent - 1; // Apply the exponent bias to encode it for the result. Doing this early makes // it easy to detect values that will be infinite or subnormal. exponent += max_normal_exponent >> 1; if (exponent > max_normal_exponent) { // Overflow: The input is too large for the result type to represent. The // FPTieEven rounding mode handles overflows using infinities. exponent = infinite_exponent; mantissa = 0; return (sign << sign_offset) | (exponent << exponent_offset) | (mantissa << mantissa_offset); } // Calculate the shift required to move the top mantissa bit to the proper // place in the destination type. const int highest_significant_bit = 63 - CountLeadingZeros(mantissa, 64); int shift = highest_significant_bit - mbits; if (exponent <= 0) { // The output will be subnormal (before rounding). // For subnormal outputs, the shift must be adjusted by the exponent. The +1 // is necessary because the exponent of a subnormal value (encoded as 0) is // the same as the exponent of the smallest normal value (encoded as 1). shift += -exponent + 1; // Handle inputs that would produce a zero output. // // Shifts higher than highest_significant_bit+1 will always produce a zero // result. A shift of exactly highest_significant_bit+1 might produce a // non-zero result after rounding. if (shift > (highest_significant_bit + 1)) { // The result will always be +/-0.0. return sign << sign_offset; } // Properly encode the exponent for a subnormal output. exponent = 0; } else { // Clear the topmost mantissa bit, since this is not encoded in IEEE-754 // normal values. mantissa &= ~(1UL << highest_significant_bit); } if (shift > 0) { // We have to shift the mantissa to the right. Some precision is lost, so we // need to apply rounding. uint64_t onebit_mantissa = (mantissa >> (shift)) & 1; uint64_t halfbit_mantissa = (mantissa >> (shift-1)) & 1; uint64_t adjusted = mantissa - (halfbit_mantissa & ~onebit_mantissa); T halfbit_adjusted = (adjusted >> (shift-1)) & 1; T result = (sign << sign_offset) | (exponent << exponent_offset) | ((mantissa >> shift) << mantissa_offset); // A very large mantissa can overflow during rounding. If this happens, the // exponent should be incremented and the mantissa set to 1.0 (encoded as // 0). Applying halfbit_adjusted after assembling the float has the nice // side-effect that this case is handled for free. // // This also handles cases where a very large finite value overflows to // infinity, or where a very large subnormal value overflows to become // normal. return result + halfbit_adjusted; } else { // We have to shift the mantissa to the left (or not at all). The input // mantissa is exactly representable in the output mantissa, so apply no // rounding correction. return (sign << sign_offset) | (exponent << exponent_offset) | ((mantissa << -shift) << mantissa_offset); } } // See FPRound for a description of this function. static inline double FPRoundToDouble(int64_t sign, int64_t exponent, uint64_t mantissa, FPRounding round_mode) { int64_t bits = FPRound(sign, exponent, mantissa, round_mode); return rawbits_to_double(bits); } // See FPRound for a description of this function. static inline float FPRoundToFloat(int64_t sign, int64_t exponent, uint64_t mantissa, FPRounding round_mode) { int32_t bits = FPRound(sign, exponent, mantissa, round_mode); return rawbits_to_float(bits); } double Simulator::FixedToDouble(int64_t src, int fbits, FPRounding round) { if (src >= 0) { return UFixedToDouble(src, fbits, round); } else { // This works for all negative values, including INT64_MIN. return -UFixedToDouble(-src, fbits, round); } } double Simulator::UFixedToDouble(uint64_t src, int fbits, FPRounding round) { // An input of 0 is a special case because the result is effectively // subnormal: The exponent is encoded as 0 and there is no implicit 1 bit. if (src == 0) { return 0.0; } // Calculate the exponent. The highest significant bit will have the value // 2^exponent. const int highest_significant_bit = 63 - CountLeadingZeros(src, 64); const int64_t exponent = highest_significant_bit - fbits; return FPRoundToDouble(0, exponent, src, round); } float Simulator::FixedToFloat(int64_t src, int fbits, FPRounding round) { if (src >= 0) { return UFixedToFloat(src, fbits, round); } else { // This works for all negative values, including INT64_MIN. return -UFixedToFloat(-src, fbits, round); } } float Simulator::UFixedToFloat(uint64_t src, int fbits, FPRounding round) { // An input of 0 is a special case because the result is effectively // subnormal: The exponent is encoded as 0 and there is no implicit 1 bit. if (src == 0) { return 0.0f; } // Calculate the exponent. The highest significant bit will have the value // 2^exponent. const int highest_significant_bit = 63 - CountLeadingZeros(src, 64); const int32_t exponent = highest_significant_bit - fbits; return FPRoundToFloat(0, exponent, src, round); } double Simulator::FPRoundInt(double value, FPRounding round_mode) { if ((value == 0.0) || (value == kFP64PositiveInfinity) || (value == kFP64NegativeInfinity)) { return value; } else if (std::isnan(value)) { return FPProcessNaN(value); } double int_result = floor(value); double error = value - int_result; switch (round_mode) { case FPTieAway: { // Take care of correctly handling the range ]-0.5, -0.0], which must // yield -0.0. if ((-0.5 < value) && (value < 0.0)) { int_result = -0.0; } else if ((error > 0.5) || ((error == 0.5) && (int_result >= 0.0))) { // If the error is greater than 0.5, or is equal to 0.5 and the integer // result is positive, round up. int_result++; } break; } case FPTieEven: { // Take care of correctly handling the range [-0.5, -0.0], which must // yield -0.0. if ((-0.5 <= value) && (value < 0.0)) { int_result = -0.0; // If the error is greater than 0.5, or is equal to 0.5 and the integer // result is odd, round up. } else if ((error > 0.5) || ((error == 0.5) && (fmod(int_result, 2) != 0))) { int_result++; } break; } case FPZero: { // If value > 0 then we take floor(value) // otherwise, ceil(value) if (value < 0) { int_result = ceil(value); } break; } case FPNegativeInfinity: { // We always use floor(value). break; } default: UNIMPLEMENTED(); } return int_result; } double Simulator::FPToDouble(float value) { switch (std::fpclassify(value)) { case FP_NAN: { if (fpcr().DN()) return kFP64DefaultNaN; // Convert NaNs as the processor would: // - The sign is propagated. // - The payload (mantissa) is transferred entirely, except that the top // bit is forced to '1', making the result a quiet NaN. The unused // (low-order) payload bits are set to 0. uint32_t raw = float_to_rawbits(value); uint64_t sign = raw >> 31; uint64_t exponent = (1 << 11) - 1; uint64_t payload = unsigned_bitextract_64(21, 0, raw); payload <<= (52 - 23); // The unused low-order bits should be 0. payload |= (1L << 51); // Force a quiet NaN. return rawbits_to_double((sign << 63) | (exponent << 52) | payload); } case FP_ZERO: case FP_NORMAL: case FP_SUBNORMAL: case FP_INFINITE: { // All other inputs are preserved in a standard cast, because every value // representable using an IEEE-754 float is also representable using an // IEEE-754 double. return static_cast(value); } } UNREACHABLE(); return static_cast(value); } float Simulator::FPToFloat(double value, FPRounding round_mode) { // Only the FPTieEven rounding mode is implemented. DCHECK(round_mode == FPTieEven); USE(round_mode); switch (std::fpclassify(value)) { case FP_NAN: { if (fpcr().DN()) return kFP32DefaultNaN; // Convert NaNs as the processor would: // - The sign is propagated. // - The payload (mantissa) is transferred as much as possible, except // that the top bit is forced to '1', making the result a quiet NaN. uint64_t raw = double_to_rawbits(value); uint32_t sign = raw >> 63; uint32_t exponent = (1 << 8) - 1; uint32_t payload = unsigned_bitextract_64(50, 52 - 23, raw); payload |= (1 << 22); // Force a quiet NaN. return rawbits_to_float((sign << 31) | (exponent << 23) | payload); } case FP_ZERO: case FP_INFINITE: { // In a C++ cast, any value representable in the target type will be // unchanged. This is always the case for +/-0.0 and infinities. return static_cast(value); } case FP_NORMAL: case FP_SUBNORMAL: { // Convert double-to-float as the processor would, assuming that FPCR.FZ // (flush-to-zero) is not set. uint64_t raw = double_to_rawbits(value); // Extract the IEEE-754 double components. uint32_t sign = raw >> 63; // Extract the exponent and remove the IEEE-754 encoding bias. int32_t exponent = unsigned_bitextract_64(62, 52, raw) - 1023; // Extract the mantissa and add the implicit '1' bit. uint64_t mantissa = unsigned_bitextract_64(51, 0, raw); if (std::fpclassify(value) == FP_NORMAL) { mantissa |= (1UL << 52); } return FPRoundToFloat(sign, exponent, mantissa, round_mode); } } UNREACHABLE(); return value; } void Simulator::VisitFPDataProcessing2Source(Instruction* instr) { AssertSupportedFPCR(); unsigned fd = instr->Rd(); unsigned fn = instr->Rn(); unsigned fm = instr->Rm(); // Fmaxnm and Fminnm have special NaN handling. switch (instr->Mask(FPDataProcessing2SourceMask)) { case FMAXNM_s: set_sreg(fd, FPMaxNM(sreg(fn), sreg(fm))); return; case FMAXNM_d: set_dreg(fd, FPMaxNM(dreg(fn), dreg(fm))); return; case FMINNM_s: set_sreg(fd, FPMinNM(sreg(fn), sreg(fm))); return; case FMINNM_d: set_dreg(fd, FPMinNM(dreg(fn), dreg(fm))); return; default: break; // Fall through. } if (FPProcessNaNs(instr)) return; switch (instr->Mask(FPDataProcessing2SourceMask)) { case FADD_s: set_sreg(fd, FPAdd(sreg(fn), sreg(fm))); break; case FADD_d: set_dreg(fd, FPAdd(dreg(fn), dreg(fm))); break; case FSUB_s: set_sreg(fd, FPSub(sreg(fn), sreg(fm))); break; case FSUB_d: set_dreg(fd, FPSub(dreg(fn), dreg(fm))); break; case FMUL_s: set_sreg(fd, FPMul(sreg(fn), sreg(fm))); break; case FMUL_d: set_dreg(fd, FPMul(dreg(fn), dreg(fm))); break; case FDIV_s: set_sreg(fd, FPDiv(sreg(fn), sreg(fm))); break; case FDIV_d: set_dreg(fd, FPDiv(dreg(fn), dreg(fm))); break; case FMAX_s: set_sreg(fd, FPMax(sreg(fn), sreg(fm))); break; case FMAX_d: set_dreg(fd, FPMax(dreg(fn), dreg(fm))); break; case FMIN_s: set_sreg(fd, FPMin(sreg(fn), sreg(fm))); break; case FMIN_d: set_dreg(fd, FPMin(dreg(fn), dreg(fm))); break; case FMAXNM_s: case FMAXNM_d: case FMINNM_s: case FMINNM_d: // These were handled before the standard FPProcessNaNs() stage. UNREACHABLE(); default: UNIMPLEMENTED(); } } void Simulator::VisitFPDataProcessing3Source(Instruction* instr) { AssertSupportedFPCR(); unsigned fd = instr->Rd(); unsigned fn = instr->Rn(); unsigned fm = instr->Rm(); unsigned fa = instr->Ra(); switch (instr->Mask(FPDataProcessing3SourceMask)) { // fd = fa +/- (fn * fm) case FMADD_s: set_sreg(fd, FPMulAdd(sreg(fa), sreg(fn), sreg(fm))); break; case FMSUB_s: set_sreg(fd, FPMulAdd(sreg(fa), -sreg(fn), sreg(fm))); break; case FMADD_d: set_dreg(fd, FPMulAdd(dreg(fa), dreg(fn), dreg(fm))); break; case FMSUB_d: set_dreg(fd, FPMulAdd(dreg(fa), -dreg(fn), dreg(fm))); break; // Negated variants of the above. case FNMADD_s: set_sreg(fd, FPMulAdd(-sreg(fa), -sreg(fn), sreg(fm))); break; case FNMSUB_s: set_sreg(fd, FPMulAdd(-sreg(fa), sreg(fn), sreg(fm))); break; case FNMADD_d: set_dreg(fd, FPMulAdd(-dreg(fa), -dreg(fn), dreg(fm))); break; case FNMSUB_d: set_dreg(fd, FPMulAdd(-dreg(fa), dreg(fn), dreg(fm))); break; default: UNIMPLEMENTED(); } } template T Simulator::FPAdd(T op1, T op2) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(op1) && !std::isnan(op2)); if (std::isinf(op1) && std::isinf(op2) && (op1 != op2)) { // inf + -inf returns the default NaN. return FPDefaultNaN(); } else { // Other cases should be handled by standard arithmetic. return op1 + op2; } } template T Simulator::FPDiv(T op1, T op2) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(op1) && !std::isnan(op2)); if ((std::isinf(op1) && std::isinf(op2)) || ((op1 == 0.0) && (op2 == 0.0))) { // inf / inf and 0.0 / 0.0 return the default NaN. return FPDefaultNaN(); } else { // Other cases should be handled by standard arithmetic. return op1 / op2; } } template T Simulator::FPMax(T a, T b) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(a) && !std::isnan(b)); if ((a == 0.0) && (b == 0.0) && (copysign(1.0, a) != copysign(1.0, b))) { // a and b are zero, and the sign differs: return +0.0. return 0.0; } else { return (a > b) ? a : b; } } template T Simulator::FPMaxNM(T a, T b) { if (IsQuietNaN(a) && !IsQuietNaN(b)) { a = kFP64NegativeInfinity; } else if (!IsQuietNaN(a) && IsQuietNaN(b)) { b = kFP64NegativeInfinity; } T result = FPProcessNaNs(a, b); return std::isnan(result) ? result : FPMax(a, b); } template T Simulator::FPMin(T a, T b) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(a) && !std::isnan(b)); if ((a == 0.0) && (b == 0.0) && (copysign(1.0, a) != copysign(1.0, b))) { // a and b are zero, and the sign differs: return -0.0. return -0.0; } else { return (a < b) ? a : b; } } template T Simulator::FPMinNM(T a, T b) { if (IsQuietNaN(a) && !IsQuietNaN(b)) { a = kFP64PositiveInfinity; } else if (!IsQuietNaN(a) && IsQuietNaN(b)) { b = kFP64PositiveInfinity; } T result = FPProcessNaNs(a, b); return std::isnan(result) ? result : FPMin(a, b); } template T Simulator::FPMul(T op1, T op2) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(op1) && !std::isnan(op2)); if ((std::isinf(op1) && (op2 == 0.0)) || (std::isinf(op2) && (op1 == 0.0))) { // inf * 0.0 returns the default NaN. return FPDefaultNaN(); } else { // Other cases should be handled by standard arithmetic. return op1 * op2; } } template T Simulator::FPMulAdd(T a, T op1, T op2) { T result = FPProcessNaNs3(a, op1, op2); T sign_a = copysign(1.0, a); T sign_prod = copysign(1.0, op1) * copysign(1.0, op2); bool isinf_prod = std::isinf(op1) || std::isinf(op2); bool operation_generates_nan = (std::isinf(op1) && (op2 == 0.0)) || // inf * 0.0 (std::isinf(op2) && (op1 == 0.0)) || // 0.0 * inf (std::isinf(a) && isinf_prod && (sign_a != sign_prod)); // inf - inf if (std::isnan(result)) { // Generated NaNs override quiet NaNs propagated from a. if (operation_generates_nan && IsQuietNaN(a)) { return FPDefaultNaN(); } else { return result; } } // If the operation would produce a NaN, return the default NaN. if (operation_generates_nan) { return FPDefaultNaN(); } // Work around broken fma implementations for exact zero results: The sign of // exact 0.0 results is positive unless both a and op1 * op2 are negative. if (((op1 == 0.0) || (op2 == 0.0)) && (a == 0.0)) { return ((sign_a < 0) && (sign_prod < 0)) ? -0.0 : 0.0; } result = FusedMultiplyAdd(op1, op2, a); DCHECK(!std::isnan(result)); // Work around broken fma implementations for rounded zero results: If a is // 0.0, the sign of the result is the sign of op1 * op2 before rounding. if ((a == 0.0) && (result == 0.0)) { return copysign(0.0, sign_prod); } return result; } template T Simulator::FPSqrt(T op) { if (std::isnan(op)) { return FPProcessNaN(op); } else if (op < 0.0) { return FPDefaultNaN(); } else { return std::sqrt(op); } } template T Simulator::FPSub(T op1, T op2) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(op1) && !std::isnan(op2)); if (std::isinf(op1) && std::isinf(op2) && (op1 == op2)) { // inf - inf returns the default NaN. return FPDefaultNaN(); } else { // Other cases should be handled by standard arithmetic. return op1 - op2; } } template T Simulator::FPProcessNaN(T op) { DCHECK(std::isnan(op)); return fpcr().DN() ? FPDefaultNaN() : ToQuietNaN(op); } template T Simulator::FPProcessNaNs(T op1, T op2) { if (IsSignallingNaN(op1)) { return FPProcessNaN(op1); } else if (IsSignallingNaN(op2)) { return FPProcessNaN(op2); } else if (std::isnan(op1)) { DCHECK(IsQuietNaN(op1)); return FPProcessNaN(op1); } else if (std::isnan(op2)) { DCHECK(IsQuietNaN(op2)); return FPProcessNaN(op2); } else { return 0.0; } } template T Simulator::FPProcessNaNs3(T op1, T op2, T op3) { if (IsSignallingNaN(op1)) { return FPProcessNaN(op1); } else if (IsSignallingNaN(op2)) { return FPProcessNaN(op2); } else if (IsSignallingNaN(op3)) { return FPProcessNaN(op3); } else if (std::isnan(op1)) { DCHECK(IsQuietNaN(op1)); return FPProcessNaN(op1); } else if (std::isnan(op2)) { DCHECK(IsQuietNaN(op2)); return FPProcessNaN(op2); } else if (std::isnan(op3)) { DCHECK(IsQuietNaN(op3)); return FPProcessNaN(op3); } else { return 0.0; } } bool Simulator::FPProcessNaNs(Instruction* instr) { unsigned fd = instr->Rd(); unsigned fn = instr->Rn(); unsigned fm = instr->Rm(); bool done = false; if (instr->Mask(FP64) == FP64) { double result = FPProcessNaNs(dreg(fn), dreg(fm)); if (std::isnan(result)) { set_dreg(fd, result); done = true; } } else { float result = FPProcessNaNs(sreg(fn), sreg(fm)); if (std::isnan(result)) { set_sreg(fd, result); done = true; } } return done; } void Simulator::VisitSystem(Instruction* instr) { // Some system instructions hijack their Op and Cp fields to represent a // range of immediates instead of indicating a different instruction. This // makes the decoding tricky. if (instr->Mask(SystemSysRegFMask) == SystemSysRegFixed) { switch (instr->Mask(SystemSysRegMask)) { case MRS: { switch (instr->ImmSystemRegister()) { case NZCV: set_xreg(instr->Rt(), nzcv().RawValue()); break; case FPCR: set_xreg(instr->Rt(), fpcr().RawValue()); break; default: UNIMPLEMENTED(); } break; } case MSR: { switch (instr->ImmSystemRegister()) { case NZCV: nzcv().SetRawValue(xreg(instr->Rt())); LogSystemRegister(NZCV); break; case FPCR: fpcr().SetRawValue(xreg(instr->Rt())); LogSystemRegister(FPCR); break; default: UNIMPLEMENTED(); } break; } } } else if (instr->Mask(SystemHintFMask) == SystemHintFixed) { DCHECK(instr->Mask(SystemHintMask) == HINT); switch (instr->ImmHint()) { case NOP: break; default: UNIMPLEMENTED(); } } else if (instr->Mask(MemBarrierFMask) == MemBarrierFixed) { __sync_synchronize(); } else { UNIMPLEMENTED(); } } bool Simulator::GetValue(const char* desc, int64_t* value) { int regnum = CodeFromName(desc); if (regnum >= 0) { unsigned code = regnum; if (code == kZeroRegCode) { // Catch the zero register and return 0. *value = 0; return true; } else if (code == kSPRegInternalCode) { // Translate the stack pointer code to 31, for Reg31IsStackPointer. code = 31; } if (desc[0] == 'w') { *value = wreg(code, Reg31IsStackPointer); } else { *value = xreg(code, Reg31IsStackPointer); } return true; } else if (strncmp(desc, "0x", 2) == 0) { return SScanF(desc + 2, "%" SCNx64, reinterpret_cast(value)) == 1; } else { return SScanF(desc, "%" SCNu64, reinterpret_cast(value)) == 1; } } bool Simulator::PrintValue(const char* desc) { if (strcmp(desc, "csp") == 0) { DCHECK(CodeFromName(desc) == static_cast(kSPRegInternalCode)); PrintF(stream_, "%s csp:%s 0x%016" PRIx64 "%s\n", clr_reg_name, clr_reg_value, xreg(31, Reg31IsStackPointer), clr_normal); return true; } else if (strcmp(desc, "wcsp") == 0) { DCHECK(CodeFromName(desc) == static_cast(kSPRegInternalCode)); PrintF(stream_, "%s wcsp:%s 0x%08" PRIx32 "%s\n", clr_reg_name, clr_reg_value, wreg(31, Reg31IsStackPointer), clr_normal); return true; } int i = CodeFromName(desc); STATIC_ASSERT(kNumberOfRegisters == kNumberOfFPRegisters); if (i < 0 || static_cast(i) >= kNumberOfFPRegisters) return false; if (desc[0] == 'v') { PrintF(stream_, "%s %s:%s 0x%016" PRIx64 "%s (%s%s:%s %g%s %s:%s %g%s)\n", clr_fpreg_name, VRegNameForCode(i), clr_fpreg_value, double_to_rawbits(dreg(i)), clr_normal, clr_fpreg_name, DRegNameForCode(i), clr_fpreg_value, dreg(i), clr_fpreg_name, SRegNameForCode(i), clr_fpreg_value, sreg(i), clr_normal); return true; } else if (desc[0] == 'd') { PrintF(stream_, "%s %s:%s %g%s\n", clr_fpreg_name, DRegNameForCode(i), clr_fpreg_value, dreg(i), clr_normal); return true; } else if (desc[0] == 's') { PrintF(stream_, "%s %s:%s %g%s\n", clr_fpreg_name, SRegNameForCode(i), clr_fpreg_value, sreg(i), clr_normal); return true; } else if (desc[0] == 'w') { PrintF(stream_, "%s %s:%s 0x%08" PRIx32 "%s\n", clr_reg_name, WRegNameForCode(i), clr_reg_value, wreg(i), clr_normal); return true; } else { // X register names have a wide variety of starting characters, but anything // else will be an X register. PrintF(stream_, "%s %s:%s 0x%016" PRIx64 "%s\n", clr_reg_name, XRegNameForCode(i), clr_reg_value, xreg(i), clr_normal); return true; } } void Simulator::Debug() { #define COMMAND_SIZE 63 #define ARG_SIZE 255 #define STR(a) #a #define XSTR(a) STR(a) char cmd[COMMAND_SIZE + 1]; char arg1[ARG_SIZE + 1]; char arg2[ARG_SIZE + 1]; char* argv[3] = { cmd, arg1, arg2 }; // Make sure to have a proper terminating character if reaching the limit. cmd[COMMAND_SIZE] = 0; arg1[ARG_SIZE] = 0; arg2[ARG_SIZE] = 0; bool done = false; bool cleared_log_disasm_bit = false; while (!done) { // Disassemble the next instruction to execute before doing anything else. PrintInstructionsAt(pc_, 1); // Read the command line. char* line = ReadLine("sim> "); if (line == NULL) { break; } else { // Repeat last command by default. char* last_input = last_debugger_input(); if (strcmp(line, "\n") == 0 && (last_input != NULL)) { DeleteArray(line); line = last_input; } else { // Update the latest command ran set_last_debugger_input(line); } // Use sscanf to parse the individual parts of the command line. At the // moment no command expects more than two parameters. int argc = SScanF(line, "%" XSTR(COMMAND_SIZE) "s " "%" XSTR(ARG_SIZE) "s " "%" XSTR(ARG_SIZE) "s", cmd, arg1, arg2); // stepi / si ------------------------------------------------------------ if ((strcmp(cmd, "si") == 0) || (strcmp(cmd, "stepi") == 0)) { // We are about to execute instructions, after which by default we // should increment the pc_. If it was set when reaching this debug // instruction, it has not been cleared because this instruction has not // completed yet. So clear it manually. pc_modified_ = false; if (argc == 1) { ExecuteInstruction(); } else { int64_t number_of_instructions_to_execute = 1; GetValue(arg1, &number_of_instructions_to_execute); set_log_parameters(log_parameters() | LOG_DISASM); while (number_of_instructions_to_execute-- > 0) { ExecuteInstruction(); } set_log_parameters(log_parameters() & ~LOG_DISASM); PrintF("\n"); } // If it was necessary, the pc has already been updated or incremented // when executing the instruction. So we do not want it to be updated // again. It will be cleared when exiting. pc_modified_ = true; // next / n -------------------------------------------------------------- } else if ((strcmp(cmd, "next") == 0) || (strcmp(cmd, "n") == 0)) { // Tell the simulator to break after the next executed BL. break_on_next_ = true; // Continue. done = true; // continue / cont / c --------------------------------------------------- } else if ((strcmp(cmd, "continue") == 0) || (strcmp(cmd, "cont") == 0) || (strcmp(cmd, "c") == 0)) { // Leave the debugger shell. done = true; // disassemble / disasm / di --------------------------------------------- } else if (strcmp(cmd, "disassemble") == 0 || strcmp(cmd, "disasm") == 0 || strcmp(cmd, "di") == 0) { int64_t n_of_instrs_to_disasm = 10; // default value. int64_t address = reinterpret_cast(pc_); // default value. if (argc >= 2) { // disasm GetValue(arg1, &n_of_instrs_to_disasm); } if (argc >= 3) { // disasm GetValue(arg2, &address); } // Disassemble. PrintInstructionsAt(reinterpret_cast(address), n_of_instrs_to_disasm); PrintF("\n"); // print / p ------------------------------------------------------------- } else if ((strcmp(cmd, "print") == 0) || (strcmp(cmd, "p") == 0)) { if (argc == 2) { if (strcmp(arg1, "all") == 0) { PrintRegisters(); PrintFPRegisters(); } else { if (!PrintValue(arg1)) { PrintF("%s unrecognized\n", arg1); } } } else { PrintF( "print \n" " Print the content of a register. (alias 'p')\n" " 'print all' will print all registers.\n" " Use 'printobject' to get more details about the value.\n"); } // printobject / po ------------------------------------------------------ } else if ((strcmp(cmd, "printobject") == 0) || (strcmp(cmd, "po") == 0)) { if (argc == 2) { int64_t value; OFStream os(stdout); if (GetValue(arg1, &value)) { Object* obj = reinterpret_cast(value); os << arg1 << ": \n"; #ifdef DEBUG obj->Print(os); os << "\n"; #else os << Brief(obj) << "\n"; #endif } else { os << arg1 << " unrecognized\n"; } } else { PrintF("printobject \n" "printobject \n" " Print details about the value. (alias 'po')\n"); } // stack / mem ---------------------------------------------------------- } else if (strcmp(cmd, "stack") == 0 || strcmp(cmd, "mem") == 0) { int64_t* cur = NULL; int64_t* end = NULL; int next_arg = 1; if (strcmp(cmd, "stack") == 0) { cur = reinterpret_cast(jssp()); } else { // "mem" int64_t value; if (!GetValue(arg1, &value)) { PrintF("%s unrecognized\n", arg1); continue; } cur = reinterpret_cast(value); next_arg++; } int64_t words = 0; if (argc == next_arg) { words = 10; } else if (argc == next_arg + 1) { if (!GetValue(argv[next_arg], &words)) { PrintF("%s unrecognized\n", argv[next_arg]); PrintF("Printing 10 double words by default"); words = 10; } } else { UNREACHABLE(); } end = cur + words; while (cur < end) { PrintF(" 0x%016" PRIx64 ": 0x%016" PRIx64 " %10" PRId64, reinterpret_cast(cur), *cur, *cur); HeapObject* obj = reinterpret_cast(*cur); int64_t value = *cur; Heap* current_heap = v8::internal::Isolate::Current()->heap(); if (((value & 1) == 0) || current_heap->Contains(obj)) { PrintF(" ("); if ((value & kSmiTagMask) == 0) { STATIC_ASSERT(kSmiValueSize == 32); int32_t untagged = (value >> kSmiShift) & 0xffffffff; PrintF("smi %" PRId32, untagged); } else { obj->ShortPrint(); } PrintF(")"); } PrintF("\n"); cur++; } // trace / t ------------------------------------------------------------- } else if (strcmp(cmd, "trace") == 0 || strcmp(cmd, "t") == 0) { if ((log_parameters() & (LOG_DISASM | LOG_REGS)) != (LOG_DISASM | LOG_REGS)) { PrintF("Enabling disassembly and registers tracing\n"); set_log_parameters(log_parameters() | LOG_DISASM | LOG_REGS); } else { PrintF("Disabling disassembly and registers tracing\n"); set_log_parameters(log_parameters() & ~(LOG_DISASM | LOG_REGS)); } // break / b ------------------------------------------------------------- } else if (strcmp(cmd, "break") == 0 || strcmp(cmd, "b") == 0) { if (argc == 2) { int64_t value; if (GetValue(arg1, &value)) { SetBreakpoint(reinterpret_cast(value)); } else { PrintF("%s unrecognized\n", arg1); } } else { ListBreakpoints(); PrintF("Use `break ` to set or disable a breakpoint\n"); } // gdb ------------------------------------------------------------------- } else if (strcmp(cmd, "gdb") == 0) { PrintF("Relinquishing control to gdb.\n"); base::OS::DebugBreak(); PrintF("Regaining control from gdb.\n"); // sysregs --------------------------------------------------------------- } else if (strcmp(cmd, "sysregs") == 0) { PrintSystemRegisters(); // help / h -------------------------------------------------------------- } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "h") == 0) { PrintF( "stepi / si\n" " stepi \n" " Step instructions.\n" "next / n\n" " Continue execution until a BL instruction is reached.\n" " At this point a breakpoint is set just after this BL.\n" " Then execution is resumed. It will probably later hit the\n" " breakpoint just set.\n" "continue / cont / c\n" " Continue execution from here.\n" "disassemble / disasm / di\n" " disassemble \n" " Disassemble
:bits (d:value, ...)". DCHECK(sizes != 0); DCHECK((sizes & kPrintAllFPRegValues) == sizes); // Print the raw bits. fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s (", clr_fpreg_name, VRegNameForCode(code), clr_fpreg_value, fpreg(code), clr_normal); // Print all requested value interpretations. bool need_separator = false; if (sizes & kPrintDRegValue) { fprintf(stream_, "%s%s%s: %s%g%s", need_separator ? ", " : "", clr_fpreg_name, DRegNameForCode(code), clr_fpreg_value, fpreg(code), clr_normal); need_separator = true; } if (sizes & kPrintSRegValue) { fprintf(stream_, "%s%s%s: %s%g%s", need_separator ? ", " : "", clr_fpreg_name, SRegNameForCode(code), clr_fpreg_value, fpreg(code), clr_normal); need_separator = true; } // End the value list. fprintf(stream_, ")\n"); } void Simulator::PrintSystemRegister(SystemRegister id) { switch (id) { case NZCV: fprintf(stream_, "# %sNZCV: %sN:%d Z:%d C:%d V:%d%s\n", clr_flag_name, clr_flag_value, nzcv().N(), nzcv().Z(), nzcv().C(), nzcv().V(), clr_normal); break; case FPCR: { static const char * rmode[] = { "0b00 (Round to Nearest)", "0b01 (Round towards Plus Infinity)", "0b10 (Round towards Minus Infinity)", "0b11 (Round towards Zero)" }; DCHECK(fpcr().RMode() < arraysize(rmode)); fprintf(stream_, "# %sFPCR: %sAHP:%d DN:%d FZ:%d RMode:%s%s\n", clr_flag_name, clr_flag_value, fpcr().AHP(), fpcr().DN(), fpcr().FZ(), rmode[fpcr().RMode()], clr_normal); break; } default: UNREACHABLE(); } } void Simulator::PrintRead(uintptr_t address, size_t size, unsigned reg_code) { USE(size); // Size is unused here. // The template is "# x:value <- address". fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s", clr_reg_name, XRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); fprintf(stream_, " <- %s0x%016" PRIxPTR "%s\n", clr_memory_address, address, clr_normal); } void Simulator::PrintReadFP(uintptr_t address, size_t size, unsigned reg_code) { // The template is "# reg:bits (reg:value) <- address". switch (size) { case kSRegSize: fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s (%s%s: %s%gf%s)", clr_fpreg_name, VRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal, clr_fpreg_name, SRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal); break; case kDRegSize: fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s (%s%s: %s%g%s)", clr_fpreg_name, VRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal, clr_fpreg_name, DRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal); break; default: UNREACHABLE(); } fprintf(stream_, " <- %s0x%016" PRIxPTR "%s\n", clr_memory_address, address, clr_normal); } void Simulator::PrintWrite(uintptr_t address, size_t size, unsigned reg_code) { // The template is "# reg:value -> address". To keep the trace tidy and // readable, the value is aligned with the values in the register trace. switch (size) { case kByteSizeInBytes: fprintf(stream_, "# %s%5s<7:0>: %s0x%02" PRIx8 "%s", clr_reg_name, WRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); break; case kHalfWordSizeInBytes: fprintf(stream_, "# %s%5s<15:0>: %s0x%04" PRIx16 "%s", clr_reg_name, WRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); break; case kWRegSize: fprintf(stream_, "# %s%5s: %s0x%08" PRIx32 "%s", clr_reg_name, WRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); break; case kXRegSize: fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s", clr_reg_name, XRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); break; default: UNREACHABLE(); } fprintf(stream_, " -> %s0x%016" PRIxPTR "%s\n", clr_memory_address, address, clr_normal); } void Simulator::PrintWriteFP(uintptr_t address, size_t size, unsigned reg_code) { // The template is "# reg:bits (reg:value) -> address". To keep the trace tidy // and readable, the value is aligned with the values in the register trace. switch (size) { case kSRegSize: fprintf(stream_, "# %s%5s<31:0>: %s0x%08" PRIx32 "%s (%s%s: %s%gf%s)", clr_fpreg_name, VRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal, clr_fpreg_name, SRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal); break; case kDRegSize: fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s (%s%s: %s%g%s)", clr_fpreg_name, VRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal, clr_fpreg_name, DRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal); break; default: UNREACHABLE(); } fprintf(stream_, " -> %s0x%016" PRIxPTR "%s\n", clr_memory_address, address, clr_normal); } // Visitors--------------------------------------------------------------------- void Simulator::VisitUnimplemented(Instruction* instr) { fprintf(stream_, "Unimplemented instruction at %p: 0x%08" PRIx32 "\n", reinterpret_cast(instr), instr->InstructionBits()); UNIMPLEMENTED(); } void Simulator::VisitUnallocated(Instruction* instr) { fprintf(stream_, "Unallocated instruction at %p: 0x%08" PRIx32 "\n", reinterpret_cast(instr), instr->InstructionBits()); UNIMPLEMENTED(); } void Simulator::VisitPCRelAddressing(Instruction* instr) { switch (instr->Mask(PCRelAddressingMask)) { case ADR: set_reg(instr->Rd(), instr->ImmPCOffsetTarget()); break; case ADRP: // Not implemented in the assembler. UNIMPLEMENTED(); break; default: UNREACHABLE(); break; } } void Simulator::VisitUnconditionalBranch(Instruction* instr) { switch (instr->Mask(UnconditionalBranchMask)) { case BL: set_lr(instr->following()); // Fall through. case B: set_pc(instr->ImmPCOffsetTarget()); break; default: UNREACHABLE(); } } void Simulator::VisitConditionalBranch(Instruction* instr) { DCHECK(instr->Mask(ConditionalBranchMask) == B_cond); if (ConditionPassed(static_cast(instr->ConditionBranch()))) { set_pc(instr->ImmPCOffsetTarget()); } } void Simulator::VisitUnconditionalBranchToRegister(Instruction* instr) { Instruction* target = reg(instr->Rn()); switch (instr->Mask(UnconditionalBranchToRegisterMask)) { case BLR: { set_lr(instr->following()); if (instr->Rn() == 31) { // BLR XZR is used as a guard for the constant pool. We should never hit // this, but if we do trap to allow debugging. Debug(); } // Fall through. } case BR: case RET: set_pc(target); break; default: UNIMPLEMENTED(); } } void Simulator::VisitTestBranch(Instruction* instr) { unsigned bit_pos = (instr->ImmTestBranchBit5() << 5) | instr->ImmTestBranchBit40(); bool take_branch = ((xreg(instr->Rt()) & (1UL << bit_pos)) == 0); switch (instr->Mask(TestBranchMask)) { case TBZ: break; case TBNZ: take_branch = !take_branch; break; default: UNIMPLEMENTED(); } if (take_branch) { set_pc(instr->ImmPCOffsetTarget()); } } void Simulator::VisitCompareBranch(Instruction* instr) { unsigned rt = instr->Rt(); bool take_branch = false; switch (instr->Mask(CompareBranchMask)) { case CBZ_w: take_branch = (wreg(rt) == 0); break; case CBZ_x: take_branch = (xreg(rt) == 0); break; case CBNZ_w: take_branch = (wreg(rt) != 0); break; case CBNZ_x: take_branch = (xreg(rt) != 0); break; default: UNIMPLEMENTED(); } if (take_branch) { set_pc(instr->ImmPCOffsetTarget()); } } template void Simulator::AddSubHelper(Instruction* instr, T op2) { bool set_flags = instr->FlagsUpdate(); T new_val = 0; Instr operation = instr->Mask(AddSubOpMask); switch (operation) { case ADD: case ADDS: { new_val = AddWithCarry(set_flags, reg(instr->Rn(), instr->RnMode()), op2); break; } case SUB: case SUBS: { new_val = AddWithCarry(set_flags, reg(instr->Rn(), instr->RnMode()), ~op2, 1); break; } default: UNREACHABLE(); } set_reg(instr->Rd(), new_val, instr->RdMode()); } void Simulator::VisitAddSubShifted(Instruction* instr) { Shift shift_type = static_cast(instr->ShiftDP()); unsigned shift_amount = instr->ImmDPShift(); if (instr->SixtyFourBits()) { int64_t op2 = ShiftOperand(xreg(instr->Rm()), shift_type, shift_amount); AddSubHelper(instr, op2); } else { int32_t op2 = ShiftOperand(wreg(instr->Rm()), shift_type, shift_amount); AddSubHelper(instr, op2); } } void Simulator::VisitAddSubImmediate(Instruction* instr) { int64_t op2 = instr->ImmAddSub() << ((instr->ShiftAddSub() == 1) ? 12 : 0); if (instr->SixtyFourBits()) { AddSubHelper(instr, op2); } else { AddSubHelper(instr, op2); } } void Simulator::VisitAddSubExtended(Instruction* instr) { Extend ext = static_cast(instr->ExtendMode()); unsigned left_shift = instr->ImmExtendShift(); if (instr->SixtyFourBits()) { int64_t op2 = ExtendValue(xreg(instr->Rm()), ext, left_shift); AddSubHelper(instr, op2); } else { int32_t op2 = ExtendValue(wreg(instr->Rm()), ext, left_shift); AddSubHelper(instr, op2); } } void Simulator::VisitAddSubWithCarry(Instruction* instr) { if (instr->SixtyFourBits()) { AddSubWithCarry(instr); } else { AddSubWithCarry(instr); } } void Simulator::VisitLogicalShifted(Instruction* instr) { Shift shift_type = static_cast(instr->ShiftDP()); unsigned shift_amount = instr->ImmDPShift(); if (instr->SixtyFourBits()) { int64_t op2 = ShiftOperand(xreg(instr->Rm()), shift_type, shift_amount); op2 = (instr->Mask(NOT) == NOT) ? ~op2 : op2; LogicalHelper(instr, op2); } else { int32_t op2 = ShiftOperand(wreg(instr->Rm()), shift_type, shift_amount); op2 = (instr->Mask(NOT) == NOT) ? ~op2 : op2; LogicalHelper(instr, op2); } } void Simulator::VisitLogicalImmediate(Instruction* instr) { if (instr->SixtyFourBits()) { LogicalHelper(instr, instr->ImmLogical()); } else { LogicalHelper(instr, instr->ImmLogical()); } } template void Simulator::LogicalHelper(Instruction* instr, T op2) { T op1 = reg(instr->Rn()); T result = 0; bool update_flags = false; // Switch on the logical operation, stripping out the NOT bit, as it has a // different meaning for logical immediate instructions. switch (instr->Mask(LogicalOpMask & ~NOT)) { case ANDS: update_flags = true; // Fall through. case AND: result = op1 & op2; break; case ORR: result = op1 | op2; break; case EOR: result = op1 ^ op2; break; default: UNIMPLEMENTED(); } if (update_flags) { nzcv().SetN(CalcNFlag(result)); nzcv().SetZ(CalcZFlag(result)); nzcv().SetC(0); nzcv().SetV(0); LogSystemRegister(NZCV); } set_reg(instr->Rd(), result, instr->RdMode()); } void Simulator::VisitConditionalCompareRegister(Instruction* instr) { if (instr->SixtyFourBits()) { ConditionalCompareHelper(instr, xreg(instr->Rm())); } else { ConditionalCompareHelper(instr, wreg(instr->Rm())); } } void Simulator::VisitConditionalCompareImmediate(Instruction* instr) { if (instr->SixtyFourBits()) { ConditionalCompareHelper(instr, instr->ImmCondCmp()); } else { ConditionalCompareHelper(instr, instr->ImmCondCmp()); } } template void Simulator::ConditionalCompareHelper(Instruction* instr, T op2) { T op1 = reg(instr->Rn()); if (ConditionPassed(static_cast(instr->Condition()))) { // If the condition passes, set the status flags to the result of comparing // the operands. if (instr->Mask(ConditionalCompareMask) == CCMP) { AddWithCarry(true, op1, ~op2, 1); } else { DCHECK(instr->Mask(ConditionalCompareMask) == CCMN); AddWithCarry(true, op1, op2, 0); } } else { // If the condition fails, set the status flags to the nzcv immediate. nzcv().SetFlags(instr->Nzcv()); LogSystemRegister(NZCV); } } void Simulator::VisitLoadStoreUnsignedOffset(Instruction* instr) { int offset = instr->ImmLSUnsigned() << instr->SizeLS(); LoadStoreHelper(instr, offset, Offset); } void Simulator::VisitLoadStoreUnscaledOffset(Instruction* instr) { LoadStoreHelper(instr, instr->ImmLS(), Offset); } void Simulator::VisitLoadStorePreIndex(Instruction* instr) { LoadStoreHelper(instr, instr->ImmLS(), PreIndex); } void Simulator::VisitLoadStorePostIndex(Instruction* instr) { LoadStoreHelper(instr, instr->ImmLS(), PostIndex); } void Simulator::VisitLoadStoreRegisterOffset(Instruction* instr) { Extend ext = static_cast(instr->ExtendMode()); DCHECK((ext == UXTW) || (ext == UXTX) || (ext == SXTW) || (ext == SXTX)); unsigned shift_amount = instr->ImmShiftLS() * instr->SizeLS(); int64_t offset = ExtendValue(xreg(instr->Rm()), ext, shift_amount); LoadStoreHelper(instr, offset, Offset); } void Simulator::LoadStoreHelper(Instruction* instr, int64_t offset, AddrMode addrmode) { unsigned srcdst = instr->Rt(); unsigned addr_reg = instr->Rn(); uintptr_t address = LoadStoreAddress(addr_reg, offset, addrmode); uintptr_t stack = 0; // Handle the writeback for stores before the store. On a CPU the writeback // and the store are atomic, but when running on the simulator it is possible // to be interrupted in between. The simulator is not thread safe and V8 does // not require it to be to run JavaScript therefore the profiler may sample // the "simulated" CPU in the middle of load/store with writeback. The code // below ensures that push operations are safe even when interrupted: the // stack pointer will be decremented before adding an element to the stack. if (instr->IsStore()) { LoadStoreWriteBack(addr_reg, offset, addrmode); // For store the address post writeback is used to check access below the // stack. stack = sp(); } LoadStoreOp op = static_cast(instr->Mask(LoadStoreOpMask)); switch (op) { // Use _no_log variants to suppress the register trace (LOG_REGS, // LOG_FP_REGS). We will print a more detailed log. case LDRB_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDRH_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDR_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDR_x: set_xreg_no_log(srcdst, MemoryRead(address)); break; case LDRSB_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDRSH_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDRSB_x: set_xreg_no_log(srcdst, MemoryRead(address)); break; case LDRSH_x: set_xreg_no_log(srcdst, MemoryRead(address)); break; case LDRSW_x: set_xreg_no_log(srcdst, MemoryRead(address)); break; case LDR_s: set_sreg_no_log(srcdst, MemoryRead(address)); break; case LDR_d: set_dreg_no_log(srcdst, MemoryRead(address)); break; case STRB_w: MemoryWrite(address, wreg(srcdst)); break; case STRH_w: MemoryWrite(address, wreg(srcdst)); break; case STR_w: MemoryWrite(address, wreg(srcdst)); break; case STR_x: MemoryWrite(address, xreg(srcdst)); break; case STR_s: MemoryWrite(address, sreg(srcdst)); break; case STR_d: MemoryWrite(address, dreg(srcdst)); break; default: UNIMPLEMENTED(); } // Print a detailed trace (including the memory address) instead of the basic // register:value trace generated by set_*reg(). size_t access_size = 1 << instr->SizeLS(); if (instr->IsLoad()) { if ((op == LDR_s) || (op == LDR_d)) { LogReadFP(address, access_size, srcdst); } else { LogRead(address, access_size, srcdst); } } else { if ((op == STR_s) || (op == STR_d)) { LogWriteFP(address, access_size, srcdst); } else { LogWrite(address, access_size, srcdst); } } // Handle the writeback for loads after the load to ensure safe pop // operation even when interrupted in the middle of it. The stack pointer // is only updated after the load so pop(fp) will never break the invariant // sp <= fp expected while walking the stack in the sampler. if (instr->IsLoad()) { // For loads the address pre writeback is used to check access below the // stack. stack = sp(); LoadStoreWriteBack(addr_reg, offset, addrmode); } // Accesses below the stack pointer (but above the platform stack limit) are // not allowed in the ABI. CheckMemoryAccess(address, stack); } void Simulator::VisitLoadStorePairOffset(Instruction* instr) { LoadStorePairHelper(instr, Offset); } void Simulator::VisitLoadStorePairPreIndex(Instruction* instr) { LoadStorePairHelper(instr, PreIndex); } void Simulator::VisitLoadStorePairPostIndex(Instruction* instr) { LoadStorePairHelper(instr, PostIndex); } void Simulator::VisitLoadStorePairNonTemporal(Instruction* instr) { LoadStorePairHelper(instr, Offset); } void Simulator::LoadStorePairHelper(Instruction* instr, AddrMode addrmode) { unsigned rt = instr->Rt(); unsigned rt2 = instr->Rt2(); unsigned addr_reg = instr->Rn(); size_t access_size = 1 << instr->SizeLSPair(); int64_t offset = instr->ImmLSPair() * access_size; uintptr_t address = LoadStoreAddress(addr_reg, offset, addrmode); uintptr_t address2 = address + access_size; uintptr_t stack = 0; // Handle the writeback for stores before the store. On a CPU the writeback // and the store are atomic, but when running on the simulator it is possible // to be interrupted in between. The simulator is not thread safe and V8 does // not require it to be to run JavaScript therefore the profiler may sample // the "simulated" CPU in the middle of load/store with writeback. The code // below ensures that push operations are safe even when interrupted: the // stack pointer will be decremented before adding an element to the stack. if (instr->IsStore()) { LoadStoreWriteBack(addr_reg, offset, addrmode); // For store the address post writeback is used to check access below the // stack. stack = sp(); } LoadStorePairOp op = static_cast(instr->Mask(LoadStorePairMask)); // 'rt' and 'rt2' can only be aliased for stores. DCHECK(((op & LoadStorePairLBit) == 0) || (rt != rt2)); switch (op) { // Use _no_log variants to suppress the register trace (LOG_REGS, // LOG_FP_REGS). We will print a more detailed log. case LDP_w: { DCHECK(access_size == kWRegSize); set_wreg_no_log(rt, MemoryRead(address)); set_wreg_no_log(rt2, MemoryRead(address2)); break; } case LDP_s: { DCHECK(access_size == kSRegSize); set_sreg_no_log(rt, MemoryRead(address)); set_sreg_no_log(rt2, MemoryRead(address2)); break; } case LDP_x: { DCHECK(access_size == kXRegSize); set_xreg_no_log(rt, MemoryRead(address)); set_xreg_no_log(rt2, MemoryRead(address2)); break; } case LDP_d: { DCHECK(access_size == kDRegSize); set_dreg_no_log(rt, MemoryRead(address)); set_dreg_no_log(rt2, MemoryRead(address2)); break; } case LDPSW_x: { DCHECK(access_size == kWRegSize); set_xreg_no_log(rt, MemoryRead(address)); set_xreg_no_log(rt2, MemoryRead(address2)); break; } case STP_w: { DCHECK(access_size == kWRegSize); MemoryWrite(address, wreg(rt)); MemoryWrite(address2, wreg(rt2)); break; } case STP_s: { DCHECK(access_size == kSRegSize); MemoryWrite(address, sreg(rt)); MemoryWrite(address2, sreg(rt2)); break; } case STP_x: { DCHECK(access_size == kXRegSize); MemoryWrite(address, xreg(rt)); MemoryWrite(address2, xreg(rt2)); break; } case STP_d: { DCHECK(access_size == kDRegSize); MemoryWrite(address, dreg(rt)); MemoryWrite(address2, dreg(rt2)); break; } default: UNREACHABLE(); } // Print a detailed trace (including the memory address) instead of the basic // register:value trace generated by set_*reg(). if (instr->IsLoad()) { if ((op == LDP_s) || (op == LDP_d)) { LogReadFP(address, access_size, rt); LogReadFP(address2, access_size, rt2); } else { LogRead(address, access_size, rt); LogRead(address2, access_size, rt2); } } else { if ((op == STP_s) || (op == STP_d)) { LogWriteFP(address, access_size, rt); LogWriteFP(address2, access_size, rt2); } else { LogWrite(address, access_size, rt); LogWrite(address2, access_size, rt2); } } // Handle the writeback for loads after the load to ensure safe pop // operation even when interrupted in the middle of it. The stack pointer // is only updated after the load so pop(fp) will never break the invariant // sp <= fp expected while walking the stack in the sampler. if (instr->IsLoad()) { // For loads the address pre writeback is used to check access below the // stack. stack = sp(); LoadStoreWriteBack(addr_reg, offset, addrmode); } // Accesses below the stack pointer (but above the platform stack limit) are // not allowed in the ABI. CheckMemoryAccess(address, stack); } void Simulator::VisitLoadLiteral(Instruction* instr) { uintptr_t address = instr->LiteralAddress(); unsigned rt = instr->Rt(); switch (instr->Mask(LoadLiteralMask)) { // Use _no_log variants to suppress the register trace (LOG_REGS, // LOG_FP_REGS), then print a more detailed log. case LDR_w_lit: set_wreg_no_log(rt, MemoryRead(address)); LogRead(address, kWRegSize, rt); break; case LDR_x_lit: set_xreg_no_log(rt, MemoryRead(address)); LogRead(address, kXRegSize, rt); break; case LDR_s_lit: set_sreg_no_log(rt, MemoryRead(address)); LogReadFP(address, kSRegSize, rt); break; case LDR_d_lit: set_dreg_no_log(rt, MemoryRead(address)); LogReadFP(address, kDRegSize, rt); break; default: UNREACHABLE(); } } uintptr_t Simulator::LoadStoreAddress(unsigned addr_reg, int64_t offset, AddrMode addrmode) { const unsigned kSPRegCode = kSPRegInternalCode & kRegCodeMask; uint64_t address = xreg(addr_reg, Reg31IsStackPointer); if ((addr_reg == kSPRegCode) && ((address % 16) != 0)) { // When the base register is SP the stack pointer is required to be // quadword aligned prior to the address calculation and write-backs. // Misalignment will cause a stack alignment fault. FATAL("ALIGNMENT EXCEPTION"); } if ((addrmode == Offset) || (addrmode == PreIndex)) { address += offset; } return address; } void Simulator::LoadStoreWriteBack(unsigned addr_reg, int64_t offset, AddrMode addrmode) { if ((addrmode == PreIndex) || (addrmode == PostIndex)) { DCHECK(offset != 0); uint64_t address = xreg(addr_reg, Reg31IsStackPointer); set_reg(addr_reg, address + offset, Reg31IsStackPointer); } } void Simulator::CheckMemoryAccess(uintptr_t address, uintptr_t stack) { if ((address >= stack_limit_) && (address < stack)) { fprintf(stream_, "ACCESS BELOW STACK POINTER:\n"); fprintf(stream_, " sp is here: 0x%016" PRIx64 "\n", static_cast(stack)); fprintf(stream_, " access was here: 0x%016" PRIx64 "\n", static_cast(address)); fprintf(stream_, " stack limit is here: 0x%016" PRIx64 "\n", static_cast(stack_limit_)); fprintf(stream_, "\n"); FATAL("ACCESS BELOW STACK POINTER"); } } void Simulator::VisitMoveWideImmediate(Instruction* instr) { MoveWideImmediateOp mov_op = static_cast(instr->Mask(MoveWideImmediateMask)); int64_t new_xn_val = 0; bool is_64_bits = instr->SixtyFourBits() == 1; // Shift is limited for W operations. DCHECK(is_64_bits || (instr->ShiftMoveWide() < 2)); // Get the shifted immediate. int64_t shift = instr->ShiftMoveWide() * 16; int64_t shifted_imm16 = instr->ImmMoveWide() << shift; // Compute the new value. switch (mov_op) { case MOVN_w: case MOVN_x: { new_xn_val = ~shifted_imm16; if (!is_64_bits) new_xn_val &= kWRegMask; break; } case MOVK_w: case MOVK_x: { unsigned reg_code = instr->Rd(); int64_t prev_xn_val = is_64_bits ? xreg(reg_code) : wreg(reg_code); new_xn_val = (prev_xn_val & ~(0xffffL << shift)) | shifted_imm16; break; } case MOVZ_w: case MOVZ_x: { new_xn_val = shifted_imm16; break; } default: UNREACHABLE(); } // Update the destination register. set_xreg(instr->Rd(), new_xn_val); } void Simulator::VisitConditionalSelect(Instruction* instr) { if (ConditionFailed(static_cast(instr->Condition()))) { uint64_t new_val = xreg(instr->Rm()); switch (instr->Mask(ConditionalSelectMask)) { case CSEL_w: set_wreg(instr->Rd(), new_val); break; case CSEL_x: set_xreg(instr->Rd(), new_val); break; case CSINC_w: set_wreg(instr->Rd(), new_val + 1); break; case CSINC_x: set_xreg(instr->Rd(), new_val + 1); break; case CSINV_w: set_wreg(instr->Rd(), ~new_val); break; case CSINV_x: set_xreg(instr->Rd(), ~new_val); break; case CSNEG_w: set_wreg(instr->Rd(), -new_val); break; case CSNEG_x: set_xreg(instr->Rd(), -new_val); break; default: UNIMPLEMENTED(); } } else { if (instr->SixtyFourBits()) { set_xreg(instr->Rd(), xreg(instr->Rn())); } else { set_wreg(instr->Rd(), wreg(instr->Rn())); } } } void Simulator::VisitDataProcessing1Source(Instruction* instr) { unsigned dst = instr->Rd(); unsigned src = instr->Rn(); switch (instr->Mask(DataProcessing1SourceMask)) { case RBIT_w: set_wreg(dst, ReverseBits(wreg(src), kWRegSizeInBits)); break; case RBIT_x: set_xreg(dst, ReverseBits(xreg(src), kXRegSizeInBits)); break; case REV16_w: set_wreg(dst, ReverseBytes(wreg(src), Reverse16)); break; case REV16_x: set_xreg(dst, ReverseBytes(xreg(src), Reverse16)); break; case REV_w: set_wreg(dst, ReverseBytes(wreg(src), Reverse32)); break; case REV32_x: set_xreg(dst, ReverseBytes(xreg(src), Reverse32)); break; case REV_x: set_xreg(dst, ReverseBytes(xreg(src), Reverse64)); break; case CLZ_w: set_wreg(dst, CountLeadingZeros(wreg(src), kWRegSizeInBits)); break; case CLZ_x: set_xreg(dst, CountLeadingZeros(xreg(src), kXRegSizeInBits)); break; case CLS_w: { set_wreg(dst, CountLeadingSignBits(wreg(src), kWRegSizeInBits)); break; } case CLS_x: { set_xreg(dst, CountLeadingSignBits(xreg(src), kXRegSizeInBits)); break; } default: UNIMPLEMENTED(); } } uint64_t Simulator::ReverseBits(uint64_t value, unsigned num_bits) { DCHECK((num_bits == kWRegSizeInBits) || (num_bits == kXRegSizeInBits)); uint64_t result = 0; for (unsigned i = 0; i < num_bits; i++) { result = (result << 1) | (value & 1); value >>= 1; } return result; } uint64_t Simulator::ReverseBytes(uint64_t value, ReverseByteMode mode) { // Split the 64-bit value into an 8-bit array, where b[0] is the least // significant byte, and b[7] is the most significant. uint8_t bytes[8]; uint64_t mask = 0xff00000000000000UL; for (int i = 7; i >= 0; i--) { bytes[i] = (value & mask) >> (i * 8); mask >>= 8; } // Permutation tables for REV instructions. // permute_table[Reverse16] is used by REV16_x, REV16_w // permute_table[Reverse32] is used by REV32_x, REV_w // permute_table[Reverse64] is used by REV_x DCHECK((Reverse16 == 0) && (Reverse32 == 1) && (Reverse64 == 2)); static const uint8_t permute_table[3][8] = { {6, 7, 4, 5, 2, 3, 0, 1}, {4, 5, 6, 7, 0, 1, 2, 3}, {0, 1, 2, 3, 4, 5, 6, 7} }; uint64_t result = 0; for (int i = 0; i < 8; i++) { result <<= 8; result |= bytes[permute_table[mode][i]]; } return result; } template void Simulator::DataProcessing2Source(Instruction* instr) { Shift shift_op = NO_SHIFT; T result = 0; switch (instr->Mask(DataProcessing2SourceMask)) { case SDIV_w: case SDIV_x: { T rn = reg(instr->Rn()); T rm = reg(instr->Rm()); if ((rn == std::numeric_limits::min()) && (rm == -1)) { result = std::numeric_limits::min(); } else if (rm == 0) { // Division by zero can be trapped, but not on A-class processors. result = 0; } else { result = rn / rm; } break; } case UDIV_w: case UDIV_x: { typedef typename make_unsigned::type unsignedT; unsignedT rn = static_cast(reg(instr->Rn())); unsignedT rm = static_cast(reg(instr->Rm())); if (rm == 0) { // Division by zero can be trapped, but not on A-class processors. result = 0; } else { result = rn / rm; } break; } case LSLV_w: case LSLV_x: shift_op = LSL; break; case LSRV_w: case LSRV_x: shift_op = LSR; break; case ASRV_w: case ASRV_x: shift_op = ASR; break; case RORV_w: case RORV_x: shift_op = ROR; break; default: UNIMPLEMENTED(); } if (shift_op != NO_SHIFT) { // Shift distance encoded in the least-significant five/six bits of the // register. unsigned shift = wreg(instr->Rm()); if (sizeof(T) == kWRegSize) { shift &= kShiftAmountWRegMask; } else { shift &= kShiftAmountXRegMask; } result = ShiftOperand(reg(instr->Rn()), shift_op, shift); } set_reg(instr->Rd(), result); } void Simulator::VisitDataProcessing2Source(Instruction* instr) { if (instr->SixtyFourBits()) { DataProcessing2Source(instr); } else { DataProcessing2Source(instr); } } // The algorithm used is described in section 8.2 of // Hacker's Delight, by Henry S. Warren, Jr. // It assumes that a right shift on a signed integer is an arithmetic shift. static int64_t MultiplyHighSigned(int64_t u, int64_t v) { uint64_t u0, v0, w0; int64_t u1, v1, w1, w2, t; u0 = u & 0xffffffffL; u1 = u >> 32; v0 = v & 0xffffffffL; v1 = v >> 32; w0 = u0 * v0; t = u1 * v0 + (w0 >> 32); w1 = t & 0xffffffffL; w2 = t >> 32; w1 = u0 * v1 + w1; return u1 * v1 + w2 + (w1 >> 32); } void Simulator::VisitDataProcessing3Source(Instruction* instr) { int64_t result = 0; // Extract and sign- or zero-extend 32-bit arguments for widening operations. uint64_t rn_u32 = reg(instr->Rn()); uint64_t rm_u32 = reg(instr->Rm()); int64_t rn_s32 = reg(instr->Rn()); int64_t rm_s32 = reg(instr->Rm()); switch (instr->Mask(DataProcessing3SourceMask)) { case MADD_w: case MADD_x: result = xreg(instr->Ra()) + (xreg(instr->Rn()) * xreg(instr->Rm())); break; case MSUB_w: case MSUB_x: result = xreg(instr->Ra()) - (xreg(instr->Rn()) * xreg(instr->Rm())); break; case SMADDL_x: result = xreg(instr->Ra()) + (rn_s32 * rm_s32); break; case SMSUBL_x: result = xreg(instr->Ra()) - (rn_s32 * rm_s32); break; case UMADDL_x: result = xreg(instr->Ra()) + (rn_u32 * rm_u32); break; case UMSUBL_x: result = xreg(instr->Ra()) - (rn_u32 * rm_u32); break; case SMULH_x: DCHECK(instr->Ra() == kZeroRegCode); result = MultiplyHighSigned(xreg(instr->Rn()), xreg(instr->Rm())); break; default: UNIMPLEMENTED(); } if (instr->SixtyFourBits()) { set_xreg(instr->Rd(), result); } else { set_wreg(instr->Rd(), result); } } template void Simulator::BitfieldHelper(Instruction* instr) { typedef typename make_unsigned::type unsignedT; T reg_size = sizeof(T) * 8; T R = instr->ImmR(); T S = instr->ImmS(); T diff = S - R; T mask; if (diff >= 0) { mask = diff < reg_size - 1 ? (static_cast(1) << (diff + 1)) - 1 : static_cast(-1); } else { mask = ((1L << (S + 1)) - 1); mask = (static_cast(mask) >> R) | (mask << (reg_size - R)); diff += reg_size; } // inzero indicates if the extracted bitfield is inserted into the // destination register value or in zero. // If extend is true, extend the sign of the extracted bitfield. bool inzero = false; bool extend = false; switch (instr->Mask(BitfieldMask)) { case BFM_x: case BFM_w: break; case SBFM_x: case SBFM_w: inzero = true; extend = true; break; case UBFM_x: case UBFM_w: inzero = true; break; default: UNIMPLEMENTED(); } T dst = inzero ? 0 : reg(instr->Rd()); T src = reg(instr->Rn()); // Rotate source bitfield into place. T result = (static_cast(src) >> R) | (src << (reg_size - R)); // Determine the sign extension. T topbits_preshift = (static_cast(1) << (reg_size - diff - 1)) - 1; T signbits = (extend && ((src >> S) & 1) ? topbits_preshift : 0) << (diff + 1); // Merge sign extension, dest/zero and bitfield. result = signbits | (result & mask) | (dst & ~mask); set_reg(instr->Rd(), result); } void Simulator::VisitBitfield(Instruction* instr) { if (instr->SixtyFourBits()) { BitfieldHelper(instr); } else { BitfieldHelper(instr); } } void Simulator::VisitExtract(Instruction* instr) { if (instr->SixtyFourBits()) { Extract(instr); } else { Extract(instr); } } void Simulator::VisitFPImmediate(Instruction* instr) { AssertSupportedFPCR(); unsigned dest = instr->Rd(); switch (instr->Mask(FPImmediateMask)) { case FMOV_s_imm: set_sreg(dest, instr->ImmFP32()); break; case FMOV_d_imm: set_dreg(dest, instr->ImmFP64()); break; default: UNREACHABLE(); } } void Simulator::VisitFPIntegerConvert(Instruction* instr) { AssertSupportedFPCR(); unsigned dst = instr->Rd(); unsigned src = instr->Rn(); FPRounding round = fpcr().RMode(); switch (instr->Mask(FPIntegerConvertMask)) { case FCVTAS_ws: set_wreg(dst, FPToInt32(sreg(src), FPTieAway)); break; case FCVTAS_xs: set_xreg(dst, FPToInt64(sreg(src), FPTieAway)); break; case FCVTAS_wd: set_wreg(dst, FPToInt32(dreg(src), FPTieAway)); break; case FCVTAS_xd: set_xreg(dst, FPToInt64(dreg(src), FPTieAway)); break; case FCVTAU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPTieAway)); break; case FCVTAU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPTieAway)); break; case FCVTAU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPTieAway)); break; case FCVTAU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPTieAway)); break; case FCVTMS_ws: set_wreg(dst, FPToInt32(sreg(src), FPNegativeInfinity)); break; case FCVTMS_xs: set_xreg(dst, FPToInt64(sreg(src), FPNegativeInfinity)); break; case FCVTMS_wd: set_wreg(dst, FPToInt32(dreg(src), FPNegativeInfinity)); break; case FCVTMS_xd: set_xreg(dst, FPToInt64(dreg(src), FPNegativeInfinity)); break; case FCVTMU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPNegativeInfinity)); break; case FCVTMU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPNegativeInfinity)); break; case FCVTMU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPNegativeInfinity)); break; case FCVTMU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPNegativeInfinity)); break; case FCVTNS_ws: set_wreg(dst, FPToInt32(sreg(src), FPTieEven)); break; case FCVTNS_xs: set_xreg(dst, FPToInt64(sreg(src), FPTieEven)); break; case FCVTNS_wd: set_wreg(dst, FPToInt32(dreg(src), FPTieEven)); break; case FCVTNS_xd: set_xreg(dst, FPToInt64(dreg(src), FPTieEven)); break; case FCVTNU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPTieEven)); break; case FCVTNU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPTieEven)); break; case FCVTNU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPTieEven)); break; case FCVTNU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPTieEven)); break; case FCVTZS_ws: set_wreg(dst, FPToInt32(sreg(src), FPZero)); break; case FCVTZS_xs: set_xreg(dst, FPToInt64(sreg(src), FPZero)); break; case FCVTZS_wd: set_wreg(dst, FPToInt32(dreg(src), FPZero)); break; case FCVTZS_xd: set_xreg(dst, FPToInt64(dreg(src), FPZero)); break; case FCVTZU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPZero)); break; case FCVTZU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPZero)); break; case FCVTZU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPZero)); break; case FCVTZU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPZero)); break; case FMOV_ws: set_wreg(dst, sreg_bits(src)); break; case FMOV_xd: set_xreg(dst, dreg_bits(src)); break; case FMOV_sw: set_sreg_bits(dst, wreg(src)); break; case FMOV_dx: set_dreg_bits(dst, xreg(src)); break; // A 32-bit input can be handled in the same way as a 64-bit input, since // the sign- or zero-extension will not affect the conversion. case SCVTF_dx: set_dreg(dst, FixedToDouble(xreg(src), 0, round)); break; case SCVTF_dw: set_dreg(dst, FixedToDouble(wreg(src), 0, round)); break; case UCVTF_dx: set_dreg(dst, UFixedToDouble(xreg(src), 0, round)); break; case UCVTF_dw: { set_dreg(dst, UFixedToDouble(reg(src), 0, round)); break; } case SCVTF_sx: set_sreg(dst, FixedToFloat(xreg(src), 0, round)); break; case SCVTF_sw: set_sreg(dst, FixedToFloat(wreg(src), 0, round)); break; case UCVTF_sx: set_sreg(dst, UFixedToFloat(xreg(src), 0, round)); break; case UCVTF_sw: { set_sreg(dst, UFixedToFloat(reg(src), 0, round)); break; } default: UNREACHABLE(); } } void Simulator::VisitFPFixedPointConvert(Instruction* instr) { AssertSupportedFPCR(); unsigned dst = instr->Rd(); unsigned src = instr->Rn(); int fbits = 64 - instr->FPScale(); FPRounding round = fpcr().RMode(); switch (instr->Mask(FPFixedPointConvertMask)) { // A 32-bit input can be handled in the same way as a 64-bit input, since // the sign- or zero-extension will not affect the conversion. case SCVTF_dx_fixed: set_dreg(dst, FixedToDouble(xreg(src), fbits, round)); break; case SCVTF_dw_fixed: set_dreg(dst, FixedToDouble(wreg(src), fbits, round)); break; case UCVTF_dx_fixed: set_dreg(dst, UFixedToDouble(xreg(src), fbits, round)); break; case UCVTF_dw_fixed: { set_dreg(dst, UFixedToDouble(reg(src), fbits, round)); break; } case SCVTF_sx_fixed: set_sreg(dst, FixedToFloat(xreg(src), fbits, round)); break; case SCVTF_sw_fixed: set_sreg(dst, FixedToFloat(wreg(src), fbits, round)); break; case UCVTF_sx_fixed: set_sreg(dst, UFixedToFloat(xreg(src), fbits, round)); break; case UCVTF_sw_fixed: { set_sreg(dst, UFixedToFloat(reg(src), fbits, round)); break; } default: UNREACHABLE(); } } int32_t Simulator::FPToInt32(double value, FPRounding rmode) { value = FPRoundInt(value, rmode); if (value >= kWMaxInt) { return kWMaxInt; } else if (value < kWMinInt) { return kWMinInt; } return std::isnan(value) ? 0 : static_cast(value); } int64_t Simulator::FPToInt64(double value, FPRounding rmode) { value = FPRoundInt(value, rmode); if (value >= kXMaxInt) { return kXMaxInt; } else if (value < kXMinInt) { return kXMinInt; } return std::isnan(value) ? 0 : static_cast(value); } uint32_t Simulator::FPToUInt32(double value, FPRounding rmode) { value = FPRoundInt(value, rmode); if (value >= kWMaxUInt) { return kWMaxUInt; } else if (value < 0.0) { return 0; } return std::isnan(value) ? 0 : static_cast(value); } uint64_t Simulator::FPToUInt64(double value, FPRounding rmode) { value = FPRoundInt(value, rmode); if (value >= kXMaxUInt) { return kXMaxUInt; } else if (value < 0.0) { return 0; } return std::isnan(value) ? 0 : static_cast(value); } void Simulator::VisitFPCompare(Instruction* instr) { AssertSupportedFPCR(); unsigned reg_size = (instr->Mask(FP64) == FP64) ? kDRegSizeInBits : kSRegSizeInBits; double fn_val = fpreg(reg_size, instr->Rn()); switch (instr->Mask(FPCompareMask)) { case FCMP_s: case FCMP_d: FPCompare(fn_val, fpreg(reg_size, instr->Rm())); break; case FCMP_s_zero: case FCMP_d_zero: FPCompare(fn_val, 0.0); break; default: UNIMPLEMENTED(); } } void Simulator::VisitFPConditionalCompare(Instruction* instr) { AssertSupportedFPCR(); switch (instr->Mask(FPConditionalCompareMask)) { case FCCMP_s: case FCCMP_d: { if (ConditionPassed(static_cast(instr->Condition()))) { // If the condition passes, set the status flags to the result of // comparing the operands. unsigned reg_size = (instr->Mask(FP64) == FP64) ? kDRegSizeInBits : kSRegSizeInBits; FPCompare(fpreg(reg_size, instr->Rn()), fpreg(reg_size, instr->Rm())); } else { // If the condition fails, set the status flags to the nzcv immediate. nzcv().SetFlags(instr->Nzcv()); LogSystemRegister(NZCV); } break; } default: UNIMPLEMENTED(); } } void Simulator::VisitFPConditionalSelect(Instruction* instr) { AssertSupportedFPCR(); Instr selected; if (ConditionPassed(static_cast(instr->Condition()))) { selected = instr->Rn(); } else { selected = instr->Rm(); } switch (instr->Mask(FPConditionalSelectMask)) { case FCSEL_s: set_sreg(instr->Rd(), sreg(selected)); break; case FCSEL_d: set_dreg(instr->Rd(), dreg(selected)); break; default: UNIMPLEMENTED(); } } void Simulator::VisitFPDataProcessing1Source(Instruction* instr) { AssertSupportedFPCR(); unsigned fd = instr->Rd(); unsigned fn = instr->Rn(); switch (instr->Mask(FPDataProcessing1SourceMask)) { case FMOV_s: set_sreg(fd, sreg(fn)); break; case FMOV_d: set_dreg(fd, dreg(fn)); break; case FABS_s: set_sreg(fd, std::fabs(sreg(fn))); break; case FABS_d: set_dreg(fd, std::fabs(dreg(fn))); break; case FNEG_s: set_sreg(fd, -sreg(fn)); break; case FNEG_d: set_dreg(fd, -dreg(fn)); break; case FSQRT_s: set_sreg(fd, FPSqrt(sreg(fn))); break; case FSQRT_d: set_dreg(fd, FPSqrt(dreg(fn))); break; case FRINTA_s: set_sreg(fd, FPRoundInt(sreg(fn), FPTieAway)); break; case FRINTA_d: set_dreg(fd, FPRoundInt(dreg(fn), FPTieAway)); break; case FRINTM_s: set_sreg(fd, FPRoundInt(sreg(fn), FPNegativeInfinity)); break; case FRINTM_d: set_dreg(fd, FPRoundInt(dreg(fn), FPNegativeInfinity)); break; case FRINTN_s: set_sreg(fd, FPRoundInt(sreg(fn), FPTieEven)); break; case FRINTN_d: set_dreg(fd, FPRoundInt(dreg(fn), FPTieEven)); break; case FRINTZ_s: set_sreg(fd, FPRoundInt(sreg(fn), FPZero)); break; case FRINTZ_d: set_dreg(fd, FPRoundInt(dreg(fn), FPZero)); break; case FCVT_ds: set_dreg(fd, FPToDouble(sreg(fn))); break; case FCVT_sd: set_sreg(fd, FPToFloat(dreg(fn), FPTieEven)); break; default: UNIMPLEMENTED(); } } // Assemble the specified IEEE-754 components into the target type and apply // appropriate rounding. // sign: 0 = positive, 1 = negative // exponent: Unbiased IEEE-754 exponent. // mantissa: The mantissa of the input. The top bit (which is not encoded for // normal IEEE-754 values) must not be omitted. This bit has the // value 'pow(2, exponent)'. // // The input value is assumed to be a normalized value. That is, the input may // not be infinity or NaN. If the source value is subnormal, it must be // normalized before calling this function such that the highest set bit in the // mantissa has the value 'pow(2, exponent)'. // // Callers should use FPRoundToFloat or FPRoundToDouble directly, rather than // calling a templated FPRound. template static T FPRound(int64_t sign, int64_t exponent, uint64_t mantissa, FPRounding round_mode) { DCHECK((sign == 0) || (sign == 1)); // Only the FPTieEven rounding mode is implemented. DCHECK(round_mode == FPTieEven); USE(round_mode); // Rounding can promote subnormals to normals, and normals to infinities. For // example, a double with exponent 127 (FLT_MAX_EXP) would appear to be // encodable as a float, but rounding based on the low-order mantissa bits // could make it overflow. With ties-to-even rounding, this value would become // an infinity. // ---- Rounding Method ---- // // The exponent is irrelevant in the rounding operation, so we treat the // lowest-order bit that will fit into the result ('onebit') as having // the value '1'. Similarly, the highest-order bit that won't fit into // the result ('halfbit') has the value '0.5'. The 'point' sits between // 'onebit' and 'halfbit': // // These bits fit into the result. // |---------------------| // mantissa = 0bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // || // / | // / halfbit // onebit // // For subnormal outputs, the range of representable bits is smaller and // the position of onebit and halfbit depends on the exponent of the // input, but the method is otherwise similar. // // onebit(frac) // | // | halfbit(frac) halfbit(adjusted) // | / / // | | | // 0b00.0 (exact) -> 0b00.0 (exact) -> 0b00 // 0b00.0... -> 0b00.0... -> 0b00 // 0b00.1 (exact) -> 0b00.0111..111 -> 0b00 // 0b00.1... -> 0b00.1... -> 0b01 // 0b01.0 (exact) -> 0b01.0 (exact) -> 0b01 // 0b01.0... -> 0b01.0... -> 0b01 // 0b01.1 (exact) -> 0b01.1 (exact) -> 0b10 // 0b01.1... -> 0b01.1... -> 0b10 // 0b10.0 (exact) -> 0b10.0 (exact) -> 0b10 // 0b10.0... -> 0b10.0... -> 0b10 // 0b10.1 (exact) -> 0b10.0111..111 -> 0b10 // 0b10.1... -> 0b10.1... -> 0b11 // 0b11.0 (exact) -> 0b11.0 (exact) -> 0b11 // ... / | / | // / | / | // / | // adjusted = frac - (halfbit(mantissa) & ~onebit(frac)); / | // // mantissa = (mantissa >> shift) + halfbit(adjusted); static const int mantissa_offset = 0; static const int exponent_offset = mantissa_offset + mbits; static const int sign_offset = exponent_offset + ebits; STATIC_ASSERT(sign_offset == (sizeof(T) * kByteSize - 1)); // Bail out early for zero inputs. if (mantissa == 0) { return sign << sign_offset; } // If all bits in the exponent are set, the value is infinite or NaN. // This is true for all binary IEEE-754 formats. static const int infinite_exponent = (1 << ebits) - 1; static const int max_normal_exponent = infinite_exponent - 1; // Apply the exponent bias to encode it for the result. Doing this early makes // it easy to detect values that will be infinite or subnormal. exponent += max_normal_exponent >> 1; if (exponent > max_normal_exponent) { // Overflow: The input is too large for the result type to represent. The // FPTieEven rounding mode handles overflows using infinities. exponent = infinite_exponent; mantissa = 0; return (sign << sign_offset) | (exponent << exponent_offset) | (mantissa << mantissa_offset); } // Calculate the shift required to move the top mantissa bit to the proper // place in the destination type. const int highest_significant_bit = 63 - CountLeadingZeros(mantissa, 64); int shift = highest_significant_bit - mbits; if (exponent <= 0) { // The output will be subnormal (before rounding). // For subnormal outputs, the shift must be adjusted by the exponent. The +1 // is necessary because the exponent of a subnormal value (encoded as 0) is // the same as the exponent of the smallest normal value (encoded as 1). shift += -exponent + 1; // Handle inputs that would produce a zero output. // // Shifts higher than highest_significant_bit+1 will always produce a zero // result. A shift of exactly highest_significant_bit+1 might produce a // non-zero result after rounding. if (shift > (highest_significant_bit + 1)) { // The result will always be +/-0.0. return sign << sign_offset; } // Properly encode the exponent for a subnormal output. exponent = 0; } else { // Clear the topmost mantissa bit, since this is not encoded in IEEE-754 // normal values. mantissa &= ~(1UL << highest_significant_bit); } if (shift > 0) { // We have to shift the mantissa to the right. Some precision is lost, so we // need to apply rounding. uint64_t onebit_mantissa = (mantissa >> (shift)) & 1; uint64_t halfbit_mantissa = (mantissa >> (shift-1)) & 1; uint64_t adjusted = mantissa - (halfbit_mantissa & ~onebit_mantissa); T halfbit_adjusted = (adjusted >> (shift-1)) & 1; T result = (sign << sign_offset) | (exponent << exponent_offset) | ((mantissa >> shift) << mantissa_offset); // A very large mantissa can overflow during rounding. If this happens, the // exponent should be incremented and the mantissa set to 1.0 (encoded as // 0). Applying halfbit_adjusted after assembling the float has the nice // side-effect that this case is handled for free. // // This also handles cases where a very large finite value overflows to // infinity, or where a very large subnormal value overflows to become // normal. return result + halfbit_adjusted; } else { // We have to shift the mantissa to the left (or not at all). The input // mantissa is exactly representable in the output mantissa, so apply no // rounding correction. return (sign << sign_offset) | (exponent << exponent_offset) | ((mantissa << -shift) << mantissa_offset); } } // See FPRound for a description of this function. static inline double FPRoundToDouble(int64_t sign, int64_t exponent, uint64_t mantissa, FPRounding round_mode) { int64_t bits = FPRound(sign, exponent, mantissa, round_mode); return rawbits_to_double(bits); } // See FPRound for a description of this function. static inline float FPRoundToFloat(int64_t sign, int64_t exponent, uint64_t mantissa, FPRounding round_mode) { int32_t bits = FPRound(sign, exponent, mantissa, round_mode); return rawbits_to_float(bits); } double Simulator::FixedToDouble(int64_t src, int fbits, FPRounding round) { if (src >= 0) { return UFixedToDouble(src, fbits, round); } else { // This works for all negative values, including INT64_MIN. return -UFixedToDouble(-src, fbits, round); } } double Simulator::UFixedToDouble(uint64_t src, int fbits, FPRounding round) { // An input of 0 is a special case because the result is effectively // subnormal: The exponent is encoded as 0 and there is no implicit 1 bit. if (src == 0) { return 0.0; } // Calculate the exponent. The highest significant bit will have the value // 2^exponent. const int highest_significant_bit = 63 - CountLeadingZeros(src, 64); const int64_t exponent = highest_significant_bit - fbits; return FPRoundToDouble(0, exponent, src, round); } float Simulator::FixedToFloat(int64_t src, int fbits, FPRounding round) { if (src >= 0) { return UFixedToFloat(src, fbits, round); } else { // This works for all negative values, including INT64_MIN. return -UFixedToFloat(-src, fbits, round); } } float Simulator::UFixedToFloat(uint64_t src, int fbits, FPRounding round) { // An input of 0 is a special case because the result is effectively // subnormal: The exponent is encoded as 0 and there is no implicit 1 bit. if (src == 0) { return 0.0f; } // Calculate the exponent. The highest significant bit will have the value // 2^exponent. const int highest_significant_bit = 63 - CountLeadingZeros(src, 64); const int32_t exponent = highest_significant_bit - fbits; return FPRoundToFloat(0, exponent, src, round); } double Simulator::FPRoundInt(double value, FPRounding round_mode) { if ((value == 0.0) || (value == kFP64PositiveInfinity) || (value == kFP64NegativeInfinity)) { return value; } else if (std::isnan(value)) { return FPProcessNaN(value); } double int_result = floor(value); double error = value - int_result; switch (round_mode) { case FPTieAway: { // Take care of correctly handling the range ]-0.5, -0.0], which must // yield -0.0. if ((-0.5 < value) && (value < 0.0)) { int_result = -0.0; } else if ((error > 0.5) || ((error == 0.5) && (int_result >= 0.0))) { // If the error is greater than 0.5, or is equal to 0.5 and the integer // result is positive, round up. int_result++; } break; } case FPTieEven: { // Take care of correctly handling the range [-0.5, -0.0], which must // yield -0.0. if ((-0.5 <= value) && (value < 0.0)) { int_result = -0.0; // If the error is greater than 0.5, or is equal to 0.5 and the integer // result is odd, round up. } else if ((error > 0.5) || ((error == 0.5) && (fmod(int_result, 2) != 0))) { int_result++; } break; } case FPZero: { // If value > 0 then we take floor(value) // otherwise, ceil(value) if (value < 0) { int_result = ceil(value); } break; } case FPNegativeInfinity: { // We always use floor(value). break; } default: UNIMPLEMENTED(); } return int_result; } double Simulator::FPToDouble(float value) { switch (std::fpclassify(value)) { case FP_NAN: { if (fpcr().DN()) return kFP64DefaultNaN; // Convert NaNs as the processor would: // - The sign is propagated. // - The payload (mantissa) is transferred entirely, except that the top // bit is forced to '1', making the result a quiet NaN. The unused // (low-order) payload bits are set to 0. uint32_t raw = float_to_rawbits(value); uint64_t sign = raw >> 31; uint64_t exponent = (1 << 11) - 1; uint64_t payload = unsigned_bitextract_64(21, 0, raw); payload <<= (52 - 23); // The unused low-order bits should be 0. payload |= (1L << 51); // Force a quiet NaN. return rawbits_to_double((sign << 63) | (exponent << 52) | payload); } case FP_ZERO: case FP_NORMAL: case FP_SUBNORMAL: case FP_INFINITE: { // All other inputs are preserved in a standard cast, because every value // representable using an IEEE-754 float is also representable using an // IEEE-754 double. return static_cast(value); } } UNREACHABLE(); return static_cast(value); } float Simulator::FPToFloat(double value, FPRounding round_mode) { // Only the FPTieEven rounding mode is implemented. DCHECK(round_mode == FPTieEven); USE(round_mode); switch (std::fpclassify(value)) { case FP_NAN: { if (fpcr().DN()) return kFP32DefaultNaN; // Convert NaNs as the processor would: // - The sign is propagated. // - The payload (mantissa) is transferred as much as possible, except // that the top bit is forced to '1', making the result a quiet NaN. uint64_t raw = double_to_rawbits(value); uint32_t sign = raw >> 63; uint32_t exponent = (1 << 8) - 1; uint32_t payload = unsigned_bitextract_64(50, 52 - 23, raw); payload |= (1 << 22); // Force a quiet NaN. return rawbits_to_float((sign << 31) | (exponent << 23) | payload); } case FP_ZERO: case FP_INFINITE: { // In a C++ cast, any value representable in the target type will be // unchanged. This is always the case for +/-0.0 and infinities. return static_cast(value); } case FP_NORMAL: case FP_SUBNORMAL: { // Convert double-to-float as the processor would, assuming that FPCR.FZ // (flush-to-zero) is not set. uint64_t raw = double_to_rawbits(value); // Extract the IEEE-754 double components. uint32_t sign = raw >> 63; // Extract the exponent and remove the IEEE-754 encoding bias. int32_t exponent = unsigned_bitextract_64(62, 52, raw) - 1023; // Extract the mantissa and add the implicit '1' bit. uint64_t mantissa = unsigned_bitextract_64(51, 0, raw); if (std::fpclassify(value) == FP_NORMAL) { mantissa |= (1UL << 52); } return FPRoundToFloat(sign, exponent, mantissa, round_mode); } } UNREACHABLE(); return value; } void Simulator::VisitFPDataProcessing2Source(Instruction* instr) { AssertSupportedFPCR(); unsigned fd = instr->Rd(); unsigned fn = instr->Rn(); unsigned fm = instr->Rm(); // Fmaxnm and Fminnm have special NaN handling. switch (instr->Mask(FPDataProcessing2SourceMask)) { case FMAXNM_s: set_sreg(fd, FPMaxNM(sreg(fn), sreg(fm))); return; case FMAXNM_d: set_dreg(fd, FPMaxNM(dreg(fn), dreg(fm))); return; case FMINNM_s: set_sreg(fd, FPMinNM(sreg(fn), sreg(fm))); return; case FMINNM_d: set_dreg(fd, FPMinNM(dreg(fn), dreg(fm))); return; default: break; // Fall through. } if (FPProcessNaNs(instr)) return; switch (instr->Mask(FPDataProcessing2SourceMask)) { case FADD_s: set_sreg(fd, FPAdd(sreg(fn), sreg(fm))); break; case FADD_d: set_dreg(fd, FPAdd(dreg(fn), dreg(fm))); break; case FSUB_s: set_sreg(fd, FPSub(sreg(fn), sreg(fm))); break; case FSUB_d: set_dreg(fd, FPSub(dreg(fn), dreg(fm))); break; case FMUL_s: set_sreg(fd, FPMul(sreg(fn), sreg(fm))); break; case FMUL_d: set_dreg(fd, FPMul(dreg(fn), dreg(fm))); break; case FDIV_s: set_sreg(fd, FPDiv(sreg(fn), sreg(fm))); break; case FDIV_d: set_dreg(fd, FPDiv(dreg(fn), dreg(fm))); break; case FMAX_s: set_sreg(fd, FPMax(sreg(fn), sreg(fm))); break; case FMAX_d: set_dreg(fd, FPMax(dreg(fn), dreg(fm))); break; case FMIN_s: set_sreg(fd, FPMin(sreg(fn), sreg(fm))); break; case FMIN_d: set_dreg(fd, FPMin(dreg(fn), dreg(fm))); break; case FMAXNM_s: case FMAXNM_d: case FMINNM_s: case FMINNM_d: // These were handled before the standard FPProcessNaNs() stage. UNREACHABLE(); default: UNIMPLEMENTED(); } } void Simulator::VisitFPDataProcessing3Source(Instruction* instr) { AssertSupportedFPCR(); unsigned fd = instr->Rd(); unsigned fn = instr->Rn(); unsigned fm = instr->Rm(); unsigned fa = instr->Ra(); switch (instr->Mask(FPDataProcessing3SourceMask)) { // fd = fa +/- (fn * fm) case FMADD_s: set_sreg(fd, FPMulAdd(sreg(fa), sreg(fn), sreg(fm))); break; case FMSUB_s: set_sreg(fd, FPMulAdd(sreg(fa), -sreg(fn), sreg(fm))); break; case FMADD_d: set_dreg(fd, FPMulAdd(dreg(fa), dreg(fn), dreg(fm))); break; case FMSUB_d: set_dreg(fd, FPMulAdd(dreg(fa), -dreg(fn), dreg(fm))); break; // Negated variants of the above. case FNMADD_s: set_sreg(fd, FPMulAdd(-sreg(fa), -sreg(fn), sreg(fm))); break; case FNMSUB_s: set_sreg(fd, FPMulAdd(-sreg(fa), sreg(fn), sreg(fm))); break; case FNMADD_d: set_dreg(fd, FPMulAdd(-dreg(fa), -dreg(fn), dreg(fm))); break; case FNMSUB_d: set_dreg(fd, FPMulAdd(-dreg(fa), dreg(fn), dreg(fm))); break; default: UNIMPLEMENTED(); } } template T Simulator::FPAdd(T op1, T op2) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(op1) && !std::isnan(op2)); if (std::isinf(op1) && std::isinf(op2) && (op1 != op2)) { // inf + -inf returns the default NaN. return FPDefaultNaN(); } else { // Other cases should be handled by standard arithmetic. return op1 + op2; } } template T Simulator::FPDiv(T op1, T op2) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(op1) && !std::isnan(op2)); if ((std::isinf(op1) && std::isinf(op2)) || ((op1 == 0.0) && (op2 == 0.0))) { // inf / inf and 0.0 / 0.0 return the default NaN. return FPDefaultNaN(); } else { // Other cases should be handled by standard arithmetic. return op1 / op2; } } template T Simulator::FPMax(T a, T b) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(a) && !std::isnan(b)); if ((a == 0.0) && (b == 0.0) && (copysign(1.0, a) != copysign(1.0, b))) { // a and b are zero, and the sign differs: return +0.0. return 0.0; } else { return (a > b) ? a : b; } } template T Simulator::FPMaxNM(T a, T b) { if (IsQuietNaN(a) && !IsQuietNaN(b)) { a = kFP64NegativeInfinity; } else if (!IsQuietNaN(a) && IsQuietNaN(b)) { b = kFP64NegativeInfinity; } T result = FPProcessNaNs(a, b); return std::isnan(result) ? result : FPMax(a, b); } template T Simulator::FPMin(T a, T b) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(a) && !std::isnan(b)); if ((a == 0.0) && (b == 0.0) && (copysign(1.0, a) != copysign(1.0, b))) { // a and b are zero, and the sign differs: return -0.0. return -0.0; } else { return (a < b) ? a : b; } } template T Simulator::FPMinNM(T a, T b) { if (IsQuietNaN(a) && !IsQuietNaN(b)) { a = kFP64PositiveInfinity; } else if (!IsQuietNaN(a) && IsQuietNaN(b)) { b = kFP64PositiveInfinity; } T result = FPProcessNaNs(a, b); return std::isnan(result) ? result : FPMin(a, b); } template T Simulator::FPMul(T op1, T op2) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(op1) && !std::isnan(op2)); if ((std::isinf(op1) && (op2 == 0.0)) || (std::isinf(op2) && (op1 == 0.0))) { // inf * 0.0 returns the default NaN. return FPDefaultNaN(); } else { // Other cases should be handled by standard arithmetic. return op1 * op2; } } template T Simulator::FPMulAdd(T a, T op1, T op2) { T result = FPProcessNaNs3(a, op1, op2); T sign_a = copysign(1.0, a); T sign_prod = copysign(1.0, op1) * copysign(1.0, op2); bool isinf_prod = std::isinf(op1) || std::isinf(op2); bool operation_generates_nan = (std::isinf(op1) && (op2 == 0.0)) || // inf * 0.0 (std::isinf(op2) && (op1 == 0.0)) || // 0.0 * inf (std::isinf(a) && isinf_prod && (sign_a != sign_prod)); // inf - inf if (std::isnan(result)) { // Generated NaNs override quiet NaNs propagated from a. if (operation_generates_nan && IsQuietNaN(a)) { return FPDefaultNaN(); } else { return result; } } // If the operation would produce a NaN, return the default NaN. if (operation_generates_nan) { return FPDefaultNaN(); } // Work around broken fma implementations for exact zero results: The sign of // exact 0.0 results is positive unless both a and op1 * op2 are negative. if (((op1 == 0.0) || (op2 == 0.0)) && (a == 0.0)) { return ((sign_a < 0) && (sign_prod < 0)) ? -0.0 : 0.0; } result = FusedMultiplyAdd(op1, op2, a); DCHECK(!std::isnan(result)); // Work around broken fma implementations for rounded zero results: If a is // 0.0, the sign of the result is the sign of op1 * op2 before rounding. if ((a == 0.0) && (result == 0.0)) { return copysign(0.0, sign_prod); } return result; } template T Simulator::FPSqrt(T op) { if (std::isnan(op)) { return FPProcessNaN(op); } else if (op < 0.0) { return FPDefaultNaN(); } else { return std::sqrt(op); } } template T Simulator::FPSub(T op1, T op2) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(op1) && !std::isnan(op2)); if (std::isinf(op1) && std::isinf(op2) && (op1 == op2)) { // inf - inf returns the default NaN. return FPDefaultNaN(); } else { // Other cases should be handled by standard arithmetic. return op1 - op2; } } template T Simulator::FPProcessNaN(T op) { DCHECK(std::isnan(op)); return fpcr().DN() ? FPDefaultNaN() : ToQuietNaN(op); } template T Simulator::FPProcessNaNs(T op1, T op2) { if (IsSignallingNaN(op1)) { return FPProcessNaN(op1); } else if (IsSignallingNaN(op2)) { return FPProcessNaN(op2); } else if (std::isnan(op1)) { DCHECK(IsQuietNaN(op1)); return FPProcessNaN(op1); } else if (std::isnan(op2)) { DCHECK(IsQuietNaN(op2)); return FPProcessNaN(op2); } else { return 0.0; } } template T Simulator::FPProcessNaNs3(T op1, T op2, T op3) { if (IsSignallingNaN(op1)) { return FPProcessNaN(op1); } else if (IsSignallingNaN(op2)) { return FPProcessNaN(op2); } else if (IsSignallingNaN(op3)) { return FPProcessNaN(op3); } else if (std::isnan(op1)) { DCHECK(IsQuietNaN(op1)); return FPProcessNaN(op1); } else if (std::isnan(op2)) { DCHECK(IsQuietNaN(op2)); return FPProcessNaN(op2); } else if (std::isnan(op3)) { DCHECK(IsQuietNaN(op3)); return FPProcessNaN(op3); } else { return 0.0; } } bool Simulator::FPProcessNaNs(Instruction* instr) { unsigned fd = instr->Rd(); unsigned fn = instr->Rn(); unsigned fm = instr->Rm(); bool done = false; if (instr->Mask(FP64) == FP64) { double result = FPProcessNaNs(dreg(fn), dreg(fm)); if (std::isnan(result)) { set_dreg(fd, result); done = true; } } else { float result = FPProcessNaNs(sreg(fn), sreg(fm)); if (std::isnan(result)) { set_sreg(fd, result); done = true; } } return done; } void Simulator::VisitSystem(Instruction* instr) { // Some system instructions hijack their Op and Cp fields to represent a // range of immediates instead of indicating a different instruction. This // makes the decoding tricky. if (instr->Mask(SystemSysRegFMask) == SystemSysRegFixed) { switch (instr->Mask(SystemSysRegMask)) { case MRS: { switch (instr->ImmSystemRegister()) { case NZCV: set_xreg(instr->Rt(), nzcv().RawValue()); break; case FPCR: set_xreg(instr->Rt(), fpcr().RawValue()); break; default: UNIMPLEMENTED(); } break; } case MSR: { switch (instr->ImmSystemRegister()) { case NZCV: nzcv().SetRawValue(xreg(instr->Rt())); LogSystemRegister(NZCV); break; case FPCR: fpcr().SetRawValue(xreg(instr->Rt())); LogSystemRegister(FPCR); break; default: UNIMPLEMENTED(); } break; } } } else if (instr->Mask(SystemHintFMask) == SystemHintFixed) { DCHECK(instr->Mask(SystemHintMask) == HINT); switch (instr->ImmHint()) { case NOP: break; default: UNIMPLEMENTED(); } } else if (instr->Mask(MemBarrierFMask) == MemBarrierFixed) { __sync_synchronize(); } else { UNIMPLEMENTED(); } } bool Simulator::GetValue(const char* desc, int64_t* value) { int regnum = CodeFromName(desc); if (regnum >= 0) { unsigned code = regnum; if (code == kZeroRegCode) { // Catch the zero register and return 0. *value = 0; return true; } else if (code == kSPRegInternalCode) { // Translate the stack pointer code to 31, for Reg31IsStackPointer. code = 31; } if (desc[0] == 'w') { *value = wreg(code, Reg31IsStackPointer); } else { *value = xreg(code, Reg31IsStackPointer); } return true; } else if (strncmp(desc, "0x", 2) == 0) { return SScanF(desc + 2, "%" SCNx64, reinterpret_cast(value)) == 1; } else { return SScanF(desc, "%" SCNu64, reinterpret_cast(value)) == 1; } } bool Simulator::PrintValue(const char* desc) { if (strcmp(desc, "csp") == 0) { DCHECK(CodeFromName(desc) == static_cast(kSPRegInternalCode)); PrintF(stream_, "%s csp:%s 0x%016" PRIx64 "%s\n", clr_reg_name, clr_reg_value, xreg(31, Reg31IsStackPointer), clr_normal); return true; } else if (strcmp(desc, "wcsp") == 0) { DCHECK(CodeFromName(desc) == static_cast(kSPRegInternalCode)); PrintF(stream_, "%s wcsp:%s 0x%08" PRIx32 "%s\n", clr_reg_name, clr_reg_value, wreg(31, Reg31IsStackPointer), clr_normal); return true; } int i = CodeFromName(desc); STATIC_ASSERT(kNumberOfRegisters == kNumberOfFPRegisters); if (i < 0 || static_cast(i) >= kNumberOfFPRegisters) return false; if (desc[0] == 'v') { PrintF(stream_, "%s %s:%s 0x%016" PRIx64 "%s (%s%s:%s %g%s %s:%s %g%s)\n", clr_fpreg_name, VRegNameForCode(i), clr_fpreg_value, double_to_rawbits(dreg(i)), clr_normal, clr_fpreg_name, DRegNameForCode(i), clr_fpreg_value, dreg(i), clr_fpreg_name, SRegNameForCode(i), clr_fpreg_value, sreg(i), clr_normal); return true; } else if (desc[0] == 'd') { PrintF(stream_, "%s %s:%s %g%s\n", clr_fpreg_name, DRegNameForCode(i), clr_fpreg_value, dreg(i), clr_normal); return true; } else if (desc[0] == 's') { PrintF(stream_, "%s %s:%s %g%s\n", clr_fpreg_name, SRegNameForCode(i), clr_fpreg_value, sreg(i), clr_normal); return true; } else if (desc[0] == 'w') { PrintF(stream_, "%s %s:%s 0x%08" PRIx32 "%s\n", clr_reg_name, WRegNameForCode(i), clr_reg_value, wreg(i), clr_normal); return true; } else { // X register names have a wide variety of starting characters, but anything // else will be an X register. PrintF(stream_, "%s %s:%s 0x%016" PRIx64 "%s\n", clr_reg_name, XRegNameForCode(i), clr_reg_value, xreg(i), clr_normal); return true; } } void Simulator::Debug() { #define COMMAND_SIZE 63 #define ARG_SIZE 255 #define STR(a) #a #define XSTR(a) STR(a) char cmd[COMMAND_SIZE + 1]; char arg1[ARG_SIZE + 1]; char arg2[ARG_SIZE + 1]; char* argv[3] = { cmd, arg1, arg2 }; // Make sure to have a proper terminating character if reaching the limit. cmd[COMMAND_SIZE] = 0; arg1[ARG_SIZE] = 0; arg2[ARG_SIZE] = 0; bool done = false; bool cleared_log_disasm_bit = false; while (!done) { // Disassemble the next instruction to execute before doing anything else. PrintInstructionsAt(pc_, 1); // Read the command line. char* line = ReadLine("sim> "); if (line == NULL) { break; } else { // Repeat last command by default. char* last_input = last_debugger_input(); if (strcmp(line, "\n") == 0 && (last_input != NULL)) { DeleteArray(line); line = last_input; } else { // Update the latest command ran set_last_debugger_input(line); } // Use sscanf to parse the individual parts of the command line. At the // moment no command expects more than two parameters. int argc = SScanF(line, "%" XSTR(COMMAND_SIZE) "s " "%" XSTR(ARG_SIZE) "s " "%" XSTR(ARG_SIZE) "s", cmd, arg1, arg2); // stepi / si ------------------------------------------------------------ if ((strcmp(cmd, "si") == 0) || (strcmp(cmd, "stepi") == 0)) { // We are about to execute instructions, after which by default we // should increment the pc_. If it was set when reaching this debug // instruction, it has not been cleared because this instruction has not // completed yet. So clear it manually. pc_modified_ = false; if (argc == 1) { ExecuteInstruction(); } else { int64_t number_of_instructions_to_execute = 1; GetValue(arg1, &number_of_instructions_to_execute); set_log_parameters(log_parameters() | LOG_DISASM); while (number_of_instructions_to_execute-- > 0) { ExecuteInstruction(); } set_log_parameters(log_parameters() & ~LOG_DISASM); PrintF("\n"); } // If it was necessary, the pc has already been updated or incremented // when executing the instruction. So we do not want it to be updated // again. It will be cleared when exiting. pc_modified_ = true; // next / n -------------------------------------------------------------- } else if ((strcmp(cmd, "next") == 0) || (strcmp(cmd, "n") == 0)) { // Tell the simulator to break after the next executed BL. break_on_next_ = true; // Continue. done = true; // continue / cont / c --------------------------------------------------- } else if ((strcmp(cmd, "continue") == 0) || (strcmp(cmd, "cont") == 0) || (strcmp(cmd, "c") == 0)) { // Leave the debugger shell. done = true; // disassemble / disasm / di --------------------------------------------- } else if (strcmp(cmd, "disassemble") == 0 || strcmp(cmd, "disasm") == 0 || strcmp(cmd, "di") == 0) { int64_t n_of_instrs_to_disasm = 10; // default value. int64_t address = reinterpret_cast(pc_); // default value. if (argc >= 2) { // disasm GetValue(arg1, &n_of_instrs_to_disasm); } if (argc >= 3) { // disasm GetValue(arg2, &address); } // Disassemble. PrintInstructionsAt(reinterpret_cast(address), n_of_instrs_to_disasm); PrintF("\n"); // print / p ------------------------------------------------------------- } else if ((strcmp(cmd, "print") == 0) || (strcmp(cmd, "p") == 0)) { if (argc == 2) { if (strcmp(arg1, "all") == 0) { PrintRegisters(); PrintFPRegisters(); } else { if (!PrintValue(arg1)) { PrintF("%s unrecognized\n", arg1); } } } else { PrintF( "print \n" " Print the content of a register. (alias 'p')\n" " 'print all' will print all registers.\n" " Use 'printobject' to get more details about the value.\n"); } // printobject / po ------------------------------------------------------ } else if ((strcmp(cmd, "printobject") == 0) || (strcmp(cmd, "po") == 0)) { if (argc == 2) { int64_t value; OFStream os(stdout); if (GetValue(arg1, &value)) { Object* obj = reinterpret_cast(value); os << arg1 << ": \n"; #ifdef DEBUG obj->Print(os); os << "\n"; #else os << Brief(obj) << "\n"; #endif } else { os << arg1 << " unrecognized\n"; } } else { PrintF("printobject \n" "printobject \n" " Print details about the value. (alias 'po')\n"); } // stack / mem ---------------------------------------------------------- } else if (strcmp(cmd, "stack") == 0 || strcmp(cmd, "mem") == 0) { int64_t* cur = NULL; int64_t* end = NULL; int next_arg = 1; if (strcmp(cmd, "stack") == 0) { cur = reinterpret_cast(jssp()); } else { // "mem" int64_t value; if (!GetValue(arg1, &value)) { PrintF("%s unrecognized\n", arg1); continue; } cur = reinterpret_cast(value); next_arg++; } int64_t words = 0; if (argc == next_arg) { words = 10; } else if (argc == next_arg + 1) { if (!GetValue(argv[next_arg], &words)) { PrintF("%s unrecognized\n", argv[next_arg]); PrintF("Printing 10 double words by default"); words = 10; } } else { UNREACHABLE(); } end = cur + words; while (cur < end) { PrintF(" 0x%016" PRIx64 ": 0x%016" PRIx64 " %10" PRId64, reinterpret_cast(cur), *cur, *cur); HeapObject* obj = reinterpret_cast(*cur); int64_t value = *cur; Heap* current_heap = v8::internal::Isolate::Current()->heap(); if (((value & 1) == 0) || current_heap->Contains(obj)) { PrintF(" ("); if ((value & kSmiTagMask) == 0) { STATIC_ASSERT(kSmiValueSize == 32); int32_t untagged = (value >> kSmiShift) & 0xffffffff; PrintF("smi %" PRId32, untagged); } else { obj->ShortPrint(); } PrintF(")"); } PrintF("\n"); cur++; } // trace / t ------------------------------------------------------------- } else if (strcmp(cmd, "trace") == 0 || strcmp(cmd, "t") == 0) { if ((log_parameters() & (LOG_DISASM | LOG_REGS)) != (LOG_DISASM | LOG_REGS)) { PrintF("Enabling disassembly and registers tracing\n"); set_log_parameters(log_parameters() | LOG_DISASM | LOG_REGS); } else { PrintF("Disabling disassembly and registers tracing\n"); set_log_parameters(log_parameters() & ~(LOG_DISASM | LOG_REGS)); } // break / b ------------------------------------------------------------- } else if (strcmp(cmd, "break") == 0 || strcmp(cmd, "b") == 0) { if (argc == 2) { int64_t value; if (GetValue(arg1, &value)) { SetBreakpoint(reinterpret_cast(value)); } else { PrintF("%s unrecognized\n", arg1); } } else { ListBreakpoints(); PrintF("Use `break ` to set or disable a breakpoint\n"); } // gdb ------------------------------------------------------------------- } else if (strcmp(cmd, "gdb") == 0) { PrintF("Relinquishing control to gdb.\n"); base::OS::DebugBreak(); PrintF("Regaining control from gdb.\n"); // sysregs --------------------------------------------------------------- } else if (strcmp(cmd, "sysregs") == 0) { PrintSystemRegisters(); // help / h -------------------------------------------------------------- } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "h") == 0) { PrintF( "stepi / si\n" " stepi \n" " Step instructions.\n" "next / n\n" " Continue execution until a BL instruction is reached.\n" " At this point a breakpoint is set just after this BL.\n" " Then execution is resumed. It will probably later hit the\n" " breakpoint just set.\n" "continue / cont / c\n" " Continue execution from here.\n" "disassemble / disasm / di\n" " disassemble \n" " Disassemble
:value, ...)". DCHECK(sizes != 0); DCHECK((sizes & kPrintAllFPRegValues) == sizes); // Print the raw bits. fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s (", clr_fpreg_name, VRegNameForCode(code), clr_fpreg_value, fpreg(code), clr_normal); // Print all requested value interpretations. bool need_separator = false; if (sizes & kPrintDRegValue) { fprintf(stream_, "%s%s%s: %s%g%s", need_separator ? ", " : "", clr_fpreg_name, DRegNameForCode(code), clr_fpreg_value, fpreg(code), clr_normal); need_separator = true; } if (sizes & kPrintSRegValue) { fprintf(stream_, "%s%s%s: %s%g%s", need_separator ? ", " : "", clr_fpreg_name, SRegNameForCode(code), clr_fpreg_value, fpreg(code), clr_normal); need_separator = true; } // End the value list. fprintf(stream_, ")\n"); } void Simulator::PrintSystemRegister(SystemRegister id) { switch (id) { case NZCV: fprintf(stream_, "# %sNZCV: %sN:%d Z:%d C:%d V:%d%s\n", clr_flag_name, clr_flag_value, nzcv().N(), nzcv().Z(), nzcv().C(), nzcv().V(), clr_normal); break; case FPCR: { static const char * rmode[] = { "0b00 (Round to Nearest)", "0b01 (Round towards Plus Infinity)", "0b10 (Round towards Minus Infinity)", "0b11 (Round towards Zero)" }; DCHECK(fpcr().RMode() < arraysize(rmode)); fprintf(stream_, "# %sFPCR: %sAHP:%d DN:%d FZ:%d RMode:%s%s\n", clr_flag_name, clr_flag_value, fpcr().AHP(), fpcr().DN(), fpcr().FZ(), rmode[fpcr().RMode()], clr_normal); break; } default: UNREACHABLE(); } } void Simulator::PrintRead(uintptr_t address, size_t size, unsigned reg_code) { USE(size); // Size is unused here. // The template is "# x:value <- address". fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s", clr_reg_name, XRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); fprintf(stream_, " <- %s0x%016" PRIxPTR "%s\n", clr_memory_address, address, clr_normal); } void Simulator::PrintReadFP(uintptr_t address, size_t size, unsigned reg_code) { // The template is "# reg:bits (reg:value) <- address". switch (size) { case kSRegSize: fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s (%s%s: %s%gf%s)", clr_fpreg_name, VRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal, clr_fpreg_name, SRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal); break; case kDRegSize: fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s (%s%s: %s%g%s)", clr_fpreg_name, VRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal, clr_fpreg_name, DRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal); break; default: UNREACHABLE(); } fprintf(stream_, " <- %s0x%016" PRIxPTR "%s\n", clr_memory_address, address, clr_normal); } void Simulator::PrintWrite(uintptr_t address, size_t size, unsigned reg_code) { // The template is "# reg:value -> address". To keep the trace tidy and // readable, the value is aligned with the values in the register trace. switch (size) { case kByteSizeInBytes: fprintf(stream_, "# %s%5s<7:0>: %s0x%02" PRIx8 "%s", clr_reg_name, WRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); break; case kHalfWordSizeInBytes: fprintf(stream_, "# %s%5s<15:0>: %s0x%04" PRIx16 "%s", clr_reg_name, WRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); break; case kWRegSize: fprintf(stream_, "# %s%5s: %s0x%08" PRIx32 "%s", clr_reg_name, WRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); break; case kXRegSize: fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s", clr_reg_name, XRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); break; default: UNREACHABLE(); } fprintf(stream_, " -> %s0x%016" PRIxPTR "%s\n", clr_memory_address, address, clr_normal); } void Simulator::PrintWriteFP(uintptr_t address, size_t size, unsigned reg_code) { // The template is "# reg:bits (reg:value) -> address". To keep the trace tidy // and readable, the value is aligned with the values in the register trace. switch (size) { case kSRegSize: fprintf(stream_, "# %s%5s<31:0>: %s0x%08" PRIx32 "%s (%s%s: %s%gf%s)", clr_fpreg_name, VRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal, clr_fpreg_name, SRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal); break; case kDRegSize: fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s (%s%s: %s%g%s)", clr_fpreg_name, VRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal, clr_fpreg_name, DRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal); break; default: UNREACHABLE(); } fprintf(stream_, " -> %s0x%016" PRIxPTR "%s\n", clr_memory_address, address, clr_normal); } // Visitors--------------------------------------------------------------------- void Simulator::VisitUnimplemented(Instruction* instr) { fprintf(stream_, "Unimplemented instruction at %p: 0x%08" PRIx32 "\n", reinterpret_cast(instr), instr->InstructionBits()); UNIMPLEMENTED(); } void Simulator::VisitUnallocated(Instruction* instr) { fprintf(stream_, "Unallocated instruction at %p: 0x%08" PRIx32 "\n", reinterpret_cast(instr), instr->InstructionBits()); UNIMPLEMENTED(); } void Simulator::VisitPCRelAddressing(Instruction* instr) { switch (instr->Mask(PCRelAddressingMask)) { case ADR: set_reg(instr->Rd(), instr->ImmPCOffsetTarget()); break; case ADRP: // Not implemented in the assembler. UNIMPLEMENTED(); break; default: UNREACHABLE(); break; } } void Simulator::VisitUnconditionalBranch(Instruction* instr) { switch (instr->Mask(UnconditionalBranchMask)) { case BL: set_lr(instr->following()); // Fall through. case B: set_pc(instr->ImmPCOffsetTarget()); break; default: UNREACHABLE(); } } void Simulator::VisitConditionalBranch(Instruction* instr) { DCHECK(instr->Mask(ConditionalBranchMask) == B_cond); if (ConditionPassed(static_cast(instr->ConditionBranch()))) { set_pc(instr->ImmPCOffsetTarget()); } } void Simulator::VisitUnconditionalBranchToRegister(Instruction* instr) { Instruction* target = reg(instr->Rn()); switch (instr->Mask(UnconditionalBranchToRegisterMask)) { case BLR: { set_lr(instr->following()); if (instr->Rn() == 31) { // BLR XZR is used as a guard for the constant pool. We should never hit // this, but if we do trap to allow debugging. Debug(); } // Fall through. } case BR: case RET: set_pc(target); break; default: UNIMPLEMENTED(); } } void Simulator::VisitTestBranch(Instruction* instr) { unsigned bit_pos = (instr->ImmTestBranchBit5() << 5) | instr->ImmTestBranchBit40(); bool take_branch = ((xreg(instr->Rt()) & (1UL << bit_pos)) == 0); switch (instr->Mask(TestBranchMask)) { case TBZ: break; case TBNZ: take_branch = !take_branch; break; default: UNIMPLEMENTED(); } if (take_branch) { set_pc(instr->ImmPCOffsetTarget()); } } void Simulator::VisitCompareBranch(Instruction* instr) { unsigned rt = instr->Rt(); bool take_branch = false; switch (instr->Mask(CompareBranchMask)) { case CBZ_w: take_branch = (wreg(rt) == 0); break; case CBZ_x: take_branch = (xreg(rt) == 0); break; case CBNZ_w: take_branch = (wreg(rt) != 0); break; case CBNZ_x: take_branch = (xreg(rt) != 0); break; default: UNIMPLEMENTED(); } if (take_branch) { set_pc(instr->ImmPCOffsetTarget()); } } template void Simulator::AddSubHelper(Instruction* instr, T op2) { bool set_flags = instr->FlagsUpdate(); T new_val = 0; Instr operation = instr->Mask(AddSubOpMask); switch (operation) { case ADD: case ADDS: { new_val = AddWithCarry(set_flags, reg(instr->Rn(), instr->RnMode()), op2); break; } case SUB: case SUBS: { new_val = AddWithCarry(set_flags, reg(instr->Rn(), instr->RnMode()), ~op2, 1); break; } default: UNREACHABLE(); } set_reg(instr->Rd(), new_val, instr->RdMode()); } void Simulator::VisitAddSubShifted(Instruction* instr) { Shift shift_type = static_cast(instr->ShiftDP()); unsigned shift_amount = instr->ImmDPShift(); if (instr->SixtyFourBits()) { int64_t op2 = ShiftOperand(xreg(instr->Rm()), shift_type, shift_amount); AddSubHelper(instr, op2); } else { int32_t op2 = ShiftOperand(wreg(instr->Rm()), shift_type, shift_amount); AddSubHelper(instr, op2); } } void Simulator::VisitAddSubImmediate(Instruction* instr) { int64_t op2 = instr->ImmAddSub() << ((instr->ShiftAddSub() == 1) ? 12 : 0); if (instr->SixtyFourBits()) { AddSubHelper(instr, op2); } else { AddSubHelper(instr, op2); } } void Simulator::VisitAddSubExtended(Instruction* instr) { Extend ext = static_cast(instr->ExtendMode()); unsigned left_shift = instr->ImmExtendShift(); if (instr->SixtyFourBits()) { int64_t op2 = ExtendValue(xreg(instr->Rm()), ext, left_shift); AddSubHelper(instr, op2); } else { int32_t op2 = ExtendValue(wreg(instr->Rm()), ext, left_shift); AddSubHelper(instr, op2); } } void Simulator::VisitAddSubWithCarry(Instruction* instr) { if (instr->SixtyFourBits()) { AddSubWithCarry(instr); } else { AddSubWithCarry(instr); } } void Simulator::VisitLogicalShifted(Instruction* instr) { Shift shift_type = static_cast(instr->ShiftDP()); unsigned shift_amount = instr->ImmDPShift(); if (instr->SixtyFourBits()) { int64_t op2 = ShiftOperand(xreg(instr->Rm()), shift_type, shift_amount); op2 = (instr->Mask(NOT) == NOT) ? ~op2 : op2; LogicalHelper(instr, op2); } else { int32_t op2 = ShiftOperand(wreg(instr->Rm()), shift_type, shift_amount); op2 = (instr->Mask(NOT) == NOT) ? ~op2 : op2; LogicalHelper(instr, op2); } } void Simulator::VisitLogicalImmediate(Instruction* instr) { if (instr->SixtyFourBits()) { LogicalHelper(instr, instr->ImmLogical()); } else { LogicalHelper(instr, instr->ImmLogical()); } } template void Simulator::LogicalHelper(Instruction* instr, T op2) { T op1 = reg(instr->Rn()); T result = 0; bool update_flags = false; // Switch on the logical operation, stripping out the NOT bit, as it has a // different meaning for logical immediate instructions. switch (instr->Mask(LogicalOpMask & ~NOT)) { case ANDS: update_flags = true; // Fall through. case AND: result = op1 & op2; break; case ORR: result = op1 | op2; break; case EOR: result = op1 ^ op2; break; default: UNIMPLEMENTED(); } if (update_flags) { nzcv().SetN(CalcNFlag(result)); nzcv().SetZ(CalcZFlag(result)); nzcv().SetC(0); nzcv().SetV(0); LogSystemRegister(NZCV); } set_reg(instr->Rd(), result, instr->RdMode()); } void Simulator::VisitConditionalCompareRegister(Instruction* instr) { if (instr->SixtyFourBits()) { ConditionalCompareHelper(instr, xreg(instr->Rm())); } else { ConditionalCompareHelper(instr, wreg(instr->Rm())); } } void Simulator::VisitConditionalCompareImmediate(Instruction* instr) { if (instr->SixtyFourBits()) { ConditionalCompareHelper(instr, instr->ImmCondCmp()); } else { ConditionalCompareHelper(instr, instr->ImmCondCmp()); } } template void Simulator::ConditionalCompareHelper(Instruction* instr, T op2) { T op1 = reg(instr->Rn()); if (ConditionPassed(static_cast(instr->Condition()))) { // If the condition passes, set the status flags to the result of comparing // the operands. if (instr->Mask(ConditionalCompareMask) == CCMP) { AddWithCarry(true, op1, ~op2, 1); } else { DCHECK(instr->Mask(ConditionalCompareMask) == CCMN); AddWithCarry(true, op1, op2, 0); } } else { // If the condition fails, set the status flags to the nzcv immediate. nzcv().SetFlags(instr->Nzcv()); LogSystemRegister(NZCV); } } void Simulator::VisitLoadStoreUnsignedOffset(Instruction* instr) { int offset = instr->ImmLSUnsigned() << instr->SizeLS(); LoadStoreHelper(instr, offset, Offset); } void Simulator::VisitLoadStoreUnscaledOffset(Instruction* instr) { LoadStoreHelper(instr, instr->ImmLS(), Offset); } void Simulator::VisitLoadStorePreIndex(Instruction* instr) { LoadStoreHelper(instr, instr->ImmLS(), PreIndex); } void Simulator::VisitLoadStorePostIndex(Instruction* instr) { LoadStoreHelper(instr, instr->ImmLS(), PostIndex); } void Simulator::VisitLoadStoreRegisterOffset(Instruction* instr) { Extend ext = static_cast(instr->ExtendMode()); DCHECK((ext == UXTW) || (ext == UXTX) || (ext == SXTW) || (ext == SXTX)); unsigned shift_amount = instr->ImmShiftLS() * instr->SizeLS(); int64_t offset = ExtendValue(xreg(instr->Rm()), ext, shift_amount); LoadStoreHelper(instr, offset, Offset); } void Simulator::LoadStoreHelper(Instruction* instr, int64_t offset, AddrMode addrmode) { unsigned srcdst = instr->Rt(); unsigned addr_reg = instr->Rn(); uintptr_t address = LoadStoreAddress(addr_reg, offset, addrmode); uintptr_t stack = 0; // Handle the writeback for stores before the store. On a CPU the writeback // and the store are atomic, but when running on the simulator it is possible // to be interrupted in between. The simulator is not thread safe and V8 does // not require it to be to run JavaScript therefore the profiler may sample // the "simulated" CPU in the middle of load/store with writeback. The code // below ensures that push operations are safe even when interrupted: the // stack pointer will be decremented before adding an element to the stack. if (instr->IsStore()) { LoadStoreWriteBack(addr_reg, offset, addrmode); // For store the address post writeback is used to check access below the // stack. stack = sp(); } LoadStoreOp op = static_cast(instr->Mask(LoadStoreOpMask)); switch (op) { // Use _no_log variants to suppress the register trace (LOG_REGS, // LOG_FP_REGS). We will print a more detailed log. case LDRB_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDRH_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDR_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDR_x: set_xreg_no_log(srcdst, MemoryRead(address)); break; case LDRSB_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDRSH_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDRSB_x: set_xreg_no_log(srcdst, MemoryRead(address)); break; case LDRSH_x: set_xreg_no_log(srcdst, MemoryRead(address)); break; case LDRSW_x: set_xreg_no_log(srcdst, MemoryRead(address)); break; case LDR_s: set_sreg_no_log(srcdst, MemoryRead(address)); break; case LDR_d: set_dreg_no_log(srcdst, MemoryRead(address)); break; case STRB_w: MemoryWrite(address, wreg(srcdst)); break; case STRH_w: MemoryWrite(address, wreg(srcdst)); break; case STR_w: MemoryWrite(address, wreg(srcdst)); break; case STR_x: MemoryWrite(address, xreg(srcdst)); break; case STR_s: MemoryWrite(address, sreg(srcdst)); break; case STR_d: MemoryWrite(address, dreg(srcdst)); break; default: UNIMPLEMENTED(); } // Print a detailed trace (including the memory address) instead of the basic // register:value trace generated by set_*reg(). size_t access_size = 1 << instr->SizeLS(); if (instr->IsLoad()) { if ((op == LDR_s) || (op == LDR_d)) { LogReadFP(address, access_size, srcdst); } else { LogRead(address, access_size, srcdst); } } else { if ((op == STR_s) || (op == STR_d)) { LogWriteFP(address, access_size, srcdst); } else { LogWrite(address, access_size, srcdst); } } // Handle the writeback for loads after the load to ensure safe pop // operation even when interrupted in the middle of it. The stack pointer // is only updated after the load so pop(fp) will never break the invariant // sp <= fp expected while walking the stack in the sampler. if (instr->IsLoad()) { // For loads the address pre writeback is used to check access below the // stack. stack = sp(); LoadStoreWriteBack(addr_reg, offset, addrmode); } // Accesses below the stack pointer (but above the platform stack limit) are // not allowed in the ABI. CheckMemoryAccess(address, stack); } void Simulator::VisitLoadStorePairOffset(Instruction* instr) { LoadStorePairHelper(instr, Offset); } void Simulator::VisitLoadStorePairPreIndex(Instruction* instr) { LoadStorePairHelper(instr, PreIndex); } void Simulator::VisitLoadStorePairPostIndex(Instruction* instr) { LoadStorePairHelper(instr, PostIndex); } void Simulator::VisitLoadStorePairNonTemporal(Instruction* instr) { LoadStorePairHelper(instr, Offset); } void Simulator::LoadStorePairHelper(Instruction* instr, AddrMode addrmode) { unsigned rt = instr->Rt(); unsigned rt2 = instr->Rt2(); unsigned addr_reg = instr->Rn(); size_t access_size = 1 << instr->SizeLSPair(); int64_t offset = instr->ImmLSPair() * access_size; uintptr_t address = LoadStoreAddress(addr_reg, offset, addrmode); uintptr_t address2 = address + access_size; uintptr_t stack = 0; // Handle the writeback for stores before the store. On a CPU the writeback // and the store are atomic, but when running on the simulator it is possible // to be interrupted in between. The simulator is not thread safe and V8 does // not require it to be to run JavaScript therefore the profiler may sample // the "simulated" CPU in the middle of load/store with writeback. The code // below ensures that push operations are safe even when interrupted: the // stack pointer will be decremented before adding an element to the stack. if (instr->IsStore()) { LoadStoreWriteBack(addr_reg, offset, addrmode); // For store the address post writeback is used to check access below the // stack. stack = sp(); } LoadStorePairOp op = static_cast(instr->Mask(LoadStorePairMask)); // 'rt' and 'rt2' can only be aliased for stores. DCHECK(((op & LoadStorePairLBit) == 0) || (rt != rt2)); switch (op) { // Use _no_log variants to suppress the register trace (LOG_REGS, // LOG_FP_REGS). We will print a more detailed log. case LDP_w: { DCHECK(access_size == kWRegSize); set_wreg_no_log(rt, MemoryRead(address)); set_wreg_no_log(rt2, MemoryRead(address2)); break; } case LDP_s: { DCHECK(access_size == kSRegSize); set_sreg_no_log(rt, MemoryRead(address)); set_sreg_no_log(rt2, MemoryRead(address2)); break; } case LDP_x: { DCHECK(access_size == kXRegSize); set_xreg_no_log(rt, MemoryRead(address)); set_xreg_no_log(rt2, MemoryRead(address2)); break; } case LDP_d: { DCHECK(access_size == kDRegSize); set_dreg_no_log(rt, MemoryRead(address)); set_dreg_no_log(rt2, MemoryRead(address2)); break; } case LDPSW_x: { DCHECK(access_size == kWRegSize); set_xreg_no_log(rt, MemoryRead(address)); set_xreg_no_log(rt2, MemoryRead(address2)); break; } case STP_w: { DCHECK(access_size == kWRegSize); MemoryWrite(address, wreg(rt)); MemoryWrite(address2, wreg(rt2)); break; } case STP_s: { DCHECK(access_size == kSRegSize); MemoryWrite(address, sreg(rt)); MemoryWrite(address2, sreg(rt2)); break; } case STP_x: { DCHECK(access_size == kXRegSize); MemoryWrite(address, xreg(rt)); MemoryWrite(address2, xreg(rt2)); break; } case STP_d: { DCHECK(access_size == kDRegSize); MemoryWrite(address, dreg(rt)); MemoryWrite(address2, dreg(rt2)); break; } default: UNREACHABLE(); } // Print a detailed trace (including the memory address) instead of the basic // register:value trace generated by set_*reg(). if (instr->IsLoad()) { if ((op == LDP_s) || (op == LDP_d)) { LogReadFP(address, access_size, rt); LogReadFP(address2, access_size, rt2); } else { LogRead(address, access_size, rt); LogRead(address2, access_size, rt2); } } else { if ((op == STP_s) || (op == STP_d)) { LogWriteFP(address, access_size, rt); LogWriteFP(address2, access_size, rt2); } else { LogWrite(address, access_size, rt); LogWrite(address2, access_size, rt2); } } // Handle the writeback for loads after the load to ensure safe pop // operation even when interrupted in the middle of it. The stack pointer // is only updated after the load so pop(fp) will never break the invariant // sp <= fp expected while walking the stack in the sampler. if (instr->IsLoad()) { // For loads the address pre writeback is used to check access below the // stack. stack = sp(); LoadStoreWriteBack(addr_reg, offset, addrmode); } // Accesses below the stack pointer (but above the platform stack limit) are // not allowed in the ABI. CheckMemoryAccess(address, stack); } void Simulator::VisitLoadLiteral(Instruction* instr) { uintptr_t address = instr->LiteralAddress(); unsigned rt = instr->Rt(); switch (instr->Mask(LoadLiteralMask)) { // Use _no_log variants to suppress the register trace (LOG_REGS, // LOG_FP_REGS), then print a more detailed log. case LDR_w_lit: set_wreg_no_log(rt, MemoryRead(address)); LogRead(address, kWRegSize, rt); break; case LDR_x_lit: set_xreg_no_log(rt, MemoryRead(address)); LogRead(address, kXRegSize, rt); break; case LDR_s_lit: set_sreg_no_log(rt, MemoryRead(address)); LogReadFP(address, kSRegSize, rt); break; case LDR_d_lit: set_dreg_no_log(rt, MemoryRead(address)); LogReadFP(address, kDRegSize, rt); break; default: UNREACHABLE(); } } uintptr_t Simulator::LoadStoreAddress(unsigned addr_reg, int64_t offset, AddrMode addrmode) { const unsigned kSPRegCode = kSPRegInternalCode & kRegCodeMask; uint64_t address = xreg(addr_reg, Reg31IsStackPointer); if ((addr_reg == kSPRegCode) && ((address % 16) != 0)) { // When the base register is SP the stack pointer is required to be // quadword aligned prior to the address calculation and write-backs. // Misalignment will cause a stack alignment fault. FATAL("ALIGNMENT EXCEPTION"); } if ((addrmode == Offset) || (addrmode == PreIndex)) { address += offset; } return address; } void Simulator::LoadStoreWriteBack(unsigned addr_reg, int64_t offset, AddrMode addrmode) { if ((addrmode == PreIndex) || (addrmode == PostIndex)) { DCHECK(offset != 0); uint64_t address = xreg(addr_reg, Reg31IsStackPointer); set_reg(addr_reg, address + offset, Reg31IsStackPointer); } } void Simulator::CheckMemoryAccess(uintptr_t address, uintptr_t stack) { if ((address >= stack_limit_) && (address < stack)) { fprintf(stream_, "ACCESS BELOW STACK POINTER:\n"); fprintf(stream_, " sp is here: 0x%016" PRIx64 "\n", static_cast(stack)); fprintf(stream_, " access was here: 0x%016" PRIx64 "\n", static_cast(address)); fprintf(stream_, " stack limit is here: 0x%016" PRIx64 "\n", static_cast(stack_limit_)); fprintf(stream_, "\n"); FATAL("ACCESS BELOW STACK POINTER"); } } void Simulator::VisitMoveWideImmediate(Instruction* instr) { MoveWideImmediateOp mov_op = static_cast(instr->Mask(MoveWideImmediateMask)); int64_t new_xn_val = 0; bool is_64_bits = instr->SixtyFourBits() == 1; // Shift is limited for W operations. DCHECK(is_64_bits || (instr->ShiftMoveWide() < 2)); // Get the shifted immediate. int64_t shift = instr->ShiftMoveWide() * 16; int64_t shifted_imm16 = instr->ImmMoveWide() << shift; // Compute the new value. switch (mov_op) { case MOVN_w: case MOVN_x: { new_xn_val = ~shifted_imm16; if (!is_64_bits) new_xn_val &= kWRegMask; break; } case MOVK_w: case MOVK_x: { unsigned reg_code = instr->Rd(); int64_t prev_xn_val = is_64_bits ? xreg(reg_code) : wreg(reg_code); new_xn_val = (prev_xn_val & ~(0xffffL << shift)) | shifted_imm16; break; } case MOVZ_w: case MOVZ_x: { new_xn_val = shifted_imm16; break; } default: UNREACHABLE(); } // Update the destination register. set_xreg(instr->Rd(), new_xn_val); } void Simulator::VisitConditionalSelect(Instruction* instr) { if (ConditionFailed(static_cast(instr->Condition()))) { uint64_t new_val = xreg(instr->Rm()); switch (instr->Mask(ConditionalSelectMask)) { case CSEL_w: set_wreg(instr->Rd(), new_val); break; case CSEL_x: set_xreg(instr->Rd(), new_val); break; case CSINC_w: set_wreg(instr->Rd(), new_val + 1); break; case CSINC_x: set_xreg(instr->Rd(), new_val + 1); break; case CSINV_w: set_wreg(instr->Rd(), ~new_val); break; case CSINV_x: set_xreg(instr->Rd(), ~new_val); break; case CSNEG_w: set_wreg(instr->Rd(), -new_val); break; case CSNEG_x: set_xreg(instr->Rd(), -new_val); break; default: UNIMPLEMENTED(); } } else { if (instr->SixtyFourBits()) { set_xreg(instr->Rd(), xreg(instr->Rn())); } else { set_wreg(instr->Rd(), wreg(instr->Rn())); } } } void Simulator::VisitDataProcessing1Source(Instruction* instr) { unsigned dst = instr->Rd(); unsigned src = instr->Rn(); switch (instr->Mask(DataProcessing1SourceMask)) { case RBIT_w: set_wreg(dst, ReverseBits(wreg(src), kWRegSizeInBits)); break; case RBIT_x: set_xreg(dst, ReverseBits(xreg(src), kXRegSizeInBits)); break; case REV16_w: set_wreg(dst, ReverseBytes(wreg(src), Reverse16)); break; case REV16_x: set_xreg(dst, ReverseBytes(xreg(src), Reverse16)); break; case REV_w: set_wreg(dst, ReverseBytes(wreg(src), Reverse32)); break; case REV32_x: set_xreg(dst, ReverseBytes(xreg(src), Reverse32)); break; case REV_x: set_xreg(dst, ReverseBytes(xreg(src), Reverse64)); break; case CLZ_w: set_wreg(dst, CountLeadingZeros(wreg(src), kWRegSizeInBits)); break; case CLZ_x: set_xreg(dst, CountLeadingZeros(xreg(src), kXRegSizeInBits)); break; case CLS_w: { set_wreg(dst, CountLeadingSignBits(wreg(src), kWRegSizeInBits)); break; } case CLS_x: { set_xreg(dst, CountLeadingSignBits(xreg(src), kXRegSizeInBits)); break; } default: UNIMPLEMENTED(); } } uint64_t Simulator::ReverseBits(uint64_t value, unsigned num_bits) { DCHECK((num_bits == kWRegSizeInBits) || (num_bits == kXRegSizeInBits)); uint64_t result = 0; for (unsigned i = 0; i < num_bits; i++) { result = (result << 1) | (value & 1); value >>= 1; } return result; } uint64_t Simulator::ReverseBytes(uint64_t value, ReverseByteMode mode) { // Split the 64-bit value into an 8-bit array, where b[0] is the least // significant byte, and b[7] is the most significant. uint8_t bytes[8]; uint64_t mask = 0xff00000000000000UL; for (int i = 7; i >= 0; i--) { bytes[i] = (value & mask) >> (i * 8); mask >>= 8; } // Permutation tables for REV instructions. // permute_table[Reverse16] is used by REV16_x, REV16_w // permute_table[Reverse32] is used by REV32_x, REV_w // permute_table[Reverse64] is used by REV_x DCHECK((Reverse16 == 0) && (Reverse32 == 1) && (Reverse64 == 2)); static const uint8_t permute_table[3][8] = { {6, 7, 4, 5, 2, 3, 0, 1}, {4, 5, 6, 7, 0, 1, 2, 3}, {0, 1, 2, 3, 4, 5, 6, 7} }; uint64_t result = 0; for (int i = 0; i < 8; i++) { result <<= 8; result |= bytes[permute_table[mode][i]]; } return result; } template void Simulator::DataProcessing2Source(Instruction* instr) { Shift shift_op = NO_SHIFT; T result = 0; switch (instr->Mask(DataProcessing2SourceMask)) { case SDIV_w: case SDIV_x: { T rn = reg(instr->Rn()); T rm = reg(instr->Rm()); if ((rn == std::numeric_limits::min()) && (rm == -1)) { result = std::numeric_limits::min(); } else if (rm == 0) { // Division by zero can be trapped, but not on A-class processors. result = 0; } else { result = rn / rm; } break; } case UDIV_w: case UDIV_x: { typedef typename make_unsigned::type unsignedT; unsignedT rn = static_cast(reg(instr->Rn())); unsignedT rm = static_cast(reg(instr->Rm())); if (rm == 0) { // Division by zero can be trapped, but not on A-class processors. result = 0; } else { result = rn / rm; } break; } case LSLV_w: case LSLV_x: shift_op = LSL; break; case LSRV_w: case LSRV_x: shift_op = LSR; break; case ASRV_w: case ASRV_x: shift_op = ASR; break; case RORV_w: case RORV_x: shift_op = ROR; break; default: UNIMPLEMENTED(); } if (shift_op != NO_SHIFT) { // Shift distance encoded in the least-significant five/six bits of the // register. unsigned shift = wreg(instr->Rm()); if (sizeof(T) == kWRegSize) { shift &= kShiftAmountWRegMask; } else { shift &= kShiftAmountXRegMask; } result = ShiftOperand(reg(instr->Rn()), shift_op, shift); } set_reg(instr->Rd(), result); } void Simulator::VisitDataProcessing2Source(Instruction* instr) { if (instr->SixtyFourBits()) { DataProcessing2Source(instr); } else { DataProcessing2Source(instr); } } // The algorithm used is described in section 8.2 of // Hacker's Delight, by Henry S. Warren, Jr. // It assumes that a right shift on a signed integer is an arithmetic shift. static int64_t MultiplyHighSigned(int64_t u, int64_t v) { uint64_t u0, v0, w0; int64_t u1, v1, w1, w2, t; u0 = u & 0xffffffffL; u1 = u >> 32; v0 = v & 0xffffffffL; v1 = v >> 32; w0 = u0 * v0; t = u1 * v0 + (w0 >> 32); w1 = t & 0xffffffffL; w2 = t >> 32; w1 = u0 * v1 + w1; return u1 * v1 + w2 + (w1 >> 32); } void Simulator::VisitDataProcessing3Source(Instruction* instr) { int64_t result = 0; // Extract and sign- or zero-extend 32-bit arguments for widening operations. uint64_t rn_u32 = reg(instr->Rn()); uint64_t rm_u32 = reg(instr->Rm()); int64_t rn_s32 = reg(instr->Rn()); int64_t rm_s32 = reg(instr->Rm()); switch (instr->Mask(DataProcessing3SourceMask)) { case MADD_w: case MADD_x: result = xreg(instr->Ra()) + (xreg(instr->Rn()) * xreg(instr->Rm())); break; case MSUB_w: case MSUB_x: result = xreg(instr->Ra()) - (xreg(instr->Rn()) * xreg(instr->Rm())); break; case SMADDL_x: result = xreg(instr->Ra()) + (rn_s32 * rm_s32); break; case SMSUBL_x: result = xreg(instr->Ra()) - (rn_s32 * rm_s32); break; case UMADDL_x: result = xreg(instr->Ra()) + (rn_u32 * rm_u32); break; case UMSUBL_x: result = xreg(instr->Ra()) - (rn_u32 * rm_u32); break; case SMULH_x: DCHECK(instr->Ra() == kZeroRegCode); result = MultiplyHighSigned(xreg(instr->Rn()), xreg(instr->Rm())); break; default: UNIMPLEMENTED(); } if (instr->SixtyFourBits()) { set_xreg(instr->Rd(), result); } else { set_wreg(instr->Rd(), result); } } template void Simulator::BitfieldHelper(Instruction* instr) { typedef typename make_unsigned::type unsignedT; T reg_size = sizeof(T) * 8; T R = instr->ImmR(); T S = instr->ImmS(); T diff = S - R; T mask; if (diff >= 0) { mask = diff < reg_size - 1 ? (static_cast(1) << (diff + 1)) - 1 : static_cast(-1); } else { mask = ((1L << (S + 1)) - 1); mask = (static_cast(mask) >> R) | (mask << (reg_size - R)); diff += reg_size; } // inzero indicates if the extracted bitfield is inserted into the // destination register value or in zero. // If extend is true, extend the sign of the extracted bitfield. bool inzero = false; bool extend = false; switch (instr->Mask(BitfieldMask)) { case BFM_x: case BFM_w: break; case SBFM_x: case SBFM_w: inzero = true; extend = true; break; case UBFM_x: case UBFM_w: inzero = true; break; default: UNIMPLEMENTED(); } T dst = inzero ? 0 : reg(instr->Rd()); T src = reg(instr->Rn()); // Rotate source bitfield into place. T result = (static_cast(src) >> R) | (src << (reg_size - R)); // Determine the sign extension. T topbits_preshift = (static_cast(1) << (reg_size - diff - 1)) - 1; T signbits = (extend && ((src >> S) & 1) ? topbits_preshift : 0) << (diff + 1); // Merge sign extension, dest/zero and bitfield. result = signbits | (result & mask) | (dst & ~mask); set_reg(instr->Rd(), result); } void Simulator::VisitBitfield(Instruction* instr) { if (instr->SixtyFourBits()) { BitfieldHelper(instr); } else { BitfieldHelper(instr); } } void Simulator::VisitExtract(Instruction* instr) { if (instr->SixtyFourBits()) { Extract(instr); } else { Extract(instr); } } void Simulator::VisitFPImmediate(Instruction* instr) { AssertSupportedFPCR(); unsigned dest = instr->Rd(); switch (instr->Mask(FPImmediateMask)) { case FMOV_s_imm: set_sreg(dest, instr->ImmFP32()); break; case FMOV_d_imm: set_dreg(dest, instr->ImmFP64()); break; default: UNREACHABLE(); } } void Simulator::VisitFPIntegerConvert(Instruction* instr) { AssertSupportedFPCR(); unsigned dst = instr->Rd(); unsigned src = instr->Rn(); FPRounding round = fpcr().RMode(); switch (instr->Mask(FPIntegerConvertMask)) { case FCVTAS_ws: set_wreg(dst, FPToInt32(sreg(src), FPTieAway)); break; case FCVTAS_xs: set_xreg(dst, FPToInt64(sreg(src), FPTieAway)); break; case FCVTAS_wd: set_wreg(dst, FPToInt32(dreg(src), FPTieAway)); break; case FCVTAS_xd: set_xreg(dst, FPToInt64(dreg(src), FPTieAway)); break; case FCVTAU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPTieAway)); break; case FCVTAU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPTieAway)); break; case FCVTAU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPTieAway)); break; case FCVTAU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPTieAway)); break; case FCVTMS_ws: set_wreg(dst, FPToInt32(sreg(src), FPNegativeInfinity)); break; case FCVTMS_xs: set_xreg(dst, FPToInt64(sreg(src), FPNegativeInfinity)); break; case FCVTMS_wd: set_wreg(dst, FPToInt32(dreg(src), FPNegativeInfinity)); break; case FCVTMS_xd: set_xreg(dst, FPToInt64(dreg(src), FPNegativeInfinity)); break; case FCVTMU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPNegativeInfinity)); break; case FCVTMU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPNegativeInfinity)); break; case FCVTMU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPNegativeInfinity)); break; case FCVTMU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPNegativeInfinity)); break; case FCVTNS_ws: set_wreg(dst, FPToInt32(sreg(src), FPTieEven)); break; case FCVTNS_xs: set_xreg(dst, FPToInt64(sreg(src), FPTieEven)); break; case FCVTNS_wd: set_wreg(dst, FPToInt32(dreg(src), FPTieEven)); break; case FCVTNS_xd: set_xreg(dst, FPToInt64(dreg(src), FPTieEven)); break; case FCVTNU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPTieEven)); break; case FCVTNU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPTieEven)); break; case FCVTNU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPTieEven)); break; case FCVTNU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPTieEven)); break; case FCVTZS_ws: set_wreg(dst, FPToInt32(sreg(src), FPZero)); break; case FCVTZS_xs: set_xreg(dst, FPToInt64(sreg(src), FPZero)); break; case FCVTZS_wd: set_wreg(dst, FPToInt32(dreg(src), FPZero)); break; case FCVTZS_xd: set_xreg(dst, FPToInt64(dreg(src), FPZero)); break; case FCVTZU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPZero)); break; case FCVTZU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPZero)); break; case FCVTZU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPZero)); break; case FCVTZU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPZero)); break; case FMOV_ws: set_wreg(dst, sreg_bits(src)); break; case FMOV_xd: set_xreg(dst, dreg_bits(src)); break; case FMOV_sw: set_sreg_bits(dst, wreg(src)); break; case FMOV_dx: set_dreg_bits(dst, xreg(src)); break; // A 32-bit input can be handled in the same way as a 64-bit input, since // the sign- or zero-extension will not affect the conversion. case SCVTF_dx: set_dreg(dst, FixedToDouble(xreg(src), 0, round)); break; case SCVTF_dw: set_dreg(dst, FixedToDouble(wreg(src), 0, round)); break; case UCVTF_dx: set_dreg(dst, UFixedToDouble(xreg(src), 0, round)); break; case UCVTF_dw: { set_dreg(dst, UFixedToDouble(reg(src), 0, round)); break; } case SCVTF_sx: set_sreg(dst, FixedToFloat(xreg(src), 0, round)); break; case SCVTF_sw: set_sreg(dst, FixedToFloat(wreg(src), 0, round)); break; case UCVTF_sx: set_sreg(dst, UFixedToFloat(xreg(src), 0, round)); break; case UCVTF_sw: { set_sreg(dst, UFixedToFloat(reg(src), 0, round)); break; } default: UNREACHABLE(); } } void Simulator::VisitFPFixedPointConvert(Instruction* instr) { AssertSupportedFPCR(); unsigned dst = instr->Rd(); unsigned src = instr->Rn(); int fbits = 64 - instr->FPScale(); FPRounding round = fpcr().RMode(); switch (instr->Mask(FPFixedPointConvertMask)) { // A 32-bit input can be handled in the same way as a 64-bit input, since // the sign- or zero-extension will not affect the conversion. case SCVTF_dx_fixed: set_dreg(dst, FixedToDouble(xreg(src), fbits, round)); break; case SCVTF_dw_fixed: set_dreg(dst, FixedToDouble(wreg(src), fbits, round)); break; case UCVTF_dx_fixed: set_dreg(dst, UFixedToDouble(xreg(src), fbits, round)); break; case UCVTF_dw_fixed: { set_dreg(dst, UFixedToDouble(reg(src), fbits, round)); break; } case SCVTF_sx_fixed: set_sreg(dst, FixedToFloat(xreg(src), fbits, round)); break; case SCVTF_sw_fixed: set_sreg(dst, FixedToFloat(wreg(src), fbits, round)); break; case UCVTF_sx_fixed: set_sreg(dst, UFixedToFloat(xreg(src), fbits, round)); break; case UCVTF_sw_fixed: { set_sreg(dst, UFixedToFloat(reg(src), fbits, round)); break; } default: UNREACHABLE(); } } int32_t Simulator::FPToInt32(double value, FPRounding rmode) { value = FPRoundInt(value, rmode); if (value >= kWMaxInt) { return kWMaxInt; } else if (value < kWMinInt) { return kWMinInt; } return std::isnan(value) ? 0 : static_cast(value); } int64_t Simulator::FPToInt64(double value, FPRounding rmode) { value = FPRoundInt(value, rmode); if (value >= kXMaxInt) { return kXMaxInt; } else if (value < kXMinInt) { return kXMinInt; } return std::isnan(value) ? 0 : static_cast(value); } uint32_t Simulator::FPToUInt32(double value, FPRounding rmode) { value = FPRoundInt(value, rmode); if (value >= kWMaxUInt) { return kWMaxUInt; } else if (value < 0.0) { return 0; } return std::isnan(value) ? 0 : static_cast(value); } uint64_t Simulator::FPToUInt64(double value, FPRounding rmode) { value = FPRoundInt(value, rmode); if (value >= kXMaxUInt) { return kXMaxUInt; } else if (value < 0.0) { return 0; } return std::isnan(value) ? 0 : static_cast(value); } void Simulator::VisitFPCompare(Instruction* instr) { AssertSupportedFPCR(); unsigned reg_size = (instr->Mask(FP64) == FP64) ? kDRegSizeInBits : kSRegSizeInBits; double fn_val = fpreg(reg_size, instr->Rn()); switch (instr->Mask(FPCompareMask)) { case FCMP_s: case FCMP_d: FPCompare(fn_val, fpreg(reg_size, instr->Rm())); break; case FCMP_s_zero: case FCMP_d_zero: FPCompare(fn_val, 0.0); break; default: UNIMPLEMENTED(); } } void Simulator::VisitFPConditionalCompare(Instruction* instr) { AssertSupportedFPCR(); switch (instr->Mask(FPConditionalCompareMask)) { case FCCMP_s: case FCCMP_d: { if (ConditionPassed(static_cast(instr->Condition()))) { // If the condition passes, set the status flags to the result of // comparing the operands. unsigned reg_size = (instr->Mask(FP64) == FP64) ? kDRegSizeInBits : kSRegSizeInBits; FPCompare(fpreg(reg_size, instr->Rn()), fpreg(reg_size, instr->Rm())); } else { // If the condition fails, set the status flags to the nzcv immediate. nzcv().SetFlags(instr->Nzcv()); LogSystemRegister(NZCV); } break; } default: UNIMPLEMENTED(); } } void Simulator::VisitFPConditionalSelect(Instruction* instr) { AssertSupportedFPCR(); Instr selected; if (ConditionPassed(static_cast(instr->Condition()))) { selected = instr->Rn(); } else { selected = instr->Rm(); } switch (instr->Mask(FPConditionalSelectMask)) { case FCSEL_s: set_sreg(instr->Rd(), sreg(selected)); break; case FCSEL_d: set_dreg(instr->Rd(), dreg(selected)); break; default: UNIMPLEMENTED(); } } void Simulator::VisitFPDataProcessing1Source(Instruction* instr) { AssertSupportedFPCR(); unsigned fd = instr->Rd(); unsigned fn = instr->Rn(); switch (instr->Mask(FPDataProcessing1SourceMask)) { case FMOV_s: set_sreg(fd, sreg(fn)); break; case FMOV_d: set_dreg(fd, dreg(fn)); break; case FABS_s: set_sreg(fd, std::fabs(sreg(fn))); break; case FABS_d: set_dreg(fd, std::fabs(dreg(fn))); break; case FNEG_s: set_sreg(fd, -sreg(fn)); break; case FNEG_d: set_dreg(fd, -dreg(fn)); break; case FSQRT_s: set_sreg(fd, FPSqrt(sreg(fn))); break; case FSQRT_d: set_dreg(fd, FPSqrt(dreg(fn))); break; case FRINTA_s: set_sreg(fd, FPRoundInt(sreg(fn), FPTieAway)); break; case FRINTA_d: set_dreg(fd, FPRoundInt(dreg(fn), FPTieAway)); break; case FRINTM_s: set_sreg(fd, FPRoundInt(sreg(fn), FPNegativeInfinity)); break; case FRINTM_d: set_dreg(fd, FPRoundInt(dreg(fn), FPNegativeInfinity)); break; case FRINTN_s: set_sreg(fd, FPRoundInt(sreg(fn), FPTieEven)); break; case FRINTN_d: set_dreg(fd, FPRoundInt(dreg(fn), FPTieEven)); break; case FRINTZ_s: set_sreg(fd, FPRoundInt(sreg(fn), FPZero)); break; case FRINTZ_d: set_dreg(fd, FPRoundInt(dreg(fn), FPZero)); break; case FCVT_ds: set_dreg(fd, FPToDouble(sreg(fn))); break; case FCVT_sd: set_sreg(fd, FPToFloat(dreg(fn), FPTieEven)); break; default: UNIMPLEMENTED(); } } // Assemble the specified IEEE-754 components into the target type and apply // appropriate rounding. // sign: 0 = positive, 1 = negative // exponent: Unbiased IEEE-754 exponent. // mantissa: The mantissa of the input. The top bit (which is not encoded for // normal IEEE-754 values) must not be omitted. This bit has the // value 'pow(2, exponent)'. // // The input value is assumed to be a normalized value. That is, the input may // not be infinity or NaN. If the source value is subnormal, it must be // normalized before calling this function such that the highest set bit in the // mantissa has the value 'pow(2, exponent)'. // // Callers should use FPRoundToFloat or FPRoundToDouble directly, rather than // calling a templated FPRound. template static T FPRound(int64_t sign, int64_t exponent, uint64_t mantissa, FPRounding round_mode) { DCHECK((sign == 0) || (sign == 1)); // Only the FPTieEven rounding mode is implemented. DCHECK(round_mode == FPTieEven); USE(round_mode); // Rounding can promote subnormals to normals, and normals to infinities. For // example, a double with exponent 127 (FLT_MAX_EXP) would appear to be // encodable as a float, but rounding based on the low-order mantissa bits // could make it overflow. With ties-to-even rounding, this value would become // an infinity. // ---- Rounding Method ---- // // The exponent is irrelevant in the rounding operation, so we treat the // lowest-order bit that will fit into the result ('onebit') as having // the value '1'. Similarly, the highest-order bit that won't fit into // the result ('halfbit') has the value '0.5'. The 'point' sits between // 'onebit' and 'halfbit': // // These bits fit into the result. // |---------------------| // mantissa = 0bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // || // / | // / halfbit // onebit // // For subnormal outputs, the range of representable bits is smaller and // the position of onebit and halfbit depends on the exponent of the // input, but the method is otherwise similar. // // onebit(frac) // | // | halfbit(frac) halfbit(adjusted) // | / / // | | | // 0b00.0 (exact) -> 0b00.0 (exact) -> 0b00 // 0b00.0... -> 0b00.0... -> 0b00 // 0b00.1 (exact) -> 0b00.0111..111 -> 0b00 // 0b00.1... -> 0b00.1... -> 0b01 // 0b01.0 (exact) -> 0b01.0 (exact) -> 0b01 // 0b01.0... -> 0b01.0... -> 0b01 // 0b01.1 (exact) -> 0b01.1 (exact) -> 0b10 // 0b01.1... -> 0b01.1... -> 0b10 // 0b10.0 (exact) -> 0b10.0 (exact) -> 0b10 // 0b10.0... -> 0b10.0... -> 0b10 // 0b10.1 (exact) -> 0b10.0111..111 -> 0b10 // 0b10.1... -> 0b10.1... -> 0b11 // 0b11.0 (exact) -> 0b11.0 (exact) -> 0b11 // ... / | / | // / | / | // / | // adjusted = frac - (halfbit(mantissa) & ~onebit(frac)); / | // // mantissa = (mantissa >> shift) + halfbit(adjusted); static const int mantissa_offset = 0; static const int exponent_offset = mantissa_offset + mbits; static const int sign_offset = exponent_offset + ebits; STATIC_ASSERT(sign_offset == (sizeof(T) * kByteSize - 1)); // Bail out early for zero inputs. if (mantissa == 0) { return sign << sign_offset; } // If all bits in the exponent are set, the value is infinite or NaN. // This is true for all binary IEEE-754 formats. static const int infinite_exponent = (1 << ebits) - 1; static const int max_normal_exponent = infinite_exponent - 1; // Apply the exponent bias to encode it for the result. Doing this early makes // it easy to detect values that will be infinite or subnormal. exponent += max_normal_exponent >> 1; if (exponent > max_normal_exponent) { // Overflow: The input is too large for the result type to represent. The // FPTieEven rounding mode handles overflows using infinities. exponent = infinite_exponent; mantissa = 0; return (sign << sign_offset) | (exponent << exponent_offset) | (mantissa << mantissa_offset); } // Calculate the shift required to move the top mantissa bit to the proper // place in the destination type. const int highest_significant_bit = 63 - CountLeadingZeros(mantissa, 64); int shift = highest_significant_bit - mbits; if (exponent <= 0) { // The output will be subnormal (before rounding). // For subnormal outputs, the shift must be adjusted by the exponent. The +1 // is necessary because the exponent of a subnormal value (encoded as 0) is // the same as the exponent of the smallest normal value (encoded as 1). shift += -exponent + 1; // Handle inputs that would produce a zero output. // // Shifts higher than highest_significant_bit+1 will always produce a zero // result. A shift of exactly highest_significant_bit+1 might produce a // non-zero result after rounding. if (shift > (highest_significant_bit + 1)) { // The result will always be +/-0.0. return sign << sign_offset; } // Properly encode the exponent for a subnormal output. exponent = 0; } else { // Clear the topmost mantissa bit, since this is not encoded in IEEE-754 // normal values. mantissa &= ~(1UL << highest_significant_bit); } if (shift > 0) { // We have to shift the mantissa to the right. Some precision is lost, so we // need to apply rounding. uint64_t onebit_mantissa = (mantissa >> (shift)) & 1; uint64_t halfbit_mantissa = (mantissa >> (shift-1)) & 1; uint64_t adjusted = mantissa - (halfbit_mantissa & ~onebit_mantissa); T halfbit_adjusted = (adjusted >> (shift-1)) & 1; T result = (sign << sign_offset) | (exponent << exponent_offset) | ((mantissa >> shift) << mantissa_offset); // A very large mantissa can overflow during rounding. If this happens, the // exponent should be incremented and the mantissa set to 1.0 (encoded as // 0). Applying halfbit_adjusted after assembling the float has the nice // side-effect that this case is handled for free. // // This also handles cases where a very large finite value overflows to // infinity, or where a very large subnormal value overflows to become // normal. return result + halfbit_adjusted; } else { // We have to shift the mantissa to the left (or not at all). The input // mantissa is exactly representable in the output mantissa, so apply no // rounding correction. return (sign << sign_offset) | (exponent << exponent_offset) | ((mantissa << -shift) << mantissa_offset); } } // See FPRound for a description of this function. static inline double FPRoundToDouble(int64_t sign, int64_t exponent, uint64_t mantissa, FPRounding round_mode) { int64_t bits = FPRound(sign, exponent, mantissa, round_mode); return rawbits_to_double(bits); } // See FPRound for a description of this function. static inline float FPRoundToFloat(int64_t sign, int64_t exponent, uint64_t mantissa, FPRounding round_mode) { int32_t bits = FPRound(sign, exponent, mantissa, round_mode); return rawbits_to_float(bits); } double Simulator::FixedToDouble(int64_t src, int fbits, FPRounding round) { if (src >= 0) { return UFixedToDouble(src, fbits, round); } else { // This works for all negative values, including INT64_MIN. return -UFixedToDouble(-src, fbits, round); } } double Simulator::UFixedToDouble(uint64_t src, int fbits, FPRounding round) { // An input of 0 is a special case because the result is effectively // subnormal: The exponent is encoded as 0 and there is no implicit 1 bit. if (src == 0) { return 0.0; } // Calculate the exponent. The highest significant bit will have the value // 2^exponent. const int highest_significant_bit = 63 - CountLeadingZeros(src, 64); const int64_t exponent = highest_significant_bit - fbits; return FPRoundToDouble(0, exponent, src, round); } float Simulator::FixedToFloat(int64_t src, int fbits, FPRounding round) { if (src >= 0) { return UFixedToFloat(src, fbits, round); } else { // This works for all negative values, including INT64_MIN. return -UFixedToFloat(-src, fbits, round); } } float Simulator::UFixedToFloat(uint64_t src, int fbits, FPRounding round) { // An input of 0 is a special case because the result is effectively // subnormal: The exponent is encoded as 0 and there is no implicit 1 bit. if (src == 0) { return 0.0f; } // Calculate the exponent. The highest significant bit will have the value // 2^exponent. const int highest_significant_bit = 63 - CountLeadingZeros(src, 64); const int32_t exponent = highest_significant_bit - fbits; return FPRoundToFloat(0, exponent, src, round); } double Simulator::FPRoundInt(double value, FPRounding round_mode) { if ((value == 0.0) || (value == kFP64PositiveInfinity) || (value == kFP64NegativeInfinity)) { return value; } else if (std::isnan(value)) { return FPProcessNaN(value); } double int_result = floor(value); double error = value - int_result; switch (round_mode) { case FPTieAway: { // Take care of correctly handling the range ]-0.5, -0.0], which must // yield -0.0. if ((-0.5 < value) && (value < 0.0)) { int_result = -0.0; } else if ((error > 0.5) || ((error == 0.5) && (int_result >= 0.0))) { // If the error is greater than 0.5, or is equal to 0.5 and the integer // result is positive, round up. int_result++; } break; } case FPTieEven: { // Take care of correctly handling the range [-0.5, -0.0], which must // yield -0.0. if ((-0.5 <= value) && (value < 0.0)) { int_result = -0.0; // If the error is greater than 0.5, or is equal to 0.5 and the integer // result is odd, round up. } else if ((error > 0.5) || ((error == 0.5) && (fmod(int_result, 2) != 0))) { int_result++; } break; } case FPZero: { // If value > 0 then we take floor(value) // otherwise, ceil(value) if (value < 0) { int_result = ceil(value); } break; } case FPNegativeInfinity: { // We always use floor(value). break; } default: UNIMPLEMENTED(); } return int_result; } double Simulator::FPToDouble(float value) { switch (std::fpclassify(value)) { case FP_NAN: { if (fpcr().DN()) return kFP64DefaultNaN; // Convert NaNs as the processor would: // - The sign is propagated. // - The payload (mantissa) is transferred entirely, except that the top // bit is forced to '1', making the result a quiet NaN. The unused // (low-order) payload bits are set to 0. uint32_t raw = float_to_rawbits(value); uint64_t sign = raw >> 31; uint64_t exponent = (1 << 11) - 1; uint64_t payload = unsigned_bitextract_64(21, 0, raw); payload <<= (52 - 23); // The unused low-order bits should be 0. payload |= (1L << 51); // Force a quiet NaN. return rawbits_to_double((sign << 63) | (exponent << 52) | payload); } case FP_ZERO: case FP_NORMAL: case FP_SUBNORMAL: case FP_INFINITE: { // All other inputs are preserved in a standard cast, because every value // representable using an IEEE-754 float is also representable using an // IEEE-754 double. return static_cast(value); } } UNREACHABLE(); return static_cast(value); } float Simulator::FPToFloat(double value, FPRounding round_mode) { // Only the FPTieEven rounding mode is implemented. DCHECK(round_mode == FPTieEven); USE(round_mode); switch (std::fpclassify(value)) { case FP_NAN: { if (fpcr().DN()) return kFP32DefaultNaN; // Convert NaNs as the processor would: // - The sign is propagated. // - The payload (mantissa) is transferred as much as possible, except // that the top bit is forced to '1', making the result a quiet NaN. uint64_t raw = double_to_rawbits(value); uint32_t sign = raw >> 63; uint32_t exponent = (1 << 8) - 1; uint32_t payload = unsigned_bitextract_64(50, 52 - 23, raw); payload |= (1 << 22); // Force a quiet NaN. return rawbits_to_float((sign << 31) | (exponent << 23) | payload); } case FP_ZERO: case FP_INFINITE: { // In a C++ cast, any value representable in the target type will be // unchanged. This is always the case for +/-0.0 and infinities. return static_cast(value); } case FP_NORMAL: case FP_SUBNORMAL: { // Convert double-to-float as the processor would, assuming that FPCR.FZ // (flush-to-zero) is not set. uint64_t raw = double_to_rawbits(value); // Extract the IEEE-754 double components. uint32_t sign = raw >> 63; // Extract the exponent and remove the IEEE-754 encoding bias. int32_t exponent = unsigned_bitextract_64(62, 52, raw) - 1023; // Extract the mantissa and add the implicit '1' bit. uint64_t mantissa = unsigned_bitextract_64(51, 0, raw); if (std::fpclassify(value) == FP_NORMAL) { mantissa |= (1UL << 52); } return FPRoundToFloat(sign, exponent, mantissa, round_mode); } } UNREACHABLE(); return value; } void Simulator::VisitFPDataProcessing2Source(Instruction* instr) { AssertSupportedFPCR(); unsigned fd = instr->Rd(); unsigned fn = instr->Rn(); unsigned fm = instr->Rm(); // Fmaxnm and Fminnm have special NaN handling. switch (instr->Mask(FPDataProcessing2SourceMask)) { case FMAXNM_s: set_sreg(fd, FPMaxNM(sreg(fn), sreg(fm))); return; case FMAXNM_d: set_dreg(fd, FPMaxNM(dreg(fn), dreg(fm))); return; case FMINNM_s: set_sreg(fd, FPMinNM(sreg(fn), sreg(fm))); return; case FMINNM_d: set_dreg(fd, FPMinNM(dreg(fn), dreg(fm))); return; default: break; // Fall through. } if (FPProcessNaNs(instr)) return; switch (instr->Mask(FPDataProcessing2SourceMask)) { case FADD_s: set_sreg(fd, FPAdd(sreg(fn), sreg(fm))); break; case FADD_d: set_dreg(fd, FPAdd(dreg(fn), dreg(fm))); break; case FSUB_s: set_sreg(fd, FPSub(sreg(fn), sreg(fm))); break; case FSUB_d: set_dreg(fd, FPSub(dreg(fn), dreg(fm))); break; case FMUL_s: set_sreg(fd, FPMul(sreg(fn), sreg(fm))); break; case FMUL_d: set_dreg(fd, FPMul(dreg(fn), dreg(fm))); break; case FDIV_s: set_sreg(fd, FPDiv(sreg(fn), sreg(fm))); break; case FDIV_d: set_dreg(fd, FPDiv(dreg(fn), dreg(fm))); break; case FMAX_s: set_sreg(fd, FPMax(sreg(fn), sreg(fm))); break; case FMAX_d: set_dreg(fd, FPMax(dreg(fn), dreg(fm))); break; case FMIN_s: set_sreg(fd, FPMin(sreg(fn), sreg(fm))); break; case FMIN_d: set_dreg(fd, FPMin(dreg(fn), dreg(fm))); break; case FMAXNM_s: case FMAXNM_d: case FMINNM_s: case FMINNM_d: // These were handled before the standard FPProcessNaNs() stage. UNREACHABLE(); default: UNIMPLEMENTED(); } } void Simulator::VisitFPDataProcessing3Source(Instruction* instr) { AssertSupportedFPCR(); unsigned fd = instr->Rd(); unsigned fn = instr->Rn(); unsigned fm = instr->Rm(); unsigned fa = instr->Ra(); switch (instr->Mask(FPDataProcessing3SourceMask)) { // fd = fa +/- (fn * fm) case FMADD_s: set_sreg(fd, FPMulAdd(sreg(fa), sreg(fn), sreg(fm))); break; case FMSUB_s: set_sreg(fd, FPMulAdd(sreg(fa), -sreg(fn), sreg(fm))); break; case FMADD_d: set_dreg(fd, FPMulAdd(dreg(fa), dreg(fn), dreg(fm))); break; case FMSUB_d: set_dreg(fd, FPMulAdd(dreg(fa), -dreg(fn), dreg(fm))); break; // Negated variants of the above. case FNMADD_s: set_sreg(fd, FPMulAdd(-sreg(fa), -sreg(fn), sreg(fm))); break; case FNMSUB_s: set_sreg(fd, FPMulAdd(-sreg(fa), sreg(fn), sreg(fm))); break; case FNMADD_d: set_dreg(fd, FPMulAdd(-dreg(fa), -dreg(fn), dreg(fm))); break; case FNMSUB_d: set_dreg(fd, FPMulAdd(-dreg(fa), dreg(fn), dreg(fm))); break; default: UNIMPLEMENTED(); } } template T Simulator::FPAdd(T op1, T op2) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(op1) && !std::isnan(op2)); if (std::isinf(op1) && std::isinf(op2) && (op1 != op2)) { // inf + -inf returns the default NaN. return FPDefaultNaN(); } else { // Other cases should be handled by standard arithmetic. return op1 + op2; } } template T Simulator::FPDiv(T op1, T op2) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(op1) && !std::isnan(op2)); if ((std::isinf(op1) && std::isinf(op2)) || ((op1 == 0.0) && (op2 == 0.0))) { // inf / inf and 0.0 / 0.0 return the default NaN. return FPDefaultNaN(); } else { // Other cases should be handled by standard arithmetic. return op1 / op2; } } template T Simulator::FPMax(T a, T b) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(a) && !std::isnan(b)); if ((a == 0.0) && (b == 0.0) && (copysign(1.0, a) != copysign(1.0, b))) { // a and b are zero, and the sign differs: return +0.0. return 0.0; } else { return (a > b) ? a : b; } } template T Simulator::FPMaxNM(T a, T b) { if (IsQuietNaN(a) && !IsQuietNaN(b)) { a = kFP64NegativeInfinity; } else if (!IsQuietNaN(a) && IsQuietNaN(b)) { b = kFP64NegativeInfinity; } T result = FPProcessNaNs(a, b); return std::isnan(result) ? result : FPMax(a, b); } template T Simulator::FPMin(T a, T b) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(a) && !std::isnan(b)); if ((a == 0.0) && (b == 0.0) && (copysign(1.0, a) != copysign(1.0, b))) { // a and b are zero, and the sign differs: return -0.0. return -0.0; } else { return (a < b) ? a : b; } } template T Simulator::FPMinNM(T a, T b) { if (IsQuietNaN(a) && !IsQuietNaN(b)) { a = kFP64PositiveInfinity; } else if (!IsQuietNaN(a) && IsQuietNaN(b)) { b = kFP64PositiveInfinity; } T result = FPProcessNaNs(a, b); return std::isnan(result) ? result : FPMin(a, b); } template T Simulator::FPMul(T op1, T op2) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(op1) && !std::isnan(op2)); if ((std::isinf(op1) && (op2 == 0.0)) || (std::isinf(op2) && (op1 == 0.0))) { // inf * 0.0 returns the default NaN. return FPDefaultNaN(); } else { // Other cases should be handled by standard arithmetic. return op1 * op2; } } template T Simulator::FPMulAdd(T a, T op1, T op2) { T result = FPProcessNaNs3(a, op1, op2); T sign_a = copysign(1.0, a); T sign_prod = copysign(1.0, op1) * copysign(1.0, op2); bool isinf_prod = std::isinf(op1) || std::isinf(op2); bool operation_generates_nan = (std::isinf(op1) && (op2 == 0.0)) || // inf * 0.0 (std::isinf(op2) && (op1 == 0.0)) || // 0.0 * inf (std::isinf(a) && isinf_prod && (sign_a != sign_prod)); // inf - inf if (std::isnan(result)) { // Generated NaNs override quiet NaNs propagated from a. if (operation_generates_nan && IsQuietNaN(a)) { return FPDefaultNaN(); } else { return result; } } // If the operation would produce a NaN, return the default NaN. if (operation_generates_nan) { return FPDefaultNaN(); } // Work around broken fma implementations for exact zero results: The sign of // exact 0.0 results is positive unless both a and op1 * op2 are negative. if (((op1 == 0.0) || (op2 == 0.0)) && (a == 0.0)) { return ((sign_a < 0) && (sign_prod < 0)) ? -0.0 : 0.0; } result = FusedMultiplyAdd(op1, op2, a); DCHECK(!std::isnan(result)); // Work around broken fma implementations for rounded zero results: If a is // 0.0, the sign of the result is the sign of op1 * op2 before rounding. if ((a == 0.0) && (result == 0.0)) { return copysign(0.0, sign_prod); } return result; } template T Simulator::FPSqrt(T op) { if (std::isnan(op)) { return FPProcessNaN(op); } else if (op < 0.0) { return FPDefaultNaN(); } else { return std::sqrt(op); } } template T Simulator::FPSub(T op1, T op2) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(op1) && !std::isnan(op2)); if (std::isinf(op1) && std::isinf(op2) && (op1 == op2)) { // inf - inf returns the default NaN. return FPDefaultNaN(); } else { // Other cases should be handled by standard arithmetic. return op1 - op2; } } template T Simulator::FPProcessNaN(T op) { DCHECK(std::isnan(op)); return fpcr().DN() ? FPDefaultNaN() : ToQuietNaN(op); } template T Simulator::FPProcessNaNs(T op1, T op2) { if (IsSignallingNaN(op1)) { return FPProcessNaN(op1); } else if (IsSignallingNaN(op2)) { return FPProcessNaN(op2); } else if (std::isnan(op1)) { DCHECK(IsQuietNaN(op1)); return FPProcessNaN(op1); } else if (std::isnan(op2)) { DCHECK(IsQuietNaN(op2)); return FPProcessNaN(op2); } else { return 0.0; } } template T Simulator::FPProcessNaNs3(T op1, T op2, T op3) { if (IsSignallingNaN(op1)) { return FPProcessNaN(op1); } else if (IsSignallingNaN(op2)) { return FPProcessNaN(op2); } else if (IsSignallingNaN(op3)) { return FPProcessNaN(op3); } else if (std::isnan(op1)) { DCHECK(IsQuietNaN(op1)); return FPProcessNaN(op1); } else if (std::isnan(op2)) { DCHECK(IsQuietNaN(op2)); return FPProcessNaN(op2); } else if (std::isnan(op3)) { DCHECK(IsQuietNaN(op3)); return FPProcessNaN(op3); } else { return 0.0; } } bool Simulator::FPProcessNaNs(Instruction* instr) { unsigned fd = instr->Rd(); unsigned fn = instr->Rn(); unsigned fm = instr->Rm(); bool done = false; if (instr->Mask(FP64) == FP64) { double result = FPProcessNaNs(dreg(fn), dreg(fm)); if (std::isnan(result)) { set_dreg(fd, result); done = true; } } else { float result = FPProcessNaNs(sreg(fn), sreg(fm)); if (std::isnan(result)) { set_sreg(fd, result); done = true; } } return done; } void Simulator::VisitSystem(Instruction* instr) { // Some system instructions hijack their Op and Cp fields to represent a // range of immediates instead of indicating a different instruction. This // makes the decoding tricky. if (instr->Mask(SystemSysRegFMask) == SystemSysRegFixed) { switch (instr->Mask(SystemSysRegMask)) { case MRS: { switch (instr->ImmSystemRegister()) { case NZCV: set_xreg(instr->Rt(), nzcv().RawValue()); break; case FPCR: set_xreg(instr->Rt(), fpcr().RawValue()); break; default: UNIMPLEMENTED(); } break; } case MSR: { switch (instr->ImmSystemRegister()) { case NZCV: nzcv().SetRawValue(xreg(instr->Rt())); LogSystemRegister(NZCV); break; case FPCR: fpcr().SetRawValue(xreg(instr->Rt())); LogSystemRegister(FPCR); break; default: UNIMPLEMENTED(); } break; } } } else if (instr->Mask(SystemHintFMask) == SystemHintFixed) { DCHECK(instr->Mask(SystemHintMask) == HINT); switch (instr->ImmHint()) { case NOP: break; default: UNIMPLEMENTED(); } } else if (instr->Mask(MemBarrierFMask) == MemBarrierFixed) { __sync_synchronize(); } else { UNIMPLEMENTED(); } } bool Simulator::GetValue(const char* desc, int64_t* value) { int regnum = CodeFromName(desc); if (regnum >= 0) { unsigned code = regnum; if (code == kZeroRegCode) { // Catch the zero register and return 0. *value = 0; return true; } else if (code == kSPRegInternalCode) { // Translate the stack pointer code to 31, for Reg31IsStackPointer. code = 31; } if (desc[0] == 'w') { *value = wreg(code, Reg31IsStackPointer); } else { *value = xreg(code, Reg31IsStackPointer); } return true; } else if (strncmp(desc, "0x", 2) == 0) { return SScanF(desc + 2, "%" SCNx64, reinterpret_cast(value)) == 1; } else { return SScanF(desc, "%" SCNu64, reinterpret_cast(value)) == 1; } } bool Simulator::PrintValue(const char* desc) { if (strcmp(desc, "csp") == 0) { DCHECK(CodeFromName(desc) == static_cast(kSPRegInternalCode)); PrintF(stream_, "%s csp:%s 0x%016" PRIx64 "%s\n", clr_reg_name, clr_reg_value, xreg(31, Reg31IsStackPointer), clr_normal); return true; } else if (strcmp(desc, "wcsp") == 0) { DCHECK(CodeFromName(desc) == static_cast(kSPRegInternalCode)); PrintF(stream_, "%s wcsp:%s 0x%08" PRIx32 "%s\n", clr_reg_name, clr_reg_value, wreg(31, Reg31IsStackPointer), clr_normal); return true; } int i = CodeFromName(desc); STATIC_ASSERT(kNumberOfRegisters == kNumberOfFPRegisters); if (i < 0 || static_cast(i) >= kNumberOfFPRegisters) return false; if (desc[0] == 'v') { PrintF(stream_, "%s %s:%s 0x%016" PRIx64 "%s (%s%s:%s %g%s %s:%s %g%s)\n", clr_fpreg_name, VRegNameForCode(i), clr_fpreg_value, double_to_rawbits(dreg(i)), clr_normal, clr_fpreg_name, DRegNameForCode(i), clr_fpreg_value, dreg(i), clr_fpreg_name, SRegNameForCode(i), clr_fpreg_value, sreg(i), clr_normal); return true; } else if (desc[0] == 'd') { PrintF(stream_, "%s %s:%s %g%s\n", clr_fpreg_name, DRegNameForCode(i), clr_fpreg_value, dreg(i), clr_normal); return true; } else if (desc[0] == 's') { PrintF(stream_, "%s %s:%s %g%s\n", clr_fpreg_name, SRegNameForCode(i), clr_fpreg_value, sreg(i), clr_normal); return true; } else if (desc[0] == 'w') { PrintF(stream_, "%s %s:%s 0x%08" PRIx32 "%s\n", clr_reg_name, WRegNameForCode(i), clr_reg_value, wreg(i), clr_normal); return true; } else { // X register names have a wide variety of starting characters, but anything // else will be an X register. PrintF(stream_, "%s %s:%s 0x%016" PRIx64 "%s\n", clr_reg_name, XRegNameForCode(i), clr_reg_value, xreg(i), clr_normal); return true; } } void Simulator::Debug() { #define COMMAND_SIZE 63 #define ARG_SIZE 255 #define STR(a) #a #define XSTR(a) STR(a) char cmd[COMMAND_SIZE + 1]; char arg1[ARG_SIZE + 1]; char arg2[ARG_SIZE + 1]; char* argv[3] = { cmd, arg1, arg2 }; // Make sure to have a proper terminating character if reaching the limit. cmd[COMMAND_SIZE] = 0; arg1[ARG_SIZE] = 0; arg2[ARG_SIZE] = 0; bool done = false; bool cleared_log_disasm_bit = false; while (!done) { // Disassemble the next instruction to execute before doing anything else. PrintInstructionsAt(pc_, 1); // Read the command line. char* line = ReadLine("sim> "); if (line == NULL) { break; } else { // Repeat last command by default. char* last_input = last_debugger_input(); if (strcmp(line, "\n") == 0 && (last_input != NULL)) { DeleteArray(line); line = last_input; } else { // Update the latest command ran set_last_debugger_input(line); } // Use sscanf to parse the individual parts of the command line. At the // moment no command expects more than two parameters. int argc = SScanF(line, "%" XSTR(COMMAND_SIZE) "s " "%" XSTR(ARG_SIZE) "s " "%" XSTR(ARG_SIZE) "s", cmd, arg1, arg2); // stepi / si ------------------------------------------------------------ if ((strcmp(cmd, "si") == 0) || (strcmp(cmd, "stepi") == 0)) { // We are about to execute instructions, after which by default we // should increment the pc_. If it was set when reaching this debug // instruction, it has not been cleared because this instruction has not // completed yet. So clear it manually. pc_modified_ = false; if (argc == 1) { ExecuteInstruction(); } else { int64_t number_of_instructions_to_execute = 1; GetValue(arg1, &number_of_instructions_to_execute); set_log_parameters(log_parameters() | LOG_DISASM); while (number_of_instructions_to_execute-- > 0) { ExecuteInstruction(); } set_log_parameters(log_parameters() & ~LOG_DISASM); PrintF("\n"); } // If it was necessary, the pc has already been updated or incremented // when executing the instruction. So we do not want it to be updated // again. It will be cleared when exiting. pc_modified_ = true; // next / n -------------------------------------------------------------- } else if ((strcmp(cmd, "next") == 0) || (strcmp(cmd, "n") == 0)) { // Tell the simulator to break after the next executed BL. break_on_next_ = true; // Continue. done = true; // continue / cont / c --------------------------------------------------- } else if ((strcmp(cmd, "continue") == 0) || (strcmp(cmd, "cont") == 0) || (strcmp(cmd, "c") == 0)) { // Leave the debugger shell. done = true; // disassemble / disasm / di --------------------------------------------- } else if (strcmp(cmd, "disassemble") == 0 || strcmp(cmd, "disasm") == 0 || strcmp(cmd, "di") == 0) { int64_t n_of_instrs_to_disasm = 10; // default value. int64_t address = reinterpret_cast(pc_); // default value. if (argc >= 2) { // disasm GetValue(arg1, &n_of_instrs_to_disasm); } if (argc >= 3) { // disasm GetValue(arg2, &address); } // Disassemble. PrintInstructionsAt(reinterpret_cast(address), n_of_instrs_to_disasm); PrintF("\n"); // print / p ------------------------------------------------------------- } else if ((strcmp(cmd, "print") == 0) || (strcmp(cmd, "p") == 0)) { if (argc == 2) { if (strcmp(arg1, "all") == 0) { PrintRegisters(); PrintFPRegisters(); } else { if (!PrintValue(arg1)) { PrintF("%s unrecognized\n", arg1); } } } else { PrintF( "print \n" " Print the content of a register. (alias 'p')\n" " 'print all' will print all registers.\n" " Use 'printobject' to get more details about the value.\n"); } // printobject / po ------------------------------------------------------ } else if ((strcmp(cmd, "printobject") == 0) || (strcmp(cmd, "po") == 0)) { if (argc == 2) { int64_t value; OFStream os(stdout); if (GetValue(arg1, &value)) { Object* obj = reinterpret_cast(value); os << arg1 << ": \n"; #ifdef DEBUG obj->Print(os); os << "\n"; #else os << Brief(obj) << "\n"; #endif } else { os << arg1 << " unrecognized\n"; } } else { PrintF("printobject \n" "printobject \n" " Print details about the value. (alias 'po')\n"); } // stack / mem ---------------------------------------------------------- } else if (strcmp(cmd, "stack") == 0 || strcmp(cmd, "mem") == 0) { int64_t* cur = NULL; int64_t* end = NULL; int next_arg = 1; if (strcmp(cmd, "stack") == 0) { cur = reinterpret_cast(jssp()); } else { // "mem" int64_t value; if (!GetValue(arg1, &value)) { PrintF("%s unrecognized\n", arg1); continue; } cur = reinterpret_cast(value); next_arg++; } int64_t words = 0; if (argc == next_arg) { words = 10; } else if (argc == next_arg + 1) { if (!GetValue(argv[next_arg], &words)) { PrintF("%s unrecognized\n", argv[next_arg]); PrintF("Printing 10 double words by default"); words = 10; } } else { UNREACHABLE(); } end = cur + words; while (cur < end) { PrintF(" 0x%016" PRIx64 ": 0x%016" PRIx64 " %10" PRId64, reinterpret_cast(cur), *cur, *cur); HeapObject* obj = reinterpret_cast(*cur); int64_t value = *cur; Heap* current_heap = v8::internal::Isolate::Current()->heap(); if (((value & 1) == 0) || current_heap->Contains(obj)) { PrintF(" ("); if ((value & kSmiTagMask) == 0) { STATIC_ASSERT(kSmiValueSize == 32); int32_t untagged = (value >> kSmiShift) & 0xffffffff; PrintF("smi %" PRId32, untagged); } else { obj->ShortPrint(); } PrintF(")"); } PrintF("\n"); cur++; } // trace / t ------------------------------------------------------------- } else if (strcmp(cmd, "trace") == 0 || strcmp(cmd, "t") == 0) { if ((log_parameters() & (LOG_DISASM | LOG_REGS)) != (LOG_DISASM | LOG_REGS)) { PrintF("Enabling disassembly and registers tracing\n"); set_log_parameters(log_parameters() | LOG_DISASM | LOG_REGS); } else { PrintF("Disabling disassembly and registers tracing\n"); set_log_parameters(log_parameters() & ~(LOG_DISASM | LOG_REGS)); } // break / b ------------------------------------------------------------- } else if (strcmp(cmd, "break") == 0 || strcmp(cmd, "b") == 0) { if (argc == 2) { int64_t value; if (GetValue(arg1, &value)) { SetBreakpoint(reinterpret_cast(value)); } else { PrintF("%s unrecognized\n", arg1); } } else { ListBreakpoints(); PrintF("Use `break ` to set or disable a breakpoint\n"); } // gdb ------------------------------------------------------------------- } else if (strcmp(cmd, "gdb") == 0) { PrintF("Relinquishing control to gdb.\n"); base::OS::DebugBreak(); PrintF("Regaining control from gdb.\n"); // sysregs --------------------------------------------------------------- } else if (strcmp(cmd, "sysregs") == 0) { PrintSystemRegisters(); // help / h -------------------------------------------------------------- } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "h") == 0) { PrintF( "stepi / si\n" " stepi \n" " Step instructions.\n" "next / n\n" " Continue execution until a BL instruction is reached.\n" " At this point a breakpoint is set just after this BL.\n" " Then execution is resumed. It will probably later hit the\n" " breakpoint just set.\n" "continue / cont / c\n" " Continue execution from here.\n" "disassemble / disasm / di\n" " disassemble \n" " Disassemble
:value <- address". fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s", clr_reg_name, XRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); fprintf(stream_, " <- %s0x%016" PRIxPTR "%s\n", clr_memory_address, address, clr_normal); } void Simulator::PrintReadFP(uintptr_t address, size_t size, unsigned reg_code) { // The template is "# reg:bits (reg:value) <- address". switch (size) { case kSRegSize: fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s (%s%s: %s%gf%s)", clr_fpreg_name, VRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal, clr_fpreg_name, SRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal); break; case kDRegSize: fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s (%s%s: %s%g%s)", clr_fpreg_name, VRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal, clr_fpreg_name, DRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal); break; default: UNREACHABLE(); } fprintf(stream_, " <- %s0x%016" PRIxPTR "%s\n", clr_memory_address, address, clr_normal); } void Simulator::PrintWrite(uintptr_t address, size_t size, unsigned reg_code) { // The template is "# reg:value -> address". To keep the trace tidy and // readable, the value is aligned with the values in the register trace. switch (size) { case kByteSizeInBytes: fprintf(stream_, "# %s%5s<7:0>: %s0x%02" PRIx8 "%s", clr_reg_name, WRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); break; case kHalfWordSizeInBytes: fprintf(stream_, "# %s%5s<15:0>: %s0x%04" PRIx16 "%s", clr_reg_name, WRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); break; case kWRegSize: fprintf(stream_, "# %s%5s: %s0x%08" PRIx32 "%s", clr_reg_name, WRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); break; case kXRegSize: fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s", clr_reg_name, XRegNameForCode(reg_code), clr_reg_value, reg(reg_code), clr_normal); break; default: UNREACHABLE(); } fprintf(stream_, " -> %s0x%016" PRIxPTR "%s\n", clr_memory_address, address, clr_normal); } void Simulator::PrintWriteFP(uintptr_t address, size_t size, unsigned reg_code) { // The template is "# reg:bits (reg:value) -> address". To keep the trace tidy // and readable, the value is aligned with the values in the register trace. switch (size) { case kSRegSize: fprintf(stream_, "# %s%5s<31:0>: %s0x%08" PRIx32 "%s (%s%s: %s%gf%s)", clr_fpreg_name, VRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal, clr_fpreg_name, SRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal); break; case kDRegSize: fprintf(stream_, "# %s%5s: %s0x%016" PRIx64 "%s (%s%s: %s%g%s)", clr_fpreg_name, VRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal, clr_fpreg_name, DRegNameForCode(reg_code), clr_fpreg_value, fpreg(reg_code), clr_normal); break; default: UNREACHABLE(); } fprintf(stream_, " -> %s0x%016" PRIxPTR "%s\n", clr_memory_address, address, clr_normal); } // Visitors--------------------------------------------------------------------- void Simulator::VisitUnimplemented(Instruction* instr) { fprintf(stream_, "Unimplemented instruction at %p: 0x%08" PRIx32 "\n", reinterpret_cast(instr), instr->InstructionBits()); UNIMPLEMENTED(); } void Simulator::VisitUnallocated(Instruction* instr) { fprintf(stream_, "Unallocated instruction at %p: 0x%08" PRIx32 "\n", reinterpret_cast(instr), instr->InstructionBits()); UNIMPLEMENTED(); } void Simulator::VisitPCRelAddressing(Instruction* instr) { switch (instr->Mask(PCRelAddressingMask)) { case ADR: set_reg(instr->Rd(), instr->ImmPCOffsetTarget()); break; case ADRP: // Not implemented in the assembler. UNIMPLEMENTED(); break; default: UNREACHABLE(); break; } } void Simulator::VisitUnconditionalBranch(Instruction* instr) { switch (instr->Mask(UnconditionalBranchMask)) { case BL: set_lr(instr->following()); // Fall through. case B: set_pc(instr->ImmPCOffsetTarget()); break; default: UNREACHABLE(); } } void Simulator::VisitConditionalBranch(Instruction* instr) { DCHECK(instr->Mask(ConditionalBranchMask) == B_cond); if (ConditionPassed(static_cast(instr->ConditionBranch()))) { set_pc(instr->ImmPCOffsetTarget()); } } void Simulator::VisitUnconditionalBranchToRegister(Instruction* instr) { Instruction* target = reg(instr->Rn()); switch (instr->Mask(UnconditionalBranchToRegisterMask)) { case BLR: { set_lr(instr->following()); if (instr->Rn() == 31) { // BLR XZR is used as a guard for the constant pool. We should never hit // this, but if we do trap to allow debugging. Debug(); } // Fall through. } case BR: case RET: set_pc(target); break; default: UNIMPLEMENTED(); } } void Simulator::VisitTestBranch(Instruction* instr) { unsigned bit_pos = (instr->ImmTestBranchBit5() << 5) | instr->ImmTestBranchBit40(); bool take_branch = ((xreg(instr->Rt()) & (1UL << bit_pos)) == 0); switch (instr->Mask(TestBranchMask)) { case TBZ: break; case TBNZ: take_branch = !take_branch; break; default: UNIMPLEMENTED(); } if (take_branch) { set_pc(instr->ImmPCOffsetTarget()); } } void Simulator::VisitCompareBranch(Instruction* instr) { unsigned rt = instr->Rt(); bool take_branch = false; switch (instr->Mask(CompareBranchMask)) { case CBZ_w: take_branch = (wreg(rt) == 0); break; case CBZ_x: take_branch = (xreg(rt) == 0); break; case CBNZ_w: take_branch = (wreg(rt) != 0); break; case CBNZ_x: take_branch = (xreg(rt) != 0); break; default: UNIMPLEMENTED(); } if (take_branch) { set_pc(instr->ImmPCOffsetTarget()); } } template void Simulator::AddSubHelper(Instruction* instr, T op2) { bool set_flags = instr->FlagsUpdate(); T new_val = 0; Instr operation = instr->Mask(AddSubOpMask); switch (operation) { case ADD: case ADDS: { new_val = AddWithCarry(set_flags, reg(instr->Rn(), instr->RnMode()), op2); break; } case SUB: case SUBS: { new_val = AddWithCarry(set_flags, reg(instr->Rn(), instr->RnMode()), ~op2, 1); break; } default: UNREACHABLE(); } set_reg(instr->Rd(), new_val, instr->RdMode()); } void Simulator::VisitAddSubShifted(Instruction* instr) { Shift shift_type = static_cast(instr->ShiftDP()); unsigned shift_amount = instr->ImmDPShift(); if (instr->SixtyFourBits()) { int64_t op2 = ShiftOperand(xreg(instr->Rm()), shift_type, shift_amount); AddSubHelper(instr, op2); } else { int32_t op2 = ShiftOperand(wreg(instr->Rm()), shift_type, shift_amount); AddSubHelper(instr, op2); } } void Simulator::VisitAddSubImmediate(Instruction* instr) { int64_t op2 = instr->ImmAddSub() << ((instr->ShiftAddSub() == 1) ? 12 : 0); if (instr->SixtyFourBits()) { AddSubHelper(instr, op2); } else { AddSubHelper(instr, op2); } } void Simulator::VisitAddSubExtended(Instruction* instr) { Extend ext = static_cast(instr->ExtendMode()); unsigned left_shift = instr->ImmExtendShift(); if (instr->SixtyFourBits()) { int64_t op2 = ExtendValue(xreg(instr->Rm()), ext, left_shift); AddSubHelper(instr, op2); } else { int32_t op2 = ExtendValue(wreg(instr->Rm()), ext, left_shift); AddSubHelper(instr, op2); } } void Simulator::VisitAddSubWithCarry(Instruction* instr) { if (instr->SixtyFourBits()) { AddSubWithCarry(instr); } else { AddSubWithCarry(instr); } } void Simulator::VisitLogicalShifted(Instruction* instr) { Shift shift_type = static_cast(instr->ShiftDP()); unsigned shift_amount = instr->ImmDPShift(); if (instr->SixtyFourBits()) { int64_t op2 = ShiftOperand(xreg(instr->Rm()), shift_type, shift_amount); op2 = (instr->Mask(NOT) == NOT) ? ~op2 : op2; LogicalHelper(instr, op2); } else { int32_t op2 = ShiftOperand(wreg(instr->Rm()), shift_type, shift_amount); op2 = (instr->Mask(NOT) == NOT) ? ~op2 : op2; LogicalHelper(instr, op2); } } void Simulator::VisitLogicalImmediate(Instruction* instr) { if (instr->SixtyFourBits()) { LogicalHelper(instr, instr->ImmLogical()); } else { LogicalHelper(instr, instr->ImmLogical()); } } template void Simulator::LogicalHelper(Instruction* instr, T op2) { T op1 = reg(instr->Rn()); T result = 0; bool update_flags = false; // Switch on the logical operation, stripping out the NOT bit, as it has a // different meaning for logical immediate instructions. switch (instr->Mask(LogicalOpMask & ~NOT)) { case ANDS: update_flags = true; // Fall through. case AND: result = op1 & op2; break; case ORR: result = op1 | op2; break; case EOR: result = op1 ^ op2; break; default: UNIMPLEMENTED(); } if (update_flags) { nzcv().SetN(CalcNFlag(result)); nzcv().SetZ(CalcZFlag(result)); nzcv().SetC(0); nzcv().SetV(0); LogSystemRegister(NZCV); } set_reg(instr->Rd(), result, instr->RdMode()); } void Simulator::VisitConditionalCompareRegister(Instruction* instr) { if (instr->SixtyFourBits()) { ConditionalCompareHelper(instr, xreg(instr->Rm())); } else { ConditionalCompareHelper(instr, wreg(instr->Rm())); } } void Simulator::VisitConditionalCompareImmediate(Instruction* instr) { if (instr->SixtyFourBits()) { ConditionalCompareHelper(instr, instr->ImmCondCmp()); } else { ConditionalCompareHelper(instr, instr->ImmCondCmp()); } } template void Simulator::ConditionalCompareHelper(Instruction* instr, T op2) { T op1 = reg(instr->Rn()); if (ConditionPassed(static_cast(instr->Condition()))) { // If the condition passes, set the status flags to the result of comparing // the operands. if (instr->Mask(ConditionalCompareMask) == CCMP) { AddWithCarry(true, op1, ~op2, 1); } else { DCHECK(instr->Mask(ConditionalCompareMask) == CCMN); AddWithCarry(true, op1, op2, 0); } } else { // If the condition fails, set the status flags to the nzcv immediate. nzcv().SetFlags(instr->Nzcv()); LogSystemRegister(NZCV); } } void Simulator::VisitLoadStoreUnsignedOffset(Instruction* instr) { int offset = instr->ImmLSUnsigned() << instr->SizeLS(); LoadStoreHelper(instr, offset, Offset); } void Simulator::VisitLoadStoreUnscaledOffset(Instruction* instr) { LoadStoreHelper(instr, instr->ImmLS(), Offset); } void Simulator::VisitLoadStorePreIndex(Instruction* instr) { LoadStoreHelper(instr, instr->ImmLS(), PreIndex); } void Simulator::VisitLoadStorePostIndex(Instruction* instr) { LoadStoreHelper(instr, instr->ImmLS(), PostIndex); } void Simulator::VisitLoadStoreRegisterOffset(Instruction* instr) { Extend ext = static_cast(instr->ExtendMode()); DCHECK((ext == UXTW) || (ext == UXTX) || (ext == SXTW) || (ext == SXTX)); unsigned shift_amount = instr->ImmShiftLS() * instr->SizeLS(); int64_t offset = ExtendValue(xreg(instr->Rm()), ext, shift_amount); LoadStoreHelper(instr, offset, Offset); } void Simulator::LoadStoreHelper(Instruction* instr, int64_t offset, AddrMode addrmode) { unsigned srcdst = instr->Rt(); unsigned addr_reg = instr->Rn(); uintptr_t address = LoadStoreAddress(addr_reg, offset, addrmode); uintptr_t stack = 0; // Handle the writeback for stores before the store. On a CPU the writeback // and the store are atomic, but when running on the simulator it is possible // to be interrupted in between. The simulator is not thread safe and V8 does // not require it to be to run JavaScript therefore the profiler may sample // the "simulated" CPU in the middle of load/store with writeback. The code // below ensures that push operations are safe even when interrupted: the // stack pointer will be decremented before adding an element to the stack. if (instr->IsStore()) { LoadStoreWriteBack(addr_reg, offset, addrmode); // For store the address post writeback is used to check access below the // stack. stack = sp(); } LoadStoreOp op = static_cast(instr->Mask(LoadStoreOpMask)); switch (op) { // Use _no_log variants to suppress the register trace (LOG_REGS, // LOG_FP_REGS). We will print a more detailed log. case LDRB_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDRH_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDR_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDR_x: set_xreg_no_log(srcdst, MemoryRead(address)); break; case LDRSB_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDRSH_w: set_wreg_no_log(srcdst, MemoryRead(address)); break; case LDRSB_x: set_xreg_no_log(srcdst, MemoryRead(address)); break; case LDRSH_x: set_xreg_no_log(srcdst, MemoryRead(address)); break; case LDRSW_x: set_xreg_no_log(srcdst, MemoryRead(address)); break; case LDR_s: set_sreg_no_log(srcdst, MemoryRead(address)); break; case LDR_d: set_dreg_no_log(srcdst, MemoryRead(address)); break; case STRB_w: MemoryWrite(address, wreg(srcdst)); break; case STRH_w: MemoryWrite(address, wreg(srcdst)); break; case STR_w: MemoryWrite(address, wreg(srcdst)); break; case STR_x: MemoryWrite(address, xreg(srcdst)); break; case STR_s: MemoryWrite(address, sreg(srcdst)); break; case STR_d: MemoryWrite(address, dreg(srcdst)); break; default: UNIMPLEMENTED(); } // Print a detailed trace (including the memory address) instead of the basic // register:value trace generated by set_*reg(). size_t access_size = 1 << instr->SizeLS(); if (instr->IsLoad()) { if ((op == LDR_s) || (op == LDR_d)) { LogReadFP(address, access_size, srcdst); } else { LogRead(address, access_size, srcdst); } } else { if ((op == STR_s) || (op == STR_d)) { LogWriteFP(address, access_size, srcdst); } else { LogWrite(address, access_size, srcdst); } } // Handle the writeback for loads after the load to ensure safe pop // operation even when interrupted in the middle of it. The stack pointer // is only updated after the load so pop(fp) will never break the invariant // sp <= fp expected while walking the stack in the sampler. if (instr->IsLoad()) { // For loads the address pre writeback is used to check access below the // stack. stack = sp(); LoadStoreWriteBack(addr_reg, offset, addrmode); } // Accesses below the stack pointer (but above the platform stack limit) are // not allowed in the ABI. CheckMemoryAccess(address, stack); } void Simulator::VisitLoadStorePairOffset(Instruction* instr) { LoadStorePairHelper(instr, Offset); } void Simulator::VisitLoadStorePairPreIndex(Instruction* instr) { LoadStorePairHelper(instr, PreIndex); } void Simulator::VisitLoadStorePairPostIndex(Instruction* instr) { LoadStorePairHelper(instr, PostIndex); } void Simulator::VisitLoadStorePairNonTemporal(Instruction* instr) { LoadStorePairHelper(instr, Offset); } void Simulator::LoadStorePairHelper(Instruction* instr, AddrMode addrmode) { unsigned rt = instr->Rt(); unsigned rt2 = instr->Rt2(); unsigned addr_reg = instr->Rn(); size_t access_size = 1 << instr->SizeLSPair(); int64_t offset = instr->ImmLSPair() * access_size; uintptr_t address = LoadStoreAddress(addr_reg, offset, addrmode); uintptr_t address2 = address + access_size; uintptr_t stack = 0; // Handle the writeback for stores before the store. On a CPU the writeback // and the store are atomic, but when running on the simulator it is possible // to be interrupted in between. The simulator is not thread safe and V8 does // not require it to be to run JavaScript therefore the profiler may sample // the "simulated" CPU in the middle of load/store with writeback. The code // below ensures that push operations are safe even when interrupted: the // stack pointer will be decremented before adding an element to the stack. if (instr->IsStore()) { LoadStoreWriteBack(addr_reg, offset, addrmode); // For store the address post writeback is used to check access below the // stack. stack = sp(); } LoadStorePairOp op = static_cast(instr->Mask(LoadStorePairMask)); // 'rt' and 'rt2' can only be aliased for stores. DCHECK(((op & LoadStorePairLBit) == 0) || (rt != rt2)); switch (op) { // Use _no_log variants to suppress the register trace (LOG_REGS, // LOG_FP_REGS). We will print a more detailed log. case LDP_w: { DCHECK(access_size == kWRegSize); set_wreg_no_log(rt, MemoryRead(address)); set_wreg_no_log(rt2, MemoryRead(address2)); break; } case LDP_s: { DCHECK(access_size == kSRegSize); set_sreg_no_log(rt, MemoryRead(address)); set_sreg_no_log(rt2, MemoryRead(address2)); break; } case LDP_x: { DCHECK(access_size == kXRegSize); set_xreg_no_log(rt, MemoryRead(address)); set_xreg_no_log(rt2, MemoryRead(address2)); break; } case LDP_d: { DCHECK(access_size == kDRegSize); set_dreg_no_log(rt, MemoryRead(address)); set_dreg_no_log(rt2, MemoryRead(address2)); break; } case LDPSW_x: { DCHECK(access_size == kWRegSize); set_xreg_no_log(rt, MemoryRead(address)); set_xreg_no_log(rt2, MemoryRead(address2)); break; } case STP_w: { DCHECK(access_size == kWRegSize); MemoryWrite(address, wreg(rt)); MemoryWrite(address2, wreg(rt2)); break; } case STP_s: { DCHECK(access_size == kSRegSize); MemoryWrite(address, sreg(rt)); MemoryWrite(address2, sreg(rt2)); break; } case STP_x: { DCHECK(access_size == kXRegSize); MemoryWrite(address, xreg(rt)); MemoryWrite(address2, xreg(rt2)); break; } case STP_d: { DCHECK(access_size == kDRegSize); MemoryWrite(address, dreg(rt)); MemoryWrite(address2, dreg(rt2)); break; } default: UNREACHABLE(); } // Print a detailed trace (including the memory address) instead of the basic // register:value trace generated by set_*reg(). if (instr->IsLoad()) { if ((op == LDP_s) || (op == LDP_d)) { LogReadFP(address, access_size, rt); LogReadFP(address2, access_size, rt2); } else { LogRead(address, access_size, rt); LogRead(address2, access_size, rt2); } } else { if ((op == STP_s) || (op == STP_d)) { LogWriteFP(address, access_size, rt); LogWriteFP(address2, access_size, rt2); } else { LogWrite(address, access_size, rt); LogWrite(address2, access_size, rt2); } } // Handle the writeback for loads after the load to ensure safe pop // operation even when interrupted in the middle of it. The stack pointer // is only updated after the load so pop(fp) will never break the invariant // sp <= fp expected while walking the stack in the sampler. if (instr->IsLoad()) { // For loads the address pre writeback is used to check access below the // stack. stack = sp(); LoadStoreWriteBack(addr_reg, offset, addrmode); } // Accesses below the stack pointer (but above the platform stack limit) are // not allowed in the ABI. CheckMemoryAccess(address, stack); } void Simulator::VisitLoadLiteral(Instruction* instr) { uintptr_t address = instr->LiteralAddress(); unsigned rt = instr->Rt(); switch (instr->Mask(LoadLiteralMask)) { // Use _no_log variants to suppress the register trace (LOG_REGS, // LOG_FP_REGS), then print a more detailed log. case LDR_w_lit: set_wreg_no_log(rt, MemoryRead(address)); LogRead(address, kWRegSize, rt); break; case LDR_x_lit: set_xreg_no_log(rt, MemoryRead(address)); LogRead(address, kXRegSize, rt); break; case LDR_s_lit: set_sreg_no_log(rt, MemoryRead(address)); LogReadFP(address, kSRegSize, rt); break; case LDR_d_lit: set_dreg_no_log(rt, MemoryRead(address)); LogReadFP(address, kDRegSize, rt); break; default: UNREACHABLE(); } } uintptr_t Simulator::LoadStoreAddress(unsigned addr_reg, int64_t offset, AddrMode addrmode) { const unsigned kSPRegCode = kSPRegInternalCode & kRegCodeMask; uint64_t address = xreg(addr_reg, Reg31IsStackPointer); if ((addr_reg == kSPRegCode) && ((address % 16) != 0)) { // When the base register is SP the stack pointer is required to be // quadword aligned prior to the address calculation and write-backs. // Misalignment will cause a stack alignment fault. FATAL("ALIGNMENT EXCEPTION"); } if ((addrmode == Offset) || (addrmode == PreIndex)) { address += offset; } return address; } void Simulator::LoadStoreWriteBack(unsigned addr_reg, int64_t offset, AddrMode addrmode) { if ((addrmode == PreIndex) || (addrmode == PostIndex)) { DCHECK(offset != 0); uint64_t address = xreg(addr_reg, Reg31IsStackPointer); set_reg(addr_reg, address + offset, Reg31IsStackPointer); } } void Simulator::CheckMemoryAccess(uintptr_t address, uintptr_t stack) { if ((address >= stack_limit_) && (address < stack)) { fprintf(stream_, "ACCESS BELOW STACK POINTER:\n"); fprintf(stream_, " sp is here: 0x%016" PRIx64 "\n", static_cast(stack)); fprintf(stream_, " access was here: 0x%016" PRIx64 "\n", static_cast(address)); fprintf(stream_, " stack limit is here: 0x%016" PRIx64 "\n", static_cast(stack_limit_)); fprintf(stream_, "\n"); FATAL("ACCESS BELOW STACK POINTER"); } } void Simulator::VisitMoveWideImmediate(Instruction* instr) { MoveWideImmediateOp mov_op = static_cast(instr->Mask(MoveWideImmediateMask)); int64_t new_xn_val = 0; bool is_64_bits = instr->SixtyFourBits() == 1; // Shift is limited for W operations. DCHECK(is_64_bits || (instr->ShiftMoveWide() < 2)); // Get the shifted immediate. int64_t shift = instr->ShiftMoveWide() * 16; int64_t shifted_imm16 = instr->ImmMoveWide() << shift; // Compute the new value. switch (mov_op) { case MOVN_w: case MOVN_x: { new_xn_val = ~shifted_imm16; if (!is_64_bits) new_xn_val &= kWRegMask; break; } case MOVK_w: case MOVK_x: { unsigned reg_code = instr->Rd(); int64_t prev_xn_val = is_64_bits ? xreg(reg_code) : wreg(reg_code); new_xn_val = (prev_xn_val & ~(0xffffL << shift)) | shifted_imm16; break; } case MOVZ_w: case MOVZ_x: { new_xn_val = shifted_imm16; break; } default: UNREACHABLE(); } // Update the destination register. set_xreg(instr->Rd(), new_xn_val); } void Simulator::VisitConditionalSelect(Instruction* instr) { if (ConditionFailed(static_cast(instr->Condition()))) { uint64_t new_val = xreg(instr->Rm()); switch (instr->Mask(ConditionalSelectMask)) { case CSEL_w: set_wreg(instr->Rd(), new_val); break; case CSEL_x: set_xreg(instr->Rd(), new_val); break; case CSINC_w: set_wreg(instr->Rd(), new_val + 1); break; case CSINC_x: set_xreg(instr->Rd(), new_val + 1); break; case CSINV_w: set_wreg(instr->Rd(), ~new_val); break; case CSINV_x: set_xreg(instr->Rd(), ~new_val); break; case CSNEG_w: set_wreg(instr->Rd(), -new_val); break; case CSNEG_x: set_xreg(instr->Rd(), -new_val); break; default: UNIMPLEMENTED(); } } else { if (instr->SixtyFourBits()) { set_xreg(instr->Rd(), xreg(instr->Rn())); } else { set_wreg(instr->Rd(), wreg(instr->Rn())); } } } void Simulator::VisitDataProcessing1Source(Instruction* instr) { unsigned dst = instr->Rd(); unsigned src = instr->Rn(); switch (instr->Mask(DataProcessing1SourceMask)) { case RBIT_w: set_wreg(dst, ReverseBits(wreg(src), kWRegSizeInBits)); break; case RBIT_x: set_xreg(dst, ReverseBits(xreg(src), kXRegSizeInBits)); break; case REV16_w: set_wreg(dst, ReverseBytes(wreg(src), Reverse16)); break; case REV16_x: set_xreg(dst, ReverseBytes(xreg(src), Reverse16)); break; case REV_w: set_wreg(dst, ReverseBytes(wreg(src), Reverse32)); break; case REV32_x: set_xreg(dst, ReverseBytes(xreg(src), Reverse32)); break; case REV_x: set_xreg(dst, ReverseBytes(xreg(src), Reverse64)); break; case CLZ_w: set_wreg(dst, CountLeadingZeros(wreg(src), kWRegSizeInBits)); break; case CLZ_x: set_xreg(dst, CountLeadingZeros(xreg(src), kXRegSizeInBits)); break; case CLS_w: { set_wreg(dst, CountLeadingSignBits(wreg(src), kWRegSizeInBits)); break; } case CLS_x: { set_xreg(dst, CountLeadingSignBits(xreg(src), kXRegSizeInBits)); break; } default: UNIMPLEMENTED(); } } uint64_t Simulator::ReverseBits(uint64_t value, unsigned num_bits) { DCHECK((num_bits == kWRegSizeInBits) || (num_bits == kXRegSizeInBits)); uint64_t result = 0; for (unsigned i = 0; i < num_bits; i++) { result = (result << 1) | (value & 1); value >>= 1; } return result; } uint64_t Simulator::ReverseBytes(uint64_t value, ReverseByteMode mode) { // Split the 64-bit value into an 8-bit array, where b[0] is the least // significant byte, and b[7] is the most significant. uint8_t bytes[8]; uint64_t mask = 0xff00000000000000UL; for (int i = 7; i >= 0; i--) { bytes[i] = (value & mask) >> (i * 8); mask >>= 8; } // Permutation tables for REV instructions. // permute_table[Reverse16] is used by REV16_x, REV16_w // permute_table[Reverse32] is used by REV32_x, REV_w // permute_table[Reverse64] is used by REV_x DCHECK((Reverse16 == 0) && (Reverse32 == 1) && (Reverse64 == 2)); static const uint8_t permute_table[3][8] = { {6, 7, 4, 5, 2, 3, 0, 1}, {4, 5, 6, 7, 0, 1, 2, 3}, {0, 1, 2, 3, 4, 5, 6, 7} }; uint64_t result = 0; for (int i = 0; i < 8; i++) { result <<= 8; result |= bytes[permute_table[mode][i]]; } return result; } template void Simulator::DataProcessing2Source(Instruction* instr) { Shift shift_op = NO_SHIFT; T result = 0; switch (instr->Mask(DataProcessing2SourceMask)) { case SDIV_w: case SDIV_x: { T rn = reg(instr->Rn()); T rm = reg(instr->Rm()); if ((rn == std::numeric_limits::min()) && (rm == -1)) { result = std::numeric_limits::min(); } else if (rm == 0) { // Division by zero can be trapped, but not on A-class processors. result = 0; } else { result = rn / rm; } break; } case UDIV_w: case UDIV_x: { typedef typename make_unsigned::type unsignedT; unsignedT rn = static_cast(reg(instr->Rn())); unsignedT rm = static_cast(reg(instr->Rm())); if (rm == 0) { // Division by zero can be trapped, but not on A-class processors. result = 0; } else { result = rn / rm; } break; } case LSLV_w: case LSLV_x: shift_op = LSL; break; case LSRV_w: case LSRV_x: shift_op = LSR; break; case ASRV_w: case ASRV_x: shift_op = ASR; break; case RORV_w: case RORV_x: shift_op = ROR; break; default: UNIMPLEMENTED(); } if (shift_op != NO_SHIFT) { // Shift distance encoded in the least-significant five/six bits of the // register. unsigned shift = wreg(instr->Rm()); if (sizeof(T) == kWRegSize) { shift &= kShiftAmountWRegMask; } else { shift &= kShiftAmountXRegMask; } result = ShiftOperand(reg(instr->Rn()), shift_op, shift); } set_reg(instr->Rd(), result); } void Simulator::VisitDataProcessing2Source(Instruction* instr) { if (instr->SixtyFourBits()) { DataProcessing2Source(instr); } else { DataProcessing2Source(instr); } } // The algorithm used is described in section 8.2 of // Hacker's Delight, by Henry S. Warren, Jr. // It assumes that a right shift on a signed integer is an arithmetic shift. static int64_t MultiplyHighSigned(int64_t u, int64_t v) { uint64_t u0, v0, w0; int64_t u1, v1, w1, w2, t; u0 = u & 0xffffffffL; u1 = u >> 32; v0 = v & 0xffffffffL; v1 = v >> 32; w0 = u0 * v0; t = u1 * v0 + (w0 >> 32); w1 = t & 0xffffffffL; w2 = t >> 32; w1 = u0 * v1 + w1; return u1 * v1 + w2 + (w1 >> 32); } void Simulator::VisitDataProcessing3Source(Instruction* instr) { int64_t result = 0; // Extract and sign- or zero-extend 32-bit arguments for widening operations. uint64_t rn_u32 = reg(instr->Rn()); uint64_t rm_u32 = reg(instr->Rm()); int64_t rn_s32 = reg(instr->Rn()); int64_t rm_s32 = reg(instr->Rm()); switch (instr->Mask(DataProcessing3SourceMask)) { case MADD_w: case MADD_x: result = xreg(instr->Ra()) + (xreg(instr->Rn()) * xreg(instr->Rm())); break; case MSUB_w: case MSUB_x: result = xreg(instr->Ra()) - (xreg(instr->Rn()) * xreg(instr->Rm())); break; case SMADDL_x: result = xreg(instr->Ra()) + (rn_s32 * rm_s32); break; case SMSUBL_x: result = xreg(instr->Ra()) - (rn_s32 * rm_s32); break; case UMADDL_x: result = xreg(instr->Ra()) + (rn_u32 * rm_u32); break; case UMSUBL_x: result = xreg(instr->Ra()) - (rn_u32 * rm_u32); break; case SMULH_x: DCHECK(instr->Ra() == kZeroRegCode); result = MultiplyHighSigned(xreg(instr->Rn()), xreg(instr->Rm())); break; default: UNIMPLEMENTED(); } if (instr->SixtyFourBits()) { set_xreg(instr->Rd(), result); } else { set_wreg(instr->Rd(), result); } } template void Simulator::BitfieldHelper(Instruction* instr) { typedef typename make_unsigned::type unsignedT; T reg_size = sizeof(T) * 8; T R = instr->ImmR(); T S = instr->ImmS(); T diff = S - R; T mask; if (diff >= 0) { mask = diff < reg_size - 1 ? (static_cast(1) << (diff + 1)) - 1 : static_cast(-1); } else { mask = ((1L << (S + 1)) - 1); mask = (static_cast(mask) >> R) | (mask << (reg_size - R)); diff += reg_size; } // inzero indicates if the extracted bitfield is inserted into the // destination register value or in zero. // If extend is true, extend the sign of the extracted bitfield. bool inzero = false; bool extend = false; switch (instr->Mask(BitfieldMask)) { case BFM_x: case BFM_w: break; case SBFM_x: case SBFM_w: inzero = true; extend = true; break; case UBFM_x: case UBFM_w: inzero = true; break; default: UNIMPLEMENTED(); } T dst = inzero ? 0 : reg(instr->Rd()); T src = reg(instr->Rn()); // Rotate source bitfield into place. T result = (static_cast(src) >> R) | (src << (reg_size - R)); // Determine the sign extension. T topbits_preshift = (static_cast(1) << (reg_size - diff - 1)) - 1; T signbits = (extend && ((src >> S) & 1) ? topbits_preshift : 0) << (diff + 1); // Merge sign extension, dest/zero and bitfield. result = signbits | (result & mask) | (dst & ~mask); set_reg(instr->Rd(), result); } void Simulator::VisitBitfield(Instruction* instr) { if (instr->SixtyFourBits()) { BitfieldHelper(instr); } else { BitfieldHelper(instr); } } void Simulator::VisitExtract(Instruction* instr) { if (instr->SixtyFourBits()) { Extract(instr); } else { Extract(instr); } } void Simulator::VisitFPImmediate(Instruction* instr) { AssertSupportedFPCR(); unsigned dest = instr->Rd(); switch (instr->Mask(FPImmediateMask)) { case FMOV_s_imm: set_sreg(dest, instr->ImmFP32()); break; case FMOV_d_imm: set_dreg(dest, instr->ImmFP64()); break; default: UNREACHABLE(); } } void Simulator::VisitFPIntegerConvert(Instruction* instr) { AssertSupportedFPCR(); unsigned dst = instr->Rd(); unsigned src = instr->Rn(); FPRounding round = fpcr().RMode(); switch (instr->Mask(FPIntegerConvertMask)) { case FCVTAS_ws: set_wreg(dst, FPToInt32(sreg(src), FPTieAway)); break; case FCVTAS_xs: set_xreg(dst, FPToInt64(sreg(src), FPTieAway)); break; case FCVTAS_wd: set_wreg(dst, FPToInt32(dreg(src), FPTieAway)); break; case FCVTAS_xd: set_xreg(dst, FPToInt64(dreg(src), FPTieAway)); break; case FCVTAU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPTieAway)); break; case FCVTAU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPTieAway)); break; case FCVTAU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPTieAway)); break; case FCVTAU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPTieAway)); break; case FCVTMS_ws: set_wreg(dst, FPToInt32(sreg(src), FPNegativeInfinity)); break; case FCVTMS_xs: set_xreg(dst, FPToInt64(sreg(src), FPNegativeInfinity)); break; case FCVTMS_wd: set_wreg(dst, FPToInt32(dreg(src), FPNegativeInfinity)); break; case FCVTMS_xd: set_xreg(dst, FPToInt64(dreg(src), FPNegativeInfinity)); break; case FCVTMU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPNegativeInfinity)); break; case FCVTMU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPNegativeInfinity)); break; case FCVTMU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPNegativeInfinity)); break; case FCVTMU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPNegativeInfinity)); break; case FCVTNS_ws: set_wreg(dst, FPToInt32(sreg(src), FPTieEven)); break; case FCVTNS_xs: set_xreg(dst, FPToInt64(sreg(src), FPTieEven)); break; case FCVTNS_wd: set_wreg(dst, FPToInt32(dreg(src), FPTieEven)); break; case FCVTNS_xd: set_xreg(dst, FPToInt64(dreg(src), FPTieEven)); break; case FCVTNU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPTieEven)); break; case FCVTNU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPTieEven)); break; case FCVTNU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPTieEven)); break; case FCVTNU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPTieEven)); break; case FCVTZS_ws: set_wreg(dst, FPToInt32(sreg(src), FPZero)); break; case FCVTZS_xs: set_xreg(dst, FPToInt64(sreg(src), FPZero)); break; case FCVTZS_wd: set_wreg(dst, FPToInt32(dreg(src), FPZero)); break; case FCVTZS_xd: set_xreg(dst, FPToInt64(dreg(src), FPZero)); break; case FCVTZU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPZero)); break; case FCVTZU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPZero)); break; case FCVTZU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPZero)); break; case FCVTZU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPZero)); break; case FMOV_ws: set_wreg(dst, sreg_bits(src)); break; case FMOV_xd: set_xreg(dst, dreg_bits(src)); break; case FMOV_sw: set_sreg_bits(dst, wreg(src)); break; case FMOV_dx: set_dreg_bits(dst, xreg(src)); break; // A 32-bit input can be handled in the same way as a 64-bit input, since // the sign- or zero-extension will not affect the conversion. case SCVTF_dx: set_dreg(dst, FixedToDouble(xreg(src), 0, round)); break; case SCVTF_dw: set_dreg(dst, FixedToDouble(wreg(src), 0, round)); break; case UCVTF_dx: set_dreg(dst, UFixedToDouble(xreg(src), 0, round)); break; case UCVTF_dw: { set_dreg(dst, UFixedToDouble(reg(src), 0, round)); break; } case SCVTF_sx: set_sreg(dst, FixedToFloat(xreg(src), 0, round)); break; case SCVTF_sw: set_sreg(dst, FixedToFloat(wreg(src), 0, round)); break; case UCVTF_sx: set_sreg(dst, UFixedToFloat(xreg(src), 0, round)); break; case UCVTF_sw: { set_sreg(dst, UFixedToFloat(reg(src), 0, round)); break; } default: UNREACHABLE(); } } void Simulator::VisitFPFixedPointConvert(Instruction* instr) { AssertSupportedFPCR(); unsigned dst = instr->Rd(); unsigned src = instr->Rn(); int fbits = 64 - instr->FPScale(); FPRounding round = fpcr().RMode(); switch (instr->Mask(FPFixedPointConvertMask)) { // A 32-bit input can be handled in the same way as a 64-bit input, since // the sign- or zero-extension will not affect the conversion. case SCVTF_dx_fixed: set_dreg(dst, FixedToDouble(xreg(src), fbits, round)); break; case SCVTF_dw_fixed: set_dreg(dst, FixedToDouble(wreg(src), fbits, round)); break; case UCVTF_dx_fixed: set_dreg(dst, UFixedToDouble(xreg(src), fbits, round)); break; case UCVTF_dw_fixed: { set_dreg(dst, UFixedToDouble(reg(src), fbits, round)); break; } case SCVTF_sx_fixed: set_sreg(dst, FixedToFloat(xreg(src), fbits, round)); break; case SCVTF_sw_fixed: set_sreg(dst, FixedToFloat(wreg(src), fbits, round)); break; case UCVTF_sx_fixed: set_sreg(dst, UFixedToFloat(xreg(src), fbits, round)); break; case UCVTF_sw_fixed: { set_sreg(dst, UFixedToFloat(reg(src), fbits, round)); break; } default: UNREACHABLE(); } } int32_t Simulator::FPToInt32(double value, FPRounding rmode) { value = FPRoundInt(value, rmode); if (value >= kWMaxInt) { return kWMaxInt; } else if (value < kWMinInt) { return kWMinInt; } return std::isnan(value) ? 0 : static_cast(value); } int64_t Simulator::FPToInt64(double value, FPRounding rmode) { value = FPRoundInt(value, rmode); if (value >= kXMaxInt) { return kXMaxInt; } else if (value < kXMinInt) { return kXMinInt; } return std::isnan(value) ? 0 : static_cast(value); } uint32_t Simulator::FPToUInt32(double value, FPRounding rmode) { value = FPRoundInt(value, rmode); if (value >= kWMaxUInt) { return kWMaxUInt; } else if (value < 0.0) { return 0; } return std::isnan(value) ? 0 : static_cast(value); } uint64_t Simulator::FPToUInt64(double value, FPRounding rmode) { value = FPRoundInt(value, rmode); if (value >= kXMaxUInt) { return kXMaxUInt; } else if (value < 0.0) { return 0; } return std::isnan(value) ? 0 : static_cast(value); } void Simulator::VisitFPCompare(Instruction* instr) { AssertSupportedFPCR(); unsigned reg_size = (instr->Mask(FP64) == FP64) ? kDRegSizeInBits : kSRegSizeInBits; double fn_val = fpreg(reg_size, instr->Rn()); switch (instr->Mask(FPCompareMask)) { case FCMP_s: case FCMP_d: FPCompare(fn_val, fpreg(reg_size, instr->Rm())); break; case FCMP_s_zero: case FCMP_d_zero: FPCompare(fn_val, 0.0); break; default: UNIMPLEMENTED(); } } void Simulator::VisitFPConditionalCompare(Instruction* instr) { AssertSupportedFPCR(); switch (instr->Mask(FPConditionalCompareMask)) { case FCCMP_s: case FCCMP_d: { if (ConditionPassed(static_cast(instr->Condition()))) { // If the condition passes, set the status flags to the result of // comparing the operands. unsigned reg_size = (instr->Mask(FP64) == FP64) ? kDRegSizeInBits : kSRegSizeInBits; FPCompare(fpreg(reg_size, instr->Rn()), fpreg(reg_size, instr->Rm())); } else { // If the condition fails, set the status flags to the nzcv immediate. nzcv().SetFlags(instr->Nzcv()); LogSystemRegister(NZCV); } break; } default: UNIMPLEMENTED(); } } void Simulator::VisitFPConditionalSelect(Instruction* instr) { AssertSupportedFPCR(); Instr selected; if (ConditionPassed(static_cast(instr->Condition()))) { selected = instr->Rn(); } else { selected = instr->Rm(); } switch (instr->Mask(FPConditionalSelectMask)) { case FCSEL_s: set_sreg(instr->Rd(), sreg(selected)); break; case FCSEL_d: set_dreg(instr->Rd(), dreg(selected)); break; default: UNIMPLEMENTED(); } } void Simulator::VisitFPDataProcessing1Source(Instruction* instr) { AssertSupportedFPCR(); unsigned fd = instr->Rd(); unsigned fn = instr->Rn(); switch (instr->Mask(FPDataProcessing1SourceMask)) { case FMOV_s: set_sreg(fd, sreg(fn)); break; case FMOV_d: set_dreg(fd, dreg(fn)); break; case FABS_s: set_sreg(fd, std::fabs(sreg(fn))); break; case FABS_d: set_dreg(fd, std::fabs(dreg(fn))); break; case FNEG_s: set_sreg(fd, -sreg(fn)); break; case FNEG_d: set_dreg(fd, -dreg(fn)); break; case FSQRT_s: set_sreg(fd, FPSqrt(sreg(fn))); break; case FSQRT_d: set_dreg(fd, FPSqrt(dreg(fn))); break; case FRINTA_s: set_sreg(fd, FPRoundInt(sreg(fn), FPTieAway)); break; case FRINTA_d: set_dreg(fd, FPRoundInt(dreg(fn), FPTieAway)); break; case FRINTM_s: set_sreg(fd, FPRoundInt(sreg(fn), FPNegativeInfinity)); break; case FRINTM_d: set_dreg(fd, FPRoundInt(dreg(fn), FPNegativeInfinity)); break; case FRINTN_s: set_sreg(fd, FPRoundInt(sreg(fn), FPTieEven)); break; case FRINTN_d: set_dreg(fd, FPRoundInt(dreg(fn), FPTieEven)); break; case FRINTZ_s: set_sreg(fd, FPRoundInt(sreg(fn), FPZero)); break; case FRINTZ_d: set_dreg(fd, FPRoundInt(dreg(fn), FPZero)); break; case FCVT_ds: set_dreg(fd, FPToDouble(sreg(fn))); break; case FCVT_sd: set_sreg(fd, FPToFloat(dreg(fn), FPTieEven)); break; default: UNIMPLEMENTED(); } } // Assemble the specified IEEE-754 components into the target type and apply // appropriate rounding. // sign: 0 = positive, 1 = negative // exponent: Unbiased IEEE-754 exponent. // mantissa: The mantissa of the input. The top bit (which is not encoded for // normal IEEE-754 values) must not be omitted. This bit has the // value 'pow(2, exponent)'. // // The input value is assumed to be a normalized value. That is, the input may // not be infinity or NaN. If the source value is subnormal, it must be // normalized before calling this function such that the highest set bit in the // mantissa has the value 'pow(2, exponent)'. // // Callers should use FPRoundToFloat or FPRoundToDouble directly, rather than // calling a templated FPRound. template static T FPRound(int64_t sign, int64_t exponent, uint64_t mantissa, FPRounding round_mode) { DCHECK((sign == 0) || (sign == 1)); // Only the FPTieEven rounding mode is implemented. DCHECK(round_mode == FPTieEven); USE(round_mode); // Rounding can promote subnormals to normals, and normals to infinities. For // example, a double with exponent 127 (FLT_MAX_EXP) would appear to be // encodable as a float, but rounding based on the low-order mantissa bits // could make it overflow. With ties-to-even rounding, this value would become // an infinity. // ---- Rounding Method ---- // // The exponent is irrelevant in the rounding operation, so we treat the // lowest-order bit that will fit into the result ('onebit') as having // the value '1'. Similarly, the highest-order bit that won't fit into // the result ('halfbit') has the value '0.5'. The 'point' sits between // 'onebit' and 'halfbit': // // These bits fit into the result. // |---------------------| // mantissa = 0bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // || // / | // / halfbit // onebit // // For subnormal outputs, the range of representable bits is smaller and // the position of onebit and halfbit depends on the exponent of the // input, but the method is otherwise similar. // // onebit(frac) // | // | halfbit(frac) halfbit(adjusted) // | / / // | | | // 0b00.0 (exact) -> 0b00.0 (exact) -> 0b00 // 0b00.0... -> 0b00.0... -> 0b00 // 0b00.1 (exact) -> 0b00.0111..111 -> 0b00 // 0b00.1... -> 0b00.1... -> 0b01 // 0b01.0 (exact) -> 0b01.0 (exact) -> 0b01 // 0b01.0... -> 0b01.0... -> 0b01 // 0b01.1 (exact) -> 0b01.1 (exact) -> 0b10 // 0b01.1... -> 0b01.1... -> 0b10 // 0b10.0 (exact) -> 0b10.0 (exact) -> 0b10 // 0b10.0... -> 0b10.0... -> 0b10 // 0b10.1 (exact) -> 0b10.0111..111 -> 0b10 // 0b10.1... -> 0b10.1... -> 0b11 // 0b11.0 (exact) -> 0b11.0 (exact) -> 0b11 // ... / | / | // / | / | // / | // adjusted = frac - (halfbit(mantissa) & ~onebit(frac)); / | // // mantissa = (mantissa >> shift) + halfbit(adjusted); static const int mantissa_offset = 0; static const int exponent_offset = mantissa_offset + mbits; static const int sign_offset = exponent_offset + ebits; STATIC_ASSERT(sign_offset == (sizeof(T) * kByteSize - 1)); // Bail out early for zero inputs. if (mantissa == 0) { return sign << sign_offset; } // If all bits in the exponent are set, the value is infinite or NaN. // This is true for all binary IEEE-754 formats. static const int infinite_exponent = (1 << ebits) - 1; static const int max_normal_exponent = infinite_exponent - 1; // Apply the exponent bias to encode it for the result. Doing this early makes // it easy to detect values that will be infinite or subnormal. exponent += max_normal_exponent >> 1; if (exponent > max_normal_exponent) { // Overflow: The input is too large for the result type to represent. The // FPTieEven rounding mode handles overflows using infinities. exponent = infinite_exponent; mantissa = 0; return (sign << sign_offset) | (exponent << exponent_offset) | (mantissa << mantissa_offset); } // Calculate the shift required to move the top mantissa bit to the proper // place in the destination type. const int highest_significant_bit = 63 - CountLeadingZeros(mantissa, 64); int shift = highest_significant_bit - mbits; if (exponent <= 0) { // The output will be subnormal (before rounding). // For subnormal outputs, the shift must be adjusted by the exponent. The +1 // is necessary because the exponent of a subnormal value (encoded as 0) is // the same as the exponent of the smallest normal value (encoded as 1). shift += -exponent + 1; // Handle inputs that would produce a zero output. // // Shifts higher than highest_significant_bit+1 will always produce a zero // result. A shift of exactly highest_significant_bit+1 might produce a // non-zero result after rounding. if (shift > (highest_significant_bit + 1)) { // The result will always be +/-0.0. return sign << sign_offset; } // Properly encode the exponent for a subnormal output. exponent = 0; } else { // Clear the topmost mantissa bit, since this is not encoded in IEEE-754 // normal values. mantissa &= ~(1UL << highest_significant_bit); } if (shift > 0) { // We have to shift the mantissa to the right. Some precision is lost, so we // need to apply rounding. uint64_t onebit_mantissa = (mantissa >> (shift)) & 1; uint64_t halfbit_mantissa = (mantissa >> (shift-1)) & 1; uint64_t adjusted = mantissa - (halfbit_mantissa & ~onebit_mantissa); T halfbit_adjusted = (adjusted >> (shift-1)) & 1; T result = (sign << sign_offset) | (exponent << exponent_offset) | ((mantissa >> shift) << mantissa_offset); // A very large mantissa can overflow during rounding. If this happens, the // exponent should be incremented and the mantissa set to 1.0 (encoded as // 0). Applying halfbit_adjusted after assembling the float has the nice // side-effect that this case is handled for free. // // This also handles cases where a very large finite value overflows to // infinity, or where a very large subnormal value overflows to become // normal. return result + halfbit_adjusted; } else { // We have to shift the mantissa to the left (or not at all). The input // mantissa is exactly representable in the output mantissa, so apply no // rounding correction. return (sign << sign_offset) | (exponent << exponent_offset) | ((mantissa << -shift) << mantissa_offset); } } // See FPRound for a description of this function. static inline double FPRoundToDouble(int64_t sign, int64_t exponent, uint64_t mantissa, FPRounding round_mode) { int64_t bits = FPRound(sign, exponent, mantissa, round_mode); return rawbits_to_double(bits); } // See FPRound for a description of this function. static inline float FPRoundToFloat(int64_t sign, int64_t exponent, uint64_t mantissa, FPRounding round_mode) { int32_t bits = FPRound(sign, exponent, mantissa, round_mode); return rawbits_to_float(bits); } double Simulator::FixedToDouble(int64_t src, int fbits, FPRounding round) { if (src >= 0) { return UFixedToDouble(src, fbits, round); } else { // This works for all negative values, including INT64_MIN. return -UFixedToDouble(-src, fbits, round); } } double Simulator::UFixedToDouble(uint64_t src, int fbits, FPRounding round) { // An input of 0 is a special case because the result is effectively // subnormal: The exponent is encoded as 0 and there is no implicit 1 bit. if (src == 0) { return 0.0; } // Calculate the exponent. The highest significant bit will have the value // 2^exponent. const int highest_significant_bit = 63 - CountLeadingZeros(src, 64); const int64_t exponent = highest_significant_bit - fbits; return FPRoundToDouble(0, exponent, src, round); } float Simulator::FixedToFloat(int64_t src, int fbits, FPRounding round) { if (src >= 0) { return UFixedToFloat(src, fbits, round); } else { // This works for all negative values, including INT64_MIN. return -UFixedToFloat(-src, fbits, round); } } float Simulator::UFixedToFloat(uint64_t src, int fbits, FPRounding round) { // An input of 0 is a special case because the result is effectively // subnormal: The exponent is encoded as 0 and there is no implicit 1 bit. if (src == 0) { return 0.0f; } // Calculate the exponent. The highest significant bit will have the value // 2^exponent. const int highest_significant_bit = 63 - CountLeadingZeros(src, 64); const int32_t exponent = highest_significant_bit - fbits; return FPRoundToFloat(0, exponent, src, round); } double Simulator::FPRoundInt(double value, FPRounding round_mode) { if ((value == 0.0) || (value == kFP64PositiveInfinity) || (value == kFP64NegativeInfinity)) { return value; } else if (std::isnan(value)) { return FPProcessNaN(value); } double int_result = floor(value); double error = value - int_result; switch (round_mode) { case FPTieAway: { // Take care of correctly handling the range ]-0.5, -0.0], which must // yield -0.0. if ((-0.5 < value) && (value < 0.0)) { int_result = -0.0; } else if ((error > 0.5) || ((error == 0.5) && (int_result >= 0.0))) { // If the error is greater than 0.5, or is equal to 0.5 and the integer // result is positive, round up. int_result++; } break; } case FPTieEven: { // Take care of correctly handling the range [-0.5, -0.0], which must // yield -0.0. if ((-0.5 <= value) && (value < 0.0)) { int_result = -0.0; // If the error is greater than 0.5, or is equal to 0.5 and the integer // result is odd, round up. } else if ((error > 0.5) || ((error == 0.5) && (fmod(int_result, 2) != 0))) { int_result++; } break; } case FPZero: { // If value > 0 then we take floor(value) // otherwise, ceil(value) if (value < 0) { int_result = ceil(value); } break; } case FPNegativeInfinity: { // We always use floor(value). break; } default: UNIMPLEMENTED(); } return int_result; } double Simulator::FPToDouble(float value) { switch (std::fpclassify(value)) { case FP_NAN: { if (fpcr().DN()) return kFP64DefaultNaN; // Convert NaNs as the processor would: // - The sign is propagated. // - The payload (mantissa) is transferred entirely, except that the top // bit is forced to '1', making the result a quiet NaN. The unused // (low-order) payload bits are set to 0. uint32_t raw = float_to_rawbits(value); uint64_t sign = raw >> 31; uint64_t exponent = (1 << 11) - 1; uint64_t payload = unsigned_bitextract_64(21, 0, raw); payload <<= (52 - 23); // The unused low-order bits should be 0. payload |= (1L << 51); // Force a quiet NaN. return rawbits_to_double((sign << 63) | (exponent << 52) | payload); } case FP_ZERO: case FP_NORMAL: case FP_SUBNORMAL: case FP_INFINITE: { // All other inputs are preserved in a standard cast, because every value // representable using an IEEE-754 float is also representable using an // IEEE-754 double. return static_cast(value); } } UNREACHABLE(); return static_cast(value); } float Simulator::FPToFloat(double value, FPRounding round_mode) { // Only the FPTieEven rounding mode is implemented. DCHECK(round_mode == FPTieEven); USE(round_mode); switch (std::fpclassify(value)) { case FP_NAN: { if (fpcr().DN()) return kFP32DefaultNaN; // Convert NaNs as the processor would: // - The sign is propagated. // - The payload (mantissa) is transferred as much as possible, except // that the top bit is forced to '1', making the result a quiet NaN. uint64_t raw = double_to_rawbits(value); uint32_t sign = raw >> 63; uint32_t exponent = (1 << 8) - 1; uint32_t payload = unsigned_bitextract_64(50, 52 - 23, raw); payload |= (1 << 22); // Force a quiet NaN. return rawbits_to_float((sign << 31) | (exponent << 23) | payload); } case FP_ZERO: case FP_INFINITE: { // In a C++ cast, any value representable in the target type will be // unchanged. This is always the case for +/-0.0 and infinities. return static_cast(value); } case FP_NORMAL: case FP_SUBNORMAL: { // Convert double-to-float as the processor would, assuming that FPCR.FZ // (flush-to-zero) is not set. uint64_t raw = double_to_rawbits(value); // Extract the IEEE-754 double components. uint32_t sign = raw >> 63; // Extract the exponent and remove the IEEE-754 encoding bias. int32_t exponent = unsigned_bitextract_64(62, 52, raw) - 1023; // Extract the mantissa and add the implicit '1' bit. uint64_t mantissa = unsigned_bitextract_64(51, 0, raw); if (std::fpclassify(value) == FP_NORMAL) { mantissa |= (1UL << 52); } return FPRoundToFloat(sign, exponent, mantissa, round_mode); } } UNREACHABLE(); return value; } void Simulator::VisitFPDataProcessing2Source(Instruction* instr) { AssertSupportedFPCR(); unsigned fd = instr->Rd(); unsigned fn = instr->Rn(); unsigned fm = instr->Rm(); // Fmaxnm and Fminnm have special NaN handling. switch (instr->Mask(FPDataProcessing2SourceMask)) { case FMAXNM_s: set_sreg(fd, FPMaxNM(sreg(fn), sreg(fm))); return; case FMAXNM_d: set_dreg(fd, FPMaxNM(dreg(fn), dreg(fm))); return; case FMINNM_s: set_sreg(fd, FPMinNM(sreg(fn), sreg(fm))); return; case FMINNM_d: set_dreg(fd, FPMinNM(dreg(fn), dreg(fm))); return; default: break; // Fall through. } if (FPProcessNaNs(instr)) return; switch (instr->Mask(FPDataProcessing2SourceMask)) { case FADD_s: set_sreg(fd, FPAdd(sreg(fn), sreg(fm))); break; case FADD_d: set_dreg(fd, FPAdd(dreg(fn), dreg(fm))); break; case FSUB_s: set_sreg(fd, FPSub(sreg(fn), sreg(fm))); break; case FSUB_d: set_dreg(fd, FPSub(dreg(fn), dreg(fm))); break; case FMUL_s: set_sreg(fd, FPMul(sreg(fn), sreg(fm))); break; case FMUL_d: set_dreg(fd, FPMul(dreg(fn), dreg(fm))); break; case FDIV_s: set_sreg(fd, FPDiv(sreg(fn), sreg(fm))); break; case FDIV_d: set_dreg(fd, FPDiv(dreg(fn), dreg(fm))); break; case FMAX_s: set_sreg(fd, FPMax(sreg(fn), sreg(fm))); break; case FMAX_d: set_dreg(fd, FPMax(dreg(fn), dreg(fm))); break; case FMIN_s: set_sreg(fd, FPMin(sreg(fn), sreg(fm))); break; case FMIN_d: set_dreg(fd, FPMin(dreg(fn), dreg(fm))); break; case FMAXNM_s: case FMAXNM_d: case FMINNM_s: case FMINNM_d: // These were handled before the standard FPProcessNaNs() stage. UNREACHABLE(); default: UNIMPLEMENTED(); } } void Simulator::VisitFPDataProcessing3Source(Instruction* instr) { AssertSupportedFPCR(); unsigned fd = instr->Rd(); unsigned fn = instr->Rn(); unsigned fm = instr->Rm(); unsigned fa = instr->Ra(); switch (instr->Mask(FPDataProcessing3SourceMask)) { // fd = fa +/- (fn * fm) case FMADD_s: set_sreg(fd, FPMulAdd(sreg(fa), sreg(fn), sreg(fm))); break; case FMSUB_s: set_sreg(fd, FPMulAdd(sreg(fa), -sreg(fn), sreg(fm))); break; case FMADD_d: set_dreg(fd, FPMulAdd(dreg(fa), dreg(fn), dreg(fm))); break; case FMSUB_d: set_dreg(fd, FPMulAdd(dreg(fa), -dreg(fn), dreg(fm))); break; // Negated variants of the above. case FNMADD_s: set_sreg(fd, FPMulAdd(-sreg(fa), -sreg(fn), sreg(fm))); break; case FNMSUB_s: set_sreg(fd, FPMulAdd(-sreg(fa), sreg(fn), sreg(fm))); break; case FNMADD_d: set_dreg(fd, FPMulAdd(-dreg(fa), -dreg(fn), dreg(fm))); break; case FNMSUB_d: set_dreg(fd, FPMulAdd(-dreg(fa), dreg(fn), dreg(fm))); break; default: UNIMPLEMENTED(); } } template T Simulator::FPAdd(T op1, T op2) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(op1) && !std::isnan(op2)); if (std::isinf(op1) && std::isinf(op2) && (op1 != op2)) { // inf + -inf returns the default NaN. return FPDefaultNaN(); } else { // Other cases should be handled by standard arithmetic. return op1 + op2; } } template T Simulator::FPDiv(T op1, T op2) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(op1) && !std::isnan(op2)); if ((std::isinf(op1) && std::isinf(op2)) || ((op1 == 0.0) && (op2 == 0.0))) { // inf / inf and 0.0 / 0.0 return the default NaN. return FPDefaultNaN(); } else { // Other cases should be handled by standard arithmetic. return op1 / op2; } } template T Simulator::FPMax(T a, T b) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(a) && !std::isnan(b)); if ((a == 0.0) && (b == 0.0) && (copysign(1.0, a) != copysign(1.0, b))) { // a and b are zero, and the sign differs: return +0.0. return 0.0; } else { return (a > b) ? a : b; } } template T Simulator::FPMaxNM(T a, T b) { if (IsQuietNaN(a) && !IsQuietNaN(b)) { a = kFP64NegativeInfinity; } else if (!IsQuietNaN(a) && IsQuietNaN(b)) { b = kFP64NegativeInfinity; } T result = FPProcessNaNs(a, b); return std::isnan(result) ? result : FPMax(a, b); } template T Simulator::FPMin(T a, T b) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(a) && !std::isnan(b)); if ((a == 0.0) && (b == 0.0) && (copysign(1.0, a) != copysign(1.0, b))) { // a and b are zero, and the sign differs: return -0.0. return -0.0; } else { return (a < b) ? a : b; } } template T Simulator::FPMinNM(T a, T b) { if (IsQuietNaN(a) && !IsQuietNaN(b)) { a = kFP64PositiveInfinity; } else if (!IsQuietNaN(a) && IsQuietNaN(b)) { b = kFP64PositiveInfinity; } T result = FPProcessNaNs(a, b); return std::isnan(result) ? result : FPMin(a, b); } template T Simulator::FPMul(T op1, T op2) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(op1) && !std::isnan(op2)); if ((std::isinf(op1) && (op2 == 0.0)) || (std::isinf(op2) && (op1 == 0.0))) { // inf * 0.0 returns the default NaN. return FPDefaultNaN(); } else { // Other cases should be handled by standard arithmetic. return op1 * op2; } } template T Simulator::FPMulAdd(T a, T op1, T op2) { T result = FPProcessNaNs3(a, op1, op2); T sign_a = copysign(1.0, a); T sign_prod = copysign(1.0, op1) * copysign(1.0, op2); bool isinf_prod = std::isinf(op1) || std::isinf(op2); bool operation_generates_nan = (std::isinf(op1) && (op2 == 0.0)) || // inf * 0.0 (std::isinf(op2) && (op1 == 0.0)) || // 0.0 * inf (std::isinf(a) && isinf_prod && (sign_a != sign_prod)); // inf - inf if (std::isnan(result)) { // Generated NaNs override quiet NaNs propagated from a. if (operation_generates_nan && IsQuietNaN(a)) { return FPDefaultNaN(); } else { return result; } } // If the operation would produce a NaN, return the default NaN. if (operation_generates_nan) { return FPDefaultNaN(); } // Work around broken fma implementations for exact zero results: The sign of // exact 0.0 results is positive unless both a and op1 * op2 are negative. if (((op1 == 0.0) || (op2 == 0.0)) && (a == 0.0)) { return ((sign_a < 0) && (sign_prod < 0)) ? -0.0 : 0.0; } result = FusedMultiplyAdd(op1, op2, a); DCHECK(!std::isnan(result)); // Work around broken fma implementations for rounded zero results: If a is // 0.0, the sign of the result is the sign of op1 * op2 before rounding. if ((a == 0.0) && (result == 0.0)) { return copysign(0.0, sign_prod); } return result; } template T Simulator::FPSqrt(T op) { if (std::isnan(op)) { return FPProcessNaN(op); } else if (op < 0.0) { return FPDefaultNaN(); } else { return std::sqrt(op); } } template T Simulator::FPSub(T op1, T op2) { // NaNs should be handled elsewhere. DCHECK(!std::isnan(op1) && !std::isnan(op2)); if (std::isinf(op1) && std::isinf(op2) && (op1 == op2)) { // inf - inf returns the default NaN. return FPDefaultNaN(); } else { // Other cases should be handled by standard arithmetic. return op1 - op2; } } template T Simulator::FPProcessNaN(T op) { DCHECK(std::isnan(op)); return fpcr().DN() ? FPDefaultNaN() : ToQuietNaN(op); } template T Simulator::FPProcessNaNs(T op1, T op2) { if (IsSignallingNaN(op1)) { return FPProcessNaN(op1); } else if (IsSignallingNaN(op2)) { return FPProcessNaN(op2); } else if (std::isnan(op1)) { DCHECK(IsQuietNaN(op1)); return FPProcessNaN(op1); } else if (std::isnan(op2)) { DCHECK(IsQuietNaN(op2)); return FPProcessNaN(op2); } else { return 0.0; } } template T Simulator::FPProcessNaNs3(T op1, T op2, T op3) { if (IsSignallingNaN(op1)) { return FPProcessNaN(op1); } else if (IsSignallingNaN(op2)) { return FPProcessNaN(op2); } else if (IsSignallingNaN(op3)) { return FPProcessNaN(op3); } else if (std::isnan(op1)) { DCHECK(IsQuietNaN(op1)); return FPProcessNaN(op1); } else if (std::isnan(op2)) { DCHECK(IsQuietNaN(op2)); return FPProcessNaN(op2); } else if (std::isnan(op3)) { DCHECK(IsQuietNaN(op3)); return FPProcessNaN(op3); } else { return 0.0; } } bool Simulator::FPProcessNaNs(Instruction* instr) { unsigned fd = instr->Rd(); unsigned fn = instr->Rn(); unsigned fm = instr->Rm(); bool done = false; if (instr->Mask(FP64) == FP64) { double result = FPProcessNaNs(dreg(fn), dreg(fm)); if (std::isnan(result)) { set_dreg(fd, result); done = true; } } else { float result = FPProcessNaNs(sreg(fn), sreg(fm)); if (std::isnan(result)) { set_sreg(fd, result); done = true; } } return done; } void Simulator::VisitSystem(Instruction* instr) { // Some system instructions hijack their Op and Cp fields to represent a // range of immediates instead of indicating a different instruction. This // makes the decoding tricky. if (instr->Mask(SystemSysRegFMask) == SystemSysRegFixed) { switch (instr->Mask(SystemSysRegMask)) { case MRS: { switch (instr->ImmSystemRegister()) { case NZCV: set_xreg(instr->Rt(), nzcv().RawValue()); break; case FPCR: set_xreg(instr->Rt(), fpcr().RawValue()); break; default: UNIMPLEMENTED(); } break; } case MSR: { switch (instr->ImmSystemRegister()) { case NZCV: nzcv().SetRawValue(xreg(instr->Rt())); LogSystemRegister(NZCV); break; case FPCR: fpcr().SetRawValue(xreg(instr->Rt())); LogSystemRegister(FPCR); break; default: UNIMPLEMENTED(); } break; } } } else if (instr->Mask(SystemHintFMask) == SystemHintFixed) { DCHECK(instr->Mask(SystemHintMask) == HINT); switch (instr->ImmHint()) { case NOP: break; default: UNIMPLEMENTED(); } } else if (instr->Mask(MemBarrierFMask) == MemBarrierFixed) { __sync_synchronize(); } else { UNIMPLEMENTED(); } } bool Simulator::GetValue(const char* desc, int64_t* value) { int regnum = CodeFromName(desc); if (regnum >= 0) { unsigned code = regnum; if (code == kZeroRegCode) { // Catch the zero register and return 0. *value = 0; return true; } else if (code == kSPRegInternalCode) { // Translate the stack pointer code to 31, for Reg31IsStackPointer. code = 31; } if (desc[0] == 'w') { *value = wreg(code, Reg31IsStackPointer); } else { *value = xreg(code, Reg31IsStackPointer); } return true; } else if (strncmp(desc, "0x", 2) == 0) { return SScanF(desc + 2, "%" SCNx64, reinterpret_cast(value)) == 1; } else { return SScanF(desc, "%" SCNu64, reinterpret_cast(value)) == 1; } } bool Simulator::PrintValue(const char* desc) { if (strcmp(desc, "csp") == 0) { DCHECK(CodeFromName(desc) == static_cast(kSPRegInternalCode)); PrintF(stream_, "%s csp:%s 0x%016" PRIx64 "%s\n", clr_reg_name, clr_reg_value, xreg(31, Reg31IsStackPointer), clr_normal); return true; } else if (strcmp(desc, "wcsp") == 0) { DCHECK(CodeFromName(desc) == static_cast(kSPRegInternalCode)); PrintF(stream_, "%s wcsp:%s 0x%08" PRIx32 "%s\n", clr_reg_name, clr_reg_value, wreg(31, Reg31IsStackPointer), clr_normal); return true; } int i = CodeFromName(desc); STATIC_ASSERT(kNumberOfRegisters == kNumberOfFPRegisters); if (i < 0 || static_cast(i) >= kNumberOfFPRegisters) return false; if (desc[0] == 'v') { PrintF(stream_, "%s %s:%s 0x%016" PRIx64 "%s (%s%s:%s %g%s %s:%s %g%s)\n", clr_fpreg_name, VRegNameForCode(i), clr_fpreg_value, double_to_rawbits(dreg(i)), clr_normal, clr_fpreg_name, DRegNameForCode(i), clr_fpreg_value, dreg(i), clr_fpreg_name, SRegNameForCode(i), clr_fpreg_value, sreg(i), clr_normal); return true; } else if (desc[0] == 'd') { PrintF(stream_, "%s %s:%s %g%s\n", clr_fpreg_name, DRegNameForCode(i), clr_fpreg_value, dreg(i), clr_normal); return true; } else if (desc[0] == 's') { PrintF(stream_, "%s %s:%s %g%s\n", clr_fpreg_name, SRegNameForCode(i), clr_fpreg_value, sreg(i), clr_normal); return true; } else if (desc[0] == 'w') { PrintF(stream_, "%s %s:%s 0x%08" PRIx32 "%s\n", clr_reg_name, WRegNameForCode(i), clr_reg_value, wreg(i), clr_normal); return true; } else { // X register names have a wide variety of starting characters, but anything // else will be an X register. PrintF(stream_, "%s %s:%s 0x%016" PRIx64 "%s\n", clr_reg_name, XRegNameForCode(i), clr_reg_value, xreg(i), clr_normal); return true; } } void Simulator::Debug() { #define COMMAND_SIZE 63 #define ARG_SIZE 255 #define STR(a) #a #define XSTR(a) STR(a) char cmd[COMMAND_SIZE + 1]; char arg1[ARG_SIZE + 1]; char arg2[ARG_SIZE + 1]; char* argv[3] = { cmd, arg1, arg2 }; // Make sure to have a proper terminating character if reaching the limit. cmd[COMMAND_SIZE] = 0; arg1[ARG_SIZE] = 0; arg2[ARG_SIZE] = 0; bool done = false; bool cleared_log_disasm_bit = false; while (!done) { // Disassemble the next instruction to execute before doing anything else. PrintInstructionsAt(pc_, 1); // Read the command line. char* line = ReadLine("sim> "); if (line == NULL) { break; } else { // Repeat last command by default. char* last_input = last_debugger_input(); if (strcmp(line, "\n") == 0 && (last_input != NULL)) { DeleteArray(line); line = last_input; } else { // Update the latest command ran set_last_debugger_input(line); } // Use sscanf to parse the individual parts of the command line. At the // moment no command expects more than two parameters. int argc = SScanF(line, "%" XSTR(COMMAND_SIZE) "s " "%" XSTR(ARG_SIZE) "s " "%" XSTR(ARG_SIZE) "s", cmd, arg1, arg2); // stepi / si ------------------------------------------------------------ if ((strcmp(cmd, "si") == 0) || (strcmp(cmd, "stepi") == 0)) { // We are about to execute instructions, after which by default we // should increment the pc_. If it was set when reaching this debug // instruction, it has not been cleared because this instruction has not // completed yet. So clear it manually. pc_modified_ = false; if (argc == 1) { ExecuteInstruction(); } else { int64_t number_of_instructions_to_execute = 1; GetValue(arg1, &number_of_instructions_to_execute); set_log_parameters(log_parameters() | LOG_DISASM); while (number_of_instructions_to_execute-- > 0) { ExecuteInstruction(); } set_log_parameters(log_parameters() & ~LOG_DISASM); PrintF("\n"); } // If it was necessary, the pc has already been updated or incremented // when executing the instruction. So we do not want it to be updated // again. It will be cleared when exiting. pc_modified_ = true; // next / n -------------------------------------------------------------- } else if ((strcmp(cmd, "next") == 0) || (strcmp(cmd, "n") == 0)) { // Tell the simulator to break after the next executed BL. break_on_next_ = true; // Continue. done = true; // continue / cont / c --------------------------------------------------- } else if ((strcmp(cmd, "continue") == 0) || (strcmp(cmd, "cont") == 0) || (strcmp(cmd, "c") == 0)) { // Leave the debugger shell. done = true; // disassemble / disasm / di --------------------------------------------- } else if (strcmp(cmd, "disassemble") == 0 || strcmp(cmd, "disasm") == 0 || strcmp(cmd, "di") == 0) { int64_t n_of_instrs_to_disasm = 10; // default value. int64_t address = reinterpret_cast(pc_); // default value. if (argc >= 2) { // disasm GetValue(arg1, &n_of_instrs_to_disasm); } if (argc >= 3) { // disasm GetValue(arg2, &address); } // Disassemble. PrintInstructionsAt(reinterpret_cast(address), n_of_instrs_to_disasm); PrintF("\n"); // print / p ------------------------------------------------------------- } else if ((strcmp(cmd, "print") == 0) || (strcmp(cmd, "p") == 0)) { if (argc == 2) { if (strcmp(arg1, "all") == 0) { PrintRegisters(); PrintFPRegisters(); } else { if (!PrintValue(arg1)) { PrintF("%s unrecognized\n", arg1); } } } else { PrintF( "print \n" " Print the content of a register. (alias 'p')\n" " 'print all' will print all registers.\n" " Use 'printobject' to get more details about the value.\n"); } // printobject / po ------------------------------------------------------ } else if ((strcmp(cmd, "printobject") == 0) || (strcmp(cmd, "po") == 0)) { if (argc == 2) { int64_t value; OFStream os(stdout); if (GetValue(arg1, &value)) { Object* obj = reinterpret_cast(value); os << arg1 << ": \n"; #ifdef DEBUG obj->Print(os); os << "\n"; #else os << Brief(obj) << "\n"; #endif } else { os << arg1 << " unrecognized\n"; } } else { PrintF("printobject \n" "printobject \n" " Print details about the value. (alias 'po')\n"); } // stack / mem ---------------------------------------------------------- } else if (strcmp(cmd, "stack") == 0 || strcmp(cmd, "mem") == 0) { int64_t* cur = NULL; int64_t* end = NULL; int next_arg = 1; if (strcmp(cmd, "stack") == 0) { cur = reinterpret_cast(jssp()); } else { // "mem" int64_t value; if (!GetValue(arg1, &value)) { PrintF("%s unrecognized\n", arg1); continue; } cur = reinterpret_cast(value); next_arg++; } int64_t words = 0; if (argc == next_arg) { words = 10; } else if (argc == next_arg + 1) { if (!GetValue(argv[next_arg], &words)) { PrintF("%s unrecognized\n", argv[next_arg]); PrintF("Printing 10 double words by default"); words = 10; } } else { UNREACHABLE(); } end = cur + words; while (cur < end) { PrintF(" 0x%016" PRIx64 ": 0x%016" PRIx64 " %10" PRId64, reinterpret_cast(cur), *cur, *cur); HeapObject* obj = reinterpret_cast(*cur); int64_t value = *cur; Heap* current_heap = v8::internal::Isolate::Current()->heap(); if (((value & 1) == 0) || current_heap->Contains(obj)) { PrintF(" ("); if ((value & kSmiTagMask) == 0) { STATIC_ASSERT(kSmiValueSize == 32); int32_t untagged = (value >> kSmiShift) & 0xffffffff; PrintF("smi %" PRId32, untagged); } else { obj->ShortPrint(); } PrintF(")"); } PrintF("\n"); cur++; } // trace / t ------------------------------------------------------------- } else if (strcmp(cmd, "trace") == 0 || strcmp(cmd, "t") == 0) { if ((log_parameters() & (LOG_DISASM | LOG_REGS)) != (LOG_DISASM | LOG_REGS)) { PrintF("Enabling disassembly and registers tracing\n"); set_log_parameters(log_parameters() | LOG_DISASM | LOG_REGS); } else { PrintF("Disabling disassembly and registers tracing\n"); set_log_parameters(log_parameters() & ~(LOG_DISASM | LOG_REGS)); } // break / b ------------------------------------------------------------- } else if (strcmp(cmd, "break") == 0 || strcmp(cmd, "b") == 0) { if (argc == 2) { int64_t value; if (GetValue(arg1, &value)) { SetBreakpoint(reinterpret_cast(value)); } else { PrintF("%s unrecognized\n", arg1); } } else { ListBreakpoints(); PrintF("Use `break ` to set or disable a breakpoint\n"); } // gdb ------------------------------------------------------------------- } else if (strcmp(cmd, "gdb") == 0) { PrintF("Relinquishing control to gdb.\n"); base::OS::DebugBreak(); PrintF("Regaining control from gdb.\n"); // sysregs --------------------------------------------------------------- } else if (strcmp(cmd, "sysregs") == 0) { PrintSystemRegisters(); // help / h -------------------------------------------------------------- } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "h") == 0) { PrintF( "stepi / si\n" " stepi \n" " Step instructions.\n" "next / n\n" " Continue execution until a BL instruction is reached.\n" " At this point a breakpoint is set just after this BL.\n" " Then execution is resumed. It will probably later hit the\n" " breakpoint just set.\n" "continue / cont / c\n" " Continue execution from here.\n" "disassemble / disasm / di\n" " disassemble \n" " Disassemble