예제 #1
0
 public BinOpInt(BINOP.Op op, Temp.Temp dst, Temp.Temp left, int right)
 {
     Op = op;
     Dst = dst;
     Left = left;
     Right = right;
 }
예제 #2
0
            public override Stm ProcessEntryExit1(Stm body)
            {
                Temp.Temp newTemp;
                for (int i = 0; i < 8; ++i)
                {
                    newTemp = new Temp.Temp();
                    body    = new SEQ(new MOVE(new TEMP(newTemp), new TEMP(Reg[16 + i])), body);
                    body    = new SEQ(body, new MOVE(new TEMP(Reg[16 + i]), new TEMP(newTemp)));
                }
                newTemp = new Temp.Temp();
                body    = new SEQ(new MOVE(new TEMP(newTemp), new TEMP(Reg[31])), body);
                body    = new SEQ(body, new MOVE(new TEMP(Reg[31]), new TEMP(newTemp)));

                int count = 0;

                for (AccessList ptr = Formals; ptr != null; ptr = ptr.Tail)
                {
                    if (ptr.Head is InReg)
                    {
                        body = new SEQ(new MOVE(ptr.Head.Exp(null), new TEMP(A(count))), body);
                        ++count;
                    }
                }
                for (; count < 4; count++)
                {
                    body = new SEQ(new MOVE(new TEMP(new Temp.Temp()), new TEMP(A(count))), body);
                }
                return(body);
            }
예제 #3
0
 static MipsFrame()
 {
     for (int i = 0; i < 32; ++i)
     {
         Reg[i] = new Temp.Temp();
     }
 }
예제 #4
0
 public override void ReplaceUse(Temp.Temp oldTemp, Temp.Temp newTemp)
 {
     if (Left == oldTemp)
         Left = newTemp;
     if (Right == oldTemp)
         Right = newTemp;
 }
예제 #5
0
            Temp.Temp TranslateExpr(CALL expr)
            {
                Call c = new Call();

                c.Name = new Label((expr.Func as NAME).Label);
                TempList args = null, ptr = null;

                for (ExpList exp = expr.Args; exp != null; exp = exp.Tail)
                {
                    Temp.Temp arg = TranslateExpr(exp.Head);
                    if (args == null)
                    {
                        ptr = args = new TempList(arg, null);
                    }
                    else
                    {
                        ptr = ptr.Tail = new TempList(arg, null);
                    }
                }
                c.Param = args;
                MipsFrame t = null;

                foreach (MipsFrame f in MipsFrame.AllFrames)
                {
                    if (c.Name.Lab == f.Name)
                    {
                        t = f;
                        break;
                    }
                }
                if (t == null)
                {
                    int count = 0;
                    for (ptr = c.Param; ptr != null; ptr = ptr.Tail)
                    {
                        InstrList.Add(new Move(Frame.A(count), ptr.Head));
                        ++count;
                    }
                }
                else
                {
                    int count = 0;
                    ptr = c.Param;
                    for (AccessList al = t.Formals; al != null; al = al.Tail, ptr = ptr.Tail)
                    {
                        if (al.Head is InReg)
                        {
                            InstrList.Add(new Move(t.A(count), ptr.Head));
                            ++count;
                        }
                        else
                        {
                            InstrList.Add(new Store(t.SP(), (al.Head as InFrame).Offset, ptr.Head));
                        }
                    }
                }
                InstrList.Add(c);
                return(Frame.RV());
            }
예제 #6
0
 private Node GetNodeByTemp(Temp.Temp t)
 {
     if (!TempToNode.ContainsKey(t))
     {
         TempToNode.Add(t, new Node(t));
     }
     return(TempToNode[t]);
 }
예제 #7
0
 Temp.Temp TranslateExpr(CONST expr)
 {
     if (expr.Value == 0)
     {
         return(MipsFrame.Reg[0]);
     }
     Temp.Temp result = new Temp.Temp();
     InstrList.Add(new MoveInt(result, expr.Value));
     return(result);
 }
