This and that have what purpose?

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

This and that have what purpose?

virote328
In High level languages like c++,
"THIS" is a pointer to the implicit parameter.

But in this book.  The vm translator deals with "THIS" and "THAT".
I can't seems to find what their purpose is.  The books definition is
that they are pointers to the heap.  Ok, fine.  in the VM to ASM project,
it is a simple translation.

But I'm trying to write the compiler and I'm just wondering what part of the
JACK code corresponds to calling "THIS" or "THAT".

If it is mentioned in the book, please help me out by pointing me to the right page.
Or you can just tell me.

Thank you in advanced
Reply | Threaded
Open this post in threaded view
|

Re: This and that have what purpose?

cadet1620
Administrator
Just like C++, this is a pointer to the object whose method was called.  that is a temporary pointer that is used to access other objects and arrays.

See figure 11.6 for a this example.  Note that all methods start with
    push argument 0
    pop pointer 0
which sets the this pointer.

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

Re: This and that have what purpose?

ybakos
In reply to this post by virote328
Once you finish the compiler, you'll have a strong understanding of how this and THIS work. Any time you see something like:

foo.bar();

The value of THIS changes. It is this feature (no pun intended) that gives an object method it's object scope, or, in other words, associates the method call bar() with the object foo's state, upon which bar() is called.

THAT is a little more elusive. Let's just say that having a second, adjustable pointer to a virtual memory segment is a huge convenience.

Stick w/ the compiler -- write back if you're still confused about this and that.
Reply | Threaded
Open this post in threaded view
|

Re: This and that have what purpose?

virote328
Thanks guys, you are really fast!

Thanks for clarifying "THIS"

Would you know of an example of how "THAT" would be used?
Reply | Threaded
Open this post in threaded view
|

Re: This and that have what purpose?

cadet1620
Administrator
I didn't see a that example in my quick scan through the compiler section. Here's a quick and dirty array example.

class Test {
    function void foo(Array a, int b, int c) {
        let a[b] = c;
        }
    }

My compiler generates:

function Test.foo 0
push argument 1
push argument 2
pop temp 0
push argument 0
add
pop pointer 1
push temp 0
pop that 0

My comment about accessing other objects doesn't apply to Jack since it doesn't allow a method to directly access fields in another object of the same type.

--Mark