compiler compiler tutorial - faculty of ict maltasspi3/csa2201_lecture-2012.pdf... [^cde] = not...

109
Compiler Compiler Tutorial CSA2010 – Compiler Techniques Gordon Mangion

Upload: vothuan

Post on 24-Mar-2018

218 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Compiler Compiler Tutorial CSA2010 – Compiler Techniques

Gordon Mangion

Page 2: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Introduction

With so many Compilers around, do we

need to write parsers/compilers in

industry?

FTP interface

Translator from VB to Java

EPS Reader

NLP

Page 3: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Topics

Prerequisites

Compiler Architecture/Modules

JavaCC

Virtual Machines

Semantic Analysis

Code Generation and Execution

Examples

The assignment (SL Compiler)

Page 4: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Prerequisites

Java

Regular Expressions

◦ ? + * …

Production rules and EBNF

Semantics

Page 5: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Regular Expressions

◦ Repetitions

+ = 1 or more

* = 0 or more

? = 0 or 1

◦ Alternative

a | b = a or b

◦ Ranges

[a-z] = a to z

[fls] = f, l and s

[^cde] = not c,d,e

◦ Examples

a+ - a, aa, aaa

b* - , b, bb

c? - , c

a | b - a, b

- a,b,c,d,e,f,g,…,y,z

- f,l,s

- a,b,f,g,…,y,z

Page 6: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Compiler

What is a compiler?

◦ Where to start from?

◦ Design / Architecture of a Compiler

Page 7: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Compiler

Is essentially a complex function which

maps a program in a source language

onto a program in the target language.

Source

Code

C o m p i l e r

Lexical

Analysis

Syntax

Analysis

Semantic

Analysis

I-Code

Optimiser

Code

Generator

Code

Execution

Code

Optimiser Optimiser

Error

Handler

Symbol

Table …

Target

Code

Page 8: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Translation

Source Code

int Sqr( int x )

{

var int n = x;

n = n * n;

return n;

}

Target Code

push ebp

mov ebp,esp

sub esp,0CCh

push ebx

push esi

push edi

lea edi,[ebp-0CCh]

mov ecx,33h

mov eax,0CCCCCCCCh

rep stos dword ptr es:[edi]

mov eax,dword ptr [x]

mov dword ptr [n],eax

mov eax,dword ptr [n]

imul eax,dword ptr [n]

mov dword ptr [n],eax

mov eax,dword ptr [n]

pop edi

pop esi

pop ebx

mov esp,ebp

pop ebp

ret

Page 9: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Translation

Source Code

int Sqr( int x )

{

var int n = x;

n = n * n;

return n;

}

Compiler

All possible translations

of the source program

Page 10: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Translation

Source Code

int Sqr( int x )

{

var int n = x;

n = n * n;

return n;

}

Target Code push ebp

mov ebp,esp

sub esp,0CCh

push ebx

push esi

push edi

lea edi,[ebp-0CCh]

mov ecx,33h

mov eax,0CCCCCCCCh

rep stos dword ptr es:[edi]

mov eax,dword ptr [x]

mov dword ptr [n],eax

mov eax,dword ptr [n]

imul eax,dword ptr [n]

mov dword ptr [n],eax

mov eax,dword ptr [n]

pop edi

pop esi

pop ebx

mov esp,ebp

pop ebp

ret

Page 11: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Production Rules

Context Free Grammars

◦ A B “c” D

BNF

◦ A ::= B “c” D

EBNF

◦ A ::= B* “c”? D+

Page 12: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

The Language game

Source Language

Target Language

Implementation Language

Page 13: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Tombstone Diagram

Implementation

Language

Source Target

Language Language

Page 14: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Compiler Architecture

Lexical

Analysis

Syntax

Analysis

Semantic

Analysis

I-Code

Optimiser

Code

Generator

Code

Execution

Code

Optimiser Optimiser

Error

Handler

Source

Code

Symbol

Table …

Sourc

e L

angu

age

Toke

ns

Intermediate

Code

Inte

rmedia

te

Code

