CSE 423 Lab #10: Control Flow in Intermediate Code

Turnin: a .zip on Canvas

This week's lab consists of practice and Q&A with the control flow aspects of intermediate code.

Using a Label Maker

1. Add fields to tree nodes to hold labels

2. Add a genlabel() function

The prototype
struct addr genlabel();
denotes a function that returns a unique label each time it is called. Not sure if this should return a struct addr, malloc and return a pointer to a struct addr, or fill in a parameter that is a pointer to a struct addr. Your choice.
int counter;
struct addr genlabel()
{
   struct addr a;
   a.region = R_LABEL;
   a.offset = counter++;
   return a;
}

3. Write traversal functions that assign labels

3.1 Write a traversal function that assign .first labels.
void assign_first(struct tree *t)
{
   int i;
   for(i=0; i<t->nkids; i++) assign_first(t->kids[i]);
   switch(t->prodrule) {
      /* all switch cases that DO need a label: */
      case THIS: case THAT: case THEOTHER:
         t->first = genlabel();
   }
}
3.2 Write a traversal function that assign .follow labels.
void assign_follow(struct tree *t)
{
   int i;
   switch(t->prodrule) {

   case StmtSeqFromStatement1Statement2Prodrule: {
      t->kids[0].follow = t->kids[1].first;
      }

   case IfThenStmtProdrule: {
      t->kids[4].follow = t->follow;
      }
      /* ??? */
   }
   for(i=0; i<t->nkids; i++) assign_first(t->kids[i]);
}
3.3 Write a traversal function that assign .onTrue and .onFalse labels.
These both depend on the .first and the .follow.

Write traversal functions for codegen

For this lab, it is enough to do boolean exprs and if statements, or while loops. From the lecture notes we might want to start with something like this:
   fun main()
   {
   var i : Int = 0;

   while (i < 20) {
      i = i * i + 1;
      }
   println("$i");
   }
and if we managed that, extend it in various ways, enough to handle e.g.:
fun main()
{
   var i : Int = 0;
   while (i < 20 && i%3==1) {
      i = i + 1;
      }
   println("$i");
}

What to turn in

Your compiler, with enough codegen efforts to reflect 2 hours worth of work.