diff --git a/emcc b/emcc index 2a7e10d0fcad6..9849e881b55bd 100755 --- a/emcc +++ b/emcc @@ -53,8 +53,6 @@ from tools import shared from tools.shared import Compression, execute, suffix, unsuffixed, unsuffixed_basename from tools.response_file import read_response_file -logging = logging.getLogger('emcc') - # Mapping of emcc opt levels to llvm opt levels. We use llvm opt level 3 in emcc opt # levels 2 and 3 (emcc 3 is unsafe opts, so unsuitable for the only level to get # llvm opt level 3, and speed-wise emcc level 2 is already the slowest/most optimizing @@ -819,8 +817,12 @@ try: newargs[i] = '' elif newargs[i] == '-v': shared.COMPILER_OPTS += ['-v'] - DEBUG = 1 os.environ['EMCC_DEBUG'] = '1' # send to child processes too + if DEBUG != 1: + # swap in debug logging + DEBUG = 1 + shared.set_logging() + logging.debug('invocation: ' + ' '.join(sys.argv)) newargs[i] = '' elif newargs[i].startswith('--shell-file'): check_bad_eq(newargs[i]) diff --git a/emscripten.py b/emscripten.py index 248d0ce420929..d831285589007 100755 --- a/emscripten.py +++ b/emscripten.py @@ -127,6 +127,9 @@ def emscript(infile, settings, outfile, libraries=[], compiler_engine=None, 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 len(funcs) == 0: + raise RuntimeError('No functions to process. Make sure you prevented LLVM from eliminating them as dead (use EXPORTED_FUNCTIONS if necessary, see the FAQ)') + #if DEBUG: # print >> sys.stderr, '========= pre ================\n' # print >> sys.stderr, ''.join(pre) diff --git a/src/analyzer.js b/src/analyzer.js index 2cc46ab69823a..de9a7940b89c2 100644 --- a/src/analyzer.js +++ b/src/analyzer.js @@ -20,7 +20,7 @@ var BRANCH_INVOKE = set('branch', 'invoke'); var LABEL_ENDERS = set('branch', 'return', 'switch'); var SIDE_EFFECT_CAUSERS = set('call', 'invoke', 'atomic'); var UNUNFOLDABLE = set('value', 'structvalue', 'type', 'phiparam'); -var I64_DOUBLE_FLIP = { i64: 'double', double: 'i64' }; +var SHADOW_FLIP = { i64: 'double', double: 'i64' }; //, i32: 'float', float: 'i32' }; // Analyzer @@ -124,13 +124,13 @@ function analyzer(data, sidePass) { var lines = label.lines; for (var i = 0; i < lines.length; i++) { var line = lines[i]; - if (line.intertype == 'bitcast' && line.type in I64_DOUBLE_FLIP) { + if (line.intertype == 'bitcast' && line.type in SHADOW_FLIP) { has = true; } } }); if (!has) return; - // there are i64<-->double bitcasts, create shadows for everything + // there are integer<->floating-point bitcasts, create shadows for everything var shadowed = {}; func.labels.forEach(function(label) { var lines = label.lines; @@ -138,11 +138,11 @@ function analyzer(data, sidePass) { while (i < lines.length) { var lines = label.lines; var line = lines[i]; - if (line.intertype == 'load' && line.type in I64_DOUBLE_FLIP) { + if (line.intertype == 'load' && line.type in SHADOW_FLIP) { if (line.pointer.intertype != 'value') { i++; continue } // TODO shadowed[line.assignTo] = 1; var shadow = line.assignTo + '$$SHADOW'; - var flip = I64_DOUBLE_FLIP[line.type]; + var flip = SHADOW_FLIP[line.type]; lines.splice(i + 1, 0, { // if necessary this element will be legalized in the next phase tokens: null, indent: 2, @@ -171,7 +171,7 @@ function analyzer(data, sidePass) { var lines = label.lines; for (var i = 0; i < lines.length; i++) { var line = lines[i]; - if (line.intertype == 'bitcast' && line.type in I64_DOUBLE_FLIP && line.ident in shadowed) { + if (line.intertype == 'bitcast' && line.type in SHADOW_FLIP && line.ident in shadowed) { var shadow = line.ident + '$$SHADOW'; line.params[0].ident = shadow; line.params[0].type = line.type; diff --git a/src/headless.js b/src/headless.js index d81fb5a3639f3..097a42f753bd6 100644 --- a/src/headless.js +++ b/src/headless.js @@ -4,6 +4,20 @@ // TODO: sync from bananabread headless.js var window = { + eventListeners: {}, + addEventListener: function(id, func) { + var listeners = this.eventListeners[id]; + if (!listeners) { + listeners = this.eventListeners[id] = []; + } + listeners.push(func); + }, + callEventListeners: function(id) { + var listeners = this.eventListeners[id]; + if (listeners) { + listeners.forEach(function(listener) { listener() }); + } + }, location: { toString: function() { return '%s'; diff --git a/src/jsifier.js b/src/jsifier.js index 156fd65dee774..faef88d514a01 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -328,7 +328,7 @@ function JSify(data, functionsOnly, givenFunctions) { var js = (index !== null ? '' : item.ident + '=') + constant; if (js) js += ';'; - if (!ASM_JS && (EXPORT_ALL || (item.ident in EXPORTED_GLOBALS))) { + if (!ASM_JS && NAMED_GLOBALS && (EXPORT_ALL || (item.ident in EXPORTED_GLOBALS))) { js += '\nModule["' + item.ident + '"] = ' + item.ident + ';'; } if (BUILD_AS_SHARED_LIB == 2 && !item.private_) { diff --git a/src/library.js b/src/library.js index 2578fda44512e..f958a436a9bac 100644 --- a/src/library.js +++ b/src/library.js @@ -6354,7 +6354,8 @@ LibraryManager.library = { // Note that we need to emulate functions that use setjmp, and also to create // a new label we can return to. Emulation make such functions slower, this // can be alleviated by making a new function containing just the setjmp - // related functionality so the slowdown is more limited. + // related functionality so the slowdown is more limited - you may need + // to prevent inlining to keep this isolated, try __attribute__((noinline)) // ========================================================================== saveSetjmp__asm: true, @@ -6374,11 +6375,11 @@ LibraryManager.library = { setjmpId = (setjmpId+1)|0; {{{ makeSetValueAsm('env', '0', 'setjmpId', 'i32') }}}; while ((i|0) < {{{ 2*MAX_SETJMPS }}}) { - if ({{{ makeGetValueAsm('table', 'i*4', 'i32') }}} == 0) { - {{{ makeSetValueAsm('table', 'i*4', 'setjmpId', 'i32') }}}; - {{{ makeSetValueAsm('table', 'i*4+4', 'label', 'i32') }}}; + if ({{{ makeGetValueAsm('table', '(i<<2)', 'i32') }}} == 0) { + {{{ makeSetValueAsm('table', '(i<<2)', 'setjmpId', 'i32') }}}; + {{{ makeSetValueAsm('table', '(i<<2)+4', 'label', 'i32') }}}; // prepare next slot - {{{ makeSetValueAsm('table', 'i*4+8', '0', 'i32') }}}; + {{{ makeSetValueAsm('table', '(i<<2)+8', '0', 'i32') }}}; return 0; } i = (i+2)|0; @@ -6395,10 +6396,10 @@ LibraryManager.library = { table = table|0; var i = 0, curr = 0; while ((i|0) < {{{ MAX_SETJMPS }}}) { - curr = {{{ makeGetValueAsm('table', 'i*4', 'i32') }}}; + curr = {{{ makeGetValueAsm('table', '(i<<2)', 'i32') }}}; if ((curr|0) == 0) break; if ((curr|0) == (id|0)) { - return {{{ makeGetValueAsm('table', 'i*4+4', 'i32') }}}; + return {{{ makeGetValueAsm('table', '(i<<2)+4', 'i32') }}}; } i = (i+2)|0; } @@ -7668,8 +7669,8 @@ LibraryManager.library = { nfds = Math.min(64, nfds); // fd sets have 64 bits for (var fd = 0; fd < nfds; fd++) { - var mask = 1 << (fd % 32), int = fd < 32 ? srcLow : srcHigh; - if (int & mask) { + var mask = 1 << (fd % 32), int_ = fd < 32 ? srcLow : srcHigh; + if (int_ & mask) { // index is in the set, check if it is ready for read var info = FS.streams[fd]; if (info && can(info)) { @@ -8057,8 +8058,8 @@ LibraryManager.library = { nfds = Math.min(64, nfds); // fd sets have 64 bits for (var fd = 0; fd < nfds; fd++) { - var mask = 1 << (fd % 32), int = fd < 32 ? srcLow : srcHigh; - if (int & mask) { + var mask = 1 << (fd % 32), int_ = fd < 32 ? srcLow : srcHigh; + if (int_ & mask) { // index is in the set, check if it is ready for read var info = FS.streams[fd]; if (info && can(info)) { diff --git a/src/library_sdl.js b/src/library_sdl.js index 1c8ac52d9799a..b2fea43a959a0 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -47,6 +47,7 @@ var LibrarySDL = { startTime: null, buttonState: 0, + modState: 0, DOMButtons: [0, 0, 0], DOMEventToSDLEvent: {}, @@ -373,7 +374,8 @@ var LibrarySDL = { if (event['movementX'] == 0 && event['movementY'] == 0) { // ignore a mousemove event if it doesn't contain any movement info // (without pointer lock, we infer movement from pageX/pageY, so this check is unnecessary) - return false; + event.preventDefault(); + return; } } // fall through @@ -396,15 +398,20 @@ var LibrarySDL = { } else if (event.type == 'mousedown') { SDL.DOMButtons[event.button] = 1; } else if (event.type == 'mouseup') { - if (!SDL.DOMButtons[event.button]) return false; // ignore extra ups, can happen if we leave the canvas while pressing down, then return, - // since we add a mouseup in that case + // ignore extra ups, can happen if we leave the canvas while pressing down, then return, + // since we add a mouseup in that case + if (!SDL.DOMButtons[event.button]) { + event.preventDefault(); + return; + } + SDL.DOMButtons[event.button] = 0; } if (event.type == 'keypress' && !SDL.textInput) { break; } - + SDL.events.push(event); break; case 'mouseout': @@ -438,7 +445,7 @@ var LibrarySDL = { // Force-run a main event loop, since otherwise this event will never be caught! Browser.mainLoop.runner(); } - return true; + return; case 'resize': SDL.events.push(event); break; @@ -447,7 +454,11 @@ var LibrarySDL = { Module.printErr('SDL event queue full, dropping events'); SDL.events = SDL.events.slice(0, 10000); } - return false; + // manually triggered resize event doesn't have a preventDefault member + if (event.preventDefault) { + event.preventDefault(); + } + return; }, makeCEvent: function(event, ptr) { @@ -473,15 +484,6 @@ var LibrarySDL = { } else { scan = SDL.scanCodes[key] || key; } - {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.type', 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}} - //{{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.which', '1', 'i32') }}} - {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.state', 'down ? 1 : 0', 'i8') }}} - {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.repeat', '0', 'i8') }}} // TODO - - {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.scancode', 'scan', 'i32') }}} - {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.sym', 'key', 'i32') }}} - {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.mod', '0', 'i32') }}} - {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.unicode', 'key', 'i32') }}} var code = SDL.keyCodes[event.keyCode] || event.keyCode; {{{ makeSetValue('SDL.keyboardState', 'code', 'down', 'i8') }}}; @@ -491,6 +493,19 @@ var LibrarySDL = { delete SDL.keyboardMap[code]; } + // TODO: lmeta, rmeta, numlock, capslock, KMOD_MODE, KMOD_RESERVED + SDL.modState = ({{{ makeGetValue('SDL.keyboardState', '1248', 'i8') }}} ? 0x0040 | 0x0080 : 0) | // KMOD_LCTRL & KMOD_RCTRL + ({{{ makeGetValue('SDL.keyboardState', '1249', 'i8') }}} ? 0x0001 | 0x0002 : 0) | // KMOD_LSHIFT & KMOD_RSHIFT + ({{{ makeGetValue('SDL.keyboardState', '1250', 'i8') }}} ? 0x0100 | 0x0200 : 0); // KMOD_LALT & KMOD_RALT + + {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.type', 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}} + {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.state', 'down ? 1 : 0', 'i8') }}} + {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.repeat', '0', 'i8') }}} // TODO + {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.scancode', 'scan', 'i32') }}} + {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.sym', 'key', 'i32') }}} + {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.mod', 'SDL.modState', 'i32') }}} + {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.unicode', 'key', 'i32') }}} + break; } case 'keypress': { @@ -614,13 +629,13 @@ var LibrarySDL = { SDL.startTime = Date.now(); // capture all key events. we just keep down and up, but also capture press to prevent default actions if (!Module['doNotCaptureKeyboard']) { - document.onkeydown = SDL.receiveEvent; - document.onkeyup = SDL.receiveEvent; - document.onkeypress = SDL.receiveEvent; - document.onblur = SDL.receiveEvent; + document.addEventListener("keydown", SDL.receiveEvent); + document.addEventListener("keyup", SDL.receiveEvent); + document.addEventListener("keypress", SDL.receiveEvent); + document.addEventListener("blur", SDL.receiveEvent); document.addEventListener("visibilitychange", SDL.receiveEvent); } - window.onunload = SDL.receiveEvent; + window.addEventListener("unload", SDL.receiveEvent); SDL.keyboardState = _malloc(0x10000); // Our SDL needs 512, but 64K is safe for older SDLs _memset(SDL.keyboardState, 0, 0x10000); // Initialize this structure carefully for closure @@ -888,12 +903,16 @@ var LibrarySDL = { SDL_GetKeyState: function() { return _SDL_GetKeyboardState(); }, + + SDL_GetKeyName: function(key) { + if (!SDL.keyName) { + SDL.keyName = allocate(intArrayFromString('unknown key'), 'i8', ALLOC_NORMAL); + } + return SDL.keyName; + }, SDL_GetModState: function() { - // TODO: numlock, capslock, etc. - return (SDL.keyboardState[16] ? 0x0001 | 0x0002 : 0) | // KMOD_LSHIFT & KMOD_RSHIFT - (SDL.keyboardState[17] ? 0x0040 | 0x0080 : 0) | // KMOD_LCTRL & KMOD_RCTRL - (SDL.keyboardState[18] ? 0x0100 | 0x0200 : 0); // KMOD_LALT & KMOD_RALT + return SDL.modState; }, SDL_GetMouseState: function(x, y) { @@ -1103,6 +1122,20 @@ var LibrarySDL = { return (a&0xff)+((b&0xff)<<8)+((g&0xff)<<16)+((r&0xff)<<24) }, + SDL_GetAppState: function() { + var state = 0; + + if (Browser.pointerLock) { + state |= 0x01; // SDL_APPMOUSEFOCUS + } + if (document.hasFocus()) { + state |= 0x02; // SDL_APPINPUTFOCUS + } + state |= 0x04; // SDL_APPACTIVE + + return state; + }, + SDL_WM_GrabInput: function() {}, SDL_WM_ToggleFullScreen: function(surf) { @@ -1755,6 +1788,10 @@ var LibrarySDL = { return -1; }, + SDL_SetGammaRamp: function (redTable, greenTable, blueTable) { + return -1; + }, + // Misc SDL_InitSubSystem: function(flags) { return 0 }, @@ -1773,7 +1810,7 @@ var LibrarySDL = { SDL_AddTimer: function(interval, callback, param) { return window.setTimeout(function() { - Runtime.dynCall('ii', callback, [interval, param]); + Runtime.dynCall('iii', callback, [interval, param]); }, interval); }, SDL_RemoveTimer: function(id) { @@ -1792,7 +1829,7 @@ var LibrarySDL = { SDL_FreeRW: function() { throw 'SDL_FreeRW: TODO' }, SDL_CondBroadcast: function() { throw 'SDL_CondBroadcast: TODO' }, SDL_CondWaitTimeout: function() { throw 'SDL_CondWaitTimeout: TODO' }, - SDL_WM_ToggleFullScreen: function() { throw 'SDL_WM_ToggleFullScreen: TODO' }, + SDL_WM_IconifyWindow: function() { throw 'SDL_WM_IconifyWindow TODO' }, Mix_SetPostMix: function() { throw 'Mix_SetPostMix: TODO' }, Mix_QuerySpec: function() { throw 'Mix_QuerySpec: TODO' }, @@ -1802,6 +1839,16 @@ var LibrarySDL = { Mix_Linked_Version: function() { throw 'Mix_Linked_Version: TODO' }, SDL_CreateRGBSurfaceFrom: function() { throw 'SDL_CreateRGBSurfaceFrom: TODO' }, SDL_SaveBMP_RW: function() { throw 'SDL_SaveBMP_RW: TODO' }, + + SDL_WM_SetIcon: function() { /* This function would set the application window icon surface, which doesn't apply for web canvases, so a no-op. */ }, + SDL_HasRDTSC: function() { return 0; }, + SDL_HasMMX: function() { return 0; }, + SDL_HasMMXExt: function() { return 0; }, + SDL_Has3DNow: function() { return 0; }, + SDL_Has3DNowExt: function() { return 0; }, + SDL_HasSSE: function() { return 0; }, + SDL_HasSSE2: function() { return 0; }, + SDL_HasAltiVec: function() { return 0; } }; autoAddDeps(LibrarySDL, '$SDL'); diff --git a/src/modules.js b/src/modules.js index e40930788dc8a..b13ab3c508dd5 100644 --- a/src/modules.js +++ b/src/modules.js @@ -341,6 +341,7 @@ var Functions = { call += (j > 1 ? ',' : '') + asmCoercion('a' + j, t[j] != 'i' ? 'float' : 'i32'); } call += ')'; + if (curr == '_setjmp') printErr('WARNING: setjmp used via a function pointer. If this is for libc setjmp (not something of your own with the same name), it will break things'); tables.pre += 'function ' + curr + '__wrapper(' + args + ') { ' + arg_coercions + ' ; ' + retPre + call + retPost + ' }\n'; wrapped[curr] = 1; } diff --git a/src/parseTools.js b/src/parseTools.js index 3949491edde4c..687faaa8820bd 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -2204,7 +2204,7 @@ function processMathop(item) { case 'sub': return handleOverflow(getFastValue(idents[0], '-', idents[1], item.type), bits); case 'sdiv': case 'udiv': return makeRounding(getFastValue(idents[0], '/', idents[1], item.type), bits, op[0] === 's'); case 'mul': return getFastValue(idents[0], '*', idents[1], item.type); // overflow handling is already done in getFastValue for '*' - case 'urem': case 'srem': return getFastValue(idents[0], '%', idents[1], item.type); + case 'urem': case 'srem': return makeRounding(getFastValue(idents[0], '%', idents[1], item.type), bits, op[0] === 's'); case 'or': { if (bits > 32) { assert(bits === 64, 'Too many bits for or: ' + bits); diff --git a/src/relooper/Relooper.cpp b/src/relooper/Relooper.cpp index 8a6e18b8e5296..7ceeb2f829f8e 100644 --- a/src/relooper/Relooper.cpp +++ b/src/relooper/Relooper.cpp @@ -897,6 +897,7 @@ void Relooper::Calculate(Block *Entry) { BlockSet Entries; Entries.insert(Entry); Root = Analyzer(this).Process(AllBlocks, Entries, NULL); + assert(Root); // Post optimizations @@ -1091,6 +1092,7 @@ void Relooper::Calculate(Block *Entry) { void Relooper::Render() { OutputBuffer = OutputBufferRoot; + assert(Root); Root->Render(false); } diff --git a/src/settings.js b/src/settings.js index 7e6079966bad3..3a91b4886fa78 100644 --- a/src/settings.js +++ b/src/settings.js @@ -245,7 +245,8 @@ var EXPORTED_FUNCTIONS = ['_main', '_malloc']; // through LLVM dead code elimination, and also made accessible outside of // the generated code even after running closure compiler (on "Module"). // Note the necessary prefix of "_". -var EXPORT_ALL = 0; // If true, we export all the symbols +var EXPORT_ALL = 0; // If true, we export all the symbols. Note that this does *not* affect LLVM, so it can + // still eliminate functions as dead. This just exports them on the Module object. var EXPORT_BINDINGS = 0; // Export all bindings generator functions (prefixed with emscripten_bind_). This // is necessary to use the bindings generator with asm.js diff --git a/system/include/libc/sys/dirent.h b/system/include/libc/sys/dirent.h index 9dcf34d1ec72c..0d8b02b5fbb1c 100644 --- a/system/include/libc/sys/dirent.h +++ b/system/include/libc/sys/dirent.h @@ -34,8 +34,22 @@ int scandir(const char *dirp, enum { DT_UNKNOWN = 0, #define DT_UNKNOWN DT_UNKNOWN - DT_DIR = 4 + DT_FIFO = 1, +#define DT_FIFO DT_FIFO + DT_CHR = 2, +#define DT_CHR DT_CHR + DT_DIR = 4, #define DT_DIR DT_DIR + DT_BLK = 6, +#define DT_BLK DT_BLK + DT_REG = 8, +#define DT_REG DT_REG + DT_LNK = 10, +#define DT_LNK DT_LNK + DT_SOCK = 12, +#define DT_SOCK DT_SOCK + DT_WHT = 14 +#define DT_WHT DT_WHT }; #ifdef __cplusplus diff --git a/system/include/sys/socket.h b/system/include/sys/socket.h index 56a37375247e2..b83ce89a9c94d 100644 --- a/system/include/sys/socket.h +++ b/system/include/sys/socket.h @@ -44,7 +44,7 @@ typedef unsigned int sa_family_t; struct sockaddr { sa_family_t sa_family; - char sa_data[]; + char sa_data[16]; }; struct sockaddr_storage { diff --git a/tests/runner.py b/tests/runner.py index 9214ae2d520c4..73dbab135b430 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -126,15 +126,6 @@ def get_dir(self): def in_dir(self, *pathelems): return os.path.join(self.get_dir(), *pathelems) - def get_shared_library_name(self, linux_name): - if platform.system() == 'Linux': - return linux_name - elif platform.system() == 'Darwin': - return linux_name.replace('.so', '') + '.dylib' - else: - print >> sys.stderr, 'get_shared_library_name needs to be implemented on %s' % platform.system() - return linux_name - def get_stdout_path(self): return os.path.join(self.get_dir(), 'stdout') @@ -8422,7 +8413,7 @@ def process(filename): [os.path.sep.join('codec/CMakeFiles/j2k_to_image.dir/index.c.o'.split('/')), os.path.sep.join('codec/CMakeFiles/j2k_to_image.dir/convert.c.o'.split('/')), os.path.sep.join('codec/CMakeFiles/j2k_to_image.dir/__/common/color.c.o'.split('/')), - os.path.join('bin', self.get_shared_library_name('libopenjpeg.so.1.4.0'))], + os.path.join('bin', 'libopenjpeg.so.1.4.0')], configure=['cmake', '.'], #configure_args=['--enable-tiff=no', '--enable-jp3d=no', '--enable-png=no'], make_args=[]) # no -j 2, since parallel builds can fail @@ -10505,6 +10496,24 @@ def test_redundant_link(self): self.assertContained('result: 1', run_js(os.path.join(self.get_dir(), 'a.out.js'))) + def test_export_all(self): + lib = r''' + #include + void libf1() { printf("libf1\n"); } + void libf2() { printf("libf2\n"); } + ''' + lib_name = os.path.join(self.get_dir(), 'lib.c') + open(lib_name, 'w').write(lib) + + open('main.js', 'w').write(''' + _libf1(); + _libf2(); + ''') + + Building.emcc(lib_name, ['-s', 'EXPORT_ALL=1', '--post-js', 'main.js'], output_filename='a.out.js') + + self.assertContained('libf1\nlibf2\n', run_js(os.path.join(self.get_dir(), 'a.out.js'))) + def test_abspaths(self): # Includes with absolute paths are generally dangerous, things like -I/usr/.. will get to system local headers, not our portable ones. @@ -12121,23 +12130,26 @@ def test_sdl_key(self): setTimeout(doOne, 1000/60); } - function simulateKeyEvent(c) { + function keydown(c) { var event = document.createEvent("KeyboardEvent"); event.initKeyEvent("keydown", true, true, window, 0, 0, 0, 0, c, c); document.dispatchEvent(event); - var event2 = document.createEvent("KeyboardEvent"); - event2.initKeyEvent("keyup", true, true, window, + } + + function keyup(c) { + var event = document.createEvent("KeyboardEvent"); + event.initKeyEvent("keyup", true, true, window, 0, 0, 0, 0, c, c); - document.dispatchEvent(event2); + document.dispatchEvent(event); } ''') open(os.path.join(self.get_dir(), 'sdl_key.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_key.c')).read())) Popen([PYTHON, EMCC, os.path.join(self.get_dir(), 'sdl_key.c'), '-o', 'page.html', '--pre-js', 'pre.js', '-s', '''EXPORTED_FUNCTIONS=['_main', '_one']''']).communicate() - self.run_browser('page.html', '', '/report_result?510510') + self.run_browser('page.html', '', '/report_result?223092870') def test_sdl_text(self): open(os.path.join(self.get_dir(), 'pre.js'), 'w').write(''' @@ -13537,6 +13549,7 @@ def output_parser(output): self.lua('scimark', '[small problem sizes]', output_parser=output_parser) def test_zzz_lua_binarytrees(self): + # js version: ['binarytrees.lua', {0: 0, 1: 9.5, 2: 11.99, 3: 12.85, 4: 14.72, 5: 15.82}[arguments[0]]] def args_processor(args): arg = int(DEFAULT_ARG) if arg == 0: @@ -13989,7 +14002,7 @@ def test_relooper(self): assert os.path.exists(RELOOPER) == (i >= 2), 'have relooper on O2: ' + output src = open('a.out.js').read() main = src.split('function _main()')[1].split('\n}\n')[0] - assert ('while (1) {' in main or 'while(1){' in main) == (i >= 2), 'reloop code on O2: ' + main + assert ('while (1) {' in main or 'while(1){' in main or '} while ($' in main or '}while($' in main) == (i >= 2), 'reloop code on O2: ' + main assert ('switch' not in main) == (i >= 2), 'reloop code on O2: ' + main def test_jcache(self): diff --git a/tests/sdl_key.c b/tests/sdl_key.c index 19b0a3d6bf83b..7a304fc1dd780 100644 --- a/tests/sdl_key.c +++ b/tests/sdl_key.c @@ -12,16 +12,30 @@ void one() { case SDL_KEYDOWN: break; case SDL_KEYUP: + // don't handle the modifier key events + if (event.key.keysym.sym == SDLK_LCTRL || + event.key.keysym.sym == SDLK_LSHIFT || + event.key.keysym.sym == SDLK_LALT) { + return; + } + if ((event.key.keysym.mod & KMOD_LCTRL) || (event.key.keysym.mod & KMOD_RCTRL)) { + result *= 2; + } + if ((event.key.keysym.mod & KMOD_LSHIFT) || (event.key.keysym.mod & KMOD_RSHIFT)) { + result *= 3; + } + if ((event.key.keysym.mod & KMOD_LALT) || (event.key.keysym.mod & KMOD_RALT)) { + result *= 5; + } switch (event.key.keysym.sym) { - case SDLK_RIGHT: printf("right\n"); result *= 2; break; - case SDLK_LEFT: printf("left\n"); result *= 3; break; - case SDLK_DOWN: printf("down\n"); result *= 5; break; - case SDLK_UP: printf("up\n"); result *= 7; break; - case SDLK_SPACE: printf("space\n"); result *= 11; break; - case SDLK_a: printf("a\n"); result *= 13; break; + case SDLK_RIGHT: printf("right\n"); result *= 7; break; + case SDLK_LEFT: printf("left\n"); result *= 11; break; + case SDLK_DOWN: printf("down\n"); result *= 13; break; + case SDLK_UP: printf("up\n"); result *= 17; break; + case SDLK_a: printf("a\n"); result *= 19; break; default: { if (event.key.keysym.scancode == SDL_SCANCODE_B) { - printf("b scancode\n"); result *= 17; break; + printf("b scancode\n"); result *= 23; break; } printf("unknown key: sym %d scancode %d\n", event.key.keysym.sym, event.key.keysym.scancode); REPORT_RESULT(); @@ -40,14 +54,13 @@ int main(int argc, char **argv) { SDL_Init(SDL_INIT_VIDEO); SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE); - emscripten_run_script("simulateKeyEvent(38)"); // up - emscripten_run_script("simulateKeyEvent(40)"); // down - emscripten_run_script("simulateKeyEvent(37)"); // left - emscripten_run_script("simulateKeyEvent(39)"); // right - emscripten_run_script("simulateKeyEvent(32)"); // space - emscripten_run_script("simulateKeyEvent(65)"); // a - emscripten_run_script("simulateKeyEvent(66)"); // b - emscripten_run_script("simulateKeyEvent(100)"); // trigger the end + emscripten_run_script("keydown(1250);keydown(38);keyup(38);keyup(1250);"); // alt, up + emscripten_run_script("keydown(1248);keydown(1249);keydown(40);keyup(40);keyup(1249);keyup(1248);"); // ctrl, shift, down + emscripten_run_script("keydown(37);keyup(37);"); // left + emscripten_run_script("keydown(39);keyup(39);"); // right + emscripten_run_script("keydown(65);keyup(65);"); // a + emscripten_run_script("keydown(66);keyup(66);"); // b + emscripten_run_script("keydown(100);keyup(100);"); // trigger the end if (argc == 1337) one(); // keep it alive diff --git a/tools/eliminator/asm-eliminator-test-output.js b/tools/eliminator/asm-eliminator-test-output.js index 52c91ce1a4652..2ce77b789124d 100644 --- a/tools/eliminator/asm-eliminator-test-output.js +++ b/tools/eliminator/asm-eliminator-test-output.js @@ -5094,4 +5094,18 @@ function multiloop2($n_0, $35) { } } } +function tempDouble2($46, $14, $28, $42, $20, $32, $45) { + $46 = $46 | 0; + $14 = $14 | 0; + $28 = $28 | 0; + $42 = $42 | 0; + $20 = $20 | 0; + $32 = $32 | 0; + $45 = $45 | 0; + var $_sroa_06_0_insert_insert$1 = 0; + $_sroa_06_0_insert_insert$1 = (HEAPF32[tempDoublePtr >> 2] = ($20 < $32 ? $20 : $32) - $42, HEAP32[tempDoublePtr >> 2] | 0) | 0; + HEAP32[$45 >> 2] = 0 | (HEAPF32[tempDoublePtr >> 2] = ($14 < $28 ? $14 : $28) - $42, HEAP32[tempDoublePtr >> 2] | 0); + HEAP32[$45 + 4 >> 2] = $_sroa_06_0_insert_insert$1; + HEAP32[$45 + 8 >> 2] = $_sroa_06_0_insert_insert$1; +} diff --git a/tools/eliminator/asm-eliminator-test.js b/tools/eliminator/asm-eliminator-test.js index e01a89ea4fca0..f45f082bf4fca 100644 --- a/tools/eliminator/asm-eliminator-test.js +++ b/tools/eliminator/asm-eliminator-test.js @@ -6823,5 +6823,20 @@ function multiloop2($n_0, $35) { } } } -// EMSCRIPTEN_GENERATED_FUNCTIONS: ["asm", "__Z11printResultPiS_j", "_segment_holding", "__ZN5identC2EiPKcPci", "_vec2Length", "exc", "label", "confuusion", "tempDouble", "_org_apache_harmony_luni_util_NumberConverter_freeFormat__", "__ZN23b2EdgeAndPolygonContact8EvaluateEP10b2ManifoldRK11b2TransformS4_", "_java_nio_charset_Charset_forNameInternal___java_lang_String", "looop2", "looop3", "looop4", "looop5", "looop6", "looop7", "multiloop", "multiloop2"] +function tempDouble2($46, $14, $28, $42, $20, $32, $45) { + $46 = $46 | 0; + $14 = $14 | 0; + $28 = $28 | 0; + $42 = $42 | 0; + $20 = $20 | 0; + $32 = $32 | 0; + $45 = $45 | 0; + var $46 = 0, $_sroa_06_0_insert_insert$1 = 0; + $46 = (HEAPF32[tempDoublePtr >> 2] = ($14 < $28 ? $14 : $28) - $42, HEAP32[tempDoublePtr >> 2] | 0); + $_sroa_06_0_insert_insert$1 = (HEAPF32[tempDoublePtr >> 2] = ($20 < $32 ? $20 : $32) - $42, HEAP32[tempDoublePtr >> 2] | 0) | 0; + HEAP32[$45 >> 2] = 0 | $46; + HEAP32[$45 + 4 >> 2] = $_sroa_06_0_insert_insert$1; + HEAP32[$45 + 8 >> 2] = $_sroa_06_0_insert_insert$1; +} +// EMSCRIPTEN_GENERATED_FUNCTIONS: ["asm", "__Z11printResultPiS_j", "_segment_holding", "__ZN5identC2EiPKcPci", "_vec2Length", "exc", "label", "confuusion", "tempDouble", "_org_apache_harmony_luni_util_NumberConverter_freeFormat__", "__ZN23b2EdgeAndPolygonContact8EvaluateEP10b2ManifoldRK11b2TransformS4_", "_java_nio_charset_Charset_forNameInternal___java_lang_String", "looop2", "looop3", "looop4", "looop5", "looop6", "looop7", "multiloop", "multiloop2", "tempDouble2"] diff --git a/tools/file_packager.py b/tools/file_packager.py index 6a8390ad17101..1443d1658e0fb 100644 --- a/tools/file_packager.py +++ b/tools/file_packager.py @@ -240,7 +240,7 @@ def was_seen(name): #if not os.path.exists(os.path.basename(crunch_name)): # print >> sys.stderr, 'Failed to crunch, perhaps a weird dxt format? Looking for a source PNG for the DDS' # Popen([CRUNCH, '-file', unsuffixed(file_['srcpath']) + '.png', '-quality', crunch] + format, stdout=sys.stderr).communicate() - assert os.path.exists(os.path.basename(src_crunch_name)), 'crunch failed to generate output' + assert os.path.exists(src_crunch_name), 'crunch failed to generate output' # prepend the dds header crunched = open(src_crunch_name, 'rb').read() c = open(src_crunch_name, 'wb') diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index 7e1d374fa08f0..07317e0a22d69 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -402,10 +402,12 @@ function removeUnneededLabelSettings(ast) { // Various expression simplifications. Pre run before closure (where we still have metadata), Post run after. +var USEFUL_BINARY_OPS = set('<<', '>>', '|', '&', '^'); + function simplifyExpressionsPre(ast) { // Look for (x&A)<>B and replace it with X&A if possible. function simplifySignExtends(ast) { - traverseGenerated(ast, function(node, type) { + traverse(ast, function(node, type) { if (type == 'binary' && node[1] == '>>' && node[3][0] == 'num' && node[2][0] == 'binary' && node[2][1] == '<<' && node[2][3][0] == 'num' && node[3][1] == node[2][3][1]) { var innerNode = node[2][2]; @@ -427,13 +429,12 @@ function simplifyExpressionsPre(ast) { // 'useful' mathops already |0 anyhow. function simplifyBitops(ast) { - var USEFUL_BINARY_OPS = set('<<', '>>', '|', '&', '^'); var SAFE_BINARY_OPS = set('+', '-', '*'); // division is unsafe as it creates non-ints in JS; mod is unsafe as signs matter so we can't remove |0's var ZERO = ['num', 0]; 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]]; @@ -479,8 +480,12 @@ function simplifyExpressionsPre(ast) { return true; } - traverseGenerated(ast, function(node, type) { - if (type == 'binary' && node[1] == '&' && node[3][0] == 'num') { + var hasTempDoublePtr = false; + + traverse(ast, function(node, type) { + if (type == 'name') { + if (node[1] == 'tempDoublePtr') hasTempDoublePtr = true; + } else 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]; var amount = node[3][1]; @@ -522,12 +527,133 @@ function simplifyExpressionsPre(ast) { return node; } } + } else if (type == 'assign') { + // optimizations for assigning into HEAP32 specifically + if (node[1] === true && node[2][0] == 'sub' && node[2][1][0] == 'name' && node[2][1][1] == 'HEAP32') { + // HEAP32[..] = x | 0 does not need the | 0 (unless it is a mandatory |0 of a call) + if (node[3][0] == 'binary' && node[3][1] == '|') { + if (node[3][2][0] == 'num' && node[3][2][1] == 0 && node[3][3][0] != 'call') { + node[3] = node[3][3]; + } else if (node[3][3][0] == 'num' && node[3][3][1] == 0 && node[3][2][0] != 'call') { + node[3] = node[3][2]; + } + } + } + var value = node[3]; + if (value[0] == 'binary' && value[1] == '|') { + // canonicalize order of |0 to end + if (value[2][0] == 'num' && value[2][1] == 0) { + var temp = value[2]; + value[2] = value[3]; + value[3] = temp; + } + // if a seq ends in an |0, remove an external |0 + // note that it is only safe to do this in assigns, like we are doing here (return (x, y|0); is not valid) + if (value[2][0] == 'seq' && value[2][2][0] == 'binary' && value[2][2][1] in USEFUL_BINARY_OPS) { + node[3] = value[2]; + } + } } }); if (asm) { + if (hasTempDoublePtr) { + traverse(ast, function(node, type) { + if (type == 'assign') { + if (node[1] === true && node[2][0] == 'sub' && node[2][1][0] == 'name' && node[2][1][1] == 'HEAP32') { + // remove bitcasts that are now obviously pointless, e.g. + // HEAP32[$45 >> 2] = HEAPF32[tempDoublePtr >> 2] = ($14 < $28 ? $14 : $28) - $42, HEAP32[tempDoublePtr >> 2] | 0; + var value = node[3]; + if (value[0] == 'seq' && value[1][0] == 'assign' && value[1][2][0] == 'sub' && value[1][2][1][0] == 'name' && value[1][2][1][1] == 'HEAPF32' && + value[1][2][2][0] == 'binary' && value[1][2][2][2][0] == 'name' && value[1][2][2][2][1] == 'tempDoublePtr') { + // transform to HEAPF32[$45 >> 2] = ($14 < $28 ? $14 : $28) - $42; + node[2][1][1] = 'HEAPF32'; + node[3] = value[1][3]; + } + } + } else if (type == 'seq') { + // (HEAP32[tempDoublePtr >> 2] = HEAP32[$37 >> 2], +HEAPF32[tempDoublePtr >> 2]) + // ==> + // +HEAPF32[$37 >> 2] + if (node[0] == 'seq' && node[1][0] == 'assign' && node[1][2][0] == 'sub' && node[1][2][1][0] == 'name' && + (node[1][2][1][1] == 'HEAP32' || node[1][2][1][1] == 'HEAPF32') && + node[1][2][2][0] == 'binary' && node[1][2][2][2][0] == 'name' && node[1][2][2][2][1] == 'tempDoublePtr' && + node[1][3][0] == 'sub' && node[1][3][1][0] == 'name' && (node[1][3][1][1] == 'HEAP32' || node[1][3][1][1] == 'HEAPF32')) { + if (node[1][2][1][1] == 'HEAP32') { + node[1][3][1][1] = 'HEAPF32'; + return ['unary-prefix', '+', node[1][3]]; + } else { + node[1][3][1][1] = 'HEAP32'; + return ['binary', '|', node[1][3], ['num', 0]]; + } + } + } + }); + + // finally, wipe out remaining ones by finding cases where all assignments to X are bitcasts, and all uses are writes to + // the other heap type, then eliminate the bitcast + var bitcastVars = {}; + traverse(ast, function(node, type) { + if (type == 'assign' && node[1] === true && node[2][0] == 'name') { + var value = node[3]; + if (value[0] == 'seq' && value[1][0] == 'assign' && value[1][2][0] == 'sub' && value[1][2][1][0] == 'name' && + (value[1][2][1][1] == 'HEAP32' || value[1][2][1][1] == 'HEAPF32') && + value[1][2][2][0] == 'binary' && value[1][2][2][2][0] == 'name' && value[1][2][2][2][1] == 'tempDoublePtr') { + var name = node[2][1]; + if (!bitcastVars[name]) bitcastVars[name] = { + define_HEAP32: 0, define_HEAPF32: 0, use_HEAP32: 0, use_HEAPF32: 0, bad: false, namings: 0, defines: [], uses: [] + }; + bitcastVars[name]['define_' + value[1][2][1][1]]++; + bitcastVars[name].defines.push(node); + } + } + }); + traverse(ast, function(node, type) { + if (type == 'name' && bitcastVars[node[1]]) { + bitcastVars[node[1]].namings++; + } else if (type == 'assign' && node[1] === true) { + var value = node[3]; + if (value[0] == 'name') { + var name = value[1]; + if (bitcastVars[name]) { + var target = node[2]; + if (target[0] == 'sub' && target[1][0] == 'name' && (target[1][1] == 'HEAP32' || target[1][1] == 'HEAPF32')) { + bitcastVars[name]['use_' + target[1][1]]++; + bitcastVars[name].uses.push(node); + } + } + } + } + }); + var asmData = normalizeAsm(ast); + for (var v in bitcastVars) { + var info = bitcastVars[v]; + // good variables define only one type, use only one type, have definitions and uses, and define as a different type than they use + if (info.define_HEAP32*info.define_HEAPF32 == 0 && info.use_HEAP32*info.use_HEAPF32 == 0 && + info.define_HEAP32+info.define_HEAPF32 > 0 && info.use_HEAP32+info.use_HEAPF32 > 0 && + info.define_HEAP32*info.use_HEAP32 == 0 && info.define_HEAPF32*info.use_HEAPF32 == 0 && + v in asmData.vars && info.namings == info.define_HEAP32+info.define_HEAPF32+info.use_HEAP32+info.use_HEAPF32) { + var correct = info.use_HEAP32 ? 'HEAPF32' : 'HEAP32'; + info.defines.forEach(function(define) { + define[3] = define[3][1][3]; + if (correct == 'HEAP32') { + define[3] = ['binary', '|', define[3], ['num', 0]]; + } else { + define[3] = ['unary-prefix', '+', define[3]]; + } + // do we want a simplifybitops on the new values here? + }); + info.uses.forEach(function(use) { + use[2][1][1] = correct; + }); + asmData.vars[v] = 1 - asmData.vars[v]; + } + } + denormalizeAsm(ast, asmData); + } + // 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]; @@ -543,7 +669,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; @@ -566,7 +692,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) { @@ -580,40 +706,40 @@ function simplifyExpressionsPre(ast) { }); } - function asmOpts(ast) { + function asmOpts(fun) { // 1. Add final returns when necessary // 2. Remove unneeded coercions on function calls that have no targets (eliminator removed it) - traverseGeneratedFunctions(ast, function(fun) { - var returnType = null; - traverse(fun, function(node, type) { - if (type == 'return' && node[1]) { - returnType = detectAsmCoercion(node[1]); - } else if (type == 'stat') { - var inner = node[1]; - if ((inner[0] == 'binary' && inner[1] in ASSOCIATIVE_BINARIES && inner[2][0] == 'call' && inner[3][0] == 'num') || - (inner[0] == 'unary-prefix' && inner[1] == '+' && inner[2][0] == 'call')) { - node[1] = inner[2]; - } - } - }); - // Add a final return if one is missing. - if (returnType !== null) { - var stats = getStatements(fun); - var last = stats[stats.length-1]; - if (last[0] != 'return') { - var returnValue = ['num', 0]; - if (returnType == ASM_DOUBLE) returnValue = ['unary-prefix', '+', returnValue]; - stats.push(['return', returnValue]); + var returnType = null; + traverse(fun, function(node, type) { + if (type == 'return' && node[1]) { + returnType = detectAsmCoercion(node[1]); + } else if (type == 'stat') { + var inner = node[1]; + if ((inner[0] == 'binary' && inner[1] in ASSOCIATIVE_BINARIES && inner[2][0] == 'call' && inner[3][0] == 'num') || + (inner[0] == 'unary-prefix' && inner[1] == '+' && inner[2][0] == 'call')) { + node[1] = inner[2]; } } }); + // Add a final return if one is missing. + if (returnType !== null) { + var stats = getStatements(fun); + var last = stats[stats.length-1]; + if (last[0] != 'return') { + var returnValue = ['num', 0]; + if (returnType == ASM_DOUBLE) returnValue = ['unary-prefix', '+', returnValue]; + stats.push(['return', returnValue]); + } + } } - simplifySignExtends(ast); - simplifyBitops(ast); - joinAdditions(ast); - // simplifyZeroComp(ast); TODO: investigate performance - if (asm) asmOpts(ast); + traverseGeneratedFunctions(ast, function(func) { + simplifySignExtends(func); + simplifyBitops(func); + joinAdditions(func); + // simplifyZeroComp(func); TODO: investigate performance + if (asm) asmOpts(func); + }); } // In typed arrays mode 2, we can have @@ -953,18 +1079,15 @@ function optimizeShiftsAggressive(ast) { function simplifyNotCompsDirect(node) { if (node[0] == 'unary-prefix' && node[1] == '!') { if (node[2][0] == 'binary') { - if (node[2][1] == '<') { - return ['binary', '>=', node[2][2], node[2][3]]; - } else if (node[2][1] == '>') { - return ['binary', '<=', node[2][2], node[2][3]]; - } else if (node[2][1] == '==') { - return ['binary', '!=', node[2][2], node[2][3]]; - } else if (node[2][1] == '!=') { - return ['binary', '==', node[2][2], node[2][3]]; - } else if (node[2][1] == '===') { - return ['binary', '!==', node[2][2], node[2][3]]; - } else if (node[2][1] == '!==') { - return ['binary', '===', node[2][2], node[2][3]]; + switch(node[2][1]) { + case '<': return ['binary', '>=', node[2][2], node[2][3]]; + case '>': return ['binary', '<=', node[2][2], node[2][3]]; + case '<=': return ['binary', '>', node[2][2], node[2][3]]; + case '>=': return ['binary', '<', node[2][2], node[2][3]]; + case '==': return ['binary', '!=', node[2][2], node[2][3]]; + case '!=': return ['binary', '==', node[2][2], node[2][3]]; + case '===': return ['binary', '!==', node[2][2], node[2][3]]; + case '!==': return ['binary', '===', node[2][2], node[2][3]]; } } else if (node[2][0] == 'unary-prefix' && node[2][1] == '!') { return node[2][2]; @@ -1850,6 +1973,13 @@ var NODES_WITHOUT_ELIMINATION_SIDE_EFFECTS = set('name', 'num', 'string', 'binar var IGNORABLE_ELIMINATOR_SCAN_NODES = set('num', 'toplevel', 'string', 'break', 'continue', 'dot'); // dot can only be STRING_TABLE.* var ABORTING_ELIMINATOR_SCAN_NODES = set('new', 'object', 'function', 'defun', 'for', 'while', 'array', 'throw'); // we could handle some of these, TODO, but nontrivial (e.g. for while, the condition is hit multiple times after the body) +function isTempDoublePtrAccess(node) { // these are used in bitcasts; they are not really affecting memory, and should cause no invalidation + assert(node[0] == 'sub'); + return (node[2][0] == 'name' && node[2][1] == 'tempDoublePtr') || + (node[2][0] == 'binary' && ((node[2][2][0] == 'name' && node[2][2][1] == 'tempDoublePtr') || + (node[2][3][0] == 'name' && node[2][3][1] == 'tempDoublePtr'))); +} + function eliminate(ast, memSafe) { // Find variables that have a single use, and if they can be eliminated, do so traverseGeneratedFunctions(ast, function(func, type) { @@ -2134,7 +2264,7 @@ function eliminate(ast, memSafe) { if (allowTracking) track(name, node[3], node); } } else if (target[0] == 'sub') { - if (!memoryInvalidated) { + if (!isTempDoublePtrAccess(target) && !memoryInvalidated) { invalidateMemory(); memoryInvalidated = true; } @@ -2142,7 +2272,8 @@ function eliminate(ast, memSafe) { } else if (type == 'sub') { traverseInOrder(node[1], false, !memSafe); // evaluate inner traverseInOrder(node[2]); // evaluate outer - if (!ignoreSub) { // ignoreSub means we are a write (happening later), not a read + // ignoreSub means we are a write (happening later), not a read + if (!ignoreSub && !isTempDoublePtrAccess(node)) { // do the memory access if (!callsInvalidated) { invalidateCalls(); @@ -2341,9 +2472,9 @@ function eliminate(ast, memSafe) { var seenUses = {}, helperReplacements = {}; // for looper-helper optimization - // clean up vars - //printErr('cleaning up ' + JSON.stringify(varsToRemove)); + // clean up vars, and loop variable elimination traverse(func, function(node, type) { + // pre if (type === 'var') { node[1] = node[1].filter(function(pair) { return !varsToRemove[pair[0]] }); if (node[1].length == 0) { @@ -2353,6 +2484,7 @@ function eliminate(ast, memSafe) { } } }, function(node, type) { + // post if (type == 'name') { var name = node[1]; if (name in helperReplacements) { diff --git a/tools/shared.py b/tools/shared.py index 8a172d9cac994..b212a9cc8234d 100644 --- a/tools/shared.py +++ b/tools/shared.py @@ -295,7 +295,7 @@ def check_node_version(): # we re-check sanity when the settings are changed) # We also re-check sanity and clear the cache when the version changes -EMSCRIPTEN_VERSION = '1.4.9' +EMSCRIPTEN_VERSION = '1.5.0' def generate_sanity(): return EMSCRIPTEN_VERSION + '|' + get_llvm_target() @@ -454,9 +454,12 @@ def get_temp_files(self): DEBUG_CACHE = configuration.DEBUG_CACHE CANONICAL_TEMP_DIR = configuration.CANONICAL_TEMP_DIR -level = logging.DEBUG if os.environ.get('EMCC_DEBUG') else logging.INFO -logging.basicConfig(level=level, format='%(levelname)-8s %(name)s: %(message)s') - +logging.basicConfig(format='%(levelname)-8s %(name)s: %(message)s') +def set_logging(): + logger = logging.getLogger() + logger.setLevel(logging.DEBUG if os.environ.get('EMCC_DEBUG') else logging.INFO) +set_logging() + if not EMSCRIPTEN_TEMP_DIR: EMSCRIPTEN_TEMP_DIR = tempfile.mkdtemp(prefix='emscripten_temp_', dir=configuration.TEMP_DIR) def clean_temp(): @@ -1091,7 +1094,7 @@ def emscripten(filename, append_ext=True, extra_args=[]): @staticmethod def can_build_standalone(): - return not Settings.BUILD_AS_SHARED_LIB and not Settings.LINKABLE + return not Settings.BUILD_AS_SHARED_LIB and not Settings.LINKABLE and not Settings.EXPORT_ALL @staticmethod def can_use_unsafe_opts(): diff --git a/tools/test-js-optimizer-asm-pre-output.js b/tools/test-js-optimizer-asm-pre-output.js index ab953e5dfa588..25d521abb93f4 100644 --- a/tools/test-js-optimizer-asm-pre-output.js +++ b/tools/test-js-optimizer-asm-pre-output.js @@ -8,6 +8,7 @@ function a() { HEAP[1024] = 5; whee(12, 13); whee(12, 13); + f((g = t(), g + g | 0) | 0); } function b($this, $__n) { $this = $this | 0; @@ -107,4 +108,57 @@ function sign_extension_simplification() { print(5); } } +function tempDoublePtr($45, $14, $28, $42) { + $45 = $45 | 0; + $14 = $14 | 0; + $28 = $28 | 0; + $42 = $42 | 0; + var unelim = +0, bad = 0, unelim2 = +0; + unelim = +(127.5 * +$14); + HEAPF32[$45 >> 2] = ($14 < $28 ? $14 : $28) - $42; + HEAP32[$world + 102916 >> 2] = _malloc(192) | 0; + f(+HEAPF32[$45 >> 2]); + g(HEAP32[$14 >> 2] | 0); + $42 = +HEAPF32[$42 >> 2]; + ch($42); + HEAPF32[$45 >> 2] = unelim; + moar(); + bad = (HEAPF32[tempDoublePtr >> 2] = 127.5 * +$14, HEAP32[tempDoublePtr >> 2] | 0); + func(); + HEAP32[4] = bad; + HEAP32[5] = bad + 1; + moar(); + unelim2 = 127 + $14 | 0; + func(); + HEAP32[4] = unelim2; +} +function boxx($this, $aabb, $xf, $childIndex) { + $this = $this | 0; + $aabb = $aabb | 0; + $xf = $xf | 0; + $childIndex = $childIndex | 0; + var $2 = +0, $4 = +0, $7 = +0, $9 = +0, $13 = +0, $14 = +0, $19 = +0, $20 = +0, $22 = +0, $25 = +0, $28 = +0, $32 = +0, $42 = +0, $45 = 0, $_sroa_06_0_insert_insert$1 = +0, $51 = 0, $_sroa_0_0_insert_insert$1 = +0; + $2 = +HEAPF32[$xf + 12 >> 2]; + $4 = +HEAPF32[$this + 12 >> 2]; + $7 = +HEAPF32[$xf + 8 >> 2]; + $9 = +HEAPF32[$this + 16 >> 2]; + $13 = +HEAPF32[$xf >> 2]; + $14 = $13 + ($2 * $4 - $7 * $9); + $19 = +HEAPF32[$xf + 4 >> 2]; + $20 = $4 * $7 + $2 * $9 + $19; + $22 = +HEAPF32[$this + 20 >> 2]; + $25 = +HEAPF32[$this + 24 >> 2]; + $28 = $13 + ($2 * $22 - $7 * $25); + $32 = $19 + ($7 * $22 + $2 * $25); + $42 = +HEAPF32[$this + 8 >> 2]; + $45 = $aabb; + $_sroa_06_0_insert_insert$1 = +(($20 < $32 ? $20 : $32) - $42); + HEAPF32[$45 >> 2] = ($14 < $28 ? $14 : $28) - $42; + HEAPF32[$45 + 4 >> 2] = $_sroa_06_0_insert_insert$1; + $51 = $aabb + 8 | 0; + $_sroa_0_0_insert_insert$1 = +($42 + ($20 > $32 ? $20 : $32)); + HEAPF32[$51 >> 2] = $42 + ($14 > $28 ? $14 : $28); + HEAPF32[$51 + 4 >> 2] = $_sroa_0_0_insert_insert$1; + return; +} diff --git a/tools/test-js-optimizer-asm-pre.js b/tools/test-js-optimizer-asm-pre.js index 264587d296cda..38487d2fff217 100644 --- a/tools/test-js-optimizer-asm-pre.js +++ b/tools/test-js-optimizer-asm-pre.js @@ -8,6 +8,7 @@ function a() { HEAP[(4096 & 8191) >> 2] = 5; whee(12, 13) | 0; +whee(12, 13); + f((g = t(), (g+g)|0)|0); } function b($this, $__n) { $this = $this | 0; @@ -109,4 +110,59 @@ function sign_extension_simplification() { print(5); } } -// EMSCRIPTEN_GENERATED_FUNCTIONS: ["a", "b", "rett", "ret2t", "retf", "i32_8"] +function tempDoublePtr($45, $14, $28, $42) { + $45 = $45 | 0; + $14 = $14 | 0; + $28 = $28 | 0; + $42 = $42 | 0; + var unelim = 0; // only used as assign to int heap, so can avoid bitcast in definition + var bad = 0; + var unelim2 = 0; // opposite types + unelim = (HEAPF32[tempDoublePtr >> 2] = 127.5 * +$14, HEAP32[tempDoublePtr >> 2] | 0); + HEAP32[$45 >> 2] = 0 | (HEAPF32[tempDoublePtr >> 2] = ($14 < $28 ? $14 : $28) - $42, HEAP32[tempDoublePtr >> 2] | 0); + HEAP32[$world + 102916 >> 2] = _malloc(192) | 0; + f((HEAP32[tempDoublePtr >> 2] = HEAP32[$45 >> 2], +HEAPF32[tempDoublePtr >> 2])); + g((HEAPF32[tempDoublePtr >> 2] = HEAPF32[$14 >> 2], HEAP32[tempDoublePtr >> 2] | 0)); + $42 = (HEAP32[tempDoublePtr >> 2] = HEAP32[$42 >> 2] | 0, +HEAPF32[tempDoublePtr >> 2]); + ch($42); + HEAP32[$45 >> 2] = unelim; + moar(); + bad = (HEAPF32[tempDoublePtr >> 2] = 127.5 * +$14, HEAP32[tempDoublePtr >> 2] | 0); + func(); + HEAP32[4] = bad; + HEAP32[5] = (bad + 1) | 0; + moar(); + unelim2 = (HEAP32[tempDoublePtr >> 2] = 127 + $14, +HEAPF32[tempDoublePtr >> 2]); + func(); + HEAPF32[4] = unelim2; +} +function boxx($this, $aabb, $xf, $childIndex) { + $this = $this | 0; + $aabb = $aabb | 0; + $xf = $xf | 0; + $childIndex = $childIndex | 0; + var $2 = +0, $4 = +0, $7 = +0, $9 = +0, $13 = +0, $14 = +0, $19 = +0, $20 = +0, $22 = +0, $25 = +0, $28 = +0, $32 = +0, $42 = +0, $45 = 0, $_sroa_06_0_insert_insert$1 = 0, $51 = 0, $_sroa_0_0_insert_insert$1 = 0; + $2 = +HEAPF32[$xf + 12 >> 2]; + $4 = +HEAPF32[$this + 12 >> 2]; + $7 = +HEAPF32[$xf + 8 >> 2]; + $9 = +HEAPF32[$this + 16 >> 2]; + $13 = +HEAPF32[$xf >> 2]; + $14 = $13 + ($2 * $4 - $7 * $9); + $19 = +HEAPF32[$xf + 4 >> 2]; + $20 = $4 * $7 + $2 * $9 + $19; + $22 = +HEAPF32[$this + 20 >> 2]; + $25 = +HEAPF32[$this + 24 >> 2]; + $28 = $13 + ($2 * $22 - $7 * $25); + $32 = $19 + ($7 * $22 + $2 * $25); + $42 = +HEAPF32[$this + 8 >> 2]; + $45 = $aabb; + $_sroa_06_0_insert_insert$1 = (HEAPF32[tempDoublePtr >> 2] = ($20 < $32 ? $20 : $32) - $42, HEAP32[tempDoublePtr >> 2] | 0) | 0; + HEAPF32[$45 >> 2] = ($14 < $28 ? $14 : $28) - $42; + HEAP32[$45 + 4 >> 2] = $_sroa_06_0_insert_insert$1; + $51 = $aabb + 8 | 0; + $_sroa_0_0_insert_insert$1 = (HEAPF32[tempDoublePtr >> 2] = $42 + ($20 > $32 ? $20 : $32), HEAP32[tempDoublePtr >> 2] | 0) | 0; + HEAPF32[$51 >> 2] = $42 + ($14 > $28 ? $14 : $28); + HEAP32[$51 + 4 >> 2] = $_sroa_0_0_insert_insert$1; + return; +} +// EMSCRIPTEN_GENERATED_FUNCTIONS: ["a", "b", "rett", "ret2t", "retf", "i32_8", "tempDoublePtr", "boxx"]