Function labels and call labels

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

Function labels and call labels

pawdwanlearner
Hello, folks i'm almost finished with the milestone completing chapter 8. However, I'm having a little trouble understanding the labeling scheme the book describes. It says that each label b  VM  command must be given an corresponding unique global label

f$b

 where f is the function name and b is the label symbol within the vm functions code. However, when i look at the VM functions given as test programs i don't see this scheme being followed. where is the label b. and is the name of vm file considered a function or is it considered the overall class.
Reply | Threaded
Open this post in threaded view
|

Re: Function labels and call labels

cadet1620
Administrator
The label in the generated ASM code is what needs the fn-name$lbl-name naming.

This is so that a "goto whatever" in function Class1.fn will jump to "label whatever" in itself and not to a "label whatever" in Class2.bar:
        VM                      ASM

function Class1.foo 1   (Class1.foo)
...                     ...
label whatever          (Class1.foo$whatever)
...                     ...
goto whatever           @Class1.foo$whatever
...                     0;jmp


function Class2.bar 1   (Class2.bar)
...                     ...
label whatever          (Class2.bar$whatever)
...                     ...
goto whatever           @Class2.bar$whatever
...                     0;jmp
Note that the Jack compiler always generates VM code with function names that include the class name. Since the VM file name must match the class name, you don't need to worry about function names being confused across VM files.

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

Re: Function labels and call labels

pawdwanlearner
Aaaaaaahhhhhhhh so the vm filename is actually the classname.function and putting the name of the label behind it allows us to see whivh class the label belomgs too like this.  (Classname.function$label)  and when I have a goto its @classname.function$label. Correct
Reply | Threaded
Open this post in threaded view
|

Re: Function labels and call labels

cadet1620
Administrator
Understanf that your VM translator does not need to add the VM filename to goto labels. The name on the function statement already includes the class name.

Here's the big picture.

MyClass.jack contains the code:
class MyClass {
    function void foo (int x) {
        while (x > 0) {
            let x = x-1;
        }
        return;
    }
}
This compiles to MyClass.vm:
function MyClass.foo 0
label WHILE_EXP0
...
if-goto WHILE_END0
...
goto WHILE_EXP0
label WHILE_END0
...
VM translator generates:
// function MyClass.foo 0
(MyClass.foo)
// label WHILE_EXP0
(MyClass.foo$WHILE_EXP0)
...
// if-goto WHILE_END0
...
    @MyClass.foo$WHILE_END0
    D;JNE
...
// goto WHILE_EXP0
    @MyClass.foo$WHILE_EXP0
    0;JMP
// label WHILE_END0
(MyClass.foo$WHILE_END0)
...
--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Function labels and call labels

pawdwanlearner
Understood thank you very much.