Esempio n. 1
0
        public virtual T VisitBinaryExprNode(BinaryExprNode n)
        {
            T a = n.lhs.Accept(this);
            T b = n.rhs.Accept(this);

            return(default(T));
        }
Esempio n. 2
0
            public override object VisitBinaryExprNode(BinaryExprNode n)
            {
                Visit(n.lhs); Visit(n.rhs);

                switch (n.op)
                {
                case "*":
                {
                    semanticChecker.CheckAndReport(n.rhs.Type.Equals(TypeSymbol.INT_SYMBOL) || n.rhs.Type.Equals(TypeSymbol.FLOAT_SYMBOL), n.sourceLoc, "type mismatch");
                    semanticChecker.CheckAndReport(n.lhs.Type.Equals(TypeSymbol.INT_SYMBOL) || n.lhs.Type.Equals(TypeSymbol.FLOAT_SYMBOL), n.sourceLoc, "type mismatch");
                    semanticChecker.CheckAndReport(n.Type.Match(n.lhs.Type) && n.Type.Match(n.rhs.Type), n.sourceLoc, "type mismatch");
                }
                break;

                case "/":
                case "%":
                case "+":
                case "-":
                case "<":
                case ">":
                case "<=":
                case ">=":
                {
                    semanticChecker.CheckAndReport(n.rhs.Type.Equals(TypeSymbol.INT_SYMBOL) || n.rhs.Type.Equals(TypeSymbol.FLOAT_SYMBOL), n.sourceLoc, "type mismatch");
                    semanticChecker.CheckAndReport(n.lhs.Type.Equals(TypeSymbol.INT_SYMBOL) || n.lhs.Type.Equals(TypeSymbol.FLOAT_SYMBOL), n.sourceLoc, "type mismatch");
                }
                break;

                case "==":
                case "!=":
                {
                    semanticChecker.CheckAndReport(n.rhs.Type.Match(n.lhs.Type), n.sourceLoc, "type mismatch");
                }
                break;

                case ">>":
                case "<<":
                case "&":
                case "|":
                case "^":
                {
                    semanticChecker.CheckAndReport(n.rhs.Type.Equals(TypeSymbol.INT_SYMBOL), n.sourceLoc, "type mismatch");
                    semanticChecker.CheckAndReport(n.lhs.Type.Equals(TypeSymbol.INT_SYMBOL), n.sourceLoc, "type mismatch");
                }
                break;

                case "&&":
                case "||":
                {
                    semanticChecker.CheckAndReport(n.rhs.Type.Equals(TypeSymbol.BOOL_SYMBOL), n.sourceLoc, "type mismatch");
                    semanticChecker.CheckAndReport(n.lhs.Type.Equals(TypeSymbol.BOOL_SYMBOL), n.sourceLoc, "type mismatch");
                }
                break;
                }
                return(null);
            }
Esempio n. 3
0
        public object VisitBinaryExprNode(BinaryExprNode n)
        {
            WriteLine(n.kind + " - " + n.op + ":" + n.Type);
            Indent();
            Visit(n.rhs);
            Visit(n.lhs);
            Dedent();

            return(null);
        }
Esempio n. 4
0
        public override MIPSRegister VisitBinaryExprNode(BinaryExprNode n)
        {
            var lhs = Gen(n.lhs);
            var rhs = Gen(n.rhs);

            switch (n.op)
            {
            case "*":
            {
                emit.Emit("mul", lhs, lhs, rhs);
            }
            break;

            case "/":
            {
                emit.Emit("rem", lhs, lhs, rhs);
            }
            break;

            case "+":
            {
                emit.Emit("add", lhs, lhs, rhs);
            }
            break;

            case "-":
            {
                emit.Emit("sub", lhs, lhs, rhs);
            }
            break;

            case ">>":
            {
                emit.Emit("srav", lhs, lhs, rhs);
            }
            break;

            case "<<":
            {
                emit.Emit("sllv", lhs, lhs, rhs);
            }
            break;

            case "&":
            {
                emit.Emit("and", lhs, lhs, rhs);
            }
            break;

            case "|":
            {
                emit.Emit("or", lhs, lhs, rhs);
            }
            break;

            case "^":
            {
                emit.Emit("xor", lhs, lhs, rhs);
            }
            break;

            case "%":
            {
                emit.Emit("rem", lhs, lhs, rhs);
            }
            break;

            case "<":
            {
                emit.Emit("slt", lhs, lhs, rhs);
            }
            break;

            case ">":
            {
                emit.Emit("slt", lhs, rhs, lhs);
            }
            break;

            case "<=":
            {
                emit.Emit("slt", lhs, rhs, lhs);
                emit.Emit("nor", lhs, lhs, lhs);
            }
            break;

            case ">=":
            {
                emit.Emit("slt", lhs, lhs, rhs);
                emit.Emit("nor", lhs, lhs, lhs);
            }
            break;

            case "==":
            {
                emit.Emit("slt", rm.AT, lhs, rhs);
                emit.Emit("slt", lhs, rhs, lhs);
                emit.Emit("nor", lhs, rm.AT, lhs);
            }
            break;

            case "!=":
            {
                emit.Emit("slt", rm.AT, lhs, rhs);
                emit.Emit("slt", lhs, rhs, lhs);
                emit.Emit("or", lhs, rm.AT, lhs);
            }
            break;

            case "&&":
            {
                emit.Emit("and", lhs, lhs, rhs);
            }
            break;

            case "||":
            {
                emit.Emit("or", lhs, lhs, rhs);
            }
            break;

            default:
                throw new Exception($"Did not match the binop: {n.op} at {n.sourceLoc}");
            }

            rm.ReleaseRegister(rhs);

            return(lhs);
        }
