Ejemplo n.º 1
0
 private IfCondition EmitCondition(OperandBase operand, OpCode conditionCode)
 {
     operand.EmitLoad(_processor);
     _processor.Emit(conditionCode);
     _processor.Emit(OpCodes.Brfalse, ElseInstruction);
     return(new IfCondition(this, _processor));
 }
Ejemplo n.º 2
0
 internal IfStatement(ILProcessor processor, OperandBase operand)
 {
     _processor       = processor;
     StartInstruction = operand.EmitLoad(_processor).First();
     ElseInstruction  = Instruction.Create(OpCodes.Nop);
     EndInstruction   = Instruction.Create(OpCodes.Nop);
 }
        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 void CopyValue(OperandBase operand)
        {
            var copy = new VariableDefinition(operand.Type);

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

            operand.EmitLoad(_processor);
            _valueCopy.EmitStore(_processor);
        }