I am implementing a simple virtual machine, and currently I am using runtime arithmetic to calculate the addresses of individual program objects as offsets from base pointers.
Today I asked a couple of questions on this issue, but it seems that I did not go anywhere.
I learned some of you, starting with the first question - Calculating access to an object and structure and calculating address offsets - I learned that modern processors have virtual addressing capabilities, allowing you to calculate memory offsets without any additional arithmetic cycles.
And from the second question - Are address offsets allowed during compilation in C / C ++? - I found out that there is no guarantee for this when you perform manual offsets.
By now, it should be clear that I want to achieve the benefits of using virtual memory functions for hardware and disconnecting from runtime.
I use GCC because for the platform - I am developing on x86 on Windows, but since it is a virtual machine, I would like it to work effectively on all platforms supported by GCC.
Therefore, ANY information on this subject is welcome and will be greatly appreciated.
Thanks in advance!
EDIT: some overview of my program code generation - at the design stage, the program is built as a tree hierarchy, which is then recursively serialized into one continuous memory block, and also indexes the objects and calculates their offset from the beginning of the program memory block.
EDIT 2: Here is some VM pseudo code:
switch *instruction case 1: call_fn1(*(instruction+1)); instruction += (1+sizeof(parameter1)); break; case 2: call_fn2(*(instruction+1), *(instruction+1+sizeof(parameter1)); instruction += (1+sizeof(parameter1)+sizeof(parameter2); break; case 3: instruction += *(instruction+1); break;
Case 1 is a function that takes one parameter, which is detected immediately after the instruction, so it is passed as an offset of 1 byte from the instruction. The instruction pointer is incremented by 1 + the size of the first parameter to find the next command.
Case 2 is a function that takes two parameters, as before, the first parameter is passed as 1 byte offset, the second parameter is passed as 1 byte offset plus the size of the first parameter. Then the instruction pointer is increased by the size of the command plus the sizes of both parameters.
Case 3 - the goto statement, the instruction pointer is incremented by the offset immediately following the goto statement.
EDIT 3: As I understand it, the OS will provide each process with its own dedicated space for addressing virtual memory. If so, does this mean that the first address is always ... zero zero, so the offset from the first byte of the memory block is actually the address of this element? If the memory address is dedicated to each process, and I know the block offset of my program memory AND the offset of each program object from the first byte of the memory block, are the addresses of the objects resolved at compile time?
The problem is that these offsets are not available during compilation of C code, they become known during the phase of "compilation" and translation into byte code. Does this mean that there is no way to make the calculation of the memory address of the object "free"?
How is this done in Java, for example, when only machine compilation is only for machine code, does this mean that calculating the addresses of objects leads to performance degradation due to runtime arithmetic?