Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with NonGeneric.Emit.Call #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Sigil/Emit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,12 @@ public IEnumerable<OperationResultUsage<DelegateType>> TraceOperationResultUsage
return ret;
}

internal Delegate InnerCreateDelegate(Type delegateType, out string instructions, OptimizationOptions optimizationOptions)
internal Delegate InnerCreateDelegate(Type delegateType, out string instructions, OptimizationOptions optimizationOptions, bool logInstructions = true)
{
Seal(optimizationOptions);

var il = DynMethod.GetILGenerator();
instructions = IL.UnBuffer(il);
instructions = IL.UnBuffer(il, logInstructions);

AutoNamer.Release(this);

Expand Down Expand Up @@ -373,7 +373,7 @@ public DelegateType CreateDelegate(OptimizationOptions optimizationOptions = Opt
/// the returned method fails validation (indicative of a bug in Sigil) or
/// behaves unexpectedly (indicative of a logic bug in the consumer code).
/// </summary>
public MethodBuilder CreateMethod(out string instructions, OptimizationOptions optimizationOptions = OptimizationOptions.All)
public MethodBuilder CreateMethod(out string instructions, OptimizationOptions optimizationOptions = OptimizationOptions.All, bool logInstructions = true)
{
if (MtdBuilder == null)
{
Expand All @@ -391,7 +391,7 @@ public MethodBuilder CreateMethod(out string instructions, OptimizationOptions o
MethodBuilt = true;

var il = MtdBuilder.GetILGenerator();
instructions = IL.UnBuffer(il);
instructions = IL.UnBuffer(il, logInstructions);

AutoNamer.Release(this);

Expand Down Expand Up @@ -430,7 +430,7 @@ public MethodBuilder CreateMethod(OptimizationOptions optimizationOptions = Opti
/// the returned constructor fails validation (indicative of a bug in Sigil) or
/// behaves unexpectedly (indicative of a logic bug in the consumer code).
/// </summary>
public ConstructorBuilder CreateConstructor(out string instructions, OptimizationOptions optimizationOptions = OptimizationOptions.All)
public ConstructorBuilder CreateConstructor(out string instructions, OptimizationOptions optimizationOptions = OptimizationOptions.All, bool logInstructions = true)
{
if (ConstrBuilder == null || !IsBuildingConstructor)
{
Expand All @@ -448,7 +448,7 @@ public ConstructorBuilder CreateConstructor(out string instructions, Optimizatio
Seal(optimizationOptions);

var il = ConstrBuilder.GetILGenerator();
instructions = IL.UnBuffer(il);
instructions = IL.UnBuffer(il, logInstructions);

AutoNamer.Release(this);

Expand Down Expand Up @@ -487,7 +487,7 @@ public ConstructorBuilder CreateConstructor(OptimizationOptions optimizationOpti
/// the returned constructor fails validation (indicative of a bug in Sigil) or
/// behaves unexpectedly (indicative of a logic bug in the consumer code).
/// </summary>
public ConstructorBuilder CreateTypeInitializer(out string instructions, OptimizationOptions optimizationOptions = OptimizationOptions.All)
public ConstructorBuilder CreateTypeInitializer(out string instructions, OptimizationOptions optimizationOptions = OptimizationOptions.All, bool logInstructions = true)
{
if (ConstrBuilder == null || IsBuildingConstructor)
{
Expand All @@ -505,7 +505,7 @@ public ConstructorBuilder CreateTypeInitializer(out string instructions, Optimiz
Seal(optimizationOptions);

var il = ConstrBuilder.GetILGenerator();
instructions = IL.UnBuffer(il);
instructions = IL.UnBuffer(il, logInstructions);

AutoNamer.Release(this);

Expand Down
111 changes: 101 additions & 10 deletions Sigil/Impl/BufferedILGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ public BufferedILGenerator()
{
}

public string UnBuffer(ILGenerator il)
public string UnBuffer(ILGenerator il, bool logInstructions = true)
{
var log = new StringBuilder();
StringBuilder log = null;
if (logInstructions)
log = new StringBuilder();

// First thing will always be a Mark for tracing purposes; no reason to actually do it
for(var i = 2; i < Buffer.Count; i++)
Expand All @@ -88,7 +90,9 @@ public string UnBuffer(ILGenerator il)
x(il, false, log);
}

return log.ToString();
if (log != null)
return log.ToString();
return null;
}

private Dictionary<int, int> LengthCache = new Dictionary<int, int>();
Expand Down Expand Up @@ -253,6 +257,9 @@ public void Insert(int ix, OpCode op)
il.Emit(op);
}

if (log == null)
return;

if (ExtensionMethods.IsPrefix(op))
{
log.Append(op.ToString());
Expand Down Expand Up @@ -289,6 +296,9 @@ public void Emit(OpCode op)
il.Emit(op);
}

if (log == null)
return;

if (ExtensionMethods.IsPrefix(op))
{
log.Append(op.ToString());
Expand Down Expand Up @@ -319,6 +329,9 @@ public void Emit(OpCode op, byte b)
il.Emit(op, b);
}

if (log == null)
return;

if (ExtensionMethods.IsPrefix(op))
{
log.Append(op + "" + b + ".");
Expand Down Expand Up @@ -349,6 +362,9 @@ public void Emit(OpCode op, short s)
il.Emit(op, s);
}

if (log == null)
return;

if (ExtensionMethods.IsPrefix(op))
{
log.Append(op + "" + s + ".");
Expand Down Expand Up @@ -379,6 +395,9 @@ public void Emit(OpCode op, int i)
il.Emit(op, i);
}

if (log == null)
return;

if (ExtensionMethods.IsPrefix(op))
{
log.Append(op + "" + i + ".");
Expand Down Expand Up @@ -414,7 +433,10 @@ public void Emit(OpCode op, uint ui)
{
il.Emit(op, asInt);
}


if (log == null)
return;

log.AppendLine(op + " " + ui);
}
);
Expand All @@ -437,7 +459,10 @@ public void Emit(OpCode op, long l)
{
il.Emit(op, l);
}


if (log == null)
return;

log.AppendLine(op + " " + l);
}
);
Expand Down Expand Up @@ -466,7 +491,10 @@ public void Emit(OpCode op, ulong ul)
{
il.Emit(op, asLong);
}


if (log == null)
return;

log.AppendLine(op + " " + ul);
}
);
Expand All @@ -489,7 +517,10 @@ public void Emit(OpCode op, float f)
{
il.Emit(op, f);
}


if (log == null)
return;

log.AppendLine(op + " " + f);
}
);
Expand All @@ -512,7 +543,10 @@ public void Emit(OpCode op, double d)
{
il.Emit(op, d);
}


if (log == null)
return;

log.AppendLine(op + " " + d);
});

Expand All @@ -535,6 +569,9 @@ public void Emit(OpCode op, MethodInfo method, IEnumerable<Type> parameterTypes)
il.Emit(op, method);
}

if (log == null)
return;

var mtdString = method is MethodBuilder ? method.Name : method.ToString();

log.AppendLine(op + " " + mtdString);
Expand Down Expand Up @@ -573,6 +610,9 @@ public void Emit(OpCode op, ConstructorInfo cons, IEnumerable<Type> parameterTyp
il.Emit(op, cons);
}

if (log == null)
return;

var mtdString = cons is ConstructorBuilder ? cons.Name : cons.ToString();

log.AppendLine(op + " " + mtdString);
Expand Down Expand Up @@ -608,7 +648,10 @@ public void Emit(OpCode op, ConstructorInfo cons)
{
il.Emit(op, cons);
}


if (log == null)
return;

log.AppendLine(op + " " + cons);
}
);
Expand All @@ -632,6 +675,9 @@ public void Emit(OpCode op, Type type)
il.Emit(op, type);
}

