3/11/2015
Program design and 1 2
analysis Compilation
aCompilation flow. aCompilation strategy (Wirth):
aBasic statement translation. `compilation = translation + optimization
aBasic optimizations
optimizations. aCompiler determines quality of code:
aInterpreters and just-in-time compilers. `use of CPU resources;
`memory access scheduling;
`code size.
3
Statement translation and 4
Basic compilation phases optimization
HLL
aSource code is translated into
parsing, symbol table intermediate form such as CDFG.
aCDFG is transformed/optimized.
/ p
machine-independent
optimizations aCDFG is translated into instructions with
optimization decisions.
machine-dependent
optimizations aInstructions are further optimized.
assembly
1
3/11/2015
5
Arithmetic expressions, 6
Arithmetic expressions contd.
b a b c d
a*b + 5*(c-d) a c d ADR r4,a
MOV r1,[r4]
* - 1 * 2 - ADR r4,b
expression MOV r2,[r4]
5 5
ADD r3,r1,r2
ADR r4,c
* 3 * MOV r1,[r4]
ADR r4,d
MOV r5,[r4]
SUB r6,r4,r5
+ 4 +
MUL r7,r6,#5
ADD r8,r7,r3
DFG DFG code
7
Control code generation, 8
Control code generation contd.
ADR r5,a
if (a+b > 0) LDR r1,[r5]
ADR r5,b
x = 5; 1 a+b>0 x=5 2 LDR r2,b
else a+b>0 x=5 ADD r3,r1,r2
BLE llabel3
b l3
x = 7; 3 LDR r3,#5
x=7 ADR r5,x
x=7 STR r3,[r5]
B stmtent
LDR r3,#7
ADR r5,x
STR r3,[r5]
stmtent ...
2
3/11/2015
9 10
Procedure linkage Procedure stacks
aNeed code to: growth
`call and return; proc1 proc1(int a) {
proc2(5);
p
`pass parameters
p and results. }
FP
aParameters and returns are passed on frame pointer
stack. proc2
`Procedures with few parameters may use 5 accessed relative to SP
SP
registers. stack pointer
11 12
ARM procedure linkage Data structures
aAPCS (ARM Procedure Call Standard): aDifferent types of data structures use
`r0-r3 pass parameters into procedure. Extra different data layouts.
parameters are put on stack frame. aSome offsets into data structure can be
`r0 holds return value. computed at compile time, others must be
`r4-r7 hold register values. computed at run time.
`r11 is frame pointer, r13 is stack pointer.
`r10 holds limiting address on stack size to
check for stack overflows.
3
3/11/2015
13 14
One-dimensional arrays Two-dimensional arrays
aC array name points to 0th element: aColumn-major layout:
a[0,0]
a a[0]
a[0,1] M
a[1] = *(a + 1)
...
N
a[2]
... a[1,0]
a[1,1] = a[i*M+j]