public void StartProc(Symbols.Func f) { string label = this.Label(f.GetName()); AddCode(Com.PUSH, new Addr(Reg.EBP, 4)); AddCode(Com.MOV, new Addr(Reg.EBP, 4), new Addr(Reg.ESP)); this.offset = 0; }
public void Call(Symbols.Func func) { Reg r = GetFreeReg(); AddCode(Com.POP, new Addr(r)); AddCode(Com.CALL, new Val(new Addr(r), 4)); if (!(func.GetRefType() is Symbols.VOID)) { AddCode(Com.PUSH, new Addr(Reg.EAX)); } }
public static void CheckCallFunction(Expression func, List <Expression> args) { Symbols.Type t = func.GetType(); if (!(t is Symbols.Func || (t is Symbols.POINTER && ((Symbols.POINTER)t).GetRefType() is Symbols.Func))) { throw new Symbols.Exception(func, NOT_FUNCTION); } Symbols.Func f = (Symbols.Func)(t is Symbols.Func ? t : ((Symbols.POINTER)t).GetRefType()); List <Symbols.ParamVar> f_args = f.GetArguments(); if (f_args.Count != args.Count && !f.IsUnspecifiedArgs()) { throw new Symbols.Exception(func, WRONG_NUMBER_ARGS); } for (int i = 0; i < f_args.Count; ++i) { new Cast(args.ElementAt(i), f_args.ElementAt(i).GetType()); } }
public void Import(Symbols.Var func) { Symbols.Func f = (Symbols.Func)func.GetType(); this.address.Add(func, new Addr(func.GetName())); this.imports.Add(func.GetName() + ", '" + func.GetName() + "'"); }
private Pair<Symbols.Var, Pair<Symbols.RefType, Symbols.RefType>> ParseDirectDeclarator( bool parse_abstract = false, bool parse_parameter = false) { Symbols.Var variable = null; Pair<Symbols.RefType, Symbols.RefType> tpair = null; Symbols.RefType type = null; if (scan.Peek().type == Token.Type.IDENTIFICATOR && !parse_abstract) { if (parse_parameter) { variable = new Symbols.ParamVar(scan.Read()); } else if (tstack.IsGlobal()) { variable = new Symbols.GlobalVar(scan.Read()); } else { variable = new Symbols.LocalVar(scan.Read()); } } else if (scan.Peek().type == Token.Type.LPAREN) { scan.Read(); Pair<Symbols.Var, Pair<Symbols.RefType, Symbols.RefType>> res = ParseDeclarator(parse_abstract, parse_parameter); variable = res.first; if (res.last != null) { if (tpair == null) { tpair = new Pair<Symbols.RefType, Symbols.RefType>(res.last.first, res.last.last); } else { tpair.last.SetType(res.last.first); tpair.last = res.last.last; } } CheckToken(scan.Peek(), Token.Type.RPAREN, false); if (res.last == null && res.first == null) { throw new Syntax.Exception("требуется выражение", scan.GetPos(), scan.GetLine()); } scan.Read(); } bool ret = false; while (true) { if (scan.Peek().type == Token.Type.LBRACKET) { type = new Symbols.ARRAY(scan.Read()); ((Symbols.ARRAY)type).SetSize(ParseAssignmentExpression()); CheckToken(scan.Peek(), Token.Type.RBRACKET, true); } else if (scan.Peek().type == Token.Type.LPAREN) { type = new Symbols.Func("", scan.Peek().pos, scan.Peek().line); scan.Read(); tstack.NewTable(); ((Symbols.Func)type).SetArguments(ParseParameterList()); ((Symbols.Func)type).SetTable(tstack.PopTable()); CheckToken(scan.Peek(), Token.Type.RPAREN, true); } else { ret = true; } if (type != null ) { if (tpair != null) { tpair.last.SetType(type); tpair.last = type; } else { tpair = new Pair<Symbols.RefType, Symbols.RefType>(type, type); } } type = null; if (ret) { return new Pair<Symbols.Var, Pair<Symbols.RefType, Symbols.RefType>>(variable, tpair); } } }
private Pair <Symbols.Var, Pair <Symbols.RefType, Symbols.RefType> > ParseDirectDeclarator( bool parse_abstract = false, bool parse_parameter = false) { Symbols.Var variable = null; Pair <Symbols.RefType, Symbols.RefType> tpair = null; Symbols.RefType type = null; if (scan.Peek().type == Token.Type.IDENTIFICATOR && !parse_abstract) { if (parse_parameter) { variable = new Symbols.ParamVar(scan.Read()); } else if (tstack.IsGlobal()) { variable = new Symbols.GlobalVar(scan.Read()); } else { variable = new Symbols.LocalVar(scan.Read()); } } else if (scan.Peek().type == Token.Type.LPAREN) { scan.Read(); Pair <Symbols.Var, Pair <Symbols.RefType, Symbols.RefType> > res = ParseDeclarator(parse_abstract, parse_parameter); variable = res.first; if (res.last != null) { if (tpair == null) { tpair = new Pair <Symbols.RefType, Symbols.RefType>(res.last.first, res.last.last); } else { tpair.last.SetType(res.last.first); tpair.last = res.last.last; } } CheckToken(scan.Peek(), Token.Type.RPAREN, false); if (res.last == null && res.first == null) { throw new Syntax.Exception("требуется выражение", scan.GetPos(), scan.GetLine()); } scan.Read(); } bool ret = false; while (true) { if (scan.Peek().type == Token.Type.LBRACKET) { type = new Symbols.ARRAY(scan.Read()); ((Symbols.ARRAY)type).SetSize(ParseAssignmentExpression()); CheckToken(scan.Peek(), Token.Type.RBRACKET, true); } else if (scan.Peek().type == Token.Type.LPAREN) { type = new Symbols.Func("", scan.Peek().pos, scan.Peek().line); scan.Read(); tstack.NewTable(); ((Symbols.Func)type).SetArguments(ParseParameterList()); ((Symbols.Func)type).SetTable(tstack.PopTable()); CheckToken(scan.Peek(), Token.Type.RPAREN, true); } else { ret = true; } if (type != null) { if (tpair != null) { tpair.last.SetType(type); tpair.last = type; } else { tpair = new Pair <Symbols.RefType, Symbols.RefType>(type, type); } } type = null; if (ret) { return(new Pair <Symbols.Var, Pair <Symbols.RefType, Symbols.RefType> >(variable, tpair)); } } }