本文整理汇总了C#中Jurassic.Compiler.ILGenerator.Add方法的典型用法代码示例。如果您正苦于以下问题:C# ILGenerator.Add方法的具体用法?C# ILGenerator.Add怎么用?C# ILGenerator.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Jurassic.Compiler.ILGenerator
的用法示例。
在下文中一共展示了ILGenerator.Add方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateIncrementOrDecrement
/// <summary>
/// Generates CIL for an increment or decrement expression.
/// </summary>
/// <param name="generator"> The generator to output the CIL to. </param>
/// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param>
/// <param name="target"> The target to modify. </param>
/// <param name="postfix"> <c>true</c> if this is the postfix version of the operator;
/// <c>false</c> otherwise. </param>
/// <param name="increment"> <c>true</c> if this is the increment operator; <c>false</c> if
/// this is the decrement operator. </param>
private void GenerateIncrementOrDecrement(ILGenerator generator, OptimizationInfo optimizationInfo, IReferenceExpression target, bool postfix, bool increment)
{
// Note: increment and decrement can produce a number that is out of range if the
// target is of type Int32. The only time this should happen is for a loop variable
// where the range has been carefully checked to make sure an out of range condition
// cannot happen.
// Evaluate the left hand side only once.
target.GenerateReference(generator, optimizationInfo);
target.DuplicateReference(generator, optimizationInfo); // For the GenerateSet, later on.
// Get the target value.
target.GenerateGet(generator, optimizationInfo, true);
// Convert it to a number.
if (target.Type != PrimitiveType.Int32)
EmitConversion.ToNumber(generator, target.Type);
// If this is PostIncrement or PostDecrement, store the value so it can be returned later.
var result = generator.CreateTemporaryVariable(target.Type == PrimitiveType.Int32 ? PrimitiveType.Int32 : PrimitiveType.Number);
if (postfix == true)
{
generator.Duplicate();
generator.StoreVariable(result);
}
// Load the increment constant.
if (target.Type == PrimitiveType.Int32)
generator.LoadInt32(1);
else
generator.LoadDouble(1.0);
// Add or subtract the constant to the target value.
if (increment == true)
generator.Add();
else
generator.Subtract();
// If this is PreIncrement or PreDecrement, store the value so it can be returned later.
if (postfix == false)
{
generator.Duplicate();
generator.StoreVariable(result);
}
// Store the value.
target.GenerateSet(generator, optimizationInfo, target.Type == PrimitiveType.Int32 ? PrimitiveType.Int32 : PrimitiveType.Number, optimizationInfo.StrictMode);
// Restore the expression result.
generator.LoadVariable(result);
generator.ReleaseTemporaryVariable(result);
}
示例2: GenerateAdd
/// <summary>
/// Generates CIL for the addition operation.
/// </summary>
/// <param name="generator"> The generator to output the CIL to. </param>
/// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param>
private void GenerateAdd(ILGenerator generator, OptimizationInfo optimizationInfo)
{
// Get the statically-determined types of the left and right operands.
PrimitiveType leftType = this.Left.ResultType;
PrimitiveType rightType = this.Right.ResultType;
// The add operator adds two strings together if at least one of the operands
// is a string, otherwise it adds two numbers.
if (PrimitiveTypeUtilities.IsString(leftType) || PrimitiveTypeUtilities.IsString(rightType))
{
// If at least one of the operands is a string, then the add operator concatenates.
// Load the left-hand side onto the stack.
this.Left.GenerateCode(generator, optimizationInfo);
// Convert the operand to a concatenated string.
EmitConversion.ToPrimitive(generator, leftType, PrimitiveTypeHint.None);
EmitConversion.ToConcatenatedString(generator, leftType);
// Load the right-hand side onto the stack.
this.Right.GenerateCode(generator, optimizationInfo);
if (rightType == PrimitiveType.String)
{
// Concatenate the two strings.
generator.Call(ReflectionHelpers.ConcatenatedString_Concatenate_String);
}
else if (rightType == PrimitiveType.ConcatenatedString)
{
// Concatenate the two strings.
generator.Call(ReflectionHelpers.ConcatenatedString_Concatenate_ConcatenatedString);
}
else
{
// Convert the operand to an object.
EmitConversion.ToPrimitive(generator, rightType, PrimitiveTypeHint.None);
EmitConversion.ToAny(generator, rightType);
// Concatenate the two strings.
generator.Call(ReflectionHelpers.ConcatenatedString_Concatenate_Object);
}
}
else if (leftType != PrimitiveType.Any && leftType != PrimitiveType.Object &&
rightType != PrimitiveType.Any && rightType != PrimitiveType.Object)
{
// Neither of the operands are strings.
// Load the left hand side onto the stack.
this.Left.GenerateCode(generator, optimizationInfo);
// Convert the operand to a number.
EmitConversion.ToNumber(generator, leftType);
// Load the right hand side onto the stack.
this.Right.GenerateCode(generator, optimizationInfo);
// Convert the operand to a number.
EmitConversion.ToNumber(generator, rightType);
// Add the two numbers.
generator.Add();
}
else
{
// It is unknown whether the operands are strings.
// Load the left hand side onto the stack.
this.Left.GenerateCode(generator, optimizationInfo);
EmitConversion.ToAny(generator, leftType);
// Load the right hand side onto the stack.
this.Right.GenerateCode(generator, optimizationInfo);
EmitConversion.ToAny(generator, rightType);
// Add the two objects.
generator.Call(ReflectionHelpers.TypeUtilities_Add);
}
}