summaryrefslogtreecommitdiff
path: root/js/src/jit/InlineList.h
blob: 72eb98d987926af94c014877a8168e819370c9fe (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * 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_InlineList_h
#define jit_InlineList_h

#include "jsutil.h"

namespace js {

template <typename T> class InlineForwardList;
template <typename T> class InlineForwardListIterator;

template <typename T>
class InlineForwardListNode
{
  public:
    InlineForwardListNode() : next(nullptr)
    { }
    explicit InlineForwardListNode(InlineForwardListNode<T>* n) : next(n)
    { }

    InlineForwardListNode(const InlineForwardListNode<T>&) = delete;

  protected:
    friend class InlineForwardList<T>;
    friend class InlineForwardListIterator<T>;

    InlineForwardListNode<T>* next;
};

template <typename T>
class InlineForwardList : protected InlineForwardListNode<T>
{
    friend class InlineForwardListIterator<T>;

    typedef InlineForwardListNode<T> Node;

    Node* tail_;
#ifdef DEBUG
    int modifyCount_;
#endif

    InlineForwardList<T>* thisFromConstructor() {
        return this;
    }

  public:
    InlineForwardList()
      : tail_(thisFromConstructor())
    {
#ifdef DEBUG
        modifyCount_ = 0;
#endif
    }

  public:
    typedef InlineForwardListIterator<T> iterator;

  public:
    iterator begin() const {
        return iterator(this);
    }
    iterator begin(Node* item) const {
        return iterator(this, item);
    }
    iterator end() const {
        return iterator(nullptr);
    }
    void removeAt(iterator where) {
        removeAfter(where.prev, where.iter);
    }
    void pushFront(Node* t) {
        insertAfter(this, t);
    }
    void pushBack(Node* t) {
        MOZ_ASSERT(t->next == nullptr);
#ifdef DEBUG
        modifyCount_++;
#endif
        tail_->next = t;
        tail_ = t;
    }
    T* popFront() {
        MOZ_ASSERT(!empty());
        T* result = static_cast<T*>(this->next);
        removeAfter(this, result);
        return result;
    }
    T* back() const {
        MOZ_ASSERT(!empty());
        return static_cast<T*>(tail_);
    }
    void insertAfter(Node* at, Node* item) {
        MOZ_ASSERT(item->next == nullptr);
#ifdef DEBUG
        modifyCount_++;
#endif
        if (at == tail_)
            tail_ = item;
        item->next = at->next;
        at->next = item;
    }
    void removeAfter(Node* at, Node* item) {
#ifdef DEBUG
        modifyCount_++;
#endif
        if (item == tail_)
            tail_ = at;
        MOZ_ASSERT(at->next == item);
        at->next = item->next;
        item->next = nullptr;
    }
    void removeAndIncrement(iterator &where) {
        // Do not change modifyCount_ here. The iterator can still be used
        // after calling this method, unlike the other methods that modify
        // the list.
        Node* item = where.iter;
        where.iter = item->next;
        if (item == tail_)
            tail_ = where.prev;
        MOZ_ASSERT(where.prev->next == item);
        where.prev->next = where.iter;
        item->next = nullptr;
    }
    void splitAfter(Node* at, InlineForwardList<T>* to) {
        MOZ_ASSERT(to->empty());
        if (!at)
            at = this;
        if (at == tail_)
            return;
#ifdef DEBUG
        modifyCount_++;
#endif
        to->next = at->next;
        to->tail_ = tail_;
        tail_ = at;
        at->next = nullptr;
    }
    bool empty() const {
        return tail_ == this;
    }
    void clear() {
        this->next = nullptr;
        tail_ = this;
#ifdef DEBUG
        modifyCount_ = 0;
#endif
    }
};

template <typename T>
class InlineForwardListIterator
{
private:
    friend class InlineForwardList<T>;

    typedef InlineForwardListNode<T> Node;

    explicit InlineForwardListIterator<T>(const InlineForwardList<T>* owner)
      : prev(const_cast<Node*>(static_cast<const Node*>(owner))),
        iter(owner ? owner->next : nullptr)
#ifdef DEBUG
      , owner_(owner),
        modifyCount_(owner ? owner->modifyCount_ : 0)
#endif
    { }

    InlineForwardListIterator<T>(const InlineForwardList<T>* owner, Node* node)
      : prev(nullptr),
        iter(node)
#ifdef DEBUG
      , owner_(owner),
        modifyCount_(owner ? owner->modifyCount_ : 0)
#endif
    { }

public:
    InlineForwardListIterator<T> & operator ++() {
        MOZ_ASSERT(modifyCount_ == owner_->modifyCount_);
        prev = iter;
        iter = iter->next;
        return *this;
    }
    InlineForwardListIterator<T> operator ++(int) {
        InlineForwardListIterator<T> old(*this);
        operator++();
        return old;
    }
    T * operator*() const {
        MOZ_ASSERT(modifyCount_ == owner_->modifyCount_);
        return static_cast<T*>(iter);
    }
    T * operator ->() const {
        MOZ_ASSERT(modifyCount_ == owner_->modifyCount_);
        return static_cast<T*>(iter);
    }
    bool operator !=(const InlineForwardListIterator<T>& where) const {
        return iter != where.iter;
    }
    bool operator ==(const InlineForwardListIterator<T>& where) const {
        return iter == where.iter;
    }
    explicit operator bool() const {
        return iter != nullptr;
    }

private:
    Node* prev;
    Node* iter;

#ifdef DEBUG
    const InlineForwardList<T>* owner_;
    int modifyCount_;
#endif
};

template <typename T> class InlineList;
template <typename T> class InlineListIterator;
template <typename T> class InlineListReverseIterator;

template <typename T>
class InlineListNode : public InlineForwardListNode<T>
{
  public:
    InlineListNode() : InlineForwardListNode<T>(nullptr), prev(nullptr)
    { }
    InlineListNode(InlineListNode<T>* n, InlineListNode<T>* p)
      : InlineForwardListNode<T>(n),
        prev(p)
    { }

    // Move constructor. Nodes may be moved without being removed from their
    // containing lists. For example, this allows list nodes to be safely
    // stored in a resizable Vector -- when the Vector resizes, the new storage
    // is initialized by this move constructor. |other| is a reference to the
    // old node which the |this| node here is replacing.
    InlineListNode(InlineListNode<T>&& other)
      : InlineForwardListNode<T>(other.next)
    {
        InlineListNode<T>* newNext = static_cast<InlineListNode<T>*>(other.next);
        InlineListNode<T>* newPrev = other.prev;
        prev = newPrev;

        // Update the pointers in the adjacent nodes to point to this node's new
        // location.
        newNext->prev = this;
        newPrev->next = this;
    }

    InlineListNode(const InlineListNode<T>&) = delete;
    void operator=(const InlineListNode<T>&) = delete;

  protected:
    friend class InlineList<T>;
    friend class InlineListIterator<T>;
    friend class InlineListReverseIterator<T>;

    InlineListNode<T>* prev;
};

template <typename T>
class InlineList : protected InlineListNode<T>
{
    typedef InlineListNode<T> Node;

  public:
    InlineList() : InlineListNode<T>(this, this)
    { }

  public:
    typedef InlineListIterator<T> iterator;
    typedef InlineListReverseIterator<T> reverse_iterator;

  public:
    iterator begin() const {
        return iterator(static_cast<Node*>(this->next));
    }
    iterator begin(Node* t) const {
        return iterator(t);
    }
    iterator end() const {
        return iterator(this);
    }
    reverse_iterator rbegin() const {
        return reverse_iterator(this->prev);
    }
    reverse_iterator rbegin(Node* t) const {
        return reverse_iterator(t);
    }
    reverse_iterator rend() const {
        return reverse_iterator(this);
    }
    void pushFront(Node* t) {
        insertAfter(this, t);
    }
    void pushFrontUnchecked(Node* t) {
        insertAfterUnchecked(this, t);
    }
    void pushBack(Node* t) {
        insertBefore(this, t);
    }
    void pushBackUnchecked(Node* t) {
        insertBeforeUnchecked(this, t);
    }
    T* popFront() {
        MOZ_ASSERT(!empty());
        T* t = static_cast<T*>(this->next);
        remove(t);
        return t;
    }
    T* popBack() {
        MOZ_ASSERT(!empty());
        T* t = static_cast<T*>(this->prev);
        remove(t);
        return t;
    }
    T* peekBack() const {
        iterator iter = end();
        iter--;
        return *iter;
    }
    void insertBefore(Node* at, Node* item) {
        MOZ_ASSERT(item->prev == nullptr);
        MOZ_ASSERT(item->next == nullptr);
        insertBeforeUnchecked(at, item);
    }
    void insertBeforeUnchecked(Node* at, Node* item) {
        Node* atPrev = at->prev;
        item->next = at;
        item->prev = atPrev;
        atPrev->next = item;
        at->prev = item;
    }
    void insertAfter(Node* at, Node* item) {
        MOZ_ASSERT(item->prev == nullptr);
        MOZ_ASSERT(item->next == nullptr);
        insertAfterUnchecked(at, item);
    }
    void insertAfterUnchecked(Node* at, Node* item) {
        Node* atNext = static_cast<Node*>(at->next);
        item->next = atNext;
        item->prev = at;
        atNext->prev = item;
        at->next = item;
    }
    void remove(Node* t) {
        Node* tNext = static_cast<Node*>(t->next);
        Node* tPrev = t->prev;
        tPrev->next = tNext;
        tNext->prev = tPrev;
        t->next = nullptr;
        t->prev = nullptr;
    }
    // Remove |old| from the list and insert |now| in its place.
    void replace(Node* old, Node* now) {
        MOZ_ASSERT(now->next == nullptr && now->prev == nullptr);
        Node* listNext = static_cast<Node*>(old->next);
        Node* listPrev = old->prev;
        listPrev->next = now;
        listNext->prev = now;
        now->next = listNext;
        now->prev = listPrev;
        old->next = nullptr;
        old->prev = nullptr;
    }
    void clear() {
        this->next = this->prev = this;
    }
    bool empty() const {
        return begin() == end();
    }
    void takeElements(InlineList& l) {
        MOZ_ASSERT(&l != this, "cannot takeElements from this");
        Node* lprev = l.prev;
        static_cast<Node*>(l.next)->prev = this;
        lprev->next = this->next;
        static_cast<Node*>(this->next)->prev = l.prev;
        this->next = l.next;
        l.clear();
    }
};

template <typename T>
class InlineListIterator
{
  private:
    friend class InlineList<T>;

    typedef InlineListNode<T> Node;

    explicit InlineListIterator(const Node* iter)
      : iter(const_cast<Node*>(iter))
    { }

  public:
    InlineListIterator<T> & operator ++() {
        iter = static_cast<Node*>(iter->next);
        return *this;
    }
    InlineListIterator<T> operator ++(int) {
        InlineListIterator<T> old(*this);
        operator++();
        return old;
    }
    InlineListIterator<T> & operator --() {
        iter = iter->prev;
        return *this;
    }
    InlineListIterator<T> operator --(int) {
        InlineListIterator<T> old(*this);
        operator--();
        return old;
    }
    T * operator*() const {
        return static_cast<T*>(iter);
    }
    T * operator ->() const {
        return static_cast<T*>(iter);
    }
    bool operator !=(const InlineListIterator<T>& where) const {
        return iter != where.iter;
    }
    bool operator ==(const InlineListIterator<T>& where) const {
        return iter == where.iter;
    }

  private:
    Node* iter;
};

template <typename T>
class InlineListReverseIterator
{
  private:
    friend class InlineList<T>;

    typedef InlineListNode<T> Node;

    explicit InlineListReverseIterator(const Node* iter)
      : iter(const_cast<Node*>(iter))
    { }

  public:
    InlineListReverseIterator<T> & operator ++() {
        iter = iter->prev;
        return *this;
    }
    InlineListReverseIterator<T> operator ++(int) {
        InlineListReverseIterator<T> old(*this);
        operator++();
        return old;
    }
    InlineListReverseIterator<T> & operator --() {
        iter = static_cast<Node*>(iter->next);
        return *this;
    }
    InlineListReverseIterator<T> operator --(int) {
        InlineListReverseIterator<T> old(*this);
        operator--();
        return old;
    }
    T * operator*() {
        return static_cast<T*>(iter);
    }
    T * operator ->() {
        return static_cast<T*>(iter);
    }
    bool operator !=(const InlineListReverseIterator<T>& where) const {
        return iter != where.iter;
    }
    bool operator ==(const InlineListReverseIterator<T>& where) const {
        return iter == where.iter;
    }

  private:
    Node* iter;
};

/* This list type is more or less exactly an InlineForwardList without a sentinel
 * node. It is useful in cases where you are doing algorithms that deal with many
 * merging singleton lists, rather than often empty ones.
 */
template <typename T> class InlineConcatListIterator;
template <typename T>
class InlineConcatList
{
  private:
    typedef InlineConcatList<T> Node;

    InlineConcatList<T>* thisFromConstructor() {
        return this;
    }

  public:
    InlineConcatList() : next(nullptr), tail(thisFromConstructor())
    { }

    typedef InlineConcatListIterator<T> iterator;

    iterator begin() const {
        return iterator(this);
    }

    iterator end() const {
        return iterator(nullptr);
    }

    void append(InlineConcatList<T>* adding)
    {
        MOZ_ASSERT(tail);
        MOZ_ASSERT(!tail->next);
        MOZ_ASSERT(adding->tail);
        MOZ_ASSERT(!adding->tail->next);

        tail->next = adding;
        tail = adding->tail;
        adding->tail = nullptr;
    }

  protected:
    friend class InlineConcatListIterator<T>;
    Node* next;
    Node* tail;
};

template <typename T>
class InlineConcatListIterator
{
  private:
    friend class InlineConcatList<T>;

    typedef InlineConcatList<T> Node;

    explicit InlineConcatListIterator(const Node* iter)
      : iter(const_cast<Node*>(iter))
    { }

  public:
    InlineConcatListIterator<T> & operator ++() {
        iter = static_cast<Node*>(iter->next);
        return *this;
    }
    InlineConcatListIterator<T> operator ++(int) {
        InlineConcatListIterator<T> old(*this);
        operator++();
        return old;
    }
    T * operator*() const {
        return static_cast<T*>(iter);
    }
    T * operator ->() const {
        return static_cast<T*>(iter);
    }
    bool operator !=(const InlineConcatListIterator<T>& where) const {
        return iter != where.iter;
    }
    bool operator ==(const InlineConcatListIterator<T>& where) const {
        return iter == where.iter;
    }

  private:
    Node* iter;
};

template <typename T> class InlineSpaghettiStack;
template <typename T> class InlineSpaghettiStackNode;
template <typename T> class InlineSpaghettiStackIterator;

template <typename T>
class InlineSpaghettiStackNode : public InlineForwardListNode<T>
{
    typedef InlineForwardListNode<T> Parent;

  public:
    InlineSpaghettiStackNode() : Parent()
    { }

    explicit InlineSpaghettiStackNode(InlineSpaghettiStackNode<T>* n)
      : Parent(n)
    { }

    InlineSpaghettiStackNode(const InlineSpaghettiStackNode<T>&) = delete;

  protected:
    friend class InlineSpaghettiStack<T>;
    friend class InlineSpaghettiStackIterator<T>;
};

template <typename T>
class InlineSpaghettiStack : protected InlineSpaghettiStackNode<T>
{
    friend class InlineSpaghettiStackIterator<T>;

    typedef InlineSpaghettiStackNode<T> Node;

  public:
    InlineSpaghettiStack()
    { }

  public:
    typedef InlineSpaghettiStackIterator<T> iterator;

  public:
    iterator begin() const {
        return iterator(this);
    }
    iterator end() const {
        return iterator(nullptr);
    }

    void push(Node* t) {
        MOZ_ASSERT(t->next == nullptr);
        t->next = this->next;
        this->next = t;
    }

    void copy(const InlineSpaghettiStack<T>& stack) {
        this->next = stack.next;
    }

    bool empty() const {
        return this->next == nullptr;
    }
};

template <typename T>
class InlineSpaghettiStackIterator
{
  private:
    friend class InlineSpaghettiStack<T>;

    typedef InlineSpaghettiStackNode<T> Node;

    explicit InlineSpaghettiStackIterator<T>(const InlineSpaghettiStack<T>* owner)
      : iter(owner ? static_cast<Node*>(owner->next) : nullptr)
    { }

  public:
    InlineSpaghettiStackIterator<T> & operator ++() {
        iter = static_cast<Node*>(iter->next);
        return *this;
    }
    InlineSpaghettiStackIterator<T> operator ++(int) {
        InlineSpaghettiStackIterator<T> old(*this);
        operator++();
        return old;
    }
    T* operator*() const {
        return static_cast<T*>(iter);
    }
    T* operator ->() const {
        return static_cast<T*>(iter);
    }
    bool operator !=(const InlineSpaghettiStackIterator<T>& where) const {
        return iter != where.iter;
    }
    bool operator ==(const InlineSpaghettiStackIterator<T>& where) const {
        return iter == where.iter;
    }

  private:
    Node* iter;
};

} // namespace js

#endif /* jit_InlineList_h */