[coco] Introduce InstrIndex (#1670)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 28 Sep 2018 08:19:48 +0000 (17:19 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 28 Sep 2018 08:19:48 +0000 (17:19 +0900)
This commit introduces InstIndex class which denotes the index of each
instruction (inside a block).

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/coco/core/include/coco/IR/InstrIndex.h [new file with mode: 0644]
contrib/coco/core/src/IR/InstrIndex.cpp [new file with mode: 0644]
contrib/coco/core/src/IR/InstrIndex.test.cpp [new file with mode: 0644]

diff --git a/contrib/coco/core/include/coco/IR/InstrIndex.h b/contrib/coco/core/include/coco/IR/InstrIndex.h
new file mode 100644 (file)
index 0000000..a61d97c
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __COCO_IR_INSTR_INDEX_H__
+#define __COCO_IR_INSTR_INDEX_H__
+
+#include <cstdint>
+
+namespace coco
+{
+
+/**
+ * @brief A InstrIndex denotes the index of an instruction in an instruction list
+ */
+class InstrIndex final
+{
+private:
+  static const uint32_t undefined = 0xffffffff;
+
+public:
+  InstrIndex() : _value{undefined}
+  {
+    // DO NOTHING
+  }
+
+public:
+  InstrIndex(uint32_t value) { set(value); }
+
+public:
+  bool valid(void) const { return _value != undefined; }
+
+public:
+  uint32_t value(void) const { return _value; }
+
+public:
+  void set(uint32_t value);
+  void reset(void) { _value = undefined; }
+
+private:
+  uint32_t _value;
+};
+
+static inline bool operator<(const InstrIndex &lhs, const InstrIndex &rhs)
+{
+  return lhs.value() < rhs.value();
+}
+
+} // namespace coco
+
+#endif // __COCO_IR_INSTR_INDEX_H__
diff --git a/contrib/coco/core/src/IR/InstrIndex.cpp b/contrib/coco/core/src/IR/InstrIndex.cpp
new file mode 100644 (file)
index 0000000..c447cfc
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "coco/IR/InstrIndex.h"
+
+#include <cassert>
+
+namespace coco
+{
+
+void InstrIndex::set(uint32_t value)
+{
+  assert(value != undefined);
+  _value = value;
+}
+
+} // namespace coco
diff --git a/contrib/coco/core/src/IR/InstrIndex.test.cpp b/contrib/coco/core/src/IR/InstrIndex.test.cpp
new file mode 100644 (file)
index 0000000..40f5d49
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "coco/IR/InstrIndex.h"
+
+#include <gtest/gtest.h>
+
+namespace
+{
+
+class InstrIndexTest : public ::testing::Test
+{
+};
+
+} // namespace
+
+TEST_F(InstrIndexTest, default_constructor)
+{
+  coco::InstrIndex ins_ind;
+
+  ASSERT_FALSE(ins_ind.valid());
+}
+
+TEST_F(InstrIndexTest, explicit_constructor)
+{
+  coco::InstrIndex ins_ind{3};
+
+  ASSERT_TRUE(ins_ind.valid());
+  ASSERT_EQ(ins_ind.value(), 3);
+}
+
+TEST_F(InstrIndexTest, operator_lt)
+{
+  // Valid index is always less than undefined one.
+  ASSERT_TRUE(coco::InstrIndex(3) < coco::InstrIndex());
+  ASSERT_TRUE(coco::InstrIndex(3) < coco::InstrIndex(4));
+}