Implementing Simulator Using Reflection

hey my friends,

I'm working on implementing a Simulator for PIC Microcontroller which executes Assembly instructions and makes appropriate changes in Register File and Memory.

The way I execute instructions is as follows:

I have an abstract parent class called Instruction, then I made each individual instruction as separate class that extends Instruction parent class. Now I use reflection to execute each individual instruction by creating a new object of that instruction passing its parameters and then calling execute() method.

Example :

MOVEW, 0xFF

CLRFPORTA

my Java code will look like :

Class instructionClass = Class.forName("MOVE");

Instruction inst = (Instruction) instructionClass.newInstance();

inst.passParameters(String[] parms);

inst.execute();

instructionClass = Class.forName("CLRF");

inst = (Instruction) instructionClass.forName("PORTA");

inst.passParameters(String[] parms);

inst.execute();

Is there a better fast idea or I shall keep on the same way ?

Message was edited by:

HypnotiC

Message was edited by:

HypnotiC

[1168 byte] By [HypnotiCa] at [2007-10-3 0:38:46]
# 1

If you know all the types of instruction you could write a method that returns

an object based on the first word of the line.public abstract class AbstractInstruction {

protected String[] paramArr;

protected void setParam(String[] line) {

paramArr = new String[line.length - 1];

System.arraycopy(line, 1, paramArr, 0, line.length - 1);

}

public abstract void execute();

public AbstractInstruction create(String[] line) {

AbstractInstruction ret;

if(line[0].equals("MOVE")) ret = new MoveInstruction();

// else if() etc

else ret = new ClrfInstruction();

ret.setParam(line);

return ret;

}

}

class MoveInstruction extends AbstractInstruction {

public void execute() {

// do something with paramArr

}

}

class ClrfInstruction extends AbstractInstruction {

public void execute() {

// do something else with paramArr

}

}

pbrockway2a at 2007-7-14 17:32:48 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

thanx pbrockway2,

but that way I will have a huge number of IF statements say I have 100 instructions means 100 IF Else statements which is not a good design.

I was asking about the reflection idea itself, I guess it will be a bit slow to reflect the required class each time, so anyone can think of a better idea to execute the instructions ?

mipsmea at 2007-7-14 17:32:48 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

You can put all the instructions into a Map:

Map instructions = new HashMap();

instructions.add("MOVE", new MoveInstruction());

instructions.add("CLRF", new ClrfInstruction() );

Make sure your instructions support cloning, then to get an instruction do something like this:

String parsedInstructionName = "MOVE";

String[] args = {"W", "0xFF"};

Instruction i = (Instruction)instructions.get(parsedInstructionName);

if( i != null )

i = (Instruction)i.clone();

i.setArgs(args);

}

Now 'I' contains a brand new instruction with the proper arguments set. Wrap this all up in a Factory class and you should be set.

gantzma at 2007-7-14 17:32:48 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
thanx gantzm, it's a good waybut do u think this is faster than the reflection way ?
mipsmea at 2007-7-14 17:32:48 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
why do i need to clone the object. I can just retrieve the same object from the Hashmap changing its state by setting new args.what do u think friends ?
javaha at 2007-7-14 17:32:48 > top of Java-index,Other Topics,Patterns & OO Design...
# 6
> thanx gantzm, it's a good way> but do u think this is faster than the reflection way> ?It certainly won't be slower!
gantzma at 2007-7-14 17:32:48 > top of Java-index,Other Topics,Patterns & OO Design...
# 7

> why do i need to clone the object. I can just

> retrieve the same object from the Hashmap changing

> its state by setting new args.

>

> what do u think friends ?

I wasn't completely sure how the instance would be used when it was retrieved. If the entire process is thread safe and only one Instruction is needed at a time then no cloning would be required.

gantzma at 2007-7-14 17:32:48 > top of Java-index,Other Topics,Patterns & OO Design...
# 8
> It certainly won't be slower!why is that, can u please compare briefly between the two mechanisms showing why it certainly won't be slowerthanx.
mipsmea at 2007-7-14 17:32:48 > top of Java-index,Other Topics,Patterns & OO Design...
# 9

> why is that, can u please compare briefly between the

> two mechanisms showing why it certainly won't be

> slower

>

Assuming the cloning step is removed, as it appears it isn't needed. One will gain a performance boost over the original code snippets posted. This will result from not having to instantiate new instances of objects over and over.

Essentialy the Map is adding a caching mechanism. It may have a slightly higher startup cost, but it will make up for in having the instance of instructions cached.

Thanks.

gantzma at 2007-7-14 17:32:48 > top of Java-index,Other Topics,Patterns & OO Design...