uk.ac.man.cs.rainbow.simulator
Class RASMCode

java.lang.Object
  |
  +--uk.ac.man.cs.rainbow.simulator.RASMCode
All Implemented Interfaces:
Serializable

public class RASMCode
extends Object
implements Serializable

A block of RASM code. This is the primary mechanism for definining the behaviour of a thread for simulation in the Rainbow simulator.

See Also:
RASMThread, Instruction, Serialized Form

Inner Class Summary
static class RASMCode.Exception
          Exceptions thrown due to bad assembly code.
static class RASMCode.Label
          A label/location in a block of code.
 
Field Summary
static String ARG_PREFIX
          Argument references are preceded by this string.
static char COMMENT_STARTER
          Indicates that this line of RASM text is a comment.
static String CONSTANT_COMMAND
          A constant command starts with this opcode.
static String LABEL_PREFIX
          Label references are preceded by this string.
static char LABEL_STARTER
          Indicates that this line of RASM text is a label definition.
static String MODULE_COMMAND
          A module command starts with this opcode.
static String MODULE_SEPARATOR
          The module/label string separator.
static String PUSH_COMMAND
          A push command starts with this opcode.
static String RASM_EXTENSION
          The extension for a RASM file.
static String STDLIB_PROPERTY_NAME
          The name of the property to read the standard library from.
static String STRING_PREFIX
          String references are preceded by this string.
static String TYPE_COMMAND
          A type command starts with this opcode.
static String TYPE_PREFIX
          Type references are preceded by this string.
 
Constructor Summary
RASMCode(Hashtable library, BufferedReader contents)
          Create a new block of code.
RASMCode(Hashtable library, BufferedReader contents, boolean resolve)
          Create a new block of code.
RASMCode(String name, Hashtable library)
          Create a new block of code.
RASMCode(String name, Hashtable library, boolean resolve)
          Create a new block of code.
RASMCode(String name, Hashtable library, BufferedReader contents)
          Create a new block of code.
RASMCode(String name, Hashtable library, BufferedReader contents, boolean resolve)
          Create a new block of code.
 
Method Summary
 void addInstruction(Instruction instr)
          Add an instruction to the block of code.
 RASMCode.Label defineLabel(String label)
          Define a new label with the given name.
 Instruction getInstr(int location)
          Get the instruction at the given location.
 RASMCode.Label getLabel(String label)
          Get the label whose name is that given.
static Hashtable getStandardLibrary()
          Get a reference to the standard library.
 void loadContents(BufferedReader reader)
          Assemble from a character stream.
 Type parseType(String s)
          Convert a string to a type in the context of this code block.
 String print(int locn)
          Print the instruction at the given location.
 String print(int from, int to)
          Print the instructions in the given range.
protected  RASMCode.Label resolveLabel(String label)
          Resolve a string to a label.
 void resolveLabels()
          Link the block of code.
 String toString()
          Get the name of the block of code.
 void useLabel(RASMCode.Label label)
          Add a reference to the given label.
 void useLabel(String label)
          Add a reference to the label with the given name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MODULE_SEPARATOR

public static final String MODULE_SEPARATOR
The module/label string separator. This separates the name of the module from the name of the label within that module in a far (non-local) label. Local labels do not contain this string.
See Also:
useLabel(String), resolveLabels()

COMMENT_STARTER

public static final char COMMENT_STARTER
Indicates that this line of RASM text is a comment.

LABEL_STARTER

public static final char LABEL_STARTER
Indicates that this line of RASM text is a label definition.

MODULE_COMMAND

public static final String MODULE_COMMAND
A module command starts with this opcode.

CONSTANT_COMMAND

public static final String CONSTANT_COMMAND
A constant command starts with this opcode.

TYPE_COMMAND

public static final String TYPE_COMMAND
A type command starts with this opcode.

PUSH_COMMAND

public static final String PUSH_COMMAND
A push command starts with this opcode.

LABEL_PREFIX

public static final String LABEL_PREFIX
Label references are preceded by this string.

STRING_PREFIX

public static final String STRING_PREFIX
String references are preceded by this string.

TYPE_PREFIX

public static final String TYPE_PREFIX
Type references are preceded by this string.

ARG_PREFIX

public static final String ARG_PREFIX
Argument references are preceded by this string.

STDLIB_PROPERTY_NAME

public static final String STDLIB_PROPERTY_NAME
The name of the property to read the standard library from.

RASM_EXTENSION

public static final String RASM_EXTENSION
The extension for a RASM file.
Constructor Detail

RASMCode

public RASMCode(String name,
                Hashtable library)
Create a new block of code.
Parameters:
name - The name of the block of code.
library - The group of other blocks that labels in this block of code can refer to.

RASMCode

public RASMCode(String name,
                Hashtable library,
                boolean resolve)
Create a new block of code.
Parameters:
name - The name of the block of code.
library - The group of other blocks that labels in this block of code can refer to.
resolve - Indicate whether to resolve all library references automatically on completion of the loading of code.

RASMCode

public RASMCode(String name,
                Hashtable library,
                BufferedReader contents)
         throws RASMCode.Exception,
                IOException,
                TypeException,
                TclFormatException
Create a new block of code. This will have the contents defined by the RASM text on the given stream.
Parameters:
name - The name of the block of code.
library - The group of other blocks that labels in this block of code can refer to.
contents - The stream of RASM text that defines the instructions and labels in this block of code.
See Also:
loadContents(BufferedReader)

RASMCode

public RASMCode(String name,
                Hashtable library,
                BufferedReader contents,
                boolean resolve)
         throws RASMCode.Exception,
                IOException,
                TypeException,
                TclFormatException
