summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2015-10-30 10:54:13 -0700
committerAlon Zakai <alonzakai@gmail.com>2015-10-30 10:54:13 -0700
commitd93ca9e91e89e6ca9dc3c974e6dfd3329a3d4f17 (patch)
tree1546e1f7f870390f51675b90f0afe485c5d991f3
parent1f3825ea4fd717de015705651b9911a47751f13c (diff)
downloadbinaryen-d93ca9e91e89e6ca9dc3c974e6dfd3329a3d4f17.tar.gz
binaryen-d93ca9e91e89e6ca9dc3c974e6dfd3329a3d4f17.tar.bz2
binaryen-d93ca9e91e89e6ca9dc3c974e6dfd3329a3d4f17.zip
fix printing of large negatives
-rw-r--r--src/simple_ast.h21
-rw-r--r--src/wasm.h4
-rw-r--r--test/unit.asm.js2
-rw-r--r--test/unit.wast5
4 files changed, 21 insertions, 11 deletions
diff --git a/src/simple_ast.h b/src/simple_ast.h
index a04fbf712..de894b4bc 100644
--- a/src/simple_ast.h
+++ b/src/simple_ast.h
@@ -811,14 +811,14 @@ struct JSPrinter {
emit(node[1]->getCString());
}
- void printNum(Ref node) {
- double d = node[1]->getNumber();
+ static char* numToString(double d, bool finalize=true) {
bool neg = d < 0;
if (neg) d = -d;
// try to emit the fewest necessary characters
bool integer = fmod(d, 1) == 0;
#define BUFFERSIZE 1000
- static char storage_f[BUFFERSIZE], storage_e[BUFFERSIZE]; // f is normal, e is scientific for float, x for integer
+ static char full_storage_f[BUFFERSIZE], full_storage_e[BUFFERSIZE]; // f is normal, e is scientific for float, x for integer
+ static char *storage_f = full_storage_f + 1, *storage_e = full_storage_e + 1; // full has one more char, for a possible '-'
double err_f, err_e;
for (int e = 0; e <= 1; e++) {
char *buffer = e ? storage_e : storage_f;
@@ -919,12 +919,21 @@ struct JSPrinter {
//errv("..current attempt: %.18f => %s", d, buffer);
}
//fprintf(stderr, "options:\n%s\n%s\n (first? %d)\n", storage_e, storage_f, strlen(storage_e) < strlen(storage_f));
- if (neg) emit('-');
+ char *ret;
if (err_e == err_f) {
- emit(strlen(storage_e) < strlen(storage_f) ? storage_e : storage_f);
+ ret = strlen(storage_e) < strlen(storage_f) ? storage_e : storage_f;
} else {
- emit(err_e < err_f ? storage_e : storage_f);
+ ret = err_e < err_f ? storage_e : storage_f;
+ }
+ if (neg) {
+ ret--; // safe to go back one, there is one more char in full_*
+ *ret = '-';
}
+ return ret;
+ }
+
+ void printNum(Ref node) {
+ emit(numToString(node[1]->getNumber(), finalize));
}
void printString(Ref node) {
diff --git a/src/wasm.h b/src/wasm.h
index 5e8b7a212..f66f8bb8c 100644
--- a/src/wasm.h
+++ b/src/wasm.h
@@ -172,8 +172,8 @@ struct Literal {
case none: abort();
case BasicType::i32: o << i32; break;
case BasicType::i64: o << i64; break;
- case BasicType::f32: o << f32; break;
- case BasicType::f64: o << f64; break;
+ case BasicType::f32: o << JSPrinter::numToString(f32); break;
+ case BasicType::f64: o << JSPrinter::numToString(f64); break;
}
restoreNormalColor(o);
o << ')';
diff --git a/test/unit.asm.js b/test/unit.asm.js
index fd05746d6..c8d52f3e0 100644
--- a/test/unit.asm.js
+++ b/test/unit.asm.js
@@ -2,7 +2,7 @@ function () {
"use asm";
function big_negative() {
var temp = 0.0;
- temp = -2147483648;
+ temp = +-2147483648;
temp = -2147483648.0;
temp = -21474836480.0;
}
diff --git a/test/unit.wast b/test/unit.wast
index b5e054593..914cbf8bd 100644
--- a/test/unit.wast
+++ b/test/unit.wast
@@ -5,7 +5,9 @@
(local $temp f64)
(block
(set_local $temp
- (i32.const -2147483648)
+ (f64.convert_s/i32
+ (i32.const -2147483648)
+ )
)
(set_local $temp
(f64.const -2147483648)
@@ -16,4 +18,3 @@
)
)
)
-