Skip to content

Commit

Permalink
Simplify more statements in functions (#11607)
Browse files Browse the repository at this point in the history
- Simplify more statements such as asserts in functions to make sure we
  don't get any `getInstanceName` calls in the flat model.
  • Loading branch information
perost committed Nov 18, 2023
1 parent 8fef40a commit 5b94ed1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFCeval.mo
Expand Up @@ -3073,7 +3073,7 @@ algorithm
end match;
end evalSolverClock;

function evalGetInstanceName
public function evalGetInstanceName
input Expression scopeArg;
output Expression result;
protected
Expand All @@ -3086,7 +3086,7 @@ algorithm
result := Expression.STRING(AbsynUtil.pathString(InstNode.rootPath(ComponentRef.node(cref))));
end evalGetInstanceName;

function evalArrayConstructor
protected function evalArrayConstructor
input Expression callExp;
output Expression result;
protected
Expand Down
27 changes: 14 additions & 13 deletions OMCompiler/Compiler/NFFrontEnd/NFSimplifyExp.mo
Expand Up @@ -251,19 +251,20 @@ algorithm
then
exp;

case "delay" then simplifyDelay(args, call);
case "der" then simplifyDer(listHead(args), call);
case "fill" then simplifyFill(listHead(args), listRest(args), call, expand);
case "homotopy" then simplifyHomotopy(args, call);
case "max" then simplifyMinMax(args, call, isMin = false);
case "min" then simplifyMinMax(args, call, isMin = true);
case "ones" then simplifyFill(Expression.INTEGER(1), args, call, expand);
case "product" then simplifySumProduct(listHead(args), call, expand, isSum = false);
case "sum" then simplifySumProduct(listHead(args), call, expand, isSum = true);
case "transpose" then simplifyTranspose(listHead(args), call, expand);
case "vector" then simplifyVector(listHead(args), call);
case "zeros" then simplifyFill(Expression.INTEGER(0), args, call, expand);
case "semiLinear" then simplifySemiLinear(args, call);
case "delay" then simplifyDelay(args, call);
case "der" then simplifyDer(listHead(args), call);
case "fill" then simplifyFill(listHead(args), listRest(args), call, expand);
case "homotopy" then simplifyHomotopy(args, call);
case "max" then simplifyMinMax(args, call, isMin = false);
case "min" then simplifyMinMax(args, call, isMin = true);
case "ones" then simplifyFill(Expression.INTEGER(1), args, call, expand);
case "product" then simplifySumProduct(listHead(args), call, expand, isSum = false);
case "sum" then simplifySumProduct(listHead(args), call, expand, isSum = true);
case "transpose" then simplifyTranspose(listHead(args), call, expand);
case "vector" then simplifyVector(listHead(args), call);
case "zeros" then simplifyFill(Expression.INTEGER(0), args, call, expand);
case "semiLinear" then simplifySemiLinear(args, call);
case "getInstanceName" then Ceval.evalGetInstanceName(listHead(args));

else Expression.CALL(call);
end match;
Expand Down
21 changes: 21 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFSimplifyModel.mo
Expand Up @@ -340,6 +340,27 @@ algorithm
then
stmt :: statements;

case Statement.ASSERT()
algorithm
stmt.condition := SimplifyExp.simplify(stmt.condition);
stmt.message := SimplifyExp.simplify(stmt.message);
stmt.level := SimplifyExp.simplify(stmt.level);
then
stmt :: statements;

case Statement.TERMINATE()
algorithm
stmt.message := SimplifyExp.simplify(stmt.message);
then
stmt :: statements;

case Statement.WHILE()
algorithm
stmt.condition := SimplifyExp.simplify(stmt.condition);
stmt.body := simplifyStatements(stmt.body);
then
stmt :: statements;

case Statement.NORETCALL()
algorithm
e := SimplifyExp.simplify(stmt.exp);
Expand Down
Expand Up @@ -29,6 +29,12 @@ model FuncBuiltinGetInstanceName
output String s = getInstanceName();
end f;

function f2
input Real dummy;
algorithm
assert(true, getInstanceName());
end f2;

String s = getInstanceName();
A a;
A a1[2];
Expand All @@ -38,9 +44,17 @@ model FuncBuiltinGetInstanceName
String pas = P.a.s;
constant String fs = f();
Real rs(displayUnit = getInstanceName());
equation
f2(time);
end FuncBuiltinGetInstanceName;

// Result:
// function FuncBuiltinGetInstanceName.f2
// input Real dummy;
// algorithm
// assert(true, "FuncBuiltinGetInstanceName.f2");
// end FuncBuiltinGetInstanceName.f2;
//
// class FuncBuiltinGetInstanceName
// String s = "FuncBuiltinGetInstanceName";
// String a.b.s = "FuncBuiltinGetInstanceName.a.b";
Expand All @@ -59,5 +73,7 @@ end FuncBuiltinGetInstanceName;
// String pas = "P.a";
// constant String fs = "FuncBuiltinGetInstanceName.f";
// Real rs(displayUnit = "FuncBuiltinGetInstanceName");
// equation
// FuncBuiltinGetInstanceName.f2(time);
// end FuncBuiltinGetInstanceName;
// endResult

0 comments on commit 5b94ed1

Please sign in to comment.