Skip to content

Commit e76c170

Browse files
author
Kristian Stavåker
committed
Made some improvements and fixed some bugs in the valueblock and for iterator constructs.
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2856 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 0662428 commit e76c170

File tree

5 files changed

+121
-20
lines changed

5 files changed

+121
-20
lines changed

Compiler/Codegen.mo

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3758,7 +3758,7 @@ algorithm
37583758
var_1 = Util.stringAppendList({"((modelica_real)",var,")"});
37593759
then
37603760
(cfn,var_1,tnr_1);
3761-
case (Exp.VALUEBLOCK(localDecls = ld,body = b,
3761+
case (Exp.VALUEBLOCK(ty,localDecls = ld,body = b,
37623762
result = res),tnr,context)
37633763
local
37643764
list<Exp.DAEElement> ld;
@@ -3778,9 +3778,14 @@ algorithm
37783778

37793779
(cfn1_2,var,tnr3) = generateExpression(res, tnr2, context);
37803780

3781-
cfn1_2 = cMergeFns({cfn,cfn1,cfn1_2});
3781+
//-----
3782+
(cfn1_2,tnr4,var) = addValueblockRetVar(ty,cfn1_2,tnr3,var,context);
3783+
//-----
3784+
3785+
cfn1_2 = cMergeFns({cfn,cfn1,cfn1_2});
3786+
37823787
cfn1_2 = cAddBlockAroundStatements(cfn1_2);
3783-
then (cfn1_2,var,tnr2);
3788+
then (cfn1_2,var,tnr4);
37843789

37853790
case (Exp.ASUB(exp = _),_,_)
37863791
equation
@@ -3852,6 +3857,70 @@ algorithm
38523857

38533858
end cAddBlockAroundStatements;
38543859

3860+
protected function addValueblockRetVar "function: addValueblockRetVar
3861+
author: KS
3862+
Used by generateExpression - Valueblock. Adds a return variable (and return
3863+
assignment) to the valueblock code.
3864+
"
3865+
input Exp.Type inType;
3866+
input CFunction inFunc;
3867+
input Integer inTnr;
3868+
input String inResVar;
3869+
input Context context;
3870+
output CFunction outFunc;
3871+
output Integer outTnr;
3872+
output String outVar;
3873+
algorithm
3874+
(outFunc,outTnr,outVar) :=
3875+
matchcontinue (inType,inFunc,inTnr,inResVar,context)
3876+
case (Exp.T_ARRAY(t,SOME(arrayDim) :: {}),localFunc,localTnr,localResVar,con)
3877+
local
3878+
String type_string,tdecl,tvar,localResVar,memStr,stmt,stmt2,tempStr;
3879+
Exp.Type t;
3880+
CFunction localFunc,cfn;
3881+
Integer localTnr,arrayDim,tnr2;
3882+
Boolean sim;
3883+
Context con;
3884+
equation // ONLY 1 DIMENSIONAL ARRAYS SUPPORTED AS OF NOW
3885+
// Create temp array var
3886+
type_string = expTypeStr(t,true);
3887+
(tdecl,tvar,tnr2) = generateTempDecl(type_string, localTnr);
3888+
cfn = cAddVariables(localFunc, {tdecl});
3889+
3890+
// Allocate temp array var
3891+
tempStr = intString(arrayDim);
3892+
stmt = Util.stringAppendList({"alloc_",type_string,"(&",tvar,",1,",tempStr,");"});
3893+
cfn = cAddInits(cfn,{stmt});
3894+
3895+
// Create the result var assignment
3896+
sim = isSimulationContext(con);
3897+
memStr = Util.if_(sim,"_mem","");
3898+
stmt2 = Util.stringAppendList({"copy_",type_string,"_data",memStr,"(&",localResVar,", &",tvar,");"});
3899+
cfn = cAddStatements(cfn, {stmt2});
3900+
then (cfn,tnr2,tvar);
3901+
case (Exp.T_ARRAY(_,SOME(_) :: _),_,_,_,_)
3902+
local
3903+
equation
3904+
Debug.fprintln("failtrace", "# Codegen: Only 1 dim return arrays from Valueblock implemented");
3905+
then fail();
3906+
case (localType,localFunc,localTnr,localResVar,_)
3907+
local
3908+
String type_string,tdecl,tvar,localResVar;
3909+
Exp.Type localType;
3910+
CFunction localFunc,cfn;
3911+
Integer localTnr,tnr2;
3912+
String stmt;
3913+
equation
3914+
type_string = expTypeStr(localType,false);
3915+
(tdecl,tvar,tnr2) = generateTempDecl(type_string, localTnr);
3916+
cfn = cAddVariables(localFunc, {tdecl});
3917+
3918+
stmt = Util.stringAppendList({tvar," = ",localResVar,";"});
3919+
cfn = cAddStatements(cfn,{stmt});
3920+
then (cfn,tnr2,tvar);
3921+
end matchcontinue;
3922+
end addValueblockRetVar;
3923+
38553924
protected function cMoveDeclsAndInitsToStatements "function: cMoveStatementsToInits
38563925
38573926
Moves all statements of the body to initialization statements.

Compiler/Exp.mo

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,10 @@ uniontype Exp "Expressions
227227
record END "array index to last element, e.g. a{end}:=1;" end END;
228228

229229
record VALUEBLOCK "Valueblock expression"
230-
list<DAEElement> localDecls;
230+
Type ty;
231+
list<DAEElement> localDecls;
231232
DAEElement body;
232-
Exp result;
233+
Exp result;
233234
end VALUEBLOCK;
234235

235236
/* Part of MetaModelica extension. KS */

Compiler/Inst.mo

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8202,7 +8202,6 @@ algorithm
82028202
Absyn.Algorithm absynStmt;
82038203
list<Absyn.Ident> idList;
82048204
Absyn.ComponentRef c;
8205-
Absyn.Ident id;
82068205
equation
82078206
// rangeList = {(id,e2)};
82088207
idList = extractLoopVars(rangeList,{});
@@ -8222,7 +8221,6 @@ algorithm
82228221
Absyn.ForIterators rangeList;
82238222
Absyn.Algorithm absynStmt,temp;
82248223
list<Absyn.Ident> idList;
8225-
Absyn.Ident id;
82268224
Absyn.ComponentRef c1,c2;
82278225
list<Absyn.ElementItem> declList;
82288226
list<Absyn.AlgorithmItem> vb_body;
@@ -8243,7 +8241,7 @@ algorithm
82438241

82448242
(cache,stmt) = instStatement(cache,env,pre,absynStmt,impl);
82458243
then
8246-
(cache,stmt);
8244+
(cache,stmt);
82478245

82488246
/* v := expr; */
82498247
case (cache,env,pre,Absyn.ALG_ASSIGN(assignComponent = Absyn.CREF(cr),value = e),impl)
@@ -10270,7 +10268,7 @@ algorithm
1027010268
list<Absyn.Subscript> localAccList;
1027110269
Env.Env localEnv;
1027210270
Env.Cache localCache;
10273-
case (localCache,localEnv,{},_,localAccList) then (localCache,localAccList);
10271+
case (localCache,_,{},_,localAccList) then (localCache,localAccList);
1027410272
case (localCache,localEnv,(_,SOME(e)) :: restList,localImpl,localAccList)
1027510273
local
1027610274
Absyn.Exp e;
@@ -10282,6 +10280,7 @@ algorithm
1028210280
(localCache,_,Types.PROP((Types.T_ARRAY(Types.DIM(SOME(i)),_),NONE()),_),_) = Static.elabExp(localCache,localEnv,e,localImpl,NONE(),false);
1028310281
elem = {Absyn.SUBSCRIPT(Absyn.INTEGER(i))};
1028410282
localAccList = listAppend(localAccList,elem);
10283+
(localCache,localAccList) = deriveArrayDimensions(localCache,localEnv,restList,localImpl,localAccList);
1028510284
then (localCache,localAccList);
1028610285
end matchcontinue;
1028710286
end deriveArrayDimensions;

Compiler/Prefix.mo

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,18 +463,19 @@ algorithm
463463
then
464464
(cache,Exp.REDUCTION(fcn,exp_1,id,iterexp_1));
465465

466-
case (cache,env,Exp.VALUEBLOCK(localDecls = lDecls,body = b,result = res),p)
466+
case (cache,env,Exp.VALUEBLOCK(t,localDecls = lDecls,body = b,result = res),p)
467467
local
468468
Prefix p;
469469
list<Exp.DAEElement> lDecls,lDecls2;
470470
Exp.DAEElement b,b2;
471-
Exp.Exp res,res2;
471+
Exp.Exp res,res2;
472+
Exp.Type t;
472473
equation
473474
(cache,lDecls2) = prefixDecls(cache,env,lDecls,{},p);
474475
(cache,b2) = prefixAlgorithm(cache,env,b,p);
475476
(cache,res2) = prefixExp(cache,env,res,p);
476477
then
477-
(cache,Exp.VALUEBLOCK(lDecls2,b2,res2));
478+
(cache,Exp.VALUEBLOCK(t,lDecls2,b2,res2));
478479

479480
// MetaModelica list. MetaModelica extension. KS
480481
case (cache,env,Exp.LIST(es),p)

Compiler/Static.mo

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ algorithm
422422
then
423423
(cache,e_1,prop_1,st_1); */
424424
/*--------------------------------*/
425-
425+
426426
case (cache,env,Absyn.CALL(function_ = fn,functionArgs = Absyn.FUNCTIONARGS(args = args,argNames = nargs)),impl,st,doVect)
427427
local Exp.Exp e;
428428
equation
@@ -432,7 +432,36 @@ algorithm
432432
(cache,e_1,prop_1) = cevalIfConstant(cache,e, prop, c, impl, env);
433433
Debug.fprintln("sei", "elab_exp CALL done");
434434
then
435-
(cache,e_1,prop_1,st_1);
435+
(cache,e_1,prop_1,st_1);
436+
437+
/* Array For iterator expression */
438+
case (cache,env,Absyn.CALL(function_ = Absyn.CREF_IDENT("array",{}),functionArgs = Absyn.FOR_ITER_FARG(e1,rangeList)),impl,st,doVect)
439+
local Exp.Exp e;
440+
list<Absyn.Ident> idList;
441+
Absyn.Exp e1,vb;
442+
Absyn.ForIterators rangeList;
443+
Absyn.Algorithm absynStmt,temp;
444+
list<Absyn.Ident> idList;
445+
Absyn.ComponentRef c1,c2;
446+
list<Absyn.ElementItem> declList;
447+
list<Absyn.AlgorithmItem> vb_body;
448+
equation
449+
idList = Inst.extractLoopVars(rangeList,{});
450+
451+
// Create temporary array to store the result from the for-iterator construct
452+
(cache,declList) = Inst.createForIteratorArray(cache,env,e1,idList,rangeList,impl);
453+
454+
// Create for-statements
455+
temp = Inst.createForIteratorAlgorithm(e1,rangeList,idList,Absyn.CREF_IDENT("VEC__",{}));
456+
457+
vb_body = Util.listCreate(Absyn.ALGORITHMITEM(temp,NONE()));
458+
vb = Absyn.VALUEBLOCK(declList,Absyn.VALUEBLOCKALGORITHMS(vb_body),
459+
Absyn.CREF(Absyn.CREF_IDENT("VEC__",{})));
460+
461+
(cache,e_1,prop_1,st_1) = elabExp(cache,env,vb,impl,st,doVect);
462+
then
463+
(cache,e_1,prop_1,st_1);
464+
436465
case (cache,env,Absyn.TUPLE(expressions = (e as (e1 :: rest))),impl,st,doVect) /* PR. Get the properties for each expression in the tuple.
437466
Each expression has its own constflag.
438467
!!The output from functions does just have one const flag.
@@ -571,9 +600,10 @@ algorithm
571600
b_alg_2 = Convert.fromAlgStatesToExpStates(b_alg,{});
572601
b_alg_dae = Exp.ALGORITHM(Exp.ALGORITHM2(b_alg_2));
573602

574-
(cache,res2,prop as Types.PROP(_,_),st) = elabExp(cache,env2,res,impl,st,doVect);
575-
576-
then (cache,Exp.VALUEBLOCK(dae2,b_alg_dae,res2),prop,st);
603+
(cache,res2,prop as Types.PROP(tp,_),st) = elabExp(cache,env2,res,impl,st,doVect);
604+
tp_1 = Types.elabType(tp);
605+
606+
then (cache,Exp.VALUEBLOCK(tp_1,dae2,b_alg_dae,res2),prop,st);
577607

578608
//Equations must converted into Algorithm statements
579609
case (cache,env,Absyn.VALUEBLOCK(ld,Absyn.VALUEBLOCKEQUATIONS(
@@ -634,9 +664,10 @@ algorithm
634664

635665
b_alg_dae = Exp.ALGORITHM(Exp.ALGORITHM2(b_alg_2));
636666

637-
(cache,res2,prop as Types.PROP(_,_),st) = elabExp(cache,env2,res,impl,st,doVect);
638-
639-
then (cache,Exp.VALUEBLOCK(dae2,b_alg_dae,res2),prop,st);
667+
(cache,res2,prop as Types.PROP(tp,_),st) = elabExp(cache,env2,res,impl,st,doVect);
668+
tp_1 = Types.elabType(tp);
669+
670+
then (cache,Exp.VALUEBLOCK(tp_1,dae2,b_alg_dae,res2),prop,st);
640671

641672
//-------------------------------------
642673
// Part of the MetaModelica extension. KS

0 commit comments

Comments
 (0)