Compilation 0368 -3133

Download Compilation   0368 -3133

Post on 23-Feb-2016

23 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

Compilation 0368 -3133. Lecture 1: Introduction Noam Rinetzky. Admin. Lecturer: Noam Rinetzky maon@tau.ac.il http://www.cs.tau.ac.il/~maon T.A.: Shachar Itzhaky shachar@tau.ac.il Textbooks: Modern Compiler Design Compilers: principles, techniques and tools. Admin. - PowerPoint PPT Presentation

TRANSCRIPT

Parametric Shape Analysis via 3-Valued Logic

Compilation 0368-3133Lecture 1: Introduction

Noam Rinetzky1AdminLecturer: Noam Rinetzkymaon@tau.ac.ilhttp://www.cs.tau.ac.il/~maon

T.A.: Shachar Itzhakyshachar@tau.ac.il

Textbooks: Modern Compiler Design Compilers: principles, techniques and tools

2AdminCompiler Project 40%4 practical exercisesGroups of 3

1 theoretical exercise 10%Groups of 1

Final exam 50% must pass

3Course GoalsWhat is a compilerHow does it work(Reusable) techniques & tools4Course GoalsWhat is a compilerHow does it work(Reusable) techniques & tools

Programming language implementationruntime systems Execution environmentsAssembly, linkers, loaders, OS 5 5What is a Compiler?A compiler is a computer program that transforms source code written in a programming language (source language) into another language (target language). The most common reason for wanting to transform source code is to create an executable program.

--Wikipedia67What is a Compiler?source languagetarget languageCompiler

Executable codeexeSourcetext txt 78What is a Compiler?Executable codeexeSourcetext txtCompilerint a, b;a = 2;b = a*2 + 1;MOV R1,2SAL R1INC R1MOV R2,R19What is a Compiler?source languagetarget languageCC++PascalJava

PostscriptTeX

PerlJavaScriptPythonRuby

Prolog

LispSchemeMLOCamlIA32IA64SPARC

CC++PascalJava

Java BytecodeCompilerLecture OutlineHigh level programming languagesInterpreters vs. CompilersTechniques and tools (1.1)why study compilers Handwritten toy compiler & interpreter (1.2)Summary10High Level Programming LanguagesImperative Algol, PL1, Fortran, Pascal, Ada, Modula, CClosely related to von Neumann ComputersObject-oriented Simula, Smalltalk, Modula3, C++, Java, C#, Python Data abstraction and evolutionary form of program developmentClass an implementation of an abstract data type (data+code)Objects Instances of a classInheritance + genericsFunctional Lisp, Scheme, ML, Miranda, Hope, Haskel, OCaml, F# Logic Programming Prolog

11Other LanguagesHardware description languages VHDL The program describes Hardware componentsThe compiler generates hardware layoutsScripting languages Shell, C-shell, REXX, Perl Include primitives constructs from the current software environmentWeb/Internet HTML, Telescript, JAVA, Javascript Graphics and Text processing TeX, LaTeX, postscriptThe compiler generates page layoutsIntermediate-languages P-Code, Java bytecode, IDL 12InterpreterA program which executes a programInput a program (P) + its input (x)Output the computed output (P(x))13InterpreterSourcetext txtInputOutputExecutionEngineInterpreterA program which executes a programInput a program (P) + its input (x)Output the computed output (P(x))14Interpreter5ExecutionEngineint x;scanf(%d, &x);x = x + 1 ;printf(%d, x);6CompilerA program which transforms programs Input a program (P)Output an object program (O)For any x, O(x) = P(x))15CompilerSourcetext txtBackend(synthesis)Executable codeexePO15Example16CompilerBackend(synthesis)int x;scanf(%d, &x);x = x + 1 ;printf(%d, x);add %fp,-8, %l1mov %l1, %o1call scanfld [%fp-8],%l0add %l0,1,%l0st %l0,[%fp-8]ld [%fp-8], %l1mov %l1, %o1call printf 5616Compiler vs. Interpreter17Executable codeexeSourcetext txtBackend(synthesis)Sourcetext txtInputOutputExecutionEngine5OutputRemarksBoth compilers and interpreters are programs written in high level languagesRequires additional step to compile the compiler/interpreter

