com.joanju.cg.bytecode
Class Bytecode

java.lang.Object
  extended by com.joanju.cg.bytecode.Bytecode
Direct Known Subclasses:
AddsnsuperInstr, AddsuperInstr, Assignment, BuiltinfuncInstr, CheckvalsInstr, CompileunitInstr, DecleventInstr, DeclsnsupInstr, DefglobalInstr, DefsharedInstr, DefvarInstr, Endpoint, GetsharedInstr, LiteralnumInstr, LiteralstrInstr, PublishInstr, PushInstr, ReturnInstr, RetvalgetInstr, RetvalsetInstr, ScriptValsInstr, SubscribeInstr, SupersInstr, SyshandleInstr, TriggerblockInstr, UnknownvalInstr, UnresInstr

public abstract class Bytecode
extends java.lang.Object

An pseudocode instruction record, either from a compile unit or else as a special system record.

This class implements compareTo, equals, and hashCode for easy storage and sorting. If stored in a TreeSet, the objects would be sorted by the internal compile unit ID and instruction address.


Field Summary
protected  int address
           
protected  int cuid
           
protected  InstructionSet instrSet
           
protected  int nodeNum
           
protected  java.util.HashSet<Xref> xrefs
          The set of xref records to be written with this Bytecode instruction.
 
Constructor Summary
Bytecode()
           
Bytecode(int cuid, int address)
           
 
Method Summary
 void addSource(int addr)
           
 void addWhereUsed(int addr)
           
 void beforeWrite(boolean isNewBuild)
          Called when a build or checkval is done processing a CU, before writing the new/changed records.
 void calcValuesAgain(InstructionSet instrs)
          This is called by the InstructionSet when it's time to chase where-used chains for local value changes.
abstract  void calcValueSets()
          Calculate the value sets for the instruction.
 int compareTo(java.lang.Object o)
          Compare by cuid and address
 boolean equals(java.lang.Object obj)
           
protected  byte[] fetchBytesFromDB()
           
protected  Bytecode fetchInCu(int addr)
          Fetch a bytecode object for an address in this CU.
 byte[] generateDataForDB()
           
 int getAddress()
           
 int getCuID()
           
 int getNodeNum()
          Currently only implemented for Calls and Procedures.
abstract  Opcode getOpcode()
           
 java.util.HashSet<java.lang.Integer> getSources()
          Returns null.
 java.util.HashSet<java.lang.Integer> getValues()
          Get the set of values for this instruction.
 java.util.HashSet<java.lang.Integer> getValuesCalcLocal(Bytecode usedBy, InstructionSet instrs)
          Calculate local values.
 java.util.HashSet<java.lang.Integer> getWhereUsed()
          Returns null.
 java.util.HashSet<Xref> getXrefs()
           
 int hashCode()
           
protected  void initData()
           
 boolean isDataModified()
           
 boolean isFromDB()
           
 void setAddress(int address)
           
 void setCuID(int cuID)
           
 void setDataBytesFromDB(byte[] bytes)
           
protected  void setDataModified()
           
 void setFromDB()
           
 void setInstructionSet(InstructionSet is)
          The instructionset assigns this when a Bytecode object is added to it.
 void setNodeNum(int nodeNum)
           
protected abstract  void setValuesFromByteStream(java.io.DataInputStream in)
           
protected  boolean shouldLoadData()
           
static
<T extends Bytecode>
java.util.ArrayList<T>
sourcesForXrefTarget(int tgtCuid, int tgtAddr, T sampleObject)
          Get an ArrayList of Bytecode instructions of subtype T which are xref sources for the specified target.
 void storeIfModified()
          Store this bytecode instruction in the database if its "modified" flag has been set.
 java.lang.String toString()
           
 java.lang.String toStringFromComments(InstructionSet instrs)
          All Bytecode instruction subclasses should override this eventually.
 java.lang.String toStringFromData()
          All Bytecode instruction subclasses should override this eventually.
 java.lang.String toStringFromName(InstructionSet instrs)
          Some subclasses should override this to return the string literal, var name, procedure name, etc.
protected  void toStringFromSet(java.util.Collection c, java.lang.StringBuffer buff)
           
protected abstract  void writeValuesToByteStream(java.io.DataOutputStream out)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

cuid

protected int cuid

address

protected int address

nodeNum

protected int nodeNum

instrSet

protected InstructionSet instrSet

xrefs

protected java.util.HashSet<Xref> xrefs
The set of xref records to be written with this Bytecode instruction. If this is null, then the xref set was never calculated, and any old records should not be deleted. If this is empty or populated, then the xref set was calculated, and any old records should be deleted before writing this set.

Constructor Detail

Bytecode

public Bytecode()

Bytecode

public Bytecode(int cuid,
                int address)
Method Detail

compareTo

public int compareTo(java.lang.Object o)
Compare by cuid and address


equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

addSource

public void addSource(int addr)
               throws java.sql.SQLException,
                      java.io.IOException
Throws:
java.sql.SQLException
java.io.IOException

addWhereUsed

public void addWhereUsed(int addr)
                  throws java.sql.SQLException,
                         java.io.IOException
Throws:
java.sql.SQLException
java.io.IOException

beforeWrite

public void beforeWrite(boolean isNewBuild)
                 throws java.sql.SQLException,
                        java.io.IOException
Called when a build or checkval is done processing a CU, before writing the new/changed records. This base class implementation is a no-op.

Parameters:
isNewBuild -
Throws:
java.sql.SQLException
java.io.IOException

calcValueSets

public abstract void calcValueSets()
                            throws java.sql.SQLException,
                                   java.io.IOException
Calculate the value sets for the instruction.

Called during build, build-recalc, checkvals, and checkvals-recalc. Implementations should only build from external values once. Calculates which xref records are needed as well.

