js0.sml 6.87 KB
Newer Older
mb0's avatar
mb0 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

structure JS0Templates = struct
   val runtime = ExpandFile.mkTemplateFromFile "detail/codegen/js0/runtime.js"

   fun expandRuntime hooks =
      ExpandFile.expandTemplate
         {src=runtime,
          dst="dis.js",
          hooks=hooks}

   fun mkPrint f os = Pretty.prettyTo(os, f())
   fun mkFunctionsHook d = ("functions", mkPrint (fn () => d))
end

structure JS0Factory = struct
   structure JS = Javascript
   open JS
   structure JSStmt = struct
      fun const (x,e) = Joint.Const (Vector.fromList [(x,e)])
      fun function (f,xs,body) = Joint.FunctionDec {args=Vector.fromList xs, name=f,body=Vector.fromList body}
      fun seq xs = Joint.Block (Vector.fromList xs)
      fun call (f,xs) = Joint.Exp (Joint.Call {args=Vector.fromList(map Joint.Id xs),func=Joint.Id f})
      fun return e = Joint.Return (SOME e)
      fun throw e = Joint.Throw e
      fun switch (x, cs, dflt) = Joint.Switch {test=x,clauses=Vector.fromList (cs@[([],dflt)])}
      fun assignVars (x, y) = Joint.Exp (Joint.Assign {lhs=Joint.Id x, oper=AssignOp.Equals, rhs=Joint.Id y})
      fun assign (x, y) = Joint.Exp (Joint.Assign {lhs=x, oper=AssignOp.Equals, rhs=y})
   end
   structure JSExp = struct
      fun lambda (xs,body) = Joint.Function {args=Vector.fromList xs,body=body,name=NONE}
      fun call (f,xs) = Joint.Call {args=Vector.fromList(map Joint.Id xs),func=Joint.Id f}
      fun object fs =
         Joint.Object
            (Vector.fromList (map (fn (p,v) =>
               Joint.Property {property=p,value=v}) fs))
      fun tagged (tag,x) =
         object
            [(PropertyName.fromString "tag", tag),
             (PropertyName.fromString "payload", x)]
      fun int n = Joint.Number (Number.fromInt n)
      fun word n = Joint.Number (Number.fromInt (Word.toInt n))
      fun string s = Joint.String (JSString.fromString s)
      fun bitvec (sz,vec) =
         object
            [(PropertyName.fromString "sz", sz),
             (PropertyName.fromString "vec", vec)]
      fun select (p, obj) = Joint.SelectId {property=p, object=obj}
      fun id x = Joint.Id x
      val unit = object []
   end
end

structure JS0 = struct
   structure JS = Javascript
   structure CM = CompilationMonad
   open JS JS0Factory CPS CPS.Exp

   fun codegen cpsSpec =
      let
mb0's avatar
mb0 committed
60
         val () = Mangle.reset()
mb0's avatar
mb0 committed
61
62
63
64
         fun id s = Id.fromString (Mangle.apply s)
         fun field f = PropertyName.fromString (Mangle.applyField f)
         fun fieldId f = Id.fromString (Mangle.applyField f)
         fun tagName tag = Mangle.getStringOfTag tag
mb0's avatar
Up.    
mb0 committed
65
66
67
68
69
         fun veclit v = 
            (* We need to handle empty bit-vector patterns here *)
            case v of
               "" => 0
             | _ => valOf (StringCvt.scanString (Int.scan StringCvt.BIN) v)
mb0's avatar
mb0 committed
70
71
72
73
74
75
76
         val empty = []
         fun visitExp (e,acc) =
            case e of
               LETVAL (x,v,t) => visitExp (t, JSStmt.const (id x, visitVal v)::acc)
             | LETREC (ds,t) => visitExp (t, foldl visitRec acc ds)
             | LETCONT (ds,t) => visitExp (t, foldl visitCont acc ds)
             | LETPRJ (x,f,y,t) => visitExp (t, JSStmt.const (id x, JSExp.select (fieldId f, JSExp.id (id y)))::acc)
