Skip to content

Commit

Permalink
Added option logging parameter
Browse files Browse the repository at this point in the history
Co-authored-by: chaplin89 <[email protected]>
  • Loading branch information
arlm and weddingmm committed Aug 7, 2019
1 parent 49d8390 commit abca9c5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 54 deletions.
20 changes: 10 additions & 10 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 All @@ -325,7 +325,7 @@ internal Delegate InnerCreateDelegate(Type delegateType, out string instructions
/// the returned delegate fails validation (indicative of a bug in Sigil) or
/// behaves unexpectedly (indicative of a logic bug in the consumer code).
/// </summary>
public DelegateType CreateDelegate(out string instructions, OptimizationOptions optimizationOptions = OptimizationOptions.All)
public DelegateType CreateDelegate(out string instructions, OptimizationOptions optimizationOptions = OptimizationOptions.All, bool logInstructions = true)
{
if (DynMethod == null)
{
Expand All @@ -338,7 +338,7 @@ public DelegateType CreateDelegate(out string instructions, OptimizationOptions
return CreatedDelegate;
}

CreatedDelegate = (DelegateType)(object)InnerCreateDelegate(typeof(DelegateType), out instructions, optimizationOptions);
CreatedDelegate = (DelegateType)(object)InnerCreateDelegate(typeof(DelegateType), out instructions, optimizationOptions, logInstructions);

return CreatedDelegate;
}
Expand Down Expand Up @@ -372,7 +372,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 @@ -390,7 +390,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 @@ -428,7 +428,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 @@ -446,7 +446,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 @@ -484,7 +484,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 @@ -502,7 +502,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
67 changes: 33 additions & 34 deletions Sigil/Impl/BufferedILGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
Expand Down Expand Up @@ -78,9 +77,9 @@ public BufferedILGenerator()
{
}

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

// 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 @@ -90,7 +89,7 @@ public string UnBuffer(ILGenerator il)
x(il, false, log);
}

return log.ToString();
return log?.ToString() ?? string.Empty;
}

private Dictionary<int, int> LengthCache = new Dictionary<int, int>();
Expand Down Expand Up @@ -261,7 +260,7 @@ public void Insert(int ix, OpCode op)
}
else
{
log.AppendLine(op.ToString());
log?.AppendLine(op.ToString());
}
}
);
Expand Down Expand Up @@ -297,7 +296,7 @@ public void Emit(OpCode op)
}
else
{
log.AppendLine(op.ToString());
log?.AppendLine(op.ToString());
}
}
);
Expand Down Expand Up @@ -327,7 +326,7 @@ public void Emit(OpCode op, byte b)
}
else
{
log.AppendLine(op + " " + b);
log?.AppendLine(op + " " + b);
}
}
);
Expand Down Expand Up @@ -357,7 +356,7 @@ public void Emit(OpCode op, short s)
}
else
{
log.AppendLine(op + " " + s);
log?.AppendLine(op + " " + s);
}
}
);
Expand Down Expand Up @@ -387,7 +386,7 @@ public void Emit(OpCode op, int i)
}
else
{
log.AppendLine(op + " " + i);
log?.AppendLine(op + " " + i);
}
}
);
Expand Down Expand Up @@ -417,7 +416,7 @@ public void Emit(OpCode op, uint ui)
il.Emit(op, asInt);
}

log.AppendLine(op + " " + ui);
log?.AppendLine(op + " " + ui);
}
);

Expand All @@ -440,7 +439,7 @@ public void Emit(OpCode op, long l)
il.Emit(op, l);
}

log.AppendLine(op + " " + l);
log?.AppendLine(op + " " + l);
}
);

Expand Down Expand Up @@ -469,7 +468,7 @@ public void Emit(OpCode op, ulong ul)
il.Emit(op, asLong);
}

log.AppendLine(op + " " + ul);
log?.AppendLine(op + " " + ul);
}
);

Expand All @@ -492,7 +491,7 @@ public void Emit(OpCode op, float f)
il.Emit(op, f);
}

log.AppendLine(op + " " + f.ToString(CultureInfo.InvariantCulture));
log?.AppendLine(op + " " + f.ToString(CultureInfo.InvariantCulture));
}
);

Expand All @@ -515,7 +514,7 @@ public void Emit(OpCode op, double d)
il.Emit(op, d);
}

log.AppendLine(op + " " + d.ToString(CultureInfo.InvariantCulture));
log?.AppendLine(op + " " + d.ToString(CultureInfo.InvariantCulture));
});

TraversableBuffer.Add(new BufferedILInstruction { IsInstruction = op });
Expand All @@ -539,7 +538,7 @@ public void Emit(OpCode op, MethodInfo method, IEnumerable<Type> parameterTypes)

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

log.AppendLine(op + " " + mtdString);
log?.AppendLine(op + " " + mtdString);
}
);

Expand Down Expand Up @@ -577,7 +576,7 @@ public void Emit(OpCode op, ConstructorInfo cons, IEnumerable<Type> parameterTyp

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

log.AppendLine(op + " " + mtdString);
log?.AppendLine(op + " " + mtdString);
}
);

Expand Down Expand Up @@ -611,7 +610,7 @@ public void Emit(OpCode op, ConstructorInfo cons)
il.Emit(op, cons);
}

log.AppendLine(op + " " + cons);
log?.AppendLine(op + " " + cons);
}
);

