mozjs_sys 0.67.1

System crate for the Mozilla SpiderMonkey JavaScript engine.
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
/* -*- 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_IonControlFlow_h
#define jit_IonControlFlow_h

#include "mozilla/Array.h"

#include "jit/BytecodeAnalysis.h"
#include "jit/FixedList.h"
#include "jit/JitAllocPolicy.h"
#include "js/TypeDecls.h"

namespace js {
namespace jit {

class CFGControlInstruction;

// Adds MFoo::New functions which are mirroring the arguments of the
// constructors. Opcodes which are using this macro can be called with a
// TempAllocator, or the fallible version of the TempAllocator.
#define TRIVIAL_CFG_NEW_WRAPPERS                                             \
  template <typename... Args>                                                \
  static CFGThisOpcode* New(TempAllocator& alloc, Args&&... args) {          \
    return new (alloc) CFGThisOpcode(std::forward<Args>(args)...);           \
  }                                                                          \
  template <typename... Args>                                                \
  static CFGThisOpcode* New(TempAllocator::Fallible alloc, Args&&... args) { \
    return new (alloc) CFGThisOpcode(std::forward<Args>(args)...);           \
  }

class CFGSpace {
  static const size_t DEFAULT_CHUNK_SIZE = 4096;

 protected:
  LifoAlloc allocator_;

 public:
  explicit CFGSpace() : allocator_(DEFAULT_CHUNK_SIZE) {}

  LifoAlloc& lifoAlloc() { return allocator_; }

  size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
    return allocator_.sizeOfExcludingThis(mallocSizeOf);
  }
};

class CFGBlock : public TempObject {
  size_t id_;
  jsbytecode* start;
  jsbytecode* stop;
  CFGControlInstruction* end;
  bool inWorkList;

 public:
  explicit CFGBlock(jsbytecode* start)
      : id_(-1), start(start), stop(nullptr), end(nullptr), inWorkList(false) {}

  static CFGBlock* New(TempAllocator& alloc, jsbytecode* start) {
    return new (alloc) CFGBlock(start);
  }

  void operator=(const CFGBlock&) = delete;

  jsbytecode* startPc() const { return start; }
  void setStartPc(jsbytecode* startPc) { start = startPc; }
  jsbytecode* stopPc() const {
    MOZ_ASSERT(stop);
    return stop;
  }
  void setStopPc(jsbytecode* stopPc) { stop = stopPc; }
  CFGControlInstruction* stopIns() const {
    MOZ_ASSERT(end);
    return end;
  }
  void setStopIns(CFGControlInstruction* stopIns) { end = stopIns; }
  bool isInWorkList() const { return inWorkList; }
  void setInWorklist() {
    MOZ_ASSERT(!inWorkList);
    inWorkList = true;
  }
  void clearInWorkList() {
    MOZ_ASSERT(inWorkList);
    inWorkList = false;
  }
  size_t id() const { return id_; }
  void setId(size_t id) { id_ = id; }
};

#define CFG_CONTROL_OPCODE_LIST(_) \
  _(Test)                          \
  _(CondSwitchCase)                \
  _(Goto)                          \
  _(Return)                        \
  _(RetRVal)                       \
  _(LoopEntry)                     \
  _(BackEdge)                      \
  _(TableSwitch)                   \
  _(Try)                           \
  _(Throw)

// Forward declarations of MIR types.
#define FORWARD_DECLARE(type) class CFG##type;
CFG_CONTROL_OPCODE_LIST(FORWARD_DECLARE)
#undef FORWARD_DECLARE

#define CFG_CONTROL_HEADER(type_name)                                      \
  static const Type classOpcode = CFGControlInstruction::Type_##type_name; \
  using CFGThisOpcode = CFG##type_name;                                    \
  Type type() const override { return classOpcode; }                       \
  const char* Name() const override { return #type_name; }

class CFGControlInstruction : public TempObject {
 public:
  enum Type {
#define DEFINE_TYPES(type) Type_##type,
    CFG_CONTROL_OPCODE_LIST(DEFINE_TYPES)
#undef DEFINE_TYPES
  };

  virtual size_t numSuccessors() const = 0;
  virtual CFGBlock* getSuccessor(size_t i) const = 0;
  virtual void replaceSuccessor(size_t i, CFGBlock* successor) = 0;
  virtual Type type() const = 0;
  virtual const char* Name() const = 0;

  template <typename CFGType>
  bool is() const {
    return type() == CFGType::classOpcode;
  }
  template <typename CFGType>
  CFGType* to() {
    MOZ_ASSERT(this->is<CFGType>());
    return static_cast<CFGType*>(this);
  }
  template <typename CFGType>
  const CFGType* to() const {
    MOZ_ASSERT(this->is<CFGType>());
    return static_cast<const CFGType*>(this);
  }
#define TYPE_CASTS(type)                                  \
  bool is##type() const { return this->is<CFG##type>(); } \
  CFG##type* to##type() { return this->to<CFG##type>(); } \
  const CFG##type* to##type() const { return this->to<CFG##type>(); }
  CFG_CONTROL_OPCODE_LIST(TYPE_CASTS)
#undef TYPE_CASTS
};

template <size_t Successors>
class CFGAryControlInstruction : public CFGControlInstruction {
  mozilla::Array<CFGBlock*, Successors> successors_;

 public:
  size_t numSuccessors() const final { return Successors; }
  CFGBlock* getSuccessor(size_t i) const final { return successors_[i]; }
  void replaceSuccessor(size_t i, CFGBlock* succ) final {
    successors_[i] = succ;
  }
};

class CFGTry : public CFGControlInstruction {
  CFGBlock* tryBlock_;
  jsbytecode* catchStartPc_;
  CFGBlock* mergePoint_;

  CFGTry(CFGBlock* successor, jsbytecode* catchStartPc, CFGBlock* mergePoint)
      : tryBlock_(successor),
        catchStartPc_(catchStartPc),
        mergePoint_(mergePoint) {}

 public:
  CFG_CONTROL_HEADER(Try)
  TRIVIAL_CFG_NEW_WRAPPERS

  static CFGTry* CopyWithNewTargets(TempAllocator& alloc, CFGTry* old,
                                    CFGBlock* tryBlock, CFGBlock* merge) {
    return new (alloc) CFGTry(tryBlock, old->catchStartPc(), merge);
  }

  size_t numSuccessors() const final { return 2; }
  CFGBlock* getSuccessor(size_t i) const final {
    MOZ_ASSERT(i < numSuccessors());
    return (i == 0) ? tryBlock_ : mergePoint_;
  }
  void replaceSuccessor(size_t i, CFGBlock* succ) final {
    MOZ_ASSERT(i < numSuccessors());
    if (i == 0) {
      tryBlock_ = succ;
    } else {
      mergePoint_ = succ;
    }
  }

  CFGBlock* tryBlock() const { return getSuccessor(0); }

  jsbytecode* catchStartPc() const { return catchStartPc_; }

  CFGBlock* afterTryCatchBlock() const { return getSuccessor(1); }
};

class CFGTableSwitch : public CFGControlInstruction {
  Vector<CFGBlock*, 4, JitAllocPolicy> successors_;
  size_t low_;
  size_t high_;

  CFGTableSwitch(TempAllocator& alloc, size_t low, size_t high)
      : successors_(alloc), low_(low), high_(high) {}

 public:
  CFG_CONTROL_HEADER(TableSwitch);

  static CFGTableSwitch* New(TempAllocator& alloc, size_t low, size_t high) {
    return new (alloc) CFGTableSwitch(alloc, low, high);
  }

  size_t numSuccessors() const final { return successors_.length(); }
  CFGBlock* getSuccessor(size_t i) const final {
    MOZ_ASSERT(i < numSuccessors());
    return successors_[i];
  }
  void replaceSuccessor(size_t i, CFGBlock* succ) final {
    MOZ_ASSERT(i < numSuccessors());
    successors_[i] = succ;
  }

  bool addDefault(CFGBlock* defaultCase) {
    MOZ_ASSERT(successors_.length() == 0);
    return successors_.append(defaultCase);
  }

  bool addCase(CFGBlock* caseBlock) {
    MOZ_ASSERT(successors_.length() > 0);
    return successors_.append(caseBlock);
  }

  CFGBlock* defaultCase() const { return getSuccessor(0); }

  CFGBlock* getCase(size_t i) const { return getSuccessor(i + 1); }

  size_t high() const { return high_; }

  size_t low() const { return low_; }
};

/**
 * CFGCondSwitchCase
 *
 * IFEQ
 *    POP truePopAmount
 *    JUMP succ1
 * IFNE
 *    POP falsePopAmount
 *    JUMP succ2
 */