if (log == null)
return;

log.AppendLine(op + " " + type);
}
);
Expand All @@ -655,6 +701,9 @@ public void Emit(OpCode op, FieldInfo field)
il.Emit(op, field);
}

if (log == null)
return;

log.AppendLine(op + " " + field);
}
);
Expand All @@ -677,7 +726,10 @@ public void Emit(OpCode op, string str)
{
il.Emit(op, str);
}


if (log == null)
return;

log.AppendLine(op + " '" + str.Replace("'", @"\'") + "'");
}
);
Expand Down Expand Up @@ -712,6 +764,9 @@ public void Emit(OpCode op, Sigil.Label label, out UpdateOpCodeDelegate update)
il.Emit(localOp, l);
}

if (log == null)
return;

log.AppendLine(localOp + " " + label);
}
);
Expand Down Expand Up @@ -746,6 +801,9 @@ public void Emit(OpCode op, Sigil.Label[] labels, out UpdateOpCodeDelegate updat
il.Emit(localOp, ls);
}

if (log == null)
return;

log.AppendLine(localOp + " " + Join(", ", ((LinqArray<Label>)labels).AsEnumerable()));
}
);
Expand Down Expand Up @@ -787,6 +845,9 @@ public void Emit(OpCode op, Sigil.Local local)
il.Emit(op, l);
}

