Having question about constructor

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

Having question about constructor

thomaschan
Since every constructor should return 'this', does it has a default 'argument 0' which named 'this', just as the method?
 
Plus, when compiling this constructor,

constructor SquareGame new() {
     return this;
}


Can the VM code be like this?
function SquareGame.new 0
push argument 0
pop pointer 0
push pointer 0
return


But when I use the JackCompiler to output this file, it appears to be like this:
function SquareGame.new 0
push constant 0
call Memory.alloc 1
pop pointer 0
push pointer 0
return


I don't understand why it should use Memory.alloc.
Reply | Threaded
Open this post in threaded view
|

Re: Having question about constructor

cadet1620
Administrator
thomaschan wrote
Since every constructor should return 'this', does it has a default 'argument 0' which named 'this', just as the method?
Constructors do not have this as a default argument. The object doesn't exist when the constructor is called so there in no pointer to pass yet. Argument 0 is the first real argument, like a function.
When I use the JackCompiler to output this file, it appears to be like this:
function SquareGame.new 0
push constant 0
call Memory.alloc 1
pop pointer 0
push pointer 0
return
Only the compiler knows how much memory is needed for the object's variables, so it must generate code that calls Memory.alloc() to allocate the memory block required to hold the object. In your test case, the SquareGame object had no "fields" declared, so 0 was passed to Memory.alloc().

The real SquareGame has 2 fields so the constructor will start with "push constant 2".

Effectively, the constructor is doing
    this = Memory.alloc( number of fields );
    // Initialize fields ...
    return this;

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

Re: Having question about constructor

twitu
My question is related to this. Take this snippet from Square.jack

/** Constructs a new square with a given location and size. */
   constructor Square new(int Ax, int Ay, int Asize) {
      let x = Ax;
      let y = Ay;
      let size = Asize;
      do draw();
      return this;
   }

and it's vm code as per provided JackCompiler

function Square.new 0
push constant 3
call Memory.alloc 1
pop pointer 0
push argument 0
pop this 0
push argument 1
pop this 1
push argument 2
pop this 2
push pointer 0
call Square.draw 1
pop temp 0
push pointer 0
return

How does the compiler realize that 3 words should be allocated,

1. I can ignore one of the given arguments
2. I can pass 2 arguments and define one of the instance variables a constant, like let x = 3;

Is the compiler counting the number of let statements ?
Reply | Threaded
Open this post in threaded view
|

Re: Having question about constructor

twitu
My bad, I got it.
So I'll have to count the number of field variables defined and I can use that as the parameter to "Memory.alloc"
Reply | Threaded
Open this post in threaded view
|

Re: Having question about constructor

cadet1620
Administrator
If you followed the suggested design, you don't need to explicitly count the fields. It is already available from SymbolTable:
    num_fields = symbol_table.varCount(FIELD)