• ›323 - µ‰¯± ±¹...

download • ›323 - µ‰¯± ±¹  ±„¹® œµ„±³»‰„„¹ƒ„½ eliasathan/courses/epl323/resources/epl...• ›323

of 32

  • date post

    11-May-2018
  • Category

    Documents

  • view

    219
  • download

    0

Embed Size (px)

Transcript of • ›323 - µ‰¯± ±¹...

  • 323-

    Lecture1Introduc)onEliasAthanasopoulos

    eliasathan@cs.ucy.ac.cy

  • Contract-LogisNcs

    OfficehoursEveryMonday,10:0012:00,B105(01)

    Credits:7.5ECTS LectureTimetableTuesdayandFriday,9:0010:30,103(01)Wednesday,9:00-10:00,110(01),onlywhenannounced!

  • Contract-LogisNcs

    Labs(Dr.PetrosPanayi)EveryWednesday,B103(01)Group1:16:00-18:00Group2:18:00-20:00

  • Contract-LogisNcs

    ScoreProgrammingAssignments(Quizzes),15%Project(mulNplesteps),15%Midterm,30%Final,40%

    RequirementsAveragegradeofwrivenexamsshouldbeatleast4.5

    Finalgradeshouldbeatleast5

  • Contract-LogisNcs

    Requiredcourses132(Cprogramming),211(Complexity),231(DataStructures)

    Studentswithconflictsshouldletmeknow.

  • Contract-LogisNcs

    ReadingmaterialDragonbook,CompilersPrinciples,Techniques,andTools(2ndEdi9on),A.V.Aho,M.S.Lam,R.SethiandJ.D.Ullman,PrenNceHall,2006.

  • INTRODUCTION(Chapter1fromDragonBook)

  • Whythiscourseisimportant?

    ManycoreconceptsofCSinareal-lifesengCS132:BehindthescenesofCprogrammingCS211,CS231:DFAs,parsingalgorithmsCS372,CS221:Assembly,ComputerArchitecture

    CompilersareeverywhereFromexoNcdevices(IoT)towebbrowsers(JavaScriptengines,DOMparsers,etc.)

    TweakingcompilersforSecurity

  • Compilerisatoughone

    SignificantefforttobuildacompilerfromscratchFirstFortrancompilertook18staff-years

    Nowadays,manytoolsexisttohelpOurunderstandingismuchbeverBevertechniques,beverprogramminglanguages

  • Whatisacompiler?

    Acompilerisaprogramthat readsaprogramwriveninonelanguage(source)andtranslatesittoanequivalentprograminanotherlanguage(target)

    important:errorreporNngduringtranslaNon

    Compilersourceprogram targetprogram

    errormessages

  • Examples

    GCC(GnuCompilerCollecNon)gcc,g++,javac,etc.

    LLVM(LowLevelVirtualMachine)clang,clang++

    Compilersareeverywhere,Prevyprinters(i.e.,colorsyntaxineditors),staNccheckers,interpreters(i.e.,scripNnglanguages),etc.

  • Analysis-SynthesisModel

    TherearetwopartsincompilaNon:AnalysisSynthesis

    AnalysisBreaksupthesourceprogramtosubpartsandcreatesintermediaterepresentaNon(s)

    SynthesisConstructsthetargetprogramfromintermediaterepresentaNon(s)

  • Example1(LaTeX)\begin{table}[tb] \centering \caption{We name gadgets based on their type (prefix), payload (body), and exit instruction (suffix). In total, we name 2$\times$3$\times$3=18 different gadget types.} \begin{tabular}{|c|c|c|} \hline \textbf{Gadget type} & \textbf{Payload instructions} & \textbf{Exit instruction} \\ \hline {Prefix} & {Body} & {Suffix} \\ \hline \begin{tabular}{l} CS - Call site\\ EP - Entry point\\ \end{tabular} &

    ...

    $ pdflatex main.tex

  • Example2(Database)

    class

    student Seq. Scan

    Hash Join

    Aggregate

    SELECT AVG(grade)FROM student, classWHERE class.name = epl223 AND class.id = student.id;

  • Requirements Compiler Reliability FastexecuNon LowmemoryoverheadGooderrorreporNng Errorrecovery PortabilityMaintainability

    Targetprogram FastexecuNon Lowmemoryoverhead

  • Sourcecode/program

    Easytoread/writebyhuman

    int expr(int n) {int d;

    d = 4 * n * n * (n + 1) * (n + 1);

    return d;}

  • AssemblyandMachineCode

    OpNmizedforexecuNonbyamachine(CPU) LessdescripNve Hardtobeprocessedbyahuman

    lda $30,-32($30)stq $26,0($30)stq $15,8($30)bis $30,$30,$15bis $16,$16,$1stl $1,16($15)lds $f1,16($15)sts $f1,24($15)ldl $5,24($15)bis $5,$5,$2s4addq $2,0,$3ldl $4,16($15)mull $4,$3,$2ldl $3,16($15)

  • OpNmizaNons

    CompilershaveseverallayersofopNmizaNonsint expr(int n){

    int d;

    d = 4 * n * n * (n + 1) * (n + 1);

    return d;

    } .expr: stw 31,-4(1) lwz 11,64(31) stwu 1,-40(1) addi 9,11,1 mr 31,1 mullw 0,0,9 stw 3,64(31) stw 0,24(31) lwz 0,64(31) lwz 0,24(31) mr 9,0 mr 3,0 slwi 0,9,2 b L..2 lwz 9,64(31) L..2: mullw 0,0,9 lwz 1,0(1) lwz 11,64(31) lwz 31,-4(1) addi 9,11,1 blr mullw 0,0,9

    .expr: addi 9,3,1 slwi 0,3,2 mullw 3,3,0 mullw 3,3,9 mullw 3,3,9 blr

    NoopNmizaNons$ gcc O0

    OpNmizaNons$ gcc O3

  • Cross-compiler

    Compilerscangeneratecodefordifferentmachines(targets) int expr(int n){

    int d;

    d = 4 * n * n * (n + 1) * (n + 1);

    return d;

    }

    expr: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax leal 1(%eax), %edx imull %eax, %eax imull %edx, %eax imull %edx, %eax sall $2, %eax popl %ebp ret

    .expr: addi 9,3,1 slwi 0,3,2 mullw 3,3,0 mullw 3,3,9 mullw 3,3,9 blr

    Forx86$ gcc O3 b i586

    ForPowerPC$ gcc O3 b powerpc

  • CompilaNonlifecycle

    PhasesSourcecodeistransformedtointermediaterepresentaNons

    EachintermediaterepresentaNonissuitableforaparNcularprocessing(lexical,syntax,opNmizaNon,etc.)

    IneachphasetheprogramistranslatedtoaformclosertothemachinerepresentaNonandlesssimilartothe(human-oriented)sourcerepresentaNon

  • CompilerPhases

    Lexical Analyzer

    Source Program

    Target Program

    Syntax Analyzer

    Semantic Analyzer

    Intermediate codegenerator

    Code optimizer

    Code generator

    Error HandlerSymbol Table Manager

    1

    2

    2

    3

    3

    OpNonal

    1:Tokens()2:Syntaxtree()3:Intermediatecode()

  • LexicalAnalysis

    Linearscanning Considertheexpression

    position := initial + rate *60

    Lexicalanalysisproducesid(1) op(:=) id(2) op(+) id(3) op(*) cons(60)id: identifier, op: operator, cons: constant

    SymbolTable 1 position 2 initial 3 rate 4

  • SyntaxAnalysis

    Hierarchical InvolvesgroupingthetokensintogrammaNcalphases

    ConstructsthestructurewiththetokenrelaNonship

    op(:=)

    id(3) cons(60)

    id(2) op(*)

    id(1) op(+)

    position := initial + rate * 60

  • SimpleGrammar

    Thehierarchicalstructureoftheprogramisusuallyexpressedbyrecursiverules1. Anyiden9fierisanexpression2. Anynumberisanexpression3. Ifexpression1andexpression2areexpressions,

    thensoare: expression1+expression2 expression1*expression2 (expression1)

  • Applyingthegrammar

    op(:=)

    id(3) cons(60)

    id(2) op(*)

    id(1) op(+)

    (1)Anyiden9fierisanexpression(2)Anynumberisanexpression(3)Ifexpression1andexpression2areexpressions,thensoare:

    expression1+expression2expression1*expression2(expression1)

  • SemanNcAnalysis

    CheckstheprogramforsemanNcerrors GatherstypeinformaNon Operandsandoperators Type-checking

    op(:=)

    id(3) int_2_real

    id(2) op(*)

    id(1) op(+)

    cons(60)

    FLOAT

    INTposition := initial + rate * 60

    int_2_real()isanextranodeforconverNng60toarealnumber.Remember:themachinerepresentaNonofintegersandrealnumbersisdifferent!

  • IntermediateCodeandOpNmizaNon

    Eachphaseproducesintermediatecode

    OpNmizaNon

    temp1 := int_2_real(60)temp2 := id(3) * temp1temp3 := id(2) + temp2id(1) := temp3

    temp1 := id(3) * 60.0id(1) := id(2) + temp1

    three-addresscode:asimpleassembly-likelanguage,whichconsistsofinstrucNons,eachofwhichhasatmostthreeoperands

    Operator:Operand:

  • CodeGeneraNon

    ThelastphaseofthecompileristhegeneraNonofthetargetcode

    RegisterallocaNonEachexpressionshoulduseregistersthatareavailable

    RelocaNoninformaNonVariablesarestoredinrelocatableaddresses

    MOVF id3, R2MULF #60.0, R2MOVF id2, R1ADDF R2, R1MOVF R1, id1

  • GenericPictureCompilerandFriends

    Sourcecode

    Compiler

    Assemblycode

    Assembler

    Machinecode

    Executable

    Linker

    SLoader

  • FrontandBackends SeparaNonofcommontasks Makesdesignand

    implementaNoneasier KcompilersforNmachines Nbackends,Kfrontends InsteadofK*Ncompilers

    Frontend(machine-

    independent)

    Backend

    Lexical Analyzer

    Source Program

    Target Program

    Syntax Analyzer

    Semantic Analyzer

    Intermediate codegenerator

    Code optimizer

    Code generator

  • Passes

    Apassiswhenthecompilerreadsthesourcecode(orintermediatefiles)

    Thenumberofpassesdependsonthesourceandtargetlanguageandtherunningenvironment

    Differentphasesthatcooperatecanbegroupedtoasinglepass(notalwayspossible)

    Whengroupingisnotpossible Backpatching:leaveemptyinformaNonthatisgoingtobefilledbyalaterphase/pass

  • Compiler-construcNonTools

    Parsergenerators Scannergenerators Syntax-directedtranslaNonengines AutomaNccodegenerators Data-flowengines