Esempio n. 1
0
        public override ABT.Expr GetExpr(ABT.Env env) {
            //Tuple<AST.Env, AST.ExprType> type_env = this.TypeName.GetTypeEnv(env);
            //env = type_env.Item1;
            //AST.ExprType Type = type_env.Item2;

            var type = Semant(this.TypeName.GetExprType, ref env);

            return new ABT.ConstULong((UInt32)type.SizeOf, env);
        }
Esempio n. 2
0
        public override ABT.Expr GetExpr(ABT.Env env) {
            ABT.Expr expr = this.Expr.GetExpr(env);

            if (!expr.Type.IsScalar) {
                throw new InvalidOperationException("Expected a scalar.");
            }

            return new ABT.PostDecrement(expr);
        }
Esempio n. 3
0
            public void Check()
            {
                ABT abt = new ABT();

                abt.thesize = 3;
                abt[2]      = "aaa";
                // Assert.IsFalse(abt.Check());
                abt[0] = "##";
                abt[1] = "##";
                Assert.IsTrue(abt.Check());
            }
Esempio n. 4
0
            public void Check()
            {
                var abt = new ABT(MemoryType);

                abt.thesize = 3;
                abt[2]      = "aaa";
                // Assert.IsFalse(abt.Check());
                abt[0] = "##";
                abt[1] = "##";
                Assert.IsTrue(abt.Check());
            }
Esempio n. 5
0
        public override ABT.Expr GetExpr(ABT.Env env) {
            switch (this.FloatSuffix) {
                case TokenFloat.FloatSuffix.F:
                    return new ABT.ConstFloat((Single)this.Value, env);

                case TokenFloat.FloatSuffix.NONE:
                case TokenFloat.FloatSuffix.L:
                    return new ABT.ConstDouble(this.Value, env);

                default:
                    throw new InvalidOperationException();
            }
        }
Esempio n. 6
0
        public override ABT.Expr GetExpr(ABT.Env env) {
            switch (this.Suffix) {
                case TokenInt.IntSuffix.U:
                case TokenInt.IntSuffix.UL:
                    return new ABT.ConstULong((UInt32)this.Value, env);

                case TokenInt.IntSuffix.NONE:
                case TokenInt.IntSuffix.L:
                    return new ABT.ConstLong((Int32)this.Value, env);

                default:
                    throw new InvalidOperationException();
            }
        }
Esempio n. 7
0
        public override ABT.Expr GetExpr(ABT.Env env) {

            // 1. semant operands
            var left = SemantExpr(this.Left, ref env);
            var right = SemantExpr(this.Right, ref env);

            // 2. perform usual arithmetic conversion
            Tuple<ABT.Expr, ABT.Expr, ABT.ExprTypeKind> castReturn = ABT.TypeCast.UsualArithmeticConversion(left, right);
            left = castReturn.Item1;
            right = castReturn.Item2;
            var typeKind = castReturn.Item3;

            var isConst = left.Type.IsConst || right.Type.IsConst;
            var isVolatile = left.Type.IsVolatile || right.Type.IsVolatile;

            // 3. if both operands are constants
            if (left.IsConstExpr && right.IsConstExpr) {
                switch (typeKind) {
                    case ABT.ExprTypeKind.DOUBLE:
                        return new ABT.ConstDouble(OperateDouble(((ABT.ConstDouble)left).Value, ((ABT.ConstDouble)right).Value), env);
                    case ABT.ExprTypeKind.FLOAT:
                        return new ABT.ConstFloat(OperateFloat(((ABT.ConstFloat)left).Value, ((ABT.ConstFloat)right).Value), env);
                    case ABT.ExprTypeKind.ULONG:
                        return new ABT.ConstULong(OperateULong(((ABT.ConstULong)left).Value, ((ABT.ConstULong)right).Value), env);
                    case ABT.ExprTypeKind.LONG:
                        return new ABT.ConstLong(OperateLong(((ABT.ConstLong)left).Value, ((ABT.ConstLong)right).Value), env);
                    default:
                        throw new InvalidOperationException("Expected arithmetic Type.");
                }
            }

            // 4. if not both operands are constants
            switch (typeKind) {
                case ABT.ExprTypeKind.DOUBLE:
                    return ConstructExpr(left, right, new ABT.DoubleType(isConst, isVolatile));
                case ABT.ExprTypeKind.FLOAT:
                    return ConstructExpr(left, right, new ABT.FloatType(isConst, isVolatile));
                case ABT.ExprTypeKind.ULONG:
                    return ConstructExpr(left, right, new ABT.ULongType(isConst, isVolatile));
                case ABT.ExprTypeKind.LONG:
                    return ConstructExpr(left, right, new ABT.LongType(isConst, isVolatile));
                default:
                    throw new InvalidOperationException("Expected arithmetic Type.");
            }

        }