예제 #8
0
            private string GetColor(Temp.Temp t)
            {
                string[] map = { "zero", "at", "v0", "v1",
                                 "a0",   "a1", "a2", "a3","t0",  "t1",
                                 "t2",   "t3", "t4", "t5","t6",  "t7",
                                 "s0",   "s1", "s2", "s3","s4",  "s5",
                                 "s6",   "s7", "t8", "t9","k0",  "k1",
                                 "gp",   "sp", "fp", "ra" };
                string   res = "$" + map[TempToNode[t].Color];

                return(res);
            }
예제 #9
0
 //public abstract Stm UnCx(Label t, Label f);
 public override Expr UnEx()
 {
     Temp.Temp r = new Temp.Temp();
     Label t = new Label();
     Label f = new Label();
     return new ESEQ(
             new SEQ(new MOVE(new TEMP(r), new CONST(1)),
                     new SEQ(UnCx(t, f),
                         new SEQ(new LABEL(f),
                             new SEQ(new MOVE(new TEMP(r), new CONST(0)),
                                 new LABEL(t))))),
             new TEMP(r));
 }
예제 #10
0
            //public abstract Stm UnCx(Label t, Label f);

            public override Expr UnEx()
            {
                Temp.Temp r = new Temp.Temp();
                Label     t = new Label();
                Label     f = new Label();

                return(new ESEQ(
                           new SEQ(new MOVE(new TEMP(r), new CONST(1)),
                                   new SEQ(UnCx(t, f),
                                           new SEQ(new LABEL(f),
                                                   new SEQ(new MOVE(new TEMP(r), new CONST(0)),
                                                           new LABEL(t))))),
                           new TEMP(r)));
            }
예제 #11
0
            public override Expr UnEx()
            {
                Temp.Temp r    = new Temp.Temp();
                Label     join = new Label();
                Label     t    = new Label();
                Label     f    = new Label();

                return(new ESEQ(new SEQ(Test.UnCx(t, f),
                                        new SEQ(new LABEL(t),
                                                new SEQ(new MOVE(new TEMP(r), Then.UnEx()),
                                                        new SEQ(new JUMP(join),
                                                                new SEQ(new LABEL(f),
                                                                        new SEQ(new MOVE(new TEMP(r), Else.UnEx()),
                                                                                new LABEL(join))))))),
                                new TEMP(r)));
            }
예제 #12
0
            public Exp TranslateRecordExp(Level home, List <Exp> field)
            {
                Temp.Temp addr  = new Temp.Temp();
                Expr      alloc = home.Frame.ExternalCall("_record",
                                                          new Tree.ExpList(new CONST(
                                                                               (field.Count == 0 ? 1 : field.Count) * home.Frame.WordSize()), null));
                Stm init = new EXP(new CONST(0));

                for (int i = field.Count - 1; i >= 0; --i)
                {
                    Expr offset = new BINOP(BINOP.Op.Plus, new TEMP(addr), new CONST(i * home.Frame.WordSize()));
                    Expr v      = field[i].UnEx();
                    init = new SEQ(new MOVE(new MEM(offset), v), init);
                }
                return(new Ex(new ESEQ(new SEQ(new MOVE(new TEMP(addr), alloc), init), new TEMP(addr))));
            }
예제 #13
0
            public Exp TranslateMultiArrayExp(Level home, Exp init, Exp size)
            {
                Expr alloc = home.Frame.ExternalCall("_malloc",
                                                     new Tree.ExpList(
                                                         new BINOP(BINOP.Op.Times, size.UnEx(), new CONST(Frame.WordSize())),
                                                         null));

                Temp.Temp addr    = new Temp.Temp();
                Access    var     = home.AllocLocal(false);
                Stm       initial = (new ForExp(home, var, new Ex(new CONST(0)),
                                                new Ex(new BINOP(BINOP.Op.Minus, size.UnEx(), new CONST(1))),
                                                new Nx(new MOVE(new MEM(new BINOP(BINOP.Op.Plus, new TEMP(addr), new BINOP(BINOP.Op.Times, var.Acc.Exp(null), new CONST(Frame.WordSize())))),
                                                                init.UnEx())),
                                                new Label())).UnNx();

                return(new Ex(new ESEQ(new SEQ(new MOVE(new TEMP(addr), alloc), initial), new TEMP(addr))));
            }
