[ppt]powerpoint presentation - rowan university - personal...

29
Our Esteemed Colleagues Dr. John Schmalzel Dr. Shreekanth Mandayam Michael Sterner John Rotter Aditya Chaubal Compiler design Compiler design The Final Word, by…

Upload: buikhue

Post on 09-Mar-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Our Esteemed ColleaguesDr. John Schmalzel

Dr. Shreekanth Mandayam

Michael SternerJohn Rotter

Aditya Chaubal

Compiler designCompiler design

The Final Word, by…

Page 2: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Overview Overview • Compiler Front-End

• What is a compiler?• Lexical Analysis• Syntax Analysis• Parsing

• Compiler Back-End• Code Generation• Register Allocation• Optimization

• Specific Examples• lex• yacc• lcc

Page 3: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

What is a Compiler? What is a Compiler?

Example of tasks of compiler1. Add two numbers

2. Move numbers from one location to another3. Move information between CPU and memory

Software Translator

Page 4: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Lexical Analysis Lexical Analysis First phase of compiler

isolate words/tokens

Example of tokens:• key words – while, procedure, var, for,..• identifier – declared by the programmer• Operators – +, -, *, /, <>, …• Numeric – numbers such as 124, 12.35, 0.09E-23, etc.• Character constants • Special characters• Comments

Page 5: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Syntax Analysis Syntax Analysis

• What is Syntax Analysis?Second phase of the compilerAlso called Parser

• What is the Parsing Problem?

• How is the Parsing problem solved?Top-down and Bottom-up

algorithm

Page 6: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Top-Down Parsing Top-Down Parsing

What does it do?

One Method: Pushdown Machine

Example:Consider the simple grammar:

1. S 0 S 1 A2. S 1 0 A3. A 0 S 04. A 1

Page 7: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Example Example Process to construct a Pushdown

Machine1. Build a table with each column labeled by a

terminal symbol (and endmarker ) and each row labeled by a nonterminal or terminal symbol (and bottom marker )

2. For each grammar rule of the form A a, fill in the cell in row A and column a with with: REP(ra), retain, where r represents reversed

3. Fill in the cell in row a and column a with pop, advance, for each terminal symbol a.

4. Fill in the cell in row and column with Accept.

5. Fill in all other cells with Reject.6. Initialize the stack with and the starting

terminal.

Page 8: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Bottom-Up Parsing Bottom-Up Parsing

What does it do?

Two Basic Operations:1. Shift Operation2. Reduce Risk Operation

Page 9: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Why Split the Compiler Why Split the Compiler

• Front- End is Machine Independent

• Front-End can be written in a high level language

• Re-use Oriented Programming

• Back-End is Machine Dependent

• Lessens Time Required to Generate New Compilers

• Makes developing new programming languages simpler

Page 10: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Code Generation Code Generation

• Convert functions into simple instructions– Simple– Complex

• Addressing the operands– Base Register– Offset– Examples

Page 11: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Single Pass vs. Multiple pass Single Pass vs. Multiple pass • Single pass

– Creates a table of Jump Instructions– Forward Jump Locations are generated incompletely– Jump Addresses entered into a fix-up table along with the label they are jumping to– As label destinations encountered, it is entered into the table of labels– After all inputs are read, CG revisits all of these problematic jump instructions

• Multiple pass– No Fix-Up table– In the first pass through the inputs, CG does nothing but generate table of labels.– Since all labels are now defined, whenever a jump is encountered, all labels already have pre-defined memory location.– Possible problem: In first pass, CG needs to know how many MLI correspond to a label.– Major Drawback-Speed

Page 12: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Register Allocation Register Allocation

• Assign specific CPU registers for specific values• CG must maintain information on which registers:

– Are used for which purposes– Are available for reuse

• Main objective:– Maximize the utilization of the CPU registers– Minimize references to memory locations

• Possible uses for CPU registers– Values used many times in a program– Values that are computationally expensive

• Importance?– Efficiency– Speed

Page 13: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Example - For the following 2 statement program segment, determine a smart register allocation scheme:

A = B + C * D B = A – C * D

An Example An Example

Simple Register AllocationLOD (R1,C)MUL (R1,D)

STO (R1,Temp)LOD (R1,B)

ADD (R1,Temp)STO (R1,A)LOD (R1,C)

MUL (R1,D)

STO (R1,Temp2)

LOD (R1,A)

SUB (R1,Temp2)

STO (R1,B)

Net Result12 instructions and memory ref.

Smart Register AllocationLOD (R1,C)MUL (R1,D) C*DLOD (R2,B)

ADD (R2,R1) B+C*DSTO (R2,A)

SUB (R2,R1) A-C*DSTO (R2,B)

Net Result7 instruc. And 5 mem. refs.

Page 14: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

• RAA determines how many registers will be needed to evaluate an expression.

• Determines the Sequence in which sub-expressions should be evaluated to minimize register use

Register Allocation Algorithm Register Allocation Algorithm

