1 Languages and Compilers (SProg og Oversættere) Code Generation.

Slides:



Advertisements
Similar presentations
8 VM code generation Aspects of code generation Address allocation
Advertisements

Intermediate Code Generation
8. Code Generation. Generate executable code for a target machine that is a faithful representation of the semantics of the source code Depends not only.
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.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 08a – Backpatching & Recap Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6.
1 Compiler Construction Intermediate Code Generation.
The Functions and Purposes of Translators Code Generation (Intermediate Code, Optimisation, Final Code), Linkers & Loaders.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch. 3: Compilation Spring 2007 Marco Valtorta.
1 Languages and Compilers (SProg og Oversættere) Lecture 11 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
1 Languages and Compilers (SProg og Oversættere) Lecture 2 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch. 3: Compilation Spring 2008 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.7: Code Generation Spring 2008 Marco Valtorta.
Run-Time Storage Organization
Run time vs. Compile time
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.1 Spring 2010 Marco Valtorta
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.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Compilation (Chapter 3) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Syntax Analysis (Chapter 4) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
Runtime Environments Compiler Construction Chapter 7.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Compiler Construction
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
Interpretation (Chapter 8) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Introduction (Chapter 1) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
5-1 5 Compilation  Overview  Compilation phases –syntactic analysis –contextual analysis –code generation  Abstract syntax trees  Case study: Fun language.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Language Translation A programming language processor is any system that manipulates programs expressed in a PL A source program in some source language.
Contextual Analysis (Chapter 5) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Review (Chapter 9) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Wei-Tek Tsai who’s.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
Code Generation How to produce intermediate or target code.
1 Languages and Compilers (SProg og Oversættere) Lecture 9 (1) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Code Generation (Chapter 7) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
1 Languages and Compilers (SProg og Oversættere) Semantic Analysis.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 14 Functions.
01 – Overview of Compilers
Compiler Design (40-414) Main Text Book:
Course Overview PART I: overview material PART II: inside a compiler
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Enumeration Types In Triangle.
6.001 SICP Compilation Context: special purpose vs. universal machines
Languages and Compilers (SProg og Oversættere)
Compiler Construction
Code Generation.
Chapter 6 Intermediate-Code Generation
Languages and Compilers (SProg og Oversættere)
Course Overview PART I: overview material PART II: inside a compiler
CSCE 531 Compiler Construction Ch.7: Code Generation
Course Overview PART I: overview material PART II: inside a compiler
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Languages and Compilers (SProg og Oversættere)
Course Overview PART I: overview material PART II: inside a compiler
Languages and Compilers (SProg og Oversættere)
Presentation transcript:

1 Languages and Compilers (SProg og Oversættere) Code Generation

2 –Describe the purpose of the code generator –Code templates –Back patching

3 The “Phases” of a Compiler Syntax Analysis Contextual Analysis Code Generation Source Program Abstract Syntax Tree Decorated Abstract Syntax Tree Object Code Error Reports

4 Multi Pass Compiler Compiler Driver Syntactic Analyzer calls Contextual AnalyzerCode Generator calls Dependency diagram of a typical Multi Pass Compiler: A multi pass compiler makes several passes over the program. The output of a preceding phase is stored in a data structure and used by subsequent phases. input Source Text output AST input output Decorated AST input output Object Code

5 Issues in Code Generation Code Selection: Deciding which sequence of target machine instructions will be used to implement each phrase in the source language. Storage Allocation Deciding the storage address for each variable in the source program. (static allocation, stack allocation etc.) Register Allocation (for register-based machines) How to use registers efficiently to store intermediate results.

6 Code Generation Source Program let var n: integer; var c: char in begin c := ‘&’; n := n+1 end PUSH 2 LOADL 38 STORE 1[SB] LOAD 0 LOADL 1 CALL add STORE 0[SB] POP 2 HALT Target program ~ ~ Source and target program must be “semantically equivalent” Semantic specification of the source language is structured in terms of phrases in the SL: expressions, commands, etc. => Code generation follows the same “inductive” structure. Q: Can you see the connection with formal semantics?

