对象和结构成员访问和地址偏移量计算 [英] Object and struct member access and address offset calculation

查看:107
本文介绍了对象和结构成员访问和地址偏移量计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的VM,并且对实现对象和结构成员访问有疑问.

I am writing a simple VM and I have a question on implementing object and structure member access.

由于程序的开始地址在每次运行时都是任意的,因此随后每个程序对象的地址也是任意的.

Since the begin address of a program is arbitrary on each run, and subsequently the address of each and every of its objects is arbitrary too.

因此,我想到访问对象或其成员对象的唯一方法是访问基本"指针的偏移量,这意味着需要进行算术运算才能访问程序结构中的任何内容.

Thus the only way I can think of to access an object or its member object is by accessing an offset from the "base" pointer, which means there is an arithmetic operation needed to access anything in a program structure.

我的问题是,这是否是在专业编译器中完成的方式,因为显然,这种方法增加了运行时的开销,而且由于缺乏这种方法,我本人也无法想到将这种过程从运行时卸载的任何方法.内存分配及其地址一致性的保证?

My question is whether this is the way it is done in professional compilers, because obviously this approach adds some overhead to the runtime, and I myself can't think of any way to offload this process from the runtime because of the lack of guarantees for consistency of memory allocation and its address?

推荐答案

几十年来,大多数计算机都提供寻址模式,通过这些寻址模式,您可以将地址指定为基数和偏移量的组合,并且实际计算是在硬件,无需花费额外的CPU时钟周期.

Most computers for many decades provide addressing modes that let you specify the address as a combination of a base and an offset, and the actual calculation is carried out in the hardware for no additional cost in CPU clock cycles.

最近(过去几十年)的计算机提供了用于虚拟化内存布局的硬件,这意味着,即使每次运行时项目的物理地址都不同,其在虚拟地址空间中的地址也保持不变.同样,使用基地址没有额外的成本,因为计算是隐式且无形地针对程序的执行二进制代码执行的.

More recent (past few decades) computers offer hardware for virtualizing memory layout, meaning that even through the physical address of an item is different on every run, its address in the virtual address space remains the same. Again, there is no additional cost for using the base address, because the calculations are performed implicitly and invisibly to the executing binary code of a program.

这篇关于对象和结构成员访问和地址偏移量计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