| 1 |
// Copyright 2015, VIXL authors |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// Redistribution and use in source and binary forms, with or without |
| 5 |
// modification, are permitted provided that the following conditions are met: |
| 6 |
// |
| 7 |
// * Redistributions of source code must retain the above copyright notice, |
| 8 |
// this list of conditions and the following disclaimer. |
| 9 |
// * Redistributions in binary form must reproduce the above copyright notice, |
| 10 |
// this list of conditions and the following disclaimer in the documentation |
| 11 |
// and/or other materials provided with the distribution. |
| 12 |
// * Neither the name of ARM Limited nor the names of its contributors may be |
| 13 |
// used to endorse or promote products derived from this software without |
| 14 |
// specific prior written permission. |
| 15 |
// |
| 16 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND |
| 17 |
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 |
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 |
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 20 |
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 |
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 |
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 |
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 |
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
|
| 27 |
#include "jstypes.h" |
| 28 |
|
| 29 |
#ifdef JS_SIMULATOR_ARM64 |
| 30 |
|
| 31 |
#include "jit/arm64/vixl/Simulator-vixl.h" |
| 32 |
|
| 33 |
#include <cmath> |
| 34 |
#include <string.h> |
| 35 |
|
| 36 |
#include "jit/AtomicOperations.h" |
| 37 |
|
| 38 |
namespace vixl { |
| 39 |
|
| 40 |
const Instruction* Simulator::kEndOfSimAddress = NULL; |
| 41 |
|
| 42 |
void SimSystemRegister::SetBits(int msb, int lsb, uint32_t bits) { |
| 43 |
int width = msb - lsb + 1; |
| 44 |
VIXL_ASSERT(IsUintN(width, bits) || IsIntN(width, bits)); |
| 45 |
|
| 46 |
bits <<= lsb; |
| 47 |
uint32_t mask = ((1 << width) - 1) << lsb; |
| 48 |
VIXL_ASSERT((mask & write_ignore_mask_) == 0); |
| 49 |
|
| 50 |
value_ = (value_ & ~mask) | (bits & mask); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
SimSystemRegister SimSystemRegister::DefaultValueFor(SystemRegister id) { |
| 55 |
switch (id) { |
| 56 |
case NZCV: |
| 57 |
return SimSystemRegister(0x00000000, NZCVWriteIgnoreMask); |
| 58 |
case FPCR: |
| 59 |
return SimSystemRegister(0x00000000, FPCRWriteIgnoreMask); |
| 60 |
default: |
| 61 |
VIXL_UNREACHABLE(); |
| 62 |
return SimSystemRegister(); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
void Simulator::enable_single_stepping(SingleStepCallback cb, void* arg) { |
| 67 |
single_stepping_ = true; |
| 68 |
single_step_callback_ = cb; |
| 69 |
single_step_callback_arg_ = arg; |
| 70 |
single_step_callback_(single_step_callback_arg_, this, (void*)get_pc()); |
| 71 |
} |
| 72 |
|
| 73 |
void Simulator::disable_single_stepping() { |
| 74 |
if (!single_stepping_) { |
| 75 |
return; |
| 76 |
} |
| 77 |
single_step_callback_(single_step_callback_arg_, this, (void*)get_pc()); |
| 78 |
single_stepping_ = false; |
| 79 |
single_step_callback_ = nullptr; |
| 80 |
single_step_callback_arg_ = nullptr; |
| 81 |
} |
| 82 |
|
| 83 |
void Simulator::Run() { |
| 84 |
if (single_stepping_) { |
| 85 |
single_step_callback_(single_step_callback_arg_, this, nullptr); |
| 86 |
} |
| 87 |
|
| 88 |
pc_modified_ = false; |
| 89 |
while (pc_ != kEndOfSimAddress) { |
| 90 |
if (single_stepping_) { |
| 91 |
single_step_callback_(single_step_callback_arg_, this, (void*)pc_); |
| 92 |
} |
| 93 |
|
| 94 |
ExecuteInstruction(); |
| 95 |
LogAllWrittenRegisters(); |
| 96 |
} |
| 97 |
|
| 98 |
if (single_stepping_) { |
| 99 |
single_step_callback_(single_step_callback_arg_, this, nullptr); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
void Simulator::RunFrom(const Instruction* first) { |
| 105 |
set_pc(first); |
| 106 |
Run(); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
const char* Simulator::xreg_names[] = { |
| 111 |
"x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", |
| 112 |
"x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", |
| 113 |
"x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23", |
| 114 |
"x24", "x25", "x26", "x27", "x28", "x29", "lr", "xzr", "sp"}; |
| 115 |
|
| 116 |
const char* Simulator::wreg_names[] = { |
| 117 |
"w0", "w1", "w2", "w3", "w4", "w5", "w6", "w7", |
| 118 |
"w8", "w9", "w10", "w11", "w12", "w13", "w14", "w15", |
| 119 |
"w16", "w17", "w18", "w19", "w20", "w21", "w22", "w23", |
| 120 |
"w24", "w25", "w26", "w27", "w28", "w29", "w30", "wzr", "wsp"}; |
| 121 |
|
| 122 |
const char* Simulator::sreg_names[] = { |
| 123 |
"s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", |
| 124 |
"s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15", |
| 125 |
"s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23", |
| 126 |
"s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31"}; |
| 127 |
|
| 128 |
const char* Simulator::dreg_names[] = { |
| 129 |
"d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", |
| 130 |
"d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15", |
| 131 |
"d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23", |
| 132 |
"d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31"}; |
| 133 |
|
| 134 |
const char* Simulator::vreg_names[] = { |
| 135 |
"v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", |
| 136 |
"v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15", |
| 137 |
"v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", |
| 138 |
"v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31"}; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
const char* Simulator::WRegNameForCode(unsigned code, Reg31Mode mode) { |
| 143 |
VIXL_ASSERT(code < kNumberOfRegisters); |
| 144 |
// If the code represents the stack pointer, index the name after zr. |
| 145 |
if ((code == kZeroRegCode) && (mode == Reg31IsStackPointer)) { |
| 146 |
code = kZeroRegCode + 1; |
| 147 |
} |
| 148 |
return wreg_names[code]; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
const char* Simulator::XRegNameForCode(unsigned code, Reg31Mode mode) { |
| 153 |
VIXL_ASSERT(code < kNumberOfRegisters); |
| 154 |
// If the code represents the stack pointer, index the name after zr. |
| 155 |
if ((code == kZeroRegCode) && (mode == Reg31IsStackPointer)) { |
| 156 |
code = kZeroRegCode + 1; |
| 157 |
} |
| 158 |
return xreg_names[code]; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
const char* Simulator::SRegNameForCode(unsigned code) { |
| 163 |
VIXL_ASSERT(code < kNumberOfFPRegisters); |
| 164 |
return sreg_names[code]; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
const char* Simulator::DRegNameForCode(unsigned code) { |
| 169 |
VIXL_ASSERT(code < kNumberOfFPRegisters); |
| 170 |
return dreg_names[code]; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
const char* Simulator::VRegNameForCode(unsigned code) { |
| 175 |
VIXL_ASSERT(code < kNumberOfVRegisters); |
| 176 |
return vreg_names[code]; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
#define COLOUR(colour_code) "\033[0;" colour_code "m" |
| 181 |
#define COLOUR_BOLD(colour_code) "\033[1;" colour_code "m" |
| 182 |
#define NORMAL "" |
| 183 |
#define GREY "30" |
| 184 |
#define RED "31" |
| 185 |
#define GREEN "32" |
| 186 |
#define YELLOW "33" |
| 187 |
#define BLUE "34" |
| 188 |
#define MAGENTA "35" |
| 189 |
#define CYAN "36" |
| 190 |
#define WHITE "37" |
| 191 |
void Simulator::set_coloured_trace(bool value) { |
| 192 |
coloured_trace_ = value; |
| 193 |
|
| 194 |
clr_normal = value ? COLOUR(NORMAL) : ""; |
| 195 |
clr_flag_name = value ? COLOUR_BOLD(WHITE) : ""; |
| 196 |
clr_flag_value = value ? COLOUR(NORMAL) : ""; |
| 197 |
clr_reg_name = value ? COLOUR_BOLD(CYAN) : ""; |
| 198 |
clr_reg_value = value ? COLOUR(CYAN) : ""; |
| 199 |
clr_vreg_name = value ? COLOUR_BOLD(MAGENTA) : ""; |
| 200 |
clr_vreg_value = value ? COLOUR(MAGENTA) : ""; |
| 201 |
clr_memory_address = value ? COLOUR_BOLD(BLUE) : ""; |
| 202 |
clr_warning = value ? COLOUR_BOLD(YELLOW) : ""; |
| 203 |
clr_warning_message = value ? COLOUR(YELLOW) : ""; |
| 204 |
clr_printf = value ? COLOUR(GREEN) : ""; |
| 205 |
} |
| 206 |
#undef COLOUR |
| 207 |
#undef COLOUR_BOLD |
| 208 |
#undef NORMAL |
| 209 |
#undef GREY |
| 210 |
#undef RED |
| 211 |
#undef GREEN |
| 212 |
#undef YELLOW |
| 213 |
#undef BLUE |
| 214 |
#undef MAGENTA |
| 215 |
#undef CYAN |
| 216 |
#undef WHITE |
| 217 |
|
| 218 |
|
| 219 |
void Simulator::set_trace_parameters(int parameters) { |
| 220 |
bool disasm_before = trace_parameters_ & LOG_DISASM; |
| 221 |
trace_parameters_ = parameters; |
| 222 |
bool disasm_after = trace_parameters_ & LOG_DISASM; |
| 223 |
|
| 224 |
if (disasm_before != disasm_after) { |
| 225 |
if (disasm_after) { |
| 226 |
decoder_->InsertVisitorBefore(print_disasm_, this); |
| 227 |
} else { |
| 228 |
decoder_->RemoveVisitor(print_disasm_); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
void Simulator::set_instruction_stats(bool value) { |
| 235 |
if (instrumentation_ == nullptr) { |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
if (value != instruction_stats_) { |
| 240 |
if (value) { |
| 241 |
decoder_->AppendVisitor(instrumentation_); |
| 242 |
} else { |
| 243 |
decoder_->RemoveVisitor(instrumentation_); |
| 244 |
} |
| 245 |
instruction_stats_ = value; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// Helpers --------------------------------------------------------------------- |
| 250 |
uint64_t Simulator::AddWithCarry(unsigned reg_size, |
| 251 |
bool set_flags, |
| 252 |
uint64_t left, |
| 253 |
uint64_t right, |
| 254 |
int carry_in) { |
| 255 |
VIXL_ASSERT((carry_in == 0) || (carry_in == 1)); |
| 256 |
VIXL_ASSERT((reg_size == kXRegSize) || (reg_size == kWRegSize)); |
| 257 |
|
| 258 |
uint64_t max_uint = (reg_size == kWRegSize) ? kWMaxUInt : kXMaxUInt; |
| 259 |
uint64_t reg_mask = (reg_size == kWRegSize) ? kWRegMask : kXRegMask; |
| 260 |
uint64_t sign_mask = (reg_size == kWRegSize) ? kWSignMask : kXSignMask; |
| 261 |
|
| 262 |
left &= reg_mask; |
| 263 |
right &= reg_mask; |
| 264 |
uint64_t result = (left + right + carry_in) & reg_mask; |
| 265 |
|
| 266 |
if (set_flags) { |
| 267 |
nzcv().SetN(CalcNFlag(result, reg_size)); |
| 268 |
nzcv().SetZ(CalcZFlag(result)); |
| 269 |
|
| 270 |
// Compute the C flag by comparing the result to the max unsigned integer. |
| 271 |
uint64_t max_uint_2op = max_uint - carry_in; |
| 272 |
bool C = (left > max_uint_2op) || ((max_uint_2op - left) < right); |
| 273 |
nzcv().SetC(C ? 1 : 0); |
| 274 |
|
| 275 |
// Overflow iff the sign bit is the same for the two inputs and different |
| 276 |
// for the result. |
| 277 |
uint64_t left_sign = left & sign_mask; |
| 278 |
uint64_t right_sign = right & sign_mask; |
| 279 |
uint64_t result_sign = result & sign_mask; |
| 280 |
bool V = (left_sign == right_sign) && (left_sign != result_sign); |
| 281 |
nzcv().SetV(V ? 1 : 0); |
| 282 |
|
| 283 |
LogSystemRegister(NZCV); |
| 284 |
} |
| 285 |
return result; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
int64_t Simulator::ShiftOperand(unsigned reg_size, |
| 290 |
int64_t value, |
| 291 |
Shift shift_type, |
| 292 |
unsigned amount) { |
| 293 |
if (amount == 0) { |
| 294 |
return value; |
| 295 |
} |
| 296 |
int64_t mask = reg_size == kXRegSize ? kXRegMask : kWRegMask; |
| 297 |
switch (shift_type) { |
| 298 |
case LSL: |
| 299 |
return (value << amount) & mask; |
| 300 |
case LSR: |
| 301 |
return static_cast<uint64_t>(value) >> amount; |
| 302 |
case ASR: { |
| 303 |
// Shift used to restore the sign. |
| 304 |
unsigned s_shift = kXRegSize - reg_size; |
| 305 |
// Value with its sign restored. |
| 306 |
int64_t s_value = (value << s_shift) >> s_shift; |
| 307 |
return (s_value >> amount) & mask; |
| 308 |
} |
| 309 |
case ROR: { |
| 310 |
if (reg_size == kWRegSize) { |
| 311 |
value &= kWRegMask; |
| 312 |
} |
| 313 |
return (static_cast<uint64_t>(value) >> amount) | |
| 314 |
((value & ((INT64_C(1) << amount) - 1)) << |
| 315 |
(reg_size - amount)); |
| 316 |
} |
| 317 |
default: |
| 318 |
VIXL_UNIMPLEMENTED(); |
| 319 |
return 0; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
int64_t Simulator::ExtendValue(unsigned reg_size, |
| 325 |
int64_t value, |
| 326 |
Extend extend_type, |
| 327 |
unsigned left_shift) { |
| 328 |
switch (extend_type) { |
| 329 |
case UXTB: |
| 330 |
value &= kByteMask; |
| 331 |
break; |
| 332 |
case UXTH: |
| 333 |
value &= kHalfWordMask; |
| 334 |
break; |
| 335 |
case UXTW: |
| 336 |
value &= kWordMask; |
| 337 |
break; |
| 338 |
case SXTB: |
| 339 |
value = (value << 56) >> 56; |
| 340 |
break; |
| 341 |
case SXTH: |
| 342 |
value = (value << 48) >> 48; |
| 343 |
break; |
| 344 |
case SXTW: |
| 345 |
value = (value << 32) >> 32; |
| 346 |
break; |
| 347 |
case UXTX: |
| 348 |
case SXTX: |
| 349 |
break; |
| 350 |
default: |
| 351 |
VIXL_UNREACHABLE(); |
| 352 |
} |
| 353 |
int64_t mask = (reg_size == kXRegSize) ? kXRegMask : kWRegMask; |
| 354 |
return (value << left_shift) & mask; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
void Simulator::FPCompare(double val0, double val1, FPTrapFlags trap) { |
| 359 |
AssertSupportedFPCR(); |
| 360 |
|
| 361 |
// TODO: This assumes that the C++ implementation handles comparisons in the |
| 362 |
// way that we expect (as per AssertSupportedFPCR()). |
| 363 |
bool process_exception = false; |
| 364 |
if ((std::isnan(val0) != 0) || (std::isnan(val1) != 0)) { |
| 365 |
nzcv().SetRawValue(FPUnorderedFlag); |
| 366 |
if (IsSignallingNaN(val0) || IsSignallingNaN(val1) || |
| 367 |
(trap == EnableTrap)) { |
| 368 |
process_exception = true; |
| 369 |
} |
| 370 |
} else if (val0 < val1) { |
| 371 |
nzcv().SetRawValue(FPLessThanFlag); |
| 372 |
} else if (val0 > val1) { |
| 373 |
nzcv().SetRawValue(FPGreaterThanFlag); |
| 374 |
} else if (val0 == val1) { |
| 375 |
nzcv().SetRawValue(FPEqualFlag); |
| 376 |
} else { |
| 377 |
VIXL_UNREACHABLE(); |
| 378 |
} |
| 379 |
LogSystemRegister(NZCV); |
| 380 |
if (process_exception) FPProcessException(); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
Simulator::PrintRegisterFormat Simulator::GetPrintRegisterFormatForSize( |
| 385 |
unsigned reg_size, unsigned lane_size) { |
| 386 |
VIXL_ASSERT(reg_size >= lane_size); |
| 387 |
|
| 388 |
uint32_t format = 0; |
| 389 |
if (reg_size != lane_size) { |
| 390 |
switch (reg_size) { |
| 391 |
default: VIXL_UNREACHABLE(); break; |
| 392 |
case kQRegSizeInBytes: format = kPrintRegAsQVector; break; |
| 393 |
case kDRegSizeInBytes: format = kPrintRegAsDVector; break; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
switch (lane_size) { |
| 398 |
default: VIXL_UNREACHABLE(); break; |
| 399 |
case kQRegSizeInBytes: format |= kPrintReg1Q; break; |
| 400 |
case kDRegSizeInBytes: format |= kPrintReg1D; break; |
| 401 |
case kSRegSizeInBytes: format |= kPrintReg1S; break; |
| 402 |
case kHRegSizeInBytes: format |= kPrintReg1H; break; |
| 403 |
case kBRegSizeInBytes: format |= kPrintReg1B; break; |
| 404 |
} |
| 405 |
// These sizes would be duplicate case labels. |
| 406 |
VIXL_STATIC_ASSERT(kXRegSizeInBytes == kDRegSizeInBytes); |
| 407 |
VIXL_STATIC_ASSERT(kWRegSizeInBytes == kSRegSizeInBytes); |
| 408 |
VIXL_STATIC_ASSERT(kPrintXReg == kPrintReg1D); |
| 409 |
VIXL_STATIC_ASSERT(kPrintWReg == kPrintReg1S); |
| 410 |
|
| 411 |
return static_cast<PrintRegisterFormat>(format); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
Simulator::PrintRegisterFormat Simulator::GetPrintRegisterFormat( |
| 416 |
VectorFormat vform) { |
| 417 |
switch (vform) { |
| 418 |
default: VIXL_UNREACHABLE(); return kPrintReg16B; |
| 419 |
case kFormat16B: return kPrintReg16B; |
| 420 |
case kFormat8B: return kPrintReg8B; |
| 421 |
case kFormat8H: return kPrintReg8H; |
| 422 |
case kFormat4H: return kPrintReg4H; |
| 423 |
case kFormat4S: return kPrintReg4S; |
| 424 |
case kFormat2S: return kPrintReg2S; |
| 425 |
case kFormat2D: return kPrintReg2D; |
| 426 |
case kFormat1D: return kPrintReg1D; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
void Simulator::PrintWrittenRegisters() { |
| 432 |
for (unsigned i = 0; i < kNumberOfRegisters; i++) { |
| 433 |
if (registers_[i].WrittenSinceLastLog()) PrintRegister(i); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
void Simulator::PrintWrittenVRegisters() { |
| 439 |
for (unsigned i = 0; i < kNumberOfVRegisters; i++) { |
| 440 |
// At this point there is no type information, so print as a raw 1Q. |
| 441 |
if (vregisters_[i].WrittenSinceLastLog()) PrintVRegister(i, kPrintReg1Q); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
void Simulator::PrintSystemRegisters() { |
| 447 |
PrintSystemRegister(NZCV); |
| 448 |
PrintSystemRegister(FPCR); |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
void Simulator::PrintRegisters() { |
| 453 |
for (unsigned i = 0; i < kNumberOfRegisters; i++) { |
| 454 |
PrintRegister(i); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
void Simulator::PrintVRegisters() { |
| 460 |
for (unsigned i = 0; i < kNumberOfVRegisters; i++) { |
| 461 |
// At this point there is no type information, so print as a raw 1Q. |
| 462 |
PrintVRegister(i, kPrintReg1Q); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
// Print a register's name and raw value. |
| 468 |
// |
| 469 |
// Only the least-significant `size_in_bytes` bytes of the register are printed, |
| 470 |
// but the value is aligned as if the whole register had been printed. |
| 471 |
// |
| 472 |
// For typical register updates, size_in_bytes should be set to kXRegSizeInBytes |
| 473 |
// -- the default -- so that the whole register is printed. Other values of |
| 474 |
// size_in_bytes are intended for use when the register hasn't actually been |
| 475 |
// updated (such as in PrintWrite). |
| 476 |
// |
| 477 |
// No newline is printed. This allows the caller to print more details (such as |
| 478 |
// a memory access annotation). |
| 479 |
void Simulator::PrintRegisterRawHelper(unsigned code, Reg31Mode r31mode, |
| 480 |
int size_in_bytes) { |
| 481 |
// The template for all supported sizes. |
| 482 |
// "# x{code}: 0xffeeddccbbaa9988" |
| 483 |
// "# w{code}: 0xbbaa9988" |
| 484 |
// "# w{code}<15:0>: 0x9988" |
| 485 |
// "# w{code}<7:0>: 0x88" |
| 486 |
unsigned padding_chars = (kXRegSizeInBytes - size_in_bytes) * 2; |
| 487 |
|
| 488 |
const char * name = ""; |
| 489 |
const char * suffix = ""; |
| 490 |
switch (size_in_bytes) { |
| 491 |
case kXRegSizeInBytes: name = XRegNameForCode(code, r31mode); break; |
| 492 |
case kWRegSizeInBytes: name = WRegNameForCode(code, r31mode); break; |
| 493 |
case 2: |
| 494 |
name = WRegNameForCode(code, r31mode); |
| 495 |
suffix = "<15:0>"; |
| 496 |
padding_chars -= strlen(suffix); |
| 497 |
break; |
| 498 |
case 1: |
| 499 |
name = WRegNameForCode(code, r31mode); |
| 500 |
suffix = "<7:0>"; |
| 501 |
padding_chars -= strlen(suffix); |
| 502 |
break; |
| 503 |
default: |
| 504 |
VIXL_UNREACHABLE(); |
| 505 |
} |
| 506 |
fprintf(stream_, "# %s%5s%s: ", clr_reg_name, name, suffix); |
| 507 |
|
| 508 |
// Print leading padding spaces. |
| 509 |
VIXL_ASSERT(padding_chars < (kXRegSizeInBytes * 2)); |
| 510 |
for (unsigned i = 0; i < padding_chars; i++) { |
| 511 |
putc(' ', stream_); |
| 512 |
} |
| 513 |
|
| 514 |
// Print the specified bits in hexadecimal format. |
| 515 |
uint64_t bits = reg<uint64_t>(code, r31mode); |
| 516 |
bits &= kXRegMask >> ((kXRegSizeInBytes - size_in_bytes) * 8); |
| 517 |
VIXL_STATIC_ASSERT(sizeof(bits) == kXRegSizeInBytes); |
| 518 |
|
| 519 |
int chars = size_in_bytes * 2; |
| 520 |
fprintf(stream_, "%s0x%0*" PRIx64 "%s", |
| 521 |
clr_reg_value, chars, bits, clr_normal); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
void Simulator::PrintRegister(unsigned code, Reg31Mode r31mode) { |
| 526 |
registers_[code].NotifyRegisterLogged(); |
| 527 |
|
| 528 |
// Don't print writes into xzr. |
| 529 |
if ((code == kZeroRegCode) && (r31mode == Reg31IsZeroRegister)) { |
| 530 |
return; |
| 531 |
} |
| 532 |
|
| 533 |
// The template for all x and w registers: |
| 534 |
// "# x{code}: 0x{value}" |
| 535 |
// "# w{code}: 0x{value}" |
| 536 |
|
| 537 |
PrintRegisterRawHelper(code, r31mode); |
| 538 |
fprintf(stream_, "\n"); |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
// Print a register's name and raw value. |
| 543 |
// |
| 544 |
// The `bytes` and `lsb` arguments can be used to limit the bytes that are |
| 545 |
// printed. These arguments are intended for use in cases where register hasn't |
| 546 |
// actually been updated (such as in PrintVWrite). |
| 547 |
// |
| 548 |
// No newline is printed. This allows the caller to print more details (such as |
| 549 |
// a floating-point interpretation or a memory access annotation). |
| 550 |
void Simulator::PrintVRegisterRawHelper(unsigned code, int bytes, int lsb) { |
| 551 |
// The template for vector types: |
| 552 |
// "# v{code}: 0xffeeddccbbaa99887766554433221100". |
| 553 |
// An example with bytes=4 and lsb=8: |
| 554 |
// "# v{code}: 0xbbaa9988 ". |
| 555 |
fprintf(stream_, "# %s%5s: %s", |
| 556 |
clr_vreg_name, VRegNameForCode(code), clr_vreg_value); |
| 557 |
|
| 558 |
int msb = lsb + bytes - 1; |
| 559 |
int byte = kQRegSizeInBytes - 1; |
| 560 |
|
| 561 |
// Print leading padding spaces. (Two spaces per byte.) |
| 562 |
while (byte > msb) { |
| 563 |
fprintf(stream_, " "); |
| 564 |
byte--; |
| 565 |
} |
| 566 |
|
| 567 |
// Print the specified part of the value, byte by byte. |
| 568 |
qreg_t rawbits = qreg(code); |
| 569 |
fprintf(stream_, "0x"); |
| 570 |
while (byte >= lsb) { |
| 571 |
fprintf(stream_, "%02x", rawbits.val[byte]); |
| 572 |
byte--; |
| 573 |
} |
| 574 |
|
| 575 |
// Print trailing padding spaces. |
| 576 |
while (byte >= 0) { |
| 577 |
fprintf(stream_, " "); |
| 578 |
byte--; |
| 579 |
} |
| 580 |
fprintf(stream_, "%s", clr_normal); |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
// Print each of the specified lanes of a register as a float or double value. |
| 585 |
// |
| 586 |
// The `lane_count` and `lslane` arguments can be used to limit the lanes that |
| 587 |
// are printed. These arguments are intended for use in cases where register |
| 588 |
// hasn't actually been updated (such as in PrintVWrite). |
| 589 |
// |
| 590 |
// No newline is printed. This allows the caller to print more details (such as |
| 591 |
// a memory access annotation). |
| 592 |
void Simulator::PrintVRegisterFPHelper(unsigned code, |
| 593 |
unsigned lane_size_in_bytes, |
| 594 |
int lane_count, |
| 595 |
int rightmost_lane) { |
| 596 |
VIXL_ASSERT((lane_size_in_bytes == kSRegSizeInBytes) || |
| 597 |
(lane_size_in_bytes == kDRegSizeInBytes)); |
| 598 |
|
| 599 |
unsigned msb = ((lane_count + rightmost_lane) * lane_size_in_bytes); |
| 600 |
VIXL_ASSERT(msb <= kQRegSizeInBytes); |
| 601 |
|
| 602 |
// For scalar types ((lane_count == 1) && (rightmost_lane == 0)), a register |
| 603 |
// name is used: |
| 604 |
// " (s{code}: {value})" |
| 605 |
// " (d{code}: {value})" |
| 606 |
// For vector types, "..." is used to represent one or more omitted lanes. |
| 607 |
// " (..., {value}, {value}, ...)" |
| 608 |
if ((lane_count == 1) && (rightmost_lane == 0)) { |
| 609 |
const char * name = |
| 610 |
(lane_size_in_bytes == kSRegSizeInBytes) ? SRegNameForCode(code) |
| 611 |
: DRegNameForCode(code); |
| 612 |
fprintf(stream_, " (%s%s: ", clr_vreg_name, name); |
| 613 |
} else { |
| 614 |
if (msb < (kQRegSizeInBytes - 1)) { |
| 615 |
fprintf(stream_, " (..., "); |
| 616 |
} else { |
| 617 |
fprintf(stream_, " ("); |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
// Print the list of values. |
| 622 |
const char * separator = ""; |
| 623 |
int leftmost_lane = rightmost_lane + lane_count - 1; |
| 624 |
for (int lane = leftmost_lane; lane >= rightmost_lane; lane--) { |
| 625 |
double value = |
| 626 |
(lane_size_in_bytes == kSRegSizeInBytes) ? vreg(code).Get<float>(lane) |
| 627 |
: vreg(code).Get<double>(lane); |
| 628 |
fprintf(stream_, "%s%s%#g%s", separator, clr_vreg_value, value, clr_normal); |
| 629 |
separator = ", "; |
| 630 |
} |
| 631 |
|
| 632 |
if (rightmost_lane > 0) { |
| 633 |
fprintf(stream_, ", ..."); |
| 634 |
} |
| 635 |
fprintf(stream_, ")"); |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
void Simulator::PrintVRegister(unsigned code, PrintRegisterFormat format) { |
| 640 |
vregisters_[code].NotifyRegisterLogged(); |
| 641 |
|
| 642 |
int lane_size_log2 = format & kPrintRegLaneSizeMask; |
| 643 |
|
| 644 |
int reg_size_log2; |
| 645 |
if (format & kPrintRegAsQVector) { |
| 646 |
reg_size_log2 = kQRegSizeInBytesLog2; |
| 647 |
} else if (format & kPrintRegAsDVector) { |
| 648 |
reg_size_log2 = kDRegSizeInBytesLog2; |
| 649 |
} else { |
| 650 |
// Scalar types. |
| 651 |
reg_size_log2 = lane_size_log2; |
| 652 |
} |
| 653 |
|
| 654 |
int lane_count = 1 << (reg_size_log2 - lane_size_log2); |
| 655 |
int lane_size = 1 << lane_size_log2; |
| 656 |
|
| 657 |
// The template for vector types: |
| 658 |
// "# v{code}: 0x{rawbits} (..., {value}, ...)". |
| 659 |
// The template for scalar types: |
| 660 |
// "# v{code}: 0x{rawbits} ({reg}:{value})". |
| 661 |
// The values in parentheses after the bit representations are floating-point |
| 662 |
// interpretations. They are displayed only if the kPrintVRegAsFP bit is set. |
| 663 |
|
| 664 |
PrintVRegisterRawHelper(code); |
| 665 |
if (format & kPrintRegAsFP) { |
| 666 |
PrintVRegisterFPHelper(code, lane_size, lane_count); |
| 667 |
} |
| 668 |
|
| 669 |
fprintf(stream_, "\n"); |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
void Simulator::PrintSystemRegister(SystemRegister id) { |
| 674 |
switch (id) { |
| 675 |
case NZCV: |
| 676 |
fprintf(stream_, "# %sNZCV: %sN:%d Z:%d C:%d V:%d%s\n", |
| 677 |
clr_flag_name, clr_flag_value, |
| 678 |
nzcv().N(), nzcv().Z(), nzcv().C(), nzcv().V(), |
| 679 |
clr_normal); |
| 680 |
break; |
| 681 |
case FPCR: { |
| 682 |
static const char * rmode[] = { |
| 683 |
"0b00 (Round to Nearest)", |
| 684 |
"0b01 (Round towards Plus Infinity)", |
| 685 |
"0b10 (Round towards Minus Infinity)", |
| 686 |
"0b11 (Round towards Zero)" |
| 687 |
}; |
| 688 |
VIXL_ASSERT(fpcr().RMode() < (sizeof(rmode) / sizeof(rmode[0]))); |
| 689 |
fprintf(stream_, |
| 690 |
"# %sFPCR: %sAHP:%d DN:%d FZ:%d RMode:%s%s\n", |
| 691 |
clr_flag_name, clr_flag_value, |
| 692 |
fpcr().AHP(), fpcr().DN(), fpcr().FZ(), rmode[fpcr().RMode()], |
| 693 |
clr_normal); |
| 694 |
break; |
| 695 |
} |
| 696 |
default: |
| 697 |
VIXL_UNREACHABLE(); |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
void Simulator::PrintRead(uintptr_t address, |
| 703 |
unsigned reg_code, |
| 704 |
PrintRegisterFormat format) { |
| 705 |
registers_[reg_code].NotifyRegisterLogged(); |
| 706 |
|
| 707 |
USE(format); |
| 708 |
|
| 709 |
// The template is "# {reg}: 0x{value} <- {address}". |
| 710 |
PrintRegisterRawHelper(reg_code, Reg31IsZeroRegister); |
| 711 |
fprintf(stream_, " <- %s0x%016" PRIxPTR "%s\n", |
| 712 |
clr_memory_address, address, clr_normal); |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
void Simulator::PrintVRead(uintptr_t address, |
| 717 |
unsigned reg_code, |
| 718 |
PrintRegisterFormat format, |
| 719 |
unsigned lane) { |
| 720 |
vregisters_[reg_code].NotifyRegisterLogged(); |
| 721 |
|
| 722 |
// The template is "# v{code}: 0x{rawbits} <- address". |
| 723 |
PrintVRegisterRawHelper(reg_code); |
| 724 |
if (format & kPrintRegAsFP) { |
| 725 |
PrintVRegisterFPHelper(reg_code, GetPrintRegLaneSizeInBytes(format), |
| 726 |
GetPrintRegLaneCount(format), lane); |
| 727 |
} |
| 728 |
fprintf(stream_, " <- %s0x%016" PRIxPTR "%s\n", |
| 729 |
clr_memory_address, address, clr_normal); |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
void Simulator::PrintWrite(uintptr_t address, |
| 734 |
unsigned reg_code, |
| 735 |
PrintRegisterFormat format) { |
| 736 |
VIXL_ASSERT(GetPrintRegLaneCount(format) == 1); |
| 737 |
|
| 738 |
// The template is "# v{code}: 0x{value} -> {address}". To keep the trace tidy |
| 739 |
// and readable, the value is aligned with the values in the register trace. |
| 740 |
PrintRegisterRawHelper(reg_code, Reg31IsZeroRegister, |
| 741 |
GetPrintRegSizeInBytes(format)); |
| 742 |
fprintf(stream_, " -> %s0x%016" PRIxPTR "%s\n", |
| 743 |
clr_memory_address, address, clr_normal); |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
void Simulator::PrintVWrite(uintptr_t address, |
| 748 |
unsigned reg_code, |
| 749 |
PrintRegisterFormat format, |
| 750 |
unsigned lane) { |
| 751 |
// The templates: |
| 752 |
// "# v{code}: 0x{rawbits} -> {address}" |
| 753 |
// "# v{code}: 0x{rawbits} (..., {value}, ...) -> {address}". |
| 754 |
// "# v{code}: 0x{rawbits} ({reg}:{value}) -> {address}" |
| 755 |
// Because this trace doesn't represent a change to the source register's |
| 756 |
// value, only the relevant part of the value is printed. To keep the trace |
| 757 |
// tidy and readable, the raw value is aligned with the other values in the |
| 758 |
// register trace. |
| 759 |
int lane_count = GetPrintRegLaneCount(format); |
| 760 |
int lane_size = GetPrintRegLaneSizeInBytes(format); |
| 761 |
int reg_size = GetPrintRegSizeInBytes(format); |
| 762 |
PrintVRegisterRawHelper(reg_code, reg_size, lane_size * lane); |
| 763 |
if (format & kPrintRegAsFP) { |
| 764 |
PrintVRegisterFPHelper(reg_code, lane_size, lane_count, lane); |
| 765 |
} |
| 766 |
fprintf(stream_, " -> %s0x%016" PRIxPTR "%s\n", |
| 767 |
clr_memory_address, address, clr_normal); |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
// Visitors--------------------------------------------------------------------- |
| 772 |
|
| 773 |
void Simulator::VisitUnimplemented(const Instruction* instr) { |
| 774 |
printf("Unimplemented instruction at %p: 0x%08" PRIx32 "\n", |
| 775 |
reinterpret_cast<const void*>(instr), instr->InstructionBits()); |
| 776 |
VIXL_UNIMPLEMENTED(); |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
void Simulator::VisitUnallocated(const Instruction* instr) { |
| 781 |
printf("Unallocated instruction at %p: 0x%08" PRIx32 "\n", |
| 782 |
reinterpret_cast<const void*>(instr), instr->InstructionBits()); |
| 783 |
VIXL_UNIMPLEMENTED(); |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
void Simulator::VisitPCRelAddressing(const Instruction* instr) { |
| 788 |
VIXL_ASSERT((instr->Mask(PCRelAddressingMask) == ADR) || |
| 789 |
(instr->Mask(PCRelAddressingMask) == ADRP)); |
| 790 |
|
| 791 |
set_reg(instr->Rd(), instr->ImmPCOffsetTarget()); |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
void Simulator::VisitUnconditionalBranch(const Instruction* instr) { |
| 796 |
switch (instr->Mask(UnconditionalBranchMask)) { |
| 797 |
case BL: |
| 798 |
set_lr(instr->NextInstruction()); |
| 799 |
VIXL_FALLTHROUGH(); |
| 800 |
case B: |
| 801 |
set_pc(instr->ImmPCOffsetTarget()); |
| 802 |
break; |
| 803 |
default: VIXL_UNREACHABLE(); |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
void Simulator::VisitConditionalBranch(const Instruction* instr) { |
| 809 |
VIXL_ASSERT(instr->Mask(ConditionalBranchMask) == B_cond); |
| 810 |
if (ConditionPassed(instr->ConditionBranch())) { |
| 811 |
set_pc(instr->ImmPCOffsetTarget()); |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
void Simulator::VisitUnconditionalBranchToRegister(const Instruction* instr) { |
| 817 |
const Instruction* target = Instruction::Cast(xreg(instr->Rn())); |
| 818 |
|
| 819 |
switch (instr->Mask(UnconditionalBranchToRegisterMask)) { |
| 820 |
case BLR: |
| 821 |
set_lr(instr->NextInstruction()); |
| 822 |
VIXL_FALLTHROUGH(); |
| 823 |
case BR: |
| 824 |
case RET: set_pc(target); break; |
| 825 |
default: VIXL_UNREACHABLE(); |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
void Simulator::VisitTestBranch(const Instruction* instr) { |
| 831 |
unsigned bit_pos = (instr->ImmTestBranchBit5() << 5) | |
| 832 |
instr->ImmTestBranchBit40(); |
| 833 |
bool bit_zero = ((xreg(instr->Rt()) >> bit_pos) & 1) == 0; |
| 834 |
bool take_branch = false; |
| 835 |
switch (instr->Mask(TestBranchMask)) { |
| 836 |
case TBZ: take_branch = bit_zero; break; |
| 837 |
case TBNZ: take_branch = !bit_zero; break; |
| 838 |
default: VIXL_UNIMPLEMENTED(); |
| 839 |
} |
| 840 |
if (take_branch) { |
| 841 |
set_pc(instr->ImmPCOffsetTarget()); |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
void Simulator::VisitCompareBranch(const Instruction* instr) { |
| 847 |
unsigned rt = instr->Rt(); |
| 848 |
bool take_branch = false; |
| 849 |
switch (instr->Mask(CompareBranchMask)) { |
| 850 |
case CBZ_w: take_branch = (wreg(rt) == 0); break; |
| 851 |
case CBZ_x: take_branch = (xreg(rt) == 0); break; |
| 852 |
case CBNZ_w: take_branch = (wreg(rt) != 0); break; |
| 853 |
case CBNZ_x: take_branch = (xreg(rt) != 0); break; |
| 854 |
default: VIXL_UNIMPLEMENTED(); |
| 855 |
} |
| 856 |
if (take_branch) { |
| 857 |
set_pc(instr->ImmPCOffsetTarget()); |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
void Simulator::AddSubHelper(const Instruction* instr, int64_t op2) { |
| 863 |
unsigned reg_size = instr->SixtyFourBits() ? kXRegSize : kWRegSize; |
| 864 |
bool set_flags = instr->FlagsUpdate(); |
| 865 |
int64_t new_val = 0; |
| 866 |
Instr operation = instr->Mask(AddSubOpMask); |
| 867 |
|
| 868 |
switch (operation) { |
| 869 |
case ADD: |
| 870 |
case ADDS: { |
| 871 |
new_val = AddWithCarry(reg_size, |
| 872 |
set_flags, |
| 873 |
reg(reg_size, instr->Rn(), instr->RnMode()), |
| 874 |
op2); |
| 875 |
break; |
| 876 |
} |
| 877 |
case SUB: |
| 878 |
case SUBS: { |
| 879 |
new_val = AddWithCarry(reg_size, |
| 880 |
set_flags, |
| 881 |
reg(reg_size, instr->Rn(), instr->RnMode()), |
| 882 |
~op2, |
| 883 |
1); |
| 884 |
break; |
| 885 |
} |
| 886 |
default: VIXL_UNREACHABLE(); |
| 887 |
} |
| 888 |
|
| 889 |
set_reg(reg_size, instr->Rd(), new_val, LogRegWrites, instr->RdMode()); |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
void Simulator::VisitAddSubShifted(const Instruction* instr) { |
| 894 |
unsigned reg_size = instr->SixtyFourBits() ? kXRegSize : kWRegSize; |
| 895 |
int64_t op2 = ShiftOperand(reg_size, |
| 896 |
reg(reg_size, instr->Rm()), |
| 897 |
static_cast<Shift>(instr->ShiftDP()), |
| 898 |
instr->ImmDPShift()); |
| 899 |
AddSubHelper(instr, op2); |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
void Simulator::VisitAddSubImmediate(const Instruction* instr) { |
| 904 |
int64_t op2 = instr->ImmAddSub() << ((instr->ShiftAddSub() == 1) ? 12 : 0); |
| 905 |
AddSubHelper(instr, op2); |
| 906 |
} |
| 907 |
|
| 908 |
|
| 909 |
void Simulator::VisitAddSubExtended(const Instruction* instr) { |
| 910 |
unsigned reg_size = instr->SixtyFourBits() ? kXRegSize : kWRegSize; |
| 911 |
int64_t op2 = ExtendValue(reg_size, |
| 912 |
reg(reg_size, instr->Rm()), |
| 913 |
static_cast<Extend>(instr->ExtendMode()), |
| 914 |
instr->ImmExtendShift()); |
| 915 |
AddSubHelper(instr, op2); |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
void Simulator::VisitAddSubWithCarry(const Instruction* instr) { |
| 920 |
unsigned reg_size = instr->SixtyFourBits() ? kXRegSize : kWRegSize; |
| 921 |
int64_t op2 = reg(reg_size, instr->Rm()); |
| 922 |
int64_t new_val; |
| 923 |
|
| 924 |
if ((instr->Mask(AddSubOpMask) == SUB) || instr->Mask(AddSubOpMask) == SUBS) { |
| 925 |
op2 = ~op2; |
| 926 |
} |
| 927 |
|
| 928 |
new_val = AddWithCarry(reg_size, |
| 929 |
instr->FlagsUpdate(), |
| 930 |
reg(reg_size, instr->Rn()), |
| 931 |
op2, |
| 932 |
C()); |
| 933 |
|
| 934 |
set_reg(reg_size, instr->Rd(), new_val); |
| 935 |
} |
| 936 |
|
| 937 |
|
| 938 |
void Simulator::VisitLogicalShifted(const Instruction* instr) { |
| 939 |
unsigned reg_size = instr->SixtyFourBits() ? kXRegSize : kWRegSize; |
| 940 |
Shift shift_type = static_cast<Shift>(instr->ShiftDP()); |
| 941 |
unsigned shift_amount = instr->ImmDPShift(); |
| 942 |
int64_t op2 = ShiftOperand(reg_size, reg(reg_size, instr->Rm()), shift_type, |
| 943 |
shift_amount); |
| 944 |
if (instr->Mask(NOT) == NOT) { |
| 945 |
op2 = ~op2; |
| 946 |
} |
| 947 |
LogicalHelper(instr, op2); |
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
void Simulator::VisitLogicalImmediate(const Instruction* instr) { |
| 952 |
LogicalHelper(instr, instr->ImmLogical()); |
| 953 |
} |
| 954 |
|
| 955 |
|
| 956 |
void Simulator::LogicalHelper(const Instruction* instr, int64_t op2) { |
| 957 |
unsigned reg_size = instr->SixtyFourBits() ? kXRegSize : kWRegSize; |
| 958 |
int64_t op1 = reg(reg_size, instr->Rn()); |
| 959 |
int64_t result = 0; |
| 960 |
bool update_flags = false; |
| 961 |
|
| 962 |
// Switch on the logical operation, stripping out the NOT bit, as it has a |
| 963 |
// different meaning for logical immediate instructions. |
| 964 |
switch (instr->Mask(LogicalOpMask & ~NOT)) { |
| 965 |
case ANDS: update_flags = true; VIXL_FALLTHROUGH(); |
| 966 |
case AND: result = op1 & op2; break; |
| 967 |
case ORR: result = op1 | op2; break; |
| 968 |
case EOR: result = op1 ^ op2; break; |
| 969 |
default: |
| 970 |
VIXL_UNIMPLEMENTED(); |
| 971 |
} |
| 972 |
|
| 973 |
if (update_flags) { |
| 974 |
nzcv().SetN(CalcNFlag(result, reg_size)); |
| 975 |
nzcv().SetZ(CalcZFlag(result)); |
| 976 |
nzcv().SetC(0); |
| 977 |
nzcv().SetV(0); |
| 978 |
LogSystemRegister(NZCV); |
| 979 |
} |
| 980 |
|
| 981 |
set_reg(reg_size, instr->Rd(), result, LogRegWrites, instr->RdMode()); |
| 982 |
} |
| 983 |
|
| 984 |
|
| 985 |
void Simulator::VisitConditionalCompareRegister(const Instruction* instr) { |
| 986 |
unsigned reg_size = instr->SixtyFourBits() ? kXRegSize : kWRegSize; |
| 987 |
ConditionalCompareHelper(instr, reg(reg_size, instr->Rm())); |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
void Simulator::VisitConditionalCompareImmediate(const Instruction* instr) { |
| 992 |
ConditionalCompareHelper(instr, instr->ImmCondCmp()); |
| 993 |
} |
| 994 |
|
| 995 |
|
| 996 |
void Simulator::ConditionalCompareHelper(const Instruction* instr, |
| 997 |
int64_t op2) { |
| 998 |
unsigned reg_size = instr->SixtyFourBits() ? kXRegSize : kWRegSize; |
| 999 |
int64_t op1 = reg(reg_size, instr->Rn()); |
| 1000 |
|
| 1001 |
if (ConditionPassed(instr->Condition())) { |
| 1002 |
// If the condition passes, set the status flags to the result of comparing |
| 1003 |
// the operands. |
| 1004 |
if (instr->Mask(ConditionalCompareMask) == CCMP) { |
| 1005 |
AddWithCarry(reg_size, true, op1, ~op2, 1); |
| 1006 |
} else { |
| 1007 |
VIXL_ASSERT(instr->Mask(ConditionalCompareMask) == CCMN); |
| 1008 |
AddWithCarry(reg_size, true, op1, op2, 0); |
| 1009 |
} |
| 1010 |
} else { |
| 1011 |
// If the condition fails, set the status flags to the nzcv immediate. |
| 1012 |
nzcv().SetFlags(instr->Nzcv()); |
| 1013 |
LogSystemRegister(NZCV); |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
void Simulator::VisitLoadStoreUnsignedOffset(const Instruction* instr) { |
| 1019 |
int offset = instr->ImmLSUnsigned() << instr->SizeLS(); |
| 1020 |
LoadStoreHelper(instr, offset, Offset); |
| 1021 |
} |
| 1022 |
|
| 1023 |
|
| 1024 |
void Simulator::VisitLoadStoreUnscaledOffset(const Instruction* instr) { |
| 1025 |
LoadStoreHelper(instr, instr->ImmLS(), Offset); |
| 1026 |
} |
| 1027 |
|
| 1028 |
|
| 1029 |
void Simulator::VisitLoadStorePreIndex(const Instruction* instr) { |
| 1030 |
LoadStoreHelper(instr, instr->ImmLS(), PreIndex); |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
void Simulator::VisitLoadStorePostIndex(const Instruction* instr) { |
| 1035 |
LoadStoreHelper(instr, instr->ImmLS(), PostIndex); |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
void Simulator::VisitLoadStoreRegisterOffset(const Instruction* instr) { |
| 1040 |
Extend ext = static_cast<Extend>(instr->ExtendMode()); |
| 1041 |
VIXL_ASSERT((ext == UXTW) || (ext == UXTX) || (ext == SXTW) || (ext == SXTX)); |
| 1042 |
unsigned shift_amount = instr->ImmShiftLS() * instr->SizeLS(); |
| 1043 |
|
| 1044 |
int64_t offset = ExtendValue(kXRegSize, xreg(instr->Rm()), ext, |
| 1045 |
shift_amount); |
| 1046 |
LoadStoreHelper(instr, offset, Offset); |
| 1047 |
} |
| 1048 |
|
| 1049 |
template<typename T> |
| 1050 |
static T Faulted() { |
| 1051 |
return ~0; |
| 1052 |
} |
| 1053 |
|
| 1054 |
template<> |
| 1055 |
Simulator::qreg_t Faulted() { |
| 1056 |
static_assert(kQRegSizeInBytes == 16, "Known constraint"); |
| 1057 |
static Simulator::qreg_t dummy = { { |
| 1058 |
255, 255, 255, 255, 255, 255, 255, 255, |
| 1059 |
255, 255, 255, 255, 255, 255, 255, 255 |
| 1060 |
} }; |
| 1061 |
return dummy; |
| 1062 |
} |
| 1063 |
|
| 1064 |
template<typename T> T |
| 1065 |
Simulator::Read(uintptr_t address) |
| 1066 |
{ |
| 1067 |
address = Memory::AddressUntag(address); |
| 1068 |
if (handle_wasm_seg_fault(address, sizeof(T))) |
| 1069 |
return Faulted<T>(); |
| 1070 |
return Memory::Read<T>(address); |
| 1071 |
} |
| 1072 |
|
| 1073 |
template <typename T> void |
| 1074 |
Simulator::Write(uintptr_t address, T value) |
| 1075 |
{ |
| 1076 |
address = Memory::AddressUntag(address); |
| 1077 |
if (handle_wasm_seg_fault(address, sizeof(T))) |
| 1078 |
return; |
| 1079 |
Memory::Write<T>(address, value); |
| 1080 |
} |
| 1081 |
|
| 1082 |
void Simulator::LoadStoreHelper(const Instruction* instr, |
| 1083 |
int64_t offset, |
| 1084 |
AddrMode addrmode) { |
| 1085 |
unsigned srcdst = instr->Rt(); |
| 1086 |
uintptr_t address = AddressModeHelper(instr->Rn(), offset, addrmode); |
| 1087 |
|
| 1088 |
LoadStoreOp op = static_cast<LoadStoreOp>(instr->Mask(LoadStoreMask)); |
| 1089 |
switch (op) { |
| 1090 |
case LDRB_w: |
| 1091 |
set_wreg(srcdst, Read<uint8_t>(address), NoRegLog); break; |
| 1092 |
case LDRH_w: |
| 1093 |
set_wreg(srcdst, Read<uint16_t>(address), NoRegLog); break; |
| 1094 |
case LDR_w: |
| 1095 |
set_wreg(srcdst, Read<uint32_t>(address), NoRegLog); break; |
| 1096 |
case LDR_x: |
| 1097 |
set_xreg(srcdst, Read<uint64_t>(address), NoRegLog); break; |
| 1098 |
case LDRSB_w: |
| 1099 |
set_wreg(srcdst, Read<int8_t>(address), NoRegLog); break; |
| 1100 |
case LDRSH_w: |
| 1101 |
set_wreg(srcdst, Read<int16_t>(address), NoRegLog); break; |
| 1102 |
case LDRSB_x: |
| 1103 |
set_xreg(srcdst, Read<int8_t>(address), NoRegLog); break; |
| 1104 |
case LDRSH_x: |
| 1105 |
set_xreg(srcdst, Read<int16_t>(address), NoRegLog); break; |
| 1106 |
case LDRSW_x: |
| 1107 |
set_xreg(srcdst, Read<int32_t>(address), NoRegLog); break; |
| 1108 |
case LDR_b: |
| 1109 |
set_breg(srcdst, Read<uint8_t>(address), NoRegLog); break; |
| 1110 |
case LDR_h: |
| 1111 |
set_hreg(srcdst, Read<uint16_t>(address), NoRegLog); break; |
| 1112 |
case LDR_s: |
| 1113 |
set_sreg(srcdst, Read<float>(address), NoRegLog); break; |
| 1114 |
case LDR_d: |
| 1115 |
set_dreg(srcdst, Read<double>(address), NoRegLog); break; |
| 1116 |
case LDR_q: |
| 1117 |
set_qreg(srcdst, Read<qreg_t>(address), NoRegLog); break; |
| 1118 |
|
| 1119 |
case STRB_w: Write<uint8_t>(address, wreg(srcdst)); break; |
| 1120 |
case STRH_w: Write<uint16_t>(address, wreg(srcdst)); break; |
| 1121 |
case STR_w: Write<uint32_t>(address, wreg(srcdst)); break; |
| 1122 |
case STR_x: Write<uint64_t>(address, xreg(srcdst)); break; |
| 1123 |
case STR_b: Write<uint8_t>(address, breg(srcdst)); break; |
| 1124 |
case STR_h: Write<uint16_t>(address, hreg(srcdst)); break; |
| 1125 |
case STR_s: Write<float>(address, sreg(srcdst)); break; |
| 1126 |
case STR_d: Write<double>(address, dreg(srcdst)); break; |
| 1127 |
case STR_q: Write<qreg_t>(address, qreg(srcdst)); break; |
| 1128 |
|
| 1129 |
// Ignore prfm hint instructions. |
| 1130 |
case PRFM: break; |
| 1131 |
|
| 1132 |
default: VIXL_UNIMPLEMENTED(); |
| 1133 |
} |
| 1134 |
|
| 1135 |
unsigned access_size = 1 << instr->SizeLS(); |
| 1136 |
if (instr->IsLoad()) { |
| 1137 |
if ((op == LDR_s) || (op == LDR_d)) { |
| 1138 |
LogVRead(address, srcdst, GetPrintRegisterFormatForSizeFP(access_size)); |
| 1139 |
} else if ((op == LDR_b) || (op == LDR_h) || (op == LDR_q)) { |
| 1140 |
LogVRead(address, srcdst, GetPrintRegisterFormatForSize(access_size)); |
| 1141 |
} else { |
| 1142 |
LogRead(address, srcdst, GetPrintRegisterFormatForSize(access_size)); |
| 1143 |
} |
| 1144 |
} else { |
| 1145 |
if ((op == STR_s) || (op == STR_d)) { |
| 1146 |
LogVWrite(address, srcdst, GetPrintRegisterFormatForSizeFP(access_size)); |
| 1147 |
} else if ((op == STR_b) || (op == STR_h) || (op == STR_q)) { |
| 1148 |
LogVWrite(address, srcdst, GetPrintRegisterFormatForSize(access_size)); |
| 1149 |
} else { |
| 1150 |
LogWrite(address, srcdst, GetPrintRegisterFormatForSize(access_size)); |
| 1151 |
} |
| 1152 |
} |
| 1153 |
|
| 1154 |
local_monitor_.MaybeClear(); |
| 1155 |
} |
| 1156 |
|
| 1157 |
|
| 1158 |
void Simulator::VisitLoadStorePairOffset(const Instruction* instr) { |
| 1159 |
LoadStorePairHelper(instr, Offset); |
| 1160 |
} |
| 1161 |
|
| 1162 |
|
| 1163 |
void Simulator::VisitLoadStorePairPreIndex(const Instruction* instr) { |
| 1164 |
LoadStorePairHelper(instr, PreIndex); |
| 1165 |
} |
| 1166 |
|
| 1167 |
|
| 1168 |
void Simulator::VisitLoadStorePairPostIndex(const Instruction* instr) { |
| 1169 |
LoadStorePairHelper(instr, PostIndex); |
| 1170 |
} |
| 1171 |
|
| 1172 |
|
| 1173 |
void Simulator::VisitLoadStorePairNonTemporal(const Instruction* instr) { |
| 1174 |
LoadStorePairHelper(instr, Offset); |
| 1175 |
} |
| 1176 |
|
| 1177 |
|
| 1178 |
void Simulator::LoadStorePairHelper(const Instruction* instr, |
| 1179 |
AddrMode addrmode) { |
| 1180 |
unsigned rt = instr->Rt(); |
| 1181 |
unsigned rt2 = instr->Rt2(); |
| 1182 |
int element_size = 1 << instr->SizeLSPair(); |
| 1183 |
int64_t offset = instr->ImmLSPair() * element_size; |
| 1184 |
uintptr_t address = AddressModeHelper(instr->Rn(), offset, addrmode); |
| 1185 |
uintptr_t address2 = address + element_size; |
| 1186 |
|
| 1187 |
LoadStorePairOp op = |
| 1188 |
static_cast<LoadStorePairOp>(instr->Mask(LoadStorePairMask)); |
| 1189 |
|
| 1190 |
// 'rt' and 'rt2' can only be aliased for stores. |
| 1191 |
VIXL_ASSERT(((op & LoadStorePairLBit) == 0) || (rt != rt2)); |
| 1192 |
|
| 1193 |
switch (op) { |
| 1194 |
// Use NoRegLog to suppress the register trace (LOG_REGS, LOG_FP_REGS). We |
| 1195 |
// will print a more detailed log. |
| 1196 |
case LDP_w: { |
| 1197 |
set_wreg(rt, Read<uint32_t>(address), NoRegLog); |
| 1198 |
set_wreg(rt2, Read<uint32_t>(address2), NoRegLog); |
| 1199 |
break; |
| 1200 |
} |
| 1201 |
case LDP_s: { |
| 1202 |
set_sreg(rt, Read<float>(address), NoRegLog); |
| 1203 |
set_sreg(rt2, Read<float>(address2), NoRegLog); |
| 1204 |
break; |
| 1205 |
} |
| 1206 |
case LDP_x: { |
| 1207 |
set_xreg(rt, Read<uint64_t>(address), NoRegLog); |
| 1208 |
set_xreg(rt2, Read<uint64_t>(address2), NoRegLog); |
| 1209 |
break; |
| 1210 |
} |
| 1211 |
case LDP_d: { |
| 1212 |
set_dreg(rt, Read<double>(address), NoRegLog); |
| 1213 |
set_dreg(rt2, Read<double>(address2), NoRegLog); |
| 1214 |
break; |
| 1215 |
} |
| 1216 |
case LDP_q: { |
| 1217 |
set_qreg(rt, Read<qreg_t>(address), NoRegLog); |
| 1218 |
set_qreg(rt2, Read<qreg_t>(address2), NoRegLog); |
| 1219 |
break; |
| 1220 |
} |
| 1221 |
case LDPSW_x: { |
| 1222 |
set_xreg(rt, Read<int32_t>(address), NoRegLog); |
| 1223 |
set_xreg(rt2, Read<int32_t>(address2), NoRegLog); |
| 1224 |
break; |
| 1225 |
} |
| 1226 |
case STP_w: { |
| 1227 |
Write<uint32_t>(address, wreg(rt)); |
| 1228 |
Write<uint32_t>(address2, wreg(rt2)); |
| 1229 |
break; |
| 1230 |
} |
| 1231 |
case STP_s: { |
| 1232 |
Write<float>(address, sreg(rt)); |
| 1233 |
Write<float>(address2, sreg(rt2)); |
| 1234 |
break; |
| 1235 |
} |
| 1236 |
case STP_x: { |
| 1237 |
Write<uint64_t>(address, xreg(rt)); |
| 1238 |
Write<uint64_t>(address2, xreg(rt2)); |
| 1239 |
break; |
| 1240 |
} |
| 1241 |
case STP_d: { |
| 1242 |
Write<double>(address, dreg(rt)); |
| 1243 |
Write<double>(address2, dreg(rt2)); |
| 1244 |
break; |
| 1245 |
} |
| 1246 |
case STP_q: { |
| 1247 |
Write<qreg_t>(address, qreg(rt)); |
| 1248 |
Write<qreg_t>(address2, qreg(rt2)); |
| 1249 |
break; |
| 1250 |
} |
| 1251 |
default: VIXL_UNREACHABLE(); |
| 1252 |
} |
| 1253 |
|
| 1254 |
// Print a detailed trace (including the memory address) instead of the basic |
| 1255 |
// register:value trace generated by set_*reg(). |
| 1256 |
if (instr->IsLoad()) { |
| 1257 |
if ((op == LDP_s) || (op == LDP_d)) { |
| 1258 |
LogVRead(address, rt, GetPrintRegisterFormatForSizeFP(element_size)); |
| 1259 |
LogVRead(address2, rt2, GetPrintRegisterFormatForSizeFP(element_size)); |
| 1260 |
} else if (op == LDP_q) { |
| 1261 |
LogVRead(address, rt, GetPrintRegisterFormatForSize(element_size)); |
| 1262 |
LogVRead(address2, rt2, GetPrintRegisterFormatForSize(element_size)); |
| 1263 |
} else { |
| 1264 |
LogRead(address, rt, GetPrintRegisterFormatForSize(element_size)); |
| 1265 |
LogRead(address2, rt2, GetPrintRegisterFormatForSize(element_size)); |
| 1266 |
} |
| 1267 |
} else { |
| 1268 |
if ((op == STP_s) || (op == STP_d)) { |
| 1269 |
LogVWrite(address, rt, GetPrintRegisterFormatForSizeFP(element_size)); |
| 1270 |
LogVWrite(address2, rt2, GetPrintRegisterFormatForSizeFP(element_size)); |
| 1271 |
} else if (op == STP_q) { |
| 1272 |
LogVWrite(address, rt, GetPrintRegisterFormatForSize(element_size)); |
| 1273 |
LogVWrite(address2, rt2, GetPrintRegisterFormatForSize(element_size)); |
| 1274 |
} else { |
| 1275 |
LogWrite(address, rt, GetPrintRegisterFormatForSize(element_size)); |
| 1276 |
LogWrite(address2, rt2, GetPrintRegisterFormatForSize(element_size)); |
| 1277 |
} |
| 1278 |
} |
| 1279 |
|
| 1280 |
local_monitor_.MaybeClear(); |
| 1281 |
} |
| 1282 |
|
| 1283 |
|
| 1284 |
void Simulator::PrintExclusiveAccessWarning() { |
| 1285 |
if (print_exclusive_access_warning_) { |
| 1286 |
fprintf( |
| 1287 |
stderr, |
| 1288 |
"%sWARNING:%s VIXL simulator support for load-/store-/clear-exclusive " |
| 1289 |
"instructions is limited. Refer to the README for details.%s\n", |
| 1290 |
clr_warning, clr_warning_message, clr_normal); |
| 1291 |
print_exclusive_access_warning_ = false; |
| 1292 |
} |
| 1293 |
} |
| 1294 |
|
| 1295 |
template <typename T> |
| 1296 |
void Simulator::CompareAndSwapHelper(const Instruction* instr) { |
| 1297 |
unsigned rs = instr->Rs(); |
| 1298 |
unsigned rt = instr->Rt(); |
| 1299 |
unsigned rn = instr->Rn(); |
| 1300 |
|
| 1301 |
unsigned element_size = sizeof(T); |
| 1302 |
uint64_t address = reg<uint64_t>(rn, Reg31IsStackPointer); |
| 1303 |
|
| 1304 |
// Verify that the address is available to the host. |
| 1305 |
VIXL_ASSERT(address == static_cast<uintptr_t>(address)); |
| 1306 |
|
| 1307 |
address = Memory::AddressUntag(address); |
| 1308 |
if (handle_wasm_seg_fault(address, element_size)) |
| 1309 |
return; |
| 1310 |
|
| 1311 |
bool is_acquire = instr->Bit(22) == 1; |
| 1312 |
bool is_release = instr->Bit(15) == 1; |
| 1313 |
|
| 1314 |
T comparevalue = reg<T>(rs); |
| 1315 |
T newvalue = reg<T>(rt); |
| 1316 |
|
| 1317 |
// The architecture permits that the data read clears any exclusive monitors |
| 1318 |
// associated with that location, even if the compare subsequently fails. |
| 1319 |
local_monitor_.Clear(); |
| 1320 |
|
| 1321 |
T data = Memory::Read<T>(address); |
| 1322 |
if (is_acquire) { |
| 1323 |
// Approximate load-acquire by issuing a full barrier after the load. |
| 1324 |
__sync_synchronize(); |
| 1325 |
} |
| 1326 |
|
| 1327 |
if (data == comparevalue) { |
| 1328 |
if (is_release) { |
| 1329 |
// Approximate store-release by issuing a full barrier before the store. |
| 1330 |
__sync_synchronize(); |
| 1331 |
} |
| 1332 |
Memory::Write<T>(address, newvalue); |
| 1333 |
LogWrite(address, rt, GetPrintRegisterFormatForSize(element_size)); |
| 1334 |
} |
| 1335 |
set_reg<T>(rs, data); |
| 1336 |
LogRead(address, rs, GetPrintRegisterFormatForSize(element_size)); |
| 1337 |
} |
| 1338 |
|
| 1339 |
template <typename T> |
| 1340 |
void Simulator::CompareAndSwapPairHelper(const Instruction* instr) { |
| 1341 |
VIXL_ASSERT((sizeof(T) == 4) || (sizeof(T) == 8)); |
| 1342 |
unsigned rs = instr->Rs(); |
| 1343 |
unsigned rt = instr->Rt(); |
| 1344 |
unsigned rn = instr->Rn(); |
| 1345 |
|
| 1346 |
VIXL_ASSERT((rs % 2 == 0) && (rs % 2 == 0)); |
| 1347 |
|
| 1348 |
unsigned element_size = sizeof(T); |
| 1349 |
uint64_t address = reg<uint64_t>(rn, Reg31IsStackPointer); |
| 1350 |
|
| 1351 |
// Verify that the address is available to the host. |
| 1352 |
VIXL_ASSERT(address == static_cast<uintptr_t>(address)); |
| 1353 |
|
| 1354 |
address = Memory::AddressUntag(address); |
| 1355 |
if (handle_wasm_seg_fault(address, element_size)) |
| 1356 |
return; |
| 1357 |
|
| 1358 |
uint64_t address2 = address + element_size; |
| 1359 |
|
| 1360 |
bool is_acquire = instr->Bit(22) == 1; |
| 1361 |
bool is_release = instr->Bit(15) == 1; |
| 1362 |
|
| 1363 |
T comparevalue_high = reg<T>(rs + 1); |
| 1364 |
T comparevalue_low = reg<T>(rs); |
| 1365 |
T newvalue_high = reg<T>(rt + 1); |
| 1366 |
T newvalue_low = reg<T>(rt); |
| 1367 |
|
| 1368 |
// The architecture permits that the data read clears any exclusive monitors |
| 1369 |
// associated with that location, even if the compare subsequently fails. |
| 1370 |
local_monitor_.Clear(); |
| 1371 |
|
| 1372 |
T data_high = Memory::Read<T>(address); |
| 1373 |
T data_low = Memory::Read<T>(address2); |
| 1374 |
|
| 1375 |
if (is_acquire) { |
| 1376 |
// Approximate load-acquire by issuing a full barrier after the load. |
| 1377 |
__sync_synchronize(); |
| 1378 |
} |
| 1379 |
|
| 1380 |
bool same = |
| 1381 |
(data_high == comparevalue_high) && (data_low == comparevalue_low); |
| 1382 |
if (same) { |
| 1383 |
if (is_release) { |
| 1384 |
// Approximate store-release by issuing a full barrier before the store. |
| 1385 |
__sync_synchronize(); |
| 1386 |
} |
| 1387 |
|
| 1388 |
Memory::Write<T>(address, newvalue_high); |
| 1389 |
Memory::Write<T>(address2, newvalue_low); |
| 1390 |
} |
| 1391 |
|
| 1392 |
set_reg<T>(rs + 1, data_high); |
| 1393 |
set_reg<T>(rs, data_low); |
| 1394 |
|
| 1395 |
LogRead(address, rs + 1, GetPrintRegisterFormatForSize(element_size)); |
| 1396 |
LogRead(address2, rs, GetPrintRegisterFormatForSize(element_size)); |
| 1397 |
|
| 1398 |
if (same) { |
| 1399 |
LogWrite(address, rt + 1, GetPrintRegisterFormatForSize(element_size)); |
| 1400 |
LogWrite(address2, rt, GetPrintRegisterFormatForSize(element_size)); |
| 1401 |
} |
| 1402 |
} |
| 1403 |
|
| 1404 |
void Simulator::VisitLoadStoreExclusive(const Instruction* instr) { |
| 1405 |
LoadStoreExclusive op = |
| 1406 |
static_cast<LoadStoreExclusive>(instr->Mask(LoadStoreExclusiveMask)); |
| 1407 |
|
| 1408 |
switch (op) { |
| 1409 |
case CAS_w: |
| 1410 |
case CASA_w: |
| 1411 |
case CASL_w: |
| 1412 |
case CASAL_w: |
| 1413 |
CompareAndSwapHelper<uint32_t>(instr); |
| 1414 |
break; |
| 1415 |
case CAS_x: |
| 1416 |
case CASA_x: |
| 1417 |
case CASL_x: |
| 1418 |
case CASAL_x: |
| 1419 |
CompareAndSwapHelper<uint64_t>(instr); |
| 1420 |
break; |
| 1421 |
case CASB: |
| 1422 |
case CASAB: |
| 1423 |
case CASLB: |
| 1424 |
case CASALB: |
| 1425 |
CompareAndSwapHelper<uint8_t>(instr); |
| 1426 |
break; |
| 1427 |
case CASH: |
| 1428 |
case CASAH: |
| 1429 |
case CASLH: |
| 1430 |
case CASALH: |
| 1431 |
CompareAndSwapHelper<uint16_t>(instr); |
| 1432 |
break; |
| 1433 |
case CASP_w: |
| 1434 |
case CASPA_w: |
| 1435 |
case CASPL_w: |
| 1436 |
case CASPAL_w: |
| 1437 |
CompareAndSwapPairHelper<uint32_t>(instr); |
| 1438 |
break; |
| 1439 |
case CASP_x: |
| 1440 |
case CASPA_x: |
| 1441 |
case CASPL_x: |
| 1442 |
case CASPAL_x: |
| 1443 |
CompareAndSwapPairHelper<uint64_t>(instr); |
| 1444 |
break; |
| 1445 |
default: |
| 1446 |
PrintExclusiveAccessWarning(); |
| 1447 |
|
| 1448 |
unsigned rs = instr->Rs(); |
| 1449 |
unsigned rt = instr->Rt(); |
| 1450 |
unsigned rt2 = instr->Rt2(); |
| 1451 |
unsigned rn = instr->Rn(); |
| 1452 |
|
| 1453 |
bool is_exclusive = !instr->LdStXNotExclusive(); |
| 1454 |
bool is_acquire_release = !is_exclusive || instr->LdStXAcquireRelease(); |
| 1455 |
bool is_load = instr->LdStXLoad(); |
| 1456 |
bool is_pair = instr->LdStXPair(); |
| 1457 |
|
| 1458 |
unsigned element_size = 1 << instr->LdStXSizeLog2(); |
| 1459 |
unsigned access_size = is_pair ? element_size * 2 : element_size; |
| 1460 |
uint64_t address = reg<uint64_t>(rn, Reg31IsStackPointer); |
| 1461 |
|
| 1462 |
// Verify that the address is available to the host. |
| 1463 |
VIXL_ASSERT(address == static_cast<uintptr_t>(address)); |
| 1464 |
|
| 1465 |
// Check the alignment of `address`. |
| 1466 |
if (AlignDown(address, access_size) != address) { |
| 1467 |
VIXL_ALIGNMENT_EXCEPTION(); |
| 1468 |
} |
| 1469 |
|
| 1470 |
// The sp must be aligned to 16 bytes when it is accessed. |
| 1471 |
if ((rn == 31) && (AlignDown(address, 16) != address)) { |
| 1472 |
VIXL_ALIGNMENT_EXCEPTION(); |
| 1473 |
} |
| 1474 |
|
| 1475 |
if (is_load) { |
| 1476 |
if (is_exclusive) { |
| 1477 |
local_monitor_.MarkExclusive(address, access_size); |
| 1478 |
} else { |
| 1479 |
// Any non-exclusive load can clear the local monitor as a side |
| 1480 |
// effect. We don't need to do this, but it is useful to stress the |
| 1481 |
// simulated code. |
| 1482 |
local_monitor_.Clear(); |
| 1483 |
} |
| 1484 |
|
| 1485 |
// Use NoRegLog to suppress the register trace (LOG_REGS, LOG_FP_REGS). |
| 1486 |
// We will print a more detailed log. |
| 1487 |
switch (op) { |
| 1488 |
case LDXRB_w: |
| 1489 |
case LDAXRB_w: |
| 1490 |
case LDARB_w: |
| 1491 |
set_wreg(rt, Read<uint8_t>(address), NoRegLog); |
| 1492 |
break; |
| 1493 |
case LDXRH_w: |
| 1494 |
case LDAXRH_w: |
| 1495 |
case LDARH_w: |
| 1496 |
set_wreg(rt, Read<uint16_t>(address), NoRegLog); |
| 1497 |
break; |
| 1498 |
case LDXR_w: |
| 1499 |
case LDAXR_w: |
| 1500 |
case LDAR_w: |
| 1501 |
set_wreg(rt, Read<uint32_t>(address), NoRegLog); |
| 1502 |
break; |
| 1503 |
case LDXR_x: |
| 1504 |
case LDAXR_x: |
| 1505 |
case LDAR_x: |
| 1506 |
set_xreg(rt, Read<uint64_t>(address), NoRegLog); |
| 1507 |
break; |
| 1508 |
case LDXP_w: |
| 1509 |
case LDAXP_w: |
| 1510 |
set_wreg(rt, Read<uint32_t>(address), NoRegLog); |
| 1511 |
set_wreg(rt2, Read<uint32_t>(address + element_size), NoRegLog); |
| 1512 |
break; |
| 1513 |
case LDXP_x: |
| 1514 |
case LDAXP_x: |
| 1515 |
set_xreg(rt, Read<uint64_t>(address), NoRegLog); |
| 1516 |
set_xreg(rt2, Read<uint64_t>(address + element_size), NoRegLog); |
| 1517 |
break; |
| 1518 |
default: |
| 1519 |
VIXL_UNREACHABLE(); |
| 1520 |
} |
| 1521 |
|
| 1522 |
if (is_acquire_release) { |
| 1523 |
// Approximate load-acquire by issuing a full barrier after the load. |
| 1524 |
js::jit::AtomicOperations::fenceSeqCst(); |
| 1525 |
} |
| 1526 |
|
| 1527 |
LogRead(address, rt, GetPrintRegisterFormatForSize(element_size)); |
| 1528 |
if (is_pair) { |
| 1529 |
LogRead(address + element_size, rt2, |
| 1530 |
GetPrintRegisterFormatForSize(element_size)); |
| 1531 |
} |
| 1532 |
} else { |
| 1533 |
if (is_acquire_release) { |
| 1534 |
// Approximate store-release by issuing a full barrier before the |
| 1535 |
// store. |
| 1536 |
js::jit::AtomicOperations::fenceSeqCst(); |
| 1537 |
} |
| 1538 |
|
| 1539 |
bool do_store = true; |
| 1540 |
if (is_exclusive) { |
| 1541 |
do_store = local_monitor_.IsExclusive(address, access_size) && |
| 1542 |
global_monitor_.IsExclusive(address, access_size); |
| 1543 |
set_wreg(rs, do_store ? 0 : 1); |
| 1544 |
|
| 1545 |
// - All exclusive stores explicitly clear the local monitor. |
| 1546 |
local_monitor_.Clear(); |
| 1547 |
} else { |
| 1548 |
// - Any other store can clear the local monitor as a side effect. |
| 1549 |
local_monitor_.MaybeClear(); |
| 1550 |
} |
| 1551 |
|
| 1552 |
if (do_store) { |
| 1553 |
switch (op) { |
| 1554 |
case STXRB_w: |
| 1555 |
case STLXRB_w: |
| 1556 |
case STLRB_w: |
| 1557 |
Write<uint8_t>(address, wreg(rt)); |
| 1558 |
break; |
| 1559 |
case STXRH_w: |
| 1560 |
case STLXRH_w: |
| 1561 |
case STLRH_w: |
| 1562 |
Write<uint16_t>(address, wreg(rt)); |
| 1563 |
break; |
| 1564 |
case STXR_w: |
| 1565 |
case STLXR_w: |
| 1566 |
case STLR_w: |
| 1567 |
Write<uint32_t>(address, wreg(rt)); |
| 1568 |
break; |
| 1569 |
case STXR_x: |
| 1570 |
case STLXR_x: |
| 1571 |
case STLR_x: |
| 1572 |
Write<uint64_t>(address, xreg(rt)); |
| 1573 |
break; |
| 1574 |
case STXP_w: |
| 1575 |
case STLXP_w: |
| 1576 |
Write<uint32_t>(address, wreg(rt)); |
| 1577 |
Write<uint32_t>(address + element_size, wreg(rt2)); |
| 1578 |
break; |
| 1579 |
case STXP_x: |
| 1580 |
case STLXP_x: |
| 1581 |
Write<uint64_t>(address, xreg(rt)); |
| 1582 |
Write<uint64_t>(address + element_size, xreg(rt2)); |
| 1583 |
break; |
| 1584 |
default: |
| 1585 |
VIXL_UNREACHABLE(); |
| 1586 |
} |
| 1587 |
|
| 1588 |
LogWrite(address, rt, GetPrintRegisterFormatForSize(element_size)); |
| 1589 |
if (is_pair) { |
| 1590 |
LogWrite(address + element_size, rt2, |
| 1591 |
GetPrintRegisterFormatForSize(element_size)); |
| 1592 |
} |
| 1593 |
} |
| 1594 |
} |
| 1595 |
} |
| 1596 |
} |
| 1597 |
|
| 1598 |
template <typename T> |
| 1599 |
void Simulator::AtomicMemorySimpleHelper(const Instruction* instr) { |
| 1600 |
unsigned rs = instr->Rs(); |
| 1601 |
unsigned rt = instr->Rt(); |
| 1602 |
unsigned rn = instr->Rn(); |
| 1603 |
|
| 1604 |
bool is_acquire = (instr->Bit(23) == 1) && (rt != kZeroRegCode); |
| 1605 |
bool is_release = instr->Bit(22) == 1; |
| 1606 |
|
| 1607 |
unsigned element_size = sizeof(T); |
| 1608 |
uint64_t address = reg<uint64_t>(rn, Reg31IsStackPointer); |
| 1609 |
|
| 1610 |
// Verify that the address is available to the host. |
| 1611 |
VIXL_ASSERT(address == static_cast<uintptr_t>(address)); |
| 1612 |
|
| 1613 |
address = Memory::AddressUntag(address); |
| 1614 |
if (handle_wasm_seg_fault(address, sizeof(T))) |
| 1615 |
return; |
| 1616 |
|
| 1617 |
T value = reg<T>(rs); |
| 1618 |
|
| 1619 |
T data = Memory::Read<T>(address); |
| 1620 |
|
| 1621 |
if (is_acquire) { |
| 1622 |
// Approximate load-acquire by issuing a full barrier after the load. |
| 1623 |
__sync_synchronize(); |
| 1624 |
} |
| 1625 |
|
| 1626 |
T result = 0; |
| 1627 |
switch (instr->Mask(AtomicMemorySimpleOpMask)) { |
| 1628 |
case LDADDOp: |
| 1629 |
result = data + value; |
| 1630 |
break; |
| 1631 |
case LDCLROp: |
| 1632 |
VIXL_ASSERT(!std::numeric_limits<T>::is_signed); |
| 1633 |
result = data & ~value; |
| 1634 |
break; |
| 1635 |
case LDEOROp: |
| 1636 |
VIXL_ASSERT(!std::numeric_limits<T>::is_signed); |
| 1637 |
result = data ^ value; |
| 1638 |
break; |
| 1639 |
case LDSETOp: |
| 1640 |
VIXL_ASSERT(!std::numeric_limits<T>::is_signed); |
| 1641 |
result = data | value; |
| 1642 |
break; |
| 1643 |
|
| 1644 |
// Signed/Unsigned difference is done via the templated type T. |
| 1645 |
case LDSMAXOp: |
| 1646 |
case LDUMAXOp: |
| 1647 |
result = (data > value) ? data : value; |
| 1648 |
break; |
| 1649 |
case LDSMINOp: |
| 1650 |
case LDUMINOp: |
| 1651 |
result = (data > value) ? value : data; |
| 1652 |
break; |
| 1653 |
} |
| 1654 |
|
| 1655 |
if (is_release) { |
| 1656 |
// Approximate store-release by issuing a full barrier before the store. |
| 1657 |
__sync_synchronize(); |
| 1658 |
} |
| 1659 |
|
| 1660 |
Memory::Write<T>(address, result); |
| 1661 |
set_reg<T>(rt, data, NoRegLog); |
| 1662 |
|
| 1663 |
LogRead(address, rt, GetPrintRegisterFormatForSize(element_size)); |
| 1664 |
LogWrite(address, rs, GetPrintRegisterFormatForSize(element_size)); |
| 1665 |
} |
| 1666 |
|
| 1667 |
template <typename T> |
| 1668 |
void Simulator::AtomicMemorySwapHelper(const Instruction* instr) { |
| 1669 |
unsigned rs = instr->Rs(); |
| 1670 |
unsigned rt = instr->Rt(); |
| 1671 |
unsigned rn = instr->Rn(); |
| 1672 |
|
| 1673 |
bool is_acquire = (instr->Bit(23) == 1) && (rt != kZeroRegCode); |
| 1674 |
bool is_release = instr->Bit(22) == 1; |
| 1675 |
|
| 1676 |
unsigned element_size = sizeof(T); |
| 1677 |
uint64_t address = reg<uint64_t>(rn, Reg31IsStackPointer); |
| 1678 |
|
| 1679 |
// Verify that the address is available to the host. |
| 1680 |
VIXL_ASSERT(address == static_cast<uintptr_t>(address)); |
| 1681 |
|
| 1682 |
address = Memory::AddressUntag(address); |
| 1683 |
if (handle_wasm_seg_fault(address, sizeof(T))) |
| 1684 |
return; |
| 1685 |
|
| 1686 |
T data = Memory::Read<T>(address); |
| 1687 |
if (is_acquire) { |
| 1688 |
// Approximate load-acquire by issuing a full barrier after the load. |
| 1689 |
__sync_synchronize(); |
| 1690 |
} |
| 1691 |
|
| 1692 |
if (is_release) { |
| 1693 |
// Approximate store-release by issuing a full barrier before the store. |
| 1694 |
__sync_synchronize(); |
| 1695 |
} |
| 1696 |
Memory::Write<T>(address, reg<T>(rs)); |
| 1697 |
|
| 1698 |
set_reg<T>(rt, data); |
| 1699 |
|
| 1700 |
LogRead(address, rt, GetPrintRegisterFormat(element_size)); |
| 1701 |
LogWrite(address, rs, GetPrintRegisterFormat(element_size)); |
| 1702 |
} |
| 1703 |
|
| 1704 |
template <typename T> |
| 1705 |
void Simulator::LoadAcquireRCpcHelper(const Instruction* instr) { |
| 1706 |
unsigned rt = instr->Rt(); |
| 1707 |
unsigned rn = instr->Rn(); |
| 1708 |
|
| 1709 |
unsigned element_size = sizeof(T); |
| 1710 |
uint64_t address = reg<uint64_t>(rn, Reg31IsStackPointer); |
| 1711 |
|
| 1712 |
// Verify that the address is available to the host. |
| 1713 |
VIXL_ASSERT(address == static_cast<uintptr_t>(address)); |
| 1714 |
|
| 1715 |
address = Memory::AddressUntag(address); |
| 1716 |
if (handle_wasm_seg_fault(address, sizeof(T))) |
| 1717 |
return; |
| 1718 |
|
| 1719 |
set_reg<T>(rt, Memory::Read<T>(address)); |
| 1720 |
|
| 1721 |
// Approximate load-acquire by issuing a full barrier after the load. |
| 1722 |
__sync_synchronize(); |
| 1723 |
|
| 1724 |
LogRead(address, rt, GetPrintRegisterFormat(element_size)); |
| 1725 |
} |
| 1726 |
|
| 1727 |
#define ATOMIC_MEMORY_SIMPLE_UINT_LIST(V) \ |
| 1728 |
V(LDADD) \ |
| 1729 |
V(LDCLR) \ |
| 1730 |
V(LDEOR) \ |
| 1731 |
V(LDSET) \ |
| 1732 |
V(LDUMAX) \ |
| 1733 |
V(LDUMIN) |
| 1734 |
|
| 1735 |
#define ATOMIC_MEMORY_SIMPLE_INT_LIST(V) \ |
| 1736 |
V(LDSMAX) \ |
| 1737 |
V(LDSMIN) |
| 1738 |
|
| 1739 |
void Simulator::VisitAtomicMemory(const Instruction* instr) { |
| 1740 |
switch (instr->Mask(AtomicMemoryMask)) { |
| 1741 |
// clang-format off |
| 1742 |
#define SIM_FUNC_B(A) \ |
| 1743 |
case A##B: \ |
| 1744 |
case A##AB: \ |
| 1745 |
case A##LB: \ |
| 1746 |
case A##ALB: |
| 1747 |
#define SIM_FUNC_H(A) \ |
| 1748 |
case A##H: \ |
| 1749 |
case A##AH: \ |
| 1750 |
case A##LH: \ |
| 1751 |
case A##ALH: |
| 1752 |
#define SIM_FUNC_w(A) \ |
| 1753 |
case A##_w: \ |
| 1754 |
case A##A_w: \ |
| 1755 |
case A##L_w: \ |
| 1756 |
case A##AL_w: |
| 1757 |
#define SIM_FUNC_x(A) \ |
| 1758 |
case A##_x: \ |
| 1759 |
case A##A_x: \ |
| 1760 |
case A##L_x: \ |
| 1761 |
case A##AL_x: |
| 1762 |
|
| 1763 |
ATOMIC_MEMORY_SIMPLE_UINT_LIST(SIM_FUNC_B) |
| 1764 |
AtomicMemorySimpleHelper<uint8_t>(instr); |
| 1765 |
break; |
| 1766 |
ATOMIC_MEMORY_SIMPLE_INT_LIST(SIM_FUNC_B) |
| 1767 |
AtomicMemorySimpleHelper<int8_t>(instr); |
| 1768 |
break; |
| 1769 |
ATOMIC_MEMORY_SIMPLE_UINT_LIST(SIM_FUNC_H) |
| 1770 |
AtomicMemorySimpleHelper<uint16_t>(instr); |
| 1771 |
break; |
| 1772 |
ATOMIC_MEMORY_SIMPLE_INT_LIST(SIM_FUNC_H) |
| 1773 |
AtomicMemorySimpleHelper<int16_t>(instr); |
| 1774 |
break; |
| 1775 |
ATOMIC_MEMORY_SIMPLE_UINT_LIST(SIM_FUNC_w) |
| 1776 |
AtomicMemorySimpleHelper<uint32_t>(instr); |
| 1777 |
break; |
| 1778 |
ATOMIC_MEMORY_SIMPLE_INT_LIST(SIM_FUNC_w) |
| 1779 |
AtomicMemorySimpleHelper<int32_t>(instr); |
| 1780 |
break; |
| 1781 |
ATOMIC_MEMORY_SIMPLE_UINT_LIST(SIM_FUNC_x) |
| 1782 |
AtomicMemorySimpleHelper<uint64_t>(instr); |
| 1783 |
break; |
| 1784 |
ATOMIC_MEMORY_SIMPLE_INT_LIST(SIM_FUNC_x) |
| 1785 |
AtomicMemorySimpleHelper<int64_t>(instr); |
| 1786 |
break; |
| 1787 |
// clang-format on |
| 1788 |
|
| 1789 |
case SWPB: |
| 1790 |
case SWPAB: |
| 1791 |
case SWPLB: |
| 1792 |
case SWPALB: |
| 1793 |
AtomicMemorySwapHelper<uint8_t>(instr); |
| 1794 |
break; |
| 1795 |
case SWPH: |
| 1796 |
case SWPAH: |
| 1797 |
case SWPLH: |
| 1798 |
case SWPALH: |
| 1799 |
AtomicMemorySwapHelper<uint16_t>(instr); |
| 1800 |
break; |
| 1801 |
case SWP_w: |
| 1802 |
case SWPA_w: |
| 1803 |
case SWPL_w: |
| 1804 |
case SWPAL_w: |
| 1805 |
AtomicMemorySwapHelper<uint32_t>(instr); |
| 1806 |
break; |
| 1807 |
case SWP_x: |
| 1808 |
case SWPA_x: |
| 1809 |
case SWPL_x: |
| 1810 |
case SWPAL_x: |
| 1811 |
AtomicMemorySwapHelper<uint64_t>(instr); |
| 1812 |
break; |
| 1813 |
case LDAPRB: |
| 1814 |
LoadAcquireRCpcHelper<uint8_t>(instr); |
| 1815 |
break; |
| 1816 |
case LDAPRH: |
| 1817 |
LoadAcquireRCpcHelper<uint16_t>(instr); |
| 1818 |
break; |
| 1819 |
case LDAPR_w: |
| 1820 |
LoadAcquireRCpcHelper<uint32_t>(instr); |
| 1821 |
break; |
| 1822 |
case LDAPR_x: |
| 1823 |
LoadAcquireRCpcHelper<uint64_t>(instr); |
| 1824 |
break; |
| 1825 |
} |
| 1826 |
} |
| 1827 |
|
| 1828 |
void Simulator::VisitLoadLiteral(const Instruction* instr) { |
| 1829 |
unsigned rt = instr->Rt(); |
| 1830 |
uint64_t address = instr->LiteralAddress<uint64_t>(); |
| 1831 |
|
| 1832 |
// Verify that the calculated address is available to the host. |
| 1833 |
VIXL_ASSERT(address == static_cast<uintptr_t>(address)); |
| 1834 |
|
| 1835 |
switch (instr->Mask(LoadLiteralMask)) { |
| 1836 |
// Use NoRegLog to suppress the register trace (LOG_REGS, LOG_VREGS), then |
| 1837 |
// print a more detailed log. |
| 1838 |
case LDR_w_lit: |
| 1839 |
set_wreg(rt, Read<uint32_t>(address), NoRegLog); |
| 1840 |
LogRead(address, rt, kPrintWReg); |
| 1841 |
break; |
| 1842 |
case LDR_x_lit: |
| 1843 |
set_xreg(rt, Read<uint64_t>(address), NoRegLog); |
| 1844 |
LogRead(address, rt, kPrintXReg); |
| 1845 |
break; |
| 1846 |
case LDR_s_lit: |
| 1847 |
set_sreg(rt, Read<float>(address), NoRegLog); |
| 1848 |
LogVRead(address, rt, kPrintSReg); |
| 1849 |
break; |
| 1850 |
case LDR_d_lit: |
| 1851 |
set_dreg(rt, Read<double>(address), NoRegLog); |
| 1852 |
LogVRead(address, rt, kPrintDReg); |
| 1853 |
break; |
| 1854 |
case LDR_q_lit: |
| 1855 |
set_qreg(rt, Read<qreg_t>(address), NoRegLog); |
| 1856 |
LogVRead(address, rt, kPrintReg1Q); |
| 1857 |
break; |
| 1858 |
case LDRSW_x_lit: |
| 1859 |
set_xreg(rt, Read<int32_t>(address), NoRegLog); |
| 1860 |
LogRead(address, rt, kPrintWReg); |
| 1861 |
break; |
| 1862 |
|
| 1863 |
// Ignore prfm hint instructions. |
| 1864 |
case PRFM_lit: break; |
| 1865 |
|
| 1866 |
default: VIXL_UNREACHABLE(); |
| 1867 |
} |
| 1868 |
|
| 1869 |
local_monitor_.MaybeClear(); |
| 1870 |
} |
| 1871 |
|
| 1872 |
|
| 1873 |
uintptr_t Simulator::AddressModeHelper(unsigned addr_reg, |
| 1874 |
int64_t offset, |
| 1875 |
AddrMode addrmode) { |
| 1876 |
uint64_t address = xreg(addr_reg, Reg31IsStackPointer); |
| 1877 |
|
| 1878 |
if ((addr_reg == 31) && ((address % 16) != 0)) { |
| 1879 |
// When the base register is SP the stack pointer is required to be |
| 1880 |
// quadword aligned prior to the address calculation and write-backs. |
| 1881 |
// Misalignment will cause a stack alignment fault. |
| 1882 |
VIXL_ALIGNMENT_EXCEPTION(); |
| 1883 |
} |
| 1884 |
|
| 1885 |
if ((addrmode == PreIndex) || (addrmode == PostIndex)) { |
| 1886 |
VIXL_ASSERT(offset != 0); |
| 1887 |
// Only preindex should log the register update here. For Postindex, the |
| 1888 |
// update will be printed automatically by LogWrittenRegisters _after_ the |
| 1889 |
// memory access itself is logged. |
| 1890 |
RegLogMode log_mode = (addrmode == PreIndex) ? LogRegWrites : NoRegLog; |
| 1891 |
set_xreg(addr_reg, address + offset, log_mode, Reg31IsStackPointer); |
| 1892 |
} |
| 1893 |
|
| 1894 |
if ((addrmode == Offset) || (addrmode == PreIndex)) { |
| 1895 |
address += offset; |
| 1896 |
} |
| 1897 |
|
| 1898 |
// Verify that the calculated address is available to the host. |
| 1899 |
VIXL_ASSERT(address == static_cast<uintptr_t>(address)); |
| 1900 |
|
| 1901 |
return static_cast<uintptr_t>(address); |
| 1902 |
} |
| 1903 |
|
| 1904 |
|
| 1905 |
void Simulator::VisitMoveWideImmediate(const Instruction* instr) { |
| 1906 |
MoveWideImmediateOp mov_op = |
| 1907 |
static_cast<MoveWideImmediateOp>(instr->Mask(MoveWideImmediateMask)); |
| 1908 |
int64_t new_xn_val = 0; |
| 1909 |
|
| 1910 |
bool is_64_bits = instr->SixtyFourBits() == 1; |
| 1911 |
// Shift is limited for W operations. |
| 1912 |
VIXL_ASSERT(is_64_bits || (instr->ShiftMoveWide() < 2)); |
| 1913 |
|
| 1914 |
// Get the shifted immediate. |
| 1915 |
int64_t shift = instr->ShiftMoveWide() * 16; |
| 1916 |
int64_t shifted_imm16 = static_cast<int64_t>(instr->ImmMoveWide()) << shift; |
| 1917 |
|
| 1918 |
// Compute the new value. |
| 1919 |
switch (mov_op) { |
| 1920 |
case MOVN_w: |
| 1921 |
case MOVN_x: { |
| 1922 |
new_xn_val = ~shifted_imm16; |
| 1923 |
if (!is_64_bits) new_xn_val &= kWRegMask; |
| 1924 |
break; |
| 1925 |
} |
| 1926 |
case MOVK_w: |
| 1927 |
case MOVK_x: { |
| 1928 |
unsigned reg_code = instr->Rd(); |
| 1929 |
int64_t prev_xn_val = is_64_bits ? xreg(reg_code) |
| 1930 |
: wreg(reg_code); |
| 1931 |
new_xn_val = |
| 1932 |
(prev_xn_val & ~(INT64_C(0xffff) << shift)) | shifted_imm16; |
| 1933 |
break; |
| 1934 |
} |
| 1935 |
case MOVZ_w: |
| 1936 |
case MOVZ_x: { |
| 1937 |
new_xn_val = shifted_imm16; |
| 1938 |
break; |
| 1939 |
} |
| 1940 |
default: |
| 1941 |
VIXL_UNREACHABLE(); |
| 1942 |
} |
| 1943 |
|
| 1944 |
// Update the destination register. |
| 1945 |
set_xreg(instr->Rd(), new_xn_val); |
| 1946 |
} |
| 1947 |
|
| 1948 |
|
| 1949 |
void Simulator::VisitConditionalSelect(const Instruction* instr) { |
| 1950 |
uint64_t new_val = xreg(instr->Rn()); |
| 1951 |
|
| 1952 |
if (ConditionFailed(static_cast<Condition>(instr->Condition()))) { |
| 1953 |
new_val = xreg(instr->Rm()); |
| 1954 |
switch (instr->Mask(ConditionalSelectMask)) { |
| 1955 |
case CSEL_w: |
| 1956 |
case CSEL_x: break; |
| 1957 |
case CSINC_w: |
| 1958 |
case CSINC_x: new_val++; break; |
| 1959 |
case CSINV_w: |
| 1960 |
case CSINV_x: new_val = ~new_val; break; |
| 1961 |
case CSNEG_w: |
| 1962 |
case CSNEG_x: new_val = -new_val; break; |
| 1963 |
default: VIXL_UNIMPLEMENTED(); |
| 1964 |
} |
| 1965 |
} |
| 1966 |
unsigned reg_size = instr->SixtyFourBits() ? kXRegSize : kWRegSize; |
| 1967 |
set_reg(reg_size, instr->Rd(), new_val); |
| 1968 |
} |
| 1969 |
|
| 1970 |
|
| 1971 |
void Simulator::VisitDataProcessing1Source(const Instruction* instr) { |
| 1972 |
unsigned dst = instr->Rd(); |
| 1973 |
unsigned src = instr->Rn(); |
| 1974 |
|
| 1975 |
switch (instr->Mask(DataProcessing1SourceMask)) { |
| 1976 |
case RBIT_w: set_wreg(dst, ReverseBits(wreg(src))); break; |
| 1977 |
case RBIT_x: set_xreg(dst, ReverseBits(xreg(src))); break; |
| 1978 |
case REV16_w: set_wreg(dst, ReverseBytes(wreg(src), 1)); break; |
| 1979 |
case REV16_x: set_xreg(dst, ReverseBytes(xreg(src), 1)); break; |
| 1980 |
case REV_w: set_wreg(dst, ReverseBytes(wreg(src), 2)); break; |
| 1981 |
case REV32_x: set_xreg(dst, ReverseBytes(xreg(src), 2)); break; |
| 1982 |
case REV_x: set_xreg(dst, ReverseBytes(xreg(src), 3)); break; |
| 1983 |
case CLZ_w: set_wreg(dst, CountLeadingZeros(wreg(src))); break; |
| 1984 |
case CLZ_x: set_xreg(dst, CountLeadingZeros(xreg(src))); break; |
| 1985 |
case CLS_w: { |
| 1986 |
set_wreg(dst, CountLeadingSignBits(wreg(src))); |
| 1987 |
break; |
| 1988 |
} |
| 1989 |
case CLS_x: { |
| 1990 |
set_xreg(dst, CountLeadingSignBits(xreg(src))); |
| 1991 |
break; |
| 1992 |
} |
| 1993 |
default: VIXL_UNIMPLEMENTED(); |
| 1994 |
} |
| 1995 |
} |
| 1996 |
|
| 1997 |
|
| 1998 |
uint32_t Simulator::Poly32Mod2(unsigned n, uint64_t data, uint32_t poly) { |
| 1999 |
VIXL_ASSERT((n > 32) && (n <= 64)); |
| 2000 |
for (unsigned i = (n - 1); i >= 32; i--) { |
| 2001 |
if (((data >> i) & 1) != 0) { |
| 2002 |
uint64_t polysh32 = (uint64_t)poly << (i - 32); |
| 2003 |
uint64_t mask = (UINT64_C(1) << i) - 1; |
| 2004 |
data = ((data & mask) ^ polysh32); |
| 2005 |
} |
| 2006 |
} |
| 2007 |
return data & 0xffffffff; |
| 2008 |
} |
| 2009 |
|
| 2010 |
|
| 2011 |
template <typename T> |
| 2012 |
uint32_t Simulator::Crc32Checksum(uint32_t acc, T val, uint32_t poly) { |
| 2013 |
unsigned size = sizeof(val) * 8; // Number of bits in type T. |
| 2014 |
VIXL_ASSERT((size == 8) || (size == 16) || (size == 32)); |
| 2015 |
uint64_t tempacc = static_cast<uint64_t>(ReverseBits(acc)) << size; |
| 2016 |
uint64_t tempval = static_cast<uint64_t>(ReverseBits(val)) << 32; |
| 2017 |
return ReverseBits(Poly32Mod2(32 + size, tempacc ^ tempval, poly)); |
| 2018 |
} |
| 2019 |
|
| 2020 |
|
| 2021 |
uint32_t Simulator::Crc32Checksum(uint32_t acc, uint64_t val, uint32_t poly) { |
| 2022 |
// Poly32Mod2 cannot handle inputs with more than 32 bits, so compute |
| 2023 |
// the CRC of each 32-bit word sequentially. |
| 2024 |
acc = Crc32Checksum(acc, (uint32_t)(val & 0xffffffff), poly); |
| 2025 |
return Crc32Checksum(acc, (uint32_t)(val >> 32), poly); |
| 2026 |
} |
| 2027 |
|
| 2028 |
|
| 2029 |
void Simulator::VisitDataProcessing2Source(const Instruction* instr) { |
| 2030 |
Shift shift_op = NO_SHIFT; |
| 2031 |
int64_t result = 0; |
| 2032 |
unsigned reg_size = instr->SixtyFourBits() ? kXRegSize : kWRegSize; |
| 2033 |
|
| 2034 |
switch (instr->Mask(DataProcessing2SourceMask)) { |
| 2035 |
case SDIV_w: { |
| 2036 |
int32_t rn = wreg(instr->Rn()); |
| 2037 |
int32_t rm = wreg(instr->Rm()); |
| 2038 |
if ((rn == kWMinInt) && (rm == -1)) { |
| 2039 |
result = kWMinInt; |
| 2040 |
} else if (rm == 0) { |
| 2041 |
// Division by zero can be trapped, but not on A-class processors. |
| 2042 |
result = 0; |
| 2043 |
} else { |
| 2044 |
result = rn / rm; |
| 2045 |
} |
| 2046 |
break; |
| 2047 |
} |
| 2048 |
case SDIV_x: { |
| 2049 |
int64_t rn = xreg(instr->Rn()); |
| 2050 |
int64_t rm = xreg(instr->Rm()); |
| 2051 |
if ((rn == kXMinInt) && (rm == -1)) { |
| 2052 |
result = kXMinInt; |
| 2053 |
} else if (rm == 0) { |
| 2054 |
// Division by zero can be trapped, but not on A-class processors. |
| 2055 |
result = 0; |
| 2056 |
} else { |
| 2057 |
result = rn / rm; |
| 2058 |
} |
| 2059 |
break; |
| 2060 |
} |
| 2061 |
case UDIV_w: { |
| 2062 |
uint32_t rn = static_cast<uint32_t>(wreg(instr->Rn())); |
| 2063 |
uint32_t rm = static_cast<uint32_t>(wreg(instr->Rm())); |
| 2064 |
if (rm == 0) { |
| 2065 |
// Division by zero can be trapped, but not on A-class processors. |
| 2066 |
result = 0; |
| 2067 |
} else { |
| 2068 |
result = rn / rm; |
| 2069 |
} |
| 2070 |
break; |
| 2071 |
} |
| 2072 |
case UDIV_x: { |
| 2073 |
uint64_t rn = static_cast<uint64_t>(xreg(instr->Rn())); |
| 2074 |
uint64_t rm = static_cast<uint64_t>(xreg(instr->Rm())); |
| 2075 |
if (rm == 0) { |
| 2076 |
// Division by zero can be trapped, but not on A-class processors. |
| 2077 |
result = 0; |
| 2078 |
} else { |
| 2079 |
result = rn / rm; |
| 2080 |
} |
| 2081 |
break; |
| 2082 |
} |
| 2083 |
case LSLV_w: |
| 2084 |
case LSLV_x: shift_op = LSL; break; |
| 2085 |
case LSRV_w: |
| 2086 |
case LSRV_x: shift_op = LSR; break; |
| 2087 |
case ASRV_w: |
| 2088 |
case ASRV_x: shift_op = ASR; break; |
| 2089 |
case RORV_w: |
| 2090 |
case RORV_x: shift_op = ROR; break; |
| 2091 |
case CRC32B: { |
| 2092 |
uint32_t acc = reg<uint32_t>(instr->Rn()); |
| 2093 |
uint8_t val = reg<uint8_t>(instr->Rm()); |
| 2094 |
result = Crc32Checksum(acc, val, CRC32_POLY); |
| 2095 |
break; |
| 2096 |
} |
| 2097 |
case CRC32H: { |
| 2098 |
uint32_t acc = reg<uint32_t>(instr->Rn()); |
| 2099 |
uint16_t val = reg<uint16_t>(instr->Rm()); |
| 2100 |
result = Crc32Checksum(acc, val, CRC32_POLY); |
| 2101 |
break; |
| 2102 |
} |
| 2103 |
case CRC32W: { |
| 2104 |
uint32_t acc = reg<uint32_t>(instr->Rn()); |
| 2105 |
uint32_t val = reg<uint32_t>(instr->Rm()); |
| 2106 |
result = Crc32Checksum(acc, val, CRC32_POLY); |
| 2107 |
break; |
| 2108 |
} |
| 2109 |
case CRC32X: { |
| 2110 |
uint32_t acc = reg<uint32_t>(instr->Rn()); |
| 2111 |
uint64_t val = reg<uint64_t>(instr->Rm()); |
| 2112 |
result = Crc32Checksum(acc, val, CRC32_POLY); |
| 2113 |
reg_size = kWRegSize; |
| 2114 |
break; |
| 2115 |
} |
| 2116 |
case CRC32CB: { |
| 2117 |
uint32_t acc = reg<uint32_t>(instr->Rn()); |
| 2118 |
uint8_t val = reg<uint8_t>(instr->Rm()); |
| 2119 |
result = Crc32Checksum(acc, val, CRC32C_POLY); |
| 2120 |
break; |
| 2121 |
} |
| 2122 |
case CRC32CH: { |
| 2123 |
uint32_t acc = reg<uint32_t>(instr->Rn()); |
| 2124 |
uint16_t val = reg<uint16_t>(instr->Rm()); |
| 2125 |
result = Crc32Checksum(acc, val, CRC32C_POLY); |
| 2126 |
break; |
| 2127 |
} |
| 2128 |
case CRC32CW: { |
| 2129 |
uint32_t acc = reg<uint32_t>(instr->Rn()); |
| 2130 |
uint32_t val = reg<uint32_t>(instr->Rm()); |
| 2131 |
result = Crc32Checksum(acc, val, CRC32C_POLY); |
| 2132 |
break; |
| 2133 |
} |
| 2134 |
case CRC32CX: { |
| 2135 |
uint32_t acc = reg<uint32_t>(instr->Rn()); |
| 2136 |
uint64_t val = reg<uint64_t>(instr->Rm()); |
| 2137 |
result = Crc32Checksum(acc, val, CRC32C_POLY); |
| 2138 |
reg_size = kWRegSize; |
| 2139 |
break; |
| 2140 |
} |
| 2141 |
default: VIXL_UNIMPLEMENTED(); |
| 2142 |
} |
| 2143 |
|
| 2144 |
if (shift_op != NO_SHIFT) { |
| 2145 |
// Shift distance encoded in the least-significant five/six bits of the |
| 2146 |
// register. |
| 2147 |
int mask = (instr->SixtyFourBits() == 1) ? 0x3f : 0x1f; |
| 2148 |
unsigned shift = wreg(instr->Rm()) & mask; |
| 2149 |
result = ShiftOperand(reg_size, reg(reg_size, instr->Rn()), shift_op, |
| 2150 |
shift); |
| 2151 |
} |
| 2152 |
set_reg(reg_size, instr->Rd(), result); |
| 2153 |
} |
| 2154 |
|
| 2155 |
|
| 2156 |
// The algorithm used is adapted from the one described in section 8.2 of |
| 2157 |
// Hacker's Delight, by Henry S. Warren, Jr. |
| 2158 |
// It assumes that a right shift on a signed integer is an arithmetic shift. |
| 2159 |
// Type T must be either uint64_t or int64_t. |
| 2160 |
template <typename T> |
| 2161 |
static T MultiplyHigh(T u, T v) { |
| 2162 |
uint64_t u0, v0, w0; |
| 2163 |
T u1, v1, w1, w2, t; |
| 2164 |
|
| 2165 |
VIXL_ASSERT(sizeof(u) == sizeof(u0)); |
| 2166 |
|
| 2167 |
u0 = u & 0xffffffff; |
| 2168 |
u1 = u >> 32; |
| 2169 |
v0 = v & 0xffffffff; |
| 2170 |
v1 = v >> 32; |
| 2171 |
|
| 2172 |
w0 = u0 * v0; |
| 2173 |
t = u1 * v0 + (w0 >> 32); |
| 2174 |
w1 = t & 0xffffffff; |
| 2175 |
w2 = t >> 32; |
| 2176 |
w1 = u0 * v1 + w1; |
| 2177 |
|
| 2178 |
return u1 * v1 + w2 + (w1 >> 32); |
| 2179 |
} |
| 2180 |
|
| 2181 |
|
| 2182 |
void Simulator::VisitDataProcessing3Source(const Instruction* instr) { |
| 2183 |
unsigned reg_size = instr->SixtyFourBits() ? kXRegSize : kWRegSize; |
| 2184 |
|
| 2185 |
int64_t result = 0; |
| 2186 |
// Extract and sign- or zero-extend 32-bit arguments for widening operations. |
| 2187 |
uint64_t rn_u32 = reg<uint32_t>(instr->Rn()); |
| 2188 |
uint64_t rm_u32 = reg<uint32_t>(instr->Rm()); |
| 2189 |
int64_t rn_s32 = reg<int32_t>(instr->Rn()); |
| 2190 |
int64_t rm_s32 = reg<int32_t>(instr->Rm()); |
| 2191 |
switch (instr->Mask(DataProcessing3SourceMask)) { |
| 2192 |
case MADD_w: |
| 2193 |
case MADD_x: |
| 2194 |
result = xreg(instr->Ra()) + (xreg(instr->Rn()) * xreg(instr->Rm())); |
| 2195 |
break; |
| 2196 |
case MSUB_w: |
| 2197 |
case MSUB_x: |
| 2198 |
result = xreg(instr->Ra()) - (xreg(instr->Rn()) * xreg(instr->Rm())); |
| 2199 |
break; |
| 2200 |
case SMADDL_x: result = xreg(instr->Ra()) + (rn_s32 * rm_s32); break; |
| 2201 |
case SMSUBL_x: result = xreg(instr->Ra()) - (rn_s32 * rm_s32); break; |
| 2202 |
case UMADDL_x: result = xreg(instr->Ra()) + (rn_u32 * rm_u32); break; |
| 2203 |
case UMSUBL_x: result = xreg(instr->Ra()) - (rn_u32 * rm_u32); break; |
| 2204 |
case UMULH_x: |
| 2205 |
result = MultiplyHigh(reg<uint64_t>(instr->Rn()), |
| 2206 |
reg<uint64_t>(instr->Rm())); |
| 2207 |
break; |
| 2208 |
case SMULH_x: |
| 2209 |
result = MultiplyHigh(xreg(instr->Rn()), xreg(instr->Rm())); |
| 2210 |
break; |
| 2211 |
default: VIXL_UNIMPLEMENTED(); |
| 2212 |
} |
| 2213 |
set_reg(reg_size, instr->Rd(), result); |
| 2214 |
} |
| 2215 |
|
| 2216 |
|
| 2217 |
void Simulator::VisitBitfield(const Instruction* instr) { |
| 2218 |
unsigned reg_size = instr->SixtyFourBits() ? kXRegSize : kWRegSize; |
| 2219 |
int64_t reg_mask = instr->SixtyFourBits() ? kXRegMask : kWRegMask; |
| 2220 |
int64_t R = instr->ImmR(); |
| 2221 |
int64_t S = instr->ImmS(); |
| 2222 |
int64_t diff = S - R; |
| 2223 |
int64_t mask; |
| 2224 |
if (diff >= 0) { |
| 2225 |
mask = (diff < (reg_size - 1)) ? (INT64_C(1) << (diff + 1)) - 1 |
| 2226 |
: reg_mask; |
| 2227 |
} else { |
| 2228 |
mask = (INT64_C(1) << (S + 1)) - 1; |
| 2229 |
mask = (static_cast<uint64_t>(mask) >> R) | (mask << (reg_size - R)); |
| 2230 |
diff += reg_size; |
| 2231 |
} |
| 2232 |
|
| 2233 |
// inzero indicates if the extracted bitfield is inserted into the |
| 2234 |
// destination register value or in zero. |
| 2235 |
// If extend is true, extend the sign of the extracted bitfield. |
| 2236 |
bool inzero = false; |
| 2237 |
bool extend = false; |
| 2238 |
switch (instr->Mask(BitfieldMask)) { |
| 2239 |
case BFM_x: |
| 2240 |
case BFM_w: |
| 2241 |
break; |
| 2242 |
case SBFM_x: |
| 2243 |
case SBFM_w: |
| 2244 |
inzero = true; |
| 2245 |
extend = true; |
| 2246 |
break; |
| 2247 |
case UBFM_x: |
| 2248 |
case UBFM_w: |
| 2249 |
inzero = true; |
| 2250 |
break; |
| 2251 |
default: |
| 2252 |
VIXL_UNIMPLEMENTED(); |
| 2253 |
} |
| 2254 |
|
| 2255 |
int64_t dst = inzero ? 0 : reg(reg_size, instr->Rd()); |
| 2256 |
int64_t src = reg(reg_size, instr->Rn()); |
| 2257 |
// Rotate source bitfield into place. |
| 2258 |
int64_t result = (static_cast<uint64_t>(src) >> R) | (src << (reg_size - R)); |
| 2259 |
// Determine the sign extension. |
| 2260 |
int64_t topbits = ((INT64_C(1) << (reg_size - diff - 1)) - 1) << (diff + 1); |
| 2261 |
int64_t signbits = extend && ((src >> S) & 1) ? topbits : 0; |
| 2262 |
|
| 2263 |
// Merge sign extension, dest/zero and bitfield. |
| 2264 |
result = signbits | (result & mask) | (dst & ~mask); |
| 2265 |
|
| 2266 |
set_reg(reg_size, instr->Rd(), result); |
| 2267 |
} |
| 2268 |
|
| 2269 |
|
| 2270 |
void Simulator::VisitExtract(const Instruction* instr) { |
| 2271 |
unsigned lsb = instr->ImmS(); |
| 2272 |
unsigned reg_size = (instr->SixtyFourBits() == 1) ? kXRegSize |
| 2273 |
: kWRegSize; |
| 2274 |
uint64_t low_res = static_cast<uint64_t>(reg(reg_size, instr->Rm())) >> lsb; |
| 2275 |
uint64_t high_res = |
| 2276 |
(lsb == 0) ? 0 : reg(reg_size, instr->Rn()) << (reg_size - lsb); |
| 2277 |
set_reg(reg_size, instr->Rd(), low_res | high_res); |
| 2278 |
} |
| 2279 |
|
| 2280 |
|
| 2281 |
void Simulator::VisitFPImmediate(const Instruction* instr) { |
| 2282 |
AssertSupportedFPCR(); |
| 2283 |
|
| 2284 |
unsigned dest = instr->Rd(); |
| 2285 |
switch (instr->Mask(FPImmediateMask)) { |
| 2286 |
case FMOV_s_imm: set_sreg(dest, instr->ImmFP32()); break; |
| 2287 |
case FMOV_d_imm: set_dreg(dest, instr->ImmFP64()); break; |
| 2288 |
default: VIXL_UNREACHABLE(); |
| 2289 |
} |
| 2290 |
} |
| 2291 |
|
| 2292 |
|
| 2293 |
void Simulator::VisitFPIntegerConvert(const Instruction* instr) { |
| 2294 |
AssertSupportedFPCR(); |
| 2295 |
|
| 2296 |
unsigned dst = instr->Rd(); |
| 2297 |
unsigned src = instr->Rn(); |
| 2298 |
|
| 2299 |
FPRounding round = RMode(); |
| 2300 |
|
| 2301 |
switch (instr->Mask(FPIntegerConvertMask)) { |
| 2302 |
case FCVTAS_ws: set_wreg(dst, FPToInt32(sreg(src), FPTieAway)); break; |
| 2303 |
case FCVTAS_xs: set_xreg(dst, FPToInt64(sreg(src), FPTieAway)); break; |
| 2304 |
case FCVTAS_wd: set_wreg(dst, FPToInt32(dreg(src), FPTieAway)); break; |
| 2305 |
case FCVTAS_xd: set_xreg(dst, FPToInt64(dreg(src), FPTieAway)); break; |
| 2306 |
case FCVTAU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPTieAway)); break; |
| 2307 |
case FCVTAU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPTieAway)); break; |
| 2308 |
case FCVTAU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPTieAway)); break; |
| 2309 |
case FCVTAU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPTieAway)); break; |
| 2310 |
case FCVTMS_ws: |
| 2311 |
set_wreg(dst, FPToInt32(sreg(src), FPNegativeInfinity)); |
| 2312 |
break; |
| 2313 |
case FCVTMS_xs: |
| 2314 |
set_xreg(dst, FPToInt64(sreg(src), FPNegativeInfinity)); |
| 2315 |
break; |
| 2316 |
case FCVTMS_wd: |
| 2317 |
set_wreg(dst, FPToInt32(dreg(src), FPNegativeInfinity)); |
| 2318 |
break; |
| 2319 |
case FCVTMS_xd: |
| 2320 |
set_xreg(dst, FPToInt64(dreg(src), FPNegativeInfinity)); |
| 2321 |
break; |
| 2322 |
case FCVTMU_ws: |
| 2323 |
set_wreg(dst, FPToUInt32(sreg(src), FPNegativeInfinity)); |
| 2324 |
break; |
| 2325 |
case FCVTMU_xs: |
| 2326 |
set_xreg(dst, FPToUInt64(sreg(src), FPNegativeInfinity)); |
| 2327 |
break; |
| 2328 |
case FCVTMU_wd: |
| 2329 |
set_wreg(dst, FPToUInt32(dreg(src), FPNegativeInfinity)); |
| 2330 |
break; |
| 2331 |
case FCVTMU_xd: |
| 2332 |
set_xreg(dst, FPToUInt64(dreg(src), FPNegativeInfinity)); |
| 2333 |
break; |
| 2334 |
case FCVTPS_ws: |
| 2335 |
set_wreg(dst, FPToInt32(sreg(src), FPPositiveInfinity)); |
| 2336 |
break; |
| 2337 |
case FCVTPS_xs: |
| 2338 |
set_xreg(dst, FPToInt64(sreg(src), FPPositiveInfinity)); |
| 2339 |
break; |
| 2340 |
case FCVTPS_wd: |
| 2341 |
set_wreg(dst, FPToInt32(dreg(src), FPPositiveInfinity)); |
| 2342 |
break; |
| 2343 |
case FCVTPS_xd: |
| 2344 |
set_xreg(dst, FPToInt64(dreg(src), FPPositiveInfinity)); |
| 2345 |
break; |
| 2346 |
case FCVTPU_ws: |
| 2347 |
set_wreg(dst, FPToUInt32(sreg(src), FPPositiveInfinity)); |
| 2348 |
break; |
| 2349 |
case FCVTPU_xs: |
| 2350 |
set_xreg(dst, FPToUInt64(sreg(src), FPPositiveInfinity)); |
| 2351 |
break; |
| 2352 |
case FCVTPU_wd: |
| 2353 |
set_wreg(dst, FPToUInt32(dreg(src), FPPositiveInfinity)); |
| 2354 |
break; |
| 2355 |
case FCVTPU_xd: |
| 2356 |
set_xreg(dst, FPToUInt64(dreg(src), FPPositiveInfinity)); |
| 2357 |
break; |
| 2358 |
case FCVTNS_ws: set_wreg(dst, FPToInt32(sreg(src), FPTieEven)); break; |
| 2359 |
case FCVTNS_xs: set_xreg(dst, FPToInt64(sreg(src), FPTieEven)); break; |
| 2360 |
case FCVTNS_wd: set_wreg(dst, FPToInt32(dreg(src), FPTieEven)); break; |
| 2361 |
case FCVTNS_xd: set_xreg(dst, FPToInt64(dreg(src), FPTieEven)); break; |
| 2362 |
case FCVTNU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPTieEven)); break; |
| 2363 |
case FCVTNU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPTieEven)); break; |
| 2364 |
case FCVTNU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPTieEven)); break; |
| 2365 |
case FCVTNU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPTieEven)); break; |
| 2366 |
case FCVTZS_ws: set_wreg(dst, FPToInt32(sreg(src), FPZero)); break; |
| 2367 |
case FCVTZS_xs: set_xreg(dst, FPToInt64(sreg(src), FPZero)); break; |
| 2368 |
case FCVTZS_wd: set_wreg(dst, FPToInt32(dreg(src), FPZero)); break; |
| 2369 |
case FCVTZS_xd: set_xreg(dst, FPToInt64(dreg(src), FPZero)); break; |
| 2370 |
case FCVTZU_ws: set_wreg(dst, FPToUInt32(sreg(src), FPZero)); break; |
| 2371 |
case FCVTZU_xs: set_xreg(dst, FPToUInt64(sreg(src), FPZero)); break; |
| 2372 |
case FCVTZU_wd: set_wreg(dst, FPToUInt32(dreg(src), FPZero)); break; |
| 2373 |
case FCVTZU_xd: set_xreg(dst, FPToUInt64(dreg(src), FPZero)); break; |
| 2374 |
case FJCVTZS: set_wreg(dst, FPToFixedJS(dreg(src))); break; |
| 2375 |
case FMOV_ws: set_wreg(dst, sreg_bits(src)); break; |
| 2376 |
case FMOV_xd: set_xreg(dst, dreg_bits(src)); break; |
| 2377 |
case FMOV_sw: set_sreg_bits(dst, wreg(src)); break; |
| 2378 |
case FMOV_dx: set_dreg_bits(dst, xreg(src)); break; |
| 2379 |
case FMOV_d1_x: |
| 2380 |
LogicVRegister(vreg(dst)).SetUint(kFormatD, 1, xreg(src)); |
| 2381 |
break; |
| 2382 |
case FMOV_x_d1: |
| 2383 |
set_xreg(dst, LogicVRegister(vreg(src)).Uint(kFormatD, 1)); |
| 2384 |
break; |
| 2385 |
|
| 2386 |
// A 32-bit input can be handled in the same way as a 64-bit input, since |
| 2387 |
// the sign- or zero-extension will not affect the conversion. |
| 2388 |
case SCVTF_dx: set_dreg(dst, FixedToDouble(xreg(src), 0, round)); break; |
| 2389 |
case SCVTF_dw: set_dreg(dst, FixedToDouble(wreg(src), 0, round)); break; |
| 2390 |
case UCVTF_dx: set_dreg(dst, UFixedToDouble(xreg(src), 0, round)); break; |
| 2391 |
case UCVTF_dw: { |
| 2392 |
set_dreg(dst, UFixedToDouble(static_cast<uint32_t>(wreg(src)), 0, round)); |
| 2393 |
break; |
| 2394 |
} |
| 2395 |
case SCVTF_sx: set_sreg(dst, FixedToFloat(xreg(src), 0, round)); break; |
| 2396 |
case SCVTF_sw: set_sreg(dst, FixedToFloat(wreg(src), 0, round)); break; |
| 2397 |
case UCVTF_sx: set_sreg(dst, UFixedToFloat(xreg(src), 0, round)); break; |
| 2398 |
case UCVTF_sw: { |
| 2399 |
set_sreg(dst, UFixedToFloat(static_cast<uint32_t>(wreg(src)), 0, round)); |
| 2400 |
break; |
| 2401 |
} |
| 2402 |
|
| 2403 |
default: VIXL_UNREACHABLE(); |
| 2404 |
} |
| 2405 |
} |
| 2406 |
|
| 2407 |
|
| 2408 |
void Simulator::VisitFPFixedPointConvert(const Instruction* instr) { |
| 2409 |
AssertSupportedFPCR(); |
| 2410 |
|
| 2411 |
unsigned dst = instr->Rd(); |
| 2412 |
unsigned src = instr->Rn(); |
| 2413 |
int fbits = 64 - instr->FPScale(); |
| 2414 |
|
| 2415 |
FPRounding round = RMode(); |
| 2416 |
|
| 2417 |
switch (instr->Mask(FPFixedPointConvertMask)) { |
| 2418 |
// A 32-bit input can be handled in the same way as a 64-bit input, since |
| 2419 |
// the sign- or zero-extension will not affect the conversion. |
| 2420 |
case SCVTF_dx_fixed: |
| 2421 |
set_dreg(dst, FixedToDouble(xreg(src), fbits, round)); |
| 2422 |
break; |
| 2423 |
case SCVTF_dw_fixed: |
| 2424 |
set_dreg(dst, FixedToDouble(wreg(src), fbits, round)); |
| 2425 |
break; |
| 2426 |
case UCVTF_dx_fixed: |
| 2427 |
set_dreg(dst, UFixedToDouble(xreg(src), fbits, round)); |
| 2428 |
break; |
| 2429 |
case UCVTF_dw_fixed: { |
| 2430 |
set_dreg(dst, |
| 2431 |
UFixedToDouble(static_cast<uint32_t>(wreg(src)), fbits, round)); |
| 2432 |
break; |
| 2433 |
} |
| 2434 |
case SCVTF_sx_fixed: |
| 2435 |
set_sreg(dst, FixedToFloat(xreg(src), fbits, round)); |
| 2436 |
break; |
| 2437 |
case SCVTF_sw_fixed: |
| 2438 |
set_sreg(dst, FixedToFloat(wreg(src), fbits, round)); |
| 2439 |
break; |
| 2440 |
case UCVTF_sx_fixed: |
| 2441 |
set_sreg(dst, UFixedToFloat(xreg(src), fbits, round)); |
| 2442 |
break; |
| 2443 |
case UCVTF_sw_fixed: { |
| 2444 |
set_sreg(dst, |
| 2445 |
UFixedToFloat(static_cast<uint32_t>(wreg(src)), fbits, round)); |
| 2446 |
break; |
| 2447 |
} |
| 2448 |
case FCVTZS_xd_fixed: |
| 2449 |
set_xreg(dst, FPToInt64(dreg(src) * std::pow(2.0, fbits), FPZero)); |
| 2450 |
break; |
| 2451 |
case FCVTZS_wd_fixed: |
| 2452 |
set_wreg(dst, FPToInt32(dreg(src) * std::pow(2.0, fbits), FPZero)); |
| 2453 |
break; |
| 2454 |
case FCVTZU_xd_fixed: |
| 2455 |
set_xreg(dst, FPToUInt64(dreg(src) * std::pow(2.0, fbits), FPZero)); |
| 2456 |
break; |
| 2457 |
case FCVTZU_wd_fixed: |
| 2458 |
set_wreg(dst, FPToUInt32(dreg(src) * std::pow(2.0, fbits), FPZero)); |
| 2459 |
break; |
| 2460 |
case FCVTZS_xs_fixed: |
| 2461 |
set_xreg(dst, FPToInt64(sreg(src) * std::pow(2.0f, fbits), FPZero)); |
| 2462 |
break; |
| 2463 |
case FCVTZS_ws_fixed: |
| 2464 |
set_wreg(dst, FPToInt32(sreg(src) * std::pow(2.0f, fbits), FPZero)); |
| 2465 |
break; |
| 2466 |
case FCVTZU_xs_fixed: |
| 2467 |
set_xreg(dst, FPToUInt64(sreg(src) * std::pow(2.0f, fbits), FPZero)); |
| 2468 |
break; |
| 2469 |
case FCVTZU_ws_fixed: |
| 2470 |
set_wreg(dst, FPToUInt32(sreg(src) * std::pow(2.0f, fbits), FPZero)); |
| 2471 |
break; |
| 2472 |
default: VIXL_UNREACHABLE(); |
| 2473 |
} |
| 2474 |
} |
| 2475 |
|
| 2476 |
|
| 2477 |
void Simulator::VisitFPCompare(const Instruction* instr) { |
| 2478 |
AssertSupportedFPCR(); |
| 2479 |
|
| 2480 |
FPTrapFlags trap = DisableTrap; |
| 2481 |
switch (instr->Mask(FPCompareMask)) { |
| 2482 |
case FCMPE_s: trap = EnableTrap; VIXL_FALLTHROUGH(); |
| 2483 |
case FCMP_s: FPCompare(sreg(instr->Rn()), sreg(instr->Rm()), trap); break; |
| 2484 |
case FCMPE_d: trap = EnableTrap; VIXL_FALLTHROUGH(); |
| 2485 |
case FCMP_d: FPCompare(dreg(instr->Rn()), dreg(instr->Rm()), trap); break; |
| 2486 |
case FCMPE_s_zero: trap = EnableTrap; VIXL_FALLTHROUGH(); |
| 2487 |
case FCMP_s_zero: FPCompare(sreg(instr->Rn()), 0.0f, trap); break; |
| 2488 |
case FCMPE_d_zero: trap = EnableTrap; VIXL_FALLTHROUGH(); |
| 2489 |
case FCMP_d_zero: FPCompare(dreg(instr->Rn()), 0.0, trap); break; |
| 2490 |
default: VIXL_UNIMPLEMENTED(); |
| 2491 |
} |
| 2492 |
} |
| 2493 |
|
| 2494 |
|
| 2495 |
void Simulator::VisitFPConditionalCompare(const Instruction* instr) { |
| 2496 |
AssertSupportedFPCR(); |
| 2497 |
|
| 2498 |
FPTrapFlags trap = DisableTrap; |
| 2499 |
switch (instr->Mask(FPConditionalCompareMask)) { |
| 2500 |
case FCCMPE_s: trap = EnableTrap; |
| 2501 |
VIXL_FALLTHROUGH(); |
| 2502 |
case FCCMP_s: |
| 2503 |
if (ConditionPassed(instr->Condition())) { |
| 2504 |
FPCompare(sreg(instr->Rn()), sreg(instr->Rm()), trap); |
| 2505 |
} else { |
| 2506 |
nzcv().SetFlags(instr->Nzcv()); |
| 2507 |
LogSystemRegister(NZCV); |
| 2508 |
} |
| 2509 |
break; |
| 2510 |
case FCCMPE_d: trap = EnableTrap; |
| 2511 |
VIXL_FALLTHROUGH(); |
| 2512 |
case FCCMP_d: |
| 2513 |
if (ConditionPassed(instr->Condition())) { |
| 2514 |
FPCompare(dreg(instr->Rn()), dreg(instr->Rm()), trap); |
| 2515 |
} else { |
| 2516 |
nzcv().SetFlags(instr->Nzcv()); |
| 2517 |
LogSystemRegister(NZCV); |
| 2518 |
} |
| 2519 |
break; |
| 2520 |
default: VIXL_UNIMPLEMENTED(); |
| 2521 |
} |
| 2522 |
} |
| 2523 |
|
| 2524 |
|
| 2525 |
void Simulator::VisitFPConditionalSelect(const Instruction* instr) { |
| 2526 |
AssertSupportedFPCR(); |
| 2527 |
|
| 2528 |
Instr selected; |
| 2529 |
if (ConditionPassed(instr->Condition())) { |
| 2530 |
selected = instr->Rn(); |
| 2531 |
} else { |
| 2532 |
selected = instr->Rm(); |
| 2533 |
} |
| 2534 |
|
| 2535 |
switch (instr->Mask(FPConditionalSelectMask)) { |
| 2536 |
case FCSEL_s: set_sreg(instr->Rd(), sreg(selected)); break; |
| 2537 |
case FCSEL_d: set_dreg(instr->Rd(), dreg(selected)); break; |
| 2538 |
default: VIXL_UNIMPLEMENTED(); |
| 2539 |
} |
| 2540 |
} |
| 2541 |
|
| 2542 |
|
| 2543 |
void Simulator::VisitFPDataProcessing1Source(const Instruction* instr) { |
| 2544 |
AssertSupportedFPCR(); |
| 2545 |
|
| 2546 |
FPRounding fpcr_rounding = static_cast<FPRounding>(fpcr().RMode()); |
| 2547 |
VectorFormat vform = (instr->Mask(FP64) == FP64) ? kFormatD : kFormatS; |
| 2548 |
SimVRegister& rd = vreg(instr->Rd()); |
| 2549 |
SimVRegister& rn = vreg(instr->Rn()); |
| 2550 |
bool inexact_exception = false; |
| 2551 |
|
| 2552 |
unsigned fd = instr->Rd(); |
| 2553 |
unsigned fn = instr->Rn(); |
| 2554 |
|
| 2555 |
switch (instr->Mask(FPDataProcessing1SourceMask)) { |
| 2556 |
case FMOV_s: set_sreg(fd, sreg(fn)); return; |
| 2557 |
case FMOV_d: set_dreg(fd, dreg(fn)); return; |
| 2558 |
case FABS_s: fabs_(kFormatS, vreg(fd), vreg(fn)); return; |
| 2559 |
case FABS_d: fabs_(kFormatD, vreg(fd), vreg(fn)); return; |
| 2560 |
case FNEG_s: fneg(kFormatS, vreg(fd), vreg(fn)); return; |
| 2561 |
case FNEG_d: fneg(kFormatD, vreg(fd), vreg(fn)); return; |
| 2562 |
case FCVT_ds: |
| 2563 |
set_dreg(fd, FPToDouble(sreg(fn), ReadDN())); |
| 2564 |
return; |
| 2565 |
case FCVT_sd: |
| 2566 |
set_sreg(fd, FPToFloat(dreg(fn), FPTieEven, ReadDN())); |
| 2567 |
return; |
| 2568 |
case FCVT_hs: |
| 2569 |
set_hreg(fd, Float16ToRawbits(FPToFloat16(sreg(fn), FPTieEven, ReadDN()))); |
| 2570 |
return; |
| 2571 |
case FCVT_sh: |
| 2572 |
set_sreg(fd, FPToFloat(RawbitsToFloat16(hreg(fn)), ReadDN())); |
| 2573 |
return; |
| 2574 |
case FCVT_dh: |
| 2575 |
set_dreg(fd, FPToDouble(RawbitsToFloat16(hreg(fn)), ReadDN())); |
| 2576 |
return; |
| 2577 |
case FCVT_hd: |
| 2578 |
set_hreg(fd, Float16ToRawbits(FPToFloat16(dreg(fn), FPTieEven, ReadDN()))); |
| 2579 |
return; |
| 2580 |
case FSQRT_s: |
| 2581 |
case FSQRT_d: fsqrt(vform, rd, rn); return; |
| 2582 |
case FRINTI_s: |
| 2583 |
case FRINTI_d: break; // Use FPCR rounding mode. |
| 2584 |
case FRINTX_s: |
| 2585 |
case FRINTX_d: inexact_exception = true; break; |
| 2586 |
case FRINTA_s: |
| 2587 |
case FRINTA_d: fpcr_rounding = FPTieAway; break; |
| 2588 |
case FRINTM_s: |
| 2589 |
case FRINTM_d: fpcr_rounding = FPNegativeInfinity; break; |
| 2590 |
case FRINTN_s: |
| 2591 |
case FRINTN_d: fpcr_rounding = FPTieEven; break; |
| 2592 |
case FRINTP_s: |
| 2593 |
case FRINTP_d: fpcr_rounding = FPPositiveInfinity; break; |
| 2594 |
case FRINTZ_s: |
| 2595 |
case FRINTZ_d: fpcr_rounding = FPZero; break; |
| 2596 |
default: VIXL_UNIMPLEMENTED(); |
| 2597 |
} |
| 2598 |
|
| 2599 |
// Only FRINT* instructions fall through the switch above. |
| 2600 |
frint(vform, rd, rn, fpcr_rounding, inexact_exception); |
| 2601 |
} |
| 2602 |
|
| 2603 |
|
| 2604 |
void Simulator::VisitFPDataProcessing2Source(const Instruction* instr) { |
| 2605 |
AssertSupportedFPCR(); |
| 2606 |
|
| 2607 |
VectorFormat vform = (instr->Mask(FP64) == FP64) ? kFormatD : kFormatS; |
| 2608 |
SimVRegister& rd = vreg(instr->Rd()); |
| 2609 |
SimVRegister& rn = vreg(instr->Rn()); |
| 2610 |
SimVRegister& rm = vreg(instr->Rm()); |
| 2611 |
|
| 2612 |
switch (instr->Mask(FPDataProcessing2SourceMask)) { |
| 2613 |
case FADD_s: |
| 2614 |
case FADD_d: fadd(vform, rd, rn, rm); break; |
| 2615 |
case FSUB_s: |
| 2616 |
case FSUB_d: fsub(vform, rd, rn, rm); break; |
| 2617 |
case FMUL_s: |
| 2618 |
case FMUL_d: fmul(vform, rd, rn, rm); break; |
| 2619 |
case FNMUL_s: |
| 2620 |
case FNMUL_d: fnmul(vform, rd, rn, rm); break; |
| 2621 |
case FDIV_s: |
| 2622 |
case FDIV_d: fdiv(vform, rd, rn, rm); break; |
| 2623 |
case FMAX_s: |
| 2624 |
case FMAX_d: fmax(vform, rd, rn, rm); break; |
| 2625 |
case FMIN_s: |
| 2626 |
case FMIN_d: fmin(vform, rd, rn, rm); break; |
| 2627 |
case FMAXNM_s: |
| 2628 |
case FMAXNM_d: fmaxnm(vform, rd, rn, rm); break; |
| 2629 |
case FMINNM_s: |
| 2630 |
case FMINNM_d: fminnm(vform, rd, rn, rm); break; |
| 2631 |
default: |
| 2632 |
VIXL_UNREACHABLE(); |
| 2633 |
} |
| 2634 |
} |
| 2635 |
|
| 2636 |
|
| 2637 |
void Simulator::VisitFPDataProcessing3Source(const Instruction* instr) { |
| 2638 |
AssertSupportedFPCR(); |
| 2639 |
|
| 2640 |
unsigned fd = instr->Rd(); |
| 2641 |
unsigned fn = instr->Rn(); |
| 2642 |
unsigned fm = instr->Rm(); |
| 2643 |
unsigned fa = instr->Ra(); |
| 2644 |
|
| 2645 |
switch (instr->Mask(FPDataProcessing3SourceMask)) { |
| 2646 |
// fd = fa +/- (fn * fm) |
| 2647 |
case FMADD_s: set_sreg(fd, FPMulAdd(sreg(fa), sreg(fn), sreg(fm))); break; |
| 2648 |
case FMSUB_s: set_sreg(fd, FPMulAdd(sreg(fa), -sreg(fn), sreg(fm))); break; |
| 2649 |
case FMADD_d: set_dreg(fd, FPMulAdd(dreg(fa), dreg(fn), dreg(fm))); break; |
| 2650 |
case FMSUB_d: set_dreg(fd, FPMulAdd(dreg(fa), -dreg(fn), dreg(fm))); break; |
| 2651 |
// Negated variants of the above. |
| 2652 |
case FNMADD_s: |
| 2653 |
set_sreg(fd, FPMulAdd(-sreg(fa), -sreg(fn), sreg(fm))); |
| 2654 |
break; |
| 2655 |
case FNMSUB_s: |
| 2656 |
set_sreg(fd, FPMulAdd(-sreg(fa), sreg(fn), sreg(fm))); |
| 2657 |
break; |
| 2658 |
case FNMADD_d: |
| 2659 |
set_dreg(fd, FPMulAdd(-dreg(fa), -dreg(fn), dreg(fm))); |
| 2660 |
break; |
| 2661 |
case FNMSUB_d: |
| 2662 |
set_dreg(fd, FPMulAdd(-dreg(fa), dreg(fn), dreg(fm))); |
| 2663 |
break; |
| 2664 |
default: VIXL_UNIMPLEMENTED(); |
| 2665 |
} |
| 2666 |
} |
| 2667 |
|
| 2668 |
|
| 2669 |
bool Simulator::FPProcessNaNs(const Instruction* instr) { |
| 2670 |
unsigned fd = instr->Rd(); |
| 2671 |
unsigned fn = instr->Rn(); |
| 2672 |
unsigned fm = instr->Rm(); |
| 2673 |
bool done = false; |
| 2674 |
|
| 2675 |
if (instr->Mask(FP64) == FP64) { |
| 2676 |
double result = FPProcessNaNs(dreg(fn), dreg(fm)); |
| 2677 |
if (std::isnan(result)) { |
| 2678 |
set_dreg(fd, result); |
| 2679 |
done = true; |
| 2680 |
} |
| 2681 |
} else { |
| 2682 |
float result = FPProcessNaNs(sreg(fn), sreg(fm)); |
| 2683 |
if (std::isnan(result)) { |
| 2684 |
set_sreg(fd, result); |
| 2685 |
done = true; |
| 2686 |
} |
| 2687 |
} |
| 2688 |
|
| 2689 |
return done; |
| 2690 |
} |
| 2691 |
|
| 2692 |
|
| 2693 |
void Simulator::SysOp_W(int op, int64_t val) { |
| 2694 |
switch (op) { |
| 2695 |
case IVAU: |
| 2696 |
case CVAC: |
| 2697 |
case CVAU: |
| 2698 |
case CIVAC: { |
| 2699 |
// Perform a dummy memory access to ensure that we have read access |
| 2700 |
// to the specified address. |
| 2701 |
volatile uint8_t y = Read<uint8_t>(val); |
| 2702 |
USE(y); |
| 2703 |
// TODO: Implement "case ZVA:". |
| 2704 |
break; |
| 2705 |
} |
| 2706 |
default: |
| 2707 |
VIXL_UNIMPLEMENTED(); |
| 2708 |
} |
| 2709 |
} |
| 2710 |
|
| 2711 |
|
| 2712 |
void Simulator::VisitSystem(const Instruction* instr) { |
| 2713 |
// Some system instructions hijack their Op and Cp fields to represent a |
| 2714 |
// range of immediates instead of indicating a different instruction. This |
| 2715 |
// makes the decoding tricky. |
| 2716 |
if (instr->Mask(SystemExclusiveMonitorFMask) == SystemExclusiveMonitorFixed) { |
| 2717 |
VIXL_ASSERT(instr->Mask(SystemExclusiveMonitorMask) == CLREX); |
| 2718 |
switch (instr->Mask(SystemExclusiveMonitorMask)) { |
| 2719 |
case CLREX: { |
| 2720 |
PrintExclusiveAccessWarning(); |
| 2721 |
ClearLocalMonitor(); |
| 2722 |
break; |
| 2723 |
} |
| 2724 |
} |
| 2725 |
} else if (instr->Mask(SystemSysRegFMask) == SystemSysRegFixed) { |
| 2726 |
switch (instr->Mask(SystemSysRegMask)) { |
| 2727 |
case MRS: { |
| 2728 |
switch (instr->ImmSystemRegister()) { |
| 2729 |
case NZCV: set_xreg(instr->Rt(), nzcv().RawValue()); break; |
| 2730 |
case FPCR: set_xreg(instr->Rt(), fpcr().RawValue()); break; |
| 2731 |
default: VIXL_UNIMPLEMENTED(); |
| 2732 |
} |
| 2733 |
break; |
| 2734 |
} |
| 2735 |
case MSR: { |
| 2736 |
switch (instr->ImmSystemRegister()) { |
| 2737 |
case NZCV: |
| 2738 |
nzcv().SetRawValue(wreg(instr->Rt())); |
| 2739 |
LogSystemRegister(NZCV); |
| 2740 |
break; |
| 2741 |
case FPCR: |
| 2742 |
fpcr().SetRawValue(wreg(instr->Rt())); |
| 2743 |
LogSystemRegister(FPCR); |
| 2744 |
break; |
| 2745 |
default: VIXL_UNIMPLEMENTED(); |
| 2746 |
} |
| 2747 |
break; |
| 2748 |
} |
| 2749 |
} |
| 2750 |
} else if (instr->Mask(SystemHintFMask) == SystemHintFixed) { |
| 2751 |
VIXL_ASSERT(instr->Mask(SystemHintMask) == HINT); |
| 2752 |
switch (instr->ImmHint()) { |
| 2753 |
case NOP: break; |
| 2754 |
case CSDB: break; |
| 2755 |
default: VIXL_UNIMPLEMENTED(); |
| 2756 |
} |
| 2757 |
} else if (instr->Mask(MemBarrierFMask) == MemBarrierFixed) { |
| 2758 |
js::jit::AtomicOperations::fenceSeqCst(); |
| 2759 |
} else if ((instr->Mask(SystemSysFMask) == SystemSysFixed)) { |
| 2760 |
switch (instr->Mask(SystemSysMask)) { |
| 2761 |
case SYS: SysOp_W(instr->SysOp(), xreg(instr->Rt())); break; |
| 2762 |
default: VIXL_UNIMPLEMENTED(); |
| 2763 |
} |
| 2764 |
} else { |
| 2765 |
VIXL_UNIMPLEMENTED(); |
| 2766 |
} |
| 2767 |
} |
| 2768 |
|
| 2769 |
|
| 2770 |
void Simulator::VisitCrypto2RegSHA(const Instruction* instr) { |
| 2771 |
VisitUnimplemented(instr); |
| 2772 |
} |
| 2773 |
|
| 2774 |
|
| 2775 |
void Simulator::VisitCrypto3RegSHA(const Instruction* instr) { |
| 2776 |
VisitUnimplemented(instr); |
| 2777 |
} |
| 2778 |
|
| 2779 |
|
| 2780 |
void Simulator::VisitCryptoAES(const Instruction* instr) { |
| 2781 |
VisitUnimplemented(instr); |
| 2782 |
} |
| 2783 |
|
| 2784 |
|
| 2785 |
void Simulator::VisitNEON2RegMisc(const Instruction* instr) { |
| 2786 |
NEONFormatDecoder nfd(instr); |
| 2787 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 2788 |
|
| 2789 |
static const NEONFormatMap map_lp = { |
| 2790 |
{23, 22, 30}, {NF_4H, NF_8H, NF_2S, NF_4S, NF_1D, NF_2D} |
| 2791 |
}; |
| 2792 |
VectorFormat vf_lp = nfd.GetVectorFormat(&map_lp); |
| 2793 |
|
| 2794 |
static const NEONFormatMap map_fcvtl = { |
| 2795 |
{22}, {NF_4S, NF_2D} |
| 2796 |
}; |
| 2797 |
VectorFormat vf_fcvtl = nfd.GetVectorFormat(&map_fcvtl); |
| 2798 |
|
| 2799 |
static const NEONFormatMap map_fcvtn = { |
| 2800 |
{22, 30}, {NF_4H, NF_8H, NF_2S, NF_4S} |
| 2801 |
}; |
| 2802 |
VectorFormat vf_fcvtn = nfd.GetVectorFormat(&map_fcvtn); |
| 2803 |
|
| 2804 |
SimVRegister& rd = vreg(instr->Rd()); |
| 2805 |
SimVRegister& rn = vreg(instr->Rn()); |
| 2806 |
|
| 2807 |
if (instr->Mask(NEON2RegMiscOpcode) <= NEON_NEG_opcode) { |
| 2808 |
// These instructions all use a two bit size field, except NOT and RBIT, |
| 2809 |
// which use the field to encode the operation. |
| 2810 |
switch (instr->Mask(NEON2RegMiscMask)) { |
| 2811 |
case NEON_REV64: rev64(vf, rd, rn); break; |
| 2812 |
case NEON_REV32: rev32(vf, rd, rn); break; |
| 2813 |
case NEON_REV16: rev16(vf, rd, rn); break; |
| 2814 |
case NEON_SUQADD: suqadd(vf, rd, rn); break; |
| 2815 |
case NEON_USQADD: usqadd(vf, rd, rn); break; |
| 2816 |
case NEON_CLS: cls(vf, rd, rn); break; |
| 2817 |
case NEON_CLZ: clz(vf, rd, rn); break; |
| 2818 |
case NEON_CNT: cnt(vf, rd, rn); break; |
| 2819 |
case NEON_SQABS: abs(vf, rd, rn).SignedSaturate(vf); break; |
| 2820 |
case NEON_SQNEG: neg(vf, rd, rn).SignedSaturate(vf); break; |
| 2821 |
case NEON_CMGT_zero: cmp(vf, rd, rn, 0, gt); break; |
| 2822 |
case NEON_CMGE_zero: cmp(vf, rd, rn, 0, ge); break; |
| 2823 |
case NEON_CMEQ_zero: cmp(vf, rd, rn, 0, eq); break; |
| 2824 |
case NEON_CMLE_zero: cmp(vf, rd, rn, 0, le); break; |
| 2825 |
case NEON_CMLT_zero: cmp(vf, rd, rn, 0, lt); break; |
| 2826 |
case NEON_ABS: abs(vf, rd, rn); break; |
| 2827 |
case NEON_NEG: neg(vf, rd, rn); break; |
| 2828 |
case NEON_SADDLP: saddlp(vf_lp, rd, rn); break; |
| 2829 |
case NEON_UADDLP: uaddlp(vf_lp, rd, rn); break; |
| 2830 |
case NEON_SADALP: sadalp(vf_lp, rd, rn); break; |
| 2831 |
case NEON_UADALP: uadalp(vf_lp, rd, rn); break; |
| 2832 |
case NEON_RBIT_NOT: |
| 2833 |
vf = nfd.GetVectorFormat(nfd.LogicalFormatMap()); |
| 2834 |
switch (instr->FPType()) { |
| 2835 |
case 0: not_(vf, rd, rn); break; |
| 2836 |
case 1: rbit(vf, rd, rn);; break; |
| 2837 |
default: |
| 2838 |
VIXL_UNIMPLEMENTED(); |
| 2839 |
} |
| 2840 |
break; |
| 2841 |
} |
| 2842 |
} else { |
| 2843 |
VectorFormat fpf = nfd.GetVectorFormat(nfd.FPFormatMap()); |
| 2844 |
FPRounding fpcr_rounding = static_cast<FPRounding>(fpcr().RMode()); |
| 2845 |
bool inexact_exception = false; |
| 2846 |
|
| 2847 |
// These instructions all use a one bit size field, except XTN, SQXTUN, |
| 2848 |
// SHLL, SQXTN and UQXTN, which use a two bit size field. |
| 2849 |
switch (instr->Mask(NEON2RegMiscFPMask)) { |
| 2850 |
case NEON_FABS: fabs_(fpf, rd, rn); return; |
| 2851 |
case NEON_FNEG: fneg(fpf, rd, rn); return; |
| 2852 |
case NEON_FSQRT: fsqrt(fpf, rd, rn); return; |
| 2853 |
case NEON_FCVTL: |
| 2854 |
if (instr->Mask(NEON_Q)) { |
| 2855 |
fcvtl2(vf_fcvtl, rd, rn); |
| 2856 |
} else { |
| 2857 |
fcvtl(vf_fcvtl, rd, rn); |
| 2858 |
} |
| 2859 |
return; |
| 2860 |
case NEON_FCVTN: |
| 2861 |
if (instr->Mask(NEON_Q)) { |
| 2862 |
fcvtn2(vf_fcvtn, rd, rn); |
| 2863 |
} else { |
| 2864 |
fcvtn(vf_fcvtn, rd, rn); |
| 2865 |
} |
| 2866 |
return; |
| 2867 |
case NEON_FCVTXN: |
| 2868 |
if (instr->Mask(NEON_Q)) { |
| 2869 |
fcvtxn2(vf_fcvtn, rd, rn); |
| 2870 |
} else { |
| 2871 |
fcvtxn(vf_fcvtn, rd, rn); |
| 2872 |
} |
| 2873 |
return; |
| 2874 |
|
| 2875 |
// The following instructions break from the switch statement, rather |
| 2876 |
// than return. |
| 2877 |
case NEON_FRINTI: break; // Use FPCR rounding mode. |
| 2878 |
case NEON_FRINTX: inexact_exception = true; break; |
| 2879 |
case NEON_FRINTA: fpcr_rounding = FPTieAway; break; |
| 2880 |
case NEON_FRINTM: fpcr_rounding = FPNegativeInfinity; break; |
| 2881 |
case NEON_FRINTN: fpcr_rounding = FPTieEven; break; |
| 2882 |
case NEON_FRINTP: fpcr_rounding = FPPositiveInfinity; break; |
| 2883 |
case NEON_FRINTZ: fpcr_rounding = FPZero; break; |
| 2884 |
|
| 2885 |
case NEON_FCVTNS: fcvts(fpf, rd, rn, FPTieEven); return; |
| 2886 |
case NEON_FCVTNU: fcvtu(fpf, rd, rn, FPTieEven); return; |
| 2887 |
case NEON_FCVTPS: fcvts(fpf, rd, rn, FPPositiveInfinity); return; |
| 2888 |
case NEON_FCVTPU: fcvtu(fpf, rd, rn, FPPositiveInfinity); return; |
| 2889 |
case NEON_FCVTMS: fcvts(fpf, rd, rn, FPNegativeInfinity); return; |
| 2890 |
case NEON_FCVTMU: fcvtu(fpf, rd, rn, FPNegativeInfinity); return; |
| 2891 |
case NEON_FCVTZS: fcvts(fpf, rd, rn, FPZero); return; |
| 2892 |
case NEON_FCVTZU: fcvtu(fpf, rd, rn, FPZero); return; |
| 2893 |
case NEON_FCVTAS: fcvts(fpf, rd, rn, FPTieAway); return; |
| 2894 |
case NEON_FCVTAU: fcvtu(fpf, rd, rn, FPTieAway); return; |
| 2895 |
case NEON_SCVTF: scvtf(fpf, rd, rn, 0, fpcr_rounding); return; |
| 2896 |
case NEON_UCVTF: ucvtf(fpf, rd, rn, 0, fpcr_rounding); return; |
| 2897 |
case NEON_URSQRTE: ursqrte(fpf, rd, rn); return; |
| 2898 |
case NEON_URECPE: urecpe(fpf, rd, rn); return; |
| 2899 |
case NEON_FRSQRTE: frsqrte(fpf, rd, rn); return; |
| 2900 |
case NEON_FRECPE: frecpe(fpf, rd, rn, fpcr_rounding); return; |
| 2901 |
case NEON_FCMGT_zero: fcmp_zero(fpf, rd, rn, gt); return; |
| 2902 |
case NEON_FCMGE_zero: fcmp_zero(fpf, rd, rn, ge); return; |
| 2903 |
case NEON_FCMEQ_zero: fcmp_zero(fpf, rd, rn, eq); return; |
| 2904 |
case NEON_FCMLE_zero: fcmp_zero(fpf, rd, rn, le); return; |
| 2905 |
case NEON_FCMLT_zero: fcmp_zero(fpf, rd, rn, lt); return; |
| 2906 |
default: |
| 2907 |
if ((NEON_XTN_opcode <= instr->Mask(NEON2RegMiscOpcode)) && |
| 2908 |
(instr->Mask(NEON2RegMiscOpcode) <= NEON_UQXTN_opcode)) { |
| 2909 |
switch (instr->Mask(NEON2RegMiscMask)) { |
| 2910 |
case NEON_XTN: xtn(vf, rd, rn); return; |
| 2911 |
case NEON_SQXTN: sqxtn(vf, rd, rn); return; |
| 2912 |
case NEON_UQXTN: uqxtn(vf, rd, rn); return; |
| 2913 |
case NEON_SQXTUN: sqxtun(vf, rd, rn); return; |
| 2914 |
case NEON_SHLL: |
| 2915 |
vf = nfd.GetVectorFormat(nfd.LongIntegerFormatMap()); |
| 2916 |
if (instr->Mask(NEON_Q)) { |
| 2917 |
shll2(vf, rd, rn); |
| 2918 |
} else { |
| 2919 |
shll(vf, rd, rn); |
| 2920 |
} |
| 2921 |
return; |
| 2922 |
default: |
| 2923 |
VIXL_UNIMPLEMENTED(); |
| 2924 |
} |
| 2925 |
} else { |
| 2926 |
VIXL_UNIMPLEMENTED(); |
| 2927 |
} |
| 2928 |
} |
| 2929 |
|
| 2930 |
// Only FRINT* instructions fall through the switch above. |
| 2931 |
frint(fpf, rd, rn, fpcr_rounding, inexact_exception); |
| 2932 |
} |
| 2933 |
} |
| 2934 |
|
| 2935 |
|
| 2936 |
void Simulator::VisitNEON3Same(const Instruction* instr) { |
| 2937 |
NEONFormatDecoder nfd(instr); |
| 2938 |
SimVRegister& rd = vreg(instr->Rd()); |
| 2939 |
SimVRegister& rn = vreg(instr->Rn()); |
| 2940 |
SimVRegister& rm = vreg(instr->Rm()); |
| 2941 |
|
| 2942 |
if (instr->Mask(NEON3SameLogicalFMask) == NEON3SameLogicalFixed) { |
| 2943 |
VectorFormat vf = nfd.GetVectorFormat(nfd.LogicalFormatMap()); |
| 2944 |
switch (instr->Mask(NEON3SameLogicalMask)) { |
| 2945 |
case NEON_AND: and_(vf, rd, rn, rm); break; |
| 2946 |
case NEON_ORR: orr(vf, rd, rn, rm); break; |
| 2947 |
case NEON_ORN: orn(vf, rd, rn, rm); break; |
| 2948 |
case NEON_EOR: eor(vf, rd, rn, rm); break; |
| 2949 |
case NEON_BIC: bic(vf, rd, rn, rm); break; |
| 2950 |
case NEON_BIF: bif(vf, rd, rn, rm); break; |
| 2951 |
case NEON_BIT: bit(vf, rd, rn, rm); break; |
| 2952 |
case NEON_BSL: bsl(vf, rd, rn, rm); break; |
| 2953 |
default: |
| 2954 |
VIXL_UNIMPLEMENTED(); |
| 2955 |
} |
| 2956 |
} else if (instr->Mask(NEON3SameFPFMask) == NEON3SameFPFixed) { |
| 2957 |
VectorFormat vf = nfd.GetVectorFormat(nfd.FPFormatMap()); |
| 2958 |
switch (instr->Mask(NEON3SameFPMask)) { |
| 2959 |
case NEON_FADD: fadd(vf, rd, rn, rm); break; |
| 2960 |
case NEON_FSUB: fsub(vf, rd, rn, rm); break; |
| 2961 |
case NEON_FMUL: fmul(vf, rd, rn, rm); break; |
| 2962 |
case NEON_FDIV: fdiv(vf, rd, rn, rm); break; |
| 2963 |
case NEON_FMAX: fmax(vf, rd, rn, rm); break; |
| 2964 |
case NEON_FMIN: fmin(vf, rd, rn, rm); break; |
| 2965 |
case NEON_FMAXNM: fmaxnm(vf, rd, rn, rm); break; |
| 2966 |
case NEON_FMINNM: fminnm(vf, rd, rn, rm); break; |
| 2967 |
case NEON_FMLA: fmla(vf, rd, rn, rm); break; |
| 2968 |
case NEON_FMLS: fmls(vf, rd, rn, rm); break; |
| 2969 |
case NEON_FMULX: fmulx(vf, rd, rn, rm); break; |
| 2970 |
case NEON_FACGE: fabscmp(vf, rd, rn, rm, ge); break; |
| 2971 |
case NEON_FACGT: fabscmp(vf, rd, rn, rm, gt); break; |
| 2972 |
case NEON_FCMEQ: fcmp(vf, rd, rn, rm, eq); break; |
| 2973 |
case NEON_FCMGE: fcmp(vf, rd, rn, rm, ge); break; |
| 2974 |
case NEON_FCMGT: fcmp(vf, rd, rn, rm, gt); break; |
| 2975 |
case NEON_FRECPS: frecps(vf, rd, rn, rm); break; |
| 2976 |
case NEON_FRSQRTS: frsqrts(vf, rd, rn, rm); break; |
| 2977 |
case NEON_FABD: fabd(vf, rd, rn, rm); break; |
| 2978 |
case NEON_FADDP: faddp(vf, rd, rn, rm); break; |
| 2979 |
case NEON_FMAXP: fmaxp(vf, rd, rn, rm); break; |
| 2980 |
case NEON_FMAXNMP: fmaxnmp(vf, rd, rn, rm); break; |
| 2981 |
case NEON_FMINP: fminp(vf, rd, rn, rm); break; |
| 2982 |
case NEON_FMINNMP: fminnmp(vf, rd, rn, rm); break; |
| 2983 |
default: |
| 2984 |
VIXL_UNIMPLEMENTED(); |
| 2985 |
} |
| 2986 |
} else { |
| 2987 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 2988 |
switch (instr->Mask(NEON3SameMask)) { |
| 2989 |
case NEON_ADD: add(vf, rd, rn, rm); break; |
| 2990 |
case NEON_ADDP: addp(vf, rd, rn, rm); break; |
| 2991 |
case NEON_CMEQ: cmp(vf, rd, rn, rm, eq); break; |
| 2992 |
case NEON_CMGE: cmp(vf, rd, rn, rm, ge); break; |
| 2993 |
case NEON_CMGT: cmp(vf, rd, rn, rm, gt); break; |
| 2994 |
case NEON_CMHI: cmp(vf, rd, rn, rm, hi); break; |
| 2995 |
case NEON_CMHS: cmp(vf, rd, rn, rm, hs); break; |
| 2996 |
case NEON_CMTST: cmptst(vf, rd, rn, rm); break; |
| 2997 |
case NEON_MLS: mls(vf, rd, rn, rm); break; |
| 2998 |
case NEON_MLA: mla(vf, rd, rn, rm); break; |
| 2999 |
case NEON_MUL: mul(vf, rd, rn, rm); break; |
| 3000 |
case NEON_PMUL: pmul(vf, rd, rn, rm); break; |
| 3001 |
case NEON_SMAX: smax(vf, rd, rn, rm); break; |
| 3002 |
case NEON_SMAXP: smaxp(vf, rd, rn, rm); break; |
| 3003 |
case NEON_SMIN: smin(vf, rd, rn, rm); break; |
| 3004 |
case NEON_SMINP: sminp(vf, rd, rn, rm); break; |
| 3005 |
case NEON_SUB: sub(vf, rd, rn, rm); break; |
| 3006 |
case NEON_UMAX: umax(vf, rd, rn, rm); break; |
| 3007 |
case NEON_UMAXP: umaxp(vf, rd, rn, rm); break; |
| 3008 |
case NEON_UMIN: umin(vf, rd, rn, rm); break; |
| 3009 |
case NEON_UMINP: uminp(vf, rd, rn, rm); break; |
| 3010 |
case NEON_SSHL: sshl(vf, rd, rn, rm); break; |
| 3011 |
case NEON_USHL: ushl(vf, rd, rn, rm); break; |
| 3012 |
case NEON_SABD: absdiff(vf, rd, rn, rm, true); break; |
| 3013 |
case NEON_UABD: absdiff(vf, rd, rn, rm, false); break; |
| 3014 |
case NEON_SABA: saba(vf, rd, rn, rm); break; |
| 3015 |
case NEON_UABA: uaba(vf, rd, rn, rm); break; |
| 3016 |
case NEON_UQADD: add(vf, rd, rn, rm).UnsignedSaturate(vf); break; |
| 3017 |
case NEON_SQADD: add(vf, rd, rn, rm).SignedSaturate(vf); break; |
| 3018 |
case NEON_UQSUB: sub(vf, rd, rn, rm).UnsignedSaturate(vf); break; |
| 3019 |
case NEON_SQSUB: sub(vf, rd, rn, rm).SignedSaturate(vf); break; |
| 3020 |
case NEON_SQDMULH: sqdmulh(vf, rd, rn, rm); break; |
| 3021 |
case NEON_SQRDMULH: sqrdmulh(vf, rd, rn, rm); break; |
| 3022 |
case NEON_UQSHL: ushl(vf, rd, rn, rm).UnsignedSaturate(vf); break; |
| 3023 |
case NEON_SQSHL: sshl(vf, rd, rn, rm).SignedSaturate(vf); break; |
| 3024 |
case NEON_URSHL: ushl(vf, rd, rn, rm).Round(vf); break; |
| 3025 |
case NEON_SRSHL: sshl(vf, rd, rn, rm).Round(vf); break; |
| 3026 |
case NEON_UQRSHL: |
| 3027 |
ushl(vf, rd, rn, rm).Round(vf).UnsignedSaturate(vf); |
| 3028 |
break; |
| 3029 |
case NEON_SQRSHL: |
| 3030 |
sshl(vf, rd, rn, rm).Round(vf).SignedSaturate(vf); |
| 3031 |
break; |
| 3032 |
case NEON_UHADD: |
| 3033 |
add(vf, rd, rn, rm).Uhalve(vf); |
| 3034 |
break; |
| 3035 |
case NEON_URHADD: |
| 3036 |
add(vf, rd, rn, rm).Uhalve(vf).Round(vf); |
| 3037 |
break; |
| 3038 |
case NEON_SHADD: |
| 3039 |
add(vf, rd, rn, rm).Halve(vf); |
| 3040 |
break; |
| 3041 |
case NEON_SRHADD: |
| 3042 |
add(vf, rd, rn, rm).Halve(vf).Round(vf); |
| 3043 |
break; |
| 3044 |
case NEON_UHSUB: |
| 3045 |
sub(vf, rd, rn, rm).Uhalve(vf); |
| 3046 |
break; |
| 3047 |
case NEON_SHSUB: |
| 3048 |
sub(vf, rd, rn, rm).Halve(vf); |
| 3049 |
break; |
| 3050 |
default: |
| 3051 |
VIXL_UNIMPLEMENTED(); |
| 3052 |
} |
| 3053 |
} |
| 3054 |
} |
| 3055 |
|
| 3056 |
|
| 3057 |
void Simulator::VisitNEON3Different(const Instruction* instr) { |
| 3058 |
NEONFormatDecoder nfd(instr); |
| 3059 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 3060 |
VectorFormat vf_l = nfd.GetVectorFormat(nfd.LongIntegerFormatMap()); |
| 3061 |
|
| 3062 |
SimVRegister& rd = vreg(instr->Rd()); |
| 3063 |
SimVRegister& rn = vreg(instr->Rn()); |
| 3064 |
SimVRegister& rm = vreg(instr->Rm()); |
| 3065 |
|
| 3066 |
switch (instr->Mask(NEON3DifferentMask)) { |
| 3067 |
case NEON_PMULL: pmull(vf_l, rd, rn, rm); break; |
| 3068 |
case NEON_PMULL2: pmull2(vf_l, rd, rn, rm); break; |
| 3069 |
case NEON_UADDL: uaddl(vf_l, rd, rn, rm); break; |
| 3070 |
case NEON_UADDL2: uaddl2(vf_l, rd, rn, rm); break; |
| 3071 |
case NEON_SADDL: saddl(vf_l, rd, rn, rm); break; |
| 3072 |
case NEON_SADDL2: saddl2(vf_l, rd, rn, rm); break; |
| 3073 |
case NEON_USUBL: usubl(vf_l, rd, rn, rm); break; |
| 3074 |
case NEON_USUBL2: usubl2(vf_l, rd, rn, rm); break; |
| 3075 |
case NEON_SSUBL: ssubl(vf_l, rd, rn, rm); break; |
| 3076 |
case NEON_SSUBL2: ssubl2(vf_l, rd, rn, rm); break; |
| 3077 |
case NEON_SABAL: sabal(vf_l, rd, rn, rm); break; |
| 3078 |
case NEON_SABAL2: sabal2(vf_l, rd, rn, rm); break; |
| 3079 |
case NEON_UABAL: uabal(vf_l, rd, rn, rm); break; |
| 3080 |
case NEON_UABAL2: uabal2(vf_l, rd, rn, rm); break; |
| 3081 |
case NEON_SABDL: sabdl(vf_l, rd, rn, rm); break; |
| 3082 |
case NEON_SABDL2: sabdl2(vf_l, rd, rn, rm); break; |
| 3083 |
case NEON_UABDL: uabdl(vf_l, rd, rn, rm); break; |
| 3084 |
case NEON_UABDL2: uabdl2(vf_l, rd, rn, rm); break; |
| 3085 |
case NEON_SMLAL: smlal(vf_l, rd, rn, rm); break; |
| 3086 |
case NEON_SMLAL2: smlal2(vf_l, rd, rn, rm); break; |
| 3087 |
case NEON_UMLAL: umlal(vf_l, rd, rn, rm); break; |
| 3088 |
case NEON_UMLAL2: umlal2(vf_l, rd, rn, rm); break; |
| 3089 |
case NEON_SMLSL: smlsl(vf_l, rd, rn, rm); break; |
| 3090 |
case NEON_SMLSL2: smlsl2(vf_l, rd, rn, rm); break; |
| 3091 |
case NEON_UMLSL: umlsl(vf_l, rd, rn, rm); break; |
| 3092 |
case NEON_UMLSL2: umlsl2(vf_l, rd, rn, rm); break; |
| 3093 |
case NEON_SMULL: smull(vf_l, rd, rn, rm); break; |
| 3094 |
case NEON_SMULL2: smull2(vf_l, rd, rn, rm); break; |
| 3095 |
case NEON_UMULL: umull(vf_l, rd, rn, rm); break; |
| 3096 |
case NEON_UMULL2: umull2(vf_l, rd, rn, rm); break; |
| 3097 |
case NEON_SQDMLAL: sqdmlal(vf_l, rd, rn, rm); break; |
| 3098 |
case NEON_SQDMLAL2: sqdmlal2(vf_l, rd, rn, rm); break; |
| 3099 |
case NEON_SQDMLSL: sqdmlsl(vf_l, rd, rn, rm); break; |
| 3100 |
case NEON_SQDMLSL2: sqdmlsl2(vf_l, rd, rn, rm); break; |
| 3101 |
case NEON_SQDMULL: sqdmull(vf_l, rd, rn, rm); break; |
| 3102 |
case NEON_SQDMULL2: sqdmull2(vf_l, rd, rn, rm); break; |
| 3103 |
case NEON_UADDW: uaddw(vf_l, rd, rn, rm); break; |
| 3104 |
case NEON_UADDW2: uaddw2(vf_l, rd, rn, rm); break; |
| 3105 |
case NEON_SADDW: saddw(vf_l, rd, rn, rm); break; |
| 3106 |
case NEON_SADDW2: saddw2(vf_l, rd, rn, rm); break; |
| 3107 |
case NEON_USUBW: usubw(vf_l, rd, rn, rm); break; |
| 3108 |
case NEON_USUBW2: usubw2(vf_l, rd, rn, rm); break; |
| 3109 |
case NEON_SSUBW: ssubw(vf_l, rd, rn, rm); break; |
| 3110 |
case NEON_SSUBW2: ssubw2(vf_l, rd, rn, rm); break; |
| 3111 |
case NEON_ADDHN: addhn(vf, rd, rn, rm); break; |
| 3112 |
case NEON_ADDHN2: addhn2(vf, rd, rn, rm); break; |
| 3113 |
case NEON_RADDHN: raddhn(vf, rd, rn, rm); break; |
| 3114 |
case NEON_RADDHN2: raddhn2(vf, rd, rn, rm); break; |
| 3115 |
case NEON_SUBHN: subhn(vf, rd, rn, rm); break; |
| 3116 |
case NEON_SUBHN2: subhn2(vf, rd, rn, rm); break; |
| 3117 |
case NEON_RSUBHN: rsubhn(vf, rd, rn, rm); break; |
| 3118 |
case NEON_RSUBHN2: rsubhn2(vf, rd, rn, rm); break; |
| 3119 |
default: |
| 3120 |
VIXL_UNIMPLEMENTED(); |
| 3121 |
} |
| 3122 |
} |
| 3123 |
|
| 3124 |
|
| 3125 |
void Simulator::VisitNEONAcrossLanes(const Instruction* instr) { |
| 3126 |
NEONFormatDecoder nfd(instr); |
| 3127 |
|
| 3128 |
SimVRegister& rd = vreg(instr->Rd()); |
| 3129 |
SimVRegister& rn = vreg(instr->Rn()); |
| 3130 |
|
| 3131 |
// The input operand's VectorFormat is passed for these instructions. |
| 3132 |
if (instr->Mask(NEONAcrossLanesFPFMask) == NEONAcrossLanesFPFixed) { |
| 3133 |
VectorFormat vf = nfd.GetVectorFormat(nfd.FPFormatMap()); |
| 3134 |
|
| 3135 |
switch (instr->Mask(NEONAcrossLanesFPMask)) { |
| 3136 |
case NEON_FMAXV: fmaxv(vf, rd, rn); break; |
| 3137 |
case NEON_FMINV: fminv(vf, rd, rn); break; |
| 3138 |
case NEON_FMAXNMV: fmaxnmv(vf, rd, rn); break; |
| 3139 |
case NEON_FMINNMV: fminnmv(vf, rd, rn); break; |
| 3140 |
default: |
| 3141 |
VIXL_UNIMPLEMENTED(); |
| 3142 |
} |
| 3143 |
} else { |
| 3144 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 3145 |
|
| 3146 |
switch (instr->Mask(NEONAcrossLanesMask)) { |
| 3147 |
case NEON_ADDV: addv(vf, rd, rn); break; |
| 3148 |
case NEON_SMAXV: smaxv(vf, rd, rn); break; |
| 3149 |
case NEON_SMINV: sminv(vf, rd, rn); break; |
| 3150 |
case NEON_UMAXV: umaxv(vf, rd, rn); break; |
| 3151 |
case NEON_UMINV: uminv(vf, rd, rn); break; |
| 3152 |
case NEON_SADDLV: saddlv(vf, rd, rn); break; |
| 3153 |
case NEON_UADDLV: uaddlv(vf, rd, rn); break; |
| 3154 |
default: |
| 3155 |
VIXL_UNIMPLEMENTED(); |
| 3156 |
} |
| 3157 |
} |
| 3158 |
} |
| 3159 |
|
| 3160 |
|
| 3161 |
void Simulator::VisitNEONByIndexedElement(const Instruction* instr) { |
| 3162 |
NEONFormatDecoder nfd(instr); |
| 3163 |
VectorFormat vf_r = nfd.GetVectorFormat(); |
| 3164 |
VectorFormat vf = nfd.GetVectorFormat(nfd.LongIntegerFormatMap()); |
| 3165 |
|
| 3166 |
SimVRegister& rd = vreg(instr->Rd()); |
| 3167 |
SimVRegister& rn = vreg(instr->Rn()); |
| 3168 |
|
| 3169 |
ByElementOp Op = NULL; |
| 3170 |
|
| 3171 |
int rm_reg = instr->Rm(); |
| 3172 |
int index = (instr->NEONH() << 1) | instr->NEONL(); |
| 3173 |
if (instr->NEONSize() == 1) { |
| 3174 |
rm_reg &= 0xf; |
| 3175 |
index = (index << 1) | instr->NEONM(); |
| 3176 |
} |
| 3177 |
|
| 3178 |
switch (instr->Mask(NEONByIndexedElementMask)) { |
| 3179 |
case NEON_MUL_byelement: Op = &Simulator::mul; vf = vf_r; break; |
| 3180 |
case NEON_MLA_byelement: Op = &Simulator::mla; vf = vf_r; break; |
| 3181 |
case NEON_MLS_byelement: Op = &Simulator::mls; vf = vf_r; break; |
| 3182 |
case NEON_SQDMULH_byelement: Op = &Simulator::sqdmulh; vf = vf_r; break; |
| 3183 |
case NEON_SQRDMULH_byelement: Op = &Simulator::sqrdmulh; vf = vf_r; break; |
| 3184 |
case NEON_SMULL_byelement: |
| 3185 |
if (instr->Mask(NEON_Q)) { |
| 3186 |
Op = &Simulator::smull2; |
| 3187 |
} else { |
| 3188 |
Op = &Simulator::smull; |
| 3189 |
} |
| 3190 |
break; |
| 3191 |
case NEON_UMULL_byelement: |
| 3192 |
if (instr->Mask(NEON_Q)) { |
| 3193 |
Op = &Simulator::umull2; |
| 3194 |
} else { |
| 3195 |
Op = &Simulator::umull; |
| 3196 |
} |
| 3197 |
break; |
| 3198 |
case NEON_SMLAL_byelement: |
| 3199 |
if (instr->Mask(NEON_Q)) { |
| 3200 |
Op = &Simulator::smlal2; |
| 3201 |
} else { |
| 3202 |
Op = &Simulator::smlal; |
| 3203 |
} |
| 3204 |
break; |
| 3205 |
case NEON_UMLAL_byelement: |
| 3206 |
if (instr->Mask(NEON_Q)) { |
| 3207 |
Op = &Simulator::umlal2; |
| 3208 |
} else { |
| 3209 |
Op = &Simulator::umlal; |
| 3210 |
} |
| 3211 |
break; |
| 3212 |
case NEON_SMLSL_byelement: |
| 3213 |
if (instr->Mask(NEON_Q)) { |
| 3214 |
Op = &Simulator::smlsl2; |
| 3215 |
} else { |
| 3216 |
Op = &Simulator::smlsl; |
| 3217 |
} |
| 3218 |
break; |
| 3219 |
case NEON_UMLSL_byelement: |
| 3220 |
if (instr->Mask(NEON_Q)) { |
| 3221 |
Op = &Simulator::umlsl2; |
| 3222 |
} else { |
| 3223 |
Op = &Simulator::umlsl; |
| 3224 |
} |
| 3225 |
break; |
| 3226 |
case NEON_SQDMULL_byelement: |
| 3227 |
if (instr->Mask(NEON_Q)) { |
| 3228 |
Op = &Simulator::sqdmull2; |
| 3229 |
} else { |
| 3230 |
Op = &Simulator::sqdmull; |
| 3231 |
} |
| 3232 |
break; |
| 3233 |
case NEON_SQDMLAL_byelement: |
| 3234 |
if (instr->Mask(NEON_Q)) { |
| 3235 |
Op = &Simulator::sqdmlal2; |
| 3236 |
} else { |
| 3237 |
Op = &Simulator::sqdmlal; |
| 3238 |
} |
| 3239 |
break; |
| 3240 |
case NEON_SQDMLSL_byelement: |
| 3241 |
if (instr->Mask(NEON_Q)) { |
| 3242 |
Op = &Simulator::sqdmlsl2; |
| 3243 |
} else { |
| 3244 |
Op = &Simulator::sqdmlsl; |
| 3245 |
} |
| 3246 |
break; |
| 3247 |
default: |
| 3248 |
index = instr->NEONH(); |
| 3249 |
if ((instr->FPType() & 1) == 0) { |
| 3250 |
index = (index << 1) | instr->NEONL(); |
| 3251 |
} |
| 3252 |
|
| 3253 |
vf = nfd.GetVectorFormat(nfd.FPFormatMap()); |
| 3254 |
|
| 3255 |
switch (instr->Mask(NEONByIndexedElementFPMask)) { |
| 3256 |
case NEON_FMUL_byelement: Op = &Simulator::fmul; break; |
| 3257 |
case NEON_FMLA_byelement: Op = &Simulator::fmla; break; |
| 3258 |
case NEON_FMLS_byelement: Op = &Simulator::fmls; break; |
| 3259 |
case NEON_FMULX_byelement: Op = &Simulator::fmulx; break; |
| 3260 |
default: VIXL_UNIMPLEMENTED(); |
| 3261 |
} |
| 3262 |
} |
| 3263 |
|
| 3264 |
(this->*Op)(vf, rd, rn, vreg(rm_reg), index); |
| 3265 |
} |
| 3266 |
|
| 3267 |
|
| 3268 |
void Simulator::VisitNEONCopy(const Instruction* instr) { |
| 3269 |
NEONFormatDecoder nfd(instr, NEONFormatDecoder::TriangularFormatMap()); |
| 3270 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 3271 |
|
| 3272 |
SimVRegister& rd = vreg(instr->Rd()); |
| 3273 |
SimVRegister& rn = vreg(instr->Rn()); |
| 3274 |
int imm5 = instr->ImmNEON5(); |
| 3275 |
int tz = CountTrailingZeros(imm5, 32); |
| 3276 |
int reg_index = imm5 >> (tz + 1); |
| 3277 |
|
| 3278 |
if (instr->Mask(NEONCopyInsElementMask) == NEON_INS_ELEMENT) { |
| 3279 |
int imm4 = instr->ImmNEON4(); |
| 3280 |
int rn_index = imm4 >> tz; |
| 3281 |
ins_element(vf, rd, reg_index, rn, rn_index); |
| 3282 |
} else if (instr->Mask(NEONCopyInsGeneralMask) == NEON_INS_GENERAL) { |
| 3283 |
ins_immediate(vf, rd, reg_index, xreg(instr->Rn())); |
| 3284 |
} else if (instr->Mask(NEONCopyUmovMask) == NEON_UMOV) { |
| 3285 |
uint64_t value = LogicVRegister(rn).Uint(vf, reg_index); |
| 3286 |
value &= MaxUintFromFormat(vf); |
| 3287 |
set_xreg(instr->Rd(), value); |
| 3288 |
} else if (instr->Mask(NEONCopyUmovMask) == NEON_SMOV) { |
| 3289 |
int64_t value = LogicVRegister(rn).Int(vf, reg_index); |
| 3290 |
if (instr->NEONQ()) { |
| 3291 |
set_xreg(instr->Rd(), value); |
| 3292 |
} else { |
| 3293 |
set_wreg(instr->Rd(), (int32_t)value); |
| 3294 |
} |
| 3295 |
} else if (instr->Mask(NEONCopyDupElementMask) == NEON_DUP_ELEMENT) { |
| 3296 |
dup_element(vf, rd, rn, reg_index); |
| 3297 |
} else if (instr->Mask(NEONCopyDupGeneralMask) == NEON_DUP_GENERAL) { |
| 3298 |
dup_immediate(vf, rd, xreg(instr->Rn())); |
| 3299 |
} else { |
| 3300 |
VIXL_UNIMPLEMENTED(); |
| 3301 |
} |
| 3302 |
} |
| 3303 |
|
| 3304 |
|
| 3305 |
void Simulator::VisitNEONExtract(const Instruction* instr) { |
| 3306 |
NEONFormatDecoder nfd(instr, NEONFormatDecoder::LogicalFormatMap()); |
| 3307 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 3308 |
SimVRegister& rd = vreg(instr->Rd()); |
| 3309 |
SimVRegister& rn = vreg(instr->Rn()); |
| 3310 |
SimVRegister& rm = vreg(instr->Rm()); |
| 3311 |
if (instr->Mask(NEONExtractMask) == NEON_EXT) { |
| 3312 |
int index = instr->ImmNEONExt(); |
| 3313 |
ext(vf, rd, rn, rm, index); |
| 3314 |
} else { |
| 3315 |
VIXL_UNIMPLEMENTED(); |
| 3316 |
} |
| 3317 |
} |
| 3318 |
|
| 3319 |
|
| 3320 |
void Simulator::NEONLoadStoreMultiStructHelper(const Instruction* instr, |
| 3321 |
AddrMode addr_mode) { |
| 3322 |
NEONFormatDecoder nfd(instr, NEONFormatDecoder::LoadStoreFormatMap()); |
| 3323 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 3324 |
|
| 3325 |
uint64_t addr_base = xreg(instr->Rn(), Reg31IsStackPointer); |
| 3326 |
int reg_size = RegisterSizeInBytesFromFormat(vf); |
| 3327 |
|
| 3328 |
int reg[4]; |
| 3329 |
uint64_t addr[4]; |
| 3330 |
for (int i = 0; i < 4; i++) { |
| 3331 |
reg[i] = (instr->Rt() + i) % kNumberOfVRegisters; |
| 3332 |
addr[i] = addr_base + (i * reg_size); |
| 3333 |
} |
| 3334 |
int count = 1; |
| 3335 |
bool log_read = true; |
| 3336 |
|
| 3337 |
Instr itype = instr->Mask(NEONLoadStoreMultiStructMask); |
| 3338 |
if (((itype == NEON_LD1_1v) || (itype == NEON_LD1_2v) || |
| 3339 |
(itype == NEON_LD1_3v) || (itype == NEON_LD1_4v) || |
| 3340 |
(itype == NEON_ST1_1v) || (itype == NEON_ST1_2v) || |
| 3341 |
(itype == NEON_ST1_3v) || (itype == NEON_ST1_4v)) && |
| 3342 |
(instr->Bits(20, 16) != 0)) { |
| 3343 |
VIXL_UNREACHABLE(); |
| 3344 |
} |
| 3345 |
|
| 3346 |
// We use the PostIndex mask here, as it works in this case for both Offset |
| 3347 |
// and PostIndex addressing. |
| 3348 |
switch (instr->Mask(NEONLoadStoreMultiStructPostIndexMask)) { |
| 3349 |
case NEON_LD1_4v: |
| 3350 |
case NEON_LD1_4v_post: ld1(vf, vreg(reg[3]), addr[3]); count++; |
| 3351 |
VIXL_FALLTHROUGH(); |
| 3352 |
case NEON_LD1_3v: |
| 3353 |
case NEON_LD1_3v_post: ld1(vf, vreg(reg[2]), addr[2]); count++; |
| 3354 |
VIXL_FALLTHROUGH(); |
| 3355 |
case NEON_LD1_2v: |
| 3356 |
case NEON_LD1_2v_post: ld1(vf, vreg(reg[1]), addr[1]); count++; |
| 3357 |
VIXL_FALLTHROUGH(); |
| 3358 |
case NEON_LD1_1v: |
| 3359 |
case NEON_LD1_1v_post: |
| 3360 |
ld1(vf, vreg(reg[0]), addr[0]); |
| 3361 |
log_read = true; |
| 3362 |
break; |
| 3363 |
case NEON_ST1_4v: |
| 3364 |
case NEON_ST1_4v_post: st1(vf, vreg(reg[3]), addr[3]); count++; |
| 3365 |
VIXL_FALLTHROUGH(); |
| 3366 |
case NEON_ST1_3v: |
| 3367 |
case NEON_ST1_3v_post: st1(vf, vreg(reg[2]), addr[2]); count++; |
| 3368 |
VIXL_FALLTHROUGH(); |
| 3369 |
case NEON_ST1_2v: |
| 3370 |
case NEON_ST1_2v_post: st1(vf, vreg(reg[1]), addr[1]); count++; |
| 3371 |
VIXL_FALLTHROUGH(); |
| 3372 |
case NEON_ST1_1v: |
| 3373 |
case NEON_ST1_1v_post: |
| 3374 |
st1(vf, vreg(reg[0]), addr[0]); |
| 3375 |
log_read = false; |
| 3376 |
break; |
| 3377 |
case NEON_LD2_post: |
| 3378 |
case NEON_LD2: |
| 3379 |
ld2(vf, vreg(reg[0]), vreg(reg[1]), addr[0]); |
| 3380 |
count = 2; |
| 3381 |
break; |
| 3382 |
case NEON_ST2: |
| 3383 |
case NEON_ST2_post: |
| 3384 |
st2(vf, vreg(reg[0]), vreg(reg[1]), addr[0]); |
| 3385 |
count = 2; |
| 3386 |
break; |
| 3387 |
case NEON_LD3_post: |
| 3388 |
case NEON_LD3: |
| 3389 |
ld3(vf, vreg(reg[0]), vreg(reg[1]), vreg(reg[2]), addr[0]); |
| 3390 |
count = 3; |
| 3391 |
break; |
| 3392 |
case NEON_ST3: |
| 3393 |
case NEON_ST3_post: |
| 3394 |
st3(vf, vreg(reg[0]), vreg(reg[1]), vreg(reg[2]), addr[0]); |
| 3395 |
count = 3; |
| 3396 |
break; |
| 3397 |
case NEON_ST4: |
| 3398 |
case NEON_ST4_post: |
| 3399 |
st4(vf, vreg(reg[0]), vreg(reg[1]), vreg(reg[2]), vreg(reg[3]), |
| 3400 |
addr[0]); |
| 3401 |
count = 4; |
| 3402 |
break; |
| 3403 |
case NEON_LD4_post: |
| 3404 |
case NEON_LD4: |
| 3405 |
ld4(vf, vreg(reg[0]), vreg(reg[1]), vreg(reg[2]), vreg(reg[3]), |
| 3406 |
addr[0]); |
| 3407 |
count = 4; |
| 3408 |
break; |
| 3409 |
default: VIXL_UNIMPLEMENTED(); |
| 3410 |
} |
| 3411 |
|
| 3412 |
// Explicitly log the register update whilst we have type information. |
| 3413 |
for (int i = 0; i < count; i++) { |
| 3414 |
// For de-interleaving loads, only print the base address. |
| 3415 |
int lane_size = LaneSizeInBytesFromFormat(vf); |
| 3416 |
PrintRegisterFormat format = GetPrintRegisterFormatTryFP( |
| 3417 |
GetPrintRegisterFormatForSize(reg_size, lane_size)); |
| 3418 |
if (log_read) { |
| 3419 |
LogVRead(addr_base, reg[i], format); |
| 3420 |
} else { |
| 3421 |
LogVWrite(addr_base, reg[i], format); |
| 3422 |
} |
| 3423 |
} |
| 3424 |
|
| 3425 |
if (addr_mode == PostIndex) { |
| 3426 |
int rm = instr->Rm(); |
| 3427 |
// The immediate post index addressing mode is indicated by rm = 31. |
| 3428 |
// The immediate is implied by the number of vector registers used. |
| 3429 |
addr_base += (rm == 31) ? RegisterSizeInBytesFromFormat(vf) * count |
| 3430 |
: xreg(rm); |
| 3431 |
set_xreg(instr->Rn(), addr_base); |
| 3432 |
} else { |
| 3433 |
VIXL_ASSERT(addr_mode == Offset); |
| 3434 |
} |
| 3435 |
} |
| 3436 |
|
| 3437 |
|
| 3438 |
void Simulator::VisitNEONLoadStoreMultiStruct(const Instruction* instr) { |
| 3439 |
NEONLoadStoreMultiStructHelper(instr, Offset); |
| 3440 |
} |
| 3441 |
|
| 3442 |
|
| 3443 |
void Simulator::VisitNEONLoadStoreMultiStructPostIndex( |
| 3444 |
const Instruction* instr) { |
| 3445 |
NEONLoadStoreMultiStructHelper(instr, PostIndex); |
| 3446 |
} |
| 3447 |
|
| 3448 |
|
| 3449 |
void Simulator::NEONLoadStoreSingleStructHelper(const Instruction* instr, |
| 3450 |
AddrMode addr_mode) { |
| 3451 |
uint64_t addr = xreg(instr->Rn(), Reg31IsStackPointer); |
| 3452 |
int rt = instr->Rt(); |
| 3453 |
|
| 3454 |
Instr itype = instr->Mask(NEONLoadStoreSingleStructMask); |
| 3455 |
if (((itype == NEON_LD1_b) || (itype == NEON_LD1_h) || |
| 3456 |
(itype == NEON_LD1_s) || (itype == NEON_LD1_d)) && |
| 3457 |
(instr->Bits(20, 16) != 0)) { |
| 3458 |
VIXL_UNREACHABLE(); |
| 3459 |
} |
| 3460 |
|
| 3461 |
// We use the PostIndex mask here, as it works in this case for both Offset |
| 3462 |
// and PostIndex addressing. |
| 3463 |
bool do_load = false; |
| 3464 |
|
| 3465 |
bool replicating = false; |
| 3466 |
|
| 3467 |
NEONFormatDecoder nfd(instr, NEONFormatDecoder::LoadStoreFormatMap()); |
| 3468 |
VectorFormat vf_t = nfd.GetVectorFormat(); |
| 3469 |
|
| 3470 |
VectorFormat vf = kFormat16B; |
| 3471 |
switch (instr->Mask(NEONLoadStoreSingleStructPostIndexMask)) { |
| 3472 |
case NEON_LD1_b: |
| 3473 |
case NEON_LD1_b_post: |
| 3474 |
case NEON_LD2_b: |
| 3475 |
case NEON_LD2_b_post: |
| 3476 |
case NEON_LD3_b: |
| 3477 |
case NEON_LD3_b_post: |
| 3478 |
case NEON_LD4_b: |
| 3479 |
case NEON_LD4_b_post: do_load = true; |
| 3480 |
VIXL_FALLTHROUGH(); |
| 3481 |
case NEON_ST1_b: |
| 3482 |
case NEON_ST1_b_post: |
| 3483 |
case NEON_ST2_b: |
| 3484 |
case NEON_ST2_b_post: |
| 3485 |
case NEON_ST3_b: |
| 3486 |
case NEON_ST3_b_post: |
| 3487 |
case NEON_ST4_b: |
| 3488 |
case NEON_ST4_b_post: break; |
| 3489 |
|
| 3490 |
case NEON_LD1_h: |
| 3491 |
case NEON_LD1_h_post: |
| 3492 |
case NEON_LD2_h: |
| 3493 |
case NEON_LD2_h_post: |
| 3494 |
case NEON_LD3_h: |
| 3495 |
case NEON_LD3_h_post: |
| 3496 |
case NEON_LD4_h: |
| 3497 |
case NEON_LD4_h_post: do_load = true; |
| 3498 |
VIXL_FALLTHROUGH(); |
| 3499 |
case NEON_ST1_h: |
| 3500 |
case NEON_ST1_h_post: |
| 3501 |
case NEON_ST2_h: |
| 3502 |
case NEON_ST2_h_post: |
| 3503 |
case NEON_ST3_h: |
| 3504 |
case NEON_ST3_h_post: |
| 3505 |
case NEON_ST4_h: |
| 3506 |
case NEON_ST4_h_post: vf = kFormat8H; break; |
| 3507 |
case NEON_LD1_s: |
| 3508 |
case NEON_LD1_s_post: |
| 3509 |
case NEON_LD2_s: |
| 3510 |
case NEON_LD2_s_post: |
| 3511 |
case NEON_LD3_s: |
| 3512 |
case NEON_LD3_s_post: |
| 3513 |
case NEON_LD4_s: |
| 3514 |
case NEON_LD4_s_post: do_load = true; |
| 3515 |
VIXL_FALLTHROUGH(); |
| 3516 |
case NEON_ST1_s: |
| 3517 |
case NEON_ST1_s_post: |
| 3518 |
case NEON_ST2_s: |
| 3519 |
case NEON_ST2_s_post: |
| 3520 |
case NEON_ST3_s: |
| 3521 |
case NEON_ST3_s_post: |
| 3522 |
case NEON_ST4_s: |
| 3523 |
case NEON_ST4_s_post: { |
| 3524 |
VIXL_STATIC_ASSERT((NEON_LD1_s | (1 << NEONLSSize_offset)) == NEON_LD1_d); |
| 3525 |
VIXL_STATIC_ASSERT( |
| 3526 |
(NEON_LD1_s_post | (1 << NEONLSSize_offset)) == NEON_LD1_d_post); |
| 3527 |
VIXL_STATIC_ASSERT((NEON_ST1_s | (1 << NEONLSSize_offset)) == NEON_ST1_d); |
| 3528 |
VIXL_STATIC_ASSERT( |
| 3529 |
(NEON_ST1_s_post | (1 << NEONLSSize_offset)) == NEON_ST1_d_post); |
| 3530 |
vf = ((instr->NEONLSSize() & 1) == 0) ? kFormat4S : kFormat2D; |
| 3531 |
break; |
| 3532 |
} |
| 3533 |
|
| 3534 |
case NEON_LD1R: |
| 3535 |
case NEON_LD1R_post: |
| 3536 |
case NEON_LD2R: |
| 3537 |
case NEON_LD2R_post: |
| 3538 |
case NEON_LD3R: |
| 3539 |
case NEON_LD3R_post: |
| 3540 |
case NEON_LD4R: |
| 3541 |
case NEON_LD4R_post: { |
| 3542 |
vf = vf_t; |
| 3543 |
do_load = true; |
| 3544 |
replicating = true; |
| 3545 |
break; |
| 3546 |
} |
| 3547 |
default: VIXL_UNIMPLEMENTED(); |
| 3548 |
} |
| 3549 |
|
| 3550 |
PrintRegisterFormat print_format = |
| 3551 |
GetPrintRegisterFormatTryFP(GetPrintRegisterFormat(vf)); |
| 3552 |
// Make sure that the print_format only includes a single lane. |
| 3553 |
print_format = |
| 3554 |
static_cast<PrintRegisterFormat>(print_format & ~kPrintRegAsVectorMask); |
| 3555 |
|
| 3556 |
int esize = LaneSizeInBytesFromFormat(vf); |
| 3557 |
int index_shift = LaneSizeInBytesLog2FromFormat(vf); |
| 3558 |
int lane = instr->NEONLSIndex(index_shift); |
| 3559 |
int scale = 0; |
| 3560 |
int rt2 = (rt + 1) % kNumberOfVRegisters; |
| 3561 |
int rt3 = (rt2 + 1) % kNumberOfVRegisters; |
| 3562 |
int rt4 = (rt3 + 1) % kNumberOfVRegisters; |
| 3563 |
switch (instr->Mask(NEONLoadStoreSingleLenMask)) { |
| 3564 |
case NEONLoadStoreSingle1: |
| 3565 |
scale = 1; |
| 3566 |
if (do_load) { |
| 3567 |
if (replicating) { |
| 3568 |
ld1r(vf, vreg(rt), addr); |
| 3569 |
} else { |
| 3570 |
ld1(vf, vreg(rt), lane, addr); |
| 3571 |
} |
| 3572 |
LogVRead(addr, rt, print_format, lane); |
| 3573 |
} else { |
| 3574 |
st1(vf, vreg(rt), lane, addr); |
| 3575 |
LogVWrite(addr, rt, print_format, lane); |
| 3576 |
} |
| 3577 |
break; |
| 3578 |
case NEONLoadStoreSingle2: |
| 3579 |
scale = 2; |
| 3580 |
if (do_load) { |
| 3581 |
if (replicating) { |
| 3582 |
ld2r(vf, vreg(rt), vreg(rt2), addr); |
| 3583 |
} else { |
| 3584 |
ld2(vf, vreg(rt), vreg(rt2), lane, addr); |
| 3585 |
} |
| 3586 |
LogVRead(addr, rt, print_format, lane); |
| 3587 |
LogVRead(addr + esize, rt2, print_format, lane); |
| 3588 |
} else { |
| 3589 |
st2(vf, vreg(rt), vreg(rt2), lane, addr); |
| 3590 |
LogVWrite(addr, rt, print_format, lane); |
| 3591 |
LogVWrite(addr + esize, rt2, print_format, lane); |
| 3592 |
} |
| 3593 |
break; |
| 3594 |
case NEONLoadStoreSingle3: |
| 3595 |
scale = 3; |
| 3596 |
if (do_load) { |
| 3597 |
if (replicating) { |
| 3598 |
ld3r(vf, vreg(rt), vreg(rt2), vreg(rt3), addr); |
| 3599 |
} else { |
| 3600 |
ld3(vf, vreg(rt), vreg(rt2), vreg(rt3), lane, addr); |
| 3601 |
} |
| 3602 |
LogVRead(addr, rt, print_format, lane); |
| 3603 |
LogVRead(addr + esize, rt2, print_format, lane); |
| 3604 |
LogVRead(addr + (2 * esize), rt3, print_format, lane); |
| 3605 |
} else { |
| 3606 |
st3(vf, vreg(rt), vreg(rt2), vreg(rt3), lane, addr); |
| 3607 |
LogVWrite(addr, rt, print_format, lane); |
| 3608 |
LogVWrite(addr + esize, rt2, print_format, lane); |
| 3609 |
LogVWrite(addr + (2 * esize), rt3, print_format, lane); |
| 3610 |
} |
| 3611 |
break; |
| 3612 |
case NEONLoadStoreSingle4: |
| 3613 |
scale = 4; |
| 3614 |
if (do_load) { |
| 3615 |
if (replicating) { |
| 3616 |
ld4r(vf, vreg(rt), vreg(rt2), vreg(rt3), vreg(rt4), addr); |
| 3617 |
} else { |
| 3618 |
ld4(vf, vreg(rt), vreg(rt2), vreg(rt3), vreg(rt4), lane, addr); |
| 3619 |
} |
| 3620 |
LogVRead(addr, rt, print_format, lane); |
| 3621 |
LogVRead(addr + esize, rt2, print_format, lane); |
| 3622 |
LogVRead(addr + (2 * esize), rt3, print_format, lane); |
| 3623 |
LogVRead(addr + (3 * esize), rt4, print_format, lane); |
| 3624 |
} else { |
| 3625 |
st4(vf, vreg(rt), vreg(rt2), vreg(rt3), vreg(rt4), lane, addr); |
| 3626 |
LogVWrite(addr, rt, print_format, lane); |
| 3627 |
LogVWrite(addr + esize, rt2, print_format, lane); |
| 3628 |
LogVWrite(addr + (2 * esize), rt3, print_format, lane); |
| 3629 |
LogVWrite(addr + (3 * esize), rt4, print_format, lane); |
| 3630 |
} |
| 3631 |
break; |
| 3632 |
default: VIXL_UNIMPLEMENTED(); |
| 3633 |
} |
| 3634 |
|
| 3635 |
if (addr_mode == PostIndex) { |
| 3636 |
int rm = instr->Rm(); |
| 3637 |
int lane_size = LaneSizeInBytesFromFormat(vf); |
| 3638 |
set_xreg(instr->Rn(), addr + ((rm == 31) ? (scale * lane_size) : xreg(rm))); |
| 3639 |
} |
| 3640 |
} |
| 3641 |
|
| 3642 |
|
| 3643 |
void Simulator::VisitNEONLoadStoreSingleStruct(const Instruction* instr) { |
| 3644 |
NEONLoadStoreSingleStructHelper(instr, Offset); |
| 3645 |
} |
| 3646 |
|
| 3647 |
|
| 3648 |
void Simulator::VisitNEONLoadStoreSingleStructPostIndex( |
| 3649 |
const Instruction* instr) { |
| 3650 |
NEONLoadStoreSingleStructHelper(instr, PostIndex); |
| 3651 |
} |
| 3652 |
|
| 3653 |
|
| 3654 |
void Simulator::VisitNEONModifiedImmediate(const Instruction* instr) { |
| 3655 |
SimVRegister& rd = vreg(instr->Rd()); |
| 3656 |
int cmode = instr->NEONCmode(); |
| 3657 |
int cmode_3_1 = (cmode >> 1) & 7; |
| 3658 |
int cmode_3 = (cmode >> 3) & 1; |
| 3659 |
int cmode_2 = (cmode >> 2) & 1; |
| 3660 |
int cmode_1 = (cmode >> 1) & 1; |
| 3661 |
int cmode_0 = cmode & 1; |
| 3662 |
int q = instr->NEONQ(); |
| 3663 |
int op_bit = instr->NEONModImmOp(); |
| 3664 |
uint64_t imm8 = instr->ImmNEONabcdefgh(); |
| 3665 |
|
| 3666 |
// Find the format and immediate value |
| 3667 |
uint64_t imm = 0; |
| 3668 |
VectorFormat vform = kFormatUndefined; |
| 3669 |
switch (cmode_3_1) { |
| 3670 |
case 0x0: |
| 3671 |
case 0x1: |
| 3672 |
case 0x2: |
| 3673 |
case 0x3: |
| 3674 |
vform = (q == 1) ? kFormat4S : kFormat2S; |
| 3675 |
imm = imm8 << (8 * cmode_3_1); |
| 3676 |
break; |
| 3677 |
case 0x4: |
| 3678 |
case 0x5: |
| 3679 |
vform = (q == 1) ? kFormat8H : kFormat4H; |
| 3680 |
imm = imm8 << (8 * cmode_1); |
| 3681 |
break; |
| 3682 |
case 0x6: |
| 3683 |
vform = (q == 1) ? kFormat4S : kFormat2S; |
| 3684 |
if (cmode_0 == 0) { |
| 3685 |
imm = imm8 << 8 | 0x000000ff; |
| 3686 |
} else { |
| 3687 |
imm = imm8 << 16 | 0x0000ffff; |
| 3688 |
} |
| 3689 |
break; |
| 3690 |
case 0x7: |
| 3691 |
if (cmode_0 == 0 && op_bit == 0) { |
| 3692 |
vform = q ? kFormat16B : kFormat8B; |
| 3693 |
imm = imm8; |
| 3694 |
} else if (cmode_0 == 0 && op_bit == 1) { |
| 3695 |
vform = q ? kFormat2D : kFormat1D; |
| 3696 |
imm = 0; |
| 3697 |
for (int i = 0; i < 8; ++i) { |
| 3698 |
if (imm8 & (1ULL << i)) { |
| 3699 |
imm |= (UINT64_C(0xff) << (8 * i)); |
| 3700 |
} |
| 3701 |
} |
| 3702 |
} else { // cmode_0 == 1, cmode == 0xf. |
| 3703 |
if (op_bit == 0) { |
| 3704 |
vform = q ? kFormat4S : kFormat2S; |
| 3705 |
imm = FloatToRawbits(instr->ImmNEONFP32()); |
| 3706 |
} else if (q == 1) { |
| 3707 |
vform = kFormat2D; |
| 3708 |
imm = DoubleToRawbits(instr->ImmNEONFP64()); |
| 3709 |
} else { |
| 3710 |
VIXL_ASSERT((q == 0) && (op_bit == 1) && (cmode == 0xf)); |
| 3711 |
VisitUnallocated(instr); |
| 3712 |
} |
| 3713 |
} |
| 3714 |
break; |
| 3715 |
default: VIXL_UNREACHABLE(); break; |
| 3716 |
} |
| 3717 |
|
| 3718 |
// Find the operation |
| 3719 |
NEONModifiedImmediateOp op; |
| 3720 |
if (cmode_3 == 0) { |
| 3721 |
if (cmode_0 == 0) { |
| 3722 |
op = op_bit ? NEONModifiedImmediate_MVNI : NEONModifiedImmediate_MOVI; |
| 3723 |
} else { // cmode<0> == '1' |
| 3724 |
op = op_bit ? NEONModifiedImmediate_BIC : NEONModifiedImmediate_ORR; |
| 3725 |
} |
| 3726 |
} else { // cmode<3> == '1' |
| 3727 |
if (cmode_2 == 0) { |
| 3728 |
if (cmode_0 == 0) { |
| 3729 |
op = op_bit ? NEONModifiedImmediate_MVNI : NEONModifiedImmediate_MOVI; |
| 3730 |
} else { // cmode<0> == '1' |
| 3731 |
op = op_bit ? NEONModifiedImmediate_BIC : NEONModifiedImmediate_ORR; |
| 3732 |
} |
| 3733 |
} else { // cmode<2> == '1' |
| 3734 |
if (cmode_1 == 0) { |
| 3735 |
op = op_bit ? NEONModifiedImmediate_MVNI : NEONModifiedImmediate_MOVI; |
| 3736 |
} else { // cmode<1> == '1' |
| 3737 |
if (cmode_0 == 0) { |
| 3738 |
op = NEONModifiedImmediate_MOVI; |
| 3739 |
} else { // cmode<0> == '1' |
| 3740 |
op = NEONModifiedImmediate_MOVI; |
| 3741 |
} |
| 3742 |
} |
| 3743 |
} |
| 3744 |
} |
| 3745 |
|
| 3746 |
// Call the logic function |
| 3747 |
if (op == NEONModifiedImmediate_ORR) { |
| 3748 |
orr(vform, rd, rd, imm); |
| 3749 |
} else if (op == NEONModifiedImmediate_BIC) { |
| 3750 |
bic(vform, rd, rd, imm); |
| 3751 |
} else if (op == NEONModifiedImmediate_MOVI) { |
| 3752 |
movi(vform, rd, imm); |
| 3753 |
} else if (op == NEONModifiedImmediate_MVNI) { |
| 3754 |
mvni(vform, rd, imm); |
| 3755 |
} else { |
| 3756 |
VisitUnimplemented(instr); |
| 3757 |
} |
| 3758 |
} |
| 3759 |
|
| 3760 |
|
| 3761 |
void Simulator::VisitNEONScalar2RegMisc(const Instruction* instr) { |
| 3762 |
NEONFormatDecoder nfd(instr, NEONFormatDecoder::ScalarFormatMap()); |
| 3763 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 3764 |
|
| 3765 |
SimVRegister& rd = vreg(instr->Rd()); |
| 3766 |
SimVRegister& rn = vreg(instr->Rn()); |
| 3767 |
|
| 3768 |
if (instr->Mask(NEON2RegMiscOpcode) <= NEON_NEG_scalar_opcode) { |
| 3769 |
// These instructions all use a two bit size field, except NOT and RBIT, |
| 3770 |
// which use the field to encode the operation. |
| 3771 |
switch (instr->Mask(NEONScalar2RegMiscMask)) { |
| 3772 |
case NEON_CMEQ_zero_scalar: cmp(vf, rd, rn, 0, eq); break; |
| 3773 |
case NEON_CMGE_zero_scalar: cmp(vf, rd, rn, 0, ge); break; |
| 3774 |
case NEON_CMGT_zero_scalar: cmp(vf, rd, rn, 0, gt); break; |
| 3775 |
case NEON_CMLT_zero_scalar: cmp(vf, rd, rn, 0, lt); break; |
| 3776 |
case NEON_CMLE_zero_scalar: cmp(vf, rd, rn, 0, le); break; |
| 3777 |
case NEON_ABS_scalar: abs(vf, rd, rn); break; |
| 3778 |
case NEON_SQABS_scalar: abs(vf, rd, rn).SignedSaturate(vf); break; |
| 3779 |
case NEON_NEG_scalar: neg(vf, rd, rn); break; |
| 3780 |
case NEON_SQNEG_scalar: neg(vf, rd, rn).SignedSaturate(vf); break; |
| 3781 |
case NEON_SUQADD_scalar: suqadd(vf, rd, rn); break; |
| 3782 |
case NEON_USQADD_scalar: usqadd(vf, rd, rn); break; |
| 3783 |
default: VIXL_UNIMPLEMENTED(); break; |
| 3784 |
} |
| 3785 |
} else { |
| 3786 |
VectorFormat fpf = nfd.GetVectorFormat(nfd.FPScalarFormatMap()); |
| 3787 |
FPRounding fpcr_rounding = static_cast<FPRounding>(fpcr().RMode()); |
| 3788 |
|
| 3789 |
// These instructions all use a one bit size field, except SQXTUN, SQXTN |
| 3790 |
// and UQXTN, which use a two bit size field. |
| 3791 |
switch (instr->Mask(NEONScalar2RegMiscFPMask)) { |
| 3792 |
case NEON_FRECPE_scalar: frecpe(fpf, rd, rn, fpcr_rounding); break; |
| 3793 |
case NEON_FRECPX_scalar: frecpx(fpf, rd, rn); break; |
| 3794 |
case NEON_FRSQRTE_scalar: frsqrte(fpf, rd, rn); break; |
| 3795 |
case NEON_FCMGT_zero_scalar: fcmp_zero(fpf, rd, rn, gt); break; |
| 3796 |
case NEON_FCMGE_zero_scalar: fcmp_zero(fpf, rd, rn, ge); break; |
| 3797 |
case NEON_FCMEQ_zero_scalar: fcmp_zero(fpf, rd, rn, eq); break; |
| 3798 |
case NEON_FCMLE_zero_scalar: fcmp_zero(fpf, rd, rn, le); break; |
| 3799 |
case NEON_FCMLT_zero_scalar: fcmp_zero(fpf, rd, rn, lt); break; |
| 3800 |
case NEON_SCVTF_scalar: scvtf(fpf, rd, rn, 0, fpcr_rounding); break; |
| 3801 |
case NEON_UCVTF_scalar: ucvtf(fpf, rd, rn, 0, fpcr_rounding); break; |
| 3802 |
case NEON_FCVTNS_scalar: fcvts(fpf, rd, rn, FPTieEven); break; |
| 3803 |
case NEON_FCVTNU_scalar: fcvtu(fpf, rd, rn, FPTieEven); break; |
| 3804 |
case NEON_FCVTPS_scalar: fcvts(fpf, rd, rn, FPPositiveInfinity); break; |
| 3805 |
case NEON_FCVTPU_scalar: fcvtu(fpf, rd, rn, FPPositiveInfinity); break; |
| 3806 |
case NEON_FCVTMS_scalar: fcvts(fpf, rd, rn, FPNegativeInfinity); break; |
| 3807 |
case NEON_FCVTMU_scalar: fcvtu(fpf, rd, rn, FPNegativeInfinity); break; |
| 3808 |
case NEON_FCVTZS_scalar: fcvts(fpf, rd, rn, FPZero); break; |
| 3809 |
case NEON_FCVTZU_scalar: fcvtu(fpf, rd, rn, FPZero); break; |
| 3810 |
case NEON_FCVTAS_scalar: fcvts(fpf, rd, rn, FPTieAway); break; |
| 3811 |
case NEON_FCVTAU_scalar: fcvtu(fpf, rd, rn, FPTieAway); break; |
| 3812 |
case NEON_FCVTXN_scalar: |
| 3813 |
// Unlike all of the other FP instructions above, fcvtxn encodes dest |
| 3814 |
// size S as size<0>=1. There's only one case, so we ignore the form. |
| 3815 |
VIXL_ASSERT(instr->Bit(22) == 1); |
| 3816 |
fcvtxn(kFormatS, rd, rn); |
| 3817 |
break; |
| 3818 |
default: |
| 3819 |
switch (instr->Mask(NEONScalar2RegMiscMask)) { |
| 3820 |
case NEON_SQXTN_scalar: sqxtn(vf, rd, rn); break; |
| 3821 |
case NEON_UQXTN_scalar: uqxtn(vf, rd, rn); break; |
| 3822 |
case NEON_SQXTUN_scalar: sqxtun(vf, rd, rn); break; |
| 3823 |
default: |
| 3824 |
VIXL_UNIMPLEMENTED(); |
| 3825 |
} |
| 3826 |
} |
| 3827 |
} |
| 3828 |
} |
| 3829 |
|
| 3830 |
|
| 3831 |
void Simulator::VisitNEONScalar3Diff(const Instruction* instr) { |
| 3832 |
NEONFormatDecoder nfd(instr, NEONFormatDecoder::LongScalarFormatMap()); |
| 3833 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 3834 |
|
| 3835 |
SimVRegister& rd = vreg(instr->Rd()); |
| 3836 |
SimVRegister& rn = vreg(instr->Rn()); |
| 3837 |
SimVRegister& rm = vreg(instr->Rm()); |
| 3838 |
switch (instr->Mask(NEONScalar3DiffMask)) { |
| 3839 |
case NEON_SQDMLAL_scalar: sqdmlal(vf, rd, rn, rm); break; |
| 3840 |
case NEON_SQDMLSL_scalar: sqdmlsl(vf, rd, rn, rm); break; |
| 3841 |
case NEON_SQDMULL_scalar: sqdmull(vf, rd, rn, rm); break; |
| 3842 |
default: |
| 3843 |
VIXL_UNIMPLEMENTED(); |
| 3844 |
} |
| 3845 |
} |
| 3846 |
|
| 3847 |
|
| 3848 |
void Simulator::VisitNEONScalar3Same(const Instruction* instr) { |
| 3849 |
NEONFormatDecoder nfd(instr, NEONFormatDecoder::ScalarFormatMap()); |
| 3850 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 3851 |
|
| 3852 |
SimVRegister& rd = vreg(instr->Rd()); |
| 3853 |
SimVRegister& rn = vreg(instr->Rn()); |
| 3854 |
SimVRegister& rm = vreg(instr->Rm()); |
| 3855 |
|
| 3856 |
if (instr->Mask(NEONScalar3SameFPFMask) == NEONScalar3SameFPFixed) { |
| 3857 |
vf = nfd.GetVectorFormat(nfd.FPScalarFormatMap()); |
| 3858 |
switch (instr->Mask(NEONScalar3SameFPMask)) { |
| 3859 |
case NEON_FMULX_scalar: fmulx(vf, rd, rn, rm); break; |
| 3860 |
case NEON_FACGE_scalar: fabscmp(vf, rd, rn, rm, ge); break; |
| 3861 |
case NEON_FACGT_scalar: fabscmp(vf, rd, rn, rm, gt); break; |
| 3862 |
case NEON_FCMEQ_scalar: fcmp(vf, rd, rn, rm, eq); break; |
| 3863 |
case NEON_FCMGE_scalar: fcmp(vf, rd, rn, rm, ge); break; |
| 3864 |
case NEON_FCMGT_scalar: fcmp(vf, rd, rn, rm, gt); break; |
| 3865 |
case NEON_FRECPS_scalar: frecps(vf, rd, rn, rm); break; |
| 3866 |
case NEON_FRSQRTS_scalar: frsqrts(vf, rd, rn, rm); break; |
| 3867 |
case NEON_FABD_scalar: fabd(vf, rd, rn, rm); break; |
| 3868 |
default: |
| 3869 |
VIXL_UNIMPLEMENTED(); |
| 3870 |
} |
| 3871 |
} else { |
| 3872 |
switch (instr->Mask(NEONScalar3SameMask)) { |
| 3873 |
case NEON_ADD_scalar: add(vf, rd, rn, rm); break; |
| 3874 |
case NEON_SUB_scalar: sub(vf, rd, rn, rm); break; |
| 3875 |
case NEON_CMEQ_scalar: cmp(vf, rd, rn, rm, eq); break; |
| 3876 |
case NEON_CMGE_scalar: cmp(vf, rd, rn, rm, ge); break; |
| 3877 |
case NEON_CMGT_scalar: cmp(vf, rd, rn, rm, gt); break; |
| 3878 |
case NEON_CMHI_scalar: cmp(vf, rd, rn, rm, hi); break; |
| 3879 |
case NEON_CMHS_scalar: cmp(vf, rd, rn, rm, hs); break; |
| 3880 |
case NEON_CMTST_scalar: cmptst(vf, rd, rn, rm); break; |
| 3881 |
case NEON_USHL_scalar: ushl(vf, rd, rn, rm); break; |
| 3882 |
case NEON_SSHL_scalar: sshl(vf, rd, rn, rm); break; |
| 3883 |
case NEON_SQDMULH_scalar: sqdmulh(vf, rd, rn, rm); break; |
| 3884 |
case NEON_SQRDMULH_scalar: sqrdmulh(vf, rd, rn, rm); break; |
| 3885 |
case NEON_UQADD_scalar: |
| 3886 |
add(vf, rd, rn, rm).UnsignedSaturate(vf); |
| 3887 |
break; |
| 3888 |
case NEON_SQADD_scalar: |
| 3889 |
add(vf, rd, rn, rm).SignedSaturate(vf); |
| 3890 |
break; |
| 3891 |
case NEON_UQSUB_scalar: |
| 3892 |
sub(vf, rd, rn, rm).UnsignedSaturate(vf); |
| 3893 |
break; |
| 3894 |
case NEON_SQSUB_scalar: |
| 3895 |
sub(vf, rd, rn, rm).SignedSaturate(vf); |
| 3896 |
break; |
| 3897 |
case NEON_UQSHL_scalar: |
| 3898 |
ushl(vf, rd, rn, rm).UnsignedSaturate(vf); |
| 3899 |
break; |
| 3900 |
case NEON_SQSHL_scalar: |
| 3901 |
sshl(vf, rd, rn, rm).SignedSaturate(vf); |
| 3902 |
break; |
| 3903 |
case NEON_URSHL_scalar: |
| 3904 |
ushl(vf, rd, rn, rm).Round(vf); |
| 3905 |
break; |
| 3906 |
case NEON_SRSHL_scalar: |
| 3907 |
sshl(vf, rd, rn, rm).Round(vf); |
| 3908 |
break; |
| 3909 |
case NEON_UQRSHL_scalar: |
| 3910 |
ushl(vf, rd, rn, rm).Round(vf).UnsignedSaturate(vf); |
| 3911 |
break; |
| 3912 |
case NEON_SQRSHL_scalar: |
| 3913 |
sshl(vf, rd, rn, rm).Round(vf).SignedSaturate(vf); |
| 3914 |
break; |
| 3915 |
default: |
| 3916 |
VIXL_UNIMPLEMENTED(); |
| 3917 |
} |
| 3918 |
} |
| 3919 |
} |
| 3920 |
|
| 3921 |
|
| 3922 |
void Simulator::VisitNEONScalarByIndexedElement(const Instruction* instr) { |
| 3923 |
NEONFormatDecoder nfd(instr, NEONFormatDecoder::LongScalarFormatMap()); |
| 3924 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 3925 |
VectorFormat vf_r = nfd.GetVectorFormat(nfd.ScalarFormatMap()); |
| 3926 |
|
| 3927 |
SimVRegister& rd = vreg(instr->Rd()); |
| 3928 |
SimVRegister& rn = vreg(instr->Rn()); |
| 3929 |
ByElementOp Op = NULL; |
| 3930 |
|
| 3931 |
int rm_reg = instr->Rm(); |
| 3932 |
int index = (instr->NEONH() << 1) | instr->NEONL(); |
| 3933 |
if (instr->NEONSize() == 1) { |
| 3934 |
rm_reg &= 0xf; |
| 3935 |
index = (index << 1) | instr->NEONM(); |
| 3936 |
} |
| 3937 |
|
| 3938 |
switch (instr->Mask(NEONScalarByIndexedElementMask)) { |
| 3939 |
case NEON_SQDMULL_byelement_scalar: Op = &Simulator::sqdmull; break; |
| 3940 |
case NEON_SQDMLAL_byelement_scalar: Op = &Simulator::sqdmlal; break; |
| 3941 |
case NEON_SQDMLSL_byelement_scalar: Op = &Simulator::sqdmlsl; break; |
| 3942 |
case NEON_SQDMULH_byelement_scalar: |
| 3943 |
Op = &Simulator::sqdmulh; |
| 3944 |
vf = vf_r; |
| 3945 |
break; |
| 3946 |
case NEON_SQRDMULH_byelement_scalar: |
| 3947 |
Op = &Simulator::sqrdmulh; |
| 3948 |
vf = vf_r; |
| 3949 |
break; |
| 3950 |
default: |
| 3951 |
vf = nfd.GetVectorFormat(nfd.FPScalarFormatMap()); |
| 3952 |
index = instr->NEONH(); |
| 3953 |
if ((instr->FPType() & 1) == 0) { |
| 3954 |
index = (index << 1) | instr->NEONL(); |
| 3955 |
} |
| 3956 |
switch (instr->Mask(NEONScalarByIndexedElementFPMask)) { |
| 3957 |
case NEON_FMUL_byelement_scalar: Op = &Simulator::fmul; break; |
| 3958 |
case NEON_FMLA_byelement_scalar: Op = &Simulator::fmla; break; |
| 3959 |
case NEON_FMLS_byelement_scalar: Op = &Simulator::fmls; break; |
| 3960 |
case NEON_FMULX_byelement_scalar: Op = &Simulator::fmulx; break; |
| 3961 |
default: VIXL_UNIMPLEMENTED(); |
| 3962 |
} |
| 3963 |
} |
| 3964 |
|
| 3965 |
(this->*Op)(vf, rd, rn, vreg(rm_reg), index); |
| 3966 |
} |
| 3967 |
|
| 3968 |
|
| 3969 |
void Simulator::VisitNEONScalarCopy(const Instruction* instr) { |
| 3970 |
NEONFormatDecoder nfd(instr, NEONFormatDecoder::TriangularScalarFormatMap()); |
| 3971 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 3972 |
|
| 3973 |
SimVRegister& rd = vreg(instr->Rd()); |
| 3974 |
SimVRegister& rn = vreg(instr->Rn()); |
| 3975 |
|
| 3976 |
if (instr->Mask(NEONScalarCopyMask) == NEON_DUP_ELEMENT_scalar) { |
| 3977 |
int imm5 = instr->ImmNEON5(); |
| 3978 |
int tz = CountTrailingZeros(imm5, 32); |
| 3979 |
int rn_index = imm5 >> (tz + 1); |
| 3980 |
dup_element(vf, rd, rn, rn_index); |
| 3981 |
} else { |
| 3982 |
VIXL_UNIMPLEMENTED(); |
| 3983 |
} |
| 3984 |
} |
| 3985 |
|
| 3986 |
|
| 3987 |
void Simulator::VisitNEONScalarPairwise(const Instruction* instr) { |
| 3988 |
NEONFormatDecoder nfd(instr, NEONFormatDecoder::FPScalarFormatMap()); |
| 3989 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 3990 |
|
| 3991 |
SimVRegister& rd = vreg(instr->Rd()); |
| 3992 |
SimVRegister& rn = vreg(instr->Rn()); |
| 3993 |
switch (instr->Mask(NEONScalarPairwiseMask)) { |
| 3994 |
case NEON_ADDP_scalar: addp(vf, rd, rn); break; |
| 3995 |
case NEON_FADDP_scalar: faddp(vf, rd, rn); break; |
| 3996 |
case NEON_FMAXP_scalar: fmaxp(vf, rd, rn); break; |
| 3997 |
case NEON_FMAXNMP_scalar: fmaxnmp(vf, rd, rn); break; |
| 3998 |
case NEON_FMINP_scalar: fminp(vf, rd, rn); break; |
| 3999 |
case NEON_FMINNMP_scalar: fminnmp(vf, rd, rn); break; |
| 4000 |
default: |
| 4001 |
VIXL_UNIMPLEMENTED(); |
| 4002 |
} |
| 4003 |
} |
| 4004 |
|
| 4005 |
|
| 4006 |
void Simulator::VisitNEONScalarShiftImmediate(const Instruction* instr) { |
| 4007 |
SimVRegister& rd = vreg(instr->Rd()); |
| 4008 |
SimVRegister& rn = vreg(instr->Rn()); |
| 4009 |
FPRounding fpcr_rounding = static_cast<FPRounding>(fpcr().RMode()); |
| 4010 |
|
| 4011 |
static const NEONFormatMap map = { |
| 4012 |
{22, 21, 20, 19}, |
| 4013 |
{NF_UNDEF, NF_B, NF_H, NF_H, NF_S, NF_S, NF_S, NF_S, |
| 4014 |
NF_D, NF_D, NF_D, NF_D, NF_D, NF_D, NF_D, NF_D} |
| 4015 |
}; |
| 4016 |
NEONFormatDecoder nfd(instr, &map); |
| 4017 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 4018 |
|
| 4019 |
int highestSetBit = HighestSetBitPosition(instr->ImmNEONImmh()); |
| 4020 |
int immhimmb = instr->ImmNEONImmhImmb(); |
| 4021 |
int right_shift = (16 << highestSetBit) - immhimmb; |
| 4022 |
int left_shift = immhimmb - (8 << highestSetBit); |
| 4023 |
switch (instr->Mask(NEONScalarShiftImmediateMask)) { |
| 4024 |
case NEON_SHL_scalar: shl(vf, rd, rn, left_shift); break; |
| 4025 |
case NEON_SLI_scalar: sli(vf, rd, rn, left_shift); break; |
| 4026 |
case NEON_SQSHL_imm_scalar: sqshl(vf, rd, rn, left_shift); break; |
| 4027 |
case NEON_UQSHL_imm_scalar: uqshl(vf, rd, rn, left_shift); break; |
| 4028 |
case NEON_SQSHLU_scalar: sqshlu(vf, rd, rn, left_shift); break; |
| 4029 |
case NEON_SRI_scalar: sri(vf, rd, rn, right_shift); break; |
| 4030 |
case NEON_SSHR_scalar: sshr(vf, rd, rn, right_shift); break; |
| 4031 |
case NEON_USHR_scalar: ushr(vf, rd, rn, right_shift); break; |
| 4032 |
case NEON_SRSHR_scalar: sshr(vf, rd, rn, right_shift).Round(vf); break; |
| 4033 |
case NEON_URSHR_scalar: ushr(vf, rd, rn, right_shift).Round(vf); break; |
| 4034 |
case NEON_SSRA_scalar: ssra(vf, rd, rn, right_shift); break; |
| 4035 |
case NEON_USRA_scalar: usra(vf, rd, rn, right_shift); break; |
| 4036 |
case NEON_SRSRA_scalar: srsra(vf, rd, rn, right_shift); break; |
| 4037 |
case NEON_URSRA_scalar: ursra(vf, rd, rn, right_shift); break; |
| 4038 |
case NEON_UQSHRN_scalar: uqshrn(vf, rd, rn, right_shift); break; |
| 4039 |
case NEON_UQRSHRN_scalar: uqrshrn(vf, rd, rn, right_shift); break; |
| 4040 |
case NEON_SQSHRN_scalar: sqshrn(vf, rd, rn, right_shift); break; |
| 4041 |
case NEON_SQRSHRN_scalar: sqrshrn(vf, rd, rn, right_shift); break; |
| 4042 |
case NEON_SQSHRUN_scalar: sqshrun(vf, rd, rn, right_shift); break; |
| 4043 |
case NEON_SQRSHRUN_scalar: sqrshrun(vf, rd, rn, right_shift); break; |
| 4044 |
case NEON_FCVTZS_imm_scalar: fcvts(vf, rd, rn, FPZero, right_shift); break; |
| 4045 |
case NEON_FCVTZU_imm_scalar: fcvtu(vf, rd, rn, FPZero, right_shift); break; |
| 4046 |
case NEON_SCVTF_imm_scalar: |
| 4047 |
scvtf(vf, rd, rn, right_shift, fpcr_rounding); |
| 4048 |
break; |
| 4049 |
case NEON_UCVTF_imm_scalar: |
| 4050 |
ucvtf(vf, rd, rn, right_shift, fpcr_rounding); |
| 4051 |
break; |
| 4052 |
default: |
| 4053 |
VIXL_UNIMPLEMENTED(); |
| 4054 |
} |
| 4055 |
} |
| 4056 |
|
| 4057 |
|
| 4058 |
void Simulator::VisitNEONShiftImmediate(const Instruction* instr) { |
| 4059 |
SimVRegister& rd = vreg(instr->Rd()); |
| 4060 |
SimVRegister& rn = vreg(instr->Rn()); |
| 4061 |
FPRounding fpcr_rounding = static_cast<FPRounding>(fpcr().RMode()); |
| 4062 |
|
| 4063 |
// 00010->8B, 00011->16B, 001x0->4H, 001x1->8H, |
| 4064 |
// 01xx0->2S, 01xx1->4S, 1xxx1->2D, all others undefined. |
| 4065 |
static const NEONFormatMap map = { |
| 4066 |
{22, 21, 20, 19, 30}, |
| 4067 |
{NF_UNDEF, NF_UNDEF, NF_8B, NF_16B, NF_4H, NF_8H, NF_4H, NF_8H, |
| 4068 |
NF_2S, NF_4S, NF_2S, NF_4S, NF_2S, NF_4S, NF_2S, NF_4S, |
| 4069 |
NF_UNDEF, NF_2D, NF_UNDEF, NF_2D, NF_UNDEF, NF_2D, NF_UNDEF, NF_2D, |
| 4070 |
NF_UNDEF, NF_2D, NF_UNDEF, NF_2D, NF_UNDEF, NF_2D, NF_UNDEF, NF_2D} |
| 4071 |
}; |
| 4072 |
NEONFormatDecoder nfd(instr, &map); |
| 4073 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 4074 |
|
| 4075 |
// 0001->8H, 001x->4S, 01xx->2D, all others undefined. |
| 4076 |
static const NEONFormatMap map_l = { |
| 4077 |
{22, 21, 20, 19}, |
| 4078 |
{NF_UNDEF, NF_8H, NF_4S, NF_4S, NF_2D, NF_2D, NF_2D, NF_2D} |
| 4079 |
}; |
| 4080 |
VectorFormat vf_l = nfd.GetVectorFormat(&map_l); |
| 4081 |
|
| 4082 |
int highestSetBit = HighestSetBitPosition(instr->ImmNEONImmh()); |
| 4083 |
int immhimmb = instr->ImmNEONImmhImmb(); |
| 4084 |
int right_shift = (16 << highestSetBit) - immhimmb; |
| 4085 |
int left_shift = immhimmb - (8 << highestSetBit); |
| 4086 |
|
| 4087 |
switch (instr->Mask(NEONShiftImmediateMask)) { |
| 4088 |
case NEON_SHL: shl(vf, rd, rn, left_shift); break; |
| 4089 |
case NEON_SLI: sli(vf, rd, rn, left_shift); break; |
| 4090 |
case NEON_SQSHLU: sqshlu(vf, rd, rn, left_shift); break; |
| 4091 |
case NEON_SRI: sri(vf, rd, rn, right_shift); break; |
| 4092 |
case NEON_SSHR: sshr(vf, rd, rn, right_shift); break; |
| 4093 |
case NEON_USHR: ushr(vf, rd, rn, right_shift); break; |
| 4094 |
case NEON_SRSHR: sshr(vf, rd, rn, right_shift).Round(vf); break; |
| 4095 |
case NEON_URSHR: ushr(vf, rd, rn, right_shift).Round(vf); break; |
| 4096 |
case NEON_SSRA: ssra(vf, rd, rn, right_shift); break; |
| 4097 |
case NEON_USRA: usra(vf, rd, rn, right_shift); break; |
| 4098 |
case NEON_SRSRA: srsra(vf, rd, rn, right_shift); break; |
| 4099 |
case NEON_URSRA: ursra(vf, rd, rn, right_shift); break; |
| 4100 |
case NEON_SQSHL_imm: sqshl(vf, rd, rn, left_shift); break; |
| 4101 |
case NEON_UQSHL_imm: uqshl(vf, rd, rn, left_shift); break; |
| 4102 |
case NEON_SCVTF_imm: scvtf(vf, rd, rn, right_shift, fpcr_rounding); break; |
| 4103 |
case NEON_UCVTF_imm: ucvtf(vf, rd, rn, right_shift, fpcr_rounding); break; |
| 4104 |
case NEON_FCVTZS_imm: fcvts(vf, rd, rn, FPZero, right_shift); break; |
| 4105 |
case NEON_FCVTZU_imm: fcvtu(vf, rd, rn, FPZero, right_shift); break; |
| 4106 |
case NEON_SSHLL: |
| 4107 |
vf = vf_l; |
| 4108 |
if (instr->Mask(NEON_Q)) { |
| 4109 |
sshll2(vf, rd, rn, left_shift); |
| 4110 |
} else { |
| 4111 |
sshll(vf, rd, rn, left_shift); |
| 4112 |
} |
| 4113 |
break; |
| 4114 |
case NEON_USHLL: |
| 4115 |
vf = vf_l; |
| 4116 |
if (instr->Mask(NEON_Q)) { |
| 4117 |
ushll2(vf, rd, rn, left_shift); |
| 4118 |
} else { |
| 4119 |
ushll(vf, rd, rn, left_shift); |
| 4120 |
} |
| 4121 |
break; |
| 4122 |
case NEON_SHRN: |
| 4123 |
if (instr->Mask(NEON_Q)) { |
| 4124 |
shrn2(vf, rd, rn, right_shift); |
| 4125 |
} else { |
| 4126 |
shrn(vf, rd, rn, right_shift); |
| 4127 |
} |
| 4128 |
break; |
| 4129 |
case NEON_RSHRN: |
| 4130 |
if (instr->Mask(NEON_Q)) { |
| 4131 |
rshrn2(vf, rd, rn, right_shift); |
| 4132 |
} else { |
| 4133 |
rshrn(vf, rd, rn, right_shift); |
| 4134 |
} |
| 4135 |
break; |
| 4136 |
case NEON_UQSHRN: |
| 4137 |
if (instr->Mask(NEON_Q)) { |
| 4138 |
uqshrn2(vf, rd, rn, right_shift); |
| 4139 |
} else { |
| 4140 |
uqshrn(vf, rd, rn, right_shift); |
| 4141 |
} |
| 4142 |
break; |
| 4143 |
case NEON_UQRSHRN: |
| 4144 |
if (instr->Mask(NEON_Q)) { |
| 4145 |
uqrshrn2(vf, rd, rn, right_shift); |
| 4146 |
} else { |
| 4147 |
uqrshrn(vf, rd, rn, right_shift); |
| 4148 |
} |
| 4149 |
break; |
| 4150 |
case NEON_SQSHRN: |
| 4151 |
if (instr->Mask(NEON_Q)) { |
| 4152 |
sqshrn2(vf, rd, rn, right_shift); |
| 4153 |
} else { |
| 4154 |
sqshrn(vf, rd, rn, right_shift); |
| 4155 |
} |
| 4156 |
break; |
| 4157 |
case NEON_SQRSHRN: |
| 4158 |
if (instr->Mask(NEON_Q)) { |
| 4159 |
sqrshrn2(vf, rd, rn, right_shift); |
| 4160 |
} else { |
| 4161 |
sqrshrn(vf, rd, rn, right_shift); |
| 4162 |
} |
| 4163 |
break; |
| 4164 |
case NEON_SQSHRUN: |
| 4165 |
if (instr->Mask(NEON_Q)) { |
| 4166 |
sqshrun2(vf, rd, rn, right_shift); |
| 4167 |
} else { |
| 4168 |
sqshrun(vf, rd, rn, right_shift); |
| 4169 |
} |
| 4170 |
break; |
| 4171 |
case NEON_SQRSHRUN: |
| 4172 |
if (instr->Mask(NEON_Q)) { |
| 4173 |
sqrshrun2(vf, rd, rn, right_shift); |
| 4174 |
} else { |
| 4175 |
sqrshrun(vf, rd, rn, right_shift); |
| 4176 |
} |
| 4177 |
break; |
| 4178 |
default: |
| 4179 |
VIXL_UNIMPLEMENTED(); |
| 4180 |
} |
| 4181 |
} |
| 4182 |
|
| 4183 |
|
| 4184 |
void Simulator::VisitNEONTable(const Instruction* instr) { |
| 4185 |
NEONFormatDecoder nfd(instr, NEONFormatDecoder::LogicalFormatMap()); |
| 4186 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 4187 |
|
| 4188 |
SimVRegister& rd = vreg(instr->Rd()); |
| 4189 |
SimVRegister& rn = vreg(instr->Rn()); |
| 4190 |
SimVRegister& rn2 = vreg((instr->Rn() + 1) % kNumberOfVRegisters); |
| 4191 |
SimVRegister& rn3 = vreg((instr->Rn() + 2) % kNumberOfVRegisters); |
| 4192 |
SimVRegister& rn4 = vreg((instr->Rn() + 3) % kNumberOfVRegisters); |
| 4193 |
SimVRegister& rm = vreg(instr->Rm()); |
| 4194 |
|
| 4195 |
switch (instr->Mask(NEONTableMask)) { |
| 4196 |
case NEON_TBL_1v: tbl(vf, rd, rn, rm); break; |
| 4197 |
case NEON_TBL_2v: tbl(vf, rd, rn, rn2, rm); break; |
| 4198 |
case NEON_TBL_3v: tbl(vf, rd, rn, rn2, rn3, rm); break; |
| 4199 |
case NEON_TBL_4v: tbl(vf, rd, rn, rn2, rn3, rn4, rm); break; |
| 4200 |
case NEON_TBX_1v: tbx(vf, rd, rn, rm); break; |
| 4201 |
case NEON_TBX_2v: tbx(vf, rd, rn, rn2, rm); break; |
| 4202 |
case NEON_TBX_3v: tbx(vf, rd, rn, rn2, rn3, rm); break; |
| 4203 |
case NEON_TBX_4v: tbx(vf, rd, rn, rn2, rn3, rn4, rm); break; |
| 4204 |
default: |
| 4205 |
VIXL_UNIMPLEMENTED(); |
| 4206 |
} |
| 4207 |
} |
| 4208 |
|
| 4209 |
|
| 4210 |
void Simulator::VisitNEONPerm(const Instruction* instr) { |
| 4211 |
NEONFormatDecoder nfd(instr); |
| 4212 |
VectorFormat vf = nfd.GetVectorFormat(); |
| 4213 |
|
| 4214 |
SimVRegister& rd = vreg(instr->Rd()); |
| 4215 |
SimVRegister& rn = vreg(instr->Rn()); |
| 4216 |
SimVRegister& rm = vreg(instr->Rm()); |
| 4217 |
|
| 4218 |
switch (instr->Mask(NEONPermMask)) { |
| 4219 |
case NEON_TRN1: trn1(vf, rd, rn, rm); break; |
| 4220 |
case NEON_TRN2: trn2(vf, rd, rn, rm); break; |
| 4221 |
case NEON_UZP1: uzp1(vf, rd, rn, rm); break; |
| 4222 |
case NEON_UZP2: uzp2(vf, rd, rn, rm); break; |
| 4223 |
case NEON_ZIP1: zip1(vf, rd, rn, rm); break; |
| 4224 |
case NEON_ZIP2: zip2(vf, rd, rn, rm); break; |
| 4225 |
default: |
| 4226 |
VIXL_UNIMPLEMENTED(); |
| 4227 |
} |
| 4228 |
} |
| 4229 |
|
| 4230 |
|
| 4231 |
void Simulator::DoUnreachable(const Instruction* instr) { |
| 4232 |
VIXL_ASSERT(instr->InstructionBits() == UNDEFINED_INST_PATTERN); |
| 4233 |
|
| 4234 |
fprintf(stream_, "Hit UNREACHABLE marker at pc=%p.\n", |
| 4235 |
reinterpret_cast<const void*>(instr)); |
| 4236 |
abort(); |
| 4237 |
} |
| 4238 |
|
| 4239 |
|
| 4240 |
void Simulator::DoTrace(const Instruction* instr) { |
| 4241 |
VIXL_ASSERT((instr->Mask(ExceptionMask) == HLT) && |
| 4242 |
(instr->ImmException() == kTraceOpcode)); |
| 4243 |
|
| 4244 |
// Read the arguments encoded inline in the instruction stream. |
| 4245 |
uint32_t parameters; |
| 4246 |
uint32_t command; |
| 4247 |
|
| 4248 |
VIXL_STATIC_ASSERT(sizeof(*instr) == 1); |
| 4249 |
memcpy(¶meters, instr + kTraceParamsOffset, sizeof(parameters)); |
| 4250 |
memcpy(&command, instr + kTraceCommandOffset, sizeof(command)); |
| 4251 |
|
| 4252 |
switch (command) { |
| 4253 |
case TRACE_ENABLE: |
| 4254 |
set_trace_parameters(trace_parameters() | parameters); |
| 4255 |
break; |
| 4256 |
case TRACE_DISABLE: |
| 4257 |
set_trace_parameters(trace_parameters() & ~parameters); |
| 4258 |
break; |
| 4259 |
default: |
| 4260 |
VIXL_UNREACHABLE(); |
| 4261 |
} |
| 4262 |
|
| 4263 |
set_pc(instr->InstructionAtOffset(kTraceLength)); |
| 4264 |
} |
| 4265 |
|
| 4266 |
|
| 4267 |
void Simulator::DoLog(const Instruction* instr) { |
| 4268 |
VIXL_ASSERT((instr->Mask(ExceptionMask) == HLT) && |
| 4269 |
(instr->ImmException() == kLogOpcode)); |
| 4270 |
|
| 4271 |
// Read the arguments encoded inline in the instruction stream. |
| 4272 |
uint32_t parameters; |
| 4273 |
|
| 4274 |
VIXL_STATIC_ASSERT(sizeof(*instr) == 1); |
| 4275 |
memcpy(¶meters, instr + kTraceParamsOffset, sizeof(parameters)); |
| 4276 |
|
| 4277 |
// We don't support a one-shot LOG_DISASM. |
| 4278 |
VIXL_ASSERT((parameters & LOG_DISASM) == 0); |
| 4279 |
// Print the requested information. |
| 4280 |
if (parameters & LOG_SYSREGS) PrintSystemRegisters(); |
| 4281 |
if (parameters & LOG_REGS) PrintRegisters(); |
| 4282 |
if (parameters & LOG_VREGS) PrintVRegisters(); |
| 4283 |
|
| 4284 |
set_pc(instr->InstructionAtOffset(kLogLength)); |
| 4285 |
} |
| 4286 |
|
| 4287 |
|
| 4288 |
void Simulator::DoPrintf(const Instruction* instr) { |
| 4289 |
VIXL_ASSERT((instr->Mask(ExceptionMask) == HLT) && |
| 4290 |
(instr->ImmException() == kPrintfOpcode)); |
| 4291 |
|
| 4292 |
// Read the arguments encoded inline in the instruction stream. |
| 4293 |
uint32_t arg_count; |
| 4294 |
uint32_t arg_pattern_list; |
| 4295 |
VIXL_STATIC_ASSERT(sizeof(*instr) == 1); |
| 4296 |
memcpy(&arg_count, |
| 4297 |
instr + kPrintfArgCountOffset, |
| 4298 |
sizeof(arg_count)); |
| 4299 |
memcpy(&arg_pattern_list, |
| 4300 |
instr + kPrintfArgPatternListOffset, |
| 4301 |
sizeof(arg_pattern_list)); |
| 4302 |
|
| 4303 |
VIXL_ASSERT(arg_count <= kPrintfMaxArgCount); |
| 4304 |
VIXL_ASSERT((arg_pattern_list >> (kPrintfArgPatternBits * arg_count)) == 0); |
| 4305 |
|
| 4306 |
// We need to call the host printf function with a set of arguments defined by |
| 4307 |
// arg_pattern_list. Because we don't know the types and sizes of the |
| 4308 |
// arguments, this is very difficult to do in a robust and portable way. To |
| 4309 |
// work around the problem, we pick apart the format string, and print one |
| 4310 |
// format placeholder at a time. |
| 4311 |
|
| 4312 |
// Allocate space for the format string. We take a copy, so we can modify it. |
| 4313 |
// Leave enough space for one extra character per expected argument (plus the |
| 4314 |
// '\0' termination). |
| 4315 |
const char * format_base = reg<const char *>(0); |
| 4316 |
VIXL_ASSERT(format_base != NULL); |
| 4317 |
size_t length = strlen(format_base) + 1; |
| 4318 |
char * const format = (char *)js_calloc(length + arg_count); |
| 4319 |
|
| 4320 |
// A list of chunks, each with exactly one format placeholder. |
| 4321 |
const char * chunks[kPrintfMaxArgCount]; |
| 4322 |
|
| 4323 |
// Copy the format string and search for format placeholders. |
| 4324 |
uint32_t placeholder_count = 0; |
| 4325 |
char * format_scratch = format; |
| 4326 |
for (size_t i = 0; i < length; i++) { |
| 4327 |
if (format_base[i] != '%') { |
| 4328 |
*format_scratch++ = format_base[i]; |
| 4329 |
} else { |
| 4330 |
if (format_base[i + 1] == '%') { |
| 4331 |
// Ignore explicit "%%" sequences. |
| 4332 |
*format_scratch++ = format_base[i]; |
| 4333 |
i++; |
| 4334 |
// Chunks after the first are passed as format strings to printf, so we |
| 4335 |
// need to escape '%' characters in those chunks. |
| 4336 |
if (placeholder_count > 0) *format_scratch++ = format_base[i]; |
| 4337 |
} else { |
| 4338 |
VIXL_CHECK(placeholder_count < arg_count); |
| 4339 |
// Insert '\0' before placeholders, and store their locations. |
| 4340 |
*format_scratch++ = '\0'; |
| 4341 |
chunks[placeholder_count++] = format_scratch; |
| 4342 |
*format_scratch++ = format_base[i]; |
| 4343 |
} |
| 4344 |
} |
| 4345 |
} |
| 4346 |
VIXL_CHECK(placeholder_count == arg_count); |
| 4347 |
|
| 4348 |
// Finally, call printf with each chunk, passing the appropriate register |
| 4349 |
// argument. Normally, printf returns the number of bytes transmitted, so we |
| 4350 |
// can emulate a single printf call by adding the result from each chunk. If |
| 4351 |
// any call returns a negative (error) value, though, just return that value. |
| 4352 |
|
| 4353 |
printf("%s", clr_printf); |
| 4354 |
|
| 4355 |
// Because '\0' is inserted before each placeholder, the first string in |
| 4356 |
// 'format' contains no format placeholders and should be printed literally. |
| 4357 |
int result = printf("%s", format); |
| 4358 |
int pcs_r = 1; // Start at x1. x0 holds the format string. |
| 4359 |
int pcs_f = 0; // Start at d0. |
| 4360 |
if (result >= 0) { |
| 4361 |
for (uint32_t i = 0; i < placeholder_count; i++) { |
| 4362 |
int part_result = -1; |
| 4363 |
|
| 4364 |
uint32_t arg_pattern = arg_pattern_list >> (i * kPrintfArgPatternBits); |
| 4365 |
arg_pattern &= (1 << kPrintfArgPatternBits) - 1; |
| 4366 |
switch (arg_pattern) { |
| 4367 |
case kPrintfArgW: part_result = printf(chunks[i], wreg(pcs_r++)); break; |
| 4368 |
case kPrintfArgX: part_result = printf(chunks[i], xreg(pcs_r++)); break; |
| 4369 |
case kPrintfArgD: part_result = printf(chunks[i], dreg(pcs_f++)); break; |
| 4370 |
default: VIXL_UNREACHABLE(); |
| 4371 |
} |
| 4372 |
|
| 4373 |
if (part_result < 0) { |
| 4374 |
// Handle error values. |
| 4375 |
result = part_result; |
| 4376 |
break; |
| 4377 |
} |
| 4378 |
|
| 4379 |
result += part_result; |
| 4380 |
} |
| 4381 |
} |
| 4382 |
|
| 4383 |
printf("%s", clr_normal); |
| 4384 |
|
| 4385 |
// Printf returns its result in x0 (just like the C library's printf). |
| 4386 |
set_xreg(0, result); |
| 4387 |
|
| 4388 |
// The printf parameters are inlined in the code, so skip them. |
| 4389 |
set_pc(instr->InstructionAtOffset(kPrintfLength)); |
| 4390 |
|
| 4391 |
// Set LR as if we'd just called a native printf function. |
| 4392 |
set_lr(pc()); |
| 4393 |
|
| 4394 |
js_free(format); |
| 4395 |
} |
| 4396 |
|
| 4397 |
} // namespace vixl |
| 4398 |
|
| 4399 |
#endif // JS_SIMULATOR_ARM64 |