Browse Source

convert compare helper explicit args to a flags field, quite modest footprint savings

pull/2/head
Sami Vaarala 11 years ago
parent
commit
7a523747ef
  1. 16
      src/duk_js.h
  2. 13
      src/duk_js_executor.c
  3. 12
      src/duk_js_ops.c

16
src/duk_js.h

@ -13,10 +13,14 @@
#define DUK_CALL_FLAG_IS_TAILCALL (1 << 4) /* duk_handle_ecma_call_setup: setup for a tailcall */
#define DUK_CALL_FLAG_DIRECT_EVAL (1 << 5) /* call is a direct eval call */
/* Flags for duk__js_equals_helper(). */
/* Flags for duk_js_equals_helper(). */
#define DUK_EQUALS_FLAG_SAMEVALUE (1 << 0) /* use SameValue instead of non-strict equality */
#define DUK_EQUALS_FLAG_STRICT (1 << 1) /* use strict equality instead of non-strict equality */
/* Flags for duk_js_compare_helper(). */
#define DUK_COMPARE_FLAG_EVAL_LEFT_FIRST (1 << 0) /* eval left argument first */
#define DUK_COMPARE_FLAG_NEGATE (1 << 1) /* negate result */
/* conversions, coercions, comparison, etc */
int duk_js_toboolean(duk_tval *tv);
double duk_js_tonumber(duk_hthread *thr, duk_tval *tv);
@ -32,7 +36,7 @@ duk_small_int_t duk_js_to_arrayindex_raw_string(duk_uint8_t *str, duk_uint32_t b
duk_uint32_t duk_js_to_arrayindex_string_helper(duk_hstring *h);
int duk_js_equals_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_int_t flags);
int duk_js_string_compare(duk_hstring *h1, duk_hstring *h2);
int duk_js_compare_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, int eval_left_first, int negate);
int duk_js_compare_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_int_t flags);
int duk_js_lessthan(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y);
int duk_js_greaterthan(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y);
int duk_js_lessthanorequal(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y);
@ -50,19 +54,19 @@ duk_hstring *duk_js_typeof(duk_hthread *thr, duk_tval *tv_x);
/* E5 Sections 11.8.1, 11.8.5; x < y */
#define duk_js_lessthan(thr,tv_x,tv_y) \
duk_js_compare_helper((thr), (tv_x), (tv_Y), 1, 0)
duk_js_compare_helper((thr), (tv_x), (tv_Y), DUK_COMPARE_FLAG_EVAL_LEFT_FIRST)
/* E5 Sections 11.8.2, 11.8.5; x > y --> y < x */
#define duk_js_greaterthan(thr,tv_x,tv_y) \
duk_js_compare_helper((thr), (tv_y), (tv_x), 0, 0)
duk_js_compare_helper((thr), (tv_y), (tv_x), 0)
/* E5 Sections 11.8.3, 11.8.5; x <= y --> not (x > y) --> not (y < x) */
#define duk_js_lessthanorequal(thr,tv_x,tv_y) \
duk_js_compare_helper((thr), (tv_y), (tv_x), 0, 1);
duk_js_compare_helper((thr), (tv_y), (tv_x), DUK_COMPARE_FLAG_NEGATE)
/* E5 Sections 11.8.4, 11.8.5; x >= y --> not (x < y) */
#define duk_js_greaterthanorequal(thr,tv_x,tv_y) \
duk_js_compare_helper((thr), (tv_x), (tv_y), 1, 1);
duk_js_compare_helper((thr), (tv_x), (tv_y), DUK_COMPARE_FLAG_EVAL_LEFT_FIRST | DUK_COMPARE_FLAG_NEGATE)
/* identifiers and environment handling */
int duk_js_getvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name, int throw_flag);

13
src/duk_js_executor.c

@ -2398,8 +2398,7 @@ void duk_js_execute_bytecode(duk_hthread *entry_thread) {
tmp = duk_js_compare_helper(thr,
DUK__REGCONSTP(c), /* y */
DUK__REGCONSTP(b), /* x */
0, /* eval_left_first */
0); /* negate */
0); /* flags */
duk_push_boolean(ctx, tmp);
duk_replace(ctx, a);
@ -2417,8 +2416,8 @@ void duk_js_execute_bytecode(duk_hthread *entry_thread) {
tmp = duk_js_compare_helper(thr,
DUK__REGCONSTP(b), /* x */
DUK__REGCONSTP(c), /* y */
1, /* eval_left_first */
1); /* negate */
DUK_COMPARE_FLAG_EVAL_LEFT_FIRST |
DUK_COMPARE_FLAG_NEGATE); /* flags */
duk_push_boolean(ctx, tmp);
duk_replace(ctx, a);
@ -2436,8 +2435,7 @@ void duk_js_execute_bytecode(duk_hthread *entry_thread) {
tmp = duk_js_compare_helper(thr,
DUK__REGCONSTP(b), /* x */
DUK__REGCONSTP(c), /* y */
1, /* eval_left_first */
0); /* negate */
DUK_COMPARE_FLAG_EVAL_LEFT_FIRST); /* flags */
duk_push_boolean(ctx, tmp);
duk_replace(ctx, a);
@ -2455,8 +2453,7 @@ void duk_js_execute_bytecode(duk_hthread *entry_thread) {
tmp = duk_js_compare_helper(thr,
DUK__REGCONSTP(c), /* y */
DUK__REGCONSTP(b), /* x */
0, /* eval_left_first */
1); /* negate */
DUK_COMPARE_FLAG_NEGATE); /* flags */
duk_push_boolean(ctx, tmp);
duk_replace(ctx, a);

12
src/duk_js_ops.c

@ -710,8 +710,6 @@ int duk_js_equals_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_s
* flags to get the rest.
*/
/* FIXME: join flags into one integer argument? */
/* FIXME: this should probably just operate on the stack top, because it
* needs to push stuff on the stack anyway...
*/
@ -770,7 +768,7 @@ int duk_js_string_compare(duk_hstring *h1, duk_hstring *h2) {
return 0;
}
int duk_js_compare_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, int eval_left_first, int negate) {
int duk_js_compare_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_int_t flags) {
duk_context *ctx = (duk_context *) thr;
double d1, d2;
int c1, c2;
@ -781,7 +779,7 @@ int duk_js_compare_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, int
duk_push_tval(ctx, tv_x);
duk_push_tval(ctx, tv_y);
if (eval_left_first) {
if (flags & DUK_COMPARE_FLAG_EVAL_LEFT_FIRST) {
duk_to_primitive(ctx, -2, DUK_HINT_NUMBER);
duk_to_primitive(ctx, -1, DUK_HINT_NUMBER);
} else {
@ -810,7 +808,7 @@ int duk_js_compare_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, int
* preserve it just in case.
*/
if (eval_left_first) {
if (flags & DUK_COMPARE_FLAG_EVAL_LEFT_FIRST) {
d1 = duk_to_number(ctx, -2);
d2 = duk_to_number(ctx, -1);
} else {
@ -873,7 +871,7 @@ int duk_js_compare_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, int
goto cleanup;
lt_true:
if (negate) {
if (flags & DUK_COMPARE_FLAG_NEGATE) {
retval = 0;
goto cleanup;
} else {
@ -883,7 +881,7 @@ int duk_js_compare_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, int
/* never here */
lt_false:
if (negate) {
if (flags & DUK_COMPARE_FLAG_NEGATE) {
retval = 1;
goto cleanup;
} else {

Loading…
Cancel
Save