diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/intertyper.js | 1 | ||||
-rw-r--r-- | src/jsifier.js | 23 | ||||
-rw-r--r-- | src/library.js | 158 | ||||
-rw-r--r-- | src/parseTools.js | 2 |
4 files changed, 172 insertions, 12 deletions
diff --git a/src/intertyper.js b/src/intertyper.js index 83a49645..7d74dfd2 100644 --- a/src/intertyper.js +++ b/src/intertyper.js @@ -796,6 +796,7 @@ function intertyper(data, parseFunctions, baseLineNum) { if (item.tokens[1].text in LLVM.LINKAGES || item.tokens[1].text in LLVM.PARAM_ATTR || item.tokens[1].text in LLVM.VISIBILITIES) { item.tokens.splice(1, 1); } + var params = parseParamTokens(item.tokens[3].item.tokens); return [{ intertype: 'functionStub', diff --git a/src/jsifier.js b/src/jsifier.js index c19eda3a..1fd4a790 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -207,6 +207,24 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { } var constant = null; if (item.external) { + // Import external global variables from the library if available. + if (LibraryManager.library[item.ident.slice(1)] && + LibraryManager.library[item.ident.slice(1)].length && + !BUILD_AS_SHARED_LIB) { + var val = LibraryManager.library[item.ident.slice(1)]; + val = val.concat(zeros(generateStructTypes(item.type).length-1)); + val = JSON.stringify(val); + ret.push({ + intertype: 'GlobalVariablePostSet', + JS: item.ident + '=' + makePointer(val, null, 'ALLOC_STATIC', item.type) + ';' + }); + if (LibraryManager.library[item.ident.slice(1) + '__postset']) { + ret.push({ + intertype: 'GlobalVariablePostSet', + JS: LibraryManager.library[item.ident.slice(1) + '__postset'] + }); + } + } return ret; } else { function needsPostSet(value) { @@ -784,10 +802,11 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { var argsTypes = []; var varargs = []; var varargsTypes = []; + var useJSArgs = (ident.slice(1) + '__jsargs') in LibraryManager.library; params.forEach(function(param, i) { var val = finalizeParam(param); - if (!func || !func.hasVarArgs || i < func.numParams-1) { + if (!func || !func.hasVarArgs || i < func.numParams-1 || useJSArgs) { args.push(val); argsTypes.push(param.type); } else { @@ -801,7 +820,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { args = args.map(function(arg, i) { return indexizeFunctions(arg, argsTypes[i]) }); varargs = varargs.map(function(vararg, i) { return vararg === 0 ? 0 : indexizeFunctions(vararg, varargsTypes[i]) }); - if (func && func.hasVarArgs) { + if (func && func.hasVarArgs && !useJSArgs) { if (varargs.length === 0) { varargs = [0]; varargsTypes = ['i32']; diff --git a/src/library.js b/src/library.js index 62e4de55..156c1c25 100644 --- a/src/library.js +++ b/src/library.js @@ -255,7 +255,7 @@ LibraryManager.library = { xhr.open('GET', obj.url, false); // Some hints to the browser that we want binary data. - xhr.responseType = 'arraybuffer'; + if (typeof Uint8Array != 'undefined') xhr.responseType = 'arraybuffer'; if (xhr.overrideMimeType) { xhr.overrideMimeType('text/plain; charset=x-user-defined'); } @@ -316,7 +316,7 @@ LibraryManager.library = { // Command line. result = readline(); } - if (!result) return null; + if (!result) result = ''; input.cache = intArrayFromString(result + '\n', true); } return input.cache.shift(); @@ -2103,6 +2103,7 @@ LibraryManager.library = { }, __01open64_: 'open', __01lseek64_: 'lseek', + __01truncate64_: 'truncate', __01ftruncate64_: 'ftruncate', // TODO: Check if any other aliases are needed. @@ -3078,6 +3079,7 @@ LibraryManager.library = { vfscanf: 'fscanf', vsscanf: 'sscanf', __01fopen64_: 'fopen', + __01freopen64_: 'freopen', __01fseeko64_: 'fseek', __01ftello64_: 'ftell', __01tmpfile64_: 'tmpfile', @@ -3584,6 +3586,11 @@ LibraryManager.library = { return String_len(ptr); }, + // TODO: Implement when we have real unicode support. + mblen: function() { + return 1; + }, + strspn: function(pstr, pset) { var str = String_copy(pstr, true); var set = String_copy(pset); @@ -3608,6 +3615,14 @@ LibraryManager.library = { } while ({{{ makeGetValue('psrc', 'i-1', 'i8') }}} != 0); return pdest; }, + stpcpy: function(pdest, psrc) { + var i = 0; + do { + {{{ makeCopyValues('pdest+i', 'psrc+i', 1, 'i8') }}} + i ++; + } while ({{{ makeGetValue('psrc', 'i-1', 'i8') }}} != 0); + return pdest + i - 1; + }, strncpy: function(pdest, psrc, num) { var padding = false, curr; @@ -3978,25 +3993,73 @@ LibraryManager.library = { return 1; }, - // Exceptions - minimal support, only (...) for now (no actual exception objects can be caught) + // Exceptions __cxa_allocate_exception: function(size) { - return _malloc(size); // warning: leaked + return _malloc(size); + }, + __cxa_free_exception: function(ptr) { + return _free(ptr); }, - __cxa_throw: function(ptr, data, dunno) { + __cxa_throw__deps: ['llvm_eh_exception'], + __cxa_throw: function(ptr, type, destructor) { #if EXCEPTION_DEBUG - print('Compiled code throwing an exception, ' + [ptr,data,dunno] + ', at ' + new Error().stack); + print('Compiled code throwing an exception, ' + [ptr,type,destructor] + ', at ' + new Error().stack); #endif + {{{ makeSetValue('_llvm_eh_exception.buf', '0', 'ptr', 'void*') }}} + {{{ makeSetValue('_llvm_eh_exception.buf', '4', 'type', 'void*') }}} + {{{ makeSetValue('_llvm_eh_exception.buf', '8', 'destructor', 'void*') }}} throw ptr; }, + __cxa_rethrow__deps: ['llvm_eh_exception', '__cxa_end_catch'], + __cxa_rethrow: function() { + ___cxa_end_catch.rethrown = true; + throw {{{ makeGetValue('_llvm_eh_exception.buf', '0', 'void*') }}}; + }, + llvm_eh_exception__postset: '_llvm_eh_exception.buf = allocate(12, "void*", ALLOC_STATIC);', llvm_eh_exception: function() { - return 'code-generated exception: ' + (new Error().stack); + return {{{ makeGetValue('_llvm_eh_exception.buf', '0', 'void*') }}}; }, - llvm_eh_selector: function(exception, personality, num) { + llvm_eh_selector__jsargs: true, + llvm_eh_selector: function(unused_exception_value, personality/*, varargs*/) { + var type = {{{ makeGetValue('_llvm_eh_exception.buf', '4', 'void*') }}} + for (var i = 2; i < arguments.length; i++) { + if (arguments[i] == type) return type; + } return 0; }, + llvm_eh_typeid_for: function(type) { + return type; + }, + _Unwind_Resume_or_Rethrow: function(ptr) { + throw ptr; + }, __cxa_begin_catch: function(ptr) { + return ptr; }, - __cxa_end_catch: function(ptr) { + __cxa_end_catch__deps: ['llvm_eh_exception', '__cxa_free_exception'], + __cxa_end_catch: function() { + if (___cxa_end_catch.rethrown) { + ___cxa_end_catch.rethrown = false; + return; + } + // Clear state flag. + __THREW__ = false; + // Free ptr if it isn't null. + if ({{{ makeGetValue('_llvm_eh_exception.buf', '0', 'void*') }}}) { + ___cxa_free_exception({{{ makeGetValue('_llvm_eh_exception.buf', '0', 'void*') }}}); + {{{ makeSetValue('_llvm_eh_exception.buf', '0', '0', 'void*') }}} + } + // Clear type. + {{{ makeSetValue('_llvm_eh_exception.buf', '4', '0', 'void*') }}} + // Call destructor if one is registered then clear it. + if ({{{ makeGetValue('_llvm_eh_exception.buf', '8', 'void*') }}}) { + FUNCTION_TABLE[{{{ makeGetValue('_llvm_eh_exception.buf', '8', 'void*') }}}](); + {{{ makeSetValue('_llvm_eh_exception.buf', '8', '0', 'i32') }}} + } + }, + __cxa_get_exception_ptr__deps: ['llvm_eh_exception'], + __cxa_get_exception_ptr: function(ptr) { + return ptr; }, __cxa_call_unexpected: function(exception) { @@ -4004,9 +4067,29 @@ LibraryManager.library = { throw exception; }, + terminate: '__cxa_call_unexpected', + __gxx_personality_v0: function() { }, + // RTTI hacks for exception handling, defining type_infos for common types. + // The values are dummies. We simply use the addresses of these statically + // allocated variables as unique identifiers. + // type_info for int. + _ZTIi: [0], + // type_info for long. + _ZTIl: [0], + // type_info for long long. + _ZTIx: [0], + // type_info for float. + _ZTIf: [0], + // type_info for double. + _ZTId: [0], + // type_info for char. + _ZTIc: [0], + // type_info for void. + _ZTIv: [0], + llvm_umul_with_overflow_i32: function(x, y) { return { f0: x*y, @@ -4665,6 +4748,28 @@ LibraryManager.library = { // sys/time.h // ========================================================================== + __timespec_struct_layout: Runtime.generateStructInfo(null, '%struct.timespec'), + // TODO: Implement these for real. + clock_gettime__deps: ['__timespec_struct_layout'], + clock_gettime: function(clk_id, tp) { + // int clock_gettime(clockid_t clk_id, struct timespec *tp); + {{{ makeSetValue('tp', '___timespec_struct_layout.tv_sec', '0', 'i32') }}} + {{{ makeSetValue('tp', '___timespec_struct_layout.tv_nsec', '0', 'i32') }}} + return 0; + }, + clock_settime: function(clk_id, tp) { + // int clock_settime(clockid_t clk_id, const struct timespec *tp); + // Nothing. + return 0; + }, + clock_getres__deps: ['__timespec_struct_layout'], + clock_getres: function(clk_id, res) { + // int clock_getres(clockid_t clk_id, struct timespec *res); + {{{ makeSetValue('res', '___timespec_struct_layout.tv_sec', '1', 'i32') }}} + {{{ makeSetValue('res', '___timespec_struct_layout.tv_nsec', '0', 'i32') }}} + return 0; + }, + // TODO: Implement remaining functions. // http://pubs.opengroup.org/onlinepubs/000095399/basedefs/sys/time.h.html gettimeofday: function(ptr) { @@ -4735,6 +4840,23 @@ LibraryManager.library = { // TODO return 0; }, + sigemptyset: function(set) { + // int sigemptyset(sigset_t *set); + // TODO: Implement for real; don't hardcode offsets. + {{{ makeSetValue('set', '0', '0', 'i32') }}} + {{{ makeSetValue('set', '4', '0', 'i32') }}} + {{{ makeSetValue('set', '8', '0', 'i32') }}} + {{{ makeSetValue('set', '12', '0', 'i32') }}} + return 0; + }, + sigfillset: 'sigemptyset', + sigdelset: 'sigemptyset', + sigaction: function(set) { + // int sigemptyset(sigset_t *set); + // TODO: Implement for real. + return 0; + }, + sigprocmask: 'sigaction', __libc_current_sigrtmin: function() { return 0; }, @@ -5145,6 +5267,24 @@ LibraryManager.library = { __errno_location: function() { return ___setErrNo.ret; }, + // ========================================================================== + // sys/resource.h + // ========================================================================== + + // TODO: Implement for real. + __rlimit_struct_layout: Runtime.generateStructInfo(null, '%struct.rlimit'), + getrlimit__deps: ['__rlimit_struct_layout'], + getrlimit: function(resource, rlp) { + // int getrlimit(int resource, struct rlimit *rlp); + {{{ makeSetValue('rlp', '___rlimit_struct_layout.rlim_cur', '-1', 'i32') }}} // RLIM_INFINITY + {{{ makeSetValue('rlp', '___rlimit_struct_layout.rlim_max', '-1', 'i32') }}} // RLIM_INFINITY + return 0; + }, + setrlimit: function(resource, rlp) { + // int setrlimit(int resource, const struct rlimit *rlp) + return 0; + }, + __01getrlimit64_: 'getrlimit', // ========================================================================== // pthread.h (stubs for mutexes only - no thread support yet!) diff --git a/src/parseTools.js b/src/parseTools.js index a3347139..955353b2 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -1050,7 +1050,7 @@ function getGetElementPtrIndexes(item) { } curr = 0; } - type = typeData ? typeData.fields[curr] : ''; + type = typeData && typeData.fields[curr] ? typeData.fields[curr] : ''; }); var ret = indexes[0]; for (var i = 1; i < indexes.length; i++) { |