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

SCM Repository

[diderot] Diff of /branches/vis15/src/compiler/cxx-util/tree-to-cxx.sml
ViewVC logotype

Diff of /branches/vis15/src/compiler/cxx-util/tree-to-cxx.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3917, Sat May 28 16:41:39 2016 UTC revision 3924, Fri Jun 3 17:30:27 2016 UTC
# Line 15  Line 15 
15    
16      val trBlock : CodeGenEnv.t * TreeIR.block -> CLang.stm      val trBlock : CodeGenEnv.t * TreeIR.block -> CLang.stm
17    
18        val trWithLocals : CodeGenEnv.t * TreeVar.t list * (CodeGenEnv.t -> CLang.stm list) -> CLang.stm
19    
20        val trStms : CodeGenEnv.t * TreeIR.stm list -> CLang.stm list
21    
22      val trExp : CodeGenEnv.t * TreeIR.exp -> CLang.exp      val trExp : CodeGenEnv.t * TreeIR.exp -> CLang.exp
23    
24    (* translate an expression to a variable form; return the variable (as an expression)    (* translate an expression to a variable form; return the variable (as an expression)
# Line 37  Line 41 
41      structure Ty = TreeTypes      structure Ty = TreeTypes
42      structure V = TreeVar      structure V = TreeVar
43      structure Env = CodeGenEnv      structure Env = CodeGenEnv
44        structure RN = CxxNames
45    
46      fun trType (env, ty) = (case ty      fun trType (env, ty) = (case ty
47             of Ty.BoolTy => CL.boolTy             of Ty.BoolTy => CL.boolTy
# Line 61  Line 66 
66      fun rvalueVar (env, x) = CL.mkVar(Env.lookup(env, x))      fun rvalueVar (env, x) = CL.mkVar(Env.lookup(env, x))
67    
68    (* translate a global variable that occurs in an l-value context *)    (* translate a global variable that occurs in an l-value context *)
69      fun lvalueGlobalVar (env, x) = CL.mkIndirect(CL.mkVar(Env.global env), TreeGlobalVar.name x)      fun lvalueGlobalVar (env, x) = CL.mkIndirect(CL.mkVar(Env.global env), TreeGlobalVar.qname x)
70    (* translate a global variable that occurs in an r-value context *)    (* translate a global variable that occurs in an r-value context *)
71      val rvalueGlobalVar = lvalueGlobalVar      val rvalueGlobalVar = lvalueGlobalVar
72    
73    (* translate a strand state variable that occurs in an l-value context *)    (* translate a strand state variable that occurs in an l-value context *)
74      fun lvalueStateVar (env, x) = CL.mkIndirect(CL.mkVar(Env.selfOut env), TreeStateVar.name x)      fun lvalueStateVar (env, x) = CL.mkIndirect(CL.mkVar(Env.selfOut env), TreeStateVar.qname x)
75    (* translate a strand state variable that occurs in an r-value context *)    (* translate a strand state variable that occurs in an r-value context *)
76      fun rvalueStateVar (env, x) = CL.mkIndirect(CL.mkVar(Env.selfIn env), TreeStateVar.name x)      fun rvalueStateVar (env, x) = CL.mkIndirect(CL.mkVar(Env.selfIn env), TreeStateVar.qname x)
77    
78    (* generate new variables *)    (* generate new variables *)
79      local      local
# Line 133  Line 138 
138              | (Op.VScale d, [a, b]) => CL.mkApply("vscale", [a, b])              | (Op.VScale d, [a, b]) => CL.mkApply("vscale", [a, b])
139              | (Op.VMul d, [a, b]) => CL.mkBinOp(a, CL.#*, b)              | (Op.VMul d, [a, b]) => CL.mkBinOp(a, CL.#*, b)
140              | (Op.VNeg d, [a]) => CL.mkUnOp(CL.%-, a)              | (Op.VNeg d, [a]) => CL.mkUnOp(CL.%-, a)
141              | (Op.VSum d, [a]) => CL.mkApply("vsum", [a])              | (Op.VSum(w, _), [a]) => CL.mkApply(RN.vsum w, [a])
142              | (Op.VIndex(w, p, i), [a]) => CL.mkSubscript(a, intExp i)              | (Op.VIndex(w, p, i), [a]) => CL.mkSubscript(a, intExp i)
143              | (Op.VClamp d, [a, b, c]) => CL.mkApply("vclamp", [a, b, c])              | (Op.VClamp(w, _), [a, b, c]) => CL.mkApply("vclamp", [a, b, c])
144              | (Op.VMapClamp d, [a, b, c]) => CL.mkApply("vclamp", [a, b, c])              | (Op.VMapClamp(w, _), [a, b, c]) => CL.mkApply("vclamp", [a, b, c])
145              | (Op.VLerp d, [a, b, c]) => CL.mkApply("vlerp", [a, b, c])              | (Op.VLerp(w, _), [a, b, c]) => CL.mkApply("vlerp", [a, b, c])
146              | (Op.VCeiling d, [a]) => CL.mkApply("vceiling", [a])              | (Op.VCeiling d, [a]) => CL.mkApply("vceiling", [a])
147              | (Op.VFloor d, [a]) => CL.mkApply("vfloor", [a])              | (Op.VFloor d, [a]) => CL.mkApply("vfloor", [a])
148              | (Op.VRound d, [a]) => CL.mkApply("vround", [a])              | (Op.VRound d, [a]) => CL.mkApply("vround", [a])
# Line 210  Line 215 
215              | IR.E_Lit(Literal.Real f) => CL.mkFlt(f, Env.realTy env)              | IR.E_Lit(Literal.Real f) => CL.mkFlt(f, Env.realTy env)
216              | IR.E_Lit(Literal.String s) => CL.mkStr s              | IR.E_Lit(Literal.String s) => CL.mkStr s
217              | IR.E_Op(rator, args) => trOp (env, rator, trExps(env, args))              | IR.E_Op(rator, args) => trOp (env, rator, trExps(env, args))
218              | IR.E_Vec(w, pw, args) => let              | IR.E_Vec(w, pw, args) => CL.mkApply(RN.vcons w, trExps (env, args))
                 val args = trExps (env, args)  
                 val args = if (w < pw) then args @ List.tabulate(pw-w, fn _ => zero) else args  
                 in  
                   CL.mkVec(CL.T_Named("vec" ^ Int.toString pw), args)  
                 end  
219              | IR.E_Cons(args, Ty.TensorTy shape) => raise Fail "unexpected E_Cons"              | IR.E_Cons(args, Ty.TensorTy shape) => raise Fail "unexpected E_Cons"
220              | IR.E_Seq(args, ty) => raise Fail "unexpected E_Seq"              | IR.E_Seq(args, ty) => raise Fail "unexpected E_Seq"
221              | IR.E_Pack(layout, args) => raise Fail "unexpected E_Pack"              | IR.E_Pack(layout, args) => raise Fail "unexpected E_Pack"
222  (* FIXME: check if e is aligned and use "vload_aligned" in that case *)  (* FIXME: check if e is aligned and use "vload_aligned" in that case *)
223              | IR.E_VLoad(layout, e, i) =>              | IR.E_VLoad(layout, e, i) =>
224                  CL.mkTemplateApply("vload",                  CL.mkApply(RN.vload(Ty.nthWidth(layout, i)),
                   [trType(env, Ty.nthVec(layout, i))],  
225                    [CL.mkBinOp(trExp(env, e), CL.#+, intExp(Ty.offsetOf(layout, i)))])                    [CL.mkBinOp(trExp(env, e), CL.#+, intExp(Ty.offsetOf(layout, i)))])
226              | _ => raise Fail "trExp"              | _ => raise Fail "trExp"
227            (* end case *))            (* end case *))
# Line 270  Line 269 
269            (* end case *))            (* end case *))
270        | trMultiAssign (env, lhs, rhs) = raise Fail "bogus multi-assignment"        | trMultiAssign (env, lhs, rhs) = raise Fail "bogus multi-assignment"
271    
272        fun trPrintStm (outS, tys, args) = let
273              fun mkExp (lhs, [], []) = CL.mkBinOp(lhs, CL.#<<, CL.mkVar "std::end")
274                | mkExp (lhs, ty::tys, e::es) = let
275                  (* if necessary, wrap the argument so that the correct "<<" instance is used *)
276                    val e = (case ty
277                           of Ty.TensorTy shape => CL.mkApply(RN.tensorStruct shape, [e])
278                            | _ => e
279                          (* end case *))
280                    in
281                      mkExp (CL.mkBinOp(lhs, CL.#<<, e), tys, es)
282                    end
283                | mkExp _ = raise Fail "trPrintStm: arity mismatch"
284              in
285                CL.mkExpStm (mkExp (outS, tys, args))
286              end
287    
288      fun trStms (env, stms : TreeIR.stm list) = let      fun trStms (env, stms : TreeIR.stm list) = let
289            fun trStm (stm, (env, stms : CL.stm list)) = (case stm            fun trStm (stm, (env, stms : CL.stm list)) = (case stm
290                   of IR.S_Comment text => (env, CL.mkComment text :: stms)                   of IR.S_Comment text => (env, CL.mkComment text :: stms)
# Line 298  Line 313 
313                        in                        in
314                          (env, stm :: stms)                          (env, stm :: stms)
315                        end                        end
316                    | IR.S_Foreach(x, IR.E_Op(Op.Range, [lo, hi]), blk) => let                    | IR.S_For(x, lo, hi, blk) => let
317                        val x' = V.name x                        val x' = V.name x
318                        val env' = Env.insert (env, x, x')                        val env' = Env.insert (env, x, x')
319                        val (hi', hiInit) = if CodeGenUtil.isSimple hi                        val (hi', hiInit) = if CodeGenUtil.isSimple hi
# Line 318  Line 333 
333                        end                        end
334                    | IR.S_Foreach(x, e, blk) => raise Fail "Foreach"                    | IR.S_Foreach(x, e, blk) => raise Fail "Foreach"
335                    | IR.S_New(strand, args) => raise Fail "New"                    | IR.S_New(strand, args) => raise Fail "New"
336                    | IR.S_Save(x, exp) => (env, trAssign (env, lvalueStateVar(env, x), exp))                    | IR.S_Save(x, exp) => (env, trAssign (env, lvalueStateVar(env, x), exp) @ stms)
337                    | IR.S_LoadNrrd(lhs, ty, nrrd) => let                    | IR.S_LoadNrrd(lhs, ty, nrrd) => let
338                        val stm = (case ty                        val stm = (case ty
339                               of APITypes.SeqTy(ty, NONE) =>                               of APITypes.SeqTy(ty, NONE) =>
# Line 339  Line 354 
354                    | IR.S_Exit => (env, stms)                    | IR.S_Exit => (env, stms)
355                    | IR.S_Print(tys, args) => let                    | IR.S_Print(tys, args) => let
356                        val args = List.map (fn e => trExp(env, e)) args                        val args = List.map (fn e => trExp(env, e)) args
357                        val stm = GenPrint.genPrintStm (                        val stm = trPrintStm (
358                              CL.mkIndirect(CL.mkVar "wrld", "_output"),                              CL.mkIndirect(CL.mkVar "wrld", "_output"),
359                              tys, args)                              tys, args)
360                        in                        in
# Line 365  Line 380 
380              CL.mkBlock (dcls @ trStms (env, body))              CL.mkBlock (dcls @ trStms (env, body))
381            end            end
382    
383        and trWithLocals (env, locals, trBody) = let
384              fun trLocal (x, (env, dcls)) = let
385                    val x' = V.name x
386                    val dcl = CL.mkDecl(trType(env, V.ty x), x', NONE)
387                    in
388                      (Env.insert(env, x, x'), dcl :: dcls)
389                    end
390              val (env, dcls) = List.foldl trLocal (env, []) locals
391              in
392                CL.mkBlock (dcls @ trBody env)
393              end
394    
395      fun errorMsgAdd msg =      fun errorMsgAdd msg =
396            CL.mkCall("biffMsgAdd", [CL.mkIndirect(CL.mkVar "wrld", "_errors"), msg])            CL.mkCall("biffMsgAdd", [CL.mkIndirect(CL.mkVar "wrld", "_errors"), msg])
397    

Legend:
Removed from v.3917  
changed lines
  Added in v.3924

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