COMP431 Compilers by Dr Alex Potanin - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

COMP431 Compilers by Dr Alex Potanin

Description:

COMP462 prepares expert OO designers. COMP431 prepares expert Java/C#/etc. ... that program (usually involves executing the source program in some fashion) ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 26
Provided by: mcsV
Category:

less

Transcript and Presenter's Notes

Title: COMP431 Compilers by Dr Alex Potanin


1
Introduction
  • COMP431 (Compilers) by Dr Alex Potanin

2
Why do this course?
  • COMP462 prepares expert OO designers
  • COMP431 prepares expert Java/C/etc. programmers
  • COMP431 is also essential for anyone planning to
    do research in programming languages
  • COMP431 is the only course here that will teach
    you about bytecode
  • If you do well in this course and like the
    material, there is a cool Wellington-based
    company that cannot wait to employ you ? (they
    will give us a guest lecture or two towards the
    end of the course)

3
People Involved
  • Dr Alex Potanin (lectures)
  • alex_at_mcs.vuw.ac.nz
  • CO230
  • 463 5302
  • Dr David Pearce (projects)
  • djp_at_mcs.vuw.ac.nz
  • CO231
  • 463 5833
  • A/Prof Lindsay Groves (ideas ?)
  • lindsay_at_mcs.vuw.ac.nz

4
Course Requirements
  • Project 1 (20) (involves milestones, due before
    mid trimester break)
  • Project 2 (20) (involves milestones, due before
    the end of the trimester)
  • Exam (60)
  • Mandatory Requirement At least 40 in each
    project and the exam.
  • 3 lectures per week or 2 lectures and a
    (substantial) reading per week

5
Recommended Book and Lectures
  • Recommended Book (Andrew W. Appel Modern Compiler
    Implementation in Java 2nd Edition) is on Closed
    Reserve and a Three Day Loan
  • Two Lectures (Parsing 1 and 2) are based on CS502
    course taught by Dr Tony Hosking at Purdue
    University
  • The majority of others are developed as part of
    either COMP471 (2007) or COMP463 (2006, 2005) by
    Dr David Pearce here at VUW

6
What is a Compiler?
  • Compiler translates an executable program in one
    language into an executable program in another
    language
  • We expect the program produced by the compiler to
    be better, in some way, than the original
  • An alternative to a compiler is an interpreter
  • Interpreter is a program that reads an executable
    program and produces the results of running that
    program (usually involves executing the source
    program in some fashion)
  • This course deals mainly with compilers, but many
    of the same issues arise in interpreters

7
Why study compilers?
  • Compiler construction is a microcosm of computer
    science
  • Artificial Intelligence (greedy algorithms,
    learning algorithms)
  • Algorithms (graph algorithms, union-find, dynamic
    programming)
  • Theory (automata for scanning, parser generators)
  • Systems (allocation and naming, locality,
    synchronisation)
  • Architecture (pipelines, instruction sets)
  • Inside a compiler, all these things come together

8
Isnt it a solved problem?
  • Machines are constantly changing
  • Changes in architecture gt changes in compilers
  • New features pose new problems
  • Changing costs lead to different concerns
  • Old solutions need re-engineering
  • Changes in compilers should prompt changes in
    architecture
  • New languages and features

9
Intrinsic Merit
  • Compiler construction is challenging and fun
  • Interesting problems
  • Primary responsibility (and blame) for
    performance
  • New architectures gt new challenges
  • Real results
  • Extremely complex interactions
  • Compilers have an impact on how computers are
    used
  • Some of the most interesting problems in
    computing

10
Experience
  • You have used several compilers
  • What qualities are important in a compiler?
  • Correct code
  • Output runs fast
  • Compiler runs fast
  • Compile time proportional to program size
  • Support for separate compilation
  • Good diagnostics for syntax errors
  • Works well with the debugger
  • Good diagnostics for flow anomalies
  • Consistent, predictable optimisation
  • Each of these shapes your expectations about this
    course

11
Abstract View
compiler
source code
machine code or bytecode
errors
  • Recognise legal (and illegal) programs
  • Generate correct code
  • Manage storage of all variables and code
  • Agreement on format for object (or assembly) code
  • Big step up from assembler higher level
    notations
  • GO AND TRY javap on your own class files!!!

