How to make the computer understand? Lecture 15: Putting it all together. Example (Output assembly code) Example (input program) Anatomy of a Computer



Similar documents
Language Processing Systems

Introduction. Compiler Design CSE 504. Overview. Programming problems are easier to solve in high-level languages

Compiler I: Syntax Analysis Human Thought

Programming Languages

CSCI 3136 Principles of Programming Languages

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

Scanning and parsing. Topics. Announcements Pick a partner by Monday Makeup lecture will be on Monday August 29th at 3pm

Compilers. Introduction to Compilers. Lecture 1. Spring term. Mick O Donnell: michael.odonnell@uam.es Alfonso Ortega: alfonso.ortega@uam.

Compiler Construction

Intel Assembler. Project administration. Non-standard project. Project administration: Repository

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Lecture 27 C and Assembly

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

1/20/2016 INTRODUCTION

CA Compiler Construction

Compiler Construction

Programming Language Pragmatics

Lecture 1: Introduction

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Division of Mathematical Sciences

Syntaktická analýza. Ján Šturc. Zima 208

03 - Lexical Analysis

n Introduction n Art of programming language design n Programming language spectrum n Why study programming languages? n Overview of compilation

Approximating Context-Free Grammars for Parsing and Verification

Programming Languages

Programming Assignment II Due Date: See online CISC 672 schedule Individual Assignment

Artificial Intelligence. Class: 3 rd

If-Then-Else Problem (a motivating example for LR grammars)

CIS570 Modern Programming Language Implementation. Office hours: TDB 605 Levine

Automated Repair of Binary and Assembly Programs for Cooperating Embedded Devices

Programming Languages

Pushdown automata. Informatics 2A: Lecture 9. Alex Simpson. 3 October, School of Informatics University of Edinburgh als@inf.ed.ac.

Module: Software Instruction Scheduling Part I

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. CSC467 Compilers and Interpreters Fall Semester, 2005

CS143 Handout 08 Summer 2008 July 02, 2007 Bottom-Up Parsing

COMPUTER SCIENCE. 1. Computer Fundamentals and Applications

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

2) Write in detail the issues in the design of code generator.

Compiler and Language Processing Tools

Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages

Scanner. tokens scanner parser IR. source code. errors

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Lecture 9. Semantic Analysis Scoping and Symbol Table

Semantic Analysis: Types and Type Checking

Programming Project 1: Lexical Analyzer (Scanner)

Object-Oriented Software Specification in Programming Language Design and Implementation

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Computer Science. Master of Science

Programming Language Concepts for Software Developers

Eastern Washington University Department of Computer Science. Questionnaire for Prospective Masters in Computer Science Students

2. Advance Certificate Course in Information Technology

Acknowledgements. In the name of Allah, the Merciful, the Compassionate

How Compilers Work. by Walter Bright. Digital Mars

Memory Systems. Static Random Access Memory (SRAM) Cell

Jonathan Worthington Scarborough Linux User Group

C Compiler Targeting the Java Virtual Machine

Basics of Compiler Design

DEGREE PLAN INSTRUCTIONS FOR COMPUTER ENGINEERING

Lexical analysis FORMAL LANGUAGES AND COMPILERS. Floriano Scioscia. Formal Languages and Compilers A.Y. 2015/2016

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Computer Programming. Course Details An Introduction to Computational Tools. Prof. Mauro Gaspari:

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Query optimization. DBMS Architecture. Query optimizer. Query optimizer.

Voice Driven Animation System

Assessment for Master s Degree Program Fall Spring 2011 Computer Science Dept. Texas A&M University - Commerce

Programming Languages & Tools

Theory of Compilation

Programming Languages CIS 443

Application Architectures

A Programming Language Where the Syntax and Semantics Are Mutable at Runtime

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

02 B The Java Virtual Machine

The CompCert verified C compiler

Optimizing compilers. CS Modern Compilers: Theory and Practise. Optimization. Compiler structure. Overview of different optimizations

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Natural Language to Relational Query by Using Parsing Compiler

Levels of Programming Languages. Gerald Penn CSC 324

Design Patterns in Parsing

CHAPTER 5 INTELLIGENT TECHNIQUES TO PREVENT SQL INJECTION ATTACKS

AQA GCSE in Computer Science Computer Science Microsoft IT Academy Mapping

CS5310 Algorithms 3 credit hours 2 hours lecture and 2 hours recitation every week

Bottom-Up Syntax Analysis LR - metódy

X86-64 Architecture Guide

Eastern Washington University Department of Computer Science. Questionnaire for Prospective Masters in Computer Science Students

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar

Semester Review. CSC 301, Fall 2015

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Technical paper review. Program visualization and explanation for novice C programmers by Matthew Heinsen Egan and Chris McDonald.

Systematically Enhancing Black-Box Web Vulnerability Scanners

Assessment Plan for CS and CIS Degree Programs Computer Science Dept. Texas A&M University - Commerce

