Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.gp.automaton.optimizer.L1.java

Here you can find all the information about the file org.dgpf.gp.automaton.optimizer.L1.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2006 Thomas Weise
 * 
 * E-Mail           : tweise@gmx.de
 * Creation Date    : 2006-02-08 09:48:45
 * Original Filename: org.dgpf.automaton.optimizer.L1.java
 * Version          : 2.2.1
 * Last modification: 2006-05-08
 *                by: Thomas Weise
 * 
 * License          : GNU LESSER GENERAL PUBLIC LICENSE
 *                    Version 2.1, February 1999
 *                    You should have received a copy of this license along
 *                    with this library; if not, write to the Free Software
 *                    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *                    MA 02111-1307, USA or download the license under
 *                    http://www.gnu.org/copyleft/lesser.html.
 *                    
 * Warranty         : This software is provided "as is" without any
 *                    warranty; without even the implied warranty of
 *                    merchantability or fitness for a particular purpose.
 *                    See the Gnu Lesser General Public License for more
 *                    details.
 */

 
package org.dgpf.gp.automaton.optimizer;

import org.dgpf.gp.automaton.base.Conventions;
import org.dgpf.gp.automaton.base.Expression;
import org.dgpf.gp.automaton.base.Instruction;
import org.dgpf.gp.automaton.expressions.Constant;
import org.dgpf.gp.automaton.instructions.IfJump;
import org.sfc.utils.MultiBuffer;
import org.sfc.utils.Typesafe;

/**
 * The level 1 optimizer removes unreachable/useless statements.
 *
 * @author Thomas Weise
 */

final class L1  extends Conventions
  {
/**
 * The level 1 optimizer cannot be instantiated, its purely static.
 */

  private L1 ()
    {
    Typesafe.do_not_call();
    }
  
/**
 * The method calculates the reachability of the instructions of the
 * specified program.
 * @param p_source    The program to calculate the reachability of.
 * @param p_max       An integer array holding the maximum count of
 *                    reachable instructions in the programs, > 0 means
 *                    the instruction is actually useful.
 */

  private static  final void  refine_reachability
                                      (final Instruction[] p_source,
                                       final byte[]        p_max)
    {
    Instruction     l_in;
    IfJump          l_ij;
    int             l_l, l_i, l_t;
    Expression      l_cond;

    l_l = p_source.length;
    l_i = 0;

main_loop:
    while(l_i < l_l)
      {
      if(p_max[l_i] == 0)
        {
        l_in = p_source[l_i];
        
        if(l_in instanceof IfJump)
          {
          l_ij   = ((IfJump)l_in);
          l_t    = l_ij.get_target();
          l_cond = l_ij.get_condition();

          if(l_cond instanceof Constant)
            {
            if(is_true( ((Constant)l_cond).get_value() ))
              {
              if(p_max[l_t] < 0) p_max[l_t] = 0;
              p_max[l_i] = 1;
              l_i++;
              continue main_loop;
              }
            }
          else
            {         
            if(p_max[l_t] < 0) p_max[l_t] = 0;
            }
          }

        p_max[l_i] = 1;

        if(l_i < (p_source.length-1))
          {
          if(p_max[l_i+1] < 0) p_max[l_i+1] = 0;
          }
        }

      l_i++;
      }
    }

/**
 * The method calculates the reachability of the instructions of the
 * specified program.
 * @param p_source    The program to calculate the reachability of.
 * @param p_ret       The reachability checker array.
 */

  private static  final void   check_reachability
                                      (final Instruction[] p_source,
                                       final byte[]        p_ret)
    {
    int    l_l;

    l_l   = p_source.length;
    p_ret[0] = 0;

main:
    for(;;)
      {
      refine_reachability(p_source, p_ret);
      for(l_l = (p_source.length-1); l_l >= 0; l_l--)
        {
        if(p_ret[l_l] == 0) continue main;
        }
      break;
      }
    }

/**
 * This method optimizes a piece of code by removing all unnecessary
 * instructions and flattening expressions.
 * @param p_source    The source instruction melange to optimize.
 * @param p_buffer    The multi-buffer to be used.
 * @return  The optimized program. If the returned value is the same as
 *          <code>p_source</code>, no optimizations could have been
 *          performed.
 */

  private static  final Instruction[] optimize_l1
                                      (final Instruction[]  p_source,
                                       final MultiBuffer    p_buffer)
    {
    final byte[]          l_max;
          int             l_l;

    l_l       = p_source.length;
    
    if(l_l <= 0) return Optimizer.EMPTY;
    
    l_max     = p_buffer.get_bytes(l_l, ((byte)(-1)));
    
    check_reachability(p_source, l_max);

    for(--l_l; l_l >= 0; l_l--)
      {
      if( (l_max[l_l] > 0) &&
          (p_source[l_l].is_useless(l_l))) l_max[l_l] = 0;
      }
    
    return Downsizer.downsize(p_source, l_max, p_buffer);
    }


/**
 * This method optimizes a piece of code by removing all unnecessary
 * instructions and flattening expressions.
 * @param p_source    The source instruction melange to optimize.
 * @param p_buffer    The multi-buffer to be used.
 * @return  The optimized program. If the returned value is the same as
 *          <code>p_source</code>, no optimizations could have been
 *          performed.
 */

  static final Instruction[] optimize(      Instruction[] p_source,
                                      final MultiBuffer   p_buffer)
    {
    Instruction[] l_is;
        
    l_is = p_source;
    
    do
      {
      p_source = l_is;
      l_is     = optimize_l1(p_source, p_buffer);      
      } while(l_is.length < p_source.length);
    
    return l_is;
    }
  }

File Information:

file name:L1.java
package:org.dgpf.gp.automaton.optimizer
qualified name:org.dgpf.gp.automaton.optimizer.L1.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/automaton/optimizer/L1.java
size:5.863 KB (6004 B)
uploaded: 2015-07-22 04:10:55 GMT+0000
last update: 2006-05-08 13:38:46 GMT+0000
last access: 2017-11-24 00:21:23 GMT+0000

statistics online since 2006-01-02.   RSS Feed
Contact us by sending an email to tweise@gmx.de to receive further information, to report errors, or to join our project.
All content on this site (http://dgpf.sourceforge.net/) is LGPL-licensed.
http://dgpf.sourceforge.net/scripts/source/source.php last modified at 2015-07-22 04:10:53 GMT+0000 served at 2017-11-24 00:21:23 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo