compile.ml 6.59 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
open Ident
open Lambda

type env = {
  vars: var_loc Env.t;
  stack_size: int
}

let empty = { vars = Env.empty; stack_size = 0 }

11
12
13
14
15
16
17
18
19
20
21
22
23
let serialize s env =
  Serialize.Put.env Id.serialize Lambda.Put.var_loc Env.iter s env.vars;
  Serialize.Put.int s env.stack_size    

let deserialize s =
  let vars = 
    Serialize.Get.env Id.deserialize Lambda.Get.var_loc Env.add Env.empty s in
  let size = Serialize.Get.int s in
  { vars = vars; stack_size = size }




24
25
26
27
28
let find x env =
  try Env.find x env.vars
  with Not_found -> 
    failwith ("Compile: cannot find " ^ (Ident.to_string x))

29
30
31
32
33
34
35
36
let from_comp_unit = ref (fun cu -> assert false)

let find_ext cu x =
  let env = !from_comp_unit cu in
  match find x env with
    | Global i -> ExtVar (cu,i)
    | _ -> assert false

37
38
39
40
let rec compile env tail e = compile_aux env tail e.Typed.exp_descr
and compile_aux env tail = function
  | Typed.Forget (e,_) -> compile env tail e
  | Typed.Var x -> Var (find x env)
41
  | Typed.ExtVar (cu,x) -> find_ext cu x
42
  | Typed.Apply (e1,e2) -> Apply (tail, compile env false e1, compile env tail e2)
43
44
  | Typed.Abstraction a -> compile_abstr env a
  | Typed.Cst c -> Const c
45
  | Typed.Pair (e1,e2) -> Pair(compile env false e1, compile env tail e2)
46
  | Typed.Xml (e1, { Typed.exp_descr = Typed.Pair (e2,e3) }) -> 
47
      Xml (compile env false e1, compile env false e2, compile env tail e3)
48
  | Typed.Xml (_,_) -> assert false
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  | Typed.RecordLitt r -> Record (LabelMap.map (compile env false) r)
  | Typed.String (i,j,s,q) -> String (i,j,s,compile env tail q)
  | Typed.Match (e,brs) -> Match (compile env false e, compile_branches env tail brs)
  | Typed.Map (e,brs) -> Map (compile env false e, compile_branches env false brs)
  | Typed.Transform (e,brs) -> Transform 
      (compile env false e, compile_branches env false brs)
  | Typed.Xtrans (e,brs) -> Xtrans (compile env false e, compile_branches env false brs)
  | Typed.Validate (e,sch,t) -> Validate (compile env tail e, sch, t)
  | Typed.RemoveField (e,l) -> RemoveField (compile env tail e,l)
  | Typed.Dot (e,l) -> Dot (compile env tail e, l)
  | Typed.Try (e,brs) -> Try (compile env false e, compile_branches env tail brs)
  | Typed.UnaryOp (op,e) -> UnaryOp (op, compile env tail e)
  | Typed.BinaryOp (op,e1,e2) -> BinaryOp (op, compile env false e1, compile env tail e2)
  | Typed.Ref (e,t) ->  Ref (compile env tail e, t)
63
64

and compile_abstr env a =
65
66
67
68
69
  let fun_env = 
    match a.Typed.fun_name with
      | Some x -> Env.add x (Env 0) Env.empty
      | None -> Env.empty in

70
71
72
  let (slots,nb_slots,fun_env) = 
    List.fold_left 
      (fun (slots,nb_slots,fun_env) x ->
73
	 match find x env with
74
75
76
77
78
79
80
81
82
83
	   | (Stack _ | Env _) as p -> 
	       p::slots,
	       succ nb_slots,
	       Env.add x (Env nb_slots) fun_env;
	   | Global _ as p -> 
	       slots,
	       nb_slots,
	       Env.add x p fun_env
	   | Dummy -> assert false
      )
84
      ([Dummy],1,fun_env) (IdSet.get a.Typed.fun_fv) in
85
86
87
88


  let slots = Array.of_list (List.rev slots) in  
  let env = { vars = fun_env; stack_size = 0 } in
89
90
  let body = compile_branches env true a.Typed.fun_body in
  Abstraction (slots, a.Typed.fun_iface, body)
91

92
and compile_branches env tail (brs : Typed.branches) =
93
  { 
94
95
96
    brs = List.map (compile_branch env tail) brs.Typed.br_branches;
    brs_tail = tail;
    brs_accept_chars = not (Types.Char.is_empty brs.Typed.br_accept);
97
98
99
100
    brs_input = brs.Typed.br_typ;
    brs_compiled = None
  }

