Example #1
0
        /// <summary>Parse the input X# source code file and generate the matching target assembly
        /// language.</summary>
        /// <param name="aReader">X# source code reader.</param>
        /// <returns>The resulting target assembler content. The returned object contains
        /// a code and a data block.</returns>
        public Assembler.Assembler Generate(TextReader aReader)
        {
            if (aReader == null)
            {
                throw new ArgumentNullException(nameof(aReader));
            }
            mPatterns.EmitUserComments = EmitUserComments;
            mLineNo = 0;
            var xResult = new Assembler.Assembler();

            try {
                // Read one X# source code line at a time and process it.
                while (true)
                {
                    mLineNo++;
                    string xLine = aReader.ReadLine();
                    if (xLine == null)
                    {
                        break;
                    }

                    ProcessLine(xLine, mLineNo);
                }
                AssertLastFunctionComplete();
                return(xResult);
            } finally {
                Assembler.Assembler.ClearCurrentInstance();
            }
        }
Example #2
0
 public override void AssembleNew(XSharp.Assembler.Assembler aAssembler, object aMethodInfo)
 {
     new LiteralAssemblerCode("%ifdef DEBUGSTUB");
     new LiteralAssemblerCode("pushad");
     new LiteralAssemblerCode("Call DebugStub_SendPtr");
     new LiteralAssemblerCode("popad");
     new LiteralAssemblerCode("%endif");
 }
Example #3
0
 public override void AssembleNew(XSharp.Assembler.Assembler aAssembler, object aMethodInfo)
 {
     new LiteralAssemblerCode("%ifdef DEBUGSTUB");
     new LiteralAssemblerCode("push dword [EBP + 8]");
     new LiteralAssemblerCode("Call DebugStub_SendComplexNumber");
     new LiteralAssemblerCode("add ESP, 4");
     new LiteralAssemblerCode("%endif");
 }
Example #4
0
 public override void AssembleNew(XSharp.Assembler.Assembler aAssembler, object aMethodInfo)
 {
     new LiteralAssemblerCode("%ifdef DEBUGSTUB");
     new LiteralAssemblerCode("mov AL, [EBP + 12]");
     new LiteralAssemblerCode("mov BL, [EBP + 8]");
     new LiteralAssemblerCode("mov ECX, 0");
     new LiteralAssemblerCode("mov ESI, EBP");
     new LiteralAssemblerCode("call DebugStub_SendCommandOnChannel");
     new LiteralAssemblerCode("%endif");
 }
Example #5
0
 public override void AssembleNew(XSharp.Assembler.Assembler aAssembler, object aMethodInfo)
 {
     new LiteralAssemblerCode("%ifdef DEBUGSTUB");
     XS.Label(".BeforeArgumentsPrepare");
     new LiteralAssemblerCode("mov EBX, [EBP + 12]");
     new LiteralAssemblerCode("push dword [EBX + 12]");
     new LiteralAssemblerCode("add EBX, 16");
     new LiteralAssemblerCode("push dword EBX");
     XS.Label(".BeforeCall");
     new LiteralAssemblerCode("Call DebugStub_SendText");
     new LiteralAssemblerCode("add ESP, 8");
     new LiteralAssemblerCode("%endif");
 }
Example #6
0
        public static void Assemble(XSharp.Assembler.Assembler aAssembler, int aSize, bool debugEnabled)
        {
            int xAlignedSize = (int)Align((uint)aSize, 4);

            XS.Comment("address at: [esp]");
            DoNullReferenceCheck(aAssembler, debugEnabled, 0);

            XS.Pop(XSRegisters.EAX);
            for (int i = xAlignedSize - 4; i >= 0; i -= 4)
            {
                XS.Push(XSRegisters.EAX, isIndirect: true, displacement: i);
            }
        }
Example #7
0
        public static void Assemble(XSharp.Assembler.Assembler aAssembler, int aSize, bool debugEnabled)
        {
            int xAlignedSize = (int)Align((uint)aSize, 4);

            XS.Comment($"address at: [esp+{xAlignedSize}]");
            DoNullReferenceCheck(aAssembler, debugEnabled, xAlignedSize);

            XS.Set(XSRegisters.EAX, XSRegisters.ESP, sourceIsIndirect: true, sourceDisplacement: xAlignedSize);
            for (int i = 0; i < xAlignedSize; i += 4)
            {
                XS.Pop(XSRegisters.EBX);
                XS.Set(XSRegisters.EAX, XSRegisters.EBX, destinationIsIndirect: true, destinationDisplacement: i);
            }
            XS.Add(XSRegisters.ESP, 4);
        }
Example #8
0
        public static void DoExecute(XSharp.Assembler.Assembler assembler, _MethodInfo aMethod, string field, Type declaringType, ILOpCode aCurrentOpCode)
        {
            // call cctor:
            var xCctor = (declaringType.GetConstructors(BindingFlags.Static | BindingFlags.NonPublic) ?? new ConstructorInfo[0]).SingleOrDefault();

            if (xCctor != null)
            {
                XS.Call(LabelName.Get(xCctor));
                if (aCurrentOpCode != null)
                {
                    ILOp.EmitExceptionLogic(assembler, aMethod, aCurrentOpCode, true, null, ".AfterCCTorExceptionCheck");
                    XS.Label(".AfterCCTorExceptionCheck");
                }
            }
            string xDataName = field;

            XS.Push(xDataName);
        }
Example #9
0
 public Mul_Ovf(XSharp.Assembler.Assembler aAsmblr)
     : base(aAsmblr)
 {
 }