class CFGCondSwitchCase : public CFGAryControlInstruction<2> {
  const uint8_t truePopAmount_;
  const uint8_t falsePopAmount_;

  CFGCondSwitchCase(CFGBlock* succ1, size_t truePopAmount, CFGBlock* succ2,
                    size_t falsePopAmount)
      : truePopAmount_(truePopAmount), falsePopAmount_(falsePopAmount) {
    MOZ_ASSERT(truePopAmount_ == truePopAmount,
               "truePopAmount should fit in uint8_t");
    MOZ_ASSERT(falsePopAmount_ == falsePopAmount,
               "falsePopAmount should fit in uint8_t");
    replaceSuccessor(0, succ1);
    replaceSuccessor(1, succ2);
  }

 public:
  CFG_CONTROL_HEADER(CondSwitchCase);

  static CFGCondSwitchCase* NewFalseBranchIsDefault(TempAllocator& alloc,
                                                    CFGBlock* case_,
                                                    CFGBlock* default_) {
    // True and false branch both go to a body and don't need the lhs and
    // rhs to the compare. Pop them.
    return new (alloc) CFGCondSwitchCase(case_, 2, default_, 2);
  }

  static CFGCondSwitchCase* NewFalseBranchIsNextCase(TempAllocator& alloc,
                                                     CFGBlock* case_,
                                                     CFGBlock* nextCase) {
    // True branch goes to the body and doesn't need the lhs and
    // rhs to the compare anymore. Pop them. The next case still
    // needs the lhs.
    return new (alloc) CFGCondSwitchCase(case_, 2, nextCase, 1);
  }

