コード例 #1
0
 void EmitInstructions(List <Instruction> ins, AssemblyWriter writer)
 {
     // Write out code
     for (int i = 0; i < ins.Count; i++)
     {
         var xOp = ins[i];
         if (xOp == null)
         {
             continue;
         }
         if (xOp.Emit)
         {
             string prefix = "\t\t\t";
             if (xOp is Label)
             {
                 var xLabel = (Label)xOp;
                 writer.WriteLine();
                 prefix = "\t\t";
                 writer.Write(prefix);
                 xLabel.WriteText(this, writer);
                 writer.WriteLine();
             }
             else
             {
                 writer.Write(prefix);
                 xOp.WriteText(this, writer);
                 writer.WriteLine();
             }
         }
     }
 }
コード例 #2
0
ファイル: Label.cs プロジェクト: Arsslensoft/NoobCompiler
 public override void WriteText(AsmContext aAssembler, AssemblyWriter aOutput)
 {
     if (IsGlobal)
     {
         aOutput.Write("global ");
         aOutput.WriteLine(QualifiedName);
     }
     aOutput.Write(QualifiedName);
     aOutput.Write(":");
     if (Comment.Length > 0)
     {
         aOutput.Write(" ;");
         aOutput.Write(Comment);
     }
 }
コード例 #3
0
        void EmitInterruptInstallCode(AssemblyWriter writer)
        {
            if (!string.IsNullOrEmpty(EntryPoint))
            {
                writer.WriteLine("global ___vatu_entry");
                writer.WriteLine("___vatu_entry:");
            }
            if (IsFlat)
            {
                writer.WriteLine("FINIT ; Initialize's the floating point unit");
            }


            foreach (Instruction inst in mDefInstructions)
            {
                writer.Write("\t");
                inst.WriteText(this, writer);
                writer.WriteLine();
            }

            if (!IsLibrary)
            {
                interrupt_name = "______INSTALL_INTERRUPTS";
            }
            else if (IsLibrary)
            {
                interrupt_name = "______INSTALL_INTERRUPTS_" + DateTime.Now.ToFileTimeUtc().ToString();
            }



            if (!string.IsNullOrEmpty(EntryPoint))
            {
                if (IsInterruptOverload && Interrupts.Count > 0)
                {
                    writer.WriteLine("\t\tcall " + interrupt_name);
                }
                writer.Write("\t\tjmp " + EntryPoint);
            }

            writer.WriteLine();
        }
コード例 #4
0
 public override void WriteText(AsmContext ec, AssemblyWriter aOutput)
 {
     aOutput.Write(mMnemonic);
 }
コード例 #5
0
 public override void WriteText(AsmContext aAssembler, AssemblyWriter aOutput)
 {
     aOutput.Write(Code);
 }
