All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class gnu.jel.Optimizer

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

public class Optimizer
extends Object
This class handles storage of the expressions, user level type checking and optimizations.

The only optimization currently supported is the evaluation of the constant subexpressions. This class goes even further in its attempts to optimize, it evaluates not only arifmetic constant subexpressions, but also calls to the static methods of the constant arguments. Namely, expressions such as sin(1) will be evaluated at compile time.

Another function of this class is the error handling. If during evaluation of the constant subexpression a error occurs (Throwable is thrown) it is caught and compiled into the resulting class image to be rethrown at execution time. Example : 1/0 will not cause any exceptions to come out of the compiler, but will be compiled into { throw caughtArithmeticError; } if the optimization is turned on. When optimization is off the previous expression will be compiled to { return 1/0 }, which will throw exception at run time ( execution of the evaluate(...) method of the gnu.jel.CompiledExpression). Another thing to note, that Throwables caught at compile time are not recreated at compile time ( they are not compiled into {throw new ArithmeticError()}). Errors being thrown from evaluate(..) method are EXACTLY the same object as those caught during the constant subexpression eveluation. This approach should greatly simplify the error handling and applies not only to arithmetic expressions but also to any exceptions, thrown by interpretable methods.

For more information on how to specify static non interpretable method (as in the case of java.Math.random() see the documentation for gnu.jel.Library

See Also:
Library

Constructor Index

 o Optimizer(Library)
Constructs the new "empty" optimizer with the library specified.

Method Index

 o binaryOP(int)
Generates a binary operation.
 o binaryOP_param()
Specifies that the parameter for the binary operation is now in stack.
 o compile()
Compiles the expression.
 o convert(Class)
Generates an explicit "convert type" operation.
 o convert(Class, boolean)
Generates an explicit "convert type" operation.
 o finish()
Finishes the function.
 o function_call(String)
Generates the function call.
 o function_param()
Specifies that the parameter for the function is now in stack.
 o function_start()
Denotes the start of the function call.
 o load(boolean)
Generates a "load boolean constant" operation.
 o load(byte)
Generates a "load byte constant" operation.
 o load(char)
Generates a "load char constant" operation.
 o load(double)
Generates a "load double constant" operation.
 o load(float)
Generates a "load float constant" operation.
 o load(int)
Generates a "load int constant" operation.
 o load(long)
Generates a "load long constant" operation.
 o load(short)
Generates a "load short constant" operation.
 o main(String[])
Performs unitary test of the interpreter.
 o optimize(int)
Optimizes the function.
 o optimizeIteration()
 o test(Tester)
Performs unitary test of the interpreter.
 o toString()
Represents the expression, contained in this optimizer as String.
 o unary(int)
Generates an unary operation.

Constructors

 o Optimizer
 public Optimizer(Library lib)
Constructs the new "empty" optimizer with the library specified.

Parameters:
lib - is the library, used for function names resolution.

Methods

 o load
 public void load(boolean c)
Generates a "load boolean constant" operation.

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

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

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

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

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

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

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

Parameters:
c - is the constant to load.
 o convert
 public void convert(Class to,
                     boolean widening) throws IllegalStateException
Generates an explicit "convert type" 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: IllegalStateException
if requested conversion is not supported.
 o convert
 public void convert(Class to) throws IllegalStateException
Generates an explicit "convert type" 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: IllegalStateException
if requested conversion is not supported.
 o 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: IllegalStateException
if operation is not supported for types in stack.
 o 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
 o 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
 o 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
 o function_call
 public void function_call(String name) throws 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: IllegalStateException
if no function with the given name and parameter types in stack can be found in the library.
 o binaryOP
 public void binaryOP(int o) throws 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: IllegalStateException
if this binary op is not supported for the types in stack.
See Also:
ExpressionImage
 o finish
 public void finish() throws IllegalStateException
Finishes the function.

This method should be called exactly once before the attempt to compile is made.

If there was nothing in the stack before it's called this method generates the returning of the null pointer.

Throws: IllegalStateException
if the function is not in the proper state to be finished (some extra items are in stack).
 o optimize
 public void optimize(int of)
Optimizes the function.

Currently only evaluation of constant subexpressions is performed.

Parameters:
of - is the optimization flags (0 - do nothing; >0 optimize).
 o optimizeIteration
 protected boolean optimizeIteration()
 o 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.
 o toString
 public String toString()
Represents the expression, contained in this optimizer as String.

Returns:
String representation of the expression.
Overrides:
toString in class Object
 o main
 public static void main(String args[])
Performs unitary test of the interpreter.

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

Parameters:
args - ignored.
See Also:
enabled
 o 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:
enabled

All Packages  Class Hierarchy  This Package  Previous  Next  Index