Lecture #19: Code Generation

Similar documents
Intermediate Languages and Machine Languages. Lecture #31: Code Generation. Stack Machine with Accumulator. Stack Machines as Virtual Machines

Lecture #31: Code Generation

Code Generation. Lecture 30

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

Code Generation. Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

Code Generation. Lecture 19

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

The Activation Record (AR)

The remote testing experiment. It works! Code Generation. Lecture 12. Remote testing. From the cs164 newsgroup

Code Generation. Lecture 12

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Assignment 11: functions, calling conventions, and the stack

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

Compiler Construction D7011E

CS4215 Programming Language Implementation

Code Generation Super Lectures

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

Parsing Scheme (+ (* 2 3) 1) * 1

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

Intel assembly language using gcc

CS61 Section Solutions 3

Integers and variables

Assembly Language: Function Calls

COMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation

x86 assembly CS449 Fall 2017

Assembly Language: Function Calls" Goals of this Lecture"

Compilers and computer architecture: A realistic compiler to MIPS

W4118: PC Hardware and x86. Junfeng Yang

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Assembly Language: Function Calls" Goals of this Lecture"

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

CIT Week13 Lecture

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Machine Programming 3: Procedures

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Integers and variables

Introduction to Compilers Professor Jeremy Siek

Project Compiler. CS031 TA Help Session November 28, 2011

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

Administration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator

CS 2210 Programming Project (Part IV)

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Syntax-Directed Translation. Lecture 14

CIS 341 Midterm February 28, Name (printed): Pennkey (login id): SOLUTIONS

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

CS 164, Midterm #2, Spring O, M, C - e1 : Bool O, M, C - e2 : t2 O, M, C - e2 : T3 O, M, C - if e1 then e2 else e3 fi : T2 _ T3

Other Forms of Intermediate Code. Local Optimizations. Lecture 34

Administrative. Other Forms of Intermediate Code. Local Optimizations. Lecture 34. Code Generation Summary. Why Intermediate Languages?

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

Subprograms: Local Variables

THEORY OF COMPILATION

CS213. Machine-Level Programming III: Procedures

You may work with a partner on this quiz; both of you must submit your answers.

G Programming Languages - Fall 2012

Systems I. Machine-Level Programming V: Procedures

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 6

Intermediate Code Generation

CS415 Compilers. Intermediate Represeation & Code Generation

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Computer Systems Lecture 9

CS241 Computer Organization Spring 2015 IA

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

ASSEMBLY III: PROCEDURES. Jo, Heeseung

IA32 Stack. Lecture 5 Machine-Level Programming III: Procedures. IA32 Stack Popping. IA32 Stack Pushing. Topics. Pushing. Popping

Assembly III: Procedures. Jo, Heeseung

Chapter 4 Processor Architecture: Y86 (Sections 4.1 & 4.3) with material from Dr. Bin Ren, College of William & Mary

Assembly Programming (III)

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

Giving credit where credit is due

Review Questions. 1 The DRAM problem [5 points] Suggest a solution. 2 Big versus Little Endian Addressing [5 points]

Assembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu

CPSC W Term 2 Problem Set #3 - Solution

ECE232: Hardware Organization and Design

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

Sungkyunkwan University

x86 assembly CS449 Spring 2016

Credits and Disclaimers

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm

Digital Forensics Lecture 3 - Reverse Engineering

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum

CS429: Computer Organization and Architecture

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

Names, Scope, and Bindings

ECS 142 Project: Code generation hints

The x86 Architecture

Transcription:

Lecture #19: Code Generation [This lecture adopted in part from notes by R. Bodik] Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 1

