Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ if (LIBSCRATCHCPP_USE_LLVM)
target_sources(scratchcpp
PUBLIC
include/scratchcpp/dev/compiler.h
include/scratchcpp/dev/compilervalue.h
include/scratchcpp/dev/compilerconstant.h
include/scratchcpp/dev/executablecode.h
include/scratchcpp/dev/executioncontext.h
)
Expand Down
9 changes: 8 additions & 1 deletion include/scratchcpp/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace libscratchcpp

class IEngine;
class Target;
#ifdef USE_LLVM
class CompilerValue;
#endif
class Input;
class Field;
class Comment;
Expand All @@ -25,7 +28,11 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
Block(const std::string &id, const std::string &opcode);
Block(const Block &) = delete;

#ifdef USE_LLVM
CompilerValue *compile(Compiler *compiler);
#else
void compile(Compiler *compiler);
#endif

const std::string &opcode() const;

Expand Down Expand Up @@ -76,7 +83,7 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
BlockComp compileFunction() const;
void setCompileFunction(BlockComp newCompileFunction);

BlockComp hatPredicateCompileFunction() const;
HatPredicateCompileFunc hatPredicateCompileFunction() const;
void setHatPredicateCompileFunction(HatPredicateCompileFunc newHatPredicateCompileFunction);

bool mutationHasNext() const;
Expand Down
109 changes: 57 additions & 52 deletions include/scratchcpp/dev/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace libscratchcpp
class IEngine;
class Target;
class ExecutableCode;
class CompilerValue;
class CompilerConstant;
class Variable;
class List;
class Input;
Expand All @@ -33,6 +35,9 @@ class LIBSCRATCHCPP_EXPORT Compiler
Unknown
};

using ArgTypes = std::vector<StaticType>;
using Args = std::vector<CompilerValue *>;

Compiler(IEngine *engine, Target *target);
Compiler(const Compiler &) = delete;

Expand All @@ -42,63 +47,63 @@ class LIBSCRATCHCPP_EXPORT Compiler

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

void addFunctionCall(const std::string &functionName, StaticType returnType = StaticType::Void, const std::vector<StaticType> &argTypes = {});
void addConstValue(const Value &value);
void addVariableValue(Variable *variable);
void addListContents(List *list);
void addListItem(List *list);
void addListItemIndex(List *list);
void addListContains(List *list);
void addListSize(List *list);
void addInput(const std::string &name);

void createAdd();
void createSub();
void createMul();
void createDiv();

void createCmpEQ();
void createCmpGT();
void createCmpLT();

void createAnd();
void createOr();
void createNot();

void createMod();
void createRound();
void createAbs();
void createFloor();
void createCeil();
void createSqrt();
void createSin();
void createCos();
void createTan();
void createAsin();
void createAcos();
void createAtan();
void createLn();
void createLog10();
void createExp();
void createExp10();

void createVariableWrite(Variable *variable);
CompilerValue *addFunctionCall(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
CompilerConstant *addConstValue(const Value &value);
CompilerValue *addVariableValue(Variable *variable);
CompilerValue *addListContents(List *list);
CompilerValue *addListItem(List *list, CompilerValue *index);
CompilerValue *addListItemIndex(List *list, CompilerValue *item);
CompilerValue *addListContains(List *list, CompilerValue *item);
CompilerValue *addListSize(List *list);
CompilerValue *addInput(const std::string &name);

CompilerValue *createAdd(CompilerValue *operand1, CompilerValue *operand2);
CompilerValue *createSub(CompilerValue *operand1, CompilerValue *operand2);
CompilerValue *createMul(CompilerValue *operand1, CompilerValue *operand2);
CompilerValue *createDiv(CompilerValue *operand1, CompilerValue *operand2);

CompilerValue *createCmpEQ(CompilerValue *operand1, CompilerValue *operand2);
CompilerValue *createCmpGT(CompilerValue *operand1, CompilerValue *operand2);
CompilerValue *createCmpLT(CompilerValue *operand1, CompilerValue *operand2);

CompilerValue *createAnd(CompilerValue *operand1, CompilerValue *operand2);
CompilerValue *createOr(CompilerValue *operand1, CompilerValue *operand2);
CompilerValue *createNot(CompilerValue *operand);

CompilerValue *createMod(CompilerValue *num1, CompilerValue *num2);
CompilerValue *createRound(CompilerValue *num);
CompilerValue *createAbs(CompilerValue *num);
CompilerValue *createFloor(CompilerValue *num);
CompilerValue *createCeil(CompilerValue *num);
CompilerValue *createSqrt(CompilerValue *num);
CompilerValue *createSin(CompilerValue *num);
CompilerValue *createCos(CompilerValue *num);
CompilerValue *createTan(CompilerValue *num);
CompilerValue *createAsin(CompilerValue *num);
CompilerValue *createAcos(CompilerValue *num);
CompilerValue *createAtan(CompilerValue *num);
CompilerValue *createLn(CompilerValue *num);
CompilerValue *createLog10(CompilerValue *num);
CompilerValue *createExp(CompilerValue *num);
CompilerValue *createExp10(CompilerValue *num);

void createVariableWrite(Variable *variable, CompilerValue *value);

void createListClear(List *list);
void createListRemove(List *list);
void createListAppend(List *list);
void createListInsert(List *list);
void createListReplace(List *list);
void createListRemove(List *list, CompilerValue *index);
void createListAppend(List *list, CompilerValue *item);
void createListInsert(List *list, CompilerValue *index, CompilerValue *item);
void createListReplace(List *list, CompilerValue *index, CompilerValue *item);

void beginIfStatement();
void beginIfStatement(CompilerValue *cond);
void beginElseBranch();
void endIf();

void moveToIf(std::shared_ptr<Block> substack);
void moveToIfElse(std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2);
void moveToRepeatLoop(std::shared_ptr<Block> substack);
void moveToWhileLoop(std::shared_ptr<Block> substack);
void moveToRepeatUntilLoop(std::shared_ptr<Block> substack);
void moveToIf(CompilerValue *cond, std::shared_ptr<Block> substack);
void moveToIfElse(CompilerValue *cond, std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2);
void moveToRepeatLoop(CompilerValue *count, std::shared_ptr<Block> substack);
void moveToWhileLoop(CompilerValue *cond, std::shared_ptr<Block> substack);
void moveToRepeatUntilLoop(CompilerValue *cond, std::shared_ptr<Block> substack);
void beginLoopCondition();
void warp();

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

private:
void addInput(Input *input);
CompilerValue *addInput(Input *input);

spimpl::unique_impl_ptr<CompilerPrivate> impl;
};
Expand Down
27 changes: 27 additions & 0 deletions include/scratchcpp/dev/compilerconstant.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "compilervalue.h"

namespace libscratchcpp
{

class CompilerConstantPrivate;

/*! \brief The CompilerConstant class represents a constant value in compiled code. */
class LIBSCRATCHCPP_EXPORT CompilerConstant : public CompilerValue
{
public:
CompilerConstant(Compiler::StaticType type, const Value &value);
CompilerConstant(const CompilerConstant &) = delete;

bool isConst() const override final { return true; };

const Value &value() const;

private:
spimpl::unique_impl_ptr<CompilerConstantPrivate> impl;
};

} // namespace libscratchcpp
29 changes: 29 additions & 0 deletions include/scratchcpp/dev/compilervalue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "compiler.h"

namespace libscratchcpp
{

class CompilerValuePrivate;

/*! \brief The CompilerValue class represents a local value in compiled code. */
class LIBSCRATCHCPP_EXPORT CompilerValue
{
public:
CompilerValue(Compiler::StaticType type);
CompilerValue(const CompilerValue &) = delete;
virtual ~CompilerValue() { }

Compiler::StaticType type() const;
void setType(Compiler::StaticType type);

virtual bool isConst() const { return false; };

private:
spimpl::unique_impl_ptr<CompilerValuePrivate> impl;
};

} // namespace libscratchcpp
21 changes: 21 additions & 0 deletions include/scratchcpp/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ namespace libscratchcpp

class VirtualMachine;
class Compiler;
#ifdef USE_LLVM
class CompilerValue;
#endif
class Block;
class Value;

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

#ifdef USE_LLVM
/*!
* \typedef BlockComp
*
* BlockComp is a function pointer for functions which are used to compile blocks.
*/
using BlockComp = CompilerValue *(*)(Compiler *);
#else
/*!
* \typedef BlockComp
*
* BlockComp is a function pointer for functions which are used to compile blocks to bytecode.
*/
using BlockComp = void (*)(Compiler *);
#endif // USE_LLVM

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

#ifdef USE_LLVM
/*!
* \typedef HatPredicateCompileFunc
*
* HatPredicateCompileFunc is a function pointer for functions which are used to compile edge-activated hat predicates.
*/
using HatPredicateCompileFunc = CompilerValue *(*)(Compiler *vm);
#else
/*!
* \typedef HatPredicateCompileFunc
*
* HatPredicateCompileFunc is a function pointer for functions which are used to compile edge-activated hat predicates to bytecode.
*/
using HatPredicateCompileFunc = void (*)(Compiler *vm);
#endif // USE_LLVM

} // namespace libscratchcpp

Expand Down
7 changes: 7 additions & 0 deletions include/scratchcpp/inputvalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
namespace libscratchcpp
{

#ifdef USE_LLVM
class CompilerValue;
#endif
class Block;
class Entity;
class InputValuePrivate;
Expand All @@ -34,7 +37,11 @@ class LIBSCRATCHCPP_EXPORT InputValue

InputValue(Type type = Type::Number);

#ifdef USE_LLVM
CompilerValue *compile(Compiler *compiler);
#else
void compile(Compiler *compiler);
#endif

Type type() const;
void setType(Type newType);
Expand Down
6 changes: 6 additions & 0 deletions src/dev/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ target_sources(scratchcpp
compiler.cpp
compiler_p.cpp
compiler_p.h
compilervalue.cpp
compilervalue_p.cpp
compilervalue_p.h
compilerconstant.cpp
compilerconstant_p.cpp
compilerconstant_p.h
executioncontext.cpp
executioncontext_p.cpp
executioncontext_p.h
Expand Down
Loading