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

SCM Repository

[diderot] Diff of /trunk/src/compiler/IL/expr-fn.sml
ViewVC logotype

Diff of /trunk/src/compiler/IL/expr-fn.sml

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

revision 1639, Wed Nov 16 01:48:07 2011 UTC revision 1640, Wed Nov 16 02:19:51 2011 UTC
# Line 14  Line 14 
14      structure Op : OPERATORS where type rator = IL.Op.rator      structure Op : OPERATORS where type rator = IL.Op.rator
15    
16      datatype expr_nd      datatype expr_nd
17        = VAR of IL.var        = STATE of IL.state_var
18          | VAR of IL.var
19        | LIT of Literal.literal        | LIT of Literal.literal
20        | OP of Op.rator * expr list        | OP of Op.rator * expr list
21          | MULTIOP of int * Op.rator * expr list     (* n'th result of operator in multi-assignment *)
22        | APPLY of ILBasis.name * expr list        | APPLY of ILBasis.name * expr list
23        | CONS of IL.Ty.ty * expr list        | CONS of IL.Ty.ty * expr list
24        | PHI of expr list        | PHI of expr list
# Line 24  Line 26 
26      withtype expr = expr_nd HashCons.obj      withtype expr = expr_nd HashCons.obj
27    
28      val same : expr * expr -> bool      val same : expr * expr -> bool
29        val toString : expr -> string
30    
31    (* hash-cons construction of expressions *)    (* hash-cons construction of expressions *)
32      type tbl      type tbl
33    
34      val new : unit -> tbl      val new : unit -> tbl
35    
36        val mkSTATE : tbl -> IL.state_var -> expr
37      val mkVAR   : tbl -> IL.var -> expr      val mkVAR   : tbl -> IL.var -> expr
38      val mkLIT   : tbl -> Literal.literal -> expr      val mkLIT   : tbl -> Literal.literal -> expr
39      val mkOP    : tbl -> Op.rator * expr list -> expr      val mkOP    : tbl -> Op.rator * expr list -> expr
40        val mkMULTIOP : tbl -> int * Op.rator * expr list -> expr
41      val mkAPPLY : tbl -> ILBasis.name * expr list -> expr      val mkAPPLY : tbl -> ILBasis.name * expr list -> expr
42      val mkCONS  : tbl -> IL.Ty.ty * expr list -> expr      val mkCONS  : tbl -> IL.Ty.ty * expr list -> expr
43      val mkPHI   : tbl -> expr list -> expr      val mkPHI   : tbl -> expr list -> expr
# Line 52  Line 57 
57      structure HC = HashCons      structure HC = HashCons
58    
59      datatype expr_nd      datatype expr_nd
60        = VAR of IL.var        = STATE of IL.state_var
61          | VAR of IL.var
62        | LIT of Literal.literal        | LIT of Literal.literal
63        | OP of Op.rator * expr list        | OP of Op.rator * expr list
64          | MULTIOP of int * Op.rator * expr list     (* n'th result of operator *)
65        | APPLY of ILBasis.name * expr list        | APPLY of ILBasis.name * expr list
66        | CONS of IL.Ty.ty * expr list        | CONS of IL.Ty.ty * expr list
67        | PHI of expr list        | PHI of expr list
# Line 64  Line 71 
71      val same : expr * expr -> bool = HC.same      val same : expr * expr -> bool = HC.same
72      val same' = ListPair.allEq same      val same' = ListPair.allEq same
73    
74      fun sameNd (VAR x, VAR y) = IL.Var.same(x, y)      fun sameNd (STATE x, STATE y) = IL.StateVar.same(x, y)
75          | sameNd (VAR x, VAR y) = IL.Var.same(x, y)
76        | sameNd (LIT a, LIT b) = Literal.same(a, b)        | sameNd (LIT a, LIT b) = Literal.same(a, b)
77        | sameNd (OP(op1, args1), OP(op2, args2)) =        | sameNd (OP(op1, args1), OP(op2, args2)) =
78            Op.same(op1, op2) andalso same'(args1, args2)            Op.same(op1, op2) andalso same'(args1, args2)
79          | sameNd (MULTIOP(i1, op1, args1), MULTIOP(i2, op2, args2)) =
80              (i1 = i2) andalso Op.same(op1, op2) andalso same'(args1, args2)
81        | sameNd (APPLY(f1, args1), APPLY(f2, args2)) =        | sameNd (APPLY(f1, args1), APPLY(f2, args2)) =
82            ILBasis.same(f1, f2) andalso same'(args1, args2)            ILBasis.same(f1, f2) andalso same'(args1, args2)
83        | sameNd (CONS(_, args1), CONS(_, args2)) = same'(args1, args2)        | sameNd (CONS(_, args1), CONS(_, args2)) = same'(args1, args2)
84        | sameNd (PHI args1, PHI args2) = same'(args1, args2)        | sameNd (PHI args1, PHI args2) = same'(args1, args2)
85        | sameNd _ = false        | sameNd _ = false
86    
87        fun toString exp = let
88              fun toS (e : expr, l) = (case #nd e
89                     of STATE x => IL.StateVar.toString x :: l
90                      | VAR x => IL.Var.toString x :: l
91                      | LIT lit => Literal.toString lit :: l
92                      | OP(rator, args) => Op.toString rator :: "(" :: argsToS (args, ")" :: l)
93                      | MULTIOP(i, rator, args) =>
94                          "#" :: Int.toString i :: "(" :: Op.toString rator :: "("
95                            :: argsToS (args, "))" :: l)
96                      | APPLY(f, args) => ILBasis.toString f :: "(" :: argsToS (args, ")" :: l)
97                      | CONS(ty, args) => "<" :: IL.Ty.toString ty :: ">[" :: argsToS (args, "]" :: l)
98                      | PHI args => "PHI(" :: argsToS (args, ")" :: l)
99                    (* end case *))
100              and argsToS ([], l) = l
101                | argsToS ([e], l) = toS(e, l)
102                | argsToS (e::es, l) = toS(e, ","::argsToS(es, l))
103              in
104                String.concat (toS (exp, []))
105              end
106    
107    (* hash-cons construction of expressions *)    (* hash-cons construction of expressions *)
108      datatype tbl = Tbl of expr_nd HC.tbl      datatype tbl = Tbl of expr_nd HC.tbl
109    
110      fun new () = Tbl(HC.new{eq = sameNd})      fun new () = Tbl(HC.new{eq = sameNd})
111    
112    (* FIXME: need a new prime! *)
113        fun mkSTATE (Tbl tbl) x = HC.cons0 tbl (0w7919 + IL.StateVar.hash x, STATE x)
114      fun mkVAR (Tbl tbl) x = HC.cons0 tbl (0w7919 + IL.Var.hash x, VAR x)      fun mkVAR (Tbl tbl) x = HC.cons0 tbl (0w7919 + IL.Var.hash x, VAR x)
115      fun mkLIT (Tbl tbl) a = HC.cons0 tbl (0w6997 + Literal.hash a, LIT a)      fun mkLIT (Tbl tbl) a = HC.cons0 tbl (0w6997 + Literal.hash a, LIT a)
116      fun mkOP (Tbl tbl) (rator, args) =      fun mkOP (Tbl tbl) (rator, args) =
117            HC.consList tbl (Op.hash rator, fn args => OP(rator, args)) args            HC.consList tbl (Op.hash rator, fn args => OP(rator, args)) args
118        fun mkMULTIOP (Tbl tbl) (i, rator, args) =
119              HC.consList tbl (Op.hash rator + Word.fromInt i, fn args => OP(rator, args)) args
120      fun mkAPPLY (Tbl tbl) (f, args) =      fun mkAPPLY (Tbl tbl) (f, args) =
121            HC.consList tbl (ILBasis.hash f, fn args => APPLY(f, args)) args            HC.consList tbl (ILBasis.hash f, fn args => APPLY(f, args)) args
122      fun mkCONS (Tbl tbl) (ty, args) =      fun mkCONS (Tbl tbl) (ty, args) =

Legend:
Removed from v.1639  
changed lines
  Added in v.1640

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