summaryrefslogtreecommitdiffstats
path: root/js/src/jit/Recover.h
blob: 7cc46c636db9e6d41b1bc34b35f110b9b32ce6a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef jit_Recover_h
#define jit_Recover_h

#include "mozilla/Attributes.h"

#include "jit/MIR.h"
#include "jit/Snapshots.h"

namespace js {
namespace jit {

// [SMDOC] IonMonkey Recover Instructions
//
// This file contains all recover instructions.
//
// A recover instruction is an equivalent of a MIR instruction which is executed
// before the reconstruction of a baseline frame. Recover instructions are used
// by resume points to fill the value which are not produced by the code
// compiled by IonMonkey. For example, if a value is optimized away by
// IonMonkey, but required by Baseline, then we should have a recover
// instruction to fill the missing baseline frame slot.
//
// Recover instructions are executed either during a bailout, or under a call
// when the stack frame is introspected. If the stack is introspected, then any
// use of recover instruction must lead to an invalidation of the code.
//
// For each MIR instruction where |canRecoverOnBailout| might return true, we
// have a RInstruction of the same name.
//
// Recover instructions are encoded by the code generator into a compact buffer
// (RecoverWriter). The MIR instruction method |writeRecoverData| should write a
// tag in the |CompactBufferWriter| which is used by
// |RInstruction::readRecoverData| to dispatch to the right Recover
// instruction. Then |writeRecoverData| writes any local fields which are
// necessary for the execution of the |recover| method. These fields are decoded
// by the Recover instruction constructor which has a |CompactBufferReader| as
// argument. The constructor of the Recover instruction should follow the same
// sequence as the |writeRecoverData| method of the MIR instruction.
//
// Recover instructions are decoded by the |SnapshotIterator| (RecoverReader),
// which is given as argument of the |recover| methods, in order to read the
// operands.  The number of operands read should be the same as the result of
// |numOperands|, which corresponds to the number of operands of the MIR
// instruction.  Operands should be decoded in the same order as the operands of
// the MIR instruction.
//
// The result of the |recover| method should either be a failure, or a value
// stored on the |SnapshotIterator|, by using the |storeInstructionResult|
// method.

#define RECOVER_OPCODE_LIST(_)    \
  _(ResumePoint)                  \
  _(BitNot)                       \
  _(BitAnd)                       \
  _(BitOr)                        \
  _(BitXor)                       \
  _(Lsh)                          \
  _(Rsh)                          \
  _(Ursh)                         \
  _(SignExtendInt32)              \
  _(Add)                          \
  _(Sub)                          \
  _(Mul)                          \
  _(Div)                          \
  _(Mod)                          \
  _(Not)                          \
  _(BigIntAdd)                    \
  _(BigIntSub)                    \
  _(BigIntMul)                    \
  _(BigIntDiv)                    \
  _(BigIntMod)                    \
  _(BigIntPow)                    \
  _(BigIntBitAnd)                 \
  _(BigIntBitOr)                  \
  _(BigIntBitXor)                 \
  _(BigIntLsh)                    \
  _(BigIntRsh)                    \
  _(BigIntIncrement)              \
  _(BigIntDecrement)              \
  _(BigIntNegate)                 \
  _(BigIntBitNot)                 \
  _(Compare)                      \
  _(Concat)                       \
  _(StringLength)                 \
  _(ArgumentsLength)              \
  _(Floor)                        \
  _(Ceil)                         \
  _(Round)                        \
  _(Trunc)                        \
  _(CharCodeAt)                   \
  _(FromCharCode)                 \
  _(FromCharCodeEmptyIfNegative)  \
  _(Pow)                          \
  _(PowHalf)                      \
  _(MinMax)                       \
  _(Abs)                          \
  _(Sqrt)                         \
  _(Atan2)                        \
  _(Hypot)                        \
  _(NearbyInt)                    \
  _(Sign)                         \
  _(MathFunction)                 \
  _(Random)                       \
  _(StringSplit)                  \
  _(NaNToZero)                    \
  _(RegExpMatcher)                \
  _(StringReplace)                \
  _(Substr)                       \
  _(TypeOf)                       \
  _(TypeOfName)                   \
  _(ToDouble)                     \
  _(ToFloat32)                    \
  _(TruncateToInt32)              \
  _(NewObject)                    \
  _(NewPlainObject)               \
  _(NewArrayObject)               \
  _(NewTypedArray)                \
  _(NewArray)                     \
  _(NewIterator)                  \
  _(NewCallObject)                \
  _(Lambda)                       \
  _(FunctionWithProto)            \
  _(ObjectKeys)                   \
  _(ObjectState)                  \
  _(ArrayState)                   \
  _(SetArrayLength)               \
  _(AtomicIsLockFree)             \
  _(BigIntAsIntN)                 \
  _(BigIntAsUintN)                \
  _(CreateArgumentsObject)        \
  _(CreateInlinedArgumentsObject) \
  _(Rest)                         \
  _(AssertRecoveredOnBailout)

class RResumePoint;
class SnapshotIterator;

class MOZ_NON_PARAM RInstruction {
 public:
  enum Opcode {
#define DEFINE_OPCODES_(op) Recover_##op,
    RECOVER_OPCODE_LIST(DEFINE_OPCODES_)
#undef DEFINE_OPCODES_
        Recover_Invalid
  };

