Exemplo n.º 1
0
        public static void Register(ITypeResolver typeResolver)
        {
            var codeBuilder = new IlCodeBuilder();

            codeBuilder.LoadArgument(0);
            codeBuilder.Add(Code.Call, 1);
            codeBuilder.Add(Code.Dup);
            codeBuilder.LoadArgument(0);
            codeBuilder.Add(Code.Stfld, 2);
            codeBuilder.Add(Code.Ret);

            var parameters = new List <IParameter>();

            parameters.Add(typeResolver.System.System_Int32.ToParameter());

            var tokenResolutions = new List <object>();

            tokenResolutions.Add(
                new SynthesizedStaticMethod(
                    string.Empty,
                    typeResolver.System.System_String,
                    typeResolver.System.System_String,
                    parameters,
                    (llvmWriter, opCode) => llvmWriter.WriteNewMethodBody(opCode, typeResolver.System.System_String, enableStringFastAllocation: true)));
            tokenResolutions.Add(typeResolver.System.System_String.GetFieldByName("m_stringLength", typeResolver));

            var locals = new List <IType>();

            MethodBodyBank.Register(Name, codeBuilder.GetCode(), tokenResolutions, locals, parameters);
        }
        /// <summary>
        /// </summary>
        /// <param name="type">
        /// </param>
        /// <param name="typeResolver">
        /// </param>
        public SynthesizedSingleDimArrayIListSetItemMethod(IType arrayType, ITypeResolver typeResolver)
            : base("set_Item", arrayType, typeResolver.System.System_Void)
        {
            var codeList = new IlCodeBuilder();

            codeList.LoadArgument(0);
            codeList.LoadArgument(1);
            codeList.LoadArgument(2);
            codeList.Add(Code.Stelem, 1);
            codeList.Add(Code.Ret);

            var locals = new List <IType>();

            this._methodBody =
                new SynthesizedMethodBodyDecorator(
                    null,
                    locals,
                    codeList.GetCode());

            this._parameters = new List <IParameter>();
            this._parameters.Add(typeResolver.System.System_Int32.ToParameter());
            this._parameters.Add(arrayType.GetElementType().ToParameter());

            this._tokenResolutions = new List <object>();
            this._tokenResolutions.Add(arrayType.GetElementType());
        }
Exemplo n.º 3
0
        public static void GetCtorSBytePtr(ITypeResolver typeResolver, out byte[] code, out IList <object> tokenResolutions, out IList <IType> locals, out IList <IParameter> parameters)
        {
            IType systemString = typeResolver.System.System_String;

            var codeBuilder = new IlCodeBuilder();

            codeBuilder.LoadArgument(0);

            // calculate length
            codeBuilder.LoadArgument(0);
            codeBuilder.Add(Code.Call, 2);

            codeBuilder.Add(Code.Call, 3);
            codeBuilder.Add(Code.Call, 1);
            codeBuilder.Add(Code.Ret);

            code = codeBuilder.GetCode();

            locals = new List <IType>();

            tokenResolutions = new List <object>();
            tokenResolutions.Add(systemString.GetMethodByName("CreateStringFromEncoding", typeResolver));
            tokenResolutions.Add(systemString.GetMethodByName("strlen", typeResolver));
            tokenResolutions.Add(typeResolver.ResolveType("System.Text.Encoding").GetMethodByName("get_ASCII", typeResolver));

            parameters = new List <IParameter>();
            parameters.Add(typeResolver.System.System_SByte.ToPointerType().ToParameter());
        }
Exemplo n.º 4
0
        public static void Register(ITypeResolver typeResolver)
        {
            var codeBuilder = new IlCodeBuilder();
            codeBuilder.LoadArgument(0);
            codeBuilder.Add(Code.Call, 1);
            codeBuilder.Add(Code.Dup);
            codeBuilder.LoadArgument(0);
            codeBuilder.Add(Code.Stfld, 2);
            codeBuilder.Add(Code.Ret);

            var parameters = new List<IParameter>();
            parameters.Add(typeResolver.System.System_Int32.ToParameter());

            var tokenResolutions = new List<object>();
            tokenResolutions.Add(
                new SynthesizedStaticMethod(
                    string.Empty,
                    typeResolver.System.System_String,
                    typeResolver.System.System_String,
                    parameters,
                    (llvmWriter, opCode) => llvmWriter.WriteNewMethodBody(opCode, typeResolver.System.System_String, enableStringFastAllocation: true)));
            tokenResolutions.Add(typeResolver.System.System_String.GetFieldByName("m_stringLength", typeResolver));

            var locals = new List<IType>();

            MethodBodyBank.Register(Name, codeBuilder.GetCode(), tokenResolutions, locals, parameters);
        }
        /// <summary>
        /// </summary>
        /// <param name="type">
        /// </param>
        /// <param name="typeResolver">
        /// </param>
        public SynthesizedSingleDimArrayIListSetItemMethod(IType arrayType, ITypeResolver typeResolver)
            : base("set_Item", arrayType, typeResolver.System.System_Void)
        {
            var codeList = new IlCodeBuilder();
            codeList.LoadArgument(0);
            codeList.LoadArgument(1);
            codeList.LoadArgument(2);
            codeList.Add(Code.Stelem, 1);
            codeList.Add(Code.Ret);

            var locals = new List<IType>();

            this._methodBody =
                new SynthesizedMethodBodyDecorator(
                    null,
                    locals,
                    codeList.GetCode());

            this._parameters = new List<IParameter>();
            this._parameters.Add(typeResolver.System.System_Int32.ToParameter());
            this._parameters.Add(arrayType.GetElementType().ToParameter());

            this._tokenResolutions = new List<object>();
            this._tokenResolutions.Add(arrayType.GetElementType());
        }
