private void DoBinaryOperation(string Operator, CParser.Expression Left, CParser.Expression Right)
{
bool EmitConstantValue = true;
DoGenerateAddress(false, () =>
{
var LeftConstant = Left.GetCachedConstantValue(null);
var RightConstant = Right.GetCachedConstantValue(null);
// Optimization.
if ((LeftConstant != null) && (RightConstant != null))
{
//Console.WriteLine("{0} {1} {2}", LeftConstant, Operator, RightConstant);
if (LeftConstant.GetType() == typeof(int) && RightConstant.GetType() == typeof(int))
{
int LeftConstantValue = (int)LeftConstant;
int RightConstantValue = (int)RightConstant;
switch (Operator)
{
case "+": SafeILGenerator.Push(LeftConstantValue + RightConstantValue); return;
case "-": SafeILGenerator.Push(LeftConstantValue - RightConstantValue); return;
case "*": SafeILGenerator.Push(LeftConstantValue * RightConstantValue); return;
case "/": SafeILGenerator.Push(LeftConstantValue / RightConstantValue); return;
default:
Console.Error.WriteLine("Unoptimized binary operator {0}", Operator);
break;
}
}
}
Traverse(Left);
_DoBinaryLeftRightPost(Operator);
Traverse(Right);
_DoBinaryLeftRightPost(Operator);
EmitConstantValue = false;
});
if (!EmitConstantValue)
{
var LeftCType = Left.GetCachedCType(this).GetCSimpleType();
var RightCType = Right.GetCachedCType(this).GetCSimpleType();
_DoBinaryOperation(Operator, LeftCType.Sign);
}
}