4 Input to the Target Code Generator

Download Report

Transcript 4 Input to the Target Code Generator

CS 404
Introduction to Compiler Design
Lecture 11
Ahmed Ezzat
1
CS 404
Ahmed Ezzat
Target Code Generation



Readings: Sec. 9.1, browse Sec. 9.2, 9.3, 9.6
Final phase of a compiler
Requirement of code generator
–
–
–
–
2
Correct output
Easy to implement, test and maintain
Use resources of target machine
Efficient
CS 404
Ahmed Ezzat
Different Target Machines

Accumulator machine
–

Stack machine
–
–

3
LOAD/STORE
Java virtual machine
Calculators
General register machines
CS 404
Ahmed Ezzat
Input to the Target Code Generator

Intermediate code: three address code
–
–
–

4
Assume no lexical and syntax error
Assume objects of intermediate code can be
represented by target machine quantities (e.g.,
bits, integers, pointers, reals.)
Assume type checking is done
Symbol table: containing information about
source objects, e.g., type, size
CS 404
Ahmed Ezzat
Output of the Code Generator

Absolute machine language
–
–

Re-locatable machine code
–

May compile sub-programs separately
Assembly code
–
–
–
5
For simple compilers, simple machines
Can directly execute
Close to TAC
Readable
Need assembly
CS 404
Ahmed Ezzat
Code Generation Tasks (1)

Memory Management
–

Mapping names in the source program to run-time
addresses (symbolic addresses for assembly
code)
Instruction Selection
–
–
Direct mapping
Plus optimizations

6
Different instructions cost differently
CS 404
Ahmed Ezzat
Code Generation Tasks (2)

Register Allocation
–
–
–
7
Try use registers instead of memory locations
Obey register usage conventions (e.g., special
usage registers)
Many strategies (Sec. 9.7), e.g., consider usage
count, loops
CS 404
Ahmed Ezzat
Code Generation Tasks (3)

Choice of evaluation order
–
–

Approaches to target code generation
–
–
–
8
A difficult problem
Save register usage
Straightforward match plus optimizations
Tree directed code selection (not required)
Other approaches
CS 404
Ahmed Ezzat
Sample Target Code Generation



9
Register descriptor: what is currently in each
register
Address descriptor: locations where the
correct value of the name can be found at
run time
Page 539 example
CS 404
Ahmed Ezzat
Linking




10
From re-locatable code to executable
One or more .o files get linked into a single
.exe file
All external symbols get resolved
Code and data of each .o get relocated into
one big address space of .exe file, starting at
0
CS 404
Ahmed Ezzat
Linking


11
Static linking: all code gets copied into one
big .exe file (including all library files)
Dynamic linking: some external routines
(mostly library routines) are not copied to
.exe. Only load when get called.
CS 404
Ahmed Ezzat
Loading




12
.exe into memory
Code and data get relocated so that lowest
address in code segment is load into
memory
(Operating System: handles memory
management)
(Computer Architecture: actual execution of
instructions)
CS 404
Ahmed Ezzat
Debuggers



Start and stop program under user’s control
Display state of memory and code at the stop
point
User can set break points
–
–

13
By location
By condition
User can set data watch points
CS 404
Ahmed Ezzat
How does a Debugger Work?

It needs to know
–
–
–


14
Names of functions, formal parameters, global
variables, local variables, labels
Locations, functions
All the stuff that a compiler knows!
A compiler produces a debug section in the
target code (may opt out)
Debugger runs the program but keeps states
of its own
CS 404
Ahmed Ezzat
Profilers

A profiler gather information about execution
of a program
–
–
–
Implemented similarly as a debugger
Add instructions in the target program to call
counters
May use statistical sampling

15
Stop executions periodically, see what function is called
more often
CS 404
Ahmed Ezzat