Exemplo n.º 6
0
        public static void Register(ITypeResolver typeResolver)
        {
            var codeList = new IlCodeBuilder();

            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 1);
            codeList.Add(Code.Ldfld, 2);

            var label1 = codeList.Branch(Code.Brtrue, Code.Brtrue_S);

            codeList.LoadArgument(1);

            var label2 = codeList.Branch(Code.Brtrue, Code.Brtrue_S);

            codeList.LoadConstant(0);
            codeList.Add(Code.Ret);

            codeList.Add(label1);
            codeList.Add(label2);

            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 3);
            codeList.Add(Code.Ldfld, 4);

            // Rank - index - 1
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 1);
            codeList.Add(Code.Ldfld, 2);
            codeList.LoadArgument(1);
            codeList.Add(Code.Sub);
            codeList.LoadConstant(1);
            codeList.Add(Code.Sub);

            codeList.Add(Code.Ldelem_I4);

            codeList.Add(Code.Ret);

            var arrayType      = typeResolver.System.System_Byte.ToArrayType(1);
            var arrayTypeMulti = typeResolver.System.System_Byte.ToArrayType(2);

            // Registering GetHashCode
            var tokenResolutions = new List <object>();

            tokenResolutions.Add(arrayType);
            tokenResolutions.Add(arrayType.GetFieldByName("rank", typeResolver));
            tokenResolutions.Add(arrayTypeMulti);
            tokenResolutions.Add(arrayTypeMulti.GetFieldByName("lowerBounds", typeResolver));

            var locals = new List <IType>();

            var parameters = new List <IParameter>();

            parameters.Add(typeResolver.System.System_Int32.ToParameter());

            MethodBodyBank.Register(Name, codeList.GetCode(), tokenResolutions, locals, parameters);
        }
Exemplo n.º 7
0
        public static void Register(ITypeResolver typeResolver)
        {
            var codeList = new IlCodeBuilder();

            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 1);
            codeList.Add(Code.Ldfld, 2);

            var label1 = codeList.Branch(Code.Brtrue, Code.Brtrue_S);

            codeList.LoadArgument(1);

            var label2 = codeList.Branch(Code.Brtrue, Code.Brtrue_S);

            codeList.LoadConstant(0);
            codeList.Add(Code.Ret);

            codeList.Add(label1);
            codeList.Add(label2);

            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 3);
            codeList.Add(Code.Ldfld, 4);

            // Rank - index - 1
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 1);
            codeList.Add(Code.Ldfld, 2);
            codeList.LoadArgument(1);
            codeList.Add(Code.Sub);
            codeList.LoadConstant(1);
            codeList.Add(Code.Sub);
                
            codeList.Add(Code.Ldelem_I4);

            codeList.Add(Code.Ret);

            var arrayType = typeResolver.System.System_Byte.ToArrayType(1);
            var arrayTypeMulti = typeResolver.System.System_Byte.ToArrayType(2);

            // Registering GetHashCode
            var tokenResolutions = new List<object>();
            tokenResolutions.Add(arrayType);
            tokenResolutions.Add(arrayType.GetFieldByName("rank", typeResolver));
            tokenResolutions.Add(arrayTypeMulti);
            tokenResolutions.Add(arrayTypeMulti.GetFieldByName("lowerBounds", typeResolver));

            var locals = new List<IType>();

            var parameters = new List<IParameter>();
            parameters.Add(typeResolver.System.System_Int32.ToParameter());

            MethodBodyBank.Register(Name, codeList.GetCode(), tokenResolutions, locals, parameters);
        }
Exemplo n.º 8
0
        /// <summary>
        /// </summary>
        /// <param name="type">
        /// </param>
        /// <param name="typeResolver">
        /// </param>
        public SynthesizedSingleDimArrayIListGetEnumeratorMethod(IType arrayType, ITypeResolver typeResolver)
            : base("GetEnumerator", arrayType, typeResolver.System.System_Collections_Generic_IEnumerator_T.Construct(arrayType.GetElementType()))
        {
            var codeList = new IlCodeBuilder();

            codeList.LoadArgument(0);
            codeList.Add(Code.Newobj, 1);
            codeList.Add(Code.Newobj, 2);
            codeList.Add(Code.Ret);

            var locals = new List <IType>();

            this._methodBody =
                new SynthesizedMethodBodyDecorator(
                    null,
                    locals,
                    codeList.GetCode());

            this._parameters = new List <IParameter>();

            this._tokenResolutions = new List <object>();

            var arraySegmentType = typeResolver.System.System_ArraySegment_T1.Construct(arrayType.GetElementType());

            this._tokenResolutions.Add(
                IlReader.Constructors(arraySegmentType, typeResolver).First(c => c.GetParameters().Count() == 1));
            this._tokenResolutions.Add(
                IlReader.Constructors(arraySegmentType.GetNestedTypes().First(), typeResolver).First(c => c.GetParameters().Count() == 1));
        }
        /// <summary>
        /// </summary>
        /// <param name="type">
        /// </param>
        /// <param name="typeResolver">
        /// </param>
        public SynthesizedSingleDimArrayIListGetEnumeratorMethod(IType arrayType, ITypeResolver typeResolver)
            : base("GetEnumerator", arrayType, typeResolver.System.System_Collections_Generic_IEnumerator_T.Construct(arrayType.GetElementType()))
        {
            var codeList = new IlCodeBuilder();
            codeList.LoadArgument(0);
            codeList.Add(Code.Newobj, 1);
            codeList.Add(Code.Newobj, 2);
            codeList.Add(Code.Ret);

            var locals = new List<IType>();

            this._methodBody =
                new SynthesizedMethodBodyDecorator(
                    null,
                    locals,
                    codeList.GetCode());

            this._parameters = new List<IParameter>();

            this._tokenResolutions = new List<object>();

            var arraySegmentType = typeResolver.System.System_ArraySegment_T1.Construct(arrayType.GetElementType());
            this._tokenResolutions.Add(
                IlReader.Constructors(arraySegmentType, typeResolver).First(c => c.GetParameters().Count() == 1));
            this._tokenResolutions.Add(
                IlReader.Constructors(arraySegmentType.GetNestedTypes().First(), typeResolver).First(c => c.GetParameters().Count() == 1));
        }
