本文整理汇总了C#中AstExpression类的典型用法代码示例。如果您正苦于以下问题:C# AstExpression类的具体用法?C# AstExpression怎么用?C# AstExpression使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AstExpression类属于命名空间,在下文中一共展示了AstExpression类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UnboxIfGeneric
private static void UnboxIfGeneric(XTypeReference type, AstExpression node, XTypeSystem typeSystem)
{
if (type.IsGenericParameter || type.IsGenericParameterArray())
{
var resultType = node.GetResultType();
if (resultType.IsByReference && !type.IsByReference)
{
var elementType = resultType.ElementType;
var clone = new AstExpression(node);
node.SetCode(AstCode.SimpleCastclass).SetArguments(clone).Operand = elementType;
}
else
{
if (TreatAsStruct(type, resultType))
{
ConvertUnboxStruct(node, resultType, typeSystem);
}
else
{
var clone = new AstExpression(node);
node.SetCode(AstCode.UnboxFromGeneric).SetArguments(clone).Operand = type;
}
}
}
}
示例2: FindNestedAssignments
private void FindNestedAssignments(AstExpression expr, ExpressionToInfer parent)
{
foreach (var arg in expr.Arguments)
{
if (arg.Code == AstCode.Stloc)
{
var expressionToInfer = new ExpressionToInfer(arg);
allExpressions.Add(expressionToInfer);
FindNestedAssignments(arg, expressionToInfer);
var v = (AstVariable)arg.Operand;
if (v.Type == null)
{
assignmentExpressions[v].Add(expressionToInfer);
// the instruction that consumes the stloc result is handled as if it was reading the variable
parent.Dependencies.Add(v);
}
}
else
{
AstVariable v;
if (arg.Match(AstCode.Ldloc, out v) && (v.Type == null))
{
parent.Dependencies.Add(v);
}
FindNestedAssignments(arg, parent);
}
}
}
示例3: Convert
/// <summary>
/// Optimize expressions
/// </summary>
public static void Convert(AstBlock ast)
{
var variableExpressions = ast.GetExpressions().Where(x => x.Operand is AstVariable).ToList();
var allVariables = variableExpressions.Select(x => (AstVariable)x.Operand).Distinct().ToList();
var allStructVariables = allVariables.Where(x => x.Type.IsStruct()).ToList();
var index = 0;
foreach (var iterator in allStructVariables)
{
var variable = iterator;
if (variable.IsThis || variable.IsParameter)
continue;
if (variableExpressions.Any(x => (x.Operand == variable) && (x.Code == AstCode.Stloc)))
continue;
// Get struct type
var structType = variable.Type.Resolve();
var defaultCtorDef = StructCallConverter.GetDefaultValueCtor(structType);
var defaultCtor = variable.Type.CreateReference(defaultCtorDef);
// Variable is not initialized
var newObjExpr = new AstExpression(ast.SourceLocation, AstCode.Newobj, defaultCtor).SetType(variable.Type);
var initExpr = new AstExpression(ast.SourceLocation, AstCode.Stloc, variable, newObjExpr).SetType(variable.Type);
ast.Body.Insert(index++, initExpr);
}
}
示例4: Convert
/// <summary>
/// Optimize expressions
/// </summary>
public static void Convert(AstNode ast)
{
// Expand typeof
foreach (var pair in ast.GetExpressionPairs())
{
var node = pair.Expression;
switch (node.Code)
{
case AstCode.Ldfld:
//case AstCode.Ldsfld: // NOT YET
{
var field = (XFieldReference) node.Operand;
var clone = new AstExpression(node);
node.SetCode(AstCode.UnboxFromGeneric).SetArguments(clone).Operand = field.FieldType;
}
break;
case AstCode.Call:
case AstCode.Calli:
case AstCode.Callvirt:
{
var method = (XMethodReference)node.Operand;
if ((!method.ReturnType.IsVoid()) && (pair.Parent != null))
{
var clone = new AstExpression(node);
node.SetCode(AstCode.UnboxFromGeneric).SetArguments(clone).Operand = method.ReturnType;
}
}
break;
}
}
}
示例5: ConvertCastclass
/// <summary>
/// Convert node with code Cast.
/// </summary>
private static void ConvertCastclass(AssemblyCompiler compiler, AstExpression node, XTypeSystem typeSystem)
{
var type = (XTypeReference) node.Operand;
if (type.IsSystemArray())
{
// Call cast method
var arrayHelper = compiler.GetDot42InternalType(InternalConstants.CompilerHelperName).Resolve();
var castToArray = arrayHelper.Methods.First(x => x.Name == "CastToArray");
var castToArrayExpr = new AstExpression(node.SourceLocation, AstCode.Call, castToArray, node.Arguments).SetType(type);
node.CopyFrom(castToArrayExpr);
return;
}
string castMethod = null;
if (type.IsSystemCollectionsIEnumerable())
{
castMethod = "CastToEnumerable";
}
else if (type.IsSystemCollectionsICollection())
{
castMethod = "CastToCollection";
}
else if (type.IsSystemCollectionsIList())
{
castMethod = "CastToList";
}
else if (type.IsSystemIFormattable())
{
castMethod = "CastToFormattable";
}
if (castMethod != null)
{
// Call cast method
var arrayHelper = compiler.GetDot42InternalType(InternalConstants.CompilerHelperName).Resolve();
var castToArray = arrayHelper.Methods.First(x => x.Name == castMethod);
// Call "(x instanceof T) ? (T)x : asMethod(x)"
// "instanceof x"
var instanceofExpr = new AstExpression(node.SourceLocation, AstCode.SimpleInstanceOf, type, node.Arguments[0]).SetType(typeSystem.Bool);
// CastX(x)
var castXExpr = new AstExpression(node.SourceLocation, AstCode.Call, castToArray, node.Arguments[0]).SetType(typeSystem.Object);
// T(x)
var txExpr = new AstExpression(node.SourceLocation, AstCode.SimpleCastclass, type, node.Arguments[0]).SetType(type);
// Combine
var conditional = new AstExpression(node.SourceLocation, AstCode.Conditional, type, instanceofExpr, txExpr, castXExpr).SetType(type);
node.CopyFrom(conditional);
return;
}
// Normal castclass
node.Code = AstCode.SimpleCastclass;
}
示例6: SimplifyLdObjAndStObj
static AstExpression SimplifyLdObjAndStObj(AstExpression expr, ref bool modified)
{
if (expr.Code == AstCode.Initobj)
{
expr.Code = AstCode.Stobj;
expr.Arguments.Add(new AstExpression(expr.SourceLocation, AstCode.DefaultValue, expr.Operand));
modified = true;
}
else if (expr.Code == AstCode.Cpobj)
{
expr.Code = AstCode.Stobj;
expr.Arguments[1] = new AstExpression(expr.SourceLocation, AstCode.Ldobj, expr.Operand, expr.Arguments[1]);
modified = true;
}
AstExpression arg, arg2;
XTypeReference type;
AstCode? newCode = null;
if (expr.Match(AstCode.Stobj, out type, out arg, out arg2))
{
switch (arg.Code)
{
case AstCode.Ldelema: newCode = AstCode.Stelem_Any; break;
case AstCode.Ldloca: newCode = AstCode.Stloc; break;
case AstCode.Ldflda: newCode = AstCode.Stfld; break;
case AstCode.Ldsflda: newCode = AstCode.Stsfld; break;
}
}
else if (expr.Match(AstCode.Ldobj, out type, out arg))
{
switch (arg.Code)
{
case AstCode.Ldelema: newCode = AstCode.Ldelem_Any; break;
case AstCode.Ldloca: newCode = AstCode.Ldloc; break;
case AstCode.Ldflda: newCode = AstCode.Ldfld; break;
case AstCode.Ldsflda: newCode = AstCode.Ldsfld; break;
}
}
if (newCode != null)
{
arg.Code = newCode.Value;
if (expr.Code == AstCode.Stobj)
{
arg.InferredType = expr.InferredType;
arg.ExpectedType = expr.ExpectedType;
arg.Arguments.Add(arg2);
}
arg.ILRanges.AddRange(expr.ILRanges);
modified = true;
return arg;
}
else
{
return expr;
}
}
示例7: Convert
/// <summary>
/// Convert the result of the given node to uint8/uint16 if needed.
/// </summary>
private static void Convert(AstExpression node, AstCode convertCode, XTypeReference expectedType)
{
// Copy load expression
var clone = new AstExpression(node);
// Convert node
node.Code = convertCode;
node.SetArguments(clone);
node.Operand = null;
node.SetType(expectedType);
}
示例8: Exit
public RLRange Exit(AstCompilerVisitor compVisit, AstExpression node, List<RLRange> args)
{
var reg = GetMonitorRegister(compVisit, node);
if (reg != null)
{
var first = compVisit.Add(node.SourceLocation, RCode.Move_object, reg, args[0].Result);
var second = compVisit.Add(node.SourceLocation, RCode.Monitor_exit, reg);
return new RLRange(first, second, null);
}
// default handling
return new RLRange(compVisit.Add(node.SourceLocation, RCode.Monitor_exit, args[0].Result), null);
}
示例9: TransformDecimalCtorToConstant
static bool TransformDecimalCtorToConstant(List<AstNode> body, AstExpression expr, int pos)
{
XMethodReference r;
List<AstExpression> args;
if (expr.Match(AstCode.Newobj, out r, out args) && r.DeclaringType.IsSystemDecimal())
{
//if (args.Count == 1)
//{
// int val;
// if (args[0].Match(AstCode.Ldc_I4, out val))
// {
// expr.Code = AstCode.Ldc_Decimal;
// expr.Operand = new decimal(val);
// expr.InferredType = r.DeclaringType;
// expr.Arguments.Clear();
// return true;
// }
//}
//else
if (args.Count == 5)
{
int lo, mid, hi, isNegative, scale;
if (expr.Arguments[0].Match(AstCode.Ldc_I4, out lo) &&
expr.Arguments[1].Match(AstCode.Ldc_I4, out mid) &&
expr.Arguments[2].Match(AstCode.Ldc_I4, out hi) &&
expr.Arguments[3].Match(AstCode.Ldc_I4, out isNegative) &&
expr.Arguments[4].Match(AstCode.Ldc_I4, out scale))
{
// convert to a static string conversion method.
var dec = new decimal(lo, mid, hi, isNegative != 0, (byte)scale);
var str = dec.ToString(CultureInfo.InvariantCulture);
expr.Code = AstCode.Call;
expr.Operand = r.DeclaringType.Resolve().Methods.First(p => p.Name == "FromInvariantString" && p.IsStatic && p.Parameters.Count == 1);
expr.Arguments.Clear();
expr.Arguments.Add(new AstExpression(expr.SourceLocation, AstCode.Ldstr, str));
expr.InferredType = r.DeclaringType;
return true;
}
}
}
bool modified = false;
foreach (AstExpression arg in expr.Arguments)
{
modified |= TransformDecimalCtorToConstant(null, arg, -1);
}
return modified;
}
示例10: GetMonitorRegister
public Register GetMonitorRegister(AstCompilerVisitor compVisit, AstExpression node)
{
// this code is still experimental, and possibly does not handle all cases.
var operand = node.Arguments[0].Operand;
Register reg = null;
if (operand is XFieldReference)
{
if (!_monitorUsage.TryGetValue(operand, out reg))
{
reg = compVisit.Frame.AllocateTemp(FrameworkReferences.Object);
_monitorUsage.Add(operand, reg);
}
}
return reg;
}
示例11: Convert
/// <summary>
/// Optimize expressions
/// </summary>
public static void Convert(AstNode ast)
{
foreach (var node in ast.GetExpressions())
{
if (node.Code == AstCode.Ldloc)
{
var variable = (AstVariable) node.Operand;
var varType = variable.Type;
var expType = node.ExpectedType;
if ((expType != null) && (!varType.IsSame(expType)))
{
if (varType.IsByte() && (expType.IsChar() || expType.IsUInt16()))
{
var ldloc = new AstExpression(node);
var code = expType.IsUInt16() ? AstCode.Int_to_ushort : AstCode.Conv_U2;
node.CopyFrom(new AstExpression(node.SourceLocation, code, null, ldloc).SetType(expType));
}
}
}
else if (node.Code.IsCall())
{
var methodRef = (XMethodReference) node.Operand;
var returnType = methodRef.ReturnType;
var expType = node.ExpectedType;
if ((expType != null) && (!returnType.IsSame(expType)))
{
if (returnType.IsByte() && (expType.IsChar() || expType.IsUInt16()))
{
var ldloc = new AstExpression(node);
var code = expType.IsUInt16() ? AstCode.Int_to_ushort : AstCode.Conv_U2;
node.CopyFrom(new AstExpression(node.SourceLocation, code, null, ldloc).SetType(expType));
}
else if (returnType.IsUInt16() && expType.IsChar())
{
var ldloc = new AstExpression(node);
node.CopyFrom(new AstExpression(node.SourceLocation, AstCode.Conv_U2, null, ldloc).SetType(expType));
}
else if (returnType.IsChar() && expType.IsUInt16())
{
var ldloc = new AstExpression(node);
node.CopyFrom(new AstExpression(node.SourceLocation, AstCode.Int_to_ushort, null, ldloc).SetType(expType));
}
}
}
}
}
示例12: ConvertIfNeeded
/// <summary>
/// Convert the result of the given node to uint8/uint16 if needed.
/// </summary>
private static void ConvertIfNeeded(AstExpression node, XTypeReference valueType, AstExpression parent)
{
AstCode convCode;
if (valueType.IsByte())
{
// Convert from int to uint8
convCode = AstCode.Int_to_ubyte;
}
else if (valueType.IsUInt16())
{
// Convert from int to uint16
convCode = AstCode.Int_to_ushort;
}
else if (valueType.IsChar())
{
// Convert from int to uint16
convCode = AstCode.Conv_U2;
}
else
{
// Do not convert
return;
}
// Avoid recursion
if ((parent != null) && (parent.Code == convCode))
return;
// Copy load expression
var clone = new AstExpression(node);
// Convert node
node.Code = convCode;
node.Arguments.Clear();
node.Arguments.Add(clone);
node.Operand = null;
node.InferredType = valueType;
// keep the expected type!
node.ExpectedType = clone.ExpectedType;
clone.ExpectedType = valueType;
}
示例13: ConvertIfNeeded
/// <summary>
/// Convert the given argument if it does not match the target type.
/// </summary>
private static void ConvertIfNeeded(AstExpression arg, XTypeReference targetType)
{
var argType = arg.GetResultType();
if (!targetType.IsSame(argType))
{
if (targetType.IsChar())
{
if (argType.IsUInt16() || argType.IsByte()) Convert(arg, AstCode.Conv_U2, targetType);
}
else if (targetType.IsUInt16())
{
if (argType.IsChar() || argType.IsByte()) Convert(arg, AstCode.Int_to_ushort, targetType);
}
else if (targetType.IsInt16())
{
if (argType.IsChar() || argType.IsByte()) Convert(arg, AstCode.Conv_I2, targetType);
}
}
}
示例14: Convert
/// <summary>
/// Optimize expressions
/// </summary>
public static void Convert(AstNode ast)
{
// Optimize brtrue (cxx(int, int)) expressions
foreach (var node in ast.GetExpressions(x => (x.Code == AstCode.Brtrue) || (x.Code == AstCode.Brfalse)))
{
var expr = node.Arguments[0];
if (expr.Code.IsCompare() && (expr.Arguments.Count == 2))
{
var arg1 = expr.Arguments[0];
var arg2 = expr.Arguments[1];
if (arg1.IsInt32() && arg2.IsInt32())
{
// Simplify
var code = (node.Code == AstCode.Brtrue) ? expr.Code : expr.Code.Reverse();
var newExpr = new AstExpression(expr.SourceLocation, code.ToBranch(), node.Operand, arg1, arg2);
node.CopyFrom(newExpr, true);
}
}
}
}
示例15: AddFieldInitializationCode
/// <summary>
/// Add initialization code to the given constructor for non-initialized struct fields.
/// </summary>
private static void AddFieldInitializationCode(MethodSource ctor, AstBlock ast)
{
List<XFieldDefinition> fieldsToInitialize;
var declaringType = ctor.Method.DeclaringType;
var fieldsThatMayNeedInitialization = declaringType.Fields.Where(x => x.IsReachable && x.FieldType.IsEnum());
var index = 0;
if (ctor.Method.IsStatic)
{
fieldsToInitialize = fieldsThatMayNeedInitialization.Where(x => x.IsStatic && !IsInitialized(ast, x)).ToList();
foreach (var field in fieldsToInitialize)
{
var defaultExpr = new AstExpression(ast.SourceLocation, AstCode.DefaultValue, field.FieldType);
var initExpr = new AstExpression(ast.SourceLocation, AstCode.Stsfld, field, defaultExpr);
ast.Body.Insert(index++, initExpr);
}
}
else
{
// If there is a this ctor being called, we do not have to initialize here.
var thisCalls = ast.GetSelfAndChildrenRecursive<AstExpression>(x => (x.Code == AstCode.Call) && ((XMethodReference) x.Operand).DeclaringType.IsSame(declaringType));
if (thisCalls.Any(x => ((XMethodReference) x.Operand).Name == ".ctor"))
return;
fieldsToInitialize = fieldsThatMayNeedInitialization.Where(x => !x.IsStatic && !IsInitialized(ast, x)).ToList();
if (fieldsToInitialize.Any())
{
var baseCall = FindFirstBaseCtorCall(ast, declaringType);
var initExpressions = new List<AstExpression>();
foreach (var field in fieldsToInitialize)
{
var thisExpr = new AstExpression(ast.SourceLocation, AstCode.Ldthis, null);
var defaultExpr = new AstExpression(ast.SourceLocation, AstCode.DefaultValue, field.FieldType);
initExpressions.Add(new AstExpression(ast.SourceLocation, AstCode.Stfld, field, thisExpr, defaultExpr));
}
InsertAfter(ast, baseCall, initExpressions);
}
}
}