• No results found

Plan for this week. Disclaimer

N/A
N/A
Protected

Academic year: 2021

Share "Plan for this week. Disclaimer"

Copied!
16
0
0

Loading.... (view fulltext now)

Full text

(1)

1

Introduction (chapter 1)

Plan for this week

• Whirlwind trip through a compiler - Simple expression language – Parsing – Typechecking – Translation to IR code • raw • linearized • trace-scheduled – X86_64 code generation • flow graph • interference graph • register allocation 1 Introduction (chapter 1)

Disclaimer

• This is just one of many ways to build a compiler

• Other ways include:

– stream-based

• Many small compilers, each takes as input a representation of the program and produces as output a modified

representation

• The output of the final compiler is the translated program – parse-tree based

• Many passes over a shared data structure • The last pass produces the translated program

• Our version is a bit of a hybrid

(2)

Introduction (chapter 1)

Our version

• Overall, stream based with two components

– Front end

• Builds an abstract syntax tree

• Has visitor passes (3 or so) that “decorate” the tree • Produces intermediate code as its output

– Back end

• Takes in the intermediate code data structure • Performs a number of analyses

• Produces target machine code

3

Expression language

Expression is a very simple calculator-like language.

An example program:

// This is a comment. zero = 0;

one = 1;

two = one + zero; three = two + one;

(3)

3

Introduction (chapter 1) 5

Example: Syntax of “Expression”

Program ::= Statements print Expression Statement ::= Identifier = Expression ; Statements ::= Statement | Statements Statement ... Introduction (chapter 1) 6

Example: Syntax of “Expression” (continued)

Expression ::= primary-Expression

(4)

Introduction (chapter 1) 7

Example: Syntax of “Expression” (continued)

Comment ::= // CommentLine eol

CommentLine ::= Graphic | CommentLine Graphic Graphic ::= any printable character or space

Example programs

(5)

5

Introduction (chapter 1)

Scanning and parsing using Racket

• Look at expression.rkt

• The scanner is scan-string

– It takes a list of characters and produces a list of tokens

• The parser is parse-tokens

– It takes a list of tokens and produces a parse tree

– Note the 1-to-1 correspondence between a list of tokens and a parse tree

• parse-tree->tokens gives back the tokens from the parse tree

9

Introduction (chapter 1)

Expression’s parser using Java

• Specified using JavaCC (Google is your friend)

– Look at ExpressionsParser.jj in parser.jcc

(6)

Introduction (chapter 1) 11

Example: Concrete Syntax of Expressions (recap)

Expression

::= primary-Expression

| Expression Operator primary-Expression | Expression ? Expression : Expression primary-Expression

::= Literal | Identifier

| ! primary-Expression | ( Expression )

Example: Abstract Syntax of Expressions

Expression

::= Literal IntegerExp

| Identifier IDExp

| ! Expression UnaryExp

| Expression Op Expression BinaryExp

(7)

7

Introduction (chapter 1) 13

Abstract Syntax Trees

Abstract Syntax Tree for: d=(c+10)*n

BinaryExpression BinaryExpression IDExp c + Op 10 * Op IntegerExp IDExp n Statement d IDExp Introduction (chapter 1)

Parsing Expression programs

• Do:

– run parse sample/cond.exp – run parse sample/exp.exp

(8)

Introduction (chapter 1) 15

Contextual Constraints

Syntax rules alone are not enough to specify the format of

well-formed programs.

Example 1: m = 2; print m + x Example 2: m = 2; n = m < 4; print n+1

Undefined!

Scope Rules

Type error!

Type Rules

Day 2

• Mention that the comparison between Java and Racket

was completely unfair as pointed out by a student

– Racket is hand coded

– JavaCC is a DSL (Domain Specific Language)

(9)

9

Introduction (chapter 1) 17

Scope Rules

Scope rules regulate visibility of identifiers. They relate

every applied occurrence of an identifier to a binding

occurrence

Example 1 m = 2; r = 10 * m; print r Terminology:

Static binding vs. dynamic binding

Example 2: m = 2;

print m + x

Introduction (chapter 1)

Checking scope rules

• Do:

– run type sample/badscope.exp

(10)

Introduction (chapter 1) 19

Type Rules

Type rules regulate the expected types of arguments and

types of returned values for the operations of a language.

Terminology:

Static typing vs. dynamic typing

Type rule of <:

E1 < E2is type correct and of type Boolean if E1and E2are type correct and of type Integer