101
and compile_branch env tail br =
102
103
104
105
106
  let env = 
    List.fold_left 
      (fun env x ->
	 { vars = Env.add x (Stack env.stack_size) env.vars;
	   stack_size = env.stack_size + 1 }
107

108
      ) env (Patterns.fv_list br.Typed.br_pat) in
109
110
111
112
113
114
115
116
117
  (br.Typed.br_pat, compile env tail br.Typed.br_body)


let enter_global env x =
  { vars = Env.add x (Global env.stack_size) env.vars;
    stack_size = env.stack_size + 1 }

let enter_globals = List.fold_left enter_global

118
119
let compile_eval env e = Eval (compile env false e)

120
121
let compile_let_decl env decl =
  let pat = decl.Typed.let_pat in
122
123
124
  let code = Let_decl (pat, compile env false (decl.Typed.let_body)) in
  let env = enter_globals env (Patterns.fv_list pat) in
  (env, code)
125
126
127
128
129
130
131
132
133
134
135
136


let compile_rec_funs env funs =
  let fun_name = function
    | { Typed.exp_descr=Typed.Abstraction{Typed.fun_name = Some x}} -> x
    | _ -> assert false in
  let fun_a = function
    | { Typed.exp_descr=Typed.Abstraction a } -> a
    | _ -> assert false in
  let names = List.map fun_name funs in
  let env = enter_globals env names in
  let exprs = List.map (compile_abstr env) (List.map fun_a funs) in
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
  (env, Let_funs exprs)


(****************************************)

open Location

let eval (tenv,cenv,codes) e =
  let (e,_) = Typer.type_expr tenv e in
  let code = compile_eval cenv e in 
  (tenv,cenv,code :: codes)
  
let let_decl (tenv,cenv,codes) p e =
  let (tenv,decl,_) = Typer.type_let_decl tenv p e in
  let (cenv,code) = compile_let_decl cenv decl in
  (tenv,cenv,code :: codes)
  
let let_funs (tenv,cenv,codes) funs =
  let (tenv,funs,_) = Typer.type_let_funs tenv funs in
  let (cenv,code) = compile_rec_funs cenv funs in
  (tenv,cenv,code :: codes)
  
let type_defs (tenv,cenv,codes) typs =
  let tenv = Typer.enter_types (Typer.type_defs tenv typs) tenv in
  (tenv,cenv,codes)

let namespace (tenv,cenv,codes) pr ns =
  let tenv = Typer.enter_ns pr ns tenv in
  (tenv,cenv,codes)

167
168
169
170
let using (tenv,cenv,codes) x cu =
  let tenv = Typer.enter_cu x cu tenv in
  (tenv,cenv,codes)

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
let rec collect_funs accu = function
  | { descr = Ast.FunDecl e } :: rest -> collect_funs (e::accu) rest
  | rest -> (accu,rest)

let rec collect_types accu = function
  | { descr = Ast.TypeDecl (x,t) } :: rest -> 
      collect_types ((x,t) :: accu) rest
  | rest -> (accu,rest)

let rec phrases accu phs = match phs with
  | { descr = Ast.FunDecl _ } :: _ -> 
      let (funs,rest) = collect_funs [] phs in
      phrases (let_funs accu funs) rest
  | { descr = Ast.TypeDecl (_,_) } :: _ ->
      let (typs,rest) = collect_types [] phs in
      phrases (type_defs accu typs) rest
  | { descr = Ast.SchemaDecl (name, schema) } :: rest ->
      Typer.register_schema name schema;
      phrases accu rest
  | { descr = Ast.Namespace (pr,ns) } :: rest ->
      phrases (namespace accu pr ns) rest
192
193
  | { descr = Ast.Using (x,cu) } :: rest ->
      phrases (using accu x cu) rest
194
195
196
197
198
199
200
201
202
203
204
205
206
  | { descr = Ast.EvalStatement e } :: rest ->
      phrases (eval accu e) rest
  | { descr = Ast.LetDecl (p,e) } :: rest ->
      phrases (let_decl accu p e) rest
  | { descr = Ast.Debug l } :: rest -> 
      phrases accu rest
  | { descr = Ast.Directive _ } :: rest ->
      phrases accu rest
  | [] -> accu

let comp_unit tenv cenv phs =
  let (tenv,cenv,codes) = phrases (tenv,cenv,[]) phs in
  (tenv,cenv,List.rev codes)