コード例 #6
0
        public static void WriteNormalizedString(string source, AssemblyWriter writer)
        {
            writer.Write("\"");
            int pos = 0;

            while (pos < source.Length)
            {
                bool def = false;
                char c   = source[pos];
                if (c == '\\')
                {
                    // --- Handle escape sequences
                    pos++;
                    if (pos >= source.Length)
                    {
                        throw new ArgumentException("Missing escape sequence");
                    }
                    switch (source[pos])
                    {
                    // --- Simple character escapes
                    case '\'': c = '\'';       break;

                    case '\"': c = '\"'; break;

                    case '\\': c = '\\'; break;

                    case '0': c = '\0'; break;

                    case 'a': c = '\a'; break;

                    case 'b': c = '\b'; break;

                    case 'f': c = '\f'; break;

                    case 'n': c = '\n'; break;

                    case 'r': c = '\r'; break;

                    case 't': c = '\t'; break;

                    case 'v': c = '\v'; break;

                    case 'x':
                        // --- Hexa escape (1-4 digits)
                        StringBuilder hexa = new StringBuilder(10);
                        pos++;
                        if (pos >= source.Length)
                        {
                            throw new ArgumentException("Missing escape sequence");
                        }
                        c = source[pos];
                        if (Char.IsDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
                        {
                            hexa.Append(c);
                            pos++;
                            if (pos < source.Length)
                            {
                                c = source[pos];
                                if (Char.IsDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
                                {
                                    hexa.Append(c);
                                    pos++;
                                    if (pos < source.Length)
                                    {
                                        c = source[pos];
                                        if (Char.IsDigit(c) || (c >= 'a' && c <= 'f') ||
                                            (c >= 'A' && c <= 'F'))
                                        {
                                            hexa.Append(c);
                                            pos++;
                                            if (pos < source.Length)
                                            {
                                                c = source[pos];
                                                if (Char.IsDigit(c) || (c >= 'a' && c <= 'f') ||
                                                    (c >= 'A' && c <= 'F'))
                                                {
                                                    hexa.Append(c);
                                                    pos++;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        c = (char)Int32.Parse(hexa.ToString(), NumberStyles.HexNumber);
                        pos--;
                        break;

                    case 'u':
                        // Unicode hexa escape (exactly 4 digits)
                        pos++;
                        if (pos + 3 >= source.Length)
                        {
                            throw new ArgumentException("Unrecognized escape sequence");
                        }
                        try
                        {
                            uint charValue = UInt32.Parse(source.Substring(pos, 4),
                                                          NumberStyles.HexNumber);
                            c    = (char)charValue;
                            pos += 3;
                        }
                        catch (SystemException)
                        {
                            throw new ArgumentException("Unrecognized escape sequence");
                        }
                        break;

                    case 'U':
                        // Unicode hexa escape (exactly 8 digits, first four must be 0000)
                        pos++;
                        if (pos + 7 >= source.Length)
                        {
                            throw new ArgumentException("Unrecognized escape sequence");
                        }
                        try
                        {
                            uint charValue = UInt32.Parse(source.Substring(pos, 8),
                                                          NumberStyles.HexNumber);
                            if (charValue > 0xffff)
                            {
                                throw new ArgumentException("Unrecognized escape sequence");
                            }
                            c    = (char)charValue;
                            pos += 7;
                        }
                        catch (SystemException)
                        {
                            throw new ArgumentException("Unrecognized escape sequence");
                        }
                        break;
                    }

                    writer.Write("\", ");
                    writer.Write((byte)c);
                    writer.Write(",\"");
                }
                else
                {
                    writer.Write(c);
                }
                pos++;
            }
            writer.Write("\"");
        }
コード例 #7
0
        public override void WriteText(AsmContext ec, AssemblyWriter aOutput)
        {
            if (RawAsm != null)
            {
                aOutput.WriteLine(RawAsm);
                return;
            }
            if (Labels != null)
            {
                aOutput.Write(Name);
                foreach (string lb in Labels)
                {
                    aOutput.WriteLine(" dw " + lb);
                }

                aOutput.WriteLine();
                return;
            }
            if (ReferenceName != null)
            {
                aOutput.Write(Name);
                aOutput.Write(" dw " + ReferenceName);
                aOutput.WriteLine();
                return;
            }
            if (FloatValue.HasValue)
            {
                aOutput.Write(Name);
                aOutput.Write(" dd " + FloatValue.ToString().Replace(",", "."));
                aOutput.WriteLine();
                return;
            }
            if (StrVal != null)
            {
                if (IsGlobal)
                {
                    aOutput.Write("global ");
                    aOutput.WriteLine(Name);
                }
                if (string.IsNullOrEmpty(StrVal))
                {
                    aOutput.Write(Name);
                    aOutput.Write(" times 256 db 0");
                    return;
                }
                aOutput.Write(Name);
                aOutput.Write(" db ");


                //foreach (char c in StrVal)
                //{
                //    if (c == '\r' || c == '\n')
                //    {
                //        if (!string.IsNullOrEmpty(last))
                //              strdecl+= "\"" + last + "\", ";
                //        strdecl += b[i].ToString();
                //        strdecl += ",";
                //        last = "";
                //    }
                //    else last += c + "";
                //    i++;
                //}
                //if (!string.IsNullOrEmpty(last))
                //    strdecl += "\"" + last + "\"";
                //else strdecl = strdecl.Remove(strdecl.Length - 1, 1);
                if (!verbatim)
                {
                    StringHelper.WriteNormalizedString(StrVal, aOutput);
                }
                else
                {
                    aOutput.Write("\"" + StringHelper.StringFromVerbatimLiteral(StrVal) + "\"");
                }

                if (!StrConst)
                {
                    aOutput.WriteLine();
                    aOutput.Write("\t\t  times " + (255 - StrVal.Length).ToString() + " db 0");
                }
                else
                {
                    aOutput.Write(",0");
                }
                return;
            }

            if (RawDefaultValue != null)
            {
                if (RawDefaultValue.Length == 0)
                {
                    aOutput.Write(Name);
                    aOutput.Write(":");
                    return;
                }
                if ((from item in RawDefaultValue
                     group item by item
                     into i
                     select i).Count() > 1 || RawDefaultValue.Length < 10)
                {
                    if (IsGlobal)
                    {
                        aOutput.Write("global ");
                        aOutput.WriteLine(Name);
                    }
                    aOutput.Write(Name);
                    aOutput.Write(" db ");


                    for (int i = 0; i < (RawDefaultValue.Length - 1); i++)
                    {
                        aOutput.Write(RawDefaultValue[i]);
                        aOutput.Write(", ");
                    }
                    aOutput.Write(RawDefaultValue.Last());
                }
                else
                {
                    //aOutputWriter.WriteLine("TIMES 0x50000 db 0");
                    if (IsGlobal)
                    {
                        aOutput.Write("global ");
                        aOutput.WriteLine(Name);
                    }
                    aOutput.Write(Name);
                    aOutput.Write(": TIMES ");
                    aOutput.Write(RawDefaultValue.Length);
                    aOutput.Write(" db ");
                    aOutput.Write(RawDefaultValue[0]);
                }
                return;
            }
            if (UntypedDefaultValue != null)
            {
                StringBuilder xSB = new StringBuilder();
                if (IsGlobal)
                {
                    aOutput.Write("global ");
                    aOutput.WriteLine(Name);
                }
                aOutput.Write(Name);
                aOutput.Write(" dw ");
                Func <object, string> xGetTextForItem = delegate(object aItem) {
                    var xElementRef = aItem as ElementReference;
                    if (xElementRef == null)
                    {
                        return((aItem ?? 0).ToString());
                    }
                    else
                    {
                        if (xElementRef.Offset == 0)
                        {
                            return(xElementRef.Name);
                        }
                        return(xElementRef.Name + " + " + xElementRef.Offset);
                    }
                };
                for (int i = 0; i < (UntypedDefaultValue.Length - 1); i++)
                {
                    aOutput.Write(xGetTextForItem(UntypedDefaultValue[i]));
                    aOutput.Write(", ");
                }
                aOutput.Write(xGetTextForItem(UntypedDefaultValue.Last()));
                return;
            }

            throw new Exception("Situation unsupported!");
        }
コード例 #8
0
        public virtual void Emit(AssemblyWriter writer)
        {
            // Optimize
            Optimize();

            // prepare emit
            EmitPrepare(writer);
            // Write out readonly
            if (!IsFlat)
            {
                writer.WriteLine(";section .rodata");
            }

            foreach (DataMember xMember in mCDataMembers)
            {
                writer.Write("\t");
                if (xMember.IsComment)
                {
                    writer.Write(xMember.Name);
                }
                else
                {
                    xMember.WriteText(this, writer);
                }
                writer.WriteLine();
            }

            writer.WriteLine();


            // Write out data declarations
            if (!IsFlat)
            {
                writer.WriteLine(";section .data");
            }

            foreach (DataMember xMember in mDataMembers)
            {
                writer.Write("\t");
                if (xMember.IsComment)
                {
                    writer.Write(xMember.Name);
                }
                else
                {
                    xMember.WriteText(this, writer);
                }
                writer.WriteLine();
            }
            // declare vars
            writer.WriteLine();
            foreach (KeyValuePair <string, StructElement> p in DeclaredStructVars)
            {
                p.Value.EmitDecl(writer, p.Key);
                writer.WriteLine();
            }
            writer.WriteLine();
            if (!IsFlat)
            {
                writer.WriteLine(";section .text");
            }

            if (!IsFlat)
            {
                // define externs
                foreach (string ex in Externals)
                {
                    writer.WriteLine("extern\t" + ex);
                }
                writer.WriteLine();
            }


            if (!IsFlat)
            {
                EmitInterruptInstallCode(writer);
            }

            if (IsInterruptOverload && Interrupts.Count > 0)
            {
                InstallINTInstruction iit = new InstallINTInstruction(Interrupts, interrupt_name);
                writer.WriteLine();
                writer.Write("\t\t");
                iit.WriteText(this, writer);
            }
            // Emit Code
            EmitInstructions(mInstructions, writer);
            EmitInstructions(AnonymousInstructions, writer);
            // Emit Finalize
            EmitFinalize(writer);
        }
コード例 #9
0
 public override void WriteText(AsmContext ec, AssemblyWriter aOutput)
 {
     aOutput.Write(Value);
 }