Exemplo n.º 1
0
 public CfgNodeCompiler(CfgNode cfgNode)
 {
     m_cppBuilder = cfgNode.CfgBuilder.CppBuilder;
     m_commonTypeLookup = new CommonTypeLookup(m_cppBuilder);
     m_cfgNode = cfgNode;
     m_ehRegion = cfgNode.CfgBuilder.Region;
     m_escapePaths = new SortedSet<uint>();
     m_temporaries = cfgNode.CfgBuilder.Temporaries;
 }
Exemplo n.º 2
0
        public ExceptionHandlingCluster(ClusterTypeEnum clusterType, uint checkInstr, ExceptionHandlingRegion tryRegion, ExceptionHandlingRegion[] exceptionHandlingRegions)
        {
            m_clusterType = clusterType;
            m_checkInstr = checkInstr;
            m_tryRegion = tryRegion;
            m_exceptionHandlingRegions = exceptionHandlingRegions;
            m_isParsed = false;
            m_escapePaths = new SortedSet<uint>();

            tryRegion.ContainingCluster = this;
            foreach (ExceptionHandlingRegion region in exceptionHandlingRegions)
                region.ContainingCluster = this;
        }
Exemplo n.º 3
0
        public CppRegionEmitter(CppBuilder builder, ExceptionHandlingRegion region, CppRegisterAllocator regAllocator, IDictionary<VReg, Clarity.Rpa.HighLocal> localLookup)
        {
            m_localLookup = localLookup;
            m_region = region;
            m_builder = builder;
            m_regAllocator = regAllocator;
            m_nodesToEmittedNodes = new Dictionary<CfgNode, HighCfgNodeHandle>();
            m_nodeOutlines = new Dictionary<CfgNode, CppCfgNodeOutline>();
            m_ssaToEmittedSsa = new Dictionary<SsaRegister, HighSsaRegister>();
            m_translatedOutboundEdges = new Dictionary<CfgOutboundEdge, CppTranslatedOutboundEdge>();

            m_pendingNodes = new Queue<CfgNode>();

            InternHighCfgNode(region.RootCfgNode);
        }
Exemplo n.º 4
0
        public CppMidCompiler(CppBuilder builder, CppClass cls, CppMethod method, ExceptionHandlingRegion mainRegion, string frameVarName, VReg[] args, VReg[] locals, VReg[] temporaries)
        {
            m_builder = builder;
            m_cls = cls;
            m_method = method;
            m_mainRegion = mainRegion;
            m_args = args;
            m_locals = locals;
            m_temporaries = temporaries;
            m_frameVarName = frameVarName;

            m_instructionStream = new MemoryStream();
            m_instructionWriter = new StreamWriter(m_instructionStream);
            m_regAllocator = new CppRegisterAllocator(builder);
        }
Exemplo n.º 5
0
        public CfgBuilder(ExceptionHandlingRegion region, CppBuilder builder, CppClass cls, CppMethod method, VReg[] args, VReg[] locals, IList<VReg> temporaries)
        {
            m_builder = builder;
            m_cls = cls;
            m_method = method;
            m_args = args;
            m_locals = locals;
            m_temporaries = temporaries;
            m_instrs = method.MethodDef.Method.Instructions;
            m_inClass = cls.TypeDef;
            m_inMethod = method.MethodDef;
            m_region = region;

            m_startInstr = (int)region.StartInstr;
            m_endInstr = (int)region.EndInstr;
            m_ehClusters = region.Clusters;

            LocateBranchTargets();
            ConstructCfg();
            CreateSuccessionGraph();
        }
Exemplo n.º 6
0
        public void Parse(ExceptionHandlingRegion ownerRegion, CfgBuilder ownerBuilder)
        {
            if (m_isParsed)
                return;

            List<ExceptionHandlingRegion> allRegions = new List<ExceptionHandlingRegion>();
            allRegions.Add(m_tryRegion);
            foreach (ExceptionHandlingRegion ehRegion in m_exceptionHandlingRegions)
                allRegions.Add(ehRegion);

            foreach (ExceptionHandlingRegion region in allRegions)
            {
                CfgBuilder builder = new CfgBuilder(region, ownerBuilder.CppBuilder, ownerBuilder.Class, ownerBuilder.CppMethod, ownerBuilder.Args, ownerBuilder.Locals, ownerBuilder.Temporaries);
                region.RootCfgNode = builder.RootNode;

                foreach (uint escapePath in region.EscapePaths)
                    m_escapePaths.Add(escapePath);
            }

            m_isParsed = true;
        }
