CS 515: Programming Languages and Compilers I
Fall 2007, Project 1
An Optimizing Compiler for A Small Imperative Language
Due date: Thursday, October 25, at 11:59pm EST


Modifications and Clarifications

Project Description: PART I

THIS IS NOT A GROUP PROJECT! Every student is expected to work on his/her own project. You may discuss overall design issues with your fellow students. Detailed discussions and/or code sharing is not allowed. The general rules for academic integrity apply.

Write a SDT (syntax directed translation) scheme to generate code for a simple imperative language shown below. The language does not contain any procedures, but only a single main program. Base types are limited to integer only. Arrays are one-dimensional or two-dimensional with the integer type as its component and index type. The following statements are included: for-do, it-then, if-then-else, assignment, write, and compound statement. Operators are restricted to arithmetic and relational. The grammar that we are using for this language is as follows:

start ::= program ID ; block .
block ::= variables cmpdstmt
variables ::= var vardcls | empty string
vardcls ::= vardcls vardcl ; | vardcl ;
vardcl ::= IDlist : type
type ::= integer | array[ ICONST ] of integer | array[ ICONST, ICONST ] of integer
IDlist ::= IDlist , ID | ID
stmtlist ::= stmtlist ; stmt | stmt
stmt ::= ifstmt | fstmt | astmt | writestmt | cmpdstmt
cmpdstmt ::= begin stmtlist end
writestmt ::= writeln ( exp )
ifstmt ::= ifhead then stmt else stmt | ifhead then stmt
ifhead ::= if condexp
fstmt ::= for ctrlexp do stmt
ctrlexp ::= ID := ICONST, ICONST
astmt ::= lhs := exp
lhs ::= ID | ID [ exp ] | ID [ exp, exp ]
exp ::= exp + exp | exp - exp | exp * exp | ID | ID [ exp ] | ID [ exp, exp ] | ICONST
condexp ::= exp != exp | exp == exp | exp < exp | exp <= exp


You may assume that the program is correct in terms of static semantics, i.e., no semantic analysis (type checking) is required.

You will write a syntax directed translation scheme that will generate ILOC code for the above language. You may test the correctness of your generated ILOC code by running it on the ILOC simulator sim provided in directory ~uli/cs515/projects/proj1/ILOCsimulator/src on the ilab machines . This directory also contains the source code of the ILOC simulator.

Code Shape Requirements

Project Description: PART II

Implement an optimization pass that performs local common subexpression elimination (CSE) at the ILOC instruction level , not the source level. Local CSE typically works on basic blocks. You are required to handle CSE detection for single source level statements, such as e=a+b*c+b*c or sum=a[i,j}+a{i,j].

You will receive extra credit if you extend CSE to individual basic blocks. You may generate a separate data structure to store the (unoptimized) instructions generated for a statement (or basic block), or you can perform CSE "on the fly", i.e., generate optimized code using syntax-directed translation.

HINT: If you use new virtual registers for every value that is computed in ILOC, you may use these virtual registers as value numbers , i.e., unique identifiers of computed values.

The optimizer should be invoked using the -O option, i.e., execute ./codegen -O < demo will run your optimizing compiler on the demo input file. The sample solution has been extended to include a statement-level CSE optimizer. The solution does not consider any arithmetic properties during CSE detection such as the commutativity of operands "+ and "*".


How To Get Started

The following code is provided as a starting point for your project. You can download the files below as a gzipped tar-ball by clicking here, or by copying the files from the directory ~uli/cs515/projects/proj1 on the ilab machines. You can also click on the links below and copy the files one at a time.
  1. Scanner: scan.l (flex) *** DO NOT MODIFY ***
  2. Parser/Optimizer/Code Generator: parse.y (bison). Here is where most of your code will go. It contains an example of how to use procedure emit to generate code. You will need to remove this in your final version, i.e., it has only be inserted as an illustration example.
  3. attr.h and attr.c . You will need to define new attribute(s)
  4. symtab.h and symtab.c . Needs to be modified.
  5. instrutil.h and instrutil.c .
  6. Makefile
For the CSE optimizer, you may want to add additional files. You need to make appropriate changes in the Makefile.

In order to get started on testing your compiler, you can use the following test cases. This is just a tentative list or source codes and their generated sample ILOC code using our code generator sample solution (sampleCodegen). We will use many more test cases to grade your project . There are many ways of generating correct code, so our sampleCodegen compiler gives you only an overall idea what needs to be done. The sample code generator does not perform any CSE optimizations. The generator can be found in directory ~uli/cs515/projects/proj1 on the ilab machines.
  1. Basic straight line code:
  2. Basic code with control flow:
  3. Basic ode with control flow and array references:
  4. Optimized code ( codegen -O ) with control flow and array references:

You can generate an executable called codegen by typing make . The parser/code generator expects the input on stdin, i.e., you can call the parser on an input program as follows: codegen < demo1 . The parser/code generator writes the resulting ILOC code into file iloc.out.


Due Date

Thursday, October 25, at 11:59pm EST

If you have specific, overriding, personal reasons why these dates are unreasonable, you should discuss them directly with the instructor before the deadline.


Submission Procedure

Use the handin facility to submit your project. Note that you are able to submit only a single file . Please tar all your source files, including the ReadMe file. Your ReadMe file may contain comments that you want the grader to know about. Do a "make clean" before tar-ing your files. Do not submit your compiler as an executable. Do not submit the simulator or the provided sample solution.

We have to be able to recreate your compiler on the ilab machines by saying "make". Your submitted Makefile has to reflect the appropriate changes.

You can submit your project as many times as you wish before the deadline. We will only see the last version that you have submitted. It is your responsibility to verify that your files have been submitted correctly . Keep the receipt provided by the system for your records.

We will use the following late policy : You will receive a penalty of 20% of your overall grade for each day late. A day is a working day (Monday through Friday). For example, if you submit your project by Friday, October 26, before 23.59 EST, you will receive 80% credit instead of 100% for the on-time submission.


Grading Criteria

The project will be mainly graded on functionality. You will receive no credit for the entire project if we cannot recreate (make) your compiler or your compiler does not run on any of our test codes.