Skip to content
This repository
tag: v1488
Fetching contributors…

Cannot retrieve contributors at this time

file 318 lines (296 sloc) 11.383 kb
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
module Q = QmlAst
module List = BaseList
module Format = BaseFormat

type env = IdentSet.t

type runtime_type_key = string
type runtime_rpc_key = string
type runtime_ident = string
type runtime_code_elt = {
  ident : runtime_ident option;
  client_equivalent : runtime_ident option;
  defines : [ `rpc of runtime_rpc_key | `type_ of runtime_type_key | `nothing ];
  ident_deps : runtime_ident list;
  rpc_deps: runtime_rpc_key list;
  type_deps : runtime_type_key list;
  root : bool;
}
type runtime_code = runtime_code_elt list

module S =
struct
  type t = env
  let pass = "pass_GenerateServerAst"
  let pp _ _ = ()
end
module R =
struct
  include ObjectFiles.Make(S)
  let load () =
    let env = IdentSet.empty in
    (* can't be a safe union because of fakesources *)
    fold IdentSet.union env
  let save ~loaded_env ~env =
    let small_env = IdentSet.diff env loaded_env in
    save small_env
end

let pp_ident_field f = function
  | None -> Format.fprintf f "None"
  | Some i -> Format.fprintf f "Some %s" i
let pp_defines f = function
  | `nothing -> Format.fprintf f "`nothing"
  | `rpc s -> Format.fprintf f "`rpc %s" s
  | `type_ s -> Format.fprintf f "`type_ %s" s
let pp_ident_deps =
  Format.pp_list ",@ " Format.pp_print_string
let pp_rpc_deps = pp_ident_deps
let pp_type_deps = pp_ident_deps
let pp_root = Format.pp_print_bool
let pp_code_elt f {ident; client_equivalent; defines; ident_deps; rpc_deps; type_deps; root} =
  Format.fprintf f
    "@[@[<v2>{@\n@[<2>ident: %a@]@\n[<2>client_equivalent: %a@]@\n@[<2>defines: %a@]@\n@[<2>ident_deps: %a@]@\n@[<2>type_deps: %a@]@\n@[<2>rpc_deps: %a@]@\n@[<2>root: %a@]@]@\n}@]"
    pp_ident_field ident
    pp_ident_field client_equivalent
    pp_defines defines
    pp_ident_deps ident_deps
    pp_type_deps type_deps
    pp_rpc_deps rpc_deps
    pp_root root
let pp_code =
  Format.pp_list "@\n" pp_code_elt

let is_root e =
  QmlAstWalk.Expr.traverse_exists
    (fun tra -> function
     | Q.Lambda _ -> false
     | Q.Apply _ -> true
     | e -> tra e) e

type acc = {
  mutable acc_rpc_deps : runtime_rpc_key list;
  mutable acc_type_deps : runtime_type_key list;
  mutable acc_rpc_def : runtime_rpc_key list;
  mutable acc_type_def : runtime_type_key list;
  mutable acc_ident_deps : IdentSet.t;
  mutable acc_rec_ident_deps : IdentSet.t;
  mutable acc_fun_action_deps : runtime_ident list;
}

let useless = function
  | {acc_rpc_deps = [];
     acc_type_deps = [];
     acc_rpc_def = [];
     acc_type_def = [];
     acc_ident_deps = x;
     acc_rec_ident_deps = _;
     acc_fun_action_deps = []} when IdentSet.is_empty x -> true
  | _ -> false
let useful acc = not (useless acc)

let process_toplevel_binding env rec_idents e =
  let acc = {
    acc_rpc_deps = [];
    acc_type_deps = [];
    acc_rpc_def = [];
    acc_type_def = [];
    acc_ident_deps = IdentSet.empty;
    acc_rec_ident_deps = IdentSet.empty;
    acc_fun_action_deps = [];
  } in
  let e =
    QmlAstWalk.Expr.map
      (fun e ->
         match e with
         | Q.Ident (_, i) ->
             if IdentSet.mem i env then
               (* dependencies only on toplevel useful idents (env)
* or rec_idents, because for recursive bindings, this is tricky *)
               acc.acc_ident_deps <- IdentSet.add i acc.acc_ident_deps
             else if IdentSet.mem i rec_idents then
               acc.acc_rec_ident_deps <- IdentSet.add i acc.acc_rec_ident_deps;
             e
         | Q.Directive (label, `tagged_string (s, kind), _, _) ->
             (match kind with
              | Q.Type_def -> acc.acc_type_def <- s :: acc.acc_type_def
              | Q.Type_use -> acc.acc_type_deps <- s :: acc.acc_type_deps
              | Q.Rpc_def -> acc.acc_rpc_def <- s :: acc.acc_rpc_def
              | Q.Rpc_use -> acc.acc_rpc_deps <- s :: acc.acc_rpc_deps
              | Q.Client_closure_use -> acc.acc_fun_action_deps <- s :: acc.acc_fun_action_deps);
             Q.Const (label, Q.String s)
         | _ -> e
      ) e in
  acc, e

let process_code_elt (rev_qml,code,env) = function
  | Q.NewVal (label, iel)
  | Q.NewValRec (label, iel) as code_elt ->
      let idents = List.fold_left (fun acc (i,_) -> IdentSet.add i acc) IdentSet.empty iel in
      let ieal =
        List.map
          (fun (i,e) ->
             let acc, e = process_toplevel_binding env idents e in
             (i, e, acc)
          ) iel in
      let rev_qml =
        let l = List.map (fun (i,e,_) -> (i,e)) ieal in
        let code_elt =
          match code_elt with
          | Q.NewVal _ -> Q.NewVal (label, l)
          | Q.NewValRec _ -> Q.NewValRec (label, l)
          | _ -> assert false in
        code_elt :: rev_qml in
      if List.exists (fun (_, _, acc) -> useful acc) ieal then
        let env = IdentSet.union env idents in
        let code = List.rev_append ieal code in
        (rev_qml, code, env)
      else
        (rev_qml, code, env)
  | _ ->
      assert false

let process_code acc code =
  List.fold_left process_code_elt acc code

let ident_to_string = Ident.to_string
 (* don't care about that serialization fonction, i think *)

let runtime_code_of_accs ~server_renaming ~client_renaming accs =
  List.map
    (fun (i,e,acc) ->
       let {
         acc_rpc_deps;
         acc_type_deps;
         acc_rpc_def;
         acc_type_def;
         acc_ident_deps;
         acc_rec_ident_deps;
         acc_fun_action_deps
       } = acc in
       let defines =
         (match acc_rpc_def, acc_type_def with
          | [], [] -> `nothing
          | [s], [] -> `rpc s
          | [], [s] -> `type_ s
          | _ ->
              Printf.printf "ident: %s, rpc:%d, type:%d\n%!"
                (Ident.to_string i)
                (List.length acc_rpc_def) (List.length acc_type_def);
              assert false
         ) in {
         ident = Some (ident_to_string i);
         client_equivalent =
           (try
              let i = QmlRenamingMap.new_from_original client_renaming (QmlRenamingMap.original_from_new server_renaming i) in
              let s = JsPrint.string_of_ident (JsAst.ExprIdent i) in
              Some s
            with Not_found -> None);
         defines = defines;
         ident_deps = acc_fun_action_deps @ List.map ident_to_string (IdentSet.elements (IdentSet.union acc_ident_deps acc_rec_ident_deps));
         rpc_deps = acc_rpc_deps;
         type_deps = acc_type_deps;
         root = (defines = `nothing && acc_fun_action_deps = [] && is_root e)
       }
    ) accs

let ser_int b i =
  (* we need to make sure that the length of an integer is fixed (or predictable at least) *)
  (* big bytes first *)
  for j = 64 / 8 - 1 downto 0 do
    Buffer.add_char b (Char.chr ((i lsr (j*8)) mod 256));
  done
let ser_string b s =
  ser_int b (String.length s);
  Buffer.add_string b s
let ser_option ser_a b = function
  | None -> Buffer.add_char b '\000'
  | Some a -> Buffer.add_char b '\001'; ser_a b a
let ser_list ser_a b l =
  ser_int b (List.length l);
  List.iter (fun a -> ser_a b a) l
let ser_bool b bool =
  Buffer.add_char b (if bool then '\001' else '\000')

let ser_rpc b s =
  (* same escaping as in qmljs_serializer! *)
  ser_string b (JsPrint.escape_string s)
let ser_type b s =
  (* same escaping as in qmljs_serializer! *)
  ser_string b (JsPrint.escape_string s)
let ser_defines b = function
  | `nothing -> Buffer.add_char b '\000'
  | `rpc s -> Buffer.add_char b '\001'; ser_rpc b s
  | `type_ s -> Buffer.add_char b '\002'; ser_type b s
let ser_ident b o =
  ser_option ser_string b o
let ser_ident_deps b l =
  ser_list ser_string b l
let ser_rpc_deps b l =
  ser_list ser_rpc b l
let ser_type_deps b l =
  ser_list ser_type b l
let ser_root = ser_bool
let ser_code_elt b {ident; client_equivalent; defines; ident_deps; rpc_deps; type_deps; root} =
  (* alphabetic order of fields *)
  ser_ident b client_equivalent;
  ser_defines b defines;
  ser_ident b ident;
  ser_ident_deps b ident_deps;
  ser_root b root;
  ser_rpc_deps b rpc_deps;
  ser_type_deps b type_deps
let ser_code b code =
  ser_list ser_code_elt b code

let generate_register ~gamma ~stdlib_gamma ~annotmap ~val_ ~server_code =
  let ident = val_ Opacapi.Core_server_code.register_server_code in
  let tsc = QmlTypes.Env.Ident.find ident stdlib_gamma in
  let ty = QmlTypes.Scheme.instantiate tsc in
  let annotmap, fun_ = QmlAstCons.TypedExpr.ident annotmap ident ty in
  let annotmap, string = QmlAstCons.TypedExpr.string annotmap server_code in
  let annotmap, package = QmlAstCons.TypedExpr.string annotmap (ObjectFiles.get_current_package_name ()) in
  let annotmap, arg = QmlAstCons.TypedExpr.record annotmap ["adhoc", string; "package_", package] in
  let annotmap, app = QmlAstCons.TypedExpr.apply gamma annotmap fun_ [arg] in
  let dummy_ident = Ident.next "server_ast" in
  let tyvoid = Q.TypeRecord (Q.TyRow ([], None)) in
  let tsc_void = QmlTypes.Scheme.quantify tyvoid in
  let gamma = QmlTypes.Env.Ident.add dummy_ident tsc_void gamma in
  IdentSet.singleton ident, gamma, annotmap, Q.NewVal (Annot.nolabel "pass_GenerateServerAst", [dummy_ident, app])

let _outputer oc ast =
  let fmt = Format.formatter_of_out_channel oc in
  Format.fprintf fmt "%a%!" pp_code ast

let process ~annotmap ~stdlib_gamma ~gamma ~val_ ~generate ~server_renaming ~client_renaming ~code =
  match ObjectFiles.compilation_mode () with
  | `init -> gamma, annotmap, code
  | `prelude | `linking | `compilation ->
  let loaded_env = R.load () in
  let (rev_qml, acc, env) = process_code ([], [], loaded_env) code in
  R.save ~loaded_env ~env;
  let qml = List.rev rev_qml in
  if generate &&
     (try ignore (val_ Opacapi.Core_server_code.register_server_code); true
      with Not_found -> false) then (
    let rc = runtime_code_of_accs ~server_renaming ~client_renaming acc in
    #<If:SERVER_SERIALIZE>
      ignore (PassTracker.file ~filename:"serverast" _outputer rc);
    #<End>;
    let b = Buffer.create 1000 in
    ser_code b rc;
    let string = Buffer.contents b in
    #<If:SERVER_SERIALIZE$contains "overhead">
      Printf.printf "length: %d\n%!" (String.length string);
      let r = ref 0 in
      for i = 0 to String.length string - 1 do
        if string.[i] < '\005' then incr r
      done;
      Printf.printf "overhead: %d, %.2f%%\n%!" !r (100. *. float !r /. float (String.length string))
    #<End>;
    let deps, gamma, annotmap, code_elt = generate_register ~gamma ~stdlib_gamma ~annotmap ~val_ ~server_code:string in
    (* for the first package, we need to insert the code_elt at the end :/ *)
    gamma, annotmap, QmlAstUtils.Code.insert ~deps ~insert:[code_elt] qml
  ) else
    gamma, annotmap, qml

(* FIXME:
the computation of roots could be finer:
f(x)(y) = ...
a = f(1) // a is considered as being a root
*)
Something went wrong with that request. Please try again.