/// <summary>
 /// Initializes a new instance of the <see cref="VariableSignature"/> class.
 /// </summary>
 /// <param name="provider">The provider.</param>
 /// <param name="token">The token.</param>
 public VariableSignature(VariableSignature signature)
     : base(signature)
 {
     this.customMods = signature.customMods;
     this.modifier = signature.modifier;
     this.type = signature.type;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="GenericInstSigType"/> class.
 /// </summary>
 /// <param name="baseType">Type of the base.</param>
 /// <param name="genericArguments">The generic args.</param>
 public GenericInstSigType(TypeSigType baseType, SigType[] genericArguments)
     : base(CilElementType.GenericInst)
 {
     this.baseType = baseType;
     this.genericArguments = genericArguments;
     this.containsGenericParameters = CheckContainsOpenGenericParameters();
 }
 public void ApplyGenericType(SigType[] genericArguments)
 {
     if (this.Type is VarSigType)
     {
         this.Type = genericArguments[(Type as VarSigType).Index];
     }
 }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StobjInstruction"/> class.
 /// </summary>
 /// <param name="opcode">The opcode.</param>
 public StobjInstruction(OpCode opcode)
     : base(opcode)
 {
     switch (opcode) {
         case OpCode.Stind_i1:
             _valueType = new SigType(CilElementType.I1);
             break;
         case OpCode.Stind_i2:
             _valueType = new SigType(CilElementType.I2);
             break;
         case OpCode.Stind_i4:
             _valueType = new SigType(CilElementType.I4);
             break;
         case OpCode.Stind_i8:
             _valueType = new SigType(CilElementType.I8);
             break;
         case OpCode.Stind_r4:
             _valueType = new SigType(CilElementType.R4);
             break;
         case OpCode.Stind_r8:
             _valueType = new SigType(CilElementType.R8);
             break;
         case OpCode.Stind_i:
             _valueType = new SigType(CilElementType.I);
             break;
         case OpCode.Stind_ref: // FIXME: Really object?
             _valueType = new SigType(CilElementType.Object);
             break;
         default:
             throw new NotImplementedException();
     }
 }
        /// <summary>
        /// Creates the ISR methods.
        /// </summary>
        /// <param name="compiler">The compiler.</param>
        private void CreateISRMethods(AssemblyCompiler compiler)
        {
            // Get RuntimeMethod for the Mosa.Kernel.X86.IDT.InterruptHandler
            RuntimeType rt = RuntimeBase.Instance.TypeLoader.GetType(@"Mosa.Kernel.X86.IDT");
            RuntimeMethod InterruptMethod = FindMethod(rt, "InterruptHandler");

            SigType I1 = new SigType(CilElementType.I1);
            SigType I2 = new SigType(CilElementType.I4);

            RegisterOperand ecx1 = new RegisterOperand(I1, GeneralPurposeRegister.ECX);
            RegisterOperand ecx2 = new RegisterOperand(I2, GeneralPurposeRegister.ECX);

            for (int i = 0; i <= 256; i++) {
                InstructionSet set = new InstructionSet(100);
                Context ctx = new Context(set, -1);

                ctx.AppendInstruction(CPUx86.Instruction.CliInstruction);
                ctx.AppendInstruction(CPUx86.Instruction.PushadInstruction);
                if ((i != 8) && (i < 10 || i > 14)) // For IRQ 8, 10, 11, 12, 13, 14 the cpu automatically pushed the error code
                    ctx.AppendInstruction(CPUx86.Instruction.PushInstruction, null, new ConstantOperand(I1, 0x0));
                ctx.AppendInstruction(CPUx86.Instruction.PushInstruction, null, new ConstantOperand(I2, i));
                ctx.AppendInstruction(CPUx86.Instruction.CallInstruction, InterruptMethod);
                // TODO: Replace next two instructions with add esp, 5 ;Stack clearing
                ctx.AppendInstruction(CPUx86.Instruction.PopInstruction, ecx2);
                ctx.AppendInstruction(CPUx86.Instruction.PopInstruction, ecx1);
                ctx.AppendInstruction(CPUx86.Instruction.PopadInstruction);
                ctx.AppendInstruction(CPUx86.Instruction.StiInstruction);
                ctx.AppendInstruction(CPUx86.Instruction.IRetdInstruction);

                CompilerGeneratedMethod method = LinkTimeCodeGenerator.Compile(compiler, @"InterruptISR" + i.ToString(), set);
            }
        }
        /// <summary>
        /// Retrieves a constant operand to represent the null-value.
        /// </summary>
        /// <returns>A new instance of <see cref="ConstantOperand"/>, that represents the null value.</returns>
        public static ConstantOperand GetNull()
        {
            if (_sObject == null)
                _sObject = new SigType(CilElementType.Object);

            return new ConstantOperand(_sObject, null);
        }
        /// <summary>
        /// Creates the ISR methods.
        /// </summary>
        /// <param name="compiler">The compiler.</param>
        private void CreateISRMethods(AssemblyCompiler compiler)
        {
            // Create Interrupt Service Routines (ISR)
            RuntimeMethod InterruptMethod = compiler.Assembly.EntryPoint; // TODO: replace with another entry point

            SigType I1 = new SigType(CilElementType.I1);
            SigType I4 = new SigType(CilElementType.I4);
            RegisterOperand eax = new RegisterOperand(I4, GeneralPurposeRegister.EAX);

            for (int i = 0; i <= 256; i++) {
                InstructionSet set = new InstructionSet(100);
                Context ctx = new Context(set, -1);

                ctx.SetInstruction(CPUx86.Instruction.CliInstruction);
                if ((i != 8) && (i < 10 || i > 14)) // For IRQ 8, 10, 11, 12, 13, 14 the cpu automatically pushed the error code
                    ctx.AppendInstruction(CPUx86.Instruction.PushInstruction, null, new ConstantOperand(I4, 0x0));
                ctx.AppendInstruction(CPUx86.Instruction.PushadInstruction);
                ctx.AppendInstruction(CPUx86.Instruction.PushInstruction, null, new ConstantOperand(I4, i));
                // TODO: Set method parameters
                ctx.AppendInstruction(CPUx86.Instruction.CallInstruction, InterruptMethod);
                ctx.AppendInstruction(CPUx86.Instruction.PopInstruction, eax);
                ctx.AppendInstruction(CPUx86.Instruction.PopadInstruction);
                ctx.AppendInstruction(CPUx86.Instruction.PopInstruction, eax);
                ctx.AppendInstruction(CPUx86.Instruction.StiInstruction);
                //ctx.AppendInstruction(CPUx86.Instruction.IRetdInstruction);

                CompilerGeneratedMethod method = LinkTimeCodeGenerator.Compile(compiler, @"InterruptISR" + i.ToString(), set);
            }
        }
Example #8
0
        /// <summary>
        /// Validates the instruction operands and creates a matching variable for the result.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="compiler">The compiler.</param>
        public override void Validate(Context ctx, IMethodCompiler compiler)
        {
            base.Validate(ctx, compiler);

            // Validate the typecode & determine the resulting stack type
            SigType resultType;

            switch (_opcode) {
                case OpCode.Conv_u: goto case OpCode.Conv_i;
                case OpCode.Conv_i:
                    resultType = compiler.Architecture.NativeType;
                    break;

                case OpCode.Conv_i1:
                    resultType = new SigType(CilElementType.I1);
                    break;

                case OpCode.Conv_i2:
                    resultType = new SigType(CilElementType.I2);
                    break;

                case OpCode.Conv_i4:
                    resultType = new SigType(CilElementType.I4);
                    break;

                case OpCode.Conv_i8:
                    resultType = new SigType(CilElementType.I8);
                    break;

                case OpCode.Conv_r4:
                    resultType = new SigType(CilElementType.R4);
                    break;

                case OpCode.Conv_r8:
                    resultType = new SigType(CilElementType.R8);
                    break;

                case OpCode.Conv_u1:
                    resultType = new SigType(CilElementType.U1);
                    break;

                case OpCode.Conv_u2:
                    resultType = new SigType(CilElementType.U2);
                    break;

                case OpCode.Conv_u4:
                    resultType = new SigType(CilElementType.U4);
                    break;

                case OpCode.Conv_u8:
                    resultType = new SigType(CilElementType.U8);
                    break;

                default:
                    throw new NotSupportedException(@"Overflow checking conversions not supported.");
            }

            ctx.Result = compiler.CreateTemporary(resultType);
        }
Example #9
0
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public override bool Equals(SigType other)
        {
            ValueTypeSigType vtst = other as ValueTypeSigType;
            if (vtst == null)
                return false;

            return (base.Equals(other) == true && _token == vtst._token);
        }
Example #10
0
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public override bool Equals(SigType other)
        {
            SZArraySigType szast = other as SZArraySigType;
            if (szast == null)
                return false;

            return (base.Equals(other) == true && _elementType.Equals(szast._elementType) && CustomMod.Equals(_customMods, szast._customMods));
        }
Example #11
0
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public override bool Equals(SigType other)
        {
            ClassSigType cst = other as ClassSigType;
            if (null == cst)
                return false;

            return (base.Equals(other) == true && _token == cst._token);
        }
Example #12
0
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public override bool Equals(SigType other)
        {
            MVarSigType mvst = other as MVarSigType;
            if (mvst == null)
                return false;

            return (base.Equals(other) == true && index == mvst.index);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="LocalVariableSignature"/> class.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <param name="token">The token.</param>
        /// <param name="genericArguments">The generic arguments.</param>
        public LocalVariableSignature(LocalVariableSignature signature, SigType[] genericArguments)
            : base(signature.Token)
        {
            locals = new VariableSignature[signature.locals.Length];

            for (int i = 0; i < signature.locals.Length; i++)
                locals[i] = new VariableSignature(signature.locals[i], genericArguments);
        }
Example #14
0
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public override bool Equals(SigType other)
        {
            FnptrSigType fst = other as FnptrSigType;
            if (null == fst)
                return false;

            return (base.Equals(other) == true && _token == fst._token);
        }
Example #15
0
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public override bool Equals(SigType other)
        {
            GenericInstSigType gist = other as GenericInstSigType;
            if (null == gist)
                return false;

            return (base.Equals(other) && _baseType == gist._baseType && SigType.Equals(_genericArgs, gist._genericArgs));
        }
Example #16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MemberOperand"/> class.
        /// </summary>
        /// <param name="member">The member to reference.</param>
        /// <param name="type">The type of data held in the operand.</param>
        /// <param name="offset">The offset From the base register or absolute address to retrieve.</param>
        public MemberOperand(RuntimeMember member, SigType type, IntPtr offset)
            : base(type, null, offset)
        {
            if (member == null)
                throw new ArgumentNullException(@"member");

            this.member = member;
        }
Example #17
0
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public override bool Equals(SigType other)
        {
            RefSigType rst = other as RefSigType;
            if (null == rst)
                return false;

            return (base.Equals(other) && this.elementType.Matches(rst.elementType) == true);
        }
Example #18
0
        /// <summary>
        /// Initializes a new <see cref="RegisterOperand"/>.
        /// </summary>
        /// <param name="type">The signature type of the value the register holds.</param>
        /// <param name="register">The machine specific register used.</param>
        public RegisterOperand(SigType type, Register register)
            : base(type)
        {
            if (null == register)
                throw new ArgumentNullException(@"register");

            _register = register;
        }
Example #19
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RefSigType"/> class.
        /// </summary>
        /// <param name="type">The referenced type.</param>
        public RefSigType(SigType type)
            : base(CilElementType.ByRef)
        {
            if (null == type)
                throw new ArgumentNullException(@"type");

            this.elementType = type;
        }
Example #20
0
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public override bool Equals(SigType other)
        {
            VarSigType vst = other as VarSigType;
            if (null == vst)
                return false;

            return (base.Equals(other) && index == vst.index);
        }
Example #21
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PtrSigType"/> class.
        /// </summary>
        /// <param name="customMods">The custom mods.</param>
        /// <param name="type">The type.</param>
        public PtrSigType(CustomMod[] customMods, SigType type)
            : base(CilElementType.Ptr)
        {
            if (null == type)
                throw new ArgumentNullException(@"type");

            this.customMods = customMods;
            this.elementType = type;
        }
Example #22
0
        /// <summary>
        /// Parses the signature.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="index">The index.</param>
        protected override void ParseSignature(byte[] buffer, ref int index)
        {
            if (Field != buffer[index])
                return;

            index++;
            _customMods = CustomMod.ParseCustomMods(buffer, ref index);
            _type = SigType.ParseTypeSignature(buffer, ref index);
        }
Example #23
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ArraySigType"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="rank">The rank.</param>
        /// <param name="sizes">The sizes.</param>
        /// <param name="lowBounds">The low bounds.</param>
        public ArraySigType(SigType type, int rank, int[] sizes, int[] lowBounds)
            : base(CilElementType.Array)
        {
            if (null == type)
                throw new ArgumentNullException(@"type");

            _type = type;
            _rank = rank;
            _sizes = sizes;
            _lowbounds = lowBounds;
        }
Example #24
0
        /// <summary>
        /// Replaces the instrinsic call site
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="typeSystem">The type system.</param>
        public void ReplaceIntrinsicCall(Context context, ITypeSystem typeSystem)
        {
            SigType I4 = new SigType(CilElementType.I4);
            RegisterOperand esp = new RegisterOperand(I4, GeneralPurposeRegister.ESP);

            context.SetInstruction(CPUx86.Instruction.MovInstruction, esp, context.Operand1);
            context.AppendInstruction(CPUx86.Instruction.PopadInstruction);
            context.AppendInstruction(CPUx86.Instruction.AddInstruction, esp, new ConstantOperand(I4, 0x08));
            context.AppendInstruction(CPUx86.Instruction.StiInstruction);
            context.AppendInstruction(CPUx86.Instruction.IRetdInstruction);
        }
Example #25
0
        /// <summary>
        /// Initializes a new instance of LiteralInstruction.
        /// </summary>
        /// <param name="label">The label used to identify the literal in code.</param>
        /// <param name="type">The signature type of the literal data.</param>
        /// <param name="data">The data to embed along with the code stream.</param>
        public LiteralData(int label, SigType type, object data)
        {
            if (null == type)
                throw new ArgumentNullException(@"type");
            if (null == data)
                throw new ArgumentNullException(@"data");

            _label = label;
            _type = type;
            _data = data;
        }
Example #26
0
        /// <summary>
        /// Replaces the instrinsic call site
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="typeSystem">The type system.</param>
        public void ReplaceIntrinsicCall(Context context, ITypeSystem typeSystem)
        {
            Operand result = context.Result;
            SigType u4 = new SigType(CilElementType.U4);
            RegisterOperand eax = new RegisterOperand(u4, GeneralPurposeRegister.EAX);

            context.SetInstruction(CPUx86.Instruction.PopInstruction, eax);
            context.AppendInstruction(CPUx86.Instruction.AddInstruction, eax, new RegisterOperand(u4, GeneralPurposeRegister.ESP));
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, eax, new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(0)));
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, result, eax);
            context.AppendInstruction(CPUx86.Instruction.PushInstruction, null, eax);
        }
