compiler flow control labels

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

compiler flow control labels

pawdwanlearner
Since the assembler creates unique labels for each variable and I was wondering how that relates to flow control in the compiler. I read in another post that flow control labels are bound to function level so does each flow control structure need its own set of labels per class or per function and can you give a hint at to how to implement nested control structure labels. No solution just a cue. Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: compiler flow control labels

cadet1620
Administrator
The important point is that the VM translator ensures that label/if/if-goto labels are decorated so that they will be unique across functions.

If you want to try to match the supplied Jack compiler's output you need to generate the labels using serial numbers that restart with every function. If you want to match my compiler's output, you need to be lazy like I was and never reinitialize the serial number 8-)

Nested control structures imply nested calls to compileIf() and compileWhile() so those routines need to generate all the labels that they might need to use before they call anything that might recursively call themselves.

General note on recursive object-oriented programming:
Some things need to be stored in the object and others on the stack—I've written some wonderfully hard to find bugs over the years by mixing them up!

--Mark

Reply | Threaded
Open this post in threaded view
|

Re: compiler flow control labels

pawdwanlearner
Got it!! just solved flow of control while and if. I keep forgetting that recursion is not like looping when it comes to data retention. Each one of the those recursive functions keeps a separate copy of its own locals on the stack that it can come back to when it returns. So simple thanks. I was doing all kinds of stuff trying to retain the pattern but it was already handled.