CS A331 Programming Language Concepts

Similar documents
Chapter 4 :: Semantic Analysis

Chapter 4. Action Routines

Programming Languages

Chapter 4 - Semantic Analysis. June 2, 2015

Semantic Analysis. Role of Semantic Analysis

Why are there so many programming languages?

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

Early computers (1940s) cost millions of dollars and were programmed in machine language. less error-prone method needed

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

Programming Languages, Summary CSC419; Odelia Schwartz

CS 3304 Comparative Languages. Lecture 1: Introduction

Working of the Compilers

CS A331 Programming Language Concepts

Compilers and interpreters

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Concepts in Programming Languages

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

CSCI 3136 Principles of Programming Languages

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

Topic I. Introduction and motivation References: Chapter 1 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

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

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

Welcome to CSE131b: Compiler Construction

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

Programming Languages

Semantic Analysis Attribute Grammars

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

CSE A215 Assembly Language Programming for Engineers

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

CS 314 Principles of Programming Languages

Summary: Semantic Analysis

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

Concepts of Programming Languages

Introduction. A. Bellaachia Page: 1

Principles in Programming: Orientation & Lecture 1. SWE2004: Principles in Programming Spring 2014 Euiseong Seo

CS101 Introduction to Programming Languages and Compilers

Static and Dynamic Semantics

LECTURE 1. Overview and History

CST-402(T): Language Processors

Computer Organization & Assembly Language Programming (CSE 2312)

Programmiersprachen (Programming Languages)

CS558 Programming Languages

10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

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

IA010: Principles of Programming Languages

Principles of Programming Languages [PLP-2015] Detailed Syllabus

Comp 333: Concepts of Programming Languages Fall 2016

What do Compilers Produce?

! Broaden your language horizons. ! Study how languages are implemented. ! Study how languages are described / specified

INTRODUCTION PRINCIPLES OF PROGRAMMING LANGUAGES. Norbert Zeh Winter Dalhousie University 1/10

Compilation I. Hwansoo Han

Com S 541. Programming Languages I

CSCI.4430/6969 Programming Languages Lecture Notes

Lecture 09. Ada to Software Engineering. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

Compiler Construction

A Tour of Language Implementation

Undergraduate Compilers in a Day

CSE A215 Assembly Language Programming for Engineers

22c:111 Programming Language Concepts. Fall Syntax III

Crafting a Compiler with C (II) Compiler V. S. Interpreter

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

Lecture 3: Instruction Set Architecture

Introduction to Programming Languages. CSE 307 Principles of Programming Languages Stony Brook University

Type Inference. Prof. Clarkson Fall Today s music: Cool, Calm, and Collected by The Rolling Stones

Chapter 3 (part 3) Describing Syntax and Semantics

Formats of Translated Programs

Semantic Analysis. Lecture 9. February 7, 2018

Control Flow February 9, Lecture 7

Principles in Programming: Orientation & Lecture 1. SWE2004: Principles in Programming Spring 2015 Euiseong Seo

What Is Computer Science? The Scientific Study of Computation. Expressing or Describing

Language Translation, History. CS152. Chris Pollett. Sep. 3, 2008.

CS 415 Midterm Exam Spring 2002

1 A question of semantics

LECTURE 3. Compiler Phases

CS508-Modern Programming Solved MCQ(S) From Midterm Papers (1 TO 22 Lectures) BY Arslan

COMPILER DESIGN LECTURE NOTES

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

Syntax-Directed Translation. Introduction

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Chapter 3. Describing Syntax and Semantics

CS 242. Fundamentals. Reading: See last slide

SOFTWARE ARCHITECTURE 6. LISP

CS Compiler Construction West Virginia fall semester 2014 August 18, 2014 syllabus 1.0

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Programming Language Concepts 1982, 1987, Outline. Period

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

Lecture 1: Course Introduction

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Types and Static Type Checking (Introducing Micro-Haskell)

CS415 Compilers Overview of the Course. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Chapter 1. Preview. Reason for Studying OPL. Language Evaluation Criteria. Programming Domains

CS 314 Principles of Programming Languages

1/14/2014. Introduction to CSE 1325 Object Oriented Programming (Using Java) Introduction (Cont.) Introduction

