diff options
-rw-r--r-- | src/compiler.js | 2 | ||||
-rw-r--r-- | src/jsifier.js | 8 | ||||
-rw-r--r-- | src/modules.js | 20 | ||||
-rw-r--r-- | src/preamble.js | 4 | ||||
-rw-r--r-- | src/runtime.js | 8 | ||||
-rw-r--r-- | src/settings.js | 3 | ||||
-rw-r--r-- | tests/runner.py | 2 | ||||
-rw-r--r-- | third_party/demangler.py | 9 |
8 files changed, 37 insertions, 19 deletions
diff --git a/src/compiler.js b/src/compiler.js index 1a48fad2..c0bd64d3 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -32,6 +32,8 @@ if (CORRECT_ROUNDINGS === 2) { CORRECT_ROUNDINGS_LINES = set(CORRECT_ROUNDINGS_LINES); // for fast checking } +EXPORTED_FUNCTIONS = set(EXPORTED_FUNCTIONS); + // Load compiler code load('framework.js'); diff --git a/src/jsifier.js b/src/jsifier.js index 2c33eba1..7a7bc47f 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -117,7 +117,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { function indexizeFunctions(value) { // TODO: Also check for other functions (externals, library, etc.) if (value in FUNCTIONS) { - value = value + '.__index__'; // Store integer value + value = Functions.getIndex(value); // Store integer value } return value; } @@ -537,7 +537,10 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { func.JS += ' return' + (func.returnType !== 'void' ? ' null' : '') + ';\n'; } func.JS += '}\n'; - func.JS += func.ident + '.__index__ = Runtime.getFunctionIndex(' + func.ident + ', "' + func.ident + '");\n'; + if (func.ident in EXPORTED_FUNCTIONS) { + func.JS += 'Module["' + func.ident + '"] = ' + func.ident + ';'; + } + return func; } }); @@ -1170,6 +1173,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { var pre = processMacros(preprocess(read('preamble.js').replace('{{RUNTIME}}', getRuntime()), CONSTANTS)); print(pre); generated.forEach(function(item) { print(indentify(item.JS || '', 2)); }); + print(Functions.generateIndexing()); var postParts = processMacros(preprocess(read('postamble.js'), CONSTANTS)).split('{{GLOBAL_VARS}}'); print(postParts[0]); diff --git a/src/modules.js b/src/modules.js index 4d225057..804de24a 100644 --- a/src/modules.js +++ b/src/modules.js @@ -114,3 +114,23 @@ var Types = { needAnalysis: {} // Types noticed during parsing, that need analysis }; +var Functions = { + indexedFunctions: [0, 0], // Start at a non-0 (even, see below) value + + // Mark a function as needing indexing, and returns the index + getIndex: function(ident) { + var key = this.indexedFunctions.indexOf(ident); + if (key < 0) { + key = this.indexedFunctions.length; + this.indexedFunctions[key] = ident; + this.indexedFunctions[key+1] = 0; // Need to have keys be even numbers, see |polymorph| test + } + return key.toString(); + }, + + // Generate code for function indexing + generateIndexing: function() { + return 'var FUNCTION_TABLE = [' + this.indexedFunctions.toString().replace('"', '') + '];'; + } +}; + diff --git a/src/preamble.js b/src/preamble.js index 9f457a5c..a2a66bd6 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -157,10 +157,6 @@ var START_TIME = Date.now(); function __globalConstructor__() { } -// Maps ints ==> functions. This lets us pass around ints, which are -// actually pointers to functions, and we convert at call()time -var FUNCTION_TABLE = []; - var __THREW__ = false; // Used in checking for thrown exceptions. var __ATEXIT__ = []; diff --git a/src/runtime.js b/src/runtime.js index 6ab78611..acd5ed8d 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -77,14 +77,6 @@ Runtime = { staticAlloc: unInline('staticAlloc', ['size']), alignMemory: unInline('alignMemory', ['size', 'quantum']), - getFunctionIndex: function getFunctionIndex(func, ident) { - var key = FUNCTION_TABLE.length; - FUNCTION_TABLE[key] = func; - FUNCTION_TABLE[key+1] = null; // Need to have keys be even numbers, see |polymorph| test - Module[ident] = func; // Export using full name, for Closure Compiler - return key; - }, - // TODO: cleanup isNumberType: function(type) { return type in Runtime.INT_TYPES || type in Runtime.FLOAT_TYPES; diff --git a/src/settings.js b/src/settings.js index ceda7ec8..a93d9183 100644 --- a/src/settings.js +++ b/src/settings.js @@ -68,6 +68,9 @@ CORRECT_ROUNDINGS = 1; // C rounds to 0 (-5.5 to -5, +5.5 to 5), while JS has no // Math.floor is to negative, ceil to positive. With CORRECT_ROUNDINGS, // we will do slow but correct C rounding operations. +EXPORTED_FUNCTIONS = ['_main']; // Functions that are explicitly exported, so they are guaranteed to + // be accessible outside of the generated code. + SHOW_LABELS = 0; // Show labels in the generated code // Compiler debugging options diff --git a/tests/runner.py b/tests/runner.py index e5cc3a27..fba28828 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -1896,7 +1896,7 @@ if 'benchmark' not in sys.argv: print('*ok*'); ''' def post(filename): - Popen(['python', DEMANGLER, filename, '.'], stdout=open(filename + '.tmp', 'w')).communicate() + Popen(['python', DEMANGLER, filename], stdout=open(filename + '.tmp', 'w')).communicate() Popen(['python', NAMESPACER, filename + '.tmp'], stdout=open(filename + '.tmp2', 'w')).communicate() src = open(filename, 'r').read().replace( '// {{MODULE_ADDITIONS}', diff --git a/third_party/demangler.py b/third_party/demangler.py index f3407825..ed8d52b0 100644 --- a/third_party/demangler.py +++ b/third_party/demangler.py @@ -19,7 +19,7 @@ JS_ENGINE_PARAMS=[] ''' -import os, sys, subprocess +import os, sys, subprocess, re abspath = os.path.abspath(os.path.dirname(__file__)) def path_from_root(*pathelems): @@ -27,14 +27,15 @@ def path_from_root(*pathelems): exec(open(path_from_root('tools', 'shared.py'), 'r').read()) data = open(sys.argv[1], 'r').readlines() -splitter = sys.argv[2] SEEN = {} for line in data: if len(line) < 4: continue if line[:2] != ' ': continue - if line[2] != '_': continue - func = line.lstrip().split(splitter)[0] + if line[2] != 'f': continue + m = re.match('^ function (?P<func>[^(]+)\(.*', line) + if not m: continue + func = m.groups('func')[0] if func in SEEN: continue SEEN[func] = True cleaned = run_js(JS_ENGINE, path_from_root('third_party', 'gcc_demangler.js'), [func[1:]]) |