Skip to content

Commit

Permalink
Redesigned vectorization of variables. Now has extra argument to elab…
Browse files Browse the repository at this point in the history
…Exp to control this. See instEquation on EQ_EQUALS. (affects array equations)

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2584 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Peter Aronsson committed Oct 26, 2006
1 parent 34ae2b0 commit 4905100
Show file tree
Hide file tree
Showing 8 changed files with 480 additions and 365 deletions.
4 changes: 2 additions & 2 deletions Compiler/Algorithm.mo
Expand Up @@ -231,9 +231,9 @@ algorithm
equation
Print.printErrorBuf("- Algorithm.make_assignment failed\n");
Print.printErrorBuf(" ");
Exp.printExp(lhs);
Print.printErrorBuf(Exp.printExpStr(lhs));
Print.printErrorBuf(" := ");
Exp.printExp(rhs);
Print.printErrorBuf(Exp.printExpStr(rhs));
Print.printErrorBuf("\n");
then
fail();
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Ceval.mo
Expand Up @@ -1230,7 +1230,7 @@ algorithm
case (cache,env,Absyn.CALL(function_ = Absyn.CREF_IDENT(name = "Eval",subscripts = {}),functionArgs = Absyn.FUNCTIONARGS(args = {e},argNames = {})),impl,st,msg)
local Exp.Exp e_1;
equation
(cache,e_1,_,_) = Static.elabExp(cache,env, e, impl, st);
(cache,e_1,_,_) = Static.elabExp(cache,env, e, impl, st,true);
(cache,Values.CODE(Absyn.C_EXPRESSION(exp)),_) = ceval(cache,env, e_1, impl, st, NONE, msg);
then
(cache,exp);
Expand Down
17 changes: 16 additions & 1 deletion Compiler/Exp.mo
Expand Up @@ -1485,6 +1485,21 @@ algorithm
res = simplifyVectorBinary(e1, SUB(tp), e2);
then
res;

// v1 - -v2 => v1 + v2
case(e1,SUB_ARR(ty=tp),e2)
equation
(UNARY(_,e2)) = simplify(e2);
e1 = simplify(e1);
then BINARY(e1,ADD_ARR(tp),e2);

// v1 + -v2 => v1 - v2
case(e1,ADD_ARR(ty=tp),e2)
equation
(UNARY(_,e2)) = simplify(e2);
e1 = simplify(e1);
then BINARY(e1,SUB_ARR(tp),e2);

case (s1,MUL_SCALAR_ARRAY(ty = tp),a1)
equation
tp = typeof(s1);
Expand Down Expand Up @@ -6976,7 +6991,7 @@ algorithm
end matchcontinue;
end solve2;

