gnu.jel
Class Optimizer

java.lang.Object
  |
  +--gnu.jel.Optimizer

public class Optimizer
extends java.lang.Object

This class handles storage of the expressions automatic widening type conversions and optimizations.

Currently the only supported type of optimization is evaluation of constant subexpressions. Unlike other compilers JEL not only evaluates arithmetic operators on constants but also attempts to call some (those which marked stateless) functions if all their arguments are constants. For example, expression "sin(1)" will be completely evaluated at compile time.

The gnu.jel.Library class handles details on which functions can be evaluated at compile time and which can not, see it's documentation for details.

Author:
Konstantin L. Metlov (metlov@fzu.cz)
See Also:
Library

Field Summary
protected  gnu.jel.OPlist code
          Double linked list of operations in this expression.
 
Constructor Summary
Optimizer(Library lib)
          Constructs the new "empty" optimizer with the library specified.
 
Method Summary
 void binaryOP_param()
          Specifies that the parameter for the binary operation is now in stack.
 void binaryOP(int o, boolean logical)
          Generates a binary operation.
 CompiledExpression compile()
          Compiles the expression.
 ExpressionBits compileBits()
          Compiles the expression into an ExpressionBits object.
 void conditional_end()
          Finishes generation of conditional ?: .
 void conditional_false()
          Continues generation of conditional ?: .
 void conditional_true()
          Starts generation of conditional ?: .
 void convert(java.lang.Class to)
          Generates an explicit type conversion operation.
 void convert(java.lang.Class to, boolean widening)
          Generates an explicit type conversion operation.
 void finish()
          Finishes the function.
 void function_call(java.lang.String name)
          Generates the function call.
 boolean function_param()
          Specifies that the parameter for the function is now in stack.
 void function_start()
          Denotes the start of the function call.
 void load(boolean c)
          Generates a "load boolean constant" operation.
 void load(byte c)
          Generates a "load byte constant" operation.
 void load(char c)
          Generates a "load char constant" operation.
 void load(double c)
          Generates a "load double constant" operation.
 void load(float c)
          Generates a "load float constant" operation.
 void load(int c)
          Generates a "load int constant" operation.
 void load(long c)
          Generates a "load long constant" operation.
 void load(short c)
          Generates a "load short constant" operation.
 void load(java.lang.String s)
          Generates a "load String constant" operation.
 void logical_not_start()
          Denotes start of group of logical operators whose result should be inverted.
 void logical_not()
          Inverts result of group of logical operators.
static void main(java.lang.String[] args)
          Performs unitary test of the interpreter.
 void optimize(int of)
          Optimizes the function.
protected static boolean optimizeIteration(gnu.jel.OPlist code)
          Performs one optimization pass on the given list of operations.
static void test(Tester t)
          Performs unitary test of the interpreter.
 java.lang.String toString()
          Represents the expression, contained in this optimizer as String.
 void unary(int o)
          Generates an unary operation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

code

protected gnu.jel.OPlist code
Double linked list of operations in this expression.
Constructor Detail

Optimizer

public Optimizer(Library lib)
Constructs the new "empty" optimizer with the library specified.
Parameters:
lib - is the library, used for function names resolution.
Method Detail

load

public void load(boolean c)
Generates a "load boolean constant" operation.
Parameters:
c - is the constant to load.

load

public void load(byte c)
Generates a "load byte constant" operation.
Parameters:
c - is the constant to load.

load

public void load(char c)
Generates a "load char constant" operation.
Parameters:
c - is the constant to load.

load

public void load(short c)
Generates a "load short constant" operation.
Parameters:
c - is the constant to load.

load

public void load(int c)
Generates a "load int constant" operation.
Parameters:
c - is the constant to load.

load

public void load(long c)
Generates a "load long constant" operation.
Parameters:
c - is the constant to load.

load

public void load(float c)
Generates a "load float constant" operation.
Parameters:
c - is the constant to load.

load

public void load(double c)
Generates a "load double constant" operation.
Parameters:
c - is the constant to load.

load

public void load(java.lang.String s)
Generates a "load String constant" operation.
Parameters:
s - is the String constant to load.

convert

public void convert(java.lang.Class to,
                    boolean widening)
             throws java.lang.IllegalStateException
Generates an explicit type conversion operation.

It has the sense to call this function only for narrowing conversions (see §5.1.3 and §5.1.5 of the Java Language Specification (JLS) ). Widening ( §5.1.2, §5.1.4 of JLS) type conversions are performed by this compiler automatically.

Parameters:
to - is the class to convert to.
widening - if true prohibits doing narrowing conversions.
Throws:
java.lang.IllegalStateException - if requested conversion is not supported.

convert

public void convert(java.lang.Class to)
             throws java.lang.IllegalStateException
Generates an explicit type conversion operation.

This function is equivalent to convert(to,false). Widening ( §5.1.2, §5.1.4 of JLS) type conversions are performed by this compiler automatically.

Parameters:
to - is the class to convert to.
Throws:
java.lang.IllegalStateException - if requested conversion is not supported.

unary

public void unary(int o)
Generates an unary operation.

The only unary operation for now is the negation (invert sign) operation.