Compilers and interpreters share functionality1818How to write a compiler?19L1 CompilerExecutable compilerexeL2 Compiler source txtL119How to write a compiler?20L1 CompilerExecutable compilerexeL2 Compiler source txtL1L2 CompilerExecutable programexeProgram source txtL2=20How to write a compiler?21L1 CompilerExecutable compilerexeL2 Compiler source txtL1L2 CompilerExecutable programexeProgram source txtL2=21ProgramOutputYInput X=21Bootstrapping a compiler 22L1 Compilersimple L2 executable compilerexeSimpleL2 compiler source txtL1L2s CompilerInefficient adv. L2 executable compilerexeadvancedL2 compiler source txtL2L2 CompilerEfficient adv. L2 executable compilerYadvancedL2 compiler source X==22Conceptual structure of a compiler23Executable codeexeSourcetext txtSemanticRepresentationBackend(synthesis)CompilerFrontend(analysis)23Interpreter vs. CompilerConceptually simpler define the prog. lang. Can provide more specific error reportEasier to port

Faster response time

[More secure]How do we know the translation is correct?Can report errors before input is givenMore efficient codeCompilation can be expensive move computations to compile-timecompile-time + execution-time < interpretation-time is possible2424Interpreters report input-specific definite errorsInput-program

Input data y = -1y = 0

25scanf(%d, &y);if (y < 0)x = 5;...If (y left)) { Error("Missing expression"); } if (!Parse_operator(&expr->oper)) { Error("Missing operator"); } if (!Parse_expression(&expr->right)) { Error("Missing expression"); } if (Token.class != ')') { Error("Missing )"); } get_next_token(); return 1; } /* failed on both attempts */ free_expression(expr); return 0;}Parser61static int Parse_operator(Operator *oper) { if (Token.class == '+') { *oper = '+'; get_next_token(); return 1; } if (Token.class == '*') { *oper = '*'; get_next_token(); return 1; } return 0;}

61AST for (2 * ((3*4)+9))62P*opertypeleftrightP+P*D2D9D4D362Semantic Analysis Trivial in our caseNo identifiersNo procedure / functionsA single type for all expressions6363Code generationStack based machineFour instructionsPUSH nADDMULTPRINT6464Code generation65#include "parser.h" #include "backend.h" static void Code_gen_expression(Expression *expr) { switch (expr->type) { case 'D': printf("PUSH %d\n", expr->value); break; case 'P': Code_gen_expression(expr->left); Code_gen_expression(expr->right); switch (expr->oper) { case '+': printf("ADD\n"); break; case '*': printf("MULT\n"); break; } break; }}void Process(AST_node *icode) { Code_gen_expression(icode); printf("PRINT\n");}65Compiling (2*((3*4)+9))66PUSH 2PUSH 3PUSH 4MULTPUSH 9ADDMULTPRINTP*opertypeleftrightP+P*D2D9D4D3Generated Code Execution67PUSH 2PUSH 3PUSH 4MULTPUSH 9ADDMULTPRINTStackStack2Generated Code Execution68PUSH 2PUSH 3PUSH 4MULTPUSH 9ADDMULTPRINTStack32Stack2Generated Code Execution69PUSH 2PUSH 3PUSH 4MULTPUSH 9ADDMULTPRINTStack432Stack32Generated Code Execution70PUSH 2PUSH 3PUSH 4MULTPUSH 9ADDMULTPRINTStack122Stack432Generated Code Execution71PUSH 2PUSH 3PUSH 4MULTPUSH 9ADDMULTPRINTStack9122Stack122Generated Code Execution72PUSH 2PUSH 3PUSH 4MULTPUSH 9ADDMULTPRINTStack212Stack9122Generated Code Execution73PUSH 2PUSH 3PUSH 4MULTPUSH 9ADDMULTPRINTStack42Stack212Generated Code Execution74PUSH 2PUSH 3PUSH 4MULTPUSH 9ADDMULTPRINTStackStack42InterpretationBottom-up evaluation of expressionsThe same interface of the compiler75#include "parser.h" #include "backend.h

