Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 4221d29

Browse files
sjoelundOpenModelica-Hudson
authored andcommitted
Add an extra step before daeExp in Susan
We now check if arrays are stored in contiguous memory before blindly creating an array that assumes so.
1 parent 97fddd8 commit 4221d29

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

Compiler/SimCode/SimCodeUtil.mo

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13380,6 +13380,45 @@ algorithm
1338013380
end matchcontinue;
1338113381
end cref2simvar;
1338213382

13383+
public function codegenExpSanityCheck "Handle some things that Susan cannot handle:
13384+
* Expand simulation context arrays that contain variables stored in different locations...
13385+
* We could move collapsing arrays here since it should be safer to do so when we can lookup which index a variable corresponds to...
13386+
"
13387+
input output DAE.Exp e;
13388+
input SimCodeFunction.Context context;
13389+
protected
13390+
list<SimCodeVar.SimVar> vars;
13391+
SimCode.SimCode simCode;
13392+
Integer index;
13393+
algorithm
13394+
if match context
13395+
case SimCodeFunction.FUNCTION_CONTEXT() then true;
13396+
case SimCodeFunction.PARALLEL_FUNCTION_CONTEXT() then true;
13397+
else false; end match
13398+
then
13399+
return;
13400+
end if;
13401+
e := match e
13402+
case DAE.CREF(ty=DAE.T_ARRAY())
13403+
algorithm
13404+
simCode := getSimCode();
13405+
vars := list(cref2simvar(cr, simCode) for cr in ComponentReference.expandCref(e.componentRef, true));
13406+
if not listEmpty(vars) then
13407+
SimCodeVar.SIMVAR(index=index)::vars := vars;
13408+
for v in vars loop
13409+
// The array needs to be expanded because it's not stored in contiguous memory
13410+
if v.index <> index+1 then
13411+
e := Expression.expandCrefs(e);
13412+
break;
13413+
end if;
13414+
index := v.index;
13415+
end for;
13416+
end if;
13417+
then e;
13418+
else e;
13419+
end match;
13420+
end codegenExpSanityCheck;
13421+
1338313422
public function isModelTooBigForCSharpInOneFile
1338413423
"Used by C# template to determine if the generated code should be split into several files
1338513424
to make Visual Studio responsive when the file is opened (C# compiler is OK,

Compiler/Stubs/SimCodeUtil.mo

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
encapsulated package SimCodeUtil
22

33
import SimCode;
4+
import SimCodeFunction;
45
import SimCodeVar;
56

67
function sortEqSystems<T>
@@ -31,5 +32,13 @@ algorithm
3132
assert(false, getInstanceName());
3233
end cref2simvar;
3334

35+
function codegenExpSanityCheck
36+
input output DAE.Exp e;
37+
input SimCodeFunction.Context context;
38+
algorithm
39+
/* Do nothing */
40+
end codegenExpSanityCheck;
41+
42+
3443
annotation(__OpenModelica_Interface="backend");
3544
end SimCodeUtil;

Compiler/Template/CodegenCFunctions.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4303,7 +4303,7 @@ end getTempDeclMatchOutputName;
43034303
"Generates code for an expression.
43044304
used in Compiler/Template/CodegenQSS.tpl"
43054305
::=
4306-
match exp
4306+
match codegenExpSanityCheck(exp, context)
43074307
case e as ICONST(__) then '((modelica_integer) <%integer%>)' /* Yes, we need to cast int to long on 64-bit arch... */
43084308
case e as RCONST(__) then real
43094309
case e as SCONST(__) then daeExpSconst(string, &preExp, &varDecls)

Compiler/Template/SimCodeTV.mo

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,12 @@ package SimCodeUtil
985985
input SimCode.SimCode simCode;
986986
output Boolean outIsTooBig;
987987
end isModelTooBigForCSharpInOneFile;
988+
989+
function codegenExpSanityCheck
990+
input DAE.Exp inExp;
991+
input SimCodeFunction.Context context;
992+
output DAE.Exp outExp;
993+
end codegenExpSanityCheck;
988994
end SimCodeUtil;
989995

990996
package SimCodeFunctionUtil

0 commit comments

Comments
 (0)