Esempio n. 5
0
        public override LLVMRegister VisitBinaryExprNode(BinaryExprNode n)
        {
            LLVMRegister result = emit.GetTempRegister();
            LLVMRegister lhs    = Visit(n.lhs);
            LLVMRegister rhs    = Visit(n.rhs);

            bool   isfloat = n.Type.Equals(TypeSymbol.FLOAT_SYMBOL);
            string type    = typeToLLVMName[n.Type];
            string op      = "";
            string cmpop   = "";

            switch (n.op)
            {
            case "*":
            {
                op = isfloat ? "mul" : "mulf";
            }
            break;

            case "/":
            {
                op = isfloat ? "div" : "divf";
            }
            break;

            case "%":
            {
                op = isfloat ? "rem" : "remf";;
            }
            break;

            case "+":
            {
                op = isfloat ? "add" : "addf";
            }
            break;

            case "-":
            {
                op = isfloat ? "sub" : "subf";
            }
            break;

            case ">>":
            {
                op = "ashr";
            }
            break;

            case "<<":
            {
                op = "shl";
            }
            break;

            case "<":
            {
                op    = isfloat ? "fcmp" : "icmp";
                cmpop = isfloat ? "olt" : "slt";
            }
            break;

            case ">":
            {
                op    = isfloat ? "fcmp" : "icmp";
                cmpop = isfloat ? "ogt" : "sgt";
            }
            break;

            case "<=":
            {
                op    = isfloat ? "fcmp" : "icmp";
                cmpop = isfloat ? "ole" : "sle";
            }
            break;

            case ">=":
            {
                op    = isfloat ? "fcmp" : "icmp";
                cmpop = isfloat ? "oge" : "sge";
            }
            break;

            case "==":
            {
                op    = isfloat ? "fcmp" : "icmp";
                cmpop = isfloat ? "oeq" : "eq";
            }
            break;

            case "!=":
            {
                op    = isfloat ? "fcmp" : "icmp";
                cmpop = isfloat ? "one" : "ne";
            }
            break;

            case "&":
            {
                op = "and";
            }
            break;

            case "|":
            {
                op = "or";
            }
            break;

            case "^":
            {
                op = "xor";
            }
            break;

            case "&&":
            {
                op = "and";
            }
            break;

            case "||":
            {
                op = "or";
            }
            break;
            }

            return(result);
        }
