Skip to content

Commit 7a86400

Browse files
authored
Merge pull request #621 from scratchcpp/remove_bytecode_vm
Remove bytecode VM
2 parents 01efb99 + 915da11 commit 7a86400

File tree

206 files changed

+5419
-28616
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

206 files changed

+5419
-28616
lines changed

.github/workflows/utests-llvm.yml

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

.github/workflows/utests-minimal.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
submodules: true
2020

2121
- name: Configure CMake
22-
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DLIBSCRATCHCPP_BUILD_UNIT_TESTS=ON -DLIBSCRATCHCPP_AUDIO_SUPPORT=OFF -DLIBSCRATCHCPP_NETWORK_SUPPORT=OFF -DLIBSCRATCHCPP_COMPUTED_GOTO=OFF
22+
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DLIBSCRATCHCPP_BUILD_UNIT_TESTS=ON -DLIBSCRATCHCPP_AUDIO_SUPPORT=OFF -DLIBSCRATCHCPP_NETWORK_SUPPORT=OFF
2323

2424
- name: Build
2525
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -j$(nproc --all)

CMakeLists.txt

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,28 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
88

99
option(LIBSCRATCHCPP_BUILD_UNIT_TESTS "Build unit tests" ON)
1010
option(LIBSCRATCHCPP_NETWORK_SUPPORT "Support for downloading projects" ON)
11-
option(LIBSCRATCHCPP_COMPUTED_GOTO "Support for computed goto" ON)
12-
option(LIBSCRATCHCPP_USE_LLVM "Compile scripts to LLVM IR (work in progress)" OFF)
1311
option(LIBSCRATCHCPP_PRINT_LLVM_IR "Print LLVM IR of compiled Scratch scripts (for debugging)" OFF)
1412

