10.5.对对象的处理——底层特性
\(10.5.\)Handling Objects: Low-level Aspects
1.Handling objects
Let's review how we handle local
and
argument
first:
The first five words in the RAM are used as pointer that hold the current value of the stack pointer, namely the base address of the
local
,argument
,this
,that
.The VM implementation also allocates a certain area on RAM to hold the global stack which keeps the working stack of the currentlt running VM functions and all the working stack and memory segments of the functions that wait to for the current function to terminate, namely the functions which are on the calling chain.
The VM implementation recalls the location of these segments of the stack by using the two pointers
LCL
andARG
. It records the base address of this segment in theLCL
.
Then how can we use the same architecture to represent object and array data?
- We use a different area altogether on the RAM called
heap
. - On the
heap
, we recall the data of all the objects and the arrays that the current program seeks to manipulate.
As opposed to the local
and argument
segments which have only one of each to worry about, here you may have
many objects on the heap. So before we access an object using
this
segment, we have to tell the system to which
object are we actually referring?
- The implementation of
this
location is first use the pointerthis
andthat
. - Before we use the two segments, we first have to use the
pointer
segment to tell the system where we want to alignthis
andthat
on the RAM.
We had to provide a mechanism to anchor this and that on the particular object and array the code has to operate on and we are using this virtual segment pointer for this purpose.
Now, suppose we want to access RAM words 8000, 8001, 8002... We can achieve this by the following VM code:
- Once we set
THIS
pointer to a particular address,THIS
can be used as if it's anchored exactly on the 8000th address in RAM. Given this desired mapping, we can use command related toTHIS
. push this 0
will take the value which is currently located inRAM 8000
onto the stack.pop this 0
will take something off the stack and put it intoRAM 8000
.