30.05.2013 Views

Computer Languages - Translation to intermediate code

Computer Languages - Translation to intermediate code

Computer Languages - Translation to intermediate code

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Computer</strong> <strong>Languages</strong><br />

<strong>Translation</strong> <strong>to</strong> <strong>intermediate</strong> <strong>code</strong><br />

Verónica Gaspes<br />

School of Information Science, <strong>Computer</strong> and Electrical Engineering<br />

April 27<br />

www.hh.se/staff/vero/languages<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Structure of a compiler<br />

A compiler translates a program written in some programming<br />

language <strong>to</strong> <strong>code</strong> that can be executed on some machine.<br />

Source −→<br />

program<br />

Our compiler<br />

IR<br />

Front End Back End<br />

Compiler<br />

−→ Target<br />

program<br />

In this project we will concentrate ourselves on the programming<br />

language minijava, a target architecture called MIPS (a RISC<br />

architecture) and a suitable <strong>intermediate</strong> representation.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Structure of a compiler<br />

A compiler translates a program written in some programming<br />

language <strong>to</strong> <strong>code</strong> that can be executed on some machine.<br />

Source −→<br />

program<br />

Our compiler<br />

IR<br />

Front End Back End<br />

Compiler<br />

−→ Target<br />

program<br />

In this project we will concentrate ourselves on the programming<br />

language minijava, a target architecture called MIPS (a RISC<br />

architecture) and a suitable <strong>intermediate</strong> representation.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Structure of a compiler<br />

A compiler translates a program written in some programming<br />

language <strong>to</strong> <strong>code</strong> that can be executed on some machine.<br />

Source −→<br />

program<br />

Our compiler<br />

IR<br />

Front End Back End<br />

Compiler<br />

−→ Target<br />

program<br />

In this project we will concentrate ourselves on the programming<br />

language minijava, a target architecture called MIPS (a RISC<br />

architecture) and a suitable <strong>intermediate</strong> representation.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Structure of a compiler<br />

A compiler translates a program written in some programming<br />

language <strong>to</strong> <strong>code</strong> that can be executed on some machine.<br />

Source −→<br />

program<br />

Our compiler<br />

IR<br />

Front End Back End<br />

Compiler<br />

−→ Target<br />

program<br />

In this project we will concentrate ourselves on the programming<br />

language minijava, a target architecture called MIPS (a RISC<br />

architecture) and a suitable <strong>intermediate</strong> representation.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Structure of a compiler<br />

A compiler translates a program written in some programming<br />

language <strong>to</strong> <strong>code</strong> that can be executed on some machine.<br />

Source −→<br />

program<br />

Our compiler<br />

IR<br />

Front End Back End<br />

Compiler<br />

−→ Target<br />

program<br />

In this project we will concentrate ourselves on the programming<br />

language minijava, a target architecture called MIPS (a RISC<br />

architecture) and a suitable <strong>intermediate</strong> representation.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Structure of a compiler<br />

A compiler translates a program written in some programming<br />

language <strong>to</strong> <strong>code</strong> that can be executed on some machine.<br />

Source −→<br />

program<br />

Our compiler<br />

IR<br />

Front End Back End<br />

Compiler<br />

−→ Target<br />

program<br />

In this project we will concentrate ourselves on the programming<br />

language minijava, a target architecture called MIPS (a RISC<br />

architecture) and a suitable <strong>intermediate</strong> representation.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Structure of a compiler<br />

A compiler translates a program written in some programming<br />

language <strong>to</strong> <strong>code</strong> that can be executed on some machine.<br />

Source −→<br />

program<br />

Our compiler<br />

IR<br />

Front End Back End<br />

Compiler<br />

−→ Target<br />

program<br />

In this project we will concentrate ourselves on the programming<br />

language minijava, a target architecture called MIPS (a RISC<br />

architecture) and a suitable <strong>intermediate</strong> representation.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Structure of a compiler<br />

A compiler translates a program written in some programming<br />

language <strong>to</strong> <strong>code</strong> that can be executed on some machine.<br />

Source −→<br />

program<br />

Our compiler<br />

IR<br />

Front End Back End<br />

Compiler<br />

−→ Target<br />

program<br />

In this project we will concentrate ourselves on the programming<br />

language minijava, a target architecture called MIPS (a RISC<br />

architecture) and a suitable <strong>intermediate</strong> representation.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Structure of a compiler<br />

A compiler translates a program written in some programming<br />

language <strong>to</strong> <strong>code</strong> that can be executed on some machine.<br />

Source −→<br />

program<br />

Our compiler<br />

IR<br />

Front End Back End<br />

Compiler<br />

−→ Target<br />

program<br />

In this project we will concentrate ourselves on the programming<br />

language minijava, a target architecture called MIPS (a RISC<br />

architecture) and a suitable <strong>intermediate</strong> representation.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Structure of a compiler<br />

A compiler translates a program written in some programming<br />

language <strong>to</strong> <strong>code</strong> that can be executed on some machine.<br />

Source −→<br />

program<br />

Our compiler<br />

IR<br />

Front End Back End<br />

Compiler<br />

−→ Target<br />

program<br />

In this project we will concentrate ourselves on the programming<br />

language minijava, a target architecture called MIPS (a RISC<br />

architecture) and a suitable <strong>intermediate</strong> representation.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


The front end<br />

Source program<br />

Lexicalanalyser<br />

Token stream<br />

Abstract syntax<br />

Parser<br />

<strong>Translation</strong><br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong><br />

Abstract syntax<br />

IR trees<br />

Static<br />

semantics


The front end<br />

Source program<br />

Lexicalanalyser<br />

Token stream<br />

Abstract syntax<br />

Parser<br />

<strong>Translation</strong><br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong><br />

Abstract syntax<br />

IR trees<br />

Static<br />

semantics


The front end<br />

Source program<br />

Lexicalanalyser<br />

Token stream<br />

Abstract syntax<br />

Parser<br />

<strong>Translation</strong><br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong><br />

Abstract syntax<br />

IR trees<br />

Static<br />

semantics


The front end<br />

Source program<br />

Lexicalanalyser<br />

Token stream<br />

Abstract syntax<br />

Parser<br />

<strong>Translation</strong><br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong><br />

Abstract syntax<br />

IR trees<br />

Static<br />

semantics


The front end<br />

Source program<br />

Lexicalanalyser<br />

Token stream<br />

Abstract syntax<br />

Parser<br />

<strong>Translation</strong><br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong><br />

Abstract syntax<br />

IR trees<br />

Static<br />

semantics


The front end<br />

Source program<br />

Lexicalanalyser<br />

Token stream<br />

Abstract syntax<br />

Parser<br />

<strong>Translation</strong><br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong><br />

Abstract syntax<br />

IR trees<br />

Static<br />

semantics


The front end<br />

Source program<br />

Lexicalanalyser<br />

Token stream<br />

Abstract syntax<br />

Parser<br />

<strong>Translation</strong><br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong><br />

Abstract syntax<br />

IR trees<br />

Static<br />

semantics


The front end<br />

Source program<br />

Lexicalanalyser<br />

Token stream<br />

Abstract syntax<br />

Parser<br />

<strong>Translation</strong><br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong><br />

Abstract syntax<br />

IR trees<br />

Static<br />

semantics


The front end<br />

Source program<br />

Lexicalanalyser<br />

Token stream<br />

Abstract syntax<br />

Parser<br />

<strong>Translation</strong><br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong><br />

Abstract syntax<br />

IR trees<br />

Static<br />

semantics


The front end<br />

Source program<br />

Lexicalanalyser<br />

Token stream<br />

Abstract syntax<br />

Parser<br />

<strong>Translation</strong><br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong><br />

Abstract syntax<br />

IR trees<br />

Static<br />

semantics


The translation phase<br />

Lexical, syntactical and contextual analysis are phases of all<br />

language processors of all computer languages (which we have<br />

exemplified with minijava).<br />

The translation phase is more dependent on the kind of<br />

computer language being processed.<br />

For programming languages it is taking a step <strong>to</strong>wards<br />

executable <strong>code</strong>.<br />

We will present some general techniques and abstractions for<br />

doing so.<br />

Note<br />

We will exemplify with minijava when possible. If minijava doesn’t<br />

have the feature we want <strong>to</strong> illustrate we will refer <strong>to</strong> some other<br />

programming language.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


The translation phase<br />

Lexical, syntactical and contextual analysis are phases of all<br />

language processors of all computer languages (which we have<br />

exemplified with minijava).<br />

The translation phase is more dependent on the kind of<br />

computer language being processed.<br />

For programming languages it is taking a step <strong>to</strong>wards<br />

executable <strong>code</strong>.<br />

We will present some general techniques and abstractions for<br />

doing so.<br />

Note<br />

We will exemplify with minijava when possible. If minijava doesn’t<br />

have the feature we want <strong>to</strong> illustrate we will refer <strong>to</strong> some other<br />

programming language.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


The translation phase<br />

Lexical, syntactical and contextual analysis are phases of all<br />

language processors of all computer languages (which we have<br />

exemplified with minijava).<br />

The translation phase is more dependent on the kind of<br />

computer language being processed.<br />

For programming languages it is taking a step <strong>to</strong>wards<br />

executable <strong>code</strong>.<br />

We will present some general techniques and abstractions for<br />

doing so.<br />

Note<br />

We will exemplify with minijava when possible. If minijava doesn’t<br />

have the feature we want <strong>to</strong> illustrate we will refer <strong>to</strong> some other<br />

programming language.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


The translation phase<br />

Lexical, syntactical and contextual analysis are phases of all<br />

language processors of all computer languages (which we have<br />

exemplified with minijava).<br />

The translation phase is more dependent on the kind of<br />

computer language being processed.<br />

For programming languages it is taking a step <strong>to</strong>wards<br />

executable <strong>code</strong>.<br />

We will present some general techniques and abstractions for<br />

doing so.<br />

Note<br />

We will exemplify with minijava when possible. If minijava doesn’t<br />

have the feature we want <strong>to</strong> illustrate we will refer <strong>to</strong> some other<br />

programming language.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


The translation phase<br />

Lexical, syntactical and contextual analysis are phases of all<br />

language processors of all computer languages (which we have<br />

exemplified with minijava).<br />

The translation phase is more dependent on the kind of<br />

computer language being processed.<br />

For programming languages it is taking a step <strong>to</strong>wards<br />

