CS412/413. Introduction to Compilers and Translators. Lecture 1: Overview 21 Jan 08

Similar documents
CA Compiler Construction

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

Language Processing Systems

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

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

Compiler I: Syntax Analysis Human Thought

Programming Languages

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

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

CSCI 3136 Principles of Programming Languages

CS 51 Intro to CS. Art Lee. September 2, 2014

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

Programming Languages

CS 40 Computing for the Web

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

Parsing Technology and its role in Legacy Modernization. A Metaware White Paper

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

An Introduction to Computer Science and Computer Organization Comp 150 Fall 2008

The programming language C. sws1 1

Division of Mathematical Sciences

Compiler Construction

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

03 - Lexical Analysis

Programming Languages

Introduction to formal semantics -

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

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

[Refer Slide Time: 05:10]

El Dorado Union High School District Educational Services

Sources: On the Web: Slides will be available on:

CS 2302 Data Structures Spring 2015

Computer Science Theory. From the course description:

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

FTP client Selection and Programming

CS 253: Intro to Systems Programming

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

1/20/2016 INTRODUCTION

Embedded Software development Process and Tools: Lesson-4 Linking and Locating Software

Administration. Instruction scheduling. Modern processors. Examples. Simplified architecture model. CS 412 Introduction to Compilers

Instruction Set Architecture (ISA)

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

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

Levels of Programming Languages. Gerald Penn CSC 324

COMPUTER SCIENCE, BACHELOR OF SCIENCE (B.S.)

Artificial Intelligence. Class: 3 rd

Special Topics in Computer Science

Semantic Analysis: Types and Type Checking

CS 3530 Operating Systems. L02 OS Intro Part 1 Dr. Ken Hoganson

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Semester Review. CSC 301, Fall 2015

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Datavetenskapligt Program (kandidat) Computer Science Programme (master)

How To Write Portable Programs In C

University of Dayton Department of Computer Science Undergraduate Programs Assessment Plan DRAFT September 14, 2011

Analytic Modeling in Python

COCOVILA Compiler-Compiler for Visual Languages

Computer and Information Sciences

Programming Language Design and Implementation. Wes Weimer MW 2:00 3:15 Thornton E-303

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

