Home My Page Projects Code Snippets Project Openings 3D graphics for Standard ML
Summary Activity SCM

SCM Repository

[sml3d] Diff of /trunk/sml3d/src/particles/compiler/translate.sml
ViewVC logotype

Diff of /trunk/sml3d/src/particles/compiler/translate.sml

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

revision 1139, Mon May 2 00:13:29 2011 UTC revision 1140, Mon May 2 06:00:36 2011 UTC
# Line 614  Line 614 
614           (* end case *))           (* end case *))
615          end          end
616    
617          fun trExpr(expr, env, state, k : IR.var -> IR.stmt) = (case expr          fun trExpr(expr, env, state, k : IR.var -> particle_state -> IR.stmt) = (case expr
618            of P.CONSTF f => k (IR.newConst ("c", IR.C_FLOAT f))            of P.CONSTF f => k (IR.newConst ("c", IR.C_FLOAT f)) state
619    
620         | P.CONST3F v => k (IR.newConst ("c", IR.C_VEC v))         | P.CONST3F v => k (IR.newConst ("c", IR.C_VEC v)) state
621    
622         | P.VAR v => k (psvToIRVar (env, v))         | P.VAR v => k (psvToIRVar (env, v)) state
623    
624         | P.STATE_VAR sv => k (getIRVarForSV (sv, state))         | P.STATE_VAR sv => k (getIRVarForSV (sv, state)) state
625    
626         | P.GENERATE3F (dom, dist) => genVecVar("genVec", env, dom, dist, k)         | P.GENERATE3F (dom, dist) => genVecVar("genVec", env, dom, dist, fn var => k var state)
627    
628         | P.GENERATEF (dom, dist) => genFloatVar("genFlt", env, dom, dist, k)         | P.GENERATEF (dom, dist) => genFloatVar("genFlt", env, dom, dist, fn var => k var state)
629    
630         | P.ADD(e1, e2) =>         | P.ADD(e1, e2) =>
631           trExpr(e1, env, state, fn e1var =>           trExpr(e1, env, state, fn e1var => fn state' =>
632           trExpr(e2, env, state, fn e2var =>           trExpr(e2, env, state', fn e2var => fn state'' =>
633           let           let
634            val IR.V{varType=vt1, ...} = e1var            val IR.V{varType=vt1, ...} = e1var
635            val IR.V{varType=vt2, ...} = e2var            val IR.V{varType=vt2, ...} = e2var
636           in           in
637            (case (vt1, vt2)            (case (vt1, vt2)
638              of (IR.T_FLOAT, IR.T_FLOAT) => letPRIM("addVar", IR.T_FLOAT, IR.ADD, [e1var, e2var], k)              of (IR.T_FLOAT, IR.T_FLOAT) => letPRIM("addVar", IR.T_FLOAT, IR.ADD, [e1var, e2var], fn var => k var state'')
639               | (IR.T_VEC, IR.T_VEC) => letPRIM("addVar", IR.T_VEC, IR.ADD_VEC, [e1var, e2var], k)               | (IR.T_VEC, IR.T_VEC) => letPRIM("addVar", IR.T_VEC, IR.ADD_VEC, [e1var, e2var], fn var => k var state'')
640               | _ => raise Fail ("Type mismatch to ADD expression")               | _ => raise Fail ("Type mismatch to ADD expression")
641            (* end case *))            (* end case *))
642           end))           end))
643    
644         | P.SCALE (e1, e2) =>         | P.SCALE (e1, e2) =>
645           trExpr(e1, env, state, fn e1var =>           trExpr(e1, env, state, fn e1var => fn state' =>
646           trExpr(e2, env, state, fn e2var =>           trExpr(e2, env, state', fn e2var => fn state'' =>
647           let           let
648            val IR.V{varType=vt1, ...} = e1var            val IR.V{varType=vt1, ...} = e1var
649            val IR.V{varType=vt2, ...} = e2var            val IR.V{varType=vt2, ...} = e2var
650           in           in
651            (case (vt1, vt2)            (case (vt1, vt2)
652              of (IR.T_FLOAT, IR.T_VEC) => letPRIM("scaleVar", IR.T_VEC, IR.SCALE, [e1var, e2var], k)              of (IR.T_FLOAT, IR.T_VEC) => letPRIM("scaleVar", IR.T_VEC, IR.SCALE, [e1var, e2var], fn var => k var state'')
653               | (IR.T_FLOAT, IR.T_FLOAT) => letPRIM("scaleVar", IR.T_FLOAT, IR.MULT, [e1var, e2var], k)               | (IR.T_FLOAT, IR.T_FLOAT) => letPRIM("scaleVar", IR.T_FLOAT, IR.MULT, [e1var, e2var], fn var => k var state'')
654               | _ => raise Fail (String.concat["Type mismatch to SCALE expression: ", IR.ty2Str vt1, ", ", IR.ty2Str vt2])               | _ => raise Fail (String.concat["Type mismatch to SCALE expression: ", IR.ty2Str vt1, ", ", IR.ty2Str vt2])
655            (* end case *))            (* end case *))
656           end))           end))
657    
658         | P.DIV (e1, e2) =>         | P.DIV (e1, e2) =>
659           trExpr(e1, env, state, fn e1var =>           trExpr(e1, env, state, fn e1var => fn state' =>
660           trExpr(e2, env, state, fn e2var =>           trExpr(e2, env, state', fn e2var => fn state'' =>
661           let           let
662            val IR.V{varType=vt1, ...} = e1var            val IR.V{varType=vt1, ...} = e1var
663            val IR.V{varType=vt2, ...} = e2var            val IR.V{varType=vt2, ...} = e2var
664           in           in
665            (case (vt1, vt2)            (case (vt1, vt2)
666              of (IR.T_FLOAT, IR.T_FLOAT) => letPRIM("divVar", IR.T_FLOAT, IR.DIV, [e1var, e2var], k)              of (IR.T_FLOAT, IR.T_FLOAT) => letPRIM("divVar", IR.T_FLOAT, IR.DIV, [e1var, e2var], fn var => k var state'')
667               | _ => raise Fail (String.concat["Type mismatch to DIV expression: ", IR.ty2Str vt1, ", ", IR.ty2Str vt2])               | _ => raise Fail (String.concat["Type mismatch to DIV expression: ", IR.ty2Str vt1, ", ", IR.ty2Str vt2])
668            (* end case *))            (* end case *))
669           end))           end))
670    
671         | P.NEG e =>         | P.NEG e =>
672           trExpr(e, env, state, fn evar =>           trExpr(e, env, state, fn evar => fn state' =>
673           let           let
674            val IR.V{varType, ...} = evar            val IR.V{varType, ...} = evar
675           in           in
676            (case varType            (case varType
677              of IR.T_FLOAT => letPRIM("negVar", IR.T_FLOAT, IR.MULT, [evar, IR.newConst("negOne", IR.C_FLOAT ~1.0)], k)              of IR.T_FLOAT => letPRIM("negVar", IR.T_FLOAT, IR.MULT, [evar, IR.newConst("negOne", IR.C_FLOAT ~1.0)], fn var => k var state')
678               | IR.T_VEC => letPRIM("negVar", IR.T_VEC, IR.NEG_VEC, [evar], k)               | IR.T_VEC => letPRIM("negVar", IR.T_VEC, IR.NEG_VEC, [evar], fn var => k var state')
679               | _ => raise Fail ("Type mismatch to NEG expression")               | _ => raise Fail ("Type mismatch to NEG expression")
680            (* end case *))            (* end case *))
681           end)           end)
682    
683         | P.DOT (e1, e2) =>         | P.DOT (e1, e2) =>
684           trExpr(e1, env, state, fn e1var =>           trExpr(e1, env, state, fn e1var => fn state' =>
685           trExpr(e2, env, state, fn e2var =>           trExpr(e2, env, state', fn e2var => fn state'' =>
686           let           let
687            val IR.V{varType=vt1, ...} = e1var            val IR.V{varType=vt1, ...} = e1var
688            val IR.V{varType=vt2, ...} = e2var            val IR.V{varType=vt2, ...} = e2var
689           in           in
690            (case (vt1, vt2)            (case (vt1, vt2)
691              of (IR.T_VEC, IR.T_VEC) => letPRIM("dotVar", IR.T_FLOAT, IR.DOT, [e1var, e2var], k)              of (IR.T_VEC, IR.T_VEC) => letPRIM("dotVar", IR.T_FLOAT, IR.DOT, [e1var, e2var], fn var => k var state'')
692               | _ => raise Fail ("Type mismatch to DOT expression")               | _ => raise Fail ("Type mismatch to DOT expression")
693            (* end case *))            (* end case *))
694           end))           end))
695    
696         | P.CROSS (e1, e2) =>         | P.CROSS (e1, e2) =>
697           trExpr(e1, env, state, fn e1var =>           trExpr(e1, env, state, fn e1var => fn state' =>
698           trExpr(e2, env, state, fn e2var =>           trExpr(e2, env, state', fn e2var => fn state'' =>
699           let           let
700            val IR.V{varType=vt1, ...} = e1var            val IR.V{varType=vt1, ...} = e1var
701            val IR.V{varType=vt2, ...} = e2var            val IR.V{varType=vt2, ...} = e2var
702           in           in
703            (case (vt1, vt2)            (case (vt1, vt2)
704              of (IR.T_VEC, IR.T_VEC) => letPRIM("crossVar", IR.T_VEC, IR.CROSS, [e1var, e2var], k)              of (IR.T_VEC, IR.T_VEC) => letPRIM("crossVar", IR.T_VEC, IR.CROSS, [e1var, e2var], fn var => k var state'')
705               | _ => raise Fail ("Type mismatch to CROSS expression")               | _ => raise Fail ("Type mismatch to CROSS expression")
706            (* end case *))            (* end case *))
707           end))           end))
708    
709         | P.NORMALIZE e =>         | P.NORMALIZE e =>
710           trExpr(e, env, state, fn evar =>           trExpr(e, env, state, fn evar => fn state' =>
711           let           let
712            val IR.V{varType, ...} = evar            val IR.V{varType, ...} = evar
713           in           in
714            (case varType            (case varType
715              of IR.T_VEC => letPRIM("normVar", IR.T_VEC, IR.NORM, [evar], k)              of IR.T_VEC => letPRIM("normVar", IR.T_VEC, IR.NORM, [evar], fn var => k var state')
716               | _ => raise Fail ("Type mismatch to NORMALIZE expression")               | _ => raise Fail ("Type mismatch to NORMALIZE expression")
717            (* end case *))            (* end case *))
718           end)           end)
719    
720         | P.LENGTH e =>         | P.LENGTH e =>
721           trExpr(e, env, state, fn evar =>           trExpr(e, env, state, fn evar => fn state' =>
722           let           let
723            val IR.V{varType, ...} = evar            val IR.V{varType, ...} = evar
724           in           in
725            (case varType            (case varType
726              of IR.T_VEC => letPRIM("lenVar", IR.T_FLOAT, IR.LEN, [evar], k)              of IR.T_VEC => letPRIM("lenVar", IR.T_FLOAT, IR.LEN, [evar], fn var => k var state')
727               | _ => raise Fail ("Type mismatch to LENGTH expression")               | _ => raise Fail ("Type mismatch to LENGTH expression")
728            (* end case *))            (* end case *))
729           end)           end)
730    
731         (* !SPEED! We're assuming that there is an intersection here... *)         (* !SPEED! We're assuming that there is an intersection here... *)
732         | P.INTERSECT {p1, p2, d} =>         | P.INTERSECT {p1, p2, d} =>
733           trExpr(p1, env, state, fn p1var =>           trExpr(p1, env, state, fn p1var => fn state' =>
734           trExpr(p2, env, state, fn p2var =>           trExpr(p2, env, state', fn p2var => fn state'' =>
735           let           let
736            val IR.V{varType=vt1, ...} = p1var            val IR.V{varType=vt1, ...} = p1var
737            val IR.V{varType=vt2, ...} = p2var            val IR.V{varType=vt2, ...} = p2var
738           in           in
739            (case (vt1, vt2)            (case (vt1, vt2)
740              of (IR.T_VEC, IR.T_VEC) => mkIntPt(env, p1var, p2var, d, k)              of (IR.T_VEC, IR.T_VEC) => mkIntPt(env, p1var, p2var, d, fn var => k var state'')
741               | _ => raise Fail("Type mismatch to INTERSECT expression")               | _ => raise Fail("Type mismatch to INTERSECT expression")
742            (* end case *))            (* end case *))
743           end))           end))
744    
745         | P.NORMALTO (e, d) =>         | P.NORMALTO (e, d) =>
746           trExpr(e, env, state, fn evar =>           trExpr(e, env, state, fn evar => fn state' =>
747           let           let
748            val IR.V{varType, ...} = evar            val IR.V{varType, ...} = evar
749            fun cont s = k s            fun cont s = k s
750           in           in
751            (case varType            (case varType
752              of IR.T_VEC => normAtPoint("normVar", d, env, evar, state, fn var => fn state' => k var)              of IR.T_VEC => normAtPoint("normVar", d, env, evar, state', k)
753               | _ => raise Fail("Type mismatch to NORMALTO expression")               | _ => raise Fail("Type mismatch to NORMALTO expression")
754            (* end case *))            (* end case *))
755           end)           end)
# Line 758  Line 758 
758           fun findVar (IR.V{name, ...}) = name = varName           fun findVar (IR.V{name, ...}) = name = varName
759          in          in
760           (case (List.find findVar state)           (case (List.find findVar state)
761             of SOME v => k v             of SOME v => k v state
762              | NONE => raise Fail("Compiler Error: Undefined variable: " ^ varName)              | NONE => raise Fail("Compiler Error: Undefined variable: " ^ varName)
763           (* end case *))           (* end case *))
764          end          end
# Line 780  Line 780 
780          val P.GEN{exp, ...} = sv_gen          val P.GEN{exp, ...} = sv_gen
781          val IR.V{varType, ...} = var          val IR.V{varType, ...} = var
782         in         in
783          (* This is kind of a hack, but it'll get optimized out. *)          (* This is kind of a hack, but it'll get optimized out.
784          trExpr(exp, env, state, fn newVal => IR.mkPRIM(var, IR.COPY, [newVal], cont))           * Also, I think it's OK to leave the state' unused since we're
785             * creating variables here and its assumed that they're independent. *)
786            trExpr(exp, env, state, fn newVal => fn state' => IR.mkPRIM(var, IR.COPY, [newVal], cont))
787         end (* genVar *)         end (* genVar *)
788    
789       in       in
# Line 796  Line 798 
798        letPRIM("isDead", IR.T_BOOL, IR.GT, [IR.newConst("small", IR.C_FLOAT 0.1), ttl], fn isDead =>        letPRIM("isDead", IR.T_BOOL, IR.GT, [IR.newConst("small", IR.C_FLOAT 0.1), ttl], fn isDead =>
799        IR.mkIF(isDead,        IR.mkIF(isDead,
800         (* then *)         (* then *)
801         trExpr(freq, env, state, fn t1 =>         trExpr(freq, env, state, fn t1 => fn state' =>
802         letPRIM("t2", IR.T_FLOAT, IR.ITOF, [psvToIRVar (env, PSV.numDead)], fn t2 =>         letPRIM("t2", IR.T_FLOAT, IR.ITOF, [psvToIRVar (env, PSV.numDead)], fn t2 =>
803         letPRIM("prob", IR.T_FLOAT, IR.DIV, [t1, t2], fn prob =>         letPRIM("prob", IR.T_FLOAT, IR.DIV, [t1, t2], fn prob =>
804         letPRIM("r", IR.T_FLOAT, IR.RAND, [], fn r =>         letPRIM("r", IR.T_FLOAT, IR.RAND, [], fn r =>
805         letPRIM("t3", IR.T_BOOL, IR.GT, [prob, r], fn t3 =>         letPRIM("t3", IR.T_BOOL, IR.GT, [prob, r], fn t3 =>
806         IR.mkIF(t3,         IR.mkIF(t3,
807          (* then *)          (* then *)
808          newParticle (sv_gens, env, state, fn state' => retState state'),          newParticle (sv_gens, env, state', fn state'' => retState state''),
809          (* else *)          (* else *)
810          IR.DISCARD)))))),          IR.DISCARD)))))),
811         (* else *)         (* else *)
# Line 813  Line 815 
815      (* trExpr(expr, env, state, k : IR.var -> IR.stmt) *)      (* trExpr(expr, env, state, k : IR.var -> IR.stmt) *)
816      (* mkFloatWithinVar (boolVar, env, var, d : Float.float P.domain, stmt : IR.var -> IR.stmt) *)      (* mkFloatWithinVar (boolVar, env, var, d : Float.float P.domain, stmt : IR.var -> IR.stmt) *)
817      fun trPred(cond, env, state, thenk : particle_state -> IR.stmt, elsek : particle_state -> IR.stmt) = let      fun trPred(cond, env, state, thenk : particle_state -> IR.stmt, elsek : particle_state -> IR.stmt) = let
818        fun grabVar(cond, env, state, k : IR.var -> IR.stmt) = (case cond        fun grabVar(cond, env, state, k : IR.var -> particle_state -> IR.stmt) = (case cond
819          of P.WITHINF(d, expr) =>          of P.WITHINF(d, expr) =>
820              trExpr(expr, env, state, fn checkMe =>              trExpr(expr, env, state, fn checkMe => fn state' =>
821              mkFloatWithinVar("wv", env, checkMe, d, k))              mkFloatWithinVar("wv", env, checkMe, d, fn var => k var state'))
822    
823           | P.WITHIN3F(d, expr) =>           | P.WITHIN3F(d, expr) =>
824              trExpr(expr, env, state, fn checkMe =>              trExpr(expr, env, state, fn checkMe => fn state' =>
825              mkVecWithinVar("wv", env, checkMe, d, k))              mkVecWithinVar("wv", env, checkMe, d, fn var => k var state'))
826    
827           | P.DO_INTERSECT {p1, p2, d} =>           | P.DO_INTERSECT {p1, p2, d} =>
828             trExpr(p1, env, state, fn p1var =>             trExpr(p1, env, state, fn p1var => fn state' =>
829             trExpr(p2, env, state, fn p2var =>             trExpr(p2, env, state', fn p2var => fn state'' =>
830             mkIntBool(env, p1var, p2var, d, state, fn var => fn state' => k var)))             mkIntBool(env, p1var, p2var, d, state'', k)))
831    
832           | P.GTHAN (e1, e2) =>           | P.GTHAN (e1, e2) =>
833             trExpr(e1, env, state, fn e1var =>             trExpr(e1, env, state, fn e1var => fn state' =>
834             trExpr(e2, env, state, fn e2var =>             trExpr(e2, env, state', fn e2var => fn state'' =>
835             letPRIM("gtVar", IR.T_BOOL, IR.GT, [e1var, e2var], k)))             letPRIM("gtVar", IR.T_BOOL, IR.GT, [e1var, e2var], fn var => k var state'')))
836    
837           | P.AND(c1, c2) =>           | P.AND(c1, c2) =>
838             grabVar(c1, env, state, fn c1Var =>             grabVar(c1, env, state, fn c1Var => fn state' =>
839             grabVar(c2, env, state, fn c2Var =>             grabVar(c2, env, state', fn c2Var => fn state'' =>
840             letPRIM("andVar", IR.T_BOOL, IR.AND, [c1Var, c2Var], k)))             letPRIM("andVar", IR.T_BOOL, IR.AND, [c1Var, c2Var], fn var => k var state'')))
841    
842           | P.OR(c1, c2) =>           | P.OR(c1, c2) =>
843             grabVar(c1, env, state, fn c1Var =>             grabVar(c1, env, state, fn c1Var => fn state' =>
844             grabVar(c2, env, state, fn c2Var =>             grabVar(c2, env, state', fn c2Var => fn state'' =>
845             letPRIM("andVar", IR.T_BOOL, IR.OR, [c1Var, c2Var], k)))             letPRIM("andVar", IR.T_BOOL, IR.OR, [c1Var, c2Var], fn var => k var state'')))
846    
847           | P.XOR(c1, c2) =>           | P.XOR(c1, c2) =>
848             grabVar(c1, env, state, fn c1Var =>             grabVar(c1, env, state, fn c1Var => fn state' =>
849             grabVar(c2, env, state, fn c2Var =>             grabVar(c2, env, state', fn c2Var => fn state'' =>
850             mkXOR ("xorVar", c1Var, c2Var, k)))             mkXOR ("xorVar", c1Var, c2Var, fn var => k var state'')))
851    
852           | P.NOT(c) =>           | P.NOT(c) =>
853             grabVar(c, env, state, fn cvar =>             grabVar(c, env, state, fn cvar => fn state' =>
854             letPRIM("notVar", IR.T_BOOL, IR.NOT, [cvar], k))             letPRIM("notVar", IR.T_BOOL, IR.NOT, [cvar], fn var => k var state'))
855    
856          (* end case *))          (* end case *))
857       in       in
858        grabVar(cond, env, state, fn result =>        grabVar(cond, env, state, fn result => fn state' =>
859        IR.mkIF(result, thenk(state), elsek(state)))        IR.mkIF(result, thenk(state'), elsek(state')))
860       end       end
861    
862      fun compile (P.PG{      fun compile (P.PG{
# Line 935  Line 937 
937                         nv :: replaceStateVar(var, svars)                         nv :: replaceStateVar(var, svars)
938                      end                      end
939                  in                  in
940                   trExpr(expr, env, state, fn newVar =>                   trExpr(expr, env, state, fn newVar => fn state' =>
941                   letPRIM(getIRNameForSV sv, IR.psvTyToIRTy ty, IR.COPY, [newVar],                   letPRIM(getIRNameForSV sv, IR.psvTyToIRTy ty, IR.COPY, [newVar],
942                     fn thisVar => f (replaceStateVar(thisVar, state))))                     fn thisVar => f (replaceStateVar(thisVar, state'))))
943                  end                  end
944    
945                 | P.LET(P.V(varName), exp, act) =>                 | P.LET(P.V(varName), exp, act) =>
946                   trExpr(exp, env, state, fn newVar => let                   trExpr(exp, env, state, fn newVar => fn state' => let
947    
948                     val joinBlk = newBlock(env, state, f)                     val joinBlk = newBlock(env, state', f)
949    
950                     fun inOriginalState (IR.V{name=vn, ...}) = let                     fun inOriginalState (IR.V{name=vn, ...}) = let
951                       fun nameCompare (IR.V{name=vn1, ...}) = vn = vn1                       fun nameCompare (IR.V{name=vn1, ...}) = vn = vn1
952                      in                      in
953                       List.exists nameCompare state                       List.exists nameCompare state'
954                      end                      end
955    
956                     fun gotoJoinBlk state' = goto(List.filter inOriginalState state', joinBlk)                     fun gotoJoinBlk state'' = goto(List.filter inOriginalState state'', joinBlk)
957    
958                     val IR.V{varType, ...} = newVar                     val IR.V{varType, ...} = newVar
959                     val newParam = IR.newParam(varName, varType)                     val newParam = IR.newParam(varName, varType)
960                     val newState = newParam :: state                     val newState = newParam :: state'
961    
962                     val blk = newBlock(env, newState, fn state' => evalActs act state' gotoJoinBlk)                     val blk = newBlock(env, newState, fn state'' => evalActs act state'' gotoJoinBlk)
963                    in                    in
964                     goto(newVar :: state, blk)                     goto(newVar :: state', blk)
965                    end                    end
966                   )                   )
967    
# Line 998  Line 1000 
1000            }            }
1001    
1002            val _ = IR.outputPgm(TextIO.stdErr, outPgm)            val _ = IR.outputPgm(TextIO.stdErr, outPgm)
1003            val optimized = if (Checker.checkIR(outPgm)) then (printErr "Pre-optimization complete."; Optimize.optimizeIR(outPgm)) else outPgm            val optimized = if (Checker.checkIR(outPgm)) then (printErr "\nPre-optimization complete."; Optimize.optimizeIR(outPgm)) else outPgm
1004          val _ = (
1005            printErr "\n\n";
1006            IR.outputPgm(TextIO.stdErr, optimized)
1007           )
1008            in            in
1009              (* Note: it only succeeds if we can optimize, too *)              (* Note: it only succeeds if we can optimize, too *)
1010          if Checker.checkIR(optimized) then printErr "Compilation succeeded." else ();          if Checker.checkIR(optimized) then printErr "Compilation succeeded." else ();

Legend:
Removed from v.1139  
changed lines
  Added in v.1140

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