Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 11: Compiler II: Code Generation slide 1www.nand2tetris.org Building.

Slides:



Advertisements
Similar presentations
Chapter 4: Machine Language
Advertisements

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 1: Boolean Logic slide 1www.idc.ac.il/tecs Chapter 1: Boolean.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Intermediate Code Generation
The University of Adelaide, School of Computer Science
Software & Services Group, Developer Products Division Copyright© 2010, Intel Corporation. All rights reserved. *Other brands and names are the property.
1 Compiler Construction Intermediate Code Generation.
The Assembly Language Level
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Run time vs. Compile time
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 3: Sequential Logic slide 1www.idc.ac.il/tecs Chapter 3:
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 1www.idc.ac.il/tecs Chapter.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 7: VM I: Stack Arithmetic slide 1www.idc.ac.il/tecs Chapter.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Introduction: Hello, World Below slide 1www.idc.ac.il/tecs Introduction:
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 10: Compiler I: Syntax Analysis slide 1www.idc.ac.il/tecs.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 9: High Level Language slide 1www.idc.ac.il/tecs Chapter.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 2: Boolean Logic slide 1www.idc.ac.il/tecs Chapter 2: Boolean.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 10: Compiler I: Syntax Analysis slide 1www.idc.ac.il/tecs Compiler.
ITEC 352 Lecture 11 ISA - CPU. ISA (2) Review Questions? HW 2 due on Friday ISA –Machine language –Buses –Memory.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 7: Virtual Machine I: Stack Arithmetic slide 1www.nand2tetris.org.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 7: Virutal Machine, Part I slide 1www.idc.ac.il/tecs Virtual Machine.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 4: Machine Language slide 1www.idc.ac.il/tecs Machine Language.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 6: Assembler slide 1www.idc.ac.il/tecs Assembler Elements of Computing.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 5: Computer Architecture slide 1www.idc.ac.il/tecs Computer Architecture.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
CSC 310 – Imperative Programming Languages, Spring, 2009 Virtual Machines and Threaded Intermediate Code (instead of PR Chapter 5 on Target Machine Architecture)
Compiler Construction
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Building a Modern Computer From First Principles
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 11: Compiler II: Code Generation slide 1www.idc.ac.il/tecs.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 1: Boolean Logic slide 1www.nand2tetris.org Building a Modern.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 1: Boolean Logic slide 1www.idc.ac.il/tecs Boolean Logic Elements.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 2: Boolean Arithmetic slide 1www.nand2tetris.org Building a Modern.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 8: Virtual Machine, Part II slide 1www.idc.ac.il/tecs Virtual Machine.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 8: Virtual Machine, Part II slide 1www.nand2tetris.org Building.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 1: Compiler II: Code Generation slide 1www.nand2tetris.org Building.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 7: Virutal Machine, Part I slide 1www.nand2tetris.org Building.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 3: Sequential Logic slide 1www.idc.ac.il/tecs Sequential Logic.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 3: Sequential Logic slide 1www.nand2tetris.org Building a Modern.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 10: Compiler I: Syntax Analysis slide 1www.nand2tetris.org Building.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 10: Compiler I: Syntax Analysis slide 1www.nand2tetris.org Building.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 9: High-Level Language slide 1www.nand2tetris.org Building a Modern.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Chapter 1 Introduction Major Data Structures in Compiler
Building a Modern Computer From First Principles
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 7: Virutal Machine, Part I slide 1www.nand2tetris.org Building.
CSC 8505 Compiler Construction Runtime Environments.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 6: Assembler slide 1www.nand2tetris.org Building a Modern Computer.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 4: Machine Language slide 1www.nand2tetris.org Building a Modern.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 4: Machine Language slide 1www.nand2tetris.org Building a Modern.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 10: Compiler I: Syntax Analysis slide 1www.nand2tetris.org Building.
Names and Attributes Names are a key programming language feature
Introduction: From Nand to Tetris
Compiler Construction (CS-636)
Virtual Machine Part I: Stack Arithmetic
Virtual Machine Part II: Program Control
Introduction to Compilers Tim Teitelbaum
Compiler Construction
Compiler II: Code Generation
Compiler I: Sytnax Analysis
PZ09A - Activation records
Presentation transcript:

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 1www.nand2tetris.org Building a Modern Computer From First Principles Compiler II: Code Generation

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 2www.nand2tetris.org Usage and Copyright Notice: Copyright © Noam Nisan and Shimon Schocken This presentation contains lecture materials that accompany the textbook “The Elements of Computing Systems” by Noam Nisan & Shimon Schocken, MIT Press, We provide both PPT and PDF versions. Our web site, a set of presentations, one for each book chapter. Each presentation is designed to support about 3 hours of classroom or self-study instruction. You are welcome to use or edit this presentation as you see fit for instructional and non- commercial purposes. If you use our materials, please include a reference to If you have any questions or comments, please write us at

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 3www.nand2tetris.org Course map Assembler Chapter 6 H.L. Language & Operating Sys. abstract interface Compiler Chapters VM Translator Chapters Computer Architecture Chapters Gate Logic Chapters Electrical Engineering Physics Virtual Machine abstract interface Software hierarchy Assembly Language abstract interface Hardware hierarchy Machine Language abstract interface Hardware Platform abstract interface Chips & Logic Gates abstract interface Human Thought Abstract design Chapters 9, 12

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 4www.nand2tetris.org The big picture Syntax analysis: extracting the semantics from the source code Code generation: expressing the semantics using the target language

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 5www.nand2tetris.org Syntax analysis (review) Class Bar { method Fraction foo(int y) { var int temp; // a variable let temp = (xxx+12)*-63; Class Bar { method Fraction foo(int y) { var int temp; // a variable let temp = (xxx+12)*-63; var int temp ; let temp = ( xxx var int temp ; let temp = ( xxx Syntax analyzer The code generation challenge: Program = a series of operations that manipulate data Compiler: converts each “understood” (parsed) source operation and data item into corresponding operations and data items in the target language Thus, we have to generate code for handling data handling operations  Our approach: extend the syntax analyzer (project 10) into a full-blown compiler: instead of generating XML, we’ll make it generate VM code.

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 6www.nand2tetris.org Memory segments (review) Where i is a non-negative integer and segment is one of the following: static : holds values of global variables, shared by all functions in the same class argument : holds values of the argument variables of the current function local : holds values of the local variables of the current function this : holds values of the private (“object”) variables of the current object that : holds array values (silly name, sorry) constant : holds all the constants in the range 0…32767 ( pseudo memory segment) pointer : used to map this and that on different areas in the heap temp : fixed 8-entry segment that holds temporary variables for general use; Shared by all VM functions in the program. VM memory Commands: pop segment i push segment i VM memory Commands: pop segment i push segment i

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 7www.nand2tetris.org Code generation example let x = x + 1 let x = x + 1 let x = x + 1; Syntax analysis push local 0 push constant 1 add pop local 0 push local 0 push constant 1 add pop local 0 (assuming that in the source code, x is the first local variable) Code generation

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 8www.nand2tetris.org Handling data When dealing with a variable, say x, we have to know: What is x’s data type? Primitive, or ADT (class name) ? (Need to know in order to properly allocate RAM resources for its representation) What kind of variable is x? local, static, field, argument ? (Need to know in order to properly allocate it to the right memory segment; this implies the variable’s life cycle ).

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 9www.nand2tetris.org Memory segments (example) When we compile this class, we have to create the following mapping: nAccounts, bankCommission are mapped on static 0,1 id, owner, balance of the current object are mapped on this 0,1,2 sum, bankAccount, when are mapped on arg 0,1,2 i, j, due are mapped on local 0,1,2. Recall that we use 8 memory segments. Each memory segment, e.g. static, is an indexed sequence of 16-bit values that can be referred to as static 0, static 1, static 2, etc.

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 10www.nand2tetris.org Symbol table Classical implementation: A linked list of hash tables, each reflecting a single scope nested within the next one in the list Identifier lookup works from the current table upwards.

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 11www.nand2tetris.org Life cycle Static: single copy must be kept alive throughout the program duration Field: different copies must be kept for each object Local: created on subroutine entry, killed on exit Argument: similar to local Good news: the VM implementation already handles all these details !!! Hurray !!!

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 12www.nand2tetris.org class Complex { // Properties (fields): int re; // Real part int im; // Imaginary part... /** Constructs a new Complex object. */ public Complex(int aRe, int aIm) { re = aRe; im = aIm; }... } // The following code can be in any class: public void bla() { Complex a, b, c;... a = new Complex(5,17); b = new Complex(12,192);... c = a; // Only the reference is copied... } class Complex { // Properties (fields): int re; // Real part int im; // Imaginary part... /** Constructs a new Complex object. */ public Complex(int aRe, int aIm) { re = aRe; im = aIm; }... } // The following code can be in any class: public void bla() { Complex a, b, c;... a = new Complex(5,17); b = new Complex(12,192);... c = a; // Only the reference is copied... } Java code Handling objects: construction / memory allocation foo = new ClassName( … ) Is handled by causing the compiler to generate code affecting: foo = Mem.alloc(n) Where n is the number of words necessary to represent the object in the host RAM. Following compilation:

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 13www.nand2tetris.org Handling objects: accessing fields class Complex { // Properties (fields): int re; // Real part int im; // Imaginary part... /** Constructs a new Complex object. */ public Complex(int aRe, int aIm) { re = aRe; im = aIm; }... // Multiplication: public void mult (int c) { re = re * c; im = im * c; }... } class Complex { // Properties (fields): int re; // Real part int im; // Imaginary part... /** Constructs a new Complex object. */ public Complex(int aRe, int aIm) { re = aRe; im = aIm; }... // Multiplication: public void mult (int c) { re = re * c; im = im * c; }... } Java code // im = im * c : *(this+1) = *(this+1) times (argument 0) // im = im * c : *(this+1) = *(this+1) times (argument 0) Translating im = im * c :  Look up the symbol table  Resulting semantics: This semantics should be expressed in the target language.

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 14www.nand2tetris.org /* Assume that b and r were passed to the function as its first two arguments. The following code implements the operation b.radius=r. */ // Get b's base address: push argument 0 // Point the this seg. to b: pop pointer 0 // Get r's value push argument 1 // Set b's third field to r: pop this 2 /* Assume that b and r were passed to the function as its first two arguments. The following code implements the operation b.radius=r. */ // Get b's base address: push argument 0 // Point the this seg. to b: pop pointer 0 // Get r's value push argument 1 // Set b's third field to r: pop this 2 Handling objects: establishing access to the object itself

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 15www.nand2tetris.org Handling objects: method calls General rule: each method call foo.bar(v1,v2,...) can be translated into push foo push v1 push v2... call bar class Complex { // Properties (fields): int re; // Real part int im; // Imaginary part... /** Constructs a new Complex object. */ public Complex(int aRe, int aIm) { re = aRe; im = aIm; }... } class Foo {... public void foo() { Complex x;... x = new Complex(1,2); x.mult(5);... } class Complex { // Properties (fields): int re; // Real part int im; // Imaginary part... /** Constructs a new Complex object. */ public Complex(int aRe, int aIm) { re = aRe; im = aIm; }... } class Foo {... public void foo() { Complex x;... x = new Complex(1,2); x.mult(5);... } Java code // x.mult(5): push x push 5 call mult // x.mult(5): push x push 5 call mult Translating x.mult(5):  Can also be viewed as: mult(x,5)  Generated code:

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 16www.nand2tetris.org class Bla {... void foo(int k) { int x, y; int[] bar; // declare an array... // Construct the array: bar = new int[10];... bar[k]=19; }... Main.foo(2); // Call the foo method... class Bla {... void foo(int k) { int x, y; int[] bar; // declare an array... // Construct the array: bar = new int[10];... bar[k]=19; }... Main.foo(2); // Call the foo method... Java code bar = new int(n) Typically handled by causing the compiler to generate code affecting: bar = Mem.alloc(n) // bar[k]=19, or *(bar+k)=19 push bar push k add // Use a pointer to access x[k] pop addr // addr points to bar[k] push 19 pop *addr // Set bar[k] to 19 // bar[k]=19, or *(bar+k)=19 push bar push k add // Use a pointer to access x[k] pop addr // addr points to bar[k] push 19 pop *addr // Set bar[k] to 19 VM Code (pseudo) // bar[k]=19, or *(bar+k)=19 push local 2 push argument 0 add // Use the that segment to access x[k] pop pointer 1 push constant 19 pop that 0 // bar[k]=19, or *(bar+k)=19 push local 2 push argument 0 add // Use the that segment to access x[k] pop pointer 1 push constant 19 pop that 0 VM Code (actual) (bar array) bar x y 2 k (local 0) (local 1) (local 2) (argument 0) RAM state, just after executing bar[k]=19... Following compilation: Handling arrays

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 17www.nand2tetris.org Handling expressions x+g(2,y,-z)*5 push x push 2 push y push z neg call g push 5 call mult add push x push 2 push y push z neg call g push 5 call mult add Code generation Syntax analysis The codeWrite(exp) algorithm:

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 18www.nand2tetris.org Handling program flow

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 19www.nand2tetris.org Final example

Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 20www.nand2tetris.org Perspective Jack simplifications which are challenging to extend: Limited primitive type system No inheritance No public class fields (e.g. must use r = c.getRadius() rather than r = c.radius ) Jack simplifications which are easy to extend: : Limited control structures (no for, switch, …) Cumbersome handling of char types (cannot use let x= ‘ c ’ ) Optimization For example, c++ is translated inefficiently into push c, push 1, add, pop c. Parallel processing Many other examples of possible improvements …