Esempio n. 6
0
        public override object VisitBinaryExprNode(BinaryExprNode n)
        {
            Visit(n.lhs);
            Visit(n.rhs);

            switch (n.op)
            {
            case "*":
            {
                if (n.lhs.Type.Equals(TypeSymbol.FLOAT_SYMBOL) || n.rhs.Type.Equals(TypeSymbol.FLOAT_SYMBOL))
                {
                    n.Type = TypeSymbol.FLOAT_SYMBOL;
                }
                else if (n.lhs.Type.Equals(TypeSymbol.INT_SYMBOL) && n.rhs.Type.Equals(TypeSymbol.INT_SYMBOL))
                {
                    n.Type = TypeSymbol.INT_SYMBOL;
                }
            }
            break;

            case "/":
            {
                if (n.lhs.Type.Equals(TypeSymbol.FLOAT_SYMBOL) || n.rhs.Type.Equals(TypeSymbol.FLOAT_SYMBOL))
                {
                    n.Type = TypeSymbol.FLOAT_SYMBOL;
                }
                else if (n.lhs.Type.Equals(TypeSymbol.INT_SYMBOL) && n.rhs.Type.Equals(TypeSymbol.INT_SYMBOL))
                {
                    n.Type = TypeSymbol.INT_SYMBOL;
                }
            }
            break;

            case "+":
            {
                if (n.lhs.Type.Equals(TypeSymbol.FLOAT_SYMBOL) || n.rhs.Type.Equals(TypeSymbol.FLOAT_SYMBOL))
                {
                    n.Type = TypeSymbol.FLOAT_SYMBOL;
                }
                else if (n.lhs.Type.Equals(TypeSymbol.INT_SYMBOL) && n.rhs.Type.Equals(TypeSymbol.INT_SYMBOL))
                {
                    n.Type = TypeSymbol.INT_SYMBOL;
                }
            }
            break;

            case "-":
            {
                if (n.lhs.Type.Equals(TypeSymbol.FLOAT_SYMBOL) || n.rhs.Type.Equals(TypeSymbol.FLOAT_SYMBOL))
                {
                    n.Type = TypeSymbol.FLOAT_SYMBOL;
                }
                else if (n.lhs.Type.Equals(TypeSymbol.INT_SYMBOL) && n.rhs.Type.Equals(TypeSymbol.INT_SYMBOL))
                {
                    n.Type = TypeSymbol.INT_SYMBOL;
                }
            }
            break;

            case ">>":
            case "<<":
            case "&":
            case "|":
            case "^":
            case "%":
            {
                n.Type = TypeSymbol.INT_SYMBOL;
            }
            break;

            case "<":
            case ">":
            case "<=":
            case ">=":
            case "==":
            case "!=":
            case "&&":
            case "||":
            {
                n.Type = TypeSymbol.BOOL_SYMBOL;
            }
            break;
            }
            return(null);
        }
Esempio n. 7
0
            public override LData VisitBinaryExprNode(BinaryExprNode n)
            {
                LData lhs = Visit(n.lhs);
                LData rhs = Visit(n.rhs);

                LData newdat = LDataMaker.GetDataFor(n.Type);

                switch (n.op)
                {
                case "*":
                {
                    newdat.SetValue(lhs.GetValue() * rhs.GetValue());
                }
                break;

                case "/":
                {
                    newdat.SetValue(lhs.GetValue() / rhs.GetValue());
                }
                break;

                case "+":
                {
                    newdat.SetValue(lhs.GetValue() + rhs.GetValue());
                }
                break;

                case "-":
                {
                    newdat.SetValue(lhs.GetValue() - rhs.GetValue());
                }
                break;

                case ">>":
                {
                    newdat.SetValue(lhs.GetValue() >> rhs.GetValue());
                }
                break;

                case "<<":
                {
                    newdat.SetValue(lhs.GetValue() << rhs.GetValue());
                }
                break;

                case "&":
                {
                    newdat.SetValue(lhs.GetValue() & rhs.GetValue());
                }
                break;

                case "|":
                {
                    newdat.SetValue(lhs.GetValue() | rhs.GetValue());
                }
                break;

                case "^":
                {
                    newdat.SetValue(lhs.GetValue() ^ rhs.GetValue());
                }
                break;

                case "%":
                {
                    newdat.SetValue(lhs.GetValue() % rhs.GetValue());
                }
                break;

                case "<":
                {
                    newdat.SetValue(lhs.GetValue() < rhs.GetValue());
                }
                break;

                case ">":
                {
                    newdat.SetValue(lhs.GetValue() > rhs.GetValue());
                }
                break;

                case "<=":
                {
                    newdat.SetValue(lhs.GetValue() <= rhs.GetValue());
                }
                break;

                case ">=":
                {
                    newdat.SetValue(lhs.GetValue() >= rhs.GetValue());
                }
                break;

                case "==":
                {
                    newdat.SetValue(lhs.GetValue() == rhs.GetValue());
                }
                break;

                case "!=":
                {
                    newdat.SetValue(lhs.GetValue() != rhs.GetValue());
                }
                break;

                case "&&":
                {
                    newdat.SetValue(lhs.GetValue() && rhs.GetValue());
                }
                break;

                case "||":
                {
                    newdat.SetValue(lhs.GetValue() || rhs.GetValue());
                }
                break;

                default:
                    throw new Exception($"Did not match the binop: {n.op} at {n.sourceLoc}");
                }
                return(newdat);
            }