  virtual Opcode opcode() const = 0;

  // As opposed to the MIR, there is no need to add more methods as every
  // other instruction is well abstracted under the "recover" method.
  bool isResumePoint() const { return opcode() == Recover_ResumePoint; }
  inline const RResumePoint* toResumePoint() const;

  // Call the copy constructor of a specific RInstruction, to do a copy of the
  // RInstruction content.
  virtual void cloneInto(RInstructionStorage* raw) const = 0;

  // Number of allocations which are encoded in the Snapshot for recovering
  // the current instruction.
  virtual uint32_t numOperands() const = 0;

  // Function used to recover the value computed by this instruction. This
  // function reads its arguments from the allocations listed on the snapshot
  // iterator and stores its returned value on the snapshot iterator too.
  [[nodiscard]] virtual bool recover(JSContext* cx,
                                     SnapshotIterator& iter) const = 0;

  // Decode an RInstruction on top of the reserved storage space, based on the
  // tag written by the writeRecoverData function of the corresponding MIR
  // instruction.
  static void readRecoverData(CompactBufferReader& reader,
                              RInstructionStorage* raw);
};

#define RINSTRUCTION_HEADER_(op)                                        \
 private:                                                               \
  friend class RInstruction;                                            \
  explicit R##op(CompactBufferReader& reader);                          \
  explicit R##op(const R##op& src) = default;                           \
                                                                        \
 public:                                                                \
  Opcode opcode() const override { return RInstruction::Recover_##op; } \
  void cloneInto(RInstructionStorage* raw) const override {             \
    new (raw->addr()) R##op(*this);                                     \
  }

#define RINSTRUCTION_HEADER_NUM_OP_MAIN(op, numOp) \
  RINSTRUCTION_HEADER_(op)                         \
  uint32_t numOperands() const override { return numOp; }

#ifdef DEBUG
#  define RINSTRUCTION_HEADER_NUM_OP_(op, numOp)                      \
    RINSTRUCTION_HEADER_NUM_OP_MAIN(op, numOp)                        \
    static_assert(                                                    \
        M##op::staticNumOperands == numOp,                            \
        "The recover instructions's numOperands should equal to the " \
        "MIR's numOperands");
#else
#  define RINSTRUCTION_HEADER_NUM_OP_(op, numOp) \
    RINSTRUCTION_HEADER_NUM_OP_MAIN(op, numOp)
#endif

class RResumePoint final : public RInstruction {
 private:
  uint32_t pcOffsetAndMode_;  // Offset from script->code and ResumeMode.
  uint32_t numOperands_;      // Number of slots.

 public:
  RINSTRUCTION_HEADER_(ResumePoint)

  // Used to encode/decode pcOffsetAndMode_.
  static constexpr uint32_t PCOffsetShift = 4;
  static constexpr uint32_t ResumeModeMask = 0b1111;
  static_assert(uint32_t(ResumeMode::Last) <= ResumeModeMask);

  uint32_t pcOffset() const { return pcOffsetAndMode_ >> PCOffsetShift; }
  ResumeMode mode() const {
    return ResumeMode(pcOffsetAndMode_ & ResumeModeMask);
  }

  uint32_t numOperands() const override { return numOperands_; }
  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBitNot final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BitNot, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBitAnd final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BitAnd, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBitOr final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BitOr, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBitXor final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BitXor, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RLsh final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(Lsh, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RRsh final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(Rsh, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RUrsh final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(Ursh, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RSignExtendInt32 final : public RInstruction {
 private:
  uint8_t mode_;

 public:
  RINSTRUCTION_HEADER_NUM_OP_(SignExtendInt32, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RAdd final : public RInstruction {
 private:
  bool isFloatOperation_;

 public:
  RINSTRUCTION_HEADER_NUM_OP_(Add, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RSub final : public RInstruction {
 private:
  bool isFloatOperation_;

 public:
  RINSTRUCTION_HEADER_NUM_OP_(Sub, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RMul final : public RInstruction {
 private:
  bool isFloatOperation_;
  uint8_t mode_;

 public:
  RINSTRUCTION_HEADER_NUM_OP_(Mul, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RDiv final : public RInstruction {
 private:
  bool isFloatOperation_;

 public:
  RINSTRUCTION_HEADER_NUM_OP_(Div, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RMod final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(Mod, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RNot final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(Not, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntAdd final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntAdd, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntSub final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntSub, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntMul final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntMul, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntDiv final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntDiv, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntMod final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntMod, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntPow final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntPow, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntBitAnd final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntBitAnd, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntBitOr final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntBitOr, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntBitXor final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntBitXor, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntLsh final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntLsh, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntRsh final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntRsh, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntIncrement final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntIncrement, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntDecrement final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntDecrement, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntNegate final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntNegate, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntBitNot final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntBitNot, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RCompare final : public RInstruction {
  JSOp jsop_;

 public:
  RINSTRUCTION_HEADER_NUM_OP_(Compare, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RConcat final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(Concat, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RStringLength final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(StringLength, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RArgumentsLength final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(ArgumentsLength, 0)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RFloor final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(Floor, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RCeil final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(Ceil, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RRound final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(Round, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RTrunc final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(Trunc, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RCharCodeAt final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(CharCodeAt, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RFromCharCode final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(FromCharCode, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RFromCharCodeEmptyIfNegative final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(FromCharCodeEmptyIfNegative, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RPow final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(Pow, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RPowHalf final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(PowHalf, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RMinMax final : public RInstruction {
 private:
  bool isMax_;

 public:
  RINSTRUCTION_HEADER_NUM_OP_(MinMax, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RAbs final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(Abs, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RSqrt final : public RInstruction {
 private:
  bool isFloatOperation_;

 public:
  RINSTRUCTION_HEADER_NUM_OP_(Sqrt, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RAtan2 final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(Atan2, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RHypot final : public RInstruction {
 private:
  uint32_t numOperands_;

 public:
  RINSTRUCTION_HEADER_(Hypot)

  uint32_t numOperands() const override { return numOperands_; }

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RNearbyInt final : public RInstruction {
 private:
  uint8_t roundingMode_;

 public:
  RINSTRUCTION_HEADER_NUM_OP_(NearbyInt, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RSign final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(Sign, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RMathFunction final : public RInstruction {
 private:
  UnaryMathFunction function_;

 public:
  RINSTRUCTION_HEADER_NUM_OP_(MathFunction, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RRandom final : public RInstruction {
  RINSTRUCTION_HEADER_NUM_OP_(Random, 0)
 public:
  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RStringSplit final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(StringSplit, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RNaNToZero final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(NaNToZero, 1);

  bool recover(JSContext* cx, SnapshotIterator& iter) const override;
};

class RRegExpMatcher final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(RegExpMatcher, 3)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RStringReplace final : public RInstruction {
 private:
  bool isFlatReplacement_;

 public:
  RINSTRUCTION_HEADER_NUM_OP_(StringReplace, 3)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RSubstr final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(Substr, 3)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RTypeOf final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(TypeOf, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RTypeOfName final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(TypeOfName, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RToDouble final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(ToDouble, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RToFloat32 final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(ToFloat32, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RTruncateToInt32 final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(TruncateToInt32, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RNewObject final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(NewObject, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RNewPlainObject final : public RInstruction {
 private:
  gc::AllocKind allocKind_;
  gc::Heap initialHeap_;

 public:
  RINSTRUCTION_HEADER_NUM_OP_(NewPlainObject, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RNewArrayObject final : public RInstruction {
 private:
  uint32_t length_;
  gc::Heap initialHeap_;

 public:
  RINSTRUCTION_HEADER_NUM_OP_(NewArrayObject, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RNewTypedArray final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(NewTypedArray, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RNewArray final : public RInstruction {
 private:
  uint32_t count_;

 public:
  RINSTRUCTION_HEADER_NUM_OP_(NewArray, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RNewIterator final : public RInstruction {
 private:
  uint8_t type_;

 public:
  RINSTRUCTION_HEADER_NUM_OP_(NewIterator, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RLambda final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(Lambda, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RFunctionWithProto final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(FunctionWithProto, 3)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RNewCallObject final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(NewCallObject, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RObjectKeys final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(ObjectKeys, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RObjectState final : public RInstruction {
 private:
  uint32_t numSlots_;  // Number of slots.

 public:
  RINSTRUCTION_HEADER_(ObjectState)

  uint32_t numSlots() const { return numSlots_; }
  uint32_t numOperands() const override {
    // +1 for the object.
    return numSlots() + 1;
  }

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RArrayState final : public RInstruction {
 private:
  uint32_t numElements_;

 public:
  RINSTRUCTION_HEADER_(ArrayState)

  uint32_t numElements() const { return numElements_; }
  uint32_t numOperands() const override {
    // +1 for the array.
    // +1 for the initalized length.
    return numElements() + 2;
  }

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RSetArrayLength final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(SetArrayLength, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RAtomicIsLockFree final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(AtomicIsLockFree, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntAsIntN final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntAsIntN, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RBigIntAsUintN final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(BigIntAsUintN, 2)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RCreateArgumentsObject final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(CreateArgumentsObject, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RCreateInlinedArgumentsObject final : public RInstruction {
 private:
  uint32_t numActuals_;

 public:
  RINSTRUCTION_HEADER_(CreateInlinedArgumentsObject)

  uint32_t numActuals() const { return numActuals_; }
  uint32_t numOperands() const override {
    // +1 for the callObj.
    // +1 for the callee.
    return numActuals() + 2;
  }

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RRest final : public RInstruction {
  uint32_t numFormals_;

 public:
  RINSTRUCTION_HEADER_NUM_OP_(Rest, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

class RAssertRecoveredOnBailout final : public RInstruction {
 public:
  RINSTRUCTION_HEADER_NUM_OP_(AssertRecoveredOnBailout, 1)

  [[nodiscard]] bool recover(JSContext* cx,
                             SnapshotIterator& iter) const override;
};

#undef RINSTRUCTION_HEADER_
#undef RINSTRUCTION_HEADER_NUM_OP_
#undef RINSTRUCTION_HEADER_NUM_OP_MAIN

const RResumePoint* RInstruction::toResumePoint() const {
  MOZ_ASSERT(isResumePoint());
  return static_cast<const RResumePoint*>(this);
}

}  // namespace jit
}  // namespace js

#endif /* jit_Recover_h */