diff options
-rw-r--r-- | tools/js-optimizer.js | 14 | ||||
-rw-r--r-- | tools/js_optimizer.py | 30 |
2 files changed, 17 insertions, 27 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index 092c9f35..7b774dee 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -1397,14 +1397,26 @@ function denormalizeAsm(func, data) { //printErr('denormalized \n\n' + astToSrc(func) + '\n\n'); } -// Very simple 'registerization', coalescing of variables into a smaller number. +// Very simple 'registerization', coalescing of variables into a smaller number, +// as part of minification. Globals-level minification began in a previous pass, +// we receive minifierInfo which tells us how to rename globals. +// // We do not optimize when there are switches, so this pass only makes sense with // relooping. // TODO: Consider how this fits in with the rest of the optimization toolchain. Do // we still need the eliminator? Closure? And in what order? Perhaps just // closure simple? function registerize(ast) { + assert(minifierInfo); + traverseGeneratedFunctions(ast, function(fun) { + // First, fix globals. Note that we know/assume that locals cannot shadow globals. + traverse(fun, function(node, type) { + if (type == 'name') { + var minified = minifierInfo.globals[node[1]]; + if (minified) node[1] = minified; + } + }); if (asm) var asmData = normalizeAsm(fun); // Add parameters as a first (fake) var (with assignment), so they get taken into consideration var params = {}; // note: params are special, they can never share a register between them (see later) diff --git a/tools/js_optimizer.py b/tools/js_optimizer.py index cc7e8104..b5213ac3 100644 --- a/tools/js_optimizer.py +++ b/tools/js_optimizer.py @@ -159,11 +159,11 @@ EMSCRIPTEN_FUNCS(); js = js[start_funcs + len(start_funcs_marker):end_funcs] minifier = Minifier(js, js_engine) - minified_asm_shell = minifier.minify_shell(asm_shell) - - asm_shell_pre, asm_shell_post = minified_asm_shell.split('EMSCRIPTEN_FUNCS();'); + asm_shell_pre, asm_shell_post = minifier.minify_shell(asm_shell).split('EMSCRIPTEN_FUNCS();'); pre += asm_shell_pre post = asm_shell_post + post + + minify_info = minifier.serialize() else: pre = '' post = '' @@ -210,35 +210,13 @@ EMSCRIPTEN_FUNCS(); else: cached_outputs = [] - if 'zzzregisterize' in passes: - assert suffix, 'need generated info for registerize' - # Find basic globals (initial asm.js imports, etc.) - asm_marker = pre.find("'use asm'") - if asm_marker < 0: asm_marker = pre.find('"use asm"') - assert asm_marker > 0 - asm_funcs = pre.find('function ', asm_marker) - assert asm_funcs > asm_marker - minifier = Minifier() - new_vars = '' - for vardef in re.findall(r'var [^;]+;', pre[asm_marker:asm_funcs]): - vs = vardef[4:-1].split(',') - for v in vs: - name, value = map(lambda x: x.strip(), v.split('=')) - new_vars += 'var ' + minifier.add_glob(name) + '=' + value + ';' - for g in generated: - minifier.add_glob(g) - minify_info = minifier.serialize() - minifier = None - pre = pre[:asm_marker+11] + new_vars + pre[asm_funcs:] - print >> sys.stderr, 'm', minify_info - if len(chunks) > 0: def write_chunk(chunk, i): temp_file = temp_files.get('.jsfunc_%d.js' % i).name f = open(temp_file, 'w') f.write(chunk) f.write(suffix_marker) - if 'zzzregisterize' in passes: + if asm_registerize: f.write('\n') f.write('// MINIFY_INFO:' + minify_info) f.close() |