4 ms·
We just discussed one aspect of this here: https://www.reddit.com/r/programming/comments/8a9zhz/implementing_a_stackbased_vm_in_c_part_of/dwxg5jp/?context=3 htt
by chubot 9y ago
We just discussed one aspect of this here: https://www.reddit.com/r/programming/comments/8a9zhz/implementing_a_stackbased_vm_in_c_part_of/dwxg5jp/?context=3 https://www.reddit.com/r/programming/comments/8a9zhz/impleme...
There is no real way for the VM to avoid dynamically allocating memory. At the very least, you have to allocate call frames (even if you know their size statically).
Maybe the only way around this is if you restrict arbitrary recursion in the language.
EDIT: Hm from the reply to that I was somewhat wrong...
- joe_the_user 9y agoThanks for the answer. "No real way" is relative, of course. I happen to have constraints pushing for a VM as small as conceivably possible without code-blow-up, like single-instruction VM but implementing most logic/arithmetic primitives. But which parts are dicier than others is worth considering.
- chubot 9y agoYeah I was a little wrong, and I edited my comment. But I am still confused by your question. There are lots of places where you might need to allocate memory -- it depends on the semantics of your source language, and it depends what you consider part of the VM proper, etc. It sounds like you are describing something with more primitive types, like a CPU emulator or WebAssembly, rather than a dynamic language VM, which is the subject of this book.
- joe_the_user 9y agoSure, that explains it. The VM has to allocate memory if it's interpreting a dynamic language. Sorry if the question was elementary.
- chubot 9y agoOK no problem, it would be interesting to see a comparison between the two styles of VM! I remember that there is at least one talk here which goes into the difference: http://soft-dev.org/events/vmss16/ http://soft-dev.org/events/vmss16/ I forget what the different names he used. He distinguished between runtimes like the JVM and runtimes like Xen, which emulate an operating system too. A CPU emulator seems closer to Xen than the JVM. ----- EDIT: The names were "system" and "process" VMs, mentioned here: https://news.ycombinator.com/item?id=16777564 https://news.ycombinator.com/item?id=16777564 So I think you are talking about a "system VM", where this book is more about "process" VMs. They are related, but distinct -- it even says so in that book's title! It's definitely true that in something like Xen you would want to avoid allocating memory more than in something like a JavaScript VM. (And it would be easier to avoid allocating, too.) I don't like that terminology, but it's a useful distinction.
- 35bge57dtjku 9y agoI don't think it's because it's a dynamic language like you seem to imply. How else would you implement both stack and heap in non-'dynamic' languages without constraining them to a fixed size?
- e12e 9y agoSay you want to model a Turing machine. You can allocate a fixed length tape (making a linear bounded automaton), or a tape that can grow (closer to a "real" Turing machine). Now, you could argue that writing to a part of the finite tape that's so far clear is "allocating memory" - but at the same time that could be a single section of statically allocated memory from the perspective of the Os that runs your vm... For some related fun, I still think the nim "howistart" is great (but I haven't tried to follow along in a while, not sure if the code works straight away with up to date nim (but I see there's a notice mentioning a 2018 update, so I hope it's still /again working!)): http://howistart.org/posts/nim/1/ http://howistart.org/posts/nim/1/
- e12e 9y agoAlthough, looking at the github repo; I'm confused. Can't find the change related to 2018 update, but there's this: https://github.com/howistart/howistart/pull/50/files https://github.com/howistart/howistart/pull/50/files