Пример #1
0
        public Compiler Emit(params EmitArg[] args)
        {
            foreach (var arg in args.Where(a => a.Arg != null))
            {
                switch (arg.Arg)
                {
                case Label label:
                    label.IsAddressSlot = true;
                    label.Value         = Compilation.Opcodes.Count;
                    LabelRefs.Add(label);
                    EmitValue(0, Config.BitsPerMemoryUnit);
                    break;

                case Opcode opcode:
                    EmitOpcodes(opcode);
                    break;

                case int number:
                    EmitValue(number);
                    break;

                case Action action:
                    action();
                    break;

                case EmitArg[] emitargs:
                    Emit(emitargs);
                    break;

                default:
                    throw new Exception($"Can't emit {arg}");
                }
            }

            return(this);
        }
Пример #2
0
 public int ConstRef(string name, object owner, int offset = 0)
 {
     Debug.Assert(CurrentConstAddr > 0);
     LabelRefs.Add(new Label(name, owner, CurrentConstAddr + offset));
     return(0);
 }
Пример #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);
            }
        }