예제 #1
0
 public void EmitCallCctors()
 {
     foreach (var method in IncludedNodes.OfType <Method>().Where(m => m.Name == ".cctor"))
     {
         EmitSource($"call {method.Signature}");
         Emit(new Label(method, this), Opcode.PSH, Opcode.JSR);
     }
 }
예제 #2
0
        public Compiler Include(Node node, object includedFrom = null)
        {
            if (node != null && !IncludedNodes.Contains(node) && (node as Method)?.MethodBase.IsAbstract != true)
            {
                Debug.Assert(node.ToString() != "SbcLibrary.Class");
                Debug.Assert(!(node is Method method && method.Owner.Type.IsGenericTypeDefinition));
                IncludedNodes.Add(node);
                node.OnInclude();
                node.IncludedFrom = includedFrom;
            }

            return(this);
        }
예제 #3
0
        public void Compile()
        {
            ProcessAssemblies();

            Global.Compiler = this;
            Global.Config   = Config;

            Include(GetClass(typeof(string)));
            Include(GetClass(typeof(int)));
            Include(GetClass(typeof(MethodMetadata)));
            Include(GetClass(typeof(MethodHandlerData)));
            Include(Main);

            EmitSource("Initialising stack and frame", "Startup");
            Emit(Config.StackStart, Opcode.PSH, Opcode.POX);
            Emit(Config.StackStart + Config.StackSize - 1, Opcode.PSH, Opcode.POY);
            EmitSource("Initialising heap");
            Emit(new Label(HeapInitialise, this), Opcode.PSH, Opcode.JSR);
            EmitSource("Calling static constructors");
            Emit(new Label(CallCctors, this), Opcode.PSH, Opcode.JSR);
            EmitSource("Calling main");
            Emit(new Label(Main, this), Opcode.PSH, Opcode.JSR);
            EmitSource("Break");
            Emit(Break.InlineAction);
            EmitMethodData("Startup", "Startup.Startup", "Startup", null, new ArgData[0], new ArgData[0]);

            for (var i = 0; i < IncludedNodes.Count; i++)
            {
                IncludedNodes[i].GenerateExecutable();
            }

            if (IncludedNodes.Contains(Newobj))
            {
                HeapInitialise.GenerateExecutable();
            }
            else
            {
                LabelRefs.Single(lr => lr.Name == HeapInitialise.Id).RemoveCall = true;
            }

            if (IncludedNodes.OfType <Method>().Any(m => m.Name == ".cctor"))
            {
                CallCctors.GenerateExecutable();
            }
            else
            {
                LabelRefs.Single(lr => lr.Name == CallCctors.Id).RemoveCall = true;
            }

            Config.ExecutableSize = (Compilation.Opcodes.Count + Config.SlotsPerMemoryUnit - 1) / Config.SlotsPerMemoryUnit;
            Config.ConstStart     = Config.ExecutableStart + Config.ExecutableSize;

            foreach (var node in IncludedNodes)
            {
                node.GenerateConstData();
            }

            Config.MethodMetadata = CurrentConstAddr;
            Compilation.ConstData.Add(IncludedNodes.OfType <Method>().Count());
            foreach (var node in IncludedNodes.OfType <Method>())
            {
                node.GenerateMethodMetadata();
            }
            Debug.Assert(CurrentConstAddr - Config.MethodMetadata == 1 + IncludedNodes.OfType <Method>().Count() * GetClass(typeof(MethodMetadata)).Elements.Count);

            Config.ConstSize  = Compilation.ConstData.Count;
            Config.StaticSize = Compilation.StaticDataCount;
            LabelDefs.Add(new Label("Config.StaticSize", this, Config.StaticSize));
            LabelDefs.Add(new Label("Config.MethodMetadata", this, Config.MethodMetadata));

            PatchLabels();
            Compilation.SetAddressInfo();

            if (!string.IsNullOrEmpty(MifFileName))
            {
                Compilation.WriteMif(MifFileName);
            }
        }