Recently for fun I have decided to build a toy programming, compiler and a low-level register based interpreted vm. While starting to implement the virtual machine I got stuck. The stack which holds the variables and structs I implemented as separate arrays for each type. The problem is when I have a reference to a struct the elements are not aligned, int struct.x might be at address 1, and float struct.y might be at address 0, so accessing the struct by a reference would be impossible, because the indexes are not linear. How could I solve this?
stack int:
//other 0: 5,
//x 1: 67
stack long:
//y 0: 56
stack reference:
//pointer to x, struct access like c array: #00x01
For each type I mean for each primitive. I know I could implement it with unions but I want to learn how it is really implemented in java, c++ or c#, that's kind of the point of making a toy language, to better understand what you are programming.