Skip to content

Commit 672ce41

Browse files
committed
Add a generic InstructionList class
1 parent 6f74836 commit 672ce41

10 files changed

Lines changed: 455 additions & 458 deletions

File tree

‎src/engine/internal/codegen/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ target_sources(scratchcpp
55
codebuilderfactory.cpp
66
codebuilderfactory.h
77
instruction.h
8+
instructionlist.h
89
)
910

1011
add_subdirectory(llvm)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <memory>
6+
#include <functional>
7+
8+
#include "test_export.h"
9+
10+
namespace libscratchcpp
11+
{
12+
13+
template<typename TInstruction>
14+
class LIBSCRATCHCPP_TEST_EXPORT InstructionList
15+
{
16+
public:
17+
InstructionList() = default;
18+
InstructionList(const InstructionList &) = delete;
19+
20+
inline TInstruction *first() const { return m_first.get(); }
21+
22+
inline TInstruction *last() const { return m_last.get(); }
23+
24+
inline bool empty() const { return !first(); }
25+
26+
inline bool containsInstruction(TInstruction *ins) const
27+
{
28+
TInstruction *ptr = m_first.get();
29+
30+
while (ptr) {
31+
if (ptr == ins)
32+
return true;
33+
34+
ptr = ptr->next;
35+
}
36+
37+
return false;
38+
}
39+
40+
inline bool containsInstruction(std::function<bool(const TInstruction *)> func) const
41+
{
42+
TInstruction *ptr = m_first.get();
43+
44+
while (ptr) {
45+
if (func(ptr))
46+
return true;
47+
48+
ptr = ptr->next;
49+
}
50+
51+
return false;
52+
}
53+
54+
inline void addInstruction(std::shared_ptr<TInstruction> ins)
55+
{
56+
if (!(m_first && m_last)) {
57+
assert(!m_first && !m_last);
58+
59+
m_first = ins;
60+
m_last = ins;
61+
62+
ins->previous = nullptr;
63+
ins->next = nullptr;
64+
ins->_next = nullptr;
65+
} else {
66+
m_last->next = ins.get();
67+
m_last->_next = ins;
68+
ins->previous = m_last.get();
69+
m_last = ins;
70+
}
71+
}
72+
73+
private:
74+
std::shared_ptr<TInstruction> m_first;
75+
std::shared_ptr<TInstruction> m_last;
76+
};
77+
78+
} // namespace libscratchcpp

‎src/engine/internal/codegen/llvm/CMakeLists.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ target_sources(scratchcpp
99
llvmregister.h
1010
llvmconstantregister.h
1111
llvminstruction.h
12-
llvminstructionlist.cpp
1312
llvminstructionlist.h
1413
llvmifstatement.h
1514
llvmloop.h

‎src/engine/internal/codegen/llvm/llvmcodeanalyzer.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
#include <unordered_map>
77

88
#include "test_export.h"
9+
#include "llvminstructionlist.h"
910

1011
namespace libscratchcpp
1112
{
1213

1314
class LLVMBuildUtils;
14-
class LLVMInstructionList;
1515
class LLVMInstruction;
1616

1717
class LIBSCRATCHCPP_TEST_EXPORT LLVMCodeAnalyzer

‎src/engine/internal/codegen/llvm/llvminstructionlist.cpp‎

Lines changed: 0 additions & 65 deletions
This file was deleted.

‎src/engine/internal/codegen/llvm/llvminstructionlist.h‎

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,13 @@
22

33
#pragma once
44

5-
#include <memory>
6-
#include <functional>
7-
8-
#include "test_export.h"
5+
#include "../instructionlist.h"
96

107
namespace libscratchcpp
118
{
129

1310
struct LLVMInstruction;
1411

15-
class LIBSCRATCHCPP_TEST_EXPORT LLVMInstructionList
16-
{
17-
public:
18-
LLVMInstructionList() = default;
19-
LLVMInstructionList(const LLVMInstructionList &) = delete;
20-
21-
LLVMInstruction *first() const;
22-
LLVMInstruction *last() const;
23-
24-
bool empty() const { return !first(); }
25-
26-
bool containsInstruction(LLVMInstruction *ins) const;
27-
bool containsInstruction(std::function<bool(const LLVMInstruction *)> func) const;
28-
29-
void addInstruction(std::shared_ptr<LLVMInstruction> ins);
30-
31-
private:
32-
std::shared_ptr<LLVMInstruction> m_first;
33-
std::shared_ptr<LLVMInstruction> m_last;
34-
};
12+
using LLVMInstructionList = InstructionList<LLVMInstruction>;
3513

3614
} // namespace libscratchcpp

‎test/codegen/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ add_executable(
1919
codebuilder_test.cpp
2020
codegentestutils.cpp
2121
codegentestutils.h
22+
instructionlist_test.cpp
2223
operators/equal_comparison_test.cpp
2324
operators/greater_than_test.cpp
2425
operators/less_than_test.cpp

0 commit comments

Comments
 (0)