net.sf.transjvm
Class Expression

java.lang.Object
  extended by net.sf.transjvm.Expression
Direct Known Subclasses:
Variable

public abstract class Expression
extends java.lang.Object

This class represents expressions. Each expression generates code which leaves a single value at the top of the stack.

Author:
Mark Shannon
See Also:
ConstructorID.newInstance(), MethodID.call(Expression object), Variable.assign(Expression value)

Field Summary
static Expression FALSE
          Expression representing the constant false.
static Expression NULL
          Expression representing the constant null.
static Expression ONE
          Expression representing the int constant 1 (one).
static Expression TRUE
          Expression representing the constant true.
static Expression ZERO
          Expression representing the int constant 0 (zero).
 
Constructor Summary
protected Expression(TypeID type)
           
 
Method Summary
abstract  java.lang.Object accept(ExpressionVisitor visitor)
           
 Expression bitwise(Operator.Math op, Expression right)
          Deprecated. Use math(op, right) instead.
 Expression bitwiseNot()
          Returns an expression representing the bitwise not of this Expression.
 Statement branch(boolean branch, Label target)
          Returns a conditional branch statement.
 void branch(boolean onTrue, Label target, InstructionStream stream)
          Branches to target if this Expression evaluates to onTrue.
 void branch(boolean onTrue, Label target, InstructionStream stream, int depth)
           
 Expression cast(TypeID cls)
          Casts this expression to type cls.
 Expression compare(Operator.Compare op, Expression right)
          Returns an expression representing the comparison this op right.
 Expression conditional(Expression ifTrue, Expression ifFalse)
          Returns an Expression which evaluates to either ifTrue or ifFalse.
static Expression constant(double value)
          Returns an Expression representing a double constant.
static Expression constant(float value)
          Returns an Expression representing the constant value.
static Expression constant(int value)
          Returns an Expression representing an int constant.
static Expression constant(long value)
          Returns an Expression representing a long constant.
static Expression constant(java.lang.String value)
          Returns an Expression representing a string constant
 Expression convert(TypeID to)
          Converts this expression to type to .
static Expression emptyArray(TypeID componentType, Expression size)
           
abstract  void eval(InstructionStream stream, int depth)
          Writes the code to stream necessary for the evaluation of this Expression.
static Expression filledArray(TypeID memberType, Expression... items)
          Returns an expression for an array of type memberType, containing items.
 TypeID getType()
          Gets the type of this Expression
 Variable index(Expression index)
          Returns a variable for an indexed array member.
 Expression isInstanceOf(TypeID type)
          Returns an expression representing this instanceof type.
 Expression length()
          Returns a Expression for the length of this array.
 Expression logicalNot()
          Returns an expression representing the logical converse of this Expression.
 Expression math(Operator.Math op, Expression right)
          Returns an Expression representing this op right.
 Expression negate()
          Returns an expression representing the negative of this Expression.
 Expression not()
          Deprecated. Use logicalNot() or bitwiseNot() instead. Acts as logicalNot() if this expression is boolean, bitwiseNot() otherwise.
 Expression shortCut(Operator.Boolean op, Expression right)
          Returns an Expression representing this op right.
 Statement toStm()
          Returns a statement which evaluates this Expression, then discards the result.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TRUE

public static Expression TRUE
Expression representing the constant true.


FALSE

public static Expression FALSE
Expression representing the constant false.


NULL

public static final Expression NULL
Expression representing the constant null.


ZERO

public static final Expression ZERO
Expression representing the int constant 0 (zero).


ONE

public static final Expression ONE
Expression representing the int constant 1 (one).

Constructor Detail

Expression

protected Expression(TypeID type)
Method Detail

accept

public abstract java.lang.Object accept(ExpressionVisitor visitor)

eval

public abstract void eval(InstructionStream stream,
                          int depth)
Writes the code to stream necessary for the evaluation of this Expression. Must also ensures that sufficient stack space is allocated if the stack depth during evaluation exceeds the final stack depth.

Parameters:
depth - The stack depth before evaluation
stream - Description of the Parameter
See Also:
InstructionStream.stackDepth(int depth)

getType

public final TypeID getType()
Gets the type of this Expression

Returns:
The type of this Expression

branch

public final void branch(boolean onTrue,
                         Label target,
                         InstructionStream stream)
Branches to target if this Expression evaluates to onTrue.

Parameters:
onTrue - If true, branches when thisExpression evaluates to true.
target - Target of the jump.
stream - The instruction stream.

branch

public Statement branch(boolean branch,
                        Label target)
Returns a conditional branch statement.

Parameters:
branch - If this expression evaluates to branch jump to target.
target - The target Label.
Returns:
A conditional branch statement.
Throws:
java.lang.IllegalStateException - if type is not boolean.