Computer System: User s View. Computer System Components: High Level View. Input. Output. Computer. Computer System: Motherboard Level

Compiler Design July 2004

Principles of Programming Languages Topic: Introduction Professor Louis Steinberg

International Journal of Scientific & Engineering Research, Volume 4, Issue 11, November ISSN

Transcription:

How to make the computer understand? Fall 2005 Lecture 15: Putting it all together From parsing to code generation Write a program using a programming language Microprocessors talk in assembly language Program written in a Programming Languages Compiler Assembly Language Translation Saman Amarasinghe 2 6.035 MIT Fall 1998 Example (input program) int expr(int n) { int d; d = 4 * n * n * (n + 1) * (n + 1); return d; } Example (Output assembly code) Unoptimized Code Optimized Code expr: expr:.lfb2:.lfb2: pushq %rbp movl %edi, %eax.lcfi0: imull %edi, %eax movq %rsp, %rbp incl %edi.lcfi1: imull %edi, %eax movl %edi, -4(%rbp) imull %edi, %eax sall $2, %eax movl -4(%rbp), %eax ret movl %eax, %edx imull -4(%rbp), %edx movl -4(%rbp), %eax incl %eax imull %eax, %edx movl -4(%rbp), %eax incl %eax imull %edx, %eax sall $2, %eax movl %eax, -8(%rbp) movl -8(%rbp), %eax leave ret Saman Amarasinghe 3 6.035 MIT Fall 1998 Saman Amarasinghe 4 6.035 MIT Fall 1998 Anatomy of a Computer Program (character stream) Lexical Analyzer (Scanner) Token Stream Syntax Analyzer (Parser) Parse Tree Semantic Analyzer Intermediate Representation Code Optimizer Optimized Intermediate Representation Code Generator Assembly code Lexical Analysis Lexical analyzer create tokens out of a text stream Tokens are defined using regular expressions Saman Amarasinghe 5 6.035 MIT Fall 1998 Saman Amarasinghe 6 6.035 MIT Fall 1998 1

Examples of Regular Expressions Regular Expression a Strings matched a a b ab a b a b ε a* a aa aaa (a ε) b ab b num = 0 1 2 3 4 5 6 7 8 9 0 1 2 3 posint = num num* 8 6035 int = (ε -) posint -42 1024 real = int (ε (. posint)) -12.56 12 1.414... Saman Amarasinghe 7 6.035 MIT Fall 1998 Lexical Analysis Lexical analyzer create tokens out of a text stream Tokens are defined using regular expressions Regular expressions can be mapped to Nondeterministic Finite Automatons (NFA) by simple construction NFA is transformed to a DFA Transformation algorithm Executing a DFA is straightforward Saman Amarasinghe 8 6.035 MIT Fall 1998 Syntax Analysis (parsing) Defining a language using context-free grammars Example: A CFG for expressions <expr> <expr> <op> <expr> <expr> ( <expr> ) <expr> - <expr> <expr> num <op> + <op> * Saman Amarasinghe 9 6.035 MIT Fall 1998 Saman Amarasinghe 10 6.035 MIT Fall 1998 Parse Tree Example <expr> <expr> <op> <expr> num * ( num * ( num + num ) <expr> <expr> <op> <expr> ) Syntax Analysis (parsing) Defining a language using context-free grammars Classification of Grammars Context free unambiguous LR(k) LR(1) LALR(1) SLR(1) LR(0) regular num + num Saman Amarasinghe 11 6.035 MIT Fall 1998 Saman Amarasinghe 12 6.035 MIT Fall 1998 2