Esempio n. 8
0
        public override ABT.Expr GetExpr(ABT.Env env) {
            Option<ABT.Env.Entry> entry_opt = env.Find(this.Name);

            if (entry_opt.IsNone) {
                throw new InvalidOperationException($"Cannot find variable '{this.Name}'");
            }

            ABT.Env.Entry entry = entry_opt.Value;

            switch (entry.Kind) {
                case ABT.Env.EntryKind.TYPEDEF:
                    throw new InvalidOperationException($"Expected a variable '{this.Name}', not a typedef.");
                case ABT.Env.EntryKind.ENUM:
                    return new ABT.ConstLong(entry.Offset, env);
                case ABT.Env.EntryKind.FRAME:
                case ABT.Env.EntryKind.GLOBAL:
                case ABT.Env.EntryKind.STACK:
                    return new ABT.Variable(entry.Type, this.Name, env);
                default:
                    throw new InvalidOperationException($"Cannot find variable '{this.Name}'");
            }
        }
Esempio n. 9
0
        public override Tuple<ABT.Env, ABT.Stmt> GetStmt(ABT.Env env) {
            env = env.InScope();
            List<Tuple<ABT.Env, ABT.Decln>> declns = new List<Tuple<ABT.Env, ABT.Decln>>();
            List<Tuple<ABT.Env, ABT.Stmt>> stmts = new List<Tuple<ABT.Env, ABT.Stmt>>();

            foreach (Decln decln in this.Declns) {
                //Tuple<AST.Env, List<Tuple<AST.Env, AST.Decln>>> r_decln = decln.GetDeclns_(env);
                //env = r_decln.Item1;
                //declns.AddRange(r_decln.Item2);

                var declns_ = Semant(decln.GetDeclns, ref env);
                declns.AddRange(declns_);
            }

            foreach (Stmt stmt in this.Stmts) {
                Tuple<ABT.Env, ABT.Stmt> r_stmt = stmt.GetStmt(env);
                env = r_stmt.Item1;
                stmts.Add(r_stmt);
            }

            env = env.OutScope();

            return new Tuple<ABT.Env, ABT.Stmt>(env, new ABT.CompoundStmt(declns, stmts));
        }
Esempio n. 10
0
 public override Tuple<ABT.Env, ABT.Stmt> GetStmt(ABT.Env env) {
     return new Tuple<ABT.Env, ABT.Stmt>(env, new ABT.BreakStmt());
 }
Esempio n. 11
0
        public override Tuple<ABT.Env, ABT.Stmt> GetStmt(ABT.Env env) {
            ABT.Expr expr = this.Expr.GetExpr(env);
            env = expr.Env;

            expr = ABT.TypeCast.MakeCast(expr, new ABT.LongType());
            if (!expr.IsConstExpr) {
                throw new InvalidOperationException("case Expr not const");
            }
            Int32 value = ((ABT.ConstLong)expr).Value;

            Tuple<ABT.Env, ABT.Stmt> r_stmt = this.Stmt.GetStmt(env);
            env = r_stmt.Item1;

            return new Tuple<ABT.Env, ABT.Stmt>(env, new ABT.CaseStmt(value, r_stmt.Item2));
        }
Esempio n. 12
0
        public override Tuple<ABT.Env, ABT.Stmt> GetStmt(ABT.Env env) {
            ABT.Expr cond = this.Cond.GetExpr(env);

            if (!cond.Type.IsScalar) {
                throw new InvalidOperationException("Error: expected scalar Type");
            }

            Tuple<ABT.Env, ABT.Stmt> r_true_stmt = this.TrueStmt.GetStmt(env);
            env = r_true_stmt.Item1;
            ABT.Stmt true_stmt = r_true_stmt.Item2;

            Tuple<ABT.Env, ABT.Stmt> r_false_stmt = this.FalseStmt.GetStmt(env);
            env = r_false_stmt.Item1;
            ABT.Stmt false_stmt = r_false_stmt.Item2;

            return new Tuple<ABT.Env, ABT.Stmt>(env, new ABT.IfElseStmt(cond, true_stmt, false_stmt));
        }
