dspl.intermediate.variable
Class Variable

java.lang.Object
  extended by dspl.intermediate.variable.Variable
All Implemented Interfaces:
ParserTokensTokenTypes
Direct Known Subclasses:
DSPLByte, DSPLComplex, DSPLFloat, DSPLInteger, DSPLString, DSPLUByte, DSPLUInteger

public abstract class Variable
extends java.lang.Object
implements ParserTokensTokenTypes

Variable is the base abstract class used as the parent of all other abstract Variable classes

Author:
David

Field Summary
protected  int arraySize
          The number of elements in an array
protected  int byteSize
          Stores the size (in bytes) of the Variable For primitives: float,int,uint = 4 bytes byte,ubyte = 1 byte complex = 8 bytes (2 floats) for arrays = # of elements * size of array's primitive type
static int DATA_TYPE_BYTE
           
static int DATA_TYPE_COMPLEX
           
static int DATA_TYPE_FLOAT
          Enumeration types of possible values.
static int DATA_TYPE_INT
           
static int DATA_TYPE_STRING
           
static int DATA_TYPE_UBYTE
           
static int DATA_TYPE_UINT
           
protected  int dataType
          Stores the data type, using one of the possible DATA_TYPE_xxx values above
protected  Variable elementIndex
          If the variable is an element of a larger variable (either array or complex), element index stores either:
the index into the array, in the case where elementsParentVar is an array
the flag indicating either the real or imaginary component of a complex, in the case where elementsParentVar is a DSPLComplex
protected  Variable elementsParentVar
          For Atomic Elements of Arrays/Complex, elementsParentVar contains the parent variable (either the array or complex variable)
protected  java.lang.String identifier
          Contains the identifier used within the actual source program
For example, "complex_array a;" would create a Variable of data type complex_array with the identifier set to "a";

In the case of constants, the identifier is equal to the string representation of the constant.
protected  boolean isConstant
          Refers to whether the Variable is a constant, meaning the actual value is known at compile time.
protected  boolean isElement
          Refers to whether a Variable represents an element within either an array a Variable Example: Within "int_array x[5]; x[0] = 1;", "x[0]" should create an array element variable to pass into the IR
protected  int lineNumDecl
          Contains the line within the source program where the Variable was declared
static int SIZE_TYPE_ARRAY
          Describes any arra of single elements (array of ints, array of floats, etc)
static int SIZE_TYPE_ATOM
          Describes any single elements (integer, float, etc)
protected  int sizeType
          Stores the size type, using one of the possible SIZE_TYPE_xxx above
 java.lang.Object value
          Stores the value of the object (Integer, Float, Array of Integers, Array of Floats, etc)
 
Fields inherited from interface dspl.frontend.ParserTokensTokenTypes
ASSIGN, COMMA, COMMENT, DECLS, DIGITS, DIV, DOT, EOF, EQ, GE, GT, ID, LBRACE, LBRACK, LE, LITERAL_byte, LITERAL_byte_array, LITERAL_ccall, LITERAL_complex, LITERAL_complex_array, LITERAL_else, LITERAL_float, LITERAL_float_array, LITERAL_for, LITERAL_if, LITERAL_int, LITERAL_int_array, LITERAL_string, LITERAL_ubyte, LITERAL_ubyte_array, LITERAL_uint, LITERAL_uint_array, LITERAL_while, LPAREN, LT, MINUS, MUL, NE, NEGATE, NEWLINE, NULL_TREE_LOOKAHEAD, NUM, PLUS, PROGRAM, RBRACE, RBRACK, REAL, RPAREN, SEMI, STRING, TILDE, WS
 
Constructor Summary
Variable(int numElements)
          Constructor immediately invoked by all derived classes of Variable in their atomic or array constructor
 
Method Summary
abstract  java.lang.Object convertToObject(java.lang.String strValue)
          Method responsible for taking a string from the source code and converting it into the appriopriate Java Object for the primitive.
For all DSPL primitives except complex, the native java objects are used (String for string, Integer for int/uint, Float for float, etc).
abstract  java.lang.Object convertToObject(java.lang.String strValue1, java.lang.String strValue2)
          Method responsible for taking two strings from the source code and converting it into the appriopriate Java Object for the primitive.
At time of writing, this is only required for Complex, which takes two strings (real and imag).
For complex, the dspl.intermediate.variable.primitive.Complex object is used
Note invoking the double string convertToObject on any type besides complex throws an error.
static int getAntlrTypeFromVarType(int varType)
          Returns the Antlr constant for a variable type based on the DSPL variable type.
