예제 #1
0
 internal IfStatement(ILProcessor processor, OperandBase operand)
 {
     _processor       = processor;
     StartInstruction = operand.EmitLoad(_processor).First();
     ElseInstruction  = Instruction.Create(OpCodes.Nop);
     EndInstruction   = Instruction.Create(OpCodes.Nop);
 }
예제 #2
0
 private IfCondition EmitCondition(OperandBase operand, OpCode conditionCode)
 {
     operand.EmitLoad(_processor);
     _processor.Emit(conditionCode);
     _processor.Emit(OpCodes.Brfalse, ElseInstruction);
     return(new IfCondition(this, _processor));
 }
        internal CallStatement(ILProcessor processor, OperandBase obj, MethodReference method, params OperandBase[] parameters)
        {
            var resolved = method.Resolve();

            if (resolved?.IsStatic == true)
            {
                throw new ArgumentException($"Method is static", nameof(method));
            }
            _processor = processor;
            if (obj.Type.IsValueType)
            {
                obj.EmitLoadAddr(processor);
            }
            else
            {
                obj.EmitLoad(processor);
            }

            foreach (var p in parameters)
            {
                p.EmitLoad(processor);
            }
            if (obj.Type.IsValueType)
            {
                processor.Emit(OpCodes.Call, method);
            }
            else
            {
                processor.Emit(OpCodes.Callvirt, method);
            }
        }
        internal SwitchStatement(ILProcessor processor, OperandBase operand)
        {
            if (processor.Body.Method.Module == null)
            {
                throw new MethodNotRegisteredException(
                          $"Method {processor.Body.Method} must be added to class and registered in module");
            }
            _processor = processor;
            CopyValue(operand);

            if (!operand.Type.IsPrimitive)    // TODO: better type checking
            {
                _equalityMethod = processor.Body.Method.Module.ImportReference(operand.Type.Resolve().Methods.First(method => method.Name == "op_Equality"));
            }

            _currentCondition      = Instruction.Create(OpCodes.Nop);
            _currentImplementation = Instruction.Create(OpCodes.Nop);
            _switchEnd             = Instruction.Create(OpCodes.Nop);

            _valueCopy.EmitLoad(_processor);
            _processor.Emit(OpCodes.Brfalse, _switchEnd);

            _processor.Append(_currentCondition);
            _processor.Emit(OpCodes.Br, _switchEnd);
            _processor.Append(_currentImplementation);
            _processor.Append(_switchEnd);
        }
 private Instruction CreateEqualityOperator(OperandBase operand)
 {
     if (_equalityMethod != null)
     {
         return(Instruction.Create(OpCodes.Call, _equalityMethod));
     }
     return(Instruction.Create(OpCodes.Ceq));
 }
        private IEnumerable <Instruction> EmitCondition(OperandBase operand, Instruction implStart)
        {
            List <Instruction> instr = new List <Instruction>();

            instr.AddRange(_valueCopy.CreateLoad());
            instr.AddRange(operand.CreateLoad());
            instr.Add(CreateEqualityOperator(operand));
            instr.Add(Instruction.Create(OpCodes.Brtrue, implStart));
            return(instr);
        }
        private void CopyValue(OperandBase operand)
        {
            var copy = new VariableDefinition(operand.Type);

            _processor.Body.Variables.Add(copy);
            _valueCopy = copy;

            operand.EmitLoad(_processor);
            _valueCopy.EmitStore(_processor);
        }
예제 #8
0
 internal FieldOperand(OperandBase container, FieldDefinition field)
 {
     if (field.IsStatic)
     {
         throw new ArgumentException($"Field {field} is static", nameof(field));
     }
     if (container.Type != field.DeclaringType)
     {
         throw new ArgumentException($"Field {field} is not declared in {container.Type}", nameof(field));
     }
     Container = container;
     Reference = field;
 }
        private IEnumerable <Instruction> CreateConditionCode(OperandBase to, int inc)
        {
            List <Instruction> code = new List <Instruction>();
            var iteratorLoad        = Iterator.CreateLoad();

            _increaseInstr = iteratorLoad.First();
            code.AddRange(iteratorLoad);
            code.AddRange(new ValueOperand(inc).CreateLoad());
            code.Add(Instruction.Create(OpCodes.Add));
            code.AddRange(Iterator.CreateStore());

            iteratorLoad    = Iterator.CreateLoad();
            _conditionStart = iteratorLoad.First();
            code.AddRange(iteratorLoad);
            code.AddRange(to.CreateLoad());
            code.Add(Instruction.Create(OpCodes.Clt));
            code.Add(Instruction.Create(OpCodes.Brtrue, _loopStart));
            return(code);
        }
        internal ForStatement(ILProcessor processor, int from, OperandBase to, int inc)
        {
            if (processor.Body.Method.Module == null)
            {
                throw new MethodNotRegisteredException(
                          $"Method {processor.Body.Method} must be added to class and registered in module");
            }
            _processor = processor;
            var iterator = processor.AddVariable(processor.Body.Method.Module.TypeSystem.Int32);

            Iterator = iterator;
            EmitIteratorInitialization(from);

            _loopStart    = Instruction.Create(OpCodes.Nop);
            ConditionCode = CreateConditionCode(to, inc);
            _processor.Emit(OpCodes.Br, _conditionStart);
            _processor.Append(_loopStart);
            _loopEnd = Instruction.Create(OpCodes.Nop);
        }
예제 #11
0
 public IfCondition Lt(OperandBase operand) => EmitCondition(operand, OpCodes.Clt);
예제 #12
0
 public IfCondition Eq(OperandBase operand) => EmitCondition(operand, OpCodes.Ceq);
 public void Store(OperandBase operand)
 {
     operand.EmitStore(_processor);
 }
예제 #14
0
 public static ForStatement For(this ILProcessor processor, int from, OperandBase to, int inc)
 {
     return(new ForStatement(processor, from, to, inc));
 }
 public static IfStatement If(this ILProcessor processor, OperandBase op)
 {
     return(new IfStatement(processor, op));
 }
예제 #16
0
 public static SwitchStatement Switch(this ILProcessor processor, OperandBase operand)
 {
     return(new SwitchStatement(processor, operand));
 }
 public static CallStatement CallMethod(this ILProcessor processor, OperandBase obj, MethodReference method, params OperandBase[] parameters)
 {
     return(new CallStatement(processor, obj, method, parameters));
 }