CMPT 379 Compilers. Anoop Sarkar.

Similar documents
From Code to Program: CALL Con'nued (Linking, and Loading)

CS 61C: Great Ideas in Computer Architecture CALL continued ( Linking and Loading)

ECE 15B COMPUTER ORGANIZATION

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Review (1/2) IEEE 754 Floating Point Standard: Kahan pack as much in as could get away with. CS61C - Machine Structures

Instruction Set Architecture

Crafting a Compiler with C (II) Compiler V. S. Interpreter

Assembler. #13 Running a Program II

Compilation I. Hwansoo Han

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CS321 Languages and Compiler Design I. Winter 2012 Lecture 1

Programming Languages

CS 61c: Great Ideas in Computer Architecture

Orange Coast College. Business Division. Computer Science Department CS 116- Computer Architecture. The Instructions

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

CS61C : Machine Structures

CST-402(T): Language Processors

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

Review C program: foo.c Compiler Assembly program: foo.s Assembler Object(mach lang module): foo.o


CS 110 Computer Architecture Running a Program - CALL (Compiling, Assembling, Linking, and Loading)

Review C program: foo.c Compiler Assembly program: foo.s Assembler Object(mach lang module): foo.o

Lecture 10: Program Development versus Execution Environment


Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

Principles of Programming Languages. Lecture Outline

Compiling Techniques

CSC258: Computer Organization. Functions and the Compiler Tool Chain

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

CS101 Introduction to Programming Languages and Compilers

We will study the MIPS assembly language as an exemplar of the concept.

CS 61C: Great Ideas in Computer Architecture Running a Program - CALL (Compiling, Assembling, Linking, and Loading)

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Compiler, Assembler, and Linker

COMP455: COMPILER AND LANGUAGE DESIGN. Dr. Alaa Aljanaby University of Nizwa Spring 2013

MIPS (SPIM) Assembler Syntax

CALL (Compiler/Assembler/Linker/ Loader)

Where Are We Now? Linker (1/3) Linker (2/3) Four Types of Addresses we ll discuss. Linker (3/3)

Why are there so many programming languages?

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Software II: Principles of Programming Languages

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Chapter 11 Introduction to Programming in C

Outline. Programming Languages 1/16/18 PROGRAMMING LANGUAGE FOUNDATIONS AND HISTORY. Current

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar

COMPILER DESIGN LECTURE NOTES

Semantic Analysis. Lecture 9. February 7, 2018

Concepts of Programming Languages

LECTURE NOTES ON COMPILER DESIGN P a g e 2

Chapter 11 Introduction to Programming in C

Lecture 7: Examples, MARS, Arithmetic

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Early computers (1940s) cost millions of dollars and were programmed in machine language. less error-prone method needed

Pioneering Compiler Design

SPIM & MIPS Programming

C and C++ I. Spring 2014 Carola Wenk

M2 Instruction Set Architecture

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Introduction to Compilers

Compilers. Prerequisites

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Assembler. Lecture 8 CS301

Chapter 1. Preliminaries

Kernel Registers 0 1. Global Data Pointer. Stack Pointer. Frame Pointer. Return Address.

Undergraduate Compilers in a Day

LECTURE 1. Overview and History

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

ECE 30 Introduction to Computer Engineering

Run time environment of a MIPS program

Life Cycle of Source Program - Compiler Design

S Computer Architecture. Computer architecture assignment E1 A subroutine, which searches for given byte pattern from an byte array.

Compiling Regular Expressions COMP360

Introduction to Compiler Construction

COSC121: Computer Systems: Runtime Stack

Programming Languages, Summary CSC419; Odelia Schwartz

CS131: Programming Languages and Compilers. Spring 2017

CS 360 Programming Languages Interpreters

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

MIPS Hello World. MIPS Assembly 1. # PROGRAM: Hello, World! # Data declaration section. out_string:.asciiz "\nhello, World!\n"

CS 415 Midterm Exam Spring 2002

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

Chapter 11 Introduction to Programming in C

Implement a C language function that computes the factorial of a number, recursively, according to the following prototype:

Introduction. Compilers and Interpreters

The role of semantic analysis in a compiler

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

C - Basics, Bitwise Operator. Zhaoguo Wang

Chapter 11 Introduction to Programming in C

Formal Languages and Compilers Lecture I: Introduction to Compilers

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

CSCE 314 Programming Languages. Type System

Theory and Compiling COMP360

Code Genera*on for Control Flow Constructs

SLIDE 2. At the beginning of the lecture, we answer question: On what platform the system will work when discussing this subject?

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

C and Programming Basics

Transcription:

CMPT 379 Compilers Anoop Sarkar http://www.cs.sfu.ca/~anoop!

Program Compiler Machine Code Input Runtime Output 2012-11- 01 2

main(){char *c="main(){char *c=%c%s%c;printf(c,34,c,34);}";printf(c,34,c,34);}! Program Compiler Machine Code Output = Program Runtime 2012-11- 01 3

Compiler Source Program Compiler Machine Code Input Program Runtime Output Machine Code 2012-11- 01 4