  static CFGCondSwitchCase* CopyWithNewTargets(TempAllocator& alloc,
                                               CFGCondSwitchCase* old,
                                               CFGBlock* succ1,
                                               CFGBlock* succ2) {
    return new (alloc) CFGCondSwitchCase(succ1, old->truePopAmount(), succ2,
                                         old->falsePopAmount());
  }

  CFGBlock* trueBranch() const { return getSuccessor(0); }
  CFGBlock* falseBranch() const { return getSuccessor(1); }
  size_t truePopAmount() const { return truePopAmount_; }
  size_t falsePopAmount() const { return falsePopAmount_; }
};

/**
 * CFGTest
 *
 * POP / PEEK (depending on keepCondition_)
 * IFEQ JUMP succ1
 * IFNEQ JUMP succ2
 *
 */
class CFGTest : public CFGAryControlInstruction<2> {
  // By default the condition gets popped. This variable
  // keeps track if we want to keep the condition.
  bool keepCondition_;

  CFGTest(CFGBlock* succ1, CFGBlock* succ2) : keepCondition_(false) {
    replaceSuccessor(0, succ1);
    replaceSuccessor(1, succ2);
  }
  CFGTest(CFGBlock* succ1, CFGBlock* succ2, bool keepCondition)
      : keepCondition_(keepCondition) {
    replaceSuccessor(0, succ1);
    replaceSuccessor(1, succ2);
  }

 public:
  CFG_CONTROL_HEADER(Test);
  TRIVIAL_CFG_NEW_WRAPPERS

