Skip to content

Commit 3b52462

Browse files
committed
Avoid printExpStr in SimCode
1 parent 7ea26e0 commit 3b52462

File tree

4 files changed

+78
-38
lines changed

4 files changed

+78
-38
lines changed

Compiler/BackEnd/BackendVariable.mo

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ protected import Global;
6060
protected import HashSet;
6161
protected import List;
6262
protected import MetaModelica.Dangerous;
63+
protected import StringUtil;
6364
protected import System;
6465
protected import Util;
6566
protected import Types;
@@ -1733,8 +1734,8 @@ algorithm
17331734
outAsserts := matchcontinue(inVar)
17341735
local
17351736
DAE.Exp e, cond, msg;
1736-
list<Option<DAE.Exp>> minmax;
1737-
String str, format;
1737+
Option<DAE.Exp> min, max;
1738+
String str, varStr, format;
17381739
DAE.Type tp;
17391740
DAE.ComponentRef name;
17401741
Option<DAE.VariableAttributes> attr;
@@ -1745,17 +1746,20 @@ algorithm
17451746
then inAsserts;
17461747

17471748
case BackendDAE.VAR(varName=name, values=attr, varType=varType, source=source) equation
1748-
minmax = DAEUtil.getMinMax(attr);
1749+
(min, max) = DAEUtil.getMinMaxValues(attr);
1750+
if isNone(min) and isNone(max) then
1751+
fail();
1752+
end if;
17491753
e = Expression.crefExp(name);
1754+
varStr = ComponentReference.printComponentRefStr(name);
17501755
tp = BackendDAEUtil.makeExpType(varType);
17511756

17521757
// do not add if const true
1753-
cond = getMinMaxAsserts1(minmax, e, tp);
1758+
cond = getMinMaxAsserts1(min, max, e, tp);
17541759
(cond, _) = ExpressionSimplify.simplify(cond);
17551760
false = Expression.isConstTrue(cond);
1761+
str = getMinMaxAsserts1Str(min, max, ComponentReference.printComponentRefStr(name));
17561762

1757-
str = "Variable " + ComponentReference.printComponentRefStr(name) + " out of [min, max] interval: ";
1758-
str = str + ExpressionDump.printExpStr(cond) + " has value: ";
17591763
// if is real use %g otherwise use %d (ints and enums)
17601764
format = if Types.isRealOrSubTypeReal(tp) then "g" else "d";
17611765
msg = DAE.BINARY(DAE.SCONST(str), DAE.ADD(DAE.T_STRING_DEFAULT), DAE.CALL(Absyn.IDENT("String"), {e, DAE.SCONST(format)}, DAE.callAttrBuiltinString));
@@ -1767,26 +1771,49 @@ algorithm
17671771
end getMinMaxAsserts;
17681772

17691773
protected function getMinMaxAsserts1 "author: Frenkel TUD 2011-03"
1770-
input list<Option<DAE.Exp>> ominmax;
1774+
input Option<DAE.Exp> omin,omax;
17711775
input DAE.Exp e;
17721776
input DAE.Type tp;
17731777
output DAE.Exp cond;
17741778
algorithm
1775-
cond := match ominmax
1779+
cond := match (omin,omax)
17761780
local
17771781
DAE.Exp min, max;
17781782

1779-
case SOME(min)::(SOME(max)::{})
1783+
case (SOME(min),SOME(max))
17801784
then DAE.LBINARY(DAE.RELATION(e, DAE.GREATEREQ(tp), min, -1, NONE()), DAE.AND(DAE.T_BOOL_DEFAULT), DAE.RELATION(e, DAE.LESSEQ(tp), max, -1, NONE()));
17811785

1782-
case SOME(min)::(NONE()::{})
1786+
case (SOME(min),NONE())
17831787
then DAE.RELATION(e, DAE.GREATEREQ(tp), min, -1, NONE());
17841788

1785-
case NONE()::(SOME(max)::{})
1789+
case (NONE(),SOME(max))
17861790
then DAE.RELATION(e, DAE.LESSEQ(tp), max, -1, NONE());
17871791
end match;
17881792
end getMinMaxAsserts1;
17891793