executable <strong>code</strong>.<br />

We will present some general techniques and abstractions for<br />

doing so.<br />

Note<br />

We will exemplify with minijava when possible. If minijava doesn’t<br />

have the feature we want <strong>to</strong> illustrate we will refer <strong>to</strong> some other<br />

programming language.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


The translation phase<br />

Lexical, syntactical and contextual analysis are phases of all<br />

language processors of all computer languages (which we have<br />

exemplified with minijava).<br />

The translation phase is more dependent on the kind of<br />

computer language being processed.<br />

For programming languages it is taking a step <strong>to</strong>wards<br />

executable <strong>code</strong>.<br />

We will present some general techniques and abstractions for<br />

doing so.<br />

Note<br />

We will exemplify with minijava when possible. If minijava doesn’t<br />

have the feature we want <strong>to</strong> illustrate we will refer <strong>to</strong> some other<br />

programming language.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


The translation phase<br />

We will present<br />

The abstract machine <strong>to</strong> be used.<br />

We present it as a language <strong>to</strong> write programs in (the<br />

instructions of the abstract machine).<br />

Because no person needs <strong>to</strong> program in it, we just present the<br />

abstract syntax!<br />

How <strong>to</strong> organize a translation <strong>to</strong> it.<br />

Some hints as <strong>to</strong> how <strong>to</strong> translate (some) the constructs of<br />

minijava <strong>to</strong> it.<br />

For the project<br />

you will get most of the material described here and will have <strong>to</strong><br />

complete the translation of the minijava constructs.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


The translation phase<br />

We will present<br />

The abstract machine <strong>to</strong> be used.<br />

We present it as a language <strong>to</strong> write programs in (the<br />

instructions of the abstract machine).<br />

Because no person needs <strong>to</strong> program in it, we just present the<br />

abstract syntax!<br />

How <strong>to</strong> organize a translation <strong>to</strong> it.<br />

Some hints as <strong>to</strong> how <strong>to</strong> translate (some) the constructs of<br />

minijava <strong>to</strong> it.<br />

For the project<br />

you will get most of the material described here and will have <strong>to</strong><br />

complete the translation of the minijava constructs.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


The translation phase<br />

We will present<br />

The abstract machine <strong>to</strong> be used.<br />

We present it as a language <strong>to</strong> write programs in (the<br />

instructions of the abstract machine).<br />

Because no person needs <strong>to</strong> program in it, we just present the<br />

abstract syntax!<br />

How <strong>to</strong> organize a translation <strong>to</strong> it.<br />

Some hints as <strong>to</strong> how <strong>to</strong> translate (some) the constructs of<br />

minijava <strong>to</strong> it.<br />

For the project<br />

you will get most of the material described here and will have <strong>to</strong><br />

complete the translation of the minijava constructs.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


The translation phase<br />

We will present<br />

The abstract machine <strong>to</strong> be used.<br />

We present it as a language <strong>to</strong> write programs in (the<br />

instructions of the abstract machine).<br />

Because no person needs <strong>to</strong> program in it, we just present the<br />

abstract syntax!<br />

How <strong>to</strong> organize a translation <strong>to</strong> it.<br />

Some hints as <strong>to</strong> how <strong>to</strong> translate (some) the constructs of<br />

minijava <strong>to</strong> it.<br />

For the project<br />

you will get most of the material described here and will have <strong>to</strong><br />

complete the translation of the minijava constructs.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


The translation phase<br />

We will present<br />

The abstract machine <strong>to</strong> be used.<br />

We present it as a language <strong>to</strong> write programs in (the<br />

instructions of the abstract machine).<br />

Because no person needs <strong>to</strong> program in it, we just present the<br />

abstract syntax!<br />

How <strong>to</strong> organize a translation <strong>to</strong> it.<br />

Some hints as <strong>to</strong> how <strong>to</strong> translate (some) the constructs of<br />

minijava <strong>to</strong> it.<br />

For the project<br />

you will get most of the material described here and will have <strong>to</strong><br />

complete the translation of the minijava constructs.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


The translation phase<br />

We will present<br />

The abstract machine <strong>to</strong> be used.<br />

We present it as a language <strong>to</strong> write programs in (the<br />

instructions of the abstract machine).<br />

Because no person needs <strong>to</strong> program in it, we just present the<br />

abstract syntax!<br />

How <strong>to</strong> organize a translation <strong>to</strong> it.<br />

Some hints as <strong>to</strong> how <strong>to</strong> translate (some) the constructs of<br />

minijava <strong>to</strong> it.<br />

For the project<br />

you will get most of the material described here and will have <strong>to</strong><br />

complete the translation of the minijava constructs.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


The translation phase<br />

We will present<br />

The abstract machine <strong>to</strong> be used.<br />

We present it as a language <strong>to</strong> write programs in (the<br />

instructions of the abstract machine).<br />

Because no person needs <strong>to</strong> program in it, we just present the<br />

abstract syntax!<br />

How <strong>to</strong> organize a translation <strong>to</strong> it.<br />

Some hints as <strong>to</strong> how <strong>to</strong> translate (some) the constructs of<br />

minijava <strong>to</strong> it.<br />

For the project<br />

you will get most of the material described here and will have <strong>to</strong><br />

complete the translation of the minijava constructs.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation<br />

The abstract syntax of the Abstract Machine language<br />

Must be convenient for the translation phase <strong>to</strong> produce.<br />

Must be convenient <strong>to</strong> translate in<strong>to</strong> machine language for the<br />

desired target machines.<br />

Each construct must have a clear and simple meaning so that<br />

optimizations that rewrite the <strong>intermediate</strong> representation can<br />

easily be specified and implemented.<br />

Notice<br />

Not always complicated pieces of abstract syntax correspond <strong>to</strong> the<br />

complex instructions that a target machine can execute!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation<br />

The abstract syntax of the Abstract Machine language<br />

Must be convenient for the translation phase <strong>to</strong> produce.<br />

Must be convenient <strong>to</strong> translate in<strong>to</strong> machine language for the<br />

desired target machines.<br />

Each construct must have a clear and simple meaning so that<br />

optimizations that rewrite the <strong>intermediate</strong> representation can<br />

easily be specified and implemented.<br />

Notice<br />

Not always complicated pieces of abstract syntax correspond <strong>to</strong> the<br />

complex instructions that a target machine can execute!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation<br />

The abstract syntax of the Abstract Machine language<br />

Must be convenient for the translation phase <strong>to</strong> produce.<br />

Must be convenient <strong>to</strong> translate in<strong>to</strong> machine language for the<br />

desired target machines.<br />

Each construct must have a clear and simple meaning so that<br />

optimizations that rewrite the <strong>intermediate</strong> representation can<br />

easily be specified and implemented.<br />

Notice<br />

Not always complicated pieces of abstract syntax correspond <strong>to</strong> the<br />

complex instructions that a target machine can execute!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation<br />

The abstract syntax of the Abstract Machine language<br />

Must be convenient for the translation phase <strong>to</strong> produce.<br />

Must be convenient <strong>to</strong> translate in<strong>to</strong> machine language for the<br />

desired target machines.<br />

Each construct must have a clear and simple meaning so that<br />

optimizations that rewrite the <strong>intermediate</strong> representation can<br />

easily be specified and implemented.<br />

Notice<br />

Not always complicated pieces of abstract syntax correspond <strong>to</strong> the<br />

complex instructions that a target machine can execute!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation<br />

The abstract syntax of the Abstract Machine language<br />

Must be convenient for the translation phase <strong>to</strong> produce.<br />

Must be convenient <strong>to</strong> translate in<strong>to</strong> machine language for the<br />

desired target machines.<br />

Each construct must have a clear and simple meaning so that<br />

optimizations that rewrite the <strong>intermediate</strong> representation can<br />

easily be specified and implemented.<br />

Notice<br />

Not always complicated pieces of abstract syntax correspond <strong>to</strong> the<br />

complex instructions that a target machine can execute!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

constants<br />

const(i)<br />

labels<br />

name(n)<br />

registers<br />

temp(t)<br />

operations<br />

binop(o,e1,e2)<br />

The integer constant (i)<br />

The symbolic constant n corresponding <strong>to</strong><br />

an assembly label<br />

Temporary t, similar <strong>to</strong> a register, there is<br />

an infinite number of these<br />

Binary opera<strong>to</strong>r. e1 is evaluated before e2.<br />

o can be one of<br />

plus,minus,mul,div,and,or,xor,<br />

lshift,rshift,arshift<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

memory<br />

mem(e)<br />

function call<br />

call(f,l)<br />

statement/value<br />

eseq(s,e)<br />

The content of wordSize bytes of memory<br />

starting at address e<br />

f is evaluated before l which is evaluated<br />

from left <strong>to</strong> right<br />

The statement s is evaluated for side<br />

effects, then e is evaluated for the result<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

memory<br />

mem(e)<br />

function call<br />

call(f,l)<br />

statement/value<br />

eseq(s,e)<br />

The content of wordSize bytes of memory<br />

starting at address e<br />

f is evaluated before l which is evaluated<br />

from left <strong>to</strong> right<br />

The statement s is evaluated for side<br />

effects, then e is evaluated for the result<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

memory<br />

mem(e)<br />

function call<br />

call(f,l)<br />

statement/value<br />

eseq(s,e)<br />

The content of wordSize bytes of memory<br />

starting at address e<br />

f is evaluated before l which is evaluated<br />

from left <strong>to</strong> right<br />

The statement s is evaluated for side<br />

effects, then e is evaluated for the result<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

memory<br />

mem(e)<br />

function call<br />

call(f,l)<br />

statement/value<br />

eseq(s,e)<br />

The content of wordSize bytes of memory<br />

starting at address e<br />

f is evaluated before l which is evaluated<br />

from left <strong>to</strong> right<br />

The statement s is evaluated for side<br />

effects, then e is evaluated for the result<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

memory<br />

mem(e)<br />

function call<br />

call(f,l)<br />

statement/value<br />

eseq(s,e)<br />

The content of wordSize bytes of memory<br />

starting at address e<br />

f is evaluated before l which is evaluated<br />

from left <strong>to</strong> right<br />

The statement s is evaluated for side<br />

effects, then e is evaluated for the result<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

memory<br />

mem(e)<br />

function call<br />

call(f,l)<br />

statement/value<br />

eseq(s,e)<br />

The content of wordSize bytes of memory<br />

starting at address e<br />