Example #27
0
        /// <summary>
        /// Replaces the instrinsic call site
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="typeSystem">The type system.</param>
        public void ReplaceIntrinsicCall(Context context, ITypeSystem typeSystem)
        {
            SigType u4 = new SigType(Runtime.Metadata.CilElementType.U4);

            RegisterOperand ebp = new RegisterOperand(u4, GeneralPurposeRegister.EBP);
            RegisterOperand esp = new RegisterOperand(u4, GeneralPurposeRegister.ESP);
            RegisterOperand eax = new RegisterOperand(u4, GeneralPurposeRegister.EAX);
            RegisterOperand ebx = new RegisterOperand(u4, GeneralPurposeRegister.EBX);
            RegisterOperand ecx = new RegisterOperand(u4, GeneralPurposeRegister.ECX);
            RegisterOperand edx = new RegisterOperand(u4, GeneralPurposeRegister.EDX);
            RegisterOperand esi = new RegisterOperand(u4, GeneralPurposeRegister.ESI);
            RegisterOperand edi = new RegisterOperand(u4, GeneralPurposeRegister.EDI);

            context.SetInstruction(CPUx86.Instruction.PushInstruction, null, ebp);
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, ebp, esp);
            context.AppendInstruction(CPUx86.Instruction.PushInstruction, null, ebx);
            context.AppendInstruction(CPUx86.Instruction.PushInstruction, null, esi);
            context.AppendInstruction(CPUx86.Instruction.PushInstruction, null, edi);

            // Load register context
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, eax, new MemoryOperand(u4, GeneralPurposeRegister.ESP, new IntPtr(28)));
            // Load exception handler
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, ecx, new MemoryOperand(u4, GeneralPurposeRegister.ESP, new IntPtr(32)));
            // Save EBP
            context.AppendInstruction(CPUx86.Instruction.PushInstruction, null, ebp);

            // Restore register values
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, ebp, new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(24)));
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, ebx, new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(4)));
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, esi, new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(16)));
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, edi, new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(20)));

            // Align ESP
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, edx, esp);
            context.AppendInstruction(CPUx86.Instruction.AndInstruction, esp, new ConstantOperand(u4, 0xFFFFFFF0u));
            context.AppendInstruction(CPUx86.Instruction.SubInstruction, esp, new ConstantOperand(u4, 0x8u));

            // Save original ESP
            context.AppendInstruction(CPUx86.Instruction.PushInstruction, null, edx);
            // Call catch handler
            context.AppendInstruction(CPUx86.Instruction.CallInstruction, ecx);

            // Restore registers
            context.AppendInstruction(CPUx86.Instruction.PopInstruction, esp);
            context.AppendInstruction(CPUx86.Instruction.PopInstruction, ebp);
            context.AppendInstruction(CPUx86.Instruction.PopInstruction, esi);
            context.AppendInstruction(CPUx86.Instruction.PopInstruction, edi);
            context.AppendInstruction(CPUx86.Instruction.PopInstruction, ebx);
            context.AppendInstruction(CPUx86.Instruction.LeaveInstruction);
            context.AppendInstruction(CPUx86.Instruction.RetInstruction);
        }
        private RegisterOperand AllocateRegister(SigType sigType)
        {
            RegisterOperand result;
            if (RequiresSseOperation(sigType))
            {
                result = new RegisterOperand(sigType, SSE2Register.XMM6);
            }
            else
            {
                result = new RegisterOperand(sigType, GeneralPurposeRegister.EDX);
            }

            return result;
        }