Exemplo n.º 10
0
        public static void GetStrLen(ITypeResolver typeResolver, out byte[] code, out IList <object> tokenResolutions, out IList <IType> locals, out IList <IParameter> parameters)
        {
            var codeBuilder = new IlCodeBuilder();

            codeBuilder.LoadArgument(0);
            codeBuilder.SaveLocal(0);
            var initialJumpLabel = codeBuilder.Branch(Code.Br, Code.Br_S);

            var jumpBack = codeBuilder.CreateLabel();

            codeBuilder.LoadLocal(0);
            codeBuilder.LoadConstant(1);
            codeBuilder.Add(Code.Conv_I);
            codeBuilder.Add(Code.Add);
            codeBuilder.SaveLocal(0);

            codeBuilder.Add(initialJumpLabel);

            codeBuilder.LoadLocal(0);
            codeBuilder.Add(Code.Ldind_I1);
            codeBuilder.LoadConstant(0);

            codeBuilder.Branch(Code.Bgt, Code.Bgt_S, jumpBack);

            codeBuilder.LoadLocal(0);
            codeBuilder.LoadArgument(0);
            codeBuilder.Add(Code.Sub);

            // and if element size is bugger 1, you need to devide it by element size
            // codeBuilder.LoadConstant(<size>);
            // codeBuilder.Add(Code.Div);

            codeBuilder.Add(Code.Ret);

            code = codeBuilder.GetCode();

            locals = new List <IType>();
            locals.Add(typeResolver.System.System_SByte.ToPointerType());

            tokenResolutions = new List <object>();

            parameters = new List <IParameter>();
            parameters.Add(typeResolver.System.System_SByte.ToPointerType().ToParameter());
        }
        /// <summary>
        /// </summary>
        /// <param name="type">
        /// </param>
        /// <param name="typeResolver">
        /// </param>
        public SynthesizedSingleDimArrayIListGetCountMethod(IType arrayType, ITypeResolver typeResolver)
            : base("get_Count", arrayType, typeResolver.System.System_Int32)
        {
            var codeList = new IlCodeBuilder();
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldlen);
            codeList.Add(Code.Ret);

            var locals = new List<IType>();

            this._methodBody =
                new SynthesizedMethodBodyDecorator(
                    null,
                    locals,
                    codeList.GetCode());

            this._parameters = new List<IParameter>();

            this._tokenResolutions = new List<object>();
        }
Exemplo n.º 12
0
        /// <summary>
        /// </summary>
        /// <param name="type">
        /// </param>
        /// <param name="typeResolver">
        /// </param>
        public SynthesizedSingleDimArrayIListGetCountMethod(IType arrayType, ITypeResolver typeResolver)
            : base("get_Count", arrayType, typeResolver.System.System_Int32)
        {
            var codeList = new IlCodeBuilder();

            codeList.LoadArgument(0);
            codeList.Add(Code.Ldlen);
            codeList.Add(Code.Ret);

            var locals = new List <IType>();

            this._methodBody =
                new SynthesizedMethodBodyDecorator(
                    null,
                    locals,
                    codeList.GetCode());

            this._parameters = new List <IParameter>();

            this._tokenResolutions = new List <object>();
        }
Exemplo n.º 13
0
        public static void Register(ITypeResolver typeResolver)
        {
            var codeList = new IlCodeBuilder();

            // set Type of TypedReference
            codeList.LoadArgument(1);
            codeList.Add(Code.Ldflda, 2);

            // Load element typeCode
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 4);
            codeList.Add(Code.Ldfld, 5);

            // Save typeCode into TypedReference TypeCode
            codeList.Add(Code.Conv_I);
            codeList.Add(Code.Stfld, 3);

            // Calculate data index
            // check if it 1-dim array
            codeList.LoadArgument(2);
            codeList.LoadConstant(1);
            codeList.Add(Code.Sub);
            var labelGotoMultiDimArray = codeList.Branch(Code.Brtrue, Code.Brtrue_S);

            // set Value of TypedReference
            codeList.LoadArgument(1);
            codeList.Add(Code.Ldflda, 1);

            // Load reference to an array (do not load reference to field data)
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 4);

            // Load elementSize
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 4);
            codeList.Add(Code.Ldfld, 6);
            codeList.Add(Code.Dup);
            codeList.SaveLocal(0);

            // Load index
            codeList.LoadArgument(3);
            codeList.Add(Code.Ldind_I4);

            // multiply it
            codeList.Add(Code.Mul);

            // load address of an element
            codeList.Add(Code.Ldelema, 7);

            // align index
            codeList.LoadLocal(0);
            // elementSize - 1
            codeList.LoadConstant(1);
            codeList.Add(Code.Sub);
            codeList.Add(Code.Dup);
            codeList.SaveLocal(0);

            // size + align - 1
            codeList.Add(Code.Add);

            // size &= ~(align - 1)
            codeList.LoadLocal(0);
            codeList.LoadConstant(-1);
            codeList.Add(Code.Xor);
            codeList.Add(Code.And);

            // Save address into TypedReference Value
            codeList.Add(Code.Conv_I);
            codeList.Add(Code.Stfld, 3);
            codeList.Add(Code.Ret);

            // for multiarray
            codeList.Add(labelGotoMultiDimArray);

            // *indeces += count;
            codeList.LoadArgument(3);
            codeList.LoadArgument(2);
            codeList.Add(Code.Conv_I);
            codeList.LoadConstant(4);
            codeList.Add(Code.Mul);
            codeList.Add(Code.Add);
            codeList.SaveArgument(3);

            // init multiplier
            // Load elementSize
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 4);
            codeList.Add(Code.Ldfld, 6);
            codeList.SaveLocal(0);

            // calculate first index
            codeList.LoadArgument(3);
            codeList.LoadConstant(4);
            codeList.Add(Code.Sub);
            codeList.Add(Code.Dup);
            codeList.SaveArgument(3);
            codeList.Add(Code.Ldind_I4);
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 9);
            codeList.Add(Code.Ldfld, 10);
            codeList.LoadConstant(0);
            codeList.Add(Code.Ldelem_I4);
            codeList.Add(Code.Sub);
            codeList.LoadLocal(0);
            codeList.Add(Code.Mul);
            codeList.SaveLocal(1);

            // init 'i' (index)
            codeList.LoadConstant(1);
            codeList.SaveLocal(2);

            var labelLoopStart = codeList.Branch(Code.Br, Code.Br_S);
            // loop start

            var labelLoopBack = codeList.CreateLabel();

            codeList.LoadLocal(0);
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 9);
            codeList.Add(Code.Ldfld, 11);
            codeList.LoadLocal(2);
            codeList.LoadConstant(1);
            codeList.Add(Code.Sub);
            codeList.Add(Code.Ldelem_I4);
            codeList.Add(Code.Mul);
            codeList.SaveLocal(0);
            codeList.LoadLocal(1);
            codeList.LoadArgument(3);
            codeList.LoadConstant(4);
            codeList.Add(Code.Sub);
            codeList.Add(Code.Dup);
            codeList.SaveArgument(3);
            codeList.Add(Code.Ldind_I4);
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 9);
            codeList.Add(Code.Ldfld, 10);
            codeList.LoadLocal(2);
            codeList.Add(Code.Ldelem_I4);
            codeList.Add(Code.Sub);
            codeList.LoadLocal(0);
            codeList.Add(Code.Mul);
            codeList.Add(Code.Add);
            codeList.SaveLocal(1);
            codeList.LoadLocal(2);
            codeList.LoadConstant(1);
            codeList.Add(Code.Add);
            codeList.SaveLocal(2);

            codeList.Add(labelLoopStart);

            codeList.LoadLocal(2);
            codeList.LoadArgument(2);

            codeList.Branch(Code.Blt, Code.Blt_S, labelLoopBack);

            // set Value of TypedReference
            codeList.LoadArgument(1);
            codeList.Add(Code.Ldflda, 1);

            // align index (array offset) (Local.0) and save it to TypedReferenece
            // Load reference to an array (do not load reference to field data)
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 9);
            codeList.LoadLocal(1);
            // load address of an element
            codeList.Add(Code.Ldelema, 7);

            // elementSize
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 4);
            codeList.Add(Code.Ldfld, 6);

            // elementSize - 1
            codeList.LoadConstant(1);
            codeList.Add(Code.Sub);
            codeList.Add(Code.Dup);
            codeList.SaveLocal(2);

            // size + align - 1
            codeList.Add(Code.Add);

            // size &= ~(align - 1)
            codeList.LoadLocal(2);
            codeList.LoadConstant(-1);
            codeList.Add(Code.Xor);
            codeList.Add(Code.And);

            // Save address into TypedReference Value
            codeList.Add(Code.Conv_I);
            codeList.Add(Code.Stfld, 3);

            codeList.Add(Code.Ret);

            var typedReferenceType = typeResolver.System.System_TypedReference;
            var intPtrType         = typeResolver.System.System_IntPtr;
            var byteType           = typeResolver.System.System_Byte;
            var arrayType          = byteType.ToArrayType(1);
            var multiArrayType     = byteType.ToArrayType(2);

            var tokenResolutions = new List <object>();

            tokenResolutions.Add(typedReferenceType.GetFieldByName("Value", typeResolver));
            tokenResolutions.Add(typedReferenceType.GetFieldByName("Type", typeResolver));
            tokenResolutions.Add(intPtrType.GetFieldByName("m_value", typeResolver));
            tokenResolutions.Add(arrayType);
            tokenResolutions.Add(arrayType.GetFieldByName("typeCode", typeResolver));
            tokenResolutions.Add(arrayType.GetFieldByName("elementSize", typeResolver));
            tokenResolutions.Add(byteType);
            tokenResolutions.Add(arrayType.GetFieldByName("rank", typeResolver));
            tokenResolutions.Add(multiArrayType);
            tokenResolutions.Add(multiArrayType.GetFieldByName("lowerBounds", typeResolver));
            tokenResolutions.Add(multiArrayType.GetFieldByName("lengths", typeResolver));

            var locals = new List <IType>();

            locals.Add(typeResolver.System.System_Int32);
            locals.Add(typeResolver.System.System_Int32);
            locals.Add(typeResolver.System.System_Int32);

            var parameters = new List <IParameter>();

            parameters.Add(typeResolver.System.System_Void.ToPointerType().ToParameter());
            parameters.Add(typeResolver.System.System_Int32.ToParameter());
            parameters.Add(typeResolver.System.System_Int32.ToPointerType().ToParameter());

            MethodBodyBank.Register(Name, codeList.GetCode(), tokenResolutions, locals, parameters);
        }
