21
1 June 24, 2022 1 June 24, 2022 June 24, 2022 Azusa, Azusa, CA CA Sheldon X. Liang Ph. D. Computer Science at Computer Science at Azusa Azusa Pacific University Pacific University Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/ CS400 Compiler Construction CS400 Compiler Construction

1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

Embed Size (px)

DESCRIPTION

3 The Structure of our Compiler Revisited February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

Citation preview

Page 1: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

1

May 4, 20231

May 4, 2023May 4, 2023 Azusa, CAAzusa, CA

Sheldon X. Liang Ph. D.

Computer Science at Computer Science at Azusa Pacific UniversityAzusa Pacific University

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS400 Compiler ConstructionCS400 Compiler Construction

Page 2: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

2

Static Checking and Type Systems

COP5621 Compiler ConstructionCopyright Robert van Engelen, Florida State University, 2007

May 4, 20232

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Semantic Analysis

Page 3: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

3

The Structure of our Compiler Revisited

May 4, 20233

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Page 4: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

4

May 4, 20234

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Keep in mind following questionsKeep in mind following questions

• Semantic Checking– Static– Dynamic– One/multi pass checking

• Type expressions– Graph representations– Name equivalence – Structural equivalence

• Constructing graphs by Yacc– Construct type nodes– SDD – attributed type production – Yacc: auto tool for constructing

Page 5: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

5

Static versus Dynamic Checking

• Static checking: the compiler enforces programming language’s static semantics– Program properties that can be checked at compile

time• Dynamic semantics: checked at run time

– Compiler generates verification code to enforce programming language’s dynamic semantics

May 4, 20235

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Page 6: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

6

Static Checking

• Typical examples of static checking are– Type checks– Flow-of-control checks– Uniqueness checks– Name-related checks

May 4, 20236

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Page 7: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

7

Type Checks, Overloading, Coercion, and Polymorphism

int op(int), op(float);int f(float);int a, c[10], d;

d = c+d; // FAIL --> access c as an address*d = a; // FAIL --> d is not pointera = op(d); // OK: overloading (C++)a = f(d); // OK: coersion of d to floatvector<int> v; // OK: template instantiation

May 4, 20237

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

• Type conversion is explicit, for example using type casts• Type coercion is implicitly performed by the compiler

Page 8: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

8

Flow-of-Control Checks

myfunc(){ … break; // ERROR}

myfunc(){ … switch (a) { case 0: … break; // OK case 1: … }}

myfunc(){ … while (n) { … if (i>10) break; // OK }}

May 4, 20238

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Page 9: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

9

Uniqueness Checks

myfunc(){ int i, j, i; // ERROR …}

struct myrec{ int name;};struct myrec // ERROR{ int id;};

May 4, 20239

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

cnufym(int a, int a) // ERROR{ …}

Page 10: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

10

Name-Related Checks

LoopA: for (int I = 0; I < n; I++) { … if (a[I] == 0) break LoopB; // Java labeled loop … }

May 4, 202310

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Page 11: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

11

One-Pass versus Multi-Pass Static Checking

• One-pass compiler: static checking for C, Pascal, Fortran, and many other languages is performed in one pass while intermediate code is generated– Influences design of a language: placement constraints

• Multi-pass compiler: static checking for Ada, Java, and C# is performed in a separate phase, sometimes by traversing the syntax tree multiple times

May 4, 202311

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Page 12: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

12

Type Expressions

• Type expressions are used in declarations and type casts to define or refer to a type– Primitive types, such as int and bool– Type constructors, such as pointer-to, array-of,

records and classes, templates, and functions– Type names, such as typedefs in C and named

types in Pascal, refer to type expressions

May 4, 202312

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Page 13: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

13

Graph Representations for Type Expressions

int *f(char*,char*)

fun

args pointer

char

intpointer

char

pointer

Tree forms

fun

args pointer

char

intpointer

DAGsMay 4, 2023

13Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Page 14: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

14

Cyclic Graph Representations

struct Node{ int val; struct Node *next;};

struct

val

pointerint

Cyclic graph

next

May 4, 202314

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Page 15: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

15

Name Equivalence

• Each type name is a distinct type, even when the type expressions the names refer to are the same

• Types are identical only if names match• Used by Pascal (inconsistently)

type link = ^node;var next : link; last : link; p : ^node; q, r : ^node;

With name equivalence in Pascal:p ≠ nextp ≠ lastp = q = rnext = last

May 4, 202315

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Page 16: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

16

Structural Equivalence of Type Expressions

• Two types are the same if they are structurally identical

• Used in C, Java, C#

=

May 4, 202316

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Page 17: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

17

Structural Equivalence of Type Expressions (cont’d)

• Two structurally equivalent type expressions have the same pointer address when constructing graphs by sharing nodes

struct Node{ int val; struct Node *next;};struct Node s, *p;

… p = &s; // OK… *p = s; // OK

May 4, 202317

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Page 18: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

18

Constructing Type Graphs in Yacc

Type *mkint() construct int node if not alreadyconstructed

Type *mkarr(Type*,int) construct array-of-type nodeif not already constructed

Type *mkptr(Type*) construct pointer-of-type nodeif not already constructed

May 4, 202318

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Page 19: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

19

Syntax-Directed Definitions for Constructing Type Graphs in Yacc%union{ Symbol *sym; int num; Type *typ;}%token INT%token <sym> ID%token <int> NUM%type <typ> type%%decl : type ID { addtype($2, $1); } | type ID ‘[’ NUM ‘]’ { addtype($2, mkarr($1, $4)); } ;type : INT { $$ = mkint(); } | type ‘*’ { $$ = mkptr($1); } | /* empty */ { $$ = mkint(); } ;

May 4, 202319

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Page 20: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

20

May 4, 202320

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Got it with following questionsGot it with following questions

• Semantic Checking– Static– Dynamic– One/multi pass checking

• Type expressions– Graph representations– Name equivalence – Structural equivalence

• Constructing graphs by Yacc– Construct type nodes– SDD – attributed type production – Yacc: auto tool for constructing

Page 21: 1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University

21

Thank you very much!

Questions?

May 4, 202321

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction