#include "coco/IR/Bag.h"
#include "coco/IR/Block.forward.h"
#include "coco/IR/Instr.forward.h"
+#include "coco/IR/InstrIndex.h"
#include "coco/IR/Entity.h"
#include "coco/ADT/DLinkedList.h"
class Instr : public coco::DLinkedList<Instr, Block>::Node, public Entity
{
public:
+ friend void DLinkedList<Instr, Block>::joined(Block *, Instr *);
+ friend void DLinkedList<Instr, Block>::leaving(Block *, Instr *);
+
+public:
Instr() = default;
public:
* TODO Make this method as a pure virtual method
*/
virtual void dispose(void) { return; }
+
+public:
+ const InstrIndex &index(void) const { return _index; }
+
+private:
+ InstrIndex _index;
};
} // namespace coco
namespace coco
{
-template <> void DLinkedList<Instr, Block>::joined(Block *, Instr *)
+template <> void DLinkedList<Instr, Block>::joined(Block *, Instr *curr_ins)
{
- // TODO Implement this
+ assert(!curr_ins->index().valid());
+ uint32_t value = 0;
+
+ if (auto prev_ins = curr_ins->prev())
+ {
+ value = prev_ins->index().value() + 1;
+ }
+
+ for (auto ins = curr_ins; ins; ins = ins->next())
+ {
+ ins->_index.set(value++);
+ }
}
-template <> void DLinkedList<Instr, Block>::leaving(Block *, Instr *)
+template <> void DLinkedList<Instr, Block>::leaving(Block *, Instr *curr_ins)
{
- // TODO Implement this
+ assert(curr_ins->index().valid());
+ uint32_t value = curr_ins->index().value();
+
+ for (auto ins = curr_ins->next(); ins; ins = ins->next())
+ {
+ ins->_index.set(value++);
+ }
+
+ curr_ins->_index.reset();
}
template <> InstrList *DLinkedList<Instr, Block>::head(Block *b) { return b->instr(); }
ASSERT_EQ(ins_2->parent(), blk);
ASSERT_EQ(ins_2->prev(), ins_1);
ASSERT_EQ(ins_2->next(), nullptr);
+
+ ASSERT_EQ(ins_1->index().value(), 0);
+ ASSERT_EQ(ins_2->index().value(), 1);
}
TEST(IR_MODULE, iterate_constant_block)