Programming Languages and Compilers CS 421 William Mansky

  • Slides: 19
Download presentation
Programming Languages and Compilers (CS 421) William Mansky http: //courses. engr. illinois. edu/cs 421/

Programming Languages and Compilers (CS 421) William Mansky http: //courses. engr. illinois. edu/cs 421/ Based in part on slides by Mattox Beckman, as updated by Vikram Adve, Gul Agha, Elsa Gunter, and Dennis Griffith 9/15/2020 1

The Big Picture n n n n n OCaml/Functional Programming Continuation Passing Style Type

The Big Picture n n n n n OCaml/Functional Programming Continuation Passing Style Type checking/inference Unification Lexing Parsing Operational Semantics Lambda Calculus Hoare Logic 9/15/2020 2

Anatomy of an Interpreter Program Text Lexing regular expressions/lex Token Buffer Parsing BNF grammar/yacc

Anatomy of an Interpreter Program Text Lexing regular expressions/lex Token Buffer Parsing BNF grammar/yacc AST type checking/inference, unification Static Semantics possibly Hoare Logic! Correct AST Dynamic Semantics operational semantics Program Output 9/15/2020 3

Anatomy of a Compiler Program Text Lexing regular expressions/lex Token Buffer Parsing BNF grammar/yacc

Anatomy of a Compiler Program Text Lexing regular expressions/lex Token Buffer Parsing BNF grammar/yacc AST type checking/inference, unification Static Semantics possibly Hoare Logic! Correct AST denotational semantics Translation possibly CPS! IR Optimization CS 426 Optimized IR Code Generation (denotational semantics again) Assembly/Machine Code (or lambda calculus!) 9/15/2020 4 }

What can we do now? n n n Write functional programs (which parallelize well

What can we do now? n n n Write functional programs (which parallelize well and compute math quickly) Build compilers and interpreters Specify and implement programming languages Prove programs correct Compare various approaches to PL syntax and semantics 9/15/2020 5

Formal Methods n n The study of proving programs correct But what is a

Formal Methods n n The study of proving programs correct But what is a program? n n Anything we can model formally/mathematically Computer programs, machines, workflows Compilers, air traffic control protocols, security protocols, simulators, Java programs And how do we prove it correct? n Syntax, semantics, formal logic, automatic/interactive provers 9/15/2020 6

Verifying a Compiler Program Text Lexing Token Buffer Parsing AST Static Semantics Correct AST

Verifying a Compiler Program Text Lexing Token Buffer Parsing AST Static Semantics Correct AST Translation IR Optimization Optimized IR Code Generation Assembly/Machine Code 9/15/2020 “do the same thing” 7

Verifying a Compiler Program Text Lexing Token Buffer Parsing AST have the same semantics?

Verifying a Compiler Program Text Lexing Token Buffer Parsing AST have the same semantics? Static Semantics Correct AST Translation IR Optimization Optimized IR Code Generation Assembly/Machine Code 9/15/2020 8

Verifying a Compiler Program Text Lexing Token Buffer Parsing AST Static Semantics Correct AST

Verifying a Compiler Program Text Lexing Token Buffer Parsing AST Static Semantics Correct AST have the same semantics Translation IR Optimization Optimized IR Code Generation Assembly/Machine Code 9/15/2020 9

Verifying a Compiler Input AST Semantics prove this! Translation Output AST 9/15/2020 Input Meaning

Verifying a Compiler Input AST Semantics prove this! Translation Output AST 9/15/2020 Input Meaning Semantics Output Meaning 10

Verifying a Compiler – Example Pico. ML AST Pico. ML SOS prove this! Denotation

Verifying a Compiler – Example Pico. ML AST Pico. ML SOS prove this! Denotation Lambda-term 9/15/2020 Pico. ML Value LC SOS LC Value 11

Verifying a Compiler – Example Pico. ML AST Pico. ML SOS Pico. ML Value

Verifying a Compiler – Example Pico. ML AST Pico. ML SOS Pico. ML Value prove this! Denotation Lambda-term LC SOS LC Value Can we prove this for every program? 9/15/2020 12

Verifying a Compiler – Example Pico. ML AST Pico. ML SOS prove this! Denotation

Verifying a Compiler – Example Pico. ML AST Pico. ML SOS prove this! Denotation Lambda-term Pico. ML Value LC SOS LC Value Can we prove this for every program? Yes. 9/15/2020 13

Verifying a Compiler – Harder Case Compiler IR Semantics Input Meaning prove this! Optimization

Verifying a Compiler – Harder Case Compiler IR Semantics Input Meaning prove this! Optimization Compiler IR Semantics Output Meaning Can we prove this for every program? 9/15/2020 14

Control Flow Graphs n n n ASTs are good for complex expressions What if

Control Flow Graphs n n n ASTs are good for complex expressions What if we care more about control flow than expression structure? Graphs with commands on nodes, edges showing control flow 9/15/2020 15

Control Flow Graphs Entry 0: x : = 1 seq 1: if x >

Control Flow Graphs Entry 0: x : = 1 seq 1: if x > 0 goto 4 x : = 1 2: x : = x + 1 if x > 0 3: goto 5 4: x : = x - 1 5: y : = x 6: return y seq branch seq x : = x + 1 x : = x - 1 seq goto seq branch y : = x seq return y seq Exit

Semantics on Control Flow Graphs n n n Configurations are (G, n, m), where

Semantics on Control Flow Graphs n n n Configurations are (G, n, m), where G is a graph and n is the currently executing node Small-step for moving between nodes, bigstep within a single node E. g. (Assignment Rule): label(G, n) = I : = E (E, m) v out(n) = n' (G, n, m) (G, n', m[x v]) 9/15/2020 17

Verifying a Compiler – Harder Case Input CFG Semantics prove this! Optimization Output CFG

Verifying a Compiler – Harder Case Input CFG Semantics prove this! Optimization Output CFG 9/15/2020 Input Value CFG Semantics Output Value 18

Questions? 9/15/2020 19

Questions? 9/15/2020 19