Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
d1cfb5d
Target: Add listData() method
adazem009 Nov 12, 2024
a22f8d3
LLVMCodeBuilder: Initial list limplementation
adazem009 Nov 12, 2024
8d0d24f
Add list_clear() function
adazem009 Nov 12, 2024
6ad13ee
LLVMCodeBuilder: Implement list clear
adazem009 Nov 12, 2024
06f551e
Compiler: Add custom if statement methods
adazem009 Nov 14, 2024
c304971
Add list_remove() function
adazem009 Nov 16, 2024
57574c4
LLVMCodeBuilder: Implement remove list item
adazem009 Nov 16, 2024
ef19574
LLVMCodeBuilder: Fix return types of list functions
adazem009 Nov 16, 2024
4901aa0
LLVMCodeBuilder: Fix value type field index
adazem009 Nov 16, 2024
9e790d0
Add value_append_empty() function
adazem009 Nov 16, 2024
fd3cc6c
LLVMCodeBuilder: Implement list append
adazem009 Nov 16, 2024
f5c1895
List: Add insert_empty() method
adazem009 Nov 17, 2024
138197f
Add list_insert_empty() function
adazem009 Nov 17, 2024
cf0f440
LLVMCodeBuilder: Allow different arg types in createOp()
adazem009 Nov 17, 2024
14840cf
LLVMCodeBuilder: Implement list insert
adazem009 Nov 17, 2024
79c605f
Add list_get_item() function
adazem009 Nov 17, 2024
aa1d055
LLVMCodeBuilder: Implement list replace
adazem009 Nov 17, 2024
d60cbe7
LLVMCodeBuilder: Implement get list item
adazem009 Nov 18, 2024
116121b
Add list_size() function
adazem009 Nov 18, 2024
ae122fc
LLVMCodeBuilder: Implement list size
adazem009 Nov 18, 2024
1b2a68e
List: Add data() method
adazem009 Nov 19, 2024
cd86c35
Add list_data() function
adazem009 Nov 19, 2024
20d6b84
LLVMTypes: Add missing padding to ValueData struct
adazem009 Nov 19, 2024
cc397a5
LLVMCodeBuilder: Use data pointer to access list data
adazem009 Nov 19, 2024
f9d1178
Target: Free list data array
adazem009 Nov 19, 2024
8e559f1
List: Add sizePtr() method
adazem009 Nov 19, 2024
06bb0b1
Add list_size_ptr() function
adazem009 Nov 19, 2024
19b76af
LLVMCodeBuilder: Optimize list size
adazem009 Nov 19, 2024
0ff30d0
Add sizePtr() method to veque
adazem009 Nov 19, 2024
3a8bbb4
List: Add allocatedSizePtr() method
adazem009 Nov 19, 2024
2f2ed14
Add list_alloc_size_ptr() function
adazem009 Nov 19, 2024
94fc6b1
LLVMCodeBuilder: Do not use alloca for list size
adazem009 Nov 19, 2024
d429e0a
LLVMCodeBuilder: Optimize list append
adazem009 Nov 20, 2024
f982aae
LLVMCodeBuilder: Update list data pointer when needed
adazem009 Nov 20, 2024
e139e3c
LLVMCodeBuilder: Add missing ValueData padding
adazem009 Nov 21, 2024
b1319ed
LLVMCodeBuilder: Fix ValueData constants
adazem009 Nov 21, 2024
b05996c
LLVMCodeBuilder: Implement list item index
adazem009 Nov 21, 2024
c40db7f
LLVMCodeBuilder: Uncomment list replace test
adazem009 Nov 21, 2024
b0d2666
LLVMCodeBuilder: Implement list contains
adazem009 Nov 21, 2024
d071783
Compiler: Add list methods
adazem009 Nov 21, 2024
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
14 changes: 14 additions & 0 deletions include/scratchcpp/dev/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class LIBSCRATCHCPP_EXPORT Compiler
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();
Expand Down Expand Up @@ -80,6 +84,16 @@ class LIBSCRATCHCPP_EXPORT Compiler

