Debian Codegraph @

js/src/jit/loong64/Simulator-loong64.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80: */
3 // Copyright 2020 the V8 project authors. All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following
12 // disclaimer in the documentation and/or other materials provided
13 // with the distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include "jit/loong64/Simulator-loong64.h"
31
32 #include <float.h>
33 #include <limits>
34
35 #include "jit/AtomicOperations.h"
36 #include "jit/loong64/Assembler-loong64.h"
37 #include "js/Conversions.h"
38 #include "threading/LockGuard.h"
39 #include "vm/JSContext.h"
40 #include "vm/Runtime.h"
41 #include "wasm/WasmInstance.h"
42 #include "wasm/WasmSignalHandlers.h"
43
44 #define I8(v) static_cast<int8_t>(v)
45 #define I16(v) static_cast<int16_t>(v)
46 #define U16(v) static_cast<uint16_t>(v)
47 #define I32(v) static_cast<int32_t>(v)
48 #define U32(v) static_cast<uint32_t>(v)
49 #define I64(v) static_cast<int64_t>(v)
50 #define U64(v) static_cast<uint64_t>(v)
51 #define I128(v) static_cast<__int128_t>(v)
52 #define U128(v) static_cast<__uint128_t>(v)
53
54 #define I32_CHECK(v) \
55 ({ \
56 MOZ_ASSERT(I64(I32(v)) == I64(v)); \
57 I32((v)); \
58 })
59
60 namespace js {
61 namespace jit {
62
63 static int64_t MultiplyHighSigned(int64_t u, int64_t v) {
64 uint64_t u0, v0, w0;
65 int64_t u1, v1, w1, w2, t;
66
67 u0 = u & 0xFFFFFFFFL;
68 u1 = u >> 32;
69 v0 = v & 0xFFFFFFFFL;
70 v1 = v >> 32;
71
72 w0 = u0 * v0;
73 t = u1 * v0 + (w0 >> 32);
74 w1 = t & 0xFFFFFFFFL;
75 w2 = t >> 32;
76 w1 = u0 * v1 + w1;
77
78 return u1 * v1 + w2 + (w1 >> 32);
79 }
80
81 static uint64_t MultiplyHighUnsigned(uint64_t u, uint64_t v) {
82 uint64_t u0, v0, w0;
83 uint64_t u1, v1, w1, w2, t;
84
85 u0 = u & 0xFFFFFFFFL;
86 u1 = u >> 32;
87 v0 = v & 0xFFFFFFFFL;
88 v1 = v >> 32;
89
90 w0 = u0 * v0;
91 t = u1 * v0 + (w0 >> 32);
92 w1 = t & 0xFFFFFFFFL;
93 w2 = t >> 32;
94 w1 = u0 * v1 + w1;
95
96 return u1 * v1 + w2 + (w1 >> 32);
97 }
98
99 // Precondition: 0 <= shift < 32
100 inline constexpr uint32_t RotateRight32(uint32_t value, uint32_t shift) {
101 return (value >> shift) | (value << ((32 - shift) & 31));
102 }
103
104 // Precondition: 0 <= shift < 32
105 inline constexpr uint32_t RotateLeft32(uint32_t value, uint32_t shift) {
106 return (value << shift) | (value >> ((32 - shift) & 31));
107 }
108
109 // Precondition: 0 <= shift < 64
110 inline constexpr uint64_t RotateRight64(uint64_t value, uint64_t shift) {
111 return (value >> shift) | (value << ((64 - shift) & 63));
112 }
113
114 // Precondition: 0 <= shift < 64
115 inline constexpr uint64_t RotateLeft64(uint64_t value, uint64_t shift) {
116 return (value << shift) | (value >> ((64 - shift) & 63));
117 }
118
119 // break instr with MAX_BREAK_CODE.
120 static const Instr kCallRedirInstr = op_break | CODEMask;
121
122 // -----------------------------------------------------------------------------
123 // LoongArch64 assembly various constants.
124
125 class SimInstruction {
126 public:
127 enum {
128 kInstrSize = 4,
129 // On LoongArch, PC cannot actually be directly accessed. We behave as if PC
130 // was always the value of the current instruction being executed.
131 kPCReadOffset = 0
132 };
133
134 // Get the raw instruction bits.
135 inline Instr instructionBits() const {
136 return *reinterpret_cast<const Instr*>(this);
137 }
138
139 // Set the raw instruction bits to value.
140 inline void setInstructionBits(Instr value) {
141 *reinterpret_cast<Instr*>(this) = value;
142 }
143
144 // Read one particular bit out of the instruction bits.
145 inline int bit(int nr) const { return (instructionBits() >> nr) & 1; }
146
147 // Read a bit field out of the instruction bits.
148 inline int bits(int hi, int lo) const {
149 return (instructionBits() >> lo) & ((2 << (hi - lo)) - 1);
150 }
151
152 // Instruction type.
153 enum Type {
154 kUnsupported = -1,
155 kOp6Type,
156 kOp7Type,
157 kOp8Type,
158 kOp10Type,
159 kOp11Type,
160 kOp12Type,
161 kOp14Type,
162 kOp15Type,
163 kOp16Type,
164 kOp17Type,
165 kOp22Type,
166 kOp24Type
167 };
168
169 // Get the encoding type of the instruction.
170 Type instructionType() const;
171
172 inline int rjValue() const { return bits(RJShift + RJBits - 1, RJShift); }
173
174 inline int rkValue() const { return bits(RKShift + RKBits - 1, RKShift); }
175
176 inline int rdValue() const { return bits(RDShift + RDBits - 1, RDShift); }
177
178 inline int sa2Value() const { return bits(SAShift + SA2Bits - 1, SAShift); }
179
180 inline int sa3Value() const { return bits(SAShift + SA3Bits - 1, SAShift); }
181
182 inline int lsbwValue() const {
183 return bits(LSBWShift + LSBWBits - 1, LSBWShift);
184 }
185
186 inline int msbwValue() const {
187 return bits(MSBWShift + MSBWBits - 1, MSBWShift);
188 }
189
190 inline int lsbdValue() const {
191 return bits(LSBDShift + LSBDBits - 1, LSBDShift);
192 }
193
194 inline int msbdValue() const {
195 return bits(MSBDShift + MSBDBits - 1, MSBDShift);
196 }
197
198 inline int fdValue() const { return bits(FDShift + FDBits - 1, FDShift); }
199
200 inline int fjValue() const { return bits(FJShift + FJBits - 1, FJShift); }
201
202 inline int fkValue() const { return bits(FKShift + FKBits - 1, FKShift); }
203
204 inline int faValue() const { return bits(FAShift + FABits - 1, FAShift); }
205
206 inline int cdValue() const { return bits(CDShift + CDBits - 1, CDShift); }
207
208 inline int cjValue() const { return bits(CJShift + CJBits - 1, CJShift); }
209
210 inline int caValue() const { return bits(CAShift + CABits - 1, CAShift); }
211
212 inline int condValue() const {
213 return bits(CONDShift + CONDBits - 1, CONDShift);
214 }
215
216 inline int imm5Value() const {
217 return bits(Imm5Shift + Imm5Bits - 1, Imm5Shift);
218 }
219
220 inline int imm6Value() const {
221 return bits(Imm6Shift + Imm6Bits - 1, Imm6Shift);
222 }
223
224 inline int imm12Value() const {
225 return bits(Imm12Shift + Imm12Bits - 1, Imm12Shift);
226 }
227
228 inline int imm14Value() const {
229 return bits(Imm14Shift + Imm14Bits - 1, Imm14Shift);
230 }
231
232 inline int imm16Value() const {
233 return bits(Imm16Shift + Imm16Bits - 1, Imm16Shift);
234 }
235
236 inline int imm20Value() const {
237 return bits(Imm20Shift + Imm20Bits - 1, Imm20Shift);
238 }
239
240 inline int32_t imm26Value() const {
241 return bits(Imm26Shift + Imm26Bits - 1, Imm26Shift);
242 }
243
244 // Say if the instruction is a debugger break/trap.
245 bool isTrap() const;
246
247 private:
248 SimInstruction() = delete;
249 SimInstruction(const SimInstruction& other) = delete;
250 void operator=(const SimInstruction& other) = delete;
251 };
252
253 bool SimInstruction::isTrap() const {
254 // is break??
255 switch (bits(31, 15) << 15) {
256 case op_break:
257 return (instructionBits() != kCallRedirInstr) && (bits(15, 0) != 6);
258 default:
259 return false;
260 };
261 }
262
263 SimInstruction::Type SimInstruction::instructionType() const {
264 SimInstruction::Type kType = kUnsupported;
265
266 // Check for kOp6Type
267 switch (bits(31, 26) << 26) {
268 case op_beqz:
269 case op_bnez:
270 case op_bcz:
271 case op_jirl:
272 case op_b:
273 case op_bl:
274 case op_beq:
275 case op_bne:
276 case op_blt:
277 case op_bge:
278 case op_bltu:
279 case op_bgeu:
280 case op_addu16i_d:
281 kType = kOp6Type;
282 break;
283 default:
284 kType = kUnsupported;
285 }
286
287 if (kType == kUnsupported) {
288 // Check for kOp7Type
289 switch (bits(31, 25) << 25) {
290 case op_lu12i_w:
291 case op_lu32i_d:
292 case op_pcaddi:
293 case op_pcalau12i:
294 case op_pcaddu12i:
295 case op_pcaddu18i:
296 kType = kOp7Type;
297 break;
298 default:
299 kType = kUnsupported;
300 }
301 }
302
303 if (kType == kUnsupported) {
304 // Check for kOp8Type
305 switch (bits(31, 24) << 24) {
306 case op_ll_w:
307 case op_sc_w:
308 case op_ll_d:
309 case op_sc_d:
310 case op_ldptr_w:
311 case op_stptr_w:
312 case op_ldptr_d:
313 case op_stptr_d:
314 kType = kOp8Type;
315 break;
316 default:
317 kType = kUnsupported;
318 }
319 }
320
321 if (kType == kUnsupported) {
322 // Check for kOp10Type
323 switch (bits(31, 22) << 22) {
324 case op_bstrins_d:
325 case op_bstrpick_d:
326 case op_slti:
327 case op_sltui:
328 case op_addi_w:
329 case op_addi_d:
330 case op_lu52i_d:
331 case op_andi:
332 case op_ori:
333 case op_xori:
334 case op_ld_b:
335 case op_ld_h:
336 case op_ld_w:
337 case op_ld_d:
338 case op_st_b:
339 case op_st_h:
340 case op_st_w:
341 case op_st_d:
342 case op_ld_bu:
343 case op_ld_hu:
344 case op_ld_wu:
345 case op_preld:
346 case op_fld_s:
347 case op_fst_s:
348 case op_fld_d:
349 case op_fst_d:
350 case op_bstr_w: // BSTRINS_W & BSTRPICK_W
351 kType = kOp10Type;
352 break;
353 default:
354 kType = kUnsupported;
355 }
356 }
357
358 if (kType == kUnsupported) {
359 // Check for kOp11Type
360 switch (bits(31, 21) << 21) {
361 case op_bstr_w:
362 kType = kOp11Type;
363 break;
364 default:
365 kType = kUnsupported;
366 }
367 }
368
369 if (kType == kUnsupported) {
370 // Check for kOp12Type
371 switch (bits(31, 20) << 20) {
372 case op_fmadd_s:
373 case op_fmadd_d:
374 case op_fmsub_s:
375 case op_fmsub_d:
376 case op_fnmadd_s:
377 case op_fnmadd_d:
378 case op_fnmsub_s:
379 case op_fnmsub_d:
380 case op_fcmp_cond_s:
381 case op_fcmp_cond_d:
382 kType = kOp12Type;
383 break;
384 default:
385 kType = kUnsupported;
386 }
387 }
388
389 if (kType == kUnsupported) {
390 // Check for kOp14Type
391 switch (bits(31, 18) << 18) {
392 case op_bytepick_d:
393 case op_fsel:
394 kType = kOp14Type;
395 break;
396 default:
397 kType = kUnsupported;
398 }
399 }
400
401 if (kType == kUnsupported) {
402 // Check for kOp15Type
403 switch (bits(31, 17) << 17) {
404 case op_bytepick_w:
405 case op_alsl_w:
406 case op_alsl_wu:
407 case op_alsl_d:
408 kType = kOp15Type;
409 break;
410 default:
411 kType = kUnsupported;
412 }
413 }
414
415 if (kType == kUnsupported) {
416 // Check for kOp16Type
417 switch (bits(31, 16) << 16) {
418 case op_slli_d:
419 case op_srli_d:
420 case op_srai_d:
421 case op_rotri_d:
422 kType = kOp16Type;
423 break;
424 default:
425 kType = kUnsupported;
426 }
427 }
428
429 if (kType == kUnsupported) {
430 // Check for kOp17Type
431 switch (bits(31, 15) << 15) {
432 case op_slli_w:
433 case op_srli_w:
434 case op_srai_w:
435 case op_rotri_w:
436 case op_add_w:
437 case op_add_d:
438 case op_sub_w:
439 case op_sub_d:
440 case op_slt:
441 case op_sltu:
442 case op_maskeqz:
443 case op_masknez:
444 case op_nor:
445 case op_and:
446 case op_or:
447 case op_xor:
448 case op_orn:
449 case op_andn:
450 case op_sll_w:
451 case op_srl_w:
452 case op_sra_w:
453 case op_sll_d:
454 case op_srl_d:
455 case op_sra_d:
456 case op_rotr_w:
457 case op_rotr_d:
458 case op_mul_w:
459 case op_mul_d:
460 case op_mulh_d:
461 case op_mulh_du:
462 case op_mulh_w:
463 case op_mulh_wu:
464 case op_mulw_d_w:
465 case op_mulw_d_wu:
466 case op_div_w:
467 case op_mod_w:
468 case op_div_wu:
469 case op_mod_wu:
470 case op_div_d:
471 case op_mod_d:
472 case op_div_du:
473 case op_mod_du:
474 case op_break:
475 case op_fadd_s:
476 case op_fadd_d:
477 case op_fsub_s:
478 case op_fsub_d:
479 case op_fmul_s:
480 case op_fmul_d:
481 case op_fdiv_s:
482 case op_fdiv_d:
483 case op_fmax_s:
484 case op_fmax_d:
485 case op_fmin_s:
486 case op_fmin_d:
487 case op_fmaxa_s:
488 case op_fmaxa_d:
489 case op_fmina_s:
490 case op_fmina_d:
491 case op_fcopysign_s:
492 case op_fcopysign_d:
493 case op_ldx_b:
494 case op_ldx_h:
495 case op_ldx_w:
496 case op_ldx_d:
497 case op_stx_b:
498 case op_stx_h:
499 case op_stx_w:
500 case op_stx_d:
501 case op_ldx_bu:
502 case op_ldx_hu:
503 case op_ldx_wu:
504 case op_fldx_s:
505 case op_fldx_d:
506 case op_fstx_s:
507 case op_fstx_d:
508 case op_amswap_w:
509 case op_amswap_d:
510 case op_amadd_w:
511 case op_amadd_d:
512 case op_amand_w:
513 case op_amand_d:
514 case op_amor_w:
515 case op_amor_d:
516 case op_amxor_w:
517 case op_amxor_d:
518 case op_ammax_w:
519 case op_ammax_d:
520 case op_ammin_w:
521 case op_ammin_d:
522 case op_ammax_wu:
523 case op_ammax_du:
524 case op_ammin_wu:
525 case op_ammin_du:
526 case op_amswap_db_w:
527 case op_amswap_db_d:
528 case op_amadd_db_w:
529 case op_amadd_db_d:
530 case op_amand_db_w:
531 case op_amand_db_d:
532 case op_amor_db_w:
533 case op_amor_db_d:
534 case op_amxor_db_w:
535 case op_amxor_db_d:
536 case op_ammax_db_w:
537 case op_ammax_db_d:
538 case op_ammin_db_w:
539 case op_ammin_db_d:
540 case op_ammax_db_wu:
541 case op_ammax_db_du:
542 case op_ammin_db_wu:
543 case op_ammin_db_du:
544 case op_dbar:
545 case op_ibar:
546 kType = kOp17Type;
547 break;
548 default:
549 kType = kUnsupported;
550 }
551 }
552
553 if (kType == kUnsupported) {
554 // Check for kOp22Type
555 switch (bits(31, 10) << 10) {
556 case op_clo_w:
557 case op_clz_w:
558 case op_cto_w:
559 case op_ctz_w:
560 case op_clo_d:
561 case op_clz_d:
562 case op_cto_d:
563 case op_ctz_d:
564 case op_revb_2h:
565 case op_revb_4h:
566 case op_revb_2w:
567 case op_revb_d:
568 case op_revh_2w:
569 case op_revh_d:
570 case op_bitrev_4b:
571 case op_bitrev_8b:
572 case op_bitrev_w:
573 case op_bitrev_d:
574 case op_ext_w_h:
575 case op_ext_w_b:
576 case op_fabs_s:
577 case op_fabs_d:
578 case op_fneg_s:
579 case op_fneg_d:
580 case op_fsqrt_s:
581 case op_fsqrt_d:
582 case op_fmov_s:
583 case op_fmov_d:
584 case op_movgr2fr_w:
585 case op_movgr2fr_d:
586 case op_movgr2frh_w:
587 case op_movfr2gr_s:
588 case op_movfr2gr_d:
589 case op_movfrh2gr_s:
590 case op_movfcsr2gr:
591 case op_movfr2cf:
592 case op_movgr2cf:
593 case op_fcvt_s_d:
594 case op_fcvt_d_s:
595 case op_ftintrm_w_s:
596 case op_ftintrm_w_d:
597 case op_ftintrm_l_s:
598 case op_ftintrm_l_d:
599 case op_ftintrp_w_s:
600 case op_ftintrp_w_d:
601 case op_ftintrp_l_s:
602 case op_ftintrp_l_d:
603 case op_ftintrz_w_s:
604 case op_ftintrz_w_d:
605 case op_ftintrz_l_s:
606 case op_ftintrz_l_d:
607 case op_ftintrne_w_s:
608 case op_ftintrne_w_d:
609 case op_ftintrne_l_s:
610 case op_ftintrne_l_d:
611 case op_ftint_w_s:
612 case op_ftint_w_d:
613 case op_ftint_l_s:
614 case op_ftint_l_d:
615 case op_ffint_s_w:
616 case op_ffint_s_l:
617 case op_ffint_d_w:
618 case op_ffint_d_l:
619 case op_frint_s:
620 case op_frint_d:
621 kType = kOp22Type;
622 break;
623 default:
624 kType = kUnsupported;
625 }
626 }
627
628 if (kType == kUnsupported) {
629 // Check for kOp24Type
630 switch (bits(31, 8) << 8) {
631 case op_movcf2fr:
632 case op_movcf2gr:
633 kType = kOp24Type;
634 break;
635 default:
636 kType = kUnsupported;
637 }
638 }
639
640 return kType;
641 }
642
643 // C/C++ argument slots size.
644 const int kCArgSlotCount = 0;
645 const int kCArgsSlotsSize = kCArgSlotCount * sizeof(uintptr_t);
646
647 class CachePage {
648 public:
649 static const int LINE_VALID = 0;
650 static const int LINE_INVALID = 1;
651
652 static const int kPageShift = 12;
653 static const int kPageSize = 1 << kPageShift;
654 static const int kPageMask = kPageSize - 1;
655 static const int kLineShift = 2; // The cache line is only 4 bytes right now.
656 static const int kLineLength = 1 << kLineShift;
657 static const int kLineMask = kLineLength - 1;
658
659 CachePage() { memset(&validity_map_, LINE_INVALID, sizeof(validity_map_)); }
660
661 char* validityByte(int offset) {
662 return &validity_map_[offset >> kLineShift];
663 }
664
665 char* cachedData(int offset) { return &data_[offset]; }
666
667 private:
668 char data_[kPageSize]; // The cached data.
669 static const int kValidityMapSize = kPageSize >> kLineShift;
670 char validity_map_[kValidityMapSize]; // One byte per line.
671 };
672
673 // Protects the icache() and redirection() properties of the
674 // Simulator.
675 class AutoLockSimulatorCache : public LockGuard<Mutex> {
676 using Base = LockGuard<Mutex>;
677
678 public:
679 explicit AutoLockSimulatorCache()
680 : Base(SimulatorProcess::singleton_->cacheLock_) {}
681 };
682
683 mozilla::Atomic<size_t, mozilla::ReleaseAcquire>
684 SimulatorProcess::ICacheCheckingDisableCount(
685 1); // Checking is disabled by default.
686 SimulatorProcess* SimulatorProcess::singleton_ = nullptr;
687
688 int64_t Simulator::StopSimAt = -1;
689
690 Simulator* Simulator::Create() {
691 auto sim = MakeUnique<Simulator>();
692 if (!sim) {
693 return nullptr;
694 }
695
696 if (!sim->init()) {
697 return nullptr;
698 }
699
700 int64_t stopAt;
701 char* stopAtStr = getenv("LOONG64_SIM_STOP_AT");
702 if (stopAtStr && sscanf(stopAtStr, "%" PRIi64, &stopAt) == 1) {
703 fprintf(stderr, "\nStopping simulation at icount %" PRIi64 "\n", stopAt);
704 Simulator::StopSimAt = stopAt;
705 }
706
707 return sim.release();
708 }
709
710 void Simulator::Destroy(Simulator* sim) { js_delete(sim); }
711
712 // The loong64Debugger class is used by the simulator while debugging simulated
713 // code.
714 class loong64Debugger {
715 public:
716 explicit loong64Debugger(Simulator* sim) : sim_(sim) {}
717
718 void stop(SimInstruction* instr);
719 void debug();
720 // Print all registers with a nice formatting.
721 void printAllRegs();
722 void printAllRegsIncludingFPU();
723
724 private:
725 // We set the breakpoint code to 0x7fff to easily recognize it.
726 static const Instr kBreakpointInstr = op_break | (0x7fff & CODEMask);
727 static const Instr kNopInstr = 0x0;
728
729 Simulator* sim_;
730
731 int64_t getRegisterValue(int regnum);
732 int64_t getFPURegisterValueLong(int regnum);
733 float getFPURegisterValueFloat(int regnum);
734 double getFPURegisterValueDouble(int regnum);
735 bool getValue(const char* desc, int64_t* value);
736
737 // Set or delete a breakpoint. Returns true if successful.
738 bool setBreakpoint(SimInstruction* breakpc);
739 bool deleteBreakpoint(SimInstruction* breakpc);
740
741 // Undo and redo all breakpoints. This is needed to bracket disassembly and
742 // execution to skip past breakpoints when run from the debugger.
743 void undoBreakpoints();
744 void redoBreakpoints();
745 };
746
747 static void UNIMPLEMENTED() {
748 printf("UNIMPLEMENTED instruction.\n");
749 MOZ_CRASH();
750 }
751 static void UNREACHABLE() {
752 printf("UNREACHABLE instruction.\n");
753 MOZ_CRASH();
754 }
755 static void UNSUPPORTED() {
756 printf("Unsupported instruction.\n");
757 MOZ_CRASH();
758 }
759
760 void loong64Debugger::stop(SimInstruction* instr) {
761 // Get the stop code.
762 uint32_t code = instr->bits(25, 6);
763 // Retrieve the encoded address, which comes just after this stop.
764 char* msg =
765 *reinterpret_cast<char**>(sim_->get_pc() + SimInstruction::kInstrSize);
766 // Update this stop description.
767 if (!sim_->watchedStops_[code].desc_) {
768 sim_->watchedStops_[code].desc_ = msg;
769 }
770 // Print the stop message and code if it is not the default code.
771 if (code != kMaxStopCode) {
772 printf("Simulator hit stop %u: %s\n", code, msg);
773 } else {
774 printf("Simulator hit %s\n", msg);
775 }
776 sim_->set_pc(sim_->get_pc() + 2 * SimInstruction::kInstrSize);
777 debug();
778 }
779
780 int64_t loong64Debugger::getRegisterValue(int regnum) {
781 if (regnum == kPCRegister) {
782 return sim_->get_pc();
783 }
784 return sim_->getRegister(regnum);
785 }
786
787 int64_t loong64Debugger::getFPURegisterValueLong(int regnum) {
788 return sim_->getFpuRegister(regnum);
789 }
790
791 float loong64Debugger::getFPURegisterValueFloat(int regnum) {
792 return sim_->getFpuRegisterFloat(regnum);
793 }
794
795 double loong64Debugger::getFPURegisterValueDouble(int regnum) {
796 return sim_->getFpuRegisterDouble(regnum);
797 }
798
799 bool loong64Debugger::getValue(const char* desc, int64_t* value) {
800 Register reg = Register::FromName(desc);
801 if (reg != InvalidReg) {
802 *value = getRegisterValue(reg.code());
803 return true;
804 }
805
806 if (strncmp(desc, "0x", 2) == 0) {
807 return sscanf(desc + 2, "%" PRIx64, reinterpret_cast<uint64_t*>(value)) ==
808 1;
809 }
810 return sscanf(desc, "%" PRIu64, reinterpret_cast<uint64_t*>(value)) == 1;
811 }
812
813 bool loong64Debugger::setBreakpoint(SimInstruction* breakpc) {
814 // Check if a breakpoint can be set. If not return without any side-effects.
815 if (sim_->break_pc_ != nullptr) {
816 return false;
817 }
818
819 // Set the breakpoint.
820 sim_->break_pc_ = breakpc;
821 sim_->break_instr_ = breakpc->instructionBits();
822 // Not setting the breakpoint instruction in the code itself. It will be set
823 // when the debugger shell continues.
824 return true;
825 }
826
827 bool loong64Debugger::deleteBreakpoint(SimInstruction* breakpc) {
828 if (sim_->break_pc_ != nullptr) {
829 sim_->break_pc_->setInstructionBits(sim_->break_instr_);
830 }
831
832 sim_->break_pc_ = nullptr;
833 sim_->break_instr_ = 0;
834 return true;
835 }
836
837 void loong64Debugger::undoBreakpoints() {
838 if (sim_->break_pc_) {
839 sim_->break_pc_->setInstructionBits(sim_->break_instr_);
840 }
841 }
842
843 void loong64Debugger::redoBreakpoints() {
844 if (sim_->break_pc_) {
845 sim_->break_pc_->setInstructionBits(kBreakpointInstr);
846 }
847 }
848
849 void loong64Debugger::printAllRegs() {
850 int64_t value;
851 for (uint32_t i = 0; i < Registers::Total; i++) {
852 value = getRegisterValue(i);
853 printf("%3s: 0x%016" PRIx64 " %20" PRIi64 " ", Registers::GetName(i),
854 value, value);
855
856 if (i % 2) {
857 printf("\n");
858 }
859 }
860 printf("\n");
861
862 value = getRegisterValue(Simulator::pc);
863 printf(" pc: 0x%016" PRIx64 "\n", value);
864 }
865
866 void loong64Debugger::printAllRegsIncludingFPU() {
867 printAllRegs();
868
869 printf("\n\n");
870 // f0, f1, f2, ... f31.
871 for (uint32_t i = 0; i < FloatRegisters::TotalPhys; i++) {
872 printf("%3s: 0x%016" PRIi64 "\tflt: %-8.4g\tdbl: %-16.4g\n",
873 FloatRegisters::GetName(i), getFPURegisterValueLong(i),
874 getFPURegisterValueFloat(i), getFPURegisterValueDouble(i));
875 }
876 }
877
878 static char* ReadLine(const char* prompt) {
879 UniqueChars result;
880 char lineBuf[256];
881 int offset = 0;
882 bool keepGoing = true;
883 fprintf(stdout, "%s", prompt);
884 fflush(stdout);
885 while (keepGoing) {
886 if (fgets(lineBuf, sizeof(lineBuf), stdin) == nullptr) {
887 // fgets got an error. Just give up.
888 return nullptr;
889 }
890 int len = strlen(lineBuf);
891 if (len > 0 && lineBuf[len - 1] == '\n') {
892 // Since we read a new line we are done reading the line. This
893 // will exit the loop after copying this buffer into the result.
894 keepGoing = false;
895 }
896 if (!result) {
897 // Allocate the initial result and make room for the terminating '\0'
898 result.reset(js_pod_malloc<char>(len + 1));
899 if (!result) {
900 return nullptr;
901 }
902 } else {
903 // Allocate a new result with enough room for the new addition.
904 int new_len = offset + len + 1;
905 char* new_result = js_pod_malloc<char>(new_len);
906 if (!new_result) {
907 return nullptr;
908 }
909 // Copy the existing input into the new array and set the new
910 // array as the result.
911 memcpy(new_result, result.get(), offset * sizeof(char));
912 result.reset(new_result);
913 }
914 // Copy the newly read line into the result.
915 memcpy(result.get() + offset, lineBuf, len * sizeof(char));
916 offset += len;
917 }
918
919 MOZ_ASSERT(result);
920 result[offset] = '\0';
921 return result.release();
922 }
923
924 static void DisassembleInstruction(uint64_t pc) {
925 printf("Not supported on loongarch64 yet\n");
926 }
927
928 void loong64Debugger::debug() {
929 intptr_t lastPC = -1;
930 bool done = false;
931
932 #define COMMAND_SIZE 63
933 #define ARG_SIZE 255
934
935 #define STR(a) #a
936 #define XSTR(a) STR(a)
937
938 char cmd[COMMAND_SIZE + 1];
939 char arg1[ARG_SIZE + 1];
940 char arg2[ARG_SIZE + 1];
941 char* argv[3] = {cmd, arg1, arg2};
942
943 // Make sure to have a proper terminating character if reaching the limit.
944 cmd[COMMAND_SIZE] = 0;
945 arg1[ARG_SIZE] = 0;
946 arg2[ARG_SIZE] = 0;
947
948 // Undo all set breakpoints while running in the debugger shell. This will
949 // make them invisible to all commands.
950 undoBreakpoints();
951
952 while (!done && (sim_->get_pc() != Simulator::end_sim_pc)) {
953 if (lastPC != sim_->get_pc()) {
954 DisassembleInstruction(sim_->get_pc());
955 printf(" 0x%016" PRIi64 " \n", sim_->get_pc());
956 lastPC = sim_->get_pc();
957 }
958 char* line = ReadLine("sim> ");
959 if (line == nullptr) {
960 break;
961 } else {
962 char* last_input = sim_->lastDebuggerInput();
963 if (strcmp(line, "\n") == 0 && last_input != nullptr) {
964 line = last_input;
965 } else {
966 // Ownership is transferred to sim_;
967 sim_->setLastDebuggerInput(line);
968 }
969 // Use sscanf to parse the individual parts of the command line. At the
970 // moment no command expects more than two parameters.
971 int argc = sscanf(line,
972 "%" XSTR(COMMAND_SIZE) "s "
973 "%" XSTR(ARG_SIZE) "s "
974 "%" XSTR(ARG_SIZE) "s",
975 cmd, arg1, arg2);
976 if ((strcmp(cmd, "si") == 0) || (strcmp(cmd, "stepi") == 0)) {
977 SimInstruction* instr =
978 reinterpret_cast<SimInstruction*>(sim_->get_pc());
979 if (!instr->isTrap()) {
980 sim_->instructionDecode(
981 reinterpret_cast<SimInstruction*>(sim_->get_pc()));
982 } else {
983 // Allow si to jump over generated breakpoints.
984 printf("/!\\ Jumping over generated breakpoint.\n");
985 sim_->set_pc(sim_->get_pc() + SimInstruction::kInstrSize);
986 }
987 sim_->icount_++;
988 } else if ((strcmp(cmd, "c") == 0) || (strcmp(cmd, "cont") == 0)) {
989 // Execute the one instruction we broke at with breakpoints disabled.
990 sim_->instructionDecode(
991 reinterpret_cast<SimInstruction*>(sim_->get_pc()));
992 sim_->icount_++;
993 // Leave the debugger shell.
994 done = true;
995 } else if ((strcmp(cmd, "p") == 0) || (strcmp(cmd, "print") == 0)) {
996 if (argc == 2) {
997 int64_t value;
998 if (strcmp(arg1, "all") == 0) {
999 printAllRegs();
1000 } else if (strcmp(arg1, "allf") == 0) {
1001 printAllRegsIncludingFPU();
1002 } else {
1003 Register reg = Register::FromName(arg1);
1004 FloatRegisters::Code fReg = FloatRegisters::FromName(arg1);
1005 if (reg != InvalidReg) {
1006 value = getRegisterValue(reg.code());
1007 printf("%s: 0x%016" PRIi64 " %20" PRIi64 " \n", arg1, value,
1008 value);
1009 } else if (fReg != FloatRegisters::Invalid) {
1010 printf("%3s: 0x%016" PRIi64 "\tflt: %-8.4g\tdbl: %-16.4g\n",
1011 FloatRegisters::GetName(fReg),
1012 getFPURegisterValueLong(fReg),
1013 getFPURegisterValueFloat(fReg),
1014 getFPURegisterValueDouble(fReg));
1015 } else {
1016 printf("%s unrecognized\n", arg1);
1017 }
1018 }
1019 } else {
1020 printf("print <register> or print <fpu register> single\n");
1021 }
1022 } else if (strcmp(cmd, "stack") == 0 || strcmp(cmd, "mem") == 0) {
1023 int64_t* cur = nullptr;
1024 int64_t* end = nullptr;
1025 int next_arg = 1;
1026
1027 if (strcmp(cmd, "stack") == 0) {
1028 cur = reinterpret_cast<int64_t*>(sim_->getRegister(Simulator::sp));
1029 } else { // Command "mem".
1030 int64_t value;
1031 if (!getValue(arg1, &value)) {
1032 printf("%s unrecognized\n", arg1);
1033 continue;
1034 }
1035 cur = reinterpret_cast<int64_t*>(value);
1036 next_arg++;
1037 }
1038
1039 int64_t words;
1040 if (argc == next_arg) {
1041 words = 10;
1042 } else {
1043 if (!getValue(argv[next_arg], &words)) {
1044 words = 10;
1045 }
1046 }
1047 end = cur + words;
1048
1049 while (cur < end) {
1050 printf(" %p: 0x%016" PRIx64 " %20" PRIi64, cur, *cur, *cur);
1051 printf("\n");
1052 cur++;
1053 }
1054
1055 } else if ((strcmp(cmd, "disasm") == 0) || (strcmp(cmd, "dpc") == 0) ||
1056 (strcmp(cmd, "di") == 0)) {
1057 uint8_t* cur = nullptr;
1058 uint8_t* end = nullptr;
1059
1060 if (argc == 1) {
1061 cur = reinterpret_cast<uint8_t*>(sim_->get_pc());
1062 end = cur + (10 * SimInstruction::kInstrSize);
1063 } else if (argc == 2) {
1064 Register reg = Register::FromName(arg1);
1065 if (reg != InvalidReg || strncmp(arg1, "0x", 2) == 0) {
1066 // The argument is an address or a register name.
1067 int64_t value;
1068 if (getValue(arg1, &value)) {
1069 cur = reinterpret_cast<uint8_t*>(value);
1070 // Disassemble 10 instructions at <arg1>.
1071 end = cur + (10 * SimInstruction::kInstrSize);
1072 }
1073 } else {
1074 // The argument is the number of instructions.
1075 int64_t value;
1076 if (getValue(arg1, &value)) {
1077 cur = reinterpret_cast<uint8_t*>(sim_->get_pc());
1078 // Disassemble <arg1> instructions.
1079 end = cur + (value * SimInstruction::kInstrSize);
1080 }
1081 }
1082 } else {
1083 int64_t value1;
1084 int64_t value2;
1085 if (getValue(arg1, &value1) && getValue(arg2, &value2)) {
1086 cur = reinterpret_cast<uint8_t*>(value1);
1087 end = cur + (value2 * SimInstruction::kInstrSize);
1088 }
1089 }
1090
1091 while (cur < end) {
1092 DisassembleInstruction(uint64_t(cur));
1093 cur += SimInstruction::kInstrSize;
1094 }
1095 } else if (strcmp(cmd, "gdb") == 0) {
1096 printf("relinquishing control to gdb\n");
1097 #if defined(__x86_64__)
1098 asm("int $3");
1099 #elif defined(__aarch64__)
1100 // see masm.breakpoint for arm64
1101 asm("brk #0xf000");
1102 #endif
1103 printf("regaining control from gdb\n");
1104 } else if (strcmp(cmd, "break") == 0) {
1105 if (argc == 2) {
1106 int64_t value;
1107 if (getValue(arg1, &value)) {
1108 if (!setBreakpoint(reinterpret_cast<SimInstruction*>(value))) {
1109 printf("setting breakpoint failed\n");
1110 }
1111 } else {
1112 printf("%s unrecognized\n", arg1);
1113 }
1114 } else {
1115 printf("break <address>\n");
1116 }
1117 } else if (strcmp(cmd, "del") == 0) {
1118 if (!deleteBreakpoint(nullptr)) {
1119 printf("deleting breakpoint failed\n");
1120 }
1121 } else if (strcmp(cmd, "flags") == 0) {
1122 printf("No flags on LOONG64 !\n");
1123 } else if (strcmp(cmd, "stop") == 0) {
1124 int64_t value;
1125 intptr_t stop_pc = sim_->get_pc() - 2 * SimInstruction::kInstrSize;
1126 SimInstruction* stop_instr = reinterpret_cast<SimInstruction*>(stop_pc);
1127 SimInstruction* msg_address = reinterpret_cast<SimInstruction*>(
1128 stop_pc + SimInstruction::kInstrSize);
1129 if ((argc == 2) && (strcmp(arg1, "unstop") == 0)) {
1130 // Remove the current stop.
1131 if (sim_->isStopInstruction(stop_instr)) {
1132 stop_instr->setInstructionBits(kNopInstr);
1133 msg_address->setInstructionBits(kNopInstr);
1134 } else {
1135 printf("Not at debugger stop.\n");
1136 }
1137 } else if (argc == 3) {
1138 // Print information about all/the specified breakpoint(s).
1139 if (strcmp(arg1, "info") == 0) {
1140 if (strcmp(arg2, "all") == 0) {
1141 printf("Stop information:\n");
1142 for (uint32_t i = kMaxWatchpointCode + 1; i <= kMaxStopCode;
1143 i++) {
1144 sim_->printStopInfo(i);
1145 }
1146 } else if (getValue(arg2, &value)) {
1147 sim_->printStopInfo(value);
1148 } else {
1149 printf("Unrecognized argument.\n");
1150 }
1151 } else if (strcmp(arg1, "enable") == 0) {
1152 // Enable all/the specified breakpoint(s).
1153 if (strcmp(arg2, "all") == 0) {
1154 for (uint32_t i = kMaxWatchpointCode + 1; i <= kMaxStopCode;
1155 i++) {
1156 sim_->enableStop(i);
1157 }
1158 } else if (getValue(arg2, &value)) {
1159 sim_->enableStop(value);
1160 } else {
1161 printf("Unrecognized argument.\n");
1162 }
1163 } else if (strcmp(arg1, "disable") == 0) {
1164 // Disable all/the specified breakpoint(s).
1165 if (strcmp(arg2, "all") == 0) {
1166 for (uint32_t i = kMaxWatchpointCode + 1; i <= kMaxStopCode;
1167 i++) {
1168 sim_->disableStop(i);
1169 }
1170 } else if (getValue(arg2, &value)) {
1171 sim_->disableStop(value);
1172 } else {
1173 printf("Unrecognized argument.\n");
1174 }
1175 }
1176 } else {
1177 printf("Wrong usage. Use help command for more information.\n");
1178 }
1179 } else if ((strcmp(cmd, "h") == 0) || (strcmp(cmd, "help") == 0)) {
1180 printf("cont\n");
1181 printf(" continue execution (alias 'c')\n");
1182 printf("stepi\n");
1183 printf(" step one instruction (alias 'si')\n");
1184 printf("print <register>\n");
1185 printf(" print register content (alias 'p')\n");
1186 printf(" use register name 'all' to print all registers\n");
1187 printf("printobject <register>\n");
1188 printf(" print an object from a register (alias 'po')\n");
1189 printf("stack [<words>]\n");
1190 printf(" dump stack content, default dump 10 words)\n");
1191 printf("mem <address> [<words>]\n");
1192 printf(" dump memory content, default dump 10 words)\n");
1193 printf("flags\n");
1194 printf(" print flags\n");
1195 printf("disasm [<instructions>]\n");
1196 printf("disasm [<address/register>]\n");
1197 printf("disasm [[<address/register>] <instructions>]\n");
1198 printf(" disassemble code, default is 10 instructions\n");
1199 printf(" from pc (alias 'di')\n");
1200 printf("gdb\n");
1201 printf(" enter gdb\n");
1202 printf("break <address>\n");
1203 printf(" set a break point on the address\n");
1204 printf("del\n");
1205 printf(" delete the breakpoint\n");
1206 printf("stop feature:\n");
1207 printf(" Description:\n");
1208 printf(" Stops are debug instructions inserted by\n");
1209 printf(" the Assembler::stop() function.\n");
1210 printf(" When hitting a stop, the Simulator will\n");
1211 printf(" stop and and give control to the Debugger.\n");
1212 printf(" All stop codes are watched:\n");
1213 printf(" - They can be enabled / disabled: the Simulator\n");
1214 printf(" will / won't stop when hitting them.\n");
1215 printf(" - The Simulator keeps track of how many times they \n");
1216 printf(" are met. (See the info command.) Going over a\n");
1217 printf(" disabled stop still increases its counter. \n");
1218 printf(" Commands:\n");
1219 printf(" stop info all/<code> : print infos about number <code>\n");
1220 printf(" or all stop(s).\n");
1221 printf(" stop enable/disable all/<code> : enables / disables\n");
1222 printf(" all or number <code> stop(s)\n");
1223 printf(" stop unstop\n");
1224 printf(" ignore the stop instruction at the current location\n");
1225 printf(" from now on\n");
1226 } else {
1227 printf("Unknown command: %s\n", cmd);
1228 }
1229 }
1230 }
1231
1232 // Add all the breakpoints back to stop execution and enter the debugger
1233 // shell when hit.
1234 redoBreakpoints();
1235
1236 #undef COMMAND_SIZE
1237 #undef ARG_SIZE
1238
1239 #undef STR
1240 #undef XSTR
1241 }
1242
1243 static bool AllOnOnePage(uintptr_t start, int size) {
1244 intptr_t start_page = (start & ~CachePage::kPageMask);
1245 intptr_t end_page = ((start + size) & ~CachePage::kPageMask);
1246 return start_page == end_page;
1247 }
1248
1249 void Simulator::setLastDebuggerInput(char* input) {
1250 js_free(lastDebuggerInput_);
1251 lastDebuggerInput_ = input;
1252 }
1253
1254 static CachePage* GetCachePageLocked(SimulatorProcess::ICacheMap& i_cache,
1255 void* page) {
1256 SimulatorProcess::ICacheMap::AddPtr p = i_cache.lookupForAdd(page);
1257 if (p) {
1258 return p->value();
1259 }
1260 AutoEnterOOMUnsafeRegion oomUnsafe;
1261 CachePage* new_page = js_new<CachePage>();
1262 if (!new_page || !i_cache.add(p, page, new_page)) {
1263 oomUnsafe.crash("Simulator CachePage");
1264 }
1265 return new_page;
1266 }
1267
1268 // Flush from start up to and not including start + size.
1269 static void FlushOnePageLocked(SimulatorProcess::ICacheMap& i_cache,
1270 intptr_t start, int size) {
1271 MOZ_ASSERT(size <= CachePage::kPageSize);
1272 MOZ_ASSERT(AllOnOnePage(start, size - 1));
1273 MOZ_ASSERT((start & CachePage::kLineMask) == 0);
1274 MOZ_ASSERT((size & CachePage::kLineMask) == 0);
1275 void* page = reinterpret_cast<void*>(start & (~CachePage::kPageMask));
1276 int offset = (start & CachePage::kPageMask);
1277 CachePage* cache_page = GetCachePageLocked(i_cache, page);
1278 char* valid_bytemap = cache_page->validityByte(offset);
1279 memset(valid_bytemap, CachePage::LINE_INVALID, size >> CachePage::kLineShift);
1280 }
1281
1282 static void FlushICacheLocked(SimulatorProcess::ICacheMap& i_cache,
1283 void* start_addr, size_t size) {
1284 intptr_t start = reinterpret_cast<intptr_t>(start_addr);
1285 int intra_line = (start & CachePage::kLineMask);
1286 start -= intra_line;
1287 size += intra_line;
1288 size = ((size - 1) | CachePage::kLineMask) + 1;
1289 int offset = (start & CachePage::kPageMask);
1290 while (!AllOnOnePage(start, size - 1)) {
1291 int bytes_to_flush = CachePage::kPageSize - offset;
1292 FlushOnePageLocked(i_cache, start, bytes_to_flush);
1293 start += bytes_to_flush;
1294 size -= bytes_to_flush;
1295 MOZ_ASSERT((start & CachePage::kPageMask) == 0);
1296 offset = 0;
1297 }
1298 if (size != 0) {
1299 FlushOnePageLocked(i_cache, start, size);
1300 }
1301 }
1302
1303 /* static */
1304 void SimulatorProcess::checkICacheLocked(SimInstruction* instr) {
1305 intptr_t address = reinterpret_cast<intptr_t>(instr);
1306 void* page = reinterpret_cast<void*>(address & (~CachePage::kPageMask));
1307 void* line = reinterpret_cast<void*>(address & (~CachePage::kLineMask));
1308 int offset = (address & CachePage::kPageMask);
1309 CachePage* cache_page = GetCachePageLocked(icache(), page);
1310 char* cache_valid_byte = cache_page->validityByte(offset);
1311 bool cache_hit = (*cache_valid_byte == CachePage::LINE_VALID);
1312 char* cached_line = cache_page->cachedData(offset & ~CachePage::kLineMask);
1313
1314 if (cache_hit) {
1315 // Check that the data in memory matches the contents of the I-cache.
1316 mozilla::DebugOnly<int> cmpret =
1317 memcmp(reinterpret_cast<void*>(instr), cache_page->cachedData(offset),
1318 SimInstruction::kInstrSize);
1319 MOZ_ASSERT(cmpret == 0);
1320 } else {
1321 // Cache miss. Load memory into the cache.
1322 memcpy(cached_line, line, CachePage::kLineLength);
1323 *cache_valid_byte = CachePage::LINE_VALID;
1324 }
1325 }
1326
1327 HashNumber SimulatorProcess::ICacheHasher::hash(const Lookup& l) {
1328 return U32(reinterpret_cast<uintptr_t>(l)) >> 2;
1329 }
1330
1331 bool SimulatorProcess::ICacheHasher::match(const Key& k, const Lookup& l) {
1332 MOZ_ASSERT((reinterpret_cast<intptr_t>(k) & CachePage::kPageMask) == 0);
1333 MOZ_ASSERT((reinterpret_cast<intptr_t>(l) & CachePage::kPageMask) == 0);
1334 return k == l;
1335 }
1336
1337 /* static */
1338 void SimulatorProcess::FlushICache(void* start_addr, size_t size) {
1339 if (!ICacheCheckingDisableCount) {
1340 AutoLockSimulatorCache als;
1341 js::jit::FlushICacheLocked(icache(), start_addr, size);
1342 }
1343 }
1344
1345 Simulator::Simulator() {
1346 // Set up simulator support first. Some of this information is needed to
1347 // setup the architecture state.
1348
1349 // Note, allocation and anything that depends on allocated memory is
1350 // deferred until init(), in order to handle OOM properly.
1351
1352 stack_ = nullptr;
1353 stackLimit_ = 0;
1354 pc_modified_ = false;
1355 icount_ = 0;
1356 break_count_ = 0;
1357 break_pc_ = nullptr;
1358 break_instr_ = 0;
1359 single_stepping_ = false;
1360 single_step_callback_ = nullptr;
1361 single_step_callback_arg_ = nullptr;
1362
1363 // Set up architecture state.
1364 // All registers are initialized to zero to start with.
1365 for (int i = 0; i < Register::kNumSimuRegisters; i++) {
1366 registers_[i] = 0;
1367 }
1368 for (int i = 0; i < Simulator::FPURegister::kNumFPURegisters; i++) {
1369 FPUregisters_[i] = 0;
1370 }
1371
1372 for (int i = 0; i < kNumCFRegisters; i++) {
1373 CFregisters_[i] = 0;
1374 }
1375
1376 FCSR_ = 0;
1377 LLBit_ = false;
1378 LLAddr_ = 0;
1379 lastLLValue_ = 0;
1380
1381 // The ra and pc are initialized to a known bad value that will cause an
1382 // access violation if the simulator ever tries to execute it.
1383 registers_[pc] = bad_ra;
1384 registers_[ra] = bad_ra;
1385
1386 for (int i = 0; i < kNumExceptions; i++) {
1387 exceptions[i] = 0;
1388 }
1389
1390 lastDebuggerInput_ = nullptr;
1391 }
1392
1393 bool Simulator::init() {
1394 // Allocate 2MB for the stack. Note that we will only use 1MB, see below.
1395 static const size_t stackSize = 2 * 1024 * 1024;
1396 stack_ = js_pod_malloc<char>(stackSize);
1397 if (!stack_) {
1398 return false;
1399 }
1400
1401 // Leave a safety margin of 1MB to prevent overrunning the stack when
1402 // pushing values (total stack size is 2MB).
1403 stackLimit_ = reinterpret_cast<uintptr_t>(stack_) + 1024 * 1024;
1404
1405 // The sp is initialized to point to the bottom (high address) of the
1406 // allocated stack area. To be safe in potential stack underflows we leave
1407 // some buffer below.
1408 registers_[sp] = reinterpret_cast<int64_t>(stack_) + stackSize - 64;
1409
1410 return true;
1411 }
1412
1413 // When the generated code calls an external reference we need to catch that in
1414 // the simulator. The external reference will be a function compiled for the
1415 // host architecture. We need to call that function instead of trying to
1416 // execute it with the simulator. We do that by redirecting the external
1417 // reference to a swi (software-interrupt) instruction that is handled by
1418 // the simulator. We write the original destination of the jump just at a known
1419 // offset from the swi instruction so the simulator knows what to call.
1420 class Redirection {
1421 friend class SimulatorProcess;
1422
1423 // sim's lock must already be held.
1424 Redirection(void* nativeFunction, ABIFunctionType type)
1425 : nativeFunction_(nativeFunction),
1426 swiInstruction_(kCallRedirInstr),
1427 type_(type),
1428 next_(nullptr) {
1429 next_ = SimulatorProcess::redirection();
1430 if (!SimulatorProcess::ICacheCheckingDisableCount) {
1431 FlushICacheLocked(SimulatorProcess::icache(), addressOfSwiInstruction(),
1432 SimInstruction::kInstrSize);
1433 }
1434 SimulatorProcess::setRedirection(this);
1435 }
1436
1437 public:
1438 void* addressOfSwiInstruction() { return &swiInstruction_; }
1439 void* nativeFunction() const { return nativeFunction_; }
1440 ABIFunctionType type() const { return type_; }
1441
1442 static Redirection* Get(void* nativeFunction, ABIFunctionType type) {
1443 AutoLockSimulatorCache als;
1444
1445 Redirection* current = SimulatorProcess::redirection();
1446 for (; current != nullptr; current = current->next_) {
1447 if (current->nativeFunction_ == nativeFunction) {
1448 MOZ_ASSERT(current->type() == type);
1449 return current;
1450 }
1451 }
1452
1453 // Note: we can't use js_new here because the constructor is private.
1454 AutoEnterOOMUnsafeRegion oomUnsafe;
1455 Redirection* redir = js_pod_malloc<Redirection>(1);
1456 if (!redir) {
1457 oomUnsafe.crash("Simulator redirection");
1458 }
1459 new (redir) Redirection(nativeFunction, type);
1460 return redir;
1461 }
1462
1463 static Redirection* FromSwiInstruction(SimInstruction* swiInstruction) {
1464 uint8_t* addrOfSwi = reinterpret_cast<uint8_t*>(swiInstruction);
1465 uint8_t* addrOfRedirection =
1466 addrOfSwi - offsetof(Redirection, swiInstruction_);
1467 return reinterpret_cast<Redirection*>(addrOfRedirection);
1468 }
1469
1470 private:
1471 void* nativeFunction_;
1472 uint32_t swiInstruction_;
1473 ABIFunctionType type_;
1474 Redirection* next_;
1475 };
1476
1477 Simulator::~Simulator() { js_free(stack_); }
1478
1479 SimulatorProcess::SimulatorProcess()
1480 : cacheLock_(mutexid::SimulatorCacheLock), redirection_(nullptr) {
1481 if (getenv("LOONG64_SIM_ICACHE_CHECKS")) {
1482 ICacheCheckingDisableCount = 0;
1483 }
1484 }
1485
1486 SimulatorProcess::~SimulatorProcess() {
1487 Redirection* r = redirection_;
1488 while (r) {
1489 Redirection* next = r->next_;
1490 js_delete(r);
1491 r = next;
1492 }
1493 }
1494
1495 /* static */
1496 void* Simulator::RedirectNativeFunction(void* nativeFunction,
1497 ABIFunctionType type) {
1498 Redirection* redirection = Redirection::Get(nativeFunction, type);
1499 return redirection->addressOfSwiInstruction();
1500 }
1501
1502 // Get the active Simulator for the current thread.
1503 Simulator* Simulator::Current() {
1504 JSContext* cx = TlsContext.get();
1505 MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
1506 return cx->simulator();
1507 }
1508
1509 // Sets the register in the architecture state. It will also deal with updating
1510 // Simulator internal state for special registers such as PC.
1511 void Simulator::setRegister(int reg, int64_t value) {
1512 MOZ_ASSERT((reg >= 0) && (reg < Register::kNumSimuRegisters));
1513 if (reg == pc) {
1514 pc_modified_ = true;
1515 }
1516
1517 // Zero register always holds 0.
1518 registers_[reg] = (reg == 0) ? 0 : value;
1519 }
1520
1521 void Simulator::setFpuRegister(int fpureg, int64_t value) {
1522 MOZ_ASSERT((fpureg >= 0) &&
1523 (fpureg < Simulator::FPURegister::kNumFPURegisters));
1524 FPUregisters_[fpureg] = value;
1525 }
1526
1527 void Simulator::setFpuRegisterHiWord(int fpureg, int32_t value) {
1528 // Set ONLY upper 32-bits, leaving lower bits untouched.
1529 MOZ_ASSERT((fpureg >= 0) &&
1530 (fpureg < Simulator::FPURegister::kNumFPURegisters));
1531 int32_t* phiword;
1532 phiword = (reinterpret_cast<int32_t*>(&FPUregisters_[fpureg])) + 1;
1533
1534 *phiword = value;
1535 }
1536
1537 void Simulator::setFpuRegisterWord(int fpureg, int32_t value) {
1538 // Set ONLY lower 32-bits, leaving upper bits untouched.
1539 MOZ_ASSERT((fpureg >= 0) &&
1540 (fpureg < Simulator::FPURegister::kNumFPURegisters));
1541 int32_t* pword;
1542 pword = reinterpret_cast<int32_t*>(&FPUregisters_[fpureg]);
1543
1544 *pword = value;
1545 }
1546
1547 void Simulator::setFpuRegisterWordInvalidResult(float original, float rounded,
1548 int fpureg) {
1549 double max_int32 = static_cast<double>(INT32_MAX);
1550 double min_int32 = static_cast<double>(INT32_MIN);
1551
1552 if (std::isnan(original)) {
1553 setFpuRegisterWord(fpureg, 0);
1554 } else if (rounded > max_int32) {
1555 setFpuRegister(fpureg, kFPUInvalidResult);
1556 } else if (rounded < min_int32) {
1557 setFpuRegister(fpureg, kFPUInvalidResultNegative);
1558 } else {
1559 UNREACHABLE();
1560 }
1561 }
1562
1563 void Simulator::setFpuRegisterWordInvalidResult(double original, double rounded,
1564 int fpureg) {
1565 double max_int32 = static_cast<double>(INT32_MAX);
1566 double min_int32 = static_cast<double>(INT32_MIN);
1567
1568 if (std::isnan(original)) {
1569 setFpuRegisterWord(fpureg, 0);
1570 } else if (rounded > max_int32) {
1571 setFpuRegisterWord(fpureg, kFPUInvalidResult);
1572 } else if (rounded < min_int32) {
1573 setFpuRegisterWord(fpureg, kFPUInvalidResultNegative);
1574 } else {
1575 UNREACHABLE();
1576 }
1577 }
1578
1579 void Simulator::setFpuRegisterInvalidResult(float original, float rounded,
1580 int fpureg) {
1581 double max_int32 = static_cast<double>(INT32_MAX);
1582 double min_int32 = static_cast<double>(INT32_MIN);
1583
1584 if (std::isnan(original)) {
1585 setFpuRegister(fpureg, 0);
1586 } else if (rounded > max_int32) {
1587 setFpuRegister(fpureg, kFPUInvalidResult);
1588 } else if (rounded < min_int32) {
1589 setFpuRegister(fpureg, kFPUInvalidResultNegative);
1590 } else {
1591 UNREACHABLE();
1592 }
1593 }
1594
1595 void Simulator::setFpuRegisterInvalidResult(double original, double rounded,
1596 int fpureg) {
1597 double max_int32 = static_cast<double>(INT32_MAX);
1598 double min_int32 = static_cast<double>(INT32_MIN);
1599
1600 if (std::isnan(original)) {
1601 setFpuRegister(fpureg, 0);
1602 } else if (rounded > max_int32) {
1603 setFpuRegister(fpureg, kFPUInvalidResult);
1604 } else if (rounded < min_int32) {
1605 setFpuRegister(fpureg, kFPUInvalidResultNegative);
1606 } else {
1607 UNREACHABLE();
1608 }
1609 }
1610
1611 void Simulator::setFpuRegisterInvalidResult64(float original, float rounded,
1612 int fpureg) {
1613 // The value of INT64_MAX (2^63-1) can't be represented as double exactly,
1614 // loading the most accurate representation into max_int64, which is 2^63.
1615 double max_int64 = static_cast<double>(INT64_MAX);
1616 double min_int64 = static_cast<double>(INT64_MIN);
1617
1618 if (std::isnan(original)) {
1619 setFpuRegister(fpureg, 0);
1620 } else if (rounded >= max_int64) {
1621 setFpuRegister(fpureg, kFPU64InvalidResult);
1622 } else if (rounded < min_int64) {
1623 setFpuRegister(fpureg, kFPU64InvalidResultNegative);
1624 } else {
1625 UNREACHABLE();
1626 }
1627 }
1628
1629 void Simulator::setFpuRegisterInvalidResult64(double original, double rounded,
1630 int fpureg) {
1631 // The value of INT64_MAX (2^63-1) can't be represented as double exactly,
1632 // loading the most accurate representation into max_int64, which is 2^63.
1633 double max_int64 = static_cast<double>(INT64_MAX);
1634 double min_int64 = static_cast<double>(INT64_MIN);
1635
1636 if (std::isnan(original)) {
1637 setFpuRegister(fpureg, 0);
1638 } else if (rounded >= max_int64) {
1639 setFpuRegister(fpureg, kFPU64InvalidResult);
1640 } else if (rounded < min_int64) {
1641 setFpuRegister(fpureg, kFPU64InvalidResultNegative);
1642 } else {
1643 UNREACHABLE();
1644 }
1645 }
1646
1647 void Simulator::setFpuRegisterFloat(int fpureg, float value) {
1648 MOZ_ASSERT((fpureg >= 0) &&
1649 (fpureg < Simulator::FPURegister::kNumFPURegisters));
1650 *mozilla::BitwiseCast<float*>(&FPUregisters_[fpureg]) = value;
1651 }
1652
1653 void Simulator::setFpuRegisterDouble(int fpureg, double value) {
1654 MOZ_ASSERT((fpureg >= 0) &&
1655 (fpureg < Simulator::FPURegister::kNumFPURegisters));
1656 *mozilla::BitwiseCast<double*>(&FPUregisters_[fpureg]) = value;
1657 }
1658
1659 void Simulator::setCFRegister(int cfreg, bool value) {
1660 MOZ_ASSERT((cfreg >= 0) && (cfreg < kNumCFRegisters));
1661 CFregisters_[cfreg] = value;
1662 }
1663
1664 bool Simulator::getCFRegister(int cfreg) const {
1665 MOZ_ASSERT((cfreg >= 0) && (cfreg < kNumCFRegisters));
1666 return CFregisters_[cfreg];
1667 }
1668
1669 // Get the register from the architecture state. This function does handle
1670 // the special case of accessing the PC register.
1671 int64_t Simulator::getRegister(int reg) const {
1672 MOZ_ASSERT((reg >= 0) && (reg < Register::kNumSimuRegisters));
1673 if (reg == 0) {
1674 return 0;
1675 }
1676 return registers_[reg] + ((reg == pc) ? SimInstruction::kPCReadOffset : 0);
1677 }
1678
1679 int64_t Simulator::getFpuRegister(int fpureg) const {
1680 MOZ_ASSERT((fpureg >= 0) &&
1681 (fpureg < Simulator::FPURegister::kNumFPURegisters));
1682 return FPUregisters_[fpureg];
1683 }
1684
1685 int32_t Simulator::getFpuRegisterWord(int fpureg) const {
1686 MOZ_ASSERT((fpureg >= 0) &&
1687 (fpureg < Simulator::FPURegister::kNumFPURegisters));
1688 return *mozilla::BitwiseCast<int32_t*>(&FPUregisters_[fpureg]);
1689 }
1690
1691 int32_t Simulator::getFpuRegisterSignedWord(int fpureg) const {
1692 MOZ_ASSERT((fpureg >= 0) &&
1693 (fpureg < Simulator::FPURegister::kNumFPURegisters));
1694 return *mozilla::BitwiseCast<int32_t*>(&FPUregisters_[fpureg]);
1695 }
1696
1697 int32_t Simulator::getFpuRegisterHiWord(int fpureg) const {
1698 MOZ_ASSERT((fpureg >= 0) &&
1699 (fpureg < Simulator::FPURegister::kNumFPURegisters));
1700 return *((mozilla::BitwiseCast<int32_t*>(&FPUregisters_[fpureg])) + 1);
1701 }
1702
1703 float Simulator::getFpuRegisterFloat(int fpureg) const {
1704 MOZ_ASSERT((fpureg >= 0) &&
1705 (fpureg < Simulator::FPURegister::kNumFPURegisters));
1706 return *mozilla::BitwiseCast<float*>(&FPUregisters_[fpureg]);
1707 }
1708
1709 double Simulator::getFpuRegisterDouble(int fpureg) const {
1710 MOZ_ASSERT((fpureg >= 0) &&
1711 (fpureg < Simulator::FPURegister::kNumFPURegisters));
1712 return *mozilla::BitwiseCast<double*>(&FPUregisters_[fpureg]);
1713 }
1714
1715 void Simulator::setCallResultDouble(double result) {
1716 setFpuRegisterDouble(f0, result);
1717 }
1718
1719 void Simulator::setCallResultFloat(float result) {
1720 setFpuRegisterFloat(f0, result);
1721 }
1722
1723 void Simulator::setCallResult(int64_t res) { setRegister(a0, res); }
1724 #ifdef XP_DARWIN
1725 // add a dedicated setCallResult for intptr_t on Darwin
1726 void Simulator::setCallResult(intptr_t res) { setRegister(v0, I64(res)); }
1727 #endif
1728 void Simulator::setCallResult(__int128_t res) {
1729 setRegister(a0, I64(res));
1730 setRegister(a1, I64(res >> 64));
1731 }
1732
1733 // Helper functions for setting and testing the FCSR register's bits.
1734 void Simulator::setFCSRBit(uint32_t cc, bool value) {
1735 if (value) {
1736 FCSR_ |= (1 << cc);
1737 } else {
1738 FCSR_ &= ~(1 << cc);
1739 }
1740 }
1741
1742 bool Simulator::testFCSRBit(uint32_t cc) { return FCSR_ & (1 << cc); }
1743
1744 unsigned int Simulator::getFCSRRoundingMode() {
1745 return FCSR_ & kFPURoundingModeMask;
1746 }
1747
1748 // Sets the rounding error codes in FCSR based on the result of the rounding.
1749 // Returns true if the operation was invalid.
1750 template <typename T>
1751 bool Simulator::setFCSRRoundError(double original, double rounded) {
1752 bool ret = false;
1753
1754 setFCSRBit(kFCSRInexactCauseBit, false);
1755 setFCSRBit(kFCSRUnderflowCauseBit, false);
1756 setFCSRBit(kFCSROverflowCauseBit, false);
1757 setFCSRBit(kFCSRInvalidOpCauseBit, false);
1758
1759 if (!std::isfinite(original) || !std::isfinite(rounded)) {
1760 setFCSRBit(kFCSRInvalidOpFlagBit, true);
1761 setFCSRBit(kFCSRInvalidOpCauseBit, true);
1762 ret = true;
1763 }
1764
1765 if (original != rounded) {
1766 setFCSRBit(kFCSRInexactFlagBit, true);
1767 setFCSRBit(kFCSRInexactCauseBit, true);
1768 }
1769
1770 if (rounded < DBL_MIN && rounded > -DBL_MIN && rounded != 0) {
1771 setFCSRBit(kFCSRUnderflowFlagBit, true);
1772 setFCSRBit(kFCSRUnderflowCauseBit, true);
1773 ret = true;
1774 }
1775
1776 if ((long double)rounded > (long double)std::numeric_limits<T>::max() ||
1777 (long double)rounded < (long double)std::numeric_limits<T>::min()) {
1778 setFCSRBit(kFCSROverflowFlagBit, true);
1779 setFCSRBit(kFCSROverflowCauseBit, true);
1780 // The reference is not really clear but it seems this is required:
1781 setFCSRBit(kFCSRInvalidOpFlagBit, true);
1782 setFCSRBit(kFCSRInvalidOpCauseBit, true);
1783 ret = true;
1784 }
1785
1786 return ret;
1787 }
1788
1789 // For cvt instructions only
1790 template <typename T>
1791 void Simulator::roundAccordingToFCSR(T toRound, T* rounded,
1792 int32_t* rounded_int) {
1793 switch ((FCSR_ >> 8) & 3) {
1794 case kRoundToNearest:
1795 *rounded = std::floor(toRound + 0.5);
1796 *rounded_int = static_cast<int32_t>(*rounded);
1797 if ((*rounded_int & 1) != 0 && *rounded_int - toRound == 0.5) {
1798 // If the number is halfway between two integers,
1799 // round to the even one.
1800 *rounded_int -= 1;
1801 *rounded -= 1.;
1802 }
1803 break;
1804 case kRoundToZero:
1805 *rounded = trunc(toRound);
1806 *rounded_int = static_cast<int32_t>(*rounded);
1807 break;
1808 case kRoundToPlusInf:
1809 *rounded = std::ceil(toRound);
1810 *rounded_int = static_cast<int32_t>(*rounded);
1811 break;
1812 case kRoundToMinusInf:
1813 *rounded = std::floor(toRound);
1814 *rounded_int = static_cast<int32_t>(*rounded);
1815 break;
1816 }
1817 }
1818
1819 template <typename T>
1820 void Simulator::round64AccordingToFCSR(T toRound, T* rounded,
1821 int64_t* rounded_int) {
1822 switch ((FCSR_ >> 8) & 3) {
1823 case kRoundToNearest:
1824 *rounded = std::floor(toRound + 0.5);
1825 *rounded_int = static_cast<int64_t>(*rounded);
1826 if ((*rounded_int & 1) != 0 && *rounded_int - toRound == 0.5) {
1827 // If the number is halfway between two integers,
1828 // round to the even one.
1829 *rounded_int -= 1;
1830 *rounded -= 1.;
1831 }
1832 break;
1833 case kRoundToZero:
1834 *rounded = trunc(toRound);
1835 *rounded_int = static_cast<int64_t>(*rounded);
1836 break;
1837 case kRoundToPlusInf:
1838 *rounded = std::ceil(toRound);
1839 *rounded_int = static_cast<int64_t>(*rounded);
1840 break;
1841 case kRoundToMinusInf:
1842 *rounded = std::floor(toRound);
1843 *rounded_int = static_cast<int64_t>(*rounded);
1844 break;
1845 }
1846 }
1847
1848 // Raw access to the PC register.
1849 void Simulator::set_pc(int64_t value) {
1850 pc_modified_ = true;
1851 registers_[pc] = value;
1852 }
1853
1854 bool Simulator::has_bad_pc() const {
1855 return ((registers_[pc] == bad_ra) || (registers_[pc] == end_sim_pc));
1856 }
1857
1858 // Raw access to the PC register without the special adjustment when reading.
1859 int64_t Simulator::get_pc() const { return registers_[pc]; }
1860
1861 JS::ProfilingFrameIterator::RegisterState Simulator::registerState() {
1862 wasm::RegisterState state;
1863 state.pc = (void*)get_pc();
1864 state.fp = (void*)getRegister(fp);
1865 state.sp = (void*)getRegister(sp);
1866 state.lr = (void*)getRegister(ra);
1867 return state;
1868 }
1869
1870 uint8_t Simulator::readBU(uint64_t addr) {
1871 if (handleWasmSegFault(addr, 1)) {
1872 return 0xff;
1873 }
1874
1875 uint8_t* ptr = reinterpret_cast<uint8_t*>(addr);
1876 return *ptr;
1877 }
1878
1879 int8_t Simulator::readB(uint64_t addr) {
1880 if (handleWasmSegFault(addr, 1)) {
1881 return -1;
1882 }
1883
1884 int8_t* ptr = reinterpret_cast<int8_t*>(addr);
1885 return *ptr;
1886 }
1887
1888 void Simulator::writeB(uint64_t addr, uint8_t value) {
1889 if (handleWasmSegFault(addr, 1)) {
1890 return;
1891 }
1892
1893 uint8_t* ptr = reinterpret_cast<uint8_t*>(addr);
1894 *ptr = value;
1895 }
1896
1897 void Simulator::writeB(uint64_t addr, int8_t value) {
1898 if (handleWasmSegFault(addr, 1)) {
1899 return;
1900 }
1901
1902 int8_t* ptr = reinterpret_cast<int8_t*>(addr);
1903 *ptr = value;
1904 }
1905
1906 uint16_t Simulator::readHU(uint64_t addr, SimInstruction* instr) {
1907 if (handleWasmSegFault(addr, 2)) {
1908 return 0xffff;
1909 }
1910
1911 uint16_t* ptr = reinterpret_cast<uint16_t*>(addr);
1912 return *ptr;
1913 }
1914
1915 int16_t Simulator::readH(uint64_t addr, SimInstruction* instr) {
1916 if (handleWasmSegFault(addr, 2)) {
1917 return -1;
1918 }
1919
1920 int16_t* ptr = reinterpret_cast<int16_t*>(addr);
1921 return *ptr;
1922 }
1923
1924 void Simulator::writeH(uint64_t addr, uint16_t value, SimInstruction* instr) {
1925 if (handleWasmSegFault(addr, 2)) {
1926 return;
1927 }
1928
1929 uint16_t* ptr = reinterpret_cast<uint16_t*>(addr);
1930 LLBit_ = false;
1931 *ptr = value;
1932 return;
1933 }
1934
1935 void Simulator::writeH(uint64_t addr, int16_t value, SimInstruction* instr) {
1936 if (handleWasmSegFault(addr, 2)) {
1937 return;
1938 }
1939
1940 int16_t* ptr = reinterpret_cast<int16_t*>(addr);
1941 LLBit_ = false;
1942 *ptr = value;
1943 return;
1944 }
1945
1946 uint32_t Simulator::readWU(uint64_t addr, SimInstruction* instr) {
1947 if (handleWasmSegFault(addr, 4)) {
1948 return -1;
1949 }
1950
1951 uint32_t* ptr = reinterpret_cast<uint32_t*>(addr);
1952 return *ptr;
1953 }
1954
1955 int32_t Simulator::readW(uint64_t addr, SimInstruction* instr) {
1956 if (handleWasmSegFault(addr, 4)) {
1957 return -1;
1958 }
1959
1960 int32_t* ptr = reinterpret_cast<int32_t*>(addr);
1961 return *ptr;
1962 }
1963
1964 void Simulator::writeW(uint64_t addr, uint32_t value, SimInstruction* instr) {
1965 if (handleWasmSegFault(addr, 4)) {
1966 return;
1967 }
1968
1969 uint32_t* ptr = reinterpret_cast<uint32_t*>(addr);
1970 LLBit_ = false;
1971 *ptr = value;
1972 return;
1973 }
1974
1975 void Simulator::writeW(uint64_t addr, int32_t value, SimInstruction* instr) {
1976 if (handleWasmSegFault(addr, 4)) {
1977 return;
1978 }
1979
1980 int32_t* ptr = reinterpret_cast<int32_t*>(addr);
1981 LLBit_ = false;
1982 *ptr = value;
1983 return;
1984 }
1985
1986 int64_t Simulator::readDW(uint64_t addr, SimInstruction* instr) {
1987 if (handleWasmSegFault(addr, 8)) {
1988 return -1;
1989 }
1990
1991 intptr_t* ptr = reinterpret_cast<intptr_t*>(addr);
1992 return *ptr;
1993 }
1994
1995 void Simulator::writeDW(uint64_t addr, int64_t value, SimInstruction* instr) {
1996 if (handleWasmSegFault(addr, 8)) {
1997 return;
1998 }
1999
2000 int64_t* ptr = reinterpret_cast<int64_t*>(addr);
2001 LLBit_ = false;
2002 *ptr = value;
2003 return;
2004 }
2005
2006 double Simulator::readD(uint64_t addr, SimInstruction* instr) {
2007 if (handleWasmSegFault(addr, 8)) {
2008 return NAN;
2009 }
2010
2011 double* ptr = reinterpret_cast<double*>(addr);
2012 return *ptr;
2013 }
2014
2015 void Simulator::writeD(uint64_t addr, double value, SimInstruction* instr) {
2016 if (handleWasmSegFault(addr, 8)) {
2017 return;
2018 }
2019
2020 double* ptr = reinterpret_cast<double*>(addr);
2021 LLBit_ = false;
2022 *ptr = value;
2023 return;
2024 }
2025
2026 int Simulator::loadLinkedW(uint64_t addr, SimInstruction* instr) {
2027 if ((addr & 3) == 0) {
2028 if (handleWasmSegFault(addr, 4)) {
2029 return -1;
2030 }
2031
2032 volatile int32_t* ptr = reinterpret_cast<volatile int32_t*>(addr);
2033 int32_t value = *ptr;
2034 lastLLValue_ = value;
2035 LLAddr_ = addr;
2036 // Note that any memory write or "external" interrupt should reset this
2037 // value to false.
2038 LLBit_ = true;
2039 return value;
2040 }
2041 printf("Unaligned write at 0x%016" PRIx64 ", pc=0x%016" PRIxPTR "\n", addr,
2042 reinterpret_cast<intptr_t>(instr));
2043 MOZ_CRASH();
2044 return 0;
2045 }
2046
2047 int Simulator::storeConditionalW(uint64_t addr, int value,
2048 SimInstruction* instr) {
2049 // Correct behavior in this case, as defined by architecture, is to just
2050 // return 0, but there is no point at allowing that. It is certainly an
2051 // indicator of a bug.
2052 if (addr != LLAddr_) {
2053 printf("SC to bad address: 0x%016" PRIx64 ", pc=0x%016" PRIxPTR
2054 ", expected: 0x%016" PRIxPTR "\n",
2055 addr, reinterpret_cast<intptr_t>(instr), LLAddr_);
2056 MOZ_CRASH();
2057 }
2058
2059 if ((addr & 3) == 0) {
2060 SharedMem<int32_t*> ptr =
2061 SharedMem<int32_t*>::shared(reinterpret_cast<int32_t*>(addr));
2062
2063 if (!LLBit_) {
2064 return 0;
2065 }
2066
2067 LLBit_ = false;
2068 LLAddr_ = 0;
2069 int32_t expected = int32_t(lastLLValue_);
2070 int32_t old =
2071 AtomicOperations::compareExchangeSeqCst(ptr, expected, int32_t(value));
2072 return (old == expected) ? 1 : 0;
2073 }
2074 printf("Unaligned SC at 0x%016" PRIx64 ", pc=0x%016" PRIxPTR "\n", addr,
2075 reinterpret_cast<intptr_t>(instr));
2076 MOZ_CRASH();
2077 return 0;
2078 }
2079
2080 int64_t Simulator::loadLinkedD(uint64_t addr, SimInstruction* instr) {
2081 if ((addr & kPointerAlignmentMask) == 0) {
2082 if (handleWasmSegFault(addr, 8)) {
2083 return -1;
2084 }
2085
2086 volatile int64_t* ptr = reinterpret_cast<volatile int64_t*>(addr);
2087 int64_t value = *ptr;
2088 lastLLValue_ = value;
2089 LLAddr_ = addr;
2090 // Note that any memory write or "external" interrupt should reset this
2091 // value to false.
2092 LLBit_ = true;
2093 return value;
2094 }
2095 printf("Unaligned write at 0x%016" PRIx64 ", pc=0x%016" PRIxPTR "\n", addr,
2096 reinterpret_cast<intptr_t>(instr));
2097 MOZ_CRASH();
2098 return 0;
2099 }
2100
2101 int Simulator::storeConditionalD(uint64_t addr, int64_t value,
2102 SimInstruction* instr) {
2103 // Correct behavior in this case, as defined by architecture, is to just
2104 // return 0, but there is no point at allowing that. It is certainly an
2105 // indicator of a bug.
2106 if (addr != LLAddr_) {
2107 printf("SC to bad address: 0x%016" PRIx64 ", pc=0x%016" PRIxPTR
2108 ", expected: 0x%016" PRIxPTR "\n",
2109 addr, reinterpret_cast<intptr_t>(instr), LLAddr_);
2110 MOZ_CRASH();
2111 }
2112
2113 if ((addr & kPointerAlignmentMask) == 0) {
2114 SharedMem<int64_t*> ptr =
2115 SharedMem<int64_t*>::shared(reinterpret_cast<int64_t*>(addr));
2116
2117 if (!LLBit_) {
2118 return 0;
2119 }
2120
2121 LLBit_ = false;
2122 LLAddr_ = 0;
2123 int64_t expected = lastLLValue_;
2124 int64_t old =
2125 AtomicOperations::compareExchangeSeqCst(ptr, expected, int64_t(value));
2126 return (old == expected) ? 1 : 0;
2127 }
2128 printf("Unaligned SC at 0x%016" PRIx64 ", pc=0x%016" PRIxPTR "\n", addr,
2129 reinterpret_cast<intptr_t>(instr));
2130 MOZ_CRASH();
2131 return 0;
2132 }
2133
2134 uintptr_t Simulator::stackLimit() const { return stackLimit_; }
2135
2136 uintptr_t* Simulator::addressOfStackLimit() { return &stackLimit_; }
2137
2138 bool Simulator::overRecursed(uintptr_t newsp) const {
2139 if (newsp == 0) {
2140 newsp = getRegister(sp);
2141 }
2142 return newsp <= stackLimit();
2143 }
2144
2145 bool Simulator::overRecursedWithExtra(uint32_t extra) const {
2146 uintptr_t newsp = getRegister(sp) - extra;
2147 return newsp <= stackLimit();
2148 }
2149
2150 // Unsupported instructions use format to print an error and stop execution.
2151 void Simulator::format(SimInstruction* instr, const char* format) {
2152 printf("Simulator found unsupported instruction:\n 0x%016lx: %s\n",
2153 reinterpret_cast<intptr_t>(instr), format);
2154 MOZ_CRASH();
2155 }
2156
2157 inline int32_t Simulator::rj_reg(SimInstruction* instr) const {
2158 return instr->rjValue();
2159 }
2160
2161 inline int64_t Simulator::rj(SimInstruction* instr) const {
2162 return getRegister(rj_reg(instr));
2163 }
2164
2165 inline uint64_t Simulator::rj_u(SimInstruction* instr) const {
2166 return static_cast<uint64_t>(getRegister(rj_reg(instr)));
2167 }
2168
2169 inline int32_t Simulator::rk_reg(SimInstruction* instr) const {
2170 return instr->rkValue();
2171 }
2172
2173 inline int64_t Simulator::rk(SimInstruction* instr) const {
2174 return getRegister(rk_reg(instr));
2175 }
2176
2177 inline uint64_t Simulator::rk_u(SimInstruction* instr) const {
2178 return static_cast<uint64_t>(getRegister(rk_reg(instr)));
2179 }
2180
2181 inline int32_t Simulator::rd_reg(SimInstruction* instr) const {
2182 return instr->rdValue();
2183 }
2184
2185 inline int64_t Simulator::rd(SimInstruction* instr) const {
2186 return getRegister(rd_reg(instr));
2187 }
2188
2189 inline uint64_t Simulator::rd_u(SimInstruction* instr) const {
2190 return static_cast<uint64_t>(getRegister(rd_reg(instr)));
2191 }
2192
2193 inline int32_t Simulator::fa_reg(SimInstruction* instr) const {
2194 return instr->faValue();
2195 }
2196
2197 inline float Simulator::fa_float(SimInstruction* instr) const {
2198 return getFpuRegisterFloat(fa_reg(instr));
2199 }
2200
2201 inline double Simulator::fa_double(SimInstruction* instr) const {
2202 return getFpuRegisterDouble(fa_reg(instr));
2203 }
2204
2205 inline int32_t Simulator::fj_reg(SimInstruction* instr) const {
2206 return instr->fjValue();
2207 }
2208
2209 inline float Simulator::fj_float(SimInstruction* instr) const {
2210 return getFpuRegisterFloat(fj_reg(instr));
2211 }
2212
2213 inline double Simulator::fj_double(SimInstruction* instr) const {
2214 return getFpuRegisterDouble(fj_reg(instr));
2215 }
2216
2217 inline int32_t Simulator::fk_reg(SimInstruction* instr) const {
2218 return instr->fkValue();
2219 }
2220
2221 inline float Simulator::fk_float(SimInstruction* instr) const {
2222 return getFpuRegisterFloat(fk_reg(instr));
2223 }
2224
2225 inline double Simulator::fk_double(SimInstruction* instr) const {
2226 return getFpuRegisterDouble(fk_reg(instr));
2227 }
2228
2229 inline int32_t Simulator::fd_reg(SimInstruction* instr) const {
2230 return instr->fdValue();
2231 }
2232
2233 inline float Simulator::fd_float(SimInstruction* instr) const {
2234 return getFpuRegisterFloat(fd_reg(instr));
2235 }
2236
2237 inline double Simulator::fd_double(SimInstruction* instr) const {
2238 return getFpuRegisterDouble(fd_reg(instr));
2239 }
2240
2241 inline int32_t Simulator::cj_reg(SimInstruction* instr) const {
2242 return instr->cjValue();
2243 }
2244
2245 inline bool Simulator::cj(SimInstruction* instr) const {
2246 return getCFRegister(cj_reg(instr));
2247 }
2248
2249 inline int32_t Simulator::cd_reg(SimInstruction* instr) const {
2250 return instr->cdValue();
2251 }
2252
2253 inline bool Simulator::cd(SimInstruction* instr) const {
2254 return getCFRegister(cd_reg(instr));
2255 }
2256
2257 inline int32_t Simulator::ca_reg(SimInstruction* instr) const {
2258 return instr->caValue();
2259 }
2260
2261 inline bool Simulator::ca(SimInstruction* instr) const {
2262 return getCFRegister(ca_reg(instr));
2263 }
2264
2265 inline uint32_t Simulator::sa2(SimInstruction* instr) const {
2266 return instr->sa2Value();
2267 }
2268
2269 inline uint32_t Simulator::sa3(SimInstruction* instr) const {
2270 return instr->sa3Value();
2271 }
2272
2273 inline uint32_t Simulator::ui5(SimInstruction* instr) const {
2274 return instr->imm5Value();
2275 }
2276
2277 inline uint32_t Simulator::ui6(SimInstruction* instr) const {
2278 return instr->imm6Value();
2279 }
2280
2281 inline uint32_t Simulator::lsbw(SimInstruction* instr) const {
2282 return instr->lsbwValue();
2283 }
2284
2285 inline uint32_t Simulator::msbw(SimInstruction* instr) const {
2286 return instr->msbwValue();
2287 }
2288
2289 inline uint32_t Simulator::lsbd(SimInstruction* instr) const {
2290 return instr->lsbdValue();
2291 }
2292
2293 inline uint32_t Simulator::msbd(SimInstruction* instr) const {
2294 return instr->msbdValue();
2295 }
2296
2297 inline uint32_t Simulator::cond(SimInstruction* instr) const {
2298 return instr->condValue();
2299 }
2300
2301 inline int32_t Simulator::si12(SimInstruction* instr) const {
2302 return (instr->imm12Value() << 20) >> 20;
2303 }
2304
2305 inline uint32_t Simulator::ui12(SimInstruction* instr) const {
2306 return instr->imm12Value();
2307 }
2308
2309 inline int32_t Simulator::si14(SimInstruction* instr) const {
2310 return (instr->imm14Value() << 18) >> 18;
2311 }
2312
2313 inline int32_t Simulator::si16(SimInstruction* instr) const {
2314 return (instr->imm16Value() << 16) >> 16;
2315 }
2316
2317 inline int32_t Simulator::si20(SimInstruction* instr) const {
2318 return (instr->imm20Value() << 12) >> 12;
2319 }
2320
2321 ABI_FUNCTION_TYPE_SIM_PROTOTYPES
2322
2323 // Software interrupt instructions are used by the simulator to call into C++.
2324 void Simulator::softwareInterrupt(SimInstruction* instr) {
2325 // the break_ instruction could get us here.
2326 mozilla::DebugOnly<int32_t> opcode_hi15 = instr->bits(31, 17);
2327 MOZ_ASSERT(opcode_hi15 == 0x15);
2328 uint32_t code = instr->bits(14, 0);
2329
2330 if (instr->instructionBits() == kCallRedirInstr) {
2331 Redirection* redirection = Redirection::FromSwiInstruction(instr);
2332 uintptr_t nativeFn =
2333 reinterpret_cast<uintptr_t>(redirection->nativeFunction());
2334
2335 // Get the SP for reading stack arguments
2336 int64_t* sp_ = reinterpret_cast<int64_t*>(getRegister(sp));
2337
2338 // Store argument register values in local variables for ease of use below.
2339 int64_t a0_ = getRegister(a0);
2340 int64_t a1_ = getRegister(a1);
2341 int64_t a2_ = getRegister(a2);
2342 int64_t a3_ = getRegister(a3);
2343 int64_t a4_ = getRegister(a4);
2344 int64_t a5_ = getRegister(a5);
2345 int64_t a6_ = getRegister(a6);
2346 int64_t a7_ = getRegister(a7);
2347 float f0_s = getFpuRegisterFloat(f0);
2348 float f1_s = getFpuRegisterFloat(f1);
2349 float f2_s = getFpuRegisterFloat(f2);
2350 float f3_s = getFpuRegisterFloat(f3);
2351 float f4_s = getFpuRegisterFloat(f4);
2352 double f0_d = getFpuRegisterDouble(f0);
2353 double f1_d = getFpuRegisterDouble(f1);
2354 double f2_d = getFpuRegisterDouble(f2);
2355 double f3_d = getFpuRegisterDouble(f3);
2356
2357 // This is dodgy but it works because the C entry stubs are never moved.
2358 // See comment in codegen-arm.cc and bug 1242173.
2359 int64_t saved_ra = getRegister(ra);
2360
2361 bool stack_aligned = (getRegister(sp) & (ABIStackAlignment - 1)) == 0;
2362 if (!stack_aligned) {
2363 fprintf(stderr, "Runtime call with unaligned stack!\n");
2364 MOZ_CRASH();
2365 }
2366
2367 if (single_stepping_) {
2368 single_step_callback_(single_step_callback_arg_, this, nullptr);
2369 }
2370
2371 switch (redirection->type()) {
2372 ABI_FUNCTION_TYPE_LOONGARCH64_SIM_DISPATCH
2373
2374 default:
2375 MOZ_CRASH("Unknown function type.");
2376 }
2377
2378 if (single_stepping_) {
2379 single_step_callback_(single_step_callback_arg_, this, nullptr);
2380 }
2381
2382 setRegister(ra, saved_ra);
2383 set_pc(getRegister(ra));
2384 } else if ((instr->bits(31, 15) << 15 == op_break) && code == kWasmTrapCode) {
2385 uint8_t* newPC;
2386 if (wasm::HandleIllegalInstruction(registerState(), &newPC)) {
2387 set_pc(int64_t(newPC));
2388 return;
2389 }
2390 } else if ((instr->bits(31, 15) << 15 == op_break) && code <= kMaxStopCode &&
2391 code != 6) {
2392 if (isWatchpoint(code)) {
2393 // printWatchpoint(code);
2394 } else {
2395 increaseStopCounter(code);
2396 handleStop(code, instr);
2397 }
2398 } else {
2399 // All remaining break_ codes, and all traps are handled here.
2400 loong64Debugger dbg(this);
2401 dbg.debug();
2402 }
2403 }
2404
2405 // Stop helper functions.
2406 bool Simulator::isWatchpoint(uint32_t code) {
2407 return (code <= kMaxWatchpointCode);
2408 }
2409
2410 void Simulator::printWatchpoint(uint32_t code) {
2411 loong64Debugger dbg(this);
2412 ++break_count_;
2413 printf("\n---- break %d marker: %20" PRIi64 " (instr count: %20" PRIi64
2414 ") ----\n",
2415 code, break_count_, icount_);
2416 dbg.printAllRegs(); // Print registers and continue running.
2417 }
2418
2419 void Simulator::handleStop(uint32_t code, SimInstruction* instr) {
2420 // Stop if it is enabled, otherwise go on jumping over the stop
2421 // and the message address.
2422 if (isEnabledStop(code)) {
2423 loong64Debugger dbg(this);
2424 dbg.stop(instr);
2425 } else {
2426 set_pc(get_pc() + 1 * SimInstruction::kInstrSize);
2427 }
2428 }
2429
2430 bool Simulator::isStopInstruction(SimInstruction* instr) {
2431 int32_t opcode_hi15 = instr->bits(31, 17);
2432 uint32_t code = static_cast<uint32_t>(instr->bits(14, 0));
2433 return (opcode_hi15 == 0x15) && code > kMaxWatchpointCode &&
2434 code <= kMaxStopCode;
2435 }
2436
2437 bool Simulator::isEnabledStop(uint32_t code) {
2438 MOZ_ASSERT(code <= kMaxStopCode);
2439 MOZ_ASSERT(code > kMaxWatchpointCode);
2440 return !(watchedStops_[code].count_ & kStopDisabledBit);
2441 }
2442
2443 void Simulator::enableStop(uint32_t code) {
2444 if (!isEnabledStop(code)) {
2445 watchedStops_[code].count_ &= ~kStopDisabledBit;
2446 }
2447 }
2448
2449 void Simulator::disableStop(uint32_t code) {
2450 if (isEnabledStop(code)) {
2451 watchedStops_[code].count_ |= kStopDisabledBit;
2452 }
2453 }
2454
2455 void Simulator::increaseStopCounter(uint32_t code) {
2456 MOZ_ASSERT(code <= kMaxStopCode);
2457 if ((watchedStops_[code].count_ & ~(1 << 31)) == 0x7fffffff) {
2458 printf(
2459 "Stop counter for code %i has overflowed.\n"
2460 "Enabling this code and reseting the counter to 0.\n",
2461 code);
2462 watchedStops_[code].count_ = 0;
2463 enableStop(code);
2464 } else {
2465 watchedStops_[code].count_++;
2466 }
2467 }
2468
2469 // Print a stop status.
2470 void Simulator::printStopInfo(uint32_t code) {
2471 if (code <= kMaxWatchpointCode) {
2472 printf("That is a watchpoint, not a stop.\n");
2473 return;
2474 } else if (code > kMaxStopCode) {
2475 printf("Code too large, only %u stops can be used\n", kMaxStopCode + 1);
2476 return;
2477 }
2478 const char* state = isEnabledStop(code) ? "Enabled" : "Disabled";
2479 int32_t count = watchedStops_[code].count_ & ~kStopDisabledBit;
2480 // Don't print the state of unused breakpoints.
2481 if (count != 0) {
2482 if (watchedStops_[code].desc_) {
2483 printf("stop %i - 0x%x: \t%s, \tcounter = %i, \t%s\n", code, code, state,
2484 count, watchedStops_[code].desc_);
2485 } else {
2486 printf("stop %i - 0x%x: \t%s, \tcounter = %i\n", code, code, state,
2487 count);
2488 }
2489 }
2490 }
2491
2492 void Simulator::signalExceptions() {
2493 for (int i = 1; i < kNumExceptions; i++) {
2494 if (exceptions[i] != 0) {
2495 MOZ_CRASH("Error: Exception raised.");
2496 }
2497 }
2498 }
2499
2500 // ReverseBits(value) returns |value| in reverse bit order.
2501 template <typename T>
2502 T ReverseBits(T value) {
2503 MOZ_ASSERT((sizeof(value) == 1) || (sizeof(value) == 2) ||
2504 (sizeof(value) == 4) || (sizeof(value) == 8));
2505 T result = 0;
2506 for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
2507 result = (result << 1) | (value & 1);
2508 value >>= 1;
2509 }
2510 return result;
2511 }
2512
2513 // Min/Max template functions for Double and Single arguments.
2514
2515 template <typename T>
2516 static T FPAbs(T a);
2517
2518 template <>
2519 double FPAbs<double>(double a) {
2520 return fabs(a);
2521 }
2522
2523 template <>
2524 float FPAbs<float>(float a) {
2525 return fabsf(a);
2526 }
2527
2528 enum class MaxMinKind : int { kMin = 0, kMax = 1 };
2529
2530 template <typename T>
2531 static bool FPUProcessNaNsAndZeros(T a, T b, MaxMinKind kind, T* result) {
2532 if (std::isnan(a) && std::isnan(b)) {
2533 *result = a;
2534 } else if (std::isnan(a)) {
2535 *result = b;
2536 } else if (std::isnan(b)) {
2537 *result = a;
2538 } else if (b == a) {
2539 // Handle -0.0 == 0.0 case.
2540 // std::signbit() returns int 0 or 1 so subtracting MaxMinKind::kMax
2541 // negates the result.
2542 *result = std::signbit(b) - static_cast<int>(kind) ? b : a;
2543 } else {
2544 return false;
2545 }
2546 return true;
2547 }
2548
2549 template <typename T>
2550 static T FPUMin(T a, T b) {
2551 T result;
2552 if (FPUProcessNaNsAndZeros(a, b, MaxMinKind::kMin, &result)) {
2553 return result;
2554 } else {
2555 return b < a ? b : a;
2556 }
2557 }
2558
2559 template <typename T>
2560 static T FPUMax(T a, T b) {
2561 T result;
2562 if (FPUProcessNaNsAndZeros(a, b, MaxMinKind::kMax, &result)) {
2563 return result;
2564 } else {
2565 return b > a ? b : a;
2566 }
2567 }
2568
2569 template <typename T>
2570 static T FPUMinA(T a, T b) {
2571 T result;
2572 if (!FPUProcessNaNsAndZeros(a, b, MaxMinKind::kMin, &result)) {
2573 if (FPAbs(a) < FPAbs(b)) {
2574 result = a;
2575 } else if (FPAbs(b) < FPAbs(a)) {
2576 result = b;
2577 } else {
2578 result = a < b ? a : b;
2579 }
2580 }
2581 return result;
2582 }
2583
2584 template <typename T>
2585 static T FPUMaxA(T a, T b) {
2586 T result;
2587 if (!FPUProcessNaNsAndZeros(a, b, MaxMinKind::kMin, &result)) {
2588 if (FPAbs(a) > FPAbs(b)) {
2589 result = a;
2590 } else if (FPAbs(b) > FPAbs(a)) {
2591 result = b;
2592 } else {
2593 result = a > b ? a : b;
2594 }
2595 }
2596 return result;
2597 }
2598
2599 enum class KeepSign : bool { no = false, yes };
2600
2601 // Handle execution based on instruction types.
2602 // decodeTypeImmediate
2603 void Simulator::decodeTypeOp6(SimInstruction* instr) {
2604 // Next pc.
2605 int64_t next_pc = bad_ra;
2606
2607 // Used for memory instructions.
2608 int64_t alu_out = 0;
2609
2610 // Branch instructions common part.
2611 auto BranchAndLinkHelper = [this, &next_pc](SimInstruction* instr) {
2612 int64_t current_pc = get_pc();
2613 setRegister(ra, current_pc + SimInstruction::kInstrSize);
2614 int32_t offs26_low16 =
2615 static_cast<uint32_t>(instr->bits(25, 10) << 16) >> 16;
2616 int32_t offs26_high10 = static_cast<int32_t>(instr->bits(9, 0) << 22) >> 6;
2617 int32_t offs26 = offs26_low16 | offs26_high10;
2618 next_pc = current_pc + (offs26 << 2);
2619 set_pc(next_pc);
2620 };
2621
2622 auto BranchOff16Helper = [this, &next_pc](SimInstruction* instr,
2623 bool do_branch) {
2624 int64_t current_pc = get_pc();
2625 int32_t offs16 = static_cast<int32_t>(instr->bits(25, 10) << 16) >> 16;
2626 int32_t offs = do_branch ? (offs16 << 2) : SimInstruction::kInstrSize;
2627 next_pc = current_pc + offs;
2628 set_pc(next_pc);
2629 };
2630
2631 auto BranchOff21Helper = [this, &next_pc](SimInstruction* instr,
2632 bool do_branch) {
2633 int64_t current_pc = get_pc();
2634 int32_t offs21_low16 =
2635 static_cast<uint32_t>(instr->bits(25, 10) << 16) >> 16;
2636 int32_t offs21_high5 = static_cast<int32_t>(instr->bits(4, 0) << 27) >> 11;
2637 int32_t offs = offs21_low16 | offs21_high5;
2638 offs = do_branch ? (offs << 2) : SimInstruction::kInstrSize;
2639 next_pc = current_pc + offs;
2640 set_pc(next_pc);
2641 };
2642
2643 auto BranchOff26Helper = [this, &next_pc](SimInstruction* instr) {
2644 int64_t current_pc = get_pc();
2645 int32_t offs26_low16 =
2646 static_cast<uint32_t>(instr->bits(25, 10) << 16) >> 16;
2647 int32_t offs26_high10 = static_cast<int32_t>(instr->bits(9, 0) << 22) >> 6;
2648 int32_t offs26 = offs26_low16 | offs26_high10;
2649 next_pc = current_pc + (offs26 << 2);
2650 set_pc(next_pc);
2651 };
2652
2653 auto JumpOff16Helper = [this, &next_pc](SimInstruction* instr) {
2654 int32_t offs16 = static_cast<int32_t>(instr->bits(25, 10) << 16) >> 16;
2655 setRegister(rd_reg(instr), get_pc() + SimInstruction::kInstrSize);
2656 next_pc = rj(instr) + (offs16 << 2);
2657 set_pc(next_pc);
2658 };
2659
2660 switch (instr->bits(31, 26) << 26) {
2661 case op_addu16i_d: {
2662 int32_t si16_upper = static_cast<int32_t>(si16(instr)) << 16;
2663 alu_out = static_cast<int64_t>(si16_upper) + rj(instr);
2664 setRegister(rd_reg(instr), alu_out);
2665 break;
2666 }
2667 case op_beqz: {
2668 BranchOff21Helper(instr, rj(instr) == 0);
2669 break;
2670 }
2671 case op_bnez: {
2672 BranchOff21Helper(instr, rj(instr) != 0);
2673 break;
2674 }
2675 case op_bcz: {
2676 if (instr->bits(9, 8) == 0b00) {
2677 // BCEQZ
2678 BranchOff21Helper(instr, cj(instr) == false);
2679 } else if (instr->bits(9, 8) == 0b01) {
2680 // BCNEZ
2681 BranchOff21Helper(instr, cj(instr) == true);
2682 } else {
2683 UNREACHABLE();
2684 }
2685 break;
2686 }
2687 case op_jirl: {
2688 JumpOff16Helper(instr);
2689 break;
2690 }
2691 case op_b: {
2692 BranchOff26Helper(instr);
2693 break;
2694 }
2695 case op_bl: {
2696 BranchAndLinkHelper(instr);
2697 break;
2698 }
2699 case op_beq: {
2700 BranchOff16Helper(instr, rj(instr) == rd(instr));
2701 break;
2702 }
2703 case op_bne: {
2704 BranchOff16Helper(instr, rj(instr) != rd(instr));
2705 break;
2706 }
2707 case op_blt: {
2708 BranchOff16Helper(instr, rj(instr) < rd(instr));
2709 break;
2710 }
2711 case op_bge: {
2712 BranchOff16Helper(instr, rj(instr) >= rd(instr));
2713 break;
2714 }
2715 case op_bltu: {
2716 BranchOff16Helper(instr, rj_u(instr) < rd_u(instr));
2717 break;
2718 }
2719 case op_bgeu: {
2720 BranchOff16Helper(instr, rj_u(instr) >= rd_u(instr));
2721 break;
2722 }
2723 default:
2724 UNREACHABLE();
2725 }
2726 }
2727
2728 void Simulator::decodeTypeOp7(SimInstruction* instr) {
2729 int64_t alu_out;
2730
2731 switch (instr->bits(31, 25) << 25) {
2732 case op_lu12i_w: {
2733 int32_t si20_upper = static_cast<int32_t>(si20(instr) << 12);
2734 setRegister(rd_reg(instr), static_cast<int64_t>(si20_upper));
2735 break;
2736 }
2737 case op_lu32i_d: {
2738 int32_t si20_signExtend = static_cast<int32_t>(si20(instr) << 12) >> 12;
2739 int64_t lower_32bit_mask = 0xFFFFFFFF;
2740 alu_out = (static_cast<int64_t>(si20_signExtend) << 32) |
2741 (rd(instr) & lower_32bit_mask);
2742 setRegister(rd_reg(instr), alu_out);
2743 break;
2744 }
2745 case op_pcaddi: {
2746 int32_t si20_signExtend = static_cast<int32_t>(si20(instr) << 12) >> 10;
2747 int64_t current_pc = get_pc();
2748 alu_out = static_cast<int64_t>(si20_signExtend) + current_pc;
2749 setRegister(rd_reg(instr), alu_out);
2750 break;
2751 }
2752 case op_pcalau12i: {
2753 int32_t si20_signExtend = static_cast<int32_t>(si20(instr) << 12);
2754 int64_t current_pc = get_pc();
2755 int64_t clear_lower12bit_mask = 0xFFFFFFFFFFFFF000;
2756 alu_out = static_cast<int64_t>(si20_signExtend) + current_pc;
2757 setRegister(rd_reg(instr), alu_out & clear_lower12bit_mask);
2758 break;
2759 }
2760 case op_pcaddu12i: {
2761 int32_t si20_signExtend = static_cast<int32_t>(si20(instr) << 12);
2762 int64_t current_pc = get_pc();
2763 alu_out = static_cast<int64_t>(si20_signExtend) + current_pc;
2764 setRegister(rd_reg(instr), alu_out);
2765 break;
2766 }
2767 case op_pcaddu18i: {
2768 int64_t si20_signExtend = (static_cast<int64_t>(si20(instr)) << 44) >> 26;
2769 int64_t current_pc = get_pc();
2770 alu_out = si20_signExtend + current_pc;
2771 setRegister(rd_reg(instr), alu_out);
2772 break;
2773 }
2774 default:
2775 UNREACHABLE();
2776 }
2777 }
2778
2779 void Simulator::decodeTypeOp8(SimInstruction* instr) {
2780 int64_t addr = 0x0;
2781 int64_t si14_se = (static_cast<int64_t>(si14(instr)) << 50) >> 48;
2782
2783 switch (instr->bits(31, 24) << 24) {
2784 case op_ldptr_w: {
2785 setRegister(rd_reg(instr), readW(rj(instr) + si14_se, instr));
2786 break;
2787 }
2788 case op_stptr_w: {
2789 writeW(rj(instr) + si14_se, static_cast<int32_t>(rd(instr)), instr);
2790 break;
2791 }
2792 case op_ldptr_d: {
2793 setRegister(rd_reg(instr), readDW(rj(instr) + si14_se, instr));
2794 break;
2795 }
2796 case op_stptr_d: {
2797 writeDW(rj(instr) + si14_se, rd(instr), instr);
2798 break;
2799 }
2800 case op_ll_w: {
2801 addr = si14_se + rj(instr);
2802 setRegister(rd_reg(instr), loadLinkedW(addr, instr));
2803 break;
2804 }
2805 case op_sc_w: {
2806 addr = si14_se + rj(instr);
2807 setRegister(
2808 rd_reg(instr),
2809 storeConditionalW(addr, static_cast<int32_t>(rd(instr)), instr));
2810 break;
2811 }
2812 case op_ll_d: {
2813 addr = si14_se + rj(instr);
2814 setRegister(rd_reg(instr), loadLinkedD(addr, instr));
2815 break;
2816 }
2817 case op_sc_d: {
2818 addr = si14_se + rj(instr);
2819 setRegister(rd_reg(instr), storeConditionalD(addr, rd(instr), instr));
2820 break;
2821 }
2822 default:
2823 UNREACHABLE();
2824 }
2825 }
2826
2827 void Simulator::decodeTypeOp10(SimInstruction* instr) {
2828 int64_t alu_out = 0x0;
2829 int64_t si12_se = (static_cast<int64_t>(si12(instr)) << 52) >> 52;
2830 uint64_t si12_ze = (static_cast<uint64_t>(ui12(instr)) << 52) >> 52;
2831
2832 switch (instr->bits(31, 22) << 22) {
2833 case op_bstrins_d: {
2834 uint8_t lsbd_ = lsbd(instr);
2835 uint8_t msbd_ = msbd(instr);
2836 MOZ_ASSERT(lsbd_ <= msbd_);
2837 uint8_t size = msbd_ - lsbd_ + 1;
2838 if (size < 64) {
2839 uint64_t mask = (1ULL << size) - 1;
2840 alu_out =
2841 (rd_u(instr) & ~(mask << lsbd_)) | ((rj_u(instr) & mask) << lsbd_);
2842 setRegister(rd_reg(instr), alu_out);
2843 } else if (size == 64) {
2844 setRegister(rd_reg(instr), rj(instr));
2845 }
2846 break;
2847 }
2848 case op_bstrpick_d: {
2849 uint8_t lsbd_ = lsbd(instr);
2850 uint8_t msbd_ = msbd(instr);
2851 MOZ_ASSERT(lsbd_ <= msbd_);
2852 uint8_t size = msbd_ - lsbd_ + 1;
2853 if (size < 64) {
2854 uint64_t mask = (1ULL << size) - 1;
2855 alu_out = (rj_u(instr) & (mask << lsbd_)) >> lsbd_;
2856 setRegister(rd_reg(instr), alu_out);
2857 } else if (size == 64) {
2858 setRegister(rd_reg(instr), rj(instr));
2859 }
2860 break;
2861 }
2862 case op_slti: {
2863 setRegister(rd_reg(instr), rj(instr) < si12_se ? 1 : 0);
2864 break;
2865 }
2866 case op_sltui: {
2867 setRegister(rd_reg(instr),
2868 rj_u(instr) < static_cast<uint64_t>(si12_se) ? 1 : 0);
2869 break;
2870 }
2871 case op_addi_w: {
2872 int32_t alu32_out =
2873 static_cast<int32_t>(rj(instr)) + static_cast<int32_t>(si12_se);
2874 setRegister(rd_reg(instr), alu32_out);
2875 break;
2876 }
2877 case op_addi_d: {
2878 setRegister(rd_reg(instr), rj(instr) + si12_se);
2879 break;
2880 }
2881 case op_lu52i_d: {
2882 int64_t si12_se = static_cast<int64_t>(si12(instr)) << 52;
2883 uint64_t mask = (1ULL << 52) - 1;
2884 alu_out = si12_se + (rj(instr) & mask);
2885 setRegister(rd_reg(instr), alu_out);
2886 break;
2887 }
2888 case op_andi: {
2889 setRegister(rd_reg(instr), rj(instr) & si12_ze);
2890 break;
2891 }
2892 case op_ori: {
2893 setRegister(rd_reg(instr), rj_u(instr) | si12_ze);
2894 break;
2895 }
2896 case op_xori: {
2897 setRegister(rd_reg(instr), rj_u(instr) ^ si12_ze);
2898 break;
2899 }
2900 case op_ld_b: {
2901 setRegister(rd_reg(instr), readB(rj(instr) + si12_se));
2902 break;
2903 }
2904 case op_ld_h: {
2905 setRegister(rd_reg(instr), readH(rj(instr) + si12_se, instr));
2906 break;
2907 }
2908 case op_ld_w: {
2909 setRegister(rd_reg(instr), readW(rj(instr) + si12_se, instr));
2910 break;
2911 }
2912 case op_ld_d: {
2913 setRegister(rd_reg(instr), readDW(rj(instr) + si12_se, instr));
2914 break;
2915 }
2916 case op_st_b: {
2917 writeB(rj(instr) + si12_se, static_cast<int8_t>(rd(instr)));
2918 break;
2919 }
2920 case op_st_h: {
2921 writeH(rj(instr) + si12_se, static_cast<int16_t>(rd(instr)), instr);
2922 break;
2923 }
2924 case op_st_w: {
2925 writeW(rj(instr) + si12_se, static_cast<int32_t>(rd(instr)), instr);
2926 break;
2927 }
2928 case op_st_d: {
2929 writeDW(rj(instr) + si12_se, rd(instr), instr);
2930 break;
2931 }
2932 case op_ld_bu: {
2933 setRegister(rd_reg(instr), readBU(rj(instr) + si12_se));
2934 break;
2935 }
2936 case op_ld_hu: {
2937 setRegister(rd_reg(instr), readHU(rj(instr) + si12_se, instr));
2938 break;
2939 }
2940 case op_ld_wu: {
2941 setRegister(rd_reg(instr), readWU(rj(instr) + si12_se, instr));
2942 break;
2943 }
2944 case op_fld_s: {
2945 setFpuRegister(fd_reg(instr), kFPUInvalidResult); // Trash upper 32 bits.
2946 setFpuRegisterWord(fd_reg(instr), readW(rj(instr) + si12_se, instr));
2947 break;
2948 }
2949 case op_fst_s: {
2950 int32_t alu_out_32 = static_cast<int32_t>(getFpuRegister(fd_reg(instr)));
2951 writeW(rj(instr) + si12_se, alu_out_32, instr);
2952 break;
2953 }
2954 case op_fld_d: {
2955 setFpuRegisterDouble(fd_reg(instr), readD(rj(instr) + si12_se, instr));
2956 break;
2957 }
2958 case op_fst_d: {
2959 writeD(rj(instr) + si12_se, getFpuRegisterDouble(fd_reg(instr)), instr);
2960 break;
2961 }
2962 case op_preld:
2963 UNIMPLEMENTED();
2964 break;
2965 default:
2966 UNREACHABLE();
2967 }
2968 }
2969
2970 void Simulator::decodeTypeOp11(SimInstruction* instr) {
2971 int64_t alu_out = 0x0;
2972
2973 switch (instr->bits(31, 21) << 21) {
2974 case op_bstr_w: {
2975 MOZ_ASSERT(instr->bit(21) == 1);
2976 uint8_t lsbw_ = lsbw(instr);
2977 uint8_t msbw_ = msbw(instr);
2978 MOZ_ASSERT(lsbw_ <= msbw_);
2979 uint8_t size = msbw_ - lsbw_ + 1;
2980 uint64_t mask = (1ULL << size) - 1;
2981 if (instr->bit(15) == 0) {
2982 // BSTRINS_W
2983 alu_out = static_cast<int32_t>((rd_u(instr) & ~(mask << lsbw_)) |
2984 ((rj_u(instr) & mask) << lsbw_));
2985 } else {
2986 // BSTRPICK_W
2987 alu_out =
2988 static_cast<int32_t>((rj_u(instr) & (mask << lsbw_)) >> lsbw_);
2989 }
2990 setRegister(rd_reg(instr), alu_out);
2991 break;
2992 }
2993 default:
2994 UNREACHABLE();
2995 }
2996 }
2997
2998 void Simulator::decodeTypeOp12(SimInstruction* instr) {
2999 switch (instr->bits(31, 20) << 20) {
3000 case op_fmadd_s: {
3001 setFpuRegisterFloat(
3002 fd_reg(instr),
3003 std::fma(fj_float(instr), fk_float(instr), fa_float(instr)));
3004 break;
3005 }
3006 case op_fmadd_d: {
3007 setFpuRegisterDouble(
3008 fd_reg(instr),
3009 std::fma(fj_double(instr), fk_double(instr), fa_double(instr)));
3010 break;
3011 }
3012 case op_fmsub_s: {
3013 setFpuRegisterFloat(
3014 fd_reg(instr),
3015 std::fma(-fj_float(instr), fk_float(instr), fa_float(instr)));
3016 break;
3017 }
3018 case op_fmsub_d: {
3019 setFpuRegisterDouble(
3020 fd_reg(instr),
3021 std::fma(-fj_double(instr), fk_double(instr), fa_double(instr)));
3022 break;
3023 }
3024 case op_fnmadd_s: {
3025 setFpuRegisterFloat(
3026 fd_reg(instr),
3027 std::fma(-fj_float(instr), fk_float(instr), -fa_float(instr)));
3028 break;
3029 }
3030 case op_fnmadd_d: {
3031 setFpuRegisterDouble(
3032 fd_reg(instr),
3033 std::fma(-fj_double(instr), fk_double(instr), -fa_double(instr)));
3034 break;
3035 }
3036 case op_fnmsub_s: {
3037 setFpuRegisterFloat(
3038 fd_reg(instr),
3039 std::fma(fj_float(instr), fk_float(instr), -fa_float(instr)));
3040 break;
3041 }
3042 case op_fnmsub_d: {
3043 setFpuRegisterDouble(
3044 fd_reg(instr),
3045 std::fma(fj_double(instr), fk_double(instr), -fa_double(instr)));
3046 break;
3047 }
3048 case op_fcmp_cond_s: {
3049 MOZ_ASSERT(instr->bits(4, 3) == 0);
3050 float fj = fj_float(instr);
3051 float fk = fk_float(instr);
3052 switch (cond(instr)) {
3053 case AssemblerLOONG64::CAF: {
3054 setCFRegister(cd_reg(instr), false);
3055 break;
3056 }
3057 case AssemblerLOONG64::CUN: {
3058 setCFRegister(cd_reg(instr), std::isnan(fj) || std::isnan(fk));
3059 break;
3060 }
3061 case AssemblerLOONG64::CEQ: {
3062 setCFRegister(cd_reg(instr), fj == fk);
3063 break;
3064 }
3065 case AssemblerLOONG64::CUEQ: {
3066 setCFRegister(cd_reg(instr),
3067 (fj == fk) || std::isnan(fj) || std::isnan(fk));
3068 break;
3069 }
3070 case AssemblerLOONG64::CLT: {
3071 setCFRegister(cd_reg(instr), fj < fk);
3072 break;
3073 }
3074 case AssemblerLOONG64::CULT: {
3075 setCFRegister(cd_reg(instr),
3076 (fj < fk) || std::isnan(fj) || std::isnan(fk));
3077 break;
3078 }
3079 case AssemblerLOONG64::CLE: {
3080 setCFRegister(cd_reg(instr), fj <= fk);
3081 break;
3082 }
3083 case AssemblerLOONG64::CULE: {
3084 setCFRegister(cd_reg(instr),
3085 (fj <= fk) || std::isnan(fj) || std::isnan(fk));
3086 break;
3087 }
3088 case AssemblerLOONG64::CNE: {
3089 setCFRegister(cd_reg(instr), (fj < fk) || (fj > fk));
3090 break;
3091 }
3092 case AssemblerLOONG64::COR: {
3093 setCFRegister(cd_reg(instr), !std::isnan(fj) && !std::isnan(fk));
3094 break;
3095 }
3096 case AssemblerLOONG64::CUNE: {
3097 setCFRegister(cd_reg(instr), (fj < fk) || (fj > fk) ||
3098 std::isnan(fj) || std::isnan(fk));
3099 break;
3100 }
3101 case AssemblerLOONG64::SAF:
3102 UNIMPLEMENTED();
3103 break;
3104 case AssemblerLOONG64::SUN:
3105 UNIMPLEMENTED();
3106 break;
3107 case AssemblerLOONG64::SEQ:
3108 UNIMPLEMENTED();
3109 break;
3110 case AssemblerLOONG64::SUEQ:
3111 UNIMPLEMENTED();
3112 break;
3113 case AssemblerLOONG64::SLT:
3114 UNIMPLEMENTED();
3115 break;
3116 case AssemblerLOONG64::SULT:
3117 UNIMPLEMENTED();
3118 break;
3119 case AssemblerLOONG64::SLE:
3120 UNIMPLEMENTED();
3121 break;
3122 case AssemblerLOONG64::SULE:
3123 UNIMPLEMENTED();
3124 break;
3125 case AssemblerLOONG64::SNE:
3126 UNIMPLEMENTED();
3127 break;
3128 case AssemblerLOONG64::SOR:
3129 UNIMPLEMENTED();
3130 break;
3131 case AssemblerLOONG64::SUNE:
3132 UNIMPLEMENTED();
3133 break;
3134 default:
3135 UNREACHABLE();
3136 }
3137 break;
3138 }
3139 case op_fcmp_cond_d: {
3140 MOZ_ASSERT(instr->bits(4, 3) == 0);
3141 double fj = fj_double(instr);
3142 double fk = fk_double(instr);
3143 switch (cond(instr)) {
3144 case AssemblerLOONG64::CAF: {
3145 setCFRegister(cd_reg(instr), false);
3146 break;
3147 }
3148 case AssemblerLOONG64::CUN: {
3149 setCFRegister(cd_reg(instr), std::isnan(fj) || std::isnan(fk));
3150 break;
3151 }
3152 case AssemblerLOONG64::CEQ: {
3153 setCFRegister(cd_reg(instr), fj == fk);
3154 break;
3155 }
3156 case AssemblerLOONG64::CUEQ: {
3157 setCFRegister(cd_reg(instr),
3158 (fj == fk) || std::isnan(fj) || std::isnan(fk));
3159 break;
3160 }
3161 case AssemblerLOONG64::CLT: {
3162 setCFRegister(cd_reg(instr), fj < fk);
3163 break;
3164 }
3165 case AssemblerLOONG64::CULT: {
3166 setCFRegister(cd_reg(instr),
3167 (fj < fk) || std::isnan(fj) || std::isnan(fk));
3168 break;
3169 }
3170 case AssemblerLOONG64::CLE: {
3171 setCFRegister(cd_reg(instr), fj <= fk);
3172 break;
3173 }
3174 case AssemblerLOONG64::CULE: {
3175 setCFRegister(cd_reg(instr),
3176 (fj <= fk) || std::isnan(fj) || std::isnan(fk));
3177 break;
3178 }
3179 case AssemblerLOONG64::CNE: {
3180 setCFRegister(cd_reg(instr), (fj < fk) || (fj > fk));
3181 break;
3182 }
3183 case AssemblerLOONG64::COR: {
3184 setCFRegister(cd_reg(instr), !std::isnan(fj) && !std::isnan(fk));
3185 break;
3186 }
3187 case AssemblerLOONG64::CUNE: {
3188 setCFRegister(cd_reg(instr),
3189 (fj != fk) || std::isnan(fj) || std::isnan(fk));
3190 break;
3191 }
3192 case AssemblerLOONG64::SAF:
3193 UNIMPLEMENTED();
3194 break;
3195 case AssemblerLOONG64::SUN:
3196 UNIMPLEMENTED();
3197 break;
3198 case AssemblerLOONG64::SEQ:
3199 UNIMPLEMENTED();
3200 break;
3201 case AssemblerLOONG64::SUEQ:
3202 UNIMPLEMENTED();
3203 break;
3204 case AssemblerLOONG64::SLT:
3205 UNIMPLEMENTED();
3206 break;
3207 case AssemblerLOONG64::SULT:
3208 UNIMPLEMENTED();
3209 break;
3210 case AssemblerLOONG64::SLE:
3211 UNIMPLEMENTED();
3212 break;
3213 case AssemblerLOONG64::SULE:
3214 UNIMPLEMENTED();
3215 break;
3216 case AssemblerLOONG64::SNE:
3217 UNIMPLEMENTED();
3218 break;
3219 case AssemblerLOONG64::SOR:
3220 UNIMPLEMENTED();
3221 break;
3222 case AssemblerLOONG64::SUNE:
3223 UNIMPLEMENTED();
3224 break;
3225 default:
3226 UNREACHABLE();
3227 }
3228 break;
3229 }
3230 default:
3231 UNREACHABLE();
3232 }
3233 }
3234
3235 void Simulator::decodeTypeOp14(SimInstruction* instr) {
3236 int64_t alu_out = 0x0;
3237
3238 switch (instr->bits(31, 18) << 18) {
3239 case op_bytepick_d: {
3240 uint8_t sa = sa3(instr) * 8;
3241 if (sa == 0) {
3242 alu_out = rk(instr);
3243 } else {
3244 int64_t mask = (1ULL << 63) >> (sa - 1);
3245 int64_t rk_hi = (rk(instr) & (~mask)) << sa;
3246 int64_t rj_lo = (rj(instr) & mask) >> (64 - sa);
3247 alu_out = rk_hi | rj_lo;
3248 }
3249 setRegister(rd_reg(instr), alu_out);
3250 break;
3251 }
3252 case op_fsel: {
3253 MOZ_ASSERT(instr->bits(19, 18) == 0);
3254 if (ca(instr) == 0) {
3255 setFpuRegisterDouble(fd_reg(instr), fj_double(instr));
3256 } else {
3257 setFpuRegisterDouble(fd_reg(instr), fk_double(instr));
3258 }
3259 break;
3260 }
3261 default:
3262 UNREACHABLE();
3263 }
3264 }
3265
3266 void Simulator::decodeTypeOp15(SimInstruction* instr) {
3267 int64_t alu_out = 0x0;
3268 int32_t alu32_out = 0x0;
3269
3270 switch (instr->bits(31, 17) << 17) {
3271 case op_bytepick_w: {
3272 MOZ_ASSERT(instr->bit(17) == 0);
3273 uint8_t sa = sa2(instr) * 8;
3274 if (sa == 0) {
3275 alu32_out = static_cast<int32_t>(rk(instr));
3276 } else {
3277 int32_t mask = (1 << 31) >> (sa - 1);
3278 int32_t rk_hi = (static_cast<int32_t>(rk(instr)) & (~mask)) << sa;
3279 int32_t rj_lo = (static_cast<uint32_t>(rj(instr)) & mask) >> (32 - sa);
3280 alu32_out = rk_hi | rj_lo;
3281 }
3282 setRegister(rd_reg(instr), static_cast<int64_t>(alu32_out));
3283 break;
3284 }
3285 case op_alsl_w: {
3286 uint8_t sa = sa2(instr) + 1;
3287 alu32_out = (static_cast<int32_t>(rj(instr)) << sa) +
3288 static_cast<int32_t>(rk(instr));
3289 setRegister(rd_reg(instr), alu32_out);
3290 break;
3291 }
3292 case op_alsl_wu: {
3293 uint8_t sa = sa2(instr) + 1;
3294 alu32_out = (static_cast<int32_t>(rj(instr)) << sa) +
3295 static_cast<int32_t>(rk(instr));
3296 setRegister(rd_reg(instr), static_cast<uint32_t>(alu32_out));
3297 break;
3298 }
3299 case op_alsl_d: {
3300 MOZ_ASSERT(instr->bit(17) == 0);
3301 uint8_t sa = sa2(instr) + 1;
3302 alu_out = (rj(instr) << sa) + rk(instr);
3303 setRegister(rd_reg(instr), alu_out);
3304 break;
3305 }
3306 default:
3307 UNREACHABLE();
3308 }
3309 }
3310
3311 void Simulator::decodeTypeOp16(SimInstruction* instr) {
3312 int64_t alu_out;
3313 switch (instr->bits(31, 16) << 16) {
3314 case op_slli_d: {
3315 MOZ_ASSERT(instr->bit(17) == 0);
3316 MOZ_ASSERT(instr->bits(17, 16) == 0b01);
3317 setRegister(rd_reg(instr), rj(instr) << ui6(instr));
3318 break;
3319 }
3320 case op_srli_d: {
3321 MOZ_ASSERT(instr->bit(17) == 0);
3322 setRegister(rd_reg(instr), rj_u(instr) >> ui6(instr));
3323 break;
3324 }
3325 case op_srai_d: {
3326 MOZ_ASSERT(instr->bit(17) == 0);
3327 setRegister(rd_reg(instr), rj(instr) >> ui6(instr));
3328 break;
3329 }
3330 case op_rotri_d: {
3331 MOZ_ASSERT(instr->bit(17) == 0);
3332 MOZ_ASSERT(instr->bits(17, 16) == 0b01);
3333 alu_out = static_cast<int64_t>(RotateRight64(rj_u(instr), ui6(instr)));
3334 setRegister(rd_reg(instr), alu_out);
3335 break;
3336 }
3337 default:
3338 UNREACHABLE();
3339 }
3340 }
3341
3342 void Simulator::decodeTypeOp17(SimInstruction* instr) {
3343 int64_t alu_out;
3344 int32_t alu32_out;
3345
3346 switch (instr->bits(31, 15) << 15) {
3347 case op_slli_w: {
3348 MOZ_ASSERT(instr->bit(17) == 0);
3349 MOZ_ASSERT(instr->bits(17, 15) == 0b001);
3350 alu32_out = static_cast<int32_t>(rj(instr)) << ui5(instr);
3351 setRegister(rd_reg(instr), static_cast<int64_t>(alu32_out));
3352 break;
3353 }
3354 case op_srai_w: {
3355 MOZ_ASSERT(instr->bit(17) == 0);
3356 MOZ_ASSERT(instr->bits(17, 15) == 0b001);
3357 alu32_out = static_cast<int32_t>(rj(instr)) >> ui5(instr);
3358 setRegister(rd_reg(instr), static_cast<int64_t>(alu32_out));
3359 break;
3360 }
3361 case op_rotri_w: {
3362 MOZ_ASSERT(instr->bit(17) == 0);
3363 MOZ_ASSERT(instr->bits(17, 15) == 0b001);
3364 alu32_out = static_cast<int32_t>(
3365 RotateRight32(static_cast<const uint32_t>(rj_u(instr)),
3366 static_cast<const uint32_t>(ui5(instr))));
3367 setRegister(rd_reg(instr), static_cast<int64_t>(alu32_out));
3368 break;
3369 }
3370 case op_srli_w: {
3371 MOZ_ASSERT(instr->bit(17) == 0);
3372 MOZ_ASSERT(instr->bits(17, 15) == 0b001);
3373 alu32_out = static_cast<uint32_t>(rj(instr)) >> ui5(instr);
3374 setRegister(rd_reg(instr), static_cast<int64_t>(alu32_out));
3375 break;
3376 }
3377 case op_add_w: {
3378 int32_t alu32_out = static_cast<int32_t>(rj(instr) + rk(instr));
3379 // Sign-extend result of 32bit operation into 64bit register.
3380 setRegister(rd_reg(instr), static_cast<int64_t>(alu32_out));
3381 break;
3382 }
3383 case op_add_d:
3384 setRegister(rd_reg(instr), rj(instr) + rk(instr));
3385 break;
3386 case op_sub_w: {
3387 int32_t alu32_out = static_cast<int32_t>(rj(instr) - rk(instr));
3388 // Sign-extend result of 32bit operation into 64bit register.
3389 setRegister(rd_reg(instr), static_cast<int64_t>(alu32_out));
3390 break;
3391 }
3392 case op_sub_d:
3393 setRegister(rd_reg(instr), rj(instr) - rk(instr));
3394 break;
3395 case op_slt:
3396 setRegister(rd_reg(instr), rj(instr) < rk(instr) ? 1 : 0);
3397 break;
3398 case op_sltu:
3399 setRegister(rd_reg(instr), rj_u(instr) < rk_u(instr) ? 1 : 0);
3400 break;
3401 case op_maskeqz:
3402 setRegister(rd_reg(instr), rk(instr) == 0 ? 0 : rj(instr));
3403 break;
3404 case op_masknez:
3405 setRegister(rd_reg(instr), rk(instr) != 0 ? 0 : rj(instr));
3406 break;
3407 case op_nor:
3408 setRegister(rd_reg(instr), ~(rj(instr) | rk(instr)));
3409 break;
3410 case op_and:
3411 setRegister(rd_reg(instr), rj(instr) & rk(instr));
3412 break;
3413 case op_or:
3414 setRegister(rd_reg(instr), rj(instr) | rk(instr));
3415 break;
3416 case op_xor:
3417 setRegister(rd_reg(instr), rj(instr) ^ rk(instr));
3418 break;
3419 case op_orn:
3420 setRegister(rd_reg(instr), rj(instr) | (~rk(instr)));
3421 break;
3422 case op_andn:
3423 setRegister(rd_reg(instr), rj(instr) & (~rk(instr)));
3424 break;
3425 case op_sll_w:
3426 setRegister(rd_reg(instr), (int32_t)rj(instr) << (rk_u(instr) % 32));
3427 break;
3428 case op_srl_w: {
3429 alu_out =
3430 static_cast<int32_t>((uint32_t)rj_u(instr) >> (rk_u(instr) % 32));
3431 setRegister(rd_reg(instr), alu_out);
3432 break;
3433 }
3434 case op_sra_w:
3435 setRegister(rd_reg(instr), (int32_t)rj(instr) >> (rk_u(instr) % 32));
3436 break;
3437 case op_sll_d:
3438 setRegister(rd_reg(instr), rj(instr) << (rk_u(instr) % 64));
3439 break;
3440 case op_srl_d: {
3441 alu_out = static_cast<int64_t>(rj_u(instr) >> (rk_u(instr) % 64));
3442 setRegister(rd_reg(instr), alu_out);
3443 break;
3444 }
3445 case op_sra_d:
3446 setRegister(rd_reg(instr), rj(instr) >> (rk_u(instr) % 64));
3447 break;
3448 case op_rotr_w: {
3449 alu_out = static_cast<int32_t>(
3450 RotateRight32(static_cast<const uint32_t>(rj_u(instr)),
3451 static_cast<const uint32_t>(rk_u(instr) % 32)));
3452 setRegister(rd_reg(instr), alu_out);
3453 break;
3454 }
3455 case op_rotr_d: {
3456 alu_out = static_cast<int64_t>(
3457 RotateRight64((rj_u(instr)), (rk_u(instr) % 64)));
3458 setRegister(rd_reg(instr), alu_out);
3459 break;
3460 }
3461 case op_mul_w: {
3462 alu_out =
3463 static_cast<int32_t>(rj(instr)) * static_cast<int32_t>(rk(instr));
3464 setRegister(rd_reg(instr), alu_out);
3465 break;
3466 }
3467 case op_mulh_w: {
3468 int32_t rj_lo = static_cast<int32_t>(rj(instr));
3469 int32_t rk_lo = static_cast<int32_t>(rk(instr));
3470 alu_out = static_cast<int64_t>(rj_lo) * static_cast<int64_t>(rk_lo);
3471 setRegister(rd_reg(instr), alu_out >> 32);
3472 break;
3473 }
3474 case op_mulh_wu: {
3475 uint32_t rj_lo = static_cast<uint32_t>(rj_u(instr));
3476 uint32_t rk_lo = static_cast<uint32_t>(rk_u(instr));
3477 alu_out = static_cast<uint64_t>(rj_lo) * static_cast<uint64_t>(rk_lo);
3478 setRegister(rd_reg(instr), alu_out >> 32);
3479 break;
3480 }
3481 case op_mul_d:
3482 setRegister(rd_reg(instr), rj(instr) * rk(instr));
3483 break;
3484 case op_mulh_d:
3485 setRegister(rd_reg(instr), MultiplyHighSigned(rj(instr), rk(instr)));
3486 break;
3487 case op_mulh_du:
3488 setRegister(rd_reg(instr),
3489 MultiplyHighUnsigned(rj_u(instr), rk_u(instr)));
3490 break;
3491 case op_mulw_d_w: {
3492 int64_t rj_i32 = static_cast<int32_t>(rj(instr));
3493 int64_t rk_i32 = static_cast<int32_t>(rk(instr));
3494 setRegister(rd_reg(instr), rj_i32 * rk_i32);
3495 break;
3496 }
3497 case op_mulw_d_wu: {
3498 uint64_t rj_u32 = static_cast<uint32_t>(rj_u(instr));
3499 uint64_t rk_u32 = static_cast<uint32_t>(rk_u(instr));
3500 setRegister(rd_reg(instr), rj_u32 * rk_u32);
3501 break;
3502 }
3503 case op_div_w: {
3504 int32_t rj_i32 = static_cast<int32_t>(rj(instr));
3505 int32_t rk_i32 = static_cast<int32_t>(rk(instr));
3506 if (rj_i32 == INT_MIN && rk_i32 == -1) {
3507 setRegister(rd_reg(instr), INT_MIN);
3508 } else if (rk_i32 != 0) {
3509 setRegister(rd_reg(instr), rj_i32 / rk_i32);
3510 }
3511 break;
3512 }
3513 case op_mod_w: {
3514 int32_t rj_i32 = static_cast<int32_t>(rj(instr));
3515 int32_t rk_i32 = static_cast<int32_t>(rk(instr));
3516 if (rj_i32 == INT_MIN && rk_i32 == -1) {
3517 setRegister(rd_reg(instr), 0);
3518 } else if (rk_i32 != 0) {
3519 setRegister(rd_reg(instr), rj_i32 % rk_i32);
3520 }
3521 break;
3522 }
3523 case op_div_wu: {
3524 uint32_t rj_u32 = static_cast<uint32_t>(rj(instr));
3525 uint32_t rk_u32 = static_cast<uint32_t>(rk(instr));
3526 if (rk_u32 != 0) {
3527 setRegister(rd_reg(instr), static_cast<int32_t>(rj_u32 / rk_u32));
3528 }
3529 break;
3530 }
3531 case op_mod_wu: {
3532 uint32_t rj_u32 = static_cast<uint32_t>(rj(instr));
3533 uint32_t rk_u32 = static_cast<uint32_t>(rk(instr));
3534 if (rk_u32 != 0) {
3535 setRegister(rd_reg(instr), static_cast<int32_t>(rj_u32 % rk_u32));
3536 }
3537 break;
3538 }
3539 case op_div_d: {
3540 if (rj(instr) == INT64_MIN && rk(instr) == -1) {
3541 setRegister(rd_reg(instr), INT64_MIN);
3542 } else if (rk(instr) != 0) {
3543 setRegister(rd_reg(instr), rj(instr) / rk(instr));
3544 }
3545 break;
3546 }
3547 case op_mod_d: {
3548 if (rj(instr) == LONG_MIN && rk(instr) == -1) {
3549 setRegister(rd_reg(instr), 0);
3550 } else if (rk(instr) != 0) {
3551 setRegister(rd_reg(instr), rj(instr) % rk(instr));
3552 }
3553 break;
3554 }
3555 case op_div_du: {
3556 if (rk_u(instr) != 0) {
3557 setRegister(rd_reg(instr),
3558 static_cast<int64_t>(rj_u(instr) / rk_u(instr)));
3559 }
3560 break;
3561 }
3562 case op_mod_du: {
3563 if (rk_u(instr) != 0) {
3564 setRegister(rd_reg(instr),
3565 static_cast<int64_t>(rj_u(instr) % rk_u(instr)));
3566 }
3567 break;
3568 }
3569 case op_break:
3570 softwareInterrupt(instr);
3571 break;
3572 case op_fadd_s: {
3573 setFpuRegisterFloat(fd_reg(instr), fj_float(instr) + fk_float(instr));
3574 break;
3575 }
3576 case op_fadd_d: {
3577 setFpuRegisterDouble(fd_reg(instr), fj_double(instr) + fk_double(instr));
3578 break;
3579 }
3580 case op_fsub_s: {
3581 setFpuRegisterFloat(fd_reg(instr), fj_float(instr) - fk_float(instr));
3582 break;
3583 }
3584 case op_fsub_d: {
3585 setFpuRegisterDouble(fd_reg(instr), fj_double(instr) - fk_double(instr));
3586 break;
3587 }
3588 case op_fmul_s: {
3589 setFpuRegisterFloat(fd_reg(instr), fj_float(instr) * fk_float(instr));
3590 break;
3591 }
3592 case op_fmul_d: {
3593 setFpuRegisterDouble(fd_reg(instr), fj_double(instr) * fk_double(instr));
3594 break;
3595 }
3596 case op_fdiv_s: {
3597 setFpuRegisterFloat(fd_reg(instr), fj_float(instr) / fk_float(instr));
3598 break;
3599 }
3600
3601 case op_fdiv_d: {
3602 setFpuRegisterDouble(fd_reg(instr), fj_double(instr) / fk_double(instr));
3603 break;
3604 }
3605 case op_fmax_s: {
3606 setFpuRegisterFloat(fd_reg(instr),
3607 FPUMax(fk_float(instr), fj_float(instr)));
3608 break;
3609 }
3610 case op_fmax_d: {
3611 setFpuRegisterDouble(fd_reg(instr),
3612 FPUMax(fk_double(instr), fj_double(instr)));
3613 break;
3614 }
3615 case op_fmin_s: {
3616 setFpuRegisterFloat(fd_reg(instr),
3617 FPUMin(fk_float(instr), fj_float(instr)));
3618 break;
3619 }
3620 case op_fmin_d: {
3621 setFpuRegisterDouble(fd_reg(instr),
3622 FPUMin(fk_double(instr), fj_double(instr)));
3623 break;
3624 }
3625 case op_fmaxa_s: {
3626 setFpuRegisterFloat(fd_reg(instr),
3627 FPUMaxA(fk_float(instr), fj_float(instr)));
3628 break;
3629 }
3630 case op_fmaxa_d: {
3631 setFpuRegisterDouble(fd_reg(instr),
3632 FPUMaxA(fk_double(instr), fj_double(instr)));
3633 break;
3634 }
3635 case op_fmina_s: {
3636 setFpuRegisterFloat(fd_reg(instr),
3637 FPUMinA(fk_float(instr), fj_float(instr)));
3638 break;
3639 }
3640 case op_fmina_d: {
3641 setFpuRegisterDouble(fd_reg(instr),
3642 FPUMinA(fk_double(instr), fj_double(instr)));
3643 break;
3644 }
3645 case op_ldx_b:
3646 setRegister(rd_reg(instr), readB(rj(instr) + rk(instr)));
3647 break;
3648 case op_ldx_h:
3649 setRegister(rd_reg(instr), readH(rj(instr) + rk(instr), instr));
3650 break;
3651 case op_ldx_w:
3652 setRegister(rd_reg(instr), readW(rj(instr) + rk(instr), instr));
3653 break;
3654 case op_ldx_d:
3655 setRegister(rd_reg(instr), readDW(rj(instr) + rk(instr), instr));
3656 break;
3657 case op_stx_b:
3658 writeB(rj(instr) + rk(instr), static_cast<int8_t>(rd(instr)));
3659 break;
3660 case op_stx_h:
3661 writeH(rj(instr) + rk(instr), static_cast<int16_t>(rd(instr)), instr);
3662 break;
3663 case op_stx_w:
3664 writeW(rj(instr) + rk(instr), static_cast<int32_t>(rd(instr)), instr);
3665 break;
3666 case op_stx_d:
3667 writeDW(rj(instr) + rk(instr), rd(instr), instr);
3668 break;
3669 case op_ldx_bu:
3670 setRegister(rd_reg(instr), readBU(rj(instr) + rk(instr)));
3671 break;
3672 case op_ldx_hu:
3673 setRegister(rd_reg(instr), readHU(rj(instr) + rk(instr), instr));
3674 break;
3675 case op_ldx_wu:
3676 setRegister(rd_reg(instr), readWU(rj(instr) + rk(instr), instr));
3677 break;
3678 case op_fldx_s:
3679 setFpuRegister(fd_reg(instr), kFPUInvalidResult); // Trash upper 32 bits.
3680 setFpuRegisterWord(fd_reg(instr), readW(rj(instr) + rk(instr), instr));
3681 break;
3682 case op_fldx_d:
3683 setFpuRegister(fd_reg(instr), kFPUInvalidResult); // Trash upper 32 bits.
3684 setFpuRegisterDouble(fd_reg(instr), readD(rj(instr) + rk(instr), instr));
3685 break;
3686 case op_fstx_s: {
3687 int32_t alu_out_32 = static_cast<int32_t>(getFpuRegister(fd_reg(instr)));
3688 writeW(rj(instr) + rk(instr), alu_out_32, instr);
3689 break;
3690 }
3691 case op_fstx_d: {
3692 writeD(rj(instr) + rk(instr), getFpuRegisterDouble(fd_reg(instr)), instr);
3693 break;
3694 }
3695 case op_amswap_w:
3696 UNIMPLEMENTED();
3697 break;
3698 case op_amswap_d:
3699 UNIMPLEMENTED();
3700 break;
3701 case op_amadd_w:
3702 UNIMPLEMENTED();
3703 break;
3704 case op_amadd_d:
3705 UNIMPLEMENTED();
3706 break;
3707 case op_amand_w:
3708 UNIMPLEMENTED();
3709 break;
3710 case op_amand_d:
3711 UNIMPLEMENTED();
3712 break;
3713 case op_amor_w:
3714 UNIMPLEMENTED();
3715 break;
3716 case op_amor_d:
3717 UNIMPLEMENTED();
3718 break;
3719 case op_amxor_w:
3720 UNIMPLEMENTED();
3721 break;
3722 case op_amxor_d:
3723 UNIMPLEMENTED();
3724 break;
3725 case op_ammax_w:
3726 UNIMPLEMENTED();
3727 break;
3728 case op_ammax_d:
3729 UNIMPLEMENTED();
3730 break;
3731 case op_ammin_w:
3732 UNIMPLEMENTED();
3733 break;
3734 case op_ammin_d:
3735 UNIMPLEMENTED();
3736 break;
3737 case op_ammax_wu:
3738 UNIMPLEMENTED();
3739 break;
3740 case op_ammax_du:
3741 UNIMPLEMENTED();
3742 break;
3743 case op_ammin_wu:
3744 UNIMPLEMENTED();
3745 break;
3746 case op_ammin_du:
3747 UNIMPLEMENTED();
3748 break;
3749 case op_amswap_db_w:
3750 UNIMPLEMENTED();
3751 break;
3752 case op_amswap_db_d:
3753 UNIMPLEMENTED();
3754 break;
3755 case op_amadd_db_w:
3756 UNIMPLEMENTED();
3757 break;
3758 case op_amadd_db_d:
3759 UNIMPLEMENTED();
3760 break;
3761 case op_amand_db_w:
3762 UNIMPLEMENTED();
3763 break;
3764 case op_amand_db_d:
3765 UNIMPLEMENTED();
3766 break;
3767 case op_amor_db_w:
3768 UNIMPLEMENTED();
3769 break;
3770 case op_amor_db_d:
3771 UNIMPLEMENTED();
3772 break;
3773 case op_amxor_db_w:
3774 UNIMPLEMENTED();
3775 break;
3776 case op_amxor_db_d:
3777 UNIMPLEMENTED();
3778 break;
3779 case op_ammax_db_w:
3780 UNIMPLEMENTED();
3781 break;
3782 case op_ammax_db_d:
3783 UNIMPLEMENTED();
3784 break;
3785 case op_ammin_db_w:
3786 UNIMPLEMENTED();
3787 break;
3788 case op_ammin_db_d:
3789 UNIMPLEMENTED();
3790 break;
3791 case op_ammax_db_wu:
3792 UNIMPLEMENTED();
3793 break;
3794 case op_ammax_db_du:
3795 UNIMPLEMENTED();
3796 break;
3797 case op_ammin_db_wu:
3798 UNIMPLEMENTED();
3799 break;
3800 case op_ammin_db_du:
3801 UNIMPLEMENTED();
3802 break;
3803 case op_dbar:
3804 // TODO(loong64): dbar simulation
3805 break;
3806 case op_ibar:
3807 UNIMPLEMENTED();
3808 break;
3809 case op_fcopysign_s:
3810 UNIMPLEMENTED();
3811 break;
3812 case op_fcopysign_d:
3813 UNIMPLEMENTED();
3814 break;
3815 default:
3816 UNREACHABLE();
3817 }
3818 }
3819
3820 void Simulator::decodeTypeOp22(SimInstruction* instr) {
3821 int64_t alu_out;
3822
3823 switch (instr->bits(31, 10) << 10) {
3824 case op_clz_w: {
3825 alu_out = U32(rj_u(instr)) ? __builtin_clz(U32(rj_u(instr))) : 32;
3826 setRegister(rd_reg(instr), alu_out);
3827 break;
3828 }
3829 case op_ctz_w: {
3830 alu_out = U32(rj_u(instr)) ? __builtin_ctz(U32(rj_u(instr))) : 32;
3831 setRegister(rd_reg(instr), alu_out);
3832 break;
3833 }
3834 case op_clz_d: {
3835 alu_out = U64(rj_u(instr)) ? __builtin_clzll(U64(rj_u(instr))) : 64;
3836 setRegister(rd_reg(instr), alu_out);
3837 break;
3838 }
3839 case op_ctz_d: {
3840 alu_out = U64(rj_u(instr)) ? __builtin_ctzll(U64(rj_u(instr))) : 64;
3841 setRegister(rd_reg(instr), alu_out);
3842 break;
3843 }
3844 case op_revb_2h: {
3845 uint32_t input = static_cast<uint32_t>(rj(instr));
3846 uint64_t output = 0;
3847
3848 uint32_t mask = 0xFF000000;
3849 for (int i = 0; i < 4; i++) {
3850 uint32_t tmp = mask & input;
3851 if (i % 2 == 0) {
3852 tmp = tmp >> 8;
3853 } else {
3854 tmp = tmp << 8;
3855 }
3856 output = output | tmp;
3857 mask = mask >> 8;
3858 }
3859
3860 alu_out = static_cast<int64_t>(static_cast<int32_t>(output));
3861 setRegister(rd_reg(instr), alu_out);
3862 break;
3863 }
3864 case op_revb_4h: {
3865 uint64_t input = rj_u(instr);
3866 uint64_t output = 0;
3867
3868 uint64_t mask = 0xFF00000000000000;
3869 for (int i = 0; i < 8; i++) {
3870 uint64_t tmp = mask & input;
3871 if (i % 2 == 0) {
3872 tmp = tmp >> 8;
3873 } else {
3874 tmp = tmp << 8;
3875 }
3876 output = output | tmp;
3877 mask = mask >> 8;
3878 }
3879
3880 alu_out = static_cast<int64_t>(output);
3881 setRegister(rd_reg(instr), alu_out);
3882 break;
3883 }
3884 case op_revb_2w: {
3885 uint64_t input = rj_u(instr);
3886 uint64_t output = 0;
3887
3888 uint64_t mask = 0xFF000000FF000000;
3889 for (int i = 0; i < 4; i++) {
3890 uint64_t tmp = mask & input;
3891 if (i <= 1) {
3892 tmp = tmp >> (24 - i * 16);
3893 } else {
3894 tmp = tmp << (i * 16 - 24);
3895 }
3896 output = output | tmp;
3897 mask = mask >> 8;
3898 }
3899
3900 alu_out = static_cast<int64_t>(output);
3901 setRegister(rd_reg(instr), alu_out);
3902 break;
3903 }
3904 case op_revb_d: {
3905 uint64_t input = rj_u(instr);
3906 uint64_t output = 0;
3907
3908 uint64_t mask = 0xFF00000000000000;
3909 for (int i = 0; i < 8; i++) {
3910 uint64_t tmp = mask & input;
3911 if (i <= 3) {
3912 tmp = tmp >> (56 - i * 16);
3913 } else {
3914 tmp = tmp << (i * 16 - 56);
3915 }
3916 output = output | tmp;
3917 mask = mask >> 8;
3918 }
3919
3920 alu_out = static_cast<int64_t>(output);
3921 setRegister(rd_reg(instr), alu_out);
3922 break;
3923 }
3924 case op_revh_2w: {
3925 uint64_t input = rj_u(instr);
3926 uint64_t output = 0;
3927
3928 uint64_t mask = 0xFFFF000000000000;
3929 for (int i = 0; i < 4; i++) {
3930 uint64_t tmp = mask & input;
3931 if (i % 2 == 0) {
3932 tmp = tmp >> 16;
3933 } else {
3934 tmp = tmp << 16;
3935 }
3936 output = output | tmp;
3937 mask = mask >> 16;
3938 }
3939
3940 alu_out = static_cast<int64_t>(output);
3941 setRegister(rd_reg(instr), alu_out);
3942 break;
3943 }
3944 case op_revh_d: {
3945 uint64_t input = rj_u(instr);
3946 uint64_t output = 0;
3947
3948 uint64_t mask = 0xFFFF000000000000;
3949 for (int i = 0; i < 4; i++) {
3950 uint64_t tmp = mask & input;
3951 if (i <= 1) {
3952 tmp = tmp >> (48 - i * 32);
3953 } else {
3954 tmp = tmp << (i * 32 - 48);
3955 }
3956 output = output | tmp;
3957 mask = mask >> 16;
3958 }
3959
3960 alu_out = static_cast<int64_t>(output);
3961 setRegister(rd_reg(instr), alu_out);
3962 break;
3963 }
3964 case op_bitrev_4b: {
3965 uint32_t input = static_cast<uint32_t>(rj(instr));
3966 uint32_t output = 0;
3967 uint8_t i_byte, o_byte;
3968
3969 // Reverse the bit in byte for each individual byte
3970 for (int i = 0; i < 4; i++) {
3971 output = output >> 8;
3972 i_byte = input & 0xFF;
3973
3974 // Fast way to reverse bits in byte
3975 // Devised by Sean Anderson, July 13, 2001
3976 o_byte = static_cast<uint8_t>(((i_byte * 0x0802LU & 0x22110LU) |
3977 (i_byte * 0x8020LU & 0x88440LU)) *
3978 0x10101LU >>
3979 16);
3980
3981 output = output | (static_cast<uint32_t>(o_byte << 24));
3982 input = input >> 8;
3983 }
3984
3985 alu_out = static_cast<int64_t>(static_cast<int32_t>(output));
3986 setRegister(rd_reg(instr), alu_out);
3987 break;
3988 }
3989 case op_bitrev_8b: {
3990 uint64_t input = rj_u(instr);
3991 uint64_t output = 0;
3992 uint8_t i_byte, o_byte;
3993
3994 // Reverse the bit in byte for each individual byte
3995 for (int i = 0; i < 8; i++) {
3996 output = output >> 8;
3997 i_byte = input & 0xFF;
3998
3999 // Fast way to reverse bits in byte
4000 // Devised by Sean Anderson, July 13, 2001
4001 o_byte = static_cast<uint8_t>(((i_byte * 0x0802LU & 0x22110LU) |
4002 (i_byte * 0x8020LU & 0x88440LU)) *
4003 0x10101LU >>
4004 16);
4005
4006 output = output | (static_cast<uint64_t>(o_byte) << 56);
4007 input = input >> 8;
4008 }
4009
4010 alu_out = static_cast<int64_t>(output);
4011 setRegister(rd_reg(instr), alu_out);
4012 break;
4013 }
4014 case op_bitrev_w: {
4015 uint32_t input = static_cast<uint32_t>(rj(instr));
4016 uint32_t output = 0;
4017 output = ReverseBits(input);
4018 alu_out = static_cast<int64_t>(static_cast<int32_t>(output));
4019 setRegister(rd_reg(instr), alu_out);
4020 break;
4021 }
4022 case op_bitrev_d: {
4023 alu_out = static_cast<int64_t>(ReverseBits(rj_u(instr)));
4024 setRegister(rd_reg(instr), alu_out);
4025 break;
4026 }
4027 case op_ext_w_b: {
4028 uint8_t input = static_cast<uint8_t>(rj(instr));
4029 alu_out = static_cast<int64_t>(static_cast<int8_t>(input));
4030 setRegister(rd_reg(instr), alu_out);
4031 break;
4032 }
4033 case op_ext_w_h: {
4034 uint16_t input = static_cast<uint16_t>(rj(instr));
4035 alu_out = static_cast<int64_t>(static_cast<int16_t>(input));
4036 setRegister(rd_reg(instr), alu_out);
4037 break;
4038 }
4039 case op_fabs_s: {
4040 setFpuRegisterFloat(fd_reg(instr), std::abs(fj_float(instr)));
4041 break;
4042 }
4043 case op_fabs_d: {
4044 setFpuRegisterDouble(fd_reg(instr), std::abs(fj_double(instr)));
4045 break;
4046 }
4047 case op_fneg_s: {
4048 setFpuRegisterFloat(fd_reg(instr), -fj_float(instr));
4049 break;
4050 }
4051 case op_fneg_d: {
4052 setFpuRegisterDouble(fd_reg(instr), -fj_double(instr));
4053 break;
4054 }
4055 case op_fsqrt_s: {
4056 if (fj_float(instr) >= 0) {
4057 setFpuRegisterFloat(fd_reg(instr), std::sqrt(fj_float(instr)));
4058 } else {
4059 setFpuRegisterFloat(fd_reg(instr), std::sqrt(-1)); // qnan
4060 setFCSRBit(kFCSRInvalidOpFlagBit, true);
4061 }
4062 break;
4063 }
4064 case op_fsqrt_d: {
4065 if (fj_double(instr) >= 0) {
4066 setFpuRegisterDouble(fd_reg(instr), std::sqrt(fj_double(instr)));
4067 } else {
4068 setFpuRegisterDouble(fd_reg(instr), std::sqrt(-1)); // qnan
4069 setFCSRBit(kFCSRInvalidOpFlagBit, true);
4070 }
4071 break;
4072 }
4073 case op_fmov_s: {
4074 setFpuRegisterFloat(fd_reg(instr), fj_float(instr));
4075 break;
4076 }
4077 case op_fmov_d: {
4078 setFpuRegisterDouble(fd_reg(instr), fj_double(instr));
4079 break;
4080 }
4081 case op_movgr2fr_w: {
4082 setFpuRegisterWord(fd_reg(instr), static_cast<int32_t>(rj(instr)));
4083 break;
4084 }
4085 case op_movgr2fr_d: {
4086 setFpuRegister(fd_reg(instr), rj(instr));
4087 break;
4088 }
4089 case op_movgr2frh_w: {
4090 setFpuRegisterHiWord(fd_reg(instr), static_cast<int32_t>(rj(instr)));
4091 break;
4092 }
4093 case op_movfr2gr_s: {
4094 setRegister(rd_reg(instr),
4095 static_cast<int64_t>(getFpuRegisterWord(fj_reg(instr))));
4096 break;
4097 }
4098 case op_movfr2gr_d: {
4099 setRegister(rd_reg(instr), getFpuRegister(fj_reg(instr)));
4100 break;
4101 }
4102 case op_movfrh2gr_s: {
4103 setRegister(rd_reg(instr), getFpuRegisterHiWord(fj_reg(instr)));
4104 break;
4105 }
4106 case op_movgr2fcsr: {
4107 // fcsr could be 0-3
4108 MOZ_ASSERT(rd_reg(instr) < 4);
4109 FCSR_ = static_cast<uint32_t>(rj(instr));
4110 break;
4111 }
4112 case op_movfcsr2gr: {
4113 setRegister(rd_reg(instr), FCSR_);
4114 break;
4115 }
4116 case op_fcvt_s_d: {
4117 setFpuRegisterFloat(fd_reg(instr), static_cast<float>(fj_double(instr)));
4118 break;
4119 }
4120 case op_fcvt_d_s: {
4121 setFpuRegisterDouble(fd_reg(instr), static_cast<double>(fj_float(instr)));
4122 break;
4123 }
4124 case op_ftintrm_w_s: {
4125 float fj = fj_float(instr);
4126 float rounded = std::floor(fj);
4127 int32_t result = static_cast<int32_t>(rounded);
4128 setFpuRegisterWord(fd_reg(instr), result);
4129 if (setFCSRRoundError<int32_t>(fj, rounded)) {
4130 setFpuRegisterWordInvalidResult(fj, rounded, fd_reg(instr));
4131 }
4132 break;
4133 }
4134 case op_ftintrm_w_d: {
4135 double fj = fj_double(instr);
4136 double rounded = std::floor(fj);
4137 int32_t result = static_cast<int32_t>(rounded);
4138 setFpuRegisterWord(fd_reg(instr), result);
4139 if (setFCSRRoundError<int32_t>(fj, rounded)) {
4140 setFpuRegisterInvalidResult(fj, rounded, fd_reg(instr));
4141 }
4142 break;
4143 }
4144 case op_ftintrm_l_s: {
4145 float fj = fj_float(instr);
4146 float rounded = std::floor(fj);
4147 int64_t result = static_cast<int64_t>(rounded);
4148 setFpuRegister(fd_reg(instr), result);
4149 if (setFCSRRoundError<int64_t>(fj, rounded)) {
4150 setFpuRegisterInvalidResult64(fj, rounded, fd_reg(instr));
4151 }
4152 break;
4153 }
4154 case op_ftintrm_l_d: {
4155 double fj = fj_double(instr);
4156 double rounded = std::floor(fj);
4157 int64_t result = static_cast<int64_t>(rounded);
4158 setFpuRegister(fd_reg(instr), result);
4159 if (setFCSRRoundError<int64_t>(fj, rounded)) {
4160 setFpuRegisterInvalidResult64(fj, rounded, fd_reg(instr));
4161 }
4162 break;
4163 }
4164 case op_ftintrp_w_s: {
4165 float fj = fj_float(instr);
4166 float rounded = std::ceil(fj);
4167 int32_t result = static_cast<int32_t>(rounded);
4168 setFpuRegisterWord(fd_reg(instr), result);
4169 if (setFCSRRoundError<int32_t>(fj, rounded)) {
4170 setFpuRegisterWordInvalidResult(fj, rounded, fd_reg(instr));
4171 }
4172 break;
4173 }
4174 case op_ftintrp_w_d: {
4175 double fj = fj_double(instr);
4176 double rounded = std::ceil(fj);
4177 int32_t result = static_cast<int32_t>(rounded);
4178 setFpuRegisterWord(fd_reg(instr), result);
4179 if (setFCSRRoundError<int32_t>(fj, rounded)) {
4180 setFpuRegisterInvalidResult(fj, rounded, fd_reg(instr));
4181 }
4182 break;
4183 }
4184 case op_ftintrp_l_s: {
4185 float fj = fj_float(instr);
4186 float rounded = std::ceil(fj);
4187 int64_t result = static_cast<int64_t>(rounded);
4188 setFpuRegister(fd_reg(instr), result);
4189 if (setFCSRRoundError<int64_t>(fj, rounded)) {
4190 setFpuRegisterInvalidResult64(fj, rounded, fd_reg(instr));
4191 }
4192 break;
4193 }
4194 case op_ftintrp_l_d: {
4195 double fj = fj_double(instr);
4196 double rounded = std::ceil(fj);
4197 int64_t result = static_cast<int64_t>(rounded);
4198 setFpuRegister(fd_reg(instr), result);
4199 if (setFCSRRoundError<int64_t>(fj, rounded)) {
4200 setFpuRegisterInvalidResult64(fj, rounded, fd_reg(instr));
4201 }
4202 break;
4203 }
4204 case op_ftintrz_w_s: {
4205 float fj = fj_float(instr);
4206 float rounded = std::trunc(fj);
4207 int32_t result = static_cast<int32_t>(rounded);
4208 setFpuRegisterWord(fd_reg(instr), result);
4209 if (setFCSRRoundError<int32_t>(fj, rounded)) {
4210 setFpuRegisterWordInvalidResult(fj, rounded, fd_reg(instr));
4211 }
4212 break;
4213 }
4214 case op_ftintrz_w_d: {
4215 double fj = fj_double(instr);
4216 double rounded = std::trunc(fj);
4217 int32_t result = static_cast<int32_t>(rounded);
4218 setFpuRegisterWord(fd_reg(instr), result);
4219 if (setFCSRRoundError<int32_t>(fj, rounded)) {
4220 setFpuRegisterInvalidResult(fj, rounded, fd_reg(instr));
4221 }
4222 break;
4223 }
4224 case op_ftintrz_l_s: {
4225 float fj = fj_float(instr);
4226 float rounded = std::trunc(fj);
4227 int64_t result = static_cast<int64_t>(rounded);
4228 setFpuRegister(fd_reg(instr), result);
4229 if (setFCSRRoundError<int64_t>(fj, rounded)) {
4230 setFpuRegisterInvalidResult64(fj, rounded, fd_reg(instr));
4231 }
4232 break;
4233 }
4234 case op_ftintrz_l_d: {
4235 double fj = fj_double(instr);
4236 double rounded = std::trunc(fj);
4237 int64_t result = static_cast<int64_t>(rounded);
4238 setFpuRegister(fd_reg(instr), result);
4239 if (setFCSRRoundError<int64_t>(fj, rounded)) {
4240 setFpuRegisterInvalidResult64(fj, rounded, fd_reg(instr));
4241 }
4242 break;
4243 }
4244 case op_ftintrne_w_s: {
4245 float fj = fj_float(instr);
4246 float rounded = std::floor(fj + 0.5);
4247 int32_t result = static_cast<int32_t>(rounded);
4248 if ((result & 1) != 0 && result - fj == 0.5) {
4249 // If the number is halfway between two integers,
4250 // round to the even one.
4251 result--;
4252 }
4253 setFpuRegisterWord(fd_reg(instr), result);
4254 if (setFCSRRoundError<int32_t>(fj, rounded)) {
4255 setFpuRegisterWordInvalidResult(fj, rounded, fd_reg(instr));
4256 }
4257 break;
4258 }
4259 case op_ftintrne_w_d: {
4260 double fj = fj_double(instr);
4261 double rounded = std::floor(fj + 0.5);
4262 int32_t result = static_cast<int32_t>(rounded);
4263 if ((result & 1) != 0 && result - fj == 0.5) {
4264 // If the number is halfway between two integers,
4265 // round to the even one.
4266 result--;
4267 }
4268 setFpuRegisterWord(fd_reg(instr), result);
4269 if (setFCSRRoundError<int32_t>(fj, rounded)) {
4270 setFpuRegisterInvalidResult(fj, rounded, fd_reg(instr));
4271 }
4272 break;
4273 }
4274 case op_ftintrne_l_s: {
4275 float fj = fj_float(instr);
4276 float rounded = std::floor(fj + 0.5);
4277 int64_t result = static_cast<int64_t>(rounded);
4278 if ((result & 1) != 0 && result - fj == 0.5) {
4279 // If the number is halfway between two integers,
4280 // round to the even one.
4281 result--;
4282 }
4283 setFpuRegister(fd_reg(instr), result);
4284 if (setFCSRRoundError<int64_t>(fj, rounded)) {
4285 setFpuRegisterInvalidResult64(fj, rounded, fd_reg(instr));
4286 }
4287 break;
4288 }
4289 case op_ftintrne_l_d: {
4290 double fj = fj_double(instr);
4291 double rounded = std::floor(fj + 0.5);
4292 int64_t result = static_cast<int64_t>(rounded);
4293 if ((result & 1) != 0 && result - fj == 0.5) {
4294 // If the number is halfway between two integers,
4295 // round to the even one.
4296 result--;
4297 }
4298 setFpuRegister(fd_reg(instr), result);
4299 if (setFCSRRoundError<int64_t>(fj, rounded)) {
4300 setFpuRegisterInvalidResult64(fj, rounded, fd_reg(instr));
4301 }
4302 break;
4303 }
4304 case op_ftint_w_s: {
4305 float fj = fj_float(instr);
4306 float rounded;
4307 int32_t result;
4308 roundAccordingToFCSR<float>(fj, &rounded, &result);
4309 setFpuRegisterWord(fd_reg(instr), result);
4310 if (setFCSRRoundError<int32_t>(fj, rounded)) {
4311 setFpuRegisterWordInvalidResult(fj, rounded, fd_reg(instr));
4312 }
4313 break;
4314 }
4315 case op_ftint_w_d: {
4316 double fj = fj_double(instr);
4317 double rounded;
4318 int32_t result;
4319 roundAccordingToFCSR<double>(fj, &rounded, &result);
4320 setFpuRegisterWord(fd_reg(instr), result);
4321 if (setFCSRRoundError<int32_t>(fj, rounded)) {
4322 setFpuRegisterWordInvalidResult(fj, rounded, fd_reg(instr));
4323 }
4324 break;
4325 }
4326 case op_ftint_l_s: {
4327 float fj = fj_float(instr);
4328 float rounded;
4329 int64_t result;
4330 round64AccordingToFCSR<float>(fj, &rounded, &result);
4331 setFpuRegister(fd_reg(instr), result);
4332 if (setFCSRRoundError<int64_t>(fj, rounded)) {
4333 setFpuRegisterInvalidResult64(fj, rounded, fd_reg(instr));
4334 }
4335 break;
4336 }
4337 case op_ftint_l_d: {
4338 double fj = fj_double(instr);
4339 double rounded;
4340 int64_t result;
4341 round64AccordingToFCSR<double>(fj, &rounded, &result);
4342 setFpuRegister(fd_reg(instr), result);
4343 if (setFCSRRoundError<int64_t>(fj, rounded)) {
4344 setFpuRegisterInvalidResult64(fj, rounded, fd_reg(instr));
4345 }
4346 break;
4347 }
4348 case op_ffint_s_w: {
4349 alu_out = getFpuRegisterSignedWord(fj_reg(instr));
4350 setFpuRegisterFloat(fd_reg(instr), static_cast<float>(alu_out));
4351 break;
4352 }
4353 case op_ffint_s_l: {
4354 alu_out = getFpuRegister(fj_reg(instr));
4355 setFpuRegisterFloat(fd_reg(instr), static_cast<float>(alu_out));
4356 break;
4357 }
4358 case op_ffint_d_w: {
4359 alu_out = getFpuRegisterSignedWord(fj_reg(instr));
4360 setFpuRegisterDouble(fd_reg(instr), static_cast<double>(alu_out));
4361 break;
4362 }
4363 case op_ffint_d_l: {
4364 alu_out = getFpuRegister(fj_reg(instr));
4365 setFpuRegisterDouble(fd_reg(instr), static_cast<double>(alu_out));
4366 break;
4367 }
4368 case op_frint_s: {
4369 float fj = fj_float(instr);
4370 float result, temp_result;
4371 double temp;
4372 float upper = std::ceil(fj);
4373 float lower = std::floor(fj);
4374 switch (getFCSRRoundingMode()) {
4375 case kRoundToNearest:
4376 if (upper - fj < fj - lower) {
4377 result = upper;
4378 } else if (upper - fj > fj - lower) {
4379 result = lower;
4380 } else {
4381 temp_result = upper / 2;
4382 float reminder = std::modf(temp_result, &temp);
4383 if (reminder == 0) {
4384 result = upper;
4385 } else {
4386 result = lower;
4387 }
4388 }
4389 break;
4390 case kRoundToZero:
4391 result = (fj > 0 ? lower : upper);
4392 break;
4393 case kRoundToPlusInf:
4394 result = upper;
4395 break;
4396 case kRoundToMinusInf:
4397 result = lower;
4398 break;
4399 }
4400 setFpuRegisterFloat(fd_reg(instr), result);
4401 if (result != fj) {
4402 setFCSRBit(kFCSRInexactFlagBit, true);
4403 }
4404 break;
4405 }
4406 case op_frint_d: {
4407 double fj = fj_double(instr);
4408 double result, temp, temp_result;
4409 double upper = std::ceil(fj);
4410 double lower = std::floor(fj);
4411 switch (getFCSRRoundingMode()) {
4412 case kRoundToNearest:
4413 if (upper - fj < fj - lower) {
4414 result = upper;
4415 } else if (upper - fj > fj - lower) {
4416 result = lower;
4417 } else {
4418 temp_result = upper / 2;
4419 double reminder = std::modf(temp_result, &temp);
4420 if (reminder == 0) {
4421 result = upper;
4422 } else {
4423 result = lower;
4424 }
4425 }
4426 break;
4427 case kRoundToZero:
4428 result = (fj > 0 ? lower : upper);
4429 break;
4430 case kRoundToPlusInf:
4431 result = upper;
4432 break;
4433 case kRoundToMinusInf:
4434 result = lower;
4435 break;
4436 }
4437 setFpuRegisterDouble(fd_reg(instr), result);
4438 if (result != fj) {
4439 setFCSRBit(kFCSRInexactFlagBit, true);
4440 }
4441 break;
4442 }
4443 case op_movfr2cf:
4444 printf("Sim UNIMPLEMENTED: MOVFR2CF\n");
4445 UNIMPLEMENTED();
4446 break;
4447 case op_movgr2cf:
4448 printf("Sim UNIMPLEMENTED: MOVGR2CF\n");
4449 UNIMPLEMENTED();
4450 break;
4451 case op_clo_w:
4452 printf("Sim UNIMPLEMENTED: FCO_W\n");
4453 UNIMPLEMENTED();
4454 break;
4455 case op_cto_w:
4456 printf("Sim UNIMPLEMENTED: FTO_W\n");
4457 UNIMPLEMENTED();
4458 break;
4459 case op_clo_d:
4460 printf("Sim UNIMPLEMENTED: FLO_D\n");
4461 UNIMPLEMENTED();
4462 break;
4463 case op_cto_d:
4464 printf("Sim UNIMPLEMENTED: FTO_D\n");
4465 UNIMPLEMENTED();
4466 break;
4467 // Unimplemented opcodes raised an error in the configuration step before,
4468 // so we can use the default here to set the destination register in common
4469 // cases.
4470 default:
4471 UNREACHABLE();
4472 }
4473 }
4474
4475 void Simulator::decodeTypeOp24(SimInstruction* instr) {
4476 switch (instr->bits(31, 8) << 8) {
4477 case op_movcf2fr:
4478 UNIMPLEMENTED();
4479 break;
4480 case op_movcf2gr:
4481 setRegister(rd_reg(instr), getCFRegister(cj_reg(instr)));
4482 break;
4483 UNIMPLEMENTED();
4484 break;
4485 default:
4486 UNREACHABLE();
4487 }
4488 }
4489
4490 // Executes the current instruction.
4491 void Simulator::instructionDecode(SimInstruction* instr) {
4492 if (!SimulatorProcess::ICacheCheckingDisableCount) {
4493 AutoLockSimulatorCache als;
4494 SimulatorProcess::checkICacheLocked(instr);
4495 }
4496 pc_modified_ = false;
4497
4498 switch (instr->instructionType()) {
4499 case SimInstruction::kOp6Type:
4500 decodeTypeOp6(instr);
4501 break;
4502 case SimInstruction::kOp7Type:
4503 decodeTypeOp7(instr);
4504 break;
4505 case SimInstruction::kOp8Type:
4506 decodeTypeOp8(instr);
4507 break;
4508 case SimInstruction::kOp10Type:
4509 decodeTypeOp10(instr);
4510 break;
4511 case SimInstruction::kOp11Type:
4512 decodeTypeOp11(instr);
4513 break;
4514 case SimInstruction::kOp12Type:
4515 decodeTypeOp12(instr);
4516 break;
4517 case SimInstruction::kOp14Type:
4518 decodeTypeOp14(instr);
4519 break;
4520 case SimInstruction::kOp15Type:
4521 decodeTypeOp15(instr);
4522 break;
4523 case SimInstruction::kOp16Type:
4524 decodeTypeOp16(instr);
4525 break;
4526 case SimInstruction::kOp17Type:
4527 decodeTypeOp17(instr);
4528 break;
4529 case SimInstruction::kOp22Type:
4530 decodeTypeOp22(instr);
4531 break;
4532 case SimInstruction::kOp24Type:
4533 decodeTypeOp24(instr);
4534 break;
4535 default:
4536 UNSUPPORTED();
4537 }
4538 if (!pc_modified_) {
4539 setRegister(pc,
4540 reinterpret_cast<int64_t>(instr) + SimInstruction::kInstrSize);
4541 }
4542 }
4543
4544 void Simulator::enable_single_stepping(SingleStepCallback cb, void* arg) {
4545 single_stepping_ = true;
4546 single_step_callback_ = cb;
4547 single_step_callback_arg_ = arg;
4548 single_step_callback_(single_step_callback_arg_, this, (void*)get_pc());
4549 }
4550
4551 void Simulator::disable_single_stepping() {
4552 if (!single_stepping_) {
4553 return;
4554 }
4555 single_step_callback_(single_step_callback_arg_, this, (void*)get_pc());
4556 single_stepping_ = false;
4557 single_step_callback_ = nullptr;
4558 single_step_callback_arg_ = nullptr;
4559 }
4560
4561 template <bool enableStopSimAt>
4562 void Simulator::execute() {
4563 if (single_stepping_) {
4564 single_step_callback_(single_step_callback_arg_, this, nullptr);
4565 }
4566
4567 // Get the PC to simulate. Cannot use the accessor here as we need the
4568 // raw PC value and not the one used as input to arithmetic instructions.
4569 int64_t program_counter = get_pc();
4570
4571 while (program_counter != end_sim_pc) {
4572 if (enableStopSimAt && (icount_ == Simulator::StopSimAt)) {
4573 loong64Debugger dbg(this);
4574 dbg.debug();
4575 } else {
4576 if (single_stepping_) {
4577 single_step_callback_(single_step_callback_arg_, this,
4578 (void*)program_counter);
4579 }
4580 SimInstruction* instr =
4581 reinterpret_cast<SimInstruction*>(program_counter);
4582 instructionDecode(instr);
4583 icount_++;
4584 }
4585 program_counter = get_pc();
4586 }
4587
4588 if (single_stepping_) {
4589 single_step_callback_(single_step_callback_arg_, this, nullptr);
4590 }
4591 }
4592
4593 void Simulator::callInternal(uint8_t* entry) {
4594 // Prepare to execute the code at entry.
4595 setRegister(pc, reinterpret_cast<int64_t>(entry));
4596 // Put down marker for end of simulation. The simulator will stop simulation
4597 // when the PC reaches this value. By saving the "end simulation" value into
4598 // the LR the simulation stops when returning to this call point.
4599 setRegister(ra, end_sim_pc);
4600
4601 // Remember the values of callee-saved registers.
4602 // The code below assumes that r9 is not used as sb (static base) in
4603 // simulator code and therefore is regarded as a callee-saved register.
4604 int64_t s0_val = getRegister(s0);
4605 int64_t s1_val = getRegister(s1);
4606 int64_t s2_val = getRegister(s2);
4607 int64_t s3_val = getRegister(s3);
4608 int64_t s4_val = getRegister(s4);
4609 int64_t s5_val = getRegister(s5);
4610 int64_t s6_val = getRegister(s6);
4611 int64_t s7_val = getRegister(s7);
4612 int64_t s8_val = getRegister(s8);
4613 int64_t gp_val = getRegister(gp);
4614 int64_t sp_val = getRegister(sp);
4615 int64_t tp_val = getRegister(tp);
4616 int64_t fp_val = getRegister(fp);
4617
4618 // Set up the callee-saved registers with a known value. To be able to check
4619 // that they are preserved properly across JS execution.
4620 int64_t callee_saved_value = icount_;
4621 setRegister(s0, callee_saved_value);
4622 setRegister(s1, callee_saved_value);
4623 setRegister(s2, callee_saved_value);
4624 setRegister(s3, callee_saved_value);
4625 setRegister(s4, callee_saved_value);
4626 setRegister(s5, callee_saved_value);
4627 setRegister(s6, callee_saved_value);
4628 setRegister(s7, callee_saved_value);
4629 setRegister(s8, callee_saved_value);
4630 setRegister(gp, callee_saved_value);
4631 setRegister(tp, callee_saved_value);
4632 setRegister(fp, callee_saved_value);
4633
4634 // Start the simulation.
4635 if (Simulator::StopSimAt != -1) {
4636 execute<true>();
4637 } else {
4638 execute<false>();
4639 }
4640
4641 // Check that the callee-saved registers have been preserved.
4642 MOZ_ASSERT(callee_saved_value == getRegister(s0));
4643 MOZ_ASSERT(callee_saved_value == getRegister(s1));
4644 MOZ_ASSERT(callee_saved_value == getRegister(s2));
4645 MOZ_ASSERT(callee_saved_value == getRegister(s3));
4646 MOZ_ASSERT(callee_saved_value == getRegister(s4));
4647 MOZ_ASSERT(callee_saved_value == getRegister(s5));
4648 MOZ_ASSERT(callee_saved_value == getRegister(s6));
4649 MOZ_ASSERT(callee_saved_value == getRegister(s7));
4650 MOZ_ASSERT(callee_saved_value == getRegister(s8));
4651 MOZ_ASSERT(callee_saved_value == getRegister(gp));
4652 MOZ_ASSERT(callee_saved_value == getRegister(tp));
4653 MOZ_ASSERT(callee_saved_value == getRegister(fp));
4654
4655 // Restore callee-saved registers with the original value.
4656 setRegister(s0, s0_val);
4657 setRegister(s1, s1_val);
4658 setRegister(s2, s2_val);
4659 setRegister(s3, s3_val);
4660 setRegister(s4, s4_val);
4661 setRegister(s5, s5_val);
4662 setRegister(s6, s6_val);
4663 setRegister(s7, s7_val);
4664 setRegister(s8, s8_val);
4665 setRegister(gp, gp_val);
4666 setRegister(sp, sp_val);
4667 setRegister(tp, tp_val);
4668 setRegister(fp, fp_val);
4669 }
4670
4671 int64_t Simulator::call(uint8_t* entry, int argument_count, ...) {
4672 va_list parameters;
4673 va_start(parameters, argument_count);
4674
4675 int64_t original_stack = getRegister(sp);
4676 // Compute position of stack on entry to generated code.
4677 int64_t entry_stack = original_stack;
4678 if (argument_count > kCArgSlotCount) {
4679 entry_stack = entry_stack - argument_count * sizeof(int64_t);
4680 } else {
4681 entry_stack = entry_stack - kCArgsSlotsSize;
4682 }
4683
4684 entry_stack &= ~U64(ABIStackAlignment - 1);
4685
4686 intptr_t* stack_argument = reinterpret_cast<intptr_t*>(entry_stack);
4687
4688 // Setup the arguments.
4689 for (int i = 0; i < argument_count; i++) {
4690 js::jit::Register argReg;
4691 if (GetIntArgReg(i, &argReg)) {
4692 setRegister(argReg.code(), va_arg(parameters, int64_t));
4693 } else {
4694 stack_argument[i] = va_arg(parameters, int64_t);
4695 }
4696 }
4697
4698 va_end(parameters);
4699 setRegister(sp, entry_stack);
4700
4701 callInternal(entry);
4702
4703 // Pop stack passed arguments.
4704 MOZ_ASSERT(entry_stack == getRegister(sp));
4705 setRegister(sp, original_stack);
4706
4707 int64_t result = getRegister(a0);
4708 return result;
4709 }
4710
4711 uintptr_t Simulator::pushAddress(uintptr_t address) {
4712 int new_sp = getRegister(sp) - sizeof(uintptr_t);
4713 uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(new_sp);
4714 *stack_slot = address;
4715 setRegister(sp, new_sp);
4716 return new_sp;
4717 }
4718
4719 uintptr_t Simulator::popAddress() {
4720 int current_sp = getRegister(sp);
4721 uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(current_sp);
4722 uintptr_t address = *stack_slot;
4723 setRegister(sp, current_sp + sizeof(uintptr_t));
4724 return address;
4725 }
4726
4727 } // namespace jit
4728 } // namespace js
4729
4730 js::jit::Simulator* JSContext::simulator() const { return simulator_; }