diff options
-rw-r--r-- | AUTHORS | 3 | ||||
-rwxr-xr-x | emcc | 63 | ||||
-rwxr-xr-x | emscripten.py | 199 | ||||
-rw-r--r-- | src/analyzer.js | 8 | ||||
-rw-r--r-- | src/compiler.js | 28 | ||||
-rw-r--r-- | src/jsifier.js | 118 | ||||
-rw-r--r-- | src/library.js | 6 | ||||
-rw-r--r-- | src/long.js | 2804 | ||||
-rw-r--r-- | src/modules.js | 63 | ||||
-rw-r--r-- | src/parseTools.js | 10 | ||||
-rw-r--r-- | src/preamble.js | 4 | ||||
-rw-r--r-- | src/settings.js | 2 | ||||
-rw-r--r-- | tests/cases/ptrtoint_blockaddr.ll | 2 | ||||
-rwxr-xr-x | tests/runner.py | 110 | ||||
-rw-r--r-- | tools/js_optimizer.py | 2 | ||||
-rw-r--r-- | tools/shared.py | 23 |
16 files changed, 1877 insertions, 1568 deletions
@@ -39,4 +39,5 @@ a license to everyone to use it as detailed in LICENSE.) * Janus Troelsen <janus.troelsen@stud.tu-darmstadt.de> * Lars Schneider <lars.schneider@autodesk.com> (copyright owned by Autodesk, Inc.) * Joel Martin <github@martintribe.org> -* Manuel Wellmann <manuel.wellmann@autodesk.com> (copyright owned by Autodesk, Inc.)
\ No newline at end of file +* Manuel Wellmann <manuel.wellmann@autodesk.com> (copyright owned by Autodesk, Inc.) + @@ -90,7 +90,7 @@ LLVM_OPT_LEVEL = { 3: 3, } -DEBUG = os.environ.get('EMCC_DEBUG') +DEBUG = int(os.environ.get('EMCC_DEBUG') or 0) TEMP_DIR = os.environ.get('EMCC_TEMP_DIR') LEAVE_INPUTS_RAW = os.environ.get('EMCC_LEAVE_INPUTS_RAW') # Do not compile .ll files into .bc, just compile them with emscripten directly # Not recommended, this is mainly for the test runner, or if you have some other @@ -173,6 +173,11 @@ Options that are modified or new in %s include: (without the external "s in either of those, you would get an error) + -g Use debug info. Note that you need this during + the last compilation phase from bitcode to + JavaScript, or else we will remove it by + default in -O1 and above. + --typed-arrays <mode> 0: No typed arrays 1: Parallel typed arrays 2: Shared (C-like) typed arrays (default) @@ -461,7 +466,7 @@ CC = shared.to_cc(CXX) if os.environ.get('EMMAKEN_CXX'): CC = CXX -CC_ADDITIONAL_ARGS = shared.COMPILER_OPTS # + ['-g']? +CC_ADDITIONAL_ARGS = shared.COMPILER_OPTS EMMAKEN_CFLAGS = os.environ.get('EMMAKEN_CFLAGS') if EMMAKEN_CFLAGS: CC_ADDITIONAL_ARGS += shlex.split(EMMAKEN_CFLAGS) @@ -533,7 +538,7 @@ if TEMP_DIR: shutil.rmtree(temp_dir) # clear it os.makedirs(temp_dir) else: - temp_root = os.path.join(shared.TEMP_DIR, 'emcc') + temp_root = shared.TEMP_DIR if not os.path.exists(temp_root): os.makedirs(temp_root) temp_dir = tempfile.mkdtemp(dir=temp_root) @@ -564,6 +569,7 @@ try: shell_path = shared.path_from_root('src', 'shell.html') js_libraries = [] remove_duplicates = False + keep_debug = False bind = False def check_bad_eq(arg): @@ -622,7 +628,9 @@ try: check_bad_eq(newargs[i]) split_js_file = int(newargs[i+1]) newargs[i] = '' - newargs[i+1] = '' + newargs[i+1] = '' + elif newargs[i] == '-g': + keep_debug = True elif newargs[i] == '--bind': bind = True newargs[i] = '-std=c++11' # Force C++11 for embind code @@ -683,6 +691,7 @@ try: if closure is None: closure = 1 if opt_level >= 2 else 0 if minify_whitespace is None: minify_whitespace = closure # if closure is run, minify whitespace + if opt_level <= 0: keep_debug = True # always keep debug in -O0 if closure: assert os.path.exists(shared.CLOSURE_COMPILER), 'emcc: fatal: Closure compiler (%s) does not exist' % shared.CLOSURE_COMPILER @@ -810,6 +819,10 @@ try: key, value = change.split('=') exec('shared.Settings.' + key + ' = ' + value) + # Apply effects from settings + if shared.Settings.CORRECT_SIGNS >= 2 or shared.Settings.CORRECT_OVERFLOWS >= 2 or shared.Settings.CORRECT_ROUNDINGS >= 2: + keep_debug = True # must keep debug info to do line-by-line operations + ## Compile source code to bitcode if DEBUG: print >> sys.stderr, 'emcc: compiling to bitcode' @@ -994,24 +1007,24 @@ try: if not LEAVE_INPUTS_RAW: save_intermediate('basebc', 'bc') # Optimize, if asked to - if llvm_opts > 0 and not LEAVE_INPUTS_RAW: - if DEBUG: print >> sys.stderr, 'emcc: LLVM -O%d' % llvm_opts - shared.Building.llvm_opt(in_temp(target_basename + '.bc'), llvm_opts) - if DEBUG: save_intermediate('opt', 'bc') - # Do LTO in a separate pass to work around LLVM bug XXX (see failure e.g. in cubescript) - if llvm_lto and shared.Building.can_use_unsafe_opts() and shared.Building.can_build_standalone(): - lto_opts = [] - if not shared.Building.can_inline(): lto_opts.append('-disable-inlining') - lto_opts.append('-std-link-opts') - if DEBUG: print >> sys.stderr, 'emcc: LLVM LTO:', lto_opts - shared.Building.llvm_opt(in_temp(target_basename + '.bc'), lto_opts) - if DEBUG: save_intermediate('lto', 'bc') - else: - # If possible, remove dead functions etc., this potentially saves a lot in the size of the generated code (and the time to compile it) - if not LEAVE_INPUTS_RAW and shared.Building.can_build_standalone(): - if DEBUG: print >> sys.stderr, 'emcc: LLVM dead globals elimination' - shared.Building.llvm_opt(in_temp(target_basename + '.bc'), ['-internalize', '-globaldce']) - if DEBUG: save_intermediate('dce', 'bc') + if not LEAVE_INPUTS_RAW: + link_opts = [] if keep_debug else ['-strip-debug'] + if llvm_opts > 0: + if DEBUG: print >> sys.stderr, 'emcc: LLVM -O%d' % llvm_opts + shared.Building.llvm_opt(in_temp(target_basename + '.bc'), llvm_opts) + if DEBUG: save_intermediate('opt', 'bc') + # Do LTO in a separate pass to work around LLVM bug XXX (see failure e.g. in cubescript) + if shared.Building.can_build_standalone(): + if llvm_lto and shared.Building.can_use_unsafe_opts(): + if not shared.Building.can_inline(): link_opts.append('-disable-inlining') + link_opts.append('-std-link-opts') + else: + # At least remove dead functions etc., this potentially saves a lot in the size of the generated code (and the time to compile it) + link_opts += ['-internalize', '-globaldce'] + if link_opts: + if DEBUG: print >> sys.stderr, 'emcc: LLVM linktime:', link_opts + shared.Building.llvm_opt(in_temp(target_basename + '.bc'), link_opts) + if DEBUG: save_intermediate('linktime', 'bc') # Prepare .ll for Emscripten if not LEAVE_INPUTS_RAW: @@ -1083,8 +1096,10 @@ try: def flush_js_optimizer_queue(): global final, js_optimizer_queue if len(js_optimizer_queue) > 0: - if not DEBUG: + if DEBUG < 2: + if DEBUG: print >> sys.stderr, 'emcc: applying js optimization passes:', js_optimizer_queue final = shared.Building.js_optimizer(final, js_optimizer_queue) + if DEBUG: save_intermediate('js_opts') else: for name in js_optimizer_queue: print >> sys.stderr, 'emcc: applying js optimization pass:', name @@ -1095,7 +1110,7 @@ try: if opt_level >= 1: if DEBUG: print >> sys.stderr, 'emcc: running pre-closure post-opts' - if DEBUG: + if DEBUG >= 2: # Clean up the syntax a bit final = shared.Building.js_optimizer(final, []) if DEBUG: save_intermediate('pretty') diff --git a/emscripten.py b/emscripten.py index 800ca8d7..5c74a319 100755 --- a/emscripten.py +++ b/emscripten.py @@ -9,12 +9,7 @@ header files (so that the JS compiler can see the constants in those headers, for the libc implementation in JS). ''' -import json -import optparse -import os -import subprocess -import re -import sys +import os, sys, json, optparse, subprocess, re, time, multiprocessing if not os.environ.get('EMSCRIPTEN_SUPPRESS_USAGE_WARNING'): print >> sys.stderr, ''' @@ -25,6 +20,8 @@ WARNING: You should normally never use this! Use emcc instead. from tools import shared +DEBUG = os.environ.get('EMCC_DEBUG') + __rootpath__ = os.path.abspath(os.path.dirname(__file__)) def path_from_root(*pathelems): """Returns the absolute path for which the given path elements are @@ -34,22 +31,192 @@ def path_from_root(*pathelems): temp_files = shared.TempFiles() +compiler_engine = None + +def scan(ll, settings): + # blockaddress(@main, %23) + blockaddrs = [] + for blockaddr in re.findall('blockaddress\([^)]*\)', ll): + b = blockaddr.split('(')[1][:-1].split(', ') + blockaddrs.append(b) + if len(blockaddrs) > 0: + settings['NECESSARY_BLOCKADDRS'] = blockaddrs + +NUM_CHUNKS_PER_CORE = 4 +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(args): + i, ll, settings_file, compiler, forwarded_file, libraries = args + funcs_file = temp_files.get('.func_%d.ll' % i).name + open(funcs_file, 'w').write(ll) + out = shared.run_js(compiler, compiler_engine, [settings_file, funcs_file, 'funcs', forwarded_file] + libraries, stdout=subprocess.PIPE, cwd=path_from_root('src')) + return out.split('//FORWARDED_DATA:') def emscript(infile, settings, outfile, libraries=[]): - """Runs the emscripten LLVM-to-JS compiler. + """Runs the emscripten LLVM-to-JS compiler. We parallelize as much as possible Args: infile: The path to the input LLVM assembly file. - settings: JSON-formatted string of settings that overrides the values + settings: JSON-formatted settings that override the values defined in src/settings.js. outfile: The file where the output is written. """ - settings_file = temp_files.get('.txt').name # Save settings to a file to work around v8 issue 1579 + + compiler = path_from_root('src', 'compiler.js') + + # Parallelization: We run 3 phases: + # 1 aka 'pre' : Process types and metadata and so forth, and generate the preamble. + # 2 aka 'funcs': Process functions. We can parallelize this, working on each function independently. + # 3 aka 'post' : Process globals, generate postamble and finishing touches. + + if DEBUG: print >> sys.stderr, 'emscript: ll=>js' + + # Pre-scan ll and alter settings as necessary + if DEBUG: t = time.time() + ll = open(infile).read() + scan(ll, settings) + total_ll_size = len(ll) + ll = None # allow collection + if DEBUG: print >> sys.stderr, ' emscript: scan took %s seconds' % (time.time() - t) + + # Split input into the relevant parts for each phase + pre = [] + funcs = [] # split up functions here, for parallelism later + meta = [] # needed by each function XXX + post = [] + + if DEBUG: t = time.time() + in_func = False + ll_lines = open(infile).readlines() + for line in ll_lines: + if in_func: + funcs[-1].append(line) + if line.startswith('}'): + in_func = False + funcs[-1] = ''.join(funcs[-1]) + pre.append(line) # pre needs it to, so we know about all implemented functions + else: + if line.startswith('define '): + in_func = True + funcs.append([line]) + pre.append(line) # pre needs it to, so we know about all implemented functions + elif line.find(' = type { ') > 0: + pre.append(line) # type + elif line.startswith('!'): + meta.append(line) # metadata + else: + post.append(line) # global + pre.append(line) # pre needs it to, so we know about globals in pre and funcs + ll_lines = None + meta = ''.join(meta) + if DEBUG and len(meta) > 1024*1024: print >> sys.stderr, 'emscript warning: large amounts of metadata, will slow things down' + if DEBUG: print >> sys.stderr, ' emscript: split took %s seconds' % (time.time() - t) + + #if DEBUG: + # print >> sys.stderr, '========= pre ================\n' + # print >> sys.stderr, ''.join(pre) + # print >> sys.stderr, '========== funcs ===============\n' + # for func in funcs: + # print >> sys.stderr, '\n// ===\n\n', ''.join(func) + # print >> sys.stderr, '========== post ==============\n' + # print >> sys.stderr, ''.join(post) + # print >> sys.stderr, '=========================\n' + + # Save settings to a file to work around v8 issue 1579 + settings_file = temp_files.get('.txt').name s = open(settings_file, 'w') - s.write(settings) + s.write(json.dumps(settings)) s.close() - compiler = path_from_root('src', 'compiler.js') - shared.run_js(compiler, shared.COMPILER_ENGINE, [settings_file, infile] + libraries, stdout=outfile, cwd=path_from_root('src')) + + # Phase 1 - pre + if DEBUG: t = time.time() + pre_file = temp_files.get('.pre.ll').name + open(pre_file, 'w').write(''.join(pre) + '\n' + meta) + out = shared.run_js(compiler, shared.COMPILER_ENGINE, [settings_file, pre_file, 'pre'] + libraries, stdout=subprocess.PIPE, cwd=path_from_root('src')) + js, forwarded_data = out.split('//FORWARDED_DATA:') + #print 'js', js + #print >> sys.stderr, 'FORWARDED_DATA 1:', forwarded_data, type(forwarded_data) + forwarded_file = temp_files.get('.json').name + open(forwarded_file, 'w').write(forwarded_data) + if DEBUG: print >> sys.stderr, ' emscript: phase 1 took %s seconds' % (time.time() - t) + + # Phase 2 - func + + cores = multiprocessing.cpu_count() + assert cores >= 1 + if cores > 1: + intended_num_chunks = cores * NUM_CHUNKS_PER_CORE + chunk_size = max(MIN_CHUNK_SIZE, total_ll_size / intended_num_chunks) + chunk_size += len(forwarded_data)/3 + 3*len(meta) # keep ratio of lots of function code to meta (expensive to process) and forwarded (cheap) + chunk_size = min(MAX_CHUNK_SIZE, chunk_size) + else: + chunk_size = MAX_CHUNK_SIZE # if 1 core, just use the max chunk size + + if DEBUG: t = time.time() + forwarded_json = json.loads(forwarded_data) + indexed_functions = set() + chunks = [] # bundles of functions + curr = '' + for i in range(len(funcs)): + func = funcs[i] + if len(curr) + len(func) < chunk_size: + curr += func + else: + chunks.append(curr) + curr = func + if curr: + chunks.append(curr) + curr = '' + if cores == 1 and total_ll_size < MAX_CHUNK_SIZE: assert len(chunks) == 1, 'no point in splitting up without multiple cores' + if DEBUG: print >> sys.stderr, ' emscript: phase 2 working on %d chunks %s (intended chunk size: %.2f MB, meta: %.2f MB, forwarded: %.2f)' % (len(chunks), ('using %d cores' % cores) if len(chunks) > 1 else '', chunk_size/(1024*1024.), len(meta)/(1024*1024.), len(forwarded_data)/(1024*1024.)) + + commands = [(i, chunk + '\n' + meta, settings_file, compiler, forwarded_file, libraries) for chunk in chunks] + + if len(chunks) > 1: + pool = multiprocessing.Pool(processes=cores) + outputs = pool.map(process_funcs, commands, chunksize=1) + else: + outputs = [process_funcs(commands[0])] + + for funcs_js, curr_forwarded_data in outputs: + js += funcs_js + # merge forwarded data + curr_forwarded_json = json.loads(curr_forwarded_data) + forwarded_json['Types']['preciseI64MathUsed'] = forwarded_json['Types']['preciseI64MathUsed'] or curr_forwarded_json['Types']['preciseI64MathUsed'] + for key, value in curr_forwarded_json['Functions']['blockAddresses'].iteritems(): + forwarded_json['Functions']['blockAddresses'][key] = value + for key in curr_forwarded_json['Functions']['indexedFunctions'].iterkeys(): + indexed_functions.add(key) + if DEBUG: print >> sys.stderr, ' emscript: phase 2 took %s seconds' % (time.time() - t) + if DEBUG: t = time.time() + + # calculations on merged forwarded data + forwarded_json['Functions']['indexedFunctions'] = {} + i = 2 + for indexed in indexed_functions: + forwarded_json['Functions']['indexedFunctions'][indexed] = i # make sure not to modify this python object later - we use it in indexize + i += 2 + forwarded_json['Functions']['nextIndex'] = i + indexing = forwarded_json['Functions']['indexedFunctions'] + def indexize(js): + return re.sub(r'{{{ FI_([\w\d_$]+) }}}', lambda m: str(indexing[m.groups(0)[0]]), js) + # forward + forwarded_data = json.dumps(forwarded_json) + forwarded_file = temp_files.get('.2.json').name + open(forwarded_file, 'w').write(forwarded_data) + if DEBUG: print >> sys.stderr, ' emscript: phase 2b took %s seconds' % (time.time() - t) + + # Phase 3 - post + if DEBUG: t = time.time() + post_file = temp_files.get('.post.ll').name + open(post_file, 'w').write(''.join(post) + '\n' + meta) + out = shared.run_js(compiler, shared.COMPILER_ENGINE, [settings_file, post_file, 'post', forwarded_file] + libraries, stdout=subprocess.PIPE, cwd=path_from_root('src')) + js += out + js = indexize(js) + if DEBUG: print >> sys.stderr, ' emscript: phase 3 took %s seconds' % (time.time() - t) + + outfile.write(js) # TODO: write in parts (see previous line though) outfile.close() @@ -127,11 +294,11 @@ def main(args): libraries = args.libraries[0].split(',') if len(args.libraries) > 0 else [] # Compile the assembly to Javascript. - emscript(args.infile, json.dumps(settings), args.outfile, libraries) + emscript(args.infile, settings, args.outfile, libraries) if __name__ == '__main__': parser = optparse.OptionParser( - usage='usage: %prog [-h] [-H HEADERS] [-o OUTFILE] [-s FOO=BAR]* infile', + usage='usage: %prog [-h] [-H HEADERS] [-o OUTFILE] [-c COMPILER_ENGINE] [-s FOO=BAR]* infile', description=('You should normally never use this! Use emcc instead. ' 'This is a wrapper around the JS compiler, converting .ll to .js.'), epilog='') @@ -146,6 +313,9 @@ if __name__ == '__main__': parser.add_option('-o', '--outfile', default=sys.stdout, help='Where to write the output; defaults to stdout.') + parser.add_option('-c', '--compiler', + default=shared.COMPILER_ENGINE, + help='Which JS engine to use to run the compiler; defaults to the one in ~/.emscripten.') parser.add_option('-s', '--setting', dest='settings', default=[], @@ -161,6 +331,7 @@ if __name__ == '__main__': keywords.infile = os.path.abspath(positional[0]) if isinstance(keywords.outfile, basestring): keywords.outfile = open(keywords.outfile, 'w') + compiler_engine = keywords.compiler temp_files.run_and_clean(lambda: main(keywords)) diff --git a/src/analyzer.js b/src/analyzer.js index 1849b58d..a6a37400 100644 --- a/src/analyzer.js +++ b/src/analyzer.js @@ -1346,6 +1346,14 @@ function analyzer(data, sidePass) { } }); }); + + if (func.ident in NECESSARY_BLOCKADDRS) { + Functions.blockAddresses[func.ident] = {}; + for (var needed in NECESSARY_BLOCKADDRS[func.ident]) { + assert(needed in func.labelIds); + Functions.blockAddresses[func.ident][needed] = func.labelIds[needed]; + } + } }); this.forwardItem(item, 'StackAnalyzer'); } diff --git a/src/compiler.js b/src/compiler.js index e589646b..3220c977 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -129,7 +129,13 @@ load('settings.js'); var settings_file = arguments_[0]; var ll_file = arguments_[1]; -additionalLibraries = Array.prototype.slice.call(arguments_, 2); +phase = arguments_[2]; +if (phase == 'pre') { + additionalLibraries = Array.prototype.slice.call(arguments_, 3); +} else { + var forwardedDataFile = arguments_[3]; + additionalLibraries = Array.prototype.slice.call(arguments_, 4); +} if (settings_file) { var settings = JSON.parse(read(settings_file)); @@ -191,6 +197,15 @@ load('jsifier.js'); globalEval(processMacros(preprocess(read('runtime.js')))); Runtime.QUANTUM_SIZE = QUANTUM_SIZE; +var temp = {}; +for (var i = 0; i < NECESSARY_BLOCKADDRS.length; i++) { + var func = toNiceIdent(NECESSARY_BLOCKADDRS[i][0]); + var label = toNiceIdent(NECESSARY_BLOCKADDRS[i][1]); + if (!temp[func]) temp[func] = {}; + temp[func][label] = 1; +} +NECESSARY_BLOCKADDRS = temp; + //=============================== // Main //=============================== @@ -209,8 +224,17 @@ raw = null; // Pre-process the LLVM assembly +//printErr('JS compiler in action, phase ' + phase); + Debugging.handleMetadata(lines); -PreProcessor.eliminateUnneededIntrinsics(lines); + +if (phase != 'pre') { + PassManager.load(read(forwardedDataFile)); + + if (phase == 'funcs') { + PreProcessor.eliminateUnneededIntrinsics(lines); + } +} // Do it diff --git a/src/jsifier.js b/src/jsifier.js index 78f48118..21628079 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -13,33 +13,36 @@ function JSify(data, functionsOnly, givenFunctions) { var mainPass = !functionsOnly; if (mainPass) { - // We will start to print out the data, but must do so carefully - we are - // dealing with potentially *huge* strings. Convenient replacements and - // manipulations may create in-memory copies, and we may OOM. - // - // Final shape that will be created: - // shell - // (body) - // preamble - // runtime - // generated code - // postamble - // global_vars - // - // First, we print out everything until the generated code. Then the - // functions will print themselves out as they are parsed. Finally, we - // will call finalCombiner in the main pass, to print out everything - // else. This lets us not hold any strings in memory, we simply print - // things out as they are ready. - var shellFile = SHELL_FILE ? SHELL_FILE : (BUILD_AS_SHARED_LIB ? 'shell_sharedlib.js' : 'shell.js'); - var shellParts = read(shellFile).split('{{BODY}}'); - print(shellParts[0]); - var preFile = BUILD_AS_SHARED_LIB ? 'preamble_sharedlib.js' : 'preamble.js'; - var pre = processMacros(preprocess(read(preFile).replace('{{RUNTIME}}', getRuntime()))); - print(pre); - Functions.implementedFunctions = set(data.unparsedFunctions.map(function(func) { return func.ident })); + if (phase == 'pre') { + // We will start to print out the data, but must do so carefully - we are + // dealing with potentially *huge* strings. Convenient replacements and + // manipulations may create in-memory copies, and we may OOM. + // + // Final shape that will be created: + // shell + // (body) + // preamble + // runtime + // generated code + // postamble + // global_vars + // + // First, we print out everything until the generated code. Then the + // functions will print themselves out as they are parsed. Finally, we + // will call finalCombiner in the main pass, to print out everything + // else. This lets us not hold any strings in memory, we simply print + // things out as they are ready. + + var shellParts = read(shellFile).split('{{BODY}}'); + print(shellParts[0]); + var preFile = BUILD_AS_SHARED_LIB ? 'preamble_sharedlib.js' : 'preamble.js'; + var pre = processMacros(preprocess(read(preFile).replace('{{RUNTIME}}', getRuntime()))); + print(pre); + + Functions.implementedFunctions = set(data.unparsedFunctions.map(function(func) { return func.ident })); + } } // Does simple 'macro' substitution, using Django-like syntax, @@ -83,19 +86,8 @@ function JSify(data, functionsOnly, givenFunctions) { // Functions - Functions.currFunctions = !mainPass ? givenFunctions.currFunctions : {}; Functions.currExternalFunctions = !mainPass ? givenFunctions.currExternalFunctions : {}; - // Now that first-pass analysis has completed (so we have basic types, etc.), we can get around to handling unparsedFunctions - (!mainPass ? data.functions : data.unparsedFunctions.concat(data.functions)).forEach(function(func) { - // Save just what we need, to save memory - Functions.currFunctions[func.ident] = { - hasVarArgs: func.hasVarArgs, - numParams: func.params.length, - labelIds: func.labelIds // TODO: We need this for globals, but perhaps we can calculate them early and free this - }; - }); - data.functionStubs.forEach(function(func) { // Don't overwrite stubs that have more info. if (!Functions.currExternalFunctions.hasOwnProperty(func.ident) || @@ -107,24 +99,26 @@ function JSify(data, functionsOnly, givenFunctions) { } }); - var MAX_BATCH_FUNC_LINES = 1000; - while (data.unparsedFunctions.length > 0) { - var currFuncLines = []; - var currBaseLineNums = []; - while (currFuncLines.length == 0 || - (data.unparsedFunctions.length > 0 && currFuncLines.length + data.unparsedFunctions[0].lines.length <= MAX_BATCH_FUNC_LINES)) { - currBaseLineNums.push([currFuncLines.length, data.unparsedFunctions[0].lineNum-1]); - currFuncLines = currFuncLines.concat(data.unparsedFunctions[0].lines); // for first one, assign, do not concat? - data.unparsedFunctions.shift(); + if (phase == 'funcs') { // pre has function shells, just to defined implementedFunctions + var MAX_BATCH_FUNC_LINES = 1000; + while (data.unparsedFunctions.length > 0) { + var currFuncLines = []; + var currBaseLineNums = []; + while (currFuncLines.length == 0 || + (data.unparsedFunctions.length > 0 && currFuncLines.length + data.unparsedFunctions[0].lines.length <= MAX_BATCH_FUNC_LINES)) { + currBaseLineNums.push([currFuncLines.length, data.unparsedFunctions[0].lineNum-1]); + currFuncLines = currFuncLines.concat(data.unparsedFunctions[0].lines); // for first one, assign, do not concat? + data.unparsedFunctions.shift(); + } + dprint('unparsedFunctions','====================\n// Processing function batch of ' + currBaseLineNums.length + + ' functions, ' + currFuncLines.length + ' lines, functions left: ' + data.unparsedFunctions.length); + if (DEBUG_MEMORY) MemoryDebugger.tick('pre-func'); + JSify(analyzer(intertyper(currFuncLines, true, currBaseLineNums), true), true, Functions); + if (DEBUG_MEMORY) MemoryDebugger.tick('post-func'); } - dprint('unparsedFunctions','====================\n// Processing function batch of ' + currBaseLineNums.length + - ' functions, ' + currFuncLines.length + ' lines, functions left: ' + data.unparsedFunctions.length); - if (DEBUG_MEMORY) MemoryDebugger.tick('pre-func'); - JSify(analyzer(intertyper(currFuncLines, true, currBaseLineNums), true), true, Functions); - if (DEBUG_MEMORY) MemoryDebugger.tick('post-func'); + currFuncLines = currBaseLineNums = null; // Do not hold on to anything from inside that loop (JS function scoping..) + data.unparsedFunctions = null; } - currFuncLines = currBaseLineNums = null; // Do not hold on to anything from inside that loop (JS function scoping..) - data.unparsedFunctions = null; // Actors @@ -1221,19 +1215,20 @@ function JSify(data, functionsOnly, givenFunctions) { if (!mainPass) { var generated = itemsDict.function.concat(itemsDict.type).concat(itemsDict.GlobalVariableStub).concat(itemsDict.GlobalVariable).concat(itemsDict.GlobalVariablePostSet); - Functions.allIdents = Functions.allIdents.concat(itemsDict.function.map(function(func) { - return func.ident; - }).filter(function(func) { - return IGNORED_FUNCTIONS.indexOf(func.ident) < 0; - })); if (!DEBUG_MEMORY) print(generated.map(function(item) { return item.JS }).join('\n')); return; } - // This is the main pass. Print out the generated code that we have here, together with the + if (phase == 'pre' || phase == 'funcs') { + // serialize out the data that later passes need + PassManager.serialize(); // XXX for funcs pass, do not serialize it all. I think we just need which were indexized. + return; + } + + // This is the main 'post' pass. Print out the generated code that we have here, together with the // rest of the output that we started to print out earlier (see comment on the // "Final shape that will be created"). - if (PRECISE_I64_MATH && preciseI64MathUsed) { + if (PRECISE_I64_MATH && Types.preciseI64MathUsed) { print(read('long.js')); } else { print('// Warning: printing of i64 values may be slightly rounded! No deep i64 math used, so precise i64 code not included'); @@ -1264,9 +1259,12 @@ function JSify(data, functionsOnly, givenFunctions) { print(postParts[1]); + var shellParts = read(shellFile).split('{{BODY}}'); print(shellParts[1]); // Print out some useful metadata (for additional optimizations later, like the eliminator) - print('// EMSCRIPTEN_GENERATED_FUNCTIONS: ' + JSON.stringify(Functions.allIdents) + '\n'); + print('// EMSCRIPTEN_GENERATED_FUNCTIONS: ' + JSON.stringify(keys(Functions.implementedFunctions).filter(function(func) { + return IGNORED_FUNCTIONS.indexOf(func.ident) < 0; + })) + '\n'); return null; } diff --git a/src/library.js b/src/library.js index 25195415..73109334 100644 --- a/src/library.js +++ b/src/library.js @@ -4748,7 +4748,7 @@ LibraryManager.library = { }, __assert_func: function(filename, line, func, condition) { - throw 'Assertion failed: ' + Pointer_stringify(condition) + ', at: ' + [Pointer_stringify(filename), line, Pointer_stringify(func)]; + throw 'Assertion failed: ' + (condition ? Pointer_stringify(condition) : 'unknown condition') + ', at: ' + [filename ? Pointer_stringify(filename) : 'unknown filename', line, func ? Pointer_stringify(func) : 'unknown function']; }, __cxa_guard_acquire: function(variable) { @@ -5042,7 +5042,7 @@ LibraryManager.library = { }; }, - llvm_uadd_with_overflow_i64__deps: [function() { preciseI64MathUsed = 1 }], + llvm_uadd_with_overflow_i64__deps: [function() { Types.preciseI64MathUsed = 1 }], llvm_uadd_with_overflow_i64: function(xl, xh, yl, yh) { i64Math.add(xl, xh, yl, yh); return { @@ -5051,7 +5051,7 @@ LibraryManager.library = { }; }, - llvm_umul_with_overflow_i64__deps: [function() { preciseI64MathUsed = 1 }], + llvm_umul_with_overflow_i64__deps: [function() { Types.preciseI64MathUsed = 1 }], llvm_umul_with_overflow_i64: function(xl, xh, yl, yh) { i64Math.mul(xl, xh, yl, yh); return { diff --git a/src/long.js b/src/long.js index 71cffa79..d5770e48 100644 --- a/src/long.js +++ b/src/long.js @@ -24,1609 +24,1609 @@ */ var i64Math = (function() { // Emscripten wrapper -var goog = { math: {} }; + var goog = { math: {} }; -/** - * Constructs a 64-bit two's-complement integer, given its low and high 32-bit - * values as *signed* integers. See the from* functions below for more - * convenient ways of constructing Longs. - * - * The internal representation of a long is the two given signed, 32-bit values. - * We use 32-bit pieces because these are the size of integers on which - * Javascript performs bit-operations. For operations like addition and - * multiplication, we split each number into 16-bit pieces, which can easily be - * multiplied within Javascript's floating-point representation without overflow - * or change in sign. - * - * In the algorithms below, we frequently reduce the negative case to the - * positive case by negating the input(s) and then post-processing the result. - * Note that we must ALWAYS check specially whether those values are MIN_VALUE - * (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as - * a positive number, it overflows back into a negative). Not handling this - * case would often result in infinite recursion. - * - * @param {number} low The low (signed) 32 bits of the long. - * @param {number} high The high (signed) 32 bits of the long. - * @constructor - */ -goog.math.Long = function(low, high) { /** - * @type {number} - * @private + * Constructs a 64-bit two's-complement integer, given its low and high 32-bit + * values as *signed* integers. See the from* functions below for more + * convenient ways of constructing Longs. + * + * The internal representation of a long is the two given signed, 32-bit values. + * We use 32-bit pieces because these are the size of integers on which + * Javascript performs bit-operations. For operations like addition and + * multiplication, we split each number into 16-bit pieces, which can easily be + * multiplied within Javascript's floating-point representation without overflow + * or change in sign. + * + * In the algorithms below, we frequently reduce the negative case to the + * positive case by negating the input(s) and then post-processing the result. + * Note that we must ALWAYS check specially whether those values are MIN_VALUE + * (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as + * a positive number, it overflows back into a negative). Not handling this + * case would often result in infinite recursion. + * + * @param {number} low The low (signed) 32 bits of the long. + * @param {number} high The high (signed) 32 bits of the long. + * @constructor */ - this.low_ = low | 0; // force into 32 signed bits. + goog.math.Long = function(low, high) { + /** + * @type {number} + * @private + */ + this.low_ = low | 0; // force into 32 signed bits. - /** - * @type {number} - * @private - */ - this.high_ = high | 0; // force into 32 signed bits. -}; + /** + * @type {number} + * @private + */ + this.high_ = high | 0; // force into 32 signed bits. + }; -// NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the -// from* methods on which they depend. + // NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the + // from* methods on which they depend. -/** - * A cache of the Long representations of small integer values. - * @type {!Object} - * @private - */ -goog.math.Long.IntCache_ = {}; + /** + * A cache of the Long representations of small integer values. + * @type {!Object} + * @private + */ + goog.math.Long.IntCache_ = {}; -/** - * Returns a Long representing the given (32-bit) integer value. - * @param {number} value The 32-bit integer in question. - * @return {!goog.math.Long} The corresponding Long value. - */ -goog.math.Long.fromInt = function(value) { - if (-128 <= value && value < 128) { - var cachedObj = goog.math.Long.IntCache_[value]; - if (cachedObj) { - return cachedObj; + /** + * Returns a Long representing the given (32-bit) integer value. + * @param {number} value The 32-bit integer in question. + * @return {!goog.math.Long} The corresponding Long value. + */ |