From c27f6040846af22c3fa18800efca362d985e0fd3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Fri, 17 Aug 2018 16:29:24 +0900 Subject: [PATCH] [coco] Introduce Read Hook & Slot (#1075) This commit introduces Read Hook & Slot classes which makes is easy to implement direct Bag reading instruction. Signed-off-by: Jonghyun Park --- contrib/coco/core/include/coco/IR/ReadHook.h | 35 +++++++++++++++++ contrib/coco/core/include/coco/IR/ReadSlot.h | 50 ++++++++++++++++++++++++ contrib/coco/core/src/IR/Read.mock.h | 17 +++++++++ contrib/coco/core/src/IR/ReadHook.cpp | 19 ++++++++++ contrib/coco/core/src/IR/ReadHook.test.cpp | 43 +++++++++++++++++++++ contrib/coco/core/src/IR/ReadSlot.test.cpp | 57 ++++++++++++++++++++++++++++ 6 files changed, 221 insertions(+) create mode 100644 contrib/coco/core/include/coco/IR/ReadHook.h create mode 100644 contrib/coco/core/include/coco/IR/ReadSlot.h create mode 100644 contrib/coco/core/src/IR/Read.mock.h create mode 100644 contrib/coco/core/src/IR/ReadHook.cpp create mode 100644 contrib/coco/core/src/IR/ReadHook.test.cpp create mode 100644 contrib/coco/core/src/IR/ReadSlot.test.cpp diff --git a/contrib/coco/core/include/coco/IR/ReadHook.h b/contrib/coco/core/include/coco/IR/ReadHook.h new file mode 100644 index 0000000..87a2e9f --- /dev/null +++ b/contrib/coco/core/include/coco/IR/ReadHook.h @@ -0,0 +1,35 @@ +#ifndef __COCO_IR_READ_HOOK_H__ +#define __COCO_IR_READ_HOOK_H__ + +#include "coco/IR/Bag.h" +#include "coco/IR/BagInfo.forward.h" + +#include "coco/ADT/PtrLink.h" + +namespace coco +{ + +class ReadHook final +{ +public: + ReadHook(const PtrLink *bag_link, Bag::Read *read) + : _bag_link{bag_link}, _read{read} + { + // DO NOTHING + } + +private: + BagInfo *info(Bag *bag) const; + +public: + void onTake(Bag *bag); + void onRelease(Bag *bag); + +private: + const PtrLink *const _bag_link; + Bag::Read *const _read; +}; + +} // namespace coco + +#endif // __COCO_IR_READ_HOOK_H__ diff --git a/contrib/coco/core/include/coco/IR/ReadSlot.h b/contrib/coco/core/include/coco/IR/ReadSlot.h new file mode 100644 index 0000000..eb4f1a6 --- /dev/null +++ b/contrib/coco/core/include/coco/IR/ReadSlot.h @@ -0,0 +1,50 @@ +#ifndef __COCO_IR_READ_SLOT_H__ +#define __COCO_IR_READ_SLOT_H__ + +#include "coco/IR/ReadHook.h" + +#include + +namespace coco +{ + +template class ReadSlot final +{ +public: + ReadSlot(const PtrLink *bag_link, Bag::Read *read) + : _hook{bag_link, read}, _value{nullptr} + { + // DO NOTHING + } + +public: + T *value(void) const { return _value; } + +public: + void value(T *value) + { + if (_value) + { + _hook.onRelease(_value); + _value = nullptr; + } + + assert(_value == nullptr); + + if (value) + { + _value = value; + _hook.onTake(_value); + } + + assert(_value == value); + } + +private: + ReadHook _hook; + T *_value; +}; + +} // namespace coco + +#endif // __COCO_IR_READ_SLOT_H__ diff --git a/contrib/coco/core/src/IR/Read.mock.h b/contrib/coco/core/src/IR/Read.mock.h new file mode 100644 index 0000000..3d7fa62 --- /dev/null +++ b/contrib/coco/core/src/IR/Read.mock.h @@ -0,0 +1,17 @@ +#ifndef __COCO_IR_READ_MOCK_H__ +#define __COCO_IR_READ_MOCK_H__ + +#include "coco/IR/Bag.h" + +namespace +{ +namespace mock +{ +struct Read final : public coco::Bag::Read +{ + coco::Instr *loc(void) override { return nullptr; } +}; +} // namespace mock +} // namespace + +#endif // __COCO_IR_READ_MOCK_H__ diff --git a/contrib/coco/core/src/IR/ReadHook.cpp b/contrib/coco/core/src/IR/ReadHook.cpp new file mode 100644 index 0000000..31929db --- /dev/null +++ b/contrib/coco/core/src/IR/ReadHook.cpp @@ -0,0 +1,19 @@ +#include "coco/IR/ReadHook.h" +#include "coco/IR/BagInfo.h" + +#include + +namespace coco +{ + +BagInfo *ReadHook::info(Bag *bag) const +{ + auto info = _bag_link->find(bag); + assert(info != nullptr); + return info; +} + +void ReadHook::onTake(Bag *bag) { info(bag)->reads()->insert(_read); } +void ReadHook::onRelease(Bag *bag) { info(bag)->reads()->erase(_read); } + +} // namespace coco diff --git a/contrib/coco/core/src/IR/ReadHook.test.cpp b/contrib/coco/core/src/IR/ReadHook.test.cpp new file mode 100644 index 0000000..246e270 --- /dev/null +++ b/contrib/coco/core/src/IR/ReadHook.test.cpp @@ -0,0 +1,43 @@ +#include "coco/IR/ReadHook.h" + +#include "coco/IR/BagInfo.h" +#include "coco/IR/BagManager.h" + +#include "Read.mock.h" + +#include + +namespace +{ +class ReadHookTest : public ::testing::Test +{ +protected: + coco::PtrLink bag_link; + + coco::BagManager bag_mgr{&bag_link}; +}; +} // namespace + +TEST_F(ReadHookTest, TakeAndRelease) +{ + auto bag = bag_mgr.create(16); + + ::mock::Read read; + + coco::ReadHook hook{&bag_link, &read}; + + hook.onTake(bag); + { + auto reads = bag->reads(); + + ASSERT_EQ(reads.size(), 1); + ASSERT_NE(reads.find(&read), reads.end()); + } + + hook.onRelease(bag); + { + auto reads = bag->reads(); + + ASSERT_EQ(reads.size(), 0); + } +} diff --git a/contrib/coco/core/src/IR/ReadSlot.test.cpp b/contrib/coco/core/src/IR/ReadSlot.test.cpp new file mode 100644 index 0000000..1b01651 --- /dev/null +++ b/contrib/coco/core/src/IR/ReadSlot.test.cpp @@ -0,0 +1,57 @@ +#include "coco/IR/ReadSlot.h" +#include "coco/IR/BagInfo.h" +#include "coco/IR/BagManager.h" + +#include "Read.mock.h" + +#include + +namespace +{ +class ReadSlotTest : public ::testing::Test +{ +protected: + coco::PtrLink bag_link; + + coco::BagManager bag_mgr{&bag_link}; +}; +} // namespace + +TEST_F(ReadSlotTest, constructor) +{ + ::mock::Read read; + + coco::ReadSlot slot{&bag_link, &read}; + + ASSERT_EQ(slot.value(), nullptr); +} + +TEST_F(ReadSlotTest, value) +{ + ::mock::Read read; + + coco::ReadSlot slot{&bag_link, &read}; + + auto bag = bag_mgr.create(16); + + slot.value(bag); + + ASSERT_EQ(slot.value(), bag); + + { + auto reads = bag->reads(); + + ASSERT_EQ(reads.size(), 1); + ASSERT_NE(reads.find(&read), reads.end()); + } + + slot.value(nullptr); + + { + auto reads = bag->reads(); + + ASSERT_EQ(slot.value(), nullptr); + + ASSERT_EQ(reads.size(), 0); + } +} -- 2.7.4