UCR CS 152 - Final Code Generation (19 pages)

Previewing pages 1, 2, 3, 4, 5, 6 of 19 page document View the full content.
View Full Document

Final Code Generation



Previewing pages 1, 2, 3, 4, 5, 6 of actual document.

View the full content.
View Full Document
View Full Document

Final Code Generation

186 views

Other


Pages:
19
School:
University of California, Riverside
Course:
Cs 152 - Compiler Design

Unformatted text preview:

Final Code Generation intermediate code src front end o optimizer pgm final code generator target pgm symbol table Input to Code Generator intermediate code program symbol table Output of Code Generator target program This can be any of assembly language program absolute machine language program relocatable machine language program 1 Translating 3 address code to final code This is almost a macro expansion process Examples 3 Address Code MIPS assembly code x A i load i into reg 1 la reg 2 A add reg 2 reg 2 reg 1 lw reg 2 reg 2 sw reg 2 x x y z load y into reg 1 load z into reg 2 add reg 3 reg 1 reg 2 sw reg 3 x if x y goto L load x into reg 1 load y into reg 2 bge reg 1 reg 2 L The resulting code may not be very efficient but can be improved via various code optimization techniques 2 Improving Code Quality Peephole Optimization We can traverse the sequence of intermediate code instructions looking for sequences that can be improved Examples of such improvements include redundant instruction elimination e g goto L L L flow of control optimizations e g goto L1 L1 goto L2 goto L2 L1 goto L2 algebraic simplifications e g instructions of the form x x 0 or x x 1 can be eliminated special case expressions can be simplified e g x 2 y can be simplified to x y y 3 Improving Code Quality Register Allocation A value in a register can be accessed much more efficiently than one in memory so we should try to keep frequently used values in registers Local Register Allocation considers only small segments of code basic blocks If an expression will be used soon after it is evaluated try to compute it into an unused register If there are no free registers we can either compute into memory if addressing modes allow or free up a register by storing its contents into memory Choose the register cheapest to store to memory and least likely to be accessed soon Global Register Allocation considers the entire body of a function or procedure Tries to keep frequently accessed values in



View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view Final Code Generation and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Final Code Generation and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?