예제 #14
0
            Temp.Temp TranslateExpr(MEM expr)
            {
                Temp.Temp result = new Temp.Temp();
                Temp.Temp mem;
                int       offset;

                if (expr.Exp is BINOP && (expr.Exp as BINOP).Right is CONST)
                {
                    mem    = TranslateExpr((expr.Exp as BINOP).Left);
                    offset = ((expr.Exp as BINOP).Right as CONST).Value;
                }
                else
                {
                    mem    = TranslateExpr(expr.Exp);
                    offset = 0;
                }
                InstrList.Add(new Load(mem, offset, result));
                return(result);
            }
예제 #15
0
            private void RewriteProgram(HashSet <Node> spilledNodes)
            {
                HashSet <Node> newTemps = new HashSet <Node>();

                foreach (Node v in spilledNodes)
                {
                    InFrame a = (InFrame)Frame.AllocLocal(true);
                    foreach (BasicBlock b in Blocks)
                    {
                        for (int i = 0; i < b.List.Count; i++)
                        {
                            TExp inst = b.List[i];
                            if (inst.LivenessNode == null)
                            {
                                continue;
                            }
                            if (inst.LivenessNode.Use.Contains(v.Temp))
                            {
                                Temp.Temp p = new Temp.Temp();
                                newTemps.Add(GetNodeByTemp(p));
                                GetNodeByTemp(p).IsNew = true;
                                b.List.Insert(i, new Load(Frame.FP(), a.Offset, p));
                                b.List[++i].ReplaceUse(v.Temp, p);
                            }
                            if (inst.LivenessNode.Def.Contains(v.Temp))
                            {
                                Temp.Temp p = new Temp.Temp();
                                newTemps.Add(GetNodeByTemp(p));
                                GetNodeByTemp(p).IsNew = true;
                                b.List.Insert(i + 1, new Store(Frame.FP(), a.Offset, p));
                                b.List[i++].ReplaceDef(v.Temp, p);
                            }
                        }
                    }
                }
                spilledNodes.Clear();
                Initial = newTemps;
                Initial.UnionWith(ColoredNodes);
                Initial.UnionWith(CoalescedNodes);
                ColoredNodes.Clear();
                CoalescedNodes.Clear();
            }
예제 #16
0
            private void AddEdge(Temp.Temp u, Temp.Temp v)
            {
                Node uu = GetNodeByTemp(u), vv = GetNodeByTemp(v);

                if (u != v && !AdjSet.Contains(new Edge(uu, vv)))
                {
                    AdjSet.Add(new Edge(uu, vv));
                    AdjSet.Add(new Edge(vv, uu));
                    if (!Precolored.Contains(uu))
                    {
                        uu.AdjList.Add(vv);
                        uu.Degree++;
                    }
                    if (!Precolored.Contains(vv))
                    {
                        vv.AdjList.Add(uu);
                        vv.Degree++;
                    }
                }
            }
예제 #17
0
 public static StmExpList Reorder(Tree.ExpList exps)
 {
     if (exps == null)
     {
         return(NopNull);
     }
     else
     {
         Tree.Expr a = exps.Head;
         if (a is Tree.CALL)
         {
             Temp.Temp t = new Temp.Temp();
             Tree.Expr e = new Tree.ESEQ(new Tree.MOVE(new Tree.TEMP(t), a),
                                         new Tree.TEMP(t));
             return(Reorder(new Tree.ExpList(e, exps.Tail)));
         }
         else
         {
             Tree.ESEQ  aa = DoExp(a);
             StmExpList bb = Reorder(exps.Tail);
             if (Commute(bb.Stm, aa.Exp))
             {
                 return(new StmExpList(Seq(aa.Stm, bb.Stm),
                                       new Tree.ExpList(aa.Exp, bb.Exps)));
             }
             else
             {
                 Temp.Temp t = new Temp.Temp();
                 return(new StmExpList(
                            Seq(aa.Stm,
                                Seq(new Tree.MOVE(new Tree.TEMP(t), aa.Exp),
                                    bb.Stm)),
                            new Tree.ExpList(new Tree.TEMP(t), bb.Exps)));
             }
         }
     }
 }