Intermediate Languages and Machine Languages From trees such as output from project #2, could produce machine language directly. However, itis oftenconvenientto first generatesome kindof intermediate language (IL): a high-level machine language for a virtual machine. Advantages: Separates problem of extracting the operational meaning (the dynamic semantics) of a program from the problem of producing good machine code from it, because it Gives a clean target for code generation from the AST. By choosing IL judiciously, we can make the conversion of IL machine language easier than the direct conversion of AST machine language. Helpful when we want to target several different architectures (e.g., gcc). Likewise, if we can use the same IL for multiple languages, we can re-use the IL machine language implementation (e.g., gcc, CIL from Microsoft s Common Language Infrastructure). Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 2

Stack Machines as Virtual Machines A simple evaluation model: instead of registers, a stack of values for intermediate results. Examples: The Java Virtual Machine, the Postscript interpreter. Each operation (1) pops its operands from the top of the stack, (2) computes the required operation on them, and (3) pushes the result on the stack. A program to compute 7 + 5: push 7 push 5 add Advantages # Push constant 7 on stack # Pop two 5 and 7 from stack, add, and push result. Uniform compilation scheme: Each operation takes operands from the same place and puts results in the same place. Fewer explict operands in instructions means smaller encoding of instructions and more compact programs. Meshes nicely with subroutine calling conventions that push arguments on stack. Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 3

Stack Machine with Accumulator The add instruction does 3 memory operations: Two reads and one write of the stack. The top of the stack is frequently accessed Idea: keep most recently computed value in a register (called the accumulator) since register accesses are faster. For an operation op(e 1,,e n ): compute each of e 1,,e n 1 into acc and then push on the stack; compute e n into the accumulator; perform op computation, with result in acc. pop e 1,,e n 1 off stack. The add instruction is now acc := acc + top_of_stack pop one item off the stack and uses just one memory operation (popping just means adding constant to stack-pointer register). After computing an expression the stack is as it was before computing the operands. Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 4

Example: Full computation of 7+5 acc := 7 push acc acc := 5 acc := acc + top_of_stack pop stack Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 5

A Point of Order Often more convenient to push operands in reverse order, so rightmost operand pushed first. This is a common convention for pushing function arguments, and is especially natural when stack grows toward lower addresses. Also nice for non-commutative operations on architectures such as the ia32. Example: compute x - y. We show assembly code on the right acc := y push acc acc := x acc := acc - top_of_stack pop stack movl y, %eax pushl %eax movl x, %eax subl (%esp), %eax addl $4, %esp Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 6

