aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralon@honor <none@none>2010-09-11 11:01:37 -0700
committeralon@honor <none@none>2010-09-11 11:01:37 -0700
commite4f6c3cba22f6ad5ce7400325b58b21214723444 (patch)
tree0c7184186f8fc763e734cf9586e518be2f66114a
parent5f3a9ffd0e69c2d00e90261c69d9cd2ec8bfa2f9 (diff)
better handling of NULL in printf, and varargs
-rw-r--r--src/parser.js23
-rw-r--r--src/preamble.js4
-rw-r--r--tests/runner.py18
3 files changed, 26 insertions, 19 deletions
diff --git a/src/parser.js b/src/parser.js
index c91d83da..94be1dc6 100644
--- a/src/parser.js
+++ b/src/parser.js
@@ -1933,7 +1933,7 @@ function JSify(data) {
return makePointer(JSON.stringify(makeEmptyStruct(type)));
} else if (value.text[0] == '"') {
value.text = value.text.substr(1, value.text.length-2);
- return makePointer(JSON.stringify(parseLLVMString(value.text)));
+ return makePointer(JSON.stringify(parseLLVMString(value.text)) + ' /* ' + value.text + '*/');
} else {
// Gets an array of constant items, separated by ',' tokens
function handleSegments(tokens) {
@@ -2062,8 +2062,6 @@ function JSify(data) {
func.splitItems -= lines.length;
if (func.splitItems === 0) {
- postJSOptimize(func);
-
// Final recombination
//print('zz params::::: ' + JSON.stringify(func.params));
//print('zz params::::: ' + JSON.stringify(parseParamTokens(func.params.item[0].tokens)));
@@ -2075,11 +2073,15 @@ function JSify(data) {
return null;
}
return toNiceIdent(param.ident);
- }).filter(function(param) { return param != null }).join(', ');
+ }).filter(function(param) { return param != null });;
- func.JS = '\nfunction ' + func.ident + '(' + params + ') {\n';
+ func.JS = '\nfunction ' + func.ident + '(' + params.join(', ') + ') {\n';
if (LABEL_DEBUG) func.JS += " print(INDENT + ' Entering: " + func.ident + "'); INDENT += ' ';\n";
+ if (hasVarArgs) {
+ func.JS += ' __numArgs__ = ' + params.length + ';\n';
+ }
+
// Walk function blocks and generate JS
function walkBlock(block, indent) {
if (!block) return '';
@@ -2136,11 +2138,6 @@ function JSify(data) {
return ret;
},
});
- function postJSOptimize(func) {
- // Some optimizations are easier to apply after JS-ing the code - for example, a lot
- // of stuff can end up as x = y; for example, bitcasts, or nativized, etc. If we
- // we to optimize pre-JS, we would need to be aware of all of that.
- }
function getVarData(funcData, ident) {
if (funcData.variables[ident]) {
@@ -2448,14 +2445,12 @@ function JSify(data) {
return ident;
});
function makeFunctionCall(ident, params) {
- //print('// zz makeFC: ' + ident + ' : ' + dump(params));
-
// Special cases
if (ident == '_llvm_va_start') {
if (SAFE_HEAP) {
- return 'SAFE_HEAP_STORE(' + params[0].ident + ', Pointer_make(Array.prototype.slice.call(arguments, 1).concat([0]), 0))';
+ return 'SAFE_HEAP_STORE(' + params[0].ident + ', Pointer_make(Array.prototype.slice.call(arguments, __numArgs__).concat([0]), 0))';
} else {
- return 'HEAP[' + params[0].ident + '] = Pointer_make(Array.prototype.slice.call(arguments, 1).concat([0]), 0)'; // XXX 1
+ return 'HEAP[' + params[0].ident + '] = Pointer_make(Array.prototype.slice.call(arguments, __numArgs__).concat([0]), 0)';
}
} else if (ident == '_llvm_va_end') {
return ';'
diff --git a/src/preamble.js b/src/preamble.js
index 1ed1b6eb..4d473ee3 100644
--- a/src/preamble.js
+++ b/src/preamble.js
@@ -8,8 +8,8 @@ var __THREW__ = false; // Used in checking for thrown exceptions.
var __ATEXIT__ = [];
-var HEAP = [0];
-var HEAPTOP = 1; // Leave 0 as an invalid address, 'NULL'
+var HEAP = intArrayFromString('(null)'); // So printing %s of NULL gives '(null)'
+var HEAPTOP = HEAP.length+1; // Leave 0 as an invalid address, 'NULL'
#if SAFE_HEAP
// Semi-manual memory corruption debugging
diff --git a/tests/runner.py b/tests/runner.py
index 4bb20c0a..b7580370 100644
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -234,11 +234,11 @@ class T(unittest.TestCase):
printf("%d", atoi(argv[3])+2);
const char *foolingthecompiler = "\\rabcd";
printf("%d", strlen(foolingthecompiler)); // Tests parsing /0D in llvm - should not be a 0 (end string) then a D!
- printf("%s*", NULL); // Should print nothing, not the string at address 0, which is a real address for us!
+ printf("%s*", NULL); // Should print '(null)', not the string at address 0, which is a real address for us!
return 0;
}
'''
- self.do_test(src, '*4*wowie*too*76*5**', ['wowie', 'too', '74'], lambda x: x.replace('\n', '*'))
+ self.do_test(src, '*4*wowie*too*76*5*(null)*', ['wowie', 'too', '74'], lambda x: x.replace('\n', '*'))
def test_funcs(self):
src = '''
@@ -613,12 +613,24 @@ class T(unittest.TestCase):
va_end(v);
}
+ void vary2(char color, const char *s, ...)
+ {
+ va_list v;
+ va_start(v, s);
+ char d[21];
+ d[0] = color;
+ vsnprintf(d+1, 20, s, v);
+ puts(d);
+ va_end(v);
+ }
+
int main() {
vary("*cheez: %d+%d*", 10, 24);
+ vary2('Q', "%d*", 85);
return 0;
}
'''
- self.do_test(src, '*cheez: 10+24*')
+ self.do_test(src, '*cheez: 10+24*\nQ85*')
def test_atexit(self):
src = '''