Which of the following is not true of FORTRAN?

G Programming Languages - Fall 2012

Programming Languages 2nd edition Tucker and Noonan"

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Program Analysis ( 软件源代码分析技术 ) ZHENG LI ( 李征 )

Types and Static Type Checking (Introducing Micro-Haskell)

Transcription:

CS A331 Programming Language Concepts Lecture 4 Programming Language Semantics and Code Generation February 3, 2014 Sam Siewert

PLP Companion Materials CD-ROM is On-Line: http://booksite.elsevier.com/9780123745149/?isbn=978 0123745149 http://booksite.elsevier.com/9780123745149/appendices /CD-ROM_Menu_Bonus_Content_and_Exercises.pdf Has Great examples and Extra Materials I have downloaded PLP_3e_CD.zip and Code_Original.zip and placed on Blackboard for you Sam Siewert 2

Digging Deeper History and Evolution PLP Mentions Many Historical Programming Languages and Designers E.g. Niklaus Wirth (Language Designer for Pascal, Oberon and many other Programming Languages) PL/0 Less is More Concept in General for Programming Popular Pedagogical Languages like Pascal See PLP Page 820 for Genealogy of Programming Languages See PLP Pages 821-829 for High Level Descriptions Sam Siewert 3

Simpler Semantics Motivation for Many Programming Languages John Backus (FORTRAN, 1957) Conventional programming languages are growing ever more enormous, but not stronger. 1977 ACM Turing Award Lecture FORTAN 77 But, today, we have Fortran 90, 95, 2003, 2008, 2015 One Might Ask, Have we Made Progress? Sam Siewert 4

Genealogy of Programming Languages Imperative Compiled: Algol, C, Ada, Fortran, Basic Scripted: C-shell, Perl, Python, Ruby, PHP Object Oriented: Smalltalk, Eiffel, C++, Java, Python, Ruby Declarative Functional: Lisp, Scheme, ML, Haskell Logical: Prolog Michael L. Scott, "Programming Language Pragmatics, 3rd Ed.", Morgan Kaufman, 2009. Sam Siewert 5

Semantics Syntax and Semantics Informal - the field concerned with the rigorous mathematical study of the meaning of programming languages. It does so by evaluating the meaning of syntactically legal strings defined by a specific programming language [Wikipedia - Semantics (computer science)] Formal Code generation to implement the meaning of a program (construct), pp. 29-35 in PLP Textbook Need Methods of Semantic Analysis (To Go With Syntactic Analysis) Sam Siewert 6

Where Semantic Analysis Fits In Following Parsing Semantic Analysis Intermediate Code Generation Purpose Enforce STATIC semantic rules e.g. formal to actual argument matching and checking for function calls Prepares compilers for code generation (back-end) Links front-end parser to back-end code generator DYNAMIC semantic rules Supported by run-time or Programmer Assertions and Exception Handling 1. Front-end: Abstract Syntax Tree with Attributes Parse Tree for CFG (Review Pages 30 to 33) 2. Intermediate: Semantic Analysis (GCC Annotation, RTL) AST 3. Back-end: Semantic Analysis and Intermediate Code Target Code Generation GCC Pass Summary Sam Siewert 7

Attribute Grammars PLP - Decoration and Annotation of a Syntax Tree Extend to define Attribute Grammar Production Semantic Rules = Attribute Grammar Semantic Rules {copy rules, semantic functions} CFG Productions Rules for Symbols Type 1) E E + T E1.val = E2.val + T.val Funct 2) E E T E1.val = E2.val - T.val Funct 3) E T E.val = T.val Copy 4) T T * F T1.val = T2.val * F.val Funct 5) T T / F T1.val = T2.val / F.val Funct 6) T F T.val = F.val Copy 7) F - F F1.val = - F2.val Funct 8) F (E) F.val = E.val Copy 9) F const F.val = C.val Copy Sam Siewert 8

Decoration of the Parse Tree AST (Abstract Syntax Tree) = Simplified Parse Tree + Attributes (PLP Page 183) Using Attribute Grammar on Previous Slide (Bottom-Up) 1. Attribute Flow (Arrows) 2. Box Holds Output of Semantic Rule 3. Inputs to Rule 1. 2 Arrows Function 2. 1 Arrow Copy S-Attributed: Synthesized (Calculate Only LHS [Left-Hand Side] Symbols in Productions) Sam Siewert (1+3)*2 9

