Skip to content

De-runtime-ification #5954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Dec 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 15 additions & 34 deletions emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ def parse_backend_output(backend_output, DEBUG):
metadata_raw = backend_output[metadata_split+len(metadata_split_marker):]
mem_init = backend_output[end_funcs+len(end_funcs_marker):metadata_split]

# we no longer use the "Runtime" object. TODO: stop emiting it in the backend
mem_init = mem_init.replace('Runtime.', '')

try:
#if DEBUG: print >> sys.stderr, "METAraw", metadata_raw
metadata = json.loads(metadata_raw, object_pairs_hook=OrderedDict)
Expand Down Expand Up @@ -378,8 +381,6 @@ def create_module(function_table_sigs, metadata, settings,

asm_end = create_asm_end(exports, settings)

runtime_library_overrides = create_runtime_library_overrides(settings)

module = [
asm_start,
temp_float,
Expand All @@ -389,11 +390,11 @@ def create_module(function_table_sigs, metadata, settings,
start_funcs_marker
] + runtime_funcs + funcs_js + ['\n ',
pre_tables, final_function_tables, asm_end,
'\n', receiving, ';\n', runtime_library_overrides]
'\n', receiving, ';\n']

if settings['SIDE_MODULE']:
module.append('''
Runtime.registerFunctions(%(sigs)s, Module);
parentModule['registerFunctions'](%(sigs)s, Module);
''' % { 'sigs': str(list(map(str, function_table_sigs))) })

return module
Expand Down Expand Up @@ -564,7 +565,7 @@ def memory_and_global_initializers(pre, metadata, mem_init, settings):
mem_init=mem_init))

if settings['SIDE_MODULE']:
pre = pre.replace('Runtime.GLOBAL_BASE', 'gb')
pre = pre.replace('GLOBAL_BASE', 'gb')
if settings['SIDE_MODULE'] or settings['BINARYEN']:
pre = pre.replace('{{{ STATIC_BUMP }}}', str(staticbump))

Expand Down Expand Up @@ -651,8 +652,8 @@ def get_implemented_functions(metadata):

def proxy_debug_print(call_type, settings):
if shared.Settings.PTHREADS_DEBUG:
if call_type == 'sync_on_main_thread_': return 'Runtime.warnOnce("sync proxying function " + code);';
elif call_type == 'async_on_main_thread_': return 'Runtime.warnOnce("async proxying function " + code);';
if call_type == 'sync_on_main_thread_': return 'warnOnce("sync proxying function " + code);';
elif call_type == 'async_on_main_thread_': return 'warnOnce("async proxying function " + code);';
return ''

def include_asm_consts(pre, forwarded_json, metadata, settings):
Expand Down Expand Up @@ -1151,9 +1152,8 @@ def table_size(table):
if not settings['EMULATED_FUNCTION_POINTERS']:
asm_setup += "\nModule['wasmMaxTableSize'] = %d;\n" % table_total_size
if settings['RELOCATABLE']:
asm_setup += 'var setTempRet0 = Runtime.setTempRet0, getTempRet0 = Runtime.getTempRet0;\n'
if not settings['SIDE_MODULE']:
asm_setup += 'var gb = Runtime.GLOBAL_BASE, fb = 0;\n'
asm_setup += 'var gb = GLOBAL_BASE, fb = 0;\n'
side = 'parent' if settings['SIDE_MODULE'] else ''
def check(extern):
if settings['ASSERTIONS']:
Expand Down Expand Up @@ -1601,25 +1601,6 @@ def create_asm_end(exports, settings):
'Module' + access_quote('asmLibraryArg'))


def create_runtime_library_overrides(settings):
overrides = []
if not settings.get('SIDE_MODULE'):
overrides += [
'stackAlloc',
'stackSave',
'stackRestore',
'establishStackSpace',
]
if settings['SAFE_HEAP']:
overrides.append('setDynamicTop')

if not settings['RELOCATABLE']:
overrides += ['setTempRet0', 'getTempRet0']

lines = ["Runtime.{0} = Module['{0}'];".format(func) for func in overrides]
return '\n'.join(lines)


def create_first_in_asm(settings):
first_in_asm = ''
if settings['SPLIT_MEMORY']:
Expand Down Expand Up @@ -1995,15 +1976,15 @@ def create_module_wasm(sending, receiving, invoke_funcs, settings):
STACKTOP = STACK_BASE + TOTAL_STACK;
STACK_MAX = STACK_BASE;
HEAP32[%d >> 2] = STACKTOP;
Runtime.stackAlloc = Module['_stackAlloc'];
Runtime.stackSave = Module['_stackSave'];
Runtime.stackRestore = Module['_stackRestore'];
Runtime.establishStackSpace = Module['establishStackSpace'];
stackAlloc = Module['_stackAlloc'];
stackSave = Module['_stackSave'];
stackRestore = Module['_stackRestore'];
establishStackSpace = Module['establishStackSpace'];
''' % shared.Settings.GLOBAL_BASE)

module.append('''
Runtime.setTempRet0 = Module['setTempRet0'];
Runtime.getTempRet0 = Module['getTempRet0'];
setTempRet0 = Module['setTempRet0'];
getTempRet0 = Module['getTempRet0'];
''')

module.append(invoke_wrappers)
Expand Down
10 changes: 5 additions & 5 deletions src/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ function emscripten_start_fetch(fetch, successcb, errorcb, progresscb) {
#if FETCH_DEBUG
console.log('fetch: operation success. e: ' + e);
#endif
if (onsuccess && Runtime.dynCall) Module['dynCall_vi'](onsuccess, fetch);
if (onsuccess && typeof dynCall === 'function') Module['dynCall_vi'](onsuccess, fetch);
else if (successcb) successcb(fetch);
};

Expand All @@ -497,29 +497,29 @@ function emscripten_start_fetch(fetch, successcb, errorcb, progresscb) {
#if FETCH_DEBUG
console.log('fetch: IndexedDB store succeeded.');
#endif
if (onsuccess && Runtime.dynCall) Module['dynCall_vi'](onsuccess, fetch);
if (onsuccess && typeof dynCall === 'function') Module['dynCall_vi'](onsuccess, fetch);
else if (successcb) successcb(fetch);
};
var storeError = function(fetch, xhr, e) {
#if FETCH_DEBUG
console.error('fetch: IndexedDB store failed.');
#endif
if (onsuccess && Runtime.dynCall) Module['dynCall_vi'](onsuccess, fetch);
if (onsuccess && typeof dynCall === 'function') Module['dynCall_vi'](onsuccess, fetch);
else if (successcb) successcb(fetch);
};
__emscripten_fetch_cache_data(Fetch.dbInstance, fetch, xhr.response, storeSuccess, storeError);
};

var reportProgress = function(fetch, xhr, e) {
if (onprogress && Runtime.dynCall) Module['dynCall_vi'](onprogress, fetch);
if (onprogress && typeof dynCall === 'function') Module['dynCall_vi'](onprogress, fetch);
else if (progresscb) progresscb(fetch);
};

var reportError = function(fetch, xhr, e) {
#if FETCH_DEBUG
console.error('fetch: operation failed: ' + e);
#endif
if (onerror && Runtime.dynCall) Module['dynCall_vi'](onerror, fetch);
if (onerror && typeof dynCall === 'function') Module['dynCall_vi'](onerror, fetch);
else if (errorcb) errorcb(fetch);
};

Expand Down
3 changes: 3 additions & 0 deletions src/closure-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,9 @@ var VoidPtr;

// Various Emscripten-specific global variables

/**
* @suppress {duplicate}
*/
var tempRet0;
var tempI64;
var tempDouble;
Expand Down
31 changes: 18 additions & 13 deletions src/jsifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ function JSify(data, functionsOnly) {

var shellParts = read(shellFile).split('{{BODY}}');
print(processMacros(preprocess(shellParts[0], shellFile)));
var preFile = BUILD_AS_SHARED_LIB || SIDE_MODULE ? 'preamble_sharedlib.js' : 'preamble.js';
var pre = processMacros(preprocess(read(preFile).replace('{{RUNTIME}}', getRuntime()), preFile));
var pre;
if (BUILD_AS_SHARED_LIB || SIDE_MODULE) {
pre = processMacros(preprocess(read('preamble_sharedlib.js'), 'preamble_sharedlib.js'));
} else {
pre = processMacros(preprocess(read('support.js'), 'support.js')) +
processMacros(preprocess(read('preamble.js'), 'preamble.js'));
}
print(pre);
}

Expand Down Expand Up @@ -114,8 +119,8 @@ function JSify(data, functionsOnly) {
// name the function; overwrite if it's already named
snippet = snippet.replace(/function(?:\s+([^(]+))?\s*\(/, 'function ' + finalName + '(');
if (LIBRARY_DEBUG && !LibraryManager.library[ident + '__asm']) {
snippet = snippet.replace('{', '{ var ret = (function() { if (Runtime.debug) Module.printErr("[library call:' + finalName + ': " + Array.prototype.slice.call(arguments).map(Runtime.prettyPrint) + "]"); ');
snippet = snippet.substr(0, snippet.length-1) + '}).apply(this, arguments); if (Runtime.debug && typeof ret !== "undefined") Module.printErr(" [ return:" + Runtime.prettyPrint(ret)); return ret; \n}';
snippet = snippet.replace('{', '{ var ret = (function() { if (runtimeDebug) Module.printErr("[library call:' + finalName + ': " + Array.prototype.slice.call(arguments).map(prettyPrint) + "]"); ');
snippet = snippet.substr(0, snippet.length-1) + '}).apply(this, arguments); if (runtimeDebug && typeof ret !== "undefined") Module.printErr(" [ return:" + prettyPrint(ret)); return ret; \n}';
}
return snippet;
}
Expand All @@ -130,7 +135,7 @@ function JSify(data, functionsOnly) {
}

var func = "function _emscripten_" + (sync ? '' : 'a') + 'sync_run_in_browser_thread_' + sig + '(func' + argsList(sig.length-1) + ') {\n';
if (sync) func += ' var waitAddress = Runtime.stackSave();\n';
if (sync) func += ' var waitAddress = stackSave();\n';

function sizeofType(t) {
switch(t) {
Expand Down Expand Up @@ -421,10 +426,10 @@ function JSify(data, functionsOnly) {
Variables.generatedGlobalBase = true;
// Globals are done, here is the rest of static memory
if (!SIDE_MODULE) {
print('STATIC_BASE = Runtime.GLOBAL_BASE;\n');
print('STATIC_BASE = GLOBAL_BASE;\n');
print('STATICTOP = STATIC_BASE + ' + Runtime.alignMemory(Variables.nextIndexedOffset) + ';\n');
} else {
print('gb = Runtime.alignMemory(getMemory({{{ STATIC_BUMP }}}, ' + MAX_GLOBAL_ALIGN + ' || 1));\n');
print('gb = alignMemory(getMemory({{{ STATIC_BUMP }}}, ' + MAX_GLOBAL_ALIGN + ' || 1));\n');
print('// STATICTOP = STATIC_BASE + ' + Runtime.alignMemory(Variables.nextIndexedOffset) + ';\n'); // comment as metadata only
}
if (BINARYEN) {
Expand Down Expand Up @@ -461,7 +466,7 @@ function JSify(data, functionsOnly) {
if (USE_PTHREADS) {
print('if (!ENVIRONMENT_IS_PTHREAD) {') // Pthreads should not initialize memory again, since it's shared with the main thread.
}
print('/* memory initializer */ ' + makePointer(memoryInitialization, null, 'ALLOC_NONE', 'i8', 'Runtime.GLOBAL_BASE' + (SIDE_MODULE ? '+H_BASE' : ''), true));
print('/* memory initializer */ ' + makePointer(memoryInitialization, null, 'ALLOC_NONE', 'i8', 'GLOBAL_BASE' + (SIDE_MODULE ? '+H_BASE' : ''), true));
if (USE_PTHREADS) {
print('}')
}
Expand All @@ -472,7 +477,7 @@ function JSify(data, functionsOnly) {
if (!BUILD_AS_SHARED_LIB && !SIDE_MODULE) {
if (USE_PTHREADS) {
print('var tempDoublePtr;\n');
print('if (!ENVIRONMENT_IS_PTHREAD) tempDoublePtr = Runtime.alignMemory(allocate(12, "i8", ALLOC_STATIC), 8);\n');
print('if (!ENVIRONMENT_IS_PTHREAD) tempDoublePtr = alignMemory(allocate(12, "i8", ALLOC_STATIC), 8);\n');
} else {
print('var tempDoublePtr = ' + makeStaticAlloc(8) + '\n');
}
Expand Down Expand Up @@ -519,11 +524,11 @@ function JSify(data, functionsOnly) {
for(i in proxiedFunctionInvokers) print(proxiedFunctionInvokers[i]+'\n');
print('if (!ENVIRONMENT_IS_PTHREAD) {\n // Only main thread initializes these, pthreads copy them over at thread worker init time (in pthread-main.js)');
}
print('DYNAMICTOP_PTR = Runtime.staticAlloc(4);\n');
print('STACK_BASE = STACKTOP = Runtime.alignMemory(STATICTOP);\n');
if (STACK_START > 0) print('if (STACKTOP < ' + STACK_START + ') STACK_BASE = STACKTOP = Runtime.alignMemory(' + STACK_START + ');\n');
print('DYNAMICTOP_PTR = staticAlloc(4);\n');
print('STACK_BASE = STACKTOP = alignMemory(STATICTOP);\n');
if (STACK_START > 0) print('if (STACKTOP < ' + STACK_START + ') STACK_BASE = STACKTOP = alignMemory(' + STACK_START + ');\n');
print('STACK_MAX = STACK_BASE + TOTAL_STACK;\n');
print('DYNAMIC_BASE = Runtime.alignMemory(STACK_MAX);\n');
print('DYNAMIC_BASE = alignMemory(STACK_MAX);\n');
print('HEAP32[DYNAMICTOP_PTR>>2] = DYNAMIC_BASE;\n');
print('staticSealed = true; // seal the static portion of memory\n');
if (ASSERTIONS) print('assert(DYNAMIC_BASE < TOTAL_MEMORY, "TOTAL_MEMORY not big enough for stack");\n');
Expand Down
34 changes: 17 additions & 17 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,14 +565,14 @@ LibraryManager.library = {
* not an issue.
*/
#if ASSERTIONS == 2
Runtime.warnOnce('using stub malloc (reference it from C to have the real one included)');
warnOnce('using stub malloc (reference it from C to have the real one included)');
#endif
var ptr = Runtime.dynamicAlloc(bytes + 8);
var ptr = dynamicAlloc(bytes + 8);
return (ptr+8) & 0xFFFFFFF8;
},
free: function() {
#if ASSERTIONS == 2
Runtime.warnOnce('using stub free (reference it from C to have the real one included)');
warnOnce('using stub free (reference it from C to have the real one included)');
#endif
},

Expand All @@ -598,7 +598,7 @@ LibraryManager.library = {
atexit: function(func, arg) {
#if ASSERTIONS
#if NO_EXIT_RUNTIME == 1
Runtime.warnOnce('atexit() called, but NO_EXIT_RUNTIME is set, so atexits() will not be called. set NO_EXIT_RUNTIME to 0 (see the FAQ)');
warnOnce('atexit() called, but NO_EXIT_RUNTIME is set, so atexits() will not be called. set NO_EXIT_RUNTIME to 0 (see the FAQ)');
#endif
#endif
__ATEXIT__.unshift({ func: func, arg: arg });
Expand Down Expand Up @@ -641,7 +641,7 @@ LibraryManager.library = {
ENV['_'] = Module['thisProgram'];
// Allocate memory.
poolPtr = allocate(TOTAL_ENV_SIZE, 'i8', ALLOC_STATIC);
envPtr = allocate(MAX_ENV_VALUES * {{{ Runtime.QUANTUM_SIZE }}},
envPtr = allocate(MAX_ENV_VALUES * {{{ Runtime.POINTER_SIZE }}},
'i8*', ALLOC_STATIC);
{{{ makeSetValue('envPtr', '0', 'poolPtr', 'i8*') }}};
{{{ makeSetValue(makeGlobalUse('_environ'), 0, 'envPtr', 'i8*') }}};
Expand Down Expand Up @@ -1432,14 +1432,14 @@ LibraryManager.library = {
if (!self.LLVM_SAVEDSTACKS) {
self.LLVM_SAVEDSTACKS = [];
}
self.LLVM_SAVEDSTACKS.push(Runtime.stackSave());
self.LLVM_SAVEDSTACKS.push(stackSave());
return self.LLVM_SAVEDSTACKS.length-1;
},
llvm_stackrestore: function(p) {
var self = _llvm_stacksave;
var ret = self.LLVM_SAVEDSTACKS[p];
self.LLVM_SAVEDSTACKS.splice(p, 1);
Runtime.stackRestore(ret);
stackRestore(ret);
},

__cxa_pure_virtual: function() {
Expand Down Expand Up @@ -1707,12 +1707,12 @@ LibraryManager.library = {
var lib_data = FS.readFile(filename, { encoding: 'binary' });
if (!(lib_data instanceof Uint8Array)) lib_data = new Uint8Array(lib_data);
//Module.printErr('libfile ' + filename + ' size: ' + lib_data.length);
lib_module = Runtime.loadWebAssemblyModule(lib_data);
lib_module = loadWebAssemblyModule(lib_data);
#else
// the shared library is a JS file, which we eval
var lib_data = FS.readFile(filename, { encoding: 'utf8' });
lib_module = eval(lib_data)(
Runtime.alignFunctionTables(),
alignFunctionTables(),
Module
);
#endif
Expand Down Expand Up @@ -1813,7 +1813,7 @@ LibraryManager.library = {
} else {
var result = lib.module[symbol];
if (typeof result == 'function') {
result = Runtime.addFunction(result);
result = addFunction(result);
//Module.printErr('adding function dlsym result for ' + symbol + ' => ' + result);
lib.cached_functions = result;
}
Expand Down Expand Up @@ -2005,7 +2005,7 @@ LibraryManager.library = {
var dst = (summerOffset != winterOffset && date.getTimezoneOffset() == Math.min(winterOffset, summerOffset))|0;
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_isdst, 'dst', 'i32') }}};

var zonePtr = {{{ makeGetValue(makeGlobalUse('_tzname'), 'dst ? Runtime.QUANTUM_SIZE : 0', 'i32') }}};
var zonePtr = {{{ makeGetValue(makeGlobalUse('_tzname'), 'dst ? ' + Runtime.QUANTUM_SIZE + ' : 0', 'i32') }}};
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_zone, 'zonePtr', 'i32') }}};

return tmPtr;
Expand Down Expand Up @@ -2052,9 +2052,9 @@ LibraryManager.library = {

ctime_r__deps: ['localtime_r', 'asctime_r'],
ctime_r: function(time, buf) {
var stack = Runtime.stackSave();
var rv = _asctime_r(_localtime_r(time, Runtime.stackAlloc({{{ C_STRUCTS.tm.__size__ }}})), buf);
Runtime.stackRestore(stack);
var stack = stackSave();
var rv = _asctime_r(_localtime_r(time, stackAlloc({{{ C_STRUCTS.tm.__size__ }}})), buf);
stackRestore(stack);
return rv;
},

Expand Down Expand Up @@ -4048,7 +4048,7 @@ LibraryManager.library = {

// If user requested to see the original source stack, but no source map information is available, just fall back to showing the JS stack.
if (flags & 8/*EM_LOG_C_STACK*/ && typeof emscripten_source_map === 'undefined') {
Runtime.warnOnce('Source map information is not available, emscripten_log with EM_LOG_C_STACK will be ignored. Build with "--pre-js $EMSCRIPTEN/src/emscripten-source-map.min.js" linker flag to add source map loading to code.');
warnOnce('Source map information is not available, emscripten_log with EM_LOG_C_STACK will be ignored. Build with "--pre-js $EMSCRIPTEN/src/emscripten-source-map.min.js" linker flag to add source map loading to code.');
flags ^= 8/*EM_LOG_C_STACK*/;
flags |= 16/*EM_LOG_JS_STACK*/;
}
Expand Down Expand Up @@ -4176,7 +4176,7 @@ LibraryManager.library = {
emscripten_log: function(flags, varargs) {
// Extract the (optionally-existing) printf format specifier field from varargs.
var format = {{{ makeGetValue('varargs', '0', 'i32', undefined, undefined, true) }}};
varargs += Math.max(Runtime.getNativeFieldSize('i32'), Runtime.getAlignSize('i32', null, true));
varargs += {{{ Math.max(Runtime.getNativeFieldSize('i32'), Runtime.getAlignSize('i32', null, true)) }}};
var str = '';
if (format) {
var result = __formatString(format, varargs);
Expand All @@ -4190,7 +4190,7 @@ LibraryManager.library = {
emscripten_get_compiler_setting: function(name) {
name = Pointer_stringify(name);

var ret = Runtime.getCompilerSetting(name);
var ret = getCompilerSetting(name);
if (typeof ret === 'number') return ret;

if (!_emscripten_get_compiler_setting.cache) _emscripten_get_compiler_setting.cache = {};
Expand Down
8 changes: 4 additions & 4 deletions src/library_bootstrap_structInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ LibraryManager.library = {
if (!self.called) {
HEAP32[DYNAMICTOP_PTR>>2] = alignUp(HEAP32[DYNAMICTOP_PTR>>2], 16777216); // make sure we start out aligned
self.called = true;
assert(Runtime.dynamicAlloc);
self.alloc = Runtime.dynamicAlloc;
Runtime.dynamicAlloc = function() { abort('cannot dynamically allocate, sbrk now has control') };
assert(dynamicAlloc);
self.alloc = dynamicAlloc;
dynamicAlloc = function() { abort('cannot dynamically allocate, sbrk now has control') };
}
var ret = HEAP32[DYNAMICTOP_PTR>>2];
if (bytes != 0) self.alloc(bytes);
return ret; // Previous break location.
},
malloc: function(x) {
return Runtime.dynamicAlloc(x);
return dynamicAlloc(x);
},
};

Loading