Download - ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Transcript
Page 1: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

ΕΠΛ323-ΘεωρίακαιΠρακτικήΜεταγλωττιστών

Lecture1Introduc)onEliasAthanasopoulos

[email protected]

Page 2: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Contract-LogisNcs

•  Officehours– EveryMonday,10:00–12:00,B105(ΘΕΕ01)

•  Credits:7.5ECTS•  LectureTimetable– TuesdayandFriday,9:00–10:30,103(ΧΩΔ01)– Wednesday,9:00-10:00,110(ΧΩΔ01),onlywhenannounced!

Page 3: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Contract-LogisNcs

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

Page 4: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Contract-LogisNcs

•  Score– ProgrammingAssignments(Quizzes),15%– Project(mulNplesteps),15%– Midterm,30%– Final,40%

•  Requirements– Averagegradeofwrivenexamsshouldbeatleast4.5

– Finalgradeshouldbeatleast5

Page 5: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Contract-LogisNcs

•  Requiredcourses– ΕΠΛ132(Cprogramming),– ΕΠΛ211(Complexity),– ΕΠΛ231(DataStructures)

Studentswithconflictsshouldletmeknow.

Page 6: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Contract-LogisNcs

•  Readingmaterial– Dragonbook,Compilers–Principles,Techniques,andTools(2ndEdi9on),A.V.Aho,M.S.Lam,R.SethiandJ.D.Ullman,PrenNceHall,2006.

Page 7: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

INTRODUCTION(Chapter1fromDragonBook)

Page 8: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Whythiscourseisimportant?

•  ManycoreconceptsofCSinareal-lifese�ng– CS132:BehindthescenesofCprogramming– CS211,CS231:DFAs,parsingalgorithms– CS372,CS221:Assembly,ComputerArchitecture

•  Compilersareeverywhere– FromexoNcdevices(IoT)towebbrowsers(JavaScriptengines,DOMparsers,etc.)

•  TweakingcompilersforSecurity

Page 9: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Compilerisatoughone

•  Significantefforttobuildacompilerfromscratch– FirstFortrancompilertook18staff-years

•  Nowadays,manytoolsexisttohelp– Ourunderstandingismuchbever– Bevertechniques,beverprogramminglanguages

Page 10: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Whatisacompiler?

•  Acompilerisaprogramthat–  readsaprogramwriveninonelanguage(source)– andtranslatesittoanequivalentprograminanotherlanguage(target)

–  important:errorreporNngduringtranslaNon

Compilersourceprogram targetprogram

errormessages

Page 11: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Examples

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

•  LLVM(LowLevelVirtualMachine)– clang,clang++

•  “Compilers”areeverywhere,– Prevyprinters(i.e.,colorsyntaxineditors),staNccheckers,interpreters(i.e.,scripNnglanguages),etc.

Page 12: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Analysis-SynthesisModel

•  TherearetwopartsincompilaNon:– Analysis– Synthesis

•  Analysis– BreaksupthesourceprogramtosubpartsandcreatesintermediaterepresentaNon(s)

•  Synthesis– ConstructsthetargetprogramfromintermediaterepresentaNon(s)

Page 13: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

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

Page 14: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Example2(Database)

class

student Seq. Scan

Hash Join

Aggregate

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

Page 15: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Requirements•  Compiler–  Reliability–  FastexecuNon–  Lowmemoryoverhead– GooderrorreporNng–  Errorrecovery–  Portability– Maintainability

•  Targetprogram–  FastexecuNon–  Lowmemoryoverhead

Page 16: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Sourcecode/program

•  Easytoread/writebyhuman

int expr(int n) {int d;

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

return d;}

Page 17: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

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)

Page 18: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

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

Page 19: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

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

Page 20: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

CompilaNonlifecycle

•  Phases– SourcecodeistransformedtointermediaterepresentaNons

– EachintermediaterepresentaNonissuitableforaparNcularprocessing(lexical,syntax,opNmizaNon,etc.)

•  IneachphasetheprogramistranslatedtoaformclosertothemachinerepresentaNonandlesssimilartothe(human-oriented)sourcerepresentaNon

Page 21: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

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(Ενδιάμεσοςκώδικας)

Page 22: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

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 … …

Page 23: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

SyntaxAnalysisΣυντακτικήΑνάλυση

•  Hierarchical•  InvolvesgroupingthetokensintogrammaNcalphases

•  ConstructsthestructurewiththetokenrelaNonship

op(:=)

id(3) cons(60)

id(2) op(*)

id(1) op(+)

position := initial + rate * 60

Page 24: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

SimpleGrammar

•  Thehierarchicalstructureoftheprogramisusuallyexpressedbyrecursiverules1.  Anyiden9fierisanexpression2.  Anynumberisanexpression3.  Ifexpression1andexpression2areexpressions,

thensoare: expression1+expression2 expression1*expression2 (expression1)

Page 25: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Applyingthegrammar

op(:=)

id(3) cons(60)

id(2) op(*)

id(1) op(+)

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

expression1+expression2expression1*expression2(expression1)

Page 26: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

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!

Page 27: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

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:τελεστέος

Page 28: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

CodeGeneraNonΠαραγωγήΚώδικα

•  ThelastphaseofthecompileristhegeneraNonofthetargetcode

•  RegisterallocaNon– Eachexpressionshoulduseregistersthatareavailable

•  RelocaNoninformaNon– Variablesarestoredinrelocatableaddresses

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

Page 29: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

GenericPictureCompilerandFriends

Sourcecode

Compiler

Assemblycode

Assembler

Machinecode

Executable

Linker

ΟSLoader

Page 30: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

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

Page 31: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Passes

•  Apassiswhenthecompilerreadsthesourcecode(orintermediatefiles)

•  Thenumberofpassesdependsonthesourceandtargetlanguageandtherunningenvironment

•  Differentphasesthatcooperatecanbegroupedtoasinglepass(notalwayspossible)

•  Whengroupingisnotpossible–  Backpatching:leaveemptyinformaNonthatisgoingtobefilledbyalaterphase/pass

Page 32: ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστώνeliasathan/courses/epl323/resources/epl... · ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Compiler-construcNonTools

•  Parsergenerators•  Scannergenerators•  Syntax-directedtranslaNonengines•  AutomaNccodegenerators•  Data-flowengines