f is evaluated before l which is evaluated<br />

from left <strong>to</strong> right<br />

The statement s is evaluated for side<br />

effects, then e is evaluated for the result<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

memory<br />

mem(e)<br />

function call<br />

call(f,l)<br />

statement/value<br />

eseq(s,e)<br />

The content of wordSize bytes of memory<br />

starting at address e<br />

f is evaluated before l which is evaluated<br />

from left <strong>to</strong> right<br />

The statement s is evaluated for side<br />

effects, then e is evaluated for the result<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

memory<br />

mem(e)<br />

function call<br />

call(f,l)<br />

statement/value<br />

eseq(s,e)<br />

The content of wordSize bytes of memory<br />

starting at address e<br />

f is evaluated before l which is evaluated<br />

from left <strong>to</strong> right<br />

The statement s is evaluated for side<br />

effects, then e is evaluated for the result<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

memory<br />

mem(e)<br />

function call<br />

call(f,l)<br />

statement/value<br />

eseq(s,e)<br />

The content of wordSize bytes of memory<br />

starting at address e<br />

f is evaluated before l which is evaluated<br />

from left <strong>to</strong> right<br />

The statement s is evaluated for side<br />

effects, then e is evaluated for the result<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

memory<br />

mem(e)<br />

function call<br />

call(f,l)<br />

statement/value<br />

eseq(s,e)<br />

The content of wordSize bytes of memory<br />

starting at address e<br />

f is evaluated before l which is evaluated<br />

from left <strong>to</strong> right<br />

The statement s is evaluated for side<br />

effects, then e is evaluated for the result<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

memory<br />

mem(e)<br />

function call<br />

call(f,l)<br />

statement/value<br />

eseq(s,e)<br />

The content of wordSize bytes of memory<br />

starting at address e<br />

f is evaluated before l which is evaluated<br />

from left <strong>to</strong> right<br />

The statement s is evaluated for side<br />

effects, then e is evaluated for the result<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

memory<br />

mem(e)<br />

function call<br />

call(f,l)<br />

statement/value<br />

eseq(s,e)<br />

The content of wordSize bytes of memory<br />

starting at address e<br />

f is evaluated before l which is evaluated<br />

from left <strong>to</strong> right<br />

The statement s is evaluated for side<br />

effects, then e is evaluated for the result<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

memory<br />

mem(e)<br />

function call<br />

call(f,l)<br />

statement/value<br />

eseq(s,e)<br />

The content of wordSize bytes of memory<br />

starting at address e<br />

f is evaluated before l which is evaluated<br />

from left <strong>to</strong> right<br />

The statement s is evaluated for side<br />

effects, then e is evaluated for the result<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

memory<br />

mem(e)<br />

function call<br />

call(f,l)<br />

statement/value<br />

eseq(s,e)<br />

The content of wordSize bytes of memory<br />

starting at address e<br />

f is evaluated before l which is evaluated<br />

from left <strong>to</strong> right<br />

The statement s is evaluated for side<br />

effects, then e is evaluated for the result<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Expressions<br />

memory<br />

mem(e)<br />

function call<br />

call(f,l)<br />

statement/value<br />

eseq(s,e)<br />

The content of wordSize bytes of memory<br />

starting at address e<br />

f is evaluated before l which is evaluated<br />

from left <strong>to</strong> right<br />

The statement s is evaluated for side<br />

effects, then e is evaluated for the result<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

move<br />

move(temp(t),e)<br />

move<br />

move(mem(e1),e2)<br />

discard<br />

exp(e)<br />

labeling<br />

label(n)<br />

Evaluate e and move the value <strong>to</strong> t<br />

Evaluate e1 <strong>to</strong> memory address a, then<br />

evaluate e2 and s<strong>to</strong>re its value in wordSize<br />

bytes after a<br />

Evaluate e and discard the result<br />

mark the current machine <strong>code</strong> address<br />

with name n<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

jump<br />

jump(e,labs)<br />

conditional jump<br />

cjump(o,e1,e2,t,f)<br />

sequence<br />

seq(s1,s2)<br />

Transfer control <strong>to</strong> address e. labs specifies<br />

all the locations e can evaluate <strong>to</strong> (needed<br />

later in the compiler)<br />

Evaluate e1 then e2 and then apply the<br />

binop o. If true jump <strong>to</strong> t else jump <strong>to</strong> f<br />

s1 followed by s2<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

jump<br />

jump(e,labs)<br />

conditional jump<br />

cjump(o,e1,e2,t,f)<br />

sequence<br />

seq(s1,s2)<br />

Transfer control <strong>to</strong> address e. labs specifies<br />

all the locations e can evaluate <strong>to</strong> (needed<br />

later in the compiler)<br />

Evaluate e1 then e2 and then apply the<br />

binop o. If true jump <strong>to</strong> t else jump <strong>to</strong> f<br />

s1 followed by s2<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

jump<br />

jump(e,labs)<br />

conditional jump<br />

cjump(o,e1,e2,t,f)<br />

sequence<br />

seq(s1,s2)<br />

Transfer control <strong>to</strong> address e. labs specifies<br />

all the locations e can evaluate <strong>to</strong> (needed<br />

later in the compiler)<br />

Evaluate e1 then e2 and then apply the<br />

binop o. If true jump <strong>to</strong> t else jump <strong>to</strong> f<br />

s1 followed by s2<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

jump<br />

jump(e,labs)<br />

conditional jump<br />

cjump(o,e1,e2,t,f)<br />

sequence<br />

seq(s1,s2)<br />

Transfer control <strong>to</strong> address e. labs specifies<br />

all the locations e can evaluate <strong>to</strong> (needed<br />

later in the compiler)<br />

Evaluate e1 then e2 and then apply the<br />

binop o. If true jump <strong>to</strong> t else jump <strong>to</strong> f<br />

s1 followed by s2<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

jump<br />

jump(e,labs)<br />

conditional jump<br />

cjump(o,e1,e2,t,f)<br />

sequence<br />

seq(s1,s2)<br />

Transfer control <strong>to</strong> address e. labs specifies<br />

all the locations e can evaluate <strong>to</strong> (needed<br />

later in the compiler)<br />

Evaluate e1 then e2 and then apply the<br />

binop o. If true jump <strong>to</strong> t else jump <strong>to</strong> f<br />

s1 followed by s2<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

jump<br />

jump(e,labs)<br />

conditional jump<br />

cjump(o,e1,e2,t,f)<br />

sequence<br />

seq(s1,s2)<br />

Transfer control <strong>to</strong> address e. labs specifies<br />

all the locations e can evaluate <strong>to</strong> (needed<br />

later in the compiler)<br />

Evaluate e1 then e2 and then apply the<br />

binop o. If true jump <strong>to</strong> t else jump <strong>to</strong> f<br />

s1 followed by s2<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

jump<br />

jump(e,labs)<br />

conditional jump<br />

cjump(o,e1,e2,t,f)<br />

sequence<br />

seq(s1,s2)<br />

Transfer control <strong>to</strong> address e. labs specifies<br />

all the locations e can evaluate <strong>to</strong> (needed<br />

later in the compiler)<br />

Evaluate e1 then e2 and then apply the<br />

binop o. If true jump <strong>to</strong> t else jump <strong>to</strong> f<br />

s1 followed by s2<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

jump<br />

jump(e,labs)<br />

conditional jump<br />

cjump(o,e1,e2,t,f)<br />

sequence<br />

seq(s1,s2)<br />

Transfer control <strong>to</strong> address e. labs specifies<br />

all the locations e can evaluate <strong>to</strong> (needed<br />

later in the compiler)<br />

Evaluate e1 then e2 and then apply the<br />

binop o. If true jump <strong>to</strong> t else jump <strong>to</strong> f<br />

s1 followed by s2<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

jump<br />

jump(e,labs)<br />

conditional jump<br />

cjump(o,e1,e2,t,f)<br />

sequence<br />

seq(s1,s2)<br />

Transfer control <strong>to</strong> address e. labs specifies<br />

all the locations e can evaluate <strong>to</strong> (needed<br />

later in the compiler)<br />

Evaluate e1 then e2 and then apply the<br />

binop o. If true jump <strong>to</strong> t else jump <strong>to</strong> f<br />

s1 followed by s2<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

jump<br />

jump(e,labs)<br />

conditional jump<br />

cjump(o,e1,e2,t,f)<br />

sequence<br />

seq(s1,s2)<br />

Transfer control <strong>to</strong> address e. labs specifies<br />

all the locations e can evaluate <strong>to</strong> (needed<br />

later in the compiler)<br />

Evaluate e1 then e2 and then apply the<br />

binop o. If true jump <strong>to</strong> t else jump <strong>to</strong> f<br />

s1 followed by s2<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

jump<br />

jump(e,labs)<br />

conditional jump<br />

cjump(o,e1,e2,t,f)<br />

sequence<br />

seq(s1,s2)<br />

Transfer control <strong>to</strong> address e. labs specifies<br />

all the locations e can evaluate <strong>to</strong> (needed<br />

later in the compiler)<br />

Evaluate e1 then e2 and then apply the<br />

binop o. If true jump <strong>to</strong> t else jump <strong>to</strong> f<br />

s1 followed by s2<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

jump<br />

jump(e,labs)<br />

conditional jump<br />

cjump(o,e1,e2,t,f)<br />

sequence<br />

seq(s1,s2)<br />

Transfer control <strong>to</strong> address e. labs specifies<br />

all the locations e can evaluate <strong>to</strong> (needed<br />

later in the compiler)<br />

Evaluate e1 then e2 and then apply the<br />

binop o. If true jump <strong>to</strong> t else jump <strong>to</strong> f<br />

s1 followed by s2<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

jump<br />

jump(e,labs)<br />

conditional jump<br />

cjump(o,e1,e2,t,f)<br />

sequence<br />

seq(s1,s2)<br />

Transfer control <strong>to</strong> address e. labs specifies<br />

all the locations e can evaluate <strong>to</strong> (needed<br />

later in the compiler)<br />

Evaluate e1 then e2 and then apply the<br />

binop o. If true jump <strong>to</strong> t else jump <strong>to</strong> f<br />

s1 followed by s2<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

jump<br />

jump(e,labs)<br />

conditional jump<br />

cjump(o,e1,e2,t,f)<br />

sequence<br />

seq(s1,s2)<br />

Transfer control <strong>to</strong> address e. labs specifies<br />