mb0's avatar
mb0 committed
77
             | LETDECON (x,y,t) => visitExp (t, JSStmt.const (id x, JSExp.select (Id.fromString "payload", JSExp.id (id y)))::acc)
mb0's avatar
mb0 committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
             | LETUPD (x,y,fs,t) =>
                  (* FIXME: destructive update! *)
                  let
                     val x = id x
                     val y = id y
                     val upds =
                        map (fn (f, z) =>
                           JSStmt.assign
                              (JSExp.select (fieldId f, JSExp.id x),
                               JSExp.id (id z))) fs
                  in
                     visitExp (t, (upds@
                        [JSStmt.const (x, JSExp.id y)])@acc)
                  end
             | APP (f,k,xs) => rev (JSStmt.return (JSExp.call (id f, map id (k::xs)))::acc)
             | CC (c,xs) => rev (JSStmt.return (JSExp.call (id c, map id xs))::acc)
             | CASE (x,cs) =>
                  let
                     fun branch (k,xs) = Vector.fromList [JSStmt.return (JSExp.call (id k, map id xs))]
                     val cs' = List.filter (fn (cs, _) => not (null cs)) cs
                     val dflt = List.find (fn (cs, _) => null cs) cs
mb0's avatar
mb0 committed
99
                     val fatalDflt = Vector.fromList [JSStmt.throw (JSExp.string "[Match]")]
mb0's avatar
mb0 committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
                     val dflt =
                        case dflt of
                           NONE => fatalDflt
                         | SOME (_, t) => branch t
                  in
                     rev (JSStmt.switch
                        (JSExp.call (Id.fromString "__casetag", [id x]),
                         map (fn (cs, t) =>
                           (map JSExp.word cs, branch t)) cs',
                         dflt)::acc)
                  end
         
         and visitRec ((f, k, xs, t),acc) = JSStmt.function (id f, map id (k::xs), visitExp (t,[]))::acc
         and visitCont ((k, xs, t),acc) = JSStmt.function (id k, map id xs, visitExp (t,[]))::acc
         and visitVal v =
            case v of
               FN (k,xs,t) => JSExp.lambda (map id (k::xs), Vector.fromList (visitExp (t,[])))
             | PRI (f,xs) => JSExp.call (id f, map id xs)
             | INJ (tag,x) => JSExp.tagged (JSExp.string (tagName tag), JSExp.id (id x))
             | REC fs => JSExp.object (map (fn (f,x) => (field f, JSExp.id (id x))) fs)
             | INT i => JSExp.int (IntInf.toInt i) (* FIXME: precision loss! *)
             | STR s => JSExp.string s
             | VEC v => JSExp.bitvec (JSExp.int (String.size v), JSExp.int (veclit v))
             | UNT => JSExp.unit
             | _ => raise Fail "codegen.js0.unimplementedValueBinding"

         val cps = Spec.get#declarations cpsSpec
         val exports = Spec.get#exports cpsSpec
         (* register exported names *)
         val () =
            app (fn f =>
               let
                  val _ = Mangle.applyExport f
               in
                  ()
               end) exports
         val js = 
            let
               val funs =
                  case cps of
                     LETREC (ds,_) => ds
                   | _ => raise Fail "codegen.js0.invalidCpsProgram"
               val jsfuns = foldl visitRec [] funs
            in
               Program.T (Vector.fromList jsfuns)
            end
         val _ =
            JS0Templates.expandRuntime
               [JS0Templates.mkFunctionsHook (Program.layout js)]
      in
         js
      end

   fun dumpPre (os, spec) = Pretty.prettyTo (os, CPS.PP.spec spec)
   fun dumpPost (os, js) = Pretty.prettyTo (os, Program.layout js)

   val pass =
      BasicControl.mkKeepPass
         {passName="js0Codegen",
          registry=CodegenControl.registry,
          pass=codegen,
          preExt="cps",
          preOutput=dumpPre,
          postExt="js",
          postOutput=dumpPost}

   fun run spec = CM.return (pass spec)
end