Esempio n. 13
0
 public override Tuple<ABT.Env, ABT.Stmt> GetStmt(ABT.Env env) {
     return new Tuple<ABT.Env, ABT.Stmt>(env, new ABT.GotoStmt(this.Label));
 }
Esempio n. 14
0
        public override Tuple<ABT.Env, ABT.Stmt> GetStmt(ABT.Env env) {
            Tuple<ABT.Env, ABT.Stmt> r_body = this.Body.GetStmt(env);
            env = r_body.Item1;
            ABT.Stmt body = r_body.Item2;

            ABT.Expr cond = this.Cond.GetExpr(env);
            env = cond.Env;

            if (!cond.Type.IsScalar) {
                throw new InvalidOperationException("Error: conditional expression in while Loop must be scalar.");
            }

            return new Tuple<ABT.Env, ABT.Stmt>(env, new ABT.DoWhileStmt(body, cond));
        }
Esempio n. 15
0
        public ABT.Expr GetPointerAddition(ABT.Expr ptr, ABT.Expr offset, Boolean order = true) {
            if (ptr.Type.Kind != ABT.ExprTypeKind.POINTER) {
                throw new InvalidOperationException();
            }
            if (offset.Type.Kind != ABT.ExprTypeKind.LONG) {
                throw new InvalidOperationException();
            }

            var env = order ? ptr.Env : offset.Env;

            if (ptr.IsConstExpr && offset.IsConstExpr) {
                var baseValue = (Int32)((ABT.ConstPtr)ptr).Value;
                Int32 scaleValue = ((ABT.PointerType)(ptr.Type)).RefType.SizeOf;
                Int32 offsetValue = ((ABT.ConstLong)offset).Value;
                return new ABT.ConstPtr((UInt32)(baseValue + scaleValue * offsetValue), ptr.Type, env);
            }

            var baseAddress = ABT.TypeCast.FromPointer(ptr, new ABT.LongType(ptr.Type.IsConst, ptr.Type.IsVolatile), ptr.Env);
            var scaleFactor = new ABT.Multiply(
                offset,
                new ABT.ConstLong(((ABT.PointerType)(ptr.Type)).RefType.SizeOf, env)
            );
            var type = new ABT.LongType(offset.Type.IsConst, offset.Type.IsVolatile);
            var add =
                order
                ? new ABT.Add(baseAddress, scaleFactor)
                : new ABT.Add(scaleFactor, baseAddress);

            return ABT.TypeCast.ToPointer(add, ptr.Type, env);
        }
Esempio n. 16
0
 public override ABT.Expr ConstructExpr(ABT.Expr left, ABT.Expr right, ABT.ExprType type) =>
     new ABT.Multiply(left, right);
Esempio n. 17
0
 public override ABT.Expr ConstructExpr(ABT.Expr left, ABT.Expr right, ABT.ExprType type) =>
     new ABT.LogicalOr(left, right);
Esempio n. 18
0
 public abstract ABT.Expr ConstructExpr(ABT.Expr left, ABT.Expr right, ABT.ExprType type);
Esempio n. 19
0
 public override Tuple<ABT.Env, ABT.Stmt> GetStmt(ABT.Env env) {
     var expr = this.Expr.Map(_ => _.GetExpr(env));
     env = expr.IsSome ? expr.Value.Env : env;
     return new Tuple<ABT.Env, ABT.Stmt>(env, new ABT.ExprStmt(expr));
 }
