Changeset 0aff3d1 in OpenModelica


Ignore:
Timestamp:
2016-04-04T10:59:00+02:00 (8 years ago)
Author:
Lennart Ochel <lennart.ochel@…>
Branches:
Added-citation-metadata, maintenance/v1.14, maintenance/v1.15, maintenance/v1.16, maintenance/v1.17, maintenance/v1.18, maintenance/v1.19, maintenance/v1.20, maintenance/v1.21, maintenance/v1.22, maintenance/v1.23, master, omlib-staging
Children:
5f55444
Parents:
7100384
git-author:
jhag <jan.hagemann@…> (03/24/16 11:54:37)
git-committer:
Lennart Ochel <lennart.ochel@…> (04/04/16 10:59:00)
Message:

wrapFunctionCalls: avoid simple equations

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Compiler/BackEnd/CommonSubExpression.mo

    re5d70b8c r0aff3d1  
    6565protected import Types;
    6666
    67 public function wrapFunctionCalls
     67constant Boolean experimentalB = false; //Experimaental boolean for statistic hashtable
     68constant Boolean debug = false;
     69
     70
     71public function wrapFunctionCalls "author: Jan Hagemann and Lennart Ochel (FH Bielefeld, Germany)
     72main function: is called by postOpt and SymbolicJacobian"
    6873  input BackendDAE.BackendDAE inDAE;
    6974  output BackendDAE.BackendDAE outDAE;
     
    7681  DAE.FunctionTree functionTree;
    7782  HashTableExpToExp.HashTable HT;
    78   Integer index=System.tmpTickIndex(Global.backendDAE_cseIndex), index2;
     83  HashTableExpToIndex.HashTable HT2;
     84  Integer index=System.tmpTickIndex(Global.backendDAE_cseIndex);
    7985  list<BackendDAE.Equation> eqList;
    8086  list<BackendDAE.Var> varList;
    81 algorithm
     87
     88algorithm
     89  if debug then
     90    print("\npost-optimization module wrapFunctionCalls (" + BackendDump.printBackendDAEType2String(inDAE.shared.backendDAEType) + "):\n\n");
     91  end if;
     92
    8293  shared := inDAE.shared;
    8394  BackendDAE.SHARED(functionTree=functionTree) := shared;
     95  HT := HashTableExpToExp.emptyHashTableSized(49999);  //2053    4013    25343   536870879
     96  HT2 := HashTableExpToIndex.emptyHashTableSized(49999);  //2053    4013    25343   536870879
    8497
    8598  for syst in inDAE.eqs loop
     
    87100    orderedEqs := syst.orderedEqs;
    88101
     102    // dump the EqSystem before the module works (debug dump +d=dumpCSE_verbose)
    89103    if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then
    90104      BackendDump.dumpEqSystem(syst, "################EQSYSTEM:###################");
    91105    end if;
    92106
    93     HT := HashTableExpToExp.emptyHashTableSized(49999);  //2053    4013    25343   536870879
     107    // the module traverses the EqSystem twice
     108    // the first time the module notices the equations CREF = CALL or CALL = CREF; and creates a statistic if the ExperimantelBoolean is true
     109    (orderedEqs, (HT, HT2)) := BackendEquation.traverseEquationArray_WithUpdate(orderedEqs, createStats, (HT, HT2));
     110    // the second time the module looks for calls and substitutes with cse-variables or with the CREF of the first iteration
    94111    (orderedEqs, (HT, index, eqList, varList, _)) := BackendEquation.traverseEquationArray_WithUpdate(orderedEqs, wrapFunctionCalls2, (HT, index, {}, {}, functionTree));
    95112
     113    // dump of the hashtable (debug)
    96114    if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then
    97115      print("\n");
     
    99117    end if;
    100118
     119    // the module has to build a new matching
    101120    if not listEmpty(eqList) or not listEmpty(varList) then
    102121      syst.orderedEqs := BackendEquation.addEquations(eqList, orderedEqs);
     
    106125      syst.matching := BackendDAE.NO_MATCHING();
    107126
     127      // dump the equations & variables after the module (debug dump +d=dumpCSE or +d=dumpCSE_verbose)
    108128      if Flags.isSet(Flags.DUMP_CSE) or Flags.isSet(Flags.DUMP_CSE_VERBOSE) then
    109         BackendDump.dumpVariables(syst.orderedVars, "########### Updated Variable List ###########");
    110         BackendDump.dumpEquationArray(syst.orderedEqs, "########### Updated Equation List ###########");
     129        BackendDump.dumpVariables(syst.orderedVars, "########### Updated Variable List (" + BackendDump.printBackendDAEType2String(inDAE.shared.backendDAEType) + ") ###########");
     130        BackendDump.dumpEquationArray(syst.orderedEqs, "########### Updated Equation List (" + BackendDump.printBackendDAEType2String(inDAE.shared.backendDAEType) + ") ###########");
    111131      end if;
    112132    end if;
     
    114134    eqs := syst::eqs;
    115135  end for;
     136
    116137  eqs := MetaModelica.Dangerous.listReverseInPlace(eqs);
    117 
    118138  System.tmpTickSetIndex(index, Global.backendDAE_cseIndex);
    119139  outDAE := BackendDAE.DAE(eqs, shared);
    120   // TODO: change pre-opt module order
    121   //outDAE := SynchronousFeatures.clockPartitioning(BackendDAE.DAE({syst}, shared));
    122140end wrapFunctionCalls;
    123141
    124142
    125 protected function wrapFunctionCalls2
     143protected function wrapFunctionCalls2 "helper function for wrapFunctionCalls; it traverses all equation of the EqSystem"
    126144  input BackendDAE.Equation inEq;
    127145  input tuple<HashTableExpToExp.HashTable, Integer, list<BackendDAE.Equation>, list<BackendDAE.Var>, DAE.FunctionTree> inTuple;
     
    144162      tuple<HashTableExpToExp.HashTable, Integer, list<BackendDAE.Equation>, list<BackendDAE.Var>, DAE.FunctionTree> tpl;
    145163
    146     case BackendDAE.ALGORITHM()  equation
    147       if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then
    148          print("\ntraverse " + BackendDump.equationString(inEq) + " algorithm\n");
    149       end if;
    150     then (inEq, inTuple);
    151 
    152     case BackendDAE.WHEN_EQUATION() equation
    153       if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then
    154          print("\ntraverse " + BackendDump.equationString(inEq) + " when\n");
    155       end if;
    156     then (inEq, inTuple);  // not necessary
    157 
     164    //special case for records or tuples of complex-equation
    158165    case BackendDAE.COMPLEX_EQUATION(left=left, right=right) equation
    159166      if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then
    160167        print("\ntraverse " + BackendDump.equationString(inEq) + " complex\n");
    161168      end if;
     169
    162170      if Expression.isPureCall(left) and Expression.isPureCall(right) then
    163171        (_, _, _ , _, functionTree) = inTuple;
     
    200208        tpl = (HT, index, eqList, varList, functionTree);
    201209        else
    202           /* special case: complex equation is generated of resolveLopps and evalFunc;
    203           normally: this case is a "normal" equation: */
    204210          eq = inEq;
    205211          tpl = inTuple;
     
    207213    then (eq, tpl);
    208214
    209     case BackendDAE.ARRAY_EQUATION() equation
    210       if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then
    211          print("\ntraverse " + BackendDump.equationString(inEq) + " array\n");
    212       end if;
    213     then (inEq, inTuple);
    214 
    215     case BackendDAE.IF_EQUATION() equation
    216       if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then
    217          print("\ntraverse " + BackendDump.equationString(inEq) + " if\n");
    218       end if;
    219     then (inEq, inTuple);
    220 
     215    // case for 'normal' equations
    221216    case BackendDAE.EQUATION(exp=exp, scalar=scalar) equation
    222217      if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then
    223218         print("\ntraverse " + BackendDump.equationString(inEq) + " normal\n");
    224219      end if;
    225       if isZeroequalCall(exp, scalar) then  //0.0 = CALL or CALL = 0.0 or CALL = CALL
    226         eq = inEq;
    227         tpl = inTuple;
    228       else
    229         (eq, (tpl, _)) = BackendEquation.traverseExpsOfEquation(inEq, wrapFunctionCalls3, (inTuple, BackendEquation.equationSource(inEq)));
    230       end if;
     220      (eq, tpl) = wrapFunctionCalls_advanced(exp, scalar, inEq, inTuple);
    231221    then (eq, tpl);
    232222
     223    // case for all other equation: WHEN, IF ...
    233224    else equation
    234225      if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then
     
    240231end wrapFunctionCalls2;
    241232
    242 protected function wrapFunctionCalls3
     233
     234protected function wrapFunctionCalls_advanced "helper function for 'normal' equation"
     235  input DAE.Exp inExp1;
     236  input DAE.Exp inExp2;
     237  input BackendDAE.Equation inEq;
     238  input tuple<HashTableExpToExp.HashTable, Integer, list<BackendDAE.Equation>, list<BackendDAE.Var>, DAE.FunctionTree> inTuple;
     239  output BackendDAE.Equation outEq;
     240  output tuple<HashTableExpToExp.HashTable, Integer, list<BackendDAE.Equation>, list<BackendDAE.Var>, DAE.FunctionTree> outTuple;
     241algorithm
     242  (outEq, outTuple) := match(inExp1, inExp2)
     243  local
     244    DAE.Exp key, value;
     245    BackendDAE.Equation eq;
     246    tuple<HashTableExpToExp.HashTable, Integer, list<BackendDAE.Equation>, list<BackendDAE.Var>, DAE.FunctionTree> tpl;
     247    Integer index;
     248    list<BackendDAE.Equation> eqList;
     249    list<BackendDAE.Var> varList;
     250    DAE.FunctionTree functionTree;
     251    HashTableExpToExp.HashTable HT;
     252
     253    case (DAE.RCONST(0.0), DAE.CALL())
     254    then (inEq, inTuple);
     255
     256    case (DAE.CALL(), DAE.RCONST(0.0))
     257    then (inEq, inTuple);
     258
     259    // case (DAE.CALL(), DAE.CALL())
     260    // then (inEq, inTuple);
     261
     262    case (DAE.CREF(), DAE.CALL())
     263    then (inEq, inTuple);
     264
     265    case (DAE.CALL(), DAE.CREF())
     266    then (inEq, inTuple);
     267
     268    else equation
     269      (eq, (tpl, _)) = BackendEquation.traverseExpsOfEquation(inEq, wrapFunctionCalls3, (inTuple, BackendEquation.equationSource(inEq)));
     270    then (eq, tpl);
     271
     272  end match;
     273end wrapFunctionCalls_advanced;
     274
     275
     276protected function wrapFunctionCalls3 "helper function: traverses all Expressions of the equation"
    243277  input DAE.Exp inExp;
    244278  input tuple<tuple<HashTableExpToExp.HashTable, Integer, list<BackendDAE.Equation>, list<BackendDAE.Var>, DAE.FunctionTree>, DAE.ElementSource> inTuple;
     
    246280  output tuple<tuple<HashTableExpToExp.HashTable, Integer, list<BackendDAE.Equation>, list<BackendDAE.Var>, DAE.FunctionTree>, DAE.ElementSource> outTuple;
    247281algorithm
    248   if Expression.isExpIfExp(inExp) then
     282  if Expression.isExpIfExp(inExp) then      //skip IfExp
    249283    outExp  := inExp;
    250284    outTuple := inTuple;
     
    254288end wrapFunctionCalls3;
    255289
    256 protected function wrapFunctionCalls_main
     290
     291protected function wrapFunctionCalls_main "helper function: traverses all Expressions from Buttom Up"
    257292  input DAE.Exp inExp;
    258293  input tuple<tuple<HashTableExpToExp.HashTable, Integer, list<BackendDAE.Equation>, list<BackendDAE.Var>, DAE.FunctionTree>, DAE.ElementSource> inTuple;
     
    260295  output tuple<tuple<HashTableExpToExp.HashTable, Integer, list<BackendDAE.Equation>, list<BackendDAE.Var>, DAE.FunctionTree>, DAE.ElementSource> outTuple;
    261296algorithm
    262   if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then
    263       print("Exp: " + ExpressionDump.dumpExpStr(inExp, 0) + "\n");
    264   end if;
    265 
    266297  (outExp, outTuple) := matchcontinue(inExp, inTuple)
    267298    local
     
    276307      DAE.FunctionTree functionTree;
    277308
    278     // skip some special function calls
    279     case (DAE.CALL(path=Absyn.IDENT("$getPart")), _)
    280     then (inExp, inTuple);
    281     case (DAE.CALL(path=Absyn.IDENT("pre")), _)
    282     then (inExp, inTuple);
    283     case (DAE.CALL(path=Absyn.IDENT("previous")), _)
    284     then (inExp, inTuple);
    285     case (DAE.CALL(path=Absyn.IDENT("change")), _)
    286     then (inExp, inTuple);
    287     case (DAE.CALL(path=Absyn.IDENT("delay")), _)
    288     then (inExp, inTuple);
    289     case (DAE.CALL(path=Absyn.IDENT("edge")), _)
    290     then (inExp, inTuple);
    291     case (DAE.CALL(path=Absyn.IDENT("$_start")), _)
    292     then (inExp, inTuple);
    293     case (DAE.CALL(path=Absyn.IDENT("$_initialGuess")), _)
    294     then (inExp, inTuple);
    295     case (DAE.CALL(path=Absyn.IDENT("initial")), _)
    296     then (inExp, inTuple);
    297     case (DAE.CALL(path=Absyn.IDENT("$_round")), _)
    298     then (inExp, inTuple);
    299     case (DAE.CALL(path=Absyn.IDENT("$_old")), _)
    300     then (inExp, inTuple);
    301     case (DAE.CALL(path=Absyn.IDENT("der")), _)
    302     then (inExp, inTuple);
    303     case (DAE.CALL(path=Absyn.IDENT("smooth")), _)
    304     then (inExp, inTuple);
    305     case (DAE.CALL(path=Absyn.IDENT("noEvent")), _)
    306     then (inExp, inTuple);
    307     case (DAE.CALL(path=Absyn.IDENT("semiLinear")), _)
    308     then (inExp, inTuple);
    309     case (DAE.CALL(path=Absyn.IDENT("homotopy")), _)
    310     then (inExp, inTuple);
    311     case (DAE.CALL(path=Absyn.IDENT("reinit")), _)
    312     then (inExp, inTuple);
    313     case (DAE.CALL(path=Absyn.IDENT("String")), _)
    314     then (inExp, inTuple);
    315     case (DAE.CALL(path=Absyn.IDENT("interval")), _)
    316     then (inExp, inTuple);
    317     case (DAE.CALL(path=Absyn.IDENT("Clock")), _)
    318     then (inExp, inTuple);
    319     case (DAE.CALL(path=Absyn.IDENT("sample")), _)
    320     then (inExp, inTuple);
    321     case (DAE.CALL(path=Absyn.IDENT("hold")), _)
    322     then (inExp, inTuple);
    323     case (DAE.CALL(path=Absyn.IDENT("subSample")), _)
    324     then (inExp, inTuple);
    325     case (DAE.CALL(path=Absyn.IDENT("superSample")), _)
    326     then (inExp, inTuple);
    327     case (DAE.CALL(path=Absyn.IDENT("shiftSample")), _)
    328     then (inExp, inTuple);
    329     case (DAE.CALL(path=Absyn.IDENT("backSample")), _)
    330     then (inExp, inTuple);
    331     case (DAE.CALL(path=Absyn.IDENT("noClock")), _)
    332     then (inExp, inTuple);
    333     case (DAE.CALL(path=Absyn.IDENT("sign")), _)
    334     then (inExp, inTuple);
    335     case (_, _) guard(Expression.isImpureCall(inExp))
    336     then (inExp, inTuple);
    337 
    338309    case (key as DAE.CALL(attr=DAE.CALL_ATTR(ty=ty)), ((HT, index, eqList, varList, functionTree), source)) equation
    339       if not BaseHashTable.hasKey(key, HT) then
    340         (value, index) = createReturnExp(ty, index);
    341         HT = BaseHashTable.add((key, value), HT);
    342         varList = createVarsForExp(value, varList);
    343         eq = BackendEquation.generateEquation(value, key, source, BackendDAE.EQ_ATTR_DEFAULT_BINDING);
    344         eqList = eq::eqList;
     310      if isSkipCase(key) then
     311        value = key;
    345312      else
    346         value = BaseHashTable.get(key, HT);
     313        if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then
     314            print("Exp: " + ExpressionDump.dumpExpStr(inExp, 0) + "\n");
     315        end if;
     316        if not BaseHashTable.hasKey(key, HT) then
     317            (value, index) = createReturnExp(ty, index);
     318            HT = BaseHashTable.add((key, value), HT);
     319            varList = createVarsForExp(value, varList);
     320            eq = BackendEquation.generateEquation(value, key, source, BackendDAE.EQ_ATTR_DEFAULT_BINDING);
     321            eqList = eq::eqList;
     322        else
     323            value = BaseHashTable.get(key, HT);
     324        end if;
     325        if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then
     326            print("  Exp_sub: " + ExpressionDump.printExpStr(value) + "\n");
     327        end if;
    347328      end if;
    348         if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then
    349           print("  Exp_sub: " + ExpressionDump.printExpStr(value) + "\n");
    350         end if;
    351329    then (value, ((HT, index, eqList, varList, functionTree), source));
    352330
     
    355333end wrapFunctionCalls_main;
    356334
    357 protected function isRecordExp
     335
     336protected function isSkipCase "outline all skip cases"
     337  input DAE.Exp inCall;
     338  output Boolean outB;
     339algorithm
     340  outB := match(inCall)
     341    case DAE.CALL(path=Absyn.IDENT("$getPart")) then true;
     342    case DAE.CALL(path=Absyn.IDENT("pre")) then true;
     343    case DAE.CALL(path=Absyn.IDENT("previous")) then true;
     344    case DAE.CALL(path=Absyn.IDENT("change")) then true;
     345    case DAE.CALL(path=Absyn.IDENT("delay")) then true;
     346    case DAE.CALL(path=Absyn.IDENT("edge")) then true;
     347    case DAE.CALL(path=Absyn.IDENT("$_start")) then true;
     348    case DAE.CALL(path=Absyn.IDENT("$_initialGuess"))  then true;
     349    case DAE.CALL(path=Absyn.IDENT("initial")) then true;
     350    case DAE.CALL(path=Absyn.IDENT("$_round")) then true;
     351    case DAE.CALL(path=Absyn.IDENT("$_old")) then true;
     352    case DAE.CALL(path=Absyn.IDENT("der")) then true;
     353    case DAE.CALL(path=Absyn.IDENT("smooth")) then true;
     354    case DAE.CALL(path=Absyn.IDENT("noEvent")) then true;
     355    case DAE.CALL(path=Absyn.IDENT("semiLinear")) then true;
     356    case DAE.CALL(path=Absyn.IDENT("homotopy")) then true;
     357    case DAE.CALL(path=Absyn.IDENT("reinit")) then true;
     358    case DAE.CALL(path=Absyn.IDENT("String")) then true;
     359    case DAE.CALL(path=Absyn.IDENT("interval")) then true;
     360    case DAE.CALL(path=Absyn.IDENT("Clock")) then true;
     361    case DAE.CALL(path=Absyn.IDENT("sample")) then true;
     362    case DAE.CALL(path=Absyn.IDENT("hold")) then true;
     363    case DAE.CALL(path=Absyn.IDENT("subSample")) then true;
     364    case DAE.CALL(path=Absyn.IDENT("superSample")) then true;
     365    case DAE.CALL(path=Absyn.IDENT("shiftSample")) then true;
     366    case DAE.CALL(path=Absyn.IDENT("backSample")) then true;
     367    case DAE.CALL(path=Absyn.IDENT("noClock")) then true;
     368    case DAE.CALL(path=Absyn.IDENT("sign")) then true;
     369    case _ guard(Expression.isImpureCall(inCall)) then true;
     370    else false;
     371  end match;
     372end isSkipCase;
     373
     374
     375protected function isRecordExp "helper function for records in complex-equation"
    358376  input DAE.Exp inExp;
    359377  output Boolean outRecord;
     
    366384end isRecordExp;
    367385
    368 protected function isZeroequalCall
     386
     387protected function isCallEqualCref "helper function for creates statistics"
    369388  input DAE.Exp inExp;
    370389  input DAE.Exp inExp2;
    371   output Boolean outB;
    372 algorithm
    373    outB := match(inExp, inExp2)
    374 
    375     case (DAE.RCONST(0.0), DAE.CALL())
    376     then true;
    377 
    378     case (DAE.CALL(), DAE.RCONST(0.0))
    379     then true;
    380 
    381     // case (DAE.CALL(), DAE.CALL())
    382     // then true;
    383 
     390  output Boolean outBoolean;
     391algorithm
     392  outBoolean := match(inExp,inExp2)
     393    case (DAE.CREF(),DAE.CALL()) then true;
     394    case (DAE.CALL(),DAE.CREF()) then true;
    384395    else false;
    385396  end match;
    386 end isZeroequalCall;
    387 
    388 protected function expand
     397end isCallEqualCref;
     398
     399
     400protected function expand ""
    389401  input list<DAE.Exp> inExpLst1;
    390402  input list<DAE.Exp> inExpLst2;
     
    399411      list<BackendDAE.Equation> eqList;
    400412
    401     case ((left as DAE.CREF(componentRef=DAE.WILD()))::{}, right::{})
    402     then inEqList;
    403 
    404     case (left::{}, (right as DAE.CREF(componentRef=DAE.WILD()))::{})
    405     then inEqList;
    406 
    407     case (left::{}, right::{}) equation
    408       eq = BackendEquation.generateEquation(left, right, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
    409       eqList = eq::inEqList;
    410     then eqList;
    411 
    412     case ((left as DAE.CREF(componentRef=DAE.WILD()))::expLst1, right::expLst2) equation
    413       eqList = expand(expLst1, expLst2, inEqList);
    414     then eqList;
    415 
    416     case (left::expLst1, (right as DAE.CREF(componentRef=DAE.WILD()))::expLst2) equation
    417       eqList = expand(expLst1, expLst2, inEqList);
    418     then eqList;
    419 
    420     case (left::expLst1, right::expLst2) equation
    421       eq = BackendEquation.generateEquation(left, right, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
    422       eqList = eq::inEqList;
    423       eqList = expand(expLst1, expLst2, eqList);
    424     then eqList;
     413      case ((left as DAE.CREF(componentRef=DAE.WILD()))::{}, right::{})
     414      then inEqList;
     415
     416      case (left::{}, (right as DAE.CREF(componentRef=DAE.WILD()))::{})
     417      then inEqList;
     418
     419      case (left::{}, right::{}) equation
     420        eq = BackendEquation.generateEquation(left, right, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
     421        eqList = eq::inEqList;
     422      then eqList;
     423
     424      case ((left as DAE.CREF(componentRef=DAE.WILD()))::expLst1, right::expLst2) equation
     425        eqList = expand(expLst1, expLst2, inEqList);
     426      then eqList;
     427
     428      case (left::expLst1, (right as DAE.CREF(componentRef=DAE.WILD()))::expLst2) equation
     429        eqList = expand(expLst1, expLst2, inEqList);
     430      then eqList;
     431
     432      case (left::expLst1, right::expLst2) equation
     433        eq = BackendEquation.generateEquation(left, right, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
     434        eqList = eq::inEqList;
     435        eqList = expand(expLst1, expLst2, eqList);
     436      then eqList;
    425437  end match;
    426438end expand;
    427439
    428440
    429 
     441protected function createStats
     442  input BackendDAE.Equation inEq;
     443  input tuple<HashTableExpToExp.HashTable, HashTableExpToIndex.HashTable> inTuple;
     444  output BackendDAE.Equation outEq;
     445  output tuple<HashTableExpToExp.HashTable, HashTableExpToIndex.HashTable> outTuple;
     446algorithm
     447  (outEq, outTuple) := match(inEq)
     448    local
     449      DAE.Exp exp, scalar;
     450      HashTableExpToExp.HashTable HT;
     451      HashTableExpToIndex.HashTable HT2;
     452
     453      // case BackendDAE.ALGORITHM()
     454      // then (inEq, inTuple);
     455
     456      // case BackendDAE.WHEN_EQUATION()
     457      // then (inEq, inTuple);
     458
     459      // case BackendDAE.COMPLEX_EQUATION equation
     460        // (eq, (tpl, _)) = BackendEquation.traverseExpsOfEquation(inEq, wrapFunctionCalls3, (inTuple, BackendEquation.equationSource(inEq)));
     461      // then (eq, tpl);
     462
     463      // case BackendDAE.ARRAY_EQUATION()
     464      // then (inEq, inTuple);
     465
     466      // case BackendDAE.IF_EQUATION()
     467      // then (inEq, inTuple);
     468
     469      case BackendDAE.EQUATION(exp=exp, scalar=scalar) equation
     470        (HT, HT2) = inTuple;
     471        if isCallEqualCref(exp, scalar) then
     472          if Expression.isCall(exp) then
     473            HT = BaseHashTable.add((exp, scalar), HT);
     474          else
     475            HT = BaseHashTable.add((scalar, exp), HT);
     476          end if;
     477        end if;
     478        if experimentalB then
     479          (outEq, outTuple) = BackendEquation.traverseExpsOfEquation(inEq, createStats2, (HT, HT2));
     480        else
     481          outEq = inEq;
     482          outTuple = (HT, HT2);
     483        end if;
     484      then (outEq, outTuple);
     485
     486      else equation
     487        if experimentalB then
     488          (outEq, outTuple) = BackendEquation.traverseExpsOfEquation(inEq, createStats2, inTuple);
     489        else
     490          outEq = inEq;
     491          outTuple = inTuple;
     492        end if;
     493      then (outEq, outTuple);
     494  end match;
     495end createStats;
     496
     497protected function createStats2 "experimental function for create statistics"
     498  input DAE.Exp inExp;
     499  input tuple<HashTableExpToExp.HashTable, HashTableExpToIndex.HashTable> inTuple;
     500  output DAE.Exp outExp;
     501  output tuple<HashTableExpToExp.HashTable, HashTableExpToIndex.HashTable> outTuple;
     502algorithm
     503  (outExp, outTuple) := Expression.traverseExpBottomUp(inExp, createStats3, inTuple);
     504end createStats2;
     505
     506
     507protected function createStats3 "experimental function for create statistics"
     508  input DAE.Exp inExp;
     509  input tuple<HashTableExpToExp.HashTable, HashTableExpToIndex.HashTable> inTuple;
     510  output DAE.Exp outExp;
     511  output tuple<HashTableExpToExp.HashTable, HashTableExpToIndex.HashTable> outTuple;
     512algorithm
     513  (outExp, outTuple) := match(inExp, inTuple)
     514  local
     515    DAE.Exp key, value;
     516    Integer counter;
     517    HashTableExpToExp.HashTable HT;
     518    HashTableExpToIndex.HashTable HT2;
     519
     520    case (key, (HT,HT2)) equation
     521      if BaseHashTable.hasKey(key, HT2) then
     522        counter = BaseHashTable.get(key, HT2);
     523        counter = counter + 1;
     524        BaseHashTable.update((key, counter), HT2);
     525      else
     526        counter = 1;
     527        HT2 = BaseHashTable.add((key, counter), HT2);
     528      end if;
     529    then (inExp, (HT,HT2));
     530
     531  end match;
     532end createStats3;
     533
     534
     535protected function createReturnExp
     536  input DAE.Type inType;
     537  input Integer inIndex;
     538  input String inPrefix = "$cse";
     539  output DAE.Exp outExp;
     540  output Integer outIndex;
     541algorithm
     542  (outExp, outIndex) := match(inType)
     543    local
     544      Integer i;
     545      String str;
     546      DAE.Exp value;
     547      DAE.ComponentRef cr;
     548      list<DAE.Type> typeLst;
     549      list<DAE.Exp> expLst;
     550      list<DAE.ComponentRef> crefs;
     551      Absyn.Path path;
     552      list<DAE.Var> varLst;
     553      list<String> varNames;
     554
     555    case DAE.T_REAL() equation
     556      str = inPrefix + intString(inIndex);
     557      cr = DAE.CREF_IDENT(str, DAE.T_REAL_DEFAULT, {});
     558      value = DAE.CREF(cr, DAE.T_REAL_DEFAULT);
     559    then (value, inIndex + 1);
     560
     561    case DAE.T_INTEGER() equation
     562      str = inPrefix + intString(inIndex);
     563      cr = DAE.CREF_IDENT(str, DAE.T_INTEGER_DEFAULT, {});
     564      value = DAE.CREF(cr, DAE.T_INTEGER_DEFAULT);
     565    then (value, inIndex + 1);
     566
     567    case DAE.T_STRING() equation
     568      str = inPrefix + intString(inIndex);
     569      cr = DAE.CREF_IDENT(str, DAE.T_STRING_DEFAULT, {});
     570      value = DAE.CREF(cr, DAE.T_STRING_DEFAULT);
     571    then (value, inIndex + 1);
     572
     573    case DAE.T_BOOL() equation
     574      str = inPrefix + intString(inIndex);
     575      cr = DAE.CREF_IDENT(str, DAE.T_BOOL_DEFAULT, {});
     576      value = DAE.CREF(cr, DAE.T_BOOL_DEFAULT);
     577    then (value, inIndex + 1);
     578
     579    case DAE.T_CLOCK() equation
     580      str = inPrefix + intString(inIndex);
     581      cr = DAE.CREF_IDENT(str, DAE.T_CLOCK_DEFAULT, {});
     582      value = DAE.CREF(cr, DAE.T_CLOCK_DEFAULT);
     583    then (value, inIndex + 1);
     584
     585    case DAE.T_TUPLE(types=typeLst) equation
     586      (expLst, i) = List.mapFold(typeLst, function createReturnExp(inPrefix=inPrefix), inIndex);
     587      value = DAE.TUPLE(expLst);
     588    then (value, i);
     589
     590    // Expanding
     591    case DAE.T_ARRAY() equation
     592      str = inPrefix + intString(inIndex);
     593      cr = DAE.CREF_IDENT(str, inType, {});
     594      // crefs = ComponentReference.expandCref(cr, false);
     595      // expLst = List.map(crefs, Expression.crefExp);
     596      // value = DAE.ARRAY(inType, true, expLst);
     597          value = DAE.CREF(cr, inType);
     598    then (value, inIndex + 1);
     599
     600    // record types
     601    case DAE.T_COMPLEX(varLst=varLst, complexClassType=ClassInf.RECORD(path)) equation
     602      str = inPrefix + intString(inIndex);
     603      cr = DAE.CREF_IDENT(str, inType, {});       //inType?
     604      // crefs = ComponentReference.expandCref(cr, true);
     605      // expLst = List.map(crefs, Expression.crefExp);
     606      // varNames = List.map(varLst, Expression.varName);
     607      // value = DAE.RECORD(path, expLst, varNames, inType);
     608      // print("   DAE.T_COMPLEX \n");
     609          value = DAE.CREF(cr, inType);
     610    then (value, inIndex + 1);
     611
     612    else equation
     613      if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then
     614        print("  - createReturnExp failed for " + Types.printTypeStr(inType) + "\n");
     615      end if;
     616    then fail();
     617  end match;
     618end createReturnExp;
     619
     620protected function createVarsForExp     //cse in varList
     621  input DAE.Exp inExp;
     622  input list<BackendDAE.Var> inAccumVarLst;
     623  output list<BackendDAE.Var> outVarLst;
     624algorithm
     625  (outVarLst) := match (inExp)
     626    local
     627      DAE.ComponentRef cr, cr_;
     628      list<DAE.ComponentRef> crefs;
     629      list<DAE.Exp> expLst;
     630      BackendDAE.Var var;
     631      DAE.Type ty;
     632      DAE.InstDims arrayDim;
     633/*
     634    case DAE.CREF(componentRef=cr) guard(not Expression.isArrayType(Expression.typeof(inExp))
     635                                         and not Expression.isRecordType(Expression.typeof(inExp))) equation
     636      // use the correct type when creating var. The cref might have subs.
     637      var = BackendVariable.createCSEVar(cr, Expression.typeof(inExp));
     638    then var::inAccumVarLst;
     639*/
     640    case DAE.CREF(componentRef=cr, ty = DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(_))) algorithm
     641      // use the correct type when creating var. The cref might have subs.
     642      crefs := ComponentReference.expandCref(cr, true /*the way it is now we won't get records here. but if we do somehow expand them*/);
     643
     644      /* Create SimVars from the list of expanded crefs.*/
     645      /* Mark the first element as an arrayCref i.e. we have 'SOME(arraycref)' since this is how the C template
     646         detects first elements of arrays to generate VARNAME_indexed(..) macros for accessing the array
     647         with variable indexes.*/
     648      outVarLst := inAccumVarLst;
     649      for cr_ in crefs loop
     650        arrayDim := ComponentReference.crefDims(cr_);
     651        outVarLst := BackendVariable.createCSEArrayVar(cr_, ComponentReference.crefTypeFull(cr_), arrayDim)::outVarLst;
     652      end for;
     653    then outVarLst;
     654
     655    case DAE.CREF(componentRef=cr) guard(Expression.isArrayType(Expression.typeof(inExp))) algorithm
     656      // use the correct type when creating var. The cref might have subs.
     657      crefs := ComponentReference.expandCref(cr, true);
     658
     659      outVarLst := inAccumVarLst;
     660      ty := DAEUtil.expTypeElementType(Expression.typeof(inExp));
     661      for cr_ in crefs loop
     662        arrayDim := ComponentReference.crefDims(cr_);
     663        //expLst := DAE.CREF(cr_, ComponentReference.crefType(cr_))::expLst;
     664        outVarLst := BackendVariable.createCSEArrayVar(cr_, ty, arrayDim)::outVarLst;
     665      end for;
     666      //expLst = list(DAE.CREF(cr_, ComponentReference.crefType(cr_)) for cr_ in crefs);
     667    then outVarLst;
     668
     669    case DAE.CREF(componentRef=cr) equation
     670      // use the correct type when creating var. The cref might have subs.
     671      var = BackendVariable.createCSEVar(cr, Expression.typeof(inExp));
     672    then var::inAccumVarLst;
     673
     674    case DAE.TUPLE(expLst) equation
     675      outVarLst = List.fold(expLst, createVarsForExp, inAccumVarLst);
     676    then outVarLst;
     677
     678    case DAE.ARRAY(array=expLst) equation
     679      print("This should never appear\n");
     680      outVarLst = List.fold(expLst, createVarsForExp, inAccumVarLst);
     681    then outVarLst;
     682
     683    case DAE.RECORD(exps=expLst) equation
     684      print("This should never appear\n");
     685      outVarLst = List.fold(expLst, createVarsForExp, inAccumVarLst);
     686    then outVarLst;
     687
     688    // all other are failing cases
     689    else fail();
     690  end match;
     691end createVarsForExp;
    430692
    431693
     
    700962end checkOp;
    701963
    702 protected function createReturnExp
    703   input DAE.Type inType;
    704   input Integer inIndex;
    705   input String inPrefix = "$cse";
    706   output DAE.Exp outExp;
    707   output Integer outIndex;
    708 algorithm
    709   (outExp, outIndex) := match(inType)
    710     local
    711       Integer i;
    712       String str;
    713       DAE.Exp value;
    714       DAE.ComponentRef cr;
    715       list<DAE.Type> typeLst;
    716       list<DAE.Exp> expLst;
    717       list<DAE.ComponentRef> crefs;
    718       Absyn.Path path;
    719       list<DAE.Var> varLst;
    720       list<String> varNames;
    721 
    722     case DAE.T_REAL() equation
    723       str = inPrefix + intString(inIndex);
    724       cr = DAE.CREF_IDENT(str, DAE.T_REAL_DEFAULT, {});
    725       value = DAE.CREF(cr, DAE.T_REAL_DEFAULT);
    726     then (value, inIndex + 1);
    727 
    728     case DAE.T_INTEGER() equation
    729       str = inPrefix + intString(inIndex);
    730       cr = DAE.CREF_IDENT(str, DAE.T_INTEGER_DEFAULT, {});
    731       value = DAE.CREF(cr, DAE.T_INTEGER_DEFAULT);
    732     then (value, inIndex + 1);
    733 
    734     case DAE.T_STRING() equation
    735       str = inPrefix + intString(inIndex);
    736       cr = DAE.CREF_IDENT(str, DAE.T_STRING_DEFAULT, {});
    737       value = DAE.CREF(cr, DAE.T_STRING_DEFAULT);
    738     then (value, inIndex + 1);
    739 
    740     case DAE.T_BOOL() equation
    741       str = inPrefix + intString(inIndex);
    742       cr = DAE.CREF_IDENT(str, DAE.T_BOOL_DEFAULT, {});
    743       value = DAE.CREF(cr, DAE.T_BOOL_DEFAULT);
    744     then (value, inIndex + 1);
    745 
    746     case DAE.T_CLOCK() equation
    747       str = inPrefix + intString(inIndex);
    748       cr = DAE.CREF_IDENT(str, DAE.T_CLOCK_DEFAULT, {});
    749       value = DAE.CREF(cr, DAE.T_CLOCK_DEFAULT);
    750     then (value, inIndex + 1);
    751 
    752     case DAE.T_TUPLE(types=typeLst) equation
    753       (expLst, i) = List.mapFold(typeLst, function createReturnExp(inPrefix=inPrefix), inIndex);
    754       value = DAE.TUPLE(expLst);
    755     then (value, i);
    756 
    757     // Expanding
    758     case DAE.T_ARRAY() equation
    759       str = inPrefix + intString(inIndex);
    760       cr = DAE.CREF_IDENT(str, inType, {});
    761       // crefs = ComponentReference.expandCref(cr, false);
    762       // expLst = List.map(crefs, Expression.crefExp);
    763       // value = DAE.ARRAY(inType, true, expLst);
    764           value = DAE.CREF(cr, inType);
    765     then (value, inIndex + 1);
    766 
    767     // record types
    768     case DAE.T_COMPLEX(varLst=varLst, complexClassType=ClassInf.RECORD(path)) equation
    769       str = inPrefix + intString(inIndex);
    770       cr = DAE.CREF_IDENT(str, inType, {});       //inType?
    771       // crefs = ComponentReference.expandCref(cr, true);
    772       // expLst = List.map(crefs, Expression.crefExp);
    773       // varNames = List.map(varLst, Expression.varName);
    774       // value = DAE.RECORD(path, expLst, varNames, inType);
    775       // print("   DAE.T_COMPLEX \n");
    776           value = DAE.CREF(cr, inType);
    777     then (value, inIndex + 1);
    778 
    779     else equation
    780       if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then
    781         print("  - createReturnExp failed for " + Types.printTypeStr(inType) + "\n");
    782       end if;
    783     then fail();
    784   end match;
    785 end createReturnExp;
    786 
    787 protected function createVarsForExp     //cse in varList
    788   input DAE.Exp inExp;
    789   input list<BackendDAE.Var> inAccumVarLst;
    790   output list<BackendDAE.Var> outVarLst;
    791 algorithm
    792   (outVarLst) := match (inExp)
    793     local
    794       DAE.ComponentRef cr, cr_;
    795       list<DAE.ComponentRef> crefs;
    796       list<DAE.Exp> expLst;
    797       BackendDAE.Var var;
    798       DAE.Type ty;
    799       DAE.InstDims arrayDim;
    800 /*
    801     case DAE.CREF(componentRef=cr) guard(not Expression.isArrayType(Expression.typeof(inExp))
    802                                          and not Expression.isRecordType(Expression.typeof(inExp))) equation
    803       // use the correct type when creating var. The cref might have subs.
    804       var = BackendVariable.createCSEVar(cr, Expression.typeof(inExp));
    805     then var::inAccumVarLst;
    806 */
    807     case DAE.CREF(componentRef=cr, ty = DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(_))) algorithm
    808       // use the correct type when creating var. The cref might have subs.
    809       crefs := ComponentReference.expandCref(cr, true /*the way it is now we won't get records here. but if we do somehow expand them*/);
    810 
    811       /* Create SimVars from the list of expanded crefs.*/
    812       /* Mark the first element as an arrayCref i.e. we have 'SOME(arraycref)' since this is how the C template
    813          detects first elements of arrays to generate VARNAME_indexed(..) macros for accessing the array
    814          with variable indexes.*/
    815       outVarLst := inAccumVarLst;
    816       for cr_ in crefs loop
    817         arrayDim := ComponentReference.crefDims(cr_);
    818         outVarLst := BackendVariable.createCSEArrayVar(cr_, ComponentReference.crefTypeFull(cr_), arrayDim)::outVarLst;
    819       end for;
    820     then outVarLst;
    821 
    822     case DAE.CREF(componentRef=cr) guard(Expression.isArrayType(Expression.typeof(inExp))) algorithm
    823       // use the correct type when creating var. The cref might have subs.
    824       crefs := ComponentReference.expandCref(cr, true);
    825 
    826       outVarLst := inAccumVarLst;
    827       ty := DAEUtil.expTypeElementType(Expression.typeof(inExp));
    828       for cr_ in crefs loop
    829         arrayDim := ComponentReference.crefDims(cr_);
    830         //expLst := DAE.CREF(cr_, ComponentReference.crefType(cr_))::expLst;
    831         outVarLst := BackendVariable.createCSEArrayVar(cr_, ty, arrayDim)::outVarLst;
    832       end for;
    833       //expLst = list(DAE.CREF(cr_, ComponentReference.crefType(cr_)) for cr_ in crefs);
    834     then outVarLst;
    835 
    836     case DAE.CREF(componentRef=cr) equation
    837       // use the correct type when creating var. The cref might have subs.
    838       var = BackendVariable.createCSEVar(cr, Expression.typeof(inExp));
    839     then var::inAccumVarLst;
    840 
    841     case DAE.TUPLE(expLst) equation
    842       outVarLst = List.fold(expLst, createVarsForExp, inAccumVarLst);
    843     then outVarLst;
    844 
    845     case DAE.ARRAY(array=expLst) equation
    846       print("This should never appear\n");
    847       outVarLst = List.fold(expLst, createVarsForExp, inAccumVarLst);
    848     then outVarLst;
    849 
    850     case DAE.RECORD(exps=expLst) equation
    851       print("This should never appear\n");
    852       outVarLst = List.fold(expLst, createVarsForExp, inAccumVarLst);
    853     then outVarLst;
    854 
    855     // all other are failing cases
    856     else fail();
    857   end match;
    858 end createVarsForExp;
    859964
    860965protected function prepareExpForReplace
Note: See TracChangeset for help on using the changeset viewer.