Skip to content

Commit

Permalink
Initial work for reduction expressions, e.g. parameter Real a[5]=arra…
Browse files Browse the repository at this point in the history
…y(2.*i for i in 1:5);

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@1155 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
levsa committed Apr 22, 2004
1 parent 8c67c34 commit 0662b52
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 14 deletions.
12 changes: 6 additions & 6 deletions modeq/dump.rml
Expand Up @@ -895,7 +895,7 @@ relation print_componentitem: Absyn.ComponentItem => () =

rule Print.print_buf "COMPONENTITEM(" &
print_component c &
Print.print_buf ",..)"
Print.print_buf ",<comment>)"
------------------------------------------
print_componentitem(Absyn.COMPONENTITEM(c,optcmt))
end
Expand Down Expand Up @@ -1238,7 +1238,7 @@ relation print_algorithmitem: Absyn.AlgorithmItem => () =
-------------------
print_algorithmitem (Absyn.ALGORITHMITEM(alg,_))

rule Print.print_buf "ALGORITHMITEMANN(..)\n"
rule Print.print_buf "ALGORITHMITEMANN(<annotation>)\n"
----------------------------------------
print_algorithmitem(Absyn.ALGORITHMITEMANN(ann))
end
Expand Down Expand Up @@ -1639,7 +1639,7 @@ relation print_exp : Absyn.Exp => () =
---------------------------------------------
print_exp(Absyn.CALL(fcn, args))

rule Print.print_buf "[" &
rule Print.print_buf "ARRAY[" &
print_list_debug("print_exp",es, print_exp, ",") &
Print.print_buf "]"
---------
Expand All @@ -1653,17 +1653,17 @@ relation print_exp : Absyn.Exp => () =
---------
print_exp Absyn.TUPLE(es)

rule Print.print_buf "[" &
rule Print.print_buf "MATRIX[" &
print_list_debug("print_exp",es, print_row, ";") &
Print.print_buf "]"
---------
print_exp Absyn.MATRIX(es)

rule Print.print_buf "(" & print_exp start & Print.print_buf ":" & print_exp stop & Print.print_buf ")"
rule Print.print_buf "RANGE(" & print_exp start & Print.print_buf ":" & print_exp stop & Print.print_buf ")"
-------------------------------------------------------------
print_exp Absyn.RANGE(start,NONE,stop)