  static CFGTest* CopyWithNewTargets(TempAllocator& alloc, CFGTest* old,
                                     CFGBlock* succ1, CFGBlock* succ2) {
    return new (alloc) CFGTest(succ1, succ2, old->mustKeepCondition());
  }

  CFGBlock* trueBranch() const { return getSuccessor(0); }
  CFGBlock* falseBranch() const { return getSuccessor(1); }
  void keepCondition() { keepCondition_ = true; }
  bool mustKeepCondition() const { return keepCondition_; }
};

/**
 * CFGReturn
 *
 * POP
 * RETURN popped value
 *
 */
class CFGReturn : public CFGAryControlInstruction<0> {
 public:
  CFG_CONTROL_HEADER(Return);
  TRIVIAL_CFG_NEW_WRAPPERS
};

/**
 * CFGRetRVal
 *
 * RETURN the value in the return value slot
 *
 */
class CFGRetRVal : public CFGAryControlInstruction<0> {
 public:
  CFG_CONTROL_HEADER(RetRVal);
  TRIVIAL_CFG_NEW_WRAPPERS
};

/**
 * CFGThrow
 *
 * POP
 * THROW popped value
 *
 */
class CFGThrow : public CFGAryControlInstruction<0> {
 public:
  CFG_CONTROL_HEADER(Throw);
  TRIVIAL_CFG_NEW_WRAPPERS
};

class CFGUnaryControlInstruction : public CFGAryControlInstruction<1> {
 public:
  explicit CFGUnaryControlInstruction(CFGBlock* block) {
    MOZ_ASSERT(block);
    replaceSuccessor(0, block);
  }

  CFGBlock* successor() const { return getSuccessor(0); }
};

/**
 * CFGGOTO
 *
 * POP (x popAmount)
 * JMP block
 *
 */
class CFGGoto : public CFGUnaryControlInstruction {
  const size_t popAmount_;

  explicit CFGGoto(CFGBlock* block)
      : CFGUnaryControlInstruction(block), popAmount_(0) {}

  CFGGoto(CFGBlock* block, size_t popAmount_)
      : CFGUnaryControlInstruction(block), popAmount_(popAmount_) {}

 public:
  CFG_CONTROL_HEADER(Goto);
  TRIVIAL_CFG_NEW_WRAPPERS

  static CFGGoto* CopyWithNewTargets(TempAllocator& alloc, CFGGoto* old,
                                     CFGBlock* block) {
    return new (alloc) CFGGoto(block, old->popAmount());
  }

  size_t popAmount() const { return popAmount_; }
};

/**
 * CFGBackEdge
 *
 * Jumps back to the start of the loop.
 *
 * JMP block
 *
 */
class CFGBackEdge : public CFGUnaryControlInstruction {
  explicit CFGBackEdge(CFGBlock* block) : CFGUnaryControlInstruction(block) {}

 public:
  CFG_CONTROL_HEADER(BackEdge);
  TRIVIAL_CFG_NEW_WRAPPERS

  static CFGBackEdge* CopyWithNewTargets(TempAllocator& alloc, CFGBackEdge* old,
                                         CFGBlock* block) {
    return new (alloc) CFGBackEdge(block);
  }
};

/**
 * CFGLOOPENTRY
 *
 * Indicates the jumping block is the start of a loop.
 * That block is the only block allowed to have a backedge.
 *
 * JMP block
 *
 */
class CFGLoopEntry : public CFGUnaryControlInstruction {
  bool canOsr_;
  bool isForIn_;
  bool isBrokenLoop_;
  size_t stackPhiCount_;
  jsbytecode* loopStopPc_;

  CFGLoopEntry(CFGBlock* block, size_t stackPhiCount)
      : CFGUnaryControlInstruction(block),
        canOsr_(false),
        isForIn_(false),
        isBrokenLoop_(false),
        stackPhiCount_(stackPhiCount),
        loopStopPc_(nullptr) {}

  CFGLoopEntry(CFGBlock* block, bool canOsr, bool isForIn, bool isBrokenLoop,
               size_t stackPhiCount, jsbytecode* loopStopPc)
      : CFGUnaryControlInstruction(block),
        canOsr_(canOsr),
        isForIn_(isForIn),
        isBrokenLoop_(isBrokenLoop),
        stackPhiCount_(stackPhiCount),
        loopStopPc_(loopStopPc) {}

 public:
  CFG_CONTROL_HEADER(LoopEntry);
  TRIVIAL_CFG_NEW_WRAPPERS

  static CFGLoopEntry* CopyWithNewTargets(TempAllocator& alloc,
                                          CFGLoopEntry* old,
                                          CFGBlock* loopEntry) {
    return new (alloc) CFGLoopEntry(loopEntry, old->canOsr(), old->isForIn(),
                                    old->isBrokenLoop(), old->stackPhiCount(),
                                    old->maybeLoopStopPc());
  }

  void setCanOsr() { canOsr_ = true; }

  bool isBrokenLoop() const { return isBrokenLoop_; }
  void setIsBrokenLoop() { isBrokenLoop_ = true; }

  bool canOsr() const { return canOsr_; }

  void setIsForIn() { isForIn_ = true; }
  bool isForIn() const { return isForIn_; }

  size_t stackPhiCount() const { return stackPhiCount_; }

  jsbytecode* maybeLoopStopPc() const { return loopStopPc_; }

  jsbytecode* loopStopPc() const {
    MOZ_ASSERT(loopStopPc_);
    return loopStopPc_;
  }

  void setLoopStopPc(jsbytecode* loopStopPc) { loopStopPc_ = loopStopPc; }
};

typedef Vector<CFGBlock*, 4, JitAllocPolicy> CFGBlockVector;

class ControlFlowGraph : public TempObject {
  // A list of blocks in RPO, containing per block a pc-range and
  // a control instruction.
  Vector<CFGBlock, 4, JitAllocPolicy> blocks_;

  explicit ControlFlowGraph(TempAllocator& alloc) : blocks_(alloc) {}

 public:
  static ControlFlowGraph* New(TempAllocator& alloc) {
    return new (alloc) ControlFlowGraph(alloc);
  }

  ControlFlowGraph(const ControlFlowGraph&) = delete;
  void operator=(const ControlFlowGraph&) = delete;

  void dump(GenericPrinter& print, JSScript* script);
  bool init(TempAllocator& alloc, const CFGBlockVector& blocks);

  const CFGBlock* block(size_t i) const { return &blocks_[i]; }

  size_t numBlocks() const { return blocks_.length(); }
};

class ControlFlowGenerator {
  static int CmpSuccessors(const void* a, const void* b);

  JSScript* script;
  CFGBlock* current;
  jsbytecode* pc;
  GSNCache gsn;
  TempAllocator& alloc_;
  CFGBlockVector blocks_;

 public:
  ControlFlowGenerator(const ControlFlowGenerator&) = delete;
  void operator=(const ControlFlowGenerator&) = delete;

  TempAllocator& alloc() { return alloc_; }

  enum class ControlStatus {
    Error,
    Abort,
    Ended,   // There is no continuation/join point.
    Joined,  // Created a join node.
    Jumped,  // Parsing another branch at the same level.
    None     // No control flow.
  };

  struct DeferredEdge : public TempObject {
    CFGBlock* block;
    DeferredEdge* next;

    DeferredEdge(CFGBlock* block, DeferredEdge* next)
        : block(block), next(next) {}
  };

  struct ControlFlowInfo {
    // Entry in the cfgStack.
    uint32_t cfgEntry;

    // Label that continues go to.
    jsbytecode* continuepc;

    ControlFlowInfo(uint32_t cfgEntry, jsbytecode* continuepc)
        : cfgEntry(cfgEntry), continuepc(continuepc) {}
  };

