aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-01-19 11:53:52 -0800
committerAlon Zakai <alonzakai@gmail.com>2013-01-19 11:53:52 -0800
commitb961c328f28daff5d1824ad4d04e076ab0ac7f34 (patch)
treece8001773dcac0d4cb58108e9751beef4f32c367 /tools
parent35cbbe82edf2173092592244aa342285fbcb5e44 (diff)
emit 5.0 instead of +5 in asm
Diffstat (limited to 'tools')
-rw-r--r--tools/js-optimizer.js27
-rw-r--r--tools/test-js-optimizer-asm-last-output.js27
-rw-r--r--tools/test-js-optimizer-asm-last.js27
3 files changed, 78 insertions, 3 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js
index 69abe23a..8cd357cd 100644
--- a/tools/js-optimizer.js
+++ b/tools/js-optimizer.js
@@ -2149,6 +2149,19 @@ function eliminateMemSafe(ast) {
eliminate(ast, true);
}
+// Change +5 to DOT$ZERO(5). We then textually change 5 to 5.0 (uglify's ast cannot differentiate between 5 and 5.0 directly)
+function prepDotZero(ast) {
+ traverse(ast, function(node, type) {
+ if (type == 'unary-prefix' && node[1] == '+') {
+ if (node[2][0] == 'num') {
+ return ['call', ['name', 'DOT$ZERO'], [['num', node[2][1]]]];
+ } else if (node[2][0] == 'unary-prefix' && node[2][1] == '-' && node[2][2][0] == 'num') {
+ return ['call', ['name', 'DOT$ZERO'], [['num', -node[2][2][1]]]];
+ }
+ }
+ });
+}
+
// Passes table
var compress = false, printMetadata = true, asm = false, last = false;
@@ -2186,15 +2199,23 @@ if (metadata) setGeneratedFunctions(metadata);
arguments_.slice(1).forEach(function(arg) {
passes[arg](ast);
});
+if (asm && last) {
+ prepDotZero(ast);
+}
var js = astToSrc(ast, compress), old;
+if (asm && last) {
+ js = js.replace(/DOT\$ZERO\(([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\)/g, function(m, num) {
+ if (num.indexOf('.') >= 0) return num;
+ var e = num.indexOf('e');
+ if (e < 0) return num + '.0';
+ return num.substr(0, e) + '.0' + num.substr(e);
+ });
+}
// remove unneeded newlines+spaces, and print
do {
old = js;
js = js.replace(/\n *\n/g, '\n');
- if (asm && last) {
- js = js.replace(/ = \+0([,;])/g, function(m, end) { return ' = 0.0' + end }); // asm requires 0.0 in var definitions, not +0
- }
} while (js != old);
print(js);
print('\n');
diff --git a/tools/test-js-optimizer-asm-last-output.js b/tools/test-js-optimizer-asm-last-output.js
new file mode 100644
index 00000000..5a87f3c4
--- /dev/null
+++ b/tools/test-js-optimizer-asm-last-output.js
@@ -0,0 +1,27 @@
+function finall(x) {
+ x = +x;
+ var a = 5.0;
+ a = +x;
+ a = 17;
+ a = 44.0;
+ a = 44.0;
+ a = 44.9;
+ a = 1278.0e3;
+ a = 12.0e10;
+ a = -x;
+ a = -17;
+ a = -44;
+ a = -44;
+ a = -44.9;
+ a = -1278e3;
+ a = -12e10;
+ a = +-x;
+ a = -17.0;
+ a = -44.0;
+ a = -44.0;
+ a = -44.9;
+ a = -1278.0e3;
+ a = -12.0e10;
+ return 12.0e10;
+}
+
diff --git a/tools/test-js-optimizer-asm-last.js b/tools/test-js-optimizer-asm-last.js
new file mode 100644
index 00000000..2b025d28
--- /dev/null
+++ b/tools/test-js-optimizer-asm-last.js
@@ -0,0 +1,27 @@
+function finall(x) {
+ x = +x;
+ var a = +5;
+ a = +x;
+ a = 17;
+ a = +44;
+ a = +44.0;
+ a = +44.9;
+ a = +12.78e5;
+ a = +12e10;
+ a = -x;
+ a = -17;
+ a = -44;
+ a = -44.0;
+ a = -44.9;
+ a = -12.78e5;
+ a = -12e10;
+ a = +-x;
+ a = +-17;
+ a = +-44;
+ a = +-44.0;
+ a = +-44.9;
+ a = +-12.78e5;
+ a = +-12e10;
+ return +12e10;
+}
+// EMSCRIPTEN_GENERATED_FUNCTIONS: ["finall"]