Parameters:
is - the operation code, for now it can be only ExpressionImage.UN_NE.
Throws:
java.lang.IllegalStateException - if operation is not supported for types in stack.

logical_not_start

public void logical_not_start()
Denotes start of group of logical operators whose result should be inverted.
See Also:
logical_not()

logical_not

public void logical_not()
                 throws java.lang.IllegalStateException
Inverts result of group of logical operators.

To generate logical not operation it is needed :

 1. logical_not_start
 2. calculate value to be inverted
 3. logical_not
 
See Also:
logical_not()

function_start

public void function_start()
Denotes the start of the function call.

Example of the sequence of method calls to perform (compile) the function invocation is given in the description of gnu.jel.Optimizer.function_call(...) method.

See Also:
function_call(java.lang.String)

binaryOP_param

public void binaryOP_param()
Specifies that the parameter for the binary operation is now in stack.

Example of the sequence of method calls to perform (compile) the binary operation is given in the description of gnu.jel.Optimizer.binaryOP() method.

See Also:
binaryOP(int, boolean)

function_param

public boolean function_param()
Specifies that the parameter for the function is now in stack.

Example of the sequence of method calls to perform (compile) the function is given in the description of gnu.jel.Optimizer.function_call(...) method.

See Also:
function_call(java.lang.String)

function_call

public void function_call(java.lang.String name)
                   throws java.lang.IllegalStateException
Generates the function call. Example : "how to call the function"
 optimizer.function_start();
 optimizer.load(2L);
 optimizer.function_param();
 optimizer.load(2.0);
 optimizer.function_param();
 optimizer.function_call("name_in_the_library");
 
The function name is searched in the library. If there are several applicable functions in the library with the same name (not necessary in the same object originally) the most specific one is called ( See § 15.11.2.2 in the Java Language Specification). Types conversion takes place automatically.
Parameters:
name - is the name of the function to call.
Throws:
java.lang.IllegalStateException - if no function with the given name and parameter types in stack can be found in the library.

binaryOP

public void binaryOP(int o,
                     boolean logical)
              throws java.lang.IllegalStateException
Generates a binary operation.

The binary operation codes are defined in gnu.jel.ExpressionImage as constants BI_XX .

Example : "how to sum up two numbers with this optimizer"

 optimizer.load(2L);
 optimizer.binaryOP_param();
 optimizer.load(2.0);
 optimizer.binaryOP(ExpressionImage.BI_PL);
 
Note different types of operands, conversion ( to double) will be performed automatically. Note also binaryOP_param() usage, its use is obligatory to denote that the parameter for subsequent binary operation is now ion stack.
Parameters:
is - the operation code, see above.
Throws:
java.lang.IllegalStateException - if this binary op is not supported for the types in stack.
See Also:
ExpressionImage

conditional_true

public void conditional_true()
                      throws java.lang.IllegalStateException
Starts generation of conditional ?: .

The stack should contain boolean value.

To generate complete conditional it is needed :

 1. calculate condition (boolean)
 2. conditional_true()
 3. add instructions to executed if condition is "true"
 4. conditional_false()
 5. add instructions to executed if condition is "false"
 6. conditional_end()
 

conditional_false

public void conditional_false()
Continues generation of conditional ?: .
See Also:
conditional_true()

conditional_end

public void conditional_end()
                     throws java.lang.IllegalStateException
Finishes generation of conditional ?: .
See Also:
conditional_true()

finish

public void finish()
Finishes the function.

This method should be called exactly once before the attempt to instantiate expression (or obtain its image) is made. proper state to be finished (some extra items are in stack).


optimize

public void optimize(int of)
Optimizes the function.

Currently only evaluation of constant subexpressions is performed.

Parameters:
of - is the maximum number of optimization iterations to perform.

optimizeIteration

protected static boolean optimizeIteration(gnu.jel.OPlist code)
Performs one optimization pass on the given list of operations.

Currently only evaluation of constant subexpressions is performed.

Parameters:
code - is a linked list of operations to optimize.
Returns:
true if additional optimization pass may simplify code further.

compile

public CompiledExpression compile()
Compiles the expression.
Returns:
The instance of the gnu.jel.CompiledExpression subclass, with the function, represented by this optimizer compiled in.

compileBits

public ExpressionBits compileBits()
Compiles the expression into an ExpressionBits object.

ExpressionBits is a wrapper allowing to store expression in any java.io.OutputStream using Java serialization mechanism.

Returns:
instance of the gnu.jel.ExpressionBits object.
See Also:
ExpressionBits

toString

public java.lang.String toString()
Represents the expression, contained in this optimizer as String.
Returns:
String representation of the expression.
Overrides:
toString in class java.lang.Object

main

public static void main(java.lang.String[] args)
Performs unitary test of the interpreter.

This function works only if the Debugging is turned "ON".

Parameters:
args - ignored.
See Also:
Debug.enabled

test

public static void test(Tester t)
Performs unitary test of the interpreter.

This function works only if the Debugging is turned "ON". Used if all package is being tested and not just this class alone.

Parameters:
t - Tester to report test results.
See Also:
Debug.enabled