Logo
Distributed Genetic Programming Framework
print print

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

Here you can find all the information about the file org.dgpf.gp.vm.optimization.O2.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-05 07:45:24
 * Original Filename: org.dgpf.gp.vm.optimization.O2.java
 * Version          : 1.0.0
 * Last modification: 2006-07-05
 *                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 org.dgpf.gp.vm.base.Instruction;
import org.dgpf.gp.vm.base.VMContext;
import org.dgpf.gp.vm.instructions.ctrl.Call;
import org.dgpf.gp.vm.instructions.ctrl.Goto;
import org.dgpf.gp.vm.mutation.CodeEditor;
import org.sfc.utils.Typesafe;

/**
 * This optimizer removes all instructions never reached.
 *
 * @author Thomas Weise
 */

final class O2
  {
/**
 * The empty instruction set.
 */

  static  final Instruction[] EMPTY = new Instruction[0];
  
/**
 * 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, l_c;
    boolean       l_b;
    Instruction[] l_x, l_y;
    
    l_b = false;    
    l_c = p_code.length;
    for(l_i = (l_c-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_c)) != l_x)
        {
        l_b = true;
        l_x = l_y;
        }
      }
    
    return l_b;
    }

/**
 * Optimize a procedure by removing unnecessary instructions.
 * @param p_code      The procedure code.
 * @param p_context   The virtual machine context to be used.
 * @param p_proc_c    The count of procedures.
 * @return  The optimized program code.
 */

  private static final Instruction[] optimize(      Instruction[] p_code,
                                              final VMContext     p_context,
                                              final int           p_proc_c)
    {
    int           l_l, l_i, l_j, l_d;
    byte[]        l_b;
    boolean       l_z;
    Instruction   l_x;
    Goto          l_g;
    
    l_l = p_code.length;    
    if(l_l <= 0) return EMPTY;
        
    l_x = p_code[0];
    if(l_x instanceof Goto)
      {
      l_g = ((Goto)l_x);
      l_j = l_g.get_target();
      if(l_g.is_unconditional() && ((l_j <= 0) || (l_j >= l_l)))
        {
        return EMPTY;
        }      
      }
    
    l_b = p_context.get_buffer().get_bytes(l_l, (byte)0);
    
    l_b[0] = -1;
    l_d    = 0;
    do
      {
      l_z = false;
      for(l_i = 0; l_i < l_l; l_i++)
        {
        if(l_b[l_i] == -1)
          {
          l_z      = true;
          l_d++;
          l_b[l_i] = -2;
          l_x      = p_code[l_i];
          l_x      = l_x.get_handler().get_replacement(l_x);
          
          if(l_x == null)
            {
            l_b[l_i] = 1;              
            l_d--;
            }
          else
            {
            p_code[l_i] = l_x;
            if(l_x instanceof Goto)
              {
              l_g = ((Goto)l_x);
              
              l_j = l_g.get_target();
              if(l_j == l_i)
                {
                l_b[l_i] = 1;              
                l_d--;
                }
              else if(l_j == (l_i+1))
                {
                l_b[l_i] = 1;
                l_d--;
                if( (l_j < l_l) && (l_b[l_j] == 0)) l_b[l_j] = -1;
                }
              else
                {
                if((l_j >= 0) && (l_j < l_l) && (l_b[l_j] == 0)) l_b[l_j] = -1;
                }
              
              if(l_g.is_unconditional()) continue;
              }            
            else if(l_x instanceof Call)
              {
              if(((Call)l_x).is_unconditional())
                {
                l_j = ((Call)l_x).get_index();
                if((l_j < 0)|| (l_j >= p_proc_c))
                  {
                  l_b[l_i] = 1;              
                  l_d--;
                  }
                }
              }
            }
          
          l_j = (l_i+1);
          if( (l_j < l_l) && (l_b[l_j] == 0) )
            {
            l_b[l_j] = -1;
            }
          }
        }
      } while(l_z);
    
        
    return clean(p_code, l_b, l_d);
    }
  
/**
 * Delete all instructions which don't have a byte value set to < 0.
 * @param p_code  The code to process.
 * @param p_b     The byte values.
 * @param p_d     The instruction count hint value.
 * @return  The new instructions.
 */

  static  final Instruction[] clean (final Instruction[]  p_code,
                                     final byte[]         p_b,
                                           int            p_d)
    {
    int           l_l, l_i, l_j;
    Instruction[] l_q;
    
    if(p_d <= 0) return EMPTY;
    
    l_l = p_code.length;
    
    for(l_i = (l_l-1); l_i >= 0; l_i--)
      {
      if(p_b[l_i] < 0)
        {
        if(!(p_code[l_i].get_handler().is_useful_at_end()))
          {
          p_b[l_i] = 0;
          p_d--;
          }
        else break;
        }
      }
    
    for(l_i = 0; l_i < l_l; l_i++)
      {
      if(p_b[l_i] < 0)
        {
        if(p_code[l_i] instanceof Goto)
          {
          if(!(((Goto)(p_code[l_i])).is_unconditional()))
            {
            p_b[l_i] = 0;
            p_d--;
            }
          else break;
          }
        else if(!(p_code[l_i].get_handler().is_useful_at_begin()))
          {
          p_b[l_i] = 0;
          p_d--;
          }
        else break;        
        }
      }
    
    if(p_d >= l_l) return p_code;
    if(p_d <= 0) return EMPTY;
    
    l_j = (l_l-1);
    for(l_i = l_j; l_i >= 0; l_i--)
      {
      if(p_b[l_i] < 0)
        {
        if(l_j > l_i)
          {
          l_j -= l_i;          
          CodeEditor.delete_instr(p_code, l_i+1, l_j, l_l, p_code);
          l_l   -= l_j;
          }
            
        l_j = (l_i-1);
        }
      }
    
    
    if(l_j > l_i)
      {
      l_j -= l_i;      
      CodeEditor.delete_instr(p_code, l_i+1, l_j, l_l, p_code);
      l_l -= l_j;
      }
    
    l_q = new Instruction[l_l];
    System.arraycopy(p_code, 0, l_q, 0, l_l);
    
    return l_q;
    }
  
/**
 * No instances of this class, hun.
 */

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

File Information:

file name:O2.java
package:org.dgpf.gp.vm.optimization
qualified name:org.dgpf.gp.vm.optimization.O2.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/vm/optimization/O2.java
size:7.290 KB (7465 B)
uploaded: 2015-07-22 04:10:58 GMT+0000
last update: 2006-08-22 07:16:10 GMT+0000
last access: 2017-11-21 15:35:06 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-21 15:35:06 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo