Skip to content

Commit

Permalink
Refactoring to make dependencies acyclic
Browse files Browse the repository at this point in the history
- Created new package for Gettext
- Move variable mangling to BackendUtil instead of Util
- Removed cyclic dependencies for Util
- Removed Prefix (merged with DAE)
- Created Testsuite.mo, split from Config.mo
- Split Flags into FlagsUtil and Flags (avoids cyclic dependency on
  Error)
- Added package ErrorTypes to avoid cycles with ErrorExt
  • Loading branch information
sjoelund committed Nov 21, 2019
1 parent 55a9813 commit 9e8c062
Show file tree
Hide file tree
Showing 144 changed files with 5,378 additions and 5,288 deletions.
2 changes: 1 addition & 1 deletion Makefile.in
Expand Up @@ -66,7 +66,7 @@ fix-whitespace:
find . -type f \( ! -path '*/.svn/*' -or -path '*/.git/*' -prune \) -regextype posix-egrep -regex '.*\.(cpp|c|h|mo|tpl)$$' -exec sh -c 'sed -i -e "s/ / /g" -e "s/ *\$$//" "{}"' ";"

spellcheck:
grep -oE 'gettext[(]["]([^"]|([\\]["]))*["]' `ls OMCompiler/Compiler/*/*.mo | grep -v Flags.mo` | sed "s/[\\%]./ /g" | aspell -p ./.openmodelica.aspell --mode=ccpp --lang=en_US list | sort -u | sed 's/^/aspell: /' | tee aspell.log
grep -oE 'gettext[(]["]([^"]|([\\]["]))*["]' `ls OMCompiler/Compiler/*/*.mo | grep -v "Flags.*mo"` | sed "s/[\\%]./ /g" | aspell -p ./.openmodelica.aspell --mode=ccpp --lang=en_US list | sort -u | sed 's/^/aspell: /' | tee aspell.log
@test ! -s aspell.log
thumbsdb-error:
! find . -name "Thumbs.db" | grep Thumbs.db
Expand Down
1 change: 0 additions & 1 deletion OMCompiler/Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -65,7 +65,6 @@ import ClassInf;
import ComponentReference;
import DAEUtil;
import DAEDump;
import Debug;
import DoubleEnded;
import Differentiate;
import ElementSource;
Expand Down
31 changes: 15 additions & 16 deletions OMCompiler/Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -50,6 +50,7 @@ import AvlSetInt;
import AvlSetPath;
import BackendDAE;
import BackendDAEFunc;
import BackendEquation;
import DAE;
import FCore;
import Util;
Expand All @@ -61,7 +62,6 @@ import Array;
import BackendDAEOptimize;
import BackendDAETransform;
import BackendDump;
import BackendEquation;
import BackendDAEEXT;
import BackendInline;
import BackendVarTransform;
Expand All @@ -70,7 +70,6 @@ import BinaryTree;
import Causalize;
import CheckModel;
import ClassInf;
import ClockIndexes;
import CommonSubExpression;
import ComponentReference;
import Config;
Expand All @@ -95,7 +94,7 @@ import ExpressionSimplify;
import ExpressionSolve;
import FindZeroCrossings;
import Flags;
import FMI;
import FlagsUtil;
import Global;
import HpcOmEqSystems;
import IndexReduction;
Expand Down Expand Up @@ -4172,7 +4171,7 @@ function testSolvabilityForEquation
"Helper function for findSolvabelVarInEquation."
input DAE.ComponentRef inCref;
input BackendDAE.Equation inEq;
input array<Boolean> varArray;
input array<Boolean> varArray;
input BackendDAE.Variables inVariables "Unknowns of system";
input BackendDAE.Variables globalKnownVars;
output Boolean solvability;
Expand Down Expand Up @@ -4238,7 +4237,7 @@ algorithm
then false;

// IF_EQUATION
// TODO : how to handle this?
// TODO : how to handle this?
// Proposal:
// 1. vars in conditions are unsolvable
// 2. vars occur in all branches: check how they are occur
Expand All @@ -4247,7 +4246,7 @@ algorithm
try
print("\n if equation?");
//check condition?
try_b := tryToFindSolvableEqInBranch(inCref, eqnselse, varArray, inVariables, globalKnownVars);
try_b := tryToFindSolvableEqInBranch(inCref, eqnselse, varArray, inVariables, globalKnownVars);
true := try_b;
for eqns in eqnslst loop
try_b := tryToFindSolvableEqInBranch(inCref, eqns, varArray, inVariables, globalKnownVars);
Expand Down Expand Up @@ -8336,7 +8335,7 @@ protected function deprecatedDebugFlag
algorithm
if Flags.isSet(inFlag) then
outModuleList := inModule::inModuleList;
Error.addCompilerWarning("Deprecated debug flag -d=" + Flags.debugFlagName(inFlag) + " detected. Use --" + inPhase + "=" + inModule + " instead.");
Error.addCompilerWarning("Deprecated debug flag -d=" + FlagsUtil.debugFlagName(inFlag) + " detected. Use --" + inPhase + "=" + inModule + " instead.");
end if;
end deprecatedDebugFlag;

Expand All @@ -8349,7 +8348,7 @@ protected function deprecatedConfigFlag
algorithm
if Flags.getConfigBool(inFlag) then
outModuleList := inModule::inModuleList;
Error.addCompilerWarning("Deprecated flag --" + Flags.configFlagName(inFlag) + " detected. Use --" + inPhase + "=" + inModule + " instead.");
Error.addCompilerWarning("Deprecated flag --" + FlagsUtil.configFlagName(inFlag) + " detected. Use --" + inPhase + "=" + inModule + " instead.");
end if;
end deprecatedConfigFlag;

Expand Down Expand Up @@ -8398,12 +8397,12 @@ algorithm
end if;

if not Flags.getConfigBool(Flags.DEFAULT_OPT_MODULES_ORDERING) and not listEmpty(enabledModules) then
Error.addCompilerError("It's not possible to combine following flags: --preOptModules+=... and --" + Flags.configFlagName(Flags.DEFAULT_OPT_MODULES_ORDERING) + "=false");
Error.addCompilerError("It's not possible to combine following flags: --preOptModules+=... and --" + FlagsUtil.configFlagName(Flags.DEFAULT_OPT_MODULES_ORDERING) + "=false");
fail();
end if;

if not Flags.getConfigBool(Flags.DEFAULT_OPT_MODULES_ORDERING) and not listEmpty(disabledModules) then
Error.addCompilerError("It's not possible to combine following flags: --postOptModules-=... and --" + Flags.configFlagName(Flags.DEFAULT_OPT_MODULES_ORDERING) + "=false");
Error.addCompilerError("It's not possible to combine following flags: --postOptModules-=... and --" + FlagsUtil.configFlagName(Flags.DEFAULT_OPT_MODULES_ORDERING) + "=false");
fail();
end if;

Expand Down Expand Up @@ -8445,7 +8444,7 @@ algorithm
enabledModules := deprecatedDebugFlag(Flags.ON_RELAXATION, enabledModules, "relaxSystem", "postOptModules+");

if Flags.getConfigBool(Flags.DISABLE_LINEAR_TEARING) then
Flags.setConfigInt(Flags.MAX_SIZE_LINEAR_TEARING, 0);
FlagsUtil.setConfigInt(Flags.MAX_SIZE_LINEAR_TEARING, 0);
Error.addCompilerWarning("Deprecated flag --disableLinearTearing detected. Use --maxSizeLinearTearing=0 instead.");
end if;

Expand Down Expand Up @@ -8506,12 +8505,12 @@ algorithm
end if;

if not Flags.getConfigBool(Flags.DEFAULT_OPT_MODULES_ORDERING) and not listEmpty(enabledModules) then
Error.addCompilerError("It's not possible to combine following flags: --postOptModules+=... and --" + Flags.configFlagName(Flags.DEFAULT_OPT_MODULES_ORDERING) + "=false");
Error.addCompilerError("It's not possible to combine following flags: --postOptModules+=... and --" + FlagsUtil.configFlagName(Flags.DEFAULT_OPT_MODULES_ORDERING) + "=false");
fail();
end if;

if not Flags.getConfigBool(Flags.DEFAULT_OPT_MODULES_ORDERING) and not listEmpty(disabledModules) then
Error.addCompilerError("It's not possible to combine following flags: --postOptModules-=... and --" + Flags.configFlagName(Flags.DEFAULT_OPT_MODULES_ORDERING) + "=false");
Error.addCompilerError("It's not possible to combine following flags: --postOptModules-=... and --" + FlagsUtil.configFlagName(Flags.DEFAULT_OPT_MODULES_ORDERING) + "=false");
fail();
end if;

Expand Down Expand Up @@ -8550,12 +8549,12 @@ algorithm
end if;

if not Flags.getConfigBool(Flags.DEFAULT_OPT_MODULES_ORDERING) and not listEmpty(enabledModules) then
Error.addCompilerError("It's not possible to combine following flags: --initOptModules+=... and --" + Flags.configFlagName(Flags.DEFAULT_OPT_MODULES_ORDERING) + "=false");
Error.addCompilerError("It's not possible to combine following flags: --initOptModules+=... and --" + FlagsUtil.configFlagName(Flags.DEFAULT_OPT_MODULES_ORDERING) + "=false");
fail();
end if;

