Skip to content

Commit 24b1866

Browse files
authored
Merge pull request #604 from scratchcpp/refactor_llvm_compiler
LLVM: Refactor Compiler to use local value pointers
2 parents 2219f8b + 076aadf commit 24b1866

34 files changed

+3046
-2957
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ if (LIBSCRATCHCPP_USE_LLVM)
7272
target_sources(scratchcpp
7373
PUBLIC
7474
include/scratchcpp/dev/compiler.h
75+
include/scratchcpp/dev/compilervalue.h
76+
include/scratchcpp/dev/compilerconstant.h
7577
include/scratchcpp/dev/executablecode.h
7678
include/scratchcpp/dev/executioncontext.h
7779
)

include/scratchcpp/block.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ namespace libscratchcpp
1010

1111
class IEngine;
1212
class Target;
13+
#ifdef USE_LLVM
14+
class CompilerValue;
15+
#endif
1316
class Input;
1417
class Field;
1518
class Comment;
@@ -25,7 +28,11 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
2528
Block(const std::string &id, const std::string &opcode);
2629
Block(const Block &) = delete;
2730

31+
#ifdef USE_LLVM
32+
CompilerValue *compile(Compiler *compiler);
33+
#else
2834
void compile(Compiler *compiler);
35+
#endif
2936

3037
const std::string &opcode() const;
3138

@@ -76,7 +83,7 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
7683
BlockComp compileFunction() const;
7784
void setCompileFunction(BlockComp newCompileFunction);
7885

79-
BlockComp hatPredicateCompileFunction() const;
86+
HatPredicateCompileFunc hatPredicateCompileFunction() const;
8087
void setHatPredicateCompileFunction(HatPredicateCompileFunc newHatPredicateCompileFunction);
8188

8289
bool mutationHasNext() const;

include/scratchcpp/dev/compiler.h

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace libscratchcpp
1414
class IEngine;
1515
class Target;
1616
class ExecutableCode;
17+
class CompilerValue;
18+
class CompilerConstant;
1719
class Variable;
1820
class List;
1921
class Input;
@@ -33,6 +35,9 @@ class LIBSCRATCHCPP_EXPORT Compiler
3335
Unknown
3436
};
3537

38+
using ArgTypes = std::vector<StaticType>;
39+
using Args = std::vector<CompilerValue *>;
40+
3641
Compiler(IEngine *engine, Target *target);
3742
Compiler(const Compiler &) = delete;
3843

@@ -42,63 +47,63 @@ class LIBSCRATCHCPP_EXPORT Compiler
4247

4348
std::shared_ptr<ExecutableCode> compile(std::shared_ptr<Block> startBlock);
4449