Example #29
0
        protected void ApplyGenericArguments(SigType[] genericArguments)
        {
            if (genericArguments == null)
            {
                return;
            }

            if (this.Type is VarSigType)
            {
                if ((Type as VarSigType).Index < genericArguments.Length)
                {
                    this.Type = genericArguments[(Type as VarSigType).Index];
                }
            }
        }
        public static Operand CreateResultOperand(IInstructionDecoder decoder, StackTypeCode operandType, SigType operandSigType)
        {
            Operand result;

            if (operandType == StackTypeCode.O || operandType == StackTypeCode.Ptr || operandType == StackTypeCode.F)
            {
                result = decoder.Compiler.CreateTemporary(operandSigType);
            }
            else
            {
                result = decoder.Compiler.CreateTemporary(Operand.SigTypeFromStackType(operandType));
            }

            return result;
        }
Example #31
0
        private static bool[] GetMaskBool(SigType type, string mask, string pattern = "")
        {
            if (type != SigType.Ida || string.IsNullOrEmpty(pattern))
            {
                return(mask.Select(x => x == 'x').ToArray());
            }

            var sb = new StringBuilder();

            foreach (var hex in pattern.Split(' '))
            {
                sb.Append(hex.Equals("?") || hex.Equals("??") ? '?' : 'x');
            }

            return(sb.ToString().Select(x => x == 'x').ToArray());
        }
        /// <summary>
        /// Decodes the specified instruction.
        /// </summary>
        /// <param name="ctx">The context.</param>
        /// <param name="decoder">The instruction decoder, which holds the code stream.</param>
        public override void Decode(Context ctx, IInstructionDecoder decoder)
        {
            // Decode base classes first
            base.Decode(ctx, decoder);

            // Do we have a type?
            if (this.typeRef == null)
            {
                // No, retrieve a type reference From the immediate argument
                Token token = decoder.DecodeTokenType();
                this.typeRef = new ClassSigType(token);
            }

            // Push the loaded value
            ctx.Result = LoadInstruction.CreateResultOperand(decoder, Operand.StackTypeFromSigType(this.typeRef), this.typeRef);
        }
