Assignment 3 Code Review

24 October 2002 - CS 509


Assignment 3 Code Review


What's the Problem?


The Big Ideas


Top-Down Design


What's the Solution?


Now what?


Code Blocks


Step 1


Optimization


Statement Packing


Combining Statements


Lifting Statements


Lifting Statements, Implemented

static bool 
lift_statements(block::code & blk)

  bool changed = false;

  block::code new_blk
  new_blk.push_back(blk[0])

  for (unsigned i = 1; i < blk.size(); i++)

    const statement_set & old_ss = blk[i]
    statement_set new_ss;

    for (unsigned j = 0; j < old_ss.size(); j++)
      const statement & s = old_ss[j]
      unsigned home = new_blk.size()

      while ((home > 0) && new_blk[home - 1].compatible(s))
        home--

      if (home != new_blk.size())
        new_blk[home].add(s)
        changed = true
      else
        ss.add(s)

    if (!ss.empty())
      new_blk.push_back(ss)

  if (changed)
    std::swap(blk, new_blk)

  return changed


Size Constraints


Operator Normalization.

bool normalize_opers(code & blk)

  bool changed = false

  for (i = 0; i < blk.size() - 1; i++)

    statement_set & ss = blk[i]

    for (j = 0; j < ss.size(); j++) 
      const statement & s = ss[j]

      if (    (s.opr() != "move")
          and (ss.opr_cnt(s.opr()) > 2)
          and blk[i + 1].compatible(s))
        blk[i + 1].add(s);
        ss.rem(s)
        changed = true

  return changed


Line Statistics - Newlines

200300400500600700
011
101
201
301
40
501211
601131
7011
8011
901
Totals348303


Line Statistics - Semicolons

100200300
022
10121
201
301
4031
502
602
70
802
901
Totals1272


Procedure Statistics

totalpages per procedure
procs123456
743
7511
10721
10811
1091
11911
141121
14131
14752
151311
171421
19172
2626
272331
282251
36351


Prefer Strings to Character Arrays

IsInstructionValid(vString insvec) {

  string ins
  char szInstruction[MAX_STRING]
  vector<string>::const_iterator it

  char *szRegisters[8] = {"r0", . . ., "r7"}

  for (it=insvec.begin(); it !=insvec.end() ; ++it)
    ins += (*it)

  strcpy(szInstruction, ins.c_str())

  for (int i = 0; i < 8; ++i)
    if (ins.find(szRegisters[i]) != -1)
      bregisterFound = true
      break


Prefer Vectors to Dynamic Memory

inputfile
read_inputfile(istream is, unsigned count, unsigned cnt1)

  inputfile ss;

  if (!readline(is, line))
    line = "";
    ss = new command_row[count + 1];
    cnt = cnt1;
  else
    ss = read_inputfile(is, count + 1, ++cnt1);

  ss[count] = line

  return ss


Use Named Constants


Avoid Duplicate Code


Use What You're Given


Points to Remember


This page last modified on 25 October 2002.