Exemplo n.º 14
0
        public static void GetMulticastDelegateInvoke(
            IMethod method,
            ITypeResolver typeResolver,
            out byte[] code,
            out IList<object> tokenResolutions,
            out IList<IType> locals,
            out IList<IParameter> parameters)
        {
            parameters = method.GetParameters().ToList();

            var codeList = new IlCodeBuilder();

            codeList.Add(Code.Ldarg_0);
            codeList.Add(Code.Ldfld, 1);

#if MSCORLIB
            // to load value from IntPtr
            codeList.Add(Code.Ldind_I);
#endif

            var jumpForBrtrue_S = codeList.Branch(Code.Brtrue, Code.Brtrue_S);
            codeList.Add(Code.Call, 2);
            codeList.Add(jumpForBrtrue_S);

            codeList.Add(Code.Ldc_I4_0);
            codeList.Add(Code.Stloc_0);

            var labelForFirstJump = codeList.Branch(Code.Br, Code.Br_S);

            // label
            var labelForConditionLoop = codeList.CreateLabel();

            codeList.Add(Code.Ldarg_0);
            codeList.Add(Code.Ldfld, 3);

#if MSCORLIB
            codeList.Add(Code.Castclass, 5);
#endif

            codeList.Add(Code.Ldloc_0);
            codeList.Add(Code.Ldelem_Ref);

            var index = 1;
            foreach (var parameter in parameters)
            {   
                codeList.LoadArgument(index);
                index++;
            }

            codeList.Add(Code.Callvirt, 4);

            if (!method.ReturnType.IsVoid())
            {
                codeList.Add(Code.Stloc_1);
            }

            codeList.LoadLocal(0);
            codeList.LoadConstant(1);
            codeList.Add(Code.Add);
            codeList.SaveLocal(0);

            // label
            codeList.Add(labelForFirstJump);

            // for test
            codeList.LoadLocal(0);
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldfld, 1);

#if MSCORLIB
            // to load value from IntPtr
            codeList.Add(Code.Ldind_I);
#endif

            codeList.Branch(Code.Blt, Code.Blt_S, labelForConditionLoop);

            if (!method.ReturnType.IsVoid())
            {
                codeList.Add(Code.Ldloc_1);
            }

            codeList.Add(Code.Ret);

            code = codeList.GetCode();

            locals = new List<IType>();
            locals.Add(typeResolver.System.System_Int32);
            if (!method.ReturnType.IsVoid())
            {
                locals.Add(method.ReturnType);
            }

            tokenResolutions = new List<object>();

            // 1
            tokenResolutions.Add(method.DeclaringType.BaseType.GetFieldByName("_invocationCount", typeResolver));

            // call Delegate.Invoke
            // 2
            tokenResolutions.Add(
                new SynthesizedStaticMethod(
                    string.Empty,
                    method.DeclaringType,
                    typeResolver.System.System_Void,
                    new List<IParameter>(),
                    (llvmWriter, opCode) =>
                    {
                        // get element size
                        llvmWriter.WriteDelegateInvoke(method, true, true);
                    }));

            // 3
            tokenResolutions.Add(method.DeclaringType.BaseType.GetFieldByName("_invocationList", typeResolver));

            // call Default stub for now - "ret undef";
            // 4
            tokenResolutions.Add(IlReader.Methods(method.DeclaringType, typeResolver).First(m => m.Name == "Invoke"));