Example #33
0
        /// <summary>
        /// 签名数据验证
        /// </summary>
        /// <param name="type">电子签名类型</param>
        /// <param name="tbsContent">待签章内容</param>
        /// <param name="signedValue">电子签章数据或签名值(SignedValue.xml文件内容)</param>
        public override VerifyResult Validate(SigType type, byte[] tbsContent, byte[] signedValue)
        {
            if (type == SigType.Sign)
            {
                throw new ArgumentOutOfRangeException(nameof(type), "签名类型(type)必须是 Seal,不支持电子印章验证");
            }
            //计算原文摘要
            SM3Digest md = new SM3Digest();

            md.BlockUpdate(tbsContent, 0, tbsContent.Length);
            byte[] output = new byte[32];
            md.DoFinal(output, 0);

            SesSignature sesSignature = SesSignature.GetInstance(signedValue);
            TbsSign      toSign       = sesSignature.TbsSign;

            byte[] exceptHash = toSign.DataHash.GetOctets();
            if (!Arrays.AreEqual(output, exceptHash))
            {
                return(VerifyResult.SignedNotMatch);
            }
            //加载证书
            byte[] certDer = sesSignature.Cert.GetOctets();
            X509CertificateParser parser = new X509CertificateParser();
            X509Certificate       cert   = parser.ReadCertificate(certDer);

            //判断证书是否过期
            if (!cert.IsValid(DateTime.Now))
            {
                return(VerifyResult.SealOutdated);
            }
            //获取签名验证对象
            ISigner signer           = SignerUtilities.GetSigner(sesSignature.SignatureAlgId);
            AsymmetricKeyParameter p = cert.GetPublicKey();

            signer.Init(false, p);
            byte[] buf = toSign.GetDerEncoded();
            signer.BlockUpdate(buf, 0, buf.Length);

            //预期的电子签章数据,签章值
            byte[] expect = sesSignature.Signature.GetOctets();

            //验证签名
            bool result = signer.VerifySignature(expect);

            return(result ? VerifyResult.Success : VerifyResult.SealTampered);
        }
        /// <summary>
        /// Retrieves the parameter operand at the specified <paramref name="index"/>.
        /// </summary>
        /// <param name="index">The index of the parameter operand to retrieve.</param>
        /// <returns>The operand at the specified index.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">The <paramref name="index"/> is not valid.</exception>
        public Operand GetParameterOperand(int index)
        {
            // HACK: Returning a new instance here breaks object identity. We should reuse operands,
            // which represent the same memory location. If we need to move a variable in an optimization
            // stage to a different memory location, it should actually be a new one so sharing object
            // only saves runtime space/perf.
            MethodSignature sig = method.Signature;

            if (sig.HasThis || sig.HasExplicitThis)
            {
                if (index == 0)
                {
                    return(new ParameterOperand(
                               architecture.StackFrameRegister,
                               new RuntimeParameter(@"this", 2, ParameterAttributes.In),
                               new ClassSigType(type.Token)));
                }
                // Decrement the index, as the caller actually wants a real parameter
                --index;
            }

            // A normal argument, decode it...
            IList <RuntimeParameter> methodParameters = method.Parameters;

            Debug.Assert(methodParameters != null, @"Method doesn't have arguments.");
            Debug.Assert(index < methodParameters.Count, @"Invalid argument index requested.");
            if (methodParameters == null || methodParameters.Count <= index)
            {
                throw new ArgumentOutOfRangeException(@"index", index, @"Invalid parameter index");
            }

            Operand parameter = null;

            if (parameters.Count > index)
            {
                parameter = parameters[index];
            }

            if (parameter == null)
            {
                SigType parameterType = sig.Parameters[index];
                parameter         = new ParameterOperand(architecture.StackFrameRegister, methodParameters[index], parameterType);
                parameters[index] = parameter;
            }

            return(parameter);
        }
Example #35
0
        public void ResolveInterfaces(ITypeModule typeModule)
        {
            foreach (RuntimeType type in baseGenericType.Interfaces)
            {
                if (!type.ContainsOpenGenericParameters)
                {
                    Interfaces.Add(type);
                }
                else
                {
                    CilGenericType genericType = type as CilGenericType;
                    Debug.Assert(genericType != null);

                    RuntimeType matchedInterfaceType = null;

                    // -- only needs to search generic type interfaces
                    foreach (RuntimeType runtimetype in typeModule.GetAllTypes())
                    {
                        if (runtimetype.IsInterface)
                        {
                            CilGenericType runtimetypegeneric = runtimetype as CilGenericType;
                            if (runtimetypegeneric != null)
                            {
                                if (genericType.baseGenericType == runtimetypegeneric.baseGenericType)
                                {
                                    if (SigType.Equals(GenericArguments, runtimetypegeneric.GenericArguments))
                                    {
                                        matchedInterfaceType = runtimetype;
                                        //Interfaces.Add(runtimetype);
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    if (matchedInterfaceType != null)
                    {
                        Interfaces.Add(matchedInterfaceType);
                    }
                    else
                    {
                        continue;
                    }
                }
            }
        }
        //public DigitalValidateContainer(X509Certificate certificate)
        //{
        //    _pk = certificate();
        //}


        public override void Validate(SigType type, string signAlgName, byte[] tbsContent, byte[] signedValue)
        {
            if (type != SigType.Sign)
            {
                throw new ArgumentOutOfRangeException(nameof(type), "签名类型(type)必须是 Sign,不支持电子印章验证");
            }

            Sm2Utils.Verify("", Hex.ToHexString(tbsContent), Hex.ToHexString(signedValue));

            //Signature sg = Signature.getInstance(alg, new BouncyCastleProvider());
            //sg.initVerify(pk);
            //sg.update(tbsContent);
            //if (!sg.verify(signedValue))
            //{
            //    throw new InvalidSignedValueException("签名值不一致");
            //}
        }
        /// <summary>
        /// Creates the ISR methods.
        /// </summary>
        private void CreateISRMethods()
        {
            // Get RuntimeMethod for the Mosa.Kernel.x86.IDT.InterruptHandler
            RuntimeType rt = typeSystem.GetType(@"Mosa.Kernel.x86.IDT");

            if (rt == null)
            {
                return;
            }

            RuntimeMethod InterruptMethod = FindMethod(rt, "InterruptHandler");

            if (InterruptMethod == null)
            {
                return;
            }

            SymbolOperand interruptMethod = SymbolOperand.FromMethod(InterruptMethod);

            SigType I1 = new SigType(CilElementType.I1);
            SigType I4 = new SigType(CilElementType.I4);

            RegisterOperand esp = new RegisterOperand(I4, GeneralPurposeRegister.ESP);

            for (int i = 0; i <= 255; i++)
            {
                InstructionSet set = new InstructionSet(100);
                Context        ctx = new Context(set, -1);

                ctx.AppendInstruction(CPUx86.Instruction.CliInstruction);
                if (i <= 7 || i >= 16 | i == 9)                 // For IRQ 8, 10, 11, 12, 13, 14 the cpu automatically pushed the error code
                {
                    ctx.AppendInstruction(CPUx86.Instruction.PushInstruction, null, new ConstantOperand(I1, 0x0));
                }
                ctx.AppendInstruction(CPUx86.Instruction.PushInstruction, null, new ConstantOperand(I1, (byte)i));
                ctx.AppendInstruction(CPUx86.Instruction.PushadInstruction);
                ctx.AppendInstruction(CPUx86.Instruction.CallInstruction, null, interruptMethod);
                ctx.AppendInstruction(CPUx86.Instruction.PopadInstruction);
                ctx.AppendInstruction(CPUx86.Instruction.AddInstruction, esp, new ConstantOperand(I4, 0x08));
                ctx.AppendInstruction(CPUx86.Instruction.StiInstruction);
                ctx.AppendInstruction(CPUx86.Instruction.IRetdInstruction);

                //LinkerGeneratedMethod method =
                LinkTimeCodeGenerator.Compile(this.compiler, @"InterruptISR" + i.ToString(), set, typeSystem);
            }
        }
Example #38
0
        public override VerifyResult Validate(SigType type, byte[] tbsContent, byte[] signedValue)
        {
            if (type == SigType.Sign)
            {
                throw new ArgumentOutOfRangeException(nameof(type), "签名类型(type)必须是 Seal,不支持电子印章验证");
            }

            // 计算原文摘要
            GeneralDigest md = new SM3Digest();

            md.BlockUpdate(tbsContent, 0, tbsContent.Length);
            byte[] expect = new byte[32];
            md.DoFinal(expect, 0);

            SesSignature sesSignature = SesSignature.GetInstance(signedValue);
            TbsSign      toSign       = sesSignature.ToSign;

            byte[] expectDataHash = toSign.DataHash.GetOctets();

            // 比较原文摘要
            if (!Arrays.AreEqual(expect, expectDataHash))
            {
                return(VerifyResult.SignedTampered);
            }

            // 预期的电子签章数据,签章值
            byte[]  expSigVal = sesSignature.Signature.GetOctets();
            ISigner sg        = SignerUtilities.GetSigner(toSign.SignatureAlgorithm);

            byte[] certDer = toSign.Cert.GetOctets();

            // 构造证书对象
            X509Certificate        x509Certificate = new X509CertificateParser().ReadCertificate(certDer);
            AsymmetricKeyParameter p = x509Certificate.GetPublicKey();

            sg.Init(false, p);

            byte[] input = toSign.GetDerEncoded();
            sg.BlockUpdate(input, 0, input.Length);

            if (!sg.VerifySignature(expSigVal))
            {
                return(VerifyResult.SignedTampered);
            }
            return(VerifyResult.Success);
        }
        /// <summary>
        /// Decodes the specified instruction.
        /// </summary>
        /// <param name="ctx">The context.</param>
        /// <param name="decoder">The instruction decoder, which holds the code stream.</param>
        public override void Decode(Context ctx, IInstructionDecoder decoder)
        {
            // Decode base classes first
            base.Decode(ctx, decoder);

            // Do we have a type?
            if (this.elementType == null)
            {
                // No, retrieve a type reference from the immediate argument
                Token token = decoder.DecodeTokenType();
                this.elementType = new ClassSigType(token);
            }

            StackTypeCode stackType = Operand.StackTypeFromSigType(this.elementType);
            Operand       result    = LoadInstruction.CreateResultOperand(decoder, stackType, this.elementType);

            ctx.Result = result;
        }
        /// <summary>
        /// Validates the instruction operands and creates a matching variable for the result.
        /// </summary>
        /// <param name="ctx">The context.</param>
        /// <param name="compiler">The compiler.</param>
        public override void Validate(Context ctx, IMethodCompiler compiler)
        {
            base.Validate(ctx, compiler);

            // If we're ldind.i8, fix an IL deficiency that the result may be U8
            if (opcode == OpCode.Ldind_i8 && this.typeRef.Type == CilElementType.I8)
            {
                SigType    opType = ctx.Operand1.Type;
                RefSigType rst    = opType as RefSigType;
                PtrSigType ptr    = opType as PtrSigType;

                if (rst != null && rst.ElementType.Type == CilElementType.U8 ||
                    ptr != null && ptr.ElementType.Type == CilElementType.U8)
                {
                    ctx.Result = compiler.CreateTemporary(BuiltInSigType.UInt64);
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="StelemInstruction"/> class.
        /// </summary>
        /// <param name="opcode">The opcode.</param>
        public StelemInstruction(OpCode opcode) : base(opcode, 3)
        {
            switch (opcode)
            {
            case OpCode.Stelem_i1:
                _typeRef = new SigType(CilElementType.I1);
                break;

            case OpCode.Stelem_i2:
                _typeRef = new SigType(CilElementType.I2);
                break;

            case OpCode.Stelem_i4:
                _typeRef = new SigType(CilElementType.I4);
                break;

            case OpCode.Stelem_i8:
                _typeRef = new SigType(CilElementType.I8);
                break;

            case OpCode.Stelem_i:
                _typeRef = new SigType(CilElementType.I);
                break;

            case OpCode.Stelem_r4:
                _typeRef = new SigType(CilElementType.R4);
                break;

            case OpCode.Stelem_r8:
                _typeRef = new SigType(CilElementType.R8);
                break;

            case OpCode.Stelem_ref:                     // FIXME: Really object?
                _typeRef = new SigType(CilElementType.Object);
                break;

            case OpCode.Stelem:
                _typeRef = new SigType(CilElementType.Type);
                break;

            default:
                throw new NotImplementedException("Not implemented: " + opcode);
            }
        }
        /// <summary>
        /// Parses a fixed argument in an attribute blob definition.
        /// </summary>
        /// <param name="reader">The binary reader to read it From.</param>
        /// <param name="sigType">The signature type of the value to read.</param>
        /// <returns></returns>
        private static object ParseFixedArg(BinaryReader reader, SigType sigType)
        {
            // Return value
            object result = null;

            // A vector?
            SZArraySigType arraySigType = sigType as SZArraySigType;

            if (arraySigType != null)
            {
                result = ParseSZArrayArg(reader, arraySigType);
            }
            else
            {
                result = ParseElem(reader, sigType);
            }

            return(result);
        }
Example #43
0
        /// <summary>
        /// Gets the type memory requirements.
        /// </summary>
        /// <param name="signatureType">The signature type.</param>
        /// <param name="memorySize">Receives the memory size of the type.</param>
        /// <param name="alignment">Receives alignment requirements of the type.</param>
        public override void GetTypeRequirements(SigType signatureType, out int memorySize, out int alignment)
        {
            if (signatureType == null)
            {
                throw new ArgumentNullException("signatureType");
            }

            switch (signatureType.Type)
            {
            case CilElementType.U1: memorySize = alignment = 4; break;

            case CilElementType.U2: memorySize = alignment = 4; break;

            case CilElementType.U4: memorySize = alignment = 4; break;

            case CilElementType.U8: memorySize = 8; alignment = 4; break;

            case CilElementType.I1: memorySize = alignment = 4; break;

            case CilElementType.I2: memorySize = alignment = 4; break;

            case CilElementType.I4: memorySize = alignment = 4; break;

            case CilElementType.I8: memorySize = 8; alignment = 4; break;

            case CilElementType.R4: memorySize = alignment = 4; break;

            case CilElementType.R8: memorySize = alignment = 8; break;

            case CilElementType.Boolean: memorySize = alignment = 4; break;

            case CilElementType.Char: memorySize = alignment = 4; break;

            // Platform specific
            case CilElementType.Ptr: memorySize = alignment = 4; break;

            case CilElementType.I: memorySize = alignment = 4; break;

            case CilElementType.U: memorySize = alignment = 4; break;

            default: memorySize = alignment = 4; break;
            }
        }
Example #44
0
        uint InitializeCodeHeader()
        {
            uint codeHeaderOffset = GetCodeHeaderOffset(peImage);

            ReadCodeHeader(codeHeaderOffset);
            sigType = GetSigType(codeHeader.signature);

            if (sigType == SigType.Unknown)
            {
                codeHeaderOffset = GetOldCodeHeaderOffset(peImage);
                if (codeHeaderOffset != 0)
                {
                    ReadCodeHeader(codeHeaderOffset);
                    sigType = GetSigType(codeHeader.signature);
                }
            }

            return(codeHeaderOffset);
        }
Example #45
0
        /// <summary>
        /// Decodes the specified instruction.
        /// </summary>
        /// <param name="ctx">The context.</param>
        /// <param name="decoder">The instruction decoder, which holds the code stream.</param>
        public override void Decode(Context ctx, IInstructionDecoder decoder)
        {
            // Decode base classes first
            base.Decode(ctx, decoder);

            Token token = decoder.DecodeTokenType();

            ctx.RuntimeField = decoder.TypeModule.GetField(token);

            if (ctx.RuntimeField.ContainsGenericParameter)
            {
                ;
            }

            SigType sigType = ctx.RuntimeField.SignatureType;

            Debug.Assert((ctx.RuntimeField.Attributes & FieldAttributes.Static) == FieldAttributes.Static, @"Static field access on non-static field.");
            ctx.Result = LoadInstruction.CreateResultOperand(decoder, Operand.StackTypeFromSigType(sigType), sigType);
        }
Example #46
0
        /// <summary>
        /// Replaces the instrinsic call site
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="typeSystem">The type system.</param>
        public void ReplaceIntrinsicCall(Context context, ITypeSystem typeSystem)
        {
            SigType u4 = new SigType(Runtime.Metadata.CilElementType.U4);

            // Retrieve register context
            context.SetInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(u4, GeneralPurposeRegister.EAX), new MemoryOperand(u4, GeneralPurposeRegister.ESP, new IntPtr(28)));

            // Restore registers (Note: EAX and EDX are NOT restored!)
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(u4, GeneralPurposeRegister.EDX), new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(28)));
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(u4, GeneralPurposeRegister.EBX), new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(4)));
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(u4, GeneralPurposeRegister.EDI), new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(20)));
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(u4, GeneralPurposeRegister.ESI), new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(16)));
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(u4, GeneralPurposeRegister.ESP), new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(32)));
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(u4, GeneralPurposeRegister.EBP), new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(24)));

            // Jmp to EIP (stored in EDX)
            context.AppendInstruction(CPUx86.Instruction.JmpInstruction);
            context.SetOperand(0, new RegisterOperand(u4, GeneralPurposeRegister.EDX));
        }
