01 – Overview of Compilers

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

1 Languages and Compilers (SProg og Oversættere) Code Generation.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Recap Mooly Sagiv. Outline Subjects Studied Questions & Answers.
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
Compiler Summary Mooly Sagiv html://
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
COP4020 Programming Languages
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
LANGUAGE TRANSLATORS: WEEK 24 TRANSLATION TO ‘INTERMEDIATE’ CODE (overview) Labs this week: Tutorial Exercises on Code Generation.
The TINY sample language and it’s compiler
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Introduction CPSC 388 Ellen Walker Hiram College.
Intermediate Code Representations
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
©SoftMoore ConsultingSlide 1 Context-Free Grammars.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
©SoftMoore ConsultingSlide 1 The CPRL Virtual Machine.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Open Source Compiler Construction (for the JVM)
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lecture 3 Translation.
Teaching Compiler Design
Syntax Analysis (a.k.a. Parsing)
Compiler Design (40-414) Main Text Book:
Introduction Chapter : Introduction.
Overview of Compilers and Language Translation
PRINCIPLES OF COMPILER DESIGN
Chapter 1 Introduction.
Introduction to Compiler Construction
Constructing Precedence Table
Compiler Construction (CS-636)
Introduction.
Compiler Chapter 9. Intermediate Languages
Overview of Compilation The Compiler BACK End
Chapter 1 Introduction.
PROGRAMMING LANGUAGES
-by Nisarg Vasavada (Compiled*)
课程名 编译原理 Compiling Techniques
Optimization Code Optimization ©SoftMoore Consulting.
Chapter 1: Introduction to Compiling (Cont.)
Compiler Lecture 1 CS510.
Languages and Compilers (SProg og Oversættere)
Bison: Parser Generator
CMPE 152: Compiler Design December 5 Class Meeting
An Overview to Compiler Design
CPSC 388 – Compiler Design and Construction
Overview of Compilation The Compiler BACK End
Compilers B V Sai Aravind (11CS10008).
COP4020 Programming Languages
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Syntax-Directed Translation
System Programming and administration
Programming Languages 2nd edition Tucker and Noonan
Compiler design.
CS 153: Concepts of Compiler Design October 30 Class Meeting
CMPE 152: Compiler Design April 9 Class Meeting
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 10: Compilers and Language Translation
Introduction Chapter : Introduction.
Compiler Structures 1. Overview Objective
Presentation transcript:

01 – Overview of Compilers

Compiler Source program Object Program input Program Results Compile Execute

Interpreter Source program Program input Execute Program Results

L P M L S  T

Python sort C++ x86 Java x86 MIPS ARM Power PC Java C++  x86 x86 C++ Java  JVM

must match M P  x86 sort  SPARC

must match M S  T S P T

Compile: Execute: x86 C++  x86 C++ sort

download Power PC C++  ARM C++ sort ARM

Functionally equivalent to a C++-to-x86 compiler C++  C C++ sort C C  x86

C# C#  M M C  M Want Have

C C#/0  M M C  M Write this Compile it To get this

Write this To get this Compile using the compiler from step 1 C#/0  M

C#/0 C#  M M C#/0  M Write this Compile it To get this

Rewritten to This version is improve efficiency more efficient than C++  M M Rewritten to improve efficiency This version is more efficient than this version Compile it using existing compiler

S L Basic Java Lisp x86 JVM x86-64

Basic x86 Basic Basic x86 sort

Compile: Compiler Execute: Java Virtual Machine (JVM) Java P x86 Java  JVM JVM Java Virtual Machine (JVM) Compiler

Compiled You will write version of this compiler. your compiler CPRL  CPRLVM/A JVM Java x86 Java  JVM You will write this compiler. Compiled version of your compiler Use the Java compiler to compile your CPRL compiler.

HelloWorld in CPRL HelloWorld in CPRL assembly language JVM x86 CRPL Hello CPRL  CPRLVM/A CPRLVM/A CPRLVM/A  CPRLVM CPRLVM HelloWorld in CPRL HelloWorld in CPRL assembly language HelloWorld in CPRLVM machine language Compiled version of your compiler Provided assembler

CPRLVM JVM x86 Provided emulator Hello

02 – Structure of Compilers

