
% Apply linear subscription heuristics

% ---------------------------------------------------------------------
 
% $Id$
 
% $Log$

% ---------------------------------------------------------------------

% ``meta-syntax'' is
%
%  <expression>  ::=
%    expRef(<exp_handle>)                                            |
%    idxRef(<exp_handle>, <idx_handle>)                              |
%    varRef(<var_handle>)                                            |
%    funRef(<fun_handle>, <tag>, List(<exp_handle>))                 |
%    subscript(<exp_handle>, List(<exp_handle>))                     |
%    arrall(List(<idx_handle>), List(<exp_handle>), <exp_handle>)    |
%    forall(List(<idx_handle>), List(<exp_handle>), List(<exp_handle>),
%           List(<exp_handle>))                                      |
%    size(<literal>, <exp_handle>)                                   |
%    literal(<literal>)                                              |
%    true                                                            |
%    false                                                           |
%    any
%
%  <index_declaration> ::= index(<range>, <idx_attr>)
%
%  <tag>         ::= <predef> | user
%
%  <predef>      ::= ...

% Attribute meta-syntax...
%
%  <fun_attr> ::= syms         :: <sym_tab>               #
%                 declarations :: List(<var_handle> |
%                                      <fun_handle> |
%                                      <exp_handle>)      #
%                 statements   :: List(<exp_handle>)      #
%                 dummies      :: List(<var_handle>)      #
%                 result       :: <var_handle>            #
%                 class        :: elemental | array       #
%                 dumShps      :: List(<extern_shape>)    #
%                 resShp       :: <extern_shape>          #
%                 dumMaps      :: List(<extern_mapping>)  #
%                 resMap       :: <extern_mapping>        #
%                 dumAlnGrps   :: List(List(<literal>))   #
%                 resAlnGrp    :: List(<literal>)
%
%  <var_attr> ::= shape    :: List(<exp_handle>)         #
%                 ishape   :: List(<exp_handle>)         #
%                 mapDesc  :: <var_mapping_desc>         #
%                 imapDesc :: <var_mapping_desc>
%
%  <exp_attr> ::= shape :: List(<range>)

% Syntax of mapping descriptors
%
%
%  <exp_mapping_desc>  ::=  align(<template>,
%                                 List(<arr_idx_mapping_desc),
%                                 List(<idx_mapping_desc),
%                                 List(<embedding_desc>))
%
%  <arr_idx_mapping_desc>  ::=  <map> :: <dimension>
%
%  <template>          ::= def | <var_handle>
%
%  <idx_mapping_desc>  ::= <map> :: <idx_handle> -> <dimension>
%
%  <map>               ::= map(<exp_handle>, <exp_handle>)  |
%                          id                               |
%                          any
%
%  <embedding_desc>    ::=  emb(<exp_handle>) :: <dimension>  |
%                           rep :: <dimension>


idxMapSubsExpressionHandleList([], _).
idxMapSubsExpressionHandleList([EH | EHs], Syms) :-
    idxMapSubsExpressionHandle(EH, Syms),
    idxMapSubsExpressionHandleList(EHs, Syms).

idxMapSubsExpressionHandle(EH, Syms) :-
    symLookUp(EH, expression(E, Atr), Syms),
    idxMapSubsExpression(E, Atr, Syms).

idxMapSubsExpression(varRef(_), Atr, Syme) :-
    expAtr(Atr),
      CtxMap := Atr -> ctxMap,  LocMap := Atr -> locMap,
    idxMapProp(CtxMap, LocMap).
idxMapSubsExpression(funRef(_, _, _), Atr, Syms) :-
    expAtr(Atr),
      EHs    := Atr -> mapDaughts,
      CtxMap := Atr -> ctxMap,  LocMap := Atr -> locMap,
    idxMapSubsExpressionHandleList(EHs, Syms),
    idxMapProp(CtxMap, LocMap).
idxMapSubsExpression(subscript(_, _), Atr, Syms) :-
    expAtr(Atr),
      []     := Atr -> mapDomains,  EHs    := Atr -> mapDaughts,
      CtxMap := Atr -> ctxMap,      LocMap := Atr -> locMap,
    idxMapSubsExpressionHandleList(EHs, Syms),
    idxMapProp(CtxMap, LocMap).
