Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf•...
Transcript of Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf•...
Getting started
Roel Jordans
• Translate a program from a high level language into something the processor can execute efficiently
• So before we start we need to know how this processor executes a program
2
Goal
Processor architectureby example: AVR
• Separate instruction and Data memories
• All 32 General purpose registers connected to ALU
• IO Modules connected to Data Bus and accessible via Special Function Registers
AVR CPU core
4
Memory organization
• One general purpose register file• Two memories– Program memory– Data memory
• And some peripherals–Memory mapped• e.g. IO ports, timers, EEPROM, ...
5
General purpose registers• 32x8-bit registers• 16-bit registers formed using
pairs of 8 bit registers• X, Y, and Z 16-bit registers– Can be used for indirect
addressing
• Operation input/output schemes:– One 8-bit operand, 8-bit result– Two 8-bit operands, 8-bit result– Two 8-bit operands, 16-bit result– One 16-bit operand, 16-bit result
• Hardware stack pointer
6
Program memory
• On chip, in system programmable
• 32K bytes, in 16K 2 byte instructions
• Accessible via special instructions (LPM,SPM)
• Bootloader support from boot flash section
• Contains interrupt vector table– Program starts with reset (IRQ0)
7
Data memory• Registers mapped in
SRAM memory space– 32 GPR– 64 SFR
• SFR's accessed through in/out instructions (IO registers)
• 2KB of internal SRAM from address 0x060
• Holds the stack!– Required for call instruction– Stack grows down from the
top of memory
8
Important information
• The AVR datasheet– Only 448 pages long!– Also includes info on the available peripherals– Focus on the following sections
• 6 & 7 for the architecture and memory organization• 31 for the instruction-set overview• 30 for the special function register
• Provided on the oncourse website
9
Compilation stages
Compilation flow overview
Assembly Source Files
C/C++ Source and Header
Files
Assembly Source Files
User-created files
compiler assembler
Object Files
Shared Object
File
Linkable Image File
Executable Image File
Link Map File
Linker and Locator
preprocessor
Linker Script
FileMakefile
Make Utility
Library Files
Archive Utility
11
compiler assembler Linker and Locatorpreprocessor
12
Preprocessor
13assembler Linker and Locatorpreprocessor compiler
• Adds some really useful extra features–#include–#define–#ifdef
• Also useful for other languages– Like the assembler
The compiler
• Compilation in three steps– AST, IR, Code generation
14compiler assembler Linker and Locatorpreprocessor
An example from C
15compiler assembler Linker and Locatorpreprocessor
int foo(int a, int b) { return a + b; }
Abstract Syntax Tree (AST)
16compiler assembler Linker and Locatorpreprocessor
TranslationUnitDecl ...|TypedefDecl ...`FunctionDecl ... <f.c:1:1, line:3:1> line:1:5 foo 'int(int,int)' |ParmVarDecl ... <col:9, col:13> col:13 used a 'int' |ParmVarDecl ... <col:16, col:20> col:20 used b 'int' `CompoundStmt ... <col:23, line:3:1> `ReturnStmt ... <line:2:2, col:11> `BinaryOperator ... 'int' '+' |ImplicitCastExpr ... 'int' <LValueToRValue> | `DeclRefExpr ... 'int' lvalue ParmVar ... 'a' 'int' `ImplicitCastExpr ... 'int' <LValueToRValue> `DeclRefExpr ... 'int' lvalue ParmVar ... 'b' 'int'
$ clang Xclang astdump fsyntaxonly f.c
int foo(int a, int b) { return a + b; }
AST Tools
17compiler assembler Linker and Locatorpreprocessor
• Direct translation back to C– Code formatting with clang-format
• AST analysis– clang-check
• Buffer checking C code
– clang-tidy• Cleans up common programming mistakes
• Transforming AST– Source-to-source transformations– clang-modernize
• Update code to use newer language features
Intermediate Representation (IR)
18compiler assembler Linker and Locatorpreprocessor
$ clang target=avr emitllvm S f.c
; ModuleID = 'f.c'target datalayout = "..."target triple = "avrunknownunknown"
; Function Attrs: nounwinddefine i16 @foo(i16 %a, i16 %b) #0 { %1 = alloca i16, align 2 %2 = alloca i16, align 2 store i16 %a, i16* %1, align 2 store i16 %b, i16* %2, align 2 %3 = load i16* %1, align 2 %4 = load i16* %2, align 2 %5 = add nsw i16 %3, %4 ret i16 %5}
int foo(int a, int b) { return a + b; }
IR optimizations
19compiler assembler Linker and Locatorpreprocessor
• Code simplification– Dead-code elimination (DCE)– Common sub-expression elimination (CSE)
• Code analysis– Alias analysis– Control-flow analysis
• Code transformation– Loop transformation– Auto vectorization
Enabling IR optimizations
20compiler assembler Linker and Locatorpreprocessor
$ opt S O1 f.ll o f.opt.ll
; ModuleID = 'f.ll'target datalayout = "..."target triple = "avrunknownunknown"
; Function Attrs: nounwinddefine i16 @foo(i16 %a, i16 %b) #0 { %add = add nsw i16 %a, %b ret i16 %add}
int foo(int a, int b) { return a + b; }
21compiler assembler Linker and Locatorpreprocessor
$ llc f.opt.ll
.text .file "f.opt.ll" .globl foo .align 2 .type foo,@functionfoo: ; @foo; BB#0: ; %entry add r24, r22 adc r25, r23 ret.Lfunc_end0: .size foo, .Lfunc_end0foo
CodeGen
int foo(int a, int b) { return a + b; }
The assembler
• From human readable to executable• Produces object files– Usually in ELF or COFF format
22compiler assembler Linker and Locatorpreprocessor
Executable and Linkable Format
• Contains the binary data for the processor– Code & Data
• Optionally contains extra information– Debug information– Symbol tables– Relocation information
compiler assembler Linker and Locatorpreprocessor23
Linux ‘Executable’ ELF files
• The Executable ELF files produced by the Linux linker are configured for execution in a private ‘virtual’ address space, whereby every program gets loaded at the identical virtual memory-address
24compiler assembler Linker and Locatorpreprocessor
Executable versus Linkable
Section-Header Table(optional)
ELF Header
Section 2 Data
Section 3 Data
…Section n Data
Segment 1 Data
Segment 2 Data
Segment 3 Data
…Segment n Data
Linkable File Executable File
Section-Header Table
Program-Header Table(optional)
Program-Header Table
ELF Header
Section 1 Data
25compiler assembler Linker and Locatorpreprocessor
ELF Header
Section-Header Table
Section 1 Data
Section 2 Data
…Section n Data
Linkable File
ELF Header
Section-Header Table
Section 1 Data
Section 2 Data
…Section n Data
Linkable File
Role of the LinkerELF Header
Program-Header Table
Segment 1 Data
Segment 2 Data
…Segment n Data
Executable File
26compiler assembler Linker and Locatorpreprocessor
The linker
• Controllable by a linker script– Default provided for simple architectures
• Required when the mapping of sections to memories becomes more complex–Multiple data memories–Moving code around at runtime– Fat binaries: supporting multiple architectures– Encrypted binaries
27compiler assembler Linker and Locatorpreprocessor
C runtime
• Initializing the processor• What happens before and after main– Setting up the stack– Calling main– Stopping execution
28compiler assembler Linker and Locatorpreprocessor
To summarize
Assembly Source Files
C/C++ Source and Header
Files
Assembly Source Files
User-created files
compiler assembler
Object Files
Shared Object
File
Linkable Image File
Executable Image File
Link Map File
Linker and Locator
preprocessor
Linker Script
FileMakefile
Make Utility
Library Files
Archive Utility
29
The compiler driver
The compiler driver
• Many different tools in the compilation process–Mostly useful when debugging the compiler
• Needs simple interface
• The frontend program (clang) can actually call the right programs for you!
30
PCP Assignment 1:AVR backend
The application
Overview
33
• Practice makes perfect• Assignments on the ES group servers
– Login information can be found on oncourse.tue.nl
– Password distributed in “My grades”
• Start with “Tool setup”• Download and follow the assignment
instructions• Answer the Assignment 1.A quiz
questions on oncourse
Bonus exercise 1
• Make (or Makefiles)• As mentioned earlier in this lecture
– Manages compilation steps– Very useful to know– (Ab)used for many other purposes
• Also available on oncourse– Take home exercise with questions
online– Try to automate the build process of
assignment 1
AcknowledgementsThese slides were partially based on the following:• www.cs.usfca.edu/~cruse/cs630f06/lesson15.ppt
• http://embsys.technikum-wien.at/staff/veigl/MDBA/slides/II-AVR-Basics%281%29.ppt
36