Skip to content

Commit

Permalink
Removing most extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-montrose committed Mar 21, 2013
1 parent 3de7274 commit 037ff58
Show file tree
Hide file tree
Showing 18 changed files with 173 additions and 106 deletions.
2 changes: 1 addition & 1 deletion Sigil/Emit.Call.cs
Expand Up @@ -30,7 +30,7 @@ private void InjectTailCall()

if (atJ.IsInstruction.HasValue)
{
if (atJ.IsInstruction.Value.IsTailableCall())
if (ExtensionMethods.IsTailableCall(atJ.IsInstruction.Value))
{
callIx = j;
call = atJ;
Expand Down
2 changes: 1 addition & 1 deletion Sigil/Emit.CallIndirect.cs
Expand Up @@ -375,7 +375,7 @@ public Emit<DelegateType> CallIndirect(CallingConventions callConventions, Type
{
var thisRef = onStack.Last();

if (!funcPtr.InstanceType.IsAssignableFrom(thisRef))
if (!ExtensionMethods.IsAssignableFrom(funcPtr.InstanceType, thisRef))
{
throw new SigilVerificationException("CallIndirect expects a 'this' value assignable to " + funcPtr.InstanceType + ", found " + thisRef, IL.Instructions(AllLocals));
}
Expand Down
2 changes: 1 addition & 1 deletion Sigil/Emit.CastClass.cs
Expand Up @@ -53,7 +53,7 @@ public Emit<DelegateType> CastClass(Type referenceType)
var onStack = stack.First();
if (onStack.All(a => referenceType.IsAssignableFrom(a)))
if (onStack.All(a => ExtensionMethods.IsAssignableFrom(referenceType, a)))
{
ElidableCasts.Add(curIndex);
elided = true;
Expand Down
2 changes: 1 addition & 1 deletion Sigil/Emit.IsInstance.cs
Expand Up @@ -39,7 +39,7 @@ public Emit<DelegateType> IsInstance(Type type)
var onStack = stack.First();
if (onStack.All(a => type.IsAssignableFrom(a)))
if (onStack.All(a => ExtensionMethods.IsAssignableFrom(type, a)))
{
ElidableCasts.Add(curIndex);
elided = true;
Expand Down
2 changes: 1 addition & 1 deletion Sigil/Emit.LoadField.cs
Expand Up @@ -29,7 +29,7 @@ public Emit<DelegateType> LoadField(FieldInfo field, bool? isVolatile = null, in
throw new ArgumentException("unaligned cannot be used with static fields");
}

var useVolatile = isVolatile ?? field.IsVolatile();
var useVolatile = isVolatile ?? ExtensionMethods.IsVolatile(field);

if (!field.IsStatic)
{
Expand Down
14 changes: 7 additions & 7 deletions Sigil/Emit.cs
Expand Up @@ -35,9 +35,9 @@ static Emit()

private LinqList<Local> AllLocals;

private HashSet<Local> UnusedLocals;
private HashSet<Label> UnusedLabels;
private HashSet<Label> UnmarkedLabels;
private LinqHashSet<Local> UnusedLocals;
private LinqHashSet<Label> UnusedLabels;
private LinqHashSet<Label> UnmarkedLabels;

private LinqList<SigilTuple<OpCode, Label, int>> Branches;
private LinqDictionary<Label, int> Marks;
Expand Down Expand Up @@ -152,9 +152,9 @@ private Emit(CallingConventions callConvention, Type returnType, Type[] paramete

AllLocals = new LinqList<Local>();

UnusedLocals = new HashSet<Local>();
UnusedLabels = new HashSet<Label>();
UnmarkedLabels = new HashSet<Label>();
UnusedLocals = new LinqHashSet<Local>();
UnusedLabels = new LinqHashSet<Label>();
UnmarkedLabels = new LinqHashSet<Label>();

Branches = new LinqList<SigilTuple<OpCode, Label, int>>();
Marks = new LinqDictionary<Label, int>();
Expand Down Expand Up @@ -836,7 +836,7 @@ private void InsertInstruction(int index, OpCode instr)
}
}

private HashSet<TrackerDescriber> VerificationCache = new HashSet<TrackerDescriber>();
private LinqHashSet<TrackerDescriber> VerificationCache = new LinqHashSet<TrackerDescriber>();
private void CheckBranchesAndLabels(string method, Label modifiedLabel, bool overrideOpts = false)
{
if (!overrideOpts)
Expand Down
32 changes: 25 additions & 7 deletions Sigil/Impl/BufferedILGenerator.cs
Expand Up @@ -27,6 +27,24 @@ internal class BufferedILInstruction

public Type MethodReturnType { get; internal set; }
public LinqRoot<Type> MethodParameterTypes { get; internal set; }

public bool TakesTypedReference()
{
var instr = this;

if (instr.MethodReturnType == typeof(TypedReference)) return true;

return instr.MethodParameterTypes.Any(p => p == typeof(TypedReference));
}

public bool TakesManagedPointer()
{
var instr = this;

if (instr.MethodReturnType.IsPointer) return true;

return instr.MethodParameterTypes.Any(p => p.IsPointer);
}
}

internal class BufferedILGenerator
Expand All @@ -41,9 +59,9 @@ internal class BufferedILGenerator

public int Index { get { return Buffer.Count; } }

private LinqList<Action<ILGenerator, bool, StringBuilder>> Buffer = new LinqList<Action<ILGenerator, bool, StringBuilder>>();
private LinqList<SigilAction<ILGenerator, bool, StringBuilder>> Buffer = new LinqList<SigilAction<ILGenerator, bool, StringBuilder>>();
private LinqList<BufferedILInstruction> TraversableBuffer = new LinqList<BufferedILInstruction>();
private LinqList<Func<int>> InstructionSizes = new LinqList<Func<int>>();
private LinqList<SigilFunc<int>> InstructionSizes = new LinqList<SigilFunc<int>>();

private Type DelegateType;

Expand Down Expand Up @@ -223,7 +241,7 @@ public void Insert(int ix, OpCode op)
il.Emit(op);
}
if (op.IsPrefix())
if (ExtensionMethods.IsPrefix(op))
{
log.Append(op.ToString());
}
Expand Down Expand Up @@ -257,7 +275,7 @@ public void Emit(OpCode op)
il.Emit(op);
}
if (op.IsPrefix())
if (ExtensionMethods.IsPrefix(op))
{
log.Append(op.ToString());
}
Expand Down Expand Up @@ -285,7 +303,7 @@ public void Emit(OpCode op, byte b)
il.Emit(op, b);
}
if (op.IsPrefix())
if (ExtensionMethods.IsPrefix(op))
{
log.Append(op + "" + b + ".");
}
Expand Down Expand Up @@ -313,7 +331,7 @@ public void Emit(OpCode op, short s)
il.Emit(op, s);
}
if (op.IsPrefix())
if (ExtensionMethods.IsPrefix(op))
{
log.Append(op + "" + s + ".");
}
Expand Down Expand Up @@ -341,7 +359,7 @@ public void Emit(OpCode op, int i)
il.Emit(op, i);
}
if (op.IsPrefix())
if (ExtensionMethods.IsPrefix(op))
{
log.Append(op + "" + i + ".");
}
Expand Down
44 changes: 10 additions & 34 deletions Sigil/Impl/ExtensionMethods.cs
Expand Up @@ -8,39 +8,15 @@ namespace Sigil.Impl
{
internal static class ExtensionMethods
{
public static bool TakesTypedReference(this BufferedILInstruction instr)
{
if (instr.MethodReturnType == typeof(TypedReference)) return true;

return instr.MethodParameterTypes.Any(p => p == typeof(TypedReference));
}

public static bool TakesManagedPointer(this BufferedILInstruction instr)
{
if (instr.MethodReturnType.IsPointer) return true;

return instr.MethodParameterTypes.Any(p => p.IsPointer);
}

public static bool IsTailableCall(this OpCode op)
public static bool IsTailableCall(OpCode op)
{
return
op == OpCodes.Call ||
//op == OpCodes.Calli ||
op == OpCodes.Callvirt;
}

public static bool Any<T>(this List<T> e, Func<T, bool> p)
{
for (var i = 0; i < e.Count; i++)
{
if (p(e[i])) return true;
}

return false;
}

public static bool StartsWithVowel(this string str)
public static bool StartsWithVowel(string str)
{
var c = char.ToLower(str[0]);

Expand All @@ -52,15 +28,15 @@ public static TransitionWrapper Wrap(this IEnumerable<StackTransition> transitio
return TransitionWrapper.Get(method, transitions);
}

public static bool IsVolatile(this FieldInfo field)
public static bool IsVolatile(FieldInfo field)
{
// field builder doesn't implement GetRequiredCustomModifiers
if (field is FieldBuilder) return false;

return Array.IndexOf(field.GetRequiredCustomModifiers(), typeof(System.Runtime.CompilerServices.IsVolatile)) >= 0;
}

public static bool IsPrefix(this OpCode op)
public static bool IsPrefix(OpCode op)
{
return
op == OpCodes.Tailcall ||
Expand All @@ -82,7 +58,7 @@ public static bool IsPrefix(this OpCode op)
{ typeof(UIntPtr), typeof(NativeIntType) }
};

public static Type Alias(this Type t)
private static Type Alias(Type t)
{
if (t.IsValueType && t.IsEnum)
{
Expand All @@ -95,12 +71,12 @@ public static Type Alias(this Type t)
return ret;
}

public static bool IsAssignableFrom(this Type type1, TypeOnStack type2)
public static bool IsAssignableFrom(Type type1, TypeOnStack type2)
{
return TypeOnStack.Get(type1).IsAssignableFrom(type2);
}

public static bool IsAssignableFrom(this TypeOnStack type1, TypeOnStack type2)
public static bool IsAssignableFrom(TypeOnStack type1, TypeOnStack type2)
{
// wildcards match *everything*
if (type1 == TypeOnStack.Get<WildcardType>() || type2 == TypeOnStack.Get<WildcardType>()) return true;
Expand Down Expand Up @@ -135,11 +111,11 @@ public static bool IsAssignableFrom(this TypeOnStack type1, TypeOnStack type2)
return ReallyIsAssignableFrom(t1, t2);
}

private static List<Type> GetBases(Type t)
private static LinqList<Type> GetBases(Type t)
{
if (t.IsValueType) return new List<Type>();
if (t.IsValueType) return new LinqList<Type>();

var ret = new List<Type>();
var ret = new LinqList<Type>();
t = t.BaseType;

while (t != null)
Expand Down

0 comments on commit 037ff58

Please sign in to comment.