Homework #3 - Compiler Construction - 2008 | EECS 665, Assignments of Electrical and Electronics Engineering

Material Type: Assignment; Professor: Kulkarni; Class: Compiler Construction; Subject: Elect Engr & Computer Science; University: University of Kansas; Term: Fall 2008;

Typology: Assignments

Pre 2010

Uploaded on 03/10/2009

koofers-user-lk3-2
koofers-user-lk3-2 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
EECS 665 - Fall 2008
Assignment 3
csem
csem reads a C program (actually a subset of C) from its standard input and compiles it into
a list of intermediate language quadruples on its standard output. The form of the quadruple
operators appear below:
x := y op z operate on yand zand place result in x
bt x lab branch to lab iff xis true
br lab branch to lab
x:= global name yield address of global identifier name
x:= local nyield address of local n
x:= param nyield address of parameter n
x:= cyield value of constant value c
x:= syield address of character string s
formal nallocate the formal having nbytes
alloc name n allocate the global name having nbytes
localloc nallocate the local having nbytes
func name begin function name
fend end function
lab=y define lab to be y
bgnstmt nbeginning of statement at line n
name denotes an identifier from the C program. ndenotes an integer. cdenotes a C integer
constant. sdenotes a string enclosed by double quotes. x,y, and zdenote quadruple temporaries.
lab denotes the location of a quadruple or a reference to a symbol defined later by a ”lab=y”
command. op denotes any of the C operators below:
== ! = <=>=
operate on xand y< > =| <<
>> + /%
invert x
negate x
@dereference x
cv convert x
fcall function ywith narguments
arg pass xas an argument
ret return x
[ ] index zinto y
followed by i(for the integer version of the operator) or by f(for the floating point version).
yis omitted for unary operators. You should assume all bitwise operators (| &<<>>) and %
only operate on integer values.
pf3

Partial preview of the text

Download Homework #3 - Compiler Construction - 2008 | EECS 665 and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

EECS 665 - Fall 2008

Assignment 3

csem

csem reads a C program (actually a subset of C) from its standard input and compiles it into a list of intermediate language quadruples on its standard output. The form of the quadruple operators appear below:

x := y op z operate on y and z and place result in x bt x lab branch to lab iff x is true br lab branch to lab x := global name yield address of global identifier name x := local n yield address of local n x := param n yield address of parameter n x := c yield value of constant value c x := s yield address of character string s formal n allocate the formal having n bytes alloc name n allocate the global name having n bytes localloc n allocate the local having n bytes func name begin function name fend end function lab=y define lab to be y bgnstmt n beginning of statement at line n

name denotes an identifier from the C program. n denotes an integer. c denotes a C integer constant. s denotes a string enclosed by double quotes. x, y, and z denote quadruple temporaries. lab denotes the location of a quadruple or a reference to a symbol defined later by a ”lab=y” command. op denotes any of the C operators below:

==! = <= >= < > = | ∧ << operate on x and y

  • − ∗ / % ∼ invert x − negate x @ dereference x cv convert x f call function y with n arguments arg pass x as an argument ret return x [ ] index z into y

followed by i (for the integer version of the operator) or by f (for the floating point version). y is omitted for unary operators. You should assume all bitwise operators (| ∧& <<>>∼) and % only operate on integer values.

For example,

double m[6];

scale(double x) { int i;

if (x == 0) return 0; for (i = 0; i < 6; i += 1) m[i] *= x; return 1; }

compiles into the intermediate operations below (actually only one column)

alloc m 48 t7 := local 0 t19 := local 0 func scale t8 := 0 t20 := @i t formal 8 t9 := t7 =i t8 t21 := global m localloc 4 label L3 t22 := t21 []f t bgnstmt 6 t10 := local 0 t23 := param 0 t1 := param 0 t11 := @i t10 t24 := @f t t2 := @f t1 t12 := 6 t25 := @f t t3 := 0 t13 := t11 <i t12 t26 := t25 *f t t4 := cvf t3 bt t13 B3 t27 := t22 =f t t5 := t2 ==f t4 br B4 br B bt t5 B1 label L4 label L br B2 t14 := local 0 B3=L label L1 t15 := 1 B4=L bgnstmt 7 t16 := @i t14 B5=L t6 := 0 t17 := t16 +i t15 B6=L reti t6 t18 := t14 =i t17 bgnstmt 10 label L2 br B5 t28 := 1 B1=L1 label L5 reti t B2=L2 bgnstmt 9 fend bgnstmt 8

Your assignment is to write the semantic actions for the csem program to produce the de- sired intermediate code. The following files which will comprise part of your program should be downloaded from the class web-page: http://www.ittc.ku.edu/∼kulkarni/teaching/eecs665/

cc.h - include file cgram.y - yacc grammar for subset of C makefile - csem makefile scan.c - lexical analyzer scan.h - defines prototypes for routines in scan.c sem.h - defines prototypes for routines in sem.c semutil.c - utitity routines for the semantic actions semutil.h - defines prototypes for routines in semutil.c sym.c - symbol table management sym.h - defines prototypes for routines in sym.c

The makefile will create an executable called csem in the current directory. You should copy the semdum.c file into your directory as sem.c. This file contains stubs for the semantic action