45-
void addFunctionCall(const std::string &functionName, StaticType returnType = StaticType::Void, const std::vector<StaticType> &argTypes = {});
46-
void addConstValue(const Value &value);
47-
void addVariableValue(Variable *variable);
48-
void addListContents(List *list);
49-
void addListItem(List *list);
50-
void addListItemIndex(List *list);
51-
void addListContains(List *list);
52-
void addListSize(List *list);
53-
void addInput(const std::string &name);
54-
55-
void createAdd();
56-
void createSub();
57-
void createMul();
58-
void createDiv();
59-
60-
void createCmpEQ();
61-
void createCmpGT();
62-
void createCmpLT();
63-
64-
void createAnd();
65-
void createOr();
66-
void createNot();
67-
68-
void createMod();
69-
void createRound();
70-
void createAbs();
71-
void createFloor();
72-
void createCeil();
73-
void createSqrt();
74-
void createSin();
75-
void createCos();
76-
void createTan();
77-
void createAsin();
78-
void createAcos();
79-
void createAtan();
80-
void createLn();
81-
void createLog10();
82-
void createExp();
83-
void createExp10();
84-
85-
void createVariableWrite(Variable *variable);
50+
CompilerValue *addFunctionCall(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
51+
CompilerConstant *addConstValue(const Value &value);
52+
CompilerValue *addVariableValue(Variable *variable);
53+
CompilerValue *addListContents(List *list);
54+
CompilerValue *addListItem(List *list, CompilerValue *index);
55+
CompilerValue *addListItemIndex(List *list, CompilerValue *item);
56+
CompilerValue *addListContains(List *list, CompilerValue *item);
57+
CompilerValue *addListSize(List *list);
58+
CompilerValue *addInput(const std::string &name);
59+
60+
CompilerValue *createAdd(CompilerValue *operand1, CompilerValue *operand2);
61+
CompilerValue *createSub(CompilerValue *operand1, CompilerValue *operand2);
62+
CompilerValue *createMul(CompilerValue *operand1, CompilerValue *operand2);
63+
CompilerValue *createDiv(CompilerValue *operand1, CompilerValue *operand2);
64+
65+
CompilerValue *createCmpEQ(CompilerValue *operand1, CompilerValue *operand2);
66+
CompilerValue *createCmpGT(CompilerValue *operand1, CompilerValue *operand2);
67+
CompilerValue *createCmpLT(CompilerValue *operand1, CompilerValue *operand2);
68+
69+
CompilerValue *createAnd(CompilerValue *operand1, CompilerValue *operand2);
70+
CompilerValue *createOr(CompilerValue *operand1, CompilerValue *operand2);
71+
CompilerValue *createNot(CompilerValue *operand);
72+
73+
CompilerValue *createMod(CompilerValue *num1, CompilerValue *num2);
74+
CompilerValue *createRound(CompilerValue *num);
75+
CompilerValue *createAbs(CompilerValue *num);
76+
CompilerValue *createFloor(CompilerValue *num);
77+
CompilerValue *createCeil(CompilerValue *num);
78+
CompilerValue *createSqrt(CompilerValue *num);
79+
CompilerValue *createSin(CompilerValue *num);
80+
CompilerValue *createCos(CompilerValue *num);
81+
CompilerValue *createTan(CompilerValue *num);
82+
CompilerValue *createAsin(CompilerValue *num);
83+
CompilerValue *createAcos(CompilerValue *num);
84+
CompilerValue *createAtan(CompilerValue *num);
85+
CompilerValue *createLn(CompilerValue *num);
86+
CompilerValue *createLog10(CompilerValue *num);
87+
CompilerValue *createExp(CompilerValue *num);
88+
CompilerValue *createExp10(CompilerValue *num);
89+
90+
void createVariableWrite(Variable *variable, CompilerValue *value);
8691

8792
void createListClear(List *list);
88-
void createListRemove(List *list);
89-
void createListAppend(List *list);
90-
void createListInsert(List *list);
91-
void createListReplace(List *list);
93+
void createListRemove(List *list, CompilerValue *index);
94+
void createListAppend(List *list, CompilerValue *item);
95+
void createListInsert(List *list, CompilerValue *index, CompilerValue *item);
96+
void createListReplace(List *list, CompilerValue *index, CompilerValue *item);
9297

93-
void beginIfStatement();
98+
void beginIfStatement(CompilerValue *cond);
9499
void beginElseBranch();
95100
void endIf();
96101

97-
void moveToIf(std::shared_ptr<Block> substack);
98-
void moveToIfElse(std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2);
99-
void moveToRepeatLoop(std::shared_ptr<Block> substack);
100-
void moveToWhileLoop(std::shared_ptr<Block> substack);
101-
void moveToRepeatUntilLoop(std::shared_ptr<Block> substack);
102+
void moveToIf(CompilerValue *cond, std::shared_ptr<Block> substack);
103+
void moveToIfElse(CompilerValue *cond, std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2);
104+
void moveToRepeatLoop(CompilerValue *count, std::shared_ptr<Block> substack);
105+
void moveToWhileLoop(CompilerValue *cond, std::shared_ptr<Block> substack);
106+
void moveToRepeatUntilLoop(CompilerValue *cond, std::shared_ptr<Block> substack);
102107
void beginLoopCondition();
103108
void warp();
104109

@@ -108,7 +113,7 @@ class LIBSCRATCHCPP_EXPORT Compiler
108113
const std::unordered_set<std::string> &unsupportedBlocks() const;
109114

110115
private:
111-
void addInput(Input *input);
116+
CompilerValue *addInput(Input *input);
112117

113118
spimpl::unique_impl_ptr<CompilerPrivate> impl;
114119
};
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+
#include "compilervalue.h"
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class CompilerConstantPrivate;
11+
12+
/*! \brief The CompilerConstant class represents a constant value in compiled code. */
13+
class LIBSCRATCHCPP_EXPORT CompilerConstant : public CompilerValue
14+
{
15+
public:
16+
CompilerConstant(Compiler::StaticType type, const Value &value);
17+
CompilerConstant(const CompilerConstant &) = delete;
18+
19+
bool isConst() const override final { return true; };
20+
21+
const Value &value() const;
22+
23+
private:
24+
spimpl::unique_impl_ptr<CompilerConstantPrivate> impl;
25+
};
26+
27+
} // namespace libscratchcpp
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "compiler.h"
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class CompilerValuePrivate;
11+
12+
/*! \brief The CompilerValue class represents a local value in compiled code. */
13+
class LIBSCRATCHCPP_EXPORT CompilerValue
14+
{
15+
public:
16+
CompilerValue(Compiler::StaticType type);
17+
CompilerValue(const CompilerValue &) = delete;
18+
virtual ~CompilerValue() { }
19+
20+
Compiler::StaticType type() const;
21+
void setType(Compiler::StaticType type);
22+
23+
virtual bool isConst() const { return false; };
24+
25+
private:
26+
spimpl::unique_impl_ptr<CompilerValuePrivate> impl;
27+
};
28+
29+
} // namespace libscratchcpp

