#include "coco/IR/Module.forward.h"
#include "coco/IR/Block.forward.h"
+#include "coco/IR/BlockIndex.h"
#include "coco/IR/Instr.h"
#include "coco/IR/Entity.h"
class Block final : public DLinkedList<Block, Module>::Node, public Entity
{
public:
+ friend void DLinkedList<Block, Module>::joined(Module *, Block *);
+ friend void DLinkedList<Block, Module>::leaving(Module *, Block *);
+
+public:
Block() : _instr{this}
{
// DO NOTHING
InstrList *instr(void) { return &_instr; }
const InstrList *instr(void) const { return &_instr; }
+public:
+ const BlockIndex &index(void) const { return _index; }
+
private:
+ BlockIndex _index;
DLinkedList<Instr, Block>::Head _instr;
};
#include "coco/IR/Block.h"
#include "coco/IR/Module.h"
+#include <cassert>
+
namespace coco
{
-template <> void DLinkedList<Block, Module>::joined(Module *, Block *)
+template <> void DLinkedList<Block, Module>::joined(Module *, Block *curr_blk)
{
- // TODO Implement this
+ assert(!curr_blk->index().valid());
+ uint32_t value = 0;
+
+ if (auto prev_blk = curr_blk->prev())
+ {
+ value = prev_blk->index().value() + 1;
+ }
+
+ for (auto blk = curr_blk; blk; blk = blk->next())
+ {
+ blk->_index.set(value++);
+ }
}
-template <> void DLinkedList<Block, Module>::leaving(Module *, Block *)
+template <> void DLinkedList<Block, Module>::leaving(Module *, Block *curr_blk)
{
- // TODO Implement this
+ assert(curr_blk->index().valid());
+ uint32_t value = curr_blk->index().value();
+
+ for (auto blk = curr_blk->next(); blk; blk = blk->next())
+ {
+ blk->_index.set(value++);
+ }
+
+ curr_blk->_index.reset();
}
template <> BlockList *DLinkedList<Block, Module>::head(Module *m) { return m->block(); }
ASSERT_EQ(blk_2->prev(), blk_1);
ASSERT_EQ(blk_2->next(), nullptr);
+
+ ASSERT_EQ(blk_1->index().value(), 0);
+ ASSERT_EQ(blk_2->index().value(), 1);
}
TEST(IR_MODULE, append_two_instrs)