Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/charisee/src/compiler/c-target/opr-to-clang.sml
ViewVC logotype

View of /branches/charisee/src/compiler/c-target/opr-to-clang.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2827 - (download) (annotate)
Tue Nov 11 00:18:38 2014 UTC (4 years, 9 months ago) by cchiw
File size: 10249 byte(s)
changed types for imgAddr and imgLd
(* WQE Tree-IL Opr expression to Clang *)

structure OprToClang= struct

    structure CL = CLang
    structure Op = TreeOps
    structure Ty = TreeILTypes
    structure IL=TreeIL
    structure ILF=TreeFunc
    structure N=CNames
   
    fun q n =Int.toString n
     val default=CL.D_NotDone

    (*-------------- Types ---------------*)
    val realTy=CL.T_Named(N.mkReal)
    val star=CL.T_Ptr realTy
    fun vecTy d= CL.T_Named(N.LocalTy [d])
    val intTy = CL.intTy
    fun unionTy d  = CL.T_Named(N.unionTy d)
    val voidTy=CL.voidTy
    fun mkRealArr n=  CL.mkRealArr(N.mkReal,n)

    (*-------------- var:strings ---------------*)

    val varR="r"   (*reals*)
    val varV="v"  (*vector*)
    val varV1="v1"  (*vector*)
    val varV2="v2"  (*vector*)
    val varI="i"  (*int*)
    val varA="a" (*float*)
    val varA2="b" (*float*)
    val varU="u" (*unionTy*)
    val tmp="b" (*unused*)

    fun mkInt i =CL.mkInt(IntInf.fromInt i)
    (*mkVars: int -> var list *)
    fun mkVar 0=[]
      | mkVar n= mkVar(n-1)@ [String.concat[tmp,q n ]]
 
 
    fun flat [] = []
      | flat (l::ls) = l @ flat ls;

    (*-------------- E_VAR:exp ---------------*)
    fun varToVars e=List.map (fn e1=>CL.E_Var e1) e
    fun varToVar e =CL.E_Var e

    (*-------------- Params ---------------*) 
    (*Params created from vars*)
    val ParamA=([], star,varA) (*float ptr * a *)
    val ParamA2=([], star,varA2) (*float ptr * a *)
    val ParamI=([],intTy, varI)   (*Int i*)
    fun ParamV n =([],vecTy n, varV) (*vector v*)
    fun ParamVs n =[([],vecTy n, varV1),([],vecTy n, varV2)] (*vector v*)
    val ParamR= ([],realTy,varR)  (*Real R *)
 
 
    (* mkParams:creates Params
    * (attr list, ty, var)-> Param List
    *)
    fun mkParam e= CL.PARAM e
    fun mkParams e=List.map (fn e1=> mkParam e1) e


    (* mkParamsSameType:Create params with the same type
     *    ty:CLangty, args: vars -> Param list 
     *)
    fun mkParamsSameType(ty,args)=List.map (fn e=>CL.PARAM([],ty,e)) args


 
    (*-------------- Exp functions ---------------*)
 
    fun mkRtn e=CL.S_Return(SOME e)
    val  voidRtn =CL.S_Return NONE
    (*mkCastStruct:Create a cast expression
     *ClangTy, exp list -> exp
     *)
    fun mkCastStruct(ty,body)=CL.E_Cast(ty, (CL.E_Struct body))
    fun mkCastVar(ty,body)=CL.E_Cast(ty, body)


    (*ty:vectTy n  u = (ty) a*)
    fun mkExtVar(n, u,VarA)=let
        val uty=unionTy n
       
        val e1=CL.E_TyVar(uty,u)
        val exp=mkCastVar(uty, VarA)
        val e2=CL.E_Ext exp
        val C= CL.S_Eq (e1, e2)
        val C4=  CL.S_Exp(CL.E_AssignOp  (e1, CL.$=,e2))
        in
            (C, CL.E_Var (u^".r"))
        end
 

    (* mkProd:create a Product Operation
      exp *exp ->  exp
    *)
    fun mkProd(a,b)=CL.mkBinOp(a, CL.#*, b)
    fun mkAdd(a,b)= CL.mkBinOp(a, CL.#+, b)
    fun mkAddM [a]=a
    | mkAddM (a::b::es)= mkAddM (CL.mkBinOp(a, CL.#+, b)::es)


    (*Should I use Subscript and E_Int?*)
    fun indexDiderotType(U,0)=[]
      | indexDiderotType(U,n)=indexDiderotType(U,n-1)@[CL.E_Subscript(U,mkInt (n-1))]
 
    fun indexDiderotTypeWithOffset(U,0,I)= []
      | indexDiderotTypeWithOffset(U,n,I)=
        indexDiderotTypeWithOffset(U,n-1,I)@
        [CL.E_Subscript(U,mkAdd(mkInt(n-1),I))]
 


    (*mkZero: mk List of 0 expressions
     * int -> exp
     *)
    fun mkZero n=let
        val z=mkInt 0
        in
            List.tabulate (n,fn _=> z)
        end
 
 
    fun setEq([],[])=[]
      | setEq (l::lhs, r::rhs)=[CL.S_Eq (l ,r)]@ setEq(lhs,rhs)
      | setEq _ =[]

    (*-------------- Dec functions from Ops ---------------*)

    (* createDec(): create a CLang Decl
     * Ty,string, exp list, exp list -> Declaration
    *)
    fun createDec(rtnType, fnName, params,blk)=   CL.D_Func([],rtnType,fnName,params,CL.S_Block blk)

 
 fun mkScaV d= let
    val rtnType= vecTy d
    val fnName=N.NameScaV d
    val params=mkParams[ParamR,([],rtnType,varV)]
    (*Return Expression *)
    val varList=List.tabulate(d, (fn _=>CL.E_Var varR))
    val fnCall=N.NameConsVec d
    val s=CL.E_Apply(fnCall, varList)
    val VarV=varToVar varV
    val rtn=mkRtn(mkProd(s,VarV))
    in
        createDec(rtnType, fnName, params,[rtn])
    end

 
    (*SumVec functions *)
    (*Vec to Real*)
    fun mkSumV(nSize,oSize)= let
        val rtnType= realTy
        val fnName=N.NameSumV oSize
        val params=mkParams[ParamV nSize]
        val VarV=varToVar varV
        val (e,VarUR)=mkExtVar(nSize, varU,VarV)

        (*Return Expression *)
        val indexedVec=indexDiderotType(VarUR,oSize)
        val rtn=mkRtn(mkAddM indexedVec)
        in
            createDec(rtnType, fnName, params,[e,rtn])
        end
 

    (*floor function *vec -> vec *)
    fun mkFloorV d= let
        val fnName=N.NameFloorV d
        val rtnType= vecTy d
        val params=mkParams[ParamV d]
        val VarV=varToVar varV
        val (e,VarUR)=mkExtVar(d, varU,VarV)
 
        (*Return Expression *)
        val indexedVec=indexDiderotType(VarUR,d) 
        val body'= mkCastStruct(rtnType,indexedVec)
        val rtn= mkRtn(CL.E_Ext(body'))
 
        in
            createDec(rtnType, fnName, params,[e,rtn])
        end
 
 
    fun mkDotV d= let
        val rtnType= realTy
        val fnName=N.NameDotV d
        val params=mkParams(ParamVs d)
         val VarV1=varToVar varV1
         val VarV2=varToVar varV2
        val prod=CL.E_Grp(CL.mkBinOp(VarV1, CL.#*, VarV2))
 
        val (sum,VarUR)=mkExtVar(d, varU,prod)
 
        (*Return Expression *)
        val indexedVec=indexDiderotType(VarUR,d)
        val rtn=mkRtn(mkAddM indexedVec)
        in
            createDec(rtnType, fnName, params,[sum,rtn])
        end
 
        (*-------------- Dec functions from CONS ---------------*)
    fun realToArr n=let
        val rtnType= voidTy
        val fnName=N.NameConsArray n
        val VarA= CL.E_Var varA
        val vars=mkVar n
        val rhs=varToVars vars
        val params=[mkParam ParamA]@ mkParamsSameType(realTy, vars)
        val lhs=indexDiderotType(VarA, n)
        val stmts=setEq(lhs,rhs)
        in
            createDec(rtnType, fnName, params,stmts@[voidRtn])
        end 
 
    fun realToVec d=let
        val rtnType= vecTy d
        val fnName=N.NameConsVec d
        val vars=mkVar d
        val params=mkParamsSameType(realTy, vars)
 
        (*Return Expression *)
        val VARS=varToVars vars
        val body'= mkCastStruct(rtnType,VARS)
        val rtn= mkRtn(CL.E_Ext(body'))
    in
        createDec(rtnType, fnName, params,[rtn])
    end
 
 
        (*-------------- Dec functions from LDVec ---------------*)
 
    (*Does Load when not aligned*)
    (* val (e,VarUR)=mkExtVar(tyOrig, varU,VarA)*)
     fun ldVec(tyNew,tyOrig,rtnType,VarA,VarI) =let
        fun fillVec var=let
            val indexedVec=indexDiderotTypeWithOffset(var,tyOrig,VarI)
            val fill=mkZero(tyNew-tyOrig)
            in
                indexedVec@fill
            end
        fun sliceVec var= indexDiderotTypeWithOffset(var,tyNew,VarI)
        val pieces= (if (tyNew>tyOrig)
            then fillVec VarA
            else sliceVec VarA
            (*end case*))
    in
        [mkRtn (mkCastStruct(rtnType,pieces))]
    end
  
    (*General Load*)
    fun generalldVec(A,tyNew,tyOrig) =let
        val VarA= CL.E_Var varA
        val VarI=CL.E_Var varI
        val rtnType= vecTy tyNew
        val fnName=N.NameLdArr(A,tyNew,tyOrig)
        val stmts=(case A
            of false =>ldVec(tyNew,tyOrig,rtnType,VarA,VarI)
           | true => [mkRtn (CL.E_Ref(vecTy tyNew,CL.E_Subscript(VarA,VarI)))]
            (*end case*))
        val params=mkParams[ParamA,ParamI]
    in
        createDec(rtnType, fnName, params,stmts)
    end
 

        (*-------------- Dec functions from Store ---------------*)
(* Aligned store vec body
 * (vec2f_t )&(A[4])=v;
 *   storeVec Aligned
 *)
  fun storeAligned(tyR, ty,VarA,VarI,VarV)= let
        val e=   CL.E_Subscript(VarA,VarI)
        val lhs= CL.E_Ref(ty, e)
    in
       [CL.S_Eq (lhs ,VarV)]
    end


 (*not aligned store Vec body *)
 fun store(tyN,tyI,VarA,VarI,VarV)= let
    val (stmt, VarUR)= mkExtVar(tyN, varU,VarV)
    val rhs= indexDiderotType(VarUR,tyI)
    val lhs=indexDiderotTypeWithOffset(VarA, tyI, VarI)
    val e=setEq(lhs,rhs)
    in
        [stmt]@e
    end
 
 
 (*General store function *)
 fun generalmkVec(aligned,tyOrig,tyN)= let
    val rtnType = voidTy
    val fnName = N.NameMkVec(aligned,tyN,tyOrig)
    val VarA = CL.E_Var varA
    val VarI = CL.E_Var varI
    val VarV = CL.E_Var varV
    val ty=vecTy tyN
    val params = mkParams [ParamA,ParamI,([],ty, varV)]
 
 
    val stmts = (case (tyN>tyOrig,aligned)
        of (false,true) =>   storeAligned(tyN, ty, VarA,VarI,VarV)
        |  (false, _ )  => store(tyN,tyN,VarA,VarI,VarV)
        |  (true, _ )  => store(tyN,tyOrig,VarA,VarI,VarV)
        (*end case*))
    in
        createDec(rtnType, fnName, params,stmts@[ voidRtn])
    end 
         (*-------------- Dec functions from Store ---------------*)
    fun mkCopy d= let
        val fnName=N.NameCopyTensor [d]
        val rtnType= voidTy
        val params=mkParams[ParamA,ParamA2,ParamI]
        val VarA = CL.E_Var varA
        val VarA2 = CL.E_Var varA2
        val VarI = CL.E_Var varI
        val rhs= indexDiderotType(VarA2,d)
        val lhs=indexDiderotTypeWithOffset(VarA, d,VarI)

        val e=setEq(lhs,rhs)
        in
            createDec(rtnType, fnName, params,e)
        end

 

    (* handleOPr: transform Tree-IL Opr to CLang Declarations*)
    fun handleOpr(fnc,getPieces)=(case fnc
      of ILF.LDArr (A,tyR,tyO)=>generalldVec(A,tyR,tyO)
      | ILF.STORE (A,tyO,tyNOp) => generalmkVec(A,tyO,tyNOp)
      | ILF.CONSV n=> realToVec n
      | ILF.CONS n=> realToArr n
      | ILF.COPY n=> mkCopy n
      | ILF.OP(Op.prodScaV d)=> mkScaV d
      | ILF.OP(Op.sumVec(nSize,oSize)) => mkSumV(nSize,oSize)
      | ( ILF.OP(Op.dotVec d))=> mkDotV d
      | ( ILF.OP(Op.Floor d))=> mkFloorV d
      | ( ILF.OP(Op.IndexTensor(a,b,c)))=> default
      | ( ILF.OP(Op.addVec _))=> default
      | ( ILF.OP(Op.subVec _))=> default
      | ( ILF.OP(Op.prodVec _))=> default
      | ( ILF.OP e)=> raise Fail("OPR of Op:"^Op.toString(e))
    (*end case*))
        
    



end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0