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