本文整理汇总了C#中Microsoft.CodeAnalysis.CodeGen.ILBuilder类的典型用法代码示例。如果您正苦于以下问题:C# ILBuilder类的具体用法?C# ILBuilder怎么用?C# ILBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ILBuilder类属于Microsoft.CodeAnalysis.CodeGen命名空间,在下文中一共展示了ILBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Create
/// <summary>
/// Create a SequencePointList with the raw sequence points from an ArrayBuilder.
/// A linked list of instances for each syntax tree is created (almost always of length one).
/// </summary>
public static SequencePointList Create(ArrayBuilder<RawSequencePoint> seqPointBuilder, ILBuilder builder)
{
if (seqPointBuilder.Count == 0)
{
return SequencePointList.s_empty;
}
SequencePointList first = null, current = null;
int totalPoints = seqPointBuilder.Count;
int last = 0;
for (int i = 1; i <= totalPoints; ++i)
{
if (i == totalPoints || seqPointBuilder[i].SyntaxTree != seqPointBuilder[i - 1].SyntaxTree)
{
// Create a new list
SequencePointList next = new SequencePointList(seqPointBuilder[i - 1].SyntaxTree, GetSubArray(seqPointBuilder, last, i - last, builder));
last = i;
// Link together with any additional.
if (current == null)
{
first = current = next;
}
else
{
current._next = next;
current = next;
}
}
}
return first;
}
示例2: CodeGenerator
public CodeGenerator(
MethodSymbol method,
BoundStatement boundBody,
ILBuilder builder,
PEModuleBuilder moduleBuilder,
DiagnosticBag diagnostics,
OptimizationLevel optimizations,
bool emittingPdb)
{
Debug.Assert((object)method != null);
Debug.Assert(boundBody != null);
Debug.Assert(builder != null);
Debug.Assert(moduleBuilder != null);
Debug.Assert(diagnostics != null);
_method = method;
_boundBody = boundBody;
_builder = builder;
_module = moduleBuilder;
_diagnostics = diagnostics;
if (!method.GenerateDebugInfo)
{
// Always optimize synthesized methods that don't contain user code.
//
// Specifically, always optimize synthesized explicit interface implementation methods
// (aka bridge methods) with by-ref returns because peverify produces errors if we
// return a ref local (which the return local will be in such cases).
_ilEmitStyle = ILEmitStyle.Release;
}
else
{
if (optimizations == OptimizationLevel.Debug)
{
_ilEmitStyle = ILEmitStyle.Debug;
}
else
{
_ilEmitStyle = IsDebugPlus() ?
ILEmitStyle.DebugFriendlyRelease :
ILEmitStyle.Release;
}
}
// Emit sequence points unless
// - the PDBs are not being generated
// - debug information for the method is not generated since the method does not contain
// user code that can be stepped through, or changed during EnC.
//
// This setting only affects generating PDB sequence points, it shall not affect generated IL in any way.
_emitPdbSequencePoints = emittingPdb && method.GenerateDebugInfo;
_boundBody = Optimizer.Optimize(
boundBody,
debugFriendly: _ilEmitStyle != ILEmitStyle.Release,
stackLocals: out _stackLocals);
_methodBodySyntaxOpt = (method as SourceMethodSymbol)?.BodySyntax;
}
示例3: BindPlace
internal override IBoundReference BindPlace(ILBuilder il, BoundAccess access, TypeRefMask thint)
{
if (_place == null)
{
// unoptimized locals
return new BoundIndirectVariablePlace(new BoundLiteral(this.Name), access);
}
else
{
return new BoundLocalPlace(_place, access, thint);
}
}
示例4: CodeGenerator
private CodeGenerator(MethodSymbol method,
BoundStatement block,
ILBuilder builder,
PEModuleBuilder module,
DiagnosticBag diagnostics,
bool optimize,
bool emitSequencePoints)
{
this.method = method;
this.block = block;
this.builder = builder;
this.module = module;
this.diagnostics = diagnostics;
this.noOptimizations = !optimize;
this.debugInformationKind = module.Compilation.Options.DebugInformationKind;
if (!this.debugInformationKind.IsValid())
{
this.debugInformationKind = DebugInformationKind.None;
}
// Special case: always optimize synthesized explicit interface implementation methods
// (aka bridge methods) with by-ref returns because peverify produces errors if we
// return a ref local (which the return local will be in such cases).
if (this.noOptimizations && method.ReturnType is ByRefReturnErrorTypeSymbol)
{
Debug.Assert(method is SynthesizedExplicitImplementationMethod);
this.noOptimizations = false;
}
this.emitSequencePoints = emitSequencePoints;
if (!this.noOptimizations)
{
this.block = Optimizer.Optimize(block, out stackLocals);
}
Debug.Assert((object)method != null);
Debug.Assert(block != null);
Debug.Assert(builder != null);
Debug.Assert(module != null);
var asSourceMethod = method as SourceMethodSymbol;
if ((object)asSourceMethod != null)
{
methodBlockSyntax = asSourceMethod.BlockSyntax;
}
}
示例5: CodeGenerator
private CodeGenerator(
MethodSymbol method,
BoundStatement block,
ILBuilder builder,
PEModuleBuilder moduleBuilder,
DiagnosticBag diagnostics,
OptimizationLevel optimizations,
bool emittingPdbs)
{
this.method = method;
this.block = block;
this.builder = builder;
this.module = moduleBuilder;
this.diagnostics = diagnostics;
// Always optimize synthesized methods that don't contain user code.
//
// Specifically, always optimize synthesized explicit interface implementation methods
// (aka bridge methods) with by-ref returns because peverify produces errors if we
// return a ref local (which the return local will be in such cases).
this.optimizations = method.GenerateDebugInfo ? optimizations : OptimizationLevel.Release;
// Emit sequence points unless
// - the PDBs are not being generated
// - debug information for the method is not generated since the method does not contain
// user code that can be stepped thru, or changed during EnC.
//
// This setting only affects generating PDB sequence points, it shall not affect generated IL in any way.
this.emitPdbSequencePoints = emittingPdbs && method.GenerateDebugInfo;
if (this.optimizations == OptimizationLevel.Release)
{
this.block = Optimizer.Optimize(block, out stackLocals);
}
Debug.Assert((object)method != null);
Debug.Assert(block != null);
Debug.Assert(builder != null);
Debug.Assert(moduleBuilder != null);
var asSourceMethod = method as SourceMethodSymbol;
if ((object)asSourceMethod != null)
{
methodBlockSyntax = asSourceMethod.BlockSyntax;
}
}
示例6: SwitchIntegralJumpTableEmitter
internal SwitchIntegralJumpTableEmitter(
ILBuilder builder,
KeyValuePair<ConstantValue, object>[] caseLabels,
object fallThroughLabel,
Cci.PrimitiveTypeCode keyTypeCode,
LocalOrParameter key)
{
this.builder = builder;
this.key = key;
this.keyTypeCode = keyTypeCode;
this.fallThroughLabel = fallThroughLabel;
// Sort the switch case labels, see comments below for more details.
Debug.Assert(caseLabels.Length > 0);
Array.Sort(caseLabels, CompareIntegralSwitchLabels);
sortedCaseLabels = ImmutableArray.Create(caseLabels);
}
示例7: SwitchStringJumpTableEmitter
internal SwitchStringJumpTableEmitter(
ILBuilder builder,
LocalOrParameter key,
KeyValuePair<ConstantValue, object>[] caseLabels,
object fallThroughLabel,
LocalDefinition keyHash,
EmitStringCompareAndBranch emitStringCondBranchDelegate,
GetStringHashCode computeStringHashcodeDelegate)
{
Debug.Assert(caseLabels.Length > 0);
Debug.Assert(emitStringCondBranchDelegate != null);
_builder = builder;
_key = key;
_caseLabels = caseLabels;
_fallThroughLabel = fallThroughLabel;
_keyHash = keyHash;
_emitStringCondBranchDelegate = emitStringCondBranchDelegate;
_computeStringHashcodeDelegate = computeStringHashcodeDelegate;
}
示例8: BasicBlockWithHandlerScope
public BasicBlockWithHandlerScope(ILBuilder builder, ExceptionHandlerScope enclosingHandler)
: base(builder)
{
this.enclosingHandler = enclosingHandler;
}
示例9: Initialize
internal void Initialize(ILBuilder builder)
{
this.builder = builder;
this.FirstILMarker = -1;
this.LastILMarker = -1;
}
示例10: BasicBlock
internal BasicBlock(ILBuilder builder)
{
Debug.Assert(BitConverter.IsLittleEndian);
Initialize(builder);
}
示例11: GenerateMethodBody
internal static MethodBody GenerateMethodBody(TypeCompilationState compilationState, MethodSymbol method, BoundStatement block, DiagnosticBag diagnostics,
bool optimize, DebugDocumentProvider debugDocumentProvider, ImmutableArray<NamespaceScope> namespaceScopes)
{
// Note: don't call diagnostics.HasAnyErrors() in release; could be expensive if compilation has many warnings.
Debug.Assert(!diagnostics.HasAnyErrors(), "Running code generator when errors exist might be dangerous; code generator not well hardened");
bool emitSequencePoints = !namespaceScopes.IsDefault && !method.IsAsync;
var module = compilationState.ModuleBuilder;
var compilation = module.Compilation;
var localSlotManager = module.CreateLocalSlotManager(method);
ILBuilder builder = new ILBuilder(module, localSlotManager, optimize);
DiagnosticBag diagnosticsForThisMethod = DiagnosticBag.GetInstance();
try
{
AsyncMethodBodyDebugInfo asyncDebugInfo = null;
if ((object)method.AsyncKickoffMethod == null) // is this the MoveNext of an async method?
{
CodeGen.CodeGenerator.Run(
method, block, builder, module, diagnosticsForThisMethod, optimize, emitSequencePoints);
}
else
{
int asyncCatchHandlerOffset;
ImmutableArray<int> asyncYieldPoints;
ImmutableArray<int> asyncResumePoints;
CodeGen.CodeGenerator.Run(
method, block, builder, module, diagnosticsForThisMethod, optimize, emitSequencePoints,
out asyncCatchHandlerOffset, out asyncYieldPoints, out asyncResumePoints);
asyncDebugInfo = new AsyncMethodBodyDebugInfo(method.AsyncKickoffMethod, asyncCatchHandlerOffset, asyncYieldPoints, asyncResumePoints);
}
var localVariables = builder.LocalSlotManager.LocalsInOrder();
if (localVariables.Length > 0xFFFE)
{
diagnosticsForThisMethod.Add(ErrorCode.ERR_TooManyLocals, method.Locations.First());
}
if (diagnosticsForThisMethod.HasAnyErrors())
{
// we are done here. Since there were errors we should not emit anything.
return null;
}
// We will only save the IL builders when running tests.
if (module.SaveTestData)
{
module.SetMethodTestData(method, builder.GetSnapshot());
}
// Only compiler-generated MoveNext methods have iterator scopes. See if this is one.
bool hasIteratorScopes =
method.Locations.IsEmpty && method.Name == "MoveNext" &&
(method.ExplicitInterfaceImplementations.Contains(compilation.GetSpecialTypeMember(SpecialMember.System_Collections_IEnumerator__MoveNext) as MethodSymbol) ||
method.ExplicitInterfaceImplementations.Contains(compilation.GetWellKnownTypeMember(WellKnownMember.System_Runtime_CompilerServices_IAsyncStateMachine_MoveNext) as MethodSymbol));
var iteratorScopes = hasIteratorScopes ? builder.GetIteratorScopes() : ImmutableArray<LocalScope>.Empty;
var iteratorOrAsyncImplementation = compilationState.GetIteratorOrAsyncImplementationClass(method);
return new MethodBody(
builder.RealizedIL,
builder.MaxStack,
method,
localVariables,
builder.RealizedSequencePoints,
debugDocumentProvider,
builder.RealizedExceptionHandlers,
builder.GetAllScopes(),
Microsoft.Cci.CustomDebugInfoKind.CSharpStyle,
builder.HasDynamicLocal,
namespaceScopes,
(object)iteratorOrAsyncImplementation == null ? null : iteratorOrAsyncImplementation.MetadataName,
iteratorScopes,
asyncMethodDebugInfo: asyncDebugInfo
);
}
finally
{
// Basic blocks contain poolable builders for IL and sequence points. Free those back
// to their pools.
builder.FreeBasicBlocks();
// Remember diagnostics.
diagnostics.AddRange(diagnosticsForThisMethod);
diagnosticsForThisMethod.Free();
}
}
示例12: DumpSwitchBlockIL
private static void DumpSwitchBlockIL(ILBuilder.SwitchBlock block, StringBuilder sb)
{
byte[] il = block.RegularInstructions.Buffer;
new ILBuilderVisualizer(block.builder.module).DumpILBlock(il, (int)block.RegularInstructionsLength, sb, SpecializedCollections.EmptyArray<ILVisualizer.HandlerSpan>(), block.Start);
// switch (N, t1, t2... tN)
// IL ==> ILOpCode.Switch < unsigned int32 > < int32 >... < int32 >
sb.Append(string.Format(" IL_{0:x4}:", block.RegularInstructionsLength + block.Start));
sb.Append(string.Format(" {0,-10}", GetInstructionName(block.BranchCode)));
sb.Append(string.Format(" IL_{0:x4}:", block.BranchesCount));
var blockBuilder = ArrayBuilder<ILBuilder.BasicBlock>.GetInstance();
block.GetBranchBlocks(blockBuilder);
foreach (var branchBlock in blockBuilder)
{
if (branchBlock == null)
{
// this happens if label is not yet marked.
sb.Append(" <unmarked label>");
}
else
{
sb.Append(string.Format(" IL_{0:x4}", branchBlock.Start));
}
}
blockBuilder.Free();
sb.AppendLine();
}
示例13: DumpBasicBlockIL
private static void DumpBasicBlockIL(ILBuilder.BasicBlock block, StringBuilder sb)
{
var instrCnt = (int)block.RegularInstructionsLength;
if (instrCnt != 0)
{
byte[] il = block.RegularInstructions.Buffer;
new ILBuilderVisualizer(block.builder.module).DumpILBlock(il, instrCnt, sb, SpecializedCollections.EmptyArray<ILVisualizer.HandlerSpan>(), block.Start);
}
if (block.BranchCode != ILOpCode.Nop)
{
sb.Append(string.Format(" IL_{0:x4}:", block.RegularInstructionsLength + block.Start));
sb.Append(string.Format(" {0,-10}", GetInstructionName(block.BranchCode)));
if (block.BranchCode.IsBranchToLabel())
{
var branchBlock = block.BranchBlock;
if (branchBlock == null)
{
// this happens if label is not yet marked.
sb.Append(" <unmarked label>");
}
else
{
sb.Append(string.Format(" IL_{0:x4}", branchBlock.Start));
}
}
sb.AppendLine();
}
}
示例14: EmitStorePrepare
public void EmitStorePrepare(ILBuilder il) { }
示例15: EmitStore
public void EmitStore(ILBuilder il) => il.EmitLocalStore(_def);