Overview of Compiler. A. Introduction

Similar documents
Program Exploitation Intro

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

CSC 8400: Computer Systems. Machine-Level Representation of Programs

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

Towards the Hardware"

EECE.3170: Microprocessor Systems Design I Summer 2017

Lab 3. The Art of Assembly Language (II)

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

SOFTWARE ARCHITECTURE 5. COMPILER

Second Part of the Course

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

C++ Tour. Administrative Trivia. Administrative Trivia. Your ID. Announcement. Announcement. Daniel Mock, About me: Piyush Kumar

CSE P 501 Exam 8/5/04 Sample Solution. 1. (10 points) Write a regular expression or regular expressions that generate the following sets of strings.

CMSC 313 Lecture 12 [draft] How C functions pass parameters

LdPinch Report. Feng Zhu Jinpeng Wei

Inline Assembler. Willi-Hans Steeb and Yorick Hardy. International School for Scientific Computing

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

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

16.317: Microprocessor Systems Design I Fall 2015

Low-Level Essentials for Understanding Security Problems Aurélien Francillon

Q1: Multiple choice / 20 Q2: Data transfers and memory addressing

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

CSE P 501 Exam 11/17/05 Sample Solution

CSCI 334: Principles of Programming Languages. Computer Architecture (a really really fast introduction) Lecture 11: Control Structures II

Module 3 Instruction Set Architecture (ISA)

Introduction to 8086 Assembly

COMPILER DESIGN. For COMPUTER SCIENCE

THEORY OF COMPILATION

The Hardware/Software Interface CSE351 Spring 2013

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

CS 4201 Compilers 2014/2015 Handout: Lab 1

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

Practical Malware Analysis

CPS104 Recitation: Assembly Programming

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

Compiler, Assembler, and Linker

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

Process Layout and Function Calls

Assembly Language for Intel-Based Computers, 4 th Edition. Lecture 25: Interface With High-Level Language

16.317: Microprocessor Systems Design I Fall 2014

System calls and assembler

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

ADDRESSING MODES. Operands specify the data to be used by an instruction

Compiling Regular Expressions COMP360

Computer Architecture and Assembly Language. Practical Session 5

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

Machine-Level Programming II: Control Flow

Overview REWARDS TIE HOWARD Summary CS 6V Data Structure Reverse Engineering. Zhiqiang Lin

Reverse Engineering Low Level Software. CS5375 Software Reverse Engineering Dr. Jaime C. Acosta

The IA-32 Stack and Function Calls. CS4379/5375 Software Reverse Engineering Dr. Jaime C. Acosta

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

15-213/18-243, Spring 2011 Exam 1

16.317: Microprocessor Systems Design I Fall 2013

Computer Systems Organization V Fall 2009

Computer Science Final Examination Wednesday December 13 th 2006

CS , Spring 2002 Exam 2

CS Bootcamp x86-64 Autumn 2015

Overview. Overview. Programming problems are easier to solve in high-level languages

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

x86 assembly CS449 Fall 2017

Sungkyunkwan University

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

Betriebssysteme und Sicherheit Sicherheit. Buffer Overflows

Function Call Convention

Summer 2003 Lecture 14 07/02/03

Lecture 2 Assembly Language

The geometry of innocent flesh on the bone: Return-into-libc without function calls (on the x86) Hovav Shacham presented by: Fabian Fäßler

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

Summer 2003 Lecture 15 07/03/03

THEORY OF COMPILATION

IA-32 Architecture. CS 4440/7440 Malware Analysis and Defense

Compilers Crash Course

Macros in Pentium. Assembly Language. October 25

Link 2. Object Files

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

3.1 DATA MOVEMENT INSTRUCTIONS 45

Chapter 2 A Quick Tour

Summary: Direct Code Generation

Intel 8086: Instruction Set

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

Chapter 3: Addressing Modes

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Compilers and optimization techniques. Gabriele Fatigati - Supercomputing Group

Machine-Level Programming II: Control and Arithmetic

CSE P 501 Compilers. x86 Lite for Compiler Writers Hal Perkins Autumn /25/ Hal Perkins & UW CSE J-1

T Jarkko Turkulainen, F-Secure Corporation

COMPUTER ENGINEERING DEPARTMENT

CVE EXPLOIT USING 108 BYTES AND DOWNLOADING A FILE WITH YOUR UNLIMITED CODE BY VALTHEK

mith College Computer Science CSC231 Assembly Week #9 Spring 2017 Dominique Thiébaut

Reverse Engineering II: The Basics

Instruction Set Architectures

CSE 582 Autumn 2002 Exam Sample Solution

Sample Exam I PAC II ANSWERS

Transcription:

CMPSC 470 Lecture 01 Topics: Overview of compiler Compiling process Structure of compiler Programming language basics Overview of Compiler A. Introduction What is compiler? What is interpreter?

