You are on page 1of 16

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005 www.idc.ac.

il/tecs

Chapter 11:
Compiler II:
Code Generation
Usage and Copyright Notice:

Copyright 2005 © 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, 2005.

The book web site, www.idc.ac.il/tecs , features 13 such 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 for instructional and
non-commercial purposes.

If you use our materials, we will appreciate it if you will include in them a
reference to the book’s web site.
And, if you have any comments, you can reach us at tecs.ta@gmail.com

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 11: Compiler II: Code Generation slide 1
Course map

Human Abstract design Software


abstract interface
Thought hierarchy
Chapters 9, 12
H.L. Language Compiler
& abstract interface
Chapters 10 - 11
Operating Sys.
Virtual VM Translator
abstract interface
Machine Chapters 7 - 8

Assembly
Language

Assembler

Chapter 6

abstract interface
Computer
Machine Architecture
abstract interface
Language
Chapters 4 - 5
Hardware Gate Logic
abstract interface
Platform Chapters 1 - 3 Electrical
Chips & Engineering
Hardware Physics
Logic Gates
hierarchy

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 11: Compiler II: Code Generation slide 2
The big picture

„ Syntax analysis: understanding the code


„ Code generation: constructing semantics

(Chapter 10) XML


Jack Compiler code

Syntax Analyzer

