9.6 The Target Operating Environment

We have discussed at length the run-time environment in Chapter 7. In all subsequent discussions, we shall assume Linux Operating System (Kernel version 2.4.x or 2.6.x as mentioned) working on an x86 processor. We shall use GNU GCC for developing the software, GNU gdb for debugging support and use GNU binutils for various utilities to be used during program development.

We do not plan to use the standard C libraries, except for testing portions of compiler code. Wherever required Linux System Calls interface will be used for requesting system services like read and write to files, time of the day, etc.

9.6.1 Memory Management

Linux uses virtual memory system, details of which are given in Appendix C. We have also discussed the run-time memory management in Chapter 7.

9.6.2 CPU Register Usage

Although in x86 series machines almost any register can be used anywhere, there are some specializations. We take the following decisions after studying this aspect of the instruction set of x86:

  1. Use %eax for a very short range of code because it is used in many short versions of instructions.
  2. Use %ecx for shift and loop counts, addressing, saved addresses.
  3. Use %edx for multiply, divide, short term only.
  4. Use %ebx auxiliary medium term, also short-term array addressing.
  5. Use %ebp reserved for forming stack-frame base pointer and stack addressing base.
  6. Use %esp reserved for stack management, stack-based addressing.
  7. Use %esi, %edi if not handling array indexing, then use as auxiliary; if array addressing then short-term address generation, possibly with %ebx.
  8. Array address generation: ({%ebx, %esi, %edi}) + displ or (%ebx) + ({%ebx, %esi, %edi}) + displ or (%ebx) + ({%ebx, %esi, %edi})*{1,2,4,8} + displ.
  9. Stack-frame-based addressing (%ebp) + ({%ebx, %esi, %edi}) + displ or (%ebp) + ({%ebx, %esi, %edi})*{1,2,4,8} + displ.
  10. Up to 4-component addressing: (base) + (index)*scale + displ.
  11. Define assembly macros only for those sequences which have no chance of being reduced by optimization, e.g. input output SysCalls.
  12. %eds, %ess, %ecs, etc. are not used at all, neither should they be touched.

9.6.3 Activation Record (AR)

We have already discussed the need and use of activation records in Chapter 7. There we discussed AR in general, but here we shall be more specific and give a design of AR for the project on a compiler for a miniC language, which we shall discuss in Chapter 12.

To remind you, an AR is created on the Run-Time stack for each invocation of any function. An AR contains:

  • return address (IP or PC);
  • call arguments;
  • saved BP (%ebp), SP (%esp);
  • automatic variables;
  • temporary locations, as decided by the compiler.

Figure 9.7 shows a typical Activation Record, according to our design. In deciding upon this layout of the AR, we had the following goals in mind:

  1. It should use the x86 subroutine calling idioms.
  2. It should be based on “caller cleans the stack” convention, so that the possibility of variable number of arguments can be used if and when needed. It will also help make it compatible with the GCC C language calling conventions.
  3. It should be compatible with the GCC C language calling conventions. This means that a set of functions compiled using the miniC compiler will be callable in standard C programs (and hopefully, vice-versa). This compatibility will be very convenient while debugging the generated code, as the functions written in miniC can be tested using test programs written in C.
  4. It should be easy to generate the function definition and invocation codes.

Figure 9.7 shows the operation on the run-time stack, which results in creating an AR, during a function call. Figure 9.8 shows a mnemonic aid to remember the positions of various values in an AR.

 

Creation of an AR during a function call, (a) caller pushes the call arguments, (b) just after entry to the called function, (c) while the called function is executing

 

Fig. 9.7 Creation of an AR during a function call, (a) caller pushes the call arguments, (b) just after entry to the called function, (c) while the called function is executing

 

Positions of various parameters and values within an AR corresponding to a function call test(i,j,1), where the function test() itself has three local variables

 

Fig. 9.8 Positions of various parameters and values within an AR corresponding to a function call test(i,j,1), where the function test() itself has three local variables

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.139.224