From 1004b390788410dedccb2dbeabc6481efc25c2e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Minisini?= Date: Fri, 7 Oct 2022 13:10:22 +0200 Subject: [PATCH] Optimization of small integer floating point constants. [COMPILER] * OPT: Optimization of small integer floating point constants. [INTEGER] * OPT: Optimization of small integer floating point constants. [GB.JIT] * OPT: Optimization of small integer floating point constants. --- main/gbc/gbc_trans_expr.c | 4 ++++ main/gbx/gbx_exec_loop.c | 13 +++++++++++-- main/lib/eval/eval_trans_expr.c | 4 ++++ main/lib/jit/jit_body.c | 10 +++++++++- main/share/gb_code.h | 1 + main/share/gb_code_temp.h | 9 +++++++++ main/share/gb_pcode.h | 1 + main/share/gb_pcode_temp.h | 8 ++++++++ 8 files changed, 47 insertions(+), 3 deletions(-) diff --git a/main/gbc/gbc_trans_expr.c b/main/gbc/gbc_trans_expr.c index 2b7684b66..517f60e3c 100644 --- a/main/gbc/gbc_trans_expr.c +++ b/main/gbc/gbc_trans_expr.c @@ -184,6 +184,10 @@ static void push_number(int index) { CODE_push_number(number.ival); } + else if (number.type == T_FLOAT && COMP_version >= 0x03180000 && number.dval == (double)(int)number.dval && number.dval >= -128 && number.dval <= 127) + { + CODE_push_float(number.dval); + } else { CLEAR(&decl); diff --git a/main/gbx/gbx_exec_loop.c b/main/gbx/gbx_exec_loop.c index ebf7b8514..9a2e0b878 100644 --- a/main/gbx/gbx_exec_loop.c +++ b/main/gbx/gbx_exec_loop.c @@ -604,7 +604,7 @@ void EXEC_loop(void) /* F4 PUSH QUICK */ &&_PUSH_QUICK, /* F5 PUSH QUICK */ &&_PUSH_VARIABLE, /* F6 PUSH QUICK */ &&_POP_VARIABLE, - /* F7 PUSH QUICK */ &&_PUSH_QUICK, + /* F7 PUSH QUICK */ &&_PUSH_FLOAT, /* F8 PUSH QUICK */ &&_PUSH_QUICK, /* F9 PUSH QUICK */ &&_POP_LOCAL_NOREF, /* FA PUSH QUICK */ &&_POP_PARAM_NOREF, @@ -642,7 +642,7 @@ void EXEC_loop(void) /* F4 PUSH QUICK */ &&_PUSH_QUICK, /* F5 PUSH QUICK */ &&_PUSH_VARIABLE, /* F6 PUSH QUICK */ &&_POP_VARIABLE, - /* F7 PUSH QUICK */ &&_PUSH_QUICK, + /* F7 PUSH QUICK */ &&_PUSH_FLOAT, /* F8 PUSH QUICK */ &&_PUSH_QUICK, /* F9 PUSH QUICK */ &&_POP_LOCAL_NOREF, /* FA PUSH QUICK */ &&_POP_PARAM_NOREF, @@ -1902,6 +1902,15 @@ _PUSH_QUICK: /*-----------------------------------------------*/ +_PUSH_FLOAT: + + SP->type = T_FLOAT; + SP->_float.value = GET_XX(); + SP++; + goto _NEXT; + +/*-----------------------------------------------*/ + _ADD_QUICK: { diff --git a/main/lib/eval/eval_trans_expr.c b/main/lib/eval/eval_trans_expr.c index c8343c7eb..2db421549 100644 --- a/main/lib/eval/eval_trans_expr.c +++ b/main/lib/eval/eval_trans_expr.c @@ -83,6 +83,10 @@ static void push_number(int index) { CODE_push_number(number.ival); } + else if (number.type == T_FLOAT && number.dval == (double)(int)number.dval && number.dval >= -128 && number.dval <= 127) + { + CODE_push_float(number.dval); + } else { cst.type = number.type; diff --git a/main/lib/jit/jit_body.c b/main/lib/jit/jit_body.c index 68341517f..3f1d9dbd0 100644 --- a/main/lib/jit/jit_body.c +++ b/main/lib/jit/jit_body.c @@ -2875,7 +2875,7 @@ bool JIT_translate_body(FUNCTION *func, int ind) /* F4 PUSH QUICK */ &&_PUSH_QUICK, /* F5 PUSH QUICK */ &&_PUSH_QUICK, /* F6 PUSH QUICK */ &&_PUSH_QUICK, - /* F7 PUSH QUICK */ &&_PUSH_QUICK, + /* F7 PUSH QUICK */ &&_PUSH_FLOAT, /* F8 PUSH QUICK */ &&_PUSH_QUICK, /* F9 PUSH QUICK */ &&_POP_LOCAL_NOREF, /* FA PUSH QUICK */ &&_POP_PARAM_NOREF, @@ -3033,6 +3033,14 @@ _PUSH_QUICK: push(T_INTEGER, "%d", GET_XXX()); goto _MAIN; +_PUSH_FLOAT: + + if (class->not_3_18) + goto _PUSH_QUICK; + + push(T_FLOAT, "%d", GET_XX()); + goto _MAIN; + _PUSH_SHORT: push(T_INTEGER, "%d", (short)PC[0]); diff --git a/main/share/gb_code.h b/main/share/gb_code.h index 229ff001c..a72e09c66 100644 --- a/main/share/gb_code.h +++ b/main/share/gb_code.h @@ -121,6 +121,7 @@ void CODE_ignore_next_stack_usage(void); void CODE_dump(PCODE *code, int count); void CODE_push_number(int value); +void CODE_push_float(int value); void CODE_push_const(ushort value); void CODE_push_local_ref(short num, bool noref); diff --git a/main/share/gb_code_temp.h b/main/share/gb_code_temp.h index 13bedaf66..5692ecc2b 100644 --- a/main/share/gb_code_temp.h +++ b/main/share/gb_code_temp.h @@ -455,6 +455,15 @@ void CODE_push_number(int value) } } +void CODE_push_float(int value) +{ + LAST_CODE; + + use_stack(1); + + write_ZZxx(C_PUSH_FLOAT, value); +} + void CODE_push_const(ushort value) { diff --git a/main/share/gb_pcode.h b/main/share/gb_pcode.h index 766310bd4..50d052f48 100644 --- a/main/share/gb_pcode.h +++ b/main/share/gb_pcode.h @@ -34,6 +34,7 @@ #define C_PUSH_PARAM_NOREF 0xF200 #define C_PUSH_VARIABLE 0xF500 #define C_POP_VARIABLE 0xF600 +#define C_PUSH_FLOAT 0xF700 #define C_POP_LOCAL_NOREF 0xF900 #define C_POP_PARAM_NOREF 0xFA00 #define C_POP_LOCAL_FAST 0xFB00 diff --git a/main/share/gb_pcode_temp.h b/main/share/gb_pcode_temp.h index 8607c34ab..f1976b0a4 100644 --- a/main/share/gb_pcode_temp.h +++ b/main/share/gb_pcode_temp.h @@ -172,6 +172,10 @@ short PCODE_dump(FILE *out, ushort addr, PCODE *code) fprintf(out, "POP PARAM FAST %d", (char)(op & 0xFF)); break; + case C_PUSH_FLOAT: + fprintf(out, "PUSH FLOAT %d", (char)(op & 0xFF)); + break; + default: fprintf(out, "PUSH QUICK %d", (short)value); } @@ -262,6 +266,10 @@ short PCODE_dump(FILE *out, ushort addr, PCODE *code) fprintf(out, "POP PARAM FAST %d", (char)(op & 0xFF)); break; + case C_PUSH_FLOAT: + fprintf(out, "PUSH FLOAT %d", (char)(op & 0xFF)); + break; + default: fprintf(out, "PUSH QUICK %d", (short)value); }