Compiler Construction

Similar documents
Compiler Construction

Compiler Construction

Compiler Construction

Compiler Construction

Compiler Construction

Compiler Construction

Compiler Construction

Compiler Construction

Compiler Construction

Compiler Construction

LECTURE 3. Compiler Phases

CS321 Languages and Compiler Design I. Winter 2012 Lecture 1

Compiler Construction

NOTE: Answer ANY FOUR of the following 6 sections:

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

G Programming Languages - Fall 2012

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Compiler Construction Lecture 1: Introduction Winter Semester 2018/19 Thomas Noll Software Modeling and Verification Group RWTH Aachen University

G Programming Languages - Fall 2012

Appendix A The DL Language

Static Program Analysis

2. Reachability in garbage collection is just an approximation of garbage.

Compiler Construction Lecture 1: Introduction Summer Semester 2017 Thomas Noll Software Modeling and Verification Group RWTH Aachen University

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

Static Program Analysis

Virtual Machine. Part I: Stack Arithmetic. Building a Modern Computer From First Principles.

Semantics and Verification of Software

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

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

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

MIDTERM EXAM (Solutions)

LECTURE 18. Control Flow

Lecture 31: Building a Runnable Program

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

Run-time Environments -Part 1

Run-time Environments - 2

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

CSE 3302 Notes 5: Memory Management

Programming Languages and Compiler Design

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

Compiler Construction

1 Little Man Computer

UNIT 3

Lecture 12: Conditional Expressions and Local Binding

Introduction to Programming Using Java (98-388)

Today's Topics. CISC 458 Winter J.R. Cordy

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Virtual Machine Where we are at: Part I: Stack Arithmetic. Motivation. Compilation models. direct compilation:... 2-tier compilation:

Application: Programming Language Semantics

Denotational Semantics

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

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Chapter 10 Implementing Subprograms

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

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

System Software Assignment 1 Runtime Support for Procedures

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Final CSE 131B Spring 2004

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee

Statement level control structures

Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University

CS5363 Final Review. cs5363 1

Implementing Subprograms

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

4. An interpreter is a program that

Informatica 3 Syntax and Semantics

CS 415 Midterm Exam Spring 2002

Static Program Analysis

CSC488S/CSC2107S - Compilers and Interpreters. CSC 488S/CSC 2107S Lecture Notes

low and not larger than high. 18 points

Implementing Algorithms in MIPS Assembly

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

CS 314 Principles of Programming Languages

Compiler Construction

Run-time Environments

Run-time Environments

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

Programming Languages: Lecture 12

CSC488S/CSC2107S - Compilers and Interpreters. CSC 488S/CSC 2107S Lecture Notes

Intermediate Representations & Symbol Tables

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Compiler Construction

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

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

Big-step Operational Semantics (aka Natural Semantics)

CS 415 Midterm Exam Fall 2003

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

Principles of Programming Languages. Lecture Outline

Mosig M1 - PLSCD Written exam

Language Proposal: tail

Overview of Compiler. A. Introduction

Chapter 10. Implementing Subprograms ISBN

CS 415 Midterm Exam Spring SOLUTION

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

Transcription:

Compiler Construction Lecture 15: Code Generation I (Intermediate Code) Thomas Noll Lehrstuhl für Informatik 2 (Software Modeling and Verification) noll@cs.rwth-aachen.de http://moves.rwth-aachen.de/teaching/ss-14/cc14/ Summer Semester 2014

Outline 1 Generation of Intermediate Code 2 The Example Programming Language EPL 3 Semantics of EPL 4 Intermediate Code for EPL 5 The Procedure Stack Compiler Construction Summer Semester 2014 15.2

Conceptual Structure of a Compiler Source code Lexical analysis (Scanner) Syntax analysis (Parser) Semantic analysis Generation of intermediate code Code optimization Assgnok intvar Expint Sumint intvar Constint tree translations LOAD y2; LIT 1; ADD; STO x1 Generation of machine code Target code Compiler Construction Summer Semester 2014 15.3

Modularization of Code Generation I Splitting of code generation for programming language PL: PL trans IC code MC Frontend: trans generates machine-independent intermediate code (IC) for abstract (stack) machine Backend: code generates actual machine code (MC) Advantages: IC machine independent = Portability: much easier to write IC compiler/interpreter for a new machine (as opposed to rewriting the whole compiler) Fast compiler implementation: generating IC much easier than generating MC Code size: IC programs usually smaller than corresponding MC programs Code optimization: division into machine-independent and machine-dependent parts Compiler Construction Summer Semester 2014 15.4

