5 ms·
Small VMs and Coroutines
- naasking 4y agoInstead of manually compiling x64 ASM, you could use something like GNU lightning: https://www.gnu.org/software/lightning/ https://www.gnu.org/software/lightning/
- Joker_vD 4y ago> abstracts over the target CPU, as it exposes to the clients a standardized RISC instruction set inspired by the MIPS and SPARC chips. A-ha-ha, well, it kinda does except that it has restrictions similar to x86 (for ease of implementation?): there are only 6 registers (3 preserved across function calls and 3 that are not), a division instruction can mangle R0 if you don't tell it not to, etc. How many "compiling one fantasy ISA to another fantasy ISA" can we realistically chain with ease anyhow?
- naasking 4y agoLightning gives you access to the full register set if you want to compile to an ABI of your own design.
- samsquire 4y agoThanks for submitting this article and thank you to the author. This is exactly the kind of technical post I love to see. I recently wrote a simple barebones toy amd64 x86_64 compiler and coroutines would be a beautiful thing to add to the compiler. I think I can learn from this and the Inko programming language which refers to Tina a coroutine library https://github.com/samsquire/compiler https://github.com/samsquire/compiler https://slembcke.github.io/Tina https://slembcke.github.io/Tina This area "coroutines" is an area I am particularly interested in. I recently wrote an unrolled switch based state machine async/await implementation in Java which can run tasks on a background thread. When I say unrolled I mean that this work would usually be done by a transpiler or macros but I wrote the switch by hand to see if it would work. This is similar to protothreads for C. My async/await unrolled switch statement could in theory be implemented in any language that has switch statements and without threading. See the Java code here. The switch statement represents a state machine that decides whether to fork or yield or execute statements of the following virtual program: task1: while True: handle = async task2() handle2 = async task3() # if threading were enabled, this would eagerly run in parallel to task2, simultaneously rvalue1 = await handle rvalue2 = await handle2 task2: while True: yield value++ task3: while True: yield value2++ https://github.com/samsquire/multiversion-concurrency-control/blob/main/src/main/java/main/TokenRingTimer2AsyncAwait3.java https://github.com/samsquire/multiversion-concurrency-contro... I am still working on allowing multiple coroutines to be in flight in parallel at the same time. At the moment the tasks share the same background thread. I asked this stackoverflow question regarding C++ coroutines, as I wanted to use coroutines with a thread pool. https://stackoverflow.com/questions/74520133/how-can-i-pass-things-to-a-c20-coroutine-co-await-await-suspend-operator-funct https://stackoverflow.com/questions/74520133/how-can-i-pass-... Another avenue of my research into parallelism is preempting threads from other threads by setting other thread's looping variable to the limit. I feel this could be an alternative to doing a check inside a loop that the author does. Code here in Rust, Java and C shows this userspace 1:M:N scheduler where N lightweight threads are multiplexed over M kernel threads and 1 scheduler thread. This is similar to a golang runtime. https://github.com/samsquire/preemptible-thread https://github.com/samsquire/preemptible-thread
- 7373737373 4y agoSounds like a cool idea! I encourage y'all to check out Space Engineer's scripting system: https://spaceengineers.fandom.com/wiki/Scripting https://spaceengineers.fandom.com/wiki/Scripting as well as Stackless Python's ability to noncooperatively preempt tasklets: https://stackless.readthedocs.io/en/2.7-slp/library/stackless/pickling.html https://stackless.readthedocs.io/en/2.7-slp/library/stackles... and eWasm metering, which uses a similar approach to the authors': https://ewasm.readthedocs.io/en/mkdocs/metering/ https://ewasm.readthedocs.io/en/mkdocs/metering/
- ngcc_hk 4y agoInteresting to learn more about Co-routine. Sometimes one does want to have a context e.g. cf other vim or assembly implementation of the same idea. That will help others to understand why this way. Further, for the game given this mass of actor executes some update is so common how to cf with other mainstream options. The reference to c and rust is interesting. Are all use assembler?
- frutiger 4y agoNot sure if the author is reading, but > mov rdx, end_unit_stack Should that say `end_data_stack`?
- MarceColl 4y agomy bad yeah! fixing
- astrange 4y agoYou can coroutine if you want, but it's not difficult to write an efficient interpreter - some keywords are "direct threading" or "code copying JIT".
- MarceColl 4y agoI think you are mixing things here. Only the first part of the post is about "writing" an interpreter for this fantasy ISA, for which I leverage the macro system of fasm. direct threading and code copying JIT has nothing to do with the coroutine part of the post.
- astrange 4y ago> Some years ago I did some tests around this idea and the VMs I came up with were underperforming messes of either rust or C. There was no way I could deliver the performance I had in mind with that. It's a way to do that efficiently.
- pjmlp 4y agoThis is exactly the approach we took back on my CS degree for our toy language. Instead of interpreting the generated bytecodes, we made use of the macro assembler capabilities from nasm, so in the end we had the satisfation of a proper compiler, even if the generated code wasn't that great.
- MarceColl 4y agoKinda unexpected waking up and seeing this here. This was more about recording my learnings than anything else. Feel free to leave some feedback. You can also check the discussion over at lobste.rs: https://lobste.rs/s/jrp9gv/small_vms_coroutines https://lobste.rs/s/jrp9gv/small_vms_coroutines
- petercooper 4y agoThis was more about recording my learnings than anything else. Best sort of technical blog post, I tend to find!
- unwind 4y agoVery cool and informative article, I was at least happy to see it here. There is a small typo, in the first whole-program assembler output (before the "Coroutines" header): jmp target is a copy-paste from the macro definition, it should of course be: jmp loop