  // To avoid recursion, the bytecode analyzer uses a stack where each entry
  // is a small state machine. As we encounter branches or jumps in the
  // bytecode, we push information about the edges on the stack so that the
  // CFG can be built in a tree-like fashion.
  struct CFGState {
    enum State {
      IF_TRUE,             // if() { }, no else.
      IF_TRUE_EMPTY_ELSE,  // if() { }, empty else
      IF_ELSE_TRUE,        // if() { X } else { }
      IF_ELSE_FALSE,       // if() { } else { X }
      DO_WHILE_LOOP_BODY,  // do { x } while ()
      DO_WHILE_LOOP_COND,  // do { } while (x)
      WHILE_LOOP_COND,     // while (x) { }
      WHILE_LOOP_BODY,     // while () { x }
      FOR_LOOP_COND,       // for (; x;) { }
      FOR_LOOP_BODY,       // for (; ;) { x }
      FOR_LOOP_UPDATE,     // for (; ; x) { }
      TABLE_SWITCH,        // switch() { x }
      COND_SWITCH_CASE,    // switch() { case X: ... }
      COND_SWITCH_BODY,    // switch() { case ...: X }
      AND_OR,              // && x, || x
      LABEL,               // label: x
      TRY                  // try { x } catch(e) { }
    };

    State state;         // Current state of this control structure.
    jsbytecode* stopAt;  // Bytecode at which to stop the processing loop.

    // For if structures, this contains branch information.
    union {
      struct {
        CFGBlock* ifFalse;
        jsbytecode* falseEnd;
        CFGBlock* ifTrue;  // Set when the end of the true path is reached.
        CFGTest* test;
      } branch;
      struct {
        // Common entry point.
        CFGBlock* entry;

        // Position of where the loop body starts and ends.
        jsbytecode* bodyStart;
        jsbytecode* bodyEnd;

        // pc immediately after the loop exits.
        jsbytecode* exitpc;

        // Common exit point. Created lazily, so it may be nullptr.
        CFGBlock* successor;

        // Deferred break and continue targets.
        DeferredEdge* breaks;
        DeferredEdge* continues;

        // Initial state, in case loop processing is restarted.
        State initialState;
        jsbytecode* initialPc;
        jsbytecode* initialStopAt;
        jsbytecode* loopHead;

        // For-loops only.
        jsbytecode* condpc;
        jsbytecode* updatepc;
        jsbytecode* updateEnd;
      } loop;
      struct {
        // Vector of body blocks to process after the cases.
        FixedList<CFGBlock*>* bodies;

        // When processing case statements, this counter points at the
        // last uninitialized body.  When processing bodies, this
        // counter targets the next body to process.
        uint32_t currentIdx;

        // Remember the block index of the default case.
        jsbytecode* defaultTarget;
        uint32_t defaultIdx;

        // Block immediately after the switch.
        jsbytecode* exitpc;
        DeferredEdge* breaks;
      } switch_;
      struct {
        DeferredEdge* breaks;
      } label;
      struct {
        CFGBlock* successor;
      } try_;
    };

    inline bool isLoop() const {
      switch (state) {
        case DO_WHILE_LOOP_COND:
        case DO_WHILE_LOOP_BODY:
        case WHILE_LOOP_COND:
        case WHILE_LOOP_BODY:
        case FOR_LOOP_COND:
        case FOR_LOOP_BODY:
        case FOR_LOOP_UPDATE:
          return true;
        default:
          return false;
      }
    }

    static CFGState If(jsbytecode* join, CFGTest* test);
    static CFGState IfElse(jsbytecode* trueEnd, jsbytecode* falseEnd,
                           CFGTest* test);
    static CFGState AndOr(jsbytecode* join, CFGBlock* lhs);
    static CFGState TableSwitch(TempAllocator& alloc, jsbytecode* exitpc);
    static CFGState CondSwitch(TempAllocator& alloc, jsbytecode* exitpc,
                               jsbytecode* defaultTarget);
    static CFGState Label(jsbytecode* exitpc);
    static CFGState Try(jsbytecode* exitpc, CFGBlock* successor);
  };

