Project, Part 4

Compiling Classes

CS 212 - Spring 2004

Due: Monday, May 10, 11:00PM

Update (May 1): An error in the example program has been corrected (the types were missing on the parameter list for the Node constructor).

Updates (Apr 28):

Objectives

Bali Part 4

Part 4 Bali is object oriented!  In other words it includes classes and (single) inheritance. See the Bali Specs for Part 4. Objects are stored in SaM's Heap. A single program can include multiple classes and multiple functions (this is different from Java where there are only classes and no functions). There is still a main function that is called automatically when a Bali program is run. Here's a sample program; this program has not yet been tested so there may be some errors.

// Sample Part 4 program that uses a Queue and a Stack
int main ( )
  {int n; Stack s; Queue q;} {
  n = 0; s = Stack(); q = Queue();
  while n < 5 do {
    s.put(n); q.put(n);
    n = n + 1;
    }
  n = 0;
  while n < 5 do {
    print s.get(); print q.get();
    n = n + 1;
    }
  return 0;
  }

class Node
  { public int data; public Node link; }
  { public Node (int data, Node link) {}
    { this.data = data;
      this.link = link;
    }
  }{}

class Queue
   { private Node head, last; }
   {}
   { public void put (int i)
       { Node n; }
       { n = null;
         n = Node(i, n);
         if head == null then head = n;
         else last.link = n;
         last = n;
         return;
       }
     public int get ()
       { Node n; }
       { n = head;
         head = head.link;
         return n.data;
       }
   }

class Stack
   { private Node top; }
   {}
   { public void put (int i)
       {}
       { top = Node(i, top);
         return;
       }
     public int get ()
       { Node n; }
       { n = top;
         top = top.link;
         return n.data;
       }
   }

Implementing Classes

A description of how classes are implemented appeared in lecture (Week11 Implementing Objects.pdf and Week13 Code for Classes.pdf). Please feel free to ask questions on the newsgroup (cornell.class.cs212), via email, during lecture, or during Office Hours.

You will need to manipulate program addresses (i.e., labels within the sam-code). To do this, use the instruction
            PUSHIMMPA label
which pushes a Program Address (indicated by label) onto the stack.

Error Handling

As in the previous assignment, you are two report the two kinds of errors:

For example, a missing semicolon or an unmatched parenthesis is a syntax error; a type mismatch or an undeclared variable is a semantic error.

Tasks

Write a Bali compiler that translates Bali code (satisfying the Bali Specs for Part 4) into valid sam-code. You may use any of the sam-code instructions; these are summarized in SaM Summary

Submission Instructions for Part 4:

Bonus Work (up to 25 bonus points)

Warning: Do not spend time on the bonus work unless your compiler already performs as required for Part 4.

There are some situations in which it is important to produce very efficient machine code. For instance, efficient machine code is needed for real-time applications, for code that will be run often, and for large simulations in engineering, physics, or biology.

Consider the following simple Bali code.

        x = 2 + 2;

Without any optimization, this is compiled to produce the following SaM instructions.

        PUSHIMM 2
        PUSHIMM 2
        ADD
        STOREOFF  3    // assumes x is at offset 3 from the FBR

Of course, the compiler already "knows" the value of 2+2 at compile time; there is no need to compute this value at runtime. Thus a smart compiler can generate half the instructions while still producing an equivalent result.

        PUSHIMM 4
        STOREOFF  3

This optimization strategy is known as constant folding.

For bonus credit, research and implement one or more optimization techniques. You can use any techniques that you want subject to the following restrictions.

Be careful that your optimization does not break other features of Bali. Such breakage will be penalized.

Please do not discuss optimizations that you plan to implement with class member outside your own group. The intent of this bonus work is to encourage some independent research. If you have any questions about optimizations, please email or visit the course staff directly.