Intermediate

Code

Page 15: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Simplified Compiler Architecture

Lexical

Analysis

Syntax

Analysis

Semantic

Analysis

Code

Generator

Source

Code

Symbol

Table

Source Language

Tokens

Intermediate Code

Intermediate Code

Target

Code

Page 16: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Lexical Analysis

Tokenises the source file

◦ Removes whitespace

◦ Tokens

◦ Keywords

Source Code

function Sqr( x : int )

: int

{

let n : int = x;

n <- n * n;

return n;

}

Function (Keyword)

Sqr (Identifier)

( (Lparen)

X (Identifier)

: (Colon)

Int (Keyword)

) (Rparen)

: (Colon)

Int (Keyword)

{ (Lbrace)

Let (Keyword)

N (Identifier)

: (Colon)

Int (Keyword)

= (Eq)

X (Identifier)

; (Semicolon)

Page 17: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Syntax Analysis (Parser)

Checks that the source file conforms to

the language grammar

◦ E.g. FunctionDecl ::= “function” identifier “(“ …

Creates intermediate code

◦ ParseTree

Source Code

function Sqr( x : int ) …

Source Code

function 1.5( x : int )

2 + 5

SimpleExpr

Page 18: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Semantic Analysis

Checks that the meaning behind the

syntax makes sense, according to the

type-system

◦ E.g. var int x = 3;

x:int, 3:int

◦ E.g. var int x = 3.2;

x:int, 3.2:Real

Page 19: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Code Generation / Execution

Traverses the Intermediate Code

representation and generates or executes

the code

2

+

5

PlusNode.Generate()

{

leftChild.Generate() ;

rightChild.Generate();

EmitCode(Add);

}

IntLiteralNode.Generate()

{

EmitCode ( Push, Integer.parseInt(this.getValue()) );

}

Page 20: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Symbol Table

Important Data Structure ◦ Keeps information about every identifier in the

source Variables

functions

Labels …

◦ Keeps the scope information

◦ Entry Properties Name

Type

Value

Scope

Page 21: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Symbol Table

A data structure that maps an Identifier’s

name onto a structure that contains the

identifier’s information

Name ( Name, Type, Value, Scope )

Page 22: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Symbol Table

Easiest way is to make use of a hash-table

/ hash-map

Create a new Hash-map for each new

scope

Page 23: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Java Interfaces

Interface is a contract

If a Class implements interface

◦ Honours the contract

◦ Implements the methods of the interface

Interface type variable can hold instance

of a class that implements that interface

Page 24: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Java Interfaces

public interface IAdder

{

int add(int n, int m);

}

public interface IMinus

{

int subtract(int n, int m);

}

Page 25: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Java Interfaces

public interface IAdder

{

int add(int n, int m);

}

public interface IMinus

{

int subtract(int n, int m);

}

public class SimpleMath

implements IAdder, IMinus

{

public int add(int n, int m)

{ … }

public int subtract(int n, int m)

{ … }

}

Page 26: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Java Interfaces

public interface IAdder

{

int add(int n, int m);

}

public interface IMinus

{

int subtract(int n, int m);

}

public class SimpleMath

implements IAdder, IMinus

{

public int add(int n, int m)

{ … }

public int subtract(int n, int m)

{ … }

}

public static void main(…)

{

IAdder a = new SimpleMath();

IMinus s = new SimpleMath();

a.add(1,2);

s.subtract(4,2);

}

Page 27: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Java Interfaces

public interface ICalculator

{

int Calculate(int n, int m);

}

Page 28: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Java Interfaces

public class Mul

implements ICalculator

{

public int Calculate(int n, int m)

{ … n * m … }

}

public class Div

implements ICalculator

{

public int Calculate(int n, int m)

{ … n / m …}

public interface ICalculator

{

int Calculate(int n, int m);

}

Page 29: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Java Interfaces

public class Mul

implements ICalculator

{

public int Calculate(int n, int m)

{ … n * m … }

}

public class Div

implements ICalculator

{

public int Calculate(int n, int m)

{ … n / m …}

public interface ICalculator

{

int Calculate(int n, int m);

}

public static void main(…)

{

ICalculator c1 = new Mul();

ICalculator c2 = new Div();

c1.Calculate(4,2);

c2.Calculate(4,2);

}

Page 30: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Simplified Compiler Architecture

Lexical

Analysis

Syntax

Analysis

Semantic

Analysis

Code

Generator

Source

Code

Symbol

Table

Source Language

Tokens

Intermediate Code

Intermediate Code

Target

Code

Front-End

Back-End

Page 31: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Compiler Implementation

Compiler Architecture

Lexical

Analysis Parser

(Syntax Analysis)

Type-Checker (Semantic Analysis)

Code

Execution

Source

Code

Symbol

Table

Com

pile

r In

terf

ace

Parse

Tree

Vis

itor

Inte

rfac

e

Vis

itor

Inte

rfac

e

Call

Visitor

Call

Visitor

Page 32: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Program Generators?

Source-to-Executable

◦ Compiler

Reverse Polish Notation

◦ 2 + 3 - 4 2 3 + 4 -

Source-to-Source

◦ Preprocessors

Page 33: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Javacc

A program that creates parsers

The source code(input) is a definition file

◦ Grammar definition

◦ Inline Java code

The target(output) is java-based parser

Recognizer?

Page 34: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Javacc

Javacc langdef.jj

*.java *.java

*.java *.java

Page 35: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

JJTree

Preprocessor to Javacc

Creates “.jj” files from “.jjt” files

From a recognizer to a proper parser

Produces Parse Tree building actions

Page 36: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

JJTree

Javacc

langdef.jjt

*.java *.java

*.java *.java

JJTree langdef.jj

Page 37: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

JJTree

Javacc

langdef.jjt

*.java *.java

*.java *.java

JJTree langdef.jj

“J J T” T O O L

Page 38: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Installing Javacc

https://javacc.dev.java.net/

◦ Javacc.zip / Javacc.tar.gz

Eclipse plugin

◦ Preferences->Install/Update->Add

http://eclipse-javacc.sourceforge.net/

◦ Help->New Software

◦ Newer versions

Eclipse Marketplace

Page 39: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

.jjt Grammar Files

Four (4) sections

◦ Options

◦ Parser

◦ Tokens

◦ Production Rules

Page 40: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

.jjt Options

options

{

BUILD_NODE_FILES=false;

STATIC=false;

MULTI=true;

}

Page 41: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

.jjt Parser block

PARSER_BEGIN(parser_name)

. . .

public class parser_name

{

}

PARSER_END(parser_name)

Page 42: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

.jjt Tokens

Lexeme to ignore

SKIP :

{

“ ”

| “\t”

| “\n”

| “\r”

| <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")>

}

Page 43: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

.jjt Tokens

Tokens that are to be returned

TOKEN :

{

<PLUS: “+”>

|<TRUE: "true" >

| <FALSE: "false" >

| <LETTER: (["A"-"Z"] | ["a"-"z"]) >

| <STRING_LITERAL: "\"" (~["\"","\r","\n"])* "\"" >

| <#DIGIT: [“0”-”9”]>

}

Page 44: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

.jjt Production Rules

Assume the following:

Header “Script” <STRING_LITERAL>

or

Header ::= “Script” <STRING_LITERAL>

Page 45: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

.jjt Production Rules

Assume the following:

Header ::= “Script” <STRING_LITERAL>

void Header():

{

<SCRIPT> <STRING_LITERAL>

}

Page 46: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

.jjt Production Rules

Header ::= “Script” <STRING_LITERAL>

void Header(): { int n = 0; n++;}

{

<SCRIPT> <STRING_LITERAL>

}

Page 47: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

.jjt Production Rules

Token matching - Actions

void Header(): { int n = 0; n++; }

{

<SCRIPT>{ n = 1; }

<STRING_LITERAL>

{ n++; }

}

Page 48: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

.jjt Production Rules

Calling Non-Terminals

void Integer(): {}

{ … }

void Header(): { }

{

<SCRIPT>

Integer()

}

Page 49: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

.jjt Production Rules

Getting Token value

void Number()

{ Token t;

int n = 0; }

{

t=<DIGIT>{ n = integer.parseInt(t.image); }

}

Page 50: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Lookahead

Consider the following java statements

◦ public int name;

◦ public int name[];

◦ public int name() { … }

Page 51: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Building the AST

(Abstract Syntax Tree) options

{

STATIC=false;

MULTI=true;

BUILD_NODE_FILES=true;

NODE_USES_PARSER=false;

NODE_PREFIX=“”; // default “AST”

}

Page 52: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Building the AST

SimpleNode Start() : {}

{

Expression()

“;”

<EOF> // Special Token

{

return jjtThis;

}

}

Page 53: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Parsing

PARSER_BEGIN(MyParser)

...

MyParser parser = new MyParser(System.in);

try {

SimpleNode n = parser.Start();

System.out.println(“Parsed!");

} catch (Exception e) {

System.out.println("Oops!");

System.out.println(e.getMessage());

}

...

PARSER_END(MyParser)

Page 54: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Example Digit ::= [“0” - “9”]

Number ::= Digit+

Plus ::= “+”

Minus ::= “-”

Op ::= Plus | Minus

Expression ::= Number { Op Number }

Page 55: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Example TOKEN:

{

< NUMBER: <DIGIT>+ >

| < Digit: [“0” - “9”] >

| < PLUS: “+” >

| < MINUS: “-” >

}

Digit ::= [“0” - “9”]

Number ::= Digit+

Plus ::= “+”

Minus ::= “-”

Page 56: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Example void Op() :{}

{

< PLUS > | < MINUS >

}

void Expression(): {}

{

< Number >

(Op() < Number >)*

}

Op ::= Plus | Minus

Expression ::=

Number { Op Number }

Page 57: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Example

PARSER_BEGIN(MyParser)

...

MyParser parser = new MyParser(System.in);

try {

parser.Expression();

System.out.println(“Parsed!");

} catch (Exception e) {

System.out.println("Oops!");

System.out.println(e.getMessage());

}

...

PARSER_END(MyParser)

Page 58: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Generated Sources

Page 59: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Example – Evaluating

Token Op() :

{ Token t; }

{

(t = < PLUS > | t = < MINUS >)

{ return t; }

}

Page 60: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Example int Expression(): { Token t, op; int n;}

{

t = < NUMBER >

{

n = Integer.parseInt( t.image );

}

( op=Op()

t = < NUMBER > {

if(op.image.equals("+"))

n += Integer.parseInt( t.image );

else

n -= Integer.parseInt( t.image ); }

)*

{ return n; }

}

Page 61: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Example PARSER_BEGIN(MyParser)

public class MyParser

{

...

MyParser parser = new MyParser(System.in);

try

{

int n = parser.Expression();

...

PARSER_END(MyParser)

Page 62: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Example - Building the AST

options

{

STATIC=false;

MULTI=true;

BUILD_NODE_FILES=true;

NODE_USES_PARSER=false;

NODE_PREFIX=“AST”;

}

Page 63: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Generated Code

Page 64: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Example void Op() :{}

{

< PLUS > | < MINUS >

}

SimpleNode Expression(): {}

{

< Number >

(Op() < Number >)*

{ return jjtThis; }

}

Op ::= Plus | Minus

Expression ::=

Number { Op Number }

Page 65: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Example PARSER_BEGIN(MyParser)

public class MyParser

{

...

MyParser parser = new MyParser(System.in);

try

{

SimpleNode rootNode = parser.Expression();

...

PARSER_END(MyParser)

Page 66: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Example Grammar 2

Digit ::= [“0” - “9”]

Number ::= Digit+

Factor ::= Expression | Number

Term ::= Factor [ “*” | “/” Factor ]

Expression ::= Term { “+” | “-” Term }

Start ::= Expression

Page 67: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

The Visitor Design Pattern

The Problem

◦ Number of operations

to be performed on

each node

◦ Options

Implement each operation

inside each node

Make use of visitor

pattern

+

2 *

A 7

Page 68: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

The Visitor Design Pattern

Consider one Node

◦ Printing Operation

We can implement the

operation in a separate

class e.g. PrintVisitor

This can be done for all

type of nodes

Plus

PrintVisitor

void PrettyPrint( Plus )

{

}

Page 69: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

The Visitor Design Pattern

Modification on node

Client

PrintVisitor

void PrettyPrint( Plus )

{

}

Plus

void Print( Visitor p_visitor)

{

p_visitor.PrettyPrint( this );

}

Caller

{

PrintVisitor pr = new PrintVisitor();

Plus plus …

plus.Print(pr);

}

Page 70: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

The Visitor Design Pattern

Finally

Interface IVisitor

void Visit( Plus );

Plus

void Accept( IVisitor p_visitor)

{

p_visitor.Visit( this );

}

PrintVisitor

implements IVisitor

void Visit( Plus )

{

}

Caller

{

PrintVisitor pr = new PrintVisitor();

Plus plus …

plus.Accept(pr);

}

Page 71: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

The Visitor Design Pattern

Benefits

Interface IVisitor

void Visit( Plus );

void Visit( Times );

Plus

void Accept( IVisitor p_visitor)

{

p_visitor.Visit( this );

}

PrintVisitor

implements IVisitor

void Visit( Plus ) {…}

void Visit( Times ) {…}

TypeCheckVisitor

implements IVisitor

void Visit( Plus ) {…}

void Visit( Times ) {…}

EvaluationVisitor

implements IVisitor

void Visit( Plus ) {…}

void Visit( Times ) {…}

Page 72: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Compiler Implementation

Compiler Architecture

Lexical

Analysis Parser

(Syntax Analysis)

Type-Checker (Semantic Analysis)

Code

Execution

Source

Code

Symbol

Table

Com

pile

r In

terf

ace

Parse

Tree

Vis

itor

Inte

rfac

e

Vis

itor

Inte

rfac

e

Call

Visitor

Call

Visitor

Page 73: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

JJTree and the Visitor Pattern

Options

{

VISITOR=true;

}

Page 74: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Visitor interface public interface MyParserVisitor

{

public Object visit(SimpleNode node, Object data);

public Object visit(ASTOp node, Object data);

public Object visit(ASTExpression node, Object data);

}

Page 75: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Visitor - Node modification

Public class ASTExpression extends SimpleNode {

public ASTExpression(int id) {

super(id);

}

public ASTExpression(MyParser p, int id) {

super(p, id);

}

/** Accept the visitor. **/

public Object jjtAccept(MyParserVisitor visitor, Object data) {

return visitor.visit(this, data);

}

}

Page 76: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

JJTree and the Visitor Pattern

Options

{

VISITOR=true;

VISITOR_DATA_TYPE=“SymbolTable”;

VISITOR_RETURN_TYPE=“Object”;

}

Page 77: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Visitor - return type

We need a return-type generic enough to accommodate the results returned by all the visitor implementations

By default jjt uses the Object class ◦ Can easily be used however class-casts

instanceof

A generic container can be designed to hold the results

Page 78: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Visitor - return type

Types –

◦ Create an

enumeration

containing the types

of the language’s

type-system.

This should always

be created

enum DataType

{

None (/ Unknown),

Error,

Skip (/ Command),

Bool,

Int,

Function,

}

Page 79: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Visitor - return type

Result class Class Result

{

DataType type;

Object value;

Getter & Setter

Conversion

}

Page 80: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Visitor - return type

We can make use of:

◦ Object

Most generic return type

Have to cast to proper instance

◦ DataType

When the Type of the node suffices

◦ Result class

When we need to return some data

Page 81: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

SymbolTable

Entry

◦ String Name

◦ DataType Type

◦ Value?

Name Entry

Page 82: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

SymbolTable

Entry

◦ String Name

◦ DataType Type

◦ Int ScopeLevel

◦ Var / Param

◦ DataType ReturnType

◦ Int VarCount

◦ Int ParamCount

◦ (Entry Containter)

Page 83: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Type-Checking – (Semantic Analysis)

SemanticAnalyser

◦ (implements Visitor)

Consider

◦ BooleanLiteral()

public DataType visit( ASTBooleanLiteral node,

SymbolTable data)

{

return DataType.Bool;

}

Page 84: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Type-Checking – (Semantic Analysis)

Consider

◦ Identifier()

public Object visit( ASTIdentifier node, SymbolTable data ) { // Get name from the node String name = (String)node.jjtGetValue(); Consult symboltable, check if name exist if yes return its DataType println(“Error ASTIdentifier : " + name); return DataType.Error; }

Page 85: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Type-Checking – (Semantic Analysis)

Consider Assignment():

{ Identifier()

<ASSIGN>

Expression()

}

Ident

=

+ 3 2

Expr

SimpleExpr

Page 86: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Type-Checking – (Semantic Analysis)

Consider

◦ Assignment(): { Identifier() <ASSIGN> Expression() }

public Object visit(ASTAssignment node, SymbolTable data ) { // Check identifier DataType identType = node.jjtGetChild(0).visit(this, data); if(identType == DataType.Error) return DataType.Error;

Page 87: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Type-Checking – (Semantic Analysis)

Consider

◦ Assignment(): { Identifier() <ASSIGN> Expression() }

// Check expresion

DataType exprType =

node.jjtGetChild(1).visit(this, data);

if(identType != exprType)

{ println(“Error ASTAssignment”);

return DataType.Error; }

return DataType.Skip;

}

Page 88: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Type-Checking – (Semantic Analysis)

Note the difference in the use of identifiers

when they are:

L-value [ a = 2 + 3 ]

R-value [ x = a + 4 ]

Page 89: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Type-Checking – (Semantic Analysis)

Consider the following decision rule

◦ IfStatement ::= “if” “(“ Expression “)” …

if( 3 + 2 )

{

}

Expression

if

… … …

Statement

Block Statement

Block

Page 90: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Type-Checking – (Semantic Analysis)

Use node.jjtGetNumChildren() to get the number of children the node has.

This determines the shape of the tree

examples are

◦ If Statement

◦ Variable Declaration Initialiser (var int n = 0;)

◦ Function Delaration

◦ Statement Block

Page 91: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Type-Checking – (Semantic Analysis)

Implement the call to the visitor: Public class MyParser {

... main ...

Try

{

SimpleNode root = parser.Expression();

MyParserVisitor visitor = new TypeChecker();

Result result = root.jjtAccept(visitor, null );

System.out.println("DataType is " +

result .getType().toString());

...

Page 92: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Interpreter – Code Execution

In a similar way to Semantic Analysis, we

can make use of the visitor pattern

This time, we return values as well rather

than type-information only

We have to take special care about the

difference in the semantics of the

languages involved

Page 93: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Code Generation

Once again we make use of the visitor

pattern

In this stage we traverse the parse-tree

and emit target language code in our case

S-Machine instructions

In particular, we traverse nodes and emit

code patterns

Page 94: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

SymbolTable

Entries require an “Offset” variable

◦ For data related nodes, “Offset” is the actual

offset in the Stack.

◦ For flow control nodes, “Offset” holds the

location in the Code Area

Page 95: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Code Generation

Since we are emitting S-Machine instructions, it is wise to create an enum of the instructions

public enum SMInstruction { NOP, LDC, LD, STORE, DUP, … }

Page 96: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Code Generation

At the end of the whole process we are writing a binary file which is the compiled program.

It is a good idea to create a buffer and perhaps a class that takes care of it

When the process finished, the buffer is then written to disk

Page 97: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

CodeBuilder

class CodeBuilder ◦ Byte[] codeBuffer

◦ Int GetCodePosition()

◦ void AddInstruction(SMInstruction inst)

◦ void AddInstruction(SMInstruction inst, int nScope, int nArg)

◦ void WriteCode(String p_strPath)

Page 98: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

CodeGenerator

Class CodeGenerator

Implements Visitor

CodeGenerator(CodeBuilder cb)

Int GetCodeOffset()

Void Emit( SMInstruction inst, int nScope, int nArg)

Void Emit( SMInstruction inst, int nArg)

Void Emit( SMInstruction inst )

Page 99: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

CodeGenerator

Endianess

◦ CodeBuffer[Offset+2] = (nArg & 0xFF00)>>8

◦ CodeBuffer[Offset+3] = (nArg & 0xFF)

Page 100: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

CodeGenerator

Consider Literal

public Object visit(

ASTIntegerLiteral node,

SymbolTable data)

{

// LDC n

Emit( SMInstruction.LDC, 0,

(Integer)node.jjtGetValue() );

return SLType.Skip;

}

Page 101: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

CodeGenerator

Consider Identifier

public DataType visit(

ASTIdentifier node,

SymbolTable data)

{

//Get name from node

//Get Entry from symbolTable

//If Param …

//Else if Var …

Emit( SMInstruction.LD, …entry.Scope… , entry.Offset );

return DataType.Skip;

}

Page 102: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

CodeGenerator Consider Assignment

public DataType visit(

ASTAssignment node,

SymbolTable data )

{

name = get name from node 0

entry = data.get(name);

node.jjtGetChild(1).jjtAccept(this, data);

Emit( SMInstruction.STORE, entry.Scope,

entry.Offset);

return DataType.Skip;

}

Page 103: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

CodeGenerator

Consider While

◦ Can you identify a potential problem?

Location_of_Condition:

00 Code for Condition

01 JZ Out_Of_While

02 Code for While_Block

03 JMP Location_of_Condition

Out_Of_While:

04

Page 104: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

CodeGenerator Consider While

public Object visit(ASTWhileStatement node, SymbolTable data)

{

int addrCond = GetCodeOffset();

node.jjtGetChild(0).jjtAccept(this, data);

int addrJump = GetCodeOffset();

Emit(SMInstruction.JZ, 0, 0);

node.jjtGetChild(1).jjtAccept(this, data);

Emit(SMInstruction.JMP, 0, addrCond);

int addrEnd = m_codeBuilder.GetCodePos();

BackPatch(addrJump, addrEnd);

return SLType.Skip;

}

Page 105: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

CodeGenerator

BackPatch(

address of instruction, new address);

Modifies the address argument of an instruction

Page 106: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

CodeGenerator Consider While

public Object visit(ASTWhileStatement node, SymbolTable data)

{

int addrCond = GetCodeOffset();

node.jjtGetChild(0).jjtAccept(this, data);

int addrJump = GetCodeOffset();

Emit(SMInstruction.JZ, 0, 0);

node.jjtGetChild(1).jjtAccept(this, data);

Emit(SMInstruction.JMP, 0, addrCond);

int addrEnd = m_codeBuilder.GetCodePos();

BackPatch(addrJump, addrEnd);

return SLType.Skip;

}

Page 107: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

CodeGenerator Consider FunctionDecl

public DataType visit(ASTFunctionDecl node, SymbolTable data)

{

Get Entry from symboltable for node 0

Set symboltable Scope to that of entry;

entry.Offset = GetCodeOffset();

Emit(SMInstruction.ENTER, 0, entry.VarNum);

if(node.jjtGetNumChildren() == 4)

node.jjtGetChild(3).jjtAccept(this, data);

else

node.jjtGetChild(2).jjtAccept(this, data);

restore symboltable scope

return DataType.Skip;

}

Page 108: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

CodeGenerator

Consider Program

◦ Function Declarations are generated first

◦ Start code with a jmp instruction

◦ Enter a new scope

Page 109: Compiler Compiler Tutorial - Faculty of ICT Maltasspi3/CSA2201_Lecture-2012.pdf... [^cde] = not c,d,e Examples a+ - a, aa, aaa b* - , b, bb c? - , c a | b - a, b - a,b,c,d,e,f,g,…,y,z

Questions?

The End