Esempio n. 20
0
        public override ABT.Expr GetExpr(ABT.Env env) {

            // 1. semant the operands
            var left = SemantExpr(this.Left, ref env);
            var right = SemantExpr(this.Right, ref env);

            if (left.Type is ABT.ArrayType) {
                left = ABT.TypeCast.MakeCast(left, new ABT.PointerType(((ABT.ArrayType)left.Type).ElemType, left.Type.IsConst, left.Type.IsVolatile));
            }

            if (right.Type is ABT.ArrayType) {
                right = ABT.TypeCast.MakeCast(right, new ABT.PointerType(((ABT.ArrayType)right.Type).ElemType, right.Type.IsConst, right.Type.IsVolatile));
            }

            // 2. ptr + int
            if (left.Type.Kind == ABT.ExprTypeKind.POINTER) {
                if (!right.Type.IsIntegral) {
                    throw new InvalidOperationException("Expected integral to be added to a pointer.");
                }
                right = ABT.TypeCast.MakeCast(right, new ABT.LongType(right.Type.IsConst, right.Type.IsVolatile));
                return GetPointerAddition(left, right);
            }

            // 3. int + ptr
            if (right.Type.Kind == ABT.ExprTypeKind.POINTER) {
                if (!left.Type.IsIntegral) {
                    throw new InvalidOperationException("Expected integral to be added to a pointer.");
                }
                left = ABT.TypeCast.MakeCast(left, new ABT.LongType(left.Type.IsConst, left.Type.IsVolatile));
                return GetPointerAddition(right, left, false);
            }

            // 4. usual arithmetic conversion
            return base.GetExpr(env);

        }
Esempio n. 21
0
        public override Tuple<ABT.Env, ABT.Stmt> GetStmt(ABT.Env env) {
            Option<ABT.Expr> init = this.Init.Map(_ => _.GetExpr(env));
            if (init.IsSome) {
                env = init.Value.Env;
            }

            Option<ABT.Expr> cond = this.Cond.Map(_ => _.GetExpr(env));
            if (cond.IsSome) {
                env = cond.Value.Env;
            }

            if (cond.IsSome && !cond.Value.Type.IsScalar) {
                throw new InvalidOperationException("Error: conditional expression in while Loop must be scalar.");
            }

            Option<ABT.Expr> loop = this.Loop.Map(_ => _.GetExpr(env));
            if (loop.IsSome) {
                env = loop.Value.Env;
            }

            Tuple<ABT.Env, ABT.Stmt> r_body = this.Body.GetStmt(env);
            env = r_body.Item1;
            ABT.Stmt body = r_body.Item2;

            return new Tuple<ABT.Env, ABT.Stmt>(env, new ABT.ForStmt(init, cond, loop, body));
        }
Esempio n. 22
0
        public static ABT.Expr GetPointerSubtraction(ABT.Expr ptr, ABT.Expr offset) {
            if (ptr.Type.Kind != ABT.ExprTypeKind.POINTER) {
                throw new InvalidOperationException("Error: expect a pointer");
            }
            if (offset.Type.Kind != ABT.ExprTypeKind.LONG) {
                throw new InvalidOperationException("Error: expect an integer");
            }

            if (ptr.IsConstExpr && offset.IsConstExpr) {
                Int32 baseAddressValue = (Int32)((ABT.ConstPtr)ptr).Value;
                Int32 scaleFactorValue = ((ABT.PointerType)(ptr.Type)).RefType.SizeOf;
                Int32 offsetValue = ((ABT.ConstLong)offset).Value;
                return new ABT.ConstPtr((UInt32)(baseAddressValue - scaleFactorValue * offsetValue), ptr.Type, offset.Env);
            }

            return ABT.TypeCast.ToPointer(new ABT.Sub(
                    ABT.TypeCast.FromPointer(ptr, new ABT.LongType(ptr.Type.IsConst, ptr.Type.IsVolatile), ptr.Env),
                    new ABT.Multiply(
                        offset,
                        new ABT.ConstLong(((ABT.PointerType)(ptr.Type)).RefType.SizeOf, offset.Env)
                    )
                ), ptr.Type, offset.Env
            );
        }
Esempio n. 23
0
        public override Tuple<ABT.Env, ABT.Stmt> GetStmt(ABT.Env env) {
            ABT.Expr expr = this.Expr.GetExpr(env);

            Tuple<ABT.Env, ABT.Stmt> r_stmt = this.Stmt.GetStmt(env);
            env = r_stmt.Item1;
            ABT.Stmt stmt = r_stmt.Item2;

            return new Tuple<ABT.Env, ABT.Stmt>(env, new ABT.SwitchStmt(expr, stmt));
        }