#if MSCORLIB
            // 5, to case Object to Object[]
            tokenResolutions.Add(typeResolver.System.System_Object.ToArrayType(1));
#endif
        }
        public static void Register(ITypeResolver typeResolver)
        {
            var codeList = new IlCodeBuilder();

            // set Type of TypedReference
            codeList.LoadArgument(1);
            codeList.Add(Code.Ldflda, 2);

            // Load element typeCode
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 4);
            codeList.Add(Code.Ldfld, 5);

            // Save typeCode into TypedReference TypeCode
            codeList.Add(Code.Conv_I);
            codeList.Add(Code.Stfld, 3);

            // Calculate data index
            // check if it 1-dim array
            codeList.LoadArgument(2);
            codeList.LoadConstant(1);
            codeList.Add(Code.Sub);
            var labelGotoMultiDimArray = codeList.Branch(Code.Brtrue, Code.Brtrue_S);

            // set Value of TypedReference
            codeList.LoadArgument(1);
            codeList.Add(Code.Ldflda, 1);

            // Load reference to an array (do not load reference to field data)
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 4);

            // Load elementSize
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 4);
            codeList.Add(Code.Ldfld, 6);
            codeList.Add(Code.Dup);
            codeList.SaveLocal(0);

            // Load index
            codeList.LoadArgument(3);
            codeList.Add(Code.Ldind_I4);

            // multiply it
            codeList.Add(Code.Mul);

            // load address of an element
            codeList.Add(Code.Ldelema, 7);

            // align index
            codeList.LoadLocal(0);
            // elementSize - 1
            codeList.LoadConstant(1);
            codeList.Add(Code.Sub);
            codeList.Add(Code.Dup);
            codeList.SaveLocal(0);

            // size + align - 1
            codeList.Add(Code.Add);

            // size &= ~(align - 1)
            codeList.LoadLocal(0);
            codeList.LoadConstant(-1);
            codeList.Add(Code.Xor);
            codeList.Add(Code.And);

            // Save address into TypedReference Value
            codeList.Add(Code.Conv_I);
            codeList.Add(Code.Stfld, 3);
            codeList.Add(Code.Ret);

            // for multiarray
            codeList.Add(labelGotoMultiDimArray);

            // *indeces += count;
            codeList.LoadArgument(3);
            codeList.LoadArgument(2);
            codeList.Add(Code.Conv_I);
            codeList.LoadConstant(4);
            codeList.Add(Code.Mul);
            codeList.Add(Code.Add);
            codeList.SaveArgument(3);

            // init multiplier
            // Load elementSize
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 4);
            codeList.Add(Code.Ldfld, 6);
            codeList.SaveLocal(0);

            // calculate first index
            codeList.LoadArgument(3);
            codeList.LoadConstant(4);
            codeList.Add(Code.Sub);
            codeList.Add(Code.Dup);
            codeList.SaveArgument(3);
            codeList.Add(Code.Ldind_I4);
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 9);
            codeList.Add(Code.Ldfld, 10);
            codeList.LoadConstant(0);
            codeList.Add(Code.Ldelem_I4);
            codeList.Add(Code.Sub);
            codeList.LoadLocal(0);
            codeList.Add(Code.Mul);
            codeList.SaveLocal(1);           
            
            // init 'i' (index)
            codeList.LoadConstant(1);
            codeList.SaveLocal(2);

            var labelLoopStart = codeList.Branch(Code.Br, Code.Br_S);
            // loop start

            var labelLoopBack = codeList.CreateLabel();

            codeList.LoadLocal(0);
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 9);
            codeList.Add(Code.Ldfld, 11);
            codeList.LoadLocal(2);
            codeList.LoadConstant(1);
            codeList.Add(Code.Sub);
            codeList.Add(Code.Ldelem_I4);
            codeList.Add(Code.Mul);
            codeList.SaveLocal(0);
            codeList.LoadLocal(1);
            codeList.LoadArgument(3);
            codeList.LoadConstant(4);
            codeList.Add(Code.Sub);
            codeList.Add(Code.Dup);
            codeList.SaveArgument(3);
            codeList.Add(Code.Ldind_I4);
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 9);
            codeList.Add(Code.Ldfld, 10);
            codeList.LoadLocal(2);
            codeList.Add(Code.Ldelem_I4);
            codeList.Add(Code.Sub);
            codeList.LoadLocal(0);
            codeList.Add(Code.Mul);
            codeList.Add(Code.Add);
            codeList.SaveLocal(1);
            codeList.LoadLocal(2);
            codeList.LoadConstant(1);
            codeList.Add(Code.Add);
            codeList.SaveLocal(2);

            codeList.Add(labelLoopStart);

            codeList.LoadLocal(2);
            codeList.LoadArgument(2);

            codeList.Branch(Code.Blt, Code.Blt_S, labelLoopBack);

            // set Value of TypedReference
            codeList.LoadArgument(1);
            codeList.Add(Code.Ldflda, 1);

            // align index (array offset) (Local.0) and save it to TypedReferenece
            // Load reference to an array (do not load reference to field data)
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 9);
            codeList.LoadLocal(1);
            // load address of an element
            codeList.Add(Code.Ldelema, 7);

            // elementSize 
            codeList.LoadArgument(0);
            codeList.Add(Code.Castclass, 4);
            codeList.Add(Code.Ldfld, 6);

            // elementSize - 1
            codeList.LoadConstant(1);
            codeList.Add(Code.Sub);
            codeList.Add(Code.Dup);
            codeList.SaveLocal(2);

            // size + align - 1
            codeList.Add(Code.Add);

            // size &= ~(align - 1)
            codeList.LoadLocal(2);
            codeList.LoadConstant(-1);
            codeList.Add(Code.Xor);
            codeList.Add(Code.And);

            // Save address into TypedReference Value
            codeList.Add(Code.Conv_I);
            codeList.Add(Code.Stfld, 3);

            codeList.Add(Code.Ret);

            var typedReferenceType = typeResolver.System.System_TypedReference;
            var intPtrType = typeResolver.System.System_IntPtr;
            var byteType = typeResolver.System.System_Byte;
            var arrayType = byteType.ToArrayType(1);
            var multiArrayType = byteType.ToArrayType(2);

            var tokenResolutions = new List<object>();
            tokenResolutions.Add(typedReferenceType.GetFieldByName("Value", typeResolver));
            tokenResolutions.Add(typedReferenceType.GetFieldByName("Type", typeResolver));
            tokenResolutions.Add(intPtrType.GetFieldByName("m_value", typeResolver));
            tokenResolutions.Add(arrayType);
            tokenResolutions.Add(arrayType.GetFieldByName("typeCode", typeResolver));
            tokenResolutions.Add(arrayType.GetFieldByName("elementSize", typeResolver));
            tokenResolutions.Add(byteType);
            tokenResolutions.Add(arrayType.GetFieldByName("rank", typeResolver));
            tokenResolutions.Add(multiArrayType);
            tokenResolutions.Add(multiArrayType.GetFieldByName("lowerBounds", typeResolver));
            tokenResolutions.Add(multiArrayType.GetFieldByName("lengths", typeResolver));

            var locals = new List<IType>();
            locals.Add(typeResolver.System.System_Int32);
            locals.Add(typeResolver.System.System_Int32);
            locals.Add(typeResolver.System.System_Int32);

            var parameters = new List<IParameter>();
            parameters.Add(typeResolver.System.System_Void.ToPointerType().ToParameter());
            parameters.Add(typeResolver.System.System_Int32.ToParameter());
            parameters.Add(typeResolver.System.System_Int32.ToPointerType().ToParameter());

            MethodBodyBank.Register(Name, codeList.GetCode(), tokenResolutions, locals, parameters);
        }
Exemplo n.º 16
0
        public static void GetMulticastDelegateInvoke(
            IMethod method,
            ITypeResolver typeResolver,
            out byte[] code,
            out IList <object> tokenResolutions,
            out IList <IType> locals,
            out IList <IParameter> parameters)
        {
            parameters = method.GetParameters().ToList();

            var codeList = new IlCodeBuilder();

            codeList.Add(Code.Ldarg_0);
            codeList.Add(Code.Ldfld, 1);

#if MSCORLIB
            // to load value from IntPtr
            codeList.Add(Code.Ldind_I);
#endif

            var jumpForBrtrue_S = codeList.Branch(Code.Brtrue, Code.Brtrue_S);
            codeList.Add(Code.Call, 2);
            codeList.Add(jumpForBrtrue_S);

            codeList.Add(Code.Ldc_I4_0);
            codeList.Add(Code.Stloc_0);

            var labelForFirstJump = codeList.Branch(Code.Br, Code.Br_S);

            // label
            var labelForConditionLoop = codeList.CreateLabel();

            codeList.Add(Code.Ldarg_0);
            codeList.Add(Code.Ldfld, 3);

#if MSCORLIB
            codeList.Add(Code.Castclass, 5);
#endif

            codeList.Add(Code.Ldloc_0);
            codeList.Add(Code.Ldelem_Ref);

            var index = 1;
            foreach (var parameter in parameters)
            {
                codeList.LoadArgument(index);
                index++;
            }

            codeList.Add(Code.Callvirt, 4);

            if (!method.ReturnType.IsVoid())
            {
                codeList.Add(Code.Stloc_1);
            }

            codeList.LoadLocal(0);
            codeList.LoadConstant(1);
            codeList.Add(Code.Add);
            codeList.SaveLocal(0);

            // label
            codeList.Add(labelForFirstJump);

            // for test
            codeList.LoadLocal(0);
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldfld, 1);

#if MSCORLIB
            // to load value from IntPtr
            codeList.Add(Code.Ldind_I);
#endif

            codeList.Branch(Code.Blt, Code.Blt_S, labelForConditionLoop);

            if (!method.ReturnType.IsVoid())
            {
                codeList.Add(Code.Ldloc_1);
            }

            codeList.Add(Code.Ret);

            code = codeList.GetCode();

            locals = new List <IType>();
            locals.Add(typeResolver.System.System_Int32);
            if (!method.ReturnType.IsVoid())
            {
                locals.Add(method.ReturnType);
            }

            tokenResolutions = new List <object>();

            // 1
            tokenResolutions.Add(method.DeclaringType.BaseType.GetFieldByName("_invocationCount", typeResolver));

            // call Delegate.Invoke
            // 2
            tokenResolutions.Add(
                new SynthesizedStaticMethod(
                    string.Empty,
                    method.DeclaringType,
                    typeResolver.System.System_Void,
                    new List <IParameter>(),
                    (llvmWriter, opCode) =>
            {
                // get element size
                llvmWriter.WriteDelegateInvoke(method, true, true);
            }));

            // 3
            tokenResolutions.Add(method.DeclaringType.BaseType.GetFieldByName("_invocationList", typeResolver));

            // call Default stub for now - "ret undef";
            // 4
            tokenResolutions.Add(IlReader.Methods(method.DeclaringType, typeResolver).First(m => m.Name == "Invoke"));

#if MSCORLIB
            // 5, to case Object to Object[]
            tokenResolutions.Add(typeResolver.System.System_Object.ToArrayType(1));
#endif
        }
Exemplo n.º 17
0
        public static void GetCtorSBytePtr(ITypeResolver typeResolver, out byte[] code, out IList<object> tokenResolutions, out IList<IType> locals, out IList<IParameter> parameters)
        {
            IType systemString = typeResolver.System.System_String;

            var codeBuilder = new IlCodeBuilder();

            codeBuilder.LoadArgument(0);
            
            // calculate length
            codeBuilder.LoadArgument(0);
            codeBuilder.Add(Code.Call, 2);

            codeBuilder.Add(Code.Call, 3);
            codeBuilder.Add(Code.Call, 1);
            codeBuilder.Add(Code.Ret);

            code = codeBuilder.GetCode();

            locals = new List<IType>();

            tokenResolutions = new List<object>();
            tokenResolutions.Add(systemString.GetMethodByName("CreateStringFromEncoding", typeResolver));
            tokenResolutions.Add(systemString.GetMethodByName("strlen", typeResolver));
            tokenResolutions.Add(typeResolver.ResolveType("System.Text.Encoding").GetMethodByName("get_ASCII", typeResolver));

            parameters = new List<IParameter>();
            parameters.Add(typeResolver.System.System_SByte.ToPointerType().ToParameter());
        }
        public static void Register(ITypeResolver typeResolver)
        {
            var codeList = new IlCodeBuilder();

            // get TypeCode of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 2);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Conv_I4);

            // switch
            var @switch = codeList.Switch();

            // goto default case
            //var defaultCaseLabel1 = codeList.Branch(Code.Br, Code.Br_S);

            // TODO: do not support Structs for now
            var defaultCaseLabel1 = codeList.CreateLabel();
            codeList.Add(Code.Newobj, 20);
            codeList.Add(Code.Throw);

            // case 0(TypeCode.Empty) -> Default
            @switch.Labels.Add(defaultCaseLabel1);

            // case 1(TypeCode.Object) -> Default
            @switch.Labels.Add(defaultCaseLabel1);

            // case 2(TypeCode.DBNull) -> Default
            @switch.Labels.Add(defaultCaseLabel1);

            // case 3(TypeCode.Boolean)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Ldind_U1);
            codeList.Add(Code.Box, 6);
            codeList.Add(Code.Ret);

            // case 4(TypeCode.Char)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Ldind_U2);
            codeList.Add(Code.Box, 7);
            codeList.Add(Code.Ret);

            // case 5(TypeCode.SByte)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Ldind_I1);
            codeList.Add(Code.Box, 8);
            codeList.Add(Code.Ret);

            // case 6(TypeCode.Byte)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Ldind_U1);
            codeList.Add(Code.Box, 9);
            codeList.Add(Code.Ret);

            // case 7(TypeCode.Int16)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Ldind_I2);
            codeList.Add(Code.Box, 10);
            codeList.Add(Code.Ret);

            // case 8(TypeCode.UInt16)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Ldind_U2);
            codeList.Add(Code.Box, 11);
            codeList.Add(Code.Ret);

            // case 9(TypeCode.Int32)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Ldind_I4);
            codeList.Add(Code.Box, 12);
            codeList.Add(Code.Ret);

            // case 10(TypeCode.UInt32)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Ldind_U4);
            codeList.Add(Code.Box, 13);
            codeList.Add(Code.Ret);

            // case 11(TypeCode.Int64)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Ldind_I8);
            codeList.Add(Code.Box, 14);
            codeList.Add(Code.Ret);

            // case 12(TypeCode.UInt64)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Ldind_I8);
            codeList.Add(Code.Box, 15);
            codeList.Add(Code.Ret);

            // case 13(TypeCode.Single)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Ldind_R4);
            codeList.Add(Code.Box, 16);
            codeList.Add(Code.Ret);

            // case 14(TypeCode.Double)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Ldind_R8);
            codeList.Add(Code.Box, 17);
            codeList.Add(Code.Ret);

            // case 15(TypeCode.Decimal)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Box, 18);
            codeList.Add(Code.Ret);

            // case 16(TypeCode.DateTime)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Box, 19);
            codeList.Add(Code.Ret);

            // case 17 -> Default
            @switch.Labels.Add(codeList.CreateLabel());
            // throw NotSupportedException
            codeList.Add(Code.Newobj, 20);
            codeList.Add(Code.Throw);

            // case 18(TypeCode.String) -> Default
            @switch.Labels.Add(defaultCaseLabel1);

            // default:
            codeList.Add(defaultCaseLabel1);

            // get Value of TypedReference (default case)
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Castclass, 4);
            codeList.Add(Code.Ldind_Ref);

            codeList.Add(Code.Ret);

            var typedReferenceType = typeResolver.System.System_TypedReference;
            var intPtrType = typeResolver.System.System_IntPtr;

            var tokenResolutions = new List<object>();
            tokenResolutions.Add(typedReferenceType.GetFieldByName("Value", typeResolver));
            tokenResolutions.Add(typedReferenceType.GetFieldByName("Type", typeResolver));
            tokenResolutions.Add(intPtrType.GetFieldByName("m_value", typeResolver));
            tokenResolutions.Add(typeResolver.System.System_Object.ToPointerType());
            tokenResolutions.Add(typeResolver.System.System_Object);
            tokenResolutions.Add(typeResolver.System.System_Boolean);
            tokenResolutions.Add(typeResolver.System.System_Char);
            tokenResolutions.Add(typeResolver.System.System_SByte);
            tokenResolutions.Add(typeResolver.System.System_Byte);
            tokenResolutions.Add(typeResolver.System.System_Int16);
            tokenResolutions.Add(typeResolver.System.System_UInt16);
            tokenResolutions.Add(typeResolver.System.System_Int32);
            tokenResolutions.Add(typeResolver.System.System_UInt32);
            tokenResolutions.Add(typeResolver.System.System_Int64);
            tokenResolutions.Add(typeResolver.System.System_UInt64);
            tokenResolutions.Add(typeResolver.System.System_Single);
            tokenResolutions.Add(typeResolver.System.System_Double);
            tokenResolutions.Add(typeResolver.System.System_Decimal);
            tokenResolutions.Add(typeResolver.System.System_DateTime);
            tokenResolutions.Add(
                IlReader.Constructors(typeResolver.System.System_NotSupportedException, typeResolver).First(c => !c.GetParameters().Any()));

            var locals = new List<IType>();

            var parameters = new List<IParameter>();
            parameters.Add(typeResolver.System.System_Void.ToPointerType().ToParameter());

            MethodBodyBank.Register(Name, codeList.GetCode(), tokenResolutions, locals, parameters);
        }