static int getAntlrTypeFromVarTypeArray(int varType)
           
 int getArraySize()
          Returns the data type of the Variable (either atomic or array)
 int getByteSize()
          Returns the size of the Variable (size = (size of type) * (num of elements)
abstract  int getDataType()
           
abstract  int getDataTypeByteSize()
          Returns the size of the Variable type's byte size (for a single element)
 java.lang.String getIdentifier()
          Gets the identifier for the Variable
Throws an error if the variable has not yet been set.
 Variable getIndexVariable()
           
static Variable getNewArray(int parserTokenID, java.lang.String identifier, int arraySize, int lineDecl)
          Called to create a new array Variable of the appriopriate size based on the provided parserTokenID
static Variable getNewAtomic(int parserTokenID, java.lang.String identifier, int lineDecl, boolean isConstant)
          Creates a new Atomic Variable.
static Variable getNewAtomicElement(Variable parentVar, Variable elementIndex, int lineDecl)
          Returns a new Atomic variable representing an element within an array or complex
 Variable getParentVariable()
           
 int getSizeType()
           
 java.lang.Object getValue()
          Returns the Variable's value
 int getVariableSizeType()
           
 void initVariable(int numElements)
          The purpose of this function is to have a single function that all derived classes can call to perform the initialization steps of setting the byte size, data type, and size type While this could be done in the constructor, by putting it here and then relying on the necessary get calls that all descended classes must implement, this makes it easier to write correct code which fills in the proper values as one need only remember to call initVariables in the constructor, rather than remember to write out all of the fields below.
 boolean isArray()
          Called to determine if the variable is an array or not
 boolean isAtomic()
          Called to determine if the variable is an atomic or not
 boolean isConstant()
          Returns whether the Variable is a constant variable or not
 boolean isElement()
           
abstract  boolean isNaturalNum()
          Natural numbers are ints, uints, chars, and uchars.
abstract  boolean isNumeric()
          Numeric types are defiend in the context of the allowable operations performable on them.
 boolean isSameDataType(Variable otherVar)
          Note that this function returns true so long as their type is the same, it does not take into account whether the sizes are the same.
 boolean isValueSet()
          Retruns whether the Variable's value has been set
The function has been included so that variables can have their initial values set implicitly when they are declared within the .data section of the ASM file instead of requiring a "mov" instruction to set it in the begining
This feature is specificaly required for floating point values because floating point operations do not accept immediate operands.
 void setIdentifier(java.lang.String newID)
          Sets the identifier of the Variable.
 void setValue(java.lang.String valueStr)
          Used to set the value of a Variable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DATA_TYPE_FLOAT

public static final int DATA_TYPE_FLOAT
Enumeration types of possible values. Note this value only indicates the type of data but not the # of elements. Both an Array of Integers and a single integer would return DATA_TYPE_INT .

See Also:
Constant Field Values

DATA_TYPE_INT

public static final int DATA_TYPE_INT
See Also:
Constant Field Values

DATA_TYPE_UINT

public static final int DATA_TYPE_UINT
See Also:
Constant Field Values

DATA_TYPE_BYTE

public static final int DATA_TYPE_BYTE
See Also:
Constant Field Values

DATA_TYPE_UBYTE

public static final int DATA_TYPE_UBYTE
See Also:
Constant Field Values

DATA_TYPE_COMPLEX

public static final int DATA_TYPE_COMPLEX
See Also:
Constant Field Values

DATA_TYPE_STRING

public static final int DATA_TYPE_STRING
See Also:
Constant Field Values

SIZE_TYPE_ATOM

public static final int SIZE_TYPE_ATOM
Describes any single elements (integer, float, etc)

See Also:
Constant Field Values

SIZE_TYPE_ARRAY

public static final int SIZE_TYPE_ARRAY
Describes any arra of single elements (array of ints, array of floats, etc)

See Also:
Constant Field Values

value

public java.lang.Object value
Stores the value of the object (Integer, Float, Array of Integers, Array of Floats, etc)


dataType

protected int dataType
Stores the data type, using one of the possible DATA_TYPE_xxx values above


sizeType

protected int sizeType
Stores the size type, using one of the possible SIZE_TYPE_xxx above


byteSize

protected int byteSize
Stores the size (in bytes) of the Variable For primitives: float,int,uint = 4 bytes byte,ubyte = 1 byte complex = 8 bytes (2 floats) for arrays = # of elements * size of array's primitive type


arraySize

protected int arraySize
The number of elements in an array

For Variables declared which have sizeType = SIZE_TYPE_ARRAY this value must be >=1
For SIZE_TYPE_ATOM it should remain -1


isConstant

protected boolean isConstant
Refers to whether the Variable is a constant, meaning the actual value is known at compile time.

Example: "int x=5;" in this case the Variable created for 5 would have isConstant = true, since its initial value is known at compile time. This is useful for generating assmebly code when one needs to decide whether to pass an actual variable or a immediate constant within the asm instruction.


isElement

protected boolean isElement
Refers to whether a Variable represents an element within either an array a Variable Example: Within "int_array x[5]; x[0] = 1;", "x[0]" should create an array element variable to pass into the IR


elementIndex

protected Variable elementIndex
If the variable is an element of a larger variable (either array or complex), element index stores either:
the index into the array, in the case where elementsParentVar is an array
the flag indicating either the real or imaginary component of a complex, in the case where elementsParentVar is a DSPLComplex


elementsParentVar

protected Variable elementsParentVar
For Atomic Elements of Arrays/Complex, elementsParentVar contains the parent variable (either the array or complex variable)

Should only be set in the case where isElement is true.


identifier

protected java.lang.String identifier
Contains the identifier used within the actual source program
For example, "complex_array a;" would create a Variable of data type complex_array with the identifier set to "a";

In the case of constants, the identifier is equal to the string representation of the constant.
For example, "int b; b = 5;", would create two Variables, one for b and one for 5. The identifier for 5 would be "5";


lineNumDecl

protected int lineNumDecl
Contains the line within the source program where the Variable was declared

Constructor Detail

Variable

public Variable(int numElements)
Constructor immediately invoked by all derived classes of Variable in their atomic or array constructor

Parameters:
numElements - number of elements to be created, numElements=1 implies ATOMIC, numElements>1 implies ARRAY
Method Detail

isConstant

public boolean isConstant()
Returns whether the Variable is a constant variable or not

Returns:
true if the Variable is a constant expression, false otherwise

getValue

public java.lang.Object getValue()
Returns the Variable's value

Returns:

isValueSet

public boolean isValueSet()
Retruns whether the Variable's value has been set
The function has been included so that variables can have their initial values set implicitly when they are declared within the .data section of the ASM file instead of requiring a "mov" instruction to set it in the begining
This feature is specificaly required for floating point values because floating point operations do not accept immediate operands.

Returns:

setValue

public void setValue(java.lang.String valueStr)
Used to set the value of a Variable.
Should only be used for creating float constants which cannot be true constants.

Parameters:
valueStr -

getSizeType

public int getSizeType()
Returns:
the size type (SIZE_TYPE_ARRAY or SIZE_TYPE_ATOMIC)

getArraySize

public int getArraySize()
Returns the data type of the Variable (either atomic or array)

Returns:
the data type (DATA_SIZE_xxx)

isArray

public boolean isArray()
Called to determine if the variable is an array or not

Returns:
true if sizeType == SIZE_TYPE_ARRAY, false otherwise

isElement

public boolean isElement()
Returns:
true if Variable is element of another Variable, false otherwise

isAtomic

public boolean isAtomic()
Called to determine if the variable is an atomic or not

Atomic types include int, uint, float, byte, ubyte, complex, and string.

Non-atomic (array-types) include int_array, uint_array, ubyte_array, byte_array, and complex_array.

Returns:
true if sizeType == SIZE_TYPE_ATOM, false otherwise

getIdentifier

public java.lang.String getIdentifier()
Gets the identifier for the Variable
Throws an error if the variable has not yet been set.

Returns:
the identifier of the variable

isSameDataType

public boolean isSameDataType(Variable otherVar)
Note that this function returns true so long as their type is the same, it does not take into account whether the sizes are the same. Therefore an array of integers compared against a single integer will have the same data type.

Parameters:
otherVar: - the Variable to compare this to
Returns:
true if otherVar and this Variable have the same Data Type, false if not

isNumeric

public abstract boolean isNumeric()
Numeric types are defiend in the context of the allowable operations performable on them. It is assumed that numeric types can undergo single and dual operand arithmatic operations, but non-numeric types cannot. For example, it is possible to add an integer constant to an array of integers, since both are numeric, but it is not possible to add a string to an integer, or add any numeric type to a string.

Returns:
true if the Variable is a numeric type (int, float, array of numeric types, etc) and false if it is not (string)

isNaturalNum

public abstract boolean isNaturalNum()
Natural numbers are ints, uints, chars, and uchars.
This function is useful when there are different arithmatic machine instructions depending on whether the number is an integer or floating point type.

Returns:

getParentVariable

public Variable getParentVariable()
Returns:
element variable's parent

getIndexVariable

public Variable getIndexVariable()
Returns:
returns an element variable's parent

getDataTypeByteSize

public abstract int getDataTypeByteSize()
Returns the size of the Variable type's byte size (for a single element)

Returns:
the size of bytes of the variable

getByteSize

public int getByteSize()
Returns the size of the Variable (size = (size of type) * (num of elements)

Returns:

setIdentifier

public void setIdentifier(java.lang.String newID)
Sets the identifier of the Variable. This method was added solely for the SymbolTable to call when renaming Strings from their actual text to their label used in the data section of the ASM File.

Parameters:
newID - String to set identifier to

getDataType

public abstract int getDataType()
Returns:
the data tyep of the variable, must be one of the Variable.DATA_TYPE_xxx values

convertToObject

public abstract java.lang.Object convertToObject(java.lang.String strValue)
Method responsible for taking a string from the source code and converting it into the appriopriate Java Object for the primitive.
For all DSPL primitives except complex, the native java objects are used (String for string, Integer for int/uint, Float for float, etc).
Note: Invoking the single string convertToObject on a complex throws an Error since it is expecting two strings.

Parameters:
strValue - the string expresion for the variable (i.e. "hello", "5", "3.4");
Returns:

convertToObject

public abstract java.lang.Object convertToObject(java.lang.String strValue1,
                                                 java.lang.String strValue2)
Method responsible for taking two strings from the source code and converting it into the appriopriate Java Object for the primitive.
At time of writing, this is only required for Complex, which takes two strings (real and imag).
For complex, the dspl.intermediate.variable.primitive.Complex object is used
Note invoking the double string convertToObject on any type besides complex throws an error.

Parameters:
strValue1 - the string expresion for the first variable
strValue2 - the string expression for the second variable
Returns:

getVariableSizeType

public int getVariableSizeType()
Returns:
the size type of the variable, must be one of the Variable.SIZE_TYPE_xxx values

initVariable

public void initVariable(int numElements)
The purpose of this function is to have a single function that all derived classes can call to perform the initialization steps of setting the byte size, data type, and size type While this could be done in the constructor, by putting it here and then relying on the necessary get calls that all descended classes must implement, this makes it easier to write correct code which fills in the proper values as one need only remember to call initVariables in the constructor, rather than remember to write out all of the fields below.


getNewArray

public static Variable getNewArray(int parserTokenID,
                                   java.lang.String identifier,
                                   int arraySize,
                                   int lineDecl)
Called to create a new array Variable of the appriopriate size based on the provided parserTokenID

Parameters:
parserTokenID - parser token id corresponding to wha tis listed in DSPLParserTokenTypes automatically generated by antler
identifier - the textual representation used to declare this variable within the source code of the program
arraySize - the size of the array
line - in the source file on which the array is declared

getNewAtomic

public static Variable getNewAtomic(int parserTokenID,
                                    java.lang.String identifier,
                                    int lineDecl,
                                    boolean isConstant)
Creates a new Atomic Variable.

if isConstant is true, then the variable is a constant literal (i.e. "5")
else if isConstant is false, then a "normal" Variable is created (i.e. "int x;") which will need a location in memory to be stored.

Parameters:
parserTokenID -
identifier -
lineDecl -
isConstant -

getAntlrTypeFromVarType

public static int getAntlrTypeFromVarType(int varType)
Returns the Antlr constant for a variable type based on the DSPL variable type.

Parameters:
varType -

getAntlrTypeFromVarTypeArray

public static int getAntlrTypeFromVarTypeArray(int varType)

getNewAtomicElement

public static Variable getNewAtomicElement(Variable parentVar,
                                           Variable elementIndex,
                                           int lineDecl)
                                    throws java.lang.Exception
Returns a new Atomic variable representing an element within an array or complex

Parameters:
parentVar - parent variable array or complex
elementIndex - 0-based index into the array or flag indicating real or imaginary component of Complex
lineDecl - line number where where array element is being used
Returns:
Variable referring to element within an array
Throws:
java.lang.Exception - if elementIndex is outside of array bounds (0 --> (arraysize-1))