CS 536 Fall 2007, Project 5

Class CG

Object
  extended by CG

public class CG
extends Object

Static methods for generating code to a file.


Field Summary
protected static int labelCounter
          Counter for generating unique assembly language labels.
static int MAX_S_REG
          Max S register number.
 
Constructor Summary
CG()
           
 
Method Summary
static void close()
          Closes the output stream.
static void comment(String text, Object... args)
          Generates a comment.
static void inst(String opcode, Object... args)
          Outputs a machine instruction.
static void label(String lab)
          Outputs a label.
static String newLabel()
          Generates a new label.
static void open(String fname)
          Opens the output stream.
static String S(int reg)
          Returns the name of an S register.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

labelCounter

protected static int labelCounter
Counter for generating unique assembly language labels.


MAX_S_REG

public static final int MAX_S_REG
Max S register number.

See Also:
Constant Field Values
Constructor Detail

CG

public CG()
Method Detail

open

public static void open(String fname)
                 throws IOException
Opens the output stream.

Parameters:
fname - the name of the output file.
Throws:
IOException - if the output file cannot be opened for writing.

close

public static void close()
                  throws IOException
Closes the output stream.

Throws:
IOException - if the stream cannot be closed.

inst

public static void inst(String opcode,
                        Object... args)
Outputs a machine instruction.

Parameters:
opcode - the operation code.
args - the arguments (may be empty).

newLabel

public static String newLabel()
Generates a new label.

Returns:
a new label

label

public static void label(String lab)
Outputs a label. For example, "L2:" or "_main:".

Parameters:
lab - the text of the label, not including the colon.

comment

public static void comment(String text,
                           Object... args)
Generates a comment. Each line is preceded by '#'

Parameters:
text - the comment text, possibly containing printf-style escapes such as '%s'.
args - arguments corresonding to the % escapes.

S

public static String S(int reg)
Returns the name of an S register.

Parameters:
reg - the register number, in the range 0 .. MAX_S_REG.
Returns:
the name of the register, for example "$s0".
Throws:
IllegalArgumentException - if reg is out of range.

CS 536 Fall 2007, Project 5