Exemple #1
0
        public override void ExitAssign1(ssuplParser.Assign1Context context)
        {
            // assign ``:`` ID EQ expr
            string vname = context.ID().GetText();

            if (!symtable.Contains(vname))
            {
                throw new Exception("Undeclared variable: " + vname);
            }
            if (type(context.expr()) != symtable.Get(vname).type)
            {
                throw new Exception("Variable type mismatch in assignment");
            }
            VarType vtype        = symtable.Get(vname).type;
            int     vsizeInQuads = SizeForType(vtype) / 8;
            string  lbl          = symtable.Get(vname).location;

            ASM asmcode = new ASM();

            asmcode += code.Get(context.expr());
            for (int i = 0; i < vsizeInQuads; i++)
            {
                asmcode += "pop rax";
                asmcode += $"mov [{lbl}+{i * 8}],rax";
            }
            code.Put(context, asmcode);
            typeAttr.Put(context, vtype);
        }
Exemple #2
0
        public static ASM operator +(ASM a1, ASM a2)
        {
            ASM res = new ASM();

            res.instructions.AddRange(a1.instructions);
            res.instructions.AddRange(a2.instructions);
            return(res);
        }
Exemple #3
0
 public ASM(string v1, string v2, string v3, string v4, ASM aSM, string v6)
 {
     this.instructions.Add(v1);
     this.instructions.Add(v2);
     this.instructions.Add(v3);
     this.instructions.Add(v4);
     this.instructions.AddRange(aSM.instructions);
     this.instructions.Add(v6);
 }
Exemple #4
0
        public override void ExitAssign2(ssuplParser.Assign2Context context)
        {
            // assign ``:`` ID DOT FIELD EQ expr
            //Console.WriteLine("Exitassign2");
            string vname = context.ID().GetText();

            if (!symtable.Contains(vname))
            {
                throw new Exception("Undeclared variable: " + vname);
            }
            if (type(context.expr()) != VarType.DOUBLE || symtable.Get(vname).type != VarType.VEC4)
            {
                throw new Exception("Variable type mismatch in assignment");
            }
            VarType vtype        = symtable.Get(vname).type;
            int     vsizeInQuads = SizeForType(vtype) / 8;

            ASM asmcode = new ASM();

            asmcode += code.Get(context.expr());
            asmcode += "pop rax";
            string lbl = symtable.Get(vname).location;

            switch (context.FIELD().GetText())
            {
            case ".x":
                asmcode += $"mov [{lbl}+{0}], rax";
                break;

            case ".y":
                asmcode += $"mov [{lbl}+{8}], rax";
                break;

            case ".z":
                asmcode += $"mov [{lbl}+{16}], rax";
                break;

            case ".w":
                asmcode += $"mov [{lbl}+{24}], rax";
                break;
            }
            code.Put(context, asmcode);
            typeAttr.Put(context, vtype);
        }
Exemple #5
0
        public override void ExitFactor7(ssuplParser.Factor7Context context)
        {
            //factor -> ID FIELD
            string vname = context.ID().GetText();

            if (!symtable.Contains(vname))
            {
                throw new Exception("Undeclared variable: " + vname);
            }
            if (symtable.Get(vname).type != VarType.VEC4)
            {
                throw new Exception("Type not a vec4 inside of ExitFactor7");
            }
            VarType vtype        = symtable.Get(vname).type;
            int     vsizeInQuads = SizeForType(vtype) / 8;
            ASM     asmcode      = new ASM();
            var     lbl          = symtable.Get(vname).location;

            switch (context.FIELD().GetText())
            {
            case ".x":
                asmcode += $"mov rax,[{lbl}+{0}]";
                break;

            case ".y":
                asmcode += $"mov rax,[{lbl}+{8}]";
                break;

            case ".z":
                asmcode += $"mov rax,[{lbl}+{16}]";
                break;

            case ".w":
                asmcode += $"mov rax,[{lbl}+{24}]";
                break;
            }
            //removed qword
            asmcode += "push rax";
            code.Put(context, asmcode);
            typeAttr.Put(context, VarType.DOUBLE);
        }
Exemple #6
0
        public override void ExitFactor3(ssuplParser.Factor3Context context)
        {
            //factor -> ID
            string vname = context.ID().GetText();

            if (!symtable.Contains(vname))
            {
                throw new Exception("Undeclared variable: " + vname);
            }
            string  lbl   = symtable.Get(vname).location;
            VarType vtype = symtable.Get(vname).type;

            int vsizeInQuads = SizeForType(vtype) / 8;
            ASM asmcode      = new ASM();

            for (int i = vsizeInQuads - 1; i >= 0; i--)
            {
                asmcode += $"mov rax,[{lbl}+{i * 8}]";
                asmcode += "push qword rax";
            }
            code.Put(context, asmcode);

            typeAttr.Put(context, symtable.Get(vname).type);
        }