예제 #18
0
            Temp.Temp TranslateExpr(BINOP expr)
            {
                Temp.Temp result = new Temp.Temp();
                if (expr.Right is CONST)
                {
                    InstrList.Add(new BinOpInt(expr.Binop, result, TranslateExpr(expr.Left), (expr.Right as CONST).Value));
                }
                else if (expr.Left is CONST)
                {
                    switch (expr.Binop)
                    {
                    case BINOP.Op.Plus:
                    case BINOP.Op.Times:
                        InstrList.Add(new BinOpInt(expr.Binop, result, TranslateExpr(expr.Right), (expr.Left as CONST).Value));
                        break;

                    case BINOP.Op.Minus:
                        InstrList.Add(new BinOpInt(expr.Binop, result, TranslateExpr(expr.Right), (expr.Left as CONST).Value));
                        InstrList.Add(new BinOp(expr.Binop, result, MipsFrame.Reg[0], result));
                        break;

                    case BINOP.Op.Divide:
                        InstrList.Add(new BinOp(expr.Binop, result, TranslateExpr(expr.Right), TranslateExpr(expr.Left)));
                        break;

                    default:
                        throw new FatalError("Error in Quadruple: TranslateExpr(BINOP)");
                        break;
                    }
                }
                else
                {
                    InstrList.Add(new BinOp(expr.Binop, result, TranslateExpr(expr.Left), TranslateExpr(expr.Right)));
                }
                return(result);
            }
예제 #19
0
 Temp.Temp TranslateExpr(NAME expr)
 {
     Temp.Temp result = new Temp.Temp();
     InstrList.Add(new MoveLabel(result, new Label(expr.Label)));
     return(result);
 }
예제 #20
0
 Temp.Temp TranslateExpr(CONST expr)
 {
     if (expr.Value == 0)
         return MipsFrame.Reg[0];
     Temp.Temp result = new Temp.Temp();
     InstrList.Add(new MoveInt(result, expr.Value));
     return result;
 }
예제 #21
0
 Temp.Temp TranslateExpr(MEM expr)
 {
     Temp.Temp result = new Temp.Temp();
     Temp.Temp mem;
     int offset;
     if (expr.Exp is BINOP && (expr.Exp as BINOP).Right is CONST)
     {
         mem = TranslateExpr((expr.Exp as BINOP).Left);
         offset = ((expr.Exp as BINOP).Right as CONST).Value;
     }
     else
     {
         mem = TranslateExpr(expr.Exp);
         offset = 0;
     }
     InstrList.Add(new Load(mem, offset, result));
     return result;
 }
예제 #22
0
파일: Canon.cs 프로젝트: Nxun/Naive-Tiger
 public static StmExpList Reorder(Tree.ExpList exps)
 {
     if (exps == null) return NopNull;
     else
     {
         Tree.Expr a = exps.Head;
         if (a is Tree.CALL)
         {
             Temp.Temp t = new Temp.Temp();
             Tree.Expr e = new Tree.ESEQ(new Tree.MOVE(new Tree.TEMP(t), a),
                            new Tree.TEMP(t));
             return Reorder(new Tree.ExpList(e, exps.Tail));
         }
         else
         {
             Tree.ESEQ aa = DoExp(a);
             StmExpList bb = Reorder(exps.Tail);
             if (Commute(bb.Stm, aa.Exp))
                 return new StmExpList(Seq(aa.Stm, bb.Stm),
                           new Tree.ExpList(aa.Exp, bb.Exps));
             else
             {
                 Temp.Temp t = new Temp.Temp();
                 return new StmExpList(
                        Seq(aa.Stm,
                          Seq(new Tree.MOVE(new Tree.TEMP(t), aa.Exp),
                           bb.Stm)),
                        new Tree.ExpList(new Tree.TEMP(t), bb.Exps));
             }
         }
     }
 }
