Skip to content

Commit

Permalink
Add an extra step before daeExp in Susan
Browse files Browse the repository at this point in the history
We now check if arrays are stored in contiguous memory before blindly
creating an array that assumes so.
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed Jul 24, 2017
1 parent 97fddd8 commit 4221d29
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
39 changes: 39 additions & 0 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -13380,6 +13380,45 @@ algorithm
end matchcontinue;
end cref2simvar;

public function codegenExpSanityCheck "Handle some things that Susan cannot handle:
* Expand simulation context arrays that contain variables stored in different locations...
* We could move collapsing arrays here since it should be safer to do so when we can lookup which index a variable corresponds to...
"
input output DAE.Exp e;
input SimCodeFunction.Context context;
protected
list<SimCodeVar.SimVar> vars;
SimCode.SimCode simCode;
Integer index;
algorithm
if match context
case SimCodeFunction.FUNCTION_CONTEXT() then true;
case SimCodeFunction.PARALLEL_FUNCTION_CONTEXT() then true;
else false; end match
then
return;
end if;
e := match e
case DAE.CREF(ty=DAE.T_ARRAY())
algorithm
simCode := getSimCode();
vars := list(cref2simvar(cr, simCode) for cr in ComponentReference.expandCref(e.componentRef, true));
if not listEmpty(vars) then
SimCodeVar.SIMVAR(index=index)::vars := vars;
for v in vars loop
// The array needs to be expanded because it's not stored in contiguous memory
if v.index <> index+1 then
e := Expression.expandCrefs(e);
break;
end if;
index := v.index;
end for;
end if;
then e;
else e;
end match;
end codegenExpSanityCheck;

public function isModelTooBigForCSharpInOneFile
"Used by C# template to determine if the generated code should be split into several files
to make Visual Studio responsive when the file is opened (C# compiler is OK,
Expand Down
9 changes: 9 additions & 0 deletions Compiler/Stubs/SimCodeUtil.mo
@@ -1,6 +1,7 @@
encapsulated package SimCodeUtil

import SimCode;
import SimCodeFunction;
import SimCodeVar;

function sortEqSystems<T>
Expand Down Expand Up @@ -31,5 +32,13 @@ algorithm
assert(false, getInstanceName());
end cref2simvar;

function codegenExpSanityCheck
input output DAE.Exp e;
input SimCodeFunction.Context context;
algorithm
/* Do nothing */
end codegenExpSanityCheck;


annotation(__OpenModelica_Interface="backend");
end SimCodeUtil;
2 changes: 1 addition & 1 deletion Compiler/Template/CodegenCFunctions.tpl
Expand Up @@ -4303,7 +4303,7 @@ end getTempDeclMatchOutputName;
"Generates code for an expression.
used in Compiler/Template/CodegenQSS.tpl"
::=
match exp
match codegenExpSanityCheck(exp, context)
case e as ICONST(__) then '((modelica_integer) <%integer%>)' /* Yes, we need to cast int to long on 64-bit arch... */
case e as RCONST(__) then real
case e as SCONST(__) then daeExpSconst(string, &preExp, &varDecls)
Expand Down
6 changes: 6 additions & 0 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -985,6 +985,12 @@ package SimCodeUtil
input SimCode.SimCode simCode;
output Boolean outIsTooBig;
end isModelTooBigForCSharpInOneFile;

function codegenExpSanityCheck
input DAE.Exp inExp;
input SimCodeFunction.Context context;
output DAE.Exp outExp;
end codegenExpSanityCheck;
end SimCodeUtil;

package SimCodeFunctionUtil
Expand Down

0 comments on commit 4221d29

Please sign in to comment.