Beispiel #1
0
        public static bool Rem(Instruction instruction, CompileContext compileContext)
        {
            var b = compileContext.Pop();
            var a = compileContext.Pop();

            compileContext.Push(new Value(a.type, CompileContext.Format("mod({0}, {1})", a, b)));
            return(true);
        }
Beispiel #2
0
        public static bool Stloc_N(Instruction instruction, CompileContext compileContext, uint index)
        {
            var value = compileContext.Pop();
            var local = compileContext.Allocate(value.type, index);

            compileContext.Assign(local, value);
            return(true);
        }
Beispiel #3
0
        public static bool Stind_R4(Instruction instruction, CompileContext compileContext)
        {
            var value  = compileContext.Pop();
            var target = compileContext.Pop();

            compileContext.Assign(target, value);
            return(true);
        }
Beispiel #4
0
        public static bool Construct(MethodReference methodReference, CompileContext compileContext)
        {
            var parameters = compileContext.Pop(methodReference.Parameters.Count);
            var method     = compileContext.Method(methodReference, parameters);

            compileContext.Push(method);
            return(true);
        }
Beispiel #5
0
        public static bool Ldobj(Instruction instruction, CompileContext compileContext)
        {
            // ldobj – copy a value from an address to the stack
            var value = compileContext.Pop();

            compileContext.Push(value);
            return(true);
        }
Beispiel #6
0
        public static bool Bgt(Instruction instruction, CompileContext compileContext)
        {
            var b = compileContext.Pop();
            var a = compileContext.Pop();

            compileContext.Push(typeof(int).GetTypeDefinition(), CompileContext.Format("int({0} > {1})", a, b));
            return(true);
        }
Beispiel #7
0
        public static bool Ble(Instruction instruction, CompileContext compileContext)
        {
            // ble target - Branch to target if less than or equal to.
            var b = compileContext.Pop();
            var a = compileContext.Pop();

            compileContext.Push(typeof(int).GetTypeDefinition(), CompileContext.Format("int({0} <= {1})", a, b));
            return(true);
        }
Beispiel #8
0
        public static bool Ldfld(Instruction instruction, CompileContext compileContext)
        {
            // ldfld field - Push the value of field of object (or value type) obj, onto the stack.
            var fieldDefinition = (instruction.Operand as FieldReference).Resolve();
            var @this           = compileContext.Pop();

            compileContext.Push(fieldDefinition.FieldType, compileContext.Field(@this, fieldDefinition));
            return(true);
        }
Beispiel #9
0
        public static bool Ldloc_S(Instruction instruction, CompileContext compileContext)
        {
            // ldloc.s indx - Load local variable of index indx onto stack, short form.
            var variableDefinition = instruction.Operand as VariableDefinition;
            var local = compileContext.Allocate(variableDefinition.VariableType, (uint)variableDefinition.Index);

            compileContext.Push(local);
            return(true);
        }
Beispiel #10
0
        public static bool Stobj(Instruction instruction, CompileContext compileContext)
        {
            // stobj typeTok - Store a value of type typeTok at an address.
            var value  = compileContext.Pop();
            var target = compileContext.Pop();

            compileContext.Assign(target, value);
            return(true);
        }
Beispiel #11
0
        public static bool Ldarga_S(Instruction instruction, CompileContext compileContext)
        {
            // ldloca.s indx - Load address of local variable with index indx, short form.
            var parameterDefinition = instruction.Operand as ParameterDefinition;
            var local = compileContext.Push(parameterDefinition);

            local.isAddress = true;
            return(true);
        }
Beispiel #12
0
        public static bool Blt_S(Instruction instruction, CompileContext compileContext)
        {
            // blt.s target - Branch to target if less than, short for
            var b = compileContext.Pop();
            var a = compileContext.Pop();

            compileContext.Push(typeof(int).GetTypeDefinition(), CompileContext.Format("int({0} < {1})", a, b));
            return(true);
        }
Beispiel #13
0
        public static bool Stfld(Instruction instruction, CompileContext compileContext)
        {
            // stfld field - Replace the value of field of the object obj with value
            var fieldDefinition = instruction.Operand as FieldDefinition;
            var value           = compileContext.Pop();
            var @this           = compileContext.Pop();

            compileContext.Assign(compileContext.Field(@this, fieldDefinition), value);
            return(true);
        }
Beispiel #14
0
 public static bool Ret(Instruction instruction, CompileContext compileContext)
 {
     // ret - Return from method, possibly with a value.
     if (instruction.Operand != null)
     {
         throw new NotImplementedException();
     }
     compileContext.WriteLine("return;");
     return(true);
 }
Beispiel #15
0
        public static bool Ldloca_S(Instruction instruction, CompileContext compileContext)
        {
            // ldloca.s indx - Load address of local variable with index indx, short form.
            var variableDefinition = instruction.Operand as VariableDefinition;
            var local = compileContext.Allocate(variableDefinition.VariableType, (uint)variableDefinition.Index);

            local.isAddress = true;
            compileContext.Push(local);
            return(true);
        }