예제 #23
0
 public Node(Temp.Temp temp)
 {
     Temp = temp;
 }
예제 #24
0
 public Move(Temp.Temp dst, Temp.Temp src)
 {
     Dst = dst;
     Src = src;
 }
예제 #25
0
 public override void ReplaceUse(Temp.Temp oldTemp, Temp.Temp newTemp)
 {
     if (Left == oldTemp)
         Left = newTemp;
 }
예제 #26
0
 public Exp TranslateRecordExp(Level home, List<Exp> field)
 {
     Temp.Temp addr = new Temp.Temp();
     Expr alloc = home.Frame.ExternalCall("_record",
         new Tree.ExpList(new CONST(
             (field.Count == 0 ? 1 : field.Count) * home.Frame.WordSize()), null));
     Stm init = new EXP(new CONST(0));
     for (int i = field.Count - 1; i >= 0; --i)
     {
         Expr offset = new BINOP(BINOP.Op.Plus, new TEMP(addr), new CONST(i * home.Frame.WordSize()));
         Expr v = field[i].UnEx();
         init = new SEQ(new MOVE(new MEM(offset), v), init);
     }
     return new Ex(new ESEQ(new SEQ(new MOVE(new TEMP(addr), alloc), init), new TEMP(addr)));
 }
예제 #27
0
 public abstract string TempMap(Temp.Temp temp);
예제 #28
0
 public TempPairInt(Temp.Temp temp, PairInt pair)
 {
     Temp = temp;
     Pair = pair;
 }
예제 #29
0
 public MoveInt(Temp.Temp dst, int src)
 {
     Dst = dst;
     Src = src;
 }
예제 #30
0
 public override Expr UnEx()
 {
     Temp.Temp r = new Temp.Temp();
     Label join = new Label();
     Label t = new Label();
     Label f = new Label();
     return new ESEQ(new SEQ(Test.UnCx(t, f),
             new SEQ(new LABEL(t),
                 new SEQ(new MOVE(new TEMP(r), Then.UnEx()),
                     new SEQ(new JUMP(join),
                         new SEQ(new LABEL(f),
                             new SEQ(new MOVE(new TEMP(r), Else.UnEx()),
                                 new LABEL(join))))))),
                                 new TEMP(r));
 }
예제 #31
0
 public MoveLabel(Temp.Temp dst, Label src)
 {
     Dst = dst;
     Src = src;
 }
예제 #32
0
 public Store(Temp.Temp mem, int offset, Temp.Temp src)
 {
     Mem = mem;
     Offset = offset;
     Src = src;
 }
예제 #33
0
 public override void ReplaceDef(Temp.Temp oldTemp, Temp.Temp newTemp)
 {
     if (Dst == oldTemp)
         Dst = newTemp;
 }
예제 #34
0
 public override void ReplaceDef(Temp.Temp oldTemp, Temp.Temp newTemp)
 {
     if (Dst == oldTemp)
         Dst = newTemp;
 }
예제 #35
0
 public Load(Temp.Temp mem, int offset, Temp.Temp dst)
 {
     Mem = mem;
     Offset = offset;
     Dst = dst;
 }
예제 #36
0
 public override void ReplaceUse(Temp.Temp oldTemp, Temp.Temp newTemp)
 {
     throw new FatalError("No ReplaceUse method for Call");
 }
예제 #37
0
 public override void ReplaceUse(Temp.Temp oldTemp, Temp.Temp newTemp)
 {
     if (Mem == oldTemp)
         Mem = newTemp;
 }
예제 #38
0
 public Load(Temp.Temp mem, int offset, Temp.Temp dst)
 {
     Mem = mem;
     Offset = offset;
     Dst = dst;
 }
예제 #39
0
 public Move(Temp.Temp dst, Temp.Temp src)
 {
     Dst = dst;
     Src = src;
 }
예제 #40
0
 public CJumpInt(CJUMP.Rel rel, Temp.Temp left, int right, Label label)
 {
     Relop = rel;
     Left = left;
     Right = right;
     Label = label;
 }
