Ticket #1736: InstSection.patch

File InstSection.patch, 11.8 KB (added by Martin Sjölund, 12 years ago)
  • Compiler/FrontEnd/InstSection.mo

     
    510510        Error.addSourceMessage(Error.TUPLE_ASSIGN_FUNCALL_ONLY,{s},info);
    511511      then fail();*/
    512512
    513     // if-equation       
    514     // if the condition is constant this case will select the correct branch and remove the if-equation
    515     case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_IF(condition = conditions,thenBranch = tb,elseBranch = fb,info=info),SCode.NON_INITIAL(),impl,graph)
    516       equation
    517         (cache, expl1,props,_) = Static.elabExpList(cache,env, conditions, impl,NONE(),true,pre,info);
    518         DAE.PROP(DAE.T_BOOL(varLst = _),cnst) = Types.propsAnd(props);
    519         true = Types.isParameterOrConstant(cnst);
    520         (cache,valList,_) = Ceval.cevalList(cache, env, expl1, impl, NONE(), Ceval.NO_MSG());
    521         // check if valList contains Values.EMPTY()
    522         containsEmpty = ValuesUtil.containsEmpty(valList);
    523         generateNoConstantBindingError(containsEmpty, info);
    524         blist = List.map(valList,ValuesUtil.valueBool);
    525         b = Util.selectList(blist, tb, fb);
    526         (cache,env_1,ih,dae,csets_1,ci_state_1,graph) = Inst.instList(cache, env, ih, mod, pre, csets, ci_state, instEEquation, b, impl, Inst.alwaysUnroll, graph);
    527       then
    528         (cache,env_1,ih,dae,csets_1,ci_state_1,graph);
    529        
    530     // if-equation
    531     // If we are doing checkModel we might get an if-equation whose condition is
    532     // a parameter without a binding, and which DAEUtil.ifEqToExpr can't handle.
    533     // If the model would have been instantiated one of the branches would have
    534     // been chosen, so this case therefore chooses one of the branches.
    535     case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_IF(condition = conditions,thenBranch = tb,elseBranch = fb,info=info),SCode.NON_INITIAL(),impl,graph)
     513    // if equation
     514    case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_IF(condition = conditions,thenBranch = tb,elseBranch = fb,info = info),SCode.NON_INITIAL(),impl,graph)
    536515      equation
    537         true = Flags.getConfigBool(Flags.CHECK_MODEL);
    538         (cache, _,props,_) = Static.elabExpList(cache,env, conditions, impl,NONE(),true,pre,info);
    539         DAE.PROP(DAE.T_BOOL(varLst = _),DAE.C_PARAM()) = Types.propsAnd(props);
    540         b = Util.selectList({true}, tb, fb);
    541         (cache,env_1,ih,dae,csets_1,ci_state_1,graph) = Inst.instList(cache, env, ih, mod, pre, csets, ci_state, instEEquation, b, impl, Inst.alwaysUnroll, graph);
    542       then
    543         (cache,env_1,ih,dae,csets_1,ci_state_1,graph);
    544 
    545     // initial if-equation
    546     // if the condition is constant this case will select the correct branch and remove the initial if-equation
    547     case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_IF(condition = conditions,thenBranch = tb,elseBranch = fb,info=info),SCode.INITIAL(),impl,graph)
    548       equation
     516        print("EQ_IF: " +& Dump.printExpStr(Absyn.TUPLE(conditions)) +& "\n");
    549517        (cache, expl1,props,_) = Static.elabExpList(cache,env, conditions, impl,NONE(),true,pre,info);
    550518        DAE.PROP(DAE.T_BOOL(varLst = _),_) = Types.propsAnd(props);
    551         (cache,valList,_) = Ceval.cevalList(cache, env, expl1, impl, NONE(), Ceval.NO_MSG());
    552         blist = List.map(valList,ValuesUtil.valueBool);
    553         b = Util.selectList(blist, tb, fb);
    554         (cache,env_1,ih,dae,csets_1,ci_state_1,graph) = Inst.instList(cache,env,ih, mod, pre, csets, ci_state, instEInitialEquation, b, impl, Inst.alwaysUnroll, graph);
    555       then
    556         (cache,env_1,ih,dae,csets_1,ci_state_1,graph);
    557 
    558     // if equation when condition is not constant
    559     case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_IF(condition = conditions,thenBranch = tb,elseBranch = fb,info = info),SCode.NON_INITIAL(),impl,graph)
    560       equation
    561         (cache, expl1,props,_) = Static.elabExpList(cache,env, conditions, impl,NONE(),true,pre,info);
    562         DAE.PROP(DAE.T_BOOL(varLst = _),DAE.C_VAR()) = Types.propsAnd(props);
    563         (cache,expl1) = PrefixUtil.prefixExpList(cache, env, ih, expl1, pre);
    564519       
    565520        // set the source of this element
    566521        source = DAEUtil.createElementSource(info, Env.getEnvPath(env), PrefixUtil.prefixToCrefOpt(pre), NONE(), NONE());
    567522       
    568523        (cache,env_1,ih,daeLLst,_,ci_state_1,graph) = instIfTrueBranches(cache, env,ih, mod, pre, csets, ci_state, tb, false, impl, graph);
    569524        (cache,env_2,ih,DAE.DAE(daeElts2),_,ci_state_2,graph) = Inst.instList(cache,env_1,ih, mod, pre, csets, ci_state, instEEquation, fb, impl, Inst.alwaysUnroll, graph) "There are no connections inside if-clauses." ;
    570         dae = DAE.DAE({DAE.IF_EQUATION(expl1,daeLLst,daeElts2,source)});
     525        (cache,dae) = makeIfEquation(cache,env,impl,ih,pre,props,false,expl1,daeLLst,daeElts2,{},{},source);
    571526      then
    572527        (cache,env_1,ih,dae,csets,ci_state_1,graph);
    573528
    574     // initial if equation  when condition is not constant
     529    // initial if equation
    575530    case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_IF(condition = conditions,thenBranch = tb,elseBranch = fb, info = info),SCode.INITIAL(),impl,graph)
    576531      equation
    577532        (cache, expl1,props,_) = Static.elabExpList(cache,env, conditions, impl,NONE(),true,pre,info);
    578         DAE.PROP(DAE.T_BOOL(varLst = _),DAE.C_VAR()) = Types.propsAnd(props);
    579         (cache,expl1) = PrefixUtil.prefixExpList(cache, env, ih, expl1, pre);
     533        DAE.PROP(DAE.T_BOOL(varLst = _),_) = Types.propsAnd(props);
    580534
    581535        // set the source of this element
    582536        source = DAEUtil.createElementSource(info, Env.getEnvPath(env), PrefixUtil.prefixToCrefOpt(pre), NONE(), NONE());
    583537       
    584538        (cache,env_1,ih,daeLLst,_,ci_state_1,graph) = instIfTrueBranches(cache,env,ih, mod, pre, csets, ci_state, tb, true, impl, graph);
    585539        (cache,env_2,ih,DAE.DAE(daeElts2),_,ci_state_2,graph) = Inst.instList(cache,env_1,ih, mod, pre, csets, ci_state, instEInitialEquation, fb, impl, Inst.alwaysUnroll, graph) "There are no connections inside if-clauses." ;
    586         dae = DAE.DAE({DAE.INITIAL_IF_EQUATION(expl1,daeLLst,daeElts2,source)});
     540        (cache,dae) = makeIfEquation(cache,env,impl,ih,pre,props,true,expl1,daeLLst,daeElts2,{},{},source);
    587541      then
    588542        (cache,env_1,ih,dae,csets,ci_state_1,graph);
    589543
     544    // if-equation
     545    // If we are doing checkModel we might get an if-equation whose condition is
     546    // a parameter without a binding, and which DAEUtil.ifEqToExpr can't handle.
     547    // If the model would have been instantiated one of the branches would have
     548    // been chosen, so this case therefore chooses one of the branches.
     549    case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_IF(condition = conditions,thenBranch = tb,elseBranch = fb,info=info),SCode.NON_INITIAL(),impl,graph)
     550      equation
     551        true = Flags.getConfigBool(Flags.CHECK_MODEL);
     552        (cache, _,props,_) = Static.elabExpList(cache,env, conditions, impl,NONE(),true,pre,info);
     553        DAE.PROP(DAE.T_BOOL(varLst = _),DAE.C_PARAM()) = Types.propsAnd(props);
     554        b = Util.selectList({true}, tb, fb);
     555        (cache,env_1,ih,dae,csets_1,ci_state_1,graph) = Inst.instList(cache, env, ih, mod, pre, csets, ci_state, instEEquation, b, impl, Inst.alwaysUnroll, graph);
     556      then
     557        (cache,env_1,ih,dae,csets_1,ci_state_1,graph);
     558
    590559    // when equation statement, modelica 1.1
    591560    //  When statements are instantiated by evaluating the conditional expression.
    592561    case (cache,env,ih,mod,pre,csets,ci_state, eq as SCode.EQ_WHEN(condition = e,eEquationLst = el,elseBranches = ((ee,eel) :: eex),info=info),(initial_ as SCode.NON_INITIAL()),impl,graph)
     
    29442913  end matchcontinue;
    29452914end instIfTrueBranches;
    29462915
     2916protected function makeIfEquation "Selects branches and creates the elements associated with the if-equation"
     2917  input Env.Cache inCache;
     2918  input Env.Env inEnv;
     2919  input Boolean impl;
     2920  input InstanceHierarchy ih;
     2921  input Prefix.Prefix pre;
     2922  input list<DAE.Properties> props;
     2923  input Boolean isInitial;
     2924  input list<DAE.Exp> exps;
     2925  input list<list<DAE.Element>> tbs;
     2926  input list<DAE.Element> fb;
     2927  input list<DAE.Exp> accExp;
     2928  input list<list<DAE.Element>> accTb;
     2929  input DAE.ElementSource source;
     2930  output Env.Cache outCache;
     2931  output DAE.DAElist dae;
     2932algorithm
     2933  (outCache,dae) := matchcontinue (inCache,inEnv,impl,ih,pre,props,isInitial,exps,tbs,fb,accExp,accTb,source)
     2934    local
     2935      DAE.Exp exp;
     2936      list<DAE.Element> tb,elts;
     2937      list<list<DAE.Element>> restTb;
     2938      list<DAE.Exp> restExp,exps;
     2939      list<DAE.Properties> restProp;
     2940      DAE.Properties prop;
     2941      Env.Cache cache;
     2942      Boolean b;
     2943      Option<Values.Value> containsEmpty;
     2944      DAE.Const cnst;
     2945      list<Values.Value> valList;
     2946      list<Boolean> blist;
     2947    case (cache,inEnv,impl,ih,pre,props,isInitial,exps,tbs,fb,accExp,accTb,source)
     2948      equation
     2949        DAE.PROP(_,cnst) = Types.propsAnd(props);
     2950        true = Types.isParameterOrConstant(cnst);
     2951        print("Exps to eval: " +& ExpressionDump.printExpStr(DAE.TUPLE(exps)) +& "\n");
     2952        (cache,valList,_) = Ceval.cevalList(cache, inEnv, exps, impl, NONE(), Ceval.NO_MSG());
     2953        print("evaled: " +& ValuesUtil.valString(Values.TUPLE(valList)) +& "\n");
     2954        // check if valList contains Values.EMPTY()
     2955        containsEmpty = ValuesUtil.containsEmpty(valList);
     2956        generateNoConstantBindingError(containsEmpty, DAEUtil.getElementSourceFileInfo(source));
     2957        blist = List.map(valList,ValuesUtil.valueBool);
     2958        elts = Util.selectList(blist, tbs, fb);
     2959      then (cache,DAE.DAE(elts));
     2960    case (cache,inEnv,impl,ih,pre,props,isInitial,exps,tbs,fb,accExp,accTb,source)
     2961      equation
     2962        DAE.PROP(_,DAE.C_VAR()) = Types.propsAnd(props);
     2963        (cache,exps) = PrefixUtil.prefixExpList(cache, inEnv, ih, exps, pre);
     2964        print("Exps to not eval: " +& ExpressionDump.printExpStr(DAE.TUPLE(exps)) +& "\n");
     2965        elts = makeIfEquation2(isInitial,exps,tbs,fb,source);
     2966      then (cache,DAE.DAE(elts));
     2967      // TODO: Use something smarter? Sadly does not work properly...
     2968      /*
     2969    case (cache,inEnv,impl,ih,pre,prop::restProp,isInitial,exp::restExp,tb::restTb,fb,accExp,accTb,source)
     2970      equation
     2971        true = Types.isParameterOrConstant(Types.propAllConst(prop));
     2972        (cache,Values.BOOL(b),_) = Ceval.ceval(cache,inEnv,exp,impl,NONE(),Ceval.NO_MSG());
     2973        (cache,dae) = makeIfEquation(cache,inEnv,impl,ih,pre,Util.if_(b,{},restProp),isInitial,Util.if_(b,{},restExp),Util.if_(b,{},restTb),Util.if_(b,tb,fb),accExp,accTb,source);
     2974      then (cache,dae);
     2975    case (cache,inEnv,impl,ih,pre,prop::restProp,isInitial,exp::restExp,tb::restTb,fb,accExp,accTb,source)
     2976      equation
     2977        false = Types.isConstant(Types.propAllConst(prop));
     2978        (cache,dae) = makeIfEquation(cache,inEnv,impl,ih,pre,restProp,isInitial,restExp,restTb,fb,exp::accExp,tb::accTb,source);
     2979      then (cache,dae);
     2980    case (cache,inEnv,impl,ih,pre,{},isInitial,{},{},fb,accExp,accTb,source)
     2981      equation
     2982        (cache,exps) = PrefixUtil.prefixExpList(cache, inEnv, ih, listReverse(accExp), pre);
     2983        elts = makeIfEquation2(isInitial,exps,listReverse(accTb),fb,source);
     2984      then (cache,DAE.DAE(elts));
     2985      */
     2986  end matchcontinue;
     2987end makeIfEquation;
     2988
     2989protected function makeIfEquation2
     2990  input Boolean isInitial;
     2991  input list<DAE.Exp> exps;
     2992  input list<list<DAE.Element>> tbs;
     2993  input list<DAE.Element> fb;
     2994  input DAE.ElementSource source;
     2995  output list<DAE.Element> outElements;
     2996algorithm
     2997  outElements := matchcontinue (isInitial,exps,tbs,fb,source)
     2998    case (_,{},_,fb,source)
     2999      then fb;
     3000    case (false,exps,tbs,fb,source)
     3001      then DAE.IF_EQUATION(exps,tbs,fb,source)::{};
     3002    case (true,exps,tbs,fb,source)
     3003      then DAE.INITIAL_IF_EQUATION(exps,tbs,fb,source)::{};
     3004  end matchcontinue;
     3005end makeIfEquation2;
     3006
    29473007protected function instElseIfs
    29483008"function: instElseIfs
    29493009  This function helps instStatement to handle elseif parts."