Skip to content

Commit 0e2f188

Browse files
sjoelundOpenModelica-Hudson
authored andcommitted
Handle bootstrapping DoubleEndedList.mo
1 parent 3094539 commit 0e2f188

File tree

7 files changed

+137
-66
lines changed

7 files changed

+137
-66
lines changed

Compiler/Script/CevalScript.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2123,7 +2123,7 @@ algorithm
21232123
path := Absyn.joinPaths(pathPrefix, Absyn.IDENT(name));
21242124
paths := if SCode.isFunction(elt) then path::acc else acc;
21252125
pathsMetarecord := match elt
2126-
case SCode.CLASS(restriction=SCode.R_METARECORD(moved = true)) then path::accMetarecord;
2126+
case SCode.CLASS(restriction=SCode.R_METARECORD()) then path::accMetarecord;
21272127
else accMetarecord;
21282128
end match;
21292129
elements := getNonPartialElementsForInstantiatedClass(sp, elt, path);

Compiler/SimCode/SimCodeUtil.mo

Lines changed: 20 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import DAEDump;
7979
import DAEUtil;
8080
import Debug;
8181
import Differentiate;
82+
import DoubleEndedList;
8283
import Error;
8384
import EvaluateFunctions;
8485
import Expression;
@@ -11029,41 +11030,6 @@ algorithm
1102911030
b := listMember(bEq,bEq1);
1103011031
end findBEqs;
1103111032

11032-
/*
11033-
This is wrong!
11034-
public function getSimVarByIndex
11035-
input Integer idx;
11036-
input SimCodeVar.SimVars allSimVars;
11037-
output SimCodeVar.SimVar simVar;
11038-
algorithm
11039-
simVar := matchcontinue(idx,allSimVars)
11040-
local
11041-
Integer size,idx2;
11042-
list<SimCodeVar.SimVar> stateVars,algVars;
11043-
SimCodeVar.SimVar var;
11044-
case(_,SimCodeVar.SIMVARS(stateVars=stateVars,algVars=algVars))
11045-
equation
11046-
size = listLength(stateVars);
11047-
true = idx > size;
11048-
//its not a stateVar
11049-
idx2 = idx - 2*size + 1;
11050-
var = listGet(algVars,idx2);
11051-
then var;
11052-
case(_,SimCodeVar.SIMVARS(stateVars=stateVars,algVars=algVars))
11053-
equation
11054-
size = listLength(stateVars);
11055-
true = idx <= size;
11056-
//its a stateVar
11057-
var = listGet(stateVars,idx);
11058-
then var;
11059-
else
11060-
equation
11061-
print("SimCodeUtil.getSimVarByIndex failed!\n");
11062-
then fail();
11063-
end matchcontinue;
11064-
end getSimVarByIndex;
11065-
*/
11066-
1106711033
public function getAssignedCrefsOfSimEq"gets the crefs of the vars that are assigned (the lhs) of the simEqSystems
1106811034
author:Waurich TUD 2014-05"
1106911035
input Integer idx;
@@ -11079,7 +11045,7 @@ algorithm
1107911045
equation
1108011046
simEqSyst = List.getMemberOnTrue(idx,allEqs,indexIsEqual);
1108111047
crefs = getSimEqSystemCrefsLHS(simEqSyst);
11082-
then crefs;
11048+
then crefs;
1108311049
end match;
1108411050
end getAssignedCrefsOfSimEq;
1108511051

