From 6782b5e97fcfb6a6da94b9ecc2fa57b76e943206 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 27 Nov 2012 09:51:36 -0800 Subject: [PATCH] switch to all or nothing named globals, and add testing --- src/intertyper.js | 2 +- src/jsifier.js | 15 ++++----------- src/modules.js | 1 - src/parseTools.js | 10 ++++++++-- src/settings.js | 7 +++---- tests/runner.py | 7 ++++--- 6 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/intertyper.js b/src/intertyper.js index dbd5f4585..124dbeb87 100644 --- a/src/intertyper.js +++ b/src/intertyper.js @@ -507,7 +507,7 @@ function intertyper(data, sidePass, baseLineNums) { private_: private_, lineNum: item.lineNum }; - if (NUM_NAMED_GLOBALS >= 0) { + if (!NAMED_GLOBALS) { Variables.globals[ret.ident].type = ret.type; } Types.needAnalysis[ret.type] = 0; diff --git a/src/jsifier.js b/src/jsifier.js index d62fa7887..e218df052 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -250,7 +250,7 @@ function JSify(data, functionsOnly, givenFunctions) { processItem: function(item) { function needsPostSet(value) { return value[0] in UNDERSCORE_OPENPARENS || value.substr(0, 14) === 'CHECK_OVERFLOW' - || false; // TODO: interact with output of makeGlobalUse/makeGlobalDef + || value.substr(0, 6) === 'GLOBAL'; } item.intertype = 'GlobalVariableStub'; @@ -265,16 +265,9 @@ function JSify(data, functionsOnly, givenFunctions) { var constant = null; var allocator = (BUILD_AS_SHARED_LIB && !item.external) ? 'ALLOC_NORMAL' : 'ALLOC_STATIC'; var index = null; - if (NUM_NAMED_GLOBALS >= 0) { - if (Variables.seenGlobals < NUM_NAMED_GLOBALS) { - Variables.seenGlobals++; // named - } else { - // indexed - Variables.indexedGlobals[item.ident] = Variables.nextIndexedOffset; - index = makeGlobalUse(item.ident); - Variables.nextIndexedOffset += Runtime.alignMemory(calcAllocatedSize(Variables.globals[item.ident].type)); - allocator = 'ALLOC_NONE'; - } + if (!NAMED_GLOBALS) { + index = makeGlobalUse(item.ident); + allocator = 'ALLOC_NONE'; } if (item.external && BUILD_AS_SHARED_LIB) { // External variables in shared libraries should not be declared as diff --git a/src/modules.js b/src/modules.js index 4f7fc784b..68626b570 100644 --- a/src/modules.js +++ b/src/modules.js @@ -178,7 +178,6 @@ var Variables = { indexedGlobals: {}, // for indexed globals, ident ==> index // Used in calculation of indexed globals nextIndexedOffset: 0, - seenGlobals: 0, }; var Types = { diff --git a/src/parseTools.js b/src/parseTools.js index 786d55c8f..5770a0d7e 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -357,12 +357,18 @@ function hasVarArgs(params) { } function makeGlobalDef(ident) { - if (ident in Variables.indexedGlobals) return ''; + if (!NAMED_GLOBALS) return ''; return 'var ' + ident + ';'; // TODO: add option for namespacing or offsetting to allow reducing the number of globals } function makeGlobalUse(ident) { - if (ident in Variables.indexedGlobals) return getFastValue('GLOBAL_BASE', '+', Variables.indexedGlobals[ident]); + if (!NAMED_GLOBALS) { + if (!(ident in Variables.indexedGlobals)) { + Variables.indexedGlobals[ident] = Variables.nextIndexedOffset; + Variables.nextIndexedOffset += Runtime.alignMemory(calcAllocatedSize(Variables.globals[ident].type)); + } + return getFastValue('GLOBAL_BASE', '+', Variables.indexedGlobals[ident]); + } return ident; // TODO: add option for namespacing or offsetting to allow reducing the number of globals } diff --git a/src/settings.js b/src/settings.js index 4881e1490..1d8805a80 100644 --- a/src/settings.js +++ b/src/settings.js @@ -194,10 +194,9 @@ var PGO = 0; // Profile-guided optimization. // All CORRECT_* options default to 1 with PGO builds. // See https://github.com/kripken/emscripten/wiki/Optimizing-Code for more info -var NUM_NAMED_GLOBALS = -1; // If >= 0, the number of globals we allow to be named. Other globals - // are then referred to by a base plus an offset (called an indexed global), - // saving global variables but adding runtime overhead. If -1, then we - // allow all globals to be named. +var NAMED_GLOBALS = 1; // If 1, we use global variables for globals. Otherwise + // they are referred to by a base plus an offset (called an indexed global), + // saving global variables but adding runtime overhead. var PROFILE = 0; // Enables runtime profiling. See test_profiling for a usage example. diff --git a/tests/runner.py b/tests/runner.py index 1beef857a..ccf6f2415 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -1645,9 +1645,9 @@ c5,de,15,8a return 0; } ''' - for named, expected in [(-1, 0), (0, 100), (1, 98), (5, 88), (1000, 0)]: + for named, expected in [(0, 100), (1, 0)]: print named - Settings.NUM_NAMED_GLOBALS = named + Settings.NAMED_GLOBALS = named self.do_run(src, '4:10,177,543,def\n4\nwowie\ntoo\n76\n5\n(null)\n/* a comment */\n// another\ntest\n', ['wowie', 'too', '74']) if self.emcc_args == []: gen = open(self.in_dir('src.cpp.o.js')).read() @@ -5956,7 +5956,7 @@ void*:16 self.do_run(path_from_root('tests', 'cubescript'), '*\nTemp is 33\n9\n5\nhello, everyone\n*', main_file='command.cpp') def test_gcc_unmangler(self): - Settings.NUM_NAMED_GLOBALS = 0 # test coverage for this + Settings.NAMED_GLOBALS = 0 # test coverage for this Building.COMPILER_TEST_OPTS = ['-I' + path_from_root('third_party')] @@ -7401,6 +7401,7 @@ class %s(T): Settings.EMULATE_UNALIGNED_ACCESSES = int(Settings.USE_TYPED_ARRAYS == 2 and Building.LLVM_OPTS == 2) Settings.DOUBLE_MODE = 1 if Settings.USE_TYPED_ARRAYS and Building.LLVM_OPTS == 0 else 0 Settings.PRECISE_I64_MATH = 0 + Settings.NAMED_GLOBALS = 0 if not (embetter and llvm_opts) else 1 Building.pick_llvm_opts(3)