annotate src/cjr_print.sml @ 50:d37518b67bd2

Better FFI function handling
author Adam Chlipala <adamc@hcoop.net>
date Sun, 22 Jun 2008 10:06:50 -0400
parents 1c91c5e6840f
children 4f641f8fddaa
rev   line source
adamc@29 1 (* Copyright (c) 2008, Adam Chlipala
adamc@29 2 * All rights reserved.
adamc@29 3 *
adamc@29 4 * Redistribution and use in source and binary forms, with or without
adamc@29 5 * modification, are permitted provided that the following conditions are met:
adamc@29 6 *
adamc@29 7 * - Redistributions of source code must retain the above copyright notice,
adamc@29 8 * this list of conditions and the following disclaimer.
adamc@29 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@29 10 * this list of conditions and the following disclaimer in the documentation
adamc@29 11 * and/or other materials provided with the distribution.
adamc@29 12 * - The names of contributors may not be used to endorse or promote products
adamc@29 13 * derived from this software without specific prior written permission.
adamc@29 14 *
adamc@29 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@29 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@29 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@29 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@29 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@29 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@29 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@29 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@29 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@29 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@29 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@29 26 *)
adamc@29 27
adamc@29 28 (* Pretty-printing C jr. *)
adamc@29 29
adamc@29 30 structure CjrPrint :> CJR_PRINT = struct
adamc@29 31
adamc@29 32 open Print.PD
adamc@29 33 open Print
adamc@29 34
adamc@29 35 open Cjr
adamc@29 36
adamc@29 37 structure E = CjrEnv
adamc@29 38 structure EM = ErrorMsg
adamc@29 39
adamc@29 40 val debug = ref false
adamc@29 41
adamc@29 42 val dummyTyp = (TNamed 0, ErrorMsg.dummySpan)
adamc@29 43
adamc@29 44 fun p_typ' par env (t, loc) =
adamc@29 45 case t of
adamc@29 46 TTop =>
adamc@29 47 (EM.errorAt loc "Undetermined type";
adamc@29 48 string "?")
adamc@29 49 | TFun =>
adamc@29 50 (EM.errorAt loc "Undetermined function type";
adamc@29 51 string "?->")
adamc@29 52 | TCode (t1, t2) => parenIf par (box [p_typ' true env t2,
adamc@29 53 space,
adamc@29 54 string "(*)",
adamc@29 55 space,
adamc@29 56 string "(",
adamc@29 57 p_typ env t1,
adamc@29 58 string ")"])
adamc@29 59 | TRecord i => box [string "struct",
adamc@29 60 space,
adamc@29 61 string "__lws_",
adamc@29 62 string (Int.toString i)]
adamc@29 63 | TNamed n =>
adamc@29 64 (string ("__lwt_" ^ #1 (E.lookupTNamed env n) ^ "_" ^ Int.toString n)
adamc@29 65 handle CjrEnv.UnboundNamed _ => string ("__lwt_UNBOUND__" ^ Int.toString n))
adamc@29 66
adamc@29 67 and p_typ env = p_typ' false env
adamc@29 68
adamc@29 69 fun p_rel env n = string ("__lwr_" ^ #1 (E.lookupERel env n) ^ "_" ^ Int.toString (E.countERels env - n - 1))
adamc@29 70 handle CjrEnv.UnboundRel _ => string ("__lwr_UNBOUND_" ^ Int.toString (E.countERels env - n - 1))
adamc@29 71
adamc@29 72 fun p_exp' par env (e, _) =
adamc@29 73 case e of
adamc@29 74 EPrim p => Prim.p_t p
adamc@29 75 | ERel n => p_rel env n
adamc@29 76 | ENamed n =>
adamc@29 77 (string ("__lwn_" ^ #1 (E.lookupENamed env n) ^ "_" ^ Int.toString n)
adamc@29 78 handle CjrEnv.UnboundNamed _ => string ("__lwn_UNBOUND_" ^ Int.toString n))
adamc@29 79 | ECode n => string ("__lwc_" ^ Int.toString n)
adamc@29 80 | EApp (e1, e2) => parenIf par (box [p_exp' true env e1,
adamc@29 81 string "(",
adamc@29 82 p_exp env e2,
adamc@29 83 string ")"])
adamc@29 84
adamc@29 85 | ERecord (i, xes) => box [string "({",
adamc@29 86 space,
adamc@29 87 string "struct",
adamc@29 88 space,
adamc@29 89 string ("__lws_" ^ Int.toString i),
adamc@29 90 space,
adamc@29 91 string "__lw_tmp",
adamc@29 92 space,
adamc@29 93 string "=",
adamc@29 94 space,
adamc@29 95 string "{",
adamc@29 96 p_list (fn (_, e) =>
adamc@29 97 p_exp env e) xes,
adamc@29 98 string "};",
adamc@29 99 space,
adamc@29 100 string "__lw_tmp;",
adamc@29 101 space,
adamc@29 102 string "})" ]
adamc@29 103 | EField (e, x) =>
adamc@29 104 box [p_exp' true env e,
adamc@29 105 string ".",
adamc@29 106 string x]
adamc@29 107
adamc@29 108 | ELet (xes, e) =>
adamc@29 109 let
adamc@29 110 val (env, pps) = foldl (fn ((x, t, e), (env, pps)) =>
adamc@29 111 let
adamc@29 112 val env' = E.pushERel env x t
adamc@29 113 in
adamc@29 114 (env',
adamc@29 115 List.revAppend ([p_typ env t,
adamc@29 116 space,
adamc@29 117 p_rel env' 0,
adamc@29 118 space,
adamc@29 119 string "=",
adamc@29 120 space,
adamc@29 121 p_exp env e,
adamc@29 122 string ";",
adamc@29 123 newline],
adamc@29 124 pps))
adamc@29 125 end)
adamc@29 126 (env, []) xes
adamc@29 127 in
adamc@29 128 box [string "({",
adamc@29 129 newline,
adamc@29 130 box (rev pps),
adamc@29 131 p_exp env e,
adamc@29 132 space,
adamc@29 133 string ";",
adamc@29 134 newline,
adamc@29 135 string "})"]
adamc@29 136 end
adamc@29 137
adamc@29 138 and p_exp env = p_exp' false env
adamc@29 139
adamc@29 140 fun p_decl env ((d, _) : decl) =
adamc@29 141 case d of
adamc@29 142 DStruct (n, xts) =>
adamc@29 143 box [string "struct",
adamc@29 144 space,
adamc@29 145 string ("__lws_" ^ Int.toString n),
adamc@29 146 space,
adamc@29 147 string "{",
adamc@29 148 newline,
adamc@29 149 p_list_sep (box []) (fn (x, t) => box [p_typ env t,
adamc@29 150 space,
adamc@29 151 string x,
adamc@29 152 string ";",
adamc@29 153 newline]) xts,
adamc@29 154 string "};"]
adamc@29 155
adamc@29 156 | DVal (x, n, t, e) =>
adamc@29 157 box [p_typ env t,
adamc@29 158 space,
adamc@29 159 string ("__lwn_" ^ x ^ "_" ^ Int.toString n),
adamc@29 160 space,
adamc@29 161 string "=",
adamc@29 162 space,
adamc@29 163 p_exp env e,
adamc@29 164 string ";"]
adamc@29 165 | DFun (n, x, dom, ran, e) =>
adamc@29 166 let
adamc@29 167 val env' = E.pushERel env x dom
adamc@29 168 in
adamc@29 169 box [p_typ env ran,
adamc@29 170 space,
adamc@29 171 string ("__lwc_" ^ Int.toString n),
adamc@29 172 string "(",
adamc@29 173 p_typ env dom,
adamc@29 174 space,
adamc@29 175 p_rel env' 0,
adamc@29 176 string ")",
adamc@29 177 space,
adamc@29 178 string "{",
adamc@29 179 newline,
adamc@29 180 box[string "return(",
adamc@29 181 p_exp env' e,
adamc@29 182 string ")"],
adamc@29 183 newline,
adamc@29 184 string "}"]
adamc@29 185 end
adamc@29 186
adamc@29 187 fun p_file env file =
adamc@29 188 let
adamc@31 189 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
adamc@31 190 (p_decl env d,
adamc@31 191 E.declBinds env d))
adamc@29 192 env file
adamc@29 193 in
adamc@29 194 p_list_sep newline (fn x => x) pds
adamc@29 195 end
adamc@29 196
adamc@29 197 end