LANGUAGE TRANSLATORS: WEEK 24 TRANSLATION TO ‘INTERMEDIATE’ CODE (overview) Labs this week: Tutorial Exercises on Code Generation.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
1 Compiler Construction Intermediate Code Generation.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
ISBN Chapter 10 Implementing Subprograms.
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)
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Run time vs. Compile time
Run-time Environment and Program Organization
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
Summer 02-03Programming Language Concepts1 Programming Language Concepts (CS 360) Lecture 1: Overview, Grammars, and Little Languages Jeremy R. Johnson.
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Chapter 8 :: Subroutines and Control Abstraction
ITEC 352 Lecture 11 ISA - CPU. ISA (2) Review Questions? HW 2 due on Friday ISA –Machine language –Buses –Memory.
ITEC 380 Organization of programming languages Dr. Andrew Ray.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
COP4020 Programming Languages
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
CS 2104 Prog. Lang. Concepts Subprograms
Language Translators - Lee McCluskey LANGUAGE TRANSLATORS: WEEK 21 LECTURE: Using JavaCup to create simple interpreters
CSC 310 – Imperative Programming Languages, Spring, 2009 Virtual Machines and Threaded Intermediate Code (instead of PR Chapter 5 on Target Machine Architecture)
Programming Languages –14 David Watt (Glasgow) Steven Wong (Singapore) Moodle : Computing Science → Level 3 → Programming Languages 3 © 2012 David.
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
5-1 Chapter 5 - Languages and the Machine Department of Information Technology, Radford University ITEC 352 Computer Organization Principles of Computer.
Copyright © 2007 Addison-Wesley. All rights reserved.1-1 Reasons for Studying Concepts of Programming Languages Increased ability to express ideas Improved.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Programming Languages and Paradigms Imperative Programming.
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? , Semester 1,
Programming Languages
Winter Compilers Software Eng. Dept. – Ort Braude Compiling Assignments and Expressions Lecturer: Esti Stein brd4.ort.org.il/~esti2.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Names, Scope, and Bindings Programming Languages and Paradigms.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
ISBN Chapter 10 Implementing Subprograms.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Implementing Subprograms
CS 3304 Comparative Languages
PROGRAMMING LANGUAGES
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
课程名 编译原理 Compiling Techniques
Run-time organization
An Overview to Compiler Design
Implementing Subprograms
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CSE401 Introduction to Compiler Construction
Course Overview PART I: overview material PART II: inside a compiler
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 6 Programming the basic computer
Implementing Subprograms
Presentation transcript:

LANGUAGE TRANSLATORS: WEEK 24 TRANSLATION TO ‘INTERMEDIATE’ CODE (overview) Labs this week: Tutorial Exercises on Code Generation

REVIEW …. n Lexical Analysis - produces token sequence n Parsing - produces abstract syntax tree --- JavaCup and Interpreter Generation --- n Semantic Analysis - produces symbol table This week (briefly): Run Time Memory Management Intermediate Code Creation

Overview.. There are HUNDREDS (N) of programming languages (C, ADA, Fortran, Java, C++, Algol, Pascal, Modula, Miranda, Haskell, Scheme..) and several different language paradigms (Functional, Logical, Imperative + OO variants..) There are many (M) different ARCHITECTURES - (Intel, SPARC, VAX..) So we need N * M complete compilers....Or N ‘front ends’ + M ‘back ends’

Complexity of HLPL High Level PLs have n static and dynamic data structures n modules / objects n recursion, embedded function calls.. n Various forms of selection / iteration All have to be translated (eventually) into an executable- machine code (in a very simple language such as in Hugh Osborne’s Little Man Computer!?) with space for data, returned values, passed parameters

Typical Layout of an “Executable” RUN-TIME STACKhigh address free memory STATIC DATA MACHINE CODElow level

Run time management of local data If Programs had all global, static data and didn’t pass parameters to subprograms, things would be easy! Subroutine/function/procedure/method all need parameter passing rules.. E.g consider C - like function: foo(int x, y) { int z; … } x and y receive values initially, and they may change. After execution the space for x, y and z can be re-used...foo will be the address of the ‘returned’ value.

C-like Example.. int x = 0; int p(int a){ int b … int q(int c,d){ … return c+d+b+x;} ; return q(2*a,b)}; x = p(x+1);...

Stack Frames/Activation Records n The compiler creates a frame ‘template’ for each function (procedure, subroutine…) n During ‘runtime’, each invocation of a function causes a new stack frame to be created and the stack pointer moved down. n The same function can be invoked many times and have many stack frames at the same time (as in recursion). n Invocations need to be able to address frames higher up (via ‘static links’) n At the end of the ‘lifetime’ of the current frame, the stack pointer in moved up to the start of the current frame, and what is below it can be re-used.

Abstract Machine Code Generation Assume we have a target code which can store to, and fetch from memory, and can do basic arithmetic on values in registers in the CPU: ABSTRACT MACHINE INSTRUCTIONS: STORE T Y - store the contents of T in the space pointed to by Y FETCH X T - fetch the contents of X and put it into the temporary (register) T TIMES T1 T2 T3 - get the contents of registers T1,T2, multiply them, and put the result into temporary T3 GOTO LABEL - control jump to the line marked ‘label’ GOTO T1 LABEL_X LABEL_Y - conditional control jump

Abstract Machine Code Generation We will design code that ‘prints out’ the AMC, when input with the n abstract syntax tree n symbol table of a simple high level program. We will consider: - expressions, assignment, loops, recursion

“Expression” -> Abstract Machine Code INPUTS the abstract syntax tree of an expression and the SYMBOL TABLE at that point in the program;OUTPUTS the AMC program and STORES the value of the expression in a register. register generate_Exp_code( Expression_Tree E, Symbol_Table ST) = { register store1,store2,store3,store4; memory_reference variable_ref; case E instanceof Variable :/* E is a variable */ variable_ref = get_reference( E, ST) ;/* Find address from Sym Table */ store1 = free_register;/* Store the contents of variable_ref print( "FETCH ", variable_ref, store1, " ; ");/* in a free register */ return store1; E instanceof Times : /* E is a times operation LHS x RHS*/ store2 = generate_Exp_code(E.lhs, ST); /* Generate code for LHS */ store3 = generate_Exp_code(E.rhs, ST); /* Generate code for RHS */ store4 = free_register; print( "TIMES ", store2, store3, store4);/* Multiply register values together */ return store4;/* and return final register reference */ E instanceof Etc Etc.… END CASE }

“Assignment” -> Abstract Machine Code INPUTS the abstract syntax tree of an assignment statement and the SYMBOL TABLE at that point in the program: OUTPUTS the AMC program void generate_Stm_code( Syntax_Tree X, Symbol_Table ST) = …….. CASE X instanceof AssignmentStm { memory_reference variable_ref; register temp; variable_ref = get_reference( X.lhs, ST) ; /* -finds the reference of the LHS variable from the SYMBOL TABLE */ temp = generate_Exp_code(X.rhs, ST); /* -prints code for RHS, puts the value of the RHS into a register */ print( "STORE ", temp, variable_ref, " ; ") } …….

“While” => Abstract Machine Code /* While X.Exp Do X.Stm */ …... case X instanceof WhileStm { int i = 0; memory_reference variable_ref; register temp; print(label(i),”;”); i++ ; temp = generate_Exp_code(X.Exp, ST); print(“GOTO”, temp, label(i), label(i+1),”;”); print(label(i),”;”); i++ ; generate_Stm_code( Syntax_Tree X.Stm, Symbol_Table ST); print(“GOTO”, label(i-2),”;”); print(label(i),”;”); } …….

Exercises and Follow on work.. Go through exercises at the end of the handout, in labs Read Chapters 6 and 7 in Appel’s book Read Chapters 7 and 8 in online book (link from week 11 on Course Website)