diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/jsifier.js | 17 | ||||
-rw-r--r-- | src/parseTools.js | 6 | ||||
-rw-r--r-- | src/preamble.js | 11 | ||||
-rw-r--r-- | src/settings.js | 4 |
4 files changed, 34 insertions, 4 deletions
diff --git a/src/jsifier.js b/src/jsifier.js index 7df0ba86..9a5d7121 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -516,11 +516,26 @@ function JSify(data) { dprint('phi', dump(item)); return '__lastLabel__ == ' + getLabelId(item.label1) + ' ? ' + toNiceIdent(item.value1) + ' : ' + toNiceIdent(item.value2); }); + + function makeUnSign(value, type) { + if (type in INT_TYPES) { + return 'unSign(' + value + ', ' + type.substr(1) + ')'; + } else { + return value; + } + } + makeFuncLineZyme('mathop', function(item) { with(item) { dprint('mathop', 'mathop: ' + dump(item)); ident = parseNumerical(ident); ident2 = parseNumerical(ident2); - switch (item.op) { + if (GUARD_SIGNS) { + if (op[0] == 'u' || (variant && variant[0] == 'u')) { + ident = makeUnSign(ident, type.text); + ident2 = makeUnSign(ident2, type.text); + } + } + switch (op) { case 'add': return ident + ' + ' + ident2; case 'sub': return ident + ' - ' + ident2; case 'sdiv': case 'udiv': return 'Math.floor(' + ident + ' / ' + ident2 + ')'; diff --git a/src/parseTools.js b/src/parseTools.js index e7796b12..7cf0fa0b 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -53,9 +53,11 @@ function toNiceIdent(ident) { return ident.replace(/[" \.@%:<>,\*]/g, '_'); } +INT_TYPES = searchable('i1', 'i8', 'i16', 'i32', 'i64'); +FLOAT_TYPES = searchable('float', 'double'); + function isNumberType(type) { - var types = ['i1', 'i8', 'i16', 'i32', 'i64', 'float', 'double']; - return types.indexOf(type) != -1; + return type in INT_TYPES || type in FLOAT_TYPES; } function isStructPointerType(type) { diff --git a/src/preamble.js b/src/preamble.js index d61a8ce5..e1036b8e 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -156,7 +156,7 @@ function __formatString() { while (curr != 0) { curr = HEAP[textIndex]; next = HEAP[textIndex+1]; - if (curr == '%'.charCodeAt(0) && ['d', 'f', '.'].indexOf(String.fromCharCode(next)) != -1) { + if (curr == '%'.charCodeAt(0) && ['d', 'u', 'f', '.'].indexOf(String.fromCharCode(next)) != -1) { var argText = String(arguments[argIndex]); // Handle very very simply formatting, namely only %.Xf if (next == '.'.charCodeAt(0)) { @@ -169,6 +169,8 @@ function __formatString() { argText += '00000000000'; // padding argText = argText.substr(0, dotIndex+1+limit); textIndex += 2; + } else if (next == 'u'.charCodeAt(0)) { + argText = String(unSign(arguments[argIndex], 32)); } argText.split('').forEach(function(chr) { ret.push(chr.charCodeAt(0)); @@ -260,5 +262,12 @@ function intArrayFromString(stringy) { return ret; } +// Converts a value we have as signed, into an unsigned value. For +// example, -1 in int32 would be a very large number as unsigned. +function unSign(value, bits) { + if (value >= 0) return value; + return 2*Math.abs(1 << (bits-1)) + value; +} + // === Body === diff --git a/src/settings.js b/src/settings.js index f6282ed4..815832ed 100644 --- a/src/settings.js +++ b/src/settings.js @@ -13,6 +13,10 @@ QUANTUM_SIZE = 1; // This is the size of an individual field in a structure. 1 w // way to prevent problems with that is to set QUANTUM_SIZE to 8. // See the 'copyop' automatic test. +GUARD_SIGNS = 1; // Whether we make sure to convert unsigned values to signed values. + // Decreases performance with additional runtime checks. Might not be + // needed in some kinds of code. + // Code embetterments OPTIMIZE = 1; // Optimize llvm operations into js commands RELOOP = 0; // Recreate js native loops from llvm data XXX - disabled pending optimizing rewrite |