Why do we need to save the THAT pointer?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Why do we need to save the THAT pointer?

tungsten
The THAT pointer is used only for array access... Unlike THIS, it is relevant only for the brief instance an array is being accessed. So if the context switches upon a subroutine call, the caller doesn't care whether THAT is changed or not, since the caller does not expect it to be a particular value...

For example...

   // let a[5] = someFunc()

   call someClass.someFunc 0   // don't really care if the function sets THAT
   pop temp 0

   push constant 5
   push location_of_a
   add
   pop pointer 1   // THAT is set here

   push temp 0
   pop that 0       // THAT is accessed here


Or am I missing something?
Reply | Threaded
Open this post in threaded view
|

Re: Why do we need to save the THAT pointer?

cadet1620
Administrator
You are correct that the supplied JackCompiler and student compilers following the design in the book do not nest usage of THAT.  These are not, however, the only possible sources of VM code.

An optimizing JackCompiler might be able to improve the generated code for a loop like
let i = 0;
while (i < 100) {
    let foo[i] = bar(i);
    let i = i+1;
}
by initializing THAT outside the loop and simply incrementing THAT in the loop.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Why do we need to save the THAT pointer?

tungsten
Ah, I see. Thank you!