Lecture 13 intermediate code generation 2.pptx

16
CSE 420 Intermediate Code Generation Lecture 13

Transcript of Lecture 13 intermediate code generation 2.pptx

Page 1: Lecture 13 intermediate code generation 2.pptx

CSE 420Intermediate Code Generation

Lecture 13

Page 2: Lecture 13 intermediate code generation 2.pptx

Conversions between primitive Conversions between primitive types in Javatypes in Java

Page 3: Lecture 13 intermediate code generation 2.pptx

Introducing type conversions into Introducing type conversions into expression evaluationexpression evaluation

Page 4: Lecture 13 intermediate code generation 2.pptx

Abstract syntax tree for the Abstract syntax tree for the function definitionfunction definitionfun length(x) = if null(x) then 0 else length(tl(x)+1)

This is a polymorphic function in ML language

Page 5: Lecture 13 intermediate code generation 2.pptx

Control FlowControl Flow

Boolean expressions are often used to:

•Alter the flow of control.•Compute logical values.

Page 6: Lecture 13 intermediate code generation 2.pptx

Short-Circuit Code

Page 7: Lecture 13 intermediate code generation 2.pptx

Flow-of-Control Flow-of-Control StatementsStatements

Page 8: Lecture 13 intermediate code generation 2.pptx

Syntax-directed definitionSyntax-directed definition

Page 9: Lecture 13 intermediate code generation 2.pptx

Generating three-address code for Generating three-address code for booleansbooleans

Page 10: Lecture 13 intermediate code generation 2.pptx

translation of a simple if-translation of a simple if-statementstatement

Page 11: Lecture 13 intermediate code generation 2.pptx

BackpatchingBackpatching• Previous codes for Boolean expressions insert

symbolic labels for jumps• It therefore needs a separate pass to set them to

appropriate addresses• We can use a technique named backpatching to avoid

this• We assume we save instructions into an array and

labels will be indices in the array• For non-terminal B we use two attributes B.truelist

and B.falselist together with following functions:▫ makelist(i): create a new list containing only I, an index into

the array of instructions▫ Merge(p1,p2): concatenates the lists pointed by p1 and p2 and

returns a pointer to the concatenated list▫ Backpatch(p,i): inserts i as the target label for each of the

instruction on the list pointed to by p

Page 12: Lecture 13 intermediate code generation 2.pptx

Backpatching for Boolean Backpatching for Boolean ExpressionsExpressions

• •

Page 13: Lecture 13 intermediate code generation 2.pptx

Backpatching for Boolean Backpatching for Boolean ExpressionsExpressions•Annotated parse tree for x < 100 || x > 200 &&

x ! = y

Page 14: Lecture 13 intermediate code generation 2.pptx

Flow-of-Control Flow-of-Control StatementsStatements

Page 15: Lecture 13 intermediate code generation 2.pptx

Translation of a switch-Translation of a switch-statementstatement

Page 16: Lecture 13 intermediate code generation 2.pptx

Any Question?