Professional Documents
Culture Documents
MIT6 - 035S10 Compilers - Unoptimized Code Generation
MIT6 - 035S10 Compilers - Unoptimized Code Generation
U ti i d Code
C d
Generation
Outline
• Introduction
• Machine Language
• Overview of a modern processor
• Memory Layout
• Procedure Abstraction
• Procedure Linkage
• Guidelines in Creating a Code Generator
Anatomy of a compiler
Program (character stream)
Lexical Analyzer
y ((Scanner))
Token Stream
Syntax
y Analyzer
y (Parser)
( )
Parse Tree
Semantic Analyzer
y
Intermediate Representation
Intermediate Code Optimizer
p
Optimized Intermediate Representation
Code Generator
Assembly code
Anatomy of a compiler
Program (character stream)
Lexical Analyzer
y ((Scanner))
Token Stream
Syntax
y Analyzer
y (Parser)
( )
Parse Tree
Semantic Analyzer
y
High-level IR
Low level IR
Low-level
Intermediate Representation
Code Generator
Assembly code
Components of a High Level
Language
CODE DATA
Temporaries
Statements
Parameter Passing
Data Access Read-only Data
Machine Code Generator
Should...
Should
• Translate all the instructions in the
intermediate representation to assembly
language
• Allocate space for the variables, arrays etc.
• Adhere to calling conventions
y symbolic
• Create the necessary y information
7
Outline
• Introduction
• Machine Language
• Overview of a modern processor
• Memory Layout
• Procedure Abstraction
• Procedure Linkage
• Guidelines in Creating a Code Generator
Machines understand...
LOCATION DATA
0046 8B45FC
0049 4863F0
004c 8B45FC
004f 4863D0
0052 8B45FC
0055 4898
0057 8B048500
000000
005e 8B149500
000000
0065 01C2
0067 8B45FC
006a 4898
006c 89D7
006e 033C8500
000000
0075 8B45FC
0078 4863C8
007b 8B45F8
007e 4898
0080 8B148500
000000
Machines understand...
LOCATION DATA ASSEMBLY INSTRUCTION
movl
Program (character stream)
Lexical Analyzer (Scanner)
Token Stream
Syntax Analyzer (Parser)
Parse Tree
Intermediate Code Generator
High-level IR
Low-level IR
Intermediate Representation
Code Generator
Assembly code
Program (character stream)
Lexical Analyzer (Scanner)
Token Stream
Syntax Analyzer (Parser)
Parse Tree
Intermediate Code Generator
High-level IR
Low-level IR
Intermediate Representation
Code Generator
Assembly code
Assembler & linker
Binary executable
Processor
Assembly
y language
g g
• Advantages
– Simplifies code generation due to use of symbolic
instructions and symbolic names
– Logical abstraction layer
– Multiple Architectures can describe by a single
assembly language
⇒ can modify the implementation
• macro assemblyy instructions
• Disadvantages
– Additional process of assembling and linking
– Assembler adds overhead
Assembly
y language
g g
• Relocatable machine language (object modules)
– all locations(addresses) represented by symbols
– Mapped to memory addresses at link and load time
– Flexibility of separate compilation
• Absolute machine language
– add
ddresses are hard d-codded
d
– simple and straightforward implementation
code
– Used in interrupt handlers and device drivers
Assembly example
11
.section
.LC0:
LC0
0000 6572726F7200 .string "error"
.text
.globl fact .rodata
fact:
0000 55 pushq
p q %rbp
p
0001 4889E5 %rsp, %rbp
0004 4883EC10 $16, %rsp
0008 897DFC %edi, -4(%rbp)
000b 837DFC00
000f 7911
0011 BF00000000 movl $0,$.LC0,
-4(%rbp)
%edi
0016 B800000000 .L2
001b E800000000
0020 EB22 jns $0, %eax
subq .L2:
movq printf
0022 837DFC00
0026 7509 cmpl
movl jne .L3 L4
0028 C745F801000000 jmp $0, -4(%rbp)
002f EB13 movl .
call
.L4: $1, -8(%rbp)
0031 8B7DFC .L3 -4(%rbp), %edi
0034 FFCF movl
0036 E800000000 jmp ll f
fact
t
003b 0FAF45FC cmpl %edi
-4(%rbp), %eax
003f 8945F8 %eax, -8(%rbp)
0042 EB00 decl
.L3:
0044 8B45F8 .L1 -8(%rbp),
( p), %eax
0047 C9 ca jmp
0048 C3 movl
imull
movl
Composition
p of an Object
j File
• We use the ELF file format .file
.section
"test2.c"
.LC0:
.string "error %d"
• The object file has: .section
.rodata
.text
– Multiple Segments .globl fact
fact:
– Symbol Information pushq %rbp
– Relocation Information movq
subq
%rsp, %rbp
$16, %rsp
movl -8(%rbp), %eax
leave
• Segments ret
.
– Global Offset Table .comm bar,4,4
– Procedure Linkage Table .comm
.comm
a,1,1
b,1,1
– Text (code)
.section
– Data .long .LECIE1-.LSCIE1
– Read Only Data .long
.byte
0x0
.eh_frame,"a",@progbits
0x1
.string ""
.uleb128 0x1
11
Outline
• Introduction
• Machine Language
• Overview of a modern processor
• Memory Layout
• Procedure Abstraction
• Procedure Linkage
• Guidelines in Creating a Code Generator
Overview of a modern
processor
• ALU Memory
• Control
• Memory Registers ALU
• Registers Control
Arithmetic and Logic
g Unit
• Performs most of the data
operations Memory
• Has the form:
OP <oprnd1>, <oprnd2>
– <oprnd2> = <oprnd1> OP <oprnd2>
Or Registers ALU
OP <oprnd1>
• Operands are: Control
– Immediate Value $25
– Register %rax
– Memory 4(%rbp)
• Operations are:
– Arithmetic operations (add, sub, imul)
– Logical operations (and
(and, sal)
– Unitary operations (inc, dec)
Arithmetic and Logic
g Unit
• Many arithmetic operations can
cause an exception Memory
– overflow and underflow
• Can operate on different data types
– addb 8 bits Registers ALU
– addw 16 bits
– addl 32 bits Control
• Executing instructions
– All instructions are in memory
Registers ALU
– Fetch the instruction pointed by the
PC and execute it Control
– For general instructions, increment
the PC to ppoint to the next location in
memory
Control
• Unconditional Branches
Memory
–FFetch
t h th
the nextt instruction
i t ti ffrom a
different location
– Unconditional jump to an address
Registers ALU
jmp .L32
– Unconditional jjump p to an address
Control
in a register
jmp %rax
–T h dl procedure
To handle d ll
calls
call fact call %r11
Control
• All arithmetic operations update the
condition codes (rFLAGS)
( ) Memory
– composed of words
y addressable
– byte
Registers ALU
• Need to store
– Program Control
– Local variables
– Global variables and data
– Stack
– Heap
H
Memory
y
Memory
Dynamic Heapp
0x800 0000 0000
Registers ALU
Stack
Control
Globals/
Data Read-only data
Text Program
0x40 0000
Unmapped
pp
0x0
Registers
g
• Instructions allow only limited memory
Memory
ti
operations
– add -4(%rbp), -8(%rbp)
add %r10, 8(%rbp)
%r10 -8(%rbp)
Registers ALU
• Important
p for p
performance
Control
– limited in number
• S
Special
i l registers
i t
– %rbp base pointer
– %rsp stack pointer
Moving
g Data
Memory
– mov source d
dest
• Moves data
– from one register to another
– from registers to memory Registers ALU
– from memory to registers
Control
– push source
• Pushes data into the stack
– pop dest
• Pops data from the stack to dest
Other interactions
• Other operations
p Memory
– Input/Output
g / secure operations
– Privilege p
Registers ALU
– Handling special hardware
• TLBs,, Caches etc.
Control
Outline
• Introduction
• Machine Language
• Overview of a modern processor
• Memory Layout
• Procedure Abstraction
• Procedure Linkage
• Guidelines in Creating a Code Generator
Components of a High Level
Language
CODE DATA
Control Flow Global Static Variables
Global Dynamic Data
Procedures Local Variables
Temporaries
Statements
Parameter Passing
Data Access Read-only Data
Memory
y Layout
y
• Heap
p management
g Dynamic Heapp
– free lists 0x800 0000 0000
Stack
• starting location in
g
the text segment Globals/
Data Read-only
R d l
data
Text Program
CODE DATA
Control Flow Global Static Variables 0x40 0000
Global Dynamic Data
Procedures Local Variables Unmapped
pp
Temporaries 0x0
Statements
Parameter Passing
Data Access Read-only Data
Allocating
g Read-Only
y Data
• All Read-Only data in the text .section .text
g
segment .globl main
main:
• Integers enter $0, $0
– use load immediate movqq $5, x(%rip)
p
• Strings
St i push x(%rip)
– use the .string macro push $.msg
call printf_035
add $16, %rsp
leave
ret
CODE DATA
Global Static Variables
.msg:
Control Flow
Global Dynamic Data .string "Five: %d\n"
Procedures Local Variables
Temporaries
Statements
Parameter Passing
Data Access Read-only Data
Global Variables
.section .text
• Allocation: Use the assembler's .globl main
.comm directive main:
enter $0, $0
$0
• Use PC relative addressing movq $5, x(%rip)
– %rip is the current instruction push x(%rip)
address call printf_035
– X(%rip) will add the offset from add $16, %rsp
the current instruction location to leave
the space for x in the data ret
segment to %rip
– Creates easily recolatable .comm x, 8
binaries
.comm name, size, alignment
The .comm di
Th directive
i allocates
ll storage iin
CODE DATA the data section. The storage is referenced
Control Flow Global Static Variables by the identifier name. Size is measured in
Global Dynamic Data bytes and must be a positive integer.
Procedures Local Variables Name cannot be predefined. Alignment is
Statements Temporaries i l If alignment
optional. li iis specified,
ifi d the
h
Parameter Passing address of name is aligned to a multiple of
Data Access Read-only Data alignment
22
Outline
• Introduction
• Machine Language
• Overview of a modern processor
• Memory Layout
• Procedure Abstraction
• Procedure Linkage
• Guidelines in Creating a Code Generator
Procedure Abstraction
• Requires system-wide compact
– Broad agreement
g on memoryy layout,
y p
protection, resource
allocation calling sequences, & error handling
– Must involve architecture (ISA), OS, & compiler
• Provides shared access to system
system-wide
wide facilities
– Storage management, flow of control, interrupts
– Interface to input/output
p p devices,, p
protection facilities,, timers,,
synchronization flags, counters, …
• Establishes the need for a private context
–CCreate private
i storage ffor each
h procedure
d iinvocation
i
– Encapsulate information about control flow & data
abstractions
The procedure abstraction is a social contract (Rousseau)
Procedure Abstraction
• In practical terms it leads to...
– multiple procedures
– library calls
– compiled by many compilers, written in different
languages, hand-written assembly
• For
F the
th project,
j t we need
d tto worry about
b t
– Parameter passing
– Registers
R i t
– Stack
– Calling convention
Parameter p
passing
g disciplines
p
• Many
y different methods
– call by reference
– call byy value
– call by value-result (copy-in/copy-out)
25
Parameter Passing
g Disciplines
p
Program {
int A;
foo(int B) {
B = B + 1
B = B + A
}
Main() {
A = 10;
foo(A);
}
}
Parameter Passing
g Disciplines
p
Program {
int A;
foo(int B) {
B = B + 1
B = B + A
}
Main() {
A = 10;
foo(A);
}
}
• Call by value A is 10
• Call by reference A is 22
• Call by value-result
value result A is 21
Parameter p
passing
g disciplines
p
• Many different methods
– call by reference
– call by value
– call by value-result
– value-result
• How do you pass the parameters?
– via. the stack
– via. the registers
– or a combination
• In th
he Decaffca lli
lling conventiion, th
he fifirst 6
Question:
• What are the advantages/disadvantages
g g of:
– Calliee saving of registers?
– Caller saving
g of registers?
g
• What registers should be used at the caller
and calliee if half is caller-saved
caller saved and the
other half is calliee-saved?
Registers
g
• What to do with live registers across a procedure
call?
– Caller Saved
– Calliee Saved
Previous
• Arguments 0 to 6 8*n+16(%rbp) argument n
…
are in: 16(%rbp) argument 7
– %rdi, %rsi, %rdx, 8(%rbp) Return address
%rcx %r8 and %r9
%rcx,
0(%rbp) Previous %rbp
urrent
-8(%rbp) local 0
…
Cu
-8*m-8(%rbp) local m
0(%rsp)
Variable size
33
Question:
• Whyy use a stack? Why y not use the heap
p or
pre-allocated in the data segment?
34
Outline
• Introduction
• Machine Language
• Overview of a modern processor
• Memory Layout
• Procedure Abstraction
• Procedure Linkage
• Guidelines in Creating a Code Generator
Procedure Linkages
g
Standard p
procedure linkage
g
procedure p Procedure has
prolog procedure q • standard prolog
prolog • standard epilog
pre-call
ll Each call involves a
post-return • pre-call sequence
epilog
• post
post-return
-return sequence
epilog
return address
• Calling: Caller
– Assume %rcx is live and local variables
is caller save
stack temporaries
– Call foo(A, B, C, D, E, F, G, H, I)
• A to I are at -8(%rbp) to -72(%rbp) dynamic area
rsp
caller
ll savedd registers
it
push %rcx argument 9
push -72(%rbp) argument 8
push -64(%rbp)
64(%rbp) argument 7
push -56(%rbp) return address
mov -48(%rbp), %r9
mov -40(%rbp), %r8
mov -32(%rbp), %rcx
mov -24(%rbp), %rdx
mov -16(%rbp), %rsi
mov -8(%rbp),
8(% b ) %%rdi
di
call foo
return address
• Calling: Calliee
– Assume %rbx is used in the function local variables
and is calliee save stack temporaries
– Assume 40 bytes are required for dynamic area
locals
caller
ll savedd registers
it
argument 9
foo: argument 8
push %rbp argument 7
enter
mov $48, $0
%rsp, %rbp
return address rsp
previous frame pointer
sub $48, %rsp calliee saved
mov % b
%rbx, -8(%rbp)
8(% b ) registers
local variables
stack temporaries
dynamic area
return address
• Arguments
Stack previous frame pointer
calliee saved
registers
• Call foo(A, B, C, D, E, F, G, H, I)
local variables
– Passed in by pushing before the call
push
push
-72(%rbp)
-64(%rbp)
stack temporaries
push -56(%rbp)
mov -48(%rbp), %r9 dynamic area
mov -40(%rbp), %r8
mov -32(%rbp), %rcx
mov -24(%rbp), %rdx caller
ll savedd registers
it
mov -16(%rbp), %rsi
mov -8(%rbp), %rdi argument 9
call foo
argument 8
– Access A to F via registers argument 7
• or put them in local memory
return address
– Access rest using 16+xx(%rbp) rbp
previous frame pointer
mov 16(%rbp), %rax calliee saved
mov 24(%rbp),
( p), %r10 registers
CODE DATA
Global Static Variables
local variables
Control Flow
Global Dynamic Data
Procedures Local Variables stack temporaries
Temporaries
rsp
Statements
Parameter Passing dynamic area
Data Access Read-only Data
return address
Question:
• Do yyou need the $rbp?
p
• What are the advantages and disadvantages
of having $rbp?
49
Outline
• Introduction
• Machine Language
• Overview of a modern processor
• Memory Layout
• Procedure Abstraction
• Procedure Linkage
• Guidelines in Creating a Code Generator
What We Covered Today..
y
CODE DATA
Temporaries
Statements
Parameter Passing
Data Access Read-only Data
Guidelines for the code
generator
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.