public BinOpInt(BINOP.Op op, Temp.Temp dst, Temp.Temp left, int right) { Op = op; Dst = dst; Left = left; Right = right; }
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); }
static MipsFrame() { for (int i = 0; i < 32; ++i) { Reg[i] = new Temp.Temp(); } }
public override void ReplaceUse(Temp.Temp oldTemp, Temp.Temp newTemp) { if (Left == oldTemp) Left = newTemp; if (Right == oldTemp) Right = newTemp; }
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()); }
private Node GetNodeByTemp(Temp.Temp t) { if (!TempToNode.ContainsKey(t)) { TempToNode.Add(t, new Node(t)); } return(TempToNode[t]); }
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); }
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); }
//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)); }
//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))); }
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))); }
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)))); }
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)))); }
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); }
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(); }
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++; } } }
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))); } } } }
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); }
Temp.Temp TranslateExpr(NAME expr) { Temp.Temp result = new Temp.Temp(); InstrList.Add(new MoveLabel(result, new Label(expr.Label))); return(result); }
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; }
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; }
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)); } } } }
public Node(Temp.Temp temp) { Temp = temp; }
public Move(Temp.Temp dst, Temp.Temp src) { Dst = dst; Src = src; }
public override void ReplaceUse(Temp.Temp oldTemp, Temp.Temp newTemp) { if (Left == oldTemp) Left = newTemp; }
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))); }
public abstract string TempMap(Temp.Temp temp);
public TempPairInt(Temp.Temp temp, PairInt pair) { Temp = temp; Pair = pair; }
public MoveInt(Temp.Temp dst, int src) { Dst = dst; Src = src; }
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)); }
public MoveLabel(Temp.Temp dst, Label src) { Dst = dst; Src = src; }
public Store(Temp.Temp mem, int offset, Temp.Temp src) { Mem = mem; Offset = offset; Src = src; }
public override void ReplaceDef(Temp.Temp oldTemp, Temp.Temp newTemp) { if (Dst == oldTemp) Dst = newTemp; }
public Load(Temp.Temp mem, int offset, Temp.Temp dst) { Mem = mem; Offset = offset; Dst = dst; }
public override void ReplaceUse(Temp.Temp oldTemp, Temp.Temp newTemp) { throw new FatalError("No ReplaceUse method for Call"); }
public override void ReplaceUse(Temp.Temp oldTemp, Temp.Temp newTemp) { if (Mem == oldTemp) Mem = newTemp; }
public CJumpInt(CJUMP.Rel rel, Temp.Temp left, int right, Label label) { Relop = rel; Left = left; Right = right; Label = label; }
public override void ReplaceUse(Temp.Temp oldTemp, Temp.Temp newTemp) { if (Src == oldTemp) Src = newTemp; }
Temp.Temp TranslateExpr(NAME expr) { Temp.Temp result = new Temp.Temp(); InstrList.Add(new MoveLabel(result, new Label(expr.Label))); return result; }
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; }
public override void ReplaceDef(Temp.Temp oldTemp, Temp.Temp newTemp) { throw new FatalError("No ReplaceDef method for Store"); }
public abstract void ReplaceDef(Temp.Temp oldTemp, Temp.Temp newTemp);
public override void ReplaceUse(Temp.Temp oldTemp, Temp.Temp newTemp) { if (Mem == oldTemp) Mem = newTemp; if (Src == oldTemp) Src = newTemp; }
public TEMP(Temp.Temp temp) { Temp = temp; }
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))); }