public FluentEmitter EndIf()
{
if(IfBlocks.Count == 0)
throw new NotSupportedException("no if-block to close");
var block = IfBlocks.Pop();
if (block.StartInstruction?.Previous.OpCode == OpCodes.Ret)
{
Body.Instructions.Remove(block.StartInstruction);
return this;
}
var firstinstructionafterblock = LastEmittedInstruction.Next;
if(firstinstructionafterblock == null)
{
Nop();
firstinstructionafterblock = LastEmittedInstruction;
}
if(block.IsDoublePop)
Body.GetILProcessor().Remove(block.StartInstruction?.Next);
var newstartinstruction = Instruction.Create(block.OpCode, firstinstructionafterblock);
Body.GetILProcessor().Replace(block.StartInstruction, newstartinstruction);
// remove Nops
Func<FluentEmitter, bool> postemitaction = (body) =>
{
if (firstinstructionafterblock.Next == null)
return false;
foreach (var instruction in body.Body.Instructions.Where(i => i.Operand == firstinstructionafterblock))
instruction.Operand = firstinstructionafterblock.Next;
Body.GetILProcessor().Remove(firstinstructionafterblock);
return true;
};
PostEmitActions.Enqueue(postemitaction);
return this;
}
}