CMPSC 160 Translation of Programming Languages. Three-Address Code
|
|
- Donna Campbell
- 6 years ago
- Views:
Transcription
1 CMPSC 160 Translation of Programming Languages Lectures 16: Code Generation: Three- Address Code Three-Address Code Each instruction can have at most three operands Each operand corresponds to a memory address or a temporary We have to break large statements into little operations that use temporary variables x=(2+3)+4 turns into to t1=2+3; x=t1+4; Temporary variables store the results at the internal nodes in the AST 1
2 Three-Address Code Assignments x := y x := y op z op: binary arithmetic or logical operators x := op y op: unary operators (unary minus, negation, integer to float conversion) Branch goto L Execute the statement with labeled L next Conditional Branch if x relop y goto L relop: <, >, =, <=, >=, ==,!= if the condition holds we execute statement labeled L next if the condition does not hold we execute the statement following this statement next Three-Address Code if (x < y) x = 5*y + 5*y/3; else y = 5; x = x + y; Temporaries: temporaries correspond to the internal nodes of the syntax tree Variables can be represented with their locations in the symbol table if x < y goto L1 goto L2 L1: t1 := 5 * y t2 := 5 * y t3 := t2 / 3 x := t1 + t3 goto L3 L2: y := 5 L3: x := x + y Three-address code instructions can be represented as an array of quadruples: operation, argument1, argument2, result triples: operation, argument1, argument2 (each triple implicitly corresponds to a temporary) 2
3 Three-Address Code vs. Stack-Based Code Three-Address Code: Good: Compact representation Good: Statement is self contained in that it has the inputs, outputs, and operation all in one instruction Bad: Requires lots of temporary variables Bad: Temporary variables have to be handled explicitly Stack-Based Code: Good: No temporaries, everything is kept on the stack Good: It is easy to generate code for this Bad: Requires more instructions to do the same thing Three-Address Code Attributes: E.place: location that holds the value of expression E (this is the temporary variable that will hold the value of the expression) E.code: sequence of instructions that are generated for E Procedures: newtemp(): Returns a new temporary each time it is called gen(): Generates instruction (have to call it with appropriate arguments) lookup(id.name): Returns the location of id from the symbol table denotes concatenation Productions S id := E E E 1 + E 2 E E 1 * E 2 E ( E 1 ) E - E 1 E id E num 3
4 Three-Address Code Attributes: E.place: location that holds the value of expression E (this is the temporary variable that will hold the value of the expression) E.code: sequence of instructions that are generated for E Procedures: newtemp(): Returns a new temporary each time it is called gen(): Generates instruction (have to call it with appropriate arguments) lookup(id.name): Returns the location of id from the symbol table Productions Semantic Rules S id := E id.place lookup(id.name); S.code E.code gen(id.place := E.place); E E 1 + E 2 E.place newtemp(); E.code E 1.code E 2.code gen(e.place := E 1.place + E 2.place); E E 1 * E 2 E.place newtemp(); E.code E 1.code E 2.code gen(e.place := E 1.place * E 2.place); E ( E 1 ) E.code E 1.code; E.place E 1.place; E - E 1 E.place newtemp(); E.code E 1.code gen(e.place := uminus E 1.place); E id E.place lookup(id.name); E.code (empty string) E num E.place newtemp(); E.code gen(e.place := num.value); Example x := ( y + z ) * a S.code = t1 := y + z t2 := t1 * a x := t2 S E.place = t2 E.code = t1 := y + z t2 := t1 * a id := E E.place = t1 E.code = t1 := y + z E * E E.place = a E.code = E.place = t1 E.code = t1 := y + z ( E ) id E.place = y E.code = id E + E id E.place = z E.code = 4
5 Code Generation for Boolean Expressions Two approaches Numerical representation Implicit representation Numerical representation Use 1 to represent true, use 0 to represent false For three-address code store this result in a temporary For stack machine code store this result in the stack Implicit representation For the boolean expressions which are used in flow-of-control statements (such as if-statements, while-statements etc.) boolean expressions do not have to explicitly compute a value, they just need to branch to the right instruction Generate code for boolean expressions which branch to the appropriate instruction based on the result of the boolean expression Boolean Expressions: Numerical Representation, Three-Address Code Attributes : E.place: location that holds the value of expression E E.code: sequence of instructions that are generated for E relop.op: operator can be ==,!=, <=, >=, >, < Global variable: nextstat: Returns the location of the next instruction to be generated (each call to gen() increments nextstat by 1) Productions Semantic Rules E E 1 relop E 2 E.place newtemp(); E.code E 1.code E 2.code gen( if E 1.place relop.op E 2.place goto nextstat+3); gen(e.place := 0 ) gen( goto nextstat+2) gen(e.place := 1 ); E E 1 and E 2 E.place newtemp(); E.code E 1.code E 2.code gen(e.place := E 1.place and E 2.place); E E 1 or E 2 E.place newtemp(); E.code E 1.code E 2.code gen(e.place := E 1.place or E 2.place); 5
6 Boolean Expressions: Numerical Representation, Three-Address Code (continued) Attributes : E.place: location that holds the value of expression E E.code: sequence of instructions that are generated for E Global variable: nextstat: Returns the location of the next instruction to be generated (each call to gen() increments nextstat by 1) Productions Semantic Rules boolean negation E not E 1 E true E false E.place newtemp(); E.code E 1.code gen(e.place := negate E 1.place); E.place newtemp(); E.code gen(e.place := 1 ); E.place newtemp(); E.code gen(e.place := 0 ); Numerical Representation of Boolean Expressions Input boolean expression: x < y and a == b Three address code: Instructions are for x < y Instructions are for a == b 100 if x < y goto t1 := goto t1 := if a = b goto t2 := goto t2 := t3 := t1 and t2 These are the locations of the instructions, they are not labels. We could generate code using labels too Stack machine code: Instructions are for x < y Instructions are for a == b 100 load x 101 load y 102 if_cmplt push goto push load a 107 load b 108 if_cmpeq push goto push and 6
7 Boolean Expressions: Implicit Representation, Three-Address Code Attributes : E.code: sequence of instructions that are generated for E E.false: instruction to branch to if E evaluates to false E.true: instruction to branch to if E evaluates to true (E.code is synthesized whereas E.true and E.false are inherited) id.place: location for id Productions Semantic Rules E E 1 relop E 2 E.code E 1.code E 2.code gen( if E 1.place relop.op E 2.place goto E.true) gen( goto E.false); E E 1 and E 2 E 1.true newlabel(); E 1.false E. false; (short-circuiting) This generated label will be inserted to the place for E 1.true in the code generated for E 1 E 2.true E. true; E 2.false E. false; E.code E 1.code gen(e 1.true : ) E 2.code ; These places will be filled with labels later on when they become available Boolean Expressions: Implicit Representation, Three-Address Code (continued) Attributes : E.code: sequence of instructions that are generated for E E.false: instruction to branch to if E evaluates to false E.true: instruction to branch to if E evaluates to true id.place: location for id Productions Semantic Rules E E 1 or E 2 E 1.true E.true; (short-circuiting) E 1.false newlabel(); E 2.true E. true; E 2.false E. false; E.code E 1.code gen(e 1.false : ) E 2.code ; E not E 1 E 1.true E.false; E 1.false E. true; E.code E 1.code; E true gen(`goto E.true); E false gen(`goto E.false); 7
8 Three-Address Code Example These are the locations of three-address code instructions, they are not labels Input boolean expression: x < y and a == b These labels will be generated later on, and will be inserted to the corresponding places Numerical representation: 100 if x < y goto t1 := goto t1 := if a = b goto t2 := goto t2 := t3 := t1 and t2 Implicit representation: if x < y goto L1 goto LFalse L1: if a = b goto LTrue goto LFalse... LTrue: LFalse: Three-Address Code, Implicit Representation false attribute will be inserted here Input: x < y and a == b true = false= E code = if x < y goto L1 goto L1:if a = b goto goto true = L1 false= and code = if x < y goto L1 goto E true = false= true attribute will be inserted here code = if a = b goto goto E E relop code = op = < E code = relop E code = op = == E code = place = x id name = x place = y id name = y place = a id name = a place = b id name = b 8
9 Flow-of-Control Statements If-then-else Branch based on the result of boolean test expression test expression Y N then-block else-block Next block Flow-of-Control Statements: Code Structure We have to decide on the code layout for the code for flow-of-control S if E then S 1 else S 2 E.code to E.true to E.false Labels E.true: S 1.code if E evaluates to true goto S.next E.false: S 2.code if E evaluates to false S.next: 9
10 Flow-of-Control Statements, Three-Address Code, Assuming Implicit Representation for Boolean Expressions Attributes : S.code: sequence of instructions that are generated for S S.next: label of the instruction that will be executed immediately after S (S.next is an inherited attribute) Productions Semantic Rules ASSUMPTION: the code generated S if E then S 1 else S 2 E.true newlabel(); for boolean expression E will branch E.false newlabel(); to E.true or E.false label based on the S 1.next S. next; result of the expression S 2.next S. next; S.code E.code gen(e.true : ) S 1.code gen( goto S.next) gen(e.false : ) S 2.code ; Flow-of-Control Statements Loops Evaluate condition before loop (if needed) Evaluate condition after loop Branch back to the top if condition holds Pre-test The layout shown here merges loop test with last block of loop body Loop body While, for, do, and until all fit this basic model You can use different layouts for example, put the test before the loop body and branch back to it at the end of the loop body Post-test Next block 10
11 Flow-of-Control Statements: Code Structure Two different layouts for while statements: The algorithms I give in the following slides use this layout: S while E do S 1 This layout places E.code after S 1.code : S while E do S 1 S.begin: E.true: E.false: E.code S 1.code goto S.begin to E.true to E.false E.true: E.begin: E.false: goto E.begin S 1.code E.code to E.true to E.false Flow-of-Control Statements, Three-Address Code, Assuming Implicit Representation for Boolean Expressions Attributes : S.code: sequence of instructions that are generated for S S.next: label of the instruction that will be executed immediately after S (S.next is an inherited attribute) Productions Semantic Rules S while E do S 1 S.begin newlabel(); E.true newlabel(); E.false S. next; S 1.next S. begin; S.code gen(s.begin : ) E.code gen(e.true : ) S 1.code gen( goto S.begin); S S 1 ; S 2 S 1.next newlabel(); S 2.next S.next; S.code S 1.code gen(s 1.next : ) S 2.code 11
12 Example Input code fragment: while (a < b) { if (c < d) x = y + z; else x = y z } L1: if a < b goto L2 goto LNext L2: if c < d goto L3 goto L4 L3: t1 := y + z x := t1 goto L1 L4: t2 := y z x := t2 goto L1 LNext:... E.true for a < b E.false for a < b E.true for c < d E.false for c < d Backpatching E.true, E.false, S.next may not be computed in a single pass (they are inherited attributes) Backpatching is a technique for generating labels for E.true, E.false, S.next and inserting them to the appropriate locations Basic idea Keep lists E.truelist, E.falselist, S.nextlist E.truelist (E.falselist): the list of instructions where the label for E.true (E.false) have to be inserted when it becomes available S.nextlist: the list of instructions where the label for S.next have to be inserted when it becomes available When labels E.true, E.false, S.next are computed these labels are inserted to the instructions in these lists 12
13 x86 C with Static Linking Compile time Run time Executable x86 Assembly x86 Assembly x86 x86 Assembler x86 Object File (.o) x86 Assembler x86 Object File (.o) Libraries At compile time, all of the code is transformed into x86 object files and then the object files are linked together with the libraries to create a statically linked executable. This executable then runs directly on the x86 hardware. Linker x86 Executable One drawback, is if you wanted to run the same code on a Power-PC (even with the same OS) you have to recompile everything. x86 C with Dynamic Linking x86 Assembly x86 Assembler x86 Assembly x86 Assembler Compile time Run time Not-fully Linked x86 Executable Loader Final Executable loaded in Memory x86 Dynamic Libraries x86 Object File (.o) The linker just links together the.o files, and does not link calls to dynamically loaded libraries (DLLs in Windows or Shared Libraries in Unix) x86 Object File (.o) Linker Not-fully Linked x86 Executable This is similar to before, but now the final linking occurs when the program is loaded (or even during program execution) Here, libraries can be shared and they can be updated across the whole system without re-linking every single executable 13
14 Java Compile time Java Bytecode (class) Run time Java Bytecode (class) Java Classes Java Assembly Java Assembler Java Bytecode (class) Java Assembly Java Assembler Java Bytecode (class) Here the compiler targets java bytecode and the bytecode is then run on top of the Java Virtual Machine (JVM). The JVM really just interprets (simulates) the bytecode like any scripting language. Because of this, any java program compiled to bytecode is portable to any machine that someone has already ported the JVM too. No need to recompile. Java Virtual Machine (bytecode interpreter) x86 Java with Just-in-Time (JIT) Compilation Compile time Java Bytecode (class) Run time Java Bytecode (class) Java Classes Java Assembly Java Assembly Java Assembler Java Assembler Java Bytecode (class) Java Bytecode (class) JIT Here the compiler targets java bytecode (which is what we do in this class) and the bytecode is then run on top of the Java Virtual Machine (JVM). The JVM really just interprets (simulates) the bytecode like any scripting language. Because of this, any java program compiled to bytecode is portable to any machine that someone has already ported the JVM too. No need to recompile. Java Virtual Machine (bytecode interpreter) x86 On Call to Compiled Method On Call or Return back to uncompiled code X86 native code compiled from Java class 14
Intermediate Code Generation
Intermediate Code Generation 1 Intermediate Code Generation Translating source program into an intermediate language" Simple CPU Independent, yet, close in spirit to machine language Benefits Retargeting
More informationFormal Languages and Compilers Lecture X Intermediate Code Generation
Formal Languages and Compilers Lecture X Intermediate Code Generation Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/
More informationModule 25 Control Flow statements and Boolean Expressions
Module 25 Control Flow statements and Boolean Expressions In this module we learn to generate three address code for control flow statements. We will also try to incorporate short circuit information in
More informationUNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.
UNIT-3 SYNTAX-DIRECTED TRANSLATION: A Grammar symbols are associated with attributes to associate information with the programming language constructs that they represent. Values of these attributes are
More informationΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών
ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών Lecture 11a Intermediate Code Generation Elias Athanasopoulos eliasathan@cs.ucy.ac.cy Declarations For each local name Creation of symbol-table entry Add type
More informationPrinciple of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale
Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (1 Principle of Compilers Lecture VIII: Intermediate Code Generation Alessandro Artale Faculty of Computer Science Free
More informationNARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1
SEM / YEAR : VI / III CS2352 PRINCIPLES OF COMPLIERS DESIGN UNIT III INTERMEDIATE CODE GENERATION PART A 1. What are the benefits of intermediate code generation? (A.U May 2008) A Compiler for different
More informationModule 27 Switch-case statements and Run-time storage management
Module 27 Switch-case statements and Run-time storage management In this module we will discuss the pending constructs in generating three-address code namely switch-case statements. We will also discuss
More informationModule 26 Backpatching and Procedures
Module 26 Backpatching and Procedures In this module, we would learn to generate three-address code for control flow statements using backpatching. We shall also discuss to combine Boolean expressions
More informationCSCI Compiler Design
University of Southern California CSCI565 Compiler Design Mterm Exam Solution - Spring 2014 CSCI 565 - Compiler Design Spring 2014 Mterm Exam - Solution Problem 1: Context-Free-Grammars and Parsing Algorithms
More informationSyntax-Directed Translation
Syntax-Directed Translation 1 Syntax-Directed Translation 2 Syntax-Directed Translation 3 Syntax-Directed Translation In a syntax-directed definition, each production A α is associated with a set of semantic
More informationTime : 1 Hour Max Marks : 30
Total No. of Questions : 6 P4890 B.E/ Insem.- 74 B.E ( Computer Engg) PRINCIPLES OF MODERN COMPILER DESIGN (2012 Pattern) (Semester I) Time : 1 Hour Max Marks : 30 Q.1 a) Explain need of symbol table with
More informationCS2210: Compiler Construction. Code Generation
Modern Compiler Project Fortran program Fortran s Lexer, Parser, and Static Checker Intermediate Code Generator IR MIPS Code Generator MIPS code C program C s Lexer, Parser, and Static Checker Intermediate
More informationCS322 Languages and Compiler Design II. Spring 2012 Lecture 7
CS322 Languages and Compiler Design II Spring 2012 Lecture 7 1 USES OF BOOLEAN EXPRESSIONS Used to drive conditional execution of program sections, e.g. IF (a < 17) OR (b = 12) THEN... ELSE...; WHILE NOT
More informationIntermediate Representations
Intermediate Representations A variety of intermediate representations are used in compilers Most common intermediate representations are: Abstract Syntax Tree Directed Acyclic Graph (DAG) Three-Address
More informationCSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona
CSc 453 Compilers and Systems Software 16 : Intermediate Code IV Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Control Structures Control Structures
More informationDixita Kagathara Page 1
2014 Sem-VII Intermediate Code Generation 1) What is intermediate code? Intermediate code is: The output of the parser and the input to the Code Generator. Relatively machine-independent: allows the compiler
More information2 Intermediate Representation. HIR (high level IR) preserves loop structure and. More of a wizardry rather than science. in a set of source languages
Acknowledgements The slides for this lecture are a modified versions of the offering by Prof. Sanjeev K Aggarwal Intermediate Code Generation... Front translates a source program into an intermediate representation
More informationTHEORY OF COMPILATION
Lecture 09 IR (ackpatching) THEORY OF COMPILATION Eran Yahav www.cs.technion.ac.il/~yahave/tocs2011/compilers-lec09.pptx Reference: Dragon 6.2,6.3,6.4,6.6 1 Recap Lexical analysis regular expressions identify
More informationIntermediate Code Generation
Intermediate Code Generation Intermediate codes are machine independent codes, but they are close to machine instructions The given program in a source language is converted to an equivalent program in
More informationIntermediate Code Generation Part II
1 Intermediate Code Generation Part II Chapter 6 (1 st ed Chapter 8) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009 2 Advanced Intermediate Code Generation
More informationControl Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV
CSc 453 Compilers and Systems Software 16 : Intermediate Code IV Control Structures Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Control Structures
More informationIntermediate Code Generation
Intermediate Code Generation Rupesh Nasre. CS3300 Compiler Design IIT Madras July 2018 Character stream Lexical Analyzer Machine-Independent Code Code Optimizer F r o n t e n d Token stream Syntax Analyzer
More informationIntermediate Code Generation
Intermediate Code Generation Rupesh Nasre. CS3300 Compiler Design IIT Madras Aug 2015 Character stream Lexical Analyzer Machine-Independent Code Optimizer F r o n t e n d Token stream Syntax Analyzer Syntax
More informationTheory of Compilation Erez Petrank. Lecture 7: Intermediate Representation Part 2: Backpatching
Theory of Compilation 236360 rez Petrank Lecture 7: Intermediate Representation Part 2: ackpatching 1 You are here Compiler Source text txt Lexical Analysis Syntax Analysis Semantic Analysis Inter. Rep.
More informationPrinciples of Programming Languages
Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp-16/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 11! Intermediate-Code Genera=on Intermediate representa=ons
More informationIntermediate Code Generation
Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target
More informationIntermediate Code Generation
Intermediate Code Generation Basic Approach and Application to Assignment and xpressions Array xpressions Boolean xpressions Copyright 2017, Pedro C. Diniz, all rights reserved. Students enrolled in the
More informationPART 6 - RUN-TIME ENVIRONMENT. F. Wotawa TU Graz) Compiler Construction Summer term / 309
PART 6 - RUN-TIME ENVIRONMENT F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2016 188 / 309 Objectives/Tasks Relate static source code to actions at program runtime. Names in the source code
More informationCOMPILER CONSTRUCTION (Intermediate Code: three address, control flow)
COMPILER CONSTRUCTION (Intermediate Code: three address, control flow) Prof. K R Chowdhary Email: kr.chowdhary@jietjodhpur.ac.in Campus Director, JIET, Jodhpur Thursday 18 th October, 2018 kr chowdhary
More informationCSc 453 Intermediate Code Generation
CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson Overview Intermediate representations span the gap between the source and target languages: closer to target language;
More informationintermediate-code Generation
intermediate-code Generation }sequence of intermediate representations source program High Level intermediate Representation Low Level intermediate Representation Target Code e.g. C programming language
More informationUNIT IV INTERMEDIATE CODE GENERATION
UNIT IV INTERMEDIATE CODE GENERATION 2 Marks 1. Draw syntax tree for the expression a=b*-c+b*-c 2. Explain postfix notation. It is the linearized representation of syntax tree.it is a list of nodes of
More informationIntermediate Code Generation
Intermediate Code Generation Basic Approach and Application to Assignment and xpressions Array xpressions Boolean xpressions Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in the
More informationCSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall
CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall Announcements Weekly team meetings starting today Wednesday (4/4) will be a workshop Wednesday - Post questions you'd like addressed
More informationAdministration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator
CS 412/413 Introduction to Compilers and Translators Andrew Myers Cornell University Administration Design reports due Friday Current demo schedule on web page send mail with preferred times if you haven
More informationComputer Components. Software{ User Programs. Operating System. Hardware
Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point
More informationIntermediate Representa.on
IR Intermediate Representa.on CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class Intermediate Representation Language Specific Language + Machine Independent Machine Dependent
More informationIntermediate Code Generation
Intermediate Code Generation Control-Flow tatements hort-circuit Predicate valuation Back-Patching Copyright 2015, Pedro C. Diniz, all rights reserved. tudents enrolled in the Compilers class at the University
More informationChapter 6 Intermediate Code Generation
Chapter 6 Intermediate Code Generation Outline Variants of Syntax Trees Three-address code Types and declarations Translation of expressions Type checking Control flow Backpatching Introduction Intermediate
More informationIntermediate Code Generation
Intermediate Code Generation Control-Flow tatements hort-circuit Predicate valuation Back-Patching Copyright 2011, Pedro C. Diniz, all rights reserved. tudents enrolled in the Compilers class at the University
More informationHomework 1 Answers. CS 322 Compiler Construction Winter Quarter 2006
Homework 1 Answers CS 322 Compiler Construction Winter Quarter 2006 Problem 1 m := 0 i := 0 L1: if i
More informationTACi: Three-Address Code Interpreter (version 1.0)
TACi: Three-Address Code Interpreter (version 1.0) David Sinclair September 23, 2018 1 Introduction TACi is an interpreter for Three-Address Code, the common intermediate representation (IR) used in compilers.
More informationCompilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam
Compilers Intermediate representations and code generation Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Today Intermediate representations and code generation Scanner Parser Semantic
More informationECS 142 Project: Code generation hints (2)
ECS 142 Project: Code generation hints (2) Winter 2011 This document describes the nature of final code generation for your project. I have made a number of assumptions here, mostly in terms of availability
More informationCompilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 7, vrijdag 2 november 2018
Compilerconstructie najaar 2018 http://www.liacs.leidenuniv.nl/~vlietrvan1/coco/ Rudy van Vliet kamer 140 Snellius, tel. 071-527 2876 rvvliet(at)liacs(dot)nl college 7, vrijdag 2 november 2018 + werkcollege
More informationCSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1
CSE P 501 Compilers Java Implementation JVMs, JITs &c Hal Perkins Winter 2008 3/11/2008 2002-08 Hal Perkins & UW CSE V-1 Agenda Java virtual machine architecture.class files Class loading Execution engines
More informationSemantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?
Semantic analysis and intermediate representations Which methods / formalisms are used in the various phases during the analysis? The task of this phase is to check the "static semantics" and generate
More informationIntermediate Representations
Intermediate Representations Intermediate Representations (EaC Chaper 5) Source Code Front End IR Middle End IR Back End Target Code Front end - produces an intermediate representation (IR) Middle end
More informationCSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall
CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http://www.cse.buffalo.edu/faculty/alphonce/sp17/cse443/index.php https://piazza.com/class/iybn4ndqa1s3ei shift/reduce conflict with
More informationPRINCIPLES OF COMPILER DESIGN
PRINCIPLES OF COMPILER DESIGN 2 MARK QUESTIONS WITH ANSWERS UNIT I 1. What is a Complier? A Complier is a program that reads a program written in one language-the source language-and translates it in to
More informationProf. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan
Language Processing Systems Prof. Mohamed Hamada Software ngineering Lab. The University of Aizu Japan Intermediate/Code Generation Source language Scanner (lexical analysis) tokens Parser (syntax analysis)
More informationLanguages and Compiler Design II IR Code Generation I
Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring 2010 rev.: 4/16/2010 PSU CS322 HM 1 Agenda Grammar G1
More informationCompiling Techniques
Lecture 10: Introduction to 10 November 2015 Coursework: Block and Procedure Table of contents Introduction 1 Introduction Overview Java Virtual Machine Frames and Function Call 2 JVM Types and Mnemonics
More informationThe analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.
COMPILER DESIGN 1. What is a compiler? A compiler is a program that reads a program written in one language the source language and translates it into an equivalent program in another language-the target
More informationAgenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1
Agenda CSE P 501 Compilers Java Implementation JVMs, JITs &c Hal Perkins Summer 2004 Java virtual machine architecture.class files Class loading Execution engines Interpreters & JITs various strategies
More informationCOP5621 Exam 3 - Spring 2005
COP5621 Exam 3 - Spring 2005 Name: (Please print) Put the answers on these sheets. Use additional sheets when necessary. Show how you derived your answer when applicable (this is required for full cred
More informationConcepts Introduced in Chapter 6
Concepts Introduced in Chapter 6 types of intermediate code representations translation of declarations arithmetic expressions boolean expressions flow-of-control statements backpatching EECS 665 Compiler
More informationLecture 15 CIS 341: COMPILERS
Lecture 15 CIS 341: COMPILERS Announcements HW4: OAT v. 1.0 Parsing & basic code generation Due: March 28 th No lecture on Thursday, March 22 Dr. Z will be away Zdancewic CIS 341: Compilers 2 Adding Integers
More informationComputer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres
Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres dgriol@inf.uc3m.es Compiler Architecture Source language Scanner (lexical analysis) tokens Parser (syntax
More informationLECTURE 17. Expressions and Assignment
LECTURE 17 Expressions and Assignment EXPRESSION SYNTAX An expression consists of An atomic object, e.g. number or variable. An operator (or function) applied to a collection of operands (or arguments)
More informationJava Primer 1: Types, Classes and Operators
Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,
More informationLecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines
Lecture Outline Code Generation Lecture 30 (based on slides by R. Bodik) Stack machines The MIPS assembly language The x86 assembly language A simple source language Stack-machine implementation of the
More informationComputer Components. Software{ User Programs. Operating System. Hardware
Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point
More informationopt. front end produce an intermediate representation optimizer transforms the code in IR form into an back end transforms the code in IR form into
Intermediate representations source code front end ir opt. ir back end target code front end produce an intermediate representation (IR) for the program. optimizer transforms the code in IR form into an
More informationCode Generation. Lecture 30
Code Generation Lecture 30 (based on slides by R. Bodik) 11/14/06 Prof. Hilfinger CS164 Lecture 30 1 Lecture Outline Stack machines The MIPS assembly language The x86 assembly language A simple source
More informationSYNTAX DIRECTED TRANSLATION FOR CONTROL STRUCTURES
UNIT 3 SYNTAX DIRECTED TRANSLATION FOR CONTROL STRUCTURES M.B.Chandak www.mbchandak.com hodcs@rknec.edu CONTROL STRUCTURES 1. If (condition) then Statement 2. If (condition) then Statement 1 else statement
More informationCOMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation
Prelude COMP 181 Lecture 14 Intermediate representations and code generation October 19, 2006 Who is Seth Lloyd? Professor of mechanical engineering at MIT, pioneer in quantum computing Article in Nature:
More informationIR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum
IR Lowering CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 19: Efficient IL Lowering 7 March 07 Use temporary variables for the translation Temporary variables in the Low IR store intermediate
More informationCompilers. Compiler Construction Tutorial The Front-end
Compilers Compiler Construction Tutorial The Front-end Salahaddin University College of Engineering Software Engineering Department 2011-2012 Amanj Sherwany http://www.amanj.me/wiki/doku.php?id=teaching:su:compilers
More informationIntroduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
More informationVIVA QUESTIONS WITH ANSWERS
VIVA QUESTIONS WITH ANSWERS 1. What is a compiler? A compiler is a program that reads a program written in one language the source language and translates it into an equivalent program in another language-the
More informationCS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08
CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 19: Efficient IL Lowering 5 March 08 CS 412/413 Spring 2008 Introduction to Compilers 1 IR Lowering Use temporary variables for the translation
More informationControl Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003
Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an
More informationCA4003 Compiler Construction Assignment Language Definition
CA4003 Compiler Construction Assignment Language Definition David Sinclair 2017-2018 1 Overview The language is not case sensitive. A nonterminal, X, is represented by enclosing it in angle brackets, e.g.
More informationCMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC
CMPT 379 Compilers Anoop Sarkar http://www.cs.sfu.ca/~anoop 11/13/07 1 TAC: Intermediate Representation Language Specific Language + Machine Independent Machine Dependent Front End AST Intermediate Code
More informationCOMPUTER ORGANIZATION & ARCHITECTURE
COMPUTER ORGANIZATION & ARCHITECTURE Instructions Sets Architecture Lesson 5a 1 What are Instruction Sets The complete collection of instructions that are understood by a CPU Can be considered as a functional
More informationo Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement
Last Time Let s all Repeat Together 10/3/05 CS150 Introduction to Computer Science 1 1 We covered o Counter and sentinel controlled loops o Formatting output Today we will o Type casting o Top-down, stepwise
More informationCOP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher
COP4020 ming Languages Compilers and Interpreters Robert van Engelen & Chris Lacher Overview Common compiler and interpreter configurations Virtual machines Integrated development environments Compiler
More informationCS 432 Fall Mike Lam, Professor. Code Generation
CS 432 Fall 2015 Mike Lam, Professor Code Generation Compilers "Back end" Source code Tokens Syntax tree Machine code char data[20]; int main() { float x = 42.0; return 7; } 7f 45 4c 46 01 01 01 00 00
More informationConcepts Introduced in Chapter 6
Concepts Introduced in Chapter 6 types of intermediate code representations translation of declarations arithmetic expressions boolean expressions flow-of-control statements backpatching EECS 665 Compiler
More informationLanguage Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.
Language Translation Compilation vs. interpretation Compilation diagram Step 1: compile program compiler Compiled program Step 2: run input Compiled program output Language Translation compilation is translation
More informationSemantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.
SEMANTIC ANALYSIS: Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known. Parsing only verifies that the program consists of tokens
More informationIntermediate Representations
Intermediate Representations Where In The Course Are We? Rest of the course: compiler writer needs to choose among alternatives Choices affect the quality of compiled code time to compile There may be
More informationOverview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1
CPE 101 mod/reusing slides from a UW course Overview (4) Lecture 4: Arithmetic Expressions Arithmetic expressions Integer and floating-point (double) types Unary and binary operators Precedence Associativity
More informationAnnouncements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop
Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00
More informationCOMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou
COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou Administrative! Any questions about the syllabus?! Course Material available at www.cs.unic.ac.cy/ioanna! Next time reading assignment [ALSU07]
More informationIntermediate Representations
COMP 506 Rice University Spring 2018 Intermediate Representations source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students
More information3. Java - Language Constructs I
Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks
More informationIntermediate Representation (IR)
Intermediate Representation (IR) Components and Design Goals for an IR IR encodes all knowledge the compiler has derived about source program. Simple compiler structure source code More typical compiler
More informationCompiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)
Compiler Theory (Intermediate Code Generation Abstract S yntax + 3 Address Code) 006 Why intermediate code? Details of the source language are confined to the frontend (analysis phase) of a compiler, while
More informationCSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow
CSE 452: Programming Languages Expressions and Control Flow Outline of Today s Lecture Expressions and Assignment Statements Arithmetic Expressions Overloaded Operators Type Conversions Relational and
More informationThe Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.
The Compiler So Far CSC 4181 Compiler Construction Scanner - Lexical analysis Detects inputs with illegal tokens e.g.: main 5 (); Parser - Syntactic analysis Detects inputs with ill-formed parse trees
More informationCSE302: Compiler Design
CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University April 24, 2007 Outline Recap Three
More informationBoolean Expressions and if 9/14/2007
Boolean Expressions and if 9/14/2007 1 Opening Discussion Do you have any questions about the quiz? Let's look at solutions to the interclass problem. Minute essay questions. What functions will we be
More informationAcknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation
Acknowledgement CS3300 - Compiler Design Semantic Analysis - IR Generation V. Krishna Nandivada IIT Madras Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all
More informationCS313D: ADVANCED PROGRAMMING LANGUAGE
CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting
More informationOperators and Expressions
Operators and Expressions Conversions. Widening and Narrowing Primitive Conversions Widening and Narrowing Reference Conversions Conversions up the type hierarchy are called widening reference conversions
More informationBuilding a Compiler with. JoeQ. Outline of this lecture. Building a compiler: what pieces we need? AKA, how to solve Homework 2
Building a Compiler with JoeQ AKA, how to solve Homework 2 Outline of this lecture Building a compiler: what pieces we need? An effective IR for Java joeq Homework hints How to Build a Compiler 1. Choose
More informationA Simple Syntax-Directed Translator
Chapter 2 A Simple Syntax-Directed Translator 1-1 Introduction The analysis phase of a compiler breaks up a source program into constituent pieces and produces an internal representation for it, called
More information