autohit.vm
Class VM

java.lang.Object
  extended byautohit.vm.VM
Direct Known Subclasses:
Service, SimVM

public abstract class VM
extends java.lang.Object

The abstract base class for virtual machines. A derived class will implement the abstract execute() method to actually run the VM. Also, it would normally use the pause() and resume() methods to control timing (rather than overloading and re-implementing them).

VM bring up sequence is as follows: instantiation with default constructor, call init(), call construct() of subclass, give VM to a Process, process will call attach(), process will call start() to run first instruction, start will call prepare() in the base class, start will call execute() for the first time.

The derived class MAY overload the method prepare() if it has anything it wants to do before the FIRST instruction (and only the first) is executed. For instance, it could add environment variables.

The pause() and resume() methods are not designed to be called by external threads. If you plan to wrap the derived vm in a threaded class, you may want to overload or just not use those methods outside of the vm. Also, these methods only manage state and timing; it is up the he execute() method in the derived class to actually stop execution.

This is not threadsafe, since one one thread should ever own an instance.

NO VM is valid until the init() method is called!!! Every VM needs a loader.

USE THESE SERVICES! Do not make your own ip, for instance! Methods of this class depend upon it.

Version:
1.0 Version History EPG - Rewrite - 8May03
Author:
Erich P. Gatejen

Field Summary
protected  java.lang.StringBuffer buf
          VM Buffer
 VMCore core
           
 int errors
          Running error total.
 int faults
          Running faults total.
protected  int ip
          Current instruction address/pointer.
protected  java.lang.Object left
          Left value -- accumulator
 VMLoader loader
          Controlling environment.
 AutohitLogInjectorWrapper myLog
          The root logging mechinism.
 VMProcess process
           
protected  java.lang.Object right
          Right value
protected  Injector rinjector
          Injector for response channel.
 java.lang.String rootProgram
          Name of the root program
 java.lang.String sname
          Session name.
protected  int state
          VM state.
static int STATE_ACTIVE_THRESHOLD
           
static int STATE_BUILDING
           
static int STATE_DONE
           
static int STATE_INVALID
          State values for the VM.
static int STATE_NEW
           
static int STATE_NO_VM
           
static int STATE_PAUSED
           
static int STATE_RUNNING
           
static int TIME_GRAN
          Granulatiry for each tick of the VM's clock.
 
Constructor Summary
VM()
          Default Constructor.
 
Method Summary
 void attach(VMProcess vmp)
          Attach VM to it's owning process.
abstract  void construct()
          Complete construction.
abstract  void destruct()
          Destroy.
 void die()
          Mark the VM for death.
abstract  void execute()
          Absract method for VM execution.
protected  void finalize()
          finalizer It will kill the logs last.
 int getState()
          Get VM state.
 void init(Injector responseChannel, java.lang.String target)
          Complete initialization.
 void pause()
          Pause execution in the VM.
 void prepare()
          Prepare for execution of the first instruction.
 void resume()
          Resume execution in the VM.
 void setParentCore(VMCore pc)
          This will set a parent core.
 void start()
          Start the VM.
 int ticks()
          Number of ticks the VM has been running.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TIME_GRAN

public static final int TIME_GRAN
Granulatiry for each tick of the VM's clock. It is used to scale the system time to the vm clock time. Currently, it is set to 1. Given the current Java implementations, this should yield a 1 millisecond tick. That is, each VM clock tick will take one millisecond.

See Also:
Constant Field Values

STATE_INVALID

public static final int STATE_INVALID
State values for the VM. Any value over STATE_ACTIVE_THRESHOLD means the VM is active.

See Also:
Constant Field Values

STATE_NO_VM

public static final int STATE_NO_VM
See Also:
Constant Field Values

STATE_BUILDING

public static final int STATE_BUILDING
See Also:
Constant Field Values

STATE_DONE

public static final int STATE_DONE
See Also:
Constant Field Values

STATE_ACTIVE_THRESHOLD

public static final int STATE_ACTIVE_THRESHOLD
See Also:
Constant Field Values

STATE_NEW

public static final int STATE_NEW
See Also:
Constant Field Values