@@ -11098,17 +11064,15 @@ algorithm
1109811064
case(SimCode.SES_RESIDUAL())
1109911065
equation
1110011066
print("implement SES_RESIDUAL in SimCodeUtil.getSimEqSystemCrefsLHS!\n");
11101-
then {};
11067+
then {};
1110211068
case(SimCode.SES_SIMPLE_ASSIGN(cref=cref))
11103-
equation
11104-
then {cref};
11069+
then {cref};
1110511070
case(SimCode.SES_ARRAY_CALL_ASSIGN(lhs=lhs))
11106-
equation
11107-
then {Expression.expCref(lhs)};
11071+
then {Expression.expCref(lhs)};
1110811072
case(SimCode.SES_IFEQUATION())
1110911073
equation
1111011074
print("implement SES_IFEQUATION in SimCodeUtil.getSimEqSystemCrefsLHS!\n");
11111-
then {};
11075+
then {};
1111211076
case(SimCode.SES_ALGORITHM()) equation
1111311077
print("implement SES_ALGORITHM in SimCodeUtil.getSimEqSystemCrefsLHS!\n");
1111411078
then {};
@@ -11117,20 +11081,15 @@ algorithm
1111711081
then {};
1111811082
case(SimCode.SES_LINEAR(SimCode.LINEARSYSTEM(vars=simVars,residual=residual)))
1111911083
equation
11120-
crefs2 = List.flatten(List.map(residual,getSimEqSystemCrefsLHS));
11121-
crefs = list(SimCodeFunctionUtil.varName(v) for v in simVars);
11122-
crefs = listAppend(crefs,crefs2);
11123-
then crefs;
11084+
crefs = List.flatten(List.map(residual,getSimEqSystemCrefsLHS));
11085+
crefs2 = list(SimCodeFunctionUtil.varName(v) for v in simVars);
11086+
then listAppend(crefs2,crefs2);
1112411087
case(SimCode.SES_NONLINEAR(SimCode.NONLINEARSYSTEM(crefs=crefs)))
11125-
equation
11126-
then crefs;
11127-
case(SimCode.SES_MIXED(discVars=simVars))
11128-
equation
11129-
crefs = list(SimCodeFunctionUtil.varName(v) for v in simVars);
1113011088
then crefs;
11089+
case(SimCode.SES_MIXED(discVars=simVars))
11090+
then list(SimCodeFunctionUtil.varName(v) for v in simVars);
1113111091
case(SimCode.SES_WHEN(whenStmtLst={BackendDAE.ASSIGN(left=cref)}))
11132-
equation
11133-
then {cref};
11092+
then {cref};
1113411093
end match;
1113511094
end getSimEqSystemCrefsLHS;
1113611095

@@ -11600,10 +11559,11 @@ protected
1160011559
list<tuple<DAE.ComponentRef, list<DAE.ComponentRef>>> spTA, spTB;
1160111560
list<tuple<Integer, list<Integer>>> sparseInts;
1160211561
list<SimCode.FmiUnknown> derivatives, outputs, discreteStates;
11603-
list<SimCodeVar.SimVar> varsA, varsB, clockedStates;
11562+
list<SimCodeVar.SimVar> varsA, varsB, varsC, clockedStates;
1160411563
SimCode.HashTableCrefToSimVar crefSimVarHT;
1160511564
list<DAE.ComponentRef> diffCrefsA, diffedCrefsA, derdiffCrefsA;
1160611565
list<DAE.ComponentRef> diffCrefsB, diffedCrefsB;
11566+
DoubleEndedList<SimCodeVar.SimVar> delst;
1160711567
algorithm
1160811568
try
1160911569
//print("Start creating createFMIModelStructure\n");
@@ -11643,13 +11603,12 @@ algorithm
1164311603
spTA := mergeSparsePatter(spTA, spTB, {});
1164411604
//print("-- merged matrixes CD\n");
1164511605

11646-
varsB := getSimVars2Crefs(diffedCrefsB, crefSimVarHT);
11647-
varsA := getSimVars2Crefs(diffCrefsB, crefSimVarHT);
11648-
varsA := listAppend(varsA, varsB);
11649-
varsB := getSimVars2Crefs(diffedCrefsA, crefSimVarHT);
11650-
varsA := listAppend(varsA, varsB);
11651-
varsB := getSimVars2Crefs(diffCrefsA, crefSimVarHT);
11652-
varsA := listAppend(varsA, varsB);
11606+
delst := DoubleEndedList.fromList(getSimVars2Crefs(diffedCrefsB, crefSimVarHT));
11607+
DoubleEndedList.push_list_back(delst, getSimVars2Crefs(diffCrefsB, crefSimVarHT));
11608+
DoubleEndedList.push_list_back(delst, getSimVars2Crefs(diffedCrefsA, crefSimVarHT));
11609+
DoubleEndedList.push_list_back(delst, getSimVars2Crefs(diffCrefsA, crefSimVarHT));
11610+
varsA := DoubleEndedList.toListAndClear(delst);
11611+
1165311612
//print("-- created vars for CD\n");
1165411613

