diff options
-rwxr-xr-x | emscripten.py | 1057 | ||||
-rw-r--r-- | tools/shared.py | 6 |
2 files changed, 536 insertions, 527 deletions
diff --git a/emscripten.py b/emscripten.py index d75214d5..9abaf60c 100755 --- a/emscripten.py +++ b/emscripten.py @@ -726,7 +726,7 @@ Runtime.getTempRet0 = asm['getTempRet0']; def emscript_fast(infile, settings, outfile, libraries=[], compiler_engine=None, jcache=None, temp_files=None, DEBUG=None, DEBUG_CACHE=None): - """Runs the emscripten LLVM-to-JS compiler. We parallelize as much as possible + """Runs the emscripten LLVM-to-JS compiler. Args: infile: The path to the input LLVM assembly file. @@ -737,571 +737,582 @@ def emscript_fast(infile, settings, outfile, libraries=[], compiler_engine=None, assert(settings['ASM_JS']) - # Overview: - # * Run LLVM backend to emit JS. JS includes function bodies, memory initializer, - # and various metadata - # * Run compiler.js on the metadata to emit the shell js code, pre/post-ambles, - # JS library dependencies, etc. - - temp_js = temp_files.get('.4.js').name - backend_compiler = os.path.join(shared.LLVM_ROOT, 'llc') - backend_args = [backend_compiler, infile, '-march=js', '-filetype=asm', '-o', temp_js] - if settings['PRECISE_F32']: - backend_args += ['-emscripten-precise-f32'] - if settings['WARN_UNALIGNED']: - backend_args += ['-emscripten-warn-unaligned'] - if settings['RESERVED_FUNCTION_POINTERS'] > 0: - backend_args += ['-emscripten-reserved-function-pointers=%d' % settings['RESERVED_FUNCTION_POINTERS']] - if settings['ASSERTIONS'] > 0: - backend_args += ['-emscripten-assertions=%d' % settings['ASSERTIONS']] - if settings['ALIASING_FUNCTION_POINTERS'] == 0: - backend_args += ['-emscripten-no-aliasing-function-pointers'] - backend_args += ['-O' + str(settings['OPT_LEVEL'])] - if DEBUG: - logging.debug('emscript: llvm backend: ' + ' '.join(backend_args)) - t = time.time() - shared.jsrun.timeout_run(subprocess.Popen(backend_args, stdout=subprocess.PIPE)) - if DEBUG: - logging.debug(' emscript: llvm backend took %s seconds' % (time.time() - t)) - t = time.time() - - # Split up output - backend_output = open(temp_js).read() - #if DEBUG: print >> sys.stderr, backend_output - - start_funcs_marker = '// EMSCRIPTEN_START_FUNCTIONS' - end_funcs_marker = '// EMSCRIPTEN_END_FUNCTIONS' - metadata_split_marker = '// EMSCRIPTEN_METADATA' - - start_funcs = backend_output.index(start_funcs_marker) - end_funcs = backend_output.rindex(end_funcs_marker) - metadata_split = backend_output.rindex(metadata_split_marker) - - funcs = backend_output[start_funcs+len(start_funcs_marker):end_funcs] - metadata_raw = backend_output[metadata_split+len(metadata_split_marker):] - #if DEBUG: print >> sys.stderr, "METAraw", metadata_raw - metadata = json.loads(metadata_raw) - mem_init = backend_output[end_funcs+len(end_funcs_marker):metadata_split] - #if DEBUG: print >> sys.stderr, "FUNCS", funcs - #if DEBUG: print >> sys.stderr, "META", metadata - #if DEBUG: print >> sys.stderr, "meminit", mem_init - - # function table masks - - table_sizes = {} - for k, v in metadata['tables'].iteritems(): - table_sizes[k] = str(v.count(',')) # undercounts by one, but that is what we want - #if settings['ASSERTIONS'] >= 2 and table_sizes[k] == 0: - # print >> sys.stderr, 'warning: no function pointers with signature ' + k + ', but there is a call, which will abort if it occurs (this can result from undefined behavior, check for compiler warnings on your source files and consider -Werror)' - funcs = re.sub(r"#FM_(\w+)#", lambda m: table_sizes[m.groups(0)[0]], funcs) - - # fix +float into float.0, if not running js opts - if not settings['RUNNING_JS_OPTS']: - def fix_dot_zero(m): - num = m.group(3) - # TODO: handle 0x floats? - if num.find('.') < 0: - e = num.find('e'); - if e < 0: - num += '.0' - else: - num = num[:e] + '.0' + num[e:] - return m.group(1) + m.group(2) + num - funcs = re.sub(r'([(=,+\-*/%<>:?] *)\+(-?)((0x)?[0-9a-f]*\.?[0-9]+([eE][-+]?[0-9]+)?)', lambda m: fix_dot_zero(m), funcs) - - # js compiler - - if DEBUG: logging.debug('emscript: js compiler glue') - - # Settings changes - assert settings['TARGET_ASMJS_UNKNOWN_EMSCRIPTEN'] == 1 - settings['TARGET_ASMJS_UNKNOWN_EMSCRIPTEN'] = 2 - i64_funcs = ['i64Add', 'i64Subtract', '__muldi3', '__divdi3', '__udivdi3', '__remdi3', '__uremdi3'] - for i64_func in i64_funcs: - if i64_func in metadata['declares']: - settings['PRECISE_I64_MATH'] = 2 - break + success = False - metadata['declares'] = filter(lambda i64_func: i64_func not in ['getHigh32', 'setHigh32', '__muldi3', '__divdi3', '__remdi3', '__udivdi3', '__uremdi3'], metadata['declares']) # FIXME: do these one by one as normal js lib funcs - - # Integrate info from backend - settings['DEFAULT_LIBRARY_FUNCS_TO_INCLUDE'] = list( - set(settings['DEFAULT_LIBRARY_FUNCS_TO_INCLUDE'] + map(shared.JS.to_nice_ident, metadata['declares'])).difference( - map(lambda x: x[1:], metadata['implementedFunctions']) - ) - ) + map(lambda x: x[1:], metadata['externs']) - if metadata['simd']: - settings['SIMD'] = 1 - if not metadata['canValidate'] and settings['ASM_JS'] != 2: - logging.warning('disabling asm.js validation due to use of non-supported features') - settings['ASM_JS'] = 2 + try: - # Save settings to a file to work around v8 issue 1579 - settings_file = temp_files.get('.txt').name - def save_settings(): - global settings_text - settings_text = json.dumps(settings, sort_keys=True) - s = open(settings_file, 'w') - s.write(settings_text) - s.close() - save_settings() + # Overview: + # * Run LLVM backend to emit JS. JS includes function bodies, memory initializer, + # and various metadata + # * Run compiler.js on the metadata to emit the shell js code, pre/post-ambles, + # JS library dependencies, etc. + + temp_js = temp_files.get('.4.js').name + backend_compiler = os.path.join(shared.LLVM_ROOT, 'llc') + backend_args = [backend_compiler, infile, '-march=js', '-filetype=asm', '-o', temp_js] + if settings['PRECISE_F32']: + backend_args += ['-emscripten-precise-f32'] + if settings['WARN_UNALIGNED']: + backend_args += ['-emscripten-warn-unaligned'] + if settings['RESERVED_FUNCTION_POINTERS'] > 0: + backend_args += ['-emscripten-reserved-function-pointers=%d' % settings['RESERVED_FUNCTION_POINTERS']] + if settings['ASSERTIONS'] > 0: + backend_args += ['-emscripten-assertions=%d' % settings['ASSERTIONS']] + if settings['ALIASING_FUNCTION_POINTERS'] == 0: + backend_args += ['-emscripten-no-aliasing-function-pointers'] + backend_args += ['-O' + str(settings['OPT_LEVEL'])] + if DEBUG: + logging.debug('emscript: llvm backend: ' + ' '.join(backend_args)) + t = time.time() + shared.jsrun.timeout_run(subprocess.Popen(backend_args, stdout=subprocess.PIPE)) + if DEBUG: + logging.debug(' emscript: llvm backend took %s seconds' % (time.time() - t)) + t = time.time() + + # Split up output + backend_output = open(temp_js).read() + #if DEBUG: print >> sys.stderr, backend_output + + start_funcs_marker = '// EMSCRIPTEN_START_FUNCTIONS' + end_funcs_marker = '// EMSCRIPTEN_END_FUNCTIONS' + metadata_split_marker = '// EMSCRIPTEN_METADATA' + + start_funcs = backend_output.index(start_funcs_marker) + end_funcs = backend_output.rindex(end_funcs_marker) + metadata_split = backend_output.rindex(metadata_split_marker) + + funcs = backend_output[start_funcs+len(start_funcs_marker):end_funcs] + metadata_raw = backend_output[metadata_split+len(metadata_split_marker):] + #if DEBUG: print >> sys.stderr, "METAraw", metadata_raw + metadata = json.loads(metadata_raw) + mem_init = backend_output[end_funcs+len(end_funcs_marker):metadata_split] + #if DEBUG: print >> sys.stderr, "FUNCS", funcs + #if DEBUG: print >> sys.stderr, "META", metadata + #if DEBUG: print >> sys.stderr, "meminit", mem_init + + # function table masks + + table_sizes = {} + for k, v in metadata['tables'].iteritems(): + table_sizes[k] = str(v.count(',')) # undercounts by one, but that is what we want + #if settings['ASSERTIONS'] >= 2 and table_sizes[k] == 0: + # print >> sys.stderr, 'warning: no function pointers with signature ' + k + ', but there is a call, which will abort if it occurs (this can result from undefined behavior, check for compiler warnings on your source files and consider -Werror)' + funcs = re.sub(r"#FM_(\w+)#", lambda m: table_sizes[m.groups(0)[0]], funcs) + + # fix +float into float.0, if not running js opts + if not settings['RUNNING_JS_OPTS']: + def fix_dot_zero(m): + num = m.group(3) + # TODO: handle 0x floats? + if num.find('.') < 0: + e = num.find('e'); + if e < 0: + num += '.0' + else: + num = num[:e] + '.0' + num[e:] + return m.group(1) + m.group(2) + num + funcs = re.sub(r'([(=,+\-*/%<>:?] *)\+(-?)((0x)?[0-9a-f]*\.?[0-9]+([eE][-+]?[0-9]+)?)', lambda m: fix_dot_zero(m), funcs) - # Call js compiler - if DEBUG: t = time.time() - out = jsrun.run_js(path_from_root('src', 'compiler.js'), compiler_engine, [settings_file, ';', 'glue'] + libraries, stdout=subprocess.PIPE, stderr=STDERR_FILE, - cwd=path_from_root('src')) - assert '//FORWARDED_DATA:' in out, 'Did not receive forwarded data in pre output - process failed?' - glue, forwarded_data = out.split('//FORWARDED_DATA:') + # js compiler - if DEBUG: - logging.debug(' emscript: glue took %s seconds' % (time.time() - t)) - t = time.time() + if DEBUG: logging.debug('emscript: js compiler glue') - last_forwarded_json = forwarded_json = json.loads(forwarded_data) + # Settings changes + assert settings['TARGET_ASMJS_UNKNOWN_EMSCRIPTEN'] == 1 + settings['TARGET_ASMJS_UNKNOWN_EMSCRIPTEN'] = 2 + i64_funcs = ['i64Add', 'i64Subtract', '__muldi3', '__divdi3', '__udivdi3', '__remdi3', '__uremdi3'] + for i64_func in i64_funcs: + if i64_func in metadata['declares']: + settings['PRECISE_I64_MATH'] = 2 + break - # merge in information from llvm backend + metadata['declares'] = filter(lambda i64_func: i64_func not in ['getHigh32', 'setHigh32', '__muldi3', '__divdi3', '__remdi3', '__udivdi3', '__uremdi3'], metadata['declares']) # FIXME: do these one by one as normal js lib funcs - last_forwarded_json['Functions']['tables'] = metadata['tables'] + # Integrate info from backend + settings['DEFAULT_LIBRARY_FUNCS_TO_INCLUDE'] = list( + set(settings['DEFAULT_LIBRARY_FUNCS_TO_INCLUDE'] + map(shared.JS.to_nice_ident, metadata['declares'])).difference( + map(lambda x: x[1:], metadata['implementedFunctions']) + ) + ) + map(lambda x: x[1:], metadata['externs']) + if metadata['simd']: + settings['SIMD'] = 1 + if not metadata['canValidate'] and settings['ASM_JS'] != 2: + logging.warning('disabling asm.js validation due to use of non-supported features') + settings['ASM_JS'] = 2 + + # Save settings to a file to work around v8 issue 1579 + settings_file = temp_files.get('.txt').name + def save_settings(): + global settings_text + settings_text = json.dumps(settings, sort_keys=True) + s = open(settings_file, 'w') + s.write(settings_text) + s.close() + save_settings() - '''indexed_functions = set() - for key in forwarded_json['Functions']['indexedFunctions'].iterkeys(): - indexed_functions.add(key)''' + # Call js compiler + if DEBUG: t = time.time() + out = jsrun.run_js(path_from_root('src', 'compiler.js'), compiler_engine, [settings_file, ';', 'glue'] + libraries, stdout=subprocess.PIPE, stderr=STDERR_FILE, + cwd=path_from_root('src')) + assert '//FORWARDED_DATA:' in out, 'Did not receive forwarded data in pre output - process failed?' + glue, forwarded_data = out.split('//FORWARDED_DATA:') - pre, post = glue.split('// EMSCRIPTEN_END_FUNCS') + if DEBUG: + logging.debug(' emscript: glue took %s seconds' % (time.time() - t)) + t = time.time() - #print >> sys.stderr, 'glue:', pre, '\n\n||||||||||||||||\n\n', post, '...............' + last_forwarded_json = forwarded_json = json.loads(forwarded_data) - # memory and global initializers + # merge in information from llvm backend - global_initializers = ', '.join(map(lambda i: '{ func: function() { %s() } }' % i, metadata['initializers'])) + last_forwarded_json['Functions']['tables'] = metadata['tables'] - pre = pre.replace('STATICTOP = STATIC_BASE + 0;', '''STATICTOP = STATIC_BASE + Runtime.alignMemory(%d); -/* global initializers */ __ATINIT__.push(%s); -%s''' % (mem_init.count(',')+1, global_initializers, mem_init)) # XXX wrong size calculation! + '''indexed_functions = set() + for key in forwarded_json['Functions']['indexedFunctions'].iterkeys(): + indexed_functions.add(key)''' - funcs_js = [funcs] - if settings.get('ASM_JS'): - parts = pre.split('// ASM_LIBRARY FUNCTIONS\n') - if len(parts) > 1: - pre = parts[0] - funcs_js.append(parts[1]) + pre, post = glue.split('// EMSCRIPTEN_END_FUNCS') - # merge forwarded data - assert settings.get('ASM_JS'), 'fastcomp is asm.js only' - settings['EXPORTED_FUNCTIONS'] = forwarded_json['EXPORTED_FUNCTIONS'] - all_exported_functions = set(settings['EXPORTED_FUNCTIONS']) # both asm.js and otherwise - for additional_export in settings['DEFAULT_LIBRARY_FUNCS_TO_INCLUDE']: # additional functions to export from asm, if they are implemented - all_exported_functions.add('_' + additional_export) - exported_implemented_functions = set(metadata['exports']) - export_bindings = settings['EXPORT_BINDINGS'] - export_all = settings['EXPORT_ALL'] - all_implemented = metadata['implementedFunctions'] + forwarded_json['Functions']['implementedFunctions'].keys() # XXX perf? - for key in all_implemented: - if key in all_exported_functions or export_all or (export_bindings and key.startswith('_emscripten_bind')): - exported_implemented_functions.add(key) - implemented_functions = set(metadata['implementedFunctions']) - if settings['ASSERTIONS'] and settings.get('ORIGINAL_EXPORTED_FUNCTIONS'): - for requested in settings['ORIGINAL_EXPORTED_FUNCTIONS']: - if requested not in all_implemented: - logging.warning('function requested to be exported, but not implemented: "%s"', requested) - - # Add named globals - named_globals = '\n'.join(['var %s = %s;' % (k, v) for k, v in metadata['namedGlobals'].iteritems()]) - pre = pre.replace('// === Body ===', '// === Body ===\n' + named_globals + '\n') + #print >> sys.stderr, 'glue:', pre, '\n\n||||||||||||||||\n\n', post, '...............' - #if DEBUG: outfile.write('// pre\n') - outfile.write(pre) - pre = None + # memory and global initializers - #if DEBUG: outfile.write('// funcs\n') + global_initializers = ', '.join(map(lambda i: '{ func: function() { %s() } }' % i, metadata['initializers'])) - if settings.get('ASM_JS'): - # Move preAsms to their right place - def move_preasm(m): - contents = m.groups(0)[0] - outfile.write(contents + '\n') - return '' - funcs_js[1] = re.sub(r'/\* PRE_ASM \*/(.*)\n', lambda m: move_preasm(m), funcs_js[1]) + pre = pre.replace('STATICTOP = STATIC_BASE + 0;', '''STATICTOP = STATIC_BASE + Runtime.alignMemory(%d); + /* global initializers */ __ATINIT__.push(%s); + %s''' % (mem_init.count(',')+1, global_initializers, mem_init)) # XXX wrong size calculation! - funcs_js += ['\n// EMSCRIPTEN_END_FUNCS\n'] + funcs_js = [funcs] + if settings.get('ASM_JS'): + parts = pre.split('// ASM_LIBRARY FUNCTIONS\n') + if len(parts) > 1: + pre = parts[0] + funcs_js.append(parts[1]) - simple = os.environ.get('EMCC_SIMPLE_ASM') - class Counter: - i = 0 - j = 0 - if 'pre' in last_forwarded_json['Functions']['tables']: - pre_tables = last_forwarded_json['Functions']['tables']['pre'] - del last_forwarded_json['Functions']['tables']['pre'] - else: - pre_tables = '' + # merge forwarded data + assert settings.get('ASM_JS'), 'fastcomp is asm.js only' + settings['EXPORTED_FUNCTIONS'] = forwarded_json['EXPORTED_FUNCTIONS'] + all_exported_functions = set(settings['EXPORTED_FUNCTIONS']) # both asm.js and otherwise + for additional_export in settings['DEFAULT_LIBRARY_FUNCS_TO_INCLUDE']: # additional functions to export from asm, if they are implemented + all_exported_functions.add('_' + additional_export) + exported_implemented_functions = set(metadata['exports']) + export_bindings = settings['EXPORT_BINDINGS'] + export_all = settings['EXPORT_ALL'] + all_implemented = metadata['implementedFunctions'] + forwarded_json['Functions']['implementedFunctions'].keys() # XXX perf? + for key in all_implemented: + if key in all_exported_functions or export_all or (export_bindings and key.startswith('_emscripten_bind')): + exported_implemented_functions.add(key) + implemented_functions = set(metadata['implementedFunctions']) + if settings['ASSERTIONS'] and settings.get('ORIGINAL_EXPORTED_FUNCTIONS'): + for requested in settings['ORIGINAL_EXPORTED_FUNCTIONS']: + if requested not in all_implemented: + logging.warning('function requested to be exported, but not implemented: "%s"', requested) + + # Add named globals + named_globals = '\n'.join(['var %s = %s;' % (k, v) for k, v in metadata['namedGlobals'].iteritems()]) + pre = pre.replace('// === Body ===', '// === Body ===\n' + named_globals + '\n') + + #if DEBUG: outfile.write('// pre\n') + outfile.write(pre) + pre = None + + #if DEBUG: outfile.write('// funcs\n') - def unfloat(s): - return 'd' if s == 'f' else s # lower float to double for ffis + if settings.get('ASM_JS'): + # Move preAsms to their right place + def move_preasm(m): + contents = m.groups(0)[0] + outfile.write(contents + '\n') + return '' + funcs_js[1] = re.sub(r'/\* PRE_ASM \*/(.*)\n', lambda m: move_preasm(m), funcs_js[1]) + + funcs_js += ['\n// EMSCRIPTEN_END_FUNCS\n'] + + simple = os.environ.get('EMCC_SIMPLE_ASM') + class Counter: + i = 0 + j = 0 + if 'pre' in last_forwarded_json['Functions']['tables']: + pre_tables = last_forwarded_json['Functions']['tables']['pre'] + del last_forwarded_json['Functions']['tables']['pre'] + else: + pre_tables = '' - if settings['ASSERTIONS'] >= 2: - debug_tables = {} + def unfloat(s): + return 'd' if s == 'f' else s # lower float to double for ffis - def make_table(sig, raw): - params = ','.join(['p%d' % p for p in range(len(sig)-1)]) - coerced_params = ','.join([shared.JS.make_coercion('p%d', unfloat(sig[p+1]), settings) % p for p in range(len(sig)-1)]) - coercions = ';'.join(['p%d = %s' % (p, shared.JS.make_coercion('p%d' % p, sig[p+1], settings)) for p in range(len(sig)-1)]) + ';' - def make_func(name, code): - return 'function %s(%s) { %s %s }' % (name, params, coercions, code) - def make_bad(target=None): - i = Counter.i - Counter.i += 1 - if target is None: target = i - name = 'b' + str(i) - if not settings['ASSERTIONS']: - code = 'abort(%s);' % target - else: - code = 'nullFunc_' + sig + '(%d);' % target - if sig[0] != 'v': - code += 'return %s' % shared.JS.make_initializer(sig[0], settings) + ';' - return name, make_func(name, code) - bad, bad_func = make_bad() # the default bad func - if settings['ASSERTIONS'] <= 1: - Counter.pre = [bad_func] - else: - Counter.pre = [] - start = raw.index('[') - end = raw.rindex(']') - body = raw[start+1:end].split(',') - for j in range(settings['RESERVED_FUNCTION_POINTERS']): - curr = 'jsCall_%s_%s' % (sig, j) - body[settings['FUNCTION_POINTER_ALIGNMENT'] * (1 + j)] = curr - implemented_functions.add(curr) - Counter.j = 0 - def fix_item(item): - Counter.j += 1 - newline = Counter.j % 30 == 29 - if item == '0': - if settings['ASSERTIONS'] <= 1: - return bad if not newline else (bad + '\n') + if settings['ASSERTIONS'] >= 2: + debug_tables = {} + + def make_table(sig, raw): + params = ','.join(['p%d' % p for p in range(len(sig)-1)]) + coerced_params = ','.join([shared.JS.make_coercion('p%d', unfloat(sig[p+1]), settings) % p for p in range(len(sig)-1)]) + coercions = ';'.join(['p%d = %s' % (p, shared.JS.make_coercion('p%d' % p, sig[p+1], settings)) for p in range(len(sig)-1)]) + ';' + def make_func(name, code): + return 'function %s(%s) { %s %s }' % (name, params, coercions, code) + def make_bad(target=None): + i = Counter.i + Counter.i += 1 + if target is None: target = i + name = 'b' + str(i) + if not settings['ASSERTIONS']: + code = 'abort(%s);' % target else: - specific_bad, specific_bad_func = make_bad(Counter.j-1) - Counter.pre.append(specific_bad_func) - return specific_bad if not newline else (specific_bad + '\n') - if item not in implemented_functions: - # this is imported into asm, we must wrap it - call_ident = item - if call_ident in metadata['redirects']: call_ident = metadata['redirects'][call_ident] - if not call_ident.startswith('_') and not call_ident.startswith('Math_'): call_ident = '_' + call_ident - code = call_ident + '(' + coerced_params + ')' + code = 'nullFunc_' + sig + '(%d);' % target if sig[0] != 'v': - # ffis cannot return float - if sig[0] == 'f': code = '+' + code - code = 'return ' + shared.JS.make_coercion(code, sig[0], settings) - code += ';' - Counter.pre.append(make_func(item + '__wrapper', code)) - return item + '__wrapper' - return item if not newline else (item + '\n') - if settings['ASSERTIONS'] >= 2: - debug_tables[sig] = body - body = ','.join(map(fix_item, body)) - return ('\n'.join(Counter.pre), ''.join([raw[:start+1], body, raw[end:]])) - - infos = [make_table(sig, raw) for sig, raw in last_forwarded_json['Functions']['tables'].iteritems()] - Counter.pre = [] - - 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']] - fundamentals = ['Math', 'Int8Array', 'Int16Array', 'Int32Array', 'Uint8Array', 'Uint16Array', 'Uint32Array', 'Float32Array', 'Float64Array'] - math_envs = ['Math.min'] # TODO: move min to maths - asm_setup += '\n'.join(['var %s = %s;' % (f.replace('.', '_'), f) for f in math_envs]) - - if settings['PRECISE_F32']: maths += ['Math.fround'] - - basic_funcs = ['abort', 'assert', 'asmPrintInt', 'asmPrintFloat'] + [m.replace('.', '_') for m in math_envs] - if settings['RESERVED_FUNCTION_POINTERS'] > 0: basic_funcs.append('jsCall') - if settings['SAFE_HEAP']: basic_funcs += ['SAFE_HEAP_LOAD', 'SAFE_HEAP_STORE', 'SAFE_FT_MASK'] - if settings['CHECK_HEAP_ALIGN']: basic_funcs += ['CHECK_ALIGN_2', 'CHECK_ALIGN_4', 'CHECK_ALIGN_8'] - if settings['ASSERTIONS']: - if settings['ASSERTIONS'] >= 2: import difflib - for sig in last_forwarded_json['Functions']['tables'].iterkeys(): - basic_funcs += ['nullFunc_' + sig] + code += 'return %s' % shared.JS.make_initializer(sig[0], settings) + ';' + return name, make_func(name, code) + bad, bad_func = make_bad() # the default bad func if settings['ASSERTIONS'] <= 1: - extra = ' Module["printErr"]("Build with ASSERTIONS=2 for more info.");' - pointer = ' ' + Counter.pre = [bad_func] else: - pointer = ' \'" + x + "\' ' - asm_setup += '\nvar debug_table_' + sig + ' = ' + json.dumps(debug_tables[sig]) + ';' - extra = ' Module["printErr"]("This pointer might make sense in another type signature: ' - # sort signatures, attempting to show most likely related ones first - sigs = last_forwarded_json['Functions']['tables'].keys() - def keyfunc(other): - ret = 0 - minlen = min(len(other), len(sig)) - maxlen = min(len(other), len(sig)) - if other.startswith(sig) or sig.startswith(other): ret -= 1000 # prioritize prefixes, could be dropped params - ret -= 133*difflib.SequenceMatcher(a=other, b=sig).ratio() # prioritize on diff similarity - ret += 15*abs(len(other) - len(sig))/float(maxlen) # deprioritize the bigger the length difference is - for i in range(minlen): - if other[i] == sig[i]: ret -= 5/float(maxlen) # prioritize on identically-placed params - ret += 20*len(other) # deprioritize on length - return ret - sigs.sort(key=keyfunc) - for other in sigs: - if other != sig: - extra += other + ': " + debug_table_' + other + '[x] + " ' - extra += '"); ' - asm_setup += '\nfunction nullFunc_' + sig + '(x) { Module["printErr"]("Invalid function pointer' + pointer + 'called with signature \'' + sig + '\'. ' + \ - 'Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? ' + \ - 'Or calling a function with an incorrect type, which will fail? ' + \ - '(it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)' + \ - '"); ' + extra + ' abort(x) }\n' + Counter.pre = [] + start = raw.index('[') + end = raw.rindex(']') + body = raw[start+1:end].split(',') + for j in range(settings['RESERVED_FUNCTION_POINTERS']): + curr = 'jsCall_%s_%s' % (sig, j) + body[settings['FUNCTION_POINTER_ALIGNMENT'] * (1 + j)] = curr + implemented_functions.add(curr) + Counter.j = 0 + def fix_item(item): + Counter.j += 1 + newline = Counter.j % 30 == 29 + if item == '0': + if settings['ASSERTIONS'] <= 1: + return bad if not newline else (bad + '\n') + else: + specific_bad, specific_bad_func = make_bad(Counter.j-1) + Counter.pre.append(specific_bad_func) + return specific_bad if not newline else (specific_bad + '\n') + if item not in implemented_functions: + # this is imported into asm, we must wrap it + call_ident = item + if call_ident in metadata['redirects']: call_ident = metadata['redirects'][call_ident] + if not call_ident.startswith('_') and not call_ident.startswith('Math_'): call_ident = '_' + call_ident + code = call_ident + '(' + coerced_params + ')' + if sig[0] != 'v': + # ffis cannot return float + if sig[0] == 'f': code = '+' + code + code = 'return ' + shared.JS.make_coercion(code, sig[0], settings) + code += ';' + Counter.pre.append(make_func(item + '__wrapper', code)) + return item + '__wrapper' + return item if not newline else (item + '\n') + if settings['ASSERTIONS'] >= 2: + debug_tables[sig] = body + body = ','.join(map(fix_item, body)) + return ('\n'.join(Counter.pre), ''.join([raw[:start+1], body, raw[end:]])) + + infos = [make_table(sig, raw) for sig, raw in last_forwarded_json['Functions']['tables'].iteritems()] + Counter.pre = [] + + 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']] + fundamentals = ['Math', 'Int8Array', 'Int16Array', 'Int32Array', 'Uint8Array', 'Uint16Array', 'Uint32Array', 'Float32Array', 'Float64Array'] + math_envs = ['Math.min'] # TODO: move min to maths + asm_setup += '\n'.join(['var %s = %s;' % (f.replace('.', '_'), f) for f in math_envs]) + + if settings['PRECISE_F32']: maths += ['Math.fround'] + + basic_funcs = ['abort', 'assert', 'asmPrintInt', 'asmPrintFloat'] + [m.replace('.', '_') for m in math_envs] + if settings['RESERVED_FUNCTION_POINTERS'] > 0: basic_funcs.append('jsCall') + if settings['SAFE_HEAP']: basic_funcs += ['SAFE_HEAP_LOAD', 'SAFE_HEAP_STORE', 'SAFE_FT_MASK'] + if settings['CHECK_HEAP_ALIGN']: basic_funcs += ['CHECK_ALIGN_2', 'CHECK_ALIGN_4', 'CHECK_ALIGN_8'] + if settings['ASSERTIONS']: + if settings['ASSERTIONS'] >= 2: import difflib + for sig in last_forwarded_json['Functions']['tables'].iterkeys(): + basic_funcs += ['nullFunc_' + sig] + if settings['ASSERTIONS'] <= 1: + extra = ' Module["printErr"]("Build with ASSERTIONS=2 for more info.");' + pointer = ' ' + else: + pointer = ' \'" + x + "\' ' + asm_setup += '\nvar debug_table_' + sig + ' = ' + json.dumps(debug_tables[sig]) + ';' + extra = ' Module["printErr"]("This pointer might make sense in another type signature: ' + # sort signatures, attempting to show most likely related ones first + sigs = last_forwarded_json['Functions']['tables'].keys() + def keyfunc(other): + ret = 0 + minlen = min(len(other), len(sig)) + maxlen = min(len(other), len(sig)) + if other.startswith(sig) or sig.startswith(other): ret -= 1000 # prioritize prefixes, could be dropped params + ret -= 133*difflib.SequenceMatcher(a=other, b=sig).ratio() # prioritize on diff similarity + ret += 15*abs(len(other) - len(sig))/float(maxlen) # deprioritize the bigger the length difference is + for i in range(minlen): + if other[i] == sig[i]: ret -= 5/float(maxlen) # prioritize on identically-placed params + ret += 20*len(other) # deprioritize on length + return ret + sigs.sort(key=keyfunc) + for other in sigs: + if other != sig: + extra += other + ': " + debug_table_' + other + '[x] + " ' + extra += '"); ' + asm_setup += '\nfunction nullFunc_' + sig + '(x) { Module["printErr"]("Invalid function pointer' + pointer + 'called with signature \'' + sig + '\'. ' + \ + 'Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? ' + \ + 'Or calling a function with an incorrect type, which will fail? ' + \ + '(it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)' + \ + '"); ' + extra + ' abort(x) }\n' + + basic_vars = ['STACKTOP', 'STACK_MAX', 'tempDoublePtr', 'ABORT'] + basic_float_vars = ['NaN', 'Infinity'] + + if metadata.get('preciseI64MathUsed') or \ + forwarded_json['Functions']['libraryFunctions'].get('llvm_cttz_i32') or \ + forwarded_json['Functions']['libraryFunctions'].get('llvm_ctlz_i32'): + basic_vars += ['cttz_i8', 'ctlz_i8'] - basic_vars = ['STACKTOP', 'STACK_MAX', 'tempDoublePtr', 'ABORT'] - basic_float_vars = ['NaN', 'Infinity'] + if settings.get('DLOPEN_SUPPORT'): + for sig in last_forwarded_json['Functions']['tables'].iterkeys(): + basic_vars.append('F_BASE_%s' % sig) + asm_setup += ' var F_BASE_%s = %s;\n' % (sig, 'FUNCTION_TABLE_OFFSET' if settings.get('SIDE_MODULE') else '0') + '\n' - if metadata.get('preciseI64MathUsed') or \ - forwarded_json['Functions']['libraryFunctions'].get('llvm_cttz_i32') or \ - forwarded_json['Functions']['libraryFunctions'].get('llvm_ctlz_i32'): - basic_vars += ['cttz_i8', 'ctlz_i8'] + if '_rand' in exported_implemented_functions or '_srand' in exported_implemented_functions: + basic_vars += ['___rand_seed'] + + asm_runtime_funcs = ['stackAlloc', 'stackSave', 'stackRestore', 'setThrew', 'setTempRet0', 'getTempRet0'] + # function tables + function_tables = ['dynCall_' + table for table in last_forwarded_json['Functions']['tables']] + function_tables_impls = [] - if settings.get('DLOPEN_SUPPORT'): for sig in last_forwarded_json['Functions']['tables'].iterkeys(): - basic_vars.append('F_BASE_%s' % sig) - asm_setup += ' var F_BASE_%s = %s;\n' % (sig, 'FUNCTION_TABLE_OFFSET' if settings.get('SIDE_MODULE') else '0') + '\n' + args = ','.join(['a' + str(i) for i in range(1, len(sig))]) + arg_coercions = ' '.join(['a' + str(i) + '=' + shared.JS.make_coercion('a' + str(i), sig[i], settings) + ';' for i in range(1, len(sig))]) + coerced_args = ','.join([shared.JS.make_coercion('a' + str(i), sig[i], settings) for i in range(1, len(sig))]) + ret = ('return ' if sig[0] != 'v' else '') + shared.JS.make_coercion('FUNCTION_TABLE_%s[index&{{{ FTM_%s }}}](%s)' % (sig, sig, coerced_args), sig[0], settings) + function_tables_impls.append(''' + function dynCall_%s(index%s%s) { + index = index|0; + %s + %s; + } + ''' % (sig, ',' if len(sig) > 1 else '', args, arg_coercions, ret)) + + ffi_args = ','.join([shared.JS.make_coercion('a' + str(i), sig[i], settings, ffi_arg=True) for i in range(1, len(sig))]) + for i in range(settings['RESERVED_FUNCTION_POINTERS']): + jsret = ('return ' if sig[0] != 'v' else '') + shared.JS.make_coercion('jsCall(%d%s%s)' % (i, ',' if ffi_args else '', ffi_args), sig[0], settings, ffi_result=True) + function_tables_impls.append(''' + function jsCall_%s_%s(%s) { + %s + %s; + } + + ''' % (sig, i, args, arg_coercions, jsret)) + shared.Settings.copy(settings) + asm_setup += '\n' + shared.JS.make_invoke(sig) + '\n' + basic_funcs.append('invoke_%s' % sig) + if settings.get('DLOPEN_SUPPORT'): + asm_setup += '\n' + shared.JS.make_extcall(sig) + '\n' + basic_funcs.append('extCall_%s' % sig) + + # calculate exports + exported_implemented_functions = list(exported_implemented_functions) + metadata['initializers'] + exported_implemented_functions.append('runPostSets') + exports = [] + if not simple: + for export in exported_implemented_functions + asm_runtime_funcs + function_tables: + exports.append("%s: %s" % (export, export)) + exports = '{ ' + ', '.join(exports) + ' }' + else: + exports = '_main' + # calculate globals + try: + del forwarded_json['Variables']['globals']['_llvm_global_ctors'] # not a true variable + except: + pass + # If no named globals, only need externals + global_vars = metadata['externs'] #+ forwarded_json['Variables']['globals'] + global_funcs = list(set(['_' + key for key, value in forwarded_json['Functions']['libraryFunctions'].iteritems() if value != 2]).difference(set(global_vars)).difference(implemented_functions)) + def math_fix(g): + return g if not g.startswith('Math_') else g.split('_')[1] + asm_global_funcs = ''.join([' var ' + g.replace('.', '_') + '=global.' + g + ';\n' for g in maths]) + \ + ''.join([' var ' + g + '=env.' + math_fix(g) + ';\n' for g in basic_funcs + global_funcs]) + asm_global_vars = ''.join([' var ' + g + '=env.' + g + '|0;\n' for g in basic_vars + global_vars]) + # In linkable modules, we need to add some explicit globals for global variables that can be linked and used across modules + if settings.get('MAIN_MODULE') or settings.get('SIDE_MODULE'): + assert settings.get('TARGET_ASMJS_UNKNOWN_EMSCRIPTEN'), 'TODO: support x86 target when linking modules (needs offset of 4 and not 8 here)' + for key, value in forwarded_json['Variables']['globals'].iteritems(): + if value.get('linkable'): + init = forwarded_json['Variables']['indexedGlobals'][key] + 8 # 8 is Runtime.GLOBAL_BASE / STATIC_BASE + if settings.get('SIDE_MODULE'): init = '(H_BASE+' + str(init) + ')|0' + asm_global_vars += ' var %s=%s;\n' % (key, str(init)) + + # sent data + the_global = '{ ' + ', '.join(['"' + math_fix(s) + '": ' + s for s in fundamentals]) + ' }' + sending = '{ ' + ', '.join(['"' + math_fix(s) + '": ' + s for s in basic_funcs + global_funcs + basic_vars + basic_float_vars + global_vars]) + ' }' + # received + if not simple: + receiving = ';\n'.join(['var ' + s + ' = Module["' + s + '"] = asm["' + s + '"]' for s in exported_implemented_functions + function_tables]) + else: + receiving = 'var _main = Module["_main"] = asm;' - if '_rand' in exported_implemented_functions or '_srand' in exported_implemented_functions: - basic_vars += ['___rand_seed'] + # finalize - asm_runtime_funcs = ['stackAlloc', 'stackSave', 'stackRestore', 'setThrew', 'setTempRet0', 'getTempRet0'] - # function tables - function_tables = ['dynCall_' + table for table in last_forwarded_json['Functions']['tables']] - function_tables_impls = [] + if DEBUG: logging.debug('asm text sizes' + str([map(len, funcs_js), len(asm_setup), len(asm_global_vars), len(asm_global_funcs), len(pre_tables), len('\n'.join(function_tables_impls)), len(function_tables_defs.replace('\n', '\n ')), len(exports), len(the_global), len(sending), len(receiving)])) - for sig in last_forwarded_json['Functions']['tables'].iterkeys(): - args = ','.join(['a' + str(i) for i in range(1, len(sig))]) - arg_coercions = ' '.join(['a' + str(i) + '=' + shared.JS.make_coercion('a' + str(i), sig[i], settings) + ';' for i in range(1, len(sig))]) - coerced_args = ','.join([shared.JS.make_coercion('a' + str(i), sig[i], settings) for i in range(1, len(sig))]) - ret = ('return ' if sig[0] != 'v' else '') + shared.JS.make_coercion('FUNCTION_TABLE_%s[index&{{{ FTM_%s }}}](%s)' % (sig, sig, coerced_args), sig[0], settings) - function_tables_impls.append(''' - function dynCall_%s(index%s%s) { - index = index|0; - %s - %s; + funcs_js = [''' + %s + function asmPrintInt(x, y) { + Module.print('int ' + x + ',' + y);// + ' ' + new Error().stack); } -''' % (sig, ',' if len(sig) > 1 else '', args, arg_coercions, ret)) - - ffi_args = ','.join([shared.JS.make_coercion('a' + str(i), sig[i], settings, ffi_arg=True) for i in range(1, len(sig))]) - for i in range(settings['RESERVED_FUNCTION_POINTERS']): - jsret = ('return ' if sig[0] != 'v' else '') + shared.JS.make_coercion('jsCall(%d%s%s)' % (i, ',' if ffi_args else '', ffi_args), sig[0], settings, ffi_result=True) - function_tables_impls.append(''' - function jsCall_%s_%s(%s) { + function asmPrintFloat(x, y) { + Module.print('float ' + x + ',' + y);// + ' ' + new Error().stack); + } + // EMSCRIPTEN_START_ASM + var asm = (function(global, env, buffer) { %s - %s; + var HEAP8 = new global.Int8Array(buffer); + var HEAP16 = new global.Int16Array(buffer); + var HEAP32 = new global.Int32Array(buffer); + var HEAPU8 = new global.Uint8Array(buffer); + var HEAPU16 = new global.Uint16Array(buffer); + var HEAPU32 = new global.Uint32Array(buffer); + var HEAPF32 = new global.Float32Array(buffer); + var HEAPF64 = new global.Float64Array(buffer); + ''' % (asm_setup, "'use asm';" if not metadata.get('hasInlineJS') and not settings['SIDE_MODULE'] and settings['ASM_JS'] == 1 else "'almost asm';") + '\n' + asm_global_vars + ''' + var __THREW__ = 0; + var threwValue = 0; + var setjmpId = 0; + var undef = 0; + var nan = +env.NaN, inf = +env.Infinity; + 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] + [' var tempFloat = %s;\n' % ('Math_fround(0)' if settings.get('PRECISE_F32') else '0.0')] + ([' const f0 = Math_fround(0);\n'] if settings.get('PRECISE_F32') else []) + [''' + // EMSCRIPTEN_START_FUNCS + function stackAlloc(size) { + size = size|0; + var ret = 0; + ret = STACKTOP; + STACKTOP = (STACKTOP + size)|0; + ''' + ('STACKTOP = (STACKTOP + 3)&-4;' if settings['TARGET_X86'] else 'STACKTOP = (STACKTOP + 7)&-8;') + ''' + return ret|0; } - -''' % (sig, i, args, arg_coercions, jsret)) - shared.Settings.copy(settings) - asm_setup += '\n' + shared.JS.make_invoke(sig) + '\n' - basic_funcs.append('invoke_%s' % sig) - if settings.get('DLOPEN_SUPPORT'): - asm_setup += '\n' + shared.JS.make_extcall(sig) + '\n' - basic_funcs.append('extCall_%s' % sig) - - # calculate exports - exported_implemented_functions = list(exported_implemented_functions) + metadata['initializers'] - exported_implemented_functions.append('runPostSets') - exports = [] - if not simple: - for export in exported_implemented_functions + asm_runtime_funcs + function_tables: - exports.append("%s: %s" % (export, export)) - exports = '{ ' + ', '.join(exports) + ' }' - else: - exports = '_main' - # calculate globals - try: - del forwarded_json['Variables']['globals']['_llvm_global_ctors'] # not a true variable - except: - pass - # If no named globals, only need externals - global_vars = metadata['externs'] #+ forwarded_json['Variables']['globals'] - global_funcs = list(set(['_' + key for key, value in forwarded_json['Functions |