Skip to content

Commit 6f74836

Browse files
committed
Add a generic Instruction struct
1 parent 4e9f77c commit 6f74836

3 files changed

Lines changed: 116 additions & 94 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ target_sources(scratchcpp
44
icodebuilderfactory.h
55
codebuilderfactory.cpp
66
codebuilderfactory.h
7+
instruction.h
78
)
89

910
add_subdirectory(llvm)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <scratchcpp/compiler.h>
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class BlockPrototype;
11+
12+
template<typename TInstruction, typename TRegister>
13+
struct Instruction
14+
{
15+
enum class Type
16+
{
17+
FunctionCall,
18+
Add,
19+
Sub,
20+
Mul,
21+
Div,
22+
Random,
23+
RandomInt,
24+
CmpEQ,
25+
CmpGT,
26+
CmpLT,
27+
StrCmpEQCS, // case sensitive
28+
StrCmpEQCI, // case insensitive
29+
And,
30+
Or,
31+
Not,
32+
Mod,
33+
Round,
34+
Abs,
35+
Floor,
36+
Ceil,
37+
Sqrt,
38+
Sin,
39+
Cos,
40+
Tan,
41+
Asin,
42+
Acos,
43+
Atan,
44+
Ln,
45+
Log10,
46+
Exp,
47+
Exp10,
48+
StringConcat,
49+
StringChar,
50+
StringLength,
51+
Select,
52+
CreateLocalVariable,
53+
WriteLocalVariable,
54+
ReadLocalVariable,
55+
WriteVariable,
56+
ReadVariable,
57+
ClearList,
58+
RemoveListItem,
59+
AppendToList,
60+
InsertToList,
61+
ListReplace,
62+
GetListContents,
63+
GetListItem,
64+
GetListSize,
65+
GetListItemIndex,
66+
ListContainsItem,
67+
Yield,
68+
BeginIf,
69+
BeginElse,
70+
EndIf,
71+
BeginRepeatLoop,
72+
LoopIndex,
73+
BeginWhileLoop,
74+
BeginRepeatUntilLoop,
75+
BeginLoopCondition,
76+
EndLoop,
77+
Stop,
78+
ThreadStop,
79+
InvalidateTarget,
80+
CallProcedure,
81+
ProcedureArg,
82+
Print
83+
};
84+
85+
Instruction(Type type, bool loopCondition) :
86+
type(type),
87+
loopCondition(loopCondition)
88+
{
89+
}
90+
91+
Type type;
92+
std::string functionName;
93+
std::vector<std::pair<Compiler::StaticType, TRegister *>> args; // target type, register
94+
TRegister *functionReturnReg = nullptr;
95+
bool functionTargetArg = false; // whether to add target ptr to function parameters
96+
bool functionCtxArg = false; // whether to add execution context ptr to function parameters
97+
Variable *targetVariable = nullptr; // for variables
98+
List *targetList = nullptr; // for lists
99+
Compiler::StaticType targetType = Compiler::StaticType::Unknown; // variable or list type (before read/write operation)
100+
BlockPrototype *procedurePrototype = nullptr;
101+
size_t procedureArgIndex = 0;
102+
bool loopCondition = false; // whether the instruction is part of a loop condition
103+
104+
// Linked list
105+
TInstruction *previous = nullptr;
106+
TInstruction *next = nullptr;
107+
std::shared_ptr<TInstruction> _next; // ensure items are not deleted
108+
};
109+
110+
} // namespace libscratchcpp

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

Lines changed: 5 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <scratchcpp/compiler.h>
66

7+
#include "../instruction.h"
78
#include "llvmregister.h"
89

910
namespace libscratchcpp
@@ -12,102 +13,12 @@ namespace libscratchcpp
1213
class LLVMLocalVariableInfo;
1314
class BlockPrototype;
1415

15-
struct LLVMInstruction
16+
struct LLVMInstruction : Instruction<LLVMInstruction, LLVMRegister>
1617
{
17-
enum class Type
18-
{
19-
FunctionCall,
20-
Add,
21-
Sub,
22-
Mul,
23-
Div,
24-
Random,
25-
RandomInt,
26-
CmpEQ,
27-
CmpGT,
28-
CmpLT,
29-
StrCmpEQCS, // case sensitive
30-
StrCmpEQCI, // case insensitive
31-
And,
32-
Or,
33-
Not,
34-
Mod,
35-
Round,
36-
Abs,
37-
Floor,
38-
Ceil,
39-
Sqrt,
40-
Sin,
41-
Cos,
42-
Tan,
43-
Asin,
44-
Acos,
45-
Atan,
46-
Ln,
47-
Log10,
48-
Exp,
49-
Exp10,
50-
StringConcat,
51-
StringChar,
52-
StringLength,
53-
Select,
54-
CreateLocalVariable,
55-
WriteLocalVariable,
56-
ReadLocalVariable,
57-
WriteVariable,
58-
ReadVariable,
59-
ClearList,
60-
RemoveListItem,
61-
AppendToList,
62-
InsertToList,
63-
ListReplace,
64-
GetListContents,
65-
GetListItem,
66-
GetListSize,
67-
GetListItemIndex,
68-
ListContainsItem,
69-
Yield,
70-
BeginIf,
71-
BeginElse,
72-
EndIf,
73-
BeginRepeatLoop,
74-
LoopIndex,
75-
BeginWhileLoop,
76-
BeginRepeatUntilLoop,
77-
BeginLoopCondition,
78-
EndLoop,
79-
Stop,
80-
ThreadStop,
81-
InvalidateTarget,
82-
CallProcedure,
83-
ProcedureArg,
84-
Print
85-
};
18+
using Type = Instruction<LLVMInstruction, LLVMRegister>::Type;
19+
using Instruction<LLVMInstruction, LLVMRegister>::Instruction; // inherit constructor
8620

87-
LLVMInstruction(Type type, bool loopCondition) :
88-
type(type),
89-
loopCondition(loopCondition)
90-
{
91-
}
92-
93-
Type type;
94-
std::string functionName;
95-
std::vector<std::pair<Compiler::StaticType, LLVMRegister *>> args; // target type, register
96-
LLVMRegister *functionReturnReg = nullptr;
97-
bool functionTargetArg = false; // whether to add target ptr to function parameters
98-
bool functionCtxArg = false; // whether to add execution context ptr to function parameters
99-
LLVMLocalVariableInfo *localVarInfo = nullptr; // for local variables
100-
Variable *targetVariable = nullptr; // for variables
101-
List *targetList = nullptr; // for lists
102-
Compiler::StaticType targetType = Compiler::StaticType::Unknown; // variable or list type (before read/write operation)
103-
BlockPrototype *procedurePrototype = nullptr;
104-
size_t procedureArgIndex = 0;
105-
bool loopCondition = false; // whether the instruction is part of a loop condition
106-
107-
// Linked list
108-
LLVMInstruction *previous = nullptr;
109-
LLVMInstruction *next = nullptr;
110-
std::shared_ptr<LLVMInstruction> _next; // ensure items are not deleted
21+
LLVMLocalVariableInfo *localVarInfo = nullptr; // for local variables
11122
};
11223

11324
} // namespace libscratchcpp

0 commit comments

Comments
 (0)