Exemple #7
0
        public static void Main(string[] args)
        {
            Syms.init("ssupl.tokens");

            string gdata;

            using (var r = new StreamReader("terminals.txt"))
            {
                gdata = r.ReadToEnd();
            }
            var tokenizer = new Tokenizer(gdata);

            string idata;

            using (var r = new StreamReader("input.txt"))
            {
                idata = r.ReadToEnd();
            }

            tokenizer.setInput(idata);
            IList <IToken> tokens = new List <IToken>();

            while (true)
            {
                Token t = tokenizer.next();
                //Console.WriteLine("token: " + t);
                if (t.Symbol == "$")
                {
                    break;
                }
                CommonToken T = new CommonToken(Syms.stringToInt[t.Symbol], t.Lexeme);
                T.Line = t.Line;
                tokens.Add(T);
            }


            var antlrtokenizer = new BufferedTokenStream(new ListTokenSource(tokens));
            var parser         = new ssuplParser(antlrtokenizer);

            parser.BuildParseTree = true;
            parser.ErrorHandler   = new BailErrorStrategy();
            var antlrroot = parser.start();

            var listener = new CodeGenerator();
            var walker   = new ParseTreeWalker();

            walker.Walk(listener, antlrroot);
            var allcode = new ASM(
                "default rel",
                "section .text",
                "global main",
                "main:",
                listener.code.Get(antlrroot),
                "section .data");

            //This makes the functions and actual program
            using (var w = new StreamWriter("out.asm"))
            {
                //Console.WriteLine("----------------------------");
                //Console.WriteLine(allcode);
                //Console.WriteLine("----------------------------");
                w.Write(allcode.ToString());
                foreach (var literal in listener.stringPool.Keys)
                {
                    w.WriteLine("; " + literal.Replace("\n", "\\n"));
                    w.WriteLine(listener.stringPool[literal].address + ":");
                    w.Write("db ");
                    byte[] b = Encoding.ASCII.GetBytes(literal);
                    for (int i = 0; i < literal.Length; ++i)
                    {
                        w.Write(b[i]);
                        w.Write(",");
                    }
                    w.WriteLine("0");
                }
            }

            //This makes variables possible in the global data after the program
            //Literally gets append to the bottom of the out.asm file
            var symtable = listener.symtable.table;

            foreach (var sym in symtable)
            {
                //Need dq to have 0,0,0,0 for vec4
                if (sym.Value.type == VarType.VEC4)
                {
                    var globaldata = new ASM(
                        $"{sym.Value.location}:",
                        "dq 0",
                        "dq 0",
                        "dq 0",
                        "dq 0");

                    using (var appendglobals = File.AppendText("out.asm"))
                    {
                        appendglobals.Write(globaldata.ToString());
                    }
                }
                else
                {
                    var globaldata = new ASM(
                        $"{sym.Value.location}:",
                        "dq 0");
                    using (var appendglobals = File.AppendText("out.asm"))
                    {
                        appendglobals.Write(globaldata.ToString());
                    }
                }
            }
        }
Exemple #8
0
        public static void Main(string[] args)
        {
            Syms.init("ssupl.tokens");

            string gdata;

            using (var r = new StreamReader("terminals.txt"))
            {
                gdata = r.ReadToEnd();
            }
            var tokenizer = new Tokenizer(gdata);

            string idata;

            using (var r = new StreamReader("input.txt"))
            {
                idata = r.ReadToEnd();
            }


            var rex = new Regex(@"\n[ \t]+([^\n]+)");

            idata  = rex.Replace(idata, " $1");
            idata += "\n";

            tokenizer.setInput(idata);
            IList <IToken> tokens = new List <IToken>();

            while (true)
            {
                Token t = tokenizer.next();
                if (t.Symbol == "$")
                {
                    break;
                }
                CommonToken T = new CommonToken(Syms.stringToInt[t.Symbol], t.Lexeme);
                T.Line = t.Line;
                tokens.Add(T);
            }



            var antlrtokenizer = new BufferedTokenStream(new ListTokenSource(tokens));
            var parser         = new ssuplParser(antlrtokenizer);

            parser.BuildParseTree = true;
            parser.ErrorHandler   = new BailErrorStrategy();
            var antlrroot = parser.start();

            var listener = new CodeGenerator();
            var walker   = new ParseTreeWalker();

            walker.Walk(listener, antlrroot);
            var allcode = new ASM(
                "default rel",
                "section .text",
                "global main",
                "main:",
                listener.code.Get(antlrroot),
                "section .data");

            using (var w = new StreamWriter("out.asm"))
            {
                w.Write(allcode.ToString());
            }
        }