1165511614
sparseInts := sortSparsePattern(varsA, spTA, true);

Compiler/Template/SCodeDumpTpl.tpl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,21 +131,21 @@ match class
131131
let cmt_str = dumpClassComment(cmt, options)
132132
let ann_str = dumpClassAnnotation(cmt, options)
133133
let cc_str = dumpReplaceableConstrainClass(prefixes, options)
134-
let header_str = dumpClassHeader(classDef, name, cmt_str, options)
134+
let header_str = dumpClassHeader(classDef, name, restriction, cmt_str, options)
135135
let footer_str = dumpClassFooter(classDef, cdef_str, name, cmt_str, ann_str, cc_str)
136136
<<
137137
<%prefixes_str%> <%header_str%> <%footer_str%>
138138
>>
139139
end dumpClass;
140140

141-
template dumpClassHeader(SCode.ClassDef classDef, String name, String cmt, SCodeDumpOptions options)
141+
template dumpClassHeader(SCode.ClassDef classDef, String name, SCode.Restriction restr, String cmt, SCodeDumpOptions options)
142142
::=
143143
match classDef
144144
case CLASS_EXTENDS(__)
145145
then
146146
let mod_str = dumpModifier(modifications, options)
147147
'extends <%name%><%mod_str%> <%cmt%>'
148-
case PARTS(__) then '<%name%> <%cmt%>'
148+
case PARTS(__) then '<%name%><%dumpRestrictionTypeVars(restr)%> <%cmt%>'
149149
else '<%name%>'
150150
end dumpClassHeader;
151151

@@ -678,6 +678,14 @@ match restriction
678678
else errorMsg("SCodeDump.dumpRestriction: Unknown restriction.")
679679
end dumpRestriction;
680680

681+
template dumpRestrictionTypeVars(SCode.Restriction restriction)
682+
::=
683+
match restriction
684+
case R_UNIONTYPE(__) then
685+
(if typeVars then ("<" + (typeVars |> tv => tv ; separator=";") + ">"))
686+
else ""
687+
end dumpRestrictionTypeVars;
688+
681689
template dumpFunctionRestriction(SCode.FunctionRestriction funcRest)
682690
::=
683691
match funcRest

Compiler/Template/SCodeTV.mo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,11 @@ package SCode
356356
record R_METARECORD
357357
Absyn.Path name;
358358
Integer index;
359+
list<String> typeVars;
359360
end R_METARECORD;
360361

361362
record R_UNIONTYPE
363+
list<String> typeVars;
362364
end R_UNIONTYPE;
363365
end Restriction;
364366

Compiler/Util/DoubleEndedList.mo

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,31 @@ algorithm
5151
delst := LIST(arrayCreate(1,1),arrayCreate(1,lst),arrayCreate(1,lst));
5252
end new;
5353

54+
impure function fromList
55+
input list<T> lst;
56+
output DoubleEndedList<T> delst;
57+
protected
58+
list<T> head,tail,tmp;
59+
Integer length=0;
60+
T t;
61+
algorithm
62+
if listEmpty(lst) then
63+
delst := LIST(arrayCreate(1,0),arrayCreate(1,{}),arrayCreate(1,{}));
64+
return;
65+
end if;
66+
t::tmp := lst;
67+
head := {t};
68+
tail := head;
69+
length := 1;
70+
for l in tmp loop
71+
tmp := {l};
72+
Dangerous.listSetRest(tail, tmp);
73+
tail := tmp;
74+
length := length+1;
75+
end for;
76+
delst := LIST(arrayCreate(1,length),arrayCreate(1,head),arrayCreate(1,tail));
77+
end fromList;
78+
5479
impure function empty
5580
input T dummy;
5681
output DoubleEndedList<T> delst;
@@ -94,6 +119,37 @@ algorithm
94119
arrayUpdate(delst.front, 1, elt::lst);
95120
end push_front;
96121

