Nearly CC
An educational compiler skeleton
instruction_seq.h
Go to the documentation of this file.
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_H
22 #define INSTRUCTION_SEQ_H
23 
24 #include <vector>
25 #include <string>
26 #include <map>
27 #include "instruction_seq_iter.h"
28 
33 
34 class Instruction;
35 
43 };
44 
51 private:
52  struct Slot {
53  std::string label;
54  Instruction *ins;
55  };
56 
57  std::vector<Slot> m_instructions;
58  std::map<std::string, unsigned> m_label_map; // label to instruction index map
59  std::string m_next_label;
60 
61  // These fields are only used when the InstructionSequence
62  // is used as a basic block in a ControlFlowGraph
63  BasicBlockKind m_kind;
64  unsigned m_block_id;
65  int m_code_order;
66 
67  // copy constructor and assignment operator are not allowed
69  InstructionSequence &operator=(const InstructionSequence &);
70 
71 public:
72  // Iterator types, providing a pointer to an Instruction object when
73  // dereferenced. These are random access.
74 
77 
80 
83 
90  InstructionSequence(BasicBlockKind kind, int code_order, const std::string &block_label);
91 
92  virtual ~InstructionSequence();
93 
97 
98  // get begin and end const_iterators
99 
102  const_iterator cbegin() const { return const_iterator(m_instructions.cbegin()); }
103 
106  const_iterator cend() const { return const_iterator(m_instructions.cend()); }
107 
108  // get begin and end const_reverse_iterators
109 
112  const_reverse_iterator crbegin() const { return const_reverse_iterator(m_instructions.crbegin()); }
113 
116  const_reverse_iterator crend() const { return const_reverse_iterator(m_instructions.crend()); }
117 
118  // Apply a function to each Instruction in order.
121  template<typename Fn>
122  void apply_to_all(Fn f) {
123  for (auto i = cbegin(); i != cend(); ++i)
124  f(*i);
125  }
126 
131  void append(Instruction *ins);
132 
135  unsigned get_length() const;
136 
140  Instruction *get_instruction(unsigned index) const;
141 
145 
151  void define_label(const std::string &label);
152 
156  bool has_label(unsigned index) const { return !m_instructions.at(index).label.empty(); }
157 
162  std::string get_label_at_index(unsigned index) const { return m_instructions.at(index).label; }
163 
167  bool has_label(const_iterator iter) const { return iter.has_label(); }
168 
175  bool has_label_at_end() const;
176 
183  const_iterator get_iterator_at_labeled_position(const std::string &label) const;
184 
190  Instruction *find_labeled_instruction(const std::string &label) const;
191 
196  unsigned get_index_of_labeled_instruction(const std::string &label) const;
197 
198  // Member functions used for basic blocks
199 
204  BasicBlockKind get_kind() const { return m_kind; }
205 
208  void set_kind(BasicBlockKind kind) { m_kind = kind; }
209 
215  bool has_block_label() const;
216 
219  std::string get_block_label() const;
220 
225  void set_block_label(const std::string &block_label);
226 
232  unsigned get_block_id() const { return m_block_id; }
233 
236  void set_block_id(unsigned block_id) { m_block_id = block_id; }
237 
242  int get_code_order() const { return m_code_order; }
243 
246  void set_code_order(int code_order) { m_code_order = code_order; }
247 };
248 
249 #endif // INSTRUCTION_SEQ_H
Definition: instruction_seq_iter.h:37
InstructionSequence class, which represents a linear sequence of Instruction objects.
Definition: instruction_seq.h:50
InstructionSequence * duplicate() const
Return a dynamically-allocated duplicate of this InstructionSequence.
Definition: instruction_seq.cpp:47
const_iterator cend() const
Get end forward iterator over the instructions.
Definition: instruction_seq.h:106
unsigned get_block_id() const
Get the basic block id.
Definition: instruction_seq.h:232
const_iterator get_iterator_at_labeled_position(const std::string &label) const
Return a forward const iterator positioned at the instruction with the specified label,...
Definition: instruction_seq.cpp:92
void set_kind(BasicBlockKind kind)
Set the BasicBlockKind.
Definition: instruction_seq.h:208
void set_block_label(const std::string &block_label)
Set the label on the first Instruction.
Definition: instruction_seq.cpp:126
ISeqIterator< std::vector< Slot >::const_iterator > const_iterator
Iterator over instructions in forward order.
Definition: instruction_seq.h:76
unsigned get_index_of_labeled_instruction(const std::string &label) const
Return the index of instruction labeled with the specified label.
Definition: instruction_seq.cpp:105
const_iterator cbegin() const
Get begin forward iterator over the instructions.
Definition: instruction_seq.h:102
void define_label(const std::string &label)
Define a label.
Definition: instruction_seq.cpp:83
std::string get_label_at_index(unsigned index) const
Get label of the Instruction at the specified index.
Definition: instruction_seq.h:162
bool has_label_at_end() const
Determine if the InstructionSequence has a label at the end This can happen in code generation for co...
Definition: instruction_seq.cpp:88
void append(Instruction *ins)
Append an Instruction.
Definition: instruction_seq.cpp:61
const_reverse_iterator crbegin() const
Get begin reverse iterator over the instructions.
Definition: instruction_seq.h:112
BasicBlockKind get_kind() const
Get the BasicBlockKind.
Definition: instruction_seq.h:204
bool has_label(const_iterator iter) const
Determine if Instruction referred to by specified iterator has a label.
Definition: instruction_seq.h:167
bool has_label(unsigned index) const
Determine if Instruction at given index has a label.
Definition: instruction_seq.h:156
std::string get_block_label() const
Get the label of the first Instruction.
Definition: instruction_seq.cpp:119
Instruction * get_last_instruction() const
Get the last Instruction.
Definition: instruction_seq.cpp:78
Instruction * get_instruction(unsigned index) const
Get Instruction at specified index.
Definition: instruction_seq.cpp:74
bool has_block_label() const
Check whether the first Instruction is labeled.
Definition: instruction_seq.cpp:112
Instruction * find_labeled_instruction(const std::string &label) const
Find Instruction labeled with specified label.
Definition: instruction_seq.cpp:100
void set_code_order(int code_order)
Set the code order value of this block.
Definition: instruction_seq.h:246
void apply_to_all(Fn f)
Definition: instruction_seq.h:122
const_reverse_iterator crend() const
Get end reverse iterator over the instructions.
Definition: instruction_seq.h:116
ISeqIterator< std::vector< Slot >::const_reverse_iterator > const_reverse_iterator
Iterator over instructions in reverse order.
Definition: instruction_seq.h:79
unsigned get_length() const
Get number of instructions.
Definition: instruction_seq.cpp:70
void set_block_id(unsigned block_id)
Set the basic block id.
Definition: instruction_seq.h:236
int get_code_order() const
Get the code order value of this block.
Definition: instruction_seq.h:242
InstructionSequence()
Default constructor.
Definition: instruction_seq.cpp:26
Instruction object type.
Definition: instruction.h:31
BasicBlockKind
Kinds of basic blocks.
Definition: instruction_seq.h:39
@ BASICBLOCK_INTERIOR
normal basic block in the "interior" of the CFG
Definition: instruction_seq.h:42
@ BASICBLOCK_ENTRY
special "entry" block
Definition: instruction_seq.h:40
@ BASICBLOCK_EXIT
special "exit" block
Definition: instruction_seq.h:41