15-
if (NOT (CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
16-
# Computed goto not supported on anything except GCC
17-
set(LIBSCRATCHCPP_COMPUTED_GOTO OFF CACHE BOOL "" FORCE)
18-
endif()
19-
2013
add_library(scratchcpp SHARED)
2114
add_subdirectory(src)
2215
include_directories(src) # TODO: Remove this line
2316
include_directories(include)
2417

25-
if (LIBSCRATCHCPP_COMPUTED_GOTO)
26-
target_compile_definitions(scratchcpp PRIVATE ENABLE_COMPUTED_GOTO)
27-
endif()
28-
2918
target_sources(scratchcpp
3019
PUBLIC
3120
include/scratchcpp/global.h
3221
include/scratchcpp/project.h
3322
include/scratchcpp/scratchconfiguration.h
3423
include/scratchcpp/iengine.h
3524
include/scratchcpp/iextension.h
25+
include/scratchcpp/compiler.h
26+
include/scratchcpp/compilercontext.h
27+
include/scratchcpp/compilervalue.h
28+
include/scratchcpp/compilerconstant.h
29+
include/scratchcpp/compilerlocalvariable.h
30+
include/scratchcpp/executablecode.h
31+
include/scratchcpp/executioncontext.h
32+
include/scratchcpp/promise.h
3633
include/scratchcpp/thread.h
3734
include/scratchcpp/asset.h
3835
include/scratchcpp/costume.h
@@ -48,7 +45,6 @@ target_sources(scratchcpp
4845
include/scratchcpp/field.h
4946
include/scratchcpp/script.h
5047
include/scratchcpp/broadcast.h
51-
include/scratchcpp/virtualmachine.h
5248
include/scratchcpp/blockprototype.h
5349
include/scratchcpp/block.h
5450
include/scratchcpp/istagehandler.h
@@ -67,31 +63,11 @@ target_sources(scratchcpp
6763
include/scratchcpp/comment.h
6864
include/scratchcpp/monitor.h
6965
include/scratchcpp/imonitorhandler.h
66+
include/scratchcpp/test/scriptbuilder.h
7067
)
7168

72-
if (LIBSCRATCHCPP_USE_LLVM)
73-
target_compile_definitions(scratchcpp PUBLIC USE_LLVM)
74-
target_sources(scratchcpp
75-
PUBLIC
76-
include/scratchcpp/dev/compiler.h
77-
include/scratchcpp/dev/compilercontext.h
78-
include/scratchcpp/dev/compilervalue.h
79-
include/scratchcpp/dev/compilerconstant.h
80-
include/scratchcpp/dev/compilerlocalvariable.h
81-
include/scratchcpp/dev/executablecode.h
82-
include/scratchcpp/dev/executioncontext.h
83-
include/scratchcpp/dev/promise.h
84-
include/scratchcpp/dev/test/scriptbuilder.h
85-
)
86-
87-
if(LIBSCRATCHCPP_PRINT_LLVM_IR)
88-
target_compile_definitions(scratchcpp PRIVATE PRINT_LLVM_IR)
89-
endif()
90-
else()
91-
target_sources(scratchcpp
92-
PUBLIC
93-
include/scratchcpp/compiler.h
94-
)
69+
if(LIBSCRATCHCPP_PRINT_LLVM_IR)
70+
target_compile_definitions(scratchcpp PRIVATE PRINT_LLVM_IR)
9571
endif()
9672

9773
include(FetchContent)
@@ -123,11 +99,9 @@ if (LIBSCRATCHCPP_NETWORK_SUPPORT)
12399
target_compile_definitions(scratchcpp PRIVATE LIBSCRATCHCPP_NETWORK_SUPPORT)
124100
endif()
125101

126-
if (LIBSCRATCHCPP_USE_LLVM)
127-
include(build/HunterPackages.cmake)
128-
include(build/LLVM.cmake)
129-
target_link_libraries(scratchcpp PRIVATE LLVM)
130-
endif()
102+
include(build/HunterPackages.cmake)
103+
include(build/LLVM.cmake)
104+
target_link_libraries(scratchcpp PRIVATE LLVM)
131105

132106
target_compile_definitions(scratchcpp PRIVATE LIBSCRATCHCPP_LIBRARY)
133107
target_compile_definitions(scratchcpp PRIVATE LIBSCRATCHCPP_VERSION="${PROJECT_VERSION}")

include/scratchcpp/block.h

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

1111
class IEngine;
1212
class Target;
13-
#ifdef USE_LLVM
1413
class CompilerValue;
15-
#endif
1614
class Input;
1715
class Field;
1816
class Comment;
@@ -28,11 +26,7 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
2826
Block(const std::string &id, const std::string &opcode);
2927
Block(const Block &) = delete;
3028

31-
#ifdef USE_LLVM
3229
CompilerValue *compile(Compiler *compiler);
33-
#else
34-
void compile(Compiler *compiler);
35-
#endif
3630

3731
const std::string &opcode() const;
3832

include/scratchcpp/compiler.h

Lines changed: 112 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,149 @@
22

33
#pragma once
44

5-
#include <vector>
6-
#include <memory>
75
#include <unordered_set>
6+
#include <vector>
87

98
#include "global.h"
109
#include "spimpl.h"
11-
#include "virtualmachine.h"
12-
#include "value.h"
1310

1411
namespace libscratchcpp
1512
{
1613

14+
class CompilerContext;
1715
class IEngine;
18-
class Block;
19-
class Input;
20-
class InputValue;
21-
class Field;
16+
class Target;
17+
class ExecutableCode;
18+
class CompilerValue;
19+
class CompilerConstant;
20+
class CompilerLocalVariable;
2221
class Variable;
2322
class List;
23+
class Input;
24+
class Field;
2425
class BlockPrototype;
25-
class Entity;
2626
class CompilerPrivate;
2727

28-
/*! \brief The Compiler class provides an API for compiling scripts of targets to bytecode. */
28+
/*! \brief The Compiler class provides API for compiling Scratch scripts. */
2929
class LIBSCRATCHCPP_EXPORT Compiler
3030
{
3131
public:
32-
enum class SubstackType
32+
enum class StaticType
3333
{
34-
Loop,
35-
IfStatement
34+
Void,
35+
Number,
36+
Bool,
37+
String,
38+
Unknown
3639
};
3740

38-
Compiler(IEngine *engine, Target *target = nullptr);
39-
Compiler(const Compiler &) = delete;
40-
41-
void init();
42-
void compile(std::shared_ptr<Block> topLevelBlock);
43-
void end();
41+
using ArgTypes = std::vector<StaticType>;
42+
using Args = std::vector<CompilerValue *>;
4443

45-
const std::vector<unsigned int> &bytecode() const;
46-
const std::vector<unsigned int> &hatPredicateBytecode() const;
44+
Compiler(CompilerContext *ctx);
45+
Compiler(IEngine *engine, Target *target);
46+
Compiler(const Compiler &) = delete;
4747

4848
IEngine *engine() const;
4949
Target *target() const;
50-
51-
const std::vector<InputValue *> &constInputValues() const;
52-
std::vector<Value> constValues() const;
53-
54-
const std::vector<Variable *> &variables() const;
55-
std::vector<Value *> variablePtrs() const;
56-
57-
const std::vector<List *> &lists() const;
58-
59-
void addInstruction(vm::Opcode opcode, const std::initializer_list<unsigned int> &args = {});
60-
void addInput(Input *input);
61-
void addInput(int id);
62-
void addConstValue(const Value &value);
63-
void addFunctionCall(BlockFunc f);
64-
void addProcedureArg(const std::string &procCode, const std::string &argName);
65-
void moveToSubstack(std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2, SubstackType type);
66-
void moveToSubstack(std::shared_ptr<Block> substack, SubstackType type);
50+
std::shared_ptr<Block> block() const;
51+
52+
std::shared_ptr<ExecutableCode> compile(std::shared_ptr<Block> startBlock);
53+
void preoptimize();
54+
55+
CompilerValue *addFunctionCall(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
56+
CompilerValue *addTargetFunctionCall(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
57+
CompilerValue *addFunctionCallWithCtx(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
58+
CompilerConstant *addConstValue(const Value &value);
59+
CompilerValue *addLoopIndex();
60+
CompilerValue *addLocalVariableValue(CompilerLocalVariable *variable);
61+
CompilerValue *addVariableValue(Variable *variable);
62+
CompilerValue *addListContents(List *list);
63+
CompilerValue *addListItem(List *list, CompilerValue *index);
64+
CompilerValue *addListItemIndex(List *list, CompilerValue *item);
65+
CompilerValue *addListContains(List *list, CompilerValue *item);
66+
CompilerValue *addListSize(List *list);
67+
CompilerValue *addProcedureArgument(const std::string &name);
68+
69+
CompilerValue *addInput(const std::string &name);
70+
CompilerValue *addInput(Input *input);
71+
72+
CompilerValue *createAdd(CompilerValue *operand1, CompilerValue *operand2);
73+
CompilerValue *createSub(CompilerValue *operand1, CompilerValue *operand2);
74+
CompilerValue *createMul(CompilerValue *operand1, CompilerValue *operand2);
75+
CompilerValue *createDiv(CompilerValue *operand1, CompilerValue *operand2);
76+
77+
CompilerValue *createRandom(CompilerValue *from, CompilerValue *to);
78+
CompilerValue *createRandomInt(CompilerValue *from, CompilerValue *to);
79+
80+
CompilerValue *createCmpEQ(CompilerValue *operand1, CompilerValue *operand2);
81+
CompilerValue *createCmpGT(CompilerValue *operand1, CompilerValue *operand2);
82+
CompilerValue *createCmpLT(CompilerValue *operand1, CompilerValue *operand2);
83+
84+
CompilerValue *createStrCmpEQ(CompilerValue *string1, CompilerValue *string2, bool caseSensitive = false);
85+
86+
CompilerValue *createAnd(CompilerValue *operand1, CompilerValue *operand2);
87+
CompilerValue *createOr(CompilerValue *operand1, CompilerValue *operand2);
88+
CompilerValue *createNot(CompilerValue *operand);
89+
90+
CompilerValue *createMod(CompilerValue *num1, CompilerValue *num2);
91+
CompilerValue *createRound(CompilerValue *num);
92+
CompilerValue *createAbs(CompilerValue *num);
93+
CompilerValue *createFloor(CompilerValue *num);
94+
CompilerValue *createCeil(CompilerValue *num);
95+
CompilerValue *createSqrt(CompilerValue *num);
96+
CompilerValue *createSin(CompilerValue *num);
97+
CompilerValue *createCos(CompilerValue *num);
98+
CompilerValue *createTan(CompilerValue *num);
99+
CompilerValue *createAsin(CompilerValue *num);
100+
CompilerValue *createAcos(CompilerValue *num);
101+
CompilerValue *createAtan(CompilerValue *num);
102+
CompilerValue *createLn(CompilerValue *num);
103+
CompilerValue *createLog10(CompilerValue *num);
104+
CompilerValue *createExp(CompilerValue *num);
105+
CompilerValue *createExp10(CompilerValue *num);
106+
107+
CompilerValue *createSelect(CompilerValue *cond, CompilerValue *trueValue, CompilerValue *falseValue, Compiler::StaticType valueType);
108+
109+
CompilerLocalVariable *createLocalVariable(Compiler::StaticType type);
110+
void createLocalVariableWrite(CompilerLocalVariable *variable, CompilerValue *value);
111+
112+
void createVariableWrite(Variable *variable, CompilerValue *value);
113+
114+
void createListClear(List *list);
115+
void createListRemove(List *list, CompilerValue *index);
116+
void createListAppend(List *list, CompilerValue *item);
117+
void createListInsert(List *list, CompilerValue *index, CompilerValue *item);
118+
void createListReplace(List *list, CompilerValue *index, CompilerValue *item);
119+
120+
void beginIfStatement(CompilerValue *cond);
121+
void beginElseBranch();
122+
void endIf();
123+
124+
void beginWhileLoop(CompilerValue *cond);
125+
void beginRepeatUntilLoop(CompilerValue *cond);
126+
void beginLoopCondition();
127+
void endLoop();
128+
129+
void moveToIf(CompilerValue *cond, std::shared_ptr<Block> substack);
130+
void moveToIfElse(CompilerValue *cond, std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2);
131+
void moveToRepeatLoop(CompilerValue *count, std::shared_ptr<Block> substack);
132+
void moveToWhileLoop(CompilerValue *cond, std::shared_ptr<Block> substack);
133+
void moveToRepeatUntilLoop(CompilerValue *cond, std::shared_ptr<Block> substack);
67134
void warp();
68135

69-
Input *input(int id) const;
70-
Field *field(int id) const;
71-
std::shared_ptr<Block> inputBlock(int id) const;
72-
unsigned int variableIndex(std::shared_ptr<Entity> varEntity);
73-
unsigned int listIndex(std::shared_ptr<Entity> listEntity);
74-
unsigned int constIndex(InputValue *value);
75-
unsigned int procedureIndex(const std::string &proc);
76-
long procedureArgIndex(const std::string &procCode, const std::string &argName);
77-
78-
const std::vector<std::string> &procedures() const;
136+
void createYield();
137+
void createStop();
79138

80-
const std::shared_ptr<Block> &block() const;
81-
void setBlock(std::shared_ptr<Block> block);
139+
void createProcedureCall(BlockPrototype *prototype, const Compiler::Args &args);
82140

83-
BlockPrototype *procedurePrototype() const;
84-
void setProcedurePrototype(BlockPrototype *prototype);
141+
Input *input(const std::string &name) const;
142+
Field *field(const std::string &name) const;
85143

86144
const std::unordered_set<std::string> &unsupportedBlocks() const;
87145

146+
static std::shared_ptr<CompilerContext> createContext(IEngine *engine, Target *target);
147+
88148
private:
89149
spimpl::unique_impl_ptr<CompilerPrivate> impl;
90150
};

include/scratchcpp/dev/compilercontext.h renamed to include/scratchcpp/compilercontext.h

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

33
#pragma once
44

5-
#include "../global.h"
6-
#include "../spimpl.h"
5+
#include "global.h"
6+
#include "spimpl.h"
77

88
namespace libscratchcpp
99
{

0 commit comments

Comments
 (0)