From 3591c116de0b7f279e9e7d96eb526f2b125786b4 Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Mon, 7 Oct 2013 15:29:19 -0700 Subject: [PATCH 1/5] COMPILER: better renaming with try catch merge me --- compiler/js_var.ml | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/compiler/js_var.ml b/compiler/js_var.ml index 228c237851..036b80afc2 100644 --- a/compiler/js_var.ml +++ b/compiler/js_var.ml @@ -306,16 +306,25 @@ and statement t s = match s with let t = match w with | None -> t | Some (id,block) -> - let t = statements t block in - let t = def_var t id in - t - (* let tbody = statements (empty t) block in *) - (* let tbody = def_var tbody id in *) - (* let tbody = mark tbody in *) - (* let t = merge_info ~from:tbody ~into:t in *) - (* { t with *) - (* use = S.union t.use (rm_var t.use id) ; *) - (* def = S.union t.def (rm_var t.def id) } *) + let t' = statements (empty t) block in + let t' = def_var t' id in + (* should we treat 'id' as a regular param ? *) + (* add_constraints [id] t'; *) + add_constraints [] t'; + + (* special merge here *) + (* we need to propagate both def and use .. *) + (* .. except 'id' because its scope is limitied to 'block' *) + let clean set sets = match id with + | S s -> set,StringSet.remove s sets + | V i -> S.remove i set, sets in + let def,def_name = clean t'.def t'.def_name in + let use,use_name = clean t'.use t'.use_name in + {t with + use = S.union t.use use; + use_name = StringSet.union t.use_name use_name; + def = S.union t.def def; + def_name = StringSet.union t.def_name def_name } in let t = match f with | None -> t From f2d0ed6d82b294b42c565d51769b7ee312fceb6d Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Mon, 7 Oct 2013 15:35:29 -0700 Subject: [PATCH 2/5] COMPILER: choose between simple and double quote from string --- compiler/js_output.ml | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/compiler/js_output.ml b/compiler/js_output.ml index 924b1bfcb3..f413bf2f74 100644 --- a/compiler/js_output.ml +++ b/compiler/js_output.ml @@ -186,7 +186,19 @@ end) = struct | EFun (_, _) | EObj _ -> true - let string_escape s = + let best_string_quote s = + let simple = ref 0 and double = ref 0 in + for i = 0 to String.length s - 1 do + match s.[i] with + | '\'' -> incr simple + | '"' -> incr double + | _ -> () + done; + if !simple < !double + then '\'' + else '"' + + let string_escape quote s = let l = String.length s in let b = Buffer.create (4 * l) in let conv = "0123456789abcdef" in @@ -209,8 +221,6 @@ end) = struct Buffer.add_string b "\\f" | '\r' -> Buffer.add_string b "\\r" - | '"' -> - Buffer.add_string b "\\\"" | '\\' -> Buffer.add_string b "\\\\" | '\000' .. '\031' | '\127' .. '\255' -> @@ -219,6 +229,7 @@ end) = struct Buffer.add_char b conv.[c lsr 4]; Buffer.add_char b conv.[c land 0xf] | _ -> + if c = quote then Buffer.add_char b '\\'; Buffer.add_char b c done; Buffer.contents b @@ -274,10 +285,12 @@ end) = struct PP.end_group f; PP.end_group f; if l > 15 then begin PP.string f ")"; PP.end_group f end - | EStr (s, `Bytes) -> - PP.string f "\""; - PP.string f (string_escape s); - PP.string f "\"" + | EStr (s, kind) -> + let quote = best_string_quote s in + let quote_s = String.make 1 quote in + PP.string f quote_s; + PP.string f (string_escape quote s); + PP.string f quote_s | EBool b -> PP.string f (if b then "true" else "false") | ENum v -> From 4dcb65fdcdf01768c09c0c60309e79bc77331efb Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Mon, 7 Oct 2013 16:09:54 -0700 Subject: [PATCH 3/5] COMPILER: utf8-string, do not escape bytes above 127 --- compiler/javascript.ml | 2 +- compiler/javascript.mli | 2 +- compiler/js_output.ml | 11 ++++++++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/compiler/javascript.ml b/compiler/javascript.ml index f177d677e9..8939737863 100644 --- a/compiler/javascript.ml +++ b/compiler/javascript.ml @@ -83,7 +83,7 @@ and expression = | ENew of expression * arguments option | EVar of ident | EFun of function_expression * node_pc - | EStr of string * [`Bytes (*| `Utf8*)] + | EStr of string * [`Bytes | `Utf8] | EArr of array_litteral | EBool of bool | ENum of float diff --git a/compiler/javascript.mli b/compiler/javascript.mli index 9b19f1c1c1..1ee08f15d1 100644 --- a/compiler/javascript.mli +++ b/compiler/javascript.mli @@ -72,7 +72,7 @@ and expression = | ENew of expression * arguments option | EVar of ident | EFun of function_expression * node_pc - | EStr of string * [`Bytes (*| `Utf8*)] + | EStr of string * [`Bytes | `Utf8] | EArr of array_litteral | EBool of bool | ENum of float diff --git a/compiler/js_output.ml b/compiler/js_output.ml index f413bf2f74..fc502f87b0 100644 --- a/compiler/js_output.ml +++ b/compiler/js_output.ml @@ -198,7 +198,7 @@ end) = struct then '\'' else '"' - let string_escape quote s = + let string_escape quote ?(utf=false) s = let l = String.length s in let b = Buffer.create (4 * l) in let conv = "0123456789abcdef" in @@ -223,7 +223,12 @@ end) = struct Buffer.add_string b "\\r" | '\\' -> Buffer.add_string b "\\\\" - | '\000' .. '\031' | '\127' .. '\255' -> + | '\000' .. '\031' -> + let c = Char.code c in + Buffer.add_string b "\\x"; + Buffer.add_char b conv.[c lsr 4]; + Buffer.add_char b conv.[c land 0xf] + | '\127' .. '\255' when not utf -> let c = Char.code c in Buffer.add_string b "\\x"; Buffer.add_char b conv.[c lsr 4]; @@ -289,7 +294,7 @@ end) = struct let quote = best_string_quote s in let quote_s = String.make 1 quote in PP.string f quote_s; - PP.string f (string_escape quote s); + PP.string f (string_escape ~utf:(kind = `Utf8) quote s); PP.string f quote_s | EBool b -> PP.string f (if b then "true" else "false") From 725ff4bc1b1b1c19ce65c7853b1b173dd46657bd Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Tue, 8 Oct 2013 09:08:27 -0700 Subject: [PATCH 4/5] Compiler: consistent name for catch parameter --- compiler/js_var.ml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/compiler/js_var.ml b/compiler/js_var.ml index 036b80afc2..6ca09f3bd3 100644 --- a/compiler/js_var.ml +++ b/compiler/js_var.ml @@ -158,7 +158,7 @@ let get_free t = S.diff t.use t.def let get_free_name t = StringSet.diff t.use_name t.def_name -let add_constraints params g = +let add_constraints ?(offset=0) params g = if Option.Optim.shortvar () then begin let u = S.union g.def g.use in let constr = g.global.constr in @@ -167,15 +167,16 @@ let add_constraints params g = (fun v -> let i = Code.Var.idx v in constr.(i) <- c :: constr.(i)) u; let params = Array.of_list params in let len = Array.length params in - if Array.length g.global.parameters < len then begin - let a = Array.make (2 * len) [] in + let len_max = len + offset in + if Array.length g.global.parameters < len_max then begin + let a = Array.make (2 * len_max) [] in Array.blit g.global.parameters 0 a 0 (Array.length g.global.parameters); g.global.parameters <- a end; for i = 0 to len - 1 do match params.(i) with Javascript.V x -> - g.global.parameters.(i) <- x :: g.global.parameters.(i) + g.global.parameters.(i + offset) <- x :: g.global.parameters.(i + offset) | _ -> () done; @@ -308,9 +309,7 @@ and statement t s = match s with | Some (id,block) -> let t' = statements (empty t) block in let t' = def_var t' id in - (* should we treat 'id' as a regular param ? *) - (* add_constraints [id] t'; *) - add_constraints [] t'; + add_constraints ~offset:5 [id] t'; (* special merge here *) (* we need to propagate both def and use .. *) From 206d58bb4421ccac33d928c3a10bee3a3bb5be9b Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Tue, 8 Oct 2013 09:11:36 -0700 Subject: [PATCH 5/5] Compiler: escape char 127 --- compiler/js_output.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/js_output.ml b/compiler/js_output.ml index fc502f87b0..093a0b643f 100644 --- a/compiler/js_output.ml +++ b/compiler/js_output.ml @@ -223,12 +223,12 @@ end) = struct Buffer.add_string b "\\r" | '\\' -> Buffer.add_string b "\\\\" - | '\000' .. '\031' -> + | '\000' .. '\031' | '\127'-> let c = Char.code c in Buffer.add_string b "\\x"; Buffer.add_char b conv.[c lsr 4]; Buffer.add_char b conv.[c land 0xf] - | '\127' .. '\255' when not utf -> + | '\128' .. '\255' when not utf -> let c = Char.code c in Buffer.add_string b "\\x"; Buffer.add_char b conv.[c lsr 4];