Skip to content

Commit 7adad6a

Browse files
committed
Add a separate class for building LLVM instructions
1 parent 5695176 commit 7adad6a

File tree

7 files changed

+125
-0
lines changed

7 files changed

+125
-0
lines changed

src/engine/internal/llvm/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ target_sources(scratchcpp
2828
llvmexecutioncontext.cpp
2929
llvmexecutioncontext.h
3030
)
31+
32+
add_subdirectory(instructions)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
target_sources(scratchcpp
2+
PRIVATE
3+
instructionbuilder.cpp
4+
instructionbuilder.h
5+
instructiongroup.cpp
6+
instructiongroup.h
7+
processresult.h
8+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "instructionbuilder.h"
4+
5+
using namespace libscratchcpp;
6+
using namespace libscratchcpp::llvmins;
7+
8+
InstructionBuilder::InstructionBuilder(LLVMBuildUtils &utils)
9+
{
10+
// Create groups
11+
}
12+
13+
LLVMInstruction *InstructionBuilder::process(LLVMInstruction *ins)
14+
{
15+
// Process all groups
16+
for (const auto &group : m_groups) {
17+
ProcessResult result = group->process(ins);
18+
19+
if (result.match)
20+
return result.next;
21+
}
22+
23+
assert(false); // instruction not found
24+
return ins;
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "instructiongroup.h"
6+
7+
namespace libscratchcpp::llvmins
8+
{
9+
10+
class InstructionBuilder
11+
{
12+
public:
13+
InstructionBuilder(LLVMBuildUtils &utils);
14+
15+
LLVMInstruction *process(LLVMInstruction *ins);
16+
17+
private:
18+
std::vector<std::unique_ptr<InstructionGroup>> m_groups;
19+
};
20+
21+
} // namespace libscratchcpp::llvmins
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "instructiongroup.h"
4+
#include "../llvmbuildutils.h"
5+
6+
using namespace libscratchcpp::llvmins;
7+
8+
InstructionGroup::InstructionGroup(LLVMBuildUtils &utils) :
9+
m_utils(utils),
10+
m_builder(utils.builder())
11+
{
12+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <llvm/IR/IRBuilder.h>
6+
7+
#include "processresult.h"
8+
9+
namespace libscratchcpp
10+
{
11+
12+
class LLVMBuildUtils;
13+
14+
namespace llvmins
15+
{
16+
17+
class InstructionGroup
18+
{
19+
public:
20+
InstructionGroup(LLVMBuildUtils &utils);
21+
22+
virtual ProcessResult process(LLVMInstruction *ins) = 0;
23+
24+
protected:
25+
LLVMBuildUtils &m_utils;
26+
llvm::IRBuilder<> &m_builder;
27+
};
28+
29+
} // namespace llvmins
30+
} // namespace libscratchcpp
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
namespace libscratchcpp
6+
{
7+
8+
class LLVMInstruction;
9+
10+
namespace llvmins
11+
{
12+
13+
// Result of group processing
14+
struct ProcessResult
15+
{
16+
ProcessResult(bool match, LLVMInstruction *next) :
17+
match(match),
18+
next(next)
19+
{
20+
}
21+
22+
bool match = false; // whether the desired instruction was found
23+
LLVMInstruction *next = nullptr; // next instruction (state)
24+
};
25+
26+
} // namespace llvmins
27+
} // namespace libscratchcpp

0 commit comments

Comments
 (0)