A Simple Code Generator
NEXT-USE INFORMATION
Input: Basic block B of three-address statements
Output: At each statement i: x = y op z, we attach to i the liveliness and next-uses of x, y and z.
Method: We start at the last statement of B and scan backwards.
1. Attach to statement i the information currently found in the symbol table regarding the next-use and liveliness of x, y and z.
2. In the symbol table, set x to “not live” and “no next use”.
A SIMPLE CODE GENERATOR
effectively uses registers to store operands of the statements.
ADD Rj, Ri Cost = 1
(or)
ADD c, Ri Cost = 2
(or)
MOV c, Rj Cost = 3
ADD Rj, Ri
Register and Address Descriptors:
A code-generation algorithm:
The algorithm takes as input a sequence of three-address statements constituting a basic block. For each three-address statement of the form x : = y op z, perform the following actions:
1. Invoke a function getreg to determine the location L where the result of the computation y op z should be stored.
2. Consult the address descriptor for y to determine y’, the current location of y. Prefer the register for y’ if the value of y is currently both in memory and a register. If the value of y is not already in L, generate the instruction MOV y’ , L to place a copy of y in L.
3. Generate the instruction OP z’ , L where z’ is a current location of z. Prefer a register to a memory location if z is in both. Update the address descriptor of x to indicate that x is in location L. If x is in L, update its descriptor and remove x from all other descriptors.
4. If the current values of y or z have no next uses, are not live on exit from the block, and are in registers, alter the register descriptor to indicate that, after execution of x : = y op z , those registers will no longer contain y or z
Generating Code for Assignment Statements:
Code sequence for the example is:
Generating Code for Indexed Assignments
The table shows the code sequences generated for the indexed assignmen a:= b[ i ] and a[ i ]:= b
Generating Code for Pointer Assignments
The table shows the code sequences generated for the pointer assignments a : = *p and *p : = a
if x < 0 goto z ADD z, R0
MOV R0,x
CJ< z
1. What is a code generator? |
2. What is code generation in computer science and IT engineering? |
3. How does a code generator work? |
4. What are the benefits of using a code generator? |
5. What are some popular code generation tools? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|