Esempio n. 24
0
        public override ABT.Expr GetExpr(ABT.Env env) {

            var left = SemantExpr(this.Left, ref env);
            var right = SemantExpr(this.Right, ref env);

            if (left.Type is ABT.ArrayType) {
                left = ABT.TypeCast.MakeCast(left, new ABT.PointerType((left.Type as ABT.ArrayType).ElemType, left.Type.IsConst, left.Type.IsVolatile));
            }

            if (right.Type is ABT.ArrayType) {
                right = ABT.TypeCast.MakeCast(right, new ABT.PointerType((right.Type as ABT.ArrayType).ElemType, right.Type.IsConst, right.Type.IsVolatile));
            }

            var isConst = left.Type.IsConst || right.Type.IsConst;
            var isVolatile = left.Type.IsVolatile || right.Type.IsVolatile;

            if (left.Type.Kind == ABT.ExprTypeKind.POINTER) {

                // 1. ptr - ptr
                if (right.Type.Kind == ABT.ExprTypeKind.POINTER) {
                    ABT.PointerType leftType = (ABT.PointerType)(left.Type);
                    ABT.PointerType rightType = (ABT.PointerType)(right.Type);
                    if (!leftType.RefType.EqualType(rightType.RefType)) {
                        throw new InvalidOperationException("The 2 pointers don't match.");
                    }

                    Int32 scale = leftType.RefType.SizeOf;

                    if (left.IsConstExpr && right.IsConstExpr) {
                        return new ABT.ConstLong((Int32)(((ABT.ConstPtr)left).Value - ((ABT.ConstPtr)right).Value) / scale, env);
                    }

                    return new ABT.Divide(
                        new ABT.Sub(
                            ABT.TypeCast.MakeCast(left, new ABT.LongType(isConst, isVolatile)),
                            ABT.TypeCast.MakeCast(right, new ABT.LongType(isConst, isVolatile))
                        ),
                        new ABT.ConstLong(scale, env)
                    );
                }

                // 2. ptr - integral
                if (!right.Type.IsIntegral) {
                    throw new InvalidOperationException("Expected an integral.");
                }
                right = ABT.TypeCast.MakeCast(right, new ABT.LongType(right.Type.IsConst, right.Type.IsVolatile));
                return GetPointerSubtraction(left, right);

            }

            // 3. arith - arith
            return base.GetExpr(env);
        }
Esempio n. 25
0
 public override Tuple<ABT.Env, ABT.Stmt> GetStmt(ABT.Env env) {
     Tuple<ABT.Env, ABT.Stmt> r_stmt = this.Stmt.GetStmt(env);
     env = r_stmt.Item1;
     return new Tuple<ABT.Env, ABT.Stmt>(env, new ABT.LabeledStmt(this.Label, r_stmt.Item2));
 }
Esempio n. 26
0
 public override ABT.Expr ConstructExpr(ABT.Expr left, ABT.Expr right, ABT.ExprType type) =>
     new ABT.RShift(left, right);
Esempio n. 27
0
 public override Tuple<ABT.Env, ABT.Stmt> GetStmt(ABT.Env env) {
     var stmt = SemantStmt(this.Stmt.GetStmt, ref env);
     return Tuple.Create(env, new ABT.DefaultStmt(stmt) as ABT.Stmt);
 }
Esempio n. 28
0
 public override ABT.Expr ConstructExpr(ABT.Expr left, ABT.Expr right, ABT.ExprType type) =>
     new ABT.Greater(left, right);
Esempio n. 29
0
 public override Tuple<ABT.Env, ABT.Stmt> GetStmt(ABT.Env env) {
     var expr = this.Expr.Map(_ => _.GetExpr(env));
     expr = expr.Map(_ => ABT.TypeCast.MakeCast(_, env.GetCurrentFunction().ReturnType));
     return new Tuple<ABT.Env, ABT.Stmt>(env, new ABT.ReturnStmt(expr));
 }
Esempio n. 30
0
 public override ABT.Expr ConstructExpr(ABT.Expr left, ABT.Expr right, ABT.ExprType type) =>
     new ABT.NotEqual(left, right);
Esempio n. 31
0
 public abstract Tuple<ABT.Env, ABT.Stmt> GetStmt(ABT.Env env);
Esempio n. 32
0
 public override ABT.Expr ConstructExpr(ABT.Expr left, ABT.Expr right, ABT.ExprType type) =>
     new ABT.BitwiseOr(left, right);