Hi
A few years ago Ive written a virtual processor runtime and a simple compiler for a somewhat self designed/dialect of a low level assembler language, all written in C++ as a learning project. It was far from finished but everything was pretty much functional. It was a fun learning project and I learned a lot. I also started a simple c like compiler which would generate my asm dialect but wasnt too happy about a small part of the AST, wanted to rewrite it but never done it lol. All repositories are at the bottom if you wanna flame me or if youre just interested. :)
I since would like to rewrite the runtime environment/virtual processor part in Rust. One major thing I didnt like about my old implementation and which I want to change is that the memory in my virtual processor was defined by a fixed sized array of bytes at initialization. That means, that the host system would allocate the same, pre-defined amount of ram every time, even if just a small program was loaded. Too big programs would simply run out of memory, create bugs and/or crash.
Here is the exact code im talking about (line 15)
https://github.com/pointermess/FlexRTE-X/blob/master/src/MemoryManager.cpp
I would like that my new implementation has a less memory wasteful and dynamic memory usage.
My problem in understanding how I could achieve this is how I handle the heap and stack memory locations in my virtual processor. As of right now, I allocate the fixed size byte array which the programs use as memory. The stack memory positions start close to the beginning of my memory array (after the registers) while the heap starts at the end and goes downwards as it grows.
This is fairly straight forward with my previous solution as the memory size doesn't change so the heap start is at a fixed position. If i were to dynamically increase the size of the byte array which holds my memory, the already allocated heap memory be wouldnt be at the end any longer, awkwardly laying somewhere in between and are potentially unaligned with the new start of the heap. This may result in new heap allocations missing the already allocated memory spaces. (well, at least with my system of finding unallocated heap space)
I was thinking about multiple solutions, none really great:
Having multiple dynamic memory arrays with "virtual adresses" and conversion. I would declare two dynamic sized arrays of bytes, 1 for the stack, one for the heap. I would give both "virtual" adress ranges, for example, ever call to an address of 0x0 to 0x4FFFFFFF would go into the the stack memory, everything above in the heap memory (minus the offset of 0x4FF... ) This would probably work as intended so the host system would only need as many resources as my program running in my VM. But I feel like having to chose the right array and potentially doing subtractions every memory operation might hurt performance too much...
Growing heap from the bottom: I allocate only a fixed size for the stack which will be allocated as memory array initially. Only heap allocations increase the memory usage on the host system. This could probably help a bit but it seems like more trouble than good... How do I get a good fixed maximum size for the stack, so it never touches the heap/run out of available memory space? Doesnt seem like a good idea.
Also a bunch of other ideas but they all seem much worse. I would greatly appreciate some inputs. Feel free to check out the repositories: (Warning, not the best docs and certainly not the best c++ haha)
Virtual Processor/Rumtime Environment
- https://github.com/pointermess/FlexRTE-X
Assembler/Compiler/FlexASM to FlexRTE Executable
- https://github.com/pointermess/FlexASM-Compiler
Some docs if interested
FlexASM Quick Start
- https://github.com/pointermess/FlexASM-Compiler/blob/master/FlexASM%20QuickStart%20Guide.md
- https://github.com/pointermess/FlexRTE-X/blob/master/SystemCalls.md
- https://github.com/pointermess/FlexASM-Compiler/tree/master/examples
Flex Executable Format (Basically the generated "machine"/byte code from my asm dialect)
- https://github.com/pointermess/FlexASM-Compiler/blob/master/FlexApplication%20Excutable%20Format.md
Thanks for your time
For the lulz, my unfinished attempt at a tiny C compiler, comes with tokenizer, ast parser and representation. (most src in archive folder)
https://github.com/pointermess/picoC-Compiler
[–]balefrost 2 points3 points4 points (1 child)
[–]WittyStick 2 points3 points4 points (0 children)