diff options
author | alon@honor <none@none> | 2010-09-30 21:02:30 -0700 |
---|---|---|
committer | alon@honor <none@none> | 2010-09-30 21:02:30 -0700 |
commit | cbea94c15230599f0f909329783e08f6713e766c (patch) | |
tree | c4bd49e9b6e29255143b76d18e7739103a3e6e6e | |
parent | 13e243c8cb9e4d91bc74d927fc2eabd4938c97a8 (diff) |
optional stricter support for unsigned values +test
-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 | ||||
-rw-r--r-- | tests/runner.py | 13 |
5 files changed, 47 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 diff --git a/tests/runner.py b/tests/runner.py index 08560fd8..8b71b223 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -132,6 +132,19 @@ class T(unittest.TestCase): ''' self.do_test(src, '*5,23,10,19,121,1,37,1,0*') + def test_unsigned(self): + src = ''' + #include <stdio.h> + int main() + { + int varey = 100; + unsigned int MAXEY = -1, MAXEY2 = -77; + printf("*%u,%d,%u*\\n", MAXEY, varey >= MAXEY, MAXEY2); // 100 >= -1? not in unsigned! + return 0; + } + ''' + self.do_test(src, '*4294967295,0,4294967219*') + def test_floatvars(self): src = ''' #include <stdio.h> |