Example #10
0
 public abstract bool IsComplete(Assembler aAssembler);
Example #11
0
 public Stelem_Ref(XSharp.Assembler.Assembler aAsmblr)
     : base(aAsmblr)
 {
 }
Example #12
0
 public Add_Ovf_Un(XSharp.Assembler.Assembler aAsmblr)
     : base(aAsmblr)
 {
 }
Example #13
0
 public abstract void WriteText(Assembler aAssembler, TextWriter aOutput);
Example #14
0
 public override void WriteText(Assembler aAssembler, System.IO.TextWriter aOutput)
 {
     aOutput.Write(this.GetAsText());
 }
Example #15
0
 public virtual void UpdateAddress(Assembler aAssembler, ref ulong aAddress)
 {
     StartAddress = aAddress;
 }
Example #16
0
 public abstract void WriteData(Assembler aAssembler, Stream aOutput);
Example #17
0
 public Ldc_R4(XSharp.Assembler.Assembler aAsmblr)
     : base(aAsmblr)
 {
 }
Example #18
0
 public LdStr(XSharp.Assembler.Assembler aAsmblr) : base(aAsmblr)
 {
 }
Example #19
0
 public Localloc(XSharp.Assembler.Assembler aAsmblr)
     : base(aAsmblr)
 {
 }
Example #20
0
 public Shl(XSharp.Assembler.Assembler aAsmblr)
     : base(aAsmblr)
 {
 }
Example #21
0
 public Sizeof(XSharp.Assembler.Assembler aAsmblr)
     : base(aAsmblr)
 {
 }
Example #22
0
 public Conv_Ovf_I8(XSharp.Assembler.Assembler aAsmblr)
     : base(aAsmblr)
 {
 }
Example #23
0
 public Clt_Un(XSharp.Assembler.Assembler aAsmblr)
     : base(aAsmblr)
 {
 }
Example #24
0
 public Cgt(XSharp.Assembler.Assembler aAsmblr)
     : base(aAsmblr)
 {
 }
Example #25
0
 public override void AssembleNew(XSharp.Assembler.Assembler aAssembler, object aMethodInfo)
 {
     new LiteralAssemblerCode("%ifdef DEBUGSTUB");
     new LiteralAssemblerCode("mov dword [DebugStub_DebugBreakOnNextTrace], 1");
     new LiteralAssemblerCode("%endif");
 }
Example #26
0
 public Ldsfld(XSharp.Assembler.Assembler aAsmblr)
     : base(aAsmblr)
 {
 }
Example #27
0
 public static void DoExecute(XSharp.Assembler.Assembler Assembler, _MethodInfo aCurrentMethod, MethodBase aTargetMethod, ILOpCode aCurrent, string currentLabel, bool debugEnabled)
 {
     DoExecute(Assembler, aCurrentMethod, aTargetMethod, aCurrent, currentLabel, ILOp.GetLabel(aCurrentMethod, aCurrent.NextPosition), debugEnabled);
 }
Example #28
0
 public Ldind_I(XSharp.Assembler.Assembler aAsmblr)
     : base(aAsmblr)
 {
 }
Example #29
0
 public Ldtoken(XSharp.Assembler.Assembler aAsmblr)
     : base(aAsmblr)
 {
 }
Example #30
0
        public static void DoExecute(XSharp.Assembler.Assembler Assembler, _MethodInfo aCurrentMethod, MethodBase aTargetMethod, ILOpCode aOp, string currentLabel, string nextLabel, bool debugEnabled)
        {
            var    xMethodInfo    = aTargetMethod as MethodInfo;
            string xNormalAddress = LabelName.Get(aTargetMethod);
            var    xParameters    = aTargetMethod.GetParameters();

            // todo: implement exception support
            uint xExtraStackSize = GetStackSizeToReservate(aTargetMethod);

            if (!aTargetMethod.IsStatic)
            {
                uint xThisOffset = 0;
                foreach (var xItem in xParameters)
                {
                    xThisOffset += Align(SizeOfType(xItem.ParameterType), 4);
                }
                var stackOffsetToCheck = xThisOffset;
                if (IsReferenceType(aTargetMethod.DeclaringType))
                {
                    DoNullReferenceCheck(Assembler, debugEnabled, (int)stackOffsetToCheck + 4);
                }
                else
                {
                    DoNullReferenceCheck(Assembler, debugEnabled, (int)stackOffsetToCheck);
                }
            }

            if (xExtraStackSize > 0)
            {
                XS.Sub(XSRegisters.ESP, (uint)xExtraStackSize);
            }
            XS.Call(xNormalAddress);

            uint xReturnSize = 0;

            if (xMethodInfo != null)
            {
                xReturnSize = SizeOfType(xMethodInfo.ReturnType);
            }
            if (aCurrentMethod != null)
            {
                EmitExceptionLogic(Assembler, aCurrentMethod, aOp, true,
                                   delegate
                {
                    var xStackOffsetBefore = aOp.StackOffsetBeforeExecution.Value;

                    uint xPopSize = 0;
                    foreach (var type in aOp.StackPopTypes)
                    {
                        xPopSize += Align(SizeOfType(type), 4);
                    }

                    var xResultSize = xReturnSize;
                    if (xResultSize % 4 != 0)
                    {
                        xResultSize += 4 - (xResultSize % 4);
                    }

                    ILOp.EmitExceptionCleanupAfterCall(Assembler, xResultSize, xStackOffsetBefore, xPopSize);
                }, nextLabel);
            }
        }