diff options
-rw-r--r-- | src/analyzer.js | 2 | ||||
-rw-r--r-- | src/jsifier.js | 30 | ||||
-rw-r--r-- | src/library.js | 15 | ||||
-rw-r--r-- | src/utility.js | 7 | ||||
-rw-r--r-- | tests/runner.py | 25 |
5 files changed, 56 insertions, 23 deletions
diff --git a/src/analyzer.js b/src/analyzer.js index 0d0915b0..c57b3be9 100644 --- a/src/analyzer.js +++ b/src/analyzer.js @@ -113,7 +113,7 @@ function analyzer(data, givenTypes) { if (!data.types[zerod]) { data.types[zerod] = { name_: zerod, - fields: [subType], + fields: [subType, subType], // Two, so we get the flatFactor right. We care about the flatFactor, not the size here lineNum: '?', }; } diff --git a/src/jsifier.js b/src/jsifier.js index e465647c..b504cb9d 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -125,28 +125,22 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions) { } function alignStruct(values, type) { - // XXX Need to add padding at the end of structures, using alignMemory()? - dprint('types', 'alignStruct: ' + dump(type)); - var ret = []; var typeData = TYPES[type]; assertTrue(typeData); - var i = 0; + var ret = []; + var i = 0, soFar = 0; while (i < values.length) { - var currField = typeData.fields[i]; - var currValue = values[i]; - if (isStructType[currField]) { - var fieldTypeData = TYPES[currField]; - assertTrue(fieldTypeData); - ret = ret.concat(alignStruct(values.slice(i, fieldTypeData.fields.length), currField)); - i += fieldTypeData.fields.length; - } else { - ret.push(currValue); - // pad to align, unless it's a structure and already aligned - if (typeof currValue !== 'object') { - ret = ret.concat(zeros(getNativeFieldSize(currField)-1)); - } - i += 1; + // Pad until the right place + var padded = typeData.flatFactor ? typeData.flatFactor*i : typeData.flatIndexes[i]; + while (soFar < padded) { + ret.push(0); + soFar++; } + // Add current value(s) + var currValue = values[i]; + ret.push(currValue); + i += 1; + soFar += typeof currValue === 'object' ? currValue.length : 1; } return ret; } diff --git a/src/library.js b/src/library.js index 1d64b914..fc5932d9 100644 --- a/src/library.js +++ b/src/library.js @@ -70,6 +70,14 @@ var Library = { } }, + fileno: function(file) { + return 1; // XXX + }, + + isatty: function(file) { + return 0; // XXX + }, + // stdlib.h abs: 'Math.abs', @@ -253,6 +261,10 @@ var Library = { return chr in { 32: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0 }; }, + iscntrl: function(chr) { + return (chr >= 0 && chr <= 0x1f) || chr === 0x7f; + }, + toupper: function(chr) { if (chr >= 'a'.charCodeAt(0) && chr <= 'z'.charCodeAt(0)) { return chr - 'a'.charCodeAt(0) + 'A'.charCodeAt(0); @@ -279,6 +291,9 @@ var Library = { return _malloc(size); // warning: leaked }, __cxa_throw: function(ptr, data, dunno) { +#if EXCEPTION_DEBUG + print('Compiled code throwing an exception, ' + [ptr,data,dunno] + ', at ' + new Error().stack); +#endif throw ptr; }, llvm_eh_exception: function() { diff --git a/src/utility.js b/src/utility.js index 391efb14..5020c911 100644 --- a/src/utility.js +++ b/src/utility.js @@ -8,7 +8,12 @@ function safeQuote(x) { function dump(item) { var CHUNK = 500; function lineify(text) { - return text.replace(/.{80}/g, '$&\n'); + var ret = ''; + while (text.length > 80) { + ret += '// ' + text.substr(0,80) + '\n'; + text = text.substr(80); + } + return ret + '// ' + text; } try { return lineify(JSON.stringify(item)); diff --git a/tests/runner.py b/tests/runner.py index 0620481e..aa22e06c 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -387,7 +387,6 @@ if 'benchmark' not in sys.argv: src = ''' #include <stdio.h> - char cache[256], *next = cache; int main() @@ -637,6 +636,26 @@ if 'benchmark' not in sys.argv: ''' self.do_test(src, '0:-1.00,-0.33 1:0.33,-1.00 2:-0.33,1.00 3:1.00,0.33') + def test_array2b(self): + src = ''' + #include <stdio.h> + + static const struct { + unsigned char left; + unsigned char right; + } prioritah[] = { + {6, 6}, {6, 6}, {7, 95}, {7, 7} + }; + + int main() { + printf("*%d,%d\\n", prioritah[1].left, prioritah[1].right); + printf("%d,%d*\\n", prioritah[2].left, prioritah[2].right); + return 0; + } + ''' + self.do_test(src, '*6,6\n7,95*') + + def test_constglobalstructs(self): src = ''' #include <stdio.h> @@ -1083,8 +1102,8 @@ if 'benchmark' not in sys.argv: def test_lua(self): self.do_ll_test(path_from_root(['tests', 'lua', 'lua.ll']), - 'hello lua world!\n\n\n17.00000000000\n\n\n1.00000000000\n\n\n2.00000000000\n\n\n3.00000000000\n\n\n4.00000000000', - args=['-e', '''print("hello lua world!");print(17);for x = 1,4 do print(x) end'''], + 'hello lua world!\n\n\n17.00000000000\n\n\n1.00000000000\n\n\n2.00000000000\n\n\n3.00000000000\n\n\n4.00000000000\n\n\n7.00000000000', + args=['-e', '''print("hello lua world!");print(17);for x = 1,4 do print(x) end;print(10-3)'''], f_opt_ll_file=path_from_root(['tests', 'lua', 'lua.Os.ll'])) ### Test cases in separate files |