Beispiel #16
0
        public override string Generate(CompileContext outer)
        {
            // Condition
            EvaluationStack unhandled = null;
            string          nothing   = outer.GenerateWithoutWriting(this.condition.first, this.condition.last, ref unhandled);

            Debug.Assert(string.IsNullOrEmpty(nothing));
            if (unhandled == null)
            {
                unhandled = outer.GetEvaluationStack();
            }
            var    condition = unhandled.Pop();
            string body      = outer.GenerateWithoutWriting(this.body.first, this.body.last, ref unhandled);

            Debug.Assert(unhandled == null);
            return(CompileContext.Format("while({0} != 0) {{\n{1}\n}}\n", condition, body));
        }
Beispiel #17
0
        public static bool Operator(Instruction instruction, CompileContext compileContext, string @operator, TypeReference type = null)
        {
            var    b = compileContext.Pop();
            var    a = compileContext.Pop();
            string pattern;

            if (type != null)
            {
                pattern = "{3}({0} {1} {2})";
            }
            else
            {
                pattern = "({0} {1} {2})";
            }
            compileContext.Push(new Value(type != null ? type : a.type, CompileContext.Format(pattern, a, @operator, b, type)));
            return(true);
        }
Beispiel #18
0
        public static bool Br(Instruction instruction, CompileContext compileContext)
        {
            var target = (instruction.Operand as Instruction);

            if (target.OpCode == OpCodes.Ret)
            {
                compileContext.WriteLine("return;");
            }
            else if (target.OpCode == OpCodes.Nop)
            {
            }
            else
            {
                throw new NotImplementedException();
            }
            return(true);
        }
Beispiel #19
0
        public static Instruction Step(this Instruction current, Instruction last, CompileContext compileContext)
        {
            if (StatementStructure.Recognize(new CodeBlock(current, last), out StatementStructure statementStructure) != StatementStructureType.Unknown)
            {
                string s = statementStructure.Generate(compileContext);
                compileContext.WriteLine(s);
                return(statementStructure.all.last.Next);
            }
            uint?localVariableIndex = compileContext.GetLocalVariableInstructionIndex(current);

            if (localVariableIndex != null && compileContext.IsUnnecessaryLocalVariableID(localVariableIndex.Value))
            {
                return(current.Next.Next);
            }
            var code = current.OpCode.Code;

            if (!methods.TryGetValue(code, out MethodInfo method))
            {
                throw new Exception(string.Format("Unsupported operate code: {0}", code));
            }
            method.Invoke(null, new object[] { current, compileContext });
            return(current.Next);
        }
Beispiel #20
0
 public static bool Ldarg_3(Instruction instruction, CompileContext compileContext)
 {
     // ldarg.3 - Load argument 3 onto the stack.
     Ldarg_N(instruction, compileContext, 3);
     return(true);
 }
Beispiel #21
0
 public static bool Ldarg_N(Instruction instruction, CompileContext compileContext, int n)
 {
     compileContext.Push(compileContext.parameters[n - 1]);
     return(true);
 }
Beispiel #22
0
 public static bool Pop(Instruction instruction, CompileContext compileContext)
 {
     compileContext.Pop();
     return(true);
 }
Beispiel #23
0
 public static bool Ldarg_0(Instruction instruction, CompileContext compileContext)
 {
     // ldarg.s num - Load argument numbered num onto the stack, short form.
     compileContext.Push(compileContext.declaringType, "this");
     return(true);
 }
Beispiel #24
0
 public static bool Dup(Instruction instruction, CompileContext compileContext)
 {
     compileContext.Push(compileContext.Peek());
     return(true);
 }
Beispiel #25
0
 public static bool Conv_R4(Instruction instruction, CompileContext compileContext)
 {
     compileContext.Push(typeof(float).GetTypeDefinition(), CompileContext.Format("float({0})", compileContext.Pop()));
     return(true);
 }
Beispiel #26
0
 public static bool Ceq(Instruction instruction, CompileContext compileContext)
 {
     // ceq - Push 1 (of type int32) if value1 > value2, else push 0.
     Operator(instruction, compileContext, "==", typeof(int).GetTypeDefinition());
     return(true);
 }
Beispiel #27
0
 public static bool Brfalse(Instruction instruction, CompileContext compileContext)
 {
     compileContext.Push(typeof(int).GetTypeDefinition(), CompileContext.Format("int({0} == 0)", compileContext.Pop()));
     return(true);
 }
Beispiel #28
0
 public static bool Ldarg_S(Instruction instruction, CompileContext compileContext)
 {
     // ldarg.s num - Load argument numbered num onto the stack, short form.
     compileContext.Push(instruction.Operand as ParameterDefinition);
     return(true);
 }
Beispiel #29
0
 public abstract string Generate(CompileContext outer);
Beispiel #30
0
 public static bool Brtrue(Instruction instruction, CompileContext compileContext)
 {
     // brtrue target - Branch to target if value is non-zero (true).
     compileContext.Push(typeof(int).GetTypeDefinition(), CompileContext.Format("int({0} == 1)", compileContext.Pop()));
     return(true);
 }