Exemple #1
0
 protected override void Initialize()
 {
     exceptionType = TypeSystem.GetTypeByName("System", "Exception");
 }
Exemple #2
0
 private MosaType GetPlatformInternalRuntimeType()
 {
     return(TypeSystem.GetTypeByName("Mosa.Runtime." + Architecture.PlatformName, "Internal"));
 }
Exemple #3
0
 private MosaType GeInternalRuntimeType()
 {
     return(TypeSystem.GetTypeByName("Mosa.Runtime", "Internal"));
 }
Exemple #4
0
        protected override void Run()
        {
            exceptionVirtualRegisters     = new Dictionary <BasicBlock, Operand>();
            finallyReturnVirtualRegisters = new Dictionary <BasicBlock, Operand>();

            exceptionType = TypeSystem.GetTypeByName("System", "Exception");

            var exceptionRegister = Operand.CreateCPURegister(exceptionType, Architecture.ExceptionRegister);

            var finallyReturnBlockRegister = Operand.CreateCPURegister(TypeSystem.BuiltIn.I4, Architecture.FinallyReturnBlockRegister);

            var nullOperand = Operand.GetNull(TypeSystem);

            for (int i = 0; i < BasicBlocks.Count; i++)
            {
                var block = BasicBlocks[i];

                for (var node = block.First; !node.IsBlockEndInstruction; node = node.Next)
                {
                    if (node.IsEmpty)
                    {
                        continue;
                    }

                    if (node.Instruction == IRInstruction.Throw)
                    {
                        var method = PlatformInternalRuntimeType.FindMethodByName("ExceptionHandler");
                        var ctx    = new Context(node);

                        ctx.SetInstruction(IRInstruction.Move, exceptionRegister, node.Operand1);

                        //ctx.AppendInstruction(IRInstruction.KillAllExcept, null, exceptionRegister);
                        ctx.AppendInstruction(IRInstruction.Call, null, Operand.CreateSymbolFromMethod(TypeSystem, method));
                        ctx.InvokeMethod = method;
                    }
                    else if (node.Instruction == IRInstruction.CallFinally)
                    {
                        var target        = node.BranchTargets[0];
                        var finallyReturn = node.BranchTargets[1];
                        var ctx           = new Context(node);

                        ctx.SetInstruction(IRInstruction.KillAll);
                        ctx.AppendInstruction(IRInstruction.Move, exceptionRegister, nullOperand);
                        ctx.AppendInstruction(IRInstruction.Move, finallyReturnBlockRegister, Operand.CreateConstant(TypeSystem, finallyReturn.Label));
                        ctx.AppendInstruction(IRInstruction.Jmp, target);
                    }
                    else if (node.Instruction == IRInstruction.FinallyStart)
                    {
                        // Remove from header blocks
                        BasicBlocks.RemoveHeaderBlock(node.Block);

                        var exceptionVirtualRegister          = node.Result;
                        var finallyReturnBlockVirtualRegister = node.Result2;
                        var ctx = new Context(node);

                        exceptionVirtualRegisters.Add(node.Block, exceptionVirtualRegister);
                        finallyReturnVirtualRegisters.Add(node.Block, finallyReturnBlockRegister);

                        ctx.SetInstruction(IRInstruction.KillAll);
                        ctx.AppendInstruction(IRInstruction.Gen, exceptionRegister);
                        ctx.AppendInstruction(IRInstruction.Gen, finallyReturnBlockRegister);

                        ctx.AppendInstruction(IRInstruction.Move, exceptionVirtualRegister, exceptionRegister);
                        ctx.AppendInstruction(IRInstruction.Move, finallyReturnBlockVirtualRegister, finallyReturnBlockRegister);
                    }
                    else if (node.Instruction == IRInstruction.FinallyEnd)
                    {
                        var header      = FindImmediateExceptionHandler(node);
                        var headerBlock = BasicBlocks.GetByLabel(header.HandlerStart);

                        var exceptionVirtualRegister          = exceptionVirtualRegisters[headerBlock];
                        var finallyReturnBlockVirtualRegister = finallyReturnVirtualRegisters[headerBlock];

                        var newBlocks = CreateNewBlockContexts(1);
                        var ctx       = new Context(node);
                        var nextBlock = Split(ctx);

                        ctx.SetInstruction(IRInstruction.IntegerCompareBranch, ConditionCode.NotEqual, null, exceptionVirtualRegister, nullOperand, newBlocks[0].Block);
                        ctx.AppendInstruction(IRInstruction.Jmp, nextBlock.Block);

                        var method = PlatformInternalRuntimeType.FindMethodByName("ExceptionHandler");

                        newBlocks[0].AppendInstruction(IRInstruction.Move, exceptionRegister, exceptionVirtualRegister);
                        newBlocks[0].AppendInstruction(IRInstruction.Call, null, Operand.CreateSymbolFromMethod(TypeSystem, method));
                        newBlocks[0].InvokeMethod = method;
                    }
                    else if (node.Instruction == IRInstruction.FinallyReturn)
                    {
                        var targets = node.BranchTargets;

                        var header      = FindImmediateExceptionHandler(node);
                        var headerBlock = BasicBlocks.GetByLabel(header.HandlerStart);

                        var finallyReturnBlockVirtualRegister = finallyReturnVirtualRegisters[headerBlock];

                        Debug.Assert(targets.Count != 0);

                        if (targets.Count == 1)
                        {
                            node.SetInstruction(IRInstruction.Jmp, targets[0]);
                        }
                        else
                        {
                            var newBlocks = CreateNewBlockContexts(targets.Count - 1);
                            var ctx       = new Context(node);

                            ctx.SetInstruction(IRInstruction.IntegerCompareBranch, ConditionCode.Equal, null, finallyReturnBlockVirtualRegister, Operand.CreateConstant(TypeSystem, targets[0].Label), targets[0]);
                            ctx.AppendInstruction(IRInstruction.Jmp, newBlocks[0].Block);

                            for (int b = 1; b < targets.Count - 2; b++)
                            {
                                newBlocks[b - 1].AppendInstruction(IRInstruction.IntegerCompareBranch, ConditionCode.Equal, null, finallyReturnBlockVirtualRegister, Operand.CreateConstant(TypeSystem, targets[b].Label), targets[b]);
                                newBlocks[b - 1].AppendInstruction(IRInstruction.Jmp, newBlocks[b + 1].Block);
                            }

                            newBlocks[targets.Count - 2].AppendInstruction(IRInstruction.Jmp, targets[targets.Count - 1]);
                        }
                    }
                    else if (node.Instruction == IRInstruction.ExceptionStart)
                    {
                        var exceptionVirtualRegister = node.Result;
                        var ctx = new Context(node);

                        ctx.SetInstruction(IRInstruction.KillAll);
                        ctx.AppendInstruction(IRInstruction.Gen, exceptionRegister);
                        ctx.AppendInstruction(IRInstruction.Move, exceptionVirtualRegister, exceptionRegister);
                    }
                    else if (node.Instruction == IRInstruction.ExceptionEnd)
                    {
                        node.SetInstruction(IRInstruction.Jmp, node.BranchTargets[0]);
                    }
                    else if (node.Instruction == IRInstruction.Flow)
                    {
                        node.Empty();
                    }
                }
            }
        }
