本文整理汇总了C#中ILGen类的典型用法代码示例。如果您正苦于以下问题:C# ILGen类的具体用法?C# ILGen怎么用?C# ILGen使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ILGen类属于命名空间,在下文中一共展示了ILGen类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compile
public override Type Compile(Executive engine, ILGen il, LocalAccess locals, Type[] parameterTypes)
{
for (int k = 0; k < parameterTypes.Length; k++)
il.Emit(OpCodes.Pop);
il.EmitFieldGet(typeof(DBNull), "Value");
return typeof(DBNull);
}
示例2: Create
internal static Type Create(GenContext context, Type baseClass)
{
//ModuleBuilder mb = context.ModuleBldr;
string name = baseClass.Name + "_impl";
//TypeBuilder baseTB = context.ModuleBldr.DefineType(name, TypeAttributes.Class | TypeAttributes.Public, baseClass);
TypeBuilder baseTB = context.AssemblyGen.DefinePublicType(name, baseClass, true);
ObjExpr.MarkAsSerializable(baseTB);
baseTB.DefineDefaultConstructor(MethodAttributes.Public);
FieldBuilder metaField = baseTB.DefineField("_meta", typeof(IPersistentMap), FieldAttributes.Public);
MethodBuilder metaMB = baseTB.DefineMethod("meta", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.ReuseSlot, typeof(IPersistentMap), Type.EmptyTypes);
ILGen gen = new ILGen(metaMB.GetILGenerator());
gen.EmitLoadArg(0);
gen.EmitFieldGet(metaField);
gen.Emit(OpCodes.Ret);
MethodBuilder withMB = baseTB.DefineMethod("withMeta", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.ReuseSlot, typeof(IObj), new Type[] { typeof(IPersistentMap)});
gen = new ILGen(withMB.GetILGenerator());
gen.EmitLoadArg(0);
gen.EmitCall(Compiler.Method_Object_MemberwiseClone);
gen.Emit(OpCodes.Castclass, baseTB);
gen.Emit(OpCodes.Dup);
gen.EmitLoadArg(1);
gen.EmitFieldSet(metaField);
gen.Emit(OpCodes.Ret);
for (int i = 0; i < 20; i++ )
DefineDelegateFieldAndOverride(baseTB, i);
return baseTB.CreateType();
}
示例3: Define
// Returns true if the label was successfully defined
// or false if the label is now ambiguous
internal void Define(ILGen il, LabelBlockInfo block) {
// Prevent the label from being shadowed, which enforces cleaner
// trees. Also we depend on this for simplicity (keeping only one
// active IL Label per LabelInfo)
for (LabelBlockInfo j = block; j != null; j = j.Parent) {
if (j.ContainsTarget(Node)) {
throw Error.LabelTargetAlreadyDefined(Node.Name);
}
}
Definitions.Add(block);
block.AddLabelInfo(Node, this);
// Once defined, validate all jumps
if (Definitions.Count == 1) {
foreach (var r in References) {
ValidateJump(r);
}
} else {
// Was just redefined, if we had any across block jumps, they're
// now invalid
if (_acrossBlockJump) {
throw Error.AmbiguousJump(Node.Name);
}
// For local jumps, we need a new IL label
// This is okay because:
// 1. no across block jumps have been made or will be made
// 2. we don't allow the label to be shadowed
Label = il.DefineLabel();
}
}
示例4: GetCCtor
protected ILGen GetCCtor() {
if (_cctor == null) {
ConstructorBuilder cctor = _tb.DefineTypeInitializer();
_cctor = CreateILGen(cctor.GetILGenerator());
}
return _cctor;
}
示例5: FixReturn
internal void FixReturn(ILGen cg)
{
cg.EmitLoadArg(_argIndex);
cg.Emit(OpCodes.Ldloc, _refSlot);
cg.Emit(OpCodes.Ldfld, _refSlot.LocalType.GetDeclaredField("Value"));
cg.EmitStoreValueIndirect(_argType.GetElementType());
}
示例6: LabelInfo
internal LabelInfo(ILGen il, LabelTarget node, bool canReturn) {
_ilg = il;
Node = node;
Label = il.DefineLabel();
_canReturn = canReturn;
if (node != null && node.Type != typeof(void)) {
Value = il.DeclareLocal(node.Type);
}
// Until we have more information, default to a leave instruction, which always works
_opCode = OpCodes.Leave;
}
示例7: GetBranch
private Branch GetBranch(List<Branch> branches, ILGen il, Type type)
{
foreach (Branch b in branches)
if (b.type == type)
return b;
Branch branch = new Branch();
branch.type = type;
branch.localVar = il.DeclareLocal(type);
branch.handler = il.DefineLabel();
branches.Add(branch);
return branch;
}
示例8: EmitArgument
internal static ReturnFixer EmitArgument(ILGen cg, int argIndex, Type argType) {
cg.EmitLoadArg(argIndex);
if (!argType.IsByRef) {
cg.EmitBoxing(argType);
return null;
}
Type elementType = argType.GetElementType();
cg.EmitLoadValueIndirect(elementType);
Type concreteType = typeof(StrongBox<>).MakeGenericType(elementType);
cg.EmitNew(concreteType, new Type[] { elementType });
LocalBuilder refSlot = cg.DeclareLocal(concreteType);
cg.Emit(OpCodes.Dup);
cg.Emit(OpCodes.Stloc, refSlot);
return new ReturnFixer(refSlot, argIndex, argType);
}
示例9: CreateVariables
public override void CreateVariables(Executive engine, object form, ILGen il, LocalAccess locals, object[] args)
{
if (args.Length == 0)
throw new ArgumentException("Unproperly formated expression");
locals.DeclareScope(form);
locals.EnterScope(form, _activeScope);
foreach (object arg in Lisp.getIterator(args[0]))
{
object[] pair = Lisp.ToArray(arg);
if (pair.Length != 2 || !Lisp.IsAtom(pair[0]))
throw new ArgumentException("Unproperly formated expression");
locals.DeclareLocal(pair[0]);
engine.CreateVariables(il, locals, pair[1]);
}
if (!_activeScope)
locals.ActivateScope();
for (int k = 1; k < args.Length; k++)
engine.CreateVariables(il, locals, args[k]);
locals.LeaveScope();
}
示例10: Init
internal void Init(ILGen.BaseILGenerator ilGen)
{
_ilGen = ilGen;
if (_availableLocals == null)
{
//First time initialization
_availableLocals = new Dictionary<Type, AvailableLocals>();
_symbolLocals = new Dictionary<JSSymbol, LocalBuilder>();
_namedLocals = new Dictionary<string, LocalBuilder>();
_unnamedLocals = new Dictionary<IR.WriteTemporaryExpression, LocalBuilder>();
_temporaryStackLocals = new List<LocalBuilder>();
}
else
Debug.Assert(
_availableLocals.Count == 0
&& _symbolLocals.Count == 0
&& _namedLocals.Count == 0
&& _unnamedLocals.Count == 0
&& _temporaryStackLocals.Count == 0
, "LocalVarManager was not cleared properly in the last use!");
}
示例11: OverrideDeserializer
private void OverrideDeserializer(ConstructorInfo/*!*/ baseCtor) {
// ctor(SerializationInfo! info, StreamingContext! context) : base(info, context) {
// RubyOps.DeserializeObject(out this._instanceData, out this._immediateClass, info);
// }
ConstructorBuilder cb = _tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, _deserializerSignature);
ILGen il = new ILGen(cb.GetILGenerator());
il.EmitLoadArg(0);
il.EmitLoadArg(1);
il.EmitLoadArg(2);
il.Emit(OpCodes.Call, baseCtor);
il.EmitLoadArg(0);
il.EmitFieldAddress(InstanceDataField);
il.EmitLoadArg(0);
il.EmitFieldAddress(ImmediateClassField);
il.EmitLoadArg(1);
il.EmitCall(Methods.DeserializeObject);
il.Emit(OpCodes.Ret);
}
示例12: BuildConstructors
private void BuildConstructors(IList<ConstructorBuilderInfo>/*!*/ ctors) {
foreach (var ctor in ctors) {
// ctor(... RubyClass! class ..., <visible params>) : base(<hidden params>, <visible params>) { _class = class; }
// ctor(... RubyClass! class ..., <visible params>) : base(... RubyOps.GetContextFromClass(class) ..., <visible params>) { _class = class; }
// ctor(RubyClass! class) : base(RubyOps.GetDefaultExceptionMessage(class)) { _class = class; }
ConstructorBuilder cb = _tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, ctor.ParameterTypes);
ILGen il = new ILGen(cb.GetILGenerator());
int paramIndex = 0;
int argIndex = 0;
// We need to initialize before calling base ctor since the ctor can call virtual methods.
// _immediateClass = immediateClass:
if (!IsDerivedRubyType) {
il.EmitLoadArg(0);
il.EmitLoadArg(1 + ctor.ClassParamIndex);
il.EmitFieldSet(ImmediateClassField);
}
// base ctor call:
il.EmitLoadArg(0);
ConstructorInfo msgCtor;
if (ctor.ParameterTypes.Length == 1 && ctor.Adjustment == SignatureAdjustment.InsertClass &&
_tb.IsSubclassOf(typeof(Exception)) && IsAvailable(msgCtor = _tb.BaseType.GetConstructor(_exceptionMessageSignature))) {
// a parameterless exception constructor should use Ruby default message:
il.EmitLoadArg(1);
il.EmitCall(Methods.GetDefaultExceptionMessage);
il.Emit(OpCodes.Call, msgCtor);
} else {
if (ctor.Adjustment == SignatureAdjustment.InsertClass) {
paramIndex++;
}
while (paramIndex < ctor.ParameterTypes.Length) {
if (ctor.Adjustment == SignatureAdjustment.ConvertClassToContext && argIndex == ctor.ContextArgIndex) {
il.EmitLoadArg(1 + ctor.ClassParamIndex);
il.EmitCall(Methods.GetContextFromModule);
} else {
ClsTypeEmitter.DefineParameterCopy(cb, paramIndex, ctor.BaseParameters[argIndex]);
il.EmitLoadArg(1 + paramIndex);
}
argIndex++;
paramIndex++;
}
il.Emit(OpCodes.Call, ctor.BaseCtor);
}
il.Emit(OpCodes.Ret);
}
}
示例13: GenerateGetRequiredArityMethod
static MethodBuilder GenerateGetRequiredArityMethod(TypeBuilder tb, int requiredArity)
{
MethodBuilder mb = tb.DefineMethod(
"getRequiredArity",
MethodAttributes.ReuseSlot | MethodAttributes.Public | MethodAttributes.Virtual,
typeof(int),
Type.EmptyTypes);
ILGen gen = new ILGen(mb.GetILGenerator());
gen.EmitInt(requiredArity);
gen.Emit(OpCodes.Ret);
return mb;
}
示例14: EmitConstantGet
private static void EmitConstantGet(ILGen il, int index, Type type) {
il.Emit(OpCodes.Ldarg_0);
il.EmitInt(index);
il.Emit(OpCodes.Ldelem_Ref);
if (type != typeof(object)) {
il.Emit(OpCodes.Castclass, type);
}
}
示例15: EmitClrCallStub
/// <summary>
/// Generates stub to receive the CLR call and then call the dynamic language code.
/// </summary>
private object[] EmitClrCallStub(ILGen cg) {
List<ReturnFixer> fixers = new List<ReturnFixer>(0);
// Create strongly typed return type from the site.
// This will, among other things, generate tighter code.
Type[] siteTypes = MakeSiteSignature();
CallSite callSite = CallSite.Create(DynamicSiteHelpers.MakeCallSiteDelegate(siteTypes), InvokeBinder);
Type siteType = callSite.GetType();
Type convertSiteType = null;
CallSite convertSite = null;
if (_returnType != typeof(void)) {
convertSite = CallSite.Create(DynamicSiteHelpers.MakeCallSiteDelegate(typeof(object), _returnType), ConvertBinder);
convertSiteType = convertSite.GetType();
}
// build up constants array
object[] constants = new object[] { TargetPlaceHolder, CallSitePlaceHolder, ConvertSitePlaceHolder };
const int TargetIndex = 0, CallSiteIndex = 1, ConvertSiteIndex = 2;
LocalBuilder convertSiteLocal = null;
FieldInfo convertTarget = null;
if (_returnType != typeof(void)) {
// load up the conversesion logic on the stack
convertSiteLocal = cg.DeclareLocal(convertSiteType);
EmitConstantGet(cg, ConvertSiteIndex, convertSiteType);
cg.Emit(OpCodes.Dup);
cg.Emit(OpCodes.Stloc, convertSiteLocal);
convertTarget = convertSiteType.GetField("Target");
cg.EmitFieldGet(convertTarget);
cg.Emit(OpCodes.Ldloc, convertSiteLocal);
}
// load up the invoke logic on the stack
LocalBuilder site = cg.DeclareLocal(siteType);
EmitConstantGet(cg, CallSiteIndex, siteType);
cg.Emit(OpCodes.Dup);
cg.Emit(OpCodes.Stloc, site);
FieldInfo target = siteType.GetField("Target");
cg.EmitFieldGet(target);
cg.Emit(OpCodes.Ldloc, site);
EmitConstantGet(cg, TargetIndex, typeof(object));
for (int i = 0; i < _parameters.Length; i++) {
if (_parameters[i].ParameterType.IsByRef) {
ReturnFixer rf = ReturnFixer.EmitArgument(cg, i + 1, _parameters[i].ParameterType);
if (rf != null) fixers.Add(rf);
} else {
cg.EmitLoadArg(i + 1);
}
}
// emit the invoke for the call
cg.EmitCall(target.FieldType, "Invoke");
// emit the invoke for the convert
if (_returnType == typeof(void)) {
cg.Emit(OpCodes.Pop);
} else {
cg.EmitCall(convertTarget.FieldType, "Invoke");
}
// fixup any references
foreach (ReturnFixer rf in fixers) {
rf.FixReturn(cg);
}
cg.Emit(OpCodes.Ret);
return constants;
}