aboutsummaryrefslogtreecommitdiff
path: root/tools/js-optimizer.js
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2011-11-23 13:12:17 -0800
committerAlon Zakai <alonzakai@gmail.com>2011-11-23 13:12:17 -0800
commit561741c27e6910d3e0cec04a2021a4b157b611f1 (patch)
tree73283d9bbf0a60169cba82d66a4318ed3ff5735d /tools/js-optimizer.js
parentd29567b09b3b50f8be74d32e7ccbd99002bccc39 (diff)
simplify !(<) etc. in js optimizer
Diffstat (limited to 'tools/js-optimizer.js')
-rw-r--r--tools/js-optimizer.js44
1 files changed, 43 insertions, 1 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js
index fcf0bbc3..7f613e5f 100644
--- a/tools/js-optimizer.js
+++ b/tools/js-optimizer.js
@@ -32,6 +32,12 @@ var UNDEFINED_NODE = ['unary-prefix', 'void', ['num', 0]];
var TRUE_NODE = ['unary-prefix', '!', ['num', 0]];
var FALSE_NODE = ['unary-prefix', '!', ['num', 1]];
+var GENERATED_FUNCTIONS_MARKER = '// EMSCRIPTEN_GENERATED_FUNCTIONS:';
+var generatedFunctions = {};
+function isGenerated(ident) {
+ return ident in generatedFunctions;
+}
+
// Traverses a JavaScript syntax tree rooted at the given node calling the given
// callback for each node.
// @arg node: The root of the AST.
@@ -219,24 +225,60 @@ function removeUnneededLabelSettings(ast) {
});
}
+// We often have branchings that are simplified so one end vanishes, and
+// we then get
+// if (!(x < 5))
+// or such. Simplifying these saves space and time.
+function simplifyNotComps(ast) {
+ traverse(ast, function(node, type) {
+ if (type == 'unary-prefix' && node[1] == '!' && node[2][0] == 'binary') {
+ if (node[2][1] == '<') {
+ return ['binary', '>=', node[2][2], node[2][3]];
+ } else if (node[2][1] == '>') {
+ return ['binary', '<=', node[2][2], node[2][3]];
+ } else if (node[2][1] == '==') {
+ return ['binary', '!=', node[2][2], node[2][3]];
+ } else if (node[2][1] == '!=') {
+ return ['binary', '==', node[2][2], node[2][3]];
+ } else if (node[2][1] == '===') {
+ return ['binary', '!==', node[2][2], node[2][3]];
+ } else if (node[2][1] == '!==') {
+ return ['binary', '===', node[2][2], node[2][3]];
+ }
+ }
+ });
+}
+
// Passes table
var passes = {
unGlobalize: unGlobalize,
removeAssignsToUndefined: removeAssignsToUndefined,
- //removeUnneededLabelSettings: removeUnneededLabelSettings
+ //removeUnneededLabelSettings: removeUnneededLabelSettings,
+ simplifyNotComps: simplifyNotComps
};
// Main
var src = fs.readFileSync('/dev/stdin').toString();
var ast = uglify.parser.parse(src);
+//print(JSON.stringify(ast));
var metadata = src.split('\n').filter(function(line) { return line.indexOf('EMSCRIPTEN_GENERATED_FUNCTIONS') >= 0 })[0];
//assert(metadata, 'Must have EMSCRIPTEN_GENERATED_FUNCTIONS metadata');
+//generatedFunctions = set(eval(metadata.replace(GENERATED_FUNCTIONS_MARKER, '')))
arguments.forEach(function(arg) {
passes[arg](ast);
});
+/* TODO: run only on generated functions (but, some passes look at globals...)
+arguments.forEach(function(arg) {
+ ast[1].forEach(function(node, i) {
+ if (node[0] == 'defun' && isGenerated(node[1])) {
+ passes[arg](node);
+ }
+ });
+});
+*/
print(uglify.uglify.gen_code(ast, {
ascii_only: true,