Exemplo n.º 19
0
        public static void GetStrLen(ITypeResolver typeResolver, out byte[] code, out IList<object> tokenResolutions, out IList<IType> locals, out IList<IParameter> parameters)
        {
            var codeBuilder = new IlCodeBuilder();

            codeBuilder.LoadArgument(0);
            codeBuilder.SaveLocal(0);
            var initialJumpLabel = codeBuilder.Branch(Code.Br, Code.Br_S);

            var jumpBack = codeBuilder.CreateLabel();

            codeBuilder.LoadLocal(0);
            codeBuilder.LoadConstant(1);
            codeBuilder.Add(Code.Conv_I);
            codeBuilder.Add(Code.Add);
            codeBuilder.SaveLocal(0);

            codeBuilder.Add(initialJumpLabel);

            codeBuilder.LoadLocal(0);
            codeBuilder.Add(Code.Ldind_I1);
            codeBuilder.LoadConstant(0);

            codeBuilder.Branch(Code.Bgt, Code.Bgt_S, jumpBack);

            codeBuilder.LoadLocal(0);
            codeBuilder.LoadArgument(0);
            codeBuilder.Add(Code.Sub);

            // and if element size is bugger 1, you need to devide it by element size
            // codeBuilder.LoadConstant(<size>);
            // codeBuilder.Add(Code.Div);

            codeBuilder.Add(Code.Ret);

            code = codeBuilder.GetCode();

            locals = new List<IType>();
            locals.Add(typeResolver.System.System_SByte.ToPointerType());

            tokenResolutions = new List<object>();

            parameters = new List<IParameter>();
            parameters.Add(typeResolver.System.System_SByte.ToPointerType().ToParameter());
        }
        public static void Register(ITypeResolver typeResolver)
        {
            var codeList = new IlCodeBuilder();

            // get TypeCode of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 2);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Conv_I4);

            // switch
            var @switch = codeList.Switch();

            // goto default case
            //var defaultCaseLabel1 = codeList.Branch(Code.Br, Code.Br_S);

            // TODO: do not support Structs for now
            var defaultCaseLabel1 = codeList.CreateLabel();

            codeList.Add(Code.Newobj, 20);
            codeList.Add(Code.Throw);

            // case 0(TypeCode.Empty) -> Default
            @switch.Labels.Add(defaultCaseLabel1);

            // case 1(TypeCode.Object) -> Default
            @switch.Labels.Add(defaultCaseLabel1);

            // case 2(TypeCode.DBNull) -> Default
            @switch.Labels.Add(defaultCaseLabel1);

            // case 3(TypeCode.Boolean)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.LoadArgument(1);
            codeList.Add(Code.Unbox, 6);
            codeList.Add(Code.Stind_I1);
            codeList.Add(Code.Ret);

            // case 4(TypeCode.Char)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.LoadArgument(1);
            codeList.Add(Code.Unbox, 7);
            codeList.Add(Code.Stind_I2);
            codeList.Add(Code.Ret);

            // case 5(TypeCode.SByte)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.LoadArgument(1);
            codeList.Add(Code.Unbox, 8);
            codeList.Add(Code.Stind_I1);
            codeList.Add(Code.Ret);

            // case 6(TypeCode.Byte)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.LoadArgument(1);
            codeList.Add(Code.Unbox, 9);
            codeList.Add(Code.Stind_I1);
            codeList.Add(Code.Ret);

            // case 7(TypeCode.Int16)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.LoadArgument(1);
            codeList.Add(Code.Unbox, 10);
            codeList.Add(Code.Stind_I2);
            codeList.Add(Code.Ret);

            // case 8(TypeCode.UInt16)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.LoadArgument(1);
            codeList.Add(Code.Unbox, 11);
            codeList.Add(Code.Stind_I2);
            codeList.Add(Code.Ret);

            // case 9(TypeCode.Int32)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.LoadArgument(1);
            codeList.Add(Code.Unbox, 12);
            codeList.Add(Code.Stind_I4);
            codeList.Add(Code.Ret);

            // case 10(TypeCode.UInt32)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.LoadArgument(1);
            codeList.Add(Code.Unbox, 13);
            codeList.Add(Code.Stind_I4);
            codeList.Add(Code.Ret);

            // case 11(TypeCode.Int64)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.LoadArgument(1);
            codeList.Add(Code.Unbox, 14);
            codeList.Add(Code.Stind_I8);
            codeList.Add(Code.Ret);

            // case 12(TypeCode.UInt64)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.LoadArgument(1);
            codeList.Add(Code.Unbox, 15);
            codeList.Add(Code.Stind_I8);
            codeList.Add(Code.Ret);

            // case 13(TypeCode.Single)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.LoadArgument(1);
            codeList.Add(Code.Unbox, 16);
            codeList.Add(Code.Stind_R4);
            codeList.Add(Code.Ret);

            // case 14(TypeCode.Double)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.LoadArgument(1);
            codeList.Add(Code.Unbox, 17);
            codeList.Add(Code.Stind_R8);
            codeList.Add(Code.Ret);

            // case 15(TypeCode.Decimal)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.LoadArgument(1);
            codeList.Add(Code.Unbox, 18);
            codeList.Add(Code.Ret);

            // case 16(TypeCode.DateTime)
            @switch.Labels.Add(codeList.CreateLabel());
            // get Value of TypedReference
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.LoadArgument(1);
            codeList.Add(Code.Unbox, 19);
            codeList.Add(Code.Ret);

            // case 17 -> Default
            @switch.Labels.Add(codeList.CreateLabel());
            // throw NotSupportedException
            codeList.Add(Code.Newobj, 20);
            codeList.Add(Code.Throw);

            // case 18(TypeCode.String) -> Default
            @switch.Labels.Add(defaultCaseLabel1);

            // default:
            codeList.Add(defaultCaseLabel1);

            // get Value of TypedReference (default case)
            codeList.LoadArgument(0);
            codeList.Add(Code.Ldflda, 1);
            // IntPtr.m_value
            codeList.Add(Code.Ldfld, 3);
            codeList.Add(Code.Castclass, 4);
            codeList.LoadArgument(1);
            codeList.Add(Code.Stind_Ref);

            codeList.Add(Code.Ret);

            var typedReferenceType = typeResolver.System.System_TypedReference;
            var intPtrType         = typeResolver.System.System_IntPtr;

            var tokenResolutions = new List <object>();

            tokenResolutions.Add(typedReferenceType.GetFieldByName("Value", typeResolver));
            tokenResolutions.Add(typedReferenceType.GetFieldByName("Type", typeResolver));
            tokenResolutions.Add(intPtrType.GetFieldByName("m_value", typeResolver));
            tokenResolutions.Add(typeResolver.System.System_Object.ToPointerType());
            tokenResolutions.Add(typeResolver.System.System_Object);
            tokenResolutions.Add(typeResolver.System.System_Boolean);
            tokenResolutions.Add(typeResolver.System.System_Char);
            tokenResolutions.Add(typeResolver.System.System_SByte);
            tokenResolutions.Add(typeResolver.System.System_Byte);
            tokenResolutions.Add(typeResolver.System.System_Int16);
            tokenResolutions.Add(typeResolver.System.System_UInt16);
            tokenResolutions.Add(typeResolver.System.System_Int32);
            tokenResolutions.Add(typeResolver.System.System_UInt32);
            tokenResolutions.Add(typeResolver.System.System_Int64);
            tokenResolutions.Add(typeResolver.System.System_UInt64);
            tokenResolutions.Add(typeResolver.System.System_Single);
            tokenResolutions.Add(typeResolver.System.System_Double);
            tokenResolutions.Add(typeResolver.System.System_Decimal);
            tokenResolutions.Add(typeResolver.System.System_DateTime);
            tokenResolutions.Add(
                IlReader.Constructors(typeResolver.System.System_NotSupportedException, typeResolver).First(c => !c.GetParameters().Any()));

            var locals = new List <IType>();

            var parameters = new List <IParameter>();

            parameters.Add(typeResolver.System.System_Void.ToPointerType().ToParameter());
            parameters.Add(typeResolver.System.System_Object.ToParameter());

            MethodBodyBank.Register(Name, codeList.GetCode(), tokenResolutions, locals, parameters);
        }