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.compiler;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
A compiled term is a handle onto a compiled down to binary code term. Compiled terms are not Java code and may be executed outside of the JVM, but the Java retains a handle on them and provides sufficient wrapping around them that they can be made to look as if they are a transparent abstract syntax tree within the JVM. This requires an ability to decompile a clause back into its abstract syntax tree.

Decompilation of functors requires access to a mapping from registers to variables, but the variable to register assignment is created at compile time accross a whole clause. Each functor that forms part of the clause head or body must therefore hold a link to its containing functor in order to access this mapping.

CRC Card
Responsibilities
Decompile/decode a binary term to restore its abstract syntax tree.

Author(s):
Rupert Smith
Todo:
For each functor in the head and body, set this as the containing clause. A mapping from variables to registers is maintained in the clause, and the functors need to be able to access this mapping.
 
 public class WAMCompiledQuery extends Clause<Functorimplements Sentence<WAMCompiledQuery>, Sizeable,
 {
    
Used for debugging.
 
     /* private static final Logger log = Logger.getLogger(WAMCompiledQuery.class.getName()); */

    
Defines the possible states of compiled code, unlinked, or linked into a machine.
 
     public enum LinkStatus
     {
        
The code is not yet linked into a binary machine.
 
         Unlinked,

        
The code is linked into a binary machine.
 
         Linked
     }

    
Holds the state of the byte code in this compiled functor, whether it has been linked or not.
 
     protected LinkStatus status;

    
Holds the register to variable id mapping for the functor.
 
     protected Map<ByteIntegervarNames;

    
Holds a listing of all the free non-anonymous variables in the query, used to decide which variables to report in the query results.
 
     protected Set<IntegernonAnonymousFreeVariables;

    
Holds the byte code instructions for the clause, when it is not linked or when it has been disassembled.
 
     protected SizeableList<WAMInstructioninstructions = new SizeableLinkedList<WAMInstruction>();

    
Holds the original unoptimized instruction listing.
 
     protected SizeableList<WAMInstructionunoptimizedInstructions;

    
Holds the offset of the compiled code for the clause within the machine it is compiled to.
 
     protected WAMCallPoint callPoint;

    
Holds a reference to the byte code machine, that provides the symbol table for the code.
 
     protected WAMMachine machine;

    
Holds a reference to the functor interner, that maps functors onto names.
 
Creates an empty (invalid) compiled program sentence in WAM. The variables of the program sentence are not recorded, since they only need to be tracked in order to display the results of queries.
    public WAMCompiledQuery()
    {
        super(nullnull);
    }

    
Creates an empty (invalid) compiled query sentence in WAM.

Parameters:
varNames A mapping from registers to variables for the compiled clause.
freeVarNames The set of variables in the clause that are free.
    public WAMCompiledQuery(Map<ByteIntegervarNamesSet<IntegerfreeVarNames)
    {
        super(nullnull);
        this. = varNames;
        this. = freeVarNames;
    }

    
Sets a compiled head functor to this clause.

Parameters:
head The head of this clause.
instructions A list of instructions to add to the head.
    public void setHead(Functor headSizeableList<WAMInstructioninstructions)
    {
        this. = head;
        addInstructions(instructions);
    }

    
Adds a conjunctive body functor to this query clause.

Parameters:
body A conjunctive body functor to add to this clause.
instructions A list of instructions to add to the body.
    public void addInstructions(Functor bodySizeableList<WAMInstructioninstructions)
    {
        int oldLength;
        if (this. == null)
        {
            oldLength = 0;
            this. = new Functor[1];
        }
        else
        {
            oldLength = this..length;
            this. = Arrays.copyOf(this.oldLength + 1);
        }
        this.[oldLength] = body;
        addInstructions(instructions);
    }

    
Adds some instructions sequentially, after any existing instructions, to the clause.

Parameters:
instructions The instructions to add to the clause.
    public void addInstructions(SizeableList<WAMInstructioninstructions)
    {
        this..addAll(instructions);
    }

    
Gets the wrapped sentence in the logical language over WAMCompiledClauses.

Returns:
The wrapped sentence in the logical language.
    public WAMCompiledQuery getT()
    {
        return this;
    }

    
Provides the mapping from registers to variable names for this compiled clause.

Returns:
The mapping from registers to variable names for this compiled clause.
    public Map<ByteIntegergetVarNames()
    {
        return ;
    }

    
Provides the set of variables in the clause that are not anonymous or bound.

Returns:
The set of variables in the clause that are not anonymous or bound.
    {
        return ;
    }

    
    public long sizeof()
    {
        return .sizeof();
    }

    
    {
        return Collections.unmodifiableList();
    }

    
    public void setOptimizedInstructions(SizeableList<WAMInstructioninstructions)
    {
         = this.;
        this. = instructions;
    }

    
    {
        return ;
    }

    
Provides the entry call point to this compiled query.

Returns:
The entry call point to this compiled query.
    public WAMCallPoint getCallPoint()
    {
        return ;
    }

    
Emmits the binary byte code for the clause into a machine, writing into the specified byte array. The state of this clause is changed to 'Linked' to indicate that it has been linked into a binary machine.

Parameters:
buffer The code buffer to write to.
machine The binary machine to resolve call-points against, and to record as being linked into.
callPoint The call point within the machine, at which the code is to be stored.
Throws:
com.thesett.aima.logic.fol.LinkageException If required symbols to link to cannot be found in the binary machine.
    public void emmitCode(ByteBuffer bufferWAMMachine machineWAMCallPoint callPointthrows LinkageException
    {
        // Ensure that the size of the instruction listing does not exceed max int (highly unlikely).
        if (sizeof() > .)
        {
            throw new RuntimeException("The instruction listing size exceeds Integer.MAX_VALUE.");
        }
        // Used to keep track of the size of the emitted code, in bytes, as it is written.
        int length = 0;
        // Insert the compiled code into the byte code machine's code area.
        for (WAMInstruction instruction : )
        {
            instruction.emmitCode(buffermachine);
            length += instruction.sizeof();
        }
        // Keep record of the machine that the code is hosted in, and the call point of the functor within the machine.
        this. = machine;
        this. = callPoint;
        // Record the fact that the code is now linked into a machine.
        this. = .;
    }
New to GrepCode? Check out our FAQ X