PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 1 제 9 장 중 간 언어 컴파일러 입문.

Slides:



Advertisements
Similar presentations
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Advertisements

Intermediate Code Generation
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.
8 Intermediate code generation
1 Compiler Construction Intermediate Code Generation.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Chapter 14: Building a Runnable Program Chapter 14: Building a runnable program 14.1 Back-End Compiler Structure 14.2 Intermediate Forms 14.3 Code.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Intermediate Representations Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Intermediate Code CS 471 October 29, CS 471 – Fall Intermediate Code Generation Source code Lexical Analysis Syntactic Analysis Semantic.
Compiler Summary Mooly Sagiv html://
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
5.3 Machine-Independent Compiler Features
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
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.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
LANGUAGE TRANSLATORS: WEEK 24 TRANSLATION TO ‘INTERMEDIATE’ CODE (overview) Labs this week: Tutorial Exercises on Code Generation.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Introduction to code generation.
Chapter 8 Intermediate Code Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Compiler Chapter# 5 Intermediate code generation.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
4-1 Chapter 4 - The Instruction Set Architecture Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
Joey Paquet, 2000, Lecture 10 Introduction to Code Generation and Intermediate Representations.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
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.
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Chapter# 6 Code generation.  The final phase in our compiler model is the code generator.  It takes as input the intermediate representation(IR) produced.
Winter Compilers Software Eng. Dept. – Ort Braude Compiling Assignments and Expressions Lecturer: Esti Stein brd4.ort.org.il/~esti2.
Intermediate Code Representations
12/18/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Winter 2008.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Code Generation CPSC 388 Ellen Walker Hiram College.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Intermediate Language  Compiler Model Front-End− language dependant part Back-End− machine dependant part [1/34]
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
1 Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
INTERMEDIATE LANGUAGES SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Compiler Chapter 9. Intermediate Languages Sung-Dong Kim Dept. of Computer Engineering, Hansung University.
CS 404 Introduction to Compiler Design
Compiler Design (40-414) Main Text Book:
Computer Architecture Instruction Set Architecture
Intermediate code Jakub Yaghob
Compiler Construction (CS-636)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler design Introduction to code generation
Compiler Chapter 9. Intermediate Languages
Chapter 1: Introduction to Compiling (Cont.)
Compiler Construction
Intermediate Representations Hal Perkins Autumn 2011
Instructions - Type and Format
Programming Languages (CS 550) Mini Language Compiler
Intermediate Representations
Chapter 6 Intermediate-Code Generation
Intermediate Representations
Intermediate Code Generating machine-independent intermediate form.
Programming Languages (CS 360) Mini Language Compiler
Presentation transcript:

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 1 제 9 장 중 간 언어 컴파일러 입문

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 2 Contents Introduction Polish Notation Three Address Code Tree Structured Code Abstract Machine Code Concluding Remarks

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 3 Compiler Model Source Program Lexical Analyzer Syntax Analyzer Semantic Analyzer Intermediate Code Generator tokens AST Front-End Code Optimizer Target Code Generator IC Back-End IL Object Program Front-End- language dependant part Back-End- machine dependant part Introduction

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 4 IL 의 필요성 –Modular Construction –Automatic Construction –Easy Translation –Portability –Optimization –Bootstrapping IL 의 분류 –P–Polish Notation --- Postfix, IR –T–Three Address Code --- Quadruple, Triple, Indirect triple –T–Tree Structured Code --- PT, AST, TCOL –A–Abstract Machine Code --- P-code, EM-code, U-code, Bytecode

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 5 Two level Code Generation IL S – 소스로부터 자동화에 의해 얻을 수 있는 형태 – 소스 언어에 의존적이며 high level 이다. IL T – 후단부의 자동화에 의해 목적기계로의 번역이 매우 쉬운 형태 – 목적기계에 의존적이며 low level 이다. IL S to IL T –IL S 에서 IL T 로의 번역이 주된 작업임. Source Front-End IL S IL S -IL T IL T Back-End Target

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 6 ☞ Polish mathematician Lucasiewiez invented the parenthesis-free notation. Postfix(Suffix) Polish Notation earliest IL popular for interpreted language - SNOBOL, BASIC –general form : e 1 e 2... e k OP (k ≥ 1) where, OP: k_ary operator e i : any postfix expression (1 ≤ i ≤ k) Polish Notation

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 7 –example : if a then if c-d then a+c else a*c else a+b 〓〉 a L1 BZ c d - L2 BZ a c + L3 BR L2: a c * L3 BR L1: a b + L3: –note 1) high level:source to IL - fast & easy translation IL to target - difficulty 2) easy evaluation - operand stack 3) optimization 부적당 - 다른 IL 로의 translation 필요 4) parentheses free notation - arithmetic expression –interpretive language 에 적합 Source Translator Postfix Evaluator Result

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 8 Internal Representation(IR) –low-level prefix polish notation - addressing structure of target machine compiler-compiler IL - table driven code generation –IR program - a sequence of root-level IR expression –IR expression: OP e1 e e k (k ≥ 1) where, OP: k-ary operator correspondence with target machine instruction. ┌─ root-level operator - not appear in an operand │ ⇒ root-level IR expression. └─ internal operator - appear in an operand ⇒ internal IR expression. e i : operand --- single symbol or internal IR expression.

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 9 –example D := E ⇔ := + d r ↑ + e r where,r : local base register d, e : location of variable D and E + : additive operator ↑ : unary operator giving the value of the location := : assignment operator(root-level) –example FOR D := E TO F DO Loop body; := + d r ↑+ e r := + temp r ↑+ f r j L2 :L1 Loop body := + d r + ↑+ d r 1 :L2 <= L1 ? ↑+ d r ↑+ temp r D := E; TEMP := F; GOTO 2 1: Loop body D := D + 1; 2: IF D <= TEMP THEN GOTO 1;

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 10 –Note 1) Shift-reduce parser --- prefix : fewer states than postfix 2) Several addressing mode ┌─ prefix : operator 만 보고 결정 (no backup) └─ postfix : backup 필요 ex) assumption: first operand computed in register r. r.1 ::= (/ d. 1 r. 2) r.1 ::= (+ r. 1 r. 2) ┌ prefix - [r -> /. d r] │ first operand changed to d and continue └ postfix - [r ->. d r /] [r ->. r r +] shift r, shift r and block([r -> r r. +]) ⇒ backup 3) Easy translation IR to target - easy source to IR - difficulty

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 11 most popular IL, optimizing compiler General form: A := B op C where,A : result address B, C : operand addresses op : operator (1) Quadruple - 4-tuple notation,,, (2) Triple - 3-tuple notation,, (3) Indirect triple - execution order table & triples Three Address Code

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 12 –example A ← B + C * D / E F ← C * D

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 13 Note Quadruple vs. Triple –quadruple - optimization 용이 –triple - removal of temporary addresses ⇒ Indirect Triple extensive code optimization 용이 – IL rearrange 가능 (triple 제외 ) easy translation - source to IL difficult to generate good code –quadruple to two-address machine –triple to three-address machine

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 14 Abstract Syntax Tree –parse tree 에서 redundant 한 information 제거. ┌ leaf node --- variable name, constant └ internal node --- operator –[ 예제 8] --- Text p.377 { x = 0; y = z + 2 * y; while ((x<n) and (v[x] != z)) x = x+1; return x; } Tree Structured Code

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 15 Tree Structured Common Language(TCOL) –Variants of AST - containing the result of semantic analysis. –TCOL operator - type & context specific operator –Context ┌ value rhs of assignment statement ├ location lhs of assignment statement ├ boolean----- conditional control statement └ statement----- statement ex). : operand --- location result --- value while : operand --- boolean, statement result --- statement

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 16 Example) int a; float b;... b = a + 1; –Representation graph orientation ┌ internal notation efficient └ external notation debug, interface linear graph notation Example) int a; float b;... b = a + 1; AST:assign badd a1 TCOL:assign bfloat addi a 1.

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 17 Note –AST automatic AST generation(output of parser) Parser Generator┌ leaf node specification └ operator node specification –TCOL automatic code generation : PQCC (1) intermediate level:high level --- parse tree like notation control structure low level --- data access (2) semantic specification:dereferencing, coercion, type specific operator dynamic subscript and type checking (3) loop optimization -----high level control structure easy reconstruction (4) extensibility define new TCOL operator

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 18 Motivation ┌ rapid development of machine architectures └ proliferation of programming languages –portable & adaptable compiler design --- P_CODE porting --- rewriting only back-end –compiler building system --- EM_CODE M front-ends N back-ends + M compilers for N target machines Abstract Machine Code

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 19 Model front -end back -end target machine abstract machine interpreter source program interface target code abstract machine code

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 20 Pascal-P Code Pascal P Compiler --- portable compiler producing P_CODE for an abstract machine(P_Machine). P_Machine hypothetical stack machine designed for Pascal language. (1) Instruction --- closely related to the PASCAL language. (2) Registers┌ PC --- program counter │ NP --- new pointer │ SP --- stack pointer └ MP --- mark pointer (3) Memory ┌ CODE --- instruction part └ STORE --- data part(constant area, stack, heap)

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 21 CODEPC STORE stack heap MP current activation record SP NP constant area

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 22 Ucode  Ucode  the intermediate form used by the Stanford Portable Pascal compiler.  stack-based and is defined in terms of a hypothetical stack machine.  Ucode Interpreter : Appendix B.  Addressing  stack addressing ===> a tuple : (B, O)  B : the block number containing the address  O : the offset in words from the beginning of the block, offsets start at 1.  label  to label any Ucode instruction with a label field.  All targets of jumps and procedures must be labeled.  All labels must be unique for the entire program.

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 23  Example :  Consider the following skeleton : program main procedure P procedure Q var i : integer; j : integer;  block number  main : 1  P : 2  Q : 3  variable addressing  i : (3,1)  j : (3,2)

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 24  Ucode Operations (35 개 )  Unary--- notop, neg  Binary--- add, sub, mult, divop, modop, swp andop, orop, gt, lt, ge, le, eq, ne  Stack Operations--- lod, str, ldr, ldp  Immediate Operation--- ldc  Control Flow--- ujp, tjp, fjp, cal, ret  Range Checking--- chkh, chkl  Indirect Addressing--- ixa, sta  Procedure Specification--- proc, endop  Program Specification--- bgn  Procedure Calling Sequence--- cal  Symbol Table Information --- sym

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 25  Example :  x = a + b * c; lod1 1 /* a */ lod1 2 /* b */ lod1 3 /* c */ mult add str 1 4 /* x */  if (a>b) a = a + b; lod1 1 /* a */ lod1 2 /* b */ gt fjp next lod1 1 /* a */ lod1 2 /* b */ add str 1 1 /* a */ next

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 26  Indirect Addressing  is used to access both array elements and var parameters.  ixa --- indirect load  replace stacktop by the value of the item at location stacktop.  to retrieve A[i] : lodi /* actually (Bi, Oi)) */ ldr A /* also (block number, offset) */ add /* effective address */ ixa /* indirect load gets contents of A[i] */  to retrieve var parameter x : lod x /* loads address of actual - since x is var */ ixa /* indirect load */

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 27 sta --- indirect store –sta stores stacktop into the address at stack[stacktop-1], both items are popped. –A[i] = j; lod i ldr A add lod j sta –x := y, where x is a var parameter lod x lod y sta

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 28  Procedure Calling Sequence  procedure definition :  procedure A(var a : integer; b,c : integer);  procedure call :  A(x, expr1, expr2);  calling sequence : ldp ldr x /* load the address of actual for var parameter */ … /* code to evaluate expr1 --- left on the stack */ … /* code to evaluate expr2 --- left on the stack */ cal A

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 29  Ucode Interpreter  The Ucode interpreter is called ucodei, it’s source is on plac.dongguk.ac.kr.  The interpreter uses the following files :  *.ucode : file containing the Ucode program.  *.lst : Ucode listing and output from the program.  Ucode format label-field op-code operand-field m m+2  m is exactly enough to hold opcode.  label field --- a 10 character label( make sure its 10 characters pad with blanks )  op-code --- starts at 12 column.

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 30 Programming Assignment #3 부록 B 에 수록된 Ucode 인터프리터를 각자 PC 에 설치하고 100 이하의 소수 (prime number) 를 구하는 프로그램을 Ucode 로 작성하시오. – 다른 문제의 프로그램을 작성해서 제출해도 됨. –Ucode 인터프리터 출력 리스트를 제출. 참고 : –#1 : recursive-decent parser –#2 : MiniPascal LR parser

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 31 IL criteria –intermediate level –input language --- high level –output machine --- low level –efficient processing –translation --- source to IL, IL to target –interpretation –optimization –extensibility –external representation –clean separation –language dependence & machine dependence Concluding Remarks

PL&C Lab, DongGuk University Compiler Lecture Note, Intermediate LanguagePage 32 A : 좋다 B : 보통이다 C : 나쁘다