# HG changeset patch # User Adam Chlipala # Date 1215719896 14400 # Node ID bed5cf0b6b757c55206eac1bd5dbf195c2cf3afa # Parent d101cb1efe55cbae3f25c1cb0f2d4251e3e20d79 Optimizing attrification of constants diff -r d101cb1efe55 -r bed5cf0b6b75 src/mono_opt.sml --- a/src/mono_opt.sml Thu Jul 10 15:49:14 2008 -0400 +++ b/src/mono_opt.sml Thu Jul 10 15:58:16 2008 -0400 @@ -33,6 +33,24 @@ fun typ t = t fun decl d = d +fun attrifyInt n = + if n < 0 then + "-" ^ Int64.toString (Int64.~ n) + else + Int64.toString n + +fun attrifyFloat n = + if n < 0.0 then + "-" ^ Real.toString (Real.~ n) + else + Real.toString n + +val attrifyString = String.translate (fn #"\"" => """ + | ch => if Char.isPrint ch then + str ch + else + "&#" ^ Int.toString (ord ch) ^ ";") + fun exp e = case e of EPrim (Prim.String s) => @@ -85,10 +103,24 @@ ESeq ((optExp (EWrite e1, loc), loc), (optExp (EWrite e2, loc), loc)) + | EFfiApp ("Basis", "attrifyInt", [(EPrim (Prim.Int n), _)]) => + EPrim (Prim.String (attrifyInt n)) + | EWrite (EFfiApp ("Basis", "attrifyInt", [(EPrim (Prim.Int n), _)]), loc) => + EWrite (EPrim (Prim.String (attrifyInt n)), loc) | EWrite (EFfiApp ("Basis", "attrifyInt", [e]), _) => EFfiApp ("Basis", "attrifyInt_w", [e]) + + | EFfiApp ("Basis", "attrifyFloat", [(EPrim (Prim.Float n), _)]) => + EPrim (Prim.String (attrifyFloat n)) + | EWrite (EFfiApp ("Basis", "attrifyFloat", [(EPrim (Prim.Float n), _)]), loc) => + EWrite (EPrim (Prim.String (attrifyFloat n)), loc) | EWrite (EFfiApp ("Basis", "attrifyFloat", [e]), _) => EFfiApp ("Basis", "attrifyFloat_w", [e]) + + | EFfiApp ("Basis", "attrifyString", [(EPrim (Prim.String s), _)]) => + EPrim (Prim.String (attrifyString s)) + | EWrite (EFfiApp ("Basis", "attrifyString", [(EPrim (Prim.String s), _)]), loc) => + EWrite (EPrim (Prim.String (attrifyString s)), loc) | EWrite (EFfiApp ("Basis", "attrifyString", [e]), _) => EFfiApp ("Basis", "attrifyString_w", [e])