protected function getTermsContainingX "function getTermsContainingX
public function getTermsContainingX "function getTermsContainingX
Retrieves all terms of an expression containng a variable, given
as second argument (in the form of an Exp)
Expand Down
201 changes: 124 additions & 77 deletions Compiler/Inst.mo

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions Compiler/Interactive.mo
Expand Up @@ -417,21 +417,21 @@ algorithm
(st as SYMBOLTABLE(ast = p)))
equation
env = buildEnvFromSymboltable(st);
(_,econd,prop,SOME(st_1)) = Static.elabExp(Env.emptyCache,env, cond, true, SOME(st));
(_,econd,prop,SOME(st_1)) = Static.elabExp(Env.emptyCache,env, cond, true, SOME(st),true);
(_,Values.BOOL(true),SOME(st_2)) = Ceval.ceval(Env.emptyCache,env, econd, true, SOME(st_1), NONE, Ceval.MSG());
then
("",st_2);
case (Absyn.ALGORITHMITEM(algorithm_ = Absyn.ALG_NORETCALL(functionCall = Absyn.CREF_IDENT(name = "assert"),functionArgs = Absyn.FUNCTIONARGS(args = {cond,msg}))),(st as SYMBOLTABLE(ast = p)))
equation
env = buildEnvFromSymboltable(st);
(_,msg_1,prop,SOME(st_1)) = Static.elabExp(Env.emptyCache,env, msg, true, SOME(st));
(_,msg_1,prop,SOME(st_1)) = Static.elabExp(Env.emptyCache,env, msg, true, SOME(st),true);
(_,Values.STRING(str),SOME(st_2)) = Ceval.ceval(Env.emptyCache,env, msg_1, true, SOME(st_1), NONE, Ceval.MSG());
then
(str,st_2);
case (Absyn.ALGORITHMITEM(algorithm_ = Absyn.ALG_ASSIGN(assignComponent = Absyn.CREF_IDENT(name = ident,subscripts = {}),value = exp)),(st as SYMBOLTABLE(ast = p)))
equation
env = buildEnvFromSymboltable(st);
(_,sexp,Types.PROP(t,_),SOME(st_1)) = Static.elabExp(Env.emptyCache,env, exp, true, SOME(st));
(_,sexp,Types.PROP(t,_),SOME(st_1)) = Static.elabExp(Env.emptyCache,env, exp, true, SOME(st),true);
(_,value,SOME(st_2)) = Ceval.ceval(Env.emptyCache,env, sexp, true, SOME(st_1), NONE, Ceval.MSG());
str = Values.valString(value);
newst = addVarToSymboltable(ident, value, t, st_2);
Expand All @@ -440,7 +440,7 @@ algorithm
case (Absyn.ALGORITHMITEM(algorithm_ = Absyn.ALG_TUPLE_ASSIGN(tuple_ = Absyn.TUPLE(expressions = crefexps),value = rexp)),(st as SYMBOLTABLE(ast = p))) /* Since expressions cannot be tuples an empty string is returned */
equation
env = buildEnvFromSymboltable(st);
(_,srexp,rprop,SOME(st_1)) = Static.elabExp(Env.emptyCache,env, rexp, true, SOME(st));
(_,srexp,rprop,SOME(st_1)) = Static.elabExp(Env.emptyCache,env, rexp, true, SOME(st),true);
((Types.T_TUPLE(types),_)) = Types.getPropType(rprop);
idents = Util.listMap(crefexps, getIdentFromTupleCrefexp);
(_,Values.TUPLE(values),SOME(st_2)) = Ceval.ceval(Env.emptyCache,env, srexp, true, SOME(st_1), NONE, Ceval.MSG());
Expand Down Expand Up @@ -749,7 +749,7 @@ algorithm
case (exp,(st as SYMBOLTABLE(ast = p)))
equation
env = buildEnvFromSymboltable(st);
(_,sexp,prop,SOME(st_1)) = Static.elabExp(Env.emptyCache,env, exp, true, SOME(st));
(_,sexp,prop,SOME(st_1)) = Static.elabExp(Env.emptyCache,env, exp, true, SOME(st),true);
(_,value,SOME(st_2)) = Ceval.ceval(Env.emptyCache,env, sexp, true, SOME(st_1), NONE, Ceval.MSG());
then
(value,st_2);
Expand All @@ -770,7 +770,7 @@ protected function stringRepresOfExpr "function: stringRepresOfExpr
InteractiveSymbolTable st_1;
algorithm
env := buildEnvFromSymboltable(st);
(_,sexp,prop,SOME(st_1)) := Static.elabExp(Env.emptyCache,env, exp, true, SOME(st));
(_,sexp,prop,SOME(st_1)) := Static.elabExp(Env.emptyCache,env, exp, true, SOME(st),true);
estr := Exp.printExpStr(sexp);
end stringRepresOfExpr;

