Compiler Construction

Similar documents
Compiler Construction

Compiler Construction

Compiler Construction

Compiler Construction

Compiler Construction

Compiler Construction

Compiler Construction

LECTURE 3. Compiler Phases

Compiler Construction

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

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

G Programming Languages - Fall 2012

Compiler Construction

Static Program Analysis

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

NOTE: Answer ANY FOUR of the following 6 sections:

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

Compiler Construction

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Appendix A The DL Language

Compiler Construction

Compiler Construction

CS 415 Midterm Exam Spring 2002

CS 415 Midterm Exam Spring SOLUTION

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

CS321 Languages and Compiler Design I. Winter 2012 Lecture 1

System Software Assignment 1 Runtime Support for Procedures

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Semantics and Verification of Software

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

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

Lecture08: Scope and Lexical Address

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

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

LECTURE 18. Control Flow

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

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

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

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

low and not larger than high. 18 points

CS5363 Final Review. cs5363 1

MIDTERM EXAM (Solutions)

CSE 3302 Notes 5: Memory Management

G Programming Languages - Fall 2012

Static Program Analysis

Static Program Analysis

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

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

Programming Languages and Compiler Design

The SPL Programming Language Reference Manual

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

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

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

Semantic Analysis and Type Checking

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

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

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

Application: Programming Language Semantics

Run-time Environments - 2

Lecture 12: Conditional Expressions and Local Binding

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

Semantic Analysis. Compiler Architecture

Informatica 3 Syntax and Semantics

Intermediate Representations & Symbol Tables

INF 212 ANALYSIS OF PROG. LANGS ELEMENTS OF IMPERATIVE PROGRAMMING STYLE. Instructors: Crista Lopes Copyright Instructors.

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

Statement level control structures

Chapter 10 Implementing Subprograms

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

What about Object-Oriented Languages?

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

CST-402(T): Language Processors

G Programming Languages - Fall 2012

Big-step Operational Semantics (aka Natural Semantics)

The PCAT Programming Language Reference Manual

Introduction to Programming Using Java (98-388)

Compilers and Interpreters

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

1 Little Man Computer

Implementing Subprograms

CSE 307: Principles of Programming Languages

Syntax Errors; Static Semantics

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

Compiler construction

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

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

CS 415 Midterm Exam Fall 2003

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

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

CS 4100 Block Structured Languages. Fixed vs Variable. Activation Record. Activation Records. State of an Activation

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Compiler Construction

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

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

Chapter 1. Fundamentals of Higher Order Programming

CS 314 Principles of Programming Languages

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

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Formal Syntax and Semantics of Programming Languages

Transcription:

Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-16/cc/

Seminar Analysis and Verification of Pointer Programs (WS 2016/17) Problem: Pointer Errors Dereferencing invalid pointers Creation of memory leaks Invalidation of data structures Deadlocks Data races,... Solution: Abstraction https://xkcd.com/371 Automata-based: regular model checking, forest automata Logic-based: shape analysis, separation logic Graph-based: graph grammars, graph transformation systems Extensions for concurrency Registration via https://www.graphics.rwth-aachen.de/apse/ 2 of 26 Compiler Construction

Generation of Intermediate Code Conceptual Structure of a Compiler Source code Lexical analysis (Scanner) Syntax analysis (Parser) Semantic analysis Generation of intermediate code LOAD y2; LIT 1; ADD; STO x1 Code optimisation int Var Asg ok int Var Exp int Sum int Con int tree translations Generation of target code Target code 4 of 26 Compiler Construction

Generation of Intermediate Code Modularisation 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 optimisation: division into machine-independent and machine-dependent parts 5 of 26 Compiler Construction

Generation of Intermediate Code Modularisation 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. PL m MC n only n + m translations (in place of n 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) 6 of 26 Compiler Construction

Generation of Intermediate Code 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 (branching, 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 7 of 26 Compiler Construction

Generation of Intermediate Code 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 8 of 26 Compiler Construction

The Example Programming Language EPL 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) 10 of 26 Compiler Construction

