Skip to content

Commit

Permalink
Handle bootstrapping DoubleEndedList.mo
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed Mar 8, 2016
1 parent 3094539 commit 0e2f188
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Compiler/Script/CevalScript.mo
Expand Up @@ -2123,7 +2123,7 @@ algorithm
path := Absyn.joinPaths(pathPrefix, Absyn.IDENT(name));
paths := if SCode.isFunction(elt) then path::acc else acc;
pathsMetarecord := match elt
case SCode.CLASS(restriction=SCode.R_METARECORD(moved = true)) then path::accMetarecord;
case SCode.CLASS(restriction=SCode.R_METARECORD()) then path::accMetarecord;
else accMetarecord;
end match;
elements := getNonPartialElementsForInstantiatedClass(sp, elt, path);
Expand Down
81 changes: 20 additions & 61 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -79,6 +79,7 @@ import DAEDump;
import DAEUtil;
import Debug;
import Differentiate;
import DoubleEndedList;
import Error;
import EvaluateFunctions;
import Expression;
Expand Down Expand Up @@ -11029,41 +11030,6 @@ algorithm
b := listMember(bEq,bEq1);
end findBEqs;

/*
This is wrong!
public function getSimVarByIndex
input Integer idx;
input SimCodeVar.SimVars allSimVars;
output SimCodeVar.SimVar simVar;
algorithm
simVar := matchcontinue(idx,allSimVars)
local
Integer size,idx2;
list<SimCodeVar.SimVar> stateVars,algVars;
SimCodeVar.SimVar var;
case(_,SimCodeVar.SIMVARS(stateVars=stateVars,algVars=algVars))
equation
size = listLength(stateVars);
true = idx > size;
//its not a stateVar
idx2 = idx - 2*size + 1;
var = listGet(algVars,idx2);
then var;
case(_,SimCodeVar.SIMVARS(stateVars=stateVars,algVars=algVars))
equation
size = listLength(stateVars);
true = idx <= size;
//its a stateVar
var = listGet(stateVars,idx);
then var;
else
equation
print("SimCodeUtil.getSimVarByIndex failed!\n");
then fail();
end matchcontinue;
end getSimVarByIndex;
*/

public function getAssignedCrefsOfSimEq"gets the crefs of the vars that are assigned (the lhs) of the simEqSystems
author:Waurich TUD 2014-05"
input Integer idx;
Expand All @@ -11079,7 +11045,7 @@ algorithm
equation
simEqSyst = List.getMemberOnTrue(idx,allEqs,indexIsEqual);
crefs = getSimEqSystemCrefsLHS(simEqSyst);
then crefs;
then crefs;
end match;
end getAssignedCrefsOfSimEq;

Expand All @@ -11098,17 +11064,15 @@ algorithm
case(SimCode.SES_RESIDUAL())
equation
print("implement SES_RESIDUAL in SimCodeUtil.getSimEqSystemCrefsLHS!\n");
then {};
then {};
case(SimCode.SES_SIMPLE_ASSIGN(cref=cref))
equation
then {cref};
then {cref};
case(SimCode.SES_ARRAY_CALL_ASSIGN(lhs=lhs))
equation
then {Expression.expCref(lhs)};
then {Expression.expCref(lhs)};
case(SimCode.SES_IFEQUATION())
equation
print("implement SES_IFEQUATION in SimCodeUtil.getSimEqSystemCrefsLHS!\n");
then {};
then {};
case(SimCode.SES_ALGORITHM()) equation
print("implement SES_ALGORITHM in SimCodeUtil.getSimEqSystemCrefsLHS!\n");
then {};
Expand All @@ -11117,20 +11081,15 @@ algorithm
then {};
case(SimCode.SES_LINEAR(SimCode.LINEARSYSTEM(vars=simVars,residual=residual)))
equation
crefs2 = List.flatten(List.map(residual,getSimEqSystemCrefsLHS));
crefs = list(SimCodeFunctionUtil.varName(v) for v in simVars);
crefs = listAppend(crefs,crefs2);
then crefs;
crefs = List.flatten(List.map(residual,getSimEqSystemCrefsLHS));
crefs2 = list(SimCodeFunctionUtil.varName(v) for v in simVars);
then listAppend(crefs2,crefs2);
case(SimCode.SES_NONLINEAR(SimCode.NONLINEARSYSTEM(crefs=crefs)))
equation
then crefs;
case(SimCode.SES_MIXED(discVars=simVars))
equation
crefs = list(SimCodeFunctionUtil.varName(v) for v in simVars);
then crefs;
case(SimCode.SES_MIXED(discVars=simVars))
then list(SimCodeFunctionUtil.varName(v) for v in simVars);
case(SimCode.SES_WHEN(whenStmtLst={BackendDAE.ASSIGN(left=cref)}))
equation
then {cref};
then {cref};
end match;
end getSimEqSystemCrefsLHS;