Expand Down
4 changes: 2 additions & 2 deletions Compiler/Mod.mo
Expand Up @@ -119,7 +119,7 @@ algorithm
case (cache,env,pre,(m as SCode.MOD(final_ = final_,each_ = each_,subModLst = subs,absynExpOption = SOME(e))),impl)
equation
(cache,subs_1) = elabSubmods(cache,env, pre, subs, impl);
(cache,e_1,prop,_) = Static.elabExp(cache,env, e, impl, NONE);
(cache,e_1,prop,_) = Static.elabExp(cache,env, e, impl, NONE,true);
(cache,e_val) = elabModValue(cache,env, e_1);
(cache,e_2) = Prefix.prefixExp(cache,env, e_1, pre) "Bug: will cause elaboration of parameters without value to fail,
But this can be ok, since a modifier is present, giving it a value
Expand Down Expand Up @@ -331,7 +331,7 @@ algorithm
case (cache,env,pre,(m as Types.MOD(final_ = f,each_ = each_,subModLst = subs,eqModOption = SOME(Types.UNTYPED(e)))),impl)
equation
(cache,subs_1) = updateSubmods(cache,env, pre, subs, impl);
(cache,e_1,prop,_) = Static.elabExp(cache,env, e, impl, NONE);
(cache,e_1,prop,_) = Static.elabExp(cache,env, e, impl, NONE,true);
(cache,e_val) = elabModValue(cache,env, e_1);
(cache,e_2) = Prefix.prefixExp(cache,env, e_1, pre);
Debug.fprint("updmod", "Updated mod: ");
Expand Down
31 changes: 29 additions & 2 deletions Compiler/SimCodegen.mo
Expand Up @@ -4093,6 +4093,7 @@ algorithm
// Since we use origname we need to replace '.' with '$point' manually.
cr_1_str = stringAppend("$",Util.modelicaStringToCStr(Exp.printComponentRefStr(cr_1)));
cr_1 = Exp.CREF_IDENT(cr_1_str,{});
(e1,e2) = solveTrivialArrayEquation(cr_1,e1,e2);
(s1,cg_id_1,f1) = generateSingleArrayEqnCode2(cr_1, cr_1, e1, e2, cg_id);
then
(s1,cg_id_1,f1);
Expand All @@ -4106,6 +4107,32 @@ algorithm
end matchcontinue;
end generateSingleArrayEqnCode;

protected function solveTrivialArrayEquation "Solves some trivial array equations, like v+v2=foo(...), w.r.t. v is v=foo(...)-v2"
input Exp.ComponentRef v;
input Exp.Exp e1;
input Exp.Exp e2;
output Exp.Exp outE1;
output Exp.Exp outE2;
algorithm
(outE1,outE2) := matchcontinue(v,e1,e2)
local Exp.Exp e12,e22,vTerm,res,rhs;
list<Exp.Exp> terms;
Exp.Type tp;
// Solve simple linear equations.
case(v,e1,e2) equation
tp = Exp.typeof(e1);
res = Exp.simplify(Exp.BINARY(e1,Exp.SUB_ARR(tp),e2));
//print("simplified to :");print(Exp.printExpStr(res));print("\n");
(vTerm as Exp.CREF(_,_),rhs) = Exp.getTermsContainingX(res,Exp.CREF(v,Exp.OTHER()));
//print("solved array equation to :");print(Exp.printExpStr(e1));print("=");
//print(Exp.printExpStr(e2));print("\n");
then (vTerm,rhs);

// not succeded to solve, return unsolved equation., catched later.
case(v,e1,e2) then (e1,e2);
end matchcontinue;
end solveTrivialArrayEquation;

protected function generateSingleArrayEqnCode2 "function generateSingleArrayEqnCode2
author: PA
Expand Down Expand Up @@ -4147,7 +4174,7 @@ algorithm
func_1 = Codegen.cAddStatements(cfunc, {stmt});
then
(func_1,cg_id_1,{});
case (cr,eltcr,e1,e2,cg_id) /* array of crefs, {v{1},v{2},...v{n}} */
case (cr,eltcr,e1,e2,cg_id) /* e2 is array of crefs, {v{1},v{2},...v{n}} */
equation
cr2 = getVectorizedCrefFromExp(e2);
s1 = Exp.printComponentRefStr(eltcr);
Expand All @@ -4156,7 +4183,7 @@ algorithm
func_1 = Codegen.cAddStatements(cfunc, {stmt});
then
(func_1,cg_id_1,{});
case (cr,eltcr,e1,e2,cg_id) /* array of crefs, {v{1},v{2},...v{n}} */
case (cr,eltcr,e1,e2,cg_id) /* e1 is array of crefs, {v{1},v{2},...v{n}} */
equation
cr2 = getVectorizedCrefFromExp(e1);
s1 = Exp.printComponentRefStr(eltcr);
Expand Down

0 comments on commit 4905100

Please sign in to comment.