A very brief history of compiler. 1. Initially programs were written in machine language: numeric codes that represent the actual machine operations to be performed. Writing a program in machine language is extremely time consuming and tedious. 2. Soon, this form of program is replaced by assembly language. 3. Later, high-level languages, such as C, were developed, which translate to assembly program or machine language.

int main() int a = 1; while (a!= 10) if (a < 10) a = a + 1; else a = a - 1; Source Program Target Program int main() 00A91650 push ebp function start, initialize stack 00A91651 mov ebp,esp 00A91653 sub esp,0d8h 00A91659 push ebx 00A9165A push esi 00A9165B push edi 00A9165C lea edi,[ebp-0d8h] 00A91662 mov ecx,36h 00A91667 mov eax,0cccccccch 00A9166C rep stos dword ptr es:[edi] int a = 1; 00A9166E mov dword ptr [a],1 move value 1 to location of a while (a!= 10) 00A91675 cmp dword ptr [a],0ah compare value of a and 10 00A91679 je main+47h (00A91697) jump to 00A91697 if equal if (a < 10) 00A9167B cmp dword ptr [a],0ah compare value of a and 10 00A9167F jge main+3ch (0A9168Ch) jump to 00A9168C if greater than or equal a = a + 1; 00A91681 mov eax,dword ptr [a] move value a to register eax 00A91684 add eax,1 add 1 to register eax 00A91687 mov dword ptr [a],eax move value in register eax to location a else 00A9168A jmp main+45h (0A91695h) jump 00A91695 a = a - 1; 00A9168C mov eax,dword ptr [a] move value a to register eax 00A9168F sub eax,1 subtract 1 from register eax 00A91692 mov dword ptr [a],eax move value in register eax to location a 00A91695 jmp main+25h (0A91675h) jump 00A91675 00A91697 xor eax,eax 00A91699 pop edi 00A9169A pop esi 00A9169B pop ebx 00A9169C mov esp,ebp 00A9169E pop ebp 00A9169F ret return

Low-level language: High-level language: Assembly language: Why should we learn compiler?

B. Overview of compiling process 1. A source program may be divided and stored in separated files. 2. Preprocessor does combining separated files into one source program, expanding macros, etc. Then, it generates a modified source program. 3. Compiler produces an assembly-language program, because using assembly program is 4. Assembler produces relocatable machine code. 5. Linker links (or combines) relocatable machine codes with other relocatable object files or library files, and generate target machine code.

C. Structure of Compiler Compiler receives modified source program as a character stream, and produces target machine code (or assembly program). It is composed of two parts: analysis, and synthesis. Analysis part is often called front-end, and synthesis part is called back-end. Front-end: Back-end: They are decomposed into 7 phases again: 1. 2. 3. 4. 5. 6. 7.

C.a) Lexical Analysis (or Scanning, Tokenizing) Lexical analyzer reads source program as a character stream, and groups characters into lexemes. C.b) Syntax Analysis (or Parsing) It create tree-like intermediate representation of source program, showing grammatical structure (called syntax tree).

C.c) Semantic Analysis Semantic analyzer checks source for semantic consistency using syntax tree and symbol table. C.d) Intermediate Code Generation Intermediate code generator generate explicit low-level or machine-like intermediate representation of source, which is a program for an abstract machine.

Three-address code C.e) Code Optimization (machine-independent) Code optimizer improves intermediate code to generate better codes in terms that:

C.f) Code Generation Code generator takes as input an intermediate representation and maps it into target language (s.t. machine dependent binary code). C.g) Symbol-Table Management Symbol-table contains records for variables, functions, etc., in a form of name and field, such that

C.h) Grouping of Phases into Passes Some compilers group several phases into a pass. Example) running each phase in separate steps with producing its output Example) running phases in front-end (lexical analysis, syntax analysis,, intermediate code generation) at the same time in one pass. C.i) Compiler-Construction Tools 1. Parser generator 2. Scanner generator 3. Syntax-directed translation engine 4. Code-generator generator 5. Data-flow analysis engine 6. Compiler-construction toolkit

D. Programming Language Basics D.a) Static policy vs Dynamic policy Scope of a declaration of (variable) xx is the reason of the program where using xx refer to this declaration. Static scope or lexical scope Example) int b = 5; int foo() int a = b + 5; return a; int bar() int b = 2; return foo(); int main() foo(); // return bar(); // return Return 0; Dynamic scope int main() foo(); // return bar(); // return Return 0;

Static policy Dynamic policy D.b) Environment and State A variable has three properties: name, location, and value. There are two stages mapping from name to value.

D.c) Function and Parameter Passing Difference between function, procedure, and method: Parameter passing mechanism call-by-value call-by-reference call-by-name What will be printed if the following program uses call-by-name? void b() cout << "call b" << endl; void c() cout << "call c" << endl; void func(a, b, c) if ( a ) b; else c; int main() func(true, b(), c());