auto-initialization of static variables?

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

auto-initialization of static variables?

code2win
This post was updated on .
I know how local vars are auto-initialized to 0, since we covered that in our VM_code-to-assembly translation.

How are static variables for a class auto-initialized to 0 before they can be referenced by Main.main (our entry-point)?
One option would be for my compiler to find Main.main and insert those initializations of all classes into the beginning of that, which seems messy.
Another option would be for static variables to be initialized to 0 upon first reference (if referenced before assignment): Cause from where I see it, static variables only exist on our symbol table until we either reference or assign to them. That means the VM has no idea how many static variables we have during runtime, until from within a function, it sees something like `pop static 2`(first assignment to 3rd static variable) or it first sees `push static 2`(reference before assignment which auto initialization is supposed to prevent).
Or is undefined behavior possible? Or does an error get thrown somewhere instead?
Should my VM-to-assembly translator have included assembly instructions to clear all static segments to 0?

Also, is there such thing as a virtual machine here that does anything besides translate our assembly-macros into assembly? Well it also shows on screen what is happening. I guess I could ask myself, "what is any machine?". Stretching my brain here...
I'm trying to put the pieces together and see how this comes full circle... get the big picture
Reply | Threaded
Open this post in threaded view
|

Re: auto-initialization of static variables?

WBahn
Administrator
Where does the language specification state that static variables are automatically initialized?
Reply | Threaded
Open this post in threaded view
|

Re: auto-initialization of static variables?

code2win
It seems to follow the mentality of safety with regards to local variables.
With regard to static variables, it doesn't seem to directly address if they are also treated this way, or if undefined behavior is possible, which is why I'm asking for clarification.
Reply | Threaded
Open this post in threaded view
|

Re: auto-initialization of static variables?

WBahn
Administrator
Any behavior that a language does not specify explicitly is not required. If you think about it, it can't be. It would be unreasonable to require that a language behave in a particular way beyond what is actually specified. Doing so would mean demanding that every implementer of a every tool for a particular language be mind readers and all come to the same conclusions about what behaviors it must have because they somehow follow other explicitly specified behavior.

I might be missing it, but I just looked at the Jack language specification (which is not really all presented in one place) and I don't see where it required any variables of any kind to be initialized. The VM language specifies that the local memory segment for a function be initialized to all zero (and I wish it didn't). I suspect that this is just to make it easy to talk about how the local memory segment is created in terms of VM machine constructs (i.e., a 0 is pushed on the stack for each local variable).