Skip to content

Commit

Permalink
Test Julia conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoelund committed May 23, 2019
1 parent dbe3960 commit 64a019f
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 37 deletions.
1 change: 1 addition & 0 deletions OMCompiler/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Compiler/Template/GenerateAPIFunctionsTpl.mo
Compiler/Template/GraphMLDumpTpl.mo
Compiler/Template/GraphvizDump.mo
Compiler/Template/Makefile
Compiler/Template/MMToJulia.mo
Compiler/Template/NFInstDumpTpl.mo
Compiler/Template/SCodeDumpTpl.mo
Compiler/Template/SimCodeDump.mo
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/3rdParty
3 changes: 1 addition & 2 deletions OMCompiler/Compiler/FrontEnd/Absyn.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4316,9 +4316,8 @@ algorithm
Ident id;
list<Subscript> sub;
ComponentRef cr2,cr_1,cr;
case (CREF_IDENT(),CREF_FULLYQUALIFIED()) then fail();
case (CREF_IDENT(name = id,subscripts = sub),cr2)
equation
failure(CREF_FULLYQUALIFIED() = cr2);
then CREF_QUAL(id,sub,cr2);
case (CREF_QUAL(name = id,subscripts = sub,componentRef = cr),cr2)
equation
Expand Down
57 changes: 26 additions & 31 deletions OMCompiler/Compiler/FrontEnd/Graphviz.mo
Original file line number Diff line number Diff line change
Expand Up @@ -43,53 +43,46 @@ encapsulated package Graphviz
Input: The tree constructed from data structures in Graphviz
Output: Textual input to graphviz, written to stdout."

public
type Type = String;

public
type Ident = String;

public
type Label = String;

public
uniontype Attribute "an Attribute is a pair of name an value."
record ATTR
String name "name" ;
String value "value" ;
end ATTR;

end Attribute;

type Attributes = list<Attribute>;

uniontype Node "A graphviz Node is a node of the graph.
It has a type and attributes and children.
It can also have a list of labels, provided by the LNODE
constructor."
record NODE
Type type_;
Attributes attributes;
Children children;
list<Node> children;
end NODE;

record LNODE
Type type_;
list<Label> labelLst;
Attributes attributes;
Children children;
list<Node> children;
end LNODE;

end Node;

public
type Children = list<Node>;

public
type Attributes = list<Attribute>;

public
uniontype Attribute "an Attribute is a pair of name an value."
record ATTR
String name "name" ;
String value "value" ;
end ATTR;
constant Attribute box=ATTR("shape","box");

end Attribute;

public constant Attribute box=ATTR("shape","box");

public function dump "Relations
function dump "Relations
function: dump
Dumps a Graphviz Node on stdout."
input Node node;
Expand All @@ -101,7 +94,9 @@ algorithm
print("}\n");
end dump;

protected function dumpNode "Dumps a node to a string."
protected

function dumpNode "Dumps a node to a string."
input Node inNode;
output Ident outIdent;
algorithm
Expand Down Expand Up @@ -137,7 +132,7 @@ algorithm
end match;
end dumpNode;

protected function makeLabel "Creates a label from a list of strings."
function makeLabel "Creates a label from a list of strings."
input list<String> sl;
output String s2;
protected
Expand All @@ -148,7 +143,7 @@ algorithm
s2 := stringAppend(s1, "\"");
end makeLabel;

protected function makeLabelReq "Helper function to makeLabel"
function makeLabelReq "Helper function to makeLabel"
input list<String> inStringLst;
input String inString;
output String outString;
Expand Down Expand Up @@ -176,7 +171,7 @@ algorithm
end match;
end makeLabelReq;

protected function dumpChildren "Helper function to dumpNode"
function dumpChildren "Helper function to dumpNode"
input Ident inIdent;
input Children inChildren;
algorithm
Expand All @@ -198,7 +193,7 @@ algorithm
end match;
end dumpChildren;

protected function nodename "Creates a unique node name,
function nodename "Creates a unique node name,
changed use of str as part of nodename, since it may contain spaces"
input String str;
output String s;
Expand All @@ -211,7 +206,7 @@ algorithm
s := stringAppend("GVNOD", is);
end nodename;

protected function printEdge "Prints an edge between two nodes."
function printEdge "Prints an edge between two nodes."
input Ident n1;
input Ident n2;
protected
Expand All @@ -222,7 +217,7 @@ algorithm
print(";\n");
end printEdge;