Exemplo n.º 7
0
        public static void WriteMethodCode(Clarity.Rpa.HighFileBuilder fileBuilder, BinaryWriter writer, CppBuilder builder, CppClass cls, CppMethod method)
        {
            List<VReg> args = new List<VReg>();
            if (!method.Static)
            {
                CppClass thisClass = builder.GetCachedClass(method.DeclaredInClassSpec);
                CLRTypeSpec thisTypeSpec = method.DeclaredInClassSpec;
                VType vt = new VType(thisClass.IsValueType ? VType.ValTypeEnum.ManagedPtr : VType.ValTypeEnum.ReferenceValue, thisTypeSpec);
                args.Add(new VReg(builder, "bThis", vt, args.Count, VReg.UsageEnum.Argument));
            }

            foreach (CLRMethodSignatureInstanceParam param in method.MethodSignature.ParamTypes)
            {
                CLRTypeSpec spec = param.Type;
                VType vt;
                switch (param.TypeOfType)
                {
                    case CLRSigParamOrRetType.TypeOfTypeEnum.ByRef:
                        vt = new VType(VType.ValTypeEnum.ManagedPtr, spec);
                        break;
                    case CLRSigParamOrRetType.TypeOfTypeEnum.Value:
                        vt = new VType(ValTypeForTypeSpec(builder, spec), spec);
                        break;
                    default:
                        throw new ArgumentException();
                }
                args.Add(new VReg(builder, "bParam", vt, args.Count, VReg.UsageEnum.Argument));
            }

            List<VReg> locals = new List<VReg>();

            CLRSigLocalVarSig localVarSig = method.MethodDef.Method.LocalVarSig;
            if (localVarSig != null)
            {
                foreach (CLRSigLocalVar localVar in localVarSig.LocalVars)
                {
                    if (localVar.Constraints != null && localVar.Constraints.Length > 0)
                        throw new NotSupportedException("Local var constraints are not supported");
                    if (localVar.CustomMods != null && localVar.CustomMods.Length > 0)
                        throw new NotSupportedException("Local var custom mods are not supported");

                    CLRTypeSpec localTypeSpec = builder.Assemblies.InternVagueType(localVar.Type);

                    VReg vreg = null;
                    switch (localVar.VarKind)
                    {
                        case CLRSigLocalVar.LocalVarKind.ByRef:
                            vreg = new VReg(builder, "bLocal", new VType(VType.ValTypeEnum.ManagedPtr, localTypeSpec), locals.Count, VReg.UsageEnum.Local);
                            break;
                        case CLRSigLocalVar.LocalVarKind.Default:
                            vreg = new VReg(builder, "bLocal", new VType(CppCilExporter.ValTypeForTypeSpec(builder, localTypeSpec), localTypeSpec), locals.Count, VReg.UsageEnum.Local);
                            break;
                        default:
                            throw new NotImplementedException();
                    }

                    locals.Add(vreg);
                }
            }

            foreach (VReg vReg in locals)
                vReg.Liven();
            foreach (VReg vReg in args)
                vReg.Liven();

            List<VReg> temporaries = new List<VReg>();

            ExceptionHandlingRegion mainRegion = new ExceptionHandlingRegion(null, builder, method, 0, (uint)method.MethodDef.Method.Instructions.Length - 1, null);
            {
                CfgBuilder cfgBuilder = new CfgBuilder(mainRegion, builder, cls, method, args.ToArray(), locals.ToArray(), temporaries);
                mainRegion.RootCfgNode = cfgBuilder.RootNode;
            }

            CppMidCompiler midCompiler = new CppMidCompiler(builder, cls, method, mainRegion, "bTLFrame", args.ToArray(), locals.ToArray(), temporaries.ToArray());

            midCompiler.EmitAll(fileBuilder, writer);

            //MidCompile(builder, cls, method, mainRegion, args.ToArray(), locals.ToArray(), writer.BaseStream);

            foreach (VReg vReg in locals)
            {
                if (!vReg.IsAlive || vReg.IsZombie)
                    throw new Exception("Internal error: local vreg was killed");
            }
            foreach (VReg vReg in args)
            {
                if (!vReg.IsAlive || vReg.IsZombie)
                    throw new Exception("Internal error: arg vreg was killed");
            }
        }