Skip to content

Commit

Permalink
Improve Base Modelica output (OpenModelica#12582)
Browse files Browse the repository at this point in the history
- Add Base Modelica format options to the dumper functions.
- Implement output formats for scalarized, partially scalarized, and non
  scalarized, with and without records.
  • Loading branch information
perost committed Jun 17, 2024
1 parent ac439f6 commit 32fe465
Show file tree
Hide file tree
Showing 32 changed files with 2,809 additions and 190 deletions.
1 change: 1 addition & 0 deletions OMCompiler/Compiler/.cmake/meta_modelica_source_list.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ set(OMC_MM_BACKEND_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/NBackEnd/Util/NBSlice.mo

# "NFFrontEnd";
${CMAKE_CURRENT_SOURCE_DIR}/NFFrontEnd/BaseModelica.mo
${CMAKE_CURRENT_SOURCE_DIR}/NFFrontEnd/NFAlgorithm.mo
${CMAKE_CURRENT_SOURCE_DIR}/NFFrontEnd/NFArrayConnections.mo
${CMAKE_CURRENT_SOURCE_DIR}/NFFrontEnd/NFAttributes.mo
Expand Down
79 changes: 79 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/BaseModelica.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Linköping University,
* 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
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, 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 BaseModelica
protected
import Flags;

public
type ScalarizeMode = enumeration(
SCALARIZED,
PARTIALLY_SCALARIZED,
NOT_SCALARIZED
);

type RecordMode = enumeration(
WITH_RECORDS,
WITHOUT_RECORDS
);

uniontype OutputFormat
record OUTPUT_FORMAT
ScalarizeMode scalarizeMode;
RecordMode recordMode;
end OUTPUT_FORMAT;
end OutputFormat;

constant OutputFormat defaultFormat = OutputFormat.OUTPUT_FORMAT(ScalarizeMode.PARTIALLY_SCALARIZED, RecordMode.WITH_RECORDS);

function formatFromFlags
output OutputFormat format = defaultFormat;
algorithm
if not Flags.isSet(Flags.NF_SCALARIZE) then
format.scalarizeMode := ScalarizeMode.NOT_SCALARIZED;
end if;

format.recordMode := RecordMode.WITH_RECORDS;

for option in Flags.getConfigStringList(Flags.BASE_MODELICA_FORMAT) loop
() := match option
case "scalarized" algorithm format.scalarizeMode := ScalarizeMode.SCALARIZED; then ();
case "partiallyScalarized" algorithm format.scalarizeMode := ScalarizeMode.PARTIALLY_SCALARIZED; then ();
case "nonScalarized" algorithm format.scalarizeMode := ScalarizeMode.NOT_SCALARIZED; then ();
case "withRecords" algorithm format.recordMode := RecordMode.WITH_RECORDS; then ();
case "withoutRecords" algorithm format.recordMode := RecordMode.WITHOUT_RECORDS; then ();
else ();
end match;
end for;
end formatFromFlags;

annotation(__OpenModelica_Interface="frontend");
end BaseModelica;
12 changes: 7 additions & 5 deletions OMCompiler/Compiler/NFFrontEnd/NFBinding.mo
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
encapsulated uniontype NFBinding

public
import BaseModelica;
import Expression = NFExpression;
import NFInstNode.InstNode;
import Type = NFType;
Expand Down Expand Up @@ -465,17 +466,18 @@ public

function toFlatString
input Binding binding;
input BaseModelica.OutputFormat format;
input String prefix = "";
output String string;
algorithm
string := match binding
case UNBOUND() then "";
case RAW_BINDING() then prefix + Dump.printExpStr(binding.bindingExp);
case UNTYPED_BINDING() then prefix + Expression.toFlatString(binding.bindingExp);
case TYPED_BINDING() then prefix + Expression.toFlatString(binding.bindingExp);
case FLAT_BINDING() then prefix + Expression.toFlatString(binding.bindingExp);
case CEVAL_BINDING() then prefix + Expression.toFlatString(binding.bindingExp);
case INVALID_BINDING() then toFlatString(binding.binding, prefix);
case UNTYPED_BINDING() then prefix + Expression.toFlatString(binding.bindingExp, format);
case TYPED_BINDING() then prefix + Expression.toFlatString(binding.bindingExp, format);
case FLAT_BINDING() then prefix + Expression.toFlatString(binding.bindingExp, format);
case CEVAL_BINDING() then prefix + Expression.toFlatString(binding.bindingExp, format);
case INVALID_BINDING() then toFlatString(binding.binding, format, prefix);
end match;
end toFlatString;

Expand Down
16 changes: 9 additions & 7 deletions OMCompiler/Compiler/NFFrontEnd/NFCall.mo
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ encapsulated uniontype NFCall

import Absyn;
import AbsynUtil;
import BaseModelica;
import DAE;
import Expression = NFExpression;
import NFCallAttributes;
Expand Down Expand Up @@ -797,6 +798,7 @@ public

function toFlatString
input NFCall call;
input BaseModelica.OutputFormat format;
output String str;
protected
String name, arg_str,c;
Expand All @@ -807,12 +809,12 @@ public
case TYPED_CALL()
algorithm
name := AbsynUtil.pathString(Function.nameConsiderBuiltin(call.fn));
arg_str := stringDelimitList(list(Expression.toFlatString(arg) for arg in call.arguments), ", ");
arg_str := stringDelimitList(list(Expression.toFlatString(arg, format) for arg in call.arguments), ", ");
then
if Function.isBuiltin(call.fn) then
stringAppendList({name, "(", arg_str, ")"})
elseif isExternalObjectConstructor(call) then
stringAppendList({Type.toFlatString(call.ty), "(", arg_str, ")"})
stringAppendList({Type.toFlatString(call.ty, format), "(", arg_str, ")"})
else
stringAppendList({Util.makeQuotedIdentifier(name), "(", arg_str, ")"});

Expand All @@ -822,12 +824,12 @@ public
// Vectorized calls contains iterators with illegal Modelica names
// (to avoid name conflicts), to make the flat output legal such
// calls are reverted to their original form here.
str := Expression.toFlatString(devectorizeCall(call));
str := Expression.toFlatString(devectorizeCall(call), format);
else
name := AbsynUtil.pathString(Function.nameConsiderBuiltin(NFBuiltinFuncs.ARRAY_FUNC));
arg_str := Expression.toFlatString(call.exp);
arg_str := Expression.toFlatString(call.exp, format);
c := stringDelimitList(list(Util.makeQuotedIdentifier(InstNode.name(Util.tuple21(iter))) + " in " +
Expression.toFlatString(Util.tuple22(iter)) for iter in call.iters), ", ");
Expression.toFlatString(Util.tuple22(iter), format) for iter in call.iters), ", ");
str := stringAppendList({"{", arg_str, " for ", c, "}"});
end if;
then
Expand All @@ -836,9 +838,9 @@ public
case TYPED_REDUCTION()
algorithm
name := AbsynUtil.pathString(Function.nameConsiderBuiltin(call.fn));
arg_str := Expression.toFlatString(call.exp);
arg_str := Expression.toFlatString(call.exp, format);
c := stringDelimitList(list(Util.makeQuotedIdentifier(InstNode.name(Util.tuple21(iter))) + " in " +
Expression.toFlatString(Util.tuple22(iter)) for iter in call.iters), ", ");
Expression.toFlatString(Util.tuple22(iter), format) for iter in call.iters), ", ");
then
if Function.isBuiltin(call.fn) then
stringAppendList({name, "(", arg_str, " for ", c, ")"})
Expand Down
7 changes: 5 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFClass.mo
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
encapsulated uniontype NFClass

