From 3776ea28c87ecc0a31d9230384587b11b72cb1cd Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Sat, 11 Jan 2014 22:38:58 +0100 Subject: [PATCH] Simplify constant folding function. --- Makefile | 4 ++-- jsoptim.c | 66 ++++++++++++++++++++++++------------------------------- 2 files changed, 31 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index ca1b00b..86062de 100644 --- a/Makefile +++ b/Makefile @@ -18,8 +18,8 @@ build/libjs.a: $(OBJS) js: build/main.o build/libjs.a $(CC) -o $@ $^ -lm -tags: - ctags *.c *.h +tags: $(SRCS) $(HDRS) + ctags $^ clean: rm -f build/* js diff --git a/jsoptim.c b/jsoptim.c index 9587907..fd8a648 100644 --- a/jsoptim.c +++ b/jsoptim.c @@ -22,57 +22,50 @@ static inline unsigned int u32(double d) return i32(d); } -static int N(js_Ast *node, double *r, double x) +static int N(js_Ast *node, double x) { node->type = AST_NUMBER; node->number = x; node->a = node->b = node->c = node->d = NULL; - *r = x; return 1; } -static int foldnumber(js_Ast *node, double *r) +static int foldnumber(js_Ast *node) { - double x, y, z, w; + double x, y; int a, b; - a = node->a ? foldnumber(node->a, &x) : 0; - b = node->b ? foldnumber(node->b, &y) : 0; - if (node->c) foldnumber(node->c, &z); - if (node->d) foldnumber(node->d, &w); - - if (node->type == AST_NUMBER) { - *r = node->number; + if (node->type == AST_NUMBER) return 1; - } - /* not an expression */ - if (node->c || node->d) - return 0; + a = node->a ? foldnumber(node->a) : 0; + b = node->b ? foldnumber(node->b) : 0; + if (node->c) foldnumber(node->c); + if (node->d) foldnumber(node->d); - /* binary */ - if (a && b) { + if (a) { + x = node->a->number; switch (node->type) { - case EXP_MUL: return N(node, r, x * y); - case EXP_DIV: return N(node, r, x / y); - case EXP_MOD: return N(node, r, fmod(x, y)); - case EXP_ADD: return N(node, r, x + y); - case EXP_SUB: return N(node, r, x - y); - case EXP_SHL: return N(node, r, i32(x) << (u32(y) & 0x1F)); - case EXP_SHR: return N(node, r, i32(x) >> (u32(y) & 0x1F)); - case EXP_USHR: return N(node, r, u32(x) >> (u32(y) & 0x1F)); - case EXP_BITAND: return N(node, r, i32(x) & i32(y)); - case EXP_BITXOR: return N(node, r, i32(x) ^ i32(y)); - case EXP_BITOR: return N(node, r, i32(x) | i32(y)); + case EXP_NEG: return N(node, -x); + case EXP_POS: return N(node, x); + case EXP_BITNOT: return N(node, ~i32(x)); } - } - /* unary */ - else if (a) { - switch (node->type) { - case EXP_NEG: return N(node, r, -x); - case EXP_POS: return N(node, r, x); - case EXP_BITNOT: return N(node, r, ~i32(x)); + if (b) { + y = node->b->number; + switch (node->type) { + case EXP_MUL: return N(node, x * y); + case EXP_DIV: return N(node, x / y); + case EXP_MOD: return N(node, fmod(x, y)); + case EXP_ADD: return N(node, x + y); + case EXP_SUB: return N(node, x - y); + case EXP_SHL: return N(node, i32(x) << (u32(y) & 0x1F)); + case EXP_SHR: return N(node, i32(x) >> (u32(y) & 0x1F)); + case EXP_USHR: return N(node, u32(x) >> (u32(y) & 0x1F)); + case EXP_BITAND: return N(node, i32(x) & i32(y)); + case EXP_BITXOR: return N(node, i32(x) ^ i32(y)); + case EXP_BITOR: return N(node, i32(x) | i32(y)); + } } } @@ -81,6 +74,5 @@ static int foldnumber(js_Ast *node, double *r) void jsP_optimize(js_State *J, js_Ast *prog) { - double x; - foldnumber(prog, &x); + foldnumber(prog); }