include/scratchcpp/global.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ namespace libscratchcpp
2525

2626
class VirtualMachine;
2727
class Compiler;
28+
#ifdef USE_LLVM
29+
class CompilerValue;
30+
#endif
2831
class Block;
2932
class Value;
3033

@@ -35,12 +38,21 @@ class Value;
3538
*/
3639
using BlockFunc = unsigned int (*)(VirtualMachine *vm);
3740

41+
#ifdef USE_LLVM
42+
/*!
43+
* \typedef BlockComp
44+
*
45+
* BlockComp is a function pointer for functions which are used to compile blocks.
46+
*/
47+
using BlockComp = CompilerValue *(*)(Compiler *);
48+
#else
3849
/*!
3950
* \typedef BlockComp
4051
*
4152
* BlockComp is a function pointer for functions which are used to compile blocks to bytecode.
4253
*/
4354
using BlockComp = void (*)(Compiler *);
55+
#endif // USE_LLVM
4456

4557
/*!
4658
* \typedef MonitorNameFunc
@@ -56,12 +68,21 @@ using MonitorNameFunc = const std::string &(*)(Block *);
5668
*/
5769
using MonitorChangeFunc = void (*)(Block *, const Value &newValue);
5870

71+
#ifdef USE_LLVM
72+
/*!
73+
* \typedef HatPredicateCompileFunc
74+
*
75+
* HatPredicateCompileFunc is a function pointer for functions which are used to compile edge-activated hat predicates.
76+
*/
77+
using HatPredicateCompileFunc = CompilerValue *(*)(Compiler *vm);
78+
#else
5979
/*!
6080
* \typedef HatPredicateCompileFunc
6181
*
6282
* HatPredicateCompileFunc is a function pointer for functions which are used to compile edge-activated hat predicates to bytecode.
6383
*/
6484
using HatPredicateCompileFunc = void (*)(Compiler *vm);
85+
#endif // USE_LLVM
6586

6687
} // namespace libscratchcpp
6788

include/scratchcpp/inputvalue.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
namespace libscratchcpp
1111
{
1212

13+
#ifdef USE_LLVM
14+
class CompilerValue;
15+
#endif
1316
class Block;
1417
class Entity;
1518
class InputValuePrivate;
@@ -34,7 +37,11 @@ class LIBSCRATCHCPP_EXPORT InputValue
3437

3538
InputValue(Type type = Type::Number);
3639

40+
#ifdef USE_LLVM
41+
CompilerValue *compile(Compiler *compiler);
42+
#else
3743
void compile(Compiler *compiler);
44+
#endif
3845

3946
Type type() const;
4047
void setType(Type newType);

src/dev/engine/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ target_sources(scratchcpp
33
compiler.cpp
44
compiler_p.cpp
55
compiler_p.h
6+
compilervalue.cpp
7+
compilervalue_p.cpp
8+
compilervalue_p.h
9+
compilerconstant.cpp
10+
compilerconstant_p.cpp
11+
compilerconstant_p.h
612
executioncontext.cpp
713
executioncontext_p.cpp
814
executioncontext_p.h

0 commit comments

Comments
 (0)