idxMapSubsExpression(subscript(ArrH, SubHs), Atr, Syms) :-
    expAtr(Atr),
      [_]    := Atr -> mapDomains,  EHs    := Atr -> mapDaughts,
      CtxMap := Atr -> ctxMap,      LocMap := Atr -> locMap,
    expGetCtxMap(ArrMap, ArrH, Syms),
    idxMapSubs(LocMap, SubHs, ArrMap, Syms),
    idxMapSubsExpressionHandleList(EHs, Syms),
    idxMapProp(CtxMap, LocMap).
idxMapSubsExpression(forall(_, Shape, _, EHs), _, Syms) :-
    idxMapSubsExpressionHandleList(Shape, Syms),
    idxMapSubsExpressionHandleList(EH, Syms).
idxMapSubsExpression(arrall(_, Shape, EH), _, Syms) :-
    idxMapSubsExpressionHandleList(Shape, Syms),
    idxMapSubsExpressionHandleList(EH, Syms).
idxMapSubsExpression(idxRef(SizeH, IH), Atr, Syms) :-
    idxMapSubsExpressionHandle(SizeH, Syms).
idxMapSubsExpression(size(_, EH), Atr, Syms) :-
    expAtr(Atr),
      CtxMap := Atr -> ctxMap,      LocMap := Atr -> locMap,
    idxMapSubsExpressionHandle(SizeH, Syms),
    idxMapProp(CtxMap, LocMap).
idxMapSubsExpression(expRef(_), _, _).
idxMapSubsExpression(E, _, _) :- isLiteral(E).


% ----- Map indices of linear subscript expressions -----


idxMapSubs(Map, SubHs, ArrMap, Syms) :-
    getTemplate(TArr, Map),
    getTemplate(TArr, ArrMap),
    getIMap(IdxMap, Map),
    getAIMap(AIMap, ArrMap),
    idxMapLinSubs(IdxMap, SubHs, AIMap, Syms).
mapLinSubs(_, _, _, _).


idxMapLinSubs(_, [], [], _).
idxMapLinSubs(IdxMap, [SubH | SubHs], [AIMapping | AIMappings], Syms) :-
    idxMapLinSub(IdxMap, SubH, AIMapping, Syms),
    idxMapLinSubs(IdxMap, SubHs, AIMappings, Syms).

idxMapLinSub(IdxMap, SubH, AlnM :: D, Syms) :-
    expGetDepen([IH], [map(_, Stride, Offset, IH)], SubH, Syms),
    composeLin(M, map(Stride, Offset), AlnM),
    member(M :: IH -> D, IdxMap).
idxMapLinSub(_, _, _, _).


% ----- Propagation of index maps -----


idxMapProp(CtxMap, LocMap) :-
    getAIMap(CtxAIMap, CtxMap),
    getAIMap(LocAIMap, LocMap),
    unifyAIMap(CtxAIMap, LocAIMap),
    getIMap(CtxIdxMap, CtxMap),
    getIMap(LocIdxMap, LocMap),
    unifyIMap(CtxIdxMap, LocIdxMap).
idxMapProp(_, _).

unifyAIMap([], []).
unifyAIMap([MA :: D | AIMappingsA], [MB :: D | AIMappingsB]) :-
    unifyLin(MA, MB),
    unifyAIMap(AIMappingsA, AIMappingsB).

unifyIMap(_, []).
unifyIMap(CtxIdxMap, [MB :: IH -> D | IMappings]) :-
    member(MA :: IH -> D, CtxIdxMap),
    unifyLin(MA, MB),
    unifyIMap(CtxIdxMap, IMappings).


unifyLin(MA, MB)   :- var(MA), !, MA = MB.
unifyLin(MA, MB)   :- var(MB), !, MA = MB.
unifyLin(_ o _, _) :- !, fail.
unifyLin(_, _ o _) :- !, fail.
unifyLin(M, M).


