Nearly CC
An educational compiler skeleton
instruction_seq_iter.h
1 // Copyright (c) 2021-2023, David H. Hovemeyer <david.hovemeyer@gmail.com>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
17 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19 // OTHER DEALINGS IN THE SOFTWARE.
20 
21 #ifndef INSTRUCTION_SEQ_ITER_H
22 #define INSTRUCTION_SEQ_ITER_H
23 
24 #include <cstddef> // for ptrdiff_t
25 
26 class Instruction;
27 
28 // Generic const_iterator type for InstructionSequence.
29 // When dereferenced, provides a pointer to the referenced Instruction
30 // object. The has_label() and get_label() member functions can
31 // be used (respectively) to determine if the referenced Instruction
32 // has a label and to access the label. It is parametized with the
33 // underlying vector const iterator type, to allow forward and reverse
34 // versions to be defined easily. It supports random access
35 // (adding or subtracting a signed integer offset.)
36 template<typename It>
37 class ISeqIterator {
38 private:
39  It slot_iter;
40 
41 public:
42  ISeqIterator() { }
43 
44  ISeqIterator(It i) : slot_iter(i) { }
45 
46  ISeqIterator(const ISeqIterator<It> &other) : slot_iter(other.slot_iter) { }
47 
48  ISeqIterator<It> &operator=(const ISeqIterator<It> &rhs) {
49  if (this != &rhs) { slot_iter = rhs.slot_iter; }
50  return *this;
51  }
52 
53  // Equality and inequality comparisons
54 
55  bool operator==(const ISeqIterator<It> &rhs) const {
56  return slot_iter == rhs.slot_iter;
57  }
58 
59  bool operator!=(const ISeqIterator<It> &rhs) const {
60  return slot_iter != rhs.slot_iter;
61  }
62 
63  // Dereference
64  Instruction* operator*() const { return slot_iter->ins; }
65 
66  // Access to the referenced Instruction's label
67  bool has_label() const { return !slot_iter->label.empty(); }
68  std::string get_label() const { return slot_iter->label; }
69 
70  // Increment and decrement
71 
72  ISeqIterator<It> &operator++() {
73  ++slot_iter;
74  return *this;
75  }
76 
77  ISeqIterator<It> operator++(int) {
78  ISeqIterator<It> copy(*this);
79  ++slot_iter;
80  return copy;
81  }
82 
83  ISeqIterator<It> &operator--() {
84  --slot_iter;
85  return *this;
86  }
87 
88  ISeqIterator<It> operator--(int) {
89  ISeqIterator<It> copy(*this);
90  --slot_iter;
91  return copy;
92  }
93 
94  // Support
95  // - adding and subtracting integer values
96  // - computing pointer difference between iterators
97  // - relational operators
98  // so that instruction sequence iterators are random access
99 
100  ISeqIterator<It> operator+(ptrdiff_t i) const {
101  return { slot_iter + i };
102  }
103 
104  ISeqIterator<It> operator-(ptrdiff_t i) const {
105  return { slot_iter - i };
106  }
107 
108  ISeqIterator<It> &operator+=(ptrdiff_t i) {
109  slot_iter += i;
110  return *this;
111  }
112 
113  ISeqIterator<It> &operator-=(ptrdiff_t i) {
114  slot_iter -= i;
115  return *this;
116  }
117 
118  ptrdiff_t operator-(ISeqIterator<It> rhs) {
119  return slot_iter - rhs.slot_iter;
120  }
121 
122  bool operator<(ISeqIterator<It> rhs) const {
123  return slot_iter < rhs.slot_iter;
124  }
125 
126  bool operator<=(ISeqIterator<It> rhs) const {
127  return slot_iter <= rhs.slot_iter;
128  }
129 
130  bool operator>(ISeqIterator<It> rhs) const {
131  return slot_iter > rhs.slot_iter;
132  }
133 
134  bool operator>=(ISeqIterator<It> rhs) const {
135  return slot_iter >= rhs.slot_iter;
136  }
137 };
138 
139 #endif // INSTRUCTION_SEQ_ITER_H
Definition: instruction_seq_iter.h:37
Instruction object type.
Definition: instruction.h:31