Inherited Attributes More Complex than S-Attributed Calculation of Production Symbol Values on RHS See Example on Pages 184-185 (LL Grammar) 9-4 - 3 Sam Siewert 10

Not Just Bottom-Up, but Left-to-Right A More Complex Decorated Parse Tree for LL AG (1 + 3) * 2 Sam Siewert 11

Action Routines There are automatic tools that generate translation schemes for context-free grammars which describe the possible structure of a syntax tree An action routine is a semantic function that we tell the compiler to execute at a particular point in the parse These tools are heavily used in syntax-based editors and incremental compilers Most ordinary compilers, however, use ad-hoc techniques Sam Siewert 12

Decorating a Syntax Tree Syntax Tree vs. Parse Tree (Pages 30-33) Removes Artificial Nodes (parsing productions) Gory Detail Annotates Tree with Attributes Sam Siewert 13

We Need Full AG to Contstruct Note that all Productions have more than one semantic rule Semantic error handling rules included Sam Siewert 14

PLP Chapter 5 Code Generation Sam Siewert 15

Code Generation Typically Last Pass of the Back-End Highly Tuned to the Target Architecture Translation from IR to Assembly / Machine Code ASM is 1-to-1 with Machine Code Recommended Approach Test what GCC generates Compare to Hand Written Assembly Learn to Check Code Generation Compiler Bugs Optimization and Efficiency Sam Siewert 16

Most Common ASM / Machine Code MIPS Simple RISC Instruction Set Architectutre Basis for Computer Organization and Design, Fourth Edition: The Hardware/Software Interface by David A. Patterson and John L. Hennessy, from Amazon Widely Used in 64-bit Network Processors (E.g. Cavium Octeon) Used in Embedded Systems (E.g. Microchip PIC32) ARM RISC Instruction Set with Unique DSP Features Most Mobile Devices are ARM NVIDIA, Qualcomm, Apple, Amazon Kindle, etc. Most Android Devices are ARM Widely Used in Embedded Systems (E.g. TI-OMAP) 32-bit and 64-bit Versions Intel Architecture IA32 and IA64 Macintosh, Netbook, Laptop, Desktop, Server Mobile Atom 32-bit and 64-bit to Compete with ARM Sam Siewert 17

IF, ID, Execute, (Mem), WB RISC ISA (Instruction Set Architecture) Typically 4-Stage or 5-Stage Pipeline ARM9 v5te ISA 5-Stage* MIPS 5-Stage Sam Siewert * The ARM Architecture, Leonid Ryzhyk, June 5, 2006. 18

Memory Hierarchy Impacts Performance Significantly for Array Operations Michael L. Scott, "Programming Language Pragmatics, 3rd Ed.", Morgan Kaufman, 2009. Sam Siewert 19

E.g. Data Representation Data Access is Expensive Cache Helps, but Misses Stall the Pipeline A Major Concern for Code Generation C/C++: Row Major [a..b, c d] is the same as [a b] of array [c d] Fortran: Column Major A[4,0] A[7,0] Sam Siewert 20

ARM Conditional Execution Conditional Execution Can Be Applied to Every Instruction in ARM Reduced Instruction Count Significantly.text.align 2.global gcda2.type gcda2, %function gcda2: mov ip, sp stmfd sp!, {fp, ip, lr, pc} sub fp, ip, #4 sub sp, sp, #8 gcda2r: str r0, [fp, #-16] str r1, [fp, #-20] cmp subgt sublt bne r0, r1 r0, r0, r1 r1, r1, r0 gcda2r END2: sub sp, fp, #12 ldmfd sp, {fp, sp, lr} bx lr Sam Siewert 21

Test Impact of Code Generation Try Putting Multi-demensional Array inside a Struct and Iterating compared to no Wrapper Compare S output to Hand Written Mixed C and Assembly Often Best Option Hand Written ASM for High Frequency Co-Routines Network Processors Digital Video Processing Small Blocks of Code Executed Often at High Rates (Hot Spot) Beware of Memory Hierarchy Inefficiency Sam Siewert 22