Code Generation Compiler Baojian Hua

Slides:



Advertisements
Similar presentations
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Advertisements

Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
1 Compiler Construction Intermediate Code Generation.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Elaboration or: Semantic Analysis Compiler Baojian Hua
UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-
Yu-Chen Kuo1 Chapter 1 Introduction to Compiling.
Function Compiler Baojian Hua Function, or Procedure, or method, or … High-level abstraction of code logically-grouped Good for many.
CS 536 Spring Run-time organization Lecture 19.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
Standard ML- Part III Compiler Baojian Hua
Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua
Abstract Syntax Trees Compiler Baojian Hua
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Elaboration or: Semantic Analysis Compiler Baojian Hua
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
Semantics of Calls and Returns
X86 ISA Compiler Baojian Hua Front End source code abstract syntax tree lexical analyzer parser tokens IR semantic analyzer.
4/6/08Prof. Hilfinger CS164 Lecture 291 Code Generation Lecture 29 (based on slides by R. Bodik)
Compiler Construction Lecture 17 Mapping Variables to Memory.
ITEC 352 Lecture 11 ISA - CPU. ISA (2) Review Questions? HW 2 due on Friday ISA –Machine language –Buses –Memory.
Imperative Programming
6.828: PC hardware and x86 Frans Kaashoek
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
CSC 310 – Imperative Programming Languages, Spring, 2009 Virtual Machines and Threaded Intermediate Code (instead of PR Chapter 5 on Target Machine Architecture)
Runtime Environments Compiler Construction Chapter 7.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Code Generation Compiler Baojian Hua
Languages and the Machine Chapter 5 CS221. Topics The Compilation Process The Assembly Process Linking and Loading Macros We will skip –Case Study: Extensions.
Abstract Syntax Trees Compiler Baojian Hua
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
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.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? , Semester 1,
Exception Compiler Baojian Hua
Introduction CPSC 388 Ellen Walker Hiram College.
Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY Week 7 and 8(10.14 and 10.21): The lexical of TINY + Implement.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Procedures and Functions Procedures and Functions – subprograms – are named fragments of program they can be called from numerous places  within a main.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *
Assembly language.
Names and Attributes Names are a key programming language feature
CS 363 – Chapter 1 What is a programming language? Kinds of languages
Run-time organization
Introduction to Compilers Tim Teitelbaum
Chapter 1: Introduction to Compiling (Cont.)
CS 536 / Fall 2017 Introduction to programming languages and compilers
Computer Architecture and Assembly Language
Discussion Section – 11/3/2012
Basic Program Analysis: AST
CSE401 Introduction to Compiler Construction
Lecture 30 (based on slides by R. Bodik)
Memory Allocation CS 217.
COP4020 Programming Languages
Information Security - 2
Multi-modules programming
UNIT V Run Time Environments.
Course Overview PART I: overview material PART II: inside a compiler
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Compiler Structures 1. Overview Objective
Computer Architecture and System Programming Laboratory
IS 135 Business Programming
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Code Generation Compiler Baojian Hua

Front End source code abstract syntax tree lexical analyzer parser tokens IR semantic analyzer

Back End IR TempMap instruction selector register allocator Assem instruction scheduler

Code Generation Generating code for some ISA this course uses x86 Many components instruction selection, register allocation, scheduling, … Many different strategies for this time, we concentrate on a simple one: stack machine and later in this course, we ’ d turn to more advanced (and sophisticated) ones

What ’ s a stack machine? A stack machine has only an operand stack and no (or few) registers all computation performed on the operand stack architecture very simple and uniform Long history: Date back at least to 70 ’ s last century Renew industry ’ s interest in the recent decade Sun ’ s JVM and Microsoft ’ s CLR, etc.

Stack Machine ISA: s86 prog -> instr prog -> instr -> push v -> pop id -> add -> sub -> times -> divide v -> num -> id // Sample Program push 8 push 2 push x times sub

The simple expression lang ’ // recall our simple // expression language exp -> num -> id -> exp + exp -> exp – exp -> exp * exp -> exp / exp -> (exp) // or in ML datatype exp = Int of int | Id of string | Add of exp * exp | Sub of exp * exp | Times of exp * exp | Divide of exp * exp // Sample Program 8-2*x

Code gen ’ from exp to s86 C (num) = push num C (id) = push id C (e1 + e2) = C (e1); C (e2); add C (e1 – e2) = C (e1); C (e2); sub C (e1 * e2) = C (e1); C (e2); times C (e1 / e2) = C (e1); C (e2); divide

Code gen ’ from exp to s86 // or in ML fun C (e) = case e of Num i => push i | Id s => push s | Add (e1, e2) => C (e1); C (e2); add | … => (* similar *)

Example C (8-2*x) = C(8); C(2*x); sub = push 8; C(2*x); sub = push 8; C(2); C(x); times; sub = …

Moral Code generation for stack machine is dirty simple recursive equation from point view of math recursive function from point view of CS think before hack! But we ’ d have more to say about: variable storage more language features statement, declarations, functions, etc..

Address space Address space is the way how programs use memory highly architecture and OS dependent right is the typical layout of 32-bit x86/Linux OS heap data text BIOS, VGA 0x stack 0xc x x xffffffff

Static Storage Static storage is an area of space in data section a typical use is to hold C/C++ file scope variables (static) and extern variable (global) Exp lang ’ has only static variables, all can be stored to static section so require a pass to collect all variables

Declarations // scale exp a bit prog -> decs exp decs -> int id; decs -> exp -> … // or in ML datatype decs = T of {var: string, ty: tipe} list // Sample Program int x; 8-2*x;

Code gen ’ rules D (int id; decs) = id:.int 0 D (decs) D ( ) =

Statement // scale the exp a by adding the following: s -> id = e; -> if (e) s else s // compile: CS (id = e;) = C (e); pop id

Statement, cont ’ // s86 should also be modified! // compile: CS (if e s1 s2) = C(e); jz.Lfalse.Ltrue: CS(s1) jmp.Lend.Lfalse: CS(s2).Lend e s1s2 …

Moral It ’ s also straightforward to translate other control structure in this style while, for, switch, etc.. This kind of code generation is called recursive decedent may be done at parsing time adopted in many compilers read the offered article on Borland Turbo Pascal 3.0 you may safely ignore the Pascal-specific features

From s86 to x86 Run the generated s86 code? design a virtual machine as we did in lab #1 this is also the way of JVM or CLR translate to native code and then exec ’ it so-called just-in-time (JIT) the dominant OO method today … Next, we discuss the 2nd method by mapping s86 to x86

Operand Stack // x86 does not have a dedicated operand stack? // Solution 1: use the control stack: ebp, esp // leave to you. // Solution 2: make a fake operand stack, as in:.set PAGE, 4096.data opStack:.space PAGE, 0xcc top:.int opStack+PAGE // “top” points to stack top, and stack grows // down to lower address

Instructions // map fake s86 instructions to x86’s:.macro s86push x sub dword ptr [top], 4 mov ebx, [top] mov eax, \x mov [ebx], eax.endm // others are similar. // Care must be taken to take account of the // machine constraints. For instance, mem-mem // move is illegal on x86.