TopUsing the LR(0) machineGenerating Code for Procedures

Generating Code for Procedures

  1. There are several issues to consider to prepare to generate code for methods:

    1. What runtime structures are involved in the invocation of a method.

    2. How to handle parameters.

    3. How to handle return values.

    4. What the basic method header and trailer code looks like.

      • Using the chain of static link pointers
      • saving return information

      • Saving and restoring registers.

    5. When to generate code for each method while traversing the syntax tree.

  2. Recall the basic 34000 call and return sequences:

  3. This depends on (and results in) the following frame structure:

  4. We will follow the usual 68000 convention and assume that A7 is the stack pointer and than A6 is used to point to (the middle of) the frame for the active method.

  5. In addition, we will assume that A5 holds a pointer to the object associated with the currently executing method.

  6. A very fundamental difference between the standard calling sequence and the code you must generate is that you cannot produce JSR instructions that statically specify the name of the target method's first instruction.

    Note: If you are just dying to do something clever to improve the quality of the code you produce you should observe that it is safe to skip the method table if the class containing the method being invoked has no subclasses or if none of the subclasses override the method.

  7. As a result, in place of a single JSR instruction, you will have to generate: or, you can build a table of branches to the methods instead of just a table of their address and then generate: Since the 34000 doens't do any sophisticated pipelining or branch prediction, the second method will be best.

  8. When to generate code (or the beauty of using the assembler): The amazing thing about using the assembler is how flexibly you can change the order of things. If you prefer to have all the method tables before all the code, you can easily arrange that too!

  9. Another difference between method calls in Woolite and the standard 68000/34000 call sequence is the need to deal with the object pointer (A5).
  10. About that chain of static links....

  11. The handling of method parameters in your compiler will reflect simple, but outdated technology. The most glaring simplification/inefficiency will be the use of memory rather than registers to pass parameters and other information between caller and callee.

  12. Recognizing the importance of effficient calls, modern compilers (and hardware calling conventions) tend to use registers for parameters when possible. To support methods with many parameters, however, such schemes have to retain the possiblity of storing some parameters in memory. We will keep things simple by placing all parameters in memory on the stack.

  13. Luckily, in Woolite, all parameter values take just one word of memory. Therefore, at the call site, all you have to do is generate code to determine the parameter's value and then push it onto the stack.

  14. Handling return values can be approached in one of two ways: Saving the result on the stack will prove simpler. This means:

Computer Science 434
Department of Computer Science
Williams College

TopUsing the LR(0) machineGenerating Code for Procedures