diff options
-rw-r--r-- | src/analyzer.js | 36 | ||||
-rw-r--r-- | src/library.js | 9 | ||||
-rw-r--r-- | src/runtime.js | 44 | ||||
-rw-r--r-- | tests/runner.py | 6 |
4 files changed, 61 insertions, 34 deletions
diff --git a/src/analyzer.js b/src/analyzer.js index 96f2d509..91b1b884 100644 --- a/src/analyzer.js +++ b/src/analyzer.js @@ -187,39 +187,9 @@ function analyzer(data) { more = true; return; } - // Calculate aligned size, just like C structs should be. TODO: Consider - // requesting that compilation be done with #pragma pack(push) /n #pragma pack(1), - // which would remove much of the complexity here. - type.flatSize = 0; - var diffs = []; - var prev = -1, maxSize = -1; - type.flatIndexes = type.fields.map(function(field) { - var size; - if (isNumberType(field) || isPointerType(field)) { - size = getNativeFieldSize(field, true); // pack char; char; in structs, also char[X]s. - maxSize = Math.max(maxSize, size); - } else if (isStructType(field)) { - size = item.types[field].flatSize; - maxSize = Math.max(maxSize, QUANTUM_SIZE); - } else { - dprint('Unclear type in struct: ' + field + ', in ' + type.name_); - assert(0); - } - var curr = Runtime.alignMemory(type.flatSize, Math.min(QUANTUM_SIZE, size)); // if necessary, place this on aligned memory - type.flatSize = curr + size; - if (prev >= 0) { - diffs.push(curr-prev); - } - prev = curr; - return curr; - }); - type.flatSize = Runtime.alignMemory(type.flatSize, maxSize); - if (diffs.length == 0) { - type.flatFactor = type.flatSize; - } else if (dedup(diffs).length == 1) { - type.flatFactor = diffs[0]; - } - type.needsFlattening = (this.flatFactor != 1); + + Runtime.calculateStructAlignment(type, item.types); + dprint('types', 'type: ' + type.name_ + ' : ' + JSON.stringify(type.fields)); dprint('types', ' has final size of ' + type.flatSize + ', flatting: ' + type.needsFlattening + ' ? ' + (type.flatFactor ? type.flatFactor : JSON.stringify(type.flatIndexes))); }); diff --git a/src/library.js b/src/library.js index 210b79e1..c064b928 100644 --- a/src/library.js +++ b/src/library.js @@ -250,6 +250,15 @@ var Library = { } return ret; }, + + gettimeofday: function(ptr) { + // %struct.timeval = type { i32, i32 } + var indexes = Runtime.calculateStructAlignment({ fields: ['i32', 'i32'] }); + var now = Date.now(); + IHEAP[ptr + indexes[0]] = Math.floor(now/1000); // seconds + IHEAP[ptr + indexes[1]] = Math.floor((now-1000*Math.floor(now/1000))*1000); // microseconds + return 0; + }, }; load('library_sdl.js'); diff --git a/src/runtime.js b/src/runtime.js index 8d2df889..3bc78f62 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -76,6 +76,50 @@ Runtime = { FUNCTION_TABLE[key] = func; return key; }, + + // TODO: cleanup + isNumberType: isNumberType, + INT_TYPES: INT_TYPES, + getNativeFieldSize: getNativeFieldSize, + dedup: dedup, + + // Calculate aligned size, just like C structs should be. TODO: Consider + // requesting that compilation be done with #pragma pack(push) /n #pragma pack(1), + // which would remove much of the complexity here. + calculateStructAlignment: function calculateStructAlignment(type, otherTypes) { + type.flatSize = 0; + var diffs = []; + var prev = -1, maxSize = -1; + type.flatIndexes = type.fields.map(function(field) { + var size; + if (isNumberType(field) || isPointerType(field)) { + size = getNativeFieldSize(field, true); // pack char; char; in structs, also char[X]s. + maxSize = Math.max(maxSize, size); + } else if (isStructType(field)) { + size = otherTypes[field].flatSize; + maxSize = Math.max(maxSize, QUANTUM_SIZE); + } else { + dprint('Unclear type in struct: ' + field + ', in ' + type.name_); + assert(0); + } + var curr = Runtime.alignMemory(type.flatSize, Math.min(QUANTUM_SIZE, size)); // if necessary, place this on aligned memory + type.flatSize = curr + size; + if (prev >= 0) { + diffs.push(curr-prev); + } + prev = curr; + return curr; + }); + type.flatSize = Runtime.alignMemory(type.flatSize, maxSize); + if (diffs.length == 0) { + type.flatFactor = type.flatSize; + } else if (dedup(diffs).length == 1) { + type.flatFactor = diffs[0]; + } + type.needsFlattening = (this.flatFactor != 1); + return type.flatIndexes; + } + }; function getRuntime() { diff --git a/tests/runner.py b/tests/runner.py index 7356ad34..54836ee7 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -737,10 +737,11 @@ if 'benchmark' not in sys.argv: ''' self.do_test(src, '*cheez: 0+24*\nQ85*') - def test_atexit(self): + def test_stdlibs(self): src = ''' #include <stdio.h> #include <stdlib.h> + #include <sys/time.h> void clean() { @@ -748,6 +749,9 @@ if 'benchmark' not in sys.argv: } int main() { + timeval t; + gettimeofday(&t, NULL); + printf("*%d,%d\\n", int(t.tv_sec), int(t.tv_usec)); atexit(clean); return 0; } |