Expand Down Expand Up @@ -11600,10 +11559,11 @@ protected
list<tuple<DAE.ComponentRef, list<DAE.ComponentRef>>> spTA, spTB;
list<tuple<Integer, list<Integer>>> sparseInts;
list<SimCode.FmiUnknown> derivatives, outputs, discreteStates;
list<SimCodeVar.SimVar> varsA, varsB, clockedStates;
list<SimCodeVar.SimVar> varsA, varsB, varsC, clockedStates;
SimCode.HashTableCrefToSimVar crefSimVarHT;
list<DAE.ComponentRef> diffCrefsA, diffedCrefsA, derdiffCrefsA;
list<DAE.ComponentRef> diffCrefsB, diffedCrefsB;
DoubleEndedList<SimCodeVar.SimVar> delst;
algorithm
try
//print("Start creating createFMIModelStructure\n");
Expand Down Expand Up @@ -11643,13 +11603,12 @@ algorithm
spTA := mergeSparsePatter(spTA, spTB, {});
//print("-- merged matrixes CD\n");

varsB := getSimVars2Crefs(diffedCrefsB, crefSimVarHT);
varsA := getSimVars2Crefs(diffCrefsB, crefSimVarHT);
varsA := listAppend(varsA, varsB);
varsB := getSimVars2Crefs(diffedCrefsA, crefSimVarHT);
varsA := listAppend(varsA, varsB);
varsB := getSimVars2Crefs(diffCrefsA, crefSimVarHT);
varsA := listAppend(varsA, varsB);
delst := DoubleEndedList.fromList(getSimVars2Crefs(diffedCrefsB, crefSimVarHT));
DoubleEndedList.push_list_back(delst, getSimVars2Crefs(diffCrefsB, crefSimVarHT));
DoubleEndedList.push_list_back(delst, getSimVars2Crefs(diffedCrefsA, crefSimVarHT));
DoubleEndedList.push_list_back(delst, getSimVars2Crefs(diffCrefsA, crefSimVarHT));
varsA := DoubleEndedList.toListAndClear(delst);

//print("-- created vars for CD\n");

sparseInts := sortSparsePattern(varsA, spTA, true);
Expand Down
14 changes: 11 additions & 3 deletions Compiler/Template/SCodeDumpTpl.tpl
Expand Up @@ -131,21 +131,21 @@ match class
let cmt_str = dumpClassComment(cmt, options)
let ann_str = dumpClassAnnotation(cmt, options)
let cc_str = dumpReplaceableConstrainClass(prefixes, options)
let header_str = dumpClassHeader(classDef, name, cmt_str, options)
let header_str = dumpClassHeader(classDef, name, restriction, cmt_str, options)
let footer_str = dumpClassFooter(classDef, cdef_str, name, cmt_str, ann_str, cc_str)
<<
<%prefixes_str%> <%header_str%> <%footer_str%>
>>
end dumpClass;

template dumpClassHeader(SCode.ClassDef classDef, String name, String cmt, SCodeDumpOptions options)
template dumpClassHeader(SCode.ClassDef classDef, String name, SCode.Restriction restr, String cmt, SCodeDumpOptions options)
::=
match classDef
case CLASS_EXTENDS(__)
then
let mod_str = dumpModifier(modifications, options)
'extends <%name%><%mod_str%> <%cmt%>'
case PARTS(__) then '<%name%> <%cmt%>'
case PARTS(__) then '<%name%><%dumpRestrictionTypeVars(restr)%> <%cmt%>'
else '<%name%>'
end dumpClassHeader;

Expand Down Expand Up @@ -678,6 +678,14 @@ match restriction
else errorMsg("SCodeDump.dumpRestriction: Unknown restriction.")
end dumpRestriction;

template dumpRestrictionTypeVars(SCode.Restriction restriction)
::=
match restriction
case R_UNIONTYPE(__) then
(if typeVars then ("<" + (typeVars |> tv => tv ; separator=";") + ">"))
else ""
end dumpRestrictionTypeVars;

template dumpFunctionRestriction(SCode.FunctionRestriction funcRest)
::=
match funcRest
Expand Down
2 changes: 2 additions & 0 deletions Compiler/Template/SCodeTV.mo
Expand Up @@ -356,9 +356,11 @@ package SCode
record R_METARECORD
Absyn.Path name;
Integer index;
list<String> typeVars;
end R_METARECORD;