7 Specifying Code Generation with Code Templates Example: Code templates specification for Mini Triangle RECAP: The mini triangle AST Program ::= Command Program Command ::= V-name := Expression AssignCmd | let Declaration in Command LetCmd... Expression ::= Integer-Literal IntegerExp | V-name VnameExp | Operator Expression UnaryExp | Expression Op Expression BinaryExp Declaration ::=... V-name::= Identifier SimpleVName Program ::= Command Program Command ::= V-name := Expression AssignCmd | let Declaration in Command LetCmd... Expression ::= Integer-Literal IntegerExp | V-name VnameExp | Operator Expression UnaryExp | Expression Op Expression BinaryExp Declaration ::=... V-name::= Identifier SimpleVName

8 Specifying Code Generation with Code Templates The code generation functions for Mini Triangle Phrase Class Function Effect of the generated code Program Command Expres- sion V-name Decla- ration run P execute C evaluate E fetch V assign V elaborate D Run program P then halt. Starting and finishing with empty stack Execute Command C. May update variables but does not shrink or grow the stack! Evaluate E, net result is pushing the value of E on the stack. Push value of constant or variable on the stack. Pop value from stack and store in variable V Elaborate declaration, make space on the stack for constants and variables in the decl.

9 Code Generation with Code Templates run [ C ] = execute [ C ] HALT The code generation functions for Mini Triangle Programs: Commands: execute [ V := E ] = evaluate [ E ] assign [ V ] execute [ I ( E ) ] = evaluate [ E ] CALL p where p is address of the routine named I

10 Code Generation with Code Templates Commands: execute [ C1 ; C2 ] = execute [ C1 ] execute [ C2 ] execute [ if E then C1 else C2 ] = evaluate [ E ] JUMPIF(0) g execute [ C1 ] JUMP h g: execute [ C2 ] h: C1 C2 E g:g: h:h:

11 Code Generation with Code Templates execute [ while E do C ] = JUMP h g: execute [ C ] h: evaluate[ E ] JUMPIF(1) g C E While command

12 Developing a Code Generator “Visitor” public Object visitSequentialCommand( SequentialCommand com,Object arg) { com.C1.visit(this,arg); com.C2.visit(this,arg); return null; } public Object visitSequentialCommand( SequentialCommand com,Object arg) { com.C1.visit(this,arg); com.C2.visit(this,arg); return null; } execute [ C1 ; C2 ] = execute[ C1 ] execute[ C2 ] LetCommand, IfCommand, WhileCommand => later. - LetCommand is more complex: memory allocation and addresses - IfCommand and WhileCommand: complications with jumps

13 Backpatching Example public Object WhileCommand ( WhileCommand com,Object arg) { short j = nextInstrAddr; emit(Instruction.JUMPop, 0, Instruction.CBr,0); short g = nextInstrAddr; com.C.visit(this,arg); short h = nextInstrAddr; code[j].d = h; com.E.visit(this,arg); emit(Instruction.JUMPIFop, 1, Instruction.CBr,g); return null; } public Object WhileCommand ( WhileCommand com,Object arg) { short j = nextInstrAddr; emit(Instruction.JUMPop, 0, Instruction.CBr,0); short g = nextInstrAddr; com.C.visit(this,arg); short h = nextInstrAddr; code[j].d = h; com.E.visit(this,arg); emit(Instruction.JUMPIFop, 1, Instruction.CBr,g); return null; } execute [ while E do C ] = JUMP h g: execute [ C ] h: evaluate[ E ] JUMPIF(1) g dummy address backpatch

14 Constants and Variables We have not yet discussed generation of LetCommand. This is the place in MiniTriangle where declarations are. execute [ let D in C ] = elaborate[ D ] execute [ C ] POP(0) s if s >0 where s = amount of storage allocated by D How to know these? fetch [ V ] = LOAD d[SB] where d = address of V relative to SB assign [ V ] = STORE d[SB] where d = address of V relative to SB Calculated during generation for elaborate[ D ]

15 Code Template: Global Procedure elaborate [ proc I () ~ C ] = JUMP g e: execute [ C ] RETURN(0) 0 g: C execute [ I () ] = CALL(SB) e

16 Code Template: Global Procedure Example: let var n: Integer; proc double() ~ n := n*2 in begin n := 9; double() end let var n: Integer; proc double() ~ n := n*2 in begin n := 9; double() end 0:PUSH1 1:JUMP7 2:LOAD0[SB] 3:LOADL2 4:CALLmult 5:STORE0[SB] 6:RETURN(0)0 7:LOADL9 8:STORE0[SB] 9:CALL(SB)2 10:POP(0)1 11:HALT n := n*2 var n: Integer proc double() ~ n := n*2 n := 9 double()