Changeset 0aff3d1 in OpenModelica
- Timestamp:
- 2016-04-04T10:59:00+02:00 (8 years ago)
- 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)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Compiler/BackEnd/CommonSubExpression.mo
re5d70b8c r0aff3d1 65 65 protected import Types; 66 66 67 public function wrapFunctionCalls 67 constant Boolean experimentalB = false; //Experimaental boolean for statistic hashtable 68 constant Boolean debug = false; 69 70 71 public function wrapFunctionCalls "author: Jan Hagemann and Lennart Ochel (FH Bielefeld, Germany) 72 main function: is called by postOpt and SymbolicJacobian" 68 73 input BackendDAE.BackendDAE inDAE; 69 74 output BackendDAE.BackendDAE outDAE; … … 76 81 DAE.FunctionTree functionTree; 77 82 HashTableExpToExp.HashTable HT; 78 Integer index=System.tmpTickIndex(Global.backendDAE_cseIndex), index2; 83 HashTableExpToIndex.HashTable HT2; 84 Integer index=System.tmpTickIndex(Global.backendDAE_cseIndex); 79 85 list<BackendDAE.Equation> eqList; 80 86 list<BackendDAE.Var> varList; 81 algorithm 87 88 algorithm 89 if debug then 90 print("\npost-optimization module wrapFunctionCalls (" + BackendDump.printBackendDAEType2String(inDAE.shared.backendDAEType) + "):\n\n"); 91 end if; 92 82 93 shared := inDAE.shared; 83 94 BackendDAE.SHARED(functionTree=functionTree) := shared; 95 HT := HashTableExpToExp.emptyHashTableSized(49999); //2053 4013 25343 536870879 96 HT2 := HashTableExpToIndex.emptyHashTableSized(49999); //2053 4013 25343 536870879 84 97 85 98 for syst in inDAE.eqs loop … … 87 100 orderedEqs := syst.orderedEqs; 88 101 102 // dump the EqSystem before the module works (debug dump +d=dumpCSE_verbose) 89 103 if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then 90 104 BackendDump.dumpEqSystem(syst, "################EQSYSTEM:###################"); 91 105 end if; 92 106 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 94 111 (orderedEqs, (HT, index, eqList, varList, _)) := BackendEquation.traverseEquationArray_WithUpdate(orderedEqs, wrapFunctionCalls2, (HT, index, {}, {}, functionTree)); 95 112 113 // dump of the hashtable (debug) 96 114 if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then 97 115 print("\n"); … … 99 117 end if; 100 118 119 // the module has to build a new matching 101 120 if not listEmpty(eqList) or not listEmpty(varList) then 102 121 syst.orderedEqs := BackendEquation.addEquations(eqList, orderedEqs); … … 106 125 syst.matching := BackendDAE.NO_MATCHING(); 107 126 127 // dump the equations & variables after the module (debug dump +d=dumpCSE or +d=dumpCSE_verbose) 108 128 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) + ") ###########"); 111 131 end if; 112 132 end if; … … 114 134 eqs := syst::eqs; 115 135 end for; 136 116 137 eqs := MetaModelica.Dangerous.listReverseInPlace(eqs); 117 118 138 System.tmpTickSetIndex(index, Global.backendDAE_cseIndex); 119 139 outDAE := BackendDAE.DAE(eqs, shared); 120 // TODO: change pre-opt module order121 //outDAE := SynchronousFeatures.clockPartitioning(BackendDAE.DAE({syst}, shared));122 140 end wrapFunctionCalls; 123 141 124 142 125 protected function wrapFunctionCalls2 143 protected function wrapFunctionCalls2 "helper function for wrapFunctionCalls; it traverses all equation of the EqSystem" 126 144 input BackendDAE.Equation inEq; 127 145 input tuple<HashTableExpToExp.HashTable, Integer, list<BackendDAE.Equation>, list<BackendDAE.Var>, DAE.FunctionTree> inTuple; … … 144 162 tuple<HashTableExpToExp.HashTable, Integer, list<BackendDAE.Equation>, list<BackendDAE.Var>, DAE.FunctionTree> tpl; 145 163 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 158 165 case BackendDAE.COMPLEX_EQUATION(left=left, right=right) equation 159 166 if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then 160 167 print("\ntraverse " + BackendDump.equationString(inEq) + " complex\n"); 161 168 end if; 169 162 170 if Expression.isPureCall(left) and Expression.isPureCall(right) then 163 171 (_, _, _ , _, functionTree) = inTuple; … … 200 208 tpl = (HT, index, eqList, varList, functionTree); 201 209 else 202 /* special case: complex equation is generated of resolveLopps and evalFunc;203 normally: this case is a "normal" equation: */204 210 eq = inEq; 205 211 tpl = inTuple; … … 207 213 then (eq, tpl); 208 214 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 221 216 case BackendDAE.EQUATION(exp=exp, scalar=scalar) equation 222 217 if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then 223 218 print("\ntraverse " + BackendDump.equationString(inEq) + " normal\n"); 224 219 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); 231 221 then (eq, tpl); 232 222 223 // case for all other equation: WHEN, IF ... 233 224 else equation 234 225 if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then … … 240 231 end wrapFunctionCalls2; 241 232 242 protected function wrapFunctionCalls3 233 234 protected 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; 241 algorithm 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; 273 end wrapFunctionCalls_advanced; 274 275 276 protected function wrapFunctionCalls3 "helper function: traverses all Expressions of the equation" 243 277 input DAE.Exp inExp; 244 278 input tuple<tuple<HashTableExpToExp.HashTable, Integer, list<BackendDAE.Equation>, list<BackendDAE.Var>, DAE.FunctionTree>, DAE.ElementSource> inTuple; … … 246 280 output tuple<tuple<HashTableExpToExp.HashTable, Integer, list<BackendDAE.Equation>, list<BackendDAE.Var>, DAE.FunctionTree>, DAE.ElementSource> outTuple; 247 281 algorithm 248 if Expression.isExpIfExp(inExp) then 282 if Expression.isExpIfExp(inExp) then //skip IfExp 249 283 outExp := inExp; 250 284 outTuple := inTuple; … … 254 288 end wrapFunctionCalls3; 255 289 256 protected function wrapFunctionCalls_main 290 291 protected function wrapFunctionCalls_main "helper function: traverses all Expressions from Buttom Up" 257 292 input DAE.Exp inExp; 258 293 input tuple<tuple<HashTableExpToExp.HashTable, Integer, list<BackendDAE.Equation>, list<BackendDAE.Var>, DAE.FunctionTree>, DAE.ElementSource> inTuple; … … 260 295 output tuple<tuple<HashTableExpToExp.HashTable, Integer, list<BackendDAE.Equation>, list<BackendDAE.Var>, DAE.FunctionTree>, DAE.ElementSource> outTuple; 261 296 algorithm 262 if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then263 print("Exp: " + ExpressionDump.dumpExpStr(inExp, 0) + "\n");264 end if;265 266 297 (outExp, outTuple) := matchcontinue(inExp, inTuple) 267 298 local … … 276 307 DAE.FunctionTree functionTree; 277 308 278 // skip some special function calls279 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 338 309 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; 345 312 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; 347 328 end if; 348 if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then349 print(" Exp_sub: " + ExpressionDump.printExpStr(value) + "\n");350 end if;351 329 then (value, ((HT, index, eqList, varList, functionTree), source)); 352 330 … … 355 333 end wrapFunctionCalls_main; 356 334 357 protected function isRecordExp 335 336 protected function isSkipCase "outline all skip cases" 337 input DAE.Exp inCall; 338 output Boolean outB; 339 algorithm 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; 372 end isSkipCase; 373 374 375 protected function isRecordExp "helper function for records in complex-equation" 358 376 input DAE.Exp inExp; 359 377 output Boolean outRecord; … … 366 384 end isRecordExp; 367 385 368 protected function isZeroequalCall 386 387 protected function isCallEqualCref "helper function for creates statistics" 369 388 input DAE.Exp inExp; 370 389 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; 391 algorithm 392 outBoolean := match(inExp,inExp2) 393 case (DAE.CREF(),DAE.CALL()) then true; 394 case (DAE.CALL(),DAE.CREF()) then true; 384 395 else false; 385 396 end match; 386 end isZeroequalCall; 387 388 protected function expand 397 end isCallEqualCref; 398 399 400 protected function expand "" 389 401 input list<DAE.Exp> inExpLst1; 390 402 input list<DAE.Exp> inExpLst2; … … 399 411 list<BackendDAE.Equation> eqList; 400 412 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::{}) equation408 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) equation413 eqList = expand(expLst1, expLst2, inEqList);414 then eqList;415 416 case (left::expLst1, (right as DAE.CREF(componentRef=DAE.WILD()))::expLst2) equation417 eqList = expand(expLst1, expLst2, inEqList);418 then eqList;419 420 case (left::expLst1, right::expLst2) equation421 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; 425 437 end match; 426 438 end expand; 427 439 428 440 429 441 protected 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; 446 algorithm 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; 495 end createStats; 496 497 protected 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; 502 algorithm 503 (outExp, outTuple) := Expression.traverseExpBottomUp(inExp, createStats3, inTuple); 504 end createStats2; 505 506 507 protected 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; 512 algorithm 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; 532 end createStats3; 533 534 535 protected 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; 541 algorithm 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; 618 end createReturnExp; 619 620 protected function createVarsForExp //cse in varList 621 input DAE.Exp inExp; 622 input list<BackendDAE.Var> inAccumVarLst; 623 output list<BackendDAE.Var> outVarLst; 624 algorithm 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; 691 end createVarsForExp; 430 692 431 693 … … 700 962 end checkOp; 701 963 702 protected function createReturnExp703 input DAE.Type inType;704 input Integer inIndex;705 input String inPrefix = "$cse";706 output DAE.Exp outExp;707 output Integer outIndex;708 algorithm709 (outExp, outIndex) := match(inType)710 local711 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() equation723 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() equation729 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() equation735 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() equation741 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() equation747 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) equation753 (expLst, i) = List.mapFold(typeLst, function createReturnExp(inPrefix=inPrefix), inIndex);754 value = DAE.TUPLE(expLst);755 then (value, i);756 757 // Expanding758 case DAE.T_ARRAY() equation759 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 types768 case DAE.T_COMPLEX(varLst=varLst, complexClassType=ClassInf.RECORD(path)) equation769 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 equation780 if Flags.isSet(Flags.DUMP_CSE_VERBOSE) then781 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 varList788 input DAE.Exp inExp;789 input list<BackendDAE.Var> inAccumVarLst;790 output list<BackendDAE.Var> outVarLst;791 algorithm792 (outVarLst) := match (inExp)793 local794 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))) equation803 // 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(_))) algorithm808 // 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 template813 detects first elements of arrays to generate VARNAME_indexed(..) macros for accessing the array814 with variable indexes.*/815 outVarLst := inAccumVarLst;816 for cr_ in crefs loop817 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))) algorithm823 // 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 loop829 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) equation837 // 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) equation842 outVarLst = List.fold(expLst, createVarsForExp, inAccumVarLst);843 then outVarLst;844 845 case DAE.ARRAY(array=expLst) equation846 print("This should never appear\n");847 outVarLst = List.fold(expLst, createVarsForExp, inAccumVarLst);848 then outVarLst;849 850 case DAE.RECORD(exps=expLst) equation851 print("This should never appear\n");852 outVarLst = List.fold(expLst, createVarsForExp, inAccumVarLst);853 then outVarLst;854 855 // all other are failing cases856 else fail();857 end match;858 end createVarsForExp;859 964 860 965 protected function prepareExpForReplace
Note: See TracChangeset
for help on using the changeset viewer.