Page 15: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

•Construct a tree starting at the bottom nodes•Assign each leaf node a weight of:

• 1 if it is the left child• 0 is it is the right child

• The weight of each parent node will be computed by the weights of the 2 children as follows:

• If the 2 children have different weights, take the max.• If the weights are the same, the parent’s weight is w+1

• The number of CPU registers is determined by the highest summed weight at any stage in the tree.

How does RAA work? How does RAA work?

Page 16: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Example of RAA Example of RAA

LOD (R1,c)ADD (R1,d) R1 = c +d

LOD (R2,e)ADD (R2,f) R2 = e + f

MUL (R1,R2) R1 = (c + d) * (e + f)

LOD (R2,a)MUL (R2,b) R2 = a * b

SUB (R2,R1) R2 = a * b - (c + d) * (e + f)

Example - For the following 2 statement program segment, determine a smart register allocation scheme:

A*B – (C+D) * (E+F)

Page 17: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Optimization Optimization

• Global– Directed Acyclic Graphs (DAGs)– Data Flow Analysis– Moving Loop Invariant Code– Other Mathematical Transformations

• Local– Load Store Optimization– Jump over Jump Optimization– Simple Algebraic Optimization

Page 18: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Analysis of specific compilers Analysis of specific compilers

Programs to be discussed:• lex – Programming utility that generates a lexical analyzer• yacc – Parser generator• lcc - ANSI C compiler

Platforms:• All three programs designed for use on Unix• lcc runs under DOS and Unix

Page 19: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

lex Programming Utility lex Programming Utility

General Information:• Input is stored in a file with *.l extension• File consists of three main sections• lex generates C function stored in lex.yy.c

Using lex:1) Specify words to be used as tokens (Extension

of regular expressions)2) Run the lex utility on the source file to

generate yylex( ), a C function3) Declares global variables char* yytext and int

yyleng

Page 20: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

lex Programming Utility lex Programming Utility

Three sections of a lex input file:

/* C declarations and #includes lex definitions */%{ #include “header.c”int i; }%%%/* lex patterns and actions */{INT} {sscanf (yytext, “%d”, &i);

printf(“INTEGER\n”);}%%/* C functions called by the above actions */{ yylex(): }

Page 21: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

yacc Parser Generator yacc Parser Generator

General Information:• Input is specification of a language• Output is a compiler for that language• yacc generates C function stored in y.tab.c• Public domain version available

Using yacc:1) Generates a C function called yyparse()2) yyparse() may include calls to yylex()3) Compile this function to obtain the compiler

bison

Page 22: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

yacc Parser Generator yacc Parser Generator yacc source

y.tab.c

a.out

yacc#include “lex.yy.c” lex.yy.c

lex sourcelex

cc

• Input source file – similar to lex input file• Declarations, Rules, Support routines• Four parts of output atom:(Operation, Left Operand, Right Operand, Result)

Page 23: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

lcc Compiler lcc Compiler General Information:• Retargetable ANSI C compiler (machine specific parts that are easy to replace)• Different stages of code:

1. Preprocessed code2. Tokens3. Trees4. DAG (directed acyclic graphs)5. Assembly language

Test program:int round(f) float f; {

return f+0.5;/* truncates the variable f */ }

Page 24: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

lcc Compiler lcc Compiler

Tokens ValuesINT inttypeID “round”‘(‘ID “f”‘)’FLOAT floattypeID “f”‘;’

Tokens Values‘{‘RETURNID “f”‘+’FCON 0.5‘;’‘}’EOI

Token Stream

Page 25: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

lcc Compiler lcc Compiler Syntax Trees

RET+IADDRF+P CVD+F

INDIR+D

“f” (Double)caller

ASGN+F

CVD+I

ADD+D

CVF+D CNST+D(0.5)

INDIR+F

ADDRF+P

“f” (float)callee

ADDRF+P

Page 26: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

lcc Compiler lcc Compiler

Register

Assembler Template

fld qword ptr %a[ebp] \nfstp dword ptr %a[ebp] \nfld dword ptr %a[ebp] \n#nop \nfadd qword ptr %a \nsub esp, 4 \nfistp dword ptr 0[esp] \n

eax pop %c \n#ret \n

Page 27: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Conclusion Conclusion

• Compiler Front-End

• Compiler Back-End

• Specific Examples

Page 28: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

References References

1. Fraser C., Hanson D. A Retargetable C Compiler: Design and Implementation. Addison-Wesley Publishing Company, 1995.

2. Bergmann S. Compiler Design: Theory, Tools, and Examples. WCB Publishers, 1994.

3. Aho A, Ullman J. The Theory of Parsing, Translation, and Compiling. Prentice-Hall, 1972.

Page 29: [PPT]PowerPoint Presentation - Rowan University - Personal …users.rowan.edu/.../fall01/comparch2/lectures/Compiler.ppt · Web viewA 1 Example Process to construct a Pushdown Machine

Questions Questions