本文整理汇总了C#中ProtoBuf.Compiler.CodeLabel类的典型用法代码示例。如果您正苦于以下问题:C# CodeLabel类的具体用法?C# CodeLabel怎么用?C# CodeLabel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CodeLabel类属于ProtoBuf.Compiler命名空间,在下文中一共展示了CodeLabel类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MarkLabel
internal void MarkLabel(CodeLabel label)
{
il.MarkLabel(label.Value);
#if DEBUG_COMPILE
Helpers.DebugWriteLine("#: " + label.Index);
#endif
}
示例2: EndTry
internal void EndTry(CodeLabel label, bool @short)
{
OpCode code = @short ? OpCodes.Leave_S : OpCodes.Leave;
il.Emit(code, label.Value);
#if DEBUG_COMPILE
Helpers.DebugWriteLine(code + ": " + label.Index);
#endif
}
示例3: DefineLabel
internal CodeLabel DefineLabel()
{
CodeLabel result = new CodeLabel(il.DefineLabel(), nextLabel++);
return result;
}
示例4: BranchIfTrue
internal void BranchIfTrue(CodeLabel label, bool @short)
{
OpCode code = @short ? OpCodes.Brtrue_S : OpCodes.Brtrue;
il.Emit(code, label.Value);
#if DEBUG_COMPILE
Helpers.DebugWriteLine(code + ": " + label.Index);
#endif
}
示例5: BeginTry
internal CodeLabel BeginTry()
{
CodeLabel label = new CodeLabel(il.BeginExceptionBlock(), nextLabel++);
#if DEBUG_COMPILE
Helpers.DebugWriteLine("BeginExceptionBlock: " + label.Index);
#endif
return label;
}
示例6: Switch
public void Switch(CodeLabel[] jumpTable)
{
Label[] labels = new Label[jumpTable.Length];
#if DEBUG_COMPILE
StringBuilder sb = new StringBuilder(OpCodes.Switch.ToString());
#endif
for (int i = 0; i < labels.Length; i++)
{
labels[i] = jumpTable[i].Value;
#if DEBUG_COMPILE
sb.Append("; ").Append(i).Append("=>").Append(jumpTable[i].Index);
#endif
}
il.Emit(OpCodes.Switch, labels);
#if DEBUG_COMPILE
Helpers.DebugWriteLine(sb.ToString());
#endif
}
示例7: Dispose
public void Dispose()
{
if (local == null || ctx == null) return;
ctx.EndTry(label, false);
ctx.BeginFinally();
Type disposableType = ctx.MapType(typeof (IDisposable));
MethodInfo dispose = disposableType.GetMethod("Dispose");
Type type = local.Type;
// remember that we've already (in the .ctor) excluded the case
// where it *cannot* be disposable
if (type.IsValueType)
{
ctx.LoadAddress(local, type);
switch (ctx.MetadataVersion)
{
case ILVersion.Net1:
ctx.LoadValue(local);
ctx.CastToObject(type);
break;
default:
#if FX11
throw new NotSupportedException();
#else
ctx.Constrain(type);
break;
#endif
}
ctx.EmitCall(dispose);
}
else
{
Compiler.CodeLabel @null = ctx.DefineLabel();
if (disposableType.IsAssignableFrom(type))
{ // *known* to be IDisposable; just needs a null-check
ctx.LoadValue(local);
ctx.BranchIfFalse(@null, true);
ctx.LoadAddress(local, type);
}
else
{ // *could* be IDisposable; test via "as"
using (Compiler.Local disp = new Compiler.Local(ctx, disposableType))
{
ctx.LoadValue(local);
ctx.TryCast(disposableType);
ctx.CopyValue();
ctx.StoreValue(disp);
ctx.BranchIfFalse(@null, true);
ctx.LoadAddress(disp, disposableType);
}
}
ctx.EmitCall(dispose);
ctx.MarkLabel(@null);
}
ctx.EndFinally();
this.local = null;
this.ctx = null;
label = new CodeLabel(); // default
}
示例8: Switch
public void Switch(CodeLabel[] jumpTable)
{
const int MAX_JUMPS = 128;
if (jumpTable.Length <= MAX_JUMPS)
{
// simple case
Label[] labels = new Label[jumpTable.Length];
for (int i = 0; i < labels.Length; i++)
{
labels[i] = jumpTable[i].Value;
}
#if DEBUG_COMPILE
Helpers.DebugWriteLine(OpCodes.Switch.ToString());
#endif
il.Emit(OpCodes.Switch, labels);
}
else
{
// too many to jump easily (especially on Android) - need to split up (note: uses a local pulled from the stack)
using (Local val = GetLocalWithValue(MapType(typeof(int)), null))
{
int count = jumpTable.Length, offset = 0;
int blockCount = count / MAX_JUMPS;
if ((count % MAX_JUMPS) != 0) blockCount++;
Label[] blockLabels = new Label[blockCount];
for (int i = 0; i < blockCount; i++)
{
blockLabels[i] = il.DefineLabel();
}
CodeLabel endOfSwitch = DefineLabel();
LoadValue(val);
LoadValue(MAX_JUMPS);
Emit(OpCodes.Div);
#if DEBUG_COMPILE
Helpers.DebugWriteLine(OpCodes.Switch.ToString());
#endif
il.Emit(OpCodes.Switch, blockLabels);
Branch(endOfSwitch, false);
Label[] innerLabels = new Label[MAX_JUMPS];
for (int blockIndex = 0; blockIndex < blockCount; blockIndex++)
{
il.MarkLabel(blockLabels[blockIndex]);
int itemsThisBlock = Math.Min(MAX_JUMPS, count);
count -= itemsThisBlock;
if (innerLabels.Length != itemsThisBlock) innerLabels = new Label[itemsThisBlock];
int subtract = offset;
for (int j = 0; j < itemsThisBlock; j++)
{
innerLabels[j] = jumpTable[offset++].Value;
}
LoadValue(val);
if (subtract != 0) // switches are always zero-based
{
LoadValue(subtract);
Emit(OpCodes.Sub);
}
#if DEBUG_COMPILE
Helpers.DebugWriteLine(OpCodes.Switch.ToString());
#endif
il.Emit(OpCodes.Switch, innerLabels);
if (count != 0)
{ // force default to the very bottom
Branch(endOfSwitch, false);
}
}
Helpers.DebugAssert(count == 0, "Should use exactly all switch items");
MarkLabel(endOfSwitch);
}
}
}
示例9: Switch
public void Switch(CodeLabel[] jumpTable)
{
const int MAX_JUMPS = 128;
// if too many jumps, push the value into a local
using(Local val = jumpTable.Length >= MAX_JUMPS ? GetLocalWithValue(MapType(typeof(int)),null) : null)
{
int count = jumpTable.Length, offset = 0;
do
{
// if multi-switch, need to load the value again, and offset if necessary
if (val != null)
{
LoadValue(val);
if (offset != 0)
{
LoadValue(offset);
Subtract();
}
}
int itemsThisIteration = Math.Min(count, MAX_JUMPS);
count -= itemsThisIteration;
Label[] labels = new Label[itemsThisIteration];
#if DEBUG_COMPILE
StringBuilder sb = new StringBuilder(OpCodes.Switch.ToString());
#endif
for (int i = 0; i < labels.Length; i++)
{
labels[i] = jumpTable[offset++].Value;
#if DEBUG_COMPILE
sb.Append("; ").Append(i).Append("=>").Append(jumpTable[i].Index);
#endif
}
il.Emit(OpCodes.Switch, labels);
#if DEBUG_COMPILE
Helpers.DebugWriteLine(sb.ToString());
#endif
} while (count > 0);
}
}
示例10: Dispose
public void Dispose()
{
if (local == null || ctx == null) return;
ctx.EndTry(label, false);
ctx.BeginFinally();
MethodInfo dispose = typeof(IDisposable).GetMethod("Dispose");
Type type = local.Type;
// remember that we've already (in the .ctor) excluded the case
// where it *cannot* be disposable
if (type.IsValueType)
{
ctx.LoadAddress(local, type);
ctx.Constrain(type);
ctx.EmitCall(dispose);
}
else
{
Compiler.CodeLabel @null = ctx.DefineLabel();
if (typeof(IDisposable).IsAssignableFrom(type))
{ // *known* to be IDisposable; just needs a null-check
ctx.LoadValue(local);
ctx.BranchIfFalse(@null, true);
ctx.LoadAddress(local, type);
}
else
{ // *could* be IDisposable; test via "as"
using (Compiler.Local disp = new Compiler.Local(ctx, typeof(IDisposable)))
{
ctx.LoadValue(local);
ctx.TryCast(typeof(IDisposable));
ctx.CopyValue();
ctx.StoreValue(disp);
ctx.BranchIfFalse(@null, true);
ctx.LoadAddress(disp, typeof(IDisposable));
}
}
ctx.EmitCall(dispose);
ctx.MarkLabel(@null);
}
ctx.EndFinally();
this.local = null;
this.ctx = null;
label = default(CodeLabel);
}
示例11: UsingBlock
/// <summary>
/// Creates a new "using" block (equivalent) around a variable;
/// the variable must exist, and note that (unlike in C#) it is
/// the variables *final* value that gets disposed. If you need
/// *original* disposal, copy your variable first.
///
/// It is the callers responsibility to ensure that the variable's
/// scope fully-encapsulates the "using"; if not, the variable
/// may be re-used (and thus re-assigned) unexpectedly.
/// </summary>
public UsingBlock(CompilerContext ctx, Local local)
{
if (ctx == null) throw new ArgumentNullException("ctx");
if (local == null) throw new ArgumentNullException("local");
Type type = local.Type;
// check if **never** disposable
if ((type.IsValueType || type.IsSealed) &&
!ctx.MapType(typeof(IDisposable)).IsAssignableFrom(type))
{
return; // nothing to do! easiest "using" block ever
// (note that C# wouldn't allow this as a "using" block,
// but we'll be generous and simply not do anything)
}
this.local = local;
this.ctx = ctx;
label = ctx.BeginTry();
}