lecture 15 control flow topics review positional encoding of booleans short circuit evaluation...
Post on 19-Dec-2015
223 views
TRANSCRIPT
Lecture 15 Control Flow
Lecture 15 Control Flow
Topics Topics Review Positional Encoding of Booleans Short Circuit Evaluation Control Flow Statements
Readings: 8.4, 8.6Readings: 8.4, 8.6
March 13, 2006
CSCE 531 Compiler Construction
– 2 – CSCE 531 Spring 2006
OverviewOverviewLast TimeLast Time
Evaluations of Expressions Numeric Implementation of Booleans Positional Encoding of Booleans Short Circuit Evaluation If-then-else semantic actions almost
Today’s Lecture Today’s Lecture Review
Booleans, quadruples
Control Flow using numeric implementation of Booleans Control Flow using
References: Sections 8.4, 8.5References: Sections 8.4, 8.5
Homework:Homework:
– 3 – CSCE 531 Spring 2006
Rest of the Semester PlanRest of the Semester Plan
Project 3 due Sunday March 19Project 3 due Sunday March 19
Project 4 – Booleans and Control Flow due Sunday Project 4 – Booleans and Control Flow due Sunday March 26March 26
Test 2 – April 5Test 2 – April 5
Project 5 – Function Calls Project 5 – Function Calls
Project 6 – Nested ScopesProject 6 – Nested Scopes
Exam April 27Exam April 27
– 4 – CSCE 531 Spring 2006
Recall Numeric Rep. of BooleansRecall Numeric Rep. of Booleans
E E id id11 relop id relop id22
{ E.place = newtemp();{ E.place = newtemp();
emit(if idemit(if id11.place relop.op id.place relop.op id22.place goto nextquad+3).place goto nextquad+3)
emit(E.place ‘:=‘ ‘0’);emit(E.place ‘:=‘ ‘0’);
emit(goto nextquad +2);emit(goto nextquad +2);
emit(E.place ‘:=‘ ‘1’);emit(E.place ‘:=‘ ‘1’);
}}
– 5 – CSCE 531 Spring 2006
AND, OR in numeric implementationAND, OR in numeric implementation
E E E E11 AND E AND E22 {{
E.place = newtemp();E.place = newtemp();
emit(E.place ‘:=’ Eemit(E.place ‘:=’ E11.place ‘and’ E.place ‘and’ E22.place);.place);
}}
E E E E11 OR E OR E22 {{
E.place = newtemp();E.place = newtemp();
emit(E.place ‘:=’ Eemit(E.place ‘:=’ E11.place ‘or’ E.place ‘or’ E22.place);.place);
}}
– 6 – CSCE 531 Spring 2006
Booleans Numeric Impl. – Finish UpBooleans Numeric Impl. – Finish Up
EE true true { E.place = newtemp();{ E.place = newtemp();
emit(E.place ‘:=’ ‘1’); }emit(E.place ‘:=’ ‘1’); }
EE false false { E.place = newtemp();{ E.place = newtemp();
emit(E.place ‘:=’ ‘0’); }emit(E.place ‘:=’ ‘0’); }
E E ( E ( E11 ) ) { E.place = E{ E.place = E11.place;}.place;}
– 7 – CSCE 531 Spring 2006
Positional Implmentation of BooleansPositional Implmentation of Booleans
AttributesAttributes
E.true - …E.true - …
E.falseE.false
FunctionsFunctions
makelist( quadnum ) –makelist( quadnum ) –
merge( list1, list2 ) –merge( list1, list2 ) –
backpatch (list, quadnum)backpatch (list, quadnum)
– 8 – CSCE 531 Spring 2006
BackpatchBackpatch
voidvoid
backpatch(QuadList p, int q)backpatch(QuadList p, int q)
{{
while (p != NULL){while (p != NULL){
target[p->quadnum] = (QuadListNode *) q;target[p->quadnum] = (QuadListNode *) q;
p = p -> link;p = p -> link;
}}
}}
– 9 – CSCE 531 Spring 2006
Intermediate Code Generation Intermediate Code Generation
voidvoid
gen(int op, struct nlist *p1, struct nlist *p2, struct nlist *r, int t)gen(int op, struct nlist *p1, struct nlist *p2, struct nlist *r, int t)
{{
opcode[nextquad] = op;opcode[nextquad] = op;
op1[nextquad] = p1;op1[nextquad] = p1;
op2[nextquad] = p2;op2[nextquad] = p2;
result[nextquad] = r;result[nextquad] = r;
branchTarget[nextquad] = t;branchTarget[nextquad] = t;
nextquad = nextquad + 1;nextquad = nextquad + 1;
}}
– 10 – CSCE 531 Spring 2006
Semantic Actions for B ID RELOP IDSemantic Actions for B ID RELOP ID
B: ID RELOP ID {B: ID RELOP ID {
gen($2, $1, $3, NULL, VOID);gen($2, $1, $3, NULL, VOID);
gen(GOTO, NULL,NULL,NULL,VOID);gen(GOTO, NULL,NULL,NULL,VOID);
$$.true = makelist(nextquad -2);$$.true = makelist(nextquad -2);
$$.false = makelist(nextquad - 1);$$.false = makelist(nextquad - 1);
}}
;;
– 11 – CSCE 531 Spring 2006
MarkersMarkers
Markers are typically nonterminals that derive Markers are typically nonterminals that derive εε that are that are inserted to insure an action is performed at a given inserted to insure an action is performed at a given time.time.
A common thing need is to remember the quad number A common thing need is to remember the quad number where something starts, so the attribute of a marker where something starts, so the attribute of a marker is just the next quad number.is just the next quad number.
M M εε { M.quad = nextquad; }{ M.quad = nextquad; }
So instead ofSo instead of
S S if B then S else S if B then S else S
We useWe use
S S if B then M if B then M11 S else M S else M22 S S ***Almost***Almost
– 12 – CSCE 531 Spring 2006
Semantic Actions for B B AND M B Semantic Actions for B B AND M B
B B B AND M B { B AND M B {
backpatch($1.true,$3);backpatch($1.true,$3);
$$.true = $4.true;$$.true = $4.true;
$$.false = merge($1.false,$4.false);$$.false = merge($1.false,$4.false);
}}
– 13 – CSCE 531 Spring 2006
Semantic Actions for B B OR M BSemantic Actions for B B OR M B
B B B OR M B { B OR M B {
backpatch($1.false,$3);backpatch($1.false,$3);
$$.false = $4.false;$$.false = $4.false;
$$.true = merge($1.true, $4.true);$$.true = merge($1.true, $4.true);
}}
– 15 – CSCE 531 Spring 2006
Semantic Actions for S if B then M S else M S ***Almost Semantic Actions for S if B then M S else M S ***Almost
S: IF B THEN M S S: IF B THEN M S NN ELSE M S { ELSE M S {
backpatch($2.true, $4);backpatch($2.true, $4);
backpatch($2.false, $8);backpatch($2.false, $8);
tmplist = merge($5, $6);tmplist = merge($5, $6);
$$ = merge(tmplist, $9);$$ = merge(tmplist, $9);
}}
;;
Why almost? Why almost?
N N εε { N.next = { N.next = makelist(makelist(nextquadnextquad));; gen(goto, _, _, _, void); } gen(goto, _, _, _, void); }
– 16 – CSCE 531 Spring 2006
Semantic Actions for Assignments Semantic Actions for Assignments
S: ID ASSIGNOP expr {S: ID ASSIGNOP expr {
gen(ASSIGNOP, $3, NULL, $1, VOID);gen(ASSIGNOP, $3, NULL, $1, VOID);
$$ = NULL;$$ = NULL;
}}
||
– 17 – CSCE 531 Spring 2006
Debugging Parsers written with YaccDebugging Parsers written with Yacc
1.1. Debug the grammarDebug the grammar1. Rewrite grammar to eliminate reduce/reduce and as many
shift/reduce as you can.
2. Tracing parses using –t option to bison or yacc -DYYDEBUG compile option int yydebug=1; in bison specification (C definitions section %{ ..%} extern int yydebug; in lex specification
2.2. Debug the semantic actionsDebug the semantic actions Compile with –g option; set CFLAGS=-g in Makefile and use
gcc $(CFLAGS) … as the compile (or rely on the builtin rules)
Use gdb (Gnu debugger) to debug the program
– 18 – CSCE 531 Spring 2006
Common MistakesCommon Mistakes
Segmentation fault - This means you have Segmentation fault - This means you have referenced a memory location that is outside of the referenced a memory location that is outside of the memory segment of your program.memory segment of your program. You have a pointer that has a bad value! First make sure everytime you copy a string value you use
strdup. Several people have had errors with strcat(s,t) where they did not allocate space for the string “s”.
Use gdb and bt (backtrace) to trace down the pointer with the bad value
– 19 – CSCE 531 Spring 2006
GDB - Essential CommandsGDB - Essential Commands
gdb program [core] - debug program gdb program [core] - debug program [using coredump core][using coredump core]
b [file:] function b [file:] function set breakpoint at function [in file]set breakpoint at function [in file]
run [arglist] run [arglist] start your program [with arglist]start your program [with arglist]
bt backtrace: bt backtrace: display program stackdisplay program stack
p expr p expr display the value of an expressiondisplay the value of an expression
c c continue running your programcontinue running your program
n n next line, stepping over function callsnext line, stepping over function calls
s s next line, stepping into function callsnext line, stepping into function calls
– 20 – CSCE 531 Spring 2006
Example using gdbExample using gdb
deneb> makedeneb> make
bison -d decaf.ybison -d decaf.y
decaf.y contains 51 shift/reduce conflicts.decaf.y contains 51 shift/reduce conflicts.
gcc -c -g decaf.tab.cgcc -c -g decaf.tab.c
flex decaf.lflex decaf.l
gcc -DYYDEBUG -g -c lex.yy.cgcc -DYYDEBUG -g -c lex.yy.c
gcc -c -g tree.cgcc -c -g tree.c
gcc -DYYDEBUG -g decaf.tab.o lex.yy.o tree.o -ly -o decafgcc -DYYDEBUG -g decaf.tab.o lex.yy.o tree.o -ly -o decaf
deneb> ./decaf < t1deneb> ./decaf < t1
Keyword intKeyword int
Segmentation Fault (core dumped) Segmentation Fault (core dumped) !!! !!!
– 21 – CSCE 531 Spring 2006
Example using gdbExample using gdb
deneb> makedeneb> make
bison -d decaf.ybison -d decaf.y
decaf.y contains 51 shift/reduce conflicts.decaf.y contains 51 shift/reduce conflicts.
gcc -c -g decaf.tab.cgcc -c -g decaf.tab.c
flex decaf.lflex decaf.l
gcc -DYYDEBUG -g -c lex.yy.cgcc -DYYDEBUG -g -c lex.yy.c
gcc -c -g tree.cgcc -c -g tree.c
gcc -DYYDEBUG -g decaf.tab.o lex.yy.o tree.o -ly -o decafgcc -DYYDEBUG -g decaf.tab.o lex.yy.o tree.o -ly -o decaf
deneb> ./decaf < t1deneb> ./decaf < t1
Keyword intKeyword int
Segmentation Fault (core dumped)Segmentation Fault (core dumped)
Note the use of the –g option (CFLAGS=-g in Makefile
– 23 – CSCE 531 Spring 2006
Attributes for Booleans and Control Flow*Attributes for Booleans and Control Flow*
*Assuming positional encoding.*Assuming positional encoding.
B (for boolean expression)B (for boolean expression) B.true – A list of …… that need to … B.false
Functions for Boolean Attributes Functions for Boolean Attributes int nextquad variable – Makelist (quad) Merge(l1, l2) Backpatch(List, target)
– 24 – CSCE 531 Spring 2006
MarkersMarkers
Markers – Grammar symbol that only derive Markers – Grammar symbol that only derive εε which are which are used perform some semantic action and usually to used perform some semantic action and usually to save some attribute valuesave some attribute value
Example S Example S A B M C D A B M C D In this case we want to perform some action when the
production M ε is reduced, like saving the starting place of the code the evaluates C
Markers for handling Booleans using the positional Markers for handling Booleans using the positional encodingencoding M ε (M.quad – marks the start of a piece of code) N ε (N.next – a list consisting of a single quad that
needs to have its target field filled in later.)
– 25 – CSCE 531 Spring 2006
Semantic Actions Control FlowSemantic Actions Control Flow
SS LHS ASSIGNOP E ‘;’LHS ASSIGNOP E ‘;’
|| if-statementif-statement
|| if-else-statementif-else-statement
|| while-statementwhile-statement
|| for-statementfor-statement
|| BEGIN L END BEGIN L END
|| function-calls ???function-calls ??? /* this is not really what /* this is not really what we mean by control flow we mean by control flow we will do this later */we will do this later */
;;
LL L SL S
|| SS
;;
Now what attributes do we need Now what attributes do we need for these nonterminals? (S, L)for these nonterminals? (S, L)
– 26 – CSCE 531 Spring 2006
Semantic Actions Statement ListsSemantic Actions Statement Lists
LL L SL S
|| SS
;;
As an example considerAs an example consider
while (B-expr){while (B-expr){
S1S1
S2S2
S3S3
}}
Parse TreeParse Tree
SS
while ‘(‘ B ‘)’ DO S ENDwhile ‘(‘ B ‘)’ DO S END
Now what attributes do we need Now what attributes do we need for these nonterminals? (S, L)for these nonterminals? (S, L)
– 27 – CSCE 531 Spring 2006
Semantic Actions for S if B then M S N else M S Semantic Actions for S if B then M S N else M S
S: IF B THEN M S S: IF B THEN M S NN ELSE M S { ELSE M S {
backpatch($2.true, $4);backpatch($2.true, $4);
backpatch($2.false, $8);backpatch($2.false, $8);
tmplist = merge($5, $6);tmplist = merge($5, $6);
$$ = merge(tmplist, $9);$$ = merge(tmplist, $9);
}}
;;
N N εε { N.next = nextquad;{ N.next = nextquad; gen(goto, _, _, _, void); } gen(goto, _, _, _, void); }
– 28 – CSCE 531 Spring 2006
Semantic Actions for S if B then M S Semantic Actions for S if B then M S
S: IF B THEN M S {S: IF B THEN M S {
backpatch($2.true, $4);backpatch($2.true, $4);
$$ = merge($2.false, $5);$$ = merge($2.false, $5);
}}
;;
– 31 – CSCE 531 Spring 2006
Project 4 – Generating Postfix Code for Expressions and Control FlowProject 4 – Generating Postfix Code for Expressions and Control Flow
BooleansBooleans
If B then assign else assignIf B then assign else assign
Undeclared variables print error message including line Undeclared variables print error message including line numbernumber
Write up in the email soonWrite up in the email soon