Open
Description
This player is going to be feature-complete soon, so I should start thinking about what to do with the current bytecode based virtual machine. It wasn't the best idea because it isn't fast. Scratch code can be compiled to LLVM IR instead which is then compiled to the architecture specific machine code by LLVM.
If anyone has experience with this, feel free to comment with any ideas :)
Notes:
- I don't want to write a JIT. I'd prefer ahead-of-time compilation when projects are loaded (just like the current compiler does it).
- This will require rewriting/refactoring all Scratch blocks because they rely on the bytecode VM.
- Since it'll take a long time to implement, I'll most likely keep the current implementation and make the LLVM-based compiler optional while it's being developed.
How will it work?
- Every Scratch script will have LLVM IR which will be executed using LLVM's ExecutionEngine with JIT compilation. It should be fast enough. If not, it should be possible to implement hacks like compiling to native code and running it from memory, but that's not portable.
- Since Scratch scripts must be able to yield at random points (for example at the end of a loop, if not running without screen refresh), coroutines will be used.
- A coroutine will execute the sequence for the appropriate part of the script (block functions in the sequence will be declared with
extern "C"
). - If the code has to yield, the coroutine will suspend (e. g. in a loop). The execution context will store the coroutine handle so it can resume later.
- If the script has to restart (current VM has a function that makes it jump to the beginning), it'll just destroy the coroutine handle and call the coroutine.
- If the code can run without screen refresh, there's no need to use coroutines which should be faster.
Progress:
- API preparation: Refactor Value class
- API preparation: Refactor List class
- API preparation: Add Thread class
- API preparation: Refactor extension API #393
- Add Compiler class (in
dev
folder to keep the old implementation) - Add code builder class (abstract)
- Add LLVM code builder class
- Add a class for executing compiled code
- Add API for Scratch blocks
- If statements
- Loops
- Yielding
- Operators
- Math instructions
- Variables
- Lists
- Promises
- API for testing Scratch blocks
- Motion blocks
- Looks blocks
- Sound blocks
- Event blocks
- Control blocks
- Sensing blocks
- Operator blocks
- Custom blocks
- Variable blocks
- List blocks
- Edge-activated hats
- Monitors
- Fix memory leaks in unfinished coroutines
- Implement type prediction within scripts
- Implement type prediction within sprites (local variables/lists)
- Implement type prediction within project (global variables/lists)
- Cache converted constants (custom block arguments, constant values, constant variables, etc.)
- Implement custom block inlining
- Remove bytecode VM
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
In Progress