if (log == null)
return;

log.AppendLine(op + " " + local);
}
);
Expand All @@ -810,6 +871,9 @@ public void Emit(OpCode op, CallingConventions callConventions, Type returnType,
il.EmitCalli(op, callConventions, returnType, parameterTypes, null);
}

if (log == null)
return;

log.AppendLine(op + " " + callConventions + " " + returnType + " " + Join(" ", ((LinqArray<Type>)parameterTypes).AsEnumerable()));
}
);
Expand All @@ -836,6 +900,9 @@ public void EmitCall(OpCode op, MethodInfo method, IEnumerable<Type> parameterTy
il.EmitCall(op, method, arglist);
}

if (log == null)
return;

var mtdString = method is MethodBuilder ? method.Name : method.ToString();

log.AppendLine(op + " " + mtdString + " __arglist(" + Join(", ", arglist) + ")");
Expand Down Expand Up @@ -879,6 +946,9 @@ public void EmitCalli(CallingConventions callingConvention, Type returnType, Typ
il.EmitCalli(OpCodes.Calli, callingConvention, returnType, parameterTypes, arglist);
}

if (log == null)
return;

log.AppendLine(OpCodes.Calli + " " + callingConvention + " " + returnType + " " + Join(" ", (IEnumerable<Type>)parameterTypes) + " __arglist(" + Join(", ", arglist) + ")");
}
);
Expand Down Expand Up @@ -928,6 +998,9 @@ public DefineLabelDelegate BeginExceptionBlock()
ret(il);
}

if (log == null)
return;

log.AppendLine("--BeginExceptionBlock--");
}
);
Expand All @@ -953,6 +1026,9 @@ public void BeginCatchBlock(Type exception)
il.BeginCatchBlock(exception);
}

if (log == null)
return;

log.AppendLine("--BeginCatchBlock(" + exception + ")--");
}
);
Expand All @@ -976,6 +1052,9 @@ public void EndExceptionBlock()
il.EndExceptionBlock();
}

if (log == null)
return;

log.AppendLine("--EndExceptionBlock--");
}
);
Expand All @@ -994,6 +1073,9 @@ public void EndCatchBlock()
Buffer.Add(
(il, logOnly, log) =>
{
if (log == null)
return;

log.AppendLine("--EndCatchBlock--");
}
);
Expand All @@ -1017,6 +1099,9 @@ public void BeginFinallyBlock()
il.BeginFinallyBlock();
}

if (log == null)
return;

log.AppendLine("--BeginFinallyBlock--");
}
);
Expand All @@ -1035,6 +1120,9 @@ public void EndFinallyBlock()
Buffer.Add(
(il, logOnly, log) =>
{
if (log == null)
return;

log.AppendLine("--EndFinallyBlock--");
}
);
Expand Down Expand Up @@ -1101,6 +1189,9 @@ public void MarkLabel(Sigil.Label label)
il.MarkLabel(l);
}

if (log == null)
return;

log.AppendLine();
log.AppendLine(label + ":");
}
Expand Down
Loading