Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.gp.vm.optimization.O3.java

Here you can find all the information about the file org.dgpf.gp.vm.optimization.O3.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-07-10 07:45:24
 * Original Filename: org.dgpf.gp.vm.optimization.O3.java
 * Version          : 1.0.0
 * Last modification: 2006-07-10
 *                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.vm.optimization;

import java.util.Arrays;

import org.dgpf.gp.vm.base.Instruction;
import org.dgpf.gp.vm.base.VMContext;
import org.dgpf.gp.vm.instructions.base.SideEffect;
import org.dgpf.gp.vm.instructions.ctrl.Call;
import org.dgpf.gp.vm.instructions.ctrl.Goto;
import org.sfc.utils.Typesafe;

/**
 * This optimizer removes all instructions that are reached but make no
 * sense.
 *
 * @author Thomas Weise
 */

final class O3
  {
  
/**
 * Optimize a program by removing unnecessary instructions.
 * @param p_code      The program code.
 * @param p_context   The virtual machine context to be used.
 * @return  <code>true</code> if and only if the program could be optimized.
 */

  static  final boolean optimize(final Instruction[][] p_code,
                                 final VMContext       p_context)
    {
    int           l_i;
    boolean       l_b;
    Instruction[] l_x, l_y;
    
    l_b = false;    
    for(l_i = (p_code.length-1); l_i >= 0; l_i--)
      {
      l_x = p_code[l_i];
      while((p_code[l_i] = l_y = optimize(l_x, p_context)) != l_x)
        {
        l_b = true;
        l_x = l_y;
        }
      }
    
    return l_b;
    }

/**
 * Color the code.
 * @param p_code  The code.
 * @param p_b     The colors.
 * @param p_from  The from index.
 * @param p_to    The to index.
 * @param p_color The color.
 * @return  The new max color.
 */

  private static  final int   color (final Instruction[] p_code,
                                     final int[]         p_b,
                                           int           p_from,
                                           int           p_to,
                                           int           p_color)
    {
    int         l_i, l_t;
    Instruction l_x;
    Goto        l_g;
    
    if(p_from < p_to)
      {
      Arrays.fill(p_b, p_from, p_to, p_color);
      
      for(l_i = p_from; l_i < p_to; l_i++)
        {
        l_x = p_code[l_i];
        if(l_x instanceof Goto)
          {
          l_g = ((Goto)l_x);
          
          l_t = l_g.get_target();
          if(l_t < l_i)
            {
            if(l_t > p_from)
              {
              p_color = (color(p_code, p_b, l_t, l_i+1, p_color+1)+1);
              }
            }
          else if(l_t > l_i)
            {
            p_color = (color(p_code, p_b, l_t, p_to, p_color+1)+1);
            if(p_to > l_t) p_to = l_t;
            }
          }
        }
      }
    
    return p_color;
    }
  
/**
 * Optimize a procedure by removing unnecessary instructions.
 * @param p_code      The procedure code.
 * @param p_context   The virtual machine context to be used.
 * @return  The optimized program code.
 */

  private static final Instruction[] optimize(      Instruction[] p_code,
                                              final VMContext     p_context)
    {
    int           l_l, l_d, l_i, l_j;
    int[]         l_c;
    byte[]        l_b;
    boolean       l_z;
    Instruction   l_f, l_r;
    
    l_l = p_code.length;    
    if(l_l <= 0) return O2.EMPTY;
        
    l_c = p_context.get_buffer().get_ints(l_l);
    color(p_code, l_c, 0, l_l, 0);
           
    l_b = p_context.get_buffer().get_bytes(l_l, (byte)-1);
    l_d = l_l;
    do
      {
      l_z = false;      
      
      for(l_i = (l_l-1); l_i > 0; l_i--)
        {
        if(l_b[l_i] < 0)
          {
          l_j = (l_i-1);
          do            
            {
            while((l_j >= 0) && (l_b[l_j] >= 0)) { l_j--; }
          
            if( (l_j < 0) || (l_c[l_i] != l_c[l_j]) ) break;
            
            l_r = p_code[l_j];
            if((l_r instanceof Goto) || (l_r instanceof Call) ||
                SideEffect.has_side_effect(l_r, p_code[l_i])) break;
             
            l_f = p_code[l_i].get_handler().try_melt(p_code[l_j],
                                                     p_code[l_i]);
            if(l_f != null)
              {
              l_b[l_i]    = 1;
              p_code[l_j] = l_f;
              l_d--;
              l_z         = true;
              break;
              }
            
            } while((--l_j) >= 0);          
          }
        }
      
      } while(l_z);
    
        
    return O2.clean(p_code, l_b, l_d);
    }
  
  
/**
 * No instances of this class, hun.
 */

  private O3()
    {
    Typesafe.do_not_call();
    }
  }

File Information:

file name:O3.java
package:org.dgpf.gp.vm.optimization
qualified name:org.dgpf.gp.vm.optimization.O3.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/vm/optimization/O3.java
size:5.523 KB (5656 B)
uploaded: 2015-07-22 04:10:58 GMT+0000
last update: 2006-07-12 05:54:37 GMT+0000
last access: 2017-11-18 06:37:25 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-18 06:37:25 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo