public static int Evaluate(Expression exp, int var) { if (exp is ConstantExpression) { ConstantExpression tExp = exp as ConstantExpression; return((int)(exp as ConstantExpression).Value); } else if (exp is VariableExpression) { return(var); } else if (exp is AddExpression) { AddExpression nExp = (AddExpression)exp; return(Evaluate(nExp.OperandA, var) + Evaluate(nExp.OperandB, var)); } else if (exp is SubExpression) { SubExpression nExp = (SubExpression)exp; return(Evaluate(nExp.OperandA, var) - Evaluate(nExp.OperandB, var)); } else if (exp is MulExpression) { MulExpression nExp = (MulExpression)exp; return(Evaluate(nExp.OperandA, var) * Evaluate(nExp.OperandB, var)); } else if (exp is DivExpression) { DivExpression nExp = (DivExpression)exp; return(Evaluate(nExp.OperandA, var) / Evaluate(nExp.OperandB, var)); } else if (exp is NegExpression) { NegExpression nExp = (NegExpression)exp; return(-Evaluate(nExp.Value, var)); } else if (exp is InvExpression) { InvExpression nExp = (InvExpression)exp; return(~Evaluate(nExp.Value, var)); } else if (exp is XorExpression) { XorExpression nExp = (XorExpression)exp; return(Evaluate(nExp.OperandA, var) ^ Evaluate(nExp.OperandB, var)); } throw new NotSupportedException(); }
public override void VisitPostOrder(Expression exp) { x86Register reg = GetFreeRegister(exp); if (exp is ConstantExpression) { insts.Add(new x86Instruction() { OpCode = x86OpCode.MOV, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = reg }, new x86ImmediateOperand() { Immediate = (exp as ConstantExpression).Value } } }); SetRegister(exp, reg); } else if (exp is VariableExpression) { insts.AddRange(args(reg)); SetRegister(exp, reg); } else if (exp is AddExpression) { AddExpression _exp = exp as AddExpression; insts.Add(new x86Instruction() { OpCode = x86OpCode.ADD, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = GetRegister(_exp.OperandA) }, new x86RegisterOperand() { Register = GetRegister(_exp.OperandB) } } }); SetRegister(exp, GetRegister(_exp.OperandA)); ReleaseRegister(_exp.OperandB); } else if (exp is SubExpression) { SubExpression _exp = exp as SubExpression; insts.Add(new x86Instruction() { OpCode = x86OpCode.SUB, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = GetRegister(_exp.OperandA) }, new x86RegisterOperand() { Register = GetRegister(_exp.OperandB) } } }); SetRegister(exp, GetRegister(_exp.OperandA)); ReleaseRegister(_exp.OperandB); } else if (exp is MulExpression) { throw new NotSupportedException(); } else if (exp is DivExpression) { throw new NotSupportedException(); } else if (exp is NegExpression) { NegExpression _exp = exp as NegExpression; insts.Add(new x86Instruction() { OpCode = x86OpCode.NEG, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = GetRegister(_exp.Value) } } }); SetRegister(exp, GetRegister(_exp.Value)); } else if (exp is InvExpression) { InvExpression _exp = exp as InvExpression; insts.Add(new x86Instruction() { OpCode = x86OpCode.NOT, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = GetRegister(_exp.Value) } } }); SetRegister(exp, GetRegister(_exp.Value)); } else if (exp is XorExpression) { XorExpression _exp = exp as XorExpression; insts.Add(new x86Instruction() { OpCode = x86OpCode.XOR, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = GetRegister(_exp.OperandA) }, new x86RegisterOperand() { Register = GetRegister(_exp.OperandB) } } }); SetRegister(exp, GetRegister(_exp.OperandA)); ReleaseRegister(_exp.OperandB); } }