Overall Structure

  • View
    221

  • Download
    0

Embed Size (px)

Citation preview

  • 8/3/2019 Overall Structure

    1/25

    Chuen-Liang Chen, NTU CS&IE / 1Overall Structure

    Overall Structure

    Chuen-Liang Chen

    Department of Computer Science

    and Information Engineering

    National Taiwan University

    [email protected]

  • 8/3/2019 Overall Structure

    2/25

    Chuen-Liang Chen, NTU CS&IE / 2Overall Structure

    OutlineLanguage Processors

    Phases of a Compiler

    Compiler Structure

    Compiler Construction

    Compiler Installation/Porting

    http://opt/scribd/conversion/tmp/scratch6058/slide3.xmlhttp://opt/scribd/conversion/tmp/scratch6058/slide6.xmlhttp://opt/scribd/conversion/tmp/scratch6058/slide9.xmlhttp://opt/scribd/conversion/tmp/scratch6058/slide13.xmlhttp://opt/scribd/conversion/tmp/scratch6058/slide20.xmlhttp://opt/scribd/conversion/tmp/scratch6058/slide20.xmlhttp://opt/scribd/conversion/tmp/scratch6058/slide13.xmlhttp://opt/scribd/conversion/tmp/scratch6058/slide9.xmlhttp://opt/scribd/conversion/tmp/scratch6058/slide6.xmlhttp://opt/scribd/conversion/tmp/scratch6058/slide3.xml
  • 8/3/2019 Overall Structure

    3/25

    Chuen-Liang Chen, NTU CS&IE / 3Overall Structure

    Compiler

    sourceprogram

    targetprogram

    input

    output

    Interprete

    r

    sourceprogram outpu

    tinput

    Language Processors

    targetprogram

    Just In TimeCompiler

    for hotspot

    Compiler

    sourceprogram

    intermediate program outputinput

    Virtual Machine(incl.

    interpreter)

  • 8/3/2019 Overall Structure

    4/25Chuen-Liang Chen, NTU CS&IE / 4Overall Structure

    Tool Chaintoolchain: a set of linked development tools

    text editor preprocessor compiler assembler linker/loader debugger, libraries

    GNU toolchainGNU makeGCC

    GNUbinutils(binary utilities)GNU debugger(gdb)libraries

    output of a compiler, may:

    assemblyrelocatableabsolute binaryother source

    http://www.gnu.org/software/make/http://gcc.gnu.org/http://www.gnu.org/software/binutils/http://www.gnu.org/software/binutils/http://www.gnu.org/software/gdb/http://www.gnu.org/software/gdb/http://www.gnu.org/software/binutils/http://www.gnu.org/software/binutils/http://gcc.gnu.org/http://www.gnu.org/software/make/
  • 8/3/2019 Overall Structure

    5/25Chuen-Liang Chen, NTU CS&IE / 5Overall Structure

    GNU Maketo automate the process of converting files

    rule format

    target: prerequisites(tab) command 1command n

    target: file to be created, or action nameprerequisites: input files to create the target

    commands: make process if any of the prerequisites changesimple example

    helloWorld: helloWorld.occ -o helloWorld helloWorld.o

    helloWorld.o: helloWorld.c

    cc -c helloWorld.cclean:

    rm helloWorld helloWorld.ousage: make or make clean

  • 8/3/2019 Overall Structure

    6/25Chuen-Liang Chen, NTU CS&IE / 6Overall Structure

    Phases of a Compiler

  • 8/3/2019 Overall Structure

    7/25Chuen-Liang Chen, NTU CS&IE / 7Overall Structure

    Analyseslexical

    regular expression

    syntactic (structure)

    context-free grammar

    semantics (meaning)

    static semantics attribute grammar

    run-time semantics

    Vienna definition

    language, ...

    syntax error a = b + ;

    static semantic error int a , b ;

    boolean c ;

    a = b + c ;

    run-time semantic error int a , b , c ;

    scanf(%d, &b);

    // assume: max int

    c = 1 ; a = b + c ;

  • 8/3/2019 Overall Structure

    8/25Chuen-Liang Chen, NTU CS&IE / 8Overall Structure

    Phases of a Compiler Example

  • 8/3/2019 Overall Structure

    9/25Chuen-Liang Chen, NTU CS&IE / 9Overall Structure

    Compiler Structuresyntax-directed translation

    driven by the syntactic structure of source program

    call graph (multi-passes)

    machinecode

    token

    SS: syntactic structure (abstract syntax tree)IR: intermediate representations

    sourcecode

    SS

    IR

    main

    parser

    scanner semanticroutines

    codegenerator

    symbol tableattribute table

    opt 2opt 1

  • 8/3/2019 Overall Structure

    10/25Chuen-Liang Chen, NTU CS&IE /Overall Structure

    Intermediate Representation of a Compiler

    front-endfrom source code to intermediate codeanalysis phaseslanguage dependent

    intermediate representation (intermediate code)

    back-endfrom intermediate code to target code

    synthesis phasesmachine dependent

    intermediaterepresentatio

    n

    C front-end

    Fortran front-end

    Java front-end

    arm back-end

    i386 back-end

    mips back-end

    sparc back-end

  • 8/3/2019 Overall Structure

    11/25

    Chuen-Liang Chen, NTU CS&IE /Overall Structure

    GCC Historyhistory / releases

    1987 Gnu C Compiler1997~1999 Experimental Gnu Compiler System (EGCS)

    pronounced eggs

    EGCS & GCC reunited in 1999; winner: EGCS

    1999 GCC Development Mission Statement1999 Gnu Compiler Collection 2.95

    2001 GCC 3.02005/04 GCC 4.0.0 2009/04 GCC 4.4.02006/02 GCC 4.1.0 2010/04 GCC 4.5.02007/05 GCC 4.2.0 2011/03 GCC 4.6.02008/03 GCC 4.3.0 2011/10 GCC 4.6.2

    design and development goals (from Mission Statement)new languages (front-end, language-dependent)new optimizations (middle-end)new targets (back-end, machine-dependent)improved runtime librariesfaster debug cyclevarious other infrastructure improvements

    http://gcc.gnu.org/wiki/Historyhttp://gcc.gnu.org/releases.htmlhttp://gcc.gnu.org/gccmission.htmlhttp://gcc.gnu.org/gccmission.htmlhttp://gcc.gnu.org/releases.htmlhttp://gcc.gnu.org/wiki/History
  • 8/3/2019 Overall Structure

    12/25

    Chuen-Liang Chen, NTU CS&IE /Overall Structure

    GCC Structure

    source

    rtl / cfg

    assembly

    tree/ cfg

    parsinggimplification

    build_cfg

    expand

    final

    tree optimizations

    rtl optimizations

    machineindependent

    machinedependent

  • 8/3/2019 Overall Structure

    13/25

    Chuen-Liang Chen, NTU CS&IE /Overall Structure

    Compiler Constructionhand-written

    compiler generatoraka, compiler compiler

    how to use C to write a C compiler?

  • 8/3/2019 Overall Structure

    14/25

    Chuen-Liang Chen, NTU CS&IE /Overall Structure

    Pascal P-code (1/5)

    (format, input output)Machine code, Assembly, P-code, Pascal, Data

    without high level language support

    P-code

    Interpreter(A, Pc ) 6553940663

    0653636594

    Sorting

    (A, DD)

    Assembler(M, AM)

    P-code interpreter(M, Pc )

    Sorting(M, DD)

    Platform (Hardware + OS)

  • 8/3/2019 Overall Structure

    15/25

    Chuen-Liang Chen, NTU CS&IE /Overall Structure

    Interpretersoftware CPU

    int IR; // IR: instruction register

    int PC = 0; // PC: program counter int code[ ];while(1) { // clockIR = code[PC]; // instruction fetchingswitch (IR) { // instruction decodingcase 1: execution of opcode 1; break;case 2: execution of opcode 2; break;

    case n: execution of opcode n; break;// instruction execution// data fetching (using PC)}PC += instruction_length; // update program counter}

    easier than a compiler

  • 8/3/2019 Overall Structure

    16/25

    Chuen-Liang Chen, NTU CS&IE /Overall Structure

    Pascal P-code (2/5)

    (format, input output)Machine code, Assembly, P-code, Pascal, Data

    slower compilation, slower execution

    PascalCompiler

    (Pc,PaPc)

    P-code

    Interpreter(A, Pc )

    Pascal

    Compiler(Pa,PaPc) 6553940663

    06536365

    94

    Sorting

    (Pa,DD)

    Sorting(Pc,DD)

    Assembler(M, AM)

    P-code interpreter(M, Pc )

    Platform (Hardware + OS)

  • 8/3/2019 Overall Structure

    17/25

    Chuen-Liang Chen, NTU CS&IE /Overall Structure

    Pascal P-code (3/5)

    (format, input output)Machine code, Assembly, P-code, Pascal, Data

    slower compilation, faster execution

    P-code

    Interpreter(A, Pc )

    Pascal

    Compiler(Pa,PaPc)

    Pascal

    Compiler(Pa,PaM) 6553940663

    0653636594

    Sorting

    (Pa,DD)

    PascalCompiler

    (Pc,PaPc)

    PascalCompiler(Pc,PaM)

    Assembler(M, AM)

    P-code interpreter(M, Pc )

    Sorting(M, DD)

    Platform (Hardware + OS)

  • 8/3/2019 Overall Structure

    18/25

    Chuen-Liang Chen, NTU CS&IE /Overall Structure

    Pascal P-code (4/5)

    (format, input output)Machine code, Assembly, P-code, Pascal, Data

    faster compilation, faster execution

    P-code

    Interpreter(A, Pc )

    PascalCompiler

    (Pc,PaPc)

    Pascal

    Compiler(Pa,PaPc)

    Pascal

    Compiler(Pa,PaM)

    PascalCompiler(Pc,PaM)

    6553940663

    0653636594

    Sorting

    (Pa,DD)

    Assembler(M, AM)

    P-code interpreter(M, Pc )

    PascalCompiler(M, PaM)

    Sorting(M, DD)

    Platform (Hardware + OS)

  • 8/3/2019 Overall Structure

    19/25

    Chuen-Liang Chen, NTU CS&IE /Overall Structure

    Pascal P-code (5/5)

    P-code

    Interpreter(A, Pc )

    Pascal

    Compiler(Pa,PaPc)

    Pascal

    Compiler(Pa,PaA)

    PascalCompiler(A, PaA)

    PascalCompiler

    (Pc,PaPc)

    PascalCompiler(Pc,PaA)

    Assembler(M, AM)

    P-code interpreter(M, Pc )

    Assembler(M, AM)

    PascalCompiler(M, PaA)

    Platform (Hardware + OS)

    l (format, inputoutput)

    u Machine code, Assembly, P-code, Pascal, Datal easier implementation

  • 8/3/2019 Overall Structure

    20/25

    Chuen-Liang Chen, NTU CS&IE /Overall Structure

    Compiler Installation/Portingbuild a GCC on a Sparc workstation

    (build machine)

    run the GCC for a sorting program on an i386 PC

    (host machine)execute the sorting on an Arm embedded system

    (target machine)

    classification of compilers

    build host target classification application example

    A A A native compiler upgradeA A C cross developing env. for

    embedded system

    A B A cross-back

    A B B crossed native new CPU, powerful

    enough to run compilerA B C canadian

  • 8/3/2019 Overall Structure

    21/25

    Chuen-Liang Chen, NTU CS&IE /Overall Structure

    Build, Host & Target Machines

    compilerexecutable(B, src H)

    compilersource

    compilerexecutable(H, src T)

    applicationsource

    applicationexecutable(T, ? ?)

    Build Machine Host MachineTarget

    Machine

  • 8/3/2019 Overall Structure

    22/25

    Chuen-Liang Chen, NTU CS&IE /Overall Structure

    Comparison of Different Versions

    gcc 2.95(Csparc)

    [anyexe]

    gcc 4.6.2(Csparc)

    [anyexe]

    sorting[src]

    sorting[sparc exe]

    {2.95 tech}

    sorting[sparc exe]

    {4.6.2 tech}

    gcc 4.6.2(Csparc)[sparc exe]{2.95 tech}

    gcc 4.6.2(Csparc)[sparc exe]{4.6.2 tech}

    searching[src]

    searching[sparc exe]{4.6.2 tech}

    searching[sparc exe]{4.6.2 tech}

    l worse execution

    l better execution

    l worse compilationl same execution

    l better compilationl same execution

    (s/w function)[code format]{code quality}

  • 8/3/2019 Overall Structure

    23/25

    Chuen-Liang Chen, NTU CS&IE /Overall Structure

    Native GCC Building

    assume: each GCC stagebundles the existed binutils

    (as, ld)stage new functionworse code quality

    stage new functionbetter code quality

    stage same as stage converged

    gcc 2.95(Csparc)

    [sparc exe]

    gcc 4.6.2(Csparc)[sparc exe]

    {2.95 tech}

    gcc 4.6.2(Csparc)[sparc exe]{4.6.2 tech}

    gcc 4.6.2(Csparc)[sparc exe]{4.6.2 tech}

    gcc 4.6.2sparc.md

    [src]

    library[src]

    library

    [sparc obj]{4.6.2 tech}

    build

    host

    target

    helloWorld[sparc exe]{4.6.2 tech}

    helloWorld[src]

    -c -c

  • 8/3/2019 Overall Structure

    24/25

    Chuen-Liang Chen, NTU CS&IE /Overall Structure

    Cross GCC Buildingassume: build machine (sparc) already has the newest

    native gcc & binutils

    cross binutils is required !!!

    gccarm.md

    [src]

    binutilscpu-arm.c

    [src]

    library[src]

    library[arm obj]

    helloWorld[arm exe]

    helloWorld[src]

    -c -c

    -Slibrary

    [arm asm]

    -S

    gcc(Carm)[sparc exe]

    binutils(arm)

    [sparc exe]

    gcc(Csparc)[sparc exe]

    binutils(sparc)

    [sparc exe]

    C il d C S i

  • 8/3/2019 Overall Structure

    25/25

    Chuen-Liang Chen, NTU CS&IE /Overall Structure

    Compiler and Computer Sciencecompiler and programming language

    new language features new compilation challengeexisted compiling problem modified language features

    compiler and architecture/platformnew resources new compilation challenge

    optimization technology other fields in computer science