IMPORTANT: Should only ever be called for instructions belonging to an InstructionSet.

Throws:
java.sql.SQLException
java.io.IOException

calcValuesAgain

public void calcValuesAgain(InstructionSet instrs)
                     throws java.sql.SQLException,
                            java.io.IOException
This is called by the InstructionSet when it's time to chase where-used chains for local value changes.

Throws:
java.sql.SQLException
java.io.IOException

fetchInCu

protected Bytecode fetchInCu(int addr)
                      throws java.sql.SQLException,
                             java.io.IOException
Fetch a bytecode object for an address in this CU. Uses the instruction set if not null. Uses BytecodeTable.fetchWithData otherwise.

Throws:
java.sql.SQLException
java.io.IOException

getNodeNum

public int getNodeNum()
Currently only implemented for Calls and Procedures. Is -1 for instructions which have not implemented it yet.


getSources

public java.util.HashSet<java.lang.Integer> getSources()
                                                throws java.sql.SQLException,
                                                       java.io.IOException
Returns null. May be overridden by subclasses.

Throws:
java.sql.SQLException
java.io.IOException

getValues

public java.util.HashSet<java.lang.Integer> getValues()
                                               throws java.sql.SQLException,
                                                      java.io.IOException
Get the set of values for this instruction. Should be overridden by most subclasses. This default implementation returns a set containing one Layout.UNRESOLVED_VALUE.

Throws:
java.sql.SQLException
java.io.IOException

getValuesCalcLocal

public final java.util.HashSet<java.lang.Integer> getValuesCalcLocal(Bytecode usedBy,
                                                                     InstructionSet instrs)
                                                              throws java.sql.SQLException,
                                                                     java.io.IOException
Calculate local values. This is called for each bytecode instruction when the compiler is done or when a CU from the DB is being refreshed, to assign the set of local values. It is called recursively. Some instructions will need values from others. Ultimately, getValues() is returned.

Parameters:
usedBy - The other instruction that wants this instructions values, otherwise null if this has been called by a build or refresh process. The usedBy object must always be in the same compile unit.
Throws:
java.sql.SQLException
java.io.IOException

getWhereUsed

public java.util.HashSet<java.lang.Integer> getWhereUsed()
                                                  throws java.sql.SQLException,
                                                         java.io.IOException
Returns null. May be overridden by subclasses.

Throws:
java.sql.SQLException
java.io.IOException

getXrefs

public java.util.HashSet<Xref> getXrefs()

fetchBytesFromDB

protected byte[] fetchBytesFromDB()
                           throws java.sql.SQLException
Throws:
java.sql.SQLException

generateDataForDB

public byte[] generateDataForDB()
                         throws java.io.IOException
Throws:
java.io.IOException

getAddress

public int getAddress()

getCuID

public int getCuID()

getOpcode

public abstract Opcode getOpcode()

initData

protected void initData()
                 throws java.sql.SQLException,
                        java.io.IOException
Throws:
java.sql.SQLException
java.io.IOException

isDataModified

public boolean isDataModified()

isFromDB

public boolean isFromDB()

setAddress

public void setAddress(int address)

setCuID

public void setCuID(int cuID)

setDataBytesFromDB

public void setDataBytesFromDB(byte[] bytes)
                        throws java.io.IOException
Throws:
java.io.IOException

setValuesFromByteStream

protected abstract void setValuesFromByteStream(java.io.DataInputStream in)
                                         throws java.io.IOException
Throws:
java.io.IOException

setDataModified

protected void setDataModified()

setFromDB

public void setFromDB()

setInstructionSet

public void setInstructionSet(InstructionSet is)
The instructionset assigns this when a Bytecode object is added to it. Usually when an object is created or fetched for modification, it and other Bytecode instructions from the same CU are cached in an InstructionSet. The instructionSet is necessarily null if the instruction is straight from the db.


setNodeNum

public void setNodeNum(int nodeNum)
See Also:
getNodeNum()

shouldLoadData

protected boolean shouldLoadData()

sourcesForXrefTarget

public static <T extends Bytecode> java.util.ArrayList<T> sourcesForXrefTarget(int tgtCuid,
                                                                               int tgtAddr,
                                                                               T sampleObject)
                                                                    throws java.sql.SQLException,
                                                                           java.io.IOException
Get an ArrayList of Bytecode instructions of subtype T which are xref sources for the specified target. Requires a "sampleObject" of the desired type T, to get the type from as well as to get the Opcode from. (Necessary because Bytecode.getOpcode() is a member method, not a class method.)

Throws:
java.sql.SQLException
java.io.IOException

storeIfModified

public void storeIfModified()
                     throws java.sql.SQLException,
                            java.io.IOException
Store this bytecode instruction in the database if its "modified" flag has been set.

Throws:
java.sql.SQLException
java.io.IOException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toStringFromComments

public java.lang.String toStringFromComments(InstructionSet instrs)
                                      throws java.sql.SQLException
All Bytecode instruction subclasses should override this eventually.

Returns:
"" if not overridden.
Throws:
java.sql.SQLException

toStringFromData

public java.lang.String toStringFromData()
All Bytecode instruction subclasses should override this eventually.

Returns:
"" if not overridden.

toStringFromName

public java.lang.String toStringFromName(InstructionSet instrs)
Some subclasses should override this to return the string literal, var name, procedure name, etc.

Returns:
"" if not overridden.

toStringFromSet

protected void toStringFromSet(java.util.Collection c,
                               java.lang.StringBuffer buff)

writeValuesToByteStream

protected abstract void writeValuesToByteStream(java.io.DataOutputStream out)
                                         throws java.io.IOException
Throws:
java.io.IOException


Copyright © 2008 Joanju Software. All Rights Reserved.