Example #47
0
        /// <summary>
        /// Performs stage specific processing on the compiler context.
        /// </summary>
        /// <param name="compiler">The compiler context to perform processing in.</param>
        public void Run(AssemblyCompiler compiler)
        {
            if (compiler == null)
            {
                throw new ArgumentNullException(@"compiler");
            }

            IAssemblyLinker linker = compiler.Pipeline.Find <IAssemblyLinker>();

            Debug.Assert(linker != null, @"No linker??");

            if (!secondStage)
            {
                IntPtr entryPoint = WriteMultibootEntryPoint(linker);
                WriteMultibootHeader(compiler, linker, entryPoint);
                secondStage = true;
            }
            else
            {
                TypeInitializerSchedulerStage typeInitializerSchedulerStage = compiler.Pipeline.Find <TypeInitializerSchedulerStage>();

                SigType I4 = new SigType(CilElementType.I4);

                RegisterOperand ecx = new RegisterOperand(I4, GeneralPurposeRegister.ECX);
                RegisterOperand eax = new RegisterOperand(I4, GeneralPurposeRegister.EAX);
                RegisterOperand ebx = new RegisterOperand(I4, GeneralPurposeRegister.EBX);

                InstructionSet instructionSet = new InstructionSet(16);
                Context        ctx            = new Context(instructionSet, -1);

                ctx.AppendInstruction(CPUx86.Instruction.MovInstruction, ecx, new ConstantOperand(I4, 0x200000));
                ctx.AppendInstruction(CPUx86.Instruction.MovInstruction, new MemoryOperand(I4, ecx.Register, new IntPtr(0x0)), eax);
                ctx.AppendInstruction(CPUx86.Instruction.MovInstruction, new MemoryOperand(I4, ecx.Register, new IntPtr(0x4)), ebx);
                ctx.AppendInstruction(IR.Instruction.CallInstruction);
                ctx.InvokeTarget = typeInitializerSchedulerStage.Method;
                ctx.AppendInstruction(CPUx86.Instruction.NopInstruction);
                ctx.AppendInstruction(CPUx86.Instruction.NopInstruction);
                ctx.AppendInstruction(CPUx86.Instruction.RetInstruction);

                CompilerGeneratedMethod method = LinkTimeCodeGenerator.Compile(compiler, @"MultibootInit", instructionSet);
                linker.EntryPoint = linker.GetSymbol(method);
            }
        }
Example #48
0
        /// <summary>
        /// Replaces the instrinsic call site
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="typeSystem">The type system.</param>
        public void ReplaceIntrinsicCall(Context context, ITypeSystem typeSystem)
        {
            SigType u4 = new SigType(Runtime.Metadata.CilElementType.U4);

            // Retrieve register context
            context.SetInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(u4, GeneralPurposeRegister.EAX), new MemoryOperand(u4, GeneralPurposeRegister.ESP, new IntPtr(28)));

            // Restore registers (Note: EAX and EDX are NOT restored!)
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(u4, GeneralPurposeRegister.EDX), new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(28)));
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(u4, GeneralPurposeRegister.EBX), new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(4)));
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(u4, GeneralPurposeRegister.EDI), new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(20)));
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(u4, GeneralPurposeRegister.ESI), new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(16)));
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(u4, GeneralPurposeRegister.ESP), new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(32)));
            context.AppendInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(u4, GeneralPurposeRegister.EBP), new MemoryOperand(u4, GeneralPurposeRegister.EAX, new IntPtr(24)));

            // Jmp to EIP (stored in EDX)
            context.AppendInstruction(CPUx86.Instruction.JmpInstruction);
            context.SetOperand(0, new RegisterOperand(u4, GeneralPurposeRegister.EDX));
        }
Example #49
0
        /// <summary>
        /// Gets the size of the type alignment.
        /// </summary>
        /// <param name="signatureType">The signature type.</param>
        /// <returns></returns>
        private int GetAlignmentSize(SigType signatureType)
        {
            if (signatureType == null)
            {
                throw new ArgumentNullException("signatureType");
            }

            switch (signatureType.Type)
            {
            case CilElementType.U1: return(4);

            case CilElementType.U2: return(4);

            case CilElementType.U4: return(4);

            case CilElementType.U8: return(4);

            case CilElementType.I1: return(4);

            case CilElementType.I2: return(4);

            case CilElementType.I4: return(4);

            case CilElementType.I8: return(4);

            case CilElementType.R4: return(4);

            case CilElementType.R8: return(8);

            case CilElementType.Boolean: return(4);

            case CilElementType.Char: return(4);

            // Platform specific
            case CilElementType.Ptr: return(nativePointerAlignment);

            case CilElementType.I: return(nativePointerAlignment);

            case CilElementType.U: return(nativePointerAlignment);

            default: return(4);
            }
        }
