diff options
-rw-r--r-- | ChangeLog | 35 | ||||
-rwxr-xr-x | emcc | 3 | ||||
-rw-r--r-- | src/closure-externs.js | 110 | ||||
-rw-r--r-- | src/library_uuid.js | 140 | ||||
-rw-r--r-- | src/modules.js | 2 | ||||
-rw-r--r-- | src/parseTools.js | 9 | ||||
-rw-r--r-- | src/settings.js | 2 | ||||
-rw-r--r-- | src/struct_info.json | 12 | ||||
-rw-r--r-- | system/include/uuid/uuid.h | 35 | ||||
-rw-r--r-- | tests/cases/gepaddoverflow.ll | 37 | ||||
-rw-r--r-- | tests/cases/gepaddoverflow.txt | 1 | ||||
-rwxr-xr-x | tests/runner.py | 2 | ||||
-rw-r--r-- | tests/test_benchmark.py | 38 | ||||
-rw-r--r-- | tests/test_browser.py | 27 | ||||
-rw-r--r-- | tests/test_core.py | 15 | ||||
-rw-r--r-- | tests/test_other.py | 1 | ||||
-rw-r--r-- | tests/uuid/test.c | 69 | ||||
-rw-r--r-- | tools/js-optimizer.js | 289 | ||||
-rw-r--r-- | tools/shared.py | 5 |
19 files changed, 670 insertions, 162 deletions
@@ -10,7 +10,40 @@ Not all changes are documented here. In particular, new features, user-oriented Current trunk code ------------------ - To see a list of commits in the active development branch 'incoming', which have not yet been packaged in a release, see - https://github.com/kripken/emscripten/compare/1.7.8...incoming + https://github.com/kripken/emscripten/compare/1.8.2...incoming + +v1.8.2: 1/4/2013 +------------------ + - Fixed glGetFramebufferAttachmentParameteriv and an issue with glGetXXX when the returned value was null. + - Full list of changes: https://github.com/kripken/emscripten/compare/1.8.1...1.8.2 + +v1.8.1: 1/3/2013 +------------------ + - Added support for WebGL hardware instancing extension. + - Improved fastcomp native LLVM backend support. + - Added support for #include filename.js to JS libraries. + - Deprecated --compression emcc command line parameter that manually compressed output JS files, due to performance issues. Instead, it is best to rely on the web server to serve compressed JS files. + - Full list of changes: https://github.com/kripken/emscripten/compare/1.8.0...1.8.1 + +v1.8.0: 12/28/2013 +------------------ + - Fix two issues with function outliner and relooper. + - Full list of changes: https://github.com/kripken/emscripten/compare/1.7.9...1.8.0 + +v1.7.9: 12/27/2013 +------------------ + - Added new command line parameter --em-config that allows specifying a custom location for the .emscripten configuration file. + - Reintroduced relaxed asm.js heap sizes, which no longer need to be power of 2, but a multiple of 16MB is sufficient. + - Added emrun command line tool that allows launching .html pages from command line on desktop and Android as if they were native applications. See https://groups.google.com/forum/#!topic/emscripten-discuss/t2juu3q1H8E . Adds --emrun compiler link flag. + - Began initial work on the "fastcomp" compiler toolchain, a rewrite of the previous JS LLVM AST parsing and codegen via a native LLVM backend. + - Added --exclude-file command line flag to emcc and a matching --exclude command line flag to file packager, which allows specifying files and directories that should be excluded while packaging a VFS data blob. + - Improved GLES2 and EGL support libraries to be more spec-conformant. + - Optimized legacy GL emulation code path. Added new GL_FFP_ONLY optimization path to fixed function pipeline emulation. + - Added new core functions emscripten_log() and emscripten_get_callstack() that allow printing out log messages with demangled and source-mapped callstack information. + - Improved BSD Sockets support. Implemented getprotobyname() for BSD Sockets library. + - Fixed issues with simd support. + - Various bugfixes: #1573, #1846, #1886, #1908, #1918, #1930, #1931, #1942, #1948, .. + - Full list of changes: https://github.com/kripken/emscripten/compare/1.7.8...1.7.9 v1.7.8: 11/19/2013 ------------------ @@ -1916,6 +1916,9 @@ try: js_optimizer_queue += [get_eliminate()] + if shared.Settings.AGGRESSIVE_VARIABLE_ELIMINATION: + js_optimizer_queue += ['aggressiveVariableElimination'] + if opt_level >= 2: js_optimizer_queue += ['simplifyExpressions'] diff --git a/src/closure-externs.js b/src/closure-externs.js new file mode 100644 index 00000000..a82aa669 --- /dev/null +++ b/src/closure-externs.js @@ -0,0 +1,110 @@ +/** + * This file contains definitions for things that we'd really rather the closure compiler *didn't* minify. + * See http://code.google.com/p/closure-compiler/wiki/FAQ#How_do_I_write_an_externs_file + * See also the discussion here: https://github.com/kripken/emscripten/issues/1979 + * + * The closure_compiler() method in tools/shared.py refers to this file when calling closure. + */ + +// Closure externs used by library_uuid.js + +/** + * @param {Array} typedArray + */ +crypto.getRandomValues = function(typedArray) {}; + +/** + BEGIN_NODE_INCLUDE + var crypto = require('crypto'); + END_NODE_INCLUDE + */ + +/** + * @type {Object.<string,*>} + */ +var crypto = {}; + +/** + * @param {number} size + * @param {function(Error, buffer.Buffer)} callback + */ +crypto.randomBytes = function(size, callback) {}; + + +// Closure externs used by library_sockfs.js + +/** + BEGIN_NODE_INCLUDE + var ws = require('ws'); + END_NODE_INCLUDE + */ + +/** + * @type {Object.<string,*>} + */ +var ws = {}; + +/** + * @param {string} event + * @param {function()} callback + */ +ws.on = function(event, callback) {}; + +/** + * @param {Object} data + * @param {Object} flags + * @param {function()=} callback + */ +ws.send = function(data, flags, callback) {}; + +/** +* @type {boolean} +*/ +ws.binaryType; + +/** + * @type {Object.<string,*>} + */ +var wss = ws.Server; + +/** + * @param {string} event + * @param {function()} callback + */ +wss.on = function(event, callback) {}; + +/** + * @param {function()} callback + */ +wss.broadcast = function(callback) {}; + +/** +* @type {Object.<string,*>} +*/ +wss._socket; + +/** +* @type {string} +*/ +wss.url; + +/** +* @type {string} +*/ +wss._socket.remoteAddress; + +/** +* @type {number} +*/ +wss._socket.remotePort; + +/** +* @type {Object.<string,*>} +*/ +var flags = {}; +/** +* @type {boolean} +*/ +flags.binary; + + diff --git a/src/library_uuid.js b/src/library_uuid.js new file mode 100644 index 00000000..adfd1882 --- /dev/null +++ b/src/library_uuid.js @@ -0,0 +1,140 @@ +// Implementation of libuuid creating RFC4122 version 4 random UUIDs. + +mergeInto(LibraryManager.library, { + // Clear a 'compact' UUID. + uuid_clear: function(uu) { + // void uuid_clear(uuid_t uu); + _memset(uu, 0, 16); + }, + + // Compare whether or not two 'compact' UUIDs are the same. + // Returns an integer less than, equal to, or greater than zero if uu1 is found, respectively, to be + // lexigraphically less than, equal, or greater than uu2. + uuid_compare__deps: ['memcmp'], + uuid_compare: function(uu1, uu2) { + // int uuid_compare(const uuid_t uu1, const uuid_t uu2); + return _memcmp(uu1, uu2, 16); + }, + + // Copies the 'compact' UUID variable from src to dst. + uuid_copy: function(dst, src) { + // void uuid_copy(uuid_t dst, const uuid_t src); + _memcpy(dst, src, 16); + }, + + // Write a RFC4122 version 4 compliant UUID largely based on the method found in + // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript + // tweaked slightly in order to use the 'compact' UUID form used by libuuid. + uuid_generate: function(out) { + // void uuid_generate(uuid_t out); + var uuid = null; + + if (ENVIRONMENT_IS_NODE) { + // If Node.js try to use crypto.randomBytes + try { + var rb = require('crypto').randomBytes; + uuid = rb(16); + } catch(e) {} + } else if (ENVIRONMENT_IS_WEB && + typeof(window.crypto) !== 'undefined' && + typeof(window.crypto.getRandomValues) !== 'undefined') { + // If crypto.getRandomValues is available try to use it. + uuid = new Uint8Array(16); + window.crypto.getRandomValues(uuid); + } + + // Fall back to Math.random if a higher quality random number generator is not available. + if (!uuid) { + uuid = new Array(16); + var d = new Date().getTime(); + for (var i = 0; i < 16; i++) { + var r = (d + Math.random()*256)%256 | 0; + d = Math.floor(d/256); + uuid[i] = r; + } + } + + uuid[6] = (uuid[6] & 0x0F) | 0x40; + uuid[8] = (uuid[8] & 0x7F) | 0x80; + writeArrayToMemory(uuid, out); + }, + + // Compares the value of the supplied 'compact' UUID variable uu to the NULL value. + // If the value is equal to the NULL UUID, 1 is returned, otherwise 0 is returned. + uuid_is_null: function(uu) { + // int uuid_is_null(const uuid_t uu); + for (var i = 0; i < 4; i++, uu = (uu+4)|0) { + var val = {{{ makeGetValue('uu', 0, 'i32') }}}; + if (val) { + return 0; + } + } + return 1; + }, + + // converts the UUID string given by inp into the binary representation. The input UUID is a string of + // the form "%08x-%04x-%04x-%04x-%012x" 36 bytes plus the trailing '\0'. + // Upon successfully parsing the input string, 0 is returned, and the UUID is stored in the location + // pointed to by uu, otherwise -1 is returned. + uuid_parse: function(inp, uu) { + // int uuid_parse(const char *in, uuid_t uu); + var inp = Pointer_stringify(inp); + if (inp.length === 36) { + var i = 0; + var uuid = new Array(16); + inp.toLowerCase().replace(/[0-9a-f]{2}/g, function(byte) { + if (i < 16) { + uuid[i++] = parseInt(byte, 16); + } + }); + + if (i < 16) { + return -1; + } else { + writeArrayToMemory(uuid, uu); + return 0; + } + } else { + return -1; + } + }, + + // Convert a 'compact' form UUID to a string, if the upper parameter is supplied make the string upper case. + uuid_unparse: function(uu, out, upper) { + // void uuid_unparse(const uuid_t uu, char *out); + var i = 0; + var uuid = 'xxxx-xx-xx-xx-xxxxxx'.replace(/[x]/g, function(c) { + var r = upper ? ({{{ makeGetValue('uu', 'i', 'i8', 0, 1) }}}).toString(16).toUpperCase() : + ({{{ makeGetValue('uu', 'i', 'i8', 0, 1) }}}).toString(16); + r = (r.length === 1) ? '0' + r : r; // Zero pad single digit hex values + i++; + return r; + }); + writeStringToMemory(uuid, out); + }, + + // Convert a 'compact' form UUID to a lower case string. + uuid_unparse_lower__deps: ['uuid_unparse'], + uuid_unparse_lower: function(uu, out) { + // void uuid_unparse_lower(const uuid_t uu, char *out); + _uuid_unparse(uu, out); + }, + + // Convert a 'compact' form UUID to an upper case string. + uuid_unparse_upper__deps: ['uuid_unparse'], + uuid_unparse_upper: function(uu, out) { + // void uuid_unparse_upper(const uuid_t uu, char *out); + _uuid_unparse(uu, out, true); + }, + + uuid_type: function(uu) { + // int uuid_type(const uuid_t uu); + return {{{ cDefine('UUID_TYPE_DCE_RANDOM') }}}; + }, + + uuid_variant: function(uu) { + // int uuid_variant(const uuid_t uu); + return {{{ cDefine('UUID_VARIANT_DCE') }}}; + } +}); + diff --git a/src/modules.js b/src/modules.js index e2d3433f..c0b98f6f 100644 --- a/src/modules.js +++ b/src/modules.js @@ -424,7 +424,7 @@ var LibraryManager = { load: function() { if (this.library) return; - var libraries = ['library.js', 'library_path.js', 'library_fs.js', 'library_idbfs.js', 'library_memfs.js', 'library_nodefs.js', 'library_sockfs.js', 'library_tty.js', 'library_browser.js', 'library_sdl.js', 'library_gl.js', 'library_glut.js', 'library_xlib.js', 'library_egl.js', 'library_gc.js', 'library_jansson.js', 'library_openal.js', 'library_glfw.js'].concat(additionalLibraries); + var libraries = ['library.js', 'library_path.js', 'library_fs.js', 'library_idbfs.js', 'library_memfs.js', 'library_nodefs.js', 'library_sockfs.js', 'library_tty.js', 'library_browser.js', 'library_sdl.js', 'library_gl.js', 'library_glut.js', 'library_xlib.js', 'library_egl.js', 'library_gc.js', 'library_jansson.js', 'library_openal.js', 'library_glfw.js', 'library_uuid.js'].concat(additionalLibraries); for (var i = 0; i < libraries.length; i++) { eval(processMacros(preprocess(read(libraries[i])))); } diff --git a/src/parseTools.js b/src/parseTools.js index 655248b3..4d6d7bd3 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -49,7 +49,8 @@ function preprocess(text) { showStack.push(ident in this && this[ident] > 0); } } else if (line[2] == 'n') { // include - ret += '\n' + read(line.substr(line.indexOf(' ')+1)) + '\n' + var included = read(line.substr(line.indexOf(' ')+1)); + ret += '\n' + preprocess(included) + '\n' } } else if (line[2] == 'l') { // else showStack.push(!showStack.pop()); @@ -1639,7 +1640,10 @@ function getFastValue(a, op, b, type) { } function getFastValues(list, op, type) { - assert(op == '+'); + assert(op === '+' && type === 'i32'); + for (var i = 0; i < list.length; i++) { + if (isNumber(list[i])) list[i] = (list[i]|0) + ''; + } var changed = true; while (changed) { changed = false; @@ -1647,6 +1651,7 @@ function getFastValues(list, op, type) { var fast = getFastValue(list[i], op, list[i+1], type); var raw = list[i] + op + list[i+1]; if (fast.length < raw.length || fast.indexOf(op) < 0) { + if (isNumber(fast)) fast = (fast|0) + ''; list[i] = fast; list.splice(i+1, 1); i--; diff --git a/src/settings.js b/src/settings.js index 753e2367..04b2cc79 100644 --- a/src/settings.js +++ b/src/settings.js @@ -156,6 +156,8 @@ var OUTLINING_LIMIT = 0; // A function size above which we try to automatically // with, but something around 20,000 to 100,000 might make sense. // (The unit size is number of AST nodes.) +var AGGRESSIVE_VARIABLE_ELIMINATION = 0; // Run aggressiveVariableElimination in js-optimizer.js + // Generated code debugging options var SAFE_HEAP = 0; // Check each write to the heap, for example, this will give a clear // error on what would be segfaults in a native build (like deferencing diff --git a/src/struct_info.json b/src/struct_info.json index c136cc8b..a22851b6 100644 --- a/src/struct_info.json +++ b/src/struct_info.json @@ -1061,5 +1061,17 @@ "patch" ] } + }, + { + "file": "uuid/uuid.h", + "defines": [ + "UUID_VARIANT_NCS", + "UUID_VARIANT_DCE", + "UUID_VARIANT_MICROSOFT", + "UUID_VARIANT_OTHER", + "UUID_TYPE_DCE_TIME", + "UUID_TYPE_DCE_RANDOM" + ], + "structs": {} } ] diff --git a/system/include/uuid/uuid.h b/system/include/uuid/uuid.h new file mode 100644 index 00000000..0a84e02c --- /dev/null +++ b/system/include/uuid/uuid.h @@ -0,0 +1,35 @@ + +#ifndef _UUID_H +#define _UUID_H + +typedef unsigned char uuid_t[16]; + +#define UUID_VARIANT_NCS 0 +#define UUID_VARIANT_DCE 1 +#define UUID_VARIANT_MICROSOFT 2 +#define UUID_VARIANT_OTHER 3 + +#define UUID_TYPE_DCE_TIME 1 +#define UUID_TYPE_DCE_RANDOM 4 + +#ifdef __cplusplus +extern "C" { +#endif + +void uuid_clear(uuid_t uu); +int uuid_compare(const uuid_t uu1, const uuid_t uu2); +void uuid_copy(uuid_t dst, const uuid_t src); +void uuid_generate(uuid_t out); +int uuid_is_null(const uuid_t uu); +int uuid_parse(const char *in, uuid_t uu); +void uuid_unparse(const uuid_t uu, char *out); +void uuid_unparse_lower(const uuid_t uu, char *out); +void uuid_unparse_upper(const uuid_t uu, char *out); +int uuid_type(const uuid_t uu); +int uuid_variant(const uuid_t uu); + +#ifdef __cplusplus +} +#endif + +#endif /* _UUID_H */ diff --git a/tests/cases/gepaddoverflow.ll b/tests/cases/gepaddoverflow.ll new file mode 100644 index 00000000..11246c1d --- /dev/null +++ b/tests/cases/gepaddoverflow.ll @@ -0,0 +1,37 @@ +; ModuleID = 'new.o' +target datalayout = "e-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-p:32:32:32-v128:32:32" +target triple = "le32-unknown-nacl" + +declare i32 @printf(i8* noalias, ...) nounwind + +@x = common global [4194304 x i8] zeroinitializer, align 4 +@.str = private constant [6 x i8] c"*%d*\0A\00", align 1 + +define i8* @test_gep(i32 %y) nounwind readnone { + ; JavaScript uses double precision 64-bit floating point values, with + ; a 53 bit mantissa. The maximum precisely representable integer is + ; 9007199254740992. A number close to that limit is constructed here + ; for the constant part of the getelementptr instruction: + ; 4194304 * 2147483647 == 9007199250546688 == 9007199254740992 - 4194304 + ; If that number appears in JavaScript source instead of being properly + ; limited to 32 bits, the %y parameter can be used to exceed the maximum + ; precisely representable integer, and make the computation inexact. + %test_res = getelementptr [4194304 x i8]* @x, i32 2147483647, i32 %y + ret i8* %test_res +} + +define i32 @main() { + %res_0 = call i8* (i32)* @test_gep(i32 1000000000) + %res_1 = call i8* (i32)* @test_gep(i32 1000000001) + %res_0_i = ptrtoint i8* %res_0 to i32 + %res_1_i = ptrtoint i8* %res_1 to i32 + + ; If getelementptr limited the constant part of the offset to 32 bits, + ; result will be 1. Otherwise, it cannot be 1 because the large numbers in + ; the calculation cannot be accurately represented by floating point math. + %res_diff = sub i32 %res_1_i, %res_0_i + %printf_res = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([6 x i8]* @.str, i32 0, i32 0), i32 %res_diff) + + ret i32 0 +} + diff --git a/tests/cases/gepaddoverflow.txt b/tests/cases/gepaddoverflow.txt new file mode 100644 index 00000000..10fd998b --- /dev/null +++ b/tests/cases/gepaddoverflow.txt @@ -0,0 +1 @@ +*1* diff --git a/tests/runner.py b/tests/runner.py index 494297db..7f0cbaed 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -342,7 +342,7 @@ process(sys.argv[1]) if self.library_cache is not None: if cache and self.library_cache.get(cache_name): - print >> sys.stderr, '<load %s from cache> ' % cache_name, + print >> sys.stderr, '<load %s from cache> ' % cache_name generated_libs = [] for basename, contents in self.library_cache[cache_name]: bc_file = os.path.join(build_dir, cache_name + '_' + basename) diff --git a/tests/test_benchmark.py b/tests/test_benchmark.py index c4d3d216..21a47178 100644 --- a/tests/test_benchmark.py +++ b/tests/test_benchmark.py @@ -119,21 +119,27 @@ process(sys.argv[1]) return run_js(self.filename, engine=self.engine, args=args, stderr=PIPE, full_output=True) # Benchmarkers -benchmarkers = [ - #NativeBenchmarker('clang', CLANG_CC, CLANG), - NativeBenchmarker('clang-3.2', os.path.join(LLVM_3_2, 'clang'), os.path.join(LLVM_3_2, 'clang++')), - #NativeBenchmarker('clang-3.3', os.path.join(LLVM_3_3, 'clang'), os.path.join(LLVM_3_3, 'clang++')), - #NativeBenchmarker('clang-3.4', os.path.join(LLVM_3_4, 'clang'), os.path.join(LLVM_3_4, 'clang++')), - #NativeBenchmarker('gcc', 'gcc', 'g++'), - JSBenchmarker('sm-f32', SPIDERMONKEY_ENGINE, ['-s', 'PRECISE_F32=2']), - #JSBenchmarker('sm-f32-3.2', SPIDERMONKEY_ENGINE, ['-s', 'PRECISE_F32=2'], env={ 'LLVM': LLVM_3_2 }), - #JSBenchmarker('sm-f32-3.3', SPIDERMONKEY_ENGINE, ['-s', 'PRECISE_F32=2'], env={ 'LLVM': LLVM_3_3 }), - #JSBenchmarker('sm-f32-3.4', SPIDERMONKEY_ENGINE, ['-s', 'PRECISE_F32=2'], env={ 'LLVM': LLVM_3_4 }), - #JSBenchmarker('sm-fc', SPIDERMONKEY_ENGINE, env={ 'EMCC_FAST_COMPILER': '1' }), - #JSBenchmarker('sm-noasm', SPIDERMONKEY_ENGINE + ['--no-asmjs']), - #JSBenchmarker('sm-noasm-f32', SPIDERMONKEY_ENGINE + ['--no-asmjs'], ['-s', 'PRECISE_F32=2']), - #JSBenchmarker('v8', V8_ENGINE) -] +try: + benchmarkers_error = '' + benchmarkers = [ + #NativeBenchmarker('clang', CLANG_CC, CLANG), + NativeBenchmarker('clang-3.2', os.path.join(LLVM_3_2, 'clang'), os.path.join(LLVM_3_2, 'clang++')), + #NativeBenchmarker('clang-3.3', os.path.join(LLVM_3_3, 'clang'), os.path.join(LLVM_3_3, 'clang++')), + #NativeBenchmarker('clang-3.4', os.path.join(LLVM_3_4, 'clang'), os.path.join(LLVM_3_4, 'clang++')), + #NativeBenchmarker('gcc', 'gcc', 'g++'), + JSBenchmarker('sm-f32', SPIDERMONKEY_ENGINE, ['-s', 'PRECISE_F32=2']), + #JSBenchmarker('sm-f32-aggro', SPIDERMONKEY_ENGINE, ['-s', 'PRECISE_F32=2', '-s', 'AGGRESSIVE_VARIABLE_ELIMINATION=1']), + #JSBenchmarker('sm-f32-3.2', SPIDERMONKEY_ENGINE, ['-s', 'PRECISE_F32=2'], env={ 'LLVM': LLVM_3_2 }), + #JSBenchmarker('sm-f32-3.3', SPIDERMONKEY_ENGINE, ['-s', 'PRECISE_F32=2'], env={ 'LLVM': LLVM_3_3 }), + #JSBenchmarker('sm-f32-3.4', SPIDERMONKEY_ENGINE, ['-s', 'PRECISE_F32=2'], env={ 'LLVM': LLVM_3_4 }), + #JSBenchmarker('sm-fc', SPIDERMONKEY_ENGINE, env={ 'EMCC_FAST_COMPILER': '1' }), + #JSBenchmarker('sm-noasm', SPIDERMONKEY_ENGINE + ['--no-asmjs']), + #JSBenchmarker('sm-noasm-f32', SPIDERMONKEY_ENGINE + ['--no-asmjs'], ['-s', 'PRECISE_F32=2']), + #JSBenchmarker('v8', V8_ENGINE) + ] +except Exception, e: + benchmarkers_error = str(e) + benchmarkers = [] class benchmark(RunnerCore): save_dir = True @@ -171,6 +177,8 @@ class benchmark(RunnerCore): Building.COMPILER_TEST_OPTS = [] def do_benchmark(self, name, src, expected_output='FAIL', args=[], emcc_args=[], native_args=[], shared_args=[], force_c=False, reps=TEST_REPS, native_exec=None, output_parser=None, args_processor=None, lib_builder=None): + if len(benchmarkers) == 0: raise Exception('error, no benchmarkers: ' + benchmarkers_error) + args = args or [DEFAULT_ARG] if args_processor: args = args_processor(args) diff --git a/tests/test_browser.py b/tests/test_browser.py index 20b38bb5..c2eaabb6 100644 --- a/tests/test_browser.py +++ b/tests/test_browser.py @@ -1720,3 +1720,30 @@ keydown(100);keyup(100); // trigger the end assert 'argv[3]: 3' in stdout assert 'hello, world!' in stdout assert 'hello, error stream!' in stderr + + def test_uuid(self): + # Run with ./runner.py browser.test_uuid + # We run this test in Node/SPIDERMONKEY and browser environments because we try to make use of + # high quality crypto random number generators such as crypto.getRandomValues or randomBytes (if available). + + # First run tests in Node and/or SPIDERMONKEY using run_js. Use closure compiler so we can check that + # require('crypto').randomBytes and window.crypto.getRandomValues doesn't get minified out. + Popen([PYTHON, EMCC, '-O2', '--closure', '1', path_from_root('tests', 'uuid', 'test.c'), '-o', path_from_root('tests', 'uuid', 'test.js')], stdout=PIPE, stderr=PIPE).communicate() + + test_js_closure = open(path_from_root('tests', 'uuid', 'test.js')).read() + + # Check that test.js compiled with --closure 1 contains ").randomBytes" and "window.crypto.getRandomValues" + assert ").randomBytes" in test_js_closure + assert "window.crypto.getRandomValues" in test_js_closure + + out = run_js(path_from_root('tests', 'uuid', 'test.js'), full_output=True) + print out + + # Tidy up files that might have been created by this test. + try_delete(path_from_root('tests', 'uuid', 'test.js')) + try_delete(path_from_root('tests', 'uuid', 'test.js.map')) + + # Now run test in browser + self.btest(path_from_root('tests', 'uuid', 'test.c'), '1') + + diff --git a/tests/test_core.py b/tests/test_core.py index 6442f894..1dc25dce 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -4652,12 +4652,15 @@ return malloc(size); if Settings.QUANTUM_SIZE == 1: return self.skip('TODO: make this work') if os.environ.get('EMCC_FAST_COMPILER') == '1': return self.skip('todo in fastcomp') - self.do_run('', - 'hello lua world!\n17\n1\n2\n3\n4\n7', - args=['-e', '''print("hello lua world!");print(17);for x = 1,4 do print(x) end;print(10-3)'''], - libraries=self.get_library('lua', [os.path.join('src', 'lua'), os.path.join('src', 'liblua.a')], make=['make', 'generic'], configure=None), - includes=[path_from_root('tests', 'lua')], - output_nicerizer=lambda string, err: (string + err).replace('\n\n', '\n').replace('\n\n', '\n')) + for aggro in ([0, 1] if '-O2' in self.emcc_args else [0]): + print aggro + Settings.AGGRESSIVE_VARIABLE_ELIMINATION = aggro + self.do_run('', + 'hello lua world!\n17\n1\n2\n3\n4\n7', + args=['-e', '''print("hello lua world!");print(17);for x = 1,4 do print(x) end;print(10-3)'''], + libraries=self.get_library('lua', [os.path.join('src', 'lua'), os.path.join('src', 'liblua.a')], make=['make', 'generic'], configure=None), + includes=[path_from_root('tests', 'lua')], + output_nicerizer=lambda string, err: (string + err).replace('\n\n', '\n').replace('\n\n', '\n')) def get_freetype(self): Settings.DEAD_FUNCTIONS += ['_inflateEnd', '_inflate', '_inflateReset', '_inflateInit2_'] diff --git a/tests/test_other.py b/tests/test_other.py index 9c983f9f..f9ee2d9d 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -2209,3 +2209,4 @@ mergeInto(LibraryManager.library, { process = Popen([PYTHON, EMCC, '-c', path_from_root('tests', 'hello_world.c'), '-o', outdir, '--default-obj-ext', 'obj'], stdout=PIPE, stderr=PIPE) process.communicate() assert(os.path.isfile(outdir + 'hello_world.obj')) + diff --git a/tests/uuid/test.c b/tests/uuid/test.c new file mode 100644 index 00000000..dc2c6589 --- /dev/null +++ b/tests/uuid/test.c @@ -0,0 +1,69 @@ +#include <uuid/uuid.h> +#include <assert.h> +#include <ctype.h> +#include <stdio.h> +#include <stdlib.h> +#include <emscripten.h> + +int isUUID(char* p, int upper) { + char* p1 = p; + do { + if (!(isxdigit(*p1) || (*p1 == '-')) || (upper && islower(*p1)) || (!upper && isupper(*p1))) { + return 0; + } else { + } + } while (*++p1 != 0); + + if ((p[8] == '-') && (p[13] == '-') && (p[18] == '-') && (p[23] == '-')) { + return 1; + } else { + return 0; + } +} + +int main() { + uuid_t uuid; + uuid_t uuid1; + uuid_t uuid2; + uuid_t empty_uuid = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + uuid_generate(uuid); + + assert(uuid_is_null(uuid) == 0); + assert(uuid_type(uuid) == UUID_TYPE_DCE_RANDOM); + assert(uuid_variant(uuid) == UUID_VARIANT_DCE); + + char *generated = (char *)malloc(37*sizeof(char)); + uuid_unparse(uuid, generated); + assert(isUUID(generated, 0) == 1); // Check it's a valid lower case UUID string. + printf("\nuuid = %s\n", generated); + + assert(uuid_parse(generated, uuid1) == 0); // Check the generated UUID parses correctly into a compact UUID. + assert(uuid_compare(uuid1, uuid) == 0); // Compare the parsed UUID with the original. + + uuid_unparse_lower(uuid, generated); + assert(isUUID(generated, 0) == 1); // Check it's a valid lower case UUID string. + printf("uuid = %s\n", generated); + + uuid_unparse_upper(uuid, generated); + assert(isUUID(generated, 1) == 1); // Check it's a valid upper case UUID string. + printf("uuid = %s\n", generated); + + + uuid_copy(uuid2, uuid); + assert(uuid_compare(uuid2, uuid) == 0); + + uuid_clear(uuid); + assert(uuid_compare(empty_uuid, uuid) == 0); + + assert(uuid_is_null(uuid) == 1); + + // The following lets the browser test exit cleanly. + int result = 1; + #if EMSCRIPTEN + #ifdef REPORT_RESULT + REPORT_RESULT(); + #endif + #endif + exit(0); +} + diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index 161ed59c..b507a45a 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -2930,162 +2930,180 @@ function relocate(ast) { var NODES_WITHOUT_ELIMINATION_SENSITIVITY = set('name', 'num', 'binary', 'unary-prefix'); var FAST_ELIMINATION_BINARIES = setUnion(setUnion(USEFUL_BINARY_OPS, COMPARE_OPS), set('+')); -function outline(ast) { - function measureSize(ast) { - var size = 0; - traverse(ast, function() { - size++; - }); - return size; - } - - function aggressiveVariableElimination(func, asmData) { - // This removes as many variables as possible. This is often not the best thing because it increases - // code size, but it is far preferable to the risk of split functions needing to do more spilling. Specifically, - // it finds 'trivial' variables: ones with 1 definition, and that definition is not sensitive to any changes: it - // only depends on constants and local variables that are themselves trivial. We can unquestionably eliminate - // such variables in a trivial manner. - - var assignments = {}; - var appearances = {}; - var defs = {}; - var considered = {}; +function measureSize(ast) { + var size = 0; + traverse(ast, function() { + size++; + }); + return size; +} - traverse(func, function(node, type) { - if (type == 'assign' && node[2][0] == 'name') { - var name = node[2][1]; - if (name in asmData.vars) { - assignments[name] = (assignments[name] || 0) + 1; - appearances[name] = (appearances[name] || 0) - 1; // this appearance is a definition, offset the counting later - defs[name] = node; - } else { - if (name in asmData.params) { - assignments[name] = (assignments[name] || 1) + 1; // init to 1 for initial parameter assignment - considered[name] = true; // this parameter is not ssa, it must be in a hand-optimized function, so it is not trivial - } - } - } else if (type == 'name') { - var name = node[1]; - if (name in asmData.vars) { - appearances[name] = (appearances[name] || 0) + 1; +function aggressiveVariableEliminationInternal(func, asmData) { + // This removes as many variables as possible. This is often not the best thing because it increases + // code size, but it is far preferable to the risk of split functions needing to do more spilling, so + // we use it when outlining. + // Specifically, this finds 'trivial' variables: ones with 1 definition, and that definition is not sensitive to any changes: it + // only depends on constants and local variables that are themselves trivial. We can unquestionably eliminate + // such variables in a trivial manner. + + var assignments = {}; + var appearances = {}; + var defs = {}; + var considered = {}; + + traverse(func, function(node, type) { + if (type == 'assign' && node[2][0] == 'name') { + var name = node[2][1]; + if (name in asmData.vars) { + assignments[name] = (assignments[name] || 0) + 1; + appearances[name] = (appearances[name] || 0) - 1; // this appearance is a definition, offset the counting later + defs[name] = node; + } else { + if (name in asmData.params) { + assignments[name] = (assignments[name] || 1) + 1; // init to 1 for initial parameter assignment + considered[name] = true; // this parameter is not ssa, it must be in a hand-optimized function, so it is not trivial } } - }); + } else if (type == 'name') { + var name = node[1]; + if (name in asmData.vars) { + appearances[name] = (appearances[name] || 0) + 1; + } + } + }); - var allTrivials = {}; // key of a trivial var => size of its (expanded) value, at least 1 - - // three levels of variables: - // 1. trivial: 1 def (or less), uses nothing sensitive, can be eliminated - // 2. safe: 1 def (or less), can be used in a trivial, but cannot itself be eliminated - // 3. sensitive: uses a global or memory or something else that prevents trivial elimination. - - function assessTriviality(name) { - // only care about vars with 0-1 assignments of (0 for parameters), and can ignore label (which is not explicitly initialized, but cannot be eliminated ever anyhow) - if (assignments[name] > 1 || (!(name in asmData.vars) && !(name in asmData.params)) || name == 'label') return false; - if (considered[name]) return allTrivials[name]; - considered[name] = true; - var sensitive = false; - var size = 0, originalSize = 0; - var def = defs[name]; - if (def) { - var value = def[3]; - originalSize = measureSize(value); - if (value) { - traverse(value, function recurseValue(node, type) { - var one = node[1]; - if (!(type in NODES_WITHOUT_ELIMINATION_SENSITIVITY)) { // || (type == 'binary' && !(one in FAST_ELIMINATION_BINARIES))) { - sensitive = true; + var allTrivials = {}; // key of a trivial var => size of its (expanded) value, at least 1 + + // three levels of variables: + // 1. trivial: 1 def (or less), uses nothing sensitive, can be eliminated + // 2. safe: 1 def (or less), can be used in a trivial, but cannot itself be eliminated + // 3. sensitive: uses a global or memory or something else that prevents trivial elimination. + + function assessTriviality(name) { + // only care about vars with 0-1 assignments of (0 for parameters), and can ignore label (which is not explicitly initialized, but cannot be eliminated ever anyhow) + if (assignments[name] > 1 || (!(name in asmData.vars) && !(name in asmData.params)) || name == 'label') return false; + if (considered[name]) return allTrivials[name]; + considered[name] = true; + var sensitive = false; + var size = 0, originalSize = 0; + var def = defs[name]; + if (def) { + var value = def[3]; + originalSize = measureSize(value); + if (value) { + traverse(value, function recurseValue(node, type) { + var one = node[1]; + if (!(type in NODES_WITHOUT_ELIMINATION_SENSITIVITY)) { // || (type == 'binary' && !(one in FAST_ELIMINATION_BINARIES))) { + sensitive = true; + return true; + } + if (type == 'name' && !assessTriviality(one)) { + if (assignments[one] > 1 || (!(one in asmData.vars) && !(one in asmData.params))) { + sensitive = true; // directly using something sensitive return true; - } - if (type == 'name' && !assessTriviality(one)) { - if (assignments[one] > 1 || (!(one in asmData.vars) && !(one in asmData.params))) { - sensitive = true; // directly using something sensitive - return true; - } // otherwise, not trivial, but at least safe. - } - // if this is a name, it must be a trivial variable (or a safe one) and we know its size - size += ((type == 'name') ? allTrivials[one] : 1) || 1; - }); - } - } - if (!sensitive) { - size = size || 1; - originalSize = originalSize || 1; - var factor = ((appearances[name] - 1) || 0) * (size - originalSize); // If no size change or just one appearance, always ok to trivially eliminate. otherwise, tradeoff - if (factor <= 12) { - allTrivials[name] = size; // trivial! - return true; - } + } // otherwise, not trivial, but at least safe. + } + // if this is a name, it must be a trivial variable (or a safe one) and we know its size + size += ((type == 'name') ? allTrivials[one] : 1) || 1; + }); } - return false; } - for (var name in asmData.vars) { - assessTriviality(name); + if (!sensitive) { + size = size || 1; + originalSize = originalSize || 1; + var factor = ((appearances[name] - 1) || 0) * (size - originalSize); // If no size change or just one appearance, always ok to trivially eliminate. otherwise, tradeoff + if (factor <= 12) { + allTrivials[name] = size; // trivial! + return true; + } } - var trivials = {}; + return false; + } + for (var name in asmData.vars) { + assessTriviality(name); + } + var trivials = {}; - for (var name in allTrivials) { // from now on, ignore parameters - if (name in asmData.vars) trivials[name] = true; - } + for (var name in allTrivials) { // from now on, ignore parameters + if (name in asmData.vars) trivials[name] = true; + } - allTrivials = {}; + allTrivials = {}; - var values = {}; + var values = {}, recursives = {}; - function evaluate(name) { - var node = values[name]; - if (node) return node; - values[node] = null; // prevent infinite recursion - var def = defs[name]; - if (def) { - node = def[3]; - if (node[0] == 'name') { - var name2 = node[1]; - if (name2 in trivials) { - node = evaluate(name2); - } - } else { - traverse(node, function(node, type) { - if (type == 'name') { - var name2 = node[1]; - if (name2 in trivials) { - return evaluate(name2); - } - } - }); + function evaluate(name) { + var node = values[name]; + if (node) return node; + values[name] = null; // prevent infinite recursion + var def = defs[name]; + if (def) { + node = def[3]; + if (node[0] == 'name') { + var name2 = node[1]; + assert(name2 !== name); + if (name2 in trivials) { + node = evaluate(name2); } - values[name] = node; + } else { + traverse(node, function(node, type) { + if (type == 'name') { + var name2 = node[1]; + if (name2 === name) { + recursives[name] = 1; + return false; + } + if (name2 in trivials) { + return evaluate(name2); + } + } + }); } - return node; + values[name] = node; } + return node; + } - for (var name in trivials) { - evaluate(name); + for (var name in trivials) { + evaluate(name); + } + for (var name in recursives) { + delete trivials[name]; + } + + for (var name in trivials) { + var def = defs[name]; + if (def) { + def.length = 0; + def[0] = 'toplevel'; + def[1] = []; } + delete asmData.vars[name]; + } - for (var name in trivials) { - var def = defs[name]; - if (def) { - def.length = 0; - def[0] = 'toplevel'; - def[1] = []; + // Perform replacements TODO: save list of uses objects before, replace directly, avoid extra traverse + traverse(func, function(node, type) { + if (type == 'name') { + var name = node[1]; + if (name in trivials) { + var value = values[name]; + if (!value) throw 'missing value: ' + [func[1], name, values[name]] + ' - faulty reliance on asm zero-init?'; + return copy(value); // must copy, or else the same object can be used multiple times } - delete asmData.vars[name]; } + }); +} - // Perform replacements TODO: save list of uses objects before, replace directly, avoid extra traverse - traverse(func, function(node, type) { - if (type == 'name') { - var name = node[1]; - if (name in trivials) { - var value = values[name]; - if (!value) throw 'missing value: ' + [func[1], name, values[name]] + ' - faulty reliance on asm zero-init?'; - return copy(value); // must copy, or else the same object can be used multiple times - } - } - }); - } +function aggressiveVariableElimination(ast) { + assert(asm, 'need ASM_JS for aggressive variable elimination'); + traverseGeneratedFunctions(ast, function(func, type) { + var asmData = normalizeAsm(func); + aggressiveVariableEliminationInternal(func, asmData); + denormalizeAsm(func, asmData); + }); +} +function outline(ast) { // Try to flatten out code as much as possible, to make outlining more feasible. function flatten(func, asmData) { var minSize = extraInfo.sizeToOutline/4; @@ -3740,7 +3758,7 @@ function outline(ast) { var size = measureSize(func); if (size >= extraInfo.sizeToOutline && maxTotalFunctions > 0) { maxTotalFunctions--; - aggressiveVariableElimination(func, asmData); + aggressiveVariableEliminationInternal(func, asmData); flatten(func, asmData); analyzeFunction(func, asmData); calculateThreshold(func, asmData); @@ -3921,6 +3939,7 @@ var passes = { registerize: registerize, eliminate: eliminate, eliminateMemSafe: eliminateMemSafe, + aggressiveVariableElimination: aggressiveVariableElimination, minifyGlobals: minifyGlobals, relocate: relocate, outline: outline, diff --git a/tools/shared.py b/tools/shared.py index 11fc7fb7..4ab476d3 100644 --- a/tools/shared.py +++ b/tools/shared.py @@ -345,7 +345,7 @@ def find_temp_directory(): # 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.8.5' +EMSCRIPTEN_VERSION = '1.8.6' def generate_sanity(): return EMSCRIPTEN_VERSION + '|' + get_llvm_target() + '|' + LLVM_ROOT + '|' + get_clang_version() @@ -1414,6 +1414,8 @@ class Building: if not os.path.exists(CLOSURE_COMPILER): raise Exception('Closure compiler appears to be missing, looked at: ' + str(CLOSURE_COMPILER)) + CLOSURE_EXTERNS = path_from_root('src', 'closure-externs.js') + # Something like this (adjust memory as needed): # java -Xmx1024m -jar CLOSURE_COMPILER --compilation_level ADVANCED_OPTIMIZATIONS --variable_map_output_file src.cpp.o.js.vars --js src.cpp.o.js --js_output_file src.cpp.o.cc.js args = [JAVA, @@ -1421,6 +1423,7 @@ class Building: '-jar', CLOSURE_COMPILER, '--compilation_level', 'ADVANCED_OPTIMIZATIONS', '--language_in', 'ECMASCRIPT5', + '--externs', CLOSURE_EXTERNS, #'--variable_map_output_file', filename + '.vars', '--js', filename, '--js_output_file', filename + '.cc.js'] if pretty: args += ['--formatting', 'PRETTY_PRINT'] |