diff options
36 files changed, 6424 insertions, 276 deletions
@@ -129,8 +129,8 @@ while response_file: for index in range(1, len(sys.argv)): if sys.argv[index][0] == '@': # found one, loop again next time - print >>sys.stderr, 'emcc: using response file: %s' % response_file response_file = sys.argv[index][1:] + print >>sys.stderr, 'emcc: using response file: %s' % response_file if not os.path.exists(response_file): print >>sys.stderr, 'emcc: error: Response file not found: %s' % response_file exit(1) @@ -332,13 +332,11 @@ Options that are modified or new in %s include: output HTML but with suffix .data.compress --minify <on> 0: Do not minify the generated JavaScript's - whitespace (default if closure compiler - will not be run) + whitespace (default in -O0, -O1, or if + -g is used) 1: Minify the generated JavaScript's - whitespace (default if closure compiler - will be run). Note that this by itself - will not minify the code (closure does - that) + whitespace (default in -O2+, assuming + -g is not used) --split <size> Splits the resulting javascript file into pieces to ease debugging. This option only works if @@ -451,6 +449,13 @@ Options that are modified or new in %s include: the bootstrapped relooper. After the cache is cleared, this process will exit. + --save-bc PATH When compiling to JavaScript or HTML, this + option will save a copy of the bitcode to + the specified path. The bitcode will include + all files being linked, including standard + libraries, and after any link-time optimizations + (if any). + The target file, if specified (-o <target>), defines what will be generated: @@ -696,6 +701,8 @@ try: keep_js_debug = False bind = False jcache = False + save_bc = False + if use_cxx: default_cxx_std = '-std=c++03' # Enforce a consistent C++ standard when compiling .cpp files, if user does not specify one on the cmdline. else: @@ -823,6 +830,11 @@ try: print >> sys.stderr, 'emcc: clearing cache' shared.Cache.erase() sys.exit(0) + elif newargs[i] == '--save-bc': + check_bad_eq(newargs[i]) + save_bc = newargs[i+1] + newargs[i] = '' + newargs[i+1] = '' elif newargs[i].startswith(('-I/', '-L/')): if not absolute_warning_shown: print >> sys.stderr, 'emcc: warning: -I or -L of an absolute path encountered. If this is to a local system header/library, it may cause problems (local system files make sense for compiling natively on your system, but not necessarily to JavaScript)' # Of course an absolute path to a non-system-specific library or header is fine, and you can ignore this warning. The danger are system headers that are e.g. x86 specific and nonportable. The emscripten bundled headers are modified to be portable, local system ones are generally not @@ -974,6 +986,7 @@ try: if shared.Settings.CORRECT_OVERFLOWS != 1: print >> sys.stderr, 'emcc: warning: setting CORRECT_OVERFLOWS to 1 for asm.js code generation' shared.Settings.CORRECT_OVERFLOWS = 1 + assert not shared.Settings.PGO, 'cannot run PGO in ASM_JS mode' if shared.Settings.CORRECT_SIGNS >= 2 or shared.Settings.CORRECT_OVERFLOWS >= 2 or shared.Settings.CORRECT_ROUNDINGS >= 2: keep_llvm_debug = True # must keep debug info to do line-by-line operations @@ -983,7 +996,7 @@ try: closure = False if minify_whitespace is None: - minify_whitespace = closure # if closure is run, minify whitespace + minify_whitespace = opt_level >= 2 and not keep_js_debug ## Compile source code to bitcode @@ -1084,10 +1097,14 @@ try: os.path.join('libc', 'stdlib', 'strtod.c'), ]; + prev_cxx = os.environ.get('EMMAKEN_CXX') + if prev_cxx: os.environ['EMMAKEN_CXX'] = '' for src in libc_files: o = in_temp(os.path.basename(src) + '.o') execute([shared.PYTHON, shared.EMCC, shared.path_from_root('system', 'lib', src), '-o', o], stdout=stdout, stderr=stderr) o_s.append(o) + if prev_cxx: os.environ['EMMAKEN_CXX'] = prev_cxx + shared.Building.link(o_s, in_temp('libc.bc')) return in_temp('libc.bc') @@ -1235,6 +1252,9 @@ try: shared.Building.llvm_opt(in_temp(target_basename + '.bc'), link_opts) if DEBUG: save_intermediate('linktime', 'bc') + if save_bc: + shutil.copyfile(final, save_bc) + # Prepare .ll for Emscripten if not LEAVE_INPUTS_RAW: final = shared.Building.llvm_dis(final, final + '.ll') @@ -1347,14 +1367,15 @@ try: if DEBUG: print >> sys.stderr, 'emcc: running closure' final = shared.Building.closure_compiler(final) if DEBUG: save_intermediate('closure') - elif shared.Settings.RELOOP and not closure and not keep_js_debug: - # do this if closure is not enabled (it gives similar speedups), and we do not need to keep debug info around - js_optimizer_queue += ['registerize'] if opt_level >= 1: if DEBUG: print >> sys.stderr, 'emcc: running post-closure post-opts' js_optimizer_queue += ['simplifyExpressionsPost'] + if not closure and shared.Settings.RELOOP and not keep_js_debug: + # do this if closure is not enabled (it gives similar speedups), and we do not need to keep debug info around + js_optimizer_queue += ['registerize'] + if minify_whitespace: js_optimizer_queue += ['compress'] @@ -1362,11 +1383,10 @@ try: flush_js_optimizer_queue() - if not minify_whitespace: - # Remove some trivial whitespace - src = open(final).read() - src = re.sub(r'\n+[ \n]*\n+', '\n', src) - open(final, 'w').write(src) + # Remove some trivial whitespace # TODO: do not run when compress has already been done on all parts of the code + src = open(final).read() + src = re.sub(r'\n+[ \n]*\n+', '\n', src) + open(final, 'w').write(src) # If we were asked to also generate HTML, do that if final_suffix == 'html': diff --git a/emscripten.py b/emscripten.py index b698654b..34a5b20f 100755 --- a/emscripten.py +++ b/emscripten.py @@ -33,7 +33,7 @@ NUM_CHUNKS_PER_CORE = 1.25 MIN_CHUNK_SIZE = 1024*1024 MAX_CHUNK_SIZE = float(os.environ.get('EMSCRIPT_MAX_CHUNK_SIZE') or 'inf') # configuring this is just for debugging purposes -def process_funcs((i, funcs, meta, settings_file, compiler, forwarded_file, libraries, compiler_engine, temp_files)): +def process_funcs((i, funcs, meta, settings_file, compiler, forwarded_file, libraries, compiler_engine, temp_files, DEBUG)): ll = ''.join(funcs) + '\n' + meta funcs_file = temp_files.get('.func_%d.ll' % i).name open(funcs_file, 'w').write(ll) @@ -44,6 +44,7 @@ def process_funcs((i, funcs, meta, settings_file, compiler, forwarded_file, libr stdout=subprocess.PIPE, cwd=path_from_root('src')) tempfiles.try_delete(funcs_file) + if DEBUG: print >> sys.stderr, '.' return out def emscript(infile, settings, outfile, libraries=[], compiler_engine=None, @@ -213,7 +214,7 @@ def emscript(infile, settings, outfile, libraries=[], compiler_engine=None, if DEBUG: print >> sys.stderr, ' emscript: phase 2 working on %d chunks %s (intended chunk size: %.2f MB, meta: %.2f MB, forwarded: %.2f MB, total: %.2f MB)' % (len(chunks), ('using %d cores' % cores) if len(chunks) > 1 else '', chunk_size/(1024*1024.), len(meta)/(1024*1024.), len(forwarded_data)/(1024*1024.), total_ll_size/(1024*1024.)) commands = [ - (i, chunk, meta, settings_file, compiler, forwarded_file, libraries, compiler_engine, temp_files) + (i, chunk, meta, settings_file, compiler, forwarded_file, libraries, compiler_engine, temp_files, DEBUG) for i, chunk in enumerate(chunks) ] @@ -332,9 +333,9 @@ def emscript(infile, settings, outfile, libraries=[], compiler_engine=None, params = ','.join(['p%d' % p for p in range(len(sig)-1)]) coercions = ';'.join(['p%d = %sp%d%s' % (p, '+' if sig[p+1] != 'i' else '', p, '' if sig[p+1] != 'i' else '|0') for p in range(len(sig)-1)]) + ';' ret = '' if sig[0] == 'v' else ('return %s0' % ('+' if sig[0] != 'i' else '')) - return ('function %s(%s) { %s abort(%d); %s };' % (bad, params, coercions, i, ret), raw.replace('[0,', '[' + bad + ',').replace(',0,', ',' + bad + ',').replace(',0,', ',' + bad + ',').replace(',0]', ',' + bad + ']').replace(',0]', ',' + bad + ']').replace(',0\n', ',' + bad + '\n')) + return ('function %s(%s) { %s abort(%d); %s }' % (bad, params, coercions, i, ret), raw.replace('[0,', '[' + bad + ',').replace(',0,', ',' + bad + ',').replace(',0,', ',' + bad + ',').replace(',0]', ',' + bad + ']').replace(',0]', ',' + bad + ']').replace(',0\n', ',' + bad + '\n')) infos = [make_table(sig, raw) for sig, raw in last_forwarded_json['Functions']['tables'].iteritems()] - function_tables_defs = '\n'.join([info[0] for info in infos] + [info[1] for info in infos]) + function_tables_defs = '\n'.join([info[0] for info in infos]) + '\n// EMSCRIPTEN_END_FUNCS\n' + '\n'.join([info[1] for info in infos]) asm_setup = '' maths = ['Math.' + func for func in ['floor', 'abs', 'sqrt', 'pow', 'cos', 'sin', 'tan', 'acos', 'asin', 'atan', 'atan2', 'exp', 'log', 'ceil', 'imul']] @@ -416,6 +417,7 @@ function asmPrintInt(x, y) { function asmPrintFloat(x, y) { Module.print('float ' + x + ',' + y);// + ' ' + new Error().stack); } +// EMSCRIPTEN_START_ASM var asm = (function(global, env, buffer) { 'use asm'; var HEAP8 = new global.Int8Array(buffer); @@ -432,6 +434,7 @@ var asm = (function(global, env, buffer) { var tempInt = 0, tempBigInt = 0, tempBigIntP = 0, tempBigIntS = 0, tempBigIntR = 0.0, tempBigIntI = 0, tempBigIntD = 0, tempValue = 0, tempDouble = 0.0; ''' + ''.join([''' var tempRet%d = 0;''' % i for i in range(10)]) + '\n' + asm_global_funcs + ''' +// EMSCRIPTEN_START_FUNCS function stackAlloc(size) { size = size|0; var ret = 0; @@ -457,11 +460,12 @@ var asm = (function(global, env, buffer) { tempRet%d = value; } ''' % (i, i) for i in range(10)]) + funcs_js + ''' - %s return %s; -})(%s, %s, buffer); +}) +// EMSCRIPTEN_END_ASM +(%s, %s, buffer); %s; Runtime.stackAlloc = function(size) { return asm.stackAlloc(size) }; Runtime.stackSave = function() { return asm.stackSave() }; @@ -483,6 +487,12 @@ Runtime.stackRestore = function(top) { asm.stackRestore(top) }; else: function_tables_defs = '\n'.join([table for table in last_forwarded_json['Functions']['tables'].itervalues()]) outfile.write(function_tables_defs) + funcs_js = ''' +// EMSCRIPTEN_START_FUNCS +''' + funcs_js + ''' +// EMSCRIPTEN_END_FUNCS +''' + outfile.write(blockaddrsize(indexize(funcs_js))) funcs_js = None diff --git a/src/analyzer.js b/src/analyzer.js index 209e3140..926ac9d3 100644 --- a/src/analyzer.js +++ b/src/analyzer.js @@ -231,9 +231,10 @@ function analyzer(data, sidePass) { } if (isIllegalType(item.valueType) || isIllegalType(item.type)) { isIllegal = true; - } - if ((item.intertype == 'load' || item.intertype == 'store') && isStructType(item.valueType)) { + } else if ((item.intertype == 'load' || item.intertype == 'store') && isStructType(item.valueType)) { isIllegal = true; // storing an entire structure is illegal + } else if (item.intertype == 'mathop' && item.op == 'trunc' && isIllegalType(item.params[1].ident)) { // trunc stores target value in second ident + isIllegal = true; } }); if (!isIllegal) { diff --git a/src/compiler.js b/src/compiler.js index 3047daf1..447d34b7 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -163,6 +163,7 @@ if (SAFE_HEAP >= 2) { EXPORTED_FUNCTIONS = set(EXPORTED_FUNCTIONS); EXPORTED_GLOBALS = set(EXPORTED_GLOBALS); EXCEPTION_CATCHING_WHITELIST = set(EXCEPTION_CATCHING_WHITELIST); +DEAD_FUNCTIONS = set(DEAD_FUNCTIONS); RUNTIME_DEBUG = LIBRARY_DEBUG || GL_DEBUG; diff --git a/src/experimental/simplifyGeneratedFunctionsDetection.diff b/src/experimental/simplifyGeneratedFunctionsDetection.diff new file mode 100644 index 00000000..09e0ebcd --- /dev/null +++ b/src/experimental/simplifyGeneratedFunctionsDetection.diff @@ -0,0 +1,336 @@ +diff --git a/emscripten.py b/emscripten.py +index b698654..a7843c7 100755 +--- a/emscripten.py ++++ b/emscripten.py +@@ -294,8 +294,6 @@ def emscript(infile, settings, outfile, libraries=[], compiler_engine=None, + outfile.write(blockaddrsize(indexize(pre))) + pre = None + +- #if DEBUG: outfile.write('// funcs\n') +- + # forward + forwarded_data = json.dumps(forwarded_json) + forwarded_file = temp_files.get('.2.json').name +@@ -483,9 +481,14 @@ Runtime.stackRestore = function(top) { asm.stackRestore(top) }; + else: + function_tables_defs = '\n'.join([table for table in last_forwarded_json['Functions']['tables'].itervalues()]) + outfile.write(function_tables_defs) ++ ++ outfile.write('// EMSCRIPTEN_START_FUNCS\n') ++ + outfile.write(blockaddrsize(indexize(funcs_js))) + funcs_js = None + ++ outfile.write('// EMSCRIPTEN_END_FUNCS\n') ++ + outfile.write(indexize(post)) + if DEBUG: print >> sys.stderr, ' emscript: phase 3 took %s seconds' % (time.time() - t) + +diff --git a/src/jsifier.js b/src/jsifier.js +index ff58ece..d6aa110 100644 +--- a/src/jsifier.js ++++ b/src/jsifier.js +@@ -1587,12 +1587,6 @@ function JSify(data, functionsOnly, givenFunctions) { + + var shellParts = read(shellFile).split('{{BODY}}'); + print(shellParts[1]); +- // Print out some useful metadata (for additional optimizations later, like the eliminator) +- if (EMIT_GENERATED_FUNCTIONS) { +- print('// EMSCRIPTEN_GENERATED_FUNCTIONS: ' + JSON.stringify(keys(Functions.implementedFunctions).filter(function(func) { +- return IGNORED_FUNCTIONS.indexOf(func.ident) < 0; +- })) + '\n'); +- } + + PassManager.serialize(); + +diff --git a/src/settings.js b/src/settings.js +index 1bfcf92..99c83f4 100644 +--- a/src/settings.js ++++ b/src/settings.js +@@ -336,8 +336,6 @@ var EXPLICIT_ZEXT = 0; // If 1, generate an explicit conversion of zext i1 to i3 + + var NECESSARY_BLOCKADDRS = []; // List of (function, block) for all block addresses that are taken. + +-var EMIT_GENERATED_FUNCTIONS = 0; // whether to emit the list of generated functions, needed for external JS optimization passes +- + // Compiler debugging options + var DEBUG_TAGS_SHOWING = []; + // Some useful items: +diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js +index f2dc516..9fa038c 100644 +--- a/tools/js-optimizer.js ++++ b/tools/js-optimizer.js +@@ -140,16 +140,6 @@ 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 = null; +-function setGeneratedFunctions(metadata) { +- var start = metadata.indexOf(GENERATED_FUNCTIONS_MARKER); +- generatedFunctions = set(eval(metadata.substr(start + GENERATED_FUNCTIONS_MARKER.length))); +-} +-function isGenerated(ident) { +- return ident in generatedFunctions; +-} +- + function srcToAst(src) { + return uglify.parser.parse(src); + } +@@ -212,21 +202,9 @@ function traverse(node, pre, post, stack) { + return result; + } + +-// Only walk through the generated functions +-function traverseGenerated(ast, pre, post, stack) { +- assert(generatedFunctions); +- traverse(ast, function(node) { +- if (node[0] == 'defun' && isGenerated(node[1])) { +- traverse(node, pre, post, stack); +- return null; +- } +- }); +-} +- +-function traverseGeneratedFunctions(ast, callback) { +- assert(generatedFunctions); ++function traverseFunctions(ast, callback) { + traverse(ast, function(node) { +- if (node[0] == 'defun' && isGenerated(node[1])) { ++ if (node[0] == 'defun') { + callback(node); + return null; + } +@@ -418,7 +396,7 @@ function simplifyExpressionsPre(ast) { + var rerun = true; + while (rerun) { + rerun = false; +- traverseGenerated(ast, function process(node, type, stack) { ++ traverse(ast, function process(node, type, stack) { + if (type == 'binary' && node[1] == '|') { + if (node[2][0] == 'num' && node[3][0] == 'num') { + return ['num', node[2][1] | node[3][1]]; +@@ -455,7 +433,7 @@ function simplifyExpressionsPre(ast) { + } + + // &-related optimizations +- traverseGenerated(ast, function(node, type) { ++ traverse(ast, function(node, type) { + if (type == 'binary' && node[1] == '&' && node[3][0] == 'num') { + if (node[2][0] == 'num') return ['num', node[2][1] & node[3][1]]; + var input = node[2]; +@@ -489,7 +467,7 @@ function simplifyExpressionsPre(ast) { + + if (asm) { + // optimize num >> num, in asm we need this here since we do not run optimizeShifts +- traverseGenerated(ast, function(node, type) { ++ traverse(ast, function(node, type) { + if (type == 'binary' && node[1] == '>>' && node[2][0] == 'num' && node[3][0] == 'num') { + node[0] = 'num'; + node[1] = node[2][1] >> node[3][1]; +@@ -505,7 +483,7 @@ function simplifyExpressionsPre(ast) { + var rerun = true; + while (rerun) { + rerun = false; +- traverseGenerated(ast, function(node, type) { ++ traverse(ast, function(node, type) { + if (type == 'binary' && node[1] == '+') { + if (node[2][0] == 'num' && node[3][0] == 'num') { + rerun = true; +@@ -528,7 +506,7 @@ function simplifyExpressionsPre(ast) { + + // if (x == 0) can be if (!x), etc. + function simplifyZeroComp(ast) { +- traverseGenerated(ast, function(node, type) { ++ traverse(ast, function(node, type) { + var binary; + if (type == 'if' && (binary = node[1])[0] == 'binary') { + if ((binary[1] == '!=' || binary[1] == '!==') && binary[3][0] == 'num' && binary[3][1] == 0) { +@@ -554,7 +532,7 @@ function simplifyExpressionsPre(ast) { + // TODO: when shifting a variable, if there are other uses, keep an unshifted version too, to prevent slowdowns? + function optimizeShiftsInternal(ast, conservative) { + var MAX_SHIFTS = 3; +- traverseGeneratedFunctions(ast, function(fun) { ++ traverseFunctions(ast, function(fun) { + var funMore = true; + var funFinished = {}; + while (funMore) { +@@ -999,7 +977,7 @@ function vacuum(ast) { + } break; + } + } +- traverseGeneratedFunctions(ast, function(node) { ++ traverseFunctions(ast, function(node) { + vacuumInternal(node); + simplifyNotComps(node); + }); +@@ -1021,7 +999,7 @@ function getStatements(node) { + // if (condition) { label == x } else .. + // We can hoist the multiple block into the condition, thus removing code and one 'if' check + function hoistMultiples(ast) { +- traverseGeneratedFunctions(ast, function(node) { ++ traverseFunctions(ast, function(node) { + traverse(node, function(node, type) { + var statements = getStatements(node); + if (!statements) return; +@@ -1135,7 +1113,7 @@ function hoistMultiples(ast) { + // if (..) { .. break|continue } else { .. } + // to + // if (..) { .. break|continue } .. +- traverseGenerated(ast, function(container, type) { ++ traverse(ast, function(container, type) { + var statements = getStatements(container); + if (!statements) return; + for (var i = 0; i < statements.length; i++) { +@@ -1168,7 +1146,7 @@ function loopOptimizer(ast) { + function passTwo(ast) { + var neededDos = []; + // Find unneeded labels +- traverseGenerated(ast, function(node, type, stack) { ++ traverse(ast, function(node, type, stack) { + if (type == 'label' && node[2][0] in LOOP) { + // this is a labelled loop. we don't know if it's needed yet. Mark its label for removal for now now. + stack.push(node); +@@ -1212,7 +1190,7 @@ function loopOptimizer(ast) { + // We return whether another pass is necessary + var more = false; + // Remove unneeded labels +- traverseGenerated(ast, function(node, type) { ++ traverse(ast, function(node, type) { + if (type == 'label' && node[1][0] == '+') { + more = true; + var ident = node[1].substr(1); +@@ -1227,13 +1205,13 @@ function loopOptimizer(ast) { + }); + // Remove unneeded one-time loops. We need such loops if (1) they have a label, or (2) they have a direct break so they are in neededDos. + // First, add all labeled loops of this nature to neededDos +- traverseGenerated(ast, function(node, type) { ++ traverse(ast, function(node, type) { + if (type == 'label' && node[2][0] == 'do') { + neededDos.push(node[2]); + } + }); + // Remove unneeded dos, we know who they are now +- traverseGenerated(ast, function(node, type) { ++ traverse(ast, function(node, type) { + if (type == 'do' && neededDos.indexOf(node) < 0) { + assert(jsonCompare(node[1], ['num', 0]), 'Trying to remove a one-time do loop that is not one of our generated ones.;'); + more = true; +@@ -1407,7 +1385,7 @@ function denormalizeAsm(func, data) { + // we still need the eliminator? Closure? And in what order? Perhaps just + // closure simple? + function registerize(ast) { +- traverseGeneratedFunctions(ast, function(fun) { ++ traverseFunctions(ast, function(fun) { + 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) +@@ -1671,7 +1649,7 @@ var ABORTING_ELIMINATOR_SCAN_NODES = set('new', 'object', 'function', 'defun', ' + + function eliminate(ast, memSafe) { + // Find variables that have a single use, and if they can be eliminated, do so +- traverseGeneratedFunctions(ast, function(func, type) { ++ traverseFunctions(ast, function(func, type) { + if (asm) var asmData = normalizeAsm(func); + //printErr('eliminate in ' + func[1]); + +@@ -2218,9 +2196,6 @@ var passes = { + var src = read(arguments_[0]); + var ast = srcToAst(src); + //printErr(JSON.stringify(ast)); throw 1; +-var metadata = src.split('\n').filter(function(line) { return line.indexOf(GENERATED_FUNCTIONS_MARKER) >= 0 })[0]; +-//assert(metadata, 'Must have EMSCRIPTEN_GENERATED_FUNCTIONS metadata'); +-if (metadata) setGeneratedFunctions(metadata); + + arguments_.slice(1).forEach(function(arg) { + passes[arg](ast); +diff --git a/tools/js_optimizer.py b/tools/js_optimizer.py +index 2fd2211..a253afb 100644 +--- a/tools/js_optimizer.py ++++ b/tools/js_optimizer.py +@@ -41,17 +41,17 @@ def run_on_js(filename, passes, js_engine, jcache): + if os.linesep != '\n': + js = js.replace(os.linesep, '\n') # we assume \n in the splitting code + +- # Find suffix +- suffix_marker = '// EMSCRIPTEN_GENERATED_FUNCTIONS' +- suffix_start = js.find(suffix_marker) +- suffix = '' +- if suffix_start >= 0: +- suffix = js[suffix_start:js.find('\n', suffix_start)] + '\n' +- # if there is metadata, we will run only on the generated functions. If there isn't, we will run on everything. +- generated = set(eval(suffix[len(suffix_marker)+1:])) +- +- if not suffix and jcache: +- # JCache cannot be used without metadata, since it might reorder stuff, and that's dangerous since only generated can be reordered ++ # Find markers ++ start_marker = '// EMSCRIPTEN_START_FUNCS\n' ++ end_marker = '// EMSCRIPTEN_END_FUNCS\n' ++ start = js.find(start_marker) ++ end = js.find(end_marker) ++ assert (start >= 0) == (end >= 0), 'must have both markers or neither' ++ have_markers = start >= 0 ++ # if there are markers, we will run only on the generated functions. If there isn't, we will run on everything. ++ ++ if not have_markers and jcache: ++ # JCache cannot be used without markers, since it might reorder stuff, and that's dangerous since only generated can be reordered + # This means jcache does not work after closure compiler runs, for example. But you won't get much benefit from jcache with closure + # anyhow (since closure is likely the longest part of the build). + if DEBUG: print >>sys.stderr, 'js optimizer: no metadata, so disabling jcache' +@@ -59,27 +59,10 @@ def run_on_js(filename, passes, js_engine, jcache): + + # If we process only generated code, find that and save the rest on the side + func_sig = re.compile('( *)function (_[\w$]+)\(') +- if suffix: +- pos = 0 +- gen_start = 0 +- gen_end = 0 +- while 1: +- m = func_sig.search(js, pos) +- if not m: break +- pos = m.end() +- indent = m.group(1) +- ident = m.group(2) +- if ident in generated: +- if not gen_start: +- gen_start = m.start() +- assert gen_start +- gen_end = js.find('\n%s}\n' % indent, m.end()) + (3 + len(indent)) +- assert gen_end > gen_start +- pre = js[:gen_start] +- post = js[gen_end:] +- if 'last' in passes: +- post = post.replace(suffix, '') # no need to write out the metadata - nothing after us needs it +- js = js[gen_start:gen_end] ++ if have_markers: ++ pre = js[:start + len(start_marker)] # includes start marker ++ post = js[end:] # includes end marker ++ js = js[start + len(start_marker):end] + else: + pre = '' + post = '' +@@ -95,7 +78,7 @@ def run_on_js(filename, passes, js_engine, jcache): + if m: + ident = m.group(2) + else: +- if suffix: continue # ignore whitespace ++ if have_markers: continue # ignore whitespace + ident = 'anon_%d' % i + assert ident + funcs.append((ident, func)) +@@ -131,7 +114,6 @@ def run_on_js(filename, passes, js_engine, jcache): + temp_file = temp_files.get('.jsfunc_%d.js' % i).name + f = open(temp_file, 'w') + f.write(chunk) +- f.write(suffix) + f.close() + return temp_file + filenames = [write_chunk(chunks[i], i) for i in range(len(chunks))] +@@ -169,7 +151,6 @@ def run_on_js(filename, passes, js_engine, jcache): + f.write(cached); # TODO: preserve order + f.write('\n') + f.write(post); +- # No need to write suffix: if there was one, it is inside post which exists when suffix is there + f.write('\n') + f.close() + diff --git a/src/intertyper.js b/src/intertyper.js index 2103ecfa..57e3011d 100644 --- a/src/intertyper.js +++ b/src/intertyper.js @@ -122,16 +122,19 @@ function intertyper(data, sidePass, baseLineNums) { SKIP_STACK_IN_SMALL = 0; } - unparsedBundles.push({ - intertype: 'unparsedFunction', - // We need this early, to know basic function info - ident, params, varargs - ident: toNiceIdent(func.ident), - params: func.params, - returnType: func.returnType, - hasVarArgs: func.hasVarArgs, - lineNum: currFunctionLineNum, - lines: currFunctionLines - }); + var ident = toNiceIdent(func.ident); + if (!(ident in DEAD_FUNCTIONS)) { + unparsedBundles.push({ + intertype: 'unparsedFunction', + // We need this early, to know basic function info - ident, params, varargs + ident: ident, + params: func.params, + returnType: func.returnType, + hasVarArgs: func.hasVarArgs, + lineNum: currFunctionLineNum, + lines: currFunctionLines + }); + } currFunctionLines = []; } } diff --git a/src/jsifier.js b/src/jsifier.js index ff58ece2..1662d249 100644< |