diff options
-rw-r--r-- | tests/runner.py | 8 | ||||
-rw-r--r-- | tools/eliminator/eliminator.coffee | 1 | ||||
-rw-r--r-- | tools/js-optimizer.js | 44 |
3 files changed, 48 insertions, 5 deletions
diff --git a/tests/runner.py b/tests/runner.py index c1743e39..8c098207 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -4517,7 +4517,7 @@ else: #JS_ENGINE = V8_ENGINE Building.COMPILER_TEST_OPTS = [] - POST_OPTIMIZATIONS = ['eliminator', 'closure', 'js-optimizer'] + POST_OPTIMIZATIONS = ['eliminator', 'closure', ['js-optimizer', 'unGlobalize', 'removeAssignsToUndefined']] TEST_REPS = 10 TOTAL_TESTS = 6 @@ -4578,6 +4578,10 @@ else: final_filename = filename + '.o.js' for post in POST_OPTIMIZATIONS: + post_args = [] + if type(post) == list: + post_args = post[1:] + post = post[0] if post == 'closure': # Something like this (adjust memory as needed): # java -Xmx1024m -jar CLOSURE_COMPILER --compilation_level ADVANCED_OPTIMIZATIONS --variable_map_output_file src.cpp.o.js.vars --js src.cpp.o.js --js_output_file src.cpp.o.cc.js @@ -4601,7 +4605,7 @@ else: f.close() elif post == 'js-optimizer': input = open(final_filename, 'r').read() - output = Popen([NODE_JS, JS_OPTIMIZER], stdin=PIPE, stdout=PIPE).communicate(input)[0] + output = Popen([NODE_JS, JS_OPTIMIZER] + post_args, stdin=PIPE, stdout=PIPE).communicate(input)[0] final_filename += '.jo.js' f = open(final_filename, 'w') f.write(output) diff --git a/tools/eliminator/eliminator.coffee b/tools/eliminator/eliminator.coffee index 52ba50c6..874ae8d9 100644 --- a/tools/eliminator/eliminator.coffee +++ b/tools/eliminator/eliminator.coffee @@ -397,6 +397,7 @@ main = -> for node in ast[1] process.stdout.write uglify.uglify.gen_code node, GEN_OPTIONS process.stdout.write '\n' + process.stdout.write generatedFunctionsLine + '\n' return undefined diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index 5aeee636..78f321a4 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -17,6 +17,7 @@ function read(filename) { if (filename[0] != '/') filename = __dirname.split('/').slice(0, -1).join('/') + '/src/' + filename; return fs.readFileSync(filename).toString(); } +var arguments = process.argv.slice(2); // Load some modules @@ -185,18 +186,55 @@ function removeAssignsToUndefined(ast) { } } +// XXX This is an invalid optimization +// We sometimes leave some settings to __label__ that are not needed, if later in +// the relooper we realize that we have a single entry, so no checks on __label__ +// are actually necessary. It's easy to clean those up now. +function removeUnneededLabelSettings(ast) { + traverse(ast, function(node, type) { + if (type == 'defun') { // all of our compiled code is in defun nodes + // Find all checks + var checked = {}; + traverse(node, function(node, type) { + if (type == 'binary' && node[1] == '==' && node[2][0] == 'name' && node[2][1] == '__label__') { + assert(node[3][0] == 'num'); + checked[node[3][1]] = 1; + } + }); + // Remove unneeded sets + traverse(node, function(node, type) { + if (type == 'assign' && node[2][0] == 'name' && node[2][1] == '__label__') { + assert(node[3][0] == 'num'); + if (!(node[3][1] in checked)) return emptyNode(); + } + }); + } + }); +} + +// Passes table + +var passes = { + unGlobalize: unGlobalize, + removeAssignsToUndefined: removeAssignsToUndefined, + //removeUnneededLabelSettings: removeUnneededLabelSettings +}; + // Main var src = fs.readFileSync('/dev/stdin').toString(); - var ast = uglify.parser.parse(src); +var metadata = src.split('\n').filter(function(line) { return line.indexOf('EMSCRIPTEN_GENERATED_FUNCTIONS') >= 0 })[0]; +//assert(metadata, 'Must have EMSCRIPTEN_GENERATED_FUNCTIONS metadata'); -unGlobalize(ast); -removeAssignsToUndefined(ast); +arguments.forEach(function(arg) { + passes[arg](ast); +}); print(uglify.uglify.gen_code(ast, { ascii_only: true, beautify: true, indent_level: 2 })); +if (metadata) print(metadata + '\n'); |