Modularization of Code Generation II Example 15.1 1 UNiversal Computer-Oriented Language (UNCOL; 1960; http://en.wikipedia.org/wiki/uncol): universal intermediate language for compilers (never fully specified or implemented; too ambitious) PL 1. UNCOL MC 1. only n+m translations (in place of n m) PL n MC m 2 Pascal s pseudocode (P-code; 1975; http://en.wikipedia.org/wiki/p-code_machine) 3 The Amsterdam Compiler Kit (TACK; 1980; http://tack.sourceforge.net/) 4 Java Virtual Machine (JVM; Sun; 1996; http://en.wikipedia.org/wiki/java_virtual_machine) 5 Common Intermediate Language (CIL; Microsoft.NET; 2002; http://en.wikipedia.org/wiki/common_intermediate_language) Compiler Construction Summer Semester 2014 15.5

Language Structures I Structures in high-level programming languages: Basic data types and basic operations Static and dynamic data structures Expressions and assignments Control structures (sequences, branching statements, loops,...) Procedures and functions Modularity: blocks, modules, and classes Use of procedures and blocks: FORTRAN: non-recursive and non-nested procedures = static memory management (requirements determined at compile time) C: recursive and non-nested procedures = dynamic memory management using runtime stack (requirements only known at runtime), no static links Algol-like languages (Pascal, Modula): recursive and nested procedures = dynamic memory management using runtime stack with static links Object-oriented languages (C++, Java): object creation and removal = dynamic memory management using heap Compiler Construction Summer Semester 2014 15.6

Language Structures II Structures in machine code: (von Neumann/SISD) Memory hierarchy: accumulators, registers, cache, main memory, background storage Instruction types: arithmetic/boolean/... operation, test/jump instruction, transfer instruction, I/O instruction,... Addressing modes: direct/indirect, absolute/relative,... Architectures: RISC (few [fast but simple] instructions, many registers), CISC (many [complex but slow] instructions, few registers) Structures in intermediate code: Data types and operations like PL Data stack with basic operations Jumping instructions for control structures Runtime stack for blocks, procedures, and static data structures Heap for dynamic data structures Compiler Construction Summer Semester 2014 15.7

Outline 1 Generation of Intermediate Code 2 The Example Programming Language EPL 3 Semantics of EPL 4 Intermediate Code for EPL 5 The Procedure Stack Compiler Construction Summer Semester 2014 15.8

The Example Programming Language EPL Structures of EPL: Only integer and Boolean values Arithmetic and Boolean expressions with strict and non-strict semantics Control structures: sequence, branching, iteration Nested blocks and recursive procedures with local and global variables ( = dynamic memory management using runtime stack with static links) (not considered: procedure parameters and [dynamic] data structures) Compiler Construction Summer Semester 2014 15.9

Syntax of EPL Definition 15.2 (Syntax of EPL) The syntax of EPL is defined as follows: Z : z (* z is an integer *) Ide : I (* I is an identifier *) AExp : A ::= z I A 1 + A 2... BExp : B ::= A 1 < A 2 not B B 1 and B 2 B 1 or B 2 Cmd : C ::= I := A C 1 ;C 2 if B then C 1 else C 2 while B do C I() Dcl : Blk : Pgm : D ::= D C D V D P D C ::= ε const I 1 := z 1,...,I n := z n ; D V ::= ε var I 1,...,I n ; D P ::= ε proc I 1 ;K 1 ;...;proc I n ;K n ; K ::= D C P ::= in/out I 1,...,I n ;K. Compiler Construction Summer Semester 2014 15.10

EPL Example: Factorial Function Example 15.3 (Factorial function) in/out x; var y; proc F; if x > 1 then y := y * x; x := x - 1; F() y := 1; F(); x := y. Compiler Construction Summer Semester 2014 15.11

Outline 1 Generation of Intermediate Code 2 The Example Programming Language EPL 3 Semantics of EPL 4 Intermediate Code for EPL 5 The Procedure Stack Compiler Construction Summer Semester 2014 15.12

Static Semantics of EPL I All identifiers in a declaration D have to be different. Every identifier occurring in the command C of a block D C must be declared in D or in the declaration list of a surrounding block. Multiple declarations of an identifier in different blocks are possible. Each usage in a command C refers to the innermost declaration. Static scoping: the usage of an identifier in the body of a called procedure refers to its declaration environment (and not to its calling environment). Compiler Construction Summer Semester 2014 15.13

Static Semantics of EPL II Example 15.4 in/out x; const c = 10; var y; proc P; var y, z; proc Q; var x, z; [... z := 1; P()...] [... P()... R()...] proc R; [... P()...] [... x := 0; P()...]. Innermost principle Static scoping: body of P can refer to x, y, z Later declaration: call of R in P followed by declaration (in Pascal: forward declarations for one-pass compilation) Compiler Construction Summer Semester 2014 15.14

Dynamic Semantics of EPL (omitting the details) To run a program, execute the main block in the state which is given by the input values Effect of statement = modification of state assignment I := A: update of I by current value of A composition C 1 ;C 2 : sequential execution branching if B then C 1 else C 2 : test of B, followed by jump to respective branch iteration while B do C: execution of C as long as B is true call I(): transfer control to body of I and return to subsequent statement afterwards Consequently, an EPL program P = in/out I 1,...,I n ;K. Pgm has as semantics a function P : Z n Z n Example 15.5 (Factorial function; cf. Example 15.3) here n = 1 and P (x) = x! (where x! := 1 for x 1) Compiler Construction Summer Semester 2014 15.15

Outline 1 Generation of Intermediate Code 2 The Example Programming Language EPL 3 Semantics of EPL 4 Intermediate Code for EPL 5 The Procedure Stack Compiler Construction Summer Semester 2014 15.16

The Abstract Machine AM Definition 15.6 (Abstract machine for EPL) The abstract machine for EPL (AM) is defined by the state space with the program counter PC := N, S := PC DS PS the data stack DS := Z (top of stack to the right), and the procedure stack (or: runtime stack) PS := Z (top of stack to the left). Thus a state s = (l,d,p) S is given by a program label l PC, a data stack d = d.r :... : d.1 DS, and a procedure stack p = p.1 :... : p.t PS. Compiler Construction Summer Semester 2014 15.17

AM Instructions Definition 15.7 (AM instructions) The set of AM instructions is divided into arithmetic instructions: ADD, MULT,... Boolean instructions: NOT, AND, OR, LT,... jumping instructions: JMP(ca), JFALSE(ca) (ca PC) procedure instructions: CALL(ca,dif,loc) (ca PC, dif,loc N), RET transfer instructions: LOAD(dif,off), STORE(dif,off) (dif, off N), LIT(z) (z Z) Compiler Construction Summer Semester 2014 15.18

Semantics of Instructions Definition 15.8 (Semantics of AM instructions (1st part)) The semantics of an AM instruction O O : S S is defined as follows: ADD (l,d : z 1 : z 2,p) := (l +1,d : z 1 +z 2,p) NOT (l,d : b,p) := (l +1,d : b,p) if b {0,1} AND (l,d : b 1 : b 2,p) := (l +1,d : b 1 b 2,p) if b 1,b 2 {0,1} OR (l,d : b 1 : b 2,p) := {(l +1,d : b 1 b 2,p) if b 1,b 2 {0,1} (l +1,d : 1,p) if z1 < z LT (l,d : z 1 : z 2,p) := 2 (l +1,d : 0,p) if z 1 z 2 JMP(ca) (l,d,p) := (ca,d,p) JFALSE(ca) (l,d : b,p) := { (ca,d,p) if b = 0 (l +1,d,p) if b = 1 Compiler Construction Summer Semester 2014 15.19

Outline 1 Generation of Intermediate Code 2 The Example Programming Language EPL 3 Semantics of EPL 4 Intermediate Code for EPL 5 The Procedure Stack Compiler Construction Summer Semester 2014 15.20

Structure of Procedure Stack I The semantics of procedure and transfer instructions requires a particular structure of the procedure stack p PS: it must be composed of frames (or: activation records) of the form where sl : dl : ra : v 1 :... : v k static link sl: points to frame of surrounding declaration environment = used to access non-local variables dynamic link dl: points to previous frame (i.e., of calling procedure) = used to remove topmost frame after termination of procedure call return address ra: program label after termination of procedure call = used to continue program execution after termination of procedure call local variables v i : values of locally declared variables Compiler Construction Summer Semester 2014 15.21

Structure of Procedure Stack II Frames are created whenever a procedure call is performed Two special frames: I/O frame: for keeping values of in/out variables (sl = dl = ra = 0) MAIN frame: for keeping values of top-level block (sl = dl = I/O frame) Compiler Construction Summer Semester 2014 15.22

Structure of Procedure Stack III Example 15.9 (cf. Example 15.4) in/out x; const c = 10; var y; proc P; var y, z; proc Q; var x, z; [... P()...] [... Q()...] proc R; [... P()...] [... P()...]. Procedure stack after second call of P: 15 4 5 4 5 4 4 3 0 0 0 y z x z y z y sl dl ra P() Q() P() MAIN I/O x Compiler Construction Summer Semester 2014 15.23

Structure of Procedure Stack IV Observation: The usage of a variable in a procedure body refers to its innermost declaration. If the level difference between the usage and the declaration is dif, then a chain of dif static links has to be followed to access the corresponding frame. Example 15.10 (cf. Example 15.9) in/out x; const c = 10; var y; proc P; var y, z; proc Q; var x, z; [... P()...] [... x... y... Q()...] proc R; [... P()...] [... P()...]. Procedure stack after second call of P: 15 4 5 4 5 4 4 3 0 0 0 y z x z y z y sl dl ra x P() Q() P() MAIN I/O P uses x = dif = 2 P uses y = dif = 0 Compiler Construction Summer Semester 2014 15.24