record R_UNIONTYPE
list<String> typeVars;
end R_UNIONTYPE;
end Restriction;

Expand Down
87 changes: 87 additions & 0 deletions Compiler/Util/DoubleEndedList.mo
Expand Up @@ -51,6 +51,31 @@ algorithm
delst := LIST(arrayCreate(1,1),arrayCreate(1,lst),arrayCreate(1,lst));
end new;

impure function fromList
input list<T> lst;
output DoubleEndedList<T> delst;
protected
list<T> head,tail,tmp;
Integer length=0;
T t;
algorithm
if listEmpty(lst) then
delst := LIST(arrayCreate(1,0),arrayCreate(1,{}),arrayCreate(1,{}));
return;
end if;
t::tmp := lst;
head := {t};
tail := head;
length := 1;
for l in tmp loop
tmp := {l};
Dangerous.listSetRest(tail, tmp);
tail := tmp;
length := length+1;
end for;
delst := LIST(arrayCreate(1,length),arrayCreate(1,head),arrayCreate(1,tail));
end fromList;

impure function empty
input T dummy;
output DoubleEndedList<T> delst;
Expand Down Expand Up @@ -94,6 +119,37 @@ algorithm
arrayUpdate(delst.front, 1, elt::lst);
end push_front;

function push_list_front
input DoubleEndedList<T> delst;
input list<T> lst;
protected
Integer length=arrayGet(delst.length,1), lstLength;
list<T> work, tail={}, tmp;
T t;
algorithm
lstLength := listLength(lst);
if lstLength==0 then
return;
end if;
arrayUpdate(delst.length, 1, length+lstLength);
t::tmp := lst;
work := {t};
tail := work;
for l in tmp loop
tmp := {l};
Dangerous.listSetRest(tail, tmp);
tail := tmp;
end for;
if length==0 then
arrayUpdate(delst.front, 1, work);
arrayUpdate(delst.back, 1, work);
return;
end if;
tmp := arrayGet(delst.front,1);
Dangerous.listSetRest(tail, tmp);
arrayUpdate(delst.front, 1, work);
end push_list_front;

function push_back<T>
input DoubleEndedList<T> delst;
input T elt;
Expand All @@ -113,6 +169,37 @@ algorithm
arrayUpdate(delst.back, 1, lst);
end push_back;

function push_list_back
input DoubleEndedList<T> delst;
input list<T> lst;
protected
Integer length=arrayGet(delst.length,1), lstLength;
list<T> work, tail={}, tmp;
T t;
algorithm
lstLength := listLength(lst);
if lstLength==0 then
return;
end if;
arrayUpdate(delst.length, 1, length+lstLength);
t::tmp := lst;
work := {t};
tail := work;
for l in tmp loop
tmp := {l};
Dangerous.listSetRest(tail, tmp);
tail := tmp;
end for;
if length==0 then
arrayUpdate(delst.front, 1, work);
arrayUpdate(delst.back, 1, work);
return;
end if;
tmp := arrayGet(delst.back,1);
Dangerous.listSetRest(tmp, tail);
arrayUpdate(delst.back, 1, tail);
end push_list_back;

function toListAndClear
input DoubleEndedList<T> delst;
output list<T> res;
Expand Down
15 changes: 15 additions & 0 deletions Compiler/Util/List.mo
Expand Up @@ -603,6 +603,21 @@ algorithm
end match;
end last;

public function lastElement<T>
"Returns the last cons-cell of a list. Fails if the list is empty. Also returns the list length."
input list<T> inList;
output list<T> lst;
output Integer listLength=0;
protected
list<T> rest=inList;
algorithm
false := listEmpty(rest);
while not listEmpty(rest) loop
(lst as (_::rest)) := rest;
listLength := listLength+1;
end while;
end lastElement;

public function lastListOrEmpty<T>
"Returns the last element(list) of a list of lists. Returns empty list
if the outer list is empty."
Expand Down
2 changes: 1 addition & 1 deletion Compiler/boot/LoadCompilerSources.mos
Expand Up @@ -321,7 +321,7 @@ if true then /* Suppress output */
"../Util/BaseAvlTree.mo",
"../Util/AvlTreeString2.mo",

// "../Util/DoubleEndedList.mo",
"../Util/DoubleEndedList.mo",
"../Util/DiffAlgorithm.mo",
"../Util/FMI.mo",
"../Util/FMIExt.mo",
Expand Down

0 comments on commit 0e2f188

Please sign in to comment.