2

I'm writting a PLC language interpreter using C#. That PLC language contains over 20 data types and 25 instructions or so. As soon as I started to generate code I balance two differents ways to write instructions:

1) Every kind of instruction is represented in one class which contains a big switch in order to chose the data type. Example:

public class ADD : Instruction
{
    private string type;

    public ADD(string type)
    {
        this.type = type;
    }

    public bool Exec(Context c)
    {
        switch (type)
        {
            case "INT":
                short valor2 = c.PopINT();
                short valor = c.PopINT();
                short result = (short)(valor + valor2);
                c.PushINT(result);
                break;
            case "DINT":
                int valor4 = c.PopDINT();
                int valor3 = c.PopDINT();
                int result2 = (int)(valor4 + valor3);
                c.PushDINT(result2);
                break;
            case "BOOL":
                // Implement BOOL
                break;
            // Implement other types...
            default:
                break;
        }

        c.IP++;
        return false; ;
    }

}

2) Each class represent a single instruction with a single data type. This way avoid the big switch. Example:

public class ADDi : Instruction
{
    public bool Exec(Context c)
    {
        short valor = c.PopINT();
        short valor2 = c.PopINT();
        short result = (short)(valor + valor2);
        c.PushINT(result);
        c.IP++;
        return false;
    }
}

I'm using COMMAND desing pattern (Exec()) to write instructions. I think second choice is better because avoids the big switch, but that choice involves to write over 400 instructions.

Always keep in mind that in this case execution performance is more important than performance in translation.

So, my precise question is as follows: Is there any other way to factorize instructions and data types? I'm looking for writing the lesser amount of instructions without penalizing performance.

EDIT:

This picture shows my type hierarchy:

Type hierarchy

This is INT class implementation:

public class INT : ANY_INT
{

    public override string DefaultInitValue()
    {
        return "0";
    }

    public override int GetBytes()
    {
        return 2;
    }

    public override string GetLastType()
    {
        return this.ToString();
    }

    public override string ToString()
    {
        return "INT";
    }

}

Some classes are more complex (structs, arrays,...).

Operations Push and Pop are defined as follows:

public void PushINT(short value)
{
    //SP -> Stack Pointer
    resMem.WriteINT(SP, value);
    SP += 2;
}

public short PopINT()
{
    SP -= 2;
    short value = resMem.ReadINT(SP);
    return value;
}

And, finally, operations to read and write in memory.

public void WriteINT(int index, short entero)
{
    SetCapacity(index + 2); // Memory grows up dinamically
    memory[index] = (sbyte)((ushort)entero >> 8 & 0x00FF);
    memory[index + 1] = (sbyte)((ushort)entero >> 0 & 0x00FF);
}

public short ReadINT(int index)
{            
    return (short)(((short)(memory[index]) << 8 & 0xFF00) |
       ((short)(memory[index + 1]) & 0x00FF));
}

I hope this info helps. Thank you.

Jose Esperon
  • 111
  • 1
  • 10

2 Answers2

7

If you can change the implementation of Context to support generic types (e.g., Pop<int> instead of PopINT()) you can use delegates to make the implementation simpler.

Addition:

var addInt = new MathInstruction<int>((a, b) => a + b));
var addDouble = new MathInstruction<double>((a, b) => a + b));
var addDecimal = new MathInstruction<decimal>((a, b) => a + b));

Subtraction:

var subtractInt = new MathInstruction<int>((a, b) => a - b));
var subtractDouble = new MathInstruction<double>((a, b) => a - b));
var subtractDecimal = new MathInstruction<decimal>((a, b) => a - b));

Division:

var divideIntAsDouble = new MathInstruction<int, double>((a, b) => a / b));
var divideDouble = new MathInstruction<double>((a, b) => a / b));
var divideDecimal = new MathInstruction<decimal>((a, b) => a / b));

And conversion between types:

var addIntAndDouble = new MathInstruction<int, double, double>((a, b) => a + b));

It would be implemented like this:

class MathInstruction<TA, TB, TResult> : Instruction
{
    private Func<TA, TB, TResult> callback;

    public MathInstruction(Func<TA, TB, TResult> callback) 
    {
        this.callback = callback;
    }

    public bool Exec(Context c)
    {
        var a = c.Pop<TA>();
        var b = c.Pop<TB>();
        var result = callback(a, b);
        c.Push<TResult>(result);
        return false;
    }
}

// Convenience
class MathInstruction<T, TResult> : MathInstruction<T, T, TResult>
class MathInstruction<T> : MathInstruction<T, T, T>

I'm imagining that your context simply has a Stack<object> and PopINT, PopBOOL etc. just pop the argument and cast. In that case you can probably just use:

public T Pop<T>()
{
    var o = stack.Pop();
    return Convert.ChangeType(o, typeof(T));
} 

public void Push<T>(T item)
{
    stack.Push(item);
} 

Note this could also handle your logical operators - for example:

var logicalAnd = new MathInstruction<bool>((a, b) => a && b);
var logicalOr = new MathInstruction<bool>((a, b) => a || b);
Paul Stovell
  • 32,377
  • 16
  • 80
  • 108
  • I really apreciate your detailed explanation. Thank you for your quickly answer. I was waiting to reply you because I'd never used delegates and I was getting some information about them. – Jose Esperon Mar 30 '12 at 12:10
  • In connection with your answer, I can not use generic data types. I wrote my own type hierarchy and my own stack representation. So, is there any way to adapt your previous code to user created data types? Thank you again. – Jose Esperon Mar 30 '12 at 12:16
  • Can you post an example of your type hierarchy and your implementations of Pop and Push? – Paul Stovell Mar 30 '12 at 15:14
  • Yes, no problem. I'm going to edit my post in order to add that information. Thank you. – Jose Esperon Mar 30 '12 at 15:51
  • Finished. I'm done. If you need more information, please make me know it ;) – Jose Esperon Mar 30 '12 at 16:28
  • I'm sure there must be a way you can generalize the implementation of PopINT into something like Pop which pops sizeof(T) bytes off the stack. That would save you having so many duplicate Pop, Push, Read, Write and so on functions. – Paul Stovell Mar 31 '12 at 01:13
2

Could you use inheritance ? I would see a clever combination of inheritance concerning the datatypes, and then a strategy pattern to delegate the execution to the appropriate objects.

But then we really would need to see a class diagramm to help you out.

Just remember to program to an interface, not a type, and also, composition is more powerful than inheritance. I hope this can help you out.

squelos
  • 1,189
  • 6
  • 16
  • Thank you for your answer. I have my own type hierarchy. So, I'd have to use composition inside instruction classes (swapping 'string type' for 'OwnType type'), then writing "concrete strategies methods" inside my data type classes and, finally, write methods to select the strategy inside Instructions. Right? – Jose Esperon Mar 30 '12 at 13:00