本文整理汇总了C#中IR类的典型用法代码示例。如果您正苦于以下问题:C# IR类的具体用法?C# IR怎么用?C# IR使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IR类属于命名空间,在下文中一共展示了IR类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AllocateRegisters
public List<int> AllocateRegisters(Stack<int> registers, IR.MethodEntry method)
{
List<int> used = new List<int>();
Dictionary<int, int> usageCount = new Dictionary<int, int>();
foreach (IR.InstructionElement i in method.Childnodes)
AllocateRegistersRecursive(i, registers, method, used, usageCount);
return used;
}
示例2: AllocateRegistersRecursive
public void AllocateRegistersRecursive(IR.InstructionElement current, Stack<int> registers, IR.MethodEntry method, List<int> used, Dictionary<int, int> usageCount)
{
//Does this instruction have an output?
if (current.Register != null)
{
//Have we already assigned a register?
if (current.Register.RegisterNumber < 0 && registers.Count > 0)
{
int regno = registers.Pop();
used.Add(regno);
usageCount.Add(regno, 0);
current.Register.RegisterNumber = regno;
}
if (current.Register.RegisterNumber > 0 && usageCount.ContainsKey(current.Register.RegisterNumber))
usageCount[current.Register.RegisterNumber]++;
//Assign the target register to the child node
if (current.Register.RegisterNumber > 0 && current.Childnodes.Length != 0 && current.Childnodes[0].Register != null)
current.Childnodes[0].Register = current.Register;
}
foreach (IR.InstructionElement i in current.Childnodes)
AllocateRegistersRecursive(i, registers, method, used, usageCount);
foreach(IR.InstructionElement i in current.Childnodes)
if (i.Register != null && usageCount.ContainsKey(i.Register.RegisterNumber))
{
usageCount[i.Register.RegisterNumber]--;
if (usageCount[i.Register.RegisterNumber] == 0)
{
usageCount.Remove(i.Register.RegisterNumber);
registers.Push(i.Register.RegisterNumber);
}
}
/*if (current.Register != null && current.Register.RegisterNumber > 0 && usageCount.ContainsKey(current.Register.RegisterNumber))
{
usageCount[current.Register.RegisterNumber]--;
if (usageCount[current.Register.RegisterNumber] == 0)
{
usageCount.Remove(current.Register.RegisterNumber);
registers.Push(current.Register.RegisterNumber);
}
}*/
}
示例3: Endfilter
/// <summary>
/// Implements the 'endfilter' IL instruction.
/// </summary>
private void Endfilter (IR.Instructions.Endfilter instruction)
{
IR.Operands.Register value = instruction.Use [0] as IR.Operands.Register;
if (value.IsRegisterSet)
this.assembly.MOV (R32.EAX, Assembly.GetRegister (value.Register));
else
this.assembly.MOV (R32.EAX, new DWordMemory (this.GetAddress (value)));
this.assembly.MOV (R32.ESP, this.GetExceptionHandlingSPSlot);
this.assembly.RET ();
}
示例4: SetInstruction
/// <summary>
/// Sets the instruction.
/// </summary>
/// <param name="instruction">The instruction.</param>
/// <param name="code">The code.</param>
/// <param name="block">The block.</param>
public void SetInstruction(IInstruction instruction, IR.ConditionCode code, BasicBlock block)
{
SetInstruction(instruction);
ConditionCode = code;
SetBranch(block);
}
示例5: AppendInstruction
/// <summary>
/// Inserts the instruction after.
/// </summary>
/// <param name="instruction">The instruction.</param>
/// <param name="code">The code.</param>
/// <param name="block">The block.</param>
public void AppendInstruction(IInstruction instruction, IR.ConditionCode code, BasicBlock block)
{
AppendInstruction();
SetInstruction(instruction, code, block);
}
示例6: Optimize
public void Optimize(IR.MethodEntry method, OptimizationLevel level)
{
throw new InvalidOperationException("The register allocator must be invoked via the IRegisterAllocator interface instead of the IOptimizer interface");
}
示例7: CreateBinaryOperationIC
public static void CreateBinaryOperationIC(ILGen.BaseILGenerator ilGen, IR.NodeType nodeType, int resultIndex, mdr.ValueTypes i0Type, mdr.ValueTypes i1Type, bool i0TypeCheck, bool i1TypeCheck)
{
ilGen.WriteComment("IC method for {0}({1}, {2}) written to {3}", nodeType, i0Type, i0Type, resultIndex);
var values = ilGen.DeclareLocal(CodeGen.Types.DValue.ArrayOf);
ilGen.Ldarg_CallFrame();
ilGen.Ldfld(CodeGen.Types.CallFrame.Values);
ilGen.Stloc(values);
///Try to make a better guess on the unknown types.
if (i0Type == mdr.ValueTypes.DValueRef)
{
if (i1Type != mdr.ValueTypes.DValueRef)
i0Type = i1Type;
else
i0Type = i1Type = mdr.ValueTypes.Int32; //Just a guess!
}
else if (i1Type == mdr.ValueTypes.DValueRef)
i1Type = i0Type;
var t0 = ilGen.DeclareLocal(CodeGen.Types.ClrSys.Int32);
//if (i0TypeCheck)
{
LoadValue(ilGen, values, resultIndex, mdr.ValueTypes.DValueRef);
ilGen.Call(CodeGen.Types.DValue.GetValueType);
ilGen.Stloc(t0);
}
var t1 = ilGen.DeclareLocal(CodeGen.Types.ClrSys.Int32);
//if (i0TypeCheck)
{
LoadValue(ilGen, values, resultIndex + 1, mdr.ValueTypes.DValueRef);
ilGen.Call(CodeGen.Types.DValue.GetValueType);
ilGen.Stloc(t1);
}
var guardFail = ilGen.DefineLabel();
var done = ilGen.DefineLabel();
ilGen.Ldloc(t0);
ilGen.Ldc_I4(8);
ilGen.Shl();
ilGen.Ldloc(t1);
ilGen.Or();
ilGen.Ldc_I4(((int)i0Type << 8) | (int)i1Type);
ilGen.Bne_Un(guardFail);
var operation = CodeGen.Types.Operations.Binary.Get(nodeType);
var mi = operation.Get(i0Type, i1Type);
var returnType = operation.ReturnType(i0Type, i1Type);
if (returnType == mdr.ValueTypes.DValueRef)
{
Debug.Assert(mi.GetParameters().Length == 3 && mi.GetParameters()[2].ParameterType == CodeGen.Types.TypeOf(mdr.ValueTypes.DValueRef), "Invalid situation, method {0} must get a third parameter of type 'ref DValue'", mi);
LoadValue(ilGen, values, resultIndex, i0Type);
LoadValue(ilGen, values, resultIndex + 1, i1Type);
LoadValue(ilGen, values, resultIndex, mdr.ValueTypes.DValueRef);
ilGen.Call(mi);
}
else
{
LoadValue(ilGen, values, resultIndex, mdr.ValueTypes.DValueRef);
LoadValue(ilGen, values, resultIndex, i0Type);
LoadValue(ilGen, values, resultIndex + 1, i1Type);
ilGen.Call(mi);
ilGen.Call(CodeGen.Types.DValue.Set.Get(returnType));
}
ilGen.Br(done);
ilGen.MarkLabel(guardFail);
ilGen.Ldarg_CallFrame();
ilGen.Ldarg_1();
ilGen.Ldc_I4((int)nodeType);
ilGen.Ldc_I4(resultIndex);
ilGen.Ldloc(t0);
ilGen.Ldloc(t1);
ilGen.Ldc_I4(i0TypeCheck);
ilGen.Ldc_I4(i1TypeCheck);
ilGen.Call(CodeGen.Types.Operations.ICMethods.RunAndUpdateBinaryOperationIC);
ilGen.Ret();
ilGen.MarkLabel(done);
}
示例8: Ldind
/// <summary>
/// Implements the ldind.* family of instructions
/// </summary>
private void Ldind (IR.Instructions.Ldind instruction)
{
IR.Operands.Register assignee = instruction.Def as IR.Operands.Register;
IR.Operands.Register register = instruction.Use [0] as IR.Operands.Register;
if (register.IsRegisterSet)
this.assembly.MOV (R32.EAX, Assembly.GetRegister (register.Register));
else
this.assembly.MOV (R32.EAX, new DWordMemory (this.GetAddress (register)));
switch (instruction.InternalType) {
case InternalType.I1:
this.assembly.MOVSX (R32.EAX, new ByteMemory (null, R32.EAX, null, 0));
if (assignee.IsRegisterSet)
this.assembly.MOV (Assembly.GetRegister (assignee.Register), R32.EAX);
else
this.assembly.MOV (new DWordMemory (this.GetAddress (assignee)), R32.EAX);
break;
case InternalType.U1:
this.assembly.MOVZX (R32.EAX, new ByteMemory (null, R32.EAX, null, 0));
if (assignee.IsRegisterSet)
this.assembly.MOV (Assembly.GetRegister (assignee.Register), R32.EAX);
else
this.assembly.MOV (new DWordMemory (this.GetAddress (assignee)), R32.EAX);
break;
case InternalType.I2:
this.assembly.MOVSX (R32.EAX, new WordMemory (null, R32.EAX, null, 0));
if (assignee.IsRegisterSet)
this.assembly.MOV (Assembly.GetRegister (assignee.Register), R32.EAX);
else
this.assembly.MOV (new DWordMemory (this.GetAddress (assignee)), R32.EAX);
break;
case InternalType.U2:
this.assembly.MOVZX (R32.EAX, new WordMemory (null, R32.EAX, null, 0));
if (assignee.IsRegisterSet)
this.assembly.MOV (Assembly.GetRegister (assignee.Register), R32.EAX);
else
this.assembly.MOV (new DWordMemory (this.GetAddress (assignee)), R32.EAX);
break;
case InternalType.O:
case InternalType.I:
case InternalType.I4:
case InternalType.U4:
case InternalType.SZArray:
case InternalType.Array:
this.assembly.MOV (R32.EAX, new DWordMemory (null, R32.EAX, null, 0));
if (assignee.IsRegisterSet)
this.assembly.MOV (Assembly.GetRegister (assignee.Register), R32.EAX);
else
this.assembly.MOV (new DWordMemory (this.GetAddress (assignee)), R32.EAX);
break;
case InternalType.I8:
case InternalType.U8:
Memory address = this.GetAddress (assignee);
address.DisplacementDelta = 4;
this.assembly.MOV (R32.EDX, R32.EAX);
this.assembly.ADD (R32.EDX, 4);
this.assembly.MOV (R32.EAX, new DWordMemory (null, R32.EAX, null, 0));
this.assembly.MOV (new DWordMemory (this.GetAddress (assignee)), R32.EAX);
this.assembly.MOV (R32.EAX, new DWordMemory (null, R32.EDX, null, 0));
this.assembly.MOV (new DWordMemory (address), R32.EAX);
break;
case InternalType.R4:
case InternalType.R8:
case InternalType.F:
case InternalType.ValueType:
case InternalType.M:
case InternalType.U:
case InternalType.TypedReference:
default:
throw new NotImplementedEngineException ("Ldind not supported for InternalType." + instruction.InternalType);
//.........这里部分代码省略.........
示例9: Ldc
/// <summary>
/// Implements the 'ldc.*' family of IL instructions.
/// </summary>
private void Ldc (IR.Instructions.Ldc instruction)
{
IR.Operands.Register assignee = instruction.Def as IR.Operands.Register;
switch (assignee.InternalType) {
case InternalType.I4:
uint intConstant = (uint) (instruction.Use [0] as IntConstant).Value;
if (assignee.IsRegisterSet)
this.assembly.MOV (Assembly.GetRegister (assignee.Register), intConstant);
else
this.assembly.MOV (new DWordMemory (this.GetAddress (assignee)), intConstant);
break;
case InternalType.I8:
ulong longConstant = (ulong) (instruction.Use [0] as LongConstant).Value;
uint hiValue = (uint) (longConstant >> 32);
uint loValue = (uint) (longConstant & 0xFFFFFFFF);
Memory address = this.GetAddress (assignee);
address.DisplacementDelta = 4;
this.assembly.MOV (new DWordMemory (address), hiValue);
this.assembly.MOV (new DWordMemory (this.GetAddress (assignee)), loValue);
break;
default:
throw new NotImplementedEngineException ("LDC of " + assignee.InternalType + " not supported yet");
}
}
示例10: Initialize
private void Initialize (IR.Operands.Identifier identifier)
{
int size = this.method.Engine.GetTypeSize (identifier.TypeFullName, 4) / 4;
if (size == 1) {
this.assembly.XOR (R32.EAX, R32.EAX);
if (identifier.IsRegisterSet)
this.assembly.MOV (Assembly.GetRegister (identifier.Register), R32.EAX);
else
this.assembly.MOV (new DWordMemory (this.GetAddress (identifier)), R32.EAX);
} else {
this.assembly.PUSH (R32.ECX);
this.assembly.PUSH (R32.EDI);
this.assembly.XOR (R32.EAX, R32.EAX);
if (identifier.IsRegisterSet)
this.assembly.MOV (R32.EDI, Assembly.GetRegister (identifier.Register));
else
this.assembly.LEA (R32.EDI, new DWordMemory (this.GetAddress (identifier)));
this.assembly.MOV (R32.ECX, (uint) size);
this.assembly.CLD ();
this.assembly.REP ();
this.assembly.STOSD ();
this.assembly.POP (R32.EDI);
this.assembly.POP (R32.ECX);
}
}
示例11: GetAssemblyStubMemoryAddress
/// <summary>
/// Creates an instance of the Memory class which has been used in the kernel to interact with the Asm stubs.
/// In the example "Asm.MOV (R32.EAX, new Memory (...))", the AOT emulates the instantiation of the Memory
/// parameter at compile-time, instead of literally translating the 'newobj' and constructor calls into the
/// encoded result.
/// </summary>
private Memory GetAssemblyStubMemoryAddress (IR.Instructions.Newobj call)
{
if (call.Use.Length == 1) {
string parameter = (Operand.GetNonRegister (call.Use [0], typeof (StringConstant)) as SharpOS.AOT.IR.Operands.StringConstant).Value;
if (call.Method.Class.TypeFullName.EndsWith (".Memory")) {
return new Memory (parameter);
} else if (call.Method.Class.TypeFullName.EndsWith (".ByteMemory")) {
return new ByteMemory (parameter);
} else if (call.Method.Class.TypeFullName.EndsWith (".WordMemory")) {
return new WordMemory (parameter);
} else if (call.Method.Class.TypeFullName.EndsWith (".DWordMemory")) {
return new DWordMemory (parameter);
} else if (call.Method.Class.TypeFullName.EndsWith (".QWordMemory")) {
return new QWordMemory (parameter);
} else if (call.Method.Class.TypeFullName.EndsWith (".TWordMemory")) {
return new TWordMemory (parameter);
} else {
throw new EngineException ("'" + call.Method.Class.TypeFullName + "' is not supported.");
}
} else if (call.Use.Length == 2) {
SegType segment = Seg.GetByID ((Operand.GetNonRegister (call.Use [0], typeof (FieldOperand)) as FieldOperand).ShortFieldTypeName);
string label = (Operand.GetNonRegister (call.Use [1], typeof (StringConstant)) as SharpOS.AOT.IR.Operands.StringConstant).Value;
if (call.Method.Class.TypeFullName.EndsWith (".Memory")) {
return new Memory (segment, label);
} else if (call.Method.Class.TypeFullName.EndsWith (".ByteMemory")) {
return new ByteMemory (segment, label);
} else if (call.Method.Class.TypeFullName.EndsWith (".WordMemory")) {
return new WordMemory (segment, label);
} else if (call.Method.Class.TypeFullName.EndsWith (".DWordMemory")) {
return new DWordMemory (segment, label);
} else if (call.Method.Class.TypeFullName.EndsWith (".QWordMemory")) {
return new QWordMemory (segment, label);
} else if (call.Method.Class.TypeFullName.EndsWith (".TWordMemory")) {
return new TWordMemory (segment, label);
} else {
throw new EngineException ("'" + call.Method.Class.TypeFullName + "' is not supported.");
}
} else if (call.Use.Length == 3) {
SegType segment = Seg.GetByID (Operand.GetNonRegister (call.Use [0], typeof (FieldOperand)));
R16Type _base = R16.GetByID (Operand.GetNonRegister (call.Use [1], typeof (FieldOperand)));
R16Type index = R16.GetByID (Operand.GetNonRegister (call.Use [2], typeof (FieldOperand)));
if (call.Method.Class.TypeFullName.EndsWith (".Memory")) {
return new Memory (segment, _base, index);
} else if (call.Method.Class.TypeFullName.EndsWith (".ByteMemory")) {
return new ByteMemory (segment, _base, index);
} else if (call.Method.Class.TypeFullName.EndsWith (".WordMemory")) {
return new WordMemory (segment, _base, index);
} else if (call.Method.Class.TypeFullName.EndsWith (".DWordMemory")) {
return new DWordMemory (segment, _base, index);
} else if (call.Method.Class.TypeFullName.EndsWith (".QWordMemory")) {
return new QWordMemory (segment, _base, index);
} else if (call.Method.Class.TypeFullName.EndsWith (".TWordMemory")) {
return new TWordMemory (segment, _base, index);
} else {
throw new EngineException ("'" + call.Method.Class.TypeFullName + "' is not supported.");
}
} else if (call.Use.Length == 4) {
if (call.Method.Arguments [1].TypeFullName.IndexOf ("16") != -1) {
SegType segment = Seg.GetByID (Operand.GetNonRegister (call.Use [0], typeof (FieldOperand)));
R16Type _base = R16.GetByID (Operand.GetNonRegister (call.Use [1], typeof (FieldOperand)));
R16Type index = R16.GetByID (Operand.GetNonRegister (call.Use [2], typeof (FieldOperand)));
Int16 displacement = System.Convert.ToInt16 ((Operand.GetNonRegister (call.Use [3], typeof (IntConstant)) as IntConstant).Value);
if (call.Method.Class.TypeFullName.EndsWith (".Memory")) {
return new Memory (segment, _base, index, displacement);
} else if (call.Method.Class.TypeFullName.EndsWith (".ByteMemory")) {
return new ByteMemory (segment, _base, index, displacement);
} else if (call.Method.Class.TypeFullName.EndsWith (".WordMemory")) {
//.........这里部分代码省略.........
示例12: Throw
/// <summary>
/// Implements the 'throw' IL instruction, which throws an exception. The bulk of the
/// work performed by this instruction is delegated to the kernel's runtime.
/// </summary>
private void Throw (IR.Instructions.Throw instruction)
{
if (instruction.Use != null && instruction.Use.Length == 1) {
IR.Operands.Register value = instruction.Use [0] as IR.Operands.Register;
if (value.IsRegisterSet)
this.assembly.MOV (R32.EAX, Assembly.GetRegister (value.Register));
else
this.assembly.MOV (R32.EAX, new DWordMemory (this.GetAddress (value)));
} else
// This is for when rethrowing an exception
this.assembly.MOV (R32.EAX, this.GetExceptionHandlingExceptionObjectSlot);
this.assembly.PUSH (R32.EAX);
this.assembly.CALL (this.assembly.Engine.Throw.AssemblyLabel);
this.assembly.ADD (R32.ESP, 4);
}
示例13: Break
/// <summary>
/// Implements the 'break' IL instruction.
/// </summary>
private void Break (IR.Instructions.Break instruction)
{
// Does nothing, perhaps emit a label?
}
示例14: GetOppositeConditionCode
/// <summary>
/// Gets the opposite condition code.
/// </summary>
/// <param name="conditionCode">The condition code.</param>
/// <returns></returns>
protected IR.ConditionCode GetOppositeConditionCode(IR.ConditionCode conditionCode)
{
switch (conditionCode) {
case IR.ConditionCode.Equal: return IR.ConditionCode.NotEqual;
case IR.ConditionCode.NotEqual: return IR.ConditionCode.Equal;
case IR.ConditionCode.GreaterOrEqual: return IR.ConditionCode.LessThan;
case IR.ConditionCode.GreaterThan: return IR.ConditionCode.LessOrEqual;
case IR.ConditionCode.LessOrEqual: return IR.ConditionCode.UnsignedLessOrEqual;
case IR.ConditionCode.LessThan: return IR.ConditionCode.GreaterOrEqual;
case IR.ConditionCode.UnsignedGreaterOrEqual: return IR.ConditionCode.UnsignedLessThan;
case IR.ConditionCode.UnsignedGreaterThan: return IR.ConditionCode.UnsignedLessOrEqual;
case IR.ConditionCode.UnsignedLessOrEqual: return IR.ConditionCode.UnsignedGreaterThan;
case IR.ConditionCode.UnsignedLessThan: return IR.ConditionCode.UnsignedGreaterOrEqual;
case IR.ConditionCode.Signed: return IR.ConditionCode.NotSigned;
case IR.ConditionCode.NotSigned: return IR.ConditionCode.Signed;
default: throw new NotSupportedException();
}
}
示例15: CreateUnaryOperationIC
public static void CreateUnaryOperationIC(ILGen.BaseILGenerator ilGen, IR.NodeType nodeType, int resultIndex, mdr.ValueTypes i0Type, bool i0TypeCheck)
{
ilGen.WriteComment("IC method for {0}({1}, {2}) written to {3}", nodeType, i0Type, i0Type, resultIndex);
var values = ilGen.DeclareLocal(CodeGen.Types.DValue.ArrayOf);
ilGen.Ldarg_CallFrame();
ilGen.Ldfld(CodeGen.Types.CallFrame.Values);
ilGen.Stloc(values);
var t0 = ilGen.DeclareLocal(CodeGen.Types.ClrSys.Int32);
//if (i0TypeCheck)
{
LoadValue(ilGen, values, resultIndex, mdr.ValueTypes.DValueRef);
ilGen.Call(CodeGen.Types.DValue.GetValueType);
ilGen.Stloc(t0);
}
var guardFail = ilGen.DefineLabel();
var done = ilGen.DefineLabel();
var operation = CodeGen.Types.Operations.Unary.Get(nodeType);
if (i0Type == mdr.ValueTypes.DValueRef) //Just a better guess!
i0Type = operation.ReturnType(i0Type); //we will need to repeat the lookup again
ilGen.Ldloc(t0);
ilGen.Ldc_I4((int)i0Type);
ilGen.Bne_Un(guardFail);
var mi = operation.Get(i0Type);
var returnType = operation.ReturnType(i0Type);
if (returnType == mdr.ValueTypes.DValueRef)
{
Debug.Assert(mi.GetParameters().Length == 2 && mi.GetParameters()[1].ParameterType == CodeGen.Types.TypeOf(mdr.ValueTypes.DValueRef), "Invalid situation, method {0} must get a second parameter of type 'ref DValue'", mi);
LoadValue(ilGen, values, resultIndex, i0Type);
LoadValue(ilGen, values, resultIndex, mdr.ValueTypes.DValueRef);
ilGen.Call(mi);
}
else
{
LoadValue(ilGen, values, resultIndex, mdr.ValueTypes.DValueRef);
LoadValue(ilGen, values, resultIndex, i0Type);
ilGen.Call(mi);
ilGen.Call(CodeGen.Types.DValue.Set.Get(returnType));
}
ilGen.Br(done);
ilGen.MarkLabel(guardFail);
ilGen.Ldarg_CallFrame();
ilGen.Ldarg_1();
ilGen.Ldc_I4((int)nodeType);
ilGen.Ldc_I4(resultIndex);
ilGen.Ldloc(t0);
ilGen.Ldc_I4(i0TypeCheck);
ilGen.Call(CodeGen.Types.Operations.ICMethods.RunAndUpdateUnaryOperationIC);
ilGen.Ret();
ilGen.MarkLabel(done);
}