Exemplo n.º 1
0
        public static CilInstruction ResolveByType(vmInstruction instr)
        {
            //If you have a better way lmk :D
            var type = instr.Operand;

            if (type is null)
            {
                return(new CilInstruction(CilOpCodes.Ldnull));
            }
            if (type is int)
            {
                return(new CilInstruction(CilOpCodes.Ldc_I4, instr.Operand));
            }
            if (type is float)
            {
                return(new CilInstruction(CilOpCodes.Ldc_R4, instr.Operand));
            }
            if (type is double)
            {
                return(new CilInstruction(CilOpCodes.Ldc_R8, instr.Operand));
            }
            if (type is string)
            {
                return(new CilInstruction(CilOpCodes.Ldstr, instr.Operand));
            }
            if (type is long)
            {
                return(new CilInstruction(CilOpCodes.Ldc_I8, instr.Operand));
            }
            return(new CilInstruction(CilOpCodes.Nop));
        }
Exemplo n.º 2
0
        public static CilInstruction CreateInstruction(VirtualizedMethod method, vmInstruction vmInstruction)
        {
            CilInstruction instruction;

            switch (vmInstruction.OpCode)
            {
            case vmOpCode.VmCall:
                instruction = new CilInstruction(CilOpCodes.Call,
                                                 method.Parent.Module.LookupMember(
                                                     int.Parse(((string)vmInstruction.Operand).Substring(2))));
                break;

            case vmOpCode.VmLdc:
                instruction = ResolveByType(vmInstruction);
                break;

            case vmOpCode.VmArray:
                instruction = (int)vmInstruction.Operand switch
                {
                    0 => new CilInstruction(CilOpCodes.Ldelem),
                    1 => new CilInstruction(CilOpCodes.Stelem),
                    _ => new CilInstruction(CilOpCodes.Nop)
                };
                break;

            case vmOpCode.VmLoc:
                instruction = int.Parse(((string)vmInstruction.Operand)[0].ToString()) switch
                {
                    0 => new CilInstruction(CilOpCodes.Ldloc),
                    _ => new CilInstruction(CilOpCodes.Stloc)
                };
                instruction.Operand = int.Parse(((string)vmInstruction.Operand).Substring(1));
                break;

            case vmOpCode.VmArg:
                instruction = int.Parse(((string)vmInstruction.Operand)[0].ToString()) switch
                {
                    0 => new CilInstruction(CilOpCodes.Ldarg),
                    _ => new CilInstruction(CilOpCodes.Starg)
                };
                instruction.Operand = int.Parse(((string)vmInstruction.Operand).Substring(1));
                break;

            case vmOpCode.VmFld:
                instruction =
                    new CilInstruction(CilOpCodes.Ldfld,
                                       method.Parent.Module.LookupMember(
                                           int.Parse(((string)vmInstruction.Operand).Substring(1))));
                break;

            case vmOpCode.VmConv:
                instruction = (int)vmInstruction.Operand switch
                {
                    0 => new CilInstruction(CilOpCodes.Conv_R4),
                    1 => new CilInstruction(CilOpCodes.Conv_R8),
                    _ => new CilInstruction(CilOpCodes.Nop)
                };
                break;

            case vmOpCode.Ldtoken:
                instruction = new CilInstruction(CilOpCodes.Ldtoken,
                                                 method.Parent.Module.LookupMember(int.Parse(((string)vmInstruction.Operand).Substring(1))));
                break;

            case vmOpCode.Brfalse:
                instruction = new CilInstruction(CilOpCodes.Brfalse, vmInstruction.Operand);
                break;

            case vmOpCode.Brtrue:
                instruction = new CilInstruction(CilOpCodes.Brtrue, vmInstruction.Operand);
                break;

            case vmOpCode.Br:
                instruction = new CilInstruction(CilOpCodes.Br, vmInstruction.Operand);
                break;

            case vmOpCode.Newobj:
                instruction = new CilInstruction(CilOpCodes.Newobj,
                                                 method.Parent.Module.LookupMember((int)vmInstruction.Operand));
                break;

            case vmOpCode.Box:
                instruction = new CilInstruction(CilOpCodes.Box,
                                                 new ReferenceImporter(method.Parent.Module).ImportType(GetTypeDefOrRef(method.Parent.Module,
                                                                                                                        (string)vmInstruction.Operand)));
                break;

            default:
                instruction = new CilInstruction(Convert(vmInstruction.OpCode));
                break;
            }

            return(instruction);
        }