Translating from AST to Stack Machine A simple recursive pattern usually serves for expressions. At the top level, our trees might have an expression-code method: class AST { /** Generate code for me, leaving my value on the stack. */ virtual void cgen (VM* machine); Implementations of cgen then obey this general comment, and each assumes that its children will as well. E.g., class BinopNode : public AST { void cgen (VM* machine) { getright ()->cgen (machine); getleft ()->cgen (machine); machine->emitinst (translatetoinst (getop ())); We assume here a VM is some abstraction of the virtual machine we re producing code for. emitinst adds machine instructions to the program, and translatetoinst converts, e.g., a + to add. Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 7

Virtual Register Machines and Three-Address Code Another common kind of virtual machine has an infinite supply of registers, each capable of holding a scalar value or address, in addition to ordinary memory. A common IL in this case is some form of three-address code, so called because the typical working instruction has the form target := operand 1 operand 2 where there are two source addresses, one destination address and an operation ( ). Often, we require that the operands in the full three-address form denote (virtual) registers or immediate (literal) values. Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 8

Three-Address Code, continued A few other forms deal with memory and other kinds of operation: memory_operand := register_or_immediate_operand register_operand := memory_operand goto label if operand1 operand2 then goto label param operand ; Push parameter for call. call operand, # of parameters ; Call, put return in specific re Here, stands for some kind of comparison. Memory operands might be labels of static locations, or indexed operands such as (in C-like notation) *(r1+4) or *(r1+r2). Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 9

Translating from AST into Three-Address Code This time, we ll have the cgen routine return where it has put its result: class AST { /** Generate code to compute my value, returning the location * of the result. */ virtual Operand* cgen (VM* machine); Where an Operand denotes some abstract place holding a value. Once again, we rely on our children to obey this general comment: class BinopNode : public AST { Operand* cgen (VM* machine) { Operand* left = getleft ()->cgen (machine); Operand* right = getright ()->cgen (machine); Operand* result = machine->allocateregister (); machine->emitinst (result, translatetoinst (getop ()), left, right); return result; emitinst now produces three-address instructions. Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 10

A Larger Example Consider a small language with integers and integer operations: P: D ";" P D D: "def" id(args) "=" E; ARGS: id "," ARGS id E: int id "if" E1 "=" E2 "then" E3 "else" E4 "fi" E1 "+" E2 E1 "-" E2 id "(" E1,,En ")" The first function definition f is the main routine Running the program on input i means computing f(i) Assume a project-2-like AST. Let s continue implementing cgen ( + and - already done). Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 11

Conversion of D ";" P: Simple Cases: Literals and Sequences class StmtListNode : public AST { Operand* cgen (VM* machine) { for (int i = 0; i < arity (); i += 1) get (i)->cgen (machine); return Operand::NoneOperand; class IntLiteralNode : public AST { Operand* cgen (VM* machine) { return machine->immediateoperand (inttokenvalue ()); NoneOperand is an Operand that contains None. Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 12

Identifiers class IdNode : public AST { Operand* cgen (VM* machine) { Operand result = machine->allocateregister (); machine->emitinst (MOVE, result, getdecl()->getmylocation (machine)); return result; That is, we assume that the declaration object holding information about this occurrence of the identifier contains its location. Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 13

Calls class CallNode : public AST { Operand* cgen (VM* machine) { AST* args = getarglist (); for (int i = args->arity ()-1; i >= 0; i -= 1) machine->emitinst (PARAM, args.get (i)->cgen (machine)); Operand* callable = getcallable ()->cgen (machine); machine->emitinst (CALL, callable, args->arity ()); return Operand::ReturnOperand; ReturnOperand is abstract location where functions return their value. Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 14

Control Expressions: if class IfExprNode : public AST { Operand* cgen (VM* machine) { Operand* left = getleft ()->cgen (machine); Operand* right = getright ()->cgen (machine); Label* elselabel = machine->newlabel (); Label* donelabel = machine->newlabel (); machine->emitinst (IFNE, left, right, elselabel); Operand* result = machine->allocateregister (); machine->emitinst (MOVE, result, getthenpart ()->cgen (machine)); machine->emitinst (GOTO, donelabel); machine->placelabel (elselabel); machine->emitinst (MOVE, result, getelsepart ()->cgen (machine)); machine->placelabel (donelabel); return result; newlabel creates a new, undefined assembler instruction label. placelabel inserts a definition of the label in the code. Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 15

Code generation for def class DefNode : public AST { Operand* cgen (VM* machine) { machine->placelabel (getname ()); machine->emitfunctionprologue (); Operand* result = getbody ()->cgen (machine); machine->emitinst (MOVE, Operand::ReturnOperand, result); machine->emitfunctionepilogue (); return Operand::NoneOperand; Where function prologues and epilogues are standard code sequences for entering and leaving functions, setting frame pointers, etc. Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 16

A Sample Translation Program for computing the Fibonacci numbers: def fib(x) = if x = 1 then 0 else if x = 2 then 1 else fib(x - 1) + fib(x - 2) Possible code generated: f: function prologue r1 := x L3: r5 := x if r1!= 1 then goto L1 r6 := r5-1 r2 := 0 param r6 goto L2 call fib, 1 L1: r3 := x r7 := rret if r3!= 2 then goto L3 r8 := x r4 := 1 r9 := r8-2 goto L4 param r9 call fib, 1 r10 := r7 + rret r4 := r10 L4: r2 := r4 L2: rret := r2 function epilogue Last modified: Wed Oct 31 12:18:49 2012 CS164: Lecture #19 17