12
Traditional two pass compiler
IR
front end
source code
machine code
back end
errors
  • Intermediate representation (IR)
  • Front end maps legal code into IR
  • Back end maps IR onto target machine
  • Simplifies retargeting
  • Allows multiple front ends
  • Multiple passes gt better code
  • Front end is O(n) or O(n logn). Back end is
    NP-complete

13
Multi Pass Compiler
source code
machine code
IR
IR
front end
middle bit
back end
errors
  • Code Improvement
  • Analyses and changes IR
  • Goal is to reduce runtime
  • Must preserve values

14
Front End
tokens
scanner
source code
parser
IR
errors
  • Recognise legal procedure
  • Report errors
  • Produce IR
  • Preliminary storage map
  • Shape the code for the back end
  • Much of front end construction can be automated,
    which is exactly what we will do with ANTLR

15
Lecture 2
  • Begins here

16
Front End (Scanner)
tokens
scanner
source code
parser
IR
errors
  • Maps characters into tokens the basic unit of
    syntax
  • x x y becomes idx idx idy
  • Typical tokens number, id, , -, , /, do, end
  • Eliminates white space (tabs, blanks, comments)
  • A key issue is speed gt sometimes (e.g. in javac)
    use specialised recogniser rather than an
    automatically generated one

17
Front End (Parser)
tokens
scanner
source code
parser
IR
errors
  • Recognise context-free syntax
  • Guide context-sensitive analysis
  • Construct IR
  • Produce meaningful error messages
  • Attempt error correction
  • Parser generators mechanise much of the work
    (e.g. ANTLR)

18
Back End
instruction selection
register allocation
machine code
IR
errors
  • Translate IR into target machine code
  • Choose instructions for each IR operation
  • Decide what to keep in registers at each point
  • Ensure conformance with system interfaces
  • Automation has been less successful here

19
Back End (Instruction Selection)
instruction selection
register allocation
machine code
IR
errors
  • Produce compact, fast code
  • Use available addressing modes
  • Pattern matching problem
  • Ad hoc techniques
  • Tree pattern or string pattern matching
  • Dynamic programming

20
Back End (Register Allocation)
instruction selection
register allocation
machine code
IR
errors
  • Have value in a register when used
  • Limited resources
  • Changes instruction choices
  • Can move loads and stores
  • Optimal allocation is hard (NP-complete)
  • Modern allocators often use an analogy to graph
    colouring

21
Optimiser (Middle Bit)
IR
IR
Opt 1
opt n
IR
IR

errors
  • Modern optimisers are usually built as a set of
    passes, e.g.
  • Constant propagation and folding
  • Code motion
  • Reduction of operator strength
  • Common subexpression elimination
  • Redundant store or dead code elimination

22
Generic Compiler Stages
Grammar
Type System
Parser
Type Checker
Flow Checker
Code Generator
Java Source
AST
Intermediate Language
Bytecode
  • Optimisers include SSA, instruction reordering
  • Additional plug-ins include JQL, Type Inference
  • Code Generator includes register allocation

Optimisers
Weeks 1 2
3 4
9 12
5 8
23
JKit Compiler Stages
24
What will you learn in this course?
  • Introduction (today)
  • Parsing (5 lectures)
  • Type Checking (6 lectures)
  • Intermediate Language (3 lectures)
  • Code Generation (2 lectures)
  • Optimisation (1 overview of techniques lecture)
  • Register Allocation (3 lectures)
  • Lattices and Dataflow Analyses (2 lectures)
  • Instruction Reordering (2 lectures)
  • SSA (2 lectures)
  • Guest Lecture by Innaworks Ltd and possibly more

25
Things To Do
  • Make sure your graduate MSCS account works in the
    graduate labs (e.g. Memphis)
  • Make sure you find the text book in the library
    (Andrew W. Appel Modern Compiler Implementation
    in Java 2nd Edition) you dont need to buy it,
    but you will need it to survive ?
  • Find the course web page http//www.mcs.vuw.ac.nz
    /courses/COMP431/
  • Try to find out about ANTLR and JKit (used for
    the course projects)
  • Make sure you remember how to program in Java
    (for example using Eclipse)
Write a Comment
User Comments (0)
About PowerShow.com