Exemple #5
0
 public BuiltInTypes(TypeSystem typeSystem, MosaModule corlib)
 {
     Void = typeSystem.GetTypeByName(corlib, "System", "Void");
     Boolean = typeSystem.GetTypeByName(corlib, "System", "Boolean");
     Char = typeSystem.GetTypeByName(corlib, "System", "Char");
     I1 = typeSystem.GetTypeByName(corlib, "System", "SByte");
     U1 = typeSystem.GetTypeByName(corlib, "System", "Byte");
     I2 = typeSystem.GetTypeByName(corlib, "System", "Int16");
     U2 = typeSystem.GetTypeByName(corlib, "System", "UInt16");
     I4 = typeSystem.GetTypeByName(corlib, "System", "Int32");
     U4 = typeSystem.GetTypeByName(corlib, "System", "UInt32");
     I8 = typeSystem.GetTypeByName(corlib, "System", "Int64");
     U8 = typeSystem.GetTypeByName(corlib, "System", "UInt64");
     R4 = typeSystem.GetTypeByName(corlib, "System", "Single");
     R8 = typeSystem.GetTypeByName(corlib, "System", "Double");
     String = typeSystem.GetTypeByName(corlib, "System", "String");
     Object = typeSystem.GetTypeByName(corlib, "System", "Object");
     I = typeSystem.GetTypeByName(corlib, "System", "IntPtr");
     U = typeSystem.GetTypeByName(corlib, "System", "UIntPtr");
     TypedRef = typeSystem.GetTypeByName(corlib, "System", "TypedReference");
     Pointer = Void.ToUnmanagedPointer();
 }
 public BuiltInTypes(TypeSystem typeSystem, MosaModule corlib)
 {
     Void     = typeSystem.GetTypeByName(corlib, "System", "Void");
     Boolean  = typeSystem.GetTypeByName(corlib, "System", "Boolean");
     Char     = typeSystem.GetTypeByName(corlib, "System", "Char");
     I1       = typeSystem.GetTypeByName(corlib, "System", "SByte");
     U1       = typeSystem.GetTypeByName(corlib, "System", "Byte");
     I2       = typeSystem.GetTypeByName(corlib, "System", "Int16");
     U2       = typeSystem.GetTypeByName(corlib, "System", "UInt16");
     I4       = typeSystem.GetTypeByName(corlib, "System", "Int32");
     U4       = typeSystem.GetTypeByName(corlib, "System", "UInt32");
     I8       = typeSystem.GetTypeByName(corlib, "System", "Int64");
     U8       = typeSystem.GetTypeByName(corlib, "System", "UInt64");
     R4       = typeSystem.GetTypeByName(corlib, "System", "Single");
     R8       = typeSystem.GetTypeByName(corlib, "System", "Double");
     String   = typeSystem.GetTypeByName(corlib, "System", "String");
     Object   = typeSystem.GetTypeByName(corlib, "System", "Object");
     I        = typeSystem.GetTypeByName(corlib, "System", "IntPtr");
     U        = typeSystem.GetTypeByName(corlib, "System", "UIntPtr");
     TypedRef = typeSystem.GetTypeByName(corlib, "System", "TypedReference");
     Pointer  = Void.ToUnmanagedPointer();
 }
