Local variable initialization: Jack vs VM spec discrepancy

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Local variable initialization: Jack vs VM spec discrepancy

gcpd
The Jack spec defines local variables as uninitialized until initialized but the VM spec requires the locals segment to be initialized and there is a project test which enforces this so in effect I don't think pg248 reflects the actual intent or compiler requirements as they currently stand. Looking at the provided VM libs there are several instances of local vars which are relying on this implementation detail (i.e. relies on an initialized value without explicit initialization) as well.

pg222: 8.5 [VM] Implementation

    Before I start executing, my argument segment has
    been initialized with the argument values passed by the
    caller, and my local variables segment has been
    allocated and initialized to zeros. My static segment
    has been set to the static segment of the VM file to
    which I belong, and my working stack is empty. The
    memory segments this, that, pointer, and temp are
    undefined upon entry [†].

pg248: 9.2.4 [Jack] Variables

    Variable initialization: Static variables are not initialized, and it is up to
    the programmer to write code that initializes them before using them. Field
    variables are not initialized; it is expected that they will be initialized by the
    class constructor, or constructors. Local variables are not initialized, and it
    is up to the programmer to initialize them. Parameter variables are
    initialized to the values of the arguments passed by the caller.

From the perspective of optimizing the VM translator it would have been nice to be able to strictly treat local variables as described in the Jack spec but enforcing initialization in the compiler is the safer choice adopted by many modern languages so I don't really have any qualms about this other than the apparent discrepancy.