public int CreateTable() { int id = m_symbolTables.Count; Symtab table = new Symtab(id); m_symbolTables.Add(table); return id; }
public static IEnumerable <iterassgn> CharUpdates(itercase ic, Symtab stab) { foreach (iterassgn ia in assign_filter.Apply(ic)) { if (stab.Get(ia.lhs).type == BekTypes.CHAR) { yield return(ia); } } }
public static IEnumerable <iterassgn> InitialChars(iterexpr e, Symtab stab) { foreach (iterassgn ia in assign_filter.Apply(e.initializer)) { if (stab.Get(ia.lhs).type == BekTypes.CHAR) { yield return(ia); } } }
public SymtabStack(Symtab globalSymbolTable) { SharedProperties.CurrentNestingLevel = 0; for (int i = 1; i < maxNestingLevel; ++i) pSymtabs[i] = null; // -- Initialize the global nesting level. pSymtabs[0] = globalSymbolTable; TType.InitializePredefinedTypes(globalSymbolTable); InitializeStandardRoutines(globalSymbolTable); }
public CodeGeneration(Context ctx) { ctx.put(CONTEXT_KEY, this); typings = Typings.instance(ctx); typeResolver = new LLVMTypeResolver(); options = CommandLineOptions.instance(ctx); symtab = Symtab.instance(ctx); log = Log.instance(ctx); targetOS = getTargetOS(); }
//internal RaiseRule<Expr> haltRule; public VarInfo(STBuilderZ3 stb, Symtab stab, iterexpr ie, Sort charsort) { this.stab = stab; this.charsort = charsort; this.stb = stb; this.binder = ie.binder; binderid = stab.Get(ie.binder).id; bekVarIds = new List <int>(); bekVarVals = new List <Expr>(); bekVarSorts = new List <Sort>(); proj = new Dictionary <int, int>(); int regPosNr = 0; foreach (iterassgn ia in IterInfo.Initializers(ie, stab)) { proj[stab.Get(ia.lhs).id] = regPosNr; bekVarIds.Add(stab.Get(ia.lhs).id); bekVarVals.Add(MkExpr(stab.Get(ia.lhs).type, ia.rhs)); bekVarSorts.Add(BekTypeToSort(stab.Get(ia.lhs).type)); regPosNr += 1; } K = bekVarSorts.Count; //register sort regSort = (K == 0 ? stb.Solver.UnitSort : (K == 1 ? bekVarSorts[0] : stb.Solver.MkTupleSort(bekVarSorts.ToArray()))); //initial register value initReg = (K == 0 ? stb.Solver.UnitConst : (K == 1 ? bekVarVals[0] : stb.Solver.MkTuple(bekVarVals.ToArray()))); //input character variable c = this.stb.MkInputVariable(charsort); //register variable r = this.stb.MkRegister(regSort); //maps variable identifiers used in the bek program to corresponding term variables varMap = new Dictionary <int, Expr>(); varMap[binderid] = c; //input character variable for (int i = 0; i < K; i++) //bek pgm variables { varMap[bekVarIds[i]] = (K == 1 ? r : stb.Solver.MkProj(i, r)); } //haltRule = new RaiseRule<Expr>(); }
public override STModel Convert(iterexpr ie, Symtab stab) { VarInfo I = new VarInfo(stb, stab, ie, charsort); var iterCases = ConsList <itercase> .Create(ie.GetNormalCases()); var endCases = ConsList <itercase> .Create(ie.GetEndCases()); BranchingRule <Expr> rule = CreateBranchingRule(iterCases, stb.Solver.True, I); BranchingRule <Expr> frule = CreateBranchingRule(endCases, stb.Solver.True, I); STModel st = new STModel(stb.Solver, "iterSTb", charsort, charsort, I.regSort, I.initReg, 0); st.AssignRule(0, rule); st.AssignFinalRule(0, frule); return(st); }
public STModel Convert(expr e, Func <ident, STModel> strexprmapping, Symtab stab) { this.strexprmapping = strexprmapping; this.stab = stab; return(router.Visit(this, e, false)); }
public override STModel Convert(iterexpr ie, Symtab stab) { var solver = stb.Solver; int binderid = stab.Get(ie.binder).id; List <int> bekVarIds = new List <int>(); List <Expr> bekVarVals = new List <Expr>(); List <Sort> bekVarSorts = new List <Sort>(); Dictionary <int, int> proj = new Dictionary <int, int>(); foreach (iterassgn ia in IterInfo.Initializers(ie, stab)) { proj[stab.Get(ia.lhs).id] = bekVarIds.Count; bekVarIds.Add(stab.Get(ia.lhs).id); bekVarVals.Add(MkExpr(stab.Get(ia.lhs).type, ia.rhs)); bekVarSorts.Add(BekTypeToSort(stab.Get(ia.lhs).type)); } int K = bekVarSorts.Count; //register sort Sort regSort = (K == 0 ? solver.UnitSort : (K == 1 ? bekVarSorts[0] : solver.MkTupleSort(bekVarSorts.ToArray()))); //initial register value Expr initReg = (K == 0 ? solver.UnitConst : (K == 1 ? bekVarVals[0] : solver.MkTuple(bekVarVals.ToArray()))); //input character variable Expr c = this.stb.MkInputVariable(charsort); //register variable Expr r = this.stb.MkRegister(regSort); //maps variable identifiers used in the bek program to corresponding term variables Dictionary <int, Expr> varMap = new Dictionary <int, Expr>(); varMap[binderid] = c; for (int i = 0; i < K; i++) { varMap[bekVarIds[i]] = (K == 1 ? r : solver.MkProj(i, r)); } List <Move <Rulez3> > moves = new List <Move <Rulez3> >(); Expr previousCaseNegated = solver.True; foreach (itercase curcase in ie.GetNormalCases()) { if (!solver.IsSatisfiable(previousCaseNegated)) { break; } //initial symbolic values are the previous register values Expr[] regs0 = new Expr[K]; for (int i = 0; i < K; i++) { regs0[i] = varMap[bekVarIds[i]]; } Expr[] regs = new Expr[K]; for (int i = 0; i < K; i++) { regs[i] = regs0[i]; } //gets the current symbolic value of ident Func <ident, Expr> idents = x => { SymtabElt se = stab.Get(x); if (se.id == binderid) { return(c); //the input character } return(regs0[proj[se.id]]); //the current sybolic value of x }; //current condition is the case condition and not the previous case conditions Expr casecond = this.expr_handler.Convert(curcase.cond, idents); Expr guard = stb.And(previousCaseNegated, casecond); Expr not_casecond = stb.Not(casecond); previousCaseNegated = stb.And(previousCaseNegated, solver.MkNot(casecond)); List <Expr> yields = new List <Expr>(); if (solver.IsSatisfiable(guard)) { #region iterate over the iter statements in the body foreach (iterstmt ist in curcase.body) { //gets the current symbolic value of ident //note that the symbolic value may have been updated by a previous assignment Func <ident, Expr> idents1 = x => { SymtabElt se = stab.Get(x); if (se.id == binderid) { return(c); //the input character } return(regs0[proj[se.id]]); //the current sybolic value of x }; iterassgn a = ist as iterassgn; if (a != null) { var v = expr_handler.Convert(a.rhs, idents1); regs[proj[stab.Get(a.lhs).id]] = v; } else { yieldstmt y = ist as yieldstmt; if (y != null) { foreach (var e in y.args) { strconst s = e as strconst; if (s == null) { yields.Add(expr_handler.Convert(e, idents1)); } else { foreach (int sc in s.content) { yields.Add(solver.MkNumeral(sc, charsort)); } } } } else { throw new BekException(); //TBD: undefined case } } } #endregion Expr upd = (K == 0 ? solver.UnitConst : (K == 1 ? regs[0] : solver.MkTuple(regs))); moves.Add(stb.MkRule(0, 0, guard, upd, yields.ToArray())); } } previousCaseNegated = solver.True; bool noEndCases = true; foreach (itercase curcase in ie.GetEndCases()) { noEndCases = false; if (!solver.IsSatisfiable(previousCaseNegated)) { break; } //initial symbolic values are the previous register values Expr[] regs = new Expr[K]; for (int i = 0; i < K; i++) { regs[i] = varMap[bekVarIds[i]]; } //gets the current symbolic value of ident Func <ident, Expr> idents = x => { SymtabElt se = stab.Get(x); if (se.id == binderid) { throw new BekException("Input var must not occur in an end case"); } return(regs[proj[se.id]]); //the current sybolic value of x }; //current condition is the case condition and not the previous case conditions Expr casecond = this.expr_handler.Convert(curcase.cond, idents); Expr guard = stb.And(previousCaseNegated, casecond); Expr not_casecond = stb.Not(casecond); previousCaseNegated = stb.And(previousCaseNegated, solver.MkNot(casecond)); List <Expr> yields = new List <Expr>(); if (solver.IsSatisfiable(guard)) { #region iterate over the iter statements in the body foreach (iterstmt ist in curcase.body) { //gets the current symbolic value of ident //note that the symbolic value may have been updated by a previous assignment Func <ident, Expr> idents1 = x => { SymtabElt se = stab.Get(x); if (se.id == binderid) { throw new BekException("Input var must not occur in an end case"); } return(regs[proj[se.id]]); //the current sybolic value of x }; iterassgn a = ist as iterassgn; if (a != null) { var v = expr_handler.Convert(a.rhs, idents1); regs[proj[stab.Get(a.lhs).id]] = v; } else { yieldstmt y = ist as yieldstmt; if (y != null) { foreach (var e in y.args) { strconst s = e as strconst; if (s == null) { yields.Add(expr_handler.Convert(e, idents1)); } else { foreach (int sc in s.content) { yields.Add(solver.MkNumeral(sc, charsort)); } } } } else { throw new BekException(); //TBD: undefined case } } } #endregion moves.Add(stb.MkFinalOutput(0, guard, yields.ToArray())); } } //if no end cases were given, assume default true end case with empty yield if (noEndCases) { moves.Add(stb.MkFinalOutput(0, solver.True)); } STModel iterST = STModel.Create(solver, "iter", initReg, charsort, charsort, regSort, 0, moves); iterST.Simplify(); return(iterST); }
public static void InitializeStandardRoutines(Symtab globalSymbolTable) { throw new NotImplementedException(); }
public static void InitializePredefinedTypes(Symtab pSymtab) { SymtabNode pIntegerId = pSymtab.Enter("integer", TDefnCode.dcType); SymtabNode pRealId = pSymtab.Enter("real", TDefnCode.dcType); SymtabNode pBooleanId = pSymtab.Enter("boolean", TDefnCode.dcType); SymtabNode pCharId = pSymtab.Enter("char", TDefnCode.dcType); SymtabNode pFalseId = pSymtab.Enter("false", TDefnCode.dcType); SymtabNode pTrueId = pSymtab.Enter("true", TDefnCode.dcType); if (pIntegerType == null) { SetType(ref pIntegerType, new TType(TFormCode.fcScalar, sizeof(int), pIntegerId)); } if (pRealType == null) { SetType(ref pRealType, new TType(TFormCode.fcScalar, sizeof(float), pRealId)); } if (pBooleanType == null) { SetType(ref pBooleanType, new TType(TFormCode.fcEnum, sizeof(int), pBooleanId)); } if (pCharType == null) { SetType(ref pCharType, new TType(TFormCode.fcScalar, sizeof(char), pCharId)); } //--Link each predefined type's id node to its type object. SetType(ref pIntegerId.pType, pIntegerType); SetType(ref pRealId.pType, pRealType); SetType(ref pBooleanId.pType, pBooleanType); SetType(ref pCharId.pType, pCharType); //--More initialization for the boolean type object. pBooleanType.Enumeration.max = 1; // max value pBooleanType.Enumeration.ConstIds = pFalseId; // first constant //--More initialization for the "false" and "true" id nodes. pFalseId.defn.constantValue.integerValue = 0; pTrueId.defn.constantValue.integerValue = 1; SetType(ref pTrueId.pType, pBooleanType); SetType(ref pFalseId.pType, pBooleanType); pFalseId.Next = pTrueId; // "false" node points to "true" node //--Initialize the dummy type object that will be used //--for erroneous type definitions and for typeless objects. SetType(ref pDummyType, new TType(TFormCode.fcNone, 1, null)); }
private void SetCurrentSymtab(Symtab symtab) { pSymtabs[SharedProperties.CurrentNestingLevel] = symtab; }
private void InitializeStandardRoutines(Symtab pSymtab) { foreach (TStdRtn pSR in stdRtnList) { SymtabNode pRoutineId = pSymtab.Enter(pSR.pName, pSR.dc); pRoutineId.defn.routine.which = pSR.rc; pRoutineId.defn.routine.parmCount = 0; pRoutineId.defn.routine.totalParmSize = 0; pRoutineId.defn.routine.totalLocalSize = 0; pRoutineId.defn.routine.locals.pParmIds = null; pRoutineId.defn.routine.locals.pConstantIds = null; pRoutineId.defn.routine.locals.pTypeIds = null; pRoutineId.defn.routine.locals.pVariableIds = null; pRoutineId.defn.routine.locals.pRoutineIds = null; pRoutineId.defn.routine.pSymtab = null; pRoutineId.defn.routine.pIcode = null; TType.SetType(ref pRoutineId.pType, TType.pDummyType); } }
public abstract S Convert(iterexpr ie, Symtab stab);