Jack Toke-
Code (Chapter 11) VM
Parser Gene
Program nizer code
-ration

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 11: Compiler II: Code Generation slide 3
Syntax analysis (review)
Class
Class Bar
Bar {{ <varDec>
<varDec>
method
method Fraction
Fraction foo(int
foo(int y)y) {{ <keyword>
<keyword> var
var </keyword>
</keyword>
var
var int
int temp;
temp; //
// aa variable <keyword>
variable <keyword> int
int </keyword>
</keyword>
let temp = (xxx+12)*-63;
let temp = (xxx+12)*-63; <identifier>
... <identifier> temp </identifier>
temp </identifier>
... <symbol>
... <symbol> ;; </symbol>
</symbol>
... </varDec>
</varDec>
Syntax analyzer <statements>
<statements>
<letStatement>
The code generation challenge: <letStatement>
<keyword>
<keyword> let
let </keyword>
</keyword>
„ Extend the syntax analyzer into a <identifier>
<identifier> temp
temp </identifier>
</identifier>
full-blown compiler <symbol> = </symbol>
<symbol> = </symbol>
<expression>
„ Program = a series of operations <expression>
<term>
that manipulate data
<term>
<symbol>
<symbol> (( </symbol>
</symbol>
„ The compiler should convert each <expression>
<expression>
“understood” (parsed) source <term>
<term>
operation and data item into <identifier>
<identifier> xxx
xxx </identifier>
</identifier>
corresponding operations and data </term>
</term>
items in the target language <symbol>
<symbol> ++ </symbol>
</symbol>
<term>
<term>
„ So we have to generate code for <int.Const.>
<int.Const.> 1212 </int.Const.>
</int.Const.>
• handling data </term>
</term>


</expression>
</expression>
handling operations. ...
...
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 11: Compiler II: Code Generation slide 4
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 to it RAM resources)

„ What kind of variable is x?


local, static, field, argument ?
(Need to know in order to properly manage its life cycle).

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 11: Compiler II: Code Generation slide 5
Symbol table

Classical implementation:
„ A list of hash tables, each reflecting a single
scope nested within the next one in the list
„ The identifier lookup works from the current
table upwards.

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 11: Compiler II: Code Generation slide 6
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 handles all these details !!! Hurray!!!

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 11: Compiler II: Code Generation slide 7
Handling arrays
RAM state, just after executing bar[k]=19
Java code 0

class ...
class Complex
Complex {{
... 275 x (local 0)
...
void
void foo(int
foo(int k)k) {{ 276 y (local 1) Bar = new int(n)
int x, y;
int x, y; 277 4315 bar (local 2)
int[]
int[] bar;
bar; //
// declare
declare an
an array
array ... Is typically handled
... by causing the
... Following 504 2 k (argument 0)
//
// Construct
Construct the
the array:
array: ... compiler to generate
compilation:
code affecting:
bar
bar == new
new int[10];
int[10];
... 4315
...
bar[k]=19;
bar[k]=19; 4316 bar = Mem.alloc(n)
}} 4317 19
... (bar array)
... 4318
Main.foo(2);
Main.foo(2); // // Call
Call the
the foo
foo method
...
method ...
...
4324
...
VM Code (pseudo) VM Code (final)
//
// bar[k]=19,
bar[k]=19, or or *(bar+k)=19
*(bar+k)=19 //
// bar[k]=19,
bar[k]=19, oror *(bar+k)=19
*(bar+k)=19
push bar
push bar push local
push local 2 2
push
push kk push
push argument
argument 00
add
add add
add
//
// Use
Use aa pointer
pointer toto access
access x[k]
x[k] //
// Use
Use the
the that
that segment
segment to
to access
access x[k]
x[k]
pop addr // addr points to
pop addr // addr points to bar[k]bar[k] pop pointer
pop pointer 1 1
push
push 19
19 push
push constant
constant 1919
pop
pop *addr //
*addr // Set
Set bar[k]
bar[k] to
to 19
19 pop that
pop that 00

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 11: Compiler II: Code Generation slide 8
Handling objects: memory allocation
Java code
class
class Complex
Complex {{
//
// Properties
Properties (fields):
(fields):
int
int re; // Real part
re; // Real part
int
int im; // Imaginary part
im; // Imaginary part
...
...
/**
/** Constructs
Constructs aa newnew Complex
Complex object.
object. */*/
public Complex(int aRe, int
public Complex(int aRe, int aIm) { aIm) {
re
re == aRe;
aRe;
im = aIm; Following
im = aIm;
}} compilation:
...
...
}}
//
// The
The following
following code
code can
can be
be in
in any
any class:
class:
public void bla()
public void bla() { {
Complex
Complex a, a, b,
b, c;
c;
...
...
aa == new
new Complex(5,17);
Complex(5,17);
bb == new Complex(12,192);
new Complex(12,192);
...
...
cc == a;
a; //// Only
Only the
the reference
reference is is copied
copied foo = new ClassName(…)
...
...
}} Is typically handled by causing
the compiler to generate code
affecting:
foo = Mem.alloc(n)
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 11: Compiler II: Code Generation slide 9
Handling objects: operations

Java code
class
class Complex
Complex {{ Translating im = im * c :
//
// Properties
Properties (fields):
(fields):
int
int re; // Real part
re; // Real part „ Look up the symbol table
int
int im; // Imaginary part
im; // Imaginary part
...
... „ Resulting semantics:
/**
/** Constructs
Constructs aa new new Complex
Complex object.
object. */
*/
public Complex(int aRe, int
public Complex(int aRe, int aIm) { aIm) {
re
re == aRe;
aRe; //
im = aIm; // im
im == im
im ** cc ::
im = aIm; *(this+1)
*(this+1) == *(this+1)
*(this+1)
}} times
... times
... (argument
(argument 0)
0)
//
// Multiplication:
Multiplication:
public
public void
void mult
mult (int
(int c)
c) {{
re
re == re
re ** c;
c;
im = im *
im = im * c; c;
}} „ Of course this should be
written in the target
...
...
}}
language.

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 11: Compiler II: Code Generation slide 10
Handling objects: method calls
Java code Translating x.mult(5):
class
class Complex
Complex {{
//
// Properties
Properties (fields):
(fields): „ Can also be viewed as
int
int re; // Real part
re; // Real part mult(x,5)
int
int im; // Imaginary part
im; // Imaginary part
...
... „ Generated code:
/**
/** Constructs
Constructs aa new
new Complex
Complex object.
object. */
*/
public Complex(int aRe, int aIm)
public Complex(int aRe, int aIm) { {
re //
// x.mult(5):
x.mult(5):
re == aRe;
aRe;
im = aIm;
im = aIm; push
push xx
}} push
... push 55
...
}} call
call mult
mult

class
class FooFoo {{
...
... General rule: each method call
public
public void
void foo()
foo() {{
Complex foo.bar(v1,v2,...)
Complex x; x;
...
can be translated into
...
xx == new
new Complex(1,2);
Complex(1,2);
x.mult(5);
x.mult(5);
... push foo
... push v1
}}
}} push v2
...
call bar

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 11: Compiler II: Code Generation slide 11
Generating code for expressions

push
push xx
push
push 22
push
push yy
push
push zz
x+g(2,y,-z)*5 Syntax Code neg
x+g(2,y,-z)*5 analysis generation
neg
call
call gg
push
push 55
call
call mult
mult
add
add

The codeWrite(exp) algorithm:

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 11: Compiler II: Code Generation slide 12
Handling control flow (e.g. IF, WHILE)

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 11: Compiler II: Code Generation slide 13
Program flow

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 11: Compiler II: Code Generation slide 14
Final example

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 11: Compiler II: Code Generation slide 15
Perspective

„ “Hard” Jack simplifications:


z Primitive type system
z No inheritance
z No public class fields (e.g. must use r=c.getRadius() rather than r=c.radius)

„ “Soft” Jack simplifications:


z Limited control structures (no for, switch, …)
z Cumbersome handling of char types (cannot use let x=‘c’)

„ Optimization
z For example, c++ will be translated into push c, push 1, add, pop c.
z Parallel processing
z Many other examples of possible improvements …

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 11: Compiler II: Code Generation slide 16

You might also like