if not Flags.getConfigBool(Flags.DEFAULT_OPT_MODULES_ORDERING) and not listEmpty(disabledModules) then
Error.addCompilerError("It's not possible to combine following flags: --initOptModules-=... and --" + Flags.configFlagName(Flags.DEFAULT_OPT_MODULES_ORDERING) + "=false");
Error.addCompilerError("It's not possible to combine following flags: --initOptModules-=... and --" + FlagsUtil.configFlagName(Flags.DEFAULT_OPT_MODULES_ORDERING) + "=false");
fail();
end if;

Expand All @@ -8582,7 +8581,7 @@ algorithm
for name in inStrOptModules loop
for index in getModuleIndexes(name, inOptModules) loop
if index < maxIndex then
Error.addCompilerWarning("Specified ordering will be ignored. Use --" + Flags.configFlagName(Flags.DEFAULT_OPT_MODULES_ORDERING) + "=false to override module ordering.");
Error.addCompilerWarning("Specified ordering will be ignored. Use --" + FlagsUtil.configFlagName(Flags.DEFAULT_OPT_MODULES_ORDERING) + "=false to override module ordering.");
maxIndex := numModules;
else
maxIndex := intMax(maxIndex, index);
Expand Down
9 changes: 5 additions & 4 deletions OMCompiler/Compiler/BackEnd/BackendInline.mo
Expand Up @@ -46,12 +46,13 @@ public import Inline;
public import SCode;

protected
import BackendVarTransform;
import BackendDAEOptimize;
import BackendDAEUtil;
import BackendDump;
import BackendEquation;
import BackendUtil;
import BackendVariable;
import BackendDAEOptimize;
import BackendVarTransform;
import ComponentReference;
import DAEDump;
import DAEUtil;
Expand Down Expand Up @@ -900,7 +901,7 @@ algorithm
guard Inline.checkInlineType(inlineType,fns) and Flags.getConfigEnum(Flags.INLINE_METHOD)==2
equation
(fn,comment) = Inline.getFunctionBody(p,fns);
funcname = Util.modelicaStringToCStr(AbsynUtil.pathString(p), false);
funcname = BackendUtil.modelicaStringToCStr(AbsynUtil.pathString(p), false);
if Flags.isSet(Flags.DUMPBACKENDINLINE_VERBOSE) then
print("Inline Function " +funcname+" type: "+DAEDump.dumpInlineTypeStr(inlineType)+"\n");
print("in : " + ExpressionDump.printExpStr(inExp) + "\n");
Expand Down Expand Up @@ -928,7 +929,7 @@ algorithm
(newExp, _) = Inline.inlineCall(inExp, {}, fns);

if Flags.isSet(Flags.DUMPBACKENDINLINE_VERBOSE) then
funcname = Util.modelicaStringToCStr(AbsynUtil.pathString(p), false);
funcname = BackendUtil.modelicaStringToCStr(AbsynUtil.pathString(p), false);
print("\nBackendInline fallback replace implementation: " +funcname+" type: " +DAEDump.dumpInlineTypeStr(inlineType)+"\n");
print("in : " + ExpressionDump.printExpStr(inExp) + "\n");
print("out: " + ExpressionDump.printExpStr(newExp) + "\n");
Expand Down
154 changes: 154 additions & 0 deletions OMCompiler/Compiler/BackEnd/BackendUtil.mo
@@ -0,0 +1,154 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2019, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/

encapsulated package BackendUtil
" file: BackendUtil.mo
package: BackendUtil
description: Miscellanous MetaModelica Compiler (MMC) utilities used by the backend."

protected

import Flags;
import List;
import System;
import Util;

uniontype ReplacePattern
record REPLACEPATTERN
String from "from string (ie \".\"" ;
String to "to string (ie \"$p\") ))" ;
end REPLACEPATTERN;
end ReplacePattern;

constant list<ReplacePattern> replaceStringPatterns =
{REPLACEPATTERN(".",pointStr),
REPLACEPATTERN("[",leftBraketStr),REPLACEPATTERN("]",rightBraketStr),
REPLACEPATTERN("(",leftParStr),REPLACEPATTERN(")",rightParStr),
REPLACEPATTERN(",",commaStr),
REPLACEPATTERN("'",appostrophStr)};

constant String pointStr = "$P";
constant String leftBraketStr = "$lB";
constant String rightBraketStr = "$rB";
constant String leftParStr = "$lP";
constant String rightParStr = "$rP";
constant String commaStr = "$c";
constant String appostrophStr = "$a";

public