The Example Programming Language EPL 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 : 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 ; Blk : K ::= D C Pgm : P ::= in/out I 1,...,I n ;K. 11 of 26 Compiler Construction

The Example Programming Language EPL 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. 12 of 26 Compiler Construction

Semantics of EPL Scoping and Visibility of Identifiers Scope of an identifier The scope (or: range of validity/visibility) of an identifier s declaration refers to the part of the program where a usage of that identifier can refer to that declaration. Important aspects: Static (aka lexical ) scoping: name resolution at compile time, depends on location in source code and lexical context (here) part = area of source code (block) usually admits at most one declaration of same identifier per block but allows additional declarations within nested blocks innermost principle: hide outer declarations (still valid, but not visible) Dynamic scoping: name resolution at runtime, depends on execution context part = runtime history, esp. procedure calls Levels: expression/block/procedure/file/module/... 14 of 26 Compiler Construction

Semantics of EPL Static Semantics of EPL I Usage of identifiers must be consistent with declaration: I :=... J... = I variable, J constant or variable I() = procedure 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). 15 of 26 Compiler Construction

Semantics of EPL 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: use of x in main program Innermost principle: use of z in Q Static scoping: call of P in Q can refer to x, y, z Later declaration: call of R in P followed by declaration (in older languages: forward declarations for one-pass compilation) 16 of 26 Compiler Construction

Semantics of EPL Dynamic Semantics of EPL (omitting the details; cf. Semantics of Programming Languages) To run program, execute main block in state (location value) determined by input values Effect of declaration = modification of environment constant value variable location procedure state transformation 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 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) 17 of 26 Compiler Construction

Intermediate Code for EPL The Abstract Machine AM Definition 15.6 (Abstract machine for EPL) The abstract machine for EPL (AM) is defined by the state space S := PC DS PS with the program counter PC := N, 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 = (pc, d, p) S is given by a program counter pc PC, a data stack d = d.r :... : d.1 DS, and a procedure stack p = p.1 :... : p.t PS. 19 of 26 Compiler Construction

Intermediate Code for EPL 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) 20 of 26 Compiler Construction

Intermediate Code for EPL Semantics of Instructions Definition 15.8 (Semantics of AM instructions (1st part)) The semantics of an AM instruction O is defined as follows: O : S S ADD (pc, d : z 1 : z 2, p) := (pc + 1, d : z 1 + z 2, p) NOT (pc, d : b, p) := (pc + 1, d : b, p) if b {0, 1} AND (pc, d : b 1 : b 2, p) := (pc + 1, d : b 1 b 2, p) if b 1, b 2 {0, 1} OR (pc, d : b 1 : b 2, p) := {(pc + 1, d : b 1 b 2, p) if b 1, b 2 {0, 1} (pc + 1, d : 1, p) if z1 < z LT (pc, d : z 1 : z 2, p) := 2 (pc + 1, d : 0, p) if z 1 z 2 JMP(ca) (pc, d, p) := (ca, d, p) JFALSE(ca) (pc, d : b, p) := { (ca, d, p) if b = 0 (pc + 1, d, p) if b = 1 21 of 26 Compiler Construction

The Procedure Stack 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 sl : dl : ra : v 1 :... : v k where 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 counter after termination of procedure call = used to continue program execution after termination of procedure call local variables v i : values of locally declared variables 23 of 26 Compiler Construction

The Procedure Stack 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) 24 of 26 Compiler Construction

The Procedure Stack 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 sl dl ra y z sl dl ra x z sl dl ra y z sl dl ra y sl dl ra x P() Q() P() MAIN I/O 25 of 26 Compiler Construction

The Procedure Stack 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 sl dl ra y z sl dl ra x z sl dl ra y z sl dl ra y sl dl ra x P() Q() P() MAIN I/O P uses x = dif = 2 P uses y = dif = 0 26 of 26 Compiler Construction