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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2646 - (download) (annotate)
Thu May 29 15:52:23 2014 UTC (5 years, 3 months ago) by cchiw
File size: 3808 byte(s)
opr to clang
(* 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
   

    val default=CL.Fn_NotDone
    fun q n =Int.toString n

    (*Creating Args*)
    val realTy=CL.T_Named(N.mkReal)
    val varS="s"
    val varV="v"
    val tmp= "a"



    fun mkArg 0=[]
      | mkArg n= mkArg(n-1)@ [String.concat[tmp,q n ]]

    (*creating Params*)
    (*P: ty, var*)
    fun mkParams(ty,args)=List.map (fn e=>CL.P_args(ty,e)) args
    fun mkDiffParams e=List.map (fn e1=> CL.P_args e1) e
      

    

    (*CLang Expressions *)
    (*(ty){ str1,str2..}    *)
    fun mkCast(args,ty)=let
        val body=CL.E_VarList args
        val cast=CL.E_Cast(ty, body)
        in
            cast
        end

    fun mkBrk(exps,ty)=let
        val body=CL.E_Bracket exps
        val cast=CL.E_Cast(ty, body)
        in
            cast
        end


    (*a, b:exp*)
    fun mkProd(a,b)=CL.mkBinOp(a, CL.#*, b)

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

    fun mkZero n=List.tabulate (n,fn e=> CL.E_Str(q e))


    (*CLang Cfn*)
    (*rtnType:strig, fnName:string, args:exp List blk:stm list*)
    fun createFnC(rtnType, fnName, args,blk)= let
        val stmt'=CL.S_Block blk
        in
            CL.Fn_C(rtnType,fnName,args,stmt')
        end



    (*create function names*)
     fun NameRealToVec d=N.mkVec d
     fun NameScaV d= N.scale d
     fun NamefillVec(tyN,tyO)= String.concat["fillVec",q tyO,"to",q tyN]

    (*fnName Variable gets name of TreeFunc()*)
    fun mkRealToVec d=let        
       
        val rtnType= CL.T_Named(N.vecTy d)
        val fnName=NameRealToVec d
        val args=mkArg d
        val params=mkParams(realTy, args)
         (*Return Expression *)
        val body=SOME (mkCast(args, rtnType))
        val rtn=CL.S_Return body
        in
            createFnC(rtnType, fnName, params,[rtn])
        end 

    fun mkScaV d= let
        val rtnType= CL.T_Named(N.vecTy d)
        val fnName=NameScaV d
        val params=mkDiffParams[(realTy,varS),(rtnType,varV)]

        (*Return Expression *)
        val varList=List.tabulate(d, (fn _=>CL.E_Var varS))
        val fnName'=NameRealToVec d
        val s=CL.E_Apply(fnName', varList)
        val v=CL.E_Var varV
        val body=SOME(mkProd(s,v))
        val rtn=CL.S_Return body
        in
            createFnC(rtnType, fnName, params,[rtn])
        end


   fun fillVec(tyNew,tyOrig)=let
        val rtnType= CL.T_Named(N.vecTy tyNew)
        val argType= CL.T_Named(N.vecTy tyOrig)
        val fnName=NamefillVec(tyNew,tyOrig)
        val params=mkDiffParams[(argType,varV)]
        val indexedVec=indexDiderotType(CL.E_Var varV,tyOrig-1)
        val n=tyNew-tyOrig
        val fill=mkZero n
        val body=SOME(mkBrk(indexedVec@fill,rtnType))
        val rtn=CL.S_Return body

        in
            createFnC(rtnType, fnName, params,[rtn])
        end 
        


    fun handleOpr( ILF.LD (tyNew,tyOrig,offset))=default
      | handleOpr( ILF.MKVEC (tyOrig))=default
      | handleOpr( ILF.CONS a0)= default
      | handleOpr( ILF.REALTOVEC d)=mkRealToVec d
      | handleOpr( ILF.FVEC (tyNew,tyOrig))= fillVec(tyNew,tyOrig)
      | handleOpr( ILF.SVEC  tyNew)= default
      | handleOpr( ILF.OP(Op.prodScaV d))=mkScaV d  
      | handleOpr( ILF.OP(Op.IndexTensor(a,RTy,b,c)))=default
      | handleOpr( ILF.OP(Op.addVec _))=default
      | handleOpr( ILF.OP(Op.subVec _))=default
      | handleOpr( ILF.OP(Op.prodVec _))=default
      | handleOpr( ILF.OP e)= raise Fail("OPr of Op:"^Op.toString(e))
end

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