Type rule of E ? V1 : V2

E ? V1 : V2 is type correct and of the same type as V1

if Eis of type Boolean and V1 and V2are type correct and have the same type

Checking type rules

• Do:

(11)

11

Introduction (chapter 1)

Checking Expression programs

• Look at the implementation:

– In package typechecker.implementation • BuildSymbolTableVisitor • TypeCheckVisitor 21 Introduction (chapter 1) 22

Semantics

Specification of semantics is concerned with specifying the

“meaning” of well-formed programs.

Terminology:

Expressions are evaluated and yield values (and may or may not perform side effects)

Statements are executed and perform side effects. Declarations are elaborated to produce bindings Side effects:

(12)

Introduction (chapter 1) 23

Semantics

Example: The (informally specified) semantics of statements in Expression.

Statements are executed to define constants and perform output. The statement C = E; is executed as follows:

first the expression E is evaluated to yield a valuev

then v is assigned to the constant named C

The sequential statement S1 S2 is executed as follows: first the statement S1 is executed

then the statement S2 is executed etc.

Semantics

Example: The semantics of expressions.

An expression is evaluated to yield a value.

A (literal expression) L yields the integer value of L

The (constant name) expression C yields the value of the constant named C

(13)

13

Introduction (chapter 1)

Semantics

• The ultimate semantics processing in a compiler is

translation

– Generate code in the target language that is equivalent in every respect to the program in the source language

• We do this in several steps

– Translation to intermediate code – Code improvement (optimization) – Instruction selection

– Register allocation

– Code improvement (optimization)

25

Introduction (chapter 1)

Expression translation to IR

• Look at the implementation:

– in package translate.implementation

• TranslateVisitor

• Do:

– run ir sample/exp.exp – run ir sample/cond.exp

• IR has four forms:

– raw

– linear (canonical) – basic blocks – trace scheduled

(14)

Introduction (chapter 1)

Optimization (Or more precisely:

anti-pessimization)

• A bunch of analysis needs to be done first

– flow graph

– liveness

• Then we can perform some correctness-preserving

transformations that will improve the code

• I’m not going to show you the code L

• Do:

– run iropt sample/exp.exp

27

Instruction selection

• Maximal munch (greedy tree matching)

• Express patterns corresponding to machine instructions

• Match the patterns against the (linear) IR

• Works from the leaves up

• Look at the code: (yuck)

(15)

15

Introduction (chapter 1)

Register allocation

• A bunch of analysis needs to be done first

– flow graph

– liveness – interference

• Then we can select registers for every temp

• I’m not going to show you the code L

• Do:

– run flow sample/exp.exp & show the graph (show flow*.dot) – run live sample/exp.exp & show the graph (show live*.dot) – run interf sample/exp.exp & show the graph (first)

– run reg sample/exp.exp & show the graph (final)

29

Introduction (chapter 1)

And then we’re all done

• Well, almost

– We need to assemble the resulting assembly code • use gcc to compile exp.s

– We need a bit of runtime support • getting the program running (main) • implementing println

• for a bigger language, other “builtin” functionality – allocating/freeing objects

– supporting builtin types

(16)

Introduction (chapter 1)

And then we’re all done ...

• Do:

– run final sample/exp.exp – gcc –c sample/exp.s

– gcc –o exp.exe exp.o runtime/runtime.o – ./exp.exe

– otool –t –v exp.exe

• Ta Da!!

References

Related documents

• curator of the UK’s largest collection of digital data in the social sciences • currently holds nearly 6,000 data collections for research and teaching, both5. quantitative

A while statement evaluates a Boolean expression, just like the if p , j statement, and executes a statement (or block of statements) if the expression is true. while (

IF the Conditional Expression is true THEN do the statement, or compound statements that follows.. 3.4 The census

Keywords: uncertainty quantification; stochastic partial differential equations; elastic wave equation; regularity; collocation method; error analysis.. AMS Subject

In the following SQL IF Statement, it evaluates the expression, and if the condition is true, then it executes the statement mentioned in IF block otherwise statements within

The Japanese Super-Kamiokande (SK) is a Water Cherenkov detector located 1000 m (2700 m w.e.) underground and since fifteen years it plays a fundamental role in most neutrino

Inspection of international study centres seeking specific accreditation will include observation of the teaching of subjects other than English, discussion with relevant staff

As a whole, it was found that internal linkage, external and commercial linkage and linkage with public research institutions, government agencies and association, explain to