Recently I learned some techique on making a VM with Mark and Sweep GC. I found that I must provide a way to allow the GC program detect if a item on stack has a type of reference, so that it can do recursive marking procedure from that item.
To achieve the goal, I design the stack as an array of item with uint64_t
, whose lowest bit is reserved to be a reference flag and the rest 63 bit can store data of any type defined at other place. If the item is a reference, its flag will be set to 1, otherwise 0. When a program is about to store some native data to the stack, it left shift the data, then set the lowest bit according to the data type. When a program is going to get some data back from the stack, it will right shift the item to discard the reference flag.
But these method is quite imperfect. For example limit the range of some type like int64_t
, and uint64_t
. As the one of the most efficency and popular VM nowaday, I really want to know how JVM cope with this problem.
I have searched Google but it didn't provide some useful imformations for me.