Example #50
0
        /// <summary>
        /// Requests the calling convention to create an appropriate move instruction to populate the return
        /// value of a method.
        /// </summary>
        /// <param name="ctx">The context.</param>
        /// <param name="operand">The operand, that's holding the return value.</param>
        void ICallingConvention.MoveReturnValue(Context ctx, Operand operand)
        {
            int size, alignment;

            architecture.GetTypeRequirements(operand.Type, out size, out alignment);

            // FIXME: Do not issue a move, if the operand is already the destination register
            if (4 == size || 2 == size || 1 == size)
            {
                ctx.SetInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(operand.Type, GeneralPurposeRegister.EAX), operand);
                return;
            }
            else if (8 == size && (operand.Type.Type == CilElementType.R4 || operand.Type.Type == CilElementType.R8))
            {
                if (!(operand is MemoryOperand))
                {
                    // Move the operand to memory by prepending an instruction
                }

                // BUG: Return values are in FP0, not XMM#0
                ctx.SetInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(operand.Type, SSE2Register.XMM0), operand);
                return;
            }
            else if (8 == size && (operand.Type.Type == CilElementType.I8 || operand.Type.Type == CilElementType.U8))
            {
                SigType HighType = (operand.Type.Type == CilElementType.I8) ? new SigType(CilElementType.I4) : new SigType(CilElementType.U4);
                SigType U4       = new SigType(CilElementType.U4);

                Operand opL, opH;
                LongOperandTransformationStage.SplitLongOperand(operand, out opL, out opH);

                // Like Win32: EDX:EAX
                ctx.SetInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(U4, GeneralPurposeRegister.EAX), opL);
                ctx.AppendInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(HighType, GeneralPurposeRegister.EDX), opH);

                return;
            }
            else
            {
                throw new NotSupportedException();
            }
        }
Example #51
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StobjInstruction"/> class.
        /// </summary>
        /// <param name="opcode">The opcode.</param>
        public StobjInstruction(OpCode opcode)
            : base(opcode)
        {
            switch (opcode)
            {
            case OpCode.Stind_i1:
                _valueType = new SigType(CilElementType.I1);
                break;

            case OpCode.Stind_i2:
                _valueType = new SigType(CilElementType.I2);
                break;

            case OpCode.Stind_i4:
                _valueType = new SigType(CilElementType.I4);
                break;

            case OpCode.Stind_i8:
                _valueType = new SigType(CilElementType.I8);
                break;

            case OpCode.Stind_r4:
                _valueType = new SigType(CilElementType.R4);
                break;

            case OpCode.Stind_r8:
                _valueType = new SigType(CilElementType.R8);
                break;

            case OpCode.Stind_i:
                _valueType = new SigType(CilElementType.I);
                break;

            case OpCode.Stind_ref:                     // FIXME: Really object?
                _valueType = new SigType(CilElementType.Object);
                break;

            default:
                throw new NotImplementedException();
            }
        }
Example #52
0
        /// <summary>
        /// Prepares the evaluation stack.
        /// </summary>
        /// <param name="architecture">The architecture.</param>
        private void PrepareEvaluationStack(IArchitecture architecture)
        {
            /*
             * This register allocator uses two sets of registers to keep
             * parts of the evaluation stack in memory. The first set is used
             * for integers and the second set for floating point values.
             *
             */
            SigType i  = new SigType(CilElementType.I);
            SigType fp = new SigType(CilElementType.R8);

            Register[] registerSet = architecture.RegisterSet;
            int        iregs = 0, fpregs = 0;

            // Enumerate all registers, until we've found all needed registers
            foreach (Register reg in registerSet)
            {
                if (reg.IsFloatingPoint == false)
                {
                    // A general purpose register. Do we need to allocate a stack register?
                    if (iregs < RegisterStackSize)
                    {
                        this.stackRegistersI[iregs++] = new RegisterOperand(i, reg);
                    }
                }
                else
                {
                    // A floating point register. Do we need to allocate a stack register?
                    if (fpregs < RegisterStackSize)
                    {
                        this.stackRegistersFp[fpregs++] = new RegisterOperand(fp, reg);
                    }
                }

                // If we've allocated all registers, break the loop
                if (iregs == RegisterStackSize && fpregs == RegisterStackSize)
                {
                    break;
                }
            }
        }
Example #53
0
        /// <summary>
        /// Moves the return value to 64 bit.
        /// </summary>
        /// <param name="resultOperand">The result operand.</param>
        /// <param name="ctx">The context.</param>
        private static void MoveReturnValueTo64Bit(Operand resultOperand, Context ctx)
        {
            SigType       I4            = new SigType(CilElementType.I4);
            SigType       U4            = new SigType(CilElementType.U4);
            MemoryOperand memoryOperand = resultOperand as MemoryOperand;

            if (memoryOperand == null)
            {
                return;
            }
            Operand opL, opH;

            LongOperandTransformationStage.SplitLongOperand(memoryOperand, out opL, out opH);
            //MemoryOperand opL = new MemoryOperand(U4, memoryOperand.Base, memoryOperand.Offset);
            //MemoryOperand opH = new MemoryOperand(I4, memoryOperand.Base, new IntPtr(memoryOperand.Offset.ToInt64() + 4));
            RegisterOperand eax = new RegisterOperand(U4, GeneralPurposeRegister.EAX);
            RegisterOperand edx = new RegisterOperand(I4, GeneralPurposeRegister.EDX);

            ctx.AppendInstruction(CPUx86.Instruction.MovInstruction, opL, eax);
            ctx.AppendInstruction(CPUx86.Instruction.MovInstruction, opH, edx);
        }
Example #54
0
        /// <summary>
        /// Sigs the type of the type From stack.
        /// </summary>
        /// <param name="typeCode">The type code.</param>
        /// <returns></returns>
        public static SigType SigTypeFromStackType(StackTypeCode typeCode)
        {
            SigType result = null;

            switch (typeCode)
            {
            case StackTypeCode.Int32: result = new SigType(CilElementType.I4); break;

            case StackTypeCode.Int64: result = new SigType(CilElementType.I8); break;

            case StackTypeCode.F: result = new SigType(CilElementType.R8); break;

            case StackTypeCode.O: result = new SigType(CilElementType.Object); break;

            case StackTypeCode.N: result = new SigType(CilElementType.I); break;

            default:
                throw new NotSupportedException(@"Can't convert stack type code to SigType.");
            }
            return(result);
        }
        /// <summary>
        /// Gets the type from the signature type.
        /// </summary>
        /// <param name="sigType">The signature type.</param>
        /// <returns>The System.Type represented by the signature type.</returns>
        private static Type GetTypeFromSigType(SigType sigType)
        {
            Type result = null;

            switch (sigType.Type)
            {
            case CilElementType.I: result = typeof(IntPtr); break;

            case CilElementType.I1: result = typeof(SByte); break;

            case CilElementType.I2: result = typeof(Int16); break;

            case CilElementType.I4: result = typeof(Int32); break;

            case CilElementType.I8: result = typeof(Int64); break;

            case CilElementType.U: result = typeof(UIntPtr); break;

            case CilElementType.U1: result = typeof(Byte); break;

            case CilElementType.U2: result = typeof(UInt16); break;

            case CilElementType.U4: result = typeof(UInt32); break;

            case CilElementType.U8: result = typeof(UInt64); break;

            case CilElementType.R4: result = typeof(Single); break;

            case CilElementType.R8: result = typeof(Double); break;

            case CilElementType.String: result = typeof(String); break;

            case CilElementType.Object: result = typeof(System.Object); break;

            default:
                throw new NotSupportedException();
            }

            return(result);
        }
