Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf•...

36
Getting started Roel Jordans

Transcript of Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf•...

Page 1: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

Getting started

Roel Jordans

Page 2: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

• 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

Page 3: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

Processor architectureby example: AVR

Page 4: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

• 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

Page 5: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 6: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 7: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 8: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 9: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 10: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

Compilation stages

Page 11: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 12: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

compiler assembler Linker and Locatorpreprocessor

12

Page 13: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

Preprocessor

13assembler Linker and Locatorpreprocessor compiler

• Adds some really useful extra features–#include–#define–#ifdef

• Also useful for other languages– Like the assembler

Page 14: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

The compiler

• Compilation in three steps– AST, IR, Code generation

14compiler assembler Linker and Locatorpreprocessor

Page 15: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

An example from C

15compiler assembler Linker and Locatorpreprocessor

int foo(int a, int b) { return a + b; }

Page 16: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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 ­ast­dump ­fsyntax­only f.c

int foo(int a, int b) { return a + b; }

Page 17: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 18: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

Intermediate Representation (IR)

18compiler assembler Linker and Locatorpreprocessor

$ clang ­­target=avr ­emit­llvm ­S f.c 

; ModuleID = 'f.c'target datalayout = "..."target triple = "avr­unknown­unknown"

; 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; }

Page 19: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 20: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

Enabling IR optimizations

20compiler assembler Linker and Locatorpreprocessor

$ opt ­S ­O1 f.ll ­o f.opt.ll

; ModuleID = 'f.ll'target datalayout = "..."target triple = "avr­unknown­unknown"

; 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; }

Page 21: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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_end0­foo

CodeGen

int foo(int a, int b) { return a + b; }

Page 22: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

The assembler

• From human readable to executable• Produces object files– Usually in ELF or COFF format

22compiler assembler Linker and Locatorpreprocessor

Page 23: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 24: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 25: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 26: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 27: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 28: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

C runtime

• Initializing the processor• What happens before and after main– Setting up the stack– Calling main– Stopping execution

28compiler assembler Linker and Locatorpreprocessor

Page 29: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 30: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 31: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

PCP Assignment 1:AVR backend

Page 32: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

The application

Page 33: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 34: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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

Page 35: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available
Page 36: Getting started - Eindhoven University of Technologyrjordans/5LIM0/01-compiler organization.pdf• The AVR datasheet – Only 448 pages long! – Also includes info on the available

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