122+
function push_list_front
123+
input DoubleEndedList<T> delst;
124+
input list<T> lst;
125+
protected
126+
Integer length=arrayGet(delst.length,1), lstLength;
127+
list<T> work, tail={}, tmp;
128+
T t;
129+
algorithm
130+
lstLength := listLength(lst);
131+
if lstLength==0 then
132+
return;
133+
end if;
134+
arrayUpdate(delst.length, 1, length+lstLength);
135+
t::tmp := lst;
136+
work := {t};
137+
tail := work;
138+
for l in tmp loop
139+
tmp := {l};
140+
Dangerous.listSetRest(tail, tmp);
141+
tail := tmp;
142+
end for;
143+
if length==0 then
144+
arrayUpdate(delst.front, 1, work);
145+
arrayUpdate(delst.back, 1, work);
146+
return;
147+
end if;
148+
tmp := arrayGet(delst.front,1);
149+
Dangerous.listSetRest(tail, tmp);
150+
arrayUpdate(delst.front, 1, work);
151+
end push_list_front;
152+
97153
function push_back<T>
98154
input DoubleEndedList<T> delst;
99155
input T elt;
@@ -113,6 +169,37 @@ algorithm
113169
arrayUpdate(delst.back, 1, lst);
114170
end push_back;
115171

172+
function push_list_back
173+
input DoubleEndedList<T> delst;
174+
input list<T> lst;
175+
protected
176+
Integer length=arrayGet(delst.length,1), lstLength;
177+
list<T> work, tail={}, tmp;
178+
T t;
179+
algorithm
180+
lstLength := listLength(lst);
181+
if lstLength==0 then
182+
return;
183+
end if;
184+
arrayUpdate(delst.length, 1, length+lstLength);
185+
t::tmp := lst;
186+
work := {t};
187+
tail := work;
188+
for l in tmp loop
189+
tmp := {l};
190+
Dangerous.listSetRest(tail, tmp);
191+
tail := tmp;
192+
end for;
193+
if length==0 then
194+
arrayUpdate(delst.front, 1, work);
195+
arrayUpdate(delst.back, 1, work);
196+
return;
197+
end if;
198+
tmp := arrayGet(delst.back,1);
199+
Dangerous.listSetRest(tmp, tail);
200+
arrayUpdate(delst.back, 1, tail);
201+
end push_list_back;
202+
116203
function toListAndClear
117204
input DoubleEndedList<T> delst;
118205
output list<T> res;

Compiler/Util/List.mo

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,21 @@ algorithm
603603
end match;
604604
end last;
605605

606+
public function lastElement<T>
607+
"Returns the last cons-cell of a list. Fails if the list is empty. Also returns the list length."
608+
input list<T> inList;
609+
output list<T> lst;
610+
output Integer listLength=0;
611+
protected
612+
list<T> rest=inList;
613+
algorithm
614+
false := listEmpty(rest);
615+
while not listEmpty(rest) loop
616+
(lst as (_::rest)) := rest;
617+
listLength := listLength+1;
618+
end while;
619+
end lastElement;
620+
606621
public function lastListOrEmpty<T>
607622
"Returns the last element(list) of a list of lists. Returns empty list
608623
if the outer list is empty."

Compiler/boot/LoadCompilerSources.mos

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ if true then /* Suppress output */
321321
"../Util/BaseAvlTree.mo",
322322
"../Util/AvlTreeString2.mo",
323323

324-
// "../Util/DoubleEndedList.mo",
324+
"../Util/DoubleEndedList.mo",
325325
"../Util/DiffAlgorithm.mo",
326326
"../Util/FMI.mo",
327327
"../Util/FMIExt.mo",

0 commit comments

Comments
 (0)