Skip to content

Commit

Permalink
- add unique indices to sample calls
Browse files Browse the repository at this point in the history
  - this will help to replace the annoying helpVars - hopefully soon
  - equal sample-calls are getting the same index


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14784 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
lochel committed Jan 15, 2013
1 parent d43df4d commit 736bf96
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 69 deletions.
131 changes: 77 additions & 54 deletions Compiler/BackEnd/BackendDAECreate.mo
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ algorithm
System.realtimeTick(BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
Debug.execStat("Enter Backend", BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
functionTree := Env.getFunctionTree(inCache);
(DAE.DAE(elems), functionTree) := processDelayExpressions(lst, functionTree);
(DAE.DAE(elems), functionTree) := processBuiltinExpressions(lst, functionTree);
vars := BackendVariable.emptyVars();
knvars := BackendVariable.emptyVars();
extVars := BackendVariable.emptyVars();
Expand Down Expand Up @@ -458,6 +458,82 @@ algorithm
end match;
end lower3;

// =============================================================================
// section for processing builtin expressions
//
// Insert a unique index (starting with 1) before the first arguments of some
// builtin calls. Equal calls will get the same index.
// - delay(expr, delayTime, delayMax)
// => delay(index, expr, delayTime, delayMax)
// - sample(start, interval)
// => sample(index, start, interval)
// =============================================================================

protected function processBuiltinExpressions "function processBuiltinExpressions
author: lochel
Assign some builtin calls with a unique id argument."
input DAE.DAElist inDAE;
input DAE.FunctionTree functionTree;
output DAE.DAElist outDAE;
output DAE.FunctionTree outTree;
protected
HashTableExpToIndex.HashTable ht;
algorithm
ht := HashTableExpToIndex.emptyHashTable();
(outDAE, outTree, _) := DAEUtil.traverseDAE(inDAE, functionTree, transformBuiltinExpressions, (ht, 1, 1));
end processBuiltinExpressions;

protected function transformBuiltinExpressions "function transformBuiltinExpressions
author: lochel
Helper for processBuiltinExpressions"
input tuple<DAE.Exp, tuple<HashTableExpToIndex.HashTable, Integer, Integer>> itpl;
output tuple<DAE.Exp, tuple<HashTableExpToIndex.HashTable, Integer, Integer>> otpl;
protected
DAE.Exp e;
tuple<HashTableExpToIndex.HashTable, Integer, Integer> i;
algorithm
(e, i) := itpl;
otpl := Expression.traverseExp(e, transformBuiltinExpression, i);
end transformBuiltinExpressions;

protected function transformBuiltinExpression "function transformBuiltinExpression
author: lochel
Helper for transformBuiltinExpressions"
input tuple<DAE.Exp, tuple<HashTableExpToIndex.HashTable, Integer, Integer>> inTuple;
output tuple<DAE.Exp, tuple<HashTableExpToIndex.HashTable, Integer, Integer>> outTuple;
algorithm
outTuple := matchcontinue(inTuple)
local
DAE.Exp e;
list<DAE.Exp> es;
HashTableExpToIndex.HashTable ht;
Integer iDelay, iSample, i;
DAE.CallAttributes attr;

// delay [already in ht]
case ((e as DAE.CALL(Absyn.IDENT("delay"), es, attr), (ht, iDelay, iSample))) equation
i = BaseHashTable.get(e, ht);
then ((DAE.CALL(Absyn.IDENT("delay"), DAE.ICONST(i)::es, attr), (ht, iDelay, iSample)));

// delay [not yet in ht]
case ((e as DAE.CALL(Absyn.IDENT("delay"), es, attr), (ht, iDelay, iSample))) equation
ht = BaseHashTable.add((e, iDelay), ht);
then ((DAE.CALL(Absyn.IDENT("delay"), DAE.ICONST(iDelay)::es, attr), (ht, iDelay+1, iSample)));

// sample [already in ht]
case ((e as DAE.CALL(Absyn.IDENT("sample"), es, attr), (ht, iDelay, iSample))) equation
i = BaseHashTable.get(e, ht);
then ((DAE.CALL(Absyn.IDENT("sample"), DAE.ICONST(i)::es, attr), (ht, iDelay, iSample)));

// sample [not yet in ht]
case ((e as DAE.CALL(Absyn.IDENT("sample"), es, attr), (ht, iDelay, iSample))) equation
ht = BaseHashTable.add((e, iSample), ht);
then ((DAE.CALL(Absyn.IDENT("sample"), DAE.ICONST(iSample)::es, attr), (ht, iDelay, iSample+1)));

else inTuple;
end matchcontinue;
end transformBuiltinExpression;

/*
* lower all variables
*/
Expand Down Expand Up @@ -2680,63 +2756,10 @@ algorithm
end match;
end replaceableAlias;


/*
* other helping functions
*/

protected function processDelayExpressions
"Assign each call to delay() with a unique id argument"
input DAE.DAElist inDAE;
input DAE.FunctionTree functionTree;
output DAE.DAElist outDAE;
output DAE.FunctionTree outTree;
protected
HashTableExpToIndex.HashTable ht;
algorithm
ht := HashTableExpToIndex.emptyHashTableSized(100);
(outDAE,outTree,_) := DAEUtil.traverseDAE(inDAE, functionTree, transformDelayExpressions, (ht,0));
end processDelayExpressions;

protected function transformDelayExpressions
"Helper for processDelayExpressions()"
input tuple<DAE.Exp,tuple<HashTableExpToIndex.HashTable,Integer>> itpl;
output tuple<DAE.Exp,tuple<HashTableExpToIndex.HashTable,Integer>> otpl;
protected
DAE.Exp e;
tuple<HashTableExpToIndex.HashTable,Integer> i;
algorithm
(e,i) := itpl;
otpl := Expression.traverseExp(e, transformDelayExpression, i);
end transformDelayExpressions;

protected function transformDelayExpression
"Insert a unique index into the arguments of a delay() expression.
Repeat delay as maxDelay if not present."
input tuple<DAE.Exp, tuple<HashTableExpToIndex.HashTable,Integer>> inTuple;
output tuple<DAE.Exp, tuple<HashTableExpToIndex.HashTable,Integer>> outTuple;
algorithm
outTuple := matchcontinue(inTuple)
local
DAE.Exp e;
list<DAE.Exp> es;
HashTableExpToIndex.HashTable ht;
Integer i;
DAE.CallAttributes attr;
case ((e as DAE.CALL(Absyn.IDENT("delay"), es, attr), (ht,_)))
equation
i = BaseHashTable.get(e,ht);
then ((DAE.CALL(Absyn.IDENT("delay"), DAE.ICONST(i)::es, attr), (ht,i)));

case ((e as DAE.CALL(Absyn.IDENT("delay"), es, attr), (ht,i)))
equation
ht = BaseHashTable.add((e,i),ht);
then ((DAE.CALL(Absyn.IDENT("delay"), DAE.ICONST(i)::es, attr), (ht,i+1)));

else inTuple;
end matchcontinue;
end transformDelayExpression;

protected function detectImplicitDiscrete
"function: detectImplicitDiscrete
This function updates the variable kind to discrete
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/BackendDAEOptimize.mo
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ algorithm
(vlst,_::_)= BackendVariable.getVar(cr, knvars) "input variables stored in known variables are input on top level" ;
false = List.mapAllValueBool(vlst,toplevelInputOrUnfixed,false);
then ((e,false,(true,vars,knvars,b1,b2)));
case((e as DAE.CALL(path = Absyn.IDENT(name = "sample"), expLst = {_,_}), (_,vars,knvars,b1,b2))) then ((e,false,(true,vars,knvars,b1,b2)));
case((e as DAE.CALL(path = Absyn.IDENT(name = "sample"), expLst = {_,_,_}), (_,vars,knvars,b1,b2))) then ((e,false,(true,vars,knvars,b1,b2)));
case((e as DAE.CALL(path = Absyn.IDENT(name = "pre"), expLst = {_}), (_,vars,knvars,b1,b2))) then ((e,false,(true,vars,knvars,b1,b2)));
case((e as DAE.CALL(path = Absyn.IDENT(name = "change"), expLst = {_}), (_,vars,knvars,b1,b2))) then ((e,false,(true,vars,knvars,b1,b2)));
case((e as DAE.CALL(path = Absyn.IDENT(name = "edge"), expLst = {_}), (_,vars,knvars,b1,b2))) then ((e,false,(true,vars,knvars,b1,b2)));
Expand Down
9 changes: 6 additions & 3 deletions Compiler/BackEnd/SimCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2505,7 +2505,7 @@ algorithm
message = stringAppendList({"Inverse Algorithm needs to be solved for ", message, " in ", algStr, ". This has not been implemented yet.\n"});
Error.addMessage(Error.INTERNAL_ERROR, {message});
then fail();
/*

// algorithm for single variable.
case (_, _, BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=eqns), _, _, true, false, _, _)
equation
Expand Down Expand Up @@ -2545,7 +2545,6 @@ algorithm
message = stringAppendList({"Inverse Algorithm needs to be solved for ", message, " in ", algStr, ". This has not been implemented yet.\n"});
Error.addMessage(Error.INTERNAL_ERROR, {message});
then fail();
*/
end matchcontinue;
end createEquation;

Expand Down Expand Up @@ -2717,6 +2716,7 @@ algorithm
args_ = listAppend(args_,{DAE.ICONST(hindex)});
condition1 = DAE.CALL(name, args_, attr);
true = Expression.expEqual(condition1, e);

//s1 = ExpressionDump.printExpStr(condition1);
//s2 = ExpressionDump.printExpStr(e);
//print("Added Hindex for \n ** Condition: " +& s1 +& " HelpVar exp: " +& s2 +& " with index : " +& intString(hindex) +& "\n");
Expand Down Expand Up @@ -2774,12 +2774,14 @@ algorithm
Absyn.Path name;
list<DAE.Exp> args_;
DAE.CallAttributes attr;

case(condition, {}) then condition;
case (condition as DAE.CALL(path = name as Absyn.IDENT("sample"),expLst=args_,attr=attr), (hindex, e, _) :: restHelpVarInfo)
equation
args_ = listAppend(args_,{DAE.ICONST(hindex)});
condition1 = DAE.CALL(name, args_, attr);
true = Expression.expEqual(condition1, e);

//s1 = ExpressionDump.printExpStr(condition1);
//s2 = ExpressionDump.printExpStr(e);
//print("** matchwithHelpVar TRUE CASE** :\n Condition: " +& s1 +& " -- HelpVar Exp: " +& s2 +& "\n");
Expand Down Expand Up @@ -5319,8 +5321,8 @@ end createInitialMatrices;


// =============================================================================
// section with unsorted function
//
// Section with unsorted function
// TODO: clean up this section ;)
// =============================================================================

Expand Down Expand Up @@ -7840,6 +7842,7 @@ algorithm
nh = nh + 1;
args_ = listAppend(args_,{DAE.ICONST(nh)});
condition = DAE.CALL(name, args_, attr);

//Error.addMessage(Error.INTERNAL_ERROR, {"Could not find help var index for Sample condition"});
then ((condition,nh),{(nh,condition,-1)});
case (condition as DAE.CALL(path = name as Absyn.IDENT("sample"),expLst=args_,attr=attr), (hindex, e, _) :: restHelpVarInfo,_)
Expand Down
6 changes: 3 additions & 3 deletions Compiler/Template/CodegenC.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ template timeEventTpl(Integer index1, Exp relation, Text &varDecls /*BUFP*/)
<<
/* <%index1%> Not a time event */
>>
case CALL(path=IDENT(name="sample"), expLst={start, interval,_}) then
case CALL(path=IDENT(name="sample"), expLst={_, start, interval, _}) then
let &preExp = buffer "" /*BUFD*/
let e1 = daeExp(start, contextOther, &preExp /*BUFC*/, &varDecls /*BUFD*/)
let e2 = daeExp(interval, contextOther, &preExp /*BUFC*/, &varDecls /*BUFD*/)
Expand Down Expand Up @@ -1580,7 +1580,7 @@ template zeroCrossingTpl(Integer index1, Exp relation, Text &varDecls /*BUFP*/)
<%preExp%>
ZEROCROSSING(<%index1%>, <%e1%>?1:-1);
>>
case CALL(path=IDENT(name="sample"), expLst={start, interval}) then
case CALL(path=IDENT(name="sample"), expLst={_, start, interval, _}) then
<< >>
case CALL(path=IDENT(name="integer"), expLst={exp1, idx}) then
let &preExp = buffer "" /*BUFD*/
Expand Down Expand Up @@ -7292,7 +7292,7 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/, Text &varD
case CALL(path=IDENT(name="noEvent"), expLst={e1}) then
daeExp(e1, context, &preExp, &varDecls)

case CALL(path=IDENT(name="sample"), expLst={e1, e2, e3}) then
case CALL(path=IDENT(name="sample"), expLst={_, e1, e2, e3}) then
let tvar = tempDecl("modelica_boolean", &varDecls /*BUFD*/)
let var1 = daeExp(e1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
let var2 = daeExp(e2, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
Expand Down
4 changes: 2 additions & 2 deletions Compiler/Template/CodegenCSharp.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ template functionInitSample(list<SampleCondition> sampleConditions, SimCode simC
<<
/* <%zcIndex%> Not a time event */
>>
case CALL(path=IDENT(name="sample"), expLst={start, interval,_}) then
case CALL(path=IDENT(name="sample"), expLst={_, start, interval, _}) then
let &preExp = buffer "" /*BUFD*/
let startE = daeExp(start, contextOther, &preExp, simCode)
let intervalE = daeExp(interval, contextOther, &preExp, simCode)
Expand Down Expand Up @@ -777,7 +777,7 @@ template zeroCrossing(Exp zcExp, Integer zcIndex, SimCode simCode) ::=
<%preExp%>
gout[<%zcIndex%>] = (<%e1%>)?1:-1;
>>
case CALL(path=IDENT(name="sample"), expLst={start, interval}) then
case CALL(path=IDENT(name="sample"), expLst={_, start, interval, _}) then
let &preExp = buffer "" //is ignored
let eStart = daeExp(start, contextOther, &preExp, simCode)
let eInterval = daeExp(interval, contextOther, &preExp, simCode)
Expand Down
10 changes: 5 additions & 5 deletions Compiler/Template/CodegenCpp.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4669,7 +4669,7 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/,
let var1 = daeExp(arg, context, &preExp, &varDecls,simCode)
'_event_handling.pre(<%var1%>,"<%cref(arg.componentRef)%>")'

case CALL(path=IDENT(name="sample"), expLst={start, interval,index}) then
case CALL(path=IDENT(name="sample"), expLst={_, start, interval, index}) then
let &preExp = buffer "" /*BUFD*/
let eStart = daeExp(start, contextOther, &preExp, &varDecls /*BUFD*/,simCode)
let eInterval = daeExp(interval, contextOther, &preExp, &varDecls /*BUFD*/,simCode)
Expand Down Expand Up @@ -6149,7 +6149,7 @@ end timeEventcondition;
template timeEventcondition1(Integer index1, Exp relation, Text &varDecls /*BUFP*/,SimCode simCode)
::=
match relation
case CALL(path=IDENT(name="sample"), expLst={start, interval,index}) then
case CALL(path=IDENT(name="sample"), expLst={_, start, interval, index}) then
let &preExp = buffer "" /*BUFD*/
let eStart = daeExp(start, contextOther, &preExp, &varDecls /*BUFD*/,simCode)
let eInterval = daeExp(interval, contextOther, &preExp, &varDecls /*BUFD*/,simCode)
Expand Down Expand Up @@ -6189,7 +6189,7 @@ end resetTimeEvent;
template resetTimeEvent1(Integer index1, Exp relation, Text &varDecls /*BUFP*/,SimCode simCode)
::=
match relation
case CALL(path=IDENT(name="sample"), expLst={start, interval,index}) then
case CALL(path=IDENT(name="sample"), expLst={_, start, interval, index}) then
let &preExp = buffer "" /*BUFD*/
let eIndex = daeExp(index, contextOther, &preExp, &varDecls /*BUFD*/,simCode)
<<
Expand Down Expand Up @@ -6221,7 +6221,7 @@ end handleEvent1;
template handleEvent2(Integer index1, Exp relation, Text &varDecls /*BUFP*/,SimCode simCode)
::=
match relation
case CALL(path=IDENT(name="sample"), expLst={start, interval,index}) then
case CALL(path=IDENT(name="sample"), expLst={_, start, interval, index}) then
let &preExp = buffer "" /*BUFD*/
let eIndex = daeExp(index, contextOther, &preExp, &varDecls /*BUFD*/,simCode)
<<
Expand Down Expand Up @@ -6511,7 +6511,7 @@ end conditionvarSample;
template conditionvarSample1(Integer index1, Exp relation, Text &varDecls /*BUFP*/,SimCode simCode)
::=
match relation
case CALL(path=IDENT(name="sample"), expLst={start, interval,index}) then
case CALL(path=IDENT(name="sample"), expLst={_, start, interval, index}) then
let &preExp = buffer "" /*BUFD*/
let eIndex = daeExp(index, contextOther, &preExp, &varDecls /*BUFD*/,simCode)
<<
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Template/CodegenQSS.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ template generateCond(list<tuple<DAE.Exp, Integer>> conds, list<DAE.ComponentRef
list<DAE.ComponentRef> disc,list<DAE.ComponentRef> algs,Text &extraCode, Integer index)
::=
match conds
case ({(DAE.CALL(path=IDENT(name="sample"),expLst={start,interval,_}),_)}) then
case ({(DAE.CALL(path=IDENT(name="sample"),expLst={_,start,interval,_}),_)}) then
let &extraCode +=
<<
d[<% intAdd(index,1) %>] := pre(d[<% intAdd(index,1) %>]) + <%ExpressionDump.printExpStr(BackendQSS.replaceVars(interval,states,disc,algs)) %>;
Expand Down

0 comments on commit 736bf96

Please sign in to comment.