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); }
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); }
public static bool Stind_R4(Instruction instruction, CompileContext compileContext) { var value = compileContext.Pop(); var target = compileContext.Pop(); compileContext.Assign(target, value); return(true); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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)); }
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); }
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); }
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); }
public static bool Ldarg_3(Instruction instruction, CompileContext compileContext) { // ldarg.3 - Load argument 3 onto the stack. Ldarg_N(instruction, compileContext, 3); return(true); }
public static bool Ldarg_N(Instruction instruction, CompileContext compileContext, int n) { compileContext.Push(compileContext.parameters[n - 1]); return(true); }
public static bool Pop(Instruction instruction, CompileContext compileContext) { compileContext.Pop(); return(true); }
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); }
public static bool Dup(Instruction instruction, CompileContext compileContext) { compileContext.Push(compileContext.Peek()); return(true); }
public static bool Conv_R4(Instruction instruction, CompileContext compileContext) { compileContext.Push(typeof(float).GetTypeDefinition(), CompileContext.Format("float({0})", compileContext.Pop())); return(true); }
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); }
public static bool Brfalse(Instruction instruction, CompileContext compileContext) { compileContext.Push(typeof(int).GetTypeDefinition(), CompileContext.Format("int({0} == 0)", compileContext.Pop())); return(true); }
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); }
public abstract string Generate(CompileContext outer);
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); }