Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright The Sett Ltd, 2005 to 2014.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.thesett.aima.logic.fol.wam.optimizer;
 
 import java.util.List;
 
WAMOptimizer performs peephole optimization on a list of WAM instructions.

CRC Card
Responsibilities Collaborations
Optimize a WAM instruction listing.

Author(s):
Rupert Smith
 
 public class WAMOptimizer implements Optimizer
 {
    
The symbol table.
 
     protected final SymbolTable<IntegerStringObjectsymbolTable;

    
Holds the variable and functor name interner for the machine.
 
     private final VariableAndFunctorInterner interner;

    
Builds a WAM instruction optimizer.

Parameters:
symbolTable The symbol table to get instruction analysis information from.
interner The variable and functor name interner for the machine.
 
     public WAMOptimizer(SymbolTable<IntegerStringObjectsymbolTableVariableAndFunctorInterner interner)
     {
         this. = symbolTable;
         this. = interner;
     }

    

Runs the instruction listing through an optimizer pass, and replaces the original instruction listing with the optimized version.

 
     public <T extends WAMOptimizeableListing> T apply(WAMOptimizeableListing listing)
     {
         SizeableList<WAMInstructionoptListing = optimize(listing.getInstructions());
         listing.setOptimizedInstructions(optListing);
 
         return (T) listing;
     }

    
Performs an optimization pass for specialized instructions.

The following instruction sequences can be optimized:

 unify_var Xi, get_struc a/0, Xi -> unify_const a/0
 get_struc a/0, Xi -> get_const a/0
 put_struc a/0, Xi, set_var Xi -> set_const a/0
 put_struc a/0, Xi -> put_const a/0
 

Parameters:
instructions The instructions to optimize.
Returns:
An list of optimized instructions.
 
     private SizeableList<WAMInstructionoptimize(List<WAMInstructioninstructions)
     {
         StateMachine optimizeConstants = new OptimizeInstructions();
         Matcher<WAMInstructionWAMInstructionmatcher =
             new Matcher<WAMInstructionWAMInstruction>(instructions.iterator(), optimizeConstants);
 
         SizeableList<WAMInstructionresult = new SizeableLinkedList<WAMInstruction>();
 
         for (WAMInstruction instruction : matcher)
         {
             result.add(instruction);
         }
 
        return result;
    }
New to GrepCode? Check out our FAQ X