aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <azakai@mozilla.com>2011-02-20 19:03:14 -0800
committerAlon Zakai <azakai@mozilla.com>2011-02-20 19:03:14 -0800
commitacbb1e6a0b49facc66e14891556e0859f9609868 (patch)
tree3eec5250316f278054ff7c0aa901ca22cb55cb4b /src
parente82689262a0ab476d5a5bf11476fbb4532b4b638 (diff)
generate strict mode javascript
Diffstat (limited to 'src')
-rw-r--r--src/analyzer.js8
-rw-r--r--src/intertyper.js4
-rw-r--r--src/jsifier.js20
-rw-r--r--src/library.js54
-rw-r--r--src/preamble.js14
-rw-r--r--src/runtime.js2
-rw-r--r--src/shell.js1
7 files changed, 65 insertions, 38 deletions
diff --git a/src/analyzer.js b/src/analyzer.js
index 54001af2..b3d311d1 100644
--- a/src/analyzer.js
+++ b/src/analyzer.js
@@ -927,9 +927,11 @@ function analyzer(data, givenTypes) {
var ret = substrate.solve();
// Add additional necessary items
- ret.functionStubs.push({
- intertype: 'functionStub',
- ident: '@memset' // we need memset as a fundamental runtime tool, so always include that from the library
+ ['memset', 'malloc', 'free'].forEach(function(ident) {
+ ret.functionStubs.push({
+ intertype: 'functionStub',
+ ident: '@' + ident
+ });
});
return ret;
}
diff --git a/src/intertyper.js b/src/intertyper.js
index a8254b30..4316425e 100644
--- a/src/intertyper.js
+++ b/src/intertyper.js
@@ -107,7 +107,7 @@ function intertyper(data, parseFunctions, baseLineNum) {
if (LLVM_STYLE === null) {
// new = clang on 2.8, old = llvm-gcc anywhere or clang on 2.7
LLVM_STYLE = (data.indexOf('<label>') == -1 && data.indexOf('entry:') != -1) ? 'old' : 'new';
- dprint('LLVM_STYLE: ' + LLVM_STYLE);
+ //dprint('LLVM_STYLE: ' + LLVM_STYLE);
}
// If the source contains debug info as LLVM metadata, process that out (and save the debugging info for later)
@@ -482,7 +482,7 @@ function intertyper(data, parseFunctions, baseLineNum) {
var ret = {
intertype: 'function',
ident: item.tokens[1].text,
- returnType: item.tokens[0],
+ returnType: item.tokens[0].text,
params: parseParamTokens(item.tokens[2].item.tokens),
lineNum: item.lineNum
};
diff --git a/src/jsifier.js b/src/jsifier.js
index 73774373..f4046423 100644
--- a/src/jsifier.js
+++ b/src/jsifier.js
@@ -311,6 +311,8 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions, givenGlobalVaria
}
});
+ var moduleFunctions = set(data.unparsedFunctions.map(function(func) { return func.ident }));
+
var addedLibraryItems = {};
// functionStub
@@ -322,6 +324,10 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions, givenGlobalVaria
function addFromLibrary(ident) {
var me = arguments.callee;
if (ident in addedLibraryItems) return '';
+ // Don't replace implemented functions with library ones (which can happen when we add dependencies).
+ // Note: We don't return the dependencies here. Be careful not to end up where this matters
+ if (('_' + ident) in moduleFunctions) return '';
+
addedLibraryItems[ident] = true;
var snippet = Library[ident];
if (typeof snippet === 'string') {
@@ -339,6 +345,9 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions, givenGlobalVaria
snippet = JSON.stringify(snippet).replace(/}$/, ', ' + funcs.join(', ') + ' }');
} else if (typeof snippet === 'function') {
snippet = snippet.toString();
+ if (/function ?\(/.exec(snippet)) { // name the function, if not already named
+ snippet = snippet.replace('function', 'function _' + ident);
+ }
}
var postsetId = ident + '__postset';
@@ -352,7 +361,7 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions, givenGlobalVaria
}
var deps = Library[ident + '__deps'];
- return '_' + ident + ' = ' + snippet + (deps ? '\n' + deps.map(addFromLibrary).join('\n') : '');
+ return 'var _' + ident + ' = ' + snippet + (deps ? '\n' + deps.map(addFromLibrary).join('\n') : '');
}
item.JS = addFromLibrary(shortident);
} else {
@@ -509,6 +518,11 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions, givenGlobalVaria
func.JS += walkBlock(func.block, ' ');
// Finalize function
if (LABEL_DEBUG) func.JS += " INDENT = INDENT.substr(0, INDENT.length-2);\n";
+ // Add an unneeded return, needed for strict mode to not throw warnings in some cases.
+ // If we are not relooping, then switches make it unimportant to have this (and, we lack hasReturn anyhow)
+ if (RELOOP && func.lines.length > 0 && func.labels.filter(function(label) { return label.hasReturn }).length > 0) {
+ func.JS += ' return' + (func.returnType !== 'void' ? ' null' : '') + ';\n';
+ }
func.JS += '}\n';
func.JS += func.ident + '.__index__ = Runtime.getFunctionIndex(' + func.ident + ', "' + func.ident + '");\n';
return func;
@@ -716,7 +730,7 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions, givenGlobalVaria
+ '} catch(e) { '
+ 'if (ABORT) throw e; __THREW__ = true; '
+ (EXCEPTION_DEBUG ? 'print("Exception: " + e + " : " + e.stack + ", currently at: " + (new Error().stack)); ' : '')
- + '} })(); if (!__THREW__) { ' + makeBranch(item.toLabel, item.currLabelId)
+ + 'return null } })(); if (!__THREW__) { ' + makeBranch(item.toLabel, item.currLabelId)
+ ' } else { ' + makeBranch(item.unwindLabel, item.currLabelId) + ' }';
return ret;
});
@@ -1028,7 +1042,7 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions, givenGlobalVaria
// Special cases
if (ident == '_llvm_va_start') {
// varargs - we received a pointer to the varargs as a final 'extra' parameter
- var data = 'arguments[arguments.callee.length]';
+ var data = 'arguments[' + Framework.currItem.funcData.ident + '.length]';
if (SAFE_HEAP) {
return 'SAFE_HEAP_STORE(' + params[0].ident + ', ' + data + ', null)';
} else {
diff --git a/src/library.js b/src/library.js
index e41ca866..7775c792 100644
--- a/src/library.js
+++ b/src/library.js
@@ -72,7 +72,6 @@ var Library = {
if (textIndex < 0) {
cStyle = true;
textIndex = -textIndex;
- slab = null;
argIndex = arguments[1];
} else {
var _arguments = arguments;
@@ -276,12 +275,18 @@ var Library = {
SEEK_CUR: 1, /* Current position. */
SEEK_END: 2, /* End of file. */
init: function() {
- _stdin = Pointer_make([0], null, ALLOC_STATIC);
- IHEAP[_stdin] = this.prepare('<<stdin>>');
- _stdout = Pointer_make([0], null, ALLOC_STATIC);
- IHEAP[_stdout] = this.prepare('<<stdout>>', null, true);
- _stderr = Pointer_make([0], null, ALLOC_STATIC);
- IHEAP[_stderr] = this.prepare('<<stderr>>', null, true);
+ try {
+ _stdin = Pointer_make([0], null, ALLOC_STATIC);
+ IHEAP[_stdin] = this.prepare('<<stdin>>');
+ } catch(e){} // stdin/out/err may not exist if not needed
+ try {
+ _stdout = Pointer_make([0], null, ALLOC_STATIC);
+ IHEAP[_stdout] = this.prepare('<<stdout>>', null, true);
+ } catch(e){}
+ try {
+ _stderr = Pointer_make([0], null, ALLOC_STATIC);
+ IHEAP[_stderr] = this.prepare('<<stderr>>', null, true);
+ } catch(e){}
},
prepare: function(filename, data, print_) {
var stream = this.counter++;
@@ -328,7 +333,7 @@ var Library = {
} else if (mode.indexOf('w') >= 0) {
return _STDIO.prepare(filename);
} else {
- assert(false, 'fopen with odd params: ' + mode);
+ return assert(false, 'fopen with odd params: ' + mode);
}
},
__01fopen64_: 'fopen',
@@ -413,7 +418,7 @@ var Library = {
} else if (flags === 1) { // WRONLY
return _STDIO.prepare(filename);
} else {
- assert(false, 'open with odd params: ' + [flags, mode]);
+ return assert(false, 'open with odd params: ' + [flags, mode]);
}
},
@@ -448,6 +453,23 @@ var Library = {
// stdlib.h
+ malloc: Runtime.staticAlloc,
+ _Znwj: 'malloc',
+ _Znaj: 'malloc',
+ _Znam: 'malloc',
+ _Znwm: 'malloc',
+
+ free: function(){},
+ _ZdlPv: 'free',
+ _ZdaPv: 'free',
+
+ calloc__deps: ['malloc'],
+ calloc: function(n, s) {
+ var ret = _malloc(n*s);
+ _memset(ret, 0, n*s);
+ return ret;
+ },
+
abs: 'Math.abs',
atoi: function(s) {
@@ -786,7 +808,7 @@ var Library = {
// ctype.h Linux specifics
__ctype_b_loc: function() { // http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/baselib---ctype-b-loc.html
- var me = arguments.callee;
+ var me = ___ctype_b_loc;
if (!me.ret) {
var values = [
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
@@ -919,8 +941,8 @@ var Library = {
_ZNSt8ios_base4InitC1Ev: function() {
// need valid 'file descriptors'
- __ZSt4cout = 1;
- __ZSt4cerr = 2;
+ //__ZSt4cout = 1;
+ //__ZSt4cerr = 2;
},
_ZNSt8ios_base4InitD1Ev: '_ZNSt8ios_base4InitC1Ev',
_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_: 0, // endl
@@ -1030,7 +1052,7 @@ var Library = {
// unfreeable allocations - the HEAP is ours, from STATICTOP up.
// TODO: We could in theory slice off the top of the HEAP when
// sbrk gets a negative increment in |bytes|...
- var self = arguments.callee;
+ var self = _sbrk;
if (!self.STATICTOP) {
STATICTOP = alignMemoryPage(STATICTOP);
self.STATICTOP = STATICTOP;
@@ -1114,7 +1136,7 @@ var Library = {
localeconv: function() {
// %struct.timeval = type { char* decimal point, other stuff... }
// var indexes = Runtime.calculateStructAlignment({ fields: ['i32', 'i32'] });
- var me = arguments.callee;
+ var me = _localeconv;
if (!me.ret) {
me.ret = Pointer_make([Pointer_make(intArrayFromString('.'), null)], null); // just decimal point, for now
}
@@ -1124,7 +1146,7 @@ var Library = {
// langinfo.h
nl_langinfo: function(item) {
- var me = arguments.callee;
+ var me = _nl_langinfo;
if (!me.ret) {
me.ret = Pointer_make(intArrayFromString("eh?"), null);
}
@@ -1134,7 +1156,7 @@ var Library = {
// errno.h
__errno_location: function() {
- var me = arguments.callee;
+ var me = ___errno_location;
if (!me.ret) {
me.ret = Pointer_make([0], 0, ALLOC_STATIC);
}
diff --git a/src/preamble.js b/src/preamble.js
index 505217ab..92997d20 100644
--- a/src/preamble.js
+++ b/src/preamble.js
@@ -238,22 +238,10 @@ var HEAP, IHEAP, FHEAP;
var STACK_ROOT, STACKTOP, STACK_MAX;
var STATICTOP;
-// Mangled |new| and |free| (various manglings, for int, long params; new and new[], etc.
-var _malloc, _calloc, _free, __Znwj, __Znaj, __Znam, __Znwm, __ZdlPv, __ZdaPv;
-
var HAS_TYPED_ARRAYS = false;
var TOTAL_MEMORY = 50*1024*1024;
function __initializeRuntime__() {
- // If we don't have malloc/free implemented, use a simple implementation.
- Module['_malloc'] = _malloc = __Znwj = __Znaj = __Znam = __Znwm = Module['_malloc'] ? Module['_malloc'] : Runtime.staticAlloc;
- Module['_calloc'] = _calloc = Module['_calloc'] ? Module['_calloc'] : function(n, s) {
- var ret = _malloc(n*s);
- _memset(ret, 0, n*s);
- return ret;
- };
- Module['_free'] = _free = __ZdlPv = __ZdaPv = Module['_free'] ? Module['_free'] : function() { };
-
#if USE_TYPED_ARRAYS
// TODO: Remove one of the 3 heaps!
HAS_TYPED_ARRAYS = false;
@@ -339,7 +327,7 @@ function String_copy(ptr, addZero) {
// Tools
-PRINTBUFFER = '';
+var PRINTBUFFER = '';
function __print__(text) {
if (text === null) {
// Flush
diff --git a/src/runtime.js b/src/runtime.js
index 1a617ae5..9cdba894 100644
--- a/src/runtime.js
+++ b/src/runtime.js
@@ -143,7 +143,7 @@ Runtime = {
};
function getRuntime() {
- var ret = 'Runtime = {\n';
+ var ret = 'var Runtime = {\n';
for (i in Runtime) {
var item = Runtime[i];
ret += ' ' + i + ': ';
diff --git a/src/shell.js b/src/shell.js
index a1f4a176..3b762e18 100644
--- a/src/shell.js
+++ b/src/shell.js
@@ -1,3 +1,4 @@
+"use strict";
// Capture the output of this into a variable, if you want
//(function(Module, args) {