Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify ++/+= #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions compiler/lib/generate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1654,16 +1654,15 @@ and translate_instr ctx expr_queue loc instr =
[ ( J.Expression_statement (J.EBin (J.Eq, J.EStructAccess (cx, int (n + 1)), cy))
, loc )
]
(* --/++ post/prefix is difficult to get working in all languages. Use +=
instead. For example x[1]++ isn't supported in PHP. *)
| Offset_ref (x, n) ->
(* FIX: may overflow.. *)
let (_px, cx), expr_queue = access_queue expr_queue x in
let sa = J.EStructAccess (cx, J.EInt 1) in
flush_queue
expr_queue
mutator_p
[ ( J.Expression_statement
(J.EBin (J.PlusEq, J.EStructAccess (cx, J.EInt 1), int n))
(J.EBin (J.Eq, sa, (J.EBin (J.IntPlus, sa, int n))))
, loc )
]
| Array_set (x, y, z) ->
Expand Down
12 changes: 0 additions & 12 deletions compiler/lib/javascript_from_rehp.re
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,10 @@ and from_unop = (unop, jsExpr) =>
| Void => EUn(Javascript.Void, jsExpr)
| Delete => EUn(Javascript.Delete, jsExpr)
| Bnot => EUn(Javascript.Bnot, jsExpr)
| IncrA => EUn(Javascript.IncrA, jsExpr)
| DecrA => EUn(Javascript.DecrA, jsExpr)
| IncrB => EUn(Javascript.IncrB, jsExpr)
| DecrB => EUn(Javascript.DecrB, jsExpr)
}
and from_binop =
fun
| Rehp.Eq => Javascript.Eq
| StarEq => StarEq
| SlashEq => SlashEq
| ModEq => ModEq
| PlusEq => PlusEq
| MinusEq => MinusEq
| BandEq => BandEq
| BxorEq => BxorEq
| BorEq => BorEq
| Or => Or
| And => And
| Bor => Bor
Expand Down
4 changes: 1 addition & 3 deletions compiler/lib/js_simpl.re
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ let rec enot_rec = e => {
| J.EInt(_)
| J.EObj(_)
| J.EQuote(_)
| J.ERegexp(_)
/* jordwalke: isn't this a bug? IncrA/Decr can have side effects! */
| J.EUn(J.IncrA | J.IncrB | J.DecrA | J.DecrB, _) => (
| J.ERegexp(_) => (
J.EUn(J.Not, e),
1,
)
Expand Down
20 changes: 0 additions & 20 deletions compiler/lib/php_from_rehp.re
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,6 @@ exception Unsupported_statement;
let binop_from_rehp = binop =>
switch (binop) {
| Rehp.Eq => Php.Eq
| StarEq => StarEq
| SlashEq => SlashEq
| ModEq => ModEq
| PlusEq => PlusEq
| MinusEq => MinusEq
| BandEq => BandEq
| BxorEq => BandEq
| BorEq => BorEq
| Or => Or
| And => And
| Bor => Bor
Expand Down Expand Up @@ -752,18 +744,6 @@ and unop_from_rehp = (input, unop, rehpExpr) =>
| Bnot =>
let (outMapped, exprMapped) = expression(input, rehpExpr);
(outMapped, EUn(Bnot, exprMapped));
| IncrA =>
let (outMapped, exprMapped) = expression(input, rehpExpr);
(outMapped, EUn(IncrA, exprMapped));
| DecrA =>
let (outMapped, exprMapped) = expression(input, rehpExpr);
(outMapped, EUn(DecrA, exprMapped));
| IncrB =>
let (outMapped, exprMapped) = expression(input, rehpExpr);
(outMapped, EUn(IncrB, exprMapped));
| DecrB =>
let (outMapped, exprMapped) = expression(input, rehpExpr);
(outMapped, EUn(DecrB, exprMapped));
}
and switchCase = (input, e) => expression(input, e)
and initialiser = (input: input, (e, pc)) => {
Expand Down
13 changes: 0 additions & 13 deletions compiler/lib/rehp.re
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ and element_list = list(option(expression))
*/
and binop =
| Eq
| StarEq
| SlashEq
| ModEq
| PlusEq
| MinusEq
/* TODO: BandEq/BxorEq/BorEq is not ever generated by the compiler */
| BandEq
| BxorEq
| BorEq
| Or
| And
| Bor
Expand Down Expand Up @@ -111,10 +102,6 @@ and unop =
| Void /* Only for stubs */
| Delete /* Only for stubs */
| Bnot
| IncrA
| DecrA
| IncrB
| DecrB
and arguments = list(expression)
and property_name_and_value_list = list((Id.property_name, expression))
and raw_segment =
Expand Down
12 changes: 0 additions & 12 deletions compiler/lib/rehp.rei
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ type array_literal = element_list
and element_list = list(option(expression))
and binop =
| Eq
| StarEq
| SlashEq
| ModEq
| PlusEq
| MinusEq
| BandEq
| BxorEq
| BorEq
| Or
| And
| Bor
Expand Down Expand Up @@ -66,10 +58,6 @@ and unop =
| Void
| Delete
| Bnot
| IncrA
| DecrA
| IncrB
| DecrB
and arguments = list(expression)
and property_name_and_value_list = list((Id.property_name, expression))
and raw_segment =
Expand Down
49 changes: 2 additions & 47 deletions compiler/lib/rehp_traverse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -775,41 +775,6 @@ class clean =
List.rev sources_rev
end

let translate_assign_op = function
| Div -> SlashEq
| Mod -> ModEq
| Band -> BandEq
| Bor -> BorEq
| Bxor -> BxorEq
| Mul -> StarEq
| Plus -> PlusEq
| Minus -> MinusEq
| _ -> assert false

let assign_op = function
| exp, EBin (Plus, exp', exp'') -> (
match exp = exp', exp = exp'' with
| false, false -> None
| true, false ->
if exp'' = EInt 1
then Some (EUn (IncrB, exp))
else Some (EBin (PlusEq, exp, exp''))
| false, true ->
if exp' = EInt 1
then Some (EUn (IncrB, exp))
else Some (EBin (PlusEq, exp, exp'))
| true, true -> Some (EBin (StarEq, exp, EInt 1)))
| exp, EBin (Minus, exp', y) when exp = exp' ->
if y = EInt 1 then Some (EUn (DecrB, exp)) else Some (EBin (MinusEq, exp, y))
| exp, EBin (Mul, exp', exp'') -> (
match exp = exp', exp = exp'' with
| false, false -> None
| true, _ -> Some (EBin (StarEq, exp, exp''))
| _, true -> Some (EBin (StarEq, exp, exp')))
| exp, EBin (((Div | Mod | Band | Bxor | Bor) as unop), exp', y) when exp = exp' ->
Some (EBin (translate_assign_op unop, exp, y))
| _ -> None

(* Performs very simple local simplifications on code that don't require
analysis of side effects *)
class simpl =
Expand Down Expand Up @@ -862,18 +827,8 @@ class simpl =
(Expression_statement (EBin (Eq, v1, ECond (cond, e1, e2))), loc) :: rem
| Variable_statement l1 ->
let x =
List.map l1 ~f:(function
| ident, None -> Variable_statement [ident, None], loc
| ident, Some (exp, pc) -> (
(* var x = x + x
Can be simplified into x *= 2 because x must have already been in scope so
no other var declaration necessary. This optimizations forms an assumption
on the nature of scope in Rehp. (TODO: Document this as part of Rehp's
semantics).
*)
match assign_op (EVar ident, exp) with
| Some e -> Expression_statement e, loc
| None -> Variable_statement [ident, Some (exp, pc)], loc))
List.map l1 ~f:(fun (ident, eo) ->
Variable_statement [ident, eo], loc)
in
x @ rem
| _ -> (st, loc) :: rem)
Expand Down
15 changes: 12 additions & 3 deletions rehack_tests/output/stdlib.cma.js/CamlinternalFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4086,7 +4086,7 @@ function transform_int_alt(iconv, s) {
for (; ; ) {
match = caml_string_unsafe_get(s, i__0);
switcher__0 = match + -48 | 0;
if (! (9 < switcher__0 >>> 0)) {n[1] += 1;}
if (! (9 < switcher__0 >>> 0)) {n[1] = n[1] + 1;}
c9_ = i__0 + 1 | 0;
if (c5_ !== i__0) {i__0 = c9_;continue;}
break;
Expand All @@ -4098,7 +4098,12 @@ function transform_int_alt(iconv, s) {
caml_ml_string_length(s) + ((digits + -1 | 0) / 3 | 0) | 0
);
pos = [0,0];
put = function(c) {caml_bytes_set(buf, pos[1], c);pos[1] += 1;return 0;};
put =
function(c) {
caml_bytes_set(buf, pos[1], c);
pos[1] = pos[1] + 1;
return 0;
};
left = [0,((digits + -1 | 0) % 3 | 0) + 1 | 0];
c7_ = caml_ml_string_length(s) + -1 | 0;
c6_ = 0;
Expand All @@ -4108,7 +4113,11 @@ function transform_int_alt(iconv, s) {
c = caml_string_unsafe_get(s, i);
switcher = c + -48 | 0;
if (9 < switcher >>> 0) put(c);
else {if (0 === left[1]) {put(95);left[1] = 3;}left[1] += -1;put(c);}
else {
if (0 === left[1]) {put(95);left[1] = 3;}
left[1] = left[1] + -1;
put(c);
}
c8_ = i + 1 | 0;
if (c7_ !== i) {i = c8_;continue;}
break;
Expand Down
8 changes: 4 additions & 4 deletions rehack_tests/output/stdlib.cma.js/CamlinternalOO.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function new_table(pub_labels) {
var az_;
var ay_;
var i;
table_count[1] += 1;
table_count[1] = table_count[1] + 1;
var len = pub_labels.length - 1;
var methods = caml_make_vect((len * 2 | 0) + 2 | 0, dummy_met);
caml_check_bound(methods, 0)[1] = len;
Expand Down Expand Up @@ -184,7 +184,7 @@ function get_method_labels(table, names) {
}

function set_method(table, label, element) {
method_count[1] += 1;
method_count[1] = method_count[1] + 1;
if (call2(Labs[27], label, table[4])) {return put(table, label, element);}
table[6] = [0,[0,label,element],table[6]];
return 0;
Expand Down Expand Up @@ -736,7 +736,7 @@ function method_impl(table, i, arr) {
var m__5;
var n__18;
function next(param) {
i[1] += 1;
i[1] = i[1] + 1;
var i_ = i[1];
return caml_check_bound(arr, i_)[i_ + 1];
}
Expand Down Expand Up @@ -864,7 +864,7 @@ function set_methods(table, methods) {
label = caml_check_bound(methods, h_)[h_ + 1];
clo = method_impl(table, i, methods);
set_method(table, label, clo);
i[1] += 1;
i[1] = i[1] + 1;
continue;
}
return 0;
Expand Down
6 changes: 3 additions & 3 deletions rehack_tests/output/stdlib.cma.js/Stdlib__arg.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ function parse_and_expand_argv_dynamic_aux(allow_expand, current, argv, speclist
}
return [0,Help,call1(Stdlib_buffer[2], b)];
}
current[1] += 1;
current[1] = current[1] + 1;
for (; ; ) {
if (current[1] < argv[1].length - 1) {
try {
Expand Down Expand Up @@ -435,7 +435,7 @@ function parse_and_expand_argv_dynamic_aux(allow_expand, current, argv, speclist
function(follow) {
function consume_arg(param) {
if (follow) {return 0;}
current[1] += 1;
current[1] = current[1] + 1;
return 0;
}
return consume_arg;
Expand Down Expand Up @@ -644,7 +644,7 @@ function parse_and_expand_argv_dynamic_aux(allow_expand, current, argv, speclist
}
throw caml_wrap_thrown_exception_reraise(exn);
}
current[1] += 1;
current[1] = current[1] + 1;
continue;
}
return 0;
Expand Down
2 changes: 1 addition & 1 deletion rehack_tests/output/stdlib.cma.js/Stdlib__array.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ function stable_sort(cmp, a) {
r_ = caml_check_bound(dst, q_)[q_ + 1];
s_ = j[1] + 1 | 0;
caml_check_bound(dst, s_)[s_ + 1] = r_;
j[1] += -1;
j[1] = j[1] + -1;
continue;
}
}
Expand Down
24 changes: 12 additions & 12 deletions rehack_tests/output/stdlib.cma.js/Stdlib__bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,12 @@ function trim(s) {
var i = [0,0];
for (; ; ) {
if (i[1] < len) {
if (is_space(caml_bytes_unsafe_get(s, i[1]))) {i[1] += 1;continue;}
if (is_space(caml_bytes_unsafe_get(s, i[1]))) {i[1] = i[1] + 1;continue;}
}
j = [0,len + -1 | 0];
for (; ; ) {
if (i[1] <= j[1]) {
if (is_space(caml_bytes_unsafe_get(s, j[1]))) {j[1] += -1;continue;}
if (is_space(caml_bytes_unsafe_get(s, j[1]))) {j[1] = j[1] + -1;continue;}
}
return i[1] <= j[1] ? sub(s, i[1], (j[1] - i[1] | 0) + 1 | 0) : empty;
}
Expand Down Expand Up @@ -398,25 +398,25 @@ function escaped(s) {
else switch (c) {
case 8:
caml_bytes_unsafe_set(s__0, n[1], 92);
n[1] += 1;
n[1] = n[1] + 1;
caml_bytes_unsafe_set(s__0, n[1], 98);
switch__2 = 3;
break;
case 9:
caml_bytes_unsafe_set(s__0, n[1], 92);
n[1] += 1;
n[1] = n[1] + 1;
caml_bytes_unsafe_set(s__0, n[1], 116);
switch__2 = 3;
break;
case 10:
caml_bytes_unsafe_set(s__0, n[1], 92);
n[1] += 1;
n[1] = n[1] + 1;
caml_bytes_unsafe_set(s__0, n[1], 110);
switch__2 = 3;
break;
case 13:
caml_bytes_unsafe_set(s__0, n[1], 92);
n[1] += 1;
n[1] = n[1] + 1;
caml_bytes_unsafe_set(s__0, n[1], 114);
switch__2 = 3;
break;
Expand All @@ -426,23 +426,23 @@ function escaped(s) {
switch (switch__2) {
case 0:
caml_bytes_unsafe_set(s__0, n[1], 92);
n[1] += 1;
n[1] = n[1] + 1;
caml_bytes_unsafe_set(s__0, n[1], 48 + (c / 100 | 0) | 0);
n[1] += 1;
n[1] = n[1] + 1;
caml_bytes_unsafe_set(s__0, n[1], 48 + ((c / 10 | 0) % 10 | 0) | 0);
n[1] += 1;
n[1] = n[1] + 1;
caml_bytes_unsafe_set(s__0, n[1], 48 + (c % 10 | 0) | 0);
break;
case 1:
caml_bytes_unsafe_set(s__0, n[1], 92);
n[1] += 1;
n[1] = n[1] + 1;
caml_bytes_unsafe_set(s__0, n[1], c);
break;
case 2:
caml_bytes_unsafe_set(s__0, n[1], c);
break
}
n[1] += 1;
n[1] = n[1] + 1;
at_ = i + 1 | 0;
if (as_ !== i) {i = at_;continue;}
break;
Expand Down Expand Up @@ -683,7 +683,7 @@ function of_seq(i) {
function W_(c) {
if (n[1] === caml_ml_bytes_length(buf[1])) {resize(0);}
caml_bytes_set(buf[1], n[1], c);
n[1] += 1;
n[1] = n[1] + 1;
return 0;
}
call2(Stdlib_seq[8], W_, i);
Expand Down
Loading