STATE_RUNNING

public static final int STATE_RUNNING
See Also:
Constant Field Values

STATE_PAUSED

public static final int STATE_PAUSED
See Also:
Constant Field Values

state

protected int state
VM state.


ip

protected int ip
Current instruction address/pointer. A pointer into the insrtuction Vector.


right

protected java.lang.Object right
Right value


left

protected java.lang.Object left
Left value -- accumulator


buf

protected java.lang.StringBuffer buf
VM Buffer


loader

public VMLoader loader
Controlling environment. A loader, a process, and our core.


process

public VMProcess process

core

public VMCore core

rootProgram

public java.lang.String rootProgram
Name of the root program


sname

public java.lang.String sname
Session name.


errors

public int errors
Running error total.


faults

public int faults
Running faults total.


myLog

public AutohitLogInjectorWrapper myLog
The root logging mechinism. Used for controller logging.

See Also:
AutohitLogInjectorWrapper

rinjector

protected Injector rinjector
Injector for response channel.

See Also:
Injector
Constructor Detail

VM

public VM()
Default Constructor.

Method Detail

setParentCore

public void setParentCore(VMCore pc)
This will set a parent core. The storage variables from the parent will be copied to this VM while attaching.


attach

public void attach(VMProcess vmp)
            throws java.lang.Exception
Attach VM to it's owning process. Typically this is called by the VMProcess. This will hook up the SystemContext attributes. This will effectively initialize the VM.

Throws:
org.omg.CORBA.Any - exceptions it encounters. TODO fix the id KLUDGE
java.lang.Exception

start

public void start()
           throws VMException
Start the VM. It will set state and timing info, then call the abstract method execute() to execute the code.

Calling this method consecutively will effectively reset the state and timing info. It is probibly a REAL BAD IDEA to call this from the execute method.

It throws any exceptions that are thrown out of execute().

Throws:
VMException

getState

public int getState()
Get VM state. Reports the state of the vm using the STATE_* values.

You may call this from another thread, but it isn't very reliable.

Returns:
a STATE_* value

pause

public void pause()
Pause execution in the VM. This should NOT be called by another thread.

It will only pause if the VM is running.


die

public void die()
Mark the VM for death.


resume

public void resume()
Resume execution in the VM. This should NOT be called by another thread.

It will only resume if the VM is paused.


ticks

public int ticks()
Number of ticks the VM has been running. It will be scaled according to the TIME_GRAN field.

Note that it returns an int rather than a long like system time usually is. This means that the VM timing. This technically could cause some overflow problems, but I doubt a VM would ever run that long.

Returns:
number of ticks the VM has run.

execute

public abstract void execute()
                      throws VMException
Absract method for VM execution. The derived class must implement the actual execution. This method will be automatically called by start(). Therefore, you probibly should not call start() from within this method.

The implimentation of this method should only execute ONE INSTRUCTION. Successive calls would then execute the entire program. If you do not impliment it this way, you are likely to ghost the vm's.

NOTE! An implementing method MUST throw a VMException(VMException.DONE) when it reaches the end of execution.

If the derived-class VM encounters an instruction that it does now support, it should throw a VMException.INVALID_INSTRUCTION.

Throws:
VMException
See Also:
VMException

construct

public abstract void construct()
                        throws VMException
Complete construction. This will be called when the VM is initialized.

Throws:
VMException

destruct

public abstract void destruct()
                       throws VMException
Destroy. This will be called when the VM is finalizing.

Throws:
VMException

init

public final void init(Injector responseChannel,
                       java.lang.String target)
                throws VMException
Complete initialization. This must be called after the VM is constructed, but before VM is attached to a process.

Parameters:
responseChannel - the response channel
target - target program in universe namespace
Throws:
VMException

prepare

public void prepare()
             throws java.lang.Exception
Prepare for execution of the first instruction. The derived class may overload this if it has any stuff it wants to do before execute() is called the first time.

Throws:
org.omg.CORBA.Any - exceptions it encounters.
java.lang.Exception

finalize

protected void finalize()
                 throws java.lang.Throwable
finalizer It will kill the logs last.

Throws:
java.lang.Throwable


Test.