CSCE 531 Compiler Construction Lecture 16 Boolean Expressions

CSCE 531 Compiler Construction Lecture 16 Boolean Expressions and Control Flow Topics n Numeric Implementation of Booleans n Positional Encoding of Booleans Short Circuit Evaluation Debugging No class Thursday, we will reschedule! n n n Readings: 5. 7, 6. 3, 7. 4 March 27, 2018

Overview Last Time n n Attribute Grammars Type Systems ILOC Symbol Table Last Time Didn’t Finish n n Pascal like declarations Hierarchical Symbol Tables Today’s Lecture n n Numeric Implementation of Booleans Positional Encoding of Booleans Short Circuit Evaluation Debugging YACC generated parsers References: Sections 7. 4 Homework: – 2– CSCE 531 Spring 2018

Pascal like declarations What if the language gets it wrong? Pascal x, y, z : integer; Grammar? – 3– CSCE 531 Spring 2018

Hierarchical Symbol Tables From last time figure 5. 12 page 246 – 4– CSCE 531 Spring 2018

Boolean Expression Grammar Bool. Expr not Or. Term | Or. Term OR And. Term | And. Term AND Bool | Bool Rel. Expr | true | false Rel. Expr Rel. Op Expr + Expr | Expr * Expr | ( Expr ) | ID | NUM – 5– CSCE 531 Spring 2018

Numeric Encoding True = non-zero False = zero Example B OR C AND NOT D Quadruples NOT r. D _ rt 1 AND r. C rt 1 rt 2 OR rt 2 rt 3 – 6– r. B CSCE 531 Spring 2018

Numeric Encod. Extended to Comparisons Comparison operations in Hardware n IA 32 l CC register set as result of arithmetic operations » Add, Subtract, … CMP = subtract without saving result l Jumps then test certain bits in the CC register » JLT, JLE, JEQ, JGE, JGT, JNEQ l So encoding includes a lot of Jumps l (x < y) AND (y < z) OR (y=x) » L 0 cmp x y _ » _ JLT L 2 » L 1 Load. I 0 T 1 » _ JMP L 3 » L 2 Load. I 1 _ T 1 » L 3 NOP – 7– CSCE 531 Spring 2018

Example: (x < y) AND (y < z) Label Op CMP A 1 A 2 Target X Y _ JLT LOADI L 2 0 T 1 JMP L 3 L 2 LOADI 1 L 3 CMP Y T 1 Z JLT LOADI L 4 0 T 2 JMP – 8– L 4 LOADI L 5 AND _ L 3 1 T 2 T 3 CSCE 531 Spring 2018

Positional Encoding In Positional Encoding we represent the value of an expression by where (the position) you end up in the code Example while(k < 20) { sum = sum + k*k; k = k + 1; } Note in the code on the right the value of the boolean expr k<20 is never explicitly represented other than in the Condition Code Register (CC) The value is represented by you end up at quad 7 – 9 whether – or quad 3 Quad Op S 1 S 2 T 1 CMPI k 20 _ 2 JGE ? 7 3 MULT k 4 ADD sum T 1 sum 5 ADDI k k 6 JMP k 1 T 1 1 7 … CSCE 531 Spring 2018

Attributes for Booleans Consider the example on the next slide As we generate the code we don’t know what the target branches should be. We need to build lists of quads whose target fields need to be filled in later E. True E. false – 10 – CSCE 531 Spring 2018

Example: (x < y) AND (y < z) Quad. Num 1 Op CMP A 1 A 2 Target X Y _ 2 3 4 5 6 7 8 9 10 11 – CSCE 531 Spring 2018

Functions for Boolean Attributes int nextquad variable – Makelist (quad) Merge(l 1, l 2) Backpatch(List, target) – 12 – CSCE 531 Spring 2018

/class/csce 531 -001/web/Examples/Booleans Don’t use this make or lex file as example. Focus on boolean. y – 13 – CSCE 531 Spring 2018