import Attributes = NFAttributes;
import BaseModelica;
import Component = NFComponent;
import Dimension = NFDimension;
import Expression = NFExpression;
Expand Down Expand Up @@ -838,6 +839,7 @@ constant Prefixes DEFAULT_PREFIXES = Prefixes.PREFIXES(
function toFlatStream
input Class cls;
input InstNode clsNode;
input BaseModelica.OutputFormat format;
input String indent;
input output IOStream.IOStream s;
protected
Expand All @@ -855,7 +857,7 @@ constant Prefixes DEFAULT_PREFIXES = Prefixes.PREFIXES(
s := IOStream.append(s, "\n");

for comp in ClassTree.getComponents(cls.elements) loop
s := IOStream.append(s, InstNode.toFlatString(comp, indent + " "));
s := IOStream.append(s, InstNode.toFlatString(comp, format, indent + " "));
s := IOStream.append(s, ";\n");
end for;

Expand Down Expand Up @@ -892,13 +894,14 @@ constant Prefixes DEFAULT_PREFIXES = Prefixes.PREFIXES(
function toFlatString
input Class cls;
input InstNode clsNode;
input BaseModelica.OutputFormat format = BaseModelica.defaultFormat;
input String indent = "";
output String str;
protected
IOStream.IOStream s;
algorithm
s := IOStream.create(getInstanceName(), IOStream.IOStreamType.LIST());
s := toFlatStream(cls, clsNode, indent, s);
s := toFlatStream(cls, clsNode, format, indent, s);
str := IOStream.string(s);
IOStream.delete(s);
end toFlatString;
Expand Down
10 changes: 6 additions & 4 deletions OMCompiler/Compiler/NFFrontEnd/NFClockKind.mo
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

encapsulated uniontype NFClockKind
import Absyn;
import BaseModelica;
import DAE;
import Expression = NFExpression;

Expand Down Expand Up @@ -500,17 +501,18 @@ public

function toFlatString
input ClockKind ck;
input BaseModelica.OutputFormat format;
output String str;
algorithm
str := match ck
local
Expression e1, e2;

case INFERRED_CLOCK() then "";
case RATIONAL_CLOCK(e1, e2) then Expression.toFlatString(e1) + ", " + Expression.toFlatString(e2);
case REAL_CLOCK(e1) then Expression.toFlatString(e1);
case EVENT_CLOCK(e1, e2) then Expression.toFlatString(e1) + ", " + Expression.toFlatString(e2);
case SOLVER_CLOCK(e1, e2) then Expression.toFlatString(e1) + ", " + Expression.toFlatString(e2);
case RATIONAL_CLOCK(e1, e2) then Expression.toFlatString(e1, format) + ", " + Expression.toFlatString(e2, format);
case REAL_CLOCK(e1) then Expression.toFlatString(e1, format);
case EVENT_CLOCK(e1, e2) then Expression.toFlatString(e1, format) + ", " + Expression.toFlatString(e2, format);
case SOLVER_CLOCK(e1, e2) then Expression.toFlatString(e1, format) + ", " + Expression.toFlatString(e2, format);
end match;

str := "Clock(" + str + ")";
Expand Down
16 changes: 10 additions & 6 deletions OMCompiler/Compiler/NFFrontEnd/NFComponent.mo
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

encapsulated uniontype NFComponent

import BaseModelica;
import Binding = NFBinding;
import Class = NFClass;
import NFClassTree.ClassTree;
Expand Down Expand Up @@ -722,6 +723,7 @@ public
function toFlatStream
input String name;
input Component component;
input BaseModelica.OutputFormat format;
input String indent;
input output IOStream.IOStream s;
protected
Expand All @@ -732,23 +734,23 @@ public
algorithm
s := IOStream.append(s, indent);
s := Attributes.toFlatStream(component.attributes, component.ty, s);
s := IOStream.append(s, Type.toFlatString(component.ty));
s := IOStream.append(s, Type.toFlatString(component.ty, format));
s := IOStream.append(s, " '");
s := IOStream.append(s, name);
s := IOStream.append(s, "'");

ty_attrs := list((Modifier.name(a), Modifier.binding(a)) for a in
Class.getTypeAttributes(InstNode.getClass(component.classInst)));
s := typeAttrsToFlatStream(ty_attrs, component.ty, s);
s := typeAttrsToFlatStream(ty_attrs, component.ty, format, s);

s := IOStream.append(s, Binding.toFlatString(component.binding, " = "));
s := IOStream.append(s, Binding.toFlatString(component.binding, format, " = "));
then
();

case TYPE_ATTRIBUTE()
algorithm
s := IOStream.append(s, name);
s := IOStream.append(s, Modifier.toFlatString(component.modifier, printName = false));
s := IOStream.append(s, Modifier.toFlatString(component.modifier, format, printName = false));
then
();
end match;
Expand All @@ -757,6 +759,7 @@ public
function typeAttrsToFlatStream
input list<tuple<String, Binding>> typeAttrs;
input Type componentType;
input BaseModelica.OutputFormat format;
input output IOStream.IOStream s;
protected
Integer var_dims, binding_dims;
Expand All @@ -783,7 +786,7 @@ public

s := IOStream.append(s, name);
s := IOStream.append(s, " = ");
s := IOStream.append(s, Expression.toFlatString(bind_exp));
s := IOStream.append(s, Expression.toFlatString(bind_exp, format));

ty_attrs := listRest(ty_attrs);
if listEmpty(ty_attrs) then
Expand All @@ -799,13 +802,14 @@ public
function toFlatString
input String name;
input Component component;
input BaseModelica.OutputFormat format;
input String indent = "";
output String str;
protected
IOStream.IOStream s;
algorithm
s := IOStream.create(name, IOStream.IOStreamType.LIST());
s := toFlatStream(name, component, indent, s);
s := toFlatStream(name, component, format, indent, s);
str := IOStream.string(s);
IOStream.delete(s);
end toFlatString;
Expand Down
Loading

0 comments on commit 32fe465

Please sign in to comment.