Method Calls with no varName

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

Method Calls with no varName

throwinshapes
In 2nd edition of the book, the section on compiling method calls with no varName mentioned states "push the symbol table mapping of this.

This makes sense to me, since calling methodName() is implicitly calling this.methodName().

However, when we are adding rows to the symbol table, the book states to only add 'this' to the table if the subroutine is a method. But what happens when a method call without a varName occurs in a constructor? This exact case happens in project 11 Square.jack on line 19 - "do Draw(). That line is in a constructor, where we won't have 'this' in the symbol table.

Now, I know I could just hard code the push command by grabbing the className elsewhere, but the book specifically says to map using the symbol table. I just want to make sure I'm not missing something.
Reply | Threaded
Open this post in threaded view
|

Re: Method Calls with no varName

dolomiti7
This post was updated on .
I think the book is indeed a bit ambiguous on that matter. Generally a "this" reference in the source code is always referring to "pointer 0", whether hidden/implicit or not. However, since methods get their object reference passed as "argument 0", it is also possible to use "argument 0" for this. This is the approach that the authors took and is reflected in Figure 11.2 (chapter 11.1.1). They add an entry to the symbol table allocating "this" to "argument 0". As a side effect this ensures that the visible arguments of a method start from argument 1. As you rightly point out, this doesn't work for constructors.

The "this" reference is available in constructors immediately after the appropriate memory has been allocated via Memory alloc, but it is only addressable via pointer 0.

If you don't want to hardcode "this" references, one approach is to allow entries in the symbol table of the kind "pointer" and generally add "this" as "pointer 0" instead of "arg 0" to the symbol table. You just have to make sure, that method arguments will still be added starting from argument 1. One solution is to add a dummy arg entry for methods that occupies the arg 0 entry (i.e. "@this") - which will never be accessed.

Edit: or you stick to adding "this" as "argument 0" for methods, but add it as "pointer 0" to the symbol table for constructors instead (also requiring to allow pointer kind entries).
Reply | Threaded
Open this post in threaded view
|

Re: Method Calls with no varName

throwinshapes
Fair enough, I appreciate the detailed response. I think I'll keep the hard coded pointer 0.