|
| 1 | +open Common;; |
| 2 | +open Semant;; |
| 3 | + |
| 4 | +let log cx = |
| 5 | + Session.log |
| 6 | + "simplify" |
| 7 | + cx.Semant.ctxt_sess.Session.sess_log_simplify |
| 8 | + cx.Semant.ctxt_sess.Session.sess_log_out |
| 9 | + |
| 10 | +let iflog cx thunk = |
| 11 | + if cx.Semant.ctxt_sess.Session.sess_log_simplify |
| 12 | + then thunk () |
| 13 | + else () |
| 14 | +;; |
| 15 | + |
| 16 | + |
| 17 | +let plval_const_marking_visitor |
| 18 | + (cx:Semant.ctxt) |
| 19 | + (inner:Walk.visitor) |
| 20 | + : Walk.visitor = |
| 21 | + let visit_pexp_pre pexp = |
| 22 | + begin |
| 23 | + match pexp.node with |
| 24 | + Ast.PEXP_lval pl -> |
| 25 | + begin |
| 26 | + let id = lval_base_id_to_defn_base_id cx pexp.id in |
| 27 | + let is_const = |
| 28 | + if defn_id_is_item cx id |
| 29 | + then match (get_item cx id).Ast.decl_item with |
| 30 | + Ast.MOD_ITEM_const _ -> true |
| 31 | + | _ -> false |
| 32 | + else false |
| 33 | + in |
| 34 | + iflog cx (fun _ -> log cx "plval %a refers to %s" |
| 35 | + Ast.sprintf_plval pl |
| 36 | + (if is_const then "const item" else "non-const")); |
| 37 | + htab_put cx.ctxt_plval_const pexp.id is_const |
| 38 | + end |
| 39 | + | _ -> () |
| 40 | + end; |
| 41 | + inner.Walk.visit_pexp_pre pexp |
| 42 | + in |
| 43 | + |
| 44 | + let visit_pexp_post p = |
| 45 | + inner.Walk.visit_pexp_post p; |
| 46 | + iflog cx (fun _ -> log cx "pexp %a is %s" |
| 47 | + Ast.sprintf_pexp p |
| 48 | + (if pexp_is_const cx p |
| 49 | + then "constant" |
| 50 | + else "non-constant")) |
| 51 | + in |
| 52 | + |
| 53 | + { inner with |
| 54 | + Walk.visit_pexp_pre = visit_pexp_pre; |
| 55 | + Walk.visit_pexp_post = visit_pexp_post; |
| 56 | + } |
| 57 | +;; |
| 58 | + |
| 59 | + |
| 60 | +let pexp_simplifying_visitor |
| 61 | + (_:Semant.ctxt) |
| 62 | + (inner:Walk.visitor) |
| 63 | + : Walk.visitor = |
| 64 | + |
| 65 | + let walk_atom at = |
| 66 | + match at with |
| 67 | + Ast.ATOM_pexp _ -> |
| 68 | + begin |
| 69 | + (* FIXME: move desugaring code from frontend to here. *) |
| 70 | + () |
| 71 | + end |
| 72 | + | _ -> () |
| 73 | + in |
| 74 | + |
| 75 | + let visit_stmt_pre s = |
| 76 | + begin |
| 77 | + match s.node with |
| 78 | + Ast.STMT_copy (_, Ast.EXPR_atom a) -> walk_atom a |
| 79 | + | _ -> () |
| 80 | + end; |
| 81 | + inner.Walk.visit_stmt_pre s; |
| 82 | + in |
| 83 | + { inner with |
| 84 | + Walk.visit_stmt_pre = visit_stmt_pre; |
| 85 | + } |
| 86 | +;; |
| 87 | + |
| 88 | + |
| 89 | +let process_crate (cx:Semant.ctxt) (crate:Ast.crate) : unit = |
| 90 | + let path = Stack.create () in |
| 91 | + |
| 92 | + let passes = |
| 93 | + [| |
| 94 | + (plval_const_marking_visitor cx Walk.empty_visitor); |
| 95 | + (pexp_simplifying_visitor cx Walk.empty_visitor) |
| 96 | + |] |
| 97 | + in |
| 98 | + let log_flag = cx.Semant.ctxt_sess.Session.sess_log_simplify in |
| 99 | + Semant.run_passes cx "simplify" path passes log_flag log crate |
| 100 | +;; |
| 101 | + |
| 102 | +(* |
| 103 | + * Local Variables: |
| 104 | + * fill-column: 78; |
| 105 | + * indent-tabs-mode: nil |
| 106 | + * buffer-file-coding-system: utf-8-unix |
| 107 | + * compile-command: "make -k -C ../.. 2>&1 | sed -e 's/\\/x\\//x:\\//g'"; |
| 108 | + * End: |
| 109 | + *) |
| 110 | + |
0 commit comments