branch

public void branch(boolean onTrue,
                   Label target,
                   InstructionStream stream,
                   int depth)

toStm

public Statement toStm()
Returns a statement which evaluates this Expression, then discards the result.

Returns:
A statement which evaluates this Expression.

constant

public static Expression constant(int value)
Returns an Expression representing an int constant.

Parameters:
value - An int constant
Returns:
An Expression representing the constant value

constant

public static Expression constant(long value)
Returns an Expression representing a long constant.

Parameters:
value - A long constant
Returns:
An Expression representing the constant value

constant

public static Expression constant(double value)
Returns an Expression representing a double constant.

Parameters:
value - A double constant
Returns:
An Expression representing the constant value

constant

public static Expression constant(float value)
Returns an Expression representing the constant value.

Parameters:
value - A float constant
Returns:
An Expression representing the constant value

constant

public static Expression constant(java.lang.String value)
Returns an Expression representing a string constant

Parameters:
value - A String constant
Returns:
An Expression representing the constant value

logicalNot

public Expression logicalNot()
Returns an expression representing the logical converse of this Expression.

Returns:
An expression representing (bitwise) not this Expression.

not

@Deprecated
public final Expression not()
Deprecated. Use logicalNot() or bitwiseNot() instead. Acts as logicalNot() if this expression is boolean, bitwiseNot() otherwise.


bitwiseNot

public Expression bitwiseNot()
Returns an expression representing the bitwise not of this Expression.

Returns:
An expression representing (bitwise) not this Expression.

negate

public final Expression negate()
Returns an expression representing the negative of this Expression.

Returns:
An expression representing the negative of this Expression.

conditional

public final Expression conditional(Expression ifTrue,
                                    Expression ifFalse)
Returns an Expression which evaluates to either ifTrue or ifFalse. Both ifTrue and ifFalse should be of the same type. This expression should be boolean.

Parameters:
ifTrue - The true Expression
ifFalse - The false Expression
Returns:
The resulting Expression, which is ifTrue if this is true, otherwise ifFalse.

shortCut

public Expression shortCut(Operator.Boolean op,
                           Expression right)
Returns an Expression representing this op right. Both this and right should be booleans. Short-cut evaluation is used, ie. right is only evaluated if required to produce the result. If you require right to be evaluated, use binary(BinaryOperator op, Expression right).

Parameters:
op - The operator
right - The right hand Expression
Returns:
The resulting Expression

math

public Expression math(Operator.Math op,
                       Expression right)
Returns an Expression representing this op right.

Parameters:
op - The operator
right - The right hand Expression
Returns:
The resulting Expression

bitwise

@Deprecated
public Expression bitwise(Operator.Math op,
                                     Expression right)
Deprecated. Use math(op, right) instead.

Returns an Expression representing this op right.

Parameters:
op - The operator
right - The right hand Expression
Returns:
The resulting Expression

cast

public Expression cast(TypeID cls)
Casts this expression to type cls.

Parameters:
cls - TypeID to be cast to
Returns:
This expression cast to cls.
Throws:
java.lang.IllegalArgumentException - if cls is primitive

convert

public Expression convert(TypeID to)
Converts this expression to type to . Both this expression and to must be numeric.

Parameters:
to - The type converted to.
Returns:
An expression of type to .

index

public Variable index(Expression index)
Returns a variable for an indexed array member.

Parameters:
index - The index of the array member, must be of type INT32. Also getType().isArray() must be true, ie. This expression must represent an array.

emptyArray

public static Expression emptyArray(TypeID componentType,
                                    Expression size)
Parameters:
componentType - The type of the components of the array.
size - The size of the new array, must be of type Int32
Returns:
An expression for the new array.

filledArray

public static Expression filledArray(TypeID memberType,
                                     Expression... items)
Returns an expression for an array of type memberType, containing items. The new array will have a length equal to items.

Parameters:
memberType - The type of the array items. ie array has type memberType[].
items - The values to fill the array.
Returns:
An expression for an array of type memberType, containing items.

compare

public Expression compare(Operator.Compare op,
                          Expression right)
Returns an expression representing the comparison this op right.

Parameters:
op - The operator.
right - The rhs of the comparison.
Returns:
A boolean expression representing this op right.

isInstanceOf

public Expression isInstanceOf(TypeID type)
Returns an expression representing this instanceof type.

Parameters:
type - The type.
Returns:
A boolean expression representing this instanceof type.
Throws:
java.lang.IllegalArgumentException - if type is primitive.

length

public Expression length()
Returns a Expression for the length of this array. This Expression must be an array.

Returns:
An expression of type INT32 representing the length of this array.


Copyright 2004-5, Mark Shannon