nany
sequence.h
1 #pragma once
2 #include "libnanyc.h"
3 #include <yuni/core/string.h>
4 #include "details/ir/isa/opcodes.h"
5 #include "details/ir/isa/data.h"
6 #include "details/ir/instruction.h"
7 #include "details/utils/stringrefs.h"
8 #include "details/utils/clid.h"
9 #include <memory>
10 #include <cassert>
11 
12 #ifdef alloca
13 # undef alloca
14 #endif
15 
16 
17 
18 namespace ny {
19 namespace ir {
20 
21 
22 struct Sequence final {
23  Sequence() = default;
24  Sequence(const Sequence&) = delete;
25  Sequence(const Sequence&, uint32_t offset);
26  ~Sequence();
27 
29 
30  bool isCursorValid(const Instruction& instr) const;
32 
34  void invalidateCursor(const Instruction*& cusror) const;
36  void invalidateCursor(Instruction*& cusror) const;
37 
39  bool jumpToLabelForward(const Instruction*& cursor, uint32_t label) const;
41  bool jumpToLabelBackward(const Instruction*& cursor, uint32_t label) const;
42 
44  void moveCursorFromBlueprintToEnd(Instruction*& cursor) const;
46  void moveCursorFromBlueprintToEnd(const Instruction*& cursor) const;
48 
50 
51  template<isa::Op O> isa::Operand<O>& at(uint32_t offset);
54  template<isa::Op O> const isa::Operand<O>& at(uint32_t offset) const;
56  const Instruction& at(uint32_t offset) const;
58  Instruction& at(uint32_t offset);
59 
61  template<isa::Op O> isa::Operand<O>& emit();
62 
64  template<isa::Op O> uint32_t offsetOf(const isa::Operand<O>& instr) const;
66  uint32_t offsetOf(const Instruction& instr) const;
68 
70 
71  template<class T> void each(T& visitor, uint32_t offset = 0);
74  template<class T> void each(T& visitor, uint32_t offset = 0) const;
76 
78 
79 
87  void increaseAllLVID(uint32_t inc, uint32_t greaterThan, uint32_t offset = 0);
89 
91 
92  uint32_t opcodeCount() const;
95  uint32_t capacity() const;
96 
98  void clear();
100  void reserve(uint32_t count);
102 
104 
105  void print(Yuni::String& out, const AtomMap* = nullptr, uint32_t offset = 0) const;
108 
110 
111  Sequence& operator = (const Sequence&) = delete;
114 
115 public:
118 
119 private:
120  void grow(uint32_t instrCount);
121 
122 private:
124  uint32_t m_size = 0u;
126  uint32_t m_capacity = 0u;
128  Instruction* m_body = nullptr;
129 
130 }; // Sequence
131 
132 
133 } // namespace ir
134 } // namespace ny
135 
136 #include "sequence.hxx"
void print(Yuni::String &out, const AtomMap *=nullptr, uint32_t offset=0) const
Print the sequence to a string.
Definition: sequence.cpp:100
isa::Operand< O > & at(uint32_t offset)
Fetch an instruction at a given offset.
Definition: sequence.hxx:42
Definition: ast.cpp:6
void each(T &visitor, uint32_t offset=0)
Visit each instruction.
Definition: sequence.hxx:127
StringRefs stringrefs
All strings.
Definition: sequence.h:117
void increaseAllLVID(uint32_t inc, uint32_t greaterThan, uint32_t offset=0)
Iterate through all opcodes and increment all lvid.
Definition: sequence.cpp:181
void reserve(uint32_t count)
Reserve enough memory for N instructions.
Definition: sequence.hxx:14
bool jumpToLabelBackward(const Instruction *&cursor, uint32_t label) const
Go to a previous label.
Definition: sequence.hxx:110
isa::Operand< O > & emit()
emit a new Instruction
Definition: sequence.hxx:159
uint32_t opcodeCount() const
Get how many instructions the sequence has.
Definition: sequence.hxx:20
Atoms.
Definition: atom-map.h:17
void moveCursorFromBlueprintToEnd(Instruction *&cursor) const
Move the cursor at the end of the blueprint.
Definition: sequence.cpp:65
Definition: instruction.h:12
bool jumpToLabelForward(const Instruction *&cursor, uint32_t label) const
Go to the next label.
Definition: sequence.hxx:93
uint32_t capacity() const
Get the capacity of the sequence (in instructions)
Definition: sequence.hxx:25
bool isCursorValid(const Instruction &instr) const
Get if a cursor is valid.
Definition: sequence.hxx:69
Definition: data.h:85
uint32_t offsetOf(const isa::Operand< O > &instr) const
Get the offset of an instruction within the sequence.
Definition: sequence.hxx:77
void invalidateCursor(const Instruction *&cusror) const
Get the upper limit.
Definition: sequence.hxx:83
Sequence & operator=(const Sequence &)=delete
Copy operator.
Definition: sequence.h:22
Container for minimizing memory use of duplicate strings.
Definition: stringrefs.h:12
void clear()
Clear the sequence.
Definition: sequence.cpp:80