all the locations e can evaluate <strong>to</strong> (needed<br />

later in the compiler)<br />

Evaluate e1 then e2 and then apply the<br />

binop o. If true jump <strong>to</strong> t else jump <strong>to</strong> f<br />

s1 followed by s2<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Intermediate Representation - Statements<br />

jump<br />

jump(e,labs)<br />

conditional jump<br />

cjump(o,e1,e2,t,f)<br />

sequence<br />

seq(s1,s2)<br />

Transfer control <strong>to</strong> address e. labs specifies<br />

all the locations e can evaluate <strong>to</strong> (needed<br />

later in the compiler)<br />

Evaluate e1 then e2 and then apply the<br />

binop o. If true jump <strong>to</strong> t else jump <strong>to</strong> f<br />

s1 followed by s2<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> - remarks<br />

We will define a visi<strong>to</strong>r class that builds the translation of a<br />

(minijava) program <strong>to</strong> its representation in <strong>intermediate</strong><br />

representation.<br />

Because a program consists of some declarations (in minijava<br />

mainclass classdeclarations) the output will be a collection of<br />

processed declarations.<br />

Because there are no function or class declarations in the<br />

<strong>intermediate</strong> representation, the output will consist of a list of<br />

fragments.<br />

Fragment<br />

the translation of the function’s <strong>code</strong> (a statement) including<br />

placing the returned value in a dedicated register.<br />

frame structure describing the memory needed for a function call.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> - remarks<br />

We will define a visi<strong>to</strong>r class that builds the translation of a<br />

(minijava) program <strong>to</strong> its representation in <strong>intermediate</strong><br />

representation.<br />

Because a program consists of some declarations (in minijava<br />

mainclass classdeclarations) the output will be a collection of<br />

processed declarations.<br />

Because there are no function or class declarations in the<br />

<strong>intermediate</strong> representation, the output will consist of a list of<br />

fragments.<br />

Fragment<br />

the translation of the function’s <strong>code</strong> (a statement) including<br />

placing the returned value in a dedicated register.<br />

frame structure describing the memory needed for a function call.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> - remarks<br />

We will define a visi<strong>to</strong>r class that builds the translation of a<br />

(minijava) program <strong>to</strong> its representation in <strong>intermediate</strong><br />

representation.<br />

Because a program consists of some declarations (in minijava<br />

mainclass classdeclarations) the output will be a collection of<br />

processed declarations.<br />

Because there are no function or class declarations in the<br />

<strong>intermediate</strong> representation, the output will consist of a list of<br />

fragments.<br />

Fragment<br />

the translation of the function’s <strong>code</strong> (a statement) including<br />

placing the returned value in a dedicated register.<br />

frame structure describing the memory needed for a function call.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> - remarks<br />

We will define a visi<strong>to</strong>r class that builds the translation of a<br />

(minijava) program <strong>to</strong> its representation in <strong>intermediate</strong><br />

representation.<br />

Because a program consists of some declarations (in minijava<br />

mainclass classdeclarations) the output will be a collection of<br />

processed declarations.<br />

Because there are no function or class declarations in the<br />

<strong>intermediate</strong> representation, the output will consist of a list of<br />

fragments.<br />

Fragment<br />

the translation of the function’s <strong>code</strong> (a statement) including<br />

placing the returned value in a dedicated register.<br />

frame structure describing the memory needed for a function call.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> - remarks<br />

We will define a visi<strong>to</strong>r class that builds the translation of a<br />

(minijava) program <strong>to</strong> its representation in <strong>intermediate</strong><br />

representation.<br />

Because a program consists of some declarations (in minijava<br />

mainclass classdeclarations) the output will be a collection of<br />

processed declarations.<br />

Because there are no function or class declarations in the<br />

<strong>intermediate</strong> representation, the output will consist of a list of<br />

fragments.<br />

Fragment<br />

the translation of the function’s <strong>code</strong> (a statement) including<br />

placing the returned value in a dedicated register.<br />

frame structure describing the memory needed for a function call.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> - remarks<br />

We will define a visi<strong>to</strong>r class that builds the translation of a<br />

(minijava) program <strong>to</strong> its representation in <strong>intermediate</strong><br />

representation.<br />

Because a program consists of some declarations (in minijava<br />

mainclass classdeclarations) the output will be a collection of<br />

processed declarations.<br />

Because there are no function or class declarations in the<br />

<strong>intermediate</strong> representation, the output will consist of a list of<br />

fragments.<br />

Fragment<br />

the translation of the function’s <strong>code</strong> (a statement) including<br />

placing the returned value in a dedicated register.<br />

frame structure describing the memory needed for a function call.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Typical frame<br />

argument n<br />

.<br />

argument 1<br />

static link<br />

local variables<br />

return address<br />

saved registers<br />

argument m<br />

.<br />

argument 1<br />

static link<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Managing Memory<br />

Runtime logical address space<br />

<strong>code</strong> static heap −→ Free memory ←− stack<br />

Code and some categories of data occupy a fixed space.<br />

Data areas for instances of classes (heap) and for frames<br />

(stack) change during execution!<br />

Garbage collec<strong>to</strong>rs work on the heap freeing unreferenced<br />

objects!<br />

The Operating System is likely <strong>to</strong> spread these through<br />

physical addresses, but this is not under control of the<br />

compiler!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Managing Memory<br />

Runtime logical address space<br />

<strong>code</strong> static heap −→ Free memory ←− stack<br />

Code and some categories of data occupy a fixed space.<br />

Data areas for instances of classes (heap) and for frames<br />

(stack) change during execution!<br />

Garbage collec<strong>to</strong>rs work on the heap freeing unreferenced<br />

objects!<br />

The Operating System is likely <strong>to</strong> spread these through<br />

physical addresses, but this is not under control of the<br />

compiler!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Managing Memory<br />

Runtime logical address space<br />

<strong>code</strong> static heap −→ Free memory ←− stack<br />

Code and some categories of data occupy a fixed space.<br />

Data areas for instances of classes (heap) and for frames<br />

(stack) change during execution!<br />

Garbage collec<strong>to</strong>rs work on the heap freeing unreferenced<br />

objects!<br />

The Operating System is likely <strong>to</strong> spread these through<br />

physical addresses, but this is not under control of the<br />

compiler!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Managing Memory<br />

Runtime logical address space<br />

<strong>code</strong> static heap −→ Free memory ←− stack<br />

Code and some categories of data occupy a fixed space.<br />

Data areas for instances of classes (heap) and for frames<br />

(stack) change during execution!<br />

Garbage collec<strong>to</strong>rs work on the heap freeing unreferenced<br />

objects!<br />

The Operating System is likely <strong>to</strong> spread these through<br />

physical addresses, but this is not under control of the<br />

compiler!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Managing Memory<br />

Runtime logical address space<br />

<strong>code</strong> static heap −→ Free memory ←− stack<br />

Code and some categories of data occupy a fixed space.<br />

Data areas for instances of classes (heap) and for frames<br />

(stack) change during execution!<br />

Garbage collec<strong>to</strong>rs work on the heap freeing unreferenced<br />

objects!<br />

The Operating System is likely <strong>to</strong> spread these through<br />

physical addresses, but this is not under control of the<br />

compiler!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> -remarks<br />

In minijava, in java, in C and in many other languages, the role of<br />

an expression or statement depends on the context in which it is<br />

used:<br />

a


Organizing the <strong>Translation</strong> -remarks<br />

In minijava, in java, in C and in many other languages, the role of<br />

an expression or statement depends on the context in which it is<br />

used:<br />

a


Organizing the <strong>Translation</strong> -remarks<br />

In minijava, in java, in C and in many other languages, the role of<br />

an expression or statement depends on the context in which it is<br />

used:<br />

a


Organizing the <strong>Translation</strong> -remarks<br />

In minijava, in java, in C and in many other languages, the role of<br />

an expression or statement depends on the context in which it is<br />

used:<br />

a


Organizing the <strong>Translation</strong> -remarks<br />

In minijava, in java, in C and in many other languages, the role of<br />

an expression or statement depends on the context in which it is<br />

used:<br />

a


Organizing the <strong>Translation</strong> -remarks<br />

In minijava, in java, in C and in many other languages, the role of<br />

an expression or statement depends on the context in which it is<br />

used:<br />

a


Organizing the <strong>Translation</strong> -remarks<br />

In minijava, in java, in C and in many other languages, the role of<br />

an expression or statement depends on the context in which it is<br />

used:<br />

a


Organizing the <strong>Translation</strong> -remarks<br />

Implementation question<br />

How do we translate abstract syntax <strong>to</strong> <strong>intermediate</strong> representation<br />

so that we can use context information?<br />

Exp<br />

Implement classes for the <strong>intermediate</strong> representation<br />

Tree.Exp and Tree.Stm<br />

But, translate <strong>to</strong> something that can be manipulated when<br />

the context is known!<br />