1794+
protected function getMinMaxAsserts1Str "author: Frenkel TUD 2011-03"
1795+
input Option<DAE.Exp> omin,omax;
1796+
input String varStr;
1797+
input Boolean nominal=false;
1798+
output String msg;
1799+
protected
1800+
String vstr = if nominal then "Nominal variable " else "Variable ";
1801+
algorithm
1802+
msg := match (omin,omax)
1803+
local
1804+
DAE.Exp min, max;
1805+
1806+
case (SOME(min),SOME(max))
1807+
then StringUtil.stringAppend9(vstr,"violating min/max constraint: ",ExpressionDump.printExpStr(min)," <= ",varStr," <= ",ExpressionDump.printExpStr(max),", has value: ");
1808+
1809+
case (SOME(min),NONE())
1810+
then StringUtil.stringAppend9(vstr,"violating min constraint: ",ExpressionDump.printExpStr(min)," <= ",varStr,", has value: ");
1811+
1812+
case (NONE(),SOME(max))
1813+
then StringUtil.stringAppend9(vstr,"violating max constraint: ",varStr," <= ",ExpressionDump.printExpStr(max),", has value: ");
1814+
end match;
1815+
end getMinMaxAsserts1Str;
1816+
17901817
public function getNominalAssert "author: Frenkel TUD 2011-03"
17911818
input BackendDAE.Var inVar;
17921819
input list<DAE.Algorithm> inAsserts;
@@ -1796,8 +1823,8 @@ algorithm
17961823
outAsserts := matchcontinue(inVar)
17971824
local
17981825
DAE.Exp e, cond, msg;
1799-
list<Option<DAE.Exp>> minmax;
1800-
String str, format;
1826+
Option<DAE.Exp> min, max;
1827+
String str, varStr, format;
18011828
DAE.Type tp;
18021829
DAE.ComponentRef name;
18031830
Option<DAE.VariableAttributes> attr;
@@ -1808,16 +1835,15 @@ algorithm
18081835
then inAsserts;
18091836

18101837
case BackendDAE.VAR(varName=name, values=attr as SOME(DAE.VAR_ATTR_REAL(nominal=SOME(e))), varType=varType, source=source) equation
1811-
minmax = DAEUtil.getMinMax(attr);
1838+
(min, max) = DAEUtil.getMinMaxValues(attr);
18121839
tp = BackendDAEUtil.makeExpType(varType);
18131840

18141841
// do not add if const true
1815-
cond = getMinMaxAsserts1(minmax, e, tp);
1842+
cond = getMinMaxAsserts1(min, max, e, tp);
18161843
(cond, _) = ExpressionSimplify.simplify(cond);
18171844
false = Expression.isConstTrue(cond);
1818-
1819-
str = "Nominal " + ComponentReference.printComponentRefStr(name) + " out of [min, max] interval: ";
1820-
str = str + ExpressionDump.printExpStr(cond) + " has value: ";
1845+
varStr = ComponentReference.printComponentRefStr(name);
1846+
str = getMinMaxAsserts1Str(min, max, ComponentReference.printComponentRefStr(name), nominal=true);
18211847
// if is real use %g otherwise use %d (ints and enums)
18221848
format = if Types.isRealOrSubTypeReal(tp) then "g" else "d";
18231849
msg = DAE.BINARY(DAE.SCONST(str), DAE.ADD(DAE.T_STRING_DEFAULT), DAE.CALL(Absyn.IDENT("String"), {e, DAE.SCONST(format)}, DAE.callAttrBuiltinString));