static int Interpret_expression(Expression *expr) { switch (expr->type) { case 'D': return expr->value; break; case 'P': int e_left = Interpret_expression(expr->left); int e_right = Interpret_expression(expr->right); switch (expr->oper) { case '+': return e_left + e_right; case '*': return e_left * e_right; break; }}

void Process(AST_node *icode) { printf("%d\n", Interpret_expression(icode));}76Interpreting (2*((3*4)+9))77P*opertypeleftrightP+P*D2D9D4D377Lecture OutlineHigh level programming languagesInterpreters vs. CompilersTechniques and tools (1.1)why study compilers Handwritten toy compiler & interpreter (1.2)Summary78Summary: Journey inside a compiler79LexicalAnalysisSyntax AnalysisSem.AnalysisInter.Rep.Code Gen.x = b*b 4*a*c

txt TokenStream80LexicalAnalysisSyntax AnalysisSem.AnalysisInter.Rep.Code Gen. b4bacIDIDIDIDIDfactortermfactorMULTtermexpressionexpressionfactortermfactorMULTtermexpressiontermMULTfactorMINUSSyntaxTreeSummary: Journey inside a compiler8081Sem.AnalysisInter.Rep.Code Gen.b4bacMULTMULTMULTMINUSLexicalAnalysisSyntax AnalysisAbstractSyntaxTreeSummary: Journey inside a compiler82LexicalAnalysisSyntax AnalysisSem.AnalysisInter.Rep.Code Gen.b4bacMULTMULTMULTMINUStype: intloc: sp+8type: intloc: consttype: intloc: sp+16type: intloc: sp+16type: intloc: sp+24type: intloc: R2type: intloc: R2type: intloc: R1type: intloc: R1AnnotatedAbstractSyntaxTreeSummary: Journey inside a compilerJourney inside a compiler83LexicalAnalysisSyntax AnalysisSem.AnalysisInter.Rep.Code Gen.b4bacMULTMULTMULTMINUStype: intloc: sp+8type: intloc: consttype: intloc: sp+16type: intloc: sp+16type: intloc: sp+24type: intloc: R2type: intloc: R2type: intloc: R1type: intloc: R1R2 = 4*aR1=b*bR2= R2*cR1=R1-R2IntermediateRepresentationJourney inside a compiler84Inter.Rep.Code Gen.b4bacMULTMULTMULTMINUStype: intloc: sp+8type: intloc: consttype: intloc: sp+16type: intloc: sp+16type: intloc: sp+24type: intloc: R2type: intloc: R2type: intloc: R1type: intloc: R1R2 = 4*aR1=b*bR2= R2*cR1=R1-R2MOV R2,(sp+8)SAL R2,2MOV R1,(sp+16)MUL R1,(sp+16)MUL R2,(sp+24)SUB R1,R2LexicalAnalysisSyntax AnalysisSem.AnalysisIntermediateRepresentationAssemblyCodeError CheckingIn every stage

Lexical analysis: illegal tokensSyntax analysis: illegal syntax Semantic analysis: incompatible types, undefined variables,

Every phase tries to recover and proceed with compilation (why?)Divergence is a challenge

85The Real Anatomy of a Compiler86Executable codeexeSourcetext txtLexicalAnalysisSem.AnalysisProcess text inputcharactersSyntaxAnalysistokensASTIntermediate code generationAnnotated A

Recommended

View more >