Beispiel #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();
             }
         }
     }
 }
        public void EmitDecl(AssemblyWriter asmw, string varname)
        {
            asmw.WriteLine(varname + ": 	ISTRUC "+ Name);
            foreach (StructVar sv in Vars)
            {
                asmw.WriteLine("AT " + Name + "." + sv.Name + ", " + (sv.IsByte?"DB":"DW") + " 0");
            }

            asmw.WriteLine("IEND");
        }
 public void Emit(AssemblyWriter asmw)
 {
     asmw.WriteLine("struc   " + Name);
     foreach (StructVar sv in Vars)
     {
         sv.Emit(asmw);
     }
     asmw.WriteLine(".size:");
     asmw.WriteLine("endstruc");
 }
 public void Emit(AssemblyWriter asmw)
 {
     if (!IsStruct)
     {
         asmw.WriteLine("." + Name + ":    " + (IsByte?"RESB":"RESW") + "    " + Size.ToString());
     }
     else
     {
         asmw.WriteLine("." + Name + ":    " + "RESB    " + Type + ".size");
     }
 }
Beispiel #5
0
        public virtual void EmitFinalize(AssemblyWriter writer)
        {
            if (IsBootLoader)
            {
                writer.WriteLine("________________VATU__BOOTLOADER_SIGNATURE:");
                writer.WriteLine("times 510 - ($-$$) db 0");
                writer.WriteLine("dw 0xaa55");
            }

            if (!IsLibrary)
            {
                writer.WriteLine("PROGRAM_END:");
            }
        }
Beispiel #6
0
 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);
     }
 }
Beispiel #7
0
        public virtual void EmitPrepare(AssemblyWriter writer)
        {
            // Emit

            writer.WriteLine("bits 16");
            if (!IsLibrary)
            {
                writer.WriteLine("PROGRAM_ORG:");
            }
            else
            {
                writer.WriteLine("extern PROGRAM_ORG");
                writer.WriteLine("extern PROGRAM_END");
            }


            // INT Install
            if (IsFlat)
            {
                EmitInterruptInstallCode(writer);
            }
            //// define
            //foreach (StructElement xMember in mStructs)
            //{
            //    xMember.Emit(writer);
            //    writer.WriteLine();
            //}

            // alloc vars
            if (!IsFlat)
            {
                writer.WriteLine(";section .bss");
            }
            foreach (KeyValuePair <string, StructElement> p in DeclaredStructVars)
            {
                p.Value.EmitAlloc(writer, p.Key);
                writer.WriteLine();
            }
            writer.WriteLine();


            // define globals
            foreach (string ex in Globals)
            {
                writer.WriteLine("global\t" + ex);
            }
            writer.WriteLine();
        }
Beispiel #8
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();
        }
 public override void WriteText(AsmContext ec, AssemblyWriter aOutput)
 {
     aOutput.Write(mMnemonic);
 }
Beispiel #10
0
 public override void WriteText(AsmContext aAssembler, AssemblyWriter aOutput)
 {
     aOutput.Write(Code);
 }
 public void EmitAlloc(AssemblyWriter asmw, string varname)
 {
     asmw.WriteLine(varname + ": 	RESB "+ Name + ".size");
 }
Beispiel #12
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("\"");
        }
Beispiel #13
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!");
        }
Beispiel #14
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);
        }
Beispiel #15
0
 public abstract void WriteText(AsmContext ec, AssemblyWriter aOutput);
Beispiel #16
0
 public override void WriteText(AsmContext ec, AssemblyWriter aOutput)
 {
     aOutput.Write(Value);
 }
Beispiel #17
0
 public AsmContext(string file)
 {
     IsFlat           = false;
     AssemblerWriter  = new AssemblyWriter(file);
     mCurrentInstance = this;
 }
Beispiel #18
0
 public AsmContext(AssemblyWriter writer)
 {
     IsFlat           = false;
     AssemblerWriter  = writer;
     mCurrentInstance = this;
 }