Target code Generation Made by Siddharth Rakesh 11
Target code Generation Made by – Siddharth Rakesh 11 CS 30036 Date – 12/11/2013
Instruction set The format is : op dst, src • Load dst, src • Store dst, src • Arithmetic/Logical operation: - OP dst, src 1, src 2 - Ex – ADD R 0, R 1, R 2 - ADD R 0, R 1, M • Unconditional Branch - BRN L • Conditional Branch -Bx. L - BGTZ x L
Addressing modes: • Immediate – We allow an immediate constant addressing mode. The constant is prefixed by #. • Register – A memory location can be an integer indexed by a register. • Indirect – We allow 2 indirect addressing modes: *r means the memory location found in the location represented by the contents of register r and *100(r) means the memory location found in the location obtained by adding 100 to the contents of r. • Indexed – A location can also be an indexed address of the form a(r), where a is a variable and r is a register.
Challenges of Code Generation 1. Instruction selection Each 3 – address code statement maps to the target code. Ex – x = y + z Load R 1, y Add R 1, z Store x, R 1 2. Register Allocation a. We have v variables, we select n variables and store them in n registers. b. Register assignment – specific register variable x c. Optimality of target code – Metric – i. Number of memory accesses required ii. Length of instruction
• Rule of assignment of cost – 1. + 1 for any instruction. 2. Additional cost of 1 for memory access. • Ex – Add R 0, R 1, R 2 Cost = 1 - Add R 0, R 1, M Cost = 2 (because of memory access)
Conversion from intermediate code to m/c code • Split the Intermediate code into basic blocks. Block: A sequence of code instructions executed together. • Ex – a = b + c c=d+f block goto L 1 block breaks here and jumps L 1: ……
• Control will enter the first block with the first instruction. • There should not be any jump in the middle of the block. • Control can leave this block by executing the last instruction. We must identify the first instruction (leaders) of all basic blocks.
Steps to identify the leaders • 1 st instruction of the 3 address code is a leader. • All labels corresponding to the target statements are leaders. • Statements following GOTO are leaders. A Block may thus be defined as ‘all instructions following a leader until next leader comes’.
• Example : 1. loc = -1 2. i = 0 3. L 1 : if i < 10 goto L 2 4. goto L 5 5. t 1 = 2 * I 6. t 2 = A[t 1] 7. If t 2 = x goto L 3 8. goto L 4 9. L 3 : loc = i 10. L 4 : t 3 = i + 1 11. i = t 3 12. goto L 1 13. L 5 : leader leader leader
Flow graph • The flow graph depicts the flow of control through the body of the graph. • Each node will be a basic block. • Arcs from a node i j signify the transfer of control from block i to block j.
Line numbers corresponding to blocks Control flow graph based on previous example: 1, 2 B 1 3 B 2 4 B 3 5, 6, 7 B 4 8 B 5 9 B 6 10, 11, 12 B 7 13 B 8
- Slides: 11