Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
RuedKamp committed Jun 22, 2015
2 parents 18b5e7e + d084a77 commit b073360
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 223 deletions.
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
@@ -0,0 +1,3 @@
# How to contribute to the OpenModelica Compiler

See [OpenModelica.git](https://github.com/OpenModelica/OpenModelica/blob/master/CONTRIBUTING.md).
115 changes: 48 additions & 67 deletions Compiler/BackEnd/FindZeroCrossings.mo
Expand Up @@ -236,8 +236,7 @@ algorithm
end match;
end encapsulateWhenConditions_WhenClause;

protected function encapsulateWhenConditions_EqSystem "author: lochel
This is a helper function for encapsulateWhenConditions."
protected function encapsulateWhenConditions_EqSystem "author: lochel"
input BackendDAE.EqSystem inEqSystem;
input Integer inIndex;
input HashTableExpToIndex.HashTable inHT;
Expand All @@ -262,8 +261,7 @@ algorithm
outEqSystem := BackendDAE.EQSYSTEM(orderedVars, orderedEqs, NONE(), NONE(), BackendDAE.NO_MATCHING(), stateSets, partitionKind);
end encapsulateWhenConditions_EqSystem;

protected function encapsulateWhenConditions_Equation "author: lochel
This is a helper function for encapsulateWhenConditions_EqSystem."
protected function encapsulateWhenConditions_Equation "author: lochel"
input BackendDAE.Equation inEq;
input tuple<BackendDAE.EquationArray, list<BackendDAE.Var>, list<BackendDAE.Equation>, Integer, HashTableExpToIndex.HashTable> inTpl;
output BackendDAE.Equation outEq;
Expand Down Expand Up @@ -332,8 +330,7 @@ algorithm
end match;
end encapsulateWhenConditions_Equation;

protected function encapsulateWhenConditions_Equations "author: lochel
This is a helper function for encapsulateWhenConditions_Equation."
protected function encapsulateWhenConditions_Equations "author: lochel"
input BackendDAE.WhenEquation inWhenEquation;
input DAE.ElementSource inSource;
input Integer inIndex;
Expand Down Expand Up @@ -378,8 +375,7 @@ algorithm
end matchcontinue;
end encapsulateWhenConditions_Equations;

protected function encapsulateWhenConditions_Equations1 "author: lochel
This is a helper function for encapsulateWhenConditions_Equations."
protected function encapsulateWhenConditions_Equations1 "author: lochel"
input DAE.Exp inCondition;
input DAE.ElementSource inSource;
input Integer inIndex;
Expand Down Expand Up @@ -442,8 +438,7 @@ algorithm
end matchcontinue;
end encapsulateWhenConditions_Equations1;

protected function encapsulateWhenConditions_EquationsWithArrayConditions "author: lochel
This is a helper function for encapsulateWhenConditions_Equations1."
protected function encapsulateWhenConditions_EquationsWithArrayConditions "author: lochel"
input list<DAE.Exp> inConditionList;
input DAE.ElementSource inSource;
input Integer inIndex;
Expand Down Expand Up @@ -481,8 +476,7 @@ algorithm
end matchcontinue;
end encapsulateWhenConditions_EquationsWithArrayConditions;

protected function encapsulateWhenConditions_Algorithms "author: lochel
This is a helper function for encapsulateWhenConditions_Equation."
protected function encapsulateWhenConditions_Algorithms "author: lochel"
input list<DAE.Statement> inStmts;
input list<BackendDAE.Var> inVars;
input Integer inIndex;
Expand Down Expand Up @@ -573,8 +567,7 @@ algorithm
end matchcontinue;
end encapsulateWhenConditions_Algorithms;

protected function encapsulateWhenConditions_Algorithms1 "author: lochel
This is a helper function for encapsulateWhenConditions_Equations."
protected function encapsulateWhenConditions_Algorithms1 "author: lochel"
input DAE.Exp inCondition;
input DAE.ElementSource inSource;
input Integer inIndex;
Expand Down Expand Up @@ -636,8 +629,7 @@ algorithm
end matchcontinue;
end encapsulateWhenConditions_Algorithms1;

protected function encapsulateWhenConditions_AlgorithmsWithArrayConditions "author: lochel
This is a helper function for encapsulateWhenConditions_Algorithms1."
protected function encapsulateWhenConditions_AlgorithmsWithArrayConditions "author: lochel"
input list<DAE.Exp> inConditionList;
input DAE.ElementSource inSource;
input Integer inIndex;
Expand Down Expand Up @@ -999,9 +991,8 @@ algorithm
(eres, ((outZeroCrossings, outrelationsinZC, outSamplesLst, outCountRelations, outCountMathFunctions), _)) := Expression.traverseExpTopDown(e, collectZC, ((inZeroCrossings, inrelationsinZC, inSamplesLst, incountRelations, incountMathFunctions), (counteq, countwc, vars, knvars)));
end findZeroCrossings3;

protected function collectZC "author: unknown
modified: 2011-01 by wbraun
Collects zero crossings in equations"
protected function collectZC
"Collects zero crossings in equations"
input DAE.Exp inExp;
input tuple<tuple<list<BackendDAE.ZeroCrossing>, list<BackendDAE.ZeroCrossing>, list<BackendDAE.ZeroCrossing>, Integer, Integer>, tuple<Integer, Integer, BackendDAE.Variables, BackendDAE.Variables>> inTpl;
output DAE.Exp outExp;
Expand Down Expand Up @@ -1209,9 +1200,8 @@ algorithm
end matchcontinue;
end collectZC;

protected function collectZCAlgsFor "Collects zero crossings in for loops
added: 2011-01 by wbraun
lochel: merged this with function collectZCAlgs"
protected function collectZCAlgsFor
"Collects zero crossings in for loops"
input DAE.Exp inExp;
input tuple<DAE.Exp, list<DAE.Exp>, DAE.Exp, tuple<list<BackendDAE.ZeroCrossing>, list<BackendDAE.ZeroCrossing>, list<BackendDAE.ZeroCrossing>, Integer, Integer>, tuple<Integer, BackendDAE.Variables, BackendDAE.Variables>> inTpl;
output DAE.Exp outExp;
Expand Down Expand Up @@ -1246,7 +1236,8 @@ algorithm
eqs = {alg_indx};
zc = createZeroCrossing(inExp, eqs, {});
samples = listAppend(samples, {zc});
samples = mergeZeroCrossings(samples, {});
// lochel: don't merge zero crossings in algorithms (see #3358)
// samples = mergeZeroCrossings(samples, {});
if Flags.isSet(Flags.RELIDX) then
print("sample index algotihm: " + intString(alg_indx) + "\n");
end if;
Expand All @@ -1269,7 +1260,8 @@ algorithm
(explst, itmp) = replaceIteratorWithStaticValues(e_1, iterator, inExpLst, numRelations);
zc_lst = createZeroCrossings(explst, {alg_indx}, {});
zc_lst = listAppend(zeroCrossings, zc_lst);
zc_lst = mergeZeroCrossings(zc_lst, {});
// lochel: don't merge zero crossings in algorithms (see #3358)
// zc_lst = mergeZeroCrossings(zc_lst, {});
itmp = (listLength(zc_lst)-listLength(zeroCrossings));
zeroCrossings = if itmp>0 then zc_lst else zeroCrossings;
if Flags.isSet(Flags.RELIDX) then
Expand Down Expand Up @@ -1316,7 +1308,8 @@ algorithm
(explst, itmp) = replaceIteratorWithStaticValues(e_1, iterator, inExpLst, numRelations1);
zc_lst = createZeroCrossings(explst, {alg_indx}, {});
zc_lst = listAppend(zeroCrossings, zc_lst);
zc_lst = mergeZeroCrossings(zc_lst, {});
// lochel: don't merge zero crossings in algorithms (see #3358)
// zc_lst = mergeZeroCrossings(zc_lst, {});
itmp = (listLength(zc_lst)-listLength(zeroCrossings));
zeroCrossings = if itmp>0 then zc_lst else zeroCrossings;
if Flags.isSet(Flags.RELIDX) then
Expand Down Expand Up @@ -1358,14 +1351,15 @@ algorithm
stepvalue = Util.getOptionOrDefault(stepvalueopt, DAE.ICONST(1));
istart = BackendDAEUtil.expInt(startvalue, knvars);
istep = BackendDAEUtil.expInt(stepvalue, knvars);
e_1 = DAE.RELATION(e1, op, e2, numRelations, SOME((iterator, istart, istep)));
eres = DAE.RELATION(e1, op, e2, numRelations, SOME((iterator, istart, istep)));
(explst, itmp) = replaceIteratorWithStaticValues(inExp, iterator, inExpLst, numRelations);
if Flags.isSet(Flags.RELIDX) then
print(" number of new zc: " + intString(listLength(explst)) + "\n");
end if;
zcLstNew = createZeroCrossings(explst, {alg_indx}, {});
zc_lst = listAppend(relations, zcLstNew);
zc_lst = mergeZeroCrossings(zc_lst, {});
// lochel: don't merge zero crossings in algorithms (see #3358)
// zc_lst = mergeZeroCrossings(zc_lst, {});
if Flags.isSet(Flags.RELIDX) then
print(" number of new zc: " + intString(listLength(zc_lst)) + "\n");
end if;
Expand All @@ -1374,9 +1368,9 @@ algorithm
print(" itmp: " + intString(itmp) + "\n");
end if;
numRelations = intAdd(itmp, numRelations);
eres = if itmp>0 then e_1 else inExp;
zeroCrossings = listAppend(zeroCrossings, zcLstNew);
zeroCrossings = mergeZeroCrossings(zeroCrossings, {});
// lochel: don't merge zero crossings in algorithms (see #3358)
// zeroCrossings = mergeZeroCrossings(zeroCrossings, {});
if Flags.isSet(Flags.RELIDX) then
print("collectZCAlgsFor result zc: " + ExpressionDump.printExpStr(eres)+ " index:" + intString(numRelations) + "\n");
end if;
Expand All @@ -1387,15 +1381,16 @@ algorithm
b1 = Expression.expContains(e1, iterator);
b2 = Expression.expContains(e2, iterator);
false = Util.boolOrList({b1, b2});
e_1 = DAE.RELATION(e1, op, e2, numRelations, NONE());
zc = createZeroCrossing(e_1, {alg_indx}, {});
eres = DAE.RELATION(e1, op, e2, numRelations, NONE());
zc = createZeroCrossing(eres, {alg_indx}, {});
zc_lst = listAppend(relations, {zc});
zc_lst = mergeZeroCrossings(zc_lst, {});
// lochel: don't merge zero crossings in algorithms (see #3358)
// zc_lst = mergeZeroCrossings(zc_lst, {});
itmp = (listLength(zc_lst)-listLength(relations));
numRelations = numRelations + itmp;
eres = if itmp>0 then e_1 else inExp;
zeroCrossings = listAppend(zeroCrossings, {zc});
zeroCrossings = mergeZeroCrossings(zeroCrossings, {});
// lochel: don't merge zero crossings in algorithms (see #3358)
// zeroCrossings = mergeZeroCrossings(zeroCrossings, {});
if Flags.isSet(Flags.RELIDX) then
print("collectZCAlgsFor result zc: " + ExpressionDump.printExpStr(eres)+ " index:" + intString(numRelations) + "\n");
end if;
Expand Down Expand Up @@ -1500,16 +1495,15 @@ algorithm
end matchcontinue;
end collectZCAlgsFor;

protected function replaceIteratorWithStaticValues "
Helper function for collectZCAlgFor "
protected function replaceIteratorWithStaticValues
input DAE.Exp inExp;
input DAE.Exp inIterator;
input list<DAE.Exp> inExpLst;
input Integer inIndex;
output list<DAE.Exp> outZeroCrossings;
output Integer outIndex;
algorithm
(outZeroCrossings, outIndex) := matchcontinue(inExp, inExpLst)
(outZeroCrossings, outIndex) := match(inExp, inExpLst)
local
DAE.Exp e, e1, e2, res1, e_1;
DAE.Operator op;
Expand Down Expand Up @@ -1543,11 +1537,10 @@ algorithm
else equation
Error.addInternalError("function replaceIteratorWithStaticValues failed", sourceInfo());
then fail();
end matchcontinue;
end match;
end replaceIteratorWithStaticValues;

protected function zcIndex "
"
protected function zcIndex
input DAE.Exp inRelation;
input Integer inIndex;
input list<BackendDAE.ZeroCrossing> inZeroCrossings;
Expand Down Expand Up @@ -1599,7 +1592,7 @@ protected function mergeZeroCrossings "
output list<BackendDAE.ZeroCrossing> outZeroCrossingLst;
algorithm
//BackendDump.dumpZeroCrossingList(inZeroCrossingLst, "mergeZeroCrossings input:");
outZeroCrossingLst := matchcontinue (inZeroCrossingLst)
outZeroCrossingLst := match (inZeroCrossingLst)
local
BackendDAE.ZeroCrossing zc, same_1;
list<BackendDAE.ZeroCrossing> samezc, diff, res, xs;
Expand All @@ -1612,37 +1605,25 @@ algorithm
same_1 = List.fold(samezc, mergeZeroCrossing, zc);
res = mergeZeroCrossings(diff, same_1::inAccum);
then res;

else equation
Error.addInternalError("function mergeZeroCrossings failed", sourceInfo());
then fail();
end matchcontinue;
end match;
end mergeZeroCrossings;

protected function mergeZeroCrossing "
Merges two zero crossings into one by makeing the union of the lists of
equaions and when clauses they appear in.
modified: 2011-01 by wbraun
merge to ZeroCrosssing with the lowest index"
equations and when clauses they appear in."
input BackendDAE.ZeroCrossing inZeroCrossing1;
input BackendDAE.ZeroCrossing inZeroCrossing2;
output BackendDAE.ZeroCrossing outZeroCrossing;
protected
list<Integer> eq, wc, eq1, wc1, eq2, wc2;
DAE.Exp e1, e2, res;
algorithm
outZeroCrossing := matchcontinue (inZeroCrossing1, inZeroCrossing2)
local
list<Integer> eq, wc, eq1, wc1, eq2, wc2;
DAE.Exp e1, e2, res;

case (BackendDAE.ZERO_CROSSING(relation_=e1, occurEquLst=eq1, occurWhenLst=wc1), BackendDAE.ZERO_CROSSING(relation_=e2, occurEquLst=eq2, occurWhenLst=wc2)) equation
res = getMinZeroCrossings(e1, e2);
eq = List.union(eq1, eq2);
wc = List.union(wc1, wc2);
then BackendDAE.ZERO_CROSSING(res, eq, wc);

case (BackendDAE.ZERO_CROSSING(relation_=e1), BackendDAE.ZERO_CROSSING(relation_=e2)) equation
Error.addInternalError("function mergeZeroCrossing failed for " + ExpressionDump.printExpStr(e1) + " and " + ExpressionDump.printExpStr(e2), sourceInfo());
then fail();
end matchcontinue;
BackendDAE.ZERO_CROSSING(relation_=e1, occurEquLst=eq1, occurWhenLst=wc1) := inZeroCrossing1;
BackendDAE.ZERO_CROSSING(relation_=e2, occurEquLst=eq2, occurWhenLst=wc2) := inZeroCrossing2;
res := getMinZeroCrossings(e1, e2);
eq := List.union(eq1, eq2);
wc := List.union(wc1, wc2);
outZeroCrossing := BackendDAE.ZERO_CROSSING(res, eq, wc);
end mergeZeroCrossing;

protected function getMinZeroCrossings "
Expand All @@ -1651,7 +1632,7 @@ protected function getMinZeroCrossings "
input DAE.Exp inZCexp2;
output DAE.Exp outMinZC;
algorithm
outMinZC := matchcontinue (inZCexp1, inZCexp2)
outMinZC := match (inZCexp1, inZCexp2)
local
DAE.Exp e1, e2, e3, e4, res, res2;
DAE.Operator op;
Expand All @@ -1677,9 +1658,9 @@ algorithm
then inZCexp2;

else equation
Error.addInternalError("function getMinZeroCrossings failed for " + ExpressionDump.printExpStr(inZCexp1) + " and " + ExpressionDump.printExpStr(inZCexp2), sourceInfo());
Error.addInternalError("function getMinZeroCrossings failed for {" + ExpressionDump.printExpStr(inZCexp1) + "} and {" + ExpressionDump.printExpStr(inZCexp2) + "}", sourceInfo());
then fail();
end matchcontinue;
end match;
end getMinZeroCrossings;

protected function zcEqual "
Expand Down
27 changes: 14 additions & 13 deletions Compiler/FrontEnd/ConnectUtil.mo
Expand Up @@ -3693,37 +3693,38 @@ protected function sizeOfVariable2
input DAE.Type inType;
output Integer outSize;
algorithm
outSize := match(inType)
outSize := match inType
local
Integer n;
DAE.Type t;
list<DAE.Var> v;

// Scalar values consist of one element.
case (DAE.T_INTEGER(_, _)) then 1;
case (DAE.T_REAL(_, _)) then 1;
case (DAE.T_STRING(_, _)) then 1;
case (DAE.T_BOOL(_, _)) then 1;
case (DAE.T_ENUMERATION(index = NONE())) then 1;
case DAE.T_INTEGER() then 1;
case DAE.T_REAL() then 1;
case DAE.T_STRING() then 1;
case DAE.T_BOOL() then 1;
case DAE.T_ENUMERATION(index = NONE()) then 1;
// The size of an array is its dimension multiplied with the size of its type.
case (DAE.T_ARRAY(dims = {DAE.DIM_INTEGER(integer = n)}, ty = t))
then n * sizeOfVariable2(t);
case DAE.T_ARRAY()
then intMul(Expression.dimensionSize(dim) for dim in inType.dims) *
sizeOfVariable2(inType.ty);
// The size of a complex type without an equalityConstraint (such as a
// record), is the sum of the sizes of its components.
case (DAE.T_COMPLEX(varLst = v, equalityConstraint = NONE()))
case DAE.T_COMPLEX(varLst = v, equalityConstraint = NONE())
then sizeOfVariableList(v);
// The size of a complex type with an equalityConstraint function is
// determined by the size of the return value of that function.
case (DAE.T_COMPLEX(equalityConstraint = SOME((_, n, _)))) then n;
case DAE.T_COMPLEX(equalityConstraint = SOME((_, n, _))) then n;
// The size of a basic subtype with equality constraint is ZERO.
case (DAE.T_SUBTYPE_BASIC(equalityConstraint = SOME(_))) then 0;
case DAE.T_SUBTYPE_BASIC(equalityConstraint = SOME(_)) then 0;
// The size of a basic subtype is the size of the extended type.
case (DAE.T_SUBTYPE_BASIC(complexType = t)) then sizeOfVariable2(t);
case DAE.T_SUBTYPE_BASIC(complexType = t) then sizeOfVariable2(t);
// Anything we forgot?
case t
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.traceln("- Inst.sizeOfVariable failed on " + Types.printTypeStr(t));
Debug.traceln("- ConnectUtil.sizeOfVariable failed on " + Types.printTypeStr(t));
then
fail();
end match;
Expand Down

0 comments on commit b073360

Please sign in to comment.