LR(k) Parser Engine ACTION Goto State ( ) $ X s0 shift to s2 goto s1 s1 accept s2 shift to s2 shift to s5 goto s3 s3 shift to s4 s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3) ACTION Goto State ( ) $ X Y s0 shi ft to s1 reduce (5) reduce (5) goto s5 goto s6 s1 shi ft to s2 reduce (5) reduce( 3 or 5 ) goto s3 s2 shi ft to s2 reduce (5) reduce (5) goto s3 s3 shift to s4 s4 reduce (4) reduce (4) s5 s6 accept reduce (2) 31 State Stack Symbol Stack Figure by MIT OCW. Current Symbol Parser Action Saman Amarasinghe 13 6.035 MIT Fall 1998 s0 s2 <S> <X> $ ( <Y> ( <Y> ) <X> Y s1 <Y> ( <Y> ) <X> ( ( <X> ( <Y> <Y> ( <Y> ) <Y> ( <Y> ) Y <Y> <Y> ( <Y> ) s3 Y <Y> X Y <Y> ( <Y> ) ) s5 s6 s4 <S> <X> $ <X> <Y> <Y> ( <Y> ) Saman Amarasinghe 14 6.035 MIT Fall 1998 ( Semantic Analysis Building a symbol table Static Checking Flow-of-control checks Uniqueness checks Type checking Dynamic Checking Array bounds check Null pointer dereference check Translation to Intermediate Format Goal: Remain Largely Machine Independent But Move Closer to Standard Machine Model From high-level IR to a low-level IR Eliminate Structured Flow of Control Convert to a Flat Address Space Saman Amarasinghe 15 6.035 MIT Fall 1998 Saman Amarasinghe 16 6.035 MIT Fall 1998 Code Optimizations Generate code as good as hand-crafted by a good assembly programmer Have stable, robust performance Abstract the architecture away from the programmer Exploit architectural strengths Hide architectural weaknesses Code Optimizations Algebraic simplification Common subexpression elimination Copy propagation Constant propagation Dead-code elimination Register allocation Instruction Scheduling Saman Amarasinghe 17 6.035 MIT Fall 1998 Saman Amarasinghe 18 6.035 MIT Fall 1998 3

Compiler Project! You guys build a full-blown compiler from the ground up!!! From decaf to working code Compiler Derby Who has the fastest compiler in the east??? Will give you the program 12 hours in advance Test and make all the optimizations work DO NOT ADD PROGRAM SPECIFIC HACKS! Wednesday, December 14 th at 11:00AM location TBA refreshments provided Saman Amarasinghe 19 6.035 MIT Fall 1998 Saman Amarasinghe 20 6.035 MIT Fall 1998 How will you use 6.035 knowledge? As an informed programmer As a designer of simple languages to aid other programming tasks As an engineer dealing with new computer architectures As a true compiler hacker 1. Informed Programmer Now you know what the compiler is doing don t treat it as a black box don t trust it to do the right thing! Implications performance debugging correctness Saman Amarasinghe 21 6.035 MIT Fall 1998 Saman Amarasinghe 22 6.035 MIT Fall 1998 1. Informed Programmer What did you learned in 6.035? How optimizations work or why they did not work How to read and understand optimized code 2. Language Extensions In many applications and systems, you may need to: implement a simple language handle input define an interface command and control extend a language add new functionality modify semantics help with optimizations 25 Saman Amarasinghe 23 6.035 MIT Fall 1998 Saman Amarasinghe 24 6.035 MIT Fall 1998 4

2. Language Extensions What you learned in 6.035 define tokens and languages using regular expressions and CFGs use tools such as jlex, lex, javacup, yacc build intermediate representations perform simple transformations on the IR 3. Computer Architectures Many special purpose processors in your cell phone, car engine, watch, etc. etc. Designing new architectures Adapting compiler back-ends for new architectures 29 Saman Amarasinghe 25 6.035 MIT Fall 1998 Saman Amarasinghe 26 6.035 MIT Fall 1998 3. Designing New Architectures Great advances in VLSI technology very fast and small transistors scaling up to billion transistors but, slow and limited wires and I/O A computer architecture is a combination of hardware and compiler need to know what a compiler can do and what hardware need to do If compiler can do it don t waste hardware resources. Saman Amarasinghe 27 6.035 MIT Fall 1998 3. Designing New Architectures What did you learned in 6.035 Capabilities of a compiler: what is simple and what is hard to do How to think like a compiler writer Saman Amarasinghe 28 6.035 MIT Fall 1998 3. Back-end support 3. Back-end support Every new architecture need a new back Instruction scheduling end Even if the ISA is the same, different resource constrains How to handle new features What do you learned in 6.035 Intermediate representations Transforming/optimizing the IR Process of generating assembly from a high-level IR Assembly interface issues (eg: calling conventions) Register allocation issues Code scheduling issues Saman Amarasinghe 29 6.035 MIT Fall 1998 Saman Amarasinghe 30 6.035 MIT Fall 1998 5

36 36 Theory Algorithms Implementation Theory: Develop general, abstract concepts Prove correctness, optimality etc. Examples parse theory lattices and data-flow abstract interpretation The language ML Saman Amarasinghe 31 6.035 MIT Fall 1998 Saman Amarasinghe 32 6.035 MIT Fall 1998 Algorithms: Design a solution to a given problem (Mostly new optimizations) Use many techniques such as graph theory, number theory, etc. May have to limit the scope and find good heuristics Examples partial redundancy elimination register allocation by graph coloring using multi-granular (MMX) operations Implementation: Develop a new compiler Issues of designing a very complex software Putting theory and algorithms into practice Examples A JIT for Java A query optimization engine for SQL A rasterizer for postscript Saman Amarasinghe 33 6.035 MIT Fall 1998 Saman Amarasinghe 34 6.035 MIT Fall 1998 Where to Look for Current Research? What did you learned in 6.035? PLDI Programming Languages Design and Implementation Conference Code Generation / Machine specific Micro Conference ASPLOS Architecture Support for Programming Languages and Operating Systems CGO Code Generation and Optimization Language Theory POPL Principles of Programming Languages OOPSLA Object Oriented Programming Systems Languages and Applications Program Analysis SAS Static Analysis Symposium PPoPP Principles and Practice of Parallel Programming Saman Amarasinghe 35 6.035 MIT Fall 1998 Saman Amarasinghe 36 6.035 MIT Fall 1998 6