%{ #include <ctype. h> #include <stdio. h> #define YYDEBUG 1 #define ADDOP 401 #define MULTOP 402 #define GOTO 407 /* $Header: gram, v 1. 1 84/12/07 12: 01 matthews Exp $ */ #define YYDEBUG 1 extern char *yytext; char *strsave(); char *newtemp(); typedef struct node{ int quadnum; struct node *link; } *LIST, LISTNODE; – 14 – CSCE 531 Spring 2018
![#define CODESIZE 1000 int opcode[CODESIZE]; char *op 1[CODESIZE], *op 2[CODESIZE], *target[CODESIZE]; char *VOID = #define CODESIZE 1000 int opcode[CODESIZE]; char *op 1[CODESIZE], *op 2[CODESIZE], *target[CODESIZE]; char *VOID =](http://slidetodoc.com/presentation_image_h2/5e5c2cf1ae9515d6f53a3038ca470e48/image-15.jpg)
#define CODESIZE 1000 int opcode[CODESIZE]; char *op 1[CODESIZE], *op 2[CODESIZE], *target[CODESIZE]; char *VOID = "VOID"; LIST tmplist; int nextquad = 0; %} %union{ char *place; struct { LIST *true; LIST *false; } list; int quad; int type; LIST next; } – 15 – CSCE 531 Spring 2018

B: ID RELOP ID { gen($2, $1, $3, VOID); gen(GOTO, VOID, VOID); $$. true = makelist(nextquad -2); $$. false = makelist(nextquad - 1); } ; – 16 – CSCE 531 Spring 2018

| B AND M B { backpatch($1. true, $3); $$. true = $4. true; $$. false = merge($1. false, $4. false); } | B OR M B { backpatch($1. false, $3); $$. false = $4. false; $$. true = merge($1. true, $4. true); } – 17 – CSCE 531 Spring 2018

S: ID ASSIGNOP expr { gen(ASSIGNOP, $<place>3, VOID, $1); $$ = NULL; } | IF B THEN M S N ELSE M S { backpatch($2. true, $4); backpatch($2. false, $8); tmplist = merge($5, $6); $$ = merge(tmplist, $9); } ; M: {$$ = nextquad; } ; N: {gen(GOTO, VOID, VOID); $$ = makelist(nextquad - 1); } – 18 – CSCE 531 Spring 2018

– 19 – CSCE 531 Spring 2018

Debugging Parsers written with Yacc 1. Debug the grammar 1. 2. Rewrite grammar to eliminate reduce/reduce and as many shift/reduce as you can. Tracing parses using l –t option to bison or yacc l -DYYDEBUG compile option l Int yydebug=1; in lex specification (C definitions section %{. . %} l Extern int yydebug; in yacc specification 2. Debug the semantic actions n n – 20 – 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 CSCE 531 Spring 2018

Parsing Traces with yydebug=1 Setting up for traces l –t option to bison or yacc l -DYYDEBUG compile option l int yydebug=1; in lex specification n C definitions section %{ … %} l extern int yydebug; in yacc specification l Generate. output file and print out so you can follow along – 21 – CSCE 531 Spring 2018

deneb>. /expr Starting parse Entering state 0 Reading a token: 23+34*3; Next token is 262 (INT) Shifting token 262 (INT), Entering state 2 Reducing via rule 6 (line 28), INT -> expr INT=23 state stack now 0 Entering state 4 Reading a token: Next token is 260 (PLUS) Shifting token 260 (PLUS), Entering state 8 Reading a token: Next token is 262 (INT) Shifting token 262 (INT), Entering state 2 Reducing via rule 6 (line 28), INT -> expr INT=34 state stack now 0 4 8 Entering state 13 Reading a token: Next token is 259 (TIMES) Shifting token 259 (TIMES), Entering state 7 Reading a token: Next token is 262 (INT) token 262 (INT), Entering state 2 – Shifting 22 – CSCE 531 Spring 2018

Common Mistakes l Segmentation fault - This means you have referenced a memory location that is outside of the memory segment of your program. n n n – 23 – 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 CSCE 531 Spring 2018
![GDB - Essential Commands gdb program [core] - debug program [using coredump core] b GDB - Essential Commands gdb program [core] - debug program [using coredump core] b](http://slidetodoc.com/presentation_image_h2/5e5c2cf1ae9515d6f53a3038ca470e48/image-24.jpg)
GDB - Essential Commands gdb program [core] - debug program [using coredump core] b [file: ] function set breakpoint at function [in file] run [arglist] start your program [with arglist] bt backtrace: display program stack p expr display the value of an expression c continue running your program n next line, stepping over function calls s next line, stepping into function calls – 24 – CSCE 531 Spring 2018

Example using gdb deneb> make bison -d decaf. y contains 51 shift/reduce conflicts. gcc -c -g decaf. tab. c flex decaf. l gcc -DYYDEBUG -g -c lex. yy. c gcc -c -g tree. c gcc -DYYDEBUG -g decaf. tab. o lex. yy. o tree. o -ly -o decaf deneb>. /decaf < t 1 Keyword int Segmentation Fault (core dumped) !!! – 25 – CSCE 531 Spring 2018

Example using gdb deneb> make Note the use of the –g option (CFLAGS=-g in Makefile bison -d decaf. y contains 51 shift/reduce conflicts. gcc -c -g decaf. tab. c flex decaf. l gcc -DYYDEBUG -g -c lex. yy. c gcc -c -g tree. c gcc -DYYDEBUG -g decaf. tab. o lex. yy. o tree. o -ly -o decaf deneb>. /decaf < t 1 Keyword int Segmentation Fault (core dumped) – 26 – CSCE 531 Spring 2018

Getting in and out of GDB deneb> gdb decaf GNU gdb 4. 18 Copyright 1998 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, … (gdb) run < t 1 Starting program: /class/csce 531 -001/submissions/huang 27/review 01/decaf < t 1 before treeprint demo=Program info=X demo= info= Program received signal SIGSEGV, Segmentation fault. 0 xff 2 b 3474 in strlen () from /usr/libc. so. 1 (gdb) quit The program is running. Exit anyway? (y or n) y deneb> – 27 – CSCE 531 Spring 2018

Backtrace(bt) To see the Activation Stack (gdb) run < t 1 Starting program: /class/csce 531 -001/submissions/huang 27/review 01/decaf < t 1 before treeprint demo=Program info=X demo= info= Program received signal SIGSEGV, Segmentation fault. 0 xff 2 b 3474 in strlen () from /usr/libc. so. 1 (gdb) bt #0 #1 #2 #3 #4 #5 #6 #7 0 xff 2 b 3474 in strlen () from /usr/libc. so. 1 0 xff 305798 in _doprnt () from /usr/libc. so. 1 0 xff 3072 cc in printf () from /usr/libc. so. 1 0 x 199 e 8 in treeprint (p=0 x 32718) at tree. c: 9 0 x 19 a 0 c in treeprint (p=0 x 32738) at tree. c: 11 0 x 19 a 0 c in treeprint (p=0 x 32748) at tree. c: 11 0 x 11414 in yyparse () at decaf. y: 91 0 xff 3804 e 0 in main () from /usr/liby. so. 1 – 28 – CSCE 531 Spring 2018

Printing values (gdb) up #1 0 xff 305798 in _doprnt () from /usr/libc. so. 1 (gdb) up #2 0 xff 3072 cc in printf () from /usr/libc. so. 1 (gdb) up #3 0 x 199 e 8 in treeprint (p=0 x 32718) at tree. c: 9 9 printf("demo=%s info=%sn", p->demo, p->info); (gdb) print *p $1 = {tag = VDECL, child = 0 x 326 e 8, next = 0 x 1, info = 0 x 1 adf 8 "", tagname = 0 x 7 <Address 0 x 7 out of bounds>, demo = 0 x 0, value = {i = 1, d = 2. 122050236999444 e-314, s = 0 x 1 <Address 0 x 1 out of bounds>}} (gdb) – 29 – CSCE 531 Spring 2018

Project Two and Three Due Tonight – dropbox project two n n Test cases using yourlogin name followed by a numeral as the names, e. g. , matthews 1, matthews 2, matthews 3 Your grammar file without necessarily having any semantic actions Your Lex, Makefile (not necessarily any routines for semantic actions) We will create a Test. Dir with all of these files that you can get to. Note I will not vouch for the validity of any test program. Project Three (the old project two) n n Due Sunday Night Note I will be at an ABET meeting the rest of the week! l I will try to read my email, but no promises! n – 30 – No class Thursday, we will reschedule! CSCE 531 Spring 2018

Test 2 – 31 – CSCE 531 Spring 2018
- Slides: 31