Compiler/FrontEnd/Inst.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5353,7 +5353,7 @@ protected function generateCachePath
53535353
protected
53545354
String name;
53555355
algorithm
5356-
name := StringUtil.stringAppend6(InstTypes.callingScopeStr(callScope), "$",
5356+
name := StringUtil.stringAppend9(InstTypes.callingScopeStr(callScope), "$",
53575357
SCodeDump.restrString(SCode.getClassRestriction(cls)), "$",
53585358
generatePrefixStr(prefix), "$");
53595359
cachePath := Absyn.joinPaths(Absyn.IDENT(name), FGraph.getGraphName(env));

Compiler/SimCode/SimCodeUtil.mo

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8032,26 +8032,34 @@ protected function extractVarUnit "author: asodja, 2010-03-11
80328032
algorithm
80338033
(unitStr, displayUnitStr) := matchcontinue(var_attr)
80348034
local
8035-
DAE.Exp uexp, duexp;
8036-
case ( SOME(DAE.VAR_ATTR_REAL(unit = SOME(uexp), displayUnit = SOME(duexp) )) )
8037-
equation
8038-
unitStr = ExpressionDump.printExpStr(uexp);
8039-
unitStr = System.stringReplace(unitStr, "\"", "");
8040-
unitStr = System.stringReplace(unitStr, "\\", "\\\\");
8041-
displayUnitStr = ExpressionDump.printExpStr(duexp);
8042-
displayUnitStr = System.stringReplace(displayUnitStr, "\"", "");
8043-
displayUnitStr = System.stringReplace(displayUnitStr, "\\", "\\\\");
8044-
then (unitStr, displayUnitStr);
8045-
case ( SOME(DAE.VAR_ATTR_REAL(unit = SOME(uexp), displayUnit = NONE())) )
8035+
Option<DAE.Exp> uexp, duexp;
8036+
case SOME(DAE.VAR_ATTR_REAL(unit = uexp, displayUnit=duexp))
80468037
equation
8047-
unitStr = ExpressionDump.printExpStr(uexp);
8048-
unitStr = System.stringReplace(unitStr, "\"", "");
8049-
unitStr = System.stringReplace(unitStr, "\\", "\\\\");
8050-
then (unitStr, unitStr);
8038+
unitStr = extractVarUnitStr(uexp);
8039+
displayUnitStr = extractVarUnitStr(duexp);
8040+
then (unitStr, displayUnitStr);
80518041
else ("", "");
80528042
end matchcontinue;
80538043
end extractVarUnit;
80548044

8045+
protected function extractVarUnitStr "author: asodja, 2010-03-11
8046+
Extract variable's unit and displayUnit as strings from
8047+
DAE.VariablesAttributes structures."
8048+
input Option<DAE.Exp> exp;
8049+
output String str;
8050+
algorithm
8051+
str := match exp
8052+
local
8053+
DAE.Exp e;
8054+
case SOME(DAE.SCONST(str)) then str;
8055+
case NONE() then "";
8056+
case SOME(e)
8057+
algorithm
8058+
Error.addInternalError("Unexpected expression (should have been handled earlier, probably in the front-end. Unit/displayUnit expression is not a string literal: " + ExpressionDump.printExpStr(e), sourceInfo());
8059+
then "";
8060+
end match;
8061+
end extractVarUnitStr;
8062+
80558063
protected function getMinMaxValues "extract min/max values from BackendDAE.Variable"
80568064
input BackendDAE.Var inDAELowVar;
80578065
output Option<DAE.Exp> outMinValue;

Compiler/Util/StringUtil.mo

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,11 @@ algorithm
349349
end for;
350350
end stringHashDjb2Work;
351351

352-
function stringAppend6
353-
input String str1,str2,str3,str4,str5,str6;
352+
function stringAppend9
353+
input String str1,str2,str3,str4="",str5="",str6="",str7="",str8="",str9="";
354354
output String str;
355355
protected
356-
System.StringAllocator sb=System.StringAllocator(stringLength(str1)+stringLength(str2)+stringLength(str3)+stringLength(str4)+stringLength(str5)+stringLength(str6));
356+
System.StringAllocator sb=System.StringAllocator(stringLength(str1)+stringLength(str2)+stringLength(str3)+stringLength(str4)+stringLength(str5)+stringLength(str6)+stringLength(str7)+stringLength(str8)+stringLength(str9));
357357
Integer c=0;
358358
algorithm
359359
System.stringAllocatorStringCopy(sb, str1, c);
@@ -368,8 +368,14 @@ algorithm
368368
c := c + stringLength(str5);
369369
System.stringAllocatorStringCopy(sb, str6, c);
370370
c := c + stringLength(str6);
371+
System.stringAllocatorStringCopy(sb, str7, c);
372+
c := c + stringLength(str7);
373+
System.stringAllocatorStringCopy(sb, str8, c);
374+
c := c + stringLength(str8);
375+
System.stringAllocatorStringCopy(sb, str9, c);
376+
c := c + stringLength(str9);
371377
str := System.stringAllocatorResult(sb,str1);
372-
end stringAppend6;
378+
end stringAppend9;
373379

374380
function endsWithNewline
375381
input String str;

0 commit comments

Comments
 (0)