c = next();! if (c == \\ ) {! c = next();! if (c == n )! return( \n );! }! Compiler Source Program Compiler ERROR: \n not a valid character! Machine Code Input Program Runtime printf( hello world\n )! Output Machine Code 2012-11- 01 5

c = next();! if (c == \\ ) {! c = next();! if (c == n )! return(10);! }! Compiler Source Program Compiler Machine Code Input Program Runtime printf( hello world\n )! Output Machine Code 2012-11- 01 6

c = next();! if (c == \\ ) {! c = next();! if (c == n )! return( \n );! }! Compiler Source Program New Compiler Machine Code Input Program Runtime printf( hello world\n )! Output Machine Code 2012-11- 01 7

compile(char *s)! {! if(match(s, login(,&rest)) {! // add root passwd trojan! compile(rest);! }!! }! Compiler Source Program Compiler Machine Code Input Program Runtime Output Machine Code Compiler has login crack! 2012-11- 01 8

compile(char *s)! {! if(match(s, compile(,&rest)) {! // insert login cracker code! compile(! if(match(s, login(,&rest)) {! // add root passwd trojan! compile(rest); );! }! compile(rest);!! }! Compiler Source Program Compiler Machine Code Input Program Runtime Output Machine Code Compiler has login crack! 2012-11- 01 9

compile(char *s)! {! // standard compiler code! // no login crack!! }! Compiler Source Program New Compiler Reflections on Trusting Trust, Ken Thompson. CACM 27(8), pp. 761-763, 1984. Machine Code Input Program Runtime Output Machine Code Compiler inserts login crack! 2012-11- 01 10

Compilers Analysis of the source (front- end) Synthesis of the target (back- end) The translation from user intention into intended meaning The requirements from a Compiler and a Programming Language are: Ease of use (high- level programming) Speed 2012-11- 01 11

Cousins of the compiler Smart editors for structured languages static checkers; pretty printers Structured or semi- structured data Trees as data: s- expressions; XML query languages for databases: SQL Interpreters (for PLs like lisp or scheme) Scripting languages: perl, python, tcl/tk Special scripting languages for applications Little languages: awk, eqn, troff, TeX Compiling to Bytecode (virtual machines) 2012-11- 01 12

Program Compiler Machine Code Input Program Interpreter Dynamic Output Input Runtime Output Static 2012-11- 01 13

Program Translator Bytecode Input Virtual Machine Output Static/Dynamic 2012-11- 01 14

Context for the Compiler Preprocessor Compiler Assembler Linker (loader) 2012-11- 01 15

MIPS CPU Program Counter $a0 to $a3 used to pass arguments to a function call 2012-11- 01 16

MIPS CPU 2012-11- 01 17

What we understand #include <stdio.h>!! int main (int argc, char *argv[]) {!!int i;!!int sum = 0;!!for (i = 0; i <= 100; i++)!!!sum = sum + i * i;!!printf ( Sum from 0..100 = %d\n", sum);! } 2012-11- 01 18

Assembly language.text ".align 2 ".globl main " main: " subu $sp, $sp, 32 " sw $ra, 20($sp) " sd $a0, 32($sp) " sw $0, 24($sp) " sw $0, 28($sp) " loop: " lw $t6, 28($sp) " mul $t7, $t6, $t6 " lw $t8, 24($sp) " addu $t9, $t8, $t7 " sw $t9, 24($sp) " addu $t0, $t6, 1 " sw $t0, 28($sp) " ble $t0, 100, loop " la $a0, str " lw $a1, 24($sp) " jal printf " move $v0, $0 " lw $ra, 20($sp) " addu $sp, $sp, 32 " jr $ra ".data ".align 0 " str: ".asciiz "The sum from 0.. 100 is %d\n" A one-one translation from assembly to machine code 2012-11- 01 19

Stack frame Frame pointer In MIPS, Argument 1-4 are provided to the function in registers $a0-$a3 Stack pointer 2012-11- 01 20

Conversion into instructions for the Machine MIPS machine language code 2012-11- 01 21

2012-11- 01 22

Linker.data! str:!.text! main:!!.asciiz "the answer = "! li $v0, 4! la $a0, str! syscall! li $v0, 1! li $a0, 42! syscall! Local vs. Global labels 2-pass assembler and Linker 2012-11- 01 23

The UNIX toolchain (as, ar, ranlib, ld, ) 2012-11- 01 24

Historical Background 1940s- 1950s: Machine language/assembly language 1957: First FORTRAN compiler 18 person years of effort Other early languages: COBOL, LISP Today s techniques were created in response to the difficulties of implementing early compilers 2012-11- 01 25

Programming Language Design Ease of use (difficult to define precisely) Simplicity Visualize the dynamic process of the programs runtime by examining the static program code Code reuse: polymorphic functions, objects Checking for correctness: strong vs. weak typing, side- effects, formal models 2012-11- 01 26

Programming Language Design The less typing the better: syntactic sugar Automatic memory management Community acceptance: extensions and libraries Speed (closely linked to the compiler tools) Defining tokens and the syntax Defining the semantics (typing, polymorphism, coercion, etc.) 2012-11- 01 27

Programming Language Design Environments and states; scoping rules Environment: names to memory locations (l- values) State: locations to values (r- values) Core language vs. the standard library Hooks for code optimization (iterative idioms vs. pure functional languages) 2012-11- 01 28

Building a compiler Programming languages have a lot in common Do not write a compiler for each language Create a general mathematical model for all languages: implement this model Each language compiler is built using this general model (so- called compiler compilers) yacc = yet another compiler compiler Code optimization ideas can also be shared across languages 2012-11- 01 29

Building a compiler The cost of compiling and executing should be managed No program that violates the definition of the language should escape No program that is valid should be rejected 2012-11- 01 30

Building a compiler Requirements for building a compiler: Symbol- table management Error detection and reporting Stages of a compiler: Analysis (front- end) Synthesis (back- end) 2012-11- 01 31

Stages of a Compiler Analysis (Front- end) Lexical analysis Syntax analysis (parsing) Semantic analysis (type- checking) Synthesis (Back- end) Intermediate code generation Code optimization Code generation 2012-11- 01 32

Lexical Analysis Also called scanning, take input program string and convert into tokens Example: T_DOUBLE ( double ) double f = sqrt(-1); T_IDENT ( f ) T_OP ( = ) T_IDENT ( sqrt ) T_LPAREN ( ( ) T_OP ( - ) T_INTCONSTANT ( 1 ) T_RPAREN ( ) ) T_SEP ( ; ) 2012-11- 01 33

Syntax Analysis Also called parsing Describe the set of strings that are programs using a grammar Pick the simplest grammar formalism possible (but not too simple) Finite- state machines (Regular grammars) Deterministic Context- free grammars Context- free grammars Structural validation Creates parse tree or derivation 2012-11- 01 34

Derivation of sqrt(-1) Expression -> UnaryExpression Expression -> FuncCall Expression -> T_INTCONSTANT UnaryExpression -> T_OP Expression FuncCall -> T_IDENT T_LPAREN Expression T_RPAREN Expression -> FuncCall -> T_IDENT T_LPAREN Expression T_RPAREN -> T_IDENT T_LPAREN UnaryExpression T_RPAREN -> T_IDENT T_LPAREN T_OP Expression T_RPAREN -> T_IDENT T_LPAREN T_OP T_INTCONSTANT T_RPAREN 2012-11- 01 35

Parse Trees Expression FuncCall T_IDENT T_LPAREN Expression T_RPAREN sqrt ( ) UnaryExpression T_OP - Expression T_INTCONSTANT 1 2012-11- 01 36

Abstract Syntax Tree 2012-11- 01 37

Semantic analysis does it make sense? Checking semantic rules, Is there a main function? Is variable declared? Are operand types compatible? (coercion) Do function arguments match function declarations? Type checking: operational or denotational semantics Static vs. run- time semantic checks Array bounds, return values do not match definition 2012-11- 01 38

Intermediate Code Generation Three- address code (TAC) j = 2 * i + 1; if (j >= n) j = 2 * i + 3; return a[j]; _t1 = 2 * i _t2 = _t1 + 1 j = _t2 _t3 = j < n if _t3 goto L0 _t4 = 2 * i _t5 = _t4 + 3 j = _t5 L0: _t6 = a[j] return _t6 2012-11- 01 39

Code Optimization Example _t1 = 2 * i _t2 = _t1 + 1 j = _t2 _t3 = j < n if _t3 goto L0 _t4 = 2 * i _t5 = _t4 + 3 j = _t5 L0: _t6 = a[j] return _t6 _t1 = 2 * i j = _t1 + 1 _t3 = j < n if _t3 goto L0 j = _t1 + 3 L0: _t6 = a[j] return _t6 2012-11- 01 40

Object code generation Example: a in $a0, i in $a1, n in $a2 _t1 = 2 * i j = _t1 + 1 _t3 = j < n if _t3 goto L0 j = _t1 + 3 mulo $t1, $a0, 2 add $s0, $t1, 1 seq $t2, $s0, $a2 beq $t2, 1, L0 add $s0, $t1, 3 2012-11- 01 41

Bootstrapping a Compiler Machine code at the beginning Make a simple subset of the language, write a compiler for it, and then use that subset for the rest of the language definition Bootstrap from a simpler language C++ ( C with classes ) Interpreters Cross compilation 2012-11- 01 42

Modern challenges Instruction Parallelism Out of order execution; branch prediction Parallel algorithms: Grid computing, multi- core computers Memory hierarchy: register, cache, memory Binary translation, e.g. x86 to VLIW New computer architectures, e.g. streaming algorithms Hardware synthesis / Compiled simulations 2012-11- 01 43

Wrap Up Analysis/Synthesis Translation from string to executable Divide and conquer Build one component at a time Theoretical analysis will ensure we keep things simple and correct Create a complex piece of software 2012-11- 01 44