Expand All @@ -634,7 +633,7 @@ public void Emit(OpCode op, Type type)
il.Emit(op, type);
}

log.AppendLine(op + " " + type);
log?.AppendLine(op + " " + type);
}
);

Expand All @@ -657,7 +656,7 @@ public void Emit(OpCode op, FieldInfo field)
il.Emit(op, field);
}

log.AppendLine(op + " " + field);
log?.AppendLine(op + " " + field);
}
);

Expand All @@ -680,7 +679,7 @@ public void Emit(OpCode op, string str)
il.Emit(op, str);
}

log.AppendLine(op + " '" + str.Replace("'", @"\'") + "'");
log?.AppendLine(op + " '" + str.Replace("'", @"\'") + "'");
}
);

Expand Down Expand Up @@ -714,7 +713,7 @@ public void Emit(OpCode op, Sigil.Label label, out UpdateOpCodeDelegate update)
il.Emit(localOp, l);
}

log.AppendLine(localOp + " " + label);
log?.AppendLine(localOp + " " + label);
}
);

Expand Down Expand Up @@ -748,7 +747,7 @@ public void Emit(OpCode op, Sigil.Label[] labels, out UpdateOpCodeDelegate updat
il.Emit(localOp, ls);
}

log.AppendLine(localOp + " " + Join(", ", ((LinqArray<Label>)labels).AsEnumerable()));
log?.AppendLine(localOp + " " + Join(", ", ((LinqArray<Label>)labels).AsEnumerable()));
}
);

Expand Down Expand Up @@ -789,7 +788,7 @@ public void Emit(OpCode op, Sigil.Local local)
il.Emit(op, l);
}

log.AppendLine(op + " " + local);
log?.AppendLine(op + " " + local);
}
);

Expand All @@ -812,7 +811,7 @@ public void Emit(OpCode op, CallingConventions callConventions, Type returnType,
il.EmitCalli(op, callConventions, returnType, parameterTypes, null);
}

log.AppendLine(op + " " + callConventions + " " + returnType + " " + Join(" ", ((LinqArray<Type>)parameterTypes).AsEnumerable()));
log?.AppendLine(op + " " + callConventions + " " + returnType + " " + Join(" ", ((LinqArray<Type>)parameterTypes).AsEnumerable()));
}
);

Expand Down Expand Up @@ -840,7 +839,7 @@ public void EmitCall(OpCode op, MethodInfo method, IEnumerable<Type> parameterTy

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

log.AppendLine(op + " " + mtdString + " __arglist(" + Join(", ", arglist) + ")");
log?.AppendLine(op + " " + mtdString + " __arglist(" + Join(", ", arglist) + ")");
}
);

Expand Down Expand Up @@ -881,7 +880,7 @@ public void EmitCalli(CallingConventions callingConvention, Type returnType, Typ
il.EmitCalli(OpCodes.Calli, callingConvention, returnType, parameterTypes, arglist);
}

log.AppendLine(OpCodes.Calli + " " + callingConvention + " " + returnType + " " + Join(" ", (IEnumerable<Type>)parameterTypes) + " __arglist(" + Join(", ", arglist) + ")");
log?.AppendLine(OpCodes.Calli + " " + callingConvention + " " + returnType + " " + Join(" ", (IEnumerable<Type>)parameterTypes) + " __arglist(" + Join(", ", arglist) + ")");
}
);

Expand Down Expand Up @@ -930,7 +929,7 @@ public DefineLabelDelegate BeginExceptionBlock()
ret(il);
}

log.AppendLine("--BeginExceptionBlock--");
log?.AppendLine("--BeginExceptionBlock--");
}
);

Expand All @@ -955,7 +954,7 @@ public void BeginCatchBlock(Type exception)
il.BeginCatchBlock(exception);
}

log.AppendLine("--BeginCatchBlock(" + exception + ")--");
log?.AppendLine("--BeginCatchBlock(" + exception + ")--");
}
);

Expand All @@ -978,7 +977,7 @@ public void EndExceptionBlock()
il.EndExceptionBlock();
}

log.AppendLine("--EndExceptionBlock--");
log?.AppendLine("--EndExceptionBlock--");
}
);

Expand All @@ -996,7 +995,7 @@ public void EndCatchBlock()
Buffer.Add(
(il, logOnly, log) =>
{
log.AppendLine("--EndCatchBlock--");
log?.AppendLine("--EndCatchBlock--");
}
);

Expand All @@ -1019,7 +1018,7 @@ public void BeginFinallyBlock()
il.BeginFinallyBlock();
}

log.AppendLine("--BeginFinallyBlock--");
log?.AppendLine("--BeginFinallyBlock--");
}
);

Expand All @@ -1037,7 +1036,7 @@ public void EndFinallyBlock()
Buffer.Add(
(il, logOnly, log) =>
{
log.AppendLine("--EndFinallyBlock--");
log?.AppendLine("--EndFinallyBlock--");
}
);

Expand Down Expand Up @@ -1103,8 +1102,8 @@ public void MarkLabel(Sigil.Label label)
il.MarkLabel(l);
}

log.AppendLine();
log.AppendLine(label + ":");
log?.AppendLine();
log?.AppendLine(label + ":");
}
);

Expand Down
Loading

0 comments on commit abca9c5

Please sign in to comment.