public abstract class Exp {<br />

abstract Tree.Exp unEx();<br />

abstract Tree.Stm unNx();<br />

abstract Tree.Stm unCx(Temp.Label t, Temp.Label f);<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> -remarks<br />

Implementation question<br />

How do we translate abstract syntax <strong>to</strong> <strong>intermediate</strong> representation<br />

so that we can use context information?<br />

Exp<br />

Implement classes for the <strong>intermediate</strong> representation<br />

Tree.Exp and Tree.Stm<br />

But, translate <strong>to</strong> something that can be manipulated when<br />

the context is known!<br />

public abstract class Exp {<br />

abstract Tree.Exp unEx();<br />

abstract Tree.Stm unNx();<br />

abstract Tree.Stm unCx(Temp.Label t, Temp.Label f);<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> -remarks<br />

Implementation question<br />

How do we translate abstract syntax <strong>to</strong> <strong>intermediate</strong> representation<br />

so that we can use context information?<br />

Exp<br />

Implement classes for the <strong>intermediate</strong> representation<br />

Tree.Exp and Tree.Stm<br />

But, translate <strong>to</strong> something that can be manipulated when<br />

the context is known!<br />

public abstract class Exp {<br />

abstract Tree.Exp unEx();<br />

abstract Tree.Stm unNx();<br />

abstract Tree.Stm unCx(Temp.Label t, Temp.Label f);<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> -remarks<br />

Implementation question<br />

How do we translate abstract syntax <strong>to</strong> <strong>intermediate</strong> representation<br />

so that we can use context information?<br />

Exp<br />

Implement classes for the <strong>intermediate</strong> representation<br />

Tree.Exp and Tree.Stm<br />

But, translate <strong>to</strong> something that can be manipulated when<br />

the context is known!<br />

public abstract class Exp {<br />

abstract Tree.Exp unEx();<br />

abstract Tree.Stm unNx();<br />

abstract Tree.Stm unCx(Temp.Label t, Temp.Label f);<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> -remarks<br />

Implementation question<br />

How do we translate abstract syntax <strong>to</strong> <strong>intermediate</strong> representation<br />

so that we can use context information?<br />

Exp<br />

Implement classes for the <strong>intermediate</strong> representation<br />

Tree.Exp and Tree.Stm<br />

But, translate <strong>to</strong> something that can be manipulated when<br />

the context is known!<br />

public abstract class Exp {<br />

abstract Tree.Exp unEx();<br />

abstract Tree.Stm unNx();<br />

abstract Tree.Stm unCx(Temp.Label t, Temp.Label f);<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> - example<br />

To translate expressions of minijava<br />

public class Ex extends Exp {<br />

Tree.Exp exp;<br />

Ex(Tree.Exp e) { exp = e; }<br />

Tree.Exp unEx(){ return exp; }<br />

Tree.Stm unNx(){ return new Tree.EXP(exp); }<br />

Tree.Stm unCx(Label t, Label f) {<br />

if (exp instanceof Tree.CONST) {<br />

Tree.CONST c = (Tree.CONST)exp;<br />

if (c.value == 0)<br />

return new Tree.JUMP(f);<br />

else<br />

return new Tree.JUMP(t);<br />

}<br />

return new Tree.CJUMP(Tree.CJUMP.NE, exp,<br />

new Tree.CONST(0), t, f);<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> - example<br />

To translate expressions of minijava<br />

public class Ex extends Exp {<br />

Tree.Exp exp;<br />

Ex(Tree.Exp e) { exp = e; }<br />

Tree.Exp unEx(){ return exp; }<br />

Tree.Stm unNx(){ return new Tree.EXP(exp); }<br />

Tree.Stm unCx(Label t, Label f) {<br />

if (exp instanceof Tree.CONST) {<br />

Tree.CONST c = (Tree.CONST)exp;<br />

if (c.value == 0)<br />

return new Tree.JUMP(f);<br />

else<br />

return new Tree.JUMP(t);<br />

}<br />

return new Tree.CJUMP(Tree.CJUMP.NE, exp,<br />

new Tree.CONST(0), t, f);<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> - example<br />

To translate expressions of minijava<br />

public class Ex extends Exp {<br />

Tree.Exp exp;<br />

Ex(Tree.Exp e) { exp = e; }<br />

Tree.Exp unEx(){ return exp; }<br />

Tree.Stm unNx(){ return new Tree.EXP(exp); }<br />

Tree.Stm unCx(Label t, Label f) {<br />

if (exp instanceof Tree.CONST) {<br />

Tree.CONST c = (Tree.CONST)exp;<br />

if (c.value == 0)<br />

return new Tree.JUMP(f);<br />

else<br />

return new Tree.JUMP(t);<br />

}<br />

return new Tree.CJUMP(Tree.CJUMP.NE, exp,<br />

new Tree.CONST(0), t, f);<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> - example<br />

To translate expressions of minijava<br />

public class Ex extends Exp {<br />

Tree.Exp exp;<br />

Ex(Tree.Exp e) { exp = e; }<br />

Tree.Exp unEx(){ return exp; }<br />

Tree.Stm unNx(){ return new Tree.EXP(exp); }<br />

Tree.Stm unCx(Label t, Label f) {<br />

if (exp instanceof Tree.CONST) {<br />

Tree.CONST c = (Tree.CONST)exp;<br />

if (c.value == 0)<br />

return new Tree.JUMP(f);<br />

else<br />

return new Tree.JUMP(t);<br />

}<br />

return new Tree.CJUMP(Tree.CJUMP.NE, exp,<br />

new Tree.CONST(0), t, f);<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> - example<br />

To translate expressions of minijava<br />

public class Ex extends Exp {<br />

Tree.Exp exp;<br />

Ex(Tree.Exp e) { exp = e; }<br />

Tree.Exp unEx(){ return exp; }<br />

Tree.Stm unNx(){ return new Tree.EXP(exp); }<br />

Tree.Stm unCx(Label t, Label f) {<br />

if (exp instanceof Tree.CONST) {<br />

Tree.CONST c = (Tree.CONST)exp;<br />

if (c.value == 0)<br />

return new Tree.JUMP(f);<br />

else<br />

return new Tree.JUMP(t);<br />

}<br />

return new Tree.CJUMP(Tree.CJUMP.NE, exp,<br />

new Tree.CONST(0), t, f);<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> - example<br />

To translate expressions of minijava<br />

public class Ex extends Exp {<br />

Tree.Exp exp;<br />

Ex(Tree.Exp e) { exp = e; }<br />

Tree.Exp unEx(){ return exp; }<br />

Tree.Stm unNx(){ return new Tree.EXP(exp); }<br />

Tree.Stm unCx(Label t, Label f) {<br />

if (exp instanceof Tree.CONST) {<br />

Tree.CONST c = (Tree.CONST)exp;<br />

if (c.value == 0)<br />

return new Tree.JUMP(f);<br />

else<br />

return new Tree.JUMP(t);<br />

}<br />

return new Tree.CJUMP(Tree.CJUMP.NE, exp,<br />

new Tree.CONST(0), t, f);<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Organizing the <strong>Translation</strong> - example<br />

To translate expressions of minijava<br />

public class Ex extends Exp {<br />

Tree.Exp exp;<br />

Ex(Tree.Exp e) { exp = e; }<br />

Tree.Exp unEx(){ return exp; }<br />

Tree.Stm unNx(){ return new Tree.EXP(exp); }<br />

Tree.Stm unCx(Label t, Label f) {<br />

if (exp instanceof Tree.CONST) {<br />

Tree.CONST c = (Tree.CONST)exp;<br />

if (c.value == 0)<br />

return new Tree.JUMP(f);<br />

else<br />

return new Tree.JUMP(t);<br />

}<br />

return new Tree.CJUMP(Tree.CJUMP.NE, exp,<br />

new Tree.CONST(0), t, f);<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- Variables<br />

Variables are always in some scope (in our <strong>code</strong> implemented<br />

by a class Level )<br />

Variables in the frame with offset k will be translated as<br />

mem(binop(plus,temp(fp),const(k)))<br />

Variables in a register tnumber will be translated as<br />

temp(tnumber )<br />

Machine dependent features like the frame pointer and the<br />

word size are provided by Frame :<br />

public abstract class Frame {<br />

public abstract Temp.Temp FP();<br />

public abstract int wordSize();<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- Variables<br />

Variables are always in some scope (in our <strong>code</strong> implemented<br />

by a class Level )<br />

Variables in the frame with offset k will be translated as<br />

mem(binop(plus,temp(fp),const(k)))<br />

Variables in a register tnumber will be translated as<br />

temp(tnumber )<br />

Machine dependent features like the frame pointer and the<br />

word size are provided by Frame :<br />

public abstract class Frame {<br />

public abstract Temp.Temp FP();<br />

public abstract int wordSize();<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- Variables<br />

Variables are always in some scope (in our <strong>code</strong> implemented<br />

by a class Level )<br />

Variables in the frame with offset k will be translated as<br />

mem(binop(plus,temp(fp),const(k)))<br />

Variables in a register tnumber will be translated as<br />

temp(tnumber )<br />

Machine dependent features like the frame pointer and the<br />

word size are provided by Frame :<br />

public abstract class Frame {<br />

public abstract Temp.Temp FP();<br />

public abstract int wordSize();<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- Variables<br />

Variables are always in some scope (in our <strong>code</strong> implemented<br />

by a class Level )<br />

Variables in the frame with offset k will be translated as<br />

mem(binop(plus,temp(fp),const(k)))<br />

Variables in a register tnumber will be translated as<br />

temp(tnumber )<br />

Machine dependent features like the frame pointer and the<br />

word size are provided by Frame :<br />

public abstract class Frame {<br />

public abstract Temp.Temp FP();<br />

public abstract int wordSize();<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- Variables<br />

Variables are always in some scope (in our <strong>code</strong> implemented<br />

by a class Level )<br />

Variables in the frame with offset k will be translated as<br />

mem(binop(plus,temp(fp),const(k)))<br />

Variables in a register tnumber will be translated as<br />

temp(tnumber )<br />

Machine dependent features like the frame pointer and the<br />

word size are provided by Frame :<br />

public abstract class Frame {<br />

public abstract Temp.Temp FP();<br />

public abstract int wordSize();<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- Variables<br />

Variables are always in some scope (in our <strong>code</strong> implemented<br />

by a class Level )<br />

Variables in the frame with offset k will be translated as<br />

mem(binop(plus,temp(fp),const(k)))<br />

Variables in a register tnumber will be translated as<br />

temp(tnumber )<br />

Machine dependent features like the frame pointer and the<br />

word size are provided by Frame :<br />

public abstract class Frame {<br />

public abstract Temp.Temp FP();<br />

public abstract int wordSize();<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- Variables<br />

In the frame or in registers?<br />

Elaborating a variable declaration binds the variable <strong>to</strong> an<br />

Access (an abstract class!).<br />

When translating a use of the variable how do we know<br />

whether it has been assigned <strong>to</strong> a register or <strong>to</strong> the frame?<br />

We don’t know!<br />

We let the different implementations of Access <strong>to</strong> resolve<br />

this:<br />

public abstract class Access {<br />

public abstract Tree.Exp exp(Tree.Exp e);<br />

}<br />

We look up the variable in the symbol table and get an<br />

Access a .<br />

We do a.exp(new Tree.TEMP(frame.FP()));<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- Variables<br />

In the frame or in registers?<br />

Elaborating a variable declaration binds the variable <strong>to</strong> an<br />

Access (an abstract class!).<br />

When translating a use of the variable how do we know<br />

whether it has been assigned <strong>to</strong> a register or <strong>to</strong> the frame?<br />

We don’t know!<br />

We let the different implementations of Access <strong>to</strong> resolve<br />

this:<br />

public abstract class Access {<br />

public abstract Tree.Exp exp(Tree.Exp e);<br />

}<br />

We look up the variable in the symbol table and get an<br />

Access a .<br />

We do a.exp(new Tree.TEMP(frame.FP()));<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- Variables<br />

In the frame or in registers?<br />

Elaborating a variable declaration binds the variable <strong>to</strong> an<br />

Access (an abstract class!).<br />

When translating a use of the variable how do we know<br />

whether it has been assigned <strong>to</strong> a register or <strong>to</strong> the frame?<br />

We don’t know!<br />

We let the different implementations of Access <strong>to</strong> resolve<br />

this:<br />

public abstract class Access {<br />

public abstract Tree.Exp exp(Tree.Exp e);<br />

}<br />

We look up the variable in the symbol table and get an<br />

Access a .<br />

We do a.exp(new Tree.TEMP(frame.FP()));<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- Variables<br />

In the frame or in registers?<br />

Elaborating a variable declaration binds the variable <strong>to</strong> an<br />

Access (an abstract class!).<br />

When translating a use of the variable how do we know<br />

whether it has been assigned <strong>to</strong> a register or <strong>to</strong> the frame?<br />

We don’t know!<br />

We let the different implementations of Access <strong>to</strong> resolve<br />

this:<br />

public abstract class Access {<br />

public abstract Tree.Exp exp(Tree.Exp e);<br />

}<br />

We look up the variable in the symbol table and get an<br />

Access a .<br />

We do a.exp(new Tree.TEMP(frame.FP()));<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- Variables<br />

In the frame or in registers?<br />

Elaborating a variable declaration binds the variable <strong>to</strong> an<br />

Access (an abstract class!).<br />

When translating a use of the variable how do we know<br />

whether it has been assigned <strong>to</strong> a register or <strong>to</strong> the frame?<br />

We don’t know!<br />

We let the different implementations of Access <strong>to</strong> resolve<br />

this:<br />

public abstract class Access {<br />

public abstract Tree.Exp exp(Tree.Exp e);<br />

}<br />

We look up the variable in the symbol table and get an<br />

Access a .<br />

We do a.exp(new Tree.TEMP(frame.FP()));<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- Variables<br />

In the frame or in registers?<br />

Elaborating a variable declaration binds the variable <strong>to</strong> an<br />

Access (an abstract class!).<br />

When translating a use of the variable how do we know<br />

whether it has been assigned <strong>to</strong> a register or <strong>to</strong> the frame?<br />

We don’t know!<br />

We let the different implementations of Access <strong>to</strong> resolve<br />

this:<br />

public abstract class Access {<br />

public abstract Tree.Exp exp(Tree.Exp e);<br />

}<br />

We look up the variable in the symbol table and get an<br />

Access a .<br />

We do a.exp(new Tree.TEMP(frame.FP()));<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- Variables<br />

In the frame or in registers?<br />

Elaborating a variable declaration binds the variable <strong>to</strong> an<br />

Access (an abstract class!).<br />

When translating a use of the variable how do we know<br />

whether it has been assigned <strong>to</strong> a register or <strong>to</strong> the frame?<br />

We don’t know!<br />

We let the different implementations of Access <strong>to</strong> resolve<br />

this:<br />

public abstract class Access {<br />

public abstract Tree.Exp exp(Tree.Exp e);<br />

}<br />

We look up the variable in the symbol table and get an<br />

Access a .<br />

We do a.exp(new Tree.TEMP(frame.FP()));<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- structured variables<br />

Different programming languages have different policies!<br />

In Pascal an array variable stands for the content of the array.<br />

assignment will copy content<br />

place for the content has <strong>to</strong> be associated <strong>to</strong> an array variable!<br />

In minijava array variables (and object variables) behave like<br />

pointers<br />

array assignment is pointer assignment<br />

only place for a pointer is associated <strong>to</strong> an array or object<br />

variable.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- structured variables<br />

Different programming languages have different policies!<br />

In Pascal an array variable stands for the content of the array.<br />

assignment will copy content<br />

place for the content has <strong>to</strong> be associated <strong>to</strong> an array variable!<br />

In minijava array variables (and object variables) behave like<br />

pointers<br />

array assignment is pointer assignment<br />

only place for a pointer is associated <strong>to</strong> an array or object<br />

variable.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- structured variables<br />

Different programming languages have different policies!<br />

In Pascal an array variable stands for the content of the array.<br />

assignment will copy content<br />

place for the content has <strong>to</strong> be associated <strong>to</strong> an array variable!<br />

In minijava array variables (and object variables) behave like<br />

pointers<br />

array assignment is pointer assignment<br />

only place for a pointer is associated <strong>to</strong> an array or object<br />

variable.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- structured variables<br />

Different programming languages have different policies!<br />

In Pascal an array variable stands for the content of the array.<br />

assignment will copy content<br />

place for the content has <strong>to</strong> be associated <strong>to</strong> an array variable!<br />

In minijava array variables (and object variables) behave like<br />

pointers<br />

array assignment is pointer assignment<br />

only place for a pointer is associated <strong>to</strong> an array or object<br />

variable.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- structured variables<br />

Different programming languages have different policies!<br />

In Pascal an array variable stands for the content of the array.<br />

assignment will copy content<br />

place for the content has <strong>to</strong> be associated <strong>to</strong> an array variable!<br />

In minijava array variables (and object variables) behave like<br />

pointers<br />

array assignment is pointer assignment<br />

only place for a pointer is associated <strong>to</strong> an array or object<br />

variable.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- structured variables<br />

Different programming languages have different policies!<br />

In Pascal an array variable stands for the content of the array.<br />

assignment will copy content<br />

place for the content has <strong>to</strong> be associated <strong>to</strong> an array variable!<br />

In minijava array variables (and object variables) behave like<br />

pointers<br />

array assignment is pointer assignment<br />

only place for a pointer is associated <strong>to</strong> an array or object<br />

variable.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- structured variables<br />

Different programming languages have different policies!<br />

In Pascal an array variable stands for the content of the array.<br />

assignment will copy content<br />

place for the content has <strong>to</strong> be associated <strong>to</strong> an array variable!<br />

In minijava array variables (and object variables) behave like<br />

pointers<br />

array assignment is pointer assignment<br />

only place for a pointer is associated <strong>to</strong> an array or object<br />

variable.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- subscripting<br />

With this, the translation of a[i] is immediate also:<br />

mem(plus( mem(e),mul(i,const(w))))<br />

mem(e) is the address where the array a starts,<br />

w is the wordsize<br />

Notice that identifiers can be used both as left-hand-sides of<br />

assignments and as parts of expressions. In some cases<br />

mem(e) nodes will stand for addresses, in other cases for<br />

values. It is only in move(mem(e1),e2) that mem(e) stands<br />

for the address. This will be reflected when generating <strong>code</strong><br />

(we will generate a s<strong>to</strong>re or a fetch)<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- subscripting<br />

With this, the translation of a[i] is immediate also:<br />

mem(plus( mem(e),mul(i,const(w))))<br />

mem(e) is the address where the array a starts,<br />

w is the wordsize<br />

Notice that identifiers can be used both as left-hand-sides of<br />

assignments and as parts of expressions. In some cases<br />

mem(e) nodes will stand for addresses, in other cases for<br />

values. It is only in move(mem(e1),e2) that mem(e) stands<br />

for the address. This will be reflected when generating <strong>code</strong><br />

(we will generate a s<strong>to</strong>re or a fetch)<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- subscripting<br />

With this, the translation of a[i] is immediate also:<br />

mem(plus( mem(e),mul(i,const(w))))<br />

mem(e) is the address where the array a starts,<br />

w is the wordsize<br />

Notice that identifiers can be used both as left-hand-sides of<br />

assignments and as parts of expressions. In some cases<br />

mem(e) nodes will stand for addresses, in other cases for<br />

values. It is only in move(mem(e1),e2) that mem(e) stands<br />

for the address. This will be reflected when generating <strong>code</strong><br />

(we will generate a s<strong>to</strong>re or a fetch)<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- subscripting<br />

With this, the translation of a[i] is immediate also:<br />

mem(plus( mem(e),mul(i,const(w))))<br />

mem(e) is the address where the array a starts,<br />

w is the wordsize<br />

Notice that identifiers can be used both as left-hand-sides of<br />

assignments and as parts of expressions. In some cases<br />

mem(e) nodes will stand for addresses, in other cases for<br />

values. It is only in move(mem(e1),e2) that mem(e) stands<br />

for the address. This will be reflected when generating <strong>code</strong><br />

(we will generate a s<strong>to</strong>re or a fetch)<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- subscripting<br />

With this, the translation of a[i] is immediate also:<br />

mem(plus( mem(e),mul(i,const(w))))<br />

mem(e) is the address where the array a starts,<br />

w is the wordsize<br />

Notice that identifiers can be used both as left-hand-sides of<br />

assignments and as parts of expressions. In some cases<br />

mem(e) nodes will stand for addresses, in other cases for<br />

values. It is only in move(mem(e1),e2) that mem(e) stands<br />

for the address. This will be reflected when generating <strong>code</strong><br />

(we will generate a s<strong>to</strong>re or a fetch)<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- operations<br />

Arithmetic<br />

Each opera<strong>to</strong>r in minijava corresponds <strong>to</strong> an opera<strong>to</strong>r construc<strong>to</strong>r<br />

in the <strong>intermediate</strong> representation. Just notice that<br />

There are no unary negations in the <strong>intermediate</strong><br />

representation<br />

Negate integers by substracting from 0<br />

Negate booleans by using XOR.<br />

Comparisons<br />

e1 < e2 will be translated using<br />

public class RelCx extends Cx {<br />

int op;<br />

Tree.Exp left, right;<br />

Tree.Stm unCx(Label t, Label f) {<br />

return new Tree.CJUMP(op, left, right, t, f);}<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- operations<br />

Arithmetic<br />

Each opera<strong>to</strong>r in minijava corresponds <strong>to</strong> an opera<strong>to</strong>r construc<strong>to</strong>r<br />

in the <strong>intermediate</strong> representation. Just notice that<br />

There are no unary negations in the <strong>intermediate</strong><br />

representation<br />

Negate integers by substracting from 0<br />

Negate booleans by using XOR.<br />

Comparisons<br />

e1 < e2 will be translated using<br />

public class RelCx extends Cx {<br />

int op;<br />

Tree.Exp left, right;<br />

Tree.Stm unCx(Label t, Label f) {<br />

return new Tree.CJUMP(op, left, right, t, f);}<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- operations<br />

Arithmetic<br />

Each opera<strong>to</strong>r in minijava corresponds <strong>to</strong> an opera<strong>to</strong>r construc<strong>to</strong>r<br />

in the <strong>intermediate</strong> representation. Just notice that<br />

There are no unary negations in the <strong>intermediate</strong><br />

representation<br />

Negate integers by substracting from 0<br />

Negate booleans by using XOR.<br />

Comparisons<br />

e1 < e2 will be translated using<br />

public class RelCx extends Cx {<br />

int op;<br />

Tree.Exp left, right;<br />

Tree.Stm unCx(Label t, Label f) {<br />

return new Tree.CJUMP(op, left, right, t, f);}<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- operations<br />

Arithmetic<br />

Each opera<strong>to</strong>r in minijava corresponds <strong>to</strong> an opera<strong>to</strong>r construc<strong>to</strong>r<br />

in the <strong>intermediate</strong> representation. Just notice that<br />

There are no unary negations in the <strong>intermediate</strong><br />

representation<br />

Negate integers by substracting from 0<br />

Negate booleans by using XOR.<br />

Comparisons<br />

e1 < e2 will be translated using<br />

public class RelCx extends Cx {<br />

int op;<br />

Tree.Exp left, right;<br />

Tree.Stm unCx(Label t, Label f) {<br />

return new Tree.CJUMP(op, left, right, t, f);}<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- operations<br />

Arithmetic<br />

Each opera<strong>to</strong>r in minijava corresponds <strong>to</strong> an opera<strong>to</strong>r construc<strong>to</strong>r<br />

in the <strong>intermediate</strong> representation. Just notice that<br />

There are no unary negations in the <strong>intermediate</strong><br />

representation<br />

Negate integers by substracting from 0<br />

Negate booleans by using XOR.<br />

Comparisons<br />

e1 < e2 will be translated using<br />

public class RelCx extends Cx {<br />

int op;<br />

Tree.Exp left, right;<br />

Tree.Stm unCx(Label t, Label f) {<br />

return new Tree.CJUMP(op, left, right, t, f);}<br />

}<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- array creation<br />

An array might outlive the method that creates it, thus it<br />

cannot be placed in the stack! It is placed in an unstructured<br />

piece of memory, the heap.<br />

The translation of new int [exp] should create an integer<br />

array of length determined by e and with each value initialized<br />

<strong>to</strong> 0:<br />

Determine the ammount of memory needed:<br />

(length + 1) ∗ size of integer<br />

Call an external function <strong>to</strong> allocate space on the heap,<br />

Generate <strong>code</strong> <strong>to</strong> s<strong>to</strong>re the length of the array at offset 0,<br />

Generate <strong>code</strong> <strong>to</strong> initialize all values <strong>to</strong> 0, starting at offset<br />

size of integer<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- array creation<br />

An array might outlive the method that creates it, thus it<br />

cannot be placed in the stack! It is placed in an unstructured<br />

piece of memory, the heap.<br />

The translation of new int [exp] should create an integer<br />

array of length determined by e and with each value initialized<br />

<strong>to</strong> 0:<br />

Determine the ammount of memory needed:<br />

(length + 1) ∗ size of integer<br />

Call an external function <strong>to</strong> allocate space on the heap,<br />

Generate <strong>code</strong> <strong>to</strong> s<strong>to</strong>re the length of the array at offset 0,<br />

Generate <strong>code</strong> <strong>to</strong> initialize all values <strong>to</strong> 0, starting at offset<br />

size of integer<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- array creation<br />

An array might outlive the method that creates it, thus it<br />

cannot be placed in the stack! It is placed in an unstructured<br />

piece of memory, the heap.<br />

The translation of new int [exp] should create an integer<br />

array of length determined by e and with each value initialized<br />

<strong>to</strong> 0:<br />

Determine the ammount of memory needed:<br />

(length + 1) ∗ size of integer<br />

Call an external function <strong>to</strong> allocate space on the heap,<br />

Generate <strong>code</strong> <strong>to</strong> s<strong>to</strong>re the length of the array at offset 0,<br />

Generate <strong>code</strong> <strong>to</strong> initialize all values <strong>to</strong> 0, starting at offset<br />

size of integer<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- array creation<br />

An array might outlive the method that creates it, thus it<br />

cannot be placed in the stack! It is placed in an unstructured<br />

piece of memory, the heap.<br />

The translation of new int [exp] should create an integer<br />

array of length determined by e and with each value initialized<br />

<strong>to</strong> 0:<br />

Determine the ammount of memory needed:<br />

(length + 1) ∗ size of integer<br />

Call an external function <strong>to</strong> allocate space on the heap,<br />

Generate <strong>code</strong> <strong>to</strong> s<strong>to</strong>re the length of the array at offset 0,<br />

Generate <strong>code</strong> <strong>to</strong> initialize all values <strong>to</strong> 0, starting at offset<br />

size of integer<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- array creation<br />

An array might outlive the method that creates it, thus it<br />

cannot be placed in the stack! It is placed in an unstructured<br />

piece of memory, the heap.<br />

The translation of new int [exp] should create an integer<br />

array of length determined by e and with each value initialized<br />

<strong>to</strong> 0:<br />

Determine the ammount of memory needed:<br />

(length + 1) ∗ size of integer<br />

Call an external function <strong>to</strong> allocate space on the heap,<br />

Generate <strong>code</strong> <strong>to</strong> s<strong>to</strong>re the length of the array at offset 0,<br />

Generate <strong>code</strong> <strong>to</strong> initialize all values <strong>to</strong> 0, starting at offset<br />

size of integer<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- array creation<br />

An array might outlive the method that creates it, thus it<br />

cannot be placed in the stack! It is placed in an unstructured<br />

piece of memory, the heap.<br />

The translation of new int [exp] should create an integer<br />

array of length determined by e and with each value initialized<br />

<strong>to</strong> 0:<br />

Determine the ammount of memory needed:<br />

(length + 1) ∗ size of integer<br />

Call an external function <strong>to</strong> allocate space on the heap,<br />

Generate <strong>code</strong> <strong>to</strong> s<strong>to</strong>re the length of the array at offset 0,<br />

Generate <strong>code</strong> <strong>to</strong> initialize all values <strong>to</strong> 0, starting at offset<br />

size of integer<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- object creation<br />

As with arrays, an object may outlive the method that creates<br />

it, it will be placed in the heap.<br />

To translate new ClassName()<br />

Generate <strong>code</strong> for allocating heap space for all fields<br />

notice difference between local variables (on the stack) and fields (on the<br />

heap)!<br />

Iterate through field memory locations and initialize them.<br />

Both for array creation and object creation we need <strong>to</strong> call<br />

external functions.<br />

in order <strong>to</strong> follow machine dependent conventions, we leave<br />

this <strong>to</strong> the class Frame :<br />

public abstract Tree.Exp externalCall(String func,<br />

List args);<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- object creation<br />

As with arrays, an object may outlive the method that creates<br />

it, it will be placed in the heap.<br />

To translate new ClassName()<br />

Generate <strong>code</strong> for allocating heap space for all fields<br />

notice difference between local variables (on the stack) and fields (on the<br />

heap)!<br />

Iterate through field memory locations and initialize them.<br />

Both for array creation and object creation we need <strong>to</strong> call<br />

external functions.<br />

in order <strong>to</strong> follow machine dependent conventions, we leave<br />

this <strong>to</strong> the class Frame :<br />

public abstract Tree.Exp externalCall(String func,<br />

List args);<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- object creation<br />

As with arrays, an object may outlive the method that creates<br />

it, it will be placed in the heap.<br />

To translate new ClassName()<br />

Generate <strong>code</strong> for allocating heap space for all fields<br />

notice difference between local variables (on the stack) and fields (on the<br />

heap)!<br />

Iterate through field memory locations and initialize them.<br />

Both for array creation and object creation we need <strong>to</strong> call<br />

external functions.<br />

in order <strong>to</strong> follow machine dependent conventions, we leave<br />

this <strong>to</strong> the class Frame :<br />

public abstract Tree.Exp externalCall(String func,<br />

List args);<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- object creation<br />

As with arrays, an object may outlive the method that creates<br />

it, it will be placed in the heap.<br />

To translate new ClassName()<br />

Generate <strong>code</strong> for allocating heap space for all fields<br />

notice difference between local variables (on the stack) and fields (on the<br />

heap)!<br />

Iterate through field memory locations and initialize them.<br />

Both for array creation and object creation we need <strong>to</strong> call<br />

external functions.<br />

in order <strong>to</strong> follow machine dependent conventions, we leave<br />

this <strong>to</strong> the class Frame :<br />

public abstract Tree.Exp externalCall(String func,<br />

List args);<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- object creation<br />

As with arrays, an object may outlive the method that creates<br />

it, it will be placed in the heap.<br />

To translate new ClassName()<br />

Generate <strong>code</strong> for allocating heap space for all fields<br />

notice difference between local variables (on the stack) and fields (on the<br />

heap)!<br />

Iterate through field memory locations and initialize them.<br />

Both for array creation and object creation we need <strong>to</strong> call<br />

external functions.<br />

in order <strong>to</strong> follow machine dependent conventions, we leave<br />

this <strong>to</strong> the class Frame :<br />

public abstract Tree.Exp externalCall(String func,<br />

List args);<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- object creation<br />

As with arrays, an object may outlive the method that creates<br />

it, it will be placed in the heap.<br />

To translate new ClassName()<br />

Generate <strong>code</strong> for allocating heap space for all fields<br />

notice difference between local variables (on the stack) and fields (on the<br />

heap)!<br />

Iterate through field memory locations and initialize them.<br />

Both for array creation and object creation we need <strong>to</strong> call<br />

external functions.<br />

in order <strong>to</strong> follow machine dependent conventions, we leave<br />

this <strong>to</strong> the class Frame :<br />

public abstract Tree.Exp externalCall(String func,<br />

List args);<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- object creation<br />

As with arrays, an object may outlive the method that creates<br />

it, it will be placed in the heap.<br />

To translate new ClassName()<br />

Generate <strong>code</strong> for allocating heap space for all fields<br />

notice difference between local variables (on the stack) and fields (on the<br />

heap)!<br />

Iterate through field memory locations and initialize them.<br />

Both for array creation and object creation we need <strong>to</strong> call<br />

external functions.<br />

in order <strong>to</strong> follow machine dependent conventions, we leave<br />

this <strong>to</strong> the class Frame :<br />

public abstract Tree.Exp externalCall(String func,<br />

List args);<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- while loops<br />

To translate<br />

while (exp) stm<br />

we use its meaning:<br />

test:<br />

if (not exp) go<strong>to</strong> done<br />

stm<br />

go<strong>to</strong> test<br />

done:<br />

Resulting in<br />

seq(seq( seq( label(test),ircjumpEQ,exp.unEx(),const(1),T,F),seq( label(T),body.unNx()),label(f)))<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- while loops<br />

To translate<br />

while (exp) stm<br />

we use its meaning:<br />

test:<br />

if (not exp) go<strong>to</strong> done<br />

stm<br />

go<strong>to</strong> test<br />

done:<br />

Resulting in<br />

seq(seq( seq( label(test),ircjumpEQ,exp.unEx(),const(1),T,F),seq( label(T),body.unNx()),label(f)))<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- while loops<br />

To translate<br />

while (exp) stm<br />

we use its meaning:<br />

test:<br />

if (not exp) go<strong>to</strong> done<br />

stm<br />

go<strong>to</strong> test<br />

done:<br />

Resulting in<br />

seq(seq( seq( label(test),ircjumpEQ,exp.unEx(),const(1),T,F),seq( label(T),body.unNx()),label(f)))<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- function calls<br />

To translate<br />

obj.f(a1, . . . a2)<br />

Use a call() node<br />

call(name(lf ),obj,e1,. . . en)<br />

Where:<br />

lf is the label generated for f when translating its declaration.<br />

obj is the translation of obj that must be added as an<br />

argument <strong>to</strong> the method.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- function calls<br />

To translate<br />

obj.f(a1, . . . a2)<br />

Use a call() node<br />

call(name(lf ),obj,e1,. . . en)<br />

Where:<br />

lf is the label generated for f when translating its declaration.<br />

obj is the translation of obj that must be added as an<br />

argument <strong>to</strong> the method.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- function calls<br />

To translate<br />

obj.f(a1, . . . a2)<br />

Use a call() node<br />

call(name(lf ),obj,e1,. . . en)<br />

Where:<br />

lf is the label generated for f when translating its declaration.<br />

obj is the translation of obj that must be added as an<br />

argument <strong>to</strong> the method.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- function calls<br />

To translate<br />

obj.f(a1, . . . a2)<br />

Use a call() node<br />

call(name(lf ),obj,e1,. . . en)<br />

Where:<br />

lf is the label generated for f when translating its declaration.<br />

obj is the translation of obj that must be added as an<br />

argument <strong>to</strong> the method.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- function calls<br />

To translate<br />

obj.f(a1, . . . a2)<br />

Use a call() node<br />

call(name(lf ),obj,e1,. . . en)<br />

Where:<br />

lf is the label generated for f when translating its declaration.<br />

obj is the translation of obj that must be added as an<br />

argument <strong>to</strong> the method.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- declarations<br />

For each variable declaration in a method body, additional<br />

space will be reserved (in the frame or in a register) and this<br />

must be recorded in the environment <strong>to</strong> be used later!<br />

We get an Access (the abstraction for inFrame or inRegister)<br />

by using<br />

frame.allocLocal(false); where frame is the Frame<br />

that has been created during the elaboration of the current<br />

method declaration.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- declarations<br />

For each variable declaration in a method body, additional<br />

space will be reserved (in the frame or in a register) and this<br />

must be recorded in the environment <strong>to</strong> be used later!<br />

We get an Access (the abstraction for inFrame or inRegister)<br />

by using<br />

frame.allocLocal(false); where frame is the Frame<br />

that has been created during the elaboration of the current<br />

method declaration.<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- declarations<br />

For each method declaration<br />

An instance of Frame is created<br />

The body is translated <strong>to</strong> a statement tree<br />

These two are put <strong>to</strong>gether in<strong>to</strong> a fragment<br />

At this stage we do not consider the machine dependent<br />

prologue and epilogue of assembler instructions <strong>to</strong> allocate the<br />

frame <strong>to</strong> the stack and, jump <strong>to</strong> the proper address, etc.<br />

To translate method<br />

B f(A1a1 . . . Anan){decls stms return exp}<br />

build an eseq(stms,exp) and (if necessary) s<strong>to</strong>re the result in<br />

a dedicated register (Frame.RV() ) You will get the part of the translation that<br />

elaborates declarations!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- declarations<br />

For each method declaration<br />

An instance of Frame is created<br />

The body is translated <strong>to</strong> a statement tree<br />

These two are put <strong>to</strong>gether in<strong>to</strong> a fragment<br />

At this stage we do not consider the machine dependent<br />

prologue and epilogue of assembler instructions <strong>to</strong> allocate the<br />

frame <strong>to</strong> the stack and, jump <strong>to</strong> the proper address, etc.<br />

To translate method<br />

B f(A1a1 . . . Anan){decls stms return exp}<br />

build an eseq(stms,exp) and (if necessary) s<strong>to</strong>re the result in<br />

a dedicated register (Frame.RV() ) You will get the part of the translation that<br />

elaborates declarations!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- declarations<br />

For each method declaration<br />

An instance of Frame is created<br />

The body is translated <strong>to</strong> a statement tree<br />

These two are put <strong>to</strong>gether in<strong>to</strong> a fragment<br />

At this stage we do not consider the machine dependent<br />

prologue and epilogue of assembler instructions <strong>to</strong> allocate the<br />

frame <strong>to</strong> the stack and, jump <strong>to</strong> the proper address, etc.<br />

To translate method<br />

B f(A1a1 . . . Anan){decls stms return exp}<br />

build an eseq(stms,exp) and (if necessary) s<strong>to</strong>re the result in<br />

a dedicated register (Frame.RV() ) You will get the part of the translation that<br />

elaborates declarations!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- declarations<br />

For each method declaration<br />

An instance of Frame is created<br />

The body is translated <strong>to</strong> a statement tree<br />

These two are put <strong>to</strong>gether in<strong>to</strong> a fragment<br />

At this stage we do not consider the machine dependent<br />

prologue and epilogue of assembler instructions <strong>to</strong> allocate the<br />

frame <strong>to</strong> the stack and, jump <strong>to</strong> the proper address, etc.<br />

To translate method<br />

B f(A1a1 . . . Anan){decls stms return exp}<br />

build an eseq(stms,exp) and (if necessary) s<strong>to</strong>re the result in<br />

a dedicated register (Frame.RV() ) You will get the part of the translation that<br />

elaborates declarations!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- declarations<br />

For each method declaration<br />

An instance of Frame is created<br />

The body is translated <strong>to</strong> a statement tree<br />

These two are put <strong>to</strong>gether in<strong>to</strong> a fragment<br />

At this stage we do not consider the machine dependent<br />

prologue and epilogue of assembler instructions <strong>to</strong> allocate the<br />

frame <strong>to</strong> the stack and, jump <strong>to</strong> the proper address, etc.<br />

To translate method<br />

B f(A1a1 . . . Anan){decls stms return exp}<br />

build an eseq(stms,exp) and (if necessary) s<strong>to</strong>re the result in<br />

a dedicated register (Frame.RV() ) You will get the part of the translation that<br />

elaborates declarations!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- declarations<br />

For each method declaration<br />

An instance of Frame is created<br />

The body is translated <strong>to</strong> a statement tree<br />

These two are put <strong>to</strong>gether in<strong>to</strong> a fragment<br />

At this stage we do not consider the machine dependent<br />

prologue and epilogue of assembler instructions <strong>to</strong> allocate the<br />

frame <strong>to</strong> the stack and, jump <strong>to</strong> the proper address, etc.<br />

To translate method<br />

B f(A1a1 . . . Anan){decls stms return exp}<br />

build an eseq(stms,exp) and (if necessary) s<strong>to</strong>re the result in<br />

a dedicated register (Frame.RV() ) You will get the part of the translation that<br />

elaborates declarations!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- fragments<br />

When elaborating a method declaration, the fragment<br />

including the frame and the ir-tree is added <strong>to</strong> a list.<br />

The visi<strong>to</strong>r class doing the translation includes a method <strong>to</strong><br />

retrieve the list of fragments (the result of translating the<br />

whole program)<br />

private LinkedList frags = new LinkedList();<br />

public Itera<strong>to</strong>r getResults() {<br />

return frags.itera<strong>to</strong>r();}<br />

This list is further used as input <strong>to</strong> the back-end of the<br />

compiler.<br />

In part 4 of the project we will just print out the list of<br />

fragments!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- fragments<br />

When elaborating a method declaration, the fragment<br />

including the frame and the ir-tree is added <strong>to</strong> a list.<br />

The visi<strong>to</strong>r class doing the translation includes a method <strong>to</strong><br />

retrieve the list of fragments (the result of translating the<br />

whole program)<br />

private LinkedList frags = new LinkedList();<br />

public Itera<strong>to</strong>r getResults() {<br />

return frags.itera<strong>to</strong>r();}<br />

This list is further used as input <strong>to</strong> the back-end of the<br />

compiler.<br />

In part 4 of the project we will just print out the list of<br />

fragments!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- fragments<br />

When elaborating a method declaration, the fragment<br />

including the frame and the ir-tree is added <strong>to</strong> a list.<br />

The visi<strong>to</strong>r class doing the translation includes a method <strong>to</strong><br />

retrieve the list of fragments (the result of translating the<br />

whole program)<br />

private LinkedList frags = new LinkedList();<br />

public Itera<strong>to</strong>r getResults() {<br />

return frags.itera<strong>to</strong>r();}<br />

This list is further used as input <strong>to</strong> the back-end of the<br />

compiler.<br />

In part 4 of the project we will just print out the list of<br />

fragments!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>


Translating minijava- fragments<br />

When elaborating a method declaration, the fragment<br />

including the frame and the ir-tree is added <strong>to</strong> a list.<br />

The visi<strong>to</strong>r class doing the translation includes a method <strong>to</strong><br />

retrieve the list of fragments (the result of translating the<br />

whole program)<br />

private LinkedList frags = new LinkedList();<br />

public Itera<strong>to</strong>r getResults() {<br />

return frags.itera<strong>to</strong>r();}<br />

This list is further used as input <strong>to</strong> the back-end of the<br />

compiler.<br />

In part 4 of the project we will just print out the list of<br />

fragments!<br />

<strong>Computer</strong> <strong>Languages</strong> <strong>Translation</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!