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

SCM Repository

[diderot] Diff of /branches/pure-cfg/src/compiler/codegen/codegen-fn.sml
ViewVC logotype

Diff of /branches/pure-cfg/src/compiler/codegen/codegen-fn.sml

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

revision 454, Mon Oct 25 19:59:47 2010 UTC revision 455, Wed Oct 27 02:42:13 2010 UTC
# Line 2  Line 2 
2   *   *
3   * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)   * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4   * All rights reserved.   * All rights reserved.
5     *
6     * Generic support for translating LowIL code to the target representation.  We
7     * assume that the LowIL has first been run through the splitting pass to match
8     * the target's vector widths.
9   *)   *)
10    
11  functor CodeGenFn (T : TARGET) : sig  functor CodeGenFn (T : TARGET) : sig
# Line 12  Line 16 
16    
17      structure Src = LowIL      structure Src = LowIL
18      structure SrcV = LowIL.Var      structure SrcV = LowIL.Var
19        structure SrcOp = LowOps
20    
21    (* a mapping from LowIL variables to target expressions.  Variables get    (* a mapping from LowIL variables to target expressions.  Variables get
22     * removed when the expressions are used as arguments.     * removed when the expressions are used as arguments.
# Line 26  Line 31 
31    
32          val clear : tbl -> unit          val clear : tbl -> unit
33    
34          val bind : var_def_tbl -> (CFG.var * MTy.mlrisc_tree) -> MTy.T.stm list          val bind : tbl -> (SrcV.var * T.exp) -> T.stm list
35    
36          (* force all pending expressions into variables *)
37            val flush : tbl -> T.stm list
38    
       (* force all pending expressions into registers *)  
         val flushLoads : var_def_tbl -> MTy.T.stm list  
39        end = struct        end = struct
40    
41            type info = {
42                cnt : int ref,      (* count of oustanding uses (usually 1) *)
43                bind : T.exp
44              }
45    
46            type tbl = info SrcV.Tbl.hash_table
47    
48            fun newTbl () = SrcV.Tbl.mkTable (512, Fail "vtbl")
49    
50            fun getDefOf tbl x = (case SrcV.Tbl.find tbl x
51                   of NONE => ??
52                    | SOME{bind, cnt} => bind
53                  (* end case *))
54    
55            fun useDefOf tbl x = (case SrcV.Tbl.find tbl x
56                   of NONE => ??
57                    | SOME{cnt=ref n, bind} => (
58                        ignore (SrcV.Tbl.remove tbl x);
59                        bind)
60                    | SOME{cnt, bind} =>  => (
61                        cnt := !cnt - 1;
62                        bind)
63                  (* end case *))
64    
65            fun setDefOf tbl (x, exp) =
66                  SrcV.Tbl.insert tbl (x, {cnt = ref(SrcV.useCount x), bind = exp})
67    
68            fun assign tbl (x, exp) = let
69                        val lhs : T.local_var = ??
70                        in
71                          SrcV.Tbl.insert tbl
72                            (x, {cnt = SrcV.useCount x, bind = T.Expr.var lhs});
73                          [T.Stmt.assign(lhs, exp)]
74                        end
75    
76            fun bind tbl (x, exp) = (case SrcV.useCount lhs
77                   of 1 => (SrcV.Tbl.insert tbl (x, {cnt = 1, bind = exp}); [])
78                    | n => let (* bind exp to a new target variable *)
79                        val lhs : T.local_var = ??
80                        in
81                          SrcV.Tbl.insert tbl (x, {cnt = n, bind = T.Expr.var lhs});
82                          [T.Stmt.assign(lhs, exp)]
83                        end
84                  (* end case *))
85    
86        end (*  VDefTbl *)        end (*  VDefTbl *)
87    
88    (* FIXME: what about splitting code where the target width doesn't match the
89     * source width?
90     *)
91        fun doRator (vtbl, lhs, rator, args) = let
92              val args' = List.map (VDefTbl.useDefOf vtbl) args
93              val rhs' = (case rator
94    (* ??? *)
95                    (* end case *))
96              in
97                VDefTbl.bind vtbl (lhs, rhs')
98  end  end
99    
100      (* translate a LowIL assignment to a list of zero or more target statements *)
101        fun doAssign vtbl (lhs, rhs) = let
102              fun setDef rhs = (VTbl.setDefOf vtbl (lhs, rhs); [])
103              in
104                case rhs
105                 of Src.VAR x => setDef (T.Expr.var(VDefTbl.useDefOf vtbl x))
106                  | Src.LIT(Literal.Int n) => setDef (T.Expr.intLit n)
107                  | Src.LIT(Literal.Bool b) => setDef (T.Expr.boolLit b)
108                  | Src.LIT(Literal.Float f) => setDef (T.Expr.floatLit f)
109                  | Src.LIT(Literal.String s) => setDef (T.Expr.stringLit s)
110                  | Src.OP(rator, args) => doRator(vtbl, lhs, rator, args)
111                  | Src.CONS args =>
112                      VTbl.assign ctbl (lhs, T.Expr.vector (List.map (VDefTbl.useDefOf vtbl) args))
113                (* end case *)
114    end    end
115    
116        fun gen (vtbl, stm) = let
117              val doAssign = doAssign vtbl
118              fun doStmt (SrcIL.STM{kind, next, ...}) = let
119                    val stms = (case kind
120                           of SrcIL.S_SIMPLE nd => doNode nd
121                            | SrcIL.S_IF{cond, thenBranch, elseBranch} =>
122                            | SrcIL.S_LOOP{hdr, cond, body} =>
123                          (* end case *))
124                    val rest = (case next
125                           of NONE => VDefTbl.flush vtbl
126                            | SOME stm = doStmt stm
127                          (* end case *))
128                    in
129                      stms @ rest
130                    end
131              in
132              end
133    
134      end

Legend:
Removed from v.454  
changed lines
  Added in v.455

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