Lesson 06: Basics of Software Development (W02D2

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

CSCI E 98: Managed Environments for the Execution of Programs

CSE 307: Principles of Programming Languages

Chapter 6: Programming Languages

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

Programming Languages CIS 443

1.1 WHAT IS A PROGRAMMING LANGUAGE?

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

CS3813 Performance Monitoring Project

Image credits:

COURSE TITLE COURSE DESCRIPTION

TECH. Requirements. Why are requirements important? The Requirements Process REQUIREMENTS ELICITATION AND ANALYSIS. Requirements vs.

Undergraduate Major in Computer Science and Engineering

EE361: Digital Computer Organization Course Syllabus

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

CS422 - Programming Language Design

CHAPTER 1 ENGINEERING PROBLEM SOLVING. Copyright 2013 Pearson Education, Inc.

Software Engineering and Service Design: courses in ITMO University

Fall 2012 Q530. Programming for Cognitive Science

Introduction to programming

Architectural Design Patterns for Language Parsers

Lecture 1: Introduction

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

CSE-111 Great Ideas in Computer Science Albert Y. C. Chen University at Buffalo, SUNY

Virtual Machines as an Aid in Teaching Computer Concepts

KS3 Computing Group 1 Programme of Study hours per week

TU e. Advanced Algorithms: experimentation project. The problem: load balancing with bounded look-ahead. Input: integer m 2: number of machines

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

CS101 Lecture 26: Low Level Programming. John Magee 30 July 2013 Some material copyright Jones and Bartlett. Overview/Questions

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

C++ Programming Language

9/11/15. What is Programming? CSCI 209: Software Development. Discussion: What Is Good Software? Characteristics of Good Software?

CMSC 152: Introduction to Computer Science II

Programming Languages & Tools

How Compilers Work. by Walter Bright. Digital Mars

CSC 281 Automata and Algorithms Spring 2009

Python Programming: An Introduction to Computer Science

Embedded Software Development with MPS

Generating a Recognizer from Declarative Machine Descriptions

Transcription:

CS412/413 Introduction to Compilers and Translators Lecture 1: Overview 21 Jan 08

Outline Course Organization General course information Homework & project information Introduction to Compilers What are they? Why do we need them? What is their general structure? CS 412/413 Spring 2008 Introduction to Compilers 2

General Information When Where Instructor Teaching Assistant Course staff email Web page Newsgroup MWF 10:10-11:00AM Phillips 213 Tim Teitelbaum TBD cs412-l@cs.cornell.edu courses.cs.cornell.edu/cs412 cornell.class.412 CS 412/413 Spring 2008 Introduction to Compilers 3

Important CS 413 is required! Large implementation project Substantial amount of theory CS 412/413 Spring 2008 Introduction to Compilers 4

Textbooks Optional texts Compilers -- Principles, Techniques and Tools (Dragon Book), by Aho, Sethi and Ullman (1986) Modern Compiler Implementation in Java, by Andrew Appel (2002) Engineering a Compiler, by Linda Torczon and Keith Cooper (2003) They will be on reserve in Engineering Library CS 412/413 Spring 2008 Introduction to Compilers 5

Work Distribution Theory: Homeworks = 20% 4 homeworks: 5% each Exams = 35% 2 prelims: 17% and 18%; no final exam Practice: Programming Assignments = 45% 6 assignments: 5/9/9/9/9 Project demo CS 412/413 Spring 2008 Introduction to Compilers 6

Homeworks 4 homework assignments Three assignments in first half of course One homework in second half Not done in groups do your own work CS 412/413 Spring 2008 Introduction to Compilers 7

Project Implementation: Designed language = a subset of Java Generated code = assembly x86 Implementation language = Java 5 programming assignments Groups of 3-4 students Usually same grade for all Group information due Friday We will respect consistent preferences CS 412/413 Spring 2008 Introduction to Compilers 8

Assignments Due at beginning of class Homeworks: paper turn in (at beginning of class) Project files: electronic turn in (day before class) Assignments managed with Course Management System (CMS) Late homework, programming assignments increasingly penalized Penalty linearly increasing : 10% per day 1 day: 10%, 2 days: 20%, 3 days: 30%, etc. CS 412/413 Spring 2008 Introduction to Compilers 9

Why Take This Course? CS412/413 is an elective course Reason #1: understand compilers/languages Understand code structure Understand language semantics Understand relation between source code and generated machine code Become a better programmer CS 412/413 Spring 2008 Introduction to Compilers 10

Why Take This Course? (ctd.) Reason #2: nice balance of theory and practice: Theory: Lots of mathematical models: regular expressions, automata, grammars, graphs, lattices Lots of algorithms that use these models Practice: Apply theoretical notions to build a real compiler Better understand why theory and practice are the same in theory, but different in practice CS 412/413 Spring 2008 Introduction to Compilers 11

Why Take This Course? (ctd.) Reason #3: Programming experience Write a large program that manipulates complex data structures Learn how to be a better programmer in groups Learn more about Java and Intel x86 architecture and assembly language CS 412/413 Spring 2008 Introduction to Compilers 12

Why Take This Course? (ctd.) Reason #4: Technical background for emerging field of software assurance Software assurance will be major priority of coming decade Bug-finding and security-violation finding tools build on compiler techniques CS 412/413 Spring 2008 Introduction to Compilers 13

What Are Compilers? Compilers translate information from one representation to another. Most commonly, the information is a program Typically Compilers translate from high-level source code to low-level code (e.g., object code) Translators transform representations at the same level of abstraction CS 412/413 Spring 2008 Introduction to Compilers 14

Examples Typical compilers: gcc, javac Non-typical compilers: latex (document compiler) : Transforms a LaTeX document into DVI printing commands Input information: document (not program) C-to-Hardware compiler: Generates hardware circuits for C programs Output is lower-level than typical compilers Translators: f2c : Fortran-to-C translator (both high-level) latex2html : LaTeX-to-HTML (both documents) dvi2ps: DVI-to-PostScript (both low-level) CS 412/413 Spring 2008 Introduction to Compilers 15

In This Class We will study typical compilation: from programs written in high-level languages to low-level object code and machine code Most of the principles and techniques in this course apply to non-typical compilers and translators CS 412/413 Spring 2008 Introduction to Compilers 16

Why Do We Need Compilers? It is difficult to write, debug, maintain, and understand programs written in assembly language Tremendous increase in productivity when first compilers appeared (about 55 years ago) There are still few cases when it is better to manually write assembly code E.g., to access low-level resources of the machine (device drivers) These code fragments are very small; the compiler handles the rest of the code in the application CS 412/413 Spring 2008 Introduction to Compilers 17

Overall Compiler Structure High-level source code Compiler Low-level machine code CS 412/413 Spring 2008 Introduction to Compilers 18

Source Code Optimized for human readability Matches human notions of grammar Uses named constructs such as variables and procedures int expr(int n) { int d; d = 4 * n * n * (n + 1) * (n + 1); return d; } CS 412/413 Spring 2008 Introduction to Compilers 19

Assembly and Machine Code Optimized for hardware Consists of machine instructions; uses registers and unnamed memory locations Much harder to understand by humans lda $30,-32($30) stq $26,0($30) stq $15,8($30) bis $30,$30,$15 bis $16,$16,$1 stl $1,16($15) lds $f1,16($15) sts $f1,24($15) ldl $5,24($15) bis $5,$5,$2 s4addq $2,0,$3 ldl $4,16($15) mull $4,$3,$2 ldl $3,16($15) $33: addq $3,1,$4 mull $2,$4,$2 ldl $3,16($15) addq $3,1,$4 mull $2,$4,$2 stl $2,20($15) ldl $0,20($15) br $31,$33 bis $15,$15,$30 ldq $26,0($30) ldq $15,8($30) addq $30,32,$30 ret $31,($26),1 CS 412/413 Spring 2008 Introduction to Compilers 20

Translation Efficiency Goal: generate machine code that describes the same computation as the source code Is there a unique translation? Is there an algorithm for an ideal translation? (ideal = either fastest or smallest generated code) Compiler optimizations = find better translations! CS 412/413 Spring 2008 Introduction to Compilers 21

Example: Output Assembly Code Unoptimized Code lda $30,-32($30) stq $26,0($30) stq $15,8($30) bis $30,$30,$15 bis $16,$16,$1 stl $1,16($15) lds $f1,16($15) sts $f1,24($15) ldl $5,24($15) bis $5,$5,$2 s4addq $2,0,$3 ldl $4,16($15) mull $4,$3,$2 ldl $3,16($15) addq $3,1,$4 mull $2,$4,$2 ldl $3,16($15) addq $3,1,$4 mull $2,$4,$2 stl $2,20($15) ldl $0,20($15) br $31,$33 $33: bis $15,$15,$30 ldq $26,0($30) ldq $15,8($30) addq $30,32,$30 ret $31,($26),1 Optimized Code s4addq $16,0,$0 mull $16,$0,$0 addq $16,1,$16 mull $0,$16,$0 mull $0,$16,$0 ret $31,($26),1 CS 412/413 Spring 2008 Introduction to Compilers 22

Translation Correctness The generated code must execute precisely the same computation as in the source code Correctness is very important! hard to debug programs with broken compiler implications for development cost, security this course: techniques known to ensure correct translation CS 412/413 Spring 2008 Introduction to Compilers 23

How To Translate? Translation is a complex process source language and generated code are very different Need to structure the translation Define intermediate steps At each step use a specific program representation More machine-specific, less languagespecific as translation proceeds CS 412/413 Spring 2008 Introduction to Compilers 24

Simplified Compiler Structure Source code if (b == 0) a = b; Intermediate code Understand source code Optimize Front end (machine-independent) Optimizer Assembly code cmp $0,ecx cmovz edx,ecx Intermediate code Generate assembly code Back end (machine-dependent) CS 412/413 Spring 2008 Introduction to Compilers 25

Simplified Front-End Structure Source code (character stream) if (b == 0) a = b; Lexical Analysis Token stream Syntax Analysis Abstract syntax tree Semantic Analysis Abstract syntax tree Lexical errors Syntax errors Semantic errors CS 412/413 Spring 2008 Introduction to Compilers 26

Analogy Front end can be explained by analogy to the way humans understand natural languages Lexical analysis Natural language: He wrote the program words: he wrote the program Programming language if (b == 0) a = b tokens: if ( b == 0 ) a = b CS 412/413 Spring 2008 Introduction to Compilers 27

Analogy (ctd) Syntactic analysis Natural language: He wrote the program noun verb article noun subject predicate object sentence Programming language if ( b == 0 ) a = b test assignment if-statement CS 412/413 Spring 2008 Introduction to Compilers 28

Analogy (ctd) Semantic analysis Natural language: He wrote the computer noun verb article noun Syntax is correct; semantics is wrong! Programming language if ( b == 0 ) a = foo test assignment if a is an integer variable and foo is a procedure, then the semantic analysis will report an error CS 412/413 Spring 2008 Introduction to Compilers 29

Compiler Big Picture Lexical Analysis Syntax Analysis Semantic Analysis Optimization Code Generation Assembler Linker Loader Source code Assembly code Object code (machine code) Fully-resolved object code (machine code) Executable image CS 412/413 Spring 2008 Introduction to Compilers 30

Tentative Schedule Lexical analysis Syntax analysis Semantic analysis Prelim #1 Simple code generation Analysis Optimizations Advanced topics Prelim #2 Advanced topics 3 lectures 6 lectures 5 lectures 6 lectures 8 lectures 3 lectures 3 lectures 3 lectures CS 412/413 Spring 2008 Introduction to Compilers 31