function modelicaStringToCStr " this replaces symbols that are illegal in C to legal symbols
see replaceStringPatterns to see the format. (example: \".\" becomes \"$P\")
author: x02lucpo

NOTE: This function should not be used in OMC, since the OMC backend no longer
uses stringified components. It is still used by MathCore though."
input String str;
input Boolean changeDerCall "if true, first change 'DER(v)' to $derivativev";
output String res_str;
algorithm
res_str := matchcontinue(str,changeDerCall)
local String s;
case(s,false) // BoschRexroth specifics
equation
false = Flags.getConfigBool(Flags.TRANSLATE_DAE_STRING);
then
s;
case(_,false)
equation
res_str = "$"+ modelicaStringToCStr1(str, replaceStringPatterns);
// debug_print("prefix$", res_str);
then res_str;
case(s,true) equation
s = modelicaStringToCStr2(s);
then s;
end matchcontinue;
end modelicaStringToCStr;

protected

function modelicaStringToCStr1 ""
input String inString;
input list<ReplacePattern> inReplacePatternLst;
output String outString;
algorithm
outString:=
matchcontinue (inString,inReplacePatternLst)
local
String str,str_1,res_str,from,to;
list<ReplacePattern> res;
case (str,{}) then str;
case (str,(REPLACEPATTERN(from = from,to = to) :: res))
equation
str_1 = modelicaStringToCStr1(str, res);
res_str = System.stringReplace(str_1, from, to);
then
res_str;
else
equation
print(getInstanceName() + " failed for str:"+inString+"\n");
then
fail();
end matchcontinue;
end modelicaStringToCStr1;

function modelicaStringToCStr2 "help function to modelicaStringToCStr,
first changes name 'der(v)' to $derivativev and 'pre(v)' to 'pre(v)' with applied rules for v"
input String inDerName;
output String outDerName;
algorithm
outDerName := matchcontinue(inDerName)
local
String name, derName;
list<String> names;

case(derName) equation
0 = System.strncmp(derName,"der(",4);
// adrpo: 2009-09-08
// the commented text: _::name::_ = listLast(System.strtok(derName,"()"));
// is wrong as der(der(x)) ends up beeing translated to $der$der instead
// of $der$der$x. Changed to the following 2 lines below!
_::names = (System.strtok(derName,"()"));
names = List.map1(names, modelicaStringToCStr, false);
name = Util.derivativeNamePrefix + stringAppendList(names);
then name;
case(derName) equation
0 = System.strncmp(derName,"pre(",4);
_::name::_= System.strtok(derName,"()");
name = "pre(" + modelicaStringToCStr(name,false) + ")";
then name;
case(derName) then modelicaStringToCStr(derName,false);
end matchcontinue;
end modelicaStringToCStr2;


annotation(__OpenModelica_Interface="backend");
end BackendUtil;
5 changes: 2 additions & 3 deletions OMCompiler/Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -45,7 +45,6 @@ import Absyn;
import AbsynUtil;
import Array;
import BackendDAEUtil;
import BackendDump;
import BaseHashSet;
import BaseHashTable;
import ComponentReference;
Expand Down Expand Up @@ -1686,7 +1685,7 @@ algorithm

case (_) guard(ComponentReference.traverseCref(inCref, ComponentReference.crefIsRec, false)) equation
DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(path)) = inType;
source = DAE.SOURCE(AbsynUtil.dummyInfo, {}, Prefix.NOCOMPPRE(), {}, {path}, {}, {});
source = DAE.SOURCE(AbsynUtil.dummyInfo, {}, DAE.NOCOMPPRE(), {}, {path}, {}, {});
varKind = if Types.isDiscreteType(inType) then BackendDAE.DISCRETE() else BackendDAE.VARIABLE();
outVar = BackendDAE.VAR(inCref, varKind, DAE.BIDIR(), DAE.NON_PARALLEL(), inType, NONE(), NONE(), {}, source, DAEUtil.setProtectedAttr(NONE(), true), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), true);
then outVar;
Expand Down Expand Up @@ -1767,7 +1766,7 @@ algorithm

case (_) guard(ComponentReference.traverseCref(inCref, ComponentReference.crefIsRec, false)) equation
DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(path)) = inType;
source = DAE.SOURCE(AbsynUtil.dummyInfo, {}, Prefix.NOCOMPPRE(), {}, {path}, {}, {});
source = DAE.SOURCE(AbsynUtil.dummyInfo, {}, DAE.NOCOMPPRE(), {}, {path}, {}, {});
varKind = if Types.isDiscreteType(inType) then BackendDAE.DISCRETE() else BackendDAE.VARIABLE();
outVar = BackendDAE.VAR(inCref, varKind, DAE.BIDIR(), DAE.NON_PARALLEL(), inType, NONE(), NONE(), inArryDim, source, DAEUtil.setProtectedAttr(NONE(), true), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), true);
then outVar;
Expand Down
1 change: 0 additions & 1 deletion OMCompiler/Compiler/BackEnd/BinaryTree.mo
Expand Up @@ -47,7 +47,6 @@ protected import BaseHashTable;
protected import ComponentReference;
protected import Error;
protected import List;
protected import System;
protected import Util;


Expand Down

0 comments on commit 9e8c062

Please sign in to comment.