Exemple #7
0
        protected override void Run()
        {
            foreach (var type in TypeSystem.AllTypes)
            {
                string plugTypeTarget = null;

                var typeAttribute = type.FindCustomAttribute(PlugTypeAttribute);

                if (typeAttribute != null)
                {
                    plugTypeTarget = (string)typeAttribute.Arguments[0].Value;
                }

                foreach (var method in type.Methods)
                {
                    if (!method.IsStatic)
                    {
                        continue;
                    }

                    string plugMethodTarget = null;

                    var methodAttribute = method.FindCustomAttribute(PlugMethodAttribute);

                    if (methodAttribute != null)
                    {
                        plugMethodTarget = (string)methodAttribute.Arguments[0].Value;
                    }

                    if (plugTypeTarget != null || plugMethodTarget != null)
                    {
                        string targetAssemblyName;
                        string targetFullTypeName;
                        string targetMethodName;

                        if (plugMethodTarget != null)
                        {
                            targetAssemblyName = ParseAssembly(plugMethodTarget);
                            targetFullTypeName = ParseFullTypeName(plugMethodTarget);
                            targetMethodName   = ParseMethod(plugMethodTarget);
                        }
                        else
                        {
                            targetAssemblyName = ParseAssembly(plugTypeTarget);
                            targetFullTypeName = RemoveModule(plugTypeTarget);
                            targetMethodName   = method.Name;
                        }

                        string targetNameSpace = ParseNameSpace(targetFullTypeName);
                        string targetTypeName  = ParseType(targetFullTypeName);

                        MosaType targetType;

                        if (targetAssemblyName != null)
                        {
                            targetType = TypeSystem.GetTypeByName(TypeSystem.GetModuleByAssembly(targetAssemblyName), targetNameSpace, targetTypeName);
                        }
                        else
                        {
                            targetType = TypeSystem.GetTypeByName(targetNameSpace, targetTypeName);
                        }

                        if (targetType == null)
                        {
                            NewCompilerTraceEvent(CompilerEvent.Warning,
                                                  String.Format("Plug target type {0} not found. Ignoring plug.",
                                                                targetAssemblyName != null ? (targetFullTypeName + "(in " + targetAssemblyName + ")") : targetFullTypeName));
                            continue;
                        }

                        MosaMethod targetMethod = null;

                        foreach (var targetMethodCandidate in targetType.Methods)
                        {
                            if (targetMethodCandidate.Name == targetMethodName)
                            {
                                if (targetMethodCandidate.IsStatic)
                                {
                                    if (targetMethodCandidate.Equals(method))
                                    {
                                        targetMethod = targetMethodCandidate;
                                        break;
                                    }
                                }
                                else
                                {
                                    if (MatchesWithStaticThis(targetMethodCandidate, method))
                                    {
                                        targetMethod = targetMethodCandidate;
                                        break;
                                    }
                                }
                            }
                        }

                        if (targetMethod != null)
                        {
                            Patch(targetMethod, method);
                        }
                        else
                        {
                            NewCompilerTraceEvent(CompilerEvent.Warning,
                                                  String.Format("No matching plug target method {0} found in type {1}. Ignoring plug.",
                                                                targetMethodName, targetType.ToString()));
                        }
                    }
                }
            }
        }
Exemple #8
0
 protected override void Initialize()
 {
     objectType = TypeSystem.GetTypeByName("System", "Object");
 }