最近,为了好玩,我决定建立一个玩具编程,编译器和虚拟机。在开始实现虚拟机时,我被卡住了。包含变量和结构的堆栈,我为每种类型实现为单独的数组。问题是,当我引用一个结构时,元素不对齐,intstruct.x可能位于地址2,浮点struct.y可能位于地址56,因此不可能通过引用访问结构,因为索引不是线性的。我怎么能解决这个问题?
编辑:
first of all for each type I mean for each primitive, and second 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.发布于 2017-06-29 04:23:37
在本例中,除了使用use 32_t/ into 64_t这样的单一数据类型之外,您别无选择,只需让编译器将值分解为整数。
int sp = 0;
uint32_t stack[MAX_STACK_SIZE];或
就像其他人说的,创建一个堆栈,它是一个联合数组,可能使用一个标记的联合。一个实现可能是..。
union values {
int i;
float f;
};
struct Type {
int tag;
union values val;
};
Type stack[MAX_STACK_SIZE];这件事由你来决定,但通常是这样做的。
https://stackoverflow.com/questions/33985848
复制相似问题