VM Emulator error: OUT OF SEGMENT SPACE...

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

VM Emulator error: OUT OF SEGMENT SPACE...

froycard
I am starting to program my compiler... but I am trying to test some chunks of jack programs to check if my compiler is working fine... but I am stuck with this, and sorry if this is a silly question [newbie here]...
I am compiling the following program:

class Main {
    field int x, y;
    static int pointCount;
    function void main() {
      var int dx, dy;
      let dy = 5;      
      return;
   }
}

it is an one-line only program,,, and the VM emulator doesn't like my VM code that my compiler generated,,, as you can see in the image:
There is an error on line 3: pop local 1... I don't know what it's wrong




Reply | Threaded
Open this post in threaded view
|

Re: VM Emulator error: OUT OF SEGMENT SPACE...

cadet1620
Administrator
"Out of segment space" means that the index number for a segment access is beyond the size of the segment. In this case, you are tyring to pop into local 1, but the local segment is 0 words long.

The numeric argument to the VM 'function' command is the number of local variables the function uses. Since your Main.main has two local variables it needs to be
    function Main.main 2

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

Re: VM Emulator error: OUT OF SEGMENT SPACE...

froycard
This post was updated on .
I am very thankful for your help throughout my posts, you seem to know thoroughly the VM emulator.
I was confused (maybe, I still am) about the memory segment and function declaration. I thought when I declared a function I'd have to count the number of arguments I had to pass to the function. But I was confusing the subroutine passing arguments with function declaration.
Let's say I have this function (in Jack Language):

function void convert(int value) {
    var int mask, position;
    var boolean loop;
   
    let loop = true;
        ...
        ...
I have this function with one argument variable, and 3 more local variables. I thought according with the course, since I have only one variable, I'd declare this function as:
function Main.convert 1
push constant 1
neg
...
...


but according to your above explanation, I must have to count the local variables, thus my function declaration was erroneous, I should have declared this way:
function Main.convert 3

3 came from the fact there are 3 local variables, so the VM allocated the corresponding segment... but what about the other segments: argument, this, that, etc...

Beforehand, I do highly  appreciate your explanations Mark, thanks
Reply | Threaded
Open this post in threaded view
|

Re: VM Emulator error: OUT OF SEGMENT SPACE...

cadet1620
Administrator
The definition of the VM 'function' command is in video 2.3 starting about 1:30, if you want to review that.  Detailed implementation of 'function' is in 2.6 starting about 4:45.

From the compiler's point of view, at the beginning of a function
  o  The argument segment has been initialized with the argument values passed by the caller. (The 'call' VM command did this.)
  o  The local segment has been allocated and initialized to zeros. The size of the local segment is the number in the 'function' command.
  o  static points to the function's class static variables.
  o  this and that are undefined.

If this is a method, the compiler generated VM code needs to set the this pointer to argument 0 before it uses the this segment.

Likewise, the generated code needs to set the that pointer before it uses it to access an array.

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

Re: VM Emulator error: OUT OF SEGMENT SPACE...

froycard
Perfect, and again, many thanks for your patience and kind in your reply. I will review your explanation.