void createVariableWrite(Variable *variable);

void createListClear(List *list);
void createListRemove(List *list);
void createListAppend(List *list);
void createListInsert(List *list);
void createListReplace(List *list);

void beginIfStatement();
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);
Expand Down
24 changes: 21 additions & 3 deletions include/scratchcpp/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ class LIBSCRATCHCPP_EXPORT List : public Entity
Monitor *monitor() const;
void setMonitor(Monitor *monitor);

/*! Returns a pointer to the raw list data. */
inline ValueData *data() const { return m_dataPtr->data(); }

/*!
* Returns a pointer to the list size.
* \note This is used internally by compiled code for various optimizations.
*/
inline size_t *sizePtr() { return &m_size; }

/*!
* Returns a pointer to the allocated list size.
* \note This is used internally by compiled code for various optimizations.
*/
inline const size_t *allocatedSizePtr() const { return m_dataPtr->sizePtr(); }

/*! Returns the list size. */
inline size_t size() const { return m_size; }

Expand Down Expand Up @@ -96,16 +111,19 @@ class LIBSCRATCHCPP_EXPORT List : public Entity
m_size--;
}

/*! Inserts an item at index. */
inline void insert(size_t index, const ValueData &value)
/*! Inserts an empty item at index and returns the reference to it. Can be used for custom initialization. */
inline ValueData &insertEmpty(size_t index)
{
assert(index >= 0 && index <= size());
m_size++;
reserve(getAllocSize(m_size));
std::rotate(m_dataPtr->rbegin() + m_dataPtr->size() - m_size, m_dataPtr->rbegin() + m_dataPtr->size() - m_size + 1, m_dataPtr->rend() - index);
value_assign_copy(&m_dataPtr->operator[](index), &value);
return m_dataPtr->operator[](index);
}

/*! Inserts an item at index. */
inline void insert(size_t index, const ValueData &value) { value_assign_copy(&insertEmpty(index), &value); }

/*! Inserts an item at index. */
inline void insert(size_t index, const Value &value) { insert(index, value.data()); }

Expand Down
2 changes: 2 additions & 0 deletions include/scratchcpp/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class LIBSCRATCHCPP_EXPORT Target : public Drawable
int findList(const std::string &listName) const;
int findListById(const std::string &id) const;

List **listData();

const std::vector<std::shared_ptr<Block>> &blocks() const;
int addBlock(std::shared_ptr<Block> block);
std::shared_ptr<Block> blockAt(int index) const;
Expand Down
5 changes: 5 additions & 0 deletions include/scratchcpp/veque.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ namespace veque
return _size;
}

// For libscratchcpp List
inline const size_type *sizePtr() const noexcept {
return &_size;
}