Front End Back End Lexical Analyzer (a.k.a. Scanner) Syntax Analyzer (a.k.a. Parser) Constraint Analyzer Code Generator Optimizer Identifier Table Error Handler Source Program Object Program Front End Back End

Scanner y := x + 1 id [“y”, (1, 1)] := [(1, 3)] id [“x”, (1, 6)] := [(1, 3)] id [“x”, (1, 6)] + [(1, 8)] literal [(“1”, (1, 10)]

Parser := id [“y”, (1, 1)] := [(1, 3)] id [“x”, (1, 6)] + [(1, 8)] := [(1, 3)] id [“x”, (1, 6)] + [(1, 8)] literal [(“1”, (1, 10)] idy idx + 1

Code Generator := idy idx inc LDLADDR 16 LDLADDR 12 LOADW LDCINT 1 ADD STOREW

Optimizer LDLADDR 16 LDLADDR 12 LOADW LDCINT 1 ADD STOREW INC

03 – Context-Free Grammars

All possible programs (valid and invalid) All programs with valid syntax All valid programs – valid syntax – satisfy contextual constraints

while loop expression statements end statement loopStmt ;

* x + 10 simpleExpression term addingOp factor varId constValue literal multiplyingOp * y

+ 2 * 3 4 expr op intLit multiplication has higher precedence addition has

+ 2 * 3 4 expr term intLit Note that “*” has higher precedence than “+” in this grammar.

binaryExpr expression operator

whileStmt booleanExpr statements

04 – Definition of CPRL

05 – Lexical Analysis

Scanner 'y' ' ' ':' '=' ' ' 'x' ' ' '+' ' ' '1' '0' id [“y”, (1, 1)] := [(1, 3)] id [“x”, (1, 6)] + [(1, 8)] literal [(“10”, (1, 10)] Source source code file y := x + 10

06 – Syntax Analysis

Sequence of tokens returned by the scanner Parser := id [“y”, (1, 1)] := [(1, 3)] id [“x”, (1, 6)] + [(1, 8)] literal [(“1”, (1, 10)] idy idx + 1 Sequence of tokens returned by the scanner

07 – Error Handling

08 – Abstract Syntax Trees

AssignmentStmt Variable Expression

LoopStmt Expression Statements *

BinaryExpr Expression (leftOperand) (rightOperand) Token (operator)

AST Program Declaration Statement ConstDecl VarDecl IfStmt LoopStmt Expression AddingExpr RelationalExpr Literal BinaryExpr SubprogramDecl FunctionDecl ProcedureDecl InitialDecl

Program DeclarativePart StatementPart SingleVarDecl identifier : x varType : Integer scopeLevel : PROGRAM ConstValue literal : 5 AssignmentStmt variable : expression : position : (4, 5) WritelnStmt NamedValue decl : position : (5, 11) Variable position : (4, 3)

09 – Constraint Analysis

... m n c a b BP

10 – CPRLVM

Program Code Variable Stack Free Space (Unused Memory) PC BP SP SB Low-numbered memory addresses High-numbered Stack grows in this direction

… machine instructions for the program m: relative address = 0 HALT LDLADDR m: relative address = 0 SB SP 87 88 BP 97 machine instructions for the program n: relative address = 4 c: relative address = 8 unused memory 92 96 PC 10

11 – Code Generation

12 – Optimization

peephole

13 – Subprograms

AST Declaration Statement ProcedureCallStmt ReturnStmt Expression FunctionCall SubprogramDecl FunctionDecl ProcedureDecl ParameterDecl

… no return value part -8 a parameter part -4 b relative to BP 2 return address n -8 -4 4 8 12 relative to BP parameter part context part local variable part BP SP 5 dynamic link temporary part … no return value part

memory address 0 higher numbered memory addresses SB BP runtime stack grows downward program and subprogram instructions block act. record for first call to P for second SP PC

… no return value part -8 a parameter part -4 b relative to BP 2 return address n -8 -4 4 8 12 relative to BP parameter part context part local variable part BP SP 5 dynamic link temporary part … no return value part

14 – Arrays

(references are copied) value semantics (array values are copied) reference semantics (references are copied) value semantics (array values are copied)

AST Declaration Expression Variable InitialDecl ArrayTypeDecl Type ArrayType

a + 0*4 a[0] a[1] a[2] a + 1*4 a + 2*4 …