ZCrypt


Object based


Make it more dynamic


Easier to integrate with java


Misc



Implementation


Instruction Interface


public interface Instruction {


/** Get the assembly language mnenomic for this instruction. **/

public String getMnenomic();


/** Get the op code, ie the instructions id. **/

public int getOpCode();


/** Get a new instance of this instruction class. **/

public Instruction newInstruction();


/** Initialize the instruction, with arguments from the assembler.**/

public void parse( Object[] args ) throws ParseException;


/** Read any extra data the instruction requires.**/

public void readData( DataInput in, Hashtable stringTable ) throws IOException;


/** Write any extra data the instruction uses. **/

public void writeData( DataOutput out, Hashtable stringTable ) throws IOException;


/** Execute the instruction. **/

public void execute( VirtualMachine vm );


}


Using this the assembler would function something like:



The VM will load instructions something like this:



In both case the finding of the appropriate instruction will be handled by some "other" class. This class could then either dynamically work out what instructions are available, or simply maintain an array of instructions. In both cases the maintanence of the assembler/vm is easier. At least in terms of adding new instructions. There is then also less chance of mismatches, as all of the data is being kept in one place (inside the instruction classes).


Instructions


Instructions operate on the stack. In general if a variable has not been initialised it will return yield the value "null". This goes for array and hashes, as well as fields on objects.

Many instructions depend on certain values being present on the stack when they are called. Values will usually be popped from the stack in reverse order. Where a and b are referred to, usually b is popped then a.





CALL_METHOD

Pops (in this order) num args, args, method name and object from the stack. method name and num args will be used to find out which method to call. Additionally when calling methods on java objects the args may have to be used too, with some coercion into different datatypes.

If object is null (and it may be) then the search for a method begins with the object "this". If no match is found then the search will procede to the functions defined. In this way we can remove the requirement for placing "this" in front of internal method calls, but also still allow the calling of functions from within a method. e.g.


class MyClass {


method myMethod() {

someOtherMethod(); # calling a method

someFunction(); # calling a function

doSomething(); # could be either

}


method someOtherMethod() {

}

}


function someFunction() {

}


Stack Frame


The stack frame will look something like:


frame

this # ref to current object

localVars # local variables array

methodName # current method name

numArgs # num of args

currentInstruction # the instruction being executed


Functions

Functions are like methods, but they are associated with the VMs environment. As with methods they are dynamic and called by CALL_METHOD. In this way it should make it easy to extend the builtin functions. To so this simply extend a VMEnvironment object and pass that to the VM. This will be queried for additional functions. User defined functions will supercede builtin functions, and are effectively methods on an extended version of the VMEnvironment object.