size_type max_size() const noexcept
{
constexpr auto compile_time_limit = std::min(
Expand Down
96 changes: 94 additions & 2 deletions src/dev/engine/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ std::shared_ptr<ExecutableCode> Compiler::compile(std::shared_ptr<Block> startBl
impl->block = startBlock;

while (impl->block) {
if (impl->block->compileFunction())
if (impl->block->compileFunction()) {
assert(impl->customIfStatementCount == 0);
impl->block->compile(this);
else {

if (impl->customIfStatementCount > 0) {
std::cerr << "error: if statement created by block '" << impl->block->opcode() << "' not terminated" << std::endl;
assert(false);
}
} else {
std::cout << "warning: unsupported block: " << impl->block->opcode() << std::endl;
impl->unsupportedBlocks.insert(impl->block->opcode());
}
Expand Down Expand Up @@ -94,6 +100,30 @@ void Compiler::addListContents(List *list)
impl->builder->addListContents(list);
}

/*! Adds the item with index from the last value of the given list to the code. */
void Compiler::addListItem(List *list)
{
impl->builder->addListItem(list);
}

/*! Adds the index of the item from the last value of the given list to the code. */
void Compiler::addListItemIndex(List *list)
{
impl->builder->addListItemIndex(list);
}

/*! Adds the result of a list contains item from the check to the code. */
void Compiler::addListContains(List *list)
{
impl->builder->addListContains(list);
}

/*! Adds the length of the given list to the code. */
void Compiler::addListSize(List *list)
{
impl->builder->addListSize(list);
}

/*! Compiles the given input (resolved by name) and adds it to the compiled code. */
void Compiler::addInput(const std::string &name)
{
Expand Down Expand Up @@ -262,6 +292,68 @@ void Compiler::createVariableWrite(Variable *variable)
impl->builder->createVariableWrite(variable);
}

/*! Creates a clear list operation. */
void Compiler::createListClear(List *list)
{
impl->builder->createListClear(list);
}

/*!
* Creates a remove item from list operation.
* \note The index starts with 0 and is cast to number, special strings like "last" are not handled.
*/
void Compiler::createListRemove(List *list)
{
impl->builder->createListRemove(list);
}

/*! Creates a list append operation using the last value. */
void Compiler::createListAppend(List *list)
{
impl->builder->createListAppend(list);
}

/*! Creates a list insert operation using the last 2 values (index, value). */
void Compiler::createListInsert(List *list)
{
impl->builder->createListInsert(list);
}

/*! Creates a list replace operation using the last 2 values (index, value). */
void Compiler::createListReplace(List *list)
{
impl->builder->createListReplace(list);
}

/*!
* Starts a custom if statement.
* \note The if statement must be terminated using endIf() after compiling your block.
*/
void Compiler::beginIfStatement()
{
impl->builder->beginIfStatement();
impl->customIfStatementCount++;
}

/*! Starts the else branch of custom if statement. */
void Compiler::beginElseBranch()
{
impl->builder->beginElseBranch();
}

/*! Ends custom if statement. */
void Compiler::endIf()
{
if (impl->customIfStatementCount == 0) {
std::cerr << "error: called Compiler::endIf() without an if statement";
assert(false);
return;
}

impl->builder->endIf();
impl->customIfStatementCount--;
}

/*! Jumps to the given if substack. */
void Compiler::moveToIf(std::shared_ptr<Block> substack)
{
Expand Down
1 change: 1 addition & 0 deletions src/dev/engine/compiler_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct CompilerPrivate
Target *target = nullptr;

std::shared_ptr<Block> block;
int customIfStatementCount = 0;
std::vector<std::pair<std::pair<std::shared_ptr<Block>, std::shared_ptr<Block>>, SubstackType>> substackTree;
bool substackHit = false;
bool warp = false;
Expand Down
10 changes: 10 additions & 0 deletions src/dev/engine/internal/icodebuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class ICodeBuilder
virtual void addConstValue(const Value &value) = 0;
virtual void addVariableValue(Variable *variable) = 0;
virtual void addListContents(List *list) = 0;
virtual void addListItem(List *list) = 0;
virtual void addListItemIndex(List *list) = 0;
virtual void addListContains(List *list) = 0;
virtual void addListSize(List *list) = 0;

virtual void createAdd() = 0;
virtual void createSub() = 0;
Expand Down Expand Up @@ -56,6 +60,12 @@ class ICodeBuilder

virtual void createVariableWrite(Variable *variable) = 0;

virtual void createListClear(List *list) = 0;
virtual void createListRemove(List *list) = 0;
virtual void createListAppend(List *list) = 0;
virtual void createListInsert(List *list) = 0;
virtual void createListReplace(List *list) = 0;

virtual void beginIfStatement() = 0;
virtual void beginElseBranch() = 0;
virtual void endIf() = 0;
Expand Down
1 change: 1 addition & 0 deletions src/dev/engine/internal/llvm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ target_sources(scratchcpp
llvmcoroutine.cpp
llvmcoroutine.h
llvmvariableptr.h
llvmlistptr.h
llvmprocedure.h
llvmtypes.cpp
llvmtypes.h
Expand Down
Loading