rule Print.print_buf "(" & print_exp start & Print.print_buf ":" & print_exp step &
rule Print.print_buf "RANGE(" & print_exp start & Print.print_buf ":" & print_exp step &
Print.print_buf ":" & print_exp stop & Print.print_buf ")"
-------------------------------------------------------------
print_exp Absyn.RANGE(start,SOME(step),stop)
Expand Down
18 changes: 17 additions & 1 deletion modeq/exp.rml
Expand Up @@ -73,6 +73,7 @@ module Exp:
| ASUB of Exp * int
| SIZE of Exp * Exp option
| CODE of Absyn.Code * Type
| REDUCTION of Absyn.Path * Exp (*expr*) * Ident * Exp (*range*)
| END
(** The `Exp' datatype closely corresponds to the `Absyn.Exp'
** datatype, but is used for statically analyzed expressions. It
Expand Down Expand Up @@ -785,7 +786,12 @@ relation print_exp2 : (Exp,int) => () =
Print.print_buf str
-------------
print_exp2 (e as SIZE(cr,NONE),_)


rule print_exp_str e => str &
Print.print_buf str
-----------------------------
print_exp2 (e as REDUCTION(fcn,exp,i,iterexp),_)

rule Print.print_buf "#UNKNOWN EXPRESSION# ----eee "
----------------------------------
print_exp2 (_,_)
Expand Down Expand Up @@ -1342,6 +1348,16 @@ relation print_exp2_str : (Exp,int) => string =
------------------------
print_exp2_str (SIZE(cr,NONE),_) => str


rule Absyn.path_string fcn => fs &
print_exp_str exp => expstr &
print_exp_str iterexp => iterstr &
Util.string_append_list(["<reduction>",
fs,"(",expstr," for ",
id," in ",iterstr,")"]) => str
-------------------------------------------------------
print_exp2_str (REDUCTION(fcn,exp,id,iterexp),_) => str

axiom print_exp2_str (_,_) => "#UNKNOWN EXPRESSION# ----eee "

end
Expand Down
14 changes: 10 additions & 4 deletions modeq/inst.rml
Expand Up @@ -104,7 +104,7 @@ module Inst:
=> ( Env.Env, DAE.Element list)
relation package_prefix : (Env.Env, Absyn.Path) => Absyn.Path
relation add_nomod: SCode.Element list => (SCode.Element*Types.Mod) list

end


Expand Down Expand Up @@ -1478,6 +1478,7 @@ relation inst_element : (Env, Mod, Prefix, Connect.Sets, ClassInf.State,
check_prot(prot, mm', vn) &
(* Debug.fprintln ("insttr", "Protection checked") &*)

Debug.fcall("myeqmod",Mod.print_mod, mod') & Debug.fprintln ("myeqmod", "\n") &
Mod.mod_equation mod' => eq &

(* Debug.fprintln ("insttr", "mod equation done, from mod:") &
Expand Down Expand Up @@ -3514,7 +3515,7 @@ relation inst_equation_common : (Env,Mod, Prefix, Connect.Sets, ClassInf.State,
=> (e',Types.PROP((Types.T_ARRAY(Types.DIM(_),
(Types.T_INTEGER(_),_)),_),
true),_) &
(* FIXEM: Check bounds *)
(* FIXME: Check bounds *)
Ceval.ceval (env,e',false,NONE,NONE) => (v,_) &
(* Not possilbe for 'when'.unroll(env,mod,pre,csets,ci_state,i,v,el) => (dae, csets') & *)
inst_list(env,mod,pre,csets,ci_state,inst_e_equation, el)
Expand All @@ -3535,7 +3536,7 @@ relation inst_equation_common : (Env,Mod, Prefix, Connect.Sets, ClassInf.State,
=> (e',Types.PROP((Types.T_ARRAY(Types.DIM(_),
(Types.T_INTEGER(_),_)),_),
true),_) &
(* FIXEM: Check bounds *)
(* FIXME: Check bounds *)
Ceval.ceval (env,e',false,NONE,NONE) => (v,_) &
(* Not possilbe for 'when'.unroll(env,mod,pre,csets,ci_state,i,v,el) => (dae, csets') & *)
inst_list(env,mod,pre,csets,ci_state,inst_e_initialequation, el)
Expand All @@ -3557,6 +3558,8 @@ relation inst_equation_common : (Env,Mod, Prefix, Connect.Sets, ClassInf.State,
**
**)

(** FIXME: Why lookup after add_for_loop_scope ? **)

rule (*Debug.fprintln ("insttr", "inst_equation_common_eqfor_1") &*)
Static.elab_exp (env,e,false,NONE)
=> (e',Types.PROP((Types.T_ARRAY(Types.DIM(_),
Expand All @@ -3571,7 +3574,7 @@ relation inst_equation_common : (Env,Mod, Prefix, Connect.Sets, ClassInf.State,
(Types.T_INTEGER(_),_), Types.UNBOUND) &
(* Debug.fprintln ("insti", "loop variable looked up") &*)

(* FIXEM: Check bounds *)
(* FIXME: Check bounds *)
Ceval.ceval (env,e',false,NONE,NONE) => (v,_) &
(* Debug.fprintln ("insti", "for expression evaluated") &*)
unroll(env',mod,pre,csets,ci_state,i,v,el,initial) => (dae, csets') &
Expand All @@ -3582,6 +3585,8 @@ relation inst_equation_common : (Env,Mod, Prefix, Connect.Sets, ClassInf.State,
inst_equation_common(env,mod,pre,csets,ci_state,SCode.EQ_FOR(i,e,el),initial)
=> (dae, env,csets', ci_state')

(** FIXME: Old code? no add_for_loop_scope **)

rule (*Debug.fprintln ("insttr", "inst_equation_common_eqfor_2") &*)
Lookup.lookup_var(env, Exp.CREF_IDENT(i,[]))
=> (Types.ATTR(false, SCode.RW, SCode.VAR, _),
Expand All @@ -3597,6 +3602,7 @@ relation inst_equation_common : (Env,Mod, Prefix, Connect.Sets, ClassInf.State,
inst_equation_common(env,mod,pre,csets,ci_state,SCode.EQ_FOR(i,e,el),initial)
=> fail

(** FIXME: Old code? loop variable need not be in scope **)
rule not Lookup.lookup_var(env, Exp.CREF_IDENT(i,[]))
=> (Types.ATTR(false, SCode.RW, SCode.VAR, _),
(Types.T_INTEGER(_),_), Types.UNBOUND) &
Expand Down
5 changes: 3 additions & 2 deletions modeq/mod.rml
Expand Up @@ -942,9 +942,10 @@ relation print_eqmod : Types.EqMod option => () =

axiom print_eqmod NONE

rule Print.print_buf " =(typed) " & Exp.print_exp e
rule Print.print_buf " =(typed) " & Exp.print_exp e &
Dump.print_select (const, "const", "non-const")
-----------------------------
print_eqmod SOME(Types.TYPED(e,prop))
print_eqmod SOME(Types.TYPED(e,Types.PROP(ty,const)))

rule Print.print_buf " =(untyped) " & Dump.print_exp e
-----------------------------
Expand Down
7 changes: 6 additions & 1 deletion modeq/prefix.rml
Expand Up @@ -210,7 +210,7 @@ end
** expression.
**)

relation prefix_exp: (Env.Env,Exp.Exp,Prefix) => Exp.Exp =
relation prefix_exp : (Env.Env,Exp.Exp,Prefix) => Exp.Exp =

axiom prefix_exp(_,e as Exp.ICONST(_),_) => e
axiom prefix_exp(_,e as Exp.RCONST(_),_) => e
Expand Down Expand Up @@ -308,6 +308,11 @@ relation prefix_exp: (Env.Env,Exp.Exp,Prefix) => Exp.Exp =
-----------------------
prefix_exp(env,Exp.CAST(Exp.REAL,e), p) => Exp.CAST(Exp.REAL,e')

rule prefix_exp(env,exp,p) => exp' &
prefix_exp(env,iterexp,p) => iterexp'
-------------------------------------
prefix_exp(env,Exp.REDUCTION(fcn,exp,id,iterexp), p) => Exp.REDUCTION(fcn,exp',id,iterexp')

rule Print.print_buf "- prefix_exp failed\n" &
Print.print_buf " expression: " & Exp.print_exp e & Print.print_buf "\n"
-----------------------------------------------------
Expand Down
61 changes: 61 additions & 0 deletions modeq/staticexp.rml
Expand Up @@ -260,6 +260,7 @@ relation elab_exp : (Env.Env, Absyn.Exp, bool, Interactive.InteractiveSymbolTabl
elab_exp (env,Absyn.CALL(fn,Absyn.FUNCTIONARGS(args,nargs)),impl,st)
=> (e,prop,st')


(*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 All @@ -281,6 +282,14 @@ relation elab_exp : (Env.Env, Absyn.Exp, bool, Interactive.InteractiveSymbolTabl

(** Array-related expressions *)

(** Elab reduction expressions, including array() constructor **)

rule elab_call_reduction(env,fn,exp,id,iterexp,impl,st) => (e,prop,st')
-------------------------------------------------------------------
elab_exp (env,Absyn.CALL(fn,Absyn.FOR_ITER_FARG(exp,id,iterexp)),impl,st)
=> (e,prop,st')


rule (*Debug.fprintln("setr", "elab_exp_range1") &*)
elab_exp (env, start,impl,st) => (start', Types.PROP(start_t, c_start),st') &
elab_exp (env, stop,impl,st') => (stop', Types.PROP(stop_t, c_stop),st'') &
Expand Down Expand Up @@ -345,6 +354,56 @@ relation elab_exp : (Env.Env, Absyn.Exp, bool, Interactive.InteractiveSymbolTabl
*)

end


relation add_for_loop_scope_const : (Env.Env, Ident, Types.Type) => Env.Env =

rule Env.open_scope (env,false,SOME("$for loop scope$")) => env' &
Env.extend_frame_v(env',
Types.VAR(i,
Types.ATTR(false,
SCode.RW,
SCode.VAR,
Absyn.BIDIR),
false,
typ,
Types.VALBOUND(Values.INTEGER(1))),NONE,false
) => env''
--------------------------------------------------------------------------
add_for_loop_scope_const(env,i,typ) => env''

end


(** relation: elab_call_reduction
*
* This relation elaborates reduction expressions, that look like function
* calls. For example an array constructor.
*)
relation elab_call_reduction : (Env.Env, Absyn.ComponentRef, Absyn.Exp,
Ident, Absyn.Exp, bool,
Interactive.InteractiveSymbolTable option)
=> (Exp.Exp, Types.Properties,Interactive.InteractiveSymbolTable option) =


rule elab_exp (env,iterexp,impl,st)
=> (iterexp',
Types.PROP((Types.T_ARRAY(arraydim as Types.DIM(_), iterty),_),
iterconst),
_) &
add_for_loop_scope_const(env,iter,iterty) => env' &
(** const so that expr is elaborated to const **)
elab_exp (env', exp, impl, st) => (exp', Types.PROP(expty, expconst), st) &
bool_and (expconst, iterconst) => const &
let prop = Types.PROP((Types.T_ARRAY(arraydim, expty),NONE), const) &
Absyn.cref_to_path fn => fn'
---------------------------------------------------
elab_call_reduction (env,fn,exp,iter,iterexp,impl,st)
=> (Exp.REDUCTION (fn',exp',iter,iterexp'), prop, st)

end


(* HL bort
relation elab_exp_dim : (Env.Env, Absyn.Exp, bool, Interactive.InteractiveSymbolTable option,int)
=> (Exp.Exp, Types.Properties,Interactive.InteractiveSymbolTable option) =
Expand Down Expand Up @@ -544,6 +603,8 @@ relation elab_graphics_exp : (Env.Env, Absyn.Exp) => (Exp.Exp, Types.Properties)
elab_graphics_exp (env, Absyn.RANGE(start, SOME(step), stop))
=> (Exp.RANGE(rt,start'',SOME(step''),stop''), Types.PROP(t, const))


(** FIXME: what is a below? Why true if is array? **)
rule (*Debug.fprintln("setr", "elab_graphics_exp_array") &*)
elab_graphics_array (env, es) => (es', Types.PROP(t, const)) & list_length es' => l&
(* Ceval.ceval_list(env,es',false,NONE) => vl &
Expand Down

0 comments on commit 0662b52

Please sign in to comment.