Example #56
0
        public bool CompareSignatures(SigType pSigTypeA, SigType pSigTypeB)
        {
            IRType typeA = PresolveType(pSigTypeA);
            IRType typeB = PresolveType(pSigTypeB);

            if (typeA.ArrayType != null)
            {
                if (typeB.ArrayType == null)
                {
                    return(false);
                }
                return(typeA.ArrayType == typeB.ArrayType);
            }
            if (typeA.PointerType != null)
            {
                if (typeB.PointerType == null)
                {
                    return(false);
                }
                return(typeA.PointerType == typeB.PointerType);
            }
            if (typeA.IsTemporaryVar || typeB.IsTemporaryVar)
            {
                if (!typeA.IsTemporaryVar || !typeB.IsTemporaryVar)
                {
                    return(false);
                }
                return(typeA.TemporaryVarOrMVarIndex == typeB.TemporaryVarOrMVarIndex);
            }
            if (typeA.IsTemporaryMVar || typeB.IsTemporaryMVar)
            {
                if (!typeA.IsTemporaryMVar || !typeB.IsTemporaryMVar)
                {
                    return(false);
                }
                return(typeA.TemporaryVarOrMVarIndex == typeB.TemporaryVarOrMVarIndex);
            }
            return(typeA == typeB);
        }
Example #57
0
        /// <summary>
        /// Decodes the specified instruction.
        /// </summary>
        /// <param name="ctx">The context.</param>
        /// <param name="decoder">The instruction decoder, which holds the code stream.</param>
        public override void Decode(Context ctx, IInstructionDecoder decoder)
        {
            Token ctor = DecodeInvocationTarget(ctx, decoder, InvokeSupport);

            /*
             * HACK: We need to remove the this parameter from the operand list, as it
             * is not available yet. It is implicitly created by newobj and appropriately
             * passed. So we do as if it doesn't exist. Upon instruction expansion a call
             * to the allocator is inserted and its result is the this pointer passed. This
             * must be done by expansion though...
             *
             */

            // Remove the this argument from the invocation, it's not on the stack yet.
            ctx.OperandCount--;

            // Get the type to allocate
            SigType sigType = CreateSignatureTypeFor(decoder.Compiler.Assembly, ctor, ctx.InvokeTarget.DeclaringType);

            // Set a return value according to the type of the object allocated
            ctx.Result      = decoder.Compiler.CreateTemporary(sigType);
            ctx.ResultCount = 1;
        }
Example #58
0
        /// <summary>
        /// Finds the invoke overload.
        /// </summary>
        /// <param name="metadata">The metadata.</param>
        /// <param name="ownerType">Type of the owner.</param>
        /// <param name="nameIdx">The name idx.</param>
        /// <param name="signatureIdx">The signature idx.</param>
        /// <returns></returns>
        private object FindInvokeOverload(IMetadataProvider metadata, SigType ownerType, HeapIndexToken nameIdx, HeapIndexToken signatureIdx)
        {
            throw new NotImplementedException();

            /*
             *      MethodDefinition result = null;
             *      TypeDefinition elementTypeDef = ownerType.ElementType as TypeDefinition;
             *      string name;
             *      Debug.Assert(null != elementTypeDef, @"Cross assembly type resolution not supported yet.");
             *      if (null == elementTypeDef)
             *      {
             *              // FIXME: Resolve the reference using all referenced assemblies
             *              throw new InvalidOperationException(@"Cross assembly type resolution not supported yet.");
             *      }
             *
             *      metadata.Read(nameIdx, out name);
             *
             *      foreach (MethodDefinition methodDef in elementTypeDef.Methods)
             *      {
             *              if (methodDef.Name.Equals(name))
             *              {
             *                      // FIXME: Check the signatures...
             *                      if (IsSameSignature(metadata, methodDef.SignatureIdx, signatureIdx))
             *                      {
             *                              // We've found the method
             *                              //result = temp;
             *                              //result.OwnerType = ownerType;
             *                              result = methodDef;
             *                              break;
             *                      }
             *
             *              }
             *      }
             *
             *      return result;
             */
        }
Example #59
0
 /// <summary>
 /// Determines if the signature type fits into the register.
 /// </summary>
 /// <param name="type">The signature type to check.</param>
 /// <returns>True if the signature type fits.</returns>
 public override bool IsValidSigType(SigType type)
 {
     return(type.Type == CilElementType.I8 || type.Type == CilElementType.U8);
 }
Example #60
0
        /// <summary>
        /// Expands the given invoke instruction to perform the method call.
        /// </summary>
        /// <param name="ctx">The context.</param>
        /// <returns>
        /// A single instruction or an array of instructions, which appropriately represent the method call.
        /// </returns>
        void ICallingConvention.Expand(Context ctx)
        {
            /*
             * Calling convention is right-to-left, pushed on the stack. Return value in EAX for integral
             * types 4 bytes or less, XMM0 for floating point and EAX:EDX for 64-bit. If this is a method
             * of a type, the this argument is moved to ECX right before the call.
             *
             */

            Runtime.Vm.RuntimeMethod invokeTarget = ctx.InvokeTarget;
            Operand result   = ctx.Result;
            Operand operand1 = ctx.Operand1;

            List <Operand> operands = new List <Operand> ();

            operands.AddRange(ctx.Operands);

            int resultCount  = ctx.ResultCount;
            int operandCount = ctx.OperandCount;

            SigType         I         = new SigType(CilElementType.I);
            RegisterOperand esp       = new RegisterOperand(I, GeneralPurposeRegister.ESP);
            int             stackSize = CalculateStackSizeForParameters(operands, invokeTarget.Signature.HasThis);

            ctx.SetInstruction(CPUx86.Instruction.NopInstruction);
            if (stackSize != 0)
            {
                ctx.AppendInstruction(CPUx86.Instruction.SubInstruction, esp, new ConstantOperand(I, stackSize));
                ctx.AppendInstruction(CPUx86.Instruction.MovInstruction, new RegisterOperand(_architecture.NativeType, GeneralPurposeRegister.EDX), esp);

                Stack <Operand> operandStack = GetOperandStackFromInstruction(operands, operandCount, invokeTarget.Signature.HasThis);

                int space = stackSize;
                CalculateRemainingSpace(ctx, operandStack, ref space);
            }

            if (invokeTarget.Signature.HasThis)
            {
                RegisterOperand ecx = new RegisterOperand(I, GeneralPurposeRegister.ECX);
                ctx.AppendInstruction(CPUx86.Instruction.MovInstruction, ecx, operand1);
            }

            ctx.AppendInstruction(IR.Instruction.CallInstruction);
            ctx.InvokeTarget = invokeTarget;

            if (stackSize != 0)
            {
                ctx.AppendInstruction(CPUx86.Instruction.AddInstruction, esp, new ConstantOperand(I, stackSize));
            }

            if (resultCount > 0)
            {
                if (result.StackType == StackTypeCode.Int64)
                {
                    MoveReturnValueTo64Bit(result, ctx);
                }
                else
                {
                    MoveReturnValueTo32Bit(result, ctx);
                }
            }

            //ctx.Remove();
        }