Sung-Dong Kim Dept. of Computer Engineering, Hansung University

Slides:



Advertisements
Similar presentations
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
Advertisements

Compilers and Language Translation
Compiler Principle and Technology Prof. Dongming LU Mar. 11th, 2015.
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
Yu-Chen Kuo1 Chapter 1 Introduction to Compiling.
Chapter3: Language Translation issues
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Course Revision Contents  Compilers  Compilers Vs Interpreters  Structure of Compiler  Compilation Phases  Compiler Construction Tools  A Simple.
COP4020 Programming Languages
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
The TINY sample language and it’s compiler
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Introduction to Code Generation and Intermediate Representations
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
COMPILER CONSTRUCTION
Introduction to Compiling
Introduction CPSC 388 Ellen Walker Hiram College.
Chapter 1 Introduction Major Data Structures in Compiler
Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY Week 7 and 8(10.14 and 10.21): The lexical of TINY + Implement.
Compiler Construction (CS-636)
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,
Compilers I CNS History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
CSC 8505 Compiler Construction
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
Prologue Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
CHAPTER 1 INTRODUCTION TO COMPILER SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
System Software Theory (5KS03).
Advanced Computer Systems
Compiler Design (40-414) Main Text Book:
Introduction Chapter : Introduction.
Chapter 1 Introduction.
CS510 Compiler Lecture 1.
Introduction to Compiler Construction
Compiler Construction (CS-636)
Introduction.
Chapter 1 Introduction.
Chapter 1: Introduction to Compiling (Cont.)
Compiler Lecture 1 CS510.
Introduction CI612 Compiler Design CI612 Compiler Design.
CSE401 Introduction to Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 10: Compilers and Language Translation
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Introduction Chapter : Introduction.
Presentation transcript:

Sung-Dong Kim Dept. of Computer Engineering, Hansung University Introduction Sung-Dong Kim Dept. of Computer Engineering, Hansung University

Definition Programs that translate one language to another Source language, source code: C, C++, … Target language, target code: machine code, object code Compiler source code target code (2010-1) Compiler

Significance Compiler is a fairly complex Command interpreter, interface programs Smaller than compilers Same techniques (2010-1) Compiler

Objects Basic knowledge Necessary tools Practical experience (2010-1) Compiler

Requirements (1) Theoretical techniques Automata theory: 2.2, 2.3, 2.4, 3.2 Data structures Discrete mathematics Machine architecture Assembly language (2010-1) Compiler

Requirements (2) Practical coding Interaction between Planning Structure of a compiler Design of the programming language (2010-1) Compiler

Sample languages TINY C-Minus Student defined language (2010-1) Compiler

1.1 Why Compiler? A Brief History (1) Machine language Numeric code C7 06 0000 0002 (8x86 processors) Assembly language Symbolic forms MOV X, 2 Assembler (2010-1) Compiler

1.1 Why Compiler? A Brief History (2) High-level programming language Machine-independent Mathematical notation, natural language X = 2 (2010-1) Compiler

1.1 Why Compiler? A Brief History (3) Chomsky Chomsky hierarchy: type 0 ~ 3 Grammar and algorithms needed to recognize Context-free grammar: type 2 (Chapter 3, 4, 5) Finite automata, regular expression: type 3 (Ch. 2) Code improvement techniques Generating efficient object code Optimization techniques  wrong (2010-1) Compiler

1.1 Why Compiler? A Brief History (4) Parser generator YACC (Ch. 5) Scanner generator LEX (Ch. 2) Recent advances Development of more sophisticated PL Part of interactive development environment (IDE) (2010-1) Compiler

1.2 Programs related to Compilers (1) Interpreters Assemblers Linkers Loaders Preprocessors Editors (2010-1) Compiler

1.2 Programs related to Compilers (2) Debuggers Profilers Project managers (2010-1) Compiler

1.3 Translation Process (1) Scanner Parser Semantic Analyzer Source Code Tokens Syntax Tree Literal Table Symbol Table (2010-1) Compiler

1.3 Translation Process (2) Source Code Optimizer Code Generator Target Code Annotated Tree Intermediate Code Error Handler (2010-1) Compiler

1.3 Translation Process (3) Scanner Lexical analysis Stream of character  token Example: a[index] = 4 + 2 a: identifier  symbol table [: left bracket index: identifier ]: right bracket =: assignment 4: number  literal table (ex: “ksd”) +: plus sign 2: number (2010-1) Compiler

1.3 Translation Process (4) Parser Syntax analysis Structure of the program  grammatical analysis Parse tree (syntax tree) expression assign-expression subscript-expression additive-expression identifier index a number 4 2 [ ] = + (2010-1) Compiler

1.3 Translation Process (5) Abstract syntax tree assign-expression subscript-expression additive-expression identifier index a number 4 2 (2010-1) Compiler

1.3 Translation Process (6) Semantic analyzer Analysis of the static semantics Declarations Type checking Dynamic semantics  only in execution Attributes: extra pieces of information (2010-1) Compiler

1.3 Translation Process (7) assign-expression subscript-expression additive-expression identifier index a number 4 2 int int array Annotated tree (2010-1) Compiler

1.3 Translation Process (8) Source code optimizer Wide variation in the kinds of optimization and the placement of the optimization phase Constant folding: 4 + 2  6 assign-expression subscript-expression identifier index a number 6 int int array (2010-1) Compiler

1.3 Translation Process (9) Intermediate code, intermediate representation Three-address code t = 4 + 2, a[index] = t t = 6, a[index] = t a[index] = 6 P-code Syntax tree (2010-1) Compiler

1.3 Translation Process (10) Code generator Target machine’s instruction Data representation Ex: assembly code MOV R0, index MUL R0, 2 MOV R1, &a ADD R1, R0 MOV *R1, 6 (2010-1) Compiler

1.3 Translation Process (11) Target code optimizer Addressing mode Faster instruction Redundant code, unnecessary operations MOV R0, index SHL R0 MOV &a[R0], 6 (2010-1) Compiler

1.4 Major Data Structures (1) Role Necessary for the phase as part of operation Serve to communicate information among the phases Tokens Generated by scanner Collection of characters  token other information String of characters: name of an identifier token Value of number token (2010-1) Compiler

1.4 Major Data Structures (2) Syntax tree Pointer-based structure dynamically allocated Single variable  root node Information collected by parser and semantic analyzer Symbol table Information associated with identifier Frequently accessed Hash table, list, stack, … (2010-1) Compiler

1.4 Major Data Structures (3) Literal table Constants, strings Quick insertion and lookup Intermediate code Array of text strings Temporary text file Linked list of structures (2010-1) Compiler

1.4 Major Data Structures (4) Temporary files Due to not enough memory For backpatching address during code generation (2010-1) Compiler

1.5 Other issues (1) Structure of compiler is very important Reliability Efficiency Usefulness Maintainability Analysis and synthesis Analysis: operations that analyze the source program Synthesis: operations involved in producing translated code (2010-1) Compiler

1.5 Other issues (2) Front end and back end Front end: operations depending on the source language Back end: operations depending on the target language Source Code Intermediate Code Target Code Front End Back End (2010-1) Compiler

1.5 Other issues (3) Passes One pass Multiple passes More passes Efficient compilation Less efficient target code Multiple passes Scanning, parsing Semantic analysis, source-level optimization Code generation, target-level optimization More passes (2010-1) Compiler

1.5 Other issues (4) Language definition and compilers Language reference manual (language definition) Formal lexical and syntactic structure Regular expression Context-free grammar Semantics of programming languages English Language standard ANSI C, PASCAL, FORTRAN Test suite: standard test programs (2010-1) Compiler

1.5 Other issues (5) TYNY C-Minus Denotational semantics Lexical structure: 2.5 Syntactic structure: 3.7 Semantic structure: 6.5 C-Minus Appendix A Denotational semantics Formal definition for semantics by mathematical terms Functional programming community (2010-1) Compiler

1.5 Other issues (6) Runtime environment Structure of data allowed Kinds of function calls and returned values allowed 3 basic types of runtime environments Static Semi-dynamic (stack-based) Fully-dynamic (2010-1) Compiler

1.5 Other issues (7) Compiler options and interfaces = compiler pragmatics Interfacing with OS I/O Access to file system Providing options to the user Listing characteristics Code optimization (2010-1) Compiler

1.5 Other issues (8) Error handling Static (compile-time) errors Exception handling (2010-1) Compiler

1.6 Bootstrapping and porting (1) Implementation (host) language Machine language Execute immediately Cross compiler T-diagram Compiler for language A Written in language B Existing Compiler for language B Running compiler for language A S T H (2010-1) Compiler

1.6 Bootstrapping and porting (2) Combination 1 Combination 2 A B C H A B H M K (2010-1) Compiler

1.6 Bootstrapping and porting (3) Scenario 1 Scenario 2: cross compiler A H B A H B K (2010-1) Compiler

1.6 Bootstrapping and porting (4) “quick and dirty” compiler in assembly language Inefficient and correct Compile “good” compiler A H Compiler written in its own language A “quick and dirty” compiler written in machine language Running but inefficient compiler (2010-1) Compiler

1.6 Bootstrapping and porting (5) Recompile  final version A H Compiler written in its own language A Running but inefficient compiler Final version of the (2010-1) Compiler

1.6 Bootstrapping and porting (6) Advantages Improvement to the source code of the compiler  working compiler Easy porting A K H Compiler source code retargeted to K Original compiler Cross compiler (2010-1) Compiler

1.6 Bootstrapping and porting (7) K H Compiler source code retargeted to K Cross compiler Retargeted compiler (2010-1) Compiler

1.7 TINY sample language (1) TINY language Sequence of statements separated by semicolons No procedures, no declarations Integer variable only Two control statements if-statement: optional else (terminated by end) repeat-statement read/write statements { comments }: cannot be nested (2010-1) Compiler

1.7 TINY sample language (2) Expressions Boolean Comparison of two arithmetic expressions: <, = Only as tests in control statements: no boolean variables, assignment, I/O (2010-1) Compiler

1.7 TINY sample language (3) Integer arithmetic read x; {input an integer} if x > 0 then {don’t compute if x <= 0 } fact := 1; repeat fact := fact * x; x := x - 1; until x = 0; write fact {output factorial of x } end (2010-1) Compiler

1.7 TINY sample language (4) TINY compiler File construction globals.h main.c util.h util.c scan.h scan.c parse.h parse.c symtab.h symtab.c analyze.h analyze.c code.h code.c cgen.h cgen.c (2010-1) Compiler

1.7 TINY sample language (5) 4 passes First pass = scanner + parser Second: constructing symbol table Third: type checking Final: code generator Central code syntax tree = parse(); buildSymtab(syntaxTree); typeCheck(syntaxTree); codeGen(syntaxTree, codefile); (2010-1) Compiler

1.7 TINY sample language (6) Conditional compilation flags NO_PARSE NO_ANALYZE NO_CODE Usage: tiny sample.tny  sample.tm (2010-1) Compiler

1.7 TINY sample language (7) Options EchoSource TraceScan TraceParse TraceAnalyze TraceCode (2010-1) Compiler

1.7 TINY sample language (8) TM machine Assembly language: target language Translation example: a[index ] =6 LDC 1,0(0) load 0 into reg 1 * The following instruction assumes index is at location 10 in memory LD 0,10(1) load val at 10+R1 into R0 LDC 1,2(0) load 2 into reg 1 MUL 0,1,0 put R1*R0 into reg 1 * The following instruction assumes a is at location 20 in memory LDA 1,20(1) load 20+R1 into R0 ADD 0,1,0 put R1+R0 into R0 LDC 1,6(0) load 6 into reg 1 ST 1,0(0) store R1 at 0+R0 (2010-1) Compiler

1.7 TINY sample language (9) Simulator Reads the assembly code from a file Execute it tm sample.tm (2010-1) Compiler

1.8 C-Minus (1) More extensive language than TINY Considerably restricted subset of C Integers Integer arrays Functions (procedure, void function) Local, global declarations Recursive functions if-, while- statement (2010-1) Compiler

1.8 C-Minus (2) Sample program int fact(int x) /* recursive factorial function */ { if (x > 1) return x * fact(x-1); else return 1; } void main(void) { int x; x = read(); if (x > 0) write(fact(x)); (2010-1) Compiler