예제 #41
0
 public override void ReplaceUse(Temp.Temp oldTemp, Temp.Temp newTemp)
 {
     if (Src == oldTemp)
         Src = newTemp;
 }
예제 #42
0
 Temp.Temp TranslateExpr(NAME expr)
 {
     Temp.Temp result = new Temp.Temp();
     InstrList.Add(new MoveLabel(result, new Label(expr.Label)));
     return result;
 }
예제 #43
0
 public MoveInt(Temp.Temp dst, int src)
 {
     Dst = dst;
     Src = src;
 }
예제 #44
0
 public MoveLabel(Temp.Temp dst, Label src)
 {
     Dst = dst;
     Src = src;
 }
예제 #45
0
 public Store(Temp.Temp mem, int offset, Temp.Temp src)
 {
     Mem = mem;
     Offset = offset;
     Src = src;
 }
예제 #46
0
 Temp.Temp TranslateExpr(BINOP expr)
 {
     Temp.Temp result = new Temp.Temp();
     if (expr.Right is CONST)
         InstrList.Add(new BinOpInt(expr.Binop, result, TranslateExpr(expr.Left), (expr.Right as CONST).Value));
     else if (expr.Left is CONST)
     {
         switch (expr.Binop)
         {
             case BINOP.Op.Plus:
             case BINOP.Op.Times:
                 InstrList.Add(new BinOpInt(expr.Binop, result, TranslateExpr(expr.Right), (expr.Left as CONST).Value));
                 break;
             case BINOP.Op.Minus:
                 InstrList.Add(new BinOpInt(expr.Binop, result, TranslateExpr(expr.Right), (expr.Left as CONST).Value));
                 InstrList.Add(new BinOp(expr.Binop, result, MipsFrame.Reg[0], result));
                 break;
             case BINOP.Op.Divide:
                 InstrList.Add(new BinOp(expr.Binop, result, TranslateExpr(expr.Right), TranslateExpr(expr.Left)));
                 break;
             default:
                 throw new FatalError("Error in Quadruple: TranslateExpr(BINOP)");
                 break;
         }
     }
     else
         InstrList.Add(new BinOp(expr.Binop, result, TranslateExpr(expr.Left), TranslateExpr(expr.Right)));
     return result;
 }
예제 #47
0
 public override void ReplaceDef(Temp.Temp oldTemp, Temp.Temp newTemp)
 {
     throw new FatalError("No ReplaceDef method for Store");
 }
예제 #48
0
 public abstract void ReplaceDef(Temp.Temp oldTemp, Temp.Temp newTemp);
예제 #49
0
 public override void ReplaceUse(Temp.Temp oldTemp, Temp.Temp newTemp)
 {
     if (Mem == oldTemp)
         Mem = newTemp;
     if (Src == oldTemp)
         Src = newTemp;
 }
예제 #50
0
파일: Tree.cs 프로젝트: Nxun/Naive-Tiger
 public TEMP(Temp.Temp temp)
 {
     Temp = temp;
 }
예제 #51
0
 public Exp TranslateMultiArrayExp(Level home, Exp init, Exp size)
 {
     Expr alloc = home.Frame.ExternalCall("_malloc",
         new Tree.ExpList(
             new BINOP(BINOP.Op.Times, size.UnEx(), new CONST(Frame.WordSize())),
             null));
     Temp.Temp addr = new Temp.Temp();
     Access var = home.AllocLocal(false);
     Stm initial = (new ForExp(home, var, new Ex(new CONST(0)),
         new Ex(new BINOP(BINOP.Op.Minus, size.UnEx(), new CONST(1))),
         new Nx(new MOVE(new MEM(new BINOP(BINOP.Op.Plus, new TEMP(addr), new BINOP(BINOP.Op.Times, var.Acc.Exp(null), new CONST(Frame.WordSize())))),
             init.UnEx())),
             new Label())).UnNx();
     return new Ex(new ESEQ(new SEQ(new MOVE(new TEMP(addr), alloc), initial), new TEMP(addr)));
 }