Code Generation clarification

Consider the example in page 8:

 int global_var;
 int main(){
    return global_var;
}

As mentioned the generated code ends with

   main:
   // ... previous code
   jl $ra

However, since this the first program being executed so main wasn’t being called from anywhere hence the register $ra would probably be 0 and jumping the address $zero will raise an error. In MIPS I guess we used to end the program manually by loading some value in $v0 then calling syscall, should we do the same in code generation? since I cannot tell how such code can be tested.

No, you should not.
You should compile the main function the same way you compile any other function.

A small piece of code, which calls main and then exits, is added automatically by the part that is already implemented for you.

To be more precise, this is added as part of the “preload” code that is added by MarsUtil (in src/prog2/tests/MarsUtil.java).

1 Like

The first Unit test in code generation succeeds, while running it another time gives time out. I thought first that the problem might be where the main is ended. However, this is not the case, since you already took care for that in the code. I have checked the generated code, it is as expected but nevertheless after the mars.run() sometime the evaluation succeeds and sometime yields time out for the same piece of generated code. Any suggestion?

Edit: I have noticed that the problem is related for the time of execution of the test, for instance running the test the breakpoint will succeed and discard the timeout problem while running it with no breakpoint will yield timeout exception when the runtime exceeds one second.

if the same test sometimes succeeds and sometimes times out, you might want to change globalTimeout in CompilerTests.java.

Note that this is only a local change and will not affect execution on the server, however, the timeout on the server should be high enough.