Skip to content

Commit

Permalink
Made some improvements and fixed some bugs in the valueblock and for …
Browse files Browse the repository at this point in the history
…iterator constructs.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2856 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Kristian Stavåker committed Jul 12, 2007
1 parent 0662428 commit e76c170
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 20 deletions.
75 changes: 72 additions & 3 deletions Compiler/Codegen.mo
Expand Up @@ -3758,7 +3758,7 @@ algorithm
var_1 = Util.stringAppendList({"((modelica_real)",var,")"});
then
(cfn,var_1,tnr_1);
case (Exp.VALUEBLOCK(localDecls = ld,body = b,
case (Exp.VALUEBLOCK(ty,localDecls = ld,body = b,
result = res),tnr,context)
local
list<Exp.DAEElement> ld;
Expand All @@ -3778,9 +3778,14 @@ algorithm

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

cfn1_2 = cMergeFns({cfn,cfn1,cfn1_2});
//-----
(cfn1_2,tnr4,var) = addValueblockRetVar(ty,cfn1_2,tnr3,var,context);
//-----

cfn1_2 = cMergeFns({cfn,cfn1,cfn1_2});

cfn1_2 = cAddBlockAroundStatements(cfn1_2);
then (cfn1_2,var,tnr2);
then (cfn1_2,var,tnr4);

case (Exp.ASUB(exp = _),_,_)
equation
Expand Down Expand Up @@ -3852,6 +3857,70 @@ algorithm

end cAddBlockAroundStatements;

protected function addValueblockRetVar "function: addValueblockRetVar
author: KS
Used by generateExpression - Valueblock. Adds a return variable (and return
assignment) to the valueblock code.
"
input Exp.Type inType;
input CFunction inFunc;
input Integer inTnr;
input String inResVar;
input Context context;
output CFunction outFunc;
output Integer outTnr;
output String outVar;
algorithm
(outFunc,outTnr,outVar) :=
matchcontinue (inType,inFunc,inTnr,inResVar,context)
case (Exp.T_ARRAY(t,SOME(arrayDim) :: {}),localFunc,localTnr,localResVar,con)
local
String type_string,tdecl,tvar,localResVar,memStr,stmt,stmt2,tempStr;
Exp.Type t;
CFunction localFunc,cfn;
Integer localTnr,arrayDim,tnr2;
Boolean sim;
Context con;
equation // ONLY 1 DIMENSIONAL ARRAYS SUPPORTED AS OF NOW
// Create temp array var
type_string = expTypeStr(t,true);
(tdecl,tvar,tnr2) = generateTempDecl(type_string, localTnr);
cfn = cAddVariables(localFunc, {tdecl});

// Allocate temp array var
tempStr = intString(arrayDim);
stmt = Util.stringAppendList({"alloc_",type_string,"(&",tvar,",1,",tempStr,");"});
cfn = cAddInits(cfn,{stmt});

// Create the result var assignment
sim = isSimulationContext(con);
memStr = Util.if_(sim,"_mem","");
stmt2 = Util.stringAppendList({"copy_",type_string,"_data",memStr,"(&",localResVar,", &",tvar,");"});
cfn = cAddStatements(cfn, {stmt2});
then (cfn,tnr2,tvar);
case (Exp.T_ARRAY(_,SOME(_) :: _),_,_,_,_)
local
equation
Debug.fprintln("failtrace", "# Codegen: Only 1 dim return arrays from Valueblock implemented");
then fail();
case (localType,localFunc,localTnr,localResVar,_)
local
String type_string,tdecl,tvar,localResVar;
Exp.Type localType;
CFunction localFunc,cfn;
Integer localTnr,tnr2;
String stmt;
equation
type_string = expTypeStr(localType,false);
(tdecl,tvar,tnr2) = generateTempDecl(type_string, localTnr);
cfn = cAddVariables(localFunc, {tdecl});

stmt = Util.stringAppendList({tvar," = ",localResVar,";"});
cfn = cAddStatements(cfn,{stmt});
then (cfn,tnr2,tvar);
end matchcontinue;
end addValueblockRetVar;

protected function cMoveDeclsAndInitsToStatements "function: cMoveStatementsToInits
Moves all statements of the body to initialization statements.
Expand Down
5 changes: 3 additions & 2 deletions Compiler/Exp.mo
Expand Up @@ -227,9 +227,10 @@ uniontype Exp "Expressions
record END "array index to last element, e.g. a{end}:=1;" end END;

record VALUEBLOCK "Valueblock expression"
list<DAEElement> localDecls;
Type ty;
list<DAEElement> localDecls;
DAEElement body;
Exp result;
Exp result;
end VALUEBLOCK;

/* Part of MetaModelica extension. KS */
Expand Down
7 changes: 3 additions & 4 deletions Compiler/Inst.mo
Expand Up @@ -8202,7 +8202,6 @@ algorithm
Absyn.Algorithm absynStmt;
list<Absyn.Ident> idList;
Absyn.ComponentRef c;
Absyn.Ident id;
equation
// rangeList = {(id,e2)};
idList = extractLoopVars(rangeList,{});
Expand All @@ -8222,7 +8221,6 @@ algorithm
Absyn.ForIterators rangeList;
Absyn.Algorithm absynStmt,temp;
list<Absyn.Ident> idList;
Absyn.Ident id;
Absyn.ComponentRef c1,c2;
list<Absyn.ElementItem> declList;
list<Absyn.AlgorithmItem> vb_body;
Expand All @@ -8243,7 +8241,7 @@ algorithm

(cache,stmt) = instStatement(cache,env,pre,absynStmt,impl);
then
(cache,stmt);
(cache,stmt);

/* v := expr; */
case (cache,env,pre,Absyn.ALG_ASSIGN(assignComponent = Absyn.CREF(cr),value = e),impl)
Expand Down Expand Up @@ -10270,7 +10268,7 @@ algorithm
list<Absyn.Subscript> localAccList;
Env.Env localEnv;
Env.Cache localCache;
case (localCache,localEnv,{},_,localAccList) then (localCache,localAccList);
case (localCache,_,{},_,localAccList) then (localCache,localAccList);
case (localCache,localEnv,(_,SOME(e)) :: restList,localImpl,localAccList)
local
Absyn.Exp e;
Expand All @@ -10282,6 +10280,7 @@ algorithm
(localCache,_,Types.PROP((Types.T_ARRAY(Types.DIM(SOME(i)),_),NONE()),_),_) = Static.elabExp(localCache,localEnv,e,localImpl,NONE(),false);
elem = {Absyn.SUBSCRIPT(Absyn.INTEGER(i))};
localAccList = listAppend(localAccList,elem);
(localCache,localAccList) = deriveArrayDimensions(localCache,localEnv,restList,localImpl,localAccList);
then (localCache,localAccList);
end matchcontinue;
end deriveArrayDimensions;
Expand Down
7 changes: 4 additions & 3 deletions Compiler/Prefix.mo
Expand Up @@ -463,18 +463,19 @@ algorithm
then
(cache,Exp.REDUCTION(fcn,exp_1,id,iterexp_1));

case (cache,env,Exp.VALUEBLOCK(localDecls = lDecls,body = b,result = res),p)
case (cache,env,Exp.VALUEBLOCK(t,localDecls = lDecls,body = b,result = res),p)
local
Prefix p;
list<Exp.DAEElement> lDecls,lDecls2;
Exp.DAEElement b,b2;
Exp.Exp res,res2;
Exp.Exp res,res2;
Exp.Type t;
equation
(cache,lDecls2) = prefixDecls(cache,env,lDecls,{},p);
(cache,b2) = prefixAlgorithm(cache,env,b,p);
(cache,res2) = prefixExp(cache,env,res,p);
then
(cache,Exp.VALUEBLOCK(lDecls2,b2,res2));
(cache,Exp.VALUEBLOCK(t,lDecls2,b2,res2));

// MetaModelica list. MetaModelica extension. KS
case (cache,env,Exp.LIST(es),p)
Expand Down
47 changes: 39 additions & 8 deletions Compiler/Static.mo
Expand Up @@ -422,7 +422,7 @@ algorithm
then
(cache,e_1,prop_1,st_1); */
/*--------------------------------*/

case (cache,env,Absyn.CALL(function_ = fn,functionArgs = Absyn.FUNCTIONARGS(args = args,argNames = nargs)),impl,st,doVect)
local Exp.Exp e;
equation
Expand All @@ -432,7 +432,36 @@ algorithm
(cache,e_1,prop_1) = cevalIfConstant(cache,e, prop, c, impl, env);
Debug.fprintln("sei", "elab_exp CALL done");
then
(cache,e_1,prop_1,st_1);
(cache,e_1,prop_1,st_1);

/* Array For iterator expression */
case (cache,env,Absyn.CALL(function_ = Absyn.CREF_IDENT("array",{}),functionArgs = Absyn.FOR_ITER_FARG(e1,rangeList)),impl,st,doVect)
local Exp.Exp e;
list<Absyn.Ident> idList;
Absyn.Exp e1,vb;
Absyn.ForIterators rangeList;
Absyn.Algorithm absynStmt,temp;
list<Absyn.Ident> idList;
Absyn.ComponentRef c1,c2;
list<Absyn.ElementItem> declList;
list<Absyn.AlgorithmItem> vb_body;
equation
idList = Inst.extractLoopVars(rangeList,{});

// Create temporary array to store the result from the for-iterator construct
(cache,declList) = Inst.createForIteratorArray(cache,env,e1,idList,rangeList,impl);

// Create for-statements
temp = Inst.createForIteratorAlgorithm(e1,rangeList,idList,Absyn.CREF_IDENT("VEC__",{}));

vb_body = Util.listCreate(Absyn.ALGORITHMITEM(temp,NONE()));
vb = Absyn.VALUEBLOCK(declList,Absyn.VALUEBLOCKALGORITHMS(vb_body),
Absyn.CREF(Absyn.CREF_IDENT("VEC__",{})));

(cache,e_1,prop_1,st_1) = elabExp(cache,env,vb,impl,st,doVect);
then
(cache,e_1,prop_1,st_1);

case (cache,env,Absyn.TUPLE(expressions = (e as (e1 :: rest))),impl,st,doVect) /* PR. Get the properties for each expression in the tuple.
Each expression has its own constflag.
!!The output from functions does just have one const flag.
Expand Down Expand Up @@ -571,9 +600,10 @@ algorithm
b_alg_2 = Convert.fromAlgStatesToExpStates(b_alg,{});
b_alg_dae = Exp.ALGORITHM(Exp.ALGORITHM2(b_alg_2));

(cache,res2,prop as Types.PROP(_,_),st) = elabExp(cache,env2,res,impl,st,doVect);

then (cache,Exp.VALUEBLOCK(dae2,b_alg_dae,res2),prop,st);
(cache,res2,prop as Types.PROP(tp,_),st) = elabExp(cache,env2,res,impl,st,doVect);
tp_1 = Types.elabType(tp);

then (cache,Exp.VALUEBLOCK(tp_1,dae2,b_alg_dae,res2),prop,st);

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

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

(cache,res2,prop as Types.PROP(_,_),st) = elabExp(cache,env2,res,impl,st,doVect);

then (cache,Exp.VALUEBLOCK(dae2,b_alg_dae,res2),prop,st);
(cache,res2,prop as Types.PROP(tp,_),st) = elabExp(cache,env2,res,impl,st,doVect);
tp_1 = Types.elabType(tp);

then (cache,Exp.VALUEBLOCK(tp_1,dae2,b_alg_dae,res2),prop,st);

//-------------------------------------
// Part of the MetaModelica extension. KS
Expand Down

0 comments on commit e76c170

Please sign in to comment.