  Vector<CFGState, 8, JitAllocPolicy> cfgStack_;
  Vector<ControlFlowInfo, 4, JitAllocPolicy> loops_;
  Vector<ControlFlowInfo, 0, JitAllocPolicy> switches_;
  Vector<ControlFlowInfo, 2, JitAllocPolicy> labels_;
  bool aborted_;
  bool checkedTryFinally_;

 public:
  ControlFlowGenerator(TempAllocator& alloc, JSScript* script);

  MOZ_MUST_USE bool traverseBytecode();
  MOZ_MUST_USE bool addBlock(CFGBlock* block);
  ControlFlowGraph* getGraph(TempAllocator& alloc) {
    ControlFlowGraph* cfg = ControlFlowGraph::New(alloc);
    if (!cfg) {
      return nullptr;
    }
    if (!cfg->init(alloc, blocks_)) {
      return nullptr;
    }
    return cfg;
  }

  bool aborted() const { return aborted_; }

 private:
  void popCfgStack();
  MOZ_MUST_USE bool processDeferredContinues(CFGState& state);
  ControlStatus processControlEnd();
  ControlStatus processCfgStack();
  ControlStatus processCfgEntry(CFGState& state);
  ControlStatus processIfStart(JSOp op);
  ControlStatus processIfEnd(CFGState& state);
  ControlStatus processIfElseTrueEnd(CFGState& state);
  ControlStatus processIfElseFalseEnd(CFGState& state);
  ControlStatus processDoWhileLoop(jssrcnote* sn);
  ControlStatus processDoWhileBodyEnd(CFGState& state);
  ControlStatus processDoWhileCondEnd(CFGState& state);
  ControlStatus processWhileCondEnd(CFGState& state);
  ControlStatus processWhileBodyEnd(CFGState& state);
  ControlStatus processForLoop(JSOp op, jssrcnote* sn);
  ControlStatus processForCondEnd(CFGState& state);
  ControlStatus processForBodyEnd(CFGState& state);
  ControlStatus processForUpdateEnd(CFGState& state);
  ControlStatus processWhileOrForInOrForOfLoop(jssrcnote* sn);
  ControlStatus processNextTableSwitchCase(CFGState& state);
  ControlStatus processCondSwitch();
  ControlStatus processCondSwitchCase(CFGState& state);
  ControlStatus processCondSwitchDefault(CFGState& state);
  ControlStatus processCondSwitchBody(CFGState& state);
  ControlStatus processSwitchBreak(JSOp op);
  ControlStatus processSwitchEnd(DeferredEdge* breaks, jsbytecode* exitpc);
  ControlStatus processTry();
  ControlStatus processTryEnd(CFGState& state);
  ControlStatus processThrow();
  ControlStatus processTableSwitch(JSOp op, jssrcnote* sn);
  ControlStatus processContinue(JSOp op);
  ControlStatus processBreak(JSOp op, jssrcnote* sn);
  ControlStatus processReturn(JSOp op);
  ControlStatus snoopControlFlow(JSOp op);
  ControlStatus processBrokenLoop(CFGState& state);
  ControlStatus finishLoop(CFGState& state, CFGBlock* successor);
  ControlStatus processAndOr(JSOp op);
  ControlStatus processAndOrEnd(CFGState& state);
  ControlStatus processLabel();
  ControlStatus processLabelEnd(CFGState& state);

  MOZ_MUST_USE bool pushLoop(CFGState::State state, jsbytecode* stopAt,
                             CFGBlock* entry, jsbytecode* loopHead,
                             jsbytecode* initialPc, jsbytecode* bodyStart,
                             jsbytecode* bodyEnd, jsbytecode* exitpc,
                             jsbytecode* continuepc);
  void endCurrentBlock(CFGControlInstruction* ins);
  CFGBlock* createBreakCatchBlock(DeferredEdge* edge, jsbytecode* pc);
};

}  // namespace jit
}  // namespace js

#endif /* jit_IonControlFlow_h */