Create a new block of code. This will have the contents defined by the RASM text on the given stream.
Parameters:
name - The name of the block of code.
library - The group of other blocks that labels in this block of code can refer to.
contents - The stream of RASM text that defines the instructions and labels in this block of code.
resolve - Indicate whether to resolve all library references automatically on completion of the loading of code.
See Also:
loadContents(BufferedReader)

RASMCode

public RASMCode(Hashtable library,
                BufferedReader contents)
         throws RASMCode.Exception,
                IOException,
                TypeException,
                TclFormatException
Create a new block of code. This will have the contents defined by the RASM text on the given stream, and the name of the block of code will be deduced from the module declaration at the front of the block of code.
Parameters:
library - The group of other blocks that labels in this block of code can refer to.
contents - The stream of RASM text that defines the instructions and labels in this block of code.
See Also:
loadContents(BufferedReader)

RASMCode

public RASMCode(Hashtable library,
                BufferedReader contents,
                boolean resolve)
         throws RASMCode.Exception,
                IOException,
                TypeException,
                TclFormatException
Create a new block of code. This will have the contents defined by the RASM text on the given stream, and the name of the block of code will be deduced from the module declaration at the front of the block of code.
Parameters:
library - The group of other blocks that labels in this block of code can refer to.
contents - The stream of RASM text that defines the instructions and labels in this block of code.
resolve - Indicate whether to resolve all library references automatically on completion of the loading of code.
See Also:
loadContents(BufferedReader)
Method Detail

getStandardLibrary

public static Hashtable getStandardLibrary()
Get a reference to the standard library. Note that you can add your own RASMCode blocks to this without polluting the namespace between compilations since you are really getting a clone of the standard library.
Returns:
A library reference that contains the standard library components.

toString

public String toString()
Get the name of the block of code.
Overrides:
toString in class Object
Returns:
The name of the block of code.

getInstr

public Instruction getInstr(int location)
Get the instruction at the given location.
Parameters:
location - The address in this block of code to get the instruction from.
Returns:
The instruction at the location specified.
Throws:
RainbowError - If the code has not yet been linked.

getLabel

public RASMCode.Label getLabel(String label)
Get the label whose name is that given. The label must be defined.
Parameters:
label - The name of the label to return.
Returns:
The label whose name was given.
Throws:
RuntimeException - If the label is unknown at this point.

defineLabel

public RASMCode.Label defineLabel(String label)
Define a new label with the given name. The label will refer to the next instruction to be added to the block of code.
Parameters:
label - The name of the label to return.
Returns:
The label whose name was given.
Throws:
RainbowError - If the code has already been linked.

addInstruction

public void addInstruction(Instruction instr)
Add an instruction to the block of code. The instruction will be appended to the code block.
Parameters:
instr - The instruction to add.
Throws:
RainbowError - If the code has already been linked.

useLabel

public void useLabel(RASMCode.Label label)
Add a reference to the given label. The instruction added will push the label onto the stack when executed.
Parameters:
label - The label to refer to.
See Also:
addInstruction(Instruction)

useLabel

public void useLabel(String label)
Add a reference to the label with the given name. The instruction added will push the label onto the stack when executed. If the label does not yet exist, this reference will be resolved when this block of code is linked.
Parameters:
label - The name of label to refer to. May also be the name of a label in another block of code.
See Also:
addInstruction(Instruction)

resolveLabels

public void resolveLabels()
                   throws RASMCode.Exception
Link the block of code. Resolves all labels which are only known by their string names to the concrete labels that they represent. Can also handle linking between blocks of code (so long as they are all part of the same group of blocks.) The resolved labels are patched into the correct location in the block of code.
Throws:
RASMCode.Exception - If there is an unresolvable label.
RainbowError - If the block of code has already been linked.
See Also:
resolveLabel(String)

resolveLabel

protected RASMCode.Label resolveLabel(String label)
                               throws RASMCode.Exception
Resolve a string to a label. If the string contains a module separator the label is looked up in the remote code block (found by searching in the list of associated blocks supplied in the constructor) using a recursive call. If the string contains no module separator, then the label is looked up locally.
Parameters:
label - The name of the label to look up.
Returns:
The label with the given name.
Throws:
RASMCode.Exception - If the label cannot be resolved.
See Also:
MODULE_SEPARATOR

print

public String print(int from,
                    int to)
Print the instructions in the given range. Includes embedded labels.
Parameters:
from - The location of the first instruction to print. Will be set to 0 internally if less than 0.
to - One greater than the location of the last instruction to print. Will be set to one past the last instruction in the block of code if past that point.
Returns:
A printable version of the instructions between the given locations. Will also include any labels embedded within the code.

print

public String print(int locn)
Print the instruction at the given location. Includes embedded labels.
Parameters:
locn - The location of the instruction to print.
Returns:
The printable version of the instruction at the given location, plus any labels at that location.
See Also:
print(int,int)

parseType

public final Type parseType(String s)
                     throws TypeException
Convert a string to a type in the context of this code block.
Parameters:
s - The string to parse.
Returns:
The type represented by the string.
Throws:
TypeException - If the string is not a valid type.

loadContents

public void loadContents(BufferedReader reader)
                  throws RASMCode.Exception,
                         IOException,
                         TypeException,
                         TclFormatException
Assemble from a character stream. Loads the contents of the stream into this code block.
Parameters:
reader - The character stream to read from.
Throws:
RASMCode.Exception - If there is a malformatted instruction.
TypeException - If the parsing of a data value or type fails.
TclFormatException - If the input stream doesn't meet standard Tcl-style formatting rules.
IOException - If the input stream ends unexpectedly.
RainbowError - If the code block has already been linked.