protected function makeEdge "Creates a string representing an edge between two nodes."
function makeEdge "Creates a string representing an edge between two nodes."
input Ident n1;
input Ident n2;
output String str;
Expand All @@ -233,7 +228,7 @@ algorithm
str := stringAppend(s, n2);
end makeEdge;

protected function makeNode "Creates string from a node."
function makeNode "Creates string from a node."
input Ident nm;
input Attributes attr;
output String str;
Expand All @@ -245,7 +240,7 @@ algorithm
str := stringAppend(s_1, ";");
end makeNode;

protected function makeAttr "Creates a string from an Attribute list."
function makeAttr "Creates a string from an Attribute list."
input list<Attribute> l;
output String str;
protected
Expand All @@ -256,7 +251,7 @@ algorithm
str := stringAppend(s, "]");
end makeAttr;

protected function makeAttrReq "Helper function to makeAttr."
function makeAttrReq "Helper function to makeAttr."
input list<Attribute> inAttributeLst;
input String inString;
output String outString;
Expand Down
5 changes: 5 additions & 0 deletions OMCompiler/Compiler/FrontEnd/ModelicaBuiltin.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4161,6 +4161,11 @@ annotation(
</html>"), preferredView="text");
end relocateFunctions;

function toJulia
output String res;
external "builtin";
end toJulia;

end Experimental;

end Scripting;
Expand Down
14 changes: 12 additions & 2 deletions OMCompiler/Compiler/FrontEnd/SCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,11 @@ algorithm
case Absyn.ALG_BREAK() then SCode.ALG_BREAK(comment, info);
case Absyn.ALG_CONTINUE() then SCode.ALG_CONTINUE(comment, info);

else
algorithm
Error.addInternalError("Failed to translate to SCode for AlgorithmItem: " + anyString(inAlgorithm), sourceInfo());
then fail();

end match;
end translateClassdefAlgorithmItem;

Expand Down Expand Up @@ -966,7 +971,7 @@ public function translateEitemlist
The boolean argument flags whether the elements are protected.
Annotations are not translated, i.e. they are removed when converting to SCode."
input list<Absyn.ElementItem> inAbsynElementItemLst;
input SCode.Visibility inVisibility;
input SCode.Visibility inVisibility = SCode.PUBLIC();
output list<SCode.Element> outElementLst;
protected
list<SCode.Element> l = {};
Expand Down Expand Up @@ -1418,7 +1423,7 @@ algorithm
end match;
end translateVariability;

protected function translateEquations
public function translateEquations
"This function transforms a list of Absyn.Equation to a list of
SCode.Equation, by applying the translateEquation function to each
equation."
Expand Down Expand Up @@ -1689,6 +1694,11 @@ algorithm
then SCode.EQ_NORETCALL(Absyn.CALL(inEquation.functionName, inEquation.functionArgs),
inComment, inInfo);

else
algorithm
Error.addInternalError("Failed to translate equation to SCode (is it a MetaModelica construct that should be in an algorithm?)", inInfo);
then fail();

end match;
end translateEquation;

Expand Down
7 changes: 7 additions & 0 deletions OMCompiler/Compiler/Script/CevalScriptBackend.mo
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import Parser;
import Print;
import Refactor;
import SCodeDump;
import MMToJulia;
import NFInst;
import NFSCodeEnv;
import NFSCodeFlatten;
Expand Down Expand Up @@ -2964,6 +2965,12 @@ algorithm
b := System.relocateFunctions(str, relocatableFunctionsTuple);
then (cache,Values.BOOL(b));

case (cache,_,"toJulia",{},_)
algorithm
sp := SymbolTable.getSCode();
str := Tpl.tplString(MMToJulia.dumpProgram, sp);
then (cache,Values.STRING(str));

case (cache,_,"relocateFunctions",_,_)
then (cache,Values.BOOL(false));

Expand Down
7 changes: 6 additions & 1 deletion OMCompiler/Compiler/Template/Makefile.common
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY : all check_tabs

GENERATED_FILES_BOOT_STAGE2=AbsynDumpTpl.mo CodegenUtil.mo DAEDumpTpl.mo ExpressionDumpTpl.mo Unparsing.mo SCodeDumpTpl.mo GenerateAPIFunctionsTpl.mo CodegenCFunctions.mo
GENERATED_FILES=$(GENERATED_FILES_BOOT_STAGE2) CodegenC.mo CodegenUtilSimulation.mo CodegenEmbeddedC.mo CodegenFMUCommon.mo CodegenFMU.mo CodegenFMU1.mo CodegenFMU2.mo CodegenCSharp.mo CodegenCppCommon.mo CodegenCpp.mo CodegenCppHpcom.mo CodegenFMUCpp.mo CodegenOMSI_common.mo CodegenOMSIC.mo CodegenOMSIC_Equations.mo CodegenFMUCppHpcom.mo CodegenCppInit.mo CodegenMidToC.mo CodegenModelica.mo GraphvizDump.mo GraphMLDumpTpl.mo NFInstDumpTpl.mo SimCodeDump.mo CodegenAdevs.mo CodegenSparseFMI.mo CodegenXML.mo CodegenJava.mo CodegenJS.mo TaskSystemDump.mo VisualXMLTpl.mo
GENERATED_FILES=$(GENERATED_FILES_BOOT_STAGE2) CodegenC.mo CodegenUtilSimulation.mo CodegenEmbeddedC.mo CodegenFMUCommon.mo CodegenFMU.mo CodegenFMU1.mo CodegenFMU2.mo CodegenCSharp.mo CodegenCppCommon.mo CodegenCpp.mo CodegenCppHpcom.mo CodegenFMUCpp.mo CodegenOMSI_common.mo CodegenOMSIC.mo CodegenOMSIC_Equations.mo CodegenFMUCppHpcom.mo CodegenCppInit.mo CodegenMidToC.mo CodegenModelica.mo GraphvizDump.mo GraphMLDumpTpl.mo NFInstDumpTpl.mo SimCodeDump.mo CodegenAdevs.mo CodegenSparseFMI.mo CodegenXML.mo CodegenJava.mo CodegenJS.mo TaskSystemDump.mo VisualXMLTpl.mo MMToJulia.mo

all : $(GENERATED_FILES) TplCodegen.mo check_tabs
templates-bootstrap : $(GENERATED_FILES_BOOT_STAGE2) TplCodegen.mo check_tabs
Expand Down Expand Up @@ -198,5 +198,10 @@ VisualXMLTpl.mo : VisualXMLTpl.tpl VisualXMLTplTV.mo
$(OMC) $< > $@.log || (cat $@.log && false)
@echo " "

MMToJulia.mo : MMToJulia.tpl MMToJuliaTV.mo
@echo " ** MMToJulia template compilation ** "
$(OMC) $< > $@.log || (cat $@.log && false)
@echo " "

clean:
rm -f $(GENERATED_FILES)
25 changes: 25 additions & 0 deletions OMCompiler/Compiler/Template/SCodeTV.mo
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ package builtin
input list<TypeVar> lst;
output Integer result;
end listLength;

uniontype SourceInfo "The Info attribute provides location information for elements and classes."
record SOURCEINFO
String fileName;
Boolean isReadOnly;
Integer lineNumberStart;
Integer columnNumberStart;
Integer lineNumberEnd;
Integer columnNumberEnd;
Real lastModification;
end SOURCEINFO;
end SourceInfo;

end builtin;

package Absyn
Expand Down Expand Up @@ -838,6 +851,11 @@ package Tpl
function addTemplateError
input String inErrMsg;
end addTemplateError;
function addSourceTemplateError
"Wraps call to Error.addSourceMessage() funtion with Error.TEMPLATE_ERROR and one MessageToken."
input String inErrMsg;
input builtin.SourceInfo inInfo;
end addSourceTemplateError;
end Tpl;

package Config
Expand Down Expand Up @@ -870,4 +888,11 @@ package Util
end makeStatefulBoolean;
end Util;

package Error
function infoStr
input builtin.SourceInfo info;
output String str;
end infoStr;
end Error;

end SCodeTV;
2 changes: 2 additions & 0 deletions OMCompiler/Compiler/boot/LoadCompilerSources.mos
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ if true then /* Suppress output */
"../Script/Binding.mo",
"../Script/OpenModelicaScriptingAPI.mo",
"../Script/CevalScriptBackend.mo",
"../Script/MMToJuliaUtil.mo",

"../SimCode/HpcOmSimCodeMain.mo",
"../SimCode/SerializeInitXML.mo",
Expand Down Expand Up @@ -406,6 +407,7 @@ if true then /* Suppress output */
"../Template/CodegenXML.mo",
"../Template/GraphvizDump.mo",
"../Template/GraphMLDumpTpl.mo",
"../Template/MMToJulia.mo",
"../Template/NFInstDumpTpl.mo",
"../Template/SimCodeDump.mo",
"../Template/TaskSystemDump.mo",
Expand Down

0 comments on commit 64a019f

Please sign in to comment.